Chromium Code Reviews| 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_loop/message_pump_win.h" | 5 #include "base/message_loop/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/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 90 if (delay < 0) | 90 if (delay < 0) |
| 91 delay = 0; | 91 delay = 0; |
| 92 | 92 |
| 93 return delay; | 93 return delay; |
| 94 } | 94 } |
| 95 | 95 |
| 96 //----------------------------------------------------------------------------- | 96 //----------------------------------------------------------------------------- |
| 97 // MessagePumpForUI public: | 97 // MessagePumpForUI public: |
| 98 | 98 |
| 99 MessagePumpForUI::MessagePumpForUI() | 99 MessagePumpForUI::MessagePumpForUI() |
| 100 : atom_(0), | 100 : atom_(0) { |
| 101 message_filter_(new MessageFilter) { | |
| 102 InitMessageWnd(); | 101 InitMessageWnd(); |
| 103 } | 102 } |
| 104 | 103 |
| 105 MessagePumpForUI::~MessagePumpForUI() { | 104 MessagePumpForUI::~MessagePumpForUI() { |
| 106 DestroyWindow(message_hwnd_); | 105 DestroyWindow(message_hwnd_); |
| 107 UnregisterClass(MAKEINTATOM(atom_), | 106 UnregisterClass(MAKEINTATOM(atom_), |
| 108 GetModuleFromAddress(&WndProcThunk)); | 107 GetModuleFromAddress(&WndProcThunk)); |
| 109 } | 108 } |
| 110 | 109 |
| 111 void MessagePumpForUI::ScheduleWork() { | 110 void MessagePumpForUI::ScheduleWork() { |
| (...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 339 // If there are sent messages in the queue then PeekMessage internally | 338 // If there are sent messages in the queue then PeekMessage internally |
| 340 // dispatches the message and returns false. We return true in this | 339 // dispatches the message and returns false. We return true in this |
| 341 // case to ensure that the message loop peeks again instead of calling | 340 // case to ensure that the message loop peeks again instead of calling |
| 342 // MsgWaitForMultipleObjectsEx again. | 341 // MsgWaitForMultipleObjectsEx again. |
| 343 bool sent_messages_in_queue = false; | 342 bool sent_messages_in_queue = false; |
| 344 DWORD queue_status = GetQueueStatus(QS_SENDMESSAGE); | 343 DWORD queue_status = GetQueueStatus(QS_SENDMESSAGE); |
| 345 if (HIWORD(queue_status) & QS_SENDMESSAGE) | 344 if (HIWORD(queue_status) & QS_SENDMESSAGE) |
| 346 sent_messages_in_queue = true; | 345 sent_messages_in_queue = true; |
| 347 | 346 |
| 348 MSG msg; | 347 MSG msg; |
| 349 if (message_filter_->DoPeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) | 348 if (!!PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) |
|
Nico
2014/02/08 20:10:46
Do you need the `!!`? Is just `if (PeekMessage(…`
yukawa
2014/02/09 08:09:08
Year, that is not necessary in C but I believe it
Nico
2014/02/09 20:36:09
I think we build with C4800 enabled. I think `!= 0
yukawa
2014/02/10 06:50:46
OK. Switched to ` != FALSE`.
| |
| 350 return ProcessMessageHelper(msg); | 349 return ProcessMessageHelper(msg); |
| 351 | 350 |
| 352 return sent_messages_in_queue; | 351 return sent_messages_in_queue; |
| 353 } | 352 } |
| 354 | 353 |
| 355 bool MessagePumpForUI::ProcessMessageHelper(const MSG& msg) { | 354 bool MessagePumpForUI::ProcessMessageHelper(const MSG& msg) { |
| 356 TRACE_EVENT1("base", "MessagePumpForUI::ProcessMessageHelper", | 355 TRACE_EVENT1("base", "MessagePumpForUI::ProcessMessageHelper", |
| 357 "message", msg.message); | 356 "message", msg.message); |
| 358 if (WM_QUIT == msg.message) { | 357 if (WM_QUIT == msg.message) { |
| 359 // Repost the QUIT message so that it will be retrieved by the primary | 358 // Repost the QUIT message so that it will be retrieved by the primary |
| 360 // GetMessage() loop. | 359 // GetMessage() loop. |
| 361 state_->should_quit = true; | 360 state_->should_quit = true; |
| 362 PostQuitMessage(static_cast<int>(msg.wParam)); | 361 PostQuitMessage(static_cast<int>(msg.wParam)); |
| 363 return false; | 362 return false; |
| 364 } | 363 } |
| 365 | 364 |
| 366 // While running our main message pump, we discard kMsgHaveWork messages. | 365 // While running our main message pump, we discard kMsgHaveWork messages. |
| 367 if (msg.message == kMsgHaveWork && msg.hwnd == message_hwnd_) | 366 if (msg.message == kMsgHaveWork && msg.hwnd == message_hwnd_) |
| 368 return ProcessPumpReplacementMessage(); | 367 return ProcessPumpReplacementMessage(); |
| 369 | 368 |
| 370 if (CallMsgFilter(const_cast<MSG*>(&msg), kMessageFilterCode)) | 369 if (CallMsgFilter(const_cast<MSG*>(&msg), kMessageFilterCode)) |
| 371 return true; | 370 return true; |
| 372 | 371 |
| 373 WillProcessMessage(msg); | 372 WillProcessMessage(msg); |
| 374 | 373 |
| 375 if (!message_filter_->ProcessMessage(msg)) { | 374 if (state_->dispatcher) { |
| 376 if (state_->dispatcher) { | 375 if (!state_->dispatcher->Dispatch(msg)) |
| 377 if (!state_->dispatcher->Dispatch(msg)) | 376 state_->should_quit = true; |
| 378 state_->should_quit = true; | 377 } else { |
| 379 } else { | 378 TranslateMessage(&msg); |
| 380 TranslateMessage(&msg); | 379 DispatchMessage(&msg); |
| 381 DispatchMessage(&msg); | |
| 382 } | |
| 383 } | 380 } |
| 384 | 381 |
| 385 DidProcessMessage(msg); | 382 DidProcessMessage(msg); |
| 386 return true; | 383 return true; |
| 387 } | 384 } |
| 388 | 385 |
| 389 bool MessagePumpForUI::ProcessPumpReplacementMessage() { | 386 bool MessagePumpForUI::ProcessPumpReplacementMessage() { |
| 390 // When we encounter a kMsgHaveWork message, this method is called to peek | 387 // When we encounter a kMsgHaveWork message, this method is called to peek |
| 391 // and process a replacement message, such as a WM_PAINT or WM_TIMER. The | 388 // and process a replacement message, such as a WM_PAINT or WM_TIMER. The |
| 392 // goal is to make the kMsgHaveWork as non-intrusive as possible, even though | 389 // goal is to make the kMsgHaveWork as non-intrusive as possible, even though |
| 393 // a continuous stream of such messages are posted. This method carefully | 390 // a continuous stream of such messages are posted. This method carefully |
| 394 // peeks a message while there is no chance for a kMsgHaveWork to be pending, | 391 // peeks a message while there is no chance for a kMsgHaveWork to be pending, |
| 395 // then resets the have_work_ flag (allowing a replacement kMsgHaveWork to | 392 // then resets the have_work_ flag (allowing a replacement kMsgHaveWork to |
| 396 // possibly be posted), and finally dispatches that peeked replacement. Note | 393 // possibly be posted), and finally dispatches that peeked replacement. Note |
| 397 // that the re-post of kMsgHaveWork may be asynchronous to this thread!! | 394 // that the re-post of kMsgHaveWork may be asynchronous to this thread!! |
| 398 | 395 |
| 399 bool have_message = false; | 396 bool have_message = false; |
| 400 MSG msg; | 397 MSG msg; |
| 401 // We should not process all window messages if we are in the context of an | 398 // We should not process all window messages if we are in the context of an |
| 402 // OS modal loop, i.e. in the context of a windows API call like MessageBox. | 399 // OS modal loop, i.e. in the context of a windows API call like MessageBox. |
| 403 // This is to ensure that these messages are peeked out by the OS modal loop. | 400 // This is to ensure that these messages are peeked out by the OS modal loop. |
| 404 if (MessageLoop::current()->os_modal_loop()) { | 401 if (MessageLoop::current()->os_modal_loop()) { |
| 405 // We only peek out WM_PAINT and WM_TIMER here for reasons mentioned above. | 402 // We only peek out WM_PAINT and WM_TIMER here for reasons mentioned above. |
| 406 have_message = PeekMessage(&msg, NULL, WM_PAINT, WM_PAINT, PM_REMOVE) || | 403 have_message = PeekMessage(&msg, NULL, WM_PAINT, WM_PAINT, PM_REMOVE) || |
| 407 PeekMessage(&msg, NULL, WM_TIMER, WM_TIMER, PM_REMOVE); | 404 PeekMessage(&msg, NULL, WM_TIMER, WM_TIMER, PM_REMOVE); |
| 408 } else { | 405 } else { |
| 409 have_message = !!message_filter_->DoPeekMessage(&msg, NULL, 0, 0, | 406 have_message = !!PeekMessage(&msg, NULL, 0, 0, PM_REMOVE); |
| 410 PM_REMOVE); | |
| 411 } | 407 } |
| 412 | 408 |
| 413 DCHECK(!have_message || kMsgHaveWork != msg.message || | 409 DCHECK(!have_message || kMsgHaveWork != msg.message || |
| 414 msg.hwnd != message_hwnd_); | 410 msg.hwnd != message_hwnd_); |
| 415 | 411 |
| 416 // Since we discarded a kMsgHaveWork message, we must update the flag. | 412 // Since we discarded a kMsgHaveWork message, we must update the flag. |
| 417 int old_have_work = InterlockedExchange(&have_work_, 0); | 413 int old_have_work = InterlockedExchange(&have_work_, 0); |
| 418 DCHECK(old_have_work); | 414 DCHECK(old_have_work); |
| 419 | 415 |
| 420 // We don't need a special time slice if we didn't have_message to process. | 416 // We don't need a special time slice if we didn't have_message to process. |
| 421 if (!have_message) | 417 if (!have_message) |
| 422 return false; | 418 return false; |
| 423 | 419 |
| 424 // Guarantee we'll get another time slice in the case where we go into native | 420 // Guarantee we'll get another time slice in the case where we go into native |
| 425 // windows code. This ScheduleWork() may hurt performance a tiny bit when | 421 // windows code. This ScheduleWork() may hurt performance a tiny bit when |
| 426 // tasks appear very infrequently, but when the event queue is busy, the | 422 // tasks appear very infrequently, but when the event queue is busy, the |
| 427 // kMsgHaveWork events get (percentage wise) rarer and rarer. | 423 // kMsgHaveWork events get (percentage wise) rarer and rarer. |
| 428 ScheduleWork(); | 424 ScheduleWork(); |
| 429 return ProcessMessageHelper(msg); | 425 return ProcessMessageHelper(msg); |
| 430 } | 426 } |
| 431 | 427 |
| 432 void MessagePumpForUI::SetMessageFilter( | |
| 433 scoped_ptr<MessageFilter> message_filter) { | |
| 434 message_filter_ = message_filter.Pass(); | |
| 435 } | |
| 436 | |
| 437 //----------------------------------------------------------------------------- | 428 //----------------------------------------------------------------------------- |
| 438 // MessagePumpForIO public: | 429 // MessagePumpForIO public: |
| 439 | 430 |
| 440 MessagePumpForIO::MessagePumpForIO() { | 431 MessagePumpForIO::MessagePumpForIO() { |
| 441 port_.Set(CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, NULL, 1)); | 432 port_.Set(CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, NULL, 1)); |
| 442 DCHECK(port_.IsValid()); | 433 DCHECK(port_.IsValid()); |
| 443 } | 434 } |
| 444 | 435 |
| 445 void MessagePumpForIO::ScheduleWork() { | 436 void MessagePumpForIO::ScheduleWork() { |
| 446 if (InterlockedExchange(&have_work_, 1)) | 437 if (InterlockedExchange(&have_work_, 1)) |
| (...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 653 | 644 |
| 654 // static | 645 // static |
| 655 MessagePumpForIO::IOHandler* MessagePumpForIO::KeyToHandler( | 646 MessagePumpForIO::IOHandler* MessagePumpForIO::KeyToHandler( |
| 656 ULONG_PTR key, | 647 ULONG_PTR key, |
| 657 bool* has_valid_io_context) { | 648 bool* has_valid_io_context) { |
| 658 *has_valid_io_context = ((key & 1) == 0); | 649 *has_valid_io_context = ((key & 1) == 0); |
| 659 return reinterpret_cast<IOHandler*>(key & ~static_cast<ULONG_PTR>(1)); | 650 return reinterpret_cast<IOHandler*>(key & ~static_cast<ULONG_PTR>(1)); |
| 660 } | 651 } |
| 661 | 652 |
| 662 } // namespace base | 653 } // namespace base |
| OLD | NEW |