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

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: add MessageFilter interface Created 8 years, 4 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/text_services_bridge.h"
13 #include "base/win/wrapped_window_proc.h" 14 #include "base/win/wrapped_window_proc.h"
14 15
15 namespace { 16 namespace {
16 17
17 enum MessageLoopProblems { 18 enum MessageLoopProblems {
18 MESSAGE_POST_ERROR, 19 MESSAGE_POST_ERROR,
19 COMPLETION_POST_ERROR, 20 COMPLETION_POST_ERROR,
20 SET_TIMER_ERROR, 21 SET_TIMER_ERROR,
21 MESSAGE_LOOP_PROBLEM_MAX, 22 MESSAGE_LOOP_PROBLEM_MAX,
22 }; 23 };
23 24
25 class SimpleMessageFilter : public base::MessagePumpForUI::MessageFilter {
26 public:
27 virtual ~SimpleMessageFilter() {}
28 virtual bool Init() OVERRIDE {
29 return true;
30 }
31 virtual BOOL DoPeekMessage(MSG* msg,
32 HWND hwnd,
rvargas (doing something else) 2012/08/21 19:45:37 Indent under the first arg
yoichio 2012/08/22 08:06:50 Done.
33 UINT msg_filter_min,
34 UINT msg_filter_max,
35 UINT remove_msg) OVERRIDE {
36 return PeekMessage(msg, hwnd, msg_filter_min, msg_filter_max, 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 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
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() : instance_(NULL) {
99 InitMessageWnd(); 118 InitMessageWnd();
119
120 if (base::win::IsTsfAwareRequired())
121 message_filter_.reset(new win::TextServicesBridge);
122
123 if (!message_filter_.get() || !message_filter_->Init())
124 message_filter_.reset(new SimpleMessageFilter);
rvargas (doing something else) 2012/08/21 19:45:37 + DCHECK Init()
yoichio 2012/08/22 08:06:50 Done.
100 } 125 }
101 126
102 MessagePumpForUI::~MessagePumpForUI() { 127 MessagePumpForUI::~MessagePumpForUI() {
103 DestroyWindow(message_hwnd_); 128 DestroyWindow(message_hwnd_);
104 UnregisterClass(kWndClass, instance_); 129 UnregisterClass(kWndClass, instance_);
105 } 130 }
106 131
107 void MessagePumpForUI::ScheduleWork() { 132 void MessagePumpForUI::ScheduleWork() {
108 if (InterlockedExchange(&have_work_, 1)) 133 if (InterlockedExchange(&have_work_, 1))
109 return; // Someone else continued the pumping. 134 return; // Someone else continued the pumping.
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
175 return; 200 return;
176 201
177 // Create a mini-message-pump to force immediate processing of only Windows 202 // 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 203 // 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 204 // to get the job done. Actual common max is 4 peeks, but we'll be a little
180 // safe here. 205 // safe here.
181 const int kMaxPeekCount = 20; 206 const int kMaxPeekCount = 20;
182 int peek_count; 207 int peek_count;
183 for (peek_count = 0; peek_count < kMaxPeekCount; ++peek_count) { 208 for (peek_count = 0; peek_count < kMaxPeekCount; ++peek_count) {
184 MSG msg; 209 MSG msg;
185 if (!PeekMessage(&msg, NULL, 0, 0, PM_REMOVE | PM_QS_PAINT)) 210 if (!message_filter_->DoPeekMessage(&msg, NULL, 0, 0,
211 PM_REMOVE | PM_QS_PAINT))
186 break; 212 break;
187 ProcessMessageHelper(msg); 213 ProcessMessageHelper(msg);
188 if (state_->should_quit) // Handle WM_QUIT. 214 if (state_->should_quit) // Handle WM_QUIT.
189 break; 215 break;
190 } 216 }
191 // Histogram what was really being used, to help to adjust kMaxPeekCount. 217 // Histogram what was really being used, to help to adjust kMaxPeekCount.
192 DHISTOGRAM_COUNTS("Loop.PumpOutPendingPaintMessages Peeks", peek_count); 218 DHISTOGRAM_COUNTS("Loop.PumpOutPendingPaintMessages Peeks", peek_count);
193 } 219 }
194 220
195 //----------------------------------------------------------------------------- 221 //-----------------------------------------------------------------------------
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
288 314
289 DWORD result; 315 DWORD result;
290 result = MsgWaitForMultipleObjectsEx(0, NULL, delay, QS_ALLINPUT, 316 result = MsgWaitForMultipleObjectsEx(0, NULL, delay, QS_ALLINPUT,
291 MWMO_INPUTAVAILABLE); 317 MWMO_INPUTAVAILABLE);
292 318
293 if (WAIT_OBJECT_0 == result) { 319 if (WAIT_OBJECT_0 == result) {
294 // A WM_* message is available. 320 // A WM_* message is available.
295 // If a parent child relationship exists between windows across threads 321 // If a parent child relationship exists between windows across threads
296 // then their thread inputs are implicitly attached. 322 // then their thread inputs are implicitly attached.
297 // This causes the MsgWaitForMultipleObjectsEx API to return indicating 323 // This causes the MsgWaitForMultipleObjectsEx API to return indicating
298 // that messages are ready for processing (specifically mouse messages 324 // that messages are ready for processing (Specifically, mouse messages
299 // intended for the child window. Occurs if the child window has capture) 325 // intended for the child window may appear if the child window has
300 // The subsequent PeekMessages call fails to return any messages thus 326 // capture).
327 // The subsequent PeekMessages call may fail to return any messages thus
301 // causing us to enter a tight loop at times. 328 // causing us to enter a tight loop at times.
302 // The WaitMessage call below is a workaround to give the child window 329 // The WaitMessage call below is a workaround to give the child window
303 // sometime to process its input messages. 330 // some time to process its input messages.
304 MSG msg = {0}; 331 MSG msg = {0};
305 DWORD queue_status = GetQueueStatus(QS_MOUSE); 332 DWORD queue_status = GetQueueStatus(QS_MOUSE);
306 if (HIWORD(queue_status) & QS_MOUSE && 333 if (HIWORD(queue_status) & QS_MOUSE &&
307 !PeekMessage(&msg, NULL, WM_MOUSEFIRST, WM_MOUSELAST, PM_NOREMOVE)) { 334 !message_filter_->DoPeekMessage(&msg, NULL, WM_MOUSEFIRST, WM_MOUSELAST,
335 PM_NOREMOVE)) {
308 WaitMessage(); 336 WaitMessage();
309 } 337 }
310 return; 338 return;
311 } 339 }
312 340
313 DCHECK_NE(WAIT_FAILED, result) << GetLastError(); 341 DCHECK_NE(WAIT_FAILED, result) << GetLastError();
314 } 342 }
315 343
316 void MessagePumpForUI::HandleWorkMessage() { 344 void MessagePumpForUI::HandleWorkMessage() {
317 // If we are being called outside of the context of Run, then don't try to do 345 // 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 382 // If there are sent messages in the queue then PeekMessage internally
355 // dispatches the message and returns false. We return true in this 383 // dispatches the message and returns false. We return true in this
356 // case to ensure that the message loop peeks again instead of calling 384 // case to ensure that the message loop peeks again instead of calling
357 // MsgWaitForMultipleObjectsEx again. 385 // MsgWaitForMultipleObjectsEx again.
358 bool sent_messages_in_queue = false; 386 bool sent_messages_in_queue = false;
359 DWORD queue_status = GetQueueStatus(QS_SENDMESSAGE); 387 DWORD queue_status = GetQueueStatus(QS_SENDMESSAGE);
360 if (HIWORD(queue_status) & QS_SENDMESSAGE) 388 if (HIWORD(queue_status) & QS_SENDMESSAGE)
361 sent_messages_in_queue = true; 389 sent_messages_in_queue = true;
362 390
363 MSG msg; 391 MSG msg;
364 if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) 392 if (message_filter_->DoPeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
365 return ProcessMessageHelper(msg); 393 return ProcessMessageHelper(msg);
366 394
367 return sent_messages_in_queue; 395 return sent_messages_in_queue;
368 } 396 }
369 397
370 bool MessagePumpForUI::ProcessMessageHelper(const MSG& msg) { 398 bool MessagePumpForUI::ProcessMessageHelper(const MSG& msg) {
371 TRACE_EVENT1("base", "MessagePumpForUI::ProcessMessageHelper", 399 TRACE_EVENT1("base", "MessagePumpForUI::ProcessMessageHelper",
372 "message", msg.message); 400 "message", msg.message);
373 if (WM_QUIT == msg.message) { 401 if (WM_QUIT == msg.message) {
374 // Repost the QUIT message so that it will be retrieved by the primary 402 // Repost the QUIT message so that it will be retrieved by the primary
375 // GetMessage() loop. 403 // GetMessage() loop.
376 state_->should_quit = true; 404 state_->should_quit = true;
377 PostQuitMessage(static_cast<int>(msg.wParam)); 405 PostQuitMessage(static_cast<int>(msg.wParam));
378 return false; 406 return false;
379 } 407 }
380 408
381 // While running our main message pump, we discard kMsgHaveWork messages. 409 // While running our main message pump, we discard kMsgHaveWork messages.
382 if (msg.message == kMsgHaveWork && msg.hwnd == message_hwnd_) 410 if (msg.message == kMsgHaveWork && msg.hwnd == message_hwnd_)
383 return ProcessPumpReplacementMessage(); 411 return ProcessPumpReplacementMessage();
384 412
385 if (CallMsgFilter(const_cast<MSG*>(&msg), kMessageFilterCode)) 413 if (CallMsgFilter(const_cast<MSG*>(&msg), kMessageFilterCode))
386 return true; 414 return true;
387 415
388 WillProcessMessage(msg); 416 WillProcessMessage(msg);
389 417
390 if (state_->dispatcher) { 418 if (!message_filter_->ProcessMessage(msg)) {
391 if (!state_->dispatcher->Dispatch(msg)) 419 if (state_->dispatcher) {
rvargas (doing something else) 2012/08/21 19:45:37 Are you sure this is the correct behavior when the
yoichio 2012/08/22 08:06:50 Yes it is prospected about ITfKeystrokeMgr::KeyDow
392 state_->should_quit = true; 420 if (!state_->dispatcher->Dispatch(msg))
393 } else { 421 state_->should_quit = true;
394 TranslateMessage(&msg); 422 } else {
395 DispatchMessage(&msg); 423 TranslateMessage(&msg);
424 DispatchMessage(&msg);
425 }
396 } 426 }
397 427
398 DidProcessMessage(msg); 428 DidProcessMessage(msg);
399 return true; 429 return true;
400 } 430 }
401 431
402 bool MessagePumpForUI::ProcessPumpReplacementMessage() { 432 bool MessagePumpForUI::ProcessPumpReplacementMessage() {
403 // When we encounter a kMsgHaveWork message, this method is called to peek 433 // 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 434 // 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 435 // 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 436 // 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, 437 // 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 438 // then resets the have_work_ flag (allowing a replacement kMsgHaveWork to
409 // possibly be posted), and finally dispatches that peeked replacement. Note 439 // possibly be posted), and finally dispatches that peeked replacement. Note
410 // that the re-post of kMsgHaveWork may be asynchronous to this thread!! 440 // that the re-post of kMsgHaveWork may be asynchronous to this thread!!
411 441
412 bool have_message = false; 442 bool have_message = false;
413 MSG msg; 443 MSG msg;
414 // We should not process all window messages if we are in the context of an 444 // 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. 445 // 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. 446 // This is to ensure that these messages are peeked out by the OS modal loop.
417 if (MessageLoop::current()->os_modal_loop()) { 447 if (MessageLoop::current()->os_modal_loop()) {
418 // We only peek out WM_PAINT and WM_TIMER here for reasons mentioned above. 448 // 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) || 449 have_message =
420 PeekMessage(&msg, NULL, WM_TIMER, WM_TIMER, PM_REMOVE); 450 message_filter_->DoPeekMessage(&msg, NULL, WM_PAINT, WM_PAINT,
451 PM_REMOVE) ||
452 message_filter_->DoPeekMessage(&msg, NULL, WM_TIMER, WM_TIMER,
453 PM_REMOVE);
421 } else { 454 } else {
422 have_message = (0 != PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)); 455 have_message = (0 != message_filter_->DoPeekMessage(&msg, NULL, 0, 0,
456 PM_REMOVE));
423 } 457 }
424 458
425 DCHECK(!have_message || kMsgHaveWork != msg.message || 459 DCHECK(!have_message || kMsgHaveWork != msg.message ||
426 msg.hwnd != message_hwnd_); 460 msg.hwnd != message_hwnd_);
427 461
428 // Since we discarded a kMsgHaveWork message, we must update the flag. 462 // Since we discarded a kMsgHaveWork message, we must update the flag.
429 int old_have_work = InterlockedExchange(&have_work_, 0); 463 int old_have_work = InterlockedExchange(&have_work_, 0);
430 DCHECK(old_have_work); 464 DCHECK(old_have_work);
431 465
432 // We don't need a special time slice if we didn't have_message to process. 466 // We don't need a special time slice if we didn't have_message to process.
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after
660 694
661 // static 695 // static
662 MessagePumpForIO::IOHandler* MessagePumpForIO::KeyToHandler( 696 MessagePumpForIO::IOHandler* MessagePumpForIO::KeyToHandler(
663 ULONG_PTR key, 697 ULONG_PTR key,
664 bool* has_valid_io_context) { 698 bool* has_valid_io_context) {
665 *has_valid_io_context = ((key & 1) == 0); 699 *has_valid_io_context = ((key & 1) == 0);
666 return reinterpret_cast<IOHandler*>(key & ~static_cast<ULONG_PTR>(1)); 700 return reinterpret_cast<IOHandler*>(key & ~static_cast<ULONG_PTR>(1));
667 } 701 }
668 702
669 } // namespace base 703 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698