dom/events/TouchEvent.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.

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #include "mozilla/dom/TouchEvent.h"
michael@0 8 #include "mozilla/dom/Touch.h"
michael@0 9 #include "mozilla/dom/TouchListBinding.h"
michael@0 10 #include "mozilla/Preferences.h"
michael@0 11 #include "mozilla/TouchEvents.h"
michael@0 12 #include "nsContentUtils.h"
michael@0 13
michael@0 14 namespace mozilla {
michael@0 15
michael@0 16 #ifdef XP_WIN
michael@0 17 namespace widget {
michael@0 18 extern int32_t IsTouchDeviceSupportPresent();
michael@0 19 } // namespace widget
michael@0 20 #endif // #ifdef XP_WIN
michael@0 21
michael@0 22 namespace dom {
michael@0 23
michael@0 24 /******************************************************************************
michael@0 25 * TouchList
michael@0 26 *****************************************************************************/
michael@0 27
michael@0 28 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(TouchList)
michael@0 29 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
michael@0 30 NS_INTERFACE_MAP_ENTRY(nsISupports)
michael@0 31 NS_INTERFACE_MAP_END
michael@0 32
michael@0 33 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_2(TouchList, mParent, mPoints)
michael@0 34
michael@0 35 NS_IMPL_CYCLE_COLLECTING_ADDREF(TouchList)
michael@0 36 NS_IMPL_CYCLE_COLLECTING_RELEASE(TouchList)
michael@0 37
michael@0 38 JSObject*
michael@0 39 TouchList::WrapObject(JSContext* aCx)
michael@0 40 {
michael@0 41 return TouchListBinding::Wrap(aCx, this);
michael@0 42 }
michael@0 43
michael@0 44 // static
michael@0 45 bool
michael@0 46 TouchList::PrefEnabled(JSContext* aCx, JSObject* aGlobal)
michael@0 47 {
michael@0 48 return TouchEvent::PrefEnabled(aCx, aGlobal);
michael@0 49 }
michael@0 50
michael@0 51 Touch*
michael@0 52 TouchList::IdentifiedTouch(int32_t aIdentifier) const
michael@0 53 {
michael@0 54 for (uint32_t i = 0; i < mPoints.Length(); ++i) {
michael@0 55 Touch* point = mPoints[i];
michael@0 56 if (point && point->Identifier() == aIdentifier) {
michael@0 57 return point;
michael@0 58 }
michael@0 59 }
michael@0 60 return nullptr;
michael@0 61 }
michael@0 62
michael@0 63 /******************************************************************************
michael@0 64 * TouchEvent
michael@0 65 *****************************************************************************/
michael@0 66
michael@0 67 TouchEvent::TouchEvent(EventTarget* aOwner,
michael@0 68 nsPresContext* aPresContext,
michael@0 69 WidgetTouchEvent* aEvent)
michael@0 70 : UIEvent(aOwner, aPresContext,
michael@0 71 aEvent ? aEvent : new WidgetTouchEvent(false, 0, nullptr))
michael@0 72 {
michael@0 73 if (aEvent) {
michael@0 74 mEventIsInternal = false;
michael@0 75
michael@0 76 for (uint32_t i = 0; i < aEvent->touches.Length(); ++i) {
michael@0 77 Touch* touch = aEvent->touches[i];
michael@0 78 touch->InitializePoints(mPresContext, aEvent);
michael@0 79 }
michael@0 80 } else {
michael@0 81 mEventIsInternal = true;
michael@0 82 mEvent->time = PR_Now();
michael@0 83 }
michael@0 84 }
michael@0 85
michael@0 86 NS_IMPL_CYCLE_COLLECTION_INHERITED(TouchEvent, UIEvent,
michael@0 87 mTouches,
michael@0 88 mTargetTouches,
michael@0 89 mChangedTouches)
michael@0 90
michael@0 91 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(TouchEvent)
michael@0 92 NS_INTERFACE_MAP_END_INHERITING(UIEvent)
michael@0 93
michael@0 94 NS_IMPL_ADDREF_INHERITED(TouchEvent, UIEvent)
michael@0 95 NS_IMPL_RELEASE_INHERITED(TouchEvent, UIEvent)
michael@0 96
michael@0 97 void
michael@0 98 TouchEvent::InitTouchEvent(const nsAString& aType,
michael@0 99 bool aCanBubble,
michael@0 100 bool aCancelable,
michael@0 101 nsIDOMWindow* aView,
michael@0 102 int32_t aDetail,
michael@0 103 bool aCtrlKey,
michael@0 104 bool aAltKey,
michael@0 105 bool aShiftKey,
michael@0 106 bool aMetaKey,
michael@0 107 TouchList* aTouches,
michael@0 108 TouchList* aTargetTouches,
michael@0 109 TouchList* aChangedTouches,
michael@0 110 ErrorResult& aRv)
michael@0 111 {
michael@0 112 aRv = UIEvent::InitUIEvent(aType, aCanBubble, aCancelable, aView, aDetail);
michael@0 113 if (aRv.Failed()) {
michael@0 114 return;
michael@0 115 }
michael@0 116
michael@0 117 mEvent->AsInputEvent()->InitBasicModifiers(aCtrlKey, aAltKey,
michael@0 118 aShiftKey, aMetaKey);
michael@0 119 mTouches = aTouches;
michael@0 120 mTargetTouches = aTargetTouches;
michael@0 121 mChangedTouches = aChangedTouches;
michael@0 122 }
michael@0 123
michael@0 124 TouchList*
michael@0 125 TouchEvent::Touches()
michael@0 126 {
michael@0 127 if (!mTouches) {
michael@0 128 WidgetTouchEvent* touchEvent = mEvent->AsTouchEvent();
michael@0 129 if (mEvent->message == NS_TOUCH_END || mEvent->message == NS_TOUCH_CANCEL) {
michael@0 130 // for touchend events, remove any changed touches from the touches array
michael@0 131 nsTArray< nsRefPtr<Touch> > unchangedTouches;
michael@0 132 const nsTArray< nsRefPtr<Touch> >& touches = touchEvent->touches;
michael@0 133 for (uint32_t i = 0; i < touches.Length(); ++i) {
michael@0 134 if (!touches[i]->mChanged) {
michael@0 135 unchangedTouches.AppendElement(touches[i]);
michael@0 136 }
michael@0 137 }
michael@0 138 mTouches = new TouchList(ToSupports(this), unchangedTouches);
michael@0 139 } else {
michael@0 140 mTouches = new TouchList(ToSupports(this), touchEvent->touches);
michael@0 141 }
michael@0 142 }
michael@0 143 return mTouches;
michael@0 144 }
michael@0 145
michael@0 146 TouchList*
michael@0 147 TouchEvent::TargetTouches()
michael@0 148 {
michael@0 149 if (!mTargetTouches) {
michael@0 150 nsTArray< nsRefPtr<Touch> > targetTouches;
michael@0 151 WidgetTouchEvent* touchEvent = mEvent->AsTouchEvent();
michael@0 152 const nsTArray< nsRefPtr<Touch> >& touches = touchEvent->touches;
michael@0 153 for (uint32_t i = 0; i < touches.Length(); ++i) {
michael@0 154 // for touchend/cancel events, don't append to the target list if this is a
michael@0 155 // touch that is ending
michael@0 156 if ((mEvent->message != NS_TOUCH_END &&
michael@0 157 mEvent->message != NS_TOUCH_CANCEL) || !touches[i]->mChanged) {
michael@0 158 if (touches[i]->mTarget == mEvent->originalTarget) {
michael@0 159 targetTouches.AppendElement(touches[i]);
michael@0 160 }
michael@0 161 }
michael@0 162 }
michael@0 163 mTargetTouches = new TouchList(ToSupports(this), targetTouches);
michael@0 164 }
michael@0 165 return mTargetTouches;
michael@0 166 }
michael@0 167
michael@0 168 TouchList*
michael@0 169 TouchEvent::ChangedTouches()
michael@0 170 {
michael@0 171 if (!mChangedTouches) {
michael@0 172 nsTArray< nsRefPtr<Touch> > changedTouches;
michael@0 173 WidgetTouchEvent* touchEvent = mEvent->AsTouchEvent();
michael@0 174 const nsTArray< nsRefPtr<Touch> >& touches = touchEvent->touches;
michael@0 175 for (uint32_t i = 0; i < touches.Length(); ++i) {
michael@0 176 if (touches[i]->mChanged) {
michael@0 177 changedTouches.AppendElement(touches[i]);
michael@0 178 }
michael@0 179 }
michael@0 180 mChangedTouches = new TouchList(ToSupports(this), changedTouches);
michael@0 181 }
michael@0 182 return mChangedTouches;
michael@0 183 }
michael@0 184
michael@0 185 // static
michael@0 186 bool
michael@0 187 TouchEvent::PrefEnabled(JSContext* aCx, JSObject* aGlobal)
michael@0 188 {
michael@0 189 bool prefValue = false;
michael@0 190 int32_t flag = 0;
michael@0 191 if (NS_SUCCEEDED(Preferences::GetInt("dom.w3c_touch_events.enabled",
michael@0 192 &flag))) {
michael@0 193 if (flag == 2) {
michael@0 194 #ifdef XP_WIN
michael@0 195 static bool sDidCheckTouchDeviceSupport = false;
michael@0 196 static bool sIsTouchDeviceSupportPresent = false;
michael@0 197 // On Windows we auto-detect based on device support.
michael@0 198 if (!sDidCheckTouchDeviceSupport) {
michael@0 199 sDidCheckTouchDeviceSupport = true;
michael@0 200 sIsTouchDeviceSupportPresent = widget::IsTouchDeviceSupportPresent();
michael@0 201 }
michael@0 202 prefValue = sIsTouchDeviceSupportPresent;
michael@0 203 #else
michael@0 204 NS_WARNING("dom.w3c_touch_events.enabled=2 not implemented!");
michael@0 205 prefValue = false;
michael@0 206 #endif
michael@0 207 } else {
michael@0 208 prefValue = !!flag;
michael@0 209 }
michael@0 210 }
michael@0 211 if (prefValue) {
michael@0 212 nsContentUtils::InitializeTouchEventTable();
michael@0 213 }
michael@0 214 return prefValue;
michael@0 215 }
michael@0 216
michael@0 217 bool
michael@0 218 TouchEvent::AltKey()
michael@0 219 {
michael@0 220 return mEvent->AsTouchEvent()->IsAlt();
michael@0 221 }
michael@0 222
michael@0 223 bool
michael@0 224 TouchEvent::MetaKey()
michael@0 225 {
michael@0 226 return mEvent->AsTouchEvent()->IsMeta();
michael@0 227 }
michael@0 228
michael@0 229 bool
michael@0 230 TouchEvent::CtrlKey()
michael@0 231 {
michael@0 232 return mEvent->AsTouchEvent()->IsControl();
michael@0 233 }
michael@0 234
michael@0 235 bool
michael@0 236 TouchEvent::ShiftKey()
michael@0 237 {
michael@0 238 return mEvent->AsTouchEvent()->IsShift();
michael@0 239 }
michael@0 240
michael@0 241 } // namespace dom
michael@0 242 } // namespace mozilla
michael@0 243
michael@0 244 using namespace mozilla;
michael@0 245 using namespace mozilla::dom;
michael@0 246
michael@0 247 nsresult
michael@0 248 NS_NewDOMTouchEvent(nsIDOMEvent** aInstancePtrResult,
michael@0 249 EventTarget* aOwner,
michael@0 250 nsPresContext* aPresContext,
michael@0 251 WidgetTouchEvent* aEvent)
michael@0 252 {
michael@0 253 TouchEvent* it = new TouchEvent(aOwner, aPresContext, aEvent);
michael@0 254 NS_ADDREF(it);
michael@0 255 *aInstancePtrResult = static_cast<Event*>(it);
michael@0 256 return NS_OK;
michael@0 257 }

mercurial