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 #ifndef mozilla_PointerEventHandler_h
8 #define mozilla_PointerEventHandler_h
10 #include "mozilla/EventForwards.h"
11 #include "mozilla/MouseEvents.h"
12 #include "mozilla/TouchEvents.h"
13 #include "mozilla/WeakPtr.h"
15 // XXX Avoid including this here by moving function bodies to the cpp file
16 #include "mozilla/dom/Document.h"
17 #include "mozilla/dom/Element.h"
33 class PointerCaptureInfo final
{
35 RefPtr
<dom::Element
> mPendingElement
;
36 RefPtr
<dom::Element
> mOverrideElement
;
38 explicit PointerCaptureInfo(dom::Element
* aPendingElement
)
39 : mPendingElement(aPendingElement
) {
40 MOZ_COUNT_CTOR(PointerCaptureInfo
);
43 MOZ_COUNTED_DTOR(PointerCaptureInfo
)
45 bool Empty() { return !(mPendingElement
|| mOverrideElement
); }
48 class PointerInfo final
{
50 uint16_t mPointerType
;
53 bool mPreventMouseEventByContent
;
54 WeakPtr
<dom::Document
> mActiveDocument
;
55 explicit PointerInfo(bool aActiveState
, uint16_t aPointerType
,
56 bool aPrimaryState
, dom::Document
* aActiveDocument
)
57 : mPointerType(aPointerType
),
58 mActiveState(aActiveState
),
59 mPrimaryState(aPrimaryState
),
60 mPreventMouseEventByContent(false),
61 mActiveDocument(aActiveDocument
) {}
64 class PointerEventHandler final
{
66 // Called in nsLayoutStatics::Initialize/Shutdown to initialize pointer event
67 // related static variables.
68 static void InitializeStatics();
69 static void ReleaseStatics();
71 // Return the preference value of implicit capture.
72 static bool IsPointerEventImplicitCaptureForTouchEnabled();
74 // Called in ESM::PreHandleEvent to update current active pointers in a hash
76 static void UpdateActivePointerState(WidgetMouseEvent
* aEvent
,
77 nsIContent
* aTargetContent
= nullptr);
79 // Request/release pointer capture of the specified pointer by the element.
80 static void RequestPointerCaptureById(uint32_t aPointerId
,
81 dom::Element
* aElement
);
82 static void ReleasePointerCaptureById(uint32_t aPointerId
);
83 static void ReleaseAllPointerCapture();
85 // Set/release pointer capture of the specified pointer by the remote target.
86 // Should only be called in parent process.
87 static bool SetPointerCaptureRemoteTarget(uint32_t aPointerId
,
88 dom::BrowserParent
* aBrowserParent
);
89 static void ReleasePointerCaptureRemoteTarget(
90 dom::BrowserParent
* aBrowserParent
);
91 static void ReleasePointerCaptureRemoteTarget(uint32_t aPointerId
);
92 static void ReleaseAllPointerCaptureRemoteTarget();
94 // Get the pointer capturing remote target of the specified pointer.
95 static dom::BrowserParent
* GetPointerCapturingRemoteTarget(
98 // Get the pointer captured info of the specified pointer.
99 static PointerCaptureInfo
* GetPointerCaptureInfo(uint32_t aPointerId
);
101 // Return the PointerInfo if the pointer with aPointerId is situated in device
102 // , nullptr otherwise.
103 static const PointerInfo
* GetPointerInfo(uint32_t aPointerId
);
105 // CheckPointerCaptureState checks cases, when got/lostpointercapture events
108 static void MaybeProcessPointerCapture(WidgetGUIEvent
* aEvent
);
110 static void ProcessPointerCaptureForMouse(WidgetMouseEvent
* aEvent
);
112 static void ProcessPointerCaptureForTouch(WidgetTouchEvent
* aEvent
);
114 static void CheckPointerCaptureState(WidgetPointerEvent
* aEvent
);
116 // Implicitly get and release capture of current pointer for touch.
117 static void ImplicitlyCapturePointer(nsIFrame
* aFrame
, WidgetEvent
* aEvent
);
119 static void ImplicitlyReleasePointerCapture(WidgetEvent
* aEvent
);
120 MOZ_CAN_RUN_SCRIPT
static void MaybeImplicitlyReleasePointerCapture(
121 WidgetGUIEvent
* aEvent
);
124 * GetPointerCapturingContent returns a target element which captures the
125 * pointer. It's applied to mouse or pointer event (except mousedown and
126 * pointerdown). When capturing, return the element. Otherwise, nullptr.
128 * @param aEvent A mouse event or pointer event which may be
131 * @return Target element for aEvent.
133 static dom::Element
* GetPointerCapturingElement(WidgetGUIEvent
* aEvent
);
135 static dom::Element
* GetPointerCapturingElement(uint32_t aPointerId
);
137 // Release pointer capture if captured by the specified content or it's
138 // descendant. This is called to handle the case that the pointer capturing
139 // content or it's parent is removed from the document.
140 static void ReleaseIfCaptureByDescendant(nsIContent
* aContent
);
143 * This function handles the case when content had called preventDefault on
144 * the active pointer. In that case we have to prevent firing subsequent mouse
145 * to content. We check the flag PointerInfo::mPreventMouseEventByContent and
146 * call PreventDefault(false) to stop default behaviors and stop firing mouse
147 * events to content and chrome.
149 * note: mouse transition events are excluded
150 * note: we have to clean mPreventMouseEventByContent on pointerup for those
151 * devices support hover
152 * note: we don't suppress firing mouse events to chrome and system group
153 * handlers because they may implement default behaviors
155 static void PreHandlePointerEventsPreventDefault(
156 WidgetPointerEvent
* aPointerEvent
, WidgetGUIEvent
* aMouseOrTouchEvent
);
159 * This function handles the preventDefault behavior of pointerdown. When user
160 * preventDefault on pointerdown, We have to mark the active pointer to
161 * prevent sebsequent mouse events (except mouse transition events) and
164 * We add mPreventMouseEventByContent flag in PointerInfo to represent the
165 * active pointer won't firing compatible mouse events. It's set to true when
166 * content preventDefault on pointerdown
168 static void PostHandlePointerEventsPreventDefault(
169 WidgetPointerEvent
* aPointerEvent
, WidgetGUIEvent
* aMouseOrTouchEvent
);
172 * Dispatch a pointer event for aMouseOrTouchEvent to aEventTargetContent.
174 * @param aShell The PresShell which is handling the event.
175 * @param aEventTargetFrame The frame for aEventTargetContent.
176 * @param aEventTargetContent The event target node.
177 * @param aMouseOrTouchEvent A mouse or touch event.
178 * @param aDontRetargetEvents If true, this won't dispatch event with
179 * different PresShell from aShell. Otherwise,
180 * pointer events may be fired on different
181 * document if and only if aMouseOrTOuchEvent is a
182 * touch event except eTouchStart.
183 * @param aState [out] The result of the pointer event.
184 * @param aMouseOrTouchEventTarget
185 * [out] The event target for the following mouse
186 * or touch event. If aEventTargetContent has not
187 * been removed from the tree, this is always set
188 * to it. If aEventTargetContent is removed from
189 * the tree and aMouseOrTouchEvent is a mouse
190 * event, this is set to inclusive ancestor of
191 * aEventTargetContent which is still connected.
192 * If aEventTargetContent is removed from the tree
193 * and aMouseOrTouchEvent is a touch event, this is
194 * set to aEventTargetContent because touch event
195 * should be dispatched even on disconnected node.
196 * FIXME: If the event is a touch event but the
197 * message is not eTouchStart, this won't be set.
199 MOZ_CAN_RUN_SCRIPT
static void DispatchPointerFromMouseOrTouch(
200 PresShell
* aShell
, nsIFrame
* aEventTargetFrame
,
201 nsIContent
* aEventTargetContent
, WidgetGUIEvent
* aMouseOrTouchEvent
,
202 bool aDontRetargetEvents
, nsEventStatus
* aStatus
,
203 nsIContent
** aMouseOrTouchEventTarget
= nullptr);
205 static void InitPointerEventFromMouse(WidgetPointerEvent
* aPointerEvent
,
206 WidgetMouseEvent
* aMouseEvent
,
207 EventMessage aMessage
);
209 static void InitPointerEventFromTouch(WidgetPointerEvent
& aPointerEvent
,
210 const WidgetTouchEvent
& aTouchEvent
,
211 const mozilla::dom::Touch
& aTouch
,
214 static bool ShouldGeneratePointerEventFromMouse(WidgetGUIEvent
* aEvent
) {
215 return aEvent
->mMessage
== eMouseDown
|| aEvent
->mMessage
== eMouseUp
||
216 (aEvent
->mMessage
== eMouseMove
&&
217 aEvent
->AsMouseEvent()->IsReal()) ||
218 aEvent
->mMessage
== eMouseExitFromWidget
;
221 static bool ShouldGeneratePointerEventFromTouch(WidgetGUIEvent
* aEvent
) {
222 return aEvent
->mMessage
== eTouchStart
|| aEvent
->mMessage
== eTouchMove
||
223 aEvent
->mMessage
== eTouchEnd
|| aEvent
->mMessage
== eTouchCancel
||
224 aEvent
->mMessage
== eTouchPointerCancel
;
227 static MOZ_ALWAYS_INLINE
int32_t GetSpoofedPointerIdForRFP() {
228 return sSpoofedPointerId
.valueOr(0);
231 static void NotifyDestroyPresContext(nsPresContext
* aPresContext
);
233 static bool IsDragAndDropEnabled(WidgetMouseEvent
& aEvent
);
236 // Get proper pointer event message for a mouse or touch event.
237 static EventMessage
ToPointerEventMessage(
238 const WidgetGUIEvent
* aMouseOrTouchEvent
);
240 // Set pointer capture of the specified pointer by the element.
241 static void SetPointerCaptureById(uint32_t aPointerId
,
242 dom::Element
* aElement
);
244 // GetPointerType returns pointer type like mouse, pen or touch for pointer
245 // event with pointerId. The return value must be one of
246 // MouseEvent_Binding::MOZ_SOURCE_*
247 static uint16_t GetPointerType(uint32_t aPointerId
);
249 // GetPointerPrimaryState returns state of attribute isPrimary for pointer
250 // event with pointerId
251 static bool GetPointerPrimaryState(uint32_t aPointerId
);
254 static void DispatchGotOrLostPointerCaptureEvent(
255 bool aIsGotCapture
, const WidgetPointerEvent
* aPointerEvent
,
256 dom::Element
* aCaptureTarget
);
258 // The cached spoofed pointer ID for fingerprinting resistance. We will use a
259 // mouse pointer id for desktop. For mobile, we should use the touch pointer
260 // id as the spoofed one, and this work will be addressed in Bug 1492775.
261 static Maybe
<int32_t> sSpoofedPointerId
;
263 // A helper function to cache the pointer id of the spoofed interface, we
264 // would only cache the pointer id once. After that, we would always stick to
265 // that pointer id for fingerprinting resistance.
266 static void MaybeCacheSpoofedPointerID(uint16_t aInputSource
,
267 uint32_t aPointerId
);
270 } // namespace mozilla
272 #endif // mozilla_PointerEventHandler_h