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 std::string name() const OVERRIDE;
44 virtual State state() const OVERRIDE;
45 virtual void OnSingleDatatypeUnrecoverableError(
46 const tracked_objects::Location& from_here,
47 const std::string& message) OVERRIDE;
51 NonUIDataTypeController();
52 // DataTypeController is RefCounted.
53 virtual ~NonUIDataTypeController();
55 // DataTypeController interface.
56 virtual void OnModelLoaded() OVERRIDE;
58 // Start any dependent services that need to be running before we can
59 // associate models. The default implementation is a no-op.
61 // True - if models are ready and association can proceed.
62 // False - if models are not ready. StartAssociationAsync should be called
63 // when the models are ready.
64 // Note: this is performed on the UI thread.
65 virtual bool StartModels();
67 // Perform any DataType controller specific state cleanup before stopping
68 // the datatype controller. The default implementation is a no-op.
69 // Note: this is performed on the UI thread.
70 virtual void StopModels();
72 // Posts the given task to the backend thread, i.e. the thread the
73 // datatype lives on. Return value: True if task posted successfully,
75 virtual bool PostTaskOnBackendThread(
76 const tracked_objects::Location& from_here,
77 const base::Closure& task) = 0;
79 // Start up complete, update the state and invoke the callback.
80 // Note: this is performed on the datatype's thread.
81 virtual void StartDone(
82 DataTypeController::StartResult start_result,
83 const syncer::SyncMergeResult& local_merge_result,
84 const syncer::SyncMergeResult& syncer_merge_result);
86 // UI thread implementation of StartDone.
87 virtual void StartDoneImpl(
88 DataTypeController::StartResult start_result,
89 DataTypeController::State new_state,
90 const syncer::SyncMergeResult& local_merge_result,
91 const syncer::SyncMergeResult& syncer_merge_result);
93 // Kick off the association process.
94 virtual bool StartAssociationAsync();
96 // Record association time.
97 virtual void RecordAssociationTime(base::TimeDelta time);
99 // Record causes of start failure.
100 virtual void RecordStartFailure(StartResult result);
102 Profile* profile() const { return profile_; }
106 // Posted on the backend thread by StartAssociationAsync().
107 void StartAssociationWithSharedChangeProcessor(
108 const scoped_refptr<SharedChangeProcessor>& shared_change_processor);
110 // Calls Disconnect() on |shared_change_processor_|, then sets it to
111 // NULL. Must be called only by StartDoneImpl() or Stop() (on the
112 // UI thread) and only after a call to Start() (i.e.,
113 // |shared_change_processor_| must be non-NULL).
114 void ClearSharedChangeProcessor();
116 // Posts StopLocalService() to the datatype's thread.
117 void StopLocalServiceAsync();
119 // Calls local_service_->StopSyncing() and releases our references to it.
120 void StopLocalService();
122 // Abort model loading and trigger the model load callback.
123 void AbortModelLoad();
125 // Disable this type with the sync service. Should only be invoked in case of
126 // an unrecoverable error.
127 // Note: this is performed on the UI thread.
128 void DisableImpl(const tracked_objects::Location& from_here,
129 const std::string& message);
131 ProfileSyncComponentsFactory* const profile_sync_factory_;
132 Profile* const profile_;
133 ProfileSyncService* const sync_service_;
135 // State of this datatype controller.
138 // Callbacks for use when starting the datatype.
139 StartCallback start_callback_;
140 ModelLoadCallback model_load_callback_;
142 // The shared change processor is the thread-safe interface to the
143 // datatype. We hold a reference to it from the UI thread so that
144 // we can call Disconnect() on it from Stop()/StartDoneImpl(). Most
145 // of the work is done on the backend thread, and in
146 // StartAssociationWithSharedChangeProcessor() for this class in
149 // Lifetime: The SharedChangeProcessor object is created on the UI
150 // thread and passed on to the backend thread. This reference is
151 // released on the UI thread in Stop()/StartDoneImpl(), but the
152 // backend thread may still have references to it (which is okay,
153 // since we call Disconnect() before releasing the UI thread
155 scoped_refptr<SharedChangeProcessor> shared_change_processor_;
157 // A weak pointer to the actual local syncable service, which performs all the
158 // real work. We do not own the object, and it is only safe to access on the
159 // DataType's thread.
160 // Lifetime: it gets set in StartAssociationWithSharedChangeProcessor(...)
161 // and released in StopLocalService().
162 base::WeakPtr<syncer::SyncableService> local_service_;
165 } // namespace browser_sync
167 #endif // CHROME_BROWSER_SYNC_GLUE_NON_UI_DATA_TYPE_CONTROLLER_H_