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
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

//! The script thread is the thread that owns the DOM in memory, runs JavaScript, and triggers
//! layout. It's in charge of processing events for all same-origin pages in a frame
//! tree, and manages the entire lifetime of pages in the frame tree from initial request to
//! teardown.
//!
//! Page loads follow a two-step process. When a request for a new page load is received, the
//! network request is initiated and the relevant data pertaining to the new page is stashed.
//! While the non-blocking request is ongoing, the script thread is free to process further events,
//! noting when they pertain to ongoing loads (such as resizes/viewport adjustments). When the
//! initial response is received for an ongoing load, the second phase starts - the frame tree
//! entry is created, along with the Window and Document objects, and the appropriate parser
//! takes over the response body. Once parsing is complete, the document lifecycle for loading
//! a page runs its course and the script thread returns to processing events in the main event
//! loop.

use std::borrow::Cow;
use std::cell::{Cell, RefCell};
use std::collections::{hash_map, HashMap, HashSet};
use std::default::Default;
use std::option::Option;
use std::rc::Rc;
use std::result::Result;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::thread;
use std::time::{Duration, Instant, SystemTime};

use background_hang_monitor_api::{
    BackgroundHangMonitor, BackgroundHangMonitorExitSignal, HangAnnotation, MonitoredComponentId,
    MonitoredComponentType, ScriptHangAnnotation,
};
use base::cross_process_instant::CrossProcessInstant;
use base::id::{
    BrowsingContextId, HistoryStateId, PipelineId, PipelineNamespace, TopLevelBrowsingContextId,
};
use base::Epoch;
use bluetooth_traits::BluetoothRequest;
use canvas_traits::webgl::WebGLPipeline;
use chrono::{DateTime, Local};
use crossbeam_channel::{select, unbounded, Receiver, Sender};
use devtools_traits::{
    CSSError, DevtoolScriptControlMsg, DevtoolsPageInfo, NavigationState,
    ScriptToDevtoolsControlMsg, WorkerId,
};
use embedder_traits::EmbedderMsg;
use euclid::default::{Point2D, Rect};
use fonts::SystemFontServiceProxy;
use headers::{HeaderMapExt, LastModified, ReferrerPolicy as ReferrerPolicyHeader};
use html5ever::{local_name, namespace_url, ns};
use hyper_serde::Serde;
use ipc_channel::ipc::{self, IpcSender};
use ipc_channel::router::ROUTER;
use js::glue::GetWindowProxyClass;
use js::jsapi::{
    JSContext as UnsafeJSContext, JSTracer, JS_AddInterruptCallback, SetWindowProxyClass,
};
use js::jsval::UndefinedValue;
use js::rust::ParentRuntime;
use media::WindowGLContext;
use metrics::{PaintTimeMetrics, MAX_TASK_NS};
use mime::{self, Mime};
use net_traits::image_cache::{ImageCache, PendingImageResponse};
use net_traits::request::{
    CredentialsMode, Destination, RedirectMode, RequestBuilder, RequestId, RequestMode,
};
use net_traits::storage_thread::StorageType;
use net_traits::{
    FetchMetadata, FetchResponseListener, FetchResponseMsg, Metadata, NetworkError,
    ResourceFetchTiming, ResourceThreads, ResourceTimingType,
};
use percent_encoding::percent_decode;
use profile_traits::mem::{self as profile_mem, OpaqueSender, ReportsChan};
use profile_traits::time::{self as profile_time, ProfilerCategory};
use profile_traits::time_profile;
use script_layout_interface::{
    node_id_from_scroll_id, LayoutConfig, LayoutFactory, ReflowGoal, ScriptThreadFactory,
};
use script_traits::webdriver_msg::WebDriverScriptCommand;
use script_traits::{
    CompositorEvent, ConstellationControlMsg, DiscardBrowsingContext, DocumentActivity,
    EventResult, HistoryEntryReplacement, InitialScriptState, JsEvalResult, LayoutMsg, LoadData,
    LoadOrigin, MediaSessionActionType, MouseButton, MouseEventType, NewLayoutInfo, Painter,
    ProgressiveWebMetricType, ScriptMsg, ScriptToConstellationChan, ScrollState,
    StructuredSerializedData, TimerSchedulerMsg, TouchEventType, TouchId, UntrustedNodeAddress,
    UpdatePipelineIdReason, WheelDelta, WindowSizeData, WindowSizeType,
};
use servo_atoms::Atom;
use servo_config::opts;
use servo_url::{ImmutableOrigin, MutableOrigin, ServoUrl};
use style::dom::OpaqueNode;
use style::thread_state::{self, ThreadState};
use url::Position;
use webgpu::{WebGPUDevice, WebGPUMsg};
use webrender_api::DocumentId;
use webrender_traits::CrossProcessCompositorApi;

use crate::document_loader::DocumentLoader;
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::DocumentBinding::{
    DocumentMethods, DocumentReadyState,
};
use crate::dom::bindings::codegen::Bindings::NavigatorBinding::NavigatorMethods;
use crate::dom::bindings::codegen::Bindings::WindowBinding::WindowMethods;
use crate::dom::bindings::conversions::{
    ConversionResult, FromJSValConvertible, StringificationBehavior,
};
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::refcounted::Trusted;
use crate::dom::bindings::reflector::DomObject;
use crate::dom::bindings::root::{
    Dom, DomRoot, MutNullableDom, RootCollection, ThreadLocalStackRoots,
};
use crate::dom::bindings::settings_stack::AutoEntryScript;
use crate::dom::bindings::str::DOMString;
use crate::dom::bindings::trace::{HashMapTracedValues, JSTraceable};
use crate::dom::customelementregistry::{
    CallbackReaction, CustomElementDefinition, CustomElementReactionStack,
};
use crate::dom::document::{
    Document, DocumentSource, FocusType, HasBrowsingContext, IsHTMLDocument, TouchEventResult,
};
use crate::dom::element::Element;
use crate::dom::event::{Event, EventBubbles, EventCancelable};
use crate::dom::globalscope::GlobalScope;
use crate::dom::htmlanchorelement::HTMLAnchorElement;
use crate::dom::htmliframeelement::HTMLIFrameElement;
use crate::dom::identityhub::IdentityHub;
use crate::dom::mutationobserver::MutationObserver;
use crate::dom::node::{window_from_node, Node, ShadowIncluding};
use crate::dom::performanceentry::PerformanceEntry;
use crate::dom::performancepainttiming::PerformancePaintTiming;
use crate::dom::serviceworker::TrustedServiceWorkerAddress;
use crate::dom::servoparser::{ParserContext, ServoParser};
use crate::dom::uievent::UIEvent;
use crate::dom::window::{ReflowReason, Window};
use crate::dom::windowproxy::{CreatorBrowsingContextInfo, WindowProxy};
use crate::dom::worker::TrustedWorkerAddress;
use crate::dom::worklet::WorkletThreadPool;
use crate::dom::workletglobalscope::WorkletGlobalScopeInit;
use crate::fetch::FetchCanceller;
use crate::microtask::{Microtask, MicrotaskQueue};
use crate::realms::enter_realm;
use crate::script_module::ScriptFetchOptions;
use crate::script_runtime::{
    CanGc, CommonScriptMsg, JSContext, Runtime, ScriptChan, ScriptPort, ScriptThreadEventCategory,
    ThreadSafeJSContext,
};
use crate::task_manager::TaskManager;
use crate::task_queue::{QueuedTask, QueuedTaskConversion, TaskQueue};
use crate::task_source::dom_manipulation::DOMManipulationTaskSource;
use crate::task_source::file_reading::FileReadingTaskSource;
use crate::task_source::gamepad::GamepadTaskSource;
use crate::task_source::history_traversal::HistoryTraversalTaskSource;
use crate::task_source::media_element::MediaElementTaskSource;
use crate::task_source::networking::NetworkingTaskSource;
use crate::task_source::performance_timeline::PerformanceTimelineTaskSource;
use crate::task_source::port_message::PortMessageQueue;
use crate::task_source::remote_event::RemoteEventTaskSource;
use crate::task_source::rendering::RenderingTaskSource;
use crate::task_source::timer::TimerTaskSource;
use crate::task_source::user_interaction::UserInteractionTaskSource;
use crate::task_source::websocket::WebsocketTaskSource;
use crate::task_source::{TaskSource, TaskSourceName};
use crate::{devtools, webdriver_handlers};

pub type ImageCacheMsg = (PipelineId, PendingImageResponse);

thread_local!(static SCRIPT_THREAD_ROOT: Cell<Option<*const ScriptThread>> = const { Cell::new(None) });

fn with_optional_script_thread<R>(f: impl FnOnce(Option<&ScriptThread>) -> R) -> R {
    SCRIPT_THREAD_ROOT.with(|root| {
        f(root
            .get()
            .and_then(|script_thread| unsafe { script_thread.as_ref() }))
    })
}

fn with_script_thread<R: Default>(f: impl FnOnce(&ScriptThread) -> R) -> R {
    with_optional_script_thread(|script_thread| script_thread.map(f).unwrap_or_default())
}

/// # Safety
///
/// The `JSTracer` argument must point to a valid `JSTracer` in memory. In addition,
/// implementors of this method must ensure that all active objects are properly traced
/// or else the garbage collector may end up collecting objects that are still reachable.
pub unsafe fn trace_thread(tr: *mut JSTracer) {
    with_script_thread(|script_thread| {
        debug!("tracing fields of ScriptThread");
        script_thread.trace(tr);
    })
}

/// A document load that is in the process of fetching the requested resource. Contains
/// data that will need to be present when the document and frame tree entry are created,
/// but is only easily available at initiation of the load and on a push basis (so some
/// data will be updated according to future resize events, viewport changes, etc.)
#[derive(JSTraceable)]
struct InProgressLoad {
    /// The pipeline which requested this load.
    #[no_trace]
    pipeline_id: PipelineId,
    /// The browsing context being loaded into.
    #[no_trace]
    browsing_context_id: BrowsingContextId,
    /// The top level ancestor browsing context.
    #[no_trace]
    top_level_browsing_context_id: TopLevelBrowsingContextId,
    /// The parent pipeline and frame type associated with this load, if any.
    #[no_trace]
    parent_info: Option<PipelineId>,
    /// The opener, if this is an auxiliary.
    #[no_trace]
    opener: Option<BrowsingContextId>,
    /// The current window size associated with this pipeline.
    #[no_trace]
    window_size: WindowSizeData,
    /// The activity level of the document (inactive, active or fully active).
    #[no_trace]
    activity: DocumentActivity,
    /// Window is throttled, running timers at a heavily limited rate.
    throttled: bool,
    /// The requested URL of the load.
    #[no_trace]
    url: ServoUrl,
    /// The origin for the document
    #[no_trace]
    origin: MutableOrigin,
    /// Timestamp reporting the time when the browser started this load.
    #[no_trace]
    navigation_start: CrossProcessInstant,
    /// For cancelling the fetch
    canceller: FetchCanceller,
    /// If inheriting the security context
    inherited_secure_context: Option<bool>,
}

impl InProgressLoad {
    /// Create a new InProgressLoad object.
    #[allow(clippy::too_many_arguments)]
    fn new(
        id: PipelineId,
        browsing_context_id: BrowsingContextId,
        top_level_browsing_context_id: TopLevelBrowsingContextId,
        parent_info: Option<PipelineId>,
        opener: Option<BrowsingContextId>,
        window_size: WindowSizeData,
        url: ServoUrl,
        origin: MutableOrigin,
        inherited_secure_context: Option<bool>,
    ) -> InProgressLoad {
        let navigation_start = CrossProcessInstant::now();
        InProgressLoad {
            pipeline_id: id,
            browsing_context_id,
            top_level_browsing_context_id,
            parent_info,
            opener,
            window_size,
            activity: DocumentActivity::FullyActive,
            throttled: false,
            url,
            origin,
            navigation_start,
            canceller: Default::default(),
            inherited_secure_context,
        }
    }
}

#[derive(Debug)]
#[allow(clippy::enum_variant_names)]
enum MixedMessage {
    FromConstellation(ConstellationControlMsg),
    FromScript(MainThreadScriptMsg),
    FromDevtools(DevtoolScriptControlMsg),
    FromImageCache((PipelineId, PendingImageResponse)),
    FromWebGPUServer(WebGPUMsg),
}

/// Messages used to control the script event loop.
#[derive(Debug)]
pub enum MainThreadScriptMsg {
    /// Common variants associated with the script messages
    Common(CommonScriptMsg),
    /// Notifies the script thread that a new worklet has been loaded, and thus the page should be
    /// reflowed.
    WorkletLoaded(PipelineId),
    /// Notifies the script thread that a new paint worklet has been registered.
    RegisterPaintWorklet {
        pipeline_id: PipelineId,
        name: Atom,
        properties: Vec<Atom>,
        painter: Box<dyn Painter>,
    },
    /// A task related to a not fully-active document has been throttled.
    Inactive,
    /// Wake-up call from the task queue.
    WakeUp,
}

impl QueuedTaskConversion for MainThreadScriptMsg {
    fn task_source_name(&self) -> Option<&TaskSourceName> {
        let script_msg = match self {
            MainThreadScriptMsg::Common(script_msg) => script_msg,
            _ => return None,
        };
        match script_msg {
            CommonScriptMsg::Task(_category, _boxed, _pipeline_id, task_source) => {
                Some(task_source)
            },
            _ => None,
        }
    }

    fn pipeline_id(&self) -> Option<PipelineId> {
        let script_msg = match self {
            MainThreadScriptMsg::Common(script_msg) => script_msg,
            _ => return None,
        };
        match script_msg {
            CommonScriptMsg::Task(_category, _boxed, pipeline_id, _task_source) => *pipeline_id,
            _ => None,
        }
    }

    fn into_queued_task(self) -> Option<QueuedTask> {
        let script_msg = match self {
            MainThreadScriptMsg::Common(script_msg) => script_msg,
            _ => return None,
        };
        let (category, boxed, pipeline_id, task_source) = match script_msg {
            CommonScriptMsg::Task(category, boxed, pipeline_id, task_source) => {
                (category, boxed, pipeline_id, task_source)
            },
            _ => return None,
        };
        Some((None, category, boxed, pipeline_id, task_source))
    }

    fn from_queued_task(queued_task: QueuedTask) -> Self {
        let (_worker, category, boxed, pipeline_id, task_source) = queued_task;
        let script_msg = CommonScriptMsg::Task(category, boxed, pipeline_id, task_source);
        MainThreadScriptMsg::Common(script_msg)
    }

    fn inactive_msg() -> Self {
        MainThreadScriptMsg::Inactive
    }

    fn wake_up_msg() -> Self {
        MainThreadScriptMsg::WakeUp
    }

    fn is_wake_up(&self) -> bool {
        matches!(self, MainThreadScriptMsg::WakeUp)
    }
}

impl OpaqueSender<CommonScriptMsg> for Box<dyn ScriptChan + Send> {
    fn send(&self, msg: CommonScriptMsg) {
        ScriptChan::send(&**self, msg).unwrap();
    }
}

impl ScriptPort for Receiver<CommonScriptMsg> {
    fn recv(&self) -> Result<CommonScriptMsg, ()> {
        self.recv().map_err(|_| ())
    }
}

impl ScriptPort for Receiver<MainThreadScriptMsg> {
    fn recv(&self) -> Result<CommonScriptMsg, ()> {
        match self.recv() {
            Ok(MainThreadScriptMsg::Common(script_msg)) => Ok(script_msg),
            Ok(_) => panic!("unexpected main thread event message!"),
            Err(_) => Err(()),
        }
    }
}

impl ScriptPort for Receiver<(TrustedWorkerAddress, CommonScriptMsg)> {
    fn recv(&self) -> Result<CommonScriptMsg, ()> {
        self.recv().map(|(_, msg)| msg).map_err(|_| ())
    }
}

impl ScriptPort for Receiver<(TrustedWorkerAddress, MainThreadScriptMsg)> {
    fn recv(&self) -> Result<CommonScriptMsg, ()> {
        match self.recv().map(|(_, msg)| msg) {
            Ok(MainThreadScriptMsg::Common(script_msg)) => Ok(script_msg),
            Ok(_) => panic!("unexpected main thread event message!"),
            Err(_) => Err(()),
        }
    }
}

impl ScriptPort for Receiver<(TrustedServiceWorkerAddress, CommonScriptMsg)> {
    fn recv(&self) -> Result<CommonScriptMsg, ()> {
        self.recv().map(|(_, msg)| msg).map_err(|_| ())
    }
}

/// Encapsulates internal communication of shared messages within the script thread.
#[derive(JSTraceable)]
pub struct SendableMainThreadScriptChan(#[no_trace] pub Sender<CommonScriptMsg>);

impl ScriptChan for SendableMainThreadScriptChan {
    fn send(&self, msg: CommonScriptMsg) -> Result<(), ()> {
        self.0.send(msg).map_err(|_| ())
    }

    fn clone(&self) -> Box<dyn ScriptChan + Send> {
        Box::new(SendableMainThreadScriptChan((self.0).clone()))
    }
}

/// Encapsulates internal communication of main thread messages within the script thread.
#[derive(JSTraceable)]
pub struct MainThreadScriptChan(#[no_trace] pub Sender<MainThreadScriptMsg>);

impl ScriptChan for MainThreadScriptChan {
    fn send(&self, msg: CommonScriptMsg) -> Result<(), ()> {
        self.0
            .send(MainThreadScriptMsg::Common(msg))
            .map_err(|_| ())
    }

    fn clone(&self) -> Box<dyn ScriptChan + Send> {
        Box::new(MainThreadScriptChan((self.0).clone()))
    }
}

impl OpaqueSender<CommonScriptMsg> for Sender<MainThreadScriptMsg> {
    fn send(&self, msg: CommonScriptMsg) {
        self.send(MainThreadScriptMsg::Common(msg)).unwrap()
    }
}

/// The set of all documents managed by this script thread.
#[derive(JSTraceable)]
#[crown::unrooted_must_root_lint::must_root]
pub struct Documents {
    map: HashMapTracedValues<PipelineId, Dom<Document>>,
}

impl Documents {
    pub fn insert(&mut self, pipeline_id: PipelineId, doc: &Document) {
        self.map.insert(pipeline_id, Dom::from_ref(doc));
    }

    pub fn remove(&mut self, pipeline_id: PipelineId) -> Option<DomRoot<Document>> {
        self.map
            .remove(&pipeline_id)
            .map(|ref doc| DomRoot::from_ref(&**doc))
    }

    pub fn find_document(&self, pipeline_id: PipelineId) -> Option<DomRoot<Document>> {
        self.map
            .get(&pipeline_id)
            .map(|doc| DomRoot::from_ref(&**doc))
    }

    pub fn find_window(&self, pipeline_id: PipelineId) -> Option<DomRoot<Window>> {
        self.find_document(pipeline_id)
            .map(|doc| DomRoot::from_ref(doc.window()))
    }

    pub fn find_global(&self, pipeline_id: PipelineId) -> Option<DomRoot<GlobalScope>> {
        self.find_window(pipeline_id)
            .map(|window| DomRoot::from_ref(window.upcast()))
    }

    pub fn find_iframe(
        &self,
        pipeline_id: PipelineId,
        browsing_context_id: BrowsingContextId,
    ) -> Option<DomRoot<HTMLIFrameElement>> {
        self.find_document(pipeline_id)
            .and_then(|doc| doc.find_iframe(browsing_context_id))
    }

    pub fn iter(&self) -> DocumentsIter<'_> {
        DocumentsIter {
            iter: self.map.iter(),
        }
    }
}

impl Default for Documents {
    #[allow(crown::unrooted_must_root)]
    fn default() -> Self {
        Self {
            map: HashMapTracedValues::new(),
        }
    }
}

#[allow(crown::unrooted_must_root)]
pub struct DocumentsIter<'a> {
    iter: hash_map::Iter<'a, PipelineId, Dom<Document>>,
}

