- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / sync / glue / data_type_manager_impl.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_DATA_TYPE_MANAGER_IMPL_H__
6 #define CHROME_BROWSER_SYNC_GLUE_DATA_TYPE_MANAGER_IMPL_H__
7
8 #include "chrome/browser/sync/glue/data_type_manager.h"
9
10 #include <map>
11 #include <queue>
12 #include <vector>
13
14 #include "base/basictypes.h"
15 #include "base/compiler_specific.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/time/time.h"
18 #include "chrome/browser/sync/glue/backend_data_type_configurer.h"
19 #include "chrome/browser/sync/glue/model_association_manager.h"
20
21 namespace syncer {
22 struct DataTypeConfigurationStats;
23 class DataTypeDebugInfoListener;
24 template <typename T> class WeakHandle;
25 }
26
27 namespace browser_sync {
28
29 class DataTypeController;
30 class DataTypeEncryptionHandler;
31 class DataTypeManagerObserver;
32 class FailedDataTypesHandler;
33
34 // List of data types grouped by priority and ordered from high priority to
35 // low priority.
36 typedef std::queue<syncer::ModelTypeSet> TypeSetPriorityList;
37
38 class DataTypeManagerImpl : public DataTypeManager,
39                             public ModelAssociationResultProcessor {
40  public:
41   DataTypeManagerImpl(
42       const syncer::WeakHandle<syncer::DataTypeDebugInfoListener>&
43           debug_info_listener,
44       const DataTypeController::TypeMap* controllers,
45       const DataTypeEncryptionHandler* encryption_handler,
46       BackendDataTypeConfigurer* configurer,
47       DataTypeManagerObserver* observer,
48       FailedDataTypesHandler* failed_data_types_handler);
49   virtual ~DataTypeManagerImpl();
50
51   // DataTypeManager interface.
52   virtual void Configure(syncer::ModelTypeSet desired_types,
53                          syncer::ConfigureReason reason) OVERRIDE;
54
55   // Needed only for backend migration.
56   virtual void PurgeForMigration(
57       syncer::ModelTypeSet undesired_types,
58       syncer::ConfigureReason reason) OVERRIDE;
59
60   virtual void Stop() OVERRIDE;
61   virtual State state() const OVERRIDE;
62
63   // |ModelAssociationResultProcessor| implementation.
64   virtual void OnSingleDataTypeAssociationDone(
65       syncer::ModelType type,
66       const syncer::DataTypeAssociationStats& association_stats) OVERRIDE;
67   virtual void OnModelAssociationDone(
68       const DataTypeManager::ConfigureResult& result) OVERRIDE;
69   virtual void OnTypesLoaded() OVERRIDE;
70
71   // Used by unit tests. TODO(sync) : This would go away if we made
72   // this class be able to do Dependency injection. crbug.com/129212.
73   ModelAssociationManager* GetModelAssociationManagerForTesting() {
74     return &model_association_manager_;
75   }
76
77  private:
78   friend class TestDataTypeManager;
79
80   // Abort configuration and stop all data types due to configuration errors.
81   void Abort(ConfigureStatus status,
82              const syncer::SyncError& error);
83
84   // Returns the priority types (control + priority user types).
85   // Virtual for overriding during tests.
86   virtual syncer::ModelTypeSet GetPriorityTypes() const;
87
88   // Divide |types| into sets by their priorities and return the sets from
89   // high priority to low priority.
90   TypeSetPriorityList PrioritizeTypes(const syncer::ModelTypeSet& types);
91
92   // Post a task to reconfigure when no downloading or association are running.
93   void ProcessReconfigure();
94
95   void Restart(syncer::ConfigureReason reason);
96   void DownloadReady(base::Time download_start_time,
97                      syncer::ModelTypeSet types_to_download,
98                      syncer::ModelTypeSet high_priority_types_before,
99                      syncer::ModelTypeSet first_sync_types,
100                      syncer::ModelTypeSet failed_configuration_types);
101
102   // Notification from the SBH that download failed due to a transient
103   // error and it will be retried.
104   void OnDownloadRetry();
105   void NotifyStart();
106   void NotifyDone(const ConfigureResult& result);
107
108   // Add to |configure_time_delta_| the time since we last called
109   // Restart().
110   void AddToConfigureTime();
111
112   void ConfigureImpl(syncer::ModelTypeSet desired_types,
113                      syncer::ConfigureReason reason);
114
115   BackendDataTypeConfigurer::DataTypeConfigStateMap
116   BuildDataTypeConfigStateMap(
117       const syncer::ModelTypeSet& types_being_configured) const;
118
119   // Start association of next batch of data types after association of
120   // previous batch finishes.
121   void StartNextAssociation();
122
123   void StopImpl();
124
125   BackendDataTypeConfigurer* configurer_;
126   // Map of all data type controllers that are available for sync.
127   // This list is determined at startup by various command line flags.
128   const DataTypeController::TypeMap* controllers_;
129   State state_;
130   std::map<syncer::ModelType, int> start_order_;
131   syncer::ModelTypeSet last_requested_types_;
132
133   // Whether an attempt to reconfigure was made while we were busy configuring.
134   // The |last_requested_types_| will reflect the newest set of requested types.
135   bool needs_reconfigure_;
136
137   // The reason for the last reconfigure attempt. Note: this will be set to a
138   // valid value only when |needs_reconfigure_| is set.
139   syncer::ConfigureReason last_configure_reason_;
140
141   // The last time Restart() was called.
142   base::Time last_restart_time_;
143
144   // The accumulated time spent between calls to Restart() and going
145   // to the DONE state.
146   base::TimeDelta configure_time_delta_;
147
148   // Sync's datatype debug info listener, which we pass model association
149   // statistics to.
150   const syncer::WeakHandle<syncer::DataTypeDebugInfoListener>
151       debug_info_listener_;
152
153   // The manager that handles the model association of the individual types.
154   ModelAssociationManager model_association_manager_;
155
156   // DataTypeManager must have only one observer -- the ProfileSyncService that
157   // created it and manages its lifetime.
158   DataTypeManagerObserver* const observer_;
159
160   // For querying failed data types (having unrecoverable error) when
161   // configuring backend.
162   browser_sync::FailedDataTypesHandler* failed_data_types_handler_;
163
164   // Types waiting to be downloaded.
165   TypeSetPriorityList download_types_queue_;
166
167   // Types waiting for association and related time tracking info.
168   struct AssociationTypesInfo {
169     AssociationTypesInfo();
170     ~AssociationTypesInfo();
171     syncer::ModelTypeSet types;
172     syncer::ModelTypeSet first_sync_types;
173     base::Time download_start_time;
174     base::Time download_ready_time;
175     base::Time association_request_time;
176     syncer::ModelTypeSet high_priority_types_before;
177     syncer::ModelTypeSet configured_types;
178   };
179   std::queue<AssociationTypesInfo> association_types_queue_;
180
181   // The encryption handler lets the DataTypeManager know the state of sync
182   // datatype encryption.
183   const browser_sync::DataTypeEncryptionHandler* encryption_handler_;
184
185   // Association and time stats of data type configuration.
186   std::vector<syncer::DataTypeConfigurationStats> configuration_stats_;
187
188   base::WeakPtrFactory<DataTypeManagerImpl> weak_ptr_factory_;
189
190   DISALLOW_COPY_AND_ASSIGN(DataTypeManagerImpl);
191 };
192
193 }  // namespace browser_sync
194
195 #endif  // CHROME_BROWSER_SYNC_GLUE_DATA_TYPE_MANAGER_IMPL_H__