Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(36)

Side by Side Diff: third_party/WebKit/Source/core/input/MouseEventManager.cpp

Issue 2350433002: Extract more of the mouse logic from EventHandler (Closed)
Patch Set: Remove redundant condition Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "core/input/MouseEventManager.h" 5 #include "core/input/MouseEventManager.h"
6 6
7 #include "core/clipboard/DataObject.h"
8 #include "core/clipboard/DataTransfer.h"
9 #include "core/dom/Element.h"
7 #include "core/dom/ElementTraversal.h" 10 #include "core/dom/ElementTraversal.h"
11 #include "core/editing/FrameSelection.h"
12 #include "core/editing/SelectionController.h"
13 #include "core/events/DragEvent.h"
8 #include "core/events/MouseEvent.h" 14 #include "core/events/MouseEvent.h"
15 #include "core/frame/FrameView.h"
16 #include "core/frame/LocalFrame.h"
17 #include "core/frame/Settings.h"
9 #include "core/html/HTMLCanvasElement.h" 18 #include "core/html/HTMLCanvasElement.h"
19 #include "core/input/EventHandler.h"
10 #include "core/input/EventHandlingUtil.h" 20 #include "core/input/EventHandlingUtil.h"
21 #include "core/input/InputDeviceCapabilities.h"
22 #include "core/input/KeyboardEventManager.h"
23 #include "core/layout/HitTestResult.h"
24 #include "core/layout/api/LayoutViewItem.h"
25 #include "core/page/AutoscrollController.h"
26 #include "core/page/DragController.h"
27 #include "core/page/DragState.h"
28 #include "core/page/FocusController.h"
29 #include "core/svg/SVGDocumentExtensions.h"
30 #include "platform/geometry/FloatQuad.h"
31
11 32
12 namespace blink { 33 namespace blink {
13 34
14 namespace { 35 namespace {
15 36
16 PlatformMouseEvent mouseEventWithRegion(Node* node, const PlatformMouseEvent& mo useEvent) 37 PlatformMouseEvent mouseEventWithRegion(Node* node, const PlatformMouseEvent& mo useEvent)
17 { 38 {
18 if (!node->isElementNode()) 39 if (!node->isElementNode())
19 return mouseEvent; 40 return mouseEvent;
20 41
21 Element* element = toElement(node); 42 Element* element = toElement(node);
22 if (!element->isInCanvasSubtree()) 43 if (!element->isInCanvasSubtree())
23 return mouseEvent; 44 return mouseEvent;
24 45
25 HTMLCanvasElement* canvas = Traversal<HTMLCanvasElement>::firstAncestorOrSel f(*element); 46 HTMLCanvasElement* canvas = Traversal<HTMLCanvasElement>::firstAncestorOrSel f(*element);
26 // In this case, the event target is canvas and mouse rerouting doesn't happ en. 47 // In this case, the event target is canvas and mouse rerouting doesn't happ en.
27 if (canvas == element) 48 if (canvas == element)
28 return mouseEvent; 49 return mouseEvent;
29 String region = canvas->getIdFromControl(element); 50 String region = canvas->getIdFromControl(element);
30 PlatformMouseEvent newMouseEvent = mouseEvent; 51 PlatformMouseEvent newMouseEvent = mouseEvent;
31 newMouseEvent.setRegion(region); 52 newMouseEvent.setRegion(region);
32 return newMouseEvent; 53 return newMouseEvent;
33 } 54 }
34 55
56 // The amount of time to wait before sending a fake mouse event triggered
57 // during a scroll.
58 const double kFakeMouseMoveInterval = 0.1;
59
60 #if OS(MACOSX)
61 const double kTextDragDelay = 0.15;
62 #else
63 const double kTextDragDelay = 0.0;
64 #endif
65
66 // The link drag hysteresis is much larger than the others because there
67 // needs to be enough space to cancel the link press without starting a link dra g,
68 // and because dragging links is rare.
69 const int kLinkDragHysteresis = 40;
70 const int kImageDragHysteresis = 5;
71 const int kTextDragHysteresis = 3;
72 const int kGeneralDragHysteresis = 3;
73
35 } // namespace 74 } // namespace
36 75
76 enum class DragInitiator { Mouse, Touch };
77
37 MouseEventManager::MouseEventBoundaryEventDispatcher::MouseEventBoundaryEventDis patcher( 78 MouseEventManager::MouseEventBoundaryEventDispatcher::MouseEventBoundaryEventDis patcher(
38 MouseEventManager* mouseEventManager, 79 MouseEventManager* mouseEventManager,
39 const PlatformMouseEvent* platformMouseEvent, 80 const PlatformMouseEvent* platformMouseEvent,
40 EventTarget* exitedTarget) 81 EventTarget* exitedTarget)
41 : m_mouseEventManager(mouseEventManager) 82 : m_mouseEventManager(mouseEventManager)
42 , m_platformMouseEvent(platformMouseEvent) 83 , m_platformMouseEvent(platformMouseEvent)
43 , m_exitedTarget(exitedTarget) 84 , m_exitedTarget(exitedTarget)
44 { 85 {
45 } 86 }
46 87
(...skipping 29 matching lines...) Expand all
76 AtomicString MouseEventManager::MouseEventBoundaryEventDispatcher::getEnterEvent () 117 AtomicString MouseEventManager::MouseEventBoundaryEventDispatcher::getEnterEvent ()
77 { 118 {
78 return EventTypeNames::mouseenter; 119 return EventTypeNames::mouseenter;
79 } 120 }
80 121
81 void MouseEventManager::MouseEventBoundaryEventDispatcher::dispatch( 122 void MouseEventManager::MouseEventBoundaryEventDispatcher::dispatch(
82 EventTarget* target, EventTarget* relatedTarget, const AtomicString& type, 123 EventTarget* target, EventTarget* relatedTarget, const AtomicString& type,
83 const PlatformMouseEvent& platformMouseEvent, bool checkForListener) 124 const PlatformMouseEvent& platformMouseEvent, bool checkForListener)
84 { 125 {
85 m_mouseEventManager->dispatchMouseEvent(target, type, platformMouseEvent, 126 m_mouseEventManager->dispatchMouseEvent(target, type, platformMouseEvent,
86 relatedTarget, 0, checkForListener); 127 relatedTarget, checkForListener);
87 } 128 }
88 129
89 void MouseEventManager::sendBoundaryEvents( 130 void MouseEventManager::sendBoundaryEvents(
90 EventTarget* exitedTarget, 131 EventTarget* exitedTarget,
91 EventTarget* enteredTarget, 132 EventTarget* enteredTarget,
92 const PlatformMouseEvent& mousePlatformEvent) 133 const PlatformMouseEvent& mousePlatformEvent)
93 { 134 {
94 MouseEventBoundaryEventDispatcher boundaryEventDispatcher(this, &mousePlatfo rmEvent, exitedTarget); 135 MouseEventBoundaryEventDispatcher boundaryEventDispatcher(this, &mousePlatfo rmEvent, exitedTarget);
95 boundaryEventDispatcher.sendBoundaryEvents(exitedTarget, enteredTarget); 136 boundaryEventDispatcher.sendBoundaryEvents(exitedTarget, enteredTarget);
96 } 137 }
97 138
98 WebInputEventResult MouseEventManager::dispatchMouseEvent( 139 WebInputEventResult MouseEventManager::dispatchMouseEvent(
99 EventTarget* target, 140 EventTarget* target,
100 const AtomicString& mouseEventType, 141 const AtomicString& mouseEventType,
101 const PlatformMouseEvent& mouseEvent, 142 const PlatformMouseEvent& mouseEvent,
102 EventTarget* relatedTarget, 143 EventTarget* relatedTarget,
103 int detail,
104 bool checkForListener) 144 bool checkForListener)
105 { 145 {
106 if (target && target->toNode() 146 if (target && target->toNode()
107 && (!checkForListener || target->hasEventListeners(mouseEventType))) { 147 && (!checkForListener || target->hasEventListeners(mouseEventType))) {
108 Node* targetNode = target->toNode(); 148 Node* targetNode = target->toNode();
149 int clickCount = 0;
150 if (mouseEventType == EventTypeNames::mouseup
151 || mouseEventType == EventTypeNames::mousedown
152 || mouseEventType == EventTypeNames::click
153 || mouseEventType == EventTypeNames::auxclick
154 || mouseEventType == EventTypeNames::dblclick) {
155 clickCount = m_clickCount;
156 }
109 MouseEvent* event = MouseEvent::create(mouseEventType, 157 MouseEvent* event = MouseEvent::create(mouseEventType,
110 targetNode->document().domWindow(), mouseEvent, detail, 158 targetNode->document().domWindow(), mouseEvent, clickCount,
111 relatedTarget ? relatedTarget->toNode() : nullptr); 159 relatedTarget ? relatedTarget->toNode() : nullptr);
112 DispatchEventResult dispatchResult = target->dispatchEvent(event); 160 DispatchEventResult dispatchResult = target->dispatchEvent(event);
113 return EventHandlingUtil::toWebInputEventResult(dispatchResult); 161 return EventHandlingUtil::toWebInputEventResult(dispatchResult);
114 } 162 }
115 return WebInputEventResult::NotHandled; 163 return WebInputEventResult::NotHandled;
116 } 164 }
117 165
118 MouseEventManager::MouseEventManager(LocalFrame* frame) 166 WebInputEventResult MouseEventManager::setMousePositionAndDispatchMouseEvent(
167 Node* targetNode, const AtomicString& eventType,
168 const PlatformMouseEvent& platformMouseEvent)
169 {
170 // If the target node is a text node, dispatch on the parent node.
171 if (targetNode && targetNode->isTextNode())
172 targetNode = FlatTreeTraversal::parent(*targetNode);
173
174 setNodeUnderMouse(targetNode, platformMouseEvent);
175
176 return dispatchMouseEvent(m_nodeUnderMouse, eventType, platformMouseEvent,
177 nullptr);
178 }
179
180
181 WebInputEventResult MouseEventManager::dispatchMouseClickIfNeeded(
182 const MouseEventWithHitTestResults& mev)
183 {
184 // We only prevent click event when the click may cause contextmenu to popup .
185 // However, we always send auxclick.
186 bool contextMenuEvent = !RuntimeEnabledFeatures::auxclickEnabled()
187 && mev.event().pointerProperties().button == WebPointerProperties::Butto n::Right;
188 #if OS(MACOSX)
189 // FIXME: The Mac port achieves the same behavior by checking whether the co ntext menu is currently open in WebPage::mouseEvent(). Consider merging the impl ementations.
190 if (mev.event().pointerProperties().button == WebPointerProperties::Button:: Left
191 && mev.event().getModifiers() & PlatformEvent::CtrlKey)
192 contextMenuEvent = true;
193 #endif
194
195 WebInputEventResult clickEventResult = WebInputEventResult::NotHandled;
196 const bool shouldDispatchClickEvent = m_clickCount > 0
197 && !contextMenuEvent
198 && mev.innerNode() && m_clickNode
199 && mev.innerNode()->canParticipateInFlatTree() && m_clickNode->canPartic ipateInFlatTree()
200 && !(m_frame->eventHandler().selectionController().hasExtendedSelection( ) && isLinkSelection(mev));
201 if (shouldDispatchClickEvent) {
202 Node* clickTargetNode = nullptr;
203 // Updates distribution because a 'mouseup' event listener can make the
204 // tree dirty at dispatchMouseEvent() invocation above.
205 // Unless distribution is updated, commonAncestor would hit ASSERT.
206 if (m_clickNode == mev.innerNode()) {
207 clickTargetNode = m_clickNode;
208 clickTargetNode->updateDistribution();
209 } else if (m_clickNode->document() == mev.innerNode()->document()) {
210 m_clickNode->updateDistribution();
211 mev.innerNode()->updateDistribution();
212 clickTargetNode = mev.innerNode()->commonAncestor(
213 *m_clickNode, EventHandlingUtil::parentForClickEvent);
214 }
215 if (clickTargetNode) {
216 clickEventResult = dispatchMouseEvent(clickTargetNode,
217 !RuntimeEnabledFeatures::auxclickEnabled()
218 || (mev.event().pointerProperties().button == WebPointerProperti es::Button::Left)
219 ? EventTypeNames::click
220 : EventTypeNames::auxclick, mev.event(), nullptr);
221 }
222 }
223 return clickEventResult;
224 }
225
226
227 void MouseEventManager::fakeMouseMoveEventTimerFired(TimerBase* timer)
228 {
229 TRACE_EVENT0("input", "MouseEventManager::fakeMouseMoveEventTimerFired");
230 DCHECK(timer == &m_fakeMouseMoveEventTimer);
231 DCHECK(!m_mousePressed);
232
233 Settings* settings = m_frame->settings();
234 if (settings && !settings->deviceSupportsMouse())
235 return;
236
237 FrameView* view = m_frame->view();
238 if (!view)
239 return;
240
241 if (!m_frame->page() || !m_frame->page()->focusController().isActive())
242 return;
243
244 // Don't dispatch a synthetic mouse move event if the mouse cursor is not vi sible to the user.
245 if (!m_frame->page()->isCursorVisible())
246 return;
247
248 PlatformMouseEvent fakeMouseMoveEvent(m_lastKnownMousePosition, m_lastKnownM ouseGlobalPosition, WebPointerProperties::Button::NoButton, PlatformEvent::Mouse Moved, 0, static_cast<PlatformEvent::Modifiers>(KeyboardEventManager::getCurrent ModifierState()), PlatformMouseEvent::RealOrIndistinguishable, monotonicallyIncr easingTime(), WebPointerProperties::PointerType::Mouse);
249 m_frame->eventHandler().handleMouseMoveEvent(fakeMouseMoveEvent);
250 }
251
252 void MouseEventManager::cancelFakeMouseMoveEvent()
253 {
254 m_fakeMouseMoveEventTimer.stop();
255 }
256
257 void MouseEventManager::setNodeUnderMouse(
258 Node* target, const PlatformMouseEvent &platformMouseEvent)
259 {
260 Node* lastNodeUnderMouse = m_nodeUnderMouse;
261 m_nodeUnderMouse = target;
262
263 PaintLayer* layerForLastNode = EventHandlingUtil::layerForNode(lastNodeUnder Mouse);
264 PaintLayer* layerForNodeUnderMouse = EventHandlingUtil::layerForNode(m_nodeU nderMouse.get());
265 Page* page = m_frame->page();
266
267 if (lastNodeUnderMouse && (!m_nodeUnderMouse || m_nodeUnderMouse->document() != m_frame->document())) {
268 // The mouse has moved between frames.
269 if (LocalFrame* frame = lastNodeUnderMouse->document().frame()) {
270 if (FrameView* frameView = frame->view())
271 frameView->mouseExitedContentArea();
272 }
273 } else if (page && (layerForLastNode && (!layerForNodeUnderMouse || layerFor NodeUnderMouse != layerForLastNode))) {
274 // The mouse has moved between layers.
275 if (ScrollableArea* scrollableAreaForLastNode = EventHandlingUtil::assoc iatedScrollableArea(layerForLastNode))
276 scrollableAreaForLastNode->mouseExitedContentArea();
277 }
278
279 if (m_nodeUnderMouse && (!lastNodeUnderMouse || lastNodeUnderMouse->document () != m_frame->document())) {
280 // The mouse has moved between frames.
281 if (LocalFrame* frame = m_nodeUnderMouse->document().frame()) {
282 if (FrameView* frameView = frame->view())
283 frameView->mouseEnteredContentArea();
284 }
285 } else if (page && (layerForNodeUnderMouse && (!layerForLastNode || layerFor NodeUnderMouse != layerForLastNode))) {
286 // The mouse has moved between layers.
287 if (ScrollableArea* scrollableAreaForNodeUnderMouse = EventHandlingUtil: :associatedScrollableArea(layerForNodeUnderMouse))
288 scrollableAreaForNodeUnderMouse->mouseEnteredContentArea();
289 }
290
291 if (lastNodeUnderMouse && lastNodeUnderMouse->document() != m_frame->documen t()) {
292 lastNodeUnderMouse = nullptr;
293 }
294
295 sendBoundaryEvents(lastNodeUnderMouse, m_nodeUnderMouse, platformMouseEvent) ;
296 }
297
298 void MouseEventManager::nodeWillBeRemoved(Node& nodeToBeRemoved)
299 {
300 if (nodeToBeRemoved.isShadowIncludingInclusiveAncestorOf(m_clickNode.get())) {
301 // We don't dispatch click events if the mousedown node is removed
302 // before a mouseup event. It is compatible with IE and Firefox.
303 m_clickNode = nullptr;
304 }
305 }
306
307 Node* MouseEventManager::getNodeUnderMouse()
308 {
309 return m_nodeUnderMouse;
310 }
311
312 WebInputEventResult MouseEventManager::handleMouseFocus(const HitTestResult& hit TestResult, InputDeviceCapabilities* sourceCapabilities)
313 {
314 // If clicking on a frame scrollbar, do not mess up with content focus.
315 if (hitTestResult.scrollbar() && !m_frame->contentLayoutItem().isNull()) {
316 if (hitTestResult.scrollbar()->getScrollableArea() == m_frame->contentLa youtItem().getScrollableArea())
317 return WebInputEventResult::NotHandled;
318 }
319
320 // The layout needs to be up to date to determine if an element is focusable .
321 m_frame->document()->updateStyleAndLayoutIgnorePendingStylesheets();
322
323 Element* element = nullptr;
324 if (m_nodeUnderMouse)
325 element = m_nodeUnderMouse->isElementNode() ? toElement(m_nodeUnderMouse ) : m_nodeUnderMouse->parentOrShadowHostElement();
326 for (; element; element = element->parentOrShadowHostElement()) {
327 if (element->isFocusable() && element->isFocusedElementInDocument())
328 return WebInputEventResult::NotHandled;
329 if (element->isMouseFocusable())
330 break;
331 }
332 DCHECK(!element || element->isMouseFocusable());
333
334 // To fix <rdar://problem/4895428> Can't drag selected ToDo, we don't focus
335 // a node on mouse down if it's selected and inside a focused node. It will
336 // be focused if the user does a mouseup over it, however, because the
337 // mouseup will set a selection inside it, which will call
338 // FrameSelection::setFocusedNodeIfNeeded.
339 if (element && m_frame->selection().isRange()) {
340 // TODO(yosin) We should not create |Range| object for calling
341 // |isNodeFullyContained()|.
342 if (createRange(m_frame->selection().selection().toNormalizedEphemeralRa nge())->isNodeFullyContained(*element)
343 && element->isDescendantOf(m_frame->document()->focusedElement()))
344 return WebInputEventResult::NotHandled;
345 }
346
347
348 // Only change the focus when clicking scrollbars if it can transfered to a
349 // mouse focusable node.
350 if (!element && hitTestResult.scrollbar())
351 return WebInputEventResult::HandledSystem;
352
353 if (Page* page = m_frame->page()) {
354 // If focus shift is blocked, we eat the event. Note we should never
355 // clear swallowEvent if the page already set it (e.g., by canceling
356 // default behavior).
357 if (element) {
358 if (slideFocusOnShadowHostIfNecessary(*element))
359 return WebInputEventResult::HandledSystem;
360 if (!page->focusController().setFocusedElement(element, m_frame, Foc usParams(SelectionBehaviorOnFocus::None, WebFocusTypeMouse, sourceCapabilities)) )
361 return WebInputEventResult::HandledSystem;
362 } else {
363 // We call setFocusedElement even with !element in order to blur
364 // current focus element when a link is clicked; this is expected by
365 // some sites that rely on onChange handlers running from form
366 // fields before the button click is processed.
367 if (!page->focusController().setFocusedElement(nullptr, m_frame, Foc usParams(SelectionBehaviorOnFocus::None, WebFocusTypeNone, sourceCapabilities)))
368 return WebInputEventResult::HandledSystem;
369 }
370 }
371
372 return WebInputEventResult::NotHandled;
373 }
374
375 bool MouseEventManager::slideFocusOnShadowHostIfNecessary(const Element& element )
376 {
377 if (element.authorShadowRoot() && element.authorShadowRoot()->delegatesFocus ()) {
378 Document* doc = m_frame->document();
379 if (element.isShadowIncludingInclusiveAncestorOf(doc->focusedElement())) {
380 // If the inner element is already focused, do nothing.
381 return true;
382 }
383
384 // If the host has a focusable inner element, focus it. Otherwise, the h ost takes focus.
385 Page* page = m_frame->page();
386 DCHECK(page);
387 Element* found = page->focusController().findFocusableElementInShadowHos t(element);
388 if (found && element.isShadowIncludingInclusiveAncestorOf(found)) {
389 // Use WebFocusTypeForward instead of WebFocusTypeMouse here to mean the focus has slided.
390 found->focus(FocusParams(SelectionBehaviorOnFocus::Reset, WebFocusTy peForward, nullptr));
391 return true;
392 }
393 }
394 return false;
395 }
396
397 void MouseEventManager::handleMousePressEventUpdateStates(
398 const PlatformMouseEvent& mouseEvent) {
399 cancelFakeMouseMoveEvent();
400 m_mousePressed = true;
401 m_capturesDragging = true;
402 setLastKnownMousePosition(mouseEvent);
403 m_mouseDownMayStartDrag = false;
404 m_mouseDownMayStartAutoscroll = false;
405 m_mouseDownTimestamp = mouseEvent.timestamp();
406
407 if (FrameView* view = m_frame->view()) {
408 m_mouseDownPos = view->rootFrameToContents(mouseEvent.position());
409 } else {
410 invalidateClick();
411 }
412 }
413
414 bool MouseEventManager::isMousePositionUnknown()
415 {
416 return m_isMousePositionUnknown;
417 }
418
419 IntPoint MouseEventManager::lastKnownMousePosition()
420 {
421 return m_lastKnownMousePosition;
422 }
423
424 void MouseEventManager::setLastKnownMousePosition(const PlatformMouseEvent& even t)
425 {
426 m_isMousePositionUnknown = false;
427 m_lastKnownMousePosition = event.position();
428 m_lastKnownMouseGlobalPosition = event.globalPosition();
429 }
430
431 void MouseEventManager::dispatchFakeMouseMoveEventSoon()
432 {
433 if (m_mousePressed)
434 return;
435
436 if (m_isMousePositionUnknown)
437 return;
438
439 Settings* settings = m_frame->settings();
440 if (settings && !settings->deviceSupportsMouse())
441 return;
442
443 // Reschedule the timer, to prevent dispatching mouse move events
444 // during a scroll. This avoids a potential source of scroll jank.
445 if (m_fakeMouseMoveEventTimer.isActive())
446 m_fakeMouseMoveEventTimer.stop();
447 m_fakeMouseMoveEventTimer.startOneShot(kFakeMouseMoveInterval, BLINK_FROM_HE RE);
448 }
449
450 void MouseEventManager::dispatchFakeMouseMoveEventSoonInQuad(const FloatQuad& qu ad)
451 {
452 FrameView* view = m_frame->view();
453 if (!view)
454 return;
455
456 if (!quad.containsPoint(view->rootFrameToContents(m_lastKnownMousePosition)) )
457 return;
458
459 dispatchFakeMouseMoveEventSoon();
460 }
461
462 WebInputEventResult MouseEventManager::handleMousePressEvent(const MouseEventWit hHitTestResults& event)
463 {
464 TRACE_EVENT0("blink", "MouseEventManager::handleMousePressEvent");
465
466 // Reset drag state.
467 dragState().m_dragSrc = nullptr;
468
469 cancelFakeMouseMoveEvent();
470
471 m_frame->document()->updateStyleAndLayoutIgnorePendingStylesheets();
472
473 if (FrameView* frameView = m_frame->view()) {
474 if (frameView->isPointInScrollbarCorner(event.event().position()))
475 return WebInputEventResult::NotHandled;
476 }
477
478 bool singleClick = event.event().clickCount() <= 1;
479
480 m_mouseDownMayStartDrag = singleClick && !isLinkSelection(event) && !isExten dingSelection(event);
481
482 m_frame->eventHandler().selectionController().handleMousePressEvent(event);
483
484 m_mouseDown = event.event();
485
486 if (m_frame->document()->isSVGDocument() && m_frame->document()->accessSVGEx tensions().zoomAndPanEnabled()) {
487 if (event.event().shiftKey() && singleClick) {
488 m_svgPan = true;
489 m_frame->document()->accessSVGExtensions().startPan(m_frame->view()- >rootFrameToContents(event.event().position()));
490 return WebInputEventResult::HandledSystem;
491 }
492 }
493
494 // We don't do this at the start of mouse down handling,
495 // because we don't want to do it until we know we didn't hit a widget.
496 if (singleClick)
497 focusDocumentView();
498
499 Node* innerNode = event.innerNode();
500
501 m_mousePressNode = innerNode;
502 m_frame->document()->setSequentialFocusNavigationStartingPoint(innerNode);
503 m_dragStartPos = event.event().position();
504
505 bool swallowEvent = false;
506 m_mousePressed = true;
507
508 if (event.event().clickCount() == 2)
509 swallowEvent = m_frame->eventHandler().selectionController().handleMouse PressEventDoubleClick(event);
510 else if (event.event().clickCount() >= 3)
511 swallowEvent = m_frame->eventHandler().selectionController().handleMouse PressEventTripleClick(event);
512 else
513 swallowEvent = m_frame->eventHandler().selectionController().handleMouse PressEventSingleClick(event);
514
515 m_mouseDownMayStartAutoscroll = m_frame->eventHandler().selectionController( ).mouseDownMayStartSelect()
516 || (m_mousePressNode && m_mousePressNode->layoutBox() && m_mousePressNod e->layoutBox()->canBeProgramaticallyScrolled());
517
518 return swallowEvent ? WebInputEventResult::HandledSystem : WebInputEventResu lt::NotHandled;
519 }
520
521 WebInputEventResult MouseEventManager::handleMouseReleaseEvent(const MouseEventW ithHitTestResults& event)
522 {
523 AutoscrollController* controller = m_scrollManager->autoscrollController();
524 if (controller && controller->autoscrollInProgress())
525 m_scrollManager->stopAutoscroll();
526
527 return m_frame->eventHandler().selectionController().handleMouseReleaseEvent (event, m_dragStartPos) ? WebInputEventResult::HandledSystem : WebInputEventResu lt::NotHandled;
528 }
529
530 void MouseEventManager::updateSelectionForMouseDrag()
531 {
532 m_frame->eventHandler().selectionController().updateSelectionForMouseDrag(m_ mousePressNode, m_dragStartPos, m_lastKnownMousePosition);
533 }
534
535 bool MouseEventManager::handleDragDropIfPossible(const GestureEventWithHitTestRe sults& targetedEvent)
536 {
537 if (m_frame->settings() && m_frame->settings()->touchDragDropEnabled() && m_ frame->view()) {
538 const PlatformGestureEvent& gestureEvent = targetedEvent.event();
539 IntPoint adjustedPoint = gestureEvent.position();
540 unsigned modifiers = gestureEvent.getModifiers();
541
542 // TODO(mustaq): Suppressing long-tap MouseEvents could break
543 // drag-drop. Will do separately because of the risk. crbug.com/606938.
544 PlatformMouseEvent mouseDownEvent(adjustedPoint, gestureEvent.globalPosi tion(), WebPointerProperties::Button::Left, PlatformEvent::MousePressed, 1,
545 static_cast<PlatformEvent::Modifiers>(modifiers | PlatformEvent::Lef tButtonDown),
546 PlatformMouseEvent::FromTouch, WTF::monotonicallyIncreasingTime(), W ebPointerProperties::PointerType::Mouse);
547 m_mouseDown = mouseDownEvent;
548
549 PlatformMouseEvent mouseDragEvent(adjustedPoint, gestureEvent.globalPosi tion(), WebPointerProperties::Button::Left, PlatformEvent::MouseMoved, 1,
550 static_cast<PlatformEvent::Modifiers>(modifiers | PlatformEvent::Lef tButtonDown),
551 PlatformMouseEvent::FromTouch, WTF::monotonicallyIncreasingTime(), W ebPointerProperties::PointerType::Mouse);
552 HitTestRequest request(HitTestRequest::ReadOnly);
553 MouseEventWithHitTestResults mev = EventHandlingUtil::performMouseEventH itTest(m_frame, request, mouseDragEvent);
554 m_mouseDownMayStartDrag = true;
555 dragState().m_dragSrc = nullptr;
556 m_mouseDownPos = m_frame->view()->rootFrameToContents(mouseDragEvent.pos ition());
557 return handleDrag(mev, DragInitiator::Touch);
558 }
559 return false;
560 }
561
562 void MouseEventManager::focusDocumentView()
563 {
564 Page* page = m_frame->page();
565 if (!page)
566 return;
567 page->focusController().focusDocumentView(m_frame);
568 }
569
570 WebInputEventResult MouseEventManager::handleMouseDraggedEvent(const MouseEventW ithHitTestResults& event)
571 {
572 TRACE_EVENT0("blink", "MouseEventManager::handleMouseDraggedEvent");
573
574 // While resetting m_mousePressed here may seem out of place, it turns out
575 // to be needed to handle some bugs^Wfeatures in Blink mouse event handling:
576 // 1. Certain elements, such as <embed>, capture mouse events. They do not
577 // bubble back up. One way for a <embed> to start capturing mouse events
578 // is on a mouse press. The problem is the <embed> node only starts
579 // capturing mouse events *after* m_mousePressed for the containing frame
580 // has already been set to true. As a result, the frame's EventHandler
581 // never sees the mouse release event, which is supposed to reset
582 // m_mousePressed... so m_mousePressed ends up remaining true until the
583 // event handler finally gets another mouse released event. Oops.
584 // 2. Dragging doesn't start until after a mouse press event, but a drag
585 // that ends as a result of a mouse release does not send a mouse release
586 // event. As a result, m_mousePressed also ends up remaining true until
587 // the next mouse release event seen by the EventHandler.
588 if (event.event().pointerProperties().button != WebPointerProperties::Button ::Left)
589 m_mousePressed = false;
590
591 if (!m_mousePressed)
592 return WebInputEventResult::NotHandled;
593
594 if (handleDrag(event, DragInitiator::Mouse))
595 return WebInputEventResult::HandledSystem;
596
597 Node* targetNode = event.innerNode();
598 if (!targetNode)
599 return WebInputEventResult::NotHandled;
600
601 LayoutObject* layoutObject = targetNode->layoutObject();
602 if (!layoutObject) {
603 Node* parent = FlatTreeTraversal::parent(*targetNode);
604 if (!parent)
605 return WebInputEventResult::NotHandled;
606
607 layoutObject = parent->layoutObject();
608 if (!layoutObject || !layoutObject->isListBox())
609 return WebInputEventResult::NotHandled;
610 }
611
612 m_mouseDownMayStartDrag = false;
613
614 if (m_mouseDownMayStartAutoscroll && !m_scrollManager->middleClickAutoscroll InProgress()) {
615 if (AutoscrollController* controller = m_scrollManager->autoscrollContro ller()) {
616 controller->startAutoscrollForSelection(layoutObject);
617 m_mouseDownMayStartAutoscroll = false;
618 }
619 }
620
621 m_frame->eventHandler().selectionController().handleMouseDraggedEvent(event, m_mouseDownPos, m_dragStartPos, m_mousePressNode.get(), m_lastKnownMousePositio n);
622 return WebInputEventResult::HandledSystem;
623 }
624
625
626 bool MouseEventManager::handleDrag(const MouseEventWithHitTestResults& event, Dr agInitiator initiator)
627 {
628 DCHECK(event.event().type() == PlatformEvent::MouseMoved);
629 // Callers must protect the reference to FrameView, since this function may dispatch DOM
630 // events, causing page/FrameView to go away.
631 DCHECK(m_frame);
632 DCHECK(m_frame->view());
633 if (!m_frame->page())
634 return false;
635
636 if (m_mouseDownMayStartDrag) {
637 HitTestRequest request(HitTestRequest::ReadOnly);
638 HitTestResult result(request, m_mouseDownPos);
639 m_frame->contentLayoutItem().hitTest(result);
640 Node* node = result.innerNode();
641 if (node) {
642 DragController::SelectionDragPolicy selectionDragPolicy = event.even t().timestamp() - m_mouseDownTimestamp < kTextDragDelay
643 ? DragController::DelayedSelectionDragResolution
644 : DragController::ImmediateSelectionDragResolution;
645 dragState().m_dragSrc = m_frame->page()->dragController().draggableN ode(m_frame, node, m_mouseDownPos, selectionDragPolicy, dragState().m_dragType);
646 } else {
647 dragState().m_dragSrc = nullptr;
648 }
649
650 if (!dragState().m_dragSrc)
651 m_mouseDownMayStartDrag = false; // no element is draggable
652 }
653
654 if (!m_mouseDownMayStartDrag)
655 return initiator == DragInitiator::Mouse && !m_frame->eventHandler().sel ectionController().mouseDownMayStartSelect() && !m_mouseDownMayStartAutoscroll;
656
657 // We are starting a text/image/url drag, so the cursor should be an arrow
658 // FIXME <rdar://7577595>: Custom cursors aren't supported during drag and d rop (default to pointer).
659 m_frame->view()->setCursor(pointerCursor());
660
661 if (initiator == DragInitiator::Mouse && !dragHysteresisExceeded(event.event ().position()))
662 return true;
663
664 // Once we're past the hysteresis point, we don't want to treat this gesture as a click
665 invalidateClick();
666
667 if (!tryStartDrag(event)) {
668 // Something failed to start the drag, clean up.
669 clearDragDataTransfer();
670 dragState().m_dragSrc = nullptr;
671 }
672
673 m_mouseDownMayStartDrag = false;
674 // Whether or not the drag actually started, no more default handling (like selection).
675 return true;
676 }
677
678 DataTransfer* MouseEventManager::createDraggingDataTransfer() const
679 {
680 return DataTransfer::create(DataTransfer::DragAndDrop, DataTransferWritable, DataObject::create());
681 }
682
683 bool MouseEventManager::tryStartDrag(const MouseEventWithHitTestResults& event)
684 {
685 // The DataTransfer would only be non-empty if we missed a dragEnd.
686 // Clear it anyway, just to make sure it gets numbified.
687 clearDragDataTransfer();
688
689 dragState().m_dragDataTransfer = createDraggingDataTransfer();
690
691 // Check to see if this a DOM based drag, if it is get the DOM specified dra g
692 // image and offset
693 if (dragState().m_dragType == DragSourceActionDHTML) {
694 if (LayoutObject* layoutObject = dragState().m_dragSrc->layoutObject()) {
695 IntRect boundingIncludingDescendants = layoutObject->absoluteBoundin gBoxRectIncludingDescendants();
696 IntSize delta = m_mouseDownPos - boundingIncludingDescendants.locati on();
697 dragState().m_dragDataTransfer->setDragImageElement(dragState().m_dr agSrc.get(), IntPoint(delta));
698 } else {
699 // The layoutObject has disappeared, this can happen if the onStartD rag handler has hidden
700 // the element in some way. In this case we just kill the drag.
701 return false;
702 }
703 }
704
705 DragController& dragController = m_frame->page()->dragController();
706 if (!dragController.populateDragDataTransfer(m_frame, dragState(), m_mouseDo wnPos))
707 return false;
708
709 // If dispatching dragstart brings about another mouse down -- one way
710 // this will happen is if a DevTools user breaks within a dragstart
711 // handler and then clicks on the suspended page -- the drag state is
712 // reset. Hence, need to check if this particular drag operation can
713 // continue even if dispatchEvent() indicates no (direct) cancellation.
714 // Do that by checking if m_dragSrc is still set.
715 m_mouseDownMayStartDrag = dispatchDragSrcEvent(EventTypeNames::dragstart, m_ mouseDown) == WebInputEventResult::NotHandled
716 && !m_frame->selection().isInPasswordField() && dragState().m_dragSrc;
717
718 // Invalidate clipboard here against anymore pasteboard writing for security . The drag
719 // image can still be changed as we drag, but not the pasteboard data.
720 dragState().m_dragDataTransfer->setAccessPolicy(DataTransferImageWritable);
721
722 if (m_mouseDownMayStartDrag) {
723 // Dispatching the event could cause Page to go away. Make sure it's sti ll valid before trying to use DragController.
724 if (m_frame->page() && dragController.startDrag(m_frame, dragState(), ev ent.event(), m_mouseDownPos))
725 return true;
726 // Drag was canned at the last minute - we owe m_dragSrc a DRAGEND event
727 dispatchDragSrcEvent(EventTypeNames::dragend, event.event());
728 }
729
730 return false;
731 }
732
733 // returns if we should continue "default processing", i.e., whether eventhandle r canceled
734 WebInputEventResult MouseEventManager::dispatchDragSrcEvent(const AtomicString& eventType, const PlatformMouseEvent& event)
735 {
736 return dispatchDragEvent(eventType, dragState().m_dragSrc.get(), event, drag State().m_dragDataTransfer.get());
737 }
738
739 WebInputEventResult MouseEventManager::dispatchDragEvent(const AtomicString& eve ntType, Node* dragTarget, const PlatformMouseEvent& event, DataTransfer* dataTra nsfer)
740 {
741 FrameView* view = m_frame->view();
742
743 // FIXME: We might want to dispatch a dragleave even if the view is gone.
744 if (!view)
745 return WebInputEventResult::NotHandled;
746
747 DragEvent* me = DragEvent::create(eventType,
748 true, true, m_frame->document()->domWindow(),
749 0, event.globalPosition().x(), event.globalPosition().y(), event.positio n().x(), event.position().y(),
750 event.movementDelta().x(), event.movementDelta().y(),
751 event.getModifiers(),
752 0, MouseEvent::platformModifiersToButtons(event.getModifiers()), nullptr , event.timestamp(), dataTransfer, event.getSyntheticEventType());
753
754 return EventHandlingUtil::toWebInputEventResult(dragTarget->dispatchEvent(me ));
755 }
756
757 void MouseEventManager::clearDragDataTransfer()
758 {
759 if (dragState().m_dragDataTransfer) {
760 dragState().m_dragDataTransfer->clearDragImage();
761 dragState().m_dragDataTransfer->setAccessPolicy(DataTransferNumb);
762 }
763 }
764
765 void MouseEventManager::dragSourceEndedAt(const PlatformMouseEvent& event, DragO peration operation)
766 {
767 // Send a hit test request so that Layer gets a chance to update the :hover and :active pseudoclasses.
768 HitTestRequest request(HitTestRequest::Release);
769 EventHandlingUtil::performMouseEventHitTest(m_frame, request, event);
770
771 if (dragState().m_dragSrc) {
772 dragState().m_dragDataTransfer->setDestinationOperation(operation);
773 // for now we don't care if event handler cancels default behavior, sinc e there is none
774 dispatchDragSrcEvent(EventTypeNames::dragend, event);
775 }
776 clearDragDataTransfer();
777 dragState().m_dragSrc = nullptr;
778 // In case the drag was ended due to an escape key press we need to ensure
779 // that consecutive mousemove events don't reinitiate the drag and drop.
780 m_mouseDownMayStartDrag = false;
781 }
782
783 DragState& MouseEventManager::dragState()
784 {
785 DEFINE_STATIC_LOCAL(DragState, state, (new DragState));
786 return state;
787 }
788
789 bool MouseEventManager::dragHysteresisExceeded(const IntPoint& dragLocationInRoo tFrame) const
790 {
791 FrameView* view = m_frame->view();
792 if (!view)
793 return false;
794 IntPoint dragLocation = view->rootFrameToContents(dragLocationInRootFrame);
795 IntSize delta = dragLocation - m_mouseDownPos;
796
797 int threshold = kGeneralDragHysteresis;
798 switch (dragState().m_dragType) {
799 case DragSourceActionSelection:
800 threshold = kTextDragHysteresis;
801 break;
802 case DragSourceActionImage:
803 threshold = kImageDragHysteresis;
804 break;
805 case DragSourceActionLink:
806 threshold = kLinkDragHysteresis;
807 break;
808 case DragSourceActionDHTML:
809 break;
810 case DragSourceActionNone:
811 NOTREACHED();
812 }
813
814 return abs(delta.width()) >= threshold || abs(delta.height()) >= threshold;
815 }
816
817 void MouseEventManager::clearDragHeuristicState()
818 {
819 // Used to prevent mouseMoveEvent from initiating a drag before
820 // the mouse is pressed again.
821 m_mousePressed = false;
822 m_capturesDragging = false;
823 m_mouseDownMayStartDrag = false;
824 m_mouseDownMayStartAutoscroll = false;
825 }
826
827 bool MouseEventManager::handleSvgPanIfNeeded(bool isReleaseEvent)
828 {
829 if (!m_svgPan)
830 return false;
831 m_svgPan = !isReleaseEvent;
832 m_frame->document()->accessSVGExtensions().updatePan(m_frame->view()->rootFr ameToContents(m_lastKnownMousePosition));
833 return true;
834 }
835
836 void MouseEventManager::invalidateClick()
837 {
838 m_clickCount = 0;
839 m_clickNode = nullptr;
840 }
841
842 bool MouseEventManager::mousePressed()
843 {
844 return m_mousePressed;
845 }
846
847 void MouseEventManager::setMousePressed(bool mousePressed)
848 {
849 m_mousePressed = mousePressed;
850 }
851
852 bool MouseEventManager::capturesDragging() const
853 {
854 return m_capturesDragging;
855 }
856
857 void MouseEventManager::setCapturesDragging(bool capturesDragging)
858 {
859 m_capturesDragging = capturesDragging;
860 }
861
862 Node* MouseEventManager::mousePressNode()
863 {
864 return m_mousePressNode;
865 }
866
867 void MouseEventManager::setMousePressNode(Node* node)
868 {
869 m_mousePressNode = node;
870 }
871
872 void MouseEventManager::setClickNode(Node* node)
873 {
874 m_clickNode = node;
875 }
876
877 void MouseEventManager::setClickCount(int clickCount)
878 {
879 m_clickCount = clickCount;
880 }
881
882 bool MouseEventManager::mouseDownMayStartDrag()
883 {
884 return m_mouseDownMayStartDrag;
885 }
886
887 MouseEventManager::MouseEventManager(LocalFrame* frame,
888 ScrollManager* scrollManager)
119 : m_frame(frame) 889 : m_frame(frame)
890 , m_scrollManager(scrollManager)
891 , m_fakeMouseMoveEventTimer(this, &MouseEventManager::fakeMouseMoveEventTime rFired)
120 { 892 {
121 clear(); 893 clear();
122 } 894 }
123 895
896 MouseEventManager::~MouseEventManager()
bokan 2016/09/30 16:57:55 For consistency with other code, we usually have t
Navid Zolghadr 2016/10/03 16:03:01 Done.
897 {
898 DCHECK(!m_fakeMouseMoveEventTimer.isActive());
bokan 2016/09/30 16:57:55 Do we really need this check? It's the only reason
Navid Zolghadr 2016/10/03 16:03:01 Probably not. I just tried to keep that. But Let's
899 }
900
124 void MouseEventManager::clear() 901 void MouseEventManager::clear()
125 { 902 {
903 m_nodeUnderMouse = nullptr;
904 m_mousePressNode = nullptr;
905 m_mouseDownMayStartAutoscroll = false;
906 m_mouseDownMayStartDrag = false;
907 m_capturesDragging = false;
908 m_isMousePositionUnknown = true;
909 m_lastKnownMousePosition = IntPoint();
910 m_lastKnownMouseGlobalPosition = IntPoint();
911 m_mousePressed = false;
912 m_clickCount = 0;
913 m_clickNode = nullptr;
914 m_mouseDownPos = IntPoint();
915 m_mouseDownTimestamp = 0;
916 m_mouseDown = PlatformMouseEvent();
917 m_svgPan = false;
918 m_dragStartPos = LayoutPoint();
919 m_fakeMouseMoveEventTimer.stop();
126 } 920 }
127 921
128 DEFINE_TRACE(MouseEventManager) 922 DEFINE_TRACE(MouseEventManager)
129 { 923 {
130 visitor->trace(m_frame); 924 visitor->trace(m_frame);
925 visitor->trace(m_scrollManager);
131 visitor->trace(m_nodeUnderMouse); 926 visitor->trace(m_nodeUnderMouse);
927 visitor->trace(m_mousePressNode);
928 visitor->trace(m_clickNode);
132 } 929 }
133 930
134 } // namespace blink 931 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698