impl<'a> Iterator for DocumentsIter<'a> {
    type Item = (PipelineId, DomRoot<Document>);

    fn next(&mut self) -> Option<(PipelineId, DomRoot<Document>)> {
        self.iter
            .next()
            .map(|(id, doc)| (*id, DomRoot::from_ref(&**doc)))
    }
}

// We borrow the incomplete parser contexts mutably during parsing,
// which is fine except that parsing can trigger evaluation,
// which can trigger GC, and so we can end up tracing the script
// thread during parsing. For this reason, we don't trace the
// incomplete parser contexts during GC.
pub struct IncompleteParserContexts(RefCell<Vec<(PipelineId, ParserContext)>>);

unsafe_no_jsmanaged_fields!(TaskQueue<MainThreadScriptMsg>);

#[derive(JSTraceable)]
// ScriptThread instances are rooted on creation, so this is okay
#[allow(crown::unrooted_must_root)]
pub struct ScriptThread {
    /// <https://html.spec.whatwg.org/multipage/#last-render-opportunity-time>
    last_render_opportunity_time: DomRefCell<Option<Instant>>,
    /// Used to batch rendering opportunities
    #[no_trace]
    update_the_rendering_task_queued_for_pipeline: DomRefCell<HashSet<PipelineId>>,
    /// The documents for pipelines managed by this thread
    documents: DomRefCell<Documents>,
    /// The window proxies known by this thread
    /// TODO: this map grows, but never shrinks. Issue #15258.
    window_proxies: DomRefCell<HashMapTracedValues<BrowsingContextId, Dom<WindowProxy>>>,
    /// A list of data pertaining to loads that have not yet received a network response
    incomplete_loads: DomRefCell<Vec<InProgressLoad>>,
    /// A vector containing parser contexts which have not yet been fully processed
    incomplete_parser_contexts: IncompleteParserContexts,
    /// Image cache for this script thread.
    #[no_trace]
    image_cache: Arc<dyn ImageCache>,
    /// A handle to the resource thread. This is an `Arc` to avoid running out of file descriptors if
    /// there are many iframes.
    #[no_trace]
    resource_threads: ResourceThreads,
    /// A handle to the bluetooth thread.
    #[no_trace]
    bluetooth_thread: IpcSender<BluetoothRequest>,

    /// A queue of tasks to be executed in this script-thread.
    task_queue: TaskQueue<MainThreadScriptMsg>,

    /// The dedicated means of communication with the background-hang-monitor for this script-thread.
    #[no_trace]
    background_hang_monitor: Box<dyn BackgroundHangMonitor>,
    /// A flag set to `true` by the BHM on exit, and checked from within the interrupt handler.
    closing: Arc<AtomicBool>,

    /// A channel to hand out to script thread-based entities that need to be able to enqueue
    /// events in the event queue.
    chan: MainThreadScriptChan,

    dom_manipulation_task_sender: Box<dyn ScriptChan>,

    gamepad_task_sender: Box<dyn ScriptChan>,

    #[no_trace]
    media_element_task_sender: Sender<MainThreadScriptMsg>,

    #[no_trace]
    user_interaction_task_sender: Sender<MainThreadScriptMsg>,

    networking_task_sender: Box<dyn ScriptChan>,

    #[no_trace]
    history_traversal_task_sender: Sender<MainThreadScriptMsg>,

    file_reading_task_sender: Box<dyn ScriptChan>,

    performance_timeline_task_sender: Box<dyn ScriptChan>,

    port_message_sender: Box<dyn ScriptChan>,

    timer_task_sender: Box<dyn ScriptChan>,

    remote_event_task_sender: Box<dyn ScriptChan>,

    rendering_task_sender: Box<dyn ScriptChan>,

    /// A channel to hand out to threads that need to respond to a message from the script thread.
    #[no_trace]
    control_chan: IpcSender<ConstellationControlMsg>,

    /// The port on which the constellation and layout can communicate with the
    /// script thread.
    #[no_trace]
    control_port: Receiver<ConstellationControlMsg>,

    /// For communicating load url messages to the constellation
    #[no_trace]
    script_sender: IpcSender<(PipelineId, ScriptMsg)>,

    /// A sender for layout to communicate to the constellation.
    #[no_trace]
    layout_to_constellation_chan: IpcSender<LayoutMsg>,

    /// A proxy to the `SystemFontService` to use for accessing system font lists.
    #[no_trace]
    system_font_service: Arc<SystemFontServiceProxy>,

    /// The port on which we receive messages from the image cache
    #[no_trace]
    image_cache_port: Receiver<ImageCacheMsg>,

    /// The channel on which the image cache can send messages to ourself.
    #[no_trace]
    image_cache_channel: Sender<ImageCacheMsg>,
    /// For providing contact with the time profiler.
    #[no_trace]
    time_profiler_chan: profile_time::ProfilerChan,

    /// For providing contact with the memory profiler.
    #[no_trace]
    mem_profiler_chan: profile_mem::ProfilerChan,

    /// For providing instructions to an optional devtools server.
    #[no_trace]
    devtools_chan: Option<IpcSender<ScriptToDevtoolsControlMsg>>,
    /// For receiving commands from an optional devtools server. Will be ignored if
    /// no such server exists.
    #[no_trace]
    devtools_port: Receiver<DevtoolScriptControlMsg>,
    #[no_trace]
    devtools_sender: IpcSender<DevtoolScriptControlMsg>,

    /// The JavaScript runtime.
    js_runtime: Rc<Runtime>,

    /// The topmost element over the mouse.
    topmost_mouse_over_target: MutNullableDom<Element>,

    /// List of pipelines that have been owned and closed by this script thread.
    #[no_trace]
    closed_pipelines: DomRefCell<HashSet<PipelineId>>,

    #[no_trace]
    scheduler_chan: IpcSender<TimerSchedulerMsg>,

    #[no_trace]
    content_process_shutdown_chan: Sender<()>,

    /// <https://html.spec.whatwg.org/multipage/#microtask-queue>
    microtask_queue: Rc<MicrotaskQueue>,

    /// Microtask Queue for adding support for mutation observer microtasks
    mutation_observer_microtask_queued: Cell<bool>,

    /// The unit of related similar-origin browsing contexts' list of MutationObserver objects
    mutation_observers: DomRefCell<Vec<Dom<MutationObserver>>>,

    /// A handle to the WebGL thread
    #[no_trace]
    webgl_chan: Option<WebGLPipeline>,

    /// The WebXR device registry
    #[no_trace]
    #[cfg(feature = "webxr")]
    webxr_registry: Option<webxr_api::Registry>,

    /// The worklet thread pool
    worklet_thread_pool: DomRefCell<Option<Rc<WorkletThreadPool>>>,

    /// A list of pipelines containing documents that finished loading all their blocking
    /// resources during a turn of the event loop.
    docs_with_no_blocking_loads: DomRefCell<HashSet<Dom<Document>>>,

    /// <https://html.spec.whatwg.org/multipage/#custom-element-reactions-stack>
    custom_element_reaction_stack: CustomElementReactionStack,

    /// The Webrender Document ID associated with this thread.
    #[no_trace]
    webrender_document: DocumentId,

    /// Cross-process access to the compositor's API.
    #[no_trace]
    compositor_api: CrossProcessCompositorApi,

    /// Periodically print out on which events script threads spend their processing time.
    profile_script_events: bool,

    /// Print Progressive Web Metrics to console.
    print_pwm: bool,

    /// Emits notifications when there is a relayout.
    relayout_event: bool,

    /// True if it is safe to write to the image.
    prepare_for_screenshot: bool,

    /// Unminify Javascript.
    unminify_js: bool,

    /// Directory with stored unminified scripts
    local_script_source: Option<String>,

    /// Unminify Css.
    unminify_css: bool,

    /// Where to load userscripts from, if any. An empty string will load from
    /// the resources/user-agent-js directory, and if the option isn't passed userscripts
    /// won't be loaded
    userscripts_path: Option<String>,

    /// True if headless mode.
    headless: bool,

    /// Replace unpaired surrogates in DOM strings with U+FFFD.
    /// See <https://github.com/servo/servo/issues/6564>
    replace_surrogates: bool,

    /// An optional string allowing the user agent to be set for testing.
    user_agent: Cow<'static, str>,

    /// Application window's GL Context for Media player
    #[no_trace]
    player_context: WindowGLContext,

    /// A set of all nodes ever created in this script thread
    node_ids: DomRefCell<HashSet<String>>,

    /// Code is running as a consequence of a user interaction
    is_user_interacting: Cell<bool>,

    /// Identity manager for WebGPU resources
    #[no_trace]
    gpu_id_hub: Arc<IdentityHub>,

    /// Receiver to receive commands from optional WebGPU server.
    #[no_trace]
    webgpu_port: RefCell<Option<Receiver<WebGPUMsg>>>,

    // Secure context
    inherited_secure_context: Option<bool>,

    /// A factory for making new layouts. This allows layout to depend on script.
    #[no_trace]
    layout_factory: Arc<dyn LayoutFactory>,
}

struct BHMExitSignal {
    closing: Arc<AtomicBool>,
    js_context: ThreadSafeJSContext,
}

impl BackgroundHangMonitorExitSignal for BHMExitSignal {
    fn signal_to_exit(&self) {
        self.closing.store(true, Ordering::SeqCst);
        self.js_context.request_interrupt_callback();
    }
}

#[allow(unsafe_code)]
unsafe extern "C" fn interrupt_callback(_cx: *mut UnsafeJSContext) -> bool {
    let res = ScriptThread::can_continue_running();
    if !res {
        ScriptThread::prepare_for_shutdown();
    }
    res
}

/// In the event of thread panic, all data on the stack runs its destructor. However, there
/// are no reachable, owning pointers to the DOM memory, so it never gets freed by default
/// when the script thread fails. The ScriptMemoryFailsafe uses the destructor bomb pattern
/// to forcibly tear down the JS realms for pages associated with the failing ScriptThread.
struct ScriptMemoryFailsafe<'a> {
    owner: Option<&'a ScriptThread>,
}

impl<'a> ScriptMemoryFailsafe<'a> {
    fn neuter(&mut self) {
        self.owner = None;
    }

    fn new(owner: &'a ScriptThread) -> ScriptMemoryFailsafe<'a> {
        ScriptMemoryFailsafe { owner: Some(owner) }
    }
}

impl<'a> Drop for ScriptMemoryFailsafe<'a> {
    #[allow(crown::unrooted_must_root)]
    fn drop(&mut self) {
        if let Some(owner) = self.owner {
            for (_, document) in owner.documents.borrow().iter() {
                document.window().clear_js_runtime_for_script_deallocation();
            }
        }
    }
}

impl ScriptThreadFactory for ScriptThread {
    fn create(
        state: InitialScriptState,
        layout_factory: Arc<dyn LayoutFactory>,
        system_font_service: Arc<SystemFontServiceProxy>,
        load_data: LoadData,
        user_agent: Cow<'static, str>,
    ) {
        let (script_chan, script_port) = unbounded();
        thread::Builder::new()
            .name(format!("Script{:?}", state.id))
            .spawn(move || {
                thread_state::initialize(ThreadState::SCRIPT | ThreadState::LAYOUT);
                PipelineNamespace::install(state.pipeline_namespace_id);
                TopLevelBrowsingContextId::install(state.top_level_browsing_context_id);
                let roots = RootCollection::new();
                let _stack_roots = ThreadLocalStackRoots::new(&roots);
                let id = state.id;
                let browsing_context_id = state.browsing_context_id;
                let top_level_browsing_context_id = state.top_level_browsing_context_id;
                let parent_info = state.parent_info;
                let opener = state.opener;
                let secure = load_data.inherited_secure_context;
                let mem_profiler_chan = state.mem_profiler_chan.clone();
                let window_size = state.window_size;

                let script_thread = ScriptThread::new(
                    state,
                    script_port,
                    script_chan.clone(),
                    layout_factory,
                    system_font_service,
                    user_agent,
                );

                SCRIPT_THREAD_ROOT.with(|root| {
                    root.set(Some(&script_thread as *const _));
                });

                let mut failsafe = ScriptMemoryFailsafe::new(&script_thread);

                let origin = MutableOrigin::new(load_data.url.origin());
                let new_load = InProgressLoad::new(
                    id,
                    browsing_context_id,
                    top_level_browsing_context_id,
                    parent_info,
                    opener,
                    window_size,
                    load_data.url.clone(),
                    origin,
                    secure,
                );
                script_thread.pre_page_load(new_load, load_data);

                let reporter_name = format!("script-reporter-{:?}", id);
                mem_profiler_chan.run_with_memory_reporting(
                    || {
                        script_thread.start(CanGc::note());
                        let _ = script_thread.content_process_shutdown_chan.send(());
                    },
                    reporter_name,
                    script_chan,
                    CommonScriptMsg::CollectReports,
                );

                // This must always be the very last operation performed before the thread completes
                failsafe.neuter();
            })
            .expect("Thread spawning failed");
    }
}

impl ScriptThread {
    pub fn note_rendering_opportunity(pipeline_id: PipelineId) {
        with_script_thread(|script_thread| {
            script_thread.rendering_opportunity(pipeline_id);
        })
    }

    pub fn runtime_handle() -> ParentRuntime {
        with_optional_script_thread(|script_thread| {
            script_thread.unwrap().js_runtime.prepare_for_new_child()
        })
    }

    pub fn can_continue_running() -> bool {
        with_script_thread(|script_thread| script_thread.can_continue_running_inner())
    }

    pub fn prepare_for_shutdown() {
        with_script_thread(|script_thread| {
            script_thread.prepare_for_shutdown_inner();
        })
    }

    pub fn set_mutation_observer_microtask_queued(value: bool) {
        with_script_thread(|script_thread| {
            script_thread.mutation_observer_microtask_queued.set(value);
        })
    }

    pub fn is_mutation_observer_microtask_queued() -> bool {
        with_script_thread(|script_thread| script_thread.mutation_observer_microtask_queued.get())
    }

    pub fn add_mutation_observer(observer: &MutationObserver) {
        with_script_thread(|script_thread| {
            script_thread
                .mutation_observers
                .borrow_mut()
                .push(Dom::from_ref(observer));
        })
    }

    pub fn get_mutation_observers() -> Vec<DomRoot<MutationObserver>> {
        with_script_thread(|script_thread| {
            script_thread
                .mutation_observers
                .borrow()
                .iter()
                .map(|o| DomRoot::from_ref(&**o))
                .collect()
        })
    }

    pub fn mark_document_with_no_blocked_loads(doc: &Document) {
        with_script_thread(|script_thread| {
            script_thread
                .docs_with_no_blocking_loads
                .borrow_mut()
                .insert(Dom::from_ref(doc));
        })
    }

    pub fn page_headers_available(
        id: &PipelineId,
        metadata: Option<Metadata>,
        can_gc: CanGc,
    ) -> Option<DomRoot<ServoParser>> {
        with_script_thread(|script_thread| {
            script_thread.handle_page_headers_available(id, metadata, can_gc)
        })
    }

    /// Process a single event as if it were the next event
    /// in the queue for this window event-loop.
    /// Returns a boolean indicating whether further events should be processed.
    pub fn process_event(msg: CommonScriptMsg) -> bool {
        with_script_thread(|script_thread| {
            if !script_thread.can_continue_running_inner() {
                return false;
            }
            script_thread.handle_msg_from_script(MainThreadScriptMsg::Common(msg));
            true
        })
    }

    // https://html.spec.whatwg.org/multipage/#await-a-stable-state
    pub fn await_stable_state(task: Microtask) {
        with_script_thread(|script_thread| {
            script_thread
                .microtask_queue
                .enqueue(task, script_thread.get_cx());
        });
    }

    /// Check that two origins are "similar enough",
    /// for now only used to prevent cross-origin JS url evaluation.
    ///
    /// <https://github.com/whatwg/html/issues/2591>
    pub fn check_load_origin(source: &LoadOrigin, target: &ImmutableOrigin) -> bool {
        match (source, target) {
            (LoadOrigin::Constellation, _) | (LoadOrigin::WebDriver, _) => {
                // Always allow loads initiated by the constellation or webdriver.
                true
            },
            (_, ImmutableOrigin::Opaque(_)) => {
                // If the target is opaque, allow.
                // This covers newly created about:blank auxiliaries, and iframe with no src.
                // TODO: https://github.com/servo/servo/issues/22879
                true
            },
            (LoadOrigin::Script(source_origin), _) => source_origin == target,
        }
    }

    /// Step 13 of <https://html.spec.whatwg.org/multipage/#navigate>
    pub fn navigate(
        browsing_context: BrowsingContextId,
        pipeline_id: PipelineId,
        mut load_data: LoadData,
        replace: HistoryEntryReplacement,
    ) {
        with_script_thread(|script_thread| {
            let is_javascript = load_data.url.scheme() == "javascript";
            // If resource is a request whose url's scheme is "javascript"
            // https://html.spec.whatwg.org/multipage/#javascript-protocol
            if is_javascript {
                let window = match script_thread.documents.borrow().find_window(pipeline_id) {
                    None => return,
                    Some(window) => window,
                };
                let global = window.upcast::<GlobalScope>();
                let trusted_global = Trusted::new(global);
                let sender = script_thread.script_sender.clone();
                let task = task!(navigate_javascript: move || {
                    // Important re security. See https://github.com/servo/servo/issues/23373
                    // TODO: check according to https://w3c.github.io/webappsec-csp/#should-block-navigation-request
                    if let Some(window) = trusted_global.root().downcast::<Window>() {
                        if ScriptThread::check_load_origin(&load_data.load_origin, &window.get_url().origin()) {
                            ScriptThread::eval_js_url(&trusted_global.root(), &mut load_data, CanGc::note());
                            sender
                                .send((pipeline_id, ScriptMsg::LoadUrl(load_data, replace)))
                                .unwrap();
                        }
                    }
                });
                global
                    .dom_manipulation_task_source()
                    .queue(task, global.upcast())
                    .expect("Enqueing navigate js task on the DOM manipulation task source failed");
            } else {
                if let Some(ref sender) = script_thread.devtools_chan {
                    let _ = sender.send(ScriptToDevtoolsControlMsg::Navigate(
                        browsing_context,
                        NavigationState::Start(load_data.url.clone()),
                    ));
                }

                script_thread
                    .script_sender
                    .send((pipeline_id, ScriptMsg::LoadUrl(load_data, replace)))
                    .expect("Sending a LoadUrl message to the constellation failed");
            }
        });
    }

    pub fn process_attach_layout(new_layout_info: NewLayoutInfo, origin: MutableOrigin) {
        with_script_thread(|script_thread| {
            let pipeline_id = Some(new_layout_info.new_pipeline_id);
            script_thread.profile_event(
                ScriptThreadEventCategory::AttachLayout,
                pipeline_id,
                || {
                    script_thread.handle_new_layout(new_layout_info, origin);
                },
            )
        });
    }

