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
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/threading/platform_thread.h"
15 namespace webkit_glue
{
17 WebThreadBase::WebThreadBase() { }
18 WebThreadBase::~WebThreadBase() { }
20 class WebThreadBase::TaskObserverAdapter
: public MessageLoop::TaskObserver
{
22 TaskObserverAdapter(WebThread::TaskObserver
* observer
)
23 : observer_(observer
) { }
25 virtual void WillProcessTask(base::TimeTicks
) OVERRIDE
{
26 observer_
->willProcessTask();
29 virtual void DidProcessTask(base::TimeTicks
) OVERRIDE
{
30 observer_
->didProcessTask();
34 WebThread::TaskObserver
* observer_
;
37 void WebThreadBase::addTaskObserver(TaskObserver
* observer
) {
38 CHECK(IsCurrentThread());
39 std::pair
<TaskObserverMap::iterator
, bool> result
= task_observer_map_
.insert(
40 std::make_pair(observer
, static_cast<TaskObserverAdapter
*>(NULL
)));
42 result
.first
->second
= new TaskObserverAdapter(observer
);
43 MessageLoop::current()->AddTaskObserver(result
.first
->second
);
46 void WebThreadBase::removeTaskObserver(TaskObserver
* observer
) {
47 CHECK(IsCurrentThread());
48 TaskObserverMap::iterator iter
= task_observer_map_
.find(observer
);
49 if (iter
== task_observer_map_
.end())
51 MessageLoop::current()->RemoveTaskObserver(iter
->second
);
53 task_observer_map_
.erase(iter
);
56 WebThreadImpl::WebThreadImpl(const char* name
)
57 : thread_(new base::Thread(name
)) {
61 void WebThreadImpl::postTask(Task
* task
) {
62 thread_
->message_loop()->PostTask(
63 FROM_HERE
, base::Bind(&WebKit::WebThread::Task::run
, base::Owned(task
)));
66 void WebThreadImpl::postDelayedTask(
67 Task
* task
, long long delay_ms
) {
68 thread_
->message_loop()->PostDelayedTask(
70 base::Bind(&WebKit::WebThread::Task::run
, base::Owned(task
)),
71 base::TimeDelta::FromMilliseconds(delay_ms
));
74 void WebThreadImpl::enterRunLoop() {
75 CHECK(IsCurrentThread());
76 CHECK(!thread_
->message_loop()->is_running()); // We don't support nesting.
77 thread_
->message_loop()->Run();
80 void WebThreadImpl::exitRunLoop() {
81 CHECK(IsCurrentThread());
82 CHECK(thread_
->message_loop()->is_running());
83 thread_
->message_loop()->Quit();
86 bool WebThreadImpl::IsCurrentThread() const {
87 return thread_
->thread_id() == base::PlatformThread::CurrentId();
90 WebThreadImpl::~WebThreadImpl() {
94 WebThreadImplForMessageLoop::WebThreadImplForMessageLoop(
95 base::MessageLoopProxy
* message_loop
)
96 : message_loop_(message_loop
) {
99 void WebThreadImplForMessageLoop::postTask(Task
* task
) {
100 message_loop_
->PostTask(
101 FROM_HERE
, base::Bind(&WebKit::WebThread::Task::run
, base::Owned(task
)));
104 void WebThreadImplForMessageLoop::postDelayedTask(
105 Task
* task
, long long delay_ms
) {
106 message_loop_
->PostDelayedTask(
108 base::Bind(&WebKit::WebThread::Task::run
, base::Owned(task
)),
109 base::TimeDelta::FromMilliseconds(delay_ms
));
112 void WebThreadImplForMessageLoop::enterRunLoop() {
113 CHECK(IsCurrentThread());
114 CHECK(!MessageLoop::current()->is_running()); // We don't support nesting.
115 MessageLoop::current()->Run();
118 void WebThreadImplForMessageLoop::exitRunLoop() {
119 CHECK(IsCurrentThread());
120 CHECK(MessageLoop::current()->is_running());
121 MessageLoop::current()->Quit();
124 bool WebThreadImplForMessageLoop::IsCurrentThread() const {
125 return message_loop_
->BelongsToCurrentThread();
128 WebThreadImplForMessageLoop::~WebThreadImplForMessageLoop() {