blob: 9a03d6b5a53d345a64d79214ff4509d11ec0c3bd [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]26fbf802011-03-25 18:48:0312#include "base/base_api.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]faabcf42009-05-18 21:12:3430#include "base/message_pump_glib.h"
[email protected]7b398be32011-02-19 00:32:4431typedef struct _XDisplay Display;
[email protected]faabcf42009-05-18 21:12:3432#endif
[email protected]e43eddf12009-12-29 00:32:5233#endif
[email protected]b2f7ac42010-10-26 18:43:1834#if defined(TOUCH_UI)
35#include "base/message_pump_glib_x_dispatch.h"
36#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]26fbf802011-03-25 18:48:0378class BASE_API 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(TOUCH_UI)
[email protected]a502bbe72011-01-07 18:06:4584 typedef base::MessagePumpGlibXDispatcher Dispatcher;
[email protected]5040dfab2011-05-11 20:50:0085 typedef base::MessagePumpXObserver Observer;
86#elif !defined(OS_MACOSX)
[email protected]a502bbe72011-01-07 18:06:4587 typedef base::MessagePumpForUI::Dispatcher Dispatcher;
[email protected]a502bbe72011-01-07 18:06:4588 typedef base::MessagePumpForUI::Observer Observer;
89#endif
90
91 // A MessageLoop has a particular type, which indicates the set of
92 // asynchronous events it may process in addition to tasks and timers.
[email protected]9cfb89a2010-06-09 21:20:4193 //
[email protected]a502bbe72011-01-07 18:06:4594 // TYPE_DEFAULT
95 // This type of ML only supports tasks and timers.
96 //
97 // TYPE_UI
98 // This type of ML also supports native UI events (e.g., Windows messages).
99 // See also MessageLoopForUI.
100 //
101 // TYPE_IO
102 // This type of ML also supports asynchronous IO. See also
103 // MessageLoopForIO.
104 //
105 enum Type {
106 TYPE_DEFAULT,
107 TYPE_UI,
108 TYPE_IO
[email protected]9cfb89a2010-06-09 21:20:41109 };
110
[email protected]a502bbe72011-01-07 18:06:45111 // Normally, it is not necessary to instantiate a MessageLoop. Instead, it
112 // is typical to make use of the current thread's MessageLoop instance.
113 explicit MessageLoop(Type type = TYPE_DEFAULT);
[email protected]3690ebe02011-05-25 09:08:19114 virtual ~MessageLoop();
[email protected]a502bbe72011-01-07 18:06:45115
[email protected]9989c9bb2011-01-07 20:23:43116 // Returns the MessageLoop object for the current thread, or null if none.
117 static MessageLoop* current();
118
initial.commitd7cae122008-07-26 21:49:38119 static void EnableHistogrammer(bool enable_histogrammer);
120
[email protected]ee132ad2008-08-06 21:27:02121 // A DestructionObserver is notified when the current MessageLoop is being
122 // destroyed. These obsevers are notified prior to MessageLoop::current()
123 // being changed to return NULL. This gives interested parties the chance to
124 // do final cleanup that depends on the MessageLoop.
125 //
126 // NOTE: Any tasks posted to the MessageLoop during this notification will
127 // not be run. Instead, they will be deleted.
128 //
[email protected]26fbf802011-03-25 18:48:03129 class BASE_API DestructionObserver {
[email protected]ee132ad2008-08-06 21:27:02130 public:
[email protected]ee132ad2008-08-06 21:27:02131 virtual void WillDestroyCurrentMessageLoop() = 0;
[email protected]23c386b2010-09-15 22:14:36132
133 protected:
134 virtual ~DestructionObserver();
[email protected]ee132ad2008-08-06 21:27:02135 };
136
137 // Add a DestructionObserver, which will start receiving notifications
138 // immediately.
139 void AddDestructionObserver(DestructionObserver* destruction_observer);
140
141 // Remove a DestructionObserver. It is safe to call this method while a
142 // DestructionObserver is receiving a notification callback.
143 void RemoveDestructionObserver(DestructionObserver* destruction_observer);
144
[email protected]752578562008-09-07 08:08:29145 // The "PostTask" family of methods call the task's Run method asynchronously
146 // from within a message loop at some point in the future.
initial.commitd7cae122008-07-26 21:49:38147 //
[email protected]752578562008-09-07 08:08:29148 // With the PostTask variant, tasks are invoked in FIFO order, inter-mixed
149 // with normal UI or IO event processing. With the PostDelayedTask variant,
150 // tasks are called after at least approximately 'delay_ms' have elapsed.
initial.commitd7cae122008-07-26 21:49:38151 //
[email protected]752578562008-09-07 08:08:29152 // The NonNestable variants work similarly except that they promise never to
153 // dispatch the task from a nested invocation of MessageLoop::Run. Instead,
154 // such tasks get deferred until the top-most MessageLoop::Run is executing.
155 //
156 // The MessageLoop takes ownership of the Task, and deletes it after it has
157 // been Run().
158 //
159 // NOTE: These methods may be called on any thread. The Task will be invoked
initial.commitd7cae122008-07-26 21:49:38160 // on the thread that executes MessageLoop::Run().
[email protected]b0992172008-10-27 18:54:18161
[email protected]752578562008-09-07 08:08:29162 void PostTask(
163 const tracked_objects::Location& from_here, Task* task);
[email protected]b0992172008-10-27 18:54:18164
[email protected]752578562008-09-07 08:08:29165 void PostDelayedTask(
[email protected]743ace42009-06-17 17:23:51166 const tracked_objects::Location& from_here, Task* task, int64 delay_ms);
initial.commitd7cae122008-07-26 21:49:38167
[email protected]752578562008-09-07 08:08:29168 void PostNonNestableTask(
169 const tracked_objects::Location& from_here, Task* task);
initial.commitd7cae122008-07-26 21:49:38170
[email protected]752578562008-09-07 08:08:29171 void PostNonNestableDelayedTask(
[email protected]743ace42009-06-17 17:23:51172 const tracked_objects::Location& from_here, Task* task, int64 delay_ms);
initial.commitd7cae122008-07-26 21:49:38173
[email protected]b224f792011-04-20 16:02:23174 // TODO(ajwong): Remove the functions above once the Task -> Closure migration
175 // is complete.
176 //
177 // There are 2 sets of Post*Task functions, one which takes the older Task*
178 // function object representation, and one that takes the newer base::Closure.
179 // We have this overload to allow a staged transition between the two systems.
180 // Once the transition is done, the functions above should be deleted.
181 void PostTask(
182 const tracked_objects::Location& from_here,
183 const base::Closure& task);
184
185 void PostDelayedTask(
186 const tracked_objects::Location& from_here,
187 const base::Closure& task, int64 delay_ms);
188
189 void PostNonNestableTask(
190 const tracked_objects::Location& from_here,
191 const base::Closure& task);
192
193 void PostNonNestableDelayedTask(
194 const tracked_objects::Location& from_here,
195 const base::Closure& task, int64 delay_ms);
196
initial.commitd7cae122008-07-26 21:49:38197 // A variant on PostTask that deletes the given object. This is useful
198 // if the object needs to live until the next run of the MessageLoop (for
199 // example, deleting a RenderProcessHost from within an IPC callback is not
200 // good).
201 //
202 // NOTE: This method may be called on any thread. The object will be deleted
203 // on the thread that executes MessageLoop::Run(). If this is not the same
204 // as the thread that calls PostDelayedTask(FROM_HERE, ), then T MUST inherit
205 // from RefCountedThreadSafe<T>!
206 template <class T>
[email protected]00ed48f2010-10-22 22:19:24207 void DeleteSoon(const tracked_objects::Location& from_here, const T* object) {
[email protected]752578562008-09-07 08:08:29208 PostNonNestableTask(from_here, new DeleteTask<T>(object));
initial.commitd7cae122008-07-26 21:49:38209 }
210
211 // A variant on PostTask that releases the given reference counted object
212 // (by calling its Release method). This is useful if the object needs to
213 // live until the next run of the MessageLoop, or if the object needs to be
214 // released on a particular thread.
215 //
216 // NOTE: This method may be called on any thread. The object will be
217 // released (and thus possibly deleted) on the thread that executes
218 // MessageLoop::Run(). If this is not the same as the thread that calls
219 // PostDelayedTask(FROM_HERE, ), then T MUST inherit from
220 // RefCountedThreadSafe<T>!
221 template <class T>
[email protected]00ed48f2010-10-22 22:19:24222 void ReleaseSoon(const tracked_objects::Location& from_here,
223 const T* object) {
[email protected]752578562008-09-07 08:08:29224 PostNonNestableTask(from_here, new ReleaseTask<T>(object));
initial.commitd7cae122008-07-26 21:49:38225 }
226
[email protected]3882c4332008-07-30 19:03:59227 // Run the message loop.
initial.commitd7cae122008-07-26 21:49:38228 void Run();
229
[email protected]7e0e8762008-07-31 13:10:20230 // Process all pending tasks, windows messages, etc., but don't wait/sleep.
231 // Return as soon as all items that can be run are taken care of.
232 void RunAllPending();
[email protected]3882c4332008-07-30 19:03:59233
initial.commitd7cae122008-07-26 21:49:38234 // Signals the Run method to return after it is done processing all pending
[email protected]fc7fb6e2008-08-16 03:09:05235 // messages. This method may only be called on the same thread that called
236 // Run, and Run must still be on the call stack.
initial.commitd7cae122008-07-26 21:49:38237 //
[email protected]fc7fb6e2008-08-16 03:09:05238 // Use QuitTask if you need to Quit another thread's MessageLoop, but note
239 // that doing so is fairly dangerous if the target thread makes nested calls
240 // to MessageLoop::Run. The problem being that you won't know which nested
241 // run loop you are quiting, so be careful!
242 //
initial.commitd7cae122008-07-26 21:49:38243 void Quit();
244
[email protected]781a7ed2010-02-23 07:12:22245 // This method is a variant of Quit, that does not wait for pending messages
246 // to be processed before returning from Run.
247 void QuitNow();
248
[email protected]fc7fb6e2008-08-16 03:09:05249 // Invokes Quit on the current MessageLoop when run. Useful to schedule an
initial.commitd7cae122008-07-26 21:49:38250 // arbitrary MessageLoop to Quit.
251 class QuitTask : public Task {
252 public:
253 virtual void Run() {
254 MessageLoop::current()->Quit();
255 }
256 };
257
[email protected]4d9bdfaf2008-08-26 05:53:57258 // Returns the type passed to the constructor.
259 Type type() const { return type_; }
260
initial.commitd7cae122008-07-26 21:49:38261 // Optional call to connect the thread name with this loop.
[email protected]fc7fb6e2008-08-16 03:09:05262 void set_thread_name(const std::string& thread_name) {
263 DCHECK(thread_name_.empty()) << "Should not rename this thread!";
264 thread_name_ = thread_name;
265 }
[email protected]ee132ad2008-08-06 21:27:02266 const std::string& thread_name() const { return thread_name_; }
initial.commitd7cae122008-07-26 21:49:38267
initial.commitd7cae122008-07-26 21:49:38268 // Enables or disables the recursive task processing. This happens in the case
269 // of recursive message loops. Some unwanted message loop may occurs when
270 // using common controls or printer functions. By default, recursive task
271 // processing is disabled.
272 //
273 // The specific case where tasks get queued is:
274 // - The thread is running a message loop.
275 // - It receives a task #1 and execute it.
276 // - The task #1 implicitly start a message loop, like a MessageBox in the
277 // unit test. This can also be StartDoc or GetSaveFileName.
278 // - The thread receives a task #2 before or while in this second message
279 // loop.
280 // - With NestableTasksAllowed set to true, the task #2 will run right away.
281 // Otherwise, it will get executed right after task #1 completes at "thread
282 // message loop level".
283 void SetNestableTasksAllowed(bool allowed);
284 bool NestableTasksAllowed() const;
285
[email protected]18d6a8f2009-12-16 22:56:33286 // Enables nestable tasks on |loop| while in scope.
287 class ScopedNestableTaskAllower {
288 public:
289 explicit ScopedNestableTaskAllower(MessageLoop* loop)
290 : loop_(loop),
291 old_state_(loop_->NestableTasksAllowed()) {
292 loop_->SetNestableTasksAllowed(true);
293 }
294 ~ScopedNestableTaskAllower() {
295 loop_->SetNestableTasksAllowed(old_state_);
296 }
297
298 private:
299 MessageLoop* loop_;
300 bool old_state_;
301 };
302
initial.commitd7cae122008-07-26 21:49:38303 // Enables or disables the restoration during an exception of the unhandled
304 // exception filter that was active when Run() was called. This can happen
305 // if some third party code call SetUnhandledExceptionFilter() and never
306 // restores the previous filter.
307 void set_exception_restoration(bool restore) {
308 exception_restoration_ = restore;
309 }
310
[email protected]b5f95102009-07-01 19:53:59311 // Returns true if we are currently running a nested message loop.
312 bool IsNested();
313
[email protected]a502bbe72011-01-07 18:06:45314 // A TaskObserver is an object that receives task notifications from the
315 // MessageLoop.
316 //
317 // NOTE: A TaskObserver implementation should be extremely fast!
[email protected]26fbf802011-03-25 18:48:03318 class BASE_API TaskObserver {
[email protected]a502bbe72011-01-07 18:06:45319 public:
320 TaskObserver();
321
322 // This method is called before processing a task.
[email protected]b224f792011-04-20 16:02:23323 virtual void WillProcessTask(base::TimeTicks time_posted) = 0;
[email protected]a502bbe72011-01-07 18:06:45324
325 // This method is called after processing a task.
[email protected]b224f792011-04-20 16:02:23326 virtual void DidProcessTask(base::TimeTicks time_posted) = 0;
[email protected]a502bbe72011-01-07 18:06:45327
328 protected:
329 virtual ~TaskObserver();
330 };
331
[email protected]9cfb89a2010-06-09 21:20:41332 // These functions can only be called on the same thread that |this| is
333 // running on.
334 void AddTaskObserver(TaskObserver* task_observer);
335 void RemoveTaskObserver(TaskObserver* task_observer);
336
[email protected]57f030a2010-06-29 04:58:15337 // Returns true if the message loop has high resolution timers enabled.
338 // Provided for testing.
339 bool high_resolution_timers_enabled() {
340#if defined(OS_WIN)
341 return !high_resolution_timer_expiration_.is_null();
342#else
343 return true;
344#endif
345 }
346
347 // When we go into high resolution timer mode, we will stay in hi-res mode
348 // for at least 1s.
349 static const int kHighResolutionTimerModeLeaseTimeMs = 1000;
350
[email protected]8d6ab8f52011-01-26 00:53:48351 // Asserts that the MessageLoop is "idle".
352 void AssertIdle() const;
353
[email protected]2ec01fe2011-03-24 03:40:28354#if defined(OS_WIN)
355 void set_os_modal_loop(bool os_modal_loop) {
356 os_modal_loop_ = os_modal_loop;
357 }
358
359 bool os_modal_loop() const {
360 return os_modal_loop_;
361 }
362#endif // OS_WIN
363
initial.commitd7cae122008-07-26 21:49:38364 //----------------------------------------------------------------------------
[email protected]4d9bdfaf2008-08-26 05:53:57365 protected:
[email protected]fc7fb6e2008-08-16 03:09:05366 struct RunState {
367 // Used to count how many Run() invocations are on the stack.
368 int run_depth;
initial.commitd7cae122008-07-26 21:49:38369
[email protected]fc7fb6e2008-08-16 03:09:05370 // Used to record that Quit() was called, or that we should quit the pump
371 // once it becomes idle.
372 bool quit_received;
initial.commitd7cae122008-07-26 21:49:38373
[email protected]e43eddf12009-12-29 00:32:52374#if !defined(OS_MACOSX)
[email protected]148d1052009-07-31 22:53:37375 Dispatcher* dispatcher;
[email protected]fc7fb6e2008-08-16 03:09:05376#endif
377 };
378
[email protected]23bb71f2011-04-21 22:22:10379 class BASE_API AutoRunState : RunState {
[email protected]fc7fb6e2008-08-16 03:09:05380 public:
[email protected]b0992172008-10-27 18:54:18381 explicit AutoRunState(MessageLoop* loop);
[email protected]fc7fb6e2008-08-16 03:09:05382 ~AutoRunState();
initial.commitd7cae122008-07-26 21:49:38383 private:
384 MessageLoop* loop_;
[email protected]fc7fb6e2008-08-16 03:09:05385 RunState* previous_state_;
386 };
initial.commitd7cae122008-07-26 21:49:38387
[email protected]752578562008-09-07 08:08:29388 // This structure is copied around by value.
389 struct PendingTask {
[email protected]b224f792011-04-20 16:02:23390 PendingTask(const base::Closure& task,
391 const tracked_objects::Location& posted_from,
392 base::TimeTicks delayed_run_time,
393 bool nestable);
394 ~PendingTask();
[email protected]b0992172008-10-27 18:54:18395
[email protected]752578562008-09-07 08:08:29396 // Used to support sorting.
397 bool operator<(const PendingTask& other) const;
[email protected]a502bbe72011-01-07 18:06:45398
[email protected]b224f792011-04-20 16:02:23399 // The task to run.
400 base::Closure task;
401
402#if defined(TRACK_ALL_TASK_OBJECTS)
403 // Counter for location where the Closure was posted from.
404 tracked_objects::Births* post_births;
405#endif // defined(TRACK_ALL_TASK_OBJECTS)
406
407 // Time this PendingTask was posted.
408 base::TimeTicks time_posted;
409
410 // The time when the task should be run.
411 base::TimeTicks delayed_run_time;
412
413 // Secondary sort key for run time.
414 int sequence_num;
415
416 // OK to dispatch from a nested loop.
417 bool nestable;
[email protected]fcb30f7b2011-05-19 22:28:25418
419 // The site this PendingTask was posted from.
420 const void* birth_program_counter;
initial.commitd7cae122008-07-26 21:49:38421 };
422
[email protected]b2f0ea12009-09-02 20:05:21423 class TaskQueue : public std::queue<PendingTask> {
424 public:
425 void Swap(TaskQueue* queue) {
426 c.swap(queue->c); // Calls std::deque::swap
427 }
428 };
429
[email protected]4bed5d82008-12-17 03:50:05430 typedef std::priority_queue<PendingTask> DelayedTaskQueue;
initial.commitd7cae122008-07-26 21:49:38431
[email protected]fc7fb6e2008-08-16 03:09:05432#if defined(OS_WIN)
433 base::MessagePumpWin* pump_win() {
434 return static_cast<base::MessagePumpWin*>(pump_.get());
435 }
[email protected]36987e92008-09-18 18:46:26436#elif defined(OS_POSIX)
437 base::MessagePumpLibevent* pump_libevent() {
438 return static_cast<base::MessagePumpLibevent*>(pump_.get());
439 }
[email protected]fc7fb6e2008-08-16 03:09:05440#endif
[email protected]3882c4332008-07-30 19:03:59441
442 // A function to encapsulate all the exception handling capability in the
[email protected]fc7fb6e2008-08-16 03:09:05443 // stacks around the running of a main message loop. It will run the message
444 // loop in a SEH try block or not depending on the set_SEH_restoration()
[email protected]aff8a132009-10-26 18:15:59445 // flag invoking respectively RunInternalInSEHFrame() or RunInternal().
[email protected]fc7fb6e2008-08-16 03:09:05446 void RunHandler();
initial.commitd7cae122008-07-26 21:49:38447
[email protected]aff8a132009-10-26 18:15:59448#if defined(OS_WIN)
449 __declspec(noinline) void RunInternalInSEHFrame();
450#endif
451
[email protected]3882c4332008-07-30 19:03:59452 // A surrounding stack frame around the running of the message loop that
453 // supports all saving and restoring of state, as is needed for any/all (ugly)
454 // recursive calls.
[email protected]fc7fb6e2008-08-16 03:09:05455 void RunInternal();
[email protected]ea15e982008-08-15 07:31:20456
[email protected]fc7fb6e2008-08-16 03:09:05457 // Called to process any delayed non-nestable tasks.
initial.commitd7cae122008-07-26 21:49:38458 bool ProcessNextDelayedNonNestableTask();
initial.commitd7cae122008-07-26 21:49:38459
[email protected]b224f792011-04-20 16:02:23460 // Runs the specified PendingTask.
461 void RunTask(const PendingTask& pending_task);
initial.commitd7cae122008-07-26 21:49:38462
[email protected]752578562008-09-07 08:08:29463 // Calls RunTask or queues the pending_task on the deferred task list if it
464 // cannot be run right now. Returns true if the task was run.
465 bool DeferOrRunPendingTask(const PendingTask& pending_task);
initial.commitd7cae122008-07-26 21:49:38466
[email protected]001747c2008-09-10 00:37:07467 // Adds the pending task to delayed_work_queue_.
468 void AddToDelayedWorkQueue(const PendingTask& pending_task);
469
[email protected]b224f792011-04-20 16:02:23470 // Adds the pending task to our incoming_queue_.
471 //
472 // Caller retains ownership of |pending_task|, but this function will
473 // reset the value of pending_task->task. This is needed to ensure
474 // that the posting call stack does not retain pending_task->task
475 // beyond this function call.
476 void AddToIncomingQueue(PendingTask* pending_task);
477
initial.commitd7cae122008-07-26 21:49:38478 // Load tasks from the incoming_queue_ into work_queue_ if the latter is
479 // empty. The former requires a lock to access, while the latter is directly
480 // accessible on this thread.
481 void ReloadWorkQueue();
482
483 // Delete tasks that haven't run yet without running them. Used in the
[email protected]001747c2008-09-10 00:37:07484 // destructor to make sure all the task's destructors get called. Returns
485 // true if some work was done.
486 bool DeletePendingTasks();
initial.commitd7cae122008-07-26 21:49:38487
[email protected]b224f792011-04-20 16:02:23488 // Calcuates the time at which a PendingTask should run.
489 base::TimeTicks CalculateDelayedRuntime(int64 delay_ms);
[email protected]fc7fb6e2008-08-16 03:09:05490
initial.commitd7cae122008-07-26 21:49:38491 // Start recording histogram info about events and action IF it was enabled
492 // and IF the statistics recorder can accept a registration of our histogram.
493 void StartHistogrammer();
494
495 // Add occurence of event to our histogram, so that we can see what is being
496 // done in a specific MessageLoop instance (i.e., specific thread).
497 // If message_histogram_ is NULL, this is a no-op.
498 void HistogramEvent(int event);
499
[email protected]a502bbe72011-01-07 18:06:45500 // base::MessagePump::Delegate methods:
501 virtual bool DoWork();
502 virtual bool DoDelayedWork(base::TimeTicks* next_delayed_work_time);
503 virtual bool DoIdleWork();
504
[email protected]4d9bdfaf2008-08-26 05:53:57505 Type type_;
506
[email protected]752578562008-09-07 08:08:29507 // A list of tasks that need to be processed by this instance. Note that
508 // this queue is only accessed (push/pop) by our current thread.
509 TaskQueue work_queue_;
[email protected]b0992172008-10-27 18:54:18510
[email protected]752578562008-09-07 08:08:29511 // Contains delayed tasks, sorted by their 'delayed_run_time' property.
512 DelayedTaskQueue delayed_work_queue_;
initial.commitd7cae122008-07-26 21:49:38513
[email protected]a8f7d3d2010-11-04 23:23:42514 // A recent snapshot of Time::Now(), used to check delayed_work_queue_.
[email protected]7e7fab42010-11-06 22:23:29515 base::TimeTicks recent_time_;
[email protected]a8f7d3d2010-11-04 23:23:42516
[email protected]752578562008-09-07 08:08:29517 // A queue of non-nestable tasks that we had to defer because when it came
518 // time to execute them we were in a nested message loop. They will execute
519 // once we're out of nested message loops.
520 TaskQueue deferred_non_nestable_work_queue_;
initial.commitd7cae122008-07-26 21:49:38521
[email protected]fc7fb6e2008-08-16 03:09:05522 scoped_refptr<base::MessagePump> pump_;
[email protected]ee132ad2008-08-06 21:27:02523
[email protected]2a127252008-08-05 23:16:41524 ObserverList<DestructionObserver> destruction_observers_;
[email protected]001747c2008-09-10 00:37:07525
initial.commitd7cae122008-07-26 21:49:38526 // A recursion block that prevents accidentally running additonal tasks when
527 // insider a (accidentally induced?) nested message pump.
528 bool nestable_tasks_allowed_;
529
530 bool exception_restoration_;
531
initial.commitd7cae122008-07-26 21:49:38532 std::string thread_name_;
533 // A profiling histogram showing the counts of various messages and events.
[email protected]81ce9f3b2011-04-05 04:48:53534 base::Histogram* message_histogram_;
initial.commitd7cae122008-07-26 21:49:38535
536 // A null terminated list which creates an incoming_queue of tasks that are
[email protected]242c4bd2011-02-25 18:43:23537 // acquired under a mutex for processing on this instance's thread. These
[email protected]b224f792011-04-20 16:02:23538 // tasks have not yet been sorted out into items for our work_queue_ vs items
539 // that will be handled by the TimerManager.
initial.commitd7cae122008-07-26 21:49:38540 TaskQueue incoming_queue_;
541 // Protect access to incoming_queue_.
[email protected]8d6ab8f52011-01-26 00:53:48542 mutable base::Lock incoming_queue_lock_;
initial.commitd7cae122008-07-26 21:49:38543
[email protected]fc7fb6e2008-08-16 03:09:05544 RunState* state_;
initial.commitd7cae122008-07-26 21:49:38545
[email protected]b224f792011-04-20 16:02:23546 // The need for this variable is subtle. Please see implementation comments
547 // around where it is used.
548 bool should_leak_tasks_;
549
[email protected]57f030a2010-06-29 04:58:15550#if defined(OS_WIN)
551 base::TimeTicks high_resolution_timer_expiration_;
[email protected]2ec01fe2011-03-24 03:40:28552 // Should be set to true before calling Windows APIs like TrackPopupMenu, etc
553 // which enter a modal message loop.
554 bool os_modal_loop_;
[email protected]57f030a2010-06-29 04:58:15555#endif
556
[email protected]752578562008-09-07 08:08:29557 // The next sequence number to use for delayed tasks.
558 int next_sequence_num_;
559
[email protected]9cfb89a2010-06-09 21:20:41560 ObserverList<TaskObserver> task_observers_;
561
[email protected]26fbf802011-03-25 18:48:03562 private:
[email protected]fc7fb6e2008-08-16 03:09:05563 DISALLOW_COPY_AND_ASSIGN(MessageLoop);
initial.commitd7cae122008-07-26 21:49:38564};
565
[email protected]4d9bdfaf2008-08-26 05:53:57566//-----------------------------------------------------------------------------
567// MessageLoopForUI extends MessageLoop with methods that are particular to a
568// MessageLoop instantiated with TYPE_UI.
569//
570// This class is typically used like so:
571// MessageLoopForUI::current()->...call some method...
572//
[email protected]26fbf802011-03-25 18:48:03573class BASE_API MessageLoopForUI : public MessageLoop {
[email protected]4d9bdfaf2008-08-26 05:53:57574 public:
575 MessageLoopForUI() : MessageLoop(TYPE_UI) {
576 }
license.botbf09a502008-08-24 00:55:55577
[email protected]4d9bdfaf2008-08-26 05:53:57578 // Returns the MessageLoopForUI of the current thread.
579 static MessageLoopForUI* current() {
580 MessageLoop* loop = MessageLoop::current();
581 DCHECK_EQ(MessageLoop::TYPE_UI, loop->type());
582 return static_cast<MessageLoopForUI*>(loop);
583 }
584
585#if defined(OS_WIN)
[email protected]4d9bdfaf2008-08-26 05:53:57586 void DidProcessMessage(const MSG& message);
[email protected]9cfb89a2010-06-09 21:20:41587#endif // defined(OS_WIN)
[email protected]1a8f5d1d2008-09-25 20:33:04588
[email protected]242c4bd2011-02-25 18:43:23589#if defined(USE_X11)
590 // Returns the Xlib Display that backs the MessagePump for this MessageLoop.
591 //
592 // This allows for raw access to the X11 server in situations where our
593 // abstractions do not provide enough power.
594 //
595 // Be careful how this is used. The MessagePump in general expects
596 // exclusive access to the Display. Calling things like XNextEvent() will
597 // likely break things in subtle, hard to detect, ways.
598 Display* GetDisplay();
599#endif // defined(OS_X11)
[email protected]7b398be32011-02-19 00:32:44600
[email protected]e43eddf12009-12-29 00:32:52601#if !defined(OS_MACOSX)
[email protected]148d1052009-07-31 22:53:37602 // Please see message_pump_win/message_pump_glib for definitions of these
603 // methods.
604 void AddObserver(Observer* observer);
605 void RemoveObserver(Observer* observer);
606 void Run(Dispatcher* dispatcher);
607
[email protected]1a8f5d1d2008-09-25 20:33:04608 protected:
609 // TODO(rvargas): Make this platform independent.
[email protected]17b89142008-11-07 21:52:15610 base::MessagePumpForUI* pump_ui() {
[email protected]1a8f5d1d2008-09-25 20:33:04611 return static_cast<base::MessagePumpForUI*>(pump_.get());
612 }
[email protected]9cfb89a2010-06-09 21:20:41613#endif // !defined(OS_MACOSX)
[email protected]4d9bdfaf2008-08-26 05:53:57614};
615
616// Do not add any member variables to MessageLoopForUI! This is important b/c
617// MessageLoopForUI is often allocated via MessageLoop(TYPE_UI). Any extra
618// data that you need should be stored on the MessageLoop's pump_ instance.
619COMPILE_ASSERT(sizeof(MessageLoop) == sizeof(MessageLoopForUI),
620 MessageLoopForUI_should_not_have_extra_member_variables);
621
622//-----------------------------------------------------------------------------
623// MessageLoopForIO extends MessageLoop with methods that are particular to a
624// MessageLoop instantiated with TYPE_IO.
625//
626// This class is typically used like so:
627// MessageLoopForIO::current()->...call some method...
628//
[email protected]26fbf802011-03-25 18:48:03629class BASE_API MessageLoopForIO : public MessageLoop {
[email protected]4d9bdfaf2008-08-26 05:53:57630 public:
[email protected]9cfb89a2010-06-09 21:20:41631#if defined(OS_WIN)
632 typedef base::MessagePumpForIO::IOHandler IOHandler;
633 typedef base::MessagePumpForIO::IOContext IOContext;
634 typedef base::MessagePumpForIO::IOObserver IOObserver;
635#elif defined(OS_POSIX)
636 typedef base::MessagePumpLibevent::Watcher Watcher;
637 typedef base::MessagePumpLibevent::FileDescriptorWatcher
638 FileDescriptorWatcher;
639 typedef base::MessagePumpLibevent::IOObserver IOObserver;
640
641 enum Mode {
642 WATCH_READ = base::MessagePumpLibevent::WATCH_READ,
643 WATCH_WRITE = base::MessagePumpLibevent::WATCH_WRITE,
644 WATCH_READ_WRITE = base::MessagePumpLibevent::WATCH_READ_WRITE
645 };
646
647#endif
648
[email protected]4d9bdfaf2008-08-26 05:53:57649 MessageLoopForIO() : MessageLoop(TYPE_IO) {
650 }
651
652 // Returns the MessageLoopForIO of the current thread.
653 static MessageLoopForIO* current() {
654 MessageLoop* loop = MessageLoop::current();
655 DCHECK_EQ(MessageLoop::TYPE_IO, loop->type());
656 return static_cast<MessageLoopForIO*>(loop);
657 }
658
[email protected]9cfb89a2010-06-09 21:20:41659 void AddIOObserver(IOObserver* io_observer) {
660 pump_io()->AddIOObserver(io_observer);
661 }
[email protected]941281482010-06-09 05:10:48662
[email protected]9cfb89a2010-06-09 21:20:41663 void RemoveIOObserver(IOObserver* io_observer) {
664 pump_io()->RemoveIOObserver(io_observer);
665 }
666
667#if defined(OS_WIN)
[email protected]4d9bdfaf2008-08-26 05:53:57668 // Please see MessagePumpWin for definitions of these methods.
[email protected]32cda29d2008-10-09 23:58:43669 void RegisterIOHandler(HANDLE file_handle, IOHandler* handler);
[email protected]17b89142008-11-07 21:52:15670 bool WaitForIOCompletion(DWORD timeout, IOHandler* filter);
[email protected]36987e92008-09-18 18:46:26671
[email protected]1a8f5d1d2008-09-25 20:33:04672 protected:
673 // TODO(rvargas): Make this platform independent.
674 base::MessagePumpForIO* pump_io() {
675 return static_cast<base::MessagePumpForIO*>(pump_.get());
676 }
677
[email protected]36987e92008-09-18 18:46:26678#elif defined(OS_POSIX)
[email protected]e45e6c02008-12-15 22:02:17679 // Please see MessagePumpLibevent for definition.
680 bool WatchFileDescriptor(int fd,
681 bool persistent,
682 Mode mode,
683 FileDescriptorWatcher *controller,
684 Watcher *delegate);
[email protected]9cfb89a2010-06-09 21:20:41685
686 private:
687 base::MessagePumpLibevent* pump_io() {
688 return static_cast<base::MessagePumpLibevent*>(pump_.get());
689 }
[email protected]1a8f5d1d2008-09-25 20:33:04690#endif // defined(OS_POSIX)
[email protected]4d9bdfaf2008-08-26 05:53:57691};
692
693// Do not add any member variables to MessageLoopForIO! This is important b/c
694// MessageLoopForIO is often allocated via MessageLoop(TYPE_IO). Any extra
695// data that you need should be stored on the MessageLoop's pump_ instance.
696COMPILE_ASSERT(sizeof(MessageLoop) == sizeof(MessageLoopForIO),
697 MessageLoopForIO_should_not_have_extra_member_variables);
698
699#endif // BASE_MESSAGE_LOOP_H_