CookieJar.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright (c) 2021-2023, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/DeprecatedString.h>
  8. #include <AK/Function.h>
  9. #include <AK/HashMap.h>
  10. #include <AK/Optional.h>
  11. #include <AK/Traits.h>
  12. #include <LibCore/DateTime.h>
  13. #include <LibSQL/Type.h>
  14. #include <LibWeb/Cookie/Cookie.h>
  15. #include <LibWeb/Forward.h>
  16. #include <LibWebView/Forward.h>
  17. namespace WebView {
  18. struct CookieStorageKey {
  19. bool operator==(CookieStorageKey const&) const = default;
  20. DeprecatedString name;
  21. DeprecatedString domain;
  22. DeprecatedString path;
  23. };
  24. class CookieJar {
  25. struct Statements {
  26. SQL::StatementID create_table { 0 };
  27. SQL::StatementID insert_cookie { 0 };
  28. SQL::StatementID update_cookie { 0 };
  29. SQL::StatementID expire_cookie { 0 };
  30. SQL::StatementID select_cookie { 0 };
  31. SQL::StatementID select_all_cookies { 0 };
  32. };
  33. struct PersistedStorage {
  34. Database& database;
  35. Statements statements;
  36. };
  37. using TransientStorage = HashMap<CookieStorageKey, Web::Cookie::Cookie>;
  38. public:
  39. static ErrorOr<CookieJar> create(Database&);
  40. static CookieJar create();
  41. DeprecatedString get_cookie(const URL& url, Web::Cookie::Source source);
  42. void set_cookie(const URL& url, Web::Cookie::ParsedCookie const& parsed_cookie, Web::Cookie::Source source);
  43. void update_cookie(Web::Cookie::Cookie);
  44. void dump_cookies();
  45. Vector<Web::Cookie::Cookie> get_all_cookies();
  46. Vector<Web::Cookie::Cookie> get_all_cookies(URL const& url);
  47. Optional<Web::Cookie::Cookie> get_named_cookie(URL const& url, DeprecatedString const& name);
  48. private:
  49. explicit CookieJar(PersistedStorage);
  50. explicit CookieJar(TransientStorage);
  51. static Optional<DeprecatedString> canonicalize_domain(const URL& url);
  52. static bool domain_matches(DeprecatedString const& string, DeprecatedString const& domain_string);
  53. static bool path_matches(DeprecatedString const& request_path, DeprecatedString const& cookie_path);
  54. static DeprecatedString default_path(const URL& url);
  55. enum class MatchingCookiesSpecMode {
  56. RFC6265,
  57. WebDriver,
  58. };
  59. void store_cookie(Web::Cookie::ParsedCookie const& parsed_cookie, const URL& url, DeprecatedString canonicalized_domain, Web::Cookie::Source source);
  60. Vector<Web::Cookie::Cookie> get_matching_cookies(const URL& url, DeprecatedString const& canonicalized_domain, Web::Cookie::Source source, MatchingCookiesSpecMode mode = MatchingCookiesSpecMode::RFC6265);
  61. void insert_cookie_into_database(Web::Cookie::Cookie const& cookie);
  62. void update_cookie_in_database(Web::Cookie::Cookie const& cookie);
  63. using OnCookieFound = Function<void(Web::Cookie::Cookie&, Web::Cookie::Cookie)>;
  64. using OnCookieNotFound = Function<void(Web::Cookie::Cookie)>;
  65. void select_cookie_from_database(Web::Cookie::Cookie cookie, OnCookieFound on_result, OnCookieNotFound on_complete_without_results);
  66. using OnSelectAllCookiesResult = Function<void(Web::Cookie::Cookie)>;
  67. void select_all_cookies_from_database(OnSelectAllCookiesResult on_result);
  68. void purge_expired_cookies();
  69. Variant<PersistedStorage, TransientStorage> m_storage;
  70. };
  71. }
  72. template<>
  73. struct AK::Traits<WebView::CookieStorageKey> : public AK::DefaultTraits<WebView::CookieStorageKey> {
  74. static unsigned hash(WebView::CookieStorageKey const& key)
  75. {
  76. unsigned hash = 0;
  77. hash = pair_int_hash(hash, string_hash(key.name.characters(), key.name.length()));
  78. hash = pair_int_hash(hash, string_hash(key.domain.characters(), key.domain.length()));
  79. hash = pair_int_hash(hash, string_hash(key.path.characters(), key.path.length()));
  80. return hash;
  81. }
  82. };