    pub fn get_top_level_for_browsing_context(
        sender_pipeline: PipelineId,
        browsing_context_id: BrowsingContextId,
    ) -> Option<TopLevelBrowsingContextId> {
        with_script_thread(|script_thread| {
            script_thread.ask_constellation_for_top_level_info(sender_pipeline, browsing_context_id)
        })
    }

    pub fn find_document(id: PipelineId) -> Option<DomRoot<Document>> {
        with_script_thread(|script_thread| script_thread.documents.borrow().find_document(id))
    }

    pub fn set_user_interacting(interacting: bool) {
        with_script_thread(|script_thread| {
            script_thread.is_user_interacting.set(interacting);
        });
    }

    pub fn is_user_interacting() -> bool {
        with_script_thread(|script_thread| script_thread.is_user_interacting.get())
    }

    pub fn get_fully_active_document_ids() -> HashSet<PipelineId> {
        with_script_thread(|script_thread| {
            script_thread
                .documents
                .borrow()
                .iter()
                .filter_map(|(id, document)| {
                    if document.is_fully_active() {
                        Some(id)
                    } else {
                        None
                    }
                })
                .fold(HashSet::new(), |mut set, id| {
                    let _ = set.insert(id);
                    set
                })
        })
    }

    pub fn find_window_proxy(id: BrowsingContextId) -> Option<DomRoot<WindowProxy>> {
        with_script_thread(|script_thread| {
            script_thread
                .window_proxies
                .borrow()
                .get(&id)
                .map(|context| DomRoot::from_ref(&**context))
        })
    }

    pub fn find_window_proxy_by_name(name: &DOMString) -> Option<DomRoot<WindowProxy>> {
        with_script_thread(|script_thread| {
            for (_, proxy) in script_thread.window_proxies.borrow().iter() {
                if proxy.get_name() == *name {
                    return Some(DomRoot::from_ref(&**proxy));
                }
            }
            None
        })
    }

    pub fn worklet_thread_pool() -> Rc<WorkletThreadPool> {
        with_optional_script_thread(|script_thread| {
            let script_thread = script_thread.unwrap();
            script_thread
                .worklet_thread_pool
                .borrow_mut()
                .get_or_insert_with(|| {
                    let init = WorkletGlobalScopeInit {
                        to_script_thread_sender: script_thread.chan.0.clone(),
                        resource_threads: script_thread.resource_threads.clone(),
                        mem_profiler_chan: script_thread.mem_profiler_chan.clone(),
                        time_profiler_chan: script_thread.time_profiler_chan.clone(),
                        devtools_chan: script_thread.devtools_chan.clone(),
                        to_constellation_sender: script_thread.script_sender.clone(),
                        scheduler_chan: script_thread.scheduler_chan.clone(),
                        image_cache: script_thread.image_cache.clone(),
                        is_headless: script_thread.headless,
                        user_agent: script_thread.user_agent.clone(),
                        gpu_id_hub: script_thread.gpu_id_hub.clone(),
                        inherited_secure_context: script_thread.inherited_secure_context,
                    };
                    Rc::new(WorkletThreadPool::spawn(init))
                })
                .clone()
        })
    }

    fn handle_register_paint_worklet(
        &self,
        pipeline_id: PipelineId,
        name: Atom,
        properties: Vec<Atom>,
        painter: Box<dyn Painter>,
    ) {
        let Some(window) = self.documents.borrow().find_window(pipeline_id) else {
            warn!("Paint worklet registered after pipeline {pipeline_id} closed.");
            return;
        };

        window
            .layout_mut()
            .register_paint_worklet_modules(name, properties, painter);
    }

    pub fn push_new_element_queue() {
        with_script_thread(|script_thread| {
            script_thread
                .custom_element_reaction_stack
                .push_new_element_queue();
        })
    }

    pub fn pop_current_element_queue(can_gc: CanGc) {
        with_script_thread(|script_thread| {
            script_thread
                .custom_element_reaction_stack
                .pop_current_element_queue(can_gc);
        })
    }

    pub fn enqueue_callback_reaction(
        element: &Element,
        reaction: CallbackReaction,
        definition: Option<Rc<CustomElementDefinition>>,
    ) {
        with_script_thread(|script_thread| {
            script_thread
                .custom_element_reaction_stack
                .enqueue_callback_reaction(element, reaction, definition);
        })
    }

    pub fn enqueue_upgrade_reaction(element: &Element, definition: Rc<CustomElementDefinition>) {
        with_script_thread(|script_thread| {
            script_thread
                .custom_element_reaction_stack
                .enqueue_upgrade_reaction(element, definition);
        })
    }

    pub fn invoke_backup_element_queue(can_gc: CanGc) {
        with_script_thread(|script_thread| {
            script_thread
                .custom_element_reaction_stack
                .invoke_backup_element_queue(can_gc);
        })
    }

    pub fn save_node_id(node_id: String) {
        with_script_thread(|script_thread| {
            script_thread.node_ids.borrow_mut().insert(node_id);
        })
    }

    pub fn has_node_id(node_id: &str) -> bool {
        with_script_thread(|script_thread| script_thread.node_ids.borrow().contains(node_id))
    }

    /// Creates a new script thread.
    pub fn new(
        state: InitialScriptState,
        port: Receiver<MainThreadScriptMsg>,
        chan: Sender<MainThreadScriptMsg>,
        layout_factory: Arc<dyn LayoutFactory>,
        system_font_service: Arc<SystemFontServiceProxy>,
        user_agent: Cow<'static, str>,
    ) -> ScriptThread {
        let opts = opts::get();
        let prepare_for_screenshot =
            opts.output_file.is_some() || opts.exit_after_load || opts.webdriver_port.is_some();

        let boxed_script_sender = Box::new(MainThreadScriptChan(chan.clone()));

        let runtime = Runtime::new(Some(NetworkingTaskSource(
            boxed_script_sender.clone(),
            state.id,
        )));
        let cx = runtime.cx();

        unsafe {
            SetWindowProxyClass(cx, GetWindowProxyClass());
            JS_AddInterruptCallback(cx, Some(interrupt_callback));
        }

        // Ask the router to proxy IPC messages from the devtools to us.
        let (ipc_devtools_sender, ipc_devtools_receiver) = ipc::channel().unwrap();
        let devtools_port =
            ROUTER.route_ipc_receiver_to_new_crossbeam_receiver(ipc_devtools_receiver);

        let (image_cache_channel, image_cache_port) = unbounded();

        let task_queue = TaskQueue::new(port, chan.clone());

        let closing = Arc::new(AtomicBool::new(false));
        let background_hang_monitor_exit_signal = BHMExitSignal {
            closing: closing.clone(),
            js_context: runtime.thread_safe_js_context(),
        };

        let background_hang_monitor = state.background_hang_monitor_register.register_component(
            MonitoredComponentId(state.id, MonitoredComponentType::Script),
            Duration::from_millis(1000),
            Duration::from_millis(5000),
            Some(Box::new(background_hang_monitor_exit_signal)),
        );

        // Ask the router to proxy IPC messages from the control port to us.
        let control_port = ROUTER.route_ipc_receiver_to_new_crossbeam_receiver(state.control_port);

        ScriptThread {
            documents: DomRefCell::new(Documents::default()),
            last_render_opportunity_time: Default::default(),
            update_the_rendering_task_queued_for_pipeline: Default::default(),
            window_proxies: DomRefCell::new(HashMapTracedValues::new()),
            incomplete_loads: DomRefCell::new(vec![]),
            incomplete_parser_contexts: IncompleteParserContexts(RefCell::new(vec![])),

            image_cache: state.image_cache.clone(),
            image_cache_channel,
            image_cache_port,

            resource_threads: state.resource_threads,
            bluetooth_thread: state.bluetooth_thread,

            task_queue,

            background_hang_monitor,
            closing,

            chan: MainThreadScriptChan(chan.clone()),
            dom_manipulation_task_sender: boxed_script_sender.clone(),
            gamepad_task_sender: boxed_script_sender.clone(),
            media_element_task_sender: chan.clone(),
            user_interaction_task_sender: chan.clone(),
            networking_task_sender: boxed_script_sender.clone(),
            port_message_sender: boxed_script_sender.clone(),
            file_reading_task_sender: boxed_script_sender.clone(),
            performance_timeline_task_sender: boxed_script_sender.clone(),
            timer_task_sender: boxed_script_sender.clone(),
            remote_event_task_sender: boxed_script_sender.clone(),
            rendering_task_sender: boxed_script_sender.clone(),

            history_traversal_task_sender: chan.clone(),

            control_chan: state.control_chan,
            control_port,
            script_sender: state.script_to_constellation_chan.sender.clone(),
            time_profiler_chan: state.time_profiler_chan.clone(),
            mem_profiler_chan: state.mem_profiler_chan,

            devtools_chan: state.devtools_chan,
            devtools_port,
            devtools_sender: ipc_devtools_sender,

            microtask_queue: runtime.microtask_queue.clone(),

            js_runtime: Rc::new(runtime),
            topmost_mouse_over_target: MutNullableDom::new(Default::default()),
            closed_pipelines: DomRefCell::new(HashSet::new()),

            scheduler_chan: state.scheduler_chan,

            content_process_shutdown_chan: state.content_process_shutdown_chan,

            mutation_observer_microtask_queued: Default::default(),

            mutation_observers: Default::default(),

            layout_to_constellation_chan: state.layout_to_constellation_chan,
            system_font_service,

            webgl_chan: state.webgl_chan,
            #[cfg(feature = "webxr")]
            webxr_registry: state.webxr_registry,

            worklet_thread_pool: Default::default(),

            docs_with_no_blocking_loads: Default::default(),

            custom_element_reaction_stack: CustomElementReactionStack::new(),

            webrender_document: state.webrender_document,
            compositor_api: state.compositor_api,

            profile_script_events: opts.debug.profile_script_events,
            print_pwm: opts.print_pwm,
            relayout_event: opts.debug.relayout_event,

            prepare_for_screenshot,
            unminify_js: opts.unminify_js,
            local_script_source: opts.local_script_source.clone(),
            unminify_css: opts.unminify_css,

            userscripts_path: opts.userscripts.clone(),
            headless: opts.headless,
            replace_surrogates: opts.debug.replace_surrogates,
            user_agent,
            player_context: state.player_context,

            node_ids: Default::default(),
            is_user_interacting: Cell::new(false),
            gpu_id_hub: Arc::new(IdentityHub::default()),
            webgpu_port: RefCell::new(None),
            inherited_secure_context: state.inherited_secure_context,
            layout_factory,
        }
    }

    #[allow(unsafe_code)]
    pub fn get_cx(&self) -> JSContext {
        unsafe { JSContext::from_ptr(self.js_runtime.cx()) }
    }

    /// Check if we are closing.
    fn can_continue_running_inner(&self) -> bool {
        if self.closing.load(Ordering::SeqCst) {
            return false;
        }
        true
    }

    /// We are closing, ensure no script can run and potentially hang.
    fn prepare_for_shutdown_inner(&self) {
        let docs = self.documents.borrow();
        for (_, document) in docs.iter() {
            let window = document.window();
            window.ignore_all_tasks();
        }
    }

    /// Starts the script thread. After calling this method, the script thread will loop receiving
    /// messages on its port.
    pub fn start(&self, can_gc: CanGc) {
        debug!("Starting script thread.");
        while self.handle_msgs(can_gc) {
            // Go on...
            debug!("Running script thread.");
        }
        debug!("Stopped script thread.");
    }

    /// <https://drafts.csswg.org/cssom-view/#document-run-the-resize-steps>
    fn run_the_resize_steps(
        &self,
        id: PipelineId,
        size: WindowSizeData,
        size_type: WindowSizeType,
        can_gc: CanGc,
    ) {
        self.profile_event(ScriptThreadEventCategory::Resize, Some(id), || {
            self.handle_resize_event(id, size, size_type, can_gc);
        });
    }

    /// Process a compositor mouse move event.
    fn process_mouse_move_event(
        &self,
        document: &Document,
        window: &Window,
        point: Point2D<f32>,
        node_address: Option<UntrustedNodeAddress>,
        pressed_mouse_buttons: u16,
        can_gc: CanGc,
    ) {
        // Get the previous target temporarily
        let prev_mouse_over_target = self.topmost_mouse_over_target.get();

        unsafe {
            document.handle_mouse_move_event(
                point,
                &self.topmost_mouse_over_target,
                node_address,
                pressed_mouse_buttons,
                can_gc,
            )
        }

        // Short-circuit if nothing changed
        if self.topmost_mouse_over_target.get() == prev_mouse_over_target {
            return;
        }

        let mut state_already_changed = false;

        // Notify Constellation about the topmost anchor mouse over target.
        if let Some(target) = self.topmost_mouse_over_target.get() {
            if let Some(anchor) = target
                .upcast::<Node>()
                .inclusive_ancestors(ShadowIncluding::No)
                .filter_map(DomRoot::downcast::<HTMLAnchorElement>)
                .next()
            {
                let status = anchor
                    .upcast::<Element>()
                    .get_attribute(&ns!(), &local_name!("href"))
                    .and_then(|href| {
                        let value = href.value();
                        let url = document.url();
                        url.join(&value).map(|url| url.to_string()).ok()
                    });
                let event = EmbedderMsg::Status(status);
                window.send_to_embedder(event);

                state_already_changed = true;
            }
        }

        // We might have to reset the anchor state
        if !state_already_changed {
            if let Some(target) = prev_mouse_over_target {
                if target
                    .upcast::<Node>()
                    .inclusive_ancestors(ShadowIncluding::No)
                    .filter_map(DomRoot::downcast::<HTMLAnchorElement>)
                    .next()
                    .is_some()
                {
                    let event = EmbedderMsg::Status(None);
                    window.send_to_embedder(event);
                }
            }
        }
    }

    /// Process compositor events as part of a "update the rendering task".
    fn process_pending_compositor_events(&self, pipeline_id: PipelineId, can_gc: CanGc) {
        let Some(document) = self.documents.borrow().find_document(pipeline_id) else {
            warn!("Processing pending compositor events for closed pipeline {pipeline_id}.");
            return;
        };
        // Do not handle events if the BC has been, or is being, discarded
        if document.window().Closed() {
            warn!("Compositor event sent to a pipeline with a closed window {pipeline_id}.");
            return;
        }
        ScriptThread::set_user_interacting(true);

        let window = document.window();
        let _realm = enter_realm(document.window());
        for event in document.take_pending_compositor_events().into_iter() {
            match event {
                CompositorEvent::ResizeEvent(new_size, size_type) => {
                    window.add_resize_event(new_size, size_type);
                },

                CompositorEvent::MouseButtonEvent(
                    event_type,
                    button,
                    point,
                    node_address,
                    point_in_node,
                    pressed_mouse_buttons,
                ) => {
                    self.handle_mouse_button_event(
                        pipeline_id,
                        event_type,
                        button,
                        point,
                        node_address,
                        point_in_node,
                        pressed_mouse_buttons,
                        can_gc,
                    );
                },

                CompositorEvent::MouseMoveEvent(point, node_address, pressed_mouse_buttons) => {
                    self.process_mouse_move_event(
                        &document,
                        window,
                        point,
                        node_address,
                        pressed_mouse_buttons,
                        can_gc,
                    );
                },

                CompositorEvent::TouchEvent(event_type, identifier, point, node_address) => {
                    let touch_result = self.handle_touch_event(
                        pipeline_id,
                        event_type,
                        identifier,
                        point,
                        node_address,
                        can_gc,
                    );
                    match (event_type, touch_result) {
                        (TouchEventType::Down, TouchEventResult::Processed(handled)) => {
                            let result = if handled {
                                // TODO: Wait to see if preventDefault is called on the first touchmove event.
                                EventResult::DefaultAllowed
                            } else {
                                EventResult::DefaultPrevented
                            };
                            let message = ScriptMsg::TouchEventProcessed(result);
                            self.script_sender.send((pipeline_id, message)).unwrap();
                        },
                        _ => {
                            // TODO: Calling preventDefault on a touchup event should prevent clicks.
                        },
                    }
                },

                CompositorEvent::WheelEvent(delta, point, node_address) => {
                    self.handle_wheel_event(pipeline_id, delta, point, node_address, can_gc);
                },

                CompositorEvent::KeyboardEvent(key_event) => {
                    document.dispatch_key_event(key_event, can_gc);
                },

                CompositorEvent::IMEDismissedEvent => {
                    document.ime_dismissed(can_gc);
                },

                CompositorEvent::CompositionEvent(composition_event) => {
                    document.dispatch_composition_event(composition_event, can_gc);
                },

                CompositorEvent::GamepadEvent(gamepad_event) => {
                    let global = window.upcast::<GlobalScope>();
                    global.handle_gamepad_event(gamepad_event);
                },
            }
        }
        ScriptThread::set_user_interacting(false);
    }

    /// <https://html.spec.whatwg.org/multipage/#update-the-rendering>
    fn update_the_rendering(&self, can_gc: CanGc) {
        self.update_the_rendering_task_queued_for_pipeline
            .borrow_mut()
            .clear();

        if !self.can_continue_running_inner() {
            return;
        }

        // Run rafs for all pipeline, if a raf tick was received for any.
        // This ensures relative ordering of rafs between parent doc and iframes.
        let should_run_rafs = self
            .documents
            .borrow()
            .iter()
            .any(|(_, doc)| doc.has_received_raf_tick());

        // TODO: The specification says to filter out non-renderable documents,
        // as well as those for which a rendering update would be unnecessary,
        // but this isn't happening here.
        let pipelines_to_update: Vec<PipelineId> = self
            .documents
            .borrow()
            .iter()
            .filter(|(_, document)| document.window().is_top_level())
            .flat_map(|(id, document)| {
                std::iter::once(id).chain(
                    document
                        .iter_iframes()
                        .filter_map(|iframe| iframe.pipeline_id()),
                )
            })
            .collect();
        // Note: the spec reads: "for doc in docs" at each step
        // whereas this runs all steps per doc in docs.
        for pipeline_id in pipelines_to_update {
            // This document is not managed by this script thread. This can happen is the pipeline is
            // unexpectedly closed or simply that it is managed by a different script thread.
            // TODO: It would be better if iframes knew whether or not their Document was managed
            // by the same script thread.
            let Some(document) = self.documents.borrow().find_document(pipeline_id) else {
                continue;
            };
            // TODO(#32004): The rendering should be updated according parent and shadow root order
            // in the specification, but this isn't happening yet.

            // TODO(#31581): The steps in the "Revealing the document" section need to be implemente
            // `process_pending_compositor_events` handles the focusing steps as well as other events
            // from the compositor.

            // TODO: Should this be broken and to match the specification more closely? For instance see
            // https://html.spec.whatwg.org/multipage/#flush-autofocus-candidates.
            self.process_pending_compositor_events(pipeline_id, can_gc);

            // TODO(#31665): Implement the "run the scroll steps" from
            // https://drafts.csswg.org/cssom-view/#document-run-the-scroll-steps.

            if let Some((size, size_type)) = document.window().take_unhandled_resize_event() {
                // Resize steps.
                self.run_the_resize_steps(pipeline_id, size, size_type, can_gc);

                // Evaluate media queries and report changes.
                document
                    .window()
                    .evaluate_media_queries_and_report_changes(can_gc);

                // https://html.spec.whatwg.org/multipage/#img-environment-changes
                // As per the spec, this can be run at any time.
                document.react_to_environment_changes()
            }

            // Update animations and send events.
            self.update_animations_and_send_events(can_gc);

            // TODO(#31866): Implement "run the fullscreen steps" from
            // https://fullscreen.spec.whatwg.org/multipage/#run-the-fullscreen-steps.

            // TODO(#31868): Implement the "context lost steps" from
            // https://html.spec.whatwg.org/multipage/#context-lost-steps.

            // Run the animation frame callbacks.
            document.tick_all_animations(should_run_rafs, can_gc);

            // Run the resize observer steps.
            let _realm = enter_realm(&*document);
            let mut depth = Default::default();
            while document.gather_active_resize_observations_at_depth(&depth, can_gc) {
                // Note: this will reflow the doc.
                depth = document.broadcast_active_resize_observations(can_gc);
            }

            if document.has_skipped_resize_observations() {
                document.deliver_resize_loop_error_notification(can_gc);
            }

            // TODO(#31870): Implement step 17: if the focused area of doc is not a focusable area,
            // then run the focusing steps for document's viewport.

            // TODO: Perform pending transition operations from
            // https://drafts.csswg.org/css-view-transitions/#perform-pending-transition-operations.

            // TODO(#31021): Run the update intersection observations steps from
            // https://w3c.github.io/IntersectionObserver/#run-the-update-intersection-observations-steps

            // TODO: Mark paint timing from https://w3c.github.io/paint-timing.

            // TODO(#31871): Update the rendering: consolidate all reflow calls into one here?

            // TODO: Process top layer removals according to
            // https://drafts.csswg.org/css-position-4/#process-top-layer-removals.
        }
    }

