| 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 "components/sync/engine/ui_model_worker.h" | 5 #include "components/sync/engine/ui_model_worker.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | |
| 10 #include "base/bind_helpers.h" | |
| 11 #include "base/callback.h" | |
| 12 #include "components/sync/base/scoped_event_signal.h" | |
| 13 | |
| 14 namespace syncer { | 9 namespace syncer { |
| 15 | 10 |
| 16 namespace { | |
| 17 | |
| 18 class ScopedEventSignalWithWorker { | |
| 19 public: | |
| 20 ScopedEventSignalWithWorker(scoped_refptr<UIModelWorker> ui_model_worker, | |
| 21 base::WaitableEvent* event) | |
| 22 : ui_model_worker_(std::move(ui_model_worker)), | |
| 23 scoped_event_signal_(event) {} | |
| 24 | |
| 25 ScopedEventSignalWithWorker(ScopedEventSignalWithWorker&&) = default; | |
| 26 ScopedEventSignalWithWorker& operator=(ScopedEventSignalWithWorker&&) = | |
| 27 default; | |
| 28 | |
| 29 bool IsStopped() const { return ui_model_worker_->IsStopped(); } | |
| 30 | |
| 31 private: | |
| 32 // This reference prevents the event in |scoped_event_signal_| from being | |
| 33 // signaled after being deleted. | |
| 34 scoped_refptr<UIModelWorker> ui_model_worker_; | |
| 35 | |
| 36 ScopedEventSignal scoped_event_signal_; | |
| 37 | |
| 38 DISALLOW_COPY_AND_ASSIGN(ScopedEventSignalWithWorker); | |
| 39 }; | |
| 40 | |
| 41 void CallDoWorkAndSignalEvent( | |
| 42 const WorkCallback& work, | |
| 43 ScopedEventSignalWithWorker scoped_event_signal_with_worker, | |
| 44 SyncerError* error_info) { | |
| 45 if (!scoped_event_signal_with_worker.IsStopped()) | |
| 46 *error_info = work.Run(); | |
| 47 // The event in |scoped_event_signal_with_worker| is signaled at the end of | |
| 48 // this scope. | |
| 49 } | |
| 50 | |
| 51 } // namespace | |
| 52 | |
| 53 UIModelWorker::UIModelWorker( | 11 UIModelWorker::UIModelWorker( |
| 54 scoped_refptr<base::SingleThreadTaskRunner> ui_thread) | 12 scoped_refptr<base::SingleThreadTaskRunner> ui_thread) |
| 55 : ui_thread_(std::move(ui_thread)), | 13 : ui_thread_(std::move(ui_thread)) {} |
| 56 work_done_or_abandoned_(base::WaitableEvent::ResetPolicy::MANUAL, | |
| 57 base::WaitableEvent::InitialState::NOT_SIGNALED), | |
| 58 stop_requested_(base::WaitableEvent::ResetPolicy::MANUAL, | |
| 59 base::WaitableEvent::InitialState::NOT_SIGNALED) { | |
| 60 sequence_checker_.DetachFromSequence(); | |
| 61 } | |
| 62 | |
| 63 SyncerError UIModelWorker::DoWorkAndWaitUntilDoneImpl( | |
| 64 const WorkCallback& work) { | |
| 65 DCHECK(sequence_checker_.CalledOnValidSequence()); | |
| 66 DCHECK(!ui_thread_->BelongsToCurrentThread()); | |
| 67 | |
| 68 SyncerError error_info; | |
| 69 work_done_or_abandoned_.Reset(); | |
| 70 | |
| 71 if (!ui_thread_->PostTask( | |
| 72 FROM_HERE, | |
| 73 base::Bind(&CallDoWorkAndSignalEvent, work, | |
| 74 base::Passed(syncer::ScopedEventSignalWithWorker( | |
| 75 this, &work_done_or_abandoned_)), | |
| 76 &error_info))) { | |
| 77 DLOG(WARNING) << "Could not post work to UI loop."; | |
| 78 error_info = CANNOT_DO_WORK; | |
| 79 return error_info; | |
| 80 } | |
| 81 | |
| 82 base::WaitableEvent* events[] = {&work_done_or_abandoned_, &stop_requested_}; | |
| 83 base::WaitableEvent::WaitMany(events, arraysize(events)); | |
| 84 | |
| 85 return error_info; | |
| 86 } | |
| 87 | |
| 88 void UIModelWorker::RequestStop() { | |
| 89 DCHECK(ui_thread_->BelongsToCurrentThread()); | |
| 90 ModelSafeWorker::RequestStop(); | |
| 91 stop_requested_.Signal(); | |
| 92 } | |
| 93 | 14 |
| 94 ModelSafeGroup UIModelWorker::GetModelSafeGroup() { | 15 ModelSafeGroup UIModelWorker::GetModelSafeGroup() { |
| 95 return GROUP_UI; | 16 return GROUP_UI; |
| 96 } | 17 } |
| 97 | 18 |
| 98 bool UIModelWorker::IsOnModelThread() { | 19 bool UIModelWorker::IsOnModelThread() { |
| 99 return ui_thread_->BelongsToCurrentThread(); | 20 return ui_thread_->BelongsToCurrentThread(); |
| 100 } | 21 } |
| 101 | 22 |
| 102 UIModelWorker::~UIModelWorker() {} | 23 UIModelWorker::~UIModelWorker() {} |
| 103 | 24 |
| 25 void UIModelWorker::ScheduleWork(base::OnceClosure work) { |
| 26 ui_thread_->PostTask(FROM_HERE, std::move(work)); |
| 27 } |
| 28 |
| 104 } // namespace syncer | 29 } // namespace syncer |
| OLD | NEW |