Bug 962140 - Use PeekMessage() instead of GetMessage() in KeyboardLayout.cpp. r=jimm, a=lsblakk
authorMasayuki Nakano <masayuki@d-toybox.com>
Sat, 01 Feb 2014 01:31:01 +0900
changeset 176283 16a356b130577b4cbb7996b1ca870d912a8a9dee
parent 176282 ce8d8b1b97a8213334d77e66b56bb98968e670ba
child 176284 708e30a08b94b68cdf37bfa255d714c05b9688df
push id445
push userffxbld
push dateMon, 10 Mar 2014 22:05:19 +0000
treeherdermozilla-release@dc38b741b04e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjimm, lsblakk
bugs962140
milestone28.0
Bug 962140 - Use PeekMessage() instead of GetMessage() in KeyboardLayout.cpp. r=jimm, a=lsblakk
widget/windows/KeyboardLayout.cpp
widget/windows/KeyboardLayout.h
--- a/widget/windows/KeyboardLayout.cpp
+++ b/widget/windows/KeyboardLayout.cpp
@@ -743,34 +743,32 @@ NativeKey::IsFollowedByCharMessage() con
   if (mFakeCharMsgs) {
     nextMsg = mFakeCharMsgs->ElementAt(0).GetCharMsg(mMsg.hwnd);
   } else {
     if (!WinUtils::PeekMessage(&nextMsg, mMsg.hwnd, WM_KEYFIRST, WM_KEYLAST,
                                PM_NOREMOVE | PM_NOYIELD)) {
       return false;
     }
   }
