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" | |
14 | 13 |
15 namespace webkit_glue { | 14 namespace webkit_glue { |
16 | 15 |
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 | |
52 WebThreadImpl::WebThreadImpl(const char* name) | 16 WebThreadImpl::WebThreadImpl(const char* name) |
53 : thread_(new base::Thread(name)) { | 17 : thread_(new base::Thread(name)) { |
54 thread_->Start(); | 18 thread_->Start(); |
55 } | 19 } |
56 | 20 |
57 void WebThreadImpl::postTask(Task* task) { | 21 void WebThreadImpl::postTask(Task* task) { |
58 thread_->message_loop()->PostTask( | 22 thread_->message_loop()->PostTask( |
59 FROM_HERE, base::Bind(&WebKit::WebThread::Task::run, base::Owned(task))); | 23 FROM_HERE, base::Bind(&WebKit::WebThread::Task::run, base::Owned(task))); |
60 } | 24 } |
61 | |
62 void WebThreadImpl::postDelayedTask( | 25 void WebThreadImpl::postDelayedTask( |
63 Task* task, long long delay_ms) { | 26 Task* task, long long delay_ms) { |
64 thread_->message_loop()->PostDelayedTask( | 27 thread_->message_loop()->PostDelayedTask( |
65 FROM_HERE, | 28 FROM_HERE, |
66 base::Bind(&WebKit::WebThread::Task::run, base::Owned(task)), | 29 base::Bind(&WebKit::WebThread::Task::run, base::Owned(task)), |
67 delay_ms); | 30 delay_ms); |
68 } | 31 } |
69 | 32 |
70 bool WebThreadImpl::IsCurrentThread() const { | |
71 return thread_->thread_id() == base::PlatformThread::CurrentId(); | |
72 } | |
73 | |
74 WebThreadImpl::~WebThreadImpl() { | 33 WebThreadImpl::~WebThreadImpl() { |
75 thread_->Stop(); | 34 thread_->Stop(); |
76 } | 35 } |
77 | 36 |
78 WebThreadImplForMessageLoop::WebThreadImplForMessageLoop( | 37 WebThreadImplForMessageLoop::WebThreadImplForMessageLoop( |
79 base::MessageLoopProxy* message_loop) | 38 base::MessageLoopProxy* message_loop) |
80 : message_loop_(message_loop) { | 39 : message_loop_(message_loop) { |
81 } | 40 } |
82 | 41 |
83 void WebThreadImplForMessageLoop::postTask(Task* task) { | 42 void WebThreadImplForMessageLoop::postTask(Task* task) { |
84 message_loop_->PostTask( | 43 message_loop_->PostTask( |
85 FROM_HERE, base::Bind(&WebKit::WebThread::Task::run, base::Owned(task))); | 44 FROM_HERE, base::Bind(&WebKit::WebThread::Task::run, base::Owned(task))); |
86 } | 45 } |
87 | 46 |
88 void WebThreadImplForMessageLoop::postDelayedTask( | 47 void WebThreadImplForMessageLoop::postDelayedTask( |
89 Task* task, long long delay_ms) { | 48 Task* task, long long delay_ms) { |
90 message_loop_->PostDelayedTask( | 49 message_loop_->PostDelayedTask( |
91 FROM_HERE, | 50 FROM_HERE, |
92 base::Bind(&WebKit::WebThread::Task::run, base::Owned(task)), | 51 base::Bind(&WebKit::WebThread::Task::run, base::Owned(task)), |
93 delay_ms); | 52 delay_ms); |
94 } | 53 } |
95 | 54 |
96 bool WebThreadImplForMessageLoop::IsCurrentThread() const { | |
97 return message_loop_->BelongsToCurrentThread(); | |
98 } | |
99 | |
100 WebThreadImplForMessageLoop::~WebThreadImplForMessageLoop() { | 55 WebThreadImplForMessageLoop::~WebThreadImplForMessageLoop() { |
101 } | 56 } |
102 | 57 |
103 } | 58 } |
OLD | NEW |