Web Inspector: move Mac-specific automation commands to a separate implementation...
[WebKit-https.git] / Source / WebKit2 / UIProcess / Automation / WebAutomationSession.h
1 /*
2  * Copyright (C) 2016, 2017 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 "Connection.h"
31 #include "ShareableBitmap.h"
32 #include "WebEvent.h"
33 #include <wtf/Forward.h>
34
35 #if ENABLE(REMOTE_INSPECTOR)
36 #include <JavaScriptCore/RemoteAutomationTarget.h>
37 #endif
38
39 namespace API {
40 class AutomationSessionClient;
41 }
42
43 namespace Inspector {
44 class BackendDispatcher;
45 class FrontendRouter;
46 }
47
48 namespace WebCore {
49 class IntPoint;
50 class IntRect;
51
52 struct Cookie;
53 }
54
55 #if USE(APPKIT)
56 OBJC_CLASS NSArray;
57 OBJC_CLASS NSEvent;
58 #endif
59
60 namespace WebKit {
61
62 class WebAutomationSessionClient;
63 class WebFrameProxy;
64 class WebPageProxy;
65 class WebProcessPool;
66
67 class WebAutomationSession final : public API::ObjectImpl<API::Object::Type::AutomationSession>, public IPC::MessageReceiver
68 #if ENABLE(REMOTE_INSPECTOR)
69     , public Inspector::RemoteAutomationTarget
70 #endif
71     , public Inspector::AutomationBackendDispatcherHandler
72 {
73 public:
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
82     WebProcessPool* processPool() const { return m_processPool; }
83     void setProcessPool(WebProcessPool*);
84
85     void navigationOccurredForPage(const WebPageProxy&);
86     void inspectorFrontendLoaded(const WebPageProxy&);
87     void keyboardEventsFlushedForPage(const WebPageProxy&);
88
89 #if ENABLE(REMOTE_INSPECTOR)
90     // Inspector::RemoteAutomationTarget API
91     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;
95 #endif
96     void terminate();
97
98     // Inspector::AutomationBackendDispatcherHandler API
99     // NOTE: the set of declarations included in this interface depend on the "platform" property in Automation.json
100     // and the --platform argument passed to the protocol bindings generator.
101
102     // Platform: Generic
103     void getBrowsingContexts(Inspector::ErrorString&, RefPtr<Inspector::Protocol::Array<Inspector::Protocol::Automation::BrowsingContext>>&) override;
104     void getBrowsingContext(Inspector::ErrorString&, const String&, RefPtr<Inspector::Protocol::Automation::BrowsingContext>&) override;
105     void createBrowsingContext(Inspector::ErrorString&, String*) override;
106     void closeBrowsingContext(Inspector::ErrorString&, const String&) override;
107     void switchToBrowsingContext(Inspector::ErrorString&, const String& browsingContextHandle, const String* optionalFrameHandle) override;
108     void navigateBrowsingContext(Inspector::ErrorString&, const String& handle, const String& url, Ref<NavigateBrowsingContextCallback>&&) override;
109     void goBackInBrowsingContext(Inspector::ErrorString&, const String&, Ref<GoBackInBrowsingContextCallback>&&) override;
110     void goForwardInBrowsingContext(Inspector::ErrorString&, const String&, Ref<GoForwardInBrowsingContextCallback>&&) override;
111     void reloadBrowsingContext(Inspector::ErrorString&, const String&, Ref<ReloadBrowsingContextCallback>&&) override;
112     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;
113     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;
114     void performKeyboardInteractions(Inspector::ErrorString&, const String& handle, const Inspector::InspectorArray& interactions, Ref<PerformKeyboardInteractionsCallback>&&) override;
115     void takeScreenshot(Inspector::ErrorString&, const String& handle, Ref<Inspector::AutomationBackendDispatcherHandler::TakeScreenshotCallback>&&) override;
116     void resolveChildFrameHandle(Inspector::ErrorString&, const String& browsingContextHandle, const String* optionalFrameHandle, const int* optionalOrdinal, const String* optionalName, const String* optionalNodeHandle, Ref<ResolveChildFrameHandleCallback>&&) override;
117     void resolveParentFrameHandle(Inspector::ErrorString&, const String& browsingContextHandle, const String& frameHandle, Ref<ResolveParentFrameHandleCallback>&&) override;
118     void computeElementLayout(Inspector::ErrorString&, const String& browsingContextHandle, const String& frameHandle, const String& nodeHandle, const bool* optionalScrollIntoViewIfNeeded, const bool* useViewportCoordinates, Ref<Inspector::AutomationBackendDispatcherHandler::ComputeElementLayoutCallback>&&) override;
119     void isShowingJavaScriptDialog(Inspector::ErrorString&, const String& browsingContextHandle, bool* result) override;
120     void dismissCurrentJavaScriptDialog(Inspector::ErrorString&, const String& browsingContextHandle) override;
121     void acceptCurrentJavaScriptDialog(Inspector::ErrorString&, const String& browsingContextHandle) override;
122     void messageOfCurrentJavaScriptDialog(Inspector::ErrorString&, const String& browsingContextHandle, String* text) override;
123     void setUserInputForCurrentJavaScriptPrompt(Inspector::ErrorString&, const String& browsingContextHandle, const String& text) override;
124     void getAllCookies(Inspector::ErrorString&, const String& browsingContextHandle, Ref<GetAllCookiesCallback>&&) override;
125     void deleteSingleCookie(Inspector::ErrorString&, const String& browsingContextHandle, const String& cookieName, Ref<DeleteSingleCookieCallback>&&) override;
126     void addSingleCookie(Inspector::ErrorString&, const String& browsingContextHandle, const Inspector::InspectorObject& cookie, Ref<AddSingleCookieCallback>&&) override;
127     void deleteAllCookies(Inspector::ErrorString&, const String& browsingContextHandle) override;
128
129     // Platform: macOS
130 #if PLATFORM(MAC)
131     void resizeWindowOfBrowsingContext(Inspector::ErrorString&, const String& handle, const Inspector::InspectorObject& size) override;
132     void moveWindowOfBrowsingContext(Inspector::ErrorString&, const String& handle, const Inspector::InspectorObject& position) override;
133     void inspectBrowsingContext(Inspector::ErrorString&, const String&, const bool* optionalEnableAutoCapturing, Ref<InspectBrowsingContextCallback>&&) override;
134 #endif
135
136     // Event Simulation Support.
137 #if PLATFORM(MAC)
138     bool wasEventSynthesizedForAutomation(NSEvent *);
139     void markEventAsSynthesizedForAutomation(NSEvent *);
140 #endif
141
142 private:
143     WebPageProxy* webPageProxyForHandle(const String&);
144     String handleForWebPageProxy(const WebPageProxy&);
145     RefPtr<Inspector::Protocol::Automation::BrowsingContext> buildBrowsingContextForPage(WebPageProxy&);
146
147     std::optional<uint64_t> webFrameIDForHandle(const String&);
148     String handleForWebFrameID(uint64_t frameID);
149     String handleForWebFrameProxy(const WebFrameProxy&);
150
151     // Implemented in generated WebAutomationSessionMessageReceiver.cpp.
152     void didReceiveMessage(IPC::Connection&, IPC::Decoder&) override;
153
154     // Called by WebAutomationSession messages.
155     void didEvaluateJavaScriptFunction(uint64_t callbackID, const String& result, const String& errorType);
156     void didResolveChildFrame(uint64_t callbackID, uint64_t frameID, const String& errorType);
157     void didResolveParentFrame(uint64_t callbackID, uint64_t frameID, const String& errorType);
158     void didComputeElementLayout(uint64_t callbackID, WebCore::IntRect, const String& errorType);
159     void didTakeScreenshot(uint64_t callbackID, const ShareableBitmap::Handle&, const String& errorType);
160     void didGetCookiesForFrame(uint64_t callbackID, Vector<WebCore::Cookie>, const String& errorType);
161     void didDeleteCookie(uint64_t callbackID, const String& errorType);
162
163     // Platform-dependent implementations.
164     void platformSimulateMouseInteraction(WebPageProxy&, const WebCore::IntPoint& viewPosition, Inspector::Protocol::Automation::MouseInteraction, Inspector::Protocol::Automation::MouseButton, WebEvent::Modifiers);
165     // Simulates a single virtual key being pressed, such as Control, F-keys, Numpad keys, etc. as allowed by the protocol.
166     void platformSimulateKeyStroke(WebPageProxy&, Inspector::Protocol::Automation::KeyboardInteractionType, Inspector::Protocol::Automation::VirtualKey);
167     // Simulates key presses to produce the codepoints in a string. One or more code points are delivered atomically at grapheme cluster boundaries.
168     void platformSimulateKeySequence(WebPageProxy&, const String&);
169     // Get base64 encoded PNG data from a bitmap.
170     String platformGetBase64EncodedPNGData(const ShareableBitmap::Handle&);
171
172 #if PLATFORM(MAC)
173     void sendSynthesizedEventsToPage(WebPageProxy&, NSArray *eventsToSend);
174 #endif
175
176     WebProcessPool* m_processPool { nullptr };
177
178     std::unique_ptr<API::AutomationSessionClient> m_client;
179     String m_sessionIdentifier { ASCIILiteral("Untitled Session") };
180     Ref<Inspector::FrontendRouter> m_frontendRouter;
181     Ref<Inspector::BackendDispatcher> m_backendDispatcher;
182     Ref<Inspector::AutomationBackendDispatcher> m_domainDispatcher;
183
184     HashMap<uint64_t, String> m_webPageHandleMap;
185     HashMap<String, uint64_t> m_handleWebPageMap;
186     String m_activeBrowsingContextHandle;
187
188     HashMap<uint64_t, String> m_webFrameHandleMap;
189     HashMap<String, uint64_t> m_handleWebFrameMap;
190
191     HashMap<uint64_t, RefPtr<Inspector::BackendDispatcher::CallbackBase>> m_pendingNavigationInBrowsingContextCallbacksPerPage;
192     HashMap<uint64_t, RefPtr<Inspector::BackendDispatcher::CallbackBase>> m_pendingInspectorCallbacksPerPage;
193     HashMap<uint64_t, RefPtr<Inspector::BackendDispatcher::CallbackBase>> m_pendingKeyboardEventsFlushedCallbacksPerPage;
194
195     uint64_t m_nextEvaluateJavaScriptCallbackID { 1 };
196     HashMap<uint64_t, RefPtr<Inspector::AutomationBackendDispatcherHandler::EvaluateJavaScriptFunctionCallback>> m_evaluateJavaScriptFunctionCallbacks;
197
198     uint64_t m_nextResolveFrameCallbackID { 1 };
199     HashMap<uint64_t, RefPtr<Inspector::AutomationBackendDispatcherHandler::ResolveChildFrameHandleCallback>> m_resolveChildFrameHandleCallbacks;
200
201     uint64_t m_nextResolveParentFrameCallbackID { 1 };
202     HashMap<uint64_t, RefPtr<Inspector::AutomationBackendDispatcherHandler::ResolveParentFrameHandleCallback>> m_resolveParentFrameHandleCallbacks;
203
204     uint64_t m_nextComputeElementLayoutCallbackID { 1 };
205     HashMap<uint64_t, RefPtr<Inspector::AutomationBackendDispatcherHandler::ComputeElementLayoutCallback>> m_computeElementLayoutCallbacks;
206
207     uint64_t m_nextScreenshotCallbackID { 1 };
208     HashMap<uint64_t, RefPtr<Inspector::AutomationBackendDispatcherHandler::TakeScreenshotCallback>> m_screenshotCallbacks;
209
210     uint64_t m_nextGetCookiesCallbackID { 1 };
211     HashMap<uint64_t, RefPtr<Inspector::AutomationBackendDispatcherHandler::GetAllCookiesCallback>> m_getCookieCallbacks;
212
213     uint64_t m_nextDeleteCookieCallbackID { 1 };
214     HashMap<uint64_t, RefPtr<Inspector::AutomationBackendDispatcherHandler::DeleteSingleCookieCallback>> m_deleteCookieCallbacks;
215
216 #if ENABLE(REMOTE_INSPECTOR)
217     Inspector::FrontendChannel* m_remoteChannel { nullptr };
218 #endif
219 };
220
221 } // namespace WebKit