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