1 // Copyright 2014 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 // An implementation of WebThread in terms of base::MessageLoop and
6 // base::Thread
7
8 #include "content/child/webthread_impl.h"
9
10 #include "base/bind.h"
11 #include "base/bind_helpers.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/pending_task.h"
14 #include "base/threading/platform_thread.h"
15
16 namespace content {
17
WebThreadBase()18 WebThreadBase::WebThreadBase() {}
~WebThreadBase()19 WebThreadBase::~WebThreadBase() {}
20
21 class WebThreadBase::TaskObserverAdapter
22 : public base::MessageLoop::TaskObserver {
23 public:
TaskObserverAdapter(WebThread::TaskObserver * observer)24 TaskObserverAdapter(WebThread::TaskObserver* observer)
25 : observer_(observer) {}
26
WillProcessTask(const base::PendingTask & pending_task)27 virtual void WillProcessTask(const base::PendingTask& pending_task) OVERRIDE {
28 observer_->willProcessTask();
29 }
30
DidProcessTask(const base::PendingTask & pending_task)31 virtual void DidProcessTask(const base::PendingTask& pending_task) OVERRIDE {
32 observer_->didProcessTask();
33 }
34
35 private:
36 WebThread::TaskObserver* observer_;
37 };
38
addTaskObserver(TaskObserver * observer)39 void WebThreadBase::addTaskObserver(TaskObserver* observer) {
40 CHECK(isCurrentThread());
41 std::pair<TaskObserverMap::iterator, bool> result = task_observer_map_.insert(
42 std::make_pair(observer, static_cast<TaskObserverAdapter*>(NULL)));
43 if (result.second)
44 result.first->second = new TaskObserverAdapter(observer);
45 base::MessageLoop::current()->AddTaskObserver(result.first->second);
46 }
47
removeTaskObserver(TaskObserver * observer)48 void WebThreadBase::removeTaskObserver(TaskObserver* observer) {
49 CHECK(isCurrentThread());
50 TaskObserverMap::iterator iter = task_observer_map_.find(observer);
51 if (iter == task_observer_map_.end())
52 return;
53 base::MessageLoop::current()->RemoveTaskObserver(iter->second);
54 delete iter->second;
55 task_observer_map_.erase(iter);
56 }
57
WebThreadImpl(const char * name)58 WebThreadImpl::WebThreadImpl(const char* name)
59 : thread_(new base::Thread(name)) {
60 thread_->Start();
61 }
62
postTask(Task * task)63 void WebThreadImpl::postTask(Task* task) {
64 thread_->message_loop()->PostTask(
65 FROM_HERE, base::Bind(&blink::WebThread::Task::run, base::Owned(task)));
66 }
67
postDelayedTask(Task * task,long long delay_ms)68 void WebThreadImpl::postDelayedTask(Task* task, long long delay_ms) {
69 thread_->message_loop()->PostDelayedTask(
70 FROM_HERE,
71 base::Bind(&blink::WebThread::Task::run, base::Owned(task)),
72 base::TimeDelta::FromMilliseconds(delay_ms));
73 }
74
enterRunLoop()75 void WebThreadImpl::enterRunLoop() {
76 CHECK(isCurrentThread());
77 CHECK(!thread_->message_loop()->is_running()); // We don't support nesting.
78 thread_->message_loop()->Run();
79 }
80
exitRunLoop()81 void WebThreadImpl::exitRunLoop() {
82 CHECK(isCurrentThread());
83 CHECK(thread_->message_loop()->is_running());
84 thread_->message_loop()->Quit();
85 }
86
isCurrentThread() const87 bool WebThreadImpl::isCurrentThread() const {
88 return thread_->thread_id() == base::PlatformThread::CurrentId();
89 }
90
~WebThreadImpl()91 WebThreadImpl::~WebThreadImpl() {
92 thread_->Stop();
93 }
94
WebThreadImplForMessageLoop(base::MessageLoopProxy * message_loop)95 WebThreadImplForMessageLoop::WebThreadImplForMessageLoop(
96 base::MessageLoopProxy* message_loop)
97 : message_loop_(message_loop) {}
98
postTask(Task * task)99 void WebThreadImplForMessageLoop::postTask(Task* task) {
100 message_loop_->PostTask(
101 FROM_HERE, base::Bind(&blink::WebThread::Task::run, base::Owned(task)));
102 }
103
postDelayedTask(Task * task,long long delay_ms)104 void WebThreadImplForMessageLoop::postDelayedTask(Task* task,
105 long long delay_ms) {
106 message_loop_->PostDelayedTask(
107 FROM_HERE,
108 base::Bind(&blink::WebThread::Task::run, base::Owned(task)),
109 base::TimeDelta::FromMilliseconds(delay_ms));
110 }
111
enterRunLoop()112 void WebThreadImplForMessageLoop::enterRunLoop() {
113 CHECK(isCurrentThread());
114 // We don't support nesting.
115 CHECK(!base::MessageLoop::current()->is_running());
116 base::MessageLoop::current()->Run();
117 }
118
exitRunLoop()119 void WebThreadImplForMessageLoop::exitRunLoop() {
120 CHECK(isCurrentThread());
121 CHECK(base::MessageLoop::current()->is_running());
122 base::MessageLoop::current()->Quit();
123 }
124
isCurrentThread() const125 bool WebThreadImplForMessageLoop::isCurrentThread() const {
126 return message_loop_->BelongsToCurrentThread();
127 }
128
~WebThreadImplForMessageLoop()129 WebThreadImplForMessageLoop::~WebThreadImplForMessageLoop() {}
130
131 } // namespace content
132