blob: e3f07bff21c07643772351c6a48c41da3b1ea7e2 [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// Copyright (c) 2006-2008 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.
initial.commitd7cae122008-07-26 21:49:384
[email protected]fc7fb6e2008-08-16 03:09:055#ifndef BASE_MESSAGE_LOOP_H_
6#define BASE_MESSAGE_LOOP_H_
initial.commitd7cae122008-07-26 21:49:387
initial.commitd7cae122008-07-26 21:49:388#include <queue>
9#include <string>
initial.commitd7cae122008-07-26 21:49:3810
11#include "base/histogram.h"
[email protected]fc7fb6e2008-08-16 03:09:0512#include "base/message_pump.h"
initial.commitd7cae122008-07-26 21:49:3813#include "base/observer_list.h"
[email protected]fc7fb6e2008-08-16 03:09:0514#include "base/ref_counted.h"
[email protected]1fec8402009-03-13 19:11:5915#include "base/scoped_ptr.h"
initial.commitd7cae122008-07-26 21:49:3816#include "base/task.h"
initial.commitd7cae122008-07-26 21:49:3817
[email protected]fc7fb6e2008-08-16 03:09:0518#if defined(OS_WIN)
19// We need this to declare base::MessagePumpWin::Dispatcher, which we should
20// really just eliminate.
21#include "base/message_pump_win.h"
[email protected]36987e92008-09-18 18:46:2622#elif defined(OS_POSIX)
23#include "base/message_pump_libevent.h"
[email protected]fc7fb6e2008-08-16 03:09:0524#endif
[email protected]faabcf42009-05-18 21:12:3425#if defined(OS_LINUX)
26#include "base/message_pump_glib.h"
27#endif
[email protected]ea15e982008-08-15 07:31:2028
[email protected]fc7fb6e2008-08-16 03:09:0529// A MessageLoop is used to process events for a particular thread. There is
30// at most one MessageLoop instance per thread.
31//
32// Events include at a minimum Task instances submitted to PostTask or those
33// managed by TimerManager. Depending on the type of message pump used by the
34// MessageLoop other events such as UI messages may be processed. On Windows
35// APC calls (as time permits) and signals sent to a registered set of HANDLEs
36// may also be processed.
initial.commitd7cae122008-07-26 21:49:3837//
38// NOTE: Unless otherwise specified, a MessageLoop's methods may only be called
39// on the thread where the MessageLoop's Run method executes.
40//
[email protected]fc7fb6e2008-08-16 03:09:0541// NOTE: MessageLoop has task reentrancy protection. This means that if a
initial.commitd7cae122008-07-26 21:49:3842// task is being processed, a second task cannot start until the first task is
[email protected]fc7fb6e2008-08-16 03:09:0543// finished. Reentrancy can happen when processing a task, and an inner
44// message pump is created. That inner pump then processes native messages
45// which could implicitly start an inner task. Inner message pumps are created
46// with dialogs (DialogBox), common dialogs (GetOpenFileName), OLE functions
47// (DoDragDrop), printer functions (StartDoc) and *many* others.
48//
initial.commitd7cae122008-07-26 21:49:3849// Sample workaround when inner task processing is needed:
50// bool old_state = MessageLoop::current()->NestableTasksAllowed();
51// MessageLoop::current()->SetNestableTasksAllowed(true);
52// HRESULT hr = DoDragDrop(...); // Implicitly runs a modal message loop here.
53// MessageLoop::current()->SetNestableTasksAllowed(old_state);
54// // Process hr (the result returned by DoDragDrop().
55//
[email protected]fc7fb6e2008-08-16 03:09:0556// Please be SURE your task is reentrant (nestable) and all global variables
57// are stable and accessible before calling SetNestableTasksAllowed(true).
initial.commitd7cae122008-07-26 21:49:3858//
[email protected]fc7fb6e2008-08-16 03:09:0559class MessageLoop : public base::MessagePump::Delegate {
initial.commitd7cae122008-07-26 21:49:3860 public:
initial.commitd7cae122008-07-26 21:49:3861 static void EnableHistogrammer(bool enable_histogrammer);
62
[email protected]ee132ad2008-08-06 21:27:0263 // A DestructionObserver is notified when the current MessageLoop is being
64 // destroyed. These obsevers are notified prior to MessageLoop::current()
65 // being changed to return NULL. This gives interested parties the chance to
66 // do final cleanup that depends on the MessageLoop.
67 //
68 // NOTE: Any tasks posted to the MessageLoop during this notification will
69 // not be run. Instead, they will be deleted.
70 //
71 class DestructionObserver {
72 public:
73 virtual ~DestructionObserver() {}
74 virtual void WillDestroyCurrentMessageLoop() = 0;
75 };
76
77 // Add a DestructionObserver, which will start receiving notifications
78 // immediately.
79 void AddDestructionObserver(DestructionObserver* destruction_observer);
80
81 // Remove a DestructionObserver. It is safe to call this method while a
82 // DestructionObserver is receiving a notification callback.
83 void RemoveDestructionObserver(DestructionObserver* destruction_observer);
84
[email protected]752578562008-09-07 08:08:2985 // The "PostTask" family of methods call the task's Run method asynchronously
86 // from within a message loop at some point in the future.
initial.commitd7cae122008-07-26 21:49:3887 //
[email protected]752578562008-09-07 08:08:2988 // With the PostTask variant, tasks are invoked in FIFO order, inter-mixed
89 // with normal UI or IO event processing. With the PostDelayedTask variant,
90 // tasks are called after at least approximately 'delay_ms' have elapsed.
initial.commitd7cae122008-07-26 21:49:3891 //
[email protected]752578562008-09-07 08:08:2992 // The NonNestable variants work similarly except that they promise never to
93 // dispatch the task from a nested invocation of MessageLoop::Run. Instead,
94 // such tasks get deferred until the top-most MessageLoop::Run is executing.
95 //
96 // The MessageLoop takes ownership of the Task, and deletes it after it has
97 // been Run().
98 //
99 // NOTE: These methods may be called on any thread. The Task will be invoked
initial.commitd7cae122008-07-26 21:49:38100 // on the thread that executes MessageLoop::Run().
[email protected]b0992172008-10-27 18:54:18101
[email protected]752578562008-09-07 08:08:29102 void PostTask(
103 const tracked_objects::Location& from_here, Task* task);
[email protected]b0992172008-10-27 18:54:18104
[email protected]752578562008-09-07 08:08:29105 void PostDelayedTask(
[email protected]743ace42009-06-17 17:23:51106 const tracked_objects::Location& from_here, Task* task, int64 delay_ms);
initial.commitd7cae122008-07-26 21:49:38107
[email protected]752578562008-09-07 08:08:29108 void PostNonNestableTask(
109 const tracked_objects::Location& from_here, Task* task);
initial.commitd7cae122008-07-26 21:49:38110
[email protected]752578562008-09-07 08:08:29111 void PostNonNestableDelayedTask(
[email protected]743ace42009-06-17 17:23:51112 const tracked_objects::Location& from_here, Task* task, int64 delay_ms);
initial.commitd7cae122008-07-26 21:49:38113
114 // A variant on PostTask that deletes the given object. This is useful
115 // if the object needs to live until the next run of the MessageLoop (for
116 // example, deleting a RenderProcessHost from within an IPC callback is not
117 // good).
118 //
119 // NOTE: This method may be called on any thread. The object will be deleted
120 // on the thread that executes MessageLoop::Run(). If this is not the same
121 // as the thread that calls PostDelayedTask(FROM_HERE, ), then T MUST inherit
122 // from RefCountedThreadSafe<T>!
123 template <class T>
124 void DeleteSoon(const tracked_objects::Location& from_here, T* object) {
[email protected]752578562008-09-07 08:08:29125 PostNonNestableTask(from_here, new DeleteTask<T>(object));
initial.commitd7cae122008-07-26 21:49:38126 }
127
128 // A variant on PostTask that releases the given reference counted object
129 // (by calling its Release method). This is useful if the object needs to
130 // live until the next run of the MessageLoop, or if the object needs to be
131 // released on a particular thread.
132 //
133 // NOTE: This method may be called on any thread. The object will be
134 // released (and thus possibly deleted) on the thread that executes
135 // MessageLoop::Run(). If this is not the same as the thread that calls
136 // PostDelayedTask(FROM_HERE, ), then T MUST inherit from
137 // RefCountedThreadSafe<T>!
138 template <class T>
139 void ReleaseSoon(const tracked_objects::Location& from_here, T* object) {
[email protected]752578562008-09-07 08:08:29140 PostNonNestableTask(from_here, new ReleaseTask<T>(object));
initial.commitd7cae122008-07-26 21:49:38141 }
142
[email protected]3882c4332008-07-30 19:03:59143 // Run the message loop.
initial.commitd7cae122008-07-26 21:49:38144 void Run();
145
[email protected]7e0e8762008-07-31 13:10:20146 // Process all pending tasks, windows messages, etc., but don't wait/sleep.
147 // Return as soon as all items that can be run are taken care of.
148 void RunAllPending();
[email protected]3882c4332008-07-30 19:03:59149
initial.commitd7cae122008-07-26 21:49:38150 // Signals the Run method to return after it is done processing all pending
[email protected]fc7fb6e2008-08-16 03:09:05151 // messages. This method may only be called on the same thread that called
152 // Run, and Run must still be on the call stack.
initial.commitd7cae122008-07-26 21:49:38153 //
[email protected]fc7fb6e2008-08-16 03:09:05154 // Use QuitTask if you need to Quit another thread's MessageLoop, but note
155 // that doing so is fairly dangerous if the target thread makes nested calls
156 // to MessageLoop::Run. The problem being that you won't know which nested
157 // run loop you are quiting, so be careful!
158 //
initial.commitd7cae122008-07-26 21:49:38159 void Quit();
160
[email protected]fc7fb6e2008-08-16 03:09:05161 // Invokes Quit on the current MessageLoop when run. Useful to schedule an
initial.commitd7cae122008-07-26 21:49:38162 // arbitrary MessageLoop to Quit.
163 class QuitTask : public Task {
164 public:
165 virtual void Run() {
166 MessageLoop::current()->Quit();
167 }
168 };
169
[email protected]4d9bdfaf2008-08-26 05:53:57170 // A MessageLoop has a particular type, which indicates the set of
171 // asynchronous events it may process in addition to tasks and timers.
172 //
173 // TYPE_DEFAULT
174 // This type of ML only supports tasks and timers.
175 //
176 // TYPE_UI
177 // This type of ML also supports native UI events (e.g., Windows messages).
178 // See also MessageLoopForUI.
179 //
180 // TYPE_IO
181 // This type of ML also supports asynchronous IO. See also
182 // MessageLoopForIO.
183 //
184 enum Type {
185 TYPE_DEFAULT,
186 TYPE_UI,
187 TYPE_IO
188 };
189
initial.commitd7cae122008-07-26 21:49:38190 // Normally, it is not necessary to instantiate a MessageLoop. Instead, it
191 // is typical to make use of the current thread's MessageLoop instance.
[email protected]4d9bdfaf2008-08-26 05:53:57192 explicit MessageLoop(Type type = TYPE_DEFAULT);
initial.commitd7cae122008-07-26 21:49:38193 ~MessageLoop();
194
[email protected]4d9bdfaf2008-08-26 05:53:57195 // Returns the type passed to the constructor.
196 Type type() const { return type_; }
197
initial.commitd7cae122008-07-26 21:49:38198 // Optional call to connect the thread name with this loop.
[email protected]fc7fb6e2008-08-16 03:09:05199 void set_thread_name(const std::string& thread_name) {
200 DCHECK(thread_name_.empty()) << "Should not rename this thread!";
201 thread_name_ = thread_name;
202 }
[email protected]ee132ad2008-08-06 21:27:02203 const std::string& thread_name() const { return thread_name_; }
initial.commitd7cae122008-07-26 21:49:38204
205 // Returns the MessageLoop object for the current thread, or null if none.
[email protected]f886b7bf2008-09-10 10:54:06206 static MessageLoop* current();
initial.commitd7cae122008-07-26 21:49:38207
initial.commitd7cae122008-07-26 21:49:38208 // Enables or disables the recursive task processing. This happens in the case
209 // of recursive message loops. Some unwanted message loop may occurs when
210 // using common controls or printer functions. By default, recursive task
211 // processing is disabled.
212 //
213 // The specific case where tasks get queued is:
214 // - The thread is running a message loop.
215 // - It receives a task #1 and execute it.
216 // - The task #1 implicitly start a message loop, like a MessageBox in the
217 // unit test. This can also be StartDoc or GetSaveFileName.
218 // - The thread receives a task #2 before or while in this second message
219 // loop.
220 // - With NestableTasksAllowed set to true, the task #2 will run right away.
221 // Otherwise, it will get executed right after task #1 completes at "thread
222 // message loop level".
223 void SetNestableTasksAllowed(bool allowed);
224 bool NestableTasksAllowed() const;
225
226 // Enables or disables the restoration during an exception of the unhandled
227 // exception filter that was active when Run() was called. This can happen
228 // if some third party code call SetUnhandledExceptionFilter() and never
229 // restores the previous filter.
230 void set_exception_restoration(bool restore) {
231 exception_restoration_ = restore;
232 }
233
initial.commitd7cae122008-07-26 21:49:38234 //----------------------------------------------------------------------------
[email protected]4d9bdfaf2008-08-26 05:53:57235 protected:
[email protected]fc7fb6e2008-08-16 03:09:05236 struct RunState {
237 // Used to count how many Run() invocations are on the stack.
238 int run_depth;
initial.commitd7cae122008-07-26 21:49:38239
[email protected]fc7fb6e2008-08-16 03:09:05240 // Used to record that Quit() was called, or that we should quit the pump
241 // once it becomes idle.
242 bool quit_received;
initial.commitd7cae122008-07-26 21:49:38243
[email protected]fc7fb6e2008-08-16 03:09:05244#if defined(OS_WIN)
245 base::MessagePumpWin::Dispatcher* dispatcher;
246#endif
247 };
248
249 class AutoRunState : RunState {
250 public:
[email protected]b0992172008-10-27 18:54:18251 explicit AutoRunState(MessageLoop* loop);
[email protected]fc7fb6e2008-08-16 03:09:05252 ~AutoRunState();
initial.commitd7cae122008-07-26 21:49:38253 private:
254 MessageLoop* loop_;
[email protected]fc7fb6e2008-08-16 03:09:05255 RunState* previous_state_;
256 };
initial.commitd7cae122008-07-26 21:49:38257
[email protected]752578562008-09-07 08:08:29258 // This structure is copied around by value.
259 struct PendingTask {
[email protected]e1acf6f2008-10-27 20:43:33260 Task* task; // The task to run.
261 base::Time delayed_run_time; // The time when the task should be run.
262 int sequence_num; // Used to facilitate sorting by run time.
263 bool nestable; // True if OK to dispatch from a nested loop.
initial.commitd7cae122008-07-26 21:49:38264
[email protected]752578562008-09-07 08:08:29265 PendingTask(Task* task, bool nestable)
266 : task(task), sequence_num(0), nestable(nestable) {
267 }
[email protected]b0992172008-10-27 18:54:18268
[email protected]752578562008-09-07 08:08:29269 // Used to support sorting.
270 bool operator<(const PendingTask& other) const;
initial.commitd7cae122008-07-26 21:49:38271 };
272
[email protected]752578562008-09-07 08:08:29273 typedef std::queue<PendingTask> TaskQueue;
[email protected]4bed5d82008-12-17 03:50:05274 typedef std::priority_queue<PendingTask> DelayedTaskQueue;
initial.commitd7cae122008-07-26 21:49:38275
[email protected]fc7fb6e2008-08-16 03:09:05276#if defined(OS_WIN)
277 base::MessagePumpWin* pump_win() {
278 return static_cast<base::MessagePumpWin*>(pump_.get());
279 }
[email protected]36987e92008-09-18 18:46:26280#elif defined(OS_POSIX)
281 base::MessagePumpLibevent* pump_libevent() {
282 return static_cast<base::MessagePumpLibevent*>(pump_.get());
283 }
[email protected]fc7fb6e2008-08-16 03:09:05284#endif
[email protected]3882c4332008-07-30 19:03:59285
286 // A function to encapsulate all the exception handling capability in the
[email protected]fc7fb6e2008-08-16 03:09:05287 // stacks around the running of a main message loop. It will run the message
288 // loop in a SEH try block or not depending on the set_SEH_restoration()
289 // flag.
290 void RunHandler();
initial.commitd7cae122008-07-26 21:49:38291
[email protected]3882c4332008-07-30 19:03:59292 // A surrounding stack frame around the running of the message loop that
293 // supports all saving and restoring of state, as is needed for any/all (ugly)
294 // recursive calls.
[email protected]fc7fb6e2008-08-16 03:09:05295 void RunInternal();
[email protected]ea15e982008-08-15 07:31:20296
[email protected]fc7fb6e2008-08-16 03:09:05297 // Called to process any delayed non-nestable tasks.
initial.commitd7cae122008-07-26 21:49:38298 bool ProcessNextDelayedNonNestableTask();
initial.commitd7cae122008-07-26 21:49:38299
300 //----------------------------------------------------------------------------
initial.commitd7cae122008-07-26 21:49:38301 // Run a work_queue_ task or new_task, and delete it (if it was processed by
302 // PostTask). If there are queued tasks, the oldest one is executed and
303 // new_task is queued. new_task is optional and can be NULL. In this NULL
304 // case, the method will run one pending task (if any exist). Returns true if
[email protected]fc7fb6e2008-08-16 03:09:05305 // it executes a task. Queued tasks accumulate only when there is a
306 // non-nestable task currently processing, in which case the new_task is
307 // appended to the list work_queue_. Such re-entrancy generally happens when
308 // an unrequested message pump (typical of a native dialog) is executing in
309 // the context of a task.
initial.commitd7cae122008-07-26 21:49:38310 bool QueueOrRunTask(Task* new_task);
311
312 // Runs the specified task and deletes it.
313 void RunTask(Task* task);
314
[email protected]752578562008-09-07 08:08:29315 // Calls RunTask or queues the pending_task on the deferred task list if it
316 // cannot be run right now. Returns true if the task was run.
317 bool DeferOrRunPendingTask(const PendingTask& pending_task);
initial.commitd7cae122008-07-26 21:49:38318
[email protected]001747c2008-09-10 00:37:07319 // Adds the pending task to delayed_work_queue_.
320 void AddToDelayedWorkQueue(const PendingTask& pending_task);
321
initial.commitd7cae122008-07-26 21:49:38322 // Load tasks from the incoming_queue_ into work_queue_ if the latter is
323 // empty. The former requires a lock to access, while the latter is directly
324 // accessible on this thread.
325 void ReloadWorkQueue();
326
327 // Delete tasks that haven't run yet without running them. Used in the
[email protected]001747c2008-09-10 00:37:07328 // destructor to make sure all the task's destructors get called. Returns
329 // true if some work was done.
330 bool DeletePendingTasks();
initial.commitd7cae122008-07-26 21:49:38331
initial.commitd7cae122008-07-26 21:49:38332 // Post a task to our incomming queue.
[email protected]752578562008-09-07 08:08:29333 void PostTask_Helper(const tracked_objects::Location& from_here, Task* task,
[email protected]743ace42009-06-17 17:23:51334 int64 delay_ms, bool nestable);
[email protected]fc7fb6e2008-08-16 03:09:05335
336 // base::MessagePump::Delegate methods:
337 virtual bool DoWork();
[email protected]e1acf6f2008-10-27 20:43:33338 virtual bool DoDelayedWork(base::Time* next_delayed_work_time);
[email protected]fc7fb6e2008-08-16 03:09:05339 virtual bool DoIdleWork();
340
initial.commitd7cae122008-07-26 21:49:38341 // Start recording histogram info about events and action IF it was enabled
342 // and IF the statistics recorder can accept a registration of our histogram.
343 void StartHistogrammer();
344
345 // Add occurence of event to our histogram, so that we can see what is being
346 // done in a specific MessageLoop instance (i.e., specific thread).
347 // If message_histogram_ is NULL, this is a no-op.
348 void HistogramEvent(int event);
349
initial.commitd7cae122008-07-26 21:49:38350 static const LinearHistogram::DescriptionPair event_descriptions_[];
351 static bool enable_histogrammer_;
352
[email protected]4d9bdfaf2008-08-26 05:53:57353 Type type_;
354
[email protected]752578562008-09-07 08:08:29355 // A list of tasks that need to be processed by this instance. Note that
356 // this queue is only accessed (push/pop) by our current thread.
357 TaskQueue work_queue_;
[email protected]b0992172008-10-27 18:54:18358
[email protected]752578562008-09-07 08:08:29359 // Contains delayed tasks, sorted by their 'delayed_run_time' property.
360 DelayedTaskQueue delayed_work_queue_;
initial.commitd7cae122008-07-26 21:49:38361
[email protected]752578562008-09-07 08:08:29362 // A queue of non-nestable tasks that we had to defer because when it came
363 // time to execute them we were in a nested message loop. They will execute
364 // once we're out of nested message loops.
365 TaskQueue deferred_non_nestable_work_queue_;
initial.commitd7cae122008-07-26 21:49:38366
[email protected]fc7fb6e2008-08-16 03:09:05367 scoped_refptr<base::MessagePump> pump_;
[email protected]ee132ad2008-08-06 21:27:02368
[email protected]2a127252008-08-05 23:16:41369 ObserverList<DestructionObserver> destruction_observers_;
[email protected]001747c2008-09-10 00:37:07370
initial.commitd7cae122008-07-26 21:49:38371 // A recursion block that prevents accidentally running additonal tasks when
372 // insider a (accidentally induced?) nested message pump.
373 bool nestable_tasks_allowed_;
374
375 bool exception_restoration_;
376
initial.commitd7cae122008-07-26 21:49:38377 std::string thread_name_;
378 // A profiling histogram showing the counts of various messages and events.
379 scoped_ptr<LinearHistogram> message_histogram_;
380
381 // A null terminated list which creates an incoming_queue of tasks that are
382 // aquired under a mutex for processing on this instance's thread. These tasks
383 // have not yet been sorted out into items for our work_queue_ vs items that
384 // will be handled by the TimerManager.
385 TaskQueue incoming_queue_;
386 // Protect access to incoming_queue_.
387 Lock incoming_queue_lock_;
388
[email protected]fc7fb6e2008-08-16 03:09:05389 RunState* state_;
initial.commitd7cae122008-07-26 21:49:38390
[email protected]752578562008-09-07 08:08:29391 // The next sequence number to use for delayed tasks.
392 int next_sequence_num_;
393
[email protected]fc7fb6e2008-08-16 03:09:05394 DISALLOW_COPY_AND_ASSIGN(MessageLoop);
initial.commitd7cae122008-07-26 21:49:38395};
396
[email protected]4d9bdfaf2008-08-26 05:53:57397//-----------------------------------------------------------------------------
398// MessageLoopForUI extends MessageLoop with methods that are particular to a
399// MessageLoop instantiated with TYPE_UI.
400//
401// This class is typically used like so:
402// MessageLoopForUI::current()->...call some method...
403//
404class MessageLoopForUI : public MessageLoop {
405 public:
406 MessageLoopForUI() : MessageLoop(TYPE_UI) {
407 }
license.botbf09a502008-08-24 00:55:55408
[email protected]4d9bdfaf2008-08-26 05:53:57409 // Returns the MessageLoopForUI of the current thread.
410 static MessageLoopForUI* current() {
411 MessageLoop* loop = MessageLoop::current();
412 DCHECK_EQ(MessageLoop::TYPE_UI, loop->type());
413 return static_cast<MessageLoopForUI*>(loop);
414 }
415
[email protected]faabcf42009-05-18 21:12:34416#if defined(OS_LINUX)
417 typedef base::MessagePumpForUI::Observer Observer;
418
419 // See message_pump_glib for definitions of these methods.
420 void AddObserver(Observer* observer);
421 void RemoveObserver(Observer* observer);
422#endif
423
[email protected]4d9bdfaf2008-08-26 05:53:57424#if defined(OS_WIN)
425 typedef base::MessagePumpWin::Dispatcher Dispatcher;
426 typedef base::MessagePumpWin::Observer Observer;
427
428 // Please see MessagePumpWin for definitions of these methods.
[email protected]4d9bdfaf2008-08-26 05:53:57429 void AddObserver(Observer* observer);
430 void RemoveObserver(Observer* observer);
[email protected]faabcf42009-05-18 21:12:34431 void Run(Dispatcher* dispatcher);
[email protected]4d9bdfaf2008-08-26 05:53:57432 void WillProcessMessage(const MSG& message);
433 void DidProcessMessage(const MSG& message);
434 void PumpOutPendingPaintMessages();
[email protected]faabcf42009-05-18 21:12:34435#endif
[email protected]1a8f5d1d2008-09-25 20:33:04436
[email protected]faabcf42009-05-18 21:12:34437#if defined(OS_WIN) || defined(OS_LINUX)
[email protected]1a8f5d1d2008-09-25 20:33:04438 protected:
439 // TODO(rvargas): Make this platform independent.
[email protected]17b89142008-11-07 21:52:15440 base::MessagePumpForUI* pump_ui() {
[email protected]1a8f5d1d2008-09-25 20:33:04441 return static_cast<base::MessagePumpForUI*>(pump_.get());
442 }
[email protected]4d9bdfaf2008-08-26 05:53:57443#endif // defined(OS_WIN)
444};
445
446// Do not add any member variables to MessageLoopForUI! This is important b/c
447// MessageLoopForUI is often allocated via MessageLoop(TYPE_UI). Any extra
448// data that you need should be stored on the MessageLoop's pump_ instance.
449COMPILE_ASSERT(sizeof(MessageLoop) == sizeof(MessageLoopForUI),
450 MessageLoopForUI_should_not_have_extra_member_variables);
451
452//-----------------------------------------------------------------------------
453// MessageLoopForIO extends MessageLoop with methods that are particular to a
454// MessageLoop instantiated with TYPE_IO.
455//
456// This class is typically used like so:
457// MessageLoopForIO::current()->...call some method...
458//
459class MessageLoopForIO : public MessageLoop {
460 public:
461 MessageLoopForIO() : MessageLoop(TYPE_IO) {
462 }
463
464 // Returns the MessageLoopForIO of the current thread.
465 static MessageLoopForIO* current() {
466 MessageLoop* loop = MessageLoop::current();
467 DCHECK_EQ(MessageLoop::TYPE_IO, loop->type());
468 return static_cast<MessageLoopForIO*>(loop);
469 }
470
471#if defined(OS_WIN)
[email protected]32cda29d2008-10-09 23:58:43472 typedef base::MessagePumpForIO::IOHandler IOHandler;
[email protected]17b89142008-11-07 21:52:15473 typedef base::MessagePumpForIO::IOContext IOContext;
[email protected]4d9bdfaf2008-08-26 05:53:57474
475 // Please see MessagePumpWin for definitions of these methods.
[email protected]32cda29d2008-10-09 23:58:43476 void RegisterIOHandler(HANDLE file_handle, IOHandler* handler);
[email protected]17b89142008-11-07 21:52:15477 bool WaitForIOCompletion(DWORD timeout, IOHandler* filter);
[email protected]36987e92008-09-18 18:46:26478
[email protected]1a8f5d1d2008-09-25 20:33:04479 protected:
480 // TODO(rvargas): Make this platform independent.
481 base::MessagePumpForIO* pump_io() {
482 return static_cast<base::MessagePumpForIO*>(pump_.get());
483 }
484
[email protected]36987e92008-09-18 18:46:26485#elif defined(OS_POSIX)
486 typedef base::MessagePumpLibevent::Watcher Watcher;
[email protected]e45e6c02008-12-15 22:02:17487 typedef base::MessagePumpLibevent::FileDescriptorWatcher
488 FileDescriptorWatcher;
[email protected]36987e92008-09-18 18:46:26489
[email protected]e45e6c02008-12-15 22:02:17490 enum Mode {
491 WATCH_READ = base::MessagePumpLibevent::WATCH_READ,
492 WATCH_WRITE = base::MessagePumpLibevent::WATCH_WRITE,
493 WATCH_READ_WRITE = base::MessagePumpLibevent::WATCH_READ_WRITE
494 };
495
496 // Please see MessagePumpLibevent for definition.
497 bool WatchFileDescriptor(int fd,
498 bool persistent,
499 Mode mode,
500 FileDescriptorWatcher *controller,
501 Watcher *delegate);
[email protected]1a8f5d1d2008-09-25 20:33:04502#endif // defined(OS_POSIX)
[email protected]4d9bdfaf2008-08-26 05:53:57503};
504
505// Do not add any member variables to MessageLoopForIO! This is important b/c
506// MessageLoopForIO is often allocated via MessageLoop(TYPE_IO). Any extra
507// data that you need should be stored on the MessageLoop's pump_ instance.
508COMPILE_ASSERT(sizeof(MessageLoop) == sizeof(MessageLoopForIO),
509 MessageLoopForIO_should_not_have_extra_member_variables);
510
511#endif // BASE_MESSAGE_LOOP_H_