Web Replay: capture and replay mouse events
[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 {
59 }
60
61 #if ENABLE(WEB_REPLAY)
62 InputCursor& UserInputBridge::activeCursor() const
63 {
64     return m_page.replayController().activeInputCursor();
65 }
66 #endif
67
68 #if ENABLE(CONTEXT_MENUS)
69 bool UserInputBridge::handleContextMenuEvent(const PlatformMouseEvent& mouseEvent, const Frame* frame, InputSource)
70 {
71     return frame->eventHandler().sendContextMenuEvent(mouseEvent);
72 }
73 #endif
74
75 bool UserInputBridge::handleMousePressEvent(const PlatformMouseEvent& mouseEvent, InputSource inputSource)
76 {
77 #if ENABLE(WEB_REPLAY)
78     EARLY_RETURN_IF_SHOULD_IGNORE_INPUT;
79
80     if (activeCursor().isCapturing()) {
81         std::unique_ptr<PlatformMouseEvent> ownedEvent = std::make_unique<PlatformMouseEvent>(mouseEvent);
82         activeCursor().appendInput<HandleMousePress>(std::move(ownedEvent));
83     }
84 #else
85     UNUSED_PARAM(inputSource);
86 #endif
87
88     return m_page.mainFrame().eventHandler().handleMousePressEvent(mouseEvent);
89 }
90
91 bool UserInputBridge::handleMouseReleaseEvent(const PlatformMouseEvent& mouseEvent, InputSource inputSource)
92 {
93 #if ENABLE(WEB_REPLAY)
94     EARLY_RETURN_IF_SHOULD_IGNORE_INPUT;
95
96     if (activeCursor().isCapturing()) {
97         std::unique_ptr<PlatformMouseEvent> ownedEvent = std::make_unique<PlatformMouseEvent>(mouseEvent);
98         activeCursor().appendInput<HandleMouseRelease>(std::move(ownedEvent));
99     }
100 #else
101     UNUSED_PARAM(inputSource);
102 #endif
103
104     return m_page.mainFrame().eventHandler().handleMouseReleaseEvent(mouseEvent);
105 }
106
107 bool UserInputBridge::handleMouseMoveEvent(const PlatformMouseEvent& mouseEvent, InputSource inputSource)
108 {
109 #if ENABLE(WEB_REPLAY)
110     EARLY_RETURN_IF_SHOULD_IGNORE_INPUT;
111
112     if (activeCursor().isCapturing()) {
113         std::unique_ptr<PlatformMouseEvent> ownedEvent = std::make_unique<PlatformMouseEvent>(mouseEvent);
114         activeCursor().appendInput<HandleMouseMove>(std::move(ownedEvent), false);
115     }
116 #else
117     UNUSED_PARAM(inputSource);
118 #endif
119
120     return m_page.mainFrame().eventHandler().mouseMoved(mouseEvent);
121 }
122
123 bool UserInputBridge::handleMouseMoveOnScrollbarEvent(const PlatformMouseEvent& mouseEvent, InputSource inputSource)
124 {
125 #if ENABLE(WEB_REPLAY)
126     EARLY_RETURN_IF_SHOULD_IGNORE_INPUT;
127
128     if (activeCursor().isCapturing()) {
129         std::unique_ptr<PlatformMouseEvent> ownedEvent = std::make_unique<PlatformMouseEvent>(mouseEvent);
130         activeCursor().appendInput<HandleMouseMove>(std::move(ownedEvent), true);
131     }
132 #else
133     UNUSED_PARAM(inputSource);
134 #endif
135
136     return m_page.mainFrame().eventHandler().passMouseMovedEventToScrollbars(mouseEvent);
137 }
138
139 bool UserInputBridge::handleKeyEvent(const PlatformKeyboardEvent& keyEvent, InputSource)
140 {
141     return m_page.focusController().focusedOrMainFrame().eventHandler().keyEvent(keyEvent);
142 }
143
144 bool UserInputBridge::handleAccessKeyEvent(const PlatformKeyboardEvent& keyEvent, InputSource)
145 {
146     return m_page.focusController().focusedOrMainFrame().eventHandler().handleAccessKey(keyEvent);
147 }
148
149 bool UserInputBridge::handleWheelEvent(const PlatformWheelEvent& wheelEvent, InputSource)
150 {
151     return m_page.mainFrame().eventHandler().handleWheelEvent(wheelEvent);
152 }
153
154 void UserInputBridge::focusSetActive(bool active, InputSource)
155 {
156     m_page.focusController().setActive(active);
157 }
158
159 void UserInputBridge::focusSetFocused(bool focused, InputSource)
160 {
161     m_page.focusController().setFocused(focused);
162 }
163
164 bool UserInputBridge::scrollRecursively(ScrollDirection direction, ScrollGranularity granularity, InputSource)
165 {
166     return m_page.focusController().focusedOrMainFrame().eventHandler().scrollRecursively(direction, granularity, nullptr);
167 }
168
169 bool UserInputBridge::logicalScrollRecursively(ScrollLogicalDirection direction, ScrollGranularity granularity, InputSource)
170 {
171     return m_page.focusController().focusedOrMainFrame().eventHandler().logicalScrollRecursively(direction, granularity, nullptr);
172 }
173
174 void UserInputBridge::loadRequest(const FrameLoadRequest& request, InputSource)
175 {
176     m_page.mainFrame().loader().load(request);
177 }
178
179 void UserInputBridge::reloadFrame(Frame* frame, bool endToEndReload, InputSource)
180 {
181     frame->loader().reload(endToEndReload);
182 }
183
184 void UserInputBridge::stopLoadingFrame(Frame* frame, InputSource)
185 {
186     frame->loader().stopForUserCancel();
187 }
188
189 bool UserInputBridge::tryClosePage(InputSource)
190 {
191     return m_page.mainFrame().loader().shouldClose();
192 }
193
194 } // namespace WebCore