OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 "chrome/browser/sync/glue/browser_thread_model_worker.h" | 5 #include "chrome/browser/sync/glue/browser_thread_model_worker.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/synchronization/waitable_event.h" | 8 #include "base/synchronization/waitable_event.h" |
9 #include "content/public/browser/browser_thread.h" | 9 #include "content/public/browser/browser_thread.h" |
10 | 10 |
11 using base::WaitableEvent; | 11 using base::WaitableEvent; |
12 using content::BrowserThread; | 12 using content::BrowserThread; |
13 | 13 |
14 namespace browser_sync { | 14 namespace browser_sync { |
15 | 15 |
16 BrowserThreadModelWorker::BrowserThreadModelWorker( | 16 BrowserThreadModelWorker::BrowserThreadModelWorker( |
17 BrowserThread::ID thread, syncer::ModelSafeGroup group) | 17 BrowserThread::ID thread, syncer::ModelSafeGroup group, |
18 : thread_(thread), group_(group) { | 18 syncer::WorkerLoopDestructionObserver* observer) |
| 19 : ModelSafeWorker(observer), |
| 20 thread_(thread), group_(group) { |
19 } | 21 } |
20 | 22 |
21 syncer::SyncerError BrowserThreadModelWorker::DoWorkAndWaitUntilDone( | 23 syncer::SyncerError BrowserThreadModelWorker::DoWorkAndWaitUntilDoneImpl( |
22 const syncer::WorkCallback& work) { | 24 const syncer::WorkCallback& work) { |
23 syncer::SyncerError error = syncer::UNSET; | 25 syncer::SyncerError error = syncer::UNSET; |
24 if (BrowserThread::CurrentlyOn(thread_)) { | 26 if (BrowserThread::CurrentlyOn(thread_)) { |
25 DLOG(WARNING) << "Already on thread " << thread_; | 27 DLOG(WARNING) << "Already on thread " << thread_; |
26 return work.Run(); | 28 return work.Run(); |
27 } | 29 } |
28 WaitableEvent done(false, false); | 30 |
29 if (!BrowserThread::PostTask( | 31 if (!BrowserThread::PostTask( |
30 thread_, | 32 thread_, |
31 FROM_HERE, | 33 FROM_HERE, |
32 base::Bind(&BrowserThreadModelWorker::CallDoWorkAndSignalTask, this, | 34 base::Bind(&BrowserThreadModelWorker::CallDoWorkAndSignalTask, |
33 work, &done, &error))) { | 35 this, work, |
| 36 work_done_or_stopped(), &error))) { |
34 DLOG(WARNING) << "Failed to post task to thread " << thread_; | 37 DLOG(WARNING) << "Failed to post task to thread " << thread_; |
35 error = syncer::CANNOT_DO_WORK; | 38 error = syncer::CANNOT_DO_WORK; |
36 return error; | 39 return error; |
37 } | 40 } |
38 done.Wait(); | 41 work_done_or_stopped()->Wait(); |
39 return error; | 42 return error; |
40 } | 43 } |
41 | 44 |
42 syncer::ModelSafeGroup BrowserThreadModelWorker::GetModelSafeGroup() { | 45 syncer::ModelSafeGroup BrowserThreadModelWorker::GetModelSafeGroup() { |
43 return group_; | 46 return group_; |
44 } | 47 } |
45 | 48 |
46 BrowserThreadModelWorker::~BrowserThreadModelWorker() {} | 49 BrowserThreadModelWorker::~BrowserThreadModelWorker() {} |
47 | 50 |
| 51 void BrowserThreadModelWorker::RegisterForLoopDestruction() { |
| 52 if (BrowserThread::CurrentlyOn(thread_)) { |
| 53 MessageLoop::current()->AddDestructionObserver(this); |
| 54 } else { |
| 55 BrowserThread::PostTask( |
| 56 thread_, FROM_HERE, |
| 57 Bind(&BrowserThreadModelWorker::RegisterForLoopDestruction, this)); |
| 58 } |
| 59 } |
| 60 |
48 void BrowserThreadModelWorker::CallDoWorkAndSignalTask( | 61 void BrowserThreadModelWorker::CallDoWorkAndSignalTask( |
49 const syncer::WorkCallback& work, | 62 const syncer::WorkCallback& work, |
50 WaitableEvent* done, | 63 WaitableEvent* done, |
51 syncer::SyncerError* error) { | 64 syncer::SyncerError* error) { |
52 DCHECK(BrowserThread::CurrentlyOn(thread_)); | 65 DCHECK(BrowserThread::CurrentlyOn(thread_)); |
53 *error = work.Run(); | 66 if (!IsStopped()) |
| 67 *error = work.Run(); |
54 done->Signal(); | 68 done->Signal(); |
55 } | 69 } |
56 | 70 |
57 DatabaseModelWorker::DatabaseModelWorker() | 71 DatabaseModelWorker::DatabaseModelWorker( |
58 : BrowserThreadModelWorker(BrowserThread::DB, syncer::GROUP_DB) { | 72 syncer::WorkerLoopDestructionObserver* observer) |
| 73 : BrowserThreadModelWorker(BrowserThread::DB, syncer::GROUP_DB, observer) { |
59 } | 74 } |
60 | 75 |
61 void DatabaseModelWorker::CallDoWorkAndSignalTask( | 76 void DatabaseModelWorker::CallDoWorkAndSignalTask( |
62 const syncer::WorkCallback& work, | 77 const syncer::WorkCallback& work, |
63 WaitableEvent* done, | 78 WaitableEvent* done, |
64 syncer::SyncerError* error) { | 79 syncer::SyncerError* error) { |
65 BrowserThreadModelWorker::CallDoWorkAndSignalTask(work, done, error); | 80 BrowserThreadModelWorker::CallDoWorkAndSignalTask(work, done, error); |
66 } | 81 } |
67 | 82 |
68 DatabaseModelWorker::~DatabaseModelWorker() {} | 83 DatabaseModelWorker::~DatabaseModelWorker() {} |
69 | 84 |
70 FileModelWorker::FileModelWorker() | 85 FileModelWorker::FileModelWorker( |
71 : BrowserThreadModelWorker(BrowserThread::FILE, syncer::GROUP_FILE) { | 86 syncer::WorkerLoopDestructionObserver* observer) |
| 87 : BrowserThreadModelWorker(BrowserThread::FILE, syncer::GROUP_FILE, |
| 88 observer) { |
72 } | 89 } |
73 | 90 |
74 void FileModelWorker::CallDoWorkAndSignalTask( | 91 void FileModelWorker::CallDoWorkAndSignalTask( |
75 const syncer::WorkCallback& work, | 92 const syncer::WorkCallback& work, |
76 WaitableEvent* done, | 93 WaitableEvent* done, |
77 syncer::SyncerError* error) { | 94 syncer::SyncerError* error) { |
78 BrowserThreadModelWorker::CallDoWorkAndSignalTask(work, done, error); | 95 BrowserThreadModelWorker::CallDoWorkAndSignalTask(work, done, error); |
79 } | 96 } |
80 | 97 |
81 FileModelWorker::~FileModelWorker() {} | 98 FileModelWorker::~FileModelWorker() {} |
82 | 99 |
83 } // namespace browser_sync | 100 } // namespace browser_sync |
OLD | NEW |