| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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.h" | 5 #include "base/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" |
| 11 #include "base/debug/alias.h" | 11 #include "base/debug/alias.h" |
| 12 #include "base/lazy_instance.h" | 12 #include "base/lazy_instance.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/message_pump_default.h" | 15 #include "base/message_pump_default.h" |
| 16 #include "base/metrics/histogram.h" | 16 #include "base/metrics/histogram.h" |
| 17 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | 17 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
| 18 #include "base/threading/thread_local.h" | 18 #include "base/threading/thread_local.h" |
| 19 #include "base/time.h" | 19 #include "base/time.h" |
| 20 #include "base/tracked_objects.h" | 20 #include "base/tracked_objects.h" |
| 21 | 21 |
| 22 #if defined(OS_MACOSX) | 22 #if defined(OS_MACOSX) |
| 23 #include "base/message_pump_mac.h" | 23 #include "base/message_pump_mac.h" |
| 24 #endif | 24 #endif |
| 25 #if defined(OS_POSIX) | 25 #if defined(OS_POSIX) |
| 26 #include "base/message_pump_libevent.h" | 26 #include "base/message_pump_libevent.h" |
| 27 #endif | 27 #endif |
| 28 | 28 #if defined(OS_ANDROID) |
| 29 #if defined(OS_POSIX) && !defined(OS_MACOSX) | 29 #include "base/message_pump_android.h" |
| 30 #endif |
| 31 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) |
| 30 #include <gdk/gdk.h> | 32 #include <gdk/gdk.h> |
| 31 #include <gdk/gdkx.h> | 33 #include <gdk/gdkx.h> |
| 32 #if defined(TOUCH_UI) | 34 #if defined(TOUCH_UI) |
| 33 #include "base/message_pump_x.h" | 35 #include "base/message_pump_x.h" |
| 34 #else | 36 #else |
| 35 #include "base/message_pump_gtk.h" | 37 #include "base/message_pump_gtk.h" |
| 36 #endif // defined(TOUCH_UI) | 38 #endif // defined(TOUCH_UI) |
| 37 #endif // defined(OS_POSIX) && !defined(OS_MACOSX) | 39 #endif // defined(OS_POSIX) && !defined(OS_MACOSX) |
| 38 | 40 |
| 39 using base::TimeDelta; | 41 using base::TimeDelta; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 | 80 |
| 79 // A few events we handle (kindred to messages), and used to profile actions. | 81 // A few events we handle (kindred to messages), and used to profile actions. |
| 80 VALUE_TO_NUMBER_AND_NAME(kTaskRunEvent) | 82 VALUE_TO_NUMBER_AND_NAME(kTaskRunEvent) |
| 81 VALUE_TO_NUMBER_AND_NAME(kTimerEvent) | 83 VALUE_TO_NUMBER_AND_NAME(kTimerEvent) |
| 82 | 84 |
| 83 {-1, NULL} // The list must be null terminated, per API to histogram. | 85 {-1, NULL} // The list must be null terminated, per API to histogram. |
| 84 }; | 86 }; |
| 85 | 87 |
| 86 bool enable_histogrammer_ = false; | 88 bool enable_histogrammer_ = false; |
| 87 | 89 |
| 90 MessageLoop::MessagePumpFactory* message_pump_for_ui_factory_ = NULL; |
| 91 |
| 88 } // namespace | 92 } // namespace |
| 89 | 93 |
| 90 //------------------------------------------------------------------------------ | 94 //------------------------------------------------------------------------------ |
| 91 | 95 |
| 92 #if defined(OS_WIN) | 96 #if defined(OS_WIN) |
| 93 | 97 |
| 94 // Upon a SEH exception in this thread, it restores the original unhandled | 98 // Upon a SEH exception in this thread, it restores the original unhandled |
| 95 // exception filter. | 99 // exception filter. |
| 96 static int SEHFilter(LPTOP_LEVEL_EXCEPTION_FILTER old_filter) { | 100 static int SEHFilter(LPTOP_LEVEL_EXCEPTION_FILTER old_filter) { |
| 97 ::SetUnhandledExceptionFilter(old_filter); | 101 ::SetUnhandledExceptionFilter(old_filter); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 DCHECK(!current()) << "should only have one message loop per thread"; | 140 DCHECK(!current()) << "should only have one message loop per thread"; |
| 137 lazy_tls_ptr.Pointer()->Set(this); | 141 lazy_tls_ptr.Pointer()->Set(this); |
| 138 | 142 |
| 139 // TODO(rvargas): Get rid of the OS guards. | 143 // TODO(rvargas): Get rid of the OS guards. |
| 140 #if defined(OS_WIN) | 144 #if defined(OS_WIN) |
| 141 #define MESSAGE_PUMP_UI new base::MessagePumpForUI() | 145 #define MESSAGE_PUMP_UI new base::MessagePumpForUI() |
| 142 #define MESSAGE_PUMP_IO new base::MessagePumpForIO() | 146 #define MESSAGE_PUMP_IO new base::MessagePumpForIO() |
| 143 #elif defined(OS_MACOSX) | 147 #elif defined(OS_MACOSX) |
| 144 #define MESSAGE_PUMP_UI base::MessagePumpMac::Create() | 148 #define MESSAGE_PUMP_UI base::MessagePumpMac::Create() |
| 145 #define MESSAGE_PUMP_IO new base::MessagePumpLibevent() | 149 #define MESSAGE_PUMP_IO new base::MessagePumpLibevent() |
| 150 #elif defined(OS_ANDROID) |
| 151 #define MESSAGE_PUMP_UI new base::MessagePumpForUI() |
| 152 #define MESSAGE_PUMP_IO new base::MessagePumpLibevent() |
| 146 #elif defined(TOUCH_UI) | 153 #elif defined(TOUCH_UI) |
| 147 #define MESSAGE_PUMP_UI new base::MessagePumpX() | 154 #define MESSAGE_PUMP_UI new base::MessagePumpX() |
| 148 #define MESSAGE_PUMP_IO new base::MessagePumpLibevent() | 155 #define MESSAGE_PUMP_IO new base::MessagePumpLibevent() |
| 149 #elif defined(OS_NACL) | 156 #elif defined(OS_NACL) |
| 150 // Currently NaCl doesn't have a UI or an IO MessageLoop. | 157 // Currently NaCl doesn't have a UI or an IO MessageLoop. |
| 151 // TODO(abarth): Figure out if we need these. | 158 // TODO(abarth): Figure out if we need these. |
| 152 #define MESSAGE_PUMP_UI NULL | 159 #define MESSAGE_PUMP_UI NULL |
| 153 #define MESSAGE_PUMP_IO NULL | 160 #define MESSAGE_PUMP_IO NULL |
| 154 #elif defined(OS_POSIX) // POSIX but not MACOSX. | 161 #elif defined(OS_POSIX) // POSIX but not MACOSX. |
| 155 #define MESSAGE_PUMP_UI new base::MessagePumpGtk() | 162 #define MESSAGE_PUMP_UI new base::MessagePumpGtk() |
| 156 #define MESSAGE_PUMP_IO new base::MessagePumpLibevent() | 163 #define MESSAGE_PUMP_IO new base::MessagePumpLibevent() |
| 157 #else | 164 #else |
| 158 #error Not implemented | 165 #error Not implemented |
| 159 #endif | 166 #endif |
| 160 | 167 |
| 161 if (type_ == TYPE_UI) { | 168 if (type_ == TYPE_UI) { |
| 162 pump_ = MESSAGE_PUMP_UI; | 169 if (message_pump_for_ui_factory_) |
| 170 pump_ = message_pump_for_ui_factory_(); |
| 171 else |
| 172 pump_ = MESSAGE_PUMP_UI; |
| 163 } else if (type_ == TYPE_IO) { | 173 } else if (type_ == TYPE_IO) { |
| 164 pump_ = MESSAGE_PUMP_IO; | 174 pump_ = MESSAGE_PUMP_IO; |
| 165 } else { | 175 } else { |
| 166 DCHECK_EQ(TYPE_DEFAULT, type_); | 176 DCHECK_EQ(TYPE_DEFAULT, type_); |
| 167 pump_ = new base::MessagePumpDefault(); | 177 pump_ = new base::MessagePumpDefault(); |
| 168 } | 178 } |
| 169 } | 179 } |
| 170 | 180 |
| 171 MessageLoop::~MessageLoop() { | 181 MessageLoop::~MessageLoop() { |
| 172 DCHECK_EQ(this, current()); | 182 DCHECK_EQ(this, current()); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 214 // when they have no intention of using us. | 224 // when they have no intention of using us. |
| 215 // DCHECK(loop) << "Ouch, did you forget to initialize me?"; | 225 // DCHECK(loop) << "Ouch, did you forget to initialize me?"; |
| 216 return lazy_tls_ptr.Pointer()->Get(); | 226 return lazy_tls_ptr.Pointer()->Get(); |
| 217 } | 227 } |
| 218 | 228 |
| 219 // static | 229 // static |
| 220 void MessageLoop::EnableHistogrammer(bool enable) { | 230 void MessageLoop::EnableHistogrammer(bool enable) { |
| 221 enable_histogrammer_ = enable; | 231 enable_histogrammer_ = enable; |
| 222 } | 232 } |
| 223 | 233 |
| 234 // static |
| 235 void MessageLoop::InitMessagePumpForUIFactory(MessagePumpFactory* factory) { |
| 236 DCHECK(!message_pump_for_ui_factory_); |
| 237 message_pump_for_ui_factory_ = factory; |
| 238 } |
| 239 |
| 224 void MessageLoop::AddDestructionObserver( | 240 void MessageLoop::AddDestructionObserver( |
| 225 DestructionObserver* destruction_observer) { | 241 DestructionObserver* destruction_observer) { |
| 226 DCHECK_EQ(this, current()); | 242 DCHECK_EQ(this, current()); |
| 227 destruction_observers_.AddObserver(destruction_observer); | 243 destruction_observers_.AddObserver(destruction_observer); |
| 228 } | 244 } |
| 229 | 245 |
| 230 void MessageLoop::RemoveDestructionObserver( | 246 void MessageLoop::RemoveDestructionObserver( |
| 231 DestructionObserver* destruction_observer) { | 247 DestructionObserver* destruction_observer) { |
| 232 DCHECK_EQ(this, current()); | 248 DCHECK_EQ(this, current()); |
| 233 destruction_observers_.RemoveObserver(destruction_observer); | 249 destruction_observers_.RemoveObserver(destruction_observer); |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 403 } | 419 } |
| 404 return; | 420 return; |
| 405 } | 421 } |
| 406 #endif | 422 #endif |
| 407 | 423 |
| 408 void MessageLoop::RunInternal() { | 424 void MessageLoop::RunInternal() { |
| 409 DCHECK_EQ(this, current()); | 425 DCHECK_EQ(this, current()); |
| 410 | 426 |
| 411 StartHistogrammer(); | 427 StartHistogrammer(); |
| 412 | 428 |
| 413 #if !defined(OS_MACOSX) | 429 #if !defined(OS_MACOSX) && !defined(OS_ANDROID) |
| 414 if (state_->dispatcher && type() == TYPE_UI) { | 430 if (state_->dispatcher && type() == TYPE_UI) { |
| 415 static_cast<base::MessagePumpForUI*>(pump_.get())-> | 431 static_cast<base::MessagePumpForUI*>(pump_.get())-> |
| 416 RunWithDispatcher(this, state_->dispatcher); | 432 RunWithDispatcher(this, state_->dispatcher); |
| 417 return; | 433 return; |
| 418 } | 434 } |
| 419 #endif | 435 #endif |
| 420 | 436 |
| 421 pump_->Run(this); | 437 pump_->Run(this); |
| 422 } | 438 } |
| 423 | 439 |
| (...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 717 previous_state_ = loop_->state_; | 733 previous_state_ = loop_->state_; |
| 718 if (previous_state_) { | 734 if (previous_state_) { |
| 719 run_depth = previous_state_->run_depth + 1; | 735 run_depth = previous_state_->run_depth + 1; |
| 720 } else { | 736 } else { |
| 721 run_depth = 1; | 737 run_depth = 1; |
| 722 } | 738 } |
| 723 loop_->state_ = this; | 739 loop_->state_ = this; |
| 724 | 740 |
| 725 // Initialize the other fields: | 741 // Initialize the other fields: |
| 726 quit_received = false; | 742 quit_received = false; |
| 727 #if !defined(OS_MACOSX) | 743 #if !defined(OS_MACOSX) && !defined(OS_ANDROID) |
| 728 dispatcher = NULL; | 744 dispatcher = NULL; |
| 729 #endif | 745 #endif |
| 730 } | 746 } |
| 731 | 747 |
| 732 MessageLoop::AutoRunState::~AutoRunState() { | 748 MessageLoop::AutoRunState::~AutoRunState() { |
| 733 loop_->state_ = previous_state_; | 749 loop_->state_ = previous_state_; |
| 734 } | 750 } |
| 735 | 751 |
| 736 //------------------------------------------------------------------------------ | 752 //------------------------------------------------------------------------------ |
| 737 // MessageLoop::PendingTask | 753 // MessageLoop::PendingTask |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 773 | 789 |
| 774 //------------------------------------------------------------------------------ | 790 //------------------------------------------------------------------------------ |
| 775 // MessageLoopForUI | 791 // MessageLoopForUI |
| 776 | 792 |
| 777 #if defined(OS_WIN) | 793 #if defined(OS_WIN) |
| 778 void MessageLoopForUI::DidProcessMessage(const MSG& message) { | 794 void MessageLoopForUI::DidProcessMessage(const MSG& message) { |
| 779 pump_win()->DidProcessMessage(message); | 795 pump_win()->DidProcessMessage(message); |
| 780 } | 796 } |
| 781 #endif // defined(OS_WIN) | 797 #endif // defined(OS_WIN) |
| 782 | 798 |
| 783 #if !defined(OS_MACOSX) && !defined(OS_NACL) | 799 #if defined(OS_ANDROID) |
| 800 void MessageLoopForUI::Start() { |
| 801 // No Histogram support for UI message loop as it is managed by Java side |
| 802 static_cast<base::MessagePumpForUI*>(pump_.get())->Start(this); |
| 803 } |
| 804 #endif |
| 805 |
| 806 #if !defined(OS_MACOSX) && !defined(OS_NACL) && !defined(OS_ANDROID) |
| 784 void MessageLoopForUI::AddObserver(Observer* observer) { | 807 void MessageLoopForUI::AddObserver(Observer* observer) { |
| 785 pump_ui()->AddObserver(observer); | 808 pump_ui()->AddObserver(observer); |
| 786 } | 809 } |
| 787 | 810 |
| 788 void MessageLoopForUI::RemoveObserver(Observer* observer) { | 811 void MessageLoopForUI::RemoveObserver(Observer* observer) { |
| 789 pump_ui()->RemoveObserver(observer); | 812 pump_ui()->RemoveObserver(observer); |
| 790 } | 813 } |
| 791 | 814 |
| 792 void MessageLoopForUI::Run(Dispatcher* dispatcher) { | 815 void MessageLoopForUI::Run(Dispatcher* dispatcher) { |
| 793 AutoRunState save_state(this); | 816 AutoRunState save_state(this); |
| 794 state_->dispatcher = dispatcher; | 817 state_->dispatcher = dispatcher; |
| 795 RunHandler(); | 818 RunHandler(); |
| 796 } | 819 } |
| 797 #endif // !defined(OS_MACOSX) && !defined(OS_NACL) | 820 #endif // !defined(OS_MACOSX) && !defined(OS_NACL) && !defined(OS_ANDROID) |
| 798 | 821 |
| 799 //------------------------------------------------------------------------------ | 822 //------------------------------------------------------------------------------ |
| 800 // MessageLoopForIO | 823 // MessageLoopForIO |
| 801 | 824 |
| 802 #if defined(OS_WIN) | 825 #if defined(OS_WIN) |
| 803 | 826 |
| 804 void MessageLoopForIO::RegisterIOHandler(HANDLE file, IOHandler* handler) { | 827 void MessageLoopForIO::RegisterIOHandler(HANDLE file, IOHandler* handler) { |
| 805 pump_io()->RegisterIOHandler(file, handler); | 828 pump_io()->RegisterIOHandler(file, handler); |
| 806 } | 829 } |
| 807 | 830 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 818 Watcher *delegate) { | 841 Watcher *delegate) { |
| 819 return pump_libevent()->WatchFileDescriptor( | 842 return pump_libevent()->WatchFileDescriptor( |
| 820 fd, | 843 fd, |
| 821 persistent, | 844 persistent, |
| 822 static_cast<base::MessagePumpLibevent::Mode>(mode), | 845 static_cast<base::MessagePumpLibevent::Mode>(mode), |
| 823 controller, | 846 controller, |
| 824 delegate); | 847 delegate); |
| 825 } | 848 } |
| 826 | 849 |
| 827 #endif | 850 #endif |
| OLD | NEW |