blob: 122bd06218ff305fcda16ce53afae60ff8141324 [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]ea15e982008-08-15 07:31:205#include "base/message_loop.h"
6
[email protected]fc7fb6e2008-08-16 03:09:057#include <algorithm>
8
[email protected]f1ea2fa2008-08-21 22:26:069#include "base/compiler_specific.h"
initial.commitd7cae122008-07-26 21:49:3810#include "base/logging.h"
[email protected]b16ef312008-08-19 18:36:2311#include "base/message_pump_default.h"
initial.commitd7cae122008-07-26 21:49:3812#include "base/string_util.h"
13#include "base/thread_local_storage.h"
initial.commitd7cae122008-07-26 21:49:3814
15// a TLS index to the message loop for the current thread
16// Note that if we start doing complex stuff in other static initializers
17// this could cause problems.
[email protected]6a6e6572008-08-20 22:54:5218// TODO(evanm): this shouldn't rely on static initialization.
19// static
20TLSSlot MessageLoop::tls_index_;
initial.commitd7cae122008-07-26 21:49:3821
22//------------------------------------------------------------------------------
23
initial.commitd7cae122008-07-26 21:49:3824// Logical events for Histogram profiling. Run with -message-loop-histogrammer
25// to get an accounting of messages and actions taken on each thread.
[email protected]fc7fb6e2008-08-16 03:09:0526static const int kTaskRunEvent = 0x1;
27static const int kTimerEvent = 0x2;
initial.commitd7cae122008-07-26 21:49:3828
29// Provide range of message IDs for use in histogramming and debug display.
30static const int kLeastNonZeroMessageId = 1;
31static const int kMaxMessageId = 1099;
32static const int kNumberOfDistinctMessagesDisplayed = 1100;
33
34//------------------------------------------------------------------------------
35
[email protected]fc7fb6e2008-08-16 03:09:0536#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:3837
initial.commitd7cae122008-07-26 21:49:3838// Upon a SEH exception in this thread, it restores the original unhandled
39// exception filter.
40static int SEHFilter(LPTOP_LEVEL_EXCEPTION_FILTER old_filter) {
41 ::SetUnhandledExceptionFilter(old_filter);
42 return EXCEPTION_CONTINUE_SEARCH;
43}
44
45// Retrieves a pointer to the current unhandled exception filter. There
46// is no standalone getter method.
47static LPTOP_LEVEL_EXCEPTION_FILTER GetTopSEHFilter() {
48 LPTOP_LEVEL_EXCEPTION_FILTER top_filter = NULL;
49 top_filter = ::SetUnhandledExceptionFilter(0);
50 ::SetUnhandledExceptionFilter(top_filter);
51 return top_filter;
52}
53
[email protected]fc7fb6e2008-08-16 03:09:0554#endif // defined(OS_WIN)
55
initial.commitd7cae122008-07-26 21:49:3856//------------------------------------------------------------------------------
57
[email protected]4d9bdfaf2008-08-26 05:53:5758MessageLoop::MessageLoop(Type type)
59 : type_(type),
[email protected]a5b94a92008-08-12 23:25:4360 nestable_tasks_allowed_(true),
[email protected]b16ef312008-08-19 18:36:2361 exception_restoration_(false),
[email protected]752578562008-09-07 08:08:2962 state_(NULL),
63 next_sequence_num_(0) {
[email protected]4d9bdfaf2008-08-26 05:53:5764 DCHECK(!tls_index_.Get()) << "should only have one message loop per thread";
[email protected]6a6e6572008-08-20 22:54:5265 tls_index_.Set(this);
[email protected]4d9bdfaf2008-08-26 05:53:5766
[email protected]ef8963172008-08-26 06:52:4167 // TODO(darin): Choose the pump based on the requested type.
[email protected]fc7fb6e2008-08-16 03:09:0568#if defined(OS_WIN)
[email protected]9bcbf472008-08-30 00:22:4869 if (type_ == TYPE_DEFAULT) {
70 pump_ = new base::MessagePumpDefault();
71 } else {
72 pump_ = new base::MessagePumpWin();
73 }
[email protected]b16ef312008-08-19 18:36:2374#else
[email protected]ef8963172008-08-26 06:52:4175 pump_ = new base::MessagePumpDefault();
[email protected]fc7fb6e2008-08-16 03:09:0576#endif
initial.commitd7cae122008-07-26 21:49:3877}
78
79MessageLoop::~MessageLoop() {
80 DCHECK(this == current());
[email protected]2a127252008-08-05 23:16:4181
82 // Let interested parties have one last shot at accessing this.
83 FOR_EACH_OBSERVER(DestructionObserver, destruction_observers_,
84 WillDestroyCurrentMessageLoop());
85
[email protected]08de3cde2008-09-09 05:55:3586 DCHECK(!state_);
87
[email protected]001747c2008-09-10 00:37:0788 // Clean up any unprocessed tasks, but take care: deleting a task could
89 // result in the addition of more tasks (e.g., via DeleteSoon). We set a
90 // limit on the number of times we will allow a deleted task to generate more
91 // tasks. Normally, we should only pass through this loop once or twice. If
92 // we end up hitting the loop limit, then it is probably due to one task that
93 // is being stubborn. Inspect the queues to see who is left.
94 bool did_work;
95 for (int i = 0; i < 100; ++i) {
96 DeletePendingTasks();
97 ReloadWorkQueue();
98 // If we end up with empty queues, then break out of the loop.
99 did_work = DeletePendingTasks();
100 if (!did_work)
101 break;
[email protected]08de3cde2008-09-09 05:55:35102 }
[email protected]001747c2008-09-10 00:37:07103 DCHECK(!did_work);
104
105 // OK, now make it so that no one can find us.
106 tls_index_.Set(NULL);
initial.commitd7cae122008-07-26 21:49:38107}
108
[email protected]2a127252008-08-05 23:16:41109void MessageLoop::AddDestructionObserver(DestructionObserver *obs) {
110 DCHECK(this == current());
111 destruction_observers_.AddObserver(obs);
112}
113
114void MessageLoop::RemoveDestructionObserver(DestructionObserver *obs) {
115 DCHECK(this == current());
116 destruction_observers_.RemoveObserver(obs);
117}
118
[email protected]ea15e982008-08-15 07:31:20119void MessageLoop::Run() {
[email protected]fc7fb6e2008-08-16 03:09:05120 AutoRunState save_state(this);
121 RunHandler();
[email protected]ea15e982008-08-15 07:31:20122}
123
[email protected]7e0e8762008-07-31 13:10:20124void MessageLoop::RunAllPending() {
[email protected]fc7fb6e2008-08-16 03:09:05125 AutoRunState save_state(this);
126 state_->quit_received = true; // Means run until we would otherwise block.
127 RunHandler();
initial.commitd7cae122008-07-26 21:49:38128}
129
130// Runs the loop in two different SEH modes:
131// enable_SEH_restoration_ = false : any unhandled exception goes to the last
132// one that calls SetUnhandledExceptionFilter().
133// enable_SEH_restoration_ = true : any unhandled exception goes to the filter
134// that was existed before the loop was run.
[email protected]fc7fb6e2008-08-16 03:09:05135void MessageLoop::RunHandler() {
136#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38137 if (exception_restoration_) {
138 LPTOP_LEVEL_EXCEPTION_FILTER current_filter = GetTopSEHFilter();
139 __try {
[email protected]fc7fb6e2008-08-16 03:09:05140 RunInternal();
initial.commitd7cae122008-07-26 21:49:38141 } __except(SEHFilter(current_filter)) {
142 }
[email protected]fc7fb6e2008-08-16 03:09:05143 return;
initial.commitd7cae122008-07-26 21:49:38144 }
[email protected]fc7fb6e2008-08-16 03:09:05145#endif
146
147 RunInternal();
initial.commitd7cae122008-07-26 21:49:38148}
149
150//------------------------------------------------------------------------------
initial.commitd7cae122008-07-26 21:49:38151
[email protected]fc7fb6e2008-08-16 03:09:05152void MessageLoop::RunInternal() {
153 DCHECK(this == current());
154
initial.commitd7cae122008-07-26 21:49:38155 StartHistogrammer();
156
[email protected]fc7fb6e2008-08-16 03:09:05157#if defined(OS_WIN)
158 if (state_->dispatcher) {
159 pump_win()->RunWithDispatcher(this, state_->dispatcher);
160 return;
[email protected]7e0e8762008-07-31 13:10:20161 }
[email protected]fc7fb6e2008-08-16 03:09:05162#endif
163
164 pump_->Run(this);
[email protected]b8f2fe5d2008-07-30 07:50:53165}
[email protected]7622bd02008-07-30 06:58:56166
[email protected]3882c4332008-07-30 19:03:59167//------------------------------------------------------------------------------
168// Wrapper functions for use in above message loop framework.
169
initial.commitd7cae122008-07-26 21:49:38170bool MessageLoop::ProcessNextDelayedNonNestableTask() {
[email protected]fc7fb6e2008-08-16 03:09:05171 if (state_->run_depth != 1)
initial.commitd7cae122008-07-26 21:49:38172 return false;
173
[email protected]752578562008-09-07 08:08:29174 if (deferred_non_nestable_work_queue_.empty())
initial.commitd7cae122008-07-26 21:49:38175 return false;
[email protected]752578562008-09-07 08:08:29176
177 Task* task = deferred_non_nestable_work_queue_.front().task;
178 deferred_non_nestable_work_queue_.pop();
179
180 RunTask(task);
initial.commitd7cae122008-07-26 21:49:38181 return true;
182}
183
initial.commitd7cae122008-07-26 21:49:38184//------------------------------------------------------------------------------
185
186void MessageLoop::Quit() {
[email protected]fc7fb6e2008-08-16 03:09:05187 DCHECK(current() == this);
188 if (state_) {
189 state_->quit_received = true;
190 } else {
191 NOTREACHED() << "Must be inside Run to call Quit";
initial.commitd7cae122008-07-26 21:49:38192 }
initial.commitd7cae122008-07-26 21:49:38193}
194
[email protected]752578562008-09-07 08:08:29195void MessageLoop::PostTask(
196 const tracked_objects::Location& from_here, Task* task) {
197 PostTask_Helper(from_here, task, 0, true);
198}
199
200void MessageLoop::PostDelayedTask(
201 const tracked_objects::Location& from_here, Task* task, int delay_ms) {
202 PostTask_Helper(from_here, task, delay_ms, true);
203}
204
205void MessageLoop::PostNonNestableTask(
206 const tracked_objects::Location& from_here, Task* task) {
207 PostTask_Helper(from_here, task, 0, false);
208}
209
210void MessageLoop::PostNonNestableDelayedTask(
211 const tracked_objects::Location& from_here, Task* task, int delay_ms) {
212 PostTask_Helper(from_here, task, delay_ms, false);
213}
214
initial.commitd7cae122008-07-26 21:49:38215// Possibly called on a background thread!
[email protected]752578562008-09-07 08:08:29216void MessageLoop::PostTask_Helper(
217 const tracked_objects::Location& from_here, Task* task, int delay_ms,
218 bool nestable) {
initial.commitd7cae122008-07-26 21:49:38219 task->SetBirthPlace(from_here);
[email protected]9bcbf472008-08-30 00:22:48220
[email protected]752578562008-09-07 08:08:29221 PendingTask pending_task(task, nestable);
[email protected]9bcbf472008-08-30 00:22:48222
223 if (delay_ms > 0) {
[email protected]752578562008-09-07 08:08:29224 pending_task.delayed_run_time =
[email protected]9bcbf472008-08-30 00:22:48225 Time::Now() + TimeDelta::FromMilliseconds(delay_ms);
226 } else {
227 DCHECK(delay_ms == 0) << "delay should not be negative";
228 }
229
initial.commitd7cae122008-07-26 21:49:38230 // Warning: Don't try to short-circuit, and handle this thread's tasks more
231 // directly, as it could starve handling of foreign threads. Put every task
232 // into this queue.
233
[email protected]fc7fb6e2008-08-16 03:09:05234 scoped_refptr<base::MessagePump> pump;
initial.commitd7cae122008-07-26 21:49:38235 {
[email protected]fc7fb6e2008-08-16 03:09:05236 AutoLock locked(incoming_queue_lock_);
237
[email protected]752578562008-09-07 08:08:29238 bool was_empty = incoming_queue_.empty();
239 incoming_queue_.push(pending_task);
initial.commitd7cae122008-07-26 21:49:38240 if (!was_empty)
241 return; // Someone else should have started the sub-pump.
242
[email protected]fc7fb6e2008-08-16 03:09:05243 pump = pump_;
[email protected]ea15e982008-08-15 07:31:20244 }
[email protected]fc7fb6e2008-08-16 03:09:05245 // Since the incoming_queue_ may contain a task that destroys this message
246 // loop, we cannot exit incoming_queue_lock_ until we are done with |this|.
247 // We use a stack-based reference to the message pump so that we can call
248 // ScheduleWork outside of incoming_queue_lock_.
[email protected]ea15e982008-08-15 07:31:20249
[email protected]fc7fb6e2008-08-16 03:09:05250 pump->ScheduleWork();
initial.commitd7cae122008-07-26 21:49:38251}
252
253void MessageLoop::SetNestableTasksAllowed(bool allowed) {
[email protected]124a2bdf2008-08-09 00:14:09254 if (nestable_tasks_allowed_ != allowed) {
255 nestable_tasks_allowed_ = allowed;
256 if (!nestable_tasks_allowed_)
257 return;
258 // Start the native pump if we are not already pumping.
[email protected]fc7fb6e2008-08-16 03:09:05259 pump_->ScheduleWork();
[email protected]124a2bdf2008-08-09 00:14:09260 }
initial.commitd7cae122008-07-26 21:49:38261}
262
263bool MessageLoop::NestableTasksAllowed() const {
264 return nestable_tasks_allowed_;
265}
266
initial.commitd7cae122008-07-26 21:49:38267//------------------------------------------------------------------------------
initial.commitd7cae122008-07-26 21:49:38268
initial.commitd7cae122008-07-26 21:49:38269void MessageLoop::RunTask(Task* task) {
initial.commitd7cae122008-07-26 21:49:38270 DCHECK(nestable_tasks_allowed_);
271 // Execute the task and assume the worst: It is probably not reentrant.
272 nestable_tasks_allowed_ = false;
[email protected]752578562008-09-07 08:08:29273
274 HistogramEvent(kTaskRunEvent);
275 task->Run();
276 delete task;
277
278 nestable_tasks_allowed_ = true;
initial.commitd7cae122008-07-26 21:49:38279}
280
[email protected]752578562008-09-07 08:08:29281bool MessageLoop::DeferOrRunPendingTask(const PendingTask& pending_task) {
282 if (pending_task.nestable || state_->run_depth == 1) {
283 RunTask(pending_task.task);
284 // Show that we ran a task (Note: a new one might arrive as a
285 // consequence!).
286 return true;
287 }
288
289 // We couldn't run the task now because we're in a nested message loop
290 // and the task isn't nestable.
291 deferred_non_nestable_work_queue_.push(pending_task);
292 return false;
initial.commitd7cae122008-07-26 21:49:38293}
294
[email protected]001747c2008-09-10 00:37:07295void MessageLoop::AddToDelayedWorkQueue(const PendingTask& pending_task) {
296 // Move to the delayed work queue. Initialize the sequence number
297 // before inserting into the delayed_work_queue_. The sequence number
298 // is used to faciliate FIFO sorting when two tasks have the same
299 // delayed_run_time value.
300 PendingTask new_pending_task(pending_task);
301 new_pending_task.sequence_num = next_sequence_num_++;
302 delayed_work_queue_.push(new_pending_task);
303}
304
initial.commitd7cae122008-07-26 21:49:38305void MessageLoop::ReloadWorkQueue() {
306 // We can improve performance of our loading tasks from incoming_queue_ to
[email protected]fc7fb6e2008-08-16 03:09:05307 // work_queue_ by waiting until the last minute (work_queue_ is empty) to
308 // load. That reduces the number of locks-per-task significantly when our
[email protected]752578562008-09-07 08:08:29309 // queues get large.
310 if (!work_queue_.empty())
initial.commitd7cae122008-07-26 21:49:38311 return; // Wait till we *really* need to lock and load.
312
313 // Acquire all we can from the inter-thread queue with one lock acquisition.
initial.commitd7cae122008-07-26 21:49:38314 {
315 AutoLock lock(incoming_queue_lock_);
[email protected]752578562008-09-07 08:08:29316 if (incoming_queue_.empty())
initial.commitd7cae122008-07-26 21:49:38317 return;
[email protected]752578562008-09-07 08:08:29318 std::swap(incoming_queue_, work_queue_);
319 DCHECK(incoming_queue_.empty());
initial.commitd7cae122008-07-26 21:49:38320 }
321}
322
[email protected]001747c2008-09-10 00:37:07323bool MessageLoop::DeletePendingTasks() {
324 bool did_work = !work_queue_.empty();
325 while (!work_queue_.empty()) {
326 PendingTask pending_task = work_queue_.front();
327 work_queue_.pop();
328 if (!pending_task.delayed_run_time.is_null()) {
329 // We want to delete delayed tasks in the same order in which they would
330 // normally be deleted in case of any funny dependencies between delayed
331 // tasks.
332 AddToDelayedWorkQueue(pending_task);
333 } else {
334 // TODO(darin): Delete all tasks once it is safe to do so.
335 //delete task;
336 }
initial.commitd7cae122008-07-26 21:49:38337 }
[email protected]001747c2008-09-10 00:37:07338 did_work |= !deferred_non_nestable_work_queue_.empty();
339 while (!deferred_non_nestable_work_queue_.empty()) {
340 Task* task = deferred_non_nestable_work_queue_.front().task;
341 deferred_non_nestable_work_queue_.pop();
342 // TODO(darin): Delete all tasks once it is safe to do so.
343 //delete task;
initial.commitd7cae122008-07-26 21:49:38344 }
[email protected]001747c2008-09-10 00:37:07345 did_work |= !delayed_work_queue_.empty();
346 while (!delayed_work_queue_.empty()) {
347 Task* task = delayed_work_queue_.top().task;
348 delayed_work_queue_.pop();
349 delete task;
350 }
351 return did_work;
initial.commitd7cae122008-07-26 21:49:38352}
353
[email protected]fc7fb6e2008-08-16 03:09:05354bool MessageLoop::DoWork() {
[email protected]752578562008-09-07 08:08:29355 if (!nestable_tasks_allowed_) {
356 // Task can't be executed right now.
357 return false;
358 }
359
360 for (;;) {
361 ReloadWorkQueue();
362 if (work_queue_.empty())
363 break;
364
365 // Execute oldest task.
366 do {
367 PendingTask pending_task = work_queue_.front();
368 work_queue_.pop();
369 if (!pending_task.delayed_run_time.is_null()) {
370 bool was_empty = delayed_work_queue_.empty();
[email protected]001747c2008-09-10 00:37:07371 AddToDelayedWorkQueue(pending_task);
[email protected]752578562008-09-07 08:08:29372 if (was_empty) // We only schedule the next delayed work item.
373 pump_->ScheduleDelayedWork(pending_task.delayed_run_time);
374 } else {
375 if (DeferOrRunPendingTask(pending_task))
376 return true;
377 }
378 } while (!work_queue_.empty());
379 }
380
381 // Nothing happened.
382 return false;
[email protected]fc7fb6e2008-08-16 03:09:05383}
384
[email protected]b24250fc2008-08-20 06:30:58385bool MessageLoop::DoDelayedWork(Time* next_delayed_work_time) {
[email protected]752578562008-09-07 08:08:29386 if (!nestable_tasks_allowed_ || delayed_work_queue_.empty()) {
387 *next_delayed_work_time = Time();
388 return false;
389 }
390
391 if (delayed_work_queue_.top().delayed_run_time > Time::Now()) {
392 *next_delayed_work_time = delayed_work_queue_.top().delayed_run_time;
393 return false;
394 }
[email protected]fc7fb6e2008-08-16 03:09:05395
[email protected]752578562008-09-07 08:08:29396 PendingTask pending_task = delayed_work_queue_.top();
397 delayed_work_queue_.pop();
398
399 if (!delayed_work_queue_.empty())
400 *next_delayed_work_time = delayed_work_queue_.top().delayed_run_time;
[email protected]fc7fb6e2008-08-16 03:09:05401
[email protected]752578562008-09-07 08:08:29402 return DeferOrRunPendingTask(pending_task);
[email protected]fc7fb6e2008-08-16 03:09:05403}
404
405bool MessageLoop::DoIdleWork() {
406 if (ProcessNextDelayedNonNestableTask())
407 return true;
408
409 if (state_->quit_received)
410 pump_->Quit();
411
412 return false;
413}
414
415//------------------------------------------------------------------------------
416// MessageLoop::AutoRunState
417
418MessageLoop::AutoRunState::AutoRunState(MessageLoop* loop) : loop_(loop) {
419 // Make the loop reference us.
420 previous_state_ = loop_->state_;
421 if (previous_state_) {
422 run_depth = previous_state_->run_depth + 1;
[email protected]ea15e982008-08-15 07:31:20423 } else {
[email protected]fc7fb6e2008-08-16 03:09:05424 run_depth = 1;
[email protected]ea15e982008-08-15 07:31:20425 }
[email protected]fc7fb6e2008-08-16 03:09:05426 loop_->state_ = this;
427
428 // Initialize the other fields:
429 quit_received = false;
430#if defined(OS_WIN)
431 dispatcher = NULL;
432#endif
433}
434
435MessageLoop::AutoRunState::~AutoRunState() {
436 loop_->state_ = previous_state_;
[email protected]a5b94a92008-08-12 23:25:43437}
438
initial.commitd7cae122008-07-26 21:49:38439//------------------------------------------------------------------------------
[email protected]752578562008-09-07 08:08:29440// MessageLoop::PendingTask
initial.commitd7cae122008-07-26 21:49:38441
[email protected]752578562008-09-07 08:08:29442bool MessageLoop::PendingTask::operator<(const PendingTask& other) const {
443 // Since the top of a priority queue is defined as the "greatest" element, we
444 // need to invert the comparison here. We want the smaller time to be at the
445 // top of the heap.
initial.commitd7cae122008-07-26 21:49:38446
[email protected]752578562008-09-07 08:08:29447 if (delayed_run_time < other.delayed_run_time)
448 return false;
initial.commitd7cae122008-07-26 21:49:38449
[email protected]752578562008-09-07 08:08:29450 if (delayed_run_time > other.delayed_run_time)
451 return true;
initial.commitd7cae122008-07-26 21:49:38452
[email protected]752578562008-09-07 08:08:29453 // If the times happen to match, then we use the sequence number to decide.
454 // Compare the difference to support integer roll-over.
455 return (sequence_num - other.sequence_num) > 0;
initial.commitd7cae122008-07-26 21:49:38456}
457
458//------------------------------------------------------------------------------
459// Method and data for histogramming events and actions taken by each instance
460// on each thread.
461
462// static
463bool MessageLoop::enable_histogrammer_ = false;
464
465// static
466void MessageLoop::EnableHistogrammer(bool enable) {
467 enable_histogrammer_ = enable;
468}
469
470void MessageLoop::StartHistogrammer() {
471 if (enable_histogrammer_ && !message_histogram_.get()
472 && StatisticsRecorder::WasStarted()) {
[email protected]fc7fb6e2008-08-16 03:09:05473 DCHECK(!thread_name_.empty());
initial.commitd7cae122008-07-26 21:49:38474 message_histogram_.reset(new LinearHistogram(
475 ASCIIToWide("MsgLoop:" + thread_name_).c_str(),
476 kLeastNonZeroMessageId,
477 kMaxMessageId,
478 kNumberOfDistinctMessagesDisplayed));
479 message_histogram_->SetFlags(message_histogram_->kHexRangePrintingFlag);
480 message_histogram_->SetRangeDescriptions(event_descriptions_);
481 }
482}
483
484void MessageLoop::HistogramEvent(int event) {
485 if (message_histogram_.get())
486 message_histogram_->Add(event);
487}
488
initial.commitd7cae122008-07-26 21:49:38489// Provide a macro that takes an expression (such as a constant, or macro
490// constant) and creates a pair to initalize an array of pairs. In this case,
491// our pair consists of the expressions value, and the "stringized" version
492// of the expression (i.e., the exrpression put in quotes). For example, if
493// we have:
494// #define FOO 2
495// #define BAR 5
496// then the following:
497// VALUE_TO_NUMBER_AND_NAME(FOO + BAR)
498// will expand to:
499// {7, "FOO + BAR"}
500// We use the resulting array as an argument to our histogram, which reads the
501// number as a bucket identifier, and proceeds to use the corresponding name
502// in the pair (i.e., the quoted string) when printing out a histogram.
503#define VALUE_TO_NUMBER_AND_NAME(name) {name, #name},
504
initial.commitd7cae122008-07-26 21:49:38505// static
506const LinearHistogram::DescriptionPair MessageLoop::event_descriptions_[] = {
initial.commitd7cae122008-07-26 21:49:38507 // Provide some pretty print capability in our histogram for our internal
508 // messages.
509
initial.commitd7cae122008-07-26 21:49:38510 // A few events we handle (kindred to messages), and used to profile actions.
511 VALUE_TO_NUMBER_AND_NAME(kTaskRunEvent)
initial.commitd7cae122008-07-26 21:49:38512 VALUE_TO_NUMBER_AND_NAME(kTimerEvent)
513
514 {-1, NULL} // The list must be null terminated, per API to histogram.
515};
license.botbf09a502008-08-24 00:55:55516
[email protected]4d9bdfaf2008-08-26 05:53:57517//------------------------------------------------------------------------------
518// MessageLoopForUI
519
520#if defined(OS_WIN)
521
522void MessageLoopForUI::Run(Dispatcher* dispatcher) {
523 AutoRunState save_state(this);
524 state_->dispatcher = dispatcher;
525 RunHandler();
526}
527
528void MessageLoopForUI::AddObserver(Observer* observer) {
529 pump_win()->AddObserver(observer);
530}
531
532void MessageLoopForUI::RemoveObserver(Observer* observer) {
533 pump_win()->RemoveObserver(observer);
534}
535
536void MessageLoopForUI::WillProcessMessage(const MSG& message) {
537 pump_win()->WillProcessMessage(message);
538}
539void MessageLoopForUI::DidProcessMessage(const MSG& message) {
540 pump_win()->DidProcessMessage(message);
541}
542void MessageLoopForUI::PumpOutPendingPaintMessages() {
543 pump_win()->PumpOutPendingPaintMessages();
544}
545
546#endif // defined(OS_WIN)
547
548//------------------------------------------------------------------------------
549// MessageLoopForIO
550
551#if defined(OS_WIN)
552
553void MessageLoopForIO::WatchObject(HANDLE object, Watcher* watcher) {
554 pump_win()->WatchObject(object, watcher);
555}
556
557#endif // defined(OS_WIN)