blob: 395ce6ed31d20ef0a54a608eba293740f744cfe5 [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
[email protected]b5f95102009-07-01 19:53:59234 // Returns true if we are currently running a nested message loop.
235 bool IsNested();
236
[email protected]148d1052009-07-31 22:53:37237#if defined(OS_WIN)
238 typedef base::MessagePumpWin::Dispatcher Dispatcher;
239 typedef base::MessagePumpWin::Observer Observer;
240#elif defined(OS_LINUX)
241 typedef base::MessagePumpForUI::Dispatcher Dispatcher;
242 typedef base::MessagePumpForUI::Observer Observer;
243#endif
244
initial.commitd7cae122008-07-26 21:49:38245 //----------------------------------------------------------------------------
[email protected]4d9bdfaf2008-08-26 05:53:57246 protected:
[email protected]fc7fb6e2008-08-16 03:09:05247 struct RunState {
248 // Used to count how many Run() invocations are on the stack.
249 int run_depth;
initial.commitd7cae122008-07-26 21:49:38250
[email protected]fc7fb6e2008-08-16 03:09:05251 // Used to record that Quit() was called, or that we should quit the pump
252 // once it becomes idle.
253 bool quit_received;
initial.commitd7cae122008-07-26 21:49:38254
[email protected]148d1052009-07-31 22:53:37255#if defined(OS_WIN) || defined(OS_LINUX)
256 Dispatcher* dispatcher;
[email protected]fc7fb6e2008-08-16 03:09:05257#endif
258 };
259
260 class AutoRunState : RunState {
261 public:
[email protected]b0992172008-10-27 18:54:18262 explicit AutoRunState(MessageLoop* loop);
[email protected]fc7fb6e2008-08-16 03:09:05263 ~AutoRunState();
initial.commitd7cae122008-07-26 21:49:38264 private:
265 MessageLoop* loop_;
[email protected]fc7fb6e2008-08-16 03:09:05266 RunState* previous_state_;
267 };
initial.commitd7cae122008-07-26 21:49:38268
[email protected]752578562008-09-07 08:08:29269 // This structure is copied around by value.
270 struct PendingTask {
[email protected]e1acf6f2008-10-27 20:43:33271 Task* task; // The task to run.
272 base::Time delayed_run_time; // The time when the task should be run.
273 int sequence_num; // Used to facilitate sorting by run time.
274 bool nestable; // True if OK to dispatch from a nested loop.
initial.commitd7cae122008-07-26 21:49:38275
[email protected]752578562008-09-07 08:08:29276 PendingTask(Task* task, bool nestable)
277 : task(task), sequence_num(0), nestable(nestable) {
278 }
[email protected]b0992172008-10-27 18:54:18279
[email protected]752578562008-09-07 08:08:29280 // Used to support sorting.
281 bool operator<(const PendingTask& other) const;
initial.commitd7cae122008-07-26 21:49:38282 };
283
[email protected]752578562008-09-07 08:08:29284 typedef std::queue<PendingTask> TaskQueue;
[email protected]4bed5d82008-12-17 03:50:05285 typedef std::priority_queue<PendingTask> DelayedTaskQueue;
initial.commitd7cae122008-07-26 21:49:38286
[email protected]fc7fb6e2008-08-16 03:09:05287#if defined(OS_WIN)
288 base::MessagePumpWin* pump_win() {
289 return static_cast<base::MessagePumpWin*>(pump_.get());
290 }
[email protected]36987e92008-09-18 18:46:26291#elif defined(OS_POSIX)
292 base::MessagePumpLibevent* pump_libevent() {
293 return static_cast<base::MessagePumpLibevent*>(pump_.get());
294 }
[email protected]fc7fb6e2008-08-16 03:09:05295#endif
[email protected]3882c4332008-07-30 19:03:59296
297 // A function to encapsulate all the exception handling capability in the
[email protected]fc7fb6e2008-08-16 03:09:05298 // stacks around the running of a main message loop. It will run the message
299 // loop in a SEH try block or not depending on the set_SEH_restoration()
300 // flag.
301 void RunHandler();
initial.commitd7cae122008-07-26 21:49:38302
[email protected]3882c4332008-07-30 19:03:59303 // A surrounding stack frame around the running of the message loop that
304 // supports all saving and restoring of state, as is needed for any/all (ugly)
305 // recursive calls.
[email protected]fc7fb6e2008-08-16 03:09:05306 void RunInternal();
[email protected]ea15e982008-08-15 07:31:20307
[email protected]fc7fb6e2008-08-16 03:09:05308 // Called to process any delayed non-nestable tasks.
initial.commitd7cae122008-07-26 21:49:38309 bool ProcessNextDelayedNonNestableTask();
initial.commitd7cae122008-07-26 21:49:38310
311 //----------------------------------------------------------------------------
initial.commitd7cae122008-07-26 21:49:38312 // Run a work_queue_ task or new_task, and delete it (if it was processed by
313 // PostTask). If there are queued tasks, the oldest one is executed and
314 // new_task is queued. new_task is optional and can be NULL. In this NULL
315 // case, the method will run one pending task (if any exist). Returns true if
[email protected]fc7fb6e2008-08-16 03:09:05316 // it executes a task. Queued tasks accumulate only when there is a
317 // non-nestable task currently processing, in which case the new_task is
318 // appended to the list work_queue_. Such re-entrancy generally happens when
319 // an unrequested message pump (typical of a native dialog) is executing in
320 // the context of a task.
initial.commitd7cae122008-07-26 21:49:38321 bool QueueOrRunTask(Task* new_task);
322
323 // Runs the specified task and deletes it.
324 void RunTask(Task* task);
325
[email protected]752578562008-09-07 08:08:29326 // Calls RunTask or queues the pending_task on the deferred task list if it
327 // cannot be run right now. Returns true if the task was run.
328 bool DeferOrRunPendingTask(const PendingTask& pending_task);
initial.commitd7cae122008-07-26 21:49:38329
[email protected]001747c2008-09-10 00:37:07330 // Adds the pending task to delayed_work_queue_.
331 void AddToDelayedWorkQueue(const PendingTask& pending_task);
332
initial.commitd7cae122008-07-26 21:49:38333 // Load tasks from the incoming_queue_ into work_queue_ if the latter is
334 // empty. The former requires a lock to access, while the latter is directly
335 // accessible on this thread.
336 void ReloadWorkQueue();
337
338 // Delete tasks that haven't run yet without running them. Used in the
[email protected]001747c2008-09-10 00:37:07339 // destructor to make sure all the task's destructors get called. Returns
340 // true if some work was done.
341 bool DeletePendingTasks();
initial.commitd7cae122008-07-26 21:49:38342
initial.commitd7cae122008-07-26 21:49:38343 // Post a task to our incomming queue.
[email protected]752578562008-09-07 08:08:29344 void PostTask_Helper(const tracked_objects::Location& from_here, Task* task,
[email protected]743ace42009-06-17 17:23:51345 int64 delay_ms, bool nestable);
[email protected]fc7fb6e2008-08-16 03:09:05346
347 // base::MessagePump::Delegate methods:
348 virtual bool DoWork();
[email protected]e1acf6f2008-10-27 20:43:33349 virtual bool DoDelayedWork(base::Time* next_delayed_work_time);
[email protected]fc7fb6e2008-08-16 03:09:05350 virtual bool DoIdleWork();
351
initial.commitd7cae122008-07-26 21:49:38352 // Start recording histogram info about events and action IF it was enabled
353 // and IF the statistics recorder can accept a registration of our histogram.
354 void StartHistogrammer();
355
356 // Add occurence of event to our histogram, so that we can see what is being
357 // done in a specific MessageLoop instance (i.e., specific thread).
358 // If message_histogram_ is NULL, this is a no-op.
359 void HistogramEvent(int event);
360
initial.commitd7cae122008-07-26 21:49:38361 static const LinearHistogram::DescriptionPair event_descriptions_[];
362 static bool enable_histogrammer_;
363
[email protected]4d9bdfaf2008-08-26 05:53:57364 Type type_;
365
[email protected]752578562008-09-07 08:08:29366 // A list of tasks that need to be processed by this instance. Note that
367 // this queue is only accessed (push/pop) by our current thread.
368 TaskQueue work_queue_;
[email protected]b0992172008-10-27 18:54:18369
[email protected]752578562008-09-07 08:08:29370 // Contains delayed tasks, sorted by their 'delayed_run_time' property.
371 DelayedTaskQueue delayed_work_queue_;
initial.commitd7cae122008-07-26 21:49:38372
[email protected]752578562008-09-07 08:08:29373 // A queue of non-nestable tasks that we had to defer because when it came
374 // time to execute them we were in a nested message loop. They will execute
375 // once we're out of nested message loops.
376 TaskQueue deferred_non_nestable_work_queue_;
initial.commitd7cae122008-07-26 21:49:38377
[email protected]fc7fb6e2008-08-16 03:09:05378 scoped_refptr<base::MessagePump> pump_;
[email protected]ee132ad2008-08-06 21:27:02379
[email protected]2a127252008-08-05 23:16:41380 ObserverList<DestructionObserver> destruction_observers_;
[email protected]001747c2008-09-10 00:37:07381
initial.commitd7cae122008-07-26 21:49:38382 // A recursion block that prevents accidentally running additonal tasks when
383 // insider a (accidentally induced?) nested message pump.
384 bool nestable_tasks_allowed_;
385
386 bool exception_restoration_;
387
initial.commitd7cae122008-07-26 21:49:38388 std::string thread_name_;
389 // A profiling histogram showing the counts of various messages and events.
390 scoped_ptr<LinearHistogram> message_histogram_;
391
392 // A null terminated list which creates an incoming_queue of tasks that are
393 // aquired under a mutex for processing on this instance's thread. These tasks
394 // have not yet been sorted out into items for our work_queue_ vs items that
395 // will be handled by the TimerManager.
396 TaskQueue incoming_queue_;
397 // Protect access to incoming_queue_.
398 Lock incoming_queue_lock_;
399
[email protected]fc7fb6e2008-08-16 03:09:05400 RunState* state_;
initial.commitd7cae122008-07-26 21:49:38401
[email protected]752578562008-09-07 08:08:29402 // The next sequence number to use for delayed tasks.
403 int next_sequence_num_;
404
[email protected]fc7fb6e2008-08-16 03:09:05405 DISALLOW_COPY_AND_ASSIGN(MessageLoop);
initial.commitd7cae122008-07-26 21:49:38406};
407
[email protected]4d9bdfaf2008-08-26 05:53:57408//-----------------------------------------------------------------------------
409// MessageLoopForUI extends MessageLoop with methods that are particular to a
410// MessageLoop instantiated with TYPE_UI.
411//
412// This class is typically used like so:
413// MessageLoopForUI::current()->...call some method...
414//
415class MessageLoopForUI : public MessageLoop {
416 public:
417 MessageLoopForUI() : MessageLoop(TYPE_UI) {
418 }
license.botbf09a502008-08-24 00:55:55419
[email protected]4d9bdfaf2008-08-26 05:53:57420 // Returns the MessageLoopForUI of the current thread.
421 static MessageLoopForUI* current() {
422 MessageLoop* loop = MessageLoop::current();
423 DCHECK_EQ(MessageLoop::TYPE_UI, loop->type());
424 return static_cast<MessageLoopForUI*>(loop);
425 }
426
[email protected]4d9bdfaf2008-08-26 05:53:57427#if defined(OS_WIN)
[email protected]4d9bdfaf2008-08-26 05:53:57428 void WillProcessMessage(const MSG& message);
429 void DidProcessMessage(const MSG& message);
430 void PumpOutPendingPaintMessages();
[email protected]faabcf42009-05-18 21:12:34431#endif
[email protected]1a8f5d1d2008-09-25 20:33:04432
[email protected]faabcf42009-05-18 21:12:34433#if defined(OS_WIN) || defined(OS_LINUX)
[email protected]148d1052009-07-31 22:53:37434 // Please see message_pump_win/message_pump_glib for definitions of these
435 // methods.
436 void AddObserver(Observer* observer);
437 void RemoveObserver(Observer* observer);
438 void Run(Dispatcher* dispatcher);
439
[email protected]1a8f5d1d2008-09-25 20:33:04440 protected:
441 // TODO(rvargas): Make this platform independent.
[email protected]17b89142008-11-07 21:52:15442 base::MessagePumpForUI* pump_ui() {
[email protected]1a8f5d1d2008-09-25 20:33:04443 return static_cast<base::MessagePumpForUI*>(pump_.get());
444 }
[email protected]148d1052009-07-31 22:53:37445#endif // defined(OS_WIN) || defined(OS_LINUX)
[email protected]4d9bdfaf2008-08-26 05:53:57446};
447
448// Do not add any member variables to MessageLoopForUI! This is important b/c
449// MessageLoopForUI is often allocated via MessageLoop(TYPE_UI). Any extra
450// data that you need should be stored on the MessageLoop's pump_ instance.
451COMPILE_ASSERT(sizeof(MessageLoop) == sizeof(MessageLoopForUI),
452 MessageLoopForUI_should_not_have_extra_member_variables);
453
454//-----------------------------------------------------------------------------
455// MessageLoopForIO extends MessageLoop with methods that are particular to a
456// MessageLoop instantiated with TYPE_IO.
457//
458// This class is typically used like so:
459// MessageLoopForIO::current()->...call some method...
460//
461class MessageLoopForIO : public MessageLoop {
462 public:
463 MessageLoopForIO() : MessageLoop(TYPE_IO) {
464 }
465
466 // Returns the MessageLoopForIO of the current thread.
467 static MessageLoopForIO* current() {
468 MessageLoop* loop = MessageLoop::current();
469 DCHECK_EQ(MessageLoop::TYPE_IO, loop->type());
470 return static_cast<MessageLoopForIO*>(loop);
471 }
472
473#if defined(OS_WIN)
[email protected]32cda29d2008-10-09 23:58:43474 typedef base::MessagePumpForIO::IOHandler IOHandler;
[email protected]17b89142008-11-07 21:52:15475 typedef base::MessagePumpForIO::IOContext IOContext;
[email protected]4d9bdfaf2008-08-26 05:53:57476
477 // Please see MessagePumpWin for definitions of these methods.
[email protected]32cda29d2008-10-09 23:58:43478 void RegisterIOHandler(HANDLE file_handle, IOHandler* handler);
[email protected]17b89142008-11-07 21:52:15479 bool WaitForIOCompletion(DWORD timeout, IOHandler* filter);
[email protected]36987e92008-09-18 18:46:26480
[email protected]1a8f5d1d2008-09-25 20:33:04481 protected:
482 // TODO(rvargas): Make this platform independent.
483 base::MessagePumpForIO* pump_io() {
484 return static_cast<base::MessagePumpForIO*>(pump_.get());
485 }
486
[email protected]36987e92008-09-18 18:46:26487#elif defined(OS_POSIX)
488 typedef base::MessagePumpLibevent::Watcher Watcher;
[email protected]e45e6c02008-12-15 22:02:17489 typedef base::MessagePumpLibevent::FileDescriptorWatcher
490 FileDescriptorWatcher;
[email protected]36987e92008-09-18 18:46:26491
[email protected]e45e6c02008-12-15 22:02:17492 enum Mode {
493 WATCH_READ = base::MessagePumpLibevent::WATCH_READ,
494 WATCH_WRITE = base::MessagePumpLibevent::WATCH_WRITE,
495 WATCH_READ_WRITE = base::MessagePumpLibevent::WATCH_READ_WRITE
496 };
497
498 // Please see MessagePumpLibevent for definition.
499 bool WatchFileDescriptor(int fd,
500 bool persistent,
501 Mode mode,
502 FileDescriptorWatcher *controller,
503 Watcher *delegate);
[email protected]1a8f5d1d2008-09-25 20:33:04504#endif // defined(OS_POSIX)
[email protected]4d9bdfaf2008-08-26 05:53:57505};
506
507// Do not add any member variables to MessageLoopForIO! This is important b/c
508// MessageLoopForIO is often allocated via MessageLoop(TYPE_IO). Any extra
509// data that you need should be stored on the MessageLoop's pump_ instance.
510COMPILE_ASSERT(sizeof(MessageLoop) == sizeof(MessageLoopForIO),
511 MessageLoopForIO_should_not_have_extra_member_variables);
512
513#endif // BASE_MESSAGE_LOOP_H_