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) != 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 uint32_t action = MessagePumpDispatcher::POST_DISPATCH_PERFORM_DEFAULT; |
376 uint32_t action = MessagePumpDispatcher::POST_DISPATCH_PERFORM_DEFAULT; | 375 if (state_->dispatcher) |
377 if (state_->dispatcher) | 376 action = state_->dispatcher->Dispatch(msg); |
378 action = state_->dispatcher->Dispatch(msg); | 377 if (action & MessagePumpDispatcher::POST_DISPATCH_QUIT_LOOP) |
379 if (action & MessagePumpDispatcher::POST_DISPATCH_QUIT_LOOP) | 378 state_->should_quit = true; |
380 state_->should_quit = true; | 379 if (action & MessagePumpDispatcher::POST_DISPATCH_PERFORM_DEFAULT) { |
381 if (action & MessagePumpDispatcher::POST_DISPATCH_PERFORM_DEFAULT) { | 380 TranslateMessage(&msg); |
382 TranslateMessage(&msg); | 381 DispatchMessage(&msg); |
383 DispatchMessage(&msg); | |
384 } | |
385 } | 382 } |
386 | 383 |
387 DidProcessMessage(msg); | 384 DidProcessMessage(msg); |
388 return true; | 385 return true; |
389 } | 386 } |
390 | 387 |
391 bool MessagePumpForUI::ProcessPumpReplacementMessage() { | 388 bool MessagePumpForUI::ProcessPumpReplacementMessage() { |
392 // When we encounter a kMsgHaveWork message, this method is called to peek | 389 // When we encounter a kMsgHaveWork message, this method is called to peek |
393 // and process a replacement message, such as a WM_PAINT or WM_TIMER. The | 390 // and process a replacement message, such as a WM_PAINT or WM_TIMER. The |
394 // goal is to make the kMsgHaveWork as non-intrusive as possible, even though | 391 // goal is to make the kMsgHaveWork as non-intrusive as possible, even though |
395 // a continuous stream of such messages are posted. This method carefully | 392 // a continuous stream of such messages are posted. This method carefully |
396 // peeks a message while there is no chance for a kMsgHaveWork to be pending, | 393 // peeks a message while there is no chance for a kMsgHaveWork to be pending, |
397 // then resets the have_work_ flag (allowing a replacement kMsgHaveWork to | 394 // then resets the have_work_ flag (allowing a replacement kMsgHaveWork to |
398 // possibly be posted), and finally dispatches that peeked replacement. Note | 395 // possibly be posted), and finally dispatches that peeked replacement. Note |
399 // that the re-post of kMsgHaveWork may be asynchronous to this thread!! | 396 // that the re-post of kMsgHaveWork may be asynchronous to this thread!! |
400 | 397 |
401 bool have_message = false; | 398 bool have_message = false; |
402 MSG msg; | 399 MSG msg; |
403 // We should not process all window messages if we are in the context of an | 400 // We should not process all window messages if we are in the context of an |
404 // OS modal loop, i.e. in the context of a windows API call like MessageBox. | 401 // OS modal loop, i.e. in the context of a windows API call like MessageBox. |
405 // This is to ensure that these messages are peeked out by the OS modal loop. | 402 // This is to ensure that these messages are peeked out by the OS modal loop. |
406 if (MessageLoop::current()->os_modal_loop()) { | 403 if (MessageLoop::current()->os_modal_loop()) { |
407 // We only peek out WM_PAINT and WM_TIMER here for reasons mentioned above. | 404 // We only peek out WM_PAINT and WM_TIMER here for reasons mentioned above. |
408 have_message = PeekMessage(&msg, NULL, WM_PAINT, WM_PAINT, PM_REMOVE) || | 405 have_message = PeekMessage(&msg, NULL, WM_PAINT, WM_PAINT, PM_REMOVE) || |
409 PeekMessage(&msg, NULL, WM_TIMER, WM_TIMER, PM_REMOVE); | 406 PeekMessage(&msg, NULL, WM_TIMER, WM_TIMER, PM_REMOVE); |
410 } else { | 407 } else { |
411 have_message = !!message_filter_->DoPeekMessage(&msg, NULL, 0, 0, | 408 have_message = PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) != FALSE; |
412 PM_REMOVE); | |
413 } | 409 } |
414 | 410 |
415 DCHECK(!have_message || kMsgHaveWork != msg.message || | 411 DCHECK(!have_message || kMsgHaveWork != msg.message || |
416 msg.hwnd != message_hwnd_); | 412 msg.hwnd != message_hwnd_); |
417 | 413 |
418 // Since we discarded a kMsgHaveWork message, we must update the flag. | 414 // Since we discarded a kMsgHaveWork message, we must update the flag. |
419 int old_have_work = InterlockedExchange(&have_work_, 0); | 415 int old_have_work = InterlockedExchange(&have_work_, 0); |
420 DCHECK(old_have_work); | 416 DCHECK(old_have_work); |
421 | 417 |
422 // We don't need a special time slice if we didn't have_message to process. | 418 // We don't need a special time slice if we didn't have_message to process. |
423 if (!have_message) | 419 if (!have_message) |
424 return false; | 420 return false; |
425 | 421 |
426 // Guarantee we'll get another time slice in the case where we go into native | 422 // Guarantee we'll get another time slice in the case where we go into native |
427 // windows code. This ScheduleWork() may hurt performance a tiny bit when | 423 // windows code. This ScheduleWork() may hurt performance a tiny bit when |
428 // tasks appear very infrequently, but when the event queue is busy, the | 424 // tasks appear very infrequently, but when the event queue is busy, the |
429 // kMsgHaveWork events get (percentage wise) rarer and rarer. | 425 // kMsgHaveWork events get (percentage wise) rarer and rarer. |
430 ScheduleWork(); | 426 ScheduleWork(); |
431 return ProcessMessageHelper(msg); | 427 return ProcessMessageHelper(msg); |
432 } | 428 } |
433 | 429 |
434 void MessagePumpForUI::SetMessageFilter( | |
435 scoped_ptr<MessageFilter> message_filter) { | |
436 message_filter_ = message_filter.Pass(); | |
437 } | |
438 | |
439 //----------------------------------------------------------------------------- | 430 //----------------------------------------------------------------------------- |
440 // MessagePumpForIO public: | 431 // MessagePumpForIO public: |
441 | 432 |
442 MessagePumpForIO::MessagePumpForIO() { | 433 MessagePumpForIO::MessagePumpForIO() { |
443 port_.Set(CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, NULL, 1)); | 434 port_.Set(CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, NULL, 1)); |
444 DCHECK(port_.IsValid()); | 435 DCHECK(port_.IsValid()); |
445 } | 436 } |
446 | 437 |
447 void MessagePumpForIO::ScheduleWork() { | 438 void MessagePumpForIO::ScheduleWork() { |
448 if (InterlockedExchange(&have_work_, 1)) | 439 if (InterlockedExchange(&have_work_, 1)) |
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
655 | 646 |
656 // static | 647 // static |
657 MessagePumpForIO::IOHandler* MessagePumpForIO::KeyToHandler( | 648 MessagePumpForIO::IOHandler* MessagePumpForIO::KeyToHandler( |
658 ULONG_PTR key, | 649 ULONG_PTR key, |
659 bool* has_valid_io_context) { | 650 bool* has_valid_io_context) { |
660 *has_valid_io_context = ((key & 1) == 0); | 651 *has_valid_io_context = ((key & 1) == 0); |
661 return reinterpret_cast<IOHandler*>(key & ~static_cast<ULONG_PTR>(1)); | 652 return reinterpret_cast<IOHandler*>(key & ~static_cast<ULONG_PTR>(1)); |
662 } | 653 } |
663 | 654 |
664 } // namespace base | 655 } // namespace base |
OLD | NEW |