    /// <https://html.spec.whatwg.org/multipage/#event-loop-processing-model:rendering-opportunity>
    fn rendering_opportunity(&self, pipeline_id: PipelineId) {
        *self.last_render_opportunity_time.borrow_mut() = Some(Instant::now());

        // Note: the pipeline should be a navigable with a rendering opportunity,
        // and we should use this opportunity to queue one task for each navigable with
        // an opportunity in this script-thread.
        let Some(document) = self.documents.borrow().find_document(pipeline_id) else {
            warn!("Trying to update the rendering for closed pipeline {pipeline_id}.");
            return;
        };
        let window = document.window();
        let task_manager = window.task_manager();
        let rendering_task_source = task_manager.rendering_task_source();
        let canceller = task_manager.task_canceller(TaskSourceName::Rendering);

        if !self
            .update_the_rendering_task_queued_for_pipeline
            .borrow_mut()
            .insert(pipeline_id)
        {
            return;
        }

        // Queues a task to update the rendering.
        // <https://html.spec.whatwg.org/multipage/#event-loop-processing-model:queue-a-global-task>
        let _ = rendering_task_source.queue_with_canceller(
            task!(update_the_rendering: move || {
                // Note: spec says to queue a task using the navigable's active window,
                // but then updates the rendering for all docs in the same event-loop.
                with_script_thread(|script_thread| {
                    script_thread.update_the_rendering(CanGc::note());
                })
            }),
            &canceller,
        );
    }

    /// Handle incoming messages from other tasks and the task queue.
    fn handle_msgs(&self, can_gc: CanGc) -> bool {
        use self::MixedMessage::{
            FromConstellation, FromDevtools, FromImageCache, FromScript, FromWebGPUServer,
        };

        // Proritize rendering tasks and others, and gather all other events as `sequential`.
        let mut sequential = vec![];

        // Notify the background-hang-monitor we are waiting for an event.
        self.background_hang_monitor.notify_wait();

        // Receive at least one message so we don't spinloop.
        debug!("Waiting for event.");
        let mut event = select! {
            recv(self.task_queue.select()) -> msg => {
                self.task_queue.take_tasks(msg.unwrap());
                let event = self
                    .task_queue
                    .recv()
                    .expect("Spurious wake-up of the event-loop, task-queue has no tasks available");
                FromScript(event)
            },
            recv(self.control_port) -> msg => FromConstellation(msg.unwrap()),
            recv(self.devtools_chan.as_ref().map(|_| &self.devtools_port).unwrap_or(&crossbeam_channel::never())) -> msg
                => FromDevtools(msg.unwrap()),
            recv(self.image_cache_port) -> msg => FromImageCache(msg.unwrap()),
            recv(self.webgpu_port.borrow().as_ref().unwrap_or(&crossbeam_channel::never())) -> msg
                => FromWebGPUServer(msg.unwrap()),
        };
        debug!("Got event.");

        // Prioritize only a single update of the rendering;
        // others will run together with the other sequential tasks.
        let mut rendering_update_already_prioritized = false;

        loop {
            let pipeline_id = self.message_to_pipeline(&event);
            let _realm = pipeline_id.map(|id| {
                let global = self.documents.borrow().find_global(id);
                global.map(|global| enter_realm(&*global))
            });

            // https://html.spec.whatwg.org/multipage/#event-loop-processing-model step 7
            match event {
                // This has to be handled before the ResizeMsg below,
                // otherwise the page may not have been added to the
                // child list yet, causing the find() to fail.
                FromConstellation(ConstellationControlMsg::AttachLayout(new_layout_info)) => {
                    let pipeline_id = new_layout_info.new_pipeline_id;
                    self.profile_event(
                        ScriptThreadEventCategory::AttachLayout,
                        Some(pipeline_id),
                        || {
                            // If this is an about:blank or about:srcdoc load, it must share the
                            // creator's origin. This must match the logic in the constellation
                            // when creating a new pipeline
                            let not_an_about_blank_and_about_srcdoc_load =
                                new_layout_info.load_data.url.as_str() != "about:blank" &&
                                    new_layout_info.load_data.url.as_str() != "about:srcdoc";
                            let origin = if not_an_about_blank_and_about_srcdoc_load {
                                MutableOrigin::new(new_layout_info.load_data.url.origin())
                            } else if let Some(parent) =
                                new_layout_info.parent_info.and_then(|pipeline_id| {
                                    self.documents.borrow().find_document(pipeline_id)
                                })
                            {
                                parent.origin().clone()
                            } else if let Some(creator) = new_layout_info
                                .load_data
                                .creator_pipeline_id
                                .and_then(|pipeline_id| {
                                    self.documents.borrow().find_document(pipeline_id)
                                })
                            {
                                creator.origin().clone()
                            } else {
                                MutableOrigin::new(ImmutableOrigin::new_opaque())
                            };

                            self.handle_new_layout(new_layout_info, origin);
                        },
                    )
                },
                FromConstellation(ConstellationControlMsg::Resize(id, size, size_type)) => {
                    self.handle_resize_message(id, size, size_type);
                },
                FromConstellation(ConstellationControlMsg::Viewport(id, rect)) => self
                    .profile_event(ScriptThreadEventCategory::SetViewport, Some(id), || {
                        self.handle_viewport(id, rect, can_gc);
                    }),
                FromConstellation(ConstellationControlMsg::TickAllAnimations(
                    pipeline_id,
                    tick_type,
                )) => {
                    if let Some(doc) = self.documents.borrow().find_document(pipeline_id) {
                        self.rendering_opportunity(pipeline_id);
                        doc.note_pending_animation_tick(tick_type);
                    } else {
                        warn!(
                            "Trying to note pending animation tick for closed pipeline {}.",
                            pipeline_id
                        )
                    }
                },
                FromConstellation(ConstellationControlMsg::SendEvent(id, event)) => {
                    self.handle_event(id, event)
                },
                FromScript(MainThreadScriptMsg::Common(CommonScriptMsg::Task(
                    category,
                    task,
                    pipeline_id,
                    TaskSourceName::Rendering,
                ))) => {
                    if rendering_update_already_prioritized {
                        // If we've already updated the rendering,
                        // run this task along with the other non-prioritized ones.
                        sequential.push(FromScript(MainThreadScriptMsg::Common(
                            CommonScriptMsg::Task(
                                category,
                                task,
                                pipeline_id,
                                TaskSourceName::Rendering,
                            ),
                        )));
                    } else {
                        // Run the "update the rendering" task.
                        task.run_box();
                        // Always perform a microtrask checkpoint after running a task.
                        self.perform_a_microtask_checkpoint(can_gc);
                        rendering_update_already_prioritized = true;
                    }
                },
                FromScript(MainThreadScriptMsg::Inactive) => {
                    // An event came-in from a document that is not fully-active, it has been stored by the task-queue.
                    // Continue without adding it to "sequential".
                },
                FromConstellation(ConstellationControlMsg::ExitFullScreen(id)) => self
                    .profile_event(ScriptThreadEventCategory::ExitFullscreen, Some(id), || {
                        self.handle_exit_fullscreen(id, can_gc);
                    }),
                _ => {
                    sequential.push(event);
                },
            }

            // If any of our input sources has an event pending, we'll perform another iteration
            // and check for more resize events. If there are no events pending, we'll move
            // on and execute the sequential non-resize events we've seen.
            match self.control_port.try_recv() {
                Err(_) => match self.task_queue.take_tasks_and_recv() {
                    Err(_) => match self.devtools_port.try_recv() {
                        Err(_) => match self.image_cache_port.try_recv() {
                            Err(_) => match &*self.webgpu_port.borrow() {
                                Some(p) => match p.try_recv() {
                                    Err(_) => break,
                                    Ok(ev) => event = FromWebGPUServer(ev),
                                },
                                None => break,
                            },
                            Ok(ev) => event = FromImageCache(ev),
                        },
                        Ok(ev) => event = FromDevtools(ev),
                    },
                    Ok(ev) => event = FromScript(ev),
                },
                Ok(ev) => event = FromConstellation(ev),
            }
        }

        // Process the gathered events.
        debug!("Processing events.");
        for msg in sequential {
            debug!("Processing event {:?}.", msg);
            let category = self.categorize_msg(&msg);
            let pipeline_id = self.message_to_pipeline(&msg);

            let _realm = pipeline_id.and_then(|id| {
                let global = self.documents.borrow().find_global(id);
                global.map(|global| enter_realm(&*global))
            });

            if self.closing.load(Ordering::SeqCst) {
                // If we've received the closed signal from the BHM, only handle exit messages.
                match msg {
                    FromConstellation(ConstellationControlMsg::ExitScriptThread) => {
                        self.handle_exit_script_thread_msg(can_gc);
                        return false;
                    },
                    FromConstellation(ConstellationControlMsg::ExitPipeline(
                        pipeline_id,
                        discard_browsing_context,
                    )) => {
                        self.handle_exit_pipeline_msg(
                            pipeline_id,
                            discard_browsing_context,
                            can_gc,
                        );
                    },
                    _ => {},
                }
                continue;
            }

            let result = self.profile_event(category, pipeline_id, move || {
                match msg {
                    FromConstellation(ConstellationControlMsg::ExitScriptThread) => {
                        self.handle_exit_script_thread_msg(can_gc);
                        return Some(false);
                    },
                    FromConstellation(inner_msg) => {
                        self.handle_msg_from_constellation(inner_msg, can_gc)
                    },
                    FromScript(inner_msg) => self.handle_msg_from_script(inner_msg),
                    FromDevtools(inner_msg) => self.handle_msg_from_devtools(inner_msg, can_gc),
                    FromImageCache(inner_msg) => self.handle_msg_from_image_cache(inner_msg),
                    FromWebGPUServer(inner_msg) => {
                        self.handle_msg_from_webgpu_server(inner_msg, can_gc)
                    },
                }

                None
            });

            if let Some(retval) = result {
                return retval;
            }

            // https://html.spec.whatwg.org/multipage/#event-loop-processing-model step 6
            // TODO(#32003): A microtask checkpoint is only supposed to be performed after running a task.
            self.perform_a_microtask_checkpoint(can_gc);
        }

        {
            // https://html.spec.whatwg.org/multipage/#the-end step 6
            let mut docs = self.docs_with_no_blocking_loads.borrow_mut();
            for document in docs.iter() {
                let _realm = enter_realm(&**document);
                document.maybe_queue_document_completion();

                // Document load is a rendering opportunity.
                ScriptThread::note_rendering_opportunity(document.window().pipeline_id());
            }
            docs.clear();
        }

        // https://html.spec.whatwg.org/multipage/#event-loop-processing-model step 7.12

        // Issue batched reflows on any pages that require it (e.g. if images loaded)
        // TODO(gw): In the future we could probably batch other types of reflows
        // into this loop too, but for now it's only images.
        debug!("Issuing batched reflows.");
        for (_, document) in self.documents.borrow().iter() {
            // Step 13
            if !document.is_fully_active() {
                continue;
            }
            let window = document.window();

            let _realm = enter_realm(&*document);

            window
                .upcast::<GlobalScope>()
                .perform_a_dom_garbage_collection_checkpoint();

            let pending_reflows = window.get_pending_reflow_count();
            if pending_reflows > 0 {
                window.reflow(ReflowGoal::Full, ReflowReason::PendingReflow, can_gc);
            } else {
                // Reflow currently happens when explicitly invoked by code that
                // knows the document could have been modified. This should really
                // be driven by the compositor on an as-needed basis instead, to
                // minimize unnecessary work.
                window.reflow(
                    ReflowGoal::Full,
                    ReflowReason::MissingExplicitReflow,
                    can_gc,
                );
            }
        }

        true
    }

    // Perform step 7.10 from https://html.spec.whatwg.org/multipage/#event-loop-processing-model.
    // Described at: https://drafts.csswg.org/web-animations-1/#update-animations-and-send-events
    fn update_animations_and_send_events(&self, can_gc: CanGc) {
        for (_, document) in self.documents.borrow().iter() {
            document.update_animation_timeline();
            document.maybe_mark_animating_nodes_as_dirty();
        }

        for (_, document) in self.documents.borrow().iter() {
            let _realm = enter_realm(&*document);
            document
                .animations()
                .send_pending_events(document.window(), can_gc);
        }
    }

    fn categorize_msg(&self, msg: &MixedMessage) -> ScriptThreadEventCategory {
        match *msg {
            MixedMessage::FromConstellation(ref inner_msg) => match *inner_msg {
                ConstellationControlMsg::SendEvent(_, _) => ScriptThreadEventCategory::DomEvent,
                _ => ScriptThreadEventCategory::ConstellationMsg,
            },
            // TODO https://github.com/servo/servo/issues/18998
            MixedMessage::FromDevtools(_) => ScriptThreadEventCategory::DevtoolsMsg,
            MixedMessage::FromImageCache(_) => ScriptThreadEventCategory::ImageCacheMsg,
            MixedMessage::FromScript(ref inner_msg) => match *inner_msg {
                MainThreadScriptMsg::Common(CommonScriptMsg::Task(category, ..)) => category,
                MainThreadScriptMsg::RegisterPaintWorklet { .. } => {
                    ScriptThreadEventCategory::WorkletEvent
                },
                _ => ScriptThreadEventCategory::ScriptEvent,
            },
            MixedMessage::FromWebGPUServer(_) => ScriptThreadEventCategory::WebGPUMsg,
        }
    }

    fn notify_activity_to_hang_monitor(&self, category: &ScriptThreadEventCategory) {
        let hang_annotation = match category {
            ScriptThreadEventCategory::AttachLayout => ScriptHangAnnotation::AttachLayout,
            ScriptThreadEventCategory::ConstellationMsg => ScriptHangAnnotation::ConstellationMsg,
            ScriptThreadEventCategory::DevtoolsMsg => ScriptHangAnnotation::DevtoolsMsg,
            ScriptThreadEventCategory::DocumentEvent => ScriptHangAnnotation::DocumentEvent,
            ScriptThreadEventCategory::DomEvent => ScriptHangAnnotation::DomEvent,
            ScriptThreadEventCategory::FileRead => ScriptHangAnnotation::FileRead,
            ScriptThreadEventCategory::FormPlannedNavigation => {
                ScriptHangAnnotation::FormPlannedNavigation
            },
            ScriptThreadEventCategory::HistoryEvent => ScriptHangAnnotation::HistoryEvent,
            ScriptThreadEventCategory::ImageCacheMsg => ScriptHangAnnotation::ImageCacheMsg,
            ScriptThreadEventCategory::InputEvent => ScriptHangAnnotation::InputEvent,
            ScriptThreadEventCategory::NetworkEvent => ScriptHangAnnotation::NetworkEvent,
            ScriptThreadEventCategory::Resize => ScriptHangAnnotation::Resize,
            ScriptThreadEventCategory::ScriptEvent => ScriptHangAnnotation::ScriptEvent,
            ScriptThreadEventCategory::SetScrollState => ScriptHangAnnotation::SetScrollState,
            ScriptThreadEventCategory::SetViewport => ScriptHangAnnotation::SetViewport,
            ScriptThreadEventCategory::StylesheetLoad => ScriptHangAnnotation::StylesheetLoad,
            ScriptThreadEventCategory::TimerEvent => ScriptHangAnnotation::TimerEvent,
            ScriptThreadEventCategory::UpdateReplacedElement => {
                ScriptHangAnnotation::UpdateReplacedElement
            },
            ScriptThreadEventCategory::WebSocketEvent => ScriptHangAnnotation::WebSocketEvent,
            ScriptThreadEventCategory::WorkerEvent => ScriptHangAnnotation::WorkerEvent,
            ScriptThreadEventCategory::WorkletEvent => ScriptHangAnnotation::WorkletEvent,
            ScriptThreadEventCategory::ServiceWorkerEvent => {
                ScriptHangAnnotation::ServiceWorkerEvent
            },
            ScriptThreadEventCategory::EnterFullscreen => ScriptHangAnnotation::EnterFullscreen,
            ScriptThreadEventCategory::ExitFullscreen => ScriptHangAnnotation::ExitFullscreen,
            ScriptThreadEventCategory::PerformanceTimelineTask => {
                ScriptHangAnnotation::PerformanceTimelineTask
            },
            ScriptThreadEventCategory::PortMessage => ScriptHangAnnotation::PortMessage,
            ScriptThreadEventCategory::WebGPUMsg => ScriptHangAnnotation::WebGPUMsg,
        };
        self.background_hang_monitor
            .notify_activity(HangAnnotation::Script(hang_annotation));
    }