-  return (nextMsg.message == WM_CHAR ||
-          nextMsg.message == WM_SYSCHAR ||
-          nextMsg.message == WM_DEADCHAR);
+  return IsCharMessage(nextMsg);
 }
 
 bool
 NativeKey::IsFollowedByDeadCharMessage() const
 {
   MSG nextMsg;
   if (mFakeCharMsgs) {
     nextMsg = mFakeCharMsgs->ElementAt(0).GetCharMsg(mMsg.hwnd);
   } else {
     if (!WinUtils::PeekMessage(&nextMsg, mMsg.hwnd, WM_KEYFIRST, WM_KEYLAST,
                                PM_NOREMOVE | PM_NOYIELD)) {
       return false;
     }
   }
-  return (nextMsg.message == WM_DEADCHAR);
+  return IsDeadCharMessage(nextMsg);
 }
 
 bool
 NativeKey::IsIMEDoingKakuteiUndo() const
 {
   // Following message pattern is caused by "Kakutei-Undo" of ATOK or WXG:
   // ---------------------------------------------------------------------------
   // WM_KEYDOWN              * n (wParam = VK_BACK, lParam = 0x1)
@@ -971,17 +969,17 @@ NativeKey::DispatchKeyEvent(WidgetKeyboa
   }
 
   return (mWidget->DispatchKeyboardEvent(&aKeyEvent) || mWidget->Destroyed());
 }
 
 bool
 NativeKey::HandleKeyDownMessage(bool* aEventDispatched) const
 {
-  MOZ_ASSERT(mMsg.message == WM_KEYDOWN || mMsg.message == WM_SYSKEYDOWN);
+  MOZ_ASSERT(IsKeyDownMessage());
 
   if (aEventDispatched) {
     *aEventDispatched = false;
   }
 
   bool defaultPrevented = false;
   if (mFakeCharMsgs ||
       !RedirectedKeyDownMessageManager::IsRedirectedMessage(mMsg)) {
@@ -1095,19 +1093,18 @@ NativeKey::HandleKeyDownMessage(bool* aE
 
   return DispatchKeyPressEventsWithKeyboardLayout();
 }
 
 bool
 NativeKey::HandleCharMessage(const MSG& aCharMsg,
                              bool* aEventDispatched) const
 {
-  MOZ_ASSERT(mMsg.message == WM_KEYDOWN || mMsg.message == WM_SYSKEYDOWN ||
-             mMsg.message == WM_CHAR || mMsg.message == WM_SYSCHAR);
-  MOZ_ASSERT(aCharMsg.message == WM_CHAR || aCharMsg.message == WM_SYSCHAR);
+  MOZ_ASSERT(IsKeyDownMessage() || IsPrintableCharMessage(mMsg));
+  MOZ_ASSERT(IsPrintableCharMessage(aCharMsg.message));
 
   if (aEventDispatched) {
     *aEventDispatched = false;
   }
 
   // Alt+Space key is handled by OS, we shouldn't touch it.
   if (mModKeyState.IsAlt() && !mModKeyState.IsControl() &&
       mVirtualKeyCode == VK_SPACE) {
@@ -1207,17 +1204,17 @@ NativeKey::HandleCharMessage(const MSG& 
     *aEventDispatched = true;
   }
   return DispatchKeyEvent(keypressEvent, &aCharMsg);
 }
 
 bool
 NativeKey::HandleKeyUpMessage(bool* aEventDispatched) const
 {
-  MOZ_ASSERT(mMsg.message == WM_KEYUP || mMsg.message == WM_SYSKEYUP);
+  MOZ_ASSERT(IsKeyUpMessage());
 
   if (aEventDispatched) {
     *aEventDispatched = false;
   }
 
   // Ignore [shift+]alt+space so the OS can handle it.
   if (mModKeyState.IsAlt() && !mModKeyState.IsControl() &&
       mVirtualKeyCode == VK_SPACE) {
@@ -1230,17 +1227,17 @@ NativeKey::HandleKeyUpMessage(bool* aEve
     *aEventDispatched = true;
   }
   return DispatchKeyEvent(keyupEvent, &mMsg);
 }
 
 bool
 NativeKey::NeedsToHandleWithoutFollowingCharMessages() const
 {
-  MOZ_ASSERT(mMsg.message == WM_KEYDOWN || mMsg.message == WM_SYSKEYDOWN);
+  MOZ_ASSERT(IsKeyDownMessage());
 
   // Enter and backspace are always handled here to avoid for example the
   // confusion between ctrl-enter and ctrl-J.
   if (mDOMKeyCode == NS_VK_RETURN || mDOMKeyCode == NS_VK_BACK) {
     return true;
   }
 
   // If any modifier keys which may cause printable keys becoming non-printable
@@ -1269,19 +1266,19 @@ NativeKey::RemoveFollowingCharMessage() 
   if (mFakeCharMsgs) {
     MOZ_ASSERT(!mFakeCharMsgs->ElementAt(0).mConsumed,
       "Doesn't assume that it's used for removing two or more char messages");
     mFakeCharMsgs->ElementAt(0).mConsumed = true;
     return mFakeCharMsgs->ElementAt(0).GetCharMsg(mMsg.hwnd);
   }
 
   MSG msg;
-  if (!WinUtils::GetMessage(&msg, mMsg.hwnd, WM_KEYFIRST, WM_KEYLAST) ||
-      !(msg.message == WM_CHAR || msg.message == WM_SYSCHAR ||
-        msg.message == WM_DEADCHAR)) {
+  if (!WinUtils::PeekMessage(&msg, mMsg.hwnd, WM_KEYFIRST, WM_KEYLAST,
+                             PM_REMOVE | PM_NOYIELD) ||
+      !IsCharMessage(msg)) {
     MOZ_CRASH("We lost the following char message");
   }
 
   return msg;
 }
 
 bool
 NativeKey::RemoveMessageAndDispatchPluginEvent(UINT aFirstMsg,
@@ -1301,29 +1298,30 @@ NativeKey::RemoveMessageAndDispatchPlugi
       }
       fakeCharMsg.mConsumed = true;
       msg = fakeMsg;
       found = true;
       break;
     }
     MOZ_ASSERT(found, "Fake char message must be found");
   } else {
-    WinUtils::GetMessage(&msg, mMsg.hwnd, aFirstMsg, aLastMsg);
+    WinUtils::PeekMessage(&msg, mMsg.hwnd, aFirstMsg, aLastMsg,
+                          PM_REMOVE | PM_NOYIELD);
   }
   if (mWidget->Destroyed()) {
     MOZ_CRASH("NativeKey tries to dispatch a plugin event on destroyed widget");
   }
   mWidget->DispatchPluginEvent(msg);
   return mWidget->Destroyed();
 }
 
 bool
 NativeKey::DispatchPluginEventsAndDiscardsCharMessages() const
 {
-  MOZ_ASSERT(mMsg.message == WM_KEYDOWN || mMsg.message == WM_SYSKEYDOWN);
+  MOZ_ASSERT(IsKeyDownMessage());
 
   if (mFakeCharMsgs) {
     for (uint32_t i = 0; i < mFakeCharMsgs->Length(); i++) {
       if (RemoveMessageAndDispatchPluginEvent(WM_KEYFIRST, WM_KEYLAST)) {
         return true;
       }
     }
     return false;
@@ -1333,19 +1331,17 @@ NativeKey::DispatchPluginEventsAndDiscar
   // They can be more than one because of:
   //  * Dead-keys not pairing with base character
   //  * Some keyboard layouts may map up to 4 characters to the single key
   bool anyCharMessagesRemoved = true;
   MSG msg;
   bool gotMsg =
     WinUtils::PeekMessage(&msg, mMsg.hwnd, WM_KEYFIRST, WM_KEYLAST,
                           PM_NOREMOVE | PM_NOYIELD);
-  while (gotMsg &&
-         (msg.message == WM_CHAR || msg.message == WM_SYSCHAR ||
-          msg.message == WM_DEADCHAR)) {
+  while (gotMsg && IsCharMessage(msg)) {
     if (RemoveMessageAndDispatchPluginEvent(WM_KEYFIRST, WM_KEYLAST)) {
       return true;
     }
     anyCharMessagesRemoved = true;
     gotMsg = WinUtils::PeekMessage(&msg, mMsg.hwnd, WM_KEYFIRST, WM_KEYLAST,
                                    PM_NOREMOVE | PM_NOYIELD);
   }
 
@@ -1356,17 +1352,17 @@ NativeKey::DispatchPluginEventsAndDiscar
   }
 
   return false;
 }
 
 bool
 NativeKey::DispatchKeyPressEventsWithKeyboardLayout() const
 {
-  MOZ_ASSERT(mMsg.message == WM_KEYDOWN || mMsg.message == WM_SYSKEYDOWN);
+  MOZ_ASSERT(IsKeyDownMessage());
   MOZ_ASSERT(!mIsDeadKey);
 
   KeyboardLayout* keyboardLayout = KeyboardLayout::GetInstance();
 
   UniCharsAndModifiers inputtingChars(mCommittedCharsAndModifiers);
   UniCharsAndModifiers shiftedChars;
   UniCharsAndModifiers unshiftedChars;
   uint32_t shiftedLatinChar = 0;
@@ -1520,21 +1516,21 @@ NativeKey::DispatchKeyPressEventsWithKey
   }
 
   return defaultPrevented;
 }
 
 bool
 NativeKey::DispatchKeyPressEventForFollowingCharMessage() const
 {
-  MOZ_ASSERT(mMsg.message == WM_KEYDOWN || mMsg.message == WM_SYSKEYDOWN);
+  MOZ_ASSERT(IsKeyDownMessage());
 
   MSG msg = RemoveFollowingCharMessage();
   if (mFakeCharMsgs) {
-    if (msg.message == WM_DEADCHAR) {
+    if (IsDeadCharMessage(msg)) {
       return false;
     }
 #ifdef DEBUG
     if (mIsPrintableKey) {
       nsPrintfCString log(
         "mOriginalVirtualKeyCode=0x%02X, mCommittedCharsAndModifiers={ "
         "mChars=[ 0x%04X, 0x%04X, 0x%04X, 0x%04X, 0x%04X ], mLength=%d }, "
         "wParam=0x%04X",
@@ -1553,27 +1549,27 @@ NativeKey::DispatchKeyPressEventForFollo
         NS_ERROR(log.get());
         NS_ABORT();
       }
     }
 #endif // #ifdef DEBUG
     return HandleCharMessage(msg);
   }
 
-  if (msg.message == WM_DEADCHAR) {
+  if (IsDeadCharMessage(msg)) {
     if (!mWidget->PluginHasFocus()) {
       return false;
     }
     return (mWidget->DispatchPluginEvent(msg) || mWidget->Destroyed());
   }
 
   bool defaultPrevented = HandleCharMessage(msg);
   // If a syschar keypress wasn't processed, Windows may want to
   // handle it to activate a native menu.
-  if (!defaultPrevented && msg.message == WM_SYSCHAR) {
+  if (!defaultPrevented && IsSysCharMessage(msg)) {
     ::DefWindowProcW(msg.hwnd, msg.message, msg.wParam, msg.lParam);
   }
   return defaultPrevented;
 }
 
 /*****************************************************************************
  * mozilla::widget::KeyboardLayout
  *****************************************************************************/
@@ -2634,15 +2630,16 @@ RedirectedKeyDownMessageManager::IsRedir
 // static
 void
 RedirectedKeyDownMessageManager::RemoveNextCharMessage(HWND aWnd)
 {
   MSG msg;
   if (WinUtils::PeekMessage(&msg, aWnd, WM_KEYFIRST, WM_KEYLAST,
                             PM_NOREMOVE | PM_NOYIELD) &&
       (msg.message == WM_CHAR || msg.message == WM_SYSCHAR)) {
-    WinUtils::GetMessage(&msg, aWnd, msg.message, msg.message);
+    WinUtils::PeekMessage(&msg, aWnd, msg.message, msg.message,
+                          PM_REMOVE | PM_NOYIELD);
   }
 }
 
 } // namespace widget
 } // namespace mozilla
 
--- a/widget/windows/KeyboardLayout.h
+++ b/widget/windows/KeyboardLayout.h
@@ -333,16 +333,52 @@ private:
    * Returns true if the widget is destoyed.  Otherwise, false.
    */
   bool RemoveMessageAndDispatchPluginEvent(UINT aFirstMsg, UINT aLastMsg) const;
 
   bool IsKeyDownMessage() const
   {
     return (mMsg.message == WM_KEYDOWN || mMsg.message == WM_SYSKEYDOWN);
   }
+  bool IsKeyUpMessage() const
+  {
+    return (mMsg.message == WM_KEYUP || mMsg.message == WM_SYSKEYUP);
+  }
+  bool IsPrintableCharMessage(const MSG& aMSG) const
+  {
+    return IsPrintableCharMessage(aMSG.message);
+  }
+  bool IsPrintableCharMessage(UINT aMessage) const
+  {
+    return (aMessage == WM_CHAR || aMessage == WM_SYSCHAR);
+  }
+  bool IsCharMessage(const MSG& aMSG) const
+  {
+    return IsCharMessage(aMSG.message);
+  }
+  bool IsCharMessage(UINT aMessage) const
+  {
+    return (IsPrintableCharMessage(aMessage) || IsDeadCharMessage(aMessage));
+  }
+  bool IsDeadCharMessage(const MSG& aMSG) const
+  {
+    return IsDeadCharMessage(aMSG.message);
+  }
+  bool IsDeadCharMessage(UINT aMessage) const
+  {
+    return (aMessage == WM_DEADCHAR || aMessage == WM_SYSDEADCHAR);
+  }
+  bool IsSysCharMessage(const MSG& aMSG) const
+  {
+    return IsSysCharMessage(aMSG.message);
+  }
+  bool IsSysCharMessage(UINT aMessage) const
+  {
+    return (aMessage == WM_SYSCHAR || aMessage == WM_SYSDEADCHAR);
+  }
   bool IsFollowedByCharMessage() const;
   bool IsFollowedByDeadCharMessage() const;
   MSG RemoveFollowingCharMessage() const;
 
   /**
    * Wraps MapVirtualKeyEx() with MAPVK_VSC_TO_VK.
    */
   uint8_t ComputeVirtualKeyCodeFromScanCode() const;