Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(579)

Unified Diff: components/sync/engine/ui_model_worker.cc

Issue 2782573002: [Sync] Refactor ModelSafeWorker::DoWorkAndWaitUntilDone() to avoid code duplication. (Closed)
Patch Set: self-review Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « components/sync/engine/ui_model_worker.h ('k') | components/sync/engine/ui_model_worker_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: components/sync/engine/ui_model_worker.cc
diff --git a/components/sync/engine/ui_model_worker.cc b/components/sync/engine/ui_model_worker.cc
index 77fe9c7799a65db7437df9cc45edf62a0611467c..08cc1a2502bbd09f396b756bc47bd070eca0782b 100644
--- a/components/sync/engine/ui_model_worker.cc
+++ b/components/sync/engine/ui_model_worker.cc
@@ -6,90 +6,11 @@
#include <utility>
-#include "base/bind.h"
-#include "base/bind_helpers.h"
-#include "base/callback.h"
-#include "components/sync/base/scoped_event_signal.h"
-
namespace syncer {
-namespace {
-
-class ScopedEventSignalWithWorker {
- public:
- ScopedEventSignalWithWorker(scoped_refptr<UIModelWorker> ui_model_worker,
- base::WaitableEvent* event)
- : ui_model_worker_(std::move(ui_model_worker)),
- scoped_event_signal_(event) {}
-
- ScopedEventSignalWithWorker(ScopedEventSignalWithWorker&&) = default;
- ScopedEventSignalWithWorker& operator=(ScopedEventSignalWithWorker&&) =
- default;
-
- bool IsStopped() const { return ui_model_worker_->IsStopped(); }
-
- private:
- // This reference prevents the event in |scoped_event_signal_| from being
- // signaled after being deleted.
- scoped_refptr<UIModelWorker> ui_model_worker_;
-
- ScopedEventSignal scoped_event_signal_;
-
- DISALLOW_COPY_AND_ASSIGN(ScopedEventSignalWithWorker);
-};
-
-void CallDoWorkAndSignalEvent(
- const WorkCallback& work,
- ScopedEventSignalWithWorker scoped_event_signal_with_worker,
- SyncerError* error_info) {
- if (!scoped_event_signal_with_worker.IsStopped())
- *error_info = work.Run();
- // The event in |scoped_event_signal_with_worker| is signaled at the end of
- // this scope.
-}
-
-} // namespace
-
UIModelWorker::UIModelWorker(
scoped_refptr<base::SingleThreadTaskRunner> ui_thread)
- : ui_thread_(std::move(ui_thread)),
- work_done_or_abandoned_(base::WaitableEvent::ResetPolicy::MANUAL,
- base::WaitableEvent::InitialState::NOT_SIGNALED),
- stop_requested_(base::WaitableEvent::ResetPolicy::MANUAL,
- base::WaitableEvent::InitialState::NOT_SIGNALED) {
- sequence_checker_.DetachFromSequence();
-}
-
-SyncerError UIModelWorker::DoWorkAndWaitUntilDoneImpl(
- const WorkCallback& work) {
- DCHECK(sequence_checker_.CalledOnValidSequence());
- DCHECK(!ui_thread_->BelongsToCurrentThread());
-
- SyncerError error_info;
- work_done_or_abandoned_.Reset();
-
- if (!ui_thread_->PostTask(
- FROM_HERE,
- base::Bind(&CallDoWorkAndSignalEvent, work,
- base::Passed(syncer::ScopedEventSignalWithWorker(
- this, &work_done_or_abandoned_)),
- &error_info))) {
- DLOG(WARNING) << "Could not post work to UI loop.";
- error_info = CANNOT_DO_WORK;
- return error_info;
- }
-
- base::WaitableEvent* events[] = {&work_done_or_abandoned_, &stop_requested_};
- base::WaitableEvent::WaitMany(events, arraysize(events));
-
- return error_info;
-}
-
-void UIModelWorker::RequestStop() {
- DCHECK(ui_thread_->BelongsToCurrentThread());
- ModelSafeWorker::RequestStop();
- stop_requested_.Signal();
-}
+ : ui_thread_(std::move(ui_thread)) {}
ModelSafeGroup UIModelWorker::GetModelSafeGroup() {
return GROUP_UI;
@@ -101,4 +22,8 @@ bool UIModelWorker::IsOnModelThread() {
UIModelWorker::~UIModelWorker() {}
+void UIModelWorker::ScheduleWork(base::OnceClosure work) {
+ ui_thread_->PostTask(FROM_HERE, std::move(work));
+}
+
} // namespace syncer
« no previous file with comments | « components/sync/engine/ui_model_worker.h ('k') | components/sync/engine/ui_model_worker_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698