    fn message_to_pipeline(&self, msg: &MixedMessage) -> Option<PipelineId> {
        use script_traits::ConstellationControlMsg::*;
        match *msg {
            MixedMessage::FromConstellation(ref inner_msg) => match *inner_msg {
                StopDelayingLoadEventsMode(id) => Some(id),
                NavigationResponse(id, _) => Some(id),
                AttachLayout(ref new_layout_info) => new_layout_info
                    .parent_info
                    .or(Some(new_layout_info.new_pipeline_id)),
                Resize(id, ..) => Some(id),
                ResizeInactive(id, ..) => Some(id),
                UnloadDocument(id) => Some(id),
                ExitPipeline(id, ..) => Some(id),
                ExitScriptThread => None,
                SendEvent(id, ..) => Some(id),
                Viewport(id, ..) => Some(id),
                GetTitle(id) => Some(id),
                SetDocumentActivity(id, ..) => Some(id),
                SetThrottled(id, ..) => Some(id),
                SetThrottledInContainingIframe(id, ..) => Some(id),
                NavigateIframe(id, ..) => Some(id),
                PostMessage { target: id, .. } => Some(id),
                UpdatePipelineId(_, _, _, id, _) => Some(id),
                UpdateHistoryState(id, ..) => Some(id),
                RemoveHistoryStates(id, ..) => Some(id),
                FocusIFrame(id, ..) => Some(id),
                WebDriverScriptCommand(id, ..) => Some(id),
                TickAllAnimations(id, ..) => Some(id),
                WebFontLoaded(id, ..) => Some(id),
                DispatchIFrameLoadEvent {
                    target: _,
                    parent: id,
                    child: _,
                } => Some(id),
                DispatchStorageEvent(id, ..) => Some(id),
                ReportCSSError(id, ..) => Some(id),
                Reload(id, ..) => Some(id),
                PaintMetric(id, ..) => Some(id),
                ExitFullScreen(id, ..) => Some(id),
                MediaSessionAction(..) => None,
                SetWebGPUPort(..) => None,
                SetScrollStates(id, ..) => Some(id),
                SetEpochPaintTime(id, ..) => Some(id),
            },
            MixedMessage::FromDevtools(_) => None,
            MixedMessage::FromScript(ref inner_msg) => match *inner_msg {
                MainThreadScriptMsg::Common(CommonScriptMsg::Task(_, _, pipeline_id, _)) => {
                    pipeline_id
                },
                MainThreadScriptMsg::Common(CommonScriptMsg::CollectReports(_)) => None,
                MainThreadScriptMsg::WorkletLoaded(pipeline_id) => Some(pipeline_id),
                MainThreadScriptMsg::RegisterPaintWorklet { pipeline_id, .. } => Some(pipeline_id),
                MainThreadScriptMsg::Inactive => None,
                MainThreadScriptMsg::WakeUp => None,
            },
            MixedMessage::FromImageCache((pipeline_id, _)) => Some(pipeline_id),
            MixedMessage::FromWebGPUServer(..) => None,
        }
    }

    fn profile_event<F, R>(
        &self,
        category: ScriptThreadEventCategory,
        pipeline_id: Option<PipelineId>,
        f: F,
    ) -> R
    where
        F: FnOnce() -> R,
    {
        self.notify_activity_to_hang_monitor(&category);
        let start = Instant::now();
        let value = if self.profile_script_events {
            let profiler_chan = self.time_profiler_chan.clone();
            match category {
                ScriptThreadEventCategory::AttachLayout => {
                    time_profile!(ProfilerCategory::ScriptAttachLayout, None, profiler_chan, f)
                },
                ScriptThreadEventCategory::ConstellationMsg => time_profile!(
                    ProfilerCategory::ScriptConstellationMsg,
                    None,
                    profiler_chan,
                    f
                ),
                ScriptThreadEventCategory::DevtoolsMsg => {
                    time_profile!(ProfilerCategory::ScriptDevtoolsMsg, None, profiler_chan, f)
                },
                ScriptThreadEventCategory::DocumentEvent => time_profile!(
                    ProfilerCategory::ScriptDocumentEvent,
                    None,
                    profiler_chan,
                    f
                ),
                ScriptThreadEventCategory::DomEvent => {
                    time_profile!(ProfilerCategory::ScriptDomEvent, None, profiler_chan, f)
                },
                ScriptThreadEventCategory::FileRead => {
                    time_profile!(ProfilerCategory::ScriptFileRead, None, profiler_chan, f)
                },
                ScriptThreadEventCategory::FormPlannedNavigation => time_profile!(
                    ProfilerCategory::ScriptPlannedNavigation,
                    None,
                    profiler_chan,
                    f
                ),
                ScriptThreadEventCategory::HistoryEvent => {
                    time_profile!(ProfilerCategory::ScriptHistoryEvent, None, profiler_chan, f)
                },
                ScriptThreadEventCategory::ImageCacheMsg => time_profile!(
                    ProfilerCategory::ScriptImageCacheMsg,
                    None,
                    profiler_chan,
                    f
                ),
                ScriptThreadEventCategory::InputEvent => {
                    time_profile!(ProfilerCategory::ScriptInputEvent, None, profiler_chan, f)
                },
                ScriptThreadEventCategory::NetworkEvent => {
                    time_profile!(ProfilerCategory::ScriptNetworkEvent, None, profiler_chan, f)
                },
                ScriptThreadEventCategory::PortMessage => {
                    time_profile!(ProfilerCategory::ScriptPortMessage, None, profiler_chan, f)
                },
                ScriptThreadEventCategory::Resize => {
                    time_profile!(ProfilerCategory::ScriptResize, None, profiler_chan, f)
                },
                ScriptThreadEventCategory::ScriptEvent => {
                    time_profile!(ProfilerCategory::ScriptEvent, None, profiler_chan, f)
                },
                ScriptThreadEventCategory::SetScrollState => time_profile!(
                    ProfilerCategory::ScriptSetScrollState,
                    None,
                    profiler_chan,
                    f
                ),
                ScriptThreadEventCategory::UpdateReplacedElement => time_profile!(
                    ProfilerCategory::ScriptUpdateReplacedElement,
                    None,
                    profiler_chan,
                    f
                ),
                ScriptThreadEventCategory::StylesheetLoad => time_profile!(
                    ProfilerCategory::ScriptStylesheetLoad,
                    None,
                    profiler_chan,
                    f
                ),
                ScriptThreadEventCategory::SetViewport => {
                    time_profile!(ProfilerCategory::ScriptSetViewport, None, profiler_chan, f)
                },
                ScriptThreadEventCategory::TimerEvent => {
                    time_profile!(ProfilerCategory::ScriptTimerEvent, None, profiler_chan, f)
                },
                ScriptThreadEventCategory::WebSocketEvent => time_profile!(
                    ProfilerCategory::ScriptWebSocketEvent,
                    None,
                    profiler_chan,
                    f
                ),
                ScriptThreadEventCategory::WorkerEvent => {
                    time_profile!(ProfilerCategory::ScriptWorkerEvent, None, profiler_chan, f)
                },
                ScriptThreadEventCategory::WorkletEvent => {
                    time_profile!(ProfilerCategory::ScriptWorkletEvent, None, profiler_chan, f)
                },
                ScriptThreadEventCategory::ServiceWorkerEvent => time_profile!(
                    ProfilerCategory::ScriptServiceWorkerEvent,
                    None,
                    profiler_chan,
                    f
                ),
                ScriptThreadEventCategory::EnterFullscreen => time_profile!(
                    ProfilerCategory::ScriptEnterFullscreen,
                    None,
                    profiler_chan,
                    f
                ),
                ScriptThreadEventCategory::ExitFullscreen => time_profile!(
                    ProfilerCategory::ScriptExitFullscreen,
                    None,
                    profiler_chan,
                    f
                ),
                ScriptThreadEventCategory::PerformanceTimelineTask => time_profile!(
                    ProfilerCategory::ScriptPerformanceEvent,
                    None,
                    profiler_chan,
                    f
                ),
                ScriptThreadEventCategory::WebGPUMsg => {
                    time_profile!(ProfilerCategory::ScriptWebGPUMsg, None, profiler_chan, f)
                },
            }
        } else {
            f()
        };
        let task_duration = start.elapsed();
        for (doc_id, doc) in self.documents.borrow().iter() {
            if let Some(pipeline_id) = pipeline_id {
                if pipeline_id == doc_id && task_duration.as_nanos() > MAX_TASK_NS.into() {
                    if self.print_pwm {
                        println!(
                            "Task took longer than max allowed ({:?}) {:?}",
                            category,
                            task_duration.as_nanos()
                        );
                    }
                    doc.start_tti();
                }
            }
            doc.record_tti_if_necessary();
        }
        value
    }

    fn handle_msg_from_constellation(&self, msg: ConstellationControlMsg, can_gc: CanGc) {
        match msg {
            ConstellationControlMsg::StopDelayingLoadEventsMode(pipeline_id) => {
                self.handle_stop_delaying_load_events_mode(pipeline_id)
            },
            ConstellationControlMsg::NavigationResponse(pipeline_id, fetch_data) => {
                match fetch_data {
                    FetchResponseMsg::ProcessResponse(request_id, metadata) => {
                        self.handle_fetch_metadata(pipeline_id, request_id, metadata)
                    },
                    FetchResponseMsg::ProcessResponseChunk(request_id, chunk) => {
                        self.handle_fetch_chunk(pipeline_id, request_id, chunk)
                    },
                    FetchResponseMsg::ProcessResponseEOF(request_id, eof) => {
                        self.handle_fetch_eof(pipeline_id, request_id, eof)
                    },
                    _ => unreachable!(),
                };
            },
            ConstellationControlMsg::NavigateIframe(
                parent_pipeline_id,
                browsing_context_id,
                load_data,
                replace,
            ) => self.handle_navigate_iframe(
                parent_pipeline_id,
                browsing_context_id,
                load_data,
                replace,
                can_gc,
            ),
            ConstellationControlMsg::UnloadDocument(pipeline_id) => {
                self.handle_unload_document(pipeline_id, can_gc)
            },
            ConstellationControlMsg::ResizeInactive(id, new_size) => {
                self.handle_resize_inactive_msg(id, new_size)
            },
            ConstellationControlMsg::GetTitle(pipeline_id) => {
                self.handle_get_title_msg(pipeline_id)
            },
            ConstellationControlMsg::SetDocumentActivity(pipeline_id, activity) => {
                self.handle_set_document_activity_msg(pipeline_id, activity)
            },
            ConstellationControlMsg::SetThrottled(pipeline_id, throttled) => {
                self.handle_set_throttled_msg(pipeline_id, throttled)
            },
            ConstellationControlMsg::SetThrottledInContainingIframe(
                parent_pipeline_id,
                browsing_context_id,
                throttled,
            ) => self.handle_set_throttled_in_containing_iframe_msg(
                parent_pipeline_id,
                browsing_context_id,
                throttled,
            ),
            ConstellationControlMsg::PostMessage {
                target: target_pipeline_id,
                source: source_pipeline_id,
                source_browsing_context,
                target_origin: origin,
                source_origin,
                data,
            } => self.handle_post_message_msg(
                target_pipeline_id,
                source_pipeline_id,
                source_browsing_context,
                origin,
                source_origin,
                data,
            ),
            ConstellationControlMsg::UpdatePipelineId(
                parent_pipeline_id,
                browsing_context_id,
                top_level_browsing_context_id,
                new_pipeline_id,
                reason,
            ) => self.handle_update_pipeline_id(
                parent_pipeline_id,
                browsing_context_id,
                top_level_browsing_context_id,
                new_pipeline_id,
                reason,
                can_gc,
            ),
            ConstellationControlMsg::UpdateHistoryState(pipeline_id, history_state_id, url) => {
                self.handle_update_history_state_msg(pipeline_id, history_state_id, url, can_gc)
            },
            ConstellationControlMsg::RemoveHistoryStates(pipeline_id, history_states) => {
                self.handle_remove_history_states(pipeline_id, history_states)
            },
            ConstellationControlMsg::FocusIFrame(parent_pipeline_id, frame_id) => {
                self.handle_focus_iframe_msg(parent_pipeline_id, frame_id, can_gc)
            },
            ConstellationControlMsg::WebDriverScriptCommand(pipeline_id, msg) => {
                self.handle_webdriver_msg(pipeline_id, msg, can_gc)
            },
            ConstellationControlMsg::WebFontLoaded(pipeline_id, success) => {
                self.handle_web_font_loaded(pipeline_id, success)
            },
            ConstellationControlMsg::DispatchIFrameLoadEvent {
                target: browsing_context_id,
                parent: parent_id,
                child: child_id,
            } => self.handle_iframe_load_event(parent_id, browsing_context_id, child_id, can_gc),
            ConstellationControlMsg::DispatchStorageEvent(
                pipeline_id,
                storage,
                url,
                key,
                old_value,
                new_value,
            ) => self.handle_storage_event(pipeline_id, storage, url, key, old_value, new_value),
            ConstellationControlMsg::ReportCSSError(pipeline_id, filename, line, column, msg) => {
                self.handle_css_error_reporting(pipeline_id, filename, line, column, msg)
            },
            ConstellationControlMsg::Reload(pipeline_id) => self.handle_reload(pipeline_id, can_gc),
            ConstellationControlMsg::ExitPipeline(pipeline_id, discard_browsing_context) => {
                self.handle_exit_pipeline_msg(pipeline_id, discard_browsing_context, can_gc)
            },
            ConstellationControlMsg::PaintMetric(pipeline_id, metric_type, metric_value) => {
                self.handle_paint_metric(pipeline_id, metric_type, metric_value, can_gc)
            },
            ConstellationControlMsg::MediaSessionAction(pipeline_id, action) => {
                self.handle_media_session_action(pipeline_id, action, can_gc)
            },
            ConstellationControlMsg::SetWebGPUPort(port) => {
                if self.webgpu_port.borrow().is_some() {
                    warn!("WebGPU port already exists for this content process");
                } else {
                    let p = ROUTER.route_ipc_receiver_to_new_crossbeam_receiver(port);
                    *self.webgpu_port.borrow_mut() = Some(p);
                }
            },
            msg @ ConstellationControlMsg::AttachLayout(..) |
            msg @ ConstellationControlMsg::Viewport(..) |
            msg @ ConstellationControlMsg::Resize(..) |
            msg @ ConstellationControlMsg::ExitFullScreen(..) |
            msg @ ConstellationControlMsg::SendEvent(..) |
            msg @ ConstellationControlMsg::TickAllAnimations(..) |
            msg @ ConstellationControlMsg::ExitScriptThread => {
                panic!("should have handled {:?} already", msg)
            },
            ConstellationControlMsg::SetScrollStates(pipeline_id, scroll_states) => {
                self.handle_set_scroll_states_msg(pipeline_id, scroll_states)
            },
            ConstellationControlMsg::SetEpochPaintTime(pipeline_id, epoch, time) => {
                self.handle_set_epoch_paint_time(pipeline_id, epoch, time)
            },
        }
    }

    fn handle_set_scroll_states_msg(
        &self,
        pipeline_id: PipelineId,
        scroll_states: Vec<ScrollState>,
    ) {
        let Some(window) = self.documents.borrow().find_window(pipeline_id) else {
            warn!("Received scroll states for closed pipeline {pipeline_id}");
            return;
        };

        self.profile_event(
            ScriptThreadEventCategory::SetScrollState,
            Some(pipeline_id),
            || {
                window.layout_mut().set_scroll_states(&scroll_states);

                let mut scroll_offsets = HashMap::new();
                for scroll_state in scroll_states.into_iter() {
                    let scroll_offset = scroll_state.scroll_offset;
                    if scroll_state.scroll_id.is_root() {
                        window.update_viewport_for_scroll(-scroll_offset.x, -scroll_offset.y);
                    } else if let Some(node_id) =
                        node_id_from_scroll_id(scroll_state.scroll_id.0 as usize)
                    {
                        scroll_offsets.insert(OpaqueNode(node_id), -scroll_offset);
                    }
                }
                window.set_scroll_offsets(scroll_offsets)
            },
        )
    }

    fn handle_set_epoch_paint_time(
        &self,
        pipeline_id: PipelineId,
        epoch: Epoch,
        time: CrossProcessInstant,
    ) {
        let Some(window) = self.documents.borrow().find_window(pipeline_id) else {
            warn!("Received set epoch paint time message for closed pipeline {pipeline_id}.");
            return;
        };
        window.layout_mut().set_epoch_paint_time(epoch, time);
    }

    fn handle_msg_from_webgpu_server(&self, msg: WebGPUMsg, can_gc: CanGc) {
        match msg {
            WebGPUMsg::FreeAdapter(id) => self.gpu_id_hub.free_adapter_id(id),
            WebGPUMsg::FreeDevice {
                device_id,
                pipeline_id,
            } => {
                self.gpu_id_hub.free_device_id(device_id);
                if let Some(global) = self.documents.borrow().find_global(pipeline_id) {
                    global.remove_gpu_device(WebGPUDevice(device_id));
                } // page can already be destroyed
            },
            WebGPUMsg::FreeBuffer(id) => self.gpu_id_hub.free_buffer_id(id),
            WebGPUMsg::FreePipelineLayout(id) => self.gpu_id_hub.free_pipeline_layout_id(id),
            WebGPUMsg::FreeComputePipeline(id) => self.gpu_id_hub.free_compute_pipeline_id(id),
            WebGPUMsg::FreeBindGroup(id) => self.gpu_id_hub.free_bind_group_id(id),
            WebGPUMsg::FreeBindGroupLayout(id) => self.gpu_id_hub.free_bind_group_layout_id(id),
            WebGPUMsg::FreeCommandBuffer(id) => self
                .gpu_id_hub
                .free_command_buffer_id(id.into_command_encoder_id()),
            WebGPUMsg::FreeSampler(id) => self.gpu_id_hub.free_sampler_id(id),
            WebGPUMsg::FreeShaderModule(id) => self.gpu_id_hub.free_shader_module_id(id),
            WebGPUMsg::FreeRenderBundle(id) => self.gpu_id_hub.free_render_bundle_id(id),
            WebGPUMsg::FreeRenderPipeline(id) => self.gpu_id_hub.free_render_pipeline_id(id),
            WebGPUMsg::FreeTexture(id) => self.gpu_id_hub.free_texture_id(id),
            WebGPUMsg::FreeTextureView(id) => self.gpu_id_hub.free_texture_view_id(id),
            WebGPUMsg::FreeComputePass(id) => self.gpu_id_hub.free_compute_pass_id(id),
            WebGPUMsg::FreeRenderPass(id) => self.gpu_id_hub.free_render_pass_id(id),
            WebGPUMsg::Exit => *self.webgpu_port.borrow_mut() = None,
            WebGPUMsg::DeviceLost {
                pipeline_id,
                device,
                reason,
                msg,
            } => {
                let global = self.documents.borrow().find_global(pipeline_id).unwrap();
                global.gpu_device_lost(device, reason, msg);
            },
            WebGPUMsg::UncapturedError {
                device,
                pipeline_id,
                error,
            } => {
                let global = self.documents.borrow().find_global(pipeline_id).unwrap();
                let _ac = enter_realm(&*global);
                global.handle_uncaptured_gpu_error(device, error, can_gc);
            },
            _ => {},
        }
    }

    fn handle_msg_from_script(&self, msg: MainThreadScriptMsg) {
        match msg {
            MainThreadScriptMsg::Common(CommonScriptMsg::Task(_, task, pipeline_id, _)) => {
                let _realm = pipeline_id.and_then(|id| {
                    let global = self.documents.borrow().find_global(id);
                    global.map(|global| enter_realm(&*global))
                });
                task.run_box()
            },
            MainThreadScriptMsg::Common(CommonScriptMsg::CollectReports(chan)) => {
                self.collect_reports(chan)
            },
            MainThreadScriptMsg::WorkletLoaded(pipeline_id) => {
                self.handle_worklet_loaded(pipeline_id, CanGc::note())
            },
            MainThreadScriptMsg::RegisterPaintWorklet {
                pipeline_id,
                name,
                properties,
                painter,
            } => self.handle_register_paint_worklet(pipeline_id, name, properties, painter),
            MainThreadScriptMsg::Inactive => {},
            MainThreadScriptMsg::WakeUp => {},
        }
    }

