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 #ifndef BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_ | 5 #ifndef BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_ |
6 #define BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_ | 6 #define BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_ |
7 | 7 |
8 #include <queue> | 8 #include <queue> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/base_export.h" | 11 #include "base/base_export.h" |
12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
13 #include "base/callback_forward.h" | 13 #include "base/callback_forward.h" |
14 #include "base/debug/task_annotator.h" | |
15 #include "base/location.h" | 14 #include "base/location.h" |
16 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
17 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
18 #include "base/message_loop/incoming_task_queue.h" | 17 #include "base/message_loop/incoming_task_queue.h" |
19 #include "base/message_loop/message_loop_proxy.h" | 18 #include "base/message_loop/message_loop_proxy.h" |
20 #include "base/message_loop/message_loop_proxy_impl.h" | 19 #include "base/message_loop/message_loop_proxy_impl.h" |
21 #include "base/message_loop/message_pump.h" | 20 #include "base/message_loop/message_pump.h" |
22 #include "base/message_loop/timer_slack.h" | 21 #include "base/message_loop/timer_slack.h" |
23 #include "base/observer_list.h" | 22 #include "base/observer_list.h" |
24 #include "base/pending_task.h" | 23 #include "base/pending_task.h" |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 enum Type { | 101 enum Type { |
103 TYPE_DEFAULT, | 102 TYPE_DEFAULT, |
104 TYPE_UI, | 103 TYPE_UI, |
105 TYPE_CUSTOM, | 104 TYPE_CUSTOM, |
106 TYPE_IO, | 105 TYPE_IO, |
107 #if defined(OS_ANDROID) | 106 #if defined(OS_ANDROID) |
108 TYPE_JAVA, | 107 TYPE_JAVA, |
109 #endif // defined(OS_ANDROID) | 108 #endif // defined(OS_ANDROID) |
110 }; | 109 }; |
111 | 110 |
| 111 struct BASE_EXPORT Options { |
| 112 using MessagePumpFactory = Callback<scoped_ptr<MessagePump>()>; |
| 113 Options(); |
| 114 ~Options(); |
| 115 |
| 116 // Specify the type of message loop. |
| 117 // This is ignored if message_pump_factory.is_null() is false. |
| 118 MessageLoop::Type message_loop_type; |
| 119 |
| 120 // Used to create the MessagePump for the MessageLoop. |
| 121 // If message_pump_factory.is_null() is true, then a MessagePump appropriate |
| 122 // for |message_loop_type| is created. Setting this forces the |
| 123 // MessageLoop::Type to TYPE_CUSTOM. |
| 124 MessagePumpFactory message_pump_factory; |
| 125 |
| 126 // If non-null incoming_task_queue is specified this message loop will |
| 127 // use it as its incoming task queue. |
| 128 scoped_refptr<internal::IncomingTaskQueue> incoming_task_queue; |
| 129 }; |
| 130 |
112 // Normally, it is not necessary to instantiate a MessageLoop. Instead, it | 131 // Normally, it is not necessary to instantiate a MessageLoop. Instead, it |
113 // is typical to make use of the current thread's MessageLoop instance. | 132 // is typical to make use of the current thread's MessageLoop instance. |
| 133 explicit MessageLoop(const Options& options); |
| 134 |
| 135 // Creates a MessageLoop for the supplied type. This constructor is used |
| 136 // only for testing. |
114 explicit MessageLoop(Type type = TYPE_DEFAULT); | 137 explicit MessageLoop(Type type = TYPE_DEFAULT); |
115 // Creates a TYPE_CUSTOM MessageLoop with the supplied MessagePump, which must | 138 // Creates a TYPE_CUSTOM MessageLoop with the supplied MessagePump, which must |
116 // be non-NULL. | 139 // be non-NULL. This constructor is used only for testing. |
117 explicit MessageLoop(scoped_ptr<base::MessagePump> pump); | 140 explicit MessageLoop(scoped_ptr<MessagePump> pump); |
118 ~MessageLoop() override; | 141 ~MessageLoop() override; |
119 | 142 |
120 // Returns the MessageLoop object for the current thread, or null if none. | 143 // Returns the MessageLoop object for the current thread, or null if none. |
121 static MessageLoop* current(); | 144 static MessageLoop* current(); |
122 | 145 |
123 static void EnableHistogrammer(bool enable_histogrammer); | 146 static void EnableHistogrammer(bool enable_histogrammer); |
124 | 147 |
125 typedef scoped_ptr<MessagePump> (MessagePumpFactory)(); | 148 typedef scoped_ptr<MessagePump> (MessagePumpFactory)(); |
126 // Uses the given base::MessagePumpForUIFactory to override the default | 149 // Uses the given base::MessagePumpForUIFactory to override the default |
127 // MessagePump implementation for 'TYPE_UI'. Returns true if the factory | 150 // MessagePump implementation for 'TYPE_UI'. Returns true if the factory |
(...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
393 | 416 |
394 // Returns true if the message loop is "idle". Provided for testing. | 417 // Returns true if the message loop is "idle". Provided for testing. |
395 bool IsIdleForTesting(); | 418 bool IsIdleForTesting(); |
396 | 419 |
397 // Wakes up the message pump. Can be called on any thread. The caller is | 420 // Wakes up the message pump. Can be called on any thread. The caller is |
398 // responsible for synchronizing ScheduleWork() calls. | 421 // responsible for synchronizing ScheduleWork() calls. |
399 void ScheduleWork(); | 422 void ScheduleWork(); |
400 | 423 |
401 // Returns the TaskAnnotator which is used to add debug information to posted | 424 // Returns the TaskAnnotator which is used to add debug information to posted |
402 // tasks. | 425 // tasks. |
403 debug::TaskAnnotator* task_annotator() { return &task_annotator_; } | 426 debug::TaskAnnotator* task_annotator() { |
| 427 return incoming_task_queue_->task_annotator(); |
| 428 } |
404 | 429 |
405 // Runs the specified PendingTask. | 430 // Runs the specified PendingTask. |
406 void RunTask(const PendingTask& pending_task); | 431 void RunTask(const PendingTask& pending_task); |
407 | 432 |
408 //---------------------------------------------------------------------------- | 433 //---------------------------------------------------------------------------- |
409 protected: | 434 protected: |
410 scoped_ptr<MessagePump> pump_; | 435 scoped_ptr<MessagePump> pump_; |
411 | 436 |
412 private: | 437 private: |
413 friend class RunLoop; | 438 friend class RunLoop; |
414 | 439 |
415 // Configures various members for the two constructors. | 440 // Configures various members for the constructors. |
416 void Init(); | 441 void Init(); |
417 | 442 |
418 // Invokes the actual run loop using the message pump. | 443 // Invokes the actual run loop using the message pump. |
419 void RunHandler(); | 444 void RunHandler(); |
420 | 445 |
421 // Called to process any delayed non-nestable tasks. | 446 // Called to process any delayed non-nestable tasks. |
422 bool ProcessNextDelayedNonNestableTask(); | 447 bool ProcessNextDelayedNonNestableTask(); |
423 | 448 |
424 // Calls RunTask or queues the pending_task on the deferred task list if it | 449 // Calls RunTask or queues the pending_task on the deferred task list if it |
425 // cannot be run right now. Returns true if the task was run. | 450 // cannot be run right now. Returns true if the task was run. |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
491 #endif | 516 #endif |
492 | 517 |
493 std::string thread_name_; | 518 std::string thread_name_; |
494 // A profiling histogram showing the counts of various messages and events. | 519 // A profiling histogram showing the counts of various messages and events. |
495 HistogramBase* message_histogram_; | 520 HistogramBase* message_histogram_; |
496 | 521 |
497 RunLoop* run_loop_; | 522 RunLoop* run_loop_; |
498 | 523 |
499 ObserverList<TaskObserver> task_observers_; | 524 ObserverList<TaskObserver> task_observers_; |
500 | 525 |
501 debug::TaskAnnotator task_annotator_; | |
502 | |
503 scoped_refptr<internal::IncomingTaskQueue> incoming_task_queue_; | 526 scoped_refptr<internal::IncomingTaskQueue> incoming_task_queue_; |
504 | 527 |
505 // The message loop proxy associated with this message loop. | 528 // The message loop proxy associated with this message loop. |
506 scoped_refptr<internal::MessageLoopProxyImpl> message_loop_proxy_; | 529 scoped_refptr<internal::MessageLoopProxyImpl> message_loop_proxy_; |
507 scoped_ptr<ThreadTaskRunnerHandle> thread_task_runner_handle_; | 530 scoped_ptr<ThreadTaskRunnerHandle> thread_task_runner_handle_; |
508 | 531 |
509 template <class T, class R> friend class base::subtle::DeleteHelperInternal; | 532 template <class T, class R> friend class base::subtle::DeleteHelperInternal; |
510 template <class T, class R> friend class base::subtle::ReleaseHelperInternal; | 533 template <class T, class R> friend class base::subtle::ReleaseHelperInternal; |
511 | 534 |
512 void DeleteSoonInternal(const tracked_objects::Location& from_here, | 535 void DeleteSoonInternal(const tracked_objects::Location& from_here, |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
654 | 677 |
655 // Do not add any member variables to MessageLoopForIO! This is important b/c | 678 // Do not add any member variables to MessageLoopForIO! This is important b/c |
656 // MessageLoopForIO is often allocated via MessageLoop(TYPE_IO). Any extra | 679 // MessageLoopForIO is often allocated via MessageLoop(TYPE_IO). Any extra |
657 // data that you need should be stored on the MessageLoop's pump_ instance. | 680 // data that you need should be stored on the MessageLoop's pump_ instance. |
658 COMPILE_ASSERT(sizeof(MessageLoop) == sizeof(MessageLoopForIO), | 681 COMPILE_ASSERT(sizeof(MessageLoop) == sizeof(MessageLoopForIO), |
659 MessageLoopForIO_should_not_have_extra_member_variables); | 682 MessageLoopForIO_should_not_have_extra_member_variables); |
660 | 683 |
661 } // namespace base | 684 } // namespace base |
662 | 685 |
663 #endif // BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_ | 686 #endif // BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_ |
OLD | NEW |