Truly independent web browser
/*
* Copyright (c) 2021-2026, Tim Flynn <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Function.h>
#include <AK/HashMap.h>
#include <AK/Optional.h>
#include <AK/String.h>
#include <AK/StringView.h>
#include <AK/Traits.h>
#include <LibCore/Timer.h>
#include <LibDatabase/Forward.h>
#include <LibHTTP/Cookie/Cookie.h>
#include <LibRequests/CacheSizes.h>
#include <LibURL/Forward.h>
#include <LibWebCommon/Forward.h>
#include <LibWebView/BrowsingSession.h>
#include <LibWebView/Forward.h>
namespace WebView {
struct CookieStorageKey {
bool operator==(CookieStorageKey const&) const = default;
String name;
String domain;
bool host_only { false };
String path;
Utf16String partition_key;
};
class WEBVIEW_API CookieJar {
public:
AK_ALLOC_WITH_KMALLOC;
static ErrorOr<Database::MigrationOutcome> migrate_schema(Database::Database&, Database::MigrationMode = Database::MigrationMode::Apply);
static ErrorOr<NonnullOwnPtr<CookieJar>> create(Database::Database&);
static NonnullOwnPtr<CookieJar> create(IsPrivate = IsPrivate::No);
~CookieJar();
// A context of nothing stands for a first-party context, as for the UI process's own requests. A third-party context
// only uses the cookies set in a third-party context under the same top-level site. See HTTP::Cookie::PartitionContext.
String get_cookie(URL::URL const& url, HTTP::Cookie::Source source, Optional<HTTP::Cookie::PartitionContext> const&, HTTP::Cookie::SameSiteContext);
void set_cookie(URL::URL const& url, HTTP::Cookie::ParsedCookie const& parsed_cookie, HTTP::Cookie::Source source, Optional<HTTP::Cookie::PartitionContext> const&, HTTP::Cookie::SameSiteContext);
void update_cookie(HTTP::Cookie::Cookie);
ErrorOr<void> set_cookie_from_devtools(URL::URL const&, Optional<CookieStorageKey> old_key, HTTP::Cookie::Cookie);
bool delete_cookie(CookieStorageKey const&);
void dump_cookies();
Vector<HTTP::Cookie::Cookie> get_all_cookies();
Vector<HTTP::Cookie::Cookie> get_all_cookies_webdriver(URL::URL const& url, Optional<HTTP::Cookie::PartitionContext> const& = {});
Vector<HTTP::Cookie::Cookie> get_all_cookies_cookiestore(URL::URL const& url, Optional<HTTP::Cookie::PartitionContext> const&, HTTP::Cookie::SameSiteContext);
Optional<HTTP::Cookie::Cookie> get_named_cookie(URL::URL const& url, StringView name);
void expire_cookies_with_time_offset(AK::Duration);
void delete_all_cookies(URL::URL const&);
void expire_cookies_accessed_since(UnixDateTime since);
Requests::CacheSizes estimate_storage_size_accessed_since(UnixDateTime since) const;
private:
struct Statements {
Database::StatementID insert_cookie { 0 };
Database::StatementID expire_cookie { 0 };
Database::StatementID select_all_cookies { 0 };
};
class WEBVIEW_API TransientStorage {
public:
using Cookies = HashMap<CookieStorageKey, HTTP::Cookie::Cookie>;
explicit TransientStorage(IsPrivate);
void set_cookies(Cookies);
void set_cookie(CookieStorageKey, HTTP::Cookie::Cookie);
Optional<HTTP::Cookie::Cookie const&> get_cookie(CookieStorageKey const&);
size_t size() const { return m_cookies.size(); }
UnixDateTime purge_expired_cookies(Optional<AK::Duration> offset = {});
void expire_and_purge_cookies_accessed_since(UnixDateTime since);
Requests::CacheSizes estimate_storage_size_accessed_since(UnixDateTime since) const;
auto take_dirty_cookies() { return move(m_dirty_cookies); }
template<typename Callback>
void for_each_cookie(Callback callback)
{
using ReturnType = InvokeResult<Callback, HTTP::Cookie::Cookie&>;
for (auto& it : m_cookies) {
if constexpr (IsSame<ReturnType, IterationDecision>) {
if (callback(it.value) == IterationDecision::Break)
return;
} else {
static_assert(IsSame<ReturnType, void>);
callback(it.value);
}
}
}
private:
using CookieEntry = decltype(declval<Cookies>().take_all_matching(nullptr))::ValueType;
void send_cookie_changed_notifications(ReadonlySpan<CookieEntry>, bool inform_web_view_about_changed_domains = true);
IsPrivate m_is_private { IsPrivate::No };
Cookies m_cookies;
Cookies m_dirty_cookies;
};
struct WEBVIEW_API PersistedStorage {
void insert_cookie(HTTP::Cookie::Cookie const& cookie);
TransientStorage::Cookies select_all_cookies();
NonnullRefPtr<Database::Database> database;
Statements statements;
RefPtr<Core::Timer> synchronization_timer {};
};
CookieJar(Optional<PersistedStorage>, IsPrivate);
AK_MAKE_NONCOPYABLE(CookieJar);
AK_MAKE_NONMOVABLE(CookieJar);
enum class MatchingCookiesSpecMode {
RFC6265,
WebDriver,
};
Vector<HTTP::Cookie::Cookie> get_matching_cookies(URL::URL const& url, HTTP::Cookie::Source source, Utf16String const& partition_key, HTTP::Cookie::SameSiteContext, MatchingCookiesSpecMode mode = MatchingCookiesSpecMode::RFC6265);
Optional<PersistedStorage> m_persisted_storage;
TransientStorage m_transient_storage;
};
}
template<>
struct AK::Traits<WebView::CookieStorageKey> : public AK::DefaultTraits<WebView::CookieStorageKey> {
static unsigned hash(WebView::CookieStorageKey const& key)
{
unsigned hash = 0;
hash = pair_int_hash(hash, key.name.hash());
hash = pair_int_hash(hash, key.domain.hash());
hash = pair_int_hash(hash, key.host_only);
hash = pair_int_hash(hash, key.path.hash());
hash = pair_int_hash(hash, key.partition_key.hash());
return hash;
}
};