Bug 855975 part.12 Move special keypress event handling from nsWindow::OnKeyDown() to widget::NativeKey::DispatchKeyPressEventsAndDiscardsCharMessages r=jimm
authorMasayuki Nakano <masayuki@d-toybox.com>
Wed, 29 May 2013 15:34:48 +0900
changeset 133239 d3aebac825b9c3af4c91c474c8aef972d896cea6
parent 133238 658bfe5bb0eac39bfb5233018f6a585635ca8441
child 133240 fef076fed8ab620052e9d4474c93a7162958cdb3
push id28671
push usermasayuki@d-toybox.com
push dateWed, 29 May 2013 06:35:17 +0000
treeherdermozilla-inbound@57461a161f93 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjimm
bugs855975
milestone24.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 855975 part.12 Move special keypress event handling from nsWindow::OnKeyDown() to widget::NativeKey::DispatchKeyPressEventsAndDiscardsCharMessages r=jimm
widget/windows/KeyboardLayout.cpp
widget/windows/KeyboardLayout.h
widget/windows/WinIMEHandler.cpp
widget/windows/WinIMEHandler.h
widget/windows/WinUtils.cpp
widget/windows/WinUtils.h
widget/windows/nsWindow.cpp
widget/windows/nsWindow.h
widget/windows/nsWindowDefs.h
--- a/widget/windows/KeyboardLayout.cpp
+++ b/widget/windows/KeyboardLayout.cpp
@@ -548,16 +548,50 @@ NativeKey::NativeKey(nsWindowBase* aWidg
     keyboardLayout->ConvertNativeKeyCodeToKeyNameIndex(mOriginalVirtualKeyCode);
 
   keyboardLayout->InitNativeKey(*this, mModKeyState);
 
   mIsDeadKey = keyboardLayout->IsDeadKey(mOriginalVirtualKeyCode, mModKeyState);
   mIsPrintableKey = KeyboardLayout::IsPrintableCharKey(mOriginalVirtualKeyCode);
 }
 
+bool
+NativeKey::IsIMEDoingKakuteiUndo() const
+{
+  // Following message pattern is caused by "Kakutei-Undo" of ATOK or WXG:
+  // ---------------------------------------------------------------------------
+  // WM_KEYDOWN              * n (wParam = VK_BACK, lParam = 0x1)
+  // WM_KEYUP                * 1 (wParam = VK_BACK, lParam = 0xC0000001) # ATOK
+  // WM_IME_STARTCOMPOSITION * 1 (wParam = 0x0, lParam = 0x0)
+  // WM_IME_COMPOSITION      * 1 (wParam = 0x0, lParam = 0x1BF)
+  // WM_CHAR                 * n (wParam = VK_BACK, lParam = 0x1)
+  // WM_KEYUP                * 1 (wParam = VK_BACK, lParam = 0xC00E0001)
+  // ---------------------------------------------------------------------------
+  // This doesn't match usual key message pattern such as:
+  //   WM_KEYDOWN -> WM_CHAR -> WM_KEYDOWN -> WM_CHAR -> ... -> WM_KEYUP
+  // See following bugs for the detail.
+  // https://bugzilla.mozilla.gr.jp/show_bug.cgi?id=2885 (written in Japanese)
+  // https://bugzilla.mozilla.org/show_bug.cgi?id=194559 (written in English)
+  MSG startCompositionMsg, compositionMsg, charMsg;
+  return WinUtils::PeekMessage(&startCompositionMsg, mMsg.hwnd,
+                               WM_IME_STARTCOMPOSITION, WM_IME_STARTCOMPOSITION,
+                               PM_NOREMOVE | PM_NOYIELD) &&
+         WinUtils::PeekMessage(&compositionMsg, mMsg.hwnd, WM_IME_COMPOSITION,
+                               WM_IME_COMPOSITION, PM_NOREMOVE | PM_NOYIELD) &&
+         WinUtils::PeekMessage(&charMsg, mMsg.hwnd, WM_CHAR, WM_CHAR,
+                               PM_NOREMOVE | PM_NOYIELD) &&
+         startCompositionMsg.wParam == 0x0 &&
+         startCompositionMsg.lParam == 0x0 &&
+         compositionMsg.wParam == 0x0 &&
+         compositionMsg.lParam == 0x1BF &&
+         charMsg.wParam == VK_BACK && charMsg.lParam == 0x1 &&
+         startCompositionMsg.time <= compositionMsg.time &&
+         compositionMsg.time <= charMsg.time;
+}
+
 UINT
 NativeKey::GetScanCodeWithExtendedFlag() const
 {
   // MapVirtualKeyEx() has been improved for supporting extended keys since
   // Vista.  When we call it for mapping a scancode of an extended key and
   // a virtual keycode, we need to add 0xE000 to the scancode.
   // On Win XP and Win Server 2003, this doesn't support. On them, we have
   // no way to get virtual keycodes from scancode of extended keys.
@@ -901,22 +935,79 @@ NativeKey::NeedsToHandleWithoutFollowing
     return false;
   }
 
   // Even if the key is a printable key, it might cause non-printable character
   // input with modifier key(s).
   return IsPrintableKey();
 }
 
