| 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 #include "base/message_loop/message_loop.h" | 5 #include "base/message_loop/message_loop.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 | 25 |
| 26 #if defined(OS_MACOSX) | 26 #if defined(OS_MACOSX) |
| 27 #include "base/message_loop/message_pump_mac.h" | 27 #include "base/message_loop/message_pump_mac.h" |
| 28 #endif | 28 #endif |
| 29 #if defined(OS_POSIX) && !defined(OS_IOS) | 29 #if defined(OS_POSIX) && !defined(OS_IOS) |
| 30 #include "base/message_loop/message_pump_libevent.h" | 30 #include "base/message_loop/message_pump_libevent.h" |
| 31 #endif | 31 #endif |
| 32 #if defined(OS_ANDROID) | 32 #if defined(OS_ANDROID) |
| 33 #include "base/message_loop/message_pump_android.h" | 33 #include "base/message_loop/message_pump_android.h" |
| 34 #endif | 34 #endif |
| 35 #if defined(USE_GLIB) |
| 36 #include "base/message_loop/message_pump_glib.h" |
| 37 #endif |
| 35 | 38 |
| 36 namespace base { | 39 namespace base { |
| 37 | 40 |
| 38 namespace { | 41 namespace { |
| 39 | 42 |
| 40 // A lazily created thread local storage for quick access to a thread's message | 43 // A lazily created thread local storage for quick access to a thread's message |
| 41 // loop, if one exists. This should be safe and free of static constructors. | 44 // loop, if one exists. This should be safe and free of static constructors. |
| 42 LazyInstance<base::ThreadLocalPointer<MessageLoop> >::Leaky lazy_tls_ptr = | 45 LazyInstance<base::ThreadLocalPointer<MessageLoop> >::Leaky lazy_tls_ptr = |
| 43 LAZY_INSTANCE_INITIALIZER; | 46 LAZY_INSTANCE_INITIALIZER; |
| 44 | 47 |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 // Returns true if MessagePump::ScheduleWork() must be called one | 91 // Returns true if MessagePump::ScheduleWork() must be called one |
| 89 // time for every task that is added to the MessageLoop incoming queue. | 92 // time for every task that is added to the MessageLoop incoming queue. |
| 90 bool AlwaysNotifyPump(MessageLoop::Type type) { | 93 bool AlwaysNotifyPump(MessageLoop::Type type) { |
| 91 #if defined(OS_ANDROID) | 94 #if defined(OS_ANDROID) |
| 92 return type == MessageLoop::TYPE_UI || type == MessageLoop::TYPE_JAVA; | 95 return type == MessageLoop::TYPE_UI || type == MessageLoop::TYPE_JAVA; |
| 93 #else | 96 #else |
| 94 return false; | 97 return false; |
| 95 #endif | 98 #endif |
| 96 } | 99 } |
| 97 | 100 |
| 101 #if defined(OS_IOS) |
| 102 typedef MessagePumpIOSForIO MessagePumpForIO; |
| 103 #elif defined(OS_NACL) |
| 104 typedef MessagePumpDefault MessagePumpForIO; |
| 105 #elif defined(OS_POSIX) |
| 106 typedef MessagePumpLibevent MessagePumpForIO; |
| 107 #endif |
| 108 |
| 109 MessagePumpForIO* ToPumpIO(MessagePump* pump) { |
| 110 return static_cast<MessagePumpForIO*>(pump); |
| 111 } |
| 112 |
| 98 } // namespace | 113 } // namespace |
| 99 | 114 |
| 100 //------------------------------------------------------------------------------ | 115 //------------------------------------------------------------------------------ |
| 101 | 116 |
| 102 MessageLoop::TaskObserver::TaskObserver() { | 117 MessageLoop::TaskObserver::TaskObserver() { |
| 103 } | 118 } |
| 104 | 119 |
| 105 MessageLoop::TaskObserver::~TaskObserver() { | 120 MessageLoop::TaskObserver::~TaskObserver() { |
| 106 } | 121 } |
| 107 | 122 |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 if (message_pump_for_ui_factory_) | 206 if (message_pump_for_ui_factory_) |
| 192 return false; | 207 return false; |
| 193 | 208 |
| 194 message_pump_for_ui_factory_ = factory; | 209 message_pump_for_ui_factory_ = factory; |
| 195 return true; | 210 return true; |
| 196 } | 211 } |
| 197 | 212 |
| 198 // static | 213 // static |
| 199 scoped_ptr<MessagePump> MessageLoop::CreateMessagePumpForType(Type type) { | 214 scoped_ptr<MessagePump> MessageLoop::CreateMessagePumpForType(Type type) { |
| 200 // TODO(rvargas): Get rid of the OS guards. | 215 // TODO(rvargas): Get rid of the OS guards. |
| 201 #if defined(OS_WIN) | 216 #if defined(USE_GLIB) && !defined(OS_NACL) |
| 202 #define MESSAGE_PUMP_UI scoped_ptr<MessagePump>(new MessagePumpForUI()) | 217 typedef MessagePumpGlib MessagePumpForUI; |
| 203 #define MESSAGE_PUMP_IO scoped_ptr<MessagePump>(new MessagePumpForIO()) | 218 #elif defined(OS_LINUX) && !defined(OS_NACL) |
| 204 #elif defined(OS_IOS) | 219 typedef MessagePumpLibevent MessagePumpForUI; |
| 220 #endif |
| 221 |
| 222 #if defined(OS_IOS) || defined(OS_MACOSX) |
| 205 #define MESSAGE_PUMP_UI scoped_ptr<MessagePump>(MessagePumpMac::Create()) | 223 #define MESSAGE_PUMP_UI scoped_ptr<MessagePump>(MessagePumpMac::Create()) |
| 206 #define MESSAGE_PUMP_IO scoped_ptr<MessagePump>(new MessagePumpIOSForIO()) | |
| 207 #elif defined(OS_MACOSX) | |
| 208 #define MESSAGE_PUMP_UI scoped_ptr<MessagePump>(MessagePumpMac::Create()) | |
| 209 #define MESSAGE_PUMP_IO scoped_ptr<MessagePump>(new MessagePumpLibevent()) | |
| 210 #elif defined(OS_NACL) | 224 #elif defined(OS_NACL) |
| 211 // Currently NaCl doesn't have a UI MessageLoop. | 225 // Currently NaCl doesn't have a UI MessageLoop. |
| 212 // TODO(abarth): Figure out if we need this. | 226 // TODO(abarth): Figure out if we need this. |
| 213 #define MESSAGE_PUMP_UI scoped_ptr<MessagePump>() | 227 #define MESSAGE_PUMP_UI scoped_ptr<MessagePump>() |
| 214 // ipc_channel_nacl.cc uses a worker thread to do socket reads currently, and | 228 #else |
| 215 // doesn't require extra support for watching file descriptors. | |
| 216 #define MESSAGE_PUMP_IO scoped_ptr<MessagePump>(new MessagePumpDefault()) | |
| 217 #elif defined(USE_OZONE) | |
| 218 #define MESSAGE_PUMP_UI scoped_ptr<MessagePump>(new MessagePumpLibevent()) | |
| 219 #define MESSAGE_PUMP_IO scoped_ptr<MessagePump>(new MessagePumpLibevent()) | |
| 220 #elif defined(OS_POSIX) // POSIX but not MACOSX. | |
| 221 #define MESSAGE_PUMP_UI scoped_ptr<MessagePump>(new MessagePumpForUI()) | 229 #define MESSAGE_PUMP_UI scoped_ptr<MessagePump>(new MessagePumpForUI()) |
| 222 #define MESSAGE_PUMP_IO scoped_ptr<MessagePump>(new MessagePumpLibevent()) | |
| 223 #else | |
| 224 #error Not implemented | |
| 225 #endif | 230 #endif |
| 226 | 231 |
| 227 if (type == MessageLoop::TYPE_UI) { | 232 if (type == MessageLoop::TYPE_UI) { |
| 228 if (message_pump_for_ui_factory_) | 233 if (message_pump_for_ui_factory_) |
| 229 return message_pump_for_ui_factory_(); | 234 return message_pump_for_ui_factory_(); |
| 230 return MESSAGE_PUMP_UI; | 235 return MESSAGE_PUMP_UI; |
| 231 } | 236 } |
| 232 if (type == MessageLoop::TYPE_IO) | 237 if (type == MessageLoop::TYPE_IO) |
| 233 return MESSAGE_PUMP_IO; | 238 return scoped_ptr<MessagePump>(new MessagePumpForIO()); |
| 239 |
| 234 #if defined(OS_ANDROID) | 240 #if defined(OS_ANDROID) |
| 235 if (type == MessageLoop::TYPE_JAVA) | 241 if (type == MessageLoop::TYPE_JAVA) |
| 236 return MESSAGE_PUMP_UI; | 242 return scoped_ptr<MessagePump>(new MessagePumpForUI()); |
| 237 #endif | 243 #endif |
| 244 |
| 238 DCHECK_EQ(MessageLoop::TYPE_DEFAULT, type); | 245 DCHECK_EQ(MessageLoop::TYPE_DEFAULT, type); |
| 239 return scoped_ptr<MessagePump>(new MessagePumpDefault()); | 246 return scoped_ptr<MessagePump>(new MessagePumpDefault()); |
| 240 } | 247 } |
| 241 | 248 |
| 242 void MessageLoop::AddDestructionObserver( | 249 void MessageLoop::AddDestructionObserver( |
| 243 DestructionObserver* destruction_observer) { | 250 DestructionObserver* destruction_observer) { |
| 244 DCHECK_EQ(this, current()); | 251 DCHECK_EQ(this, current()); |
| 245 destruction_observers_.AddObserver(destruction_observer); | 252 destruction_observers_.AddObserver(destruction_observer); |
| 246 } | 253 } |
| 247 | 254 |
| (...skipping 389 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 637 PostNonNestableTask(from_here, Bind(deleter, object)); | 644 PostNonNestableTask(from_here, Bind(deleter, object)); |
| 638 } | 645 } |
| 639 | 646 |
| 640 void MessageLoop::ReleaseSoonInternal( | 647 void MessageLoop::ReleaseSoonInternal( |
| 641 const tracked_objects::Location& from_here, | 648 const tracked_objects::Location& from_here, |
| 642 void(*releaser)(const void*), | 649 void(*releaser)(const void*), |
| 643 const void* object) { | 650 const void* object) { |
| 644 PostNonNestableTask(from_here, Bind(releaser, object)); | 651 PostNonNestableTask(from_here, Bind(releaser, object)); |
| 645 } | 652 } |
| 646 | 653 |
| 654 #if !defined(OS_NACL) |
| 647 //------------------------------------------------------------------------------ | 655 //------------------------------------------------------------------------------ |
| 648 // MessageLoopForUI | 656 // MessageLoopForUI |
| 649 | 657 |
| 650 #if defined(OS_ANDROID) | 658 #if defined(OS_ANDROID) |
| 651 void MessageLoopForUI::Start() { | 659 void MessageLoopForUI::Start() { |
| 652 // No Histogram support for UI message loop as it is managed by Java side | 660 // No Histogram support for UI message loop as it is managed by Java side |
| 653 static_cast<MessagePumpForUI*>(pump_.get())->Start(this); | 661 static_cast<MessagePumpForUI*>(pump_.get())->Start(this); |
| 654 } | 662 } |
| 655 #endif | 663 #endif |
| 656 | 664 |
| 657 #if defined(OS_IOS) | 665 #if defined(OS_IOS) |
| 658 void MessageLoopForUI::Attach() { | 666 void MessageLoopForUI::Attach() { |
| 659 static_cast<MessagePumpUIApplication*>(pump_.get())->Attach(this); | 667 static_cast<MessagePumpUIApplication*>(pump_.get())->Attach(this); |
| 660 } | 668 } |
| 661 #endif | 669 #endif |
| 662 | 670 |
| 663 #if !defined(OS_NACL) && defined(OS_WIN) | 671 #if defined(OS_WIN) |
| 664 void MessageLoopForUI::AddObserver(Observer* observer) { | 672 void MessageLoopForUI::AddObserver(Observer* observer) { |
| 665 pump_ui()->AddObserver(observer); | 673 static_cast<MessagePumpWin*>(pump_.get())->AddObserver(observer); |
| 666 } | 674 } |
| 667 | 675 |
| 668 void MessageLoopForUI::RemoveObserver(Observer* observer) { | 676 void MessageLoopForUI::RemoveObserver(Observer* observer) { |
| 669 pump_ui()->RemoveObserver(observer); | 677 static_cast<MessagePumpWin*>(pump_.get())->RemoveObserver(observer); |
| 670 } | 678 } |
| 671 #endif // !defined(OS_NACL) && defined(OS_WIN) | 679 #endif // defined(OS_WIN) |
| 672 | 680 |
| 673 #if !defined(OS_NACL) && \ | 681 #if defined(USE_OZONE) || (defined(OS_CHROMEOS) && !defined(USE_GLIB)) |
| 674 (defined(USE_OZONE) || (defined(OS_CHROMEOS) && !defined(USE_GLIB))) | |
| 675 bool MessageLoopForUI::WatchFileDescriptor( | 682 bool MessageLoopForUI::WatchFileDescriptor( |
| 676 int fd, | 683 int fd, |
| 677 bool persistent, | 684 bool persistent, |
| 678 MessagePumpLibevent::Mode mode, | 685 MessagePumpLibevent::Mode mode, |
| 679 MessagePumpLibevent::FileDescriptorWatcher *controller, | 686 MessagePumpLibevent::FileDescriptorWatcher *controller, |
| 680 MessagePumpLibevent::Watcher *delegate) { | 687 MessagePumpLibevent::Watcher *delegate) { |
| 681 return pump_libevent()->WatchFileDescriptor( | 688 return static_cast<MessagePumpLibevent*>(pump_.get())->WatchFileDescriptor( |
| 682 fd, | 689 fd, |
| 683 persistent, | 690 persistent, |
| 684 mode, | 691 mode, |
| 685 controller, | 692 controller, |
| 686 delegate); | 693 delegate); |
| 687 } | 694 } |
| 688 #endif | 695 #endif |
| 689 | 696 |
| 697 #endif // !defined(OS_NACL) |
| 698 |
| 690 //------------------------------------------------------------------------------ | 699 //------------------------------------------------------------------------------ |
| 691 // MessageLoopForIO | 700 // MessageLoopForIO |
| 692 | 701 |
| 702 #if !defined(OS_NACL) |
| 703 void MessageLoopForIO::AddIOObserver( |
| 704 MessageLoopForIO::IOObserver* io_observer) { |
| 705 ToPumpIO(pump_.get())->AddIOObserver(io_observer); |
| 706 } |
| 707 |
| 708 void MessageLoopForIO::RemoveIOObserver( |
| 709 MessageLoopForIO::IOObserver* io_observer) { |
| 710 ToPumpIO(pump_.get())->RemoveIOObserver(io_observer); |
| 711 } |
| 712 |
| 693 #if defined(OS_WIN) | 713 #if defined(OS_WIN) |
| 694 | |
| 695 void MessageLoopForIO::RegisterIOHandler(HANDLE file, IOHandler* handler) { | 714 void MessageLoopForIO::RegisterIOHandler(HANDLE file, IOHandler* handler) { |
| 696 pump_io()->RegisterIOHandler(file, handler); | 715 ToPumpIO(pump_.get())->RegisterIOHandler(file, handler); |
| 697 } | 716 } |
| 698 | 717 |
| 699 bool MessageLoopForIO::RegisterJobObject(HANDLE job, IOHandler* handler) { | 718 bool MessageLoopForIO::RegisterJobObject(HANDLE job, IOHandler* handler) { |
| 700 return pump_io()->RegisterJobObject(job, handler); | 719 return ToPumpIO(pump_.get())->RegisterJobObject(job, handler); |
| 701 } | 720 } |
| 702 | 721 |
| 703 bool MessageLoopForIO::WaitForIOCompletion(DWORD timeout, IOHandler* filter) { | 722 bool MessageLoopForIO::WaitForIOCompletion(DWORD timeout, IOHandler* filter) { |
| 704 return pump_io()->WaitForIOCompletion(timeout, filter); | 723 return ToPumpIO(pump_.get())->WaitForIOCompletion(timeout, filter); |
| 705 } | 724 } |
| 706 | 725 #elif defined(OS_POSIX) |
| 707 #elif defined(OS_IOS) | |
| 708 | |
| 709 bool MessageLoopForIO::WatchFileDescriptor(int fd, | 726 bool MessageLoopForIO::WatchFileDescriptor(int fd, |
| 710 bool persistent, | 727 bool persistent, |
| 711 Mode mode, | 728 Mode mode, |
| 712 FileDescriptorWatcher *controller, | 729 FileDescriptorWatcher *controller, |
| 713 Watcher *delegate) { | 730 Watcher *delegate) { |
| 714 return pump_io()->WatchFileDescriptor( | 731 return ToPumpIO(pump_.get())->WatchFileDescriptor( |
| 715 fd, | 732 fd, |
| 716 persistent, | 733 persistent, |
| 717 mode, | 734 mode, |
| 718 controller, | 735 controller, |
| 719 delegate); | 736 delegate); |
| 720 } | 737 } |
| 721 | |
| 722 #elif defined(OS_POSIX) && !defined(OS_NACL) | |
| 723 | |
| 724 bool MessageLoopForIO::WatchFileDescriptor(int fd, | |
| 725 bool persistent, | |
| 726 Mode mode, | |
| 727 FileDescriptorWatcher *controller, | |
| 728 Watcher *delegate) { | |
| 729 return pump_libevent()->WatchFileDescriptor( | |
| 730 fd, | |
| 731 persistent, | |
| 732 mode, | |
| 733 controller, | |
| 734 delegate); | |
| 735 } | |
| 736 | |
| 737 #endif | 738 #endif |
| 738 | 739 |
| 740 #endif // !defined(OS_NACL) |
| 741 |
| 739 } // namespace base | 742 } // namespace base |
| OLD | NEW |