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__
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 // |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
{
24 class DataTypeController
;
26 // |ModelAssociationManager| association functions are async. The results of
27 // those operations are passed back via this interface.
28 class ModelAssociationResultProcessor
{
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() {}
38 // The class that is responsible for model association.
39 class ModelAssociationManager
{
41 ModelAssociationManager(const DataTypeController::TypeMap
* controllers
,
42 ModelAssociationResultProcessor
* processor
);
43 virtual ~ModelAssociationManager();
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
);
53 // Can be called at any time. Synchronously stops all datatypes.
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
);
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();
69 // This is the state after |Initialize| is called.
70 INITIALIZED_TO_CONFIGURE
,
71 // Starting a new configuration.
73 // No configuration is in progress.
77 // Called at the end of association to reset state to prepare for next
78 // round of association.
79 void ResetForNextAssociation();
81 // Called by Initialize() to stop types that are not in |desired_types_|.
82 void StopDisabledTypes();
84 // Start loading non-running types that are in |desired_types_|.
85 void LoadEnabledTypes();
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
);
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
);
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
);
103 // Called when all requested types are associated or association times out.
104 // Notify |result_processor_| of configuration results.
105 void ModelAssociationDone();
109 // Data types that are enabled.
110 syncer::ModelTypeSet desired_types_
;
112 // Data types that are requested to associate.
113 syncer::ModelTypeSet requested_types_
;
115 // Data types currently being associated, including types waiting for model
117 syncer::ModelTypeSet associating_types_
;
119 // Data types that are loaded, i.e. ready to associate.
120 syncer::ModelTypeSet loaded_types_
;
122 // Data types that are associated, i.e. no more action needed during
123 // reconfiguration if not disabled.
124 syncer::ModelTypeSet associated_types_
;
126 // Data types that are still loading/associating when configuration times
128 syncer::ModelTypeSet slow_types_
;
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_
;
135 // The set of types that can't configure due to cryptographer errors.
136 syncer::ModelTypeSet needs_crypto_types_
;
138 // Time when StartAssociationAsync() is called to associate for a set of data
140 base::TimeTicks association_start_time_
;
142 // Set of all registered controllers.
143 const DataTypeController::TypeMap
* controllers_
;
145 // The processor in charge of handling model association results.
146 ModelAssociationResultProcessor
* result_processor_
;
148 // Timer to track and limit how long a datatype takes to model associate.
149 base::OneShotTimer
<ModelAssociationManager
> timer_
;
151 base::WeakPtrFactory
<ModelAssociationManager
> weak_ptr_factory_
;
153 DataTypeManager::ConfigureStatus configure_status_
;
155 DISALLOW_COPY_AND_ASSIGN(ModelAssociationManager
);
157 } // namespace browser_sync
158 #endif // COMPONENTS_SYNC_DRIVER_MODEL_ASSOCIATION_MANAGER_H__