CookieJar.h 3.6 KB

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