+void
+NativeKey::RemoveMessageAndDispatchPluginEvent(UINT aFirstMsg, UINT aLastMsg,
+                        const nsFakeCharMessage* aFakeCharMessage) const
+{
+  MSG msg;
+  if (aFakeCharMessage) {
+    if (aFirstMsg > WM_CHAR || aLastMsg < WM_CHAR) {
+      return;
+    }
+    msg = aFakeCharMessage->GetCharMessage(mMsg.hwnd);
+  } else {
+    WinUtils::GetMessage(&msg, mMsg.hwnd, aFirstMsg, aLastMsg);
+  }
+  mWidget->DispatchPluginEvent(msg);
+}
+
+bool
+NativeKey::DispatchKeyPressEventsAndDiscardsCharMessages(
+                        const UniCharsAndModifiers& aInputtingChars,
+                        const EventFlags& aExtraFlags,
+                        const nsFakeCharMessage* aFakeCharMessage) const
+{
+  MOZ_ASSERT(mMsg.message == WM_KEYDOWN || mMsg.message == WM_SYSKEYDOWN);
+
+  // Remove a possible WM_CHAR or WM_SYSCHAR messages from the message queue.
+  // 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 = false;
+
+  if (aFakeCharMessage) {
+    RemoveMessageAndDispatchPluginEvent(WM_KEYFIRST, WM_KEYLAST,
+                                        aFakeCharMessage);
+    anyCharMessagesRemoved = true;
+  } else {
+    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)) {
+      RemoveMessageAndDispatchPluginEvent(WM_KEYFIRST, WM_KEYLAST);
+      anyCharMessagesRemoved = true;
+      gotMsg = WinUtils::PeekMessage(&msg, mMsg.hwnd, WM_KEYFIRST, WM_KEYLAST,
+                                     PM_NOREMOVE | PM_NOYIELD);
+    }
+  }
+
+  if (!anyCharMessagesRemoved &&
+      mDOMKeyCode == NS_VK_BACK && IsIMEDoingKakuteiUndo()) {
+    MOZ_ASSERT(!aFakeCharMessage);
+    RemoveMessageAndDispatchPluginEvent(WM_CHAR, WM_CHAR);
+  }
+
+  return DispatchKeyPressEventsWithKeyboardLayout(aInputtingChars, aExtraFlags);
+}
+
 bool
 NativeKey::DispatchKeyPressEventsWithKeyboardLayout(
                         const UniCharsAndModifiers& aInputtingChars,
                         const EventFlags& aExtraFlags) const
 {
   MOZ_ASSERT(mMsg.message == WM_KEYDOWN || mMsg.message == WM_SYSKEYDOWN);
+  MOZ_ASSERT(!mIsDeadKey);
 
   KeyboardLayout* keyboardLayout = KeyboardLayout::GetInstance();
 
   UniCharsAndModifiers inputtingChars(aInputtingChars);
   UniCharsAndModifiers shiftedChars;
   UniCharsAndModifiers unshiftedChars;
   uint32_t shiftedLatinChar = 0;
   uint32_t unshiftedLatinChar = 0;
--- a/widget/windows/KeyboardLayout.h
+++ b/widget/windows/KeyboardLayout.h
@@ -6,16 +6,17 @@
 #ifndef KeyboardLayout_h__
 #define KeyboardLayout_h__
 
 #include "nscore.h"
 #include "nsAutoPtr.h"
 #include "nsEvent.h"
 #include "nsString.h"
 #include "nsWindowBase.h"
+#include "nsWindowDefs.h "
 #include <windows.h>
 
 #define NS_NUM_OF_KEYS          68
 
 #define VK_OEM_1                0xBA   // ';:' for US
 #define VK_OEM_PLUS             0xBB   // '+' any country
 #define VK_OEM_COMMA            0xBC
 #define VK_OEM_MINUS            0xBD   // '-' any country
@@ -344,16 +345,27 @@ public:
    * DispatchKeyPressEventsWithKeyboardLayout() dispatches keypress event(s)
    * with the information provided by KeyboardLayout class.
    */
   bool DispatchKeyPressEventsWithKeyboardLayout(
                         const UniCharsAndModifiers& aInputtingChars,
                         const EventFlags& aExtraFlags) const;
 
   /**
+   * Dispatches keypress events after removing WM_*CHAR messages for the
+   * WM_*KEYDOWN message.
+   * Returns true if the dispatched keypress event is consumed.  Otherwise,
+   * false.
+   */
+  bool DispatchKeyPressEventsAndDiscardsCharMessages(
+                        const UniCharsAndModifiers& aInputtingChars,
+                        const EventFlags& aExtraFlags,
+                        const nsFakeCharMessage* aFakeCharMessage) const;
+
+  /**
    * Checkes whether the key event down message is handled without following
    * WM_CHAR messages.  For example, if following WM_CHAR message indicates
    * control character input, the WM_CHAR message is unclear whether it's
    * caused by a printable key with Ctrl or just a function key such as Enter
    * or Backspace.
    */
   bool NeedsToHandleWithoutFollowingCharMessages() const;
 
@@ -409,16 +421,29 @@ private:
   {
     MOZ_NOT_REACHED("The default constructor of NativeKey isn't available");
   }
 
   UINT GetScanCodeWithExtendedFlag() const;
 
   // The result is one of nsIDOMKeyEvent::DOM_KEY_LOCATION_*.
   uint32_t GetKeyLocation() const;
+
+  /**
+   * "Kakutei-Undo" of ATOK or WXG (both of them are Japanese IME) causes
+   * strange WM_KEYDOWN/WM_KEYUP/WM_CHAR message pattern.  So, when this
+   * returns true, the caller needs to be careful for processing the messages.
+   */
+  bool IsIMEDoingKakuteiUndo() const;
+
+  /*
+   * Dispatches a plugin event after the specified message is removed.
+   */
+  void RemoveMessageAndDispatchPluginEvent(UINT aFirstMsg, UINT aLastMsg,
+                const nsFakeCharMessage* aFakeCharMessage = nullptr) const;
 };
 
 class KeyboardLayout
 {
   friend class NativeKey;
 
 private:
   KeyboardLayout();
--- a/widget/windows/WinIMEHandler.cpp
+++ b/widget/windows/WinIMEHandler.cpp
@@ -368,51 +368,16 @@ IMEHandler::CurrentKeyboardLayoutHasIME(
   }
 #endif // #ifdef NS_ENABLE_TSF
 
   return nsIMM32Handler::IsIMEAvailable();
 }
 #endif // #ifdef DEBUG
 
 // static
