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
3// found in the LICENSE file.
4
5#ifndef CHROME_BROWSER_SYNC_GLUE_FRONTEND_DATA_TYPE_CONTROLLER_H__
6#define CHROME_BROWSER_SYNC_GLUE_FRONTEND_DATA_TYPE_CONTROLLER_H__
7
8#include <string>
9
10#include "base/basictypes.h"
11#include "base/compiler_specific.h"
12#include "base/memory/scoped_ptr.h"
13#include "components/sync_driver/data_type_controller.h"
14#include "components/sync_driver/data_type_error_handler.h"
15
16class Profile;
17class ProfileSyncService;
18class ProfileSyncComponentsFactory;
19
20namespace base {
21class TimeDelta;
22}
23
24namespace syncer {
25class SyncError;
26}
27
28namespace sync_driver {
29class AssociatorInterface;
30class ChangeProcessor;
31}
32
33namespace browser_sync {
34
35// Implementation for datatypes that reside on the frontend thread
36// (UI thread). This is the same thread we perform initialization on, so we
37// don't have to worry about thread safety. The main start/stop funtionality is
38// implemented by default.
39// Derived classes must implement (at least):
40//    syncer::ModelType type() const
41//    void CreateSyncComponents();
42// NOTE: This class is deprecated! New sync datatypes should be using the
43// syncer::SyncableService API and the UIDataTypeController instead.
44// TODO(zea): Delete this once all types are on the new API.
45class FrontendDataTypeController : public sync_driver::DataTypeController {
46 public:
47  FrontendDataTypeController(
48      scoped_refptr<base::MessageLoopProxy> ui_thread,
49      const base::Closure& error_callback,
50      ProfileSyncComponentsFactory* profile_sync_factory,
51      Profile* profile,
52      ProfileSyncService* sync_service);
53
54  // DataTypeController interface.
55  virtual void LoadModels(
56      const ModelLoadCallback& model_load_callback) OVERRIDE;
57  virtual void StartAssociating(const StartCallback& start_callback) OVERRIDE;
58  virtual void Stop() OVERRIDE;
59  virtual syncer::ModelType type() const = 0;
60  virtual syncer::ModelSafeGroup model_safe_group() const OVERRIDE;
61  virtual std::string name() const OVERRIDE;
62  virtual State state() const OVERRIDE;
63
64  // DataTypeErrorHandler interface.
65  virtual void OnSingleDataTypeUnrecoverableError(
66      const syncer::SyncError& error) OVERRIDE;
67
68 protected:
69  friend class FrontendDataTypeControllerMock;
70
71  // For testing only.
72  FrontendDataTypeController();
73  virtual ~FrontendDataTypeController();
74
75  // Kick off any dependent services that need to be running before we can
76  // associate models. The default implementation is a no-op.
77  // Return value:
78  //   True - if models are ready and association can proceed.
79  //   False - if models are not ready. Associate() should be called when the
80  //           models are ready. Refer to Start(_) implementation.
81  virtual bool StartModels();
82
83  // Datatype specific creation of sync components.
84  virtual void CreateSyncComponents() = 0;
85
86  // DataTypeController interface.
87  virtual void OnModelLoaded() OVERRIDE;
88
89  // Perform any DataType controller specific state cleanup before stopping
90  // the datatype controller. The default implementation is a no-op.
91  virtual void CleanUpState();
92
93  // Helper method for cleaning up state and running the start callback.
94  virtual void StartDone(
95      ConfigureResult start_result,
96      const syncer::SyncMergeResult& local_merge_result,
97      const syncer::SyncMergeResult& syncer_merge_result);
98
99  // Record association time.
100  virtual void RecordAssociationTime(base::TimeDelta time);
101  // Record causes of start failure.
102  virtual void RecordStartFailure(ConfigureResult result);
103
104  virtual sync_driver::AssociatorInterface* model_associator() const;
105  virtual void set_model_associator(
106      sync_driver::AssociatorInterface* associator);
107  virtual sync_driver::ChangeProcessor* GetChangeProcessor() const OVERRIDE;
108  virtual void set_change_processor(sync_driver::ChangeProcessor* processor);
109
110  // Handles the reporting of unrecoverable error. It records stuff in
111  // UMA and reports to breakpad.
112  // Virtual for testing purpose.
113  virtual void RecordUnrecoverableError(
114      const tracked_objects::Location& from_here,
115      const std::string& message);
116
117  ProfileSyncComponentsFactory* const profile_sync_factory_;
118  Profile* const profile_;
119  ProfileSyncService* const sync_service_;
120
121  State state_;
122
123  StartCallback start_callback_;
124  ModelLoadCallback model_load_callback_;
125
126  // TODO(sync): transition all datatypes to SyncableService and deprecate
127  // AssociatorInterface.
128  scoped_ptr<sync_driver::AssociatorInterface> model_associator_;
129  scoped_ptr<sync_driver::ChangeProcessor> change_processor_;
130
131 private:
132  // Build sync components and associate models.
133  // Return value:
134  //   True - if association was successful. FinishStart should have been
135  //          invoked.
136  //   False - if association failed. StartFailed should have been invoked.
137  virtual bool Associate();
138
139  void AbortModelLoad();
140
141  // Clean up our state and state variables. Called in response
142  // to a failure or abort or stop.
143  void CleanUp();
144
145  DISALLOW_COPY_AND_ASSIGN(FrontendDataTypeController);
146};
147
148}  // namespace browser_sync
149
150#endif  // CHROME_BROWSER_SYNC_GLUE_FRONTEND_DATA_TYPE_CONTROLLER_H__
151