Sfoglia il codice sorgente

Browser+WebContent+WebDriver: Move Execute Async Script to WebContent

With this, WebDriverEndpoints is unused and removed :^)
Timothy Flynn 2 anni fa
parent
commit
31469ee45a

+ 0 - 4
Userland/Applications/Browser/BrowserWindow.cpp

@@ -607,10 +607,6 @@ void BrowserWindow::create_new_tab(URL url, bool activate)
         return active_tab().view().take_screenshot();
     };
 
-    new_tab.webdriver_endpoints().on_execute_script = [this](String const& body, Vector<String> const& json_arguments, Optional<u64> const& timeout, bool async) {
-        return active_tab().view().webdriver_execute_script(body, json_arguments, timeout, async);
-    };
-
     new_tab.load(url);
 
     dbgln_if(SPAM_DEBUG, "Added new tab {:p}, loading {}", &new_tab, url);

+ 0 - 4
Userland/Applications/Browser/Tab.h

@@ -8,7 +8,6 @@
 #pragma once
 
 #include "History.h"
-#include "WebDriverEndpoints.h"
 #include <AK/Optional.h>
 #include <AK/URL.h>
 #include <LibGUI/ActionGroup.h>
@@ -74,7 +73,6 @@ public:
     Function<OrderedHashMap<String, String>()> on_get_session_storage_entries;
     Function<Gfx::ShareableBitmap()> on_take_screenshot;
 
-    WebDriverEndpoints& webdriver_endpoints() { return m_webdriver_endpoints; }
     void enable_webdriver_mode();
 
     enum class InspectorTarget {
@@ -143,8 +141,6 @@ private:
 
     Optional<URL> m_navigating_url;
 
-    WebDriverEndpoints m_webdriver_endpoints {};
-
     bool m_loaded { false };
     bool m_is_history_navigation { false };
 };

+ 0 - 15
Userland/Applications/Browser/WebDriverConnection.cpp

@@ -58,21 +58,6 @@ void WebDriverConnection::forward()
         browser_window->active_tab().go_forward();
 }
 
