brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_ |
| 6 | #define BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_ |
| 7 | |
| 8 | #include <queue> |
| 9 | #include <string> |
| 10 | |
| 11 | #include "base/base_export.h" |
| 12 | #include "base/basictypes.h" |
| 13 | #include "base/callback_forward.h" |
| 14 | #include "base/location.h" |
| 15 | #include "base/memory/ref_counted.h" |
alexeypa@chromium.org | 4018323 | 2013-07-23 07:24:13 +0900 | [diff] [blame] | 16 | #include "base/memory/scoped_ptr.h" |
| 17 | #include "base/message_loop/incoming_task_queue.h" |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 18 | #include "base/message_loop/message_loop_proxy.h" |
alexeypa@chromium.org | 4018323 | 2013-07-23 07:24:13 +0900 | [diff] [blame] | 19 | #include "base/message_loop/message_loop_proxy_impl.h" |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 20 | #include "base/message_loop/message_pump.h" |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 21 | #include "base/observer_list.h" |
| 22 | #include "base/pending_task.h" |
| 23 | #include "base/sequenced_task_runner_helpers.h" |
| 24 | #include "base/synchronization/lock.h" |
avi@chromium.org | b039e8b | 2013-06-28 09:49:07 +0900 | [diff] [blame] | 25 | #include "base/time/time.h" |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 26 | #include "base/tracking_info.h" |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 27 | |
sky@chromium.org | 66a00b3 | 2014-01-17 09:10:29 +0900 | [diff] [blame] | 28 | // TODO(sky): these includes should not be necessary. Nuke them. |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 29 | #if defined(OS_WIN) |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 30 | #include "base/message_loop/message_pump_win.h" |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 31 | #elif defined(OS_IOS) |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 32 | #include "base/message_loop/message_pump_io_ios.h" |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 33 | #elif defined(OS_POSIX) |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 34 | #include "base/message_loop/message_pump_libevent.h" |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 35 | #if !defined(OS_MACOSX) && !defined(OS_ANDROID) |
| 36 | |
| 37 | #if defined(USE_AURA) && defined(USE_X11) && !defined(OS_NACL) |
sadrul@chromium.org | 1999572 | 2013-09-07 12:21:04 +0900 | [diff] [blame] | 38 | #include "base/message_loop/message_pump_x11.h" |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 39 | #elif defined(USE_OZONE) && !defined(OS_NACL) |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 40 | #include "base/message_loop/message_pump_ozone.h" |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 41 | #else |
sadrul@chromium.org | a06ba83 | 2013-09-07 10:13:39 +0900 | [diff] [blame] | 42 | #define USE_GTK_MESSAGE_PUMP |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 43 | #include "base/message_loop/message_pump_gtk.h" |
ccameron@chromium.org | bfe6007 | 2013-09-13 07:51:10 +0900 | [diff] [blame] | 44 | #if defined(TOOLKIT_GTK) |
| 45 | #include "base/message_loop/message_pump_x11.h" |
| 46 | #endif |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 47 | #endif |
| 48 | |
| 49 | #endif |
| 50 | #endif |
| 51 | |
| 52 | namespace base { |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 53 | |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 54 | class HistogramBase; |
sadrul@chromium.org | a06ba83 | 2013-09-07 10:13:39 +0900 | [diff] [blame] | 55 | class MessagePumpObserver; |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 56 | class RunLoop; |
| 57 | class ThreadTaskRunnerHandle; |
| 58 | #if defined(OS_ANDROID) |
| 59 | class MessagePumpForUI; |
| 60 | #endif |
alexeypa@chromium.org | 4018323 | 2013-07-23 07:24:13 +0900 | [diff] [blame] | 61 | class WaitableEvent; |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 62 | |
| 63 | // A MessageLoop is used to process events for a particular thread. There is |
| 64 | // at most one MessageLoop instance per thread. |
| 65 | // |
| 66 | // Events include at a minimum Task instances submitted to PostTask and its |
| 67 | // variants. Depending on the type of message pump used by the MessageLoop |
| 68 | // other events such as UI messages may be processed. On Windows APC calls (as |
| 69 | // time permits) and signals sent to a registered set of HANDLEs may also be |
| 70 | // processed. |
| 71 | // |
| 72 | // NOTE: Unless otherwise specified, a MessageLoop's methods may only be called |
| 73 | // on the thread where the MessageLoop's Run method executes. |
| 74 | // |
| 75 | // NOTE: MessageLoop has task reentrancy protection. This means that if a |
| 76 | // task is being processed, a second task cannot start until the first task is |
| 77 | // finished. Reentrancy can happen when processing a task, and an inner |
| 78 | // message pump is created. That inner pump then processes native messages |
| 79 | // which could implicitly start an inner task. Inner message pumps are created |
| 80 | // with dialogs (DialogBox), common dialogs (GetOpenFileName), OLE functions |
| 81 | // (DoDragDrop), printer functions (StartDoc) and *many* others. |
| 82 | // |
| 83 | // Sample workaround when inner task processing is needed: |
| 84 | // HRESULT hr; |
| 85 | // { |
| 86 | // MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current()); |
| 87 | // hr = DoDragDrop(...); // Implicitly runs a modal message loop. |
| 88 | // } |
| 89 | // // Process |hr| (the result returned by DoDragDrop()). |
| 90 | // |
| 91 | // Please be SURE your task is reentrant (nestable) and all global variables |
| 92 | // are stable and accessible before calling SetNestableTasksAllowed(true). |
| 93 | // |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 94 | class BASE_EXPORT MessageLoop : public MessagePump::Delegate { |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 95 | public: |
| 96 | |
ccameron@chromium.org | 6c63e29 | 2014-01-08 06:42:02 +0900 | [diff] [blame] | 97 | #if defined(USE_AURA) |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 98 | typedef MessagePumpObserver Observer; |
ccameron@chromium.org | 6c63e29 | 2014-01-08 06:42:02 +0900 | [diff] [blame] | 99 | #elif defined(USE_GTK_MESSAGE_PUMP) |
| 100 | typedef MessagePumpGdkObserver Observer; |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 101 | #endif |
| 102 | |
| 103 | // A MessageLoop has a particular type, which indicates the set of |
| 104 | // asynchronous events it may process in addition to tasks and timers. |
| 105 | // |
| 106 | // TYPE_DEFAULT |
| 107 | // This type of ML only supports tasks and timers. |
| 108 | // |
| 109 | // TYPE_UI |
| 110 | // This type of ML also supports native UI events (e.g., Windows messages). |
| 111 | // See also MessageLoopForUI. |
| 112 | // |
ccameron@chromium.org | bfe6007 | 2013-09-13 07:51:10 +0900 | [diff] [blame] | 113 | // TYPE_GPU |
| 114 | // This type of ML also supports native UI events for use in the GPU |
| 115 | // process. On Linux this will always be an X11 ML (as compared with the |
| 116 | // sometimes-GTK ML in the browser process). |
| 117 | // |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 118 | // TYPE_IO |
| 119 | // This type of ML also supports asynchronous IO. See also |
| 120 | // MessageLoopForIO. |
| 121 | // |
kristianm@chromium.org | a78bfa9 | 2013-08-08 10:31:52 +0900 | [diff] [blame] | 122 | // TYPE_JAVA |
| 123 | // This type of ML is backed by a Java message handler which is responsible |
| 124 | // for running the tasks added to the ML. This is only for use on Android. |
| 125 | // TYPE_JAVA behaves in essence like TYPE_UI, except during construction |
| 126 | // where it does not use the main thread specific pump factory. |
| 127 | // |
sky@chromium.org | ab45280 | 2013-11-08 15:16:53 +0900 | [diff] [blame] | 128 | // TYPE_CUSTOM |
| 129 | // MessagePump was supplied to constructor. |
| 130 | // |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 131 | enum Type { |
| 132 | TYPE_DEFAULT, |
| 133 | TYPE_UI, |
sky@chromium.org | ab45280 | 2013-11-08 15:16:53 +0900 | [diff] [blame] | 134 | TYPE_CUSTOM, |
ccameron@chromium.org | bfe6007 | 2013-09-13 07:51:10 +0900 | [diff] [blame] | 135 | #if defined(TOOLKIT_GTK) |
| 136 | TYPE_GPU, |
| 137 | #endif |
kristianm@chromium.org | a78bfa9 | 2013-08-08 10:31:52 +0900 | [diff] [blame] | 138 | TYPE_IO, |
| 139 | #if defined(OS_ANDROID) |
| 140 | TYPE_JAVA, |
| 141 | #endif // defined(OS_ANDROID) |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 142 | }; |
| 143 | |
| 144 | // Normally, it is not necessary to instantiate a MessageLoop. Instead, it |
| 145 | // is typical to make use of the current thread's MessageLoop instance. |
| 146 | explicit MessageLoop(Type type = TYPE_DEFAULT); |
sky@chromium.org | ab45280 | 2013-11-08 15:16:53 +0900 | [diff] [blame] | 147 | // Creates a TYPE_CUSTOM MessageLoop with the supplied MessagePump, which must |
| 148 | // be non-NULL. |
| 149 | explicit MessageLoop(scoped_ptr<base::MessagePump> pump); |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 150 | virtual ~MessageLoop(); |
| 151 | |
| 152 | // Returns the MessageLoop object for the current thread, or null if none. |
| 153 | static MessageLoop* current(); |
| 154 | |
| 155 | static void EnableHistogrammer(bool enable_histogrammer); |
| 156 | |
suyash.s@samsung.com | b5f1fc9 | 2014-03-08 02:03:54 +0900 | [diff] [blame] | 157 | typedef scoped_ptr<MessagePump> (MessagePumpFactory)(); |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 158 | // Uses the given base::MessagePumpForUIFactory to override the default |
| 159 | // MessagePump implementation for 'TYPE_UI'. Returns true if the factory |
| 160 | // was successfully registered. |
| 161 | static bool InitMessagePumpForUIFactory(MessagePumpFactory* factory); |
| 162 | |
sky@chromium.org | 4f42682 | 2013-11-13 01:35:02 +0900 | [diff] [blame] | 163 | // Creates the default MessagePump based on |type|. Caller owns return |
| 164 | // value. |
suyash.s@samsung.com | b5f1fc9 | 2014-03-08 02:03:54 +0900 | [diff] [blame] | 165 | static scoped_ptr<MessagePump> CreateMessagePumpForType(Type type); |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 166 | // A DestructionObserver is notified when the current MessageLoop is being |
| 167 | // destroyed. These observers are notified prior to MessageLoop::current() |
| 168 | // being changed to return NULL. This gives interested parties the chance to |
| 169 | // do final cleanup that depends on the MessageLoop. |
| 170 | // |
| 171 | // NOTE: Any tasks posted to the MessageLoop during this notification will |
| 172 | // not be run. Instead, they will be deleted. |
| 173 | // |
| 174 | class BASE_EXPORT DestructionObserver { |
| 175 | public: |
| 176 | virtual void WillDestroyCurrentMessageLoop() = 0; |
| 177 | |
| 178 | protected: |
| 179 | virtual ~DestructionObserver(); |
| 180 | }; |
| 181 | |
| 182 | // Add a DestructionObserver, which will start receiving notifications |
| 183 | // immediately. |
| 184 | void AddDestructionObserver(DestructionObserver* destruction_observer); |
| 185 | |
| 186 | // Remove a DestructionObserver. It is safe to call this method while a |
| 187 | // DestructionObserver is receiving a notification callback. |
| 188 | void RemoveDestructionObserver(DestructionObserver* destruction_observer); |
| 189 | |
| 190 | // The "PostTask" family of methods call the task's Run method asynchronously |
| 191 | // from within a message loop at some point in the future. |
| 192 | // |
| 193 | // With the PostTask variant, tasks are invoked in FIFO order, inter-mixed |
| 194 | // with normal UI or IO event processing. With the PostDelayedTask variant, |
| 195 | // tasks are called after at least approximately 'delay_ms' have elapsed. |
| 196 | // |
| 197 | // The NonNestable variants work similarly except that they promise never to |
| 198 | // dispatch the task from a nested invocation of MessageLoop::Run. Instead, |
| 199 | // such tasks get deferred until the top-most MessageLoop::Run is executing. |
| 200 | // |
| 201 | // The MessageLoop takes ownership of the Task, and deletes it after it has |
| 202 | // been Run(). |
| 203 | // |
| 204 | // PostTask(from_here, task) is equivalent to |
| 205 | // PostDelayedTask(from_here, task, 0). |
| 206 | // |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 207 | // NOTE: These methods may be called on any thread. The Task will be invoked |
| 208 | // on the thread that executes MessageLoop::Run(). |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 209 | void PostTask(const tracked_objects::Location& from_here, |
| 210 | const Closure& task); |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 211 | |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 212 | void PostDelayedTask(const tracked_objects::Location& from_here, |
| 213 | const Closure& task, |
| 214 | TimeDelta delay); |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 215 | |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 216 | void PostNonNestableTask(const tracked_objects::Location& from_here, |
| 217 | const Closure& task); |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 218 | |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 219 | void PostNonNestableDelayedTask(const tracked_objects::Location& from_here, |
| 220 | const Closure& task, |
| 221 | TimeDelta delay); |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 222 | |
| 223 | // A variant on PostTask that deletes the given object. This is useful |
| 224 | // if the object needs to live until the next run of the MessageLoop (for |
| 225 | // example, deleting a RenderProcessHost from within an IPC callback is not |
| 226 | // good). |
| 227 | // |
| 228 | // NOTE: This method may be called on any thread. The object will be deleted |
| 229 | // on the thread that executes MessageLoop::Run(). If this is not the same |
| 230 | // as the thread that calls PostDelayedTask(FROM_HERE, ), then T MUST inherit |
| 231 | // from RefCountedThreadSafe<T>! |
| 232 | template <class T> |
| 233 | void DeleteSoon(const tracked_objects::Location& from_here, const T* object) { |
| 234 | base::subtle::DeleteHelperInternal<T, void>::DeleteViaSequencedTaskRunner( |
| 235 | this, from_here, object); |
| 236 | } |
| 237 | |
| 238 | // A variant on PostTask that releases the given reference counted object |
| 239 | // (by calling its Release method). This is useful if the object needs to |
| 240 | // live until the next run of the MessageLoop, or if the object needs to be |
| 241 | // released on a particular thread. |
| 242 | // |
| 243 | // NOTE: This method may be called on any thread. The object will be |
| 244 | // released (and thus possibly deleted) on the thread that executes |
| 245 | // MessageLoop::Run(). If this is not the same as the thread that calls |
| 246 | // PostDelayedTask(FROM_HERE, ), then T MUST inherit from |
| 247 | // RefCountedThreadSafe<T>! |
| 248 | template <class T> |
| 249 | void ReleaseSoon(const tracked_objects::Location& from_here, |
| 250 | const T* object) { |
| 251 | base::subtle::ReleaseHelperInternal<T, void>::ReleaseViaSequencedTaskRunner( |
| 252 | this, from_here, object); |
| 253 | } |
| 254 | |
| 255 | // Deprecated: use RunLoop instead. |
| 256 | // Run the message loop. |
| 257 | void Run(); |
| 258 | |
| 259 | // Deprecated: use RunLoop instead. |
| 260 | // Process all pending tasks, windows messages, etc., but don't wait/sleep. |
| 261 | // Return as soon as all items that can be run are taken care of. |
| 262 | void RunUntilIdle(); |
| 263 | |
| 264 | // TODO(jbates) remove this. crbug.com/131220. See QuitWhenIdle(). |
| 265 | void Quit() { QuitWhenIdle(); } |
| 266 | |
| 267 | // Deprecated: use RunLoop instead. |
| 268 | // |
| 269 | // Signals the Run method to return when it becomes idle. It will continue to |
| 270 | // process pending messages and future messages as long as they are enqueued. |
| 271 | // Warning: if the MessageLoop remains busy, it may never quit. Only use this |
| 272 | // Quit method when looping procedures (such as web pages) have been shut |
| 273 | // down. |
| 274 | // |
| 275 | // This method may only be called on the same thread that called Run, and Run |
| 276 | // must still be on the call stack. |
| 277 | // |
| 278 | // Use QuitClosure variants if you need to Quit another thread's MessageLoop, |
| 279 | // but note that doing so is fairly dangerous if the target thread makes |
| 280 | // nested calls to MessageLoop::Run. The problem being that you won't know |
| 281 | // which nested run loop you are quitting, so be careful! |
| 282 | void QuitWhenIdle(); |
| 283 | |
| 284 | // Deprecated: use RunLoop instead. |
| 285 | // |
| 286 | // This method is a variant of Quit, that does not wait for pending messages |
| 287 | // to be processed before returning from Run. |
| 288 | void QuitNow(); |
| 289 | |
| 290 | // TODO(jbates) remove this. crbug.com/131220. See QuitWhenIdleClosure(). |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 291 | static Closure QuitClosure() { return QuitWhenIdleClosure(); } |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 292 | |
| 293 | // Deprecated: use RunLoop instead. |
| 294 | // Construct a Closure that will call QuitWhenIdle(). Useful to schedule an |
| 295 | // arbitrary MessageLoop to QuitWhenIdle. |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 296 | static Closure QuitWhenIdleClosure(); |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 297 | |
| 298 | // Returns true if this loop is |type|. This allows subclasses (especially |
| 299 | // those in tests) to specialize how they are identified. |
| 300 | virtual bool IsType(Type type) const; |
| 301 | |
| 302 | // Returns the type passed to the constructor. |
| 303 | Type type() const { return type_; } |
| 304 | |
| 305 | // Optional call to connect the thread name with this loop. |
| 306 | void set_thread_name(const std::string& thread_name) { |
| 307 | DCHECK(thread_name_.empty()) << "Should not rename this thread!"; |
| 308 | thread_name_ = thread_name; |
| 309 | } |
| 310 | const std::string& thread_name() const { return thread_name_; } |
| 311 | |
| 312 | // Gets the message loop proxy associated with this message loop. |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 313 | scoped_refptr<MessageLoopProxy> message_loop_proxy() { |
alexeypa@chromium.org | 4018323 | 2013-07-23 07:24:13 +0900 | [diff] [blame] | 314 | return message_loop_proxy_; |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | // Enables or disables the recursive task processing. This happens in the case |
| 318 | // of recursive message loops. Some unwanted message loop may occurs when |
| 319 | // using common controls or printer functions. By default, recursive task |
| 320 | // processing is disabled. |
| 321 | // |
| 322 | // Please utilize |ScopedNestableTaskAllower| instead of calling these methods |
| 323 | // directly. In general nestable message loops are to be avoided. They are |
| 324 | // dangerous and difficult to get right, so please use with extreme caution. |
| 325 | // |
| 326 | // The specific case where tasks get queued is: |
| 327 | // - The thread is running a message loop. |
| 328 | // - It receives a task #1 and execute it. |
| 329 | // - The task #1 implicitly start a message loop, like a MessageBox in the |
| 330 | // unit test. This can also be StartDoc or GetSaveFileName. |
| 331 | // - The thread receives a task #2 before or while in this second message |
| 332 | // loop. |
| 333 | // - With NestableTasksAllowed set to true, the task #2 will run right away. |
| 334 | // Otherwise, it will get executed right after task #1 completes at "thread |
| 335 | // message loop level". |
| 336 | void SetNestableTasksAllowed(bool allowed); |
| 337 | bool NestableTasksAllowed() const; |
| 338 | |
| 339 | // Enables nestable tasks on |loop| while in scope. |
| 340 | class ScopedNestableTaskAllower { |
| 341 | public: |
| 342 | explicit ScopedNestableTaskAllower(MessageLoop* loop) |
| 343 | : loop_(loop), |
| 344 | old_state_(loop_->NestableTasksAllowed()) { |
| 345 | loop_->SetNestableTasksAllowed(true); |
| 346 | } |
| 347 | ~ScopedNestableTaskAllower() { |
| 348 | loop_->SetNestableTasksAllowed(old_state_); |
| 349 | } |
| 350 | |
| 351 | private: |
| 352 | MessageLoop* loop_; |
| 353 | bool old_state_; |
| 354 | }; |
| 355 | |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 356 | // Returns true if we are currently running a nested message loop. |
| 357 | bool IsNested(); |
| 358 | |
| 359 | // A TaskObserver is an object that receives task notifications from the |
| 360 | // MessageLoop. |
| 361 | // |
| 362 | // NOTE: A TaskObserver implementation should be extremely fast! |
| 363 | class BASE_EXPORT TaskObserver { |
| 364 | public: |
| 365 | TaskObserver(); |
| 366 | |
| 367 | // This method is called before processing a task. |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 368 | virtual void WillProcessTask(const PendingTask& pending_task) = 0; |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 369 | |
| 370 | // This method is called after processing a task. |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 371 | virtual void DidProcessTask(const PendingTask& pending_task) = 0; |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 372 | |
| 373 | protected: |
| 374 | virtual ~TaskObserver(); |
| 375 | }; |
| 376 | |
| 377 | // These functions can only be called on the same thread that |this| is |
| 378 | // running on. |
| 379 | void AddTaskObserver(TaskObserver* task_observer); |
| 380 | void RemoveTaskObserver(TaskObserver* task_observer); |
| 381 | |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 382 | // When we go into high resolution timer mode, we will stay in hi-res mode |
| 383 | // for at least 1s. |
| 384 | static const int kHighResolutionTimerModeLeaseTimeMs = 1000; |
| 385 | |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 386 | #if defined(OS_WIN) |
| 387 | void set_os_modal_loop(bool os_modal_loop) { |
| 388 | os_modal_loop_ = os_modal_loop; |
| 389 | } |
| 390 | |
| 391 | bool os_modal_loop() const { |
| 392 | return os_modal_loop_; |
| 393 | } |
| 394 | #endif // OS_WIN |
| 395 | |
| 396 | // Can only be called from the thread that owns the MessageLoop. |
| 397 | bool is_running() const; |
| 398 | |
alexeypa@chromium.org | 4018323 | 2013-07-23 07:24:13 +0900 | [diff] [blame] | 399 | // Returns true if the message loop has high resolution timers enabled. |
| 400 | // Provided for testing. |
| 401 | bool IsHighResolutionTimerEnabledForTesting(); |
| 402 | |
| 403 | // Returns true if the message loop is "idle". Provided for testing. |
| 404 | bool IsIdleForTesting(); |
| 405 | |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 406 | //---------------------------------------------------------------------------- |
| 407 | protected: |
| 408 | |
| 409 | #if defined(OS_WIN) |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 410 | MessagePumpWin* pump_win() { |
| 411 | return static_cast<MessagePumpWin*>(pump_.get()); |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 412 | } |
| 413 | #elif defined(OS_POSIX) && !defined(OS_IOS) |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 414 | MessagePumpLibevent* pump_libevent() { |
| 415 | return static_cast<MessagePumpLibevent*>(pump_.get()); |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 416 | } |
ccameron@chromium.org | bfe6007 | 2013-09-13 07:51:10 +0900 | [diff] [blame] | 417 | #if defined(TOOLKIT_GTK) |
| 418 | friend class MessagePumpX11; |
| 419 | MessagePumpX11* pump_gpu() { |
| 420 | DCHECK_EQ(TYPE_GPU, type()); |
| 421 | return static_cast<MessagePumpX11*>(pump_.get()); |
| 422 | } |
| 423 | #endif |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 424 | #endif |
| 425 | |
alexeypa@chromium.org | 4018323 | 2013-07-23 07:24:13 +0900 | [diff] [blame] | 426 | scoped_ptr<MessagePump> pump_; |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 427 | |
| 428 | private: |
alexeypa@chromium.org | 4018323 | 2013-07-23 07:24:13 +0900 | [diff] [blame] | 429 | friend class internal::IncomingTaskQueue; |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 430 | friend class RunLoop; |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 431 | |
sky@chromium.org | ab45280 | 2013-11-08 15:16:53 +0900 | [diff] [blame] | 432 | // Configures various members for the two constructors. |
| 433 | void Init(); |
| 434 | |
cpu@chromium.org | 33353ba | 2014-01-04 06:25:26 +0900 | [diff] [blame] | 435 | // Invokes the actual run loop using the message pump. |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 436 | void RunHandler(); |
| 437 | |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 438 | // Called to process any delayed non-nestable tasks. |
| 439 | bool ProcessNextDelayedNonNestableTask(); |
| 440 | |
| 441 | // Runs the specified PendingTask. |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 442 | void RunTask(const PendingTask& pending_task); |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 443 | |
| 444 | // Calls RunTask or queues the pending_task on the deferred task list if it |
| 445 | // cannot be run right now. Returns true if the task was run. |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 446 | bool DeferOrRunPendingTask(const PendingTask& pending_task); |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 447 | |
| 448 | // Adds the pending task to delayed_work_queue_. |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 449 | void AddToDelayedWorkQueue(const PendingTask& pending_task); |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 450 | |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 451 | // Delete tasks that haven't run yet without running them. Used in the |
| 452 | // destructor to make sure all the task's destructors get called. Returns |
| 453 | // true if some work was done. |
| 454 | bool DeletePendingTasks(); |
| 455 | |
alexeypa@chromium.org | 4018323 | 2013-07-23 07:24:13 +0900 | [diff] [blame] | 456 | // Creates a process-wide unique ID to represent this task in trace events. |
| 457 | // This will be mangled with a Process ID hash to reduce the likelyhood of |
| 458 | // colliding with MessageLoop pointers on other processes. |
| 459 | uint64 GetTaskTraceID(const PendingTask& task); |
| 460 | |
| 461 | // Loads tasks from the incoming queue to |work_queue_| if the latter is |
| 462 | // empty. |
| 463 | void ReloadWorkQueue(); |
| 464 | |
| 465 | // Wakes up the message pump. Can be called on any thread. The caller is |
| 466 | // responsible for synchronizing ScheduleWork() calls. |
| 467 | void ScheduleWork(bool was_empty); |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 468 | |
| 469 | // Start recording histogram info about events and action IF it was enabled |
| 470 | // and IF the statistics recorder can accept a registration of our histogram. |
| 471 | void StartHistogrammer(); |
| 472 | |
| 473 | // Add occurrence of event to our histogram, so that we can see what is being |
| 474 | // done in a specific MessageLoop instance (i.e., specific thread). |
| 475 | // If message_histogram_ is NULL, this is a no-op. |
| 476 | void HistogramEvent(int event); |
| 477 | |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 478 | // MessagePump::Delegate methods: |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 479 | virtual bool DoWork() OVERRIDE; |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 480 | virtual bool DoDelayedWork(TimeTicks* next_delayed_work_time) OVERRIDE; |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 481 | virtual bool DoIdleWork() OVERRIDE; |
rsesek@chromium.org | ee8420d | 2013-09-05 23:53:12 +0900 | [diff] [blame] | 482 | virtual void GetQueueingInformation(size_t* queue_size, |
| 483 | TimeDelta* queueing_delay) OVERRIDE; |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 484 | |
sky@chromium.org | ab45280 | 2013-11-08 15:16:53 +0900 | [diff] [blame] | 485 | const Type type_; |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 486 | |
| 487 | // A list of tasks that need to be processed by this instance. Note that |
| 488 | // this queue is only accessed (push/pop) by our current thread. |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 489 | TaskQueue work_queue_; |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 490 | |
| 491 | // Contains delayed tasks, sorted by their 'delayed_run_time' property. |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 492 | DelayedTaskQueue delayed_work_queue_; |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 493 | |
| 494 | // A recent snapshot of Time::Now(), used to check delayed_work_queue_. |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 495 | TimeTicks recent_time_; |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 496 | |
| 497 | // A queue of non-nestable tasks that we had to defer because when it came |
| 498 | // time to execute them we were in a nested message loop. They will execute |
| 499 | // once we're out of nested message loops. |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 500 | TaskQueue deferred_non_nestable_work_queue_; |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 501 | |
| 502 | ObserverList<DestructionObserver> destruction_observers_; |
| 503 | |
| 504 | // A recursion block that prevents accidentally running additional tasks when |
| 505 | // insider a (accidentally induced?) nested message pump. |
| 506 | bool nestable_tasks_allowed_; |
| 507 | |
alexeypa@google.com | bb819d6 | 2013-07-23 05:06:56 +0900 | [diff] [blame] | 508 | #if defined(OS_WIN) |
alexeypa@google.com | bb819d6 | 2013-07-23 05:06:56 +0900 | [diff] [blame] | 509 | // Should be set to true before calling Windows APIs like TrackPopupMenu, etc |
| 510 | // which enter a modal message loop. |
| 511 | bool os_modal_loop_; |
| 512 | #endif |
| 513 | |
alexeypa@chromium.org | 4018323 | 2013-07-23 07:24:13 +0900 | [diff] [blame] | 514 | std::string thread_name_; |
| 515 | // A profiling histogram showing the counts of various messages and events. |
| 516 | HistogramBase* message_histogram_; |
| 517 | |
| 518 | RunLoop* run_loop_; |
alexeypa@google.com | bb819d6 | 2013-07-23 05:06:56 +0900 | [diff] [blame] | 519 | |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 520 | ObserverList<TaskObserver> task_observers_; |
| 521 | |
alexeypa@chromium.org | 4018323 | 2013-07-23 07:24:13 +0900 | [diff] [blame] | 522 | scoped_refptr<internal::IncomingTaskQueue> incoming_task_queue_; |
| 523 | |
| 524 | // The message loop proxy associated with this message loop. |
| 525 | scoped_refptr<internal::MessageLoopProxyImpl> message_loop_proxy_; |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 526 | scoped_ptr<ThreadTaskRunnerHandle> thread_task_runner_handle_; |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 527 | |
| 528 | template <class T, class R> friend class base::subtle::DeleteHelperInternal; |
| 529 | template <class T, class R> friend class base::subtle::ReleaseHelperInternal; |
| 530 | |
| 531 | void DeleteSoonInternal(const tracked_objects::Location& from_here, |
| 532 | void(*deleter)(const void*), |
| 533 | const void* object); |
| 534 | void ReleaseSoonInternal(const tracked_objects::Location& from_here, |
| 535 | void(*releaser)(const void*), |
| 536 | const void* object); |
| 537 | |
| 538 | DISALLOW_COPY_AND_ASSIGN(MessageLoop); |
| 539 | }; |
| 540 | |
| 541 | //----------------------------------------------------------------------------- |
| 542 | // MessageLoopForUI extends MessageLoop with methods that are particular to a |
| 543 | // MessageLoop instantiated with TYPE_UI. |
| 544 | // |
| 545 | // This class is typically used like so: |
| 546 | // MessageLoopForUI::current()->...call some method... |
| 547 | // |
| 548 | class BASE_EXPORT MessageLoopForUI : public MessageLoop { |
| 549 | public: |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 550 | MessageLoopForUI() : MessageLoop(TYPE_UI) { |
| 551 | } |
| 552 | |
| 553 | // Returns the MessageLoopForUI of the current thread. |
| 554 | static MessageLoopForUI* current() { |
| 555 | MessageLoop* loop = MessageLoop::current(); |
| 556 | DCHECK(loop); |
| 557 | DCHECK_EQ(MessageLoop::TYPE_UI, loop->type()); |
| 558 | return static_cast<MessageLoopForUI*>(loop); |
| 559 | } |
| 560 | |
sky@chromium.org | 8a7aae7 | 2014-01-20 17:59:52 +0900 | [diff] [blame] | 561 | static bool IsCurrent() { |
| 562 | MessageLoop* loop = MessageLoop::current(); |
| 563 | return loop && loop->type() == MessageLoop::TYPE_UI; |
| 564 | } |
| 565 | |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 566 | #if defined(OS_IOS) |
| 567 | // On iOS, the main message loop cannot be Run(). Instead call Attach(), |
| 568 | // which connects this MessageLoop to the UI thread's CFRunLoop and allows |
| 569 | // PostTask() to work. |
| 570 | void Attach(); |
| 571 | #endif |
| 572 | |
| 573 | #if defined(OS_ANDROID) |
| 574 | // On Android, the UI message loop is handled by Java side. So Run() should |
| 575 | // never be called. Instead use Start(), which will forward all the native UI |
| 576 | // events to the Java message loop. |
| 577 | void Start(); |
sky@chromium.org | f0ef6b6 | 2014-01-12 08:12:06 +0900 | [diff] [blame] | 578 | #endif |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 579 | |
sky@chromium.org | f0ef6b6 | 2014-01-12 08:12:06 +0900 | [diff] [blame] | 580 | #if !defined(OS_NACL) && (defined(TOOLKIT_GTK) || defined(USE_OZONE) || \ |
| 581 | defined(OS_WIN) || defined(USE_X11)) |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 582 | // Please see message_pump_win/message_pump_glib for definitions of these |
| 583 | // methods. |
| 584 | void AddObserver(Observer* observer); |
| 585 | void RemoveObserver(Observer* observer); |
sky@chromium.org | f0ef6b6 | 2014-01-12 08:12:06 +0900 | [diff] [blame] | 586 | #endif |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 587 | |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 588 | protected: |
ccameron@chromium.org | bfe6007 | 2013-09-13 07:51:10 +0900 | [diff] [blame] | 589 | #if defined(USE_X11) |
sadrul@chromium.org | 1999572 | 2013-09-07 12:21:04 +0900 | [diff] [blame] | 590 | friend class MessagePumpX11; |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 591 | #endif |
| 592 | #if defined(USE_OZONE) && !defined(OS_NACL) |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 593 | friend class MessagePumpOzone; |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 594 | #endif |
| 595 | |
sky@chromium.org | f0ef6b6 | 2014-01-12 08:12:06 +0900 | [diff] [blame] | 596 | #if !defined(OS_MACOSX) && !defined(OS_ANDROID) |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 597 | // TODO(rvargas): Make this platform independent. |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 598 | MessagePumpForUI* pump_ui() { |
| 599 | return static_cast<MessagePumpForUI*>(pump_.get()); |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 600 | } |
sky@chromium.org | f0ef6b6 | 2014-01-12 08:12:06 +0900 | [diff] [blame] | 601 | #endif |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 602 | }; |
| 603 | |
| 604 | // Do not add any member variables to MessageLoopForUI! This is important b/c |
| 605 | // MessageLoopForUI is often allocated via MessageLoop(TYPE_UI). Any extra |
| 606 | // data that you need should be stored on the MessageLoop's pump_ instance. |
| 607 | COMPILE_ASSERT(sizeof(MessageLoop) == sizeof(MessageLoopForUI), |
| 608 | MessageLoopForUI_should_not_have_extra_member_variables); |
| 609 | |
| 610 | //----------------------------------------------------------------------------- |
| 611 | // MessageLoopForIO extends MessageLoop with methods that are particular to a |
| 612 | // MessageLoop instantiated with TYPE_IO. |
| 613 | // |
| 614 | // This class is typically used like so: |
| 615 | // MessageLoopForIO::current()->...call some method... |
| 616 | // |
| 617 | class BASE_EXPORT MessageLoopForIO : public MessageLoop { |
| 618 | public: |
| 619 | #if defined(OS_WIN) |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 620 | typedef MessagePumpForIO::IOHandler IOHandler; |
| 621 | typedef MessagePumpForIO::IOContext IOContext; |
| 622 | typedef MessagePumpForIO::IOObserver IOObserver; |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 623 | #elif defined(OS_IOS) |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 624 | typedef MessagePumpIOSForIO::Watcher Watcher; |
| 625 | typedef MessagePumpIOSForIO::FileDescriptorWatcher |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 626 | FileDescriptorWatcher; |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 627 | typedef MessagePumpIOSForIO::IOObserver IOObserver; |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 628 | |
| 629 | enum Mode { |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 630 | WATCH_READ = MessagePumpIOSForIO::WATCH_READ, |
| 631 | WATCH_WRITE = MessagePumpIOSForIO::WATCH_WRITE, |
| 632 | WATCH_READ_WRITE = MessagePumpIOSForIO::WATCH_READ_WRITE |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 633 | }; |
| 634 | #elif defined(OS_POSIX) |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 635 | typedef MessagePumpLibevent::Watcher Watcher; |
| 636 | typedef MessagePumpLibevent::FileDescriptorWatcher |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 637 | FileDescriptorWatcher; |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 638 | typedef MessagePumpLibevent::IOObserver IOObserver; |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 639 | |
| 640 | enum Mode { |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 641 | WATCH_READ = MessagePumpLibevent::WATCH_READ, |
| 642 | WATCH_WRITE = MessagePumpLibevent::WATCH_WRITE, |
| 643 | WATCH_READ_WRITE = MessagePumpLibevent::WATCH_READ_WRITE |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 644 | }; |
| 645 | |
| 646 | #endif |
| 647 | |
| 648 | MessageLoopForIO() : MessageLoop(TYPE_IO) { |
| 649 | } |
| 650 | |
| 651 | // Returns the MessageLoopForIO of the current thread. |
| 652 | static MessageLoopForIO* current() { |
| 653 | MessageLoop* loop = MessageLoop::current(); |
| 654 | DCHECK_EQ(MessageLoop::TYPE_IO, loop->type()); |
| 655 | return static_cast<MessageLoopForIO*>(loop); |
| 656 | } |
| 657 | |
sky@chromium.org | 8a7aae7 | 2014-01-20 17:59:52 +0900 | [diff] [blame] | 658 | static bool IsCurrent() { |
| 659 | MessageLoop* loop = MessageLoop::current(); |
| 660 | return loop && loop->type() == MessageLoop::TYPE_IO; |
| 661 | } |
| 662 | |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 663 | void AddIOObserver(IOObserver* io_observer) { |
| 664 | pump_io()->AddIOObserver(io_observer); |
| 665 | } |
| 666 | |
| 667 | void RemoveIOObserver(IOObserver* io_observer) { |
| 668 | pump_io()->RemoveIOObserver(io_observer); |
| 669 | } |
| 670 | |
| 671 | #if defined(OS_WIN) |
| 672 | // Please see MessagePumpWin for definitions of these methods. |
| 673 | void RegisterIOHandler(HANDLE file, IOHandler* handler); |
| 674 | bool RegisterJobObject(HANDLE job, IOHandler* handler); |
| 675 | bool WaitForIOCompletion(DWORD timeout, IOHandler* filter); |
| 676 | |
| 677 | protected: |
| 678 | // TODO(rvargas): Make this platform independent. |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 679 | MessagePumpForIO* pump_io() { |
| 680 | return static_cast<MessagePumpForIO*>(pump_.get()); |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | #elif defined(OS_IOS) |
| 684 | // Please see MessagePumpIOSForIO for definition. |
| 685 | bool WatchFileDescriptor(int fd, |
| 686 | bool persistent, |
| 687 | Mode mode, |
| 688 | FileDescriptorWatcher *controller, |
| 689 | Watcher *delegate); |
| 690 | |
| 691 | private: |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 692 | MessagePumpIOSForIO* pump_io() { |
| 693 | return static_cast<MessagePumpIOSForIO*>(pump_.get()); |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | #elif defined(OS_POSIX) |
| 697 | // Please see MessagePumpLibevent for definition. |
| 698 | bool WatchFileDescriptor(int fd, |
| 699 | bool persistent, |
| 700 | Mode mode, |
| 701 | FileDescriptorWatcher* controller, |
| 702 | Watcher* delegate); |
| 703 | |
| 704 | private: |
brettw@chromium.org | 710ecb9 | 2013-06-19 05:27:52 +0900 | [diff] [blame] | 705 | MessagePumpLibevent* pump_io() { |
| 706 | return static_cast<MessagePumpLibevent*>(pump_.get()); |
brettw@chromium.org | 6318b39 | 2013-06-14 12:27:49 +0900 | [diff] [blame] | 707 | } |
| 708 | #endif // defined(OS_POSIX) |
| 709 | }; |
| 710 | |
| 711 | // Do not add any member variables to MessageLoopForIO! This is important b/c |
| 712 | // MessageLoopForIO is often allocated via MessageLoop(TYPE_IO). Any extra |
| 713 | // data that you need should be stored on the MessageLoop's pump_ instance. |
| 714 | COMPILE_ASSERT(sizeof(MessageLoop) == sizeof(MessageLoopForIO), |
| 715 | MessageLoopForIO_should_not_have_extra_member_variables); |
| 716 | |
| 717 | } // namespace base |
| 718 | |
| 719 | #endif // BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_ |