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 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <limits> | 10 #include <limits> |
(...skipping 10 matching lines...) Expand all Loading... |
21 | 21 |
22 namespace { | 22 namespace { |
23 | 23 |
24 enum MessageLoopProblems { | 24 enum MessageLoopProblems { |
25 MESSAGE_POST_ERROR, | 25 MESSAGE_POST_ERROR, |
26 COMPLETION_POST_ERROR, | 26 COMPLETION_POST_ERROR, |
27 SET_TIMER_ERROR, | 27 SET_TIMER_ERROR, |
28 MESSAGE_LOOP_PROBLEM_MAX, | 28 MESSAGE_LOOP_PROBLEM_MAX, |
29 }; | 29 }; |
30 | 30 |
31 // The following define pointers to user32 API's for the API's which are used | |
32 // in this file. These are added to avoid directly depending on user32 from | |
33 // base as there are users of base who don't want this. | |
34 decltype(::TranslateMessage)* g_translate_message = nullptr; | |
35 decltype(::DispatchMessageW)* g_dispatch_message = nullptr; | |
36 decltype(::PeekMessageW)* g_peek_message = nullptr; | |
37 decltype(::PostMessageW)* g_post_message = nullptr; | |
38 decltype(::DefWindowProcW)* g_def_window_proc = nullptr; | |
39 decltype(::PostQuitMessage)* g_post_quit = nullptr; | |
40 decltype(::UnregisterClassW)* g_unregister_class = nullptr; | |
41 decltype(::RegisterClassExW)* g_register_class = nullptr; | |
42 decltype(::CreateWindowExW)* g_create_window_ex = nullptr; | |
43 decltype(::DestroyWindow)* g_destroy_window = nullptr; | |
44 decltype(::CallMsgFilterW)* g_call_msg_filter = nullptr; | |
45 decltype(::GetQueueStatus)* g_get_queue_status = nullptr; | |
46 decltype(::MsgWaitForMultipleObjectsEx)* g_msg_wait_for_multiple_objects_ex = | |
47 nullptr; | |
48 decltype(::SetTimer)* g_set_timer = nullptr; | |
49 decltype(::KillTimer)* g_kill_timer = nullptr; | |
50 | |
51 #define GET_USER32_API(module, name) \ | |
52 reinterpret_cast<decltype(name)*>(::GetProcAddress(module, #name)) | |
53 | |
54 // Initializes the global pointers to user32 APIs for the API's used in this | |
55 // file. | |
56 void InitUser32APIs() { | |
57 if (g_translate_message) | |
58 return; | |
59 | |
60 HMODULE user32_module = ::GetModuleHandle(L"user32.dll"); | |
61 CHECK(user32_module); | |
62 | |
63 g_translate_message = GET_USER32_API(user32_module, TranslateMessage); | |
64 CHECK(g_translate_message); | |
65 | |
66 g_dispatch_message = GET_USER32_API(user32_module, DispatchMessageW); | |
67 CHECK(g_dispatch_message); | |
68 | |
69 g_peek_message = GET_USER32_API(user32_module, PeekMessageW); | |
70 CHECK(g_peek_message); | |
71 | |
72 g_post_message = GET_USER32_API(user32_module, PostMessageW); | |
73 CHECK(g_post_message); | |
74 | |
75 g_def_window_proc = GET_USER32_API(user32_module, DefWindowProcW); | |
76 CHECK(g_def_window_proc); | |
77 | |
78 g_post_quit = GET_USER32_API(user32_module, PostQuitMessage); | |
79 CHECK(g_post_quit); | |
80 | |
81 g_unregister_class = GET_USER32_API(user32_module, UnregisterClassW); | |
82 CHECK(g_unregister_class); | |
83 | |
84 g_register_class = GET_USER32_API(user32_module, RegisterClassExW); | |
85 CHECK(g_register_class); | |
86 | |
87 g_create_window_ex = GET_USER32_API(user32_module, CreateWindowExW); | |
88 CHECK(g_create_window_ex); | |
89 | |
90 g_destroy_window = GET_USER32_API(user32_module, DestroyWindow); | |
91 CHECK(g_destroy_window); | |
92 | |
93 g_call_msg_filter = GET_USER32_API(user32_module, CallMsgFilterW); | |
94 CHECK(g_call_msg_filter); | |
95 | |
96 g_get_queue_status = GET_USER32_API(user32_module, GetQueueStatus); | |
97 CHECK(g_get_queue_status); | |
98 | |
99 g_msg_wait_for_multiple_objects_ex = | |
100 GET_USER32_API(user32_module, MsgWaitForMultipleObjectsEx); | |
101 CHECK(g_msg_wait_for_multiple_objects_ex); | |
102 | |
103 g_set_timer = GET_USER32_API(user32_module, SetTimer); | |
104 CHECK(g_set_timer); | |
105 | |
106 g_kill_timer = GET_USER32_API(user32_module, KillTimer); | |
107 CHECK(g_kill_timer); | |
108 } | |
109 | |
110 } // namespace | 31 } // namespace |
111 | 32 |
112 static const wchar_t kWndClassFormat[] = L"Chrome_MessagePumpWindow_%p"; | 33 static const wchar_t kWndClassFormat[] = L"Chrome_MessagePumpWindow_%p"; |
113 | 34 |
114 // Message sent to get an additional time slice for pumping (processing) another | 35 // Message sent to get an additional time slice for pumping (processing) another |
115 // task (a series of such messages creates a continuous task pump). | 36 // task (a series of such messages creates a continuous task pump). |
116 static const int kMsgHaveWork = WM_USER + 1; | 37 static const int kMsgHaveWork = WM_USER + 1; |
117 | 38 |
118 // The application-defined code passed to the hook procedure. | 39 // The application-defined code passed to the hook procedure. |
119 static const int kMessageFilterCode = 0x5001; | 40 static const int kMessageFilterCode = 0x5001; |
120 | 41 |
121 //----------------------------------------------------------------------------- | 42 //----------------------------------------------------------------------------- |
122 // MessagePumpWin public: | 43 // MessagePumpWin public: |
123 | 44 |
124 MessagePumpWin::MessagePumpWin() { | |
125 InitUser32APIs(); | |
126 } | |
127 | |
128 void MessagePumpWin::Run(Delegate* delegate) { | 45 void MessagePumpWin::Run(Delegate* delegate) { |
129 RunState s; | 46 RunState s; |
130 s.delegate = delegate; | 47 s.delegate = delegate; |
131 s.should_quit = false; | 48 s.should_quit = false; |
132 s.run_depth = state_ ? state_->run_depth + 1 : 1; | 49 s.run_depth = state_ ? state_->run_depth + 1 : 1; |
133 | 50 |
134 // TODO(stanisc): crbug.com/596190: Remove this code once the bug is fixed. | 51 // TODO(stanisc): crbug.com/596190: Remove this code once the bug is fixed. |
135 s.schedule_work_error_count = 0; | 52 s.schedule_work_error_count = 0; |
136 s.last_schedule_work_error_time = Time(); | 53 s.last_schedule_work_error_time = Time(); |
137 | 54 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 | 89 |
173 //----------------------------------------------------------------------------- | 90 //----------------------------------------------------------------------------- |
174 // MessagePumpForUI public: | 91 // MessagePumpForUI public: |
175 | 92 |
176 MessagePumpForUI::MessagePumpForUI() | 93 MessagePumpForUI::MessagePumpForUI() |
177 : atom_(0) { | 94 : atom_(0) { |
178 InitMessageWnd(); | 95 InitMessageWnd(); |
179 } | 96 } |
180 | 97 |
181 MessagePumpForUI::~MessagePumpForUI() { | 98 MessagePumpForUI::~MessagePumpForUI() { |
182 g_destroy_window(message_hwnd_); | 99 DestroyWindow(message_hwnd_); |
183 g_unregister_class(MAKEINTATOM(atom_), CURRENT_MODULE()); | 100 UnregisterClass(MAKEINTATOM(atom_), CURRENT_MODULE()); |
184 } | 101 } |
185 | 102 |
186 void MessagePumpForUI::ScheduleWork() { | 103 void MessagePumpForUI::ScheduleWork() { |
187 if (InterlockedExchange(&work_state_, HAVE_WORK) != READY) | 104 if (InterlockedExchange(&work_state_, HAVE_WORK) != READY) |
188 return; // Someone else continued the pumping. | 105 return; // Someone else continued the pumping. |
189 | 106 |
190 // Make sure the MessagePump does some work for us. | 107 // Make sure the MessagePump does some work for us. |
191 BOOL ret = g_post_message(message_hwnd_, kMsgHaveWork, | 108 BOOL ret = PostMessage(message_hwnd_, kMsgHaveWork, |
192 reinterpret_cast<WPARAM>(this), 0); | 109 reinterpret_cast<WPARAM>(this), 0); |
193 if (ret) | 110 if (ret) |
194 return; // There was room in the Window Message queue. | 111 return; // There was room in the Window Message queue. |
195 | 112 |
196 // We have failed to insert a have-work message, so there is a chance that we | 113 // We have failed to insert a have-work message, so there is a chance that we |
197 // will starve tasks/timers while sitting in a nested message loop. Nested | 114 // will starve tasks/timers while sitting in a nested message loop. Nested |
198 // loops only look at Windows Message queues, and don't look at *our* task | 115 // loops only look at Windows Message queues, and don't look at *our* task |
199 // queues, etc., so we might not get a time slice in such. :-( | 116 // queues, etc., so we might not get a time slice in such. :-( |
200 // We could abort here, but the fear is that this failure mode is plausibly | 117 // We could abort here, but the fear is that this failure mode is plausibly |
201 // common (queue is full, of about 2000 messages), so we'll do a near-graceful | 118 // common (queue is full, of about 2000 messages), so we'll do a near-graceful |
202 // recovery. Nested loops are pretty transient (we think), so this will | 119 // recovery. Nested loops are pretty transient (we think), so this will |
(...skipping 19 matching lines...) Expand all Loading... |
222 LRESULT CALLBACK MessagePumpForUI::WndProcThunk( | 139 LRESULT CALLBACK MessagePumpForUI::WndProcThunk( |
223 HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) { | 140 HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) { |
224 switch (message) { | 141 switch (message) { |
225 case kMsgHaveWork: | 142 case kMsgHaveWork: |
226 reinterpret_cast<MessagePumpForUI*>(wparam)->HandleWorkMessage(); | 143 reinterpret_cast<MessagePumpForUI*>(wparam)->HandleWorkMessage(); |
227 break; | 144 break; |
228 case WM_TIMER: | 145 case WM_TIMER: |
229 reinterpret_cast<MessagePumpForUI*>(wparam)->HandleTimerMessage(); | 146 reinterpret_cast<MessagePumpForUI*>(wparam)->HandleTimerMessage(); |
230 break; | 147 break; |
231 } | 148 } |
232 return g_def_window_proc(hwnd, message, wparam, lparam); | 149 return DefWindowProc(hwnd, message, wparam, lparam); |
233 } | 150 } |
234 | 151 |
235 void MessagePumpForUI::DoRunLoop() { | 152 void MessagePumpForUI::DoRunLoop() { |
236 // IF this was just a simple PeekMessage() loop (servicing all possible work | 153 // IF this was just a simple PeekMessage() loop (servicing all possible work |
237 // queues), then Windows would try to achieve the following order according | 154 // queues), then Windows would try to achieve the following order according |
238 // to MSDN documentation about PeekMessage with no filter): | 155 // to MSDN documentation about PeekMessage with no filter): |
239 // * Sent messages | 156 // * Sent messages |
240 // * Posted messages | 157 // * Posted messages |
241 // * Sent messages (again) | 158 // * Sent messages (again) |
242 // * WM_PAINT messages | 159 // * WM_PAINT messages |
(...skipping 20 matching lines...) Expand all Loading... |
263 if (state_->should_quit) | 180 if (state_->should_quit) |
264 break; | 181 break; |
265 | 182 |
266 more_work_is_plausible |= | 183 more_work_is_plausible |= |
267 state_->delegate->DoDelayedWork(&delayed_work_time_); | 184 state_->delegate->DoDelayedWork(&delayed_work_time_); |
268 // If we did not process any delayed work, then we can assume that our | 185 // If we did not process any delayed work, then we can assume that our |
269 // existing WM_TIMER if any will fire when delayed work should run. We | 186 // existing WM_TIMER if any will fire when delayed work should run. We |
270 // don't want to disturb that timer if it is already in flight. However, | 187 // don't want to disturb that timer if it is already in flight. However, |
271 // if we did do all remaining delayed work, then lets kill the WM_TIMER. | 188 // if we did do all remaining delayed work, then lets kill the WM_TIMER. |
272 if (more_work_is_plausible && delayed_work_time_.is_null()) | 189 if (more_work_is_plausible && delayed_work_time_.is_null()) |
273 g_kill_timer(message_hwnd_, reinterpret_cast<UINT_PTR>(this)); | 190 KillTimer(message_hwnd_, reinterpret_cast<UINT_PTR>(this)); |
274 if (state_->should_quit) | 191 if (state_->should_quit) |
275 break; | 192 break; |
276 | 193 |
277 if (more_work_is_plausible) | 194 if (more_work_is_plausible) |
278 continue; | 195 continue; |
279 | 196 |
280 more_work_is_plausible = state_->delegate->DoIdleWork(); | 197 more_work_is_plausible = state_->delegate->DoIdleWork(); |
281 if (state_->should_quit) | 198 if (state_->should_quit) |
282 break; | 199 break; |
283 | 200 |
284 if (more_work_is_plausible) | 201 if (more_work_is_plausible) |
285 continue; | 202 continue; |
286 | 203 |
287 WaitForWork(); // Wait (sleep) until we have work to do again. | 204 WaitForWork(); // Wait (sleep) until we have work to do again. |
288 } | 205 } |
289 } | 206 } |
290 | 207 |
291 void MessagePumpForUI::InitMessageWnd() { | 208 void MessagePumpForUI::InitMessageWnd() { |
292 // Generate a unique window class name. | 209 // Generate a unique window class name. |
293 string16 class_name = StringPrintf(kWndClassFormat, this); | 210 string16 class_name = StringPrintf(kWndClassFormat, this); |
294 | 211 |
295 HINSTANCE instance = CURRENT_MODULE(); | 212 HINSTANCE instance = CURRENT_MODULE(); |
296 WNDCLASSEX wc = {0}; | 213 WNDCLASSEX wc = {0}; |
297 wc.cbSize = sizeof(wc); | 214 wc.cbSize = sizeof(wc); |
298 wc.lpfnWndProc = base::win::WrappedWindowProc<WndProcThunk>; | 215 wc.lpfnWndProc = base::win::WrappedWindowProc<WndProcThunk>; |
299 wc.hInstance = instance; | 216 wc.hInstance = instance; |
300 wc.lpszClassName = class_name.c_str(); | 217 wc.lpszClassName = class_name.c_str(); |
301 atom_ = g_register_class(&wc); | 218 atom_ = RegisterClassEx(&wc); |
302 DCHECK(atom_); | 219 DCHECK(atom_); |
303 | 220 |
304 message_hwnd_ = g_create_window_ex(0, MAKEINTATOM(atom_), 0, 0, 0, 0, 0, 0, | 221 message_hwnd_ = CreateWindow(MAKEINTATOM(atom_), 0, 0, 0, 0, 0, 0, |
305 HWND_MESSAGE, 0, instance, 0); | 222 HWND_MESSAGE, 0, instance, 0); |
306 DCHECK(message_hwnd_); | 223 DCHECK(message_hwnd_); |
307 } | 224 } |
308 | 225 |
309 void MessagePumpForUI::WaitForWork() { | 226 void MessagePumpForUI::WaitForWork() { |
310 // Wait until a message is available, up to the time needed by the timer | 227 // Wait until a message is available, up to the time needed by the timer |
311 // manager to fire the next set of timers. | 228 // manager to fire the next set of timers. |
312 int delay; | 229 int delay; |
313 DWORD wait_flags = MWMO_INPUTAVAILABLE; | 230 DWORD wait_flags = MWMO_INPUTAVAILABLE; |
314 | 231 |
315 while ((delay = GetCurrentDelay()) != 0) { | 232 while ((delay = GetCurrentDelay()) != 0) { |
316 if (delay < 0) // Negative value means no timers waiting. | 233 if (delay < 0) // Negative value means no timers waiting. |
317 delay = INFINITE; | 234 delay = INFINITE; |
318 | 235 |
319 DWORD result = g_msg_wait_for_multiple_objects_ex(0, nullptr, delay, | 236 DWORD result = |
320 QS_ALLINPUT, wait_flags); | 237 MsgWaitForMultipleObjectsEx(0, NULL, delay, QS_ALLINPUT, wait_flags); |
321 | 238 |
322 if (WAIT_OBJECT_0 == result) { | 239 if (WAIT_OBJECT_0 == result) { |
323 // A WM_* message is available. | 240 // A WM_* message is available. |
324 // If a parent child relationship exists between windows across threads | 241 // If a parent child relationship exists between windows across threads |
325 // then their thread inputs are implicitly attached. | 242 // then their thread inputs are implicitly attached. |
326 // This causes the MsgWaitForMultipleObjectsEx API to return indicating | 243 // This causes the MsgWaitForMultipleObjectsEx API to return indicating |
327 // that messages are ready for processing (Specifically, mouse messages | 244 // that messages are ready for processing (Specifically, mouse messages |
328 // intended for the child window may appear if the child window has | 245 // intended for the child window may appear if the child window has |
329 // capture). | 246 // capture). |
330 // The subsequent PeekMessages call may fail to return any messages thus | 247 // The subsequent PeekMessages call may fail to return any messages thus |
331 // causing us to enter a tight loop at times. | 248 // causing us to enter a tight loop at times. |
332 // The code below is a workaround to give the child window | 249 // The code below is a workaround to give the child window |
333 // some time to process its input messages by looping back to | 250 // some time to process its input messages by looping back to |
334 // MsgWaitForMultipleObjectsEx above when there are no messages for the | 251 // MsgWaitForMultipleObjectsEx above when there are no messages for the |
335 // current thread. | 252 // current thread. |
336 MSG msg = {0}; | 253 MSG msg = {0}; |
337 bool has_pending_sent_message = | 254 bool has_pending_sent_message = |
338 (HIWORD(g_get_queue_status(QS_SENDMESSAGE)) & QS_SENDMESSAGE) != 0; | 255 (HIWORD(GetQueueStatus(QS_SENDMESSAGE)) & QS_SENDMESSAGE) != 0; |
339 if (has_pending_sent_message || | 256 if (has_pending_sent_message || |
340 g_peek_message(&msg, nullptr, 0, 0, PM_NOREMOVE)) { | 257 PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)) { |
341 return; | 258 return; |
342 } | 259 } |
343 | 260 |
344 // We know there are no more messages for this thread because PeekMessage | 261 // We know there are no more messages for this thread because PeekMessage |
345 // has returned false. Reset |wait_flags| so that we wait for a *new* | 262 // has returned false. Reset |wait_flags| so that we wait for a *new* |
346 // message. | 263 // message. |
347 wait_flags = 0; | 264 wait_flags = 0; |
348 } | 265 } |
349 | 266 |
350 DCHECK_NE(WAIT_FAILED, result) << GetLastError(); | 267 DCHECK_NE(WAIT_FAILED, result) << GetLastError(); |
(...skipping 17 matching lines...) Expand all Loading... |
368 | 285 |
369 // Now give the delegate a chance to do some work. He'll let us know if he | 286 // Now give the delegate a chance to do some work. He'll let us know if he |
370 // needs to do more work. | 287 // needs to do more work. |
371 if (state_->delegate->DoWork()) | 288 if (state_->delegate->DoWork()) |
372 ScheduleWork(); | 289 ScheduleWork(); |
373 state_->delegate->DoDelayedWork(&delayed_work_time_); | 290 state_->delegate->DoDelayedWork(&delayed_work_time_); |
374 RescheduleTimer(); | 291 RescheduleTimer(); |
375 } | 292 } |
376 | 293 |
377 void MessagePumpForUI::HandleTimerMessage() { | 294 void MessagePumpForUI::HandleTimerMessage() { |
378 g_kill_timer(message_hwnd_, reinterpret_cast<UINT_PTR>(this)); | 295 KillTimer(message_hwnd_, reinterpret_cast<UINT_PTR>(this)); |
379 | 296 |
380 // If we are being called outside of the context of Run, then don't do | 297 // If we are being called outside of the context of Run, then don't do |
381 // anything. This could correspond to a MessageBox call or something of | 298 // anything. This could correspond to a MessageBox call or something of |
382 // that sort. | 299 // that sort. |
383 if (!state_) | 300 if (!state_) |
384 return; | 301 return; |
385 | 302 |
386 state_->delegate->DoDelayedWork(&delayed_work_time_); | 303 state_->delegate->DoDelayedWork(&delayed_work_time_); |
387 RescheduleTimer(); | 304 RescheduleTimer(); |
388 } | 305 } |
(...skipping 24 matching lines...) Expand all Loading... |
413 int delay_msec = GetCurrentDelay(); | 330 int delay_msec = GetCurrentDelay(); |
414 DCHECK_GE(delay_msec, 0); | 331 DCHECK_GE(delay_msec, 0); |
415 if (delay_msec == 0) { | 332 if (delay_msec == 0) { |
416 ScheduleWork(); | 333 ScheduleWork(); |
417 } else { | 334 } else { |
418 if (delay_msec < USER_TIMER_MINIMUM) | 335 if (delay_msec < USER_TIMER_MINIMUM) |
419 delay_msec = USER_TIMER_MINIMUM; | 336 delay_msec = USER_TIMER_MINIMUM; |
420 | 337 |
421 // Create a WM_TIMER event that will wake us up to check for any pending | 338 // Create a WM_TIMER event that will wake us up to check for any pending |
422 // timers (in case we are running within a nested, external sub-pump). | 339 // timers (in case we are running within a nested, external sub-pump). |
423 BOOL ret = g_set_timer(message_hwnd_, reinterpret_cast<UINT_PTR>(this), | 340 BOOL ret = SetTimer(message_hwnd_, reinterpret_cast<UINT_PTR>(this), |
424 delay_msec, nullptr); | 341 delay_msec, NULL); |
425 if (ret) | 342 if (ret) |
426 return; | 343 return; |
427 // If we can't set timers, we are in big trouble... but cross our fingers | 344 // If we can't set timers, we are in big trouble... but cross our fingers |
428 // for now. | 345 // for now. |
429 // TODO(jar): If we don't see this error, use a CHECK() here instead. | 346 // TODO(jar): If we don't see this error, use a CHECK() here instead. |
430 UMA_HISTOGRAM_ENUMERATION("Chrome.MessageLoopProblem", SET_TIMER_ERROR, | 347 UMA_HISTOGRAM_ENUMERATION("Chrome.MessageLoopProblem", SET_TIMER_ERROR, |
431 MESSAGE_LOOP_PROBLEM_MAX); | 348 MESSAGE_LOOP_PROBLEM_MAX); |
432 } | 349 } |
433 } | 350 } |
434 | 351 |
435 bool MessagePumpForUI::ProcessNextWindowsMessage() { | 352 bool MessagePumpForUI::ProcessNextWindowsMessage() { |
436 // If there are sent messages in the queue then PeekMessage internally | 353 // If there are sent messages in the queue then PeekMessage internally |
437 // dispatches the message and returns false. We return true in this | 354 // dispatches the message and returns false. We return true in this |
438 // case to ensure that the message loop peeks again instead of calling | 355 // case to ensure that the message loop peeks again instead of calling |
439 // MsgWaitForMultipleObjectsEx again. | 356 // MsgWaitForMultipleObjectsEx again. |
440 bool sent_messages_in_queue = false; | 357 bool sent_messages_in_queue = false; |
441 DWORD queue_status = g_get_queue_status(QS_SENDMESSAGE); | 358 DWORD queue_status = GetQueueStatus(QS_SENDMESSAGE); |
442 if (HIWORD(queue_status) & QS_SENDMESSAGE) | 359 if (HIWORD(queue_status) & QS_SENDMESSAGE) |
443 sent_messages_in_queue = true; | 360 sent_messages_in_queue = true; |
444 | 361 |
445 MSG msg; | 362 MSG msg; |
446 if (g_peek_message(&msg, nullptr, 0, 0, PM_REMOVE) != FALSE) | 363 if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) != FALSE) |
447 return ProcessMessageHelper(msg); | 364 return ProcessMessageHelper(msg); |
448 | 365 |
449 return sent_messages_in_queue; | 366 return sent_messages_in_queue; |
450 } | 367 } |
451 | 368 |
452 bool MessagePumpForUI::ProcessMessageHelper(const MSG& msg) { | 369 bool MessagePumpForUI::ProcessMessageHelper(const MSG& msg) { |
453 TRACE_EVENT1("base", "MessagePumpForUI::ProcessMessageHelper", | 370 TRACE_EVENT1("base", "MessagePumpForUI::ProcessMessageHelper", |
454 "message", msg.message); | 371 "message", msg.message); |
455 if (WM_QUIT == msg.message) { | 372 if (WM_QUIT == msg.message) { |
456 // Repost the QUIT message so that it will be retrieved by the primary | 373 // Repost the QUIT message so that it will be retrieved by the primary |
457 // GetMessage() loop. | 374 // GetMessage() loop. |
458 state_->should_quit = true; | 375 state_->should_quit = true; |
459 g_post_quit(static_cast<int>(msg.wParam)); | 376 PostQuitMessage(static_cast<int>(msg.wParam)); |
460 return false; | 377 return false; |
461 } | 378 } |
462 | 379 |
463 // While running our main message pump, we discard kMsgHaveWork messages. | 380 // While running our main message pump, we discard kMsgHaveWork messages. |
464 if (msg.message == kMsgHaveWork && msg.hwnd == message_hwnd_) | 381 if (msg.message == kMsgHaveWork && msg.hwnd == message_hwnd_) |
465 return ProcessPumpReplacementMessage(); | 382 return ProcessPumpReplacementMessage(); |
466 | 383 |
467 if (g_call_msg_filter(const_cast<MSG*>(&msg), kMessageFilterCode)) | 384 if (CallMsgFilter(const_cast<MSG*>(&msg), kMessageFilterCode)) |
468 return true; | 385 return true; |
469 | 386 |
470 g_translate_message(&msg); | 387 TranslateMessage(&msg); |
471 g_dispatch_message(&msg); | 388 DispatchMessage(&msg); |
472 | 389 |
473 return true; | 390 return true; |
474 } | 391 } |
475 | 392 |
476 bool MessagePumpForUI::ProcessPumpReplacementMessage() { | 393 bool MessagePumpForUI::ProcessPumpReplacementMessage() { |
477 // When we encounter a kMsgHaveWork message, this method is called to peek and | 394 // When we encounter a kMsgHaveWork message, this method is called to peek and |
478 // process a replacement message. The goal is to make the kMsgHaveWork as non- | 395 // process a replacement message. The goal is to make the kMsgHaveWork as non- |
479 // intrusive as possible, even though a continuous stream of such messages are | 396 // intrusive as possible, even though a continuous stream of such messages are |
480 // posted. This method carefully peeks a message while there is no chance for | 397 // posted. This method carefully peeks a message while there is no chance for |
481 // a kMsgHaveWork to be pending, then resets the |have_work_| flag (allowing a | 398 // a kMsgHaveWork to be pending, then resets the |have_work_| flag (allowing a |
482 // replacement kMsgHaveWork to possibly be posted), and finally dispatches | 399 // replacement kMsgHaveWork to possibly be posted), and finally dispatches |
483 // that peeked replacement. Note that the re-post of kMsgHaveWork may be | 400 // that peeked replacement. Note that the re-post of kMsgHaveWork may be |
484 // asynchronous to this thread!! | 401 // asynchronous to this thread!! |
485 | 402 |
486 MSG msg; | 403 MSG msg; |
487 const bool have_message = | 404 const bool have_message = PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) != FALSE; |
488 g_peek_message(&msg, nullptr, 0, 0, PM_REMOVE) != FALSE; | |
489 | 405 |
490 // Expect no message or a message different than kMsgHaveWork. | 406 // Expect no message or a message different than kMsgHaveWork. |
491 DCHECK(!have_message || kMsgHaveWork != msg.message || | 407 DCHECK(!have_message || kMsgHaveWork != msg.message || |
492 msg.hwnd != message_hwnd_); | 408 msg.hwnd != message_hwnd_); |
493 | 409 |
494 // Since we discarded a kMsgHaveWork message, we must update the flag. | 410 // Since we discarded a kMsgHaveWork message, we must update the flag. |
495 int old_work_state_ = InterlockedExchange(&work_state_, READY); | 411 int old_work_state_ = InterlockedExchange(&work_state_, READY); |
496 DCHECK_EQ(HAVE_WORK, old_work_state_); | 412 DCHECK_EQ(HAVE_WORK, old_work_state_); |
497 | 413 |
498 // We don't need a special time slice if we didn't have_message to process. | 414 // We don't need a special time slice if we didn't have_message to process. |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
608 if (delay < 0) // Negative value means no timers waiting. | 524 if (delay < 0) // Negative value means no timers waiting. |
609 delay = INFINITE; | 525 delay = INFINITE; |
610 | 526 |
611 // TODO(stanisc): crbug.com/596190: Preserve for crash dump analysis. | 527 // TODO(stanisc): crbug.com/596190: Preserve for crash dump analysis. |
612 // Remove this when the bug is fixed. | 528 // Remove this when the bug is fixed. |
613 TimeTicks wait_for_work_timeticks = TimeTicks::Now(); | 529 TimeTicks wait_for_work_timeticks = TimeTicks::Now(); |
614 debug::Alias(&wait_for_work_timeticks); | 530 debug::Alias(&wait_for_work_timeticks); |
615 debug::Alias(&delay); | 531 debug::Alias(&delay); |
616 | 532 |
617 DWORD result = | 533 DWORD result = |
618 g_msg_wait_for_multiple_objects_ex(1, &event_, delay, QS_ALLINPUT, 0); | 534 MsgWaitForMultipleObjectsEx(1, &event_, delay, QS_ALLINPUT, 0); |
619 DCHECK_NE(WAIT_FAILED, result) << GetLastError(); | 535 DCHECK_NE(WAIT_FAILED, result) << GetLastError(); |
620 if (result != WAIT_TIMEOUT) { | 536 if (result != WAIT_TIMEOUT) { |
621 // Either work or message available. | 537 // Either work or message available. |
622 return; | 538 return; |
623 } | 539 } |
624 } | 540 } |
625 } | 541 } |
626 | 542 |
627 bool MessagePumpForGpu::ProcessNextMessage() { | 543 bool MessagePumpForGpu::ProcessNextMessage() { |
628 MSG msg; | 544 MSG msg; |
629 if (!g_peek_message(&msg, nullptr, 0, 0, PM_REMOVE)) | 545 if (!PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE)) |
630 return false; | 546 return false; |
631 | 547 |
632 if (msg.message == WM_QUIT) { | 548 if (msg.message == WM_QUIT) { |
633 // Repost the QUIT message so that it will be retrieved by the primary | 549 // Repost the QUIT message so that it will be retrieved by the primary |
634 // GetMessage() loop. | 550 // GetMessage() loop. |
635 state_->should_quit = true; | 551 state_->should_quit = true; |
636 g_post_quit(static_cast<int>(msg.wParam)); | 552 PostQuitMessage(static_cast<int>(msg.wParam)); |
637 return false; | 553 return false; |
638 } | 554 } |
639 | 555 |
640 if (!g_call_msg_filter(const_cast<MSG*>(&msg), kMessageFilterCode)) { | 556 if (!CallMsgFilter(const_cast<MSG*>(&msg), kMessageFilterCode)) { |
641 g_translate_message(&msg); | 557 TranslateMessage(&msg); |
642 g_dispatch_message(&msg); | 558 DispatchMessage(&msg); |
643 } | 559 } |
644 | 560 |
645 return true; | 561 return true; |
646 } | 562 } |
647 | 563 |
648 //----------------------------------------------------------------------------- | 564 //----------------------------------------------------------------------------- |
649 // MessagePumpForIO public: | 565 // MessagePumpForIO public: |
650 | 566 |
651 MessagePumpForIO::IOContext::IOContext() { | 567 MessagePumpForIO::IOContext::IOContext() { |
652 memset(&overlapped, 0, sizeof(overlapped)); | 568 memset(&overlapped, 0, sizeof(overlapped)); |
653 } | 569 } |
654 | 570 |
655 MessagePumpForIO::MessagePumpForIO() { | 571 MessagePumpForIO::MessagePumpForIO() { |
656 port_.Set(CreateIoCompletionPort(INVALID_HANDLE_VALUE, nullptr, | 572 port_.Set(CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, NULL, 1)); |
657 reinterpret_cast<ULONG_PTR>(nullptr), 1)); | |
658 DCHECK(port_.IsValid()); | 573 DCHECK(port_.IsValid()); |
659 } | 574 } |
660 | 575 |
661 MessagePumpForIO::~MessagePumpForIO() = default; | 576 MessagePumpForIO::~MessagePumpForIO() = default; |
662 | 577 |
663 void MessagePumpForIO::ScheduleWork() { | 578 void MessagePumpForIO::ScheduleWork() { |
664 if (InterlockedExchange(&work_state_, HAVE_WORK) != READY) | 579 if (InterlockedExchange(&work_state_, HAVE_WORK) != READY) |
665 return; // Someone else continued the pumping. | 580 return; // Someone else continued the pumping. |
666 | 581 |
667 // Make sure the MessagePump does some work for us. | 582 // Make sure the MessagePump does some work for us. |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
715 // messages waiting. On the other hand, when any of these methods return | 630 // messages waiting. On the other hand, when any of these methods return |
716 // having done no work, then it is pretty unlikely that calling them | 631 // having done no work, then it is pretty unlikely that calling them |
717 // again quickly will find any work to do. Finally, if they all say they | 632 // again quickly will find any work to do. Finally, if they all say they |
718 // had no work, then it is a good time to consider sleeping (waiting) for | 633 // had no work, then it is a good time to consider sleeping (waiting) for |
719 // more work. | 634 // more work. |
720 | 635 |
721 bool more_work_is_plausible = state_->delegate->DoWork(); | 636 bool more_work_is_plausible = state_->delegate->DoWork(); |
722 if (state_->should_quit) | 637 if (state_->should_quit) |
723 break; | 638 break; |
724 | 639 |
725 more_work_is_plausible |= WaitForIOCompletion(0, nullptr); | 640 more_work_is_plausible |= WaitForIOCompletion(0, NULL); |
726 if (state_->should_quit) | 641 if (state_->should_quit) |
727 break; | 642 break; |
728 | 643 |
729 more_work_is_plausible |= | 644 more_work_is_plausible |= |
730 state_->delegate->DoDelayedWork(&delayed_work_time_); | 645 state_->delegate->DoDelayedWork(&delayed_work_time_); |
731 if (state_->should_quit) | 646 if (state_->should_quit) |
732 break; | 647 break; |
733 | 648 |
734 if (more_work_is_plausible) | 649 if (more_work_is_plausible) |
735 continue; | 650 continue; |
(...skipping 13 matching lines...) Expand all Loading... |
749 // the next set of timers. | 664 // the next set of timers. |
750 void MessagePumpForIO::WaitForWork() { | 665 void MessagePumpForIO::WaitForWork() { |
751 // We do not support nested IO message loops. This is to avoid messy | 666 // We do not support nested IO message loops. This is to avoid messy |
752 // recursion problems. | 667 // recursion problems. |
753 DCHECK_EQ(1, state_->run_depth) << "Cannot nest an IO message loop!"; | 668 DCHECK_EQ(1, state_->run_depth) << "Cannot nest an IO message loop!"; |
754 | 669 |
755 int timeout = GetCurrentDelay(); | 670 int timeout = GetCurrentDelay(); |
756 if (timeout < 0) // Negative value means no timers waiting. | 671 if (timeout < 0) // Negative value means no timers waiting. |
757 timeout = INFINITE; | 672 timeout = INFINITE; |
758 | 673 |
759 WaitForIOCompletion(timeout, nullptr); | 674 WaitForIOCompletion(timeout, NULL); |
760 } | 675 } |
761 | 676 |
762 bool MessagePumpForIO::WaitForIOCompletion(DWORD timeout, IOHandler* filter) { | 677 bool MessagePumpForIO::WaitForIOCompletion(DWORD timeout, IOHandler* filter) { |
763 IOItem item; | 678 IOItem item; |
764 if (completed_io_.empty() || !MatchCompletedIOItem(filter, &item)) { | 679 if (completed_io_.empty() || !MatchCompletedIOItem(filter, &item)) { |
765 // We have to ask the system for another IO completion. | 680 // We have to ask the system for another IO completion. |
766 if (!GetIOItem(timeout, &item)) | 681 if (!GetIOItem(timeout, &item)) |
767 return false; | 682 return false; |
768 | 683 |
769 if (ProcessInternalIOItem(item)) | 684 if (ProcessInternalIOItem(item)) |
770 return true; | 685 return true; |
771 } | 686 } |
772 | 687 |
773 if (filter && item.handler != filter) { | 688 if (filter && item.handler != filter) { |
774 // Save this item for later | 689 // Save this item for later |
775 completed_io_.push_back(item); | 690 completed_io_.push_back(item); |
776 } else { | 691 } else { |
777 item.handler->OnIOCompleted(item.context, item.bytes_transfered, | 692 item.handler->OnIOCompleted(item.context, item.bytes_transfered, |
778 item.error); | 693 item.error); |
779 } | 694 } |
780 return true; | 695 return true; |
781 } | 696 } |
782 | 697 |
783 // Asks the OS for another IO completion result. | 698 // Asks the OS for another IO completion result. |
784 bool MessagePumpForIO::GetIOItem(DWORD timeout, IOItem* item) { | 699 bool MessagePumpForIO::GetIOItem(DWORD timeout, IOItem* item) { |
785 memset(item, 0, sizeof(*item)); | 700 memset(item, 0, sizeof(*item)); |
786 ULONG_PTR key = reinterpret_cast<ULONG_PTR>(nullptr); | 701 ULONG_PTR key = NULL; |
787 OVERLAPPED* overlapped = nullptr; | 702 OVERLAPPED* overlapped = NULL; |
788 if (!GetQueuedCompletionStatus(port_.Get(), &item->bytes_transfered, &key, | 703 if (!GetQueuedCompletionStatus(port_.Get(), &item->bytes_transfered, &key, |
789 &overlapped, timeout)) { | 704 &overlapped, timeout)) { |
790 if (!overlapped) | 705 if (!overlapped) |
791 return false; // Nothing in the queue. | 706 return false; // Nothing in the queue. |
792 item->error = GetLastError(); | 707 item->error = GetLastError(); |
793 item->bytes_transfered = 0; | 708 item->bytes_transfered = 0; |
794 } | 709 } |
795 | 710 |
796 item->handler = reinterpret_cast<IOHandler*>(key); | 711 item->handler = reinterpret_cast<IOHandler*>(key); |
797 item->context = reinterpret_cast<IOContext*>(overlapped); | 712 item->context = reinterpret_cast<IOContext*>(overlapped); |
(...skipping 19 matching lines...) Expand all Loading... |
817 if (!filter || it->handler == filter) { | 732 if (!filter || it->handler == filter) { |
818 *item = *it; | 733 *item = *it; |
819 completed_io_.erase(it); | 734 completed_io_.erase(it); |
820 return true; | 735 return true; |
821 } | 736 } |
822 } | 737 } |
823 return false; | 738 return false; |
824 } | 739 } |
825 | 740 |
826 } // namespace base | 741 } // namespace base |
OLD | NEW |