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