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_UI_DATA_TYPE_CONTROLLER_H__
6 #define CHROME_BROWSER_SYNC_GLUE_UI_DATA_TYPE_CONTROLLER_H__
10 #include "base/basictypes.h"
11 #include "base/compiler_specific.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/weak_ptr.h"
14 #include "chrome/browser/sync/glue/shared_change_processor.h"
15 #include "components/sync_driver/data_type_controller.h"
18 class ProfileSyncService;
19 class ProfileSyncComponentsFactory;
26 class SyncableService;
30 namespace browser_sync {
32 // Implementation for datatypes that reside on the (UI thread). This is the same
33 // thread we perform initialization on, so we don't have to worry about thread
34 // safety. The main start/stop funtionality is implemented by default.
35 // Note: RefCountedThreadSafe by way of DataTypeController.
36 class UIDataTypeController : public DataTypeController {
39 scoped_refptr<base::MessageLoopProxy> ui_thread,
40 const base::Closure& error_callback,
41 syncer::ModelType type,
42 ProfileSyncComponentsFactory* profile_sync_factory,
44 ProfileSyncService* sync_service);
46 // DataTypeController interface.
47 virtual void LoadModels(
48 const ModelLoadCallback& model_load_callback) OVERRIDE;
49 virtual void StartAssociating(const StartCallback& start_callback) OVERRIDE;
50 virtual void Stop() OVERRIDE;
51 virtual syncer::ModelType type() const OVERRIDE;
52 virtual syncer::ModelSafeGroup model_safe_group() const OVERRIDE;
53 virtual ChangeProcessor* GetChangeProcessor() const OVERRIDE;
54 virtual std::string name() const OVERRIDE;
55 virtual State state() const OVERRIDE;
57 // DataTypeErrorHandler interface.
58 virtual void OnSingleDatatypeUnrecoverableError(
59 const tracked_objects::Location& from_here,
60 const std::string& message) OVERRIDE;
62 // Used by tests to override the factory used to create
63 // GenericChangeProcessors.
64 void SetGenericChangeProcessorFactoryForTest(
65 scoped_ptr<GenericChangeProcessorFactory> factory);
69 UIDataTypeController();
70 // DataTypeController is RefCounted.
71 virtual ~UIDataTypeController();
73 // Start any dependent services that need to be running before we can
74 // associate models. The default implementation is a no-op.
76 // True - if models are ready and association can proceed.
77 // False - if models are not ready. Associate() should be called when the
79 virtual bool StartModels();
81 // Perform any DataType controller specific state cleanup before stopping
82 // the datatype controller. The default implementation is a no-op.
83 virtual void StopModels();
85 // DataTypeController interface.
86 virtual void OnModelLoaded() OVERRIDE;
88 // Helper method for cleaning up state and invoking the start callback.
89 virtual void StartDone(StartResult result,
90 const syncer::SyncMergeResult& local_merge_result,
91 const syncer::SyncMergeResult& syncer_merge_result);
93 // Record association time.
94 virtual void RecordAssociationTime(base::TimeDelta time);
95 // Record causes of start failure.
96 virtual void RecordStartFailure(StartResult result);
98 ProfileSyncComponentsFactory* const profile_sync_factory_;
99 Profile* const profile_;
100 ProfileSyncService* const sync_service_;
104 StartCallback start_callback_;
105 ModelLoadCallback model_load_callback_;
107 // The sync datatype being controlled.
108 syncer::ModelType type_;
110 // Sync's interface to the datatype. All sync changes for |type_| are pushed
111 // through it to the datatype as well as vice versa.
113 // Lifetime: it gets created when Start()) is called, and a reference to it
114 // is passed to |local_service_| during Associate(). We release our reference
115 // when Stop() or StartFailed() is called, and |local_service_| releases its
116 // reference when local_service_->StopSyncing() is called or when it is
119 // Note: we use refcounting here primarily so that we can keep a uniform
120 // SyncableService API, whether the datatype lives on the UI thread or not
121 // (a syncer::SyncableService takes ownership of its SyncChangeProcessor when
122 // MergeDataAndStartSyncing is called). This will help us eventually merge the
123 // two datatype controller implementations (for ui and non-ui thread
125 scoped_refptr<SharedChangeProcessor> shared_change_processor_;
127 scoped_ptr<GenericChangeProcessorFactory> processor_factory_;
129 // A weak pointer to the actual local syncable service, which performs all the
130 // real work. We do not own the object.
131 base::WeakPtr<syncer::SyncableService> local_service_;
134 // Associate the sync model with the service's model, then start syncing.
135 virtual void Associate();
137 virtual void AbortModelLoad();
139 DISALLOW_COPY_AND_ASSIGN(UIDataTypeController);
142 } // namespace browser_sync
144 #endif // CHROME_BROWSER_SYNC_GLUE_UI_DATA_TYPE_CONTROLLER_H__