File: bidding_and_auction_server_key_fetcher.h

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (247 lines) | stat: -rw-r--r-- 10,204 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_BROWSER_INTEREST_GROUP_BIDDING_AND_AUCTION_SERVER_KEY_FETCHER_H_
#define CONTENT_BROWSER_INTEREST_GROUP_BIDDING_AND_AUCTION_SERVER_KEY_FETCHER_H_

#include <optional>
#include <string>
#include <vector>

#include "base/containers/circular_deque.h"
#include "base/containers/enum_set.h"
#include "base/containers/flat_map.h"
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/types/expected.h"
#include "content/common/content_export.h"
#include "content/public/browser/interest_group_manager.h"
#include "services/data_decoder/public/cpp/data_decoder.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "url/gurl.h"
#include "url/origin.h"

namespace network {
class SimpleURLLoader;
}  // namespace network

namespace content {
class InterestGroupManagerImpl;

inline constexpr char kDefaultBiddingAndAuctionGCPCoordinatorOrigin[] =
    "https://publickeyservice.gcp.privacysandboxservices.com";
inline constexpr char kBiddingAndAuctionGCPCoordinatorOrigin[] =
    "https://publickeyservice.pa.gcp.privacysandboxservices.com";
inline constexpr char kBiddingAndAuctionGCPCoordinatorKeyURL[] =
    "https://publickeyservice.pa.gcp.privacysandboxservices.com/.well-known/"
    "protected-auction/v1/public-keys";
inline constexpr char kBiddingAndAuctionAWSCoordinatorOrigin[] =
    "https://publickeyservice.pa.aws.privacysandboxservices.com";
inline constexpr char kBiddingAndAuctionAWSCoordinatorKeyURL[] =
    "https://publickeyservice.pa.aws.privacysandboxservices.com/.well-known/"
    "protected-auction/v1/public-keys";

struct BiddingAndAuctionServerKey {
  std::string key;  // bytes containing the key.
  std::string id;   // key id corresponding to this key.
};

// This class abstracts the set of keys. This makes code accessing the keys more
// generic to ease the transition as we consider alternative key scopes.
// See https://github.com/WICG/turtledove/issues/1334 for details.
class CONTENT_EXPORT BiddingAndAuctionKeySet {
 public:
  explicit BiddingAndAuctionKeySet(
      std::vector<BiddingAndAuctionServerKey> keys);
  explicit BiddingAndAuctionKeySet(
      base::flat_map<url::Origin, std::vector<BiddingAndAuctionServerKey>>
          origin_scoped_keys);
  ~BiddingAndAuctionKeySet();

  BiddingAndAuctionKeySet(BiddingAndAuctionKeySet&& keyset);
  BiddingAndAuctionKeySet& operator=(BiddingAndAuctionKeySet&& keyset);

  // Returns true if we have any keys in this Keyset.
  bool HasKeys() const;
  uint8_t SchemaVersion() const;

  // Returns a random key from the set of keys for this coordinator. If keys are
  // scoped by origin, the provided `scoped_origin` is used to select the the
  // keyset to select from.
  std::optional<BiddingAndAuctionServerKey> GetRandomKey(
      const url::Origin& scoped_origin) const;

  // Convert Keyset to binary Protobuf for storage.
  std::string AsBinaryProto() const;
  // Create a Keyset from binary Protobuf.
  static std::optional<BiddingAndAuctionKeySet> FromBinaryProto(
      std::string key_blob);

 private:
  std::vector<BiddingAndAuctionServerKey> keys_;
  base::flat_map<url::Origin, std::vector<BiddingAndAuctionServerKey>>
      origin_scoped_keys_;
};

// BiddingAndAuctionServerKeyFetcher manages fetching and caching of the public
// keys for Bidding and Auction Server endpoints from each of the designated
// Coordinators with the provided `loader_factory`. Values are cached both in
// memory and in the database.
class CONTENT_EXPORT BiddingAndAuctionServerKeyFetcher {
 public:
  using BiddingAndAuctionServerKeyFetcherCallback = base::OnceCallback<void(
      base::expected<BiddingAndAuctionServerKey, std::string>)>;
  using TrustedServerAPIType = InterestGroupManager::TrustedServerAPIType;

  // `manager` should be the InterestGroupManagerImpl that owns this
  // BiddingAndAuctionServerKeyFetcher.
  BiddingAndAuctionServerKeyFetcher(
      InterestGroupManagerImpl* manager,
      scoped_refptr<network::SharedURLLoaderFactory> loader_factory);
  ~BiddingAndAuctionServerKeyFetcher();
  // no copy
  BiddingAndAuctionServerKeyFetcher(const BiddingAndAuctionServerKeyFetcher&) =
      delete;
  BiddingAndAuctionServerKeyFetcher& operator=(
      const BiddingAndAuctionServerKeyFetcher&) = delete;

  // Fetch keys for all coordinators in kFledgeBiddingAndAuctionKeyConfig if
  // kFledgeBiddingAndAuctionServer is enabled and if the keys haven't been
  // fetched yet.
  void MaybePrefetchKeys();

  // GetOrFetchKey provides a key in the callback if necessary. If the key is
  // immediately available then the callback may be called synchronously.
  void GetOrFetchKey(TrustedServerAPIType api,
                     const url::Origin& scope_origin,
                     const std::optional<url::Origin>& maybe_coordinator,
                     BiddingAndAuctionServerKeyFetcherCallback callback);

