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