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

content / browser / interest_group / bidding_and_auction_server_key_fetcher.h [blame]

// 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/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/types/expected.h"
#include "content/common/content_export.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.
  uint8_t id;       // key id corresponding to this key.
};

// 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>)>;

  // `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
  // kFledgePrefetchBandAKeys and kFledgeBiddingAndAuctionServer are 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(const std::optional<url::Origin>& maybe_coordinator,
                     BiddingAndAuctionServerKeyFetcherCallback callback);

 private:
  struct PerCoordinatorFetcherState {
    PerCoordinatorFetcherState();
    ~PerCoordinatorFetcherState();

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

    GURL key_url;

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

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

    // 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& coordinator,
                 PerCoordinatorFetcherState& state,
                 BiddingAndAuctionServerKeyFetcherCallback callback);

  void OnFetchKeysFromDatabaseComplete(
      const url::Origin& coordinator,
      std::pair<base::Time, std::vector<BiddingAndAuctionServerKey>> 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);

  void CacheKeysAndRunAllCallbacks(
      const url::Origin& coordinator,
      const std::vector<BiddingAndAuctionServerKey>& keys,
      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_