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