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