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