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