blob: fd9fb7e33db0048e15ccd46a3829ea7a161ec4ec [file] [log] [blame]
[email protected]a502bbe72011-01-07 18:06:451// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commitd7cae122008-07-26 21:49:384
[email protected]fc7fb6e2008-08-16 03:09:055#ifndef BASE_MESSAGE_LOOP_H_
6#define BASE_MESSAGE_LOOP_H_
[email protected]32b76ef2010-07-26 23:08:247#pragma once
initial.commitd7cae122008-07-26 21:49:388
initial.commitd7cae122008-07-26 21:49:389#include <queue>
10#include <string>
initial.commitd7cae122008-07-26 21:49:3811
[email protected]0bea7252011-08-05 15:34:0012#include "base/base_export.h"
[email protected]9cfb89a2010-06-09 21:20:4113#include "base/basictypes.h"
[email protected]b224f792011-04-20 16:02:2314#include "base/callback.h"
[email protected]3b63f8f42011-03-28 01:54:1515#include "base/memory/ref_counted.h"
[email protected]fc7fb6e2008-08-16 03:09:0516#include "base/message_pump.h"
initial.commitd7cae122008-07-26 21:49:3817#include "base/observer_list.h"
[email protected]20305ec2011-01-21 04:55:5218#include "base/synchronization/lock.h"
initial.commitd7cae122008-07-26 21:49:3819#include "base/task.h"
[email protected]b224f792011-04-20 16:02:2320#include "base/time.h"
21#include "base/tracked.h"
initial.commitd7cae122008-07-26 21:49:3822
[email protected]fc7fb6e2008-08-16 03:09:0523#if defined(OS_WIN)
24// We need this to declare base::MessagePumpWin::Dispatcher, which we should
25// really just eliminate.
26#include "base/message_pump_win.h"
[email protected]36987e92008-09-18 18:46:2627#elif defined(OS_POSIX)
28#include "base/message_pump_libevent.h"
[email protected]e43eddf12009-12-29 00:32:5229#if !defined(OS_MACOSX)
[email protected]2047ef42011-06-24 20:10:2530#if defined(TOUCH_UI)
31#include "base/message_pump_x.h"
32#else
33#include "base/message_pump_gtk.h"
34#endif
[email protected]faabcf42009-05-18 21:12:3435#endif
[email protected]e43eddf12009-12-29 00:32:5236#endif
[email protected]ea15e982008-08-15 07:31:2037
[email protected]835d7c82010-10-14 04:38:3838namespace base {
[email protected]5097dc82010-07-15 17:23:2339class Histogram;
[email protected]835d7c82010-10-14 04:38:3840}
[email protected]5097dc82010-07-15 17:23:2341
[email protected]b224f792011-04-20 16:02:2342#if defined(TRACK_ALL_TASK_OBJECTS)
43namespace tracked_objects {
44class Births;
45}
46#endif // defined(TRACK_ALL_TASK_OBJECTS)
47
[email protected]fc7fb6e2008-08-16 03:09:0548// A MessageLoop is used to process events for a particular thread. There is
49// at most one MessageLoop instance per thread.
50//
51// Events include at a minimum Task instances submitted to PostTask or those
52// managed by TimerManager. Depending on the type of message pump used by the
53// MessageLoop other events such as UI messages may be processed. On Windows
54// APC calls (as time permits) and signals sent to a registered set of HANDLEs
55// may also be processed.
initial.commitd7cae122008-07-26 21:49:3856//
57// NOTE: Unless otherwise specified, a MessageLoop's methods may only be called
58// on the thread where the MessageLoop's Run method executes.
59//
[email protected]fc7fb6e2008-08-16 03:09:0560// NOTE: MessageLoop has task reentrancy protection. This means that if a
initial.commitd7cae122008-07-26 21:49:3861// task is being processed, a second task cannot start until the first task is
[email protected]fc7fb6e2008-08-16 03:09:0562// finished. Reentrancy can happen when processing a task, and an inner
63// message pump is created. That inner pump then processes native messages
64// which could implicitly start an inner task. Inner message pumps are created
65// with dialogs (DialogBox), common dialogs (GetOpenFileName), OLE functions
66// (DoDragDrop), printer functions (StartDoc) and *many* others.
67//
initial.commitd7cae122008-07-26 21:49:3868// Sample workaround when inner task processing is needed:
69// bool old_state = MessageLoop::current()->NestableTasksAllowed();
70// MessageLoop::current()->SetNestableTasksAllowed(true);
71// HRESULT hr = DoDragDrop(...); // Implicitly runs a modal message loop here.
72// MessageLoop::current()->SetNestableTasksAllowed(old_state);
73// // Process hr (the result returned by DoDragDrop().
74//
[email protected]fc7fb6e2008-08-16 03:09:0575// Please be SURE your task is reentrant (nestable) and all global variables
76// are stable and accessible before calling SetNestableTasksAllowed(true).
initial.commitd7cae122008-07-26 21:49:3877//
[email protected]0bea7252011-08-05 15:34:0078class BASE_EXPORT MessageLoop : public base::MessagePump::Delegate {
initial.commitd7cae122008-07-26 21:49:3879 public:
[email protected]a502bbe72011-01-07 18:06:4580#if defined(OS_WIN)
81 typedef base::MessagePumpWin::Dispatcher Dispatcher;
82 typedef base::MessagePumpForUI::Observer Observer;
[email protected]5040dfab2011-05-11 20:50:0083#elif !defined(OS_MACOSX)
[email protected]2047ef42011-06-24 20:10:2584 typedef base::MessagePumpDispatcher Dispatcher;
85 typedef base::MessagePumpObserver Observer;
[email protected]a502bbe72011-01-07 18:06:4586#endif
87
88 // A MessageLoop has a particular type, which indicates the set of
89 // asynchronous events it may process in addition to tasks and timers.
[email protected]9cfb89a2010-06-09 21:20:4190 //
[email protected]a502bbe72011-01-07 18:06:4591 // TYPE_DEFAULT
92 // This type of ML only supports tasks and timers.
93 //
94 // TYPE_UI
95 // This type of ML also supports native UI events (e.g., Windows messages).
96 // See also MessageLoopForUI.
97 //
98 // TYPE_IO
99 // This type of ML also supports asynchronous IO. See also
100 // MessageLoopForIO.
101 //
102 enum Type {
103 TYPE_DEFAULT,
104 TYPE_UI,
105 TYPE_IO
[email protected]9cfb89a2010-06-09 21:20:41106 };
107
[email protected]a502bbe72011-01-07 18:06:45108 // Normally, it is not necessary to instantiate a MessageLoop. Instead, it
109 // is typical to make use of the current thread's MessageLoop instance.
110 explicit MessageLoop(Type type = TYPE_DEFAULT);
[email protected]3690ebe02011-05-25 09:08:19111 virtual ~MessageLoop();
[email protected]a502bbe72011-01-07 18:06:45112
[email protected]9989c9bb2011-01-07 20:23:43113 // Returns the MessageLoop object for the current thread, or null if none.
114 static MessageLoop* current();
115
initial.commitd7cae122008-07-26 21:49:38116 static void EnableHistogrammer(bool enable_histogrammer);
117
[email protected]61c86c62011-08-02 16:11:16118 typedef base::MessagePump* (MessagePumpFactory)();
119 // Using the given base::MessagePumpForUIFactory to override the default
120 // MessagePump implementation for 'TYPE_UI'.
121 static void InitMessagePumpForUIFactory(MessagePumpFactory* factory);
122
[email protected]ee132ad2008-08-06 21:27:02123 // A DestructionObserver is notified when the current MessageLoop is being
124 // destroyed. These obsevers are notified prior to MessageLoop::current()
125 // being changed to return NULL. This gives interested parties the chance to
126 // do final cleanup that depends on the MessageLoop.
127 //
128 // NOTE: Any tasks posted to the MessageLoop during this notification will
129 // not be run. Instead, they will be deleted.
130 //
[email protected]0bea7252011-08-05 15:34:00131 class BASE_EXPORT DestructionObserver {
[email protected]ee132ad2008-08-06 21:27:02132 public:
[email protected]ee132ad2008-08-06 21:27:02133 virtual void WillDestroyCurrentMessageLoop() = 0;
[email protected]23c386b2010-09-15 22:14:36134
135 protected:
136 virtual ~DestructionObserver();
[email protected]ee132ad2008-08-06 21:27:02137 };
138
139 // Add a DestructionObserver, which will start receiving notifications
140 // immediately.
141 void AddDestructionObserver(DestructionObserver* destruction_observer);
142
143 // Remove a DestructionObserver. It is safe to call this method while a
144 // DestructionObserver is receiving a notification callback.
145 void RemoveDestructionObserver(DestructionObserver* destruction_observer);
146
[email protected]752578562008-09-07 08:08:29147 // The "PostTask" family of methods call the task's Run method asynchronously
148 // from within a message loop at some point in the future.
initial.commitd7cae122008-07-26 21:49:38149 //
[email protected]752578562008-09-07 08:08:29150 // With the PostTask variant, tasks are invoked in FIFO order, inter-mixed
151 // with normal UI or IO event processing. With the PostDelayedTask variant,
152 // tasks are called after at least approximately 'delay_ms' have elapsed.
initial.commitd7cae122008-07-26 21:49:38153 //
[email protected]752578562008-09-07 08:08:29154 // The NonNestable variants work similarly except that they promise never to
155 // dispatch the task from a nested invocation of MessageLoop::Run. Instead,
156 // such tasks get deferred until the top-most MessageLoop::Run is executing.
157 //
158 // The MessageLoop takes ownership of the Task, and deletes it after it has
159 // been Run().
160 //
161 // NOTE: These methods may be called on any thread. The Task will be invoked
initial.commitd7cae122008-07-26 21:49:38162 // on the thread that executes MessageLoop::Run().
[email protected]b0992172008-10-27 18:54:18163
[email protected]752578562008-09-07 08:08:29164 void PostTask(
165 const tracked_objects::Location& from_here, Task* task);
[email protected]b0992172008-10-27 18:54:18166
[email protected]752578562008-09-07 08:08:29167 void PostDelayedTask(
[email protected]743ace42009-06-17 17:23:51168 const tracked_objects::Location& from_here, Task* task, int64 delay_ms);
initial.commitd7cae122008-07-26 21:49:38169
[email protected]752578562008-09-07 08:08:29170 void PostNonNestableTask(
171 const tracked_objects::Location& from_here, Task* task);
initial.commitd7cae122008-07-26 21:49:38172
[email protected]752578562008-09-07 08:08:29173 void PostNonNestableDelayedTask(
[email protected]743ace42009-06-17 17:23:51174 const tracked_objects::Location& from_here, Task* task, int64 delay_ms);
initial.commitd7cae122008-07-26 21:49:38175
[email protected]b224f792011-04-20 16:02:23176 // TODO(ajwong): Remove the functions above once the Task -> Closure migration
177 // is complete.
178 //
179 // There are 2 sets of Post*Task functions, one which takes the older Task*
180 // function object representation, and one that takes the newer base::Closure.
181 // We have this overload to allow a staged transition between the two systems.
182 // Once the transition is done, the functions above should be deleted.
183 void PostTask(
184 const tracked_objects::Location& from_here,
185 const base::Closure& task);
186
187 void PostDelayedTask(
188 const tracked_objects::Location& from_here,
189 const base::Closure& task, int64 delay_ms);
190
191 void PostNonNestableTask(
192 const tracked_objects::Location& from_here,
193 const base::Closure& task);
194
195 void PostNonNestableDelayedTask(
196 const tracked_objects::Location& from_here,
197 const base::Closure& task, int64 delay_ms);
198
initial.commitd7cae122008-07-26 21:49:38199 // A variant on PostTask that deletes the given object. This is useful
200 // if the object needs to live until the next run of the MessageLoop (for
201 // example, deleting a RenderProcessHost from within an IPC callback is not
202 // good).
203 //
204 // NOTE: This method may be called on any thread. The object will be deleted
205 // on the thread that executes MessageLoop::Run(). If this is not the same
206 // as the thread that calls PostDelayedTask(FROM_HERE, ), then T MUST inherit
207 // from RefCountedThreadSafe<T>!
208 template <class T>
[email protected]00ed48f2010-10-22 22:19:24209 void DeleteSoon(const tracked_objects::Location& from_here, const T* object) {
[email protected]752578562008-09-07 08:08:29210 PostNonNestableTask(from_here, new DeleteTask<T>(object));
initial.commitd7cae122008-07-26 21:49:38211 }
212
213 // A variant on PostTask that releases the given reference counted object
214 // (by calling its Release method). This is useful if the object needs to
215 // live until the next run of the MessageLoop, or if the object needs to be
216 // released on a particular thread.
217 //
218 // NOTE: This method may be called on any thread. The object will be
219 // released (and thus possibly deleted) on the thread that executes
220 // MessageLoop::Run(). If this is not the same as the thread that calls
221 // PostDelayedTask(FROM_HERE, ), then T MUST inherit from
222 // RefCountedThreadSafe<T>!
223 template <class T>
[email protected]00ed48f2010-10-22 22:19:24224 void ReleaseSoon(const tracked_objects::Location& from_here,
225 const T* object) {
[email protected]752578562008-09-07 08:08:29226 PostNonNestableTask(from_here, new ReleaseTask<T>(object));
initial.commitd7cae122008-07-26 21:49:38227 }
228
[email protected]3882c4332008-07-30 19:03:59229 // Run the message loop.
initial.commitd7cae122008-07-26 21:49:38230 void Run();
231
[email protected]7e0e8762008-07-31 13:10:20232 // Process all pending tasks, windows messages, etc., but don't wait/sleep.
233 // Return as soon as all items that can be run are taken care of.
234 void RunAllPending();
[email protected]3882c4332008-07-30 19:03:59235
initial.commitd7cae122008-07-26 21:49:38236 // Signals the Run method to return after it is done processing all pending
[email protected]fc7fb6e2008-08-16 03:09:05237 // messages. This method may only be called on the same thread that called
238 // Run, and Run must still be on the call stack.
initial.commitd7cae122008-07-26 21:49:38239 //
[email protected]fc7fb6e2008-08-16 03:09:05240 // Use QuitTask if you need to Quit another thread's MessageLoop, but note
241 // that doing so is fairly dangerous if the target thread makes nested calls
242 // to MessageLoop::Run. The problem being that you won't know which nested
243 // run loop you are quiting, so be careful!
244 //
initial.commitd7cae122008-07-26 21:49:38245 void Quit();
246
[email protected]781a7ed2010-02-23 07:12:22247 // This method is a variant of Quit, that does not wait for pending messages
248 // to be processed before returning from Run.
249 void QuitNow();
250
[email protected]fc7fb6e2008-08-16 03:09:05251 // Invokes Quit on the current MessageLoop when run. Useful to schedule an
initial.commitd7cae122008-07-26 21:49:38252 // arbitrary MessageLoop to Quit.
253 class QuitTask : public Task {
254 public:
255 virtual void Run() {
256 MessageLoop::current()->Quit();
257 }
258 };
259
[email protected]4d9bdfaf2008-08-26 05:53:57260 // Returns the type passed to the constructor.
261 Type type() const { return type_; }
262
initial.commitd7cae122008-07-26 21:49:38263 // Optional call to connect the thread name with this loop.
[email protected]fc7fb6e2008-08-16 03:09:05264 void set_thread_name(const std::string& thread_name) {
265 DCHECK(thread_name_.empty()) << "Should not rename this thread!";
266 thread_name_ = thread_name;
267 }
[email protected]ee132ad2008-08-06 21:27:02268 const std::string& thread_name() const { return thread_name_; }
initial.commitd7cae122008-07-26 21:49:38269
initial.commitd7cae122008-07-26 21:49:38270 // Enables or disables the recursive task processing. This happens in the case
271 // of recursive message loops. Some unwanted message loop may occurs when
272 // using common controls or printer functions. By default, recursive task
273 // processing is disabled.
274 //
275 // The specific case where tasks get queued is:
276 // - The thread is running a message loop.
277 // - It receives a task #1 and execute it.
278 // - The task #1 implicitly start a message loop, like a MessageBox in the
279 // unit test. This can also be StartDoc or GetSaveFileName.
280 // - The thread receives a task #2 before or while in this second message
281 // loop.
282 // - With NestableTasksAllowed set to true, the task #2 will run right away.
283 // Otherwise, it will get executed right after task #1 completes at "thread
284 // message loop level".
285 void SetNestableTasksAllowed(bool allowed);
286 bool NestableTasksAllowed() const;
287
[email protected]18d6a8f2009-12-16 22:56:33288 // Enables nestable tasks on |loop| while in scope.
289 class ScopedNestableTaskAllower {
290 public:
291 explicit ScopedNestableTaskAllower(MessageLoop* loop)
292 : loop_(loop),
293 old_state_(loop_->NestableTasksAllowed()) {
294 loop_->SetNestableTasksAllowed(true);
295 }
296 ~ScopedNestableTaskAllower() {
297 loop_->SetNestableTasksAllowed(old_state_);
298 }
299
300 private:
301 MessageLoop* loop_;
302 bool old_state_;
303 };
304
initial.commitd7cae122008-07-26 21:49:38305 // Enables or disables the restoration during an exception of the unhandled
306 // exception filter that was active when Run() was called. This can happen
307 // if some third party code call SetUnhandledExceptionFilter() and never
308 // restores the previous filter.
309 void set_exception_restoration(bool restore) {
310 exception_restoration_ = restore;
311 }
312
[email protected]b5f95102009-07-01 19:53:59313 // Returns true if we are currently running a nested message loop.
314 bool IsNested();
315
[email protected]a502bbe72011-01-07 18:06:45316 // A TaskObserver is an object that receives task notifications from the
317 // MessageLoop.
318 //
319 // NOTE: A TaskObserver implementation should be extremely fast!
[email protected]0bea7252011-08-05 15:34:00320 class BASE_EXPORT TaskObserver {
[email protected]a502bbe72011-01-07 18:06:45321 public:
322 TaskObserver();
323
324 // This method is called before processing a task.
[email protected]b224f792011-04-20 16:02:23325 virtual void WillProcessTask(base::TimeTicks time_posted) = 0;
[email protected]a502bbe72011-01-07 18:06:45326
327 // This method is called after processing a task.
[email protected]b224f792011-04-20 16:02:23328 virtual void DidProcessTask(base::TimeTicks time_posted) = 0;
[email protected]a502bbe72011-01-07 18:06:45329
330 protected:
331 virtual ~TaskObserver();
332 };
333
[email protected]9cfb89a2010-06-09 21:20:41334 // These functions can only be called on the same thread that |this| is
335 // running on.
336 void AddTaskObserver(TaskObserver* task_observer);
337 void RemoveTaskObserver(TaskObserver* task_observer);
338
[email protected]57f030a2010-06-29 04:58:15339 // Returns true if the message loop has high resolution timers enabled.
340 // Provided for testing.
341 bool high_resolution_timers_enabled() {
342#if defined(OS_WIN)
343 return !high_resolution_timer_expiration_.is_null();
344#else
345 return true;
346#endif
347 }
348
349 // When we go into high resolution timer mode, we will stay in hi-res mode
350 // for at least 1s.
351 static const int kHighResolutionTimerModeLeaseTimeMs = 1000;
352
[email protected]8d6ab8f52011-01-26 00:53:48353 // Asserts that the MessageLoop is "idle".
354 void AssertIdle() const;
355
[email protected]2ec01fe2011-03-24 03:40:28356#if defined(OS_WIN)
357 void set_os_modal_loop(bool os_modal_loop) {
358 os_modal_loop_ = os_modal_loop;
359 }
360
361 bool os_modal_loop() const {
362 return os_modal_loop_;
363 }
364#endif // OS_WIN
365
initial.commitd7cae122008-07-26 21:49:38366 //----------------------------------------------------------------------------
[email protected]4d9bdfaf2008-08-26 05:53:57367 protected:
[email protected]fc7fb6e2008-08-16 03:09:05368 struct RunState {
369 // Used to count how many Run() invocations are on the stack.
370 int run_depth;
initial.commitd7cae122008-07-26 21:49:38371
[email protected]fc7fb6e2008-08-16 03:09:05372 // Used to record that Quit() was called, or that we should quit the pump
373 // once it becomes idle.
374 bool quit_received;
initial.commitd7cae122008-07-26 21:49:38375
[email protected]61c86c62011-08-02 16:11:16376#if !defined(OS_MACOSX) && !defined(OS_ANDROID)
[email protected]148d1052009-07-31 22:53:37377 Dispatcher* dispatcher;
[email protected]fc7fb6e2008-08-16 03:09:05378#endif
379 };
380
[email protected]61c86c62011-08-02 16:11:16381#if defined(OS_ANDROID)
382 // Android Java process manages the UI thread message loop. So its
383 // MessagePumpForUI needs to keep the RunState.
384 public:
385#endif
[email protected]0bea7252011-08-05 15:34:00386 class BASE_EXPORT AutoRunState : RunState {
[email protected]fc7fb6e2008-08-16 03:09:05387 public:
[email protected]b0992172008-10-27 18:54:18388 explicit AutoRunState(MessageLoop* loop);
[email protected]fc7fb6e2008-08-16 03:09:05389 ~AutoRunState();
initial.commitd7cae122008-07-26 21:49:38390 private:
391 MessageLoop* loop_;
[email protected]fc7fb6e2008-08-16 03:09:05392 RunState* previous_state_;
393 };
[email protected]61c86c62011-08-02 16:11:16394#if defined(OS_ANDROID)
395 protected:
396#endif
initial.commitd7cae122008-07-26 21:49:38397
[email protected]752578562008-09-07 08:08:29398 // This structure is copied around by value.
399 struct PendingTask {
[email protected]b224f792011-04-20 16:02:23400 PendingTask(const base::Closure& task,
401 const tracked_objects::Location& posted_from,
402 base::TimeTicks delayed_run_time,
403 bool nestable);
404 ~PendingTask();
[email protected]b0992172008-10-27 18:54:18405
[email protected]752578562008-09-07 08:08:29406 // Used to support sorting.
407 bool operator<(const PendingTask& other) const;
[email protected]a502bbe72011-01-07 18:06:45408
[email protected]b224f792011-04-20 16:02:23409 // The task to run.
410 base::Closure task;
411
412#if defined(TRACK_ALL_TASK_OBJECTS)
413 // Counter for location where the Closure was posted from.
414 tracked_objects::Births* post_births;
415#endif // defined(TRACK_ALL_TASK_OBJECTS)
416
417 // Time this PendingTask was posted.
418 base::TimeTicks time_posted;
419
420 // The time when the task should be run.
421 base::TimeTicks delayed_run_time;
422
423 // Secondary sort key for run time.
424 int sequence_num;
425
426 // OK to dispatch from a nested loop.
427 bool nestable;
[email protected]fcb30f7b2011-05-19 22:28:25428
429 // The site this PendingTask was posted from.
430 const void* birth_program_counter;
initial.commitd7cae122008-07-26 21:49:38431 };
432
[email protected]b2f0ea12009-09-02 20:05:21433 class TaskQueue : public std::queue<PendingTask> {
434 public:
435 void Swap(TaskQueue* queue) {
436 c.swap(queue->c); // Calls std::deque::swap
437 }
438 };
439
[email protected]4bed5d82008-12-17 03:50:05440 typedef std::priority_queue<PendingTask> DelayedTaskQueue;
initial.commitd7cae122008-07-26 21:49:38441
[email protected]fc7fb6e2008-08-16 03:09:05442#if defined(OS_WIN)
443 base::MessagePumpWin* pump_win() {
444 return static_cast<base::MessagePumpWin*>(pump_.get());
445 }
[email protected]36987e92008-09-18 18:46:26446#elif defined(OS_POSIX)
447 base::MessagePumpLibevent* pump_libevent() {
448 return static_cast<base::MessagePumpLibevent*>(pump_.get());
449 }
[email protected]fc7fb6e2008-08-16 03:09:05450#endif
[email protected]3882c4332008-07-30 19:03:59451
452 // A function to encapsulate all the exception handling capability in the
[email protected]fc7fb6e2008-08-16 03:09:05453 // stacks around the running of a main message loop. It will run the message
454 // loop in a SEH try block or not depending on the set_SEH_restoration()
[email protected]aff8a132009-10-26 18:15:59455 // flag invoking respectively RunInternalInSEHFrame() or RunInternal().
[email protected]fc7fb6e2008-08-16 03:09:05456 void RunHandler();
initial.commitd7cae122008-07-26 21:49:38457
[email protected]aff8a132009-10-26 18:15:59458#if defined(OS_WIN)
459 __declspec(noinline) void RunInternalInSEHFrame();
460#endif
461
[email protected]3882c4332008-07-30 19:03:59462 // A surrounding stack frame around the running of the message loop that
463 // supports all saving and restoring of state, as is needed for any/all (ugly)
464 // recursive calls.
[email protected]fc7fb6e2008-08-16 03:09:05465 void RunInternal();
[email protected]ea15e982008-08-15 07:31:20466
[email protected]fc7fb6e2008-08-16 03:09:05467 // Called to process any delayed non-nestable tasks.
initial.commitd7cae122008-07-26 21:49:38468 bool ProcessNextDelayedNonNestableTask();
initial.commitd7cae122008-07-26 21:49:38469
[email protected]b224f792011-04-20 16:02:23470 // Runs the specified PendingTask.
471 void RunTask(const PendingTask& pending_task);
initial.commitd7cae122008-07-26 21:49:38472
[email protected]752578562008-09-07 08:08:29473 // Calls RunTask or queues the pending_task on the deferred task list if it
474 // cannot be run right now. Returns true if the task was run.
475 bool DeferOrRunPendingTask(const PendingTask& pending_task);
initial.commitd7cae122008-07-26 21:49:38476
[email protected]001747c2008-09-10 00:37:07477 // Adds the pending task to delayed_work_queue_.
478 void AddToDelayedWorkQueue(const PendingTask& pending_task);
479
[email protected]b224f792011-04-20 16:02:23480 // Adds the pending task to our incoming_queue_.
481 //
482 // Caller retains ownership of |pending_task|, but this function will
483 // reset the value of pending_task->task. This is needed to ensure
484 // that the posting call stack does not retain pending_task->task
485 // beyond this function call.
486 void AddToIncomingQueue(PendingTask* pending_task);
487
initial.commitd7cae122008-07-26 21:49:38488 // Load tasks from the incoming_queue_ into work_queue_ if the latter is
489 // empty. The former requires a lock to access, while the latter is directly
490 // accessible on this thread.
491 void ReloadWorkQueue();
492
493 // Delete tasks that haven't run yet without running them. Used in the
[email protected]001747c2008-09-10 00:37:07494 // destructor to make sure all the task's destructors get called. Returns
495 // true if some work was done.
496 bool DeletePendingTasks();
initial.commitd7cae122008-07-26 21:49:38497
[email protected]b224f792011-04-20 16:02:23498 // Calcuates the time at which a PendingTask should run.
499 base::TimeTicks CalculateDelayedRuntime(int64 delay_ms);
[email protected]fc7fb6e2008-08-16 03:09:05500
initial.commitd7cae122008-07-26 21:49:38501 // Start recording histogram info about events and action IF it was enabled
502 // and IF the statistics recorder can accept a registration of our histogram.
503 void StartHistogrammer();
504
505 // Add occurence of event to our histogram, so that we can see what is being
506 // done in a specific MessageLoop instance (i.e., specific thread).
507 // If message_histogram_ is NULL, this is a no-op.
508 void HistogramEvent(int event);
509
[email protected]a502bbe72011-01-07 18:06:45510 // base::MessagePump::Delegate methods:
511 virtual bool DoWork();
512 virtual bool DoDelayedWork(base::TimeTicks* next_delayed_work_time);
513 virtual bool DoIdleWork();
514
[email protected]4d9bdfaf2008-08-26 05:53:57515 Type type_;
516
[email protected]752578562008-09-07 08:08:29517 // A list of tasks that need to be processed by this instance. Note that
518 // this queue is only accessed (push/pop) by our current thread.
519 TaskQueue work_queue_;
[email protected]b0992172008-10-27 18:54:18520
[email protected]752578562008-09-07 08:08:29521 // Contains delayed tasks, sorted by their 'delayed_run_time' property.
522 DelayedTaskQueue delayed_work_queue_;
initial.commitd7cae122008-07-26 21:49:38523
[email protected]a8f7d3d2010-11-04 23:23:42524 // A recent snapshot of Time::Now(), used to check delayed_work_queue_.
[email protected]7e7fab42010-11-06 22:23:29525 base::TimeTicks recent_time_;
[email protected]a8f7d3d2010-11-04 23:23:42526
[email protected]752578562008-09-07 08:08:29527 // A queue of non-nestable tasks that we had to defer because when it came
528 // time to execute them we were in a nested message loop. They will execute
529 // once we're out of nested message loops.
530 TaskQueue deferred_non_nestable_work_queue_;
initial.commitd7cae122008-07-26 21:49:38531
[email protected]fc7fb6e2008-08-16 03:09:05532 scoped_refptr<base::MessagePump> pump_;
[email protected]ee132ad2008-08-06 21:27:02533
[email protected]2a127252008-08-05 23:16:41534 ObserverList<DestructionObserver> destruction_observers_;
[email protected]001747c2008-09-10 00:37:07535
initial.commitd7cae122008-07-26 21:49:38536 // A recursion block that prevents accidentally running additonal tasks when
537 // insider a (accidentally induced?) nested message pump.
538 bool nestable_tasks_allowed_;
539
540 bool exception_restoration_;
541
initial.commitd7cae122008-07-26 21:49:38542 std::string thread_name_;
543 // A profiling histogram showing the counts of various messages and events.
[email protected]81ce9f3b2011-04-05 04:48:53544 base::Histogram* message_histogram_;
initial.commitd7cae122008-07-26 21:49:38545
546 // A null terminated list which creates an incoming_queue of tasks that are
[email protected]242c4bd2011-02-25 18:43:23547 // acquired under a mutex for processing on this instance's thread. These
[email protected]b224f792011-04-20 16:02:23548 // tasks have not yet been sorted out into items for our work_queue_ vs items
549 // that will be handled by the TimerManager.
initial.commitd7cae122008-07-26 21:49:38550 TaskQueue incoming_queue_;
551 // Protect access to incoming_queue_.
[email protected]8d6ab8f52011-01-26 00:53:48552 mutable base::Lock incoming_queue_lock_;
initial.commitd7cae122008-07-26 21:49:38553
[email protected]fc7fb6e2008-08-16 03:09:05554 RunState* state_;
initial.commitd7cae122008-07-26 21:49:38555
[email protected]b224f792011-04-20 16:02:23556 // The need for this variable is subtle. Please see implementation comments
557 // around where it is used.
558 bool should_leak_tasks_;
559
[email protected]57f030a2010-06-29 04:58:15560#if defined(OS_WIN)
561 base::TimeTicks high_resolution_timer_expiration_;
[email protected]2ec01fe2011-03-24 03:40:28562 // Should be set to true before calling Windows APIs like TrackPopupMenu, etc
563 // which enter a modal message loop.
564 bool os_modal_loop_;
[email protected]57f030a2010-06-29 04:58:15565#endif
566
[email protected]752578562008-09-07 08:08:29567 // The next sequence number to use for delayed tasks.
568 int next_sequence_num_;
569
[email protected]9cfb89a2010-06-09 21:20:41570 ObserverList<TaskObserver> task_observers_;
571
[email protected]26fbf802011-03-25 18:48:03572 private:
[email protected]fc7fb6e2008-08-16 03:09:05573 DISALLOW_COPY_AND_ASSIGN(MessageLoop);
initial.commitd7cae122008-07-26 21:49:38574};
575
[email protected]4d9bdfaf2008-08-26 05:53:57576//-----------------------------------------------------------------------------
577// MessageLoopForUI extends MessageLoop with methods that are particular to a
578// MessageLoop instantiated with TYPE_UI.
579//
580// This class is typically used like so:
581// MessageLoopForUI::current()->...call some method...
582//
[email protected]0bea7252011-08-05 15:34:00583class BASE_EXPORT MessageLoopForUI : public MessageLoop {
[email protected]4d9bdfaf2008-08-26 05:53:57584 public:
585 MessageLoopForUI() : MessageLoop(TYPE_UI) {
586 }
license.botbf09a502008-08-24 00:55:55587
[email protected]4d9bdfaf2008-08-26 05:53:57588 // Returns the MessageLoopForUI of the current thread.
589 static MessageLoopForUI* current() {
590 MessageLoop* loop = MessageLoop::current();
591 DCHECK_EQ(MessageLoop::TYPE_UI, loop->type());
592 return static_cast<MessageLoopForUI*>(loop);
593 }
594
595#if defined(OS_WIN)
[email protected]4d9bdfaf2008-08-26 05:53:57596 void DidProcessMessage(const MSG& message);
[email protected]9cfb89a2010-06-09 21:20:41597#endif // defined(OS_WIN)
[email protected]1a8f5d1d2008-09-25 20:33:04598
[email protected]61c86c62011-08-02 16:11:16599#if defined(OS_ANDROID)
600 // On Android, the UI message loop is handled by Java side. So Run() should
601 // never be called. Instead use Start(), which will forward all the native UI
602 // events to the Java message loop.
603 void Start();
604#elif !defined(OS_MACOSX)
[email protected]148d1052009-07-31 22:53:37605 // Please see message_pump_win/message_pump_glib for definitions of these
606 // methods.
607 void AddObserver(Observer* observer);
608 void RemoveObserver(Observer* observer);
609 void Run(Dispatcher* dispatcher);
610
[email protected]1a8f5d1d2008-09-25 20:33:04611 protected:
612 // TODO(rvargas): Make this platform independent.
[email protected]17b89142008-11-07 21:52:15613 base::MessagePumpForUI* pump_ui() {
[email protected]1a8f5d1d2008-09-25 20:33:04614 return static_cast<base::MessagePumpForUI*>(pump_.get());
615 }
[email protected]9cfb89a2010-06-09 21:20:41616#endif // !defined(OS_MACOSX)
[email protected]4d9bdfaf2008-08-26 05:53:57617};
618
619// Do not add any member variables to MessageLoopForUI! This is important b/c
620// MessageLoopForUI is often allocated via MessageLoop(TYPE_UI). Any extra
621// data that you need should be stored on the MessageLoop's pump_ instance.
622COMPILE_ASSERT(sizeof(MessageLoop) == sizeof(MessageLoopForUI),
623 MessageLoopForUI_should_not_have_extra_member_variables);
624
625//-----------------------------------------------------------------------------
626// MessageLoopForIO extends MessageLoop with methods that are particular to a
627// MessageLoop instantiated with TYPE_IO.
628//
629// This class is typically used like so:
630// MessageLoopForIO::current()->...call some method...
631//
[email protected]0bea7252011-08-05 15:34:00632class BASE_EXPORT MessageLoopForIO : public MessageLoop {
[email protected]4d9bdfaf2008-08-26 05:53:57633 public:
[email protected]9cfb89a2010-06-09 21:20:41634#if defined(OS_WIN)
635 typedef base::MessagePumpForIO::IOHandler IOHandler;
636 typedef base::MessagePumpForIO::IOContext IOContext;
637 typedef base::MessagePumpForIO::IOObserver IOObserver;
638#elif defined(OS_POSIX)
639 typedef base::MessagePumpLibevent::Watcher Watcher;
640 typedef base::MessagePumpLibevent::FileDescriptorWatcher
641 FileDescriptorWatcher;
642 typedef base::MessagePumpLibevent::IOObserver IOObserver;
643
644 enum Mode {
645 WATCH_READ = base::MessagePumpLibevent::WATCH_READ,
646 WATCH_WRITE = base::MessagePumpLibevent::WATCH_WRITE,
647 WATCH_READ_WRITE = base::MessagePumpLibevent::WATCH_READ_WRITE
648 };
649
650#endif
651
[email protected]4d9bdfaf2008-08-26 05:53:57652 MessageLoopForIO() : MessageLoop(TYPE_IO) {
653 }
654
655 // Returns the MessageLoopForIO of the current thread.
656 static MessageLoopForIO* current() {
657 MessageLoop* loop = MessageLoop::current();
658 DCHECK_EQ(MessageLoop::TYPE_IO, loop->type());
659 return static_cast<MessageLoopForIO*>(loop);
660 }
661
[email protected]9cfb89a2010-06-09 21:20:41662 void AddIOObserver(IOObserver* io_observer) {
663 pump_io()->AddIOObserver(io_observer);
664 }
[email protected]941281482010-06-09 05:10:48665
[email protected]9cfb89a2010-06-09 21:20:41666 void RemoveIOObserver(IOObserver* io_observer) {
667 pump_io()->RemoveIOObserver(io_observer);
668 }
669
670#if defined(OS_WIN)
[email protected]4d9bdfaf2008-08-26 05:53:57671 // Please see MessagePumpWin for definitions of these methods.
[email protected]32cda29d2008-10-09 23:58:43672 void RegisterIOHandler(HANDLE file_handle, IOHandler* handler);
[email protected]17b89142008-11-07 21:52:15673 bool WaitForIOCompletion(DWORD timeout, IOHandler* filter);
[email protected]36987e92008-09-18 18:46:26674
[email protected]1a8f5d1d2008-09-25 20:33:04675 protected:
676 // TODO(rvargas): Make this platform independent.
677 base::MessagePumpForIO* pump_io() {
678 return static_cast<base::MessagePumpForIO*>(pump_.get());
679 }
680
[email protected]36987e92008-09-18 18:46:26681#elif defined(OS_POSIX)
[email protected]e45e6c02008-12-15 22:02:17682 // Please see MessagePumpLibevent for definition.
683 bool WatchFileDescriptor(int fd,
684 bool persistent,
685 Mode mode,
686 FileDescriptorWatcher *controller,
687 Watcher *delegate);
[email protected]9cfb89a2010-06-09 21:20:41688
689 private:
690 base::MessagePumpLibevent* pump_io() {
691 return static_cast<base::MessagePumpLibevent*>(pump_.get());
692 }
[email protected]1a8f5d1d2008-09-25 20:33:04693#endif // defined(OS_POSIX)
[email protected]4d9bdfaf2008-08-26 05:53:57694};
695
696// Do not add any member variables to MessageLoopForIO! This is important b/c
697// MessageLoopForIO is often allocated via MessageLoop(TYPE_IO). Any extra
698// data that you need should be stored on the MessageLoop's pump_ instance.
699COMPILE_ASSERT(sizeof(MessageLoop) == sizeof(MessageLoopForIO),
700 MessageLoopForIO_should_not_have_extra_member_variables);
701
702#endif // BASE_MESSAGE_LOOP_H_