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 14 matching lines...) Expand all Loading... |
25 #include "base/time/time.h" | 25 #include "base/time/time.h" |
26 #include "base/tracking_info.h" | 26 #include "base/tracking_info.h" |
27 | 27 |
28 // TODO(sky): these includes should not be necessary. Nuke them. | 28 // TODO(sky): these includes should not be necessary. Nuke them. |
29 #if defined(OS_WIN) | 29 #if defined(OS_WIN) |
30 #include "base/message_loop/message_pump_win.h" | 30 #include "base/message_loop/message_pump_win.h" |
31 #elif defined(OS_IOS) | 31 #elif defined(OS_IOS) |
32 #include "base/message_loop/message_pump_io_ios.h" | 32 #include "base/message_loop/message_pump_io_ios.h" |
33 #elif defined(OS_POSIX) | 33 #elif defined(OS_POSIX) |
34 #include "base/message_loop/message_pump_libevent.h" | 34 #include "base/message_loop/message_pump_libevent.h" |
35 #if !defined(OS_MACOSX) && !defined(OS_ANDROID) | |
36 | |
37 #if defined(OS_CHROMEOS) && !defined(OS_NACL) && !defined(USE_GLIB) | |
38 #include "base/message_loop/message_pump_libevent.h" | |
39 #elif defined(USE_GLIB) && !defined(OS_NACL) | |
40 #include "base/message_loop/message_pump_glib.h" | |
41 #elif !defined(OS_ANDROID_HOST) | |
42 #include "base/message_loop/message_pump_glib.h" | |
43 #endif | |
44 | |
45 #endif | |
46 #endif | 35 #endif |
47 | 36 |
48 namespace base { | 37 namespace base { |
49 | 38 |
50 class HistogramBase; | 39 class HistogramBase; |
51 class MessagePumpObserver; | 40 class MessagePumpObserver; |
52 class RunLoop; | 41 class RunLoop; |
53 class ThreadTaskRunnerHandle; | 42 class ThreadTaskRunnerHandle; |
54 #if defined(OS_ANDROID) | |
55 class MessagePumpForUI; | |
56 #elif defined(OS_ANDROID_HOST) || (defined(OS_CHROMEOS) && !defined(USE_GLIB)) | |
57 typedef MessagePumpLibevent MessagePumpForUI; | |
58 #endif | |
59 class WaitableEvent; | 43 class WaitableEvent; |
60 | 44 |
61 // A MessageLoop is used to process events for a particular thread. There is | 45 // A MessageLoop is used to process events for a particular thread. There is |
62 // at most one MessageLoop instance per thread. | 46 // at most one MessageLoop instance per thread. |
63 // | 47 // |
64 // Events include at a minimum Task instances submitted to PostTask and its | 48 // Events include at a minimum Task instances submitted to PostTask and its |
65 // variants. Depending on the type of message pump used by the MessageLoop | 49 // variants. Depending on the type of message pump used by the MessageLoop |
66 // other events such as UI messages may be processed. On Windows APC calls (as | 50 // other events such as UI messages may be processed. On Windows APC calls (as |
67 // time permits) and signals sent to a registered set of HANDLEs may also be | 51 // time permits) and signals sent to a registered set of HANDLEs may also be |
68 // processed. | 52 // processed. |
(...skipping 15 matching lines...) Expand all Loading... |
84 // MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current()); | 68 // MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current()); |
85 // hr = DoDragDrop(...); // Implicitly runs a modal message loop. | 69 // hr = DoDragDrop(...); // Implicitly runs a modal message loop. |
86 // } | 70 // } |
87 // // Process |hr| (the result returned by DoDragDrop()). | 71 // // Process |hr| (the result returned by DoDragDrop()). |
88 // | 72 // |
89 // Please be SURE your task is reentrant (nestable) and all global variables | 73 // Please be SURE your task is reentrant (nestable) and all global variables |
90 // are stable and accessible before calling SetNestableTasksAllowed(true). | 74 // are stable and accessible before calling SetNestableTasksAllowed(true). |
91 // | 75 // |
92 class BASE_EXPORT MessageLoop : public MessagePump::Delegate { | 76 class BASE_EXPORT MessageLoop : public MessagePump::Delegate { |
93 public: | 77 public: |
94 #if defined(OS_WIN) | |
95 typedef MessagePumpObserver Observer; | |
96 #endif | |
97 | |
98 // A MessageLoop has a particular type, which indicates the set of | 78 // A MessageLoop has a particular type, which indicates the set of |
99 // asynchronous events it may process in addition to tasks and timers. | 79 // asynchronous events it may process in addition to tasks and timers. |
100 // | 80 // |
101 // TYPE_DEFAULT | 81 // TYPE_DEFAULT |
102 // This type of ML only supports tasks and timers. | 82 // This type of ML only supports tasks and timers. |
103 // | 83 // |
104 // TYPE_UI | 84 // TYPE_UI |
105 // This type of ML also supports native UI events (e.g., Windows messages). | 85 // This type of ML also supports native UI events (e.g., Windows messages). |
106 // See also MessageLoopForUI. | 86 // See also MessageLoopForUI. |
107 // | 87 // |
(...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
385 | 365 |
386 // Returns true if the message loop has high resolution timers enabled. | 366 // Returns true if the message loop has high resolution timers enabled. |
387 // Provided for testing. | 367 // Provided for testing. |
388 bool IsHighResolutionTimerEnabledForTesting(); | 368 bool IsHighResolutionTimerEnabledForTesting(); |
389 | 369 |
390 // Returns true if the message loop is "idle". Provided for testing. | 370 // Returns true if the message loop is "idle". Provided for testing. |
391 bool IsIdleForTesting(); | 371 bool IsIdleForTesting(); |
392 | 372 |
393 //---------------------------------------------------------------------------- | 373 //---------------------------------------------------------------------------- |
394 protected: | 374 protected: |
395 | |
396 #if defined(OS_WIN) | |
397 MessagePumpWin* pump_win() { | |
398 return static_cast<MessagePumpWin*>(pump_.get()); | |
399 } | |
400 #elif defined(OS_POSIX) && !defined(OS_IOS) | |
401 MessagePumpLibevent* pump_libevent() { | |
402 return static_cast<MessagePumpLibevent*>(pump_.get()); | |
403 } | |
404 #endif | |
405 | |
406 scoped_ptr<MessagePump> pump_; | 375 scoped_ptr<MessagePump> pump_; |
407 | 376 |
408 private: | 377 private: |
409 friend class internal::IncomingTaskQueue; | 378 friend class internal::IncomingTaskQueue; |
410 friend class RunLoop; | 379 friend class RunLoop; |
411 | 380 |
412 // Configures various members for the two constructors. | 381 // Configures various members for the two constructors. |
413 void Init(); | 382 void Init(); |
414 | 383 |
415 // Invokes the actual run loop using the message pump. | 384 // Invokes the actual run loop using the message pump. |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
511 void DeleteSoonInternal(const tracked_objects::Location& from_here, | 480 void DeleteSoonInternal(const tracked_objects::Location& from_here, |
512 void(*deleter)(const void*), | 481 void(*deleter)(const void*), |
513 const void* object); | 482 const void* object); |
514 void ReleaseSoonInternal(const tracked_objects::Location& from_here, | 483 void ReleaseSoonInternal(const tracked_objects::Location& from_here, |
515 void(*releaser)(const void*), | 484 void(*releaser)(const void*), |
516 const void* object); | 485 const void* object); |
517 | 486 |
518 DISALLOW_COPY_AND_ASSIGN(MessageLoop); | 487 DISALLOW_COPY_AND_ASSIGN(MessageLoop); |
519 }; | 488 }; |
520 | 489 |
| 490 #if !defined(OS_NACL) |
| 491 |
521 //----------------------------------------------------------------------------- | 492 //----------------------------------------------------------------------------- |
522 // MessageLoopForUI extends MessageLoop with methods that are particular to a | 493 // MessageLoopForUI extends MessageLoop with methods that are particular to a |
523 // MessageLoop instantiated with TYPE_UI. | 494 // MessageLoop instantiated with TYPE_UI. |
524 // | 495 // |
525 // This class is typically used like so: | 496 // This class is typically used like so: |
526 // MessageLoopForUI::current()->...call some method... | 497 // MessageLoopForUI::current()->...call some method... |
527 // | 498 // |
528 class BASE_EXPORT MessageLoopForUI : public MessageLoop { | 499 class BASE_EXPORT MessageLoopForUI : public MessageLoop { |
529 public: | 500 public: |
530 MessageLoopForUI() : MessageLoop(TYPE_UI) { | 501 MessageLoopForUI() : MessageLoop(TYPE_UI) { |
(...skipping 19 matching lines...) Expand all Loading... |
550 void Attach(); | 521 void Attach(); |
551 #endif | 522 #endif |
552 | 523 |
553 #if defined(OS_ANDROID) | 524 #if defined(OS_ANDROID) |
554 // On Android, the UI message loop is handled by Java side. So Run() should | 525 // On Android, the UI message loop is handled by Java side. So Run() should |
555 // never be called. Instead use Start(), which will forward all the native UI | 526 // never be called. Instead use Start(), which will forward all the native UI |
556 // events to the Java message loop. | 527 // events to the Java message loop. |
557 void Start(); | 528 void Start(); |
558 #endif | 529 #endif |
559 | 530 |
560 #if !defined(OS_NACL) && defined(OS_WIN) | 531 #if defined(OS_WIN) |
| 532 typedef MessagePumpObserver Observer; |
| 533 |
561 // Please see message_pump_win for definitions of these methods. | 534 // Please see message_pump_win for definitions of these methods. |
562 void AddObserver(Observer* observer); | 535 void AddObserver(Observer* observer); |
563 void RemoveObserver(Observer* observer); | 536 void RemoveObserver(Observer* observer); |
564 #endif | 537 #endif |
565 | 538 |
566 #if !defined(OS_NACL) && \ | 539 #if defined(USE_OZONE) || (defined(OS_CHROMEOS) && !defined(USE_GLIB)) |
567 (defined(USE_OZONE) || (defined(OS_CHROMEOS) && !defined(USE_GLIB))) | |
568 // Please see MessagePumpLibevent for definition. | 540 // Please see MessagePumpLibevent for definition. |
569 bool WatchFileDescriptor( | 541 bool WatchFileDescriptor( |
570 int fd, | 542 int fd, |
571 bool persistent, | 543 bool persistent, |
572 MessagePumpLibevent::Mode mode, | 544 MessagePumpLibevent::Mode mode, |
573 MessagePumpLibevent::FileDescriptorWatcher* controller, | 545 MessagePumpLibevent::FileDescriptorWatcher* controller, |
574 MessagePumpLibevent::Watcher* delegate); | 546 MessagePumpLibevent::Watcher* delegate); |
575 #endif | 547 #endif |
576 | |
577 protected: | |
578 #if !defined(OS_MACOSX) && !defined(OS_ANDROID) | |
579 // TODO(rvargas): Make this platform independent. | |
580 MessagePumpForUI* pump_ui() { | |
581 return static_cast<MessagePumpForUI*>(pump_.get()); | |
582 } | |
583 #endif | |
584 }; | 548 }; |
585 | 549 |
586 // Do not add any member variables to MessageLoopForUI! This is important b/c | 550 // Do not add any member variables to MessageLoopForUI! This is important b/c |
587 // MessageLoopForUI is often allocated via MessageLoop(TYPE_UI). Any extra | 551 // MessageLoopForUI is often allocated via MessageLoop(TYPE_UI). Any extra |
588 // data that you need should be stored on the MessageLoop's pump_ instance. | 552 // data that you need should be stored on the MessageLoop's pump_ instance. |
589 COMPILE_ASSERT(sizeof(MessageLoop) == sizeof(MessageLoopForUI), | 553 COMPILE_ASSERT(sizeof(MessageLoop) == sizeof(MessageLoopForUI), |
590 MessageLoopForUI_should_not_have_extra_member_variables); | 554 MessageLoopForUI_should_not_have_extra_member_variables); |
591 | 555 |
| 556 #endif // !defined(OS_NACL) |
| 557 |
592 //----------------------------------------------------------------------------- | 558 //----------------------------------------------------------------------------- |
593 // MessageLoopForIO extends MessageLoop with methods that are particular to a | 559 // MessageLoopForIO extends MessageLoop with methods that are particular to a |
594 // MessageLoop instantiated with TYPE_IO. | 560 // MessageLoop instantiated with TYPE_IO. |
595 // | 561 // |
596 // This class is typically used like so: | 562 // This class is typically used like so: |
597 // MessageLoopForIO::current()->...call some method... | 563 // MessageLoopForIO::current()->...call some method... |
598 // | 564 // |
599 class BASE_EXPORT MessageLoopForIO : public MessageLoop { | 565 class BASE_EXPORT MessageLoopForIO : public MessageLoop { |
600 public: | 566 public: |
| 567 MessageLoopForIO() : MessageLoop(TYPE_IO) { |
| 568 } |
| 569 |
| 570 // Returns the MessageLoopForIO of the current thread. |
| 571 static MessageLoopForIO* current() { |
| 572 MessageLoop* loop = MessageLoop::current(); |
| 573 DCHECK_EQ(MessageLoop::TYPE_IO, loop->type()); |
| 574 return static_cast<MessageLoopForIO*>(loop); |
| 575 } |
| 576 |
| 577 static bool IsCurrent() { |
| 578 MessageLoop* loop = MessageLoop::current(); |
| 579 return loop && loop->type() == MessageLoop::TYPE_IO; |
| 580 } |
| 581 |
| 582 #if !defined(OS_NACL) |
| 583 |
601 #if defined(OS_WIN) | 584 #if defined(OS_WIN) |
602 typedef MessagePumpForIO::IOHandler IOHandler; | 585 typedef MessagePumpForIO::IOHandler IOHandler; |
603 typedef MessagePumpForIO::IOContext IOContext; | 586 typedef MessagePumpForIO::IOContext IOContext; |
604 typedef MessagePumpForIO::IOObserver IOObserver; | 587 typedef MessagePumpForIO::IOObserver IOObserver; |
605 #elif defined(OS_IOS) | 588 #elif defined(OS_IOS) |
606 typedef MessagePumpIOSForIO::Watcher Watcher; | 589 typedef MessagePumpIOSForIO::Watcher Watcher; |
607 typedef MessagePumpIOSForIO::FileDescriptorWatcher | 590 typedef MessagePumpIOSForIO::FileDescriptorWatcher |
608 FileDescriptorWatcher; | 591 FileDescriptorWatcher; |
609 typedef MessagePumpIOSForIO::IOObserver IOObserver; | 592 typedef MessagePumpIOSForIO::IOObserver IOObserver; |
610 | 593 |
611 enum Mode { | 594 enum Mode { |
612 WATCH_READ = MessagePumpIOSForIO::WATCH_READ, | 595 WATCH_READ = MessagePumpIOSForIO::WATCH_READ, |
613 WATCH_WRITE = MessagePumpIOSForIO::WATCH_WRITE, | 596 WATCH_WRITE = MessagePumpIOSForIO::WATCH_WRITE, |
614 WATCH_READ_WRITE = MessagePumpIOSForIO::WATCH_READ_WRITE | 597 WATCH_READ_WRITE = MessagePumpIOSForIO::WATCH_READ_WRITE |
615 }; | 598 }; |
616 #elif defined(OS_POSIX) | 599 #elif defined(OS_POSIX) |
617 typedef MessagePumpLibevent::Watcher Watcher; | 600 typedef MessagePumpLibevent::Watcher Watcher; |
618 typedef MessagePumpLibevent::FileDescriptorWatcher | 601 typedef MessagePumpLibevent::FileDescriptorWatcher |
619 FileDescriptorWatcher; | 602 FileDescriptorWatcher; |
620 typedef MessagePumpLibevent::IOObserver IOObserver; | 603 typedef MessagePumpLibevent::IOObserver IOObserver; |
621 | 604 |
622 enum Mode { | 605 enum Mode { |
623 WATCH_READ = MessagePumpLibevent::WATCH_READ, | 606 WATCH_READ = MessagePumpLibevent::WATCH_READ, |
624 WATCH_WRITE = MessagePumpLibevent::WATCH_WRITE, | 607 WATCH_WRITE = MessagePumpLibevent::WATCH_WRITE, |
625 WATCH_READ_WRITE = MessagePumpLibevent::WATCH_READ_WRITE | 608 WATCH_READ_WRITE = MessagePumpLibevent::WATCH_READ_WRITE |
626 }; | 609 }; |
627 | |
628 #endif | 610 #endif |
629 | 611 |
630 MessageLoopForIO() : MessageLoop(TYPE_IO) { | 612 void AddIOObserver(IOObserver* io_observer); |
631 } | 613 void RemoveIOObserver(IOObserver* io_observer); |
632 | |
633 // Returns the MessageLoopForIO of the current thread. | |
634 static MessageLoopForIO* current() { | |
635 MessageLoop* loop = MessageLoop::current(); | |
636 DCHECK_EQ(MessageLoop::TYPE_IO, loop->type()); | |
637 return static_cast<MessageLoopForIO*>(loop); | |
638 } | |
639 | |
640 static bool IsCurrent() { | |
641 MessageLoop* loop = MessageLoop::current(); | |
642 return loop && loop->type() == MessageLoop::TYPE_IO; | |
643 } | |
644 | |
645 void AddIOObserver(IOObserver* io_observer) { | |
646 pump_io()->AddIOObserver(io_observer); | |
647 } | |
648 | |
649 void RemoveIOObserver(IOObserver* io_observer) { | |
650 pump_io()->RemoveIOObserver(io_observer); | |
651 } | |
652 | 614 |
653 #if defined(OS_WIN) | 615 #if defined(OS_WIN) |
654 // Please see MessagePumpWin for definitions of these methods. | 616 // Please see MessagePumpWin for definitions of these methods. |
655 void RegisterIOHandler(HANDLE file, IOHandler* handler); | 617 void RegisterIOHandler(HANDLE file, IOHandler* handler); |
656 bool RegisterJobObject(HANDLE job, IOHandler* handler); | 618 bool RegisterJobObject(HANDLE job, IOHandler* handler); |
657 bool WaitForIOCompletion(DWORD timeout, IOHandler* filter); | 619 bool WaitForIOCompletion(DWORD timeout, IOHandler* filter); |
658 | 620 #elif defined(OS_POSIX) |
659 protected: | 621 // Please see MessagePumpIOSForIO/MessagePumpLibevent for definition. |
660 // TODO(rvargas): Make this platform independent. | |
661 MessagePumpForIO* pump_io() { | |
662 return static_cast<MessagePumpForIO*>(pump_.get()); | |
663 } | |
664 | |
665 #elif defined(OS_IOS) | |
666 // Please see MessagePumpIOSForIO for definition. | |
667 bool WatchFileDescriptor(int fd, | 622 bool WatchFileDescriptor(int fd, |
668 bool persistent, | 623 bool persistent, |
669 Mode mode, | 624 Mode mode, |
670 FileDescriptorWatcher *controller, | 625 FileDescriptorWatcher *controller, |
671 Watcher *delegate); | 626 Watcher *delegate); |
672 | 627 #endif // defined(OS_IOS) || defined(OS_POSIX) |
673 private: | 628 #endif // !defined(OS_NACL) |
674 MessagePumpIOSForIO* pump_io() { | |
675 return static_cast<MessagePumpIOSForIO*>(pump_.get()); | |
676 } | |
677 | |
678 #elif defined(OS_POSIX) | |
679 // Please see MessagePumpLibevent for definition. | |
680 bool WatchFileDescriptor(int fd, | |
681 bool persistent, | |
682 Mode mode, | |
683 FileDescriptorWatcher* controller, | |
684 Watcher* delegate); | |
685 | |
686 private: | |
687 MessagePumpLibevent* pump_io() { | |
688 return static_cast<MessagePumpLibevent*>(pump_.get()); | |
689 } | |
690 #endif // defined(OS_POSIX) | |
691 }; | 629 }; |
692 | 630 |
693 // Do not add any member variables to MessageLoopForIO! This is important b/c | 631 // Do not add any member variables to MessageLoopForIO! This is important b/c |
694 // MessageLoopForIO is often allocated via MessageLoop(TYPE_IO). Any extra | 632 // MessageLoopForIO is often allocated via MessageLoop(TYPE_IO). Any extra |
695 // data that you need should be stored on the MessageLoop's pump_ instance. | 633 // data that you need should be stored on the MessageLoop's pump_ instance. |
696 COMPILE_ASSERT(sizeof(MessageLoop) == sizeof(MessageLoopForIO), | 634 COMPILE_ASSERT(sizeof(MessageLoop) == sizeof(MessageLoopForIO), |
697 MessageLoopForIO_should_not_have_extra_member_variables); | 635 MessageLoopForIO_should_not_have_extra_member_variables); |
698 | 636 |
699 } // namespace base | 637 } // namespace base |
700 | 638 |
701 #endif // BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_ | 639 #endif // BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_ |
OLD | NEW |