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.
5 #ifndef CHROME_BROWSER_SYNC_GLUE_DATA_TYPE_MANAGER_IMPL_H__
6 #define CHROME_BROWSER_SYNC_GLUE_DATA_TYPE_MANAGER_IMPL_H__
8 #include "chrome/browser/sync/glue/data_type_manager.h"
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"
22 struct DataTypeConfigurationStats;
23 class DataTypeDebugInfoListener;
24 template <typename T> class WeakHandle;
27 namespace browser_sync {
29 class DataTypeController;
30 class DataTypeEncryptionHandler;
31 class DataTypeManagerObserver;
32 class FailedDataTypesHandler;
34 // List of data types grouped by priority and ordered from high priority to
36 typedef std::queue<syncer::ModelTypeSet> TypeSetPriorityList;
38 class DataTypeManagerImpl : public DataTypeManager,
39 public ModelAssociationResultProcessor {
42 const syncer::WeakHandle<syncer::DataTypeDebugInfoListener>&
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();
51 // DataTypeManager interface.
52 virtual void Configure(syncer::ModelTypeSet desired_types,
53 syncer::ConfigureReason reason) OVERRIDE;
55 // Needed only for backend migration.
56 virtual void PurgeForMigration(
57 syncer::ModelTypeSet undesired_types,
58 syncer::ConfigureReason reason) OVERRIDE;
60 virtual void Stop() OVERRIDE;
61 virtual State state() const OVERRIDE;
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;
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_;
78 friend class TestDataTypeManager;
80 // Abort configuration and stop all data types due to configuration errors.
81 void Abort(ConfigureStatus status,
82 const syncer::SyncError& error);
84 // Returns the priority types (control + priority user types).
85 // Virtual for overriding during tests.
86 virtual syncer::ModelTypeSet GetPriorityTypes() const;
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);
92 // Post a task to reconfigure when no downloading or association are running.
93 void ProcessReconfigure();
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);
102 // Notification from the SBH that download failed due to a transient
103 // error and it will be retried.
104 void OnDownloadRetry();
106 void NotifyDone(const ConfigureResult& result);
108 // Add to |configure_time_delta_| the time since we last called
110 void AddToConfigureTime();
112 void ConfigureImpl(syncer::ModelTypeSet desired_types,
113 syncer::ConfigureReason reason);
115 BackendDataTypeConfigurer::DataTypeConfigStateMap
116 BuildDataTypeConfigStateMap(
117 const syncer::ModelTypeSet& types_being_configured) const;
119 // Start association of next batch of data types after association of
120 // previous batch finishes.
121 void StartNextAssociation();
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_;
130 std::map<syncer::ModelType, int> start_order_;
131 syncer::ModelTypeSet last_requested_types_;
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_;
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_;
141 // The last time Restart() was called.
142 base::Time last_restart_time_;
144 // The accumulated time spent between calls to Restart() and going
145 // to the DONE state.
146 base::TimeDelta configure_time_delta_;
148 // Sync's datatype debug info listener, which we pass model association
150 const syncer::WeakHandle<syncer::DataTypeDebugInfoListener>
151 debug_info_listener_;
153 // The manager that handles the model association of the individual types.
154 ModelAssociationManager model_association_manager_;
156 // DataTypeManager must have only one observer -- the ProfileSyncService that
157 // created it and manages its lifetime.
158 DataTypeManagerObserver* const observer_;
160 // For querying failed data types (having unrecoverable error) when
161 // configuring backend.
162 browser_sync::FailedDataTypesHandler* failed_data_types_handler_;
164 // Types waiting to be downloaded.
165 TypeSetPriorityList download_types_queue_;
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;
179 std::queue<AssociationTypesInfo> association_types_queue_;
181 // The encryption handler lets the DataTypeManager know the state of sync
182 // datatype encryption.
183 const browser_sync::DataTypeEncryptionHandler* encryption_handler_;
185 // Association and time stats of data type configuration.
186 std::vector<syncer::DataTypeConfigurationStats> configuration_stats_;
188 base::WeakPtrFactory<DataTypeManagerImpl> weak_ptr_factory_;
190 DISALLOW_COPY_AND_ASSIGN(DataTypeManagerImpl);
193 } // namespace browser_sync
195 #endif // CHROME_BROWSER_SYNC_GLUE_DATA_TYPE_MANAGER_IMPL_H__