CookieJar.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 <LibSQL/Type.h>
  15. #include <LibWeb/Cookie/Cookie.h>
  16. #include <LibWeb/Forward.h>
  17. #include <LibWebView/Forward.h>
  18. namespace WebView {
  19. struct CookieStorageKey {
  20. bool operator==(CookieStorageKey const&) const = default;
  21. String name;
  22. String domain;
  23. String path;
  24. };
  25. class CookieJar {
  26. struct Statements {
  27. SQL::StatementID create_table { 0 };
  28. SQL::StatementID insert_cookie { 0 };
  29. SQL::StatementID update_cookie { 0 };
  30. SQL::StatementID expire_cookie { 0 };
  31. SQL::StatementID select_cookie { 0 };
  32. SQL::StatementID select_all_cookies { 0 };
  33. };
  34. struct PersistedStorage {
  35. Database& database;
  36. Statements statements;
  37. };
  38. using TransientStorage = HashMap<CookieStorageKey, Web::Cookie::Cookie>;
  39. public:
  40. static ErrorOr<CookieJar> create(Database&);
  41. static CookieJar create();
  42. String get_cookie(const URL& url, Web::Cookie::Source source);
  43. void set_cookie(const URL& url, Web::Cookie::ParsedCookie const& parsed_cookie, Web::Cookie::Source source);
  44. void update_cookie(Web::Cookie::Cookie);
  45. void dump_cookies();
  46. Vector<Web::Cookie::Cookie> get_all_cookies();
  47. Vector<Web::Cookie::Cookie> get_all_cookies(URL const& url);
  48. Optional<Web::Cookie::Cookie> get_named_cookie(URL const& url, StringView name);
  49. private:
  50. explicit CookieJar(PersistedStorage);
  51. explicit CookieJar(TransientStorage);
  52. static Optional<String> canonicalize_domain(const URL& url);
  53. static bool domain_matches(StringView string, StringView domain_string);
  54. static bool path_matches(StringView request_path, StringView cookie_path);
  55. static String default_path(const URL& url);
  56. enum class MatchingCookiesSpecMode {
  57. RFC6265,
  58. WebDriver,
  59. };
  60. void store_cookie(Web::Cookie::ParsedCookie const& parsed_cookie, const URL& url, String canonicalized_domain, Web::Cookie::Source source);
  61. Vector<Web::Cookie::Cookie> get_matching_cookies(const URL& url, StringView canonicalized_domain, Web::Cookie::Source source, MatchingCookiesSpecMode mode = MatchingCookiesSpecMode::RFC6265);
  62. void insert_cookie_into_database(Web::Cookie::Cookie const& cookie);
  63. void update_cookie_in_database(Web::Cookie::Cookie const& cookie);
  64. using OnCookieFound = Function<void(Web::Cookie::Cookie&, Web::Cookie::Cookie)>;
  65. using OnCookieNotFound = Function<void(Web::Cookie::Cookie)>;
  66. void select_cookie_from_database(Web::Cookie::Cookie cookie, OnCookieFound on_result, OnCookieNotFound on_complete_without_results);
  67. using OnSelectAllCookiesResult = Function<void(Web::Cookie::Cookie)>;
  68. void select_all_cookies_from_database(OnSelectAllCookiesResult on_result);
  69. void purge_expired_cookies();
  70. Variant<PersistedStorage, TransientStorage> m_storage;
  71. };
  72. }
  73. template<>
  74. struct AK::Traits<WebView::CookieStorageKey> : public AK::DefaultTraits<WebView::CookieStorageKey> {
  75. static unsigned hash(WebView::CookieStorageKey const& key)
  76. {
  77. unsigned hash = 0;
  78. hash = pair_int_hash(hash, key.name.hash());
  79. hash = pair_int_hash(hash, key.domain.hash());
  80. hash = pair_int_hash(hash, key.path.hash());
  81. return hash;
  82. }
  83. };