forked from SerenityOS/serenity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CookieJar.h
105 lines (83 loc) · 3.68 KB
/
CookieJar.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
* Copyright (c) 2021-2023, Tim Flynn <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include "Forward.h"
#include <AK/DeprecatedString.h>
#include <AK/Function.h>
#include <AK/HashMap.h>
#include <AK/Optional.h>
#include <AK/Traits.h>
#include <LibCore/DateTime.h>
#include <LibSQL/Type.h>
#include <LibWeb/Cookie/Cookie.h>
#include <LibWeb/Forward.h>
namespace Browser {
class Database;
struct CookieStorageKey {
bool operator==(CookieStorageKey const&) const = default;
DeprecatedString name;
DeprecatedString domain;
DeprecatedString path;
};
class CookieJar {
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 };
};
struct PersistedStorage {
Database& database;
Statements statements;
};
using TransientStorage = HashMap<CookieStorageKey, Web::Cookie::Cookie>;
public:
static ErrorOr<CookieJar> create(Database&);
static CookieJar create();
DeprecatedString 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(Web::Cookie::Cookie);
void dump_cookies();
Vector<Web::Cookie::Cookie> get_all_cookies();
Vector<Web::Cookie::Cookie> get_all_cookies(URL const& url);
Optional<Web::Cookie::Cookie> get_named_cookie(URL const& url, DeprecatedString const& name);
private:
explicit CookieJar(PersistedStorage);
explicit CookieJar(TransientStorage);
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);
enum class MatchingCookiesSpecMode {
RFC6265,
WebDriver,
};
void store_cookie(Web::Cookie::ParsedCookie const& parsed_cookie, const URL& url, DeprecatedString canonicalized_domain, Web::Cookie::Source source);
Vector<Web::Cookie::Cookie> get_matching_cookies(const URL& url, DeprecatedString const& canonicalized_domain, Web::Cookie::Source source, MatchingCookiesSpecMode mode = MatchingCookiesSpecMode::RFC6265);
void insert_cookie_into_database(Web::Cookie::Cookie const& cookie);
void update_cookie_in_database(Web::Cookie::Cookie const& cookie);
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();
Variant<PersistedStorage, TransientStorage> m_storage;
};
}
template<>
struct AK::Traits<Browser::CookieStorageKey> : public AK::GenericTraits<Browser::CookieStorageKey> {
static unsigned hash(Browser::CookieStorageKey const& key)
{
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;
}
};