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 sync_driver {
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(
72 syncer::ModelType type,
73 const syncer::SyncError& error) OVERRIDE;
75 // Used by unit tests. TODO(sync) : This would go away if we made
76 // this class be able to do Dependency injection. crbug.com/129212.
77 ModelAssociationManager* GetModelAssociationManagerForTesting() {
78 return &model_association_manager_;
82 friend class TestDataTypeManager;
84 // Abort configuration and stop all data types due to configuration errors.
85 void Abort(ConfigureStatus status);
87 // Returns the priority types (control + priority user types).
88 // Virtual for overriding during tests.
89 virtual syncer::ModelTypeSet GetPriorityTypes() const;
91 // Divide |types| into sets by their priorities and return the sets from
92 // high priority to low priority.
93 TypeSetPriorityList PrioritizeTypes(const syncer::ModelTypeSet& types);
95 // Post a task to reconfigure when no downloading or association are running.
96 void ProcessReconfigure();
98 void Restart(syncer::ConfigureReason reason);
99 void DownloadReady(base::Time download_start_time,
100 syncer::ModelTypeSet types_to_download,
101 syncer::ModelTypeSet high_priority_types_before,
102 syncer::ModelTypeSet first_sync_types,
103 syncer::ModelTypeSet failed_configuration_types);
105 // Notification from the SBH that download failed due to a transient
106 // error and it will be retried.
107 void OnDownloadRetry();
109 void NotifyDone(const ConfigureResult& result);
111 // Add to |configure_time_delta_| the time since we last called
113 void AddToConfigureTime();
115 void ConfigureImpl(syncer::ModelTypeSet desired_types,
116 syncer::ConfigureReason reason);
118 BackendDataTypeConfigurer::DataTypeConfigStateMap
119 BuildDataTypeConfigStateMap(
120 const syncer::ModelTypeSet& types_being_configured) const;
122 // Start association of next batch of data types after association of
123 // previous batch finishes.
124 void StartNextAssociation();
128 BackendDataTypeConfigurer* configurer_;
129 // Map of all data type controllers that are available for sync.
130 // This list is determined at startup by various command line flags.
131 const DataTypeController::TypeMap* controllers_;
133 std::map<syncer::ModelType, int> start_order_;
134 syncer::ModelTypeSet last_requested_types_;
136 // Whether an attempt to reconfigure was made while we were busy configuring.
137 // The |last_requested_types_| will reflect the newest set of requested types.
138 bool needs_reconfigure_;
140 // The reason for the last reconfigure attempt. Note: this will be set to a
141 // valid value only when |needs_reconfigure_| is set.
142 syncer::ConfigureReason last_configure_reason_;
144 // The last time Restart() was called.
145 base::Time last_restart_time_;
147 // The accumulated time spent between calls to Restart() and going
148 // to the DONE state.
149 base::TimeDelta configure_time_delta_;
151 // Sync's datatype debug info listener, which we pass model association
153 const syncer::WeakHandle<syncer::DataTypeDebugInfoListener>
154 debug_info_listener_;
156 // The manager that handles the model association of the individual types.
157 ModelAssociationManager model_association_manager_;
159 // DataTypeManager must have only one observer -- the ProfileSyncService that
160 // created it and manages its lifetime.
161 DataTypeManagerObserver* const observer_;
163 // For querying failed data types (having unrecoverable error) when
164 // configuring backend.
165 FailedDataTypesHandler* failed_data_types_handler_;
167 // Types waiting to be downloaded.
168 TypeSetPriorityList download_types_queue_;
170 // Types waiting for association and related time tracking info.
171 struct AssociationTypesInfo {
172 AssociationTypesInfo();
173 ~AssociationTypesInfo();
174 syncer::ModelTypeSet types;
175 syncer::ModelTypeSet first_sync_types;
176 base::Time download_start_time;
177 base::Time download_ready_time;
178 base::Time association_request_time;
179 syncer::ModelTypeSet high_priority_types_before;
180 syncer::ModelTypeSet configured_types;
182 std::queue<AssociationTypesInfo> association_types_queue_;
184 // The encryption handler lets the DataTypeManager know the state of sync
185 // datatype encryption.
186 const DataTypeEncryptionHandler* encryption_handler_;
188 // Association and time stats of data type configuration.
189 std::vector<syncer::DataTypeConfigurationStats> configuration_stats_;
191 base::Closure unrecoverable_error_method_;
193 base::WeakPtrFactory<DataTypeManagerImpl> weak_ptr_factory_;
195 DISALLOW_COPY_AND_ASSIGN(DataTypeManagerImpl);
198 } // namespace sync_driver
200 #endif // COMPONENTS_SYNC_DRIVER_DATA_TYPE_MANAGER_IMPL_H__