summaryrefslogtreecommitdiffstats
path: root/chrome/browser/sync/glue/browser_thread_model_worker.cc
blob: 57e4b9d8f6b3fd078b0895e2f8775cc19bbeac74 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// 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/browser_thread_model_worker.h"

#include "base/bind.h"
#include "base/synchronization/waitable_event.h"
#include "content/public/browser/browser_thread.h"

using base::WaitableEvent;
using content::BrowserThread;

namespace browser_sync {

BrowserThreadModelWorker::BrowserThreadModelWorker(
    BrowserThread::ID thread, syncer::ModelSafeGroup group,
    syncer::WorkerLoopDestructionObserver* observer)
    : ModelSafeWorker(observer),
      thread_(thread), group_(group) {
}

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

  if (!BrowserThread::PostTask(
      thread_,
      FROM_HERE,
      base::Bind(&BrowserThreadModelWorker::CallDoWorkAndSignalTask,
                 this, work,
                 work_done_or_stopped(), &error))) {
    DLOG(WARNING) << "Failed to post task to thread " << thread_;
    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 (BrowserThread::CurrentlyOn(thread_)) {
    base::MessageLoop::current()->AddDestructionObserver(this);
  } else {
    BrowserThread::PostTask(
        thread_, FROM_HERE,
        Bind(&BrowserThreadModelWorker::RegisterForLoopDestruction, this));
  }
}

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

DatabaseModelWorker::DatabaseModelWorker(
    syncer::WorkerLoopDestructionObserver* observer)
    : BrowserThreadModelWorker(BrowserThread::DB, syncer::GROUP_DB, observer) {
}

void DatabaseModelWorker::CallDoWorkAndSignalTask(
    const syncer::WorkCallback& work,
    WaitableEvent* done,
    syncer::SyncerError* error) {
  BrowserThreadModelWorker::CallDoWorkAndSignalTask(work, done, error);
}

DatabaseModelWorker::~DatabaseModelWorker() {}

FileModelWorker::FileModelWorker(
    syncer::WorkerLoopDestructionObserver* observer)
    : BrowserThreadModelWorker(BrowserThread::FILE, syncer::GROUP_FILE,
                               observer) {
}

void FileModelWorker::CallDoWorkAndSignalTask(
    const syncer::WorkCallback& work,
    WaitableEvent* done,
    syncer::SyncerError* error) {
  BrowserThreadModelWorker::CallDoWorkAndSignalTask(work, done, error);
}

FileModelWorker::~FileModelWorker() {}

}  // namespace browser_sync