| 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/engine/signal_event_on_delete.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( |
| 19 void CallDoWorkAndSignalCallback(const WorkCallback& work, | 20 const WorkCallback& work, |
| 20 base::WaitableEvent* work_done, | 21 syncer::SignalEventOnDelete signal_event_on_delete, |
| 21 SyncerError* error_info) { | 22 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(); | 23 *error_info = work.Run(); |
| 35 | 24 // The event in |signal_event_on_delete| is signaled at the end of this scope. |
| 36 work_done->Signal(); // Unblock the syncer thread that scheduled us. | |
| 37 } | 25 } |
| 38 | 26 |
| 39 } // namespace | 27 } // namespace |
| 40 | 28 |
| 41 UIModelWorker::UIModelWorker( | 29 UIModelWorker::UIModelWorker( |
| 42 const scoped_refptr<base::SingleThreadTaskRunner>& ui_thread, | 30 const scoped_refptr<base::SingleThreadTaskRunner>& ui_thread, |
| 43 WorkerLoopDestructionObserver* observer) | 31 WorkerLoopDestructionObserver* observer) |
| 44 : ModelSafeWorker(observer), ui_thread_(ui_thread) {} | 32 : ModelSafeWorker(observer), ui_thread_(ui_thread) {} |
| 45 | 33 |
| 46 void UIModelWorker::RegisterForLoopDestruction() { | 34 void UIModelWorker::RegisterForLoopDestruction() { |
| 47 CHECK(ui_thread_->BelongsToCurrentThread()); | 35 CHECK(ui_thread_->BelongsToCurrentThread()); |
| 48 SetWorkingLoopToCurrent(); | 36 SetWorkingLoopToCurrent(); |
| 49 } | 37 } |
| 50 | 38 |
| 51 SyncerError UIModelWorker::DoWorkAndWaitUntilDoneImpl( | 39 SyncerError UIModelWorker::DoWorkAndWaitUntilDoneImpl( |
| 52 const WorkCallback& work) { | 40 const WorkCallback& work) { |
| 53 SyncerError error_info; | 41 SyncerError error_info; |
| 54 if (ui_thread_->BelongsToCurrentThread()) { | 42 if (ui_thread_->BelongsToCurrentThread()) { |
| 55 DLOG(WARNING) << "DoWorkAndWaitUntilDone called from " | 43 DLOG(WARNING) << "DoWorkAndWaitUntilDone called from " |
| 56 << "ui_loop_. Probably a nested invocation?"; | 44 << "ui_loop_. Probably a nested invocation?"; |
| 57 return work.Run(); | 45 return work.Run(); |
| 58 } | 46 } |
| 59 | 47 |
| 48 // Signaled when the task is deleted, i.e. after it runs or when it is |
| 49 // abandonned. |
| 50 base::WaitableEvent work_done_or_abandonned( |
| 51 base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| 52 base::WaitableEvent::InitialState::NOT_SIGNALED); |
| 53 |
| 60 if (!ui_thread_->PostTask(FROM_HERE, | 54 if (!ui_thread_->PostTask(FROM_HERE, |
| 61 base::Bind(&CallDoWorkAndSignalCallback, work, | 55 base::Bind(&CallDoWorkAndSignalEvent, work, |
| 62 work_done_or_stopped(), &error_info))) { | 56 base::Passed(syncer::SignalEventOnDelete( |
| 57 &work_done_or_abandonned)), |
| 58 &error_info))) { |
| 63 DLOG(WARNING) << "Could not post work to UI loop."; | 59 DLOG(WARNING) << "Could not post work to UI loop."; |
| 64 error_info = CANNOT_DO_WORK; | 60 error_info = CANNOT_DO_WORK; |
| 65 return error_info; | 61 return error_info; |
| 66 } | 62 } |
| 67 work_done_or_stopped()->Wait(); | 63 work_done_or_abandonned.Wait(); |
| 68 | 64 |
| 69 return error_info; | 65 return error_info; |
| 70 } | 66 } |
| 71 | 67 |
| 72 ModelSafeGroup UIModelWorker::GetModelSafeGroup() { | 68 ModelSafeGroup UIModelWorker::GetModelSafeGroup() { |
| 73 return GROUP_UI; | 69 return GROUP_UI; |
| 74 } | 70 } |
| 75 | 71 |
| 76 UIModelWorker::~UIModelWorker() {} | 72 UIModelWorker::~UIModelWorker() {} |
| 77 | 73 |
| 78 } // namespace syncer | 74 } // namespace syncer |
| OLD | NEW |