1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef COMPONENTS_SYNC_DRIVER_NON_UI_DATA_TYPE_CONTROLLER_H_
6 #define COMPONENTS_SYNC_DRIVER_NON_UI_DATA_TYPE_CONTROLLER_H_
10 #include "base/basictypes.h"
11 #include "base/compiler_specific.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/weak_ptr.h"
14 #include "components/sync_driver/data_type_controller.h"
15 #include "components/sync_driver/shared_change_processor.h"
18 class SyncableService;
21 namespace sync_driver {
23 class SyncApiComponentFactory;
25 class NonUIDataTypeController : public DataTypeController {
27 NonUIDataTypeController(
28 scoped_refptr<base::MessageLoopProxy> ui_thread,
29 const base::Closure& error_callback,
30 SyncApiComponentFactory* sync_factory);
32 // DataTypeController interface.
33 void LoadModels(const ModelLoadCallback& model_load_callback) override;
34 void StartAssociating(const StartCallback& start_callback) override;
36 virtual syncer::ModelType type() const = 0;
37 virtual syncer::ModelSafeGroup model_safe_group() const = 0;
38 ChangeProcessor* GetChangeProcessor() const override;
39 std::string name() const override;
40 State state() const override;
41 void OnSingleDataTypeUnrecoverableError(
42 const syncer::SyncError& error) override;
46 NonUIDataTypeController();
47 // DataTypeController is RefCounted.
48 ~NonUIDataTypeController() override;
50 // DataTypeController interface.
51 void OnModelLoaded() override;
53 // Start any dependent services that need to be running before we can
54 // associate models. The default implementation is a no-op.
56 // True - if models are ready and association can proceed.
57 // False - if models are not ready. StartAssociationAsync should be called
58 // when the models are ready.
59 // Note: this is performed on the UI thread.
60 virtual bool StartModels();
62 // Perform any DataType controller specific state cleanup before stopping
63 // the datatype controller. The default implementation is a no-op.
64 // Note: this is performed on the UI thread.
65 virtual void StopModels();
67 // Posts the given task to the backend thread, i.e. the thread the
68 // datatype lives on. Return value: True if task posted successfully,
70 virtual bool PostTaskOnBackendThread(
71 const tracked_objects::Location& from_here,
72 const base::Closure& task) = 0;
74 // Start up complete, update the state and invoke the callback.
75 // Note: this is performed on the datatype's thread.
76 virtual void StartDone(
77 DataTypeController::ConfigureResult start_result,
78 const syncer::SyncMergeResult& local_merge_result,
79 const syncer::SyncMergeResult& syncer_merge_result);
81 // UI thread implementation of StartDone.
82 virtual void StartDoneImpl(
83 DataTypeController::ConfigureResult start_result,
84 DataTypeController::State new_state,
85 const syncer::SyncMergeResult& local_merge_result,
86 const syncer::SyncMergeResult& syncer_merge_result);
88 // Kick off the association process.
89 virtual bool StartAssociationAsync();
91 // Record association time.
92 virtual void RecordAssociationTime(base::TimeDelta time);
94 // Record causes of start failure.
95 virtual void RecordStartFailure(ConfigureResult result);
97 // To allow unit tests to control thread interaction during non-ui startup
98 // and shutdown, use a factory method to create the SharedChangeProcessor.
99 virtual SharedChangeProcessor* CreateSharedChangeProcessor();
103 // Posted on the backend thread by StartAssociationAsync().
104 void StartAssociationWithSharedChangeProcessor(
105 const scoped_refptr<SharedChangeProcessor>& shared_change_processor);
107 // Calls Disconnect() on |shared_change_processor_|, then sets it to
108 // NULL. Must be called only by StartDoneImpl() or Stop() (on the
109 // UI thread) and only after a call to Start() (i.e.,
110 // |shared_change_processor_| must be non-NULL).
111 void ClearSharedChangeProcessor();
113 // Posts StopLocalService() to the datatype's thread.
114 void StopLocalServiceAsync();
116 // Calls local_service_->StopSyncing() and releases our references to it.
117 void StopLocalService();
119 // Abort model loading and trigger the model load callback.
120 void AbortModelLoad();
122 // Disable this type with the sync service. Should only be invoked in case of
123 // an unrecoverable error.
124 // Note: this is performed on the UI thread.
125 void DisableImpl(const syncer::SyncError& error);
127 SyncApiComponentFactory* const sync_factory_;
129 // State of this datatype controller.
132 // Callbacks for use when starting the datatype.
133 StartCallback start_callback_;
134 ModelLoadCallback model_load_callback_;
136 // The shared change processor is the thread-safe interface to the
137 // datatype. We hold a reference to it from the UI thread so that
138 // we can call Disconnect() on it from Stop()/StartDoneImpl(). Most
139 // of the work is done on the backend thread, and in
140 // StartAssociationWithSharedChangeProcessor() for this class in
143 // Lifetime: The SharedChangeProcessor object is created on the UI
144 // thread and passed on to the backend thread. This reference is
145 // released on the UI thread in Stop()/StartDoneImpl(), but the
146 // backend thread may still have references to it (which is okay,
147 // since we call Disconnect() before releasing the UI thread
149 scoped_refptr<SharedChangeProcessor> shared_change_processor_;
151 // A weak pointer to the actual local syncable service, which performs all the
152 // real work. We do not own the object, and it is only safe to access on the
153 // DataType's thread.
154 // Lifetime: it gets set in StartAssociationWithSharedChangeProcessor(...)
155 // and released in StopLocalService().
156 base::WeakPtr<syncer::SyncableService> local_service_;
158 scoped_refptr<base::MessageLoopProxy> ui_thread_;
161 } // namespace sync_driver
163 #endif // COMPONENTS_SYNC_DRIVER_NON_UI_DATA_TYPE_CONTROLLER_H_