OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef COMPONENTS_SYNC_DRIVER_GLUE_SYNC_BACKEND_REGISTRAR_H_ | 5 #ifndef COMPONENTS_SYNC_DRIVER_GLUE_SYNC_BACKEND_REGISTRAR_H_ |
6 #define COMPONENTS_SYNC_DRIVER_GLUE_SYNC_BACKEND_REGISTRAR_H_ | 6 #define COMPONENTS_SYNC_DRIVER_GLUE_SYNC_BACKEND_REGISTRAR_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <map> | 10 #include <map> |
(...skipping 10 matching lines...) Expand all Loading... |
21 #include "components/sync/core/sync_manager.h" | 21 #include "components/sync/core/sync_manager.h" |
22 #include "components/sync/engine/model_safe_worker.h" | 22 #include "components/sync/engine/model_safe_worker.h" |
23 | 23 |
24 class Profile; | 24 class Profile; |
25 | 25 |
26 namespace base { | 26 namespace base { |
27 class MessageLoop; | 27 class MessageLoop; |
28 } | 28 } |
29 | 29 |
30 namespace syncer { | 30 namespace syncer { |
31 struct UserShare; | |
32 } // namespace syncer | |
33 | 31 |
34 namespace sync_driver { | |
35 class ChangeProcessor; | 32 class ChangeProcessor; |
36 class SyncClient; | 33 class SyncClient; |
37 } | |
38 | |
39 namespace browser_sync { | |
40 | |
41 class UIModelWorker; | 34 class UIModelWorker; |
| 35 struct UserShare; |
42 | 36 |
43 // A class that keep track of the workers, change processors, and | 37 // A class that keep track of the workers, change processors, and |
44 // routing info for the enabled sync types, and also routes change | 38 // routing info for the enabled sync types, and also routes change |
45 // events to the right processors. | 39 // events to the right processors. |
46 class SyncBackendRegistrar : public syncer::SyncManager::ChangeDelegate, | 40 class SyncBackendRegistrar : public SyncManager::ChangeDelegate, |
47 public syncer::WorkerLoopDestructionObserver { | 41 public WorkerLoopDestructionObserver { |
48 public: | 42 public: |
49 // |name| is used for debugging. Does not take ownership of |profile|. | 43 // |name| is used for debugging. Does not take ownership of |profile|. |
50 // Must be created on the UI thread. | 44 // Must be created on the UI thread. |
51 SyncBackendRegistrar( | 45 SyncBackendRegistrar( |
52 const std::string& name, | 46 const std::string& name, |
53 sync_driver::SyncClient* sync_client, | 47 SyncClient* sync_client, |
54 std::unique_ptr<base::Thread> sync_thread, | 48 std::unique_ptr<base::Thread> sync_thread, |
55 const scoped_refptr<base::SingleThreadTaskRunner>& ui_thread, | 49 const scoped_refptr<base::SingleThreadTaskRunner>& ui_thread, |
56 const scoped_refptr<base::SingleThreadTaskRunner>& db_thread, | 50 const scoped_refptr<base::SingleThreadTaskRunner>& db_thread, |
57 const scoped_refptr<base::SingleThreadTaskRunner>& file_thread); | 51 const scoped_refptr<base::SingleThreadTaskRunner>& file_thread); |
58 | 52 |
59 // SyncBackendRegistrar must be destroyed as follows: | 53 // SyncBackendRegistrar must be destroyed as follows: |
60 // | 54 // |
61 // 1) On the UI thread, call RequestWorkerStopOnUIThread(). | 55 // 1) On the UI thread, call RequestWorkerStopOnUIThread(). |
62 // 2) UI posts task to shut down syncer on sync thread. | 56 // 2) UI posts task to shut down syncer on sync thread. |
63 // 3) If sync is disabled, call ReleaseSyncThread() on the UI thread. | 57 // 3) If sync is disabled, call ReleaseSyncThread() on the UI thread. |
64 // 3) UI posts SyncBackendRegistrar::ShutDown() on sync thread to | 58 // 3) UI posts SyncBackendRegistrar::ShutDown() on sync thread to |
65 // unregister workers from observing destruction of their working loops. | 59 // unregister workers from observing destruction of their working loops. |
66 // 4) Workers notify registrar when unregistration finishes or working | 60 // 4) Workers notify registrar when unregistration finishes or working |
67 // loops are destroyed. Registrar destroys itself on last worker | 61 // loops are destroyed. Registrar destroys itself on last worker |
68 // notification. Sync thread will be stopped if ownership was not | 62 // notification. Sync thread will be stopped if ownership was not |
69 // released. | 63 // released. |
70 ~SyncBackendRegistrar() override; | 64 ~SyncBackendRegistrar() override; |
71 | 65 |
72 // Adds |type| to set of non-blocking types. These types are assigned to | 66 // Adds |type| to set of non-blocking types. These types are assigned to |
73 // GROUP_NON_BLOCKING model safe group and will be treated differently in | 67 // GROUP_NON_BLOCKING model safe group and will be treated differently in |
74 // ModelTypeRegistry. Unlike directory types, non-blocking types always stay | 68 // ModelTypeRegistry. Unlike directory types, non-blocking types always stay |
75 // assigned to GROUP_NON_BLOCKING group. | 69 // assigned to GROUP_NON_BLOCKING group. |
76 void RegisterNonBlockingType(syncer::ModelType type); | 70 void RegisterNonBlockingType(ModelType type); |
77 | 71 |
78 // Informs the SyncBackendRegistrar of the currently enabled set of types. | 72 // Informs the SyncBackendRegistrar of the currently enabled set of types. |
79 // These types will be placed in the passive group. This function should be | 73 // These types will be placed in the passive group. This function should be |
80 // called exactly once during startup. | 74 // called exactly once during startup. |
81 void SetInitialTypes(syncer::ModelTypeSet initial_types); | 75 void SetInitialTypes(ModelTypeSet initial_types); |
82 | 76 |
83 // Informs SyncBackendRegistrar about non-blocking type loaded from local | 77 // Informs SyncBackendRegistrar about non-blocking type loaded from local |
84 // storage. Initial sync was already performed for this type, therefore its | 78 // storage. Initial sync was already performed for this type, therefore its |
85 // data shouldn't be downloaded as part of configuration. | 79 // data shouldn't be downloaded as part of configuration. |
86 void AddRestoredNonBlockingType(syncer::ModelType type); | 80 void AddRestoredNonBlockingType(ModelType type); |
87 | 81 |
88 // Returns whether or not we are currently syncing encryption keys. | 82 // Returns whether or not we are currently syncing encryption keys. |
89 // Must be called on the UI thread. | 83 // Must be called on the UI thread. |
90 bool IsNigoriEnabled() const; | 84 bool IsNigoriEnabled() const; |
91 | 85 |
92 // Removes all types in |types_to_remove| from the routing info and | 86 // Removes all types in |types_to_remove| from the routing info and |
93 // adds all the types in |types_to_add| to the routing info that are | 87 // adds all the types in |types_to_add| to the routing info that are |
94 // not already there (initially put in the passive group). | 88 // not already there (initially put in the passive group). |
95 // |types_to_remove| and |types_to_add| must be disjoint. Returns | 89 // |types_to_remove| and |types_to_add| must be disjoint. Returns |
96 // the set of newly-added types. Must be called on the UI thread. | 90 // the set of newly-added types. Must be called on the UI thread. |
97 syncer::ModelTypeSet ConfigureDataTypes(syncer::ModelTypeSet types_to_add, | 91 ModelTypeSet ConfigureDataTypes(ModelTypeSet types_to_add, |
98 syncer::ModelTypeSet types_to_remove); | 92 ModelTypeSet types_to_remove); |
99 | 93 |
100 // Returns the set of enabled types as of the last configuration. Note that | 94 // Returns the set of enabled types as of the last configuration. Note that |
101 // this might be different from the current types in the routing info due | 95 // this might be different from the current types in the routing info due |
102 // to DeactiveDataType being called separately from ConfigureDataTypes. | 96 // to DeactiveDataType being called separately from ConfigureDataTypes. |
103 syncer::ModelTypeSet GetLastConfiguredTypes() const; | 97 ModelTypeSet GetLastConfiguredTypes() const; |
104 | 98 |
105 // Must be called from the UI thread. (See destructor comment.) | 99 // Must be called from the UI thread. (See destructor comment.) |
106 void RequestWorkerStopOnUIThread(); | 100 void RequestWorkerStopOnUIThread(); |
107 | 101 |
108 // Activates the given data type (which should belong to the given | 102 // Activates the given data type (which should belong to the given |
109 // group) and starts the given change processor. Must be called | 103 // group) and starts the given change processor. Must be called |
110 // from |group|'s native thread. | 104 // from |group|'s native thread. |
111 void ActivateDataType(syncer::ModelType type, | 105 void ActivateDataType(ModelType type, |
112 syncer::ModelSafeGroup group, | 106 ModelSafeGroup group, |
113 sync_driver::ChangeProcessor* change_processor, | 107 ChangeProcessor* change_processor, |
114 syncer::UserShare* user_share); | 108 UserShare* user_share); |
115 | 109 |
116 // Deactivates the given type if necessary. Must be called from the | 110 // Deactivates the given type if necessary. Must be called from the |
117 // UI thread and not |type|'s native thread. Yes, this is | 111 // UI thread and not |type|'s native thread. Yes, this is |
118 // surprising: see http://crbug.com/92804. | 112 // surprising: see http://crbug.com/92804. |
119 void DeactivateDataType(syncer::ModelType type); | 113 void DeactivateDataType(ModelType type); |
120 | 114 |
121 // Returns true only between calls to ActivateDataType(type, ...) | 115 // Returns true only between calls to ActivateDataType(type, ...) |
122 // and DeactivateDataType(type). Used only by tests. | 116 // and DeactivateDataType(type). Used only by tests. |
123 bool IsTypeActivatedForTest(syncer::ModelType type) const; | 117 bool IsTypeActivatedForTest(ModelType type) const; |
124 | 118 |
125 // SyncManager::ChangeDelegate implementation. May be called from | 119 // SyncManager::ChangeDelegate implementation. May be called from |
126 // any thread. | 120 // any thread. |
127 void OnChangesApplied( | 121 void OnChangesApplied(ModelType model_type, |
128 syncer::ModelType model_type, | 122 int64_t model_version, |
129 int64_t model_version, | 123 const BaseTransaction* trans, |
130 const syncer::BaseTransaction* trans, | 124 const ImmutableChangeRecordList& changes) override; |
131 const syncer::ImmutableChangeRecordList& changes) override; | 125 void OnChangesComplete(ModelType model_type) override; |
132 void OnChangesComplete(syncer::ModelType model_type) override; | |
133 | 126 |
134 void GetWorkers(std::vector<scoped_refptr<syncer::ModelSafeWorker>>* out); | 127 void GetWorkers(std::vector<scoped_refptr<ModelSafeWorker>>* out); |
135 void GetModelSafeRoutingInfo(syncer::ModelSafeRoutingInfo* out); | 128 void GetModelSafeRoutingInfo(ModelSafeRoutingInfo* out); |
136 | 129 |
137 // syncer::WorkerLoopDestructionObserver implementation. | 130 // WorkerLoopDestructionObserver implementation. |
138 void OnWorkerLoopDestroyed(syncer::ModelSafeGroup group) override; | 131 void OnWorkerLoopDestroyed(ModelSafeGroup group) override; |
139 | 132 |
140 // Release ownership of |sync_thread_|. Called when sync is disabled. | 133 // Release ownership of |sync_thread_|. Called when sync is disabled. |
141 std::unique_ptr<base::Thread> ReleaseSyncThread(); | 134 std::unique_ptr<base::Thread> ReleaseSyncThread(); |
142 | 135 |
143 // Unregister workers from loop destruction observation. | 136 // Unregister workers from loop destruction observation. |
144 void Shutdown(); | 137 void Shutdown(); |
145 | 138 |
146 base::Thread* sync_thread(); | 139 base::Thread* sync_thread(); |
147 | 140 |
148 private: | 141 private: |
149 typedef std::map<syncer::ModelSafeGroup, | 142 typedef std::map<ModelSafeGroup, scoped_refptr<ModelSafeWorker>> WorkerMap; |
150 scoped_refptr<syncer::ModelSafeWorker>> | 143 typedef std::map<ModelType, ChangeProcessor*> ProcessorMap; |
151 WorkerMap; | |
152 typedef std::map<syncer::ModelType, sync_driver::ChangeProcessor*> | |
153 ProcessorMap; | |
154 | 144 |
155 // Add a worker for |group| to the worker map if one can be created. | 145 // Add a worker for |group| to the worker map if one can be created. |
156 void MaybeAddWorker(syncer::ModelSafeGroup group); | 146 void MaybeAddWorker(ModelSafeGroup group); |
157 | 147 |
158 // Callback after workers unregister from observing destruction of their | 148 // Callback after workers unregister from observing destruction of their |
159 // working loops. | 149 // working loops. |
160 void OnWorkerUnregistrationDone(syncer::ModelSafeGroup group); | 150 void OnWorkerUnregistrationDone(ModelSafeGroup group); |
161 | 151 |
162 void RemoveWorker(syncer::ModelSafeGroup group); | 152 void RemoveWorker(ModelSafeGroup group); |
163 | 153 |
164 // Returns the change processor for the given model, or NULL if none | 154 // Returns the change processor for the given model, or NULL if none |
165 // exists. Must be called from |group|'s native thread. | 155 // exists. Must be called from |group|'s native thread. |
166 sync_driver::ChangeProcessor* GetProcessor(syncer::ModelType type) const; | 156 ChangeProcessor* GetProcessor(ModelType type) const; |
167 | 157 |
168 // Must be called with |lock_| held. Simply returns the change | 158 // Must be called with |lock_| held. Simply returns the change |
169 // processor for the given type, if it exists. May be called from | 159 // processor for the given type, if it exists. May be called from |
170 // any thread. | 160 // any thread. |
171 sync_driver::ChangeProcessor* GetProcessorUnsafe( | 161 ChangeProcessor* GetProcessorUnsafe(ModelType type) const; |
172 syncer::ModelType type) const; | |
173 | 162 |
174 // Return true if |model_type| lives on the current thread. Must be | 163 // Return true if |model_type| lives on the current thread. Must be |
175 // called with |lock_| held. May be called on any thread. | 164 // called with |lock_| held. May be called on any thread. |
176 bool IsCurrentThreadSafeForModel(syncer::ModelType model_type) const; | 165 bool IsCurrentThreadSafeForModel(ModelType model_type) const; |
177 | 166 |
178 // Returns true if the current thread is the native thread for the | 167 // Returns true if the current thread is the native thread for the |
179 // given group (or if it is undeterminable). | 168 // given group (or if it is undeterminable). |
180 bool IsOnThreadForGroup(syncer::ModelType type, | 169 bool IsOnThreadForGroup(ModelType type, ModelSafeGroup group) const; |
181 syncer::ModelSafeGroup group) const; | |
182 | 170 |
183 // Returns model safe group that should be assigned to type when it is first | 171 // Returns model safe group that should be assigned to type when it is first |
184 // configured (before activation). Returns GROUP_PASSIVE for directory types | 172 // configured (before activation). Returns GROUP_PASSIVE for directory types |
185 // and GROUP_NON_BLOCKING for non-blocking types. | 173 // and GROUP_NON_BLOCKING for non-blocking types. |
186 syncer::ModelSafeGroup GetInitialGroupForType(syncer::ModelType type) const; | 174 ModelSafeGroup GetInitialGroupForType(ModelType type) const; |
187 | 175 |
188 // Name used for debugging. | 176 // Name used for debugging. |
189 const std::string name_; | 177 const std::string name_; |
190 | 178 |
191 // A pointer to the sync client. | 179 // A pointer to the sync client. |
192 sync_driver::SyncClient* const sync_client_; | 180 SyncClient* const sync_client_; |
193 | 181 |
194 // Protects all variables below. | 182 // Protects all variables below. |
195 mutable base::Lock lock_; | 183 mutable base::Lock lock_; |
196 | 184 |
197 // We maintain ownership of all workers. In some cases, we need to | 185 // We maintain ownership of all workers. In some cases, we need to |
198 // ensure shutdown occurs in an expected sequence by Stop()ing | 186 // ensure shutdown occurs in an expected sequence by Stop()ing |
199 // certain workers. They are guaranteed to be valid because we only | 187 // certain workers. They are guaranteed to be valid because we only |
200 // destroy elements of |workers_| after the syncapi has been | 188 // destroy elements of |workers_| after the syncapi has been |
201 // destroyed. Unless a worker is no longer needed because all types | 189 // destroyed. Unless a worker is no longer needed because all types |
202 // that get routed to it have been disabled (from syncing). In that | 190 // that get routed to it have been disabled (from syncing). In that |
203 // case, we'll destroy on demand *after* routing any dependent types | 191 // case, we'll destroy on demand *after* routing any dependent types |
204 // to syncer::GROUP_PASSIVE, so that the syncapi doesn't call into garbage. | 192 // to GROUP_PASSIVE, so that the syncapi doesn't call into garbage. |
205 // If a key is present, it means at least one ModelType that routes | 193 // If a key is present, it means at least one ModelType that routes |
206 // to that model safe group is being synced. | 194 // to that model safe group is being synced. |
207 WorkerMap workers_; | 195 WorkerMap workers_; |
208 syncer::ModelSafeRoutingInfo routing_info_; | 196 ModelSafeRoutingInfo routing_info_; |
209 | 197 |
210 // The change processors that handle the different data types. | 198 // The change processors that handle the different data types. |
211 ProcessorMap processors_; | 199 ProcessorMap processors_; |
212 | 200 |
213 // The types that were enabled as of the last configuration. Updated on each | 201 // The types that were enabled as of the last configuration. Updated on each |
214 // call to ConfigureDataTypes as well as SetInitialTypes. | 202 // call to ConfigureDataTypes as well as SetInitialTypes. |
215 syncer::ModelTypeSet last_configured_types_; | 203 ModelTypeSet last_configured_types_; |
216 | 204 |
217 // Parks stopped workers because they may still be referenced by syncer. | 205 // Parks stopped workers because they may still be referenced by syncer. |
218 std::vector<scoped_refptr<syncer::ModelSafeWorker>> stopped_workers_; | 206 std::vector<scoped_refptr<ModelSafeWorker>> stopped_workers_; |
219 | 207 |
220 // References to the thread task runners that sync depends on. | 208 // References to the thread task runners that sync depends on. |
221 const scoped_refptr<base::SingleThreadTaskRunner> ui_thread_; | 209 const scoped_refptr<base::SingleThreadTaskRunner> ui_thread_; |
222 const scoped_refptr<base::SingleThreadTaskRunner> db_thread_; | 210 const scoped_refptr<base::SingleThreadTaskRunner> db_thread_; |
223 const scoped_refptr<base::SingleThreadTaskRunner> file_thread_; | 211 const scoped_refptr<base::SingleThreadTaskRunner> file_thread_; |
224 | 212 |
225 // Declare |sync_thread_| at the end so that it will be destroyed before | 213 // Declare |sync_thread_| at the end so that it will be destroyed before |
226 // objects above because tasks on sync thread depend on those objects, | 214 // objects above because tasks on sync thread depend on those objects, |
227 // e.g. Shutdown() depends on |lock_|, SyncManager::Init() depends on | 215 // e.g. Shutdown() depends on |lock_|, SyncManager::Init() depends on |
228 // workers, etc. | 216 // workers, etc. |
229 std::unique_ptr<base::Thread> sync_thread_; | 217 std::unique_ptr<base::Thread> sync_thread_; |
230 | 218 |
231 // Set of types with non-blocking implementation (as opposed to directory | 219 // Set of types with non-blocking implementation (as opposed to directory |
232 // based). | 220 // based). |
233 syncer::ModelTypeSet non_blocking_types_; | 221 ModelTypeSet non_blocking_types_; |
234 | 222 |
235 DISALLOW_COPY_AND_ASSIGN(SyncBackendRegistrar); | 223 DISALLOW_COPY_AND_ASSIGN(SyncBackendRegistrar); |
236 }; | 224 }; |
237 | 225 |
238 } // namespace browser_sync | 226 } // namespace syncer |
239 | 227 |
240 #endif // COMPONENTS_SYNC_DRIVER_GLUE_SYNC_BACKEND_REGISTRAR_H_ | 228 #endif // COMPONENTS_SYNC_DRIVER_GLUE_SYNC_BACKEND_REGISTRAR_H_ |
OLD | NEW |