blob: 95fae1be50116253f5b4b6a1dd2ee112b223644b [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]c62dd9d2011-09-21 18:05:4115#include "base/location.h"
[email protected]3b63f8f42011-03-28 01:54:1516#include "base/memory/ref_counted.h"
[email protected]edd685f2011-08-15 20:33:4617#include "base/message_loop_proxy.h"
[email protected]fc7fb6e2008-08-16 03:09:0518#include "base/message_pump.h"
initial.commitd7cae122008-07-26 21:49:3819#include "base/observer_list.h"
[email protected]dd1f9fe2011-11-15 23:36:3020#include "base/pending_task.h"
[email protected]20305ec2011-01-21 04:55:5221#include "base/synchronization/lock.h"
initial.commitd7cae122008-07-26 21:49:3822#include "base/task.h"
[email protected]b2a9bbd2011-10-31 22:36:2123#include "base/tracking_info.h"
[email protected]b224f792011-04-20 16:02:2324#include "base/time.h"
initial.commitd7cae122008-07-26 21:49:3825
[email protected]fc7fb6e2008-08-16 03:09:0526#if defined(OS_WIN)
27// We need this to declare base::MessagePumpWin::Dispatcher, which we should
28// really just eliminate.
29#include "base/message_pump_win.h"
[email protected]36987e92008-09-18 18:46:2630#elif defined(OS_POSIX)
31#include "base/message_pump_libevent.h"
[email protected]4d0f93c2011-09-29 04:43:5432#if !defined(OS_MACOSX) && !defined(OS_ANDROID)
[email protected]4076d2f2011-08-11 18:20:2233
34#if defined(USE_WAYLAND)
35#include "base/message_pump_wayland.h"
[email protected]4a6bef32011-09-07 02:06:0136#elif defined(TOUCH_UI) || defined(USE_AURA)
[email protected]2047ef42011-06-24 20:10:2537#include "base/message_pump_x.h"
38#else
39#include "base/message_pump_gtk.h"
40#endif
[email protected]4076d2f2011-08-11 18:20:2241
[email protected]faabcf42009-05-18 21:12:3442#endif
[email protected]e43eddf12009-12-29 00:32:5243#endif
[email protected]ea15e982008-08-15 07:31:2044
[email protected]835d7c82010-10-14 04:38:3845namespace base {
[email protected]5097dc82010-07-15 17:23:2346class Histogram;
[email protected]835d7c82010-10-14 04:38:3847}
[email protected]5097dc82010-07-15 17:23:2348
[email protected]fc7fb6e2008-08-16 03:09:0549// A MessageLoop is used to process events for a particular thread. There is
50// at most one MessageLoop instance per thread.
51//
52// Events include at a minimum Task instances submitted to PostTask or those
53// managed by TimerManager. Depending on the type of message pump used by the
54// MessageLoop other events such as UI messages may be processed. On Windows
55// APC calls (as time permits) and signals sent to a registered set of HANDLEs
56// may also be processed.
initial.commitd7cae122008-07-26 21:49:3857//
58// NOTE: Unless otherwise specified, a MessageLoop's methods may only be called
59// on the thread where the MessageLoop's Run method executes.
60//
[email protected]fc7fb6e2008-08-16 03:09:0561// NOTE: MessageLoop has task reentrancy protection. This means that if a
initial.commitd7cae122008-07-26 21:49:3862// task is being processed, a second task cannot start until the first task is
[email protected]fc7fb6e2008-08-16 03:09:0563// finished. Reentrancy can happen when processing a task, and an inner
64// message pump is created. That inner pump then processes native messages
65// which could implicitly start an inner task. Inner message pumps are created
66// with dialogs (DialogBox), common dialogs (GetOpenFileName), OLE functions
67// (DoDragDrop), printer functions (StartDoc) and *many* others.
68//
initial.commitd7cae122008-07-26 21:49:3869// Sample workaround when inner task processing is needed:
70// bool old_state = MessageLoop::current()->NestableTasksAllowed();
71// MessageLoop::current()->SetNestableTasksAllowed(true);
72// HRESULT hr = DoDragDrop(...); // Implicitly runs a modal message loop here.
73// MessageLoop::current()->SetNestableTasksAllowed(old_state);
74// // Process hr (the result returned by DoDragDrop().
75//
[email protected]fc7fb6e2008-08-16 03:09:0576// Please be SURE your task is reentrant (nestable) and all global variables
77// are stable and accessible before calling SetNestableTasksAllowed(true).
initial.commitd7cae122008-07-26 21:49:3878//
[email protected]0bea7252011-08-05 15:34:0079class BASE_EXPORT MessageLoop : public base::MessagePump::Delegate {
initial.commitd7cae122008-07-26 21:49:3880 public:
[email protected]a502bbe72011-01-07 18:06:4581#if defined(OS_WIN)
82 typedef base::MessagePumpWin::Dispatcher Dispatcher;
[email protected]2e5597c2011-10-04 00:10:4783 typedef base::MessagePumpObserver Observer;
[email protected]4d0f93c2011-09-29 04:43:5484#elif !defined(OS_MACOSX) && !defined(OS_ANDROID)
[email protected]2047ef42011-06-24 20:10:2585 typedef base::MessagePumpDispatcher Dispatcher;
86 typedef base::MessagePumpObserver Observer;
[email protected]a502bbe72011-01-07 18:06:4587#endif
88
89 // A MessageLoop has a particular type, which indicates the set of
90 // asynchronous events it may process in addition to tasks and timers.
[email protected]9cfb89a2010-06-09 21:20:4191 //
[email protected]a502bbe72011-01-07 18:06:4592 // TYPE_DEFAULT
93 // This type of ML only supports tasks and timers.
94 //
95 // TYPE_UI
96 // This type of ML also supports native UI events (e.g., Windows messages).
97 // See also MessageLoopForUI.
98 //
99 // TYPE_IO
100 // This type of ML also supports asynchronous IO. See also
101 // MessageLoopForIO.
102 //
103 enum Type {
104 TYPE_DEFAULT,
105 TYPE_UI,
106 TYPE_IO
[email protected]9cfb89a2010-06-09 21:20:41107 };
108
[email protected]a502bbe72011-01-07 18:06:45109 // Normally, it is not necessary to instantiate a MessageLoop. Instead, it
110 // is typical to make use of the current thread's MessageLoop instance.
111 explicit MessageLoop(Type type = TYPE_DEFAULT);
[email protected]3690ebe02011-05-25 09:08:19112 virtual ~MessageLoop();
[email protected]a502bbe72011-01-07 18:06:45113
[email protected]9989c9bb2011-01-07 20:23:43114 // Returns the MessageLoop object for the current thread, or null if none.
115 static MessageLoop* current();
116
initial.commitd7cae122008-07-26 21:49:38117 static void EnableHistogrammer(bool enable_histogrammer);
118
[email protected]61c86c62011-08-02 16:11:16119 typedef base::MessagePump* (MessagePumpFactory)();
120 // Using the given base::MessagePumpForUIFactory to override the default
121 // MessagePump implementation for 'TYPE_UI'.
122 static void InitMessagePumpForUIFactory(MessagePumpFactory* factory);
123
[email protected]ee132ad2008-08-06 21:27:02124 // A DestructionObserver is notified when the current MessageLoop is being
[email protected]dd1f9fe2011-11-15 23:36:30125 // destroyed. These observers are notified prior to MessageLoop::current()
[email protected]ee132ad2008-08-06 21:27:02126 // being changed to return NULL. This gives interested parties the chance to
127 // do final cleanup that depends on the MessageLoop.
128 //
129 // NOTE: Any tasks posted to the MessageLoop during this notification will
130 // not be run. Instead, they will be deleted.
131 //
[email protected]0bea7252011-08-05 15:34:00132 class BASE_EXPORT DestructionObserver {
[email protected]ee132ad2008-08-06 21:27:02133 public:
[email protected]ee132ad2008-08-06 21:27:02134 virtual void WillDestroyCurrentMessageLoop() = 0;
[email protected]23c386b2010-09-15 22:14:36135
136 protected:
137 virtual ~DestructionObserver();
[email protected]ee132ad2008-08-06 21:27:02138 };
139
140 // Add a DestructionObserver, which will start receiving notifications
141 // immediately.
142 void AddDestructionObserver(DestructionObserver* destruction_observer);
143
144 // Remove a DestructionObserver. It is safe to call this method while a
145 // DestructionObserver is receiving a notification callback.
146 void RemoveDestructionObserver(DestructionObserver* destruction_observer);
147
[email protected]752578562008-09-07 08:08:29148 // The "PostTask" family of methods call the task's Run method asynchronously
149 // from within a message loop at some point in the future.
initial.commitd7cae122008-07-26 21:49:38150 //
[email protected]752578562008-09-07 08:08:29151 // With the PostTask variant, tasks are invoked in FIFO order, inter-mixed
152 // with normal UI or IO event processing. With the PostDelayedTask variant,
153 // tasks are called after at least approximately 'delay_ms' have elapsed.
initial.commitd7cae122008-07-26 21:49:38154 //
[email protected]752578562008-09-07 08:08:29155 // The NonNestable variants work similarly except that they promise never to
156 // dispatch the task from a nested invocation of MessageLoop::Run. Instead,
157 // such tasks get deferred until the top-most MessageLoop::Run is executing.
158 //
159 // The MessageLoop takes ownership of the Task, and deletes it after it has
160 // been Run().
161 //
162 // NOTE: These methods may be called on any thread. The Task will be invoked
initial.commitd7cae122008-07-26 21:49:38163 // on the thread that executes MessageLoop::Run().
[email protected]b0992172008-10-27 18:54:18164
[email protected]752578562008-09-07 08:08:29165 void PostTask(
166 const tracked_objects::Location& from_here, Task* task);
[email protected]b0992172008-10-27 18:54:18167
[email protected]752578562008-09-07 08:08:29168 void PostDelayedTask(
[email protected]743ace42009-06-17 17:23:51169 const tracked_objects::Location& from_here, Task* task, int64 delay_ms);
initial.commitd7cae122008-07-26 21:49:38170
[email protected]752578562008-09-07 08:08:29171 void PostNonNestableTask(
172 const tracked_objects::Location& from_here, Task* task);
initial.commitd7cae122008-07-26 21:49:38173
[email protected]752578562008-09-07 08:08:29174 void PostNonNestableDelayedTask(
[email protected]743ace42009-06-17 17:23:51175 const tracked_objects::Location& from_here, Task* task, int64 delay_ms);
initial.commitd7cae122008-07-26 21:49:38176
[email protected]b224f792011-04-20 16:02:23177 // TODO(ajwong): Remove the functions above once the Task -> Closure migration
178 // is complete.
179 //
180 // There are 2 sets of Post*Task functions, one which takes the older Task*
181 // function object representation, and one that takes the newer base::Closure.
182 // We have this overload to allow a staged transition between the two systems.
183 // Once the transition is done, the functions above should be deleted.
184 void PostTask(
185 const tracked_objects::Location& from_here,
186 const base::Closure& task);
187
188 void PostDelayedTask(
189 const tracked_objects::Location& from_here,
190 const base::Closure& task, int64 delay_ms);
191
192 void PostNonNestableTask(
193 const tracked_objects::Location& from_here,
194 const base::Closure& task);
195
196 void PostNonNestableDelayedTask(
197 const tracked_objects::Location& from_here,
198 const base::Closure& task, int64 delay_ms);
199
initial.commitd7cae122008-07-26 21:49:38200 // A variant on PostTask that deletes the given object. This is useful
201 // if the object needs to live until the next run of the MessageLoop (for
202 // example, deleting a RenderProcessHost from within an IPC callback is not
203 // good).
204 //
205 // NOTE: This method may be called on any thread. The object will be deleted
206 // on the thread that executes MessageLoop::Run(). If this is not the same
207 // as the thread that calls PostDelayedTask(FROM_HERE, ), then T MUST inherit
208 // from RefCountedThreadSafe<T>!
209 template <class T>
[email protected]00ed48f2010-10-22 22:19:24210 void DeleteSoon(const tracked_objects::Location& from_here, const T* object) {
[email protected]752578562008-09-07 08:08:29211 PostNonNestableTask(from_here, new DeleteTask<T>(object));
initial.commitd7cae122008-07-26 21:49:38212 }
213
214 // A variant on PostTask that releases the given reference counted object
215 // (by calling its Release method). This is useful if the object needs to
216 // live until the next run of the MessageLoop, or if the object needs to be
217 // released on a particular thread.
218 //
219 // NOTE: This method may be called on any thread. The object will be
220 // released (and thus possibly deleted) on the thread that executes
221 // MessageLoop::Run(). If this is not the same as the thread that calls
222 // PostDelayedTask(FROM_HERE, ), then T MUST inherit from
223 // RefCountedThreadSafe<T>!
224 template <class T>
[email protected]00ed48f2010-10-22 22:19:24225 void ReleaseSoon(const tracked_objects::Location& from_here,
226 const T* object) {
[email protected]752578562008-09-07 08:08:29227 PostNonNestableTask(from_here, new ReleaseTask<T>(object));
initial.commitd7cae122008-07-26 21:49:38228 }
229
[email protected]3882c4332008-07-30 19:03:59230 // Run the message loop.
initial.commitd7cae122008-07-26 21:49:38231 void Run();
232
[email protected]7e0e8762008-07-31 13:10:20233 // Process all pending tasks, windows messages, etc., but don't wait/sleep.
234 // Return as soon as all items that can be run are taken care of.
235 void RunAllPending();
[email protected]3882c4332008-07-30 19:03:59236
initial.commitd7cae122008-07-26 21:49:38237 // Signals the Run method to return after it is done processing all pending
[email protected]fc7fb6e2008-08-16 03:09:05238 // messages. This method may only be called on the same thread that called
239 // Run, and Run must still be on the call stack.
initial.commitd7cae122008-07-26 21:49:38240 //
[email protected]8c6517e52011-10-17 01:20:36241 // Use QuitTask or QuitClosure if you need to Quit another thread's
242 // MessageLoop, but note that doing so is fairly dangerous if the target
243 // thread makes nested calls to MessageLoop::Run. The problem being that you
244 // won't know which nested run loop you are quitting, so be careful!
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.
[email protected]8c6517e52011-10-17 01:20:36253 // TODO(jhawkins): Remove once task.h is removed.
initial.commitd7cae122008-07-26 21:49:38254 class QuitTask : public Task {
255 public:
[email protected]b1719462011-11-16 00:08:08256 virtual void Run() OVERRIDE {
initial.commitd7cae122008-07-26 21:49:38257 MessageLoop::current()->Quit();
258 }
259 };
260
[email protected]8c6517e52011-10-17 01:20:36261 // Invokes Quit on the current MessageLoop when run. Useful to schedule an
262 // arbitrary MessageLoop to Quit.
263 static base::Closure QuitClosure();
264
[email protected]4d9bdfaf2008-08-26 05:53:57265 // Returns the type passed to the constructor.
266 Type type() const { return type_; }
267
initial.commitd7cae122008-07-26 21:49:38268 // Optional call to connect the thread name with this loop.
[email protected]fc7fb6e2008-08-16 03:09:05269 void set_thread_name(const std::string& thread_name) {
270 DCHECK(thread_name_.empty()) << "Should not rename this thread!";
271 thread_name_ = thread_name;
272 }
[email protected]ee132ad2008-08-06 21:27:02273 const std::string& thread_name() const { return thread_name_; }
initial.commitd7cae122008-07-26 21:49:38274
[email protected]c31af70db22011-08-18 23:13:01275 // Gets the message loop proxy associated with this message loop.
[email protected]edd685f2011-08-15 20:33:46276 scoped_refptr<base::MessageLoopProxy> message_loop_proxy() {
277 return message_loop_proxy_.get();
278 }
279
initial.commitd7cae122008-07-26 21:49:38280 // Enables or disables the recursive task processing. This happens in the case
281 // of recursive message loops. Some unwanted message loop may occurs when
282 // using common controls or printer functions. By default, recursive task
283 // processing is disabled.
284 //
285 // The specific case where tasks get queued is:
286 // - The thread is running a message loop.
287 // - It receives a task #1 and execute it.
288 // - The task #1 implicitly start a message loop, like a MessageBox in the
289 // unit test. This can also be StartDoc or GetSaveFileName.
290 // - The thread receives a task #2 before or while in this second message
291 // loop.
292 // - With NestableTasksAllowed set to true, the task #2 will run right away.
293 // Otherwise, it will get executed right after task #1 completes at "thread
294 // message loop level".
295 void SetNestableTasksAllowed(bool allowed);
296 bool NestableTasksAllowed() const;
297
[email protected]18d6a8f2009-12-16 22:56:33298 // Enables nestable tasks on |loop| while in scope.
299 class ScopedNestableTaskAllower {
300 public:
301 explicit ScopedNestableTaskAllower(MessageLoop* loop)
302 : loop_(loop),
303 old_state_(loop_->NestableTasksAllowed()) {
304 loop_->SetNestableTasksAllowed(true);
305 }
306 ~ScopedNestableTaskAllower() {
307 loop_->SetNestableTasksAllowed(old_state_);
308 }
309
310 private:
311 MessageLoop* loop_;
312 bool old_state_;
313 };
314
initial.commitd7cae122008-07-26 21:49:38315 // Enables or disables the restoration during an exception of the unhandled
316 // exception filter that was active when Run() was called. This can happen
317 // if some third party code call SetUnhandledExceptionFilter() and never
318 // restores the previous filter.
319 void set_exception_restoration(bool restore) {
320 exception_restoration_ = restore;
321 }
322
[email protected]b5f95102009-07-01 19:53:59323 // Returns true if we are currently running a nested message loop.
324 bool IsNested();
325
[email protected]a502bbe72011-01-07 18:06:45326 // A TaskObserver is an object that receives task notifications from the
327 // MessageLoop.
328 //
329 // NOTE: A TaskObserver implementation should be extremely fast!
[email protected]0bea7252011-08-05 15:34:00330 class BASE_EXPORT TaskObserver {
[email protected]a502bbe72011-01-07 18:06:45331 public:
332 TaskObserver();
333
334 // This method is called before processing a task.
[email protected]b224f792011-04-20 16:02:23335 virtual void WillProcessTask(base::TimeTicks time_posted) = 0;
[email protected]a502bbe72011-01-07 18:06:45336
337 // This method is called after processing a task.
[email protected]b224f792011-04-20 16:02:23338 virtual void DidProcessTask(base::TimeTicks time_posted) = 0;
[email protected]a502bbe72011-01-07 18:06:45339
340 protected:
341 virtual ~TaskObserver();
342 };
343
[email protected]9cfb89a2010-06-09 21:20:41344 // These functions can only be called on the same thread that |this| is
345 // running on.
346 void AddTaskObserver(TaskObserver* task_observer);
347 void RemoveTaskObserver(TaskObserver* task_observer);
348
[email protected]57f030a2010-06-29 04:58:15349 // Returns true if the message loop has high resolution timers enabled.
350 // Provided for testing.
351 bool high_resolution_timers_enabled() {
352#if defined(OS_WIN)
353 return !high_resolution_timer_expiration_.is_null();
354#else
355 return true;
356#endif
357 }
358
359 // When we go into high resolution timer mode, we will stay in hi-res mode
360 // for at least 1s.
361 static const int kHighResolutionTimerModeLeaseTimeMs = 1000;
362
[email protected]8d6ab8f52011-01-26 00:53:48363 // Asserts that the MessageLoop is "idle".
364 void AssertIdle() const;
365
[email protected]2ec01fe2011-03-24 03:40:28366#if defined(OS_WIN)
367 void set_os_modal_loop(bool os_modal_loop) {
368 os_modal_loop_ = os_modal_loop;
369 }
370
371 bool os_modal_loop() const {
372 return os_modal_loop_;
373 }
374#endif // OS_WIN
375
[email protected]e6244c182011-11-01 22:06:58376 // Can only be called from the thread that owns the MessageLoop.
377 bool is_running() const;
378
initial.commitd7cae122008-07-26 21:49:38379 //----------------------------------------------------------------------------
[email protected]4d9bdfaf2008-08-26 05:53:57380 protected:
[email protected]fc7fb6e2008-08-16 03:09:05381 struct RunState {
382 // Used to count how many Run() invocations are on the stack.
383 int run_depth;
initial.commitd7cae122008-07-26 21:49:38384
[email protected]fc7fb6e2008-08-16 03:09:05385 // Used to record that Quit() was called, or that we should quit the pump
386 // once it becomes idle.
387 bool quit_received;
initial.commitd7cae122008-07-26 21:49:38388
[email protected]61c86c62011-08-02 16:11:16389#if !defined(OS_MACOSX) && !defined(OS_ANDROID)
[email protected]148d1052009-07-31 22:53:37390 Dispatcher* dispatcher;
[email protected]fc7fb6e2008-08-16 03:09:05391#endif
392 };
393
[email protected]61c86c62011-08-02 16:11:16394#if defined(OS_ANDROID)
395 // Android Java process manages the UI thread message loop. So its
396 // MessagePumpForUI needs to keep the RunState.
397 public:
398#endif
[email protected]0bea7252011-08-05 15:34:00399 class BASE_EXPORT AutoRunState : RunState {
[email protected]fc7fb6e2008-08-16 03:09:05400 public:
[email protected]b0992172008-10-27 18:54:18401 explicit AutoRunState(MessageLoop* loop);
[email protected]fc7fb6e2008-08-16 03:09:05402 ~AutoRunState();
initial.commitd7cae122008-07-26 21:49:38403 private:
404 MessageLoop* loop_;
[email protected]fc7fb6e2008-08-16 03:09:05405 RunState* previous_state_;
406 };
[email protected]61c86c62011-08-02 16:11:16407#if defined(OS_ANDROID)
408 protected:
409#endif
initial.commitd7cae122008-07-26 21:49:38410
[email protected]fc7fb6e2008-08-16 03:09:05411#if defined(OS_WIN)
412 base::MessagePumpWin* pump_win() {
413 return static_cast<base::MessagePumpWin*>(pump_.get());
414 }
[email protected]36987e92008-09-18 18:46:26415#elif defined(OS_POSIX)
416 base::MessagePumpLibevent* pump_libevent() {
417 return static_cast<base::MessagePumpLibevent*>(pump_.get());
418 }
[email protected]fc7fb6e2008-08-16 03:09:05419#endif
[email protected]3882c4332008-07-30 19:03:59420
421 // A function to encapsulate all the exception handling capability in the
[email protected]fc7fb6e2008-08-16 03:09:05422 // stacks around the running of a main message loop. It will run the message
423 // loop in a SEH try block or not depending on the set_SEH_restoration()
[email protected]aff8a132009-10-26 18:15:59424 // flag invoking respectively RunInternalInSEHFrame() or RunInternal().
[email protected]fc7fb6e2008-08-16 03:09:05425 void RunHandler();
initial.commitd7cae122008-07-26 21:49:38426
[email protected]aff8a132009-10-26 18:15:59427#if defined(OS_WIN)
428 __declspec(noinline) void RunInternalInSEHFrame();
429#endif
430
[email protected]3882c4332008-07-30 19:03:59431 // A surrounding stack frame around the running of the message loop that
432 // supports all saving and restoring of state, as is needed for any/all (ugly)
433 // recursive calls.
[email protected]fc7fb6e2008-08-16 03:09:05434 void RunInternal();
[email protected]ea15e982008-08-15 07:31:20435
[email protected]fc7fb6e2008-08-16 03:09:05436 // Called to process any delayed non-nestable tasks.
initial.commitd7cae122008-07-26 21:49:38437 bool ProcessNextDelayedNonNestableTask();
initial.commitd7cae122008-07-26 21:49:38438
[email protected]b224f792011-04-20 16:02:23439 // Runs the specified PendingTask.
[email protected]dd1f9fe2011-11-15 23:36:30440 void RunTask(const base::PendingTask& pending_task);
initial.commitd7cae122008-07-26 21:49:38441
[email protected]752578562008-09-07 08:08:29442 // Calls RunTask or queues the pending_task on the deferred task list if it
443 // cannot be run right now. Returns true if the task was run.
[email protected]dd1f9fe2011-11-15 23:36:30444 bool DeferOrRunPendingTask(const base::PendingTask& pending_task);
initial.commitd7cae122008-07-26 21:49:38445
[email protected]001747c2008-09-10 00:37:07446 // Adds the pending task to delayed_work_queue_.
[email protected]dd1f9fe2011-11-15 23:36:30447 void AddToDelayedWorkQueue(const base::PendingTask& pending_task);
[email protected]001747c2008-09-10 00:37:07448
[email protected]b224f792011-04-20 16:02:23449 // Adds the pending task to our incoming_queue_.
450 //
451 // Caller retains ownership of |pending_task|, but this function will
452 // reset the value of pending_task->task. This is needed to ensure
453 // that the posting call stack does not retain pending_task->task
454 // beyond this function call.
[email protected]dd1f9fe2011-11-15 23:36:30455 void AddToIncomingQueue(base::PendingTask* pending_task);
[email protected]b224f792011-04-20 16:02:23456
initial.commitd7cae122008-07-26 21:49:38457 // Load tasks from the incoming_queue_ into work_queue_ if the latter is
458 // empty. The former requires a lock to access, while the latter is directly
459 // accessible on this thread.
460 void ReloadWorkQueue();
461
462 // Delete tasks that haven't run yet without running them. Used in the
[email protected]001747c2008-09-10 00:37:07463 // destructor to make sure all the task's destructors get called. Returns
464 // true if some work was done.
465 bool DeletePendingTasks();
initial.commitd7cae122008-07-26 21:49:38466
[email protected]dd1f9fe2011-11-15 23:36:30467 // Calculates the time at which a PendingTask should run.
[email protected]b224f792011-04-20 16:02:23468 base::TimeTicks CalculateDelayedRuntime(int64 delay_ms);
[email protected]fc7fb6e2008-08-16 03:09:05469
initial.commitd7cae122008-07-26 21:49:38470 // Start recording histogram info about events and action IF it was enabled
471 // and IF the statistics recorder can accept a registration of our histogram.
472 void StartHistogrammer();
473
[email protected]dd1f9fe2011-11-15 23:36:30474 // Add occurrence of event to our histogram, so that we can see what is being
initial.commitd7cae122008-07-26 21:49:38475 // done in a specific MessageLoop instance (i.e., specific thread).
476 // If message_histogram_ is NULL, this is a no-op.
477 void HistogramEvent(int event);
478
[email protected]a502bbe72011-01-07 18:06:45479 // base::MessagePump::Delegate methods:
[email protected]b1719462011-11-16 00:08:08480 virtual bool DoWork() OVERRIDE;
481 virtual bool DoDelayedWork(base::TimeTicks* next_delayed_work_time) OVERRIDE;
482 virtual bool DoIdleWork() OVERRIDE;
[email protected]a502bbe72011-01-07 18:06:45483
[email protected]4d9bdfaf2008-08-26 05:53:57484 Type type_;
485
[email protected]752578562008-09-07 08:08:29486 // A list of tasks that need to be processed by this instance. Note that
487 // this queue is only accessed (push/pop) by our current thread.
[email protected]dd1f9fe2011-11-15 23:36:30488 base::TaskQueue work_queue_;
[email protected]b0992172008-10-27 18:54:18489
[email protected]752578562008-09-07 08:08:29490 // Contains delayed tasks, sorted by their 'delayed_run_time' property.
[email protected]262060ff2011-11-17 23:26:53491 base::DelayedTaskQueue delayed_work_queue_;
initial.commitd7cae122008-07-26 21:49:38492
[email protected]a8f7d3d2010-11-04 23:23:42493 // A recent snapshot of Time::Now(), used to check delayed_work_queue_.
[email protected]7e7fab42010-11-06 22:23:29494 base::TimeTicks recent_time_;
[email protected]a8f7d3d2010-11-04 23:23:42495
[email protected]752578562008-09-07 08:08:29496 // A queue of non-nestable tasks that we had to defer because when it came
497 // time to execute them we were in a nested message loop. They will execute
498 // once we're out of nested message loops.
[email protected]dd1f9fe2011-11-15 23:36:30499 base::TaskQueue deferred_non_nestable_work_queue_;
initial.commitd7cae122008-07-26 21:49:38500
[email protected]fc7fb6e2008-08-16 03:09:05501 scoped_refptr<base::MessagePump> pump_;
[email protected]ee132ad2008-08-06 21:27:02502
[email protected]2a127252008-08-05 23:16:41503 ObserverList<DestructionObserver> destruction_observers_;
[email protected]001747c2008-09-10 00:37:07504
[email protected]dd1f9fe2011-11-15 23:36:30505 // A recursion block that prevents accidentally running additional tasks when
initial.commitd7cae122008-07-26 21:49:38506 // insider a (accidentally induced?) nested message pump.
507 bool nestable_tasks_allowed_;
508
509 bool exception_restoration_;
510
initial.commitd7cae122008-07-26 21:49:38511 std::string thread_name_;
512 // A profiling histogram showing the counts of various messages and events.
[email protected]81ce9f3b2011-04-05 04:48:53513 base::Histogram* message_histogram_;
initial.commitd7cae122008-07-26 21:49:38514
515 // A null terminated list which creates an incoming_queue of tasks that are
[email protected]242c4bd2011-02-25 18:43:23516 // acquired under a mutex for processing on this instance's thread. These
[email protected]b224f792011-04-20 16:02:23517 // tasks have not yet been sorted out into items for our work_queue_ vs items
518 // that will be handled by the TimerManager.
[email protected]dd1f9fe2011-11-15 23:36:30519 base::TaskQueue incoming_queue_;
initial.commitd7cae122008-07-26 21:49:38520 // Protect access to incoming_queue_.
[email protected]8d6ab8f52011-01-26 00:53:48521 mutable base::Lock incoming_queue_lock_;
initial.commitd7cae122008-07-26 21:49:38522
[email protected]fc7fb6e2008-08-16 03:09:05523 RunState* state_;
initial.commitd7cae122008-07-26 21:49:38524
[email protected]b224f792011-04-20 16:02:23525 // The need for this variable is subtle. Please see implementation comments
526 // around where it is used.
527 bool should_leak_tasks_;
528
[email protected]57f030a2010-06-29 04:58:15529#if defined(OS_WIN)
530 base::TimeTicks high_resolution_timer_expiration_;
[email protected]2ec01fe2011-03-24 03:40:28531 // Should be set to true before calling Windows APIs like TrackPopupMenu, etc
532 // which enter a modal message loop.
533 bool os_modal_loop_;
[email protected]57f030a2010-06-29 04:58:15534#endif
535
[email protected]752578562008-09-07 08:08:29536 // The next sequence number to use for delayed tasks.
537 int next_sequence_num_;
538
[email protected]9cfb89a2010-06-09 21:20:41539 ObserverList<TaskObserver> task_observers_;
540
[email protected]edd685f2011-08-15 20:33:46541 // The message loop proxy associated with this message loop, if one exists.
542 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_;
543
[email protected]26fbf802011-03-25 18:48:03544 private:
[email protected]fc7fb6e2008-08-16 03:09:05545 DISALLOW_COPY_AND_ASSIGN(MessageLoop);
initial.commitd7cae122008-07-26 21:49:38546};
547
[email protected]4d9bdfaf2008-08-26 05:53:57548//-----------------------------------------------------------------------------
549// MessageLoopForUI extends MessageLoop with methods that are particular to a
550// MessageLoop instantiated with TYPE_UI.
551//
552// This class is typically used like so:
553// MessageLoopForUI::current()->...call some method...
554//
[email protected]0bea7252011-08-05 15:34:00555class BASE_EXPORT MessageLoopForUI : public MessageLoop {
[email protected]4d9bdfaf2008-08-26 05:53:57556 public:
557 MessageLoopForUI() : MessageLoop(TYPE_UI) {
558 }
license.botbf09a502008-08-24 00:55:55559
[email protected]4d9bdfaf2008-08-26 05:53:57560 // Returns the MessageLoopForUI of the current thread.
561 static MessageLoopForUI* current() {
562 MessageLoop* loop = MessageLoop::current();
563 DCHECK_EQ(MessageLoop::TYPE_UI, loop->type());
564 return static_cast<MessageLoopForUI*>(loop);
565 }
566
567#if defined(OS_WIN)
[email protected]4d9bdfaf2008-08-26 05:53:57568 void DidProcessMessage(const MSG& message);
[email protected]9cfb89a2010-06-09 21:20:41569#endif // defined(OS_WIN)
[email protected]1a8f5d1d2008-09-25 20:33:04570
[email protected]61c86c62011-08-02 16:11:16571#if defined(OS_ANDROID)
572 // On Android, the UI message loop is handled by Java side. So Run() should
573 // never be called. Instead use Start(), which will forward all the native UI
574 // events to the Java message loop.
575 void Start();
576#elif !defined(OS_MACOSX)
[email protected]148d1052009-07-31 22:53:37577 // Please see message_pump_win/message_pump_glib for definitions of these
578 // methods.
579 void AddObserver(Observer* observer);
580 void RemoveObserver(Observer* observer);
[email protected]4d6285312011-10-24 07:19:51581 void RunWithDispatcher(Dispatcher* dispatcher);
[email protected]35e9b66a2011-10-06 18:19:21582 void RunAllPendingWithDispatcher(Dispatcher* dispatcher);
[email protected]148d1052009-07-31 22:53:37583
[email protected]1a8f5d1d2008-09-25 20:33:04584 protected:
585 // TODO(rvargas): Make this platform independent.
[email protected]17b89142008-11-07 21:52:15586 base::MessagePumpForUI* pump_ui() {
[email protected]1a8f5d1d2008-09-25 20:33:04587 return static_cast<base::MessagePumpForUI*>(pump_.get());
588 }
[email protected]9cfb89a2010-06-09 21:20:41589#endif // !defined(OS_MACOSX)
[email protected]4d9bdfaf2008-08-26 05:53:57590};
591
592// Do not add any member variables to MessageLoopForUI! This is important b/c
593// MessageLoopForUI is often allocated via MessageLoop(TYPE_UI). Any extra
594// data that you need should be stored on the MessageLoop's pump_ instance.
595COMPILE_ASSERT(sizeof(MessageLoop) == sizeof(MessageLoopForUI),
596 MessageLoopForUI_should_not_have_extra_member_variables);
597
598//-----------------------------------------------------------------------------
599// MessageLoopForIO extends MessageLoop with methods that are particular to a
600// MessageLoop instantiated with TYPE_IO.
601//
602// This class is typically used like so:
603// MessageLoopForIO::current()->...call some method...
604//
[email protected]0bea7252011-08-05 15:34:00605class BASE_EXPORT MessageLoopForIO : public MessageLoop {
[email protected]4d9bdfaf2008-08-26 05:53:57606 public:
[email protected]9cfb89a2010-06-09 21:20:41607#if defined(OS_WIN)
608 typedef base::MessagePumpForIO::IOHandler IOHandler;
609 typedef base::MessagePumpForIO::IOContext IOContext;
610 typedef base::MessagePumpForIO::IOObserver IOObserver;
611#elif defined(OS_POSIX)
612 typedef base::MessagePumpLibevent::Watcher Watcher;
613 typedef base::MessagePumpLibevent::FileDescriptorWatcher
614 FileDescriptorWatcher;
615 typedef base::MessagePumpLibevent::IOObserver IOObserver;
616
617 enum Mode {
618 WATCH_READ = base::MessagePumpLibevent::WATCH_READ,
619 WATCH_WRITE = base::MessagePumpLibevent::WATCH_WRITE,
620 WATCH_READ_WRITE = base::MessagePumpLibevent::WATCH_READ_WRITE
621 };
622
623#endif
624
[email protected]4d9bdfaf2008-08-26 05:53:57625 MessageLoopForIO() : MessageLoop(TYPE_IO) {
626 }
627
628 // Returns the MessageLoopForIO of the current thread.
629 static MessageLoopForIO* current() {
630 MessageLoop* loop = MessageLoop::current();
631 DCHECK_EQ(MessageLoop::TYPE_IO, loop->type());
632 return static_cast<MessageLoopForIO*>(loop);
633 }
634
[email protected]9cfb89a2010-06-09 21:20:41635 void AddIOObserver(IOObserver* io_observer) {
636 pump_io()->AddIOObserver(io_observer);
637 }
[email protected]941281482010-06-09 05:10:48638
[email protected]9cfb89a2010-06-09 21:20:41639 void RemoveIOObserver(IOObserver* io_observer) {
640 pump_io()->RemoveIOObserver(io_observer);
641 }
642
643#if defined(OS_WIN)
[email protected]4d9bdfaf2008-08-26 05:53:57644 // Please see MessagePumpWin for definitions of these methods.
[email protected]32cda29d2008-10-09 23:58:43645 void RegisterIOHandler(HANDLE file_handle, IOHandler* handler);
[email protected]17b89142008-11-07 21:52:15646 bool WaitForIOCompletion(DWORD timeout, IOHandler* filter);
[email protected]36987e92008-09-18 18:46:26647
[email protected]1a8f5d1d2008-09-25 20:33:04648 protected:
649 // TODO(rvargas): Make this platform independent.
650 base::MessagePumpForIO* pump_io() {
651 return static_cast<base::MessagePumpForIO*>(pump_.get());
652 }
653
[email protected]36987e92008-09-18 18:46:26654#elif defined(OS_POSIX)
[email protected]e45e6c02008-12-15 22:02:17655 // Please see MessagePumpLibevent for definition.
656 bool WatchFileDescriptor(int fd,
657 bool persistent,
658 Mode mode,
[email protected]320eff42011-11-15 00:29:48659 FileDescriptorWatcher* controller,
660 Watcher* delegate);
[email protected]9cfb89a2010-06-09 21:20:41661
662 private:
663 base::MessagePumpLibevent* pump_io() {
664 return static_cast<base::MessagePumpLibevent*>(pump_.get());
665 }
[email protected]1a8f5d1d2008-09-25 20:33:04666#endif // defined(OS_POSIX)
[email protected]4d9bdfaf2008-08-26 05:53:57667};
668
669// Do not add any member variables to MessageLoopForIO! This is important b/c
670// MessageLoopForIO is often allocated via MessageLoop(TYPE_IO). Any extra
671// data that you need should be stored on the MessageLoop's pump_ instance.
672COMPILE_ASSERT(sizeof(MessageLoop) == sizeof(MessageLoopForIO),
673 MessageLoopForIO_should_not_have_extra_member_variables);
674
675#endif // BASE_MESSAGE_LOOP_H_