2 * Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All rights reserved.
3 * Copyright (C) 2006 Alexey Proskuryakov (ap@webkit.org)
4 * Copyright (C) 2012 Digia Plc. and/or its subsidiary(-ies)
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "core/page/EventHandler.h"
31 #include "bindings/core/v8/ExceptionStatePlaceholder.h"
32 #include "core/HTMLNames.h"
33 #include "core/InputTypeNames.h"
34 #include "core/SVGNames.h"
35 #include "core/clipboard/DataObject.h"
36 #include "core/clipboard/DataTransfer.h"
37 #include "core/dom/Document.h"
38 #include "core/dom/DocumentMarkerController.h"
39 #include "core/dom/Fullscreen.h"
40 #include "core/dom/NodeRenderingTraversal.h"
41 #include "core/dom/TouchList.h"
42 #include "core/dom/shadow/ShadowRoot.h"
43 #include "core/editing/Editor.h"
44 #include "core/editing/FrameSelection.h"
45 #include "core/editing/TextIterator.h"
46 #include "core/editing/htmlediting.h"
47 #include "core/events/DOMWindowEventQueue.h"
48 #include "core/events/EventPath.h"
49 #include "core/events/KeyboardEvent.h"
50 #include "core/events/MouseEvent.h"
51 #include "core/events/TextEvent.h"
52 #include "core/events/TouchEvent.h"
53 #include "core/events/WheelEvent.h"
54 #include "core/fetch/ImageResource.h"
55 #include "core/frame/EventHandlerRegistry.h"
56 #include "core/frame/FrameView.h"
57 #include "core/frame/LocalFrame.h"
58 #include "core/frame/Settings.h"
59 #include "core/html/HTMLDialogElement.h"
60 #include "core/html/HTMLFrameElementBase.h"
61 #include "core/html/HTMLFrameSetElement.h"
62 #include "core/html/HTMLInputElement.h"
63 #include "core/inspector/InspectorController.h"
64 #include "core/loader/FrameLoader.h"
65 #include "core/loader/FrameLoaderClient.h"
66 #include "core/page/AutoscrollController.h"
67 #include "core/page/BackForwardClient.h"
68 #include "core/page/Chrome.h"
69 #include "core/page/ChromeClient.h"
70 #include "core/page/DragController.h"
71 #include "core/page/DragState.h"
72 #include "core/page/EditorClient.h"
73 #include "core/page/FocusController.h"
74 #include "core/page/FrameTree.h"
75 #include "core/page/Page.h"
76 #include "core/page/SpatialNavigation.h"
77 #include "core/page/TouchAdjustment.h"
78 #include "core/rendering/HitTestRequest.h"
79 #include "core/rendering/HitTestResult.h"
80 #include "core/rendering/RenderFlowThread.h"
81 #include "core/rendering/RenderLayer.h"
82 #include "core/rendering/RenderTextControlSingleLine.h"
83 #include "core/rendering/RenderView.h"
84 #include "core/rendering/RenderWidget.h"
85 #include "core/rendering/style/RenderStyle.h"
86 #include "core/svg/SVGDocumentExtensions.h"
87 #include "platform/PlatformGestureEvent.h"
88 #include "platform/PlatformKeyboardEvent.h"
89 #include "platform/PlatformTouchEvent.h"
90 #include "platform/PlatformWheelEvent.h"
91 #include "platform/RuntimeEnabledFeatures.h"
92 #include "platform/TraceEvent.h"
93 #include "platform/WindowsKeyboardCodes.h"
94 #include "platform/geometry/FloatPoint.h"
95 #include "platform/graphics/Image.h"
96 #include "platform/heap/Handle.h"
97 #include "platform/scroll/ScrollAnimator.h"
98 #include "platform/scroll/Scrollbar.h"
99 #include "wtf/Assertions.h"
100 #include "wtf/CurrentTime.h"
101 #include "wtf/StdLibExtras.h"
102 #include "wtf/TemporaryChange.h"
106 using namespace HTMLNames;
108 // The link drag hysteresis is much larger than the others because there
109 // needs to be enough space to cancel the link press without starting a link drag,
110 // and because dragging links is rare.
111 static const int LinkDragHysteresis = 40;
112 static const int ImageDragHysteresis = 5;
113 static const int TextDragHysteresis = 3;
114 static const int GeneralDragHysteresis = 3;
116 // The amount of time to wait before sending a fake mouse event, triggered
117 // during a scroll. The short interval is used if the content responds to the mouse events quickly enough,
118 // otherwise the long interval is used.
119 static const double fakeMouseMoveShortInterval = 0.1;
120 static const double fakeMouseMoveLongInterval = 0.250;
122 // The amount of time to wait for a cursor update on style and layout changes
123 // Set to 50Hz, no need to be faster than common screen refresh rate
124 static const double cursorUpdateInterval = 0.02;
126 static const int maximumCursorSize = 128;
128 // It's pretty unlikely that a scale of less than one would ever be used. But all we really
129 // need to ensure here is that the scale isn't so small that integer overflow can occur when
130 // dividing cursor sizes (limited above) by the scale.
131 static const double minimumCursorScale = 0.001;
133 // The minimum amount of time an element stays active after a ShowPress
134 // This is roughly 9 frames, which should be long enough to be noticeable.
135 static const double minimumActiveInterval = 0.15;
138 static const double TextDragDelay = 0.15;
140 static const double TextDragDelay = 0.0;
143 enum NoCursorChangeType { NoCursorChange };
145 class OptionalCursor {
147 OptionalCursor(NoCursorChangeType) : m_isCursorChange(false) { }
148 OptionalCursor(const Cursor& cursor) : m_isCursorChange(true), m_cursor(cursor) { }
150 bool isCursorChange() const { return m_isCursorChange; }
151 const Cursor& cursor() const { ASSERT(m_isCursorChange); return m_cursor; }
154 bool m_isCursorChange;
158 class MaximumDurationTracker {
160 explicit MaximumDurationTracker(double *maxDuration)
161 : m_maxDuration(maxDuration)
162 , m_start(monotonicallyIncreasingTime())
166 ~MaximumDurationTracker()
168 *m_maxDuration = max(*m_maxDuration, monotonicallyIncreasingTime() - m_start);
172 double* m_maxDuration;
176 static inline ScrollGranularity wheelGranularityToScrollGranularity(unsigned deltaMode)
179 case WheelEvent::DOM_DELTA_PAGE:
181 case WheelEvent::DOM_DELTA_LINE:
183 case WheelEvent::DOM_DELTA_PIXEL:
184 return ScrollByPixel;
186 return ScrollByPixel;
190 // Refetch the event target node if it is removed or currently is the shadow node inside an <input> element.
191 // If a mouse event handler changes the input element type to one that has a widget associated,
192 // we'd like to EventHandler::handleMousePressEvent to pass the event to the widget and thus the
193 // event target node can't still be the shadow node.
194 static inline bool shouldRefetchEventTarget(const MouseEventWithHitTestResults& mev)
196 Node* targetNode = mev.innerNode();
197 if (!targetNode || !targetNode->parentNode())
199 return targetNode->isShadowRoot() && isHTMLInputElement(*toShadowRoot(targetNode)->host());
202 EventHandler::EventHandler(LocalFrame* frame)
204 , m_mousePressed(false)
205 , m_capturesDragging(false)
206 , m_mouseDownMayStartSelect(false)
207 , m_mouseDownMayStartDrag(false)
208 , m_mouseDownWasSingleClickInSelection(false)
209 , m_selectionInitiationState(HaveNotStartedSelection)
210 , m_hoverTimer(this, &EventHandler::hoverTimerFired)
211 , m_cursorUpdateTimer(this, &EventHandler::cursorUpdateTimerFired)
212 , m_mouseDownMayStartAutoscroll(false)
213 , m_mouseDownWasInSubframe(false)
214 , m_fakeMouseMoveEventTimer(this, &EventHandler::fakeMouseMoveEventTimerFired)
216 , m_resizeScrollableArea(0)
217 , m_eventHandlerWillResetCapturingMouseEventsNode(0)
219 , m_shouldOnlyFireDragOverEvent(false)
220 , m_mousePositionIsUnknown(true)
221 , m_mouseDownTimestamp(0)
222 , m_widgetIsLatched(false)
223 , m_touchPressed(false)
224 , m_scrollGestureHandlingNode(nullptr)
225 , m_lastGestureScrollOverWidget(false)
226 , m_maxMouseMovedDuration(0)
227 , m_didStartDrag(false)
228 , m_longTapShouldInvokeContextMenu(false)
229 , m_activeIntervalTimer(this, &EventHandler::activeIntervalTimerFired)
230 , m_lastShowPressTimestamp(0)
234 EventHandler::~EventHandler()
236 ASSERT(!m_fakeMouseMoveEventTimer.isActive());
239 void EventHandler::trace(Visitor* visitor)
242 visitor->trace(m_mousePressNode);
243 visitor->trace(m_capturingMouseEventsNode);
244 visitor->trace(m_nodeUnderMouse);
245 visitor->trace(m_lastNodeUnderMouse);
246 visitor->trace(m_lastMouseMoveEventSubframe);
247 visitor->trace(m_clickNode);
248 visitor->trace(m_dragTarget);
249 visitor->trace(m_frameSetBeingResized);
250 visitor->trace(m_latchedWheelEventNode);
251 visitor->trace(m_previousWheelScrolledNode);
252 visitor->trace(m_targetForTouchID);
253 visitor->trace(m_touchSequenceDocument);
254 visitor->trace(m_scrollGestureHandlingNode);
255 visitor->trace(m_previousGestureScrolledNode);
256 visitor->trace(m_lastDeferredTapElement);
260 DragState& EventHandler::dragState()
262 DEFINE_STATIC_LOCAL(OwnPtrWillBePersistent<DragState>, state, (adoptPtrWillBeNoop(new DragState())));
266 void EventHandler::clear()
269 m_cursorUpdateTimer.stop();
270 m_fakeMouseMoveEventTimer.stop();
271 m_activeIntervalTimer.stop();
272 m_resizeScrollableArea = 0;
273 m_nodeUnderMouse = nullptr;
274 m_lastNodeUnderMouse = nullptr;
275 m_lastMouseMoveEventSubframe = nullptr;
276 m_lastScrollbarUnderMouse = nullptr;
278 m_clickNode = nullptr;
279 m_frameSetBeingResized = nullptr;
280 m_dragTarget = nullptr;
281 m_shouldOnlyFireDragOverEvent = false;
282 m_mousePositionIsUnknown = true;
283 m_lastKnownMousePosition = IntPoint();
284 m_lastKnownMouseGlobalPosition = IntPoint();
285 m_lastMouseDownUserGestureToken.clear();
286 m_mousePressNode = nullptr;
287 m_mousePressed = false;
288 m_capturesDragging = false;
289 m_capturingMouseEventsNode = nullptr;
290 m_latchedWheelEventNode = nullptr;
291 m_previousWheelScrolledNode = nullptr;
292 m_targetForTouchID.clear();
293 m_touchSequenceDocument.clear();
294 m_touchSequenceUserGestureToken.clear();
295 m_scrollGestureHandlingNode = nullptr;
296 m_lastGestureScrollOverWidget = false;
297 m_previousGestureScrolledNode = nullptr;
298 m_scrollbarHandlingScrollGesture = nullptr;
299 m_maxMouseMovedDuration = 0;
300 m_didStartDrag = false;
301 m_touchPressed = false;
302 m_mouseDownMayStartSelect = false;
303 m_mouseDownMayStartDrag = false;
304 m_lastShowPressTimestamp = 0;
305 m_lastDeferredTapElement = nullptr;
308 void EventHandler::nodeWillBeRemoved(Node& nodeToBeRemoved)
310 if (!nodeToBeRemoved.containsIncludingShadowDOM(m_clickNode.get()))
312 if (nodeToBeRemoved.isInShadowTree()) {
313 m_clickNode = nodeToBeRemoved.parentOrShadowHostNode();
315 // We don't dispatch click events if the mousedown node is removed
316 // before a mouseup event. It is compatible with IE and Firefox.
317 m_clickNode = nullptr;
321 static void setSelectionIfNeeded(FrameSelection& selection, const VisibleSelection& newSelection)
323 if (selection.selection() != newSelection)
324 selection.setSelection(newSelection);
327 static inline bool dispatchSelectStart(Node* node)
329 if (!node || !node->renderer())
332 return node->dispatchEvent(Event::createCancelableBubble(EventTypeNames::selectstart));
335 static VisibleSelection expandSelectionToRespectUserSelectAll(Node* targetNode, const VisibleSelection& selection)
337 Node* rootUserSelectAll = Position::rootUserSelectAllForNode(targetNode);
338 if (!rootUserSelectAll)
341 VisibleSelection newSelection(selection);
342 newSelection.setBase(positionBeforeNode(rootUserSelectAll).upstream(CanCrossEditingBoundary));
343 newSelection.setExtent(positionAfterNode(rootUserSelectAll).downstream(CanCrossEditingBoundary));
348 bool EventHandler::updateSelectionForMouseDownDispatchingSelectStart(Node* targetNode, const VisibleSelection& selection, TextGranularity granularity)
350 if (Position::nodeIsUserSelectNone(targetNode))
353 if (!dispatchSelectStart(targetNode))
356 if (selection.isRange())
357 m_selectionInitiationState = ExtendedSelection;
359 granularity = CharacterGranularity;
360 m_selectionInitiationState = PlacedCaret;
363 m_frame->selection().setNonDirectionalSelectionIfNeeded(selection, granularity);
368 void EventHandler::selectClosestWordFromHitTestResult(const HitTestResult& result, AppendTrailingWhitespace appendTrailingWhitespace)
370 Node* innerNode = result.innerNode();
371 VisibleSelection newSelection;
373 if (innerNode && innerNode->renderer()) {
374 VisiblePosition pos(innerNode->renderer()->positionForPoint(result.localPoint()));
375 if (pos.isNotNull()) {
376 newSelection = VisibleSelection(pos);
377 newSelection.expandUsingGranularity(WordGranularity);
380 if (appendTrailingWhitespace == ShouldAppendTrailingWhitespace && newSelection.isRange())
381 newSelection.appendTrailingWhitespace();
383 updateSelectionForMouseDownDispatchingSelectStart(innerNode, expandSelectionToRespectUserSelectAll(innerNode, newSelection), WordGranularity);
387 void EventHandler::selectClosestMisspellingFromHitTestResult(const HitTestResult& result, AppendTrailingWhitespace appendTrailingWhitespace)
389 Node* innerNode = result.innerNode();
390 VisibleSelection newSelection;
392 if (innerNode && innerNode->renderer()) {
393 VisiblePosition pos(innerNode->renderer()->positionForPoint(result.localPoint()));
394 Position start = pos.deepEquivalent();
395 Position end = pos.deepEquivalent();
396 if (pos.isNotNull()) {
397 DocumentMarkerVector markers = innerNode->document().markers().markersInRange(makeRange(pos, pos).get(), DocumentMarker::MisspellingMarkers());
398 if (markers.size() == 1) {
399 start.moveToOffset(markers[0]->startOffset());
400 end.moveToOffset(markers[0]->endOffset());
401 newSelection = VisibleSelection(start, end);
405 if (appendTrailingWhitespace == ShouldAppendTrailingWhitespace && newSelection.isRange())
406 newSelection.appendTrailingWhitespace();
408 updateSelectionForMouseDownDispatchingSelectStart(innerNode, expandSelectionToRespectUserSelectAll(innerNode, newSelection), WordGranularity);
412 void EventHandler::selectClosestWordFromMouseEvent(const MouseEventWithHitTestResults& result)
414 if (m_mouseDownMayStartSelect) {
415 selectClosestWordFromHitTestResult(result.hitTestResult(),
416 (result.event().clickCount() == 2 && m_frame->editor().isSelectTrailingWhitespaceEnabled()) ? ShouldAppendTrailingWhitespace : DontAppendTrailingWhitespace);
420 void EventHandler::selectClosestMisspellingFromMouseEvent(const MouseEventWithHitTestResults& result)
422 if (m_mouseDownMayStartSelect) {
423 selectClosestMisspellingFromHitTestResult(result.hitTestResult(),
424 (result.event().clickCount() == 2 && m_frame->editor().isSelectTrailingWhitespaceEnabled()) ? ShouldAppendTrailingWhitespace : DontAppendTrailingWhitespace);
428 void EventHandler::selectClosestWordOrLinkFromMouseEvent(const MouseEventWithHitTestResults& result)
430 if (!result.hitTestResult().isLiveLink())
431 return selectClosestWordFromMouseEvent(result);
433 Node* innerNode = result.innerNode();
435 if (innerNode && innerNode->renderer() && m_mouseDownMayStartSelect) {
436 VisibleSelection newSelection;
437 Element* URLElement = result.hitTestResult().URLElement();
438 VisiblePosition pos(innerNode->renderer()->positionForPoint(result.localPoint()));
439 if (pos.isNotNull() && pos.deepEquivalent().deprecatedNode()->isDescendantOf(URLElement))
440 newSelection = VisibleSelection::selectionFromContentsOfNode(URLElement);
442 updateSelectionForMouseDownDispatchingSelectStart(innerNode, expandSelectionToRespectUserSelectAll(innerNode, newSelection), WordGranularity);
446 bool EventHandler::handleMousePressEventDoubleClick(const MouseEventWithHitTestResults& event)
448 TRACE_EVENT0("blink", "EventHandler::handleMousePressEventDoubleClick");
450 if (event.event().button() != LeftButton)
453 if (m_frame->selection().isRange()) {
454 // A double-click when range is already selected
455 // should not change the selection. So, do not call
456 // selectClosestWordFromMouseEvent, but do set
457 // m_beganSelectingText to prevent handleMouseReleaseEvent
458 // from setting caret selection.
459 m_selectionInitiationState = ExtendedSelection;
461 selectClosestWordFromMouseEvent(event);
466 bool EventHandler::handleMousePressEventTripleClick(const MouseEventWithHitTestResults& event)
468 TRACE_EVENT0("blink", "EventHandler::handleMousePressEventTripleClick");
470 if (event.event().button() != LeftButton)
473 Node* innerNode = event.innerNode();
474 if (!(innerNode && innerNode->renderer() && m_mouseDownMayStartSelect))
477 VisibleSelection newSelection;
478 VisiblePosition pos(innerNode->renderer()->positionForPoint(event.localPoint()));
479 if (pos.isNotNull()) {
480 newSelection = VisibleSelection(pos);
481 newSelection.expandUsingGranularity(ParagraphGranularity);
484 return updateSelectionForMouseDownDispatchingSelectStart(innerNode, expandSelectionToRespectUserSelectAll(innerNode, newSelection), ParagraphGranularity);
487 static int textDistance(const Position& start, const Position& end)
489 RefPtrWillBeRawPtr<Range> range = Range::create(*start.document(), start, end);
490 return TextIterator::rangeLength(range.get(), true);
493 bool EventHandler::handleMousePressEventSingleClick(const MouseEventWithHitTestResults& event)
495 TRACE_EVENT0("blink", "EventHandler::handleMousePressEventSingleClick");
497 m_frame->document()->updateLayoutIgnorePendingStylesheets();
498 Node* innerNode = event.innerNode();
499 if (!(innerNode && innerNode->renderer() && m_mouseDownMayStartSelect))
502 // Extend the selection if the Shift key is down, unless the click is in a link.
503 bool extendSelection = event.event().shiftKey() && !event.isOverLink();
505 // Don't restart the selection when the mouse is pressed on an
506 // existing selection so we can allow for text dragging.
507 if (FrameView* view = m_frame->view()) {
508 LayoutPoint vPoint = view->windowToContents(event.event().position());
509 if (!extendSelection && m_frame->selection().contains(vPoint)) {
510 m_mouseDownWasSingleClickInSelection = true;
515 VisiblePosition visiblePos(innerNode->renderer()->positionForPoint(event.localPoint()));
516 if (visiblePos.isNull())
517 visiblePos = VisiblePosition(firstPositionInOrBeforeNode(innerNode), DOWNSTREAM);
518 Position pos = visiblePos.deepEquivalent();
520 VisibleSelection newSelection = m_frame->selection().selection();
521 TextGranularity granularity = CharacterGranularity;
523 if (extendSelection && newSelection.isCaretOrRange()) {
524 VisibleSelection selectionInUserSelectAll(expandSelectionToRespectUserSelectAll(innerNode, VisibleSelection(VisiblePosition(pos))));
525 if (selectionInUserSelectAll.isRange()) {
526 if (comparePositions(selectionInUserSelectAll.start(), newSelection.start()) < 0)
527 pos = selectionInUserSelectAll.start();
528 else if (comparePositions(newSelection.end(), selectionInUserSelectAll.end()) < 0)
529 pos = selectionInUserSelectAll.end();
532 if (!m_frame->editor().behavior().shouldConsiderSelectionAsDirectional()) {
533 if (pos.isNotNull()) {
534 // See <rdar://problem/3668157> REGRESSION (Mail): shift-click deselects when selection
535 // was created right-to-left
536 Position start = newSelection.start();
537 Position end = newSelection.end();
538 int distanceToStart = textDistance(start, pos);
539 int distanceToEnd = textDistance(pos, end);
540 if (distanceToStart <= distanceToEnd)
541 newSelection = VisibleSelection(end, pos);
543 newSelection = VisibleSelection(start, pos);
546 newSelection.setExtent(pos);
548 if (m_frame->selection().granularity() != CharacterGranularity) {
549 granularity = m_frame->selection().granularity();
550 newSelection.expandUsingGranularity(m_frame->selection().granularity());
553 newSelection = expandSelectionToRespectUserSelectAll(innerNode, VisibleSelection(visiblePos));
556 // Updating the selection is considered side-effect of the event and so it doesn't impact the handled state.
557 updateSelectionForMouseDownDispatchingSelectStart(innerNode, newSelection, granularity);
561 static inline bool canMouseDownStartSelect(Node* node)
563 if (!node || !node->renderer())
566 if (!node->canStartSelection())
572 bool EventHandler::handleMousePressEvent(const MouseEventWithHitTestResults& event)
574 TRACE_EVENT0("blink", "EventHandler::handleMousePressEvent");
577 dragState().m_dragSrc = nullptr;
579 cancelFakeMouseMoveEvent();
581 m_frame->document()->updateLayoutIgnorePendingStylesheets();
583 if (ScrollView* scrollView = m_frame->view()) {
584 if (scrollView->isPointInScrollbarCorner(event.event().position()))
588 bool singleClick = event.event().clickCount() <= 1;
590 // If we got the event back, that must mean it wasn't prevented,
591 // so it's allowed to start a drag or selection if it wasn't in a scrollbar.
592 m_mouseDownMayStartSelect = canMouseDownStartSelect(event.innerNode()) && !event.scrollbar();
594 m_mouseDownMayStartDrag = singleClick;
596 m_mouseDownWasSingleClickInSelection = false;
598 m_mouseDown = event.event();
600 if (event.isOverWidget() && passWidgetMouseDownEventToWidget(event))
603 if (m_frame->document()->isSVGDocument() && m_frame->document()->accessSVGExtensions().zoomAndPanEnabled()) {
604 if (event.event().shiftKey() && singleClick) {
606 m_frame->document()->accessSVGExtensions().startPan(m_frame->view()->windowToContents(event.event().position()));
611 // We don't do this at the start of mouse down handling,
612 // because we don't want to do it until we know we didn't hit a widget.
616 Node* innerNode = event.innerNode();
618 m_mousePressNode = innerNode;
619 m_dragStartPos = event.event().position();
621 bool swallowEvent = false;
622 m_mousePressed = true;
624 if (event.event().clickCount() == 2)
625 swallowEvent = handleMousePressEventDoubleClick(event);
626 else if (event.event().clickCount() >= 3)
627 swallowEvent = handleMousePressEventTripleClick(event);
629 swallowEvent = handleMousePressEventSingleClick(event);
631 m_mouseDownMayStartAutoscroll = m_mouseDownMayStartSelect
632 || (m_mousePressNode && m_mousePressNode->renderBox() && m_mousePressNode->renderBox()->canBeProgramaticallyScrolled());
637 bool EventHandler::handleMouseDraggedEvent(const MouseEventWithHitTestResults& event)
639 TRACE_EVENT0("blink", "EventHandler::handleMouseDraggedEvent");
644 if (handleDrag(event, ShouldCheckDragHysteresis))
647 Node* targetNode = event.innerNode();
648 if (event.event().button() != LeftButton || !targetNode)
651 RenderObject* renderer = targetNode->renderer();
653 Node* parent = NodeRenderingTraversal::parent(targetNode);
657 renderer = parent->renderer();
658 if (!renderer || !renderer->isListBox())
662 m_mouseDownMayStartDrag = false;
664 if (m_mouseDownMayStartAutoscroll && !panScrollInProgress()) {
665 if (AutoscrollController* controller = autoscrollController()) {
666 controller->startAutoscrollForSelection(renderer);
667 m_mouseDownMayStartAutoscroll = false;
671 if (m_selectionInitiationState != ExtendedSelection) {
672 HitTestRequest request(HitTestRequest::ReadOnly | HitTestRequest::Active);
673 HitTestResult result(m_mouseDownPos);
674 m_frame->document()->renderView()->hitTest(request, result);
676 updateSelectionForMouseDrag(result);
678 updateSelectionForMouseDrag(event.hitTestResult());
682 void EventHandler::updateSelectionForMouseDrag()
684 FrameView* view = m_frame->view();
687 RenderView* renderer = m_frame->contentRenderer();
691 HitTestRequest request(HitTestRequest::ReadOnly | HitTestRequest::Active | HitTestRequest::Move);
692 HitTestResult result(view->windowToContents(m_lastKnownMousePosition));
693 renderer->hitTest(request, result);
694 updateSelectionForMouseDrag(result);
697 void EventHandler::updateSelectionForMouseDrag(const HitTestResult& hitTestResult)
699 if (!m_mouseDownMayStartSelect)
702 Node* target = hitTestResult.innerNode();
706 VisiblePosition targetPosition = m_frame->selection().selection().visiblePositionRespectingEditingBoundary(hitTestResult.localPoint(), target);
707 // Don't modify the selection if we're not on a node.
708 if (targetPosition.isNull())
711 // Restart the selection if this is the first mouse move. This work is usually
712 // done in handleMousePressEvent, but not if the mouse press was on an existing selection.
713 VisibleSelection newSelection = m_frame->selection().selection();
715 // Special case to limit selection to the containing block for SVG text.
716 // FIXME: Isn't there a better non-SVG-specific way to do this?
717 if (Node* selectionBaseNode = newSelection.base().deprecatedNode())
718 if (RenderObject* selectionBaseRenderer = selectionBaseNode->renderer())
719 if (selectionBaseRenderer->isSVGText())
720 if (target->renderer()->containingBlock() != selectionBaseRenderer->containingBlock())
723 if (m_selectionInitiationState == HaveNotStartedSelection && !dispatchSelectStart(target))
726 if (m_selectionInitiationState != ExtendedSelection) {
727 // Always extend selection here because it's caused by a mouse drag
728 m_selectionInitiationState = ExtendedSelection;
729 newSelection = VisibleSelection(targetPosition);
732 if (RuntimeEnabledFeatures::userSelectAllEnabled()) {
733 Node* rootUserSelectAllForMousePressNode = Position::rootUserSelectAllForNode(m_mousePressNode.get());
734 if (rootUserSelectAllForMousePressNode && rootUserSelectAllForMousePressNode == Position::rootUserSelectAllForNode(target)) {
735 newSelection.setBase(positionBeforeNode(rootUserSelectAllForMousePressNode).upstream(CanCrossEditingBoundary));
736 newSelection.setExtent(positionAfterNode(rootUserSelectAllForMousePressNode).downstream(CanCrossEditingBoundary));
738 // Reset base for user select all when base is inside user-select-all area and extent < base.
739 if (rootUserSelectAllForMousePressNode && comparePositions(target->renderer()->positionForPoint(hitTestResult.localPoint()), m_mousePressNode->renderer()->positionForPoint(m_dragStartPos)) < 0)
740 newSelection.setBase(positionAfterNode(rootUserSelectAllForMousePressNode).downstream(CanCrossEditingBoundary));
742 Node* rootUserSelectAllForTarget = Position::rootUserSelectAllForNode(target);
743 if (rootUserSelectAllForTarget && m_mousePressNode->renderer() && comparePositions(target->renderer()->positionForPoint(hitTestResult.localPoint()), m_mousePressNode->renderer()->positionForPoint(m_dragStartPos)) < 0)
744 newSelection.setExtent(positionBeforeNode(rootUserSelectAllForTarget).upstream(CanCrossEditingBoundary));
745 else if (rootUserSelectAllForTarget && m_mousePressNode->renderer())
746 newSelection.setExtent(positionAfterNode(rootUserSelectAllForTarget).downstream(CanCrossEditingBoundary));
748 newSelection.setExtent(targetPosition);
751 newSelection.setExtent(targetPosition);
754 if (m_frame->selection().granularity() != CharacterGranularity)
755 newSelection.expandUsingGranularity(m_frame->selection().granularity());
757 m_frame->selection().setNonDirectionalSelectionIfNeeded(newSelection, m_frame->selection().granularity(),
758 FrameSelection::AdjustEndpointsAtBidiBoundary);
761 bool EventHandler::handleMouseReleaseEvent(const MouseEventWithHitTestResults& event)
763 AutoscrollController* controller = autoscrollController();
764 if (controller && controller->autoscrollInProgress())
767 // Used to prevent mouseMoveEvent from initiating a drag before
768 // the mouse is pressed again.
769 m_mousePressed = false;
770 m_capturesDragging = false;
771 m_mouseDownMayStartDrag = false;
772 m_mouseDownMayStartSelect = false;
773 m_mouseDownMayStartAutoscroll = false;
774 m_mouseDownWasInSubframe = false;
776 bool handled = false;
778 // Clear the selection if the mouse didn't move after the last mouse
779 // press and it's not a context menu click. We do this so when clicking
780 // on the selection, the selection goes away. However, if we are
781 // editing, place the caret.
782 if (m_mouseDownWasSingleClickInSelection && m_selectionInitiationState != ExtendedSelection
783 && m_dragStartPos == event.event().position()
784 && m_frame->selection().isRange()
785 && event.event().button() != RightButton) {
786 VisibleSelection newSelection;
787 Node* node = event.innerNode();
788 bool caretBrowsing = m_frame->settings() && m_frame->settings()->caretBrowsingEnabled();
789 if (node && node->renderer() && (caretBrowsing || node->hasEditableStyle())) {
790 VisiblePosition pos = VisiblePosition(node->renderer()->positionForPoint(event.localPoint()));
791 newSelection = VisibleSelection(pos);
794 setSelectionIfNeeded(m_frame->selection(), newSelection);
799 m_frame->selection().notifyRendererOfSelectionChange(UserTriggered);
801 m_frame->selection().selectFrameElementInParentIfFullySelected();
803 if (event.event().button() == MiddleButton && !event.isOverLink()) {
804 // Ignore handled, since we want to paste to where the caret was placed anyway.
805 handled = handlePasteGlobalSelection(event.event()) || handled;
813 void EventHandler::startPanScrolling(RenderObject* renderer)
815 if (!renderer->isBox())
817 AutoscrollController* controller = autoscrollController();
820 controller->startPanScrolling(toRenderBox(renderer), lastKnownMousePosition());
826 AutoscrollController* EventHandler::autoscrollController() const
828 if (Page* page = m_frame->page())
829 return &page->autoscrollController();
833 bool EventHandler::panScrollInProgress() const
835 return autoscrollController() && autoscrollController()->panScrollInProgress();
838 HitTestResult EventHandler::hitTestResultAtPoint(const LayoutPoint& point, HitTestRequest::HitTestRequestType hitType, const LayoutSize& padding)
840 TRACE_EVENT0("blink", "EventHandler::hitTestResultAtPoint");
842 // We always send hitTestResultAtPoint to the main frame if we have one,
843 // otherwise we might hit areas that are obscured by higher frames.
844 if (m_frame->page()) {
845 LocalFrame* mainFrame = m_frame->localFrameRoot();
846 if (mainFrame && m_frame != mainFrame) {
847 FrameView* frameView = m_frame->view();
848 FrameView* mainView = mainFrame->view();
849 if (frameView && mainView) {
850 IntPoint mainFramePoint = mainView->rootViewToContents(frameView->contentsToRootView(roundedIntPoint(point)));
851 return mainFrame->eventHandler().hitTestResultAtPoint(mainFramePoint, hitType, padding);
856 HitTestResult result(point, padding.height(), padding.width(), padding.height(), padding.width());
858 // RenderView::hitTest causes a layout, and we don't want to hit that until the first
859 // layout because until then, there is nothing shown on the screen - the user can't
860 // have intentionally clicked on something belonging to this page. Furthermore,
861 // mousemove events before the first layout should not lead to a premature layout()
862 // happening, which could show a flash of white.
863 // See also the similar code in Document::prepareMouseEvent.
864 if (!m_frame->contentRenderer() || !m_frame->view() || !m_frame->view()->didFirstLayout())
867 // hitTestResultAtPoint is specifically used to hitTest into all frames, thus it always allows child frame content.
868 HitTestRequest request(hitType | HitTestRequest::AllowChildFrameContent);
869 m_frame->contentRenderer()->hitTest(request, result);
870 if (!request.readOnly())
871 m_frame->document()->updateHoverActiveState(request, result.innerElement());
876 void EventHandler::stopAutoscroll()
878 if (AutoscrollController* controller = autoscrollController())
879 controller->stopAutoscroll();
882 Node* EventHandler::mousePressNode() const
884 return m_mousePressNode.get();
887 bool EventHandler::scroll(ScrollDirection direction, ScrollGranularity granularity, Node* startNode, Node** stopNode, float delta, IntPoint absolutePoint)
892 Node* node = startNode;
895 node = m_frame->document()->focusedElement();
898 node = m_mousePressNode.get();
900 if (!node || !node->renderer())
903 RenderBox* curBox = node->renderer()->enclosingBox();
904 while (curBox && !curBox->isRenderView()) {
905 ScrollDirection physicalDirection = toPhysicalDirection(
906 direction, curBox->isHorizontalWritingMode(), curBox->style()->isFlippedBlocksWritingMode());
908 // If we're at the stopNode, we should try to scroll it but we shouldn't bubble past it
909 bool shouldStopBubbling = stopNode && *stopNode && curBox->node() == *stopNode;
910 bool didScroll = curBox->scroll(physicalDirection, granularity, delta);
912 if (didScroll && stopNode)
913 *stopNode = curBox->node();
915 if (didScroll || shouldStopBubbling) {
916 setFrameWasScrolledByUser();
920 curBox = curBox->containingBlock();
926 bool EventHandler::bubblingScroll(ScrollDirection direction, ScrollGranularity granularity, Node* startingNode)
928 // The layout needs to be up to date to determine if we can scroll. We may be
929 // here because of an onLoad event, in which case the final layout hasn't been performed yet.
930 m_frame->document()->updateLayoutIgnorePendingStylesheets();
931 if (scroll(direction, granularity, startingNode))
933 LocalFrame* frame = m_frame;
934 FrameView* view = frame->view();
935 if (view && view->scroll(direction, granularity))
937 Frame* parentFrame = frame->tree().parent();
938 if (!parentFrame || !parentFrame->isLocalFrame())
940 // FIXME: Broken for OOPI.
941 return toLocalFrame(parentFrame)->eventHandler().bubblingScroll(direction, granularity, m_frame->deprecatedLocalOwner());
944 IntPoint EventHandler::lastKnownMousePosition() const
946 return m_lastKnownMousePosition;
949 static LocalFrame* subframeForTargetNode(Node* node)
954 RenderObject* renderer = node->renderer();
955 if (!renderer || !renderer->isWidget())
958 // FIXME: This explicit check is needed only until RemoteFrames have RemoteFrameViews.
959 if (isHTMLFrameElementBase(node) && toHTMLFrameElementBase(node)->contentFrame() && toHTMLFrameElementBase(node)->contentFrame()->isRemoteFrameTemporary())
962 Widget* widget = toRenderWidget(renderer)->widget();
963 if (!widget || !widget->isFrameView())
966 return &toFrameView(widget)->frame();
969 static LocalFrame* subframeForHitTestResult(const MouseEventWithHitTestResults& hitTestResult)
971 if (!hitTestResult.isOverWidget())
973 return subframeForTargetNode(hitTestResult.innerNode());
976 static bool isSubmitImage(Node* node)
978 return isHTMLInputElement(node) && toHTMLInputElement(node)->type() == InputTypeNames::image;
981 bool EventHandler::useHandCursor(Node* node, bool isOverLink)
986 return ((isOverLink || isSubmitImage(node)) && !node->hasEditableStyle());
989 void EventHandler::cursorUpdateTimerFired(Timer<EventHandler>*)
992 ASSERT(m_frame->document());
997 void EventHandler::updateCursor()
999 if (m_mousePositionIsUnknown)
1002 FrameView* view = m_frame->view();
1003 if (!view || !view->shouldSetCursor())
1006 RenderView* renderView = view->renderView();
1010 m_frame->document()->updateLayout();
1012 HitTestRequest request(HitTestRequest::ReadOnly);
1013 HitTestResult result(view->windowToContents(m_lastKnownMousePosition));
1014 renderView->hitTest(request, result);
1016 OptionalCursor optionalCursor = selectCursor(result);
1017 if (optionalCursor.isCursorChange()) {
1018 m_currentMouseCursor = optionalCursor.cursor();
1019 view->setCursor(m_currentMouseCursor);
1023 OptionalCursor EventHandler::selectCursor(const HitTestResult& result)
1025 if (m_resizeScrollableArea && m_resizeScrollableArea->inResizeMode())
1026 return NoCursorChange;
1028 Page* page = m_frame->page();
1030 return NoCursorChange;
1032 if (panScrollInProgress())
1033 return NoCursorChange;
1036 Node* node = result.innerPossiblyPseudoNode();
1038 return selectAutoCursor(result, node, iBeamCursor());
1040 RenderObject* renderer = node->renderer();
1041 RenderStyle* style = renderer ? renderer->style() : 0;
1044 Cursor overrideCursor;
1045 switch (renderer->getCursor(roundedIntPoint(result.localPoint()), overrideCursor)) {
1046 case SetCursorBasedOnStyle:
1049 return overrideCursor;
1050 case DoNotSetCursor:
1051 return NoCursorChange;
1055 if (style && style->cursors()) {
1056 const CursorList* cursors = style->cursors();
1057 for (unsigned i = 0; i < cursors->size(); ++i) {
1058 StyleImage* styleImage = (*cursors)[i].image();
1061 ImageResource* cachedImage = styleImage->cachedImage();
1064 float scale = styleImage->imageScaleFactor();
1065 // Get hotspot and convert from logical pixels to physical pixels.
1066 IntPoint hotSpot = (*cursors)[i].hotSpot();
1067 hotSpot.scale(scale, scale);
1068 IntSize size = cachedImage->imageForRenderer(renderer)->size();
1069 if (cachedImage->errorOccurred())
1071 // Limit the size of cursors (in UI pixels) so that they cannot be
1072 // used to cover UI elements in chrome.
1073 size.scale(1 / scale);
1074 if (size.width() > maximumCursorSize || size.height() > maximumCursorSize)
1077 Image* image = cachedImage->imageForRenderer(renderer);
1078 // Ensure no overflow possible in calculations above.
1079 if (scale < minimumCursorScale)
1081 return Cursor(image, hotSpot, scale);
1085 switch (style ? style->cursor() : CURSOR_AUTO) {
1087 bool horizontalText = !style || style->isHorizontalWritingMode();
1088 const Cursor& iBeam = horizontalText ? iBeamCursor() : verticalTextCursor();
1089 return selectAutoCursor(result, node, iBeam);
1092 return crossCursor();
1093 case CURSOR_POINTER:
1094 return handCursor();
1096 return moveCursor();
1097 case CURSOR_ALL_SCROLL:
1098 return moveCursor();
1099 case CURSOR_E_RESIZE:
1100 return eastResizeCursor();
1101 case CURSOR_W_RESIZE:
1102 return westResizeCursor();
1103 case CURSOR_N_RESIZE:
1104 return northResizeCursor();
1105 case CURSOR_S_RESIZE:
1106 return southResizeCursor();
1107 case CURSOR_NE_RESIZE:
1108 return northEastResizeCursor();
1109 case CURSOR_SW_RESIZE:
1110 return southWestResizeCursor();
1111 case CURSOR_NW_RESIZE:
1112 return northWestResizeCursor();
1113 case CURSOR_SE_RESIZE:
1114 return southEastResizeCursor();
1115 case CURSOR_NS_RESIZE:
1116 return northSouthResizeCursor();
1117 case CURSOR_EW_RESIZE:
1118 return eastWestResizeCursor();
1119 case CURSOR_NESW_RESIZE:
1120 return northEastSouthWestResizeCursor();
1121 case CURSOR_NWSE_RESIZE:
1122 return northWestSouthEastResizeCursor();
1123 case CURSOR_COL_RESIZE:
1124 return columnResizeCursor();
1125 case CURSOR_ROW_RESIZE:
1126 return rowResizeCursor();
1128 return iBeamCursor();
1130 return waitCursor();
1132 return helpCursor();
1133 case CURSOR_VERTICAL_TEXT:
1134 return verticalTextCursor();
1136 return cellCursor();
1137 case CURSOR_CONTEXT_MENU:
1138 return contextMenuCursor();
1139 case CURSOR_PROGRESS:
1140 return progressCursor();
1141 case CURSOR_NO_DROP:
1142 return noDropCursor();
1144 return aliasCursor();
1146 return copyCursor();
1148 return noneCursor();
1149 case CURSOR_NOT_ALLOWED:
1150 return notAllowedCursor();
1151 case CURSOR_DEFAULT:
1152 return pointerCursor();
1153 case CURSOR_ZOOM_IN:
1154 return zoomInCursor();
1155 case CURSOR_ZOOM_OUT:
1156 return zoomOutCursor();
1157 case CURSOR_WEBKIT_GRAB:
1158 return grabCursor();
1159 case CURSOR_WEBKIT_GRABBING:
1160 return grabbingCursor();
1162 return pointerCursor();
1165 OptionalCursor EventHandler::selectAutoCursor(const HitTestResult& result, Node* node, const Cursor& iBeam)
1167 bool editable = (node && node->hasEditableStyle());
1169 if (useHandCursor(node, result.isOverLink()))
1170 return handCursor();
1172 bool inResizer = false;
1173 RenderObject* renderer = node ? node->renderer() : 0;
1174 if (renderer && m_frame->view()) {
1175 RenderLayer* layer = renderer->enclosingLayer();
1176 inResizer = layer->scrollableArea() && layer->scrollableArea()->isPointInResizeControl(result.roundedPointInMainFrame(), ResizerForPointer);
1179 // During selection, use an I-beam no matter what we're over.
1180 // If a drag may be starting or we're capturing mouse events for a particular node, don't treat this as a selection.
1181 if (m_mousePressed && m_mouseDownMayStartSelect
1182 && !m_mouseDownMayStartDrag
1183 && m_frame->selection().isCaretOrRange()
1184 && !m_capturingMouseEventsNode) {
1188 if ((editable || (renderer && renderer->isText() && node->canStartSelection())) && !inResizer && !result.scrollbar())
1190 return pointerCursor();
1193 static LayoutPoint documentPointForWindowPoint(LocalFrame* frame, const IntPoint& windowPoint)
1195 FrameView* view = frame->view();
1196 // FIXME: Is it really OK to use the wrong coordinates here when view is 0?
1197 // Historically the code would just crash; this is clearly no worse than that.
1198 return view ? view->windowToContents(windowPoint) : windowPoint;
1201 bool EventHandler::handleMousePressEvent(const PlatformMouseEvent& mouseEvent)
1203 TRACE_EVENT0("blink", "EventHandler::handleMousePressEvent");
1205 RefPtr<FrameView> protector(m_frame->view());
1207 UserGestureIndicator gestureIndicator(DefinitelyProcessingUserGesture);
1208 m_frame->localFrameRoot()->eventHandler().m_lastMouseDownUserGestureToken = gestureIndicator.currentToken();
1210 cancelFakeMouseMoveEvent();
1211 if (m_eventHandlerWillResetCapturingMouseEventsNode)
1212 m_capturingMouseEventsNode = nullptr;
1213 m_mousePressed = true;
1214 m_capturesDragging = true;
1215 setLastKnownMousePosition(mouseEvent);
1216 m_mouseDownTimestamp = mouseEvent.timestamp();
1217 m_mouseDownMayStartDrag = false;
1218 m_mouseDownMayStartSelect = false;
1219 m_mouseDownMayStartAutoscroll = false;
1220 if (FrameView* view = m_frame->view())
1221 m_mouseDownPos = view->windowToContents(mouseEvent.position());
1226 m_mouseDownWasInSubframe = false;
1228 HitTestRequest request(HitTestRequest::Active);
1229 // Save the document point we generate in case the window coordinate is invalidated by what happens
1230 // when we dispatch the event.
1231 LayoutPoint documentPoint = documentPointForWindowPoint(m_frame, mouseEvent.position());
1232 MouseEventWithHitTestResults mev = m_frame->document()->prepareMouseEvent(request, documentPoint, mouseEvent);
1234 if (!mev.innerNode()) {
1239 m_mousePressNode = mev.innerNode();
1241 RefPtrWillBeRawPtr<LocalFrame> subframe = subframeForHitTestResult(mev);
1242 if (subframe && passMousePressEventToSubframe(mev, subframe.get())) {
1243 // Start capturing future events for this frame. We only do this if we didn't clear
1244 // the m_mousePressed flag, which may happen if an AppKit widget entered a modal event loop.
1245 m_capturesDragging = subframe->eventHandler().capturesDragging();
1246 if (m_mousePressed && m_capturesDragging) {
1247 m_capturingMouseEventsNode = mev.innerNode();
1248 m_eventHandlerWillResetCapturingMouseEventsNode = true;
1255 // We store whether pan scrolling is in progress before calling stopAutoscroll()
1256 // because it will set m_autoscrollType to NoAutoscroll on return.
1257 bool isPanScrollInProgress = panScrollInProgress();
1259 if (isPanScrollInProgress) {
1260 // We invalidate the click when exiting pan scrolling so that we don't inadvertently navigate
1261 // away from the current page (e.g. the click was on a hyperlink). See <rdar://problem/6095023>.
1267 m_clickCount = mouseEvent.clickCount();
1268 m_clickNode = mev.innerNode()->isTextNode() ? NodeRenderingTraversal::parent(mev.innerNode()) : mev.innerNode();
1270 if (FrameView* view = m_frame->view()) {
1271 RenderLayer* layer = mev.innerNode()->renderer() ? mev.innerNode()->renderer()->enclosingLayer() : 0;
1272 IntPoint p = view->windowToContents(mouseEvent.position());
1273 if (layer && layer->scrollableArea() && layer->scrollableArea()->isPointInResizeControl(p, ResizerForPointer)) {
1274 m_resizeScrollableArea = layer->scrollableArea();
1275 m_resizeScrollableArea->setInResizeMode(true);
1276 m_offsetFromResizeCorner = m_resizeScrollableArea->offsetFromResizeCorner(p);
1282 m_frame->selection().setCaretBlinkingSuspended(true);
1284 bool swallowEvent = !dispatchMouseEvent(EventTypeNames::mousedown, mev.innerNode(), m_clickCount, mouseEvent, true);
1285 // m_selectionInitiationState is initialized after dispatching mousedown event in order not to keep the selection by DOM APIs
1286 // Because we can't give the user the chance to handle the selection by user action like dragging if we keep the selection in case of mousedown.
1287 // FireFox also has the same behavior and it's more compatible with other browsers.
1288 m_selectionInitiationState = HaveNotStartedSelection;
1289 swallowEvent = swallowEvent || handleMouseFocus(mouseEvent);
1290 m_capturesDragging = !swallowEvent || mev.scrollbar();
1292 // If the hit testing originally determined the event was in a scrollbar, refetch the MouseEventWithHitTestResults
1293 // in case the scrollbar widget was destroyed when the mouse event was handled.
1294 if (mev.scrollbar()) {
1295 const bool wasLastScrollBar = mev.scrollbar() == m_lastScrollbarUnderMouse.get();
1296 HitTestRequest request(HitTestRequest::ReadOnly | HitTestRequest::Active);
1297 mev = m_frame->document()->prepareMouseEvent(request, documentPoint, mouseEvent);
1298 if (wasLastScrollBar && mev.scrollbar() != m_lastScrollbarUnderMouse.get())
1299 m_lastScrollbarUnderMouse = nullptr;
1303 // scrollbars should get events anyway, even disabled controls might be scrollable
1304 passMousePressEventToScrollbar(mev);
1306 if (shouldRefetchEventTarget(mev)) {
1307 HitTestRequest request(HitTestRequest::ReadOnly | HitTestRequest::Active);
1308 mev = m_frame->document()->prepareMouseEvent(request, documentPoint, mouseEvent);
1311 if (passMousePressEventToScrollbar(mev))
1312 swallowEvent = true;
1314 swallowEvent = handleMousePressEvent(mev);
1317 return swallowEvent;
1320 static RenderLayer* layerForNode(Node* node)
1325 RenderObject* renderer = node->renderer();
1329 RenderLayer* layer = renderer->enclosingLayer();
1336 ScrollableArea* EventHandler::associatedScrollableArea(const RenderLayer* layer) const
1338 if (RenderLayerScrollableArea* scrollableArea = layer->scrollableArea()) {
1339 if (scrollableArea->scrollsOverflow())
1340 return scrollableArea;
1346 bool EventHandler::handleMouseMoveEvent(const PlatformMouseEvent& event)
1348 TRACE_EVENT0("blink", "EventHandler::handleMouseMoveEvent");
1350 RefPtr<FrameView> protector(m_frame->view());
1351 MaximumDurationTracker maxDurationTracker(&m_maxMouseMovedDuration);
1353 HitTestResult hoveredNode = HitTestResult(LayoutPoint());
1354 bool result = handleMouseMoveOrLeaveEvent(event, &hoveredNode);
1356 Page* page = m_frame->page();
1360 if (RenderLayer* layer = layerForNode(hoveredNode.innerNode())) {
1361 if (ScrollableArea* layerScrollableArea = associatedScrollableArea(layer))
1362 layerScrollableArea->mouseMovedInContentArea();
1365 if (FrameView* frameView = m_frame->view())
1366 frameView->mouseMovedInContentArea();
1368 hoveredNode.setToShadowHostIfInUserAgentShadowRoot();
1369 page->chrome().mouseDidMoveOverElement(hoveredNode, event.modifierFlags());
1370 page->chrome().setToolTip(hoveredNode);
1375 void EventHandler::handleMouseLeaveEvent(const PlatformMouseEvent& event)
1377 TRACE_EVENT0("blink", "EventHandler::handleMouseLeaveEvent");
1379 RefPtr<FrameView> protector(m_frame->view());
1380 handleMouseMoveOrLeaveEvent(event);
1383 bool EventHandler::handleMouseMoveOrLeaveEvent(const PlatformMouseEvent& mouseEvent, HitTestResult* hoveredNode, bool onlyUpdateScrollbars)
1386 ASSERT(m_frame->view());
1388 setLastKnownMousePosition(mouseEvent);
1390 if (m_hoverTimer.isActive())
1391 m_hoverTimer.stop();
1393 m_cursorUpdateTimer.stop();
1395 cancelFakeMouseMoveEvent();
1398 m_frame->document()->accessSVGExtensions().updatePan(m_frame->view()->windowToContents(m_lastKnownMousePosition));
1402 if (m_frameSetBeingResized)
1403 return !dispatchMouseEvent(EventTypeNames::mousemove, m_frameSetBeingResized.get(), 0, mouseEvent, false);
1405 // Send events right to a scrollbar if the mouse is pressed.
1406 if (m_lastScrollbarUnderMouse && m_mousePressed) {
1407 m_lastScrollbarUnderMouse->mouseMoved(mouseEvent);
1411 // Mouse events simulated from touch should not hit-test again.
1412 ASSERT(!mouseEvent.fromTouch());
1414 HitTestRequest::HitTestRequestType hitType = HitTestRequest::Move;
1416 hitType |= HitTestRequest::Active;
1417 else if (onlyUpdateScrollbars) {
1418 // Mouse events should be treated as "read-only" if we're updating only scrollbars. This
1419 // means that :hover and :active freeze in the state they were in, rather than updating
1420 // for nodes the mouse moves while the window is not key (which will be the case if
1421 // onlyUpdateScrollbars is true).
1422 hitType |= HitTestRequest::ReadOnly;
1425 // Treat any mouse move events as readonly if the user is currently touching the screen.
1427 hitType |= HitTestRequest::Active | HitTestRequest::ReadOnly;
1428 HitTestRequest request(hitType);
1429 MouseEventWithHitTestResults mev = prepareMouseEvent(request, mouseEvent);
1431 *hoveredNode = mev.hitTestResult();
1433 Scrollbar* scrollbar = 0;
1435 if (m_resizeScrollableArea && m_resizeScrollableArea->inResizeMode())
1436 m_resizeScrollableArea->resize(mouseEvent, m_offsetFromResizeCorner);
1439 scrollbar = mev.scrollbar();
1441 updateLastScrollbarUnderMouse(scrollbar, !m_mousePressed);
1442 if (onlyUpdateScrollbars)
1446 bool swallowEvent = false;
1447 RefPtrWillBeRawPtr<LocalFrame> newSubframe = m_capturingMouseEventsNode.get() ? subframeForTargetNode(m_capturingMouseEventsNode.get()) : subframeForHitTestResult(mev);
1449 // We want mouseouts to happen first, from the inside out. First send a move event to the last subframe so that it will fire mouseouts.
1450 if (m_lastMouseMoveEventSubframe && m_lastMouseMoveEventSubframe->tree().isDescendantOf(m_frame) && m_lastMouseMoveEventSubframe != newSubframe)
1451 passMouseMoveEventToSubframe(mev, m_lastMouseMoveEventSubframe.get());
1454 // Update over/out state before passing the event to the subframe.
1455 updateMouseEventTargetNode(mev.innerNode(), mouseEvent, true);
1457 // Event dispatch in updateMouseEventTargetNode may have caused the subframe of the target
1458 // node to be detached from its FrameView, in which case the event should not be passed.
1459 if (newSubframe->view())
1460 swallowEvent |= passMouseMoveEventToSubframe(mev, newSubframe.get(), hoveredNode);
1462 if (scrollbar && !m_mousePressed)
1463 scrollbar->mouseMoved(mouseEvent); // Handle hover effects on platforms that support visual feedback on scrollbar hovering.
1464 if (FrameView* view = m_frame->view()) {
1465 OptionalCursor optionalCursor = selectCursor(mev.hitTestResult());
1466 if (optionalCursor.isCursorChange()) {
1467 m_currentMouseCursor = optionalCursor.cursor();
1468 view->setCursor(m_currentMouseCursor);
1473 m_lastMouseMoveEventSubframe = newSubframe;
1478 swallowEvent = !dispatchMouseEvent(EventTypeNames::mousemove, mev.innerNode(), 0, mouseEvent, true);
1480 swallowEvent = handleMouseDraggedEvent(mev);
1482 return swallowEvent;
1485 void EventHandler::invalidateClick()
1488 m_clickNode = nullptr;
1491 static Node* parentForClickEvent(const Node& node)
1493 // IE doesn't dispatch click events for mousedown/mouseup events across form
1495 if (node.isHTMLElement() && toHTMLElement(node).isInteractiveContent())
1497 return NodeRenderingTraversal::parent(&node);
1500 bool EventHandler::handleMouseReleaseEvent(const PlatformMouseEvent& mouseEvent)
1502 TRACE_EVENT0("blink", "EventHandler::handleMouseReleaseEvent");
1504 RefPtr<FrameView> protector(m_frame->view());
1506 m_frame->selection().setCaretBlinkingSuspended(false);
1508 OwnPtr<UserGestureIndicator> gestureIndicator;
1510 if (m_frame->localFrameRoot()->eventHandler().m_lastMouseDownUserGestureToken)
1511 gestureIndicator = adoptPtr(new UserGestureIndicator(m_frame->localFrameRoot()->eventHandler().m_lastMouseDownUserGestureToken.release()));
1513 gestureIndicator = adoptPtr(new UserGestureIndicator(DefinitelyProcessingUserGesture));
1516 if (Page* page = m_frame->page())
1517 page->autoscrollController().handleMouseReleaseForPanScrolling(m_frame, mouseEvent);
1520 m_mousePressed = false;
1521 setLastKnownMousePosition(mouseEvent);
1525 m_frame->document()->accessSVGExtensions().updatePan(m_frame->view()->windowToContents(m_lastKnownMousePosition));
1529 if (m_frameSetBeingResized)
1530 return !dispatchMouseEvent(EventTypeNames::mouseup, m_frameSetBeingResized.get(), m_clickCount, mouseEvent, false);
1532 if (m_lastScrollbarUnderMouse) {
1534 m_lastScrollbarUnderMouse->mouseUp(mouseEvent);
1535 bool setUnder = false;
1536 return !dispatchMouseEvent(EventTypeNames::mouseup, m_lastNodeUnderMouse.get(), m_clickCount, mouseEvent, setUnder);
1539 // Mouse events simulated from touch should not hit-test again.
1540 ASSERT(!mouseEvent.fromTouch());
1542 HitTestRequest::HitTestRequestType hitType = HitTestRequest::Release;
1543 HitTestRequest request(hitType);
1544 MouseEventWithHitTestResults mev = prepareMouseEvent(request, mouseEvent);
1545 LocalFrame* subframe = m_capturingMouseEventsNode.get() ? subframeForTargetNode(m_capturingMouseEventsNode.get()) : subframeForHitTestResult(mev);
1546 if (m_eventHandlerWillResetCapturingMouseEventsNode)
1547 m_capturingMouseEventsNode = nullptr;
1548 if (subframe && passMouseReleaseEventToSubframe(mev, subframe))
1551 bool swallowMouseUpEvent = !dispatchMouseEvent(EventTypeNames::mouseup, mev.innerNode(), m_clickCount, mouseEvent, false);
1553 bool contextMenuEvent = mouseEvent.button() == RightButton;
1555 // FIXME: The Mac port achieves the same behavior by checking whether the context menu is currently open in WebPage::mouseEvent(). Consider merging the implementations.
1556 if (mouseEvent.button() == LeftButton && mouseEvent.modifiers() & PlatformEvent::CtrlKey)
1557 contextMenuEvent = true;
1560 bool swallowClickEvent = false;
1561 if (m_clickCount > 0 && !contextMenuEvent && mev.innerNode() && m_clickNode) {
1562 if (Node* clickTargetNode = mev.innerNode()->commonAncestor(*m_clickNode, parentForClickEvent))
1563 swallowClickEvent = !dispatchMouseEvent(EventTypeNames::click, clickTargetNode, m_clickCount, mouseEvent, true);
1566 if (m_resizeScrollableArea) {
1567 m_resizeScrollableArea->setInResizeMode(false);
1568 m_resizeScrollableArea = 0;
1571 bool swallowMouseReleaseEvent = false;
1572 if (!swallowMouseUpEvent)
1573 swallowMouseReleaseEvent = handleMouseReleaseEvent(mev);
1577 return swallowMouseUpEvent || swallowClickEvent || swallowMouseReleaseEvent;
1580 bool EventHandler::handlePasteGlobalSelection(const PlatformMouseEvent& mouseEvent)
1582 // If the event was a middle click, attempt to copy global selection in after
1583 // the newly set caret position.
1585 // This code is called from either the mouse up or mouse down handling. There
1586 // is some debate about when the global selection is pasted:
1587 // xterm: pastes on up.
1588 // GTK: pastes on down.
1589 // Qt: pastes on up.
1590 // Firefox: pastes on up.
1591 // Chromium: pastes on up.
1593 // There is something of a webcompat angle to this well, as highlighted by
1594 // crbug.com/14608. Pages can clear text boxes 'onclick' and, if we paste on
1595 // down then the text is pasted just before the onclick handler runs and
1596 // clears the text box. So it's important this happens after the event
1597 // handlers have been fired.
1598 if (mouseEvent.type() != PlatformEvent::MouseReleased)
1601 if (!m_frame->page())
1603 Frame* focusFrame = m_frame->page()->focusController().focusedOrMainFrame();
1604 // Do not paste here if the focus was moved somewhere else.
1605 if (m_frame == focusFrame && m_frame->editor().behavior().supportsGlobalSelection())
1606 return m_frame->editor().command("PasteGlobalSelection").execute();
1612 bool EventHandler::dispatchDragEvent(const AtomicString& eventType, Node* dragTarget, const PlatformMouseEvent& event, DataTransfer* dataTransfer)
1614 FrameView* view = m_frame->view();
1616 // FIXME: We might want to dispatch a dragleave even if the view is gone.
1620 RefPtrWillBeRawPtr<MouseEvent> me = MouseEvent::create(eventType,
1621 true, true, m_frame->document()->domWindow(),
1622 0, event.globalPosition().x(), event.globalPosition().y(), event.position().x(), event.position().y(),
1623 event.movementDelta().x(), event.movementDelta().y(),
1624 event.ctrlKey(), event.altKey(), event.shiftKey(), event.metaKey(),
1625 0, nullptr, dataTransfer);
1627 dragTarget->dispatchEvent(me.get(), IGNORE_EXCEPTION);
1628 return me->defaultPrevented();
1631 static bool targetIsFrame(Node* target, LocalFrame*& frame)
1633 if (!isHTMLFrameElementBase(target))
1636 // Cross-process drag and drop is not yet supported.
1637 if (toHTMLFrameElementBase(target)->contentFrame() && !toHTMLFrameElementBase(target)->contentFrame()->isLocalFrame())
1640 frame = toLocalFrame(toHTMLFrameElementBase(target)->contentFrame());
1644 static bool findDropZone(Node* target, DataTransfer* dataTransfer)
1646 Element* element = target->isElementNode() ? toElement(target) : target->parentElement();
1647 for (; element; element = element->parentElement()) {
1648 bool matched = false;
1649 AtomicString dropZoneStr = element->fastGetAttribute(webkitdropzoneAttr);
1651 if (dropZoneStr.isEmpty())
1654 UseCounter::count(element->document(), UseCounter::PrefixedHTMLElementDropzone);
1656 dropZoneStr = dropZoneStr.lower();
1658 SpaceSplitString keywords(dropZoneStr, false);
1659 if (keywords.isNull())
1662 DragOperation dragOperation = DragOperationNone;
1663 for (unsigned i = 0; i < keywords.size(); i++) {
1664 DragOperation op = convertDropZoneOperationToDragOperation(keywords[i]);
1665 if (op != DragOperationNone) {
1666 if (dragOperation == DragOperationNone)
1669 matched = matched || dataTransfer->hasDropZoneType(keywords[i].string());
1672 if (matched && dragOperation != DragOperationNone)
1676 dataTransfer->setDropEffect(convertDragOperationToDropZoneOperation(dragOperation));
1683 bool EventHandler::updateDragAndDrop(const PlatformMouseEvent& event, DataTransfer* dataTransfer)
1685 bool accept = false;
1687 if (!m_frame->view())
1690 HitTestRequest request(HitTestRequest::ReadOnly);
1691 MouseEventWithHitTestResults mev = prepareMouseEvent(request, event);
1693 // Drag events should never go to text nodes (following IE, and proper mouseover/out dispatch)
1694 RefPtrWillBeRawPtr<Node> newTarget = mev.innerNode();
1695 if (newTarget && newTarget->isTextNode())
1696 newTarget = NodeRenderingTraversal::parent(newTarget.get());
1698 if (AutoscrollController* controller = autoscrollController())
1699 controller->updateDragAndDrop(newTarget.get(), event.position(), event.timestamp());
1701 if (m_dragTarget != newTarget) {
1702 // FIXME: this ordering was explicitly chosen to match WinIE. However,
1703 // it is sometimes incorrect when dragging within subframes, as seen with
1704 // LayoutTests/fast/events/drag-in-frames.html.
1706 // Moreover, this ordering conforms to section 7.9.4 of the HTML 5 spec. <http://dev.w3.org/html5/spec/Overview.html#drag-and-drop-processing-model>.
1707 LocalFrame* targetFrame;
1708 if (targetIsFrame(newTarget.get(), targetFrame)) {
1710 accept = targetFrame->eventHandler().updateDragAndDrop(event, dataTransfer);
1711 } else if (newTarget) {
1712 // As per section 7.9.4 of the HTML 5 spec., we must always fire a drag event before firing a dragenter, dragleave, or dragover event.
1713 if (dragState().m_dragSrc) {
1714 // for now we don't care if event handler cancels default behavior, since there is none
1715 dispatchDragSrcEvent(EventTypeNames::drag, event);
1717 accept = dispatchDragEvent(EventTypeNames::dragenter, newTarget.get(), event, dataTransfer);
1719 accept = findDropZone(newTarget.get(), dataTransfer);
1722 if (targetIsFrame(m_dragTarget.get(), targetFrame)) {
1724 accept = targetFrame->eventHandler().updateDragAndDrop(event, dataTransfer);
1725 } else if (m_dragTarget) {
1726 dispatchDragEvent(EventTypeNames::dragleave, m_dragTarget.get(), event, dataTransfer);
1730 // We do not explicitly call dispatchDragEvent here because it could ultimately result in the appearance that
1731 // two dragover events fired. So, we mark that we should only fire a dragover event on the next call to this function.
1732 m_shouldOnlyFireDragOverEvent = true;
1735 LocalFrame* targetFrame;
1736 if (targetIsFrame(newTarget.get(), targetFrame)) {
1738 accept = targetFrame->eventHandler().updateDragAndDrop(event, dataTransfer);
1739 } else if (newTarget) {
1740 // Note, when dealing with sub-frames, we may need to fire only a dragover event as a drag event may have been fired earlier.
1741 if (!m_shouldOnlyFireDragOverEvent && dragState().m_dragSrc) {
1742 // for now we don't care if event handler cancels default behavior, since there is none
1743 dispatchDragSrcEvent(EventTypeNames::drag, event);
1745 accept = dispatchDragEvent(EventTypeNames::dragover, newTarget.get(), event, dataTransfer);
1747 accept = findDropZone(newTarget.get(), dataTransfer);
1748 m_shouldOnlyFireDragOverEvent = false;
1751 m_dragTarget = newTarget;
1756 void EventHandler::cancelDragAndDrop(const PlatformMouseEvent& event, DataTransfer* dataTransfer)
1758 LocalFrame* targetFrame;
1759 if (targetIsFrame(m_dragTarget.get(), targetFrame)) {
1761 targetFrame->eventHandler().cancelDragAndDrop(event, dataTransfer);
1762 } else if (m_dragTarget.get()) {
1763 if (dragState().m_dragSrc)
1764 dispatchDragSrcEvent(EventTypeNames::drag, event);
1765 dispatchDragEvent(EventTypeNames::dragleave, m_dragTarget.get(), event, dataTransfer);
1770 bool EventHandler::performDragAndDrop(const PlatformMouseEvent& event, DataTransfer* dataTransfer)
1772 LocalFrame* targetFrame;
1773 bool preventedDefault = false;
1774 if (targetIsFrame(m_dragTarget.get(), targetFrame)) {
1776 preventedDefault = targetFrame->eventHandler().performDragAndDrop(event, dataTransfer);
1777 } else if (m_dragTarget.get()) {
1778 preventedDefault = dispatchDragEvent(EventTypeNames::drop, m_dragTarget.get(), event, dataTransfer);
1781 return preventedDefault;
1784 void EventHandler::clearDragState()
1787 m_dragTarget = nullptr;
1788 m_capturingMouseEventsNode = nullptr;
1789 m_shouldOnlyFireDragOverEvent = false;
1792 void EventHandler::setCapturingMouseEventsNode(PassRefPtrWillBeRawPtr<Node> n)
1794 m_capturingMouseEventsNode = n;
1795 m_eventHandlerWillResetCapturingMouseEventsNode = false;
1798 MouseEventWithHitTestResults EventHandler::prepareMouseEvent(const HitTestRequest& request, const PlatformMouseEvent& mev)
1801 ASSERT(m_frame->document());
1803 return m_frame->document()->prepareMouseEvent(request, documentPointForWindowPoint(m_frame, mev.position()), mev);
1806 void EventHandler::updateMouseEventTargetNode(Node* targetNode, const PlatformMouseEvent& mouseEvent, bool fireMouseOverOut)
1808 Node* result = targetNode;
1810 // If we're capturing, we always go right to that node.
1811 if (m_capturingMouseEventsNode)
1812 result = m_capturingMouseEventsNode.get();
1814 // If the target node is a text node, dispatch on the parent node - rdar://4196646
1815 if (result && result->isTextNode())
1816 result = NodeRenderingTraversal::parent(result);
1818 m_nodeUnderMouse = result;
1820 // Fire mouseout/mouseover if the mouse has shifted to a different node.
1821 if (fireMouseOverOut) {
1822 RenderLayer* layerForLastNode = layerForNode(m_lastNodeUnderMouse.get());
1823 RenderLayer* layerForNodeUnderMouse = layerForNode(m_nodeUnderMouse.get());
1824 Page* page = m_frame->page();
1826 if (m_lastNodeUnderMouse && (!m_nodeUnderMouse || m_nodeUnderMouse->document() != m_frame->document())) {
1827 // The mouse has moved between frames.
1828 if (LocalFrame* frame = m_lastNodeUnderMouse->document().frame()) {
1829 if (FrameView* frameView = frame->view())
1830 frameView->mouseExitedContentArea();
1832 } else if (page && (layerForLastNode && (!layerForNodeUnderMouse || layerForNodeUnderMouse != layerForLastNode))) {
1833 // The mouse has moved between layers.
1834 if (ScrollableArea* scrollableAreaForLastNode = associatedScrollableArea(layerForLastNode))
1835 scrollableAreaForLastNode->mouseExitedContentArea();
1838 if (m_nodeUnderMouse && (!m_lastNodeUnderMouse || m_lastNodeUnderMouse->document() != m_frame->document())) {
1839 // The mouse has moved between frames.
1840 if (LocalFrame* frame = m_nodeUnderMouse->document().frame()) {
1841 if (FrameView* frameView = frame->view())
1842 frameView->mouseEnteredContentArea();
1844 } else if (page && (layerForNodeUnderMouse && (!layerForLastNode || layerForNodeUnderMouse != layerForLastNode))) {
1845 // The mouse has moved between layers.
1846 if (ScrollableArea* scrollableAreaForNodeUnderMouse = associatedScrollableArea(layerForNodeUnderMouse))
1847 scrollableAreaForNodeUnderMouse->mouseEnteredContentArea();
1850 if (m_lastNodeUnderMouse && m_lastNodeUnderMouse->document() != m_frame->document()) {
1851 m_lastNodeUnderMouse = nullptr;
1852 m_lastScrollbarUnderMouse = nullptr;
1855 if (m_lastNodeUnderMouse != m_nodeUnderMouse) {
1856 // send mouseout event to the old node
1857 if (m_lastNodeUnderMouse)
1858 m_lastNodeUnderMouse->dispatchMouseEvent(mouseEvent, EventTypeNames::mouseout, 0, m_nodeUnderMouse.get());
1859 // send mouseover event to the new node
1860 if (m_nodeUnderMouse)
1861 m_nodeUnderMouse->dispatchMouseEvent(mouseEvent, EventTypeNames::mouseover, 0, m_lastNodeUnderMouse.get());
1863 m_lastNodeUnderMouse = m_nodeUnderMouse;
1867 // The return value means 'continue default handling.'
1868 bool EventHandler::dispatchMouseEvent(const AtomicString& eventType, Node* targetNode, int clickCount, const PlatformMouseEvent& mouseEvent, bool setUnder)
1870 updateMouseEventTargetNode(targetNode, mouseEvent, setUnder);
1871 return !m_nodeUnderMouse || m_nodeUnderMouse->dispatchMouseEvent(mouseEvent, eventType, clickCount);
1874 // The return value means 'swallow event' (was handled), as for other handle* functions.
1875 bool EventHandler::handleMouseFocus(const PlatformMouseEvent& mouseEvent)
1877 // If clicking on a frame scrollbar, do not mess up with content focus.
1878 if (FrameView* view = m_frame->view()) {
1879 if (view->scrollbarAtWindowPoint(mouseEvent.position()))
1883 // The layout needs to be up to date to determine if an element is focusable.
1884 m_frame->document()->updateLayoutIgnorePendingStylesheets();
1886 Element* element = 0;
1887 if (m_nodeUnderMouse)
1888 element = m_nodeUnderMouse->isElementNode() ? toElement(m_nodeUnderMouse) : m_nodeUnderMouse->parentOrShadowHostElement();
1889 for (; element; element = element->parentOrShadowHostElement()) {
1890 if (element->isFocusable() && element->focused())
1892 if (element->isMouseFocusable())
1895 ASSERT(!element || element->isMouseFocusable());
1897 // To fix <rdar://problem/4895428> Can't drag selected ToDo, we don't focus
1898 // a node on mouse down if it's selected and inside a focused node. It will
1899 // be focused if the user does a mouseup over it, however, because the
1900 // mouseup will set a selection inside it, which will call
1901 // FrameSelection::setFocusedNodeIfNeeded.
1903 && m_frame->selection().isRange()
1904 && m_frame->selection().toNormalizedRange()->compareNode(element, IGNORE_EXCEPTION) == Range::NODE_INSIDE
1905 && element->isDescendantOf(m_frame->document()->focusedElement()))
1908 // Only change the focus when clicking scrollbars if it can transfered to a
1909 // mouse focusable node.
1910 if (!element && isInsideScrollbar(mouseEvent.position()))
1913 if (Page* page = m_frame->page()) {
1914 // If focus shift is blocked, we eat the event. Note we should never
1915 // clear swallowEvent if the page already set it (e.g., by canceling
1916 // default behavior).
1918 if (!page->focusController().setFocusedElement(element, m_frame, FocusTypeMouse))
1921 // We call setFocusedElement even with !element in order to blur
1922 // current focus element when a link is clicked; this is expected by
1923 // some sites that rely on onChange handlers running from form
1924 // fields before the button click is processed.
1925 if (!page->focusController().setFocusedElement(0, m_frame))
1933 bool EventHandler::isInsideScrollbar(const IntPoint& windowPoint) const
1935 if (RenderView* renderView = m_frame->contentRenderer()) {
1936 HitTestRequest request(HitTestRequest::ReadOnly);
1937 HitTestResult result(windowPoint);
1938 renderView->hitTest(request, result);
1939 return result.scrollbar();
1945 bool EventHandler::handleWheelEvent(const PlatformWheelEvent& event)
1947 #define RETURN_WHEEL_EVENT_HANDLED() \
1949 setFrameWasScrolledByUser(); \
1953 Document* doc = m_frame->document();
1955 if (!doc->renderView())
1958 RefPtr<FrameView> protector(m_frame->view());
1960 FrameView* view = m_frame->view();
1964 LayoutPoint vPoint = view->windowToContents(event.position());
1966 HitTestRequest request(HitTestRequest::ReadOnly);
1967 HitTestResult result(vPoint);
1968 doc->renderView()->hitTest(request, result);
1970 Node* node = result.innerNode();
1971 // Wheel events should not dispatch to text nodes.
1972 if (node && node->isTextNode())
1973 node = NodeRenderingTraversal::parent(node);
1976 if (event.useLatchedEventNode()) {
1977 if (!m_latchedWheelEventNode) {
1978 m_latchedWheelEventNode = node;
1979 m_widgetIsLatched = result.isOverWidget();
1981 node = m_latchedWheelEventNode.get();
1983 isOverWidget = m_widgetIsLatched;
1985 if (m_latchedWheelEventNode)
1986 m_latchedWheelEventNode = nullptr;
1987 if (m_previousWheelScrolledNode)
1988 m_previousWheelScrolledNode = nullptr;
1990 isOverWidget = result.isOverWidget();
1994 // Figure out which view to send the event to.
1995 RenderObject* target = node->renderer();
1997 if (isOverWidget && target && target->isWidget()) {
1998 Widget* widget = toRenderWidget(target)->widget();
1999 if (widget && passWheelEventToWidget(event, widget))
2000 RETURN_WHEEL_EVENT_HANDLED();
2003 if (node && !node->dispatchWheelEvent(event))
2004 RETURN_WHEEL_EVENT_HANDLED();
2007 // We do another check on the frame view because the event handler can run JS which results in the frame getting destroyed.
2008 view = m_frame->view();
2009 if (!view || !view->wheelEvent(event))
2012 RETURN_WHEEL_EVENT_HANDLED();
2014 #undef RETURN_WHEEL_EVENT_HANDLED
2017 void EventHandler::defaultWheelEventHandler(Node* startNode, WheelEvent* wheelEvent)
2019 if (!startNode || !wheelEvent)
2022 // Ctrl + scrollwheel is reserved for triggering zoom in/out actions in Chromium.
2023 if (wheelEvent->ctrlKey())
2026 Node* stopNode = m_previousWheelScrolledNode.get();
2027 ScrollGranularity granularity = wheelGranularityToScrollGranularity(wheelEvent->deltaMode());
2029 // Break up into two scrolls if we need to. Diagonal movement on
2030 // a MacBook pro is an example of a 2-dimensional mouse wheel event (where both deltaX and deltaY can be set).
2031 if (scroll(ScrollRight, granularity, startNode, &stopNode, wheelEvent->deltaX(), roundedIntPoint(wheelEvent->absoluteLocation())))
2032 wheelEvent->setDefaultHandled();
2034 if (scroll(ScrollDown, granularity, startNode, &stopNode, wheelEvent->deltaY(), roundedIntPoint(wheelEvent->absoluteLocation())))
2035 wheelEvent->setDefaultHandled();
2037 if (!m_latchedWheelEventNode)
2038 m_previousWheelScrolledNode = stopNode;
2041 bool EventHandler::handleGestureShowPress()
2043 m_lastShowPressTimestamp = WTF::currentTime();
2045 FrameView* view = m_frame->view();
2048 if (ScrollAnimator* scrollAnimator = view->existingScrollAnimator())
2049 scrollAnimator->cancelAnimations();
2050 const FrameView::ScrollableAreaSet* areas = view->scrollableAreas();
2053 for (FrameView::ScrollableAreaSet::const_iterator it = areas->begin(); it != areas->end(); ++it) {
2054 ScrollableArea* sa = *it;
2055 ScrollAnimator* animator = sa->existingScrollAnimator();
2057 animator->cancelAnimations();
2062 bool EventHandler::handleGestureEvent(const PlatformGestureEvent& gestureEvent)
2064 // Propagation to inner frames is handled below this function.
2065 ASSERT(m_frame == m_frame->localFrameRoot());
2067 // Scrolling-related gesture events invoke EventHandler recursively for each frame down
2068 // the chain, doing a single-frame hit-test per frame. This matches handleWheelEvent.
2069 // FIXME: Add a test that traverses this path, e.g. for devtools overlay.
2070 if (gestureEvent.isScrollEvent())
2071 return handleGestureScrollEvent(gestureEvent);
2073 // Hit test across all frames and do touch adjustment as necessary for the event type.
2074 GestureEventWithHitTestResults targetedEvent = targetGestureEvent(gestureEvent);
2076 return handleGestureEvent(targetedEvent);
2079 bool EventHandler::handleGestureEvent(const GestureEventWithHitTestResults& targetedEvent)
2081 TRACE_EVENT0("input", "EventHandler::handleGestureEvent");
2083 // Propagation to inner frames is handled below this function.
2084 ASSERT(m_frame == m_frame->localFrameRoot());
2086 // Non-scrolling related gesture events do a single cross-frame hit-test and jump
2087 // directly to the inner most frame. This matches handleMousePressEvent etc.
2088 ASSERT(!targetedEvent.event().isScrollEvent());
2090 // Route to the correct frame.
2091 if (LocalFrame* innerFrame = targetedEvent.hitTestResult().innerNodeFrame())
2092 return innerFrame->eventHandler().handleGestureEventInFrame(targetedEvent);
2094 // No hit test result, handle in root instance. Perhaps we should just return false instead?
2095 return handleGestureEventInFrame(targetedEvent);
2098 bool EventHandler::handleGestureEventInFrame(const GestureEventWithHitTestResults& targetedEvent)
2100 ASSERT(!targetedEvent.event().isScrollEvent());
2102 RefPtrWillBeRawPtr<Node> eventTarget = targetedEvent.hitTestResult().innerNode();
2103 RefPtr<Scrollbar> scrollbar = targetedEvent.hitTestResult().scrollbar();
2104 const PlatformGestureEvent& gestureEvent = targetedEvent.event();
2107 bool eventSwallowed = scrollbar->gestureEvent(gestureEvent);
2108 if (gestureEvent.type() == PlatformEvent::GestureTapDown && eventSwallowed)
2109 m_scrollbarHandlingScrollGesture = scrollbar;
2114 if (eventTarget && eventTarget->dispatchGestureEvent(gestureEvent))
2117 switch (gestureEvent.type()) {
2118 case PlatformEvent::GestureTap:
2119 return handleGestureTap(targetedEvent);
2120 case PlatformEvent::GestureShowPress:
2121 return handleGestureShowPress();
2122 case PlatformEvent::GestureLongPress:
2123 return handleGestureLongPress(targetedEvent);
2124 case PlatformEvent::GestureLongTap:
2125 return handleGestureLongTap(targetedEvent);
2126 case PlatformEvent::GestureTwoFingerTap:
2127 return sendContextMenuEventForGesture(targetedEvent);
2128 case PlatformEvent::GestureTapDown:
2129 case PlatformEvent::GesturePinchBegin:
2130 case PlatformEvent::GesturePinchEnd:
2131 case PlatformEvent::GesturePinchUpdate:
2132 case PlatformEvent::GestureTapDownCancel:
2133 case PlatformEvent::GestureTapUnconfirmed:
2136 ASSERT_NOT_REACHED();
2142 bool EventHandler::handleGestureScrollEvent(const PlatformGestureEvent& gestureEvent)
2144 TRACE_EVENT0("input", "EventHandler::handleGestureScrollEvent");
2146 RefPtrWillBeRawPtr<Node> eventTarget = nullptr;
2147 RefPtr<Scrollbar> scrollbar;
2148 if (gestureEvent.type() != PlatformEvent::GestureScrollBegin) {
2149 scrollbar = m_scrollbarHandlingScrollGesture.get();
2150 eventTarget = m_scrollGestureHandlingNode.get();
2154 Document* document = m_frame->document();
2155 if (!document->renderView())
2158 FrameView* view = m_frame->view();
2159 LayoutPoint viewPoint = view->windowToContents(gestureEvent.position());
2160 HitTestRequest request(HitTestRequest::ReadOnly);
2161 HitTestResult result(viewPoint);
2162 document->renderView()->hitTest(request, result);
2164 eventTarget = result.innerNode();
2166 m_lastGestureScrollOverWidget = result.isOverWidget();
2167 m_scrollGestureHandlingNode = eventTarget;
2168 m_previousGestureScrolledNode = nullptr;
2171 scrollbar = result.scrollbar();
2175 bool eventSwallowed = scrollbar->gestureEvent(gestureEvent);
2176 if (gestureEvent.type() == PlatformEvent::GestureScrollEnd
2177 || gestureEvent.type() == PlatformEvent::GestureFlingStart
2178 || !eventSwallowed) {
2179 m_scrollbarHandlingScrollGesture = nullptr;
2186 bool eventSwallowed = handleScrollGestureOnResizer(eventTarget.get(), gestureEvent);
2187 if (!eventSwallowed)
2188 eventSwallowed = eventTarget->dispatchGestureEvent(gestureEvent);
2193 switch (gestureEvent.type()) {
2194 case PlatformEvent::GestureScrollBegin:
2195 return handleGestureScrollBegin(gestureEvent);
2196 case PlatformEvent::GestureScrollUpdate:
2197 case PlatformEvent::GestureScrollUpdateWithoutPropagation:
2198 return handleGestureScrollUpdate(gestureEvent);
2199 case PlatformEvent::GestureScrollEnd:
2200 return handleGestureScrollEnd(gestureEvent);
2201 case PlatformEvent::GestureFlingStart:
2202 case PlatformEvent::GesturePinchBegin:
2203 case PlatformEvent::GesturePinchEnd:
2204 case PlatformEvent::GesturePinchUpdate:
2207 ASSERT_NOT_REACHED();
2212 bool EventHandler::handleGestureTap(const GestureEventWithHitTestResults& targetedEvent)
2214 RefPtr<FrameView> protector(m_frame->view());
2215 const PlatformGestureEvent& gestureEvent = targetedEvent.event();
2216 HitTestRequest::HitTestRequestType hitType = getHitTypeForGestureType(gestureEvent.type());
2218 UserGestureIndicator gestureIndicator(DefinitelyProcessingUserGesture);
2220 unsigned modifierFlags = 0;
2221 if (gestureEvent.altKey())
2222 modifierFlags |= PlatformEvent::AltKey;
2223 if (gestureEvent.ctrlKey())
2224 modifierFlags |= PlatformEvent::CtrlKey;
2225 if (gestureEvent.metaKey())
2226 modifierFlags |= PlatformEvent::MetaKey;
2227 if (gestureEvent.shiftKey())
2228 modifierFlags |= PlatformEvent::ShiftKey;
2229 PlatformEvent::Modifiers modifiers = static_cast<PlatformEvent::Modifiers>(modifierFlags);
2231 HitTestResult currentHitTest = targetedEvent.hitTestResult();
2233 // We use the adjusted position so the application isn't surprised to see a event with
2234 // co-ordinates outside the target's bounds.
2235 IntPoint adjustedPoint = m_frame->view()->windowToContents(gestureEvent.position());
2237 PlatformMouseEvent fakeMouseMove(adjustedPoint, gestureEvent.globalPosition(),
2238 NoButton, PlatformEvent::MouseMoved, /* clickCount */ 0,
2239 modifiers, PlatformMouseEvent::FromTouch, gestureEvent.timestamp());
2240 dispatchMouseEvent(EventTypeNames::mousemove, currentHitTest.innerNode(), 0, fakeMouseMove, true);
2242 // Do a new hit-test in case the mousemove event changed the DOM.
2243 // Note that if the original hit test wasn't over an element (eg. was over a scrollbar) we
2244 // don't want to re-hit-test because it may be in the wrong frame (and there's no way the page
2245 // could have seen the event anyway).
2246 // FIXME: Use a hit-test cache to avoid unnecessary hit tests. http://crbug.com/398920
2247 if (currentHitTest.innerNode())
2248 currentHitTest = hitTestResultInFrame(m_frame, adjustedPoint, hitType);
2249 m_clickNode = currentHitTest.innerNode();
2250 if (m_clickNode && m_clickNode->isTextNode())
2251 m_clickNode = NodeRenderingTraversal::parent(m_clickNode.get());
2253 PlatformMouseEvent fakeMouseDown(adjustedPoint, gestureEvent.globalPosition(),
2254 LeftButton, PlatformEvent::MousePressed, gestureEvent.tapCount(),
2255 modifiers, PlatformMouseEvent::FromTouch, gestureEvent.timestamp());
2256 bool swallowMouseDownEvent = !dispatchMouseEvent(EventTypeNames::mousedown, currentHitTest.innerNode(), gestureEvent.tapCount(), fakeMouseDown, true);
2257 if (!swallowMouseDownEvent)
2258 swallowMouseDownEvent = handleMouseFocus(fakeMouseDown);
2259 if (!swallowMouseDownEvent)
2260 swallowMouseDownEvent = handleMousePressEvent(MouseEventWithHitTestResults(fakeMouseDown, currentHitTest));
2262 // FIXME: Use a hit-test cache to avoid unnecessary hit tests. http://crbug.com/398920
2263 if (currentHitTest.innerNode())
2264 currentHitTest = hitTestResultInFrame(m_frame, adjustedPoint, hitType);
2265 PlatformMouseEvent fakeMouseUp(adjustedPoint, gestureEvent.globalPosition(),
2266 LeftButton, PlatformEvent::MouseReleased, gestureEvent.tapCount(),
2267 modifiers, PlatformMouseEvent::FromTouch, gestureEvent.timestamp());
2268 bool swallowMouseUpEvent = !dispatchMouseEvent(EventTypeNames::mouseup, currentHitTest.innerNode(), gestureEvent.tapCount(), fakeMouseUp, false);
2270 bool swallowClickEvent = false;
2272 if (currentHitTest.innerNode()) {
2273 Node* clickTargetNode = currentHitTest.innerNode()->commonAncestor(*m_clickNode, parentForClickEvent);
2274 swallowClickEvent = !dispatchMouseEvent(EventTypeNames::click, clickTargetNode, gestureEvent.tapCount(), fakeMouseUp, true);
2276 m_clickNode = nullptr;
2279 if (!swallowMouseUpEvent)
2280 swallowMouseUpEvent = handleMouseReleaseEvent(MouseEventWithHitTestResults(fakeMouseUp, currentHitTest));
2282 return swallowMouseDownEvent | swallowMouseUpEvent | swallowClickEvent;
2285 bool EventHandler::handleGestureLongPress(const GestureEventWithHitTestResults& targetedEvent)
2287 const PlatformGestureEvent& gestureEvent = targetedEvent.event();
2288 IntPoint adjustedPoint = gestureEvent.position();
2290 // FIXME: Ideally we should try to remove the extra mouse-specific hit-tests here (re-using the
2291 // supplied HitTestResult), but that will require some overhaul of the touch drag-and-drop code
2292 // and LongPress is such a special scenario that it's unlikely to matter much in practice.
2294 m_longTapShouldInvokeContextMenu = false;
2295 if (m_frame->settings() && m_frame->settings()->touchDragDropEnabled() && m_frame->view()) {
2296 PlatformMouseEvent mouseDownEvent(adjustedPoint, gestureEvent.globalPosition(), LeftButton, PlatformEvent::MousePressed, 1,
2297 gestureEvent.shiftKey(), gestureEvent.ctrlKey(), gestureEvent.altKey(), gestureEvent.metaKey(), PlatformMouseEvent::FromTouch, WTF::currentTime());
2298 m_mouseDown = mouseDownEvent;
2300 PlatformMouseEvent mouseDragEvent(adjustedPoint, gestureEvent.globalPosition(), LeftButton, PlatformEvent::MouseMoved, 1,
2301 gestureEvent.shiftKey(), gestureEvent.ctrlKey(), gestureEvent.altKey(), gestureEvent.metaKey(), PlatformMouseEvent::FromTouch, WTF::currentTime());
2302 HitTestRequest request(HitTestRequest::ReadOnly);
2303 MouseEventWithHitTestResults mev = prepareMouseEvent(request, mouseDragEvent);
2304 m_didStartDrag = false;
2305 m_mouseDownMayStartDrag = true;
2306 dragState().m_dragSrc = nullptr;
2307 m_mouseDownPos = m_frame->view()->windowToContents(mouseDragEvent.position());
2308 RefPtr<FrameView> protector(m_frame->view());
2309 handleDrag(mev, DontCheckDragHysteresis);
2310 if (m_didStartDrag) {
2311 m_longTapShouldInvokeContextMenu = true;
2316 bool shouldLongPressSelectWord = true;
2318 bool shouldLongPressSelectWord = m_frame->settings() && m_frame->settings()->touchEditingEnabled();
2320 if (shouldLongPressSelectWord) {
2321 IntPoint hitTestPoint = m_frame->view()->windowToContents(gestureEvent.position());
2322 HitTestResult result = hitTestResultAtPoint(hitTestPoint);
2323 Node* innerNode = result.innerNode();
2324 if (!result.isLiveLink() && innerNode && (innerNode->isContentEditable() || innerNode->isTextNode())) {
2325 selectClosestWordFromHitTestResult(result, DontAppendTrailingWhitespace);
2326 if (m_frame->selection().isRange()) {
2327 focusDocumentView();
2332 return sendContextMenuEventForGesture(targetedEvent);
2335 bool EventHandler::handleGestureLongTap(const GestureEventWithHitTestResults& targetedEvent)
2338 if (m_longTapShouldInvokeContextMenu) {
2339 m_longTapShouldInvokeContextMenu = false;
2340 return sendContextMenuEventForGesture(targetedEvent);
2346 bool EventHandler::handleScrollGestureOnResizer(Node* eventTarget, const PlatformGestureEvent& gestureEvent) {
2347 if (gestureEvent.type() == PlatformEvent::GestureScrollBegin) {
2348 RenderLayer* layer = eventTarget->renderer() ? eventTarget->renderer()->enclosingLayer() : 0;
2349 IntPoint p = m_frame->view()->windowToContents(gestureEvent.position());
2350 if (layer && layer->scrollableArea() && layer->scrollableArea()->isPointInResizeControl(p, ResizerForTouch)) {
2351 m_resizeScrollableArea = layer->scrollableArea();
2352 m_resizeScrollableArea->setInResizeMode(true);
2353 m_offsetFromResizeCorner = m_resizeScrollableArea->offsetFromResizeCorner(p);
2356 } else if (gestureEvent.type() == PlatformEvent::GestureScrollUpdate ||
2357 gestureEvent.type() == PlatformEvent::GestureScrollUpdateWithoutPropagation) {
2358 if (m_resizeScrollableArea && m_resizeScrollableArea->inResizeMode()) {
2359 m_resizeScrollableArea->resize(gestureEvent, m_offsetFromResizeCorner);
2362 } else if (gestureEvent.type() == PlatformEvent::GestureScrollEnd) {
2363 if (m_resizeScrollableArea && m_resizeScrollableArea->inResizeMode()) {
2364 m_resizeScrollableArea->setInResizeMode(false);
2365 m_resizeScrollableArea = 0;
2373 bool EventHandler::passScrollGestureEventToWidget(const PlatformGestureEvent& gestureEvent, RenderObject* renderer)
2375 ASSERT(gestureEvent.isScrollEvent());
2377 if (!m_lastGestureScrollOverWidget)
2380 if (!renderer || !renderer->isWidget())
2383 Widget* widget = toRenderWidget(renderer)->widget();
2385 if (!widget || !widget->isFrameView())
2388 return toFrameView(widget)->frame().eventHandler().handleGestureScrollEvent(gestureEvent);
2391 bool EventHandler::handleGestureScrollEnd(const PlatformGestureEvent& gestureEvent) {
2392 RefPtrWillBeRawPtr<Node> node = m_scrollGestureHandlingNode;
2393 clearGestureScrollNodes();
2396 passScrollGestureEventToWidget(gestureEvent, node->renderer());
2401 bool EventHandler::handleGestureScrollBegin(const PlatformGestureEvent& gestureEvent)
2403 Document* document = m_frame->document();
2404 if (!document->renderView())
2407 FrameView* view = m_frame->view();
2411 // If there's no renderer on the node, send the event to the nearest ancestor with a renderer.
2412 // Needed for <option> and <optgroup> elements so we can touch scroll <select>s
2413 while (m_scrollGestureHandlingNode && !m_scrollGestureHandlingNode->renderer())
2414 m_scrollGestureHandlingNode = m_scrollGestureHandlingNode->parentOrShadowHostNode();
2416 if (!m_scrollGestureHandlingNode)
2419 passScrollGestureEventToWidget(gestureEvent, m_scrollGestureHandlingNode->renderer());
2424 bool EventHandler::handleGestureScrollUpdate(const PlatformGestureEvent& gestureEvent)
2426 FloatSize delta(gestureEvent.deltaX(), gestureEvent.deltaY());
2430 const float scaleFactor = m_frame->pageZoomFactor();
2431 delta.scale(1 / scaleFactor, 1 / scaleFactor);
2433 Node* node = m_scrollGestureHandlingNode.get();
2435 return sendScrollEventToView(gestureEvent, delta);
2437 // Ignore this event if the targeted node does not have a valid renderer.
2438 RenderObject* renderer = node->renderer();
2442 RefPtr<FrameView> protector(m_frame->view());
2445 bool scrollShouldNotPropagate = gestureEvent.type() == PlatformEvent::GestureScrollUpdateWithoutPropagation;
2447 // Try to send the event to the correct view.
2448 if (passScrollGestureEventToWidget(gestureEvent, renderer)) {
2449 if(scrollShouldNotPropagate)
2450 m_previousGestureScrolledNode = m_scrollGestureHandlingNode;
2455 if (scrollShouldNotPropagate)
2456 stopNode = m_previousGestureScrolledNode.get();
2458 // First try to scroll the closest scrollable RenderBox ancestor of |node|.
2459 ScrollGranularity granularity = ScrollByPixel;
2460 bool horizontalScroll = scroll(ScrollLeft, granularity, node, &stopNode, delta.width());
2461 bool verticalScroll = scroll(ScrollUp, granularity, node, &stopNode, delta.height());
2463 if (scrollShouldNotPropagate)
2464 m_previousGestureScrolledNode = stopNode;
2466 if (horizontalScroll || verticalScroll) {
2467 setFrameWasScrolledByUser();
2471 // Otherwise try to scroll the view.
2472 return sendScrollEventToView(gestureEvent, delta);
2475 bool EventHandler::sendScrollEventToView(const PlatformGestureEvent& gestureEvent, const FloatSize& scaledDelta)
2477 FrameView* view = m_frame->view();
2481 const float tickDivisor = static_cast<float>(WheelEvent::TickMultiplier);
2482 IntPoint point(gestureEvent.position().x(), gestureEvent.position().y());
2483 IntPoint globalPoint(gestureEvent.globalPosition().x(), gestureEvent.globalPosition().y());
2484 PlatformWheelEvent syntheticWheelEvent(point, globalPoint,
2485 scaledDelta.width(), scaledDelta.height(),
2486 scaledDelta.width() / tickDivisor, scaledDelta.height() / tickDivisor,
2487 ScrollByPixelWheelEvent,
2488 gestureEvent.shiftKey(), gestureEvent.ctrlKey(), gestureEvent.altKey(), gestureEvent.metaKey());
2489 syntheticWheelEvent.setHasPreciseScrollingDeltas(true);
2491 bool scrolledFrame = view->wheelEvent(syntheticWheelEvent);
2493 setFrameWasScrolledByUser();
2495 return scrolledFrame;
2498 void EventHandler::clearGestureScrollNodes()
2500 m_scrollGestureHandlingNode = nullptr;
2501 m_previousGestureScrolledNode = nullptr;
2504 bool EventHandler::isScrollbarHandlingGestures() const
2506 return m_scrollbarHandlingScrollGesture.get();
2509 bool EventHandler::shouldApplyTouchAdjustment(const PlatformGestureEvent& event) const
2511 if (m_frame->settings() && !m_frame->settings()->touchAdjustmentEnabled())
2513 return !event.area().isEmpty();
2516 bool EventHandler::bestClickableNodeForHitTestResult(const HitTestResult& result, IntPoint& targetPoint, Node*& targetNode)
2518 // FIXME: Unify this with the other best* functions which are very similar.
2520 TRACE_EVENT0("input", "EventHandler::bestClickableNodeForHitTestResult");
2521 ASSERT(result.isRectBasedTest());
2523 // If the touch is over a scrollbar, don't adjust the touch point since touch adjustment only takes into account
2524 // DOM nodes so a touch over a scrollbar will be adjusted towards nearby nodes. This leads to things like textarea
2525 // scrollbars being untouchable.
2526 if (result.scrollbar())
2529 IntPoint touchCenter = m_frame->view()->contentsToWindow(result.roundedPointInMainFrame());
2530 IntRect touchRect = m_frame->view()->contentsToWindow(result.hitTestLocation().boundingBox());
2532 WillBeHeapVector<RefPtrWillBeMember<Node>, 11> nodes;
2533 copyToVector(result.rectBasedTestResult(), nodes);
2535 // FIXME: the explicit Vector conversion copies into a temporary and is wasteful.
2536 return findBestClickableCandidate(targetNode, targetPoint, touchCenter, touchRect, WillBeHeapVector<RefPtrWillBeMember<Node> > (nodes));
2539 bool EventHandler::bestContextMenuNodeForHitTestResult(const HitTestResult& result, IntPoint& targetPoint, Node*& targetNode)
2541 ASSERT(result.isRectBasedTest());
2542 IntPoint touchCenter = m_frame->view()->contentsToWindow(result.roundedPointInMainFrame());
2543 IntRect touchRect = m_frame->view()->contentsToWindow(result.hitTestLocation().boundingBox());
2544 WillBeHeapVector<RefPtrWillBeMember<Node>, 11> nodes;
2545 copyToVector(result.rectBasedTestResult(), nodes);
2547 // FIXME: the explicit Vector conversion copies into a temporary and is wasteful.
2548 return findBestContextMenuCandidate(targetNode, targetPoint, touchCenter, touchRect, WillBeHeapVector<RefPtrWillBeMember<Node> >(nodes));
2551 bool EventHandler::bestZoomableAreaForTouchPoint(const IntPoint& touchCenter, const IntSize& touchRadius, IntRect& targetArea, Node*& targetNode)
2553 IntPoint hitTestPoint = m_frame->view()->windowToContents(touchCenter);
2554 HitTestResult result = hitTestResultAtPoint(hitTestPoint, HitTestRequest::ReadOnly | HitTestRequest::Active, touchRadius);
2556 IntRect touchRect(touchCenter - touchRadius, touchRadius + touchRadius);
2557 WillBeHeapVector<RefPtrWillBeMember<Node>, 11> nodes;
2558 copyToVector(result.rectBasedTestResult(), nodes);
2560 // FIXME: the explicit Vector conversion copies into a temporary and is wasteful.
2561 return findBestZoomableArea(targetNode, targetArea, touchCenter, touchRect, WillBeHeapVector<RefPtrWillBeMember<Node> >(nodes));
2564 GestureEventWithHitTestResults EventHandler::targetGestureEvent(const PlatformGestureEvent& gestureEvent, bool readOnly)
2566 TRACE_EVENT0("input", "EventHandler::targetGestureEvent");
2568 ASSERT(m_frame == m_frame->localFrameRoot());
2569 // Scrolling events get hit tested per frame (like wheel events do).
2570 ASSERT(!gestureEvent.isScrollEvent());
2572 HitTestRequest::HitTestRequestType hitType = getHitTypeForGestureType(gestureEvent.type());
2573 double activeInterval = 0;
2574 bool shouldKeepActiveForMinInterval = false;
2576 hitType |= HitTestRequest::ReadOnly;
2577 } else if (gestureEvent.type() == PlatformEvent::GestureTap) {
2578 // If the Tap is received very shortly after ShowPress, we want to
2579 // delay clearing of the active state so that it's visible to the user
2580 // for at least a couple of frames.
2581 activeInterval = WTF::currentTime() - m_lastShowPressTimestamp;
2582 shouldKeepActiveForMinInterval = m_lastShowPressTimestamp && activeInterval < minimumActiveInterval;
2583 if (shouldKeepActiveForMinInterval)
2584 hitType |= HitTestRequest::ReadOnly;
2587 // Perform the rect-based hit-test (or point-based if adjustment is disabled). Note that
2588 // we don't yet apply hover/active state here because we need to resolve touch adjustment
2589 // first so that we apply hover/active it to the final adjusted node.
2590 IntPoint hitTestPoint = m_frame->view()->windowToContents(gestureEvent.position());
2592 if (shouldApplyTouchAdjustment(gestureEvent)) {
2593 padding = gestureEvent.area();
2594 padding.scale(1.f / 2);
2596 HitTestResult hitTestResult = hitTestResultAtPoint(hitTestPoint, hitType | HitTestRequest::ReadOnly, padding);
2598 // Adjust the location of the gesture to the most likely nearby node, as appropriate for the
2600 PlatformGestureEvent adjustedEvent = gestureEvent;
2601 applyTouchAdjustment(&adjustedEvent, &hitTestResult);
2603 // Do a new hit-test at the (adjusted) gesture co-ordinates. This is necessary because
2604 // rect-based hit testing and touch adjustment sometimes return a different node than
2605 // what a point-based hit test would return for the same point.
2606 // FIXME: Fix touch adjustment to avoid the need for a redundant hit test. http://crbug.com/398914
2607 if (shouldApplyTouchAdjustment(gestureEvent)) {
2608 LocalFrame* hitFrame = hitTestResult.innerNodeFrame();
2611 hitTestResult = hitTestResultInFrame(hitFrame, hitFrame->view()->windowToContents(adjustedEvent.position()), hitType | HitTestRequest::ReadOnly);
2614 // If we did a rect-based hit test it must be resolved to the best single node by now to
2615 // ensure consumers don't accidentally use one of the other candidates.
2616 ASSERT(!hitTestResult.isRectBasedTest());
2618 // Now apply hover/active state to the final target.
2619 // FIXME: This is supposed to send mouseenter/mouseleave events, but doesn't because we
2620 // aren't passing a PlatformMouseEvent.
2621 HitTestRequest request(hitType | HitTestRequest::AllowChildFrameContent);
2622 if (!request.readOnly())
2623 m_frame->document()->updateHoverActiveState(request, hitTestResult.innerElement());
2625 if (shouldKeepActiveForMinInterval) {
2626 m_lastDeferredTapElement = hitTestResult.innerElement();
2627 m_activeIntervalTimer.startOneShot(minimumActiveInterval - activeInterval, FROM_HERE);
2630 return GestureEventWithHitTestResults(adjustedEvent, hitTestResult);
2633 HitTestRequest::HitTestRequestType EventHandler::getHitTypeForGestureType(PlatformEvent::Type type)
2635 HitTestRequest::HitTestRequestType hitType = HitTestRequest::TouchEvent;
2637 case PlatformEvent::GestureShowPress:
2638 case PlatformEvent::GestureTapUnconfirmed:
2639 return hitType | HitTestRequest::Active;
2640 case PlatformEvent::GestureTapDownCancel:
2641 // A TapDownCancel received when no element is active shouldn't really be changing hover state.
2642 if (!m_frame->document()->activeHoverElement())
2643 hitType |= HitTestRequest::ReadOnly;
2644 return hitType | HitTestRequest::Release;
2645 case PlatformEvent::GestureTap:
2646 return hitType | HitTestRequest::Release;
2647 case PlatformEvent::GestureTapDown:
2648 case PlatformEvent::GestureLongPress:
2649 case PlatformEvent::GestureLongTap:
2650 case PlatformEvent::GestureTwoFingerTap:
2651 // FIXME: Shouldn't LongTap and TwoFingerTap clear the Active state?
2652 return hitType | HitTestRequest::Active | HitTestRequest::ReadOnly;
2654 ASSERT_NOT_REACHED();
2655 return hitType | HitTestRequest::Active | HitTestRequest::ReadOnly;
2659 void EventHandler::applyTouchAdjustment(PlatformGestureEvent* gestureEvent, HitTestResult* hitTestResult)
2661 if (!shouldApplyTouchAdjustment(*gestureEvent))
2664 Node* adjustedNode = 0;
2665 IntPoint adjustedPoint = gestureEvent->position();
2666 IntSize radius = gestureEvent->area();
2667 radius.scale(1.f / 2);
2668 bool adjusted = false;
2669 switch (gestureEvent->type()) {
2670 case PlatformEvent::GestureTap:
2671 case PlatformEvent::GestureTapUnconfirmed:
2672 case PlatformEvent::GestureTapDown:
2673 case PlatformEvent::GestureShowPress:
2674 adjusted = bestClickableNodeForHitTestResult(*hitTestResult, adjustedPoint, adjustedNode);
2676 case PlatformEvent::GestureLongPress:
2677 case PlatformEvent::GestureLongTap:
2678 case PlatformEvent::GestureTwoFingerTap:
2679 adjusted = bestContextMenuNodeForHitTestResult(*hitTestResult, adjustedPoint, adjustedNode);
2682 ASSERT_NOT_REACHED();
2685 // Update the hit-test result to be a point-based result instead of a rect-based result.
2686 // FIXME: We should do this even when no candidate matches the node filter. crbug.com/398914
2688 hitTestResult->resolveRectBasedTest(adjustedNode, m_frame->view()->windowToContents(adjustedPoint));
2689 gestureEvent->applyTouchAdjustment(adjustedPoint);
2693 bool EventHandler::sendContextMenuEvent(const PlatformMouseEvent& event)
2695 Document* doc = m_frame->document();
2696 FrameView* v = m_frame->view();
2700 // Clear mouse press state to avoid initiating a drag while context menu is up.
2701 m_mousePressed = false;
2702 LayoutPoint viewportPos = v->windowToContents(event.position());
2703 HitTestRequest request(HitTestRequest::Active);
2704 MouseEventWithHitTestResults mev = doc->prepareMouseEvent(request, viewportPos, event);
2706 if (!m_frame->selection().contains(viewportPos)
2708 // FIXME: In the editable case, word selection sometimes selects content that isn't underneath the mouse.
2709 // If the selection is non-editable, we do word selection to make it easier to use the contextual menu items
2710 // available for text selections. But only if we're above text.
2711 && (m_frame->selection().isContentEditable() || (mev.innerNode() && mev.innerNode()->isTextNode()))) {
2712 m_mouseDownMayStartSelect = true; // context menu events are always allowed to perform a selection
2714 if (mev.hitTestResult().isMisspelled())
2715 selectClosestMisspellingFromMouseEvent(mev);
2716 else if (m_frame->editor().behavior().shouldSelectOnContextualMenuClick())
2717 selectClosestWordOrLinkFromMouseEvent(mev);
2720 return !dispatchMouseEvent(EventTypeNames::contextmenu, mev.innerNode(), 0, event, false);
2723 bool EventHandler::sendContextMenuEventForKey()
2725 FrameView* view = m_frame->view();
2729 Document* doc = m_frame->document();
2733 // Clear mouse press state to avoid initiating a drag while context menu is up.
2734 m_mousePressed = false;
2736 static const int kContextMenuMargin = 1;
2739 int rightAligned = ::GetSystemMetrics(SM_MENUDROPALIGNMENT);
2741 int rightAligned = 0;
2745 Element* focusedElement = doc->focusedElement();
2746 FrameSelection& selection = m_frame->selection();
2747 Position start = selection.selection().start();
2748 bool shouldTranslateToRootView = true;
2750 if (start.deprecatedNode() && (selection.rootEditableElement() || selection.isRange())) {
2751 RefPtrWillBeRawPtr<Range> selectionRange = selection.toNormalizedRange();
2752 IntRect firstRect = m_frame->editor().firstRectForRange(selectionRange.get());
2754 int x = rightAligned ? firstRect.maxX() : firstRect.x();
2755 // In a multiline edit, firstRect.maxY() would endup on the next line, so -1.
2756 int y = firstRect.maxY() ? firstRect.maxY() - 1 : 0;
2757 location = IntPoint(x, y);
2758 } else if (focusedElement) {
2759 IntRect clippedRect = focusedElement->boundsInRootViewSpace();
2760 location = IntPoint(clippedRect.center());
2762 location = IntPoint(
2763 rightAligned ? view->contentsWidth() - kContextMenuMargin : kContextMenuMargin,
2764 kContextMenuMargin);
2765 shouldTranslateToRootView = false;
2768 m_frame->view()->setCursor(pointerCursor());
2770 IntPoint position = shouldTranslateToRootView ? view->contentsToRootView(location) : location;
2771 IntPoint globalPosition = view->hostWindow()->rootViewToScreen(IntRect(position, IntSize())).location();
2773 Node* targetNode = doc->focusedElement();
2777 // Use the focused node as the target for hover and active.
2778 HitTestResult result(position);
2779 result.setInnerNode(targetNode);
2780 doc->updateHoverActiveState(HitTestRequest::Active, result.innerElement());
2782 // The contextmenu event is a mouse event even when invoked using the keyboard.
2783 // This is required for web compatibility.
2786 PlatformEvent::Type eventType = PlatformEvent::MouseReleased;
2788 PlatformEvent::Type eventType = PlatformEvent::MousePressed;
2791 PlatformMouseEvent mouseEvent(position, globalPosition, RightButton, eventType, 1, false, false, false, false, PlatformMouseEvent::RealOrIndistinguishable, WTF::currentTime());
2793 handleMousePressEvent(mouseEvent);
2794 return sendContextMenuEvent(mouseEvent);
2797 bool EventHandler::sendContextMenuEventForGesture(const GestureEventWithHitTestResults& targetedEvent)
2800 PlatformEvent::Type eventType = PlatformEvent::MouseReleased;
2802 PlatformEvent::Type eventType = PlatformEvent::MousePressed;
2805 PlatformMouseEvent mouseEvent(targetedEvent.event().position(), targetedEvent.event().globalPosition(), RightButton, eventType, 1, false, false, false, false, PlatformMouseEvent::FromTouch, WTF::currentTime());
2806 // To simulate right-click behavior, we send a right mouse down and then
2807 // context menu event.
2808 // FIXME: Send HitTestResults to avoid redundant hit tests.
2809 handleMousePressEvent(mouseEvent);
2810 return sendContextMenuEvent(mouseEvent);
2811 // We do not need to send a corresponding mouse release because in case of
2812 // right-click, the context menu takes capture and consumes all events.
2815 void EventHandler::scheduleHoverStateUpdate()
2817 if (!m_hoverTimer.isActive())
2818 m_hoverTimer.startOneShot(0, FROM_HERE);
2821 void EventHandler::scheduleCursorUpdate()
2823 if (!m_cursorUpdateTimer.isActive())
2824 m_cursorUpdateTimer.startOneShot(cursorUpdateInterval, FROM_HERE);
2827 void EventHandler::dispatchFakeMouseMoveEventSoon()
2832 if (m_mousePositionIsUnknown)
2835 Settings* settings = m_frame->settings();
2836 if (settings && !settings->deviceSupportsMouse())
2839 // If the content has ever taken longer than fakeMouseMoveShortInterval we
2840 // reschedule the timer and use a longer time. This will cause the content
2841 // to receive these moves only after the user is done scrolling, reducing
2842 // pauses during the scroll.
2843 if (m_maxMouseMovedDuration > fakeMouseMoveShortInterval) {
2844 if (m_fakeMouseMoveEventTimer.isActive())
2845 m_fakeMouseMoveEventTimer.stop();
2846 m_fakeMouseMoveEventTimer.startOneShot(fakeMouseMoveLongInterval, FROM_HERE);
2848 if (!m_fakeMouseMoveEventTimer.isActive())
2849 m_fakeMouseMoveEventTimer.startOneShot(fakeMouseMoveShortInterval, FROM_HERE);
2853 void EventHandler::dispatchFakeMouseMoveEventSoonInQuad(const FloatQuad& quad)
2855 FrameView* view = m_frame->view();
2859 if (!quad.containsPoint(view->windowToContents(m_lastKnownMousePosition)))
2862 dispatchFakeMouseMoveEventSoon();
2865 void EventHandler::fakeMouseMoveEventTimerFired(Timer<EventHandler>* timer)
2867 ASSERT_UNUSED(timer, timer == &m_fakeMouseMoveEventTimer);
2868 ASSERT(!m_mousePressed);
2870 Settings* settings = m_frame->settings();
2871 if (settings && !settings->deviceSupportsMouse())
2874 FrameView* view = m_frame->view();
2878 if (!m_frame->page() || !m_frame->page()->focusController().isActive())
2881 // Don't dispatch a synthetic mouse move event if the mouse cursor is not visible to the user.
2882 if (!isCursorVisible())
2889 PlatformKeyboardEvent::getCurrentModifierState(shiftKey, ctrlKey, altKey, metaKey);
2890 PlatformMouseEvent fakeMouseMoveEvent(m_lastKnownMousePosition, m_lastKnownMouseGlobalPosition, NoButton, PlatformEvent::MouseMoved, 0, shiftKey, ctrlKey, altKey, metaKey, PlatformMouseEvent::RealOrIndistinguishable, currentTime());
2891 handleMouseMoveEvent(fakeMouseMoveEvent);
2894 void EventHandler::cancelFakeMouseMoveEvent()
2896 m_fakeMouseMoveEventTimer.stop();
2899 bool EventHandler::isCursorVisible() const
2901 return m_frame->page()->isCursorVisible();
2904 void EventHandler::setResizingFrameSet(HTMLFrameSetElement* frameSet)
2906 m_frameSetBeingResized = frameSet;
2909 void EventHandler::resizeScrollableAreaDestroyed()
2911 ASSERT(m_resizeScrollableArea);
2912 m_resizeScrollableArea = 0;
2915 void EventHandler::hoverTimerFired(Timer<EventHandler>*)
2917 m_hoverTimer.stop();
2920 ASSERT(m_frame->document());
2922 if (RenderView* renderer = m_frame->contentRenderer()) {
2923 if (FrameView* view = m_frame->view()) {
2924 HitTestRequest request(HitTestRequest::Move);
2925 HitTestResult result(view->windowToContents(m_lastKnownMousePosition));
2926 renderer->hitTest(request, result);
2927 m_frame->document()->updateHoverActiveState(request, result.innerElement());
2932 void EventHandler::activeIntervalTimerFired(Timer<EventHandler>*)
2934 m_activeIntervalTimer.stop();
2937 && m_frame->document()
2938 && m_lastDeferredTapElement) {
2939 // FIXME: Enable condition when http://crbug.com/226842 lands
2940 // m_lastDeferredTapElement.get() == m_frame->document()->activeElement()
2941 HitTestRequest request(HitTestRequest::TouchEvent | HitTestRequest::Release);
2942 m_frame->document()->updateHoverActiveState(request, m_lastDeferredTapElement.get());
2944 m_lastDeferredTapElement = nullptr;
2947 void EventHandler::notifyElementActivated()
2949 // Since another element has been set to active, stop current timer and clear reference.
2950 if (m_activeIntervalTimer.isActive())
2951 m_activeIntervalTimer.stop();
2952 m_lastDeferredTapElement = nullptr;
2955 void EventHandler::notifySelectionChanged()
2957 m_selectionInitiationState = ExtendedSelection;
2960 bool EventHandler::handleAccessKey(const PlatformKeyboardEvent& evt)
2962 // FIXME: Ignoring the state of Shift key is what neither IE nor Firefox do.
2963 // IE matches lower and upper case access keys regardless of Shift key state - but if both upper and
2964 // lower case variants are present in a document, the correct element is matched based on Shift key state.
2965 // Firefox only matches an access key if Shift is not pressed, and does that case-insensitively.
2966 ASSERT(!(accessKeyModifiers() & PlatformEvent::ShiftKey));
2967 if ((evt.modifiers() & ~PlatformEvent::ShiftKey) != accessKeyModifiers())
2969 String key = evt.unmodifiedText();
2970 Element* elem = m_frame->document()->getElementByAccessKey(key.lower());
2973 elem->accessKeyAction(false);
2977 bool EventHandler::isKeyEventAllowedInFullScreen(Fullscreen* fullscreen, const PlatformKeyboardEvent& keyEvent) const
2979 if (fullscreen->webkitFullScreenKeyboardInputAllowed())
2982 if (keyEvent.type() == PlatformKeyboardEvent::Char) {
2983 if (keyEvent.text().length() != 1)
2985 UChar character = keyEvent.text()[0];
2986 return character == ' ';
2989 int keyCode = keyEvent.windowsVirtualKeyCode();
2990 return (keyCode >= VK_BACK && keyCode <= VK_CAPITAL)
2991 || (keyCode >= VK_SPACE && keyCode <= VK_DELETE)
2992 || (keyCode >= VK_OEM_1 && keyCode <= VK_OEM_PLUS)
2993 || (keyCode >= VK_MULTIPLY && keyCode <= VK_OEM_8);
2996 bool EventHandler::keyEvent(const PlatformKeyboardEvent& initialKeyEvent)
2998 RefPtr<FrameView> protector(m_frame->view());
3000 ASSERT(m_frame->document());
3001 if (Fullscreen* fullscreen = Fullscreen::fromIfExists(*m_frame->document())) {
3002 if (fullscreen->webkitCurrentFullScreenElement() && !isKeyEventAllowedInFullScreen(fullscreen, initialKeyEvent)) {
3003 UseCounter::count(*m_frame->document(), UseCounter::KeyEventNotAllowedInFullScreen);
3008 if (initialKeyEvent.windowsVirtualKeyCode() == VK_CAPITAL)
3009 capsLockStateMayHaveChanged();
3012 if (panScrollInProgress()) {
3013 // If a key is pressed while the panScroll is in progress then we want to stop
3014 if (initialKeyEvent.type() == PlatformEvent::KeyDown || initialKeyEvent.type() == PlatformEvent::RawKeyDown)
3017 // If we were in panscroll mode, we swallow the key event
3022 // Check for cases where we are too early for events -- possible unmatched key up
3023 // from pressing return in the location bar.
3024 RefPtrWillBeRawPtr<Node> node = eventTargetNodeForDocument(m_frame->document());
3028 UserGestureIndicator gestureIndicator(DefinitelyProcessingUserGesture);
3030 // In IE, access keys are special, they are handled after default keydown processing, but cannot be canceled - this is hard to match.
3031 // On Mac OS X, we process them before dispatching keydown, as the default keydown handler implements Emacs key bindings, which may conflict
3032 // with access keys. Then we dispatch keydown, but suppress its default handling.
3033 // On Windows, WebKit explicitly calls handleAccessKey() instead of dispatching a keypress event for WM_SYSCHAR messages.
3034 // Other platforms currently match either Mac or Windows behavior, depending on whether they send combined KeyDown events.
3035 bool matchedAnAccessKey = false;
3036 if (initialKeyEvent.type() == PlatformEvent::KeyDown)
3037 matchedAnAccessKey = handleAccessKey(initialKeyEvent);
3039 // FIXME: it would be fair to let an input method handle KeyUp events before DOM dispatch.
3040 if (initialKeyEvent.type() == PlatformEvent::KeyUp || initialKeyEvent.type() == PlatformEvent::Char)
3041 return !node->dispatchKeyEvent(initialKeyEvent);
3043 PlatformKeyboardEvent keyDownEvent = initialKeyEvent;
3044 if (keyDownEvent.type() != PlatformEvent::RawKeyDown)
3045 keyDownEvent.disambiguateKeyDownEvent(PlatformEvent::RawKeyDown);
3046 RefPtrWillBeRawPtr<KeyboardEvent> keydown = KeyboardEvent::create(keyDownEvent, m_frame->document()->domWindow());
3047 if (matchedAnAccessKey)
3048 keydown->setDefaultPrevented(true);
3049 keydown->setTarget(node);
3051 if (initialKeyEvent.type() == PlatformEvent::RawKeyDown) {
3052 node->dispatchEvent(keydown, IGNORE_EXCEPTION);
3053 // If frame changed as a result of keydown dispatch, then return true to avoid sending a subsequent keypress message to the new frame.
3054 bool changedFocusedFrame = m_frame->page() && m_frame != m_frame->page()->focusController().focusedOrMainFrame();
3055 return keydown->defaultHandled() || keydown->defaultPrevented() || changedFocusedFrame;
3058 node->dispatchEvent(keydown, IGNORE_EXCEPTION);
3059 // If frame changed as a result of keydown dispatch, then return early to avoid sending a subsequent keypress message to the new frame.
3060 bool changedFocusedFrame = m_frame->page() && m_frame != m_frame->page()->focusController().focusedOrMainFrame();
3061 bool keydownResult = keydown->defaultHandled() || keydown->defaultPrevented() || changedFocusedFrame;
3063 return keydownResult;
3065 // Focus may have changed during keydown handling, so refetch node.
3066 // But if we are dispatching a fake backward compatibility keypress, then we pretend that the keypress happened on the original node.
3067 node = eventTargetNodeForDocument(m_frame->document());
3071 PlatformKeyboardEvent keyPressEvent = initialKeyEvent;
3072 keyPressEvent.disambiguateKeyDownEvent(PlatformEvent::Char);
3073 if (keyPressEvent.text().isEmpty())
3074 return keydownResult;
3075 RefPtrWillBeRawPtr<KeyboardEvent> keypress = KeyboardEvent::create(keyPressEvent, m_frame->document()->domWindow());
3076 keypress->setTarget(node);
3078 keypress->setDefaultPrevented(true);
3079 node->dispatchEvent(keypress, IGNORE_EXCEPTION);
3081 return keydownResult || keypress->defaultPrevented() || keypress->defaultHandled();
3084 static FocusType focusDirectionForKey(const AtomicString& keyIdentifier)
3086 DEFINE_STATIC_LOCAL(AtomicString, Down, ("Down", AtomicString::ConstructFromLiteral));
3087 DEFINE_STATIC_LOCAL(AtomicString, Up, ("Up", AtomicString::ConstructFromLiteral));
3088 DEFINE_STATIC_LOCAL(AtomicString, Left, ("Left", AtomicString::ConstructFromLiteral));
3089 DEFINE_STATIC_LOCAL(AtomicString, Right, ("Right", AtomicString::ConstructFromLiteral));
3091 FocusType retVal = FocusTypeNone;
3093 if (keyIdentifier == Down)
3094 retVal = FocusTypeDown;
3095 else if (keyIdentifier == Up)
3096 retVal = FocusTypeUp;
3097 else if (keyIdentifier == Left)
3098 retVal = FocusTypeLeft;
3099 else if (keyIdentifier == Right)
3100 retVal = FocusTypeRight;
3105 void EventHandler::defaultKeyboardEventHandler(KeyboardEvent* event)
3107 if (event->type() == EventTypeNames::keydown) {
3108 // Clear caret blinking suspended state to make sure that caret blinks
3109 // when we type again after long pressing on an empty input field.
3110 if (m_frame && m_frame->selection().isCaretBlinkingSuspended())
3111 m_frame->selection().setCaretBlinkingSuspended(false);
3113 m_frame->editor().handleKeyboardEvent(event);
3114 if (event->defaultHandled())
3116 if (event->keyIdentifier() == "U+0009")
3117 defaultTabEventHandler(event);
3118 else if (event->keyIdentifier() == "U+0008")
3119 defaultBackspaceEventHandler(event);
3120 else if (event->keyIdentifier() == "U+001B")
3121 defaultEscapeEventHandler(event);
3123 FocusType type = focusDirectionForKey(AtomicString(event->keyIdentifier()));
3124 if (type != FocusTypeNone)
3125 defaultArrowEventHandler(type, event);
3128 if (event->type() == EventTypeNames::keypress) {
3129 m_frame->editor().handleKeyboardEvent(event);
3130 if (event->defaultHandled())
3132 if (event->charCode() == ' ')
3133 defaultSpaceEventHandler(event);
3137 bool EventHandler::dragHysteresisExceeded(const FloatPoint& floatDragViewportLocation) const
3139 return dragHysteresisExceeded(flooredIntPoint(floatDragViewportLocation));
3142 bool EventHandler::dragHysteresisExceeded(const IntPoint& dragViewportLocation) const
3144 FrameView* view = m_frame->view();
3147 IntPoint dragLocation = view->windowToContents(dragViewportLocation);
3148 IntSize delta = dragLocation - m_mouseDownPos;
3150 int threshold = GeneralDragHysteresis;
3151 switch (dragState().m_dragType) {
3152 case DragSourceActionSelection:
3153 threshold = TextDragHysteresis;
3155 case DragSourceActionImage:
3156 threshold = ImageDragHysteresis;
3158 case DragSourceActionLink:
3159 threshold = LinkDragHysteresis;
3161 case DragSourceActionDHTML:
3163 case DragSourceActionNone:
3164 ASSERT_NOT_REACHED();
3167 return abs(delta.width()) >= threshold || abs(delta.height()) >= threshold;
3170 void EventHandler::clearDragDataTransfer()
3172 if (dragState().m_dragDataTransfer) {
3173 dragState().m_dragDataTransfer->clearDragImage();
3174 dragState().m_dragDataTransfer->setAccessPolicy(DataTransferNumb);
3178 void EventHandler::dragSourceEndedAt(const PlatformMouseEvent& event, DragOperation operation)
3180 // Send a hit test request so that RenderLayer gets a chance to update the :hover and :active pseudoclasses.
3181 HitTestRequest request(HitTestRequest::Release);
3182 prepareMouseEvent(request, event);
3184 if (dragState().m_dragSrc) {
3185 dragState().m_dragDataTransfer->setDestinationOperation(operation);
3186 // for now we don't care if event handler cancels default behavior, since there is none
3187 dispatchDragSrcEvent(EventTypeNames::dragend, event);
3189 clearDragDataTransfer();
3190 dragState().m_dragSrc = nullptr;
3191 // In case the drag was ended due to an escape key press we need to ensure
3192 // that consecutive mousemove events don't reinitiate the drag and drop.
3193 m_mouseDownMayStartDrag = false;
3196 void EventHandler::updateDragStateAfterEditDragIfNeeded(Element* rootEditableElement)
3198 // If inserting the dragged contents removed the drag source, we still want to fire dragend at the root editble element.
3199 if (dragState().m_dragSrc && !dragState().m_dragSrc->inDocument())
3200 dragState().m_dragSrc = rootEditableElement;
3203 // returns if we should continue "default processing", i.e., whether eventhandler canceled
3204 bool EventHandler::dispatchDragSrcEvent(const AtomicString& eventType, const PlatformMouseEvent& event)
3206 return !dispatchDragEvent(eventType, dragState().m_dragSrc.get(), event, dragState().m_dragDataTransfer.get());
3209 bool EventHandler::handleDrag(const MouseEventWithHitTestResults& event, CheckDragHysteresis checkDragHysteresis)
3211 ASSERT(event.event().type() == PlatformEvent::MouseMoved);
3212 // Callers must protect the reference to FrameView, since this function may dispatch DOM
3213 // events, causing page/FrameView to go away.
3215 ASSERT(m_frame->view());
3216 if (!m_frame->page())
3219 if (event.event().button() != LeftButton || event.event().type() != PlatformEvent::MouseMoved) {
3220 // If we allowed the other side of the bridge to handle a drag
3221 // last time, then m_mousePressed might still be set. So we
3222 // clear it now to make sure the next move after a drag
3223 // doesn't look like a drag.
3224 m_mousePressed = false;
3228 if (m_mouseDownMayStartDrag) {
3229 HitTestRequest request(HitTestRequest::ReadOnly);
3230 HitTestResult result(m_mouseDownPos);
3231 m_frame->contentRenderer()->hitTest(request, result);
3232 Node* node = result.innerNode();
3234 DragController::SelectionDragPolicy selectionDragPolicy = event.event().timestamp() - m_mouseDownTimestamp < TextDragDelay
3235 ? DragController::DelayedSelectionDragResolution
3236 : DragController::ImmediateSelectionDragResolution;
3237 dragState().m_dragSrc = m_frame->page()->dragController().draggableNode(m_frame, node, m_mouseDownPos, selectionDragPolicy, dragState().m_dragType);
3239 dragState().m_dragSrc = nullptr;
3242 if (!dragState().m_dragSrc)
3243 m_mouseDownMayStartDrag = false; // no element is draggable
3246 if (!m_mouseDownMayStartDrag)
3247 return !mouseDownMayStartSelect() && !m_mouseDownMayStartAutoscroll;
3249 // We are starting a text/image/url drag, so the cursor should be an arrow
3250 // FIXME <rdar://7577595>: Custom cursors aren't supported during drag and drop (default to pointer).
3251 m_frame->view()->setCursor(pointerCursor());
3253 if (checkDragHysteresis == ShouldCheckDragHysteresis && !dragHysteresisExceeded(event.event().position()))
3256 // Once we're past the hysteresis point, we don't want to treat this gesture as a click
3259 if (!tryStartDrag(event)) {
3260 // Something failed to start the drag, clean up.
3261 clearDragDataTransfer();
3262 dragState().m_dragSrc = nullptr;
3265 m_mouseDownMayStartDrag = false;
3266 // Whether or not the drag actually started, no more default handling (like selection).
3270 bool EventHandler::tryStartDrag(const MouseEventWithHitTestResults& event)
3272 // The DataTransfer would only be non-empty if we missed a dragEnd.
3273 // Clear it anyway, just to make sure it gets numbified.
3274 clearDragDataTransfer();
3276 dragState().m_dragDataTransfer = createDraggingDataTransfer();
3278 // Check to see if this a DOM based drag, if it is get the DOM specified drag
3280 if (dragState().m_dragType == DragSourceActionDHTML) {
3281 if (RenderObject* renderer = dragState().m_dragSrc->renderer()) {
3282 FloatPoint absPos = renderer->localToAbsolute(FloatPoint(), UseTransforms);
3283 IntSize delta = m_mouseDownPos - roundedIntPoint(absPos);
3284 dragState().m_dragDataTransfer->setDragImageElement(dragState().m_dragSrc.get(), IntPoint(delta));
3286 // The renderer has disappeared, this can happen if the onStartDrag handler has hidden
3287 // the element in some way. In this case we just kill the drag.
3292 DragController& dragController = m_frame->page()->dragController();
3293 if (!dragController.populateDragDataTransfer(m_frame, dragState(), m_mouseDownPos))
3296 // If dispatching dragstart brings about another mouse down -- one way
3297 // this will happen is if a DevTools user breaks within a dragstart
3298 // handler and then clicks on the suspended page -- the drag state is
3299 // reset. Hence, need to check if this particular drag operation can
3300 // continue even if dispatchEvent() indicates no (direct) cancellation.
3301 // Do that by checking if m_dragSrc is still set.
3302 m_mouseDownMayStartDrag = dispatchDragSrcEvent(EventTypeNames::dragstart, m_mouseDown)
3303 && !m_frame->selection().isInPasswordField() && dragState().m_dragSrc;
3305 // Invalidate clipboard here against anymore pasteboard writing for security. The drag
3306 // image can still be changed as we drag, but not the pasteboard data.
3307 dragState().m_dragDataTransfer->setAccessPolicy(DataTransferImageWritable);
3309 if (m_mouseDownMayStartDrag) {
3310 // Dispatching the event could cause Page to go away. Make sure it's still valid before trying to use DragController.
3311 m_didStartDrag = m_frame->page() && dragController.startDrag(m_frame, dragState(), event.event(), m_mouseDownPos);
3312 // FIXME: This seems pretty useless now. The gesture code uses this as a signal for
3313 // whether or not the drag started, but perhaps it can simply use the return value from
3314 // handleDrag(), even though it doesn't mean exactly the same thing.
3317 // Drag was canned at the last minute - we owe m_dragSrc a DRAGEND event
3318 dispatchDragSrcEvent(EventTypeNames::dragend, event.event());
3324 bool EventHandler::handleTextInputEvent(const String& text, Event* underlyingEvent, TextEventInputType inputType)
3326 // Platforms should differentiate real commands like selectAll from text input in disguise (like insertNewline),
3327 // and avoid dispatching text input events from keydown default handlers.
3328 ASSERT(!underlyingEvent || !underlyingEvent->isKeyboardEvent() || toKeyboardEvent(underlyingEvent)->type() == EventTypeNames::keypress);
3333 EventTarget* target;
3334 if (underlyingEvent)
3335 target = underlyingEvent->target();
3337 target = eventTargetNodeForDocument(m_frame->document());
3341 RefPtrWillBeRawPtr<TextEvent> event = TextEvent::create(m_frame->domWindow(), text, inputType);
3342 event->setUnderlyingEvent(underlyingEvent);
3344 target->dispatchEvent(event, IGNORE_EXCEPTION);
3345 return event->defaultHandled();
3348 void EventHandler::defaultTextInputEventHandler(TextEvent* event)
3350 if (m_frame->editor().handleTextEvent(event))
3351 event->setDefaultHandled();
3354 void EventHandler::defaultSpaceEventHandler(KeyboardEvent* event)
3356 ASSERT(event->type() == EventTypeNames::keypress);
3358 if (event->ctrlKey() || event->metaKey() || event->altKey())
3361 ScrollDirection direction = event->shiftKey() ? ScrollBlockDirectionBackward : ScrollBlockDirectionForward;
3362 if (scroll(direction, ScrollByPage)) {
3363 event->setDefaultHandled();
3367 FrameView* view = m_frame->view();
3371 if (view->scroll(direction, ScrollByPage))
3372 event->setDefaultHandled();
3375 void EventHandler::defaultBackspaceEventHandler(KeyboardEvent* event)
3377 ASSERT(event->type() == EventTypeNames::keydown);
3379 if (event->ctrlKey() || event->metaKey() || event->altKey())
3382 if (!m_frame->editor().behavior().shouldNavigateBackOnBackspace())
3385 Page* page = m_frame->page();
3386 if (!page || !page->mainFrame()->isLocalFrame())
3388 bool handledEvent = page->deprecatedLocalMainFrame()->loader().client()->navigateBackForward(event->shiftKey() ? 1 : -1);
3390 event->setDefaultHandled();
3393 void EventHandler::defaultArrowEventHandler(FocusType focusType, KeyboardEvent* event)
3395 ASSERT(event->type() == EventTypeNames::keydown);
3397 if (event->ctrlKey() || event->metaKey() || event->shiftKey())
3400 Page* page = m_frame->page();
3404 if (!isSpatialNavigationEnabled(m_frame))
3407 // Arrows and other possible directional navigation keys can be used in design
3409 if (m_frame->document()->inDesignMode())
3412 if (page->focusController().advanceFocus(focusType))
3413 event->setDefaultHandled();
3416 void EventHandler::defaultTabEventHandler(KeyboardEvent* event)
3418 ASSERT(event->type() == EventTypeNames::keydown);
3420 // We should only advance focus on tabs if no special modifier keys are held down.
3421 if (event->ctrlKey() || event->metaKey())
3424 Page* page = m_frame->page();
3427 if (!page->tabKeyCyclesThroughElements())
3430 FocusType focusType = event->shiftKey() ? FocusTypeBackward : FocusTypeForward;
3432 // Tabs can be used in design mode editing.
3433 if (m_frame->document()->inDesignMode())
3436 if (page->focusController().advanceFocus(focusType))
3437 event->setDefaultHandled();
3440 void EventHandler::defaultEscapeEventHandler(KeyboardEvent* event)
3442 if (HTMLDialogElement* dialog = m_frame->document()->activeModalDialog())
3443 dialog->dispatchEvent(Event::createCancelable(EventTypeNames::cancel));
3446 void EventHandler::capsLockStateMayHaveChanged()
3448 if (Element* element = m_frame->document()->focusedElement()) {
3449 if (RenderObject* r = element->renderer()) {
3450 if (r->isTextField())
3451 toRenderTextControlSingleLine(r)->capsLockStateMayHaveChanged();
3456 void EventHandler::setFrameWasScrolledByUser()
3458 if (FrameView* view = m_frame->view())
3459 view->setWasScrolledByUser(true);
3462 bool EventHandler::passMousePressEventToScrollbar(MouseEventWithHitTestResults& mev)
3464 Scrollbar* scrollbar = mev.scrollbar();
3465 updateLastScrollbarUnderMouse(scrollbar, true);
3467 if (!scrollbar || !scrollbar->enabled())
3469 setFrameWasScrolledByUser();
3470 scrollbar->mouseDown(mev.event());
3474 // If scrollbar (under mouse) is different from last, send a mouse exited. Set
3475 // last to scrollbar if setLast is true; else set last to 0.
3476 void EventHandler::updateLastScrollbarUnderMouse(Scrollbar* scrollbar, bool setLast)
3478 if (m_lastScrollbarUnderMouse != scrollbar) {
3479 // Send mouse exited to the old scrollbar.
3480 if (m_lastScrollbarUnderMouse)
3481 m_lastScrollbarUnderMouse->mouseExited();
3483 // Send mouse entered if we're setting a new scrollbar.
3484 if (scrollbar && setLast)
3485 scrollbar->mouseEntered();
3487 m_lastScrollbarUnderMouse = setLast ? scrollbar : 0;
3491 static const AtomicString& eventNameForTouchPointState(PlatformTouchPoint::State state)
3494 case PlatformTouchPoint::TouchReleased:
3495 return EventTypeNames::touchend;
3496 case PlatformTouchPoint::TouchCancelled:
3497 return EventTypeNames::touchcancel;
3498 case PlatformTouchPoint::TouchPressed:
3499 return EventTypeNames::touchstart;
3500 case PlatformTouchPoint::TouchMoved:
3501 return EventTypeNames::touchmove;
3502 case PlatformTouchPoint::TouchStationary:
3503 // TouchStationary state is not converted to touch events, so fall through to assert.
3505 ASSERT_NOT_REACHED();
3510 HitTestResult EventHandler::hitTestResultInFrame(LocalFrame* frame, const LayoutPoint& point, HitTestRequest::HitTestRequestType hitType)
3512 HitTestResult result(point);
3514 if (!frame || !frame->contentRenderer())
3516 if (frame->view()) {
3517 IntRect rect = frame->view()->visibleContentRect(IncludeScrollbars);
3518 if (!rect.contains(roundedIntPoint(point)))
3521 frame->contentRenderer()->hitTest(HitTestRequest(hitType), result);
3525 bool EventHandler::handleTouchEvent(const PlatformTouchEvent& event)
3527 TRACE_EVENT0("blink", "EventHandler::handleTouchEvent");
3529 const Vector<PlatformTouchPoint>& points = event.touchPoints();
3532 bool freshTouchEvents = true;
3533 bool allTouchReleased = true;
3534 for (i = 0; i < points.size(); ++i) {
3535 const PlatformTouchPoint& point = points[i];
3536 if (point.state() != PlatformTouchPoint::TouchPressed)
3537 freshTouchEvents = false;
3538 if (point.state() != PlatformTouchPoint::TouchReleased && point.state() != PlatformTouchPoint::TouchCancelled)
3539 allTouchReleased = false;
3541 if (freshTouchEvents) {
3542 // Ideally we'd ASSERT !m_touchSequenceDocument here since we should
3543 // have cleared the active document when we saw the last release. But we
3544 // have some tests that violate this, ClusterFuzz could trigger it, and
3545 // there may be cases where the browser doesn't reliably release all
3546 // touches. http://crbug.com/345372 tracks this.
3547 m_touchSequenceDocument.clear();
3548 m_touchSequenceUserGestureToken.clear();
3551 OwnPtr<UserGestureIndicator> gestureIndicator;
3553 if (m_touchSequenceUserGestureToken)
3554 gestureIndicator = adoptPtr(new UserGestureIndicator(m_touchSequenceUserGestureToken.release()));
3556 gestureIndicator = adoptPtr(new UserGestureIndicator(DefinitelyProcessingUserGesture));
3558 m_touchSequenceUserGestureToken = gestureIndicator->currentToken();
3560 ASSERT(m_frame->view());
3561 if (m_touchSequenceDocument && (!m_touchSequenceDocument->frame() || !m_touchSequenceDocument->frame()->view())) {
3562 // If the active touch document has no frame or view, it's probably being destroyed
3563 // so we can't dispatch events.
3567 // First do hit tests for any new touch points.
3568 for (i = 0; i < points.size(); ++i) {
3569 const PlatformTouchPoint& point = points[i];
3571 // Touch events implicitly capture to the touched node, and don't change
3572 // active/hover states themselves (Gesture events do). So we only need
3573 // to hit-test on touchstart, and it can be read-only.
3574 if (point.state() == PlatformTouchPoint::TouchPressed) {
3575 HitTestRequest::HitTestRequestType hitType = HitTestRequest::TouchEvent | HitTestRequest::ReadOnly | HitTestRequest::Active;
3576 LayoutPoint pagePoint = roundedLayoutPoint(m_frame->view()->windowToContents(point.pos()));
3577 HitTestResult result;
3578 if (!m_touchSequenceDocument) {
3579 result = hitTestResultAtPoint(pagePoint, hitType);
3580 } else if (m_touchSequenceDocument->frame()) {
3581 LayoutPoint framePoint = roundedLayoutPoint(m_touchSequenceDocument->frame()->view()->windowToContents(point.pos()));
3582 result = hitTestResultInFrame(m_touchSequenceDocument->frame(), framePoint, hitType);
3586 Node* node = result.innerNode();
3590 // Touch events should not go to text nodes
3591 if (node->isTextNode())
3592 node = NodeRenderingTraversal::parent(node);
3594 if (!m_touchSequenceDocument) {
3595 // Keep track of which document should receive all touch events
3596 // in the active sequence. This must be a single document to
3597 // ensure we don't leak Nodes between documents.
3598 m_touchSequenceDocument = &(result.innerNode()->document());
3599 ASSERT(m_touchSequenceDocument->frame()->view());
3602 // Ideally we'd ASSERT(!m_targetForTouchID.contains(point.id())
3603 // since we shouldn't get a touchstart for a touch that's already
3604 // down. However EventSender allows this to be violated and there's
3605 // some tests that take advantage of it. There may also be edge
3606 // cases in the browser where this happens.
3607 // See http://crbug.com/345372.
3608 m_targetForTouchID.set(point.id(), node);
3610 TouchAction effectiveTouchAction = computeEffectiveTouchAction(*node);
3611 if (effectiveTouchAction != TouchActionAuto)
3612 m_frame->page()->chrome().client().setTouchAction(effectiveTouchAction);
3616 m_touchPressed = !allTouchReleased;
3618 // If there's no document receiving touch events, or no handlers on the
3619 // document set to receive the events, then we can skip all the rest of
3621 if (!m_touchSequenceDocument || !m_touchSequenceDocument->frameHost() || !m_touchSequenceDocument->frameHost()->eventHandlerRegistry().hasEventHandlers(EventHandlerRegistry::TouchEvent) || !m_touchSequenceDocument->frame()) {
3622 if (allTouchReleased) {
3623 m_touchSequenceDocument.clear();
3624 m_touchSequenceUserGestureToken.clear();
3629 // Build up the lists to use for the 'touches', 'targetTouches' and
3630 // 'changedTouches' attributes in the JS event. See
3631 // http://www.w3.org/TR/touch-events/#touchevent-interface for how these
3632 // lists fit together.
3634 // Holds the complete set of touches on the screen.
3635 RefPtrWillBeRawPtr<TouchList> touches = TouchList::create();
3637 // A different view on the 'touches' list above, filtered and grouped by
3638 // event target. Used for the 'targetTouches' list in the JS event.
3639 typedef WillBeHeapHashMap<EventTarget*, RefPtrWillBeMember<TouchList> > TargetTouchesHeapMap;
3640 TargetTouchesHeapMap touchesByTarget;
3642 // Array of touches per state, used to assemble the 'changedTouches' list.
3643 typedef WillBeHeapHashSet<RefPtrWillBeMember<EventTarget> > EventTargetSet;
3645 // The touches corresponding to the particular change state this struct
3646 // instance represents.
3647 RefPtrWillBeMember<TouchList> m_touches;
3648 // Set of targets involved in m_touches.
3649 EventTargetSet m_targets;
3650 } changedTouches[PlatformTouchPoint::TouchStateEnd];
3652 for (i = 0; i < points.size(); ++i) {
3653 const PlatformTouchPoint& point = points[i];
3654 PlatformTouchPoint::State pointState = point.state();
3655 RefPtrWillBeRawPtr<EventTarget> touchTarget = nullptr;
3657 if (pointState == PlatformTouchPoint::TouchReleased || pointState == PlatformTouchPoint::TouchCancelled) {
3658 // The target should be the original target for this touch, so get
3659 // it from the hashmap. As it's a release or cancel we also remove
3661 touchTarget = m_targetForTouchID.take(point.id());
3663 // No hittest is performed on move or stationary, since the target
3664 // is not allowed to change anyway.
3665 touchTarget = m_targetForTouchID.get(point.id());
3668 LocalFrame* targetFrame = 0;
3669 bool knownTarget = false;
3671 Document& doc = touchTarget->toNode()->document();
3672 // If the target node has moved to a new document while it was being touched,
3673 // we can't send events to the new document because that could leak nodes
3674 // from one document to another. See http://crbug.com/394339.
3675 if (&doc == m_touchSequenceDocument.get()) {
3676 targetFrame = doc.frame();
3681 // If we don't have a target registered for the point it means we've
3682 // missed our opportunity to do a hit test for it (due to some
3683 // optimization that prevented blink from ever seeing the
3684 // touchstart), or that the touch started outside the active touch
3685 // sequence document. We should still include the touch in the
3686 // Touches list reported to the application (eg. so it can
3687 // differentiate between a one and two finger gesture), but we won't
3688 // actually dispatch any events for it. Set the target to the
3689 // Document so that there's some valid node here. Perhaps this
3690 // should really be LocalDOMWindow, but in all other cases the target of
3691 // a Touch is a Node so using the window could be a breaking change.
3692 // Since we know there was no handler invoked, the specific target
3693 // should be completely irrelevant to the application.
3694 touchTarget = m_touchSequenceDocument;
3695 targetFrame = m_touchSequenceDocument->frame();
3697 ASSERT(targetFrame);
3699 // pagePoint should always be relative to the target elements
3700 // containing frame.
3701 FloatPoint pagePoint = targetFrame->view()->windowToContents(point.pos());
3703 float scaleFactor = 1.0f / targetFrame->pageZoomFactor();
3705 FloatPoint adjustedPagePoint = pagePoint.scaledBy(scaleFactor);
3706 FloatSize adjustedRadius = point.radius().scaledBy(scaleFactor);
3708 RefPtrWillBeRawPtr<Touch> touch = Touch::create(
3709 targetFrame, touchTarget.get(), point.id(), point.screenPos(), adjustedPagePoint, adjustedRadius, point.rotationAngle(), point.force());
3711 // Ensure this target's touch list exists, even if it ends up empty, so
3712 // it can always be passed to TouchEvent::Create below.
3713 TargetTouchesHeapMap::iterator targetTouchesIterator = touchesByTarget.find(touchTarget.get());
3714 if (targetTouchesIterator == touchesByTarget.end()) {
3715 touchesByTarget.set(touchTarget.get(), TouchList::create());
3716 targetTouchesIterator = touchesByTarget.find(touchTarget.get());
3719 // touches and targetTouches should only contain information about
3720 // touches still on the screen, so if this point is released or
3721 // cancelled it will only appear in the changedTouches list.
3722 if (pointState != PlatformTouchPoint::TouchReleased && pointState != PlatformTouchPoint::TouchCancelled) {
3723 touches->append(touch);
3724 targetTouchesIterator->value->append(touch);
3727 // Now build up the correct list for changedTouches.
3728 // Note that any touches that are in the TouchStationary state (e.g. if
3729 // the user had several points touched but did not move them all) should
3730 // never be in the changedTouches list so we do not handle them
3731 // explicitly here. See https://bugs.webkit.org/show_bug.cgi?id=37609
3732 // for further discussion about the TouchStationary state.
3733 if (pointState != PlatformTouchPoint::TouchStationary && knownTarget) {
3734 ASSERT(pointState < PlatformTouchPoint::TouchStateEnd);
3735 if (!changedTouches[pointState].m_touches)
3736 changedTouches[pointState].m_touches = TouchList::create();
3737 changedTouches[pointState].m_touches->append(touch);
3738 changedTouches[pointState].m_targets.add(touchTarget);
3741 if (allTouchReleased) {
3742 m_touchSequenceDocument.clear();
3743 m_touchSequenceUserGestureToken.clear();
3746 // Now iterate the changedTouches list and m_targets within it, sending
3747 // events to the targets as required.
3748 bool swallowedEvent = false;
3749 for (unsigned state = 0; state != PlatformTouchPoint::TouchStateEnd; ++state) {
3750 if (!changedTouches[state].m_touches)
3753 const AtomicString& stateName(eventNameForTouchPointState(static_cast<PlatformTouchPoint::State>(state)));
3754 const EventTargetSet& targetsForState = changedTouches[state].m_targets;
3755 for (EventTargetSet::const_iterator it = targetsForState.begin(); it != targetsForState.end(); ++it) {
3756 EventTarget* touchEventTarget = it->get();
3757 RefPtrWillBeRawPtr<TouchEvent> touchEvent = TouchEvent::create(
3758 touches.get(), touchesByTarget.get(touchEventTarget), changedTouches[state].m_touches.get(),
3759 stateName, touchEventTarget->toNode()->document().domWindow(),
3760 event.ctrlKey(), event.altKey(), event.shiftKey(), event.metaKey(), event.cancelable());
3761 touchEventTarget->toNode()->dispatchTouchEvent(touchEvent.get());
3762 swallowedEvent = swallowedEvent || touchEvent->defaultPrevented() || touchEvent->defaultHandled();
3766 return swallowedEvent;
3769 TouchAction EventHandler::intersectTouchAction(TouchAction action1, TouchAction action2)
3771 if (action1 == TouchActionNone || action2 == TouchActionNone)
3772 return TouchActionNone;
3773 if (action1 == TouchActionAuto)
3775 if (action2 == TouchActionAuto)
3777 if (!(action1 & action2))
3778 return TouchActionNone;
3779 return action1 & action2;
3782 TouchAction EventHandler::computeEffectiveTouchAction(const Node& node)
3784 // Start by permitting all actions, then walk the elements supporting
3785 // touch-action from the target node up to the nearest scrollable ancestor
3786 // and exclude any prohibited actions.
3787 TouchAction effectiveTouchAction = TouchActionAuto;
3788 for (const Node* curNode = &node; curNode; curNode = NodeRenderingTraversal::parent(curNode)) {
3789 if (RenderObject* renderer = curNode->renderer()) {
3790 if (renderer->supportsTouchAction()) {
3791 TouchAction action = renderer->style()->touchAction();
3792 effectiveTouchAction = intersectTouchAction(action, effectiveTouchAction);
3793 if (effectiveTouchAction == TouchActionNone)
3797 // If we've reached an ancestor that supports a touch action, search no further.
3798 if (renderer->isBox() && toRenderBox(renderer)->scrollsOverflow())
3802 return effectiveTouchAction;
3805 void EventHandler::setLastKnownMousePosition(const PlatformMouseEvent& event)
3807 m_mousePositionIsUnknown = false;
3808 m_lastKnownMousePosition = event.position();
3809 m_lastKnownMouseGlobalPosition = event.globalPosition();
3812 bool EventHandler::passMousePressEventToSubframe(MouseEventWithHitTestResults& mev, LocalFrame* subframe)
3814 // If we're clicking into a frame that is selected, the frame will appear
3815 // greyed out even though we're clicking on the selection. This looks
3816 // really strange (having the whole frame be greyed out), so we deselect the
3818 IntPoint p = m_frame->view()->windowToContents(mev.event().position());
3819 if (m_frame->selection().contains(p)) {
3820 VisiblePosition visiblePos(
3821 mev.innerNode()->renderer()->positionForPoint(mev.localPoint()));
3822 VisibleSelection newSelection(visiblePos);
3823 m_frame->selection().setSelection(newSelection);
3826 subframe->eventHandler().handleMousePressEvent(mev.event());
3830 bool EventHandler::passMouseMoveEventToSubframe(MouseEventWithHitTestResults& mev, LocalFrame* subframe, HitTestResult* hoveredNode)
3832 if (m_mouseDownMayStartDrag && !m_mouseDownWasInSubframe)
3834 subframe->eventHandler().handleMouseMoveOrLeaveEvent(mev.event(), hoveredNode);
3838 bool EventHandler::passMouseReleaseEventToSubframe(MouseEventWithHitTestResults& mev, LocalFrame* subframe)
3840 subframe->eventHandler().handleMouseReleaseEvent(mev.event());
3844 bool EventHandler::passWheelEventToWidget(const PlatformWheelEvent& wheelEvent, Widget* widget)
3846 // We can sometimes get a null widget! EventHandlerMac handles a null
3847 // widget by returning false, so we do the same.
3851 // If not a FrameView, then probably a plugin widget. Those will receive
3852 // the event via an EventTargetNode dispatch when this returns false.
3853 if (!widget->isFrameView())
3856 return toFrameView(widget)->frame().eventHandler().handleWheelEvent(wheelEvent);
3859 bool EventHandler::passWidgetMouseDownEventToWidget(const MouseEventWithHitTestResults& event)
3861 // Figure out which view to send the event to.
3862 if (!event.innerNode() || !event.innerNode()->renderer() || !event.innerNode()->renderer()->isWidget())
3867 PassRefPtrWillBeRawPtr<DataTransfer> EventHandler::createDraggingDataTransfer() const
3869 return DataTransfer::create(DataTransfer::DragAndDrop, DataTransferWritable, DataObject::create());
3872 void EventHandler::focusDocumentView()
3874 Page* page = m_frame->page();
3877 page->focusController().focusDocumentView(m_frame);
3880 unsigned EventHandler::accessKeyModifiers()
3883 return PlatformEvent::CtrlKey | PlatformEvent::AltKey;
3885 return PlatformEvent::AltKey;
3889 } // namespace blink