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/data_type_controller.h"
15 #include "chrome/browser/sync/glue/shared_change_processor.h"
18 class ProfileSyncService;
19 class ProfileSyncComponentsFactory;
22 class SyncableService;
25 namespace browser_sync {
27 class NonUIDataTypeController : public DataTypeController {
29 NonUIDataTypeController(
30 ProfileSyncComponentsFactory* profile_sync_factory,
32 ProfileSyncService* sync_service);
34 // DataTypeController interface.
35 virtual void LoadModels(
36 const ModelLoadCallback& model_load_callback) OVERRIDE;
37 virtual void StartAssociating(const StartCallback& start_callback) OVERRIDE;
38 virtual void Stop() OVERRIDE;
39 virtual syncer::ModelType type() const = 0;
40 virtual syncer::ModelSafeGroup model_safe_group() const = 0;
41 virtual std::string name() const OVERRIDE;
42 virtual State state() const OVERRIDE;
43 virtual void OnSingleDatatypeUnrecoverableError(
44 const tracked_objects::Location& from_here,
45 const std::string& message) OVERRIDE;
49 NonUIDataTypeController();
50 // DataTypeController is RefCounted.
51 virtual ~NonUIDataTypeController();
53 // DataTypeController interface.
54 virtual void OnModelLoaded() OVERRIDE;
56 // Start any dependent services that need to be running before we can
57 // associate models. The default implementation is a no-op.
59 // True - if models are ready and association can proceed.
60 // False - if models are not ready. StartAssociationAsync should be called
61 // when the models are ready.
62 // Note: this is performed on the UI thread.
63 virtual bool StartModels();
65 // Perform any DataType controller specific state cleanup before stopping
66 // the datatype controller. The default implementation is a no-op.
67 // Note: this is performed on the UI thread.
68 virtual void StopModels();
70 // Posts the given task to the backend thread, i.e. the thread the
71 // datatype lives on. Return value: True if task posted successfully,
73 virtual bool PostTaskOnBackendThread(
74 const tracked_objects::Location& from_here,
75 const base::Closure& task) = 0;
77 // Start up complete, update the state and invoke the callback.
78 // Note: this is performed on the datatype's thread.
79 virtual void StartDone(
80 DataTypeController::StartResult start_result,
81 const syncer::SyncMergeResult& local_merge_result,
82 const syncer::SyncMergeResult& syncer_merge_result);
84 // UI thread implementation of StartDone.
85 virtual void StartDoneImpl(
86 DataTypeController::StartResult start_result,
87 DataTypeController::State new_state,
88 const syncer::SyncMergeResult& local_merge_result,
89 const syncer::SyncMergeResult& syncer_merge_result);
91 // Kick off the association process.
92 virtual bool StartAssociationAsync();
94 // Record association time.
95 virtual void RecordAssociationTime(base::TimeDelta time);
97 // Record causes of start failure.
98 virtual void RecordStartFailure(StartResult result);
100 Profile* profile() const { return profile_; }
104 // Posted on the backend thread by StartAssociationAsync().
105 void StartAssociationWithSharedChangeProcessor(
106 const scoped_refptr<SharedChangeProcessor>& shared_change_processor);
108 // Calls Disconnect() on |shared_change_processor_|, then sets it to
109 // NULL. Must be called only by StartDoneImpl() or Stop() (on the
110 // UI thread) and only after a call to Start() (i.e.,
111 // |shared_change_processor_| must be non-NULL).
112 void ClearSharedChangeProcessor();
114 // Posts StopLocalService() to the datatype's thread.
115 void StopLocalServiceAsync();
117 // Calls local_service_->StopSyncing() and releases our references to it.
118 void StopLocalService();
120 // Abort model loading and trigger the model load callback.
121 void AbortModelLoad();
123 // Disable this type with the sync service. Should only be invoked in case of
124 // an unrecoverable error.
125 // Note: this is performed on the UI thread.
126 void DisableImpl(const tracked_objects::Location& from_here,
127 const std::string& message);
129 ProfileSyncComponentsFactory* const profile_sync_factory_;
130 Profile* const profile_;
131 ProfileSyncService* const sync_service_;
133 // State of this datatype controller.
136 // Callbacks for use when starting the datatype.
137 StartCallback start_callback_;
138 ModelLoadCallback model_load_callback_;
140 // The shared change processor is the thread-safe interface to the
141 // datatype. We hold a reference to it from the UI thread so that
142 // we can call Disconnect() on it from Stop()/StartDoneImpl(). Most
143 // of the work is done on the backend thread, and in
144 // StartAssociationWithSharedChangeProcessor() for this class in
147 // Lifetime: The SharedChangeProcessor object is created on the UI
148 // thread and passed on to the backend thread. This reference is
149 // released on the UI thread in Stop()/StartDoneImpl(), but the
150 // backend thread may still have references to it (which is okay,
151 // since we call Disconnect() before releasing the UI thread
153 scoped_refptr<SharedChangeProcessor> shared_change_processor_;
155 // A weak pointer to the actual local syncable service, which performs all the
156 // real work. We do not own the object, and it is only safe to access on the
157 // DataType's thread.
158 // Lifetime: it gets set in StartAssociationWithSharedChangeProcessor(...)
159 // and released in StopLocalService().
160 base::WeakPtr<syncer::SyncableService> local_service_;
163 } // namespace browser_sync
165 #endif // CHROME_BROWSER_SYNC_GLUE_NON_UI_DATA_TYPE_CONTROLLER_H_