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 |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 #if defined(OS_ANDROID) | 107 #if defined(OS_ANDROID) |
108 TYPE_JAVA, | 108 TYPE_JAVA, |
109 #endif // defined(OS_ANDROID) | 109 #endif // defined(OS_ANDROID) |
110 }; | 110 }; |
111 | 111 |
112 // Normally, it is not necessary to instantiate a MessageLoop. Instead, it | 112 // Normally, it is not necessary to instantiate a MessageLoop. Instead, it |
113 // is typical to make use of the current thread's MessageLoop instance. | 113 // is typical to make use of the current thread's MessageLoop instance. |
114 explicit MessageLoop(Type type = TYPE_DEFAULT); | 114 explicit MessageLoop(Type type = TYPE_DEFAULT); |
115 // Creates a TYPE_CUSTOM MessageLoop with the supplied MessagePump, which must | 115 // Creates a TYPE_CUSTOM MessageLoop with the supplied MessagePump, which must |
116 // be non-NULL. | 116 // be non-NULL. |
117 explicit MessageLoop(scoped_ptr<base::MessagePump> pump); | 117 explicit MessageLoop(scoped_ptr<MessagePump> pump); |
| 118 |
118 ~MessageLoop() override; | 119 ~MessageLoop() override; |
119 | 120 |
120 // Returns the MessageLoop object for the current thread, or null if none. | 121 // Returns the MessageLoop object for the current thread, or null if none. |
121 static MessageLoop* current(); | 122 static MessageLoop* current(); |
122 | 123 |
123 static void EnableHistogrammer(bool enable_histogrammer); | 124 static void EnableHistogrammer(bool enable_histogrammer); |
124 | 125 |
125 typedef scoped_ptr<MessagePump> (MessagePumpFactory)(); | 126 typedef scoped_ptr<MessagePump> (MessagePumpFactory)(); |
126 // Uses the given base::MessagePumpForUIFactory to override the default | 127 // Uses the given base::MessagePumpForUIFactory to override the default |
127 // MessagePump implementation for 'TYPE_UI'. Returns true if the factory | 128 // MessagePump implementation for 'TYPE_UI'. Returns true if the factory |
(...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
387 // Can only be called from the thread that owns the MessageLoop. | 388 // Can only be called from the thread that owns the MessageLoop. |
388 bool is_running() const; | 389 bool is_running() const; |
389 | 390 |
390 // Returns true if the message loop has high resolution timers enabled. | 391 // Returns true if the message loop has high resolution timers enabled. |
391 // Provided for testing. | 392 // Provided for testing. |
392 bool HasHighResolutionTasks(); | 393 bool HasHighResolutionTasks(); |
393 | 394 |
394 // Returns true if the message loop is "idle". Provided for testing. | 395 // Returns true if the message loop is "idle". Provided for testing. |
395 bool IsIdleForTesting(); | 396 bool IsIdleForTesting(); |
396 | 397 |
397 // Wakes up the message pump. Can be called on any thread. The caller is | |
398 // responsible for synchronizing ScheduleWork() calls. | |
399 void ScheduleWork(); | |
400 | |
401 // Returns the TaskAnnotator which is used to add debug information to posted | 398 // Returns the TaskAnnotator which is used to add debug information to posted |
402 // tasks. | 399 // tasks. |
403 debug::TaskAnnotator* task_annotator() { return &task_annotator_; } | 400 debug::TaskAnnotator* task_annotator() { return &task_annotator_; } |
404 | 401 |
405 // Runs the specified PendingTask. | 402 // Runs the specified PendingTask. |
406 void RunTask(const PendingTask& pending_task); | 403 void RunTask(const PendingTask& pending_task); |
407 | 404 |
408 //---------------------------------------------------------------------------- | 405 //---------------------------------------------------------------------------- |
409 protected: | 406 protected: |
410 scoped_ptr<MessagePump> pump_; | 407 scoped_ptr<MessagePump> pump_; |
411 | 408 |
412 private: | 409 private: |
413 friend class RunLoop; | 410 friend class RunLoop; |
| 411 friend class internal::IncomingTaskQueue; |
| 412 friend class ScheduleWorkTest; |
| 413 friend class Thread; |
414 | 414 |
415 // Configures various members for the two constructors. | 415 using MessagePumpFactoryCallback = Callback<scoped_ptr<MessagePump>()>; |
416 void Init(); | 416 |
| 417 // Creates a MessageLoop without binding to a thread. |
| 418 // If |type| is TYPE_CUSTOM non-null |pump_factory| must be also given |
| 419 // to create a message pump for this message loop. Otherwise a default |
| 420 // message pump for the |type| is created. |
| 421 // |
| 422 // It is valid to call this to create a new message loop on one thread, |
| 423 // and then pass it to the thread where the message loop actually runs. |
| 424 // The message loop's BindToCurrentThread() method must be called on the |
| 425 // thread the message loop runs on, before calling Run(). |
| 426 // Before BindToCurrentThread() is called only Post*Task() functions can |
| 427 // be called on the message loop. |
| 428 scoped_ptr<MessageLoop> CreateUnbound( |
| 429 Type type, |
| 430 MessagePumpFactoryCallback pump_factory); |
| 431 |
| 432 // Common private constructor. Other constructors delegate the initialization |
| 433 // to this constructor. |
| 434 MessageLoop(Type type, MessagePumpFactoryCallback pump_factory); |
| 435 |
| 436 // Configure various members and bind this message loop to the current thread. |
| 437 void BindToCurrentThread(); |
417 | 438 |
418 // Invokes the actual run loop using the message pump. | 439 // Invokes the actual run loop using the message pump. |
419 void RunHandler(); | 440 void RunHandler(); |
420 | 441 |
421 // Called to process any delayed non-nestable tasks. | 442 // Called to process any delayed non-nestable tasks. |
422 bool ProcessNextDelayedNonNestableTask(); | 443 bool ProcessNextDelayedNonNestableTask(); |
423 | 444 |
424 // Calls RunTask or queues the pending_task on the deferred task list if it | 445 // 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. | 446 // cannot be run right now. Returns true if the task was run. |
426 bool DeferOrRunPendingTask(const PendingTask& pending_task); | 447 bool DeferOrRunPendingTask(const PendingTask& pending_task); |
427 | 448 |
428 // Adds the pending task to delayed_work_queue_. | 449 // Adds the pending task to delayed_work_queue_. |
429 void AddToDelayedWorkQueue(const PendingTask& pending_task); | 450 void AddToDelayedWorkQueue(const PendingTask& pending_task); |
430 | 451 |
431 // Delete tasks that haven't run yet without running them. Used in the | 452 // Delete tasks that haven't run yet without running them. Used in the |
432 // destructor to make sure all the task's destructors get called. Returns | 453 // destructor to make sure all the task's destructors get called. Returns |
433 // true if some work was done. | 454 // true if some work was done. |
434 bool DeletePendingTasks(); | 455 bool DeletePendingTasks(); |
435 | 456 |
436 // Loads tasks from the incoming queue to |work_queue_| if the latter is | 457 // Loads tasks from the incoming queue to |work_queue_| if the latter is |
437 // empty. | 458 // empty. |
438 void ReloadWorkQueue(); | 459 void ReloadWorkQueue(); |
439 | 460 |
| 461 // Wakes up the message pump. Can be called on any thread. The caller is |
| 462 // responsible for synchronizing ScheduleWork() calls. |
| 463 void ScheduleWork(); |
| 464 |
440 // Start recording histogram info about events and action IF it was enabled | 465 // Start recording histogram info about events and action IF it was enabled |
441 // and IF the statistics recorder can accept a registration of our histogram. | 466 // and IF the statistics recorder can accept a registration of our histogram. |
442 void StartHistogrammer(); | 467 void StartHistogrammer(); |
443 | 468 |
444 // Add occurrence of event to our histogram, so that we can see what is being | 469 // Add occurrence of event to our histogram, so that we can see what is being |
445 // done in a specific MessageLoop instance (i.e., specific thread). | 470 // done in a specific MessageLoop instance (i.e., specific thread). |
446 // If message_histogram_ is NULL, this is a no-op. | 471 // If message_histogram_ is NULL, this is a no-op. |
447 void HistogramEvent(int event); | 472 void HistogramEvent(int event); |
448 | 473 |
449 // MessagePump::Delegate methods: | 474 // MessagePump::Delegate methods: |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
483 // A recursion block that prevents accidentally running additional tasks when | 508 // A recursion block that prevents accidentally running additional tasks when |
484 // insider a (accidentally induced?) nested message pump. | 509 // insider a (accidentally induced?) nested message pump. |
485 bool nestable_tasks_allowed_; | 510 bool nestable_tasks_allowed_; |
486 | 511 |
487 #if defined(OS_WIN) | 512 #if defined(OS_WIN) |
488 // Should be set to true before calling Windows APIs like TrackPopupMenu, etc | 513 // Should be set to true before calling Windows APIs like TrackPopupMenu, etc |
489 // which enter a modal message loop. | 514 // which enter a modal message loop. |
490 bool os_modal_loop_; | 515 bool os_modal_loop_; |
491 #endif | 516 #endif |
492 | 517 |
| 518 // pump_factory_.Run() is called to create a message pump for this loop |
| 519 // if type_ is TYPE_CUSTOM and pump_ is null. |
| 520 MessagePumpFactoryCallback pump_factory_; |
| 521 |
493 std::string thread_name_; | 522 std::string thread_name_; |
494 // A profiling histogram showing the counts of various messages and events. | 523 // A profiling histogram showing the counts of various messages and events. |
495 HistogramBase* message_histogram_; | 524 HistogramBase* message_histogram_; |
496 | 525 |
497 RunLoop* run_loop_; | 526 RunLoop* run_loop_; |
498 | 527 |
499 ObserverList<TaskObserver> task_observers_; | 528 ObserverList<TaskObserver> task_observers_; |
500 | 529 |
501 debug::TaskAnnotator task_annotator_; | 530 debug::TaskAnnotator task_annotator_; |
502 | 531 |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
654 | 683 |
655 // Do not add any member variables to MessageLoopForIO! This is important b/c | 684 // Do not add any member variables to MessageLoopForIO! This is important b/c |
656 // MessageLoopForIO is often allocated via MessageLoop(TYPE_IO). Any extra | 685 // MessageLoopForIO is often allocated via MessageLoop(TYPE_IO). Any extra |
657 // data that you need should be stored on the MessageLoop's pump_ instance. | 686 // data that you need should be stored on the MessageLoop's pump_ instance. |
658 COMPILE_ASSERT(sizeof(MessageLoop) == sizeof(MessageLoopForIO), | 687 COMPILE_ASSERT(sizeof(MessageLoop) == sizeof(MessageLoopForIO), |
659 MessageLoopForIO_should_not_have_extra_member_variables); | 688 MessageLoopForIO_should_not_have_extra_member_variables); |
660 | 689 |
661 } // namespace base | 690 } // namespace base |
662 | 691 |
663 #endif // BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_ | 692 #endif // BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_ |
OLD | NEW |