| 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 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 207 DCHECK(atom_); | 207 DCHECK(atom_); |
| 208 | 208 |
| 209 message_hwnd_ = CreateWindow(MAKEINTATOM(atom_), 0, 0, 0, 0, 0, 0, | 209 message_hwnd_ = CreateWindow(MAKEINTATOM(atom_), 0, 0, 0, 0, 0, 0, |
| 210 HWND_MESSAGE, 0, instance, 0); | 210 HWND_MESSAGE, 0, instance, 0); |
| 211 DCHECK(message_hwnd_); | 211 DCHECK(message_hwnd_); |
| 212 } | 212 } |
| 213 | 213 |
| 214 void MessagePumpForUI::WaitForWork() { | 214 void MessagePumpForUI::WaitForWork() { |
| 215 // Wait until a message is available, up to the time needed by the timer | 215 // Wait until a message is available, up to the time needed by the timer |
| 216 // manager to fire the next set of timers. | 216 // manager to fire the next set of timers. |
| 217 int delay = GetCurrentDelay(); | 217 int delay; |
| 218 if (delay < 0) // Negative value means no timers waiting. | 218 DWORD wait_flags = MWMO_INPUTAVAILABLE; |
| 219 delay = INFINITE; | |
| 220 | 219 |
| 221 DWORD result; | 220 while ((delay = GetCurrentDelay()) != 0) { |
| 222 result = MsgWaitForMultipleObjectsEx(0, NULL, delay, QS_ALLINPUT, | 221 if (delay < 0) // Negative value means no timers waiting. |
| 223 MWMO_INPUTAVAILABLE); | 222 delay = INFINITE; |
| 224 | 223 |
| 225 if (WAIT_OBJECT_0 == result) { | 224 DWORD result = |
| 226 // A WM_* message is available. | 225 MsgWaitForMultipleObjectsEx(0, NULL, delay, QS_ALLINPUT, wait_flags); |
| 227 // If a parent child relationship exists between windows across threads | 226 |
| 228 // then their thread inputs are implicitly attached. | 227 if (WAIT_OBJECT_0 == result) { |
| 229 // This causes the MsgWaitForMultipleObjectsEx API to return indicating | 228 // A WM_* message is available. |
| 230 // that messages are ready for processing (Specifically, mouse messages | 229 // If a parent child relationship exists between windows across threads |
| 231 // intended for the child window may appear if the child window has | 230 // then their thread inputs are implicitly attached. |
| 232 // capture). | 231 // This causes the MsgWaitForMultipleObjectsEx API to return indicating |
| 233 // The subsequent PeekMessages call may fail to return any messages thus | 232 // that messages are ready for processing (Specifically, mouse messages |
| 234 // causing us to enter a tight loop at times. | 233 // intended for the child window may appear if the child window has |
| 235 // The WaitMessage call below is a workaround to give the child window | 234 // capture). |
| 236 // some time to process its input messages. | 235 // The subsequent PeekMessages call may fail to return any messages thus |
| 237 MSG msg = {0}; | 236 // causing us to enter a tight loop at times. |
| 238 bool has_pending_sent_message = | 237 // The code below is a workaround to give the child window |
| 239 (HIWORD(GetQueueStatus(QS_SENDMESSAGE)) & QS_SENDMESSAGE) != 0; | 238 // some time to process its input messages by looping back to |
| 240 if (!has_pending_sent_message && | 239 // MsgWaitForMultipleObjectsEx above when there are no messages for the |
| 241 !PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)) { | 240 // current thread. |
| 242 WaitMessage(); | 241 MSG msg = {0}; |
| 242 bool has_pending_sent_message = |
| 243 (HIWORD(GetQueueStatus(QS_SENDMESSAGE)) & QS_SENDMESSAGE) != 0; |
| 244 if (has_pending_sent_message || |
| 245 PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)) { |
| 246 return; |
| 247 } |
| 248 |
| 249 // We know there are no more messages for this thread because PeekMessage |
| 250 // has returned false. Reset |wait_flags| so that we wait for a *new* |
| 251 // message. |
| 252 wait_flags = 0; |
| 243 } | 253 } |
| 244 return; | 254 |
| 255 DCHECK_NE(WAIT_FAILED, result) << GetLastError(); |
| 245 } | 256 } |
| 246 | |
| 247 DCHECK_NE(WAIT_FAILED, result) << GetLastError(); | |
| 248 } | 257 } |
| 249 | 258 |
| 250 void MessagePumpForUI::HandleWorkMessage() { | 259 void MessagePumpForUI::HandleWorkMessage() { |
| 251 // If we are being called outside of the context of Run, then don't try to do | 260 // If we are being called outside of the context of Run, then don't try to do |
| 252 // any work. This could correspond to a MessageBox call or something of that | 261 // any work. This could correspond to a MessageBox call or something of that |
| 253 // sort. | 262 // sort. |
| 254 if (!state_) { | 263 if (!state_) { |
| 255 // Since we handled a kMsgHaveWork message, we must still update this flag. | 264 // Since we handled a kMsgHaveWork message, we must still update this flag. |
| 256 InterlockedExchange(&have_work_, 0); | 265 InterlockedExchange(&have_work_, 0); |
| 257 return; | 266 return; |
| (...skipping 375 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 633 | 642 |
| 634 // static | 643 // static |
| 635 MessagePumpForIO::IOHandler* MessagePumpForIO::KeyToHandler( | 644 MessagePumpForIO::IOHandler* MessagePumpForIO::KeyToHandler( |
| 636 ULONG_PTR key, | 645 ULONG_PTR key, |
| 637 bool* has_valid_io_context) { | 646 bool* has_valid_io_context) { |
| 638 *has_valid_io_context = ((key & 1) == 0); | 647 *has_valid_io_context = ((key & 1) == 0); |
| 639 return reinterpret_cast<IOHandler*>(key & ~static_cast<ULONG_PTR>(1)); | 648 return reinterpret_cast<IOHandler*>(key & ~static_cast<ULONG_PTR>(1)); |
| 640 } | 649 } |
| 641 | 650 |
| 642 } // namespace base | 651 } // namespace base |
| OLD | NEW |