OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "components/sync_driver/glue/browser_thread_model_worker.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/single_thread_task_runner.h" | |
9 #include "base/synchronization/waitable_event.h" | |
10 | |
11 using base::SingleThreadTaskRunner; | |
12 using base::WaitableEvent; | |
13 | |
14 namespace browser_sync { | |
15 | |
16 BrowserThreadModelWorker::BrowserThreadModelWorker( | |
17 const scoped_refptr<SingleThreadTaskRunner>& runner, | |
18 syncer::ModelSafeGroup group, | |
19 syncer::WorkerLoopDestructionObserver* observer) | |
20 : ModelSafeWorker(observer), runner_(runner), group_(group) {} | |
21 | |
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(); | |
28 } | |
29 | |
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; | |
37 } | |
38 work_done_or_stopped()->Wait(); | |
39 return error; | |
40 } | |
41 | |
42 syncer::ModelSafeGroup BrowserThreadModelWorker::GetModelSafeGroup() { | |
43 return group_; | |
44 } | |
45 | |
46 BrowserThreadModelWorker::~BrowserThreadModelWorker() {} | |
47 | |
48 void BrowserThreadModelWorker::RegisterForLoopDestruction() { | |
49 if (runner_->BelongsToCurrentThread()) { | |
50 SetWorkingLoopToCurrent(); | |
51 } else { | |
52 runner_->PostTask( | |
53 FROM_HERE, | |
54 Bind(&BrowserThreadModelWorker::RegisterForLoopDestruction, this)); | |
55 } | |
56 } | |
57 | |
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(); | |
66 } | |
67 | |
68 } // namespace browser_sync | |
OLD | NEW |