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
54namespace WebCore {
55
56UserInputBridge::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)
65InputCursor& UserInputBridge::activeCursor() const
66{
67    return m_page.replayController().activeInputCursor();
68}
69#endif
70
71#if ENABLE(CONTEXT_MENUS)
72bool UserInputBridge::handleContextMenuEvent(const PlatformMouseEvent& mouseEvent, const Frame* frame, InputSource)
73{
74    return frame->eventHandler().sendContextMenuEvent(mouseEvent);
75}
76#endif
77
78bool 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
96bool 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
114bool 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
132bool 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
150bool UserInputBridge::handleKeyEvent(const PlatformKeyboardEvent& keyEvent, InputSource inputSource)
151{
152#if ENABLE(WEB_REPLAY)
153    EARLY_RETURN_IF_SHOULD_IGNORE_INPUT;
154
155    InputCursor& cursor = activeCursor();
156    if (cursor.isCapturing()) {
157        std::unique_ptr<PlatformKeyboardEvent> ownedEvent = std::make_unique<PlatformKeyboardEvent>(keyEvent);
158        cursor.appendInput<HandleKeyPress>(WTF::move(ownedEvent));
159    }
160    EventLoopInputExtent extent(cursor);
161#else
162    UNUSED_PARAM(inputSource);
163#endif
164
165    return m_page.focusController().focusedOrMainFrame().eventHandler().keyEvent(keyEvent);
166}
167
168bool UserInputBridge::handleAccessKeyEvent(const PlatformKeyboardEvent& keyEvent, InputSource)
169{
170    return m_page.focusController().focusedOrMainFrame().eventHandler().handleAccessKey(keyEvent);
171}
172
173bool UserInputBridge::handleWheelEvent(const PlatformWheelEvent& wheelEvent, InputSource inputSource)
174{
175#if ENABLE(WEB_REPLAY)
176    EARLY_RETURN_IF_SHOULD_IGNORE_INPUT;
177
178    InputCursor& cursor = activeCursor();
179    if (cursor.isCapturing()) {
180        std::unique_ptr<PlatformWheelEvent> ownedEvent = std::make_unique<PlatformWheelEvent>(wheelEvent);
181        cursor.appendInput<HandleWheelEvent>(WTF::move(ownedEvent));
182    }
183    EventLoopInputExtent extent(cursor);
184#else
185    UNUSED_PARAM(inputSource);
186#endif
187
188    return m_page.mainFrame().eventHandler().handleWheelEvent(wheelEvent);
189}
190
191void UserInputBridge::focusSetActive(bool active, InputSource)
192{
193    m_page.focusController().setActive(active);
194}
195
196void UserInputBridge::focusSetFocused(bool focused, InputSource)
197{
198    m_page.focusController().setFocused(focused);
199}
200
201bool UserInputBridge::scrollRecursively(ScrollDirection direction, ScrollGranularity granularity, InputSource inputSource)
202{
203#if ENABLE(WEB_REPLAY)
204    EARLY_RETURN_IF_SHOULD_IGNORE_INPUT;
205
206    InputCursor& cursor = activeCursor();
207    if (cursor.isCapturing())
208        cursor.appendInput<ScrollPage>(direction, granularity);
209
210    EventLoopInputExtent extent(cursor);
211#else
212    UNUSED_PARAM(inputSource);
213#endif
214
215    return m_page.focusController().focusedOrMainFrame().eventHandler().scrollRecursively(direction, granularity, nullptr);
216}
217
218bool UserInputBridge::logicalScrollRecursively(ScrollLogicalDirection direction, ScrollGranularity granularity, InputSource inputSource)
219{
220#if ENABLE(WEB_REPLAY)
221    EARLY_RETURN_IF_SHOULD_IGNORE_INPUT;
222
223    InputCursor& cursor = activeCursor();
224    if (cursor.isCapturing())
225        cursor.appendInput<LogicalScrollPage>(direction, granularity);
226
227    EventLoopInputExtent extent(cursor);
228#else
229    UNUSED_PARAM(inputSource);
230#endif
231
232    return m_page.focusController().focusedOrMainFrame().eventHandler().logicalScrollRecursively(direction, granularity, nullptr);
233}
234
235void UserInputBridge::loadRequest(const FrameLoadRequest& request, InputSource)
236{
237    m_page.mainFrame().loader().load(request);
238}
239
240void UserInputBridge::reloadFrame(Frame* frame, bool endToEndReload, InputSource)
241{
242    frame->loader().reload(endToEndReload);
243}
244
245void UserInputBridge::stopLoadingFrame(Frame* frame, InputSource)
246{
247    frame->loader().stopForUserCancel();
248}
249
250bool UserInputBridge::tryClosePage(InputSource)
251{
252    return m_page.mainFrame().loader().shouldClose();
253}
254
255} // namespace WebCore
256