dom/events/WheelEvent.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: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* vim: set ts=2 et sw=2 tw=80: */
     3 /* This Source Code Form is subject to the terms of the Mozilla Public
     4  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     5  * You can obtain one at http://mozilla.org/MPL/2.0/. */
     7 #include "mozilla/dom/WheelEvent.h"
     8 #include "mozilla/MouseEvents.h"
     9 #include "prtime.h"
    11 namespace mozilla {
    12 namespace dom {
    14 WheelEvent::WheelEvent(EventTarget* aOwner,
    15                        nsPresContext* aPresContext,
    16                        WidgetWheelEvent* aWheelEvent)
    17   : MouseEvent(aOwner, aPresContext,
    18                aWheelEvent ? aWheelEvent :
    19                              new WidgetWheelEvent(false, 0, nullptr))
    20   , mAppUnitsPerDevPixel(0)
    21 {
    22   if (aWheelEvent) {
    23     mEventIsInternal = false;
    24     // If the delta mode is pixel, the WidgetWheelEvent's delta values are in
    25     // device pixels.  However, JS contents need the delta values in CSS pixels.
    26     // We should store the value of mAppUnitsPerDevPixel here because
    27     // it might be changed by changing zoom or something.
    28     if (aWheelEvent->deltaMode == nsIDOMWheelEvent::DOM_DELTA_PIXEL) {
    29       mAppUnitsPerDevPixel = aPresContext->AppUnitsPerDevPixel();
    30     }
    31   } else {
    32     mEventIsInternal = true;
    33     mEvent->time = PR_Now();
    34     mEvent->refPoint.x = mEvent->refPoint.y = 0;
    35     mEvent->AsWheelEvent()->inputSource = nsIDOMMouseEvent::MOZ_SOURCE_UNKNOWN;
    36   }
    37 }
    39 NS_IMPL_ADDREF_INHERITED(WheelEvent, MouseEvent)
    40 NS_IMPL_RELEASE_INHERITED(WheelEvent, MouseEvent)
    42 NS_INTERFACE_MAP_BEGIN(WheelEvent)
    43   NS_INTERFACE_MAP_ENTRY(nsIDOMWheelEvent)
    44 NS_INTERFACE_MAP_END_INHERITING(MouseEvent)
    46 NS_IMETHODIMP
    47 WheelEvent::InitWheelEvent(const nsAString& aType,
    48                            bool aCanBubble,
    49                            bool aCancelable,
    50                            nsIDOMWindow* aView,
    51                            int32_t aDetail,
    52                            int32_t aScreenX,
    53                            int32_t aScreenY,
    54                            int32_t aClientX,
    55                            int32_t aClientY, 
    56                            uint16_t aButton,
    57                            nsIDOMEventTarget* aRelatedTarget,
    58                            const nsAString& aModifiersList,
    59                            double aDeltaX,
    60                            double aDeltaY,
    61                            double aDeltaZ,
    62                            uint32_t aDeltaMode)
    63 {
    64   nsresult rv =
    65     MouseEvent::InitMouseEvent(aType, aCanBubble, aCancelable, aView, aDetail,
    66                                aScreenX, aScreenY, aClientX, aClientY, aButton,
    67                                aRelatedTarget, aModifiersList);
    68   NS_ENSURE_SUCCESS(rv, rv);
    70   WidgetWheelEvent* wheelEvent = mEvent->AsWheelEvent();
    71   wheelEvent->deltaX = aDeltaX;
    72   wheelEvent->deltaY = aDeltaY;
    73   wheelEvent->deltaZ = aDeltaZ;
    74   wheelEvent->deltaMode = aDeltaMode;
    76   return NS_OK;
    77 }
    79 double
    80 WheelEvent::DeltaX()
    81 {
    82   if (!mAppUnitsPerDevPixel) {
    83     return mEvent->AsWheelEvent()->deltaX;
    84   }
    85   return mEvent->AsWheelEvent()->deltaX *
    86     mAppUnitsPerDevPixel / nsPresContext::AppUnitsPerCSSPixel();
    87 }
    89 NS_IMETHODIMP
    90 WheelEvent::GetDeltaX(double* aDeltaX)
    91 {
    92   NS_ENSURE_ARG_POINTER(aDeltaX);
    94   *aDeltaX = DeltaX();
    95   return NS_OK;
    96 }
    98 double
    99 WheelEvent::DeltaY()
   100 {
   101   if (!mAppUnitsPerDevPixel) {
   102     return mEvent->AsWheelEvent()->deltaY;
   103   }
   104   return mEvent->AsWheelEvent()->deltaY *
   105     mAppUnitsPerDevPixel / nsPresContext::AppUnitsPerCSSPixel();
   106 }
   108 NS_IMETHODIMP
   109 WheelEvent::GetDeltaY(double* aDeltaY)
   110 {
   111   NS_ENSURE_ARG_POINTER(aDeltaY);
   113   *aDeltaY = DeltaY();
   114   return NS_OK;
   115 }
   117 double
   118 WheelEvent::DeltaZ()
   119 {
   120   if (!mAppUnitsPerDevPixel) {
   121     return mEvent->AsWheelEvent()->deltaZ;
   122   }
   123   return mEvent->AsWheelEvent()->deltaZ *
   124     mAppUnitsPerDevPixel / nsPresContext::AppUnitsPerCSSPixel();
   125 }
   127 NS_IMETHODIMP
   128 WheelEvent::GetDeltaZ(double* aDeltaZ)
   129 {
   130   NS_ENSURE_ARG_POINTER(aDeltaZ);
   132   *aDeltaZ = DeltaZ();
   133   return NS_OK;
   134 }
   136 uint32_t
   137 WheelEvent::DeltaMode()
   138 {
   139   return mEvent->AsWheelEvent()->deltaMode;
   140 }
   142 NS_IMETHODIMP
   143 WheelEvent::GetDeltaMode(uint32_t* aDeltaMode)
   144 {
   145   NS_ENSURE_ARG_POINTER(aDeltaMode);
   147   *aDeltaMode = DeltaMode();
   148   return NS_OK;
   149 }
   151 static void
   152 GetModifierList(bool aCtrl, bool aShift, bool aAlt, bool aMeta,
   153                 nsAString& aModifierList)
   154 {
   155   if (aCtrl) {
   156     aModifierList.AppendLiteral(NS_DOM_KEYNAME_CONTROL);
   157   }
   158   if (aShift) {
   159     if (!aModifierList.IsEmpty()) {
   160       aModifierList.AppendLiteral(" ");
   161     }
   162     aModifierList.AppendLiteral(NS_DOM_KEYNAME_SHIFT);
   163   }
   164   if (aAlt) {
   165     if (!aModifierList.IsEmpty()) {
   166       aModifierList.AppendLiteral(" ");
   167     }
   168     aModifierList.AppendLiteral(NS_DOM_KEYNAME_ALT);
   169   }
   170   if (aMeta) {
   171     if (!aModifierList.IsEmpty()) {
   172       aModifierList.AppendLiteral(" ");
   173     }
   174     aModifierList.AppendLiteral(NS_DOM_KEYNAME_META);
   175   }
   176 }
   178 already_AddRefed<WheelEvent>
   179 WheelEvent::Constructor(const GlobalObject& aGlobal,
   180                         const nsAString& aType,
   181                         const WheelEventInit& aParam,
   182                         ErrorResult& aRv)
   183 {
   184   nsCOMPtr<EventTarget> t = do_QueryInterface(aGlobal.GetAsSupports());
   185   nsRefPtr<WheelEvent> e = new WheelEvent(t, nullptr, nullptr);
   186   bool trusted = e->Init(t);
   187   nsAutoString modifierList;
   188   GetModifierList(aParam.mCtrlKey, aParam.mShiftKey,
   189                   aParam.mAltKey, aParam.mMetaKey,
   190                   modifierList);
   191   aRv = e->InitWheelEvent(aType, aParam.mBubbles, aParam.mCancelable,
   192                           aParam.mView, aParam.mDetail,
   193                           aParam.mScreenX, aParam.mScreenY,
   194                           aParam.mClientX, aParam.mClientY,
   195                           aParam.mButton, aParam.mRelatedTarget,
   196                           modifierList, aParam.mDeltaX,
   197                           aParam.mDeltaY, aParam.mDeltaZ, aParam.mDeltaMode);
   198   e->mEvent->AsWheelEvent()->buttons = aParam.mButtons;
   199   e->SetTrusted(trusted);
   200   return e.forget();
   201 }
   203 } // namespace dom
   204 } // namespace mozilla
   206 using namespace mozilla;
   207 using namespace mozilla::dom;
   209 nsresult
   210 NS_NewDOMWheelEvent(nsIDOMEvent** aInstancePtrResult,
   211                     EventTarget* aOwner,
   212                     nsPresContext* aPresContext,
   213                     WidgetWheelEvent* aEvent)
   214 {
   215   WheelEvent* it = new WheelEvent(aOwner, aPresContext, aEvent);
   216   NS_ADDREF(it);
   217   *aInstancePtrResult = static_cast<Event*>(it);
   218   return NS_OK;
   219 }

mercurial