1 | /* |
2 | * Copyright (C) 2016 Apple Inc. All rights reserved. |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without |
5 | * modification, are permitted provided that the following conditions |
6 | * are met: |
7 | * 1. Redistributions of source code must retain the above copyright |
8 | * notice, this list of conditions and the following disclaimer. |
9 | * 2. Redistributions in binary form must reproduce the above copyright |
10 | * notice, this list of conditions and the following disclaimer in the |
11 | * documentation and/or other materials provided with the distribution. |
12 | * |
13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #include "config.h" |
27 | #include "SimulatedClick.h" |
28 | |
29 | #include "DOMRect.h" |
30 | #include "DataTransfer.h" |
31 | #include "Element.h" |
32 | #include "EventNames.h" |
33 | #include "MouseEvent.h" |
34 | #include <wtf/IsoMallocInlines.h> |
35 | #include <wtf/NeverDestroyed.h> |
36 | |
37 | namespace WebCore { |
38 | |
39 | class SimulatedMouseEvent final : public MouseEvent { |
40 | WTF_MAKE_ISO_ALLOCATED_INLINE(SimulatedMouseEvent); |
41 | public: |
42 | static Ref<SimulatedMouseEvent> create(const AtomicString& eventType, RefPtr<WindowProxy>&& view, RefPtr<Event>&& underlyingEvent, Element& target, SimulatedClickSource source) |
43 | { |
44 | return adoptRef(*new SimulatedMouseEvent(eventType, WTFMove(view), WTFMove(underlyingEvent), target, source)); |
45 | } |
46 | |
47 | private: |
48 | SimulatedMouseEvent(const AtomicString& eventType, RefPtr<WindowProxy>&& view, RefPtr<Event>&& underlyingEvent, Element& target, SimulatedClickSource source) |
49 | : MouseEvent(eventType, CanBubble::Yes, IsCancelable::Yes, IsComposed::Yes, |
50 | underlyingEvent ? underlyingEvent->timeStamp() : MonotonicTime::now(), WTFMove(view), /* detail */ 0, |
51 | { }, { }, { }, modifiersFromUnderlyingEvent(underlyingEvent), 0, 0, nullptr, 0, 0, nullptr, IsSimulated::Yes, |
52 | source == SimulatedClickSource::UserAgent ? IsTrusted::Yes : IsTrusted::No) |
53 | { |
54 | setUnderlyingEvent(underlyingEvent.get()); |
55 | |
56 | if (is<MouseEvent>(this->underlyingEvent())) { |
57 | MouseEvent& mouseEvent = downcast<MouseEvent>(*this->underlyingEvent()); |
58 | m_screenLocation = mouseEvent.screenLocation(); |
59 | initCoordinates(mouseEvent.clientLocation()); |
60 | } else if (source == SimulatedClickSource::UserAgent) { |
61 | // If there is no underlying event, we only populate the coordinates for events coming |
62 | // from the user agent (e.g. accessibility). For those coming from JavaScript (e.g. |
63 | // (element.click()), the coordinates will be 0, similarly to Firefox and Chrome. |
64 | // Note that the call to screenRect() causes a synchronous IPC with the UI process. |
65 | m_screenLocation = target.screenRect().center(); |
66 | initCoordinates(LayoutPoint(target.boundingClientRect().center())); |
67 | } |
68 | } |
69 | |
70 | static OptionSet<Modifier> modifiersFromUnderlyingEvent(const RefPtr<Event>& underlyingEvent) |
71 | { |
72 | UIEventWithKeyState* keyStateEvent = findEventWithKeyState(underlyingEvent.get()); |
73 | if (!keyStateEvent) |
74 | return { }; |
75 | return keyStateEvent->modifierKeys(); |
76 | } |
77 | }; |
78 | |
79 | static void simulateMouseEvent(const AtomicString& eventType, Element& element, Event* underlyingEvent, SimulatedClickSource source) |
80 | { |
81 | element.dispatchEvent(SimulatedMouseEvent::create(eventType, element.document().windowProxy(), underlyingEvent, element, source)); |
82 | } |
83 | |
84 | void simulateClick(Element& element, Event* underlyingEvent, SimulatedClickMouseEventOptions mouseEventOptions, SimulatedClickVisualOptions visualOptions, SimulatedClickSource creationOptions) |
85 | { |
86 | if (element.isDisabledFormControl()) |
87 | return; |
88 | |
89 | static NeverDestroyed<HashSet<Element*>> elementsDispatchingSimulatedClicks; |
90 | if (!elementsDispatchingSimulatedClicks.get().add(&element).isNewEntry) |
91 | return; |
92 | |
93 | if (mouseEventOptions == SendMouseOverUpDownEvents) |
94 | simulateMouseEvent(eventNames().mouseoverEvent, element, underlyingEvent, creationOptions); |
95 | |
96 | if (mouseEventOptions != SendNoEvents) |
97 | simulateMouseEvent(eventNames().mousedownEvent, element, underlyingEvent, creationOptions); |
98 | element.setActive(true, visualOptions == ShowPressedLook); |
99 | if (mouseEventOptions != SendNoEvents) |
100 | simulateMouseEvent(eventNames().mouseupEvent, element, underlyingEvent, creationOptions); |
101 | element.setActive(false); |
102 | |
103 | simulateMouseEvent(eventNames().clickEvent, element, underlyingEvent, creationOptions); |
104 | |
105 | elementsDispatchingSimulatedClicks.get().remove(&element); |
106 | } |
107 | |
108 | } // namespace WebCore |
109 | |