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 virtual void LoadModels(
34 const ModelLoadCallback& model_load_callback) OVERRIDE;
35 virtual void StartAssociating(const StartCallback& start_callback) OVERRIDE;
36 virtual void Stop() OVERRIDE;
37 virtual syncer::ModelType type() const = 0;
38 virtual syncer::ModelSafeGroup model_safe_group() const = 0;
39 virtual ChangeProcessor* GetChangeProcessor() const OVERRIDE;
40 virtual std::string name() const OVERRIDE;
41 virtual State state() const OVERRIDE;
42 virtual void OnSingleDataTypeUnrecoverableError(
43 const syncer::SyncError& error) OVERRIDE;
47 NonUIDataTypeController();
48 // DataTypeController is RefCounted.
49 virtual ~NonUIDataTypeController();
51 // DataTypeController interface.
52 virtual void OnModelLoaded() OVERRIDE;
54 // Start any dependent services that need to be running before we can
55 // associate models. The default implementation is a no-op.
57 // True - if models are ready and association can proceed.
58 // False - if models are not ready. StartAssociationAsync should be called
59 // when the models are ready.
60 // Note: this is performed on the UI thread.
61 virtual bool StartModels();
63 // Perform any DataType controller specific state cleanup before stopping
64 // the datatype controller. The default implementation is a no-op.
65 // Note: this is performed on the UI thread.
66 virtual void StopModels();
68 // Posts the given task to the backend thread, i.e. the thread the
69 // datatype lives on. Return value: True if task posted successfully,
71 virtual bool PostTaskOnBackendThread(
72 const tracked_objects::Location& from_here,
73 const base::Closure& task) = 0;
75 // Start up complete, update the state and invoke the callback.
76 // Note: this is performed on the datatype's thread.
77 virtual void StartDone(
78 DataTypeController::ConfigureResult start_result,
79 const syncer::SyncMergeResult& local_merge_result,
80 const syncer::SyncMergeResult& syncer_merge_result);
82 // UI thread implementation of StartDone.
83 virtual void StartDoneImpl(
84 DataTypeController::ConfigureResult start_result,
85 DataTypeController::State new_state,
86 const syncer::SyncMergeResult& local_merge_result,
87 const syncer::SyncMergeResult& syncer_merge_result);
89 // Kick off the association process.
90 virtual bool StartAssociationAsync();
92 // Record association time.
93 virtual void RecordAssociationTime(base::TimeDelta time);
95 // Record causes of start failure.
96 virtual void RecordStartFailure(ConfigureResult result);
98 // To allow unit tests to control thread interaction during non-ui startup
99 // and shutdown, use a factory method to create the SharedChangeProcessor.
100 virtual SharedChangeProcessor* CreateSharedChangeProcessor();
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 syncer::SyncError& error);
128 SyncApiComponentFactory* const sync_factory_;
130 // State of this datatype controller.
133 // Callbacks for use when starting the datatype.
134 StartCallback start_callback_;
135 ModelLoadCallback model_load_callback_;
137 // The shared change processor is the thread-safe interface to the
138 // datatype. We hold a reference to it from the UI thread so that
139 // we can call Disconnect() on it from Stop()/StartDoneImpl(). Most
140 // of the work is done on the backend thread, and in
141 // StartAssociationWithSharedChangeProcessor() for this class in
144 // Lifetime: The SharedChangeProcessor object is created on the UI
145 // thread and passed on to the backend thread. This reference is
146 // released on the UI thread in Stop()/StartDoneImpl(), but the
147 // backend thread may still have references to it (which is okay,
148 // since we call Disconnect() before releasing the UI thread
150 scoped_refptr<SharedChangeProcessor> shared_change_processor_;
152 // A weak pointer to the actual local syncable service, which performs all the
153 // real work. We do not own the object, and it is only safe to access on the
154 // DataType's thread.
155 // Lifetime: it gets set in StartAssociationWithSharedChangeProcessor(...)
156 // and released in StopLocalService().
157 base::WeakPtr<syncer::SyncableService> local_service_;
159 scoped_refptr<base::MessageLoopProxy> ui_thread_;
162 } // namespace sync_driver
164 #endif // COMPONENTS_SYNC_DRIVER_NON_UI_DATA_TYPE_CONTROLLER_H_