    fn handle_msg_from_devtools(&self, msg: DevtoolScriptControlMsg, can_gc: CanGc) {
        let documents = self.documents.borrow();
        match msg {
            DevtoolScriptControlMsg::EvaluateJS(id, s, reply) => match documents.find_window(id) {
                Some(window) => {
                    let global = window.upcast::<GlobalScope>();
                    let _aes = AutoEntryScript::new(global);
                    devtools::handle_evaluate_js(global, s, reply, can_gc)
                },
                None => warn!("Message sent to closed pipeline {}.", id),
            },
            DevtoolScriptControlMsg::GetRootNode(id, reply) => {
                devtools::handle_get_root_node(&documents, id, reply)
            },
            DevtoolScriptControlMsg::GetDocumentElement(id, reply) => {
                devtools::handle_get_document_element(&documents, id, reply)
            },
            DevtoolScriptControlMsg::GetChildren(id, node_id, reply) => {
                devtools::handle_get_children(&documents, id, node_id, reply)
            },
            DevtoolScriptControlMsg::GetAttributeStyle(id, node_id, reply) => {
                devtools::handle_get_attribute_style(&documents, id, node_id, reply, can_gc)
            },
            DevtoolScriptControlMsg::GetStylesheetStyle(
                id,
                node_id,
                selector,
                stylesheet,
                reply,
            ) => devtools::handle_get_stylesheet_style(
                &documents, id, node_id, selector, stylesheet, reply, can_gc,
            ),
            DevtoolScriptControlMsg::GetSelectors(id, node_id, reply) => {
                devtools::handle_get_selectors(&documents, id, node_id, reply)
            },
            DevtoolScriptControlMsg::GetComputedStyle(id, node_id, reply) => {
                devtools::handle_get_computed_style(&documents, id, node_id, reply, can_gc)
            },
            DevtoolScriptControlMsg::GetLayout(id, node_id, reply) => {
                devtools::handle_get_layout(&documents, id, node_id, reply, can_gc)
            },
            DevtoolScriptControlMsg::ModifyAttribute(id, node_id, modifications) => {
                devtools::handle_modify_attribute(&documents, id, node_id, modifications, can_gc)
            },
            DevtoolScriptControlMsg::ModifyRule(id, node_id, modifications) => {
                devtools::handle_modify_rule(&documents, id, node_id, modifications, can_gc)
            },
            DevtoolScriptControlMsg::WantsLiveNotifications(id, to_send) => match documents
                .find_window(id)
            {
                Some(window) => devtools::handle_wants_live_notifications(window.upcast(), to_send),
                None => warn!("Message sent to closed pipeline {}.", id),
            },
            DevtoolScriptControlMsg::SetTimelineMarkers(id, marker_types, reply) => {
                devtools::handle_set_timeline_markers(&documents, id, marker_types, reply)
            },
            DevtoolScriptControlMsg::DropTimelineMarkers(id, marker_types) => {
                devtools::handle_drop_timeline_markers(&documents, id, marker_types)
            },
            DevtoolScriptControlMsg::RequestAnimationFrame(id, name) => {
                devtools::handle_request_animation_frame(&documents, id, name)
            },
            DevtoolScriptControlMsg::Reload(id) => devtools::handle_reload(&documents, id, can_gc),
            DevtoolScriptControlMsg::GetCssDatabase(reply) => {
                devtools::handle_get_css_database(reply)
            },
        }
    }

    fn handle_msg_from_image_cache(&self, (id, response): (PipelineId, PendingImageResponse)) {
        let window = self.documents.borrow().find_window(id);
        if let Some(ref window) = window {
            window.pending_image_notification(response);
        }
    }

    fn handle_webdriver_msg(
        &self,
        pipeline_id: PipelineId,
        msg: WebDriverScriptCommand,
        can_gc: CanGc,
    ) {
        // https://github.com/servo/servo/issues/23535
        // These two messages need different treatment since the JS script might mutate
        // `self.documents`, which would conflict with the immutable borrow of it that
        // occurs for the rest of the messages
        match msg {
            WebDriverScriptCommand::ExecuteScript(script, reply) => {
                let window = self.documents.borrow().find_window(pipeline_id);
                return webdriver_handlers::handle_execute_script(window, script, reply, can_gc);
            },
            WebDriverScriptCommand::ExecuteAsyncScript(script, reply) => {
                let window = self.documents.borrow().find_window(pipeline_id);
                return webdriver_handlers::handle_execute_async_script(
                    window, script, reply, can_gc,
                );
            },
            _ => (),
        }

        let documents = self.documents.borrow();
        match msg {
            WebDriverScriptCommand::AddCookie(params, reply) => {
                webdriver_handlers::handle_add_cookie(&documents, pipeline_id, params, reply)
            },
            WebDriverScriptCommand::DeleteCookies(reply) => {
                webdriver_handlers::handle_delete_cookies(&documents, pipeline_id, reply)
            },
            WebDriverScriptCommand::FindElementCSS(selector, reply) => {
                webdriver_handlers::handle_find_element_css(
                    &documents,
                    pipeline_id,
                    selector,
                    reply,
                )
            },
            WebDriverScriptCommand::FindElementLinkText(selector, partial, reply) => {
                webdriver_handlers::handle_find_element_link_text(
                    &documents,
                    pipeline_id,
                    selector,
                    partial,
                    reply,
                )
            },
            WebDriverScriptCommand::FindElementTagName(selector, reply) => {
                webdriver_handlers::handle_find_element_tag_name(
                    &documents,
                    pipeline_id,
                    selector,
                    reply,
                )
            },
            WebDriverScriptCommand::FindElementsCSS(selector, reply) => {
                webdriver_handlers::handle_find_elements_css(
                    &documents,
                    pipeline_id,
                    selector,
                    reply,
                )
            },
            WebDriverScriptCommand::FindElementsLinkText(selector, partial, reply) => {
                webdriver_handlers::handle_find_elements_link_text(
                    &documents,
                    pipeline_id,
                    selector,
                    partial,
                    reply,
                )
            },
            WebDriverScriptCommand::FindElementsTagName(selector, reply) => {
                webdriver_handlers::handle_find_elements_tag_name(
                    &documents,
                    pipeline_id,
                    selector,
                    reply,
                )
            },
            WebDriverScriptCommand::FindElementElementCSS(selector, element_id, reply) => {
                webdriver_handlers::handle_find_element_element_css(
                    &documents,
                    pipeline_id,
                    element_id,
                    selector,
                    reply,
                )
            },
            WebDriverScriptCommand::FindElementElementLinkText(
                selector,
                element_id,
                partial,
                reply,
            ) => webdriver_handlers::handle_find_element_element_link_text(
                &documents,
                pipeline_id,
                element_id,
                selector,
                partial,
                reply,
            ),
            WebDriverScriptCommand::FindElementElementTagName(selector, element_id, reply) => {
                webdriver_handlers::handle_find_element_element_tag_name(
                    &documents,
                    pipeline_id,
                    element_id,
                    selector,
                    reply,
                )
            },
            WebDriverScriptCommand::FindElementElementsCSS(selector, element_id, reply) => {
                webdriver_handlers::handle_find_element_elements_css(
                    &documents,
                    pipeline_id,
                    element_id,
                    selector,
                    reply,
                )
            },
            WebDriverScriptCommand::FindElementElementsLinkText(
                selector,
                element_id,
                partial,
                reply,
            ) => webdriver_handlers::handle_find_element_elements_link_text(
                &documents,
                pipeline_id,
                element_id,
                selector,
                partial,
                reply,
            ),
            WebDriverScriptCommand::FindElementElementsTagName(selector, element_id, reply) => {
                webdriver_handlers::handle_find_element_elements_tag_name(
                    &documents,
                    pipeline_id,
                    element_id,
                    selector,
                    reply,
                )
            },
            WebDriverScriptCommand::FocusElement(element_id, reply) => {
                webdriver_handlers::handle_focus_element(
                    &documents,
                    pipeline_id,
                    element_id,
                    reply,
                    can_gc,
                )
            },
            WebDriverScriptCommand::ElementClick(element_id, reply) => {
                webdriver_handlers::handle_element_click(
                    &documents,
                    pipeline_id,
                    element_id,
                    reply,
                    can_gc,
                )
            },
            WebDriverScriptCommand::GetActiveElement(reply) => {
                webdriver_handlers::handle_get_active_element(&documents, pipeline_id, reply)
            },
            WebDriverScriptCommand::GetPageSource(reply) => {
                webdriver_handlers::handle_get_page_source(&documents, pipeline_id, reply, can_gc)
            },
            WebDriverScriptCommand::GetCookies(reply) => {
                webdriver_handlers::handle_get_cookies(&documents, pipeline_id, reply)
            },
            WebDriverScriptCommand::GetCookie(name, reply) => {
                webdriver_handlers::handle_get_cookie(&documents, pipeline_id, name, reply)
            },
            WebDriverScriptCommand::GetElementTagName(node_id, reply) => {
                webdriver_handlers::handle_get_name(&documents, pipeline_id, node_id, reply)
            },
            WebDriverScriptCommand::GetElementAttribute(node_id, name, reply) => {
                webdriver_handlers::handle_get_attribute(
                    &documents,
                    pipeline_id,
                    node_id,
                    name,
                    reply,
                )
            },
            WebDriverScriptCommand::GetElementProperty(node_id, name, reply) => {
                webdriver_handlers::handle_get_property(
                    &documents,
                    pipeline_id,
                    node_id,
                    name,
                    reply,
                )
            },
            WebDriverScriptCommand::GetElementCSS(node_id, name, reply) => {
                webdriver_handlers::handle_get_css(
                    &documents,
                    pipeline_id,
                    node_id,
                    name,
                    reply,
                    can_gc,
                )
            },
            WebDriverScriptCommand::GetElementRect(node_id, reply) => {
                webdriver_handlers::handle_get_rect(&documents, pipeline_id, node_id, reply, can_gc)
            },
            WebDriverScriptCommand::GetBoundingClientRect(node_id, reply) => {
                webdriver_handlers::handle_get_bounding_client_rect(
                    &documents,
                    pipeline_id,
                    node_id,
                    reply,
                    can_gc,
                )
            },
            WebDriverScriptCommand::GetElementText(node_id, reply) => {
                webdriver_handlers::handle_get_text(&documents, pipeline_id, node_id, reply)
            },
            WebDriverScriptCommand::GetElementInViewCenterPoint(node_id, reply) => {
                webdriver_handlers::handle_get_element_in_view_center_point(
                    &documents,
                    pipeline_id,
                    node_id,
                    reply,
                    can_gc,
                )
            },
            WebDriverScriptCommand::GetBrowsingContextId(webdriver_frame_id, reply) => {
                webdriver_handlers::handle_get_browsing_context_id(
                    &documents,
                    pipeline_id,
                    webdriver_frame_id,
                    reply,
                )
            },
            WebDriverScriptCommand::GetUrl(reply) => {
                webdriver_handlers::handle_get_url(&documents, pipeline_id, reply)
            },
            WebDriverScriptCommand::IsEnabled(element_id, reply) => {
                webdriver_handlers::handle_is_enabled(&documents, pipeline_id, element_id, reply)
            },
            WebDriverScriptCommand::IsSelected(element_id, reply) => {
                webdriver_handlers::handle_is_selected(&documents, pipeline_id, element_id, reply)
            },
            WebDriverScriptCommand::GetTitle(reply) => {
                webdriver_handlers::handle_get_title(&documents, pipeline_id, reply)
            },
            _ => (),
        }
    }

    /// Batch window resize operations into a single "update the rendering" task,
    /// or, if a load is in progress, set the window size directly.
    fn handle_resize_message(
        &self,
        id: PipelineId,
        size: WindowSizeData,
        size_type: WindowSizeType,
    ) {
        let window = self.documents.borrow().find_window(id);
        if let Some(ref window) = window {
            self.rendering_opportunity(id);
            window.add_resize_event(size, size_type);
            return;
        }
        let mut loads = self.incomplete_loads.borrow_mut();
        if let Some(ref mut load) = loads.iter_mut().find(|load| load.pipeline_id == id) {
            load.window_size = size;
            return;
        }
        warn!("resize sent to nonexistent pipeline");
    }

    // exit_fullscreen creates a new JS promise object, so we need to have entered a realm
    fn handle_exit_fullscreen(&self, id: PipelineId, can_gc: CanGc) {
        let document = self.documents.borrow().find_document(id);
        if let Some(document) = document {
            let _ac = enter_realm(&*document);
            document.exit_fullscreen(can_gc);
        }
    }

    fn handle_viewport(&self, id: PipelineId, rect: Rect<f32>, can_gc: CanGc) {
        let document = self.documents.borrow().find_document(id);
        if let Some(document) = document {
            if document.window().set_page_clip_rect_with_new_viewport(rect) {
                self.rebuild_and_force_reflow(&document, ReflowReason::Viewport, can_gc);
            }
            return;
        }
        let loads = self.incomplete_loads.borrow();
        if loads.iter().any(|load| load.pipeline_id == id) {
            return;
        }
        warn!("Page rect message sent to nonexistent pipeline");
    }

    fn handle_new_layout(&self, new_layout_info: NewLayoutInfo, origin: MutableOrigin) {
        let NewLayoutInfo {
            parent_info,
            new_pipeline_id,
            browsing_context_id,
            top_level_browsing_context_id,
            opener,
            load_data,
            window_size,
        } = new_layout_info;

        // Kick off the fetch for the new resource.
        let new_load = InProgressLoad::new(
            new_pipeline_id,
            browsing_context_id,
            top_level_browsing_context_id,
            parent_info,
            opener,
            window_size,
            load_data.url.clone(),
            origin,
            load_data.inherited_secure_context,
        );
        if load_data.url.as_str() == "about:blank" {
            self.start_page_load_about_blank(new_load, load_data);
        } else if load_data.url.as_str() == "about:srcdoc" {
            self.page_load_about_srcdoc(new_load, load_data);
        } else {
            self.pre_page_load(new_load, load_data);
        }
    }

    fn collect_reports(&self, reports_chan: ReportsChan) {
        let documents = self.documents.borrow();
        let urls = itertools::join(documents.iter().map(|(_, d)| d.url().to_string()), ", ");

        let mut reports = self.get_cx().get_reports(format!("url({})", urls));
        for (_, document) in documents.iter() {
            document.window().layout().collect_reports(&mut reports);
        }

        reports_chan.send(reports);
    }

    /// Updates iframe element after a change in visibility
    fn handle_set_throttled_in_containing_iframe_msg(
        &self,
        parent_pipeline_id: PipelineId,
        browsing_context_id: BrowsingContextId,
        throttled: bool,
    ) {
        let iframe = self
            .documents
            .borrow()
            .find_iframe(parent_pipeline_id, browsing_context_id);
        if let Some(iframe) = iframe {
            iframe.set_throttled(throttled);
        }
    }

    fn handle_set_throttled_msg(&self, id: PipelineId, throttled: bool) {
        // Separate message sent since parent script thread could be different (Iframe of different
        // domain)
        self.script_sender
            .send((id, ScriptMsg::SetThrottledComplete(throttled)))
            .unwrap();

        let window = self.documents.borrow().find_window(id);
        match window {
            Some(window) => {
                window.set_throttled(throttled);
                return;
            },
            None => {
                let mut loads = self.incomplete_loads.borrow_mut();
                if let Some(ref mut load) = loads.iter_mut().find(|load| load.pipeline_id == id) {
                    load.throttled = throttled;
                    return;
                }
            },
        }

        warn!("SetThrottled sent to nonexistent pipeline");
    }

    /// Handles activity change message
    fn handle_set_document_activity_msg(&self, id: PipelineId, activity: DocumentActivity) {
        debug!(
            "Setting activity of {} to be {:?} in {:?}.",
            id,
            activity,
            thread::current().name()
        );
        let document = self.documents.borrow().find_document(id);
        if let Some(document) = document {
            document.set_activity(activity, CanGc::note());
            return;
        }
        let mut loads = self.incomplete_loads.borrow_mut();
        if let Some(ref mut load) = loads.iter_mut().find(|load| load.pipeline_id == id) {
            load.activity = activity;
            return;
        }
        warn!("change of activity sent to nonexistent pipeline");
    }

    fn handle_focus_iframe_msg(
        &self,
        parent_pipeline_id: PipelineId,
        browsing_context_id: BrowsingContextId,
        can_gc: CanGc,
    ) {
        let doc = self
            .documents
            .borrow()
            .find_document(parent_pipeline_id)
            .unwrap();
        let frame_element = doc.find_iframe(browsing_context_id);

        if let Some(ref frame_element) = frame_element {
            doc.request_focus(Some(frame_element.upcast()), FocusType::Parent, can_gc);
        }
    }

    fn handle_post_message_msg(
        &self,
        pipeline_id: PipelineId,
        source_pipeline_id: PipelineId,
        source_browsing_context: TopLevelBrowsingContextId,
        origin: Option<ImmutableOrigin>,
        source_origin: ImmutableOrigin,
        data: StructuredSerializedData,
    ) {
        let window = self.documents.borrow().find_window(pipeline_id);
        match window {
            None => warn!("postMessage after target pipeline {} closed.", pipeline_id),
            Some(window) => {
                // FIXME: synchronously talks to constellation.
                // send the required info as part of postmessage instead.
                let source = match self.remote_window_proxy(
                    &window.global(),
                    source_browsing_context,
                    source_pipeline_id,
                    None,
                ) {
                    None => {
                        return warn!(
                            "postMessage after source pipeline {} closed.",
                            source_pipeline_id,
                        );
                    },
                    Some(source) => source,
                };
                // FIXME(#22512): enqueues a task; unnecessary delay.
                window.post_message(origin, source_origin, &source, data)
            },
        }
    }

    fn handle_stop_delaying_load_events_mode(&self, pipeline_id: PipelineId) {
        let window = self.documents.borrow().find_window(pipeline_id);
        if let Some(window) = window {
            match window.undiscarded_window_proxy() {
                Some(window_proxy) => window_proxy.stop_delaying_load_events_mode(),
                None => warn!(
                    "Attempted to take {} of 'delaying-load-events-mode' after having been discarded.",
                    pipeline_id
                ),
            };
        }
    }

    fn handle_unload_document(&self, pipeline_id: PipelineId, can_gc: CanGc) {
        let document = self.documents.borrow().find_document(pipeline_id);
        if let Some(document) = document {
            document.unload(false, can_gc);
        }
    }

    fn handle_update_pipeline_id(
        &self,
        parent_pipeline_id: PipelineId,
        browsing_context_id: BrowsingContextId,
        top_level_browsing_context_id: TopLevelBrowsingContextId,
        new_pipeline_id: PipelineId,
        reason: UpdatePipelineIdReason,
        can_gc: CanGc,
    ) {
        let frame_element = self
            .documents
            .borrow()
            .find_iframe(parent_pipeline_id, browsing_context_id);
        if let Some(frame_element) = frame_element {
            frame_element.update_pipeline_id(new_pipeline_id, reason, can_gc);
        }

        if let Some(window) = self.documents.borrow().find_window(new_pipeline_id) {
            // Ensure that the state of any local window proxies accurately reflects
            // the new pipeline.
            let _ = self.local_window_proxy(
                &window,
                browsing_context_id,
                top_level_browsing_context_id,
                Some(parent_pipeline_id),
                // Any local window proxy has already been created, so there
                // is no need to pass along existing opener information that
                // will be discarded.
                None,
            );
        }
    }

    fn handle_update_history_state_msg(
        &self,
        pipeline_id: PipelineId,
        history_state_id: Option<HistoryStateId>,
        url: ServoUrl,
        can_gc: CanGc,
    ) {
        let window = self.documents.borrow().find_window(pipeline_id);
        match window {
            None => {
                warn!(
                    "update history state after pipeline {} closed.",
                    pipeline_id
                );
            },
            Some(window) => window
                .History()
                .activate_state(history_state_id, url, can_gc),
        }
    }

