| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/sync/driver/glue/ui_model_worker.h" | |
| 6 | |
| 7 #include <utility> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/bind_helpers.h" | |
| 11 #include "base/callback.h" | |
| 12 #include "base/synchronization/waitable_event.h" | |
| 13 #include "components/sync/base/scoped_event_signal.h" | |
| 14 | |
| 15 namespace syncer { | |
| 16 | |
| 17 namespace { | |
| 18 | |
| 19 void CallDoWorkAndSignalEvent(const WorkCallback& work, | |
| 20 syncer::ScopedEventSignal scoped_event_signal, | |
| 21 SyncerError* error_info) { | |
| 22 *error_info = work.Run(); | |
| 23 // The event in |scoped_event_signal| is signaled at the end of this scope. | |
| 24 } | |
| 25 | |
| 26 } // namespace | |
| 27 | |
| 28 UIModelWorker::UIModelWorker( | |
| 29 scoped_refptr<base::SingleThreadTaskRunner> ui_thread) | |
| 30 : ui_thread_(std::move(ui_thread)) {} | |
| 31 | |
| 32 SyncerError UIModelWorker::DoWorkAndWaitUntilDoneImpl( | |
| 33 const WorkCallback& work) { | |
| 34 SyncerError error_info; | |
| 35 if (ui_thread_->BelongsToCurrentThread()) { | |
| 36 DLOG(WARNING) << "DoWorkAndWaitUntilDone called from " | |
| 37 << "ui_loop_. Probably a nested invocation?"; | |
| 38 return work.Run(); | |
| 39 } | |
| 40 | |
| 41 // Signaled when the task is deleted, i.e. after it runs or when it is | |
| 42 // abandoned. | |
| 43 base::WaitableEvent work_done_or_abandoned( | |
| 44 base::WaitableEvent::ResetPolicy::AUTOMATIC, | |
| 45 base::WaitableEvent::InitialState::NOT_SIGNALED); | |
| 46 | |
| 47 if (!ui_thread_->PostTask(FROM_HERE, | |
| 48 base::Bind(&CallDoWorkAndSignalEvent, work, | |
| 49 base::Passed(syncer::ScopedEventSignal( | |
| 50 &work_done_or_abandoned)), | |
| 51 &error_info))) { | |
| 52 DLOG(WARNING) << "Could not post work to UI loop."; | |
| 53 error_info = CANNOT_DO_WORK; | |
| 54 return error_info; | |
| 55 } | |
| 56 work_done_or_abandoned.Wait(); | |
| 57 | |
| 58 return error_info; | |
| 59 } | |
| 60 | |
| 61 ModelSafeGroup UIModelWorker::GetModelSafeGroup() { | |
| 62 return GROUP_UI; | |
| 63 } | |
| 64 | |
| 65 bool UIModelWorker::IsOnModelThread() { | |
| 66 return ui_thread_->BelongsToCurrentThread(); | |
| 67 } | |
| 68 | |
| 69 UIModelWorker::~UIModelWorker() {} | |
| 70 | |
| 71 } // namespace syncer | |
| OLD | NEW |