Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / sync_driver / glue / browser_thread_model_worker.cc
blobfaf53fc4a3ec8f32371569e697d1f33169f1311e
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
3 // found in the LICENSE file.
5 #include "components/sync_driver/glue/browser_thread_model_worker.h"
7 #include "base/bind.h"
8 #include "base/single_thread_task_runner.h"
9 #include "base/synchronization/waitable_event.h"
11 using base::SingleThreadTaskRunner;
12 using base::WaitableEvent;
14 namespace browser_sync {
16 BrowserThreadModelWorker::BrowserThreadModelWorker(
17 const scoped_refptr<SingleThreadTaskRunner>& runner,
18 syncer::ModelSafeGroup group,
19 syncer::WorkerLoopDestructionObserver* observer)
20 : ModelSafeWorker(observer), runner_(runner), group_(group) {}
22 syncer::SyncerError BrowserThreadModelWorker::DoWorkAndWaitUntilDoneImpl(
23 const syncer::WorkCallback& work) {
24 syncer::SyncerError error = syncer::UNSET;
25 if (runner_->BelongsToCurrentThread()) {
26 DLOG(WARNING) << "Already on thread " << runner_;
27 return work.Run();
30 if (!runner_->PostTask(
31 FROM_HERE,
32 base::Bind(&BrowserThreadModelWorker::CallDoWorkAndSignalTask, this,
33 work, work_done_or_stopped(), &error))) {
34 DLOG(WARNING) << "Failed to post task to runner " << runner_;
35 error = syncer::CANNOT_DO_WORK;
36 return error;
38 work_done_or_stopped()->Wait();
39 return error;
42 syncer::ModelSafeGroup BrowserThreadModelWorker::GetModelSafeGroup() {
43 return group_;
46 BrowserThreadModelWorker::~BrowserThreadModelWorker() {}
48 void BrowserThreadModelWorker::RegisterForLoopDestruction() {
49 if (runner_->BelongsToCurrentThread()) {
50 SetWorkingLoopToCurrent();
51 } else {
52 runner_->PostTask(
53 FROM_HERE,
54 Bind(&BrowserThreadModelWorker::RegisterForLoopDestruction, this));
58 void BrowserThreadModelWorker::CallDoWorkAndSignalTask(
59 const syncer::WorkCallback& work,
60 WaitableEvent* done,
61 syncer::SyncerError* error) {
62 DCHECK(runner_->BelongsToCurrentThread());
63 if (!IsStopped())
64 *error = work.Run();
65 done->Signal();
68 } // namespace browser_sync