2 * Copyright (C) 2019 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include "PointerCaptureController.h"
28 #if ENABLE(POINTER_EVENTS)
32 #include "EventHandler.h"
33 #include "EventNames.h"
34 #include "EventTarget.h"
36 #include "PointerEvent.h"
40 PointerCaptureController::PointerCaptureController(Page& page)
45 ExceptionOr<void> PointerCaptureController::setPointerCapture(Element* capturingTarget, int32_t pointerId)
47 // https://w3c.github.io/pointerevents/#setting-pointer-capture
49 // 1. If the pointerId provided as the method's argument does not match any of the active pointers, then throw a DOMException with the name NotFoundError.
50 auto iterator = m_activePointerIdsToCapturingData.find(pointerId);
51 if (iterator == m_activePointerIdsToCapturingData.end())
52 return Exception { NotFoundError };
54 // 2. If the Element on which this method is invoked is not connected, throw an exception with the name InvalidStateError.
55 if (!capturingTarget->isConnected())
56 return Exception { InvalidStateError };
58 #if ENABLE(POINTER_LOCK)
59 // 3. If this method is invoked while the document has a locked element, throw an exception with the name InvalidStateError.
60 if (auto* page = capturingTarget->document().page()) {
61 if (page->pointerLockController().isLocked())
62 return Exception { InvalidStateError };
66 // 4. If the pointer is not in the active buttons state, then terminate these steps.
67 // FIXME: implement when we support mouse events.
69 // 5. For the specified pointerId, set the pending pointer capture target override to the Element on which this method was invoked.
70 iterator->value.pendingTargetOverride = capturingTarget;
75 ExceptionOr<void> PointerCaptureController::releasePointerCapture(Element* capturingTarget, int32_t pointerId)
77 // https://w3c.github.io/pointerevents/#releasing-pointer-capture
79 // Pointer capture is released on an element explicitly by calling the element.releasePointerCapture(pointerId) method.
80 // When this method is called, a user agent MUST run the following steps:
82 // 1. If the pointerId provided as the method's argument does not match any of the active pointers and these steps are not
83 // being invoked as a result of the implicit release of pointer capture, then throw a DOMException with the name NotFoundError.
84 auto iterator = m_activePointerIdsToCapturingData.find(pointerId);
85 if (iterator == m_activePointerIdsToCapturingData.end())
86 return Exception { NotFoundError };
88 // 2. If hasPointerCapture is false for the Element with the specified pointerId, then terminate these steps.
89 if (!hasPointerCapture(capturingTarget, pointerId))
92 // 3. For the specified pointerId, clear the pending pointer capture target override, if set.
93 iterator->value.pendingTargetOverride = nullptr;
98 bool PointerCaptureController::hasPointerCapture(Element* capturingTarget, int32_t pointerId)
100 // https://w3c.github.io/pointerevents/#dom-element-haspointercapture
102 // Indicates whether the element on which this method is invoked has pointer capture for the pointer identified by the argument pointerId.
103 // In particular, returns true if the pending pointer capture target override for pointerId is set to the element on which this method is
104 // invoked, and false otherwise.
106 auto iterator = m_activePointerIdsToCapturingData.find(pointerId);
107 if (iterator == m_activePointerIdsToCapturingData.end())
110 auto& capturingData = iterator->value;
111 return capturingData.pendingTargetOverride == capturingTarget || capturingData.targetOverride == capturingTarget;
114 void PointerCaptureController::pointerLockWasApplied()
116 // https://w3c.github.io/pointerevents/#implicit-release-of-pointer-capture
118 // When a pointer lock is successfully applied on an element, a user agent MUST run the steps as if the releasePointerCapture()
119 // method has been called if any element is set to be captured or pending to be captured.
120 for (auto& capturingData : m_activePointerIdsToCapturingData.values()) {
121 capturingData.pendingTargetOverride = nullptr;
122 capturingData.targetOverride = nullptr;
126 void PointerCaptureController::touchEndedOrWasCancelledForIdentifier(int32_t pointerId)
128 m_activePointerIdsToCapturingData.remove(pointerId);
131 bool PointerCaptureController::hasCancelledPointerEventForIdentifier(int32_t pointerId)
133 auto iterator = m_activePointerIdsToCapturingData.find(pointerId);
134 return iterator != m_activePointerIdsToCapturingData.end() && iterator->value.cancelled;
137 void PointerCaptureController::pointerEventWillBeDispatched(const PointerEvent& event, EventTarget* target)
139 // https://w3c.github.io/pointerevents/#implicit-pointer-capture
141 // Some input devices (such as touchscreens) implement a "direct manipulation" metaphor where a pointer is intended to act primarily on the UI
142 // element it became active upon (providing a physical illusion of direct contact, instead of indirect contact via a cursor that conceptually
143 // floats above the UI). Such devices are identified by the InputDeviceCapabilities.pointerMovementScrolls property and should have "implicit
144 // pointer capture" behavior as follows.
146 // Direct manipulation devices should behave exactly as if setPointerCapture was called on the target element just before the invocation of any
147 // pointerdown listeners. The hasPointerCapture API may be used (eg. within any pointerdown listener) to determine whether this has occurred. If
148 // releasePointerCapture is not called for the pointer before the next pointer event is fired, then a gotpointercapture event will be dispatched
149 // to the target (as normal) indicating that capture is active.
151 if (!is<Element>(target) || event.type() != eventNames().pointerdownEvent)
154 auto pointerId = event.pointerId();
155 CapturingData capturingData;
156 capturingData.pointerType = event.pointerType();
157 m_activePointerIdsToCapturingData.set(pointerId, capturingData);
158 setPointerCapture(downcast<Element>(target), pointerId);
161 void PointerCaptureController::pointerEventWasDispatched(const PointerEvent& event)
163 // https://w3c.github.io/pointerevents/#implicit-release-of-pointer-capture
165 auto iterator = m_activePointerIdsToCapturingData.find(event.pointerId());
166 if (iterator != m_activePointerIdsToCapturingData.end()) {
167 auto& capturingData = iterator->value;
168 // Immediately after firing the pointerup or pointercancel events, a user agent MUST clear the pending pointer capture target
169 // override for the pointerId of the pointerup or pointercancel event that was just dispatched, and then run Process Pending
170 // Pointer Capture steps to fire lostpointercapture if necessary.
171 if (event.type() == eventNames().pointerupEvent)
172 capturingData.pendingTargetOverride = nullptr;
174 // When the pointer capture target override is no longer connected, the pending pointer capture target override and pointer
175 // capture target override nodes SHOULD be cleared and also a PointerEvent named lostpointercapture corresponding to the captured
176 // pointer SHOULD be fired at the document.
177 if (capturingData.targetOverride && !capturingData.targetOverride->isConnected()) {
178 capturingData.pendingTargetOverride = nullptr;
179 capturingData.targetOverride = nullptr;
183 processPendingPointerCapture(event);
186 void PointerCaptureController::cancelPointer(int32_t pointerId, const IntPoint& documentPoint)
188 // https://w3c.github.io/pointerevents/#the-pointercancel-event
190 // A user agent MUST fire a pointer event named pointercancel in the following circumstances:
192 // The user agent has determined that a pointer is unlikely to continue to produce events (for example, because of a hardware event).
193 // After having fired the pointerdown event, if the pointer is subsequently used to manipulate the page viewport (e.g. panning or zooming).
194 // Immediately before drag operation starts [HTML], for the pointer that caused the drag operation.
195 // After firing the pointercancel event, a user agent MUST also fire a pointer event named pointerout followed by firing a pointer event named pointerleave.
197 // https://w3c.github.io/pointerevents/#implicit-release-of-pointer-capture
199 // Immediately after firing the pointerup or pointercancel events, a user agent MUST clear the pending pointer capture target
200 // override for the pointerId of the pointerup or pointercancel event that was just dispatched, and then run Process Pending
201 // Pointer Capture steps to fire lostpointercapture if necessary. After running Process Pending Pointer Capture steps, if the
202 // pointer supports hover, user agent MUST also send corresponding boundary events necessary to reflect the current position of
203 // the pointer with no capture.
205 auto iterator = m_activePointerIdsToCapturingData.find(pointerId);
206 if (iterator == m_activePointerIdsToCapturingData.end())
209 auto& capturingData = iterator->value;
210 if (capturingData.cancelled)
213 capturingData.pendingTargetOverride = nullptr;
214 capturingData.cancelled = true;
216 auto& target = capturingData.targetOverride;
218 target = m_page.mainFrame().eventHandler().hitTestResultAtPoint(documentPoint, HitTestRequest::ReadOnly | HitTestRequest::Active | HitTestRequest::DisallowUserAgentShadowContent | HitTestRequest::AllowChildFrameContent).innerNonSharedElement();
223 auto event = PointerEvent::create(eventNames().pointercancelEvent, pointerId, capturingData.pointerType);
224 target->dispatchEvent(event);
225 processPendingPointerCapture(WTFMove(event));
228 void PointerCaptureController::processPendingPointerCapture(const PointerEvent& event)
230 // https://w3c.github.io/pointerevents/#process-pending-pointer-capture
232 auto iterator = m_activePointerIdsToCapturingData.find(event.pointerId());
233 if (iterator == m_activePointerIdsToCapturingData.end())
236 auto& capturingData = iterator->value;
238 // 1. If the pointer capture target override for this pointer is set and is not equal to the pending pointer capture target override,
239 // then fire a pointer event named lostpointercapture at the pointer capture target override node.
240 if (capturingData.targetOverride && capturingData.targetOverride != capturingData.pendingTargetOverride)
241 capturingData.targetOverride->dispatchEvent(PointerEvent::createForPointerCapture(eventNames().lostpointercaptureEvent, event));
243 // 2. If the pending pointer capture target override for this pointer is set and is not equal to the pointer capture target override,
244 // then fire a pointer event named gotpointercapture at the pending pointer capture target override.
245 if (capturingData.pendingTargetOverride && capturingData.targetOverride != capturingData.pendingTargetOverride)
246 capturingData.pendingTargetOverride->dispatchEvent(PointerEvent::createForPointerCapture(eventNames().gotpointercaptureEvent, event));
248 // 3. Set the pointer capture target override to the pending pointer capture target override, if set. Otherwise, clear the pointer
249 // capture target override.
250 capturingData.targetOverride = capturingData.pendingTargetOverride;
253 } // namespace WebCore
255 #endif // ENABLE(POINTER_EVENTS)