blob: 355d58b62b46a907c078332779dc1e795639e8eb [file] [log] [blame]
[email protected]e7b3a612012-01-05 02:18:181// Copyright (c) 2012 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]2d715662011-11-28 22:00:2914#include "base/callback_forward.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]6b28d942012-02-15 01:43:1921#include "base/sequenced_task_runner_helpers.h"
[email protected]20305ec2011-01-21 04:55:5222#include "base/synchronization/lock.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]69c88e12011-11-23 02:07:4536#elif 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:
[email protected]b5717a42012-02-14 19:33:5270// HRESULT hr;
71// {
72// MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current());
73// hr = DoDragDrop(...); // Implicitly runs a modal message loop.
74// }
75// // Process |hr| (the result returned by DoDragDrop()).
initial.commitd7cae122008-07-26 21:49:3876//
[email protected]fc7fb6e2008-08-16 03:09:0577// Please be SURE your task is reentrant (nestable) and all global variables
78// are stable and accessible before calling SetNestableTasksAllowed(true).
initial.commitd7cae122008-07-26 21:49:3879//
[email protected]0bea7252011-08-05 15:34:0080class BASE_EXPORT MessageLoop : public base::MessagePump::Delegate {
initial.commitd7cae122008-07-26 21:49:3881 public:
[email protected]a502bbe72011-01-07 18:06:4582#if defined(OS_WIN)
83 typedef base::MessagePumpWin::Dispatcher Dispatcher;
[email protected]2e5597c2011-10-04 00:10:4784 typedef base::MessagePumpObserver Observer;
[email protected]4d0f93c2011-09-29 04:43:5485#elif !defined(OS_MACOSX) && !defined(OS_ANDROID)
[email protected]2047ef42011-06-24 20:10:2586 typedef base::MessagePumpDispatcher Dispatcher;
87 typedef base::MessagePumpObserver Observer;
[email protected]a502bbe72011-01-07 18:06:4588#endif
89
90 // A MessageLoop has a particular type, which indicates the set of
91 // asynchronous events it may process in addition to tasks and timers.
[email protected]9cfb89a2010-06-09 21:20:4192 //
[email protected]a502bbe72011-01-07 18:06:4593 // TYPE_DEFAULT
94 // This type of ML only supports tasks and timers.
95 //
96 // TYPE_UI
97 // This type of ML also supports native UI events (e.g., Windows messages).
98 // See also MessageLoopForUI.
99 //
100 // TYPE_IO
101 // This type of ML also supports asynchronous IO. See also
102 // MessageLoopForIO.
103 //
104 enum Type {
105 TYPE_DEFAULT,
106 TYPE_UI,
107 TYPE_IO
[email protected]9cfb89a2010-06-09 21:20:41108 };
109
[email protected]a502bbe72011-01-07 18:06:45110 // Normally, it is not necessary to instantiate a MessageLoop. Instead, it
111 // is typical to make use of the current thread's MessageLoop instance.
112 explicit MessageLoop(Type type = TYPE_DEFAULT);
[email protected]3690ebe02011-05-25 09:08:19113 virtual ~MessageLoop();
[email protected]a502bbe72011-01-07 18:06:45114
[email protected]9989c9bb2011-01-07 20:23:43115 // Returns the MessageLoop object for the current thread, or null if none.
116 static MessageLoop* current();
117
initial.commitd7cae122008-07-26 21:49:38118 static void EnableHistogrammer(bool enable_histogrammer);
119
[email protected]61c86c62011-08-02 16:11:16120 typedef base::MessagePump* (MessagePumpFactory)();
121 // Using the given base::MessagePumpForUIFactory to override the default
122 // MessagePump implementation for 'TYPE_UI'.
123 static void InitMessagePumpForUIFactory(MessagePumpFactory* factory);
124
[email protected]ee132ad2008-08-06 21:27:02125 // A DestructionObserver is notified when the current MessageLoop is being
[email protected]dd1f9fe2011-11-15 23:36:30126 // destroyed. These observers are notified prior to MessageLoop::current()
[email protected]ee132ad2008-08-06 21:27:02127 // being changed to return NULL. This gives interested parties the chance to
128 // do final cleanup that depends on the MessageLoop.
129 //
130 // NOTE: Any tasks posted to the MessageLoop during this notification will
131 // not be run. Instead, they will be deleted.
132 //
[email protected]0bea7252011-08-05 15:34:00133 class BASE_EXPORT DestructionObserver {
[email protected]ee132ad2008-08-06 21:27:02134 public:
[email protected]ee132ad2008-08-06 21:27:02135 virtual void WillDestroyCurrentMessageLoop() = 0;
[email protected]23c386b2010-09-15 22:14:36136
137 protected:
138 virtual ~DestructionObserver();
[email protected]ee132ad2008-08-06 21:27:02139 };
140
141 // Add a DestructionObserver, which will start receiving notifications
142 // immediately.
143 void AddDestructionObserver(DestructionObserver* destruction_observer);
144
145 // Remove a DestructionObserver. It is safe to call this method while a
146 // DestructionObserver is receiving a notification callback.
147 void RemoveDestructionObserver(DestructionObserver* destruction_observer);
148
[email protected]752578562008-09-07 08:08:29149 // The "PostTask" family of methods call the task's Run method asynchronously
150 // from within a message loop at some point in the future.
initial.commitd7cae122008-07-26 21:49:38151 //
[email protected]752578562008-09-07 08:08:29152 // With the PostTask variant, tasks are invoked in FIFO order, inter-mixed
153 // with normal UI or IO event processing. With the PostDelayedTask variant,
154 // tasks are called after at least approximately 'delay_ms' have elapsed.
initial.commitd7cae122008-07-26 21:49:38155 //
[email protected]752578562008-09-07 08:08:29156 // The NonNestable variants work similarly except that they promise never to
157 // dispatch the task from a nested invocation of MessageLoop::Run. Instead,
158 // such tasks get deferred until the top-most MessageLoop::Run is executing.
159 //
160 // The MessageLoop takes ownership of the Task, and deletes it after it has
161 // been Run().
162 //
163 // NOTE: These methods may be called on any thread. The Task will be invoked
initial.commitd7cae122008-07-26 21:49:38164 // on the thread that executes MessageLoop::Run().
[email protected]b224f792011-04-20 16:02:23165 void PostTask(
166 const tracked_objects::Location& from_here,
167 const base::Closure& task);
168
169 void PostDelayedTask(
170 const tracked_objects::Location& from_here,
171 const base::Closure& task, int64 delay_ms);
172
[email protected]767842922011-12-30 01:04:36173 void PostDelayedTask(
174 const tracked_objects::Location& from_here,
175 const base::Closure& task,
176 base::TimeDelta delay);
177
[email protected]b224f792011-04-20 16:02:23178 void PostNonNestableTask(
179 const tracked_objects::Location& from_here,
180 const base::Closure& task);
181
182 void PostNonNestableDelayedTask(
183 const tracked_objects::Location& from_here,
184 const base::Closure& task, int64 delay_ms);
185
[email protected]767842922011-12-30 01:04:36186 void PostNonNestableDelayedTask(
187 const tracked_objects::Location& from_here,
188 const base::Closure& task,
189 base::TimeDelta delay);
190
initial.commitd7cae122008-07-26 21:49:38191 // A variant on PostTask that deletes the given object. This is useful
192 // if the object needs to live until the next run of the MessageLoop (for
193 // example, deleting a RenderProcessHost from within an IPC callback is not
194 // good).
195 //
196 // NOTE: This method may be called on any thread. The object will be deleted
197 // on the thread that executes MessageLoop::Run(). If this is not the same
198 // as the thread that calls PostDelayedTask(FROM_HERE, ), then T MUST inherit
199 // from RefCountedThreadSafe<T>!
200 template <class T>
[email protected]00ed48f2010-10-22 22:19:24201 void DeleteSoon(const tracked_objects::Location& from_here, const T* object) {
[email protected]6b28d942012-02-15 01:43:19202 base::subtle::DeleteHelperInternal<T, void>::DeleteViaSequencedTaskRunner(
[email protected]b1d183282011-12-30 04:32:58203 this, from_here, object);
initial.commitd7cae122008-07-26 21:49:38204 }
205
206 // A variant on PostTask that releases the given reference counted object
207 // (by calling its Release method). This is useful if the object needs to
208 // live until the next run of the MessageLoop, or if the object needs to be
209 // released on a particular thread.
210 //
211 // NOTE: This method may be called on any thread. The object will be
212 // released (and thus possibly deleted) on the thread that executes
213 // MessageLoop::Run(). If this is not the same as the thread that calls
214 // PostDelayedTask(FROM_HERE, ), then T MUST inherit from
215 // RefCountedThreadSafe<T>!
216 template <class T>
[email protected]00ed48f2010-10-22 22:19:24217 void ReleaseSoon(const tracked_objects::Location& from_here,
218 const T* object) {
[email protected]6b28d942012-02-15 01:43:19219 base::subtle::ReleaseHelperInternal<T, void>::ReleaseViaSequencedTaskRunner(
[email protected]c29985e2011-12-30 06:46:30220 this, from_here, object);
initial.commitd7cae122008-07-26 21:49:38221 }
222
[email protected]3882c4332008-07-30 19:03:59223 // Run the message loop.
initial.commitd7cae122008-07-26 21:49:38224 void Run();
225
[email protected]7e0e8762008-07-31 13:10:20226 // Process all pending tasks, windows messages, etc., but don't wait/sleep.
227 // Return as soon as all items that can be run are taken care of.
228 void RunAllPending();
[email protected]3882c4332008-07-30 19:03:59229
initial.commitd7cae122008-07-26 21:49:38230 // Signals the Run method to return after it is done processing all pending
[email protected]fc7fb6e2008-08-16 03:09:05231 // messages. This method may only be called on the same thread that called
232 // Run, and Run must still be on the call stack.
initial.commitd7cae122008-07-26 21:49:38233 //
[email protected]45ce1e202011-12-10 19:44:25234 // Use QuitClosure if you need to Quit another thread's MessageLoop, but note
235 // that doing so is fairly dangerous if the target thread makes nested calls
236 // to MessageLoop::Run. The problem being that you won't know which nested
237 // run loop you are quitting, so be careful!
initial.commitd7cae122008-07-26 21:49:38238 void Quit();
239
[email protected]781a7ed2010-02-23 07:12:22240 // This method is a variant of Quit, that does not wait for pending messages
241 // to be processed before returning from Run.
242 void QuitNow();
243
[email protected]8c6517e52011-10-17 01:20:36244 // Invokes Quit on the current MessageLoop when run. Useful to schedule an
245 // arbitrary MessageLoop to Quit.
246 static base::Closure QuitClosure();
247
[email protected]4d9bdfaf2008-08-26 05:53:57248 // Returns the type passed to the constructor.
249 Type type() const { return type_; }
250
initial.commitd7cae122008-07-26 21:49:38251 // Optional call to connect the thread name with this loop.
[email protected]fc7fb6e2008-08-16 03:09:05252 void set_thread_name(const std::string& thread_name) {
253 DCHECK(thread_name_.empty()) << "Should not rename this thread!";
254 thread_name_ = thread_name;
255 }
[email protected]ee132ad2008-08-06 21:27:02256 const std::string& thread_name() const { return thread_name_; }
initial.commitd7cae122008-07-26 21:49:38257
[email protected]c31af70db22011-08-18 23:13:01258 // Gets the message loop proxy associated with this message loop.
[email protected]edd685f2011-08-15 20:33:46259 scoped_refptr<base::MessageLoopProxy> message_loop_proxy() {
260 return message_loop_proxy_.get();
261 }
262
initial.commitd7cae122008-07-26 21:49:38263 // Enables or disables the recursive task processing. This happens in the case
264 // of recursive message loops. Some unwanted message loop may occurs when
265 // using common controls or printer functions. By default, recursive task
266 // processing is disabled.
267 //
[email protected]b5717a42012-02-14 19:33:52268 // Please utilize |ScopedNestableTaskAllower| instead of calling these methods
269 // directly. In general nestable message loops are to be avoided. They are
270 // dangerous and difficult to get right, so please use with extreme caution.
271 //
initial.commitd7cae122008-07-26 21:49:38272 // The specific case where tasks get queued is:
273 // - The thread is running a message loop.
274 // - It receives a task #1 and execute it.
275 // - The task #1 implicitly start a message loop, like a MessageBox in the
276 // unit test. This can also be StartDoc or GetSaveFileName.
277 // - The thread receives a task #2 before or while in this second message
278 // loop.
279 // - With NestableTasksAllowed set to true, the task #2 will run right away.
280 // Otherwise, it will get executed right after task #1 completes at "thread
281 // message loop level".
282 void SetNestableTasksAllowed(bool allowed);
283 bool NestableTasksAllowed() const;
284
[email protected]18d6a8f2009-12-16 22:56:33285 // Enables nestable tasks on |loop| while in scope.
286 class ScopedNestableTaskAllower {
287 public:
288 explicit ScopedNestableTaskAllower(MessageLoop* loop)
289 : loop_(loop),
290 old_state_(loop_->NestableTasksAllowed()) {
291 loop_->SetNestableTasksAllowed(true);
292 }
293 ~ScopedNestableTaskAllower() {
294 loop_->SetNestableTasksAllowed(old_state_);
295 }
296
297 private:
298 MessageLoop* loop_;
299 bool old_state_;
300 };
301
initial.commitd7cae122008-07-26 21:49:38302 // Enables or disables the restoration during an exception of the unhandled
303 // exception filter that was active when Run() was called. This can happen
304 // if some third party code call SetUnhandledExceptionFilter() and never
305 // restores the previous filter.
306 void set_exception_restoration(bool restore) {
307 exception_restoration_ = restore;
308 }
309
[email protected]b5f95102009-07-01 19:53:59310 // Returns true if we are currently running a nested message loop.
311 bool IsNested();
312
[email protected]a502bbe72011-01-07 18:06:45313 // A TaskObserver is an object that receives task notifications from the
314 // MessageLoop.
315 //
316 // NOTE: A TaskObserver implementation should be extremely fast!
[email protected]0bea7252011-08-05 15:34:00317 class BASE_EXPORT TaskObserver {
[email protected]a502bbe72011-01-07 18:06:45318 public:
319 TaskObserver();
320
321 // This method is called before processing a task.
[email protected]b224f792011-04-20 16:02:23322 virtual void WillProcessTask(base::TimeTicks time_posted) = 0;
[email protected]a502bbe72011-01-07 18:06:45323
324 // This method is called after processing a task.
[email protected]b224f792011-04-20 16:02:23325 virtual void DidProcessTask(base::TimeTicks time_posted) = 0;
[email protected]a502bbe72011-01-07 18:06:45326
327 protected:
328 virtual ~TaskObserver();
329 };
330
[email protected]9cfb89a2010-06-09 21:20:41331 // These functions can only be called on the same thread that |this| is
332 // running on.
333 void AddTaskObserver(TaskObserver* task_observer);
334 void RemoveTaskObserver(TaskObserver* task_observer);
335
[email protected]57f030a2010-06-29 04:58:15336 // Returns true if the message loop has high resolution timers enabled.
337 // Provided for testing.
338 bool high_resolution_timers_enabled() {
339#if defined(OS_WIN)
340 return !high_resolution_timer_expiration_.is_null();
341#else
342 return true;
343#endif
344 }
345
346 // When we go into high resolution timer mode, we will stay in hi-res mode
347 // for at least 1s.
348 static const int kHighResolutionTimerModeLeaseTimeMs = 1000;
349
[email protected]8d6ab8f52011-01-26 00:53:48350 // Asserts that the MessageLoop is "idle".
351 void AssertIdle() const;
352
[email protected]2ec01fe2011-03-24 03:40:28353#if defined(OS_WIN)
354 void set_os_modal_loop(bool os_modal_loop) {
355 os_modal_loop_ = os_modal_loop;
356 }
357
358 bool os_modal_loop() const {
359 return os_modal_loop_;
360 }
361#endif // OS_WIN
362
[email protected]e6244c182011-11-01 22:06:58363 // Can only be called from the thread that owns the MessageLoop.
364 bool is_running() const;
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]fc7fb6e2008-08-16 03:09:05398#if defined(OS_WIN)
399 base::MessagePumpWin* pump_win() {
400 return static_cast<base::MessagePumpWin*>(pump_.get());
401 }
[email protected]36987e92008-09-18 18:46:26402#elif defined(OS_POSIX)
403 base::MessagePumpLibevent* pump_libevent() {
404 return static_cast<base::MessagePumpLibevent*>(pump_.get());
405 }
[email protected]fc7fb6e2008-08-16 03:09:05406#endif
[email protected]3882c4332008-07-30 19:03:59407
408 // A function to encapsulate all the exception handling capability in the
[email protected]fc7fb6e2008-08-16 03:09:05409 // stacks around the running of a main message loop. It will run the message
410 // loop in a SEH try block or not depending on the set_SEH_restoration()
[email protected]aff8a132009-10-26 18:15:59411 // flag invoking respectively RunInternalInSEHFrame() or RunInternal().
[email protected]fc7fb6e2008-08-16 03:09:05412 void RunHandler();
initial.commitd7cae122008-07-26 21:49:38413
[email protected]aff8a132009-10-26 18:15:59414#if defined(OS_WIN)
415 __declspec(noinline) void RunInternalInSEHFrame();
416#endif
417
[email protected]3882c4332008-07-30 19:03:59418 // A surrounding stack frame around the running of the message loop that
419 // supports all saving and restoring of state, as is needed for any/all (ugly)
420 // recursive calls.
[email protected]fc7fb6e2008-08-16 03:09:05421 void RunInternal();
[email protected]ea15e982008-08-15 07:31:20422
[email protected]fc7fb6e2008-08-16 03:09:05423 // Called to process any delayed non-nestable tasks.
initial.commitd7cae122008-07-26 21:49:38424 bool ProcessNextDelayedNonNestableTask();
initial.commitd7cae122008-07-26 21:49:38425
[email protected]b224f792011-04-20 16:02:23426 // Runs the specified PendingTask.
[email protected]dd1f9fe2011-11-15 23:36:30427 void RunTask(const base::PendingTask& pending_task);
initial.commitd7cae122008-07-26 21:49:38428
[email protected]752578562008-09-07 08:08:29429 // Calls RunTask or queues the pending_task on the deferred task list if it
430 // cannot be run right now. Returns true if the task was run.
[email protected]dd1f9fe2011-11-15 23:36:30431 bool DeferOrRunPendingTask(const base::PendingTask& pending_task);
initial.commitd7cae122008-07-26 21:49:38432
[email protected]001747c2008-09-10 00:37:07433 // Adds the pending task to delayed_work_queue_.
[email protected]dd1f9fe2011-11-15 23:36:30434 void AddToDelayedWorkQueue(const base::PendingTask& pending_task);
[email protected]001747c2008-09-10 00:37:07435
[email protected]b224f792011-04-20 16:02:23436 // Adds the pending task to our incoming_queue_.
437 //
438 // Caller retains ownership of |pending_task|, but this function will
439 // reset the value of pending_task->task. This is needed to ensure
440 // that the posting call stack does not retain pending_task->task
441 // beyond this function call.
[email protected]dd1f9fe2011-11-15 23:36:30442 void AddToIncomingQueue(base::PendingTask* pending_task);
[email protected]b224f792011-04-20 16:02:23443
initial.commitd7cae122008-07-26 21:49:38444 // Load tasks from the incoming_queue_ into work_queue_ if the latter is
445 // empty. The former requires a lock to access, while the latter is directly
446 // accessible on this thread.
447 void ReloadWorkQueue();
448
449 // Delete tasks that haven't run yet without running them. Used in the
[email protected]001747c2008-09-10 00:37:07450 // destructor to make sure all the task's destructors get called. Returns
451 // true if some work was done.
452 bool DeletePendingTasks();
initial.commitd7cae122008-07-26 21:49:38453
[email protected]dd1f9fe2011-11-15 23:36:30454 // Calculates the time at which a PendingTask should run.
[email protected]b224f792011-04-20 16:02:23455 base::TimeTicks CalculateDelayedRuntime(int64 delay_ms);
[email protected]fc7fb6e2008-08-16 03:09:05456
initial.commitd7cae122008-07-26 21:49:38457 // Start recording histogram info about events and action IF it was enabled
458 // and IF the statistics recorder can accept a registration of our histogram.
459 void StartHistogrammer();
460
[email protected]dd1f9fe2011-11-15 23:36:30461 // Add occurrence of event to our histogram, so that we can see what is being
initial.commitd7cae122008-07-26 21:49:38462 // done in a specific MessageLoop instance (i.e., specific thread).
463 // If message_histogram_ is NULL, this is a no-op.
464 void HistogramEvent(int event);
465
[email protected]a502bbe72011-01-07 18:06:45466 // base::MessagePump::Delegate methods:
[email protected]b1719462011-11-16 00:08:08467 virtual bool DoWork() OVERRIDE;
468 virtual bool DoDelayedWork(base::TimeTicks* next_delayed_work_time) OVERRIDE;
469 virtual bool DoIdleWork() OVERRIDE;
[email protected]a502bbe72011-01-07 18:06:45470
[email protected]4d9bdfaf2008-08-26 05:53:57471 Type type_;
472
[email protected]752578562008-09-07 08:08:29473 // A list of tasks that need to be processed by this instance. Note that
474 // this queue is only accessed (push/pop) by our current thread.
[email protected]dd1f9fe2011-11-15 23:36:30475 base::TaskQueue work_queue_;
[email protected]b0992172008-10-27 18:54:18476
[email protected]752578562008-09-07 08:08:29477 // Contains delayed tasks, sorted by their 'delayed_run_time' property.
[email protected]262060ff2011-11-17 23:26:53478 base::DelayedTaskQueue delayed_work_queue_;
initial.commitd7cae122008-07-26 21:49:38479
[email protected]a8f7d3d2010-11-04 23:23:42480 // A recent snapshot of Time::Now(), used to check delayed_work_queue_.
[email protected]7e7fab42010-11-06 22:23:29481 base::TimeTicks recent_time_;
[email protected]a8f7d3d2010-11-04 23:23:42482
[email protected]752578562008-09-07 08:08:29483 // A queue of non-nestable tasks that we had to defer because when it came
484 // time to execute them we were in a nested message loop. They will execute
485 // once we're out of nested message loops.
[email protected]dd1f9fe2011-11-15 23:36:30486 base::TaskQueue deferred_non_nestable_work_queue_;
initial.commitd7cae122008-07-26 21:49:38487
[email protected]fc7fb6e2008-08-16 03:09:05488 scoped_refptr<base::MessagePump> pump_;
[email protected]ee132ad2008-08-06 21:27:02489
[email protected]2a127252008-08-05 23:16:41490 ObserverList<DestructionObserver> destruction_observers_;
[email protected]001747c2008-09-10 00:37:07491
[email protected]dd1f9fe2011-11-15 23:36:30492 // A recursion block that prevents accidentally running additional tasks when
initial.commitd7cae122008-07-26 21:49:38493 // insider a (accidentally induced?) nested message pump.
494 bool nestable_tasks_allowed_;
495
496 bool exception_restoration_;
497
initial.commitd7cae122008-07-26 21:49:38498 std::string thread_name_;
499 // A profiling histogram showing the counts of various messages and events.
[email protected]81ce9f3b2011-04-05 04:48:53500 base::Histogram* message_histogram_;
initial.commitd7cae122008-07-26 21:49:38501
502 // A null terminated list which creates an incoming_queue of tasks that are
[email protected]242c4bd2011-02-25 18:43:23503 // acquired under a mutex for processing on this instance's thread. These
[email protected]b224f792011-04-20 16:02:23504 // tasks have not yet been sorted out into items for our work_queue_ vs items
505 // that will be handled by the TimerManager.
[email protected]dd1f9fe2011-11-15 23:36:30506 base::TaskQueue incoming_queue_;
initial.commitd7cae122008-07-26 21:49:38507 // Protect access to incoming_queue_.
[email protected]8d6ab8f52011-01-26 00:53:48508 mutable base::Lock incoming_queue_lock_;
initial.commitd7cae122008-07-26 21:49:38509
[email protected]fc7fb6e2008-08-16 03:09:05510 RunState* state_;
initial.commitd7cae122008-07-26 21:49:38511
[email protected]b224f792011-04-20 16:02:23512 // The need for this variable is subtle. Please see implementation comments
513 // around where it is used.
514 bool should_leak_tasks_;
515
[email protected]57f030a2010-06-29 04:58:15516#if defined(OS_WIN)
517 base::TimeTicks high_resolution_timer_expiration_;
[email protected]2ec01fe2011-03-24 03:40:28518 // Should be set to true before calling Windows APIs like TrackPopupMenu, etc
519 // which enter a modal message loop.
520 bool os_modal_loop_;
[email protected]57f030a2010-06-29 04:58:15521#endif
522
[email protected]752578562008-09-07 08:08:29523 // The next sequence number to use for delayed tasks.
524 int next_sequence_num_;
525
[email protected]9cfb89a2010-06-09 21:20:41526 ObserverList<TaskObserver> task_observers_;
527
[email protected]edd685f2011-08-15 20:33:46528 // The message loop proxy associated with this message loop, if one exists.
529 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_;
530
[email protected]26fbf802011-03-25 18:48:03531 private:
[email protected]b1d183282011-12-30 04:32:58532 template <class T, class R> friend class base::subtle::DeleteHelperInternal;
[email protected]c29985e2011-12-30 06:46:30533 template <class T, class R> friend class base::subtle::ReleaseHelperInternal;
[email protected]b1d183282011-12-30 04:32:58534
535 void DeleteSoonInternal(const tracked_objects::Location& from_here,
536 void(*deleter)(const void*),
537 const void* object);
[email protected]c29985e2011-12-30 06:46:30538 void ReleaseSoonInternal(const tracked_objects::Location& from_here,
539 void(*releaser)(const void*),
540 const void* object);
541
[email protected]b1d183282011-12-30 04:32:58542
[email protected]fc7fb6e2008-08-16 03:09:05543 DISALLOW_COPY_AND_ASSIGN(MessageLoop);
initial.commitd7cae122008-07-26 21:49:38544};
545
[email protected]4d9bdfaf2008-08-26 05:53:57546//-----------------------------------------------------------------------------
547// MessageLoopForUI extends MessageLoop with methods that are particular to a
548// MessageLoop instantiated with TYPE_UI.
549//
550// This class is typically used like so:
551// MessageLoopForUI::current()->...call some method...
552//
[email protected]0bea7252011-08-05 15:34:00553class BASE_EXPORT MessageLoopForUI : public MessageLoop {
[email protected]4d9bdfaf2008-08-26 05:53:57554 public:
555 MessageLoopForUI() : MessageLoop(TYPE_UI) {
556 }
license.botbf09a502008-08-24 00:55:55557
[email protected]4d9bdfaf2008-08-26 05:53:57558 // Returns the MessageLoopForUI of the current thread.
559 static MessageLoopForUI* current() {
560 MessageLoop* loop = MessageLoop::current();
561 DCHECK_EQ(MessageLoop::TYPE_UI, loop->type());
562 return static_cast<MessageLoopForUI*>(loop);
563 }
564
565#if defined(OS_WIN)
[email protected]4d9bdfaf2008-08-26 05:53:57566 void DidProcessMessage(const MSG& message);
[email protected]9cfb89a2010-06-09 21:20:41567#endif // defined(OS_WIN)
[email protected]1a8f5d1d2008-09-25 20:33:04568
[email protected]61c86c62011-08-02 16:11:16569#if defined(OS_ANDROID)
570 // On Android, the UI message loop is handled by Java side. So Run() should
571 // never be called. Instead use Start(), which will forward all the native UI
572 // events to the Java message loop.
573 void Start();
574#elif !defined(OS_MACOSX)
[email protected]148d1052009-07-31 22:53:37575 // Please see message_pump_win/message_pump_glib for definitions of these
576 // methods.
577 void AddObserver(Observer* observer);
578 void RemoveObserver(Observer* observer);
[email protected]4d6285312011-10-24 07:19:51579 void RunWithDispatcher(Dispatcher* dispatcher);
[email protected]35e9b66a2011-10-06 18:19:21580 void RunAllPendingWithDispatcher(Dispatcher* dispatcher);
[email protected]148d1052009-07-31 22:53:37581
[email protected]1a8f5d1d2008-09-25 20:33:04582 protected:
583 // TODO(rvargas): Make this platform independent.
[email protected]17b89142008-11-07 21:52:15584 base::MessagePumpForUI* pump_ui() {
[email protected]1a8f5d1d2008-09-25 20:33:04585 return static_cast<base::MessagePumpForUI*>(pump_.get());
586 }
[email protected]9cfb89a2010-06-09 21:20:41587#endif // !defined(OS_MACOSX)
[email protected]4d9bdfaf2008-08-26 05:53:57588};
589
590// Do not add any member variables to MessageLoopForUI! This is important b/c
591// MessageLoopForUI is often allocated via MessageLoop(TYPE_UI). Any extra
592// data that you need should be stored on the MessageLoop's pump_ instance.
593COMPILE_ASSERT(sizeof(MessageLoop) == sizeof(MessageLoopForUI),
594 MessageLoopForUI_should_not_have_extra_member_variables);
595
596//-----------------------------------------------------------------------------
597// MessageLoopForIO extends MessageLoop with methods that are particular to a
598// MessageLoop instantiated with TYPE_IO.
599//
600// This class is typically used like so:
601// MessageLoopForIO::current()->...call some method...
602//
[email protected]0bea7252011-08-05 15:34:00603class BASE_EXPORT MessageLoopForIO : public MessageLoop {
[email protected]4d9bdfaf2008-08-26 05:53:57604 public:
[email protected]9cfb89a2010-06-09 21:20:41605#if defined(OS_WIN)
606 typedef base::MessagePumpForIO::IOHandler IOHandler;
607 typedef base::MessagePumpForIO::IOContext IOContext;
608 typedef base::MessagePumpForIO::IOObserver IOObserver;
609#elif defined(OS_POSIX)
610 typedef base::MessagePumpLibevent::Watcher Watcher;
611 typedef base::MessagePumpLibevent::FileDescriptorWatcher
612 FileDescriptorWatcher;
613 typedef base::MessagePumpLibevent::IOObserver IOObserver;
614
615 enum Mode {
616 WATCH_READ = base::MessagePumpLibevent::WATCH_READ,
617 WATCH_WRITE = base::MessagePumpLibevent::WATCH_WRITE,
618 WATCH_READ_WRITE = base::MessagePumpLibevent::WATCH_READ_WRITE
619 };
620
621#endif
622
[email protected]4d9bdfaf2008-08-26 05:53:57623 MessageLoopForIO() : MessageLoop(TYPE_IO) {
624 }
625
626 // Returns the MessageLoopForIO of the current thread.
627 static MessageLoopForIO* current() {
628 MessageLoop* loop = MessageLoop::current();
629 DCHECK_EQ(MessageLoop::TYPE_IO, loop->type());
630 return static_cast<MessageLoopForIO*>(loop);
631 }
632
[email protected]9cfb89a2010-06-09 21:20:41633 void AddIOObserver(IOObserver* io_observer) {
634 pump_io()->AddIOObserver(io_observer);
635 }
[email protected]941281482010-06-09 05:10:48636
[email protected]9cfb89a2010-06-09 21:20:41637 void RemoveIOObserver(IOObserver* io_observer) {
638 pump_io()->RemoveIOObserver(io_observer);
639 }
640
641#if defined(OS_WIN)
[email protected]4d9bdfaf2008-08-26 05:53:57642 // Please see MessagePumpWin for definitions of these methods.
[email protected]32cda29d2008-10-09 23:58:43643 void RegisterIOHandler(HANDLE file_handle, IOHandler* handler);
[email protected]17b89142008-11-07 21:52:15644 bool WaitForIOCompletion(DWORD timeout, IOHandler* filter);
[email protected]36987e92008-09-18 18:46:26645
[email protected]1a8f5d1d2008-09-25 20:33:04646 protected:
647 // TODO(rvargas): Make this platform independent.
648 base::MessagePumpForIO* pump_io() {
649 return static_cast<base::MessagePumpForIO*>(pump_.get());
650 }
651
[email protected]36987e92008-09-18 18:46:26652#elif defined(OS_POSIX)
[email protected]e45e6c02008-12-15 22:02:17653 // Please see MessagePumpLibevent for definition.
654 bool WatchFileDescriptor(int fd,
655 bool persistent,
656 Mode mode,
[email protected]320eff42011-11-15 00:29:48657 FileDescriptorWatcher* controller,
658 Watcher* delegate);
[email protected]9cfb89a2010-06-09 21:20:41659
660 private:
661 base::MessagePumpLibevent* pump_io() {
662 return static_cast<base::MessagePumpLibevent*>(pump_.get());
663 }
[email protected]1a8f5d1d2008-09-25 20:33:04664#endif // defined(OS_POSIX)
[email protected]4d9bdfaf2008-08-26 05:53:57665};
666
667// Do not add any member variables to MessageLoopForIO! This is important b/c
668// MessageLoopForIO is often allocated via MessageLoop(TYPE_IO). Any extra
669// data that you need should be stored on the MessageLoop's pump_ instance.
670COMPILE_ASSERT(sizeof(MessageLoop) == sizeof(MessageLoopForIO),
671 MessageLoopForIO_should_not_have_extra_member_variables);
672
673#endif // BASE_MESSAGE_LOOP_H_