| 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 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 // Returns true if MessagePump::ScheduleWork() must be called one | 91 // Returns true if MessagePump::ScheduleWork() must be called one |
| 92 // 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. |
| 93 bool AlwaysNotifyPump(MessageLoop::Type type) { | 93 bool AlwaysNotifyPump(MessageLoop::Type type) { |
| 94 #if defined(OS_ANDROID) | 94 #if defined(OS_ANDROID) |
| 95 return type == MessageLoop::TYPE_UI || type == MessageLoop::TYPE_JAVA; | 95 return type == MessageLoop::TYPE_UI || type == MessageLoop::TYPE_JAVA; |
| 96 #else | 96 #else |
| 97 return false; | 97 return false; |
| 98 #endif | 98 #endif |
| 99 } | 99 } |
| 100 | 100 |
| 101 MessagePump* CreateMessagePump(MessageLoop::Type type) { |
| 102 // TODO(rvargas): Get rid of the OS guards. |
| 103 #if defined(OS_WIN) |
| 104 #define MESSAGE_PUMP_UI new MessagePumpForUI() |
| 105 #define MESSAGE_PUMP_IO new MessagePumpForIO() |
| 106 #elif defined(OS_IOS) |
| 107 #define MESSAGE_PUMP_UI MessagePumpMac::Create() |
| 108 #define MESSAGE_PUMP_IO new MessagePumpIOSForIO() |
| 109 #elif defined(OS_MACOSX) |
| 110 #define MESSAGE_PUMP_UI MessagePumpMac::Create() |
| 111 #define MESSAGE_PUMP_IO new MessagePumpLibevent() |
| 112 #elif defined(OS_NACL) |
| 113 // Currently NaCl doesn't have a UI MessageLoop. |
| 114 // TODO(abarth): Figure out if we need this. |
| 115 #define MESSAGE_PUMP_UI NULL |
| 116 // ipc_channel_nacl.cc uses a worker thread to do socket reads currently, and |
| 117 // doesn't require extra support for watching file descriptors. |
| 118 #define MESSAGE_PUMP_IO new MessagePumpDefault() |
| 119 #elif defined(OS_POSIX) // POSIX but not MACOSX. |
| 120 #define MESSAGE_PUMP_UI new MessagePumpForUI() |
| 121 #define MESSAGE_PUMP_IO new MessagePumpLibevent() |
| 122 #else |
| 123 #error Not implemented |
| 124 #endif |
| 125 |
| 126 if (type == MessageLoop::TYPE_UI) { |
| 127 if (message_pump_for_ui_factory_) |
| 128 return message_pump_for_ui_factory_(); |
| 129 return MESSAGE_PUMP_UI; |
| 130 } |
| 131 if (type == MessageLoop::TYPE_IO) |
| 132 return MESSAGE_PUMP_IO; |
| 133 #if defined(TOOLKIT_GTK) |
| 134 if (type == MessageLoop::TYPE_GPU) |
| 135 return new MessagePumpX11(); |
| 136 #endif |
| 137 #if defined(OS_ANDROID) |
| 138 if (type == MessageLoop::TYPE_JAVA) |
| 139 return MESSAGE_PUMP_UI; |
| 140 #endif |
| 141 DCHECK_EQ(MessageLoop::TYPE_DEFAULT, type); |
| 142 return new MessagePumpDefault(); |
| 143 } |
| 144 |
| 101 } // namespace | 145 } // namespace |
| 102 | 146 |
| 103 //------------------------------------------------------------------------------ | 147 //------------------------------------------------------------------------------ |
| 104 | 148 |
| 105 #if defined(OS_WIN) | 149 #if defined(OS_WIN) |
| 106 | 150 |
| 107 // Upon a SEH exception in this thread, it restores the original unhandled | 151 // Upon a SEH exception in this thread, it restores the original unhandled |
| 108 // exception filter. | 152 // exception filter. |
| 109 static int SEHFilter(LPTOP_LEVEL_EXCEPTION_FILTER old_filter) { | 153 static int SEHFilter(LPTOP_LEVEL_EXCEPTION_FILTER old_filter) { |
| 110 ::SetUnhandledExceptionFilter(old_filter); | 154 ::SetUnhandledExceptionFilter(old_filter); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 128 } | 172 } |
| 129 | 173 |
| 130 MessageLoop::TaskObserver::~TaskObserver() { | 174 MessageLoop::TaskObserver::~TaskObserver() { |
| 131 } | 175 } |
| 132 | 176 |
| 133 MessageLoop::DestructionObserver::~DestructionObserver() { | 177 MessageLoop::DestructionObserver::~DestructionObserver() { |
| 134 } | 178 } |
| 135 | 179 |
| 136 //------------------------------------------------------------------------------ | 180 //------------------------------------------------------------------------------ |
| 137 | 181 |
| 138 MessageLoop::MessageLoop(Type type) | 182 MessageLoop::MessageLoop(Type type, MessagePump* message_pump) |
| 139 : type_(type), | 183 : pump_(message_pump), |
| 184 type_(type), |
| 140 exception_restoration_(false), | 185 exception_restoration_(false), |
| 141 nestable_tasks_allowed_(true), | 186 nestable_tasks_allowed_(true), |
| 142 #if defined(OS_WIN) | 187 #if defined(OS_WIN) |
| 143 os_modal_loop_(false), | 188 os_modal_loop_(false), |
| 144 #endif // OS_WIN | 189 #endif // OS_WIN |
| 145 message_histogram_(NULL), | 190 message_histogram_(NULL), |
| 146 run_loop_(NULL) { | 191 run_loop_(NULL) { |
| 147 DCHECK(!current()) << "should only have one message loop per thread"; | 192 DCHECK(!current()) << "should only have one message loop per thread"; |
| 148 lazy_tls_ptr.Pointer()->Set(this); | 193 lazy_tls_ptr.Pointer()->Set(this); |
| 149 | 194 |
| 150 incoming_task_queue_ = new internal::IncomingTaskQueue(this); | 195 incoming_task_queue_ = new internal::IncomingTaskQueue(this); |
| 151 message_loop_proxy_ = | 196 message_loop_proxy_ = |
| 152 new internal::MessageLoopProxyImpl(incoming_task_queue_); | 197 new internal::MessageLoopProxyImpl(incoming_task_queue_); |
| 153 thread_task_runner_handle_.reset( | 198 thread_task_runner_handle_.reset( |
| 154 new ThreadTaskRunnerHandle(message_loop_proxy_)); | 199 new ThreadTaskRunnerHandle(message_loop_proxy_)); |
| 155 | 200 |
| 156 // TODO(rvargas): Get rid of the OS guards. | 201 if (!pump_.get()) |
| 157 #if defined(OS_WIN) | 202 pump_.reset(CreateMessagePump(type)); |
| 158 #define MESSAGE_PUMP_UI new MessagePumpForUI() | |
| 159 #define MESSAGE_PUMP_IO new MessagePumpForIO() | |
| 160 #elif defined(OS_IOS) | |
| 161 #define MESSAGE_PUMP_UI MessagePumpMac::Create() | |
| 162 #define MESSAGE_PUMP_IO new MessagePumpIOSForIO() | |
| 163 #elif defined(OS_MACOSX) | |
| 164 #define MESSAGE_PUMP_UI MessagePumpMac::Create() | |
| 165 #define MESSAGE_PUMP_IO new MessagePumpLibevent() | |
| 166 #elif defined(OS_NACL) | |
| 167 // Currently NaCl doesn't have a UI MessageLoop. | |
| 168 // TODO(abarth): Figure out if we need this. | |
| 169 #define MESSAGE_PUMP_UI NULL | |
| 170 // ipc_channel_nacl.cc uses a worker thread to do socket reads currently, and | |
| 171 // doesn't require extra support for watching file descriptors. | |
| 172 #define MESSAGE_PUMP_IO new MessagePumpDefault() | |
| 173 #elif defined(OS_POSIX) // POSIX but not MACOSX. | |
| 174 #define MESSAGE_PUMP_UI new MessagePumpForUI() | |
| 175 #define MESSAGE_PUMP_IO new MessagePumpLibevent() | |
| 176 #else | |
| 177 #error Not implemented | |
| 178 #endif | |
| 179 | |
| 180 if (type_ == TYPE_UI) { | |
| 181 if (message_pump_for_ui_factory_) | |
| 182 pump_.reset(message_pump_for_ui_factory_()); | |
| 183 else | |
| 184 pump_.reset(MESSAGE_PUMP_UI); | |
| 185 } else if (type_ == TYPE_IO) { | |
| 186 pump_.reset(MESSAGE_PUMP_IO); | |
| 187 #if defined(TOOLKIT_GTK) | |
| 188 } else if (type_ == TYPE_GPU) { | |
| 189 pump_.reset(new MessagePumpX11()); | |
| 190 #endif | |
| 191 #if defined(OS_ANDROID) | |
| 192 } else if (type_ == TYPE_JAVA) { | |
| 193 pump_.reset(MESSAGE_PUMP_UI); | |
| 194 #endif | |
| 195 } else { | |
| 196 DCHECK_EQ(TYPE_DEFAULT, type_); | |
| 197 pump_.reset(new MessagePumpDefault()); | |
| 198 } | |
| 199 } | 203 } |
| 200 | 204 |
| 201 MessageLoop::~MessageLoop() { | 205 MessageLoop::~MessageLoop() { |
| 202 DCHECK_EQ(this, current()); | 206 DCHECK_EQ(this, current()); |
| 203 | 207 |
| 204 DCHECK(!run_loop_); | 208 DCHECK(!run_loop_); |
| 205 | 209 |
| 206 // Clean up any unprocessed tasks, but take care: deleting a task could | 210 // Clean up any unprocessed tasks, but take care: deleting a task could |
| 207 // result in the addition of more tasks (e.g., via DeleteSoon). We set a | 211 // result in the addition of more tasks (e.g., via DeleteSoon). We set a |
| 208 // limit on the number of times we will allow a deleted task to generate more | 212 // limit on the number of times we will allow a deleted task to generate more |
| (...skipping 553 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 762 fd, | 766 fd, |
| 763 persistent, | 767 persistent, |
| 764 mode, | 768 mode, |
| 765 controller, | 769 controller, |
| 766 delegate); | 770 delegate); |
| 767 } | 771 } |
| 768 | 772 |
| 769 #endif | 773 #endif |
| 770 | 774 |
| 771 } // namespace base | 775 } // namespace base |
| OLD | NEW |