Don't switch browser locale on secondary user login
[chromium-blink-merge.git] / base / message_loop / message_loop.h
blob3520b72516f4dfc80c8286f7b8bb64d4606c7cfa
1 // 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.
5 #ifndef BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_
6 #define BASE_MESSAGE_LOOP_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/memory/scoped_ptr.h"
17 #include "base/message_loop/incoming_task_queue.h"
18 #include "base/message_loop/message_loop_proxy.h"
19 #include "base/message_loop/message_loop_proxy_impl.h"
20 #include "base/message_loop/message_pump.h"
21 #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"
25 #include "base/time/time.h"
26 #include "base/tracking_info.h"
28 #if defined(OS_WIN)
29 // We need this to declare base::MessagePumpWin::Dispatcher, which we should
30 // really just eliminate.
31 #include "base/message_loop/message_pump_win.h"
32 #elif defined(OS_IOS)
33 #include "base/message_loop/message_pump_io_ios.h"
34 #elif defined(OS_POSIX)
35 #include "base/message_loop/message_pump_libevent.h"
36 #if !defined(OS_MACOSX) && !defined(OS_ANDROID)
38 #if defined(USE_AURA) && defined(USE_X11) && !defined(OS_NACL)
39 #include "base/message_loop/message_pump_x11.h"
40 #elif defined(USE_OZONE) && !defined(OS_NACL)
41 #include "base/message_loop/message_pump_ozone.h"
42 #else
43 #define USE_GTK_MESSAGE_PUMP
44 #include "base/message_loop/message_pump_gtk.h"
45 #if defined(TOOLKIT_GTK)
46 #include "base/message_loop/message_pump_x11.h"
47 #endif
48 #endif
50 #endif
51 #endif
53 namespace base {
55 class HistogramBase;
56 class MessagePumpDispatcher;
57 class MessagePumpObserver;
58 class RunLoop;
59 class ThreadTaskRunnerHandle;
60 #if defined(OS_ANDROID)
61 class MessagePumpForUI;
62 #endif
63 class WaitableEvent;
65 // A MessageLoop is used to process events for a particular thread. There is
66 // at most one MessageLoop instance per thread.
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.
74 // NOTE: Unless otherwise specified, a MessageLoop's methods may only be called
75 // on the thread where the MessageLoop's Run method executes.
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.
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()).
93 // Please be SURE your task is reentrant (nestable) and all global variables
94 // are stable and accessible before calling SetNestableTasksAllowed(true).
96 class BASE_EXPORT MessageLoop : public MessagePump::Delegate {
97 public:
99 #if defined(USE_GTK_MESSAGE_PUMP)
100 typedef MessagePumpGdkObserver Observer;
101 #elif !defined(OS_MACOSX) && !defined(OS_ANDROID)
102 typedef MessagePumpDispatcher Dispatcher;
103 typedef MessagePumpObserver Observer;
104 #endif
106 // A MessageLoop has a particular type, which indicates the set of
107 // asynchronous events it may process in addition to tasks and timers.
109 // TYPE_DEFAULT
110 // This type of ML only supports tasks and timers.
112 // TYPE_UI
113 // This type of ML also supports native UI events (e.g., Windows messages).
114 // See also MessageLoopForUI.
116 // TYPE_GPU
117 // This type of ML also supports native UI events for use in the GPU
118 // process. On Linux this will always be an X11 ML (as compared with the
119 // sometimes-GTK ML in the browser process).
121 // TYPE_IO
122 // This type of ML also supports asynchronous IO. See also
123 // MessageLoopForIO.
125 // TYPE_JAVA
126 // This type of ML is backed by a Java message handler which is responsible
127 // for running the tasks added to the ML. This is only for use on Android.
128 // TYPE_JAVA behaves in essence like TYPE_UI, except during construction
129 // where it does not use the main thread specific pump factory.
131 enum Type {
132 TYPE_DEFAULT,
133 TYPE_UI,
134 #if defined(TOOLKIT_GTK)
135 TYPE_GPU,
136 #endif
137 TYPE_IO,
138 #if defined(OS_ANDROID)
139 TYPE_JAVA,
140 #endif // defined(OS_ANDROID)
143 // Normally, it is not necessary to instantiate a MessageLoop. Instead, it
144 // is typical to make use of the current thread's MessageLoop instance.
145 explicit MessageLoop(Type type = TYPE_DEFAULT);
146 virtual ~MessageLoop();
148 // Returns the MessageLoop object for the current thread, or null if none.
149 static MessageLoop* current();
151 static void EnableHistogrammer(bool enable_histogrammer);
153 typedef MessagePump* (MessagePumpFactory)();
154 // Uses the given base::MessagePumpForUIFactory to override the default
155 // MessagePump implementation for 'TYPE_UI'. Returns true if the factory
156 // was successfully registered.
157 static bool InitMessagePumpForUIFactory(MessagePumpFactory* factory);
159 // A DestructionObserver is notified when the current MessageLoop is being
160 // destroyed. These observers are notified prior to MessageLoop::current()
161 // being changed to return NULL. This gives interested parties the chance to
162 // do final cleanup that depends on the MessageLoop.
164 // NOTE: Any tasks posted to the MessageLoop during this notification will
165 // not be run. Instead, they will be deleted.
167 class BASE_EXPORT DestructionObserver {
168 public:
169 virtual void WillDestroyCurrentMessageLoop() = 0;
171 protected:
172 virtual ~DestructionObserver();
175 // Add a DestructionObserver, which will start receiving notifications
176 // immediately.
177 void AddDestructionObserver(DestructionObserver* destruction_observer);
179 // Remove a DestructionObserver. It is safe to call this method while a
180 // DestructionObserver is receiving a notification callback.
181 void RemoveDestructionObserver(DestructionObserver* destruction_observer);
183 // The "PostTask" family of methods call the task's Run method asynchronously
184 // from within a message loop at some point in the future.
186 // With the PostTask variant, tasks are invoked in FIFO order, inter-mixed
187 // with normal UI or IO event processing. With the PostDelayedTask variant,
188 // tasks are called after at least approximately 'delay_ms' have elapsed.
190 // The NonNestable variants work similarly except that they promise never to
191 // dispatch the task from a nested invocation of MessageLoop::Run. Instead,
192 // such tasks get deferred until the top-most MessageLoop::Run is executing.
194 // The MessageLoop takes ownership of the Task, and deletes it after it has
195 // been Run().
197 // PostTask(from_here, task) is equivalent to
198 // PostDelayedTask(from_here, task, 0).
200 // The TryPostTask is meant for the cases where the calling thread cannot
201 // block. If posting the task will block, the call returns false, the task
202 // is not posted but the task is consumed anyways.
204 // NOTE: These methods may be called on any thread. The Task will be invoked
205 // on the thread that executes MessageLoop::Run().
206 void PostTask(const tracked_objects::Location& from_here,
207 const Closure& task);
209 bool TryPostTask(const tracked_objects::Location& from_here,
210 const Closure& task);
212 void PostDelayedTask(const tracked_objects::Location& from_here,
213 const Closure& task,
214 TimeDelta delay);
216 void PostNonNestableTask(const tracked_objects::Location& from_here,
217 const Closure& task);
219 void PostNonNestableDelayedTask(const tracked_objects::Location& from_here,
220 const Closure& task,
221 TimeDelta delay);
223 // A variant on PostTask that deletes the given object. This is useful
224 // if the object needs to live until the next run of the MessageLoop (for
225 // example, deleting a RenderProcessHost from within an IPC callback is not
226 // good).
228 // NOTE: This method may be called on any thread. The object will be deleted
229 // on the thread that executes MessageLoop::Run(). If this is not the same
230 // as the thread that calls PostDelayedTask(FROM_HERE, ), then T MUST inherit
231 // from RefCountedThreadSafe<T>!
232 template <class T>
233 void DeleteSoon(const tracked_objects::Location& from_here, const T* object) {
234 base::subtle::DeleteHelperInternal<T, void>::DeleteViaSequencedTaskRunner(
235 this, from_here, object);
238 // A variant on PostTask that releases the given reference counted object
239 // (by calling its Release method). This is useful if the object needs to
240 // live until the next run of the MessageLoop, or if the object needs to be
241 // released on a particular thread.
243 // NOTE: This method may be called on any thread. The object will be
244 // released (and thus possibly deleted) on the thread that executes
245 // MessageLoop::Run(). If this is not the same as the thread that calls
246 // PostDelayedTask(FROM_HERE, ), then T MUST inherit from
247 // RefCountedThreadSafe<T>!
248 template <class T>
249 void ReleaseSoon(const tracked_objects::Location& from_here,
250 const T* object) {
251 base::subtle::ReleaseHelperInternal<T, void>::ReleaseViaSequencedTaskRunner(
252 this, from_here, object);
255 // Deprecated: use RunLoop instead.
256 // Run the message loop.
257 void Run();
259 // Deprecated: use RunLoop instead.
260 // Process all pending tasks, windows messages, etc., but don't wait/sleep.
261 // Return as soon as all items that can be run are taken care of.
262 void RunUntilIdle();
264 // TODO(jbates) remove this. crbug.com/131220. See QuitWhenIdle().
265 void Quit() { QuitWhenIdle(); }
267 // Deprecated: use RunLoop instead.
269 // Signals the Run method to return when it becomes idle. It will continue to
270 // process pending messages and future messages as long as they are enqueued.
271 // Warning: if the MessageLoop remains busy, it may never quit. Only use this
272 // Quit method when looping procedures (such as web pages) have been shut
273 // down.
275 // This method may only be called on the same thread that called Run, and Run
276 // must still be on the call stack.
278 // Use QuitClosure variants if you need to Quit another thread's MessageLoop,
279 // but note that doing so is fairly dangerous if the target thread makes
280 // nested calls to MessageLoop::Run. The problem being that you won't know
281 // which nested run loop you are quitting, so be careful!
282 void QuitWhenIdle();
284 // Deprecated: use RunLoop instead.
286 // This method is a variant of Quit, that does not wait for pending messages
287 // to be processed before returning from Run.
288 void QuitNow();
290 // TODO(jbates) remove this. crbug.com/131220. See QuitWhenIdleClosure().
291 static Closure QuitClosure() { return QuitWhenIdleClosure(); }
293 // Deprecated: use RunLoop instead.
294 // Construct a Closure that will call QuitWhenIdle(). Useful to schedule an
295 // arbitrary MessageLoop to QuitWhenIdle.
296 static Closure QuitWhenIdleClosure();
298 // Returns true if this loop is |type|. This allows subclasses (especially
299 // those in tests) to specialize how they are identified.
300 virtual bool IsType(Type type) const;
302 // Returns the type passed to the constructor.
303 Type type() const { return type_; }
305 // Optional call to connect the thread name with this loop.
306 void set_thread_name(const std::string& thread_name) {
307 DCHECK(thread_name_.empty()) << "Should not rename this thread!";
308 thread_name_ = thread_name;
310 const std::string& thread_name() const { return thread_name_; }
312 // Gets the message loop proxy associated with this message loop.
313 scoped_refptr<MessageLoopProxy> message_loop_proxy() {
314 return message_loop_proxy_;
317 // Enables or disables the recursive task processing. This happens in the case
318 // of recursive message loops. Some unwanted message loop may occurs when
319 // using common controls or printer functions. By default, recursive task
320 // processing is disabled.
322 // Please utilize |ScopedNestableTaskAllower| instead of calling these methods
323 // directly. In general nestable message loops are to be avoided. They are
324 // dangerous and difficult to get right, so please use with extreme caution.
326 // The specific case where tasks get queued is:
327 // - The thread is running a message loop.
328 // - It receives a task #1 and execute it.
329 // - The task #1 implicitly start a message loop, like a MessageBox in the
330 // unit test. This can also be StartDoc or GetSaveFileName.
331 // - The thread receives a task #2 before or while in this second message
332 // loop.
333 // - With NestableTasksAllowed set to true, the task #2 will run right away.
334 // Otherwise, it will get executed right after task #1 completes at "thread
335 // message loop level".
336 void SetNestableTasksAllowed(bool allowed);
337 bool NestableTasksAllowed() const;
339 // Enables nestable tasks on |loop| while in scope.
340 class ScopedNestableTaskAllower {
341 public:
342 explicit ScopedNestableTaskAllower(MessageLoop* loop)
343 : loop_(loop),
344 old_state_(loop_->NestableTasksAllowed()) {
345 loop_->SetNestableTasksAllowed(true);
347 ~ScopedNestableTaskAllower() {
348 loop_->SetNestableTasksAllowed(old_state_);
351 private:
352 MessageLoop* loop_;
353 bool old_state_;
356 // Enables or disables the restoration during an exception of the unhandled
357 // exception filter that was active when Run() was called. This can happen
358 // if some third party code call SetUnhandledExceptionFilter() and never
359 // restores the previous filter.
360 void set_exception_restoration(bool restore) {
361 exception_restoration_ = restore;
364 // Returns true if we are currently running a nested message loop.
365 bool IsNested();
367 // A TaskObserver is an object that receives task notifications from the
368 // MessageLoop.
370 // NOTE: A TaskObserver implementation should be extremely fast!
371 class BASE_EXPORT TaskObserver {
372 public:
373 TaskObserver();
375 // This method is called before processing a task.
376 virtual void WillProcessTask(const PendingTask& pending_task) = 0;
378 // This method is called after processing a task.
379 virtual void DidProcessTask(const PendingTask& pending_task) = 0;
381 protected:
382 virtual ~TaskObserver();
385 // These functions can only be called on the same thread that |this| is
386 // running on.
387 void AddTaskObserver(TaskObserver* task_observer);
388 void RemoveTaskObserver(TaskObserver* task_observer);
390 // When we go into high resolution timer mode, we will stay in hi-res mode
391 // for at least 1s.
392 static const int kHighResolutionTimerModeLeaseTimeMs = 1000;
394 #if defined(OS_WIN)
395 void set_os_modal_loop(bool os_modal_loop) {
396 os_modal_loop_ = os_modal_loop;
399 bool os_modal_loop() const {
400 return os_modal_loop_;
402 #endif // OS_WIN
404 // Can only be called from the thread that owns the MessageLoop.
405 bool is_running() const;
407 // Returns true if the message loop has high resolution timers enabled.
408 // Provided for testing.
409 bool IsHighResolutionTimerEnabledForTesting();
411 // Returns true if the message loop is "idle". Provided for testing.
412 bool IsIdleForTesting();
414 // Takes the incoming queue lock, signals |caller_wait| and waits until
415 // |caller_signal| is signalled.
416 void LockWaitUnLockForTesting(WaitableEvent* caller_wait,
417 WaitableEvent* caller_signal);
419 //----------------------------------------------------------------------------
420 protected:
422 #if defined(OS_WIN)
423 MessagePumpWin* pump_win() {
424 return static_cast<MessagePumpWin*>(pump_.get());
426 #elif defined(OS_POSIX) && !defined(OS_IOS)
427 MessagePumpLibevent* pump_libevent() {
428 return static_cast<MessagePumpLibevent*>(pump_.get());
430 #if defined(TOOLKIT_GTK)
431 friend class MessagePumpX11;
432 MessagePumpX11* pump_gpu() {
433 DCHECK_EQ(TYPE_GPU, type());
434 return static_cast<MessagePumpX11*>(pump_.get());
436 #endif
437 #endif
439 scoped_ptr<MessagePump> pump_;
441 private:
442 friend class internal::IncomingTaskQueue;
443 friend class RunLoop;
445 // A function to encapsulate all the exception handling capability in the
446 // stacks around the running of a main message loop. It will run the message
447 // loop in a SEH try block or not depending on the set_SEH_restoration()
448 // flag invoking respectively RunInternalInSEHFrame() or RunInternal().
449 void RunHandler();
451 #if defined(OS_WIN)
452 __declspec(noinline) void RunInternalInSEHFrame();
453 #endif
455 // A surrounding stack frame around the running of the message loop that
456 // supports all saving and restoring of state, as is needed for any/all (ugly)
457 // recursive calls.
458 void RunInternal();
460 // Called to process any delayed non-nestable tasks.
461 bool ProcessNextDelayedNonNestableTask();
463 // Runs the specified PendingTask.
464 void RunTask(const PendingTask& pending_task);
466 // Calls RunTask or queues the pending_task on the deferred task list if it
467 // cannot be run right now. Returns true if the task was run.
468 bool DeferOrRunPendingTask(const PendingTask& pending_task);
470 // Adds the pending task to delayed_work_queue_.
471 void AddToDelayedWorkQueue(const PendingTask& pending_task);
473 // Delete tasks that haven't run yet without running them. Used in the
474 // destructor to make sure all the task's destructors get called. Returns
475 // true if some work was done.
476 bool DeletePendingTasks();
478 // Creates a process-wide unique ID to represent this task in trace events.
479 // This will be mangled with a Process ID hash to reduce the likelyhood of
480 // colliding with MessageLoop pointers on other processes.
481 uint64 GetTaskTraceID(const PendingTask& task);
483 // Loads tasks from the incoming queue to |work_queue_| if the latter is
484 // empty.
485 void ReloadWorkQueue();
487 // Wakes up the message pump. Can be called on any thread. The caller is
488 // responsible for synchronizing ScheduleWork() calls.
489 void ScheduleWork(bool was_empty);
491 // Start recording histogram info about events and action IF it was enabled
492 // and IF the statistics recorder can accept a registration of our histogram.
493 void StartHistogrammer();
495 // Add occurrence of event to our histogram, so that we can see what is being
496 // done in a specific MessageLoop instance (i.e., specific thread).
497 // If message_histogram_ is NULL, this is a no-op.
498 void HistogramEvent(int event);
500 // MessagePump::Delegate methods:
501 virtual bool DoWork() OVERRIDE;
502 virtual bool DoDelayedWork(TimeTicks* next_delayed_work_time) OVERRIDE;
503 virtual bool DoIdleWork() OVERRIDE;
504 virtual void GetQueueingInformation(size_t* queue_size,
505 TimeDelta* queueing_delay) OVERRIDE;
507 Type type_;
509 // A list of tasks that need to be processed by this instance. Note that
510 // this queue is only accessed (push/pop) by our current thread.
511 TaskQueue work_queue_;
513 // Contains delayed tasks, sorted by their 'delayed_run_time' property.
514 DelayedTaskQueue delayed_work_queue_;
516 // A recent snapshot of Time::Now(), used to check delayed_work_queue_.
517 TimeTicks recent_time_;
519 // A queue of non-nestable tasks that we had to defer because when it came
520 // time to execute them we were in a nested message loop. They will execute
521 // once we're out of nested message loops.
522 TaskQueue deferred_non_nestable_work_queue_;
524 ObserverList<DestructionObserver> destruction_observers_;
526 bool exception_restoration_;
528 // A recursion block that prevents accidentally running additional tasks when
529 // insider a (accidentally induced?) nested message pump.
530 bool nestable_tasks_allowed_;
532 #if defined(OS_WIN)
533 // Should be set to true before calling Windows APIs like TrackPopupMenu, etc
534 // which enter a modal message loop.
535 bool os_modal_loop_;
536 #endif
538 std::string thread_name_;
539 // A profiling histogram showing the counts of various messages and events.
540 HistogramBase* message_histogram_;
542 RunLoop* run_loop_;
544 ObserverList<TaskObserver> task_observers_;
546 scoped_refptr<internal::IncomingTaskQueue> incoming_task_queue_;
548 // The message loop proxy associated with this message loop.
549 scoped_refptr<internal::MessageLoopProxyImpl> message_loop_proxy_;
550 scoped_ptr<ThreadTaskRunnerHandle> thread_task_runner_handle_;
552 template <class T, class R> friend class base::subtle::DeleteHelperInternal;
553 template <class T, class R> friend class base::subtle::ReleaseHelperInternal;
555 void DeleteSoonInternal(const tracked_objects::Location& from_here,
556 void(*deleter)(const void*),
557 const void* object);
558 void ReleaseSoonInternal(const tracked_objects::Location& from_here,
559 void(*releaser)(const void*),
560 const void* object);
562 DISALLOW_COPY_AND_ASSIGN(MessageLoop);
565 //-----------------------------------------------------------------------------
566 // MessageLoopForUI extends MessageLoop with methods that are particular to a
567 // MessageLoop instantiated with TYPE_UI.
569 // This class is typically used like so:
570 // MessageLoopForUI::current()->...call some method...
572 class BASE_EXPORT MessageLoopForUI : public MessageLoop {
573 public:
574 #if defined(OS_WIN)
575 typedef MessagePumpForUI::MessageFilter MessageFilter;
576 #endif
578 MessageLoopForUI() : MessageLoop(TYPE_UI) {
581 // Returns the MessageLoopForUI of the current thread.
582 static MessageLoopForUI* current() {
583 MessageLoop* loop = MessageLoop::current();
584 DCHECK(loop);
585 DCHECK_EQ(MessageLoop::TYPE_UI, loop->type());
586 return static_cast<MessageLoopForUI*>(loop);
589 #if defined(OS_WIN)
590 void DidProcessMessage(const MSG& message);
591 #endif // defined(OS_WIN)
593 #if defined(OS_IOS)
594 // On iOS, the main message loop cannot be Run(). Instead call Attach(),
595 // which connects this MessageLoop to the UI thread's CFRunLoop and allows
596 // PostTask() to work.
597 void Attach();
598 #endif
600 #if defined(OS_ANDROID)
601 // On Android, the UI message loop is handled by Java side. So Run() should
602 // never be called. Instead use Start(), which will forward all the native UI
603 // events to the Java message loop.
604 void Start();
605 #elif !defined(OS_MACOSX)
607 // Please see message_pump_win/message_pump_glib for definitions of these
608 // methods.
609 void AddObserver(Observer* observer);
610 void RemoveObserver(Observer* observer);
612 #if defined(OS_WIN)
613 // Plese see MessagePumpForUI for definitions of this method.
614 void SetMessageFilter(scoped_ptr<MessageFilter> message_filter) {
615 pump_ui()->SetMessageFilter(message_filter.Pass());
617 #endif
619 protected:
620 #if defined(USE_X11)
621 friend class MessagePumpX11;
622 #endif
623 #if defined(USE_OZONE) && !defined(OS_NACL)
624 friend class MessagePumpOzone;
625 #endif
627 // TODO(rvargas): Make this platform independent.
628 MessagePumpForUI* pump_ui() {
629 return static_cast<MessagePumpForUI*>(pump_.get());
631 #endif // !defined(OS_MACOSX)
634 // Do not add any member variables to MessageLoopForUI! This is important b/c
635 // MessageLoopForUI is often allocated via MessageLoop(TYPE_UI). Any extra
636 // data that you need should be stored on the MessageLoop's pump_ instance.
637 COMPILE_ASSERT(sizeof(MessageLoop) == sizeof(MessageLoopForUI),
638 MessageLoopForUI_should_not_have_extra_member_variables);
640 //-----------------------------------------------------------------------------
641 // MessageLoopForIO extends MessageLoop with methods that are particular to a
642 // MessageLoop instantiated with TYPE_IO.
644 // This class is typically used like so:
645 // MessageLoopForIO::current()->...call some method...
647 class BASE_EXPORT MessageLoopForIO : public MessageLoop {
648 public:
649 #if defined(OS_WIN)
650 typedef MessagePumpForIO::IOHandler IOHandler;
651 typedef MessagePumpForIO::IOContext IOContext;
652 typedef MessagePumpForIO::IOObserver IOObserver;
653 #elif defined(OS_IOS)
654 typedef MessagePumpIOSForIO::Watcher Watcher;
655 typedef MessagePumpIOSForIO::FileDescriptorWatcher
656 FileDescriptorWatcher;
657 typedef MessagePumpIOSForIO::IOObserver IOObserver;
659 enum Mode {
660 WATCH_READ = MessagePumpIOSForIO::WATCH_READ,
661 WATCH_WRITE = MessagePumpIOSForIO::WATCH_WRITE,
662 WATCH_READ_WRITE = MessagePumpIOSForIO::WATCH_READ_WRITE
664 #elif defined(OS_POSIX)
665 typedef MessagePumpLibevent::Watcher Watcher;
666 typedef MessagePumpLibevent::FileDescriptorWatcher
667 FileDescriptorWatcher;
668 typedef MessagePumpLibevent::IOObserver IOObserver;
670 enum Mode {
671 WATCH_READ = MessagePumpLibevent::WATCH_READ,
672 WATCH_WRITE = MessagePumpLibevent::WATCH_WRITE,
673 WATCH_READ_WRITE = MessagePumpLibevent::WATCH_READ_WRITE
676 #endif
678 MessageLoopForIO() : MessageLoop(TYPE_IO) {
681 // Returns the MessageLoopForIO of the current thread.
682 static MessageLoopForIO* current() {
683 MessageLoop* loop = MessageLoop::current();
684 DCHECK_EQ(MessageLoop::TYPE_IO, loop->type());
685 return static_cast<MessageLoopForIO*>(loop);
688 void AddIOObserver(IOObserver* io_observer) {
689 pump_io()->AddIOObserver(io_observer);
692 void RemoveIOObserver(IOObserver* io_observer) {
693 pump_io()->RemoveIOObserver(io_observer);
696 #if defined(OS_WIN)
697 // Please see MessagePumpWin for definitions of these methods.
698 void RegisterIOHandler(HANDLE file, IOHandler* handler);
699 bool RegisterJobObject(HANDLE job, IOHandler* handler);
700 bool WaitForIOCompletion(DWORD timeout, IOHandler* filter);
702 protected:
703 // TODO(rvargas): Make this platform independent.
704 MessagePumpForIO* pump_io() {
705 return static_cast<MessagePumpForIO*>(pump_.get());
708 #elif defined(OS_IOS)
709 // Please see MessagePumpIOSForIO for definition.
710 bool WatchFileDescriptor(int fd,
711 bool persistent,
712 Mode mode,
713 FileDescriptorWatcher *controller,
714 Watcher *delegate);
716 private:
717 MessagePumpIOSForIO* pump_io() {
718 return static_cast<MessagePumpIOSForIO*>(pump_.get());
721 #elif defined(OS_POSIX)
722 // Please see MessagePumpLibevent for definition.
723 bool WatchFileDescriptor(int fd,
724 bool persistent,
725 Mode mode,
726 FileDescriptorWatcher* controller,
727 Watcher* delegate);
729 private:
730 MessagePumpLibevent* pump_io() {
731 return static_cast<MessagePumpLibevent*>(pump_.get());
733 #endif // defined(OS_POSIX)
736 // Do not add any member variables to MessageLoopForIO! This is important b/c
737 // MessageLoopForIO is often allocated via MessageLoop(TYPE_IO). Any extra
738 // data that you need should be stored on the MessageLoop's pump_ instance.
739 COMPILE_ASSERT(sizeof(MessageLoop) == sizeof(MessageLoopForIO),
740 MessageLoopForIO_should_not_have_extra_member_variables);
742 } // namespace base
744 #endif // BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_