2021-04-11 10:43:54 -04:00
|
|
|
/*
|
2024-01-26 10:47:57 -05:00
|
|
|
* Copyright (c) 2021-2024, Tim Flynn <trflynn89@serenityos.org>
|
2021-04-11 10:43:54 -04:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2021-04-11 10:43:54 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
#include <AK/ByteString.h>
|
2022-10-27 12:56:22 -04:00
|
|
|
#include <AK/Function.h>
|
2021-04-11 10:43:54 -04:00
|
|
|
#include <AK/HashMap.h>
|
|
|
|
#include <AK/Optional.h>
|
2024-01-26 10:47:57 -05:00
|
|
|
#include <AK/StringView.h>
|
2023-04-20 14:22:40 -04:00
|
|
|
#include <AK/Traits.h>
|
2021-04-11 23:40:49 -04:00
|
|
|
#include <LibCore/DateTime.h>
|
2022-10-27 12:56:22 -04:00
|
|
|
#include <LibSQL/Type.h>
|
2021-04-13 17:01:20 -04:00
|
|
|
#include <LibWeb/Cookie/Cookie.h>
|
2021-04-13 16:47:05 -04:00
|
|
|
#include <LibWeb/Forward.h>
|
2023-08-31 07:07:07 -04:00
|
|
|
#include <LibWebView/Forward.h>
|
2021-04-11 10:43:54 -04:00
|
|
|
|
2023-08-31 07:07:07 -04:00
|
|
|
namespace WebView {
|
2021-04-11 10:43:54 -04:00
|
|
|
|
2023-04-20 14:22:40 -04:00
|
|
|
struct CookieStorageKey {
|
|
|
|
bool operator==(CookieStorageKey const&) const = default;
|
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
ByteString name;
|
|
|
|
ByteString domain;
|
|
|
|
ByteString path;
|
2023-04-20 14:22:40 -04:00
|
|
|
};
|
|
|
|
|
2021-04-11 10:43:54 -04:00
|
|
|
class CookieJar {
|
2022-10-27 12:56:22 -04:00
|
|
|
struct Statements {
|
|
|
|
SQL::StatementID create_table { 0 };
|
|
|
|
SQL::StatementID insert_cookie { 0 };
|
|
|
|
SQL::StatementID update_cookie { 0 };
|
|
|
|
SQL::StatementID expire_cookie { 0 };
|
|
|
|
SQL::StatementID select_cookie { 0 };
|
|
|
|
SQL::StatementID select_all_cookies { 0 };
|
|
|
|
};
|
|
|
|
|
2023-04-20 14:22:40 -04:00
|
|
|
struct PersistedStorage {
|
|
|
|
Database& database;
|
|
|
|
Statements statements;
|
|
|
|
};
|
|
|
|
|
|
|
|
using TransientStorage = HashMap<CookieStorageKey, Web::Cookie::Cookie>;
|
|
|
|
|
2021-04-11 10:43:54 -04:00
|
|
|
public:
|
2023-04-20 14:22:40 -04:00
|
|
|
static ErrorOr<CookieJar> create(Database&);
|
|
|
|
static CookieJar create();
|
2022-10-27 12:56:22 -04:00
|
|
|
|
2024-01-26 11:10:15 -05:00
|
|
|
String get_cookie(const URL& url, Web::Cookie::Source source);
|
2022-04-01 20:58:27 +03:00
|
|
|
void set_cookie(const URL& url, Web::Cookie::ParsedCookie const& parsed_cookie, Web::Cookie::Source source);
|
2022-11-28 11:24:04 -05:00
|
|
|
void update_cookie(Web::Cookie::Cookie);
|
2022-10-27 12:56:22 -04:00
|
|
|
void dump_cookies();
|
|
|
|
Vector<Web::Cookie::Cookie> get_all_cookies();
|
2022-11-11 09:24:07 -05:00
|
|
|
Vector<Web::Cookie::Cookie> get_all_cookies(URL const& url);
|
2024-01-26 10:47:57 -05:00
|
|
|
Optional<Web::Cookie::Cookie> get_named_cookie(URL const& url, StringView name);
|
2021-04-11 10:43:54 -04:00
|
|
|
|
|
|
|
private:
|
2023-04-20 14:22:40 -04:00
|
|
|
explicit CookieJar(PersistedStorage);
|
|
|
|
explicit CookieJar(TransientStorage);
|
2022-10-27 12:56:22 -04:00
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
static Optional<ByteString> canonicalize_domain(const URL& url);
|
2024-01-26 10:47:57 -05:00
|
|
|
static bool domain_matches(StringView string, StringView domain_string);
|
|
|
|
static bool path_matches(StringView request_path, StringView cookie_path);
|
2023-12-16 17:49:34 +03:30
|
|
|
static ByteString default_path(const URL& url);
|
2021-04-12 23:16:27 -04:00
|
|
|
|
2022-11-11 09:24:07 -05:00
|
|
|
enum class MatchingCookiesSpecMode {
|
|
|
|
RFC6265,
|
|
|
|
WebDriver,
|
|
|
|
};
|
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
void store_cookie(Web::Cookie::ParsedCookie const& parsed_cookie, const URL& url, ByteString canonicalized_domain, Web::Cookie::Source source);
|
2024-01-26 10:47:57 -05:00
|
|
|
Vector<Web::Cookie::Cookie> get_matching_cookies(const URL& url, StringView canonicalized_domain, Web::Cookie::Source source, MatchingCookiesSpecMode mode = MatchingCookiesSpecMode::RFC6265);
|
2021-04-12 23:16:27 -04:00
|
|
|
|
2022-10-27 12:56:22 -04:00
|
|
|
void insert_cookie_into_database(Web::Cookie::Cookie const& cookie);
|
|
|
|
void update_cookie_in_database(Web::Cookie::Cookie const& cookie);
|
2021-04-12 23:16:27 -04:00
|
|
|
|
2022-10-27 12:56:22 -04:00
|
|
|
using OnCookieFound = Function<void(Web::Cookie::Cookie&, Web::Cookie::Cookie)>;
|
|
|
|
using OnCookieNotFound = Function<void(Web::Cookie::Cookie)>;
|
|
|
|
void select_cookie_from_database(Web::Cookie::Cookie cookie, OnCookieFound on_result, OnCookieNotFound on_complete_without_results);
|
|
|
|
|
|
|
|
using OnSelectAllCookiesResult = Function<void(Web::Cookie::Cookie)>;
|
|
|
|
void select_all_cookies_from_database(OnSelectAllCookiesResult on_result);
|
|
|
|
|
|
|
|
void purge_expired_cookies();
|
2021-04-12 23:16:27 -04:00
|
|
|
|
2023-04-20 14:22:40 -04:00
|
|
|
Variant<PersistedStorage, TransientStorage> m_storage;
|
2021-04-11 10:43:54 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
2023-04-20 14:22:40 -04:00
|
|
|
|
|
|
|
template<>
|
2023-11-08 20:29:12 +01:00
|
|
|
struct AK::Traits<WebView::CookieStorageKey> : public AK::DefaultTraits<WebView::CookieStorageKey> {
|
2023-08-31 07:07:07 -04:00
|
|
|
static unsigned hash(WebView::CookieStorageKey const& key)
|
2023-04-20 14:22:40 -04:00
|
|
|
{
|
|
|
|
unsigned hash = 0;
|
|
|
|
hash = pair_int_hash(hash, string_hash(key.name.characters(), key.name.length()));
|
|
|
|
hash = pair_int_hash(hash, string_hash(key.domain.characters(), key.domain.length()));
|
|
|
|
hash = pair_int_hash(hash, string_hash(key.path.characters(), key.path.length()));
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
};
|