-bool
-IMEHandler::IsDoingKakuteiUndo(HWND aWnd)
-{
-  // Following message pattern is caused by "Kakutei-Undo" of ATOK or WXG:
-  // ---------------------------------------------------------------------------
-  // WM_KEYDOWN              * n (wParam = VK_BACK, lParam = 0x1)
-  // WM_KEYUP                * 1 (wParam = VK_BACK, lParam = 0xC0000001) # ATOK
-  // WM_IME_STARTCOMPOSITION * 1 (wParam = 0x0, lParam = 0x0)
-  // WM_IME_COMPOSITION      * 1 (wParam = 0x0, lParam = 0x1BF)
-  // WM_CHAR                 * n (wParam = VK_BACK, lParam = 0x1)
-  // WM_KEYUP                * 1 (wParam = VK_BACK, lParam = 0xC00E0001)
-  // ---------------------------------------------------------------------------
-  // This doesn't match usual key message pattern such as:
-  //   WM_KEYDOWN -> WM_CHAR -> WM_KEYDOWN -> WM_CHAR -> ... -> WM_KEYUP
-  // See following bugs for the detail.
-  // https://bugzilla.mozilla.gr.jp/show_bug.cgi?id=2885 (written in Japanese)
-  // https://bugzilla.mozilla.org/show_bug.cgi?id=194559 (written in English)
-  MSG startCompositionMsg, compositionMsg, charMsg;
-  return WinUtils::PeekMessage(&startCompositionMsg, aWnd,
-                               WM_IME_STARTCOMPOSITION, WM_IME_STARTCOMPOSITION,
-                               PM_NOREMOVE | PM_NOYIELD) &&
-         WinUtils::PeekMessage(&compositionMsg, aWnd, WM_IME_COMPOSITION,
-                               WM_IME_COMPOSITION, PM_NOREMOVE | PM_NOYIELD) &&
-         WinUtils::PeekMessage(&charMsg, aWnd, WM_CHAR, WM_CHAR,
-                               PM_NOREMOVE | PM_NOYIELD) &&
-         startCompositionMsg.wParam == 0x0 &&
-         startCompositionMsg.lParam == 0x0 &&
-         compositionMsg.wParam == 0x0 &&
-         compositionMsg.lParam == 0x1BF &&
-         charMsg.wParam == VK_BACK && charMsg.lParam == 0x1 &&
-         startCompositionMsg.time <= compositionMsg.time &&
-         compositionMsg.time <= charMsg.time;
-}
-
-// static
 void
 IMEHandler::SetInputScopeForIMM32(nsWindow* aWindow,
                                   const nsAString& aHTMLInputType)
 {
   if (sIsInTSFMode || !sSetInputScopes || aWindow->Destroyed()) {
     return;
   }
   UINT arraySize = 0;
--- a/widget/windows/WinIMEHandler.h
+++ b/widget/windows/WinIMEHandler.h
@@ -107,23 +107,16 @@ public:
                               InputContext& aInputContext,
                               const InputContextAction& aAction);
 
   /**
    * Called when the window is created.
    */
   static void InitInputContext(nsWindow* aWindow, InputContext& aInputContext);
 
