| 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 10 matching lines...) Expand all Loading... |
| 21 #include "base/message_loop/message_pump.h" | 21 #include "base/message_loop/message_pump.h" |
| 22 #include "base/message_loop/timer_slack.h" | 22 #include "base/message_loop/timer_slack.h" |
| 23 #include "base/observer_list.h" | 23 #include "base/observer_list.h" |
| 24 #include "base/pending_task.h" | 24 #include "base/pending_task.h" |
| 25 #include "base/sequenced_task_runner_helpers.h" | 25 #include "base/sequenced_task_runner_helpers.h" |
| 26 #include "base/synchronization/lock.h" | 26 #include "base/synchronization/lock.h" |
| 27 #include "base/time/time.h" | 27 #include "base/time/time.h" |
| 28 #include "base/tracking_info.h" | 28 #include "base/tracking_info.h" |
| 29 | 29 |
| 30 // TODO(sky): these includes should not be necessary. Nuke them. | 30 // TODO(sky): these includes should not be necessary. Nuke them. |
| 31 #if defined(OS_WIN) | 31 #if defined(OS_IOS) |
| 32 #include "base/message_loop/message_pump_win.h" | |
| 33 #elif defined(OS_IOS) | |
| 34 #include "base/message_loop/message_pump_io_ios.h" | 32 #include "base/message_loop/message_pump_io_ios.h" |
| 35 #elif defined(OS_POSIX) | 33 #elif defined(OS_POSIX) |
| 36 #include "base/message_loop/message_pump_libevent.h" | 34 #include "base/message_loop/message_pump_libevent.h" |
| 37 #endif | 35 #endif |
| 38 | 36 |
| 39 namespace base { | 37 namespace base { |
| 40 | 38 |
| 41 class HistogramBase; | 39 class HistogramBase; |
| 42 class RunLoop; | 40 class RunLoop; |
| 43 class ThreadTaskRunnerHandle; | 41 class ThreadTaskRunnerHandle; |
| (...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 368 | 366 |
| 369 protected: | 367 protected: |
| 370 virtual ~TaskObserver(); | 368 virtual ~TaskObserver(); |
| 371 }; | 369 }; |
| 372 | 370 |
| 373 // These functions can only be called on the same thread that |this| is | 371 // These functions can only be called on the same thread that |this| is |
| 374 // running on. | 372 // running on. |
| 375 void AddTaskObserver(TaskObserver* task_observer); | 373 void AddTaskObserver(TaskObserver* task_observer); |
| 376 void RemoveTaskObserver(TaskObserver* task_observer); | 374 void RemoveTaskObserver(TaskObserver* task_observer); |
| 377 | 375 |
| 378 #if defined(OS_WIN) | |
| 379 void set_os_modal_loop(bool os_modal_loop) { | |
| 380 os_modal_loop_ = os_modal_loop; | |
| 381 } | |
| 382 | |
| 383 bool os_modal_loop() const { | |
| 384 return os_modal_loop_; | |
| 385 } | |
| 386 #endif // OS_WIN | |
| 387 | |
| 388 // Can only be called from the thread that owns the MessageLoop. | 376 // Can only be called from the thread that owns the MessageLoop. |
| 389 bool is_running() const; | 377 bool is_running() const; |
| 390 | 378 |
| 391 // Returns true if the message loop has high resolution timers enabled. | 379 // Returns true if the message loop has high resolution timers enabled. |
| 392 // Provided for testing. | 380 // Provided for testing. |
| 393 bool HasHighResolutionTasks(); | 381 bool HasHighResolutionTasks(); |
| 394 | 382 |
| 395 // Returns true if the message loop is "idle". Provided for testing. | 383 // Returns true if the message loop is "idle". Provided for testing. |
| 396 bool IsIdleForTesting(); | 384 bool IsIdleForTesting(); |
| 397 | 385 |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 475 bool DoWork() override; | 463 bool DoWork() override; |
| 476 bool DoDelayedWork(TimeTicks* next_delayed_work_time) override; | 464 bool DoDelayedWork(TimeTicks* next_delayed_work_time) override; |
| 477 bool DoIdleWork() override; | 465 bool DoIdleWork() override; |
| 478 | 466 |
| 479 const Type type_; | 467 const Type type_; |
| 480 | 468 |
| 481 // A list of tasks that need to be processed by this instance. Note that | 469 // A list of tasks that need to be processed by this instance. Note that |
| 482 // this queue is only accessed (push/pop) by our current thread. | 470 // this queue is only accessed (push/pop) by our current thread. |
| 483 TaskQueue work_queue_; | 471 TaskQueue work_queue_; |
| 484 | 472 |
| 485 #if defined(OS_WIN) | |
| 486 // How many high resolution tasks are in the pending task queue. This value | |
| 487 // increases by N every time we call ReloadWorkQueue() and decreases by 1 | |
| 488 // every time we call RunTask() if the task needs a high resolution timer. | |
| 489 int pending_high_res_tasks_; | |
| 490 // Tracks if we have requested high resolution timers. Its only use is to | |
| 491 // turn off the high resolution timer upon loop destruction. | |
| 492 bool in_high_res_mode_; | |
| 493 #endif | |
| 494 | |
| 495 // Contains delayed tasks, sorted by their 'delayed_run_time' property. | 473 // Contains delayed tasks, sorted by their 'delayed_run_time' property. |
| 496 DelayedTaskQueue delayed_work_queue_; | 474 DelayedTaskQueue delayed_work_queue_; |
| 497 | 475 |
| 498 // A recent snapshot of Time::Now(), used to check delayed_work_queue_. | 476 // A recent snapshot of Time::Now(), used to check delayed_work_queue_. |
| 499 TimeTicks recent_time_; | 477 TimeTicks recent_time_; |
| 500 | 478 |
| 501 // A queue of non-nestable tasks that we had to defer because when it came | 479 // A queue of non-nestable tasks that we had to defer because when it came |
| 502 // time to execute them we were in a nested message loop. They will execute | 480 // time to execute them we were in a nested message loop. They will execute |
| 503 // once we're out of nested message loops. | 481 // once we're out of nested message loops. |
| 504 TaskQueue deferred_non_nestable_work_queue_; | 482 TaskQueue deferred_non_nestable_work_queue_; |
| 505 | 483 |
| 506 ObserverList<DestructionObserver> destruction_observers_; | 484 ObserverList<DestructionObserver> destruction_observers_; |
| 507 | 485 |
| 508 // A recursion block that prevents accidentally running additional tasks when | 486 // A recursion block that prevents accidentally running additional tasks when |
| 509 // insider a (accidentally induced?) nested message pump. | 487 // insider a (accidentally induced?) nested message pump. |
| 510 bool nestable_tasks_allowed_; | 488 bool nestable_tasks_allowed_; |
| 511 | 489 |
| 512 #if defined(OS_WIN) | |
| 513 // Should be set to true before calling Windows APIs like TrackPopupMenu, etc | |
| 514 // which enter a modal message loop. | |
| 515 bool os_modal_loop_; | |
| 516 #endif | |
| 517 | |
| 518 // pump_factory_.Run() is called to create a message pump for this loop | 490 // pump_factory_.Run() is called to create a message pump for this loop |
| 519 // if type_ is TYPE_CUSTOM and pump_ is null. | 491 // if type_ is TYPE_CUSTOM and pump_ is null. |
| 520 MessagePumpFactoryCallback pump_factory_; | 492 MessagePumpFactoryCallback pump_factory_; |
| 521 | 493 |
| 522 std::string thread_name_; | 494 std::string thread_name_; |
| 523 // A profiling histogram showing the counts of various messages and events. | 495 // A profiling histogram showing the counts of various messages and events. |
| 524 HistogramBase* message_histogram_; | 496 HistogramBase* message_histogram_; |
| 525 | 497 |
| 526 RunLoop* run_loop_; | 498 RunLoop* run_loop_; |
| 527 | 499 |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 627 return static_cast<MessageLoopForIO*>(loop); | 599 return static_cast<MessageLoopForIO*>(loop); |
| 628 } | 600 } |
| 629 | 601 |
| 630 static bool IsCurrent() { | 602 static bool IsCurrent() { |
| 631 MessageLoop* loop = MessageLoop::current(); | 603 MessageLoop* loop = MessageLoop::current(); |
| 632 return loop && loop->type() == MessageLoop::TYPE_IO; | 604 return loop && loop->type() == MessageLoop::TYPE_IO; |
| 633 } | 605 } |
| 634 | 606 |
| 635 #if !defined(OS_NACL_SFI) | 607 #if !defined(OS_NACL_SFI) |
| 636 | 608 |
| 637 #if defined(OS_WIN) | 609 #if defined(OS_IOS) |
| 638 typedef MessagePumpForIO::IOHandler IOHandler; | |
| 639 typedef MessagePumpForIO::IOContext IOContext; | |
| 640 typedef MessagePumpForIO::IOObserver IOObserver; | |
| 641 #elif defined(OS_IOS) | |
| 642 typedef MessagePumpIOSForIO::Watcher Watcher; | 610 typedef MessagePumpIOSForIO::Watcher Watcher; |
| 643 typedef MessagePumpIOSForIO::FileDescriptorWatcher | 611 typedef MessagePumpIOSForIO::FileDescriptorWatcher |
| 644 FileDescriptorWatcher; | 612 FileDescriptorWatcher; |
| 645 typedef MessagePumpIOSForIO::IOObserver IOObserver; | 613 typedef MessagePumpIOSForIO::IOObserver IOObserver; |
| 646 | 614 |
| 647 enum Mode { | 615 enum Mode { |
| 648 WATCH_READ = MessagePumpIOSForIO::WATCH_READ, | 616 WATCH_READ = MessagePumpIOSForIO::WATCH_READ, |
| 649 WATCH_WRITE = MessagePumpIOSForIO::WATCH_WRITE, | 617 WATCH_WRITE = MessagePumpIOSForIO::WATCH_WRITE, |
| 650 WATCH_READ_WRITE = MessagePumpIOSForIO::WATCH_READ_WRITE | 618 WATCH_READ_WRITE = MessagePumpIOSForIO::WATCH_READ_WRITE |
| 651 }; | 619 }; |
| 652 #elif defined(OS_POSIX) | 620 #elif defined(OS_POSIX) |
| 653 typedef MessagePumpLibevent::Watcher Watcher; | 621 typedef MessagePumpLibevent::Watcher Watcher; |
| 654 typedef MessagePumpLibevent::FileDescriptorWatcher | 622 typedef MessagePumpLibevent::FileDescriptorWatcher |
| 655 FileDescriptorWatcher; | 623 FileDescriptorWatcher; |
| 656 typedef MessagePumpLibevent::IOObserver IOObserver; | 624 typedef MessagePumpLibevent::IOObserver IOObserver; |
| 657 | 625 |
| 658 enum Mode { | 626 enum Mode { |
| 659 WATCH_READ = MessagePumpLibevent::WATCH_READ, | 627 WATCH_READ = MessagePumpLibevent::WATCH_READ, |
| 660 WATCH_WRITE = MessagePumpLibevent::WATCH_WRITE, | 628 WATCH_WRITE = MessagePumpLibevent::WATCH_WRITE, |
| 661 WATCH_READ_WRITE = MessagePumpLibevent::WATCH_READ_WRITE | 629 WATCH_READ_WRITE = MessagePumpLibevent::WATCH_READ_WRITE |
| 662 }; | 630 }; |
| 663 #endif | 631 #endif |
| 664 | 632 |
| 665 void AddIOObserver(IOObserver* io_observer); | 633 void AddIOObserver(IOObserver* io_observer); |
| 666 void RemoveIOObserver(IOObserver* io_observer); | 634 void RemoveIOObserver(IOObserver* io_observer); |
| 667 | 635 |
| 668 #if defined(OS_WIN) | 636 #if defined(OS_POSIX) |
| 669 // Please see MessagePumpWin for definitions of these methods. | |
| 670 void RegisterIOHandler(HANDLE file, IOHandler* handler); | |
| 671 bool RegisterJobObject(HANDLE job, IOHandler* handler); | |
| 672 bool WaitForIOCompletion(DWORD timeout, IOHandler* filter); | |
| 673 #elif defined(OS_POSIX) | |
| 674 // Please see MessagePumpIOSForIO/MessagePumpLibevent for definition. | 637 // Please see MessagePumpIOSForIO/MessagePumpLibevent for definition. |
| 675 bool WatchFileDescriptor(int fd, | 638 bool WatchFileDescriptor(int fd, |
| 676 bool persistent, | 639 bool persistent, |
| 677 Mode mode, | 640 Mode mode, |
| 678 FileDescriptorWatcher* controller, | 641 FileDescriptorWatcher* controller, |
| 679 Watcher* delegate); | 642 Watcher* delegate); |
| 680 #endif // defined(OS_IOS) || defined(OS_POSIX) | 643 #endif // defined(OS_IOS) || defined(OS_POSIX) |
| 681 #endif // !defined(OS_NACL_SFI) | 644 #endif // !defined(OS_NACL_SFI) |
| 682 }; | 645 }; |
| 683 | 646 |
| 684 // Do not add any member variables to MessageLoopForIO! This is important b/c | 647 // Do not add any member variables to MessageLoopForIO! This is important b/c |
| 685 // MessageLoopForIO is often allocated via MessageLoop(TYPE_IO). Any extra | 648 // MessageLoopForIO is often allocated via MessageLoop(TYPE_IO). Any extra |
| 686 // data that you need should be stored on the MessageLoop's pump_ instance. | 649 // data that you need should be stored on the MessageLoop's pump_ instance. |
| 687 COMPILE_ASSERT(sizeof(MessageLoop) == sizeof(MessageLoopForIO), | 650 COMPILE_ASSERT(sizeof(MessageLoop) == sizeof(MessageLoopForIO), |
| 688 MessageLoopForIO_should_not_have_extra_member_variables); | 651 MessageLoopForIO_should_not_have_extra_member_variables); |
| 689 | 652 |
| 690 } // namespace base | 653 } // namespace base |
| 691 | 654 |
| 692 #endif // BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_ | 655 #endif // BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_ |
| OLD | NEW |