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/history_model_worker.h" | 5 #include "chrome/browser/sync/glue/history_model_worker.h" |
6 | 6 |
7 #include "base/memory/ref_counted.h" | 7 #include "base/memory/ref_counted.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "base/synchronization/waitable_event.h" | 9 #include "base/synchronization/waitable_event.h" |
10 #include "chrome/browser/history/history_db_task.h" | |
11 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
12 | 11 |
13 using base::WaitableEvent; | 12 using base::WaitableEvent; |
14 using content::BrowserThread; | 13 using content::BrowserThread; |
15 | 14 |
16 namespace browser_sync { | 15 namespace browser_sync { |
17 | 16 |
18 class WorkerTask : public history::HistoryDBTask { | 17 class WorkerTask : public history::HistoryDBTask { |
19 public: | 18 public: |
20 WorkerTask( | 19 WorkerTask( |
21 const syncer::WorkCallback& work, | 20 const syncer::WorkCallback& work, |
22 WaitableEvent* done, | 21 WaitableEvent* done, |
23 syncer::SyncerError* error) | 22 syncer::SyncerError* error) |
24 : work_(work), done_(done), error_(error) {} | 23 : work_(work), done_(done), error_(error) {} |
25 | 24 |
26 virtual bool RunOnDBThread(history::HistoryBackend* backend, | 25 virtual bool RunOnDBThread(history::HistoryBackend* backend, |
27 history::HistoryDatabase* db) OVERRIDE { | 26 history::HistoryDatabase* db) OVERRIDE { |
28 *error_ = work_.Run(); | 27 *error_ = work_.Run(); |
29 done_->Signal(); | 28 done_->Signal(); |
30 return true; | 29 return true; |
31 } | 30 } |
32 | 31 |
33 // Since the DoWorkAndWaitUntilDone() is syncronous, we don't need to run any | 32 // Since the DoWorkAndWaitUntilDone() is synchronous, we don't need to run |
34 // code asynchronously on the main thread after completion. | 33 // any code asynchronously on the main thread after completion. |
35 virtual void DoneRunOnMainThread() OVERRIDE {} | 34 virtual void DoneRunOnMainThread() OVERRIDE {} |
36 | 35 |
37 protected: | 36 protected: |
38 virtual ~WorkerTask() {} | 37 virtual ~WorkerTask() {} |
39 | 38 |
40 syncer::WorkCallback work_; | 39 syncer::WorkCallback work_; |
41 WaitableEvent* done_; | 40 WaitableEvent* done_; |
42 syncer::SyncerError* error_; | 41 syncer::SyncerError* error_; |
43 }; | 42 }; |
44 | 43 |
| 44 class AddDBThreadObserverTask : public history::HistoryDBTask { |
| 45 public: |
| 46 AddDBThreadObserverTask(HistoryModelWorker* history_worker) |
| 47 : history_worker_(history_worker) {} |
| 48 |
| 49 virtual bool RunOnDBThread(history::HistoryBackend* backend, |
| 50 history::HistoryDatabase* db) OVERRIDE { |
| 51 MessageLoop::current()->AddDestructionObserver(history_worker_.get()); |
| 52 return true; |
| 53 } |
| 54 |
| 55 virtual void DoneRunOnMainThread() OVERRIDE {} |
| 56 |
| 57 private: |
| 58 virtual ~AddDBThreadObserverTask() {} |
| 59 |
| 60 scoped_refptr<HistoryModelWorker> history_worker_; |
| 61 }; |
| 62 |
45 namespace { | 63 namespace { |
46 | 64 |
47 // Post the work task on |history_service|'s DB thread from the UI | 65 // Post the work task on |history_service|'s DB thread from the UI |
48 // thread. | 66 // thread. |
49 void PostWorkerTask(const base::WeakPtr<HistoryService>& history_service, | 67 void PostWorkerTask(const base::WeakPtr<HistoryService>& history_service, |
50 const syncer::WorkCallback& work, | 68 const syncer::WorkCallback& work, |
51 CancelableRequestConsumerT<int, 0>* cancelable_consumer, | 69 CancelableRequestConsumerT<int, 0>* cancelable_consumer, |
52 WaitableEvent* done, | 70 WaitableEvent* done, |
53 syncer::SyncerError* error) { | 71 syncer::SyncerError* error) { |
54 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 72 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
55 if (history_service) { | 73 if (history_service) { |
56 scoped_refptr<WorkerTask> task(new WorkerTask(work, done, error)); | 74 scoped_refptr<WorkerTask> task(new WorkerTask(work, done, error)); |
57 history_service->ScheduleDBTask(task.get(), cancelable_consumer); | 75 history_service->ScheduleDBTask(task.get(), cancelable_consumer); |
58 } else { | 76 } else { |
59 *error = syncer::CANNOT_DO_WORK; | 77 *error = syncer::CANNOT_DO_WORK; |
60 done->Signal(); | 78 done->Signal(); |
61 } | 79 } |
62 } | 80 } |
63 | 81 |
64 } // namespace | 82 } // namespace |
65 | 83 |
66 HistoryModelWorker::HistoryModelWorker( | 84 HistoryModelWorker::HistoryModelWorker( |
67 const base::WeakPtr<HistoryService>& history_service) | 85 const base::WeakPtr<HistoryService>& history_service, |
68 : history_service_(history_service) { | 86 syncer::WorkerLoopDestructionObserver* observer) |
| 87 : syncer::ModelSafeWorker(observer), |
| 88 history_service_(history_service) { |
69 CHECK(history_service.get()); | 89 CHECK(history_service.get()); |
70 } | 90 } |
71 | 91 |
72 syncer::SyncerError HistoryModelWorker::DoWorkAndWaitUntilDone( | 92 void HistoryModelWorker::RegisterForLoopDestruction() { |
| 93 CHECK(history_service_.get()); |
| 94 history_service_->ScheduleDBTask(new AddDBThreadObserverTask(this), |
| 95 &cancelable_consumer_); |
| 96 } |
| 97 |
| 98 syncer::SyncerError HistoryModelWorker::DoWorkAndWaitUntilDoneImpl( |
73 const syncer::WorkCallback& work) { | 99 const syncer::WorkCallback& work) { |
74 syncer::SyncerError error = syncer::UNSET; | 100 syncer::SyncerError error = syncer::UNSET; |
75 WaitableEvent done(false, false); | |
76 if (BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 101 if (BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
77 base::Bind(&PostWorkerTask, | 102 base::Bind(&PostWorkerTask, history_service_, |
78 history_service_, work, | 103 work, &cancelable_consumer_, |
79 &cancelable_consumer_, | 104 work_done_or_stopped(), |
80 &done, &error))) { | 105 &error))) { |
81 done.Wait(); | 106 work_done_or_stopped()->Wait(); |
82 } else { | 107 } else { |
83 error = syncer::CANNOT_DO_WORK; | 108 error = syncer::CANNOT_DO_WORK; |
84 } | 109 } |
85 return error; | 110 return error; |
86 } | 111 } |
87 | 112 |
88 syncer::ModelSafeGroup HistoryModelWorker::GetModelSafeGroup() { | 113 syncer::ModelSafeGroup HistoryModelWorker::GetModelSafeGroup() { |
89 return syncer::GROUP_HISTORY; | 114 return syncer::GROUP_HISTORY; |
90 } | 115 } |
91 | 116 |
92 HistoryModelWorker::~HistoryModelWorker() {} | 117 HistoryModelWorker::~HistoryModelWorker() {} |
93 | 118 |
94 } // namespace browser_sync | 119 } // namespace browser_sync |
OLD | NEW |