Force mouse events should go through normal mouse event handling code paths
[WebKit-https.git] / Source / WebCore / replay / UserInputBridge.cpp
1 /*
2  * Copyright (C) 2013 University of Washington. All rights reserved.
3  * Copyright (C) 2014 Apple Inc. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
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 THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
18  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY 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 "UserInputBridge.h"
30
31 #include "EventHandler.h"
32 #include "FocusController.h"
33 #include "Frame.h"
34 #include "FrameLoadRequest.h"
35 #include "MainFrame.h"
36 #include "Page.h"
37 #include "PlatformKeyboardEvent.h"
38 #include "PlatformMouseEvent.h"
39 #include "PlatformWheelEvent.h"
40
41 #if ENABLE(WEB_REPLAY)
42 #include "ReplayController.h"
43 #include "SerializationMethods.h"
44 #include "WebReplayInputs.h"
45 #include <replay/InputCursor.h>
46 #endif
47
48 #define EARLY_RETURN_IF_SHOULD_IGNORE_INPUT \
49     do { \
50         if (inputSource == InputSource::User && m_state == UserInputBridge::State::Replaying) \
51             return true; \
52     } while (false)
53
54 namespace WebCore {
55
56 UserInputBridge::UserInputBridge(Page& page)
57     : m_page(page)
58 #if ENABLE(WEB_REPLAY)
59     , m_state(UserInputBridge::State::Open)
60 #endif
61 {
62 }
63
64 #if ENABLE(WEB_REPLAY)
65 InputCursor& UserInputBridge::activeCursor() const
66 {
67     return m_page.replayController().activeInputCursor();
68 }
69 #endif
70
71 #if ENABLE(CONTEXT_MENUS)
72 bool UserInputBridge::handleContextMenuEvent(const PlatformMouseEvent& mouseEvent, const Frame* frame, InputSource)
73 {
74     return frame->eventHandler().sendContextMenuEvent(mouseEvent);
75 }
76 #endif
77
78 bool UserInputBridge::handleMousePressEvent(const PlatformMouseEvent& mouseEvent, InputSource inputSource)
79 {
80 #if ENABLE(WEB_REPLAY)
81     EARLY_RETURN_IF_SHOULD_IGNORE_INPUT;
82
83     InputCursor& cursor = activeCursor();
84     if (cursor.isCapturing()) {
85         std::unique_ptr<PlatformMouseEvent> ownedEvent = std::make_unique<PlatformMouseEvent>(mouseEvent);
86         cursor.appendInput<HandleMousePress>(WTF::move(ownedEvent));
87     }
88     EventLoopInputExtent extent(cursor);
89 #else
90     UNUSED_PARAM(inputSource);
91 #endif
92
93     return m_page.mainFrame().eventHandler().handleMousePressEvent(mouseEvent);
94 }
95
96 bool UserInputBridge::handleMouseReleaseEvent(const PlatformMouseEvent& mouseEvent, InputSource inputSource)
97 {
98 #if ENABLE(WEB_REPLAY)
99     EARLY_RETURN_IF_SHOULD_IGNORE_INPUT;
100
101     InputCursor& cursor = activeCursor();
102     if (cursor.isCapturing()) {
103         std::unique_ptr<PlatformMouseEvent> ownedEvent = std::make_unique<PlatformMouseEvent>(mouseEvent);
104         cursor.appendInput<HandleMouseRelease>(WTF::move(ownedEvent));
105     }
106     EventLoopInputExtent extent(cursor);
107 #else
108     UNUSED_PARAM(inputSource);
109 #endif
110
111     return m_page.mainFrame().eventHandler().handleMouseReleaseEvent(mouseEvent);
112 }
113
114 bool UserInputBridge::handleMouseMoveEvent(const PlatformMouseEvent& mouseEvent, InputSource inputSource)
115 {
116 #if ENABLE(WEB_REPLAY)
117     EARLY_RETURN_IF_SHOULD_IGNORE_INPUT;
118
119     InputCursor& cursor = activeCursor();
120     if (cursor.isCapturing()) {
121         std::unique_ptr<PlatformMouseEvent> ownedEvent = std::make_unique<PlatformMouseEvent>(mouseEvent);
122         cursor.appendInput<HandleMouseMove>(WTF::move(ownedEvent), false);
123     }
124     EventLoopInputExtent extent(cursor);
125 #else
126     UNUSED_PARAM(inputSource);
127 #endif
128
129     return m_page.mainFrame().eventHandler().mouseMoved(mouseEvent);
130 }
131
132 bool UserInputBridge::handleMouseMoveOnScrollbarEvent(const PlatformMouseEvent& mouseEvent, InputSource inputSource)
133 {
134 #if ENABLE(WEB_REPLAY)
135     EARLY_RETURN_IF_SHOULD_IGNORE_INPUT;
136
137     InputCursor& cursor = activeCursor();
138     if (cursor.isCapturing()) {
139         std::unique_ptr<PlatformMouseEvent> ownedEvent = std::make_unique<PlatformMouseEvent>(mouseEvent);
140         cursor.appendInput<HandleMouseMove>(WTF::move(ownedEvent), true);
141     }
142     EventLoopInputExtent extent(cursor);
143 #else
144     UNUSED_PARAM(inputSource);
145 #endif
146
147     return m_page.mainFrame().eventHandler().passMouseMovedEventToScrollbars(mouseEvent);
148 }
149
150 bool UserInputBridge::handleMouseForceEvent(const PlatformMouseEvent& mouseEvent, InputSource)
151 {
152     return m_page.mainFrame().eventHandler().handleMouseForceEvent(mouseEvent);
153 }
154
155 bool UserInputBridge::handleKeyEvent(const PlatformKeyboardEvent& keyEvent, InputSource inputSource)
156 {
157 #if ENABLE(WEB_REPLAY)
158     EARLY_RETURN_IF_SHOULD_IGNORE_INPUT;
159
160     InputCursor& cursor = activeCursor();
161     if (cursor.isCapturing()) {
162         std::unique_ptr<PlatformKeyboardEvent> ownedEvent = std::make_unique<PlatformKeyboardEvent>(keyEvent);
163         cursor.appendInput<HandleKeyPress>(WTF::move(ownedEvent));
164     }
165     EventLoopInputExtent extent(cursor);
166 #else
167     UNUSED_PARAM(inputSource);
168 #endif
169
170     return m_page.focusController().focusedOrMainFrame().eventHandler().keyEvent(keyEvent);
171 }
172
173 bool UserInputBridge::handleAccessKeyEvent(const PlatformKeyboardEvent& keyEvent, InputSource)
174 {
175     return m_page.focusController().focusedOrMainFrame().eventHandler().handleAccessKey(keyEvent);
176 }
177
178 bool UserInputBridge::handleWheelEvent(const PlatformWheelEvent& wheelEvent, InputSource inputSource)
179 {
180 #if ENABLE(WEB_REPLAY)
181     EARLY_RETURN_IF_SHOULD_IGNORE_INPUT;
182
183     InputCursor& cursor = activeCursor();
184     if (cursor.isCapturing()) {
185         std::unique_ptr<PlatformWheelEvent> ownedEvent = std::make_unique<PlatformWheelEvent>(wheelEvent);
186         cursor.appendInput<HandleWheelEvent>(WTF::move(ownedEvent));
187     }
188     EventLoopInputExtent extent(cursor);
189 #else
190     UNUSED_PARAM(inputSource);
191 #endif
192
193     return m_page.mainFrame().eventHandler().handleWheelEvent(wheelEvent);
194 }
195
196 void UserInputBridge::focusSetActive(bool active, InputSource)
197 {
198     m_page.focusController().setActive(active);
199 }
200
201 void UserInputBridge::focusSetFocused(bool focused, InputSource)
202 {
203     m_page.focusController().setFocused(focused);
204 }
205
206 bool UserInputBridge::scrollRecursively(ScrollDirection direction, ScrollGranularity granularity, InputSource inputSource)
207 {
208 #if ENABLE(WEB_REPLAY)
209     EARLY_RETURN_IF_SHOULD_IGNORE_INPUT;
210
211     InputCursor& cursor = activeCursor();
212     if (cursor.isCapturing())
213         cursor.appendInput<ScrollPage>(direction, granularity);
214
215     EventLoopInputExtent extent(cursor);
216 #else
217     UNUSED_PARAM(inputSource);
218 #endif
219
220     return m_page.focusController().focusedOrMainFrame().eventHandler().scrollRecursively(direction, granularity, nullptr);
221 }
222
223 bool UserInputBridge::logicalScrollRecursively(ScrollLogicalDirection direction, ScrollGranularity granularity, InputSource inputSource)
224 {
225 #if ENABLE(WEB_REPLAY)
226     EARLY_RETURN_IF_SHOULD_IGNORE_INPUT;
227
228     InputCursor& cursor = activeCursor();
229     if (cursor.isCapturing())
230         cursor.appendInput<LogicalScrollPage>(direction, granularity);
231
232     EventLoopInputExtent extent(cursor);
233 #else
234     UNUSED_PARAM(inputSource);
235 #endif
236
237     return m_page.focusController().focusedOrMainFrame().eventHandler().logicalScrollRecursively(direction, granularity, nullptr);
238 }
239
240 void UserInputBridge::loadRequest(const FrameLoadRequest& request, InputSource)
241 {
242     m_page.mainFrame().loader().load(request);
243 }
244
245 void UserInputBridge::reloadFrame(Frame* frame, bool endToEndReload, InputSource)
246 {
247     frame->loader().reload(endToEndReload);
248 }
249
250 void UserInputBridge::stopLoadingFrame(Frame* frame, InputSource)
251 {
252     frame->loader().stopForUserCancel();
253 }
254
255 bool UserInputBridge::tryClosePage(InputSource)
256 {
257     return m_page.mainFrame().loader().shouldClose();
258 }
259
260 } // namespace WebCore