Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / components / sync_driver / non_ui_data_type_controller.h
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.
4
5 #ifndef COMPONENTS_SYNC_DRIVER_NON_UI_DATA_TYPE_CONTROLLER_H_
6 #define COMPONENTS_SYNC_DRIVER_NON_UI_DATA_TYPE_CONTROLLER_H_
7
8 #include <string>
9
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"
16
17 namespace syncer {
18 class SyncableService;
19 }
20
21 namespace sync_driver {
22
23 class SyncApiComponentFactory;
24
25 class NonUIDataTypeController : public DataTypeController {
26  public:
27   NonUIDataTypeController(
28       scoped_refptr<base::MessageLoopProxy> ui_thread,
29       const base::Closure& error_callback,
30       SyncApiComponentFactory* sync_factory);
31
32   // DataTypeController interface.
33   void LoadModels(const ModelLoadCallback& model_load_callback) override;
34   void StartAssociating(const StartCallback& start_callback) override;
35   void Stop() 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;
43
44  protected:
45   // For testing only.
46   NonUIDataTypeController();
47   // DataTypeController is RefCounted.
48   ~NonUIDataTypeController() override;
49
50   // DataTypeController interface.
51   void OnModelLoaded() override;
52
53   // Start any dependent services that need to be running before we can
54   // associate models. The default implementation is a no-op.
55   // Return value:
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();
61
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();
66
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,
69   // false otherwise.
70   virtual bool PostTaskOnBackendThread(
71       const tracked_objects::Location& from_here,
72       const base::Closure& task) = 0;
73
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);
80
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);
87
88   // Kick off the association process.
89   virtual bool StartAssociationAsync();
90
91   // Record association time.
92   virtual void RecordAssociationTime(base::TimeDelta time);
93
94   // Record causes of start failure.
95   virtual void RecordStartFailure(ConfigureResult result);
96
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();
100
101  private:
102
103   // Posted on the backend thread by StartAssociationAsync().
104   void StartAssociationWithSharedChangeProcessor(
105       const scoped_refptr<SharedChangeProcessor>& shared_change_processor);
106
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();
112
113   // Posts StopLocalService() to the datatype's thread.
114   void StopLocalServiceAsync();
115
116   // Calls local_service_->StopSyncing() and releases our references to it.
117   void StopLocalService();
118
119   // Abort model loading and trigger the model load callback.
120   void AbortModelLoad();
121
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);
126
127   SyncApiComponentFactory* const sync_factory_;
128
129   // State of this datatype controller.
130   State state_;
131
132   // Callbacks for use when starting the datatype.
133   StartCallback start_callback_;
134   ModelLoadCallback model_load_callback_;
135
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
141   // particular.
142   //
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
148   // reference).
149   scoped_refptr<SharedChangeProcessor> shared_change_processor_;
150
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_;
157
158   scoped_refptr<base::MessageLoopProxy> ui_thread_;
159 };
160
161 }  // namespace sync_driver
162
163 #endif  // COMPONENTS_SYNC_DRIVER_NON_UI_DATA_TYPE_CONTROLLER_H_