2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2013-11-21 09:44:22 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*
|
|
|
|
* Portions Copyright 2013 Microsoft Open Technologies, Inc. */
|
|
|
|
|
2018-03-20 07:16:06 +03:00
|
|
|
#include "mozilla/dom/MouseEventBinding.h"
|
2014-02-27 14:51:15 +04:00
|
|
|
#include "mozilla/dom/PointerEvent.h"
|
2017-09-20 08:00:57 +03:00
|
|
|
#include "mozilla/dom/PointerEventBinding.h"
|
2013-11-21 09:44:22 +04:00
|
|
|
#include "mozilla/MouseEvents.h"
|
2018-10-09 14:55:43 +03:00
|
|
|
#include "nsContentUtils.h"
|
2013-11-21 09:44:22 +04:00
|
|
|
#include "prtime.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
PointerEvent::PointerEvent(EventTarget* aOwner, nsPresContext* aPresContext,
|
|
|
|
WidgetPointerEvent* aEvent)
|
2014-02-27 14:51:15 +04:00
|
|
|
: MouseEvent(aOwner, aPresContext,
|
2015-08-26 15:56:59 +03:00
|
|
|
aEvent ? aEvent
|
2015-08-29 02:58:26 +03:00
|
|
|
: new WidgetPointerEvent(false, eVoidEvent, nullptr)) {
|
2014-08-04 09:28:52 +04:00
|
|
|
NS_ASSERTION(mEvent->mClass == ePointerEventClass,
|
|
|
|
"event type mismatch ePointerEventClass");
|
2013-11-21 09:44:22 +04:00
|
|
|
|
|
|
|
WidgetMouseEvent* mouseEvent = mEvent->AsMouseEvent();
|
|
|
|
if (aEvent) {
|
|
|
|
mEventIsInternal = false;
|
|
|
|
} else {
|
|
|
|
mEventIsInternal = true;
|
2016-03-28 07:29:42 +03:00
|
|
|
mEvent->mTime = PR_Now();
|
2016-04-18 17:09:02 +03:00
|
|
|
mEvent->mRefPoint = LayoutDeviceIntPoint(0, 0);
|
2019-04-21 21:19:43 +03:00
|
|
|
mouseEvent->mInputSource = MouseEvent_Binding::MOZ_SOURCE_UNKNOWN;
|
2013-11-21 09:44:22 +04:00
|
|
|
}
|
2016-09-02 06:10:48 +03:00
|
|
|
// 5.2 Pointer Event types, for all pointer events, |detail| attribute SHOULD
|
|
|
|
// be 0.
|
|
|
|
mDetail = 0;
|
2013-11-21 09:44:22 +04:00
|
|
|
}
|
|
|
|
|
2017-09-20 08:00:57 +03:00
|
|
|
JSObject* PointerEvent::WrapObjectInternal(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) {
|
2018-06-26 00:20:54 +03:00
|
|
|
return PointerEvent_Binding::Wrap(aCx, this, aGivenProto);
|
2017-09-20 08:00:57 +03:00
|
|
|
}
|
|
|
|
|
2013-11-21 09:44:22 +04:00
|
|
|
static uint16_t ConvertStringToPointerType(const nsAString& aPointerTypeArg) {
|
|
|
|
if (aPointerTypeArg.EqualsLiteral("mouse")) {
|
2018-06-26 00:20:54 +03:00
|
|
|
return MouseEvent_Binding::MOZ_SOURCE_MOUSE;
|
2013-11-21 09:44:22 +04:00
|
|
|
}
|
|
|
|
if (aPointerTypeArg.EqualsLiteral("pen")) {
|
2018-06-26 00:20:54 +03:00
|
|
|
return MouseEvent_Binding::MOZ_SOURCE_PEN;
|
2013-11-21 09:44:22 +04:00
|
|
|
}
|
|
|
|
if (aPointerTypeArg.EqualsLiteral("touch")) {
|
2018-06-26 00:20:54 +03:00
|
|
|
return MouseEvent_Binding::MOZ_SOURCE_TOUCH;
|
2013-11-21 09:44:22 +04:00
|
|
|
}
|
|
|
|
|
2018-06-26 00:20:54 +03:00
|
|
|
return MouseEvent_Binding::MOZ_SOURCE_UNKNOWN;
|
2013-11-21 09:44:22 +04:00
|
|
|
}
|
|
|
|
|
2014-11-11 10:29:00 +03:00
|
|
|
void ConvertPointerTypeToString(uint16_t aPointerTypeSrc,
|
|
|
|
nsAString& aPointerTypeDest) {
|
|
|
|
switch (aPointerTypeSrc) {
|
2018-06-26 00:20:54 +03:00
|
|
|
case MouseEvent_Binding::MOZ_SOURCE_MOUSE:
|
2014-11-11 10:29:00 +03:00
|
|
|
aPointerTypeDest.AssignLiteral("mouse");
|
|
|
|
break;
|
2018-06-26 00:20:54 +03:00
|
|
|
case MouseEvent_Binding::MOZ_SOURCE_PEN:
|
2014-11-11 10:29:00 +03:00
|
|
|
aPointerTypeDest.AssignLiteral("pen");
|
|
|
|
break;
|
2018-06-26 00:20:54 +03:00
|
|
|
case MouseEvent_Binding::MOZ_SOURCE_TOUCH:
|
2014-11-11 10:29:00 +03:00
|
|
|
aPointerTypeDest.AssignLiteral("touch");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
aPointerTypeDest.Truncate();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-04 21:09:10 +04:00
|
|
|
// static
|
|
|
|
already_AddRefed<PointerEvent> PointerEvent::Constructor(
|
2013-11-21 09:44:22 +04:00
|
|
|
EventTarget* aOwner, const nsAString& aType,
|
2014-04-04 21:09:10 +04:00
|
|
|
const PointerEventInit& aParam) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PointerEvent> e = new PointerEvent(aOwner, nullptr, nullptr);
|
2014-04-04 21:09:10 +04:00
|
|
|
bool trusted = e->Init(aOwner);
|
2013-11-21 09:44:22 +04:00
|
|
|
|
2014-04-04 21:09:10 +04:00
|
|
|
e->InitMouseEvent(aType, aParam.mBubbles, aParam.mCancelable, aParam.mView,
|
|
|
|
aParam.mDetail, aParam.mScreenX, aParam.mScreenY,
|
|
|
|
aParam.mClientX, aParam.mClientY, false, false, false,
|
|
|
|
false, aParam.mButton, aParam.mRelatedTarget);
|
2015-05-19 23:26:02 +03:00
|
|
|
e->InitializeExtraMouseEventDictionaryMembers(aParam);
|
2013-11-21 09:44:22 +04:00
|
|
|
|
|
|
|
WidgetPointerEvent* widgetEvent = e->mEvent->AsPointerEvent();
|
|
|
|
widgetEvent->pointerId = aParam.mPointerId;
|
2016-08-03 11:18:04 +03:00
|
|
|
widgetEvent->mWidth = aParam.mWidth;
|
2016-08-03 11:23:56 +03:00
|
|
|
widgetEvent->mHeight = aParam.mHeight;
|
2019-04-21 21:19:18 +03:00
|
|
|
widgetEvent->mPressure = aParam.mPressure;
|
2017-02-09 05:49:00 +03:00
|
|
|
widgetEvent->tangentialPressure = aParam.mTangentialPressure;
|
2013-11-21 09:44:22 +04:00
|
|
|
widgetEvent->tiltX = aParam.mTiltX;
|
|
|
|
widgetEvent->tiltY = aParam.mTiltY;
|
2017-02-09 05:49:00 +03:00
|
|
|
widgetEvent->twist = aParam.mTwist;
|
2019-04-21 21:19:43 +03:00
|
|
|
widgetEvent->mInputSource = ConvertStringToPointerType(aParam.mPointerType);
|
2016-08-03 11:34:53 +03:00
|
|
|
widgetEvent->mIsPrimary = aParam.mIsPrimary;
|
2019-04-21 22:42:37 +03:00
|
|
|
widgetEvent->mButtons = aParam.mButtons;
|
2013-11-21 09:44:22 +04:00
|
|
|
|
2017-09-20 08:00:57 +03:00
|
|
|
if (!aParam.mCoalescedEvents.IsEmpty()) {
|
|
|
|
e->mCoalescedEvents.AppendElements(aParam.mCoalescedEvents);
|
|
|
|
}
|
2013-11-21 09:44:22 +04:00
|
|
|
e->SetTrusted(trusted);
|
2016-08-31 06:16:11 +03:00
|
|
|
e->SetComposed(aParam.mComposed);
|
2013-11-21 09:44:22 +04:00
|
|
|
return e.forget();
|
|
|
|
}
|
|
|
|
|
2014-04-04 21:09:10 +04:00
|
|
|
// static
|
|
|
|
already_AddRefed<PointerEvent> PointerEvent::Constructor(
|
|
|
|
const GlobalObject& aGlobal, const nsAString& aType,
|
|
|
|
const PointerEventInit& aParam, ErrorResult& aRv) {
|
|
|
|
nsCOMPtr<EventTarget> owner = do_QueryInterface(aGlobal.GetAsSupports());
|
|
|
|
return Constructor(owner, aType, aParam);
|
|
|
|
}
|
|
|
|
|
2017-09-20 08:00:57 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(PointerEvent)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(PointerEvent, MouseEvent)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mCoalescedEvents)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(PointerEvent, MouseEvent)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mCoalescedEvents)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(PointerEvent)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(MouseEvent)
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(PointerEvent, MouseEvent)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(PointerEvent, MouseEvent)
|
|
|
|
|
2018-10-09 14:55:43 +03:00
|
|
|
void PointerEvent::GetPointerType(nsAString& aPointerType,
|
|
|
|
CallerType aCallerType) {
|
|
|
|
if (ShouldResistFingerprinting(aCallerType)) {
|
|
|
|
aPointerType.AssignLiteral("mouse");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-21 21:19:43 +03:00
|
|
|
ConvertPointerTypeToString(mEvent->AsPointerEvent()->mInputSource,
|
2014-11-11 10:29:00 +03:00
|
|
|
aPointerType);
|
2013-11-21 09:44:22 +04:00
|
|
|
}
|
|
|
|
|
2018-11-20 12:54:52 +03:00
|
|
|
int32_t PointerEvent::PointerId(CallerType aCallerType) {
|
|
|
|
return ShouldResistFingerprinting(aCallerType)
|
|
|
|
? PointerEventHandler::GetSpoofedPointerIdForRFP()
|
|
|
|
: mEvent->AsPointerEvent()->pointerId;
|
2013-11-21 09:44:22 +04:00
|
|
|
}
|
|
|
|
|
2018-10-09 14:55:43 +03:00
|
|
|
int32_t PointerEvent::Width(CallerType aCallerType) {
|
|
|
|
return ShouldResistFingerprinting(aCallerType)
|
|
|
|
? 1
|
|
|
|
: mEvent->AsPointerEvent()->mWidth;
|
2013-11-21 09:44:22 +04:00
|
|
|
}
|
|
|
|
|
2018-10-09 14:55:43 +03:00
|
|
|
int32_t PointerEvent::Height(CallerType aCallerType) {
|
|
|
|
return ShouldResistFingerprinting(aCallerType)
|
|
|
|
? 1
|
|
|
|
: mEvent->AsPointerEvent()->mHeight;
|
2013-11-21 09:44:22 +04:00
|
|
|
}
|
|
|
|
|
2018-10-09 14:55:43 +03:00
|
|
|
float PointerEvent::Pressure(CallerType aCallerType) {
|
|
|
|
if (mEvent->mMessage == ePointerUp ||
|
|
|
|
!ShouldResistFingerprinting(aCallerType)) {
|
2019-04-21 21:19:18 +03:00
|
|
|
return mEvent->AsPointerEvent()->mPressure;
|
2018-10-09 14:55:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// According to [1], we should use 0.5 when it is in active buttons state and
|
|
|
|
// 0 otherwise for devices that don't support pressure. And a pointerup event
|
|
|
|
// always reports 0, so we don't need to spoof that.
|
|
|
|
//
|
|
|
|
// [1] https://www.w3.org/TR/pointerevents/#dom-pointerevent-pressure
|
|
|
|
float spoofedPressure = 0.0;
|
2019-04-21 22:42:37 +03:00
|
|
|
if (mEvent->AsPointerEvent()->mButtons) {
|
2018-10-09 14:55:43 +03:00
|
|
|
spoofedPressure = 0.5;
|
|
|
|
}
|
|
|
|
|
|
|
|
return spoofedPressure;
|
2013-11-21 09:44:22 +04:00
|
|
|
}
|
|
|
|
|
2018-10-09 14:55:43 +03:00
|
|
|
float PointerEvent::TangentialPressure(CallerType aCallerType) {
|
|
|
|
return ShouldResistFingerprinting(aCallerType)
|
|
|
|
? 0
|
|
|
|
: mEvent->AsPointerEvent()->tangentialPressure;
|
2017-02-09 05:49:00 +03:00
|
|
|
}
|
|
|
|
|
2018-10-09 14:55:43 +03:00
|
|
|
int32_t PointerEvent::TiltX(CallerType aCallerType) {
|
|
|
|
return ShouldResistFingerprinting(aCallerType)
|
|
|
|
? 0
|
|
|
|
: mEvent->AsPointerEvent()->tiltX;
|
2013-11-21 09:44:22 +04:00
|
|
|
}
|
|
|
|
|
2018-10-09 14:55:43 +03:00
|
|
|
int32_t PointerEvent::TiltY(CallerType aCallerType) {
|
|
|
|
return ShouldResistFingerprinting(aCallerType)
|
|
|
|
? 0
|
|
|
|
: mEvent->AsPointerEvent()->tiltY;
|
2013-11-21 09:44:22 +04:00
|
|
|
}
|
|
|
|
|
2018-10-09 14:55:43 +03:00
|
|
|
int32_t PointerEvent::Twist(CallerType aCallerType) {
|
|
|
|
return ShouldResistFingerprinting(aCallerType)
|
|
|
|
? 0
|
|
|
|
: mEvent->AsPointerEvent()->twist;
|
2017-02-09 05:49:00 +03:00
|
|
|
}
|
|
|
|
|
2016-08-03 11:34:53 +03:00
|
|
|
bool PointerEvent::IsPrimary() { return mEvent->AsPointerEvent()->mIsPrimary; }
|
2013-11-21 09:44:22 +04:00
|
|
|
|
2017-09-20 08:00:57 +03:00
|
|
|
void PointerEvent::GetCoalescedEvents(
|
|
|
|
nsTArray<RefPtr<PointerEvent>>& aPointerEvents) {
|
|
|
|
WidgetPointerEvent* widgetEvent = mEvent->AsPointerEvent();
|
|
|
|
if (mCoalescedEvents.IsEmpty() && widgetEvent &&
|
|
|
|
widgetEvent->mCoalescedWidgetEvents &&
|
|
|
|
!widgetEvent->mCoalescedWidgetEvents->mEvents.IsEmpty()) {
|
|
|
|
for (WidgetPointerEvent& event :
|
|
|
|
widgetEvent->mCoalescedWidgetEvents->mEvents) {
|
|
|
|
RefPtr<PointerEvent> domEvent =
|
|
|
|
NS_NewDOMPointerEvent(nullptr, nullptr, &event);
|
|
|
|
|
2017-11-08 06:42:04 +03:00
|
|
|
// The dom event is derived from an OS generated widget event. Setup
|
|
|
|
// mWidget and mPresContext since they are necessary to calculate
|
|
|
|
// offsetX / offsetY.
|
|
|
|
domEvent->mEvent->AsGUIEvent()->mWidget = widgetEvent->mWidget;
|
|
|
|
domEvent->mPresContext = mPresContext;
|
|
|
|
|
2017-09-20 08:00:57 +03:00
|
|
|
// The coalesced widget mouse events shouldn't have been dispatched.
|
|
|
|
MOZ_ASSERT(!domEvent->mEvent->mTarget);
|
|
|
|
// The event target should be the same as the dispatched event's target.
|
|
|
|
domEvent->mEvent->mTarget = mEvent->mTarget;
|
|
|
|
|
|
|
|
// JS could hold reference to dom events. We have to ask dom event to
|
|
|
|
// duplicate its private data to avoid the widget event is destroyed.
|
|
|
|
domEvent->DuplicatePrivateData();
|
2017-11-08 06:42:04 +03:00
|
|
|
|
|
|
|
// Setup mPresContext again after DuplicatePrivateData since it clears
|
|
|
|
// mPresContext.
|
|
|
|
domEvent->mPresContext = mPresContext;
|
2017-09-20 08:00:57 +03:00
|
|
|
mCoalescedEvents.AppendElement(domEvent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (mEvent->mTarget) {
|
|
|
|
for (RefPtr<PointerEvent>& pointerEvent : mCoalescedEvents) {
|
|
|
|
// Only set event target when it's null.
|
|
|
|
if (!pointerEvent->mEvent->mTarget) {
|
|
|
|
pointerEvent->mEvent->mTarget = mEvent->mTarget;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
aPointerEvents.AppendElements(mCoalescedEvents);
|
|
|
|
}
|
|
|
|
|
2018-10-09 14:55:43 +03:00
|
|
|
bool PointerEvent::ShouldResistFingerprinting(CallerType aCallerType) {
|
|
|
|
// There are four situations we don't need to spoof this pointer event.
|
|
|
|
// 1. This event is generated by scripts.
|
|
|
|
// 2. This event is a mouse pointer event.
|
|
|
|
// 3. The caller type is system.
|
|
|
|
// 4. The pref privcy.resistFingerprinting' is false, we fast return here
|
|
|
|
// since we don't need to do any QI of following codes.
|
|
|
|
// We don't need to check for the system group since pointer events won't be
|
|
|
|
// dispatched to the system group.
|
|
|
|
if (!mEvent->IsTrusted() || aCallerType == CallerType::System ||
|
|
|
|
!nsContentUtils::ShouldResistFingerprinting() ||
|
2019-04-21 21:19:43 +03:00
|
|
|
mEvent->AsPointerEvent()->mInputSource ==
|
2018-10-09 14:55:43 +03:00
|
|
|
MouseEvent_Binding::MOZ_SOURCE_MOUSE) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-01-02 16:05:23 +03:00
|
|
|
nsCOMPtr<Document> doc = GetDocument();
|
2018-10-09 14:55:43 +03:00
|
|
|
|
|
|
|
return doc && !nsContentUtils::IsChromeDoc(doc);
|
|
|
|
}
|
|
|
|
|
2013-11-21 09:44:22 +04:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
using namespace mozilla;
|
2014-02-27 14:51:15 +04:00
|
|
|
using namespace mozilla::dom;
|
2013-11-21 09:44:22 +04:00
|
|
|
|
2015-08-12 14:39:31 +03:00
|
|
|
already_AddRefed<PointerEvent> NS_NewDOMPointerEvent(
|
2014-02-27 14:51:15 +04:00
|
|
|
EventTarget* aOwner, nsPresContext* aPresContext,
|
|
|
|
WidgetPointerEvent* aEvent) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PointerEvent> it = new PointerEvent(aOwner, aPresContext, aEvent);
|
2015-08-12 14:39:31 +03:00
|
|
|
return it.forget();
|
2013-11-21 09:44:22 +04:00
|
|
|
}
|