OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // An implementation of WebThread in terms of base::MessageLoop and | 5 // An implementation of WebThread in terms of base::MessageLoop and |
6 // base::Thread | 6 // base::Thread |
7 | 7 |
8 #include "webkit/glue/webthread_impl.h" | 8 #include "webkit/glue/webthread_impl.h" |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
12 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
| 13 #include "base/threading/platform_thread.h" |
13 | 14 |
14 namespace webkit_glue { | 15 namespace webkit_glue { |
15 | 16 |
| 17 WebThreadBase::WebThreadBase() { } |
| 18 WebThreadBase::~WebThreadBase() { } |
| 19 |
| 20 class WebThreadBase::TaskObserverAdapter : public MessageLoop::TaskObserver { |
| 21 public: |
| 22 TaskObserverAdapter(WebThread::TaskObserver* observer) |
| 23 : observer_(observer) { } |
| 24 |
| 25 // WebThread::TaskObserver does not have a willProcessTask method. |
| 26 virtual void WillProcessTask(base::TimeTicks) OVERRIDE { } |
| 27 |
| 28 virtual void DidProcessTask(base::TimeTicks) OVERRIDE { |
| 29 observer_->didProcessTask(); |
| 30 } |
| 31 |
| 32 private: |
| 33 WebThread::TaskObserver* observer_; |
| 34 }; |
| 35 |
| 36 void WebThreadBase::addTaskObserver(TaskObserver* observer) { |
| 37 CHECK(IsCurrentThread()); |
| 38 std::pair<TaskObserverMap::iterator, bool> result = task_observer_map_.insert( |
| 39 std::make_pair(observer, static_cast<TaskObserverAdapter*>(NULL))); |
| 40 if (result.second) |
| 41 result.first->second = new TaskObserverAdapter(observer); |
| 42 MessageLoop::current()->AddTaskObserver(result.first->second); |
| 43 } |
| 44 |
| 45 void WebThreadBase::removeTaskObserver(TaskObserver* observer) { |
| 46 CHECK(IsCurrentThread()); |
| 47 TaskObserverMap::iterator iter = task_observer_map_.find(observer); |
| 48 if (iter == task_observer_map_.end()) |
| 49 return; |
| 50 MessageLoop::current()->RemoveTaskObserver(iter->second); |
| 51 delete iter->second; |
| 52 task_observer_map_.erase(iter); |
| 53 } |
| 54 |
16 WebThreadImpl::WebThreadImpl(const char* name) | 55 WebThreadImpl::WebThreadImpl(const char* name) |
17 : thread_(new base::Thread(name)) { | 56 : thread_(new base::Thread(name)) { |
18 thread_->Start(); | 57 thread_->Start(); |
19 } | 58 } |
20 | 59 |
21 void WebThreadImpl::postTask(Task* task) { | 60 void WebThreadImpl::postTask(Task* task) { |
22 thread_->message_loop()->PostTask( | 61 thread_->message_loop()->PostTask( |
23 FROM_HERE, base::Bind(&WebKit::WebThread::Task::run, base::Owned(task))); | 62 FROM_HERE, base::Bind(&WebKit::WebThread::Task::run, base::Owned(task))); |
24 } | 63 } |
| 64 |
25 void WebThreadImpl::postDelayedTask( | 65 void WebThreadImpl::postDelayedTask( |
26 Task* task, long long delay_ms) { | 66 Task* task, long long delay_ms) { |
27 thread_->message_loop()->PostDelayedTask( | 67 thread_->message_loop()->PostDelayedTask( |
28 FROM_HERE, | 68 FROM_HERE, |
29 base::Bind(&WebKit::WebThread::Task::run, base::Owned(task)), | 69 base::Bind(&WebKit::WebThread::Task::run, base::Owned(task)), |
30 delay_ms); | 70 delay_ms); |
31 } | 71 } |
32 | 72 |
| 73 bool WebThreadImpl::IsCurrentThread() const { |
| 74 return thread_->thread_id() == base::PlatformThread::CurrentId(); |
| 75 } |
| 76 |
33 WebThreadImpl::~WebThreadImpl() { | 77 WebThreadImpl::~WebThreadImpl() { |
34 thread_->Stop(); | 78 thread_->Stop(); |
35 } | 79 } |
36 | 80 |
37 WebThreadImplForMessageLoop::WebThreadImplForMessageLoop( | 81 WebThreadImplForMessageLoop::WebThreadImplForMessageLoop( |
38 base::MessageLoopProxy* message_loop) | 82 base::MessageLoopProxy* message_loop) |
39 : message_loop_(message_loop) { | 83 : message_loop_(message_loop) { |
40 } | 84 } |
41 | 85 |
42 void WebThreadImplForMessageLoop::postTask(Task* task) { | 86 void WebThreadImplForMessageLoop::postTask(Task* task) { |
43 message_loop_->PostTask( | 87 message_loop_->PostTask( |
44 FROM_HERE, base::Bind(&WebKit::WebThread::Task::run, base::Owned(task))); | 88 FROM_HERE, base::Bind(&WebKit::WebThread::Task::run, base::Owned(task))); |
45 } | 89 } |
46 | 90 |
47 void WebThreadImplForMessageLoop::postDelayedTask( | 91 void WebThreadImplForMessageLoop::postDelayedTask( |
48 Task* task, long long delay_ms) { | 92 Task* task, long long delay_ms) { |
49 message_loop_->PostDelayedTask( | 93 message_loop_->PostDelayedTask( |
50 FROM_HERE, | 94 FROM_HERE, |
51 base::Bind(&WebKit::WebThread::Task::run, base::Owned(task)), | 95 base::Bind(&WebKit::WebThread::Task::run, base::Owned(task)), |
52 delay_ms); | 96 delay_ms); |
53 } | 97 } |
54 | 98 |
| 99 bool WebThreadImplForMessageLoop::IsCurrentThread() const { |
| 100 return message_loop_->BelongsToCurrentThread(); |
| 101 } |
| 102 |
55 WebThreadImplForMessageLoop::~WebThreadImplForMessageLoop() { | 103 WebThreadImplForMessageLoop::~WebThreadImplForMessageLoop() { |
56 } | 104 } |
57 | 105 |
58 } | 106 } |
OLD | NEW |