Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / sync / glue / frontend_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_FRONTEND_DATA_TYPE_CONTROLLER_H__
6 #define CHROME_BROWSER_SYNC_GLUE_FRONTEND_DATA_TYPE_CONTROLLER_H__
7
8 #include <string>
9
10 #include "base/basictypes.h"
11 #include "base/compiler_specific.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "components/sync_driver/data_type_controller.h"
14 #include "components/sync_driver/data_type_error_handler.h"
15
16 class Profile;
17 class ProfileSyncService;
18 class ProfileSyncComponentsFactory;
19
20 namespace base {
21 class TimeDelta;
22 }
23
24 namespace syncer {
25 class SyncError;
26 }
27
28 namespace sync_driver {
29 class AssociatorInterface;
30 class ChangeProcessor;
31 }
32
33 namespace browser_sync {
34
35 // Implementation for datatypes that reside on the frontend thread
36 // (UI thread). This is the same thread we perform initialization on, so we
37 // don't have to worry about thread safety. The main start/stop funtionality is
38 // implemented by default.
39 // Derived classes must implement (at least):
40 //    syncer::ModelType type() const
41 //    void CreateSyncComponents();
42 // NOTE: This class is deprecated! New sync datatypes should be using the
43 // syncer::SyncableService API and the UIDataTypeController instead.
44 // TODO(zea): Delete this once all types are on the new API.
45 class FrontendDataTypeController : public sync_driver::DataTypeController {
46  public:
47   FrontendDataTypeController(
48       scoped_refptr<base::MessageLoopProxy> ui_thread,
49       const base::Closure& error_callback,
50       ProfileSyncComponentsFactory* profile_sync_factory,
51       Profile* profile,
52       ProfileSyncService* sync_service);
53
54   // DataTypeController interface.
55   void LoadModels(const ModelLoadCallback& model_load_callback) override;
56   void StartAssociating(const StartCallback& start_callback) override;
57   void Stop() override;
58   virtual syncer::ModelType type() const = 0;
59   syncer::ModelSafeGroup model_safe_group() const override;
60   std::string name() const override;
61   State state() const override;
62
63   // DataTypeErrorHandler interface.
64   void OnSingleDataTypeUnrecoverableError(
65       const syncer::SyncError& error) override;
66
67  protected:
68   friend class FrontendDataTypeControllerMock;
69
70   // For testing only.
71   FrontendDataTypeController();
72   ~FrontendDataTypeController() override;
73
74   // Kick off any dependent services that need to be running before we can
75   // associate models. The default implementation is a no-op.
76   // Return value:
77   //   True - if models are ready and association can proceed.
78   //   False - if models are not ready. Associate() should be called when the
79   //           models are ready. Refer to Start(_) implementation.
80   virtual bool StartModels();
81
82   // Datatype specific creation of sync components.
83   virtual void CreateSyncComponents() = 0;
84
85   // DataTypeController interface.
86   void OnModelLoaded() override;
87
88   // Perform any DataType controller specific state cleanup before stopping
89   // the datatype controller. The default implementation is a no-op.
90   virtual void CleanUpState();
91
92   // Helper method for cleaning up state and running the start callback.
93   virtual void StartDone(
94       ConfigureResult start_result,
95       const syncer::SyncMergeResult& local_merge_result,
96       const syncer::SyncMergeResult& syncer_merge_result);
97
98   // Record association time.
99   virtual void RecordAssociationTime(base::TimeDelta time);
100   // Record causes of start failure.
101   virtual void RecordStartFailure(ConfigureResult result);
102
103   virtual sync_driver::AssociatorInterface* model_associator() const;
104   virtual void set_model_associator(
105       sync_driver::AssociatorInterface* associator);
106   sync_driver::ChangeProcessor* GetChangeProcessor() const override;
107   virtual void set_change_processor(sync_driver::ChangeProcessor* processor);
108
109   // Handles the reporting of unrecoverable error. It records stuff in
110   // UMA and reports to breakpad.
111   // Virtual for testing purpose.
112   virtual void RecordUnrecoverableError(
113       const tracked_objects::Location& from_here,
114       const std::string& message);
115
116   ProfileSyncComponentsFactory* const profile_sync_factory_;
117   Profile* const profile_;
118   ProfileSyncService* const sync_service_;
119
120   State state_;
121
122   StartCallback start_callback_;
123   ModelLoadCallback model_load_callback_;
124
125   // TODO(sync): transition all datatypes to SyncableService and deprecate
126   // AssociatorInterface.
127   scoped_ptr<sync_driver::AssociatorInterface> model_associator_;
128   scoped_ptr<sync_driver::ChangeProcessor> change_processor_;
129
130  private:
131   // Build sync components and associate models.
132   // Return value:
133   //   True - if association was successful. FinishStart should have been
134   //          invoked.
135   //   False - if association failed. StartFailed should have been invoked.
136   virtual bool Associate();
137
138   void AbortModelLoad();
139
140   // Clean up our state and state variables. Called in response
141   // to a failure or abort or stop.
142   void CleanUp();
143
144   DISALLOW_COPY_AND_ASSIGN(FrontendDataTypeController);
145 };
146
147 }  // namespace browser_sync
148
149 #endif  // CHROME_BROWSER_SYNC_GLUE_FRONTEND_DATA_TYPE_CONTROLLER_H__