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