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. 4 5 #ifndef COMPONENTS_SYNC_DRIVER_MODEL_ASSOCIATION_MANAGER_H__ 6 #define COMPONENTS_SYNC_DRIVER_MODEL_ASSOCIATION_MANAGER_H__ 7 8 #include <map> 9 10 #include "base/memory/weak_ptr.h" 11 #include "base/timer/timer.h" 12 13 #include "components/sync_driver/data_type_manager.h" 14 #include "sync/internal_api/public/data_type_association_stats.h" 15 #include "sync/internal_api/public/util/weak_handle.h" 16 17 // |ModelAssociationManager| does the heavy lifting for doing the actual model 18 // association. It instructs DataTypeControllers to load models, start 19 // associating and stopping. Since the operations are async it uses an 20 // interface to inform DataTypeManager the results of the operations. 21 // This class is owned by DataTypeManager. 22 namespace browser_sync { 23 24 class DataTypeController; 25 26 // |ModelAssociationManager| association functions are async. The results of 27 // those operations are passed back via this interface. 28 class ModelAssociationManagerDelegate { 29 public: 30 // Called when model association (MergeDataAndStartSyncing) has completed 31 // for |type|, regardless of success or failure. 32 virtual void OnSingleDataTypeAssociationDone( 33 syncer::ModelType type, 34 const syncer::DataTypeAssociationStats& association_stats) = 0; 35 36 // Called when the ModelAssociationManager has decided it must stop |type|, 37 // likely because it is no longer a desired data type or sync is shutting 38 // down. 39 virtual void OnSingleDataTypeWillStop(syncer::ModelType type) = 0; 40 41 // Called when the ModelAssociationManager has tried to perform model 42 // association for all desired types and has nothing left to do. 43 virtual void OnModelAssociationDone( 44 const DataTypeManager::ConfigureResult& result) = 0; ~ModelAssociationManagerDelegate()45 virtual ~ModelAssociationManagerDelegate() {} 46 }; 47 48 // The class that is responsible for model association. 49 class ModelAssociationManager { 50 public: 51 ModelAssociationManager(const DataTypeController::TypeMap* controllers, 52 ModelAssociationManagerDelegate* delegate); 53 virtual ~ModelAssociationManager(); 54 55 // Initializes the state to do the model association in future. This 56 // should be called before communicating with sync server. A subsequent call 57 // of Initialize is only allowed if the ModelAssociationManager has invoked 58 // |OnModelAssociationDone| on the |ModelAssociationManagerDelegate|. After 59 // this call, there should be several calls to StartAssociationAsync() 60 // to associate subset of |desired_types|. 61 void Initialize(syncer::ModelTypeSet desired_types); 62 63 // Can be called at any time. Synchronously stops all datatypes. 64 void Stop(); 65 66 // Should only be called after Initialize to start the actual association. 67 // |types_to_associate| should be subset of |desired_types| in Initialize(). 68 // When this is completed, |OnModelAssociationDone| will be invoked. 69 void StartAssociationAsync(const syncer::ModelTypeSet& types_to_associate); 70 71 // This is used for TESTING PURPOSE ONLY. The test case can inspect 72 // and modify the timer. 73 // TODO(sync) : This would go away if we made this class be able to do 74 // Dependency injection. crbug.com/129212. 75 base::OneShotTimer<ModelAssociationManager>* GetTimerForTesting(); 76 77 private: 78 enum State { 79 // This is the state after |Initialize| is called. 80 INITIALIZED_TO_CONFIGURE, 81 // Starting a new configuration. 82 CONFIGURING, 83 // No configuration is in progress. 84 IDLE 85 }; 86 87 // Called at the end of association to reset state to prepare for next 88 // round of association. 89 void ResetForNextAssociation(); 90 91 // Called by Initialize() to stop types that are not in |desired_types_|. 92 void StopDisabledTypes(); 93 94 // Start loading non-running types that are in |desired_types_|. 95 void LoadEnabledTypes(); 96 97 // Callback passed to each data type controller on starting association. This 98 // callback will be invoked when the model association is done. 99 void TypeStartCallback(syncer::ModelType type, 100 base::TimeTicks type_start_time, 101 DataTypeController::StartResult start_result, 102 const syncer::SyncMergeResult& local_merge_result, 103 const syncer::SyncMergeResult& syncer_merge_result); 104 105 // Callback that will be invoked when the models finish loading. This callback 106 // will be passed to |LoadModels| function. 107 void ModelLoadCallback(syncer::ModelType type, syncer::SyncError error); 108 109 // When a type fails to load or fails associating this method is invoked to 110 // do the book keeping and do the UMA reporting. 111 void AppendToFailedDatatypesAndLogError(const syncer::SyncError& error); 112 113 // Called when all requested types are associated or association times out. 114 // Notify |delegate_| of configuration results. 115 void ModelAssociationDone(); 116 117 // A helper to stop an individual datatype. 118 void StopDatatype(DataTypeController* dtc); 119 120 State state_; 121 122 // Data types that are enabled. 123 syncer::ModelTypeSet desired_types_; 124 125 // Data types that are requested to associate. 126 syncer::ModelTypeSet requested_types_; 127 128 // Data types currently being associated, including types waiting for model 129 // load. 130 syncer::ModelTypeSet associating_types_; 131 132 // Data types that are loaded, i.e. ready to associate. 133 syncer::ModelTypeSet loaded_types_; 134 135 // Data types that are associated, i.e. no more action needed during 136 // reconfiguration if not disabled. 137 syncer::ModelTypeSet associated_types_; 138 139 // Data types that are still loading/associating when configuration times 140 // out. 141 syncer::ModelTypeSet slow_types_; 142 143 // Collects the list of errors resulting from failing to start a type. This 144 // would eventually be sent to the listeners after all the types have 145 // been given a chance to start. 146 std::map<syncer::ModelType, syncer::SyncError> failed_data_types_info_; 147 148 // The set of types that can't configure due to cryptographer errors. 149 syncer::ModelTypeSet needs_crypto_types_; 150 151 // Time when StartAssociationAsync() is called to associate for a set of data 152 // types. 153 base::TimeTicks association_start_time_; 154 155 // Set of all registered controllers. 156 const DataTypeController::TypeMap* controllers_; 157 158 // The processor in charge of handling model association results. 159 ModelAssociationManagerDelegate* delegate_; 160 161 // Timer to track and limit how long a datatype takes to model associate. 162 base::OneShotTimer<ModelAssociationManager> timer_; 163 164 base::WeakPtrFactory<ModelAssociationManager> weak_ptr_factory_; 165 166 DataTypeManager::ConfigureStatus configure_status_; 167 168 DISALLOW_COPY_AND_ASSIGN(ModelAssociationManager); 169 }; 170 } // namespace browser_sync 171 #endif // COMPONENTS_SYNC_DRIVER_MODEL_ASSOCIATION_MANAGER_H__ 172