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

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: modify member name 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
« base/message_pump_win.h ('K') | « base/message_pump_win.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 <imm.h>
7 #include <math.h> 8 #include <math.h>
9 #include <msctf.h>
10 #include <Windows.h>
8 11
9 #include "base/debug/trace_event.h" 12 #include "base/debug/trace_event.h"
10 #include "base/message_loop.h" 13 #include "base/message_loop.h"
11 #include "base/metrics/histogram.h" 14 #include "base/metrics/histogram.h"
12 #include "base/process_util.h" 15 #include "base/process_util.h"
16 #include "base/win/metro.h"
17 #include "base/win/scoped_comptr.h"
13 #include "base/win/wrapped_window_proc.h" 18 #include "base/win/wrapped_window_proc.h"
14 19
20 namespace base {
21
22 static const wchar_t kWndClass[] = L"Chrome_MessagePumpWindow";
23
24 // Message sent to get an additional time slice for pumping (processing) another
25 // task (a series of such messages creates a continuous task pump).
26 static const int kMsgHaveWork = WM_USER + 1;
27
28 class MessagePumpTSFInternal {
29 public:
30 TfClientId client_id;
31 bool is_initialized;
32 base::win::ScopedComPtr<ITfThreadMgr> thread_mgr;
33 base::win::ScopedComPtr<ITfMessagePump> message_pump;
34 base::win::ScopedComPtr<ITfKeystrokeMgr> keystroke_mgr;
35 MessagePumpTSFInternal()
36 : client_id(TF_CLIENTID_NULL), is_initialized(true) {
37 is_initialized &= SUCCEEDED(thread_mgr.CreateInstance(CLSID_TF_ThreadMgr));
38 if (!thread_mgr)
39 return;
40
41 is_initialized &= SUCCEEDED(message_pump.QueryFrom(thread_mgr));
42 is_initialized &= SUCCEEDED(keystroke_mgr.QueryFrom(thread_mgr));
43 // When activate succeeded, |client_id| is set valid value.
44 is_initialized &= SUCCEEDED(thread_mgr->Activate(&client_id));
45 }
46 ~MessagePumpTSFInternal() {
47 if (thread_mgr && client_id != TF_CLIENTID_NULL)
48 thread_mgr->Deactivate();
49 }
50
51 bool ForwardKeyMessageForTSF(const MSG& msg) {
52 if (!is_initialized)
53 return false;
54
55 if(msg.message == WM_KEYDOWN) {
56 BOOL eaten = FALSE;
57 HRESULT hr = keystroke_mgr->TestKeyDown(msg.wParam, msg.lParam, &eaten);
58 if (FAILED(hr) && !eaten)
59 return false;
60 eaten = FALSE;
61 hr = keystroke_mgr->KeyDown(msg.wParam, msg.lParam, &eaten);
62 return (SUCCEEDED(hr) && eaten);
63 }
64
65 if(msg.message == WM_KEYUP) {
66 BOOL eaten = FALSE;
67 HRESULT hr = keystroke_mgr->TestKeyUp(msg.wParam, msg.lParam, &eaten);
68 if (FAILED(hr) && !eaten)
69 return false;
70 eaten = FALSE;
71 hr = keystroke_mgr->KeyUp(msg.wParam, msg.lParam, &eaten);
72 return (SUCCEEDED(hr) && eaten);
73 }
74
75 return false;
76 }
77
78 bool PeekMessageForTSF(MSG* msg, HWND hwnd, UINT wmin, UINT wmax, UINT wmsg) {
79 if (!is_initialized)
80 // FallBack.
81 return !!::PeekMessage(msg, hwnd, wmin, wmax, wmsg);
82
83 BOOL result = FALSE;
84 if(FAILED(message_pump->PeekMessage(msg, hwnd, wmin, wmax, wmsg, &result)))
85 result = FALSE;
86 return !!result;
87 }
88 };
89
15 namespace { 90 namespace {
16 91
17 enum MessageLoopProblems { 92 enum MessageLoopProblems {
18 MESSAGE_POST_ERROR, 93 MESSAGE_POST_ERROR,
19 COMPLETION_POST_ERROR, 94 COMPLETION_POST_ERROR,
20 SET_TIMER_ERROR, 95 SET_TIMER_ERROR,
21 MESSAGE_LOOP_PROBLEM_MAX, 96 MESSAGE_LOOP_PROBLEM_MAX,
22 }; 97 };
23 98
24 } // namespace 99 } // namespace
25 100
26 namespace base {
27
28 static const wchar_t kWndClass[] = L"Chrome_MessagePumpWindow";
29
30 // Message sent to get an additional time slice for pumping (processing) another
31 // task (a series of such messages creates a continuous task pump).
32 static const int kMsgHaveWork = WM_USER + 1;
33
34 //----------------------------------------------------------------------------- 101 //-----------------------------------------------------------------------------
35 // MessagePumpWin public: 102 // MessagePumpWin public:
36 103
37 void MessagePumpWin::AddObserver(MessagePumpObserver* observer) { 104 void MessagePumpWin::AddObserver(MessagePumpObserver* observer) {
38 observers_.AddObserver(observer); 105 observers_.AddObserver(observer);
39 } 106 }
40 107
41 void MessagePumpWin::RemoveObserver(MessagePumpObserver* observer) { 108 void MessagePumpWin::RemoveObserver(MessagePumpObserver* observer) {
42 observers_.RemoveObserver(observer); 109 observers_.RemoveObserver(observer);
43 } 110 }
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
90 delay = 0; 157 delay = 0;
91 158
92 return delay; 159 return delay;
93 } 160 }
94 161
95 //----------------------------------------------------------------------------- 162 //-----------------------------------------------------------------------------
96 // MessagePumpForUI public: 163 // MessagePumpForUI public:
97 164
98 MessagePumpForUI::MessagePumpForUI() : instance_(NULL) { 165 MessagePumpForUI::MessagePumpForUI() : instance_(NULL) {
99 InitMessageWnd(); 166 InitMessageWnd();
167
168 if (base::win::IsTsfAwareRequired())
169 tsf_message_pump_.reset(new MessagePumpTSFInternal);
100 } 170 }
101 171
102 MessagePumpForUI::~MessagePumpForUI() { 172 MessagePumpForUI::~MessagePumpForUI() {
173 if (base::win::IsTsfAwareRequired())
174 tsf_message_pump_.reset(NULL);
103 DestroyWindow(message_hwnd_); 175 DestroyWindow(message_hwnd_);
104 UnregisterClass(kWndClass, instance_); 176 UnregisterClass(kWndClass, instance_);
105 } 177 }
106 178
107 void MessagePumpForUI::ScheduleWork() { 179 void MessagePumpForUI::ScheduleWork() {
108 if (InterlockedExchange(&have_work_, 1)) 180 if (InterlockedExchange(&have_work_, 1))
109 return; // Someone else continued the pumping. 181 return; // Someone else continued the pumping.
110 182
111 // Make sure the MessagePump does some work for us. 183 // Make sure the MessagePump does some work for us.
112 BOOL ret = PostMessage(message_hwnd_, kMsgHaveWork, 184 BOOL ret = PostMessage(message_hwnd_, kMsgHaveWork,
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
175 return; 247 return;
176 248
177 // Create a mini-message-pump to force immediate processing of only Windows 249 // 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 250 // 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 251 // to get the job done. Actual common max is 4 peeks, but we'll be a little
180 // safe here. 252 // safe here.
181 const int kMaxPeekCount = 20; 253 const int kMaxPeekCount = 20;
182 int peek_count; 254 int peek_count;
183 for (peek_count = 0; peek_count < kMaxPeekCount; ++peek_count) { 255 for (peek_count = 0; peek_count < kMaxPeekCount; ++peek_count) {
184 MSG msg; 256 MSG msg;
185 if (!PeekMessage(&msg, NULL, 0, 0, PM_REMOVE | PM_QS_PAINT)) 257 if (!PeekMessageInternal(&msg, NULL, 0, 0, PM_REMOVE | PM_QS_PAINT))
186 break; 258 break;
187 ProcessMessageHelper(msg); 259 ProcessMessageHelper(msg);
188 if (state_->should_quit) // Handle WM_QUIT. 260 if (state_->should_quit) // Handle WM_QUIT.
189 break; 261 break;
190 } 262 }
191 // Histogram what was really being used, to help to adjust kMaxPeekCount. 263 // Histogram what was really being used, to help to adjust kMaxPeekCount.
192 DHISTOGRAM_COUNTS("Loop.PumpOutPendingPaintMessages Peeks", peek_count); 264 DHISTOGRAM_COUNTS("Loop.PumpOutPendingPaintMessages Peeks", peek_count);
193 } 265 }
194 266
195 //----------------------------------------------------------------------------- 267 //-----------------------------------------------------------------------------
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
297 // This causes the MsgWaitForMultipleObjectsEx API to return indicating 369 // This causes the MsgWaitForMultipleObjectsEx API to return indicating
298 // that messages are ready for processing (specifically mouse messages 370 // that messages are ready for processing (specifically mouse messages
299 // intended for the child window. Occurs if the child window has capture) 371 // intended for the child window. Occurs if the child window has capture)
300 // The subsequent PeekMessages call fails to return any messages thus 372 // The subsequent PeekMessages call fails to return any messages thus
301 // causing us to enter a tight loop at times. 373 // causing us to enter a tight loop at times.
302 // The WaitMessage call below is a workaround to give the child window 374 // The WaitMessage call below is a workaround to give the child window
303 // sometime to process its input messages. 375 // sometime to process its input messages.
304 MSG msg = {0}; 376 MSG msg = {0};
305 DWORD queue_status = GetQueueStatus(QS_MOUSE); 377 DWORD queue_status = GetQueueStatus(QS_MOUSE);
306 if (HIWORD(queue_status) & QS_MOUSE && 378 if (HIWORD(queue_status) & QS_MOUSE &&
307 !PeekMessage(&msg, NULL, WM_MOUSEFIRST, WM_MOUSELAST, PM_NOREMOVE)) { 379 !PeekMessageInternal(&msg, NULL, WM_MOUSEFIRST, WM_MOUSELAST,
380 PM_NOREMOVE)) {
308 WaitMessage(); 381 WaitMessage();
309 } 382 }
310 return; 383 return;
311 } 384 }
312 385
313 DCHECK_NE(WAIT_FAILED, result) << GetLastError(); 386 DCHECK_NE(WAIT_FAILED, result) << GetLastError();
314 } 387 }
315 388
316 void MessagePumpForUI::HandleWorkMessage() { 389 void MessagePumpForUI::HandleWorkMessage() {
317 // If we are being called outside of the context of Run, then don't try to do 390 // 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 427 // If there are sent messages in the queue then PeekMessage internally
355 // dispatches the message and returns false. We return true in this 428 // dispatches the message and returns false. We return true in this
356 // case to ensure that the message loop peeks again instead of calling 429 // case to ensure that the message loop peeks again instead of calling
357 // MsgWaitForMultipleObjectsEx again. 430 // MsgWaitForMultipleObjectsEx again.
358 bool sent_messages_in_queue = false; 431 bool sent_messages_in_queue = false;
359 DWORD queue_status = GetQueueStatus(QS_SENDMESSAGE); 432 DWORD queue_status = GetQueueStatus(QS_SENDMESSAGE);
360 if (HIWORD(queue_status) & QS_SENDMESSAGE) 433 if (HIWORD(queue_status) & QS_SENDMESSAGE)
361 sent_messages_in_queue = true; 434 sent_messages_in_queue = true;
362 435
363 MSG msg; 436 MSG msg;
364 if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) 437 if (PeekMessageInternal(&msg, NULL, 0, 0, PM_REMOVE))
365 return ProcessMessageHelper(msg); 438 return ProcessMessageHelper(msg);
366 439
367 return sent_messages_in_queue; 440 return sent_messages_in_queue;
368 } 441 }
369 442
370 bool MessagePumpForUI::ProcessMessageHelper(const MSG& msg) { 443 bool MessagePumpForUI::ProcessMessageHelper(const MSG& msg) {
371 TRACE_EVENT1("base", "MessagePumpForUI::ProcessMessageHelper", 444 TRACE_EVENT1("base", "MessagePumpForUI::ProcessMessageHelper",
372 "message", msg.message); 445 "message", msg.message);
373 if (WM_QUIT == msg.message) { 446 if (WM_QUIT == msg.message) {
374 // Repost the QUIT message so that it will be retrieved by the primary 447 // Repost the QUIT message so that it will be retrieved by the primary
375 // GetMessage() loop. 448 // GetMessage() loop.
376 state_->should_quit = true; 449 state_->should_quit = true;
377 PostQuitMessage(static_cast<int>(msg.wParam)); 450 PostQuitMessage(static_cast<int>(msg.wParam));
378 return false; 451 return false;
379 } 452 }
380 453
381 // While running our main message pump, we discard kMsgHaveWork messages. 454 // While running our main message pump, we discard kMsgHaveWork messages.
382 if (msg.message == kMsgHaveWork && msg.hwnd == message_hwnd_) 455 if (msg.message == kMsgHaveWork && msg.hwnd == message_hwnd_)
383 return ProcessPumpReplacementMessage(); 456 return ProcessPumpReplacementMessage();
384 457
385 if (CallMsgFilter(const_cast<MSG*>(&msg), kMessageFilterCode)) 458 if (CallMsgFilter(const_cast<MSG*>(&msg), kMessageFilterCode))
386 return true; 459 return true;
387 460
388 WillProcessMessage(msg); 461 WillProcessMessage(msg);
389 462
390 if (state_->dispatcher) { 463 bool is_key_dispatched_by_tsf = false;
391 if (!state_->dispatcher->Dispatch(msg)) 464 if (base::win::IsTsfAwareRequired()) {
392 state_->should_quit = true; 465 is_key_dispatched_by_tsf = tsf_message_pump_->ForwardKeyMessageForTSF(msg);
393 } else { 466 }
394 TranslateMessage(&msg); 467
395 DispatchMessage(&msg); 468 if (!is_key_dispatched_by_tsf) {
469 if (state_->dispatcher) {
470 if (!state_->dispatcher->Dispatch(msg))
471 state_->should_quit = true;
472 } else {
473 TranslateMessage(&msg);
474 DispatchMessage(&msg);
475 }
396 } 476 }
397 477
398 DidProcessMessage(msg); 478 DidProcessMessage(msg);
399 return true; 479 return true;
400 } 480 }
401 481
402 bool MessagePumpForUI::ProcessPumpReplacementMessage() { 482 bool MessagePumpForUI::ProcessPumpReplacementMessage() {
403 // When we encounter a kMsgHaveWork message, this method is called to peek 483 // 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 484 // 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 485 // 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 486 // 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, 487 // 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 488 // then resets the have_work_ flag (allowing a replacement kMsgHaveWork to
409 // possibly be posted), and finally dispatches that peeked replacement. Note 489 // possibly be posted), and finally dispatches that peeked replacement. Note
410 // that the re-post of kMsgHaveWork may be asynchronous to this thread!! 490 // that the re-post of kMsgHaveWork may be asynchronous to this thread!!
411 491
412 bool have_message = false; 492 bool have_message = false;
413 MSG msg; 493 MSG msg;
414 // We should not process all window messages if we are in the context of an 494 // 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. 495 // 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. 496 // This is to ensure that these messages are peeked out by the OS modal loop.
417 if (MessageLoop::current()->os_modal_loop()) { 497 if (MessageLoop::current()->os_modal_loop()) {
418 // We only peek out WM_PAINT and WM_TIMER here for reasons mentioned above. 498 // 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) || 499 have_message =
420 PeekMessage(&msg, NULL, WM_TIMER, WM_TIMER, PM_REMOVE); 500 PeekMessageInternal(&msg, NULL, WM_PAINT, WM_PAINT, PM_REMOVE) ||
501 PeekMessageInternal(&msg, NULL, WM_TIMER, WM_TIMER, PM_REMOVE);
421 } else { 502 } else {
422 have_message = (0 != PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)); 503 have_message = PeekMessageInternal(&msg, NULL, 0, 0, PM_REMOVE);
423 } 504 }
424 505
425 DCHECK(!have_message || kMsgHaveWork != msg.message || 506 DCHECK(!have_message || kMsgHaveWork != msg.message ||
426 msg.hwnd != message_hwnd_); 507 msg.hwnd != message_hwnd_);
427 508
428 // Since we discarded a kMsgHaveWork message, we must update the flag. 509 // Since we discarded a kMsgHaveWork message, we must update the flag.
429 int old_have_work = InterlockedExchange(&have_work_, 0); 510 int old_have_work = InterlockedExchange(&have_work_, 0);
430 DCHECK(old_have_work); 511 DCHECK(old_have_work);
431 512
432 // We don't need a special time slice if we didn't have_message to process. 513 // We don't need a special time slice if we didn't have_message to process.
433 if (!have_message) 514 if (!have_message)
434 return false; 515 return false;
435 516
436 // Guarantee we'll get another time slice in the case where we go into native 517 // 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 518 // windows code. This ScheduleWork() may hurt performance a tiny bit when
438 // tasks appear very infrequently, but when the event queue is busy, the 519 // tasks appear very infrequently, but when the event queue is busy, the
439 // kMsgHaveWork events get (percentage wise) rarer and rarer. 520 // kMsgHaveWork events get (percentage wise) rarer and rarer.
440 ScheduleWork(); 521 ScheduleWork();
441 return ProcessMessageHelper(msg); 522 return ProcessMessageHelper(msg);
442 } 523 }
443 524
525 bool MessagePumpForUI::PeekMessageInternal(
526 MSG* msg, HWND hwnd, UINT wmin, UINT wmax, UINT wmsg) {
527 if (base::win::IsTsfAwareRequired())
528 return tsf_message_pump_->PeekMessageForTSF(msg, hwnd, wmin, wmax, wmsg);
529
530 return !!::PeekMessage(msg, hwnd, wmin, wmax, wmsg);
531 }
532
444 //----------------------------------------------------------------------------- 533 //-----------------------------------------------------------------------------
445 // MessagePumpForIO public: 534 // MessagePumpForIO public:
446 535
447 MessagePumpForIO::MessagePumpForIO() { 536 MessagePumpForIO::MessagePumpForIO() {
448 port_.Set(CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, NULL, 1)); 537 port_.Set(CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, NULL, 1));
449 DCHECK(port_.IsValid()); 538 DCHECK(port_.IsValid());
450 } 539 }
451 540
452 void MessagePumpForIO::ScheduleWork() { 541 void MessagePumpForIO::ScheduleWork() {
453 if (InterlockedExchange(&have_work_, 1)) 542 if (InterlockedExchange(&have_work_, 1))
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after
617 706
618 void MessagePumpForIO::WillProcessIOEvent() { 707 void MessagePumpForIO::WillProcessIOEvent() {
619 FOR_EACH_OBSERVER(IOObserver, io_observers_, WillProcessIOEvent()); 708 FOR_EACH_OBSERVER(IOObserver, io_observers_, WillProcessIOEvent());
620 } 709 }
621 710
622 void MessagePumpForIO::DidProcessIOEvent() { 711 void MessagePumpForIO::DidProcessIOEvent() {
623 FOR_EACH_OBSERVER(IOObserver, io_observers_, DidProcessIOEvent()); 712 FOR_EACH_OBSERVER(IOObserver, io_observers_, DidProcessIOEvent());
624 } 713 }
625 714
626 } // namespace base 715 } // namespace base
OLDNEW
« base/message_pump_win.h ('K') | « base/message_pump_win.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698