-Messages::WebDriverSessionClient::ExecuteScriptResponse WebDriverConnection::execute_script(String const& body, Vector<String> const& json_arguments, Optional<u64> const& timeout, bool async)
-{
-    dbgln("WebDriverConnection: execute_script");
-    if (auto browser_window = m_browser_window.strong_ref()) {
-        auto& tab = browser_window->active_tab();
-        if (tab.webdriver_endpoints().on_execute_script) {
-            auto response = tab.webdriver_endpoints().on_execute_script(body, json_arguments, timeout, async);
-            // WebContentServer's and WebDriverSessionClient's ExecuteScriptResponse have an identical
-            // structure but are distinct types, so we have to convert here.
-            return { response.result_type(), response.json_result() };
-        }
-    }
-    return { {} };
-}
-
 Messages::WebDriverSessionClient::GetAllCookiesResponse WebDriverConnection::get_all_cookies()
 {
     dbgln_if(WEBDRIVER_DEBUG, "WebDriverConnection: get_cookies");

+ 0 - 1
Userland/Applications/Browser/WebDriverConnection.h

@@ -42,7 +42,6 @@ public:
     virtual void refresh() override;
     virtual void back() override;
     virtual void forward() override;
-    virtual Messages::WebDriverSessionClient::ExecuteScriptResponse execute_script(String const& body, Vector<String> const& json_arguments, Optional<u64> const& timeout, bool async) override;
     virtual Messages::WebDriverSessionClient::GetAllCookiesResponse get_all_cookies() override;
     virtual Messages::WebDriverSessionClient::GetNamedCookieResponse get_named_cookie(String const& name) override;
     virtual void add_cookie(Web::Cookie::ParsedCookie const&) override;

+ 0 - 29
Userland/Applications/Browser/WebDriverEndpoints.h

@@ -1,29 +0,0 @@
-/*
- * Copyright (c) 2022, Tobias Christiansen <tobyase@serenityos.org>
- * Copyright (c) 2022, Tim Flynn <trflynn89@serenityos.org>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#pragma once
-
-#include <AK/Forward.h>
-#include <AK/Function.h>
-#include <LibGfx/Rect.h>
-#include <LibWeb/Forward.h>
-
-namespace Messages::WebContentServer {
-class WebdriverExecuteScriptResponse;
-}
-
-namespace Browser {
-
-class WebDriverEndpoints {
-public:
-    WebDriverEndpoints() = default;
-    ~WebDriverEndpoints() = default;
-
-    Function<Messages::WebContentServer::WebdriverExecuteScriptResponse(String const& body, Vector<String> const& json_arguments, Optional<u64> const& timeout, bool async)> on_execute_script;
-};
-
-}

+ 0 - 1
Userland/Applications/Browser/WebDriverSessionClient.ipc

@@ -18,7 +18,6 @@ endpoint WebDriverSessionClient {
     refresh() =|
     back() =|
     forward() =|
-    execute_script(String body, Vector<String> json_arguments, Optional<u64> timeout, bool async) => (Web::WebDriver::ExecuteScriptResultType result_type, String json_result)
     get_all_cookies() => (Vector<Web::Cookie::Cookie> cookies)
     get_named_cookie(String name) => (Optional<Web::Cookie::Cookie> cookie)
     add_cookie(Web::Cookie::ParsedCookie cookie) =|

+ 0 - 5
Userland/Libraries/LibWebView/OutOfProcessWebView.cpp

@@ -591,11 +591,6 @@ Gfx::ShareableBitmap OutOfProcessWebView::take_document_screenshot()
     return client().take_document_screenshot();
 }
 
-Messages::WebContentServer::WebdriverExecuteScriptResponse OutOfProcessWebView::webdriver_execute_script(String const& body, Vector<String> const& json_arguments, Optional<u64> const& timeout, bool async)
-{
-    return client().webdriver_execute_script(body, json_arguments, timeout, async);
-}
-
 void OutOfProcessWebView::focusin_event(GUI::FocusEvent&)
 {
     client().async_set_has_focus(true);

+ 0 - 2
Userland/Libraries/LibWebView/OutOfProcessWebView.h

@@ -75,8 +75,6 @@ public:
     Gfx::ShareableBitmap take_screenshot() const;
     Gfx::ShareableBitmap take_document_screenshot();
 
-    Messages::WebContentServer::WebdriverExecuteScriptResponse webdriver_execute_script(String const& body, Vector<String> const& json_arguments, Optional<u64> const& timeout, bool async);
-
     Function<void(Gfx::IntPoint const& screen_position)> on_context_menu_request;
     Function<void(const AK::URL&, String const& target, unsigned modifiers)> on_link_click;
     Function<void(const AK::URL&, Gfx::IntPoint const& screen_position)> on_link_context_menu_request;

+ 0 - 21
Userland/Services/WebContent/ConnectionFromClient.cpp

@@ -18,7 +18,6 @@
 #include <LibJS/Heap/Heap.h>
 #include <LibJS/Parser.h>
 #include <LibJS/Runtime/ConsoleObject.h>
-#include <LibJS/Runtime/JSONObject.h>
 #include <LibWeb/Bindings/MainThreadVM.h>
 #include <LibWeb/Cookie/ParsedCookie.h>
 #include <LibWeb/DOM/Document.h>
@@ -574,24 +573,4 @@ void ConnectionFromClient::set_system_visibility_state(bool visible)
             : Web::HTML::VisibilityState::Hidden);
 }
 
-Messages::WebContentServer::WebdriverExecuteScriptResponse ConnectionFromClient::webdriver_execute_script(String const& body, Vector<String> const& json_arguments, Optional<u64> const& timeout, bool async)
-{
-    auto& page = m_page_host->page();
-
-    auto* window = page.top_level_browsing_context().active_window();
-    auto& vm = window->vm();
-
-    auto arguments = JS::MarkedVector<JS::Value> { vm.heap() };
-    for (auto const& argument_string : json_arguments) {
-        // NOTE: These are assumed to be valid JSON values.
-        auto json_value = MUST(JsonValue::from_string(argument_string));
-        arguments.append(JS::JSONObject::parse_json_value(vm, json_value));
-    }
-
-    auto result = async
-        ? Web::WebDriver::execute_async_script(page, body, move(arguments), timeout)
-        : Web::WebDriver::execute_script(page, body, move(arguments), timeout);
-    return { result.type, result.value.serialized<StringBuilder>() };
-}
-
 }

+ 0 - 2
Userland/Services/WebContent/ConnectionFromClient.h

@@ -90,8 +90,6 @@ private:
     virtual Messages::WebContentServer::GetSelectedTextResponse get_selected_text() override;
     virtual void select_all() override;
 
-    virtual Messages::WebContentServer::WebdriverExecuteScriptResponse webdriver_execute_script(String const& body, Vector<String> const& json_arguments, Optional<u64> const& timeout, bool async) override;
-
     void flush_pending_paint_requests();
 
     NonnullOwnPtr<PageHost> m_page_host;

+ 0 - 2
Userland/Services/WebContent/WebContentServer.ipc

@@ -43,8 +43,6 @@ endpoint WebContentServer
 
     take_document_screenshot() => (Gfx::ShareableBitmap data)
 
-    webdriver_execute_script(String body, Vector<String> json_arguments, Optional<u64> timeout, bool async) => (Web::WebDriver::ExecuteScriptResultType result_type, String json_result)
-
     run_javascript(String js_source) =|
 
     dump_layout_tree() => (String dump)

+ 1 - 0
Userland/Services/WebContent/WebDriverClient.ipc

@@ -23,6 +23,7 @@ endpoint WebDriverClient {
     is_element_enabled(String element_id) => (Web::WebDriver::Response response)
     get_source() => (Web::WebDriver::Response response)
     execute_script(JsonValue payload) => (Web::WebDriver::Response response)
+    execute_async_script(JsonValue payload) => (Web::WebDriver::Response response)
     take_screenshot() => (Web::WebDriver::Response response)
     take_element_screenshot(String element_id) => (Web::WebDriver::Response response)
 }

+ 32 - 0
Userland/Services/WebContent/WebDriverConnection.cpp

@@ -797,6 +797,38 @@ Messages::WebDriverClient::ExecuteScriptResponse WebDriverConnection::execute_sc
     VERIFY_NOT_REACHED();
 }
 
+// 13.2.2 Execute Async Script, https://w3c.github.io/webdriver/#dfn-execute-async-script
+Messages::WebDriverClient::ExecuteAsyncScriptResponse WebDriverConnection::execute_async_script(JsonValue const& payload)
+{
+    // 1. Let body and arguments by the result of trying to extract the script arguments from a request with argument parameters.
+    auto const& [body, arguments] = TRY(extract_the_script_arguments_from_a_request(payload));
+
+    // 2. If the current browsing context is no longer open, return error with error code no such window.
+    TRY(ensure_open_top_level_browsing_context());
+
+    // FIXME: 3. Handle any user prompts, and return its value if it is an error.
+
+    // 4., 5.1-5.11.
+    // FIXME: Move timeouts from WebDriver to WebContent and pass the script timeout through here.
+    auto result = Web::WebDriver::execute_async_script(m_page_host.page(), body, move(arguments), {});
+    dbgln_if(WEBDRIVER_DEBUG, "Executing async script returned: {}", result.value);
+
+    switch (result.type) {
+    // 6. If promise is still pending and the session script timeout is reached, return error with error code script timeout.
+    case Web::WebDriver::ExecuteScriptResultType::Timeout:
+        return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::ScriptTimeoutError, "Script timed out");
+    // 7. Upon fulfillment of promise with value v, let result be a JSON clone of v, and return success with data result.
+    case Web::WebDriver::ExecuteScriptResultType::PromiseResolved:
+        return make_success_response(move(result.value));
+    // 8. Upon rejection of promise with reason r, let result be a JSON clone of r, and return error with error code javascript error and data result.
+    case Web::WebDriver::ExecuteScriptResultType::PromiseRejected:
+    case Web::WebDriver::ExecuteScriptResultType::JavaScriptError:
+        return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::JavascriptError, "Script returned an error", move(result.value));
+    }
+
+    VERIFY_NOT_REACHED();
+}
+
 // 17.1 Take Screenshot, https://w3c.github.io/webdriver/#take-screenshot
 Messages::WebDriverClient::TakeScreenshotResponse WebDriverConnection::take_screenshot()
 {

+ 1 - 0
Userland/Services/WebContent/WebDriverConnection.h

@@ -55,6 +55,7 @@ private:
     virtual Messages::WebDriverClient::IsElementEnabledResponse is_element_enabled(String const& element_id) override;
     virtual Messages::WebDriverClient::GetSourceResponse get_source() override;
     virtual Messages::WebDriverClient::ExecuteScriptResponse execute_script(JsonValue const& payload) override;
+    virtual Messages::WebDriverClient::ExecuteAsyncScriptResponse execute_async_script(JsonValue const& payload) override;
     virtual Messages::WebDriverClient::TakeScreenshotResponse take_screenshot() override;
     virtual Messages::WebDriverClient::TakeElementScreenshotResponse take_element_screenshot(String const& element_id) override;
 

+ 1 - 2
Userland/Services/WebDriver/Client.cpp

@@ -744,8 +744,7 @@ Web::WebDriver::Response Client::handle_execute_async_script(Vector<StringView>
 {
     dbgln_if(WEBDRIVER_DEBUG, "Handling POST /session/<session_id>/execute/async");
     auto* session = TRY(find_session_with_id(parameters[0]));
-    auto result = TRY(session->execute_async_script(payload));
-    return make_json_value(result);
+    return session->web_content_connection().execute_async_script(payload);
 }
 
 // 14.1 Get All Cookies, https://w3c.github.io/webdriver/#dfn-get-all-cookies

+ 0 - 72
Userland/Services/WebDriver/Session.cpp

@@ -297,78 +297,6 @@ Web::WebDriver::Response Session::get_window_handles() const
     return JsonValue { handles };
 }
 
-struct ScriptArguments {
-    String script;
-    JsonArray const& arguments;
-};
-
-// https://w3c.github.io/webdriver/#dfn-extract-the-script-arguments-from-a-request
-static ErrorOr<ScriptArguments, Web::WebDriver::Error> extract_the_script_arguments_from_a_request(JsonValue const& payload)
-{
-    if (!payload.is_object())
-        return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, "Payload is not a JSON object");
-
-    auto const& properties = payload.as_object();
-
-    // 1. Let script be the result of getting a property named script from the parameters.
-    // 2. If script is not a String, return error with error code invalid argument.
-    if (!properties.has_string("script"sv))
-        return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, "Payload doesn't have a 'script' string property");
-    auto script = properties.get("script"sv).as_string();
-
-    // 3. Let args be the result of getting a property named args from the parameters.
-    // 4. If args is not an Array return error with error code invalid argument.
-    if (!properties.has_array("args"sv))
-        return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, "Payload doesn't have an 'args' string property");
-    auto const& args = properties.get("args"sv).as_array();
-
-    // 5. Let arguments be the result of calling the JSON deserialize algorithm with arguments args.
-    // NOTE: We forward the JSON array to the Browser and then WebContent process over IPC, so this is not necessary.
-
-    // 6. Return success with data script and arguments.
-    return ScriptArguments { script, args };
-}
-
-// 13.2.2 Execute Async Script, https://w3c.github.io/webdriver/#dfn-execute-async-script
-Web::WebDriver::Response Session::execute_async_script(JsonValue const& parameters)
-{
-    // 1. Let body and arguments by the result of trying to extract the script arguments from a request with argument parameters.
-    auto [body, arguments] = TRY(extract_the_script_arguments_from_a_request(parameters));
-
-    // 2. If the current browsing context is no longer open, return error with error code no such window.
-    TRY(check_for_open_top_level_browsing_context_or_return_error());
-
-    // FIXME: 3. Handle any user prompts, and return its value if it is an error.
-
-    // 4., 5.1-5.11.
-    Vector<String> json_arguments;
-    arguments.for_each([&](JsonValue const& json_value) {
-        // NOTE: serialized() instead of to_string() ensures proper quoting.
-        json_arguments.append(json_value.serialized<StringBuilder>());
-    });
-
-    dbgln("Executing async script with 'args': [{}] / 'body':\n{}", String::join(", "sv, json_arguments), body);
-    auto execute_script_response = m_browser_connection->execute_script(body, json_arguments, m_timeouts_configuration.script_timeout, true);
-    dbgln("Executing async script returned: {}", execute_script_response.json_result());
-
-    // NOTE: This is assumed to be a valid JSON value.
-    auto result = MUST(JsonValue::from_string(execute_script_response.json_result()));
-
-    switch (execute_script_response.result_type()) {
-    // 6. If promise is still pending and the session script timeout is reached, return error with error code script timeout.
-    case Web::WebDriver::ExecuteScriptResultType::Timeout:
-        return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::ScriptTimeoutError, "Script timed out");
-    // 7. Upon fulfillment of promise with value v, let result be a JSON clone of v, and return success with data result.
-    case Web::WebDriver::ExecuteScriptResultType::PromiseResolved:
-        return result;
-    // 8. Upon rejection of promise with reason r, let result be a JSON clone of r, and return error with error code javascript error and data result.
-    case Web::WebDriver::ExecuteScriptResultType::PromiseRejected:
-        return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::JavascriptError, "Script returned an error", move(result));
-    default:
-        VERIFY_NOT_REACHED();
-    }
-}
-
 // https://w3c.github.io/webdriver/#dfn-serialized-cookie
 static JsonObject serialize_cookie(Web::Cookie::Cookie const& cookie)
 {

+ 0 - 1
Userland/Services/WebDriver/Session.h

@@ -58,7 +58,6 @@ public:
     Web::WebDriver::Response get_window_handle();
     ErrorOr<void, Variant<Web::WebDriver::Error, Error>> close_window();
     Web::WebDriver::Response get_window_handles() const;
-    Web::WebDriver::Response execute_async_script(JsonValue const& payload);
     Web::WebDriver::Response get_all_cookies();
     Web::WebDriver::Response get_named_cookie(String const& name);
     Web::WebDriver::Response add_cookie(JsonValue const& payload);