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