first_party_sets_manager.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // Copyright 2020 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_FIRST_PARTY_SETS_FIRST_PARTY_SETS_MANAGER_H_
  5. #define SERVICES_NETWORK_FIRST_PARTY_SETS_FIRST_PARTY_SETS_MANAGER_H_
  6. #include <map>
  7. #include <memory>
  8. #include <set>
  9. #include "base/callback.h"
  10. #include "base/containers/circular_deque.h"
  11. #include "base/containers/flat_map.h"
  12. #include "base/containers/flat_set.h"
  13. #include "base/sequence_checker.h"
  14. #include "base/thread_annotations.h"
  15. #include "base/time/time.h"
  16. #include "base/timer/elapsed_timer.h"
  17. #include "net/base/schemeful_site.h"
  18. #include "net/cookies/first_party_set_entry.h"
  19. #include "net/cookies/first_party_set_metadata.h"
  20. #include "net/cookies/first_party_sets_context_config.h"
  21. #include "services/network/public/mojom/first_party_sets.mojom.h"
  22. #include "third_party/abseil-cpp/absl/types/optional.h"
  23. namespace network {
  24. // Class FirstPartySetsManager is a pseudo-singleton owned by NetworkService; it
  25. // answers queries about First-Party Sets after they've been loaded.
  26. class FirstPartySetsManager {
  27. public:
  28. using OwnersResult =
  29. base::flat_map<net::SchemefulSite, net::FirstPartySetEntry>;
  30. using FlattenedSets =
  31. base::flat_map<net::SchemefulSite, net::FirstPartySetEntry>;
  32. explicit FirstPartySetsManager(bool enabled);
  33. ~FirstPartySetsManager();
  34. FirstPartySetsManager(const FirstPartySetsManager&) = delete;
  35. FirstPartySetsManager& operator=(const FirstPartySetsManager&) = delete;
  36. bool is_enabled() const {
  37. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  38. return enabled_;
  39. }
  40. // Computes the First-Party Set metadata related to the given request context.
  41. //
  42. // This may return a result synchronously, or asynchronously invoke `callback`
  43. // with the result. The callback will be invoked iff the return value is
  44. // nullopt; i.e. a result will be provided via return value or callback, but
  45. // not both, and not neither.
  46. [[nodiscard]] absl::optional<net::FirstPartySetMetadata> ComputeMetadata(
  47. const net::SchemefulSite& site,
  48. const net::SchemefulSite* top_frame_site,
  49. const std::set<net::SchemefulSite>& party_context,
  50. const net::FirstPartySetsContextConfig& fps_context_config,
  51. base::OnceCallback<void(net::FirstPartySetMetadata)> callback);
  52. // Stores the First-Party Sets data.
  53. //
  54. // Only the first call to SetCompleteSets can have any effect; subsequent
  55. // invocations are ignored.
  56. void SetCompleteSets(mojom::PublicFirstPartySetsPtr public_sets);
  57. // Sets the enabled_ attribute for testing.
  58. void SetEnabledForTesting(bool enabled);
  59. // Returns the mapping of sites to entries for the given input sites (if an
  60. // entry exists).
  61. //
  62. // When FPS is disabled, returns an empty map.
  63. // When FPS is enabled, this maps each input site to its entry (if one
  64. // exists), and returns the resulting mapping. If a site isn't in a
  65. // non-trivial First-Party Set, it is not added to the output map.
  66. //
  67. // This may return a result synchronously, or asynchronously invoke `callback`
  68. // with the result. The callback will be invoked iff the return value is
  69. // nullopt; i.e. a result will be provided via return value or callback, but
  70. // not both, and not neither.
  71. [[nodiscard]] absl::optional<OwnersResult> FindOwners(
  72. const base::flat_set<net::SchemefulSite>& sites,
  73. const net::FirstPartySetsContextConfig& fps_context_config,
  74. base::OnceCallback<void(OwnersResult)> callback);
  75. private:
  76. // Same as `ComputeMetadata`, but plumbs the result into the callback. Must
  77. // only be called once the instance is fully initialized.
  78. void ComputeMetadataAndInvoke(
  79. const net::SchemefulSite& site,
  80. const absl::optional<net::SchemefulSite> top_frame_site,
  81. const std::set<net::SchemefulSite>& party_context,
  82. const net::FirstPartySetsContextConfig& fps_context_config,
  83. base::OnceCallback<void(net::FirstPartySetMetadata)> callback,
  84. base::ElapsedTimer timer) const;
  85. // Synchronous version of `ComputeMetadata`, to be run only once the instance
  86. // is fully initialized.
  87. net::FirstPartySetMetadata ComputeMetadataInternal(
  88. const net::SchemefulSite& site,
  89. const net::SchemefulSite* top_frame_site,
  90. const std::set<net::SchemefulSite>& party_context,
  91. const net::FirstPartySetsContextConfig& fps_context_config) const;
  92. // Returns whether the `site` is same-party with the `party_context`, and
  93. // `top_frame_site` (if it is not nullptr). That is, is the `site`'s owner the
  94. // same as the owners of every member of `party_context` and of
  95. // `top_frame_site`? Note: if `site` is not a member of a First-Party Set
  96. // (with more than one member), then this returns false. If `top_frame_site`
  97. // is nullptr, then it is ignored.
  98. bool IsContextSamePartyWithSite(
  99. const net::SchemefulSite& site,
  100. const net::SchemefulSite* top_frame_site,
  101. const std::set<net::SchemefulSite>& party_context,
  102. const net::FirstPartySetsContextConfig& fps_context_config) const;
  103. // Returns `site`'s entry, or `nullopt` if `site` has no entry.
  104. // `fps_context_config` is the configuration to be used in this context.
  105. //
  106. // This is synchronous, and must not be called until the instance is fully
  107. // initialized.
  108. absl::optional<net::FirstPartySetEntry> FindEntry(
  109. const net::SchemefulSite& site,
  110. const net::FirstPartySetsContextConfig& fps_context_config) const;
  111. // Same as `FindOwners`, but plumbs the result into the callback. Must only be
  112. // called once the instance is fully initialized.
  113. void FindOwnersAndInvoke(
  114. const base::flat_set<net::SchemefulSite>& sites,
  115. const net::FirstPartySetsContextConfig& fps_context_config,
  116. base::OnceCallback<void(OwnersResult)> callback,
  117. base::ElapsedTimer timer) const;
  118. // Synchronous version of `FindOwners`, to be run only once the instance is
  119. // initialized.
  120. OwnersResult FindOwnersInternal(
  121. const base::flat_set<net::SchemefulSite>& sites,
  122. const net::FirstPartySetsContextConfig& fps_context_config) const;
  123. // Enqueues a query to be answered once the instance is fully initialized.
  124. void EnqueuePendingQuery(base::OnceClosure run_query);
  125. // Runs all pending queries. Must not be called until the instance is fully
  126. // initialized.
  127. void InvokePendingQueries();
  128. // Represents the mapping of site -> site, where keys are members of sets, and
  129. // values are owners of the sets. Owners are explicitly represented as members
  130. // of the set.
  131. //
  132. // Optional because it is unset until all of the required inputs have been
  133. // received.
  134. absl::optional<FlattenedSets> sets_ GUARDED_BY_CONTEXT(sequence_checker_);
  135. // The site aliases. Used to normalize a given SchemefulSite into its
  136. // canonical representative, before looking it up in `sets_`.
  137. base::flat_map<net::SchemefulSite, net::SchemefulSite> aliases_
  138. GUARDED_BY_CONTEXT(sequence_checker_);
  139. bool enabled_ GUARDED_BY_CONTEXT(sequence_checker_) = false;
  140. // The queue of queries that are waiting for the instance to be initialized.
  141. std::unique_ptr<base::circular_deque<base::OnceClosure>> pending_queries_
  142. GUARDED_BY_CONTEXT(sequence_checker_);
  143. // Timer starting when the first async query was enqueued, if any. Used for
  144. // metrics.
  145. absl::optional<base::ElapsedTimer> first_async_query_timer_
  146. GUARDED_BY_CONTEXT(sequence_checker_);
  147. // Timer starting when the instance is constructed. Used for metrics.
  148. base::ElapsedTimer construction_timer_ GUARDED_BY_CONTEXT(sequence_checker_);
  149. SEQUENCE_CHECKER(sequence_checker_);
  150. base::WeakPtrFactory<FirstPartySetsManager> weak_factory_{this};
  151. };
  152. } // namespace network
  153. #endif // SERVICES_NETWORK_FIRST_PARTY_SETS_FIRST_PARTY_SETS_MANAGER_H_