Bug 1688354 [wpt PR 27298] - Treat 'rem' as an absolute unit for font size, a=testonly
[gecko.git] / dom / events / Touch.cpp
blobdd68c651cc5ce0309d4f895c85f9c552de71f43e
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=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
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/dom/Touch.h"
9 #include "mozilla/dom/EventTarget.h"
10 #include "mozilla/dom/TouchEvent.h"
11 #include "nsGlobalWindow.h"
12 #include "nsContentUtils.h"
13 #include "nsIContent.h"
15 namespace mozilla::dom {
17 // static
18 already_AddRefed<Touch> Touch::Constructor(const GlobalObject& aGlobal,
19 const TouchInit& aParam) {
20 // Annoyingly many parameters, make sure the ordering is the same as in the
21 // Touch constructor.
22 RefPtr<Touch> touch = new Touch(
23 aParam.mTarget, aParam.mIdentifier, aParam.mPageX, aParam.mPageY,
24 aParam.mScreenX, aParam.mScreenY, aParam.mClientX, aParam.mClientY,
25 aParam.mRadiusX, aParam.mRadiusY, aParam.mRotationAngle, aParam.mForce);
26 return touch.forget();
29 Touch::Touch(EventTarget* aTarget, int32_t aIdentifier, int32_t aPageX,
30 int32_t aPageY, int32_t aScreenX, int32_t aScreenY,
31 int32_t aClientX, int32_t aClientY, int32_t aRadiusX,
32 int32_t aRadiusY, float aRotationAngle, float aForce)
33 : mIsTouchEventSuppressed(false) {
34 mTarget = aTarget;
35 mOriginalTarget = aTarget;
36 mIdentifier = aIdentifier;
37 mPagePoint = CSSIntPoint(aPageX, aPageY);
38 mScreenPoint = CSSIntPoint(aScreenX, aScreenY);
39 mClientPoint = CSSIntPoint(aClientX, aClientY);
40 mRefPoint = LayoutDeviceIntPoint(0, 0);
41 mPointsInitialized = true;
42 mRadius.x = aRadiusX;
43 mRadius.y = aRadiusY;
44 mRotationAngle = aRotationAngle;
45 mForce = aForce;
47 mChanged = false;
48 mMessage = 0;
49 nsJSContext::LikelyShortLivingObjectCreated();
52 Touch::Touch(int32_t aIdentifier, LayoutDeviceIntPoint aPoint,
53 LayoutDeviceIntPoint aRadius, float aRotationAngle, float aForce)
54 : mIsTouchEventSuppressed(false) {
55 mIdentifier = aIdentifier;
56 mPagePoint = CSSIntPoint(0, 0);
57 mScreenPoint = CSSIntPoint(0, 0);
58 mClientPoint = CSSIntPoint(0, 0);
59 mRefPoint = aPoint;
60 mPointsInitialized = false;
61 mRadius = aRadius;
62 mRotationAngle = aRotationAngle;
63 mForce = aForce;
65 mChanged = false;
66 mMessage = 0;
67 nsJSContext::LikelyShortLivingObjectCreated();
70 Touch::Touch(const Touch& aOther)
71 : mOriginalTarget(aOther.mOriginalTarget),
72 mTarget(aOther.mTarget),
73 mRefPoint(aOther.mRefPoint),
74 mChanged(aOther.mChanged),
75 mIsTouchEventSuppressed(aOther.mIsTouchEventSuppressed),
76 mMessage(aOther.mMessage),
77 mIdentifier(aOther.mIdentifier),
78 mPagePoint(aOther.mPagePoint),
79 mClientPoint(aOther.mClientPoint),
80 mScreenPoint(aOther.mScreenPoint),
81 mRadius(aOther.mRadius),
82 mRotationAngle(aOther.mRotationAngle),
83 mForce(aOther.mForce),
84 mPointsInitialized(aOther.mPointsInitialized) {
85 nsJSContext::LikelyShortLivingObjectCreated();
88 Touch::~Touch() = default;
90 // static
91 bool Touch::PrefEnabled(JSContext* aCx, JSObject* aGlobal) {
92 return TouchEvent::PrefEnabled(aCx, aGlobal);
95 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(Touch, mTarget, mOriginalTarget)
97 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Touch)
98 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
99 NS_INTERFACE_MAP_ENTRY(nsISupports)
100 NS_INTERFACE_MAP_END
102 NS_IMPL_CYCLE_COLLECTING_ADDREF(Touch)
103 NS_IMPL_CYCLE_COLLECTING_RELEASE(Touch)
105 EventTarget* Touch::GetTarget() const {
106 nsCOMPtr<nsIContent> content = do_QueryInterface(mTarget);
107 if (content && content->ChromeOnlyAccess() &&
108 !nsContentUtils::LegacyIsCallerNativeCode() &&
109 !nsContentUtils::CanAccessNativeAnon()) {
110 return content->FindFirstNonChromeOnlyAccessContent();
113 return mTarget;
116 int32_t Touch::ScreenX(CallerType aCallerType) const {
117 if (nsContentUtils::ResistFingerprinting(aCallerType)) {
118 return ClientX();
121 return mScreenPoint.x;
124 int32_t Touch::ScreenY(CallerType aCallerType) const {
125 if (nsContentUtils::ResistFingerprinting(aCallerType)) {
126 return ClientY();
129 return mScreenPoint.y;
132 int32_t Touch::RadiusX(CallerType aCallerType) const {
133 if (nsContentUtils::ResistFingerprinting(aCallerType)) {
134 return 0;
137 return mRadius.x;
140 int32_t Touch::RadiusY(CallerType aCallerType) const {
141 if (nsContentUtils::ResistFingerprinting(aCallerType)) {
142 return 0;
145 return mRadius.y;
148 float Touch::RotationAngle(CallerType aCallerType) const {
149 if (nsContentUtils::ResistFingerprinting(aCallerType)) {
150 return 0.0f;
153 return mRotationAngle;
156 float Touch::Force(CallerType aCallerType) const {
157 if (nsContentUtils::ResistFingerprinting(aCallerType)) {
158 return 0.0f;
161 return mForce;
164 void Touch::InitializePoints(nsPresContext* aPresContext, WidgetEvent* aEvent) {
165 if (mPointsInitialized) {
166 return;
168 mClientPoint =
169 Event::GetClientCoords(aPresContext, aEvent, mRefPoint, mClientPoint);
170 mPagePoint =
171 Event::GetPageCoords(aPresContext, aEvent, mRefPoint, mClientPoint);
172 mScreenPoint = Event::GetScreenCoords(aPresContext, aEvent, mRefPoint);
173 mPointsInitialized = true;
176 void Touch::SetTouchTarget(EventTarget* aTarget) {
177 mOriginalTarget = aTarget;
178 mTarget = aTarget;
181 bool Touch::Equals(Touch* aTouch) const {
182 return mRefPoint == aTouch->mRefPoint && mForce == aTouch->mForce &&
183 mRotationAngle == aTouch->mRotationAngle &&
184 mRadius.x == aTouch->mRadius.x && mRadius.y == aTouch->mRadius.y;
187 JSObject* Touch::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) {
188 return Touch_Binding::Wrap(aCx, this, aGivenProto);
191 // Parent ourselves to the global of the target. This achieves the desirable
192 // effects of parenting to the target, but avoids making the touch inaccessible
193 // when the target happens to be NAC and therefore reflected into the XBL scope.
194 nsIGlobalObject* Touch::GetParentObject() {
195 if (!mOriginalTarget) {
196 return nullptr;
198 return mOriginalTarget->GetOwnerGlobal();
201 } // namespace mozilla::dom