blob: 726deb6dfc70e4060f2c3e1294a4c4d1499f04f9 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/sync/glue/ui_model_worker.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/message_loop/message_loop.h"
#include "base/synchronization/waitable_event.h"
#include "base/third_party/dynamic_annotations/dynamic_annotations.h"
#include "base/threading/thread_restrictions.h"
#include "content/public/browser/browser_thread.h"
using content::BrowserThread;
namespace browser_sync {
namespace {
// A simple callback to signal a waitable event after running a closure.
void CallDoWorkAndSignalCallback(const syncer::WorkCallback& work,
base::WaitableEvent* work_done,
syncer::SyncerError* error_info) {
if (work.is_null()) {
// This can happen during tests or cases where there are more than just the
// default UIModelWorker in existence and it gets destroyed before
// the main UI loop has terminated. There is no easy way to assert the
// loop is running / not running at the moment, so we just provide cancel
// semantics here and short-circuit.
// TODO(timsteele): Maybe we should have the message loop destruction
// observer fire when the loop has ended, just a bit before it
// actually gets destroyed.
return;
}
*error_info = work.Run();
work_done->Signal(); // Unblock the syncer thread that scheduled us.
}
} // namespace
UIModelWorker::UIModelWorker(syncer::WorkerLoopDestructionObserver* observer)
: syncer::ModelSafeWorker(observer) {
}
void UIModelWorker::RegisterForLoopDestruction() {
CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
SetWorkingLoopToCurrent();
}
syncer::SyncerError UIModelWorker::DoWorkAndWaitUntilDoneImpl(
const syncer::WorkCallback& work) {
syncer::SyncerError error_info;
if (BrowserThread::CurrentlyOn(BrowserThread::UI)) {
DLOG(WARNING) << "DoWorkAndWaitUntilDone called from "
<< "ui_loop_. Probably a nested invocation?";
return work.Run();
}
if (!BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
base::Bind(&CallDoWorkAndSignalCallback,
work, work_done_or_stopped(), &error_info))) {
DLOG(WARNING) << "Could not post work to UI loop.";
error_info = syncer::CANNOT_DO_WORK;
return error_info;
}
work_done_or_stopped()->Wait();
return error_info;
}
syncer::ModelSafeGroup UIModelWorker::GetModelSafeGroup() {
return syncer::GROUP_UI;
}
UIModelWorker::~UIModelWorker() {
}
} // namespace browser_sync
|