Wrapper script for isolating telemetry_gpu_unittests.
[chromium-blink-merge.git] / components / sync_driver / model_association_manager.h
blob56d291fef7414f5a9b41b9bf1d4305007e944a44
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.
5 #ifndef COMPONENTS_SYNC_DRIVER_MODEL_ASSOCIATION_MANAGER_H__
6 #define COMPONENTS_SYNC_DRIVER_MODEL_ASSOCIATION_MANAGER_H__
8 #include <map>
10 #include "base/memory/weak_ptr.h"
11 #include "base/timer/timer.h"
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"
17 namespace sync_driver {
19 class DataTypeController;
21 // |ModelAssociationManager| does the heavy lifting for doing the actual model
22 // association. It instructs DataTypeControllers to load models, start
23 // associating and stopping. Since the operations are async it uses an
24 // interface to inform DataTypeManager the results of the operations.
25 // This class is owned by DataTypeManager.
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;
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,
40 const syncer::SyncError& error) = 0;
42 // Called when the ModelAssociationManager has tried to perform model
43 // association for all desired types and has nothing left to do.
44 virtual void OnModelAssociationDone(
45 const DataTypeManager::ConfigureResult& result) = 0;
46 virtual ~ModelAssociationManagerDelegate() {}
49 // The class that is responsible for model association.
50 class ModelAssociationManager {
51 public:
52 enum State {
53 // No configuration is in progress.
54 IDLE,
55 // The model association manager has been initialized with a set of desired
56 // types, but is not actively associating any.
57 INITIALIZED,
58 // One or more types from |desired_types_| are in the process of
59 // associating.
60 ASSOCIATING,
63 ModelAssociationManager(const DataTypeController::TypeMap* controllers,
64 ModelAssociationManagerDelegate* delegate);
65 virtual ~ModelAssociationManager();
67 // Initializes the state to do the model association in future. This
68 // should be called before communicating with sync server. A subsequent call
69 // of Initialize is only allowed if the ModelAssociationManager has invoked
70 // |OnModelAssociationDone| on the |ModelAssociationManagerDelegate|. After
71 // this call, there should be several calls to StartAssociationAsync()
72 // to associate subset of |desired_types|.
73 void Initialize(syncer::ModelTypeSet desired_types);
75 // Can be called at any time. Synchronously stops all datatypes.
76 void Stop();
78 // Should only be called after Initialize to start the actual association.
79 // |types_to_associate| should be subset of |desired_types| in Initialize().
80 // When this is completed, |OnModelAssociationDone| will be invoked.
81 void StartAssociationAsync(const syncer::ModelTypeSet& types_to_associate);
83 // This is used for TESTING PURPOSE ONLY. The test case can inspect
84 // and modify the timer.
85 // TODO(sync) : This would go away if we made this class be able to do
86 // Dependency injection. crbug.com/129212.
87 base::OneShotTimer<ModelAssociationManager>* GetTimerForTesting();
89 State state() const { return state_; }
91 private:
92 // Called at the end of association to reset state to prepare for next
93 // round of association.
94 void ResetForNextAssociation();
96 // Called by Initialize() to stop types that are not in |desired_types_|.
97 void StopDisabledTypes();
99 // Start loading non-running types that are in |desired_types_|.
100 void LoadEnabledTypes();
102 // Callback passed to each data type controller on starting association. This
103 // callback will be invoked when the model association is done.
104 void TypeStartCallback(syncer::ModelType type,
105 base::TimeTicks type_start_time,
106 DataTypeController::ConfigureResult start_result,
107 const syncer::SyncMergeResult& local_merge_result,
108 const syncer::SyncMergeResult& syncer_merge_result);
110 // Callback that will be invoked when the models finish loading. This callback
111 // will be passed to |LoadModels| function.
112 void ModelLoadCallback(syncer::ModelType type, syncer::SyncError error);
114 // Called when all requested types are associated or association times out.
115 // Will clean up any unfinished types, and update |state_| to be |new_state|
116 // Finally, it will notify |delegate_| of the configuration result.
117 void ModelAssociationDone(State new_state);
119 // A helper to stop an individual datatype.
120 void StopDatatype(const syncer::SyncError& error, DataTypeController* dtc);
122 State state_;
124 // Data types that are enabled.
125 syncer::ModelTypeSet desired_types_;
127 // Data types that are requested to associate.
128 syncer::ModelTypeSet requested_types_;
130 // Data types currently being associated, including types waiting for model
131 // load.
132 syncer::ModelTypeSet associating_types_;
134 // Data types that are loaded, i.e. ready to associate.
135 syncer::ModelTypeSet loaded_types_;
137 // Data types that are associated, i.e. no more action needed during
138 // reconfiguration if not disabled.
139 syncer::ModelTypeSet associated_types_;
141 // Time when StartAssociationAsync() is called to associate for a set of data
142 // types.
143 base::TimeTicks association_start_time_;
145 // Set of all registered controllers.
146 const DataTypeController::TypeMap* controllers_;
148 // The processor in charge of handling model association results.
149 ModelAssociationManagerDelegate* delegate_;
151 // Timer to track and limit how long a datatype takes to model associate.
152 base::OneShotTimer<ModelAssociationManager> timer_;
154 DataTypeManager::ConfigureStatus configure_status_;
156 base::WeakPtrFactory<ModelAssociationManager> weak_ptr_factory_;
158 DISALLOW_COPY_AND_ASSIGN(ModelAssociationManager);
161 } // namespace sync_driver
163 #endif // COMPONENTS_SYNC_DRIVER_MODEL_ASSOCIATION_MANAGER_H__