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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
content / services / auction_worklet / bidder_worklet.cc [blame]
// Copyright 2021 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/services/auction_worklet/bidder_worklet.h"
#include <stdint.h>
#include <algorithm>
#include <cmath>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <vector>
#include "base/check.h"
#include "base/containers/contains.h"
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/hash/hash.h"
#include "base/memory/scoped_refptr.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "base/rand_util.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "base/task/bind_post_task.h"
#include "base/task/sequenced_task_runner.h"
#include "base/time/time.h"
#include "base/timer/elapsed_timer.h"
#include "base/trace_event/trace_event.h"
#include "base/types/optional_util.h"
#include "content/public/common/content_features.h"
#include "content/services/auction_worklet/auction_v8_helper.h"
#include "content/services/auction_worklet/auction_v8_logger.h"
#include "content/services/auction_worklet/auction_worklet_util.h"
#include "content/services/auction_worklet/bidder_lazy_filler.h"
#include "content/services/auction_worklet/deprecated_url_lazy_filler.h"
#include "content/services/auction_worklet/direct_from_seller_signals_requester.h"
#include "content/services/auction_worklet/for_debugging_only_bindings.h"
#include "content/services/auction_worklet/private_aggregation_bindings.h"
#include "content/services/auction_worklet/public/cpp/auction_network_events_delegate.h"
#include "content/services/auction_worklet/public/cpp/private_aggregation_reporting.h"
#include "content/services/auction_worklet/public/mojom/auction_worklet_service.mojom.h"
#include "content/services/auction_worklet/public/mojom/bidder_worklet.mojom-shared.h"
#include "content/services/auction_worklet/public/mojom/bidder_worklet.mojom.h"
#include "content/services/auction_worklet/public/mojom/private_aggregation_request.mojom.h"
#include "content/services/auction_worklet/public/mojom/real_time_reporting.mojom.h"
#include "content/services/auction_worklet/public/mojom/trusted_signals_cache.mojom.h"
#include "content/services/auction_worklet/real_time_reporting_bindings.h"
#include "content/services/auction_worklet/register_ad_beacon_bindings.h"
#include "content/services/auction_worklet/register_ad_macro_bindings.h"
#include "content/services/auction_worklet/report_bindings.h"
#include "content/services/auction_worklet/set_bid_bindings.h"
#include "content/services/auction_worklet/set_priority_bindings.h"
#include "content/services/auction_worklet/set_priority_signals_override_bindings.h"
#include "content/services/auction_worklet/shared_storage_bindings.h"
#include "content/services/auction_worklet/trusted_signals.h"
#include "content/services/auction_worklet/trusted_signals_kvv2_manager.h"
#include "content/services/auction_worklet/trusted_signals_request_manager.h"
#include "content/services/auction_worklet/worklet_loader.h"
#include "content/services/auction_worklet/worklet_util.h"
#include "context_recycler.h"
#include "gin/converter.h"
#include "gin/dictionary.h"
#include "mojo/public/cpp/bindings/associated_remote.h"
#include "mojo/public/cpp/bindings/pending_associated_remote.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "mojo/public/cpp/bindings/struct_ptr.h"
#include "services/network/public/mojom/url_loader_factory.mojom.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/common/interest_group/ad_auction_constants.h"
#include "third_party/blink/public/common/interest_group/ad_auction_currencies.h"
#include "third_party/blink/public/common/interest_group/ad_display_size.h"
#include "third_party/blink/public/common/interest_group/ad_display_size_utils.h"
#include "third_party/blink/public/mojom/interest_group/interest_group_types.mojom.h"
#include "url/gurl.h"
#include "url/origin.h"
#include "v8-statistics.h"
#include "v8/include/v8-container.h"
#include "v8/include/v8-context.h"
#include "v8/include/v8-exception.h"
#include "v8/include/v8-forward.h"
#include "v8/include/v8-object.h"
#include "v8/include/v8-primitive.h"
#include "v8/include/v8-template.h"
#include "v8/include/v8-wasm.h"
namespace auction_worklet {
namespace {
class DeepFreezeAllowAll : public v8::Context::DeepFreezeDelegate {
public:
bool FreezeEmbedderObjectAndGetChildren(
v8::Local<v8::Object> obj,
v8::LocalVector<v8::Object>& children_out) override {
return true;
}
};
bool AppendJsonValueOrNull(AuctionV8Helper* const v8_helper,
v8::Local<v8::Context> context,
const std::string* maybe_json,
v8::LocalVector<v8::Value>* args) {
v8::Isolate* isolate = v8_helper->isolate();
if (maybe_json) {
if (!v8_helper->AppendJsonValue(context, *maybe_json, args)) {
return false;
}
} else {
args->push_back(v8::Null(isolate));
}
return true;
}
// Checks both types of DirectFromSellerSignals results (subresource bundle
// based and header based) -- at most one of these should be non-null.
//
// Returns the V8 conversion of the in-use version of DirectFromSellerSignals,
// or v8::Null() if both types of DirectFromSellerSignals are null.
v8::Local<v8::Value> GetDirectFromSellerSignals(
const DirectFromSellerSignalsRequester::Result& subresource_bundle_result,
const std::optional<std::string>& header_result,
AuctionV8Helper& v8_helper,
v8::Local<v8::Context> context,
std::vector<std::string>& errors) {
CHECK(subresource_bundle_result.IsNull() || !header_result);
if (header_result) {
// `header_result` JSON was validated, parsed and reconstructed into a
// string by the browser process, so CHECK it is valid JSON.
return v8_helper.CreateValueFromJson(context, *header_result)
.ToLocalChecked();
}
return subresource_bundle_result.GetSignals(v8_helper, context, errors);
}
std::optional<base::TimeDelta> NullOptIfZero(base::TimeDelta delta) {
if (delta.is_zero()) {
return std::nullopt;
}
return delta;
}
// Adjust `bid` to meet `target_num_ad_components`, if any.
void TrimExtraAdComponents(mojom::BidderWorkletBid& bid,
std::optional<size_t> target_num_ad_components) {
if (!target_num_ad_components.has_value()) {
return;
}
// SetBidBindings should have enforced that there are enough adComponents,
// as should have HandleComponentsKAnon() when bifurcating bids,
// which also implies they exist.
DCHECK(bid.ad_component_descriptors.has_value());
DCHECK_LE(*target_num_ad_components, bid.ad_component_descriptors->size());
bid.ad_component_descriptors->resize(*target_num_ad_components);
}
// Applies `target_num_ad_components` to `bid` and appends it to `out`.
void TrimAndCollectBid(mojom::BidderWorkletBidPtr bid,
std::optional<size_t> target_num_ad_components,
std::vector<mojom::BidderWorkletBidPtr>& out) {
TrimExtraAdComponents(*bid, target_num_ad_components);
out.push_back(std::move(bid));
}
// Given `bid` that has k-anonymous main ad and some component ads, handles the
// k-anon check on its components, possibly dropping some if allowed. Appends
// the resulting bid (and perhaps a non-k-anon alternative) to `out`.
void HandleComponentsKAnon(
const mojom::BidderWorkletNonSharedParams* bidder_worklet_non_shared_params,
mojom::BidderWorkletBidPtr bid,
std::optional<size_t> target_num_ad_components,
size_t num_mandatory_ad_components,
std::vector<mojom::BidderWorkletBidPtr>& out) {
size_t num_required_components =
target_num_ad_components.value_or(bid->ad_component_descriptors->size());
// Go through the ad component list and try to collect
// `num_required_components` k-anonymous ones into
// `usable_ad_component_indices`. Gives up if a mandatory ad component isn't
// k-anonymous. Sets `saw_non_k_anon` to true if it needed to skip over any
// non-k-anonymous ones.
std::vector<size_t> usable_ad_component_indices;
usable_ad_component_indices.reserve(num_required_components);
bool saw_non_k_anon = false;
for (size_t i = 0; i < bid->ad_component_descriptors->size(); ++i) {
const blink::AdDescriptor& ad_component_descriptor =
bid->ad_component_descriptors.value()[i];
if (BidderWorklet::IsComponentAdKAnon(bidder_worklet_non_shared_params,
ad_component_descriptor)) {
usable_ad_component_indices.push_back(i);
if (usable_ad_component_indices.size() == num_required_components) {
break;
}
} else {
saw_non_k_anon = true;
if (i < num_mandatory_ad_components) {
// One of the required component ads is not k-anon, so have to give up
// on this.
break;
}
}
}
DCHECK_LE(usable_ad_component_indices.size(), num_required_components);
if (usable_ad_component_indices.size() == num_required_components) {
if (!saw_non_k_anon) {
// The bid was actually completely fine without getting fancy.
bid->bid_role = mojom::BidRole::kBothKAnonModes;
TrimAndCollectBid(std::move(bid), target_num_ad_components, out);
} else {
// Split the bid into two, with one having just the usable ad components.
mojom::BidderWorkletBidPtr non_kanon_alternative = bid->Clone();
DCHECK_EQ(non_kanon_alternative->bid_role,
mojom::BidRole::kUnenforcedKAnon);
bid->bid_role = mojom::BidRole::kEnforcedKAnon;
std::vector<blink::AdDescriptor> usable_ad_components;
usable_ad_components.reserve(num_required_components);
for (size_t index : usable_ad_component_indices) {
usable_ad_components.push_back(
std::move(bid->ad_component_descriptors.value()[index]));
}
bid->ad_component_descriptors = std::move(usable_ad_components);
TrimAndCollectBid(std::move(bid), target_num_ad_components, out);
TrimAndCollectBid(std::move(non_kanon_alternative),
target_num_ad_components, out);
}
} else {
// Could not salvage the bid; just drop any extra component ads and mark it
// as non-k-anon.
DCHECK_EQ(bid->bid_role, mojom::BidRole::kUnenforcedKAnon);
TrimAndCollectBid(std::move(bid), target_num_ad_components, out);
}
}
std::vector<mojom::BidderWorkletBidPtr> ClassifyBidsAndApplyComponentAdLimits(
mojom::KAnonymityBidMode kanon_mode,
const mojom::BidderWorkletNonSharedParams* bidder_worklet_non_shared_params,
const GURL& script_source_url,
std::vector<SetBidBindings::BidAndWorkletOnlyMetadata> bid_info) {
std::vector<mojom::BidderWorkletBidPtr> bids;
for (auto& candidate : bid_info) {
if (kanon_mode == mojom::KAnonymityBidMode::kNone ||
!BidderWorklet::IsMainAdKAnon(bidder_worklet_non_shared_params,
script_source_url, candidate)) {
DCHECK_EQ(candidate.bid->bid_role, mojom::BidRole::kUnenforcedKAnon);
TrimAndCollectBid(std::move(candidate.bid),
candidate.target_num_ad_components, bids);
} else {
// We care about k-anonymity, and whether the bid is k-anonymous or not
// depends on whether component ads are k-anonymous; we also may be able
// to salvage the bid by throwing out some components while trying to get
// it to the target.
if (!candidate.bid->ad_component_descriptors.has_value() ||
candidate.bid->ad_component_descriptors->empty()) {
// There are no components to worry about, so it's k-anon.
candidate.bid->bid_role = mojom::BidRole::kBothKAnonModes;
bids.push_back(std::move(candidate.bid));
} else {
HandleComponentsKAnon(bidder_worklet_non_shared_params,
std::move(candidate.bid),
candidate.target_num_ad_components,
candidate.num_mandatory_ad_components, bids);
}
}
}
return bids;
}
size_t GetNumberOfGroupByOriginContextsToKeep() {
if (base::FeatureList::IsEnabled(
blink::features::
kFledgeNumberBidderWorkletGroupByOriginContextsToKeep)) {
// Avoid using multiple contexts for the testing population
// unless otherwise specified by
// kFledgeNumberBidderWorkletContextsIncludeFacilitedTesting.
if (blink::features::
kFledgeNumberBidderWorkletContextsIncludeFacilitedTesting.Get() ||
!base::FeatureList::IsEnabled(
features::kCookieDeprecationFacilitatedTesting)) {
return blink::features::
kFledgeNumberBidderWorkletGroupByOriginContextsToKeepValue.Get();
}
}
return 1;
}
// Check if trusted bidding signals, if any, are same-origin or cross-origin.
BidderWorklet::SignalsOriginRelation ClassifyTrustedSignals(
const GURL& script_source_url,
const std::optional<url::Origin>& trusted_bidding_signals_origin) {
if (!trusted_bidding_signals_origin) {
return BidderWorklet::SignalsOriginRelation::kNoTrustedSignals;
}
if (trusted_bidding_signals_origin->IsSameOriginWith(script_source_url)) {
return BidderWorklet::SignalsOriginRelation::kSameOriginSignals;
}
return BidderWorklet::SignalsOriginRelation::kCrossOriginSignals;
}
// Sets the appropriate field (if any) of `browser_signals_dict` to data
// version. Returns success/failure.
bool SetDataVersion(
BidderWorklet::SignalsOriginRelation trusted_signals_relation,
std::optional<uint32_t> bidding_signals_data_version,
gin::Dictionary& browser_signals_dict) {
if (!bidding_signals_data_version.has_value()) {
return true;
}
switch (trusted_signals_relation) {
case BidderWorklet::SignalsOriginRelation::kNoTrustedSignals:
return true;
case BidderWorklet::SignalsOriginRelation::kSameOriginSignals:
return browser_signals_dict.Set("dataVersion",
bidding_signals_data_version.value());
case BidderWorklet::SignalsOriginRelation::kCrossOriginSignals:
return browser_signals_dict.Set("crossOriginDataVersion",
bidding_signals_data_version.value());
}
}
} // namespace
BidderWorklet::BidderWorklet(
std::vector<scoped_refptr<AuctionV8Helper>> v8_helpers,
std::vector<mojo::PendingRemote<mojom::AuctionSharedStorageHost>>
shared_storage_hosts,
bool pause_for_debugger_on_start,
mojo::PendingRemote<network::mojom::URLLoaderFactory>
pending_url_loader_factory,
mojo::PendingRemote<auction_worklet::mojom::AuctionNetworkEventsHandler>
auction_network_events_handler,
TrustedSignalsKVv2Manager* trusted_signals_kvv2_manager,
const GURL& script_source_url,
const std::optional<GURL>& wasm_helper_url,
const std::optional<GURL>& trusted_bidding_signals_url,
const std::string& trusted_bidding_signals_slot_size_param,
const url::Origin& top_window_origin,
mojom::AuctionWorkletPermissionsPolicyStatePtr permissions_policy_state,
std::optional<uint16_t> experiment_group_id,
mojom::TrustedSignalsPublicKeyPtr public_key)
: join_origin_hash_salt_(base::NumberToString(base::RandUint64())),
url_loader_factory_(std::move(pending_url_loader_factory)),
trusted_signals_kvv2_manager_(trusted_signals_kvv2_manager),
script_source_url_(script_source_url),
wasm_helper_url_(wasm_helper_url),
top_window_origin_(top_window_origin),
auction_network_events_handler_(
std::move(auction_network_events_handler)) {
DCHECK_CALLED_ON_VALID_SEQUENCE(user_sequence_checker_);
DCHECK(!v8_helpers.empty());
DCHECK_EQ(v8_helpers.size(), shared_storage_hosts.size());
for (size_t i = 0; i < v8_helpers.size(); ++i) {
v8_runners_.push_back(v8_helpers[i]->v8_runner());
v8_helpers_.push_back(std::move(v8_helpers[i]));
debug_ids_.push_back(
base::MakeRefCounted<AuctionV8Helper::DebugId>(v8_helpers_[i].get()));
v8_state_.push_back(std::unique_ptr<V8State, base::OnTaskRunnerDeleter>(
new V8State(v8_helpers_[i], debug_ids_[i],
std::move(shared_storage_hosts[i]), script_source_url_,
top_window_origin_, permissions_policy_state->Clone(),
wasm_helper_url_, trusted_bidding_signals_url,
weak_ptr_factory_.GetWeakPtr()),
base::OnTaskRunnerDeleter(v8_runners_[i])));
}
trusted_signals_request_manager_ =
(trusted_bidding_signals_url
? std::make_unique<TrustedSignalsRequestManager>(
TrustedSignalsRequestManager::Type::kBiddingSignals,
url_loader_factory_.get(),
/*auction_network_events_handler=*/
CreateNewAuctionNetworkEventsHandlerRemote(
auction_network_events_handler_),
/*automatically_send_requests=*/false, top_window_origin,
*trusted_bidding_signals_url, experiment_group_id,
trusted_bidding_signals_slot_size_param, std::move(public_key),
v8_helpers_[GetNextThreadIndex()].get())
: nullptr);
paused_ = pause_for_debugger_on_start;
if (!paused_) {
Start();
}
}
BidderWorklet::~BidderWorklet() {
DCHECK_CALLED_ON_VALID_SEQUENCE(user_sequence_checker_);
for (const auto& debug_id : debug_ids_) {
debug_id->AbortDebuggerPauses();
}
}
std::vector<int> BidderWorklet::context_group_ids_for_testing() const {
std::vector<int> results;
for (const auto& debug_id : debug_ids_) {
results.push_back(debug_id->context_group_id());
}
return results;
}
size_t BidderWorklet::GetNextThreadIndex() {
size_t result = next_thread_index_;
next_thread_index_++;
next_thread_index_ %= v8_helpers_.size();
return result;
}
// static
bool BidderWorklet::IsKAnon(
const mojom::BidderWorkletNonSharedParams* bidder_worklet_non_shared_params,
const std::string& key) {
return std::find(bidder_worklet_non_shared_params->kanon_keys.begin(),
bidder_worklet_non_shared_params->kanon_keys.end(),
mojom::KAnonKey::New(key)) !=
bidder_worklet_non_shared_params->kanon_keys.end();
}
// static
bool BidderWorklet::IsMainAdKAnon(
const mojom::BidderWorkletNonSharedParams* bidder_worklet_non_shared_params,
const GURL& script_source_url,
const SetBidBindings::BidAndWorkletOnlyMetadata& bid_and_metadata) {
const mojom::BidderWorkletBidPtr& bid = bid_and_metadata.bid;
if (!bid) {
return true;
}
url::Origin owner = url::Origin::Create(script_source_url);
if (!BidderWorklet::IsKAnon(
bidder_worklet_non_shared_params,
blink::HashedKAnonKeyForAdBid(owner, script_source_url,
bid->ad_descriptor.url.spec()))) {
return false;
}
if (bid->selected_buyer_and_seller_reporting_id.has_value()) {
if (!BidderWorklet::IsKAnon(
bidder_worklet_non_shared_params,
blink::HashedKAnonKeyForAdNameReportingWithoutInterestGroup(
/*interest_group_owner=*/owner,
/*interest_group_name=*/bidder_worklet_non_shared_params->name,
/*interest_group_bidding_url=*/script_source_url,
/*ad_render_url=*/bid->ad_descriptor.url.spec(),
bid_and_metadata.buyer_reporting_id,
bid_and_metadata.buyer_and_seller_reporting_id,
bid_and_metadata.bid
->selected_buyer_and_seller_reporting_id))) {
return false;
}
}
return true;
}
// static
bool BidderWorklet::IsComponentAdKAnon(
const mojom::BidderWorkletNonSharedParams* bidder_worklet_non_shared_params,
const blink::AdDescriptor& ad_component_descriptor) {
return IsKAnon(
bidder_worklet_non_shared_params,
blink::HashedKAnonKeyForAdComponentBid(ad_component_descriptor));
}
// static
bool BidderWorklet::SupportMultiBid() {
// Multi-bid is auto-disabled in mode-A/B trials.
return base::FeatureList::IsEnabled(blink::features::kFledgeMultiBid) &&
!base::FeatureList::IsEnabled(
features::kCookieDeprecationFacilitatedTesting);
}
void BidderWorklet::BeginGenerateBid(
mojom::BidderWorkletNonSharedParamsPtr bidder_worklet_non_shared_params,
mojom::TrustedSignalsCacheKeyPtr trusted_signals_cache_key,
mojom::KAnonymityBidMode kanon_mode,
const url::Origin& interest_group_join_origin,
const std::optional<GURL>& direct_from_seller_per_buyer_signals,
const std::optional<GURL>& direct_from_seller_auction_signals,
const url::Origin& browser_signal_seller_origin,
const std::optional<url::Origin>& browser_signal_top_level_seller_origin,
const base::TimeDelta browser_signal_recency,
blink::mojom::BiddingBrowserSignalsPtr bidding_browser_signals,
base::Time auction_start_time,
const std::optional<blink::AdSize>& requested_ad_size,
uint16_t multi_bid_limit,
uint64_t trace_id,
mojo::PendingAssociatedRemote<mojom::GenerateBidClient> generate_bid_client,
mojo::PendingAssociatedReceiver<mojom::GenerateBidFinalizer>
bid_finalizer) {
DCHECK_CALLED_ON_VALID_SEQUENCE(user_sequence_checker_);
generate_bid_tasks_.emplace_front();
auto generate_bid_task = generate_bid_tasks_.begin();
generate_bid_task->bidder_worklet_non_shared_params =
std::move(bidder_worklet_non_shared_params);
generate_bid_task->kanon_mode = kanon_mode;
generate_bid_task->interest_group_join_origin = interest_group_join_origin;
generate_bid_task->browser_signal_seller_origin =
browser_signal_seller_origin;
generate_bid_task->browser_signal_top_level_seller_origin =
browser_signal_top_level_seller_origin;
generate_bid_task->browser_signal_recency = browser_signal_recency;
generate_bid_task->bidding_browser_signals =
std::move(bidding_browser_signals);
generate_bid_task->auction_start_time = auction_start_time;
generate_bid_task->requested_ad_size = requested_ad_size;
generate_bid_task->multi_bid_limit = multi_bid_limit;
generate_bid_task->trace_id = trace_id;
generate_bid_task->generate_bid_client.Bind(std::move(generate_bid_client));
// Deleting `generate_bid_task` will destroy `generate_bid_client` and thus
// abort this callback, so it's safe to use Unretained(this) and
// `generate_bid_task` here.
generate_bid_task->generate_bid_client.set_disconnect_handler(
base::BindOnce(&BidderWorklet::OnGenerateBidClientDestroyed,
base::Unretained(this), generate_bid_task));
// Listen to call to FinalizeGenerateBid completing arguments.
generate_bid_task->finalize_generate_bid_receiver_id =
finalize_receiver_set_.Add(this, std::move(bid_finalizer),
generate_bid_task);
HandleDirectFromSellerForGenerateBid(direct_from_seller_per_buyer_signals,
direct_from_seller_auction_signals,
generate_bid_task);
const auto& trusted_bidding_signals_keys =
generate_bid_task->bidder_worklet_non_shared_params
->trusted_bidding_signals_keys;
generate_bid_task->trace_wait_deps_start = base::TimeTicks::Now();
TRACE_EVENT_NESTABLE_ASYNC_BEGIN0("fledge", "wait_generate_bid_deps",
trace_id);
if (trusted_signals_cache_key) {
// A non-null `trusted_signals_cache_key` indicates that the
// TrustedSignalsCache should be used through the
// `trusted_signals_kvv2_manager_` interface.
generate_bid_task->trusted_bidding_signals_kvv2_request =
trusted_signals_kvv2_manager_->RequestSignals(
TrustedSignalsKVv2Manager::SignalsType::kBidding,
trusted_signals_cache_key->compression_group_token,
trusted_signals_cache_key->partition_id,
base::BindOnce(&BidderWorklet::OnTrustedBiddingSignalsDownloaded,
base::Unretained(this), generate_bid_task));
return;
} else if (trusted_signals_request_manager_) {
if (trusted_signals_request_manager_->HasPublicKey()) {
DCHECK(base::FeatureList::IsEnabled(
blink::features::kFledgeTrustedSignalsKVv2Support));
generate_bid_task->trusted_bidding_signals_request =
trusted_signals_request_manager_->RequestKVv2BiddingSignals(
generate_bid_task->bidder_worklet_non_shared_params->name,
trusted_bidding_signals_keys,
generate_bid_task->interest_group_join_origin,
generate_bid_task->bidder_worklet_non_shared_params
->execution_mode,
base::BindOnce(&BidderWorklet::OnTrustedBiddingSignalsDownloaded,
base::Unretained(this), generate_bid_task));
} else {
generate_bid_task->trusted_bidding_signals_request =
trusted_signals_request_manager_->RequestBiddingSignals(
generate_bid_task->bidder_worklet_non_shared_params->name,
trusted_bidding_signals_keys,
generate_bid_task->bidder_worklet_non_shared_params
->max_trusted_bidding_signals_url_length,
base::BindOnce(&BidderWorklet::OnTrustedBiddingSignalsDownloaded,
base::Unretained(this), generate_bid_task));
}
return;
}
// Deleting `generate_bid_task` will destroy `generate_bid_client` and thus
// abort this callback, so it's safe to use Unretained(this) and
// `generate_bid_task` here.
generate_bid_task->generate_bid_client->OnBiddingSignalsReceived(
/*priority_vector=*/{},
/*trusted_signals_fetch_latency=*/base::TimeDelta(),
/*update_if_older_than=*/std::nullopt,
base::BindOnce(&BidderWorklet::SignalsReceivedCallback,
base::Unretained(this), generate_bid_task));
}
void BidderWorklet::SendPendingSignalsRequests() {
if (trusted_signals_request_manager_) {
trusted_signals_request_manager_->StartBatchedTrustedSignalsRequest();
}
}
void BidderWorklet::ReportWin(
bool is_for_additional_bid,
const std::optional<std::string>& interest_group_name_reporting_id,
const std::optional<std::string>& buyer_reporting_id,
const std::optional<std::string>& buyer_and_seller_reporting_id,
const std::optional<std::string>& selected_buyer_and_seller_reporting_id,
const std::optional<std::string>& auction_signals_json,
const std::optional<std::string>& per_buyer_signals_json,
const std::optional<GURL>& direct_from_seller_per_buyer_signals,
const std::optional<std::string>&
direct_from_seller_per_buyer_signals_header_ad_slot,
const std::optional<GURL>& direct_from_seller_auction_signals,
const std::optional<std::string>&
direct_from_seller_auction_signals_header_ad_slot,
const std::string& seller_signals_json,
mojom::KAnonymityBidMode kanon_mode,
bool bid_is_kanon,
const GURL& browser_signal_render_url,
double browser_signal_bid,
const std::optional<blink::AdCurrency>& browser_signal_bid_currency,
double browser_signal_highest_scoring_other_bid,
const std::optional<blink::AdCurrency>&
browser_signal_highest_scoring_other_bid_currency,
bool browser_signal_made_highest_scoring_other_bid,
std::optional<double> browser_signal_ad_cost,
std::optional<uint16_t> browser_signal_modeling_signals,
uint8_t browser_signal_join_count,
uint8_t browser_signal_recency,
const url::Origin& browser_signal_seller_origin,
const std::optional<url::Origin>& browser_signal_top_level_seller_origin,
const std::optional<base::TimeDelta> browser_signal_reporting_timeout,
std::optional<uint32_t> bidding_signals_data_version,
uint64_t trace_id,
ReportWinCallback report_win_callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(user_sequence_checker_);
CHECK((!direct_from_seller_per_buyer_signals &&
!direct_from_seller_auction_signals) ||
(!direct_from_seller_per_buyer_signals_header_ad_slot &&
!direct_from_seller_auction_signals_header_ad_slot));
report_win_tasks_.emplace_front();
auto report_win_task = report_win_tasks_.begin();
report_win_task->is_for_additional_bid = is_for_additional_bid;
report_win_task->interest_group_name_reporting_id =
interest_group_name_reporting_id;
report_win_task->buyer_reporting_id = buyer_reporting_id;
report_win_task->buyer_and_seller_reporting_id =
buyer_and_seller_reporting_id;
report_win_task->selected_buyer_and_seller_reporting_id =
selected_buyer_and_seller_reporting_id;
report_win_task->auction_signals_json = auction_signals_json;
report_win_task->per_buyer_signals_json = per_buyer_signals_json;
report_win_task->seller_signals_json = seller_signals_json;
report_win_task->kanon_mode = kanon_mode;
report_win_task->bid_is_kanon = bid_is_kanon;
report_win_task->browser_signal_render_url = browser_signal_render_url;
report_win_task->browser_signal_bid = browser_signal_bid;
report_win_task->browser_signal_bid_currency = browser_signal_bid_currency;
report_win_task->browser_signal_highest_scoring_other_bid =
browser_signal_highest_scoring_other_bid;
report_win_task->browser_signal_highest_scoring_other_bid_currency =
browser_signal_highest_scoring_other_bid_currency;
report_win_task->browser_signal_made_highest_scoring_other_bid =
browser_signal_made_highest_scoring_other_bid;
report_win_task->browser_signal_ad_cost = browser_signal_ad_cost;
report_win_task->browser_signal_modeling_signals =
browser_signal_modeling_signals;
report_win_task->browser_signal_join_count = browser_signal_join_count;
report_win_task->browser_signal_recency = browser_signal_recency;
report_win_task->browser_signal_seller_origin = browser_signal_seller_origin;
report_win_task->browser_signal_top_level_seller_origin =
browser_signal_top_level_seller_origin;
report_win_task->browser_signal_reporting_timeout =
browser_signal_reporting_timeout;
report_win_task->bidding_signals_data_version = bidding_signals_data_version;
report_win_task->trace_id = trace_id;
report_win_task->callback = std::move(report_win_callback);
if (direct_from_seller_per_buyer_signals) {
// Deleting `report_win_task` will destroy
// `direct_from_seller_request_seller_signals` and thus abort this
// callback, so it's safe to use Unretained(this) and `report_win_task`
// here.
report_win_task->direct_from_seller_request_per_buyer_signals =
direct_from_seller_requester_per_buyer_signals_.LoadSignals(
*url_loader_factory_, *direct_from_seller_per_buyer_signals,
base::BindOnce(
&BidderWorklet::
OnDirectFromSellerPerBuyerSignalsDownloadedReportWin,
base::Unretained(this), report_win_task));
} else {
report_win_task->direct_from_seller_result_per_buyer_signals =
DirectFromSellerSignalsRequester::Result();
}
if (direct_from_seller_auction_signals) {
// Deleting `report_win_task` will destroy
// `direct_from_seller_request_auction_signals` and thus abort this
// callback, so it's safe to use Unretained(this) and `report_win_task`
// here.
report_win_task->direct_from_seller_request_auction_signals =
direct_from_seller_requester_auction_signals_.LoadSignals(
*url_loader_factory_, *direct_from_seller_auction_signals,
base::BindOnce(
&BidderWorklet::
OnDirectFromSellerAuctionSignalsDownloadedReportWin,
base::Unretained(this), report_win_task));
} else {
report_win_task->direct_from_seller_result_auction_signals =
DirectFromSellerSignalsRequester::Result();
}
report_win_task->trace_wait_deps_start = base::TimeTicks::Now();
report_win_task->direct_from_seller_per_buyer_signals_header_ad_slot =
direct_from_seller_per_buyer_signals_header_ad_slot;
report_win_task->direct_from_seller_auction_signals_header_ad_slot =
direct_from_seller_auction_signals_header_ad_slot;
TRACE_EVENT_NESTABLE_ASYNC_BEGIN0("fledge", "wait_report_win_deps", trace_id);
RunReportWinIfReady(report_win_task);
}
void BidderWorklet::ConnectDevToolsAgent(
mojo::PendingAssociatedReceiver<blink::mojom::DevToolsAgent> agent,
uint32_t thread_index) {
DCHECK_CALLED_ON_VALID_SEQUENCE(user_sequence_checker_);
v8_runners_[thread_index]->PostTask(
FROM_HERE, base::BindOnce(&V8State::ConnectDevToolsAgent,
base::Unretained(v8_state_[thread_index].get()),
std::move(agent)));
}
void BidderWorklet::FinishGenerateBid(
const std::optional<std::string>& auction_signals_json,
const std::optional<std::string>& per_buyer_signals_json,
const std::optional<base::TimeDelta> per_buyer_timeout,
const std::optional<blink::AdCurrency>& expected_buyer_currency,
const std::optional<GURL>& direct_from_seller_per_buyer_signals,
const std::optional<std::string>&
direct_from_seller_per_buyer_signals_header_ad_slot,
const std::optional<GURL>& direct_from_seller_auction_signals,
const std::optional<std::string>&
direct_from_seller_auction_signals_header_ad_slot) {
CHECK((!direct_from_seller_per_buyer_signals &&
!direct_from_seller_auction_signals) ||
(!direct_from_seller_per_buyer_signals_header_ad_slot &&
!direct_from_seller_auction_signals_header_ad_slot));
GenerateBidTaskList::iterator task = finalize_receiver_set_.current_context();
task->auction_signals_json = auction_signals_json;
task->per_buyer_signals_json = per_buyer_signals_json;
task->per_buyer_timeout = per_buyer_timeout;
task->expected_buyer_currency = expected_buyer_currency;
task->finalize_generate_bid_called = true;
task->direct_from_seller_per_buyer_signals_header_ad_slot =
direct_from_seller_per_buyer_signals_header_ad_slot;
task->direct_from_seller_auction_signals_header_ad_slot =
direct_from_seller_auction_signals_header_ad_slot;
HandleDirectFromSellerForGenerateBid(direct_from_seller_per_buyer_signals,
direct_from_seller_auction_signals,
task);
finalize_receiver_set_.Remove(*task->finalize_generate_bid_receiver_id);
task->finalize_generate_bid_receiver_id = std::nullopt;
task->wait_promises = base::TimeTicks::Now() - task->trace_wait_deps_start;
GenerateBidIfReady(task);
}
BidderWorklet::GenerateBidTask::GenerateBidTask() = default;
BidderWorklet::GenerateBidTask::~GenerateBidTask() = default;
BidderWorklet::ReportWinTask::ReportWinTask() = default;
BidderWorklet::ReportWinTask::~ReportWinTask() = default;
BidderWorklet::V8State::V8State(
scoped_refptr<AuctionV8Helper> v8_helper,
scoped_refptr<AuctionV8Helper::DebugId> debug_id,
mojo::PendingRemote<mojom::AuctionSharedStorageHost>
shared_storage_host_remote,
const GURL& script_source_url,
const url::Origin& top_window_origin,
mojom::AuctionWorkletPermissionsPolicyStatePtr permissions_policy_state,
const std::optional<GURL>& wasm_helper_url,
const std::optional<GURL>& trusted_bidding_signals_url,
base::WeakPtr<BidderWorklet> parent)
: v8_helper_(std::move(v8_helper)),
debug_id_(std::move(debug_id)),
parent_(std::move(parent)),
user_thread_(base::SequencedTaskRunner::GetCurrentDefault()),
owner_(url::Origin::Create(script_source_url)),
script_source_url_(script_source_url),
top_window_origin_(top_window_origin),
permissions_policy_state_(std::move(permissions_policy_state)),
wasm_helper_url_(wasm_helper_url),
trusted_bidding_signals_url_(trusted_bidding_signals_url),
trusted_bidding_signals_origin_(
trusted_bidding_signals_url_ ? std::make_optional(url::Origin::Create(
*trusted_bidding_signals_url_))
: std::nullopt),
context_recyclers_for_origin_group_mode_(
GetNumberOfGroupByOriginContextsToKeep()) {
DETACH_FROM_SEQUENCE(v8_sequence_checker_);
v8_helper_->v8_runner()->PostTask(
FROM_HERE, base::BindOnce(&V8State::FinishInit, base::Unretained(this),
std::move(shared_storage_host_remote)));
}
void BidderWorklet::V8State::SetWorkletScript(
WorkletLoader::Result worklet_script) {
DCHECK_CALLED_ON_VALID_SEQUENCE(v8_sequence_checker_);
worklet_script_ = WorkletLoader::TakeScript(std::move(worklet_script));
}
void BidderWorklet::V8State::SetWasmHelper(
WorkletWasmLoader::Result wasm_helper) {
DCHECK_CALLED_ON_VALID_SEQUENCE(v8_sequence_checker_);
wasm_helper_ = std::move(wasm_helper);
}
BidderWorklet::V8State::SingleGenerateBidResult::SingleGenerateBidResult() =
default;
BidderWorklet::V8State::SingleGenerateBidResult::SingleGenerateBidResult(
std::unique_ptr<ContextRecycler> context_recycler_for_rerun,
std::vector<SetBidBindings::BidAndWorkletOnlyMetadata> bids,
std::optional<uint32_t> bidding_signals_data_version,
std::optional<GURL> debug_loss_report_url,
std::optional<GURL> debug_win_report_url,
std::optional<double> set_priority,
base::flat_map<std::string, mojom::PrioritySignalsDoublePtr>
update_priority_signals_overrides,
PrivateAggregationRequests pa_requests,
RealTimeReportingContributions real_time_contributions,
mojom::RejectReason reject_reason,
bool script_timed_out,
std::vector<std::string> error_msgs)
: context_recycler_for_rerun(std::move(context_recycler_for_rerun)),
bids(std::move(bids)),
bidding_signals_data_version(std::move(bidding_signals_data_version)),
debug_loss_report_url(std::move(debug_loss_report_url)),
debug_win_report_url(std::move(debug_win_report_url)),
set_priority(std::move(set_priority)),
update_priority_signals_overrides(
std::move(update_priority_signals_overrides)),
pa_requests(std::move(pa_requests)),
real_time_contributions(std::move(real_time_contributions)),
reject_reason(reject_reason),
script_timed_out(script_timed_out),
error_msgs(std::move(error_msgs)) {}
BidderWorklet::V8State::SingleGenerateBidResult::SingleGenerateBidResult(
SingleGenerateBidResult&&) = default;
BidderWorklet::V8State::SingleGenerateBidResult::~SingleGenerateBidResult() =
default;
BidderWorklet::V8State::SingleGenerateBidResult&
BidderWorklet::V8State::SingleGenerateBidResult::operator=(
SingleGenerateBidResult&&) = default;
void BidderWorklet::V8State::ReportWin(
bool is_for_additional_bid,
const std::optional<std::string>& interest_group_name_reporting_id,
const std::optional<std::string>& buyer_reporting_id,
const std::optional<std::string>& buyer_and_seller_reporting_id,
const std::optional<std::string>& selected_buyer_and_seller_reporting_id,
const std::optional<std::string>& auction_signals_json,
const std::optional<std::string>& per_buyer_signals_json,
DirectFromSellerSignalsRequester::Result
direct_from_seller_result_per_buyer_signals,
const std::optional<std::string>&
direct_from_seller_per_buyer_signals_header_ad_slot,
DirectFromSellerSignalsRequester::Result
direct_from_seller_result_auction_signals,
const std::optional<std::string>&
direct_from_seller_auction_signals_header_ad_slot,
const std::string& seller_signals_json,
mojom::KAnonymityBidMode kanon_mode,
bool bid_is_kanon,
const GURL& browser_signal_render_url,
double browser_signal_bid,
const std::optional<blink::AdCurrency>& browser_signal_bid_currency,
double browser_signal_highest_scoring_other_bid,
const std::optional<blink::AdCurrency>&
browser_signal_highest_scoring_other_bid_currency,
bool browser_signal_made_highest_scoring_other_bid,
const std::optional<double>& browser_signal_ad_cost,
const std::optional<uint16_t>& browser_signal_modeling_signals,
uint8_t browser_signal_join_count,
uint8_t browser_signal_recency,
const url::Origin& browser_signal_seller_origin,
const std::optional<url::Origin>& browser_signal_top_level_seller_origin,
const std::optional<base::TimeDelta> browser_signal_reporting_timeout,
const std::optional<uint32_t>& bidding_signals_data_version,
uint64_t trace_id,
ReportWinCallbackInternal callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(v8_sequence_checker_);
TRACE_EVENT_NESTABLE_ASYNC_END0("fledge", "post_v8_task", trace_id);
base::ElapsedTimer elapsed_timer;
// We may not be allowed any time to run.
if (browser_signal_reporting_timeout.has_value() &&
!browser_signal_reporting_timeout->is_positive()) {
PostReportWinCallbackToUserThread(
std::move(callback),
/*report_url=*/std::nullopt,
/*ad_beacon_map=*/{},
/*ad_macro_map=*/{},
/*pa_requests=*/{}, base::TimeDelta(),
/*script_timed_out=*/true,
/*errors=*/{"reportWin() aborted due to zero timeout."});
return;
}
AuctionV8Helper::FullIsolateScope isolate_scope(v8_helper_.get());
v8::Isolate* isolate = v8_helper_->isolate();
// Short lived context, to avoid leaking data at global scope between either
// repeated calls to this worklet, or to calls to any other worklet.
ContextRecycler context_recycler(v8_helper_.get());
ContextRecyclerScope context_recycler_scope(context_recycler);
v8::Local<v8::Context> context = context_recycler_scope.GetContext();
AuctionV8Logger v8_logger(v8_helper_.get(), context);
v8::LocalVector<v8::Value> args(isolate);
if (!AppendJsonValueOrNull(v8_helper_.get(), context,
base::OptionalToPtr(auction_signals_json),
&args) ||
!AppendJsonValueOrNull(v8_helper_.get(), context,
base::OptionalToPtr(per_buyer_signals_json),
&args) ||
!v8_helper_->AppendJsonValue(context, seller_signals_json, &args)) {
PostReportWinCallbackToUserThread(std::move(callback),
/*report_url=*/std::nullopt,
/*ad_beacon_map=*/{},
/*ad_macro_map=*/{},
/*pa_requests=*/{}, base::TimeDelta(),
/*script_timed_out=*/false,
/*errors=*/std::vector<std::string>());
return;
}
v8::Local<v8::Object> browser_signals = v8::Object::New(isolate);
gin::Dictionary browser_signals_dict(isolate, browser_signals);
std::string kanon_status;
switch (kanon_mode) {
case mojom::KAnonymityBidMode::kEnforce:
// If k-anon was truly enforced and it passed.
kanon_status = "passedAndEnforced";
break;
case mojom::KAnonymityBidMode::kSimulate:
if (bid_is_kanon) {
// If K-anon can determine the value and kSimulate is on.
kanon_status = "passedNotEnforced";
} else {
// Number of ad was below the threshold and kSimulate is on.
kanon_status = "belowThreshold";
}
break;
case mojom::KAnonymityBidMode::kNone:
// K-anon cannot determine the theoretical outcome.
kanon_status = "notCalculated";
break;
}
DeprecatedUrlLazyFiller deprecated_render_url(
v8_helper_.get(), &v8_logger, &browser_signal_render_url,
"browserSignals.renderUrl is deprecated."
" Please use browserSignals.renderURL instead.");
base::TimeDelta reporting_timeout =
browser_signal_reporting_timeout.has_value()
? *browser_signal_reporting_timeout
: AuctionV8Helper::kScriptTimeout;
if (!browser_signals_dict.Set("topWindowHostname",
top_window_origin_.host()) ||
!browser_signals_dict.Set(
"interestGroupOwner",
url::Origin::Create(script_source_url_).Serialize()) ||
(interest_group_name_reporting_id.has_value() &&
!browser_signals_dict.Set("interestGroupName",
*interest_group_name_reporting_id)) ||
(buyer_reporting_id.has_value() &&
!browser_signals_dict.Set("buyerReportingId", *buyer_reporting_id)) ||
(buyer_and_seller_reporting_id.has_value() &&
!browser_signals_dict.Set("buyerAndSellerReportingId",
*buyer_and_seller_reporting_id)) ||
(selected_buyer_and_seller_reporting_id.has_value() &&
!browser_signals_dict.Set("selectedBuyerAndSellerReportingId",
*selected_buyer_and_seller_reporting_id)) ||
!browser_signals_dict.Set("renderURL",
browser_signal_render_url.spec()) ||
// TODO(crbug.com/40266734): Remove deprecated `renderUrl` alias.
!deprecated_render_url.AddDeprecatedUrlGetter(browser_signals,
"renderUrl") ||
!browser_signals_dict.Set("bid", browser_signal_bid) ||
!browser_signals_dict.Set(
"bidCurrency",
blink::PrintableAdCurrency(browser_signal_bid_currency)) ||
(browser_signal_ad_cost.has_value() &&
!browser_signals_dict.Set("adCost", *browser_signal_ad_cost)) ||
(browser_signal_modeling_signals.has_value() &&
!browser_signals_dict.Set(
"modelingSignals",
static_cast<double>(*browser_signal_modeling_signals))) ||
!browser_signals_dict.Set(
"joinCount", static_cast<double>(browser_signal_join_count)) ||
(!is_for_additional_bid &&
!browser_signals_dict.Set(
"recency", static_cast<double>(browser_signal_recency))) ||
!browser_signals_dict.Set("highestScoringOtherBid",
browser_signal_highest_scoring_other_bid) ||
!browser_signals_dict.Set(
"highestScoringOtherBidCurrency",
blink::PrintableAdCurrency(
browser_signal_highest_scoring_other_bid_currency)) ||
!browser_signals_dict.Set(
"madeHighestScoringOtherBid",
browser_signal_made_highest_scoring_other_bid) ||
!browser_signals_dict.Set("seller",
browser_signal_seller_origin.Serialize()) ||
(browser_signal_top_level_seller_origin &&
!browser_signals_dict.Set(
"topLevelSeller",
browser_signal_top_level_seller_origin->Serialize())) ||
(bidding_signals_data_version.has_value() &&
!browser_signals_dict.Set("dataVersion",
bidding_signals_data_version.value())) ||
(base::FeatureList::IsEnabled(
blink::features::kFledgePassKAnonStatusToReportWin) &&
!kanon_status.empty() &&
!browser_signals_dict.Set("kAnonStatus", kanon_status)) ||
(base::FeatureList::IsEnabled(blink::features::kFledgeReportingTimeout) &&
!browser_signals_dict.Set("reportingTimeout",
reporting_timeout.InMilliseconds()))) {
PostReportWinCallbackToUserThread(std::move(callback),
/*report_url=*/std::nullopt,
/*ad_beacon_map=*/{},
/*ad_macro_map=*/{},
/*pa_requests=*/{}, base::TimeDelta(),
/*script_timed_out=*/false,
/*errors=*/std::vector<std::string>());
return;
}
args.push_back(browser_signals);
std::vector<std::string> errors_out;
v8::Local<v8::Object> direct_from_seller_signals = v8::Object::New(isolate);
gin::Dictionary direct_from_seller_signals_dict(isolate,
direct_from_seller_signals);
v8::Local<v8::Value> per_buyer_signals = GetDirectFromSellerSignals(
direct_from_seller_result_per_buyer_signals,
direct_from_seller_per_buyer_signals_header_ad_slot, *v8_helper_, context,
errors_out);
v8::Local<v8::Value> auction_signals = GetDirectFromSellerSignals(
direct_from_seller_result_auction_signals,
direct_from_seller_auction_signals_header_ad_slot, *v8_helper_, context,
errors_out);
if (!direct_from_seller_signals_dict.Set("perBuyerSignals",
per_buyer_signals) ||
!direct_from_seller_signals_dict.Set("auctionSignals", auction_signals)) {
PostReportWinCallbackToUserThread(std::move(callback),
/*report_url=*/std::nullopt,
/*ad_beacon_map=*/{},
/*ad_macro_map=*/{}, /*pa_requests=*/{},
base::TimeDelta(),
/*script_timed_out=*/false,
/*errors=*/std::move(errors_out));
return;
}
args.push_back(direct_from_seller_signals);
// An empty return value indicates an exception was thrown. Any other return
// value indicates no exception.
v8_helper_->MaybeTriggerInstrumentationBreakpoint(
*debug_id_, "beforeBidderWorkletReportingStart");
v8::Local<v8::UnboundScript> unbound_worklet_script =
worklet_script_.Get(isolate);
TRACE_EVENT_NESTABLE_ASYNC_BEGIN0("fledge", "report_win", trace_id);
std::unique_ptr<AuctionV8Helper::TimeLimit> total_timeout =
v8_helper_->CreateTimeLimit(
/*script_timeout=*/browser_signal_reporting_timeout);
AuctionV8Helper::Result result =
v8_helper_->RunScript(context, unbound_worklet_script, debug_id_.get(),
total_timeout.get(), errors_out);
if (result != AuctionV8Helper::Result::kSuccess) {
TRACE_EVENT_NESTABLE_ASYNC_END0("fledge", "report_win", trace_id);
PostReportWinCallbackToUserThread(
std::move(callback), /*report_url=*/std::nullopt,
/*ad_beacon_map=*/{}, /*ad_macro_map=*/{},
/*pa_requests=*/{}, elapsed_timer.Elapsed(),
/*script_timed_out=*/result == AuctionV8Helper::Result::kTimeout,
std::move(errors_out));
return;
}
context_recycler.AddReportBindings();
context_recycler.AddRegisterAdBeaconBindings();
context_recycler.AddRegisterAdMacroBindings();
context_recycler.AddPrivateAggregationBindings(
permissions_policy_state_->private_aggregation_allowed,
/*reserved_once_allowed=*/false);
if (base::FeatureList::IsEnabled(blink::features::kSharedStorageAPI)) {
context_recycler.AddSharedStorageBindings(
shared_storage_host_remote_.is_bound()
? shared_storage_host_remote_.get()
: nullptr,
mojom::AuctionWorkletFunction::kBidderReportWin,
permissions_policy_state_->shared_storage_allowed);
}
v8::MaybeLocal<v8::Value> maybe_report_result_ret;
result = v8_helper_->CallFunction(
context, debug_id_.get(),
v8_helper_->FormatScriptName(unbound_worklet_script),
is_for_additional_bid ? "reportAdditionalBidWin" : "reportWin", args,
total_timeout.get(), maybe_report_result_ret, errors_out);
TRACE_EVENT_NESTABLE_ASYNC_END0("fledge", "report_win", trace_id);
base::TimeDelta elapsed = elapsed_timer.Elapsed();
base::UmaHistogramTimes("Ads.InterestGroup.Auction.ReportWinTime", elapsed);
if (result != AuctionV8Helper::Result::kSuccess) {
// Keep Private Aggregation API requests since `reportWin()` might use it to
// detect script timeout or failures.
PostReportWinCallbackToUserThread(
std::move(callback), /*report_url=*/std::nullopt,
/*ad_beacon_map=*/{}, /*ad_macro_map=*/{},
context_recycler.private_aggregation_bindings()
->TakePrivateAggregationRequests(),
elapsed,
/*script_timed_out=*/result == AuctionV8Helper::Result::kTimeout,
std::move(errors_out));
return;
}
// This covers both the case where a report URL was provided, and the case one
// was not.
PostReportWinCallbackToUserThread(
std::move(callback), context_recycler.report_bindings()->report_url(),
context_recycler.register_ad_beacon_bindings()->TakeAdBeaconMap(),
context_recycler.register_ad_macro_bindings()->TakeAdMacroMap(),
context_recycler.private_aggregation_bindings()
->TakePrivateAggregationRequests(),
elapsed, /*script_timed_out=*/false, std::move(errors_out));
}
void BidderWorklet::V8State::GenerateBid(
mojom::BidderWorkletNonSharedParamsPtr bidder_worklet_non_shared_params,
mojom::KAnonymityBidMode kanon_mode,
const url::Origin& interest_group_join_origin,
const std::optional<std::string>& auction_signals_json,
const std::optional<std::string>& per_buyer_signals_json,
DirectFromSellerSignalsRequester::Result
direct_from_seller_result_per_buyer_signals,
const std::optional<std::string>&
direct_from_seller_per_buyer_signals_header_ad_slot,
DirectFromSellerSignalsRequester::Result
direct_from_seller_result_auction_signals,
const std::optional<std::string>&
direct_from_seller_auction_signals_header_ad_slot,
const std::optional<base::TimeDelta> per_buyer_timeout,
const std::optional<blink::AdCurrency>& expected_buyer_currency,
const url::Origin& browser_signal_seller_origin,
const std::optional<url::Origin>& browser_signal_top_level_seller_origin,
const base::TimeDelta browser_signal_recency,
blink::mojom::BiddingBrowserSignalsPtr bidding_browser_signals,
base::Time auction_start_time,
const std::optional<blink::AdSize>& requested_ad_size,
uint16_t multi_bid_limit,
scoped_refptr<TrustedSignals::Result> trusted_bidding_signals_result,
bool trusted_bidding_signals_fetch_failed,
uint64_t trace_id,
base::ScopedClosureRunner cleanup_generate_bid_task,
GenerateBidCallbackInternal callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(v8_sequence_checker_);
TRACE_EVENT_NESTABLE_ASYNC_END0("fledge", "post_v8_task", trace_id);
// Don't need to run `cleanup_generate_bid_task` if this method is invoked;
// it's bound to the closure to clean things up if this method got cancelled.
cleanup_generate_bid_task.ReplaceClosure(base::OnceClosure());
base::TimeTicks bidding_start = base::TimeTicks::Now();
std::optional<SingleGenerateBidResult> result = RunGenerateBidOnce(
*bidder_worklet_non_shared_params, interest_group_join_origin,
base::OptionalToPtr(auction_signals_json),
base::OptionalToPtr(per_buyer_signals_json),
direct_from_seller_result_per_buyer_signals,
direct_from_seller_per_buyer_signals_header_ad_slot,
direct_from_seller_result_auction_signals,
direct_from_seller_auction_signals_header_ad_slot, per_buyer_timeout,
expected_buyer_currency, browser_signal_seller_origin,
base::OptionalToPtr(browser_signal_top_level_seller_origin),
browser_signal_recency, bidding_browser_signals, auction_start_time,
requested_ad_size, multi_bid_limit, trusted_bidding_signals_result,
trace_id,
/*context_recycler_for_rerun=*/nullptr,
/*restrict_to_kanon_ads=*/false);
if (!result.has_value()) {
PostErrorBidCallbackToUserThread(
std::move(callback),
/*bidding_latency=*/base::TimeTicks::Now() - bidding_start,
PrivateAggregationRequests(),
GetRealTimeReportingContributionsOnError(
trusted_bidding_signals_fetch_failed, /*is_bidding_signal=*/true));
return;
}
std::vector<mojom::BidderWorkletBidPtr> bids =
ClassifyBidsAndApplyComponentAdLimits(
kanon_mode, bidder_worklet_non_shared_params.get(),
script_source_url_, std::move(result->bids));
if (kanon_mode != mojom::KAnonymityBidMode::kNone) {
// Go through and see which bids are actually k-anon appropriate.
bool found_kanon_bid = false;
for (const auto& bid : bids) {
if (bid->bid_role != mojom::BidRole::kUnenforcedKAnon) {
found_kanon_bid = true;
break;
}
}
// If bids were returned, and none were k-anon, we re-run the script with
// only k-anon ads available to it.
//
// At this point, we don't need to worry about the impact of k-anonymity
// for reporting on k-anon where a bid returns a value for
// `selectedBuyerAndSellerReportingId`. Assuming we find some
// k-anon ads in the loop below that would encourage us to make the
// k-anon-restricted call to `generateBid()`, we're only going to pass
// `selectableBuyerAndSellerReportingIds` that would, in combination with
// the renderUrl and other reporting ids, cause that bid to be k-anonymous
// for reporting. If no `selectableBuyerAndSellerReportingIds` meet that
// criteria, that's still fine: The bid could be returned with no
// `selectedBuyerAndSellerReportingId`, such that the k-anonymous bid
// would remain k-anonymous, even if it isn't k-anonymous for reporting.
if (!bids.empty() && !found_kanon_bid) {
bool has_kanon_ads = false;
for (const auto& ad : *bidder_worklet_non_shared_params->ads) {
if (BidderWorklet::IsKAnon(
bidder_worklet_non_shared_params.get(),
blink::HashedKAnonKeyForAdBid(owner_, script_source_url_,
ad.render_url()))) {
has_kanon_ads = true;
break;
}
}
// Main run got a non-k-anon result, and we care about k-anonymity. Re-run
// the bidder with non-k-anon ads hidden, limiting it to a single bid.
std::optional<SingleGenerateBidResult> restricted_result;
if (has_kanon_ads) {
restricted_result = RunGenerateBidOnce(
*bidder_worklet_non_shared_params.get(), interest_group_join_origin,
base::OptionalToPtr(auction_signals_json),
base::OptionalToPtr(per_buyer_signals_json),
direct_from_seller_result_per_buyer_signals,
direct_from_seller_per_buyer_signals_header_ad_slot,
direct_from_seller_result_auction_signals,
direct_from_seller_auction_signals_header_ad_slot,
per_buyer_timeout, expected_buyer_currency,
browser_signal_seller_origin,
base::OptionalToPtr(browser_signal_top_level_seller_origin),
browser_signal_recency, bidding_browser_signals, auction_start_time,
requested_ad_size, /* multi_bid_limit=*/1,
trusted_bidding_signals_result, trace_id,
std::move(result->context_recycler_for_rerun),
/*restrict_to_kanon_ads=*/true);
} else {
restricted_result = SingleGenerateBidResult(
std::unique_ptr<ContextRecycler>(),
std::vector<SetBidBindings::BidAndWorkletOnlyMetadata>(),
/*bidding_signals_data_version=*/std::nullopt,
/*debug_loss_report_url=*/std::nullopt,
/*debug_win_report_url=*/std::nullopt,
/*set_priority=*/std::nullopt,
/*update_priority_signals_overrides=*/{},
/*pa_requests=*/{},
/*real_time_contributions=*/{},
/*reject_reason=*/mojom::RejectReason::kNotAvailable,
/*script_timed_out=*/false,
/*error_msgs=*/{});
}
if (restricted_result.has_value()) {
// All the bids from the re-run will be k-anon enforced; we need to make
// sure to apply the component ad reduction, too.
for (auto& candidate : restricted_result->bids) {
candidate.bid->bid_role =
auction_worklet::mojom::BidRole::kEnforcedKAnon;
TrimAndCollectBid(std::move(candidate.bid),
candidate.target_num_ad_components, bids);
}
}
// Figure out which of `restricted_result` or `result` we actually want
// to use.
if (kanon_mode == mojom::KAnonymityBidMode::kEnforce) {
PrivateAggregationRequests non_kanon_pa_requests =
std::move(result->pa_requests);
std::erase_if(
non_kanon_pa_requests,
[](const auction_worklet::mojom::PrivateAggregationRequestPtr&
request) { return !HasKAnonFailureComponent(*request); });
// We are enforcing the k-anonymity, so the restricted result is the one
// to use for reporting, etc., and needs to succeed.
if (!restricted_result.has_value()) {
PostErrorBidCallbackToUserThread(
std::move(callback),
/*bidding_latency=*/base::TimeTicks::Now() - bidding_start,
std::move(non_kanon_pa_requests),
GetRealTimeReportingContributionsOnError(
trusted_bidding_signals_fetch_failed,
/*is_bidding_signal=*/true));
return;
}
result = std::move(restricted_result);
result->non_kanon_pa_requests = std::move(non_kanon_pa_requests);
} else {
DCHECK_EQ(kanon_mode, mojom::KAnonymityBidMode::kSimulate);
// Here, `result` is already what we want for reporting, etc., so
// nothing actually to do in this case.
}
}
}
// Add platform contributions if there needs to be one.
MaybeAddRealTimeReportingPlatformContributions(
trusted_bidding_signals_fetch_failed, /*is_bidding_signal=*/true,
result->real_time_contributions);
user_thread_->PostTask(
FROM_HERE,
base::BindOnce(std::move(callback), std::move(bids),
std::move(result->bidding_signals_data_version),
std::move(result->debug_loss_report_url),
std::move(result->debug_win_report_url),
std::move(result->set_priority),
std::move(result->update_priority_signals_overrides),
std::move(result->pa_requests),
std::move(result->non_kanon_pa_requests),
std::move(result->real_time_contributions),
/*bidding_latency=*/base::TimeTicks::Now() - bidding_start,
result->reject_reason, result->script_timed_out,
std::move(result->error_msgs)));
}
std::optional<BidderWorklet::V8State::SingleGenerateBidResult>
BidderWorklet::V8State::RunGenerateBidOnce(
const mojom::BidderWorkletNonSharedParams& bidder_worklet_non_shared_params,
const url::Origin& interest_group_join_origin,
const std::string* auction_signals_json,
const std::string* per_buyer_signals_json,
const DirectFromSellerSignalsRequester::Result&
direct_from_seller_result_per_buyer_signals,
const std::optional<std::string>&
direct_from_seller_per_buyer_signals_header_ad_slot,
const DirectFromSellerSignalsRequester::Result&
direct_from_seller_result_auction_signals,
const std::optional<std::string>&
direct_from_seller_auction_signals_header_ad_slot,
const std::optional<base::TimeDelta> per_buyer_timeout,
const std::optional<blink::AdCurrency>& expected_buyer_currency,
const url::Origin& browser_signal_seller_origin,
const url::Origin* browser_signal_top_level_seller_origin,
const base::TimeDelta browser_signal_recency,
const blink::mojom::BiddingBrowserSignalsPtr& bidding_browser_signals,
base::Time auction_start_time,
const std::optional<blink::AdSize>& requested_ad_size,
uint16_t multi_bid_limit,
const scoped_refptr<TrustedSignals::Result>& trusted_bidding_signals_result,
uint64_t trace_id,
std::unique_ptr<ContextRecycler> context_recycler_for_rerun,
bool restrict_to_kanon_ads) {
// Can't make a bid without any ads, or if we aren't permitted to spend any
// time on it.
if (!bidder_worklet_non_shared_params.ads) {
return std::nullopt;
}
std::vector<std::string> errors_out;
if (per_buyer_timeout.has_value() &&
!per_buyer_timeout.value().is_positive()) {
errors_out.push_back("generateBid() aborted due to zero timeout.");
return std::make_optional(SingleGenerateBidResult(
std::unique_ptr<ContextRecycler>(),
std::vector<SetBidBindings::BidAndWorkletOnlyMetadata>(),
/*bidding_signals_data_version=*/std::nullopt,
/*debug_loss_report_url=*/std::nullopt,
/*debug_win_report_url=*/std::nullopt,
/*set_priority=*/std::nullopt,
/*update_priority_signals_overrides=*/{},
/*pa_requests=*/{},
/*real_time_contributions=*/{},
/*reject_reason=*/mojom::RejectReason::kNotAvailable,
/*script_timed_out=*/true, std::move(errors_out)));
}
if (context_recycler_for_rerun) {
DCHECK(restrict_to_kanon_ads);
}
bool script_timed_out = false;
base::TimeTicks start = base::TimeTicks::Now();
AuctionV8Helper::FullIsolateScope isolate_scope(v8_helper_.get());
v8::Isolate* isolate = v8_helper_->isolate();
ContextRecycler* context_recycler = nullptr;
std::unique_ptr<ContextRecycler> fresh_context_recycler;
bool reused_context = false;
bool should_deep_freeze = false;
std::string_view execution_mode_string;
// See if we can reuse an existing context, and determine string to use for
// `executionMode`.
switch (bidder_worklet_non_shared_params.execution_mode) {
case blink::mojom::InterestGroup::ExecutionMode::kGroupedByOriginMode:
execution_mode_string = "group-by-origin";
{
auto it = context_recyclers_for_origin_group_mode_.Get(
interest_group_join_origin);
if (it != context_recyclers_for_origin_group_mode_.end()) {
context_recycler = it->second.get();
reused_context = true;
}
}
break;
case blink::mojom::InterestGroup::ExecutionMode::kFrozenContext:
execution_mode_string = "frozen-context";
if (!base::FeatureList::IsEnabled(
blink::features::kFledgeAlwaysReuseBidderContext)) {
should_deep_freeze = true;
}
if (context_recycler_for_frozen_context_) {
context_recycler = context_recycler_for_frozen_context_.get();
reused_context = true;
}
break;
case blink::mojom::InterestGroup::ExecutionMode::kCompatibilityMode:
execution_mode_string = "compatibility";
break;
}
DCHECK(!execution_mode_string.empty());
base::UmaHistogramBoolean("Ads.InterestGroup.Auction.ContextReused",
reused_context);
if (!context_recycler && context_recycler_for_always_reuse_feature_) {
context_recycler = context_recycler_for_always_reuse_feature_.get();
reused_context = true;
}
// See if we can reuse a context for k-anon re-run. The group-by-origin and
// frozen context mode would do that, too, so this is only a fallback for
// when that's not on.
if (!context_recycler && context_recycler_for_rerun) {
context_recycler = context_recycler_for_rerun.get();
reused_context = true;
}
v8_helper_->MaybeTriggerInstrumentationBreakpoint(
*debug_id_, "beforeBidderWorkletBiddingStart");
std::unique_ptr<AuctionV8Helper::TimeLimit> total_timeout =
v8_helper_->CreateTimeLimit(per_buyer_timeout);
// No recycled context, make a fresh one.
if (!context_recycler) {
fresh_context_recycler = CreateContextRecyclerAndRunTopLevelForGenerateBid(
trace_id, *total_timeout, should_deep_freeze, script_timed_out,
errors_out);
if (!fresh_context_recycler) {
return std::make_optional(SingleGenerateBidResult(
std::unique_ptr<ContextRecycler>(),
std::vector<SetBidBindings::BidAndWorkletOnlyMetadata>(),
/*bidding_signals_data_version=*/std::nullopt,
/*debug_loss_report_url=*/std::nullopt,
/*debug_win_report_url=*/std::nullopt,
/*set_priority=*/std::nullopt,
/*update_priority_signals_overrides=*/{},
/*pa_requests=*/{},
/*real_time_contributions=*/{},
/*reject_reason=*/mojom::RejectReason::kNotAvailable,
/*script_timed_out=*/script_timed_out, std::move(errors_out)));
}
context_recycler = fresh_context_recycler.get();
// Save the context for next time (if applicable).
if (base::FeatureList::IsEnabled(
blink::features::kFledgeAlwaysReuseBidderContext)) {
context_recycler_for_always_reuse_feature_ =
std::move(fresh_context_recycler);
} else {
switch (bidder_worklet_non_shared_params.execution_mode) {
case blink::mojom::InterestGroup::ExecutionMode::kGroupedByOriginMode:
context_recyclers_for_origin_group_mode_.Put(
interest_group_join_origin, std::move(fresh_context_recycler));
break;
case blink::mojom::InterestGroup::ExecutionMode::kFrozenContext:
context_recycler_for_frozen_context_ =
std::move(fresh_context_recycler);
break;
case blink::mojom::InterestGroup::ExecutionMode::kCompatibilityMode:
break;
}
}
}
base::RepeatingCallback<bool(const std::string&)>
should_exclude_ad_due_to_kanon = base::BindRepeating(
[](bool restrict_to_kanon_ads,
const mojom::BidderWorkletNonSharedParams* params,
const url::Origin* owner, const GURL* bidding_url,
const std::string& ad_url_from_gurl_spec) {
return restrict_to_kanon_ads &&
!BidderWorklet::IsKAnon(
params,
blink::HashedKAnonKeyForAdBid(*owner, *bidding_url,
ad_url_from_gurl_spec));
},
restrict_to_kanon_ads, &bidder_worklet_non_shared_params, &owner_,
&script_source_url_);
base::RepeatingCallback<bool(const std::string&)>
should_exclude_component_ad_due_to_kanon = base::BindRepeating(
[](bool restrict_to_kanon_ads,
const mojom::BidderWorkletNonSharedParams* params,
const std::string& ad_url_from_gurl_spec) {
return restrict_to_kanon_ads &&
!BidderWorklet::IsKAnon(
params, blink::HashedKAnonKeyForAdComponentBid(
ad_url_from_gurl_spec));
},
restrict_to_kanon_ads, &bidder_worklet_non_shared_params);
base::RepeatingCallback<bool(const std::string&,
base::optional_ref<const std::string>,
base::optional_ref<const std::string>,
base::optional_ref<const std::string>)>
should_exclude_reporting_id_set_due_to_kanon = base::BindRepeating(
[](bool restrict_to_kanon_ads,
const mojom::BidderWorkletNonSharedParams* params,
const url::Origin* owner, const GURL* bidding_url,
const std::string& ad_url_from_gurl_spec,
base::optional_ref<const std::string> buyer_reporting_id,
base::optional_ref<const std::string>
buyer_and_seller_reporting_id,
base::optional_ref<const std::string>
selected_buyer_and_seller_reporting_id) {
return restrict_to_kanon_ads &&
!BidderWorklet::IsKAnon(
params,
blink::
HashedKAnonKeyForAdNameReportingWithoutInterestGroup(
/*interest_group_owner=*/*owner,
/*interest_group_name=*/params->name,
/*interest_group_bidding_url=*/*bidding_url,
/*ad_render_url=*/ad_url_from_gurl_spec,
buyer_reporting_id,
buyer_and_seller_reporting_id,
selected_buyer_and_seller_reporting_id));
},
restrict_to_kanon_ads, &bidder_worklet_non_shared_params, &owner_,
&script_source_url_);
ContextRecyclerScope context_recycler_scope(*context_recycler);
v8::Local<v8::Context> context = context_recycler_scope.GetContext();
context_recycler->set_bid_bindings()->ReInitialize(
start, browser_signal_top_level_seller_origin != nullptr,
&bidder_worklet_non_shared_params, expected_buyer_currency,
multi_bid_limit, should_exclude_ad_due_to_kanon,
should_exclude_component_ad_due_to_kanon,
should_exclude_reporting_id_set_due_to_kanon);
v8::LocalVector<v8::Value> args(isolate);
v8::Local<v8::Object> interest_group_object = v8::Object::New(isolate);
gin::Dictionary interest_group_dict(isolate, interest_group_object);
if (!interest_group_dict.Set("owner", owner_.Serialize()) ||
!interest_group_dict.Set("name", bidder_worklet_non_shared_params.name) ||
!interest_group_dict.Set("enableBiddingSignalsPrioritization",
bidder_worklet_non_shared_params
.enable_bidding_signals_prioritization) ||
!interest_group_dict.Set("executionMode", execution_mode_string) ||
!interest_group_dict.Set(
"trustedBiddingSignalsSlotSizeMode",
blink::InterestGroup::TrustedBiddingSignalsSlotSizeModeToString(
bidder_worklet_non_shared_params
.trusted_bidding_signals_slot_size_mode))) {
return std::nullopt;
}
context_recycler->interest_group_lazy_filler()->ReInitialize(
&script_source_url_,
wasm_helper_url_.has_value() ? &wasm_helper_url_.value() : nullptr,
trusted_bidding_signals_url_.has_value()
? &trusted_bidding_signals_url_.value()
: nullptr,
&bidder_worklet_non_shared_params);
if (!context_recycler->interest_group_lazy_filler()->FillInObject(
interest_group_object, should_exclude_ad_due_to_kanon,
should_exclude_component_ad_due_to_kanon,
should_exclude_reporting_id_set_due_to_kanon)) {
return std::nullopt;
}
args.push_back(std::move(interest_group_object));
if (!AppendJsonValueOrNull(v8_helper_.get(), context, auction_signals_json,
&args) ||
!AppendJsonValueOrNull(v8_helper_.get(), context, per_buyer_signals_json,
&args)) {
return std::nullopt;
}
v8::Local<v8::Value> trusted_signals;
SignalsOriginRelation trusted_signals_relation = ClassifyTrustedSignals(
script_source_url_, trusted_bidding_signals_origin_);
UMA_HISTOGRAM_ENUMERATION(
"Ads.InterestGroup.Auction.TrustedBidderSignalsOriginRelation",
trusted_signals_relation);
if (!trusted_bidding_signals_result ||
!bidder_worklet_non_shared_params.trusted_bidding_signals_keys ||
bidder_worklet_non_shared_params.trusted_bidding_signals_keys->empty()) {
trusted_signals = v8::Null(isolate);
} else {
trusted_signals = trusted_bidding_signals_result->GetBiddingSignals(
v8_helper_.get(), context,
*bidder_worklet_non_shared_params.trusted_bidding_signals_keys);
}
if (trusted_signals_relation == SignalsOriginRelation::kSameOriginSignals) {
args.push_back(trusted_signals);
} else {
args.push_back(v8::Null(isolate));
}
std::optional<uint32_t> bidding_signals_data_version;
if (trusted_bidding_signals_result) {
bidding_signals_data_version =
trusted_bidding_signals_result->GetDataVersion();
}
v8::Local<v8::Object> browser_signals = v8::Object::New(isolate);
gin::Dictionary browser_signals_dict(isolate, browser_signals);
// TODO(crbug.com/336164429): Construct the fields of browser signals lazily.
if (!browser_signals_dict.Set("topWindowHostname",
top_window_origin_.host()) ||
!browser_signals_dict.Set("seller",
browser_signal_seller_origin.Serialize()) ||
(browser_signal_top_level_seller_origin &&
!browser_signals_dict.Set(
"topLevelSeller",
browser_signal_top_level_seller_origin->Serialize())) ||
!browser_signals_dict.Set("joinCount",
bidding_browser_signals->join_count) ||
!browser_signals_dict.Set("bidCount",
bidding_browser_signals->bid_count) ||
(base::FeatureList::IsEnabled(
blink::features::kBiddingAndScoringDebugReportingAPI) &&
base::FeatureList::IsEnabled(
blink::features::kFledgeSampleDebugReports) &&
!browser_signals_dict.Set(
"forDebuggingOnlyInCooldownOrLockout",
bidding_browser_signals
->for_debugging_only_in_cooldown_or_lockout)) ||
// `adComponentsLimit` is reported only when the corresponding change
// is rolled out, to avoid affecting behavior if it's not.
(base::FeatureList::IsEnabled(
blink::features::kFledgeCustomMaxAuctionAdComponents) &&
!browser_signals_dict.Set(
"adComponentsLimit",
// Cast to help gin on mac.
static_cast<uint64_t>(blink::MaxAdAuctionAdComponents()))) ||
// Report the multi-bid limit if the corresponding feature on.
(SupportMultiBid() &&
!browser_signals_dict.Set("multiBidLimit",
static_cast<uint32_t>(multi_bid_limit))) ||
(base::FeatureList::IsEnabled(
blink::features::kFledgePassRecencyToGenerateBid) &&
!browser_signals_dict.Set("recency",
browser_signal_recency.InMilliseconds())) ||
!SetDataVersion(trusted_signals_relation, bidding_signals_data_version,
browser_signals_dict) ||
(requested_ad_size.has_value() &&
!MaybeSetSizeMember(isolate, browser_signals_dict, "requestedSize",
requested_ad_size.value()))) {
return std::nullopt;
}
if (wasm_helper_.success()) {
v8::Local<v8::WasmModuleObject> module;
v8::Maybe<bool> result = v8::Nothing<bool>();
if (WorkletWasmLoader::MakeModule(wasm_helper_).ToLocal(&module)) {
result = browser_signals->Set(
context, gin::StringToV8(isolate, "wasmHelper"), module);
}
if (result.IsNothing() || !result.FromJust()) {
return std::nullopt;
}
}
context_recycler->bidding_browser_signals_lazy_filler()->ReInitialize(
bidding_browser_signals.get(), auction_start_time);
if (!context_recycler->bidding_browser_signals_lazy_filler()->FillInObject(
browser_signals)) {
return std::nullopt;
}
args.push_back(browser_signals);
v8::Local<v8::Object> direct_from_seller_signals = v8::Object::New(isolate);
gin::Dictionary direct_from_seller_signals_dict(isolate,
direct_from_seller_signals);
v8::Local<v8::Value> per_buyer_signals = GetDirectFromSellerSignals(
direct_from_seller_result_per_buyer_signals,
direct_from_seller_per_buyer_signals_header_ad_slot, *v8_helper_, context,
errors_out);
v8::Local<v8::Value> auction_signals = GetDirectFromSellerSignals(
direct_from_seller_result_auction_signals,
direct_from_seller_auction_signals_header_ad_slot, *v8_helper_, context,
errors_out);
if (!direct_from_seller_signals_dict.Set("perBuyerSignals",
per_buyer_signals) ||
!direct_from_seller_signals_dict.Set("auctionSignals", auction_signals)) {
return std::nullopt;
}
args.push_back(direct_from_seller_signals);
if (base::FeatureList::IsEnabled(
blink::features::kFledgePermitCrossOriginTrustedSignals)) {
v8::Local<v8::Value> cross_origin_trusted_bidding_signals_value;
if (trusted_signals_relation ==
SignalsOriginRelation::kCrossOriginSignals) {
cross_origin_trusted_bidding_signals_value =
TrustedSignals::Result::WrapCrossOriginSignals(
v8_helper_.get(), context, *trusted_bidding_signals_origin_,
trusted_signals);
} else {
cross_origin_trusted_bidding_signals_value = v8::Null(isolate);
}
args.push_back(cross_origin_trusted_bidding_signals_value);
}
v8::Local<v8::Value> generate_bid_result;
TRACE_EVENT_NESTABLE_ASYNC_BEGIN0("fledge", "generate_bid", trace_id);
v8::MaybeLocal<v8::Value> maybe_generate_bid_result;
AuctionV8Helper::Result script_result = v8_helper_->CallFunction(
context, debug_id_.get(),
v8_helper_->FormatScriptName(worklet_script_.Get(isolate)), "generateBid",
args, total_timeout.get(), maybe_generate_bid_result, errors_out);
bool got_return_value =
script_result == AuctionV8Helper::Result::kSuccess &&
maybe_generate_bid_result.ToLocal(&generate_bid_result);
script_timed_out = script_result == AuctionV8Helper::Result::kTimeout;
TRACE_EVENT_NESTABLE_ASYNC_END0("fledge", "generate_bid", trace_id);
base::TimeDelta time_duration = base::TimeTicks::Now() - start;
base::UmaHistogramTimes("Ads.InterestGroup.Auction.GenerateBidTime",
time_duration);
RealTimeReportingContributions real_time_contributions =
context_recycler->real_time_reporting_bindings()
->TakeRealTimeReportingContributions();
// Remove worklet latency contributions if the worklet execution time is
// within the threshold.
std::erase_if(
real_time_contributions,
[time_duration](
const auction_worklet::mojom::RealTimeReportingContributionPtr&
contribution) {
return contribution->latency_threshold.has_value() &&
time_duration.InMilliseconds() <=
contribution->latency_threshold.value();
});
if (got_return_value) {
IdlConvert::Status status =
context_recycler->set_bid_bindings()->SetBidImpl(
generate_bid_result,
base::StrCat({script_source_url_.spec(), " generateBid() "}));
if (!status.is_success()) {
if (status.is_timeout()) {
script_timed_out = true;
}
errors_out.push_back(status.ConvertToErrorString(isolate));
}
}
if (!context_recycler->set_bid_bindings()->has_bids()) {
// If no bid was returned (due to an error or just not choosing to bid), or
// the method timed out and no intermediate result was given through
// `setBid()`, return an error. Keep debug loss reports, Private
// Aggregation requests and real time reporting contributions since
// `generateBid()` might use them to detect script timeout or failures. Keep
// any set priority and set priority overrides because an interest group may
// want to update them even when not bidding. No need to return a
// ContextRecycler since this will not be re-run.
return std::make_optional(SingleGenerateBidResult(
std::unique_ptr<ContextRecycler>(),
std::vector<SetBidBindings::BidAndWorkletOnlyMetadata>(),
/*bidding_signals_data_version=*/std::nullopt,
context_recycler->for_debugging_only_bindings()->TakeLossReportUrl(),
/*debug_win_report_url=*/std::nullopt,
context_recycler->set_priority_bindings()->set_priority(),
context_recycler->set_priority_signals_override_bindings()
->TakeSetPrioritySignalsOverrides(),
context_recycler->private_aggregation_bindings()
->TakePrivateAggregationRequests(),
std::move(real_time_contributions),
context_recycler->set_bid_bindings()->reject_reason(), script_timed_out,
std::move(errors_out)));
}
// If the context recycler wasn't saved based on `execution_mode`,
// `fresh_context_recycler` is non-null here, and it will be provided to the
// caller for potential re-use for k-anon re-run.
return std::make_optional(SingleGenerateBidResult(
std::move(fresh_context_recycler),
context_recycler->set_bid_bindings()->TakeBids(),
bidding_signals_data_version,
context_recycler->for_debugging_only_bindings()->TakeLossReportUrl(),
context_recycler->for_debugging_only_bindings()->TakeWinReportUrl(),
context_recycler->set_priority_bindings()->set_priority(),
context_recycler->set_priority_signals_override_bindings()
->TakeSetPrioritySignalsOverrides(),
context_recycler->private_aggregation_bindings()
->TakePrivateAggregationRequests(),
std::move(real_time_contributions), mojom::RejectReason::kNotAvailable,
script_timed_out, std::move(errors_out)));
}
std::unique_ptr<ContextRecycler>
BidderWorklet::V8State::CreateContextRecyclerAndRunTopLevelForGenerateBid(
uint64_t trace_id,
AuctionV8Helper::TimeLimit& total_timeout,
bool should_deep_freeze,
bool& script_timed_out,
std::vector<std::string>& errors_out) {
base::TimeTicks start = base::TimeTicks::Now();
std::unique_ptr<ContextRecycler> context_recycler =
std::make_unique<ContextRecycler>(v8_helper_.get());
TRACE_EVENT_NESTABLE_ASYNC_BEGIN0("fledge", "get_bidder_context", trace_id);
ContextRecyclerScope context_recycler_scope(*context_recycler);
v8::Local<v8::Context> context = context_recycler_scope.GetContext();
TRACE_EVENT_NESTABLE_ASYNC_END0("fledge", "get_bidder_context", trace_id);
v8::Local<v8::UnboundScript> unbound_worklet_script =
worklet_script_.Get(v8_helper_->isolate());
TRACE_EVENT_NESTABLE_ASYNC_BEGIN0("fledge", "biddingScript", trace_id);
AuctionV8Helper::Result result =
v8_helper_->RunScript(context, unbound_worklet_script, debug_id_.get(),
&total_timeout, errors_out);
TRACE_EVENT_NESTABLE_ASYNC_END0("fledge", "biddingScript", trace_id);
base::UmaHistogramTimes("Ads.InterestGroup.Auction.BidScriptTime",
base::TimeTicks::Now() - start);
if (result == AuctionV8Helper::Result::kTimeout) {
script_timed_out = true;
}
if (result != AuctionV8Helper::Result::kSuccess) {
return nullptr;
}
context_recycler->AddForDebuggingOnlyBindings();
context_recycler->AddPrivateAggregationBindings(
permissions_policy_state_->private_aggregation_allowed,
/*reserved_once_allowed=*/true);
context_recycler->AddRealTimeReportingBindings();
if (base::FeatureList::IsEnabled(blink::features::kSharedStorageAPI)) {
context_recycler->AddSharedStorageBindings(
shared_storage_host_remote_.is_bound()
? shared_storage_host_remote_.get()
: nullptr,
mojom::AuctionWorkletFunction::kBidderGenerateBid,
permissions_policy_state_->shared_storage_allowed);
}
context_recycler->AddSetBidBindings();
context_recycler->AddSetPriorityBindings();
context_recycler->AddSetPrioritySignalsOverrideBindings();
context_recycler->AddInterestGroupLazyFiller();
context_recycler->AddBiddingBrowserSignalsLazyFiller();
if (should_deep_freeze) {
v8::TryCatch try_catch(v8_helper_->isolate());
DeepFreezeAllowAll allow_jsapiobject;
context->DeepFreeze(&allow_jsapiobject);
if (try_catch.HasCaught()) {
errors_out.push_back(AuctionV8Helper::FormatExceptionMessage(
context, try_catch.Message()));
return nullptr;
}
}
return context_recycler;
}
void BidderWorklet::V8State::ConnectDevToolsAgent(
mojo::PendingAssociatedReceiver<blink::mojom::DevToolsAgent> agent) {
DCHECK_CALLED_ON_VALID_SEQUENCE(v8_sequence_checker_);
v8_helper_->ConnectDevToolsAgent(std::move(agent), user_thread_, *debug_id_);
}
BidderWorklet::V8State::~V8State() {
DCHECK_CALLED_ON_VALID_SEQUENCE(v8_sequence_checker_);
AuctionV8Helper::FullIsolateScope isolate_scope(v8_helper_.get());
v8::Isolate* isolate = v8_helper_->isolate();
v8::HeapStatistics heap_statistics;
isolate->GetHeapStatistics(&heap_statistics);
base::UmaHistogramCounts100000(
"Ads.InterestGroup.Auction.BidderWorkletIsolateUsedHeapSizeKilobytes",
heap_statistics.used_heap_size() / 1024);
base::UmaHistogramCounts100000(
"Ads.InterestGroup.Auction.BidderWorkletIsolateTotalHeapSizeKilobytes",
heap_statistics.total_heap_size() / 1024);
}
void BidderWorklet::V8State::FinishInit(
mojo::PendingRemote<mojom::AuctionSharedStorageHost>
shared_storage_host_remote) {
DCHECK_CALLED_ON_VALID_SEQUENCE(v8_sequence_checker_);
if (shared_storage_host_remote) {
shared_storage_host_remote_.Bind(std::move(shared_storage_host_remote));
}
debug_id_->SetResumeCallback(base::BindOnce(
&BidderWorklet::V8State::PostResumeToUserThread, parent_, user_thread_));
}
// static
void BidderWorklet::V8State::PostResumeToUserThread(
base::WeakPtr<BidderWorklet> parent,
scoped_refptr<base::SequencedTaskRunner> user_thread) {
// This is static since it's called from debugging, not BidderWorklet,
// so the usual guarantee that BidderWorklet posts things before posting
// V8State destruction is irrelevant.
user_thread->PostTask(FROM_HERE,
base::BindOnce(&BidderWorklet::ResumeIfPaused, parent));
}
void BidderWorklet::V8State::PostReportWinCallbackToUserThread(
ReportWinCallbackInternal callback,
const std::optional<GURL>& report_url,
base::flat_map<std::string, GURL> ad_beacon_map,
base::flat_map<std::string, std::string> ad_macro_map,
PrivateAggregationRequests pa_requests,
base::TimeDelta reporting_latency,
bool script_timed_out,
std::vector<std::string> errors) {
DCHECK_CALLED_ON_VALID_SEQUENCE(v8_sequence_checker_);
user_thread_->PostTask(
FROM_HERE,
base::BindOnce(std::move(callback), std::move(report_url),
std::move(ad_beacon_map), std::move(ad_macro_map),
std::move(pa_requests), reporting_latency,
script_timed_out, std::move(errors)));
}
void BidderWorklet::V8State::PostErrorBidCallbackToUserThread(
GenerateBidCallbackInternal callback,
base::TimeDelta bidding_latency,
PrivateAggregationRequests non_kanon_pa_requests,
RealTimeReportingContributions real_time_contributions,
std::vector<std::string> error_msgs) {
DCHECK_CALLED_ON_VALID_SEQUENCE(v8_sequence_checker_);
user_thread_->PostTask(
FROM_HERE,
base::BindOnce(
std::move(callback), std::vector<mojom::BidderWorkletBidPtr>(),
/*bidding_signals_data_version=*/std::nullopt,
/*debug_loss_report_url=*/std::nullopt,
/*debug_win_report_url=*/std::nullopt,
/*set_priority=*/std::nullopt,
/*update_priority_signals_overrides=*/
base::flat_map<std::string, mojom::PrioritySignalsDoublePtr>(),
/*pa_requests=*/
PrivateAggregationRequests(), std::move(non_kanon_pa_requests),
std::move(real_time_contributions), bidding_latency,
/*reject_reason=*/mojom::RejectReason::kNotAvailable,
/*script_timed_out=*/false, std::move(error_msgs)));
}
void BidderWorklet::ResumeIfPaused() {
DCHECK_CALLED_ON_VALID_SEQUENCE(user_sequence_checker_);
if (!paused_) {
return;
}
resumed_count_++;
if (resumed_count_ == v8_helpers_.size()) {
paused_ = false;
Start();
}
}
void BidderWorklet::Start() {
DCHECK_CALLED_ON_VALID_SEQUENCE(user_sequence_checker_);
DCHECK(!paused_);
base::UmaHistogramCounts100000(
"Ads.InterestGroup.Net.RequestUrlSizeBytes.BiddingScriptJS",
script_source_url_.spec().size());
code_download_start_ = base::TimeTicks::Now();
worklet_loader_ = std::make_unique<WorkletLoader>(
url_loader_factory_.get(),
/*auction_network_events_handler=*/
CreateNewAuctionNetworkEventsHandlerRemote(
auction_network_events_handler_),
script_source_url_, v8_helpers_, debug_ids_,
WorkletLoader::AllowTrustedScoringSignalsCallback(),
base::BindOnce(&BidderWorklet::OnScriptDownloaded,
base::Unretained(this)));
if (wasm_helper_url_.has_value()) {
base::UmaHistogramCounts100000(
"Ads.InterestGroup.Net.RequestUrlSizeBytes.BiddingScriptWasm",
wasm_helper_url_->spec().size());
wasm_loader_ = std::make_unique<WorkletWasmLoader>(
url_loader_factory_.get(),
/*auction_network_events_handler=*/
CreateNewAuctionNetworkEventsHandlerRemote(
auction_network_events_handler_),
wasm_helper_url_.value(), v8_helpers_, debug_ids_,
base::BindOnce(&BidderWorklet::OnWasmDownloaded,
base::Unretained(this)));
}
}
void BidderWorklet::OnScriptDownloaded(
std::vector<WorkletLoader::Result> worklet_scripts,
std::optional<std::string> error_msg) {
DCHECK_CALLED_ON_VALID_SEQUENCE(user_sequence_checker_);
DCHECK_EQ(worklet_scripts.size(), v8_helpers_.size());
js_fetch_latency_ = base::TimeTicks::Now() - code_download_start_;
// Use `worklet_scripts[0]` for metrics and for the failure check. All the
// results should be the same.
base::UmaHistogramCounts10M(
"Ads.InterestGroup.Net.ResponseSizeBytes.BiddingScriptJS",
worklet_scripts[0].original_size_bytes());
base::UmaHistogramTimes("Ads.InterestGroup.Net.DownloadTime.BiddingScriptJS",
worklet_scripts[0].download_time());
worklet_loader_.reset();
// On failure, close pipe and delete `this`, as it can't do anything without a
// loaded script.
if (!worklet_scripts[0].success()) {
std::move(close_pipe_callback_)
.Run(error_msg ? error_msg.value() : std::string());
// `this` should be deleted at this point.
return;
}
if (error_msg.has_value()) {
load_code_error_msgs_.push_back(std::move(error_msg.value()));
}
for (size_t i = 0; i < v8_runners_.size(); ++i) {
v8_runners_[i]->PostTask(
FROM_HERE, base::BindOnce(&BidderWorklet::V8State::SetWorkletScript,
base::Unretained(v8_state_[i].get()),
std::move(worklet_scripts[i])));
}
MaybeRecordCodeWait();
RunReadyTasks();
}
void BidderWorklet::OnWasmDownloaded(
std::vector<WorkletWasmLoader::Result> worklet_scripts,
std::optional<std::string> error_msg) {
DCHECK_CALLED_ON_VALID_SEQUENCE(user_sequence_checker_);
DCHECK_EQ(worklet_scripts.size(), v8_helpers_.size());
wasm_fetch_latency_ = base::TimeTicks::Now() - code_download_start_;
// Use `worklet_scripts[0]` for metrics and for the failure check. All the
// results should be the same.
base::UmaHistogramCounts10M(
"Ads.InterestGroup.Net.ResponseSizeBytes.BiddingScriptWasm",
worklet_scripts[0].original_size_bytes());
base::UmaHistogramTimes(
"Ads.InterestGroup.Net.DownloadTime.BiddingScriptWasm",
worklet_scripts[0].download_time());
wasm_loader_.reset();
// If the WASM helper is actually requested, delete `this` and inform the
// browser process of the failure. ReportWin() calls would theoretically still
// be allowed, but that adds a lot more complexity around BidderWorklet reuse.
if (!worklet_scripts[0].success()) {
std::move(close_pipe_callback_)
.Run(error_msg ? error_msg.value() : std::string());
// `this` should be deleted at this point.
return;
}
if (error_msg.has_value()) {
load_code_error_msgs_.push_back(std::move(error_msg.value()));
}
for (size_t i = 0; i < v8_runners_.size(); ++i) {
v8_runners_[i]->PostTask(
FROM_HERE, base::BindOnce(&BidderWorklet::V8State::SetWasmHelper,
base::Unretained(v8_state_[i].get()),
std::move(worklet_scripts[i])));
}
MaybeRecordCodeWait();
RunReadyTasks();
}
void BidderWorklet::MaybeRecordCodeWait() {
if (!IsCodeReady()) {
return;
}
base::TimeTicks now = base::TimeTicks::Now();
for (auto& task : generate_bid_tasks_) {
task.wait_code = now - task.trace_wait_deps_start;
}
for (auto& task : report_win_tasks_) {
task.wait_code = now - task.trace_wait_deps_start;
}
}
void BidderWorklet::RunReadyTasks() {
// Run all GenerateBid() tasks that are ready. GenerateBidIfReady() does *not*
// modify `generate_bid_tasks_` when invoked, so this is safe.
for (auto generate_bid_task = generate_bid_tasks_.begin();
generate_bid_task != generate_bid_tasks_.end(); ++generate_bid_task) {
GenerateBidIfReady(generate_bid_task);
}
// While reportWin() doesn't use WASM, since we do load it, we wait for it in
// order to ensure determinism if the load fails.
if (!IsCodeReady()) {
return;
}
// Run all ReportWin() tasks that are ready. RunReportWinIfReady() does *not*
// modify `report_win_tasks_` when invoked, so this is safe.
for (auto report_win_task = report_win_tasks_.begin();
report_win_task != report_win_tasks_.end(); ++report_win_task) {
RunReportWinIfReady(report_win_task);
}
}
void BidderWorklet::OnTrustedBiddingSignalsDownloaded(
GenerateBidTaskList::iterator task,
scoped_refptr<TrustedSignals::Result> result,
std::optional<std::string> error_msg) {
DCHECK_CALLED_ON_VALID_SEQUENCE(user_sequence_checker_);
const TrustedSignals::Result::PerGroupData* per_group_data = nullptr;
if (result) {
per_group_data =
result->GetPerGroupData(task->bidder_worklet_non_shared_params->name);
}
task->trusted_bidding_signals_fetch_failed = !result ? true : false;
task->trusted_bidding_signals_result = std::move(result);
task->trusted_bidding_signals_error_msg = std::move(error_msg);
// If using TrustedSignalsRequestManager, the request is no longer needed, so
// delete it to save resources. If using TrustedSignalsKVv2Manager, however,
// that request keeps the reusable cached compression group alive, so keep the
// KVv2 request around until finished generating the bid.
task->trusted_bidding_signals_request.reset();
// Deleting `generate_bid_task` will destroy `generate_bid_client` and thus
// abort this callback, so it's safe to use Unretained(this) and
// `generate_bid_task` here.
task->generate_bid_client->OnBiddingSignalsReceived(
per_group_data && per_group_data->priority_vector
? *per_group_data->priority_vector
: TrustedSignals::Result::PriorityVector(),
/*trusted_signals_fetch_latency=*/base::TimeTicks::Now() -
task->trace_wait_deps_start,
/*update_if_older_than=*/
per_group_data ? per_group_data->update_if_older_than : std::nullopt,
base::BindOnce(&BidderWorklet::SignalsReceivedCallback,
base::Unretained(this), task));
}
void BidderWorklet::OnGenerateBidClientDestroyed(
GenerateBidTaskList::iterator task) {
// If the task hasn't received the signals called callback or the code hasn't
// loaded, it hasn't posted a task to run off-thread, so can be safely
// deleted, as everything else, including fetching trusted bidding signals,
// can be safely cancelled.
if (!IsReadyToGenerateBid(*task)) {
// GenerateBidIfReady is never called so make sure to close out this trace
// event.
TRACE_EVENT_NESTABLE_ASYNC_END0("fledge", "wait_generate_bid_deps",
task->trace_id);
CleanUpBidTaskOnUserThread(task);
} else {
// Otherwise, there should be a pending V8 call. Try to cancel that, but if
// it already started, it will just run and invoke the GenerateBidClient's
// OnGenerateBidComplete() method, which will safely do nothing since the
// pipe is now closed.
DCHECK_NE(task->task_id, base::CancelableTaskTracker::kBadTaskId);
cancelable_task_tracker_.TryCancel(task->task_id);
}
}
void BidderWorklet::SignalsReceivedCallback(
GenerateBidTaskList::iterator task) {
DCHECK(!task->signals_received_callback_invoked);
task->signals_received_callback_invoked = true;
task->wait_trusted_signals =
base::TimeTicks::Now() - task->trace_wait_deps_start;
GenerateBidIfReady(task);
}
void BidderWorklet::HandleDirectFromSellerForGenerateBid(
const std::optional<GURL>& direct_from_seller_per_buyer_signals,
const std::optional<GURL>& direct_from_seller_auction_signals,
GenerateBidTaskList::iterator task) {
if (direct_from_seller_per_buyer_signals) {
// We expect each parameter to be provided at most once between
// BeginGenerateBid/FinishGenerateBid. If we are already fetching this
// kind of signals this is clearly the second time it was specified.
DCHECK(!task->direct_from_seller_request_per_buyer_signals);
// Deleting `task` will destroy
// `direct_from_seller_request_per_buyer_signals` and thus abort this
// callback, so it's safe to use Unretained(this) and `task`
// here.
task->direct_from_seller_request_per_buyer_signals =
direct_from_seller_requester_per_buyer_signals_.LoadSignals(
*url_loader_factory_, *direct_from_seller_per_buyer_signals,
base::BindOnce(
&BidderWorklet::
OnDirectFromSellerPerBuyerSignalsDownloadedGenerateBid,
base::Unretained(this), task));
}
if (direct_from_seller_auction_signals) {
// We expect each parameter to be provided at most once between
// BeginGenerateBid/FinishGenerateBid. If we are already fetching this
// kind of signals this is clearly the second time it was specified.
DCHECK(!task->direct_from_seller_request_auction_signals);
// Deleting `task` will destroy
// `direct_from_seller_request_auction_signals` and thus abort this
// callback, so it's safe to use Unretained(this) and `task`
// here.
task->direct_from_seller_request_auction_signals =
direct_from_seller_requester_auction_signals_.LoadSignals(
*url_loader_factory_, *direct_from_seller_auction_signals,
base::BindOnce(
&BidderWorklet::
OnDirectFromSellerAuctionSignalsDownloadedGenerateBid,
base::Unretained(this), task));
}
}
void BidderWorklet::OnDirectFromSellerPerBuyerSignalsDownloadedGenerateBid(
GenerateBidTaskList::iterator task,
DirectFromSellerSignalsRequester::Result result) {
DCHECK_CALLED_ON_VALID_SEQUENCE(user_sequence_checker_);
task->direct_from_seller_result_per_buyer_signals = std::move(result);
task->direct_from_seller_request_per_buyer_signals.reset();
// The two direct from seller signals metrics for tracing are combined since
// they should be roughly the same.
task->wait_direct_from_seller_signals =
std::max(task->wait_direct_from_seller_signals,
base::TimeTicks::Now() - task->trace_wait_deps_start);
GenerateBidIfReady(task);
}
void BidderWorklet::OnDirectFromSellerAuctionSignalsDownloadedGenerateBid(
GenerateBidTaskList::iterator task,
DirectFromSellerSignalsRequester::Result result) {
DCHECK_CALLED_ON_VALID_SEQUENCE(user_sequence_checker_);
task->direct_from_seller_result_auction_signals = std::move(result);
task->direct_from_seller_request_auction_signals.reset();
// The two direct from seller signals metrics for tracing are combined since
// they should be roughly the same.
task->wait_direct_from_seller_signals =
std::max(task->wait_direct_from_seller_signals,
base::TimeTicks::Now() - task->trace_wait_deps_start);
GenerateBidIfReady(task);
}
bool BidderWorklet::IsReadyToGenerateBid(const GenerateBidTask& task) const {
return task.signals_received_callback_invoked &&
task.finalize_generate_bid_called &&
!task.direct_from_seller_request_per_buyer_signals &&
!task.direct_from_seller_request_auction_signals && IsCodeReady();
}
void BidderWorklet::GenerateBidIfReady(GenerateBidTaskList::iterator task) {
DCHECK_CALLED_ON_VALID_SEQUENCE(user_sequence_checker_);
if (!IsReadyToGenerateBid(*task)) {
return;
}
// If there was a trusted signals request, it should have already completed
// and been cleaned up before `signals_received_callback_invoked` was set to
// true.
DCHECK(!task->trusted_bidding_signals_request);
TRACE_EVENT_NESTABLE_ASYNC_END1(
"fledge", "wait_generate_bid_deps", task->trace_id, "data",
[&](perfetto::TracedValue trace_context) {
auto dict = std::move(trace_context).WriteDictionary();
if (!task->wait_code.is_zero()) {
dict.Add("wait_code_ms", task->wait_code.InMillisecondsF());
}
if (!task->wait_trusted_signals.is_zero()) {
dict.Add("wait_trusted_signals_ms",
task->wait_trusted_signals.InMillisecondsF());
}
if (!task->wait_direct_from_seller_signals.is_zero()) {
dict.Add("wait_direct_from_seller_signals_ms",
task->wait_direct_from_seller_signals.InMillisecondsF());
}
if (!task->wait_promises.is_zero()) {
dict.Add("wait_promises_ms", task->wait_promises.InMillisecondsF());
}
});
TRACE_EVENT_NESTABLE_ASYNC_BEGIN0("fledge", "post_v8_task", task->trace_id);
// Normally the PostTask below will eventually get `task` cleaned up once it
// posts back to DeliverBidCallbackOnUserThread with its results, but that
// won't happen if it gets cancelled. To deal with that, a ScopedClosureRunner
// is passed to ask for `task` to get cleaned up in case the
// V8State::GenerateBid closure gets destroyed without running.
base::OnceClosure cleanup_generate_bid_task =
base::BindPostTaskToCurrentDefault(
base::BindOnce(&BidderWorklet::CleanUpBidTaskOnUserThread,
weak_ptr_factory_.GetWeakPtr(), task));
// In 'group-by-origin' mode, make the thread assignment sticky to
// join_origin. This favors context reuse to save memory. The per-worklet
// random salt is added to make sure certain origins won't always be grouped
// together.
int thread_index = 0;
if (task->bidder_worklet_non_shared_params->execution_mode ==
blink::mojom::InterestGroup::ExecutionMode::kGroupedByOriginMode) {
size_t join_origin_hash = base::FastHash(
join_origin_hash_salt_ + task->interest_group_join_origin.Serialize());
thread_index = join_origin_hash % v8_helpers_.size();
} else {
thread_index = GetNextThreadIndex();
}
// Other than the `generate_bid_client` and `task_id` fields, no fields of
// `task` are needed after this point, so can consume them instead of copying
// them.
//
// Since IsReadyToGenerateBid() is true, the GenerateBidTask won't be deleted
// on the main thread during this call, even if the GenerateBidClient pipe is
// deleted by the caller (unless the BidderWorklet itself is deleted).
// Therefore, it's safe to post a callback with the `task` iterator the v8
// thread.
task->generate_bid_start_time = base::TimeTicks::Now();
task->task_id = cancelable_task_tracker_.PostTask(
v8_runners_[thread_index].get(), FROM_HERE,
base::BindOnce(
&BidderWorklet::V8State::GenerateBid,
base::Unretained(v8_state_[thread_index].get()),
std::move(task->bidder_worklet_non_shared_params), task->kanon_mode,
std::move(task->interest_group_join_origin),
std::move(task->auction_signals_json),
std::move(task->per_buyer_signals_json),
std::move(task->direct_from_seller_result_per_buyer_signals),
std::move(task->direct_from_seller_per_buyer_signals_header_ad_slot),
std::move(task->direct_from_seller_result_auction_signals),
std::move(task->direct_from_seller_auction_signals_header_ad_slot),
std::move(task->per_buyer_timeout),
std::move(task->expected_buyer_currency),
std::move(task->browser_signal_seller_origin),
std::move(task->browser_signal_top_level_seller_origin),
std::move(task->browser_signal_recency),
std::move(task->bidding_browser_signals), task->auction_start_time,
std::move(task->requested_ad_size), task->multi_bid_limit,
std::move(task->trusted_bidding_signals_result),
task->trusted_bidding_signals_fetch_failed, task->trace_id,
base::ScopedClosureRunner(std::move(cleanup_generate_bid_task)),
base::BindOnce(&BidderWorklet::DeliverBidCallbackOnUserThread,
weak_ptr_factory_.GetWeakPtr(), task)));
}
void BidderWorklet::OnDirectFromSellerPerBuyerSignalsDownloadedReportWin(
ReportWinTaskList::iterator task,
DirectFromSellerSignalsRequester::Result result) {
DCHECK_CALLED_ON_VALID_SEQUENCE(user_sequence_checker_);
task->direct_from_seller_result_per_buyer_signals = std::move(result);
task->direct_from_seller_request_per_buyer_signals.reset();
// The two direct from seller signals metrics for tracing are combined since
// they should be roughly the same.
task->wait_direct_from_seller_signals =
std::max(task->wait_direct_from_seller_signals,
base::TimeTicks::Now() - task->trace_wait_deps_start);
RunReportWinIfReady(task);
}
void BidderWorklet::OnDirectFromSellerAuctionSignalsDownloadedReportWin(
ReportWinTaskList::iterator task,
DirectFromSellerSignalsRequester::Result result) {
DCHECK_CALLED_ON_VALID_SEQUENCE(user_sequence_checker_);
task->direct_from_seller_result_auction_signals = std::move(result);
task->direct_from_seller_request_auction_signals.reset();
// The two direct from seller signals metrics for tracing are combined since
// they should be roughly the same.
task->wait_direct_from_seller_signals =
std::max(task->wait_direct_from_seller_signals,
base::TimeTicks::Now() - task->trace_wait_deps_start);
RunReportWinIfReady(task);
}
bool BidderWorklet::IsReadyToReportWin(const ReportWinTask& task) const {
return IsCodeReady() && !task.direct_from_seller_request_per_buyer_signals &&
!task.direct_from_seller_request_auction_signals;
}
void BidderWorklet::RunReportWinIfReady(ReportWinTaskList::iterator task) {
DCHECK_CALLED_ON_VALID_SEQUENCE(user_sequence_checker_);
if (!IsReadyToReportWin(*task)) {
return;
}
TRACE_EVENT_NESTABLE_ASYNC_END1(
"fledge", "wait_report_win_deps", task->trace_id, "data",
[&](perfetto::TracedValue trace_context) {
auto dict = std::move(trace_context).WriteDictionary();
if (!task->wait_code.is_zero()) {
dict.Add("wait_code_ms", task->wait_code.InMillisecondsF());
}
if (!task->wait_direct_from_seller_signals.is_zero()) {
dict.Add("wait_direct_from_seller_signals_ms",
task->wait_direct_from_seller_signals.InMillisecondsF());
}
});
TRACE_EVENT_NESTABLE_ASYNC_BEGIN0("fledge", "post_v8_task", task->trace_id);
// Other than the callback field, no fields of `task` are needed after this
// point, so can consume them instead of copying them.
size_t thread_index = GetNextThreadIndex();
cancelable_task_tracker_.PostTask(
v8_runners_[thread_index].get(), FROM_HERE,
base::BindOnce(
&BidderWorklet::V8State::ReportWin,
base::Unretained(v8_state_[thread_index].get()),
std::move(task->is_for_additional_bid),
std::move(task->interest_group_name_reporting_id),
std::move(task->buyer_reporting_id),
std::move(task->buyer_and_seller_reporting_id),
std::move(task->selected_buyer_and_seller_reporting_id),
std::move(task->auction_signals_json),
std::move(task->per_buyer_signals_json),
std::move(task->direct_from_seller_result_per_buyer_signals),
std::move(task->direct_from_seller_per_buyer_signals_header_ad_slot),
std::move(task->direct_from_seller_result_auction_signals),
std::move(task->direct_from_seller_auction_signals_header_ad_slot),
std::move(task->seller_signals_json), std::move(task->kanon_mode),
std::move(task->bid_is_kanon),
std::move(task->browser_signal_render_url),
std::move(task->browser_signal_bid),
std::move(task->browser_signal_bid_currency),
std::move(task->browser_signal_highest_scoring_other_bid),
std::move(task->browser_signal_highest_scoring_other_bid_currency),
std::move(task->browser_signal_made_highest_scoring_other_bid),
std::move(task->browser_signal_ad_cost),
std::move(task->browser_signal_modeling_signals),
std::move(task->browser_signal_join_count),
std::move(task->browser_signal_recency),
std::move(task->browser_signal_seller_origin),
std::move(task->browser_signal_top_level_seller_origin),
std::move(task->browser_signal_reporting_timeout),
std::move(task->bidding_signals_data_version), task->trace_id,
base::BindOnce(&BidderWorklet::DeliverReportWinOnUserThread,
weak_ptr_factory_.GetWeakPtr(), task)));
}
void BidderWorklet::DeliverBidCallbackOnUserThread(
GenerateBidTaskList::iterator task,
std::vector<mojom::BidderWorkletBidPtr> bids,
std::optional<uint32_t> bidding_signals_data_version,
std::optional<GURL> debug_loss_report_url,
std::optional<GURL> debug_win_report_url,
std::optional<double> set_priority,
base::flat_map<std::string, mojom::PrioritySignalsDoublePtr>
update_priority_signals_overrides,
PrivateAggregationRequests pa_requests,
PrivateAggregationRequests non_kanon_pa_requests,
RealTimeReportingContributions real_time_contributions,
base::TimeDelta bidding_latency,
mojom::RejectReason reject_reason,
bool script_timed_out,
std::vector<std::string> error_msgs) {
DCHECK_CALLED_ON_VALID_SEQUENCE(user_sequence_checker_);
// TODO(https://crbug.com): Remove once bug is identified and fixed.
CHECK(!debug_loss_report_url || debug_loss_report_url->is_valid());
CHECK(!debug_win_report_url || debug_win_report_url->is_valid());
error_msgs.insert(error_msgs.end(), load_code_error_msgs_.begin(),
load_code_error_msgs_.end());
if (task->trusted_bidding_signals_error_msg) {
error_msgs.emplace_back(
std::move(task->trusted_bidding_signals_error_msg).value());
}
task->generate_bid_client->OnGenerateBidComplete(
std::move(bids), bidding_signals_data_version, debug_loss_report_url,
debug_win_report_url, set_priority,
std::move(update_priority_signals_overrides), std::move(pa_requests),
std::move(non_kanon_pa_requests), std::move(real_time_contributions),
mojom::BidderTimingMetrics::New(
/*js_fetch_latency=*/js_fetch_latency_,
/*wasm_fetch_latency=*/wasm_fetch_latency_,
/*script_latency=*/bidding_latency,
/*script_timed_out=*/script_timed_out),
mojom::GenerateBidDependencyLatencies::New(
/*code_ready_latency=*/NullOptIfZero(task->wait_code),
/*config_promises_latency=*/NullOptIfZero(task->wait_promises),
/*direct_from_seller_signals_latency=*/
NullOptIfZero(task->wait_direct_from_seller_signals),
/*trusted_bidding_signals_latency=*/
NullOptIfZero(task->wait_trusted_signals),
/*deps_wait_start_time=*/task->trace_wait_deps_start,
/*generate_bid_start_time=*/task->generate_bid_start_time,
/*generate_bid_finish_time=*/base::TimeTicks::Now()),
reject_reason, error_msgs);
CleanUpBidTaskOnUserThread(task);
}
void BidderWorklet::CleanUpBidTaskOnUserThread(
GenerateBidTaskList::iterator task) {
DCHECK_CALLED_ON_VALID_SEQUENCE(user_sequence_checker_);
// Disconnect the FinalizeGenerateBid pipe, if any, since that refers
// to `task` (it generally will be closed already, but may not be if
// GenerateBidClient disconnected before FinalizeGenerateBid was called).
if (task->finalize_generate_bid_receiver_id.has_value()) {
finalize_receiver_set_.Remove(*task->finalize_generate_bid_receiver_id);
}
generate_bid_tasks_.erase(task);
}
void BidderWorklet::DeliverReportWinOnUserThread(
ReportWinTaskList::iterator task,
std::optional<GURL> report_url,
base::flat_map<std::string, GURL> ad_beacon_map,
base::flat_map<std::string, std::string> ad_macro_map,
PrivateAggregationRequests pa_requests,
base::TimeDelta reporting_latency,
bool script_timed_out,
std::vector<std::string> errors) {
DCHECK_CALLED_ON_VALID_SEQUENCE(user_sequence_checker_);
errors.insert(errors.end(), load_code_error_msgs_.begin(),
load_code_error_msgs_.end());
std::move(task->callback)
.Run(std::move(report_url), std::move(ad_beacon_map),
std::move(ad_macro_map), std::move(pa_requests),
mojom::BidderTimingMetrics::New(
/*js_fetch_latency=*/js_fetch_latency_,
/*wasm_fetch_latency=*/wasm_fetch_latency_,
/*script_latency=*/reporting_latency,
/*script_timed_out=*/script_timed_out),
std::move(errors));
report_win_tasks_.erase(task);
}
bool BidderWorklet::IsCodeReady() const {
// If `paused_`, loading hasn't started yet. Otherwise, null loaders indicate
// the worklet script has loaded successfully, and there's no WASM helper, or
// it has also loaded successfully.
return !paused_ && !worklet_loader_ && !wasm_loader_;
}
} // namespace auction_worklet