forked from WICG/turtledove
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spec.bs
2239 lines (1986 loc) · 120 KB
/
spec.bs
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
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<pre class="metadata">
Title: Protected Audience (formerly FLEDGE)
Shortname: protected-audience
Repository: WICG/turtledove
Inline Github Issues: true
Group: WICG
Status: CG-DRAFT
Level: 1
URL: https://wicg.github.io/turtledove/
Boilerplate: omit conformance, omit feedback-header
Editor: Paul Jensen, Google https://www.google.com/, [email protected]
Abstract: Provides a privacy advancing API to facilitate interest group based advertising.
!Participate: <a href="https://github.com/WICG/turtledove">GitHub WICG/turtledove</a> (<a href="https://github.com/WICG/turtledove/issues/new">new issue</a>, <a href="https://github.com/WICG/turtledove/issues?state=open">open issues</a>)
!Commits: <a href="https://github.com/WICG/turtledove/commits/main/spec.bs">GitHub spec.bs commits</a>
Complain About: accidental-2119 yes, missing-example-ids yes
Indent: 2
Default Biblio Status: current
Markup Shorthands: markdown yes
Assume Explicit For: yes
</pre>
<pre class="anchors">
urlPrefix: https://www.ietf.org/rfc/rfc4122.txt
type: dfn; text: urn uuid
spec: html; urlPrefix: https://html.spec.whatwg.org/C
type: dfn
text: create an agent; url: create-an-agent
text: immediately; url: immediately
spec: ECMASCRIPT; urlPrefix: https://tc39.es/ecma262/
type: dfn
text: ParseScript; url: sec-parse-script
text: abrupt completion; url: sec-completion-record-specification-type
text: throw completion; url: sec-completion-record-specification-type
text: ScriptEvaluation; url: sec-runtime-semantics-scriptevaluation
spec: RFC8941; urlPrefix: https://httpwg.org/specs/rfc8941.html
type: dfn
text: structured header; url: top
for: structured header
text: integer; url: integer
spec: WebAssembly-js-api; urlPrefix: https://webassembly.github.io/spec/js-api/
type: dfn
text: compiling a WebAssembly module; url: #compile-a-webassembly-module
</pre>
# Introduction # {#intro}
<em>This section is non-normative</em>
The Protected Audience API facilitates selecting an advertisement to display to a user based on a
previous interaction with the advertiser or advertising network.
When a user's interactions with an advertiser indicate an interest in something, the advertiser can
ask the browser to record this interest on-device by calling
{{Window/navigator}}.{{Navigator/joinAdInterestGroup()}}. Later, when a website wants to select an
advertisement to show to the user, the website can call
{{Window/navigator}}.{{Navigator/runAdAuction()}} to ask the browser to conduct an auction where
each of these on-device recorded interests are given the chance to calculate a bid to display their
advertisement.
<h2 id="joining-interest-groups">Joining Interest Groups</h2>
When a user's interactions with a website indicate that the user may have a particular interest, an
advertiser or someone working on behalf of the advertiser (e.g. a demand side platform, DSP) can ask
the user's browser to record this interest on-device by calling
{{Window/navigator}}.{{Navigator/joinAdInterestGroup()}}. This indicates an intent to display an
advertisement relevant to this interest to this user in the future. The user agent has an
<dfn>interest group set</dfn>, a [=list=] of [=interest groups=] in which
[=interest group/owner=] / [=interest group/name=] pairs are unique.
<xmp class="idl">
[SecureContext]
partial interface Navigator {
Promise<undefined> joinAdInterestGroup(AuctionAdInterestGroup group, double durationSeconds);
};
dictionary AuctionAd {
required USVString renderURL;
any metadata;
};
dictionary AuctionAdInterestGroup {
required USVString owner;
required USVString name;
double priority = 0.0;
boolean enableBiddingSignalsPrioritization = false;
record<DOMString, double> priorityVector;
record<DOMString, double> prioritySignalsOverrides;
DOMString executionMode = "compatibility";
USVString biddingLogicURL;
USVString biddingWasmHelperURL;
USVString updateURL;
USVString trustedBiddingSignalsURL;
sequence<USVString> trustedBiddingSignalsKeys;
any userBiddingSignals;
sequence<AuctionAd> ads;
sequence<AuctionAd> adComponents;
};
</xmp>
<div algorithm="joinAdInterestGroup()">
The <dfn for=Navigator method>joinAdInterestGroup(|group|, |durationSeconds|)</dfn> method steps
are:
1. If [=this=]'s [=relevant global object=]'s [=associated Document=] is not [=allowed to use=] the
"[=join-ad-interest-group=]" [=policy-controlled feature=], then [=exception/throw=] a
"{{NotAllowedError}}" {{DOMException}}.
1. Let |frameOrigin| be the [=relevant settings object=]'s [=environment settings object/origin=].
1. [=Assert=] that |frameOrigin| is not an [=opaque origin=] and its [=origin/scheme=] is "https".
1. Let |interestGroup| be a new [=interest group=].
1. Validate the given |group| and set |interestGroup|'s fields accordingly.
1. Set |interestGroup|'s [=interest group/expiry=] to the [=current wall time=] plus |durationSeconds|.
1. Set |interestGroup|'s [=interest group/next update after=] to the [=current wall time=] plus 24 hours.
1. Set |interestGroup|'s [=interest group/owner=] to the result of [=parsing an origin=] on
|group|["{{AuctionAdInterestGroup/owner}}"].
1. If |interestGroup|'s [=interest group/owner=] is failure, or its [=url/scheme=] is not
"`https`", [=exception/throw=] a {{TypeError}}.
1. Set |interestGroup|'s [=interest group/name=] to |group|["{{AuctionAdInterestGroup/name}}"].
1. Set |interestGroup|'s [=interest group/priority=] to
|group|["{{AuctionAdInterestGroup/priority}}"].
1. Set |interestGroup|'s [=interest group/enable bidding signals prioritization=] to
|group|["{{AuctionAdInterestGroup/enableBiddingSignalsPrioritization}}"].
1. If |group|["{{AuctionAdInterestGroup/priorityVector}}"] [=map/exists=], then set
|interestGroup|'s [=interest group/priority vector=] to
|group|["{{AuctionAdInterestGroup/priorityVector}}"].
1. If |group|["{{AuctionAdInterestGroup/prioritySignalsOverrides}}"] [=map/exists=], then set
|interestGroup|'s [=interest group/priority signals overrides=] to
|group|["{{AuctionAdInterestGroup/prioritySignalsOverrides}}"].
1. Set |interestGroup|'s [=interest group/execution mode=] to
|group|["{{AuctionAdInterestGroup/executionMode}}"].
1. For each |groupMember| and |interestGroupField| in the following table <table class="data">
<thead><tr><th>Group member</th><th>Interest group field</th></tr></thead>
<tr>
<td>"{{AuctionAdInterestGroup/biddingLogicURL}}"</td>
<td>[=interest group/bidding url=]</td>
</tr>
<tr>
<td>"{{AuctionAdInterestGroup/biddingWasmHelperURL}}"</td>
<td>[=interest group/bidding wasm helper url=]</td>
</tr>
<tr>
<td>"{{AuctionAdInterestGroup/updateURL}}"</td>
<td>[=interest group/update url=]</td>
</tr>
<tr>
<td>"{{AuctionAdInterestGroup/trustedBiddingSignalsURL}}"</td>
<td>[=interest group/trusted bidding signals url=]</td>
</tr>
</table>
1. If |group| [=map/contains=] |groupMember|:
1. Let |parsedUrl| be the result of running the [=URL parser=] on |group|[|groupMember|].
1. [=exception/Throw=] a {{TypeError}} if any of the following conditions hold:
* |parsedUrl| is failure;
* |parsedUrl| is not [=same origin=] with |interestGroup|'s [=interest group/owner=];
* |parsedUrl| [=includes credentials=];
* |parsedUrl| [=url/fragment=] is not null.
1. Set |interestGroup|'s |interestGroupField| to |parsedUrl|.
1. If |interestGroup|'s [=interest group/trusted bidding signals url=]'s [=url/query=] is not
null, then [=exception/throw=] a {{TypeError}}.
1. If |group|["{{AuctionAdInterestGroup/trustedBiddingSignalsKeys}}"] [=map/exists=], then set
|interestGroup|'s [=interest group/trusted bidding signals keys=] to
|group|["{{AuctionAdInterestGroup/trustedBiddingSignalsKeys}}"].
1. If |group|["{{AuctionAdInterestGroup/userBiddingSignals}}"] [=map/exists=]:
1. Let |interestGroup|'s [=interest group/user bidding signals=] be the result of
[=serializing a JavaScript value to a JSON string=], given
|group|["{{AuctionAdInterestGroup/userBiddingSignals}}"]. This can [=exception/throw=] a
{{TypeError}}.
1. For each |groupMember| and |interestGroupField| in the following table <table class="data">
<thead><tr><th>Group member</th><th>Interest group field</th></tr></thead>
<tr>
<td>"{{AuctionAdInterestGroup/ads}}"</td>
<td>[=interest group/ads=]</td>
</tr>
<tr>
<td>"{{AuctionAdInterestGroup/adComponents}}"</td>
<td>[=interest group/ad components=]</td>
</tr>
</table>
1. [=list/For each=] |ad| of |group|[|groupMember|]:
1. Let |igAd| be a new [=interest group ad=].
1. Let |renderURL| be the result of running the [=URL parser=] on
|ad|["{{AuctionAd/renderURL}}"].
1. [=exception/Throw=] a {{TypeError}} if any of the following conditions hold:
* |renderURL| is failure;
* |renderURL| [=url/scheme=] is not "`https`";
* |renderURL| [=includes credentials=].
1. Set |igAd|'s [=interest group ad/render url=] to |renderURL|.
1. If |ad|["{{AuctionAd/metadata}}"] [=map/exists=], then let
|igAd|'s [=interest group ad/metadata=] be the result of
[=serializing a JavaScript value to a JSON string=], given |ad|["{{AuctionAd/metadata}}"].
This can [=exception/throw=] a {{TypeError}}.
1. [=list/Append=] |igAd| to |interestGroup|'s |interestGroupField|.
1. If |interestGroup|'s [=interest group/estimated size=] is greater than 50 KB, then
[=exception/throw=] a {{TypeError}}.
1. Let |p| be [=a new promise=].
1. Let |queue| be the result of [=starting a new parallel queue=].
1. [=parallel queue/enqueue steps|Enqueue the following steps=] to |queue|:
1. Let |permission| be the result of [=checking interest group permissions=] with
|interestGroup|'s [=interest group/owner=], |frameOrigin|, and true.
1. If |permission| is false, then [=queue a task=] to [=reject=] |p| with a
"{{NotAllowedError}}" {{DOMException}} and do not run the remaining steps.
1. [=Queue a task=] to [=resolve=] |p| with `undefined`.
1. If the browser is currently storing an interest group with `owner` and `name` that matches
|interestGroup|, then set the [=interest group/bid counts=],
[=interest group/join counts=], and [=interest group/previous wins=] of
|interestGroup| to the values of the currently stored one and remove
the currently stored one from the browser.
1. Set |interestGroup|'s [=interest group/joining origin=] to [=this=]'s
[=relevant settings object=]'s [=environment/top-level origin=].
1. If the most recent entry in [=interest group/join counts=] corresponds to
the current local day, increment its count. If not, insert a new entry with
the time set to the current local day and a count of 1.
1. Store |interestGroup| in the browser’s [=interest group set=].
1. Return |p|.
</div>
<div algorithm>
The <dfn for="interest group">estimated size</dfn> of an [=interest group=] |ig| is the sum of:
1. The [=string/length=] of the [=serialization of an origin|serialization=] of |ig|'s
[=interest group/owner=].
1. The [=string/length=] of |ig|'s [=interest group/name=].
1. 8 bytes, which is the size of |ig|'s [=interest group/priority=].
1. The [=string/length=] of |ig|'s [=interest group/execution mode=].
1. 2 bytes, which is the size of |ig|'s [=interest group/enable bidding signals prioritization=].
1. If |ig|'s [=interest group/priority vector=] is not null, [=map/for each=] |key| → |value| of
[=interest group/priority vector=]:
1. The [=string/length=] of |key|.
1. 8 bytes, which is the size of |value|.
1. If |ig|'s [=interest group/priority signals overrides=] is not null, [=map/for each=] |key| →
|value| of [=interest group/priority signals overrides=]:
1. The [=string/length=] of |key|.
1. 8 bytes, which is the size of |value|.
1. The size of [=interest group/execution mode=].
1. The [=string/length=] of the [=URL serializer|serialization=] of |ig|'s
[=interest group/bidding url=], if the field is not null.
1. The [=string/length=] of the [=URL serializer|serialization=] of |ig|'s
[=interest group/bidding wasm helper url=], if the field is not null.
1. The [=string/length=] of the [=URL serializer|serialization=] of |ig|'s
[=interest group/update url=], if the field is not null.
1. The [=string/length=] of the [=URL serializer|serialization=] of |ig|'s
[=interest group/trusted bidding signals url=], if the field is not null.
1. [=list/For each=] |key| of |ig|'s [=interest group/trusted bidding signals keys=]:
1. The [=string/length=] of |key|.
1. The [=string/length=] of |ig|'s [=interest group/user bidding signals=].
1. If |ig|'s [=interest group/ads=] is not null, [=list/for each=] |ad| of it:
1. The [=string/length=] of the [=URL serializer|serialization=] of |ad|'s
[=interest group ad/render url=].
1. The [=string/length=] of |ad|'s [=interest group ad/metadata=] if the field is not null.
1. If |ig|'s [=interest group/ad components=] is not null, [=list/for each=] |ad| of it:
1. The [=string/length=] of the [=URL serializer|serialization=] of |ad|'s
[=interest group ad/render url=].
1. The [=string/length=] of |ad|'s [=interest group ad/metadata=] if the field is not null.
</div>
<div algorithm>
To <dfn>check interest group permissions</dfn> given an [=origin=]
|ownerOrigin|, an [=origin=] |frameOrigin|, and a [=boolean=] |isJoin|:
1. If |ownerOrigin| is [=same origin=] to |frameOrigin|, then return true.
1. Let |permissionsUrl| be the result of [=building an interest group permissions url=]
with |ownerOrigin| and |frameOrigin|.
1. Let |request| be the result of [=creating a request=] with |permissionsUrl|,
"`application/json`", and null.
1. Let |resource| be null.
1. [=Fetch=] |request| with [=fetch/processResponseConsumeBody=] set to the following steps given a
[=response=] |response| and |responseBody|:
1. If |responseBody| is null or failure, set |resource| to failure and return.
1. Let |headers| be |response|'s [=response/header list=].
1. Let |mimeType| be the result of [=header list/extracting a MIME type=] from |headers|.
1. If |mimeType| is failure or is not a [=JSON MIME Type=], throw, set |resource| to failure and return.
1. Set |resource| to |responseBody|.
1. Wait for |resource| to be set.
1. If |resource| is failure, then return false.
1. Let |permissions| be the result of [=parsing a JSON string to an Infra value=] with |resource|, returning false
on failure.
1. If |permissions| is not an [=ordered map=], then return false.
1. If |isJoin| is true and |permissions|["`joinAdInterestGroup`"] [=map/exists=], then return |permissions|["`joinAdInterestGroup`"].
1. If |isJoin| is false and |permissions|["`leaveAdInterestGroup`"] [=map/exists=], then return |permissions|["`leaveAdInterestGroup`"].
1. Return false.
The browser may cache requests for |permissionsUrl| within a network partition.
In order to prevent leaking data, the browser must request |permissionsUrl|
regardless of whether the user is a member of the ad interest group. This
prevents a leak of the user's ad interest group membership to the server.
</div>
<div algorithm>
To <dfn>build an interest group permissions url</dfn> given a [=origin=] |ownerOrigin| and a [=origin=] |frameOrigin|:
1. Let |serializedFrameOrigin| be the result of [=serialization of an origin|serializing=] |frameOrigin|.
1. Return the string formed by [=string/concatenating=]
* The [=serialization of an origin|serialization=] of |ownerOrigin|,
* The string "`/.well-known/interest-group/permissions/?origin=`", and
* The result of [=string/UTF-8 percent-encoding=] |serializedFrameOrigin| using [=component percent-encode set=].
</div>
<h2 id="leaving-interest-groups">Leaving Interest Groups</h2>
In order to remove a user from a particular interest group,
{{Window/navigator}}.{{Navigator/leaveAdInterestGroup()}} can be called.
TODO: Edit the following from the explainer. As a special case to support in-ad UIs, invoking
navigator.leaveAdInterestGroup() from inside an ad that is being targeted at a particular interest
group will cause the browser to leave that group, irrespective of permission policies. Note that
calling navigator.leaveAdInterestGroup() without arguments isn't supported inside a component ad
frame.
<xmp class="idl">
[SecureContext]
partial interface Navigator {
Promise<undefined> leaveAdInterestGroup(AuctionAdInterestGroupKey group);
};
dictionary AuctionAdInterestGroupKey {
required USVString owner;
required USVString name;
};
</xmp>
<div algorithm>
The <dfn for=Navigator method>leaveAdInterestGroup(group)</dfn> method steps
are:
1. If [=this=]'s [=relevant global object=]'s [=associated Document=] is not [=allowed to use=] the
"[=join-ad-interest-group=]" [=policy-controlled feature=], then [=exception/throw=] a
"{{NotAllowedError}}" {{DOMException}}.
1. Let |frameOrigin| be the [=relevant settings object=]'s [=environment settings object/origin=].
1. [=Assert=] that |frameOrigin| is not an [=opaque origin=] and its [=origin/scheme=] is "https".
1. Let |owner| be the result of [=parsing an origin=] with
|group|["{{AuctionAdInterestGroupKey/owner}}"].
1. If |owner| is failure, [=exception/throw=] a {{TypeError}}.
1. Let |name| be |group|["{{AuctionAdInterestGroupKey/name}}"].
1. Let |p| be [=a new promise=].
1. Run these steps [=in parallel=]:
1. Let |permission| be the result of [=checking interest group permissions=] with
|owner|, |frameOrigin|, and false.
1. If |permission| is false, then [=queue a task=] to [=reject=] |p| with a
"{{NotAllowedError}}" {{DOMException}} and do not run the remaining steps.
1. [=Queue a task=] to [=resolve=] |p| with `undefined`.
1. [=list/Remove=] [=interest groups=] from the user agent's [=interest group set=] whose
[=interest group/owner=] is |owner| and [=interest group/name=] is |name|.
1. Return |p|.
</div>
<h2 id="running-ad-auctions">Running Ad Auctions</h2>
When a website or someone working on behalf of the website (e.g. a supply side platform, SSP) wants
to conduct an auction to select an advertisement to display to the user, they can call the
{{Window/navigator}}.{{Navigator/runAdAuction()}} function, providing an auction configuration that
tells the browser how to conduct the auction and which on-device recorded interests are allowed to
bid in the auction for the chance to display their advertisement.
<h3 id="runadauction">runAdAuction()</h3>
<xmp class="idl">
[SecureContext]
partial interface Navigator {
Promise<USVString?> runAdAuction(AuctionAdConfig config);
};
dictionary AuctionAdConfig {
required USVString seller;
required USVString decisionLogicURL;
USVString trustedScoringSignalsURL;
sequence<USVString> interestGroupBuyers;
any auctionSignals;
any sellerSignals;
USVString directFromSellerSignals;
unsigned long long sellerTimeout;
unsigned short sellerExperimentGroupId;
record<USVString, any> perBuyerSignals;
record<USVString, unsigned long long> perBuyerTimeouts;
record<USVString, unsigned short> perBuyerGroupLimits;
record<USVString, unsigned short> perBuyerExperimentGroupIds;
record<USVString, record<USVString, double>> perBuyerPrioritySignals;
sequence<AuctionAdConfig> componentAuctions = [];
AbortSignal? signal;
};
</xmp>
<div algorithm="runAdAuction()">
The <dfn for=Navigator method>runAdAuction(|config|)</dfn> method steps are:
1. Let |global| be [=this=]'s [=relevant global object=].
1. Let |settings| be [=this=]'s [=relevant settings object=].
1. If |global|'s [=associated Document=] is not [=allowed to use=] the "[=run-ad-auction=]"
[=policy-controlled feature=], then [=exception/throw=] a "{{NotAllowedError}}" {{DOMException}}.
1. Let |auctionConfig| be the result of running [=validate and convert auction ad config=] with
|config| and [=validate and convert auction ad config/isTopLevel=] set to true.
1. If |auctionConfig| is failure, then [=exception/throw=] a {{TypeError}}.
1. Let |p| be [=a new promise=].
1. If |config|["{{AuctionAdConfig/signal}}"] [=map/exists=], then:
1. Let |signal| be |config|["{{AuctionAdConfig/signal}}"].
1. If |signal| is [=AbortSignal/aborted=], then [=reject=] |p| with |signal|'s
[=AbortSignal/abort reason=] and return |p|.
1. [=AbortSignal/Add|Add the following abort steps=] to |signal|:
1. [=Reject=] |p| with |signal|’s [=AbortSignal/abort reason=].
1. TODO: Update bidCount for interest groups that participated in the auction.
1. Run [=interest group update=] with |auctionConfig|'s
[=auction config/interest group buyers=].
1. Let |queue| be the result of [=starting a new parallel queue=].
1. [=parallel queue/enqueue steps|Enqueue the following steps=] to |queue|:
1. Let |winner| be the result of running [=generate and score bids=] with |auctionConfig|, null,
|global|, and |settings|.
1. If |winner| is failure:
1. [=Queue a global task=] on [=DOM manipulation task source=], given |global|, to [=reject=]
|p| with a "{{TypeError}}".
1. If |winner| is null:
1. [=Queue a global task=] on [=DOM manipulation task source=], given |global|, to resolve |p|
with null.
1. Otherwise:
1. [=Queue a global task=] on the [=DOM manipulation task source=], given |global|, to resolve
|p| with |winner|'s [=generated bid/ad descriptor=]. TODO: resolve |p| with urn-uuid, instead
of a URL.
1. Run [=interest group update=] with |auctionConfig|'s [=auction config/interest group buyers=].
1. TODO: Update bidCount and prevWins for interest groups that participated in the auction.
1. Return |p|.
</div>
<div algorithm="validate and convert auction ad config">
To <dfn>validate and convert auction ad config</dfn> given an {{AuctionAdConfig}} |config| and a
[=boolean=] <dfn for="validate and convert auction ad config">|isTopLevel|</dfn>:
1. Let |auctionConfig| be a new [=auction config=].
1. Let |auctionConfig|'s [=auction config/seller=] be the result of [=parsing an origin=] with
|config|["{{AuctionAdConfig/seller}}"].
1. [=exception/Throw=] a {{TypeError}} if |auctionConfig|'s [=auction config/seller=] is failure, or
its [=url/scheme=] is not "`https`".
1. Let |decisionLogicURL| be the result of running the [=URL parser=] on
|config|["{{AuctionAdConfig/decisionLogicURL}}"].
1. [=exception/Throw=] a {{TypeError}} if |decisionLogicURL| is failure, or it is not
[=same origin=] with |auctionConfig|'s [=auction config/seller=].
1. [=Assert=]: |decisionLogicURL|'s [=url/scheme=] is "`https`".
1. Set |auctionConfig|'s [=auction config/decision logic url=] to |decisionLogicURL|.
1. If |config|["{{AuctionAdConfig/trustedScoringSignalsURL}}"] [=map/exists=]:
1. Let |trustedScoringSignalsURL| be the result of running the [=URL parser=] on
|config|["{{AuctionAdConfig/trustedScoringSignalsURL}}"].
1. [=exception/Throw=] a {{TypeError}} if |trustedScoringSignalsURL| is failure, or it is not
[=same origin=] with |auctionConfig|'s [=auction config/seller=].
1. [=Assert=]: |trustedScoringSignalsURL|'s [=url/scheme=] is "`https`".
1. Set |auctionConfig|'s [=auction config/trusted scoring signals url=] to
|trustedScoringSignalsURL|.
1. If |config|["{{AuctionAdConfig/interestGroupBuyers}}"] [=map/exists=], let |buyers| be a new
[=list/is empty|empty=] [=list=].
1. [=list/For each=] |buyerString| in |config|["{{AuctionAdConfig/interestGroupBuyers}}"]:
1. Let |buyer| be the result of [=parsing an origin=] with |buyerString|. If |buyer| is failure,
or |buyer|'s [=url/scheme=] is not "`https`", then [=exception/throw=] a {{TypeError}}.
Otherwise, [=list/append=] |buyer| to |buyers|.
1. Set |auctionConfig|'s [=auction config/interest group buyers=] to |buyers|.
1. If |config|["{{AuctionAdConfig/auctionSignals}}"] [=map/exists=]:
1. If |config|["{{AuctionAdConfig/auctionSignals}}"] is a {{Promise}}:
1. Let |auctionConfig|'s [=auction config/auction signals=] be
|config|["{{AuctionAdConfig/auctionSignals}}"].
1. Increment |auctionConfig|'s [=auction config/pending promise count=].
1. [=Upon fulfillment=] of |auctionConfig|'s [=auction config/auction signals=] with |result|:
1. Set |auctionConfig|'s [=auction config/auction signals=] to the result of
[=serializing a JavaScript value to a JSON string=], given |result|.
1. Decrement |auctionConfig|'s [=auction config/pending promise count=].
1. [=Upon rejection=] of |auctionConfig|'s [=auction config/auction signals=]:
1. Set |auctionConfig|'s [=auction config/auction signals=] to failure.
1. Decrement |auctionConfig|'s [=auction config/pending promise count=].
1. Otherwise, let |auctionConfig|'s [=auction config/auction signals=] be the result of
[=serializing a JavaScript value to a JSON string=], given
|config|["{{AuctionAdConfig/auctionSignals}}"].
1. If |config|["{{AuctionAdConfig/sellerSignals}}"] [=map/exists=]:
1. If |config|["{{AuctionAdConfig/sellerSignals}}"] is a {{Promise}}:
1. Let |auctionConfig|'s [=auction config/seller signals=] be
|config|["{{AuctionAdConfig/sellerSignals}}"].
1. Increment |auctionConfig|'s [=auction config/pending promise count=].
1. [=Upon fulfillment=] of |auctionConfig|'s [=auction config/seller signals=] with |result|:
1. Set |auctionConfig|'s [=auction config/seller signals=] to the result of
[=serializing a JavaScript value to a JSON string=], given |result|.
1. Decrement |auctionConfig|'s [=auction config/pending promise count=].
1. [=Upon rejection=] of |auctionConfig|'s [=auction config/seller signals=]:
1. Set |auctionConfig|'s [=auction config/seller signals=] to failure.
1. Decrement |auctionConfig|'s [=auction config/pending promise count=].
1. Otherwise, let |auctionConfig|'s [=auction config/seller signals=] be the result of
[=serializing a JavaScript value to a JSON string=], given
|config|["{{AuctionAdConfig/sellerSignals}}"].
1. If |config|["{{AuctionAdConfig/directFromSellerSignals}}"] [=map/exists=], let
|directFromSellerSignalsPrefix| be the result of running the [=URL parser=] on
|config|["{{AuctionAdConfig/directFromSellerSignals}}"].
1. [=exception/Throw=] a {{TypeError}} if any of the following conditions hold:
* |directFromSellerSignalsPrefix| is failure;
* |directFromSellerSignalsPrefix| is not [=same origin=] with |auctionConfig|'s
[=auction config/seller=];
* |directFromSellerSignalsPrefix|'s [=url/query=] is not null.
1. [=Assert=]: |directFromSellerSignalsPrefix|'s [=url/scheme=] is "`https`".
1. If |config|["{{AuctionAdConfig/sellerTimeout}}"] [=map/exists=], set |auctionConfig|'s
[=auction config/seller timeout=] to min(|config|["{{AuctionAdConfig/sellerTimeout}}"], 500)
milliseconds.
1. If |config|["{{AuctionAdConfig/sellerExperimentGroupId}}"] [=map/exists=]:
1. Set |auctionConfig|'s [=auction config/seller experiment group id=] to
|config|["{{AuctionAdConfig/sellerExperimentGroupId}}"].
1. If |config|["{{AuctionAdConfig/perBuyerSignals}}"] [=map/exists=], [=map/for each=] |key| →
|value| of |config|["{{AuctionAdConfig/perBuyerSignals}}"]:
1. Let |buyer| be the result of [=parsing an origin=] with |key|. If |buyer| is failure,
[=exception/throw=] a {{TypeError}}.
1. Let |signalsString| be the result of [=serializing a JavaScript value to a JSON string=], given
|value|.
1. [=map/Set=] |auctionConfig|'s [=auction config/per buyer signals=][|buyer|] to
|signalsString|.
1. If |config|["{{AuctionAdConfig/perBuyerTimeouts}}"] [=map/exists=], [=map/for each=] |key| →
|value| of |config|["{{AuctionAdConfig/perBuyerTimeouts}}"]:
1. If |key| equals to "*", then set |auctionConfig|'s [=auction config/all buyers timeout=]
to min(|value|, 500) milliseconds, and [=iteration/continue=].
1. Let |buyer| the result of [=parsing an origin=] with |key|. If |buyer| is failure,
[=exception/throw=] a {{TypeError}}.
1. [=map/Set=] |auctionConfig|'s [=auction config/per buyer timeouts=][|buyer|] to
min(|value|, 500) milliseconds.
1. If |config|["{{AuctionAdConfig/perBuyerGroupLimits}}"] [=map/exists=], [=map/for each=]
|key| → |value| of |config|["{{AuctionAdConfig/perBuyerGroupLimits}}"]:
1. If |value| is 0, [=exception/throw=] a {{TypeError}}.
1. If |key| equals to "*", then set |auctionConfig|'s [=auction config/all buyers group limit=]
to |value|, and [=iteration/continue=].
1. Let |buyer| be the result of [=parsing an origin=] with |key|. If |buyer| is failure,
[=exception/throw=] a {{TypeError}}.
1. [=map/Set=] |auctionConfig|'s [=auction config/per buyer group limits=][|buyer|] to |value|.
1. If |config|["{{AuctionAdConfig/perBuyerExperimentGroupIds}}"] [=map/exists=], [=map/for each=]
|key| → |value| of |config|["{{AuctionAdConfig/perBuyerExperimentGroupIds}}"]:
1. If |key| equals to "*", then set |auctionConfig|'s
[=auction config/all buyer experiment group id=] to |value|, and [=iteration/continue=].
1. Let |buyer| the result of [=parsing an origin=] with |key|. If |buyer| is failure,
[=exception/throw=] a {{TypeError}}.
1. [=map/Set=] |auctionConfig|'s [=auction config/per buyer experiment group ids=][|buyer|] to
|value|.
1. If |config|["{{AuctionAdConfig/perBuyerPrioritySignals}}"] [=map/exists=], [=map/for each=]
|key| → |value| of |config|["{{AuctionAdConfig/perBuyerPrioritySignals}}"]:
1. Let |signals| be an [=ordered map=] whose [=map/keys=] are [=strings=] and whose [=map/values=]
are {{double}}.
1. [=map/for each=] |k| → |v| of |value|:
1. If |k| [=string/starts with=] "browserSignals.", [=exception/throw=] a {{TypeError}}.
1. [=map/Set=] |signals|[|k|] to |v|.
1. If |key| equals to "*", then set |auctionConfig|'s
[=auction config/all buyers priority signals=] to |value|, and [=iteration/continue=].
1. Let |buyer| be the result of [=parsing an origin=] with |key|. If it fails, [=exception/throw=]
a {{TypeError}}.
1. [=map/Set=] |auctionConfig|'s [=auction config/per buyer priority signals=][|buyer|] to
|signals|.
1. [=list/For each=] |component| in |config|["{{AuctionAdConfig/componentAuctions}}"]:
1. If |isTopLevel| is false, [=exception/throw=] a {{TypeError}}.
1. Let |componentAuction| be the result of running [=validate and convert auction ad config=] with
|component| and [=validate and convert auction ad config/isTopLevel=] set to false.
1. [=list/Append=] |componentAuction| to |auctionConfig|'s [=auction config/component auctions=].
1. Return |auctionConfig|.
</div>
<div algorithm>
To <dfn>parse an origin</dfn> given a [=string=] |input|:
1. Let |url| be the result of running the [=URL parser=] on |input|.
1. If |url| is failure, then return failure.
1. Return |url|'s [=url/origin=].
</div>
<div algorithm>
To <dfn>build bid generators map</dfn> given an [=auction config=] |auctionConfig|:
1. Let |bidGenerators| be a new [=ordered map=] whose [=map/keys=] are [=origins=] and whose
[=map/values=] are [=per buyer bid generators=].
1. [=list/For each=] |buyer| in |auctionConfig|'s [=auction config/interest group buyers=]:
1. [=list/For each=] |ig| of the user agent's [=interest group set=] whose
[=interest group/owner=] is |buyer|:
1. Let |signalsUrl| be |ig|'s [=interest group/trusted bidding signals url=].
1. Let |joiningOrigin| be |ig|'s [=interest group/joining origin=].
1. If |bidGenerators| does not [=map/contain=] |buyer|:
1. Let |perBuyerGenerator| be a new [=per buyer bid generator=].
1. Let |perSignalsUrlGenerator| be a new [=per signals url bid generator=].
1. [=map/Set=] |perSignalsUrlGenerator|[|joiningOrigin|] to « |ig| ».
1. [=map/Set=] |perBuyerGenerator|[|signalsUrl|] to |perSignalsUrlGenerator|.
1. [=map/Set=] |bidGenerators|[|buyer|] to |perBuyerGenerator|.
1. TODO: add a perBiddingScriptUrlGenerator layer that replaces the list of IGs with a map
from biddingScriptUrl to a list of IGs.
1. Otherwise:
1. Let |perBuyerGenerator| be |bidGenerators|[|buyer|].
1. If |perBuyerGenerator| does not [=map/contain=] |signalsUrl|:
1. Let |perSignalsUrlGenerator| be a new [=per signals url bid generator=].
1. [=map/Set=] |perSignalsUrlGenerator|[|joiningOrigin|] to « |ig| ».
1. [=map/Set=] |perBuyerGenerator|[|signalsUrl|] to |perSignalsUrlGenerator|.
1. Otherwise:
1. Let |perSignalsUrlGenerator| be |perBuyerGenerator|[|signalsUrl|].
1. If |perSignalsUrlGenerator| does not [=map/contain=] |joiningOrigin|:
1. [=map/Set=] |perSignalsUrlGenerator|[|joiningOrigin|] to « |ig| ».
1. Otherwise:
1. [=list/Append=] |ig| to |perSignalsUrlGenerator|[|joiningOrigin|].
1. Return |bidGenerators|.
</div>
<div algorithm="generate and score bids">
To <dfn>generate and score bids</dfn> given an [=auction config=] |auctionConfig|, an
[=auction config=]-or-null |topLevelAuctionConfig|, a [=global object=] |global|, and an
[=environment settings object=] |settings|:
1. [=Assert=] that these steps are running [=in parallel=].
1. Let |decisionLogicScript| be the result of [=fetching script=] with |auctionConfig|'s
[=auction config/decision logic url=].
1. If |decisionLogicScript| is failure, return null.
1. Let |bidGenerators| be the result of running [=build bid generators map=] with |auctionConfig|.
1. Let |leadingBidInfo| be a new [=leading bid info=].
1. Let |queue| be the result of [=starting a new parallel queue=].
1. If |auctionConfig|'s [=auction config/component auctions=] are not [=list/is empty|empty=]:
1. [=Assert=] |topLevelAuctionConfig| is null.
1. Let |pendingComponentAuctions| be the [=list/size=] of |auctionConfig|'s
[=auction config/component auctions=].
1. [=list/For each=] |component| in |auctionConfig|'s [=auction config/component auctions=],
[=parallel queue/enqueue steps|enqueue the following steps=] to |queue|:
1. Let |compWinner| be the result of running [=generate and score bids=] with |component|,
|auctionConfig|, and |global|.
1. If |compWinner| is failure, return failure.
1. If |compWinner| is not null:
1. Run [=score and rank a bid=] with |auctionConfig|, |compWinner|, |leadingBidInfo|,
|decisionLogicScript|, null, true and |settings|'s [=environment/top-level origin=].
1. Decrement |pendingComponentAuctions| by 1.
1. Wait until |pendingComponentAuctions| is 0.
1. If |leadingBidInfo|'s [=leading bid info/leading bid=] is null, return null.
1. Let |winningComponentConfig| be |leadingBidInfo|'s [=leading bid info/auction config=].
1. Set |leadingBidInfo|'s [=leading bid info/auction config=] to |auctionConfig|.
1. Set |leadingBidInfo|'s [=leading bid info/component seller=] to |winningComponentConfig|'s
[=auction config/seller=].
1. Let « |topLevelSellerSignals|, unusedTopLevelReportResultBrowserSignals » be the result of
running [=report result=] with |leadingBidInfo|.
1. Set |leadingBidInfo|'s [=leading bid info/auction config=] to |winningComponentConfig|.
1. Set |leadingBidInfo|'s [=leading bid info/component seller=] to null.
1. Set |leadingBidInfo|'s [=leading bid info/top level seller=] to |auctionConfig|'s
[=auction config/seller=].
1. Set |leadingBidInfo|'s [=leading bid info/top level seller signals=] to
|topLevelSellerSignals|.
1. Let « |sellerSignals|, |reportResultBrowserSignals| » be the result of running
[=report result=] with |leadingBidInfo|.
1. Run [=report win=] with |leadingBidInfo|, |sellerSignals|, and |reportResultBrowserSignals|.
1. Return |leadingBidInfo|'s [=leading bid info/leading bid=].
1. Let |allBuyersExperimentGroupId| be |auctionConfig|'s
[=auction config/all buyer experiment group id=].
1. Let |allBuyersGroupLimit| be |auctionConfig|'s
[=auction config/all buyers group limit=].
1. Wait until |auctionConfig|'s [=auction config/pending promise count=] is 0.
1. Let |auctionSignals| be |auctionConfig|'s [=auction config/auction signals=].
1. [=Assert=] |auctionSignals| and |auctionConfig|'s [=auction config/seller signals=] are not {{Promise}}s.
1. If |auctionSignals| or |auctionConfig|'s [=auction config/seller signals=] is failure, return failure.
1. Let |browserSignals| be an [=ordered map=] whose [=map/keys=] are [=strings=] and whose
[=map/values=] are {{any}}.
1. [=map/Set=] |browserSignals|["`topWindowHostname`"] to [=this=]'s [=relevant settings object=]'s
[=environment/top-level origin=]'s [=origin/host=].
1. [=map/Set=] |browserSignals|["seller"] to |auctionConfig|'s [=auction config/seller=].
1. Let |isComponentAuction| be false.
1. If |topLevelAuctionConfig| is not null:
1. [=map/Set=] |browserSignals|["topLevelSeller"] to the
[=serialization of an origin|serialization=] of |topLevelAuctionConfig|'s
[=auction config/seller=].
1. Set |isComponentAuction| to true.
1. Let |pendingBuyers| be the [=map/size=] of |bidGenerators|.
1. [=map/For each=] |buyer| → |perBuyerGenerator| of |bidGenerators|,
[=parallel queue/enqueue steps|enqueue the following steps=] to |queue|:
1. Let |buyerExperimentGroupId| be |allBuyersExperimentGroupId|.
1. Let |perBuyerExperimentGroupIds| be |auctionConfig|'s
[=auction config/per buyer experiment group ids=].
1. If |perBuyerExperimentGroupIds| is not null and |perBuyerExperimentGroupIds|[|buyer|]
[=map/exists=]:
1. Set |buyerExperimentGroupId| to |perBuyerExperimentGroupIds|[|buyer|].
1. <dfn>Apply interest groups limits to prioritized list</dfn>:
1. Let |buyerGroupLimit| be |allBuyersGroupLimit|.
1. Let |perBuyerGroupLimits| be |auctionConfig|'s
[=auction config/per buyer group limits=].
1. If |perBuyerGroupLimits| is not null and |perBuyerGroupLimits|[|buyer|] exists:
1. Set |buyerGroupLimit| to |perBuyerGroupLimits|[|buyer|].
1. Let |igs| be a new [=list=] of [=interest groups=].
1. [=map/For each=] signalsUrl → |perSignalsUrlGenerator| of |perBuyerGenerator|:
1. [=map/For each=] joiningOrigin → |groups| of |perSignalsUrlGenerator|:
1. [=list/Extend=] |igs| with |groups|.
1. [=list/Sort in descending order=] |igs|, with |a| being less than |b| if |a|'s
[=interest group/priority=] is less than |b|'s [=interest group/priority=].
1. [=list/Remove=] the first |buyerGroupLimit| items from |igs|.
1. [=map/For each=] signalsUrl → |perSignalsUrlGenerator| of |perBuyerGenerator|:
1. [=map/For each=] joiningOrigin → |groups| of |perSignalsUrlGenerator|:
1. [=list/Remove=] from |groups| any [=interest group=] [=list/contained=] in |igs|.
1. Let |perBuyerSignals| be null.
1. If |auctionConfig|'s [=auction config/per buyer signals=] is not null and
[=auction config/per buyer signals=][|buyer|] [=map/exists=]:
1. Set |perBuyerSignals| to |auctionConfig|'s [=auction config/per buyer signals=][|buyer|].
1. Let |perBuyerTimeout| be |auctionConfig|'s [=auction config/all buyers timeout=].
1. If |auctionConfig|'s [=auction config/per buyer timeouts=] is not null and
[=auction config/per buyer timeouts=][|buyer|] [=map/exists=]:
1. Set |perBuyerTimeout| to |auctionConfig|'s [=auction config/per buyer timeouts=][|buyer|].
1. [=map/For each=] |signalsUrl| → |perSignalsUrlGenerator| of |perBuyerGenerator|:
1. Let |keys| be a new [=ordered set=].
1. Let |igNames| be a new [=ordered set=].
1. [=map/For each=] joiningOrigin → |groups| of |perSignalsUrlGenerator|:
1. [=list/For each=] |ig| of |groups|:
1. [=set/Append=] |ig|'s [=interest group/trusted bidding signals keys=] to |keys|.
1. [=set/Append=] |ig|'s [=interest group/name=] to |igNames|.
1. Let |biddingSignalsUrl| be the result of [=building trusted bidding signals url=] with
|signalsUrl|, |keys|, |igNames|, |buyerExperimentGroupId|.
1. Let « |allTrustedBiddingSignals|, |dataVersion| » be the result of [=fetching trusted signals=]
with |biddingSignalsUrl| and true.
1. [=map/For each=] joiningOrigin → |groups| of |perSignalsUrlGenerator|:
1. [=list/For each=] |ig| of |groups|:
1. TODO: If rerunning `generateBid()` with only k-anonymous ads, remember to swap out |ig|'s ads
to just k-anonymous ones so that [=validating an ad url=] excludes the non-k-anonymous ones.
1. TODO: Let |interestGroup| be ... from |ig| ... minus priority and prioritySignalsOverrides and any browser-defined pieces
1. [=map/Set=] |browserSignals|["`joinCount`"] to the sum of |ig|'s
[=interest group/join counts=] for all days within the last 30 days.
1. [=map/Set=] |browserSignals|["`bidCount`"] to the sum of |ig|'s
[=interest group/bid counts=] for all days within the last 30 days.
1. [=map/Set=] |browserSignals|["`prevWins`"] to a [=list=] containing [=tuples=] of the
time and the corresponding winning [=interest group ad=] from |ig|'s
[=interest group/previous wins=] field with a data within the last 30 days. The time
field is specified in seconds relative to the start of the auction.
1. If |dataVersion| is not null:
1. [=map/Set=] |browserSignals|["`dataVersion`"] to |dataVersion|.
1. Let |biddingScript| be the result of [=fetching script=] with |ig|'s
[=interest group/bidding url=].
1. If |biddingScript| is failure, [=iteration/continue=].
1. If |ig|'s [=interest group/bidding wasm helper url=] is not null:
1. Let |wasmModuleObject| be the result of [=fetching WebAssembly=] with |ig|'s
[=interest group/bidding wasm helper url=].
1. If |wasmModuleObject| is not failure:
1. [=map/Set=] |browserSignals|["`wasmHelper`"] to |wasmModuleObject|.
1. Let |trustedBiddingSignals| be an [=ordered map=] whose [=map/keys=] are [=strings=] and
whose [=map/values=] are {{any}}.
1. [=list/For each=] |key| of |ig|'s [=interest group/trusted bidding signals keys=]:
1. If |allTrustedBiddingSignals| is an [=ordered map=] and |allTrustedBiddingSignals|[|key|]
[=map/exists=], then [=map/set=] |trustedBiddingSignals|[|key|] to
|allTrustedBiddingSignals|[|key|].
1. Let |startTime| be the current time.
1. Let |generatedBidResult| be the result of [=evaluating a bidding script=] with
|biddingScript|, |ig|, « |interestGroup|, |auctionSignals|, |perBuyerSignals|,
|trustedBiddingSignals|, |browserSignals| », and |perBuyerTimeout|.
1. Let |duration| be the current time minus |startTime| in milliseconds.
1. If |generatedBidResult| is an [=abrupt completion=], [=iteration/continue=].
1. Let |generatedBidIDL| be the result of [=converted to an IDL value|converting=]
|generatedBidResult| to a {{GenerateBidOutput}}.
1. If an exception was caught in the previous step, [=iteration/continue=].
1. Let |groupHasAdComponents| be true.
1. If |interestGroup|'s [=interest group/ad components=] is null:
1. Set |groupHasAdComponents| be false.
1. Let |generatedBid| be the result of [=converting GenerateBidOutput to generated bid=]
with |generatedBidIDL|, |ig|, |isComponentAuction|, and |groupHasAdComponents|.
1. Set |generatedBid|'s [=generated bid/bid duration=] to |duration|.
1. If |generatedBid| is failure, [=iteration/continue=].
1. Set |generatedBid|'s [=generated bid/interest group=] to |ig|.
1. [=Score and rank a bid=] with |auctionConfig|, |generatedBid|, |leadingBidInfo|,
|decisionLogicScript|, |dataVersion|, |isComponentAuction|, and |settings|'s
[=environment/top-level origin=].
1. Decrement |pendingBuyers| by 1.
1. Wait until |pendingBuyers| is 0.
1. If |leadingBidInfo|'s [=leading bid info/leading bid=] is null, return null.
1. If |topLevelAuctionConfig| is null:
1. Let « |sellerSignals|, |reportResultBrowserSignals| » be the result of running
[=report result=] with |leadingBidInfo|.
1. Run [=report win=] with |leadingBidInfo|, |sellerSignals|, and |reportResultBrowserSignals|.
1. Return |leadingBidInfo|'s [=leading bid info/leading bid=].
</div>
<div algorithm>
To <dfn>score and rank a bid</dfn> given an [=auction config=] |auctionConfig|, a [=generated bid=]
|generatedBid|, a [=leading bid info=] |leadingBidInfo|, a [=string=] |decisionLogicScript|, a
{{unsigned long}}-or-null |biddingDataVersion|, a [=boolean=] |isComponentAuction|, and an [=origin=]
|topWindowOrigin|:
1. Let |renderURLs| be a new [=set=].
1. Let |adComponentRenderUrls| be a new [=set=].
1. [=set/Append=] |generatedBid|'s [=generated bid/ad descriptor=]'s [=ad descriptor/url=] to |renderURLs|.
1. If |generatedBid|'s [=generated bid/ad component descriptors=] is not null:
1. [=set/For each=] |adComponentDescriptor| in |generatedBid|'s
[=generated bid/ad component descriptors=]:
1. [=set/Append=] |adComponentDescriptor|'s [=ad descriptor/url=] to |adComponentRenderUrls|.
1. Let |fullSignalsUrl| be the result of [=building trusted scoring signals url=] with |auctionConfig|'s
[=auction config/trusted scoring signals url=], |renderURLs|, |adComponentRenderUrls|,
|auctionConfig|'s [=auction config/seller experiment group id=], and |topWindowOrigin|.
Implementations may batch requests by collecting render URLs and ad component render URLs
from multiple invocations of [=score and rank a bid=] and passing them all to a single invocation
of [=building trusted scoring signals url=] -- the network response has to be parsed to pull out the pieces
relevant to each [=evaluating a scoring script|evaluation of a scoring script=].
1. Let |trustedScoringSignals| be null.
1. Let «|allTrustedScoringSignals|, |scoringDataVersion|» be the result of [=fetching trusted signals=] with
|fullSignalsUrl| and false.
1. If |allTrustedScoringSignals| is an [=ordered map=]:
1. Let |trustedScoringSignals| be a new [=map=].
1. [=Assert=]: |renderURLs|'s [=set/size=] is 1.
1. [=set/For each=] |renderURL| in |renderURLs|:
1. If |allTrustedScoringSignals|["`renderURLs`"] [=map/exists=] and
|allTrustedScoringSignals|["`renderURLs`"][|renderURL|] [=map/exists=]:
1. Let |renderURLValue| be a new [=map=].
1. [=map/Set=] |renderURLValue|[|renderURL|] to |allTrustedScoringSignals|["`renderURLs`"][|renderURL|].
1. [=map/Set=] |trustedScoringSignals|["`renderURL`"] to |renderURLValue|.
1. Let |adComponentRenderUrlsValue| be a new [=map=].
1. [=set/For each=] |adComponentRenderUrl| in |adComponentRenderUrls|:
1. If |allTrustedScoringSignals|["`adComponentRenderUrls`"] [=map/exists=] and
|allTrustedScoringSignals|["`adComponentRenderUrls`"][|adComponentRenderUrl|] [=map/exists=]:
1. [=map/Set=] |adComponentRenderUrlsValue|[|adComponentRenderUrl|] to
|allTrustedScoringSignals|["`adComponentRenderUrls`"][|adComponentRenderUrl|].
1. If |adComponentRenderUrlsValue| is not [=map/is empty|empty=]:
1. [=map/Set=] |trustedScoringSignals|["`adComponentRenderUrls`"] to |adComponentRenderUrlsValue|.
1. Let |adMetadata| be |generatedBid|'s [=generated bid/ad=].
1. Let |bidValue| be |generatedBid|'s [=generated bid/bid=].
1. If |generatedBid|'s [=generated bid/modified bid=] is not null:
1. Set |bidValue| to |generatedBid|'s [=generated bid/modified bid=].
1. Let |owner| be |generatedBid|'s [=generated bid/interest group=]'s [=interest group/owner=].
1. Let |browserSignals| be an [=ordered map=] whose [=map/keys=] are [=strings=] and whose
[=map/values=] are {{any}}.
1. [=map/Set=] |browserSignals|["`topWindowHostname`"] to |topWindowOrigin|'s [=origin/host=].
1. [=map/Set=] |browserSignals|["interestGroupOwner"] to |owner|.
1. [=map/Set=] |browserSignals|["`renderURL`"] to |generatedBid|'s [=generated bid/ad descriptor=]'s
[=ad descriptor/url=].
1. [=map/Set=] |browserSignals|["`adComponents`"] to |generatedBid|'s
[=generated bid/ad component descriptors=].
1. [=map/Set=] |browserSignals|["`biddingDurationMsec`"] to |generatedBid|'s
[=generated bid/bid duration=].
1. If |scoringDataVersion| is not null:
1. [=map/Set=] |browserSignals|["`dataVersion`"] to |scoringDataVersion|.
1. TODO: Remove fields of |auctionConfig| that don't pass through.
1. Let |scoreAdOutput| be the result of [=evaluating a scoring script=] with
|decisionLogicScript|, « |adMetadata|, |bidValue|, |auctionConfig|, |trustedScoringSignals|,
|browserSignals| », and |auctionConfig|'s [=auction config/seller timeout=].
1. If |isComponentAuction| is true, and |scoreAdOutput|'s
[=score ad output/allow component auction=] is false, return.
1. Let |score| be |scoreAdOutput|'s [=score ad output/desirability=].
1. If |score| is negative or 0, return.
1. If |isComponentAuction| is true and |scoreAdOutput|'s [=score ad output/bid=] is not null:
1. Set |generatedBid|'s [=generated bid/modified bid=] to |scoreAdOutput|'s
[=score ad output/bid=].
1. Let |updateLeadingBid| be false.
1. If |leadingBidInfo|'s [=leading bid info/leading bid=] is null, or |score| is greater than
|leadingBidInfo|'s [=leading bid info/top score=]:
1. Set |updateLeadingBid| to true.
1. Set |leadingBidInfo|'s [=leading bid info/top bids count=] to 1.
1. Set |leadingBidInfo|'s [=leading bid info/at most one top bid owner=] to true.
1. Otherwise if |score| equals |leadingBidInfo|'s [=leading bid info/top score=]:
1. Increment |leadingBidInfo|'s [=leading bid info/top bids count=] by 1.
1. Set |updateLeadingBid| to true with 1 in |leadingBidInfo|'s [=leading bid info/top bids count=]
chance.
1. If |updateLeadingBid| is false:
1. [=Update highest scoring other bid=] with |score|, |leadingBidInfo|'s
[=leading bid info/leading bid=], and |leadingBidInfo|.
1. If |owner| is not [=same origin=] with |leadingBidInfo|'s [=leading bid info/leading bid=]'s
[=generated bid/interest group=]'s [=interest group/owner=]:
1. Set |leadingBidInfo|'s [=leading bid info/at most one top bid owner=] to false.
1. Otherwise if |score| is greater than or equal to |leadingBidInfo|'s
[=leading bid info/second highest score=]:
1. [=Update highest scoring other bid=] with |score|, |bidValue|, and |leadingBidInfo|.
1. If |updateLeadingBid| is true:
1. If |leadingBidInfo|'s [=leading bid info/leading bid=] is not null:
1. [=Update highest scoring other bid=] with |leadingBidInfo|'s [=leading bid info/top score=],
|leadingBidInfo|'s [=leading bid info/leading bid=], and |leadingBidInfo|.
1. Set |leadingBidInfo|'s [=leading bid info/top score=] to |score|.
1. Set |leadingBidInfo|'s [=leading bid info/leading bid=] to |generatedBid|.
1. Set |leadingBidInfo|'s [=leading bid info/auction config=] to |auctionConfig|.
1. Set |leadingBidInfo|'s [=leading bid info/bidding data version=] to |biddingDataVersion|.
1. Set |leadingBidInfo|'s [=leading bid info/scoring data version=] to |scoringDataVersion|.
</div>
<div algorithm>
To <dfn>update highest scoring other bid</dfn> given a {{double}} |score|, a
[=generated bid=]-or-null |bid|, and a [=leading bid info=] |leadingBidInfo|:
1. If |bid| is null, return.
1. Let |owner| be |bid|'s [=generated bid/interest group=]'s [=interest group/owner=].
1. If |score| is greater than |leadingBidInfo|'s [=leading bid info/second highest score=]:
1. Set |leadingBidInfo|'s [=leading bid info/highest scoring other bid=] to |bid|.
1. Set |leadingBidInfo|'s [=leading bid info/highest scoring other bids count=] to 1.
1. Set |leadingBidInfo|'s [=leading bid info/second highest score=] to |score|.
1. If |leadingBidInfo|'s [=leading bid info/at most one top bid owner=] is true:
1. Set |leadingBidInfo|'s [=leading bid info/highest scoring other bid owner=] to |owner|.
1. Otherwise,
1. Set |leadingBidInfo|'s [=leading bid info/highest scoring other bid owner=] to null.
1. Otherwise if |score| is equal to |leadingBidInfo|'s [=leading bid info/second highest score=]:
1. Increment |leadingBidInfo|'s [=leading bid info/highest scoring other bids count=] by 1.
1. Set |leadingBidInfo|'s [=leading bid info/highest scoring other bid=] to |bid| with 1 in
|leadingBidInfo|'s [=leading bid info/highest scoring other bids count=] chance.
1. If |leadingBidInfo|'s [=leading bid info/highest scoring other bid owner=] is not null:
1. If |owner| is not [=same origin=] with |leadingBidInfo|'s
[=leading bid info/highest scoring other bid owner=]:
1. Set |leadingBidInfo|'s [=leading bid info/highest scoring other bid owner=] to null.
</div>
<div algorithm>
To <dfn>create a request</dfn> given a [=URL=] |url|, a [=string=] |accept|, and an [=origin=] or
null |origin|:
1. Let |request| be a new [=request=] with the following properties:
: [=request/URL=]
:: |url|
: [=request/header list=]
:: A new [=header list=] containing a [=header=] named "`Accept`" whose value is |accept|
: [=request/client=]
:: `null`
: [=request/window=]
:: "`no-window`" TODO: verify
: [=request/service-workers mode=]
:: "`none`"
: [=request/origin=]
:: If |origin| is null, then [=opaque origin=], otherwise |origin|.
: [=request/referrer=]
:: "`no-referrer`"
: [=request/credentials mode=]
:: "`omit`"
: [=request/cache mode=]
:: "`no-store`"
: [=request/redirect mode=]
:: "`error`"
1. Return |request|.
</div>
<div algorithm>
To <dfn>validate fetching response</dfn> given a [=response=] |response| and |responseBody|, and a
[=string=] |mimeType|:
1. If |responseBody| is null or failure, return false.
1. If [=header list/getting a structured field value|getting=] "X-Allow-Protected-Audience" from
|response|'s [=response/header list=] does not return true, return false.
1. Let |headerMimeType| be the result of [=header list/extracting a MIME type=] from |response|'s
[=response/header list=].
1. Return false if any of the following conditions hold:
* |headerMimeType| is failure;
* |mimeType| is "`text/javascript`" and |headerMimeType| is not a [=JavaScript MIME type=];
* |mimeType| is "`application/json`" and |headerMimeType| is not a [=JSON MIME type=].
1. Let |mimeTypeCharset| be |headerMimeType|'s [=MIME type/parameters=]["`charset`"].
1. Return false if any of the following conditions hold:
* |mimeTypeCharset| does not [=map/exist=], or |mimeTypeCharset| is "utf-8", and |responseBody|
is not [=UTF-8=] encoded;
* |mimeTypeCharset| is "us-ascii", and |responseBody| is not [=ascii string=].
1. Return true.
</div>
<div algorithm>
To <dfn>fetch script</dfn> given a [=URL=] |url|:
1. Let |request| be the result of [=creating a request=] with |url|, "`text/javascript`", and
null.
1. Let |script| be null.
1. [=Fetch=] |request| with [=fetch/processResponseConsumeBody=] set to the following steps given
a [=response=] |response| and |responseBody|:
1. If [=validate fetching response=] with |response|, |responseBody| and "`text/javascript`"
returns false, set |script| to failure and return.
1. Set |script| to |responseBody|.
1. Wait for |script| to be set.
1. Return |script|.
</div>
<div algorithm>
To <dfn>fetch WebAssembly</dfn> given a [=URL=] |url|:
1. Let |request| be the result of [=creating a request=] with |url|, "`application/wasm`", and
null.
1. Let |moduleObject| be null.
1. [=Fetch=] |request| with [=fetch/processResponseConsumeBody=] set to the following steps given
a [=response=] |response| and |responseBody|:
1. Set |moduleObject| to failure and return, if any of the following conditions hold:
* |responseBody| is null or failure;
* [=header list/getting a structured field value|Getting=] "X-Allow-Protected-Audience" from
|response|'s [=response/header list=] does not return true.
1. Let |moduleObjectPromise| be the result of [=compiling a WebAssembly module=] |response|.
1. [=Upon fulfillment=] of |moduleObjectPromise| with value |module|:
1. Set |moduleObject| to |module|.
1. [=Upon rejection=] of |moduleObjectPromise|:
1. Set |moduleObject| to failure.
1. Wait for |moduleObject| to be set.
1. Return |moduleObject|.
</div>
The <dfn http-header><code>Data-Version</code></dfn> HTTP response header is a
[=structured header=] whose value must be an [=structured header/integer=].
The <dfn http-header><code>X-protected-audience-bidding-signals-format-version</code></dfn>
HTTP response header is a [=structured header=] whose value must be an [=structured header/integer=].
<div algorithm>
To <dfn>fetch trusted signals</dfn> given a [=URL=] |url|, and a [=boolean=] |isBiddingSignal|:
1. Let |request| be the result of [=creating a request=] with |url|, "`application/json`", and
null.
1. Let |signals| be null.
1. Let |dataVersion| be null.
1. Let |formatVersion| be null.
1. [=Fetch=] |request| with [=fetch/processResponseConsumeBody=] set to the following steps given
a [=response=] |response| and |responseBody|:
1. If [=validate fetching response=] with |response|, |responseBody| and "`application/json`"
returns false, set |signals| to failure and return.