File: bidding_and_auction_server_key_fetcher.cc

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 (734 lines) | stat: -rw-r--r-- 26,660 bytes parent folder | download | duplicates (3)
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
// 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.

#include "content/browser/interest_group/bidding_and_auction_server_key_fetcher.h"

#include "base/base64.h"
#include "base/functional/callback_helpers.h"
#include "base/json/json_reader.h"
#include "base/metrics/histogram_functions.h"
#include "base/rand_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "content/browser/interest_group/interest_group_features.h"
#include "content/browser/interest_group/interest_group_manager_impl.h"
#include "content/browser/interest_group/interest_group_storage.pb.h"
#include "content/browser/interest_group/interest_group_storage_metric_types.h"
#include "net/base/isolation_info.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "services/data_decoder/public/cpp/data_decoder.h"
#include "services/network/public/cpp/resource_request.h"
#include "services/network/public/cpp/simple_url_loader.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/protobuf/src/google/protobuf/repeated_ptr_field.h"
#include "url/origin.h"

namespace content {

namespace {
constexpr base::TimeDelta kKeyRequestInterval = base::Days(7);
constexpr base::TimeDelta kRequestTimeout = base::Seconds(5);
const size_t kMaxBodySize = 2048;

constexpr net::NetworkTrafficAnnotationTag
    kBiddingAndAuctionServerKeyFetchTrafficAnnotation =
        net::DefineNetworkTrafficAnnotation(
            "bidding_and_auction_server_key_fetch",
            R"(
    semantics {
      sender: "Chrome Bidding and Auction Server Key Fetch"
      last_reviewed: "2023-06-05"
      description:
        "Request to the Bidding and Auction Server keystore to fetch the "
        "public key which will be used to encrypt the request payload sent "
        "to the trusted auction server."
      trigger:
        "Start of a Protected Audience Bidding and Server auction"
      data:
        "No data is sent with this request."
      user_data {
        type: NONE
      }
      destination: GOOGLE_OWNED_SERVICE
      internal {
        contacts {
          email: "privacy-sandbox-dev@chromium.org"
        }
      }
    }
    policy {
      cookies_allowed: NO
      setting:
        "Disable the Protected Audiences API."
      chrome_policy {
      }
    }
    comments:
      ""
    )");

const struct {
  const char* origin;
  const char* key_url;
} kDefaultKeys[] = {
    {kDefaultBiddingAndAuctionGCPCoordinatorOrigin,
     kBiddingAndAuctionGCPCoordinatorKeyURL},
    {kBiddingAndAuctionGCPCoordinatorOrigin,
     kBiddingAndAuctionGCPCoordinatorKeyURL},
    {kBiddingAndAuctionAWSCoordinatorOrigin,
     kBiddingAndAuctionAWSCoordinatorKeyURL},
};

std::vector<BiddingAndAuctionServerKey> ParseKeysList(
    const base::Value::List* keys_list) {
  std::vector<BiddingAndAuctionServerKey> keys;
  for (const auto& entry : *keys_list) {
    BiddingAndAuctionServerKey key;
    const base::Value::Dict* key_dict = entry.GetIfDict();
    if (!key_dict) {
      continue;
    }
    const std::string* key_value = key_dict->FindString("key");
    if (!key_value) {
      continue;
    }
    if (!base::Base64Decode(*key_value, &key.key)) {
      continue;
    }
    const std::string* id_value = key_dict->FindString("id");
    // Previous versions depend on the first two characters being hex digits.
    if (!id_value || id_value->size() < 2 ||
        !base::IsHexDigit((*id_value)[0]) ||
        !base::IsHexDigit((*id_value)[1])) {
      continue;
    }
    key.id = *id_value;
    keys.push_back(std::move(key));
  }
  return keys;
}

std::vector<BiddingAndAuctionServerKey> KeysFromProto(
    google::protobuf::RepeatedPtrField<
        BiddingAndAuctionServerKeysProtos_BiddingAndAuctionServerKeyProto>&
        keys_proto) {
  std::vector<BiddingAndAuctionServerKey> keys;
  keys.reserve(keys_proto.size());
  for (auto& key_proto : keys_proto) {
    if (key_proto.id_str().empty()) {
      std::string id_str;
      base::AppendHexEncodedByte(key_proto.id(), id_str);
      keys.emplace_back(std::move(*key_proto.mutable_key()), std::move(id_str));
    } else {
      keys.emplace_back(std::move(*key_proto.mutable_key()),
                        std::move(*key_proto.mutable_id_str()));
    }
  }
  return keys;
}

void PopulateProtoFromKeys(
    const std::vector<BiddingAndAuctionServerKey>& keys,
    google::protobuf::RepeatedPtrField<
        BiddingAndAuctionServerKeysProtos_BiddingAndAuctionServerKeyProto>&
        proto_keys_out) {
  for (const auto& key : keys) {
    BiddingAndAuctionServerKeysProtos_BiddingAndAuctionServerKeyProto*
        key_proto = proto_keys_out.Add();
    uint32_t key_id = 0;
    bool success =
        base::HexStringToUInt(std::string_view(key.id).substr(0, 2), &key_id);
    DCHECK(success);
    key_proto->set_key(key.key);
    key_proto->set_id(key_id);
    key_proto->set_id_str(key.id);
  }
}

}  // namespace

BiddingAndAuctionKeySet::BiddingAndAuctionKeySet(
    std::vector<BiddingAndAuctionServerKey> keys)
    : keys_(std::move(keys)) {}
BiddingAndAuctionKeySet::BiddingAndAuctionKeySet(
    base::flat_map<url::Origin, std::vector<BiddingAndAuctionServerKey>>
        origin_scoped_keys)
    : origin_scoped_keys_(std::move(origin_scoped_keys)) {}
BiddingAndAuctionKeySet::~BiddingAndAuctionKeySet() = default;

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

bool BiddingAndAuctionKeySet::HasKeys() const {
  if (!keys_.empty() || !origin_scoped_keys_.empty()) {
    return true;
  }
  return false;
}

uint8_t BiddingAndAuctionKeySet::SchemaVersion() const {
  if (!origin_scoped_keys_.empty()) {
    return 2;
  }
  if (!keys_.empty()) {
    return 1;
  }
  return 0;
}

std::optional<BiddingAndAuctionServerKey> BiddingAndAuctionKeySet::GetRandomKey(
    const url::Origin& scoped_origin) const {
  if (!origin_scoped_keys_.empty()) {
    auto it = origin_scoped_keys_.find(scoped_origin);
    if (it == origin_scoped_keys_.end() || it->second.empty()) {
      return std::nullopt;
    }
    return it->second[base::RandInt(0, it->second.size() - 1)];
  } else if (!keys_.empty()) {
    return keys_[base::RandInt(0, keys_.size() - 1)];
  }
  return std::nullopt;
}

std::string BiddingAndAuctionKeySet::AsBinaryProto() const {
  BiddingAndAuctionServerKeysProtos keys_protos;
  PopulateProtoFromKeys(keys_, *keys_protos.mutable_keys());
  for (const auto& [origin, keys] : origin_scoped_keys_) {
    BiddingAndAuctionServerKeysProtos_BiddingAndAuctionServerKeyProtos
        key_protos;
    PopulateProtoFromKeys(keys, *key_protos.mutable_keys());
    keys_protos.mutable_origin_scoped_keys()->insert(
        {origin.Serialize(), std::move(key_protos)});
  }
  std::string key_protos_str;
  if (!keys_protos.SerializeToString(&key_protos_str)) {
    base::UmaHistogramEnumeration(
        "Storage.InterestGroup.ProtoSerializationResult."
        "BiddingAndAuctionServerKeyProtos",
        InterestGroupStorageProtoSerializationResult::kFailed);
    return "";
  }
  base::UmaHistogramEnumeration(
      "Storage.InterestGroup.ProtoSerializationResult."
      "BiddingAndAuctionServerKeyProtos",
      InterestGroupStorageProtoSerializationResult::kSucceeded);
  return key_protos_str;
}

/*static*/ std::optional<BiddingAndAuctionKeySet>
BiddingAndAuctionKeySet::FromBinaryProto(std::string key_blob) {
  BiddingAndAuctionServerKeysProtos keys_protos;
  bool success = keys_protos.ParseFromString(key_blob);
  if (!success) {
    base::UmaHistogramEnumeration(
        "Storage.InterestGroup.ProtoDeserializationResult."
        "BiddingAndAuctionServerKeyProtos",
        InterestGroupStorageProtoDeserializationResult::kFailed);
    return std::nullopt;
  }
  base::UmaHistogramEnumeration(
      "Storage.InterestGroup.ProtoDeserializationResult."
      "BiddingAndAuctionServerKeyProtos",
      InterestGroupStorageProtoDeserializationResult::kSucceeded);

  if (!keys_protos.origin_scoped_keys().empty()) {
    std::vector<std::pair<url::Origin, std::vector<BiddingAndAuctionServerKey>>>
        origin_scoped_keys;
    for (auto& [origin_str, key_protos] :
         *keys_protos.mutable_origin_scoped_keys()) {
      url::Origin origin = url::Origin::Create(GURL(origin_str));
      if (origin.opaque()) {
        // If we can't recover the key list, just return nullopt so we fetch it
        // again.
        return std::nullopt;
      }

      std::vector<BiddingAndAuctionServerKey> keys =
          KeysFromProto(*key_protos.mutable_keys());
      origin_scoped_keys.emplace_back(origin, std::move(keys));
    }
    return BiddingAndAuctionKeySet(std::move(origin_scoped_keys));
  } else {
    if (keys_protos.keys().empty()) {
      // No keys in this proto. Return nullopt so we fetch it again.
      return std::nullopt;
    }
    std::vector<BiddingAndAuctionServerKey> keys =
        KeysFromProto(*keys_protos.mutable_keys());
    return BiddingAndAuctionKeySet(std::move(keys));
  }
}

BiddingAndAuctionServerKeyFetcher::CallbackQueueItem::CallbackQueueItem(
    BiddingAndAuctionServerKeyFetcherCallback callback,
    url::Origin scope_origin)
    : callback(std::move(callback)), scope_origin(std::move(scope_origin)) {}
BiddingAndAuctionServerKeyFetcher::CallbackQueueItem::~CallbackQueueItem() =
    default;
BiddingAndAuctionServerKeyFetcher::CallbackQueueItem::CallbackQueueItem(
    CallbackQueueItem&& item) = default;
BiddingAndAuctionServerKeyFetcher::CallbackQueueItem&
BiddingAndAuctionServerKeyFetcher::CallbackQueueItem::operator=(
    CallbackQueueItem&& item) = default;

BiddingAndAuctionServerKeyFetcher::PerCoordinatorFetcherState::
    PerCoordinatorFetcherState() = default;
BiddingAndAuctionServerKeyFetcher::PerCoordinatorFetcherState::
    ~PerCoordinatorFetcherState() = default;
BiddingAndAuctionServerKeyFetcher::PerCoordinatorFetcherState::
    PerCoordinatorFetcherState(PerCoordinatorFetcherState&& state) = default;
BiddingAndAuctionServerKeyFetcher::PerCoordinatorFetcherState&
BiddingAndAuctionServerKeyFetcher::PerCoordinatorFetcherState::operator=(
    PerCoordinatorFetcherState&& state) = default;

BiddingAndAuctionServerKeyFetcher::BiddingAndAuctionServerKeyFetcher(
    InterestGroupManagerImpl* manager,
    scoped_refptr<network::SharedURLLoaderFactory> loader_factory)
    : loader_factory_(std::move(loader_factory)), manager_(manager) {
  for (const auto& key_config : kDefaultKeys) {
    url::Origin coordinator = url::Origin::Create(GURL(key_config.origin));
    DCHECK_EQ(coordinator.scheme(), url::kHttpsScheme);
    PerCoordinatorFetcherState state;
    state.key_url = GURL(key_config.key_url);
    state.version = 1;
    state.apis.Put(TrustedServerAPIType::kBiddingAndAuction);
    state.apis.Put(TrustedServerAPIType::kTrustedKeyValue);
    if (!state.key_url.is_valid()) {
      continue;
    }
    fetcher_state_map_.insert_or_assign(std::move(coordinator),
                                        std::move(state));
  }
  if (base::FeatureList::IsEnabled(
          blink::features::kFledgeBiddingAndAuctionServer)) {
    std::string config =
        blink::features::kFledgeBiddingAndAuctionKeyConfig.Get();
    if (!config.empty()) {
      std::optional<base::Value> config_value = base::JSONReader::Read(config);
      if (config_value && config_value->is_dict()) {
        for (const auto kv : config_value->GetDict()) {
          if (!kv.second.is_string()) {
            continue;
          }
          url::Origin coordinator = url::Origin::Create(GURL(kv.first));
          if (coordinator.scheme() != url::kHttpsScheme) {
            continue;
          }

          PerCoordinatorFetcherState state;
          state.key_url = GURL(kv.second.GetString());
          state.version = 1;
          state.apis.Put(TrustedServerAPIType::kBiddingAndAuction);
          state.apis.Put(TrustedServerAPIType::kTrustedKeyValue);
          if (!state.key_url.is_valid()) {
            fetcher_state_map_.erase(coordinator);
            continue;
          }
          fetcher_state_map_.insert_or_assign(std::move(coordinator),
                                              std::move(state));
        }
      }
    }
    GURL key_url = GURL(blink::features::kFledgeBiddingAndAuctionKeyURL.Get());
    if (key_url.is_valid()) {
      PerCoordinatorFetcherState state;
      state.key_url = std::move(key_url);
      state.version = 1;
      state.apis.Put(TrustedServerAPIType::kBiddingAndAuction);
      state.apis.Put(TrustedServerAPIType::kTrustedKeyValue);
      fetcher_state_map_.insert_or_assign(default_gcp_coordinator_,
                                          std::move(state));
    }
  }
  if (base::FeatureList::IsEnabled(blink::features::kFledgeOriginScopedKeys)) {
    std::string config = blink::features::kFledgeOriginScopedKeyConfig.Get();
    if (!config.empty()) {
      std::optional<base::Value> config_value = base::JSONReader::Read(config);
      if (config_value && config_value->is_dict()) {
        for (const auto kv : config_value->GetDict()) {
          if (!kv.second.is_string()) {
            continue;
          }
          url::Origin coordinator = url::Origin::Create(GURL(kv.first));
          if (coordinator.scheme() != url::kHttpsScheme) {
            continue;
          }

          PerCoordinatorFetcherState state;
          state.key_url = GURL(kv.second.GetString());
          state.version = 2;
          state.apis.Put(TrustedServerAPIType::kBiddingAndAuction);
          state.apis.Put(TrustedServerAPIType::kTrustedKeyValue);
          if (!state.key_url.is_valid()) {
            fetcher_state_map_.erase(coordinator);
            continue;
          }
          fetcher_state_map_.insert_or_assign(std::move(coordinator),
                                              std::move(state));
        }
      }
    }
  }
}

BiddingAndAuctionServerKeyFetcher::~BiddingAndAuctionServerKeyFetcher() =
    default;

void BiddingAndAuctionServerKeyFetcher::MaybePrefetchKeys() {
  // We only want to prefetch once.
  if (did_prefetch_keys_) {
    return;
  }
  did_prefetch_keys_ = true;
  for (auto& [coordinator, state] : fetcher_state_map_) {
    if (!state.keyset || !state.keyset->HasKeys() ||
        state.expiration < base::Time::Now()) {
      FetchKeys(url::Origin(), coordinator, state, base::DoNothing());
    }
  }
}

void BiddingAndAuctionServerKeyFetcher::GetOrFetchKey(
    TrustedServerAPIType api,
    const url::Origin& scope_origin,
    const std::optional<url::Origin>& maybe_coordinator,
    BiddingAndAuctionServerKeyFetcherCallback callback) {
  const url::Origin& coordinator = maybe_coordinator.has_value()
                                       ? *maybe_coordinator
                                       : default_gcp_coordinator_;
  auto it = fetcher_state_map_.find(coordinator);
  if (it == fetcher_state_map_.end()) {
    std::move(callback).Run(
        base::unexpected<std::string>("Invalid Coordinator"));
    return;
  }
  PerCoordinatorFetcherState& state = it->second;
  if (!state.apis.Has(api)) {
    std::move(callback).Run(
        base::unexpected<std::string>("API not supported by coordinator"));
    return;
  }

  // If we have keys and they haven't expired just call the callback now.
  if (state.keyset && state.keyset->HasKeys() &&
      (state.debug_override || state.expiration > base::Time::Now())) {
    std::optional<BiddingAndAuctionServerKey> key =
        state.keyset->GetRandomKey(scope_origin);
    if (!key) {
      std::move(callback).Run(
          base::unexpected<std::string>("No key for adtech origin"));
      return;
    }

    // Use a random key from the set to limit the server's ability to identify
    // us based on the key we use.
    base::UmaHistogramBoolean("Ads.InterestGroup.ServerAuction.KeyFetch.Cached",
                              true);
    std::move(callback).Run(std::move(*key));
    return;
  }

  if (!state.key_url.is_valid()) {
    std::move(callback).Run(
        base::unexpected<std::string>("Invalid Coordinator"));
    return;
  }

  base::UmaHistogramBoolean("Ads.InterestGroup.ServerAuction.KeyFetch.Cached",
                            false);
  FetchKeys(scope_origin, coordinator, state, std::move(callback));
}

void BiddingAndAuctionServerKeyFetcher::AddKeysDebugOverride(
    TrustedServerAPIType api,
    const url::Origin& coordinator,
    std::string serialized_keys,
    DebugOverrideCallback callback) {
  auto it = fetcher_state_map_.find(coordinator);
  if (it != fetcher_state_map_.end()) {
    std::move(callback).Run(
        "Can't add debug override because coordinator with origin "
        "already exists");
    return;
  }

  PerCoordinatorFetcherState state;
  state.version = 2;
  state.apis.Put(api);
  state.debug_override = true;
  state.debug_override_callback = std::move(callback);
  fetcher_state_map_.insert(std::pair(coordinator, std::move(state)));
  // Pretend we succeeded in loading from network.
  OnFetchKeysFromNetworkComplete(
      std::move(coordinator),
      std::make_unique<std::string>(std::move(serialized_keys)));
}

void BiddingAndAuctionServerKeyFetcher::FetchKeys(
    const url::Origin& scope_origin,
    const url::Origin& coordinator,
    PerCoordinatorFetcherState& state,
    BiddingAndAuctionServerKeyFetcherCallback callback) {
  state.fetch_start = base::TimeTicks::Now();
  state.queue.emplace_back(std::move(callback), scope_origin);
  // If we're already fetching or parsing a debug override, just wait for that
  // to finish.
  if (state.queue.size() > 1 || state.debug_override) {
    return;
  }

  state.keyset.reset();

  if (base::FeatureList::IsEnabled(features::kFledgeStoreBandAKeysInDB)) {
    manager_->GetBiddingAndAuctionServerKeys(
        coordinator,
        base::BindOnce(
            &BiddingAndAuctionServerKeyFetcher::OnFetchKeysFromDatabaseComplete,
            weak_ptr_factory_.GetWeakPtr(), coordinator));
  } else {
    FetchKeysFromNetwork(coordinator);
  }
}

void BiddingAndAuctionServerKeyFetcher::OnFetchKeysFromDatabaseComplete(
    const url::Origin& coordinator,
    std::pair<base::Time, std::string> expiration_and_keys) {
  if (expiration_and_keys.first < base::Time::Now()) {
    base::UmaHistogramBoolean(
        "Ads.InterestGroup.ServerAuction.KeyFetch.DBCached", false);
    FetchKeysFromNetwork(coordinator);
    return;
  }

  PerCoordinatorFetcherState& state = fetcher_state_map_.at(coordinator);
  std::optional<BiddingAndAuctionKeySet> keyset =
      BiddingAndAuctionKeySet::FromBinaryProto(expiration_and_keys.second);
  if (!keyset || keyset->SchemaVersion() != state.version) {
    base::UmaHistogramBoolean(
        "Ads.InterestGroup.ServerAuction.KeyFetch.DBCached", false);
    FetchKeysFromNetwork(coordinator);
    return;
  }

  base::UmaHistogramBoolean("Ads.InterestGroup.ServerAuction.KeyFetch.DBCached",
                            true);
  CacheKeysAndRunAllCallbacks(coordinator, std::move(*keyset),
                              expiration_and_keys.first);
}

void BiddingAndAuctionServerKeyFetcher::FetchKeysFromNetwork(
    const url::Origin& coordinator) {
  PerCoordinatorFetcherState& state = fetcher_state_map_.at(coordinator);
  state.network_fetch_start = base::TimeTicks::Now();

  CHECK(!state.loader);
  auto resource_request = std::make_unique<network::ResourceRequest>();
  resource_request->url = state.key_url;
  resource_request->credentials_mode = network::mojom::CredentialsMode::kOmit;
  resource_request->trusted_params.emplace();
  resource_request->trusted_params->isolation_info = net::IsolationInfo();
  state.loader = network::SimpleURLLoader::Create(
      std::move(resource_request),
      kBiddingAndAuctionServerKeyFetchTrafficAnnotation);
  state.loader->SetTimeoutDuration(kRequestTimeout);

  state.loader->DownloadToString(
      loader_factory_.get(),
      base::BindOnce(
          &BiddingAndAuctionServerKeyFetcher::OnFetchKeysFromNetworkComplete,
          weak_ptr_factory_.GetWeakPtr(), coordinator),
      /*max_body_size=*/kMaxBodySize);
}

void BiddingAndAuctionServerKeyFetcher::OnFetchKeysFromNetworkComplete(
    url::Origin coordinator,
    std::unique_ptr<std::string> response) {
  PerCoordinatorFetcherState& state = fetcher_state_map_.at(coordinator);
  bool was_cached = !state.debug_override && state.loader->LoadedFromCache();
  state.loader.reset();
  if (!response) {
    FailAllCallbacks(coordinator);
    return;
  }
  if (!state.debug_override) {
    base::UmaHistogramTimes(
        "Ads.InterestGroup.ServerAuction.KeyFetch.NetworkTime",
        base::TimeTicks::Now() - state.network_fetch_start);
    base::UmaHistogramBoolean(
        "Ads.InterestGroup.ServerAuction.KeyFetch.NetworkCached", was_cached);
  }
  if (state.version == 1) {
    data_decoder::DataDecoder::ParseJsonIsolated(
        *response,
        base::BindOnce(&BiddingAndAuctionServerKeyFetcher::OnParsedKeys,
                       weak_ptr_factory_.GetWeakPtr(), coordinator));
  } else {
    data_decoder::DataDecoder::ParseJsonIsolated(
        *response,
        base::BindOnce(&BiddingAndAuctionServerKeyFetcher::OnParsedKeysV2,
                       weak_ptr_factory_.GetWeakPtr(), coordinator));
  }
}

void BiddingAndAuctionServerKeyFetcher::OnParsedKeys(
    url::Origin coordinator,
    data_decoder::DataDecoder::ValueOrError result) {
  if (!result.has_value()) {
    FailAllCallbacks(coordinator);
    return;
  }

  const base::Value::Dict* response_dict = result->GetIfDict();
  if (!response_dict) {
    FailAllCallbacks(coordinator);
    return;
  }

  const base::Value::List* keys_list = response_dict->FindList("keys");
  if (!keys_list) {
    FailAllCallbacks(coordinator);
    return;
  }

  std::vector<BiddingAndAuctionServerKey> keys = ParseKeysList(keys_list);

  if (keys.size() == 0) {
    FailAllCallbacks(coordinator);
    return;
  }

  BiddingAndAuctionKeySet keyset(std::move(keys));
  base::Time expiration = base::Time::Now() + kKeyRequestInterval;
  if (base::FeatureList::IsEnabled(features::kFledgeStoreBandAKeysInDB) &&
      !fetcher_state_map_.at(coordinator).debug_override) {
    manager_->SetBiddingAndAuctionServerKeys(
        coordinator, keyset.AsBinaryProto(), expiration);
  }
  CacheKeysAndRunAllCallbacks(coordinator, std::move(keyset), expiration);
}

void BiddingAndAuctionServerKeyFetcher::OnParsedKeysV2(
    url::Origin coordinator,
    data_decoder::DataDecoder::ValueOrError result) {
  if (!result.has_value()) {
    FailAllCallbacks(coordinator);
    return;
  }

  const base::Value::Dict* response_dict = result->GetIfDict();
  if (!response_dict) {
    FailAllCallbacks(coordinator);
    return;
  }
  std::vector<std::pair<url::Origin, std::vector<BiddingAndAuctionServerKey>>>
      origin_scoped_keys;

  const base::Value::Dict* origin_scoped_keys_dict =
      response_dict->FindDict("originScopedKeys");
  if (!origin_scoped_keys_dict) {
    FailAllCallbacks(coordinator);
    return;
  }

  for (const auto [origin_str, per_origin_keys_value] :
       *origin_scoped_keys_dict) {
    url::Origin origin = url::Origin::Create(GURL(origin_str));

    if (origin.opaque()) {
      // Origins should never be opaque. That means parsing failed.
      FailAllCallbacks(coordinator);
      return;
    }

    if (!per_origin_keys_value.is_dict()) {
      FailAllCallbacks(coordinator);
      return;
    }
    const base::Value::List* keys_list =
        per_origin_keys_value.GetDict().FindList("keys");
    if (!keys_list) {
      FailAllCallbacks(coordinator);
      return;
    }

    std::vector<BiddingAndAuctionServerKey> keys = ParseKeysList(keys_list);

    if (keys.empty()) {
      FailAllCallbacks(coordinator);
      return;
    }

    origin_scoped_keys.emplace_back(origin, std::move(keys));
  }
  if (origin_scoped_keys.empty()) {
    FailAllCallbacks(coordinator);
    return;
  }

  BiddingAndAuctionKeySet keyset(std::move(origin_scoped_keys));
  base::Time expiration = base::Time::Now() + kKeyRequestInterval;
  if (base::FeatureList::IsEnabled(features::kFledgeStoreBandAKeysInDB) &&
      !fetcher_state_map_.at(coordinator).debug_override) {
    manager_->SetBiddingAndAuctionServerKeys(
        coordinator, keyset.AsBinaryProto(), expiration);
  }
  CacheKeysAndRunAllCallbacks(coordinator, std::move(keyset), expiration);
}

void BiddingAndAuctionServerKeyFetcher::CacheKeysAndRunAllCallbacks(
    const url::Origin& coordinator,
    BiddingAndAuctionKeySet keyset,
    base::Time expiration) {
  PerCoordinatorFetcherState& state = fetcher_state_map_.at(coordinator);
  DCHECK_EQ(keyset.SchemaVersion(), state.version);
  state.keyset = std::move(keyset);
  state.expiration = expiration;

  while (!state.queue.empty()) {
    // We call the callback *before* removing the current request from the list.
    // That avoids the problem if callback were to synchronously enqueue another
    // request. If we removed the current request first then enqueued the
    // request, that would start another thread of execution since there was an
    // empty queue.
    // Use a random key from the set to limit the server's ability to identify
    // us based on the key we use.

    std::optional<BiddingAndAuctionServerKey> key =
        state.keyset->GetRandomKey(state.queue.front().scope_origin);
    if (!key) {
      std::move(state.queue.front().callback)
          .Run(base::unexpected<std::string>("No key for adtech origin"));
    } else {
      std::move(state.queue.front().callback).Run(std::move(*key));
    }
    state.queue.pop_front();
  }

  if (state.debug_override_callback) {
    std::move(state.debug_override_callback).Run(std::nullopt);
  }
}

void BiddingAndAuctionServerKeyFetcher::FailAllCallbacks(
    url::Origin coordinator) {
  PerCoordinatorFetcherState& state = fetcher_state_map_.at(coordinator);
  while (!state.queue.empty()) {
    // We call the callback *before* removing the current request from the list.
    // That avoids the problem if callback were to synchronously enqueue another
    // request. If we removed the current request first then enqueued the
    // request, that would start another thread of execution since there was an
    // empty queue.
    std::move(state.queue.front().callback)
        .Run(base::unexpected<std::string>("Key fetch failed"));
    state.queue.pop_front();
  }

  if (state.debug_override_callback) {
    std::move(state.debug_override_callback).Run("Key config decoding failed");
  }
}

}  // namespace content