Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5)

Side by Side Diff: components/sync/driver/glue/sync_backend_registrar.cc

Issue 2489433002: [Sync] Move thread checking into the ModelSafeWorker interface. (Closed)
Patch Set: Improve/add comments. Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 #include "components/sync/driver/glue/sync_backend_registrar.h" 5 #include "components/sync/driver/glue/sync_backend_registrar.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <cstddef> 8 #include <cstddef>
9 #include <utility> 9 #include <utility>
10 10
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/memory/ptr_util.h" 12 #include "base/memory/ptr_util.h"
13 #include "components/sync/driver/change_processor.h" 13 #include "components/sync/driver/change_processor.h"
14 #include "components/sync/driver/sync_client.h" 14 #include "components/sync/driver/sync_client.h"
15 #include "components/sync/syncable/user_share.h" 15 #include "components/sync/syncable/user_share.h"
16 16
17 namespace syncer { 17 namespace syncer {
18 18
19 SyncBackendRegistrar::SyncBackendRegistrar( 19 SyncBackendRegistrar::SyncBackendRegistrar(const std::string& name,
20 const std::string& name, 20 SyncClient* sync_client)
21 SyncClient* sync_client, 21 : name_(name), sync_client_(sync_client) {
22 const scoped_refptr<base::SingleThreadTaskRunner>& ui_thread,
23 const scoped_refptr<base::SingleThreadTaskRunner>& db_thread,
24 const scoped_refptr<base::SingleThreadTaskRunner>& file_thread)
25 : name_(name),
26 sync_client_(sync_client),
27 ui_thread_(ui_thread),
28 db_thread_(db_thread),
29 file_thread_(file_thread) {
30 DCHECK(ui_thread_->BelongsToCurrentThread());
31 DCHECK(sync_client_); 22 DCHECK(sync_client_);
32 23
33 MaybeAddWorker(GROUP_DB); 24 MaybeAddWorker(GROUP_DB);
34 MaybeAddWorker(GROUP_FILE); 25 MaybeAddWorker(GROUP_FILE);
35 MaybeAddWorker(GROUP_UI); 26 MaybeAddWorker(GROUP_UI);
36 MaybeAddWorker(GROUP_PASSIVE); 27 MaybeAddWorker(GROUP_PASSIVE);
37 MaybeAddWorker(GROUP_HISTORY); 28 MaybeAddWorker(GROUP_HISTORY);
38 MaybeAddWorker(GROUP_PASSWORD); 29 MaybeAddWorker(GROUP_PASSWORD);
39
40 // Must have at least one worker for SyncBackendRegistrar to be destroyed
41 // correctly, as it is destroyed after the last worker dies.
42 DCHECK_GT(workers_.size(), 0u);
43 } 30 }
44 31
45 void SyncBackendRegistrar::RegisterNonBlockingType(ModelType type) { 32 void SyncBackendRegistrar::RegisterNonBlockingType(ModelType type) {
46 DCHECK(ui_thread_->BelongsToCurrentThread()); 33 DCHECK(ui_thread_checker_.CalledOnValidThread());
47 base::AutoLock lock(lock_); 34 base::AutoLock lock(lock_);
48 // There may have been a previously successful sync of a type when passive, 35 // There may have been a previously successful sync of a type when passive,
49 // which is now NonBlocking. We're not sure what order these two sets of types 36 // which is now NonBlocking. We're not sure what order these two sets of types
50 // are being registered in, so guard against SetInitialTypes(...) having been 37 // are being registered in, so guard against SetInitialTypes(...) having been
51 // already called by undoing everything to these types. 38 // already called by undoing everything to these types.
52 if (routing_info_.find(type) != routing_info_.end() && 39 if (routing_info_.find(type) != routing_info_.end() &&
53 routing_info_[type] != GROUP_NON_BLOCKING) { 40 routing_info_[type] != GROUP_NON_BLOCKING) {
54 routing_info_.erase(type); 41 routing_info_.erase(type);
55 last_configured_types_.Remove(type); 42 last_configured_types_.Remove(type);
56 } 43 }
(...skipping 29 matching lines...) Expand all
86 LOG_IF(WARNING, initial_types.Has(PASSWORDS)) 73 LOG_IF(WARNING, initial_types.Has(PASSWORDS))
87 << "Password store not initialized, cannot sync passwords"; 74 << "Password store not initialized, cannot sync passwords";
88 routing_info_.erase(PASSWORDS); 75 routing_info_.erase(PASSWORDS);
89 } 76 }
90 77
91 // Although this can re-set NonBlocking types, this should be idempotent. 78 // Although this can re-set NonBlocking types, this should be idempotent.
92 last_configured_types_ = GetRoutingInfoTypes(routing_info_); 79 last_configured_types_ = GetRoutingInfoTypes(routing_info_);
93 } 80 }
94 81
95 void SyncBackendRegistrar::AddRestoredNonBlockingType(ModelType type) { 82 void SyncBackendRegistrar::AddRestoredNonBlockingType(ModelType type) {
96 DCHECK(ui_thread_->BelongsToCurrentThread()); 83 DCHECK(ui_thread_checker_.CalledOnValidThread());
97 base::AutoLock lock(lock_); 84 base::AutoLock lock(lock_);
98 DCHECK(non_blocking_types_.Has(type)); 85 DCHECK(non_blocking_types_.Has(type));
99 DCHECK(routing_info_.find(type) == routing_info_.end()); 86 DCHECK(routing_info_.find(type) == routing_info_.end());
100 routing_info_[type] = GROUP_NON_BLOCKING; 87 routing_info_[type] = GROUP_NON_BLOCKING;
101 last_configured_types_.Put(type); 88 last_configured_types_.Put(type);
102 } 89 }
103 90
104 bool SyncBackendRegistrar::IsNigoriEnabled() const { 91 bool SyncBackendRegistrar::IsNigoriEnabled() const {
105 DCHECK(ui_thread_->BelongsToCurrentThread()); 92 DCHECK(ui_thread_checker_.CalledOnValidThread());
106 base::AutoLock lock(lock_); 93 base::AutoLock lock(lock_);
107 return routing_info_.find(NIGORI) != routing_info_.end(); 94 return routing_info_.find(NIGORI) != routing_info_.end();
108 } 95 }
109 96
110 ModelTypeSet SyncBackendRegistrar::ConfigureDataTypes( 97 ModelTypeSet SyncBackendRegistrar::ConfigureDataTypes(
111 ModelTypeSet types_to_add, 98 ModelTypeSet types_to_add,
112 ModelTypeSet types_to_remove) { 99 ModelTypeSet types_to_remove) {
113 DCHECK(Intersection(types_to_add, types_to_remove).Empty()); 100 DCHECK(Intersection(types_to_add, types_to_remove).Empty());
114 ModelTypeSet filtered_types_to_add = types_to_add; 101 ModelTypeSet filtered_types_to_add = types_to_add;
115 if (workers_.count(GROUP_HISTORY) == 0) { 102 if (workers_.count(GROUP_HISTORY) == 0) {
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
147 last_configured_types_ = GetRoutingInfoTypes(routing_info_); 134 last_configured_types_ = GetRoutingInfoTypes(routing_info_);
148 135
149 return newly_added_types; 136 return newly_added_types;
150 } 137 }
151 138
152 ModelTypeSet SyncBackendRegistrar::GetLastConfiguredTypes() const { 139 ModelTypeSet SyncBackendRegistrar::GetLastConfiguredTypes() const {
153 return last_configured_types_; 140 return last_configured_types_;
154 } 141 }
155 142
156 void SyncBackendRegistrar::RequestWorkerStopOnUIThread() { 143 void SyncBackendRegistrar::RequestWorkerStopOnUIThread() {
157 DCHECK(ui_thread_->BelongsToCurrentThread()); 144 DCHECK(ui_thread_checker_.CalledOnValidThread());
158 base::AutoLock lock(lock_); 145 base::AutoLock lock(lock_);
159 for (WorkerMap::const_iterator it = workers_.begin(); it != workers_.end(); 146 for (WorkerMap::const_iterator it = workers_.begin(); it != workers_.end();
160 ++it) { 147 ++it) {
161 it->second->RequestStop(); 148 it->second->RequestStop();
162 } 149 }
163 } 150 }
164 151
165 void SyncBackendRegistrar::ActivateDataType(ModelType type, 152 void SyncBackendRegistrar::ActivateDataType(ModelType type,
166 ModelSafeGroup group, 153 ModelSafeGroup group,
167 ChangeProcessor* change_processor, 154 ChangeProcessor* change_processor,
(...skipping 13 matching lines...) Expand all
181 processors_[type] = change_processor; 168 processors_[type] = change_processor;
182 169
183 // Start the change processor. 170 // Start the change processor.
184 change_processor->Start(user_share); 171 change_processor->Start(user_share);
185 DCHECK(GetProcessorUnsafe(type)); 172 DCHECK(GetProcessorUnsafe(type));
186 } 173 }
187 174
188 void SyncBackendRegistrar::DeactivateDataType(ModelType type) { 175 void SyncBackendRegistrar::DeactivateDataType(ModelType type) {
189 DVLOG(1) << "Deactivate: " << ModelTypeToString(type); 176 DVLOG(1) << "Deactivate: " << ModelTypeToString(type);
190 177
191 DCHECK(ui_thread_->BelongsToCurrentThread() || IsControlType(type)); 178 DCHECK(ui_thread_checker_.CalledOnValidThread() || IsControlType(type));
192 base::AutoLock lock(lock_); 179 base::AutoLock lock(lock_);
193 180
194 routing_info_.erase(type); 181 routing_info_.erase(type);
195 ignore_result(processors_.erase(type)); 182 ignore_result(processors_.erase(type));
196 DCHECK(!GetProcessorUnsafe(type)); 183 DCHECK(!GetProcessorUnsafe(type));
197 } 184 }
198 185
199 bool SyncBackendRegistrar::IsTypeActivatedForTest(ModelType type) const { 186 bool SyncBackendRegistrar::IsTypeActivatedForTest(ModelType type) const {
200 return GetProcessor(type) != nullptr; 187 return GetProcessor(type) != nullptr;
201 } 188 }
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
265 // |processors_| list. 252 // |processors_| list.
266 if (it == processors_.end()) 253 if (it == processors_.end())
267 return nullptr; 254 return nullptr;
268 255
269 return it->second; 256 return it->second;
270 } 257 }
271 258
272 bool SyncBackendRegistrar::IsCurrentThreadSafeForModel( 259 bool SyncBackendRegistrar::IsCurrentThreadSafeForModel(
273 ModelType model_type) const { 260 ModelType model_type) const {
274 lock_.AssertAcquired(); 261 lock_.AssertAcquired();
275 return IsOnThreadForGroup(model_type, 262 ModelSafeGroup group = GetGroupForModelType(model_type, routing_info_);
276 GetGroupForModelType(model_type, routing_info_)); 263 DCHECK_NE(GROUP_NON_BLOCKING, group);
277 }
278 264
279 bool SyncBackendRegistrar::IsOnThreadForGroup(ModelType type, 265 if (group == GROUP_PASSIVE) {
280 ModelSafeGroup group) const { 266 return IsControlType(model_type);
281 switch (group) {
282 case GROUP_PASSIVE:
283 return IsControlType(type);
284 case GROUP_UI:
285 return ui_thread_->BelongsToCurrentThread();
286 case GROUP_DB:
287 return db_thread_->BelongsToCurrentThread();
288 case GROUP_FILE:
289 return file_thread_->BelongsToCurrentThread();
290 case GROUP_HISTORY:
291 // TODO(sync): How to check we're on the right thread?
292 return type == TYPED_URLS;
293 case GROUP_PASSWORD:
294 // TODO(sync): How to check we're on the right thread?
295 return type == PASSWORDS;
296 case GROUP_NON_BLOCKING:
297 // IsOnThreadForGroup shouldn't be called for non-blocking types.
298 return false;
299 } 267 }
300 NOTREACHED(); 268
301 return false; 269 auto it = workers_.find(group);
270 DCHECK(it != workers_.end());
271 return it->second->IsOnModelThread();
302 } 272 }
303 273
304 SyncBackendRegistrar::~SyncBackendRegistrar() { 274 SyncBackendRegistrar::~SyncBackendRegistrar() {
305 // All data types should have been deactivated by now. 275 // All data types should have been deactivated by now.
306 DCHECK(processors_.empty()); 276 DCHECK(processors_.empty());
307 } 277 }
308 278
309 void SyncBackendRegistrar::MaybeAddWorker(ModelSafeGroup group) { 279 void SyncBackendRegistrar::MaybeAddWorker(ModelSafeGroup group) {
310 const scoped_refptr<ModelSafeWorker> worker = 280 const scoped_refptr<ModelSafeWorker> worker =
311 sync_client_->CreateModelWorkerForGroup(group); 281 sync_client_->CreateModelWorkerForGroup(group);
312 if (worker) { 282 if (worker) {
313 DCHECK(workers_.find(group) == workers_.end()); 283 DCHECK(workers_.find(group) == workers_.end());
314 workers_[group] = worker; 284 workers_[group] = worker;
315 } 285 }
316 } 286 }
317 287
318 ModelSafeGroup SyncBackendRegistrar::GetInitialGroupForType( 288 ModelSafeGroup SyncBackendRegistrar::GetInitialGroupForType(
319 ModelType type) const { 289 ModelType type) const {
320 return non_blocking_types_.Has(type) ? GROUP_NON_BLOCKING : GROUP_PASSIVE; 290 return non_blocking_types_.Has(type) ? GROUP_NON_BLOCKING : GROUP_PASSIVE;
321 } 291 }
322 292
323 } // namespace syncer 293 } // namespace syncer
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698