Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / webkit / glue / webthread_impl.cc
blobbbc2429d69796c98c15173b81410c8d6a90c5f44
1 // Copyright (c) 2012 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.
5 // An implementation of WebThread in terms of base::MessageLoop and
6 // base::Thread
8 #include "webkit/glue/webthread_impl.h"
10 #include "base/bind.h"
11 #include "base/bind_helpers.h"
12 #include "base/message_loop.h"
13 #include "base/pending_task.h"
14 #include "base/threading/platform_thread.h"
16 namespace webkit_glue {
18 WebThreadBase::WebThreadBase() { }
19 WebThreadBase::~WebThreadBase() { }
21 class WebThreadBase::TaskObserverAdapter : public MessageLoop::TaskObserver {
22 public:
23 TaskObserverAdapter(WebThread::TaskObserver* observer)
24 : observer_(observer) { }
26 virtual void WillProcessTask(const base::PendingTask& pending_task) OVERRIDE {
27 observer_->willProcessTask();
30 virtual void DidProcessTask(const base::PendingTask& pending_task) OVERRIDE {
31 observer_->didProcessTask();
34 private:
35 WebThread::TaskObserver* observer_;
38 void WebThreadBase::addTaskObserver(TaskObserver* observer) {
39 CHECK(IsCurrentThread());
40 std::pair<TaskObserverMap::iterator, bool> result = task_observer_map_.insert(
41 std::make_pair(observer, static_cast<TaskObserverAdapter*>(NULL)));
42 if (result.second)
43 result.first->second = new TaskObserverAdapter(observer);
44 MessageLoop::current()->AddTaskObserver(result.first->second);
47 void WebThreadBase::removeTaskObserver(TaskObserver* observer) {
48 CHECK(IsCurrentThread());
49 TaskObserverMap::iterator iter = task_observer_map_.find(observer);
50 if (iter == task_observer_map_.end())
51 return;
52 MessageLoop::current()->RemoveTaskObserver(iter->second);
53 delete iter->second;
54 task_observer_map_.erase(iter);
57 WebThreadImpl::WebThreadImpl(const char* name)
58 : thread_(new base::Thread(name)) {
59 thread_->Start();
62 void WebThreadImpl::postTask(Task* task) {
63 thread_->message_loop()->PostTask(
64 FROM_HERE, base::Bind(&WebKit::WebThread::Task::run, base::Owned(task)));
67 void WebThreadImpl::postDelayedTask(
68 Task* task, long long delay_ms) {
69 thread_->message_loop()->PostDelayedTask(
70 FROM_HERE,
71 base::Bind(&WebKit::WebThread::Task::run, base::Owned(task)),
72 base::TimeDelta::FromMilliseconds(delay_ms));
75 void WebThreadImpl::enterRunLoop() {
76 CHECK(IsCurrentThread());
77 CHECK(!thread_->message_loop()->is_running()); // We don't support nesting.
78 thread_->message_loop()->Run();
81 void WebThreadImpl::exitRunLoop() {
82 CHECK(IsCurrentThread());
83 CHECK(thread_->message_loop()->is_running());
84 thread_->message_loop()->Quit();
87 bool WebThreadImpl::IsCurrentThread() const {
88 return thread_->thread_id() == base::PlatformThread::CurrentId();
91 WebThreadImpl::~WebThreadImpl() {
92 thread_->Stop();
95 WebThreadImplForMessageLoop::WebThreadImplForMessageLoop(
96 base::MessageLoopProxy* message_loop)
97 : message_loop_(message_loop) {
100 void WebThreadImplForMessageLoop::postTask(Task* task) {
101 message_loop_->PostTask(
102 FROM_HERE, base::Bind(&WebKit::WebThread::Task::run, base::Owned(task)));
105 void WebThreadImplForMessageLoop::postDelayedTask(
106 Task* task, long long delay_ms) {
107 message_loop_->PostDelayedTask(
108 FROM_HERE,
109 base::Bind(&WebKit::WebThread::Task::run, base::Owned(task)),
110 base::TimeDelta::FromMilliseconds(delay_ms));
113 void WebThreadImplForMessageLoop::enterRunLoop() {
114 CHECK(IsCurrentThread());
115 CHECK(!MessageLoop::current()->is_running()); // We don't support nesting.
116 MessageLoop::current()->Run();
119 void WebThreadImplForMessageLoop::exitRunLoop() {
120 CHECK(IsCurrentThread());
121 CHECK(MessageLoop::current()->is_running());
122 MessageLoop::current()->Quit();
125 bool WebThreadImplForMessageLoop::IsCurrentThread() const {
126 return message_loop_->BelongsToCurrentThread();
129 WebThreadImplForMessageLoop::~WebThreadImplForMessageLoop() {