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 pointer 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(name) \ |
| 52 reinterpret_cast<decltype(name)*>( \ |
| 53 ::GetProcAddress(::GetModuleHandle(L"user32.dll"), #name)) |
| 54 |
| 55 // Initializes the global pointers to user32 APIs for the API's used in this |
| 56 // file. |
| 57 void InitUser32APIs() { |
| 58 if (g_translate_message) |
| 59 return; |
| 60 |
| 61 g_translate_message = GET_USER32_API(TranslateMessage); |
| 62 CHECK(g_translate_message); |
| 63 |
| 64 g_dispatch_message = GET_USER32_API(DispatchMessageW); |
| 65 CHECK(g_dispatch_message); |
| 66 |
| 67 g_peek_message = GET_USER32_API(PeekMessageW); |
| 68 CHECK(g_peek_message); |
| 69 |
| 70 g_post_message = GET_USER32_API(PostMessageW); |
| 71 CHECK(g_post_message); |
| 72 |
| 73 g_def_window_proc = GET_USER32_API(DefWindowProcW); |
| 74 CHECK(g_def_window_proc); |
| 75 |
| 76 g_post_quit = GET_USER32_API(PostQuitMessage); |
| 77 CHECK(g_post_quit); |
| 78 |
| 79 g_unregister_class = GET_USER32_API(UnregisterClassW); |
| 80 CHECK(g_unregister_class); |
| 81 |
| 82 g_register_class = GET_USER32_API(RegisterClassExW); |
| 83 CHECK(g_register_class); |
| 84 |
| 85 g_create_window_ex = GET_USER32_API(CreateWindowExW); |
| 86 CHECK(g_create_window_ex); |
| 87 |
| 88 g_destroy_window = GET_USER32_API(DestroyWindow); |
| 89 CHECK(g_destroy_window); |
| 90 |
| 91 g_call_msg_filter = GET_USER32_API(CallMsgFilterW); |
| 92 CHECK(g_call_msg_filter); |
| 93 |
| 94 g_get_queue_status = GET_USER32_API(GetQueueStatus); |
| 95 CHECK(g_get_queue_status); |
| 96 |
| 97 g_msg_wait_for_multiple_objects_ex = |
| 98 GET_USER32_API(MsgWaitForMultipleObjectsEx); |
| 99 CHECK(g_msg_wait_for_multiple_objects_ex); |
| 100 |
| 101 g_set_timer = GET_USER32_API(SetTimer); |
| 102 CHECK(g_set_timer); |
| 103 |
| 104 g_kill_timer = GET_USER32_API(KillTimer); |
| 105 CHECK(g_kill_timer); |
| 106 } |
| 107 |
31 } // namespace | 108 } // namespace |
32 | 109 |
33 static const wchar_t kWndClassFormat[] = L"Chrome_MessagePumpWindow_%p"; | 110 static const wchar_t kWndClassFormat[] = L"Chrome_MessagePumpWindow_%p"; |
34 | 111 |
35 // Message sent to get an additional time slice for pumping (processing) another | 112 // Message sent to get an additional time slice for pumping (processing) another |
36 // task (a series of such messages creates a continuous task pump). | 113 // task (a series of such messages creates a continuous task pump). |
37 static const int kMsgHaveWork = WM_USER + 1; | 114 static const int kMsgHaveWork = WM_USER + 1; |
38 | 115 |
39 // The application-defined code passed to the hook procedure. | 116 // The application-defined code passed to the hook procedure. |
40 static const int kMessageFilterCode = 0x5001; | 117 static const int kMessageFilterCode = 0x5001; |
41 | 118 |
42 //----------------------------------------------------------------------------- | 119 //----------------------------------------------------------------------------- |
43 // MessagePumpWin public: | 120 // MessagePumpWin public: |
44 | 121 |
| 122 MessagePumpWin::MessagePumpWin() { |
| 123 InitUser32APIs(); |
| 124 } |
| 125 |
45 void MessagePumpWin::Run(Delegate* delegate) { | 126 void MessagePumpWin::Run(Delegate* delegate) { |
46 RunState s; | 127 RunState s; |
47 s.delegate = delegate; | 128 s.delegate = delegate; |
48 s.should_quit = false; | 129 s.should_quit = false; |
49 s.run_depth = state_ ? state_->run_depth + 1 : 1; | 130 s.run_depth = state_ ? state_->run_depth + 1 : 1; |
50 | 131 |
51 // TODO(stanisc): crbug.com/596190: Remove this code once the bug is fixed. | 132 // TODO(stanisc): crbug.com/596190: Remove this code once the bug is fixed. |
52 s.schedule_work_error_count = 0; | 133 s.schedule_work_error_count = 0; |
53 s.last_schedule_work_error_time = Time(); | 134 s.last_schedule_work_error_time = Time(); |
54 | 135 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 | 170 |
90 //----------------------------------------------------------------------------- | 171 //----------------------------------------------------------------------------- |
91 // MessagePumpForUI public: | 172 // MessagePumpForUI public: |
92 | 173 |
93 MessagePumpForUI::MessagePumpForUI() | 174 MessagePumpForUI::MessagePumpForUI() |
94 : atom_(0) { | 175 : atom_(0) { |
95 InitMessageWnd(); | 176 InitMessageWnd(); |
96 } | 177 } |
97 | 178 |
98 MessagePumpForUI::~MessagePumpForUI() { | 179 MessagePumpForUI::~MessagePumpForUI() { |
99 DestroyWindow(message_hwnd_); | 180 g_destroy_window(message_hwnd_); |
100 UnregisterClass(MAKEINTATOM(atom_), CURRENT_MODULE()); | 181 g_unregister_class(MAKEINTATOM(atom_), CURRENT_MODULE()); |
101 } | 182 } |
102 | 183 |
103 void MessagePumpForUI::ScheduleWork() { | 184 void MessagePumpForUI::ScheduleWork() { |
104 if (InterlockedExchange(&work_state_, HAVE_WORK) != READY) | 185 if (InterlockedExchange(&work_state_, HAVE_WORK) != READY) |
105 return; // Someone else continued the pumping. | 186 return; // Someone else continued the pumping. |
106 | 187 |
107 // Make sure the MessagePump does some work for us. | 188 // Make sure the MessagePump does some work for us. |
108 BOOL ret = PostMessage(message_hwnd_, kMsgHaveWork, | 189 BOOL ret = g_post_message(message_hwnd_, kMsgHaveWork, |
109 reinterpret_cast<WPARAM>(this), 0); | 190 reinterpret_cast<WPARAM>(this), 0); |
110 if (ret) | 191 if (ret) |
111 return; // There was room in the Window Message queue. | 192 return; // There was room in the Window Message queue. |
112 | 193 |
113 // We have failed to insert a have-work message, so there is a chance that we | 194 // 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 | 195 // 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 | 196 // 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. :-( | 197 // 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 | 198 // 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 | 199 // 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 | 200 // recovery. Nested loops are pretty transient (we think), so this will |
(...skipping 19 matching lines...) Expand all Loading... |
139 LRESULT CALLBACK MessagePumpForUI::WndProcThunk( | 220 LRESULT CALLBACK MessagePumpForUI::WndProcThunk( |
140 HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) { | 221 HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) { |
141 switch (message) { | 222 switch (message) { |
142 case kMsgHaveWork: | 223 case kMsgHaveWork: |
143 reinterpret_cast<MessagePumpForUI*>(wparam)->HandleWorkMessage(); | 224 reinterpret_cast<MessagePumpForUI*>(wparam)->HandleWorkMessage(); |
144 break; | 225 break; |
145 case WM_TIMER: | 226 case WM_TIMER: |
146 reinterpret_cast<MessagePumpForUI*>(wparam)->HandleTimerMessage(); | 227 reinterpret_cast<MessagePumpForUI*>(wparam)->HandleTimerMessage(); |
147 break; | 228 break; |
148 } | 229 } |
149 return DefWindowProc(hwnd, message, wparam, lparam); | 230 return g_def_window_proc(hwnd, message, wparam, lparam); |
150 } | 231 } |
151 | 232 |
152 void MessagePumpForUI::DoRunLoop() { | 233 void MessagePumpForUI::DoRunLoop() { |
153 // IF this was just a simple PeekMessage() loop (servicing all possible work | 234 // IF this was just a simple PeekMessage() loop (servicing all possible work |
154 // queues), then Windows would try to achieve the following order according | 235 // queues), then Windows would try to achieve the following order according |
155 // to MSDN documentation about PeekMessage with no filter): | 236 // to MSDN documentation about PeekMessage with no filter): |
156 // * Sent messages | 237 // * Sent messages |
157 // * Posted messages | 238 // * Posted messages |
158 // * Sent messages (again) | 239 // * Sent messages (again) |
159 // * WM_PAINT messages | 240 // * WM_PAINT messages |
(...skipping 20 matching lines...) Expand all Loading... |
180 if (state_->should_quit) | 261 if (state_->should_quit) |
181 break; | 262 break; |
182 | 263 |
183 more_work_is_plausible |= | 264 more_work_is_plausible |= |
184 state_->delegate->DoDelayedWork(&delayed_work_time_); | 265 state_->delegate->DoDelayedWork(&delayed_work_time_); |
185 // If we did not process any delayed work, then we can assume that our | 266 // 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 | 267 // 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, | 268 // 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. | 269 // 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()) | 270 if (more_work_is_plausible && delayed_work_time_.is_null()) |
190 KillTimer(message_hwnd_, reinterpret_cast<UINT_PTR>(this)); | 271 g_kill_timer(message_hwnd_, reinterpret_cast<UINT_PTR>(this)); |
191 if (state_->should_quit) | 272 if (state_->should_quit) |
192 break; | 273 break; |
193 | 274 |
194 if (more_work_is_plausible) | 275 if (more_work_is_plausible) |
195 continue; | 276 continue; |
196 | 277 |
197 more_work_is_plausible = state_->delegate->DoIdleWork(); | 278 more_work_is_plausible = state_->delegate->DoIdleWork(); |
198 if (state_->should_quit) | 279 if (state_->should_quit) |
199 break; | 280 break; |
200 | 281 |
201 if (more_work_is_plausible) | 282 if (more_work_is_plausible) |
202 continue; | 283 continue; |
203 | 284 |
204 WaitForWork(); // Wait (sleep) until we have work to do again. | 285 WaitForWork(); // Wait (sleep) until we have work to do again. |
205 } | 286 } |
206 } | 287 } |
207 | 288 |
208 void MessagePumpForUI::InitMessageWnd() { | 289 void MessagePumpForUI::InitMessageWnd() { |
209 // Generate a unique window class name. | 290 // Generate a unique window class name. |
210 string16 class_name = StringPrintf(kWndClassFormat, this); | 291 string16 class_name = StringPrintf(kWndClassFormat, this); |
211 | 292 |
212 HINSTANCE instance = CURRENT_MODULE(); | 293 HINSTANCE instance = CURRENT_MODULE(); |
213 WNDCLASSEX wc = {0}; | 294 WNDCLASSEX wc = {0}; |
214 wc.cbSize = sizeof(wc); | 295 wc.cbSize = sizeof(wc); |
215 wc.lpfnWndProc = base::win::WrappedWindowProc<WndProcThunk>; | 296 wc.lpfnWndProc = base::win::WrappedWindowProc<WndProcThunk>; |
216 wc.hInstance = instance; | 297 wc.hInstance = instance; |
217 wc.lpszClassName = class_name.c_str(); | 298 wc.lpszClassName = class_name.c_str(); |
218 atom_ = RegisterClassEx(&wc); | 299 atom_ = g_register_class(&wc); |
219 DCHECK(atom_); | 300 DCHECK(atom_); |
220 | 301 |
221 message_hwnd_ = CreateWindow(MAKEINTATOM(atom_), 0, 0, 0, 0, 0, 0, | 302 message_hwnd_ = g_create_window_ex(0, MAKEINTATOM(atom_), 0, 0, 0, 0, 0, 0, |
222 HWND_MESSAGE, 0, instance, 0); | 303 HWND_MESSAGE, 0, instance, 0); |
223 DCHECK(message_hwnd_); | 304 DCHECK(message_hwnd_); |
224 } | 305 } |
225 | 306 |
226 void MessagePumpForUI::WaitForWork() { | 307 void MessagePumpForUI::WaitForWork() { |
227 // Wait until a message is available, up to the time needed by the timer | 308 // Wait until a message is available, up to the time needed by the timer |
228 // manager to fire the next set of timers. | 309 // manager to fire the next set of timers. |
229 int delay; | 310 int delay; |
230 DWORD wait_flags = MWMO_INPUTAVAILABLE; | 311 DWORD wait_flags = MWMO_INPUTAVAILABLE; |
231 | 312 |
232 while ((delay = GetCurrentDelay()) != 0) { | 313 while ((delay = GetCurrentDelay()) != 0) { |
233 if (delay < 0) // Negative value means no timers waiting. | 314 if (delay < 0) // Negative value means no timers waiting. |
234 delay = INFINITE; | 315 delay = INFINITE; |
235 | 316 |
236 DWORD result = | 317 DWORD result = g_msg_wait_for_multiple_objects_ex(0, NULL, delay, |
237 MsgWaitForMultipleObjectsEx(0, NULL, delay, QS_ALLINPUT, wait_flags); | 318 QS_ALLINPUT, wait_flags); |
238 | 319 |
239 if (WAIT_OBJECT_0 == result) { | 320 if (WAIT_OBJECT_0 == result) { |
240 // A WM_* message is available. | 321 // A WM_* message is available. |
241 // If a parent child relationship exists between windows across threads | 322 // If a parent child relationship exists between windows across threads |
242 // then their thread inputs are implicitly attached. | 323 // then their thread inputs are implicitly attached. |
243 // This causes the MsgWaitForMultipleObjectsEx API to return indicating | 324 // This causes the MsgWaitForMultipleObjectsEx API to return indicating |
244 // that messages are ready for processing (Specifically, mouse messages | 325 // that messages are ready for processing (Specifically, mouse messages |
245 // intended for the child window may appear if the child window has | 326 // intended for the child window may appear if the child window has |
246 // capture). | 327 // capture). |
247 // The subsequent PeekMessages call may fail to return any messages thus | 328 // The subsequent PeekMessages call may fail to return any messages thus |
248 // causing us to enter a tight loop at times. | 329 // causing us to enter a tight loop at times. |
249 // The code below is a workaround to give the child window | 330 // The code below is a workaround to give the child window |
250 // some time to process its input messages by looping back to | 331 // some time to process its input messages by looping back to |
251 // MsgWaitForMultipleObjectsEx above when there are no messages for the | 332 // MsgWaitForMultipleObjectsEx above when there are no messages for the |
252 // current thread. | 333 // current thread. |
253 MSG msg = {0}; | 334 MSG msg = {0}; |
254 bool has_pending_sent_message = | 335 bool has_pending_sent_message = |
255 (HIWORD(GetQueueStatus(QS_SENDMESSAGE)) & QS_SENDMESSAGE) != 0; | 336 (HIWORD(g_get_queue_status(QS_SENDMESSAGE)) & QS_SENDMESSAGE) != 0; |
256 if (has_pending_sent_message || | 337 if (has_pending_sent_message || |
257 PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)) { | 338 g_peek_message(&msg, NULL, 0, 0, PM_NOREMOVE)) { |
258 return; | 339 return; |
259 } | 340 } |
260 | 341 |
261 // We know there are no more messages for this thread because PeekMessage | 342 // 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* | 343 // has returned false. Reset |wait_flags| so that we wait for a *new* |
263 // message. | 344 // message. |
264 wait_flags = 0; | 345 wait_flags = 0; |
265 } | 346 } |
266 | 347 |
267 DCHECK_NE(WAIT_FAILED, result) << GetLastError(); | 348 DCHECK_NE(WAIT_FAILED, result) << GetLastError(); |
(...skipping 17 matching lines...) Expand all Loading... |
285 | 366 |
286 // Now give the delegate a chance to do some work. He'll let us know if he | 367 // Now give the delegate a chance to do some work. He'll let us know if he |
287 // needs to do more work. | 368 // needs to do more work. |
288 if (state_->delegate->DoWork()) | 369 if (state_->delegate->DoWork()) |
289 ScheduleWork(); | 370 ScheduleWork(); |
290 state_->delegate->DoDelayedWork(&delayed_work_time_); | 371 state_->delegate->DoDelayedWork(&delayed_work_time_); |
291 RescheduleTimer(); | 372 RescheduleTimer(); |
292 } | 373 } |
293 | 374 |
294 void MessagePumpForUI::HandleTimerMessage() { | 375 void MessagePumpForUI::HandleTimerMessage() { |
295 KillTimer(message_hwnd_, reinterpret_cast<UINT_PTR>(this)); | 376 g_kill_timer(message_hwnd_, reinterpret_cast<UINT_PTR>(this)); |
296 | 377 |
297 // If we are being called outside of the context of Run, then don't do | 378 // 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 | 379 // anything. This could correspond to a MessageBox call or something of |
299 // that sort. | 380 // that sort. |
300 if (!state_) | 381 if (!state_) |
301 return; | 382 return; |
302 | 383 |
303 state_->delegate->DoDelayedWork(&delayed_work_time_); | 384 state_->delegate->DoDelayedWork(&delayed_work_time_); |
304 RescheduleTimer(); | 385 RescheduleTimer(); |
305 } | 386 } |
(...skipping 24 matching lines...) Expand all Loading... |
330 int delay_msec = GetCurrentDelay(); | 411 int delay_msec = GetCurrentDelay(); |
331 DCHECK_GE(delay_msec, 0); | 412 DCHECK_GE(delay_msec, 0); |
332 if (delay_msec == 0) { | 413 if (delay_msec == 0) { |
333 ScheduleWork(); | 414 ScheduleWork(); |
334 } else { | 415 } else { |
335 if (delay_msec < USER_TIMER_MINIMUM) | 416 if (delay_msec < USER_TIMER_MINIMUM) |
336 delay_msec = USER_TIMER_MINIMUM; | 417 delay_msec = USER_TIMER_MINIMUM; |
337 | 418 |
338 // Create a WM_TIMER event that will wake us up to check for any pending | 419 // 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). | 420 // timers (in case we are running within a nested, external sub-pump). |
340 BOOL ret = SetTimer(message_hwnd_, reinterpret_cast<UINT_PTR>(this), | 421 BOOL ret = g_set_timer(message_hwnd_, reinterpret_cast<UINT_PTR>(this), |
341 delay_msec, NULL); | 422 delay_msec, NULL); |
342 if (ret) | 423 if (ret) |
343 return; | 424 return; |
344 // If we can't set timers, we are in big trouble... but cross our fingers | 425 // If we can't set timers, we are in big trouble... but cross our fingers |
345 // for now. | 426 // for now. |
346 // TODO(jar): If we don't see this error, use a CHECK() here instead. | 427 // TODO(jar): If we don't see this error, use a CHECK() here instead. |
347 UMA_HISTOGRAM_ENUMERATION("Chrome.MessageLoopProblem", SET_TIMER_ERROR, | 428 UMA_HISTOGRAM_ENUMERATION("Chrome.MessageLoopProblem", SET_TIMER_ERROR, |
348 MESSAGE_LOOP_PROBLEM_MAX); | 429 MESSAGE_LOOP_PROBLEM_MAX); |
349 } | 430 } |
350 } | 431 } |
351 | 432 |
352 bool MessagePumpForUI::ProcessNextWindowsMessage() { | 433 bool MessagePumpForUI::ProcessNextWindowsMessage() { |
353 // If there are sent messages in the queue then PeekMessage internally | 434 // If there are sent messages in the queue then PeekMessage internally |
354 // dispatches the message and returns false. We return true in this | 435 // dispatches the message and returns false. We return true in this |
355 // case to ensure that the message loop peeks again instead of calling | 436 // case to ensure that the message loop peeks again instead of calling |
356 // MsgWaitForMultipleObjectsEx again. | 437 // MsgWaitForMultipleObjectsEx again. |
357 bool sent_messages_in_queue = false; | 438 bool sent_messages_in_queue = false; |
358 DWORD queue_status = GetQueueStatus(QS_SENDMESSAGE); | 439 DWORD queue_status = g_get_queue_status(QS_SENDMESSAGE); |
359 if (HIWORD(queue_status) & QS_SENDMESSAGE) | 440 if (HIWORD(queue_status) & QS_SENDMESSAGE) |
360 sent_messages_in_queue = true; | 441 sent_messages_in_queue = true; |
361 | 442 |
362 MSG msg; | 443 MSG msg; |
363 if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) != FALSE) | 444 if (g_peek_message(&msg, NULL, 0, 0, PM_REMOVE) != FALSE) |
364 return ProcessMessageHelper(msg); | 445 return ProcessMessageHelper(msg); |
365 | 446 |
366 return sent_messages_in_queue; | 447 return sent_messages_in_queue; |
367 } | 448 } |
368 | 449 |
369 bool MessagePumpForUI::ProcessMessageHelper(const MSG& msg) { | 450 bool MessagePumpForUI::ProcessMessageHelper(const MSG& msg) { |
370 TRACE_EVENT1("base", "MessagePumpForUI::ProcessMessageHelper", | 451 TRACE_EVENT1("base", "MessagePumpForUI::ProcessMessageHelper", |
371 "message", msg.message); | 452 "message", msg.message); |
372 if (WM_QUIT == msg.message) { | 453 if (WM_QUIT == msg.message) { |
373 // Repost the QUIT message so that it will be retrieved by the primary | 454 // Repost the QUIT message so that it will be retrieved by the primary |
374 // GetMessage() loop. | 455 // GetMessage() loop. |
375 state_->should_quit = true; | 456 state_->should_quit = true; |
376 PostQuitMessage(static_cast<int>(msg.wParam)); | 457 g_post_quit(static_cast<int>(msg.wParam)); |
377 return false; | 458 return false; |
378 } | 459 } |
379 | 460 |
380 // While running our main message pump, we discard kMsgHaveWork messages. | 461 // While running our main message pump, we discard kMsgHaveWork messages. |
381 if (msg.message == kMsgHaveWork && msg.hwnd == message_hwnd_) | 462 if (msg.message == kMsgHaveWork && msg.hwnd == message_hwnd_) |
382 return ProcessPumpReplacementMessage(); | 463 return ProcessPumpReplacementMessage(); |
383 | 464 |
384 if (CallMsgFilter(const_cast<MSG*>(&msg), kMessageFilterCode)) | 465 if (g_call_msg_filter(const_cast<MSG*>(&msg), kMessageFilterCode)) |
385 return true; | 466 return true; |
386 | 467 |
387 TranslateMessage(&msg); | 468 g_translate_message(&msg); |
388 DispatchMessage(&msg); | 469 g_dispatch_message(&msg); |
389 | 470 |
390 return true; | 471 return true; |
391 } | 472 } |
392 | 473 |
393 bool MessagePumpForUI::ProcessPumpReplacementMessage() { | 474 bool MessagePumpForUI::ProcessPumpReplacementMessage() { |
394 // When we encounter a kMsgHaveWork message, this method is called to peek and | 475 // 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- | 476 // 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 | 477 // 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 | 478 // 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 | 479 // a kMsgHaveWork to be pending, then resets the |have_work_| flag (allowing a |
399 // replacement kMsgHaveWork to possibly be posted), and finally dispatches | 480 // replacement kMsgHaveWork to possibly be posted), and finally dispatches |
400 // that peeked replacement. Note that the re-post of kMsgHaveWork may be | 481 // that peeked replacement. Note that the re-post of kMsgHaveWork may be |
401 // asynchronous to this thread!! | 482 // asynchronous to this thread!! |
402 | 483 |
403 MSG msg; | 484 MSG msg; |
404 const bool have_message = PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) != FALSE; | 485 const bool have_message = |
| 486 g_peek_message(&msg, NULL, 0, 0, PM_REMOVE) != FALSE; |
405 | 487 |
406 // Expect no message or a message different than kMsgHaveWork. | 488 // Expect no message or a message different than kMsgHaveWork. |
407 DCHECK(!have_message || kMsgHaveWork != msg.message || | 489 DCHECK(!have_message || kMsgHaveWork != msg.message || |
408 msg.hwnd != message_hwnd_); | 490 msg.hwnd != message_hwnd_); |
409 | 491 |
410 // Since we discarded a kMsgHaveWork message, we must update the flag. | 492 // Since we discarded a kMsgHaveWork message, we must update the flag. |
411 int old_work_state_ = InterlockedExchange(&work_state_, READY); | 493 int old_work_state_ = InterlockedExchange(&work_state_, READY); |
412 DCHECK_EQ(HAVE_WORK, old_work_state_); | 494 DCHECK_EQ(HAVE_WORK, old_work_state_); |
413 | 495 |
414 // We don't need a special time slice if we didn't have_message to process. | 496 // 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. | 606 if (delay < 0) // Negative value means no timers waiting. |
525 delay = INFINITE; | 607 delay = INFINITE; |
526 | 608 |
527 // TODO(stanisc): crbug.com/596190: Preserve for crash dump analysis. | 609 // TODO(stanisc): crbug.com/596190: Preserve for crash dump analysis. |
528 // Remove this when the bug is fixed. | 610 // Remove this when the bug is fixed. |
529 TimeTicks wait_for_work_timeticks = TimeTicks::Now(); | 611 TimeTicks wait_for_work_timeticks = TimeTicks::Now(); |
530 debug::Alias(&wait_for_work_timeticks); | 612 debug::Alias(&wait_for_work_timeticks); |
531 debug::Alias(&delay); | 613 debug::Alias(&delay); |
532 | 614 |
533 DWORD result = | 615 DWORD result = |
534 MsgWaitForMultipleObjectsEx(1, &event_, delay, QS_ALLINPUT, 0); | 616 g_msg_wait_for_multiple_objects_ex(1, &event_, delay, QS_ALLINPUT, 0); |
535 DCHECK_NE(WAIT_FAILED, result) << GetLastError(); | 617 DCHECK_NE(WAIT_FAILED, result) << GetLastError(); |
536 if (result != WAIT_TIMEOUT) { | 618 if (result != WAIT_TIMEOUT) { |
537 // Either work or message available. | 619 // Either work or message available. |
538 return; | 620 return; |
539 } | 621 } |
540 } | 622 } |
541 } | 623 } |
542 | 624 |
543 bool MessagePumpForGpu::ProcessNextMessage() { | 625 bool MessagePumpForGpu::ProcessNextMessage() { |
544 MSG msg; | 626 MSG msg; |
545 if (!PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE)) | 627 if (!g_peek_message(&msg, nullptr, 0, 0, PM_REMOVE)) |
546 return false; | 628 return false; |
547 | 629 |
548 if (msg.message == WM_QUIT) { | 630 if (msg.message == WM_QUIT) { |
549 // Repost the QUIT message so that it will be retrieved by the primary | 631 // Repost the QUIT message so that it will be retrieved by the primary |
550 // GetMessage() loop. | 632 // GetMessage() loop. |
551 state_->should_quit = true; | 633 state_->should_quit = true; |
552 PostQuitMessage(static_cast<int>(msg.wParam)); | 634 g_post_quit(static_cast<int>(msg.wParam)); |
553 return false; | 635 return false; |
554 } | 636 } |
555 | 637 |
556 if (!CallMsgFilter(const_cast<MSG*>(&msg), kMessageFilterCode)) { | 638 if (!g_call_msg_filter(const_cast<MSG*>(&msg), kMessageFilterCode)) { |
557 TranslateMessage(&msg); | 639 g_translate_message(&msg); |
558 DispatchMessage(&msg); | 640 g_dispatch_message(&msg); |
559 } | 641 } |
560 | 642 |
561 return true; | 643 return true; |
562 } | 644 } |
563 | 645 |
564 //----------------------------------------------------------------------------- | 646 //----------------------------------------------------------------------------- |
565 // MessagePumpForIO public: | 647 // MessagePumpForIO public: |
566 | 648 |
567 MessagePumpForIO::IOContext::IOContext() { | 649 MessagePumpForIO::IOContext::IOContext() { |
568 memset(&overlapped, 0, sizeof(overlapped)); | 650 memset(&overlapped, 0, sizeof(overlapped)); |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
732 if (!filter || it->handler == filter) { | 814 if (!filter || it->handler == filter) { |
733 *item = *it; | 815 *item = *it; |
734 completed_io_.erase(it); | 816 completed_io_.erase(it); |
735 return true; | 817 return true; |
736 } | 818 } |
737 } | 819 } |
738 return false; | 820 return false; |
739 } | 821 } |
740 | 822 |
741 } // namespace base | 823 } // namespace base |
OLD | NEW |