blob: 6024aa49bb30608773392b608e18a3013a00c183 [file] [log] [blame]
bburg@apple.com5adb8092016-02-12 19:28:29 +00001/*
2 * Copyright (C) 2016 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
bburg@apple.com2023c3a2016-03-29 20:19:45 +000026#pragma once
bburg@apple.com5adb8092016-02-12 19:28:29 +000027
28#include "APIObject.h"
bburg@apple.come17bd1d2016-03-25 18:18:50 +000029#include "AutomationBackendDispatchers.h"
timothy@apple.com63c22542016-03-28 15:56:53 +000030#include "Connection.h"
joepeck@webkit.orgee4a2dd2016-03-31 19:03:37 +000031#include "ShareableBitmap.h"
bburg@apple.com2023c3a2016-03-29 20:19:45 +000032#include "WebEvent.h"
bburg@apple.com5adb8092016-02-12 19:28:29 +000033#include <wtf/Forward.h>
34
35#if ENABLE(REMOTE_INSPECTOR)
36#include <JavaScriptCore/RemoteAutomationTarget.h>
37#endif
38
39namespace API {
40class AutomationSessionClient;
41}
42
bburg@apple.com0160e892016-02-23 07:18:41 +000043namespace Inspector {
44class BackendDispatcher;
45class FrontendRouter;
46}
47
timothy@apple.com9896ab42016-03-28 15:57:37 +000048namespace WebCore {
bburg@apple.com2023c3a2016-03-29 20:19:45 +000049class IntPoint;
bburg@apple.com5e4defb2016-04-06 03:23:18 +000050class IntRect;
51
52struct Cookie;
bburg@apple.com2023c3a2016-03-29 20:19:45 +000053}
54
55#if USE(APPKIT)
56OBJC_CLASS NSArray;
bburg@apple.com5870c052016-03-29 20:19:51 +000057OBJC_CLASS NSEvent;
bburg@apple.com2023c3a2016-03-29 20:19:45 +000058#endif
59
bburg@apple.com5adb8092016-02-12 19:28:29 +000060namespace WebKit {
61
62class WebAutomationSessionClient;
timothy@apple.com4643d7e2016-03-28 15:57:23 +000063class WebFrameProxy;
timothy@apple.com74f070b2016-03-06 00:40:09 +000064class WebPageProxy;
65class WebProcessPool;
bburg@apple.com5adb8092016-02-12 19:28:29 +000066
timothy@apple.com63c22542016-03-28 15:56:53 +000067class WebAutomationSession final : public API::ObjectImpl<API::Object::Type::AutomationSession>, public IPC::MessageReceiver
bburg@apple.com5adb8092016-02-12 19:28:29 +000068#if ENABLE(REMOTE_INSPECTOR)
69 , public Inspector::RemoteAutomationTarget
70#endif
bburg@apple.coma9cfd0d2016-02-23 17:40:20 +000071 , public Inspector::AutomationBackendDispatcherHandler
bburg@apple.com5adb8092016-02-12 19:28:29 +000072{
73public:
74 WebAutomationSession();
75 ~WebAutomationSession();
76
77 void setClient(std::unique_ptr<API::AutomationSessionClient>);
78
79 void setSessionIdentifier(const String& sessionIdentifier) { m_sessionIdentifier = sessionIdentifier; }
80 String sessionIdentifier() const { return m_sessionIdentifier; }
81
timothy@apple.com4643d7e2016-03-28 15:57:23 +000082 WebProcessPool* processPool() const { return m_processPool; }
83 void setProcessPool(WebProcessPool*);
timothy@apple.com74f070b2016-03-06 00:40:09 +000084
timothy@apple.com7454dec2016-03-31 20:52:44 +000085 void navigationOccurredForPage(const WebPageProxy&);
bburg@apple.com600b79a2016-05-11 20:57:08 +000086 void inspectorFrontendLoaded(const WebPageProxy&);
joepeck@webkit.org860a47f2016-07-23 06:15:22 +000087 void keyboardEventsFlushedForPage(const WebPageProxy&);
timothy@apple.com7454dec2016-03-31 20:52:44 +000088
bburg@apple.com5adb8092016-02-12 19:28:29 +000089#if ENABLE(REMOTE_INSPECTOR)
90 // Inspector::RemoteAutomationTarget API
darin@apple.com11ff47c2016-03-04 16:47:55 +000091 String name() const override { return m_sessionIdentifier; }
92 void dispatchMessageFromRemote(const String& message) override;
93 void connect(Inspector::FrontendChannel*, bool isAutomaticConnection = false) override;
94 void disconnect(Inspector::FrontendChannel*) override;
bburg@apple.com5adb8092016-02-12 19:28:29 +000095#endif
bburg@apple.comf4f185d2016-11-12 22:24:33 +000096 void terminate();
bburg@apple.com5adb8092016-02-12 19:28:29 +000097
bburg@apple.coma9cfd0d2016-02-23 17:40:20 +000098 // Inspector::AutomationBackendDispatcherHandler API
timothy@apple.com4ef6bcf2016-03-06 00:40:01 +000099 void getBrowsingContexts(Inspector::ErrorString&, RefPtr<Inspector::Protocol::Array<Inspector::Protocol::Automation::BrowsingContext>>&) override;
timothy@apple.com3d3df2f2016-03-08 17:55:01 +0000100 void getBrowsingContext(Inspector::ErrorString&, const String&, RefPtr<Inspector::Protocol::Automation::BrowsingContext>&) override;
timothy@apple.com4ef6bcf2016-03-06 00:40:01 +0000101 void createBrowsingContext(Inspector::ErrorString&, String*) override;
102 void closeBrowsingContext(Inspector::ErrorString&, const String&) override;
timothy@apple.com4643d7e2016-03-28 15:57:23 +0000103 void switchToBrowsingContext(Inspector::ErrorString&, const String& browsingContextHandle, const String* optionalFrameHandle) override;
bburg@apple.com4dd7f382016-03-28 22:34:44 +0000104 void resizeWindowOfBrowsingContext(Inspector::ErrorString&, const String& handle, const Inspector::InspectorObject& size) override;
105 void moveWindowOfBrowsingContext(Inspector::ErrorString&, const String& handle, const Inspector::InspectorObject& position) override;
timothy@apple.com7454dec2016-03-31 20:52:44 +0000106 void navigateBrowsingContext(Inspector::ErrorString&, const String& handle, const String& url, Ref<NavigateBrowsingContextCallback>&&) override;
107 void goBackInBrowsingContext(Inspector::ErrorString&, const String&, Ref<GoBackInBrowsingContextCallback>&&) override;
108 void goForwardInBrowsingContext(Inspector::ErrorString&, const String&, Ref<GoForwardInBrowsingContextCallback>&&) override;
109 void reloadBrowsingContext(Inspector::ErrorString&, const String&, Ref<ReloadBrowsingContextCallback>&&) override;
bburg@apple.come1ba2742016-06-22 21:41:01 +0000110 void inspectBrowsingContext(Inspector::ErrorString&, const String&, const bool* optionalEnableAutoCapturing, Ref<InspectBrowsingContextCallback>&&) override;
timothy@apple.come6d8b082016-03-31 20:52:38 +0000111 void evaluateJavaScriptFunction(Inspector::ErrorString&, const String& browsingContextHandle, const String* optionalFrameHandle, const String& function, const Inspector::InspectorArray& arguments, const bool* optionalExpectsImplicitCallbackArgument, const int* optionalCallbackTimeout, Ref<Inspector::AutomationBackendDispatcherHandler::EvaluateJavaScriptFunctionCallback>&&) override;
bburg@apple.com2023c3a2016-03-29 20:19:45 +0000112 void performMouseInteraction(Inspector::ErrorString&, const String& handle, const Inspector::InspectorObject& requestedPosition, const String& mouseButton, const String& mouseInteraction, const Inspector::InspectorArray& keyModifiers, RefPtr<Inspector::Protocol::Automation::Point>& updatedPosition) override;
joepeck@webkit.org860a47f2016-07-23 06:15:22 +0000113 void performKeyboardInteractions(Inspector::ErrorString&, const String& handle, const Inspector::InspectorArray& interactions, Ref<PerformKeyboardInteractionsCallback>&&) override;
joepeck@webkit.orgee4a2dd2016-03-31 19:03:37 +0000114 void takeScreenshot(Inspector::ErrorString&, const String& handle, Ref<Inspector::AutomationBackendDispatcherHandler::TakeScreenshotCallback>&&) override;
timothy@apple.com4643d7e2016-03-28 15:57:23 +0000115 void resolveChildFrameHandle(Inspector::ErrorString&, const String& browsingContextHandle, const String* optionalFrameHandle, const int* optionalOrdinal, const String* optionalName, const String* optionalNodeHandle, Ref<ResolveChildFrameHandleCallback>&&) override;
116 void resolveParentFrameHandle(Inspector::ErrorString&, const String& browsingContextHandle, const String& frameHandle, Ref<ResolveParentFrameHandleCallback>&&) override;
timothy@apple.com9896ab42016-03-28 15:57:37 +0000117 void computeElementLayout(Inspector::ErrorString&, const String& browsingContextHandle, const String& frameHandle, const String& nodeHandle, const bool* optionalScrollIntoViewIfNeeded, const bool* useViewportCoordinates, Ref<Inspector::AutomationBackendDispatcherHandler::ComputeElementLayoutCallback>&&) override;
timothy@apple.com0ba48cd2016-03-28 15:57:49 +0000118 void isShowingJavaScriptDialog(Inspector::ErrorString&, const String& browsingContextHandle, bool* result) override;
119 void dismissCurrentJavaScriptDialog(Inspector::ErrorString&, const String& browsingContextHandle) override;
120 void acceptCurrentJavaScriptDialog(Inspector::ErrorString&, const String& browsingContextHandle) override;
121 void messageOfCurrentJavaScriptDialog(Inspector::ErrorString&, const String& browsingContextHandle, String* text) override;
122 void setUserInputForCurrentJavaScriptPrompt(Inspector::ErrorString&, const String& browsingContextHandle, const String& text) override;
bburg@apple.com5e4defb2016-04-06 03:23:18 +0000123 void getAllCookies(Inspector::ErrorString&, const String& browsingContextHandle, Ref<GetAllCookiesCallback>&&) override;
124 void deleteSingleCookie(Inspector::ErrorString&, const String& browsingContextHandle, const String& cookieName, Ref<DeleteSingleCookieCallback>&&) override;
125 void addSingleCookie(Inspector::ErrorString&, const String& browsingContextHandle, const Inspector::InspectorObject& cookie, Ref<AddSingleCookieCallback>&&) override;
joepeck@webkit.orgb8344912016-07-23 06:15:19 +0000126 void deleteAllCookies(Inspector::ErrorString&, const String& browsingContextHandle) override;
bburg@apple.com5870c052016-03-29 20:19:51 +0000127#if USE(APPKIT)
128 bool wasEventSynthesizedForAutomation(NSEvent *);
bburg@apple.com679e6042016-04-30 03:21:14 +0000129 void markEventAsSynthesizedForAutomation(NSEvent *);
bburg@apple.com5870c052016-03-29 20:19:51 +0000130#endif
131
bburg@apple.com5adb8092016-02-12 19:28:29 +0000132private:
timothy@apple.com4643d7e2016-03-28 15:57:23 +0000133 WebPageProxy* webPageProxyForHandle(const String&);
134 String handleForWebPageProxy(const WebPageProxy&);
bburg@apple.com8a78ae12016-03-28 18:52:12 +0000135 RefPtr<Inspector::Protocol::Automation::BrowsingContext> buildBrowsingContextForPage(WebPageProxy&);
timothy@apple.com4643d7e2016-03-28 15:57:23 +0000136
utatane.tea@gmail.com43926962016-11-27 06:08:16 +0000137 std::optional<uint64_t> webFrameIDForHandle(const String&);
timothy@apple.com4643d7e2016-03-28 15:57:23 +0000138 String handleForWebFrameID(uint64_t frameID);
139 String handleForWebFrameProxy(const WebFrameProxy&);
timothy@apple.com74f070b2016-03-06 00:40:09 +0000140
timothy@apple.com63c22542016-03-28 15:56:53 +0000141 // Implemented in generated WebAutomationSessionMessageReceiver.cpp
andersca@apple.com5ecb5312016-08-20 00:11:17 +0000142 void didReceiveMessage(IPC::Connection&, IPC::Decoder&) override;
timothy@apple.com63c22542016-03-28 15:56:53 +0000143
144 // Called by WebAutomationSession messages
timothy@apple.com2619bbd2016-03-28 15:57:06 +0000145 void didEvaluateJavaScriptFunction(uint64_t callbackID, const String& result, const String& errorType);
timothy@apple.com4643d7e2016-03-28 15:57:23 +0000146 void didResolveChildFrame(uint64_t callbackID, uint64_t frameID, const String& errorType);
147 void didResolveParentFrame(uint64_t callbackID, uint64_t frameID, const String& errorType);
timothy@apple.com9896ab42016-03-28 15:57:37 +0000148 void didComputeElementLayout(uint64_t callbackID, WebCore::IntRect, const String& errorType);
joepeck@webkit.orgee4a2dd2016-03-31 19:03:37 +0000149 void didTakeScreenshot(uint64_t callbackID, const ShareableBitmap::Handle&, const String& errorType);
bburg@apple.com5e4defb2016-04-06 03:23:18 +0000150 void didGetCookiesForFrame(uint64_t callbackID, Vector<WebCore::Cookie>, const String& errorType);
151 void didDeleteCookie(uint64_t callbackID, const String& errorType);
timothy@apple.com63c22542016-03-28 15:56:53 +0000152
bburg@apple.com2023c3a2016-03-29 20:19:45 +0000153 // Platform-specific helper methods.
154 void platformSimulateMouseInteraction(WebPageProxy&, const WebCore::IntPoint& viewPosition, Inspector::Protocol::Automation::MouseInteraction, Inspector::Protocol::Automation::MouseButton, WebEvent::Modifiers);
joepeck@webkit.org8a791782016-03-31 02:03:57 +0000155 // Simulates a single virtual key being pressed, such as Control, F-keys, Numpad keys, etc. as allowed by the protocol.
156 void platformSimulateKeyStroke(WebPageProxy&, Inspector::Protocol::Automation::KeyboardInteractionType, Inspector::Protocol::Automation::VirtualKey);
157 // Simulates key presses to produce the codepoints in a string. One or more code points are delivered atomically at grapheme cluster boundaries.
158 void platformSimulateKeySequence(WebPageProxy&, const String&);
joepeck@webkit.orgee4a2dd2016-03-31 19:03:37 +0000159 // Get base64 encoded PNG data from a bitmap.
160 String platformGetBase64EncodedPNGData(const ShareableBitmap::Handle&);
bburg@apple.com2023c3a2016-03-29 20:19:45 +0000161
162#if USE(APPKIT)
163 void sendSynthesizedEventsToPage(WebPageProxy&, NSArray *eventsToSend);
164#endif
165
timothy@apple.com4643d7e2016-03-28 15:57:23 +0000166 WebProcessPool* m_processPool { nullptr };
167
bburg@apple.com5adb8092016-02-12 19:28:29 +0000168 std::unique_ptr<API::AutomationSessionClient> m_client;
169 String m_sessionIdentifier { ASCIILiteral("Untitled Session") };
bburg@apple.com0160e892016-02-23 07:18:41 +0000170 Ref<Inspector::FrontendRouter> m_frontendRouter;
171 Ref<Inspector::BackendDispatcher> m_backendDispatcher;
bburg@apple.coma9cfd0d2016-02-23 17:40:20 +0000172 Ref<Inspector::AutomationBackendDispatcher> m_domainDispatcher;
bburg@apple.com5adb8092016-02-12 19:28:29 +0000173
timothy@apple.com4643d7e2016-03-28 15:57:23 +0000174 HashMap<uint64_t, String> m_webPageHandleMap;
175 HashMap<String, uint64_t> m_handleWebPageMap;
timothy@apple.com74f070b2016-03-06 00:40:09 +0000176 String m_activeBrowsingContextHandle;
177
timothy@apple.com4643d7e2016-03-28 15:57:23 +0000178 HashMap<uint64_t, String> m_webFrameHandleMap;
179 HashMap<String, uint64_t> m_handleWebFrameMap;
180
timothy@apple.com7454dec2016-03-31 20:52:44 +0000181 HashMap<uint64_t, RefPtr<Inspector::BackendDispatcher::CallbackBase>> m_pendingNavigationInBrowsingContextCallbacksPerPage;
bburg@apple.com600b79a2016-05-11 20:57:08 +0000182 HashMap<uint64_t, RefPtr<Inspector::BackendDispatcher::CallbackBase>> m_pendingInspectorCallbacksPerPage;
joepeck@webkit.org860a47f2016-07-23 06:15:22 +0000183 HashMap<uint64_t, RefPtr<Inspector::BackendDispatcher::CallbackBase>> m_pendingKeyboardEventsFlushedCallbacksPerPage;
bburg@apple.com600b79a2016-05-11 20:57:08 +0000184
timothy@apple.com2619bbd2016-03-28 15:57:06 +0000185 uint64_t m_nextEvaluateJavaScriptCallbackID { 1 };
186 HashMap<uint64_t, RefPtr<Inspector::AutomationBackendDispatcherHandler::EvaluateJavaScriptFunctionCallback>> m_evaluateJavaScriptFunctionCallbacks;
187
timothy@apple.com4643d7e2016-03-28 15:57:23 +0000188 uint64_t m_nextResolveFrameCallbackID { 1 };
189 HashMap<uint64_t, RefPtr<Inspector::AutomationBackendDispatcherHandler::ResolveChildFrameHandleCallback>> m_resolveChildFrameHandleCallbacks;
190
191 uint64_t m_nextResolveParentFrameCallbackID { 1 };
192 HashMap<uint64_t, RefPtr<Inspector::AutomationBackendDispatcherHandler::ResolveParentFrameHandleCallback>> m_resolveParentFrameHandleCallbacks;
193
timothy@apple.com9896ab42016-03-28 15:57:37 +0000194 uint64_t m_nextComputeElementLayoutCallbackID { 1 };
195 HashMap<uint64_t, RefPtr<Inspector::AutomationBackendDispatcherHandler::ComputeElementLayoutCallback>> m_computeElementLayoutCallbacks;
196
joepeck@webkit.orgee4a2dd2016-03-31 19:03:37 +0000197 uint64_t m_nextScreenshotCallbackID { 1 };
198 HashMap<uint64_t, RefPtr<Inspector::AutomationBackendDispatcherHandler::TakeScreenshotCallback>> m_screenshotCallbacks;
199
bburg@apple.com5e4defb2016-04-06 03:23:18 +0000200 uint64_t m_nextGetCookiesCallbackID { 1 };
201 HashMap<uint64_t, RefPtr<Inspector::AutomationBackendDispatcherHandler::GetAllCookiesCallback>> m_getCookieCallbacks;
202
203 uint64_t m_nextDeleteCookieCallbackID { 1 };
204 HashMap<uint64_t, RefPtr<Inspector::AutomationBackendDispatcherHandler::DeleteSingleCookieCallback>> m_deleteCookieCallbacks;
205
bburg@apple.com5adb8092016-02-12 19:28:29 +0000206#if ENABLE(REMOTE_INSPECTOR)
207 Inspector::FrontendChannel* m_remoteChannel { nullptr };
208#endif
209};
210
211} // namespace WebKit