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