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

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

Issue 2471183003: Do not observe MessageLoop destruction from ModelSafeWorker. (Closed)
Patch Set: CR maxbogue #23 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 "base/message_loop/message_loop.h"
14 #include "components/sync/driver/change_processor.h" 13 #include "components/sync/driver/change_processor.h"
15 #include "components/sync/driver/sync_client.h" 14 #include "components/sync/driver/sync_client.h"
16 #include "components/sync/syncable/user_share.h" 15 #include "components/sync/syncable/user_share.h"
17 16
18 namespace syncer { 17 namespace syncer {
19 18
20 SyncBackendRegistrar::SyncBackendRegistrar( 19 SyncBackendRegistrar::SyncBackendRegistrar(
21 const std::string& name, 20 const std::string& name,
22 SyncClient* sync_client, 21 SyncClient* sync_client,
23 std::unique_ptr<base::Thread> sync_thread, 22 std::unique_ptr<base::Thread> sync_thread,
(...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after
305 return type == PASSWORDS; 304 return type == PASSWORDS;
306 case GROUP_NON_BLOCKING: 305 case GROUP_NON_BLOCKING:
307 // IsOnThreadForGroup shouldn't be called for non-blocking types. 306 // IsOnThreadForGroup shouldn't be called for non-blocking types.
308 return false; 307 return false;
309 } 308 }
310 NOTREACHED(); 309 NOTREACHED();
311 return false; 310 return false;
312 } 311 }
313 312
314 SyncBackendRegistrar::~SyncBackendRegistrar() { 313 SyncBackendRegistrar::~SyncBackendRegistrar() {
315 DCHECK(workers_.empty()); 314 // All data types should have been deactivated by now.
316 } 315 DCHECK(processors_.empty());
317
318 void SyncBackendRegistrar::OnWorkerLoopDestroyed(ModelSafeGroup group) {
319 RemoveWorker(group);
320 } 316 }
321 317
322 void SyncBackendRegistrar::MaybeAddWorker(ModelSafeGroup group) { 318 void SyncBackendRegistrar::MaybeAddWorker(ModelSafeGroup group) {
323 const scoped_refptr<ModelSafeWorker> worker = 319 const scoped_refptr<ModelSafeWorker> worker =
324 sync_client_->CreateModelWorkerForGroup(group, this); 320 sync_client_->CreateModelWorkerForGroup(group);
325 if (worker) { 321 if (worker) {
326 DCHECK(workers_.find(group) == workers_.end()); 322 DCHECK(workers_.find(group) == workers_.end());
327 workers_[group] = worker; 323 workers_[group] = worker;
328 workers_[group]->RegisterForLoopDestruction();
329 } 324 }
330 } 325 }
331 326
332 void SyncBackendRegistrar::OnWorkerUnregistrationDone(ModelSafeGroup group) {
333 RemoveWorker(group);
334 }
335
336 void SyncBackendRegistrar::RemoveWorker(ModelSafeGroup group) {
337 DVLOG(1) << "Remove " << ModelSafeGroupToString(group) << " worker.";
338
339 bool last_worker = false;
340 {
341 base::AutoLock al(lock_);
342 WorkerMap::iterator it = workers_.find(group);
343 CHECK(it != workers_.end());
344 stopped_workers_.push_back(it->second);
345 workers_.erase(it);
346 last_worker = workers_.empty();
347 }
348
349 if (last_worker) {
350 // Self-destruction after last worker.
351 DVLOG(1) << "Destroy registrar on loop of "
352 << ModelSafeGroupToString(group);
353 delete this;
354 }
355 }
356
357 std::unique_ptr<base::Thread> SyncBackendRegistrar::ReleaseSyncThread() { 327 std::unique_ptr<base::Thread> SyncBackendRegistrar::ReleaseSyncThread() {
358 return std::move(sync_thread_); 328 return std::move(sync_thread_);
359 } 329 }
360 330
361 void SyncBackendRegistrar::Shutdown() {
362 // All data types should have been deactivated by now.
363 DCHECK(processors_.empty());
364
365 // Unregister worker from observing loop destruction.
366 base::AutoLock al(lock_);
367 for (WorkerMap::iterator it = workers_.begin(); it != workers_.end(); ++it) {
368 it->second->UnregisterForLoopDestruction(
369 base::Bind(&SyncBackendRegistrar::OnWorkerUnregistrationDone,
370 base::Unretained(this)));
371 }
372 }
373
374 base::Thread* SyncBackendRegistrar::sync_thread() { 331 base::Thread* SyncBackendRegistrar::sync_thread() {
375 return sync_thread_.get(); 332 return sync_thread_.get();
376 } 333 }
377 334
378 ModelSafeGroup SyncBackendRegistrar::GetInitialGroupForType( 335 ModelSafeGroup SyncBackendRegistrar::GetInitialGroupForType(
379 ModelType type) const { 336 ModelType type) const {
380 return non_blocking_types_.Has(type) ? GROUP_NON_BLOCKING : GROUP_PASSIVE; 337 return non_blocking_types_.Has(type) ? GROUP_NON_BLOCKING : GROUP_PASSIVE;
381 } 338 }
382 339
383 } // namespace syncer 340 } // namespace syncer
OLDNEW
« no previous file with comments | « components/sync/driver/glue/sync_backend_registrar.h ('k') | components/sync/driver/glue/sync_backend_registrar_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698