| 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/driver/glue/ui_model_worker.h" | 5 #include "components/sync/driver/glue/ui_model_worker.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/synchronization/waitable_event.h" | 10 #include "base/synchronization/waitable_event.h" |
| 11 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | 11 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
| 12 #include "base/threading/thread_restrictions.h" | 12 #include "base/threading/thread_restrictions.h" |
| 13 #include "components/sync/base/scoped_event_signal.h" |
| 13 | 14 |
| 14 namespace syncer { | 15 namespace syncer { |
| 15 | 16 |
| 16 namespace { | 17 namespace { |
| 17 | 18 |
| 18 // A simple callback to signal a waitable event after running a closure. | 19 void CallDoWorkAndSignalEvent(const WorkCallback& work, |
| 19 void CallDoWorkAndSignalCallback(const WorkCallback& work, | 20 syncer::ScopedEventSignal scoped_event_signal, |
| 20 base::WaitableEvent* work_done, | 21 SyncerError* error_info) { |
| 21 SyncerError* error_info) { | |
| 22 if (work.is_null()) { | |
| 23 // This can happen during tests or cases where there are more than just the | |
| 24 // default UIModelWorker in existence and it gets destroyed before | |
| 25 // the main UI loop has terminated. There is no easy way to assert the | |
| 26 // loop is running / not running at the moment, so we just provide cancel | |
| 27 // semantics here and short-circuit. | |
| 28 // TODO(timsteele): Maybe we should have the message loop destruction | |
| 29 // observer fire when the loop has ended, just a bit before it | |
| 30 // actually gets destroyed. | |
| 31 return; | |
| 32 } | |
| 33 | |
| 34 *error_info = work.Run(); | 22 *error_info = work.Run(); |
| 35 | 23 // The event in |scoped_event_signal| is signaled at the end of this scope. |
| 36 work_done->Signal(); // Unblock the syncer thread that scheduled us. | |
| 37 } | 24 } |
| 38 | 25 |
| 39 } // namespace | 26 } // namespace |
| 40 | 27 |
| 41 UIModelWorker::UIModelWorker( | 28 UIModelWorker::UIModelWorker( |
| 42 const scoped_refptr<base::SingleThreadTaskRunner>& ui_thread, | 29 const scoped_refptr<base::SingleThreadTaskRunner>& ui_thread, |
| 43 WorkerLoopDestructionObserver* observer) | 30 WorkerLoopDestructionObserver* observer) |
| 44 : ModelSafeWorker(observer), ui_thread_(ui_thread) {} | 31 : ModelSafeWorker(observer), ui_thread_(ui_thread) {} |
| 45 | 32 |
| 46 void UIModelWorker::RegisterForLoopDestruction() { | 33 void UIModelWorker::RegisterForLoopDestruction() { |
| 47 CHECK(ui_thread_->BelongsToCurrentThread()); | 34 CHECK(ui_thread_->BelongsToCurrentThread()); |
| 48 SetWorkingLoopToCurrent(); | 35 SetWorkingLoopToCurrent(); |
| 49 } | 36 } |
| 50 | 37 |
| 51 SyncerError UIModelWorker::DoWorkAndWaitUntilDoneImpl( | 38 SyncerError UIModelWorker::DoWorkAndWaitUntilDoneImpl( |
| 52 const WorkCallback& work) { | 39 const WorkCallback& work) { |
| 53 SyncerError error_info; | 40 SyncerError error_info; |
| 54 if (ui_thread_->BelongsToCurrentThread()) { | 41 if (ui_thread_->BelongsToCurrentThread()) { |
| 55 DLOG(WARNING) << "DoWorkAndWaitUntilDone called from " | 42 DLOG(WARNING) << "DoWorkAndWaitUntilDone called from " |
| 56 << "ui_loop_. Probably a nested invocation?"; | 43 << "ui_loop_. Probably a nested invocation?"; |
| 57 return work.Run(); | 44 return work.Run(); |
| 58 } | 45 } |
| 59 | 46 |
| 47 // Signaled when the task is deleted, i.e. after it runs or when it is |
| 48 // abandoned. |
| 49 base::WaitableEvent work_done_or_abandoned( |
| 50 base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| 51 base::WaitableEvent::InitialState::NOT_SIGNALED); |
| 52 |
| 60 if (!ui_thread_->PostTask(FROM_HERE, | 53 if (!ui_thread_->PostTask(FROM_HERE, |
| 61 base::Bind(&CallDoWorkAndSignalCallback, work, | 54 base::Bind(&CallDoWorkAndSignalEvent, work, |
| 62 work_done_or_stopped(), &error_info))) { | 55 base::Passed(syncer::ScopedEventSignal( |
| 56 &work_done_or_abandoned)), |
| 57 &error_info))) { |
| 63 DLOG(WARNING) << "Could not post work to UI loop."; | 58 DLOG(WARNING) << "Could not post work to UI loop."; |
| 64 error_info = CANNOT_DO_WORK; | 59 error_info = CANNOT_DO_WORK; |
| 65 return error_info; | 60 return error_info; |
| 66 } | 61 } |
| 67 work_done_or_stopped()->Wait(); | 62 work_done_or_abandoned.Wait(); |
| 68 | 63 |
| 69 return error_info; | 64 return error_info; |
| 70 } | 65 } |
| 71 | 66 |
| 72 ModelSafeGroup UIModelWorker::GetModelSafeGroup() { | 67 ModelSafeGroup UIModelWorker::GetModelSafeGroup() { |
| 73 return GROUP_UI; | 68 return GROUP_UI; |
| 74 } | 69 } |
| 75 | 70 |
| 76 UIModelWorker::~UIModelWorker() {} | 71 UIModelWorker::~UIModelWorker() {} |
| 77 | 72 |
| 78 } // namespace syncer | 73 } // namespace syncer |
| OLD | NEW |