cros: Emit 'login-prompt-visible' signal when auto launching kiosk app.
[chromium-blink-merge.git] / base / message_loop.h
blob0de51ab51789886aa42d677454702f832ce02acf
1 // Copyright (c) 2012 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.
5 #ifndef BASE_MESSAGE_LOOP_H_
6 #define BASE_MESSAGE_LOOP_H_
8 #include <queue>
9 #include <string>
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"
16 #include "base/message_loop_proxy.h"
17 #include "base/message_pump.h"
18 #include "base/observer_list.h"
19 #include "base/pending_task.h"
20 #include "base/sequenced_task_runner_helpers.h"
21 #include "base/synchronization/lock.h"
22 #include "base/tracking_info.h"
23 #include "base/time.h"
25 #if defined(OS_WIN)
26 // We need this to declare base::MessagePumpWin::Dispatcher, which we should
27 // really just eliminate.
28 #include "base/message_pump_win.h"
29 #elif defined(OS_IOS)
30 #include "base/message_pump_io_ios.h"
31 #elif defined(OS_POSIX)
32 #include "base/message_pump_libevent.h"
33 #if !defined(OS_MACOSX) && !defined(OS_ANDROID)
35 #if defined(USE_AURA) && defined(USE_X11) && !defined(OS_NACL)
36 #include "base/message_pump_aurax11.h"
37 #else
38 #include "base/message_pump_gtk.h"
39 #endif
41 #endif
42 #endif
44 namespace base {
46 class HistogramBase;
47 class RunLoop;
48 class ThreadTaskRunnerHandle;
49 #if defined(OS_ANDROID)
50 class MessagePumpForUI;
51 #endif
53 // A MessageLoop is used to process events for a particular thread. There is
54 // at most one MessageLoop instance per thread.
56 // Events include at a minimum Task instances submitted to PostTask and its
57 // variants. Depending on the type of message pump used by the MessageLoop
58 // other events such as UI messages may be processed. On Windows APC calls (as
59 // time permits) and signals sent to a registered set of HANDLEs may also be
60 // processed.
62 // NOTE: Unless otherwise specified, a MessageLoop's methods may only be called
63 // on the thread where the MessageLoop's Run method executes.
65 // NOTE: MessageLoop has task reentrancy protection. This means that if a
66 // task is being processed, a second task cannot start until the first task is
67 // finished. Reentrancy can happen when processing a task, and an inner
68 // message pump is created. That inner pump then processes native messages
69 // which could implicitly start an inner task. Inner message pumps are created
70 // with dialogs (DialogBox), common dialogs (GetOpenFileName), OLE functions
71 // (DoDragDrop), printer functions (StartDoc) and *many* others.
73 // Sample workaround when inner task processing is needed:
74 // HRESULT hr;
75 // {
76 // MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current());
77 // hr = DoDragDrop(...); // Implicitly runs a modal message loop.
78 // }
79 // // Process |hr| (the result returned by DoDragDrop()).
81 // Please be SURE your task is reentrant (nestable) and all global variables
82 // are stable and accessible before calling SetNestableTasksAllowed(true).
84 class BASE_EXPORT MessageLoop : public base::MessagePump::Delegate {
85 public:
87 #if !defined(OS_MACOSX) && !defined(OS_ANDROID)
88 typedef base::MessagePumpDispatcher Dispatcher;
89 typedef base::MessagePumpObserver Observer;
90 #endif
92 // A MessageLoop has a particular type, which indicates the set of
93 // asynchronous events it may process in addition to tasks and timers.
95 // TYPE_DEFAULT
96 // This type of ML only supports tasks and timers.
98 // TYPE_UI
99 // This type of ML also supports native UI events (e.g., Windows messages).
100 // See also MessageLoopForUI.
102 // TYPE_IO
103 // This type of ML also supports asynchronous IO. See also
104 // MessageLoopForIO.
106 enum Type {
107 TYPE_DEFAULT,
108 TYPE_UI,
109 TYPE_IO
112 // Normally, it is not necessary to instantiate a MessageLoop. Instead, it
113 // is typical to make use of the current thread's MessageLoop instance.
114 explicit MessageLoop(Type type = TYPE_DEFAULT);
115 virtual ~MessageLoop();
117 // Returns the MessageLoop object for the current thread, or null if none.
118 static MessageLoop* current();
120 static void EnableHistogrammer(bool enable_histogrammer);
122 typedef base::MessagePump* (MessagePumpFactory)();
123 // Uses the given base::MessagePumpForUIFactory to override the default
124 // MessagePump implementation for 'TYPE_UI'. Returns true if the factory
125 // was successfully registered.
126 static bool InitMessagePumpForUIFactory(MessagePumpFactory* factory);
128 // A DestructionObserver is notified when the current MessageLoop is being
129 // destroyed. These observers are notified prior to MessageLoop::current()
130 // being changed to return NULL. This gives interested parties the chance to
131 // do final cleanup that depends on the MessageLoop.
133 // NOTE: Any tasks posted to the MessageLoop during this notification will
134 // not be run. Instead, they will be deleted.
136 class BASE_EXPORT DestructionObserver {
137 public:
138 virtual void WillDestroyCurrentMessageLoop() = 0;
140 protected:
141 virtual ~DestructionObserver();
144 // Add a DestructionObserver, which will start receiving notifications
145 // immediately.
146 void AddDestructionObserver(DestructionObserver* destruction_observer);
148 // Remove a DestructionObserver. It is safe to call this method while a
149 // DestructionObserver is receiving a notification callback.
150 void RemoveDestructionObserver(DestructionObserver* destruction_observer);
152 // The "PostTask" family of methods call the task's Run method asynchronously
153 // from within a message loop at some point in the future.
155 // With the PostTask variant, tasks are invoked in FIFO order, inter-mixed
156 // with normal UI or IO event processing. With the PostDelayedTask variant,
157 // tasks are called after at least approximately 'delay_ms' have elapsed.
159 // The NonNestable variants work similarly except that they promise never to
160 // dispatch the task from a nested invocation of MessageLoop::Run. Instead,
161 // such tasks get deferred until the top-most MessageLoop::Run is executing.
163 // The MessageLoop takes ownership of the Task, and deletes it after it has
164 // been Run().
166 // PostTask(from_here, task) is equivalent to
167 // PostDelayedTask(from_here, task, 0).
169 // NOTE: These methods may be called on any thread. The Task will be invoked
170 // on the thread that executes MessageLoop::Run().
171 void PostTask(
172 const tracked_objects::Location& from_here,
173 const base::Closure& task);
175 void PostDelayedTask(
176 const tracked_objects::Location& from_here,
177 const base::Closure& task,
178 base::TimeDelta delay);
180 void PostNonNestableTask(
181 const tracked_objects::Location& from_here,
182 const base::Closure& task);
184 void PostNonNestableDelayedTask(
185 const tracked_objects::Location& from_here,
186 const base::Closure& task,
187 base::TimeDelta delay);
189 // A variant on PostTask that deletes the given object. This is useful
190 // if the object needs to live until the next run of the MessageLoop (for
191 // example, deleting a RenderProcessHost from within an IPC callback is not
192 // good).
194 // NOTE: This method may be called on any thread. The object will be deleted
195 // on the thread that executes MessageLoop::Run(). If this is not the same
196 // as the thread that calls PostDelayedTask(FROM_HERE, ), then T MUST inherit
197 // from RefCountedThreadSafe<T>!
198 template <class T>
199 void DeleteSoon(const tracked_objects::Location& from_here, const T* object) {
200 base::subtle::DeleteHelperInternal<T, void>::DeleteViaSequencedTaskRunner(
201 this, from_here, object);
204 // A variant on PostTask that releases the given reference counted object
205 // (by calling its Release method). This is useful if the object needs to
206 // live until the next run of the MessageLoop, or if the object needs to be
207 // released on a particular thread.
209 // NOTE: This method may be called on any thread. The object will be
210 // released (and thus possibly deleted) on the thread that executes
211 // MessageLoop::Run(). If this is not the same as the thread that calls
212 // PostDelayedTask(FROM_HERE, ), then T MUST inherit from
213 // RefCountedThreadSafe<T>!
214 template <class T>
215 void ReleaseSoon(const tracked_objects::Location& from_here,
216 const T* object) {
217 base::subtle::ReleaseHelperInternal<T, void>::ReleaseViaSequencedTaskRunner(
218 this, from_here, object);
221 // Deprecated: use RunLoop instead.
222 // Run the message loop.
223 void Run();
225 // Deprecated: use RunLoop instead.
226 // 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 RunUntilIdle();
230 // TODO(jbates) remove this. crbug.com/131220. See QuitWhenIdle().
231 void Quit() { QuitWhenIdle(); }
233 // Deprecated: use RunLoop instead.
235 // Signals the Run method to return when it becomes idle. It will continue to
236 // process pending messages and future messages as long as they are enqueued.
237 // Warning: if the MessageLoop remains busy, it may never quit. Only use this
238 // Quit method when looping procedures (such as web pages) have been shut
239 // down.
241 // This method may only be called on the same thread that called Run, and Run
242 // must still be on the call stack.
244 // Use QuitClosure variants if you need to Quit another thread's MessageLoop,
245 // but note that doing so is fairly dangerous if the target thread makes
246 // nested calls to MessageLoop::Run. The problem being that you won't know
247 // which nested run loop you are quitting, so be careful!
248 void QuitWhenIdle();
250 // Deprecated: use RunLoop instead.
252 // This method is a variant of Quit, that does not wait for pending messages
253 // to be processed before returning from Run.
254 void QuitNow();
256 // TODO(jbates) remove this. crbug.com/131220. See QuitWhenIdleClosure().
257 static base::Closure QuitClosure() { return QuitWhenIdleClosure(); }
259 // Deprecated: use RunLoop instead.
260 // Construct a Closure that will call QuitWhenIdle(). Useful to schedule an
261 // arbitrary MessageLoop to QuitWhenIdle.
262 static base::Closure QuitWhenIdleClosure();
264 // Returns true if this loop is |type|. This allows subclasses (especially
265 // those in tests) to specialize how they are identified.
266 virtual bool IsType(Type type) const;
268 // Returns the type passed to the constructor.
269 Type type() const { return type_; }
271 // Optional call to connect the thread name with this loop.
272 void set_thread_name(const std::string& thread_name) {
273 DCHECK(thread_name_.empty()) << "Should not rename this thread!";
274 thread_name_ = thread_name;
276 const std::string& thread_name() const { return thread_name_; }
278 // Gets the message loop proxy associated with this message loop.
279 scoped_refptr<base::MessageLoopProxy> message_loop_proxy() {
280 return message_loop_proxy_.get();
283 // Enables or disables the recursive task processing. This happens in the case
284 // of recursive message loops. Some unwanted message loop may occurs when
285 // using common controls or printer functions. By default, recursive task
286 // processing is disabled.
288 // Please utilize |ScopedNestableTaskAllower| instead of calling these methods
289 // directly. In general nestable message loops are to be avoided. They are
290 // dangerous and difficult to get right, so please use with extreme caution.
292 // The specific case where tasks get queued is:
293 // - The thread is running a message loop.
294 // - It receives a task #1 and execute it.
295 // - The task #1 implicitly start a message loop, like a MessageBox in the
296 // unit test. This can also be StartDoc or GetSaveFileName.
297 // - The thread receives a task #2 before or while in this second message
298 // loop.
299 // - With NestableTasksAllowed set to true, the task #2 will run right away.
300 // Otherwise, it will get executed right after task #1 completes at "thread
301 // message loop level".
302 void SetNestableTasksAllowed(bool allowed);
303 bool NestableTasksAllowed() const;
305 // Enables nestable tasks on |loop| while in scope.
306 class ScopedNestableTaskAllower {
307 public:
308 explicit ScopedNestableTaskAllower(MessageLoop* loop)
309 : loop_(loop),
310 old_state_(loop_->NestableTasksAllowed()) {
311 loop_->SetNestableTasksAllowed(true);
313 ~ScopedNestableTaskAllower() {
314 loop_->SetNestableTasksAllowed(old_state_);
317 private:
318 MessageLoop* loop_;
319 bool old_state_;
322 // Enables or disables the restoration during an exception of the unhandled
323 // exception filter that was active when Run() was called. This can happen
324 // if some third party code call SetUnhandledExceptionFilter() and never
325 // restores the previous filter.
326 void set_exception_restoration(bool restore) {
327 exception_restoration_ = restore;
330 // Returns true if we are currently running a nested message loop.
331 bool IsNested();
333 // A TaskObserver is an object that receives task notifications from the
334 // MessageLoop.
336 // NOTE: A TaskObserver implementation should be extremely fast!
337 class BASE_EXPORT TaskObserver {
338 public:
339 TaskObserver();
341 // This method is called before processing a task.
342 virtual void WillProcessTask(const base::PendingTask& pending_task) = 0;
344 // This method is called after processing a task.
345 virtual void DidProcessTask(const base::PendingTask& pending_task) = 0;
347 protected:
348 virtual ~TaskObserver();
351 // These functions can only be called on the same thread that |this| is
352 // running on.
353 void AddTaskObserver(TaskObserver* task_observer);
354 void RemoveTaskObserver(TaskObserver* task_observer);
356 // Returns true if the message loop has high resolution timers enabled.
357 // Provided for testing.
358 bool high_resolution_timers_enabled() {
359 #if defined(OS_WIN)
360 return !high_resolution_timer_expiration_.is_null();
361 #else
362 return true;
363 #endif
366 // When we go into high resolution timer mode, we will stay in hi-res mode
367 // for at least 1s.
368 static const int kHighResolutionTimerModeLeaseTimeMs = 1000;
370 // Asserts that the MessageLoop is "idle".
371 void AssertIdle() const;
373 #if defined(OS_WIN)
374 void set_os_modal_loop(bool os_modal_loop) {
375 os_modal_loop_ = os_modal_loop;
378 bool os_modal_loop() const {
379 return os_modal_loop_;
381 #endif // OS_WIN
383 // Can only be called from the thread that owns the MessageLoop.
384 bool is_running() const;
386 //----------------------------------------------------------------------------
387 protected:
389 #if defined(OS_WIN)
390 base::MessagePumpWin* pump_win() {
391 return static_cast<base::MessagePumpWin*>(pump_.get());
393 #elif defined(OS_POSIX) && !defined(OS_IOS)
394 base::MessagePumpLibevent* pump_libevent() {
395 return static_cast<base::MessagePumpLibevent*>(pump_.get());
397 #endif
399 scoped_refptr<base::MessagePump> pump_;
401 private:
402 friend class base::RunLoop;
404 // A function to encapsulate all the exception handling capability in the
405 // stacks around the running of a main message loop. It will run the message
406 // loop in a SEH try block or not depending on the set_SEH_restoration()
407 // flag invoking respectively RunInternalInSEHFrame() or RunInternal().
408 void RunHandler();
410 #if defined(OS_WIN)
411 __declspec(noinline) void RunInternalInSEHFrame();
412 #endif
414 // A surrounding stack frame around the running of the message loop that
415 // supports all saving and restoring of state, as is needed for any/all (ugly)
416 // recursive calls.
417 void RunInternal();
419 // Called to process any delayed non-nestable tasks.
420 bool ProcessNextDelayedNonNestableTask();
422 // Runs the specified PendingTask.
423 void RunTask(const base::PendingTask& pending_task);
425 // Calls RunTask or queues the pending_task on the deferred task list if it
426 // cannot be run right now. Returns true if the task was run.
427 bool DeferOrRunPendingTask(const base::PendingTask& pending_task);
429 // Adds the pending task to delayed_work_queue_.
430 void AddToDelayedWorkQueue(const base::PendingTask& pending_task);
432 // Adds the pending task to our incoming_queue_.
434 // Caller retains ownership of |pending_task|, but this function will
435 // reset the value of pending_task->task. This is needed to ensure
436 // that the posting call stack does not retain pending_task->task
437 // beyond this function call.
438 void AddToIncomingQueue(base::PendingTask* pending_task);
440 // Load tasks from the incoming_queue_ into work_queue_ if the latter is
441 // empty. The former requires a lock to access, while the latter is directly
442 // accessible on this thread.
443 void ReloadWorkQueue();
445 // Delete tasks that haven't run yet without running them. Used in the
446 // destructor to make sure all the task's destructors get called. Returns
447 // true if some work was done.
448 bool DeletePendingTasks();
450 // Calculates the time at which a PendingTask should run.
451 base::TimeTicks CalculateDelayedRuntime(base::TimeDelta delay);
453 // Start recording histogram info about events and action IF it was enabled
454 // and IF the statistics recorder can accept a registration of our histogram.
455 void StartHistogrammer();
457 // Add occurrence of event to our histogram, so that we can see what is being
458 // done in a specific MessageLoop instance (i.e., specific thread).
459 // If message_histogram_ is NULL, this is a no-op.
460 void HistogramEvent(int event);
462 // base::MessagePump::Delegate methods:
463 virtual bool DoWork() OVERRIDE;
464 virtual bool DoDelayedWork(base::TimeTicks* next_delayed_work_time) OVERRIDE;
465 virtual bool DoIdleWork() OVERRIDE;
467 Type type_;
469 // A list of tasks that need to be processed by this instance. Note that
470 // this queue is only accessed (push/pop) by our current thread.
471 base::TaskQueue work_queue_;
473 // Contains delayed tasks, sorted by their 'delayed_run_time' property.
474 base::DelayedTaskQueue delayed_work_queue_;
476 // A recent snapshot of Time::Now(), used to check delayed_work_queue_.
477 base::TimeTicks recent_time_;
479 // A queue of non-nestable tasks that we had to defer because when it came
480 // time to execute them we were in a nested message loop. They will execute
481 // once we're out of nested message loops.
482 base::TaskQueue deferred_non_nestable_work_queue_;
484 ObserverList<DestructionObserver> destruction_observers_;
486 // A recursion block that prevents accidentally running additional tasks when
487 // insider a (accidentally induced?) nested message pump.
488 bool nestable_tasks_allowed_;
490 bool exception_restoration_;
492 std::string thread_name_;
493 // A profiling histogram showing the counts of various messages and events.
494 base::HistogramBase* message_histogram_;
496 // An incoming queue of tasks that are acquired under a mutex for processing
497 // on this instance's thread. These tasks have not yet been sorted out into
498 // items for our work_queue_ vs delayed_work_queue_.
499 base::TaskQueue incoming_queue_;
500 // Protect access to incoming_queue_.
501 mutable base::Lock incoming_queue_lock_;
503 base::RunLoop* run_loop_;
505 #if defined(OS_WIN)
506 base::TimeTicks high_resolution_timer_expiration_;
507 // Should be set to true before calling Windows APIs like TrackPopupMenu, etc
508 // which enter a modal message loop.
509 bool os_modal_loop_;
510 #endif
512 // The next sequence number to use for delayed tasks. Updating this counter is
513 // protected by incoming_queue_lock_.
514 int next_sequence_num_;
516 ObserverList<TaskObserver> task_observers_;
518 // The message loop proxy associated with this message loop, if one exists.
519 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_;
520 scoped_ptr<base::ThreadTaskRunnerHandle> thread_task_runner_handle_;
522 template <class T, class R> friend class base::subtle::DeleteHelperInternal;
523 template <class T, class R> friend class base::subtle::ReleaseHelperInternal;
525 void DeleteSoonInternal(const tracked_objects::Location& from_here,
526 void(*deleter)(const void*),
527 const void* object);
528 void ReleaseSoonInternal(const tracked_objects::Location& from_here,
529 void(*releaser)(const void*),
530 const void* object);
532 DISALLOW_COPY_AND_ASSIGN(MessageLoop);
535 //-----------------------------------------------------------------------------
536 // MessageLoopForUI extends MessageLoop with methods that are particular to a
537 // MessageLoop instantiated with TYPE_UI.
539 // This class is typically used like so:
540 // MessageLoopForUI::current()->...call some method...
542 class BASE_EXPORT MessageLoopForUI : public MessageLoop {
543 public:
544 #if defined(OS_WIN)
545 typedef base::MessagePumpForUI::MessageFilter MessageFilter;
546 #endif
548 MessageLoopForUI() : MessageLoop(TYPE_UI) {
551 // Returns the MessageLoopForUI of the current thread.
552 static MessageLoopForUI* current() {
553 MessageLoop* loop = MessageLoop::current();
554 DCHECK(loop);
555 DCHECK_EQ(MessageLoop::TYPE_UI, loop->type());
556 return static_cast<MessageLoopForUI*>(loop);
559 #if defined(OS_WIN)
560 void DidProcessMessage(const MSG& message);
561 #endif // defined(OS_WIN)
563 #if defined(OS_IOS)
564 // On iOS, the main message loop cannot be Run(). Instead call Attach(),
565 // which connects this MessageLoop to the UI thread's CFRunLoop and allows
566 // PostTask() to work.
567 void Attach();
568 #endif
570 #if defined(OS_ANDROID)
571 // On Android, the UI message loop is handled by Java side. So Run() should
572 // never be called. Instead use Start(), which will forward all the native UI
573 // events to the Java message loop.
574 void Start();
575 #elif !defined(OS_MACOSX)
576 // Please see message_pump_win/message_pump_glib for definitions of these
577 // methods.
578 void AddObserver(Observer* observer);
579 void RemoveObserver(Observer* observer);
581 #if defined(OS_WIN)
582 // Plese see MessagePumpForUI for definitions of this method.
583 void SetMessageFilter(scoped_ptr<MessageFilter> message_filter) {
584 pump_ui()->SetMessageFilter(message_filter.Pass());
586 #endif
588 protected:
589 #if defined(USE_AURA) && defined(USE_X11) && !defined(OS_NACL)
590 friend class base::MessagePumpAuraX11;
591 #endif
593 // TODO(rvargas): Make this platform independent.
594 base::MessagePumpForUI* pump_ui() {
595 return static_cast<base::MessagePumpForUI*>(pump_.get());
597 #endif // !defined(OS_MACOSX)
600 // Do not add any member variables to MessageLoopForUI! This is important b/c
601 // MessageLoopForUI is often allocated via MessageLoop(TYPE_UI). Any extra
602 // data that you need should be stored on the MessageLoop's pump_ instance.
603 COMPILE_ASSERT(sizeof(MessageLoop) == sizeof(MessageLoopForUI),
604 MessageLoopForUI_should_not_have_extra_member_variables);
606 //-----------------------------------------------------------------------------
607 // MessageLoopForIO extends MessageLoop with methods that are particular to a
608 // MessageLoop instantiated with TYPE_IO.
610 // This class is typically used like so:
611 // MessageLoopForIO::current()->...call some method...
613 class BASE_EXPORT MessageLoopForIO : public MessageLoop {
614 public:
615 #if defined(OS_WIN)
616 typedef base::MessagePumpForIO::IOHandler IOHandler;
617 typedef base::MessagePumpForIO::IOContext IOContext;
618 typedef base::MessagePumpForIO::IOObserver IOObserver;
619 #elif defined(OS_IOS)
620 typedef base::MessagePumpIOSForIO::Watcher Watcher;
621 typedef base::MessagePumpIOSForIO::FileDescriptorWatcher
622 FileDescriptorWatcher;
623 typedef base::MessagePumpIOSForIO::IOObserver IOObserver;
625 enum Mode {
626 WATCH_READ = base::MessagePumpIOSForIO::WATCH_READ,
627 WATCH_WRITE = base::MessagePumpIOSForIO::WATCH_WRITE,
628 WATCH_READ_WRITE = base::MessagePumpIOSForIO::WATCH_READ_WRITE
630 #elif defined(OS_POSIX)
631 typedef base::MessagePumpLibevent::Watcher Watcher;
632 typedef base::MessagePumpLibevent::FileDescriptorWatcher
633 FileDescriptorWatcher;
634 typedef base::MessagePumpLibevent::IOObserver IOObserver;
636 enum Mode {
637 WATCH_READ = base::MessagePumpLibevent::WATCH_READ,
638 WATCH_WRITE = base::MessagePumpLibevent::WATCH_WRITE,
639 WATCH_READ_WRITE = base::MessagePumpLibevent::WATCH_READ_WRITE
642 #endif
644 MessageLoopForIO() : MessageLoop(TYPE_IO) {
647 // Returns the MessageLoopForIO of the current thread.
648 static MessageLoopForIO* current() {
649 MessageLoop* loop = MessageLoop::current();
650 DCHECK_EQ(MessageLoop::TYPE_IO, loop->type());
651 return static_cast<MessageLoopForIO*>(loop);
654 void AddIOObserver(IOObserver* io_observer) {
655 pump_io()->AddIOObserver(io_observer);
658 void RemoveIOObserver(IOObserver* io_observer) {
659 pump_io()->RemoveIOObserver(io_observer);
662 #if defined(OS_WIN)
663 // Please see MessagePumpWin for definitions of these methods.
664 void RegisterIOHandler(HANDLE file, IOHandler* handler);
665 bool RegisterJobObject(HANDLE job, IOHandler* handler);
666 bool WaitForIOCompletion(DWORD timeout, IOHandler* filter);
668 protected:
669 // TODO(rvargas): Make this platform independent.
670 base::MessagePumpForIO* pump_io() {
671 return static_cast<base::MessagePumpForIO*>(pump_.get());
674 #elif defined(OS_IOS)
675 // Please see MessagePumpIOSForIO for definition.
676 bool WatchFileDescriptor(int fd,
677 bool persistent,
678 Mode mode,
679 FileDescriptorWatcher *controller,
680 Watcher *delegate);
682 private:
683 base::MessagePumpIOSForIO* pump_io() {
684 return static_cast<base::MessagePumpIOSForIO*>(pump_.get());
687 #elif defined(OS_POSIX)
688 // Please see MessagePumpLibevent for definition.
689 bool WatchFileDescriptor(int fd,
690 bool persistent,
691 Mode mode,
692 FileDescriptorWatcher* controller,
693 Watcher* delegate);
695 private:
696 base::MessagePumpLibevent* pump_io() {
697 return static_cast<base::MessagePumpLibevent*>(pump_.get());
699 #endif // defined(OS_POSIX)
702 // Do not add any member variables to MessageLoopForIO! This is important b/c
703 // MessageLoopForIO is often allocated via MessageLoop(TYPE_IO). Any extra
704 // data that you need should be stored on the MessageLoop's pump_ instance.
705 COMPILE_ASSERT(sizeof(MessageLoop) == sizeof(MessageLoopForIO),
706 MessageLoopForIO_should_not_have_extra_member_variables);
708 } // namespace base
710 // TODO(brettw) remove this when all users are updated to explicitly use the
711 // namespace
712 using base::MessageLoop;
713 using base::MessageLoopForIO;
714 using base::MessageLoopForUI;
716 #endif // BASE_MESSAGE_LOOP_H_