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/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "base/synchronization/waitable_event.h" | 9 #include "base/synchronization/waitable_event.h" |
10 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 | 59 |
60 base::Closure register_callback_; | 60 base::Closure register_callback_; |
61 }; | 61 }; |
62 | 62 |
63 namespace { | 63 namespace { |
64 | 64 |
65 // 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 |
66 // thread. | 66 // thread. |
67 void PostWorkerTask(const base::WeakPtr<HistoryService>& history_service, | 67 void PostWorkerTask(const base::WeakPtr<HistoryService>& history_service, |
68 const syncer::WorkCallback& work, | 68 const syncer::WorkCallback& work, |
69 CancelableRequestConsumerT<int, 0>* cancelable_consumer, | 69 base::CancelableTaskTracker* cancelable_tracker, |
70 WaitableEvent* done, | 70 WaitableEvent* done, |
71 syncer::SyncerError* error) { | 71 syncer::SyncerError* error) { |
72 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 72 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
73 if (history_service.get()) { | 73 if (history_service.get()) { |
74 scoped_refptr<WorkerTask> task(new WorkerTask(work, done, error)); | 74 scoped_refptr<WorkerTask> task(new WorkerTask(work, done, error)); |
75 history_service->ScheduleDBTask(task.get(), cancelable_consumer); | 75 history_service->ScheduleDBTask(task.get(), cancelable_tracker); |
76 } else { | 76 } else { |
77 *error = syncer::CANNOT_DO_WORK; | 77 *error = syncer::CANNOT_DO_WORK; |
78 done->Signal(); | 78 done->Signal(); |
79 } | 79 } |
80 } | 80 } |
81 | 81 |
82 } // namespace | 82 } // namespace |
83 | 83 |
84 HistoryModelWorker::HistoryModelWorker( | 84 HistoryModelWorker::HistoryModelWorker( |
85 const base::WeakPtr<HistoryService>& history_service, | 85 const base::WeakPtr<HistoryService>& history_service, |
86 syncer::WorkerLoopDestructionObserver* observer) | 86 syncer::WorkerLoopDestructionObserver* observer) |
87 : syncer::ModelSafeWorker(observer), | 87 : syncer::ModelSafeWorker(observer), |
88 history_service_(history_service) { | 88 history_service_(history_service) { |
89 CHECK(history_service.get()); | 89 CHECK(history_service.get()); |
| 90 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 91 cancelable_tracker_.reset(new base::CancelableTaskTracker); |
90 } | 92 } |
91 | 93 |
92 void HistoryModelWorker::RegisterForLoopDestruction() { | 94 void HistoryModelWorker::RegisterForLoopDestruction() { |
93 CHECK(history_service_.get()); | 95 CHECK(history_service_.get()); |
94 history_service_->ScheduleDBTask( | 96 history_service_->ScheduleDBTask( |
95 new AddDBThreadObserverTask( | 97 new AddDBThreadObserverTask( |
96 base::Bind(&HistoryModelWorker::RegisterOnDBThread, this)), | 98 base::Bind(&HistoryModelWorker::RegisterOnDBThread, this)), |
97 &cancelable_consumer_); | 99 cancelable_tracker_.get()); |
98 } | 100 } |
99 | 101 |
100 void HistoryModelWorker::RegisterOnDBThread() { | 102 void HistoryModelWorker::RegisterOnDBThread() { |
101 base::MessageLoop::current()->AddDestructionObserver(this); | 103 base::MessageLoop::current()->AddDestructionObserver(this); |
102 SetWorkingLoopToCurrent(); | 104 SetWorkingLoopToCurrent(); |
103 } | 105 } |
104 | 106 |
105 syncer::SyncerError HistoryModelWorker::DoWorkAndWaitUntilDoneImpl( | 107 syncer::SyncerError HistoryModelWorker::DoWorkAndWaitUntilDoneImpl( |
106 const syncer::WorkCallback& work) { | 108 const syncer::WorkCallback& work) { |
107 syncer::SyncerError error = syncer::UNSET; | 109 syncer::SyncerError error = syncer::UNSET; |
108 if (BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 110 if (BrowserThread::PostTask(BrowserThread::UI, |
109 base::Bind(&PostWorkerTask, history_service_, | 111 FROM_HERE, |
110 work, &cancelable_consumer_, | 112 base::Bind(&PostWorkerTask, |
| 113 history_service_, |
| 114 work, |
| 115 cancelable_tracker_.get(), |
111 work_done_or_stopped(), | 116 work_done_or_stopped(), |
112 &error))) { | 117 &error))) { |
113 work_done_or_stopped()->Wait(); | 118 work_done_or_stopped()->Wait(); |
114 } else { | 119 } else { |
115 error = syncer::CANNOT_DO_WORK; | 120 error = syncer::CANNOT_DO_WORK; |
116 } | 121 } |
117 return error; | 122 return error; |
118 } | 123 } |
119 | 124 |
120 syncer::ModelSafeGroup HistoryModelWorker::GetModelSafeGroup() { | 125 syncer::ModelSafeGroup HistoryModelWorker::GetModelSafeGroup() { |
121 return syncer::GROUP_HISTORY; | 126 return syncer::GROUP_HISTORY; |
122 } | 127 } |
123 | 128 |
124 HistoryModelWorker::~HistoryModelWorker() {} | 129 HistoryModelWorker::~HistoryModelWorker() { |
| 130 // The base::CancelableTaskTracker class is not thread-safe and must only be |
| 131 // used from a single thread but the current object may not be destroyed from |
| 132 // the UI thread, so delete it from the UI thread. |
| 133 BrowserThread::DeleteOnUIThread::Destruct(cancelable_tracker_.release()); |
| 134 } |
125 | 135 |
126 } // namespace browser_sync | 136 } // namespace browser_sync |
OLD | NEW |