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 class WebThreadBase::TaskObserverAdapter : public MessageLoop::TaskObserver { | |
18 public: | |
19 TaskObserverAdapter(WebThread::TaskObserver* observer) | |
20 : observer_(observer) { } | |
21 | |
22 // WebThread::TaskObserver does not have a willProcessTask method. | |
23 virtual void WillProcessTask(base::TimeTicks) OVERRIDE { } | |
24 | |
25 virtual void DidProcessTask(base::TimeTicks) OVERRIDE { | |
26 observer_->didProcessTask(); | |
27 } | |
28 | |
29 private: | |
30 WebThread::TaskObserver* observer_; | |
31 }; | |
32 | |
33 void WebThreadBase::addTaskObserver(TaskObserver* observer) { | |
34 CHECK(IsCurrentThread()); | |
35 std::pair<TaskObserverMap::iterator, bool> result = task_observer_map_.insert( | |
36 std::make_pair(observer, static_cast<TaskObserverAdapter*>(NULL))); | |
37 if (result.second) | |
38 result.first->second = new TaskObserverAdapter(observer); | |
39 MessageLoop::current()->AddTaskObserver(result.first->second); | |
40 } | |
41 | |
42 void WebThreadBase::removeTaskObserver(TaskObserver* observer) { | |
43 CHECK(IsCurrentThread()); | |
44 TaskObserverMap::iterator iter = task_observer_map_.find(observer); | |
45 if (iter == task_observer_map_.end()) | |
46 return; | |
47 MessageLoop::current()->RemoveTaskObserver(iter->second); | |
48 delete iter->second; | |
49 task_observer_map_.erase(iter); | |
50 } | |
51 | |
16 WebThreadImpl::WebThreadImpl(const char* name) | 52 WebThreadImpl::WebThreadImpl(const char* name) |
17 : thread_(new base::Thread(name)) { | 53 : thread_(new base::Thread(name)) { |
18 thread_->Start(); | 54 thread_->Start(); |
19 } | 55 } |
20 | 56 |
21 void WebThreadImpl::postTask(Task* task) { | 57 void WebThreadImpl::postTask(Task* task) { |
22 thread_->message_loop()->PostTask( | 58 thread_->message_loop()->PostTask( |
23 FROM_HERE, base::Bind(&WebKit::WebThread::Task::run, base::Owned(task))); | 59 FROM_HERE, base::Bind(&WebKit::WebThread::Task::run, base::Owned(task))); |
24 } | 60 } |
61 | |
25 void WebThreadImpl::postDelayedTask( | 62 void WebThreadImpl::postDelayedTask( |
26 Task* task, long long delay_ms) { | 63 Task* task, long long delay_ms) { |
27 thread_->message_loop()->PostDelayedTask( | 64 thread_->message_loop()->PostDelayedTask( |
28 FROM_HERE, | 65 FROM_HERE, |
29 base::Bind(&WebKit::WebThread::Task::run, base::Owned(task)), | 66 base::Bind(&WebKit::WebThread::Task::run, base::Owned(task)), |
30 delay_ms); | 67 delay_ms); |
31 } | 68 } |
32 | 69 |
70 bool WebThreadImpl::IsCurrentThread() const { | |
71 return thread_->thread_id() == base::PlatformThread::CurrentId(); | |
darin (slow to review)
2011/11/30 06:45:39
it feels like this should be a method on the Threa
adamk
2011/11/30 21:58:25
Yeah...I think this doesn't come up too often beca
| |
72 } | |
73 | |
33 WebThreadImpl::~WebThreadImpl() { | 74 WebThreadImpl::~WebThreadImpl() { |
34 thread_->Stop(); | 75 thread_->Stop(); |
35 } | 76 } |
36 | 77 |
37 WebThreadImplForMessageLoop::WebThreadImplForMessageLoop( | 78 WebThreadImplForMessageLoop::WebThreadImplForMessageLoop( |
38 base::MessageLoopProxy* message_loop) | 79 base::MessageLoopProxy* message_loop) |
39 : message_loop_(message_loop) { | 80 : message_loop_(message_loop) { |
40 } | 81 } |
41 | 82 |
42 void WebThreadImplForMessageLoop::postTask(Task* task) { | 83 void WebThreadImplForMessageLoop::postTask(Task* task) { |
43 message_loop_->PostTask( | 84 message_loop_->PostTask( |
44 FROM_HERE, base::Bind(&WebKit::WebThread::Task::run, base::Owned(task))); | 85 FROM_HERE, base::Bind(&WebKit::WebThread::Task::run, base::Owned(task))); |
45 } | 86 } |
46 | 87 |
47 void WebThreadImplForMessageLoop::postDelayedTask( | 88 void WebThreadImplForMessageLoop::postDelayedTask( |
48 Task* task, long long delay_ms) { | 89 Task* task, long long delay_ms) { |
49 message_loop_->PostDelayedTask( | 90 message_loop_->PostDelayedTask( |
50 FROM_HERE, | 91 FROM_HERE, |
51 base::Bind(&WebKit::WebThread::Task::run, base::Owned(task)), | 92 base::Bind(&WebKit::WebThread::Task::run, base::Owned(task)), |
52 delay_ms); | 93 delay_ms); |
53 } | 94 } |
54 | 95 |
96 bool WebThreadImplForMessageLoop::IsCurrentThread() const { | |
97 return message_loop_->BelongsToCurrentThread(); | |
98 } | |
99 | |
55 WebThreadImplForMessageLoop::~WebThreadImplForMessageLoop() { | 100 WebThreadImplForMessageLoop::~WebThreadImplForMessageLoop() { |
56 } | 101 } |
57 | 102 |
58 } | 103 } |
OLD | NEW |