File: MouseEventManager.cpp

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (1003 lines) | stat: -rw-r--r-- 37,017 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "core/input/MouseEventManager.h"

#include "core/clipboard/DataObject.h"
#include "core/clipboard/DataTransfer.h"
#include "core/dom/Element.h"
#include "core/dom/ElementTraversal.h"
#include "core/dom/TaskRunnerHelper.h"
#include "core/editing/FrameSelection.h"
#include "core/editing/SelectionController.h"
#include "core/events/DragEvent.h"
#include "core/events/MouseEvent.h"
#include "core/frame/FrameView.h"
#include "core/frame/LocalFrame.h"
#include "core/frame/Settings.h"
#include "core/html/HTMLCanvasElement.h"
#include "core/input/EventHandler.h"
#include "core/input/EventHandlingUtil.h"
#include "core/input/InputDeviceCapabilities.h"
#include "core/input/KeyboardEventManager.h"
#include "core/layout/HitTestResult.h"
#include "core/layout/api/LayoutViewItem.h"
#include "core/page/AutoscrollController.h"
#include "core/page/DragController.h"
#include "core/page/DragState.h"
#include "core/page/FocusController.h"
#include "core/paint/PaintLayer.h"
#include "core/svg/SVGDocumentExtensions.h"
#include "platform/geometry/FloatQuad.h"

