From 1179d951f6ea518b88da129f5ff269a9139ec9c8 Mon Sep 17 00:00:00 2001 From: Tobias Christiansen Date: Fri, 14 Oct 2022 18:09:33 +0200 Subject: [PATCH] WebDriver: Implement `POST /session/{id}/refresh` endpoint --- .../Browser/WebDriverConnection.cpp | 7 ++++++ .../Browser/WebDriverConnection.h | 1 + .../Browser/WebDriverSessionClient.ipc | 1 + Userland/Services/WebDriver/Client.cpp | 12 +++++++++ Userland/Services/WebDriver/Client.h | 1 + Userland/Services/WebDriver/Session.cpp | 25 +++++++++++++++++++ Userland/Services/WebDriver/Session.h | 1 + 7 files changed, 48 insertions(+) diff --git a/Userland/Applications/Browser/WebDriverConnection.cpp b/Userland/Applications/Browser/WebDriverConnection.cpp index 9d4960fd80..37854ff0db 100644 --- a/Userland/Applications/Browser/WebDriverConnection.cpp +++ b/Userland/Applications/Browser/WebDriverConnection.cpp @@ -46,4 +46,11 @@ Messages::WebDriverSessionClient::GetTitleResponse WebDriverConnection::get_titl return { "" }; } +void WebDriverConnection::refresh() +{ + dbgln("WebDriverConnection: refresh"); + if (auto browser_window = m_browser_window.strong_ref()) + browser_window->active_tab().reload(); +} + } diff --git a/Userland/Applications/Browser/WebDriverConnection.h b/Userland/Applications/Browser/WebDriverConnection.h index c887ce30c4..0ba9bb7c79 100644 --- a/Userland/Applications/Browser/WebDriverConnection.h +++ b/Userland/Applications/Browser/WebDriverConnection.h @@ -39,6 +39,7 @@ public: virtual Messages::WebDriverSessionClient::GetUrlResponse get_url() override; virtual void set_url(AK::URL const& url) override; virtual Messages::WebDriverSessionClient::GetTitleResponse get_title() override; + virtual void refresh() override; private: WebDriverConnection(NonnullOwnPtr socket, NonnullRefPtr browser_window); diff --git a/Userland/Applications/Browser/WebDriverSessionClient.ipc b/Userland/Applications/Browser/WebDriverSessionClient.ipc index 361a634add..e8644394a1 100644 --- a/Userland/Applications/Browser/WebDriverSessionClient.ipc +++ b/Userland/Applications/Browser/WebDriverSessionClient.ipc @@ -6,4 +6,5 @@ endpoint WebDriverSessionClient { get_url() => (URL url) set_url(URL url) =| get_title() => (String title) + refresh() =| } diff --git a/Userland/Services/WebDriver/Client.cpp b/Userland/Services/WebDriver/Client.cpp index 8daaa507c4..25ea87bba4 100644 --- a/Userland/Services/WebDriver/Client.cpp +++ b/Userland/Services/WebDriver/Client.cpp @@ -26,6 +26,7 @@ Vector Client::s_routes = { { HTTP::HttpRequest::Method::GET, { "session", ":session_id", "url" }, &Client::handle_get_url }, { HTTP::HttpRequest::Method::GET, { "session", ":session_id", "title" }, &Client::handle_get_title }, { HTTP::HttpRequest::Method::DELETE, { "session", ":session_id", "window" }, &Client::handle_delete_window }, + { HTTP::HttpRequest::Method::POST, { "session", ":session_id", "refresh" }, &Client::handle_refresh }, }; Client::Client(NonnullOwnPtr socket, Core::Object* parent) @@ -451,4 +452,15 @@ ErrorOr Client::handle_delete_window(Vector pa return make_json_value(JsonValue()); } +// POST /session/{session id}/refresh https://w3c.github.io/webdriver/#dfn-refresh +ErrorOr Client::handle_refresh(Vector parameters, JsonValue const&) +{ + dbgln_if(WEBDRIVER_DEBUG, "Handling POST /session//refresh"); + Session* session = TRY(find_session_with_id(parameters[0])); + + // NOTE: Spec steps handled in Session::refresh(). + auto result = TRY(session->refresh()); + return make_json_value(result); +} + } diff --git a/Userland/Services/WebDriver/Client.h b/Userland/Services/WebDriver/Client.h index 5360fdd5e8..a5b5d8bcde 100644 --- a/Userland/Services/WebDriver/Client.h +++ b/Userland/Services/WebDriver/Client.h @@ -53,6 +53,7 @@ private: ErrorOr handle_get_url(Vector, JsonValue const& payload); ErrorOr handle_get_title(Vector, JsonValue const& payload); ErrorOr handle_delete_window(Vector, JsonValue const& payload); + ErrorOr handle_refresh(Vector, JsonValue const& payload); ErrorOr find_session_with_id(StringView session_id); JsonValue make_json_value(JsonValue const&); diff --git a/Userland/Services/WebDriver/Session.cpp b/Userland/Services/WebDriver/Session.cpp index f12716e220..36fd0110a1 100644 --- a/Userland/Services/WebDriver/Session.cpp +++ b/Userland/Services/WebDriver/Session.cpp @@ -158,4 +158,29 @@ ErrorOr Session::get_title() return JsonValue(m_browser_connection->get_title()); } +// POST /session/{session id}/refresh https://w3c.github.io/webdriver/#dfn-refresh +ErrorOr Session::refresh() +{ + // 1. If the current top-level browsing context is no longer open, return error with error code no such window. + auto current_window = get_window_object(); + if (!current_window.has_value()) + return HttpError { 404, "no such window", "Window not found" }; + + // FIXME: 2. Handle any user prompts and return its value if it is an error. + + // 3. Initiate an overridden reload of the current top-level browsing context’s active document. + m_browser_connection->async_refresh(); + + // FIXME: 4. If url is special except for file: + + // FIXME: 1. Try to wait for navigation to complete. + + // FIXME: 2. Try to run the post-navigation checks. + + // FIXME: 5. Set the current browsing context with current top-level browsing context. + + // 6. Return success with data null. + return JsonValue(); +} + } diff --git a/Userland/Services/WebDriver/Session.h b/Userland/Services/WebDriver/Session.h index 407702d988..a334322958 100644 --- a/Userland/Services/WebDriver/Session.h +++ b/Userland/Services/WebDriver/Session.h @@ -37,6 +37,7 @@ public: ErrorOr post_url(JsonValue const& url); ErrorOr get_url(); ErrorOr get_title(); + ErrorOr refresh(); private: NonnullRefPtr m_client;