OLD | NEW |
| (Empty) |
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_NON_UI_DATA_TYPE_CONTROLLER_H_ | |
6 #define COMPONENTS_SYNC_DRIVER_NON_UI_DATA_TYPE_CONTROLLER_H_ | |
7 | |
8 #include <memory> | |
9 #include <string> | |
10 | |
11 #include "base/compiler_specific.h" | |
12 #include "base/memory/ref_counted.h" | |
13 #include "base/memory/weak_ptr.h" | |
14 #include "base/sequenced_task_runner.h" | |
15 #include "components/sync/driver/directory_data_type_controller.h" | |
16 #include "components/sync/driver/shared_change_processor.h" | |
17 | |
18 namespace syncer { | |
19 | |
20 class SyncClient; | |
21 struct UserShare; | |
22 | |
23 // Implementation for datatypes that reside on non-UI thread. All interaction | |
24 // with datatype controller happens on UI thread. Calls to SyncableService are | |
25 // posted to model thread through PostTaskOnModelThread(). | |
26 // Note: RefCountedThreadSafe by way of DataTypeController. | |
27 class NonUIDataTypeController : public DirectoryDataTypeController { | |
28 public: | |
29 // |dump_stack| is called when an unrecoverable error occurs. | |
30 NonUIDataTypeController( | |
31 ModelType type, | |
32 const base::Closure& dump_stack, | |
33 SyncClient* sync_client, | |
34 ModelSafeGroup model_safe_group, | |
35 scoped_refptr<base::SequencedTaskRunner> model_thread); | |
36 ~NonUIDataTypeController() override; | |
37 | |
38 // DataTypeController interface. | |
39 void LoadModels(const ModelLoadCallback& model_load_callback) override; | |
40 void StartAssociating(const StartCallback& start_callback) override; | |
41 void Stop() override; | |
42 ChangeProcessor* GetChangeProcessor() const override; | |
43 std::string name() const override; | |
44 State state() const override; | |
45 | |
46 // Used by tests to override the factory used to create | |
47 // GenericChangeProcessors. | |
48 void SetGenericChangeProcessorFactoryForTest( | |
49 std::unique_ptr<GenericChangeProcessorFactory> factory); | |
50 | |
51 protected: | |
52 // For testing only. | |
53 NonUIDataTypeController(); | |
54 | |
55 // Start any dependent services that need to be running before we can | |
56 // associate models. The default implementation is a no-op. | |
57 // Return value: | |
58 // True - if models are ready and association can proceed. | |
59 // False - if models are not ready. StartAssociationAsync should be called | |
60 // when the models are ready. | |
61 // Note: this is performed on the UI thread. | |
62 virtual bool StartModels(); | |
63 | |
64 // Perform any DataType controller specific state cleanup before stopping | |
65 // the datatype controller. The default implementation is a no-op. | |
66 // Note: this is performed on the UI thread. | |
67 virtual void StopModels(); | |
68 | |
69 // Posts the given task to the model thread, i.e. the thread the datatype | |
70 // lives on. Return value: True if task posted successfully, false otherwise. | |
71 // Default implementation posts task to model_thread_. Types that don't use | |
72 // TaskRunner need to override this method. | |
73 virtual bool PostTaskOnModelThread(const tracked_objects::Location& from_here, | |
74 const base::Closure& task); | |
75 | |
76 // Start up complete, update the state and invoke the callback. | |
77 virtual void StartDone(DataTypeController::ConfigureResult start_result, | |
78 const SyncMergeResult& local_merge_result, | |
79 const SyncMergeResult& syncer_merge_result); | |
80 | |
81 // Kick off the association process. | |
82 virtual bool StartAssociationAsync(); | |
83 | |
84 // Record causes of start failure. | |
85 virtual void RecordStartFailure(ConfigureResult result); | |
86 | |
87 // To allow unit tests to control thread interaction during non-ui startup | |
88 // and shutdown, use a factory method to create the SharedChangeProcessor. | |
89 virtual SharedChangeProcessor* CreateSharedChangeProcessor(); | |
90 | |
91 // If the DTC is waiting for models to load, once the models are | |
92 // loaded the datatype service will call this function on DTC to let | |
93 // us know that it is safe to start associating. | |
94 void OnModelLoaded(); | |
95 | |
96 std::unique_ptr<DataTypeErrorHandler> CreateErrorHandler() override; | |
97 | |
98 private: | |
99 // Posted on the backend thread by StartAssociationAsync(). | |
100 void StartAssociationWithSharedChangeProcessor( | |
101 const scoped_refptr<SharedChangeProcessor>& shared_change_processor); | |
102 | |
103 // Calls Disconnect() on |shared_change_processor_|, then sets it to | |
104 // null. Must be called only by StartDoneImpl() or Stop() (on the | |
105 // UI thread) and only after a call to Start() (i.e., | |
106 // |shared_change_processor_| must be non-null). | |
107 void DisconnectSharedChangeProcessor(); | |
108 | |
109 // Posts StopLocalService() to the processor on the model type thread. | |
110 void StopSyncableService(); | |
111 | |
112 // Disable this type with the sync service. Should only be invoked in case of | |
113 // an unrecoverable error. | |
114 // Note: this is performed on the UI thread. | |
115 void DisableImpl(const SyncError& error); | |
116 | |
117 // UserShare is stored in StartAssociating while on UI thread and | |
118 // passed to SharedChangeProcessor::Connect on the model thread. | |
119 UserShare* user_share_; | |
120 | |
121 // Factory is used by tests to inject custom implementation of | |
122 // GenericChangeProcessor. | |
123 std::unique_ptr<GenericChangeProcessorFactory> processor_factory_; | |
124 | |
125 // State of this datatype controller. | |
126 State state_; | |
127 | |
128 // Callbacks for use when starting the datatype. | |
129 StartCallback start_callback_; | |
130 ModelLoadCallback model_load_callback_; | |
131 | |
132 // Task runner of the model thread. Can be nullptr in which case datatype | |
133 // controller needs to override PostTaskOnModelThread(). | |
134 scoped_refptr<base::SequencedTaskRunner> model_thread_; | |
135 | |
136 // The shared change processor is the thread-safe interface to the | |
137 // datatype. We hold a reference to it from the UI thread so that | |
138 // we can call Disconnect() on it from Stop()/StartDoneImpl(). Most | |
139 // of the work is done on the backend thread, and in | |
140 // StartAssociationWithSharedChangeProcessor() for this class in | |
141 // particular. | |
142 // | |
143 // Lifetime: The SharedChangeProcessor object is created on the UI | |
144 // thread and passed on to the backend thread. This reference is | |
145 // released on the UI thread in Stop()/StartDoneImpl(), but the | |
146 // backend thread may still have references to it (which is okay, | |
147 // since we call Disconnect() before releasing the UI thread | |
148 // reference). | |
149 scoped_refptr<SharedChangeProcessor> shared_change_processor_; | |
150 }; | |
151 | |
152 } // namespace syncer | |
153 | |
154 #endif // COMPONENTS_SYNC_DRIVER_NON_UI_DATA_TYPE_CONTROLLER_H_ | |
OLD | NEW |