blob: e52963f2533058e9e08d258c98a12a78fe98d6e6 [file] [log] [blame]
brettw@chromium.org6318b392013-06-14 12:27:49 +09001// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_
6#define BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_
7
8#include <queue>
9#include <string>
10
11#include "base/base_export.h"
12#include "base/basictypes.h"
13#include "base/callback_forward.h"
14#include "base/location.h"
15#include "base/memory/ref_counted.h"
alexeypa@chromium.org40183232013-07-23 07:24:13 +090016#include "base/memory/scoped_ptr.h"
17#include "base/message_loop/incoming_task_queue.h"
brettw@chromium.org6318b392013-06-14 12:27:49 +090018#include "base/message_loop/message_loop_proxy.h"
alexeypa@chromium.org40183232013-07-23 07:24:13 +090019#include "base/message_loop/message_loop_proxy_impl.h"
brettw@chromium.org710ecb92013-06-19 05:27:52 +090020#include "base/message_loop/message_pump.h"
brettw@chromium.org6318b392013-06-14 12:27:49 +090021#include "base/observer_list.h"
22#include "base/pending_task.h"
23#include "base/sequenced_task_runner_helpers.h"
24#include "base/synchronization/lock.h"
avi@chromium.orgb039e8b2013-06-28 09:49:07 +090025#include "base/time/time.h"
brettw@chromium.org6318b392013-06-14 12:27:49 +090026#include "base/tracking_info.h"
brettw@chromium.org6318b392013-06-14 12:27:49 +090027
sky@chromium.org66a00b32014-01-17 09:10:29 +090028// TODO(sky): these includes should not be necessary. Nuke them.
brettw@chromium.org6318b392013-06-14 12:27:49 +090029#if defined(OS_WIN)
brettw@chromium.org710ecb92013-06-19 05:27:52 +090030#include "base/message_loop/message_pump_win.h"
brettw@chromium.org6318b392013-06-14 12:27:49 +090031#elif defined(OS_IOS)
brettw@chromium.org710ecb92013-06-19 05:27:52 +090032#include "base/message_loop/message_pump_io_ios.h"
brettw@chromium.org6318b392013-06-14 12:27:49 +090033#elif defined(OS_POSIX)
brettw@chromium.org710ecb92013-06-19 05:27:52 +090034#include "base/message_loop/message_pump_libevent.h"
brettw@chromium.org6318b392013-06-14 12:27:49 +090035#if !defined(OS_MACOSX) && !defined(OS_ANDROID)
36
37#if defined(USE_AURA) && defined(USE_X11) && !defined(OS_NACL)
sadrul@chromium.org19995722013-09-07 12:21:04 +090038#include "base/message_loop/message_pump_x11.h"
brettw@chromium.org6318b392013-06-14 12:27:49 +090039#elif defined(USE_OZONE) && !defined(OS_NACL)
brettw@chromium.org710ecb92013-06-19 05:27:52 +090040#include "base/message_loop/message_pump_ozone.h"
zhenyu.liang@intel.comd4ad4362014-03-19 14:47:01 +090041#elif !defined(OS_ANDROID_HOST)
sadrul@chromium.orga06ba832013-09-07 10:13:39 +090042#define USE_GTK_MESSAGE_PUMP
brettw@chromium.org710ecb92013-06-19 05:27:52 +090043#include "base/message_loop/message_pump_gtk.h"
ccameron@chromium.orgbfe60072013-09-13 07:51:10 +090044#if defined(TOOLKIT_GTK)
45#include "base/message_loop/message_pump_x11.h"
46#endif
brettw@chromium.org6318b392013-06-14 12:27:49 +090047#endif
48
49#endif
50#endif
51
52namespace base {
brettw@chromium.org710ecb92013-06-19 05:27:52 +090053
brettw@chromium.org6318b392013-06-14 12:27:49 +090054class HistogramBase;
sadrul@chromium.orga06ba832013-09-07 10:13:39 +090055class MessagePumpObserver;
brettw@chromium.org6318b392013-06-14 12:27:49 +090056class RunLoop;
57class ThreadTaskRunnerHandle;
58#if defined(OS_ANDROID)
59class MessagePumpForUI;
zhenyu.liang@intel.comd4ad4362014-03-19 14:47:01 +090060#elif defined(OS_ANDROID_HOST)
61typedef MessagePumpLibevent MessagePumpForUI;
brettw@chromium.org6318b392013-06-14 12:27:49 +090062#endif
alexeypa@chromium.org40183232013-07-23 07:24:13 +090063class WaitableEvent;
brettw@chromium.org6318b392013-06-14 12:27:49 +090064
65// A MessageLoop is used to process events for a particular thread. There is
66// at most one MessageLoop instance per thread.
67//
68// Events include at a minimum Task instances submitted to PostTask and its
69// variants. Depending on the type of message pump used by the MessageLoop
70// other events such as UI messages may be processed. On Windows APC calls (as
71// time permits) and signals sent to a registered set of HANDLEs may also be
72// processed.
73//
74// NOTE: Unless otherwise specified, a MessageLoop's methods may only be called
75// on the thread where the MessageLoop's Run method executes.
76//
77// NOTE: MessageLoop has task reentrancy protection. This means that if a
78// task is being processed, a second task cannot start until the first task is
79// finished. Reentrancy can happen when processing a task, and an inner
80// message pump is created. That inner pump then processes native messages
81// which could implicitly start an inner task. Inner message pumps are created
82// with dialogs (DialogBox), common dialogs (GetOpenFileName), OLE functions
83// (DoDragDrop), printer functions (StartDoc) and *many* others.
84//
85// Sample workaround when inner task processing is needed:
86// HRESULT hr;
87// {
88// MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current());
89// hr = DoDragDrop(...); // Implicitly runs a modal message loop.
90// }
91// // Process |hr| (the result returned by DoDragDrop()).
92//
93// Please be SURE your task is reentrant (nestable) and all global variables
94// are stable and accessible before calling SetNestableTasksAllowed(true).
95//
brettw@chromium.org710ecb92013-06-19 05:27:52 +090096class BASE_EXPORT MessageLoop : public MessagePump::Delegate {
brettw@chromium.org6318b392013-06-14 12:27:49 +090097 public:
98
ccameron@chromium.org6c63e292014-01-08 06:42:02 +090099#if defined(USE_AURA)
brettw@chromium.org710ecb92013-06-19 05:27:52 +0900100 typedef MessagePumpObserver Observer;
ccameron@chromium.org6c63e292014-01-08 06:42:02 +0900101#elif defined(USE_GTK_MESSAGE_PUMP)
102 typedef MessagePumpGdkObserver Observer;
brettw@chromium.org6318b392013-06-14 12:27:49 +0900103#endif
104
105 // A MessageLoop has a particular type, which indicates the set of
106 // asynchronous events it may process in addition to tasks and timers.
107 //
108 // TYPE_DEFAULT
109 // This type of ML only supports tasks and timers.
110 //
111 // TYPE_UI
112 // This type of ML also supports native UI events (e.g., Windows messages).
113 // See also MessageLoopForUI.
114 //
ccameron@chromium.orgbfe60072013-09-13 07:51:10 +0900115 // TYPE_GPU
116 // This type of ML also supports native UI events for use in the GPU
117 // process. On Linux this will always be an X11 ML (as compared with the
118 // sometimes-GTK ML in the browser process).
119 //
brettw@chromium.org6318b392013-06-14 12:27:49 +0900120 // TYPE_IO
121 // This type of ML also supports asynchronous IO. See also
122 // MessageLoopForIO.
123 //
kristianm@chromium.orga78bfa92013-08-08 10:31:52 +0900124 // TYPE_JAVA
125 // This type of ML is backed by a Java message handler which is responsible
126 // for running the tasks added to the ML. This is only for use on Android.
127 // TYPE_JAVA behaves in essence like TYPE_UI, except during construction
128 // where it does not use the main thread specific pump factory.
129 //
sky@chromium.orgab452802013-11-08 15:16:53 +0900130 // TYPE_CUSTOM
131 // MessagePump was supplied to constructor.
132 //
brettw@chromium.org6318b392013-06-14 12:27:49 +0900133 enum Type {
134 TYPE_DEFAULT,
135 TYPE_UI,
sky@chromium.orgab452802013-11-08 15:16:53 +0900136 TYPE_CUSTOM,
ccameron@chromium.orgbfe60072013-09-13 07:51:10 +0900137#if defined(TOOLKIT_GTK)
138 TYPE_GPU,
139#endif
kristianm@chromium.orga78bfa92013-08-08 10:31:52 +0900140 TYPE_IO,
141#if defined(OS_ANDROID)
142 TYPE_JAVA,
143#endif // defined(OS_ANDROID)
brettw@chromium.org6318b392013-06-14 12:27:49 +0900144 };
145
146 // Normally, it is not necessary to instantiate a MessageLoop. Instead, it
147 // is typical to make use of the current thread's MessageLoop instance.
148 explicit MessageLoop(Type type = TYPE_DEFAULT);
sky@chromium.orgab452802013-11-08 15:16:53 +0900149 // Creates a TYPE_CUSTOM MessageLoop with the supplied MessagePump, which must
150 // be non-NULL.
151 explicit MessageLoop(scoped_ptr<base::MessagePump> pump);
brettw@chromium.org6318b392013-06-14 12:27:49 +0900152 virtual ~MessageLoop();
153
154 // Returns the MessageLoop object for the current thread, or null if none.
155 static MessageLoop* current();
156
157 static void EnableHistogrammer(bool enable_histogrammer);
158
suyash.s@samsung.comb5f1fc92014-03-08 02:03:54 +0900159 typedef scoped_ptr<MessagePump> (MessagePumpFactory)();
brettw@chromium.org6318b392013-06-14 12:27:49 +0900160 // Uses the given base::MessagePumpForUIFactory to override the default
161 // MessagePump implementation for 'TYPE_UI'. Returns true if the factory
162 // was successfully registered.
163 static bool InitMessagePumpForUIFactory(MessagePumpFactory* factory);
164
sky@chromium.org4f426822013-11-13 01:35:02 +0900165 // Creates the default MessagePump based on |type|. Caller owns return
166 // value.
suyash.s@samsung.comb5f1fc92014-03-08 02:03:54 +0900167 static scoped_ptr<MessagePump> CreateMessagePumpForType(Type type);
brettw@chromium.org6318b392013-06-14 12:27:49 +0900168 // A DestructionObserver is notified when the current MessageLoop is being
169 // destroyed. These observers are notified prior to MessageLoop::current()
170 // being changed to return NULL. This gives interested parties the chance to
171 // do final cleanup that depends on the MessageLoop.
172 //
173 // NOTE: Any tasks posted to the MessageLoop during this notification will
174 // not be run. Instead, they will be deleted.
175 //
176 class BASE_EXPORT DestructionObserver {
177 public:
178 virtual void WillDestroyCurrentMessageLoop() = 0;
179
180 protected:
181 virtual ~DestructionObserver();
182 };
183
184 // Add a DestructionObserver, which will start receiving notifications
185 // immediately.
186 void AddDestructionObserver(DestructionObserver* destruction_observer);
187
188 // Remove a DestructionObserver. It is safe to call this method while a
189 // DestructionObserver is receiving a notification callback.
190 void RemoveDestructionObserver(DestructionObserver* destruction_observer);
191
192 // The "PostTask" family of methods call the task's Run method asynchronously
193 // from within a message loop at some point in the future.
194 //
195 // With the PostTask variant, tasks are invoked in FIFO order, inter-mixed
196 // with normal UI or IO event processing. With the PostDelayedTask variant,
197 // tasks are called after at least approximately 'delay_ms' have elapsed.
198 //
199 // The NonNestable variants work similarly except that they promise never to
200 // dispatch the task from a nested invocation of MessageLoop::Run. Instead,
201 // such tasks get deferred until the top-most MessageLoop::Run is executing.
202 //
203 // The MessageLoop takes ownership of the Task, and deletes it after it has
204 // been Run().
205 //
206 // PostTask(from_here, task) is equivalent to
207 // PostDelayedTask(from_here, task, 0).
208 //
brettw@chromium.org6318b392013-06-14 12:27:49 +0900209 // NOTE: These methods may be called on any thread. The Task will be invoked
210 // on the thread that executes MessageLoop::Run().
brettw@chromium.org710ecb92013-06-19 05:27:52 +0900211 void PostTask(const tracked_objects::Location& from_here,
212 const Closure& task);
brettw@chromium.org6318b392013-06-14 12:27:49 +0900213
brettw@chromium.org710ecb92013-06-19 05:27:52 +0900214 void PostDelayedTask(const tracked_objects::Location& from_here,
215 const Closure& task,
216 TimeDelta delay);
brettw@chromium.org6318b392013-06-14 12:27:49 +0900217
brettw@chromium.org710ecb92013-06-19 05:27:52 +0900218 void PostNonNestableTask(const tracked_objects::Location& from_here,
219 const Closure& task);
brettw@chromium.org6318b392013-06-14 12:27:49 +0900220
brettw@chromium.org710ecb92013-06-19 05:27:52 +0900221 void PostNonNestableDelayedTask(const tracked_objects::Location& from_here,
222 const Closure& task,
223 TimeDelta delay);
brettw@chromium.org6318b392013-06-14 12:27:49 +0900224
225 // A variant on PostTask that deletes the given object. This is useful
226 // if the object needs to live until the next run of the MessageLoop (for
227 // example, deleting a RenderProcessHost from within an IPC callback is not
228 // good).
229 //
230 // NOTE: This method may be called on any thread. The object will be deleted
231 // on the thread that executes MessageLoop::Run(). If this is not the same
232 // as the thread that calls PostDelayedTask(FROM_HERE, ), then T MUST inherit
233 // from RefCountedThreadSafe<T>!
234 template <class T>
235 void DeleteSoon(const tracked_objects::Location& from_here, const T* object) {
236 base::subtle::DeleteHelperInternal<T, void>::DeleteViaSequencedTaskRunner(
237 this, from_here, object);
238 }
239
240 // A variant on PostTask that releases the given reference counted object
241 // (by calling its Release method). This is useful if the object needs to
242 // live until the next run of the MessageLoop, or if the object needs to be
243 // released on a particular thread.
244 //
245 // NOTE: This method may be called on any thread. The object will be
246 // released (and thus possibly deleted) on the thread that executes
247 // MessageLoop::Run(). If this is not the same as the thread that calls
248 // PostDelayedTask(FROM_HERE, ), then T MUST inherit from
249 // RefCountedThreadSafe<T>!
250 template <class T>
251 void ReleaseSoon(const tracked_objects::Location& from_here,
252 const T* object) {
253 base::subtle::ReleaseHelperInternal<T, void>::ReleaseViaSequencedTaskRunner(
254 this, from_here, object);
255 }
256
257 // Deprecated: use RunLoop instead.
258 // Run the message loop.
259 void Run();
260
261 // Deprecated: use RunLoop instead.
262 // Process all pending tasks, windows messages, etc., but don't wait/sleep.
263 // Return as soon as all items that can be run are taken care of.
264 void RunUntilIdle();
265
266 // TODO(jbates) remove this. crbug.com/131220. See QuitWhenIdle().
267 void Quit() { QuitWhenIdle(); }
268
269 // Deprecated: use RunLoop instead.
270 //
271 // Signals the Run method to return when it becomes idle. It will continue to
272 // process pending messages and future messages as long as they are enqueued.
273 // Warning: if the MessageLoop remains busy, it may never quit. Only use this
274 // Quit method when looping procedures (such as web pages) have been shut
275 // down.
276 //
277 // This method may only be called on the same thread that called Run, and Run
278 // must still be on the call stack.
279 //
280 // Use QuitClosure variants if you need to Quit another thread's MessageLoop,
281 // but note that doing so is fairly dangerous if the target thread makes
282 // nested calls to MessageLoop::Run. The problem being that you won't know
283 // which nested run loop you are quitting, so be careful!
284 void QuitWhenIdle();
285
286 // Deprecated: use RunLoop instead.
287 //
288 // This method is a variant of Quit, that does not wait for pending messages
289 // to be processed before returning from Run.
290 void QuitNow();
291
292 // TODO(jbates) remove this. crbug.com/131220. See QuitWhenIdleClosure().
brettw@chromium.org710ecb92013-06-19 05:27:52 +0900293 static Closure QuitClosure() { return QuitWhenIdleClosure(); }
brettw@chromium.org6318b392013-06-14 12:27:49 +0900294
295 // Deprecated: use RunLoop instead.
296 // Construct a Closure that will call QuitWhenIdle(). Useful to schedule an
297 // arbitrary MessageLoop to QuitWhenIdle.
brettw@chromium.org710ecb92013-06-19 05:27:52 +0900298 static Closure QuitWhenIdleClosure();
brettw@chromium.org6318b392013-06-14 12:27:49 +0900299
300 // Returns true if this loop is |type|. This allows subclasses (especially
301 // those in tests) to specialize how they are identified.
302 virtual bool IsType(Type type) const;
303
304 // Returns the type passed to the constructor.
305 Type type() const { return type_; }
306
307 // Optional call to connect the thread name with this loop.
308 void set_thread_name(const std::string& thread_name) {
309 DCHECK(thread_name_.empty()) << "Should not rename this thread!";
310 thread_name_ = thread_name;
311 }
312 const std::string& thread_name() const { return thread_name_; }
313
314 // Gets the message loop proxy associated with this message loop.
brettw@chromium.org710ecb92013-06-19 05:27:52 +0900315 scoped_refptr<MessageLoopProxy> message_loop_proxy() {
alexeypa@chromium.org40183232013-07-23 07:24:13 +0900316 return message_loop_proxy_;
brettw@chromium.org6318b392013-06-14 12:27:49 +0900317 }
318
319 // Enables or disables the recursive task processing. This happens in the case
320 // of recursive message loops. Some unwanted message loop may occurs when
321 // using common controls or printer functions. By default, recursive task
322 // processing is disabled.
323 //
324 // Please utilize |ScopedNestableTaskAllower| instead of calling these methods
325 // directly. In general nestable message loops are to be avoided. They are
326 // dangerous and difficult to get right, so please use with extreme caution.
327 //
328 // The specific case where tasks get queued is:
329 // - The thread is running a message loop.
330 // - It receives a task #1 and execute it.
331 // - The task #1 implicitly start a message loop, like a MessageBox in the
332 // unit test. This can also be StartDoc or GetSaveFileName.
333 // - The thread receives a task #2 before or while in this second message
334 // loop.
335 // - With NestableTasksAllowed set to true, the task #2 will run right away.
336 // Otherwise, it will get executed right after task #1 completes at "thread
337 // message loop level".
338 void SetNestableTasksAllowed(bool allowed);
339 bool NestableTasksAllowed() const;
340
341 // Enables nestable tasks on |loop| while in scope.
342 class ScopedNestableTaskAllower {
343 public:
344 explicit ScopedNestableTaskAllower(MessageLoop* loop)
345 : loop_(loop),
346 old_state_(loop_->NestableTasksAllowed()) {
347 loop_->SetNestableTasksAllowed(true);
348 }
349 ~ScopedNestableTaskAllower() {
350 loop_->SetNestableTasksAllowed(old_state_);
351 }
352
353 private:
354 MessageLoop* loop_;
355 bool old_state_;
356 };
357
brettw@chromium.org6318b392013-06-14 12:27:49 +0900358 // Returns true if we are currently running a nested message loop.
359 bool IsNested();
360
361 // A TaskObserver is an object that receives task notifications from the
362 // MessageLoop.
363 //
364 // NOTE: A TaskObserver implementation should be extremely fast!
365 class BASE_EXPORT TaskObserver {
366 public:
367 TaskObserver();
368
369 // This method is called before processing a task.
brettw@chromium.org710ecb92013-06-19 05:27:52 +0900370 virtual void WillProcessTask(const PendingTask& pending_task) = 0;
brettw@chromium.org6318b392013-06-14 12:27:49 +0900371
372 // This method is called after processing a task.
brettw@chromium.org710ecb92013-06-19 05:27:52 +0900373 virtual void DidProcessTask(const PendingTask& pending_task) = 0;
brettw@chromium.org6318b392013-06-14 12:27:49 +0900374
375 protected:
376 virtual ~TaskObserver();
377 };
378
379 // These functions can only be called on the same thread that |this| is
380 // running on.
381 void AddTaskObserver(TaskObserver* task_observer);
382 void RemoveTaskObserver(TaskObserver* task_observer);
383
brettw@chromium.org6318b392013-06-14 12:27:49 +0900384 // When we go into high resolution timer mode, we will stay in hi-res mode
385 // for at least 1s.
386 static const int kHighResolutionTimerModeLeaseTimeMs = 1000;
387
brettw@chromium.org6318b392013-06-14 12:27:49 +0900388#if defined(OS_WIN)
389 void set_os_modal_loop(bool os_modal_loop) {
390 os_modal_loop_ = os_modal_loop;
391 }
392
393 bool os_modal_loop() const {
394 return os_modal_loop_;
395 }
396#endif // OS_WIN
397
398 // Can only be called from the thread that owns the MessageLoop.
399 bool is_running() const;
400
alexeypa@chromium.org40183232013-07-23 07:24:13 +0900401 // Returns true if the message loop has high resolution timers enabled.
402 // Provided for testing.
403 bool IsHighResolutionTimerEnabledForTesting();
404
405 // Returns true if the message loop is "idle". Provided for testing.
406 bool IsIdleForTesting();
407
brettw@chromium.org6318b392013-06-14 12:27:49 +0900408 //----------------------------------------------------------------------------
409 protected:
410
411#if defined(OS_WIN)
brettw@chromium.org710ecb92013-06-19 05:27:52 +0900412 MessagePumpWin* pump_win() {
413 return static_cast<MessagePumpWin*>(pump_.get());
brettw@chromium.org6318b392013-06-14 12:27:49 +0900414 }
415#elif defined(OS_POSIX) && !defined(OS_IOS)
brettw@chromium.org710ecb92013-06-19 05:27:52 +0900416 MessagePumpLibevent* pump_libevent() {
417 return static_cast<MessagePumpLibevent*>(pump_.get());
brettw@chromium.org6318b392013-06-14 12:27:49 +0900418 }
ccameron@chromium.orgbfe60072013-09-13 07:51:10 +0900419#if defined(TOOLKIT_GTK)
420 friend class MessagePumpX11;
421 MessagePumpX11* pump_gpu() {
422 DCHECK_EQ(TYPE_GPU, type());
423 return static_cast<MessagePumpX11*>(pump_.get());
424 }
425#endif
brettw@chromium.org6318b392013-06-14 12:27:49 +0900426#endif
427
alexeypa@chromium.org40183232013-07-23 07:24:13 +0900428 scoped_ptr<MessagePump> pump_;
brettw@chromium.org6318b392013-06-14 12:27:49 +0900429
430 private:
alexeypa@chromium.org40183232013-07-23 07:24:13 +0900431 friend class internal::IncomingTaskQueue;
brettw@chromium.org710ecb92013-06-19 05:27:52 +0900432 friend class RunLoop;
brettw@chromium.org6318b392013-06-14 12:27:49 +0900433
sky@chromium.orgab452802013-11-08 15:16:53 +0900434 // Configures various members for the two constructors.
435 void Init();
436
cpu@chromium.org33353ba2014-01-04 06:25:26 +0900437 // Invokes the actual run loop using the message pump.
brettw@chromium.org6318b392013-06-14 12:27:49 +0900438 void RunHandler();
439
brettw@chromium.org6318b392013-06-14 12:27:49 +0900440 // Called to process any delayed non-nestable tasks.
441 bool ProcessNextDelayedNonNestableTask();
442
443 // Runs the specified PendingTask.
brettw@chromium.org710ecb92013-06-19 05:27:52 +0900444 void RunTask(const PendingTask& pending_task);
brettw@chromium.org6318b392013-06-14 12:27:49 +0900445
446 // Calls RunTask or queues the pending_task on the deferred task list if it
447 // cannot be run right now. Returns true if the task was run.
brettw@chromium.org710ecb92013-06-19 05:27:52 +0900448 bool DeferOrRunPendingTask(const PendingTask& pending_task);
brettw@chromium.org6318b392013-06-14 12:27:49 +0900449
450 // Adds the pending task to delayed_work_queue_.
brettw@chromium.org710ecb92013-06-19 05:27:52 +0900451 void AddToDelayedWorkQueue(const PendingTask& pending_task);
brettw@chromium.org6318b392013-06-14 12:27:49 +0900452
brettw@chromium.org6318b392013-06-14 12:27:49 +0900453 // Delete tasks that haven't run yet without running them. Used in the
454 // destructor to make sure all the task's destructors get called. Returns
455 // true if some work was done.
456 bool DeletePendingTasks();
457
alexeypa@chromium.org40183232013-07-23 07:24:13 +0900458 // Creates a process-wide unique ID to represent this task in trace events.
459 // This will be mangled with a Process ID hash to reduce the likelyhood of
460 // colliding with MessageLoop pointers on other processes.
461 uint64 GetTaskTraceID(const PendingTask& task);
462
463 // Loads tasks from the incoming queue to |work_queue_| if the latter is
464 // empty.
465 void ReloadWorkQueue();
466
467 // Wakes up the message pump. Can be called on any thread. The caller is
468 // responsible for synchronizing ScheduleWork() calls.
469 void ScheduleWork(bool was_empty);
brettw@chromium.org6318b392013-06-14 12:27:49 +0900470
471 // Start recording histogram info about events and action IF it was enabled
472 // and IF the statistics recorder can accept a registration of our histogram.
473 void StartHistogrammer();
474
475 // Add occurrence of event to our histogram, so that we can see what is being
476 // done in a specific MessageLoop instance (i.e., specific thread).
477 // If message_histogram_ is NULL, this is a no-op.
478 void HistogramEvent(int event);
479
brettw@chromium.org710ecb92013-06-19 05:27:52 +0900480 // MessagePump::Delegate methods:
brettw@chromium.org6318b392013-06-14 12:27:49 +0900481 virtual bool DoWork() OVERRIDE;
brettw@chromium.org710ecb92013-06-19 05:27:52 +0900482 virtual bool DoDelayedWork(TimeTicks* next_delayed_work_time) OVERRIDE;
brettw@chromium.org6318b392013-06-14 12:27:49 +0900483 virtual bool DoIdleWork() OVERRIDE;
rsesek@chromium.orgee8420d2013-09-05 23:53:12 +0900484 virtual void GetQueueingInformation(size_t* queue_size,
485 TimeDelta* queueing_delay) OVERRIDE;
brettw@chromium.org6318b392013-06-14 12:27:49 +0900486
sky@chromium.orgab452802013-11-08 15:16:53 +0900487 const Type type_;
brettw@chromium.org6318b392013-06-14 12:27:49 +0900488
489 // A list of tasks that need to be processed by this instance. Note that
490 // this queue is only accessed (push/pop) by our current thread.
brettw@chromium.org710ecb92013-06-19 05:27:52 +0900491 TaskQueue work_queue_;
brettw@chromium.org6318b392013-06-14 12:27:49 +0900492
493 // Contains delayed tasks, sorted by their 'delayed_run_time' property.
brettw@chromium.org710ecb92013-06-19 05:27:52 +0900494 DelayedTaskQueue delayed_work_queue_;
brettw@chromium.org6318b392013-06-14 12:27:49 +0900495
496 // A recent snapshot of Time::Now(), used to check delayed_work_queue_.
brettw@chromium.org710ecb92013-06-19 05:27:52 +0900497 TimeTicks recent_time_;
brettw@chromium.org6318b392013-06-14 12:27:49 +0900498
499 // A queue of non-nestable tasks that we had to defer because when it came
500 // time to execute them we were in a nested message loop. They will execute
501 // once we're out of nested message loops.
brettw@chromium.org710ecb92013-06-19 05:27:52 +0900502 TaskQueue deferred_non_nestable_work_queue_;
brettw@chromium.org6318b392013-06-14 12:27:49 +0900503
504 ObserverList<DestructionObserver> destruction_observers_;
505
506 // A recursion block that prevents accidentally running additional tasks when
507 // insider a (accidentally induced?) nested message pump.
508 bool nestable_tasks_allowed_;
509
alexeypa@google.combb819d62013-07-23 05:06:56 +0900510#if defined(OS_WIN)
alexeypa@google.combb819d62013-07-23 05:06:56 +0900511 // Should be set to true before calling Windows APIs like TrackPopupMenu, etc
512 // which enter a modal message loop.
513 bool os_modal_loop_;
514#endif
515
alexeypa@chromium.org40183232013-07-23 07:24:13 +0900516 std::string thread_name_;
517 // A profiling histogram showing the counts of various messages and events.
518 HistogramBase* message_histogram_;
519
520 RunLoop* run_loop_;
alexeypa@google.combb819d62013-07-23 05:06:56 +0900521
brettw@chromium.org6318b392013-06-14 12:27:49 +0900522 ObserverList<TaskObserver> task_observers_;
523
alexeypa@chromium.org40183232013-07-23 07:24:13 +0900524 scoped_refptr<internal::IncomingTaskQueue> incoming_task_queue_;
525
526 // The message loop proxy associated with this message loop.
527 scoped_refptr<internal::MessageLoopProxyImpl> message_loop_proxy_;
brettw@chromium.org710ecb92013-06-19 05:27:52 +0900528 scoped_ptr<ThreadTaskRunnerHandle> thread_task_runner_handle_;
brettw@chromium.org6318b392013-06-14 12:27:49 +0900529
530 template <class T, class R> friend class base::subtle::DeleteHelperInternal;
531 template <class T, class R> friend class base::subtle::ReleaseHelperInternal;
532
533 void DeleteSoonInternal(const tracked_objects::Location& from_here,
534 void(*deleter)(const void*),
535 const void* object);
536 void ReleaseSoonInternal(const tracked_objects::Location& from_here,
537 void(*releaser)(const void*),
538 const void* object);
539
540 DISALLOW_COPY_AND_ASSIGN(MessageLoop);
541};
542
543//-----------------------------------------------------------------------------
544// MessageLoopForUI extends MessageLoop with methods that are particular to a
545// MessageLoop instantiated with TYPE_UI.
546//
547// This class is typically used like so:
548// MessageLoopForUI::current()->...call some method...
549//
550class BASE_EXPORT MessageLoopForUI : public MessageLoop {
551 public:
brettw@chromium.org6318b392013-06-14 12:27:49 +0900552 MessageLoopForUI() : MessageLoop(TYPE_UI) {
553 }
554
555 // Returns the MessageLoopForUI of the current thread.
556 static MessageLoopForUI* current() {
557 MessageLoop* loop = MessageLoop::current();
558 DCHECK(loop);
559 DCHECK_EQ(MessageLoop::TYPE_UI, loop->type());
560 return static_cast<MessageLoopForUI*>(loop);
561 }
562
sky@chromium.org8a7aae72014-01-20 17:59:52 +0900563 static bool IsCurrent() {
564 MessageLoop* loop = MessageLoop::current();
565 return loop && loop->type() == MessageLoop::TYPE_UI;
566 }
567
brettw@chromium.org6318b392013-06-14 12:27:49 +0900568#if defined(OS_IOS)
569 // On iOS, the main message loop cannot be Run(). Instead call Attach(),
570 // which connects this MessageLoop to the UI thread's CFRunLoop and allows
571 // PostTask() to work.
572 void Attach();
573#endif
574
575#if defined(OS_ANDROID)
576 // On Android, the UI message loop is handled by Java side. So Run() should
577 // never be called. Instead use Start(), which will forward all the native UI
578 // events to the Java message loop.
579 void Start();
sky@chromium.orgf0ef6b62014-01-12 08:12:06 +0900580#endif
brettw@chromium.org6318b392013-06-14 12:27:49 +0900581
sky@chromium.orgf0ef6b62014-01-12 08:12:06 +0900582#if !defined(OS_NACL) && (defined(TOOLKIT_GTK) || defined(USE_OZONE) || \
583 defined(OS_WIN) || defined(USE_X11))
brettw@chromium.org6318b392013-06-14 12:27:49 +0900584 // Please see message_pump_win/message_pump_glib for definitions of these
585 // methods.
586 void AddObserver(Observer* observer);
587 void RemoveObserver(Observer* observer);
sky@chromium.orgf0ef6b62014-01-12 08:12:06 +0900588#endif
brettw@chromium.org6318b392013-06-14 12:27:49 +0900589
brettw@chromium.org6318b392013-06-14 12:27:49 +0900590 protected:
ccameron@chromium.orgbfe60072013-09-13 07:51:10 +0900591#if defined(USE_X11)
sadrul@chromium.org19995722013-09-07 12:21:04 +0900592 friend class MessagePumpX11;
brettw@chromium.org6318b392013-06-14 12:27:49 +0900593#endif
594#if defined(USE_OZONE) && !defined(OS_NACL)
brettw@chromium.org710ecb92013-06-19 05:27:52 +0900595 friend class MessagePumpOzone;
brettw@chromium.org6318b392013-06-14 12:27:49 +0900596#endif
597
sky@chromium.orgf0ef6b62014-01-12 08:12:06 +0900598#if !defined(OS_MACOSX) && !defined(OS_ANDROID)
brettw@chromium.org6318b392013-06-14 12:27:49 +0900599 // TODO(rvargas): Make this platform independent.
brettw@chromium.org710ecb92013-06-19 05:27:52 +0900600 MessagePumpForUI* pump_ui() {
601 return static_cast<MessagePumpForUI*>(pump_.get());
brettw@chromium.org6318b392013-06-14 12:27:49 +0900602 }
sky@chromium.orgf0ef6b62014-01-12 08:12:06 +0900603#endif
brettw@chromium.org6318b392013-06-14 12:27:49 +0900604};
605
606// Do not add any member variables to MessageLoopForUI! This is important b/c
607// MessageLoopForUI is often allocated via MessageLoop(TYPE_UI). Any extra
608// data that you need should be stored on the MessageLoop's pump_ instance.
609COMPILE_ASSERT(sizeof(MessageLoop) == sizeof(MessageLoopForUI),
610 MessageLoopForUI_should_not_have_extra_member_variables);
611
612//-----------------------------------------------------------------------------
613// MessageLoopForIO extends MessageLoop with methods that are particular to a
614// MessageLoop instantiated with TYPE_IO.
615//
616// This class is typically used like so:
617// MessageLoopForIO::current()->...call some method...
618//
619class BASE_EXPORT MessageLoopForIO : public MessageLoop {
620 public:
621#if defined(OS_WIN)
brettw@chromium.org710ecb92013-06-19 05:27:52 +0900622 typedef MessagePumpForIO::IOHandler IOHandler;
623 typedef MessagePumpForIO::IOContext IOContext;
624 typedef MessagePumpForIO::IOObserver IOObserver;
brettw@chromium.org6318b392013-06-14 12:27:49 +0900625#elif defined(OS_IOS)
brettw@chromium.org710ecb92013-06-19 05:27:52 +0900626 typedef MessagePumpIOSForIO::Watcher Watcher;
627 typedef MessagePumpIOSForIO::FileDescriptorWatcher
brettw@chromium.org6318b392013-06-14 12:27:49 +0900628 FileDescriptorWatcher;
brettw@chromium.org710ecb92013-06-19 05:27:52 +0900629 typedef MessagePumpIOSForIO::IOObserver IOObserver;
brettw@chromium.org6318b392013-06-14 12:27:49 +0900630
631 enum Mode {
brettw@chromium.org710ecb92013-06-19 05:27:52 +0900632 WATCH_READ = MessagePumpIOSForIO::WATCH_READ,
633 WATCH_WRITE = MessagePumpIOSForIO::WATCH_WRITE,
634 WATCH_READ_WRITE = MessagePumpIOSForIO::WATCH_READ_WRITE
brettw@chromium.org6318b392013-06-14 12:27:49 +0900635 };
636#elif defined(OS_POSIX)
brettw@chromium.org710ecb92013-06-19 05:27:52 +0900637 typedef MessagePumpLibevent::Watcher Watcher;
638 typedef MessagePumpLibevent::FileDescriptorWatcher
brettw@chromium.org6318b392013-06-14 12:27:49 +0900639 FileDescriptorWatcher;
brettw@chromium.org710ecb92013-06-19 05:27:52 +0900640 typedef MessagePumpLibevent::IOObserver IOObserver;
brettw@chromium.org6318b392013-06-14 12:27:49 +0900641
642 enum Mode {
brettw@chromium.org710ecb92013-06-19 05:27:52 +0900643 WATCH_READ = MessagePumpLibevent::WATCH_READ,
644 WATCH_WRITE = MessagePumpLibevent::WATCH_WRITE,
645 WATCH_READ_WRITE = MessagePumpLibevent::WATCH_READ_WRITE
brettw@chromium.org6318b392013-06-14 12:27:49 +0900646 };
647
648#endif
649
650 MessageLoopForIO() : MessageLoop(TYPE_IO) {
651 }
652
653 // Returns the MessageLoopForIO of the current thread.
654 static MessageLoopForIO* current() {
655 MessageLoop* loop = MessageLoop::current();
656 DCHECK_EQ(MessageLoop::TYPE_IO, loop->type());
657 return static_cast<MessageLoopForIO*>(loop);
658 }
659
sky@chromium.org8a7aae72014-01-20 17:59:52 +0900660 static bool IsCurrent() {
661 MessageLoop* loop = MessageLoop::current();
662 return loop && loop->type() == MessageLoop::TYPE_IO;
663 }
664
brettw@chromium.org6318b392013-06-14 12:27:49 +0900665 void AddIOObserver(IOObserver* io_observer) {
666 pump_io()->AddIOObserver(io_observer);
667 }
668
669 void RemoveIOObserver(IOObserver* io_observer) {
670 pump_io()->RemoveIOObserver(io_observer);
671 }
672
673#if defined(OS_WIN)
674 // Please see MessagePumpWin for definitions of these methods.
675 void RegisterIOHandler(HANDLE file, IOHandler* handler);
676 bool RegisterJobObject(HANDLE job, IOHandler* handler);
677 bool WaitForIOCompletion(DWORD timeout, IOHandler* filter);
678
679 protected:
680 // TODO(rvargas): Make this platform independent.
brettw@chromium.org710ecb92013-06-19 05:27:52 +0900681 MessagePumpForIO* pump_io() {
682 return static_cast<MessagePumpForIO*>(pump_.get());
brettw@chromium.org6318b392013-06-14 12:27:49 +0900683 }
684
685#elif defined(OS_IOS)
686 // Please see MessagePumpIOSForIO for definition.
687 bool WatchFileDescriptor(int fd,
688 bool persistent,
689 Mode mode,
690 FileDescriptorWatcher *controller,
691 Watcher *delegate);
692
693 private:
brettw@chromium.org710ecb92013-06-19 05:27:52 +0900694 MessagePumpIOSForIO* pump_io() {
695 return static_cast<MessagePumpIOSForIO*>(pump_.get());
brettw@chromium.org6318b392013-06-14 12:27:49 +0900696 }
697
698#elif defined(OS_POSIX)
699 // Please see MessagePumpLibevent for definition.
700 bool WatchFileDescriptor(int fd,
701 bool persistent,
702 Mode mode,
703 FileDescriptorWatcher* controller,
704 Watcher* delegate);
705
706 private:
brettw@chromium.org710ecb92013-06-19 05:27:52 +0900707 MessagePumpLibevent* pump_io() {
708 return static_cast<MessagePumpLibevent*>(pump_.get());
brettw@chromium.org6318b392013-06-14 12:27:49 +0900709 }
710#endif // defined(OS_POSIX)
711};
712
713// Do not add any member variables to MessageLoopForIO! This is important b/c
714// MessageLoopForIO is often allocated via MessageLoop(TYPE_IO). Any extra
715// data that you need should be stored on the MessageLoop's pump_ instance.
716COMPILE_ASSERT(sizeof(MessageLoop) == sizeof(MessageLoopForIO),
717 MessageLoopForIO_should_not_have_extra_member_variables);
718
719} // namespace base
720
721#endif // BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_