Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / sync / glue / 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_UI_DATA_TYPE_CONTROLLER_H__
6 #define CHROME_BROWSER_SYNC_GLUE_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 base {
22 class TimeDelta;
23 }
24
25 namespace syncer {
26 class SyncableService;
27 class SyncError;
28 }
29
30 namespace browser_sync {
31
32 // Implementation for datatypes that reside on the (UI thread). This is the same
33 // thread we perform initialization on, so we don't have to worry about thread
34 // safety. The main start/stop funtionality is implemented by default.
35 // Note: RefCountedThreadSafe by way of DataTypeController.
36 class UIDataTypeController : public DataTypeController {
37  public:
38   UIDataTypeController(
39       scoped_refptr<base::MessageLoopProxy> ui_thread,
40       const base::Closure& error_callback,
41       syncer::ModelType type,
42       ProfileSyncComponentsFactory* profile_sync_factory,
43       Profile* profile,
44       ProfileSyncService* sync_service);
45
46   // DataTypeController interface.
47   virtual void LoadModels(
48       const ModelLoadCallback& model_load_callback) OVERRIDE;
49   virtual void StartAssociating(const StartCallback& start_callback) OVERRIDE;
50   virtual void Stop() OVERRIDE;
51   virtual syncer::ModelType type() const OVERRIDE;
52   virtual syncer::ModelSafeGroup model_safe_group() const OVERRIDE;
53   virtual ChangeProcessor* GetChangeProcessor() const OVERRIDE;
54   virtual std::string name() const OVERRIDE;
55   virtual State state() const OVERRIDE;
56
57   // DataTypeErrorHandler interface.
58   virtual void OnSingleDatatypeUnrecoverableError(
59       const tracked_objects::Location& from_here,
60       const std::string& message) OVERRIDE;
61
62   // Used by tests to override the factory used to create
63   // GenericChangeProcessors.
64   void SetGenericChangeProcessorFactoryForTest(
65       scoped_ptr<GenericChangeProcessorFactory> factory);
66
67  protected:
68   // For testing only.
69   UIDataTypeController();
70   // DataTypeController is RefCounted.
71   virtual ~UIDataTypeController();
72
73   // Start any dependent services that need to be running before we can
74   // associate models. The default implementation is a no-op.
75   // Return value:
76   //   True - if models are ready and association can proceed.
77   //   False - if models are not ready. Associate() should be called when the
78   //           models are ready.
79   virtual bool StartModels();
80
81   // Perform any DataType controller specific state cleanup before stopping
82   // the datatype controller. The default implementation is a no-op.
83   virtual void StopModels();
84
85   // DataTypeController interface.
86   virtual void OnModelLoaded() OVERRIDE;
87
88   // Helper method for cleaning up state and invoking the start callback.
89   virtual void StartDone(StartResult result,
90                          const syncer::SyncMergeResult& local_merge_result,
91                          const syncer::SyncMergeResult& syncer_merge_result);
92
93   // Record association time.
94   virtual void RecordAssociationTime(base::TimeDelta time);
95   // Record causes of start failure.
96   virtual void RecordStartFailure(StartResult result);
97
98   ProfileSyncComponentsFactory* const profile_sync_factory_;
99   Profile* const profile_;
100   ProfileSyncService* const sync_service_;
101
102   State state_;
103
104   StartCallback start_callback_;
105   ModelLoadCallback model_load_callback_;
106
107   // The sync datatype being controlled.
108   syncer::ModelType type_;
109
110   // Sync's interface to the datatype. All sync changes for |type_| are pushed
111   // through it to the datatype as well as vice versa.
112   //
113   // Lifetime: it gets created when Start()) is called, and a reference to it
114   // is passed to |local_service_| during Associate(). We release our reference
115   // when Stop() or StartFailed() is called, and |local_service_| releases its
116   // reference when local_service_->StopSyncing() is called or when it is
117   // destroyed.
118   //
119   // Note: we use refcounting here primarily so that we can keep a uniform
120   // SyncableService API, whether the datatype lives on the UI thread or not
121   // (a syncer::SyncableService takes ownership of its SyncChangeProcessor when
122   // MergeDataAndStartSyncing is called). This will help us eventually merge the
123   // two datatype controller implementations (for ui and non-ui thread
124   // datatypes).
125   scoped_refptr<SharedChangeProcessor> shared_change_processor_;
126
127   scoped_ptr<GenericChangeProcessorFactory> processor_factory_;
128
129   // A weak pointer to the actual local syncable service, which performs all the
130   // real work. We do not own the object.
131   base::WeakPtr<syncer::SyncableService> local_service_;
132
133  private:
134    // Associate the sync model with the service's model, then start syncing.
135   virtual void Associate();
136
137   virtual void AbortModelLoad();
138
139   DISALLOW_COPY_AND_ASSIGN(UIDataTypeController);
140 };
141
142 }  // namespace browser_sync
143
144 #endif  // CHROME_BROWSER_SYNC_GLUE_UI_DATA_TYPE_CONTROLLER_H__