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