[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 4 | |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 5 | #ifndef BASE_MESSAGE_LOOP_H_ |
| 6 | #define BASE_MESSAGE_LOOP_H_ |
[email protected] | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame] | 7 | #pragma once |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 8 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 9 | #include <queue> |
| 10 | #include <string> |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 11 | |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 12 | #include "base/base_export.h" |
[email protected] | 9cfb89a | 2010-06-09 21:20:41 | [diff] [blame] | 13 | #include "base/basictypes.h" |
[email protected] | 2d71566 | 2011-11-28 22:00:29 | [diff] [blame] | 14 | #include "base/callback_forward.h" |
[email protected] | c62dd9d | 2011-09-21 18:05:41 | [diff] [blame] | 15 | #include "base/location.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 16 | #include "base/memory/ref_counted.h" |
[email protected] | edd685f | 2011-08-15 20:33:46 | [diff] [blame] | 17 | #include "base/message_loop_proxy.h" |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 18 | #include "base/message_pump.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 19 | #include "base/observer_list.h" |
[email protected] | dd1f9fe | 2011-11-15 23:36:30 | [diff] [blame] | 20 | #include "base/pending_task.h" |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 21 | #include "base/synchronization/lock.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 22 | #include "base/task.h" |
[email protected] | b2a9bbd | 2011-10-31 22:36:21 | [diff] [blame] | 23 | #include "base/tracking_info.h" |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 24 | #include "base/time.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 25 | |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 26 | #if defined(OS_WIN) |
| 27 | // We need this to declare base::MessagePumpWin::Dispatcher, which we should |
| 28 | // really just eliminate. |
| 29 | #include "base/message_pump_win.h" |
[email protected] | 36987e9 | 2008-09-18 18:46:26 | [diff] [blame] | 30 | #elif defined(OS_POSIX) |
| 31 | #include "base/message_pump_libevent.h" |
[email protected] | 4d0f93c | 2011-09-29 04:43:54 | [diff] [blame] | 32 | #if !defined(OS_MACOSX) && !defined(OS_ANDROID) |
[email protected] | 4076d2f | 2011-08-11 18:20:22 | [diff] [blame] | 33 | |
| 34 | #if defined(USE_WAYLAND) |
| 35 | #include "base/message_pump_wayland.h" |
[email protected] | 69c88e1 | 2011-11-23 02:07:45 | [diff] [blame] | 36 | #elif defined(USE_AURA) |
[email protected] | 2047ef4 | 2011-06-24 20:10:25 | [diff] [blame] | 37 | #include "base/message_pump_x.h" |
| 38 | #else |
| 39 | #include "base/message_pump_gtk.h" |
| 40 | #endif |
[email protected] | 4076d2f | 2011-08-11 18:20:22 | [diff] [blame] | 41 | |
[email protected] | faabcf4 | 2009-05-18 21:12:34 | [diff] [blame] | 42 | #endif |
[email protected] | e43eddf1 | 2009-12-29 00:32:52 | [diff] [blame] | 43 | #endif |
[email protected] | ea15e98 | 2008-08-15 07:31:20 | [diff] [blame] | 44 | |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 45 | namespace base { |
[email protected] | 5097dc8 | 2010-07-15 17:23:23 | [diff] [blame] | 46 | class Histogram; |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 47 | } |
[email protected] | 5097dc8 | 2010-07-15 17:23:23 | [diff] [blame] | 48 | |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 49 | // A MessageLoop is used to process events for a particular thread. There is |
| 50 | // at most one MessageLoop instance per thread. |
| 51 | // |
| 52 | // Events include at a minimum Task instances submitted to PostTask or those |
| 53 | // managed by TimerManager. Depending on the type of message pump used by the |
| 54 | // MessageLoop other events such as UI messages may be processed. On Windows |
| 55 | // APC calls (as time permits) and signals sent to a registered set of HANDLEs |
| 56 | // may also be processed. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 57 | // |
| 58 | // NOTE: Unless otherwise specified, a MessageLoop's methods may only be called |
| 59 | // on the thread where the MessageLoop's Run method executes. |
| 60 | // |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 61 | // NOTE: MessageLoop has task reentrancy protection. This means that if a |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 62 | // task is being processed, a second task cannot start until the first task is |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 63 | // finished. Reentrancy can happen when processing a task, and an inner |
| 64 | // message pump is created. That inner pump then processes native messages |
| 65 | // which could implicitly start an inner task. Inner message pumps are created |
| 66 | // with dialogs (DialogBox), common dialogs (GetOpenFileName), OLE functions |
| 67 | // (DoDragDrop), printer functions (StartDoc) and *many* others. |
| 68 | // |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 69 | // Sample workaround when inner task processing is needed: |
| 70 | // bool old_state = MessageLoop::current()->NestableTasksAllowed(); |
| 71 | // MessageLoop::current()->SetNestableTasksAllowed(true); |
| 72 | // HRESULT hr = DoDragDrop(...); // Implicitly runs a modal message loop here. |
| 73 | // MessageLoop::current()->SetNestableTasksAllowed(old_state); |
| 74 | // // Process hr (the result returned by DoDragDrop(). |
| 75 | // |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 76 | // Please be SURE your task is reentrant (nestable) and all global variables |
| 77 | // are stable and accessible before calling SetNestableTasksAllowed(true). |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 78 | // |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 79 | class BASE_EXPORT MessageLoop : public base::MessagePump::Delegate { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 80 | public: |
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 81 | #if defined(OS_WIN) |
| 82 | typedef base::MessagePumpWin::Dispatcher Dispatcher; |
[email protected] | 2e5597c | 2011-10-04 00:10:47 | [diff] [blame] | 83 | typedef base::MessagePumpObserver Observer; |
[email protected] | 4d0f93c | 2011-09-29 04:43:54 | [diff] [blame] | 84 | #elif !defined(OS_MACOSX) && !defined(OS_ANDROID) |
[email protected] | 2047ef4 | 2011-06-24 20:10:25 | [diff] [blame] | 85 | typedef base::MessagePumpDispatcher Dispatcher; |
| 86 | typedef base::MessagePumpObserver Observer; |
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 87 | #endif |
| 88 | |
| 89 | // A MessageLoop has a particular type, which indicates the set of |
| 90 | // asynchronous events it may process in addition to tasks and timers. |
[email protected] | 9cfb89a | 2010-06-09 21:20:41 | [diff] [blame] | 91 | // |
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 92 | // TYPE_DEFAULT |
| 93 | // This type of ML only supports tasks and timers. |
| 94 | // |
| 95 | // TYPE_UI |
| 96 | // This type of ML also supports native UI events (e.g., Windows messages). |
| 97 | // See also MessageLoopForUI. |
| 98 | // |
| 99 | // TYPE_IO |
| 100 | // This type of ML also supports asynchronous IO. See also |
| 101 | // MessageLoopForIO. |
| 102 | // |
| 103 | enum Type { |
| 104 | TYPE_DEFAULT, |
| 105 | TYPE_UI, |
| 106 | TYPE_IO |
[email protected] | 9cfb89a | 2010-06-09 21:20:41 | [diff] [blame] | 107 | }; |
| 108 | |
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 109 | // Normally, it is not necessary to instantiate a MessageLoop. Instead, it |
| 110 | // is typical to make use of the current thread's MessageLoop instance. |
| 111 | explicit MessageLoop(Type type = TYPE_DEFAULT); |
[email protected] | 3690ebe0 | 2011-05-25 09:08:19 | [diff] [blame] | 112 | virtual ~MessageLoop(); |
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 113 | |
[email protected] | 9989c9bb | 2011-01-07 20:23:43 | [diff] [blame] | 114 | // Returns the MessageLoop object for the current thread, or null if none. |
| 115 | static MessageLoop* current(); |
| 116 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 117 | static void EnableHistogrammer(bool enable_histogrammer); |
| 118 | |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 119 | typedef base::MessagePump* (MessagePumpFactory)(); |
| 120 | // Using the given base::MessagePumpForUIFactory to override the default |
| 121 | // MessagePump implementation for 'TYPE_UI'. |
| 122 | static void InitMessagePumpForUIFactory(MessagePumpFactory* factory); |
| 123 | |
[email protected] | ee132ad | 2008-08-06 21:27:02 | [diff] [blame] | 124 | // A DestructionObserver is notified when the current MessageLoop is being |
[email protected] | dd1f9fe | 2011-11-15 23:36:30 | [diff] [blame] | 125 | // destroyed. These observers are notified prior to MessageLoop::current() |
[email protected] | ee132ad | 2008-08-06 21:27:02 | [diff] [blame] | 126 | // being changed to return NULL. This gives interested parties the chance to |
| 127 | // do final cleanup that depends on the MessageLoop. |
| 128 | // |
| 129 | // NOTE: Any tasks posted to the MessageLoop during this notification will |
| 130 | // not be run. Instead, they will be deleted. |
| 131 | // |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 132 | class BASE_EXPORT DestructionObserver { |
[email protected] | ee132ad | 2008-08-06 21:27:02 | [diff] [blame] | 133 | public: |
[email protected] | ee132ad | 2008-08-06 21:27:02 | [diff] [blame] | 134 | virtual void WillDestroyCurrentMessageLoop() = 0; |
[email protected] | 23c386b | 2010-09-15 22:14:36 | [diff] [blame] | 135 | |
| 136 | protected: |
| 137 | virtual ~DestructionObserver(); |
[email protected] | ee132ad | 2008-08-06 21:27:02 | [diff] [blame] | 138 | }; |
| 139 | |
| 140 | // Add a DestructionObserver, which will start receiving notifications |
| 141 | // immediately. |
| 142 | void AddDestructionObserver(DestructionObserver* destruction_observer); |
| 143 | |
| 144 | // Remove a DestructionObserver. It is safe to call this method while a |
| 145 | // DestructionObserver is receiving a notification callback. |
| 146 | void RemoveDestructionObserver(DestructionObserver* destruction_observer); |
| 147 | |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 148 | // The "PostTask" family of methods call the task's Run method asynchronously |
| 149 | // from within a message loop at some point in the future. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 150 | // |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 151 | // With the PostTask variant, tasks are invoked in FIFO order, inter-mixed |
| 152 | // with normal UI or IO event processing. With the PostDelayedTask variant, |
| 153 | // tasks are called after at least approximately 'delay_ms' have elapsed. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 154 | // |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 155 | // The NonNestable variants work similarly except that they promise never to |
| 156 | // dispatch the task from a nested invocation of MessageLoop::Run. Instead, |
| 157 | // such tasks get deferred until the top-most MessageLoop::Run is executing. |
| 158 | // |
| 159 | // The MessageLoop takes ownership of the Task, and deletes it after it has |
| 160 | // been Run(). |
| 161 | // |
| 162 | // NOTE: These methods may be called on any thread. The Task will be invoked |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 163 | // on the thread that executes MessageLoop::Run(). |
[email protected] | b099217 | 2008-10-27 18:54:18 | [diff] [blame] | 164 | |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 165 | void PostTask( |
| 166 | const tracked_objects::Location& from_here, Task* task); |
[email protected] | b099217 | 2008-10-27 18:54:18 | [diff] [blame] | 167 | |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 168 | void PostDelayedTask( |
[email protected] | 743ace4 | 2009-06-17 17:23:51 | [diff] [blame] | 169 | const tracked_objects::Location& from_here, Task* task, int64 delay_ms); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 170 | |
[email protected] | 76784292 | 2011-12-30 01:04:36 | [diff] [blame] | 171 | void PostDelayedTask( |
| 172 | const tracked_objects::Location& from_here, |
| 173 | Task* task, |
| 174 | base::TimeDelta delay); |
| 175 | |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 176 | void PostNonNestableTask( |
| 177 | const tracked_objects::Location& from_here, Task* task); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 178 | |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 179 | void PostNonNestableDelayedTask( |
[email protected] | 743ace4 | 2009-06-17 17:23:51 | [diff] [blame] | 180 | const tracked_objects::Location& from_here, Task* task, int64 delay_ms); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 181 | |
[email protected] | 76784292 | 2011-12-30 01:04:36 | [diff] [blame] | 182 | void PostNonNestableDelayedTask( |
| 183 | const tracked_objects::Location& from_here, |
| 184 | Task* task, |
| 185 | base::TimeDelta delay); |
| 186 | |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 187 | // TODO(ajwong): Remove the functions above once the Task -> Closure migration |
| 188 | // is complete. |
| 189 | // |
| 190 | // There are 2 sets of Post*Task functions, one which takes the older Task* |
| 191 | // function object representation, and one that takes the newer base::Closure. |
| 192 | // We have this overload to allow a staged transition between the two systems. |
| 193 | // Once the transition is done, the functions above should be deleted. |
| 194 | void PostTask( |
| 195 | const tracked_objects::Location& from_here, |
| 196 | const base::Closure& task); |
| 197 | |
| 198 | void PostDelayedTask( |
| 199 | const tracked_objects::Location& from_here, |
| 200 | const base::Closure& task, int64 delay_ms); |
| 201 | |
[email protected] | 76784292 | 2011-12-30 01:04:36 | [diff] [blame] | 202 | void PostDelayedTask( |
| 203 | const tracked_objects::Location& from_here, |
| 204 | const base::Closure& task, |
| 205 | base::TimeDelta delay); |
| 206 | |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 207 | void PostNonNestableTask( |
| 208 | const tracked_objects::Location& from_here, |
| 209 | const base::Closure& task); |
| 210 | |
| 211 | void PostNonNestableDelayedTask( |
| 212 | const tracked_objects::Location& from_here, |
| 213 | const base::Closure& task, int64 delay_ms); |
| 214 | |
[email protected] | 76784292 | 2011-12-30 01:04:36 | [diff] [blame] | 215 | void PostNonNestableDelayedTask( |
| 216 | const tracked_objects::Location& from_here, |
| 217 | const base::Closure& task, |
| 218 | base::TimeDelta delay); |
| 219 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 220 | // A variant on PostTask that deletes the given object. This is useful |
| 221 | // if the object needs to live until the next run of the MessageLoop (for |
| 222 | // example, deleting a RenderProcessHost from within an IPC callback is not |
| 223 | // good). |
| 224 | // |
| 225 | // NOTE: This method may be called on any thread. The object will be deleted |
| 226 | // on the thread that executes MessageLoop::Run(). If this is not the same |
| 227 | // as the thread that calls PostDelayedTask(FROM_HERE, ), then T MUST inherit |
| 228 | // from RefCountedThreadSafe<T>! |
| 229 | template <class T> |
[email protected] | 00ed48f | 2010-10-22 22:19:24 | [diff] [blame] | 230 | void DeleteSoon(const tracked_objects::Location& from_here, const T* object) { |
[email protected] | 4abe061 | 2011-12-29 23:13:03 | [diff] [blame] | 231 | PostNonNestableTask(from_here, new DeleteTask<T>(object)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | // A variant on PostTask that releases the given reference counted object |
| 235 | // (by calling its Release method). This is useful if the object needs to |
| 236 | // live until the next run of the MessageLoop, or if the object needs to be |
| 237 | // released on a particular thread. |
| 238 | // |
| 239 | // NOTE: This method may be called on any thread. The object will be |
| 240 | // released (and thus possibly deleted) on the thread that executes |
| 241 | // MessageLoop::Run(). If this is not the same as the thread that calls |
| 242 | // PostDelayedTask(FROM_HERE, ), then T MUST inherit from |
| 243 | // RefCountedThreadSafe<T>! |
| 244 | template <class T> |
[email protected] | 00ed48f | 2010-10-22 22:19:24 | [diff] [blame] | 245 | void ReleaseSoon(const tracked_objects::Location& from_here, |
| 246 | const T* object) { |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 247 | PostNonNestableTask(from_here, new ReleaseTask<T>(object)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 248 | } |
| 249 | |
[email protected] | 3882c433 | 2008-07-30 19:03:59 | [diff] [blame] | 250 | // Run the message loop. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 251 | void Run(); |
| 252 | |
[email protected] | 7e0e876 | 2008-07-31 13:10:20 | [diff] [blame] | 253 | // Process all pending tasks, windows messages, etc., but don't wait/sleep. |
| 254 | // Return as soon as all items that can be run are taken care of. |
| 255 | void RunAllPending(); |
[email protected] | 3882c433 | 2008-07-30 19:03:59 | [diff] [blame] | 256 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 257 | // Signals the Run method to return after it is done processing all pending |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 258 | // messages. This method may only be called on the same thread that called |
| 259 | // Run, and Run must still be on the call stack. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 260 | // |
[email protected] | 45ce1e20 | 2011-12-10 19:44:25 | [diff] [blame] | 261 | // Use QuitClosure if you need to Quit another thread's MessageLoop, but note |
| 262 | // that doing so is fairly dangerous if the target thread makes nested calls |
| 263 | // to MessageLoop::Run. The problem being that you won't know which nested |
| 264 | // run loop you are quitting, so be careful! |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 265 | void Quit(); |
| 266 | |
[email protected] | 781a7ed | 2010-02-23 07:12:22 | [diff] [blame] | 267 | // This method is a variant of Quit, that does not wait for pending messages |
| 268 | // to be processed before returning from Run. |
| 269 | void QuitNow(); |
| 270 | |
[email protected] | 8c6517e5 | 2011-10-17 01:20:36 | [diff] [blame] | 271 | // Invokes Quit on the current MessageLoop when run. Useful to schedule an |
| 272 | // arbitrary MessageLoop to Quit. |
| 273 | static base::Closure QuitClosure(); |
| 274 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 275 | // Returns the type passed to the constructor. |
| 276 | Type type() const { return type_; } |
| 277 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 278 | // Optional call to connect the thread name with this loop. |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 279 | void set_thread_name(const std::string& thread_name) { |
| 280 | DCHECK(thread_name_.empty()) << "Should not rename this thread!"; |
| 281 | thread_name_ = thread_name; |
| 282 | } |
[email protected] | ee132ad | 2008-08-06 21:27:02 | [diff] [blame] | 283 | const std::string& thread_name() const { return thread_name_; } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 284 | |
[email protected] | c31af70db2 | 2011-08-18 23:13:01 | [diff] [blame] | 285 | // Gets the message loop proxy associated with this message loop. |
[email protected] | edd685f | 2011-08-15 20:33:46 | [diff] [blame] | 286 | scoped_refptr<base::MessageLoopProxy> message_loop_proxy() { |
| 287 | return message_loop_proxy_.get(); |
| 288 | } |
| 289 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 290 | // Enables or disables the recursive task processing. This happens in the case |
| 291 | // of recursive message loops. Some unwanted message loop may occurs when |
| 292 | // using common controls or printer functions. By default, recursive task |
| 293 | // processing is disabled. |
| 294 | // |
| 295 | // The specific case where tasks get queued is: |
| 296 | // - The thread is running a message loop. |
| 297 | // - It receives a task #1 and execute it. |
| 298 | // - The task #1 implicitly start a message loop, like a MessageBox in the |
| 299 | // unit test. This can also be StartDoc or GetSaveFileName. |
| 300 | // - The thread receives a task #2 before or while in this second message |
| 301 | // loop. |
| 302 | // - With NestableTasksAllowed set to true, the task #2 will run right away. |
| 303 | // Otherwise, it will get executed right after task #1 completes at "thread |
| 304 | // message loop level". |
| 305 | void SetNestableTasksAllowed(bool allowed); |
| 306 | bool NestableTasksAllowed() const; |
| 307 | |
[email protected] | 18d6a8f | 2009-12-16 22:56:33 | [diff] [blame] | 308 | // Enables nestable tasks on |loop| while in scope. |
| 309 | class ScopedNestableTaskAllower { |
| 310 | public: |
| 311 | explicit ScopedNestableTaskAllower(MessageLoop* loop) |
| 312 | : loop_(loop), |
| 313 | old_state_(loop_->NestableTasksAllowed()) { |
| 314 | loop_->SetNestableTasksAllowed(true); |
| 315 | } |
| 316 | ~ScopedNestableTaskAllower() { |
| 317 | loop_->SetNestableTasksAllowed(old_state_); |
| 318 | } |
| 319 | |
| 320 | private: |
| 321 | MessageLoop* loop_; |
| 322 | bool old_state_; |
| 323 | }; |
| 324 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 325 | // Enables or disables the restoration during an exception of the unhandled |
| 326 | // exception filter that was active when Run() was called. This can happen |
| 327 | // if some third party code call SetUnhandledExceptionFilter() and never |
| 328 | // restores the previous filter. |
| 329 | void set_exception_restoration(bool restore) { |
| 330 | exception_restoration_ = restore; |
| 331 | } |
| 332 | |
[email protected] | b5f9510 | 2009-07-01 19:53:59 | [diff] [blame] | 333 | // Returns true if we are currently running a nested message loop. |
| 334 | bool IsNested(); |
| 335 | |
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 336 | // A TaskObserver is an object that receives task notifications from the |
| 337 | // MessageLoop. |
| 338 | // |
| 339 | // NOTE: A TaskObserver implementation should be extremely fast! |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 340 | class BASE_EXPORT TaskObserver { |
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 341 | public: |
| 342 | TaskObserver(); |
| 343 | |
| 344 | // This method is called before processing a task. |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 345 | virtual void WillProcessTask(base::TimeTicks time_posted) = 0; |
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 346 | |
| 347 | // This method is called after processing a task. |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 348 | virtual void DidProcessTask(base::TimeTicks time_posted) = 0; |
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 349 | |
| 350 | protected: |
| 351 | virtual ~TaskObserver(); |
| 352 | }; |
| 353 | |
[email protected] | 9cfb89a | 2010-06-09 21:20:41 | [diff] [blame] | 354 | // These functions can only be called on the same thread that |this| is |
| 355 | // running on. |
| 356 | void AddTaskObserver(TaskObserver* task_observer); |
| 357 | void RemoveTaskObserver(TaskObserver* task_observer); |
| 358 | |
[email protected] | 57f030a | 2010-06-29 04:58:15 | [diff] [blame] | 359 | // Returns true if the message loop has high resolution timers enabled. |
| 360 | // Provided for testing. |
| 361 | bool high_resolution_timers_enabled() { |
| 362 | #if defined(OS_WIN) |
| 363 | return !high_resolution_timer_expiration_.is_null(); |
| 364 | #else |
| 365 | return true; |
| 366 | #endif |
| 367 | } |
| 368 | |
| 369 | // When we go into high resolution timer mode, we will stay in hi-res mode |
| 370 | // for at least 1s. |
| 371 | static const int kHighResolutionTimerModeLeaseTimeMs = 1000; |
| 372 | |
[email protected] | 8d6ab8f5 | 2011-01-26 00:53:48 | [diff] [blame] | 373 | // Asserts that the MessageLoop is "idle". |
| 374 | void AssertIdle() const; |
| 375 | |
[email protected] | 2ec01fe | 2011-03-24 03:40:28 | [diff] [blame] | 376 | #if defined(OS_WIN) |
| 377 | void set_os_modal_loop(bool os_modal_loop) { |
| 378 | os_modal_loop_ = os_modal_loop; |
| 379 | } |
| 380 | |
| 381 | bool os_modal_loop() const { |
| 382 | return os_modal_loop_; |
| 383 | } |
| 384 | #endif // OS_WIN |
| 385 | |
[email protected] | e6244c18 | 2011-11-01 22:06:58 | [diff] [blame] | 386 | // Can only be called from the thread that owns the MessageLoop. |
| 387 | bool is_running() const; |
| 388 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 389 | //---------------------------------------------------------------------------- |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 390 | protected: |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 391 | struct RunState { |
| 392 | // Used to count how many Run() invocations are on the stack. |
| 393 | int run_depth; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 394 | |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 395 | // Used to record that Quit() was called, or that we should quit the pump |
| 396 | // once it becomes idle. |
| 397 | bool quit_received; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 398 | |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 399 | #if !defined(OS_MACOSX) && !defined(OS_ANDROID) |
[email protected] | 148d105 | 2009-07-31 22:53:37 | [diff] [blame] | 400 | Dispatcher* dispatcher; |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 401 | #endif |
| 402 | }; |
| 403 | |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 404 | #if defined(OS_ANDROID) |
| 405 | // Android Java process manages the UI thread message loop. So its |
| 406 | // MessagePumpForUI needs to keep the RunState. |
| 407 | public: |
| 408 | #endif |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 409 | class BASE_EXPORT AutoRunState : RunState { |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 410 | public: |
[email protected] | b099217 | 2008-10-27 18:54:18 | [diff] [blame] | 411 | explicit AutoRunState(MessageLoop* loop); |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 412 | ~AutoRunState(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 413 | private: |
| 414 | MessageLoop* loop_; |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 415 | RunState* previous_state_; |
| 416 | }; |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 417 | #if defined(OS_ANDROID) |
| 418 | protected: |
| 419 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 420 | |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 421 | #if defined(OS_WIN) |
| 422 | base::MessagePumpWin* pump_win() { |
| 423 | return static_cast<base::MessagePumpWin*>(pump_.get()); |
| 424 | } |
[email protected] | 36987e9 | 2008-09-18 18:46:26 | [diff] [blame] | 425 | #elif defined(OS_POSIX) |
| 426 | base::MessagePumpLibevent* pump_libevent() { |
| 427 | return static_cast<base::MessagePumpLibevent*>(pump_.get()); |
| 428 | } |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 429 | #endif |
[email protected] | 3882c433 | 2008-07-30 19:03:59 | [diff] [blame] | 430 | |
| 431 | // A function to encapsulate all the exception handling capability in the |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 432 | // stacks around the running of a main message loop. It will run the message |
| 433 | // loop in a SEH try block or not depending on the set_SEH_restoration() |
[email protected] | aff8a13 | 2009-10-26 18:15:59 | [diff] [blame] | 434 | // flag invoking respectively RunInternalInSEHFrame() or RunInternal(). |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 435 | void RunHandler(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 436 | |
[email protected] | aff8a13 | 2009-10-26 18:15:59 | [diff] [blame] | 437 | #if defined(OS_WIN) |
| 438 | __declspec(noinline) void RunInternalInSEHFrame(); |
| 439 | #endif |
| 440 | |
[email protected] | 3882c433 | 2008-07-30 19:03:59 | [diff] [blame] | 441 | // A surrounding stack frame around the running of the message loop that |
| 442 | // supports all saving and restoring of state, as is needed for any/all (ugly) |
| 443 | // recursive calls. |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 444 | void RunInternal(); |
[email protected] | ea15e98 | 2008-08-15 07:31:20 | [diff] [blame] | 445 | |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 446 | // Called to process any delayed non-nestable tasks. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 447 | bool ProcessNextDelayedNonNestableTask(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 448 | |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 449 | // Runs the specified PendingTask. |
[email protected] | dd1f9fe | 2011-11-15 23:36:30 | [diff] [blame] | 450 | void RunTask(const base::PendingTask& pending_task); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 451 | |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 452 | // Calls RunTask or queues the pending_task on the deferred task list if it |
| 453 | // cannot be run right now. Returns true if the task was run. |
[email protected] | dd1f9fe | 2011-11-15 23:36:30 | [diff] [blame] | 454 | bool DeferOrRunPendingTask(const base::PendingTask& pending_task); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 455 | |
[email protected] | 001747c | 2008-09-10 00:37:07 | [diff] [blame] | 456 | // Adds the pending task to delayed_work_queue_. |
[email protected] | dd1f9fe | 2011-11-15 23:36:30 | [diff] [blame] | 457 | void AddToDelayedWorkQueue(const base::PendingTask& pending_task); |
[email protected] | 001747c | 2008-09-10 00:37:07 | [diff] [blame] | 458 | |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 459 | // Adds the pending task to our incoming_queue_. |
| 460 | // |
| 461 | // Caller retains ownership of |pending_task|, but this function will |
| 462 | // reset the value of pending_task->task. This is needed to ensure |
| 463 | // that the posting call stack does not retain pending_task->task |
| 464 | // beyond this function call. |
[email protected] | dd1f9fe | 2011-11-15 23:36:30 | [diff] [blame] | 465 | void AddToIncomingQueue(base::PendingTask* pending_task); |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 466 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 467 | // Load tasks from the incoming_queue_ into work_queue_ if the latter is |
| 468 | // empty. The former requires a lock to access, while the latter is directly |
| 469 | // accessible on this thread. |
| 470 | void ReloadWorkQueue(); |
| 471 | |
| 472 | // Delete tasks that haven't run yet without running them. Used in the |
[email protected] | 001747c | 2008-09-10 00:37:07 | [diff] [blame] | 473 | // destructor to make sure all the task's destructors get called. Returns |
| 474 | // true if some work was done. |
| 475 | bool DeletePendingTasks(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 476 | |
[email protected] | dd1f9fe | 2011-11-15 23:36:30 | [diff] [blame] | 477 | // Calculates the time at which a PendingTask should run. |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 478 | base::TimeTicks CalculateDelayedRuntime(int64 delay_ms); |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 479 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 480 | // Start recording histogram info about events and action IF it was enabled |
| 481 | // and IF the statistics recorder can accept a registration of our histogram. |
| 482 | void StartHistogrammer(); |
| 483 | |
[email protected] | dd1f9fe | 2011-11-15 23:36:30 | [diff] [blame] | 484 | // Add occurrence of event to our histogram, so that we can see what is being |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 485 | // done in a specific MessageLoop instance (i.e., specific thread). |
| 486 | // If message_histogram_ is NULL, this is a no-op. |
| 487 | void HistogramEvent(int event); |
| 488 | |
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 489 | // base::MessagePump::Delegate methods: |
[email protected] | b171946 | 2011-11-16 00:08:08 | [diff] [blame] | 490 | virtual bool DoWork() OVERRIDE; |
| 491 | virtual bool DoDelayedWork(base::TimeTicks* next_delayed_work_time) OVERRIDE; |
| 492 | virtual bool DoIdleWork() OVERRIDE; |
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 493 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 494 | Type type_; |
| 495 | |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 496 | // A list of tasks that need to be processed by this instance. Note that |
| 497 | // this queue is only accessed (push/pop) by our current thread. |
[email protected] | dd1f9fe | 2011-11-15 23:36:30 | [diff] [blame] | 498 | base::TaskQueue work_queue_; |
[email protected] | b099217 | 2008-10-27 18:54:18 | [diff] [blame] | 499 | |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 500 | // Contains delayed tasks, sorted by their 'delayed_run_time' property. |
[email protected] | 262060ff | 2011-11-17 23:26:53 | [diff] [blame] | 501 | base::DelayedTaskQueue delayed_work_queue_; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 502 | |
[email protected] | a8f7d3d | 2010-11-04 23:23:42 | [diff] [blame] | 503 | // A recent snapshot of Time::Now(), used to check delayed_work_queue_. |
[email protected] | 7e7fab4 | 2010-11-06 22:23:29 | [diff] [blame] | 504 | base::TimeTicks recent_time_; |
[email protected] | a8f7d3d | 2010-11-04 23:23:42 | [diff] [blame] | 505 | |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 506 | // A queue of non-nestable tasks that we had to defer because when it came |
| 507 | // time to execute them we were in a nested message loop. They will execute |
| 508 | // once we're out of nested message loops. |
[email protected] | dd1f9fe | 2011-11-15 23:36:30 | [diff] [blame] | 509 | base::TaskQueue deferred_non_nestable_work_queue_; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 510 | |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 511 | scoped_refptr<base::MessagePump> pump_; |
[email protected] | ee132ad | 2008-08-06 21:27:02 | [diff] [blame] | 512 | |
[email protected] | 2a12725 | 2008-08-05 23:16:41 | [diff] [blame] | 513 | ObserverList<DestructionObserver> destruction_observers_; |
[email protected] | 001747c | 2008-09-10 00:37:07 | [diff] [blame] | 514 | |
[email protected] | dd1f9fe | 2011-11-15 23:36:30 | [diff] [blame] | 515 | // A recursion block that prevents accidentally running additional tasks when |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 516 | // insider a (accidentally induced?) nested message pump. |
| 517 | bool nestable_tasks_allowed_; |
| 518 | |
| 519 | bool exception_restoration_; |
| 520 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 521 | std::string thread_name_; |
| 522 | // A profiling histogram showing the counts of various messages and events. |
[email protected] | 81ce9f3b | 2011-04-05 04:48:53 | [diff] [blame] | 523 | base::Histogram* message_histogram_; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 524 | |
| 525 | // A null terminated list which creates an incoming_queue of tasks that are |
[email protected] | 242c4bd | 2011-02-25 18:43:23 | [diff] [blame] | 526 | // acquired under a mutex for processing on this instance's thread. These |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 527 | // tasks have not yet been sorted out into items for our work_queue_ vs items |
| 528 | // that will be handled by the TimerManager. |
[email protected] | dd1f9fe | 2011-11-15 23:36:30 | [diff] [blame] | 529 | base::TaskQueue incoming_queue_; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 530 | // Protect access to incoming_queue_. |
[email protected] | 8d6ab8f5 | 2011-01-26 00:53:48 | [diff] [blame] | 531 | mutable base::Lock incoming_queue_lock_; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 532 | |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 533 | RunState* state_; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 534 | |
[email protected] | b224f79 | 2011-04-20 16:02:23 | [diff] [blame] | 535 | // The need for this variable is subtle. Please see implementation comments |
| 536 | // around where it is used. |
| 537 | bool should_leak_tasks_; |
| 538 | |
[email protected] | 57f030a | 2010-06-29 04:58:15 | [diff] [blame] | 539 | #if defined(OS_WIN) |
| 540 | base::TimeTicks high_resolution_timer_expiration_; |
[email protected] | 2ec01fe | 2011-03-24 03:40:28 | [diff] [blame] | 541 | // Should be set to true before calling Windows APIs like TrackPopupMenu, etc |
| 542 | // which enter a modal message loop. |
| 543 | bool os_modal_loop_; |
[email protected] | 57f030a | 2010-06-29 04:58:15 | [diff] [blame] | 544 | #endif |
| 545 | |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 546 | // The next sequence number to use for delayed tasks. |
| 547 | int next_sequence_num_; |
| 548 | |
[email protected] | 9cfb89a | 2010-06-09 21:20:41 | [diff] [blame] | 549 | ObserverList<TaskObserver> task_observers_; |
| 550 | |
[email protected] | edd685f | 2011-08-15 20:33:46 | [diff] [blame] | 551 | // The message loop proxy associated with this message loop, if one exists. |
| 552 | scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; |
| 553 | |
[email protected] | 26fbf80 | 2011-03-25 18:48:03 | [diff] [blame] | 554 | private: |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 555 | DISALLOW_COPY_AND_ASSIGN(MessageLoop); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 556 | }; |
| 557 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 558 | //----------------------------------------------------------------------------- |
| 559 | // MessageLoopForUI extends MessageLoop with methods that are particular to a |
| 560 | // MessageLoop instantiated with TYPE_UI. |
| 561 | // |
| 562 | // This class is typically used like so: |
| 563 | // MessageLoopForUI::current()->...call some method... |
| 564 | // |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 565 | class BASE_EXPORT MessageLoopForUI : public MessageLoop { |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 566 | public: |
| 567 | MessageLoopForUI() : MessageLoop(TYPE_UI) { |
| 568 | } |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 569 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 570 | // Returns the MessageLoopForUI of the current thread. |
| 571 | static MessageLoopForUI* current() { |
| 572 | MessageLoop* loop = MessageLoop::current(); |
| 573 | DCHECK_EQ(MessageLoop::TYPE_UI, loop->type()); |
| 574 | return static_cast<MessageLoopForUI*>(loop); |
| 575 | } |
| 576 | |
| 577 | #if defined(OS_WIN) |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 578 | void DidProcessMessage(const MSG& message); |
[email protected] | 9cfb89a | 2010-06-09 21:20:41 | [diff] [blame] | 579 | #endif // defined(OS_WIN) |
[email protected] | 1a8f5d1d | 2008-09-25 20:33:04 | [diff] [blame] | 580 | |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 581 | #if defined(OS_ANDROID) |
| 582 | // On Android, the UI message loop is handled by Java side. So Run() should |
| 583 | // never be called. Instead use Start(), which will forward all the native UI |
| 584 | // events to the Java message loop. |
| 585 | void Start(); |
| 586 | #elif !defined(OS_MACOSX) |
[email protected] | 148d105 | 2009-07-31 22:53:37 | [diff] [blame] | 587 | // Please see message_pump_win/message_pump_glib for definitions of these |
| 588 | // methods. |
| 589 | void AddObserver(Observer* observer); |
| 590 | void RemoveObserver(Observer* observer); |
[email protected] | 4d628531 | 2011-10-24 07:19:51 | [diff] [blame] | 591 | void RunWithDispatcher(Dispatcher* dispatcher); |
[email protected] | 35e9b66a | 2011-10-06 18:19:21 | [diff] [blame] | 592 | void RunAllPendingWithDispatcher(Dispatcher* dispatcher); |
[email protected] | 148d105 | 2009-07-31 22:53:37 | [diff] [blame] | 593 | |
[email protected] | 1a8f5d1d | 2008-09-25 20:33:04 | [diff] [blame] | 594 | protected: |
| 595 | // TODO(rvargas): Make this platform independent. |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 596 | base::MessagePumpForUI* pump_ui() { |
[email protected] | 1a8f5d1d | 2008-09-25 20:33:04 | [diff] [blame] | 597 | return static_cast<base::MessagePumpForUI*>(pump_.get()); |
| 598 | } |
[email protected] | 9cfb89a | 2010-06-09 21:20:41 | [diff] [blame] | 599 | #endif // !defined(OS_MACOSX) |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 600 | }; |
| 601 | |
| 602 | // Do not add any member variables to MessageLoopForUI! This is important b/c |
| 603 | // MessageLoopForUI is often allocated via MessageLoop(TYPE_UI). Any extra |
| 604 | // data that you need should be stored on the MessageLoop's pump_ instance. |
| 605 | COMPILE_ASSERT(sizeof(MessageLoop) == sizeof(MessageLoopForUI), |
| 606 | MessageLoopForUI_should_not_have_extra_member_variables); |
| 607 | |
| 608 | //----------------------------------------------------------------------------- |
| 609 | // MessageLoopForIO extends MessageLoop with methods that are particular to a |
| 610 | // MessageLoop instantiated with TYPE_IO. |
| 611 | // |
| 612 | // This class is typically used like so: |
| 613 | // MessageLoopForIO::current()->...call some method... |
| 614 | // |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 615 | class BASE_EXPORT MessageLoopForIO : public MessageLoop { |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 616 | public: |
[email protected] | 9cfb89a | 2010-06-09 21:20:41 | [diff] [blame] | 617 | #if defined(OS_WIN) |
| 618 | typedef base::MessagePumpForIO::IOHandler IOHandler; |
| 619 | typedef base::MessagePumpForIO::IOContext IOContext; |
| 620 | typedef base::MessagePumpForIO::IOObserver IOObserver; |
| 621 | #elif defined(OS_POSIX) |
| 622 | typedef base::MessagePumpLibevent::Watcher Watcher; |
| 623 | typedef base::MessagePumpLibevent::FileDescriptorWatcher |
| 624 | FileDescriptorWatcher; |
| 625 | typedef base::MessagePumpLibevent::IOObserver IOObserver; |
| 626 | |
| 627 | enum Mode { |
| 628 | WATCH_READ = base::MessagePumpLibevent::WATCH_READ, |
| 629 | WATCH_WRITE = base::MessagePumpLibevent::WATCH_WRITE, |
| 630 | WATCH_READ_WRITE = base::MessagePumpLibevent::WATCH_READ_WRITE |
| 631 | }; |
| 632 | |
| 633 | #endif |
| 634 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 635 | MessageLoopForIO() : MessageLoop(TYPE_IO) { |
| 636 | } |
| 637 | |
| 638 | // Returns the MessageLoopForIO of the current thread. |
| 639 | static MessageLoopForIO* current() { |
| 640 | MessageLoop* loop = MessageLoop::current(); |
| 641 | DCHECK_EQ(MessageLoop::TYPE_IO, loop->type()); |
| 642 | return static_cast<MessageLoopForIO*>(loop); |
| 643 | } |
| 644 | |
[email protected] | 9cfb89a | 2010-06-09 21:20:41 | [diff] [blame] | 645 | void AddIOObserver(IOObserver* io_observer) { |
| 646 | pump_io()->AddIOObserver(io_observer); |
| 647 | } |
[email protected] | 94128148 | 2010-06-09 05:10:48 | [diff] [blame] | 648 | |
[email protected] | 9cfb89a | 2010-06-09 21:20:41 | [diff] [blame] | 649 | void RemoveIOObserver(IOObserver* io_observer) { |
| 650 | pump_io()->RemoveIOObserver(io_observer); |
| 651 | } |
| 652 | |
| 653 | #if defined(OS_WIN) |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 654 | // Please see MessagePumpWin for definitions of these methods. |
[email protected] | 32cda29d | 2008-10-09 23:58:43 | [diff] [blame] | 655 | void RegisterIOHandler(HANDLE file_handle, IOHandler* handler); |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 656 | bool WaitForIOCompletion(DWORD timeout, IOHandler* filter); |
[email protected] | 36987e9 | 2008-09-18 18:46:26 | [diff] [blame] | 657 | |
[email protected] | 1a8f5d1d | 2008-09-25 20:33:04 | [diff] [blame] | 658 | protected: |
| 659 | // TODO(rvargas): Make this platform independent. |
| 660 | base::MessagePumpForIO* pump_io() { |
| 661 | return static_cast<base::MessagePumpForIO*>(pump_.get()); |
| 662 | } |
| 663 | |
[email protected] | 36987e9 | 2008-09-18 18:46:26 | [diff] [blame] | 664 | #elif defined(OS_POSIX) |
[email protected] | e45e6c0 | 2008-12-15 22:02:17 | [diff] [blame] | 665 | // Please see MessagePumpLibevent for definition. |
| 666 | bool WatchFileDescriptor(int fd, |
| 667 | bool persistent, |
| 668 | Mode mode, |
[email protected] | 320eff4 | 2011-11-15 00:29:48 | [diff] [blame] | 669 | FileDescriptorWatcher* controller, |
| 670 | Watcher* delegate); |
[email protected] | 9cfb89a | 2010-06-09 21:20:41 | [diff] [blame] | 671 | |
| 672 | private: |
| 673 | base::MessagePumpLibevent* pump_io() { |
| 674 | return static_cast<base::MessagePumpLibevent*>(pump_.get()); |
| 675 | } |
[email protected] | 1a8f5d1d | 2008-09-25 20:33:04 | [diff] [blame] | 676 | #endif // defined(OS_POSIX) |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 677 | }; |
| 678 | |
| 679 | // Do not add any member variables to MessageLoopForIO! This is important b/c |
| 680 | // MessageLoopForIO is often allocated via MessageLoop(TYPE_IO). Any extra |
| 681 | // data that you need should be stored on the MessageLoop's pump_ instance. |
| 682 | COMPILE_ASSERT(sizeof(MessageLoop) == sizeof(MessageLoopForIO), |
| 683 | MessageLoopForIO_should_not_have_extra_member_variables); |
| 684 | |
| 685 | #endif // BASE_MESSAGE_LOOP_H_ |