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