瀏覽代碼

Browser+WebContent+WebDriver: Move Get Named Cookie to WebContent

Instead of sending *all* cookies over IPC and filtering by name, we now
filter by name from the cookie jar and send just the first matching
cookie.
Timothy Flynn 2 年之前
父節點
當前提交
a3d6c2f6af

+ 4 - 0
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);
     };

+ 16 - 0
Userland/Applications/Browser/CookieJar.cpp

@@ -134,6 +134,22 @@ Vector<Web::Cookie::Cookie> CookieJar::get_all_cookies(URL const& url)
     return cookies;
 }
 
+Optional<Web::Cookie::Cookie> 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<String> CookieJar::canonicalize_domain(const URL& url)
 {
     // https://tools.ietf.org/html/rfc6265#section-5.1.2

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

@@ -32,6 +32,7 @@ public:
     void dump_cookies() const;
     Vector<Web::Cookie::Cookie> get_all_cookies() const;
     Vector<Web::Cookie::Cookie> get_all_cookies(URL const& url);
+    Optional<Web::Cookie::Cookie> get_named_cookie(URL const& url, String const& name);
 
 private:
     static Optional<String> canonicalize_domain(const URL& url);

+ 6 - 0
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<Web::Cookie::Cookie> {
+        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);

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

@@ -65,6 +65,7 @@ public:
     Function<void(Tab&)> on_tab_close_other_request;
     Function<void(Gfx::Bitmap const&)> on_favicon_change;
     Function<Vector<Web::Cookie::Cookie>(AK::URL const& url)> on_get_all_cookies;
+    Function<Optional<Web::Cookie::Cookie>(AK::URL const& url, String const& name)> on_get_named_cookie;
     Function<String(const URL&, Web::Cookie::Source source)> on_get_cookie;
     Function<void(const URL&, Web::Cookie::ParsedCookie const& cookie, Web::Cookie::Source source)> on_set_cookie;
     Function<void()> on_dump_cookies;

+ 0 - 15
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<Web::Cookie::Cookie> {};
-        }
-    }
-    return { {} };
-}
-
 void WebDriverConnection::add_cookie(Web::Cookie::ParsedCookie const& cookie)
 {
     dbgln_if(WEBDRIVER_DEBUG, "WebDriverConnection: add_cookie {}", cookie.name);

+ 0 - 1
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;
 

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

@@ -19,7 +19,6 @@ endpoint WebDriverSessionClient {
     back() =|
     forward() =|
     get_all_cookies() => (Vector<Web::Cookie::Cookie> cookies)
-    get_named_cookie(String name) => (Optional<Web::Cookie::Cookie> cookie)
     add_cookie(Web::Cookie::ParsedCookie cookie) =|
     update_cookie(Web::Cookie::Cookie cookie) =|
 }

+ 1 - 0
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)
 }

+ 20 - 0
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()
 {

+ 1 - 0
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;
 

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

@@ -762,8 +762,7 @@ Web::WebDriver::Response Client::handle_get_named_cookie(Vector<StringView> cons
 {
     dbgln_if(WEBDRIVER_DEBUG, "Handling GET /session/<session_id>/cookie/<name>");
     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

+ 0 - 37
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)
 {

+ 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 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();