Get foreground tab on Android
[chromium-blink-merge.git] / webkit / child / webthread_impl.cc
blob4646662699074915c3c0a977ccff33b5142aebd4
1 // Copyright 2013 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/child/webthread_impl.h"
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"
16 namespace webkit_glue {
18 WebThreadBase::WebThreadBase() { }
19 WebThreadBase::~WebThreadBase() { }
21 class WebThreadBase::TaskObserverAdapter :
22 public base::MessageLoop::TaskObserver {
23 public:
24 TaskObserverAdapter(WebThread::TaskObserver* observer)
25 : observer_(observer) { }
27 virtual void WillProcessTask(const base::PendingTask& pending_task) OVERRIDE {
28 observer_->willProcessTask();
31 virtual void DidProcessTask(const base::PendingTask& pending_task) OVERRIDE {
32 observer_->didProcessTask();
35 private:
36 WebThread::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);
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);
58 WebThreadImpl::WebThreadImpl(const char* name)
59 : thread_(new base::Thread(name)) {
60 thread_->Start();
63 void WebThreadImpl::postTask(Task* task) {
64 thread_->message_loop()->PostTask(
65 FROM_HERE, base::Bind(&blink::WebThread::Task::run, base::Owned(task)));
68 void WebThreadImpl::postDelayedTask(
69 Task* task, long long delay_ms) {
70 thread_->message_loop()->PostDelayedTask(
71 FROM_HERE,
72 base::Bind(&blink::WebThread::Task::run, base::Owned(task)),
73 base::TimeDelta::FromMilliseconds(delay_ms));
76 void WebThreadImpl::enterRunLoop() {
77 CHECK(isCurrentThread());
78 CHECK(!thread_->message_loop()->is_running()); // We don't support nesting.
79 thread_->message_loop()->Run();
82 void WebThreadImpl::exitRunLoop() {
83 CHECK(isCurrentThread());
84 CHECK(thread_->message_loop()->is_running());
85 thread_->message_loop()->Quit();
88 bool WebThreadImpl::isCurrentThread() const {
89 return thread_->thread_id() == base::PlatformThread::CurrentId();
92 WebThreadImpl::~WebThreadImpl() {
93 thread_->Stop();
96 WebThreadImplForMessageLoop::WebThreadImplForMessageLoop(
97 base::MessageLoopProxy* message_loop)
98 : message_loop_(message_loop) {
101 void WebThreadImplForMessageLoop::postTask(Task* task) {
102 message_loop_->PostTask(
103 FROM_HERE, base::Bind(&blink::WebThread::Task::run, base::Owned(task)));
106 void WebThreadImplForMessageLoop::postDelayedTask(
107 Task* task, long long delay_ms) {
108 message_loop_->PostDelayedTask(
109 FROM_HERE,
110 base::Bind(&blink::WebThread::Task::run, base::Owned(task)),
111 base::TimeDelta::FromMilliseconds(delay_ms));
114 void WebThreadImplForMessageLoop::enterRunLoop() {
115 CHECK(isCurrentThread());
116 CHECK(!base::MessageLoop::current()
117 ->is_running()); // We don't support nesting.
118 base::MessageLoop::current()->Run();
121 void WebThreadImplForMessageLoop::exitRunLoop() {
122 CHECK(isCurrentThread());
123 CHECK(base::MessageLoop::current()->is_running());
124 base::MessageLoop::current()->Quit();
127 bool WebThreadImplForMessageLoop::isCurrentThread() const {
128 return message_loop_->BelongsToCurrentThread();
131 WebThreadImplForMessageLoop::~WebThreadImplForMessageLoop() {