Bug 1257731 - Part 2. Don't use Win32 API on content process. r?masayuki draft
authorMakoto Kato <m_kato@ga2.so-net.ne.jp>
Wed, 08 Jun 2016 13:23:11 +0900
changeset 376493 ea52839d427431a2ba7624ef199f9842fe12f8ef
parent 376492 fbfb473ebfd28a0f19b81b8030a0dfe60bc310a9
child 523169 a8e85afab178cecedf661d7487840adb5d0fb184
push id20595
push userm_kato@ga2.so-net.ne.jp
push dateWed, 08 Jun 2016 04:53:01 +0000
reviewersmasayuki
bugs1257731
milestone50.0a1
Bug 1257731 - Part 2. Don't use Win32 API on content process. r?masayuki On content sandbox process, GetKeyboardState() API doesn't return current keyboard state. So we shouldn't use it. MozReview-Commit-ID: 4phnJf0sJFZ
editor/libeditor/TypeInState.h
editor/libeditor/nsEditorEventListener.cpp
editor/libeditor/nsHTMLEditRules.cpp
--- a/editor/libeditor/TypeInState.h
+++ b/editor/libeditor/TypeInState.h
@@ -9,16 +9,21 @@
 #include "nsCOMPtr.h"
 #include "nsCycleCollectionParticipant.h"
 #include "nsISelectionListener.h"
 #include "nsISupportsImpl.h"
 #include "nsString.h"
 #include "nsTArray.h"
 #include "nscore.h"
 
+// Workaround for windows headers
+#ifdef SetProp
+#undef SetProp
+#endif
+
 class nsIAtom;
 class nsIDOMNode;
 namespace mozilla {
 namespace dom {
 class Selection;
 } // namespace dom
 } // namespace mozilla
 
--- a/editor/libeditor/nsEditorEventListener.cpp
+++ b/editor/libeditor/nsEditorEventListener.cpp
@@ -493,73 +493,53 @@ nsEditorEventListener::HandleEvent(nsIDO
     NS_ConvertUTF16toUTF8(eventType).get());
   NS_ASSERTION(false, assertMessage.get());
 #endif
 
   return NS_OK;
 }
 
 #ifdef HANDLE_NATIVE_TEXT_DIRECTION_SWITCH
-#include <windows.h>
-// Undo the windows.h damage
-#undef GetMessage
-#undef CreateEvent
-#undef GetClassName
-#undef GetBinaryType
-#undef RemoveDirectory
-#undef SetProp
-
 namespace {
 
 // This function is borrowed from Chromium's ImeInput::IsCtrlShiftPressed
-bool IsCtrlShiftPressed(bool& isRTL)
+bool IsCtrlShiftPressed(nsIDOMKeyEvent* aEvent, bool& isRTL)
 {
-  BYTE keystate[256];
-  if (!::GetKeyboardState(keystate)) {
-    return false;
-  }
-
   // To check if a user is pressing only a control key and a right-shift key
   // (or a left-shift key), we use the steps below:
   // 1. Check if a user is pressing a control key and a right-shift key (or
   //    a left-shift key).
   // 2. If the condition 1 is true, we should check if there are any other
   //    keys pressed at the same time.
   //    To ignore the keys checked in 1, we set their status to 0 before
   //    checking the key status.
-  const int kKeyDownMask = 0x80;
-  if ((keystate[VK_CONTROL] & kKeyDownMask) == 0) {
+  WidgetKeyboardEvent* keyboardEvent =
+    aEvent->AsEvent()->WidgetEventPtr()->AsKeyboardEvent();
+  MOZ_ASSERT(keyboardEvent,
+             "DOM key event's internal event must be WidgetKeyboardEvent");
+
+  if (!keyboardEvent->IsControl()) {
     return false;
   }
 
-  if (keystate[VK_RSHIFT] & kKeyDownMask) {
-    keystate[VK_RSHIFT] = 0;
+  uint32_t location = keyboardEvent->mLocation;
+  if (location == nsIDOMKeyEvent::DOM_KEY_LOCATION_RIGHT) {
     isRTL = true;
-  } else if (keystate[VK_LSHIFT] & kKeyDownMask) {
-    keystate[VK_LSHIFT] = 0;
+  } else if (location == nsIDOMKeyEvent::DOM_KEY_LOCATION_LEFT) {
     isRTL = false;
   } else {
     return false;
   }
 
   // Scan the key status to find pressed keys. We should abandon changing the
   // text direction when there are other pressed keys.
-  // This code is executed only when a user is pressing a control key and a
-  // right-shift key (or a left-shift key), i.e. we should ignore the status of
-  // the keys: VK_SHIFT, VK_CONTROL, VK_RCONTROL, and VK_LCONTROL.
-  // So, we reset their status to 0 and ignore them.
-  keystate[VK_SHIFT] = 0;
-  keystate[VK_CONTROL] = 0;
-  keystate[VK_RCONTROL] = 0;
-  keystate[VK_LCONTROL] = 0;
-  for (int i = 0; i <= VK_PACKET; ++i) {
-    if (keystate[i] & kKeyDownMask) {
-      return false;
-    }
+  if (keyboardEvent->IsAlt() || keyboardEvent->IsOS()) {
+    return false;
   }
+
   return true;
 }
 
 }
 
 // This logic is mostly borrowed from Chromium's
 // RenderWidgetHostViewWin::OnKeyEvent.
 
@@ -593,17 +573,17 @@ nsEditorEventListener::KeyDown(nsIDOMKey
   if (!mHaveBidiKeyboards) {
     return NS_OK;
   }
 
   uint32_t keyCode = 0;
   aKeyEvent->GetKeyCode(&keyCode);
   if (keyCode == nsIDOMKeyEvent::DOM_VK_SHIFT) {
     bool switchToRTL;
-    if (IsCtrlShiftPressed(switchToRTL)) {
+    if (IsCtrlShiftPressed(aKeyEvent, switchToRTL)) {
       mShouldSwitchTextDirection = true;
       mSwitchToRTL = switchToRTL;
     }
   } else if (keyCode != nsIDOMKeyEvent::DOM_VK_CONTROL) {
     // In case the user presses any other key besides Ctrl and Shift
     mShouldSwitchTextDirection = false;
   }
   return NS_OK;
--- a/editor/libeditor/nsHTMLEditRules.cpp
+++ b/editor/libeditor/nsHTMLEditRules.cpp
@@ -49,16 +49,21 @@
 #include "nsStringFwd.h"
 #include "nsTArray.h"
 #include "nsTextEditUtils.h"
 #include "nsThreadUtils.h"
 #include "nsUnicharUtils.h"
 #include "nsWSRunObject.h"
 #include <algorithm>
 
+// Workaround for windows headers
+#ifdef SetProp
+#undef SetProp
+#endif
+
 class nsISupports;
 class nsRulesInfo;
 
 using namespace mozilla;
 using namespace mozilla::dom;
 
 //const static char* kMOZEditorBogusNodeAttr="MOZ_EDITOR_BOGUS_NODE";
 //const static char* kMOZEditorBogusNodeValue="TRUE";