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