2021-04-11 17:43:54 +03:00
|
|
|
/*
|
2023-04-20 21:22:40 +03:00
|
|
|
* Copyright (c) 2021-2023, Tim Flynn <trflynn89@serenityos.org>
|
2021-04-11 17:43:54 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2021-04-11 17:43:54 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2022-12-04 21:02:33 +03:00
|
|
|
#include <AK/DeprecatedString.h>
|
2022-10-27 19:56:22 +03:00
|
|
|
#include <AK/Function.h>
|
2021-04-11 17:43:54 +03:00
|
|
|
#include <AK/HashMap.h>
|
|
|
|
#include <AK/Optional.h>
|
2023-04-20 21:22:40 +03:00
|
|
|
#include <AK/Traits.h>
|
2021-04-12 06:40:49 +03:00
|
|
|
#include <LibCore/DateTime.h>
|
2022-10-27 19:56:22 +03:00
|
|
|
#include <LibSQL/Type.h>
|
2021-04-14 00:01:20 +03:00
|
|
|
#include <LibWeb/Cookie/Cookie.h>
|
2021-04-13 23:47:05 +03:00
|
|
|
#include <LibWeb/Forward.h>
|
2023-08-31 14:07:07 +03:00
|
|
|
#include <LibWebView/Forward.h>
|
2021-04-11 17:43:54 +03:00
|
|
|
|
2023-08-31 14:07:07 +03:00
|
|
|
namespace WebView {
|
2021-04-11 17:43:54 +03:00
|
|
|
|
2023-04-20 21:22:40 +03:00
|
|
|
struct CookieStorageKey {
|
|
|
|
bool operator==(CookieStorageKey const&) const = default;
|
|
|
|
|
|
|
|
DeprecatedString name;
|
|
|
|
DeprecatedString domain;
|
|
|
|
DeprecatedString path;
|
|
|
|
};
|
|
|
|
|
2021-04-11 17:43:54 +03:00
|
|
|
class CookieJar {
|
2022-10-27 19:56:22 +03: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 21:22:40 +03:00
|
|
|
struct PersistedStorage {
|
|
|
|
Database& database;
|
|
|
|
Statements statements;
|
|
|
|
};
|
|
|
|
|
|
|
|
using TransientStorage = HashMap<CookieStorageKey, Web::Cookie::Cookie>;
|
|
|
|
|
2021-04-11 17:43:54 +03:00
|
|
|
public:
|
2023-04-20 21:22:40 +03:00
|
|
|
static ErrorOr<CookieJar> create(Database&);
|
|
|
|
static CookieJar create();
|
2022-10-27 19:56:22 +03:00
|
|
|
|
2022-12-04 21:02:33 +03:00
|
|
|
DeprecatedString 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 19:24:04 +03:00
|
|
|
void update_cookie(Web::Cookie::Cookie);
|
2022-10-27 19:56:22 +03:00
|
|
|
void dump_cookies();
|
|
|
|
Vector<Web::Cookie::Cookie> get_all_cookies();
|
2022-11-11 17:24:07 +03:00
|
|
|
Vector<Web::Cookie::Cookie> get_all_cookies(URL const& url);
|
2022-12-04 21:02:33 +03:00
|
|
|
Optional<Web::Cookie::Cookie> get_named_cookie(URL const& url, DeprecatedString const& name);
|
2021-04-11 17:43:54 +03:00
|
|
|
|
|
|
|
private:
|
2023-04-20 21:22:40 +03:00
|
|
|
explicit CookieJar(PersistedStorage);
|
|
|
|
explicit CookieJar(TransientStorage);
|
2022-10-27 19:56:22 +03:00
|
|
|
|
2022-12-04 21:02:33 +03:00
|
|
|
static Optional<DeprecatedString> canonicalize_domain(const URL& url);
|
|
|
|
static bool domain_matches(DeprecatedString const& string, DeprecatedString const& domain_string);
|
|
|
|
static bool path_matches(DeprecatedString const& request_path, DeprecatedString const& cookie_path);
|
|
|
|
static DeprecatedString default_path(const URL& url);
|
2021-04-13 06:16:27 +03:00
|
|
|
|
2022-11-11 17:24:07 +03:00
|
|
|
enum class MatchingCookiesSpecMode {
|
|
|
|
RFC6265,
|
|
|
|
WebDriver,
|
|
|
|
};
|
|
|
|
|
2022-12-04 21:02:33 +03:00
|
|
|
void store_cookie(Web::Cookie::ParsedCookie const& parsed_cookie, const URL& url, DeprecatedString canonicalized_domain, Web::Cookie::Source source);
|
2022-10-27 19:56:22 +03:00
|
|
|
Vector<Web::Cookie::Cookie> get_matching_cookies(const URL& url, DeprecatedString const& canonicalized_domain, Web::Cookie::Source source, MatchingCookiesSpecMode mode = MatchingCookiesSpecMode::RFC6265);
|
2021-04-13 06:16:27 +03:00
|
|
|
|
2022-10-27 19:56:22 +03:00
|
|
|
void insert_cookie_into_database(Web::Cookie::Cookie const& cookie);
|
|
|
|
void update_cookie_in_database(Web::Cookie::Cookie const& cookie);
|
2021-04-13 06:16:27 +03:00
|
|
|
|
2022-10-27 19:56:22 +03: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-13 06:16:27 +03:00
|
|
|
|
2023-04-20 21:22:40 +03:00
|
|
|
Variant<PersistedStorage, TransientStorage> m_storage;
|
2021-04-11 17:43:54 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
2023-04-20 21:22:40 +03:00
|
|
|
|
|
|
|
template<>
|
2023-08-31 14:07:07 +03:00
|
|
|
struct AK::Traits<WebView::CookieStorageKey> : public AK::GenericTraits<WebView::CookieStorageKey> {
|
|
|
|
static unsigned hash(WebView::CookieStorageKey const& key)
|
2023-04-20 21:22:40 +03: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;
|
|
|
|
}
|
|
|
|
};
|