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.
5 #ifndef CHROME_BROWSER_SYNC_GLUE_FRONTEND_DATA_TYPE_CONTROLLER_H__
6 #define CHROME_BROWSER_SYNC_GLUE_FRONTEND_DATA_TYPE_CONTROLLER_H__
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"
17 class ProfileSyncService;
18 class ProfileSyncComponentsFactory;
28 namespace browser_sync {
30 class AssociatorInterface;
31 class ChangeProcessor;
33 // Implementation for datatypes that reside on the frontend thread
34 // (UI thread). This is the same thread we perform initialization on, so we
35 // don't have to worry about thread safety. The main start/stop funtionality is
36 // implemented by default.
37 // Derived classes must implement (at least):
38 // syncer::ModelType type() const
39 // void CreateSyncComponents();
40 // NOTE: This class is deprecated! New sync datatypes should be using the
41 // syncer::SyncableService API and the UIDataTypeController instead.
42 // TODO(zea): Delete this once all types are on the new API.
43 class FrontendDataTypeController : public DataTypeController {
45 FrontendDataTypeController(
46 scoped_refptr<base::MessageLoopProxy> ui_thread,
47 const base::Closure& error_callback,
48 ProfileSyncComponentsFactory* profile_sync_factory,
50 ProfileSyncService* sync_service);
52 // DataTypeController interface.
53 virtual void LoadModels(
54 const ModelLoadCallback& model_load_callback) OVERRIDE;
55 virtual void StartAssociating(const StartCallback& start_callback) OVERRIDE;
56 virtual void Stop() OVERRIDE;
57 virtual syncer::ModelType type() const = 0;
58 virtual syncer::ModelSafeGroup model_safe_group() const OVERRIDE;
59 virtual std::string name() const OVERRIDE;
60 virtual State state() const OVERRIDE;
62 // DataTypeErrorHandler interface.
63 virtual void OnSingleDatatypeUnrecoverableError(
64 const tracked_objects::Location& from_here,
65 const std::string& message) OVERRIDE;
68 friend class FrontendDataTypeControllerMock;
71 FrontendDataTypeController();
72 virtual ~FrontendDataTypeController();
74 // Kick off any dependent services that need to be running before we can
75 // associate models. The default implementation is a no-op.
77 // True - if models are ready and association can proceed.
78 // False - if models are not ready. Associate() should be called when the
79 // models are ready. Refer to Start(_) implementation.
80 virtual bool StartModels();
82 // Datatype specific creation of sync components.
83 virtual void CreateSyncComponents() = 0;
85 // DataTypeController interface.
86 virtual void OnModelLoaded() OVERRIDE;
88 // Perform any DataType controller specific state cleanup before stopping
89 // the datatype controller. The default implementation is a no-op.
90 virtual void CleanUpState();
92 // Helper method for cleaning up state and running the start callback.
93 virtual void StartDone(
94 StartResult start_result,
95 const syncer::SyncMergeResult& local_merge_result,
96 const syncer::SyncMergeResult& syncer_merge_result);
98 // Record association time.
99 virtual void RecordAssociationTime(base::TimeDelta time);
100 // Record causes of start failure.
101 virtual void RecordStartFailure(StartResult result);
103 virtual AssociatorInterface* model_associator() const;
104 virtual void set_model_associator(AssociatorInterface* associator);
105 virtual ChangeProcessor* GetChangeProcessor() const OVERRIDE;
106 virtual void set_change_processor(ChangeProcessor* processor);
108 ProfileSyncComponentsFactory* const profile_sync_factory_;
109 Profile* const profile_;
110 ProfileSyncService* const sync_service_;
114 StartCallback start_callback_;
115 ModelLoadCallback model_load_callback_;
117 // TODO(sync): transition all datatypes to SyncableService and deprecate
118 // AssociatorInterface.
119 scoped_ptr<AssociatorInterface> model_associator_;
120 scoped_ptr<ChangeProcessor> change_processor_;
123 // Build sync components and associate models.
125 // True - if association was successful. FinishStart should have been
127 // False - if association failed. StartFailed should have been invoked.
128 virtual bool Associate();
130 void AbortModelLoad();
132 // Clean up our state and state variables. Called in response
133 // to a failure or abort or stop.
136 DISALLOW_COPY_AND_ASSIGN(FrontendDataTypeController);
139 } // namespace browser_sync
141 #endif // CHROME_BROWSER_SYNC_GLUE_FRONTEND_DATA_TYPE_CONTROLLER_H__