namespace blink {

namespace {

PlatformMouseEvent mouseEventWithRegion(Node* node,
                                        const PlatformMouseEvent& mouseEvent) {
  if (!node->isElementNode())
    return mouseEvent;

  Element* element = toElement(node);
  if (!element->isInCanvasSubtree())
    return mouseEvent;

  HTMLCanvasElement* canvas =
      Traversal<HTMLCanvasElement>::firstAncestorOrSelf(*element);
  // In this case, the event target is canvas and mouse rerouting doesn't
  // happen.
  if (canvas == element)
    return mouseEvent;
  String region = canvas->getIdFromControl(element);
  PlatformMouseEvent newMouseEvent = mouseEvent;
  newMouseEvent.setRegion(region);
  return newMouseEvent;
}

// The amount of time to wait before sending a fake mouse event triggered
// during a scroll.
const double kFakeMouseMoveInterval = 0.1;

// TODO(crbug.com/653490): Read these values from the OS.
#if OS(MACOSX)
const int kDragThresholdX = 3;
const int kDragThresholdY = 3;
const TimeDelta kTextDragDelay = TimeDelta::FromSecondsD(0.15);
#else
const int kDragThresholdX = 4;
const int kDragThresholdY = 4;
const TimeDelta kTextDragDelay = TimeDelta::FromSecondsD(0.0);
#endif

}  // namespace

enum class DragInitiator { Mouse, Touch };

MouseEventManager::MouseEventManager(LocalFrame& frame,
                                     ScrollManager& scrollManager)
    : m_frame(frame),
      m_scrollManager(scrollManager),
      m_fakeMouseMoveEventTimer(
          TaskRunnerHelper::get(TaskType::UserInteraction, &frame),
          this,
          &MouseEventManager::fakeMouseMoveEventTimerFired) {
  clear();
}

void MouseEventManager::clear() {
  m_nodeUnderMouse = nullptr;
  m_mousePressNode = nullptr;
  m_mouseDownMayStartAutoscroll = false;
  m_mouseDownMayStartDrag = false;
  m_capturesDragging = false;
  m_isMousePositionUnknown = true;
  m_lastKnownMousePosition = IntPoint();
  m_lastKnownMouseGlobalPosition = IntPoint();
  m_mousePressed = false;
  m_clickCount = 0;
  m_clickNode = nullptr;
  m_mouseDownPos = IntPoint();
  m_mouseDownTimestamp = TimeTicks();
  m_mouseDown = PlatformMouseEvent();
  m_svgPan = false;
  m_dragStartPos = LayoutPoint();
  m_fakeMouseMoveEventTimer.stop();
}

MouseEventManager::~MouseEventManager() = default;

DEFINE_TRACE(MouseEventManager) {
  visitor->trace(m_frame);
  visitor->trace(m_scrollManager);
  visitor->trace(m_nodeUnderMouse);
  visitor->trace(m_mousePressNode);
  visitor->trace(m_clickNode);
  SynchronousMutationObserver::trace(visitor);
}

MouseEventManager::MouseEventBoundaryEventDispatcher::
    MouseEventBoundaryEventDispatcher(
        MouseEventManager* mouseEventManager,
        const PlatformMouseEvent* platformMouseEvent,
        EventTarget* exitedTarget)
    : m_mouseEventManager(mouseEventManager),
      m_platformMouseEvent(platformMouseEvent),
      m_exitedTarget(exitedTarget) {}

void MouseEventManager::MouseEventBoundaryEventDispatcher::dispatchOut(
    EventTarget* target,
    EventTarget* relatedTarget) {
  dispatch(
      target, relatedTarget, EventTypeNames::mouseout,
      mouseEventWithRegion(m_exitedTarget->toNode(), *m_platformMouseEvent),
      false);
}

void MouseEventManager::MouseEventBoundaryEventDispatcher::dispatchOver(
    EventTarget* target,
    EventTarget* relatedTarget) {
  dispatch(target, relatedTarget, EventTypeNames::mouseover,
           *m_platformMouseEvent, false);
}

void MouseEventManager::MouseEventBoundaryEventDispatcher::dispatchLeave(
    EventTarget* target,
    EventTarget* relatedTarget,
    bool checkForListener) {
  dispatch(
      target, relatedTarget, EventTypeNames::mouseleave,
      mouseEventWithRegion(m_exitedTarget->toNode(), *m_platformMouseEvent),
      checkForListener);
}

void MouseEventManager::MouseEventBoundaryEventDispatcher::dispatchEnter(
    EventTarget* target,
    EventTarget* relatedTarget,
    bool checkForListener) {
  dispatch(target, relatedTarget, EventTypeNames::mouseenter,
           *m_platformMouseEvent, checkForListener);
}

AtomicString
MouseEventManager::MouseEventBoundaryEventDispatcher::getLeaveEvent() {
  return EventTypeNames::mouseleave;
}

AtomicString
MouseEventManager::MouseEventBoundaryEventDispatcher::getEnterEvent() {
  return EventTypeNames::mouseenter;
}

void MouseEventManager::MouseEventBoundaryEventDispatcher::dispatch(
    EventTarget* target,
    EventTarget* relatedTarget,
    const AtomicString& type,
    const PlatformMouseEvent& platformMouseEvent,
    bool checkForListener) {
  m_mouseEventManager->dispatchMouseEvent(target, type, platformMouseEvent,
                                          relatedTarget, checkForListener);
}

void MouseEventManager::sendBoundaryEvents(
    EventTarget* exitedTarget,
    EventTarget* enteredTarget,
    const PlatformMouseEvent& mousePlatformEvent) {
  MouseEventBoundaryEventDispatcher boundaryEventDispatcher(
      this, &mousePlatformEvent, exitedTarget);
  boundaryEventDispatcher.sendBoundaryEvents(exitedTarget, enteredTarget);
}

WebInputEventResult MouseEventManager::dispatchMouseEvent(
    EventTarget* target,
    const AtomicString& mouseEventType,
    const PlatformMouseEvent& mouseEvent,
    EventTarget* relatedTarget,
    bool checkForListener) {
  if (target && target->toNode() &&
      (!checkForListener || target->hasEventListeners(mouseEventType))) {
    Node* targetNode = target->toNode();
    int clickCount = 0;
    if (mouseEventType == EventTypeNames::mouseup ||
        mouseEventType == EventTypeNames::mousedown ||
        mouseEventType == EventTypeNames::click ||
        mouseEventType == EventTypeNames::auxclick ||
        mouseEventType == EventTypeNames::dblclick) {
      clickCount = m_clickCount;
    }
    MouseEvent* event = MouseEvent::create(
        mouseEventType, targetNode->document().domWindow(), mouseEvent,
        clickCount, relatedTarget ? relatedTarget->toNode() : nullptr);
    DispatchEventResult dispatchResult = target->dispatchEvent(event);
    return EventHandlingUtil::toWebInputEventResult(dispatchResult);
  }
  return WebInputEventResult::NotHandled;
}

WebInputEventResult MouseEventManager::setMousePositionAndDispatchMouseEvent(
    Node* targetNode,
    const AtomicString& eventType,
    const PlatformMouseEvent& platformMouseEvent) {
  // If the target node is a text node, dispatch on the parent node.
  if (targetNode && targetNode->isTextNode())
    targetNode = FlatTreeTraversal::parent(*targetNode);

  setNodeUnderMouse(targetNode, platformMouseEvent);

  return dispatchMouseEvent(m_nodeUnderMouse, eventType, platformMouseEvent,
                            nullptr);
}

WebInputEventResult MouseEventManager::dispatchMouseClickIfNeeded(
    const MouseEventWithHitTestResults& mev) {
  // We only prevent click event when the click may cause contextmenu to popup.
  // However, we always send auxclick.
  bool contextMenuEvent = !RuntimeEnabledFeatures::auxclickEnabled() &&
                          mev.event().pointerProperties().button ==
                              WebPointerProperties::Button::Right;
#if OS(MACOSX)
  // FIXME: The Mac port achieves the same behavior by checking whether the
  // context menu is currently open in WebPage::mouseEvent(). Consider merging
  // the implementations.
  if (mev.event().pointerProperties().button ==
          WebPointerProperties::Button::Left &&
      mev.event().getModifiers() & PlatformEvent::CtrlKey)
    contextMenuEvent = true;
#endif

  WebInputEventResult clickEventResult = WebInputEventResult::NotHandled;
  const bool shouldDispatchClickEvent =
      m_clickCount > 0 && !contextMenuEvent && mev.innerNode() && m_clickNode &&
      mev.innerNode()->canParticipateInFlatTree() &&
      m_clickNode->canParticipateInFlatTree() &&
      !(m_frame->eventHandler().selectionController().hasExtendedSelection() &&
        isLinkSelection(mev));
  if (shouldDispatchClickEvent) {
    Node* clickTargetNode = nullptr;
    // Updates distribution because a 'mouseup' event listener can make the
    // tree dirty at dispatchMouseEvent() invocation above.
    // Unless distribution is updated, commonAncestor would hit ASSERT.
    if (m_clickNode == mev.innerNode()) {
      clickTargetNode = m_clickNode;
      clickTargetNode->updateDistribution();
    } else if (m_clickNode->document() == mev.innerNode()->document()) {
      m_clickNode->updateDistribution();
      mev.innerNode()->updateDistribution();
      clickTargetNode = mev.innerNode()->commonAncestor(
          *m_clickNode, EventHandlingUtil::parentForClickEvent);
    }
    if (clickTargetNode) {
      clickEventResult = dispatchMouseEvent(
          clickTargetNode, !RuntimeEnabledFeatures::auxclickEnabled() ||
                                   (mev.event().pointerProperties().button ==
                                    WebPointerProperties::Button::Left)
                               ? EventTypeNames::click
                               : EventTypeNames::auxclick,
          mev.event(), nullptr);
    }
  }
  return clickEventResult;
}

void MouseEventManager::fakeMouseMoveEventTimerFired(TimerBase* timer) {
  TRACE_EVENT0("input", "MouseEventManager::fakeMouseMoveEventTimerFired");
  DCHECK(timer == &m_fakeMouseMoveEventTimer);
  DCHECK(!m_mousePressed);

  if (m_isMousePositionUnknown)
    return;

  FrameView* view = m_frame->view();
  if (!view)
    return;

  if (!m_frame->page() || !m_frame->page()->focusController().isActive())
    return;

  // Don't dispatch a synthetic mouse move event if the mouse cursor is not
  // visible to the user.
  if (!m_frame->page()->isCursorVisible())
    return;

  PlatformMouseEvent fakeMouseMoveEvent(
      m_lastKnownMousePosition, m_lastKnownMouseGlobalPosition,
      WebPointerProperties::Button::NoButton, PlatformEvent::MouseMoved, 0,
      static_cast<PlatformEvent::Modifiers>(
          KeyboardEventManager::getCurrentModifierState()),
      PlatformMouseEvent::RealOrIndistinguishable, TimeTicks::Now(),
      WebPointerProperties::PointerType::Mouse);
  Vector<PlatformMouseEvent> coalescedEvents;
  m_frame->eventHandler().handleMouseMoveEvent(fakeMouseMoveEvent,
                                               coalescedEvents);
}

void MouseEventManager::cancelFakeMouseMoveEvent() {
  m_fakeMouseMoveEventTimer.stop();
}

void MouseEventManager::setNodeUnderMouse(
    Node* target,
    const PlatformMouseEvent& platformMouseEvent) {
  Node* lastNodeUnderMouse = m_nodeUnderMouse;
  m_nodeUnderMouse = target;

  PaintLayer* layerForLastNode =
      EventHandlingUtil::layerForNode(lastNodeUnderMouse);
  PaintLayer* layerForNodeUnderMouse =
      EventHandlingUtil::layerForNode(m_nodeUnderMouse.get());
  Page* page = m_frame->page();

  if (lastNodeUnderMouse &&
      (!m_nodeUnderMouse ||
       m_nodeUnderMouse->document() != m_frame->document())) {
    // The mouse has moved between frames.
    if (LocalFrame* frame = lastNodeUnderMouse->document().frame()) {
      if (FrameView* frameView = frame->view())
        frameView->mouseExitedContentArea();
    }
  } else if (page && (layerForLastNode &&
                      (!layerForNodeUnderMouse ||
                       layerForNodeUnderMouse != layerForLastNode))) {
    // The mouse has moved between layers.
    if (ScrollableArea* scrollableAreaForLastNode =
            EventHandlingUtil::associatedScrollableArea(layerForLastNode))
      scrollableAreaForLastNode->mouseExitedContentArea();
  }

  if (m_nodeUnderMouse &&
      (!lastNodeUnderMouse ||
       lastNodeUnderMouse->document() != m_frame->document())) {
    // The mouse has moved between frames.
    if (LocalFrame* frame = m_nodeUnderMouse->document().frame()) {
      if (FrameView* frameView = frame->view())
        frameView->mouseEnteredContentArea();
    }
  } else if (page && (layerForNodeUnderMouse &&
                      (!layerForLastNode ||
                       layerForNodeUnderMouse != layerForLastNode))) {
    // The mouse has moved between layers.
    if (ScrollableArea* scrollableAreaForNodeUnderMouse =
            EventHandlingUtil::associatedScrollableArea(layerForNodeUnderMouse))
      scrollableAreaForNodeUnderMouse->mouseEnteredContentArea();
  }

  if (lastNodeUnderMouse &&
      lastNodeUnderMouse->document() != m_frame->document()) {
    lastNodeUnderMouse = nullptr;
  }

  sendBoundaryEvents(lastNodeUnderMouse, m_nodeUnderMouse, platformMouseEvent);
}

void MouseEventManager::nodeChildrenWillBeRemoved(ContainerNode& container) {
  if (container == m_clickNode)
    return;
  if (!container.isShadowIncludingInclusiveAncestorOf(m_clickNode.get()))
    return;
  m_clickNode = nullptr;
}

void MouseEventManager::nodeWillBeRemoved(Node& nodeToBeRemoved) {
  if (nodeToBeRemoved.isShadowIncludingInclusiveAncestorOf(m_clickNode.get())) {
    // We don't dispatch click events if the mousedown node is removed
    // before a mouseup event. It is compatible with IE and Firefox.
    m_clickNode = nullptr;
  }
}

Node* MouseEventManager::getNodeUnderMouse() {
  return m_nodeUnderMouse;
}

WebInputEventResult MouseEventManager::handleMouseFocus(
    const HitTestResult& hitTestResult,
    InputDeviceCapabilities* sourceCapabilities) {
  // If clicking on a frame scrollbar, do not mess up with content focus.
  if (hitTestResult.scrollbar() && !m_frame->contentLayoutItem().isNull()) {
    if (hitTestResult.scrollbar()->getScrollableArea() ==
        m_frame->contentLayoutItem().getScrollableArea())
      return WebInputEventResult::NotHandled;
  }

  // The layout needs to be up to date to determine if an element is focusable.
  m_frame->document()->updateStyleAndLayoutIgnorePendingStylesheets();

  Element* element = nullptr;
  if (m_nodeUnderMouse) {
    element = m_nodeUnderMouse->isElementNode()
                  ? toElement(m_nodeUnderMouse)
                  : m_nodeUnderMouse->parentOrShadowHostElement();
  }
  for (; element; element = element->parentOrShadowHostElement()) {
    if (element->isFocusable() && element->isFocusedElementInDocument())
      return WebInputEventResult::NotHandled;
    if (element->isMouseFocusable())
      break;
  }
  DCHECK(!element || element->isMouseFocusable());

  // To fix <rdar://problem/4895428> Can't drag selected ToDo, we don't focus
  // a node on mouse down if it's selected and inside a focused node. It will
  // be focused if the user does a mouseup over it, however, because the
  // mouseup will set a selection inside it, which will call
  // FrameSelection::setFocusedNodeIfNeeded.
  if (element && m_frame->selection().isRange()) {
    // TODO(yosin) We should not create |Range| object for calling
    // |isNodeFullyContained()|.
    if (createRange(
            m_frame->selection().selection().toNormalizedEphemeralRange())
            ->isNodeFullyContained(*element) &&
        element->isDescendantOf(m_frame->document()->focusedElement()))
      return WebInputEventResult::NotHandled;
  }

  // Only change the focus when clicking scrollbars if it can transfered to a
  // mouse focusable node.
  if (!element && hitTestResult.scrollbar())
    return WebInputEventResult::HandledSystem;

  if (Page* page = m_frame->page()) {
    // If focus shift is blocked, we eat the event. Note we should never
    // clear swallowEvent if the page already set it (e.g., by canceling
    // default behavior).
    if (element) {
      if (slideFocusOnShadowHostIfNecessary(*element))
        return WebInputEventResult::HandledSystem;
      if (!page->focusController().setFocusedElement(
              element, m_frame,
              FocusParams(SelectionBehaviorOnFocus::None, WebFocusTypeMouse,
                          sourceCapabilities)))
        return WebInputEventResult::HandledSystem;
    } else {
      // We call setFocusedElement even with !element in order to blur
      // current focus element when a link is clicked; this is expected by
      // some sites that rely on onChange handlers running from form
      // fields before the button click is processed.
      if (!page->focusController().setFocusedElement(
              nullptr, m_frame,
              FocusParams(SelectionBehaviorOnFocus::None, WebFocusTypeNone,
                          sourceCapabilities)))
        return WebInputEventResult::HandledSystem;
    }
  }

  return WebInputEventResult::NotHandled;
}

bool MouseEventManager::slideFocusOnShadowHostIfNecessary(
    const Element& element) {
  if (element.authorShadowRoot() &&
      element.authorShadowRoot()->delegatesFocus()) {
    Document* doc = m_frame->document();
    if (element.isShadowIncludingInclusiveAncestorOf(doc->focusedElement())) {
      // If the inner element is already focused, do nothing.
      return true;
    }

    // If the host has a focusable inner element, focus it. Otherwise, the host
    // takes focus.
    Page* page = m_frame->page();
    DCHECK(page);
    Element* found =
        page->focusController().findFocusableElementInShadowHost(element);
    if (found && element.isShadowIncludingInclusiveAncestorOf(found)) {
      // Use WebFocusTypeForward instead of WebFocusTypeMouse here to mean the
      // focus has slided.
      found->focus(FocusParams(SelectionBehaviorOnFocus::Reset,
                               WebFocusTypeForward, nullptr));
      return true;
    }
  }
  return false;
}

void MouseEventManager::handleMousePressEventUpdateStates(
    const PlatformMouseEvent& mouseEvent) {
  cancelFakeMouseMoveEvent();
  m_mousePressed = true;
  m_capturesDragging = true;
  setLastKnownMousePosition(mouseEvent);
  m_mouseDownMayStartDrag = false;
  m_mouseDownMayStartAutoscroll = false;
  m_mouseDownTimestamp = mouseEvent.timestamp();

  if (FrameView* view = m_frame->view()) {
    m_mouseDownPos = view->rootFrameToContents(mouseEvent.position());
  } else {
    invalidateClick();
  }
}

bool MouseEventManager::isMousePositionUnknown() {
  return m_isMousePositionUnknown;
}

IntPoint MouseEventManager::lastKnownMousePosition() {
  return m_lastKnownMousePosition;
}

void MouseEventManager::setLastKnownMousePosition(
    const PlatformMouseEvent& event) {
  m_isMousePositionUnknown = false;
  m_lastKnownMousePosition = event.position();
  m_lastKnownMouseGlobalPosition = event.globalPosition();
}

void MouseEventManager::dispatchFakeMouseMoveEventSoon() {
  if (m_mousePressed)
    return;

  if (m_isMousePositionUnknown)
    return;

  // Reschedule the timer, to prevent dispatching mouse move events
  // during a scroll. This avoids a potential source of scroll jank.
  m_fakeMouseMoveEventTimer.startOneShot(kFakeMouseMoveInterval,
                                         BLINK_FROM_HERE);
}

void MouseEventManager::dispatchFakeMouseMoveEventSoonInQuad(
    const FloatQuad& quad) {
  FrameView* view = m_frame->view();
  if (!view)
    return;

  if (!quad.containsPoint(view->rootFrameToContents(m_lastKnownMousePosition)))
    return;

  dispatchFakeMouseMoveEventSoon();
}

WebInputEventResult MouseEventManager::handleMousePressEvent(
    const MouseEventWithHitTestResults& event) {
  TRACE_EVENT0("blink", "MouseEventManager::handleMousePressEvent");

  // Reset drag state.
  dragState().m_dragSrc = nullptr;

  cancelFakeMouseMoveEvent();

  m_frame->document()->updateStyleAndLayoutIgnorePendingStylesheets();

  if (FrameView* frameView = m_frame->view()) {
    if (frameView->isPointInScrollbarCorner(event.event().position()))
      return WebInputEventResult::NotHandled;
  }

  bool singleClick = event.event().clickCount() <= 1;

  m_mouseDownMayStartDrag =
      singleClick && !isLinkSelection(event) && !isExtendingSelection(event);

  m_frame->eventHandler().selectionController().handleMousePressEvent(event);

  m_mouseDown = event.event();

  if (m_frame->document()->isSVGDocument() &&
      m_frame->document()->accessSVGExtensions().zoomAndPanEnabled()) {
    if (event.event().shiftKey() && singleClick) {
      m_svgPan = true;
      m_frame->document()->accessSVGExtensions().startPan(
          m_frame->view()->rootFrameToContents(event.event().position()));
      return WebInputEventResult::HandledSystem;
    }
  }

  // We don't do this at the start of mouse down handling,
  // because we don't want to do it until we know we didn't hit a widget.
  if (singleClick)
    focusDocumentView();

  Node* innerNode = event.innerNode();

  m_mousePressNode = innerNode;
  m_frame->document()->setSequentialFocusNavigationStartingPoint(innerNode);
  m_dragStartPos = event.event().position();

  bool swallowEvent = false;
  m_mousePressed = true;

  if (event.event().clickCount() == 2) {
    swallowEvent = m_frame->eventHandler()
                       .selectionController()
                       .handleMousePressEventDoubleClick(event);
  } else if (event.event().clickCount() >= 3) {
    swallowEvent = m_frame->eventHandler()
                       .selectionController()
                       .handleMousePressEventTripleClick(event);
  } else {
    swallowEvent = m_frame->eventHandler()
                       .selectionController()
                       .handleMousePressEventSingleClick(event);
  }

  m_mouseDownMayStartAutoscroll =
      m_frame->eventHandler().selectionController().mouseDownMayStartSelect() ||
      (m_mousePressNode && m_mousePressNode->layoutBox() &&
       m_mousePressNode->layoutBox()->canBeProgramaticallyScrolled());

  return swallowEvent ? WebInputEventResult::HandledSystem
                      : WebInputEventResult::NotHandled;
}

WebInputEventResult MouseEventManager::handleMouseReleaseEvent(
    const MouseEventWithHitTestResults& event) {
  AutoscrollController* controller = m_scrollManager->autoscrollController();
  if (controller && controller->autoscrollInProgress())
    m_scrollManager->stopAutoscroll();

  return m_frame->eventHandler().selectionController().handleMouseReleaseEvent(
             event, m_dragStartPos)
             ? WebInputEventResult::HandledSystem
             : WebInputEventResult::NotHandled;
}

void MouseEventManager::updateSelectionForMouseDrag() {
  m_frame->eventHandler().selectionController().updateSelectionForMouseDrag(
      m_mousePressNode, m_dragStartPos, m_lastKnownMousePosition);
}

bool MouseEventManager::handleDragDropIfPossible(
    const GestureEventWithHitTestResults& targetedEvent) {
  if (m_frame->settings() && m_frame->settings()->getTouchDragDropEnabled() &&
      m_frame->view()) {
    const WebGestureEvent& gestureEvent = targetedEvent.event();
    unsigned modifiers = gestureEvent.modifiers();

    // TODO(mustaq): Suppressing long-tap MouseEvents could break
    // drag-drop. Will do separately because of the risk. crbug.com/606938.
    PlatformMouseEvent mouseDownEvent(
        gestureEvent, WebPointerProperties::Button::Left,
        PlatformEvent::MousePressed, 1,
        static_cast<PlatformEvent::Modifiers>(modifiers |
                                              PlatformEvent::LeftButtonDown),
        PlatformMouseEvent::FromTouch, TimeTicks::Now(),
        WebPointerProperties::PointerType::Mouse);
    m_mouseDown = mouseDownEvent;

    PlatformMouseEvent mouseDragEvent(
        gestureEvent, WebPointerProperties::Button::Left,
        PlatformEvent::MouseMoved, 1,
        static_cast<PlatformEvent::Modifiers>(modifiers |
                                              PlatformEvent::LeftButtonDown),
        PlatformMouseEvent::FromTouch, TimeTicks::Now(),
        WebPointerProperties::PointerType::Mouse);
    HitTestRequest request(HitTestRequest::ReadOnly);
    MouseEventWithHitTestResults mev =
        EventHandlingUtil::performMouseEventHitTest(m_frame, request,
                                                    mouseDragEvent);
    m_mouseDownMayStartDrag = true;
    dragState().m_dragSrc = nullptr;
    m_mouseDownPos =
        m_frame->view()->rootFrameToContents(mouseDragEvent.position());
    return handleDrag(mev, DragInitiator::Touch);
  }
  return false;
}

void MouseEventManager::focusDocumentView() {
  Page* page = m_frame->page();
  if (!page)
    return;
  page->focusController().focusDocumentView(m_frame);
}

WebInputEventResult MouseEventManager::handleMouseDraggedEvent(
    const MouseEventWithHitTestResults& event) {
  TRACE_EVENT0("blink", "MouseEventManager::handleMouseDraggedEvent");

  // While resetting m_mousePressed here may seem out of place, it turns out
  // to be needed to handle some bugs^Wfeatures in Blink mouse event handling:
  // 1. Certain elements, such as <embed>, capture mouse events. They do not
  //    bubble back up. One way for a <embed> to start capturing mouse events
  //    is on a mouse press. The problem is the <embed> node only starts
  //    capturing mouse events *after* m_mousePressed for the containing frame
  //    has already been set to true. As a result, the frame's EventHandler
  //    never sees the mouse release event, which is supposed to reset
  //    m_mousePressed... so m_mousePressed ends up remaining true until the
  //    event handler finally gets another mouse released event. Oops.
  // 2. Dragging doesn't start until after a mouse press event, but a drag
  //    that ends as a result of a mouse release does not send a mouse release
  //    event. As a result, m_mousePressed also ends up remaining true until
  //    the next mouse release event seen by the EventHandler.
  if (event.event().pointerProperties().button !=
      WebPointerProperties::Button::Left)
    m_mousePressed = false;

  if (!m_mousePressed)
    return WebInputEventResult::NotHandled;

  if (handleDrag(event, DragInitiator::Mouse))
    return WebInputEventResult::HandledSystem;

  Node* targetNode = event.innerNode();
  if (!targetNode)
    return WebInputEventResult::NotHandled;

  LayoutObject* layoutObject = targetNode->layoutObject();
  if (!layoutObject) {
    Node* parent = FlatTreeTraversal::parent(*targetNode);
    if (!parent)
      return WebInputEventResult::NotHandled;

    layoutObject = parent->layoutObject();
    if (!layoutObject || !layoutObject->isListBox())
      return WebInputEventResult::NotHandled;
  }

  m_mouseDownMayStartDrag = false;

  m_frame->eventHandler().selectionController().handleMouseDraggedEvent(
      event, m_mouseDownPos, m_dragStartPos, m_mousePressNode.get(),
      m_lastKnownMousePosition);

  // The call into handleMouseDraggedEvent may have caused a re-layout,
  // so get the LayoutObject again.
  layoutObject = targetNode->layoutObject();

  if (layoutObject && m_mouseDownMayStartAutoscroll &&
      !m_scrollManager->middleClickAutoscrollInProgress() &&
      !m_frame->selection().selectedHTMLForClipboard().isEmpty()) {
    if (AutoscrollController* controller =
            m_scrollManager->autoscrollController()) {
      controller->startAutoscrollForSelection(layoutObject);
      m_mouseDownMayStartAutoscroll = false;
    }
  }

  return WebInputEventResult::HandledSystem;
}

bool MouseEventManager::handleDrag(const MouseEventWithHitTestResults& event,
                                   DragInitiator initiator) {
  DCHECK(event.event().type() == PlatformEvent::MouseMoved);
  // Callers must protect the reference to FrameView, since this function may
  // dispatch DOM events, causing page/FrameView to go away.
  DCHECK(m_frame);
  DCHECK(m_frame->view());
  if (!m_frame->page())
    return false;

  if (m_mouseDownMayStartDrag) {
    HitTestRequest request(HitTestRequest::ReadOnly);
    HitTestResult result(request, m_mouseDownPos);
    m_frame->contentLayoutItem().hitTest(result);
    Node* node = result.innerNode();
    if (node) {
      DragController::SelectionDragPolicy selectionDragPolicy =
          event.event().timestamp() - m_mouseDownTimestamp < kTextDragDelay
              ? DragController::DelayedSelectionDragResolution
              : DragController::ImmediateSelectionDragResolution;
      dragState().m_dragSrc = m_frame->page()->dragController().draggableNode(
          m_frame, node, m_mouseDownPos, selectionDragPolicy,
          dragState().m_dragType);
    } else {
      dragState().m_dragSrc = nullptr;
    }

    if (!dragState().m_dragSrc)
      m_mouseDownMayStartDrag = false;  // no element is draggable
  }

  if (!m_mouseDownMayStartDrag) {
    return initiator == DragInitiator::Mouse &&
           !m_frame->eventHandler()
                .selectionController()
                .mouseDownMayStartSelect() &&
           !m_mouseDownMayStartAutoscroll;
  }

  // We are starting a text/image/url drag, so the cursor should be an arrow
  // FIXME <rdar://7577595>: Custom cursors aren't supported during drag and
  // drop (default to pointer).
  m_frame->view()->setCursor(pointerCursor());

  if (initiator == DragInitiator::Mouse &&
      !dragThresholdExceeded(event.event().position())) {
    dragState().m_dragSrc = nullptr;
    return true;
  }

  // Once we're past the drag threshold, we don't want to treat this gesture as
  // a click.
  invalidateClick();

  if (!tryStartDrag(event)) {
    // Something failed to start the drag, clean up.
    clearDragDataTransfer();
    dragState().m_dragSrc = nullptr;
  }

  m_mouseDownMayStartDrag = false;
  // Whether or not the drag actually started, no more default handling (like
  // selection).
  return true;
}

DataTransfer* MouseEventManager::createDraggingDataTransfer() const {
  return DataTransfer::create(DataTransfer::DragAndDrop, DataTransferWritable,
                              DataObject::create());
}

bool MouseEventManager::tryStartDrag(
    const MouseEventWithHitTestResults& event) {
  // The DataTransfer would only be non-empty if we missed a dragEnd.
  // Clear it anyway, just to make sure it gets numbified.
  clearDragDataTransfer();

  dragState().m_dragDataTransfer = createDraggingDataTransfer();

  DragController& dragController = m_frame->page()->dragController();
  if (!dragController.populateDragDataTransfer(m_frame, dragState(),
                                               m_mouseDownPos))
    return false;

  // If dispatching dragstart brings about another mouse down -- one way
  // this will happen is if a DevTools user breaks within a dragstart
  // handler and then clicks on the suspended page -- the drag state is
  // reset. Hence, need to check if this particular drag operation can
  // continue even if dispatchEvent() indicates no (direct) cancellation.
  // Do that by checking if m_dragSrc is still set.
  m_mouseDownMayStartDrag =
      dispatchDragSrcEvent(EventTypeNames::dragstart, m_mouseDown) ==
          WebInputEventResult::NotHandled &&
      !m_frame->selection().isInPasswordField() && dragState().m_dragSrc;

  // Invalidate clipboard here against anymore pasteboard writing for security.
  // The drag image can still be changed as we drag, but not the pasteboard
  // data.
  dragState().m_dragDataTransfer->setAccessPolicy(DataTransferImageWritable);

  if (m_mouseDownMayStartDrag) {
    // Dispatching the event could cause Page to go away. Make sure it's still
    // valid before trying to use DragController.
    if (m_frame->page() &&
        dragController.startDrag(m_frame, dragState(), event.event(),
                                 m_mouseDownPos))
      return true;
    // Drag was canned at the last minute - we owe m_dragSrc a DRAGEND event
    dispatchDragSrcEvent(EventTypeNames::dragend, event.event());
  }

  return false;
}

// Returns if we should continue "default processing", i.e., whether
// eventhandler canceled.
WebInputEventResult MouseEventManager::dispatchDragSrcEvent(
    const AtomicString& eventType,
    const PlatformMouseEvent& event) {
  return dispatchDragEvent(eventType, dragState().m_dragSrc.get(), event,
                           dragState().m_dragDataTransfer.get());
}

WebInputEventResult MouseEventManager::dispatchDragEvent(
    const AtomicString& eventType,
    Node* dragTarget,
    const PlatformMouseEvent& event,
    DataTransfer* dataTransfer) {
  FrameView* view = m_frame->view();

  // FIXME: We might want to dispatch a dragleave even if the view is gone.
  if (!view)
    return WebInputEventResult::NotHandled;

  const bool cancelable = eventType != EventTypeNames::dragleave &&
                          eventType != EventTypeNames::dragend;

  DragEvent* me = DragEvent::create(
      eventType, true, cancelable, m_frame->document()->domWindow(), 0,
      event.globalPosition().x(), event.globalPosition().y(),
      event.position().x(), event.position().y(), event.movementDelta().x(),
      event.movementDelta().y(), event.getModifiers(), 0,
      MouseEvent::platformModifiersToButtons(event.getModifiers()), nullptr,
      event.timestamp(), dataTransfer, event.getSyntheticEventType());

  return EventHandlingUtil::toWebInputEventResult(
      dragTarget->dispatchEvent(me));
}

void MouseEventManager::clearDragDataTransfer() {
  if (dragState().m_dragDataTransfer) {
    dragState().m_dragDataTransfer->clearDragImage();
    dragState().m_dragDataTransfer->setAccessPolicy(DataTransferNumb);
  }
}

void MouseEventManager::dragSourceEndedAt(const PlatformMouseEvent& event,
                                          DragOperation operation) {
  if (dragState().m_dragSrc) {
    dragState().m_dragDataTransfer->setDestinationOperation(operation);
    // The return value is ignored because dragend is not cancelable.
    dispatchDragSrcEvent(EventTypeNames::dragend, event);
  }
  clearDragDataTransfer();
  dragState().m_dragSrc = nullptr;
  // In case the drag was ended due to an escape key press we need to ensure
  // that consecutive mousemove events don't reinitiate the drag and drop.
  m_mouseDownMayStartDrag = false;
}

DragState& MouseEventManager::dragState() {
  DEFINE_STATIC_LOCAL(DragState, state, (new DragState));
  return state;
}

bool MouseEventManager::dragThresholdExceeded(
    const IntPoint& dragLocationInRootFrame) const {
  FrameView* view = m_frame->view();
  if (!view)
    return false;
  IntPoint dragLocation = view->rootFrameToContents(dragLocationInRootFrame);
  IntSize delta = dragLocation - m_mouseDownPos;

  // WebKit's drag thresholds depend on the type of object being dragged. If we
  // want to revive that behavior, we can multiply the threshold constants with
  // a number based on dragState().m_dragType.

  return abs(delta.width()) >= kDragThresholdX ||
         abs(delta.height()) >= kDragThresholdY;
}

void MouseEventManager::clearDragHeuristicState() {
  // Used to prevent mouseMoveEvent from initiating a drag before
  // the mouse is pressed again.
  m_mousePressed = false;
  m_capturesDragging = false;
  m_mouseDownMayStartDrag = false;
  m_mouseDownMayStartAutoscroll = false;
}

bool MouseEventManager::handleSvgPanIfNeeded(bool isReleaseEvent) {
  if (!m_svgPan)
    return false;
  m_svgPan = !isReleaseEvent;
  m_frame->document()->accessSVGExtensions().updatePan(
      m_frame->view()->rootFrameToContents(m_lastKnownMousePosition));
  return true;
}

void MouseEventManager::invalidateClick() {
  m_clickCount = 0;
  m_clickNode = nullptr;
}

bool MouseEventManager::mousePressed() {
  return m_mousePressed;
}

void MouseEventManager::setMousePressed(bool mousePressed) {
  m_mousePressed = mousePressed;
}

bool MouseEventManager::capturesDragging() const {
  return m_capturesDragging;
}

void MouseEventManager::setCapturesDragging(bool capturesDragging) {
  m_capturesDragging = capturesDragging;
}

Node* MouseEventManager::mousePressNode() {
  return m_mousePressNode;
}

void MouseEventManager::setMousePressNode(Node* node) {
  m_mousePressNode = node;
}

void MouseEventManager::setClickNode(Node* node) {
  setContext(node ? node->ownerDocument() : nullptr);
  m_clickNode = node;
}

void MouseEventManager::setClickCount(int clickCount) {
  m_clickCount = clickCount;
}

bool MouseEventManager::mouseDownMayStartDrag() {
  return m_mouseDownMayStartDrag;
}

}  // namespace blink