    fn handle_remove_history_states(
        &self,
        pipeline_id: PipelineId,
        history_states: Vec<HistoryStateId>,
    ) {
        let window = self.documents.borrow().find_window(pipeline_id);
        match window {
            None => {
                warn!(
                    "update history state after pipeline {} closed.",
                    pipeline_id
                );
            },
            Some(window) => window.History().remove_states(history_states),
        }
    }

    /// Window was resized, but this script was not active, so don't reflow yet
    fn handle_resize_inactive_msg(&self, id: PipelineId, new_size: WindowSizeData) {
        let window = self.documents.borrow().find_window(id)
            .expect("ScriptThread: received a resize msg for a pipeline not in this script thread. This is a bug.");
        window.set_window_size(new_size);
    }

    /// We have received notification that the response associated with a load has completed.
    /// Kick off the document and frame tree creation process using the result.
    fn handle_page_headers_available(
        &self,
        id: &PipelineId,
        metadata: Option<Metadata>,
        can_gc: CanGc,
    ) -> Option<DomRoot<ServoParser>> {
        let idx = self
            .incomplete_loads
            .borrow()
            .iter()
            .position(|load| load.pipeline_id == *id);
        // The matching in progress load structure may not exist if
        // the pipeline exited before the page load completed.
        match idx {
            Some(idx) => {
                // https://html.spec.whatwg.org/multipage/#process-a-navigate-response
                // 2. If response's status is 204 or 205, then abort these steps.
                let is20x = match metadata {
                    Some(ref metadata) => metadata.status.in_range(204..=205),
                    _ => false,
                };

                if is20x {
                    // If we have an existing window that is being navigated:
                    if let Some(window) = self.documents.borrow().find_window(*id) {
                        let window_proxy = window.window_proxy();
                        // https://html.spec.whatwg.org/multipage/
                        // #navigating-across-documents:delaying-load-events-mode-2
                        if window_proxy.parent().is_some() {
                            // The user agent must take this nested browsing context
                            // out of the delaying load events mode
                            // when this navigation algorithm later matures,
                            // or when it terminates (whether due to having run all the steps,
                            // or being canceled, or being aborted), whichever happens first.
                            window_proxy.stop_delaying_load_events_mode();
                        }
                    }
                    self.script_sender
                        .send((*id, ScriptMsg::AbortLoadUrl))
                        .unwrap();
                    return None;
                };

                let load = self.incomplete_loads.borrow_mut().remove(idx);
                metadata.map(|meta| self.load(meta, load, can_gc))
            },
            None => {
                assert!(self.closed_pipelines.borrow().contains(id));
                None
            },
        }
    }

    pub fn dom_manipulation_task_source(
        &self,
        pipeline_id: PipelineId,
    ) -> DOMManipulationTaskSource {
        DOMManipulationTaskSource(self.dom_manipulation_task_sender.clone(), pipeline_id)
    }

    pub fn gamepad_task_source(&self, pipeline_id: PipelineId) -> GamepadTaskSource {
        GamepadTaskSource(self.gamepad_task_sender.clone(), pipeline_id)
    }

    pub fn media_element_task_source(&self, pipeline_id: PipelineId) -> MediaElementTaskSource {
        MediaElementTaskSource(self.media_element_task_sender.clone(), pipeline_id)
    }

    pub fn performance_timeline_task_source(
        &self,
        pipeline_id: PipelineId,
    ) -> PerformanceTimelineTaskSource {
        PerformanceTimelineTaskSource(self.performance_timeline_task_sender.clone(), pipeline_id)
    }

    pub fn history_traversal_task_source(
        &self,
        pipeline_id: PipelineId,
    ) -> HistoryTraversalTaskSource {
        HistoryTraversalTaskSource(self.history_traversal_task_sender.clone(), pipeline_id)
    }

    pub fn user_interaction_task_source(
        &self,
        pipeline_id: PipelineId,
    ) -> UserInteractionTaskSource {
        UserInteractionTaskSource(self.user_interaction_task_sender.clone(), pipeline_id)
    }

    pub fn networking_task_source(&self, pipeline_id: PipelineId) -> NetworkingTaskSource {
        NetworkingTaskSource(self.networking_task_sender.clone(), pipeline_id)
    }

    pub fn port_message_queue(&self, pipeline_id: PipelineId) -> PortMessageQueue {
        PortMessageQueue(self.port_message_sender.clone(), pipeline_id)
    }

    pub fn file_reading_task_source(&self, pipeline_id: PipelineId) -> FileReadingTaskSource {
        FileReadingTaskSource(self.file_reading_task_sender.clone(), pipeline_id)
    }

    pub fn remote_event_task_source(&self, pipeline_id: PipelineId) -> RemoteEventTaskSource {
        RemoteEventTaskSource(self.remote_event_task_sender.clone(), pipeline_id)
    }

    fn rendering_task_source(&self, pipeline_id: PipelineId) -> RenderingTaskSource {
        RenderingTaskSource(self.rendering_task_sender.clone(), pipeline_id)
    }

    pub fn timer_task_source(&self, pipeline_id: PipelineId) -> TimerTaskSource {
        TimerTaskSource(self.timer_task_sender.clone(), pipeline_id)
    }

    pub fn websocket_task_source(&self, pipeline_id: PipelineId) -> WebsocketTaskSource {
        WebsocketTaskSource(self.remote_event_task_sender.clone(), pipeline_id)
    }

    /// Handles a request for the window title.
    fn handle_get_title_msg(&self, pipeline_id: PipelineId) {
        let document = match self.documents.borrow().find_document(pipeline_id) {
            Some(document) => document,
            None => return warn!("Message sent to closed pipeline {}.", pipeline_id),
        };
        document.send_title_to_embedder();
    }

    /// Handles a request to exit a pipeline and shut down layout.
    fn handle_exit_pipeline_msg(
        &self,
        id: PipelineId,
        discard_bc: DiscardBrowsingContext,
        can_gc: CanGc,
    ) {
        debug!("{id}: Starting pipeline exit.");

        self.closed_pipelines.borrow_mut().insert(id);

        // Abort the parser, if any,
        // to prevent any further incoming networking messages from being handled.
        let document = self.documents.borrow_mut().remove(id);
        if let Some(document) = document {
            // We should never have a pipeline that's still an incomplete load, but also has a Document.
            debug_assert!(!self
                .incomplete_loads
                .borrow()
                .iter()
                .any(|load| load.pipeline_id == id));

            if let Some(parser) = document.get_current_parser() {
                parser.abort(can_gc);
            }

            debug!("{id}: Shutting down layout");
            document.window().layout_mut().exit_now();

            debug!("{id}: Sending PipelineExited message to constellation");
            self.script_sender
                .send((id, ScriptMsg::PipelineExited))
                .ok();

            // Clear any active animations and unroot all of the associated DOM objects.
            debug!("{id}: Clearing animations");
            document.animations().clear();

            // We don't want to dispatch `mouseout` event pointing to non-existing element
            if let Some(target) = self.topmost_mouse_over_target.get() {
                if target.upcast::<Node>().owner_doc() == document {
                    self.topmost_mouse_over_target.set(None);
                }
            }

            // We discard the browsing context after requesting layout shut down,
            // to avoid running layout on detached iframes.
            let window = document.window();
            if discard_bc == DiscardBrowsingContext::Yes {
                window.discard_browsing_context();
            }

            debug!("{id}: Clearing JavaScript runtime");
            window.clear_js_runtime();
        }

        debug!("{id}: Finished pipeline exit");
    }

    /// Handles a request to exit the script thread and shut down layout.
    fn handle_exit_script_thread_msg(&self, can_gc: CanGc) {
        debug!("Exiting script thread.");

        let mut pipeline_ids = Vec::new();
        pipeline_ids.extend(
            self.incomplete_loads
                .borrow()
                .iter()
                .next()
                .map(|load| load.pipeline_id),
        );
        pipeline_ids.extend(
            self.documents
                .borrow()
                .iter()
                .next()
                .map(|(pipeline_id, _)| pipeline_id),
        );

        for pipeline_id in pipeline_ids {
            self.handle_exit_pipeline_msg(pipeline_id, DiscardBrowsingContext::Yes, can_gc);
        }

        self.background_hang_monitor.unregister();

        // If we're in multiprocess mode, shut-down the IPC router for this process.
        if opts::multiprocess() {
            debug!("Exiting IPC router thread in script thread.");
            ROUTER.shutdown();
        }

        debug!("Exited script thread.");
    }

    /// Handles animation tick requested during testing.
    pub fn handle_tick_all_animations_for_testing(id: PipelineId) {
        with_script_thread(|script_thread| {
            let Some(document) = script_thread.documents.borrow().find_document(id) else {
                warn!("Animation tick for tests for closed pipeline {id}.");
                return;
            };
            document.maybe_mark_animating_nodes_as_dirty();
        });
    }

    /// Handles a Web font being loaded. Does nothing if the page no longer exists.
    fn handle_web_font_loaded(&self, pipeline_id: PipelineId, _success: bool) {
        let Some(document) = self.documents.borrow().find_document(pipeline_id) else {
            warn!("Web font loaded in closed pipeline {}.", pipeline_id);
            return;
        };

        // TODO: This should only dirty nodes that are waiting for a web font to finish loading!
        document.dirty_all_nodes();
        document.window().add_pending_reflow();

        // This is required because the handlers added to the promise exposed at
        // `document.fonts.ready` are run by the event loop only when it performs a microtask
        // checkpoint. Without the call below, this never happens and the promise is 'stuck' waiting
        // to be resolved until another event forces a microtask checkpoint.
        self.rendering_opportunity(pipeline_id);
    }

    /// Handles a worklet being loaded. Does nothing if the page no longer exists.
    fn handle_worklet_loaded(&self, pipeline_id: PipelineId, can_gc: CanGc) {
        let document = self.documents.borrow().find_document(pipeline_id);
        if let Some(document) = document {
            self.rebuild_and_force_reflow(&document, ReflowReason::WorkletLoaded, can_gc);
        }
    }

    /// Notify a window of a storage event
    fn handle_storage_event(
        &self,
        pipeline_id: PipelineId,
        storage_type: StorageType,
        url: ServoUrl,
        key: Option<String>,
        old_value: Option<String>,
        new_value: Option<String>,
    ) {
        let window = match self.documents.borrow().find_window(pipeline_id) {
            None => return warn!("Storage event sent to closed pipeline {}.", pipeline_id),
            Some(window) => window,
        };

        let storage = match storage_type {
            StorageType::Local => window.LocalStorage(),
            StorageType::Session => window.SessionStorage(),
        };

        storage.queue_storage_event(url, key, old_value, new_value);
    }

    /// Notify the containing document of a child iframe that has completed loading.
    fn handle_iframe_load_event(
        &self,
        parent_id: PipelineId,
        browsing_context_id: BrowsingContextId,
        child_id: PipelineId,
        can_gc: CanGc,
    ) {
        let iframe = self
            .documents
            .borrow()
            .find_iframe(parent_id, browsing_context_id);
        match iframe {
            Some(iframe) => iframe.iframe_load_event_steps(child_id, can_gc),
            None => warn!("Message sent to closed pipeline {}.", parent_id),
        }
    }

    fn ask_constellation_for_browsing_context_info(
        &self,
        pipeline_id: PipelineId,
    ) -> Option<(BrowsingContextId, Option<PipelineId>)> {
        let (result_sender, result_receiver) = ipc::channel().unwrap();
        let msg = ScriptMsg::GetBrowsingContextInfo(pipeline_id, result_sender);
        self.script_sender
            .send((pipeline_id, msg))
            .expect("Failed to send to constellation.");
        result_receiver
            .recv()
            .expect("Failed to get browsing context info from constellation.")
    }

    fn ask_constellation_for_top_level_info(
        &self,
        sender_pipeline: PipelineId,
        browsing_context_id: BrowsingContextId,
    ) -> Option<TopLevelBrowsingContextId> {
        let (result_sender, result_receiver) = ipc::channel().unwrap();
        let msg = ScriptMsg::GetTopForBrowsingContext(browsing_context_id, result_sender);
        self.script_sender
            .send((sender_pipeline, msg))
            .expect("Failed to send to constellation.");
        result_receiver
            .recv()
            .expect("Failed to get top-level id from constellation.")
    }

    // Get the browsing context for a pipeline that may exist in another
    // script thread.  If the browsing context already exists in the
    // `window_proxies` map, we return it, otherwise we recursively
    // get the browsing context for the parent if there is one,
    // construct a new dissimilar-origin browsing context, add it
    // to the `window_proxies` map, and return it.
    fn remote_window_proxy(
        &self,
        global_to_clone: &GlobalScope,
        top_level_browsing_context_id: TopLevelBrowsingContextId,
        pipeline_id: PipelineId,
        opener: Option<BrowsingContextId>,
    ) -> Option<DomRoot<WindowProxy>> {
        let (browsing_context_id, parent_pipeline_id) =
            self.ask_constellation_for_browsing_context_info(pipeline_id)?;
        if let Some(window_proxy) = self.window_proxies.borrow().get(&browsing_context_id) {
            return Some(DomRoot::from_ref(window_proxy));
        }

        let parent_browsing_context = parent_pipeline_id.and_then(|parent_id| {
            self.remote_window_proxy(
                global_to_clone,
                top_level_browsing_context_id,
                parent_id,
                opener,
            )
        });

        let opener_browsing_context = opener.and_then(ScriptThread::find_window_proxy);

        let creator = CreatorBrowsingContextInfo::from(
            parent_browsing_context.as_deref(),
            opener_browsing_context.as_deref(),
        );

        let window_proxy = WindowProxy::new_dissimilar_origin(
            global_to_clone,
            browsing_context_id,
            top_level_browsing_context_id,
            parent_browsing_context.as_deref(),
            opener,
            creator,
        );
        self.window_proxies
            .borrow_mut()
            .insert(browsing_context_id, Dom::from_ref(&*window_proxy));
        Some(window_proxy)
    }

    // Get the browsing context for a pipeline that exists in this
    // script thread.  If the browsing context already exists in the
    // `window_proxies` map, we return it, otherwise we recursively
    // get the browsing context for the parent if there is one,
    // construct a new similar-origin browsing context, add it
    // to the `window_proxies` map, and return it.
    fn local_window_proxy(
        &self,
        window: &Window,
        browsing_context_id: BrowsingContextId,
        top_level_browsing_context_id: TopLevelBrowsingContextId,
        parent_info: Option<PipelineId>,
        opener: Option<BrowsingContextId>,
    ) -> DomRoot<WindowProxy> {
        if let Some(window_proxy) = self.window_proxies.borrow().get(&browsing_context_id) {
            // Note: we do not set the window to be the currently-active one,
            // this will be done instead when the script-thread handles the `SetDocumentActivity` msg.
            return DomRoot::from_ref(window_proxy);
        }
        let iframe = parent_info.and_then(|parent_id| {
            self.documents
                .borrow()
                .find_iframe(parent_id, browsing_context_id)
        });
        let parent_browsing_context = match (parent_info, iframe.as_ref()) {
            (_, Some(iframe)) => Some(window_from_node(&**iframe).window_proxy()),
            (Some(parent_id), _) => self.remote_window_proxy(
                window.upcast(),
                top_level_browsing_context_id,
                parent_id,
                opener,
            ),
            _ => None,
        };

        let opener_browsing_context = opener.and_then(ScriptThread::find_window_proxy);

        let creator = CreatorBrowsingContextInfo::from(
            parent_browsing_context.as_deref(),
            opener_browsing_context.as_deref(),
        );

        let window_proxy = WindowProxy::new(
            window,
            browsing_context_id,
            top_level_browsing_context_id,
            iframe.as_deref().map(Castable::upcast),
            parent_browsing_context.as_deref(),
            opener,
            creator,
        );
        self.window_proxies
            .borrow_mut()
            .insert(browsing_context_id, Dom::from_ref(&*window_proxy));
        window_proxy
    }

