blob: 165299d53d0fb70220ab97e80c0a043cffbd7a42 [file] [log] [blame]
[email protected]186ced82013-06-14 03:27:491// 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"
[email protected]54aa4f12013-07-22 22:24:1316#include "base/memory/scoped_ptr.h"
17#include "base/message_loop/incoming_task_queue.h"
[email protected]186ced82013-06-14 03:27:4918#include "base/message_loop/message_loop_proxy.h"
[email protected]54aa4f12013-07-22 22:24:1319#include "base/message_loop/message_loop_proxy_impl.h"
[email protected]59e69e742013-06-18 20:27:5220#include "base/message_loop/message_pump.h"
[email protected]186ced82013-06-14 03:27:4921#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"
[email protected]99084f62013-06-28 00:49:0725#include "base/time/time.h"
[email protected]186ced82013-06-14 03:27:4926#include "base/tracking_info.h"
[email protected]186ced82013-06-14 03:27:4927
[email protected]440e3572014-01-17 00:10:2928// TODO(sky): these includes should not be necessary. Nuke them.
[email protected]186ced82013-06-14 03:27:4929#if defined(OS_WIN)
[email protected]59e69e742013-06-18 20:27:5230#include "base/message_loop/message_pump_win.h"
[email protected]186ced82013-06-14 03:27:4931#elif defined(OS_IOS)
[email protected]59e69e742013-06-18 20:27:5232#include "base/message_loop/message_pump_io_ios.h"
[email protected]186ced82013-06-14 03:27:4933#elif defined(OS_POSIX)
[email protected]59e69e742013-06-18 20:27:5234#include "base/message_loop/message_pump_libevent.h"
[email protected]186ced82013-06-14 03:27:4935#endif
36
37namespace base {
[email protected]59e69e742013-06-18 20:27:5238
[email protected]186ced82013-06-14 03:27:4939class HistogramBase;
[email protected]827d38a2013-09-07 01:13:3940class MessagePumpObserver;
[email protected]186ced82013-06-14 03:27:4941class RunLoop;
42class ThreadTaskRunnerHandle;
[email protected]54aa4f12013-07-22 22:24:1343class WaitableEvent;
[email protected]186ced82013-06-14 03:27:4944
45// A MessageLoop is used to process events for a particular thread. There is
46// at most one MessageLoop instance per thread.
47//
48// Events include at a minimum Task instances submitted to PostTask and its
49// variants. Depending on the type of message pump used by the MessageLoop
50// other events such as UI messages may be processed. On Windows APC calls (as
51// time permits) and signals sent to a registered set of HANDLEs may also be
52// processed.
53//
54// NOTE: Unless otherwise specified, a MessageLoop's methods may only be called
55// on the thread where the MessageLoop's Run method executes.
56//
57// NOTE: MessageLoop has task reentrancy protection. This means that if a
58// task is being processed, a second task cannot start until the first task is
59// finished. Reentrancy can happen when processing a task, and an inner
60// message pump is created. That inner pump then processes native messages
61// which could implicitly start an inner task. Inner message pumps are created
62// with dialogs (DialogBox), common dialogs (GetOpenFileName), OLE functions
63// (DoDragDrop), printer functions (StartDoc) and *many* others.
64//
65// Sample workaround when inner task processing is needed:
66// HRESULT hr;
67// {
68// MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current());
69// hr = DoDragDrop(...); // Implicitly runs a modal message loop.
70// }
71// // Process |hr| (the result returned by DoDragDrop()).
72//
73// Please be SURE your task is reentrant (nestable) and all global variables
74// are stable and accessible before calling SetNestableTasksAllowed(true).
75//
[email protected]59e69e742013-06-18 20:27:5276class BASE_EXPORT MessageLoop : public MessagePump::Delegate {
[email protected]186ced82013-06-14 03:27:4977 public:
[email protected]186ced82013-06-14 03:27:4978 // A MessageLoop has a particular type, which indicates the set of
79 // asynchronous events it may process in addition to tasks and timers.
80 //
81 // TYPE_DEFAULT
82 // This type of ML only supports tasks and timers.
83 //
84 // TYPE_UI
85 // This type of ML also supports native UI events (e.g., Windows messages).
86 // See also MessageLoopForUI.
87 //
88 // TYPE_IO
89 // This type of ML also supports asynchronous IO. See also
90 // MessageLoopForIO.
91 //
[email protected]349ad582013-08-08 01:31:5292 // TYPE_JAVA
93 // This type of ML is backed by a Java message handler which is responsible
94 // for running the tasks added to the ML. This is only for use on Android.
95 // TYPE_JAVA behaves in essence like TYPE_UI, except during construction
96 // where it does not use the main thread specific pump factory.
97 //
[email protected]9f0e4f72013-11-08 06:16:5398 // TYPE_CUSTOM
99 // MessagePump was supplied to constructor.
100 //
[email protected]186ced82013-06-14 03:27:49101 enum Type {
102 TYPE_DEFAULT,
103 TYPE_UI,
[email protected]9f0e4f72013-11-08 06:16:53104 TYPE_CUSTOM,
[email protected]349ad582013-08-08 01:31:52105 TYPE_IO,
106#if defined(OS_ANDROID)
107 TYPE_JAVA,
108#endif // defined(OS_ANDROID)
[email protected]186ced82013-06-14 03:27:49109 };
110
111 // Normally, it is not necessary to instantiate a MessageLoop. Instead, it
112 // is typical to make use of the current thread's MessageLoop instance.
113 explicit MessageLoop(Type type = TYPE_DEFAULT);
[email protected]9f0e4f72013-11-08 06:16:53114 // Creates a TYPE_CUSTOM MessageLoop with the supplied MessagePump, which must
115 // be non-NULL.
116 explicit MessageLoop(scoped_ptr<base::MessagePump> pump);
[email protected]186ced82013-06-14 03:27:49117 virtual ~MessageLoop();
118
119 // Returns the MessageLoop object for the current thread, or null if none.
120 static MessageLoop* current();
121
122 static void EnableHistogrammer(bool enable_histogrammer);
123
[email protected]eed33182014-03-07 17:03:54124 typedef scoped_ptr<MessagePump> (MessagePumpFactory)();
[email protected]186ced82013-06-14 03:27:49125 // Uses the given base::MessagePumpForUIFactory to override the default
126 // MessagePump implementation for 'TYPE_UI'. Returns true if the factory
127 // was successfully registered.
128 static bool InitMessagePumpForUIFactory(MessagePumpFactory* factory);
129
[email protected]3fee57b2013-11-12 16:35:02130 // Creates the default MessagePump based on |type|. Caller owns return
131 // value.
[email protected]eed33182014-03-07 17:03:54132 static scoped_ptr<MessagePump> CreateMessagePumpForType(Type type);
[email protected]186ced82013-06-14 03:27:49133 // A DestructionObserver is notified when the current MessageLoop is being
134 // destroyed. These observers are notified prior to MessageLoop::current()
135 // being changed to return NULL. This gives interested parties the chance to
136 // do final cleanup that depends on the MessageLoop.
137 //
138 // NOTE: Any tasks posted to the MessageLoop during this notification will
139 // not be run. Instead, they will be deleted.
140 //
141 class BASE_EXPORT DestructionObserver {
142 public:
143 virtual void WillDestroyCurrentMessageLoop() = 0;
144
145 protected:
146 virtual ~DestructionObserver();
147 };
148
149 // Add a DestructionObserver, which will start receiving notifications
150 // immediately.
151 void AddDestructionObserver(DestructionObserver* destruction_observer);
152
153 // Remove a DestructionObserver. It is safe to call this method while a
154 // DestructionObserver is receiving a notification callback.
155 void RemoveDestructionObserver(DestructionObserver* destruction_observer);
156
157 // The "PostTask" family of methods call the task's Run method asynchronously
158 // from within a message loop at some point in the future.
159 //
160 // With the PostTask variant, tasks are invoked in FIFO order, inter-mixed
161 // with normal UI or IO event processing. With the PostDelayedTask variant,
162 // tasks are called after at least approximately 'delay_ms' have elapsed.
163 //
164 // The NonNestable variants work similarly except that they promise never to
165 // dispatch the task from a nested invocation of MessageLoop::Run. Instead,
166 // such tasks get deferred until the top-most MessageLoop::Run is executing.
167 //
168 // The MessageLoop takes ownership of the Task, and deletes it after it has
169 // been Run().
170 //
171 // PostTask(from_here, task) is equivalent to
172 // PostDelayedTask(from_here, task, 0).
173 //
[email protected]186ced82013-06-14 03:27:49174 // NOTE: These methods may be called on any thread. The Task will be invoked
175 // on the thread that executes MessageLoop::Run().
[email protected]59e69e742013-06-18 20:27:52176 void PostTask(const tracked_objects::Location& from_here,
177 const Closure& task);
[email protected]186ced82013-06-14 03:27:49178
[email protected]59e69e742013-06-18 20:27:52179 void PostDelayedTask(const tracked_objects::Location& from_here,
180 const Closure& task,
181 TimeDelta delay);
[email protected]186ced82013-06-14 03:27:49182
[email protected]59e69e742013-06-18 20:27:52183 void PostNonNestableTask(const tracked_objects::Location& from_here,
184 const Closure& task);
[email protected]186ced82013-06-14 03:27:49185
[email protected]59e69e742013-06-18 20:27:52186 void PostNonNestableDelayedTask(const tracked_objects::Location& from_here,
187 const Closure& task,
188 TimeDelta delay);
[email protected]186ced82013-06-14 03:27:49189
190 // A variant on PostTask that deletes the given object. This is useful
191 // if the object needs to live until the next run of the MessageLoop (for
192 // example, deleting a RenderProcessHost from within an IPC callback is not
193 // good).
194 //
195 // NOTE: This method may be called on any thread. The object will be deleted
196 // on the thread that executes MessageLoop::Run(). If this is not the same
197 // as the thread that calls PostDelayedTask(FROM_HERE, ), then T MUST inherit
198 // from RefCountedThreadSafe<T>!
199 template <class T>
200 void DeleteSoon(const tracked_objects::Location& from_here, const T* object) {
201 base::subtle::DeleteHelperInternal<T, void>::DeleteViaSequencedTaskRunner(
202 this, from_here, object);
203 }
204
205 // A variant on PostTask that releases the given reference counted object
206 // (by calling its Release method). This is useful if the object needs to
207 // live until the next run of the MessageLoop, or if the object needs to be
208 // released on a particular thread.
209 //
[email protected]ab8748e2014-05-27 22:37:50210 // A common pattern is to manually increment the object's reference count
[email protected]b76b02782014-05-30 18:07:17211 // (AddRef), clear the pointer, then issue a ReleaseSoon. The reference count
[email protected]ab8748e2014-05-27 22:37:50212 // is incremented manually to ensure clearing the pointer does not trigger a
213 // delete and to account for the upcoming decrement (ReleaseSoon). For
214 // example:
215 //
216 // scoped_refptr<Foo> foo = ...
[email protected]b76b02782014-05-30 18:07:17217 // foo->AddRef();
218 // Foo* raw_foo = foo.get();
[email protected]ab8748e2014-05-27 22:37:50219 // foo = NULL;
[email protected]b76b02782014-05-30 18:07:17220 // message_loop->ReleaseSoon(raw_foo);
[email protected]ab8748e2014-05-27 22:37:50221 //
[email protected]186ced82013-06-14 03:27:49222 // NOTE: This method may be called on any thread. The object will be
223 // released (and thus possibly deleted) on the thread that executes
224 // MessageLoop::Run(). If this is not the same as the thread that calls
225 // PostDelayedTask(FROM_HERE, ), then T MUST inherit from
226 // RefCountedThreadSafe<T>!
227 template <class T>
228 void ReleaseSoon(const tracked_objects::Location& from_here,
229 const T* object) {
230 base::subtle::ReleaseHelperInternal<T, void>::ReleaseViaSequencedTaskRunner(
231 this, from_here, object);
232 }
233
234 // Deprecated: use RunLoop instead.
235 // Run the message loop.
236 void Run();
237
238 // Deprecated: use RunLoop instead.
239 // Process all pending tasks, windows messages, etc., but don't wait/sleep.
240 // Return as soon as all items that can be run are taken care of.
241 void RunUntilIdle();
242
243 // TODO(jbates) remove this. crbug.com/131220. See QuitWhenIdle().
244 void Quit() { QuitWhenIdle(); }
245
246 // Deprecated: use RunLoop instead.
247 //
248 // Signals the Run method to return when it becomes idle. It will continue to
249 // process pending messages and future messages as long as they are enqueued.
250 // Warning: if the MessageLoop remains busy, it may never quit. Only use this
251 // Quit method when looping procedures (such as web pages) have been shut
252 // down.
253 //
254 // This method may only be called on the same thread that called Run, and Run
255 // must still be on the call stack.
256 //
257 // Use QuitClosure variants if you need to Quit another thread's MessageLoop,
258 // but note that doing so is fairly dangerous if the target thread makes
259 // nested calls to MessageLoop::Run. The problem being that you won't know
260 // which nested run loop you are quitting, so be careful!
261 void QuitWhenIdle();
262
263 // Deprecated: use RunLoop instead.
264 //
265 // This method is a variant of Quit, that does not wait for pending messages
266 // to be processed before returning from Run.
267 void QuitNow();
268
269 // TODO(jbates) remove this. crbug.com/131220. See QuitWhenIdleClosure().
[email protected]59e69e742013-06-18 20:27:52270 static Closure QuitClosure() { return QuitWhenIdleClosure(); }
[email protected]186ced82013-06-14 03:27:49271
272 // Deprecated: use RunLoop instead.
273 // Construct a Closure that will call QuitWhenIdle(). Useful to schedule an
274 // arbitrary MessageLoop to QuitWhenIdle.
[email protected]59e69e742013-06-18 20:27:52275 static Closure QuitWhenIdleClosure();
[email protected]186ced82013-06-14 03:27:49276
277 // Returns true if this loop is |type|. This allows subclasses (especially
278 // those in tests) to specialize how they are identified.
279 virtual bool IsType(Type type) const;
280
281 // Returns the type passed to the constructor.
282 Type type() const { return type_; }
283
284 // Optional call to connect the thread name with this loop.
285 void set_thread_name(const std::string& thread_name) {
286 DCHECK(thread_name_.empty()) << "Should not rename this thread!";
287 thread_name_ = thread_name;
288 }
289 const std::string& thread_name() const { return thread_name_; }
290
291 // Gets the message loop proxy associated with this message loop.
[email protected]59e69e742013-06-18 20:27:52292 scoped_refptr<MessageLoopProxy> message_loop_proxy() {
[email protected]54aa4f12013-07-22 22:24:13293 return message_loop_proxy_;
[email protected]186ced82013-06-14 03:27:49294 }
295
296 // Enables or disables the recursive task processing. This happens in the case
297 // of recursive message loops. Some unwanted message loop may occurs when
298 // using common controls or printer functions. By default, recursive task
299 // processing is disabled.
300 //
301 // Please utilize |ScopedNestableTaskAllower| instead of calling these methods
302 // directly. In general nestable message loops are to be avoided. They are
303 // dangerous and difficult to get right, so please use with extreme caution.
304 //
305 // The specific case where tasks get queued is:
306 // - The thread is running a message loop.
307 // - It receives a task #1 and execute it.
308 // - The task #1 implicitly start a message loop, like a MessageBox in the
309 // unit test. This can also be StartDoc or GetSaveFileName.
310 // - The thread receives a task #2 before or while in this second message
311 // loop.
312 // - With NestableTasksAllowed set to true, the task #2 will run right away.
313 // Otherwise, it will get executed right after task #1 completes at "thread
314 // message loop level".
315 void SetNestableTasksAllowed(bool allowed);
316 bool NestableTasksAllowed() const;
317
318 // Enables nestable tasks on |loop| while in scope.
319 class ScopedNestableTaskAllower {
320 public:
321 explicit ScopedNestableTaskAllower(MessageLoop* loop)
322 : loop_(loop),
323 old_state_(loop_->NestableTasksAllowed()) {
324 loop_->SetNestableTasksAllowed(true);
325 }
326 ~ScopedNestableTaskAllower() {
327 loop_->SetNestableTasksAllowed(old_state_);
328 }
329
330 private:
331 MessageLoop* loop_;
332 bool old_state_;
333 };
334
[email protected]186ced82013-06-14 03:27:49335 // Returns true if we are currently running a nested message loop.
336 bool IsNested();
337
338 // A TaskObserver is an object that receives task notifications from the
339 // MessageLoop.
340 //
341 // NOTE: A TaskObserver implementation should be extremely fast!
342 class BASE_EXPORT TaskObserver {
343 public:
344 TaskObserver();
345
346 // This method is called before processing a task.
[email protected]59e69e742013-06-18 20:27:52347 virtual void WillProcessTask(const PendingTask& pending_task) = 0;
[email protected]186ced82013-06-14 03:27:49348
349 // This method is called after processing a task.
[email protected]59e69e742013-06-18 20:27:52350 virtual void DidProcessTask(const PendingTask& pending_task) = 0;
[email protected]186ced82013-06-14 03:27:49351
352 protected:
353 virtual ~TaskObserver();
354 };
355
356 // These functions can only be called on the same thread that |this| is
357 // running on.
358 void AddTaskObserver(TaskObserver* task_observer);
359 void RemoveTaskObserver(TaskObserver* task_observer);
360
[email protected]186ced82013-06-14 03:27:49361 // When we go into high resolution timer mode, we will stay in hi-res mode
362 // for at least 1s.
363 static const int kHighResolutionTimerModeLeaseTimeMs = 1000;
364
[email protected]186ced82013-06-14 03:27:49365#if defined(OS_WIN)
366 void set_os_modal_loop(bool os_modal_loop) {
367 os_modal_loop_ = os_modal_loop;
368 }
369
370 bool os_modal_loop() const {
371 return os_modal_loop_;
372 }
373#endif // OS_WIN
374
375 // Can only be called from the thread that owns the MessageLoop.
376 bool is_running() const;
377
[email protected]54aa4f12013-07-22 22:24:13378 // Returns true if the message loop has high resolution timers enabled.
379 // Provided for testing.
380 bool IsHighResolutionTimerEnabledForTesting();
381
382 // Returns true if the message loop is "idle". Provided for testing.
383 bool IsIdleForTesting();
384
[email protected]186ced82013-06-14 03:27:49385 //----------------------------------------------------------------------------
386 protected:
[email protected]54aa4f12013-07-22 22:24:13387 scoped_ptr<MessagePump> pump_;
[email protected]186ced82013-06-14 03:27:49388
389 private:
[email protected]54aa4f12013-07-22 22:24:13390 friend class internal::IncomingTaskQueue;
[email protected]59e69e742013-06-18 20:27:52391 friend class RunLoop;
[email protected]186ced82013-06-14 03:27:49392
[email protected]9f0e4f72013-11-08 06:16:53393 // Configures various members for the two constructors.
394 void Init();
395
[email protected]62a314e2014-01-03 21:25:26396 // Invokes the actual run loop using the message pump.
[email protected]186ced82013-06-14 03:27:49397 void RunHandler();
398
[email protected]186ced82013-06-14 03:27:49399 // Called to process any delayed non-nestable tasks.
400 bool ProcessNextDelayedNonNestableTask();
401
402 // Runs the specified PendingTask.
[email protected]59e69e742013-06-18 20:27:52403 void RunTask(const PendingTask& pending_task);
[email protected]186ced82013-06-14 03:27:49404
405 // Calls RunTask or queues the pending_task on the deferred task list if it
406 // cannot be run right now. Returns true if the task was run.
[email protected]59e69e742013-06-18 20:27:52407 bool DeferOrRunPendingTask(const PendingTask& pending_task);
[email protected]186ced82013-06-14 03:27:49408
409 // Adds the pending task to delayed_work_queue_.
[email protected]59e69e742013-06-18 20:27:52410 void AddToDelayedWorkQueue(const PendingTask& pending_task);
[email protected]186ced82013-06-14 03:27:49411
[email protected]186ced82013-06-14 03:27:49412 // Delete tasks that haven't run yet without running them. Used in the
413 // destructor to make sure all the task's destructors get called. Returns
414 // true if some work was done.
415 bool DeletePendingTasks();
416
[email protected]54aa4f12013-07-22 22:24:13417 // Creates a process-wide unique ID to represent this task in trace events.
418 // This will be mangled with a Process ID hash to reduce the likelyhood of
419 // colliding with MessageLoop pointers on other processes.
420 uint64 GetTaskTraceID(const PendingTask& task);
421
422 // Loads tasks from the incoming queue to |work_queue_| if the latter is
423 // empty.
424 void ReloadWorkQueue();
425
426 // Wakes up the message pump. Can be called on any thread. The caller is
427 // responsible for synchronizing ScheduleWork() calls.
428 void ScheduleWork(bool was_empty);
[email protected]186ced82013-06-14 03:27:49429
430 // Start recording histogram info about events and action IF it was enabled
431 // and IF the statistics recorder can accept a registration of our histogram.
432 void StartHistogrammer();
433
434 // Add occurrence of event to our histogram, so that we can see what is being
435 // done in a specific MessageLoop instance (i.e., specific thread).
436 // If message_histogram_ is NULL, this is a no-op.
437 void HistogramEvent(int event);
438
[email protected]59e69e742013-06-18 20:27:52439 // MessagePump::Delegate methods:
[email protected]186ced82013-06-14 03:27:49440 virtual bool DoWork() OVERRIDE;
[email protected]59e69e742013-06-18 20:27:52441 virtual bool DoDelayedWork(TimeTicks* next_delayed_work_time) OVERRIDE;
[email protected]186ced82013-06-14 03:27:49442 virtual bool DoIdleWork() OVERRIDE;
[email protected]370d7cb2013-09-05 14:53:12443 virtual void GetQueueingInformation(size_t* queue_size,
444 TimeDelta* queueing_delay) OVERRIDE;
[email protected]186ced82013-06-14 03:27:49445
[email protected]9f0e4f72013-11-08 06:16:53446 const Type type_;
[email protected]186ced82013-06-14 03:27:49447
448 // A list of tasks that need to be processed by this instance. Note that
449 // this queue is only accessed (push/pop) by our current thread.
[email protected]59e69e742013-06-18 20:27:52450 TaskQueue work_queue_;
[email protected]186ced82013-06-14 03:27:49451
452 // Contains delayed tasks, sorted by their 'delayed_run_time' property.
[email protected]59e69e742013-06-18 20:27:52453 DelayedTaskQueue delayed_work_queue_;
[email protected]186ced82013-06-14 03:27:49454
455 // A recent snapshot of Time::Now(), used to check delayed_work_queue_.
[email protected]59e69e742013-06-18 20:27:52456 TimeTicks recent_time_;
[email protected]186ced82013-06-14 03:27:49457
458 // A queue of non-nestable tasks that we had to defer because when it came
459 // time to execute them we were in a nested message loop. They will execute
460 // once we're out of nested message loops.
[email protected]59e69e742013-06-18 20:27:52461 TaskQueue deferred_non_nestable_work_queue_;
[email protected]186ced82013-06-14 03:27:49462
463 ObserverList<DestructionObserver> destruction_observers_;
464
465 // A recursion block that prevents accidentally running additional tasks when
466 // insider a (accidentally induced?) nested message pump.
467 bool nestable_tasks_allowed_;
468
[email protected]c9904b42013-07-22 20:06:56469#if defined(OS_WIN)
[email protected]c9904b42013-07-22 20:06:56470 // Should be set to true before calling Windows APIs like TrackPopupMenu, etc
471 // which enter a modal message loop.
472 bool os_modal_loop_;
473#endif
474
[email protected]54aa4f12013-07-22 22:24:13475 std::string thread_name_;
476 // A profiling histogram showing the counts of various messages and events.
477 HistogramBase* message_histogram_;
478
479 RunLoop* run_loop_;
[email protected]c9904b42013-07-22 20:06:56480
[email protected]186ced82013-06-14 03:27:49481 ObserverList<TaskObserver> task_observers_;
482
[email protected]54aa4f12013-07-22 22:24:13483 scoped_refptr<internal::IncomingTaskQueue> incoming_task_queue_;
484
485 // The message loop proxy associated with this message loop.
486 scoped_refptr<internal::MessageLoopProxyImpl> message_loop_proxy_;
[email protected]59e69e742013-06-18 20:27:52487 scoped_ptr<ThreadTaskRunnerHandle> thread_task_runner_handle_;
[email protected]186ced82013-06-14 03:27:49488
489 template <class T, class R> friend class base::subtle::DeleteHelperInternal;
490 template <class T, class R> friend class base::subtle::ReleaseHelperInternal;
491
492 void DeleteSoonInternal(const tracked_objects::Location& from_here,
493 void(*deleter)(const void*),
494 const void* object);
495 void ReleaseSoonInternal(const tracked_objects::Location& from_here,
496 void(*releaser)(const void*),
497 const void* object);
498
499 DISALLOW_COPY_AND_ASSIGN(MessageLoop);
500};
501
[email protected]bf933e52014-04-23 00:55:07502#if !defined(OS_NACL)
503
[email protected]186ced82013-06-14 03:27:49504//-----------------------------------------------------------------------------
505// MessageLoopForUI extends MessageLoop with methods that are particular to a
506// MessageLoop instantiated with TYPE_UI.
507//
508// This class is typically used like so:
509// MessageLoopForUI::current()->...call some method...
510//
511class BASE_EXPORT MessageLoopForUI : public MessageLoop {
512 public:
[email protected]186ced82013-06-14 03:27:49513 MessageLoopForUI() : MessageLoop(TYPE_UI) {
514 }
515
516 // Returns the MessageLoopForUI of the current thread.
517 static MessageLoopForUI* current() {
518 MessageLoop* loop = MessageLoop::current();
519 DCHECK(loop);
520 DCHECK_EQ(MessageLoop::TYPE_UI, loop->type());
521 return static_cast<MessageLoopForUI*>(loop);
522 }
523
[email protected]9d434e22014-01-20 08:59:52524 static bool IsCurrent() {
525 MessageLoop* loop = MessageLoop::current();
526 return loop && loop->type() == MessageLoop::TYPE_UI;
527 }
528
[email protected]186ced82013-06-14 03:27:49529#if defined(OS_IOS)
530 // On iOS, the main message loop cannot be Run(). Instead call Attach(),
531 // which connects this MessageLoop to the UI thread's CFRunLoop and allows
532 // PostTask() to work.
533 void Attach();
534#endif
535
536#if defined(OS_ANDROID)
537 // On Android, the UI message loop is handled by Java side. So Run() should
538 // never be called. Instead use Start(), which will forward all the native UI
539 // events to the Java message loop.
540 void Start();
[email protected]0b41c522014-01-11 23:12:06541#endif
[email protected]186ced82013-06-14 03:27:49542
[email protected]bf933e52014-04-23 00:55:07543#if defined(OS_WIN)
544 typedef MessagePumpObserver Observer;
545
[email protected]e10f5fe12014-04-11 03:02:29546 // Please see message_pump_win for definitions of these methods.
[email protected]186ced82013-06-14 03:27:49547 void AddObserver(Observer* observer);
548 void RemoveObserver(Observer* observer);
[email protected]0b41c522014-01-11 23:12:06549#endif
[email protected]186ced82013-06-14 03:27:49550
[email protected]bf933e52014-04-23 00:55:07551#if defined(USE_OZONE) || (defined(OS_CHROMEOS) && !defined(USE_GLIB))
[email protected]ef1a61b2014-04-10 17:19:38552 // Please see MessagePumpLibevent for definition.
553 bool WatchFileDescriptor(
554 int fd,
555 bool persistent,
556 MessagePumpLibevent::Mode mode,
557 MessagePumpLibevent::FileDescriptorWatcher* controller,
558 MessagePumpLibevent::Watcher* delegate);
559#endif
[email protected]186ced82013-06-14 03:27:49560};
561
562// Do not add any member variables to MessageLoopForUI! This is important b/c
563// MessageLoopForUI is often allocated via MessageLoop(TYPE_UI). Any extra
564// data that you need should be stored on the MessageLoop's pump_ instance.
565COMPILE_ASSERT(sizeof(MessageLoop) == sizeof(MessageLoopForUI),
566 MessageLoopForUI_should_not_have_extra_member_variables);
567
[email protected]bf933e52014-04-23 00:55:07568#endif // !defined(OS_NACL)
569
[email protected]186ced82013-06-14 03:27:49570//-----------------------------------------------------------------------------
571// MessageLoopForIO extends MessageLoop with methods that are particular to a
572// MessageLoop instantiated with TYPE_IO.
573//
574// This class is typically used like so:
575// MessageLoopForIO::current()->...call some method...
576//
577class BASE_EXPORT MessageLoopForIO : public MessageLoop {
578 public:
[email protected]bf933e52014-04-23 00:55:07579 MessageLoopForIO() : MessageLoop(TYPE_IO) {
580 }
581
582 // Returns the MessageLoopForIO of the current thread.
583 static MessageLoopForIO* current() {
584 MessageLoop* loop = MessageLoop::current();
585 DCHECK_EQ(MessageLoop::TYPE_IO, loop->type());
586 return static_cast<MessageLoopForIO*>(loop);
587 }
588
589 static bool IsCurrent() {
590 MessageLoop* loop = MessageLoop::current();
591 return loop && loop->type() == MessageLoop::TYPE_IO;
592 }
593
594#if !defined(OS_NACL)
595
[email protected]186ced82013-06-14 03:27:49596#if defined(OS_WIN)
[email protected]59e69e742013-06-18 20:27:52597 typedef MessagePumpForIO::IOHandler IOHandler;
598 typedef MessagePumpForIO::IOContext IOContext;
599 typedef MessagePumpForIO::IOObserver IOObserver;
[email protected]186ced82013-06-14 03:27:49600#elif defined(OS_IOS)
[email protected]59e69e742013-06-18 20:27:52601 typedef MessagePumpIOSForIO::Watcher Watcher;
602 typedef MessagePumpIOSForIO::FileDescriptorWatcher
[email protected]186ced82013-06-14 03:27:49603 FileDescriptorWatcher;
[email protected]59e69e742013-06-18 20:27:52604 typedef MessagePumpIOSForIO::IOObserver IOObserver;
[email protected]186ced82013-06-14 03:27:49605
606 enum Mode {
[email protected]59e69e742013-06-18 20:27:52607 WATCH_READ = MessagePumpIOSForIO::WATCH_READ,
608 WATCH_WRITE = MessagePumpIOSForIO::WATCH_WRITE,
609 WATCH_READ_WRITE = MessagePumpIOSForIO::WATCH_READ_WRITE
[email protected]186ced82013-06-14 03:27:49610 };
611#elif defined(OS_POSIX)
[email protected]59e69e742013-06-18 20:27:52612 typedef MessagePumpLibevent::Watcher Watcher;
613 typedef MessagePumpLibevent::FileDescriptorWatcher
[email protected]186ced82013-06-14 03:27:49614 FileDescriptorWatcher;
[email protected]59e69e742013-06-18 20:27:52615 typedef MessagePumpLibevent::IOObserver IOObserver;
[email protected]186ced82013-06-14 03:27:49616
617 enum Mode {
[email protected]59e69e742013-06-18 20:27:52618 WATCH_READ = MessagePumpLibevent::WATCH_READ,
619 WATCH_WRITE = MessagePumpLibevent::WATCH_WRITE,
620 WATCH_READ_WRITE = MessagePumpLibevent::WATCH_READ_WRITE
[email protected]186ced82013-06-14 03:27:49621 };
[email protected]186ced82013-06-14 03:27:49622#endif
623
[email protected]bf933e52014-04-23 00:55:07624 void AddIOObserver(IOObserver* io_observer);
625 void RemoveIOObserver(IOObserver* io_observer);
[email protected]186ced82013-06-14 03:27:49626
627#if defined(OS_WIN)
628 // Please see MessagePumpWin for definitions of these methods.
629 void RegisterIOHandler(HANDLE file, IOHandler* handler);
630 bool RegisterJobObject(HANDLE job, IOHandler* handler);
631 bool WaitForIOCompletion(DWORD timeout, IOHandler* filter);
[email protected]bf933e52014-04-23 00:55:07632#elif defined(OS_POSIX)
633 // Please see MessagePumpIOSForIO/MessagePumpLibevent for definition.
[email protected]186ced82013-06-14 03:27:49634 bool WatchFileDescriptor(int fd,
635 bool persistent,
636 Mode mode,
637 FileDescriptorWatcher *controller,
638 Watcher *delegate);
[email protected]bf933e52014-04-23 00:55:07639#endif // defined(OS_IOS) || defined(OS_POSIX)
640#endif // !defined(OS_NACL)
[email protected]186ced82013-06-14 03:27:49641};
642
643// Do not add any member variables to MessageLoopForIO! This is important b/c
644// MessageLoopForIO is often allocated via MessageLoop(TYPE_IO). Any extra
645// data that you need should be stored on the MessageLoop's pump_ instance.
646COMPILE_ASSERT(sizeof(MessageLoop) == sizeof(MessageLoopForIO),
647 MessageLoopForIO_should_not_have_extra_member_variables);
648
649} // namespace base
650
651#endif // BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_