Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / sync / glue / non_ui_data_type_controller.h
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.
4
5 #ifndef CHROME_BROWSER_SYNC_GLUE_NON_UI_DATA_TYPE_CONTROLLER_H_
6 #define CHROME_BROWSER_SYNC_GLUE_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 "chrome/browser/sync/glue/shared_change_processor.h"
15 #include "components/sync_driver/data_type_controller.h"
16
17 class Profile;
18 class ProfileSyncService;
19 class ProfileSyncComponentsFactory;
20
21 namespace syncer {
22 class SyncableService;
23 }
24
25 namespace browser_sync {
26
27 class NonUIDataTypeController : public DataTypeController {
28  public:
29   NonUIDataTypeController(
30       scoped_refptr<base::MessageLoopProxy> ui_thread,
31       const base::Closure& error_callback,
32       ProfileSyncComponentsFactory* profile_sync_factory,
33       Profile* profile,
34       ProfileSyncService* sync_service);
35
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;
49
50  protected:
51   // For testing only.
52   NonUIDataTypeController();
53   // DataTypeController is RefCounted.
54   virtual ~NonUIDataTypeController();
55
56   // DataTypeController interface.
57   virtual void OnModelLoaded() OVERRIDE;
58
59   // Start any dependent services that need to be running before we can
60   // associate models. The default implementation is a no-op.
61   // Return value:
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();
67
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();
72
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,
75   // false otherwise.
76   virtual bool PostTaskOnBackendThread(
77       const tracked_objects::Location& from_here,
78       const base::Closure& task) = 0;
79
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);
86
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);
93
94   // Kick off the association process.
95   virtual bool StartAssociationAsync();
96
97   // Record association time.
98   virtual void RecordAssociationTime(base::TimeDelta time);
99
100   // Record causes of start failure.
101   virtual void RecordStartFailure(StartResult result);
102
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();
106
107   Profile* profile() const { return profile_; }
108
109  private:
110
111   // Posted on the backend thread by StartAssociationAsync().
112   void StartAssociationWithSharedChangeProcessor(
113       const scoped_refptr<SharedChangeProcessor>& shared_change_processor);
114
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();
120
121   // Posts StopLocalService() to the datatype's thread.
122   void StopLocalServiceAsync();
123
124   // Calls local_service_->StopSyncing() and releases our references to it.
125   void StopLocalService();
126
127   // Abort model loading and trigger the model load callback.
128   void AbortModelLoad();
129
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);
135
136   ProfileSyncComponentsFactory* const profile_sync_factory_;
137   Profile* const profile_;
138   ProfileSyncService* const sync_service_;
139
140   // State of this datatype controller.
141   State state_;
142
143   // Callbacks for use when starting the datatype.
144   StartCallback start_callback_;
145   ModelLoadCallback model_load_callback_;
146
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
152   // particular.
153   //
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
159   // reference).
160   scoped_refptr<SharedChangeProcessor> shared_change_processor_;
161
162   // The UserShare to connect the SharedChangeProcessor to. NULL until set in
163   // LoadModels.
164   syncer::UserShare* user_share_;
165
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_;
172 };
173
174 }  // namespace browser_sync
175
176 #endif  // CHROME_BROWSER_SYNC_GLUE_NON_UI_DATA_TYPE_CONTROLLER_H_