license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1 | // Copyright (c) 2006-2008 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. |
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_ |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 7 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 8 | #include <queue> |
| 9 | #include <string> |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 10 | |
| 11 | #include "base/histogram.h" |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 12 | #include "base/message_pump.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 13 | #include "base/observer_list.h" |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 14 | #include "base/ref_counted.h" |
[email protected] | 1fec840 | 2009-03-13 19:11:59 | [diff] [blame] | 15 | #include "base/scoped_ptr.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 16 | #include "base/task.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 17 | |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 18 | #if defined(OS_WIN) |
| 19 | // We need this to declare base::MessagePumpWin::Dispatcher, which we should |
| 20 | // really just eliminate. |
| 21 | #include "base/message_pump_win.h" |
[email protected] | 36987e9 | 2008-09-18 18:46:26 | [diff] [blame] | 22 | #elif defined(OS_POSIX) |
| 23 | #include "base/message_pump_libevent.h" |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 24 | #endif |
[email protected] | faabcf4 | 2009-05-18 21:12:34 | [diff] [blame] | 25 | #if defined(OS_LINUX) |
| 26 | #include "base/message_pump_glib.h" |
| 27 | #endif |
[email protected] | ea15e98 | 2008-08-15 07:31:20 | [diff] [blame] | 28 | |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 29 | // A MessageLoop is used to process events for a particular thread. There is |
| 30 | // at most one MessageLoop instance per thread. |
| 31 | // |
| 32 | // Events include at a minimum Task instances submitted to PostTask or those |
| 33 | // managed by TimerManager. Depending on the type of message pump used by the |
| 34 | // MessageLoop other events such as UI messages may be processed. On Windows |
| 35 | // APC calls (as time permits) and signals sent to a registered set of HANDLEs |
| 36 | // may also be processed. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 37 | // |
| 38 | // NOTE: Unless otherwise specified, a MessageLoop's methods may only be called |
| 39 | // on the thread where the MessageLoop's Run method executes. |
| 40 | // |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 41 | // NOTE: MessageLoop has task reentrancy protection. This means that if a |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 42 | // 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] | 43 | // finished. Reentrancy can happen when processing a task, and an inner |
| 44 | // message pump is created. That inner pump then processes native messages |
| 45 | // which could implicitly start an inner task. Inner message pumps are created |
| 46 | // with dialogs (DialogBox), common dialogs (GetOpenFileName), OLE functions |
| 47 | // (DoDragDrop), printer functions (StartDoc) and *many* others. |
| 48 | // |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 49 | // Sample workaround when inner task processing is needed: |
| 50 | // bool old_state = MessageLoop::current()->NestableTasksAllowed(); |
| 51 | // MessageLoop::current()->SetNestableTasksAllowed(true); |
| 52 | // HRESULT hr = DoDragDrop(...); // Implicitly runs a modal message loop here. |
| 53 | // MessageLoop::current()->SetNestableTasksAllowed(old_state); |
| 54 | // // Process hr (the result returned by DoDragDrop(). |
| 55 | // |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 56 | // Please be SURE your task is reentrant (nestable) and all global variables |
| 57 | // are stable and accessible before calling SetNestableTasksAllowed(true). |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 58 | // |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 59 | class MessageLoop : public base::MessagePump::Delegate { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 60 | public: |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 61 | static void EnableHistogrammer(bool enable_histogrammer); |
| 62 | |
[email protected] | ee132ad | 2008-08-06 21:27:02 | [diff] [blame] | 63 | // A DestructionObserver is notified when the current MessageLoop is being |
| 64 | // destroyed. These obsevers are notified prior to MessageLoop::current() |
| 65 | // being changed to return NULL. This gives interested parties the chance to |
| 66 | // do final cleanup that depends on the MessageLoop. |
| 67 | // |
| 68 | // NOTE: Any tasks posted to the MessageLoop during this notification will |
| 69 | // not be run. Instead, they will be deleted. |
| 70 | // |
| 71 | class DestructionObserver { |
| 72 | public: |
| 73 | virtual ~DestructionObserver() {} |
| 74 | virtual void WillDestroyCurrentMessageLoop() = 0; |
| 75 | }; |
| 76 | |
| 77 | // Add a DestructionObserver, which will start receiving notifications |
| 78 | // immediately. |
| 79 | void AddDestructionObserver(DestructionObserver* destruction_observer); |
| 80 | |
| 81 | // Remove a DestructionObserver. It is safe to call this method while a |
| 82 | // DestructionObserver is receiving a notification callback. |
| 83 | void RemoveDestructionObserver(DestructionObserver* destruction_observer); |
| 84 | |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 85 | // The "PostTask" family of methods call the task's Run method asynchronously |
| 86 | // from within a message loop at some point in the future. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 87 | // |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 88 | // With the PostTask variant, tasks are invoked in FIFO order, inter-mixed |
| 89 | // with normal UI or IO event processing. With the PostDelayedTask variant, |
| 90 | // tasks are called after at least approximately 'delay_ms' have elapsed. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 91 | // |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 92 | // The NonNestable variants work similarly except that they promise never to |
| 93 | // dispatch the task from a nested invocation of MessageLoop::Run. Instead, |
| 94 | // such tasks get deferred until the top-most MessageLoop::Run is executing. |
| 95 | // |
| 96 | // The MessageLoop takes ownership of the Task, and deletes it after it has |
| 97 | // been Run(). |
| 98 | // |
| 99 | // 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] | 100 | // on the thread that executes MessageLoop::Run(). |
[email protected] | b099217 | 2008-10-27 18:54:18 | [diff] [blame] | 101 | |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 102 | void PostTask( |
| 103 | const tracked_objects::Location& from_here, Task* task); |
[email protected] | b099217 | 2008-10-27 18:54:18 | [diff] [blame] | 104 | |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 105 | void PostDelayedTask( |
[email protected] | 743ace4 | 2009-06-17 17:23:51 | [diff] [blame^] | 106 | const tracked_objects::Location& from_here, Task* task, int64 delay_ms); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 107 | |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 108 | void PostNonNestableTask( |
| 109 | const tracked_objects::Location& from_here, Task* task); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 110 | |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 111 | void PostNonNestableDelayedTask( |
[email protected] | 743ace4 | 2009-06-17 17:23:51 | [diff] [blame^] | 112 | const tracked_objects::Location& from_here, Task* task, int64 delay_ms); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 113 | |
| 114 | // A variant on PostTask that deletes the given object. This is useful |
| 115 | // if the object needs to live until the next run of the MessageLoop (for |
| 116 | // example, deleting a RenderProcessHost from within an IPC callback is not |
| 117 | // good). |
| 118 | // |
| 119 | // NOTE: This method may be called on any thread. The object will be deleted |
| 120 | // on the thread that executes MessageLoop::Run(). If this is not the same |
| 121 | // as the thread that calls PostDelayedTask(FROM_HERE, ), then T MUST inherit |
| 122 | // from RefCountedThreadSafe<T>! |
| 123 | template <class T> |
| 124 | void DeleteSoon(const tracked_objects::Location& from_here, T* object) { |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 125 | PostNonNestableTask(from_here, new DeleteTask<T>(object)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | // A variant on PostTask that releases the given reference counted object |
| 129 | // (by calling its Release method). This is useful if the object needs to |
| 130 | // live until the next run of the MessageLoop, or if the object needs to be |
| 131 | // released on a particular thread. |
| 132 | // |
| 133 | // NOTE: This method may be called on any thread. The object will be |
| 134 | // released (and thus possibly deleted) on the thread that executes |
| 135 | // MessageLoop::Run(). If this is not the same as the thread that calls |
| 136 | // PostDelayedTask(FROM_HERE, ), then T MUST inherit from |
| 137 | // RefCountedThreadSafe<T>! |
| 138 | template <class T> |
| 139 | void ReleaseSoon(const tracked_objects::Location& from_here, T* object) { |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 140 | PostNonNestableTask(from_here, new ReleaseTask<T>(object)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 141 | } |
| 142 | |
[email protected] | 3882c433 | 2008-07-30 19:03:59 | [diff] [blame] | 143 | // Run the message loop. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 144 | void Run(); |
| 145 | |
[email protected] | 7e0e876 | 2008-07-31 13:10:20 | [diff] [blame] | 146 | // Process all pending tasks, windows messages, etc., but don't wait/sleep. |
| 147 | // Return as soon as all items that can be run are taken care of. |
| 148 | void RunAllPending(); |
[email protected] | 3882c433 | 2008-07-30 19:03:59 | [diff] [blame] | 149 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 150 | // Signals the Run method to return after it is done processing all pending |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 151 | // messages. This method may only be called on the same thread that called |
| 152 | // Run, and Run must still be on the call stack. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 153 | // |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 154 | // Use QuitTask if you need to Quit another thread's MessageLoop, but note |
| 155 | // that doing so is fairly dangerous if the target thread makes nested calls |
| 156 | // to MessageLoop::Run. The problem being that you won't know which nested |
| 157 | // run loop you are quiting, so be careful! |
| 158 | // |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 159 | void Quit(); |
| 160 | |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 161 | // Invokes Quit on the current MessageLoop when run. Useful to schedule an |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 162 | // arbitrary MessageLoop to Quit. |
| 163 | class QuitTask : public Task { |
| 164 | public: |
| 165 | virtual void Run() { |
| 166 | MessageLoop::current()->Quit(); |
| 167 | } |
| 168 | }; |
| 169 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 170 | // A MessageLoop has a particular type, which indicates the set of |
| 171 | // asynchronous events it may process in addition to tasks and timers. |
| 172 | // |
| 173 | // TYPE_DEFAULT |
| 174 | // This type of ML only supports tasks and timers. |
| 175 | // |
| 176 | // TYPE_UI |
| 177 | // This type of ML also supports native UI events (e.g., Windows messages). |
| 178 | // See also MessageLoopForUI. |
| 179 | // |
| 180 | // TYPE_IO |
| 181 | // This type of ML also supports asynchronous IO. See also |
| 182 | // MessageLoopForIO. |
| 183 | // |
| 184 | enum Type { |
| 185 | TYPE_DEFAULT, |
| 186 | TYPE_UI, |
| 187 | TYPE_IO |
| 188 | }; |
| 189 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 190 | // Normally, it is not necessary to instantiate a MessageLoop. Instead, it |
| 191 | // is typical to make use of the current thread's MessageLoop instance. |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 192 | explicit MessageLoop(Type type = TYPE_DEFAULT); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 193 | ~MessageLoop(); |
| 194 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 195 | // Returns the type passed to the constructor. |
| 196 | Type type() const { return type_; } |
| 197 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 198 | // Optional call to connect the thread name with this loop. |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 199 | void set_thread_name(const std::string& thread_name) { |
| 200 | DCHECK(thread_name_.empty()) << "Should not rename this thread!"; |
| 201 | thread_name_ = thread_name; |
| 202 | } |
[email protected] | ee132ad | 2008-08-06 21:27:02 | [diff] [blame] | 203 | const std::string& thread_name() const { return thread_name_; } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 204 | |
| 205 | // Returns the MessageLoop object for the current thread, or null if none. |
[email protected] | f886b7bf | 2008-09-10 10:54:06 | [diff] [blame] | 206 | static MessageLoop* current(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 207 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 208 | // Enables or disables the recursive task processing. This happens in the case |
| 209 | // of recursive message loops. Some unwanted message loop may occurs when |
| 210 | // using common controls or printer functions. By default, recursive task |
| 211 | // processing is disabled. |
| 212 | // |
| 213 | // The specific case where tasks get queued is: |
| 214 | // - The thread is running a message loop. |
| 215 | // - It receives a task #1 and execute it. |
| 216 | // - The task #1 implicitly start a message loop, like a MessageBox in the |
| 217 | // unit test. This can also be StartDoc or GetSaveFileName. |
| 218 | // - The thread receives a task #2 before or while in this second message |
| 219 | // loop. |
| 220 | // - With NestableTasksAllowed set to true, the task #2 will run right away. |
| 221 | // Otherwise, it will get executed right after task #1 completes at "thread |
| 222 | // message loop level". |
| 223 | void SetNestableTasksAllowed(bool allowed); |
| 224 | bool NestableTasksAllowed() const; |
| 225 | |
| 226 | // Enables or disables the restoration during an exception of the unhandled |
| 227 | // exception filter that was active when Run() was called. This can happen |
| 228 | // if some third party code call SetUnhandledExceptionFilter() and never |
| 229 | // restores the previous filter. |
| 230 | void set_exception_restoration(bool restore) { |
| 231 | exception_restoration_ = restore; |
| 232 | } |
| 233 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 234 | //---------------------------------------------------------------------------- |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 235 | protected: |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 236 | struct RunState { |
| 237 | // Used to count how many Run() invocations are on the stack. |
| 238 | int run_depth; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 239 | |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 240 | // Used to record that Quit() was called, or that we should quit the pump |
| 241 | // once it becomes idle. |
| 242 | bool quit_received; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 243 | |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 244 | #if defined(OS_WIN) |
| 245 | base::MessagePumpWin::Dispatcher* dispatcher; |
| 246 | #endif |
| 247 | }; |
| 248 | |
| 249 | class AutoRunState : RunState { |
| 250 | public: |
[email protected] | b099217 | 2008-10-27 18:54:18 | [diff] [blame] | 251 | explicit AutoRunState(MessageLoop* loop); |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 252 | ~AutoRunState(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 253 | private: |
| 254 | MessageLoop* loop_; |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 255 | RunState* previous_state_; |
| 256 | }; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 257 | |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 258 | // This structure is copied around by value. |
| 259 | struct PendingTask { |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 260 | Task* task; // The task to run. |
| 261 | base::Time delayed_run_time; // The time when the task should be run. |
| 262 | int sequence_num; // Used to facilitate sorting by run time. |
| 263 | bool nestable; // True if OK to dispatch from a nested loop. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 264 | |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 265 | PendingTask(Task* task, bool nestable) |
| 266 | : task(task), sequence_num(0), nestable(nestable) { |
| 267 | } |
[email protected] | b099217 | 2008-10-27 18:54:18 | [diff] [blame] | 268 | |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 269 | // Used to support sorting. |
| 270 | bool operator<(const PendingTask& other) const; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 271 | }; |
| 272 | |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 273 | typedef std::queue<PendingTask> TaskQueue; |
[email protected] | 4bed5d8 | 2008-12-17 03:50:05 | [diff] [blame] | 274 | typedef std::priority_queue<PendingTask> DelayedTaskQueue; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 275 | |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 276 | #if defined(OS_WIN) |
| 277 | base::MessagePumpWin* pump_win() { |
| 278 | return static_cast<base::MessagePumpWin*>(pump_.get()); |
| 279 | } |
[email protected] | 36987e9 | 2008-09-18 18:46:26 | [diff] [blame] | 280 | #elif defined(OS_POSIX) |
| 281 | base::MessagePumpLibevent* pump_libevent() { |
| 282 | return static_cast<base::MessagePumpLibevent*>(pump_.get()); |
| 283 | } |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 284 | #endif |
[email protected] | 3882c433 | 2008-07-30 19:03:59 | [diff] [blame] | 285 | |
| 286 | // A function to encapsulate all the exception handling capability in the |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 287 | // stacks around the running of a main message loop. It will run the message |
| 288 | // loop in a SEH try block or not depending on the set_SEH_restoration() |
| 289 | // flag. |
| 290 | void RunHandler(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 291 | |
[email protected] | 3882c433 | 2008-07-30 19:03:59 | [diff] [blame] | 292 | // A surrounding stack frame around the running of the message loop that |
| 293 | // supports all saving and restoring of state, as is needed for any/all (ugly) |
| 294 | // recursive calls. |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 295 | void RunInternal(); |
[email protected] | ea15e98 | 2008-08-15 07:31:20 | [diff] [blame] | 296 | |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 297 | // Called to process any delayed non-nestable tasks. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 298 | bool ProcessNextDelayedNonNestableTask(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 299 | |
| 300 | //---------------------------------------------------------------------------- |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 301 | // Run a work_queue_ task or new_task, and delete it (if it was processed by |
| 302 | // PostTask). If there are queued tasks, the oldest one is executed and |
| 303 | // new_task is queued. new_task is optional and can be NULL. In this NULL |
| 304 | // case, the method will run one pending task (if any exist). Returns true if |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 305 | // it executes a task. Queued tasks accumulate only when there is a |
| 306 | // non-nestable task currently processing, in which case the new_task is |
| 307 | // appended to the list work_queue_. Such re-entrancy generally happens when |
| 308 | // an unrequested message pump (typical of a native dialog) is executing in |
| 309 | // the context of a task. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 310 | bool QueueOrRunTask(Task* new_task); |
| 311 | |
| 312 | // Runs the specified task and deletes it. |
| 313 | void RunTask(Task* task); |
| 314 | |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 315 | // Calls RunTask or queues the pending_task on the deferred task list if it |
| 316 | // cannot be run right now. Returns true if the task was run. |
| 317 | bool DeferOrRunPendingTask(const PendingTask& pending_task); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 318 | |
[email protected] | 001747c | 2008-09-10 00:37:07 | [diff] [blame] | 319 | // Adds the pending task to delayed_work_queue_. |
| 320 | void AddToDelayedWorkQueue(const PendingTask& pending_task); |
| 321 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 322 | // Load tasks from the incoming_queue_ into work_queue_ if the latter is |
| 323 | // empty. The former requires a lock to access, while the latter is directly |
| 324 | // accessible on this thread. |
| 325 | void ReloadWorkQueue(); |
| 326 | |
| 327 | // Delete tasks that haven't run yet without running them. Used in the |
[email protected] | 001747c | 2008-09-10 00:37:07 | [diff] [blame] | 328 | // destructor to make sure all the task's destructors get called. Returns |
| 329 | // true if some work was done. |
| 330 | bool DeletePendingTasks(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 331 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 332 | // Post a task to our incomming queue. |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 333 | void PostTask_Helper(const tracked_objects::Location& from_here, Task* task, |
[email protected] | 743ace4 | 2009-06-17 17:23:51 | [diff] [blame^] | 334 | int64 delay_ms, bool nestable); |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 335 | |
| 336 | // base::MessagePump::Delegate methods: |
| 337 | virtual bool DoWork(); |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 338 | virtual bool DoDelayedWork(base::Time* next_delayed_work_time); |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 339 | virtual bool DoIdleWork(); |
| 340 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 341 | // Start recording histogram info about events and action IF it was enabled |
| 342 | // and IF the statistics recorder can accept a registration of our histogram. |
| 343 | void StartHistogrammer(); |
| 344 | |
| 345 | // Add occurence of event to our histogram, so that we can see what is being |
| 346 | // done in a specific MessageLoop instance (i.e., specific thread). |
| 347 | // If message_histogram_ is NULL, this is a no-op. |
| 348 | void HistogramEvent(int event); |
| 349 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 350 | static const LinearHistogram::DescriptionPair event_descriptions_[]; |
| 351 | static bool enable_histogrammer_; |
| 352 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 353 | Type type_; |
| 354 | |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 355 | // A list of tasks that need to be processed by this instance. Note that |
| 356 | // this queue is only accessed (push/pop) by our current thread. |
| 357 | TaskQueue work_queue_; |
[email protected] | b099217 | 2008-10-27 18:54:18 | [diff] [blame] | 358 | |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 359 | // Contains delayed tasks, sorted by their 'delayed_run_time' property. |
| 360 | DelayedTaskQueue delayed_work_queue_; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 361 | |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 362 | // A queue of non-nestable tasks that we had to defer because when it came |
| 363 | // time to execute them we were in a nested message loop. They will execute |
| 364 | // once we're out of nested message loops. |
| 365 | TaskQueue deferred_non_nestable_work_queue_; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 366 | |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 367 | scoped_refptr<base::MessagePump> pump_; |
[email protected] | ee132ad | 2008-08-06 21:27:02 | [diff] [blame] | 368 | |
[email protected] | 2a12725 | 2008-08-05 23:16:41 | [diff] [blame] | 369 | ObserverList<DestructionObserver> destruction_observers_; |
[email protected] | 001747c | 2008-09-10 00:37:07 | [diff] [blame] | 370 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 371 | // A recursion block that prevents accidentally running additonal tasks when |
| 372 | // insider a (accidentally induced?) nested message pump. |
| 373 | bool nestable_tasks_allowed_; |
| 374 | |
| 375 | bool exception_restoration_; |
| 376 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 377 | std::string thread_name_; |
| 378 | // A profiling histogram showing the counts of various messages and events. |
| 379 | scoped_ptr<LinearHistogram> message_histogram_; |
| 380 | |
| 381 | // A null terminated list which creates an incoming_queue of tasks that are |
| 382 | // aquired under a mutex for processing on this instance's thread. These tasks |
| 383 | // have not yet been sorted out into items for our work_queue_ vs items that |
| 384 | // will be handled by the TimerManager. |
| 385 | TaskQueue incoming_queue_; |
| 386 | // Protect access to incoming_queue_. |
| 387 | Lock incoming_queue_lock_; |
| 388 | |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 389 | RunState* state_; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 390 | |
[email protected] | 75257856 | 2008-09-07 08:08:29 | [diff] [blame] | 391 | // The next sequence number to use for delayed tasks. |
| 392 | int next_sequence_num_; |
| 393 | |
[email protected] | fc7fb6e | 2008-08-16 03:09:05 | [diff] [blame] | 394 | DISALLOW_COPY_AND_ASSIGN(MessageLoop); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 395 | }; |
| 396 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 397 | //----------------------------------------------------------------------------- |
| 398 | // MessageLoopForUI extends MessageLoop with methods that are particular to a |
| 399 | // MessageLoop instantiated with TYPE_UI. |
| 400 | // |
| 401 | // This class is typically used like so: |
| 402 | // MessageLoopForUI::current()->...call some method... |
| 403 | // |
| 404 | class MessageLoopForUI : public MessageLoop { |
| 405 | public: |
| 406 | MessageLoopForUI() : MessageLoop(TYPE_UI) { |
| 407 | } |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 408 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 409 | // Returns the MessageLoopForUI of the current thread. |
| 410 | static MessageLoopForUI* current() { |
| 411 | MessageLoop* loop = MessageLoop::current(); |
| 412 | DCHECK_EQ(MessageLoop::TYPE_UI, loop->type()); |
| 413 | return static_cast<MessageLoopForUI*>(loop); |
| 414 | } |
| 415 | |
[email protected] | faabcf4 | 2009-05-18 21:12:34 | [diff] [blame] | 416 | #if defined(OS_LINUX) |
| 417 | typedef base::MessagePumpForUI::Observer Observer; |
| 418 | |
| 419 | // See message_pump_glib for definitions of these methods. |
| 420 | void AddObserver(Observer* observer); |
| 421 | void RemoveObserver(Observer* observer); |
| 422 | #endif |
| 423 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 424 | #if defined(OS_WIN) |
| 425 | typedef base::MessagePumpWin::Dispatcher Dispatcher; |
| 426 | typedef base::MessagePumpWin::Observer Observer; |
| 427 | |
| 428 | // Please see MessagePumpWin for definitions of these methods. |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 429 | void AddObserver(Observer* observer); |
| 430 | void RemoveObserver(Observer* observer); |
[email protected] | faabcf4 | 2009-05-18 21:12:34 | [diff] [blame] | 431 | void Run(Dispatcher* dispatcher); |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 432 | void WillProcessMessage(const MSG& message); |
| 433 | void DidProcessMessage(const MSG& message); |
| 434 | void PumpOutPendingPaintMessages(); |
[email protected] | faabcf4 | 2009-05-18 21:12:34 | [diff] [blame] | 435 | #endif |
[email protected] | 1a8f5d1d | 2008-09-25 20:33:04 | [diff] [blame] | 436 | |
[email protected] | faabcf4 | 2009-05-18 21:12:34 | [diff] [blame] | 437 | #if defined(OS_WIN) || defined(OS_LINUX) |
[email protected] | 1a8f5d1d | 2008-09-25 20:33:04 | [diff] [blame] | 438 | protected: |
| 439 | // TODO(rvargas): Make this platform independent. |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 440 | base::MessagePumpForUI* pump_ui() { |
[email protected] | 1a8f5d1d | 2008-09-25 20:33:04 | [diff] [blame] | 441 | return static_cast<base::MessagePumpForUI*>(pump_.get()); |
| 442 | } |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 443 | #endif // defined(OS_WIN) |
| 444 | }; |
| 445 | |
| 446 | // Do not add any member variables to MessageLoopForUI! This is important b/c |
| 447 | // MessageLoopForUI is often allocated via MessageLoop(TYPE_UI). Any extra |
| 448 | // data that you need should be stored on the MessageLoop's pump_ instance. |
| 449 | COMPILE_ASSERT(sizeof(MessageLoop) == sizeof(MessageLoopForUI), |
| 450 | MessageLoopForUI_should_not_have_extra_member_variables); |
| 451 | |
| 452 | //----------------------------------------------------------------------------- |
| 453 | // MessageLoopForIO extends MessageLoop with methods that are particular to a |
| 454 | // MessageLoop instantiated with TYPE_IO. |
| 455 | // |
| 456 | // This class is typically used like so: |
| 457 | // MessageLoopForIO::current()->...call some method... |
| 458 | // |
| 459 | class MessageLoopForIO : public MessageLoop { |
| 460 | public: |
| 461 | MessageLoopForIO() : MessageLoop(TYPE_IO) { |
| 462 | } |
| 463 | |
| 464 | // Returns the MessageLoopForIO of the current thread. |
| 465 | static MessageLoopForIO* current() { |
| 466 | MessageLoop* loop = MessageLoop::current(); |
| 467 | DCHECK_EQ(MessageLoop::TYPE_IO, loop->type()); |
| 468 | return static_cast<MessageLoopForIO*>(loop); |
| 469 | } |
| 470 | |
| 471 | #if defined(OS_WIN) |
[email protected] | 32cda29d | 2008-10-09 23:58:43 | [diff] [blame] | 472 | typedef base::MessagePumpForIO::IOHandler IOHandler; |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 473 | typedef base::MessagePumpForIO::IOContext IOContext; |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 474 | |
| 475 | // Please see MessagePumpWin for definitions of these methods. |
[email protected] | 32cda29d | 2008-10-09 23:58:43 | [diff] [blame] | 476 | void RegisterIOHandler(HANDLE file_handle, IOHandler* handler); |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 477 | bool WaitForIOCompletion(DWORD timeout, IOHandler* filter); |
[email protected] | 36987e9 | 2008-09-18 18:46:26 | [diff] [blame] | 478 | |
[email protected] | 1a8f5d1d | 2008-09-25 20:33:04 | [diff] [blame] | 479 | protected: |
| 480 | // TODO(rvargas): Make this platform independent. |
| 481 | base::MessagePumpForIO* pump_io() { |
| 482 | return static_cast<base::MessagePumpForIO*>(pump_.get()); |
| 483 | } |
| 484 | |
[email protected] | 36987e9 | 2008-09-18 18:46:26 | [diff] [blame] | 485 | #elif defined(OS_POSIX) |
| 486 | typedef base::MessagePumpLibevent::Watcher Watcher; |
[email protected] | e45e6c0 | 2008-12-15 22:02:17 | [diff] [blame] | 487 | typedef base::MessagePumpLibevent::FileDescriptorWatcher |
| 488 | FileDescriptorWatcher; |
[email protected] | 36987e9 | 2008-09-18 18:46:26 | [diff] [blame] | 489 | |
[email protected] | e45e6c0 | 2008-12-15 22:02:17 | [diff] [blame] | 490 | enum Mode { |
| 491 | WATCH_READ = base::MessagePumpLibevent::WATCH_READ, |
| 492 | WATCH_WRITE = base::MessagePumpLibevent::WATCH_WRITE, |
| 493 | WATCH_READ_WRITE = base::MessagePumpLibevent::WATCH_READ_WRITE |
| 494 | }; |
| 495 | |
| 496 | // Please see MessagePumpLibevent for definition. |
| 497 | bool WatchFileDescriptor(int fd, |
| 498 | bool persistent, |
| 499 | Mode mode, |
| 500 | FileDescriptorWatcher *controller, |
| 501 | Watcher *delegate); |
[email protected] | 1a8f5d1d | 2008-09-25 20:33:04 | [diff] [blame] | 502 | #endif // defined(OS_POSIX) |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 503 | }; |
| 504 | |
| 505 | // Do not add any member variables to MessageLoopForIO! This is important b/c |
| 506 | // MessageLoopForIO is often allocated via MessageLoop(TYPE_IO). Any extra |
| 507 | // data that you need should be stored on the MessageLoop's pump_ instance. |
| 508 | COMPILE_ASSERT(sizeof(MessageLoop) == sizeof(MessageLoopForIO), |
| 509 | MessageLoopForIO_should_not_have_extra_member_variables); |
| 510 | |
| 511 | #endif // BASE_MESSAGE_LOOP_H_ |