Browser: Allow changing of existing Cookies in the CookieJar

And attach all the plumbing through to Tab over BrowserWindow.
This commit is contained in:
Tobias Christiansen 2022-10-16 19:48:19 +02:00 committed by Linus Groh
parent 813ca5ebbe
commit 30360918d4
Notes: sideshowbarker 2024-07-17 18:08:55 +09:00
4 changed files with 35 additions and 0 deletions

View File

@ -566,6 +566,10 @@ void BrowserWindow::create_new_tab(URL url, bool activate)
m_cookie_jar.dump_cookies();
};
new_tab.on_update_cookie = [this](auto const& url, auto cookie) {
m_cookie_jar.update_cookie(url, move(cookie));
};
new_tab.on_get_cookies_entries = [this]() {
return m_cookie_jar.get_all_cookies();
};

View File

@ -1,6 +1,7 @@
/*
* Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org>
* Copyright (c) 2022, the SerenityOS developers.
* Copyright (c) 2022, Tobias Christiansen <tobyase@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -48,6 +49,34 @@ void CookieJar::set_cookie(const URL& url, Web::Cookie::ParsedCookie const& pars
purge_expired_cookies();
}
// This is based on https://www.rfc-editor.org/rfc/rfc6265#section-5.3 as store_cookie() below
// however the whole ParsedCookie->Cookie conversion is skipped.
void CookieJar::update_cookie(URL const& url, Web::Cookie::Cookie cookie)
{
auto domain = canonicalize_domain(url);
if (!domain.has_value())
return;
// 6. If the canonicalized request-host does not domain-match the domain-attribute: Ignore the cookie entirely and abort these steps.
if (!domain_matches(domain.value(), cookie.domain))
return;
// 11. If the cookie store contains a cookie with the same name, domain, and path as the newly created cookie:
CookieStorageKey key { cookie.name, cookie.domain, cookie.path };
if (auto old_cookie = m_cookies.find(key); old_cookie != m_cookies.end()) {
// Update the creation-time of the newly created cookie to match the creation-time of the old-cookie.
cookie.creation_time = old_cookie->value.creation_time;
// Remove the old-cookie from the cookie store.
m_cookies.remove(old_cookie);
}
// 12. Insert the newly created cookie into the cookie store.
m_cookies.set(key, move(cookie));
purge_expired_cookies();
}
void CookieJar::dump_cookies() const
{
constexpr auto key_color = "\033[34;1m"sv;

View File

@ -28,6 +28,7 @@ class CookieJar {
public:
String get_cookie(const URL& url, Web::Cookie::Source source);
void set_cookie(const URL& url, Web::Cookie::ParsedCookie const& parsed_cookie, Web::Cookie::Source source);
void update_cookie(URL const&, Web::Cookie::Cookie);
void dump_cookies() const;
Vector<Web::Cookie::Cookie> get_all_cookies() const;

View File

@ -64,6 +64,7 @@ public:
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;
Function<void(URL const&, Web::Cookie::Cookie)> on_update_cookie;
Function<Vector<Web::Cookie::Cookie>()> on_get_cookies_entries;
Function<OrderedHashMap<String, String>()> on_get_local_storage_entries;
Function<OrderedHashMap<String, String>()> on_get_session_storage_entries;