| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef COMPONENTS_SYNC_DRIVER_DATA_TYPE_MANAGER_IMPL_H__ | 5 #ifndef COMPONENTS_SYNC_DRIVER_DATA_TYPE_MANAGER_IMPL_H__ |
| 6 #define COMPONENTS_SYNC_DRIVER_DATA_TYPE_MANAGER_IMPL_H__ | 6 #define COMPONENTS_SYNC_DRIVER_DATA_TYPE_MANAGER_IMPL_H__ |
| 7 | 7 |
| 8 #include "components/sync/driver/data_type_manager.h" | 8 #include "components/sync/driver/data_type_manager.h" |
| 9 | 9 |
| 10 #include <map> | 10 #include <map> |
| 11 #include <queue> | 11 #include <queue> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include "base/callback_forward.h" | 14 #include "base/callback_forward.h" |
| 15 #include "base/compiler_specific.h" | 15 #include "base/compiler_specific.h" |
| 16 #include "base/macros.h" | 16 #include "base/macros.h" |
| 17 #include "base/memory/weak_ptr.h" | 17 #include "base/memory/weak_ptr.h" |
| 18 #include "base/time/time.h" | 18 #include "base/time/time.h" |
| 19 #include "components/sync/base/weak_handle.h" |
| 19 #include "components/sync/driver/backend_data_type_configurer.h" | 20 #include "components/sync/driver/backend_data_type_configurer.h" |
| 20 #include "components/sync/driver/model_association_manager.h" | 21 #include "components/sync/driver/model_association_manager.h" |
| 21 | 22 |
| 22 namespace syncer { | 23 namespace syncer { |
| 23 struct DataTypeConfigurationStats; | |
| 24 class DataTypeDebugInfoListener; | |
| 25 template <typename T> | |
| 26 class WeakHandle; | |
| 27 } | |
| 28 | |
| 29 namespace sync_driver { | |
| 30 | 24 |
| 31 class DataTypeController; | 25 class DataTypeController; |
| 26 class DataTypeDebugInfoListener; |
| 32 class DataTypeEncryptionHandler; | 27 class DataTypeEncryptionHandler; |
| 33 class DataTypeManagerObserver; | 28 class DataTypeManagerObserver; |
| 29 struct DataTypeConfigurationStats; |
| 34 | 30 |
| 35 // List of data types grouped by priority and ordered from high priority to | 31 // List of data types grouped by priority and ordered from high priority to |
| 36 // low priority. | 32 // low priority. |
| 37 typedef std::queue<syncer::ModelTypeSet> TypeSetPriorityList; | 33 typedef std::queue<ModelTypeSet> TypeSetPriorityList; |
| 38 | 34 |
| 39 class DataTypeManagerImpl : public DataTypeManager, | 35 class DataTypeManagerImpl : public DataTypeManager, |
| 40 public ModelAssociationManagerDelegate { | 36 public ModelAssociationManagerDelegate { |
| 41 public: | 37 public: |
| 42 DataTypeManagerImpl( | 38 DataTypeManagerImpl( |
| 43 const syncer::WeakHandle<syncer::DataTypeDebugInfoListener>& | 39 const WeakHandle<DataTypeDebugInfoListener>& debug_info_listener, |
| 44 debug_info_listener, | |
| 45 const DataTypeController::TypeMap* controllers, | 40 const DataTypeController::TypeMap* controllers, |
| 46 const DataTypeEncryptionHandler* encryption_handler, | 41 const DataTypeEncryptionHandler* encryption_handler, |
| 47 BackendDataTypeConfigurer* configurer, | 42 BackendDataTypeConfigurer* configurer, |
| 48 DataTypeManagerObserver* observer); | 43 DataTypeManagerObserver* observer); |
| 49 ~DataTypeManagerImpl() override; | 44 ~DataTypeManagerImpl() override; |
| 50 | 45 |
| 51 // DataTypeManager interface. | 46 // DataTypeManager interface. |
| 52 void Configure(syncer::ModelTypeSet desired_types, | 47 void Configure(ModelTypeSet desired_types, ConfigureReason reason) override; |
| 53 syncer::ConfigureReason reason) override; | 48 void ReenableType(ModelType type) override; |
| 54 void ReenableType(syncer::ModelType type) override; | |
| 55 void ResetDataTypeErrors() override; | 49 void ResetDataTypeErrors() override; |
| 56 | 50 |
| 57 // Needed only for backend migration. | 51 // Needed only for backend migration. |
| 58 void PurgeForMigration(syncer::ModelTypeSet undesired_types, | 52 void PurgeForMigration(ModelTypeSet undesired_types, |
| 59 syncer::ConfigureReason reason) override; | 53 ConfigureReason reason) override; |
| 60 | 54 |
| 61 void Stop() override; | 55 void Stop() override; |
| 62 State state() const override; | 56 State state() const override; |
| 63 | 57 |
| 64 // |ModelAssociationManagerDelegate| implementation. | 58 // |ModelAssociationManagerDelegate| implementation. |
| 65 void OnAllDataTypesReadyForConfigure() override; | 59 void OnAllDataTypesReadyForConfigure() override; |
| 66 void OnSingleDataTypeAssociationDone( | 60 void OnSingleDataTypeAssociationDone( |
| 67 syncer::ModelType type, | 61 ModelType type, |
| 68 const syncer::DataTypeAssociationStats& association_stats) override; | 62 const DataTypeAssociationStats& association_stats) override; |
| 69 void OnModelAssociationDone( | 63 void OnModelAssociationDone( |
| 70 const DataTypeManager::ConfigureResult& result) override; | 64 const DataTypeManager::ConfigureResult& result) override; |
| 71 void OnSingleDataTypeWillStop(syncer::ModelType type, | 65 void OnSingleDataTypeWillStop(ModelType type, |
| 72 const syncer::SyncError& error) override; | 66 const SyncError& error) override; |
| 73 | 67 |
| 74 // Used by unit tests. TODO(sync) : This would go away if we made | 68 // Used by unit tests. TODO(sync) : This would go away if we made |
| 75 // this class be able to do Dependency injection. crbug.com/129212. | 69 // this class be able to do Dependency injection. crbug.com/129212. |
| 76 ModelAssociationManager* GetModelAssociationManagerForTesting() { | 70 ModelAssociationManager* GetModelAssociationManagerForTesting() { |
| 77 return &model_association_manager_; | 71 return &model_association_manager_; |
| 78 } | 72 } |
| 79 | 73 |
| 80 private: | 74 private: |
| 81 // Helper enum for identifying which types within a priority group to | 75 // Helper enum for identifying which types within a priority group to |
| 82 // associate. | 76 // associate. |
| 83 enum AssociationGroup { | 77 enum AssociationGroup { |
| 84 // Those types that were already downloaded and didn't have an error at | 78 // Those types that were already downloaded and didn't have an error at |
| 85 // configuration time. Corresponds with AssociationTypesInfo's | 79 // configuration time. Corresponds with AssociationTypesInfo's |
| 86 // |ready_types|. These types can start associating as soon as the | 80 // |ready_types|. These types can start associating as soon as the |
| 87 // ModelAssociationManager is not busy. | 81 // ModelAssociationManager is not busy. |
| 88 READY_AT_CONFIG, | 82 READY_AT_CONFIG, |
| 89 // All other types, including first time sync types and those that have | 83 // All other types, including first time sync types and those that have |
| 90 // encountered an error. These types must wait until the syncer has done | 84 // encountered an error. These types must wait until the syncer has done |
| 91 // any db changes and/or downloads before associating. | 85 // any db changes and/or downloads before associating. |
| 92 UNREADY_AT_CONFIG, | 86 UNREADY_AT_CONFIG, |
| 93 }; | 87 }; |
| 94 | 88 |
| 95 friend class TestDataTypeManager; | 89 friend class TestDataTypeManager; |
| 96 | 90 |
| 97 // Abort configuration and stop all data types due to configuration errors. | 91 // Abort configuration and stop all data types due to configuration errors. |
| 98 void Abort(ConfigureStatus status); | 92 void Abort(ConfigureStatus status); |
| 99 | 93 |
| 100 // Returns the priority types (control + priority user types). | 94 // Returns the priority types (control + priority user types). |
| 101 // Virtual for overriding during tests. | 95 // Virtual for overriding during tests. |
| 102 virtual syncer::ModelTypeSet GetPriorityTypes() const; | 96 virtual ModelTypeSet GetPriorityTypes() const; |
| 103 | 97 |
| 104 // Divide |types| into sets by their priorities and return the sets from | 98 // Divide |types| into sets by their priorities and return the sets from |
| 105 // high priority to low priority. | 99 // high priority to low priority. |
| 106 TypeSetPriorityList PrioritizeTypes(const syncer::ModelTypeSet& types); | 100 TypeSetPriorityList PrioritizeTypes(const ModelTypeSet& types); |
| 107 | 101 |
| 108 // Post a task to reconfigure when no downloading or association are running. | 102 // Post a task to reconfigure when no downloading or association are running. |
| 109 void ProcessReconfigure(); | 103 void ProcessReconfigure(); |
| 110 | 104 |
| 111 void Restart(syncer::ConfigureReason reason); | 105 void Restart(ConfigureReason reason); |
| 112 void DownloadReady(syncer::ModelTypeSet types_to_download, | 106 void DownloadReady(ModelTypeSet types_to_download, |
| 113 syncer::ModelTypeSet first_sync_types, | 107 ModelTypeSet first_sync_types, |
| 114 syncer::ModelTypeSet failed_configuration_types); | 108 ModelTypeSet failed_configuration_types); |
| 115 | 109 |
| 116 // Notification from the SBH that download failed due to a transient | 110 // Notification from the SBH that download failed due to a transient |
| 117 // error and it will be retried. | 111 // error and it will be retried. |
| 118 void OnDownloadRetry(); | 112 void OnDownloadRetry(); |
| 119 void NotifyStart(); | 113 void NotifyStart(); |
| 120 void NotifyDone(const ConfigureResult& result); | 114 void NotifyDone(const ConfigureResult& result); |
| 121 | 115 |
| 122 // Add to |configure_time_delta_| the time since we last called | 116 // Add to |configure_time_delta_| the time since we last called |
| 123 // Restart(). | 117 // Restart(). |
| 124 void AddToConfigureTime(); | 118 void AddToConfigureTime(); |
| 125 | 119 |
| 126 void ConfigureImpl(syncer::ModelTypeSet desired_types, | 120 void ConfigureImpl(ModelTypeSet desired_types, ConfigureReason reason); |
| 127 syncer::ConfigureReason reason); | |
| 128 | 121 |
| 129 // Calls data type controllers of requested types to register with backend. | 122 // Calls data type controllers of requested types to register with backend. |
| 130 void RegisterTypesWithBackend(); | 123 void RegisterTypesWithBackend(); |
| 131 | 124 |
| 132 BackendDataTypeConfigurer::DataTypeConfigStateMap BuildDataTypeConfigStateMap( | 125 BackendDataTypeConfigurer::DataTypeConfigStateMap BuildDataTypeConfigStateMap( |
| 133 const syncer::ModelTypeSet& types_being_configured) const; | 126 const ModelTypeSet& types_being_configured) const; |
| 134 | 127 |
| 135 // Start download of next set of types in |download_types_queue_| (if | 128 // Start download of next set of types in |download_types_queue_| (if |
| 136 // any exist, does nothing otherwise). | 129 // any exist, does nothing otherwise). |
| 137 // Will kick off association of any new ready types. | 130 // Will kick off association of any new ready types. |
| 138 void StartNextDownload(syncer::ModelTypeSet high_priority_types_before); | 131 void StartNextDownload(ModelTypeSet high_priority_types_before); |
| 139 | 132 |
| 140 // Start association of next batch of data types after association of | 133 // Start association of next batch of data types after association of |
| 141 // previous batch finishes. |group| controls which set of types within | 134 // previous batch finishes. |group| controls which set of types within |
| 142 // an AssociationTypesInfo to associate. Does nothing if model associator | 135 // an AssociationTypesInfo to associate. Does nothing if model associator |
| 143 // is busy performing association. | 136 // is busy performing association. |
| 144 void StartNextAssociation(AssociationGroup group); | 137 void StartNextAssociation(AssociationGroup group); |
| 145 | 138 |
| 146 void StopImpl(); | 139 void StopImpl(); |
| 147 | 140 |
| 148 // Returns the currently enabled types. | 141 // Returns the currently enabled types. |
| 149 syncer::ModelTypeSet GetEnabledTypes() const; | 142 ModelTypeSet GetEnabledTypes() const; |
| 150 | 143 |
| 151 BackendDataTypeConfigurer* configurer_; | 144 BackendDataTypeConfigurer* configurer_; |
| 152 // Map of all data type controllers that are available for sync. | 145 // Map of all data type controllers that are available for sync. |
| 153 // This list is determined at startup by various command line flags. | 146 // This list is determined at startup by various command line flags. |
| 154 const DataTypeController::TypeMap* controllers_; | 147 const DataTypeController::TypeMap* controllers_; |
| 155 State state_; | 148 State state_; |
| 156 syncer::ModelTypeSet last_requested_types_; | 149 ModelTypeSet last_requested_types_; |
| 157 | 150 |
| 158 // A set of types that were enabled at the time initialization with the | 151 // A set of types that were enabled at the time initialization with the |
| 159 // |model_association_manager_| was last attempted. | 152 // |model_association_manager_| was last attempted. |
| 160 syncer::ModelTypeSet last_enabled_types_; | 153 ModelTypeSet last_enabled_types_; |
| 161 | 154 |
| 162 // Whether an attempt to reconfigure was made while we were busy configuring. | 155 // Whether an attempt to reconfigure was made while we were busy configuring. |
| 163 // The |last_requested_types_| will reflect the newest set of requested types. | 156 // The |last_requested_types_| will reflect the newest set of requested types. |
| 164 bool needs_reconfigure_; | 157 bool needs_reconfigure_; |
| 165 | 158 |
| 166 // The reason for the last reconfigure attempt. Note: this will be set to a | 159 // The reason for the last reconfigure attempt. Note: this will be set to a |
| 167 // valid value only when |needs_reconfigure_| is set. | 160 // valid value only when |needs_reconfigure_| is set. |
| 168 syncer::ConfigureReason last_configure_reason_; | 161 ConfigureReason last_configure_reason_; |
| 169 | 162 |
| 170 // The last time Restart() was called. | 163 // The last time Restart() was called. |
| 171 base::Time last_restart_time_; | 164 base::Time last_restart_time_; |
| 172 | 165 |
| 173 // The accumulated time spent between calls to Restart() and going | 166 // The accumulated time spent between calls to Restart() and going |
| 174 // to the DONE state. | 167 // to the DONE state. |
| 175 base::TimeDelta configure_time_delta_; | 168 base::TimeDelta configure_time_delta_; |
| 176 | 169 |
| 177 // Sync's datatype debug info listener, which we pass model association | 170 // Sync's datatype debug info listener, which we pass model association |
| 178 // statistics to. | 171 // statistics to. |
| 179 const syncer::WeakHandle<syncer::DataTypeDebugInfoListener> | 172 const WeakHandle<DataTypeDebugInfoListener> debug_info_listener_; |
| 180 debug_info_listener_; | |
| 181 | 173 |
| 182 // The manager that handles the model association of the individual types. | 174 // The manager that handles the model association of the individual types. |
| 183 ModelAssociationManager model_association_manager_; | 175 ModelAssociationManager model_association_manager_; |
| 184 | 176 |
| 185 // DataTypeManager must have only one observer -- the ProfileSyncService that | 177 // DataTypeManager must have only one observer -- the ProfileSyncService that |
| 186 // created it and manages its lifetime. | 178 // created it and manages its lifetime. |
| 187 DataTypeManagerObserver* const observer_; | 179 DataTypeManagerObserver* const observer_; |
| 188 | 180 |
| 189 // For querying failed data types (having unrecoverable error) when | 181 // For querying failed data types (having unrecoverable error) when |
| 190 // configuring backend. | 182 // configuring backend. |
| 191 DataTypeStatusTable data_type_status_table_; | 183 DataTypeStatusTable data_type_status_table_; |
| 192 | 184 |
| 193 // Types waiting to be downloaded. | 185 // Types waiting to be downloaded. |
| 194 TypeSetPriorityList download_types_queue_; | 186 TypeSetPriorityList download_types_queue_; |
| 195 | 187 |
| 196 // Types waiting for association and related time tracking info. | 188 // Types waiting for association and related time tracking info. |
| 197 struct AssociationTypesInfo { | 189 struct AssociationTypesInfo { |
| 198 AssociationTypesInfo(); | 190 AssociationTypesInfo(); |
| 199 AssociationTypesInfo(const AssociationTypesInfo& other); | 191 AssociationTypesInfo(const AssociationTypesInfo& other); |
| 200 ~AssociationTypesInfo(); | 192 ~AssociationTypesInfo(); |
| 201 | 193 |
| 202 // Types to associate. | 194 // Types to associate. |
| 203 syncer::ModelTypeSet types; | 195 ModelTypeSet types; |
| 204 // Types that have just been downloaded and are being associated for the | 196 // Types that have just been downloaded and are being associated for the |
| 205 // first time. This includes types that had previously encountered an error | 197 // first time. This includes types that had previously encountered an error |
| 206 // and had to be purged/unapplied from the sync db. | 198 // and had to be purged/unapplied from the sync db. |
| 207 // This is a subset of |types|. | 199 // This is a subset of |types|. |
| 208 syncer::ModelTypeSet first_sync_types; | 200 ModelTypeSet first_sync_types; |
| 209 // Types that were already ready for association at configuration time. | 201 // Types that were already ready for association at configuration time. |
| 210 syncer::ModelTypeSet ready_types; | 202 ModelTypeSet ready_types; |
| 211 // Time at which |types| began downloading. | 203 // Time at which |types| began downloading. |
| 212 base::Time download_start_time; | 204 base::Time download_start_time; |
| 213 // Time at which |types| finished downloading. | 205 // Time at which |types| finished downloading. |
| 214 base::Time download_ready_time; | 206 base::Time download_ready_time; |
| 215 // Time at which the association for |read_types| began. | 207 // Time at which the association for |read_types| began. |
| 216 base::Time ready_association_request_time; | 208 base::Time ready_association_request_time; |
| 217 // Time at which the association for |types| began (not relevant to | 209 // Time at which the association for |types| began (not relevant to |
| 218 // |ready_types|. | 210 // |ready_types|. |
| 219 base::Time full_association_request_time; | 211 base::Time full_association_request_time; |
| 220 // The set of types that are higher priority (and were therefore blocking) | 212 // The set of types that are higher priority (and were therefore blocking) |
| 221 // the association of |types|. | 213 // the association of |types|. |
| 222 syncer::ModelTypeSet high_priority_types_before; | 214 ModelTypeSet high_priority_types_before; |
| 223 // The subset of |types| that were successfully configured. | 215 // The subset of |types| that were successfully configured. |
| 224 syncer::ModelTypeSet configured_types; | 216 ModelTypeSet configured_types; |
| 225 }; | 217 }; |
| 226 std::queue<AssociationTypesInfo> association_types_queue_; | 218 std::queue<AssociationTypesInfo> association_types_queue_; |
| 227 | 219 |
| 228 // The encryption handler lets the DataTypeManager know the state of sync | 220 // The encryption handler lets the DataTypeManager know the state of sync |
| 229 // datatype encryption. | 221 // datatype encryption. |
| 230 const DataTypeEncryptionHandler* encryption_handler_; | 222 const DataTypeEncryptionHandler* encryption_handler_; |
| 231 | 223 |
| 232 // Association and time stats of data type configuration. | 224 // Association and time stats of data type configuration. |
| 233 std::vector<syncer::DataTypeConfigurationStats> configuration_stats_; | 225 std::vector<DataTypeConfigurationStats> configuration_stats_; |
| 234 | 226 |
| 235 // True iff we are in the process of catching up datatypes. | 227 // True iff we are in the process of catching up datatypes. |
| 236 bool catch_up_in_progress_; | 228 bool catch_up_in_progress_; |
| 237 | 229 |
| 238 // Configuration process is started when ModelAssociationManager notifies | 230 // Configuration process is started when ModelAssociationManager notifies |
| 239 // DataTypeManager that all types are ready for configure. | 231 // DataTypeManager that all types are ready for configure. |
| 240 // This flag ensures that this process is started only once. | 232 // This flag ensures that this process is started only once. |
| 241 bool download_started_; | 233 bool download_started_; |
| 242 | 234 |
| 243 base::WeakPtrFactory<DataTypeManagerImpl> weak_ptr_factory_; | 235 base::WeakPtrFactory<DataTypeManagerImpl> weak_ptr_factory_; |
| 244 | 236 |
| 245 DISALLOW_COPY_AND_ASSIGN(DataTypeManagerImpl); | 237 DISALLOW_COPY_AND_ASSIGN(DataTypeManagerImpl); |
| 246 }; | 238 }; |
| 247 | 239 |
| 248 } // namespace sync_driver | 240 } // namespace syncer |
| 249 | 241 |
| 250 #endif // COMPONENTS_SYNC_DRIVER_DATA_TYPE_MANAGER_IMPL_H__ | 242 #endif // COMPONENTS_SYNC_DRIVER_DATA_TYPE_MANAGER_IMPL_H__ |
| OLD | NEW |