cookie_manager.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // Copyright 2017 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef SERVICES_NETWORK_COOKIE_MANAGER_H_
  5. #define SERVICES_NETWORK_COOKIE_MANAGER_H_
  6. #include <memory>
  7. #include <string>
  8. #include <vector>
  9. #include "base/component_export.h"
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/memory/weak_ptr.h"
  12. #include "base/synchronization/lock.h"
  13. #include "components/content_settings/core/common/content_settings.h"
  14. #include "mojo/public/cpp/bindings/receiver_set.h"
  15. #include "mojo/public/cpp/bindings/remote.h"
  16. #include "net/cookies/cookie_change_dispatcher.h"
  17. #include "net/cookies/cookie_deletion_info.h"
  18. #include "services/network/cookie_settings.h"
  19. #include "services/network/public/mojom/cookie_manager.mojom.h"
  20. namespace net {
  21. class CookieStore;
  22. class URLRequestContext;
  23. } // namespace net
  24. class GURL;
  25. namespace network {
  26. class FirstPartySetsAccessDelegate;
  27. class SessionCleanupCookieStore;
  28. // Wrap a cookie store in an implementation of the mojo cookie interface.
  29. class COMPONENT_EXPORT(NETWORK_SERVICE) CookieManager
  30. : public mojom::CookieManager {
  31. public:
  32. // Construct a CookieService that can serve mojo requests for the underlying
  33. // cookie store. |url_request_context->cookie_store()| must outlive this
  34. // object. `*first_party_sets_access_delegate` must outlive
  35. // `url_request_context->cookie_store()`.
  36. CookieManager(
  37. net::URLRequestContext* url_request_context,
  38. FirstPartySetsAccessDelegate* const first_party_sets_access_delegate,
  39. scoped_refptr<SessionCleanupCookieStore> session_cleanup_cookie_store,
  40. mojom::CookieManagerParamsPtr params);
  41. CookieManager(const CookieManager&) = delete;
  42. CookieManager& operator=(const CookieManager&) = delete;
  43. ~CookieManager() override;
  44. const CookieSettings& cookie_settings() const { return cookie_settings_; }
  45. // Bind a cookie receiver to this object. Mojo messages
  46. // coming through the associated pipe will be served by this object.
  47. void AddReceiver(mojo::PendingReceiver<mojom::CookieManager> receiver);
  48. // TODO(rdsmith): Add a verion of AddRequest that does renderer-appropriate
  49. // security checks on bindings coming through that interface.
  50. // mojom::CookieManager
  51. void GetAllCookies(GetAllCookiesCallback callback) override;
  52. void GetAllCookiesWithAccessSemantics(
  53. GetAllCookiesWithAccessSemanticsCallback callback) override;
  54. void GetCookieList(
  55. const GURL& url,
  56. const net::CookieOptions& cookie_options,
  57. const net::CookiePartitionKeyCollection& cookie_partition_key_collection,
  58. GetCookieListCallback callback) override;
  59. void SetCanonicalCookie(const net::CanonicalCookie& cookie,
  60. const GURL& source_url,
  61. const net::CookieOptions& cookie_options,
  62. SetCanonicalCookieCallback callback) override;
  63. void DeleteCanonicalCookie(const net::CanonicalCookie& cookie,
  64. DeleteCanonicalCookieCallback callback) override;
  65. void SetContentSettings(const ContentSettingsForOneType& settings) override;
  66. void DeleteCookies(mojom::CookieDeletionFilterPtr filter,
  67. DeleteCookiesCallback callback) override;
  68. void DeleteSessionOnlyCookies(
  69. DeleteSessionOnlyCookiesCallback callback) override;
  70. void AddCookieChangeListener(
  71. const GURL& url,
  72. const absl::optional<std::string>& name,
  73. mojo::PendingRemote<mojom::CookieChangeListener> listener) override;
  74. void AddGlobalChangeListener(
  75. mojo::PendingRemote<mojom::CookieChangeListener> listener) override;
  76. void CloneInterface(
  77. mojo::PendingReceiver<mojom::CookieManager> new_interface) override;
  78. size_t GetClientsBoundForTesting() const { return receivers_.size(); }
  79. size_t GetListenersRegisteredForTesting() const {
  80. return listener_registrations_.size();
  81. }
  82. void FlushCookieStore(FlushCookieStoreCallback callback) override;
  83. void AllowFileSchemeCookies(bool allow,
  84. AllowFileSchemeCookiesCallback callback) override;
  85. void SetForceKeepSessionState() override;
  86. void BlockThirdPartyCookies(bool block) override;
  87. void SetContentSettingsForLegacyCookieAccess(
  88. const ContentSettingsForOneType& settings) override;
  89. void SetStorageAccessGrantSettings(
  90. const ContentSettingsForOneType& settings,
  91. SetStorageAccessGrantSettingsCallback callback) override;
  92. // Configures |out| based on |params|. (This doesn't honor
  93. // allow_file_scheme_cookies, which affects the cookie store rather than the
  94. // settings).
  95. static void ConfigureCookieSettings(
  96. const network::mojom::CookieManagerParams& params,
  97. CookieSettings* out);
  98. // Causes the next call to GetCookieList to crash the process.
  99. static void CrashOnGetCookieList();
  100. // Will convert a site's partitioned cookies into unpartitioned cookies. This
  101. // may result in multiple cookies which have the same (partition_key, name,
  102. // host_key, path), which violates the database's unique constraint. The
  103. // algorithm we use to coalesce the cookies into a single unpartitioned cookie
  104. // is the following:
  105. //
  106. // 1. If one of the cookies has no partition key (i.e. it is unpartitioned)
  107. // choose this cookie.
  108. //
  109. // 2. Choose the partitioned cookie with the most recent last_access_time.
  110. //
  111. // This function is a no-op when PartitionedCookies are disabled or
  112. // PartitionedCookiesBypassOriginTrial is enabled.
  113. // TODO(crbug.com/1296161): Delete this when the partitioned cookies Origin
  114. // Trial ends.
  115. void ConvertPartitionedCookiesToUnpartitioned(const GURL& url) override;
  116. private:
  117. // State associated with a CookieChangeListener.
  118. struct ListenerRegistration {
  119. ListenerRegistration();
  120. ListenerRegistration(const ListenerRegistration&) = delete;
  121. ListenerRegistration& operator=(const ListenerRegistration&) = delete;
  122. ~ListenerRegistration();
  123. // Translates a CookieStore change callback to a CookieChangeListener call.
  124. void DispatchCookieStoreChange(const net::CookieChangeInfo& change);
  125. // Owns the callback registration in the store.
  126. std::unique_ptr<net::CookieChangeSubscription> subscription;
  127. // The observer receiving change notifications.
  128. mojo::Remote<mojom::CookieChangeListener> listener;
  129. };
  130. // Handles connection errors on change listener pipes.
  131. void RemoveChangeListener(ListenerRegistration* registration);
  132. // Called after getting the First-Party-Set-aware partition key when setting a
  133. // cookie. `adjusted_cookie_partition_key` holds a bool representing whether
  134. // the cookie needs to be recreated (due to using the wrong partition key);
  135. // and the partition key to use when recreating the cookie (for any reason).
  136. void OnGotFirstPartySetPartitionKeyForSet(
  137. const GURL& source_url,
  138. const net::CookieOptions& cookie_options,
  139. std::unique_ptr<net::CanonicalCookie> cookie,
  140. SetCanonicalCookieCallback callback,
  141. std::pair<bool, absl::optional<net::CookiePartitionKey>>
  142. adjusted_cookie_partition_key);
  143. // Called after getting the First-Party-Set-aware partition key when deleting
  144. // a cookie.
  145. void OnGotFirstPartySetPartitionKeyForDelete(
  146. std::unique_ptr<net::CanonicalCookie> cookie,
  147. DeleteCanonicalCookieCallback callback,
  148. bool cookie_partition_keys_match);
  149. void OnGotCookiePartitionKeyCollection(
  150. const GURL& url,
  151. const net::CookieOptions& cookie_options,
  152. GetCookieListCallback callback,
  153. net::CookiePartitionKeyCollection cookie_partition_key_collection);
  154. const raw_ptr<net::CookieStore> cookie_store_;
  155. scoped_refptr<SessionCleanupCookieStore> session_cleanup_cookie_store_;
  156. mojo::ReceiverSet<mojom::CookieManager> receivers_;
  157. std::vector<std::unique_ptr<ListenerRegistration>> listener_registrations_;
  158. // Note: RestrictedCookieManager and CookieAccessDelegate store pointers to
  159. // |cookie_settings_|.
  160. CookieSettings cookie_settings_;
  161. base::WeakPtrFactory<CookieManager> weak_factory_{this};
  162. };
  163. COMPONENT_EXPORT(NETWORK_SERVICE)
  164. net::CookieDeletionInfo DeletionFilterToInfo(
  165. mojom::CookieDeletionFilterPtr filter);
  166. } // namespace network
  167. #endif // SERVICES_NETWORK_COOKIE_MANAGER_H_