Use WeakPtrs to avoid using deallocated Widgets and ScrollableAreas
[WebKit.git] / Source / WebCore / page / EventHandler.cpp
1 /*
2  * Copyright (C) 2006-2016 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)
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
15  * THIS SOFTWARE IS PROVIDED BY APPLE 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 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. 
26  */
27
28 #include "config.h"
29 #include "EventHandler.h"
30
31 #include "AXObjectCache.h"
32 #include "AutoscrollController.h"
33 #include "BackForwardController.h"
34 #include "CachedImage.h"
35 #include "Chrome.h"
36 #include "ChromeClient.h"
37 #include "Cursor.h"
38 #include "CursorList.h"
39 #include "Document.h"
40 #include "DocumentEventQueue.h"
41 #include "DragController.h"
42 #include "DragState.h"
43 #include "Editor.h"
44 #include "EditorClient.h"
45 #include "EventNames.h"
46 #include "ExceptionCodePlaceholder.h"
47 #include "FileList.h"
48 #include "FloatPoint.h"
49 #include "FloatRect.h"
50 #include "FocusController.h"
51 #include "FrameLoader.h"
52 #include "FrameSelection.h"
53 #include "FrameTree.h"
54 #include "FrameView.h"
55 #include "htmlediting.h"
56 #include "HTMLFrameElementBase.h"
57 #include "HTMLFrameSetElement.h"
58 #include "HTMLInputElement.h"
59 #include "HTMLNames.h"
60 #include "HitTestRequest.h"
61 #include "HitTestResult.h"
62 #include "Image.h"
63 #include "InspectorInstrumentation.h"
64 #include "KeyboardEvent.h"
65 #include "MainFrame.h"
66 #include "MouseEvent.h"
67 #include "MouseEventWithHitTestResults.h"
68 #include "Page.h"
69 #include "PageOverlayController.h"
70 #include "PlatformEvent.h"
71 #include "PlatformKeyboardEvent.h"
72 #include "PlatformWheelEvent.h"
73 #include "PluginDocument.h"
74 #include "RenderFrameSet.h"
75 #include "RenderLayer.h"
76 #include "RenderListBox.h"
77 #include "RenderNamedFlowThread.h"
78 #include "RenderTextControlSingleLine.h"
79 #include "RenderView.h"
80 #include "RenderWidget.h"
81 #include "RuntimeApplicationChecks.h"
82 #include "SVGDocument.h"
83 #include "SVGNames.h"
84 #include "ScrollLatchingState.h"
85 #include "Scrollbar.h"
86 #include "Settings.h"
87 #include "ShadowRoot.h"
88 #include "SpatialNavigation.h"
89 #include "StyleCachedImage.h"
90 #include "TextEvent.h"
91 #include "TextIterator.h"
92 #include "UserGestureIndicator.h"
93 #include "UserTypingGestureIndicator.h"
94 #include "VisibleUnits.h"
95 #include "WheelEvent.h"
96 #include "WindowsKeyboardCodes.h"
97 #include <wtf/Assertions.h>
98 #include <wtf/CurrentTime.h>
99 #include <wtf/NeverDestroyed.h>
100 #include <wtf/StdLibExtras.h>
101 #include <wtf/TemporaryChange.h>
102 #include <wtf/WeakPtr.h>
103
104 #if ENABLE(CSS_IMAGE_SET)
105 #include "StyleCachedImageSet.h"
106 #endif
107
108 #if ENABLE(IOS_TOUCH_EVENTS)
109 #include "PlatformTouchEventIOS.h"
110 #endif
111
112 #if ENABLE(TOUCH_EVENTS)
113 #include "TouchEvent.h"
114 #include "TouchList.h"
115 #endif
116
117 #if ENABLE(TOUCH_EVENTS) && !ENABLE(IOS_TOUCH_EVENTS)
118 #include "PlatformTouchEvent.h"
119 #endif
120
121 #if ENABLE(MAC_GESTURE_EVENTS)
122 #include "PlatformGestureEventMac.h"
123 #endif
124
125 namespace WebCore {
126
127 using namespace HTMLNames;
128
129 #if ENABLE(DRAG_SUPPORT)
130 // The link drag hysteresis is much larger than the others because there
131 // needs to be enough space to cancel the link press without starting a link drag,
132 // and because dragging links is rare.
133 const int LinkDragHysteresis = 40;
134 const int ImageDragHysteresis = 5;
135 const int TextDragHysteresis = 3;
136 const int GeneralDragHysteresis = 3;
137 #endif // ENABLE(DRAG_SUPPORT)
138
139 #if ENABLE(IOS_GESTURE_EVENTS) || ENABLE(MAC_GESTURE_EVENTS)
140 const float GestureUnknown = 0;
141 #endif
142
143 #if ENABLE(IOS_TOUCH_EVENTS)
144 // FIXME: Share this constant with EventHandler and SliderThumbElement.
145 const unsigned InvalidTouchIdentifier = 0;
146 #endif
147
148 // Match key code of composition keydown event on windows.
149 // IE sends VK_PROCESSKEY which has value 229;
150 const int CompositionEventKeyCode = 229;
151
152 using namespace SVGNames;
153
154 #if !ENABLE(IOS_TOUCH_EVENTS)
155 // The amount of time to wait before sending a fake mouse event, triggered
156 // during a scroll. The short interval is used if the content responds to the mouse events
157 // in fakeMouseMoveDurationThreshold or less, otherwise the long interval is used.
158 const double fakeMouseMoveDurationThreshold = 0.01;
159 const double fakeMouseMoveShortInterval = 0.1;
160 const double fakeMouseMoveLongInterval = 0.25;
161 #endif
162
163 #if ENABLE(CURSOR_SUPPORT)
164 // The amount of time to wait for a cursor update on style and layout changes
165 // Set to 50Hz, no need to be faster than common screen refresh rate
166 const double cursorUpdateInterval = 0.02;
167
168 const int maximumCursorSize = 128;
169 #endif
170
171 #if ENABLE(MOUSE_CURSOR_SCALE)
172 // It's pretty unlikely that a scale of less than one would ever be used. But all we really
173 // need to ensure here is that the scale isn't so small that integer overflow can occur when
174 // dividing cursor sizes (limited above) by the scale.
175 const double minimumCursorScale = 0.001;
176 #endif
177
178 enum NoCursorChangeType { NoCursorChange };
179
180 class OptionalCursor {
181 public:
182     OptionalCursor(NoCursorChangeType) : m_isCursorChange(false) { }
183     OptionalCursor(const Cursor& cursor) : m_isCursorChange(true), m_cursor(cursor) { }
184
185     bool isCursorChange() const { return m_isCursorChange; }
186     const Cursor& cursor() const { ASSERT(m_isCursorChange); return m_cursor; }
187
188 private:
189     bool m_isCursorChange;
190     Cursor m_cursor;
191 };
192
193 class MaximumDurationTracker {
194 public:
195     explicit MaximumDurationTracker(double *maxDuration)
196         : m_maxDuration(maxDuration)
197         , m_start(monotonicallyIncreasingTime())
198     {
199     }
200
201     ~MaximumDurationTracker()
202     {
203         *m_maxDuration = std::max(*m_maxDuration, monotonicallyIncreasingTime() - m_start);
204     }
205
206 private:
207     double* m_maxDuration;
208     double m_start;
209 };
210
211 #if ENABLE(TOUCH_EVENTS) && !ENABLE(IOS_TOUCH_EVENTS)
212 class SyntheticTouchPoint : public PlatformTouchPoint {
213 public:
214
215     // The default values are based on http://dvcs.w3.org/hg/webevents/raw-file/tip/touchevents.html
216     explicit SyntheticTouchPoint(const PlatformMouseEvent& event)
217     {
218         const static int idDefaultValue = 0;
219         const static int radiusYDefaultValue = 1;
220         const static int radiusXDefaultValue = 1;
221         const static float rotationAngleDefaultValue = 0.0f;
222         const static float forceDefaultValue = 1.0f;
223
224         m_id = idDefaultValue; // There is only one active TouchPoint.
225         m_screenPos = event.globalPosition();
226         m_pos = event.position();
227         m_radiusY = radiusYDefaultValue;
228         m_radiusX = radiusXDefaultValue;
229         m_rotationAngle = rotationAngleDefaultValue;
230         m_force = forceDefaultValue;
231
232         PlatformEvent::Type type = event.type();
233         ASSERT(type == PlatformEvent::MouseMoved || type == PlatformEvent::MousePressed || type == PlatformEvent::MouseReleased);
234
235         switch (type) {
236         case PlatformEvent::MouseMoved:
237             m_state = TouchMoved;
238             break;
239         case PlatformEvent::MousePressed:
240             m_state = TouchPressed;
241             break;
242         case PlatformEvent::MouseReleased:
243             m_state = TouchReleased;
244             break;
245         default:
246             ASSERT_NOT_REACHED();
247             break;
248         }
249     }
250 };
251
252 class SyntheticSingleTouchEvent : public PlatformTouchEvent {
253 public:
254     explicit SyntheticSingleTouchEvent(const PlatformMouseEvent& event)
255     {
256         switch (event.type()) {
257         case PlatformEvent::MouseMoved:
258             m_type = TouchMove;
259             break;
260         case PlatformEvent::MousePressed:
261             m_type = TouchStart;
262             break;
263         case PlatformEvent::MouseReleased:
264             m_type = TouchEnd;
265             break;
266         default:
267             ASSERT_NOT_REACHED();
268             m_type = NoType;
269             break;
270         }
271         m_timestamp = event.timestamp();
272         m_modifiers = event.modifiers();
273         m_touchPoints.append(SyntheticTouchPoint(event));
274     }
275 };
276 #endif // ENABLE(TOUCH_EVENTS) && !ENABLE(IOS_TOUCH_EVENTS)
277
278 static inline ScrollGranularity wheelGranularityToScrollGranularity(unsigned deltaMode)
279 {
280     switch (deltaMode) {
281     case WheelEvent::DOM_DELTA_PAGE:
282         return ScrollByPage;
283     case WheelEvent::DOM_DELTA_LINE:
284         return ScrollByLine;
285     case WheelEvent::DOM_DELTA_PIXEL:
286         return ScrollByPixel;
287     default:
288         return ScrollByPixel;
289     }
290 }
291
292 static inline bool didScrollInScrollableArea(ScrollableArea* scrollableArea, WheelEvent* wheelEvent)
293 {
294     ScrollGranularity scrollGranularity = wheelGranularityToScrollGranularity(wheelEvent->deltaMode());
295     bool didHandleWheelEvent = false;
296     if (float absoluteDelta = std::abs(wheelEvent->deltaX()))
297         didHandleWheelEvent |= scrollableArea->scroll(wheelEvent->deltaX() > 0 ? ScrollRight : ScrollLeft, scrollGranularity, absoluteDelta);
298     
299     if (float absoluteDelta = std::abs(wheelEvent->deltaY()))
300         didHandleWheelEvent |= scrollableArea->scroll(wheelEvent->deltaY() > 0 ? ScrollDown : ScrollUp, scrollGranularity, absoluteDelta);
301     
302     return didHandleWheelEvent;
303 }
304
305 static inline bool handleWheelEventInAppropriateEnclosingBox(Node* startNode, WheelEvent* wheelEvent, Element** stopElement, const FloatSize& filteredPlatformDelta)
306 {
307     bool shouldHandleEvent = wheelEvent->deltaX() || wheelEvent->deltaY();
308 #if PLATFORM(MAC)
309     shouldHandleEvent |= wheelEvent->phase() == PlatformWheelEventPhaseEnded;
310 #if ENABLE(CSS_SCROLL_SNAP)
311     shouldHandleEvent |= wheelEvent->momentumPhase() == PlatformWheelEventPhaseEnded;
312 #endif
313 #endif
314     if (!startNode->renderer() || !shouldHandleEvent)
315         return false;
316
317     RenderBox& initialEnclosingBox = startNode->renderer()->enclosingBox();
318     if (initialEnclosingBox.isListBox())
319         return didScrollInScrollableArea(static_cast<RenderListBox*>(&initialEnclosingBox), wheelEvent);
320
321     RenderBox* currentEnclosingBox = &initialEnclosingBox;
322     while (currentEnclosingBox) {
323         if (RenderLayer* boxLayer = currentEnclosingBox->layer()) {
324             const PlatformWheelEvent* platformEvent = wheelEvent->wheelEvent();
325             bool scrollingWasHandled;
326             if (platformEvent != nullptr)
327                 scrollingWasHandled = boxLayer->handleWheelEvent(platformEvent->copyWithDeltas(filteredPlatformDelta.width(), filteredPlatformDelta.height()));
328             else
329                 scrollingWasHandled = didScrollInScrollableArea(boxLayer, wheelEvent);
330
331             if (scrollingWasHandled) {
332                 if (stopElement)
333                     *stopElement = currentEnclosingBox->element();
334                 return true;
335             }
336         }
337
338         if (stopElement && *stopElement && *stopElement == currentEnclosingBox->element())
339             return true;
340
341         currentEnclosingBox = currentEnclosingBox->containingBlock();
342         if (currentEnclosingBox && currentEnclosingBox->isRenderNamedFlowThread())
343             currentEnclosingBox = RenderNamedFlowThread::fragmentFromRenderBoxAsRenderBlock(currentEnclosingBox, roundedIntPoint(wheelEvent->absoluteLocation()), initialEnclosingBox);
344         if (!currentEnclosingBox || currentEnclosingBox->isRenderView())
345             return false;
346     }
347     return false;
348 }
349
350 #if (ENABLE(TOUCH_EVENTS) && !PLATFORM(IOS))
351 static inline bool shouldGesturesTriggerActive()
352 {
353     // If the platform we're on supports GestureTapDown and GestureTapCancel then we'll
354     // rely on them to set the active state. Unfortunately there's no generic way to
355     // know in advance what event types are supported.
356     return false;
357 }
358 #endif
359
360 #if !PLATFORM(COCOA)
361
362 inline bool EventHandler::eventLoopHandleMouseUp(const MouseEventWithHitTestResults&)
363 {
364     return false;
365 }
366
367 #if ENABLE(DRAG_SUPPORT)
368 inline bool EventHandler::eventLoopHandleMouseDragged(const MouseEventWithHitTestResults&)
369 {
370     return false;
371 }
372 #endif
373
374 #endif
375
376 EventHandler::EventHandler(Frame& frame)
377     : m_frame(frame)
378     , m_hoverTimer(*this, &EventHandler::hoverTimerFired)
379 #if ENABLE(CURSOR_SUPPORT)
380     , m_cursorUpdateTimer(*this, &EventHandler::cursorUpdateTimerFired)
381 #endif
382 #if PLATFORM(MAC)
383     , m_pendingMomentumWheelEventsTimer(*this, &EventHandler::clearLatchedState)
384 #endif
385     , m_autoscrollController(std::make_unique<AutoscrollController>())
386 #if !ENABLE(IOS_TOUCH_EVENTS)
387     , m_fakeMouseMoveEventTimer(*this, &EventHandler::fakeMouseMoveEventTimerFired)
388 #endif
389 #if ENABLE(CURSOR_VISIBILITY)
390     , m_autoHideCursorTimer(*this, &EventHandler::autoHideCursorTimerFired)
391 #endif
392 {
393 }
394
395 EventHandler::~EventHandler()
396 {
397 #if !ENABLE(IOS_TOUCH_EVENTS)
398     ASSERT(!m_fakeMouseMoveEventTimer.isActive());
399 #endif
400 #if ENABLE(CURSOR_VISIBILITY)
401     ASSERT(!m_autoHideCursorTimer.isActive());
402 #endif
403 }
404     
405 #if ENABLE(DRAG_SUPPORT)
406 DragState& EventHandler::dragState()
407 {
408     static NeverDestroyed<DragState> state;
409     return state;
410 }
411 #endif // ENABLE(DRAG_SUPPORT)
412     
413 void EventHandler::clear()
414 {
415     m_hoverTimer.stop();
416 #if ENABLE(CURSOR_SUPPORT)
417     m_cursorUpdateTimer.stop();
418 #endif
419 #if !ENABLE(IOS_TOUCH_EVENTS)
420     m_fakeMouseMoveEventTimer.stop();
421 #endif
422 #if ENABLE(CURSOR_VISIBILITY)
423     cancelAutoHideCursorTimer();
424 #endif
425     m_resizeLayer = nullptr;
426     m_elementUnderMouse = nullptr;
427     m_lastElementUnderMouse = nullptr;
428     m_lastMouseMoveEventSubframe = nullptr;
429     m_lastScrollbarUnderMouse = nullptr;
430     m_clickCount = 0;
431     m_clickNode = nullptr;
432 #if ENABLE(IOS_GESTURE_EVENTS)
433     m_gestureInitialDiameter = GestureUnknown;
434     m_gestureInitialRotation = GestureUnknown;
435 #endif
436 #if ENABLE(IOS_GESTURE_EVENTS) || ENABLE(MAC_GESTURE_EVENTS)
437     m_gestureLastDiameter = GestureUnknown;
438     m_gestureLastRotation = GestureUnknown;
439     m_gestureTargets.clear();
440 #endif
441 #if ENABLE(IOS_TOUCH_EVENTS)
442     m_touches.clear();
443     m_firstTouchID = InvalidTouchIdentifier;
444     m_touchEventTargetSubframe = nullptr;
445 #endif
446     m_frameSetBeingResized = nullptr;
447 #if ENABLE(DRAG_SUPPORT)
448     m_dragTarget = nullptr;
449     m_shouldOnlyFireDragOverEvent = false;
450 #endif
451     m_mousePositionIsUnknown = true;
452     m_lastKnownMousePosition = IntPoint();
453     m_lastKnownMouseGlobalPosition = IntPoint();
454     m_mousePressNode = nullptr;
455     m_mousePressed = false;
456     m_capturesDragging = false;
457     m_capturingMouseEventsElement = nullptr;
458     clearLatchedState();
459 #if ENABLE(TOUCH_EVENTS) && !ENABLE(IOS_TOUCH_EVENTS)
460     m_originatingTouchPointTargets.clear();
461     m_originatingTouchPointDocument = nullptr;
462     m_originatingTouchPointTargetKey = 0;
463 #endif
464     m_maxMouseMovedDuration = 0;
465     m_baseEventType = PlatformEvent::NoType;
466     m_didStartDrag = false;
467 }
468
469 void EventHandler::nodeWillBeRemoved(Node& nodeToBeRemoved)
470 {
471     if (nodeToBeRemoved.contains(m_clickNode.get()))
472         m_clickNode = nullptr;
473 }
474
475 static void setSelectionIfNeeded(FrameSelection& selection, const VisibleSelection& newSelection)
476 {
477     if (selection.selection() != newSelection && selection.shouldChangeSelection(newSelection))
478         selection.setSelection(newSelection);
479 }
480
481 static inline bool dispatchSelectStart(Node* node)
482 {
483     if (!node || !node->renderer())
484         return true;
485
486     return node->dispatchEvent(Event::create(eventNames().selectstartEvent, true, true));
487 }
488
489 static Node* nodeToSelectOnMouseDownForNode(Node& targetNode)
490 {
491 #if ENABLE(USERSELECT_ALL)
492     if (Node* rootUserSelectAll = Position::rootUserSelectAllForNode(&targetNode))
493         return rootUserSelectAll;
494 #endif
495
496     if (targetNode.shouldSelectOnMouseDown())
497         return &targetNode;
498
499     return nullptr;
500 }
501
502 static VisibleSelection expandSelectionToRespectSelectOnMouseDown(Node& targetNode, const VisibleSelection& selection)
503 {
504     Node* nodeToSelect = nodeToSelectOnMouseDownForNode(targetNode);
505     if (!nodeToSelect)
506         return selection;
507
508     VisibleSelection newSelection(selection);
509     newSelection.setBase(positionBeforeNode(nodeToSelect).upstream(CanCrossEditingBoundary));
510     newSelection.setExtent(positionAfterNode(nodeToSelect).downstream(CanCrossEditingBoundary));
511
512     return newSelection;
513 }
514
515 bool EventHandler::updateSelectionForMouseDownDispatchingSelectStart(Node* targetNode, const VisibleSelection& selection, TextGranularity granularity)
516 {
517     if (Position::nodeIsUserSelectNone(targetNode))
518         return false;
519
520     if (!dispatchSelectStart(targetNode))
521         return false;
522
523     if (selection.isRange())
524         m_selectionInitiationState = ExtendedSelection;
525     else {
526         granularity = CharacterGranularity;
527         m_selectionInitiationState = PlacedCaret;
528     }
529
530     m_frame.selection().setSelectionByMouseIfDifferent(selection, granularity);
531
532     return true;
533 }
534
535 void EventHandler::selectClosestWordFromHitTestResult(const HitTestResult& result, AppendTrailingWhitespace appendTrailingWhitespace)
536 {
537     Node* targetNode = result.targetNode();
538     VisibleSelection newSelection;
539
540     if (targetNode && targetNode->renderer()) {
541         VisiblePosition pos(targetNode->renderer()->positionForPoint(result.localPoint(), nullptr));
542         if (pos.isNotNull()) {
543             newSelection = VisibleSelection(pos);
544             newSelection.expandUsingGranularity(WordGranularity);
545         }
546
547         if (appendTrailingWhitespace == ShouldAppendTrailingWhitespace && newSelection.isRange())
548             newSelection.appendTrailingWhitespace();
549
550         updateSelectionForMouseDownDispatchingSelectStart(targetNode, expandSelectionToRespectSelectOnMouseDown(*targetNode, newSelection), WordGranularity);
551     }
552 }
553
554 static AppendTrailingWhitespace shouldAppendTrailingWhitespace(const MouseEventWithHitTestResults& result, const Frame& frame)
555 {
556     return (result.event().clickCount() == 2 && frame.editor().isSelectTrailingWhitespaceEnabled()) ? ShouldAppendTrailingWhitespace : DontAppendTrailingWhitespace;
557 }
558
559 void EventHandler::selectClosestWordFromMouseEvent(const MouseEventWithHitTestResults& result)
560 {
561     if (m_mouseDownMayStartSelect)
562         selectClosestWordFromHitTestResult(result.hitTestResult(), shouldAppendTrailingWhitespace(result, m_frame));
563 }
564
565 #if !PLATFORM(MAC)
566 VisibleSelection EventHandler::selectClosestWordFromHitTestResultBasedOnLookup(const HitTestResult&)
567 {
568     return VisibleSelection();
569 }
570 #endif
571     
572 void EventHandler::selectClosestContextualWordFromMouseEvent(const MouseEventWithHitTestResults& mouseEvent)
573 {
574     Node* targetNode = mouseEvent.targetNode();
575     const HitTestResult& result = mouseEvent.hitTestResult();
576     VisibleSelection newSelection;
577     bool appendTrailingWhitespace = shouldAppendTrailingWhitespace(mouseEvent, m_frame);
578     
579     if (targetNode && targetNode->renderer()) {
580         newSelection = selectClosestWordFromHitTestResultBasedOnLookup(result);
581         if (newSelection.isNone()) {
582             VisiblePosition pos(targetNode->renderer()->positionForPoint(result.localPoint(), nullptr));
583             if (pos.isNotNull()) {
584                 newSelection = VisibleSelection(pos);
585                 newSelection.expandUsingGranularity(WordGranularity);
586             }
587         }
588         
589         if (appendTrailingWhitespace == ShouldAppendTrailingWhitespace && newSelection.isRange())
590             newSelection.appendTrailingWhitespace();
591         
592         updateSelectionForMouseDownDispatchingSelectStart(targetNode, expandSelectionToRespectSelectOnMouseDown(*targetNode, newSelection), WordGranularity);
593     }
594 }
595     
596 void EventHandler::selectClosestContextualWordOrLinkFromMouseEvent(const MouseEventWithHitTestResults& result)
597 {
598     Element* urlElement = result.hitTestResult().URLElement();
599     if (!urlElement || !isDraggableLink(*urlElement)) {
600         if (Node* targetNode = result.targetNode()) {
601             if (isEditableNode(*targetNode))
602                 return selectClosestWordFromMouseEvent(result);
603         }
604
605         return selectClosestContextualWordFromMouseEvent(result);
606     }
607
608     Node* targetNode = result.targetNode();
609
610     if (targetNode && targetNode->renderer() && m_mouseDownMayStartSelect) {
611         VisibleSelection newSelection;
612         VisiblePosition pos(targetNode->renderer()->positionForPoint(result.localPoint(), nullptr));
613         if (pos.isNotNull() && pos.deepEquivalent().deprecatedNode()->isDescendantOf(urlElement))
614             newSelection = VisibleSelection::selectionFromContentsOfNode(urlElement);
615
616         updateSelectionForMouseDownDispatchingSelectStart(targetNode, expandSelectionToRespectSelectOnMouseDown(*targetNode, newSelection), WordGranularity);
617     }
618 }
619
620 bool EventHandler::handleMousePressEventDoubleClick(const MouseEventWithHitTestResults& event)
621 {
622     if (event.event().button() != LeftButton)
623         return false;
624
625     if (m_frame.selection().isRange())
626         // A double-click when range is already selected
627         // should not change the selection.  So, do not call
628         // selectClosestWordFromMouseEvent, but do set
629         // m_beganSelectingText to prevent handleMouseReleaseEvent
630         // from setting caret selection.
631         m_selectionInitiationState = ExtendedSelection;
632     else
633         selectClosestWordFromMouseEvent(event);
634
635     return true;
636 }
637
638 bool EventHandler::handleMousePressEventTripleClick(const MouseEventWithHitTestResults& event)
639 {
640     if (event.event().button() != LeftButton)
641         return false;
642     
643     Node* targetNode = event.targetNode();
644     if (!(targetNode && targetNode->renderer() && m_mouseDownMayStartSelect))
645         return false;
646
647     VisibleSelection newSelection;
648     VisiblePosition pos(targetNode->renderer()->positionForPoint(event.localPoint(), nullptr));
649     if (pos.isNotNull()) {
650         newSelection = VisibleSelection(pos);
651         newSelection.expandUsingGranularity(ParagraphGranularity);
652     }
653
654     return updateSelectionForMouseDownDispatchingSelectStart(targetNode, expandSelectionToRespectSelectOnMouseDown(*targetNode, newSelection), ParagraphGranularity);
655 }
656
657 static int textDistance(const Position& start, const Position& end)
658 {
659     RefPtr<Range> range = Range::create(start.anchorNode()->document(), start, end);
660     return TextIterator::rangeLength(range.get(), true);
661 }
662
663 bool EventHandler::handleMousePressEventSingleClick(const MouseEventWithHitTestResults& event)
664 {
665     m_frame.document()->updateLayoutIgnorePendingStylesheets();
666     Node* targetNode = event.targetNode();
667     if (!(targetNode && targetNode->renderer() && m_mouseDownMayStartSelect))
668         return false;
669
670     // Extend the selection if the Shift key is down, unless the click is in a link.
671     bool extendSelection = event.event().shiftKey() && !event.isOverLink();
672
673     // Don't restart the selection when the mouse is pressed on an
674     // existing selection so we can allow for text dragging.
675     if (FrameView* view = m_frame.view()) {
676         LayoutPoint vPoint = view->windowToContents(event.event().position());
677         if (!extendSelection && m_frame.selection().contains(vPoint)) {
678             m_mouseDownWasSingleClickInSelection = true;
679             return false;
680         }
681     }
682
683     VisiblePosition visiblePos(targetNode->renderer()->positionForPoint(event.localPoint(), nullptr));
684     if (visiblePos.isNull())
685         visiblePos = VisiblePosition(firstPositionInOrBeforeNode(targetNode), DOWNSTREAM);
686     Position pos = visiblePos.deepEquivalent();
687
688     VisibleSelection newSelection = m_frame.selection().selection();
689     TextGranularity granularity = CharacterGranularity;
690
691     if (extendSelection && newSelection.isCaretOrRange()) {
692         VisibleSelection selectionInUserSelectAll = expandSelectionToRespectSelectOnMouseDown(*targetNode, VisibleSelection(pos));
693         if (selectionInUserSelectAll.isRange()) {
694             if (comparePositions(selectionInUserSelectAll.start(), newSelection.start()) < 0)
695                 pos = selectionInUserSelectAll.start();
696             else if (comparePositions(newSelection.end(), selectionInUserSelectAll.end()) < 0)
697                 pos = selectionInUserSelectAll.end();
698         }
699
700         if (!m_frame.editor().behavior().shouldConsiderSelectionAsDirectional() && pos.isNotNull()) {
701             // See <rdar://problem/3668157> REGRESSION (Mail): shift-click deselects when selection
702             // was created right-to-left
703             Position start = newSelection.start();
704             Position end = newSelection.end();
705             int distanceToStart = textDistance(start, pos);
706             int distanceToEnd = textDistance(pos, end);
707             if (distanceToStart <= distanceToEnd)
708                 newSelection = VisibleSelection(end, pos);
709             else
710                 newSelection = VisibleSelection(start, pos);
711         } else
712             newSelection.setExtent(pos);
713
714         if (m_frame.selection().granularity() != CharacterGranularity) {
715             granularity = m_frame.selection().granularity();
716             newSelection.expandUsingGranularity(m_frame.selection().granularity());
717         }
718     } else
719         newSelection = expandSelectionToRespectSelectOnMouseDown(*targetNode, visiblePos);
720
721     bool handled = updateSelectionForMouseDownDispatchingSelectStart(targetNode, newSelection, granularity);
722
723     if (event.event().button() == MiddleButton) {
724         // Ignore handled, since we want to paste to where the caret was placed anyway.
725         handled = handlePasteGlobalSelection(event.event()) || handled;
726     }
727     return handled;
728 }
729
730 static inline bool canMouseDownStartSelect(Node* node)
731 {
732     if (!node || !node->renderer())
733         return true;
734
735     return node->canStartSelection() || Position::nodeIsUserSelectAll(node);
736 }
737
738 bool EventHandler::handleMousePressEvent(const MouseEventWithHitTestResults& event)
739 {
740 #if ENABLE(DRAG_SUPPORT)
741     // Reset drag state.
742     dragState().source = nullptr;
743 #endif
744
745 #if !ENABLE(IOS_TOUCH_EVENTS)
746     cancelFakeMouseMoveEvent();
747 #endif
748
749     m_frame.document()->updateLayoutIgnorePendingStylesheets();
750
751     if (ScrollView* scrollView = m_frame.view()) {
752         if (scrollView->isPointInScrollbarCorner(event.event().position()))
753             return false;
754     }
755
756     bool singleClick = event.event().clickCount() <= 1;
757
758     // If we got the event back, that must mean it wasn't prevented,
759     // so it's allowed to start a drag or selection if it wasn't in a scrollbar.
760     m_mouseDownMayStartSelect = canMouseDownStartSelect(event.targetNode()) && !event.scrollbar();
761     
762 #if ENABLE(DRAG_SUPPORT)
763     // Careful that the drag starting logic stays in sync with eventMayStartDrag()
764     // FIXME: eventMayStartDrag() does not check for shift key press, link or image event targets.
765     // Bug: https://bugs.webkit.org/show_bug.cgi?id=155390
766
767     // Single mouse down on links or images can always trigger drag-n-drop.
768     bool isMouseDownOnLinkOrImage = event.isOverLink() || event.hitTestResult().image();
769     m_mouseDownMayStartDrag = singleClick && (!event.event().shiftKey() || isMouseDownOnLinkOrImage);
770 #endif
771
772     m_mouseDownWasSingleClickInSelection = false;
773
774     m_mouseDown = event.event();
775
776     if (m_immediateActionStage != ImmediateActionStage::PerformedHitTest)
777         m_immediateActionStage = ImmediateActionStage::None;
778
779     if (event.isOverWidget() && passWidgetMouseDownEventToWidget(event))
780         return true;
781
782     if (is<SVGDocument>(*m_frame.document()) && downcast<SVGDocument>(*m_frame.document()).zoomAndPanEnabled()) {
783         if (event.event().shiftKey() && singleClick) {
784             m_svgPan = true;
785             downcast<SVGDocument>(*m_frame.document()).startPan(m_frame.view()->windowToContents(event.event().position()));
786             return true;
787         }
788     }
789
790     // We don't do this at the start of mouse down handling,
791     // because we don't want to do it until we know we didn't hit a widget.
792     if (singleClick)
793         focusDocumentView();
794
795     m_mousePressNode = event.targetNode();
796 #if ENABLE(DRAG_SUPPORT)
797     m_dragStartPos = event.event().position();
798 #endif
799
800     bool swallowEvent = false;
801     m_mousePressed = true;
802     m_selectionInitiationState = HaveNotStartedSelection;
803
804     if (event.event().clickCount() == 2)
805         swallowEvent = handleMousePressEventDoubleClick(event);
806     else if (event.event().clickCount() >= 3)
807         swallowEvent = handleMousePressEventTripleClick(event);
808     else
809         swallowEvent = handleMousePressEventSingleClick(event);
810     
811     m_mouseDownMayStartAutoscroll = m_mouseDownMayStartSelect
812         || (m_mousePressNode && m_mousePressNode->renderBox() && m_mousePressNode->renderBox()->canBeProgramaticallyScrolled());
813
814     return swallowEvent;
815 }
816
817 #if ENABLE(DRAG_SUPPORT)
818 bool EventHandler::handleMouseDraggedEvent(const MouseEventWithHitTestResults& event)
819 {
820     if (!m_mousePressed)
821         return false;
822
823     if (handleDrag(event, ShouldCheckDragHysteresis))
824         return true;
825
826     Node* targetNode = event.targetNode();
827     if (event.event().button() != LeftButton || !targetNode)
828         return false;
829
830     RenderObject* renderer = targetNode->renderer();
831     if (!renderer) {
832         Element* parent = targetNode->parentOrShadowHostElement();
833         if (!parent)
834             return false;
835
836         renderer = parent->renderer();
837         if (!renderer || !renderer->isListBox())
838             return false;
839     }
840
841 #if PLATFORM(COCOA) // FIXME: Why does this assertion fire on other platforms?
842     ASSERT(m_mouseDownMayStartSelect || m_mouseDownMayStartAutoscroll);
843 #endif
844
845     m_mouseDownMayStartDrag = false;
846
847     if (m_mouseDownMayStartAutoscroll && !panScrollInProgress()) {
848         m_autoscrollController->startAutoscrollForSelection(renderer);
849         m_mouseDownMayStartAutoscroll = false;
850     }
851
852     if (m_selectionInitiationState != ExtendedSelection) {
853         HitTestResult result(m_mouseDownPos);
854         m_frame.document()->renderView()->hitTest(HitTestRequest(), result);
855
856         updateSelectionForMouseDrag(result);
857     }
858     updateSelectionForMouseDrag(event.hitTestResult());
859     return true;
860 }
861     
862 bool EventHandler::eventMayStartDrag(const PlatformMouseEvent& event) const
863 {
864     // This is a pre-flight check of whether the event might lead to a drag being started.  Be careful
865     // that its logic needs to stay in sync with handleMouseMoveEvent() and the way we setMouseDownMayStartDrag
866     // in handleMousePressEvent
867     RenderView* renderView = m_frame.contentRenderer();
868     if (!renderView)
869         return false;
870
871     if (event.button() != LeftButton || event.clickCount() != 1)
872         return false;
873
874     FrameView* view = m_frame.view();
875     if (!view)
876         return false;
877
878     Page* page = m_frame.page();
879     if (!page)
880         return false;
881
882     updateDragSourceActionsAllowed();
883     HitTestRequest request(HitTestRequest::ReadOnly | HitTestRequest::DisallowShadowContent);
884     HitTestResult result(view->windowToContents(event.position()));
885     renderView->hitTest(request, result);
886     DragState state;
887     return result.innerElement() && page->dragController().draggableElement(&m_frame, result.innerElement(), result.roundedPointInInnerNodeFrame(), state);
888 }
889
890 void EventHandler::updateSelectionForMouseDrag()
891 {
892     FrameView* view = m_frame.view();
893     if (!view)
894         return;
895     RenderView* renderView = m_frame.contentRenderer();
896     if (!renderView)
897         return;
898
899     HitTestRequest request(HitTestRequest::ReadOnly | HitTestRequest::Active | HitTestRequest::Move | HitTestRequest::DisallowShadowContent);
900     HitTestResult result(view->windowToContents(m_lastKnownMousePosition));
901     renderView->hitTest(request, result);
902     updateSelectionForMouseDrag(result);
903 }
904
905 static VisiblePosition selectionExtentRespectingEditingBoundary(const VisibleSelection& selection, const LayoutPoint& localPoint, Node* targetNode)
906 {
907     FloatPoint selectionEndPoint = localPoint;
908     Element* editableElement = selection.rootEditableElement();
909
910     if (!targetNode->renderer())
911         return VisiblePosition();
912
913     if (editableElement && !editableElement->contains(targetNode)) {
914         if (!editableElement->renderer())
915             return VisiblePosition();
916
917         FloatPoint absolutePoint = targetNode->renderer()->localToAbsolute(FloatPoint(selectionEndPoint));
918         selectionEndPoint = editableElement->renderer()->absoluteToLocal(absolutePoint);
919         targetNode = editableElement;
920     }
921
922     return targetNode->renderer()->positionForPoint(LayoutPoint(selectionEndPoint), nullptr);
923 }
924
925 void EventHandler::updateSelectionForMouseDrag(const HitTestResult& hitTestResult)
926 {
927     if (!m_mouseDownMayStartSelect)
928         return;
929
930     Node* target = hitTestResult.targetNode();
931     if (!target)
932         return;
933
934     VisiblePosition targetPosition = selectionExtentRespectingEditingBoundary(m_frame.selection().selection(), hitTestResult.localPoint(), target);
935
936     // Don't modify the selection if we're not on a node.
937     if (targetPosition.isNull())
938         return;
939
940     // Restart the selection if this is the first mouse move. This work is usually
941     // done in handleMousePressEvent, but not if the mouse press was on an existing selection.
942     VisibleSelection newSelection = m_frame.selection().selection();
943
944     // Special case to limit selection to the containing block for SVG text.
945     // FIXME: Isn't there a better non-SVG-specific way to do this?
946     if (Node* selectionBaseNode = newSelection.base().deprecatedNode())
947         if (RenderObject* selectionBaseRenderer = selectionBaseNode->renderer())
948             if (selectionBaseRenderer->isSVGText())
949                 if (target->renderer()->containingBlock() != selectionBaseRenderer->containingBlock())
950                     return;
951
952     if (m_selectionInitiationState == HaveNotStartedSelection && !dispatchSelectStart(target))
953         return;
954
955     if (m_selectionInitiationState != ExtendedSelection) {
956         // Always extend selection here because it's caused by a mouse drag
957         m_selectionInitiationState = ExtendedSelection;
958         newSelection = VisibleSelection(targetPosition);
959     }
960
961 #if ENABLE(USERSELECT_ALL)
962     Node* rootUserSelectAllForMousePressNode = Position::rootUserSelectAllForNode(m_mousePressNode.get());
963     if (rootUserSelectAllForMousePressNode && rootUserSelectAllForMousePressNode == Position::rootUserSelectAllForNode(target)) {
964         newSelection.setBase(positionBeforeNode(rootUserSelectAllForMousePressNode).upstream(CanCrossEditingBoundary));
965         newSelection.setExtent(positionAfterNode(rootUserSelectAllForMousePressNode).downstream(CanCrossEditingBoundary));
966     } else {
967         // Reset base for user select all when base is inside user-select-all area and extent < base.
968         if (rootUserSelectAllForMousePressNode && comparePositions(target->renderer()->positionForPoint(hitTestResult.localPoint(), nullptr), m_mousePressNode->renderer()->positionForPoint(m_dragStartPos, nullptr)) < 0)
969             newSelection.setBase(positionAfterNode(rootUserSelectAllForMousePressNode).downstream(CanCrossEditingBoundary));
970         
971         Node* rootUserSelectAllForTarget = Position::rootUserSelectAllForNode(target);
972         if (rootUserSelectAllForTarget && m_mousePressNode->renderer() && comparePositions(target->renderer()->positionForPoint(hitTestResult.localPoint(), nullptr), m_mousePressNode->renderer()->positionForPoint(m_dragStartPos, nullptr)) < 0)
973             newSelection.setExtent(positionBeforeNode(rootUserSelectAllForTarget).upstream(CanCrossEditingBoundary));
974         else if (rootUserSelectAllForTarget && m_mousePressNode->renderer())
975             newSelection.setExtent(positionAfterNode(rootUserSelectAllForTarget).downstream(CanCrossEditingBoundary));
976         else
977             newSelection.setExtent(targetPosition);
978     }
979 #else
980     newSelection.setExtent(targetPosition);
981 #endif
982
983     if (m_frame.selection().granularity() != CharacterGranularity)
984         newSelection.expandUsingGranularity(m_frame.selection().granularity());
985
986     m_frame.selection().setSelectionByMouseIfDifferent(newSelection, m_frame.selection().granularity(),
987         FrameSelection::AdjustEndpointsAtBidiBoundary);
988 }
989 #endif // ENABLE(DRAG_SUPPORT)
990
991 void EventHandler::lostMouseCapture()
992 {
993     m_frame.selection().setCaretBlinkingSuspended(false);
994 }
995
996 bool EventHandler::handleMouseUp(const MouseEventWithHitTestResults& event)
997 {
998     if (eventLoopHandleMouseUp(event))
999         return true;
1000     
1001     // If this was the first click in the window, we don't even want to clear the selection.
1002     // This case occurs when the user clicks on a draggable element, since we have to process
1003     // the mouse down and drag events to see if we might start a drag.  For other first clicks
1004     // in a window, we just don't acceptFirstMouse, and the whole down-drag-up sequence gets
1005     // ignored upstream of this layer.
1006     return eventActivatedView(event.event());
1007 }    
1008
1009 bool EventHandler::handleMouseReleaseEvent(const MouseEventWithHitTestResults& event)
1010 {
1011     if (autoscrollInProgress())
1012         stopAutoscrollTimer();
1013
1014     if (handleMouseUp(event))
1015         return true;
1016
1017     // Used to prevent mouseMoveEvent from initiating a drag before
1018     // the mouse is pressed again.
1019     m_mousePressed = false;
1020     m_capturesDragging = false;
1021 #if ENABLE(DRAG_SUPPORT)
1022     m_mouseDownMayStartDrag = false;
1023 #endif
1024     m_mouseDownMayStartSelect = false;
1025     m_mouseDownMayStartAutoscroll = false;
1026     m_mouseDownWasInSubframe = false;
1027   
1028     bool handled = false;
1029
1030     // Clear the selection if the mouse didn't move after the last mouse
1031     // press and it's not a context menu click.  We do this so when clicking
1032     // on the selection, the selection goes away.  However, if we are
1033     // editing, place the caret.
1034     if (m_mouseDownWasSingleClickInSelection && m_selectionInitiationState != ExtendedSelection
1035 #if ENABLE(DRAG_SUPPORT)
1036             && m_dragStartPos == event.event().position()
1037 #endif
1038             && m_frame.selection().isRange()
1039             && event.event().button() != RightButton) {
1040         VisibleSelection newSelection;
1041         Node* node = event.targetNode();
1042         bool caretBrowsing = m_frame.settings().caretBrowsingEnabled();
1043         if (node && node->renderer() && (caretBrowsing || node->hasEditableStyle())) {
1044             VisiblePosition pos = node->renderer()->positionForPoint(event.localPoint(), nullptr);
1045             newSelection = VisibleSelection(pos);
1046         }
1047
1048         setSelectionIfNeeded(m_frame.selection(), newSelection);
1049
1050         handled = true;
1051     }
1052
1053     if (event.event().button() == MiddleButton) {
1054         // Ignore handled, since we want to paste to where the caret was placed anyway.
1055         handled = handlePasteGlobalSelection(event.event()) || handled;
1056     }
1057
1058     return handled;
1059 }
1060
1061 #if ENABLE(PAN_SCROLLING)
1062
1063 void EventHandler::didPanScrollStart()
1064 {
1065     m_autoscrollController->didPanScrollStart();
1066 }
1067
1068 void EventHandler::didPanScrollStop()
1069 {
1070     m_autoscrollController->didPanScrollStop();
1071 }
1072
1073 void EventHandler::startPanScrolling(RenderElement* renderer)
1074 {
1075 #if !PLATFORM(IOS)
1076     if (!is<RenderBox>(*renderer))
1077         return;
1078     m_autoscrollController->startPanScrolling(downcast<RenderBox>(renderer), lastKnownMousePosition());
1079     invalidateClick();
1080 #endif
1081 }
1082
1083 #endif // ENABLE(PAN_SCROLLING)
1084
1085 RenderBox* EventHandler::autoscrollRenderer() const
1086 {
1087     return m_autoscrollController->autoscrollRenderer();
1088 }
1089
1090 void EventHandler::updateAutoscrollRenderer()
1091 {
1092     m_autoscrollController->updateAutoscrollRenderer();
1093 }
1094
1095 bool EventHandler::autoscrollInProgress() const
1096 {
1097     return m_autoscrollController->autoscrollInProgress();
1098 }
1099
1100 bool EventHandler::panScrollInProgress() const
1101 {
1102     return m_autoscrollController->panScrollInProgress();
1103 }
1104
1105 #if ENABLE(DRAG_SUPPORT)
1106 DragSourceAction EventHandler::updateDragSourceActionsAllowed() const
1107 {
1108     Page* page = m_frame.page();
1109     if (!page)
1110         return DragSourceActionNone;
1111
1112     FrameView* view = m_frame.view();
1113     if (!view)
1114         return DragSourceActionNone;
1115
1116     return page->dragController().delegateDragSourceAction(view->contentsToRootView(m_mouseDownPos));
1117 }
1118 #endif // ENABLE(DRAG_SUPPORT)
1119
1120 HitTestResult EventHandler::hitTestResultAtPoint(const LayoutPoint& point, HitTestRequest::HitTestRequestType hitType, const LayoutSize& padding)
1121 {
1122     // We always send hitTestResultAtPoint to the main frame if we have one,
1123     // otherwise we might hit areas that are obscured by higher frames.
1124     if (!m_frame.isMainFrame()) {
1125         Frame& mainFrame = m_frame.mainFrame();
1126         FrameView* frameView = m_frame.view();
1127         FrameView* mainView = mainFrame.view();
1128         if (frameView && mainView) {
1129             IntPoint mainFramePoint = mainView->rootViewToContents(frameView->contentsToRootView(roundedIntPoint(point)));
1130             return mainFrame.eventHandler().hitTestResultAtPoint(mainFramePoint, hitType, padding);
1131         }
1132     }
1133
1134     HitTestResult result(point, padding.height(), padding.width(), padding.height(), padding.width());
1135
1136     RenderView* renderView = m_frame.contentRenderer();
1137     if (!renderView)
1138         return result;
1139     
1140     // hitTestResultAtPoint is specifically used to hitTest into all frames, thus it always allows child frame content.
1141     HitTestRequest request(hitType | HitTestRequest::AllowChildFrameContent);
1142     renderView->hitTest(request, result);
1143     if (!request.readOnly())
1144         m_frame.document()->updateHoverActiveState(request, result.innerElement());
1145
1146     if (request.disallowsShadowContent())
1147         result.setToNonShadowAncestor();
1148
1149     return result;
1150 }
1151
1152 void EventHandler::stopAutoscrollTimer(bool rendererIsBeingDestroyed)
1153 {
1154     m_autoscrollController->stopAutoscrollTimer(rendererIsBeingDestroyed);
1155 }
1156
1157 bool EventHandler::scrollOverflow(ScrollDirection direction, ScrollGranularity granularity, Node* startingNode)
1158 {
1159     Node* node = startingNode;
1160
1161     if (!node)
1162         node = m_frame.document()->focusedElement();
1163
1164     if (!node)
1165         node = m_mousePressNode.get();
1166     
1167     if (node) {
1168         auto r = node->renderer();
1169         if (r && !r->isListBox() && r->enclosingBox().scroll(direction, granularity)) {
1170             setFrameWasScrolledByUser();
1171             return true;
1172         }
1173     }
1174
1175     return false;
1176 }
1177
1178 bool EventHandler::logicalScrollOverflow(ScrollLogicalDirection direction, ScrollGranularity granularity, Node* startingNode)
1179 {
1180     Node* node = startingNode;
1181
1182     if (!node)
1183         node = m_frame.document()->focusedElement();
1184
1185     if (!node)
1186         node = m_mousePressNode.get();
1187     
1188     if (node) {
1189         auto r = node->renderer();
1190         if (r && !r->isListBox() && r->enclosingBox().logicalScroll(direction, granularity)) {
1191             setFrameWasScrolledByUser();
1192             return true;
1193         }
1194     }
1195
1196     return false;
1197 }
1198
1199 bool EventHandler::scrollRecursively(ScrollDirection direction, ScrollGranularity granularity, Node* startingNode)
1200 {
1201     // The layout needs to be up to date to determine if we can scroll. We may be
1202     // here because of an onLoad event, in which case the final layout hasn't been performed yet.
1203     m_frame.document()->updateLayoutIgnorePendingStylesheets();
1204     if (scrollOverflow(direction, granularity, startingNode))
1205         return true;    
1206     Frame* frame = &m_frame;
1207     FrameView* view = frame->view();
1208     if (view && view->scroll(direction, granularity))
1209         return true;
1210     frame = frame->tree().parent();
1211     if (!frame)
1212         return false;
1213     return frame->eventHandler().scrollRecursively(direction, granularity, m_frame.ownerElement());
1214 }
1215
1216 bool EventHandler::logicalScrollRecursively(ScrollLogicalDirection direction, ScrollGranularity granularity, Node* startingNode)
1217 {
1218     // The layout needs to be up to date to determine if we can scroll. We may be
1219     // here because of an onLoad event, in which case the final layout hasn't been performed yet.
1220     m_frame.document()->updateLayoutIgnorePendingStylesheets();
1221     if (logicalScrollOverflow(direction, granularity, startingNode))
1222         return true;    
1223     Frame* frame = &m_frame;
1224     FrameView* view = frame->view();
1225     
1226     bool scrolled = false;
1227 #if PLATFORM(COCOA)
1228     // Mac also resets the scroll position in the inline direction.
1229     if (granularity == ScrollByDocument && view && view->logicalScroll(ScrollInlineDirectionBackward, ScrollByDocument))
1230         scrolled = true;
1231 #endif
1232     if (view && view->logicalScroll(direction, granularity))
1233         scrolled = true;
1234     
1235     if (scrolled)
1236         return true;
1237     
1238     frame = frame->tree().parent();
1239     if (!frame)
1240         return false;
1241
1242     return frame->eventHandler().logicalScrollRecursively(direction, granularity, m_frame.ownerElement());
1243 }
1244
1245 IntPoint EventHandler::lastKnownMousePosition() const
1246 {
1247     return m_lastKnownMousePosition;
1248 }
1249
1250 Frame* EventHandler::subframeForHitTestResult(const MouseEventWithHitTestResults& hitTestResult)
1251 {
1252     if (!hitTestResult.isOverWidget())
1253         return 0;
1254     return subframeForTargetNode(hitTestResult.targetNode());
1255 }
1256
1257 Frame* EventHandler::subframeForTargetNode(Node* node)
1258 {
1259     if (!node)
1260         return nullptr;
1261
1262     auto renderer = node->renderer();
1263     if (!is<RenderWidget>(renderer))
1264         return nullptr;
1265
1266     Widget* widget = downcast<RenderWidget>(*renderer).widget();
1267     if (!is<FrameView>(widget))
1268         return nullptr;
1269
1270     return &downcast<FrameView>(*widget).frame();
1271 }
1272
1273 #if ENABLE(CURSOR_SUPPORT)
1274 static bool isSubmitImage(Node* node)
1275 {
1276     return is<HTMLInputElement>(node) && downcast<HTMLInputElement>(*node).isImageButton();
1277 }
1278
1279 // Returns true if the node's editable block is not current focused for editing
1280 static bool nodeIsNotBeingEdited(const Node& node, const Frame& frame)
1281 {
1282     return frame.selection().selection().rootEditableElement() != node.rootEditableElement();
1283 }
1284
1285 bool EventHandler::useHandCursor(Node* node, bool isOverLink, bool shiftKey)
1286 {
1287     if (!node)
1288         return false;
1289
1290     bool editable = node->hasEditableStyle();
1291     bool editableLinkEnabled = false;
1292
1293     // If the link is editable, then we need to check the settings to see whether or not the link should be followed
1294     if (editable) {
1295         switch (m_frame.settings().editableLinkBehavior()) {
1296         default:
1297         case EditableLinkDefaultBehavior:
1298         case EditableLinkAlwaysLive:
1299             editableLinkEnabled = true;
1300             break;
1301
1302         case EditableLinkNeverLive:
1303             editableLinkEnabled = false;
1304             break;
1305
1306         case EditableLinkLiveWhenNotFocused:
1307             editableLinkEnabled = nodeIsNotBeingEdited(*node, m_frame) || shiftKey;
1308             break;
1309
1310         case EditableLinkOnlyLiveWithShiftKey:
1311             editableLinkEnabled = shiftKey;
1312             break;
1313         }
1314     }
1315
1316     return ((isOverLink || isSubmitImage(node)) && (!editable || editableLinkEnabled));
1317 }
1318
1319 void EventHandler::cursorUpdateTimerFired()
1320 {
1321     ASSERT(m_frame.document());
1322     updateCursor();
1323 }
1324
1325 void EventHandler::updateCursor()
1326 {
1327     if (m_mousePositionIsUnknown)
1328         return;
1329
1330     FrameView* view = m_frame.view();
1331     if (!view)
1332         return;
1333
1334     RenderView* renderView = view->renderView();
1335     if (!renderView)
1336         return;
1337
1338     if (!view->shouldSetCursor())
1339         return;
1340
1341     bool shiftKey;
1342     bool ctrlKey;
1343     bool altKey;
1344     bool metaKey;
1345     PlatformKeyboardEvent::getCurrentModifierState(shiftKey, ctrlKey, altKey, metaKey);
1346
1347     HitTestRequest request(HitTestRequest::ReadOnly | HitTestRequest::AllowFrameScrollbars);
1348     HitTestResult result(view->windowToContents(m_lastKnownMousePosition));
1349     renderView->hitTest(request, result);
1350
1351     OptionalCursor optionalCursor = selectCursor(result, shiftKey);
1352     if (optionalCursor.isCursorChange()) {
1353         m_currentMouseCursor = optionalCursor.cursor();
1354         view->setCursor(m_currentMouseCursor);
1355     }
1356 }
1357
1358 OptionalCursor EventHandler::selectCursor(const HitTestResult& result, bool shiftKey)
1359 {
1360     if (m_resizeLayer && m_resizeLayer->inResizeMode())
1361         return NoCursorChange;
1362
1363     if (!m_frame.page())
1364         return NoCursorChange;
1365
1366 #if ENABLE(PAN_SCROLLING)
1367     if (m_frame.mainFrame().eventHandler().panScrollInProgress())
1368         return NoCursorChange;
1369 #endif
1370
1371     // Use always pointer cursor for scrollbars.
1372     if (result.scrollbar()) {
1373 #if ENABLE(CURSOR_VISIBILITY)
1374         cancelAutoHideCursorTimer();
1375 #endif
1376         return pointerCursor();
1377     }
1378
1379     Node* node = result.targetNode();
1380     if (!node)
1381         return NoCursorChange;
1382
1383     auto renderer = node->renderer();
1384     RenderStyle* style = renderer ? &renderer->style() : nullptr;
1385     bool horizontalText = !style || style->isHorizontalWritingMode();
1386     const Cursor& iBeam = horizontalText ? iBeamCursor() : verticalTextCursor();
1387
1388 #if ENABLE(CURSOR_VISIBILITY)
1389     if (style && style->cursorVisibility() == CursorVisibilityAutoHide)
1390         startAutoHideCursorTimer();
1391     else
1392         cancelAutoHideCursorTimer();
1393 #endif
1394
1395     if (renderer) {
1396         Cursor overrideCursor;
1397         switch (renderer->getCursor(roundedIntPoint(result.localPoint()), overrideCursor)) {
1398         case SetCursorBasedOnStyle:
1399             break;
1400         case SetCursor:
1401             return overrideCursor;
1402         case DoNotSetCursor:
1403             return NoCursorChange;
1404         }
1405     }
1406
1407     if (style && style->cursors()) {
1408         const CursorList* cursors = style->cursors();
1409         for (unsigned i = 0; i < cursors->size(); ++i) {
1410             StyleImage* styleImage = (*cursors)[i].image();
1411             if (!styleImage)
1412                 continue;
1413             CachedImage* cachedImage = styleImage->cachedImage();
1414             if (!cachedImage)
1415                 continue;
1416             float scale = styleImage->imageScaleFactor();
1417             // Get hotspot and convert from logical pixels to physical pixels.
1418             IntPoint hotSpot = (*cursors)[i].hotSpot();
1419             FloatSize size = cachedImage->imageForRenderer(renderer)->size();
1420             if (cachedImage->errorOccurred())
1421                 continue;
1422             // Limit the size of cursors (in UI pixels) so that they cannot be
1423             // used to cover UI elements in chrome.
1424             size.scale(1 / scale);
1425             if (size.width() > maximumCursorSize || size.height() > maximumCursorSize)
1426                 continue;
1427
1428             Image* image = cachedImage->imageForRenderer(renderer);
1429 #if ENABLE(MOUSE_CURSOR_SCALE)
1430             // Ensure no overflow possible in calculations above.
1431             if (scale < minimumCursorScale)
1432                 continue;
1433             return Cursor(image, hotSpot, scale);
1434 #else
1435             ASSERT(scale == 1);
1436             return Cursor(image, hotSpot);
1437 #endif // ENABLE(MOUSE_CURSOR_SCALE)
1438         }
1439     }
1440
1441     // During selection, use an I-beam regardless of the content beneath the cursor.
1442     // If a drag may be starting or we're capturing mouse events for a particular node, don't treat this as a selection.
1443     if (m_mousePressed
1444         && m_mouseDownMayStartSelect
1445 #if ENABLE(DRAG_SUPPORT)
1446         && !m_mouseDownMayStartDrag
1447 #endif
1448         && m_frame.selection().isCaretOrRange()
1449         && !m_capturingMouseEventsElement)
1450         return iBeam;
1451
1452     switch (style ? style->cursor() : CursorAuto) {
1453     case CursorAuto: {
1454         bool editable = node->hasEditableStyle();
1455
1456         if (useHandCursor(node, result.isOverLink(), shiftKey))
1457             return handCursor();
1458
1459         bool inResizer = false;
1460         if (renderer) {
1461             if (RenderLayer* layer = renderer->enclosingLayer()) {
1462                 if (FrameView* view = m_frame.view())
1463                     inResizer = layer->isPointInResizeControl(view->windowToContents(roundedIntPoint(result.localPoint())));
1464             }
1465         }
1466
1467         if ((editable || (renderer && renderer->isText() && node->canStartSelection())) && !inResizer && !result.scrollbar())
1468             return iBeam;
1469         return pointerCursor();
1470     }
1471     case CursorCross:
1472         return crossCursor();
1473     case CursorPointer:
1474         return handCursor();
1475     case CursorMove:
1476         return moveCursor();
1477     case CursorAllScroll:
1478         return moveCursor();
1479     case CursorEResize:
1480         return eastResizeCursor();
1481     case CursorWResize:
1482         return westResizeCursor();
1483     case CursorNResize:
1484         return northResizeCursor();
1485     case CursorSResize:
1486         return southResizeCursor();
1487     case CursorNeResize:
1488         return northEastResizeCursor();
1489     case CursorSwResize:
1490         return southWestResizeCursor();
1491     case CursorNwResize:
1492         return northWestResizeCursor();
1493     case CursorSeResize:
1494         return southEastResizeCursor();
1495     case CursorNsResize:
1496         return northSouthResizeCursor();
1497     case CursorEwResize:
1498         return eastWestResizeCursor();
1499     case CursorNeswResize:
1500         return northEastSouthWestResizeCursor();
1501     case CursorNwseResize:
1502         return northWestSouthEastResizeCursor();
1503     case CursorColResize:
1504         return columnResizeCursor();
1505     case CursorRowResize:
1506         return rowResizeCursor();
1507     case CursorText:
1508         return iBeamCursor();
1509     case CursorWait:
1510         return waitCursor();
1511     case CursorHelp:
1512         return helpCursor();
1513     case CursorVerticalText:
1514         return verticalTextCursor();
1515     case CursorCell:
1516         return cellCursor();
1517     case CursorContextMenu:
1518         return contextMenuCursor();
1519     case CursorProgress:
1520         return progressCursor();
1521     case CursorNoDrop:
1522         return noDropCursor();
1523     case CursorAlias:
1524         return aliasCursor();
1525     case CursorCopy:
1526         return copyCursor();
1527     case CursorNone:
1528         return noneCursor();
1529     case CursorNotAllowed:
1530         return notAllowedCursor();
1531     case CursorDefault:
1532         return pointerCursor();
1533     case CursorZoomIn:
1534         return zoomInCursor();
1535     case CursorZoomOut:
1536         return zoomOutCursor();
1537     case CursorWebkitGrab:
1538         return grabCursor();
1539     case CursorWebkitGrabbing:
1540         return grabbingCursor();
1541     }
1542     return pointerCursor();
1543 }
1544 #endif // ENABLE(CURSOR_SUPPORT)
1545
1546 #if ENABLE(CURSOR_VISIBILITY)
1547 void EventHandler::startAutoHideCursorTimer()
1548 {
1549     Page* page = m_frame.page();
1550     if (!page)
1551         return;
1552
1553     m_autoHideCursorTimer.startOneShot(page->settings().timeWithoutMouseMovementBeforeHidingControls());
1554
1555 #if !ENABLE(IOS_TOUCH_EVENTS)
1556     // The fake mouse move event screws up the auto-hide feature (by resetting the auto-hide timer)
1557     // so cancel any pending fake mouse moves.
1558     if (m_fakeMouseMoveEventTimer.isActive())
1559         m_fakeMouseMoveEventTimer.stop();
1560 #endif
1561 }
1562
1563 void EventHandler::cancelAutoHideCursorTimer()
1564 {
1565     if (m_autoHideCursorTimer.isActive())
1566         m_autoHideCursorTimer.stop();
1567 }
1568
1569 void EventHandler::autoHideCursorTimerFired()
1570 {
1571     m_currentMouseCursor = noneCursor();
1572     FrameView* view = m_frame.view();
1573     if (view && view->isActive())
1574         view->setCursor(m_currentMouseCursor);
1575 }
1576 #endif
1577
1578 static LayoutPoint documentPointForWindowPoint(Frame& frame, const IntPoint& windowPoint)
1579 {
1580     FrameView* view = frame.view();
1581     // FIXME: Is it really OK to use the wrong coordinates here when view is 0?
1582     // Historically the code would just crash; this is clearly no worse than that.
1583     return view ? view->windowToContents(windowPoint) : windowPoint;
1584 }
1585
1586 static Scrollbar* scrollbarForMouseEvent(const MouseEventWithHitTestResults& mouseEvent, FrameView* view)
1587 {
1588     if (view) {
1589         if (auto* scrollbar = view->scrollbarAtPoint(mouseEvent.event().position()))
1590             return scrollbar;
1591     }
1592     return mouseEvent.scrollbar();
1593
1594 }
1595
1596 bool EventHandler::handleMousePressEvent(const PlatformMouseEvent& platformMouseEvent)
1597 {
1598     RefPtr<FrameView> protector(m_frame.view());
1599
1600     if (InspectorInstrumentation::handleMousePress(m_frame)) {
1601         invalidateClick();
1602         return true;
1603     }
1604
1605     if (m_frame.mainFrame().pageOverlayController().handleMouseEvent(platformMouseEvent))
1606         return true;
1607
1608 #if ENABLE(TOUCH_EVENTS)
1609     bool defaultPrevented = dispatchSyntheticTouchEventIfEnabled(platformMouseEvent);
1610     if (defaultPrevented)
1611         return true;
1612 #endif
1613
1614     UserGestureIndicator gestureIndicator(DefinitelyProcessingUserGesture, m_frame.document());
1615
1616     // FIXME (bug 68185): this call should be made at another abstraction layer
1617     m_frame.loader().resetMultipleFormSubmissionProtection();
1618
1619 #if !ENABLE(IOS_TOUCH_EVENTS)
1620     cancelFakeMouseMoveEvent();
1621 #endif
1622     m_mousePressed = true;
1623     m_capturesDragging = true;
1624     setLastKnownMousePosition(platformMouseEvent);
1625     m_mouseDownTimestamp = platformMouseEvent.timestamp();
1626 #if ENABLE(DRAG_SUPPORT)
1627     m_mouseDownMayStartDrag = false;
1628 #endif
1629     m_mouseDownMayStartSelect = false;
1630     m_mouseDownMayStartAutoscroll = false;
1631     if (FrameView* view = m_frame.view())
1632         m_mouseDownPos = view->windowToContents(platformMouseEvent.position());
1633     else {
1634         invalidateClick();
1635         return false;
1636     }
1637     m_mouseDownWasInSubframe = false;
1638
1639     HitTestRequest request(HitTestRequest::Active | HitTestRequest::DisallowShadowContent);
1640     // Save the document point we generate in case the window coordinate is invalidated by what happens
1641     // when we dispatch the event.
1642     LayoutPoint documentPoint = documentPointForWindowPoint(m_frame, platformMouseEvent.position());
1643     MouseEventWithHitTestResults mouseEvent = m_frame.document()->prepareMouseEvent(request, documentPoint, platformMouseEvent);
1644
1645     if (!mouseEvent.targetNode()) {
1646         invalidateClick();
1647         return false;
1648     }
1649
1650     m_mousePressNode = mouseEvent.targetNode();
1651
1652     RefPtr<Frame> subframe = subframeForHitTestResult(mouseEvent);
1653     if (subframe && passMousePressEventToSubframe(mouseEvent, subframe.get())) {
1654         // Start capturing future events for this frame.  We only do this if we didn't clear
1655         // the m_mousePressed flag, which may happen if an AppKit widget entered a modal event loop.
1656         m_capturesDragging = subframe->eventHandler().capturesDragging();
1657         if (m_mousePressed && m_capturesDragging) {
1658             m_capturingMouseEventsElement = subframe->ownerElement();
1659             m_eventHandlerWillResetCapturingMouseEventsElement = true;
1660         }
1661         invalidateClick();
1662         return true;
1663     }
1664
1665 #if ENABLE(PAN_SCROLLING)
1666     // We store whether pan scrolling is in progress before calling stopAutoscrollTimer()
1667     // because it will set m_autoscrollType to NoAutoscroll on return.
1668     bool isPanScrollInProgress = m_frame.mainFrame().eventHandler().panScrollInProgress();
1669     stopAutoscrollTimer();
1670     if (isPanScrollInProgress) {
1671         // We invalidate the click when exiting pan scrolling so that we don't inadvertently navigate
1672         // away from the current page (e.g. the click was on a hyperlink). See <rdar://problem/6095023>.
1673         invalidateClick();
1674         return true;
1675     }
1676 #endif
1677
1678     m_clickCount = platformMouseEvent.clickCount();
1679     m_clickNode = mouseEvent.targetNode();
1680
1681     if (!m_clickNode) {
1682         invalidateClick();
1683         return false;
1684     }
1685
1686     if (FrameView* view = m_frame.view()) {
1687         RenderLayer* layer = m_clickNode->renderer() ? m_clickNode->renderer()->enclosingLayer() : 0;
1688         IntPoint p = view->windowToContents(platformMouseEvent.position());
1689         if (layer && layer->isPointInResizeControl(p)) {
1690             layer->setInResizeMode(true);
1691             m_resizeLayer = layer;
1692             m_offsetFromResizeCorner = layer->offsetFromResizeCorner(p);
1693             invalidateClick();
1694             return true;
1695         }
1696     }
1697
1698     m_frame.selection().setCaretBlinkingSuspended(true);
1699
1700     bool swallowEvent = !dispatchMouseEvent(eventNames().mousedownEvent, mouseEvent.targetNode(), true, m_clickCount, platformMouseEvent, true);
1701     m_capturesDragging = !swallowEvent || mouseEvent.scrollbar();
1702
1703     // If the hit testing originally determined the event was in a scrollbar, refetch the MouseEventWithHitTestResults
1704     // in case the scrollbar widget was destroyed when the mouse event was handled.
1705     if (mouseEvent.scrollbar()) {
1706         const bool wasLastScrollBar = mouseEvent.scrollbar() == m_lastScrollbarUnderMouse.get();
1707         mouseEvent = m_frame.document()->prepareMouseEvent(HitTestRequest(), documentPoint, platformMouseEvent);
1708         if (wasLastScrollBar && mouseEvent.scrollbar() != m_lastScrollbarUnderMouse.get())
1709             m_lastScrollbarUnderMouse = nullptr;
1710     }
1711
1712     if (!swallowEvent) {
1713         // Refetch the event target node if it currently is the shadow node inside an <input> element.
1714         // If a mouse event handler changes the input element type to one that has a widget associated,
1715         // we'd like to EventHandler::handleMousePressEvent to pass the event to the widget and thus the
1716         // event target node can't still be the shadow node.
1717         if (is<ShadowRoot>(*mouseEvent.targetNode()) && is<HTMLInputElement>(*downcast<ShadowRoot>(*mouseEvent.targetNode()).host()))
1718             mouseEvent = m_frame.document()->prepareMouseEvent(HitTestRequest(), documentPoint, platformMouseEvent);
1719     }
1720
1721     Scrollbar* scrollbar = scrollbarForMouseEvent(mouseEvent, m_frame.view());
1722     updateLastScrollbarUnderMouse(scrollbar, true);
1723
1724     bool passedToScrollbar = scrollbar && passMousePressEventToScrollbar(mouseEvent, scrollbar);
1725     if (!swallowEvent) {
1726         if (passedToScrollbar)
1727             swallowEvent = true;
1728         else
1729             swallowEvent = handleMousePressEvent(mouseEvent);
1730     }
1731     return swallowEvent;
1732 }
1733
1734 // This method only exists for platforms that don't know how to deliver 
1735 bool EventHandler::handleMouseDoubleClickEvent(const PlatformMouseEvent& platformMouseEvent)
1736 {
1737     RefPtr<FrameView> protector(m_frame.view());
1738
1739     m_frame.selection().setCaretBlinkingSuspended(false);
1740
1741     UserGestureIndicator gestureIndicator(DefinitelyProcessingUserGesture, m_frame.document());
1742
1743     // We get this instead of a second mouse-up 
1744     m_mousePressed = false;
1745     setLastKnownMousePosition(platformMouseEvent);
1746
1747     HitTestRequest request(HitTestRequest::Active | HitTestRequest::DisallowShadowContent);
1748     MouseEventWithHitTestResults mouseEvent = prepareMouseEvent(request, platformMouseEvent);
1749     Frame* subframe = subframeForHitTestResult(mouseEvent);
1750     if (m_eventHandlerWillResetCapturingMouseEventsElement)
1751         m_capturingMouseEventsElement = nullptr;
1752     if (subframe && passMousePressEventToSubframe(mouseEvent, subframe))
1753         return true;
1754
1755     m_clickCount = platformMouseEvent.clickCount();
1756     bool swallowMouseUpEvent = !dispatchMouseEvent(eventNames().mouseupEvent, mouseEvent.targetNode(), true, m_clickCount, platformMouseEvent, false);
1757
1758     bool swallowClickEvent = platformMouseEvent.button() != RightButton && mouseEvent.targetNode() == m_clickNode && !dispatchMouseEvent(eventNames().clickEvent, mouseEvent.targetNode(), true, m_clickCount, platformMouseEvent, true);
1759
1760     if (m_lastScrollbarUnderMouse)
1761         swallowMouseUpEvent = m_lastScrollbarUnderMouse->mouseUp(platformMouseEvent);
1762
1763     bool swallowMouseReleaseEvent = !swallowMouseUpEvent && handleMouseReleaseEvent(mouseEvent);
1764
1765     invalidateClick();
1766
1767     return swallowMouseUpEvent || swallowClickEvent || swallowMouseReleaseEvent;
1768 }
1769
1770 static ScrollableArea* enclosingScrollableArea(Node* node)
1771 {
1772     if (!node)
1773         return nullptr;
1774
1775     for (auto element = node; element; element = element->parentOrShadowHostNode()) {
1776         if (is<HTMLIFrameElement>(*element) || is<HTMLHtmlElement>(*element) || is<HTMLDocument>(*element))
1777             return nullptr;
1778
1779         auto renderer = element->renderer();
1780         if (!renderer)
1781             continue;
1782
1783         if (is<RenderListBox>(*renderer))
1784             return downcast<RenderListBox>(renderer);
1785
1786         return renderer->enclosingLayer();
1787     }
1788
1789     return nullptr;
1790 }
1791
1792 bool EventHandler::mouseMoved(const PlatformMouseEvent& event)
1793 {
1794     RefPtr<FrameView> protector(m_frame.view());
1795     MaximumDurationTracker maxDurationTracker(&m_maxMouseMovedDuration);
1796
1797     if (m_frame.mainFrame().pageOverlayController().handleMouseEvent(event))
1798         return true;
1799
1800     HitTestResult hoveredNode = HitTestResult(LayoutPoint());
1801     bool result = handleMouseMoveEvent(event, &hoveredNode);
1802
1803     Page* page = m_frame.page();
1804     if (!page)
1805         return result;
1806
1807     if (auto scrolledArea = enclosingScrollableArea(hoveredNode.innerNode())) {
1808         if (FrameView* frameView = m_frame.view()) {
1809             if (frameView->containsScrollableArea(scrolledArea))
1810                 scrolledArea->mouseMovedInContentArea();
1811         }
1812     }
1813
1814     if (FrameView* frameView = m_frame.view())
1815         frameView->mouseMovedInContentArea();  
1816
1817     hoveredNode.setToNonShadowAncestor();
1818     page->chrome().mouseDidMoveOverElement(hoveredNode, event.modifierFlags());
1819     page->chrome().setToolTip(hoveredNode);
1820     return result;
1821 }
1822
1823 bool EventHandler::passMouseMovedEventToScrollbars(const PlatformMouseEvent& event)
1824 {
1825     HitTestResult hoveredNode;
1826     return handleMouseMoveEvent(event, &hoveredNode, true);
1827 }
1828
1829 bool EventHandler::handleMouseMoveEvent(const PlatformMouseEvent& platformMouseEvent, HitTestResult* hoveredNode, bool onlyUpdateScrollbars)
1830 {
1831 #if ENABLE(TOUCH_EVENTS)
1832     bool defaultPrevented = dispatchSyntheticTouchEventIfEnabled(platformMouseEvent);
1833     if (defaultPrevented)
1834         return true;
1835 #endif
1836
1837     RefPtr<FrameView> protector(m_frame.view());
1838     
1839     setLastKnownMousePosition(platformMouseEvent);
1840
1841     if (m_hoverTimer.isActive())
1842         m_hoverTimer.stop();
1843
1844 #if ENABLE(CURSOR_SUPPORT)
1845     m_cursorUpdateTimer.stop();
1846 #endif
1847
1848 #if !ENABLE(IOS_TOUCH_EVENTS)
1849     cancelFakeMouseMoveEvent();
1850 #endif
1851
1852     if (m_svgPan) {
1853         downcast<SVGDocument>(*m_frame.document()).updatePan(m_frame.view()->windowToContents(m_lastKnownMousePosition));
1854         return true;
1855     }
1856
1857     if (m_frameSetBeingResized)
1858         return !dispatchMouseEvent(eventNames().mousemoveEvent, m_frameSetBeingResized.get(), false, 0, platformMouseEvent, false);
1859
1860     // On iOS, our scrollbars are managed by UIKit.
1861 #if !PLATFORM(IOS)
1862     // Send events right to a scrollbar if the mouse is pressed.
1863     if (m_lastScrollbarUnderMouse && m_mousePressed)
1864         return m_lastScrollbarUnderMouse->mouseMoved(platformMouseEvent);
1865 #endif
1866
1867     HitTestRequest::HitTestRequestType hitType = HitTestRequest::Move | HitTestRequest::DisallowShadowContent | HitTestRequest::AllowFrameScrollbars;
1868     if (m_mousePressed)
1869         hitType |= HitTestRequest::Active;
1870     else if (onlyUpdateScrollbars) {
1871         // Mouse events should be treated as "read-only" if we're updating only scrollbars. This  
1872         // means that :hover and :active freeze in the state they were in, rather than updating  
1873         // for nodes the mouse moves while the window is not key (which will be the case if 
1874         // onlyUpdateScrollbars is true). 
1875         hitType |= HitTestRequest::ReadOnly;
1876     }
1877
1878 #if ENABLE(TOUCH_EVENTS) && !ENABLE(IOS_TOUCH_EVENTS)
1879     // Treat any mouse move events as readonly if the user is currently touching the screen.
1880     if (m_touchPressed)
1881         hitType |= HitTestRequest::Active | HitTestRequest::ReadOnly;
1882 #endif
1883     HitTestRequest request(hitType);
1884     MouseEventWithHitTestResults mouseEvent = prepareMouseEvent(request, platformMouseEvent);
1885     if (hoveredNode)
1886         *hoveredNode = mouseEvent.hitTestResult();
1887
1888     if (m_resizeLayer && m_resizeLayer->inResizeMode())
1889         m_resizeLayer->resize(platformMouseEvent, m_offsetFromResizeCorner);
1890     else {
1891         Scrollbar* scrollbar = mouseEvent.scrollbar();
1892         updateLastScrollbarUnderMouse(scrollbar, !m_mousePressed);
1893
1894         // On iOS, our scrollbars are managed by UIKit.
1895 #if !PLATFORM(IOS)
1896         if (!m_mousePressed && scrollbar)
1897             scrollbar->mouseMoved(platformMouseEvent); // Handle hover effects on platforms that support visual feedback on scrollbar hovering.
1898 #endif
1899         if (onlyUpdateScrollbars) {
1900             updateMouseEventTargetNode(mouseEvent.targetNode(), platformMouseEvent, true);
1901             return true;
1902         }
1903     }
1904
1905     bool swallowEvent = false;
1906     RefPtr<Frame> newSubframe = m_capturingMouseEventsElement.get() ? subframeForTargetNode(m_capturingMouseEventsElement.get()) : subframeForHitTestResult(mouseEvent);
1907  
1908     // 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.
1909     if (m_lastMouseMoveEventSubframe && m_lastMouseMoveEventSubframe->tree().isDescendantOf(&m_frame) && m_lastMouseMoveEventSubframe != newSubframe)
1910         passMouseMoveEventToSubframe(mouseEvent, m_lastMouseMoveEventSubframe.get());
1911
1912     if (newSubframe) {
1913         // Update over/out state before passing the event to the subframe.
1914         updateMouseEventTargetNode(mouseEvent.targetNode(), platformMouseEvent, true);
1915         
1916         // Event dispatch in updateMouseEventTargetNode may have caused the subframe of the target
1917         // node to be detached from its FrameView, in which case the event should not be passed.
1918         if (newSubframe->view())
1919             swallowEvent |= passMouseMoveEventToSubframe(mouseEvent, newSubframe.get(), hoveredNode);
1920 #if ENABLE(CURSOR_SUPPORT)
1921     } else {
1922         if (FrameView* view = m_frame.view()) {
1923             OptionalCursor optionalCursor = selectCursor(mouseEvent.hitTestResult(), platformMouseEvent.shiftKey());
1924             if (optionalCursor.isCursorChange()) {
1925                 m_currentMouseCursor = optionalCursor.cursor();
1926                 view->setCursor(m_currentMouseCursor);
1927             }
1928         }
1929 #endif
1930     }
1931     
1932     m_lastMouseMoveEventSubframe = newSubframe;
1933
1934     if (swallowEvent)
1935         return true;
1936     
1937     swallowEvent = !dispatchMouseEvent(eventNames().mousemoveEvent, mouseEvent.targetNode(), false, 0, platformMouseEvent, true);
1938 #if ENABLE(DRAG_SUPPORT)
1939     if (!swallowEvent)
1940         swallowEvent = handleMouseDraggedEvent(mouseEvent);
1941 #endif // ENABLE(DRAG_SUPPORT)
1942
1943     return swallowEvent;
1944 }
1945
1946 void EventHandler::invalidateClick()
1947 {
1948     m_clickCount = 0;
1949     m_clickNode = nullptr;
1950 }
1951
1952 static Node* targetNodeForClickEvent(Node* mousePressNode, Node* mouseReleaseNode)
1953 {
1954     if (!mousePressNode || !mouseReleaseNode)
1955         return nullptr;
1956
1957     if (mousePressNode == mouseReleaseNode)
1958         return mouseReleaseNode;
1959
1960     Element* mouseReleaseShadowHost = mouseReleaseNode->shadowHost();
1961     if (mouseReleaseShadowHost && mouseReleaseShadowHost == mousePressNode->shadowHost()) {
1962         // We want to dispatch the click to the shadow tree host element to give listeners the illusion that the
1963         // shadom tree is a single element. For example, we want to give the illusion that <input type="range">
1964         // is a single element even though it is a composition of multiple shadom tree elements.
1965         return mouseReleaseShadowHost;
1966     }
1967     return nullptr;
1968 }
1969
1970 bool EventHandler::handleMouseReleaseEvent(const PlatformMouseEvent& platformMouseEvent)
1971 {
1972     RefPtr<FrameView> protector(m_frame.view());
1973
1974     m_frame.selection().setCaretBlinkingSuspended(false);
1975
1976     if (m_frame.mainFrame().pageOverlayController().handleMouseEvent(platformMouseEvent))
1977         return true;
1978
1979 #if ENABLE(TOUCH_EVENTS)
1980     bool defaultPrevented = dispatchSyntheticTouchEventIfEnabled(platformMouseEvent);
1981     if (defaultPrevented)
1982         return true;
1983 #endif
1984
1985     UserGestureIndicator gestureIndicator(DefinitelyProcessingUserGesture, m_frame.document());
1986
1987 #if ENABLE(PAN_SCROLLING)
1988     m_autoscrollController->handleMouseReleaseEvent(platformMouseEvent);
1989 #endif
1990
1991     m_mousePressed = false;
1992     setLastKnownMousePosition(platformMouseEvent);
1993
1994     if (m_svgPan) {
1995         m_svgPan = false;
1996         downcast<SVGDocument>(*m_frame.document()).updatePan(m_frame.view()->windowToContents(m_lastKnownMousePosition));
1997         return true;
1998     }
1999
2000     if (m_frameSetBeingResized)
2001         return !dispatchMouseEvent(eventNames().mouseupEvent, m_frameSetBeingResized.get(), true, m_clickCount, platformMouseEvent, false);
2002
2003     // If an immediate action began or was completed using this series of mouse events, then we should send mouseup to
2004     // the DOM and return now so that we don't perform our own default behaviors.
2005     if (m_immediateActionStage == ImmediateActionStage::ActionCompleted || m_immediateActionStage == ImmediateActionStage::ActionUpdated || m_immediateActionStage == ImmediateActionStage::ActionCancelledAfterUpdate) {
2006         m_immediateActionStage = ImmediateActionStage::None;
2007         return !dispatchMouseEvent(eventNames().mouseupEvent, m_lastElementUnderMouse.get(), true, m_clickCount, platformMouseEvent, false);
2008     }
2009     m_immediateActionStage = ImmediateActionStage::None;
2010
2011     if (m_lastScrollbarUnderMouse) {
2012         invalidateClick();
2013         m_lastScrollbarUnderMouse->mouseUp(platformMouseEvent);
2014         bool cancelable = true;
2015         bool setUnder = false;
2016         return !dispatchMouseEvent(eventNames().mouseupEvent, m_lastElementUnderMouse.get(), cancelable, m_clickCount, platformMouseEvent, setUnder);
2017     }
2018
2019     HitTestRequest request(HitTestRequest::Release | HitTestRequest::DisallowShadowContent);
2020     MouseEventWithHitTestResults mouseEvent = prepareMouseEvent(request, platformMouseEvent);
2021     Frame* subframe = m_capturingMouseEventsElement.get() ? subframeForTargetNode(m_capturingMouseEventsElement.get()) : subframeForHitTestResult(mouseEvent);
2022     if (m_eventHandlerWillResetCapturingMouseEventsElement)
2023         m_capturingMouseEventsElement = nullptr;
2024     if (subframe && passMouseReleaseEventToSubframe(mouseEvent, subframe))
2025         return true;
2026
2027     bool swallowMouseUpEvent = !dispatchMouseEvent(eventNames().mouseupEvent, mouseEvent.targetNode(), true, m_clickCount, platformMouseEvent, false);
2028
2029     bool contextMenuEvent = platformMouseEvent.button() == RightButton;
2030
2031     Node* nodeToClick = targetNodeForClickEvent(m_clickNode.get(), mouseEvent.targetNode());
2032     bool swallowClickEvent = m_clickCount > 0 && !contextMenuEvent && nodeToClick && !dispatchMouseEvent(eventNames().clickEvent, nodeToClick, true, m_clickCount, platformMouseEvent, true);
2033
2034     if (m_resizeLayer) {
2035         m_resizeLayer->setInResizeMode(false);
2036         m_resizeLayer = nullptr;
2037     }
2038
2039     bool swallowMouseReleaseEvent = false;
2040     if (!swallowMouseUpEvent)
2041         swallowMouseReleaseEvent = handleMouseReleaseEvent(mouseEvent);
2042
2043     invalidateClick();
2044
2045     return swallowMouseUpEvent || swallowClickEvent || swallowMouseReleaseEvent;
2046 }
2047
2048 #if ENABLE(MOUSE_FORCE_EVENTS)
2049 bool EventHandler::handleMouseForceEvent(const PlatformMouseEvent& event)
2050 {
2051     RefPtr<FrameView> protector(m_frame.view());
2052
2053     setLastKnownMousePosition(event);
2054
2055     HitTestRequest::HitTestRequestType hitType = HitTestRequest::DisallowShadowContent | HitTestRequest::Active;
2056
2057     HitTestRequest request(hitType);
2058     MouseEventWithHitTestResults mouseEvent = prepareMouseEvent(request, event);
2059
2060     bool swallowedEvent = !dispatchMouseEvent(eventNames().webkitmouseforcechangedEvent, mouseEvent.targetNode(), false, 0, event, false);
2061     if (event.type() == PlatformEvent::MouseForceDown)
2062         swallowedEvent |= !dispatchMouseEvent(eventNames().webkitmouseforcedownEvent, mouseEvent.targetNode(), false, 0, event, false);
2063     if (event.type() == PlatformEvent::MouseForceUp)
2064         swallowedEvent |= !dispatchMouseEvent(eventNames().webkitmouseforceupEvent, mouseEvent.targetNode(), false, 0, event, false);
2065
2066     return swallowedEvent;
2067 }
2068 #else
2069 bool EventHandler::handleMouseForceEvent(const PlatformMouseEvent& )
2070 {
2071     return false;
2072 }
2073 #endif // #if ENABLE(MOUSE_FORCE_EVENTS)
2074
2075 bool EventHandler::handlePasteGlobalSelection(const PlatformMouseEvent& platformMouseEvent)
2076 {
2077     // If the event was a middle click, attempt to copy global selection in after
2078     // the newly set caret position.
2079     //
2080     // This code is called from either the mouse up or mouse down handling. There
2081     // is some debate about when the global selection is pasted:
2082     //   xterm: pastes on up.
2083     //   GTK: pastes on down.
2084     //   Qt: pastes on up.
2085     //   Firefox: pastes on up.
2086     //   Chromium: pastes on up.
2087     //
2088     // There is something of a webcompat angle to this well, as highlighted by
2089     // crbug.com/14608. Pages can clear text boxes 'onclick' and, if we paste on
2090     // down then the text is pasted just before the onclick handler runs and
2091     // clears the text box. So it's important this happens after the event
2092     // handlers have been fired.
2093 #if PLATFORM(GTK)
2094     if (platformMouseEvent.type() != PlatformEvent::MousePressed)
2095         return false;
2096 #else
2097     if (platformMouseEvent.type() != PlatformEvent::MouseReleased)
2098         return false;
2099 #endif
2100
2101     if (!m_frame.page())
2102         return false;
2103     Frame& focusFrame = m_frame.page()->focusController().focusedOrMainFrame();
2104     // Do not paste here if the focus was moved somewhere else.
2105     if (&m_frame == &focusFrame && m_frame.editor().client()->supportsGlobalSelection())
2106         return m_frame.editor().command(ASCIILiteral("PasteGlobalSelection")).execute();
2107
2108     return false;
2109 }
2110
2111 #if ENABLE(DRAG_SUPPORT)
2112
2113 bool EventHandler::dispatchDragEvent(const AtomicString& eventType, Element& dragTarget, const PlatformMouseEvent& event, DataTransfer* dataTransfer)
2114 {
2115     FrameView* view = m_frame.view();
2116
2117     // FIXME: We might want to dispatch a dragleave even if the view is gone.
2118     if (!view)
2119         return false;
2120
2121     view->disableLayerFlushThrottlingTemporarilyForInteraction();
2122     Ref<MouseEvent> me = MouseEvent::create(eventType,
2123         true, true, event.timestamp(), m_frame.document()->defaultView(),
2124         0, event.globalPosition().x(), event.globalPosition().y(), event.position().x(), event.position().y(),
2125 #if ENABLE(POINTER_LOCK)
2126         event.movementDelta().x(), event.movementDelta().y(),
2127 #endif
2128         event.ctrlKey(), event.altKey(), event.shiftKey(), event.metaKey(),
2129         0, 0, event.force(), dataTransfer);
2130
2131     dragTarget.dispatchEvent(me);
2132     return me->defaultPrevented();
2133 }
2134
2135 static bool targetIsFrame(Node* target, Frame*& frame)
2136 {
2137     if (!is<HTMLFrameElementBase>(target))
2138         return false;
2139
2140     frame = downcast<HTMLFrameElementBase>(*target).contentFrame();
2141     return true;
2142 }
2143
2144 static DragOperation convertDropZoneOperationToDragOperation(const String& dragOperation)
2145 {
2146     if (dragOperation == "copy")
2147         return DragOperationCopy;
2148     if (dragOperation == "move")
2149         return DragOperationMove;
2150     if (dragOperation == "link")
2151         return DragOperationLink;
2152     return DragOperationNone;
2153 }
2154
2155 static String convertDragOperationToDropZoneOperation(DragOperation operation)
2156 {
2157     switch (operation) {
2158     case DragOperationCopy:
2159         return ASCIILiteral("copy");
2160     case DragOperationMove:
2161         return ASCIILiteral("move");
2162     case DragOperationLink:
2163         return ASCIILiteral("link");
2164     default:
2165         return ASCIILiteral("copy");
2166     }
2167 }
2168
2169 static bool hasDropZoneType(DataTransfer& dataTransfer, const String& keyword)
2170 {
2171     if (keyword.startsWith("file:"))
2172         return dataTransfer.hasFileOfType(keyword.substring(5));
2173
2174     if (keyword.startsWith("string:"))
2175         return dataTransfer.hasStringOfType(keyword.substring(7));
2176
2177     return false;
2178 }
2179
2180 static bool findDropZone(Node* target, DataTransfer* dataTransfer)
2181 {
2182     ASSERT(target);
2183     Element* element = is<Element>(*target) ? downcast<Element>(target) : target->parentElement();
2184     for (; element; element = element->parentElement()) {
2185         SpaceSplitString keywords(element->fastGetAttribute(webkitdropzoneAttr), true);
2186         bool matched = false;
2187         DragOperation dragOperation = DragOperationNone;
2188         for (unsigned i = 0, size = keywords.size(); i < size; ++i) {
2189             DragOperation op = convertDropZoneOperationToDragOperation(keywords[i]);
2190             if (op != DragOperationNone) {
2191                 if (dragOperation == DragOperationNone)
2192                     dragOperation = op;
2193             } else
2194                 matched = matched || hasDropZoneType(*dataTransfer, keywords[i].string());
2195             if (matched && dragOperation != DragOperationNone)
2196                 break;
2197         }
2198         if (matched) {
2199             dataTransfer->setDropEffect(convertDragOperationToDropZoneOperation(dragOperation));
2200             return true;
2201         }
2202     }
2203     return false;
2204 }
2205     
2206 bool EventHandler::updateDragAndDrop(const PlatformMouseEvent& event, DataTransfer* dataTransfer)
2207 {
2208     bool accept = false;
2209
2210     if (!m_frame.view())
2211         return false;
2212
2213     HitTestRequest request(HitTestRequest::ReadOnly | HitTestRequest::DisallowShadowContent);
2214     MouseEventWithHitTestResults mouseEvent = prepareMouseEvent(request, event);
2215
2216     RefPtr<Element> newTarget;
2217     if (Node* targetNode = mouseEvent.targetNode()) {
2218         // Drag events should never go to non-element nodes (following IE, and proper mouseover/out dispatch)
2219         if (!is<Element>(*targetNode))
2220             newTarget = targetNode->parentOrShadowHostElement();
2221         else
2222             newTarget = downcast<Element>(targetNode);
2223     }
2224
2225     m_autoscrollController->updateDragAndDrop(newTarget.get(), event.position(), event.timestamp());
2226
2227     if (m_dragTarget != newTarget) {
2228         // FIXME: this ordering was explicitly chosen to match WinIE. However,
2229         // it is sometimes incorrect when dragging within subframes, as seen with
2230         // LayoutTests/fast/events/drag-in-frames.html.
2231         //
2232         // 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>.
2233         Frame* targetFrame;
2234         if (targetIsFrame(newTarget.get(), targetFrame)) {
2235             if (targetFrame)
2236                 accept = targetFrame->eventHandler().updateDragAndDrop(event, dataTransfer);
2237         } else if (newTarget) {
2238             // 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.
2239             if (dragState().source && dragState().shouldDispatchEvents) {
2240                 // for now we don't care if event handler cancels default behavior, since there is none
2241                 dispatchDragSrcEvent(eventNames().dragEvent, event);
2242             }
2243             accept = dispatchDragEvent(eventNames().dragenterEvent, *newTarget, event, dataTransfer);
2244             if (!accept)
2245                 accept = findDropZone(newTarget.get(), dataTransfer);
2246         }
2247
2248         if (targetIsFrame(m_dragTarget.get(), targetFrame)) {
2249             if (targetFrame)
2250                 accept = targetFrame->eventHandler().updateDragAndDrop(event, dataTransfer);
2251         } else if (m_dragTarget)
2252             dispatchDragEvent(eventNames().dragleaveEvent, *m_dragTarget, event, dataTransfer);
2253
2254         if (newTarget) {
2255             // We do not explicitly call dispatchDragEvent here because it could ultimately result in the appearance that
2256             // two dragover events fired. So, we mark that we should only fire a dragover event on the next call to this function.
2257             m_shouldOnlyFireDragOverEvent = true;
2258         }
2259     } else {
2260         Frame* targetFrame;
2261         if (targetIsFrame(newTarget.get(), targetFrame)) {
2262             if (targetFrame)
2263                 accept = targetFrame->eventHandler().updateDragAndDrop(event, dataTransfer);
2264         } else if (newTarget) {
2265             // Note, when dealing with sub-frames, we may need to fire only a dragover event as a drag event may have been fired earlier.
2266             if (!m_shouldOnlyFireDragOverEvent && dragState().source && dragState().shouldDispatchEvents) {
2267                 // for now we don't care if event handler cancels default behavior, since there is none
2268                 dispatchDragSrcEvent(eventNames().dragEvent, event);
2269             }
2270             accept = dispatchDragEvent(eventNames().dragoverEvent, *newTarget, event, dataTransfer);
2271             if (!accept)
2272                 accept = findDropZone(newTarget.get(), dataTransfer);
2273             m_shouldOnlyFireDragOverEvent = false;
2274         }
2275     }
2276     m_dragTarget = newTarget.release();
2277     return accept;
2278 }
2279
2280 void EventHandler::cancelDragAndDrop(const PlatformMouseEvent& event, DataTransfer* dataTransfer)
2281 {
2282     Frame* targetFrame;
2283     if (targetIsFrame(m_dragTarget.get(), targetFrame)) {
2284         if (targetFrame)
2285             targetFrame->eventHandler().cancelDragAndDrop(event, dataTransfer);
2286     } else if (m_dragTarget) {
2287         if (dragState().source && dragState().shouldDispatchEvents)
2288             dispatchDragSrcEvent(eventNames().dragEvent, event);
2289         dispatchDragEvent(eventNames().dragleaveEvent, *m_dragTarget, event, dataTransfer);
2290     }
2291     clearDragState();
2292 }
2293
2294 bool EventHandler::performDragAndDrop(const PlatformMouseEvent& event, DataTransfer* dataTransfer)
2295 {
2296     Frame* targetFrame;
2297     bool preventedDefault = false;
2298     if (targetIsFrame(m_dragTarget.get(), targetFrame)) {
2299         if (targetFrame)
2300             preventedDefault = targetFrame->eventHandler().performDragAndDrop(event, dataTransfer);
2301     } else if (m_dragTarget)
2302         preventedDefault = dispatchDragEvent(eventNames().dropEvent, *m_dragTarget, event, dataTransfer);
2303     clearDragState();
2304     return preventedDefault;
2305 }
2306
2307 void EventHandler::clearDragState()
2308 {
2309     stopAutoscrollTimer();
2310     m_dragTarget = nullptr;
2311     m_capturingMouseEventsElement = nullptr;
2312     m_shouldOnlyFireDragOverEvent = false;
2313 #if PLATFORM(COCOA)
2314     m_sendingEventToSubview = false;
2315 #endif
2316 }
2317 #endif // ENABLE(DRAG_SUPPORT)
2318
2319 void EventHandler::setCapturingMouseEventsElement(PassRefPtr<Element> element)
2320 {
2321     m_capturingMouseEventsElement = element;
2322     m_eventHandlerWillResetCapturingMouseEventsElement = false;
2323 }
2324
2325 MouseEventWithHitTestResults EventHandler::prepareMouseEvent(const HitTestRequest& request, const PlatformMouseEvent& mouseEvent)
2326 {
2327     ASSERT(m_frame.document());
2328     return m_frame.document()->prepareMouseEvent(request, documentPointForWindowPoint(m_frame, mouseEvent.position()), mouseEvent);
2329 }
2330
2331 static RenderElement* nearestCommonHoverAncestor(RenderElement* obj1, RenderElement* obj2)
2332 {
2333     if (!obj1 || !obj2)
2334         return nullptr;
2335
2336     for (RenderElement* currObj1 = obj1; currObj1; currObj1 = currObj1->hoverAncestor()) {
2337         for (RenderElement* currObj2 = obj2; currObj2; currObj2 = currObj2->hoverAncestor()) {
2338             if (currObj1 == currObj2)
2339                 return currObj1;
2340         }
2341     }
2342
2343     return nullptr;
2344 }
2345
2346 static bool hierarchyHasCapturingEventListeners(Element* element, const AtomicString& eventName)
2347 {
2348     for (ContainerNode* curr = element; curr; curr = curr->parentOrShadowHostNode()) {
2349         if (curr->hasCapturingEventListeners(eventName))
2350             return true;
2351     }
2352     return false;
2353 }
2354
2355 void EventHandler::updateMouseEventTargetNode(Node* targetNode, const PlatformMouseEvent& platformMouseEvent, bool fireMouseOverOut)
2356 {
2357     Element* targetElement = nullptr;
2358     
2359     // If we're capturing, we always go right to that element.
2360     if (m_capturingMouseEventsElement)
2361         targetElement = m_capturingMouseEventsElement.get();
2362     else if (targetNode) {
2363         // If the target node is a non-element, dispatch on the parent. <rdar://problem/4196646>
2364         if (!is<Element>(*targetNode))
2365             targetElement = targetNode->parentOrShadowHostElement();
2366         else
2367             targetElement = downcast<Element>(targetNode);
2368     }
2369
2370     m_elementUnderMouse = targetElement;
2371
2372     // Fire mouseout/mouseover if the mouse has shifted to a different node.
2373     if (fireMouseOverOut) {
2374         auto scrollableAreaForLastNode = enclosingScrollableArea(m_lastElementUnderMouse.get());
2375         auto scrollableAreaForNodeUnderMouse = enclosingScrollableArea(m_elementUnderMouse.get());
2376         Page* page = m_frame.page();
2377
2378         if (m_lastElementUnderMouse && (!m_elementUnderMouse || &m_elementUnderMouse->document() != m_frame.document())) {
2379             // The mouse has moved between frames.
2380             if (Frame* frame = m_lastElementUnderMouse->document().frame()) {
2381                 if (FrameView* frameView = frame->view())
2382                     frameView->mouseExitedContentArea();
2383             }
2384         } else if (page && (scrollableAreaForLastNode && (!scrollableAreaForNodeUnderMouse || scrollableAreaForNodeUnderMouse != scrollableAreaForLastNode))) {
2385             // The mouse has moved between layers.
2386             if (Frame* frame = m_lastElementUnderMouse->document().frame()) {
2387                 if (FrameView* frameView = frame->view()) {
2388                     if (frameView->containsScrollableArea(scrollableAreaForLastNode))
2389                         scrollableAreaForLastNode->mouseExitedContentArea();
2390                 }
2391             }
2392         }
2393
2394         if (m_elementUnderMouse && (!m_lastElementUnderMouse || &m_lastElementUnderMouse->document() != m_frame.document())) {
2395             // The mouse has moved between frames.
2396             if (Frame* frame = m_elementUnderMouse->document().frame()) {
2397                 if (FrameView* frameView = frame->view())
2398                     frameView->mouseEnteredContentArea();
2399             }
2400         } else if (page && (scrollableAreaForNodeUnderMouse && (!scrollableAreaForLastNode || scrollableAreaForNodeUnderMouse != scrollableAreaForLastNode))) {
2401             // The mouse has moved between layers.
2402             if (Frame* frame = m_elementUnderMouse->document().frame()) {
2403                 if (FrameView* frameView = frame->view()) {
2404                     if (frameView->containsScrollableArea(scrollableAreaForNodeUnderMouse))
2405                         scrollableAreaForNodeUnderMouse->mouseEnteredContentArea();
2406                 }
2407             }
2408         }
2409
2410         if (m_lastElementUnderMouse && &m_lastElementUnderMouse->document() != m_frame.document()) {
2411             m_lastElementUnderMouse = nullptr;
2412             m_lastScrollbarUnderMouse = nullptr;
2413         }
2414
2415         if (m_lastElementUnderMouse != m_elementUnderMouse) {
2416             // mouseenter and mouseleave events are only dispatched if there is a capturing eventhandler on an ancestor
2417             // or a normal eventhandler on the element itself (they don't bubble).
2418             // This optimization is necessary since these events can cause O(n^2) capturing event-handler checks.
2419             bool hasCapturingMouseEnterListener = hierarchyHasCapturingEventListeners(m_elementUnderMouse.get(), eventNames().mouseenterEvent);
2420             bool hasCapturingMouseLeaveListener = hierarchyHasCapturingEventListeners(m_lastElementUnderMouse.get(), eventNames().mouseleaveEvent);
2421
2422             RenderElement* oldHoverRenderer = m_lastElementUnderMouse ? m_lastElementUnderMouse->renderer() : nullptr;
2423             RenderElement* newHoverRenderer = m_elementUnderMouse ? m_elementUnderMouse->renderer() : nullptr;
2424             RenderElement* ancestor = nearestCommonHoverAncestor(oldHoverRenderer, newHoverRenderer);
2425
2426             Vector<Ref<Element>, 32> leftElementsChain;
2427             if (oldHoverRenderer) {
2428                 for (RenderElement* curr = oldHoverRenderer; curr && curr != ancestor; curr = curr->hoverAncestor()) {
2429                     if (Element* element = curr->element())
2430                         leftElementsChain.append(*element);
2431                 }
2432             } else {
2433                 // If the old hovered element is not null but it's renderer is, it was probably detached.
2434                 // In this case, the old hovered element (and its ancestors) must be updated, to ensure it's normal style is re-applied.
2435                 for (Element* element = m_lastElementUnderMouse.get(); element; element = element->parentElement())
2436                     leftElementsChain.append(*element);
2437             }
2438
2439             Vector<Ref<Element>, 32> enteredElementsChain;
2440             const Element* ancestorElement = ancestor ? ancestor->element() : nullptr;
2441             for (RenderElement* curr = newHoverRenderer; curr; curr = curr->hoverAncestor()) {
2442                 if (Element *element = curr->element()) {
2443                     if (element == ancestorElement)
2444                         break;
2445                     enteredElementsChain.append(*element);
2446                 }
2447             }
2448
2449             // Send mouseout event to the old node.
2450             if (m_lastElementUnderMouse)
2451                 m_lastElementUnderMouse->dispatchMouseEvent(platformMouseEvent, eventNames().mouseoutEvent, 0, m_elementUnderMouse.get());
2452
2453             // Send mouseleave to the node hierarchy no longer under the mouse.
2454             for (auto& chain : leftElementsChain) {
2455                 if (hasCapturingMouseLeaveListener || chain->hasEventListeners(eventNames().mouseleaveEvent))
2456                     chain->dispatchMouseEvent(platformMouseEvent, eventNames().mouseleaveEvent, 0, m_elementUnderMouse.get());
2457             }
2458
2459             // Send mouseover event to the new node.
2460             if (m_elementUnderMouse)
2461                 m_elementUnderMouse->dispatchMouseEvent(platformMouseEvent, eventNames().mouseoverEvent, 0, m_lastElementUnderMouse.get());
2462
2463             // Send mouseleave event to the nodes hierarchy under the mouse.
2464             for (auto& chain : enteredElementsChain) {
2465                 if (hasCapturingMouseEnterListener || chain->hasEventListeners(eventNames().mouseenterEvent))
2466                     chain->dispatchMouseEvent(platformMouseEvent, eventNames().mouseenterEvent, 0, m_lastElementUnderMouse.get());
2467             }
2468         }
2469         m_lastElementUnderMouse = m_elementUnderMouse;
2470     }
2471 }
2472
2473 bool EventHandler::dispatchMouseEvent(const AtomicString& eventType, Node* targetNode, bool /*cancelable*/, int clickCount, const PlatformMouseEvent& platformMouseEvent, bool setUnder)
2474 {
2475     if (FrameView* view = m_frame.view())
2476         view->disableLayerFlushThrottlingTemporarilyForInteraction();
2477
2478     updateMouseEventTargetNode(targetNode, platformMouseEvent, setUnder);
2479
2480     bool swallowEvent = false;
2481
2482     if (m_elementUnderMouse)
2483         swallowEvent = !(m_elementUnderMouse->dispatchMouseEvent(platformMouseEvent, eventType, clickCount));
2484
2485     if (!swallowEvent && eventType == eventNames().mousedownEvent) {
2486
2487         // If clicking on a frame scrollbar, do not mess up with content focus.
2488         if (FrameView* view = m_frame.view()) {
2489             if (view->scrollbarAtPoint(platformMouseEvent.position()))
2490                 return true;
2491         }
2492
2493         // The layout needs to be up to date to determine if an element is focusable.
2494         m_frame.document()->updateLayoutIgnorePendingStylesheets();
2495
2496         // Blur current focus node when a link/button is clicked; this
2497         // is expected by some sites that rely on onChange handlers running
2498         // from form fields before the button click is processed.
2499
2500         Element* element = m_elementUnderMouse.get();
2501
2502         // Walk up the DOM tree to search for an element to focus.
2503         while (element) {
2504             if (element->isMouseFocusable()) {
2505                 // To fix <rdar://problem/4895428> Can't drag selected ToDo, we don't focus a
2506                 // node on mouse down if it's selected and inside a focused node. It will be
2507                 // focused if the user does a mouseup over it, however, because the mouseup
2508                 // will set a selection inside it, which will call setFocuseNodeIfNeeded.
2509                 if (m_frame.selection().isRange()
2510                     && m_frame.selection().toNormalizedRange()->compareNode(element, IGNORE_EXCEPTION) == Range::NODE_INSIDE
2511                     && element->isDescendantOf(m_frame.document()->focusedElement()))
2512                     return true;
2513                     
2514                 break;
2515             }
2516             element = element->parentOrShadowHostElement();
2517         }
2518
2519         // Only change the focus when clicking scrollbars if it can transfered to a mouse focusable node.
2520         if ((!element || !element->isMouseFocusable()) && isInsideScrollbar(platformMouseEvent.position()))
2521             return false;
2522
2523         // If focus shift is blocked, we eat the event.  Note we should never clear swallowEvent
2524         // if the page already set it (e.g., by canceling default behavior).
2525         if (Page* page = m_frame.page()) {
2526             if (element && element->isMouseFocusable()) {
2527                 if (!page->focusController().setFocusedElement(element, &m_frame))
2528                     swallowEvent = true;
2529             } else if (!element || !element->focused()) {
2530                 if (!page->focusController().setFocusedElement(0, &m_frame))
2531                     swallowEvent = true;
2532             }
2533         }
2534     }
2535
2536     return !swallowEvent;
2537 }
2538
2539 bool EventHandler::isInsideScrollbar(const IntPoint& windowPoint) const
2540 {
2541     if (RenderView* renderView = m_frame.contentRenderer()) {
2542         HitTestRequest request(HitTestRequest::ReadOnly | HitTestRequest::DisallowShadowContent);
2543         HitTestResult result(windowPoint);
2544         renderView->hitTest(request, result);
2545         return result.scrollbar();
2546     }
2547
2548     return false;
2549 }
2550
2551 #if !PLATFORM(GTK)
2552
2553 bool EventHandler::shouldTurnVerticalTicksIntoHorizontal(const HitTestResult&, const PlatformWheelEvent&) const
2554 {
2555     return false;
2556 }
2557
2558 #endif
2559
2560 #if !PLATFORM(MAC)
2561
2562 void EventHandler::platformPrepareForWheelEvents(const PlatformWheelEvent&, const HitTestResult&, RefPtr<Element>&, RefPtr<ContainerNode>&, WeakPtr<ScrollableArea>&, bool&)
2563 {
2564 }
2565
2566 void EventHandler::platformRecordWheelEvent(const PlatformWheelEvent& event)
2567 {
2568     m_frame.mainFrame().wheelEventDeltaFilter()->updateFromDelta(FloatSize(event.deltaX(), event.deltaY()));
2569 }
2570
2571 bool EventHandler::platformCompleteWheelEvent(const PlatformWheelEvent& event, ContainerNode*, const WeakPtr<ScrollableArea>&)
2572 {
2573     // We do another check on the frame view because the event handler can run JS which results in the frame getting destroyed.
2574     FrameView* view = m_frame.view();
2575     
2576     bool didHandleEvent = view ? view->wheelEvent(event) : false;
2577     m_isHandlingWheelEvent = false;
2578     return didHandleEvent;
2579 }
2580
2581 bool EventHandler::platformCompletePlatformWidgetWheelEvent(const PlatformWheelEvent&, const Widget&, ContainerNode*)
2582 {
2583     return true;
2584 }
2585
2586 void EventHandler::platformNotifyIfEndGesture(const PlatformWheelEvent&, const WeakPtr<ScrollableArea>&)
2587 {
2588 }
2589
2590 IntPoint EventHandler::effectiveMousePositionForSelectionAutoscroll() const
2591 {
2592     return m_lastKnownMousePosition;
2593 }
2594
2595 void EventHandler::clearOrScheduleClearingLatchedStateIfNeeded(const PlatformWheelEvent&)
2596 {
2597     clearLatchedState();
2598 }
2599 #endif
2600
2601 Widget* EventHandler::widgetForEventTarget(Element* eventTarget)
2602 {
2603     if (!eventTarget)
2604         return nullptr;
2605
2606     auto* target = eventTarget->renderer();
2607     if (!is<RenderWidget>(target))
2608         return nullptr;
2609
2610     return downcast<RenderWidget>(*target).widget();
2611 }
2612
2613 static WeakPtr<Widget> widgetForElement(const Element& element)
2614 {
2615     auto target = element.renderer();
2616     if (!is<RenderWidget>(target) || !downcast<RenderWidget>(*target).widget())
2617         return { };
2618
2619     return downcast<RenderWidget>(*target).widget()->createWeakPtr();
2620 }
2621
2622 bool EventHandler::completeWidgetWheelEvent(const PlatformWheelEvent& event, const WeakPtr<Widget>& widget, const WeakPtr<ScrollableArea>& scrollableArea, ContainerNode* scrollableContainer)
2623 {
2624     m_isHandlingWheelEvent = false;
2625     
2626     // We do another check on the widget because the event handler can run JS which results in the frame getting destroyed.
2627     if (!widget)
2628         return false;
2629     
2630     if (scrollableArea)
2631         scrollableArea->setScrolledProgrammatically(false);
2632
2633     platformNotifyIfEndGesture(event, scrollableArea);
2634
2635     if (!widget->platformWidget())
2636         return true;
2637
2638     return platformCompletePlatformWidgetWheelEvent(event, *widget.get(), scrollableContainer);
2639 }
2640
2641 bool EventHandler::handleWheelEvent(const PlatformWheelEvent& event)
2642 {
2643     RenderView* renderView = m_frame.contentRenderer();
2644     if (!renderView)
2645         return false;
2646
2647     RefPtr<FrameView> protector(m_frame.view());
2648
2649     FrameView* view = m_frame.view();
2650     if (!view)
2651         return false;
2652
2653     m_isHandlingWheelEvent = true;
2654     setFrameWasScrolledByUser();
2655
2656     HitTestRequest request(HitTestRequest::ReadOnly | HitTestRequest::DisallowShadowContent);
2657     HitTestResult result(view->windowToContents(event.position()));
2658     renderView->hitTest(request, result);
2659
2660     RefPtr<Element> element = result.innerElement();
2661     RefPtr<ContainerNode> scrollableContainer;
2662     WeakPtr<ScrollableArea> scrollableArea;
2663     bool isOverWidget = result.isOverWidget();
2664     platformPrepareForWheelEvents(event, result, element, scrollableContainer, scrollableArea, isOverWidget);
2665
2666 #if PLATFORM(MAC)
2667     if (event.phase() == PlatformWheelEventPhaseNone && event.momentumPhase() == PlatformWheelEventPhaseNone)
2668         m_frame.mainFrame().resetLatchingState();
2669 #endif
2670
2671     // FIXME: It should not be necessary to do this mutation here.
2672     // Instead, the handlers should know convert vertical scrolls appropriately.
2673     PlatformWheelEvent adjustedEvent = event;
2674     if (m_baseEventType == PlatformEvent::NoType && shouldTurnVerticalTicksIntoHorizontal(result, event))
2675         adjustedEvent = event.copyTurningVerticalTicksIntoHorizontalTicks();
2676
2677     platformRecordWheelEvent(adjustedEvent);
2678
2679     if (element) {
2680         if (isOverWidget) {
2681             if (WeakPtr<Widget> widget = widgetForElement(*element)) {
2682                 if (widgetDidHandleWheelEvent(event, *widget.get()))
2683                     return completeWidgetWheelEvent(adjustedEvent, widget, scrollableArea, scrollableContainer.get());
2684             }
2685         }
2686
2687         if (!element->dispatchWheelEvent(adjustedEvent)) {
2688             m_isHandlingWheelEvent = false;
2689             if (scrollableArea && scrollableArea->isScrolledProgrammatically()) {
2690                 // Web developer is controlling scrolling, so don't attempt to latch.
2691                 clearLatchedState();
2692                 scrollableArea->setScrolledProgrammatically(false);
2693             }
2694
2695             platformNotifyIfEndGesture(adjustedEvent, scrollableArea);
2696             return true;
2697         }
2698     }
2699
2700     if (scrollableArea)
2701         scrollableArea->setScrolledProgrammatically(false);
2702
2703     bool handledEvent = platformCompleteWheelEvent(adjustedEvent, scrollableContainer.get(), scrollableArea);
2704     platformNotifyIfEndGesture(adjustedEvent, scrollableArea);
2705     return handledEvent;
2706 }
2707
2708 void EventHandler::clearLatchedState()
2709 {
2710 #if PLATFORM(MAC)
2711     m_frame.mainFrame().resetLatchingState();
2712 #endif
2713     if (auto filter = m_frame.mainFrame().wheelEventDeltaFilter())
2714         filter->endFilteringDeltas();
2715 }
2716
2717 void EventHandler::defaultWheelEventHandler(Node* startNode, WheelEvent* wheelEvent)
2718 {
2719     if (!startNode || !wheelEvent)
2720         return;
2721     
2722     FloatSize filteredPlatformDelta(wheelEvent->deltaX(), wheelEvent->deltaY());
2723     if (const PlatformWheelEvent* platformWheelEvent = wheelEvent->wheelEvent()) {
2724         filteredPlatformDelta.setWidth(platformWheelEvent->deltaX());
2725         filteredPlatformDelta.setHeight(platformWheelEvent->deltaY());
2726     }
2727     
2728 #if PLATFORM(MAC)
2729     ScrollLatchingState* latchedState = m_frame.mainFrame().latchingState();
2730     Element* stopElement = latchedState ? latchedState->previousWheelScrolledElement() : nullptr;
2731
2732     if (m_frame.mainFrame().wheelEventDeltaFilter()->isFilteringDeltas())
2733         filteredPlatformDelta = m_frame.mainFrame().wheelEventDeltaFilter()->filteredDelta();
2734 #else
2735     Element* stopElement = nullptr;
2736 #endif
2737     
2738     
2739     if (handleWheelEventInAppropriateEnclosingBox(startNode, wheelEvent, &stopElement, filteredPlatformDelta))
2740         wheelEvent->setDefaultHandled();
2741     
2742 #if PLATFORM(MAC)
2743     if (latchedState && !latchedState->wheelEventElement())
2744         latchedState->setPreviousWheelScrolledElement(stopElement);
2745 #endif
2746 }
2747
2748 #if ENABLE(CONTEXT_MENUS)
2749 bool EventHandler::sendContextMenuEvent(const PlatformMouseEvent& event)
2750 {
2751     Document* doc = m_frame.document();
2752     FrameView* view = m_frame.view();
2753     if (!view)
2754         return false;
2755
2756     // Clear mouse press state to avoid initiating a drag while context menu is up.
2757     m_mousePressed = false;
2758     bool swallowEvent;
2759     LayoutPoint viewportPos = view->windowToContents(event.position());
2760     HitTestRequest request(HitTestRequest::Active | HitTestRequest::DisallowShadowContent);
2761     MouseEventWithHitTestResults mouseEvent = doc->prepareMouseEvent(request, viewportPos, event);
2762
2763     // Do not show context menus when clicking on scrollbars.
2764     if (mouseEvent.scrollbar() || view->scrollbarAtPoint(event.position()))
2765         return false;
2766
2767     if (m_frame.editor().behavior().shouldSelectOnContextualMenuClick()
2768         && !m_frame.selection().contains(viewportPos)
2769         // FIXME: In the editable case, word selection sometimes selects content that isn't underneath the mouse.
2770         // If the selection is non-editable, we do word selection to make it easier to use the contextual menu items
2771         // available for text selections.  But only if we're above text.
2772         && (m_frame.selection().selection().isContentEditable() || (mouseEvent.targetNode() && mouseEvent.targetNode()->isTextNode()))) {
2773         m_mouseDownMayStartSelect = true; // context menu events are always allowed to perform a selection
2774         selectClosestContextualWordOrLinkFromMouseEvent(mouseEvent);
2775     }
2776
2777     swallowEvent = !dispatchMouseEvent(eventNames().contextmenuEvent, mouseEvent.targetNode(), true, 0, event, false);
2778     
2779     return swallowEvent;
2780 }
2781
2782 bool EventHandler::sendContextMenuEventForKey()
2783 {
2784     FrameView* view = m_frame.view();
2785     if (!view)
2786         return false;
2787
2788     Document* doc = m_frame.document();
2789     if (!doc)
2790         return false;
2791
2792     // Clear mouse press state to avoid initiating a drag while context menu is up.
2793     m_mousePressed = false;
2794
2795     static const int kContextMenuMargin = 1;
2796
2797 #if OS(WINDOWS)
2798     int rightAligned = ::GetSystemMetrics(SM_MENUDROPALIGNMENT);
2799 #else
2800     int rightAligned = 0;
2801 #endif
2802     IntPoint location;
2803
2804     Element* focusedElement = doc->focusedElement();
2805     const VisibleSelection& selection = m_frame.selection().selection();
2806     Position start = selection.start();
2807
2808     if (start.deprecatedNode() && (selection.rootEditableElement() || selection.isRange())) {
2809         RefPtr<Range> selectionRange = selection.toNormalizedRange();
2810         IntRect firstRect = m_frame.editor().firstRectForRange(selectionRange.get());
2811
2812         int x = rightAligned ? firstRect.maxX() : firstRect.x();
2813         // In a multiline edit, firstRect.maxY() would endup on the next line, so -1.
2814         int y = firstRect.maxY() ? firstRect.maxY() - 1 : 0;
2815         location = IntPoint(x, y);
2816     } else if (focusedElement) {
2817         RenderBoxModelObject* box = focusedElement->renderBoxModelObject();
2818         if (!box)
2819             return false;
2820         IntRect clippedRect = box->pixelSnappedAbsoluteClippedOverflowRect();
2821         location = IntPoint(clippedRect.x(), clippedRect.maxY() - 1);
2822     } else {
2823         location = IntPoint(
2824             rightAligned ? view->contentsWidth() - kContextMenuMargin : kContextMenuMargin,
2825             kContextMenuMargin);
2826     }
2827
2828     m_frame.view()->setCursor(pointerCursor());
2829
2830     IntPoint position = view->contentsToRootView(location);
2831     IntPoint globalPosition = view->hostWindow()->rootViewToScreen(IntRect(position, IntSize())).location();
2832
2833     Node* targetNode = doc->focusedElement();
2834     if (!targetNode)
2835         targetNode = doc;
2836
2837     // Use the focused node as the target for hover and active.
2838     HitTestResult result(position);
2839     result.setInnerNode(targetNode);
2840     doc->updateHoverActiveState(HitTestRequest::Active | HitTestRequest::DisallowShadowContent, result.innerElement());
2841
2842     // The contextmenu event is a mouse event even when invoked using the keyboard.
2843     // This is required for web compatibility.
2844
2845 #if OS(WINDOWS)
2846     PlatformEvent::Type eventType = PlatformEvent::MouseReleased;
2847 #else
2848     PlatformEvent::Type eventType = PlatformEvent::MousePressed;
2849 #endif
2850
2851     PlatformMouseEvent platformMouseEvent(position, globalPosition, RightButton, eventType, 1, false, false, false, false, WTF::currentTime(), ForceAtClick);
2852
2853     return !dispatchMouseEvent(eventNames().contextmenuEvent, targetNode, true, 0, platformMouseEvent, false);
2854 }
2855 #endif // ENABLE(CONTEXT_MENUS)
2856
2857 void EventHandler::scheduleHoverStateUpdate()
2858 {
2859     if (!m_hoverTimer.isActive())
2860         m_hoverTimer.startOneShot(0);
2861 }
2862
2863 #if ENABLE(CURSOR_SUPPORT)
2864 void EventHandler::scheduleCursorUpdate()
2865 {
2866     if (!m_cursorUpdateTimer.isActive())
2867         m_cursorUpdateTimer.startOneShot(cursorUpdateInterval);
2868 }
2869 #endif
2870
2871 void EventHandler::dispatchFakeMouseMoveEventSoon()
2872 {
2873 #if !ENABLE(IOS_TOUCH_EVENTS)
2874     if (m_mousePressed)
2875         return;
2876
2877     if (m_mousePositionIsUnknown)
2878         return;
2879
2880     if (Page* page = m_frame.page()) {
2881         if (!page->chrome().client().shouldDispatchFakeMouseMoveEvents())
2882             return;
2883     }
2884
2885     // If the content has ever taken longer than fakeMouseMoveShortInterval we
2886     // reschedule the timer and use a longer time. This will cause the content
2887     // to receive these moves only after the user is done scrolling, reducing
2888     // pauses during the scroll.
2889     if (m_fakeMouseMoveEventTimer.isActive())
2890         m_fakeMouseMoveEventTimer.stop();
2891     m_fakeMouseMoveEventTimer.startOneShot(m_maxMouseMovedDuration > fakeMouseMoveDurationThreshold ? fakeMouseMoveLongInterval : fakeMouseMoveShortInterval);
2892 #endif
2893 }
2894
2895 void EventHandler::dispatchFakeMouseMoveEventSoonInQuad(const FloatQuad& quad)
2896 {
2897 #if ENABLE(IOS_TOUCH_EVENTS)
2898     UNUSED_PARAM(quad);
2899 #else
2900     FrameView* view = m_frame.view();
2901     if (!view)
2902         return;
2903
2904     if (!quad.containsPoint(view->windowToContents(m_lastKnownMousePosition)))
2905         return;
2906
2907     dispatchFakeMouseMoveEventSoon();
2908 #endif
2909 }
2910
2911 #if !ENABLE(IOS_TOUCH_EVENTS)
2912 void EventHandler::cancelFakeMouseMoveEvent()
2913 {
2914     m_fakeMouseMoveEventTimer.stop();
2915 }
2916
2917 void EventHandler::fakeMouseMoveEventTimerFired()
2918 {
2919     ASSERT(!m_mousePressed);
2920
2921     FrameView* view = m_frame.view();
2922     if (!view)
2923         return;
2924
2925     if (!m_frame.page() || !m_frame.page()->isVisible() || !m_frame.page()->focusController().isActive())
2926         return;
2927
2928     bool shiftKey;
2929     bool ctrlKey;
2930     bool altKey;
2931     bool metaKey;
2932     PlatformKeyboardEvent::getCurrentModifierState(shiftKey, ctrlKey, altKey, metaKey);
2933     PlatformMouseEvent fakeMouseMoveEvent(m_lastKnownMousePosition, m_lastKnownMouseGlobalPosition, NoButton, PlatformEvent::MouseMoved, 0, shiftKey, ctrlKey, altKey, metaKey, currentTime(), 0);
2934     mouseMoved(fakeMouseMoveEvent);
2935 }
2936 #endif // !ENABLE(IOS_TOUCH_EVENTS)
2937
2938 void EventHandler::setResizingFrameSet(HTMLFrameSetElement* frameSet)
2939 {
2940     m_frameSetBeingResized = frameSet;
2941 }
2942
2943 void EventHandler::resizeLayerDestroyed()
2944 {
2945     ASSERT(m_resizeLayer);
2946     m_resizeLayer = nullptr;
2947 }
2948
2949 void EventHandler::hoverTimerFired()
2950 {
2951     m_hoverTimer.stop();
2952
2953     ASSERT(m_frame.document());
2954
2955     if (RenderView* renderView = m_frame.contentRenderer()) {
2956         if (FrameView* view = m_frame.view()) {
2957             HitTestRequest request(HitTestRequest::Move | HitTestRequest::DisallowShadowContent);
2958             HitTestResult result(view->windowToContents(m_lastKnownMousePosition));
2959             renderView->hitTest(request, result);
2960             m_frame.document()->updateHoverActiveState(request, result.innerElement());
2961         }
2962     }
2963 }
2964
2965 bool EventHandler::handleAccessKey(const PlatformKeyboardEvent& event)
2966 {
2967     // FIXME: Ignoring the state of Shift key is what neither IE nor Firefox do.
2968     // IE matches lower and upper case access keys regardless of Shift key state - but if both upper and
2969     // lower case variants are present in a document, the correct element is matched based on Shift key state.
2970     // Firefox only matches an access key if Shift is not pressed, and does that case-insensitively.
2971     ASSERT(!(accessKeyModifiers() & PlatformEvent::ShiftKey));
2972     if ((event.modifiers() & ~PlatformEvent::ShiftKey) != accessKeyModifiers())
2973         return false;
2974     Element* element = m_frame.document()->getElementByAccessKey(event.unmodifiedText());
2975     if (!element)
2976         return false;
2977     element->accessKeyAction(false);
2978     return true;
2979 }
2980
2981 #if !PLATFORM(MAC)
2982 bool EventHandler::needsKeyboardEventDisambiguationQuirks() const
2983 {
2984     return false;
2985 }
2986 #endif
2987
2988 #if ENABLE(FULLSCREEN_API)
2989 bool EventHandler::isKeyEventAllowedInFullScreen(const PlatformKeyboardEvent& keyEvent) const
2990 {
2991     Document* document = m_frame.document();
2992     if (document->webkitFullScreenKeyboardInputAllowed())
2993         return true;
2994
2995     if (keyEvent.type() == PlatformKeyboardEvent::Char) {
2996         if (keyEvent.text().length() != 1)
2997             return false;
2998         UChar character = keyEvent.text()[0];
2999         return character == ' ';
3000     }
3001
3002     int keyCode = keyEvent.windowsVirtualKeyCode();
3003     return (keyCode >= VK_BACK && keyCode <= VK_CAPITAL)
3004         || (keyCode >= VK_SPACE && keyCode <= VK_DELETE)
3005         || (keyCode >= VK_OEM_1 && keyCode <= VK_OEM_PLUS)
3006         || (keyCode >= VK_MULTIPLY && keyCode <= VK_OEM_8);
3007 }
3008 #endif
3009
3010 bool EventHandler::keyEvent(const PlatformKeyboardEvent& initialKeyEvent)
3011 {
3012     RefPtr<FrameView> protector(m_frame.view());
3013
3014 #if ENABLE(FULLSCREEN_API)
3015     if (m_frame.document()->webkitIsFullScreen() && !isKeyEventAllowedInFullScreen(initialKeyEvent))
3016         return false;
3017 #endif
3018
3019     if (initialKeyEvent.windowsVirtualKeyCode() == VK_CAPITAL) {
3020         if (auto* element = m_frame.document()->focusedElement()) {
3021             if (is<HTMLInputElement>(*element))
3022                 downcast<HTMLInputElement>(*element).capsLockStateMayHaveChanged();
3023         }
3024     }
3025
3026 #if ENABLE(PAN_SCROLLING)
3027     if (m_frame.mainFrame().eventHandler().panScrollInProgress()) {
3028         // If a key is pressed while the panScroll is in progress then we want to stop
3029         if (initialKeyEvent.type() == PlatformEvent::KeyDown || initialKeyEvent.type() == PlatformEvent::RawKeyDown)
3030             stopAutoscrollTimer();
3031
3032         // If we were in panscroll mode, we swallow the key event
3033         return true;
3034     }
3035 #endif
3036
3037     // Check for cases where we are too early for events -- possible unmatched key up
3038     // from pressing return in the location bar.
3039     RefPtr<Element> element = eventTargetElementForDocument(m_frame.document());
3040     if (!element)
3041         return false;
3042
3043     UserGestureIndicator gestureIndicator(DefinitelyProcessingUserGesture, m_frame.document());
3044     UserTypingGestureIndicator typingGestureIndicator(m_frame);
3045
3046     if (FrameView* view = m_frame.view())
3047         view->disableLayerFlushThrottlingTemporarilyForInteraction();
3048
3049     // FIXME (bug 68185): this call should be made at another abstraction layer
3050     m_frame.loader().resetMultipleFormSubmissionProtection();
3051
3052     // In IE, access keys are special, they are handled after default keydown processing, but cannot be canceled - this is hard to match.
3053     // On Mac OS X, we process them before dispatching keydown, as the default keydown handler implements Emacs key bindings, which may conflict
3054     // with access keys. Then we dispatch keydown, but suppress its default handling.
3055     // On Windows, WebKit explicitly calls handleAccessKey() instead of dispatching a keypress event for WM_SYSCHAR messages.
3056     // Other platforms currently match either Mac or Windows behavior, depending on whether they send combined KeyDown events.
3057     bool matchedAnAccessKey = false;
3058     if (initialKeyEvent.type() == PlatformEvent::KeyDown)
3059         matchedAnAccessKey = handleAccessKey(initialKeyEvent);
3060
3061     // FIXME: it would be fair to let an input method handle KeyUp events before DOM dispatch.
3062     if (initialKeyEvent.type() == PlatformEvent::KeyUp || initialKeyEvent.type() == PlatformEvent::Char)
3063         return !element->dispatchKeyEvent(initialKeyEvent);
3064
3065     bool backwardCompatibilityMode = needsKeyboardEventDisambiguationQuirks();
3066
3067     PlatformKeyboardEvent keyDownEvent = initialKeyEvent;    
3068     if (keyDownEvent.type() != PlatformEvent::RawKeyDown)
3069         keyDownEvent.disambiguateKeyDownEvent(PlatformEvent::RawKeyDown, backwardCompatibilityMode);
3070     Ref<KeyboardEvent> keydown = KeyboardEvent::create(keyDownEvent, m_frame.document()->defaultView());
3071     if (matchedAnAccessKey)
3072         keydown->setDefaultPrevented(true);
3073     keydown->setTarget(element);
3074
3075     if (initialKeyEvent.type() == PlatformEvent::RawKeyDown) {
3076         element->dispatchEvent(keydown);
3077         // If frame changed as a result of keydown dispatch, then return true to avoid sending a subsequent keypress message to the new frame.
3078         bool changedFocusedFrame = m_frame.page() && &m_frame != &m_frame.page()->focusController().focusedOrMainFrame();
3079         return keydown->defaultHandled() || keydown->defaultPrevented() || changedFocusedFrame;
3080     }
3081
3082     // Run input method in advance of DOM event handling.  This may result in the IM
3083     // modifying the page prior the keydown event, but this behaviour is necessary
3084     // in order to match IE:
3085     // 1. preventing default handling of keydown and keypress events has no effect on IM input;
3086     // 2. if an input method handles the event, its keyCode is set to 229 in keydown event.
3087     m_frame.editor().handleInputMethodKeydown(keydown.ptr());
3088     
3089     bool handledByInputMethod = keydown->defaultHandled();
3090     
3091     if (handledByInputMethod) {
3092         keyDownEvent.setWindowsVirtualKeyCode(CompositionEventKeyCode);
3093         keydown = KeyboardEvent::create(keyDownEvent, m_frame.document()->defaultView());
3094         keydown->setTarget(element);
3095         keydown->setDefaultHandled();
3096     }
3097     
3098     if (accessibilityPreventsEventPropogation(keydown))
3099         keydown->stopPropagation();
3100
3101     element->dispatchEvent(keydown);
3102     // If frame changed as a result of keydown dispatch, then return early to avoid sending a subsequent keypress message to the new frame.
3103     bool changedFocusedFrame = m_frame.page() && &m_frame != &m_frame.page()->focusController().focusedOrMainFrame();
3104     bool keydownResult = keydown->defaultHandled() || keydown->defaultPrevented() || changedFocusedFrame;
3105     if (handledByInputMethod || (keydownResult && !backwardCompatibilityMode))
3106         return keydownResult;
3107     
3108     // Focus may have changed during keydown handling, so refetch element.
3109     // But if we are dispatching a fake backward compatibility keypress, then we pretend that the keypress happened on the original element.
3110     if (!keydownResult) {
3111         element = eventTargetElementForDocument(m_frame.document());
3112         if (!element)
3113             return false;
3114     }
3115
3116     PlatformKeyboardEvent keyPressEvent = initialKeyEvent;
3117     keyPressEvent.disambiguateKeyDownEvent(PlatformEvent::Char, backwardCompatibilityMode);
3118     if (keyPressEvent.text().isEmpty())
3119         return keydownResult;
3120     Ref<KeyboardEvent> keypress = KeyboardEvent::create(keyPressEvent, m_frame.document()->defaultView());
3121     keypress->setTarget(element);
3122     if (keydownResult)
3123         keypress->setDefaultPrevented(true);
3124 #if PLATFORM(COCOA)
3125     keypress->keypressCommands() = keydown->keypressCommands();
3126 #endif
3127     element->dispatchEvent(keypress);
3128
3129     return keydownResult || keypress->defaultPrevented() || keypress->defaultHandled();
3130 }
3131
3132 static FocusDirection focusDirectionForKey(const AtomicString& keyIdentifier)
3133 {
3134     static NeverDestroyed<AtomicString> Down("Down", AtomicString::ConstructFromLiteral);
3135     static NeverDestroyed<AtomicString> Up("Up", AtomicString::ConstructFromLiteral);
3136     static NeverDestroyed<AtomicString> Left("Left", AtomicString::ConstructFromLiteral);
3137     static NeverDestroyed<AtomicString> Right("Right", AtomicString::ConstructFromLiteral);
3138
3139     FocusDirection retVal = FocusDirectionNone;
3140
3141     if (keyIdentifier == Down)
3142         retVal = FocusDirectionDown;
3143     else if (keyIdentifier == Up)
3144         retVal = FocusDirectionUp;
3145     else if (keyIdentifier == Left)
3146         retVal = FocusDirectionLeft;
3147     else if (keyIdentifier == Right)
3148         retVal = FocusDirectionRight;
3149
3150     return retVal;
3151 }
3152
3153 static void setInitialKeyboardSelection(Frame& frame, SelectionDirection direction)
3154 {
3155     Document* document = frame.document();
3156     if (!document)
3157         return;
3158
3159     FrameSelection& selection = frame.selection();
3160
3161     if (!selection.isNone())
3162         return;
3163
3164     Element* focusedElement = document->focusedElement();
3165     VisiblePosition visiblePosition;
3166
3167     switch (direction) {
3168     case DirectionBackward:
3169     case DirectionLeft:
3170         if (focusedElement)
3171             visiblePosition = VisiblePosition(positionBeforeNode(focusedElement));
3172         else
3173             visiblePosition = endOfDocument(document);
3174         break;
3175     case DirectionForward:
3176     case DirectionRight:
3177         if (focusedElement)
3178             visiblePosition = VisiblePosition(positionAfterNode(focusedElement));
3179         else
3180             visiblePosition = startOfDocument(document);
3181         break;
3182     }
3183
3184     AXTextStateChangeIntent intent(AXTextStateChangeTypeSelectionMove, AXTextSelection { AXTextSelectionDirectionDiscontiguous, AXTextSelectionGranularityUnknown, false });
3185     selection.setSelection(visiblePosition, FrameSelection::defaultSetSelectionOptions(UserTriggered), intent);
3186 }
3187
3188 static void handleKeyboardSelectionMovement(Frame& frame, KeyboardEvent* event)
3189 {
3190     if (!event)
3191         return;
3192
3193     FrameSelection& selection = frame.selection();
3194
3195     bool isCommanded = event->getModifierState("Meta");
3196     bool isOptioned = event->getModifierState("Alt");
3197     bool isSelection = !selection.isNone();
3198
3199     FrameSelection::EAlteration alternation = event->getModifierState("Shift") ? FrameSelection::AlterationExtend : FrameSelection::AlterationMove;
3200     SelectionDirection direction = DirectionForward;
3201     TextGranularity granularity = CharacterGranularity;
3202
3203     switch (focusDirectionForKey(event->keyIdentifier())) {
3204     case FocusDirectionNone:
3205         return;
3206     case FocusDirectionForward:
3207     case FocusDirectionBackward:
3208         ASSERT_NOT_REACHED();
3209         return;
3210     case FocusDirectionUp:
3211         direction = DirectionBackward;
3212         granularity = isCommanded ? DocumentBoundary : LineGranularity;
3213         break;
3214     case FocusDirectionDown:
3215         direction = DirectionForward;
3216         granularity = isCommanded ? DocumentBoundary : LineGranularity;
3217         break;
3218     case FocusDirectionLeft:
3219         direction = DirectionLeft;
3220         granularity = (isCommanded) ? LineBoundary : (isOptioned) ? WordGranularity : CharacterGranularity;
3221         break;
3222     case FocusDirectionRight:
3223         direction = DirectionRight;
3224         granularity = (isCommanded) ? LineBoundary : (isOptioned) ? WordGranularity : CharacterGranularity;
3225         break;
3226     }
3227
3228     if (isSelection)
3229         selection.modify(alternation, direction, granularity, UserTriggered);
3230     else
3231         setInitialKeyboardSelection(frame, direction);
3232
3233     event->setDefaultHandled();
3234 }
3235
3236 void EventHandler::handleKeyboardSelectionMovementForAccessibility(KeyboardEvent* event)
3237 {
3238     if (event->type() == eventNames().keydownEvent) {
3239         if (AXObjectCache::accessibilityEnhancedUserInterfaceEnabled())
3240             handleKeyboardSelectionMovement(m_frame, event);
3241     }
3242 }
3243
3244 bool EventHandler::accessibilityPreventsEventPropogation(KeyboardEvent& event)
3245 {
3246 #if PLATFORM(COCOA)
3247     if (!AXObjectCache::accessibilityEnhancedUserInterfaceEnabled())
3248         return false;
3249
3250     if (!m_frame.settings().preventKeyboardDOMEventDispatch())
3251         return false;
3252
3253     // Check for key events that are relevant to accessibility: tab and arrows keys that change focus
3254     if (event.keyIdentifier() == "U+0009")
3255         return true;
3256     FocusDirection direction = focusDirectionForKey(event.keyIdentifier());
3257     if (direction != FocusDirectionNone)
3258         return true;
3259 #else
3260     UNUSED_PARAM(event);
3261 #endif
3262     return false;
3263 }
3264
3265 void EventHandler::defaultKeyboardEventHandler(KeyboardEvent* event)
3266 {
3267     if (event->type() == eventNames().keydownEvent) {
3268         m_frame.editor().handleKeyboardEvent(event);
3269         if (event->defaultHandled())
3270             return;
3271         if (event->keyIdentifier() == "U+0009")
3272             defaultTabEventHandler(event);
3273         else if (event->keyIdentifier() == "U+0008")
3274             defaultBackspaceEventHandler(event);
3275         else {
3276             FocusDirection direction = focusDirectionForKey(event->keyIdentifier());
3277             if (direction != FocusDirectionNone)
3278                 defaultArrowEventHandler(direction, event);
3279         }
3280
3281         handleKeyboardSelectionMovementForAccessibility(event);
3282     }
3283     if (event->type() == eventNames().keypressEvent) {
3284         m_frame.editor().handleKeyboardEvent(event);
3285         if (event->defaultHandled())
3286             return;
3287         if (event->charCode() == ' ')
3288             defaultSpaceEventHandler(event);
3289     }
3290 }
3291
3292 #if ENABLE(DRAG_SUPPORT)
3293 bool EventHandler::dragHysteresisExceeded(const IntPoint& floatDragViewportLocation) const
3294 {
3295     FloatPoint dragViewportLocation(floatDragViewportLocation.x(), floatDragViewportLocation.y());
3296     return dragHysteresisExceeded(dragViewportLocation);
3297 }
3298
3299 bool EventHandler::dragHysteresisExceeded(const FloatPoint& dragViewportLocation) const
3300 {
3301     int threshold = GeneralDragHysteresis;
3302     switch (dragState().type) {
3303     case DragSourceActionSelection:
3304         threshold = TextDragHysteresis;
3305         break;
3306     case DragSourceActionImage:
3307 #if ENABLE(ATTACHMENT_ELEMENT)
3308     case DragSourceActionAttachment:
3309 #endif
3310         threshold = ImageDragHysteresis;
3311         break;
3312     case DragSourceActionLink:
3313         threshold = LinkDragHysteresis;
3314         break;
3315     case DragSourceActionDHTML:
3316         break;
3317     case DragSourceActionNone:
3318     case DragSourceActionAny:
3319         ASSERT_NOT_REACHED();
3320     }
3321     
3322     return mouseMovementExceedsThreshold(dragViewportLocation, threshold);
3323 }
3324     
3325 void EventHandler::freeDataTransfer()
3326 {
3327     if (!dragState().dataTransfer)
3328         return;
3329     dragState().dataTransfer->setAccessPolicy(DataTransferAccessPolicy::Numb);
3330     dragState().dataTransfer = nullptr;
3331 }
3332
3333 void EventHandler::dragSourceEndedAt(const PlatformMouseEvent& event, DragOperation operation)
3334 {
3335     // Send a hit test request so that RenderLayer gets a chance to update the :hover and :active pseudoclasses.
3336     HitTestRequest request(HitTestRequest::Release | HitTestRequest::DisallowShadowContent);
3337     prepareMouseEvent(request, event);
3338
3339     if (dragState().source && dragState().shouldDispatchEvents) {
3340         dragState().dataTransfer->setDestinationOperation(operation);
3341         // For now we don't care if event handler cancels default behavior, since there is no default behavior.
3342         dispatchDragSrcEvent(eventNames().dragendEvent, event);
3343     }
3344     freeDataTransfer();
3345     dragState().source = nullptr;
3346     // In case the drag was ended due to an escape key press we need to ensure
3347     // that consecutive mousemove events don't reinitiate the drag and drop.
3348     m_mouseDownMayStartDrag = false;
3349 }
3350
3351 void EventHandler::updateDragStateAfterEditDragIfNeeded(Element* rootEditableElement)
3352 {
3353     // If inserting the dragged contents removed the drag source, we still want to fire dragend at the root editable element.
3354     if (dragState().source && !dragState().source->inDocument())
3355         dragState().source = rootEditableElement;
3356 }
3357
3358 // Return value indicates if we should continue "default processing", i.e., whether event handler canceled.
3359 bool EventHandler::dispatchDragSrcEvent(const AtomicString& eventType, const PlatformMouseEvent& event)
3360 {
3361     return !dispatchDragEvent(eventType, *dragState().source, event, dragState().dataTransfer.get());
3362 }
3363     
3364 static bool ExactlyOneBitSet(DragSourceAction n)
3365 {
3366     return n && !(n & (n - 1));
3367 }
3368
3369 bool EventHandler::handleDrag(const MouseEventWithHitTestResults& event, CheckDragHysteresis checkDragHysteresis)
3370 {
3371     if (event.event().button() != LeftButton || event.event().type() != PlatformEvent::MouseMoved) {
3372         // If we allowed the other side of the bridge to handle a drag
3373         // last time, then m_mousePressed might still be set. So we
3374         // clear it now to make sure the next move after a drag
3375         // doesn't look like a drag.
3376         m_mousePressed = false;
3377         return false;
3378     }
3379     
3380     if (eventLoopHandleMouseDragged(event))
3381         return true;
3382     
3383     // Careful that the drag starting logic stays in sync with eventMayStartDrag()
3384     
3385     if (m_mouseDownMayStartDrag && !dragState().source) {
3386         dragState().shouldDispatchEvents = (updateDragSourceActionsAllowed() & DragSourceActionDHTML);
3387
3388         // try to find an element that wants to be dragged
3389         HitTestRequest request(HitTestRequest::ReadOnly | HitTestRequest::DisallowShadowContent);
3390         HitTestResult result(m_mouseDownPos);
3391         m_frame.contentRenderer()->hitTest(request, result);
3392         if (m_frame.page())
3393             dragState().source = m_frame.page()->dragController().draggableElement(&m_frame, result.innerElement(), m_mouseDownPos, dragState());
3394         
3395         if (!dragState().source)
3396             m_mouseDownMayStartDrag = false; // no element is draggable
3397         else
3398             m_dragMayStartSelectionInstead = (dragState().type & DragSourceActionSelection);
3399     }
3400     
3401     // For drags starting in the selection, the user must wait between the mousedown and mousedrag,
3402     // or else we bail on the dragging stuff and allow selection to occur
3403     if (m_mouseDownMayStartDrag && m_dragMayStartSelectionInstead && (dragState().type & DragSourceActionSelection) && event.event().timestamp() - m_mouseDownTimestamp < TextDragDelay) {
3404         ASSERT(event.event().type() == PlatformEvent::MouseMoved);
3405         if ((dragState().type & DragSourceActionImage)) {
3406             // ... unless the mouse is over an image, then we start dragging just the image
3407             dragState().type = DragSourceActionImage;
3408         } else if (!(dragState().type & (DragSourceActionDHTML | DragSourceActionLink))) {
3409             // ... but only bail if we're not over an unselectable element.
3410             m_mouseDownMayStartDrag = false;
3411             dragState().source = nullptr;
3412             // ... but if this was the first click in the window, we don't even want to start selection
3413             if (eventActivatedView(event.event()))
3414                 m_mouseDownMayStartSelect = false;
3415         } else {
3416             // Prevent the following case from occuring:
3417             // 1. User starts a drag immediately after mouse down over an unselectable element.
3418             // 2. We enter this block and decided that since we're over an unselectable element, don't cancel the drag.
3419             // 3. The drag gets resolved as a potential selection drag below /but/ we haven't exceeded the drag hysteresis yet.
3420             // 4. We enter this block again, and since it's now marked as a selection drag, we cancel the drag.
3421             m_dragMayStartSelectionInstead = false;
3422         }
3423     }
3424     
3425     if (!m_mouseDownMayStartDrag)
3426         return !mouseDownMayStartSelect() && !m_mouseDownMayStartAutoscroll;
3427     
3428     if (!ExactlyOneBitSet(dragState().type)) {
3429         ASSERT((dragState().type & DragSourceActionSelection));
3430 #if ENABLE(ATTACHMENT_ELEMENT)
3431         ASSERT((dragState().type & ~DragSourceActionSelection) == DragSourceActionDHTML
3432                || (dragState().type & ~DragSourceActionSelection) == DragSourceActionImage
3433                || (dragState().type & ~DragSourceActionSelection) == DragSourceActionAttachment
3434                || (dragState().type & ~DragSourceActionSelection) == DragSourceActionLink);
3435 #else
3436         ASSERT((dragState().type & ~DragSourceActionSelection) == DragSourceActionDHTML
3437             || (dragState().type & ~DragSourceActionSelection) == DragSourceActionImage
3438             || (dragState().type & ~DragSourceActionSelection) == DragSourceActionLink);
3439 #endif
3440         dragState().type = DragSourceActionSelection;
3441     }
3442
3443     // We are starting a text/image/url drag, so the cursor should be an arrow
3444     if (FrameView* view = m_frame.view()) {
3445         // FIXME <rdar://7577595>: Custom cursors aren't supported during drag and drop (default to pointer).
3446         view->setCursor(pointerCursor());
3447     }
3448
3449     if (checkDragHysteresis == ShouldCheckDragHysteresis && !dragHysteresisExceeded(event.event().position()))
3450         return true;
3451     
3452     // Once we're past the hysteresis point, we don't want to treat this gesture as a click
3453     invalidateClick();
3454     
3455     DragOperation srcOp = DragOperationNone;      
3456     
3457     // This does work only if we missed a dragEnd. Do it anyway, just to make sure the old dataTransfer gets numbed.
3458     freeDataTransfer();
3459
3460     dragState().dataTransfer = createDraggingDataTransfer();
3461     
3462     if (dragState().shouldDispatchEvents) {
3463         // Check to see if the is a DOM based drag. If it is, get the DOM specified drag image and offset.
3464         if (dragState().type == DragSourceActionDHTML) {
3465             if (RenderObject* renderer = dragState().source->renderer()) {
3466                 // FIXME: This doesn't work correctly with transforms.
3467                 FloatPoint absPos = renderer->localToAbsolute();
3468                 IntSize delta = m_mouseDownPos - roundedIntPoint(absPos);
3469                 dragState().dataTransfer->setDragImage(dragState().source.get(), delta.width(), delta.height());
3470             } else {
3471                 // The renderer has disappeared, this can happen if the onStartDrag handler has hidden
3472                 // the element in some way.  In this case we just kill the drag.
3473                 m_mouseDownMayStartDrag = false;
3474                 goto cleanupDrag;
3475             }
3476         } 
3477         
3478         m_mouseDownMayStartDrag = dispatchDragSrcEvent(eventNames().dragstartEvent, m_mouseDown)
3479             && !m_frame.selection().selection().isInPasswordField();
3480         
3481         // Invalidate dataTransfer here against anymore pasteboard writing for security. The drag
3482         // image can still be changed as we drag, but not the pasteboard data.
3483         dragState().dataTransfer->setAccessPolicy(DataTransferAccessPolicy::ImageWritable);
3484         
3485         if (m_mouseDownMayStartDrag) {
3486             // Gather values from DHTML element, if it set any.
3487             srcOp = dragState().dataTransfer->sourceOperation();
3488             
3489             // Yuck, a draggedImage:moveTo: message can be fired as a result of kicking off the
3490             // drag with dragImage! Because of that dumb reentrancy, we may think we've not
3491             // started the drag when that happens. So we have to assume it's started before we kick it off.
3492             dragState().dataTransfer->setDragHasStarted();
3493         }
3494     }
3495     
3496     if (m_mouseDownMayStartDrag) {
3497         Page* page = m_frame.page();
3498         m_didStartDrag = page && page->dragController().startDrag(m_frame, dragState(), srcOp, event.event(), m_mouseDownPos);
3499         // In WebKit2 we could re-enter this code and start another drag.
3500         // On OS X this causes problems with the ownership of the pasteboard and the promised types.
3501         if (m_didStartDrag) {
3502             m_mouseDownMayStartDrag = false;
3503             return true;
3504         }
3505         if (dragState().source && dragState().shouldDispatchEvents) {
3506             // Drag was canned at the last minute. We owe dragSource a dragend event.
3507             dispatchDragSrcEvent(eventNames().dragendEvent, event.event());
3508             m_mouseDownMayStartDrag = false;
3509         }
3510     } 
3511
3512 cleanupDrag:
3513     if (!m_mouseDownMayStartDrag) {
3514         // Something failed to start the drag, clean up.
3515         freeDataTransfer();
3516         dragState().source = nullptr;
3517     }
3518     
3519     // No more default handling (like selection), whether we're past the hysteresis bounds or not
3520     return true;
3521 }
3522 #endif // ENABLE(DRAG_SUPPORT)
3523
3524 bool EventHandler::mouseMovementExceedsThreshold(const FloatPoint& viewportLocation, int pointsThreshold) const
3525 {
3526     FrameView* view = m_frame.view();
3527     if (!view)
3528         return false;
3529     IntPoint location = view->windowToContents(flooredIntPoint(viewportLocation));
3530     IntSize delta = location - m_mouseDownPos;
3531     
3532     return abs(delta.width()) >= pointsThreshold || abs(delta.height()) >= pointsThreshold;
3533 }
3534
3535 bool EventHandler::handleTextInputEvent(const String& text, Event* underlyingEvent, TextEventInputType inputType)
3536 {
3537     // Platforms should differentiate real commands like selectAll from text input in disguise (like insertNewline),
3538     // and avoid dispatching text input events from keydown default handlers.
3539     ASSERT(!is<KeyboardEvent>(underlyingEvent) || downcast<KeyboardEvent>(*underlyingEvent).type() == eventNames().keypressEvent);
3540
3541     EventTarget* target;
3542     if (underlyingEvent)
3543         target = underlyingEvent->target();
3544     else
3545