| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
| 10 | 10 |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 //----------------------------------------------------------------------------- | 59 //----------------------------------------------------------------------------- |
| 60 // MessagePumpWin protected: | 60 // MessagePumpWin protected: |
| 61 | 61 |
| 62 int MessagePumpWin::GetCurrentDelay() const { | 62 int MessagePumpWin::GetCurrentDelay() const { |
| 63 if (delayed_work_time_.is_null()) | 63 if (delayed_work_time_.is_null()) |
| 64 return -1; | 64 return -1; |
| 65 | 65 |
| 66 // Be careful here. TimeDelta has a precision of microseconds, but we want a | 66 // Be careful here. TimeDelta has a precision of microseconds, but we want a |
| 67 // value in milliseconds. If there are 5.5ms left, should the delay be 5 or | 67 // value in milliseconds. If there are 5.5ms left, should the delay be 5 or |
| 68 // 6? It should be 6 to avoid executing delayed work too early. | 68 // 6? It should be 6 to avoid executing delayed work too early. |
| 69 double timeout = ceil((delayed_work_time_ - Time::Now()).InMillisecondsF()); | 69 double timeout = |
| 70 ceil((delayed_work_time_ - TimeTicks::Now()).InMillisecondsF()); |
| 70 | 71 |
| 71 // If this value is negative, then we need to run delayed work soon. | 72 // If this value is negative, then we need to run delayed work soon. |
| 72 int delay = static_cast<int>(timeout); | 73 int delay = static_cast<int>(timeout); |
| 73 if (delay < 0) | 74 if (delay < 0) |
| 74 delay = 0; | 75 delay = 0; |
| 75 | 76 |
| 76 return delay; | 77 return delay; |
| 77 } | 78 } |
| 78 | 79 |
| 79 //----------------------------------------------------------------------------- | 80 //----------------------------------------------------------------------------- |
| 80 // MessagePumpForUI public: | 81 // MessagePumpForUI public: |
| 81 | 82 |
| 82 MessagePumpForUI::MessagePumpForUI() { | 83 MessagePumpForUI::MessagePumpForUI() { |
| 83 InitMessageWnd(); | 84 InitMessageWnd(); |
| 84 } | 85 } |
| 85 | 86 |
| 86 MessagePumpForUI::~MessagePumpForUI() { | 87 MessagePumpForUI::~MessagePumpForUI() { |
| 87 DestroyWindow(message_hwnd_); | 88 DestroyWindow(message_hwnd_); |
| 88 UnregisterClass(kWndClass, GetModuleHandle(NULL)); | 89 UnregisterClass(kWndClass, GetModuleHandle(NULL)); |
| 89 } | 90 } |
| 90 | 91 |
| 91 void MessagePumpForUI::ScheduleWork() { | 92 void MessagePumpForUI::ScheduleWork() { |
| 92 if (InterlockedExchange(&have_work_, 1)) | 93 if (InterlockedExchange(&have_work_, 1)) |
| 93 return; // Someone else continued the pumping. | 94 return; // Someone else continued the pumping. |
| 94 | 95 |
| 95 // Make sure the MessagePump does some work for us. | 96 // Make sure the MessagePump does some work for us. |
| 96 PostMessage(message_hwnd_, kMsgHaveWork, reinterpret_cast<WPARAM>(this), 0); | 97 PostMessage(message_hwnd_, kMsgHaveWork, reinterpret_cast<WPARAM>(this), 0); |
| 97 } | 98 } |
| 98 | 99 |
| 99 void MessagePumpForUI::ScheduleDelayedWork(const Time& delayed_work_time) { | 100 void MessagePumpForUI::ScheduleDelayedWork(const TimeTicks& delayed_work_time) { |
| 100 // | 101 // |
| 101 // We would *like* to provide high resolution timers. Windows timers using | 102 // We would *like* to provide high resolution timers. Windows timers using |
| 102 // SetTimer() have a 10ms granularity. We have to use WM_TIMER as a wakeup | 103 // SetTimer() have a 10ms granularity. We have to use WM_TIMER as a wakeup |
| 103 // mechanism because the application can enter modal windows loops where it | 104 // mechanism because the application can enter modal windows loops where it |
| 104 // is not running our MessageLoop; the only way to have our timers fire in | 105 // is not running our MessageLoop; the only way to have our timers fire in |
| 105 // these cases is to post messages there. | 106 // these cases is to post messages there. |
| 106 // | 107 // |
| 107 // To provide sub-10ms timers, we process timers directly from our run loop. | 108 // To provide sub-10ms timers, we process timers directly from our run loop. |
| 108 // For the common case, timers will be processed there as the run loop does | 109 // For the common case, timers will be processed there as the run loop does |
| 109 // its normal work. However, we *also* set the system timer so that WM_TIMER | 110 // its normal work. However, we *also* set the system timer so that WM_TIMER |
| (...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 402 if (InterlockedExchange(&have_work_, 1)) | 403 if (InterlockedExchange(&have_work_, 1)) |
| 403 return; // Someone else continued the pumping. | 404 return; // Someone else continued the pumping. |
| 404 | 405 |
| 405 // Make sure the MessagePump does some work for us. | 406 // Make sure the MessagePump does some work for us. |
| 406 BOOL ret = PostQueuedCompletionStatus(port_, 0, | 407 BOOL ret = PostQueuedCompletionStatus(port_, 0, |
| 407 reinterpret_cast<ULONG_PTR>(this), | 408 reinterpret_cast<ULONG_PTR>(this), |
| 408 reinterpret_cast<OVERLAPPED*>(this)); | 409 reinterpret_cast<OVERLAPPED*>(this)); |
| 409 DCHECK(ret); | 410 DCHECK(ret); |
| 410 } | 411 } |
| 411 | 412 |
| 412 void MessagePumpForIO::ScheduleDelayedWork(const Time& delayed_work_time) { | 413 void MessagePumpForIO::ScheduleDelayedWork(const TimeTicks& delayed_work_time) { |
| 413 // We know that we can't be blocked right now since this method can only be | 414 // We know that we can't be blocked right now since this method can only be |
| 414 // called on the same thread as Run, so we only need to update our record of | 415 // called on the same thread as Run, so we only need to update our record of |
| 415 // how long to sleep when we do sleep. | 416 // how long to sleep when we do sleep. |
| 416 delayed_work_time_ = delayed_work_time; | 417 delayed_work_time_ = delayed_work_time; |
| 417 } | 418 } |
| 418 | 419 |
| 419 void MessagePumpForIO::RegisterIOHandler(HANDLE file_handle, | 420 void MessagePumpForIO::RegisterIOHandler(HANDLE file_handle, |
| 420 IOHandler* handler) { | 421 IOHandler* handler) { |
| 421 ULONG_PTR key = reinterpret_cast<ULONG_PTR>(handler); | 422 ULONG_PTR key = reinterpret_cast<ULONG_PTR>(handler); |
| 422 HANDLE port = CreateIoCompletionPort(file_handle, port_, key, 1); | 423 HANDLE port = CreateIoCompletionPort(file_handle, port_, key, 1); |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 560 | 561 |
| 561 void MessagePumpForIO::WillProcessIOEvent() { | 562 void MessagePumpForIO::WillProcessIOEvent() { |
| 562 FOR_EACH_OBSERVER(IOObserver, io_observers_, WillProcessIOEvent()); | 563 FOR_EACH_OBSERVER(IOObserver, io_observers_, WillProcessIOEvent()); |
| 563 } | 564 } |
| 564 | 565 |
| 565 void MessagePumpForIO::DidProcessIOEvent() { | 566 void MessagePumpForIO::DidProcessIOEvent() { |
| 566 FOR_EACH_OBSERVER(IOObserver, io_observers_, DidProcessIOEvent()); | 567 FOR_EACH_OBSERVER(IOObserver, io_observers_, DidProcessIOEvent()); |
| 567 } | 568 } |
| 568 | 569 |
| 569 } // namespace base | 570 } // namespace base |
| OLD | NEW |