1/*
2 * Copyright (C) 2016-2018 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. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include "APIObject.h"
29#include "AutomationBackendDispatchers.h"
30#include "AutomationFrontendDispatchers.h"
31#include "Connection.h"
32#include "MessageReceiver.h"
33#include "MessageSender.h"
34#include "ShareableBitmap.h"
35#include "SimulatedInputDispatcher.h"
36#include "WebEvent.h"
37#include <wtf/CompletionHandler.h>
38#include <wtf/Forward.h>
39#include <wtf/RunLoop.h>
40
41#if ENABLE(REMOTE_INSPECTOR)
42#include <JavaScriptCore/RemoteAutomationTarget.h>
43#endif
44
45namespace API {
46class AutomationSessionClient;
47}
48
49namespace Inspector {
50class BackendDispatcher;
51class FrontendRouter;
52}
53
54namespace WebCore {
55class IntPoint;
56class IntRect;
57
58struct Cookie;
59}
60
61#if PLATFORM(COCOA)
62OBJC_CLASS NSArray;
63typedef unsigned short unichar;
64#endif
65
66#if USE(APPKIT)
67OBJC_CLASS NSEvent;
68#endif
69
70namespace API {
71class OpenPanelParameters;
72}
73
74namespace WebKit {
75
76class WebAutomationSessionClient;
77class WebFrameProxy;
78class WebOpenPanelResultListenerProxy;
79class WebPageProxy;
80class WebProcessPool;
81
82class AutomationCommandError {
83public:
84 Inspector::Protocol::Automation::ErrorMessage type;
85 Optional<String> message { WTF::nullopt };
86
87 AutomationCommandError(Inspector::Protocol::Automation::ErrorMessage type)
88 : type(type) { }
89
90 AutomationCommandError(Inspector::Protocol::Automation::ErrorMessage type, const String& message)
91 : type(type)
92 , message(message) { }
93
94 String toProtocolString();
95};
96
97using AutomationCompletionHandler = WTF::CompletionHandler<void(Optional<AutomationCommandError>)>;
98
99class WebAutomationSession final : public API::ObjectImpl<API::Object::Type::AutomationSession>, public IPC::MessageReceiver
100#if ENABLE(REMOTE_INSPECTOR)
101 , public Inspector::RemoteAutomationTarget
102#endif
103 , public Inspector::AutomationBackendDispatcherHandler
104#if ENABLE(WEBDRIVER_ACTIONS_API)
105 , public SimulatedInputDispatcher::Client
106#endif
107{
108public:
109 WebAutomationSession();
110 ~WebAutomationSession();
111
112 void setClient(std::unique_ptr<API::AutomationSessionClient>&&);
113
114 void setSessionIdentifier(const String& sessionIdentifier) { m_sessionIdentifier = sessionIdentifier; }
115 String sessionIdentifier() const { return m_sessionIdentifier; }
116
117 WebProcessPool* processPool() const { return m_processPool; }
118 void setProcessPool(WebProcessPool*);
119
120 void navigationOccurredForFrame(const WebFrameProxy&);
121 void documentLoadedForFrame(const WebFrameProxy&);
122 void inspectorFrontendLoaded(const WebPageProxy&);
123 void keyboardEventsFlushedForPage(const WebPageProxy&);
124 void mouseEventsFlushedForPage(const WebPageProxy&);
125 void willClosePage(const WebPageProxy&);
126 void handleRunOpenPanel(const WebPageProxy&, const WebFrameProxy&, const API::OpenPanelParameters&, WebOpenPanelResultListenerProxy&);
127 void willShowJavaScriptDialog(WebPageProxy&);
128 void didEnterFullScreenForPage(const WebPageProxy&);
129 void didExitFullScreenForPage(const WebPageProxy&);
130
131 bool shouldAllowGetUserMediaForPage(const WebPageProxy&) const;
132
133#if ENABLE(REMOTE_INSPECTOR)
134 // Inspector::RemoteAutomationTarget API
135 String name() const override { return m_sessionIdentifier; }
136 void dispatchMessageFromRemote(const String& message) override;
137 void connect(Inspector::FrontendChannel&, bool isAutomaticConnection = false, bool immediatelyPause = false) override;
138 void disconnect(Inspector::FrontendChannel&) override;
139#endif
140 void terminate();
141
142#if ENABLE(WEBDRIVER_ACTIONS_API)
143
144 // SimulatedInputDispatcher::Client API
145#if ENABLE(WEBDRIVER_MOUSE_INTERACTIONS)
146 void simulateMouseInteraction(WebPageProxy&, MouseInteraction, WebMouseEvent::Button, const WebCore::IntPoint& locationInView, AutomationCompletionHandler&&) final;
147#endif
148#if ENABLE(WEBDRIVER_TOUCH_INTERACTIONS)
149 void simulateTouchInteraction(WebPageProxy&, TouchInteraction, const WebCore::IntPoint& locationInView, Optional<Seconds> duration, AutomationCompletionHandler&&) final;
150#endif
151#if ENABLE(WEBDRIVER_KEYBOARD_INTERACTIONS)
152 void simulateKeyboardInteraction(WebPageProxy&, KeyboardInteraction, WTF::Variant<VirtualKey, CharKey>&&, AutomationCompletionHandler&&) final;
153#endif
154 void viewportInViewCenterPointOfElement(WebPageProxy&, uint64_t frameID, const String& nodeHandle, Function<void (Optional<WebCore::IntPoint>, Optional<AutomationCommandError>)>&&) final;
155
156#endif // ENABLE(WEBDRIVER_ACTIONS_API)
157
158 // Inspector::AutomationBackendDispatcherHandler API
159 // NOTE: the set of declarations included in this interface depend on the "platform" property in Automation.json
160 // and the --platform argument passed to the protocol bindings generator.
161
162 // Platform: Generic
163 void getBrowsingContexts(Ref<GetBrowsingContextsCallback>&&) final;
164 void getBrowsingContext(const String&, Ref<GetBrowsingContextCallback>&&) final;
165 void createBrowsingContext(const String* optionalPresentationHint, Ref<CreateBrowsingContextCallback>&&) final;
166 void closeBrowsingContext(Inspector::ErrorString&, const String&) final;
167 void switchToBrowsingContext(const String& browsingContextHandle, const String* optionalFrameHandle, Ref<SwitchToBrowsingContextCallback>&&) final;
168 void setWindowFrameOfBrowsingContext(const String& handle, const JSON::Object* origin, const JSON::Object* size, Ref<SetWindowFrameOfBrowsingContextCallback>&&) final;
169 void maximizeWindowOfBrowsingContext(const String& handle, Ref<MaximizeWindowOfBrowsingContextCallback>&&) final;
170 void hideWindowOfBrowsingContext(const String& handle, Ref<HideWindowOfBrowsingContextCallback>&&) final;
171 void navigateBrowsingContext(const String& handle, const String& url, const String* optionalPageLoadStrategyString, const int* optionalPageLoadTimeout, Ref<NavigateBrowsingContextCallback>&&) override;
172 void goBackInBrowsingContext(const String&, const String* optionalPageLoadStrategyString, const int* optionalPageLoadTimeout, Ref<GoBackInBrowsingContextCallback>&&) override;
173 void goForwardInBrowsingContext(const String&, const String* optionalPageLoadStrategyString, const int* optionalPageLoadTimeout, Ref<GoForwardInBrowsingContextCallback>&&) override;
174 void reloadBrowsingContext(const String&, const String* optionalPageLoadStrategyString, const int* optionalPageLoadTimeout, Ref<ReloadBrowsingContextCallback>&&) override;
175 void waitForNavigationToComplete(const String& browsingContextHandle, const String* optionalFrameHandle, const String* optionalPageLoadStrategyString, const int* optionalPageLoadTimeout, Ref<WaitForNavigationToCompleteCallback>&&) override;
176 void evaluateJavaScriptFunction(const String& browsingContextHandle, const String* optionalFrameHandle, const String& function, const JSON::Array& arguments, const bool* optionalExpectsImplicitCallbackArgument, const int* optionalCallbackTimeout, Ref<Inspector::AutomationBackendDispatcherHandler::EvaluateJavaScriptFunctionCallback>&&) override;
177 void performMouseInteraction(const String& handle, const JSON::Object& requestedPosition, const String& mouseButton, const String& mouseInteraction, const JSON::Array& keyModifiers, Ref<PerformMouseInteractionCallback>&&) final;
178 void performKeyboardInteractions(const String& handle, const JSON::Array& interactions, Ref<PerformKeyboardInteractionsCallback>&&) override;
179 void performInteractionSequence(const String& handle, const String* optionalFrameHandle, const JSON::Array& sources, const JSON::Array& steps, Ref<PerformInteractionSequenceCallback>&&) override;
180 void cancelInteractionSequence(const String& handle, const String* optionalFrameHandle, Ref<CancelInteractionSequenceCallback>&&) override;
181 void takeScreenshot(const String& handle, const String* optionalFrameHandle, const String* optionalNodeHandle, const bool* optionalScrollIntoViewIfNeeded, const bool* optionalClipToViewport, Ref<TakeScreenshotCallback>&&) override;
182 void resolveChildFrameHandle(const String& browsingContextHandle, const String* optionalFrameHandle, const int* optionalOrdinal, const String* optionalName, const String* optionalNodeHandle, Ref<ResolveChildFrameHandleCallback>&&) override;
183 void resolveParentFrameHandle(const String& browsingContextHandle, const String& frameHandle, Ref<ResolveParentFrameHandleCallback>&&) override;
184 void computeElementLayout(const String& browsingContextHandle, const String& frameHandle, const String& nodeHandle, const bool* optionalScrollIntoViewIfNeeded, const String& coordinateSystem, Ref<Inspector::AutomationBackendDispatcherHandler::ComputeElementLayoutCallback>&&) override;
185 void selectOptionElement(const String& browsingContextHandle, const String& frameHandle, const String& nodeHandle, Ref<Inspector::AutomationBackendDispatcherHandler::SelectOptionElementCallback>&&) override;
186 void isShowingJavaScriptDialog(Inspector::ErrorString&, const String& browsingContextHandle, bool* result) override;
187 void dismissCurrentJavaScriptDialog(Inspector::ErrorString&, const String& browsingContextHandle) override;
188 void acceptCurrentJavaScriptDialog(Inspector::ErrorString&, const String& browsingContextHandle) override;
189 void messageOfCurrentJavaScriptDialog(Inspector::ErrorString&, const String& browsingContextHandle, String* text) override;
190 void setUserInputForCurrentJavaScriptPrompt(Inspector::ErrorString&, const String& browsingContextHandle, const String& text) override;
191 void setFilesToSelectForFileUpload(Inspector::ErrorString&, const String& browsingContextHandle, const JSON::Array& filenames, const JSON::Array* optionalFileContents) override;
192 void getAllCookies(const String& browsingContextHandle, Ref<GetAllCookiesCallback>&&) override;
193 void deleteSingleCookie(const String& browsingContextHandle, const String& cookieName, Ref<DeleteSingleCookieCallback>&&) override;
194 void addSingleCookie(const String& browsingContextHandle, const JSON::Object& cookie, Ref<AddSingleCookieCallback>&&) override;
195 void deleteAllCookies(Inspector::ErrorString&, const String& browsingContextHandle) override;
196 void getSessionPermissions(Inspector::ErrorString&, RefPtr<JSON::ArrayOf<Inspector::Protocol::Automation::SessionPermissionData>>& out_permissions) override;
197 void setSessionPermissions(Inspector::ErrorString&, const JSON::Array& in_permissions) override;
198
199 // Platform: macOS
200#if PLATFORM(MAC)
201 void inspectBrowsingContext(const String&, const bool* optionalEnableAutoCapturing, Ref<InspectBrowsingContextCallback>&&) override;
202#endif
203
204 // Event Simulation Support.
205 bool isSimulatingUserInteraction() const;
206#if ENABLE(WEBDRIVER_ACTIONS_API)
207 SimulatedInputDispatcher& inputDispatcherForPage(WebPageProxy&);
208 SimulatedInputSource* inputSourceForType(SimulatedInputSourceType) const;
209#endif
210
211#if PLATFORM(MAC)
212 bool wasEventSynthesizedForAutomation(NSEvent *);
213 void markEventAsSynthesizedForAutomation(NSEvent *);
214#endif
215
216private:
217 WebPageProxy* webPageProxyForHandle(const String&);
218 String handleForWebPageProxy(const WebPageProxy&);
219 Ref<Inspector::Protocol::Automation::BrowsingContext> buildBrowsingContextForPage(WebPageProxy&, WebCore::FloatRect windowFrame);
220 void getNextContext(Ref<WebAutomationSession>&&, Vector<Ref<WebPageProxy>>&&, Ref<JSON::ArrayOf<Inspector::Protocol::Automation::BrowsingContext>>, Ref<WebAutomationSession::GetBrowsingContextsCallback>&&);
221
222 Optional<uint64_t> webFrameIDForHandle(const String&);
223 String handleForWebFrameID(uint64_t frameID);
224 String handleForWebFrameProxy(const WebFrameProxy&);
225
226 void waitForNavigationToCompleteOnPage(WebPageProxy&, Inspector::Protocol::Automation::PageLoadStrategy, Seconds, Ref<Inspector::BackendDispatcher::CallbackBase>&&);
227 void waitForNavigationToCompleteOnFrame(WebFrameProxy&, Inspector::Protocol::Automation::PageLoadStrategy, Seconds, Ref<Inspector::BackendDispatcher::CallbackBase>&&);
228 void respondToPendingPageNavigationCallbacksWithTimeout(HashMap<uint64_t, RefPtr<Inspector::BackendDispatcher::CallbackBase>>&);
229 void respondToPendingFrameNavigationCallbacksWithTimeout(HashMap<uint64_t, RefPtr<Inspector::BackendDispatcher::CallbackBase>>&);
230 void loadTimerFired();
231
232 void exitFullscreenWindowForPage(WebPageProxy&, WTF::CompletionHandler<void()>&&);
233 void restoreWindowForPage(WebPageProxy&, WTF::CompletionHandler<void()>&&);
234 void maximizeWindowForPage(WebPageProxy&, WTF::CompletionHandler<void()>&&);
235 void hideWindowForPage(WebPageProxy&, WTF::CompletionHandler<void()>&&);
236
237 // IPC::MessageReceiver (Implemented by generated code in WebAutomationSessionMessageReceiver.cpp).
238 void didReceiveMessage(IPC::Connection&, IPC::Decoder&) override;
239
240 // Called by WebAutomationSession messages.
241 void didEvaluateJavaScriptFunction(uint64_t callbackID, const String& result, const String& errorType);
242 void didTakeScreenshot(uint64_t callbackID, const ShareableBitmap::Handle&, const String& errorType);
243
244 // Platform-dependent implementations.
245#if ENABLE(WEBDRIVER_MOUSE_INTERACTIONS)
246 void platformSimulateMouseInteraction(WebPageProxy&, MouseInteraction, WebMouseEvent::Button, const WebCore::IntPoint& locationInView, OptionSet<WebEvent::Modifier>);
247#endif
248#if ENABLE(WEBDRIVER_TOUCH_INTERACTIONS)
249 // Simulates a single touch point being pressed, moved, and released.
250 void platformSimulateTouchInteraction(WebPageProxy&, TouchInteraction, const WebCore::IntPoint& locationInViewport, Optional<Seconds> duration, AutomationCompletionHandler&&);
251#endif
252#if ENABLE(WEBDRIVER_KEYBOARD_INTERACTIONS)
253 // Simulates a single virtual or char key being pressed/released, such as 'a', Control, F-keys, Numpad keys, etc. as allowed by the protocol.
254 void platformSimulateKeyboardInteraction(WebPageProxy&, KeyboardInteraction, WTF::Variant<VirtualKey, CharKey>&&);
255 // Simulates key presses to produce the codepoints in a string. One or more code points are delivered atomically at grapheme cluster boundaries.
256 void platformSimulateKeySequence(WebPageProxy&, const String&);
257#endif // ENABLE(WEBDRIVER_KEYBOARD_INTERACTIONS)
258
259 // Get base64-encoded PNG data from a bitmap.
260 Optional<String> platformGetBase64EncodedPNGData(const ShareableBitmap::Handle&);
261
262 // Save base64-encoded file contents to a local file path and return the path.
263 // This reuses the basename of the remote file path so that the filename exposed to DOM API remains the same.
264 Optional<String> platformGenerateLocalFilePathForRemoteFile(const String& remoteFilePath, const String& base64EncodedFileContents);
265
266#if PLATFORM(COCOA)
267 // The type parameter of the NSArray argument is platform-dependent.
268 void sendSynthesizedEventsToPage(WebPageProxy&, NSArray *eventsToSend);
269
270 Optional<unichar> charCodeForVirtualKey(Inspector::Protocol::Automation::VirtualKey) const;
271 Optional<unichar> charCodeIgnoringModifiersForVirtualKey(Inspector::Protocol::Automation::VirtualKey) const;
272#endif
273
274 WebProcessPool* m_processPool { nullptr };
275
276 std::unique_ptr<API::AutomationSessionClient> m_client;
277 String m_sessionIdentifier { "Untitled Session"_s };
278 Ref<Inspector::FrontendRouter> m_frontendRouter;
279 Ref<Inspector::BackendDispatcher> m_backendDispatcher;
280 Ref<Inspector::AutomationBackendDispatcher> m_domainDispatcher;
281 std::unique_ptr<Inspector::AutomationFrontendDispatcher> m_domainNotifier;
282
283 HashMap<uint64_t, String> m_webPageHandleMap;
284 HashMap<String, uint64_t> m_handleWebPageMap;
285
286 HashMap<uint64_t, String> m_webFrameHandleMap;
287 HashMap<String, uint64_t> m_handleWebFrameMap;
288
289 HashMap<uint64_t, RefPtr<Inspector::BackendDispatcher::CallbackBase>> m_pendingNormalNavigationInBrowsingContextCallbacksPerPage;
290 HashMap<uint64_t, RefPtr<Inspector::BackendDispatcher::CallbackBase>> m_pendingEagerNavigationInBrowsingContextCallbacksPerPage;
291 HashMap<uint64_t, RefPtr<Inspector::BackendDispatcher::CallbackBase>> m_pendingNormalNavigationInBrowsingContextCallbacksPerFrame;
292 HashMap<uint64_t, RefPtr<Inspector::BackendDispatcher::CallbackBase>> m_pendingEagerNavigationInBrowsingContextCallbacksPerFrame;
293 HashMap<uint64_t, RefPtr<Inspector::BackendDispatcher::CallbackBase>> m_pendingInspectorCallbacksPerPage;
294#if ENABLE(WEBDRIVER_KEYBOARD_INTERACTIONS)
295 HashMap<uint64_t, Function<void(Optional<AutomationCommandError>)>> m_pendingKeyboardEventsFlushedCallbacksPerPage;
296#endif
297#if ENABLE(WEBDRIVER_MOUSE_INTERACTIONS)
298 HashMap<uint64_t, Function<void(Optional<AutomationCommandError>)>> m_pendingMouseEventsFlushedCallbacksPerPage;
299#endif
300
301 uint64_t m_nextEvaluateJavaScriptCallbackID { 1 };
302 HashMap<uint64_t, RefPtr<Inspector::AutomationBackendDispatcherHandler::EvaluateJavaScriptFunctionCallback>> m_evaluateJavaScriptFunctionCallbacks;
303
304 uint64_t m_nextScreenshotCallbackID { 1 };
305 HashMap<uint64_t, RefPtr<Inspector::AutomationBackendDispatcherHandler::TakeScreenshotCallback>> m_screenshotCallbacks;
306
307 enum class WindowTransitionedToState {
308 Fullscreen,
309 Unfullscreen,
310 };
311 Function<void(WindowTransitionedToState)> m_windowStateTransitionCallback { };
312
313 RunLoop::Timer<WebAutomationSession> m_loadTimer;
314 Vector<String> m_filesToSelectForFileUpload;
315
316 bool m_permissionForGetUserMedia { true };
317
318#if ENABLE(WEBDRIVER_ACTIONS_API)
319 // SimulatedInputDispatcher APIs take a set of input sources. We also intern these
320 // so that previous input source state is used as initial state for later commands.
321 HashSet<Ref<SimulatedInputSource>> m_inputSources;
322 HashMap<uint64_t, Ref<SimulatedInputDispatcher>> m_inputDispatchersByPage;
323#endif
324#if ENABLE(WEBDRIVER_KEYBOARD_INTERACTIONS)
325 // Keep track of currently active modifiers across multiple keystrokes.
326 // Most platforms do not track current modifiers from synthesized events.
327 unsigned m_currentModifiers { 0 };
328#endif
329#if ENABLE(WEBDRIVER_TOUCH_INTERACTIONS)
330 bool m_simulatingTouchInteraction { false };
331#endif
332
333#if ENABLE(REMOTE_INSPECTOR)
334 Inspector::FrontendChannel* m_remoteChannel { nullptr };
335#endif
336
337};
338
339} // namespace WebKit
340