history_model_worker.cc revision 3f50c38dc070f4bb515c1b64450dae14f316474e
1// Copyright (c) 2010 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 "chrome/browser/sync/glue/history_model_worker.h"
6
7#include "base/message_loop.h"
8#include "base/ref_counted.h"
9#include "base/task.h"
10#include "base/synchronization/waitable_event.h"
11#include "chrome/browser/history/history.h"
12
13using base::WaitableEvent;
14
15namespace browser_sync {
16
17class WorkerTask : public HistoryDBTask {
18 public:
19  WorkerTask(Callback0::Type* work, WaitableEvent* done)
20    : work_(work), done_(done) {}
21
22  virtual bool RunOnDBThread(history::HistoryBackend* backend,
23                             history::HistoryDatabase* db) {
24    work_->Run();
25    done_->Signal();
26    return true;
27  }
28
29  // Since the DoWorkAndWaitUntilDone() is syncronous, we don't need to run any
30  // code asynchronously on the main thread after completion.
31  virtual void DoneRunOnMainThread() {}
32
33 protected:
34  Callback0::Type* work_;
35  WaitableEvent* done_;
36};
37
38
39HistoryModelWorker::HistoryModelWorker(HistoryService* history_service)
40  : history_service_(history_service) {
41}
42
43HistoryModelWorker::~HistoryModelWorker() {
44}
45
46void HistoryModelWorker::DoWorkAndWaitUntilDone(Callback0::Type* work) {
47  WaitableEvent done(false, false);
48  scoped_refptr<WorkerTask> task(new WorkerTask(work, &done));
49  history_service_->ScheduleDBTask(task.get(), this);
50  done.Wait();
51}
52
53ModelSafeGroup HistoryModelWorker::GetModelSafeGroup() {
54  return GROUP_HISTORY;
55}
56
57bool HistoryModelWorker::CurrentThreadIsWorkThread() {
58  // TODO(ncarter): How to determine this?
59  return true;
60}
61
62}  // namespace browser_sync
63