OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "content/child/webthread_impl.h" | 8 #include "content/child/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/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
13 #include "base/pending_task.h" | 13 #include "base/pending_task.h" |
14 #include "base/threading/platform_thread.h" | 14 #include "base/threading/platform_thread.h" |
15 #include "third_party/WebKit/public/platform/WebTraceLocation.h" | |
15 | 16 |
16 namespace content { | 17 namespace content { |
17 | 18 |
18 WebThreadBase::WebThreadBase() {} | 19 WebThreadBase::WebThreadBase() {} |
19 WebThreadBase::~WebThreadBase() {} | 20 WebThreadBase::~WebThreadBase() {} |
20 | 21 |
21 class WebThreadBase::TaskObserverAdapter | 22 class WebThreadBase::TaskObserverAdapter |
22 : public base::MessageLoop::TaskObserver { | 23 : public base::MessageLoop::TaskObserver { |
23 public: | 24 public: |
24 TaskObserverAdapter(WebThread::TaskObserver* observer) | 25 TaskObserverAdapter(WebThread::TaskObserver* observer) |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
81 // When the closure never runs at all, the corresponding WebThread::Task is | 82 // When the closure never runs at all, the corresponding WebThread::Task is |
82 // destructed when base::Closure is deleted (like [B]). In this case, there | 83 // destructed when base::Closure is deleted (like [B]). In this case, there |
83 // are no reference counter modification like [A] (because task->run() is not | 84 // are no reference counter modification like [A] (because task->run() is not |
84 // executed), so there are no race conditions. | 85 // executed), so there are no race conditions. |
85 // See https://crbug.com/390851 for more details. | 86 // See https://crbug.com/390851 for more details. |
86 static void RunWebThreadTask(scoped_ptr<blink::WebThread::Task> task) { | 87 static void RunWebThreadTask(scoped_ptr<blink::WebThread::Task> task) { |
87 task->run(); | 88 task->run(); |
88 } | 89 } |
89 | 90 |
90 void WebThreadImpl::postTask(Task* task) { | 91 void WebThreadImpl::postTask(Task* task) { |
91 thread_->message_loop()->PostTask( | 92 postDelayedTask(task, 0); |
92 FROM_HERE, | 93 } |
93 base::Bind(RunWebThreadTask, base::Passed(scoped_ptr<Task>(task)))); | 94 |
95 void WebThreadImpl::postTask(const blink::WebTraceLocation& location, | |
96 Task* task) { | |
97 postDelayedTask(location, task, 0); | |
94 } | 98 } |
95 | 99 |
96 void WebThreadImpl::postDelayedTask(Task* task, long long delay_ms) { | 100 void WebThreadImpl::postDelayedTask(Task* task, long long delay_ms) { |
97 thread_->message_loop()->PostDelayedTask( | 101 thread_->message_loop()->PostDelayedTask( |
98 FROM_HERE, | 102 FROM_HERE, |
99 base::Bind(RunWebThreadTask, base::Passed(scoped_ptr<Task>(task))), | 103 base::Bind(RunWebThreadTask, base::Passed(scoped_ptr<Task>(task))), |
100 base::TimeDelta::FromMilliseconds(delay_ms)); | 104 base::TimeDelta::FromMilliseconds(delay_ms)); |
101 } | 105 } |
102 | 106 |
107 void WebThreadImpl::postDelayedTask(const blink::WebTraceLocation& web_location, | |
108 Task* task, | |
109 long long delay_ms) { | |
110 tracked_objects::Location location(web_location.functionName(), | |
111 web_location.fileName(), -1, nullptr); | |
112 thread_->message_loop()->PostDelayedTask( | |
113 location, | |
114 base::Bind(RunWebThreadTask, base::Passed(scoped_ptr<Task>(task))), | |
no sievers
2015/01/23 20:39:47
nit: make_scoped_ptr
Sami
2015/01/26 13:41:17
Thanks, fixed.
| |
115 base::TimeDelta::FromMilliseconds(delay_ms)); | |
116 } | |
117 | |
103 void WebThreadImpl::enterRunLoop() { | 118 void WebThreadImpl::enterRunLoop() { |
104 CHECK(isCurrentThread()); | 119 CHECK(isCurrentThread()); |
105 CHECK(!thread_->message_loop()->is_running()); // We don't support nesting. | 120 CHECK(!thread_->message_loop()->is_running()); // We don't support nesting. |
106 thread_->message_loop()->Run(); | 121 thread_->message_loop()->Run(); |
107 } | 122 } |
108 | 123 |
109 void WebThreadImpl::exitRunLoop() { | 124 void WebThreadImpl::exitRunLoop() { |
110 CHECK(isCurrentThread()); | 125 CHECK(isCurrentThread()); |
111 CHECK(thread_->message_loop()->is_running()); | 126 CHECK(thread_->message_loop()->is_running()); |
112 thread_->message_loop()->Quit(); | 127 thread_->message_loop()->Quit(); |
(...skipping 10 matching lines...) Expand all Loading... | |
123 WebThreadImpl::~WebThreadImpl() { | 138 WebThreadImpl::~WebThreadImpl() { |
124 thread_->Stop(); | 139 thread_->Stop(); |
125 } | 140 } |
126 | 141 |
127 WebThreadImplForMessageLoop::WebThreadImplForMessageLoop( | 142 WebThreadImplForMessageLoop::WebThreadImplForMessageLoop( |
128 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner) | 143 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner) |
129 : main_thread_task_runner_(main_thread_task_runner), | 144 : main_thread_task_runner_(main_thread_task_runner), |
130 thread_id_(base::PlatformThread::CurrentId()) {} | 145 thread_id_(base::PlatformThread::CurrentId()) {} |
131 | 146 |
132 void WebThreadImplForMessageLoop::postTask(Task* task) { | 147 void WebThreadImplForMessageLoop::postTask(Task* task) { |
133 main_thread_task_runner_->PostTask( | 148 postDelayedTask(task, 0); |
134 FROM_HERE, | 149 } |
135 base::Bind(RunWebThreadTask, base::Passed(make_scoped_ptr(task)))); | 150 |
151 void WebThreadImplForMessageLoop::postTask( | |
152 const blink::WebTraceLocation& location, | |
153 Task* task) { | |
154 postDelayedTask(location, task, 0); | |
136 } | 155 } |
137 | 156 |
138 void WebThreadImplForMessageLoop::postDelayedTask(Task* task, | 157 void WebThreadImplForMessageLoop::postDelayedTask(Task* task, |
139 long long delay_ms) { | 158 long long delay_ms) { |
140 main_thread_task_runner_->PostDelayedTask( | 159 main_thread_task_runner_->PostDelayedTask( |
141 FROM_HERE, | 160 FROM_HERE, |
142 base::Bind(RunWebThreadTask, base::Passed(make_scoped_ptr(task))), | 161 base::Bind(RunWebThreadTask, base::Passed(make_scoped_ptr(task))), |
143 base::TimeDelta::FromMilliseconds(delay_ms)); | 162 base::TimeDelta::FromMilliseconds(delay_ms)); |
144 } | 163 } |
145 | 164 |
165 void WebThreadImplForMessageLoop::postDelayedTask( | |
166 const blink::WebTraceLocation& web_location, | |
167 Task* task, | |
168 long long delay_ms) { | |
169 tracked_objects::Location location(web_location.functionName(), | |
170 web_location.fileName(), -1, nullptr); | |
171 main_thread_task_runner_->PostDelayedTask( | |
172 location, | |
173 base::Bind(RunWebThreadTask, base::Passed(make_scoped_ptr(task))), | |
174 base::TimeDelta::FromMilliseconds(delay_ms)); | |
175 } | |
176 | |
146 void WebThreadImplForMessageLoop::enterRunLoop() { | 177 void WebThreadImplForMessageLoop::enterRunLoop() { |
147 CHECK(isCurrentThread()); | 178 CHECK(isCurrentThread()); |
148 // We don't support nesting. | 179 // We don't support nesting. |
149 CHECK(!base::MessageLoop::current()->is_running()); | 180 CHECK(!base::MessageLoop::current()->is_running()); |
150 base::MessageLoop::current()->Run(); | 181 base::MessageLoop::current()->Run(); |
151 } | 182 } |
152 | 183 |
153 void WebThreadImplForMessageLoop::exitRunLoop() { | 184 void WebThreadImplForMessageLoop::exitRunLoop() { |
154 CHECK(isCurrentThread()); | 185 CHECK(isCurrentThread()); |
155 CHECK(base::MessageLoop::current()->is_running()); | 186 CHECK(base::MessageLoop::current()->is_running()); |
156 base::MessageLoop::current()->Quit(); | 187 base::MessageLoop::current()->Quit(); |
157 } | 188 } |
158 | 189 |
159 bool WebThreadImplForMessageLoop::isCurrentThread() const { | 190 bool WebThreadImplForMessageLoop::isCurrentThread() const { |
160 return main_thread_task_runner_->BelongsToCurrentThread(); | 191 return main_thread_task_runner_->BelongsToCurrentThread(); |
161 } | 192 } |
162 | 193 |
163 blink::PlatformThreadId WebThreadImplForMessageLoop::threadId() const { | 194 blink::PlatformThreadId WebThreadImplForMessageLoop::threadId() const { |
164 return thread_id_; | 195 return thread_id_; |
165 } | 196 } |
166 | 197 |
167 WebThreadImplForMessageLoop::~WebThreadImplForMessageLoop() {} | 198 WebThreadImplForMessageLoop::~WebThreadImplForMessageLoop() {} |
168 | 199 |
169 } // namespace content | 200 } // namespace content |
OLD | NEW |