1 // Copyright (c) 2012 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 CHROME_BROWSER_SYNC_GLUE_NON_UI_DATA_TYPE_CONTROLLER_H_
6 #define CHROME_BROWSER_SYNC_GLUE_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 "chrome/browser/sync/glue/shared_change_processor.h"
15 #include "components/sync_driver/data_type_controller.h"
18 class ProfileSyncService;
19 class ProfileSyncComponentsFactory;
22 class SyncableService;
25 namespace browser_sync {
27 class NonUIDataTypeController : public DataTypeController {
29 NonUIDataTypeController(
30 scoped_refptr<base::MessageLoopProxy> ui_thread,
31 const base::Closure& error_callback,
32 ProfileSyncComponentsFactory* profile_sync_factory,
34 ProfileSyncService* sync_service);
36 // DataTypeController interface.
37 virtual void LoadModels(
38 const ModelLoadCallback& model_load_callback) OVERRIDE;
39 virtual void StartAssociating(const StartCallback& start_callback) OVERRIDE;
40 virtual void Stop() OVERRIDE;
41 virtual syncer::ModelType type() const = 0;
42 virtual syncer::ModelSafeGroup model_safe_group() const = 0;
43 virtual ChangeProcessor* GetChangeProcessor() const OVERRIDE;
44 virtual std::string name() const OVERRIDE;
45 virtual State state() const OVERRIDE;
46 virtual void OnSingleDatatypeUnrecoverableError(
47 const tracked_objects::Location& from_here,
48 const std::string& message) OVERRIDE;
52 NonUIDataTypeController();
53 // DataTypeController is RefCounted.
54 virtual ~NonUIDataTypeController();
56 // DataTypeController interface.
57 virtual void OnModelLoaded() OVERRIDE;
59 // Start any dependent services that need to be running before we can
60 // associate models. The default implementation is a no-op.
62 // True - if models are ready and association can proceed.
63 // False - if models are not ready. StartAssociationAsync should be called
64 // when the models are ready.
65 // Note: this is performed on the UI thread.
66 virtual bool StartModels();
68 // Perform any DataType controller specific state cleanup before stopping
69 // the datatype controller. The default implementation is a no-op.
70 // Note: this is performed on the UI thread.
71 virtual void StopModels();
73 // Posts the given task to the backend thread, i.e. the thread the
74 // datatype lives on. Return value: True if task posted successfully,
76 virtual bool PostTaskOnBackendThread(
77 const tracked_objects::Location& from_here,
78 const base::Closure& task) = 0;
80 // Start up complete, update the state and invoke the callback.
81 // Note: this is performed on the datatype's thread.
82 virtual void StartDone(
83 DataTypeController::StartResult start_result,
84 const syncer::SyncMergeResult& local_merge_result,
85 const syncer::SyncMergeResult& syncer_merge_result);
87 // UI thread implementation of StartDone.
88 virtual void StartDoneImpl(
89 DataTypeController::StartResult start_result,
90 DataTypeController::State new_state,
91 const syncer::SyncMergeResult& local_merge_result,
92 const syncer::SyncMergeResult& syncer_merge_result);
94 // Kick off the association process.
95 virtual bool StartAssociationAsync();
97 // Record association time.
98 virtual void RecordAssociationTime(base::TimeDelta time);
100 // Record causes of start failure.
101 virtual void RecordStartFailure(StartResult result);
103 // To allow unit tests to control thread interaction during non-ui startup
104 // and shutdown, use a factory method to create the SharedChangeProcessor.
105 virtual SharedChangeProcessor* CreateSharedChangeProcessor();
107 Profile* profile() const { return profile_; }
111 // Posted on the backend thread by StartAssociationAsync().
112 void StartAssociationWithSharedChangeProcessor(
113 const scoped_refptr<SharedChangeProcessor>& shared_change_processor);
115 // Calls Disconnect() on |shared_change_processor_|, then sets it to
116 // NULL. Must be called only by StartDoneImpl() or Stop() (on the
117 // UI thread) and only after a call to Start() (i.e.,
118 // |shared_change_processor_| must be non-NULL).
119 void ClearSharedChangeProcessor();
121 // Posts StopLocalService() to the datatype's thread.
122 void StopLocalServiceAsync();
124 // Calls local_service_->StopSyncing() and releases our references to it.
125 void StopLocalService();
127 // Abort model loading and trigger the model load callback.
128 void AbortModelLoad();
130 // Disable this type with the sync service. Should only be invoked in case of
131 // an unrecoverable error.
132 // Note: this is performed on the UI thread.
133 void DisableImpl(const tracked_objects::Location& from_here,
134 const std::string& message);
136 ProfileSyncComponentsFactory* const profile_sync_factory_;
137 Profile* const profile_;
138 ProfileSyncService* const sync_service_;
140 // State of this datatype controller.
143 // Callbacks for use when starting the datatype.
144 StartCallback start_callback_;
145 ModelLoadCallback model_load_callback_;
147 // The shared change processor is the thread-safe interface to the
148 // datatype. We hold a reference to it from the UI thread so that
149 // we can call Disconnect() on it from Stop()/StartDoneImpl(). Most
150 // of the work is done on the backend thread, and in
151 // StartAssociationWithSharedChangeProcessor() for this class in
154 // Lifetime: The SharedChangeProcessor object is created on the UI
155 // thread and passed on to the backend thread. This reference is
156 // released on the UI thread in Stop()/StartDoneImpl(), but the
157 // backend thread may still have references to it (which is okay,
158 // since we call Disconnect() before releasing the UI thread
160 scoped_refptr<SharedChangeProcessor> shared_change_processor_;
162 // The UserShare to connect the SharedChangeProcessor to. NULL until set in
164 syncer::UserShare* user_share_;
166 // A weak pointer to the actual local syncable service, which performs all the
167 // real work. We do not own the object, and it is only safe to access on the
168 // DataType's thread.
169 // Lifetime: it gets set in StartAssociationWithSharedChangeProcessor(...)
170 // and released in StopLocalService().
171 base::WeakPtr<syncer::SyncableService> local_service_;
174 } // namespace browser_sync
176 #endif // CHROME_BROWSER_SYNC_GLUE_NON_UI_DATA_TYPE_CONTROLLER_H_