bc1b40e61c206240dac4f53d4570176b5ded3181
[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>&, 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*, 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&, 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 static Widget* widgetForElement(const Element& element)
2602 {
2603     RenderElement* target = element.renderer();
2604     if (!target)
2605         return nullptr;
2606
2607     if (!is<RenderWidget>(target))
2608         return nullptr;
2609
2610     return downcast<RenderWidget>(*target).widget();
2611 }
2612
2613 bool EventHandler::handleWheelEvent(const PlatformWheelEvent& event)
2614 {
2615     RenderView* renderView = m_frame.contentRenderer();
2616     if (!renderView)
2617         return false;
2618
2619     RefPtr<FrameView> protector(m_frame.view());
2620
2621     FrameView* view = m_frame.view();
2622     if (!view)
2623         return false;
2624
2625     m_isHandlingWheelEvent = true;
2626     setFrameWasScrolledByUser();
2627
2628     HitTestRequest request(HitTestRequest::ReadOnly | HitTestRequest::DisallowShadowContent);
2629     HitTestResult result(view->windowToContents(event.position()));
2630     renderView->hitTest(request, result);
2631
2632     RefPtr<Element> element = result.innerElement();
2633     RefPtr<ContainerNode> scrollableContainer;
2634     ScrollableArea* scrollableArea = nullptr;
2635     bool isOverWidget = result.isOverWidget();
2636     platformPrepareForWheelEvents(event, result, element, scrollableContainer, scrollableArea, isOverWidget);
2637
2638 #if PLATFORM(MAC)
2639     if (event.phase() == PlatformWheelEventPhaseNone && event.momentumPhase() == PlatformWheelEventPhaseNone)
2640         m_frame.mainFrame().resetLatchingState();
2641 #endif
2642
2643     // FIXME: It should not be necessary to do this mutation here.
2644     // Instead, the handlers should know convert vertical scrolls appropriately.
2645     PlatformWheelEvent adjustedEvent = event;
2646     if (m_baseEventType == PlatformEvent::NoType && shouldTurnVerticalTicksIntoHorizontal(result, event))
2647         adjustedEvent = event.copyTurningVerticalTicksIntoHorizontalTicks();
2648
2649     platformRecordWheelEvent(adjustedEvent);
2650
2651     if (element) {
2652         if (isOverWidget) {
2653             Widget* widget = widgetForElement(*element);
2654             if (widget && passWheelEventToWidget(event, *widget)) {
2655                 m_isHandlingWheelEvent = false;
2656
2657                 // We do another check on the widget because the event handler can run JS which results in the frame getting destroyed.
2658                 Widget* widget = widgetForElement(*element);
2659                 if (!widget)
2660                     return false;
2661
2662                 if (scrollableArea)
2663                     scrollableArea->setScrolledProgrammatically(false);
2664                 platformNotifyIfEndGesture(adjustedEvent, scrollableArea);
2665                 if (!widget->platformWidget())
2666                     return true;
2667                 return platformCompletePlatformWidgetWheelEvent(event, *widget, scrollableContainer.get());
2668             }
2669         }
2670
2671         if (!element->dispatchWheelEvent(adjustedEvent)) {
2672             m_isHandlingWheelEvent = false;
2673             if (scrollableArea && scrollableArea->isScrolledProgrammatically()) {
2674                 // Web developer is controlling scrolling, so don't attempt to latch.
2675                 clearLatchedState();
2676                 scrollableArea->setScrolledProgrammatically(false);
2677             }
2678
2679             platformNotifyIfEndGesture(adjustedEvent, scrollableArea);
2680             return true;
2681         }
2682     }
2683
2684     if (scrollableArea)
2685         scrollableArea->setScrolledProgrammatically(false);
2686
2687     bool handledEvent = platformCompleteWheelEvent(adjustedEvent, scrollableContainer.get(), scrollableArea);
2688     platformNotifyIfEndGesture(adjustedEvent, scrollableArea);
2689     return handledEvent;
2690 }
2691
2692 void EventHandler::clearLatchedState()
2693 {
2694 #if PLATFORM(MAC)
2695     m_frame.mainFrame().resetLatchingState();
2696 #endif
2697     if (auto filter = m_frame.mainFrame().wheelEventDeltaFilter())
2698         filter->endFilteringDeltas();
2699 }
2700
2701 void EventHandler::defaultWheelEventHandler(Node* startNode, WheelEvent* wheelEvent)
2702 {
2703     if (!startNode || !wheelEvent)
2704         return;
2705     
2706     FloatSize filteredPlatformDelta(wheelEvent->deltaX(), wheelEvent->deltaY());
2707     if (const PlatformWheelEvent* platformWheelEvent = wheelEvent->wheelEvent()) {
2708         filteredPlatformDelta.setWidth(platformWheelEvent->deltaX());
2709         filteredPlatformDelta.setHeight(platformWheelEvent->deltaY());
2710     }
2711     
2712 #if PLATFORM(MAC)
2713     ScrollLatchingState* latchedState = m_frame.mainFrame().latchingState();
2714     Element* stopElement = latchedState ? latchedState->previousWheelScrolledElement() : nullptr;
2715
2716     if (m_frame.mainFrame().wheelEventDeltaFilter()->isFilteringDeltas())
2717         filteredPlatformDelta = m_frame.mainFrame().wheelEventDeltaFilter()->filteredDelta();
2718 #else
2719     Element* stopElement = nullptr;
2720 #endif
2721     
2722     
2723     if (handleWheelEventInAppropriateEnclosingBox(startNode, wheelEvent, &stopElement, filteredPlatformDelta))
2724         wheelEvent->setDefaultHandled();
2725     
2726 #if PLATFORM(MAC)
2727     if (latchedState && !latchedState->wheelEventElement())
2728         latchedState->setPreviousWheelScrolledElement(stopElement);
2729 #endif
2730 }
2731
2732 #if ENABLE(CONTEXT_MENUS)
2733 bool EventHandler::sendContextMenuEvent(const PlatformMouseEvent& event)
2734 {
2735     Document* doc = m_frame.document();
2736     FrameView* view = m_frame.view();
2737     if (!view)
2738         return false;
2739
2740     // Clear mouse press state to avoid initiating a drag while context menu is up.
2741     m_mousePressed = false;
2742     bool swallowEvent;
2743     LayoutPoint viewportPos = view->windowToContents(event.position());
2744     HitTestRequest request(HitTestRequest::Active | HitTestRequest::DisallowShadowContent);
2745     MouseEventWithHitTestResults mouseEvent = doc->prepareMouseEvent(request, viewportPos, event);
2746
2747     // Do not show context menus when clicking on scrollbars.
2748     if (mouseEvent.scrollbar() || view->scrollbarAtPoint(event.position()))
2749         return false;
2750
2751     if (m_frame.editor().behavior().shouldSelectOnContextualMenuClick()
2752         && !m_frame.selection().contains(viewportPos)
2753         // FIXME: In the editable case, word selection sometimes selects content that isn't underneath the mouse.
2754         // If the selection is non-editable, we do word selection to make it easier to use the contextual menu items
2755         // available for text selections.  But only if we're above text.
2756         && (m_frame.selection().selection().isContentEditable() || (mouseEvent.targetNode() && mouseEvent.targetNode()->isTextNode()))) {
2757         m_mouseDownMayStartSelect = true; // context menu events are always allowed to perform a selection
2758         selectClosestContextualWordOrLinkFromMouseEvent(mouseEvent);
2759     }
2760
2761     swallowEvent = !dispatchMouseEvent(eventNames().contextmenuEvent, mouseEvent.targetNode(), true, 0, event, false);
2762     
2763     return swallowEvent;
2764 }
2765
2766 bool EventHandler::sendContextMenuEventForKey()
2767 {
2768     FrameView* view = m_frame.view();
2769     if (!view)
2770         return false;
2771
2772     Document* doc = m_frame.document();
2773     if (!doc)
2774         return false;
2775
2776     // Clear mouse press state to avoid initiating a drag while context menu is up.
2777     m_mousePressed = false;
2778
2779     static const int kContextMenuMargin = 1;
2780
2781 #if OS(WINDOWS)
2782     int rightAligned = ::GetSystemMetrics(SM_MENUDROPALIGNMENT);
2783 #else
2784     int rightAligned = 0;
2785 #endif
2786     IntPoint location;
2787
2788     Element* focusedElement = doc->focusedElement();
2789     const VisibleSelection& selection = m_frame.selection().selection();
2790     Position start = selection.start();
2791
2792     if (start.deprecatedNode() && (selection.rootEditableElement() || selection.isRange())) {
2793         RefPtr<Range> selectionRange = selection.toNormalizedRange();
2794         IntRect firstRect = m_frame.editor().firstRectForRange(selectionRange.get());
2795
2796         int x = rightAligned ? firstRect.maxX() : firstRect.x();
2797         // In a multiline edit, firstRect.maxY() would endup on the next line, so -1.
2798         int y = firstRect.maxY() ? firstRect.maxY() - 1 : 0;
2799         location = IntPoint(x, y);
2800     } else if (focusedElement) {
2801         RenderBoxModelObject* box = focusedElement->renderBoxModelObject();
2802         if (!box)
2803             return false;
2804         IntRect clippedRect = box->pixelSnappedAbsoluteClippedOverflowRect();
2805         location = IntPoint(clippedRect.x(), clippedRect.maxY() - 1);
2806     } else {
2807         location = IntPoint(
2808             rightAligned ? view->contentsWidth() - kContextMenuMargin : kContextMenuMargin,
2809             kContextMenuMargin);
2810     }
2811
2812     m_frame.view()->setCursor(pointerCursor());
2813
2814     IntPoint position = view->contentsToRootView(location);
2815     IntPoint globalPosition = view->hostWindow()->rootViewToScreen(IntRect(position, IntSize())).location();
2816
2817     Node* targetNode = doc->focusedElement();
2818     if (!targetNode)
2819         targetNode = doc;
2820
2821     // Use the focused node as the target for hover and active.
2822     HitTestResult result(position);
2823     result.setInnerNode(targetNode);
2824     doc->updateHoverActiveState(HitTestRequest::Active | HitTestRequest::DisallowShadowContent, result.innerElement());
2825
2826     // The contextmenu event is a mouse event even when invoked using the keyboard.
2827     // This is required for web compatibility.
2828
2829 #if OS(WINDOWS)
2830     PlatformEvent::Type eventType = PlatformEvent::MouseReleased;
2831 #else
2832     PlatformEvent::Type eventType = PlatformEvent::MousePressed;
2833 #endif
2834
2835     PlatformMouseEvent platformMouseEvent(position, globalPosition, RightButton, eventType, 1, false, false, false, false, WTF::currentTime(), ForceAtClick);
2836
2837     return !dispatchMouseEvent(eventNames().contextmenuEvent, targetNode, true, 0, platformMouseEvent, false);
2838 }
2839 #endif // ENABLE(CONTEXT_MENUS)
2840
2841 void EventHandler::scheduleHoverStateUpdate()
2842 {
2843     if (!m_hoverTimer.isActive())
2844         m_hoverTimer.startOneShot(0);
2845 }
2846
2847 #if ENABLE(CURSOR_SUPPORT)
2848 void EventHandler::scheduleCursorUpdate()
2849 {
2850     if (!m_cursorUpdateTimer.isActive())
2851         m_cursorUpdateTimer.startOneShot(cursorUpdateInterval);
2852 }
2853 #endif
2854
2855 void EventHandler::dispatchFakeMouseMoveEventSoon()
2856 {
2857 #if !ENABLE(IOS_TOUCH_EVENTS)
2858     if (m_mousePressed)
2859         return;
2860
2861     if (m_mousePositionIsUnknown)
2862         return;
2863
2864     if (Page* page = m_frame.page()) {
2865         if (!page->chrome().client().shouldDispatchFakeMouseMoveEvents())
2866             return;
2867     }
2868
2869     // If the content has ever taken longer than fakeMouseMoveShortInterval we
2870     // reschedule the timer and use a longer time. This will cause the content
2871     // to receive these moves only after the user is done scrolling, reducing
2872     // pauses during the scroll.
2873     if (m_fakeMouseMoveEventTimer.isActive())
2874         m_fakeMouseMoveEventTimer.stop();
2875     m_fakeMouseMoveEventTimer.startOneShot(m_maxMouseMovedDuration > fakeMouseMoveDurationThreshold ? fakeMouseMoveLongInterval : fakeMouseMoveShortInterval);
2876 #endif
2877 }
2878
2879 void EventHandler::dispatchFakeMouseMoveEventSoonInQuad(const FloatQuad& quad)
2880 {
2881 #if ENABLE(IOS_TOUCH_EVENTS)
2882     UNUSED_PARAM(quad);
2883 #else
2884     FrameView* view = m_frame.view();
2885     if (!view)
2886         return;
2887
2888     if (!quad.containsPoint(view->windowToContents(m_lastKnownMousePosition)))
2889         return;
2890
2891     dispatchFakeMouseMoveEventSoon();
2892 #endif
2893 }
2894
2895 #if !ENABLE(IOS_TOUCH_EVENTS)
2896 void EventHandler::cancelFakeMouseMoveEvent()
2897 {
2898     m_fakeMouseMoveEventTimer.stop();
2899 }
2900
2901 void EventHandler::fakeMouseMoveEventTimerFired()
2902 {
2903     ASSERT(!m_mousePressed);
2904
2905     FrameView* view = m_frame.view();
2906     if (!view)
2907         return;
2908
2909     if (!m_frame.page() || !m_frame.page()->isVisible() || !m_frame.page()->focusController().isActive())
2910         return;
2911
2912     bool shiftKey;
2913     bool ctrlKey;
2914     bool altKey;
2915     bool metaKey;
2916     PlatformKeyboardEvent::getCurrentModifierState(shiftKey, ctrlKey, altKey, metaKey);
2917     PlatformMouseEvent fakeMouseMoveEvent(m_lastKnownMousePosition, m_lastKnownMouseGlobalPosition, NoButton, PlatformEvent::MouseMoved, 0, shiftKey, ctrlKey, altKey, metaKey, currentTime(), 0);
2918     mouseMoved(fakeMouseMoveEvent);
2919 }
2920 #endif // !ENABLE(IOS_TOUCH_EVENTS)
2921
2922 void EventHandler::setResizingFrameSet(HTMLFrameSetElement* frameSet)
2923 {
2924     m_frameSetBeingResized = frameSet;
2925 }
2926
2927 void EventHandler::resizeLayerDestroyed()
2928 {
2929     ASSERT(m_resizeLayer);
2930     m_resizeLayer = nullptr;
2931 }
2932
2933 void EventHandler::hoverTimerFired()
2934 {
2935     m_hoverTimer.stop();
2936
2937     ASSERT(m_frame.document());
2938
2939     if (RenderView* renderView = m_frame.contentRenderer()) {
2940         if (FrameView* view = m_frame.view()) {
2941             HitTestRequest request(HitTestRequest::Move | HitTestRequest::DisallowShadowContent);
2942             HitTestResult result(view->windowToContents(m_lastKnownMousePosition));
2943             renderView->hitTest(request, result);
2944             m_frame.document()->updateHoverActiveState(request, result.innerElement());
2945         }
2946     }
2947 }
2948
2949 bool EventHandler::handleAccessKey(const PlatformKeyboardEvent& event)
2950 {
2951     // FIXME: Ignoring the state of Shift key is what neither IE nor Firefox do.
2952     // IE matches lower and upper case access keys regardless of Shift key state - but if both upper and
2953     // lower case variants are present in a document, the correct element is matched based on Shift key state.
2954     // Firefox only matches an access key if Shift is not pressed, and does that case-insensitively.
2955     ASSERT(!(accessKeyModifiers() & PlatformEvent::ShiftKey));
2956     if ((event.modifiers() & ~PlatformEvent::ShiftKey) != accessKeyModifiers())
2957         return false;
2958     Element* element = m_frame.document()->getElementByAccessKey(event.unmodifiedText());
2959     if (!element)
2960         return false;
2961     element->accessKeyAction(false);
2962     return true;
2963 }
2964
2965 #if !PLATFORM(MAC)
2966 bool EventHandler::needsKeyboardEventDisambiguationQuirks() const
2967 {
2968     return false;
2969 }
2970 #endif
2971
2972 #if ENABLE(FULLSCREEN_API)
2973 bool EventHandler::isKeyEventAllowedInFullScreen(const PlatformKeyboardEvent& keyEvent) const
2974 {
2975     Document* document = m_frame.document();
2976     if (document->webkitFullScreenKeyboardInputAllowed())
2977         return true;
2978
2979     if (keyEvent.type() == PlatformKeyboardEvent::Char) {
2980         if (keyEvent.text().length() != 1)
2981             return false;
2982         UChar character = keyEvent.text()[0];
2983         return character == ' ';
2984     }
2985
2986     int keyCode = keyEvent.windowsVirtualKeyCode();
2987     return (keyCode >= VK_BACK && keyCode <= VK_CAPITAL)
2988         || (keyCode >= VK_SPACE && keyCode <= VK_DELETE)
2989         || (keyCode >= VK_OEM_1 && keyCode <= VK_OEM_PLUS)
2990         || (keyCode >= VK_MULTIPLY && keyCode <= VK_OEM_8);
2991 }
2992 #endif
2993
2994 bool EventHandler::keyEvent(const PlatformKeyboardEvent& initialKeyEvent)
2995 {
2996     RefPtr<FrameView> protector(m_frame.view());
2997
2998 #if ENABLE(FULLSCREEN_API)
2999     if (m_frame.document()->webkitIsFullScreen() && !isKeyEventAllowedInFullScreen(initialKeyEvent))
3000         return false;
3001 #endif
3002
3003     if (initialKeyEvent.windowsVirtualKeyCode() == VK_CAPITAL) {
3004         if (auto* element = m_frame.document()->focusedElement()) {
3005             if (is<HTMLInputElement>(*element))
3006                 downcast<HTMLInputElement>(*element).capsLockStateMayHaveChanged();
3007         }
3008     }
3009
3010 #if ENABLE(PAN_SCROLLING)
3011     if (m_frame.mainFrame().eventHandler().panScrollInProgress()) {
3012         // If a key is pressed while the panScroll is in progress then we want to stop
3013         if (initialKeyEvent.type() == PlatformEvent::KeyDown || initialKeyEvent.type() == PlatformEvent::RawKeyDown)
3014             stopAutoscrollTimer();
3015
3016         // If we were in panscroll mode, we swallow the key event
3017         return true;
3018     }
3019 #endif
3020
3021     // Check for cases where we are too early for events -- possible unmatched key up
3022     // from pressing return in the location bar.
3023     RefPtr<Element> element = eventTargetElementForDocument(m_frame.document());
3024     if (!element)
3025         return false;
3026
3027     UserGestureIndicator gestureIndicator(DefinitelyProcessingUserGesture, m_frame.document());
3028     UserTypingGestureIndicator typingGestureIndicator(m_frame);
3029
3030     if (FrameView* view = m_frame.view())
3031         view->disableLayerFlushThrottlingTemporarilyForInteraction();
3032
3033     // FIXME (bug 68185): this call should be made at another abstraction layer
3034     m_frame.loader().resetMultipleFormSubmissionProtection();
3035
3036     // In IE, access keys are special, they are handled after default keydown processing, but cannot be canceled - this is hard to match.
3037     // On Mac OS X, we process them before dispatching keydown, as the default keydown handler implements Emacs key bindings, which may conflict
3038     // with access keys. Then we dispatch keydown, but suppress its default handling.
3039     // On Windows, WebKit explicitly calls handleAccessKey() instead of dispatching a keypress event for WM_SYSCHAR messages.
3040     // Other platforms currently match either Mac or Windows behavior, depending on whether they send combined KeyDown events.
3041     bool matchedAnAccessKey = false;
3042     if (initialKeyEvent.type() == PlatformEvent::KeyDown)
3043         matchedAnAccessKey = handleAccessKey(initialKeyEvent);
3044
3045     // FIXME: it would be fair to let an input method handle KeyUp events before DOM dispatch.
3046     if (initialKeyEvent.type() == PlatformEvent::KeyUp || initialKeyEvent.type() == PlatformEvent::Char)
3047         return !element->dispatchKeyEvent(initialKeyEvent);
3048
3049     bool backwardCompatibilityMode = needsKeyboardEventDisambiguationQuirks();
3050
3051     PlatformKeyboardEvent keyDownEvent = initialKeyEvent;    
3052     if (keyDownEvent.type() != PlatformEvent::RawKeyDown)
3053         keyDownEvent.disambiguateKeyDownEvent(PlatformEvent::RawKeyDown, backwardCompatibilityMode);
3054     Ref<KeyboardEvent> keydown = KeyboardEvent::create(keyDownEvent, m_frame.document()->defaultView());
3055     if (matchedAnAccessKey)
3056         keydown->setDefaultPrevented(true);
3057     keydown->setTarget(element);
3058
3059     if (initialKeyEvent.type() == PlatformEvent::RawKeyDown) {
3060         element->dispatchEvent(keydown);
3061         // If frame changed as a result of keydown dispatch, then return true to avoid sending a subsequent keypress message to the new frame.
3062         bool changedFocusedFrame = m_frame.page() && &m_frame != &m_frame.page()->focusController().focusedOrMainFrame();
3063         return keydown->defaultHandled() || keydown->defaultPrevented() || changedFocusedFrame;
3064     }
3065
3066     // Run input method in advance of DOM event handling.  This may result in the IM
3067     // modifying the page prior the keydown event, but this behaviour is necessary
3068     // in order to match IE:
3069     // 1. preventing default handling of keydown and keypress events has no effect on IM input;
3070     // 2. if an input method handles the event, its keyCode is set to 229 in keydown event.
3071     m_frame.editor().handleInputMethodKeydown(keydown.ptr());
3072     
3073     bool handledByInputMethod = keydown->defaultHandled();
3074     
3075     if (handledByInputMethod) {
3076         keyDownEvent.setWindowsVirtualKeyCode(CompositionEventKeyCode);
3077         keydown = KeyboardEvent::create(keyDownEvent, m_frame.document()->defaultView());
3078         keydown->setTarget(element);
3079         keydown->setDefaultHandled();
3080     }
3081     
3082     if (accessibilityPreventsEventPropogation(keydown))
3083         keydown->stopPropagation();
3084
3085     element->dispatchEvent(keydown);
3086     // If frame changed as a result of keydown dispatch, then return early to avoid sending a subsequent keypress message to the new frame.
3087     bool changedFocusedFrame = m_frame.page() && &m_frame != &m_frame.page()->focusController().focusedOrMainFrame();
3088     bool keydownResult = keydown->defaultHandled() || keydown->defaultPrevented() || changedFocusedFrame;
3089     if (handledByInputMethod || (keydownResult && !backwardCompatibilityMode))
3090         return keydownResult;
3091     
3092     // Focus may have changed during keydown handling, so refetch element.
3093     // But if we are dispatching a fake backward compatibility keypress, then we pretend that the keypress happened on the original element.
3094     if (!keydownResult) {
3095         element = eventTargetElementForDocument(m_frame.document());
3096         if (!element)
3097             return false;
3098     }
3099
3100     PlatformKeyboardEvent keyPressEvent = initialKeyEvent;
3101     keyPressEvent.disambiguateKeyDownEvent(PlatformEvent::Char, backwardCompatibilityMode);
3102     if (keyPressEvent.text().isEmpty())
3103         return keydownResult;
3104     Ref<KeyboardEvent> keypress = KeyboardEvent::create(keyPressEvent, m_frame.document()->defaultView());
3105     keypress->setTarget(element);
3106     if (keydownResult)
3107         keypress->setDefaultPrevented(true);
3108 #if PLATFORM(COCOA)
3109     keypress->keypressCommands() = keydown->keypressCommands();
3110 #endif
3111     element->dispatchEvent(keypress);
3112
3113     return keydownResult || keypress->defaultPrevented() || keypress->defaultHandled();
3114 }
3115
3116 static FocusDirection focusDirectionForKey(const AtomicString& keyIdentifier)
3117 {
3118     static NeverDestroyed<AtomicString> Down("Down", AtomicString::ConstructFromLiteral);
3119     static NeverDestroyed<AtomicString> Up("Up", AtomicString::ConstructFromLiteral);
3120     static NeverDestroyed<AtomicString> Left("Left", AtomicString::ConstructFromLiteral);
3121     static NeverDestroyed<AtomicString> Right("Right", AtomicString::ConstructFromLiteral);
3122
3123     FocusDirection retVal = FocusDirectionNone;
3124
3125     if (keyIdentifier == Down)
3126         retVal = FocusDirectionDown;
3127     else if (keyIdentifier == Up)
3128         retVal = FocusDirectionUp;
3129     else if (keyIdentifier == Left)
3130         retVal = FocusDirectionLeft;
3131     else if (keyIdentifier == Right)
3132         retVal = FocusDirectionRight;
3133
3134     return retVal;
3135 }
3136
3137 static void setInitialKeyboardSelection(Frame& frame, SelectionDirection direction)
3138 {
3139     Document* document = frame.document();
3140     if (!document)
3141         return;
3142
3143     FrameSelection& selection = frame.selection();
3144
3145     if (!selection.isNone())
3146         return;
3147
3148     Element* focusedElement = document->focusedElement();
3149     VisiblePosition visiblePosition;
3150
3151     switch (direction) {
3152     case DirectionBackward:
3153     case DirectionLeft:
3154         if (focusedElement)
3155             visiblePosition = VisiblePosition(positionBeforeNode(focusedElement));
3156         else
3157             visiblePosition = endOfDocument(document);
3158         break;
3159     case DirectionForward:
3160     case DirectionRight:
3161         if (focusedElement)
3162             visiblePosition = VisiblePosition(positionAfterNode(focusedElement));
3163         else
3164             visiblePosition = startOfDocument(document);
3165         break;
3166     }
3167
3168     AXTextStateChangeIntent intent(AXTextStateChangeTypeSelectionMove, AXTextSelection { AXTextSelectionDirectionDiscontiguous, AXTextSelectionGranularityUnknown, false });
3169     selection.setSelection(visiblePosition, FrameSelection::defaultSetSelectionOptions(UserTriggered), intent);
3170 }
3171
3172 static void handleKeyboardSelectionMovement(Frame& frame, KeyboardEvent* event)
3173 {
3174     if (!event)
3175         return;
3176
3177     FrameSelection& selection = frame.selection();
3178
3179     bool isCommanded = event->getModifierState("Meta");
3180     bool isOptioned = event->getModifierState("Alt");
3181     bool isSelection = !selection.isNone();
3182
3183     FrameSelection::EAlteration alternation = event->getModifierState("Shift") ? FrameSelection::AlterationExtend : FrameSelection::AlterationMove;
3184     SelectionDirection direction = DirectionForward;
3185     TextGranularity granularity = CharacterGranularity;
3186
3187     switch (focusDirectionForKey(event->keyIdentifier())) {
3188     case FocusDirectionNone:
3189         return;
3190     case FocusDirectionForward:
3191     case FocusDirectionBackward:
3192         ASSERT_NOT_REACHED();
3193         return;
3194     case FocusDirectionUp:
3195         direction = DirectionBackward;
3196         granularity = isCommanded ? DocumentBoundary : LineGranularity;
3197         break;
3198     case FocusDirectionDown:
3199         direction = DirectionForward;
3200         granularity = isCommanded ? DocumentBoundary : LineGranularity;
3201         break;
3202     case FocusDirectionLeft:
3203         direction = DirectionLeft;
3204         granularity = (isCommanded) ? LineBoundary : (isOptioned) ? WordGranularity : CharacterGranularity;
3205         break;
3206     case FocusDirectionRight:
3207         direction = DirectionRight;
3208         granularity = (isCommanded) ? LineBoundary : (isOptioned) ? WordGranularity : CharacterGranularity;
3209         break;
3210     }
3211
3212     if (isSelection)
3213         selection.modify(alternation, direction, granularity, UserTriggered);
3214     else
3215         setInitialKeyboardSelection(frame, direction);
3216
3217     event->setDefaultHandled();
3218 }
3219
3220 void EventHandler::handleKeyboardSelectionMovementForAccessibility(KeyboardEvent* event)
3221 {
3222     if (event->type() == eventNames().keydownEvent) {
3223         if (AXObjectCache::accessibilityEnhancedUserInterfaceEnabled())
3224             handleKeyboardSelectionMovement(m_frame, event);
3225     }
3226 }
3227
3228 bool EventHandler::accessibilityPreventsEventPropogation(KeyboardEvent& event)
3229 {
3230 #if PLATFORM(COCOA)
3231     if (!AXObjectCache::accessibilityEnhancedUserInterfaceEnabled())
3232         return false;
3233
3234     if (!m_frame.settings().preventKeyboardDOMEventDispatch())
3235         return false;
3236
3237     // Check for key events that are relevant to accessibility: tab and arrows keys that change focus
3238     if (event.keyIdentifier() == "U+0009")
3239         return true;
3240     FocusDirection direction = focusDirectionForKey(event.keyIdentifier());
3241     if (direction != FocusDirectionNone)
3242         return true;
3243 #else
3244     UNUSED_PARAM(event);
3245 #endif
3246     return false;
3247 }
3248
3249 void EventHandler::defaultKeyboardEventHandler(KeyboardEvent* event)
3250 {
3251     if (event->type() == eventNames().keydownEvent) {
3252         m_frame.editor().handleKeyboardEvent(event);
3253         if (event->defaultHandled())
3254             return;
3255         if (event->keyIdentifier() == "U+0009")
3256             defaultTabEventHandler(event);
3257         else if (event->keyIdentifier() == "U+0008")
3258             defaultBackspaceEventHandler(event);
3259         else {
3260             FocusDirection direction = focusDirectionForKey(event->keyIdentifier());
3261             if (direction != FocusDirectionNone)
3262                 defaultArrowEventHandler(direction, event);
3263         }
3264
3265         handleKeyboardSelectionMovementForAccessibility(event);
3266     }
3267     if (event->type() == eventNames().keypressEvent) {
3268         m_frame.editor().handleKeyboardEvent(event);
3269         if (event->defaultHandled())
3270             return;
3271         if (event->charCode() == ' ')
3272             defaultSpaceEventHandler(event);
3273     }
3274 }
3275
3276 #if ENABLE(DRAG_SUPPORT)
3277 bool EventHandler::dragHysteresisExceeded(const IntPoint& floatDragViewportLocation) const
3278 {
3279     FloatPoint dragViewportLocation(floatDragViewportLocation.x(), floatDragViewportLocation.y());
3280     return dragHysteresisExceeded(dragViewportLocation);
3281 }
3282
3283 bool EventHandler::dragHysteresisExceeded(const FloatPoint& dragViewportLocation) const
3284 {
3285     int threshold = GeneralDragHysteresis;
3286     switch (dragState().type) {
3287     case DragSourceActionSelection:
3288         threshold = TextDragHysteresis;
3289         break;
3290     case DragSourceActionImage:
3291 #if ENABLE(ATTACHMENT_ELEMENT)
3292     case DragSourceActionAttachment:
3293 #endif
3294         threshold = ImageDragHysteresis;
3295         break;
3296     case DragSourceActionLink:
3297         threshold = LinkDragHysteresis;
3298         break;
3299     case DragSourceActionDHTML:
3300         break;
3301     case DragSourceActionNone:
3302     case DragSourceActionAny:
3303         ASSERT_NOT_REACHED();
3304     }
3305     
3306     return mouseMovementExceedsThreshold(dragViewportLocation, threshold);
3307 }
3308     
3309 void EventHandler::freeDataTransfer()
3310 {
3311     if (!dragState().dataTransfer)
3312         return;
3313     dragState().dataTransfer->setAccessPolicy(DataTransferAccessPolicy::Numb);
3314     dragState().dataTransfer = nullptr;
3315 }
3316
3317 void EventHandler::dragSourceEndedAt(const PlatformMouseEvent& event, DragOperation operation)
3318 {
3319     // Send a hit test request so that RenderLayer gets a chance to update the :hover and :active pseudoclasses.
3320     HitTestRequest request(HitTestRequest::Release | HitTestRequest::DisallowShadowContent);
3321     prepareMouseEvent(request, event);
3322
3323     if (dragState().source && dragState().shouldDispatchEvents) {
3324         dragState().dataTransfer->setDestinationOperation(operation);
3325         // For now we don't care if event handler cancels default behavior, since there is no default behavior.
3326         dispatchDragSrcEvent(eventNames().dragendEvent, event);
3327     }
3328     freeDataTransfer();
3329     dragState().source = nullptr;
3330     // In case the drag was ended due to an escape key press we need to ensure
3331     // that consecutive mousemove events don't reinitiate the drag and drop.
3332     m_mouseDownMayStartDrag = false;
3333 }
3334
3335 void EventHandler::updateDragStateAfterEditDragIfNeeded(Element* rootEditableElement)
3336 {
3337     // If inserting the dragged contents removed the drag source, we still want to fire dragend at the root editable element.
3338     if (dragState().source && !dragState().source->inDocument())
3339         dragState().source = rootEditableElement;
3340 }
3341
3342 // Return value indicates if we should continue "default processing", i.e., whether event handler canceled.
3343 bool EventHandler::dispatchDragSrcEvent(const AtomicString& eventType, const PlatformMouseEvent& event)
3344 {
3345     return !dispatchDragEvent(eventType, *dragState().source, event, dragState().dataTransfer.get());
3346 }
3347     
3348 static bool ExactlyOneBitSet(DragSourceAction n)
3349 {
3350     return n && !(n & (n - 1));
3351 }
3352
3353 bool EventHandler::handleDrag(const MouseEventWithHitTestResults& event, CheckDragHysteresis checkDragHysteresis)
3354 {
3355     if (event.event().button() != LeftButton || event.event().type() != PlatformEvent::MouseMoved) {
3356         // If we allowed the other side of the bridge to handle a drag
3357         // last time, then m_mousePressed might still be set. So we
3358         // clear it now to make sure the next move after a drag
3359         // doesn't look like a drag.
3360         m_mousePressed = false;
3361         return false;
3362     }
3363     
3364     if (eventLoopHandleMouseDragged(event))
3365         return true;
3366     
3367     // Careful that the drag starting logic stays in sync with eventMayStartDrag()
3368     
3369     if (m_mouseDownMayStartDrag && !dragState().source) {
3370         dragState().shouldDispatchEvents = (updateDragSourceActionsAllowed() & DragSourceActionDHTML);
3371
3372         // try to find an element that wants to be dragged
3373         HitTestRequest request(HitTestRequest::ReadOnly | HitTestRequest::DisallowShadowContent);
3374         HitTestResult result(m_mouseDownPos);
3375         m_frame.contentRenderer()->hitTest(request, result);
3376         if (m_frame.page())
3377             dragState().source = m_frame.page()->dragController().draggableElement(&m_frame, result.innerElement(), m_mouseDownPos, dragState());
3378         
3379         if (!dragState().source)
3380             m_mouseDownMayStartDrag = false; // no element is draggable
3381         else
3382             m_dragMayStartSelectionInstead = (dragState().type & DragSourceActionSelection);
3383     }
3384     
3385     // For drags starting in the selection, the user must wait between the mousedown and mousedrag,
3386     // or else we bail on the dragging stuff and allow selection to occur
3387     if (m_mouseDownMayStartDrag && m_dragMayStartSelectionInstead && (dragState().type & DragSourceActionSelection) && event.event().timestamp() - m_mouseDownTimestamp < TextDragDelay) {
3388         ASSERT(event.event().type() == PlatformEvent::MouseMoved);
3389         if ((dragState().type & DragSourceActionImage)) {
3390             // ... unless the mouse is over an image, then we start dragging just the image
3391             dragState().type = DragSourceActionImage;
3392         } else if (!(dragState().type & (DragSourceActionDHTML | DragSourceActionLink))) {
3393             // ... but only bail if we're not over an unselectable element.
3394             m_mouseDownMayStartDrag = false;
3395             dragState().source = nullptr;
3396             // ... but if this was the first click in the window, we don't even want to start selection
3397             if (eventActivatedView(event.event()))
3398                 m_mouseDownMayStartSelect = false;
3399         } else {
3400             // Prevent the following case from occuring:
3401             // 1. User starts a drag immediately after mouse down over an unselectable element.
3402             // 2. We enter this block and decided that since we're over an unselectable element, don't cancel the drag.
3403             // 3. The drag gets resolved as a potential selection drag below /but/ we haven't exceeded the drag hysteresis yet.
3404             // 4. We enter this block again, and since it's now marked as a selection drag, we cancel the drag.
3405             m_dragMayStartSelectionInstead = false;
3406         }
3407     }
3408     
3409     if (!m_mouseDownMayStartDrag)
3410         return !mouseDownMayStartSelect() && !m_mouseDownMayStartAutoscroll;
3411     
3412     if (!ExactlyOneBitSet(dragState().type)) {
3413         ASSERT((dragState().type & DragSourceActionSelection));
3414 #if ENABLE(ATTACHMENT_ELEMENT)
3415         ASSERT((dragState().type & ~DragSourceActionSelection) == DragSourceActionDHTML
3416                || (dragState().type & ~DragSourceActionSelection) == DragSourceActionImage
3417                || (dragState().type & ~DragSourceActionSelection) == DragSourceActionAttachment
3418                || (dragState().type & ~DragSourceActionSelection) == DragSourceActionLink);
3419 #else
3420         ASSERT((dragState().type & ~DragSourceActionSelection) == DragSourceActionDHTML
3421             || (dragState().type & ~DragSourceActionSelection) == DragSourceActionImage
3422             || (dragState().type & ~DragSourceActionSelection) == DragSourceActionLink);
3423 #endif
3424         dragState().type = DragSourceActionSelection;
3425     }
3426
3427     // We are starting a text/image/url drag, so the cursor should be an arrow
3428     if (FrameView* view = m_frame.view()) {
3429         // FIXME <rdar://7577595>: Custom cursors aren't supported during drag and drop (default to pointer).
3430         view->setCursor(pointerCursor());
3431     }
3432
3433     if (checkDragHysteresis == ShouldCheckDragHysteresis && !dragHysteresisExceeded(event.event().position()))
3434         return true;
3435     
3436     // Once we're past the hysteresis point, we don't want to treat this gesture as a click
3437     invalidateClick();
3438     
3439     DragOperation srcOp = DragOperationNone;      
3440     
3441     // This does work only if we missed a dragEnd. Do it anyway, just to make sure the old dataTransfer gets numbed.
3442     freeDataTransfer();
3443
3444     dragState().dataTransfer = createDraggingDataTransfer();
3445     
3446     if (dragState().shouldDispatchEvents) {
3447         // Check to see if the is a DOM based drag. If it is, get the DOM specified drag image and offset.
3448         if (dragState().type == DragSourceActionDHTML) {
3449             if (RenderObject* renderer = dragState().source->renderer()) {
3450                 // FIXME: This doesn't work correctly with transforms.
3451                 FloatPoint absPos = renderer->localToAbsolute();
3452                 IntSize delta = m_mouseDownPos - roundedIntPoint(absPos);
3453                 dragState().dataTransfer->setDragImage(dragState().source.get(), delta.width(), delta.height());
3454             } else {
3455                 // The renderer has disappeared, this can happen if the onStartDrag handler has hidden
3456                 // the element in some way.  In this case we just kill the drag.
3457                 m_mouseDownMayStartDrag = false;
3458                 goto cleanupDrag;
3459             }
3460         } 
3461         
3462         m_mouseDownMayStartDrag = dispatchDragSrcEvent(eventNames().dragstartEvent, m_mouseDown)
3463             && !m_frame.selection().selection().isInPasswordField();
3464         
3465         // Invalidate dataTransfer here against anymore pasteboard writing for security. The drag
3466         // image can still be changed as we drag, but not the pasteboard data.
3467         dragState().dataTransfer->setAccessPolicy(DataTransferAccessPolicy::ImageWritable);
3468         
3469         if (m_mouseDownMayStartDrag) {
3470             // Gather values from DHTML element, if it set any.
3471             srcOp = dragState().dataTransfer->sourceOperation();
3472             
3473             // Yuck, a draggedImage:moveTo: message can be fired as a result of kicking off the
3474             // drag with dragImage! Because of that dumb reentrancy, we may think we've not
3475             // started the drag when that happens. So we have to assume it's started before we kick it off.
3476             dragState().dataTransfer->setDragHasStarted();
3477         }
3478     }
3479     
3480     if (m_mouseDownMayStartDrag) {
3481         Page* page = m_frame.page();
3482         m_didStartDrag = page && page->dragController().startDrag(m_frame, dragState(), srcOp, event.event(), m_mouseDownPos);
3483         // In WebKit2 we could re-enter this code and start another drag.
3484         // On OS X this causes problems with the ownership of the pasteboard and the promised types.
3485         if (m_didStartDrag) {
3486             m_mouseDownMayStartDrag = false;
3487             return true;
3488         }
3489         if (dragState().source && dragState().shouldDispatchEvents) {
3490             // Drag was canned at the last minute. We owe dragSource a dragend event.
3491             dispatchDragSrcEvent(eventNames().dragendEvent, event.event());
3492             m_mouseDownMayStartDrag = false;
3493         }
3494     } 
3495
3496 cleanupDrag:
3497     if (!m_mouseDownMayStartDrag) {
3498         // Something failed to start the drag, clean up.
3499         freeDataTransfer();
3500         dragState().source = nullptr;
3501     }
3502     
3503     // No more default handling (like selection), whether we're past the hysteresis bounds or not
3504     return true;
3505 }
3506 #endif // ENABLE(DRAG_SUPPORT)
3507
3508 bool EventHandler::mouseMovementExceedsThreshold(const FloatPoint& viewportLocation, int pointsThreshold) const
3509 {
3510     FrameView* view = m_frame.view();
3511     if (!view)
3512         return false;
3513     IntPoint location = view->windowToContents(flooredIntPoint(viewportLocation));
3514     IntSize delta = location - m_mouseDownPos;
3515     
3516     return abs(delta.width()) >= pointsThreshold || abs(delta.height()) >= pointsThreshold;
3517 }
3518
3519 bool EventHandler::handleTextInputEvent(const String& text, Event* underlyingEvent, TextEventInputType inputType)
3520 {
3521     // Platforms should differentiate real commands like selectAll from text input in disguise (like insertNewline),
3522     // and avoid dispatching text input events from keydown default handlers.
3523     ASSERT(!is<KeyboardEvent>(underlyingEvent) || downcast<KeyboardEvent>(*underlyingEvent).type() == eventNames().keypressEvent);
3524
3525     EventTarget* target;
3526     if (underlyingEvent)
3527         target = underlyingEvent->target();
3528     else
3529         target = eventTargetElementForDocument(m_frame.document());
3530     if (!target)
3531         return false;
3532     
3533     if (FrameView* view = m_frame.view())
3534         view->disableLayerFlushThrottlingTemporarilyForInteraction();
3535
3536     Ref<TextEvent> event = TextEvent::create(m_frame.document()->domWindow(), text, inputType);
3537     event->setUnderlyingEvent(underlyingEvent);
3538
3539     target->dispatchEvent(event);
3540     return event->defaultHandled();
3541 }
3542     
3543 bool EventHandler::isKeyboardOptionTab(KeyboardEvent* event)
3544 {
3545     return event
3546         && (event->type() == eventNames().keydownEvent || event->type() == eventNames().keypressEvent)
3547         && event->altKey()
3548         && event->keyIdentifier() == "U+0009";    
3549 }
3550
3551 bool EventHandler::eventInvertsTabsToLinksClientCallResult(KeyboardEvent* event)
3552 {
3553 #if PLATFORM(COCOA) || PLATFORM(EFL)
3554     return EventHandler::isKeyboardOptionTab(event);
3555 #else
3556     UNUSED_PARAM(event);
3557     return false;
3558 #endif
3559 }
3560
3561 bool EventHandler::tabsToLinks(KeyboardEvent* event) const
3562 {
3563     // FIXME: This function needs a better name. It can be called for keypresses other than Tab when spatial navigation is enabled.
3564
3565     Page* page = m_frame.page();
3566     if (!page)
3567         return false;
3568
3569     bool tabsToLinksClientCallResult = page->chrome().client().keyboardUIMode() & KeyboardAccessTabsToLinks;
3570     return eventInvertsTabsToLinksClientCallResult(event) ? !tabsToLinksClientCallResult : tabsToLinksClientCallResult;
3571 }
3572
3573 void EventHandler::defaultTextInputEventHandler(TextEvent* event)
3574 {
3575     if (m_frame.editor().handleTextEvent(event))
3576         event->setDefaultHandled();
3577 }
3578
3579
3580 void EventHandler::defaultSpaceEventHandler(KeyboardEvent* event)
3581 {
3582     ASSERT(event->type() == eventNames().keypressEvent);
3583
3584     if (event->ctrlKey() || event->metaKey() || event->altKey() || event->altGraphKey())
3585         return;
3586
3587     ScrollLogicalDirection direction = event->shiftKey() ? ScrollBlockDirectionBackward : ScrollBlockDirectionForward;
3588     if (logicalScrollOverflow(direction, ScrollByPage)) {
3589         event->setDefaultHandled();
3590         return;
3591     }
3592
3593     FrameView* view = m_frame.view();
3594     if (!view)
3595         return;
3596
3597     if (view->logicalScroll(direction, ScrollByPage))
3598         event->setDefaultHandled();
3599 }
3600
3601 void EventHandler::defaultBackspaceEventHandler(KeyboardEvent* event)
3602 {
3603     ASSERT(event->type() == eventNames().keydownEvent);
3604
3605     if (event->ctrlKey() || event->metaKey() || event->altKey() || event->altGraphKey())
3606         return;
3607
3608     if (!m_frame.editor().behavior().shouldNavigateBackOnBackspace())
3609         return;
3610     
3611     Page* page = m_frame.page();
3612     if (!page)
3613         return;
3614
3615     if (!m_frame.settings().backspaceKeyNavigationEnabled())
3616         return;
3617     
3618     bool handledEvent = false;
3619
3620     if (event->shiftKey())
3621         handledEvent = page->backForward().goForward();
3622     else
3623         handledEvent = page->backForward().goBack();
3624
3625     if (handledEvent)
3626         event->setDefaultHandled();
3627 }
3628
3629
3630 void EventHandler::defaultArrowEventHandler(FocusDirection focusDirection, KeyboardEvent* event)
3631 {
3632     ASSERT(event->type() == eventNames().keydownEvent);
3633
3634     if (event->ctrlKey() || event->metaKey() || event->altGraphKey() || event->shiftKey())
3635         return;
3636
3637     Page* page = m_frame.page();
3638     if (!page)
3639         return;
3640
3641     if (!isSpatialNavigationEnabled(&m_frame))
3642         return;
3643
3644     // Arrows and other possible directional navigation keys can be used in design
3645     // mode editing.
3646     if (m_frame.document()->inDesignMode())
3647         return;
3648
3649     if (page->focusController().advanceFocus(focusDirection, event))
3650         event->setDefaultHandled();
3651 }
3652
3653 void EventHandler::defaultTabEventHandler(KeyboardEvent* event)
3654 {
3655     ASSERT(event->type() == eventNames().keydownEvent);
3656
3657     // We should only advance focus on tabs if no special modifier keys are held down.
3658     if (event->ctrlKey() || event->metaKey() || event->altGraphKey())
3659         return;
3660
3661     Page* page = m_frame.page();
3662     if (!page)
3663         return;
3664     if (!page->tabKeyCyclesThroughElements())
3665         return;
3666
3667     FocusDirection focusDirection = event->shiftKey() ? FocusDirectionBackward : FocusDirectionForward;
3668
3669     // Tabs can be used in design mode editing.
3670     if (m_frame.document()->inDesignMode())
3671         return;
3672
3673     if (page->focusController().advanceFocus(focusDirection, event))
3674         event->setDefaultHandled();
3675 }
3676
3677 void EventHandler::sendScrollEvent()
3678 {
3679     setFrameWasScrolledByUser();
3680     if (m_frame.view() && m_frame.document())
3681         m_frame.document()->eventQueue().enqueueOrDispatchScrollEvent(*m_frame.document());
3682 }
3683
3684 void EventHandler::setFrameWasScrolledByUser()
3685 {
3686     FrameView* v = m_frame.view();
3687     if (v)
3688         v->setWasScrolledByUser(true);
3689 }
3690
3691 bool EventHandler::passMousePressEventToScrollbar(MouseEventWithHitTestResults& mouseEvent, Scrollbar* scrollbar)
3692 {
3693     if (!scrollbar || !scrollbar->enabled())
3694         return false;
3695     setFrameWasScrolledByUser();
3696     return scrollbar->mouseDown(mouseEvent.event());
3697 }
3698
3699 // If scrollbar (under mouse) is different from last, send a mouse exited. Set
3700 // last to scrollbar if setLast is true; else set last to nullptr.
3701 void EventHandler::updateLastScrollbarUnderMouse(Scrollbar* scrollbar, bool setLast)
3702 {
3703     if (m_lastScrollbarUnderMouse.get() != scrollbar) {
3704         // Send mouse exited to the old scrollbar.
3705         if (m_lastScrollbarUnderMouse)
3706             m_lastScrollbarUnderMouse->mouseExited();
3707
3708         // Send mouse entered if we're setting a new scrollbar.
3709         if (scrollbar && setLast)
3710             scrollbar->mouseEntered();
3711
3712         if (setLast && scrollbar)
3713             m_lastScrollbarUnderMouse = scrollbar->createWeakPtr();
3714         else
3715             m_lastScrollbarUnderMouse = nullptr;
3716     }
3717 }
3718
3719 #if ENABLE(TOUCH_EVENTS) && !ENABLE(IOS_TOUCH_EVENTS)
3720 static const AtomicString& eventNameForTouchPointState(PlatformTouchPoint::State state)
3721 {
3722     switch (state) {
3723     case PlatformTouchPoint::TouchReleased:
3724         return eventNames().touchendEvent;
3725     case PlatformTouchPoint::TouchCancelled:
3726         return eventNames().touchcancelEvent;
3727     case PlatformTouchPoint::TouchPressed:
3728         return eventNames().touchstartEvent;
3729     case PlatformTouchPoint::TouchMoved:
3730         return eventNames().touchmoveEvent;
3731     case PlatformTouchPoint::TouchStationary:
3732         // TouchStationary state is not converted to touch events, so fall through to assert.
3733     default:
3734         ASSERT_NOT_REACHED();
3735         return emptyAtom;
3736     }
3737 }
3738
3739 static HitTestResult hitTestResultInFrame(Frame* frame, const LayoutPoint& point, HitTestRequest::HitTestRequestType hitType)
3740 {
3741     HitTestResult result(point);
3742
3743     if (!frame || !frame->contentRenderer())
3744         return result;
3745
3746     if (frame->view()) {
3747         IntRect rect = frame->view()->visibleContentRect();
3748         if (!rect.contains(roundedIntPoint(point)))
3749             return result;
3750     }
3751     frame->contentRenderer()->hitTest(HitTestRequest(hitType), result);
3752     return result;
3753 }
3754
3755 bool EventHandler::handleTouchEvent(const PlatformTouchEvent& event)
3756 {
3757     // First build up the lists to use for the 'touches', 'targetTouches' and 'changedTouches' attributes
3758     // in the JS event. See http://www.sitepen.com/blog/2008/07/10/touching-and-gesturing-on-the-iphone/
3759     // for an overview of how these lists fit together.
3760
3761     // Holds the complete set of touches on the screen and will be used as the 'touches' list in the JS event.
3762     RefPtr<TouchList> touches = TouchList::create();
3763
3764     // A different view on the 'touches' list above, filtered and grouped by event target. Used for the
3765     // 'targetTouches' list in the JS event.
3766     typedef HashMap<EventTarget*, RefPtr<TouchList>> TargetTouchesMap;
3767     TargetTouchesMap touchesByTarget;
3768
3769     // Array of touches per state, used to assemble the 'changedTouches' list in the JS event.
3770     typedef HashSet<RefPtr<EventTarget>> EventTargetSet;
3771     struct {
3772         // The touches corresponding to the particular change state this struct instance represents.
3773         RefPtr<TouchList> m_touches;
3774         // Set of targets involved in m_touches.
3775         EventTargetSet m_targets;
3776     } changedTouches[PlatformTouchPoint::TouchStateEnd];
3777
3778     const Vector<PlatformTouchPoint>& points = event.touchPoints();
3779
3780     UserGestureIndicator gestureIndicator(DefinitelyProcessingUserGesture, m_frame.document());
3781
3782     bool freshTouchEvents = true;
3783     bool allTouchReleased = true;
3784     for (auto& point : points) {
3785         if (point.state() != PlatformTouchPoint::TouchPressed)
3786             freshTouchEvents = false;
3787         if (point.state() != PlatformTouchPoint::TouchReleased && point.state() != PlatformTouchPoint::TouchCancelled)
3788             allTouchReleased = false;
3789     }
3790
3791     for (auto& point : points) {
3792         PlatformTouchPoint::State pointState = point.state();
3793         LayoutPoint pagePoint = documentPointForWindowPoint(m_frame, point.pos());
3794
3795         HitTestRequest::HitTestRequestType hitType = HitTestRequest::TouchEvent;
3796         // The HitTestRequest types used for mouse events map quite adequately
3797         // to touch events. Note that in addition to meaning that the hit test
3798         // should affect the active state of the current node if necessary,
3799         // HitTestRequest::Active signifies that the hit test is taking place
3800         // with the mouse (or finger in this case) being pressed.
3801         switch (pointState) {
3802         case PlatformTouchPoint::TouchPressed:
3803             hitType |= HitTestRequest::Active;
3804             break;
3805         case PlatformTouchPoint::TouchMoved:
3806             hitType |= HitTestRequest::Active | HitTestRequest::Move | HitTestRequest::ReadOnly;
3807             break;
3808         case PlatformTouchPoint::TouchReleased:
3809         case PlatformTouchPoint::TouchCancelled:
3810             hitType |= HitTestRequest::Release;
3811             break;
3812         case PlatformTouchPoint::TouchStationary:
3813             hitType |= HitTestRequest::Active | HitTestRequest::ReadOnly;
3814             break;
3815         default:
3816             ASSERT_NOT_REACHED();
3817             break;
3818         }
3819
3820         if (shouldGesturesTriggerActive())
3821             hitType |= HitTestRequest::ReadOnly;
3822
3823         // Increment the platform touch id by 1 to avoid storing a key of 0 in the hashmap.
3824         unsigned touchPointTargetKey = point.id() + 1;
3825         RefPtr<EventTarget> touchTarget;
3826         if (pointState == PlatformTouchPoint::TouchPressed) {
3827             HitTestResult result;
3828             if (freshTouchEvents) {
3829                 result = hitTestResultAtPoint(pagePoint, hitType);
3830                 m_originatingTouchPointTargetKey = touchPointTargetKey;
3831             } else if (m_originatingTouchPointDocument.get() && m_originatingTouchPointDocument->frame()) {
3832                 LayoutPoint pagePointInOriginatingDocument = documentPointForWindowPoint(*m_originatingTouchPointDocument->frame(), point.pos());
3833                 result = hitTestResultInFrame(m_originatingTouchPointDocument->frame(), pagePointInOriginatingDocument, hitType);
3834                 if (!result.innerNode())
3835                     continue;
3836             } else
3837                 continue;
3838
3839             // FIXME: This code should use Element* instead of Node*.
3840             Node* node = result.innerElement();
3841             ASSERT(node);
3842
3843             if (node && InspectorInstrumentation::handleTouchEvent(m_frame, *node))
3844                 return true;
3845
3846             Document& doc = node->document();
3847             // Record the originating touch document even if it does not have a touch listener.
3848             if (freshTouchEvents) {
3849                 m_originatingTouchPointDocument = &doc;
3850                 freshTouchEvents = false;
3851             }
3852             if (!doc.hasTouchEventHandlers())
3853                 continue;
3854             m_originatingTouchPointTargets.set(touchPointTargetKey, node);
3855             touchTarget = node;
3856         } else if (pointState == PlatformTouchPoint::TouchReleased || pointState == PlatformTouchPoint::TouchCancelled) {
3857             // No need to perform a hit-test since we only need to unset :hover and :active states.
3858             if (!shouldGesturesTriggerActive() && allTouchReleased)
3859                 m_frame.document()->updateHoverActiveState(hitType, 0);
3860             if (touchPointTargetKey == m_originatingTouchPointTargetKey)
3861                 m_originatingTouchPointTargetKey = 0;
3862
3863             // The target should be the original target for this touch, so get it from the hashmap. As it's a release or cancel
3864             // we also remove it from the map.
3865             touchTarget = m_originatingTouchPointTargets.take(touchPointTargetKey);
3866         } else
3867             // No hittest is performed on move or stationary, since the target is not allowed to change anyway.
3868             touchTarget = m_originatingTouchPointTargets.get(touchPointTargetKey);
3869
3870         if (!touchTarget.get())
3871             continue;
3872         Document& doc = touchTarget->toNode()->document();
3873         if (!doc.hasTouchEventHandlers())
3874             continue;
3875         Frame* targetFrame = doc.frame();
3876         if (!targetFrame)
3877             continue;
3878
3879         if (&m_frame != targetFrame) {
3880             // pagePoint should always be relative to the target elements containing frame.
3881             pagePoint = documentPointForWindowPoint(*targetFrame, point.pos());
3882         }
3883
3884         float scaleFactor = targetFrame->pageZoomFactor() * targetFrame->frameScaleFactor();
3885
3886         int adjustedPageX = lroundf(pagePoint.x() / scaleFactor);
3887         int adjustedPageY = lroundf(pagePoint.y() / scaleFactor);
3888
3889         RefPtr<Touch> touch = Touch::create(targetFrame, touchTarget.get(), point.id(),
3890                                             point.screenPos().x(), point.screenPos().y(),
3891                                             adjustedPageX, adjustedPageY,
3892                                             point.radiusX(), point.radiusY(), point.rotationAngle(), point.force());
3893
3894         // Ensure this target's touch list exists, even if it ends up empty, so it can always be passed to TouchEvent::Create below.
3895         TargetTouchesMap::iterator targetTouchesIterator = touchesByTarget.find(touchTarget.get());
3896         if (targetTouchesIterator == touchesByTarget.end())
3897             targetTouchesIterator = touchesByTarget.set(touchTarget.get(), TouchList::create()).iterator;
3898
3899         // touches and targetTouches should only contain information about touches still on the screen, so if this point is
3900         // released or cancelled it will only appear in the changedTouches list.
3901         if (pointState != PlatformTouchPoint::TouchReleased && pointState != PlatformTouchPoint::TouchCancelled) {
3902             touches->append(touch);
3903             targetTouchesIterator->value->append(touch);
3904         }
3905
3906         // Now build up the correct list for changedTouches.
3907         // Note that  any touches that are in the TouchStationary state (e.g. if
3908         // the user had several points touched but did not move them all) should
3909         // never be in the changedTouches list so we do not handle them explicitly here.
3910         // See https://bugs.webkit.org/show_bug.cgi?id=37609 for further discussion
3911         // about the TouchStationary state.
3912         if (pointState != PlatformTouchPoint::TouchStationary) {
3913             ASSERT(pointState < PlatformTouchPoint::TouchStateEnd);
3914             if (!changedTouches[pointState].m_touches)
3915                 changedTouches[pointState].m_touches = TouchList::create();
3916             changedTouches[pointState].m_touches->append(touch);
3917             changedTouches[pointState].m_targets.add(touchTarget);
3918         }
3919     }
3920     m_touchPressed = touches->length() > 0;
3921     if (allTouchReleased)
3922         m_originatingTouchPointDocument = nullptr;
3923
3924     // Now iterate the changedTouches list and m_targets within it, sending events to the targets as required.
3925     bool swallowedEvent = false;
3926     RefPtr<TouchList> emptyList = TouchList::create();
3927     for (unsigned state = 0; state != PlatformTouchPoint::TouchStateEnd; ++state) {
3928         if (!changedTouches[state].m_touches)
3929             continue;
3930
3931         // When sending a touch cancel event, use empty touches and targetTouches lists.
3932         bool isTouchCancelEvent = (state == PlatformTouchPoint::TouchCancelled);
3933         RefPtr<TouchList>& effectiveTouches(isTouchCancelEvent ? emptyList : touches);
3934         const AtomicString& stateName(eventNameForTouchPointState(static_cast<PlatformTouchPoint::State>(state)));
3935
3936         for (auto& taget : changedTouches[state].m_targets) {
3937             EventTarget* touchEventTarget = taget.get();
3938             RefPtr<TouchList> targetTouches(isTouchCancelEvent ? emptyList : touchesByTarget.get(touchEventTarget));
3939             ASSERT(targetTouches);
3940
3941             Ref<TouchEvent> touchEvent =
3942                 TouchEvent::create(effectiveTouches.get(), targetTouches.get(), changedTouches[state].m_touches.get(),
3943                     stateName, touchEventTarget->toNode()->document().defaultView(),
3944                     0, 0, 0, 0, event.ctrlKey(), event.altKey(), event.shiftKey(), event.metaKey());
3945             touchEventTarget->toNode()->dispatchTouchEvent(touchEvent);
3946             swallowedEvent = swallowedEvent || touchEvent->defaultPrevented() || touchEvent->defaultHandled();
3947         }
3948     }
3949
3950     return swallowedEvent;
3951 }
3952 #endif // ENABLE(TOUCH_EVENTS) && !ENABLE(IOS_TOUCH_EVENTS)
3953
3954 #if ENABLE(TOUCH_EVENTS)
3955 bool EventHandler::dispatchSyntheticTouchEventIfEnabled(const PlatformMouseEvent& platformMouseEvent)
3956 {
3957 #if ENABLE(IOS_TOUCH_EVENTS)
3958     UNUSED_PARAM(platformMouseEvent);
3959     return false;
3960 #else
3961     if (!m_frame.settings().isTouchEventEmulationEnabled())
3962         return false;
3963
3964     PlatformEvent::Type eventType = platformMouseEvent.type();
3965     if (eventType != PlatformEvent::MouseMoved && eventType != PlatformEvent::MousePressed && eventType != PlatformEvent::MouseReleased)
3966         return false;
3967
3968     HitTestRequest request(HitTestRequest::Active | HitTestRequest::DisallowShadowContent);
3969     MouseEventWithHitTestResults mouseEvent = prepareMouseEvent(request, platformMouseEvent);
3970     if (mouseEvent.scrollbar() || subframeForHitTestResult(mouseEvent))
3971         return false;
3972
3973     // The order is important. This check should follow the subframe test: http://webkit.org/b/111292.
3974     if (eventType == PlatformEvent::MouseMoved && !m_touchPressed)
3975         return true;
3976
3977     SyntheticSingleTouchEvent touchEvent(platformMouseEvent);
3978     return handleTouchEvent(touchEvent);
3979 #endif
3980 }
3981 #endif // ENABLE(TOUCH_EVENTS)
3982
3983 void EventHandler::setLastKnownMousePosition(const PlatformMouseEvent& event)
3984 {
3985     m_mousePositionIsUnknown = false;
3986     m_lastKnownMousePosition = event.position();
3987     m_lastKnownMouseGlobalPosition = event.globalPosition();
3988 }
3989
3990 void EventHandler::setImmediateActionStage(ImmediateActionStage stage)
3991 {
3992     m_immediateActionStage = stage;
3993 }
3994
3995 } // namespace WebCore