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_MODEL_ASSOCIATION_MANAGER_H__ 6 #define CHROME_BROWSER_SYNC_GLUE_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 "chrome/browser/sync/glue/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 ModelAssociationResultProcessor { 29 public: 30 virtual void OnSingleDataTypeAssociationDone( 31 syncer::ModelType type, 32 const syncer::DataTypeAssociationStats& association_stats) = 0; 33 virtual void OnModelAssociationDone( 34 const DataTypeManager::ConfigureResult& result) = 0; 35 virtual ~ModelAssociationResultProcessor() {} 36 }; 37 38 // The class that is responsible for model association. 39 class ModelAssociationManager { 40 public: 41 ModelAssociationManager(const DataTypeController::TypeMap* controllers, 42 ModelAssociationResultProcessor* processor); 43 virtual ~ModelAssociationManager(); 44 45 // Initializes the state to do the model association in future. This 46 // should be called before communicating with sync server. A subsequent call 47 // of Initialize is only allowed if the ModelAssociationManager has invoked 48 // |OnModelAssociationDone| on the |ModelAssociationResultProcessor|. After 49 // this call, there should be several calls to StartAssociationAsync() 50 // to associate subset of |desired_types|. 51 void Initialize(syncer::ModelTypeSet desired_types); 52 53 // Can be called at any time. Synchronously stops all datatypes. 54 void Stop(); 55 56 // Should only be called after Initialize to start the actual association. 57 // |types_to_associate| should be subset of |desired_types| in Initialize(). 58 // When this is completed, |OnModelAssociationDone| will be invoked. 59 void StartAssociationAsync(const syncer::ModelTypeSet& types_to_associate); 60 61 // This is used for TESTING PURPOSE ONLY. The test case can inspect 62 // and modify the timer. 63 // TODO(sync) : This would go away if we made this class be able to do 64 // Dependency injection. crbug.com/129212. 65 base::OneShotTimer<ModelAssociationManager>* GetTimerForTesting(); 66 67 private: 68 enum State { 69 // This is the state after |Initialize| is called. 70 INITIALIZED_TO_CONFIGURE, 71 // Starting a new configuration. 72 CONFIGURING, 73 // No configuration is in progress. 74 IDLE 75 }; 76 77 // Called at the end of association to reset state to prepare for next 78 // round of association. 79 void ResetForNextAssociation(); 80 81 // Called by Initialize() to stop types that are not in |desired_types_|. 82 void StopDisabledTypes(); 83 84 // Start loading non-running types that are in |desired_types_|. 85 void LoadEnabledTypes(); 86 87 // Callback passed to each data type controller on starting association. This 88 // callback will be invoked when the model association is done. 89 void TypeStartCallback(syncer::ModelType type, 90 base::TimeTicks type_start_time, 91 DataTypeController::StartResult start_result, 92 const syncer::SyncMergeResult& local_merge_result, 93 const syncer::SyncMergeResult& syncer_merge_result); 94 95 // Callback that will be invoked when the models finish loading. This callback 96 // will be passed to |LoadModels| function. 97 void ModelLoadCallback(syncer::ModelType type, syncer::SyncError error); 98 99 // When a type fails to load or fails associating this method is invoked to 100 // do the book keeping and do the UMA reporting. 101 void AppendToFailedDatatypesAndLogError(const syncer::SyncError& error); 102 103 // Called when all requested types are associated or association times out. 104 // Notify |result_processor_| of configuration results. 105 void ModelAssociationDone(); 106 107 State state_; 108 109 // Data types that are enabled. 110 syncer::ModelTypeSet desired_types_; 111 112 // Data types that are requested to associate. 113 syncer::ModelTypeSet requested_types_; 114 115 // Data types currently being associated, including types waiting for model 116 // load. 117 syncer::ModelTypeSet associating_types_; 118 119 // Data types that are loaded, i.e. ready to associate. 120 syncer::ModelTypeSet loaded_types_; 121 122 // Data types that are associated, i.e. no more action needed during 123 // reconfiguration if not disabled. 124 syncer::ModelTypeSet associated_types_; 125 126 // Data types that are still loading/associating when configuration times 127 // out. 128 syncer::ModelTypeSet slow_types_; 129 130 // Collects the list of errors resulting from failing to start a type. This 131 // would eventually be sent to the listeners after all the types have 132 // been given a chance to start. 133 std::map<syncer::ModelType, syncer::SyncError> failed_data_types_info_; 134 135 // The set of types that can't configure due to cryptographer errors. 136 syncer::ModelTypeSet needs_crypto_types_; 137 138 // Time when StartAssociationAsync() is called to associate for a set of data 139 // types. 140 base::TimeTicks association_start_time_; 141 142 // Set of all registered controllers. 143 const DataTypeController::TypeMap* controllers_; 144 145 // The processor in charge of handling model association results. 146 ModelAssociationResultProcessor* result_processor_; 147 148 // Timer to track and limit how long a datatype takes to model associate. 149 base::OneShotTimer<ModelAssociationManager> timer_; 150 151 base::WeakPtrFactory<ModelAssociationManager> weak_ptr_factory_; 152 153 DataTypeManager::ConfigureStatus configure_status_; 154 155 DISALLOW_COPY_AND_ASSIGN(ModelAssociationManager); 156 }; 157 } // namespace browser_sync 158 #endif // CHROME_BROWSER_SYNC_GLUE_MODEL_ASSOCIATION_MANAGER_H__ 159