summaryrefslogtreecommitdiffstats
path: root/components/sync_driver/glue/browser_thread_model_worker.cc
blob: faf53fc4a3ec8f32371569e697d1f33169f1311e (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
// Copyright 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 "components/sync_driver/glue/browser_thread_model_worker.h"

#include "base/bind.h"
#include "base/single_thread_task_runner.h"
#include "base/synchronization/waitable_event.h"

using base::SingleThreadTaskRunner;
using base::WaitableEvent;

namespace browser_sync {

BrowserThreadModelWorker::BrowserThreadModelWorker(
    const scoped_refptr<SingleThreadTaskRunner>& runner,
    syncer::ModelSafeGroup group,
    syncer::WorkerLoopDestructionObserver* observer)
    : ModelSafeWorker(observer), runner_(runner), group_(group) {}

syncer::SyncerError BrowserThreadModelWorker::DoWorkAndWaitUntilDoneImpl(
    const syncer::WorkCallback& work) {
  syncer::SyncerError error = syncer::UNSET;
  if (runner_->BelongsToCurrentThread()) {
    DLOG(WARNING) << "Already on thread " << runner_;
    return work.Run();
  }

  if (!runner_->PostTask(
          FROM_HERE,
          base::Bind(&BrowserThreadModelWorker::CallDoWorkAndSignalTask, this,
                     work, work_done_or_stopped(), &error))) {
    DLOG(WARNING) << "Failed to post task to runner " << runner_;
    error = syncer::CANNOT_DO_WORK;
    return error;
  }
  work_done_or_stopped()->Wait();
  return error;
}

syncer::ModelSafeGroup BrowserThreadModelWorker::GetModelSafeGroup() {
  return group_;
}

BrowserThreadModelWorker::~BrowserThreadModelWorker() {}

void BrowserThreadModelWorker::RegisterForLoopDestruction() {
  if (runner_->BelongsToCurrentThread()) {
    SetWorkingLoopToCurrent();
  } else {
    runner_->PostTask(
        FROM_HERE,
        Bind(&BrowserThreadModelWorker::RegisterForLoopDestruction, this));
  }
}

void BrowserThreadModelWorker::CallDoWorkAndSignalTask(
    const syncer::WorkCallback& work,
    WaitableEvent* done,
    syncer::SyncerError* error) {
  DCHECK(runner_->BelongsToCurrentThread());
  if (!IsStopped())
    *error = work.Run();
  done->Signal();
}

}  // namespace browser_sync