Upstream version 5.34.104.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 std::string name() const OVERRIDE;
54   virtual State state() const OVERRIDE;
55
56   // DataTypeErrorHandler interface.
57   virtual void OnSingleDatatypeUnrecoverableError(
58       const tracked_objects::Location& from_here,
59       const std::string& message) OVERRIDE;
60
61  protected:
62   // For testing only.
63   UIDataTypeController();
64   // DataTypeController is RefCounted.
65   virtual ~UIDataTypeController();
66
67   // Start any dependent services that need to be running before we can
68   // associate models. The default implementation is a no-op.
69   // Return value:
70   //   True - if models are ready and association can proceed.
71   //   False - if models are not ready. Associate() should be called when the
72   //           models are ready.
73   virtual bool StartModels();
74
75   // Perform any DataType controller specific state cleanup before stopping
76   // the datatype controller. The default implementation is a no-op.
77   virtual void StopModels();
78
79   // DataTypeController interface.
80   virtual void OnModelLoaded() OVERRIDE;
81
82   // Helper method for cleaning up state and invoking the start callback.
83   virtual void StartDone(StartResult result,
84                          const syncer::SyncMergeResult& local_merge_result,
85                          const syncer::SyncMergeResult& syncer_merge_result);
86
87   // Record association time.
88   virtual void RecordAssociationTime(base::TimeDelta time);
89   // Record causes of start failure.
90   virtual void RecordStartFailure(StartResult result);
91
92   ProfileSyncComponentsFactory* const profile_sync_factory_;
93   Profile* const profile_;
94   ProfileSyncService* const sync_service_;
95
96   State state_;
97
98   StartCallback start_callback_;
99   ModelLoadCallback model_load_callback_;
100
101   // The sync datatype being controlled.
102   syncer::ModelType type_;
103
104   // Sync's interface to the datatype. All sync changes for |type_| are pushed
105   // through it to the datatype as well as vice versa.
106   //
107   // Lifetime: it gets created when Start()) is called, and a reference to it
108   // is passed to |local_service_| during Associate(). We release our reference
109   // when Stop() or StartFailed() is called, and |local_service_| releases its
110   // reference when local_service_->StopSyncing() is called or when it is
111   // destroyed.
112   //
113   // Note: we use refcounting here primarily so that we can keep a uniform
114   // SyncableService API, whether the datatype lives on the UI thread or not
115   // (a syncer::SyncableService takes ownership of its SyncChangeProcessor when
116   // MergeDataAndStartSyncing is called). This will help us eventually merge the
117   // two datatype controller implementations (for ui and non-ui thread
118   // datatypes).
119   scoped_refptr<SharedChangeProcessor> shared_change_processor_;
120
121   // A weak pointer to the actual local syncable service, which performs all the
122   // real work. We do not own the object.
123   base::WeakPtr<syncer::SyncableService> local_service_;
124
125  private:
126    // Associate the sync model with the service's model, then start syncing.
127   virtual void Associate();
128
129   virtual void AbortModelLoad();
130
131   DISALLOW_COPY_AND_ASSIGN(UIDataTypeController);
132 };
133
134 }  // namespace browser_sync
135
136 #endif  // CHROME_BROWSER_SYNC_GLUE_UI_DATA_TYPE_CONTROLLER_H__