-  /**
-   * "Kakutei-Undo" of ATOK or WXG (both of them are Japanese IME) causes
-   * strange WM_KEYDOWN/WM_KEYUP/WM_CHAR message pattern.  So, when this
-   * returns true, the caller needs to be careful for processing the messages.
-   */
-  static bool IsDoingKakuteiUndo(HWND aWnd);
-
 #ifdef DEBUG
   /**
    * Returns true when current keyboard layout has IME.  Otherwise, false.
    */
   static bool CurrentKeyboardLayoutHasIME();
 #endif // #ifdef DEBUG
 
 private:
--- a/widget/windows/WinUtils.cpp
+++ b/widget/windows/WinUtils.cpp
@@ -411,22 +411,23 @@ WinUtils::GetMouseInputSource()
     inputSource = (lParamExtraInfo & TABLET_INK_TOUCH) ?
       nsIDOMMouseEvent::MOZ_SOURCE_TOUCH : nsIDOMMouseEvent::MOZ_SOURCE_PEN;
   }
   return static_cast<uint16_t>(inputSource);
 }
 
 /* static */
 MSG
-WinUtils::InitMSG(UINT aMessage, WPARAM wParam, LPARAM lParam)
+WinUtils::InitMSG(UINT aMessage, WPARAM wParam, LPARAM lParam, HWND aWnd)
 {
   MSG msg;
   msg.message = aMessage;
   msg.wParam  = wParam;
   msg.lParam  = lParam;
+  msg.hwnd    = aWnd;
   return msg;
 }
 
 /* static */
 HRESULT
 WinUtils::SHCreateItemFromParsingName(PCWSTR pszPath, IBindCtx *pbc,
                                       REFIID riid, void **ppv)
 {
--- a/widget/windows/WinUtils.h
+++ b/widget/windows/WinUtils.h
@@ -156,17 +156,17 @@ public:
    *       plugin's window.
    */
   static HWND FindOurWindowAtPoint(const POINT& aPointInScreen);
 
   /**
    * InitMSG() returns an MSG struct which was initialized by the params.
    * Don't trust the other members in the result.
    */
-  static MSG InitMSG(UINT aMessage, WPARAM wParam, LPARAM lParam);
+  static MSG InitMSG(UINT aMessage, WPARAM wParam, LPARAM lParam, HWND aWnd);
 
   /**
    * GetScanCode() returns a scan code for the LPARAM of WM_KEYDOWN, WM_KEYUP,
    * WM_CHAR and WM_UNICHAR.
    *
    */
   static WORD GetScanCode(LPARAM aLParam)
   {
--- a/widget/windows/nsWindow.cpp
+++ b/widget/windows/nsWindow.cpp
@@ -3731,38 +3731,23 @@ void nsWindow::DispatchPendingEvents()
 }
 
 bool nsWindow::DispatchPluginEvent(UINT aMessage,
                                      WPARAM aWParam,
                                      LPARAM aLParam,
                                      bool aDispatchPendingEvents)
 {
   bool ret = nsWindowBase::DispatchPluginEvent(
-               WinUtils::InitMSG(aMessage, aWParam, aLParam));
+               WinUtils::InitMSG(aMessage, aWParam, aLParam, mWnd));
   if (aDispatchPendingEvents) {
     DispatchPendingEvents();
   }
   return ret;
 }
 
