dom/events/KeyboardEvent.cpp

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

     1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 #include "mozilla/dom/KeyboardEvent.h"
     7 #include "mozilla/TextEvents.h"
     8 #include "prtime.h"
    10 namespace mozilla {
    11 namespace dom {
    13 KeyboardEvent::KeyboardEvent(EventTarget* aOwner,
    14                              nsPresContext* aPresContext,
    15                              WidgetKeyboardEvent* aEvent)
    16   : UIEvent(aOwner, aPresContext,
    17             aEvent ? aEvent : new WidgetKeyboardEvent(false, 0, nullptr))
    18   , mInitializedByCtor(false)
    19   , mInitialzedWhichValue(0)
    20 {
    21   NS_ASSERTION(mEvent->eventStructType == NS_KEY_EVENT, "event type mismatch");
    23   if (aEvent) {
    24     mEventIsInternal = false;
    25   }
    26   else {
    27     mEventIsInternal = true;
    28     mEvent->time = PR_Now();
    29     mEvent->AsKeyboardEvent()->mKeyNameIndex = KEY_NAME_INDEX_USE_STRING;
    30   }
    31 }
    33 NS_IMPL_ADDREF_INHERITED(KeyboardEvent, UIEvent)
    34 NS_IMPL_RELEASE_INHERITED(KeyboardEvent, UIEvent)
    36 NS_INTERFACE_MAP_BEGIN(KeyboardEvent)
    37   NS_INTERFACE_MAP_ENTRY(nsIDOMKeyEvent)
    38 NS_INTERFACE_MAP_END_INHERITING(UIEvent)
    40 bool
    41 KeyboardEvent::AltKey()
    42 {
    43   return mEvent->AsKeyboardEvent()->IsAlt();
    44 }
    46 NS_IMETHODIMP
    47 KeyboardEvent::GetAltKey(bool* aIsDown)
    48 {
    49   NS_ENSURE_ARG_POINTER(aIsDown);
    50   *aIsDown = AltKey();
    51   return NS_OK;
    52 }
    54 bool
    55 KeyboardEvent::CtrlKey()
    56 {
    57   return mEvent->AsKeyboardEvent()->IsControl();
    58 }
    60 NS_IMETHODIMP
    61 KeyboardEvent::GetCtrlKey(bool* aIsDown)
    62 {
    63   NS_ENSURE_ARG_POINTER(aIsDown);
    64   *aIsDown = CtrlKey();
    65   return NS_OK;
    66 }
    68 bool
    69 KeyboardEvent::ShiftKey()
    70 {
    71   return mEvent->AsKeyboardEvent()->IsShift();
    72 }
    74 NS_IMETHODIMP
    75 KeyboardEvent::GetShiftKey(bool* aIsDown)
    76 {
    77   NS_ENSURE_ARG_POINTER(aIsDown);
    78   *aIsDown = ShiftKey();
    79   return NS_OK;
    80 }
    82 bool
    83 KeyboardEvent::MetaKey()
    84 {
    85   return mEvent->AsKeyboardEvent()->IsMeta();
    86 }
    88 NS_IMETHODIMP
    89 KeyboardEvent::GetMetaKey(bool* aIsDown)
    90 {
    91   NS_ENSURE_ARG_POINTER(aIsDown);
    92   *aIsDown = MetaKey();
    93   return NS_OK;
    94 }
    96 bool
    97 KeyboardEvent::Repeat()
    98 {
    99   return mEvent->AsKeyboardEvent()->mIsRepeat;
   100 }
   102 NS_IMETHODIMP
   103 KeyboardEvent::GetRepeat(bool* aIsRepeat)
   104 {
   105   NS_ENSURE_ARG_POINTER(aIsRepeat);
   106   *aIsRepeat = Repeat();
   107   return NS_OK;
   108 }
   110 bool
   111 KeyboardEvent::IsComposing()
   112 {
   113   return mEvent->AsKeyboardEvent()->mIsComposing;
   114 }
   116 NS_IMETHODIMP
   117 KeyboardEvent::GetModifierState(const nsAString& aKey,
   118                                 bool* aState)
   119 {
   120   NS_ENSURE_ARG_POINTER(aState);
   122   *aState = GetModifierState(aKey);
   123   return NS_OK;
   124 }
   126 NS_IMETHODIMP
   127 KeyboardEvent::GetKey(nsAString& aKeyName)
   128 {
   129   mEvent->AsKeyboardEvent()->GetDOMKeyName(aKeyName);
   130   return NS_OK;
   131 }
   133 NS_IMETHODIMP
   134 KeyboardEvent::GetCharCode(uint32_t* aCharCode)
   135 {
   136   NS_ENSURE_ARG_POINTER(aCharCode);
   137   *aCharCode = CharCode();
   138   return NS_OK;
   139 }
   141 uint32_t
   142 KeyboardEvent::CharCode()
   143 {
   144   // If this event is initialized with ctor, we shouldn't check event type.
   145   if (mInitializedByCtor) {
   146     return mEvent->AsKeyboardEvent()->charCode;
   147   }
   149   switch (mEvent->message) {
   150   case NS_KEY_UP:
   151   case NS_KEY_DOWN:
   152     return 0;
   153   case NS_KEY_PRESS:
   154     return mEvent->AsKeyboardEvent()->charCode;
   155   }
   156   return 0;
   157 }
   159 NS_IMETHODIMP
   160 KeyboardEvent::GetKeyCode(uint32_t* aKeyCode)
   161 {
   162   NS_ENSURE_ARG_POINTER(aKeyCode);
   163   *aKeyCode = KeyCode();
   164   return NS_OK;
   165 }
   167 uint32_t
   168 KeyboardEvent::KeyCode()
   169 {
   170   // If this event is initialized with ctor, we shouldn't check event type.
   171   if (mInitializedByCtor) {
   172     return mEvent->AsKeyboardEvent()->keyCode;
   173   }
   175   switch (mEvent->message) {
   176   case NS_KEY_UP:
   177   case NS_KEY_PRESS:
   178   case NS_KEY_DOWN:
   179     return mEvent->AsKeyboardEvent()->keyCode;
   180   }
   181   return 0;
   182 }
   184 uint32_t
   185 KeyboardEvent::Which()
   186 {
   187   // If this event is initialized with ctor, which can have independent value.
   188   if (mInitializedByCtor) {
   189     return mInitialzedWhichValue;
   190   }
   192   switch (mEvent->message) {
   193     case NS_KEY_UP:
   194     case NS_KEY_DOWN:
   195       return KeyCode();
   196     case NS_KEY_PRESS:
   197       //Special case for 4xp bug 62878.  Try to make value of which
   198       //more closely mirror the values that 4.x gave for RETURN and BACKSPACE
   199       {
   200         uint32_t keyCode = mEvent->AsKeyboardEvent()->keyCode;
   201         if (keyCode == NS_VK_RETURN || keyCode == NS_VK_BACK) {
   202           return keyCode;
   203         }
   204         return CharCode();
   205       }
   206   }
   208   return 0;
   209 }
   211 NS_IMETHODIMP
   212 KeyboardEvent::GetLocation(uint32_t* aLocation)
   213 {
   214   NS_ENSURE_ARG_POINTER(aLocation);
   216   *aLocation = Location();
   217   return NS_OK;
   218 }
   220 uint32_t
   221 KeyboardEvent::Location()
   222 {
   223   return mEvent->AsKeyboardEvent()->location;
   224 }
   226 // static
   227 already_AddRefed<KeyboardEvent>
   228 KeyboardEvent::Constructor(const GlobalObject& aGlobal,
   229                            const nsAString& aType,
   230                            const KeyboardEventInit& aParam,
   231                            ErrorResult& aRv)
   232 {
   233   nsCOMPtr<EventTarget> target = do_QueryInterface(aGlobal.GetAsSupports());
   234   nsRefPtr<KeyboardEvent> newEvent =
   235     new KeyboardEvent(target, nullptr, nullptr);
   236   bool trusted = newEvent->Init(target);
   237   aRv = newEvent->InitKeyEvent(aType, aParam.mBubbles, aParam.mCancelable,
   238                                aParam.mView, aParam.mCtrlKey, aParam.mAltKey,
   239                                aParam.mShiftKey, aParam.mMetaKey,
   240                                aParam.mKeyCode, aParam.mCharCode);
   241   newEvent->SetTrusted(trusted);
   242   newEvent->mDetail = aParam.mDetail;
   243   newEvent->mInitializedByCtor = true;
   244   newEvent->mInitialzedWhichValue = aParam.mWhich;
   246   WidgetKeyboardEvent* internalEvent = newEvent->mEvent->AsKeyboardEvent();
   247   internalEvent->location = aParam.mLocation;
   248   internalEvent->mIsRepeat = aParam.mRepeat;
   249   internalEvent->mIsComposing = aParam.mIsComposing;
   250   internalEvent->mKeyNameIndex = KEY_NAME_INDEX_USE_STRING;
   251   internalEvent->mKeyValue = aParam.mKey;
   253   return newEvent.forget();
   254 }
   256 NS_IMETHODIMP
   257 KeyboardEvent::InitKeyEvent(const nsAString& aType,
   258                             bool aCanBubble,
   259                             bool aCancelable,
   260                             nsIDOMWindow* aView,
   261                             bool aCtrlKey,
   262                             bool aAltKey,
   263                             bool aShiftKey,
   264                             bool aMetaKey,
   265                             uint32_t aKeyCode,
   266                             uint32_t aCharCode)
   267 {
   268   nsresult rv = UIEvent::InitUIEvent(aType, aCanBubble, aCancelable, aView, 0);
   269   NS_ENSURE_SUCCESS(rv, rv);
   271   WidgetKeyboardEvent* keyEvent = mEvent->AsKeyboardEvent();
   272   keyEvent->InitBasicModifiers(aCtrlKey, aAltKey, aShiftKey, aMetaKey);
   273   keyEvent->keyCode = aKeyCode;
   274   keyEvent->charCode = aCharCode;
   276   return NS_OK;
   277 }
   279 } // namespace dom
   280 } // namespace mozilla
   282 using namespace mozilla;
   283 using namespace mozilla::dom;
   285 nsresult
   286 NS_NewDOMKeyboardEvent(nsIDOMEvent** aInstancePtrResult,
   287                        EventTarget* aOwner,
   288                        nsPresContext* aPresContext,
   289                        WidgetKeyboardEvent* aEvent)
   290 {
   291   KeyboardEvent* it = new KeyboardEvent(aOwner, aPresContext, aEvent);
   292   NS_ADDREF(it);
   293   *aInstancePtrResult = static_cast<Event*>(it);
   294   return NS_OK;
   295 }

mercurial