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 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 : type_(type), | 139 : type_(type), |
140 exception_restoration_(false), | 140 exception_restoration_(false), |
141 nestable_tasks_allowed_(true), | 141 nestable_tasks_allowed_(true), |
142 #if defined(OS_WIN) | 142 #if defined(OS_WIN) |
143 os_modal_loop_(false), | 143 os_modal_loop_(false), |
144 #endif // OS_WIN | 144 #endif // OS_WIN |
145 message_histogram_(NULL), | 145 message_histogram_(NULL), |
146 run_loop_(NULL) { | 146 run_loop_(NULL) { |
147 Init(); | 147 Init(); |
148 | 148 |
149 // TODO(rvargas): Get rid of the OS guards. | 149 pump_.reset(CreateMessagePumpForType(type)); |
150 #if defined(OS_WIN) | |
151 #define MESSAGE_PUMP_UI new MessagePumpForUI() | |
152 #define MESSAGE_PUMP_IO new MessagePumpForIO() | |
153 #elif defined(OS_IOS) | |
154 #define MESSAGE_PUMP_UI MessagePumpMac::Create() | |
155 #define MESSAGE_PUMP_IO new MessagePumpIOSForIO() | |
156 #elif defined(OS_MACOSX) | |
157 #define MESSAGE_PUMP_UI MessagePumpMac::Create() | |
158 #define MESSAGE_PUMP_IO new MessagePumpLibevent() | |
159 #elif defined(OS_NACL) | |
160 // Currently NaCl doesn't have a UI MessageLoop. | |
161 // TODO(abarth): Figure out if we need this. | |
162 #define MESSAGE_PUMP_UI NULL | |
163 // ipc_channel_nacl.cc uses a worker thread to do socket reads currently, and | |
164 // doesn't require extra support for watching file descriptors. | |
165 #define MESSAGE_PUMP_IO new MessagePumpDefault() | |
166 #elif defined(OS_POSIX) // POSIX but not MACOSX. | |
167 #define MESSAGE_PUMP_UI new MessagePumpForUI() | |
168 #define MESSAGE_PUMP_IO new MessagePumpLibevent() | |
169 #else | |
170 #error Not implemented | |
171 #endif | |
172 | |
173 if (type_ == TYPE_UI) { | |
174 if (message_pump_for_ui_factory_) | |
175 pump_.reset(message_pump_for_ui_factory_()); | |
176 else | |
177 pump_.reset(MESSAGE_PUMP_UI); | |
178 } else if (type_ == TYPE_IO) { | |
179 pump_.reset(MESSAGE_PUMP_IO); | |
180 #if defined(TOOLKIT_GTK) | |
181 } else if (type_ == TYPE_GPU) { | |
182 pump_.reset(new MessagePumpX11()); | |
183 #endif | |
184 #if defined(OS_ANDROID) | |
185 } else if (type_ == TYPE_JAVA) { | |
186 pump_.reset(MESSAGE_PUMP_UI); | |
187 #endif | |
188 } else { | |
189 DCHECK_EQ(TYPE_DEFAULT, type_); | |
190 pump_.reset(new MessagePumpDefault()); | |
191 } | |
192 } | 150 } |
193 | 151 |
194 MessageLoop::MessageLoop(scoped_ptr<MessagePump> pump) | 152 MessageLoop::MessageLoop(scoped_ptr<MessagePump> pump) |
195 : pump_(pump.Pass()), | 153 : pump_(pump.Pass()), |
196 type_(TYPE_CUSTOM), | 154 type_(TYPE_CUSTOM), |
197 exception_restoration_(false), | 155 exception_restoration_(false), |
198 nestable_tasks_allowed_(true), | 156 nestable_tasks_allowed_(true), |
199 #if defined(OS_WIN) | 157 #if defined(OS_WIN) |
200 os_modal_loop_(false), | 158 os_modal_loop_(false), |
201 #endif // OS_WIN | 159 #endif // OS_WIN |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
257 | 215 |
258 // static | 216 // static |
259 bool MessageLoop::InitMessagePumpForUIFactory(MessagePumpFactory* factory) { | 217 bool MessageLoop::InitMessagePumpForUIFactory(MessagePumpFactory* factory) { |
260 if (message_pump_for_ui_factory_) | 218 if (message_pump_for_ui_factory_) |
261 return false; | 219 return false; |
262 | 220 |
263 message_pump_for_ui_factory_ = factory; | 221 message_pump_for_ui_factory_ = factory; |
264 return true; | 222 return true; |
265 } | 223 } |
266 | 224 |
| 225 // static |
| 226 MessagePump* MessageLoop::CreateMessagePumpForType(Type type) { |
| 227 // TODO(rvargas): Get rid of the OS guards. |
| 228 #if defined(OS_WIN) |
| 229 #define MESSAGE_PUMP_UI new MessagePumpForUI() |
| 230 #define MESSAGE_PUMP_IO new MessagePumpForIO() |
| 231 #elif defined(OS_IOS) |
| 232 #define MESSAGE_PUMP_UI MessagePumpMac::Create() |
| 233 #define MESSAGE_PUMP_IO new MessagePumpIOSForIO() |
| 234 #elif defined(OS_MACOSX) |
| 235 #define MESSAGE_PUMP_UI MessagePumpMac::Create() |
| 236 #define MESSAGE_PUMP_IO new MessagePumpLibevent() |
| 237 #elif defined(OS_NACL) |
| 238 // Currently NaCl doesn't have a UI MessageLoop. |
| 239 // TODO(abarth): Figure out if we need this. |
| 240 #define MESSAGE_PUMP_UI NULL |
| 241 // ipc_channel_nacl.cc uses a worker thread to do socket reads currently, and |
| 242 // doesn't require extra support for watching file descriptors. |
| 243 #define MESSAGE_PUMP_IO new MessagePumpDefault() |
| 244 #elif defined(OS_POSIX) // POSIX but not MACOSX. |
| 245 #define MESSAGE_PUMP_UI new MessagePumpForUI() |
| 246 #define MESSAGE_PUMP_IO new MessagePumpLibevent() |
| 247 #else |
| 248 #error Not implemented |
| 249 #endif |
| 250 |
| 251 if (type == MessageLoop::TYPE_UI) { |
| 252 if (message_pump_for_ui_factory_) |
| 253 return message_pump_for_ui_factory_(); |
| 254 return MESSAGE_PUMP_UI; |
| 255 } |
| 256 if (type == MessageLoop::TYPE_IO) |
| 257 return MESSAGE_PUMP_IO; |
| 258 #if defined(TOOLKIT_GTK) |
| 259 if (type == MessageLoop::TYPE_GPU) |
| 260 return new MessagePumpX11(); |
| 261 #endif |
| 262 #if defined(OS_ANDROID) |
| 263 if (type == MessageLoop::TYPE_JAVA) |
| 264 return MESSAGE_PUMP_UI; |
| 265 #endif |
| 266 DCHECK_EQ(MessageLoop::TYPE_DEFAULT, type); |
| 267 return new MessagePumpDefault(); |
| 268 } |
| 269 |
267 void MessageLoop::AddDestructionObserver( | 270 void MessageLoop::AddDestructionObserver( |
268 DestructionObserver* destruction_observer) { | 271 DestructionObserver* destruction_observer) { |
269 DCHECK_EQ(this, current()); | 272 DCHECK_EQ(this, current()); |
270 destruction_observers_.AddObserver(destruction_observer); | 273 destruction_observers_.AddObserver(destruction_observer); |
271 } | 274 } |
272 | 275 |
273 void MessageLoop::RemoveDestructionObserver( | 276 void MessageLoop::RemoveDestructionObserver( |
274 DestructionObserver* destruction_observer) { | 277 DestructionObserver* destruction_observer) { |
275 DCHECK_EQ(this, current()); | 278 DCHECK_EQ(this, current()); |
276 destruction_observers_.RemoveObserver(destruction_observer); | 279 destruction_observers_.RemoveObserver(destruction_observer); |
(...skipping 503 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
780 fd, | 783 fd, |
781 persistent, | 784 persistent, |
782 mode, | 785 mode, |
783 controller, | 786 controller, |
784 delegate); | 787 delegate); |
785 } | 788 } |
786 | 789 |
787 #endif | 790 #endif |
788 | 791 |
789 } // namespace base | 792 } // namespace base |
OLD | NEW |