-void nsWindow::RemoveMessageAndDispatchPluginEvent(UINT aFirstMsg,
-                 UINT aLastMsg, nsFakeCharMessage* aFakeCharMessage)
-{
-  MSG msg;
-  if (aFakeCharMessage) {
-    if (aFirstMsg > WM_CHAR || aLastMsg < WM_CHAR) {
-      return;
-    }
-    msg = aFakeCharMessage->GetCharMessage(mWnd);
-  } else {
-    WinUtils::GetMessage(&msg, mWnd, aFirstMsg, aLastMsg);
-  }
-  nsWindowBase::DispatchPluginEvent(msg);
-}
-
 // Deal with all sort of mouse event
 bool nsWindow::DispatchMouseEvent(uint32_t aEventType, WPARAM wParam,
                                     LPARAM lParam, bool aIsContextMenuKey,
                                     int16_t aButton, uint16_t aInputSource)
 {
   bool result = false;
 
   UserActivity();
@@ -4448,17 +4433,17 @@ bool nsWindow::ProcessMessage(UINT msg, 
 
   if (MouseScrollHandler::ProcessMessage(this, msg, wParam, lParam, aRetValue,
                                          eatMessage)) {
     return mWnd ? eatMessage : true;
   }
 
   if (PluginHasFocus()) {
     bool callDefaultWndProc;
-    MSG nativeMsg = WinUtils::InitMSG(msg, wParam, lParam);
+    MSG nativeMsg = WinUtils::InitMSG(msg, wParam, lParam, mWnd);
     if (ProcessMessageForPlugin(nativeMsg, aRetValue, callDefaultWndProc)) {
       return mWnd ? !callDefaultWndProc : true;
     }
   }
 
   bool result = false;    // call the default nsWindow proc
   *aRetValue = 0;
 
@@ -4743,36 +4728,36 @@ bool nsWindow::ProcessMessage(UINT msg, 
 
     case WM_HOTKEY:
       result = OnHotKey(wParam, lParam);
       break;
 
     case WM_SYSCHAR:
     case WM_CHAR:
     {
-      MSG nativeMsg = WinUtils::InitMSG(msg, wParam, lParam);
+      MSG nativeMsg = WinUtils::InitMSG(msg, wParam, lParam, mWnd);
       result = ProcessCharMessage(nativeMsg, nullptr);
       DispatchPendingEvents();
     }
     break;
 
     case WM_SYSKEYUP:
     case WM_KEYUP:
     {
-      MSG nativeMsg = WinUtils::InitMSG(msg, wParam, lParam);
+      MSG nativeMsg = WinUtils::InitMSG(msg, wParam, lParam, mWnd);
       nativeMsg.time = ::GetMessageTime();
       result = ProcessKeyUpMessage(nativeMsg, nullptr);
       DispatchPendingEvents();
     }
     break;
 
     case WM_SYSKEYDOWN:
     case WM_KEYDOWN:
     {
-      MSG nativeMsg = WinUtils::InitMSG(msg, wParam, lParam);
+      MSG nativeMsg = WinUtils::InitMSG(msg, wParam, lParam, mWnd);
       result = ProcessKeyDownMessage(nativeMsg, nullptr);
       DispatchPendingEvents();
     }
     break;
 
     // say we've dealt with erase background if widget does
     // not need auto-erasing
     case WM_ERASEBKGND:
@@ -5784,17 +5769,17 @@ nsWindow::SynthesizeNativeKeyEvent(int32
     UINT scanCode = keyboardLayout->ComputeScanCodeForVirtualKeyCode(
       argumentKeySpecific ? argumentKeySpecific : aNativeKeyCode);
     LPARAM lParam = static_cast<LPARAM>(scanCode << 16);
     // Add extended key flag to the lParam for right control key and right alt
     // key.
     if (keySpecific == VK_RCONTROL || keySpecific == VK_RMENU) {
       lParam |= 0x1000000;
     }
-    MSG msg = WinUtils::InitMSG(WM_KEYDOWN, key, lParam);
+    MSG msg = WinUtils::InitMSG(WM_KEYDOWN, key, lParam, mWnd);
     if (i == keySequence.Length() - 1) {
       bool makeDeadCharMessage =
         keyboardLayout->IsDeadKey(key, modKeyState) && aCharacters.IsEmpty();
       nsAutoString chars(aCharacters);
       if (makeDeadCharMessage) {
         UniCharsAndModifiers deadChars =
           keyboardLayout->GetUniCharsAndModifiers(key, modKeyState);
         chars = deadChars.ToString();
@@ -5830,17 +5815,17 @@ nsWindow::SynthesizeNativeKeyEvent(int32
     UINT scanCode = keyboardLayout->ComputeScanCodeForVirtualKeyCode(
       argumentKeySpecific ? argumentKeySpecific : aNativeKeyCode);
     LPARAM lParam = static_cast<LPARAM>(scanCode << 16);
     // Add extended key flag to the lParam for right control key and right alt
     // key.
     if (keySpecific == VK_RCONTROL || keySpecific == VK_RMENU) {
       lParam |= 0x1000000;
     }
-    MSG msg = WinUtils::InitMSG(WM_KEYUP, key, lParam);
+    MSG msg = WinUtils::InitMSG(WM_KEYUP, key, lParam, mWnd);
     NativeKey nativeKey(this, msg, modKeyState);
     nativeKey.HandleKeyUpMessage();
   }
 
   // Restore old key state and layout
   ::SetKeyboardState(originalKbdState);
   keyboardLayout->RestoreLayout();
 
@@ -6460,58 +6445,29 @@ LRESULT nsWindow::OnKeyDown(const MSG &a
     case NS_VK_NUM_LOCK:
     case NS_VK_SCROLL_LOCK:
     case NS_VK_WIN:
       return noDefault;
   }
 
   EventFlags extraFlags;
   extraFlags.mDefaultPrevented = noDefault;
+
+  if (nativeKey.NeedsToHandleWithoutFollowingCharMessages()) {
+    return nativeKey.DispatchKeyPressEventsAndDiscardsCharMessages(
+                       inputtingChars, extraFlags, aFakeCharMessage);
+  }
+
   MSG msg;
   BOOL gotMsg = aFakeCharMessage ||
     WinUtils::PeekMessage(&msg, mWnd, WM_KEYFIRST, WM_KEYLAST,
                           PM_NOREMOVE | PM_NOYIELD);
-  // Enter and backspace are always handled here to avoid for example the
-  // confusion between ctrl-enter and ctrl-J.
-  if (nativeKey.NeedsToHandleWithoutFollowingCharMessages()) {
-    // Remove a possible WM_CHAR or WM_SYSCHAR messages from the message queue.
-    // 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 = false;
-
-    if (aFakeCharMessage) {
-      RemoveMessageAndDispatchPluginEvent(WM_KEYFIRST, WM_KEYLAST,
-                                          aFakeCharMessage);
-      anyCharMessagesRemoved = true;
-    } else {
-      while (gotMsg && (msg.message == WM_CHAR || msg.message == WM_SYSCHAR))
-      {
-        PR_LOG(gWindowsLog, PR_LOG_ALWAYS,
-               ("%s charCode=%d scanCode=%d\n", msg.message == WM_SYSCHAR ? 
-                                                "WM_SYSCHAR" : "WM_CHAR",
-                msg.wParam, HIWORD(msg.lParam) & 0xFF));
-        RemoveMessageAndDispatchPluginEvent(WM_KEYFIRST, WM_KEYLAST);
-        anyCharMessagesRemoved = true;
-
-        gotMsg = WinUtils::PeekMessage(&msg, mWnd, WM_KEYFIRST, WM_KEYLAST,
-                                       PM_NOREMOVE | PM_NOYIELD);
-      }
-    }
-
-    if (!anyCharMessagesRemoved && DOMKeyCode == NS_VK_BACK &&
-        IMEHandler::IsDoingKakuteiUndo(mWnd)) {
-      NS_ASSERTION(!aFakeCharMessage,
-                   "We shouldn't be touching the real msg queue");
-      RemoveMessageAndDispatchPluginEvent(WM_CHAR, WM_CHAR);
-    }
-  }
-  else if (gotMsg &&
-           (aFakeCharMessage ||
-            msg.message == WM_CHAR || msg.message == WM_SYSCHAR || msg.message == WM_DEADCHAR)) {
+  if (gotMsg &&
+      (aFakeCharMessage || msg.message == WM_CHAR ||
+       msg.message == WM_SYSCHAR || msg.message == WM_DEADCHAR)) {
     if (aFakeCharMessage) {
       MSG msg = aFakeCharMessage->GetCharMessage(mWnd);
       if (msg.message == WM_DEADCHAR) {
         return false;
       }
 #ifdef DEBUG
       if (nativeKey.IsPrintableKey()) {
         nsPrintfCString log(
--- a/widget/windows/nsWindow.h
+++ b/widget/windows/nsWindow.h
@@ -321,19 +321,16 @@ protected:
   /**
    * Event processing helpers
    */
   void                    DispatchFocusToTopLevelWindow(bool aIsActivate);
   bool                    DispatchStandardEvent(uint32_t aMsg);
   bool                    DispatchCommandEvent(uint32_t aEventCommand);
   void                    RelayMouseEvent(UINT aMsg, WPARAM wParam, LPARAM lParam);
   static void             RemoveNextCharMessage(HWND aWnd);
-  void                    RemoveMessageAndDispatchPluginEvent(UINT aFirstMsg,
-                            UINT aLastMsg,
-                            nsFakeCharMessage* aFakeCharMessage = nullptr);
   virtual bool            ProcessMessage(UINT msg, WPARAM &wParam,
                                          LPARAM &lParam, LRESULT *aRetValue);
   bool                    ProcessMessageForPlugin(const MSG &aMsg,
                                                   LRESULT *aRetValue, bool &aCallDefWndProc);
   LRESULT                 ProcessCharMessage(const MSG &aMsg,
                                              bool *aEventDispatched);
   LRESULT                 ProcessKeyUpMessage(const MSG &aMsg,
                                               bool *aEventDispatched);
--- a/widget/windows/nsWindowDefs.h
+++ b/widget/windows/nsWindowDefs.h
@@ -216,17 +216,17 @@ static const uint32_t sModifierKeyMap[][
 
 // Used in OnKeyDown
 struct nsAlternativeCharCode; // defined in nsGUIEvent.h
 struct nsFakeCharMessage {
   UINT mCharCode;
   UINT mScanCode;
   bool mIsDeadKey;
 
-  MSG GetCharMessage(HWND aWnd)
+  MSG GetCharMessage(HWND aWnd) const
   {
     MSG msg;
     msg.hwnd = aWnd;
     msg.message = mIsDeadKey ? WM_DEADCHAR : WM_CHAR;
     msg.wParam = static_cast<WPARAM>(mCharCode);
     msg.lParam = static_cast<LPARAM>(mScanCode);
     msg.time = 0;
     msg.pt.x = msg.pt.y = 0;