CookieJar.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (c) 2021-2024, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Function.h>
  8. #include <AK/HashMap.h>
  9. #include <AK/Optional.h>
  10. #include <AK/String.h>
  11. #include <AK/StringView.h>
  12. #include <AK/Traits.h>
  13. #include <LibCore/DateTime.h>
  14. #include <LibCore/Timer.h>
  15. #include <LibURL/Forward.h>
  16. #include <LibWeb/Cookie/Cookie.h>
  17. #include <LibWeb/Forward.h>
  18. #include <LibWebView/Database.h>
  19. #include <LibWebView/Forward.h>
  20. namespace WebView {
  21. struct CookieStorageKey {
  22. bool operator==(CookieStorageKey const&) const = default;
  23. String name;
  24. String domain;
  25. String path;
  26. };
  27. class CookieJar {
  28. struct Statements {
  29. Database::StatementID insert_cookie { 0 };
  30. Database::StatementID expire_cookie { 0 };
  31. Database::StatementID select_all_cookies { 0 };
  32. };
  33. class TransientStorage {
  34. public:
  35. using Cookies = HashMap<CookieStorageKey, Web::Cookie::Cookie>;
  36. void set_cookies(Cookies);
  37. void set_cookie(CookieStorageKey, Web::Cookie::Cookie);
  38. Optional<Web::Cookie::Cookie> get_cookie(CookieStorageKey const&);
  39. size_t size() const { return m_cookies.size(); }
  40. UnixDateTime purge_expired_cookies();
  41. auto take_dirty_cookies() { return move(m_dirty_cookies); }
  42. template<typename Callback>
  43. void for_each_cookie(Callback callback)
  44. {
  45. for (auto& it : m_cookies)
  46. callback(it.value);
  47. }
  48. private:
  49. Cookies m_cookies;
  50. Cookies m_dirty_cookies;
  51. };
  52. struct PersistedStorage {
  53. void insert_cookie(Web::Cookie::Cookie const& cookie);
  54. TransientStorage::Cookies select_all_cookies();
  55. Database& database;
  56. Statements statements;
  57. RefPtr<Core::Timer> synchronization_timer {};
  58. };
  59. public:
  60. static ErrorOr<NonnullOwnPtr<CookieJar>> create(Database&);
  61. static NonnullOwnPtr<CookieJar> create();
  62. ~CookieJar();
  63. String get_cookie(const URL::URL& url, Web::Cookie::Source source);
  64. void set_cookie(const URL::URL& url, Web::Cookie::ParsedCookie const& parsed_cookie, Web::Cookie::Source source);
  65. void update_cookie(Web::Cookie::Cookie);
  66. void dump_cookies();
  67. Vector<Web::Cookie::Cookie> get_all_cookies();
  68. Vector<Web::Cookie::Cookie> get_all_cookies(URL::URL const& url);
  69. Optional<Web::Cookie::Cookie> get_named_cookie(URL::URL const& url, StringView name);
  70. private:
  71. explicit CookieJar(Optional<PersistedStorage>);
  72. AK_MAKE_NONCOPYABLE(CookieJar);
  73. AK_MAKE_NONMOVABLE(CookieJar);
  74. static Optional<String> canonicalize_domain(const URL::URL& url);
  75. static bool domain_matches(StringView string, StringView domain_string);
  76. static bool path_matches(StringView request_path, StringView cookie_path);
  77. static String default_path(const URL::URL& url);
  78. enum class MatchingCookiesSpecMode {
  79. RFC6265,
  80. WebDriver,
  81. };
  82. void store_cookie(Web::Cookie::ParsedCookie const& parsed_cookie, const URL::URL& url, String canonicalized_domain, Web::Cookie::Source source);
  83. Vector<Web::Cookie::Cookie> get_matching_cookies(const URL::URL& url, StringView canonicalized_domain, Web::Cookie::Source source, MatchingCookiesSpecMode mode = MatchingCookiesSpecMode::RFC6265);
  84. Optional<PersistedStorage> m_persisted_storage;
  85. TransientStorage m_transient_storage;
  86. };
  87. }
  88. template<>
  89. struct AK::Traits<WebView::CookieStorageKey> : public AK::DefaultTraits<WebView::CookieStorageKey> {
  90. static unsigned hash(WebView::CookieStorageKey const& key)
  91. {
  92. unsigned hash = 0;
  93. hash = pair_int_hash(hash, key.name.hash());
  94. hash = pair_int_hash(hash, key.domain.hash());
  95. hash = pair_int_hash(hash, key.path.hash());
  96. return hash;
  97. }
  98. };