OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/message_pump_win.h" | 5 #include "base/message_pump_win.h" |
6 | 6 |
7 #include <math.h> | 7 #include <math.h> |
8 | 8 |
9 #include "base/debug/trace_event.h" | 9 #include "base/debug/trace_event.h" |
10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
11 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
12 #include "base/process_util.h" | 12 #include "base/process_util.h" |
13 #include "base/win/wrapped_window_proc.h" | 13 #include "base/win/wrapped_window_proc.h" |
14 | 14 |
15 namespace { | 15 namespace { |
16 | 16 |
17 enum MessageLoopProblems { | 17 enum MessageLoopProblems { |
18 MESSAGE_POST_ERROR, | 18 MESSAGE_POST_ERROR, |
19 COMPLETION_POST_ERROR, | 19 COMPLETION_POST_ERROR, |
20 SET_TIMER_ERROR, | 20 SET_TIMER_ERROR, |
21 MESSAGE_LOOP_PROBLEM_MAX, | 21 MESSAGE_LOOP_PROBLEM_MAX, |
22 }; | 22 }; |
23 | 23 |
| 24 class SimpleMessageFilter : public base::MessagePumpForUI::MessageFilter { |
| 25 public: |
| 26 virtual ~SimpleMessageFilter() {} |
| 27 virtual bool Init() OVERRIDE { |
| 28 return true; |
| 29 } |
| 30 virtual BOOL DoPeekMessage(MSG* message, |
| 31 HWND window, |
| 32 UINT msg_filter_min, |
| 33 UINT msg_filter_max, |
| 34 UINT remove_msg) OVERRIDE { |
| 35 return PeekMessage(message, window, msg_filter_min, msg_filter_max, |
| 36 remove_msg); |
| 37 } |
| 38 virtual bool ProcessMessage(const MSG& message) OVERRIDE { |
| 39 return false; |
| 40 } |
| 41 }; |
| 42 |
24 } // namespace | 43 } // namespace |
25 | 44 |
26 namespace base { | 45 namespace base { |
27 | 46 |
28 static const wchar_t kWndClass[] = L"Chrome_MessagePumpWindow"; | 47 static const wchar_t kWndClass[] = L"Chrome_MessagePumpWindow"; |
29 | 48 |
30 // Message sent to get an additional time slice for pumping (processing) another | 49 // Message sent to get an additional time slice for pumping (processing) another |
31 // task (a series of such messages creates a continuous task pump). | 50 // task (a series of such messages creates a continuous task pump). |
32 static const int kMsgHaveWork = WM_USER + 1; | 51 static const int kMsgHaveWork = WM_USER + 1; |
33 | 52 |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 int delay = static_cast<int>(timeout); | 107 int delay = static_cast<int>(timeout); |
89 if (delay < 0) | 108 if (delay < 0) |
90 delay = 0; | 109 delay = 0; |
91 | 110 |
92 return delay; | 111 return delay; |
93 } | 112 } |
94 | 113 |
95 //----------------------------------------------------------------------------- | 114 //----------------------------------------------------------------------------- |
96 // MessagePumpForUI public: | 115 // MessagePumpForUI public: |
97 | 116 |
98 MessagePumpForUI::MessagePumpForUI() : instance_(NULL) { | 117 MessagePumpForUI::MessagePumpForUI() |
| 118 : instance_(NULL), |
| 119 message_filter_(new SimpleMessageFilter) { |
| 120 bool init = message_filter_->Init(); |
| 121 DCHECK(init); |
| 122 |
99 InitMessageWnd(); | 123 InitMessageWnd(); |
100 } | 124 } |
101 | 125 |
102 MessagePumpForUI::~MessagePumpForUI() { | 126 MessagePumpForUI::~MessagePumpForUI() { |
103 DestroyWindow(message_hwnd_); | 127 DestroyWindow(message_hwnd_); |
104 UnregisterClass(kWndClass, instance_); | 128 UnregisterClass(kWndClass, instance_); |
105 } | 129 } |
106 | 130 |
107 void MessagePumpForUI::ScheduleWork() { | 131 void MessagePumpForUI::ScheduleWork() { |
108 if (InterlockedExchange(&have_work_, 1)) | 132 if (InterlockedExchange(&have_work_, 1)) |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
174 if (!state_) | 198 if (!state_) |
175 return; | 199 return; |
176 | 200 |
177 // Create a mini-message-pump to force immediate processing of only Windows | 201 // Create a mini-message-pump to force immediate processing of only Windows |
178 // WM_PAINT messages. Don't provide an infinite loop, but do enough peeking | 202 // WM_PAINT messages. Don't provide an infinite loop, but do enough peeking |
179 // to get the job done. Actual common max is 4 peeks, but we'll be a little | 203 // to get the job done. Actual common max is 4 peeks, but we'll be a little |
180 // safe here. | 204 // safe here. |
181 const int kMaxPeekCount = 20; | 205 const int kMaxPeekCount = 20; |
182 int peek_count; | 206 int peek_count; |
183 for (peek_count = 0; peek_count < kMaxPeekCount; ++peek_count) { | 207 for (peek_count = 0; peek_count < kMaxPeekCount; ++peek_count) { |
184 MSG msg; | 208 MSG message; |
185 if (!PeekMessage(&msg, NULL, 0, 0, PM_REMOVE | PM_QS_PAINT)) | 209 if (!PeekMessage(&message, NULL, 0, 0, PM_REMOVE | PM_QS_PAINT)) |
186 break; | 210 break; |
187 ProcessMessageHelper(msg); | 211 ProcessMessageHelper(message); |
188 if (state_->should_quit) // Handle WM_QUIT. | 212 if (state_->should_quit) // Handle WM_QUIT. |
189 break; | 213 break; |
190 } | 214 } |
191 // Histogram what was really being used, to help to adjust kMaxPeekCount. | 215 // Histogram what was really being used, to help to adjust kMaxPeekCount. |
192 DHISTOGRAM_COUNTS("Loop.PumpOutPendingPaintMessages Peeks", peek_count); | 216 DHISTOGRAM_COUNTS("Loop.PumpOutPendingPaintMessages Peeks", peek_count); |
193 } | 217 } |
194 | 218 |
195 //----------------------------------------------------------------------------- | 219 //----------------------------------------------------------------------------- |
196 // MessagePumpForUI private: | 220 // MessagePumpForUI private: |
197 | 221 |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
288 | 312 |
289 DWORD result; | 313 DWORD result; |
290 result = MsgWaitForMultipleObjectsEx(0, NULL, delay, QS_ALLINPUT, | 314 result = MsgWaitForMultipleObjectsEx(0, NULL, delay, QS_ALLINPUT, |
291 MWMO_INPUTAVAILABLE); | 315 MWMO_INPUTAVAILABLE); |
292 | 316 |
293 if (WAIT_OBJECT_0 == result) { | 317 if (WAIT_OBJECT_0 == result) { |
294 // A WM_* message is available. | 318 // A WM_* message is available. |
295 // If a parent child relationship exists between windows across threads | 319 // If a parent child relationship exists between windows across threads |
296 // then their thread inputs are implicitly attached. | 320 // then their thread inputs are implicitly attached. |
297 // This causes the MsgWaitForMultipleObjectsEx API to return indicating | 321 // This causes the MsgWaitForMultipleObjectsEx API to return indicating |
298 // that messages are ready for processing (specifically mouse messages | 322 // that messages are ready for processing (Specifically, mouse messages |
299 // intended for the child window. Occurs if the child window has capture) | 323 // intended for the child window may appear if the child window has |
300 // The subsequent PeekMessages call fails to return any messages thus | 324 // capture). |
| 325 // The subsequent PeekMessages call may fail to return any messages thus |
301 // causing us to enter a tight loop at times. | 326 // causing us to enter a tight loop at times. |
302 // The WaitMessage call below is a workaround to give the child window | 327 // The WaitMessage call below is a workaround to give the child window |
303 // sometime to process its input messages. | 328 // some time to process its input messages. |
304 MSG msg = {0}; | 329 MSG msg = {0}; |
305 DWORD queue_status = GetQueueStatus(QS_MOUSE); | 330 DWORD queue_status = GetQueueStatus(QS_MOUSE); |
306 if (HIWORD(queue_status) & QS_MOUSE && | 331 if (HIWORD(queue_status) & QS_MOUSE && |
307 !PeekMessage(&msg, NULL, WM_MOUSEFIRST, WM_MOUSELAST, PM_NOREMOVE)) { | 332 !PeekMessage(&msg, NULL, WM_MOUSEFIRST, WM_MOUSELAST, PM_NOREMOVE)) { |
308 WaitMessage(); | 333 WaitMessage(); |
309 } | 334 } |
310 return; | 335 return; |
311 } | 336 } |
312 | 337 |
313 DCHECK_NE(WAIT_FAILED, result) << GetLastError(); | 338 DCHECK_NE(WAIT_FAILED, result) << GetLastError(); |
314 } | 339 } |
315 | 340 |
316 void MessagePumpForUI::HandleWorkMessage() { | 341 void MessagePumpForUI::HandleWorkMessage() { |
317 // If we are being called outside of the context of Run, then don't try to do | 342 // If we are being called outside of the context of Run, then don't try to do |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
354 // If there are sent messages in the queue then PeekMessage internally | 379 // If there are sent messages in the queue then PeekMessage internally |
355 // dispatches the message and returns false. We return true in this | 380 // dispatches the message and returns false. We return true in this |
356 // case to ensure that the message loop peeks again instead of calling | 381 // case to ensure that the message loop peeks again instead of calling |
357 // MsgWaitForMultipleObjectsEx again. | 382 // MsgWaitForMultipleObjectsEx again. |
358 bool sent_messages_in_queue = false; | 383 bool sent_messages_in_queue = false; |
359 DWORD queue_status = GetQueueStatus(QS_SENDMESSAGE); | 384 DWORD queue_status = GetQueueStatus(QS_SENDMESSAGE); |
360 if (HIWORD(queue_status) & QS_SENDMESSAGE) | 385 if (HIWORD(queue_status) & QS_SENDMESSAGE) |
361 sent_messages_in_queue = true; | 386 sent_messages_in_queue = true; |
362 | 387 |
363 MSG msg; | 388 MSG msg; |
364 if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) | 389 if (message_filter_->DoPeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) |
365 return ProcessMessageHelper(msg); | 390 return ProcessMessageHelper(msg); |
366 | 391 |
367 return sent_messages_in_queue; | 392 return sent_messages_in_queue; |
368 } | 393 } |
369 | 394 |
370 bool MessagePumpForUI::ProcessMessageHelper(const MSG& msg) { | 395 bool MessagePumpForUI::ProcessMessageHelper(const MSG& message) { |
371 TRACE_EVENT1("base", "MessagePumpForUI::ProcessMessageHelper", | 396 TRACE_EVENT1("base", "MessagePumpForUI::ProcessMessageHelper", |
372 "message", msg.message); | 397 "message", message.message); |
373 if (WM_QUIT == msg.message) { | 398 if (WM_QUIT == message.message) { |
374 // Repost the QUIT message so that it will be retrieved by the primary | 399 // Repost the QUIT message so that it will be retrieved by the primary |
375 // GetMessage() loop. | 400 // GetMessage() loop. |
376 state_->should_quit = true; | 401 state_->should_quit = true; |
377 PostQuitMessage(static_cast<int>(msg.wParam)); | 402 PostQuitMessage(static_cast<int>(message.wParam)); |
378 return false; | 403 return false; |
379 } | 404 } |
380 | 405 |
381 // While running our main message pump, we discard kMsgHaveWork messages. | 406 // While running our main message pump, we discard kMsgHaveWork messages. |
382 if (msg.message == kMsgHaveWork && msg.hwnd == message_hwnd_) | 407 if (message.message == kMsgHaveWork && message.hwnd == message_hwnd_) |
383 return ProcessPumpReplacementMessage(); | 408 return ProcessPumpReplacementMessage(); |
384 | 409 |
385 if (CallMsgFilter(const_cast<MSG*>(&msg), kMessageFilterCode)) | 410 if (CallMsgFilter(const_cast<MSG*>(&message), kMessageFilterCode)) |
386 return true; | 411 return true; |
387 | 412 |
388 WillProcessMessage(msg); | 413 WillProcessMessage(message); |
389 | 414 |
390 if (state_->dispatcher) { | 415 if (!message_filter_->ProcessMessage(message)) { |
391 if (!state_->dispatcher->Dispatch(msg)) | 416 if (state_->dispatcher) { |
392 state_->should_quit = true; | 417 if (!state_->dispatcher->Dispatch(message)) |
393 } else { | 418 state_->should_quit = true; |
394 TranslateMessage(&msg); | 419 } else { |
395 DispatchMessage(&msg); | 420 TranslateMessage(&message); |
| 421 DispatchMessage(&message); |
| 422 } |
396 } | 423 } |
397 | 424 |
398 DidProcessMessage(msg); | 425 DidProcessMessage(message); |
399 return true; | 426 return true; |
400 } | 427 } |
401 | 428 |
402 bool MessagePumpForUI::ProcessPumpReplacementMessage() { | 429 bool MessagePumpForUI::ProcessPumpReplacementMessage() { |
403 // When we encounter a kMsgHaveWork message, this method is called to peek | 430 // When we encounter a kMsgHaveWork message, this method is called to peek |
404 // and process a replacement message, such as a WM_PAINT or WM_TIMER. The | 431 // and process a replacement message, such as a WM_PAINT or WM_TIMER. The |
405 // goal is to make the kMsgHaveWork as non-intrusive as possible, even though | 432 // goal is to make the kMsgHaveWork as non-intrusive as possible, even though |
406 // a continuous stream of such messages are posted. This method carefully | 433 // a continuous stream of such messages are posted. This method carefully |
407 // peeks a message while there is no chance for a kMsgHaveWork to be pending, | 434 // peeks a message while there is no chance for a kMsgHaveWork to be pending, |
408 // then resets the have_work_ flag (allowing a replacement kMsgHaveWork to | 435 // then resets the have_work_ flag (allowing a replacement kMsgHaveWork to |
409 // possibly be posted), and finally dispatches that peeked replacement. Note | 436 // possibly be posted), and finally dispatches that peeked replacement. Note |
410 // that the re-post of kMsgHaveWork may be asynchronous to this thread!! | 437 // that the re-post of kMsgHaveWork may be asynchronous to this thread!! |
411 | 438 |
412 bool have_message = false; | 439 bool have_message = false; |
413 MSG msg; | 440 MSG message; |
414 // We should not process all window messages if we are in the context of an | 441 // We should not process all window messages if we are in the context of an |
415 // OS modal loop, i.e. in the context of a windows API call like MessageBox. | 442 // OS modal loop, i.e. in the context of a windows API call like MessageBox. |
416 // This is to ensure that these messages are peeked out by the OS modal loop. | 443 // This is to ensure that these messages are peeked out by the OS modal loop. |
417 if (MessageLoop::current()->os_modal_loop()) { | 444 if (MessageLoop::current()->os_modal_loop()) { |
418 // We only peek out WM_PAINT and WM_TIMER here for reasons mentioned above. | 445 // We only peek out WM_PAINT and WM_TIMER here for reasons mentioned above. |
419 have_message = PeekMessage(&msg, NULL, WM_PAINT, WM_PAINT, PM_REMOVE) || | 446 have_message = PeekMessage(&message, NULL, WM_PAINT, WM_PAINT, PM_REMOVE) || |
420 PeekMessage(&msg, NULL, WM_TIMER, WM_TIMER, PM_REMOVE); | 447 PeekMessage(&message, NULL, WM_TIMER, WM_TIMER, PM_REMOVE); |
421 } else { | 448 } else { |
422 have_message = (0 != PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)); | 449 have_message = !!message_filter_->DoPeekMessage(&message, NULL, 0, 0, |
| 450 PM_REMOVE); |
423 } | 451 } |
424 | 452 |
425 DCHECK(!have_message || kMsgHaveWork != msg.message || | 453 DCHECK(!have_message || kMsgHaveWork != message.message || |
426 msg.hwnd != message_hwnd_); | 454 message.hwnd != message_hwnd_); |
427 | 455 |
428 // Since we discarded a kMsgHaveWork message, we must update the flag. | 456 // Since we discarded a kMsgHaveWork message, we must update the flag. |
429 int old_have_work = InterlockedExchange(&have_work_, 0); | 457 int old_have_work = InterlockedExchange(&have_work_, 0); |
430 DCHECK(old_have_work); | 458 DCHECK(old_have_work); |
431 | 459 |
432 // We don't need a special time slice if we didn't have_message to process. | 460 // We don't need a special time slice if we didn't have_message to process. |
433 if (!have_message) | 461 if (!have_message) |
434 return false; | 462 return false; |
435 | 463 |
436 // Guarantee we'll get another time slice in the case where we go into native | 464 // Guarantee we'll get another time slice in the case where we go into native |
437 // windows code. This ScheduleWork() may hurt performance a tiny bit when | 465 // windows code. This ScheduleWork() may hurt performance a tiny bit when |
438 // tasks appear very infrequently, but when the event queue is busy, the | 466 // tasks appear very infrequently, but when the event queue is busy, the |
439 // kMsgHaveWork events get (percentage wise) rarer and rarer. | 467 // kMsgHaveWork events get (percentage wise) rarer and rarer. |
440 ScheduleWork(); | 468 ScheduleWork(); |
441 return ProcessMessageHelper(msg); | 469 return ProcessMessageHelper(message); |
| 470 } |
| 471 |
| 472 void MessagePumpForUI::SetMessageFilter(MessageFilter *message_filter) { |
| 473 message_filter_.reset(message_filter); |
442 } | 474 } |
443 | 475 |
444 //----------------------------------------------------------------------------- | 476 //----------------------------------------------------------------------------- |
445 // MessagePumpForIO public: | 477 // MessagePumpForIO public: |
446 | 478 |
447 MessagePumpForIO::MessagePumpForIO() { | 479 MessagePumpForIO::MessagePumpForIO() { |
448 port_.Set(CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, NULL, 1)); | 480 port_.Set(CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, NULL, 1)); |
449 DCHECK(port_.IsValid()); | 481 DCHECK(port_.IsValid()); |
450 } | 482 } |
451 | 483 |
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
660 | 692 |
661 // static | 693 // static |
662 MessagePumpForIO::IOHandler* MessagePumpForIO::KeyToHandler( | 694 MessagePumpForIO::IOHandler* MessagePumpForIO::KeyToHandler( |
663 ULONG_PTR key, | 695 ULONG_PTR key, |
664 bool* has_valid_io_context) { | 696 bool* has_valid_io_context) { |
665 *has_valid_io_context = ((key & 1) == 0); | 697 *has_valid_io_context = ((key & 1) == 0); |
666 return reinterpret_cast<IOHandler*>(key & ~static_cast<ULONG_PTR>(1)); | 698 return reinterpret_cast<IOHandler*>(key & ~static_cast<ULONG_PTR>(1)); |
667 } | 699 } |
668 | 700 |
669 } // namespace base | 701 } // namespace base |
OLD | NEW |