Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(236)

Side by Side Diff: base/message_pump_win.cc

Issue 10826223: Replace PeekMessage for TSF awareness (Closed) Base URL: http://git.chromium.org/chromium/src.git@yukawa
Patch Set: use PeekMessage for PumpOutPendingPaintMessages/WaitForWork Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 DCHECK(message_filter_->Init());
rvargas (doing something else) 2012/09/05 21:25:03 Sorry, I missed this. This skips the execution of
yoichio 2012/09/06 00:48:33 Done.
121
cpu_(ooo_6.6-7.5) 2012/09/05 22:25:58 +1
99 InitMessageWnd(); 122 InitMessageWnd();
100 } 123 }
101 124
102 MessagePumpForUI::~MessagePumpForUI() { 125 MessagePumpForUI::~MessagePumpForUI() {
103 DestroyWindow(message_hwnd_); 126 DestroyWindow(message_hwnd_);
104 UnregisterClass(kWndClass, instance_); 127 UnregisterClass(kWndClass, instance_);
105 } 128 }
106 129
107 void MessagePumpForUI::ScheduleWork() { 130 void MessagePumpForUI::ScheduleWork() {
108 if (InterlockedExchange(&have_work_, 1)) 131 if (InterlockedExchange(&have_work_, 1))
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
174 if (!state_) 197 if (!state_)
175 return; 198 return;
176 199
177 // Create a mini-message-pump to force immediate processing of only Windows 200 // 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 201 // 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 202 // to get the job done. Actual common max is 4 peeks, but we'll be a little
180 // safe here. 203 // safe here.
181 const int kMaxPeekCount = 20; 204 const int kMaxPeekCount = 20;
182 int peek_count; 205 int peek_count;
183 for (peek_count = 0; peek_count < kMaxPeekCount; ++peek_count) { 206 for (peek_count = 0; peek_count < kMaxPeekCount; ++peek_count) {
184 MSG msg; 207 MSG message;
185 if (!PeekMessage(&msg, NULL, 0, 0, PM_REMOVE | PM_QS_PAINT)) 208 if (!PeekMessage(&message, NULL, 0, 0, PM_REMOVE | PM_QS_PAINT))
186 break; 209 break;
187 ProcessMessageHelper(msg); 210 ProcessMessageHelper(message);
188 if (state_->should_quit) // Handle WM_QUIT. 211 if (state_->should_quit) // Handle WM_QUIT.
189 break; 212 break;
190 } 213 }
191 // Histogram what was really being used, to help to adjust kMaxPeekCount. 214 // Histogram what was really being used, to help to adjust kMaxPeekCount.
192 DHISTOGRAM_COUNTS("Loop.PumpOutPendingPaintMessages Peeks", peek_count); 215 DHISTOGRAM_COUNTS("Loop.PumpOutPendingPaintMessages Peeks", peek_count);
193 } 216 }
194 217
195 //----------------------------------------------------------------------------- 218 //-----------------------------------------------------------------------------
196 // MessagePumpForUI private: 219 // MessagePumpForUI private:
197 220
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
288 311
289 DWORD result; 312 DWORD result;
290 result = MsgWaitForMultipleObjectsEx(0, NULL, delay, QS_ALLINPUT, 313 result = MsgWaitForMultipleObjectsEx(0, NULL, delay, QS_ALLINPUT,
291 MWMO_INPUTAVAILABLE); 314 MWMO_INPUTAVAILABLE);
292 315
293 if (WAIT_OBJECT_0 == result) { 316 if (WAIT_OBJECT_0 == result) {
294 // A WM_* message is available. 317 // A WM_* message is available.
295 // If a parent child relationship exists between windows across threads 318 // If a parent child relationship exists between windows across threads
296 // then their thread inputs are implicitly attached. 319 // then their thread inputs are implicitly attached.
297 // This causes the MsgWaitForMultipleObjectsEx API to return indicating 320 // This causes the MsgWaitForMultipleObjectsEx API to return indicating
298 // that messages are ready for processing (specifically mouse messages 321 // that messages are ready for processing (Specifically, mouse messages
299 // intended for the child window. Occurs if the child window has capture) 322 // intended for the child window may appear if the child window has
300 // The subsequent PeekMessages call fails to return any messages thus 323 // capture).
324 // The subsequent PeekMessages call may fail to return any messages thus
301 // causing us to enter a tight loop at times. 325 // causing us to enter a tight loop at times.
302 // The WaitMessage call below is a workaround to give the child window 326 // The WaitMessage call below is a workaround to give the child window
303 // sometime to process its input messages. 327 // some time to process its input messages.
304 MSG msg = {0}; 328 MSG msg = {0};
305 DWORD queue_status = GetQueueStatus(QS_MOUSE); 329 DWORD queue_status = GetQueueStatus(QS_MOUSE);
306 if (HIWORD(queue_status) & QS_MOUSE && 330 if (HIWORD(queue_status) & QS_MOUSE &&
307 !PeekMessage(&msg, NULL, WM_MOUSEFIRST, WM_MOUSELAST, PM_NOREMOVE)) { 331 !PeekMessage(&msg, NULL, WM_MOUSEFIRST, WM_MOUSELAST, PM_NOREMOVE)) {
308 WaitMessage(); 332 WaitMessage();
309 } 333 }
310 return; 334 return;
311 } 335 }
312 336
313 DCHECK_NE(WAIT_FAILED, result) << GetLastError(); 337 DCHECK_NE(WAIT_FAILED, result) << GetLastError();
314 } 338 }
315 339
316 void MessagePumpForUI::HandleWorkMessage() { 340 void MessagePumpForUI::HandleWorkMessage() {
317 // If we are being called outside of the context of Run, then don't try to do 341 // 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
354 // If there are sent messages in the queue then PeekMessage internally 378 // If there are sent messages in the queue then PeekMessage internally
355 // dispatches the message and returns false. We return true in this 379 // dispatches the message and returns false. We return true in this
356 // case to ensure that the message loop peeks again instead of calling 380 // case to ensure that the message loop peeks again instead of calling
357 // MsgWaitForMultipleObjectsEx again. 381 // MsgWaitForMultipleObjectsEx again.
358 bool sent_messages_in_queue = false; 382 bool sent_messages_in_queue = false;
359 DWORD queue_status = GetQueueStatus(QS_SENDMESSAGE); 383 DWORD queue_status = GetQueueStatus(QS_SENDMESSAGE);
360 if (HIWORD(queue_status) & QS_SENDMESSAGE) 384 if (HIWORD(queue_status) & QS_SENDMESSAGE)
361 sent_messages_in_queue = true; 385 sent_messages_in_queue = true;
362 386
363 MSG msg; 387 MSG msg;
364 if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) 388 if (message_filter_->DoPeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
365 return ProcessMessageHelper(msg); 389 return ProcessMessageHelper(msg);
366 390
367 return sent_messages_in_queue; 391 return sent_messages_in_queue;
368 } 392 }
369 393
370 bool MessagePumpForUI::ProcessMessageHelper(const MSG& msg) { 394 bool MessagePumpForUI::ProcessMessageHelper(const MSG& message) {
371 TRACE_EVENT1("base", "MessagePumpForUI::ProcessMessageHelper", 395 TRACE_EVENT1("base", "MessagePumpForUI::ProcessMessageHelper",
372 "message", msg.message); 396 "message", message.message);
373 if (WM_QUIT == msg.message) { 397 if (WM_QUIT == message.message) {
374 // Repost the QUIT message so that it will be retrieved by the primary 398 // Repost the QUIT message so that it will be retrieved by the primary
375 // GetMessage() loop. 399 // GetMessage() loop.
376 state_->should_quit = true; 400 state_->should_quit = true;
377 PostQuitMessage(static_cast<int>(msg.wParam)); 401 PostQuitMessage(static_cast<int>(message.wParam));
378 return false; 402 return false;
379 } 403 }
380 404
381 // While running our main message pump, we discard kMsgHaveWork messages. 405 // While running our main message pump, we discard kMsgHaveWork messages.
382 if (msg.message == kMsgHaveWork && msg.hwnd == message_hwnd_) 406 if (message.message == kMsgHaveWork && message.hwnd == message_hwnd_)
383 return ProcessPumpReplacementMessage(); 407 return ProcessPumpReplacementMessage();
384 408
385 if (CallMsgFilter(const_cast<MSG*>(&msg), kMessageFilterCode)) 409 if (CallMsgFilter(const_cast<MSG*>(&message), kMessageFilterCode))
386 return true; 410 return true;
387 411
388 WillProcessMessage(msg); 412 WillProcessMessage(message);
389 413
390 if (state_->dispatcher) { 414 if (!message_filter_->ProcessMessage(message)) {
391 if (!state_->dispatcher->Dispatch(msg)) 415 if (state_->dispatcher) {
392 state_->should_quit = true; 416 if (!state_->dispatcher->Dispatch(message))
393 } else { 417 state_->should_quit = true;
394 TranslateMessage(&msg); 418 } else {
395 DispatchMessage(&msg); 419 TranslateMessage(&message);
420 DispatchMessage(&message);
421 }
396 } 422 }
397 423
398 DidProcessMessage(msg); 424 DidProcessMessage(message);
399 return true; 425 return true;
400 } 426 }
401 427
402 bool MessagePumpForUI::ProcessPumpReplacementMessage() { 428 bool MessagePumpForUI::ProcessPumpReplacementMessage() {
403 // When we encounter a kMsgHaveWork message, this method is called to peek 429 // 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 430 // 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 431 // 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 432 // 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, 433 // 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 434 // then resets the have_work_ flag (allowing a replacement kMsgHaveWork to
409 // possibly be posted), and finally dispatches that peeked replacement. Note 435 // possibly be posted), and finally dispatches that peeked replacement. Note
410 // that the re-post of kMsgHaveWork may be asynchronous to this thread!! 436 // that the re-post of kMsgHaveWork may be asynchronous to this thread!!
411 437
412 bool have_message = false; 438 bool have_message = false;
413 MSG msg; 439 MSG message;
414 // We should not process all window messages if we are in the context of an 440 // 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. 441 // 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. 442 // This is to ensure that these messages are peeked out by the OS modal loop.
417 if (MessageLoop::current()->os_modal_loop()) { 443 if (MessageLoop::current()->os_modal_loop()) {
418 // We only peek out WM_PAINT and WM_TIMER here for reasons mentioned above. 444 // 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) || 445 have_message = PeekMessage(&message, NULL, WM_PAINT, WM_PAINT, PM_REMOVE) ||
420 PeekMessage(&msg, NULL, WM_TIMER, WM_TIMER, PM_REMOVE); 446 PeekMessage(&message, NULL, WM_TIMER, WM_TIMER, PM_REMOVE);
421 } else { 447 } else {
422 have_message = (0 != PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)); 448 have_message = !!message_filter_->DoPeekMessage(&message, NULL, 0, 0,
449 PM_REMOVE);
423 } 450 }
424 451
425 DCHECK(!have_message || kMsgHaveWork != msg.message || 452 DCHECK(!have_message || kMsgHaveWork != message.message ||
426 msg.hwnd != message_hwnd_); 453 message.hwnd != message_hwnd_);
427 454
428 // Since we discarded a kMsgHaveWork message, we must update the flag. 455 // Since we discarded a kMsgHaveWork message, we must update the flag.
429 int old_have_work = InterlockedExchange(&have_work_, 0); 456 int old_have_work = InterlockedExchange(&have_work_, 0);
430 DCHECK(old_have_work); 457 DCHECK(old_have_work);
431 458
432 // We don't need a special time slice if we didn't have_message to process. 459 // We don't need a special time slice if we didn't have_message to process.
433 if (!have_message) 460 if (!have_message)
434 return false; 461 return false;
435 462
436 // Guarantee we'll get another time slice in the case where we go into native 463 // 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 464 // windows code. This ScheduleWork() may hurt performance a tiny bit when
438 // tasks appear very infrequently, but when the event queue is busy, the 465 // tasks appear very infrequently, but when the event queue is busy, the
439 // kMsgHaveWork events get (percentage wise) rarer and rarer. 466 // kMsgHaveWork events get (percentage wise) rarer and rarer.
440 ScheduleWork(); 467 ScheduleWork();
441 return ProcessMessageHelper(msg); 468 return ProcessMessageHelper(message);
469 }
470
471 void MessagePumpForUI::SetMessageFilter(MessageFilter *message_filter) {
472 message_filter_.reset(message_filter);
442 } 473 }
443 474
444 //----------------------------------------------------------------------------- 475 //-----------------------------------------------------------------------------
445 // MessagePumpForIO public: 476 // MessagePumpForIO public:
446 477
447 MessagePumpForIO::MessagePumpForIO() { 478 MessagePumpForIO::MessagePumpForIO() {
448 port_.Set(CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, NULL, 1)); 479 port_.Set(CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, NULL, 1));
449 DCHECK(port_.IsValid()); 480 DCHECK(port_.IsValid());
450 } 481 }
451 482
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after
660 691
661 // static 692 // static
662 MessagePumpForIO::IOHandler* MessagePumpForIO::KeyToHandler( 693 MessagePumpForIO::IOHandler* MessagePumpForIO::KeyToHandler(
663 ULONG_PTR key, 694 ULONG_PTR key,
664 bool* has_valid_io_context) { 695 bool* has_valid_io_context) {
665 *has_valid_io_context = ((key & 1) == 0); 696 *has_valid_io_context = ((key & 1) == 0);
666 return reinterpret_cast<IOHandler*>(key & ~static_cast<ULONG_PTR>(1)); 697 return reinterpret_cast<IOHandler*>(key & ~static_cast<ULONG_PTR>(1));
667 } 698 }
668 699
669 } // namespace base 700 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698