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/memory/weak_ptr.h"
11#include "base/timer/timer.h"
12
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"
16
17namespace sync_driver {
18
19class DataTypeController;
20
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.
28class 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;
35
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;
41
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() {}
47};
48
49// The class that is responsible for model association.
50class ModelAssociationManager {
51 public:
52  ModelAssociationManager(const DataTypeController::TypeMap* controllers,
53                          ModelAssociationManagerDelegate* delegate);
54  virtual ~ModelAssociationManager();
55
56  // Initializes the state to do the model association in future. This
57  // should be called before communicating with sync server. A subsequent call
58  // of Initialize is only allowed if the ModelAssociationManager has invoked
59  // |OnModelAssociationDone| on the |ModelAssociationManagerDelegate|. After
60  // this call, there should be several calls to StartAssociationAsync()
61  // to associate subset of |desired_types|.
62  void Initialize(syncer::ModelTypeSet desired_types);
63
64  // Can be called at any time. Synchronously stops all datatypes.
65  void Stop();
66
67  // Should only be called after Initialize to start the actual association.
68  // |types_to_associate| should be subset of |desired_types| in Initialize().
69  // When this is completed, |OnModelAssociationDone| will be invoked.
70  void StartAssociationAsync(const syncer::ModelTypeSet& types_to_associate);
71
72  // This is used for TESTING PURPOSE ONLY. The test case can inspect
73  // and modify the timer.
74  // TODO(sync) : This would go away if we made this class be able to do
75  // Dependency injection. crbug.com/129212.
76   base::OneShotTimer<ModelAssociationManager>* GetTimerForTesting();
77
78 private:
79  enum State {
80    // This is the state after |Initialize| is called.
81    INITIALIZED_TO_CONFIGURE,
82    // Starting a new configuration.
83    CONFIGURING,
84    // No configuration is in progress.
85    IDLE
86  };
87
88  // Called at the end of association to reset state to prepare for next
89  // round of association.
90  void ResetForNextAssociation();
91
92  // Called by Initialize() to stop types that are not in |desired_types_|.
93  void StopDisabledTypes();
94
95  // Start loading non-running types that are in |desired_types_|.
96  void LoadEnabledTypes();
97
98  // Callback passed to each data type controller on starting association. This
99  // callback will be invoked when the model association is done.
100  void TypeStartCallback(syncer::ModelType type,
101                         base::TimeTicks type_start_time,
102                         DataTypeController::ConfigureResult start_result,
103                         const syncer::SyncMergeResult& local_merge_result,
104                         const syncer::SyncMergeResult& syncer_merge_result);
105
106  // Callback that will be invoked when the models finish loading. This callback
107  // will be passed to |LoadModels| function.
108  void ModelLoadCallback(syncer::ModelType type, syncer::SyncError error);
109
110  // Called when all requested types are associated or association times out.
111  // Notify |delegate_| of configuration results.
112  void ModelAssociationDone();
113
114  // A helper to stop an individual datatype.
115  void StopDatatype(const syncer::SyncError& error, DataTypeController* dtc);
116
117  State state_;
118
119  // Data types that are enabled.
120  syncer::ModelTypeSet desired_types_;
121
122  // Data types that are requested to associate.
123  syncer::ModelTypeSet requested_types_;
124
125  // Data types currently being associated, including types waiting for model
126  // load.
127  syncer::ModelTypeSet associating_types_;
128
129  // Data types that are loaded, i.e. ready to associate.
130  syncer::ModelTypeSet loaded_types_;
131
132  // Data types that are associated, i.e. no more action needed during
133  // reconfiguration if not disabled.
134  syncer::ModelTypeSet associated_types_;
135
136  // Time when StartAssociationAsync() is called to associate for a set of data
137  // types.
138  base::TimeTicks association_start_time_;
139
140  // Set of all registered controllers.
141  const DataTypeController::TypeMap* controllers_;
142
143  // The processor in charge of handling model association results.
144  ModelAssociationManagerDelegate* delegate_;
145
146  // Timer to track and limit how long a datatype takes to model associate.
147  base::OneShotTimer<ModelAssociationManager> timer_;
148
149  DataTypeManager::ConfigureStatus configure_status_;
150
151  base::WeakPtrFactory<ModelAssociationManager> weak_ptr_factory_;
152
153  DISALLOW_COPY_AND_ASSIGN(ModelAssociationManager);
154};
155
156}  // namespace sync_driver
157
158#endif  // COMPONENTS_SYNC_DRIVER_MODEL_ASSOCIATION_MANAGER_H__
159