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

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 just PeekMessage for WM_PAINT/TIMER messages 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());
121
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 (!message_filter_->DoPeekMessage(&message, NULL, 0, 0,
rvargas (doing something else) 2012/08/31 21:39:49 This is also a candidate for not using the filter.
yoichio 2012/09/03 04:45:08 Done.
209 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
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 !message_filter_->DoPeekMessage(&msg, NULL, WM_MOUSEFIRST, WM_MOUSELAST,
rvargas (doing something else) 2012/08/31 21:39:49 I assume that TSF can generate mouse events... and
yoichio 2012/09/03 04:45:08 Done.
333 PM_NOREMOVE)) {
308 WaitMessage(); 334 WaitMessage();
309 } 335 }
310 return; 336 return;
311 } 337 }
312 338
313 DCHECK_NE(WAIT_FAILED, result) << GetLastError(); 339 DCHECK_NE(WAIT_FAILED, result) << GetLastError();
314 } 340 }
315 341
316 void MessagePumpForUI::HandleWorkMessage() { 342 void MessagePumpForUI::HandleWorkMessage() {
317 // If we are being called outside of the context of Run, then don't try to do 343 // 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 380 // If there are sent messages in the queue then PeekMessage internally
355 // dispatches the message and returns false. We return true in this 381 // dispatches the message and returns false. We return true in this
356 // case to ensure that the message loop peeks again instead of calling 382 // case to ensure that the message loop peeks again instead of calling
357 // MsgWaitForMultipleObjectsEx again. 383 // MsgWaitForMultipleObjectsEx again.
358 bool sent_messages_in_queue = false; 384 bool sent_messages_in_queue = false;
359 DWORD queue_status = GetQueueStatus(QS_SENDMESSAGE); 385 DWORD queue_status = GetQueueStatus(QS_SENDMESSAGE);
360 if (HIWORD(queue_status) & QS_SENDMESSAGE) 386 if (HIWORD(queue_status) & QS_SENDMESSAGE)
361 sent_messages_in_queue = true; 387 sent_messages_in_queue = true;
362 388
363 MSG msg; 389 MSG msg;
364 if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) 390 if (message_filter_->DoPeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
365 return ProcessMessageHelper(msg); 391 return ProcessMessageHelper(msg);
366 392
367 return sent_messages_in_queue; 393 return sent_messages_in_queue;
368 } 394 }
369 395
370 bool MessagePumpForUI::ProcessMessageHelper(const MSG& msg) { 396 bool MessagePumpForUI::ProcessMessageHelper(const MSG& message) {
371 TRACE_EVENT1("base", "MessagePumpForUI::ProcessMessageHelper", 397 TRACE_EVENT1("base", "MessagePumpForUI::ProcessMessageHelper",
372 "message", msg.message); 398 "message", message.message);
373 if (WM_QUIT == msg.message) { 399 if (WM_QUIT == message.message) {
374 // Repost the QUIT message so that it will be retrieved by the primary 400 // Repost the QUIT message so that it will be retrieved by the primary
375 // GetMessage() loop. 401 // GetMessage() loop.
376 state_->should_quit = true; 402 state_->should_quit = true;
377 PostQuitMessage(static_cast<int>(msg.wParam)); 403 PostQuitMessage(static_cast<int>(message.wParam));
378 return false; 404 return false;
379 } 405 }
380 406
381 // While running our main message pump, we discard kMsgHaveWork messages. 407 // While running our main message pump, we discard kMsgHaveWork messages.
382 if (msg.message == kMsgHaveWork && msg.hwnd == message_hwnd_) 408 if (message.message == kMsgHaveWork && message.hwnd == message_hwnd_)
383 return ProcessPumpReplacementMessage(); 409 return ProcessPumpReplacementMessage();
384 410
385 if (CallMsgFilter(const_cast<MSG*>(&msg), kMessageFilterCode)) 411 if (CallMsgFilter(const_cast<MSG*>(&message), kMessageFilterCode))
386 return true; 412 return true;
387 413
388 WillProcessMessage(msg); 414 WillProcessMessage(message);
389 415
390 if (state_->dispatcher) { 416 if (!message_filter_->ProcessMessage(message)) {
391 if (!state_->dispatcher->Dispatch(msg)) 417 if (state_->dispatcher) {
392 state_->should_quit = true; 418 if (!state_->dispatcher->Dispatch(message))
393 } else { 419 state_->should_quit = true;
394 TranslateMessage(&msg); 420 } else {
395 DispatchMessage(&msg); 421 TranslateMessage(&message);
422 DispatchMessage(&message);
423 }
396 } 424 }
397 425
398 DidProcessMessage(msg); 426 DidProcessMessage(message);
399 return true; 427 return true;
400 } 428 }
401 429
402 bool MessagePumpForUI::ProcessPumpReplacementMessage() { 430 bool MessagePumpForUI::ProcessPumpReplacementMessage() {
403 // When we encounter a kMsgHaveWork message, this method is called to peek 431 // 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 432 // 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 433 // 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 434 // 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, 435 // 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 436 // then resets the have_work_ flag (allowing a replacement kMsgHaveWork to
409 // possibly be posted), and finally dispatches that peeked replacement. Note 437 // possibly be posted), and finally dispatches that peeked replacement. Note
410 // that the re-post of kMsgHaveWork may be asynchronous to this thread!! 438 // that the re-post of kMsgHaveWork may be asynchronous to this thread!!
411 439
412 bool have_message = false; 440 bool have_message = false;
413 MSG msg; 441 MSG message;
414 // We should not process all window messages if we are in the context of an 442 // 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. 443 // 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. 444 // This is to ensure that these messages are peeked out by the OS modal loop.
417 if (MessageLoop::current()->os_modal_loop()) { 445 if (MessageLoop::current()->os_modal_loop()) {
418 // We only peek out WM_PAINT and WM_TIMER here for reasons mentioned above. 446 // 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) || 447 have_message = PeekMessage(&message, NULL, WM_PAINT, WM_PAINT, PM_REMOVE) ||
420 PeekMessage(&msg, NULL, WM_TIMER, WM_TIMER, PM_REMOVE); 448 PeekMessage(&message, NULL, WM_TIMER, WM_TIMER, PM_REMOVE);
421 } else { 449 } else {
422 have_message = (0 != PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)); 450 have_message = !!message_filter_->DoPeekMessage(&message, NULL, 0, 0,
451 PM_REMOVE);
423 } 452 }
424 453
425 DCHECK(!have_message || kMsgHaveWork != msg.message || 454 DCHECK(!have_message || kMsgHaveWork != message.message ||
426 msg.hwnd != message_hwnd_); 455 message.hwnd != message_hwnd_);
427 456
428 // Since we discarded a kMsgHaveWork message, we must update the flag. 457 // Since we discarded a kMsgHaveWork message, we must update the flag.
429 int old_have_work = InterlockedExchange(&have_work_, 0); 458 int old_have_work = InterlockedExchange(&have_work_, 0);
430 DCHECK(old_have_work); 459 DCHECK(old_have_work);
431 460
432 // We don't need a special time slice if we didn't have_message to process. 461 // We don't need a special time slice if we didn't have_message to process.
433 if (!have_message) 462 if (!have_message)
434 return false; 463 return false;
435 464
436 // Guarantee we'll get another time slice in the case where we go into native 465 // 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 466 // windows code. This ScheduleWork() may hurt performance a tiny bit when
438 // tasks appear very infrequently, but when the event queue is busy, the 467 // tasks appear very infrequently, but when the event queue is busy, the
439 // kMsgHaveWork events get (percentage wise) rarer and rarer. 468 // kMsgHaveWork events get (percentage wise) rarer and rarer.
440 ScheduleWork(); 469 ScheduleWork();
441 return ProcessMessageHelper(msg); 470 return ProcessMessageHelper(message);
471 }
472
473 void MessagePumpForUI::SetMessageFilter(MessageFilter *message_filter) {
474 message_filter_.reset(message_filter);
442 } 475 }
443 476
444 //----------------------------------------------------------------------------- 477 //-----------------------------------------------------------------------------
445 // MessagePumpForIO public: 478 // MessagePumpForIO public:
446 479
447 MessagePumpForIO::MessagePumpForIO() { 480 MessagePumpForIO::MessagePumpForIO() {
448 port_.Set(CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, NULL, 1)); 481 port_.Set(CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, NULL, 1));
449 DCHECK(port_.IsValid()); 482 DCHECK(port_.IsValid());
450 } 483 }
451 484
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after
660 693
661 // static 694 // static
662 MessagePumpForIO::IOHandler* MessagePumpForIO::KeyToHandler( 695 MessagePumpForIO::IOHandler* MessagePumpForIO::KeyToHandler(
663 ULONG_PTR key, 696 ULONG_PTR key,
664 bool* has_valid_io_context) { 697 bool* has_valid_io_context) {
665 *has_valid_io_context = ((key & 1) == 0); 698 *has_valid_io_context = ((key & 1) == 0);
666 return reinterpret_cast<IOHandler*>(key & ~static_cast<ULONG_PTR>(1)); 699 return reinterpret_cast<IOHandler*>(key & ~static_cast<ULONG_PTR>(1));
667 } 700 }
668 701
669 } // namespace base 702 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698