  // Adds a non-database-persistent testing override to key configuration for
  // given `coordinator`. Invokes the callback with an error string if there is
  // a problem, such as if a configuration for a given coordinator already
  // exists. `callback` is called with nullopt on success. Either success or
  // failure may be synchronous or asynchronous.
  using DebugOverrideCallback =
      base::OnceCallback<void(std::optional<std::string>)>;
  void AddKeysDebugOverride(TrustedServerAPIType api,
                            const url::Origin& coordinator,
                            std::string serialized_keys,
                            DebugOverrideCallback callback);

 private:
  struct CallbackQueueItem {
    CallbackQueueItem(BiddingAndAuctionServerKeyFetcherCallback callback,
                      url::Origin scope_origin);
    ~CallbackQueueItem();

    CallbackQueueItem(CallbackQueueItem&& item);
    CallbackQueueItem& operator=(CallbackQueueItem&& item);

    BiddingAndAuctionServerKeyFetcherCallback callback;
    url::Origin scope_origin;
  };

  struct PerCoordinatorFetcherState {
    PerCoordinatorFetcherState();
    ~PerCoordinatorFetcherState();

    PerCoordinatorFetcherState(PerCoordinatorFetcherState&& state);
    PerCoordinatorFetcherState& operator=(PerCoordinatorFetcherState&& state);

    GURL key_url;
    uint8_t version;
    base::EnumSet<TrustedServerAPIType,
                  TrustedServerAPIType::kInvalid,
                  TrustedServerAPIType::kMaxValue>
        apis;

    // If this is set, this is a temporary configuration applied via
    // SetBiddingAndAuctionServerKeysDebugOverride(), and so should not
    // use the DB, and doesn't expire.
    bool debug_override = false;

    // Callback for debug override of config getting parsed. Unlike the
    // normal callbacks in `queue` these are not scoped to origin.
    DebugOverrideCallback debug_override_callback;

    // queue_ contains callbacks waiting for a key to be fetched over the
    // network.
    base::circular_deque<CallbackQueueItem> queue;

    // keys_ contains a list of keys received from the server (if any).
    std::optional<BiddingAndAuctionKeySet> keyset;

    // expiration_ contains the expiration time for any keys that are cached by
    // this object.
    base::Time expiration = base::Time::Min();

    // The time the key fetch starts.
    base::TimeTicks fetch_start;
    // The time the key fetch from the network starts. This time may be after
    // unsuccessfully trying to load the key from the database.
    base::TimeTicks network_fetch_start;

    // loader_ contains the SimpleURLLoader being used to fetch the keys.
    std::unique_ptr<network::SimpleURLLoader> loader;
  };

  // Fetch keys for a particular coordinator, first checking if the key is
  // in the database.
  void FetchKeys(const url::Origin& scope_origin,
                 const url::Origin& coordinator,
                 PerCoordinatorFetcherState& state,
                 BiddingAndAuctionServerKeyFetcherCallback callback);

  void OnFetchKeysFromDatabaseComplete(const url::Origin& coordinator,
                                       std::pair<base::Time, std::string> keys);

  void FetchKeysFromNetwork(const url::Origin& coordinator);

  // Called when the JSON blob containing the keys have been successfully
  // fetched over the network.
  void OnFetchKeysFromNetworkComplete(url::Origin coordinator,
                                      std::unique_ptr<std::string> response);

  // Called when the JSON blob containing the keys has be parsed into
  // base::Values. Uses the parsed result to add keys to the cache and calls
  // queued callbacks.
  void OnParsedKeys(url::Origin coordinator,
                    data_decoder::DataDecoder::ValueOrError result);

  // Called when the JSON blob containing the keys has be parsed into
  // base::Values for v2 keys. Uses the parsed result to add keys to the cache
  // and calls queued callbacks.
  void OnParsedKeysV2(url::Origin coordinator,
                      data_decoder::DataDecoder::ValueOrError result);

  void CacheKeysAndRunAllCallbacks(const url::Origin& coordinator,
                                   BiddingAndAuctionKeySet keyset,
                                   base::Time expiration);

  void FailAllCallbacks(url::Origin coordinator);

  bool did_prefetch_keys_ = false;

  // May be referenced by the fetcher_state_map, so the loader_factory_ should
  // be destructed last.
  const scoped_refptr<network::SharedURLLoaderFactory> loader_factory_;

  base::flat_map<url::Origin, PerCoordinatorFetcherState> fetcher_state_map_;

  // An unowned pointer to the InterestGroupManagerImpl that owns this
  // BiddingAndAuctionServerKeyFetcher. Used as an intermediary to talk to the
  // database.
  raw_ptr<InterestGroupManagerImpl> manager_;

  const url::Origin default_gcp_coordinator_ =
      url::Origin::Create(GURL(kDefaultBiddingAndAuctionGCPCoordinatorOrigin));

  base::WeakPtrFactory<BiddingAndAuctionServerKeyFetcher> weak_ptr_factory_{
      this};
};

}  // namespace content

#endif  // CONTENT_BROWSER_INTEREST_GROUP_BIDDING_AND_AUCTION_SERVER_KEY_FETCHER_H_