| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 CHROME_BROWSER_SYNC_GLUE_MODEL_ASSOCIATION_MANAGER_H__ | 5 #ifndef CHROME_BROWSER_SYNC_GLUE_MODEL_ASSOCIATION_MANAGER_H__ |
| 6 #define CHROME_BROWSER_SYNC_GLUE_MODEL_ASSOCIATION_MANAGER_H__ | 6 #define CHROME_BROWSER_SYNC_GLUE_MODEL_ASSOCIATION_MANAGER_H__ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <list> | 9 #include <list> |
| 10 | 10 |
| 11 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
| 12 #include "base/timer.h" |
| 12 | 13 |
| 13 #include "chrome/browser/sync/glue/data_type_manager.h" | 14 #include "chrome/browser/sync/glue/data_type_manager.h" |
| 14 | 15 |
| 15 // |ModelAssociationManager| does the heavy lifting for doing the actual model | 16 // |ModelAssociationManager| does the heavy lifting for doing the actual model |
| 16 // association. It instructs DataTypeControllers to load models, start | 17 // association. It instructs DataTypeControllers to load models, start |
| 17 // associating and stopping. Since the operations are async it uses an | 18 // associating and stopping. Since the operations are async it uses an |
| 18 // interface to inform DTM the results of the operations. | 19 // interface to inform DataTypeManager the results of the operations. |
| 19 // This class is owned by DTM. | 20 // This class is owned by DataTypeManager. |
| 20 namespace browser_sync { | 21 namespace browser_sync { |
| 21 | 22 |
| 22 class DataTypeController; | 23 class DataTypeController; |
| 23 | 24 |
| 24 // |ModelAssociationManager| association functions are async. The results of | 25 // |ModelAssociationManager| association functions are async. The results of |
| 25 // those operations are passed back via this interface. | 26 // those operations are passed back via this interface. |
| 26 class ModelAssociationResultProcessor { | 27 class ModelAssociationResultProcessor { |
| 27 public: | 28 public: |
| 28 virtual void OnModelAssociationDone( | 29 virtual void OnModelAssociationDone( |
| 29 const DataTypeManager::ConfigureResult& result) = 0; | 30 const DataTypeManager::ConfigureResult& result) = 0; |
| 30 virtual ~ModelAssociationResultProcessor() {} | 31 virtual ~ModelAssociationResultProcessor() {} |
| 32 |
| 33 // Called to let the |ModelAssociationResultProcessor| know that "delayed" |
| 34 // types have finished loading and association should take place. (A delayed |
| 35 // type here is a type that did not finish loading during the previous |
| 36 // configure cycle.) |
| 37 virtual void OnTypesLoaded() = 0; |
| 31 }; | 38 }; |
| 32 | 39 |
| 33 // The class that is responsible for model association. | 40 // The class that is responsible for model association. |
| 34 class ModelAssociationManager { | 41 class ModelAssociationManager { |
| 35 public: | 42 public: |
| 36 ModelAssociationManager(const DataTypeController::TypeMap* controllers, | 43 ModelAssociationManager(const DataTypeController::TypeMap* controllers, |
| 37 ModelAssociationResultProcessor* processor); | 44 ModelAssociationResultProcessor* processor); |
| 38 virtual ~ModelAssociationManager(); | 45 virtual ~ModelAssociationManager(); |
| 39 | 46 |
| 40 // Initializes the state to do the model association in future. This | 47 // Initializes the state to do the model association in future. This |
| 41 // should be called before communicating with sync server. A subsequent call | 48 // should be called before communicating with sync server. A subsequent call |
| 42 // of Initialize is only allowed if the current configure cycle is completed, | 49 // of Initialize is only allowed if the ModelAssociationManager has invoked |
| 43 // aborted or stopped. | 50 // |OnModelAssociationDone| on the |ModelAssociationResultProcessor|. |
| 44 void Initialize(syncable::ModelTypeSet desired_types); | 51 void Initialize(syncable::ModelTypeSet desired_types); |
| 45 | 52 |
| 46 // Can be called at any time. Synchronously stops all datatypes. | 53 // Can be called at any time. Synchronously stops all datatypes. |
| 47 void Stop(); | 54 void Stop(); |
| 48 | 55 |
| 49 // Should only be called after Initialize. | 56 // Should only be called after Initialize. |
| 50 // Starts the actual association. When this is completed | 57 // Starts the actual association. When this is completed |
| 51 // |OnModelAssociationDone| would be called invoked. | 58 // |OnModelAssociationDone| would be called invoked. |
| 52 void StartAssociationAsync(); | 59 void StartAssociationAsync(); |
| 53 | 60 |
| 54 // It is valid to call this only when we are initialized to configure | 61 // It is valid to call this only when we are initialized to configure |
| 55 // but we have not started the configuration.(i.e., |Initialize| has | 62 // but we have not started the configuration.(i.e., |Initialize| has |
| 56 // been called but |StartAssociationAsync| has not yet been called.) | 63 // been called but |StartAssociationAsync| has not yet been called.) |
| 57 // If we have started configuration then the DTM will wait until | 64 // If we have started configuration then the DataTypeManager will wait until |
| 58 // the current configuration is done before processing the reconfigure | 65 // the current configuration is done before processing the reconfigure |
| 59 // request. We goto IDLE state and clear all our internal state. It is | 66 // request. We goto IDLE state and clear all our internal state. It is |
| 60 // safe to do this as we have not started association on any DTCs. | 67 // safe to do this as we have not started association on any DTCs. |
| 61 void ResetForReconfiguration(); | 68 void ResetForReconfiguration(); |
| 62 | 69 |
| 63 // Should only be called after Initialize. | 70 // Should only be called after Initialize. |
| 64 // Stops any disabled types. | 71 // Stops any disabled types. |
| 65 void StopDisabledTypes(); | 72 void StopDisabledTypes(); |
| 66 | 73 |
| 74 // This is used for TESTING PURPOSE ONLY. The test case can inspect |
| 75 // and modify the timer. |
| 76 // TODO(sync) : This would go away if we made this class be able to do |
| 77 // Dependency injection. crbug.com/129212. |
| 78 base::OneShotTimer<ModelAssociationManager>* GetTimerForTesting(); |
| 79 |
| 67 private: | 80 private: |
| 68 enum State { | 81 enum State { |
| 69 // This is the state after |Initialize| is called. | 82 // This is the state after |Initialize| is called. |
| 70 INITIAILIZED_TO_CONFIGURE, | 83 INITIAILIZED_TO_CONFIGURE, |
| 71 // Starting a new configuration. | 84 // Starting a new configuration. |
| 72 CONFIGURING, | 85 CONFIGURING, |
| 73 // A stop command was issued. | 86 // A stop command was issued. |
| 74 ABORTED, | 87 ABORTED, |
| 75 // No configuration is in progress. | 88 // No configuration is in progress. |
| 76 IDLE | 89 IDLE |
| (...skipping 13 matching lines...) Expand all Loading... |
| 90 // will be passed to |LoadModels| function. | 103 // will be passed to |LoadModels| function. |
| 91 void ModelLoadCallback(syncable::ModelType type, SyncError error); | 104 void ModelLoadCallback(syncable::ModelType type, SyncError error); |
| 92 | 105 |
| 93 // Calls the |LoadModels| method on the next controller waiting to start. | 106 // Calls the |LoadModels| method on the next controller waiting to start. |
| 94 void LoadModelForNextType(); | 107 void LoadModelForNextType(); |
| 95 | 108 |
| 96 // Calls |StartAssociating| on the next available controller whose models are | 109 // Calls |StartAssociating| on the next available controller whose models are |
| 97 // loaded. | 110 // loaded. |
| 98 void StartAssociatingNextType(); | 111 void StartAssociatingNextType(); |
| 99 | 112 |
| 113 // When a type fails to load or fails associating this method is invoked to |
| 114 // do the book keeping and do the UMA reporting. |
| 115 void HandleFailedTypes(DataTypeController::StartResult result, |
| 116 const SyncError& error); |
| 117 |
| 118 syncable::ModelTypeSet GetTypesWaitingToLoad(); |
| 119 |
| 120 |
| 100 State state_; | 121 State state_; |
| 101 syncable::ModelTypeSet desired_types_; | 122 syncable::ModelTypeSet desired_types_; |
| 102 std::list<SyncError> failed_datatypes_info_; | 123 std::list<SyncError> failed_datatypes_info_; |
| 103 std::map<syncable::ModelType, int> start_order_; | 124 std::map<syncable::ModelType, int> start_order_; |
| 104 | 125 |
| 105 // This illustration explains the movement of one DTC through various lists. | 126 // This illustration explains the movement of one DTC through various lists. |
| 106 // Consider a dataype, say, BOOKMARKS which is NOT_RUNNING and will be | 127 // Consider a dataype, say, BOOKMARKS which is NOT_RUNNING and will be |
| 107 // configured now. | 128 // configured now. |
| 108 // Initially all lists are empty. BOOKMARKS is in the |controllers_| | 129 // Initially all lists are empty. BOOKMARKS is in the |controllers_| |
| 109 // map. Here is how the controller moves to various list | 130 // map. Here is how the controller moves to various list |
| (...skipping 23 matching lines...) Expand all Loading... |
| 133 // list to this list as they finish loading their model. | 154 // list to this list as they finish loading their model. |
| 134 std::vector<DataTypeController*> waiting_to_associate_; | 155 std::vector<DataTypeController*> waiting_to_associate_; |
| 135 | 156 |
| 136 // Controller currently doing model association. | 157 // Controller currently doing model association. |
| 137 DataTypeController* currently_associating_; | 158 DataTypeController* currently_associating_; |
| 138 | 159 |
| 139 // Set of all registered controllers. | 160 // Set of all registered controllers. |
| 140 const DataTypeController::TypeMap* controllers_; | 161 const DataTypeController::TypeMap* controllers_; |
| 141 ModelAssociationResultProcessor* result_processor_; | 162 ModelAssociationResultProcessor* result_processor_; |
| 142 base::WeakPtrFactory<ModelAssociationManager> weak_ptr_factory_; | 163 base::WeakPtrFactory<ModelAssociationManager> weak_ptr_factory_; |
| 164 base::OneShotTimer<ModelAssociationManager> timer_; |
| 143 DISALLOW_COPY_AND_ASSIGN(ModelAssociationManager); | 165 DISALLOW_COPY_AND_ASSIGN(ModelAssociationManager); |
| 144 }; | 166 }; |
| 145 } // namespace browser_sync | 167 } // namespace browser_sync |
| 146 #endif // CHROME_BROWSER_SYNC_GLUE_MODEL_ASSOCIATION_MANAGER_H__ | 168 #endif // CHROME_BROWSER_SYNC_GLUE_MODEL_ASSOCIATION_MANAGER_H__ |
| OLD | NEW |