    /// The entry point to document loading. Defines bindings, sets up the window and document
    /// objects, parses HTML and CSS, and kicks off initial layout.
    fn load(
        &self,
        metadata: Metadata,
        incomplete: InProgressLoad,
        can_gc: CanGc,
    ) -> DomRoot<ServoParser> {
        let final_url = metadata.final_url.clone();
        {
            self.script_sender
                .send((
                    incomplete.pipeline_id,
                    ScriptMsg::SetFinalUrl(final_url.clone()),
                ))
                .unwrap();
        }
        debug!(
            "ScriptThread: loading {} on pipeline {:?}",
            incomplete.url, incomplete.pipeline_id
        );

        let MainThreadScriptChan(ref sender) = self.chan;

        let origin = if final_url.as_str() == "about:blank" || final_url.as_str() == "about:srcdoc"
        {
            incomplete.origin.clone()
        } else {
            MutableOrigin::new(final_url.origin())
        };

        let script_to_constellation_chan = ScriptToConstellationChan {
            sender: self.script_sender.clone(),
            pipeline_id: incomplete.pipeline_id,
        };

        let task_manager = TaskManager::new(
            self.dom_manipulation_task_source(incomplete.pipeline_id),
            self.file_reading_task_source(incomplete.pipeline_id),
            self.gamepad_task_source(incomplete.pipeline_id),
            self.history_traversal_task_source(incomplete.pipeline_id),
            self.media_element_task_source(incomplete.pipeline_id),
            self.networking_task_source(incomplete.pipeline_id),
            self.performance_timeline_task_source(incomplete.pipeline_id)
                .clone(),
            self.port_message_queue(incomplete.pipeline_id),
            self.user_interaction_task_source(incomplete.pipeline_id),
            self.remote_event_task_source(incomplete.pipeline_id),
            self.rendering_task_source(incomplete.pipeline_id),
            self.timer_task_source(incomplete.pipeline_id),
            self.websocket_task_source(incomplete.pipeline_id),
        );

        let paint_time_metrics = PaintTimeMetrics::new(
            incomplete.pipeline_id,
            self.time_profiler_chan.clone(),
            self.layout_to_constellation_chan.clone(),
            self.control_chan.clone(),
            final_url.clone(),
            incomplete.navigation_start,
        );

        let layout_config = LayoutConfig {
            id: incomplete.pipeline_id,
            url: final_url.clone(),
            is_iframe: incomplete.parent_info.is_some(),
            constellation_chan: self.layout_to_constellation_chan.clone(),
            script_chan: self.control_chan.clone(),
            image_cache: self.image_cache.clone(),
            system_font_service: self.system_font_service.clone(),
            resource_threads: self.resource_threads.clone(),
            time_profiler_chan: self.time_profiler_chan.clone(),
            compositor_api: self.compositor_api.clone(),
            paint_time_metrics,
            window_size: incomplete.window_size,
        };

        // Create the window and document objects.
        let window = Window::new(
            self.js_runtime.clone(),
            MainThreadScriptChan(sender.clone()),
            task_manager,
            self.layout_factory.create(layout_config),
            self.image_cache_channel.clone(),
            self.image_cache.clone(),
            self.resource_threads.clone(),
            self.bluetooth_thread.clone(),
            self.mem_profiler_chan.clone(),
            self.time_profiler_chan.clone(),
            self.devtools_chan.clone(),
            script_to_constellation_chan,
            self.control_chan.clone(),
            self.scheduler_chan.clone(),
            incomplete.pipeline_id,
            incomplete.parent_info,
            incomplete.window_size,
            origin.clone(),
            final_url.clone(),
            incomplete.navigation_start,
            self.webgl_chan.as_ref().map(|chan| chan.channel()),
            #[cfg(feature = "webxr")]
            self.webxr_registry.clone(),
            self.microtask_queue.clone(),
            self.webrender_document,
            self.compositor_api.clone(),
            self.relayout_event,
            self.prepare_for_screenshot,
            self.unminify_js,
            self.unminify_css,
            self.local_script_source.clone(),
            self.userscripts_path.clone(),
            self.headless,
            self.replace_surrogates,
            self.user_agent.clone(),
            self.player_context.clone(),
            self.gpu_id_hub.clone(),
            incomplete.inherited_secure_context,
        );

        let _realm = enter_realm(&*window);

        // Initialize the browsing context for the window.
        let window_proxy = self.local_window_proxy(
            &window,
            incomplete.browsing_context_id,
            incomplete.top_level_browsing_context_id,
            incomplete.parent_info,
            incomplete.opener,
        );
        if window_proxy.parent().is_some() {
            // https://html.spec.whatwg.org/multipage/#navigating-across-documents:delaying-load-events-mode-2
            // The user agent must take this nested browsing context
            // out of the delaying load events mode
            // when this navigation algorithm later matures.
            window_proxy.stop_delaying_load_events_mode();
        }
        window.init_window_proxy(&window_proxy);

        let last_modified = metadata.headers.as_ref().and_then(|headers| {
            headers.typed_get::<LastModified>().map(|tm| {
                let tm: SystemTime = tm.into();
                let local_time: DateTime<Local> = tm.into();
                local_time.format("%m/%d/%Y %H:%M:%S").to_string()
            })
        });

        let loader = DocumentLoader::new_with_threads(
            self.resource_threads.clone(),
            Some(final_url.clone()),
        );

        let content_type: Option<Mime> =
            metadata.content_type.map(Serde::into_inner).map(Into::into);

        let is_html_document = match content_type {
            Some(ref mime)
                if mime.type_() == mime::APPLICATION && mime.suffix() == Some(mime::XML) =>
            {
                IsHTMLDocument::NonHTMLDocument
            },

            Some(ref mime)
                if (mime.type_() == mime::TEXT && mime.subtype() == mime::XML) ||
                    (mime.type_() == mime::APPLICATION && mime.subtype() == mime::XML) =>
            {
                IsHTMLDocument::NonHTMLDocument
            },
            _ => IsHTMLDocument::HTMLDocument,
        };

        let referrer = metadata
            .referrer
            .as_ref()
            .map(|referrer| referrer.clone().into_string());

        let document = Document::new(
            &window,
            HasBrowsingContext::Yes,
            Some(final_url.clone()),
            origin,
            is_html_document,
            content_type,
            last_modified,
            incomplete.activity,
            DocumentSource::FromParser,
            loader,
            referrer,
            Some(metadata.status.raw_code()),
            incomplete.canceller,
            can_gc,
        );

        let referrer_policy = metadata
            .headers
            .as_deref()
            .and_then(|h| h.typed_get::<ReferrerPolicyHeader>())
            .into();

        document.set_referrer_policy(referrer_policy);
        document.set_ready_state(DocumentReadyState::Loading, can_gc);

        self.documents
            .borrow_mut()
            .insert(incomplete.pipeline_id, &document);

        window.init_document(&document);

        // For any similar-origin iframe, ensure that the contentWindow/contentDocument
        // APIs resolve to the new window/document as soon as parsing starts.
        if let Some(frame) = window_proxy
            .frame_element()
            .and_then(|e| e.downcast::<HTMLIFrameElement>())
        {
            let parent_pipeline = frame.global().pipeline_id();
            self.handle_update_pipeline_id(
                parent_pipeline,
                window_proxy.browsing_context_id(),
                window_proxy.top_level_browsing_context_id(),
                incomplete.pipeline_id,
                UpdatePipelineIdReason::Navigation,
                can_gc,
            );
        }

        self.script_sender
            .send((incomplete.pipeline_id, ScriptMsg::ActivateDocument))
            .unwrap();

        // Notify devtools that a new script global exists.
        if incomplete.top_level_browsing_context_id.0 == incomplete.browsing_context_id {
            self.notify_devtools(
                document.Title(),
                final_url.clone(),
                (incomplete.browsing_context_id, incomplete.pipeline_id, None),
            );
        }

        document.set_https_state(metadata.https_state);
        document.set_navigation_start(incomplete.navigation_start);

        if is_html_document == IsHTMLDocument::NonHTMLDocument {
            ServoParser::parse_xml_document(&document, None, final_url, can_gc);
        } else {
            ServoParser::parse_html_document(&document, None, final_url, can_gc);
        }

        if incomplete.activity == DocumentActivity::FullyActive {
            window.resume();
        } else {
            window.suspend();
        }

        if incomplete.throttled {
            window.set_throttled(true);
        }

        document.get_current_parser().unwrap()
    }

    fn notify_devtools(
        &self,
        title: DOMString,
        url: ServoUrl,
        (bc, p, w): (BrowsingContextId, PipelineId, Option<WorkerId>),
    ) {
        if let Some(ref chan) = self.devtools_chan {
            let page_info = DevtoolsPageInfo {
                title: String::from(title),
                url,
            };
            chan.send(ScriptToDevtoolsControlMsg::NewGlobal(
                (bc, p, w),
                self.devtools_sender.clone(),
                page_info.clone(),
            ))
            .unwrap();

            let state = NavigationState::Stop(p, page_info);
            let _ = chan.send(ScriptToDevtoolsControlMsg::Navigate(bc, state));
        }
    }

    /// Reflows non-incrementally, rebuilding the entire layout tree in the process.
    fn rebuild_and_force_reflow(&self, document: &Document, reason: ReflowReason, can_gc: CanGc) {
        let window = window_from_node(document);
        document.dirty_all_nodes();
        window.reflow(ReflowGoal::Full, reason, can_gc);
    }

    /// Queue compositor events for later dispatching as part of a
    /// `update_the_rendering` task.
    fn handle_event(&self, pipeline_id: PipelineId, event: CompositorEvent) {
        let Some(document) = self.documents.borrow().find_document(pipeline_id) else {
            warn!("Compositor event sent to closed pipeline {pipeline_id}.");
            return;
        };
        self.rendering_opportunity(pipeline_id);
        document.note_pending_compositor_event(event);
    }

    #[allow(clippy::too_many_arguments)]
    fn handle_mouse_button_event(
        &self,
        pipeline_id: PipelineId,
        mouse_event_type: MouseEventType,
        button: MouseButton,
        point: Point2D<f32>,
        node_address: Option<UntrustedNodeAddress>,
        point_in_node: Option<Point2D<f32>>,
        pressed_mouse_buttons: u16,
        can_gc: CanGc,
    ) {
        let Some(document) = self.documents.borrow().find_document(pipeline_id) else {
            warn!("Message sent to closed pipeline {pipeline_id}.");
            return;
        };
        unsafe {
            document.handle_mouse_button_event(
                button,
                point,
                mouse_event_type,
                node_address,
                point_in_node,
                pressed_mouse_buttons,
                can_gc,
            )
        }
    }

    fn handle_touch_event(
        &self,
        pipeline_id: PipelineId,
        event_type: TouchEventType,
        identifier: TouchId,
        point: Point2D<f32>,
        node_address: Option<UntrustedNodeAddress>,
        can_gc: CanGc,
    ) -> TouchEventResult {
        let document = match self.documents.borrow().find_document(pipeline_id) {
            Some(document) => document,
            None => {
                warn!("Message sent to closed pipeline {}.", pipeline_id);
                return TouchEventResult::Processed(true);
            },
        };
        unsafe { document.handle_touch_event(event_type, identifier, point, node_address, can_gc) }
    }

    fn handle_wheel_event(
        &self,
        pipeline_id: PipelineId,
        wheel_delta: WheelDelta,
        point: Point2D<f32>,
        node_address: Option<UntrustedNodeAddress>,
        can_gc: CanGc,
    ) {
        let Some(document) = self.documents.borrow().find_document(pipeline_id) else {
            warn!("Message sent to closed pipeline {pipeline_id}.");
            return;
        };
        unsafe { document.handle_wheel_event(wheel_delta, point, node_address, can_gc) };
    }

    /// Handle a "navigate an iframe" message from the constellation.
    fn handle_navigate_iframe(
        &self,
        parent_pipeline_id: PipelineId,
        browsing_context_id: BrowsingContextId,
        load_data: LoadData,
        replace: HistoryEntryReplacement,
        can_gc: CanGc,
    ) {
        let iframe = self
            .documents
            .borrow()
            .find_iframe(parent_pipeline_id, browsing_context_id);
        if let Some(iframe) = iframe {
            iframe.navigate_or_reload_child_browsing_context(load_data, replace, can_gc);
        }
    }

    /// Turn javascript: URL into JS code to eval, according to the steps in
    /// <https://html.spec.whatwg.org/multipage/#javascript-protocol>
    pub fn eval_js_url(global_scope: &GlobalScope, load_data: &mut LoadData, can_gc: CanGc) {
        // This slice of the URL’s serialization is equivalent to (5.) to (7.):
        // Start with the scheme data of the parsed URL;
        // append question mark and query component, if any;
        // append number sign and fragment component if any.
        let encoded = &load_data.url.clone()[Position::BeforePath..];

        // Percent-decode (8.) and UTF-8 decode (9.)
        let script_source = percent_decode(encoded.as_bytes()).decode_utf8_lossy();

        // Script source is ready to be evaluated (11.)
        let _ac = enter_realm(global_scope);
        rooted!(in(*GlobalScope::get_cx()) let mut jsval = UndefinedValue());
        global_scope.evaluate_js_on_global_with_result(
            &script_source,
            jsval.handle_mut(),
            ScriptFetchOptions::default_classic_script(global_scope),
            global_scope.api_base_url(),
            can_gc,
        );

        load_data.js_eval_result = if jsval.get().is_string() {
            unsafe {
                let strval = DOMString::from_jsval(
                    *GlobalScope::get_cx(),
                    jsval.handle(),
                    StringificationBehavior::Empty,
                );
                match strval {
                    Ok(ConversionResult::Success(s)) => {
                        Some(JsEvalResult::Ok(String::from(s).as_bytes().to_vec()))
                    },
                    _ => None,
                }
            }
        } else {
            Some(JsEvalResult::NoContent)
        };

        load_data.url = ServoUrl::parse("about:blank").unwrap();
    }

    fn handle_resize_event(
        &self,
        pipeline_id: PipelineId,
        new_size: WindowSizeData,
        size_type: WindowSizeType,
        can_gc: CanGc,
    ) {
        let document = match self.documents.borrow().find_document(pipeline_id) {
            Some(document) => document,
            None => return warn!("Message sent to closed pipeline {}.", pipeline_id),
        };

        let window = document.window();
        if window.window_size() == new_size {
            return;
        }
        debug!(
            "resizing pipeline {:?} from {:?} to {:?}",
            pipeline_id,
            window.window_size(),
            new_size
        );
        window.set_window_size(new_size);
        window.force_reflow(ReflowGoal::Full, ReflowReason::WindowResize, None);

        // http://dev.w3.org/csswg/cssom-view/#resizing-viewports
        if size_type == WindowSizeType::Resize {
            let uievent = UIEvent::new(
                window,
                DOMString::from("resize"),
                EventBubbles::DoesNotBubble,
                EventCancelable::NotCancelable,
                Some(window),
                0i32,
                can_gc,
            );
            uievent.upcast::<Event>().fire(window.upcast(), can_gc);
        }
    }

    /// Instructs the constellation to fetch the document that will be loaded. Stores the InProgressLoad
    /// argument until a notification is received that the fetch is complete.
    fn pre_page_load(&self, mut incomplete: InProgressLoad, load_data: LoadData) {
        let id = incomplete.pipeline_id;
        let req_init = RequestBuilder::new(load_data.url.clone(), load_data.referrer)
            .method(load_data.method)
            .destination(Destination::Document)
            .mode(RequestMode::Navigate)
            .credentials_mode(CredentialsMode::Include)
            .use_url_credentials(true)
            .pipeline_id(Some(id))
            .referrer_policy(load_data.referrer_policy)
            .headers(load_data.headers)
            .body(load_data.data)
            .redirect_mode(RedirectMode::Manual)
            .origin(incomplete.origin.immutable().clone())
            .crash(load_data.crash);

        let context = ParserContext::new(id, load_data.url);
        self.incomplete_parser_contexts
            .0
            .borrow_mut()
            .push((id, context));

        let cancel_chan = incomplete.canceller.initialize();

        self.script_sender
            .send((
                id,
                ScriptMsg::InitiateNavigateRequest(req_init, cancel_chan),
            ))
            .unwrap();
        self.incomplete_loads.borrow_mut().push(incomplete);
    }

    fn handle_fetch_metadata(
        &self,
        id: PipelineId,
        request_id: RequestId,
        fetch_metadata: Result<FetchMetadata, NetworkError>,
    ) {
        match fetch_metadata {
            Ok(_) => (),
            Err(NetworkError::Crash(..)) => (),
            Err(ref e) => {
                warn!("Network error: {:?}", e);
            },
        };

        let mut incomplete_parser_contexts = self.incomplete_parser_contexts.0.borrow_mut();
        let parser = incomplete_parser_contexts
            .iter_mut()
            .find(|&&mut (pipeline_id, _)| pipeline_id == id);
        if let Some(&mut (_, ref mut ctxt)) = parser {
            ctxt.process_response(request_id, fetch_metadata);
        }
    }

    fn handle_fetch_chunk(&self, pipeline_id: PipelineId, request_id: RequestId, chunk: Vec<u8>) {
        let mut incomplete_parser_contexts = self.incomplete_parser_contexts.0.borrow_mut();
        let parser = incomplete_parser_contexts
            .iter_mut()
            .find(|&&mut (parser_pipeline_id, _)| parser_pipeline_id == pipeline_id);
        if let Some(&mut (_, ref mut ctxt)) = parser {
            ctxt.process_response_chunk(request_id, chunk);
        }
    }

    fn handle_fetch_eof(
        &self,
        id: PipelineId,
        request_id: RequestId,
        eof: Result<ResourceFetchTiming, NetworkError>,
    ) {
        let idx = self
            .incomplete_parser_contexts
            .0
            .borrow()
            .iter()
            .position(|&(pipeline_id, _)| pipeline_id == id);

        if let Some(idx) = idx {
            let (_, mut ctxt) = self.incomplete_parser_contexts.0.borrow_mut().remove(idx);
            ctxt.process_response_eof(request_id, eof);
        }
    }

    /// Synchronously fetch `about:blank`. Stores the `InProgressLoad`
    /// argument until a notification is received that the fetch is complete.
    fn start_page_load_about_blank(&self, incomplete: InProgressLoad, load_data: LoadData) {
        let id = incomplete.pipeline_id;

        self.incomplete_loads.borrow_mut().push(incomplete);

        let url = ServoUrl::parse("about:blank").unwrap();
        let mut context = ParserContext::new(id, url.clone());

        let mut meta = Metadata::default(url);
        meta.set_content_type(Some(&mime::TEXT_HTML));
        meta.set_referrer_policy(load_data.referrer_policy);

        // If this page load is the result of a javascript scheme url, map
        // the evaluation result into a response.
        let chunk = match load_data.js_eval_result {
            Some(JsEvalResult::Ok(content)) => content,
            Some(JsEvalResult::NoContent) => {
                meta.status = http::StatusCode::NO_CONTENT.into();
                vec![]
            },
            None => vec![],
        };

        let dummy_request_id = RequestId::next();
        context.process_response(dummy_request_id, Ok(FetchMetadata::Unfiltered(meta)));
        context.process_response_chunk(dummy_request_id, chunk);
        context.process_response_eof(
            dummy_request_id,
            Ok(ResourceFetchTiming::new(ResourceTimingType::None)),
        );
    }

    /// Synchronously parse a srcdoc document from a giving HTML string.
    fn page_load_about_srcdoc(&self, incomplete: InProgressLoad, load_data: LoadData) {
        let id = incomplete.pipeline_id;

        self.incomplete_loads.borrow_mut().push(incomplete);

        let url = ServoUrl::parse("about:srcdoc").unwrap();
        let mut context = ParserContext::new(id, url.clone());

        let mut meta = Metadata::default(url);
        meta.set_content_type(Some(&mime::TEXT_HTML));
        meta.set_referrer_policy(load_data.referrer_policy);

        let chunk = load_data.srcdoc.into_bytes();

        let dummy_request_id = RequestId::next();
        context.process_response(dummy_request_id, Ok(FetchMetadata::Unfiltered(meta)));
        context.process_response_chunk(dummy_request_id, chunk);
        context.process_response_eof(
            dummy_request_id,
            Ok(ResourceFetchTiming::new(ResourceTimingType::None)),
        );
    }

    fn handle_css_error_reporting(
        &self,
        pipeline_id: PipelineId,
        filename: String,
        line: u32,
        column: u32,
        msg: String,
    ) {
        let sender = match self.devtools_chan {
            Some(ref sender) => sender,
            None => return,
        };

        if let Some(global) = self.documents.borrow().find_global(pipeline_id) {
            if global.live_devtools_updates() {
                let css_error = CSSError {
                    filename,
                    line,
                    column,
                    msg,
                };
                let message = ScriptToDevtoolsControlMsg::ReportCSSError(pipeline_id, css_error);
                sender.send(message).unwrap();
            }
        }
    }

    fn handle_reload(&self, pipeline_id: PipelineId, can_gc: CanGc) {
        let window = self.documents.borrow().find_window(pipeline_id);
        if let Some(window) = window {
            window.Location().reload_without_origin_check(can_gc);
        }
    }

    fn handle_paint_metric(
        &self,
        pipeline_id: PipelineId,
        metric_type: ProgressiveWebMetricType,
        metric_value: CrossProcessInstant,
        can_gc: CanGc,
    ) {
        let window = self.documents.borrow().find_window(pipeline_id);
        if let Some(window) = window {
            let entry = PerformancePaintTiming::new(
                window.upcast::<GlobalScope>(),
                metric_type,
                metric_value,
            );
            window
                .Performance()
                .queue_entry(entry.upcast::<PerformanceEntry>(), can_gc);
        }
    }

    fn handle_media_session_action(
        &self,
        pipeline_id: PipelineId,
        action: MediaSessionActionType,
        can_gc: CanGc,
    ) {
        if let Some(window) = self.documents.borrow().find_window(pipeline_id) {
            let media_session = window.Navigator().MediaSession();
            media_session.handle_action(action, can_gc);
        } else {
            warn!("No MediaSession for this pipeline ID");
        };
    }

    pub fn enqueue_microtask(job: Microtask) {
        with_script_thread(|script_thread| {
            script_thread
                .microtask_queue
                .enqueue(job, script_thread.get_cx());
        });
    }

    fn perform_a_microtask_checkpoint(&self, can_gc: CanGc) {
        // Only perform the checkpoint if we're not shutting down.
        if self.can_continue_running_inner() {
            let globals = self
                .documents
                .borrow()
                .iter()
                .map(|(_id, document)| DomRoot::from_ref(document.window().upcast()))
                .collect();

            self.microtask_queue.checkpoint(
                self.get_cx(),
                |id| self.documents.borrow().find_global(id),
                globals,
                can_gc,
            )
        }
    }
}

impl Drop for ScriptThread {
    fn drop(&mut self) {
        SCRIPT_THREAD_ROOT.with(|root| {
            root.set(None);
        });
    }
}