OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 #include "base/message_loop/message_loop.h" | 5 #include "base/message_loop/message_loop.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 | 163 |
164 // Let interested parties have one last shot at accessing this. | 164 // Let interested parties have one last shot at accessing this. |
165 FOR_EACH_OBSERVER(DestructionObserver, destruction_observers_, | 165 FOR_EACH_OBSERVER(DestructionObserver, destruction_observers_, |
166 WillDestroyCurrentMessageLoop()); | 166 WillDestroyCurrentMessageLoop()); |
167 | 167 |
168 thread_task_runner_handle_.reset(); | 168 thread_task_runner_handle_.reset(); |
169 | 169 |
170 // Tell the incoming queue that we are dying. | 170 // Tell the incoming queue that we are dying. |
171 incoming_task_queue_->WillDestroyCurrentMessageLoop(); | 171 incoming_task_queue_->WillDestroyCurrentMessageLoop(); |
172 incoming_task_queue_ = NULL; | 172 incoming_task_queue_ = NULL; |
173 message_loop_proxy_ = NULL; | 173 task_runner_ = NULL; |
174 | 174 |
175 // OK, now make it so that no one can find us. | 175 // OK, now make it so that no one can find us. |
176 lazy_tls_ptr.Pointer()->Set(NULL); | 176 lazy_tls_ptr.Pointer()->Set(NULL); |
177 } | 177 } |
178 | 178 |
179 // static | 179 // static |
180 MessageLoop* MessageLoop::current() { | 180 MessageLoop* MessageLoop::current() { |
181 // TODO(darin): sadly, we cannot enable this yet since people call us even | 181 // TODO(darin): sadly, we cannot enable this yet since people call us even |
182 // when they have no intention of using us. | 182 // when they have no intention of using us. |
183 // DCHECK(loop) << "Ouch, did you forget to initialize me?"; | 183 // DCHECK(loop) << "Ouch, did you forget to initialize me?"; |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 | 250 |
251 void MessageLoop::RemoveDestructionObserver( | 251 void MessageLoop::RemoveDestructionObserver( |
252 DestructionObserver* destruction_observer) { | 252 DestructionObserver* destruction_observer) { |
253 DCHECK_EQ(this, current()); | 253 DCHECK_EQ(this, current()); |
254 destruction_observers_.RemoveObserver(destruction_observer); | 254 destruction_observers_.RemoveObserver(destruction_observer); |
255 } | 255 } |
256 | 256 |
257 void MessageLoop::PostTask( | 257 void MessageLoop::PostTask( |
258 const tracked_objects::Location& from_here, | 258 const tracked_objects::Location& from_here, |
259 const Closure& task) { | 259 const Closure& task) { |
260 message_loop_proxy_->PostTask(from_here, task); | 260 task_runner_->PostTask(from_here, task); |
261 } | 261 } |
262 | 262 |
263 void MessageLoop::PostDelayedTask( | 263 void MessageLoop::PostDelayedTask( |
264 const tracked_objects::Location& from_here, | 264 const tracked_objects::Location& from_here, |
265 const Closure& task, | 265 const Closure& task, |
266 TimeDelta delay) { | 266 TimeDelta delay) { |
267 message_loop_proxy_->PostDelayedTask(from_here, task, delay); | 267 task_runner_->PostDelayedTask(from_here, task, delay); |
268 } | 268 } |
269 | 269 |
270 void MessageLoop::PostNonNestableTask( | 270 void MessageLoop::PostNonNestableTask( |
271 const tracked_objects::Location& from_here, | 271 const tracked_objects::Location& from_here, |
272 const Closure& task) { | 272 const Closure& task) { |
273 message_loop_proxy_->PostNonNestableTask(from_here, task); | 273 task_runner_->PostNonNestableTask(from_here, task); |
274 } | 274 } |
275 | 275 |
276 void MessageLoop::PostNonNestableDelayedTask( | 276 void MessageLoop::PostNonNestableDelayedTask( |
277 const tracked_objects::Location& from_here, | 277 const tracked_objects::Location& from_here, |
278 const Closure& task, | 278 const Closure& task, |
279 TimeDelta delay) { | 279 TimeDelta delay) { |
280 message_loop_proxy_->PostNonNestableDelayedTask(from_here, task, delay); | 280 task_runner_->PostNonNestableDelayedTask(from_here, task, delay); |
281 } | 281 } |
282 | 282 |
283 void MessageLoop::Run() { | 283 void MessageLoop::Run() { |
284 DCHECK(pump_); | 284 DCHECK(pump_); |
285 RunLoop run_loop; | 285 RunLoop run_loop; |
286 run_loop.Run(); | 286 run_loop.Run(); |
287 } | 287 } |
288 | 288 |
289 void MessageLoop::RunUntilIdle() { | 289 void MessageLoop::RunUntilIdle() { |
290 DCHECK(pump_); | 290 DCHECK(pump_); |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
379 in_high_res_mode_(false), | 379 in_high_res_mode_(false), |
380 #endif | 380 #endif |
381 nestable_tasks_allowed_(true), | 381 nestable_tasks_allowed_(true), |
382 #if defined(OS_WIN) | 382 #if defined(OS_WIN) |
383 os_modal_loop_(false), | 383 os_modal_loop_(false), |
384 #endif // OS_WIN | 384 #endif // OS_WIN |
385 pump_factory_(pump_factory), | 385 pump_factory_(pump_factory), |
386 message_histogram_(NULL), | 386 message_histogram_(NULL), |
387 run_loop_(NULL), | 387 run_loop_(NULL), |
388 incoming_task_queue_(new internal::IncomingTaskQueue(this)), | 388 incoming_task_queue_(new internal::IncomingTaskQueue(this)), |
389 message_loop_proxy_( | 389 task_runner_(new internal::MessageLoopTaskRunner(incoming_task_queue_)) { |
390 new internal::MessageLoopProxyImpl(incoming_task_queue_)) { | |
391 // If type is TYPE_CUSTOM non-null pump_factory must be given. | 390 // If type is TYPE_CUSTOM non-null pump_factory must be given. |
392 DCHECK_EQ(type_ == TYPE_CUSTOM, !pump_factory_.is_null()); | 391 DCHECK_EQ(type_ == TYPE_CUSTOM, !pump_factory_.is_null()); |
393 } | 392 } |
394 | 393 |
395 void MessageLoop::BindToCurrentThread() { | 394 void MessageLoop::BindToCurrentThread() { |
396 DCHECK(!pump_); | 395 DCHECK(!pump_); |
397 if (!pump_factory_.is_null()) | 396 if (!pump_factory_.is_null()) |
398 pump_ = pump_factory_.Run(); | 397 pump_ = pump_factory_.Run(); |
399 else | 398 else |
400 pump_ = CreateMessagePumpForType(type_); | 399 pump_ = CreateMessagePumpForType(type_); |
401 | 400 |
402 DCHECK(!current()) << "should only have one message loop per thread"; | 401 DCHECK(!current()) << "should only have one message loop per thread"; |
403 lazy_tls_ptr.Pointer()->Set(this); | 402 lazy_tls_ptr.Pointer()->Set(this); |
404 | 403 |
405 incoming_task_queue_->StartScheduling(); | 404 incoming_task_queue_->StartScheduling(); |
406 message_loop_proxy_->BindToCurrentThread(); | 405 task_runner_->BindToCurrentThread(); |
407 thread_task_runner_handle_.reset( | 406 thread_task_runner_handle_.reset(new ThreadTaskRunnerHandle(task_runner_)); |
408 new ThreadTaskRunnerHandle(message_loop_proxy_)); | |
409 } | 407 } |
410 | 408 |
411 void MessageLoop::RunHandler() { | 409 void MessageLoop::RunHandler() { |
412 DCHECK_EQ(this, current()); | 410 DCHECK_EQ(this, current()); |
413 | 411 |
414 StartHistogrammer(); | 412 StartHistogrammer(); |
415 | 413 |
416 #if defined(OS_WIN) | 414 #if defined(OS_WIN) |
417 if (run_loop_->dispatcher_ && type() == TYPE_UI) { | 415 if (run_loop_->dispatcher_ && type() == TYPE_UI) { |
418 static_cast<MessagePumpForUI*>(pump_.get())-> | 416 static_cast<MessagePumpForUI*>(pump_.get())-> |
(...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
729 persistent, | 727 persistent, |
730 mode, | 728 mode, |
731 controller, | 729 controller, |
732 delegate); | 730 delegate); |
733 } | 731 } |
734 #endif | 732 #endif |
735 | 733 |
736 #endif // !defined(OS_NACL_SFI) | 734 #endif // !defined(OS_NACL_SFI) |
737 | 735 |
738 } // namespace base | 736 } // namespace base |
OLD | NEW |