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_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/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
| 11 #include "base/process_util.h" | |
| 12 #include "base/stringprintf.h" | |
| 11 #include "base/win/wrapped_window_proc.h" | 13 #include "base/win/wrapped_window_proc.h" |
| 12 | 14 |
| 13 namespace base { | 15 namespace base { |
| 14 | 16 |
| 15 static const wchar_t kWndClass[] = L"Chrome_MessagePumpWindow"; | 17 static const wchar_t kWndClassFormat[] = L"Chrome_MessagePumpWindow%p"; |
| 16 | 18 |
| 17 // Message sent to get an additional time slice for pumping (processing) another | 19 // Message sent to get an additional time slice for pumping (processing) another |
| 18 // task (a series of such messages creates a continuous task pump). | 20 // task (a series of such messages creates a continuous task pump). |
| 19 static const int kMsgHaveWork = WM_USER + 1; | 21 static const int kMsgHaveWork = WM_USER + 1; |
| 20 | 22 |
| 21 //----------------------------------------------------------------------------- | 23 //----------------------------------------------------------------------------- |
| 22 // MessagePumpWin public: | 24 // MessagePumpWin public: |
| 23 | 25 |
| 24 void MessagePumpWin::AddObserver(MessagePumpObserver* observer) { | 26 void MessagePumpWin::AddObserver(MessagePumpObserver* observer) { |
| 25 observers_.AddObserver(observer); | 27 observers_.AddObserver(observer); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 75 int delay = static_cast<int>(timeout); | 77 int delay = static_cast<int>(timeout); |
| 76 if (delay < 0) | 78 if (delay < 0) |
| 77 delay = 0; | 79 delay = 0; |
| 78 | 80 |
| 79 return delay; | 81 return delay; |
| 80 } | 82 } |
| 81 | 83 |
| 82 //----------------------------------------------------------------------------- | 84 //----------------------------------------------------------------------------- |
| 83 // MessagePumpForUI public: | 85 // MessagePumpForUI public: |
| 84 | 86 |
| 85 MessagePumpForUI::MessagePumpForUI() { | 87 MessagePumpForUI::MessagePumpForUI() |
| 88 : atom_(0), | |
| 89 instance_(NULL), | |
| 90 message_hwnd_(NULL) { | |
| 86 InitMessageWnd(); | 91 InitMessageWnd(); |
| 87 } | 92 } |
| 88 | 93 |
| 89 MessagePumpForUI::~MessagePumpForUI() { | 94 MessagePumpForUI::~MessagePumpForUI() { |
| 90 DestroyWindow(message_hwnd_); | 95 if (message_hwnd_ != NULL) |
|
jar (doing other things)
2012/05/17 00:57:24
nit: you probably don't need this test. The const
| |
| 91 UnregisterClass(kWndClass, GetModuleHandle(NULL)); | 96 DestroyWindow(message_hwnd_); |
| 97 | |
| 98 if (atom_ != 0) | |
|
jar (doing other things)
2012/05/17 00:57:24
nit: The test is not needed since the constructors
| |
| 99 UnregisterClass(MAKEINTATOM(atom_), instance_); | |
| 92 } | 100 } |
| 93 | 101 |
| 94 void MessagePumpForUI::ScheduleWork() { | 102 void MessagePumpForUI::ScheduleWork() { |
| 95 if (InterlockedExchange(&have_work_, 1)) | 103 if (InterlockedExchange(&have_work_, 1)) |
| 96 return; // Someone else continued the pumping. | 104 return; // Someone else continued the pumping. |
| 97 | 105 |
| 98 // Make sure the MessagePump does some work for us. | 106 // Make sure the MessagePump does some work for us. |
| 99 PostMessage(message_hwnd_, kMsgHaveWork, reinterpret_cast<WPARAM>(this), 0); | 107 PostMessage(message_hwnd_, kMsgHaveWork, reinterpret_cast<WPARAM>(this), 0); |
| 100 } | 108 } |
| 101 | 109 |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 223 break; | 231 break; |
| 224 | 232 |
| 225 if (more_work_is_plausible) | 233 if (more_work_is_plausible) |
| 226 continue; | 234 continue; |
| 227 | 235 |
| 228 WaitForWork(); // Wait (sleep) until we have work to do again. | 236 WaitForWork(); // Wait (sleep) until we have work to do again. |
| 229 } | 237 } |
| 230 } | 238 } |
| 231 | 239 |
| 232 void MessagePumpForUI::InitMessageWnd() { | 240 void MessagePumpForUI::InitMessageWnd() { |
| 233 HINSTANCE hinst = GetModuleHandle(NULL); | 241 // Register a unique window class for each instance of the UI pump. |
| 242 string16 class_name = base::StringPrintf(kWndClassFormat, this); | |
| 234 | 243 |
| 235 WNDCLASSEX wc = {0}; | 244 WNDCLASSEX wc = {0}; |
| 236 wc.cbSize = sizeof(wc); | 245 wc.cbSize = sizeof(wc); |
| 237 wc.lpfnWndProc = base::win::WrappedWindowProc<WndProcThunk>; | 246 wc.lpfnWndProc = base::win::WrappedWindowProc<WndProcThunk>; |
| 238 wc.hInstance = hinst; | 247 instance_ = base::GetModuleFromAddress(static_cast<void*>(wc.lpfnWndProc)); |
|
jar (doing other things)
2012/05/17 02:48:09
Talking to rvargas, he notes that a "minimal" chan
alexeypa (please no reviews)
2012/05/17 18:30:19
I guess you are right. Sure such a fix will not gi
| |
| 239 wc.lpszClassName = kWndClass; | 248 wc.hInstance = instance_; |
| 240 RegisterClassEx(&wc); | 249 wc.lpszClassName = class_name.c_str(); |
| 250 atom_ = RegisterClassEx(&wc); | |
| 251 CHECK(atom_ != 0); | |
| 241 | 252 |
| 242 message_hwnd_ = | 253 message_hwnd_ = CreateWindow( |
| 243 CreateWindow(kWndClass, 0, 0, 0, 0, 0, 0, HWND_MESSAGE, 0, hinst, 0); | 254 MAKEINTATOM(atom_), 0, 0, 0, 0, 0, 0, HWND_MESSAGE, 0, instance_, 0); |
| 244 DCHECK(message_hwnd_); | 255 CHECK(message_hwnd_); |
| 245 } | 256 } |
| 246 | 257 |
| 247 void MessagePumpForUI::WaitForWork() { | 258 void MessagePumpForUI::WaitForWork() { |
| 248 // Wait until a message is available, up to the time needed by the timer | 259 // Wait until a message is available, up to the time needed by the timer |
| 249 // manager to fire the next set of timers. | 260 // manager to fire the next set of timers. |
| 250 int delay = GetCurrentDelay(); | 261 int delay = GetCurrentDelay(); |
| 251 if (delay < 0) // Negative value means no timers waiting. | 262 if (delay < 0) // Negative value means no timers waiting. |
| 252 delay = INFINITE; | 263 delay = INFINITE; |
| 253 | 264 |
| 254 DWORD result; | 265 DWORD result; |
| (...skipping 319 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 574 | 585 |
| 575 void MessagePumpForIO::WillProcessIOEvent() { | 586 void MessagePumpForIO::WillProcessIOEvent() { |
| 576 FOR_EACH_OBSERVER(IOObserver, io_observers_, WillProcessIOEvent()); | 587 FOR_EACH_OBSERVER(IOObserver, io_observers_, WillProcessIOEvent()); |
| 577 } | 588 } |
| 578 | 589 |
| 579 void MessagePumpForIO::DidProcessIOEvent() { | 590 void MessagePumpForIO::DidProcessIOEvent() { |
| 580 FOR_EACH_OBSERVER(IOObserver, io_observers_, DidProcessIOEvent()); | 591 FOR_EACH_OBSERVER(IOObserver, io_observers_, DidProcessIOEvent()); |
| 581 } | 592 } |
| 582 | 593 |
| 583 } // namespace base | 594 } // namespace base |
| OLD | NEW |