diff --git a/Userland/Applications/Browser/BrowserWindow.cpp b/Userland/Applications/Browser/BrowserWindow.cpp index ba2dac87ea..eb7c25064c 100644 --- a/Userland/Applications/Browser/BrowserWindow.cpp +++ b/Userland/Applications/Browser/BrowserWindow.cpp @@ -579,6 +579,10 @@ void BrowserWindow::create_new_tab(URL url, bool activate) return m_cookie_jar.get_all_cookies(url); }; + new_tab.on_get_named_cookie = [this](auto& url, auto& name) { + return m_cookie_jar.get_named_cookie(url, name); + }; + new_tab.on_get_cookie = [this](auto& url, auto source) -> String { return m_cookie_jar.get_cookie(url, source); }; diff --git a/Userland/Applications/Browser/CookieJar.cpp b/Userland/Applications/Browser/CookieJar.cpp index bf1ec26260..5d44870c91 100644 --- a/Userland/Applications/Browser/CookieJar.cpp +++ b/Userland/Applications/Browser/CookieJar.cpp @@ -134,6 +134,22 @@ Vector CookieJar::get_all_cookies(URL const& url) return cookies; } +Optional CookieJar::get_named_cookie(URL const& url, String const& name) +{ + auto domain = canonicalize_domain(url); + if (!domain.has_value()) + return {}; + + auto cookie_list = get_matching_cookies(url, domain.value(), Web::Cookie::Source::Http, MatchingCookiesSpecMode::WebDriver); + + for (auto const& cookie : cookie_list) { + if (cookie.name == name) + return cookie; + } + + return {}; +} + Optional CookieJar::canonicalize_domain(const URL& url) { // https://tools.ietf.org/html/rfc6265#section-5.1.2 diff --git a/Userland/Applications/Browser/CookieJar.h b/Userland/Applications/Browser/CookieJar.h index 2db6f41b73..89ce521ee5 100644 --- a/Userland/Applications/Browser/CookieJar.h +++ b/Userland/Applications/Browser/CookieJar.h @@ -32,6 +32,7 @@ public: void dump_cookies() const; Vector get_all_cookies() const; Vector get_all_cookies(URL const& url); + Optional get_named_cookie(URL const& url, String const& name); private: static Optional canonicalize_domain(const URL& url); diff --git a/Userland/Applications/Browser/Tab.cpp b/Userland/Applications/Browser/Tab.cpp index 905978ab3f..81e0bb388f 100644 --- a/Userland/Applications/Browser/Tab.cpp +++ b/Userland/Applications/Browser/Tab.cpp @@ -357,6 +357,12 @@ Tab::Tab(BrowserWindow& window) return {}; }; + view().on_get_named_cookie = [this](auto& url, auto& name) -> Optional { + if (on_get_named_cookie) + return on_get_named_cookie(url, name); + return {}; + }; + view().on_get_cookie = [this](auto& url, auto source) -> String { if (on_get_cookie) return on_get_cookie(url, source); diff --git a/Userland/Applications/Browser/Tab.h b/Userland/Applications/Browser/Tab.h index 9477fefb7f..2334631094 100644 --- a/Userland/Applications/Browser/Tab.h +++ b/Userland/Applications/Browser/Tab.h @@ -65,6 +65,7 @@ public: Function on_tab_close_other_request; Function on_favicon_change; Function(AK::URL const& url)> on_get_all_cookies; + Function(AK::URL const& url, String const& name)> on_get_named_cookie; Function on_get_cookie; Function on_set_cookie; Function on_dump_cookies; diff --git a/Userland/Applications/Browser/WebDriverConnection.cpp b/Userland/Applications/Browser/WebDriverConnection.cpp index 2785cafe6a..4119f8e3a6 100644 --- a/Userland/Applications/Browser/WebDriverConnection.cpp +++ b/Userland/Applications/Browser/WebDriverConnection.cpp @@ -69,21 +69,6 @@ Messages::WebDriverSessionClient::GetAllCookiesResponse WebDriverConnection::get return { {} }; } -Messages::WebDriverSessionClient::GetNamedCookieResponse WebDriverConnection::get_named_cookie(String const& name) -{ - dbgln_if(WEBDRIVER_DEBUG, "WebDriverConnection: get_named_cookie {}", name); - if (auto browser_window = m_browser_window.strong_ref()) { - if (browser_window->active_tab().on_get_cookies_entries) { - for (auto cookie : browser_window->active_tab().on_get_cookies_entries()) { - if (cookie.name == name) - return { cookie }; - } - return Optional {}; - } - } - return { {} }; -} - void WebDriverConnection::add_cookie(Web::Cookie::ParsedCookie const& cookie) { dbgln_if(WEBDRIVER_DEBUG, "WebDriverConnection: add_cookie {}", cookie.name); diff --git a/Userland/Applications/Browser/WebDriverConnection.h b/Userland/Applications/Browser/WebDriverConnection.h index e10bbf5bb7..9da9b4bf41 100644 --- a/Userland/Applications/Browser/WebDriverConnection.h +++ b/Userland/Applications/Browser/WebDriverConnection.h @@ -43,7 +43,6 @@ public: virtual void back() override; virtual void forward() 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; virtual void update_cookie(Web::Cookie::Cookie const&) override; diff --git a/Userland/Applications/Browser/WebDriverSessionClient.ipc b/Userland/Applications/Browser/WebDriverSessionClient.ipc index ef0d9c6db0..98103889d2 100644 --- a/Userland/Applications/Browser/WebDriverSessionClient.ipc +++ b/Userland/Applications/Browser/WebDriverSessionClient.ipc @@ -19,7 +19,6 @@ endpoint WebDriverSessionClient { back() =| forward() =| get_all_cookies() => (Vector cookies) - get_named_cookie(String name) => (Optional cookie) add_cookie(Web::Cookie::ParsedCookie cookie) =| update_cookie(Web::Cookie::Cookie cookie) =| } diff --git a/Userland/Services/WebContent/WebDriverClient.ipc b/Userland/Services/WebContent/WebDriverClient.ipc index a7d70c8ff5..9fd833e53e 100644 --- a/Userland/Services/WebContent/WebDriverClient.ipc +++ b/Userland/Services/WebContent/WebDriverClient.ipc @@ -25,6 +25,7 @@ endpoint WebDriverClient { execute_script(JsonValue payload) => (Web::WebDriver::Response response) execute_async_script(JsonValue payload) => (Web::WebDriver::Response response) get_all_cookies() => (Web::WebDriver::Response response) + get_named_cookie(String name) => (Web::WebDriver::Response response) take_screenshot() => (Web::WebDriver::Response response) take_element_screenshot(String element_id) => (Web::WebDriver::Response response) } diff --git a/Userland/Services/WebContent/WebDriverConnection.cpp b/Userland/Services/WebContent/WebDriverConnection.cpp index 94b12c8a46..81470c73f0 100644 --- a/Userland/Services/WebContent/WebDriverConnection.cpp +++ b/Userland/Services/WebContent/WebDriverConnection.cpp @@ -872,6 +872,26 @@ Messages::WebDriverClient::GetAllCookiesResponse WebDriverConnection::get_all_co return make_success_response(move(cookies)); } +// 14.2 Get Named Cookie, https://w3c.github.io/webdriver/#dfn-get-named-cookie +Messages::WebDriverClient::GetNamedCookieResponse WebDriverConnection::get_named_cookie(String const& name) +{ + // 1. 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: 2. Handle any user prompts, and return its value if it is an error. + + // 3. If the url variable name is equal to a cookie’s cookie name amongst all associated cookies of the current browsing context’s active document, return success with the serialized cookie as data. + auto* document = m_page_host.page().top_level_browsing_context().active_document(); + + if (auto cookie = m_web_content_client.did_request_named_cookie(document->url(), name); cookie.has_value()) { + auto serialized_cookie = serialize_cookie(*cookie); + return make_success_response(move(serialized_cookie)); + } + + // 4. Otherwise, return error with error code no such cookie. + return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::NoSuchCookie, String::formatted("Cookie '{}' not found", name)); +} + // 17.1 Take Screenshot, https://w3c.github.io/webdriver/#take-screenshot Messages::WebDriverClient::TakeScreenshotResponse WebDriverConnection::take_screenshot() { diff --git a/Userland/Services/WebContent/WebDriverConnection.h b/Userland/Services/WebContent/WebDriverConnection.h index 91f6735056..69a38fb765 100644 --- a/Userland/Services/WebContent/WebDriverConnection.h +++ b/Userland/Services/WebContent/WebDriverConnection.h @@ -57,6 +57,7 @@ private: virtual Messages::WebDriverClient::ExecuteScriptResponse execute_script(JsonValue const& payload) override; virtual Messages::WebDriverClient::ExecuteAsyncScriptResponse execute_async_script(JsonValue const& payload) override; virtual Messages::WebDriverClient::GetAllCookiesResponse get_all_cookies() override; + virtual Messages::WebDriverClient::GetNamedCookieResponse get_named_cookie(String const& name) override; virtual Messages::WebDriverClient::TakeScreenshotResponse take_screenshot() override; virtual Messages::WebDriverClient::TakeElementScreenshotResponse take_element_screenshot(String const& element_id) override; diff --git a/Userland/Services/WebDriver/Client.cpp b/Userland/Services/WebDriver/Client.cpp index c5f660eacc..5ee58593af 100644 --- a/Userland/Services/WebDriver/Client.cpp +++ b/Userland/Services/WebDriver/Client.cpp @@ -762,8 +762,7 @@ Web::WebDriver::Response Client::handle_get_named_cookie(Vector cons { dbgln_if(WEBDRIVER_DEBUG, "Handling GET /session//cookie/"); auto* session = TRY(find_session_with_id(parameters[0])); - auto cookies = TRY(session->get_named_cookie(parameters[1])); - return make_json_value(cookies); + return session->web_content_connection().get_named_cookie(parameters[1]); } // 14.3 Add Cookie, https://w3c.github.io/webdriver/#dfn-adding-a-cookie diff --git a/Userland/Services/WebDriver/Session.cpp b/Userland/Services/WebDriver/Session.cpp index 3c9c816c8c..16aa2f94ea 100644 --- a/Userland/Services/WebDriver/Session.cpp +++ b/Userland/Services/WebDriver/Session.cpp @@ -297,43 +297,6 @@ Web::WebDriver::Response Session::get_window_handles() const return JsonValue { handles }; } -// https://w3c.github.io/webdriver/#dfn-serialized-cookie -static JsonObject serialize_cookie(Web::Cookie::Cookie const& cookie) -{ - JsonObject serialized_cookie = {}; - serialized_cookie.set("name", cookie.name); - serialized_cookie.set("value", cookie.value); - serialized_cookie.set("path", cookie.path); - serialized_cookie.set("domain", cookie.domain); - serialized_cookie.set("secure", cookie.secure); - serialized_cookie.set("httpOnly", cookie.http_only); - serialized_cookie.set("expiry", cookie.expiry_time.timestamp()); - // FIXME: Add sameSite to Cookie and serialize it here too. - - return serialized_cookie; -} - -// 14.2 Get Named Cookie, https://w3c.github.io/webdriver/#dfn-get-named-cookie -Web::WebDriver::Response Session::get_named_cookie(String const& name) -{ - // 1. 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: 2. Handle any user prompts, and return its value if it is an error. - - // 3. If the url variable name is equal to a cookie’s cookie name amongst all associated cookies of the - // current browsing context’s active document, return success with the serialized cookie as data. - auto maybe_cookie = m_browser_connection->get_named_cookie(name); - if (maybe_cookie.has_value()) { - auto cookie = maybe_cookie.release_value(); - auto serialized_cookie = serialize_cookie(cookie); - return JsonValue(serialized_cookie); - } - - // 4. Otherwise, return error with error code no such cookie. - return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::NoSuchCookie, "Cookie not found"); -} - // 14.3 Add Cookie, https://w3c.github.io/webdriver/#dfn-adding-a-cookie Web::WebDriver::Response Session::add_cookie(JsonValue const& payload) { diff --git a/Userland/Services/WebDriver/Session.h b/Userland/Services/WebDriver/Session.h index cb7fcdccc5..c60cc8b654 100644 --- a/Userland/Services/WebDriver/Session.h +++ b/Userland/Services/WebDriver/Session.h @@ -58,7 +58,6 @@ public: Web::WebDriver::Response get_window_handle(); ErrorOr> close_window(); Web::WebDriver::Response get_window_handles() const; - Web::WebDriver::Response get_named_cookie(String const& name); Web::WebDriver::Response add_cookie(JsonValue const& payload); Web::WebDriver::Response delete_cookie(StringView name); Web::WebDriver::Response delete_all_cookies();