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