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
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 http://mozilla.org/MPL/2.0/. */
/* a presentation of a document, part 1 */
#include "nsPresContext.h"
#include "nsPresContextInlines.h"
#include "mozilla/ArrayUtils.h"
#if defined(MOZ_WIDGET_ANDROID)
# include "mozilla/AsyncEventDispatcher.h"
#endif
#include "mozilla/CycleCollectedJSContext.h"
#include "mozilla/DebugOnly.h"
#include "mozilla/Encoding.h"
#include "mozilla/EventDispatcher.h"
#include "mozilla/EventStateManager.h"
#include "mozilla/PresShell.h"
#include "mozilla/PresShellInlines.h"
#include "base/basictypes.h"
#include "nsCRT.h"
#include "nsCOMPtr.h"
#include "nsCSSFrameConstructor.h"
#include "nsDocShell.h"
#include "nsIConsoleService.h"
#include "nsIDocumentViewer.h"
#include "nsPIDOMWindow.h"
#include "mozilla/ServoStyleSet.h"
#include "mozilla/MediaFeatureChange.h"
#include "nsIContent.h"
#include "nsIFrame.h"
#include "mozilla/dom/BrowsingContext.h"
#include "mozilla/dom/Document.h"
#include "mozilla/dom/DocumentInlines.h"
#include "nsIPrintSettings.h"
#include "nsLanguageAtomService.h"
#include "mozilla/LookAndFeel.h"
#include "nsIInterfaceRequestorUtils.h"
#include "nsHTMLDocument.h"
#include "nsIWeakReferenceUtils.h"
#include "nsThreadUtils.h"
#include "nsLayoutUtils.h"
#include "nsViewManager.h"
#include "mozilla/RestyleManager.h"
#include "gfxPlatform.h"
#include "nsFontFaceLoader.h"
#include "mozilla/AnimationEventDispatcher.h"
#include "mozilla/EffectCompositor.h"
#include "mozilla/EventListenerManager.h"
#include "prenv.h"
#include "nsTransitionManager.h"
#include "nsAnimationManager.h"
#include "CounterStyleManager.h"
#include "mozilla/MemoryReporting.h"
#include "mozilla/dom/Element.h"
#include "nsIMessageManager.h"
#include "mozilla/dom/HTMLBodyElement.h"
#include "mozilla/dom/MediaQueryList.h"
#include "mozilla/SMILAnimationController.h"
#include "mozilla/css/ImageLoader.h"
#include "mozilla/dom/PBrowserParent.h"
#include "mozilla/dom/BrowserChild.h"
#include "mozilla/dom/BrowserParent.h"
#include "mozilla/dom/FontFaceSet.h"
#include "mozilla/StaticPresData.h"
#include "nsRefreshDriver.h"
#include "LayerUserData.h"
#include "mozilla/dom/NotifyPaintEvent.h"
#include "nsFontCache.h"
#include "nsFrameLoader.h"
#include "nsContentUtils.h"
#include "nsPIWindowRoot.h"
#include "mozilla/Preferences.h"
#include "gfxTextRun.h"
#include "nsFontFaceUtils.h"
#include "COLRFonts.h"
#include "mozilla/ContentBlockingAllowList.h"
#include "mozilla/GlobalStyleSheetCache.h"
#include "mozilla/ServoBindings.h"
#include "mozilla/StaticPrefs_bidi.h"
#include "mozilla/StaticPrefs_layout.h"
#include "mozilla/StaticPrefs_widget.h"
#include "mozilla/StaticPrefs_zoom.h"
#include "mozilla/StyleSheet.h"
#include "mozilla/StyleSheetInlines.h"
#include "mozilla/Telemetry.h"
#include "mozilla/TimelineManager.h"
#include "mozilla/dom/Performance.h"
#include "mozilla/dom/PerformanceMainThread.h"
#include "mozilla/dom/PerformanceTiming.h"
#include "mozilla/dom/PerformancePaintTiming.h"
#include "mozilla/layers/APZThreadUtils.h"
#include "MobileViewportManager.h"
#include "mozilla/dom/ImageTracker.h"
#ifdef ACCESSIBILITY
# include "mozilla/a11y/DocAccessible.h"
#endif
// Needed for Start/Stop of Image Animation
#include "imgIContainer.h"
#include "nsIImageLoadingContent.h"
#include "nsBidiUtils.h"
#include "nsServiceManagerUtils.h"
#include "mozilla/dom/URL.h"
#include "mozilla/ServoCSSParser.h"
using namespace mozilla;
using namespace mozilla::dom;
using namespace mozilla::gfx;
using namespace mozilla::layers;
/**
* Layer UserData for ContainerLayers that want to be notified
* of local invalidations of them and their descendant layers.
* Pass a callback to ComputeDifferences to have these called.
*/
class ContainerLayerPresContext : public LayerUserData {
public:
nsPresContext* mPresContext;
};
bool nsPresContext::IsDOMPaintEventPending() {
if (!mTransactions.IsEmpty()) {
return true;
}
nsRootPresContext* drpc = GetRootPresContext();
if (drpc && drpc->mRefreshDriver->ViewManagerFlushIsPending()) {
// Since we're promising that there will be a MozAfterPaint event
// fired, we record an empty invalidation in case display list
// invalidation doesn't invalidate anything further.
NotifyInvalidation(drpc->mRefreshDriver->LastTransactionId().Next(),
nsRect(0, 0, 0, 0));
return true;
}
return false;
}
struct WeakRunnableMethod : Runnable {
using Method = void (nsPresContext::*)();
WeakRunnableMethod(const char* aName, nsPresContext* aPc, Method aMethod)
: Runnable(aName), mPresContext(aPc), mMethod(aMethod) {}
NS_IMETHOD Run() override {
if (nsPresContext* pc = mPresContext.get()) {
(pc->*mMethod)();
}
return NS_OK;
}
private:
WeakPtr<nsPresContext> mPresContext;
Method mMethod;
};
// When forcing a font-info-update reflow from style, we don't need to reframe,
// but we'll need to restyle to pick up updated font metrics. In order to avoid
// synchronously having to deal with multiple restyles, we use an early refresh
// driver runner, which should prevent flashing for users.
//
// We might do a bit of extra work if the page flushes layout between the
// restyle and when this happens, which is a bit unfortunate, but not worse than
// what we used to do...
//
// A better solution would be to be able to synchronously initialize font
// information from style worker threads, perhaps...
void nsPresContext::ForceReflowForFontInfoUpdateFromStyle() {
if (mPendingFontInfoUpdateReflowFromStyle) {
return;
}
mPendingFontInfoUpdateReflowFromStyle = true;
nsCOMPtr<nsIRunnable> ev = new WeakRunnableMethod(
"nsPresContext::DoForceReflowForFontInfoUpdateFromStyle", this,
&nsPresContext::DoForceReflowForFontInfoUpdateFromStyle);
RefreshDriver()->AddEarlyRunner(ev);
}
void nsPresContext::DoForceReflowForFontInfoUpdateFromStyle() {
mPendingFontInfoUpdateReflowFromStyle = false;
ForceReflowForFontInfoUpdate(false);
}
void nsPresContext::ForceReflowForFontInfoUpdate(bool aNeedsReframe) {
// In the case of a static-clone document used for printing or print-preview,
// this is undesirable because the nsPrintJob is holding weak refs to frames
// that will get blown away unexpectedly by this reconstruction. So the
// prescontext for a print/preview doc ignores the font-list update.
//
// This means the print document may still be using cached fonts that are no
// longer present in the font list, but that should be safe given that all the
// required font instances have already been created, so it won't be depending
// on access to the font-list entries.
//
// XXX Actually, I think it's probably a bad idea to do *any* restyling of
// print documents in response to pref changes. We could be in the middle
// of printing the document, and reflowing all the frames might cause some
// kind of unwanted mid-document discontinuity.
if (IsPrintingOrPrintPreview()) {
return;
}
// If there's a user font set, discard any src:local() faces it may have
// loaded because their font entries may no longer be valid.
if (auto* fonts = Document()->GetFonts()) {
fonts->GetImpl()->ForgetLocalFaces();
}
FlushFontCache();
if (!mPresShell) {
// RebuildAllStyleData won't do anything without mPresShell.
return;
}
nsChangeHint changeHint =
aNeedsReframe ? nsChangeHint_ReconstructFrame : NS_STYLE_HINT_REFLOW;
// We also need to trigger restyling for ex/ch units changes to take effect,
// if needed.
auto restyleHint = StyleSet()->UsesFontMetrics()
? RestyleHint::RecascadeSubtree()
: RestyleHint{0};
RebuildAllStyleData(changeHint, restyleHint);
}
static bool IsVisualCharset(NotNull<const Encoding*> aCharset) {
return aCharset == ISO_8859_8_ENCODING;
}
nsPresContext::nsPresContext(dom::Document* aDocument, nsPresContextType aType)
: mPresShell(nullptr),
mDocument(aDocument),
mMedium(aType == eContext_Galley ? nsGkAtoms::screen : nsGkAtoms::print),
mTextZoom(1.0),
mFullZoom(1.0),
mLastFontInflationScreenSize(gfxSize(-1.0, -1.0)),
mCurAppUnitsPerDevPixel(0),
mDynamicToolbarMaxHeight(0),
mDynamicToolbarHeight(0),
mPageSize(-1, -1),
mPageScale(0.0),
mPPScale(1.0f),
mViewportScrollOverrideElement(nullptr),
mElementsRestyled(0),
mFramesConstructed(0),
mFramesReflowed(0),
mAnimationTriggeredRestyles(0),
mInterruptChecksToSkip(0),
mNextFrameRateMultiplier(0),
mMeasuredTicksSinceLoading(0),
mViewportScrollStyles(StyleOverflow::Auto, StyleOverflow::Auto),
// mImageAnimationMode is initialised below, in constructor body
mImageAnimationModePref(imgIContainer::kNormalAnimMode),
mType(aType),
mInflationDisabledForShrinkWrap(false),
mInteractionTimeEnabled(true),
mHasPendingInterrupt(false),
mHasEverBuiltInvisibleText(false),
mPendingInterruptFromTest(false),
mInterruptsEnabled(false),
mDrawImageBackground(true), // always draw the background
mDrawColorBackground(true),
// mNeverAnimate is initialised below, in constructor body
mPaginated(aType != eContext_Galley),
mCanPaginatedScroll(false),
mDoScaledTwips(true),
mIsRootPaginatedDocument(false),
mPendingThemeChanged(false),
mPendingThemeChangeKind(0),
mPendingUIResolutionChanged(false),
mPendingFontInfoUpdateReflowFromStyle(false),
mIsGlyph(false),
mCounterStylesDirty(true),
mFontFeatureValuesDirty(true),
mFontPaletteValuesDirty(true),
mIsVisual(false),
mInRDMPane(false),
mHasWarnedAboutTooLargeDashedOrDottedRadius(false),
mQuirkSheetAdded(false),
mHadNonBlankPaint(false),
mHadFirstContentfulPaint(false),
mHadNonTickContentfulPaint(false),
mHadContentfulPaintComposite(false),
mNeedsToUpdateHiddenByContentVisibilityForAnimations(false),
mUserInputEventsAllowed(false),
#ifdef DEBUG
mInitialized(false),
#endif
mOverriddenOrEmbedderColorScheme(dom::PrefersColorSchemeOverride::None) {
#ifdef DEBUG
PodZero(&mLayoutPhaseCount);
#endif
if (!IsDynamic()) {
mImageAnimationMode = imgIContainer::kDontAnimMode;
mNeverAnimate = true;
} else {
mImageAnimationMode = imgIContainer::kNormalAnimMode;
mNeverAnimate = false;
}
NS_ASSERTION(mDocument, "Null document");
// if text perf logging enabled, init stats struct
if (MOZ_LOG_TEST(gfxPlatform::GetLog(eGfxLog_textperf), LogLevel::Warning)) {
mTextPerf = MakeUnique<gfxTextPerfMetrics>();
}
if (StaticPrefs::gfx_missing_fonts_notify()) {
mMissingFonts = MakeUnique<gfxMissingFontRecorder>();
}
if (StaticPrefs::layout_dynamic_toolbar_max_height() > 0) {
// The pref for dynamic toolbar max height is only used in reftests so it's
// fine to set here.
mDynamicToolbarMaxHeight = StaticPrefs::layout_dynamic_toolbar_max_height();
}
UpdateFontVisibility();
}
static const char* gExactCallbackPrefs[] = {
"browser.active_color",
"browser.anchor_color",
"browser.visited_color",
"dom.meta-viewport.enabled",
"image.animation_mode",
"intl.accept_languages",
"layout.css.devPixelsPerPx",
"layout.css.dpi",
"layout.css.letter-spacing.model",
"layout.css.text-transform.uppercase-eszett.enabled",
"privacy.trackingprotection.enabled",
"ui.use_standins_for_native_colors",
nullptr,
};
static const char* gPrefixCallbackPrefs[] = {
"bidi.", "browser.display.", "browser.viewport.",
"font.", "gfx.font_rendering.", "layout.css.font-visibility.",
nullptr,
};
void nsPresContext::Destroy() {
if (mEventManager) {
// unclear if these are needed, but can't hurt
mEventManager->NotifyDestroyPresContext(this);
mEventManager->SetPresContext(nullptr);
mEventManager = nullptr;
}
if (mFontCache) {
mFontCache->Destroy();
mFontCache = nullptr;
}
// Unregister preference callbacks
Preferences::UnregisterPrefixCallbacks(nsPresContext::PreferenceChanged,
gPrefixCallbackPrefs, this);
Preferences::UnregisterCallbacks(nsPresContext::PreferenceChanged,
gExactCallbackPrefs, this);
mRefreshDriver = nullptr;
MOZ_ASSERT(mManagedPostRefreshObservers.IsEmpty());
}
nsPresContext::~nsPresContext() {
MOZ_ASSERT(!mPresShell, "Presshell forgot to clear our mPresShell pointer");
DetachPresShell();
Destroy();
}
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsPresContext)
NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsPresContext)
NS_IMPL_CYCLE_COLLECTING_RELEASE_WITH_LAST_RELEASE(nsPresContext, LastRelease())
void nsPresContext::LastRelease() {
if (mMissingFonts) {
mMissingFonts->Clear();
}
}
NS_IMPL_CYCLE_COLLECTION_CLASS(nsPresContext)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsPresContext)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mAnimationEventDispatcher);
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mDocument);
// NS_IMPL_CYCLE_COLLECTION_TRAVERSE_RAWPTR(mDeviceContext); // not xpcom
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mEffectCompositor);
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mEventManager);
// NS_IMPL_CYCLE_COLLECTION_TRAVERSE_RAWPTR(mLanguage); // an atom
// NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mTheme); // a service
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mPrintSettings);
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsPresContext)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mAnimationEventDispatcher);
NS_IMPL_CYCLE_COLLECTION_UNLINK(mDocument);
NS_IMPL_CYCLE_COLLECTION_UNLINK(mDeviceContext); // worth bothering?
NS_IMPL_CYCLE_COLLECTION_UNLINK(mEffectCompositor);
// NS_RELEASE(tmp->mLanguage); // an atom
// NS_IMPL_CYCLE_COLLECTION_UNLINK(mTheme); // a service
NS_IMPL_CYCLE_COLLECTION_UNLINK(mPrintSettings);
NS_IMPL_CYCLE_COLLECTION_UNLINK_WEAK_PTR
tmp->Destroy();
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
bool nsPresContext::IsChrome() const {
return Document()->IsInChromeDocShell();
}
void nsPresContext::GetUserPreferences() {
if (!GetPresShell()) {
// No presshell means nothing to do here. We'll do this when we
// get a presshell.
return;
}
PreferenceSheet::EnsureInitialized();
Document()->SetMayNeedFontPrefsUpdate();
// * image animation
nsAutoCString animatePref;
Preferences::GetCString("image.animation_mode", animatePref);
if (animatePref.EqualsLiteral("normal"))
mImageAnimationModePref = imgIContainer::kNormalAnimMode;
else if (animatePref.EqualsLiteral("none"))
mImageAnimationModePref = imgIContainer::kDontAnimMode;
else if (animatePref.EqualsLiteral("once"))
mImageAnimationModePref = imgIContainer::kLoopOnceAnimMode;
else // dynamic change to invalid value should act like it does initially
mImageAnimationModePref = imgIContainer::kNormalAnimMode;
uint32_t bidiOptions = GetBidi();
SET_BIDI_OPTION_DIRECTION(bidiOptions, StaticPrefs::bidi_direction());
SET_BIDI_OPTION_TEXTTYPE(bidiOptions, StaticPrefs::bidi_texttype());
SET_BIDI_OPTION_NUMERAL(bidiOptions, StaticPrefs::bidi_numeral());
// We don't need to force reflow: either we are initializing a new
// prescontext or we are being called from PreferenceChanged()
// which triggers a reflow anyway.
SetBidi(bidiOptions);
}
void nsPresContext::InvalidatePaintedLayers() {
if (!mPresShell) {
return;
}
if (nsIFrame* rootFrame = mPresShell->GetRootFrame()) {
// FrameLayerBuilder caches invalidation-related values that depend on the
// appunits-per-dev-pixel ratio, so ensure that all PaintedLayer drawing
// is completely flushed.
rootFrame->InvalidateFrameSubtree();
}
}
void nsPresContext::AppUnitsPerDevPixelChanged() {
int32_t oldAppUnitsPerDevPixel = mCurAppUnitsPerDevPixel;
InvalidatePaintedLayers();
FlushFontCache();
MediaFeatureValuesChanged(
{RestyleHint::RecascadeSubtree(), NS_STYLE_HINT_REFLOW,
MediaFeatureChangeReason::ResolutionChange},
MediaFeatureChangePropagation::JustThisDocument);
mCurAppUnitsPerDevPixel = mDeviceContext->AppUnitsPerDevPixel();
#ifdef ACCESSIBILITY
if (mCurAppUnitsPerDevPixel != oldAppUnitsPerDevPixel) {
if (nsAccessibilityService* accService = GetAccService()) {
accService->NotifyOfDevPixelRatioChange(mPresShell,
mCurAppUnitsPerDevPixel);
}
}
#endif
// Recompute the size for vh units since it's changed by the dynamic toolbar
// max height which is stored in screen coord.
if (IsRootContentDocumentCrossProcess()) {
AdjustSizeForViewportUnits();
}
// nsSubDocumentFrame uses a AppUnitsPerDevPixel difference between parent and
// child document to determine if it needs to build a nsDisplayZoom item. So
// if we that changes then we need to invalidate the subdoc frame so that
// item gets created/removed.
if (mPresShell) {
if (nsIFrame* frame = mPresShell->GetRootFrame()) {
frame = nsLayoutUtils::GetCrossDocParentFrameInProcess(frame);
if (frame) {
int32_t parentAPD = frame->PresContext()->AppUnitsPerDevPixel();
if ((parentAPD == oldAppUnitsPerDevPixel) !=
(parentAPD == mCurAppUnitsPerDevPixel)) {
frame->InvalidateFrame();
}
}
}
}
// We would also have to look at all of our child subdocuments but the
// InvalidatePaintedLayers call above calls InvalidateFrameSubtree which
// would invalidate all subdocument frames already.
}
// static
void nsPresContext::PreferenceChanged(const char* aPrefName, void* aSelf) {
static_cast<nsPresContext*>(aSelf)->PreferenceChanged(aPrefName);
}
void nsPresContext::PreferenceChanged(const char* aPrefName) {
if (!mPresShell) {
return;
}
nsDependentCString prefName(aPrefName);
if (prefName.EqualsLiteral("layout.css.dpi") ||
prefName.EqualsLiteral("layout.css.devPixelsPerPx")) {
int32_t oldAppUnitsPerDevPixel = mDeviceContext->AppUnitsPerDevPixel();
// We need to assume the DPI changes, since `mDeviceContext` is shared with
// other documents, and we'd need to save the return value of the first call
// for all of them.
Unused << mDeviceContext->CheckDPIChange();
OwningNonNull<mozilla::PresShell> presShell(*mPresShell);
// Re-fetch the view manager's window dimensions in case there's a
// deferred resize which hasn't affected our mVisibleArea yet
nscoord oldWidthAppUnits, oldHeightAppUnits;
RefPtr<nsViewManager> vm = presShell->GetViewManager();
if (!vm) {
return;
}
vm->GetWindowDimensions(&oldWidthAppUnits, &oldHeightAppUnits);
float oldWidthDevPixels = oldWidthAppUnits / oldAppUnitsPerDevPixel;
float oldHeightDevPixels = oldHeightAppUnits / oldAppUnitsPerDevPixel;
UIResolutionChangedInternal();
nscoord width = NSToCoordRound(oldWidthDevPixels * AppUnitsPerDevPixel());
nscoord height = NSToCoordRound(oldHeightDevPixels * AppUnitsPerDevPixel());
vm->SetWindowDimensions(width, height);
return;
}
if (StringBeginsWith(prefName, "browser.viewport."_ns) ||
StringBeginsWith(prefName, "font.size.inflation."_ns) ||
prefName.EqualsLiteral("dom.meta-viewport.enabled")) {
mPresShell->MaybeReflowForInflationScreenSizeChange();
}
auto changeHint = nsChangeHint{0};
auto restyleHint = RestyleHint{0};
// Changing any of these potentially changes the value of @media
// (prefers-contrast).
// The layout.css.prefers-contrast.enabled pref itself is not handled here,
// because that pref doesn't just affect the "live" value of the media query;
// it affects whether it is parsed at all.
if (prefName.EqualsLiteral("browser.display.document_color_use") ||
prefName.EqualsLiteral("browser.display.foreground_color") ||
prefName.EqualsLiteral("browser.display.background_color")) {
MediaFeatureValuesChanged({MediaFeatureChangeReason::PreferenceChange},
MediaFeatureChangePropagation::JustThisDocument);
}
if (prefName.EqualsLiteral(GFX_MISSING_FONTS_NOTIFY_PREF)) {
if (StaticPrefs::gfx_missing_fonts_notify()) {
if (!mMissingFonts) {
mMissingFonts = MakeUnique<gfxMissingFontRecorder>();
// trigger reflow to detect missing fonts on the current page
changeHint |= NS_STYLE_HINT_REFLOW;
}
} else {
if (mMissingFonts) {
mMissingFonts->Clear();
}
mMissingFonts = nullptr;
}
}
if (StringBeginsWith(prefName, "font."_ns) ||
// Changes to font family preferences don't change anything in the
// computed style data, so the style system won't generate a reflow hint
// for us. We need to do that manually.
prefName.EqualsLiteral("intl.accept_languages") ||
// Changes to bidi prefs need to trigger a reflow (see bug 443629)
StringBeginsWith(prefName, "bidi."_ns) ||
// Changes to font_rendering prefs need to trigger a reflow
StringBeginsWith(prefName, "gfx.font_rendering."_ns)) {
changeHint |= NS_STYLE_HINT_REFLOW;
if (StyleSet()->UsesFontMetrics()) {
restyleHint |= RestyleHint::RecascadeSubtree();
}
}
if (prefName.EqualsLiteral(
"layout.css.text-transform.uppercase-eszett.enabled") ||
prefName.EqualsLiteral("layout.css.letter-spacing.model")) {
changeHint |= NS_STYLE_HINT_REFLOW;
}
if (PreferenceSheet::AffectedByPref(prefName)) {
restyleHint |= RestyleHint::RestyleSubtree();
PreferenceSheet::Refresh();
}
// Same, this just frees a bunch of memory.
StaticPresData::Get()->InvalidateFontPrefs();
Document()->SetMayNeedFontPrefsUpdate();
// Initialize our state from the user preferences.
GetUserPreferences();
FlushFontCache();
if (UpdateFontVisibility()) {
changeHint |= NS_STYLE_HINT_REFLOW;
}
// Preferences require rerunning selector matching because we rebuild
// the pref style sheet for some preference changes.
if (changeHint || restyleHint) {
RebuildAllStyleData(changeHint, restyleHint);
}
InvalidatePaintedLayers();
}
nsresult nsPresContext::Init(nsDeviceContext* aDeviceContext) {
NS_ASSERTION(!mInitialized, "attempt to reinit pres context");
NS_ENSURE_ARG(aDeviceContext);
mDeviceContext = aDeviceContext;
// In certain rare cases (such as changing page mode), we tear down layout
// state and re-initialize a new prescontext for a document. Given that we
// hang style state off the DOM, we detect that re-initialization case and
// lazily drop the servo data. We don't do this eagerly during layout teardown
// because that would incur an extra whole-tree traversal that's unnecessary
// most of the time.
//
// FIXME(emilio): I'm pretty sure this doesn't happen after bug 1414999.
Element* root = mDocument->GetRootElement();
if (root && root->HasServoData()) {
RestyleManager::ClearServoDataFromSubtree(root);
}
if (mDeviceContext->SetFullZoom(mFullZoom)) {
FlushFontCache();
}
mCurAppUnitsPerDevPixel = mDeviceContext->AppUnitsPerDevPixel();
mEventManager = new mozilla::EventStateManager();
mAnimationEventDispatcher = new mozilla::AnimationEventDispatcher(this);
mEffectCompositor = new mozilla::EffectCompositor(this);
mTransitionManager = MakeUnique<nsTransitionManager>(this);
mAnimationManager = MakeUnique<nsAnimationManager>(this);
mTimelineManager = MakeUnique<mozilla::TimelineManager>(this);
if (mDocument->GetDisplayDocument()) {
NS_ASSERTION(mDocument->GetDisplayDocument()->GetPresContext(),
"Why are we being initialized?");
mRefreshDriver =
mDocument->GetDisplayDocument()->GetPresContext()->RefreshDriver();
} else {
dom::Document* parent = mDocument->GetInProcessParentDocument();
// Unfortunately, sometimes |parent| here has no presshell because
// printing screws up things. Assert that in other cases it does,
// but whenever the shell is null just fall back on using our own
// refresh driver.
NS_ASSERTION(
!parent || mDocument->IsStaticDocument() || parent->GetPresShell(),
"How did we end up with a presshell if our parent doesn't "
"have one?");
if (parent && parent->GetPresContext()) {
// XXX the document can change in AttachPresShell, does this work?
dom::BrowsingContext* browsingContext = mDocument->GetBrowsingContext();
if (browsingContext && !browsingContext->IsTop()) {
Element* containingElement = mDocument->GetEmbedderElement();
if (!containingElement->IsXULElement() ||
!containingElement->HasAttr(nsGkAtoms::forceOwnRefreshDriver)) {
mRefreshDriver = parent->GetPresContext()->RefreshDriver();
}
}
}
if (!mRefreshDriver) {
mRefreshDriver = new nsRefreshDriver(this);
}
}
// Register callbacks so we're notified when the preferences change
Preferences::RegisterPrefixCallbacks(nsPresContext::PreferenceChanged,
gPrefixCallbackPrefs, this);
Preferences::RegisterCallbacks(nsPresContext::PreferenceChanged,
gExactCallbackPrefs, this);
nsresult rv = mEventManager->Init();
NS_ENSURE_SUCCESS(rv, rv);
mEventManager->SetPresContext(this);
#if defined(MOZ_WIDGET_ANDROID)
if (IsRootContentDocumentCrossProcess() &&
MOZ_LIKELY(
!Preferences::HasUserValue("layout.dynamic-toolbar-max-height"))) {
if (BrowserChild* browserChild =
BrowserChild::GetFrom(mDocument->GetDocShell())) {
mDynamicToolbarMaxHeight = browserChild->GetDynamicToolbarMaxHeight();
mDynamicToolbarHeight = mDynamicToolbarMaxHeight;
}
}
#endif
#ifdef DEBUG
mInitialized = true;
#endif
return NS_OK;
}
bool nsPresContext::UpdateFontVisibility() {
FontVisibility oldValue = mFontVisibility;
/*
* Expected behavior in order of precedence:
* 1 Chrome Rules give User Level (3)
* 2 RFP gives Highest Level (1 aka Base)
* 3 An RFPTarget of Base gives Base Level (1)
* 4 An RFPTarget of LangPack gives LangPack Level (2)
* 5 The value of the Standard Font Visibility Pref
*
* If the ETP toggle is disabled (aka
* ContentBlockingAllowList::Check is true), it will only override 3-5,
* not rules 1 or 2.
*/
// Rule 1: Allow all font access for privileged contexts, including
// chrome and devtools contexts.
if (Document()->ChromeRulesEnabled()) {
mFontVisibility = FontVisibility::User;
return mFontVisibility != oldValue;
}
// Is this a private browsing context?
bool isPrivate = false;
if (nsCOMPtr<nsILoadContext> loadContext = mDocument->GetLoadContext()) {
isPrivate = loadContext->UsePrivateBrowsing();
}
int32_t level;
// Rule 3
if (mDocument->ShouldResistFingerprinting(
RFPTarget::FontVisibilityBaseSystem)) {
// Rule 2: Check RFP pref
// This is inside Rule 3 in case this document is exempted from RFP.
// But if it is not exempted, and RFP is enabled, we return immediately
// to prevent the override below from occurring.
if (nsRFPService::IsRFPPrefEnabled(isPrivate)) {
mFontVisibility = FontVisibility::Base;
return mFontVisibility != oldValue;
}
level = int32_t(FontVisibility::Base);
}
// Rule 4
else if (mDocument->ShouldResistFingerprinting(
RFPTarget::FontVisibilityLangPack)) {
level = int32_t(FontVisibility::LangPack);
}
// Rule 5
else {
level = StaticPrefs::layout_css_font_visibility();
}
// Override Rules 3-5 Only: Determine if the user has exempted the
// domain from tracking protections, if so, use the default value.
if (level != StaticPrefs::layout_css_font_visibility() &&
ContentBlockingAllowList::Check(mDocument->CookieJarSettings())) {
level = StaticPrefs::layout_css_font_visibility();
}
// Clamp result to the valid range of levels.
level = std::max(std::min(level, int32_t(FontVisibility::User)),
int32_t(FontVisibility::Base));
mFontVisibility = FontVisibility(level);
return mFontVisibility != oldValue;
}
void nsPresContext::ReportBlockedFontFamilyName(const nsCString& aFamily,
FontVisibility aVisibility) {
if (!mBlockedFonts.EnsureInserted(aFamily)) {
return;
}
nsAutoString msg;
msg.AppendPrintf(
"Request for font \"%s\" blocked at visibility level %d (requires %d)\n",
aFamily.get(), int(GetFontVisibility()), int(aVisibility));
nsContentUtils::ReportToConsoleNonLocalized(msg, nsIScriptError::warningFlag,
"Security"_ns, mDocument);
}
void nsPresContext::ReportBlockedFontFamily(const fontlist::Family& aFamily) {
auto* fontList = gfxPlatformFontList::PlatformFontList()->SharedFontList();
const nsCString& name = aFamily.DisplayName().AsString(fontList);
ReportBlockedFontFamilyName(name, aFamily.Visibility());
}
void nsPresContext::ReportBlockedFontFamily(const gfxFontFamily& aFamily) {
ReportBlockedFontFamilyName(aFamily.Name(), aFamily.Visibility());
}
void nsPresContext::InitFontCache() {
if (!mFontCache) {
mFontCache = new nsFontCache();
mFontCache->Init(this);
}
}
void nsPresContext::UpdateFontCacheUserFonts(gfxUserFontSet* aUserFontSet) {
if (mFontCache) {
mFontCache->UpdateUserFonts(aUserFontSet);
}
}
already_AddRefed<nsFontMetrics> nsPresContext::GetMetricsFor(
const nsFont& aFont, const nsFontMetrics::Params& aParams) {
InitFontCache();
return mFontCache->GetMetricsFor(aFont, aParams);
}
nsresult nsPresContext::FlushFontCache() {
if (mFontCache) {
mFontCache->Flush();
}
return NS_OK;
}
nsresult nsPresContext::FontMetricsDeleted(const nsFontMetrics* aFontMetrics) {
if (mFontCache) {
mFontCache->FontMetricsDeleted(aFontMetrics);
}
return NS_OK;
}
// Note: We don't hold a reference on the shell; it has a reference to
// us
void nsPresContext::AttachPresShell(mozilla::PresShell* aPresShell) {
MOZ_ASSERT(!mPresShell);
mPresShell = aPresShell;
mRestyleManager = MakeUnique<mozilla::RestyleManager>(this);
// Since CounterStyleManager is also the name of a method of
// nsPresContext, it is necessary to prefix the class with the mozilla
// namespace here.
mCounterStyleManager = new mozilla::CounterStyleManager(this);
dom::Document* doc = mPresShell->GetDocument();
MOZ_ASSERT(doc);
// Have to update PresContext's mDocument before calling any other methods.
mDocument = doc;
LookAndFeel::HandleGlobalThemeChange();
// Initialize our state from the user preferences, now that we
// have a presshell, and hence a document.
GetUserPreferences();
EnsureTheme();
nsIURI* docURI = doc->GetDocumentURI();
if (IsDynamic() && docURI) {
if (!docURI->SchemeIs("chrome") && !docURI->SchemeIs("resource"))
mImageAnimationMode = mImageAnimationModePref;
else
mImageAnimationMode = imgIContainer::kNormalAnimMode;
}
UpdateCharSet(doc->GetDocumentCharacterSet());
}
Maybe<ColorScheme> nsPresContext::GetOverriddenOrEmbedderColorScheme() const {
if (Medium() == nsGkAtoms::print) {
return Some(ColorScheme::Light);
}
switch (mOverriddenOrEmbedderColorScheme) {
case dom::PrefersColorSchemeOverride::Dark:
return Some(ColorScheme::Dark);
case dom::PrefersColorSchemeOverride::Light:
return Some(ColorScheme::Light);
case dom::PrefersColorSchemeOverride::None:
break;
}
return Nothing();
}
void nsPresContext::SetColorSchemeOverride(
PrefersColorSchemeOverride aOverride) {
auto oldScheme = mDocument->PreferredColorScheme();
mOverriddenOrEmbedderColorScheme = aOverride;
if (mDocument->PreferredColorScheme() != oldScheme) {
MediaFeatureValuesChanged(
MediaFeatureChange::ForPreferredColorSchemeChange(),
MediaFeatureChangePropagation::JustThisDocument);
}
}
void nsPresContext::RecomputeBrowsingContextDependentData() {
MOZ_ASSERT(mDocument);
dom::Document* doc = mDocument;
// Resource documents inherit all this state from their display document.
while (dom::Document* outer = doc->GetDisplayDocument()) {
doc = outer;
}
auto* browsingContext = doc->GetBrowsingContext();
if (!browsingContext) {
// This can legitimately happen for e.g. SVG images. Those just get scaled
// as a result of the zoom on the embedder document so it doesn't really
// matter... Medium also doesn't affect those.
return;
}
if (!IsPrintingOrPrintPreview()) {
auto systemZoom = LookAndFeel::SystemZoomSettings();
SetFullZoom(browsingContext->FullZoom() * systemZoom.mFullZoom);
SetTextZoom(browsingContext->TextZoom() * systemZoom.mTextZoom);
SetOverrideDPPX(browsingContext->OverrideDPPX());
}
auto* top = browsingContext->Top();
SetColorSchemeOverride([&] {
auto overriden = top->PrefersColorSchemeOverride();
if (overriden != PrefersColorSchemeOverride::None) {
return overriden;
}
if (!StaticPrefs::
layout_css_iframe_embedder_prefers_color_scheme_content_enabled()) {
return top->GetEmbedderColorSchemes().mPreferred;
}
return browsingContext->GetEmbedderColorSchemes().mPreferred;
}());
SetInRDMPane(top->GetInRDMPane());
if (doc == mDocument) {
// Medium doesn't apply to resource documents, etc.
RefPtr<nsAtom> mediumToEmulate;
if (MOZ_UNLIKELY(!top->GetMediumOverride().IsEmpty())) {
nsAutoString lower;
nsContentUtils::ASCIIToLower(top->GetMediumOverride(), lower);
mediumToEmulate = NS_Atomize(lower);
}
EmulateMedium(mediumToEmulate);
}
mDocument->EnumerateExternalResources([](dom::Document& aSubResource) {
if (nsPresContext* subResourcePc = aSubResource.GetPresContext()) {
subResourcePc->RecomputeBrowsingContextDependentData();
}
return CallState::Continue;
});
}
void nsPresContext::DetachPresShell() {
// The counter style manager's destructor needs to deallocate with the
// presshell arena. Disconnect it before nulling out the shell.
//
// XXXbholley: Given recent refactorings, it probably makes more sense to
// just null our mPresShell at the bottom of this function. I'm leaving it
// this way to preserve the old ordering, but I doubt anything would break.
if (mCounterStyleManager) {
mCounterStyleManager->Disconnect();
mCounterStyleManager = nullptr;
}
mPresShell = nullptr;
CancelManagedPostRefreshObservers();
if (mAnimationEventDispatcher) {
mAnimationEventDispatcher->Disconnect();
mAnimationEventDispatcher = nullptr;
}
if (mEffectCompositor) {
mEffectCompositor->Disconnect();
mEffectCompositor = nullptr;
}
if (mTransitionManager) {
mTransitionManager->Disconnect();
mTransitionManager = nullptr;
}
if (mAnimationManager) {
mAnimationManager->Disconnect();
mAnimationManager = nullptr;
}
if (mTimelineManager) {
mTimelineManager->Disconnect();
mTimelineManager = nullptr;
}
if (mRestyleManager) {
mRestyleManager->Disconnect();
mRestyleManager = nullptr;
}
if (mRefreshDriver && mRefreshDriver->GetPresContext() == this) {
mRefreshDriver->Disconnect();
// Can't null out the refresh driver here.
}
}
struct QueryContainerState {
nsSize mSize;
WritingMode mWm;
StyleContainerType mType;
nscoord GetInlineSize() const { return LogicalSize(mWm, mSize).ISize(mWm); }
bool Changed(const QueryContainerState& aNewState) {
if (mType != aNewState.mType) {
return true;
}
switch (mType) {
case StyleContainerType::Normal:
break;
case StyleContainerType::Size:
return mSize != aNewState.mSize;
case StyleContainerType::InlineSize:
return GetInlineSize() != aNewState.GetInlineSize();
}
return false;
}
};
NS_DECLARE_FRAME_PROPERTY_DELETABLE(ContainerState, QueryContainerState);
void nsPresContext::RegisterContainerQueryFrame(nsIFrame* aFrame) {
mContainerQueryFrames.Add(aFrame);
}
void nsPresContext::UnregisterContainerQueryFrame(nsIFrame* aFrame) {
mContainerQueryFrames.Remove(aFrame);
}
void nsPresContext::FinishedContainerQueryUpdate() {
mUpdatedContainerQueryContents.Clear();
}
bool nsPresContext::UpdateContainerQueryStyles() {
if (mContainerQueryFrames.IsEmpty()) {
return false;
}
AUTO_PROFILER_LABEL_RELEVANT_FOR_JS("Container Query Styles Update", LAYOUT);
AUTO_PROFILER_MARKER_TEXT("UpdateContainerQueryStyles", LAYOUT, {}, ""_ns);
PresShell()->DoFlushLayout(/* aInterruptible = */ false);
AutoTArray<nsIFrame*, 8> framesToUpdate;
bool anyChanged = false;
for (nsIFrame* frame : mContainerQueryFrames.IterFromShallowest()) {
MOZ_ASSERT(frame->IsPrimaryFrame());
auto type = frame->StyleDisplay()->mContainerType;
MOZ_ASSERT(type != StyleContainerType::Normal,
"Non-container frames shouldn't be in this set");
const QueryContainerState newState{frame->GetSize(),
frame->GetWritingMode(), type};
QueryContainerState* oldState = frame->GetProperty(ContainerState());
const bool changed = !oldState || oldState->Changed(newState);
// Make sure to update the state regardless. It's cheap and it keeps tracks
// of both axes correctly even if only one axis is contained.
if (oldState) {
*oldState = newState;
} else {
frame->SetProperty(ContainerState(), new QueryContainerState(newState));
}
if (!changed) {
continue;
}
const bool updatingAncestor = [&] {
for (nsIFrame* f : framesToUpdate) {
if (nsLayoutUtils::IsProperAncestorFrame(f, frame)) {
return true;
}
}
return false;
}();
if (updatingAncestor) {
// We're going to update an ancestor container of this frame already,
// avoid updating this one too until all our ancestor containers are
// updated.
continue;
}
// To prevent unstable layout, only update once per-element per-flush.
if (NS_WARN_IF(!mUpdatedContainerQueryContents.EnsureInserted(
frame->GetContent()))) {
continue;
}
framesToUpdate.AppendElement(frame);
// TODO(emilio): More fine-grained invalidation rather than invalidating the
// whole subtree, probably!
RestyleManager()->PostRestyleEvent(frame->GetContent()->AsElement(),
RestyleHint::RestyleSubtree(),
nsChangeHint(0));
anyChanged = true;
}
return anyChanged;
}
void nsPresContext::DocumentCharSetChanged(NotNull<const Encoding*> aCharSet) {
UpdateCharSet(aCharSet);
FlushFontCache();
// If a document contains one or more <script> elements, frame construction
// might happen earlier than the UpdateCharSet(), so we need to restyle
// descendants to make their style data up-to-date.
//
// FIXME(emilio): Revisit whether this is true after bug 1438911.
RebuildAllStyleData(NS_STYLE_HINT_REFLOW, RestyleHint::RecascadeSubtree());
}
void nsPresContext::UpdateCharSet(NotNull<const Encoding*> aCharSet) {
switch (GET_BIDI_OPTION_TEXTTYPE(GetBidi())) {
case IBMBIDI_TEXTTYPE_LOGICAL:
SetVisualMode(false);
break;
case IBMBIDI_TEXTTYPE_VISUAL:
SetVisualMode(true);
break;
case IBMBIDI_TEXTTYPE_CHARSET:
default:
SetVisualMode(IsVisualCharset(aCharSet));
}
}
nsPresContext* nsPresContext::GetParentPresContext() const {
mozilla::PresShell* presShell = GetPresShell();
if (presShell) {
nsViewManager* viewManager = presShell->GetViewManager();
if (viewManager) {
nsView* view = viewManager->GetRootView();
if (view) {
view = view->GetParent(); // anonymous inner view
if (view) {
view = view->GetParent(); // subdocumentframe's view
if (view) {
nsIFrame* f = view->GetFrame();
if (f) {
return f->PresContext();
}
}
}
}
}
}
return nullptr;
}
nsPresContext* nsPresContext::GetInProcessRootContentDocumentPresContext() {
if (IsChrome()) return nullptr;
nsPresContext* pc = this;
for (;;) {
nsPresContext* parent = pc->GetParentPresContext();
if (!parent || parent->IsChrome()) return pc;
pc = parent;
}
}
nsIWidget* nsPresContext::GetNearestWidget(nsPoint* aOffset) {
NS_ENSURE_TRUE(mPresShell, nullptr);
nsViewManager* vm = mPresShell->GetViewManager();
NS_ENSURE_TRUE(vm, nullptr);
nsView* rootView = vm->GetRootView();
NS_ENSURE_TRUE(rootView, nullptr);
return rootView->GetNearestWidget(aOffset);
}
nsIWidget* nsPresContext::GetRootWidget() const {
NS_ENSURE_TRUE(mPresShell, nullptr);
nsViewManager* vm = mPresShell->GetViewManager();
if (!vm) {
return nullptr;
}
return vm->GetRootWidget();
}
// We may want to replace this with something faster, maybe caching the root
// prescontext
nsRootPresContext* nsPresContext::GetRootPresContext() const {
nsPresContext* pc = const_cast<nsPresContext*>(this);
for (;;) {
nsPresContext* parent = pc->GetParentPresContext();
if (!parent) break;
pc = parent;
}
return pc->IsRoot() ? static_cast<nsRootPresContext*>(pc) : nullptr;
}
bool nsPresContext::UserInputEventsAllowed() {
MOZ_ASSERT(IsRoot());
if (mUserInputEventsAllowed) {
return true;
}
// Special document
if (Document()->IsEverInitialDocument()) {
return true;
}
if (mRefreshDriver->IsThrottled()) {
MOZ_ASSERT(!mPresShell->IsVisible());
// This implies that the BC is not visibile and users can't
// interact with it, so we are okay with handling user inputs here.
return true;
}
if (mMeasuredTicksSinceLoading <
StaticPrefs::dom_input_events_security_minNumTicks()) {
return false;
}
if (!StaticPrefs::dom_input_events_security_minTimeElapsedInMS()) {
return true;
}
dom::Document* doc = Document();
MOZ_ASSERT_IF(StaticPrefs::dom_input_events_security_minNumTicks(),
doc->GetReadyStateEnum() >= Document::READYSTATE_LOADING);
TimeStamp loadingOrRestoredFromBFCacheTime =
doc->GetLoadingOrRestoredFromBFCacheTimeStamp();
MOZ_ASSERT(!loadingOrRestoredFromBFCacheTime.IsNull());
TimeDuration elapsed = TimeStamp::Now() - loadingOrRestoredFromBFCacheTime;
if (elapsed.ToMilliseconds() >=
StaticPrefs::dom_input_events_security_minTimeElapsedInMS()) {
mUserInputEventsAllowed = true;
return true;
}
return false;
}
void nsPresContext::MaybeIncreaseMeasuredTicksSinceLoading() {
MOZ_ASSERT(IsRoot());
if (mMeasuredTicksSinceLoading >=
StaticPrefs::dom_input_events_security_minNumTicks()) {
return;
}
// We consider READYSTATE_LOADING is the point when the page
// becomes interactive
if (Document()->GetReadyStateEnum() >= Document::READYSTATE_LOADING ||
Document()->IsInitialDocument()) {
++mMeasuredTicksSinceLoading;
}
if (mMeasuredTicksSinceLoading <
StaticPrefs::dom_input_events_security_minNumTicks()) {
// Here we are forcing refresh driver to run because we can't always
// guarantee refresh driver will run enough times to meet the minNumTicks
// requirement. i.e. about:blank.
if (!RefreshDriver()->HasPendingTick()) {
RefreshDriver()->InitializeTimer();
}
}
}
bool nsPresContext::NeedsMoreTicksForUserInput() const {
MOZ_ASSERT(IsRoot());
return mMeasuredTicksSinceLoading <
StaticPrefs::dom_input_events_security_minNumTicks();
}
// Helper function for setting Anim Mode on image
static void SetImgAnimModeOnImgReq(imgIRequest* aImgReq, uint16_t aMode) {
if (aImgReq) {
nsCOMPtr<imgIContainer> imgCon;
aImgReq->GetImage(getter_AddRefs(imgCon));
if (imgCon) {
imgCon->SetAnimationMode(aMode);
}
}
}
// IMPORTANT: Assumption is that all images for a Presentation
// have the same Animation Mode (pavlov said this was OK)
//
// Walks content and set the animation mode
// this is a way to turn on/off image animations
void nsPresContext::SetImgAnimations(nsIContent* aParent, uint16_t aMode) {
nsCOMPtr<nsIImageLoadingContent> imgContent(do_QueryInterface(aParent));
if (imgContent) {
nsCOMPtr<imgIRequest> imgReq;
imgContent->GetRequest(nsIImageLoadingContent::CURRENT_REQUEST,
getter_AddRefs(imgReq));
SetImgAnimModeOnImgReq(imgReq, aMode);
}
for (nsIContent* childContent = aParent->GetFirstChild(); childContent;
childContent = childContent->GetNextSibling()) {
SetImgAnimations(childContent, aMode);
}
}
void nsPresContext::SetSMILAnimations(dom::Document* aDoc, uint16_t aNewMode,
uint16_t aOldMode) {
if (aDoc->HasAnimationController()) {
SMILAnimationController* controller = aDoc->GetAnimationController();
switch (aNewMode) {
case imgIContainer::kNormalAnimMode:
case imgIContainer::kLoopOnceAnimMode:
if (aOldMode == imgIContainer::kDontAnimMode)
controller->Resume(SMILTimeContainer::PAUSE_USERPREF);
break;
case imgIContainer::kDontAnimMode:
if (aOldMode != imgIContainer::kDontAnimMode)
controller->Pause(SMILTimeContainer::PAUSE_USERPREF);
break;
}
}
}
void nsPresContext::SetImageAnimationMode(uint16_t aMode) {
NS_ASSERTION(aMode == imgIContainer::kNormalAnimMode ||
aMode == imgIContainer::kDontAnimMode ||
aMode == imgIContainer::kLoopOnceAnimMode,
"Wrong Animation Mode is being set!");
// Image animation mode cannot be changed when rendering to a printer.
if (!IsDynamic()) return;
// Now walk the content tree and set the animation mode
// on all the images.
if (mPresShell) {
dom::Document* doc = mPresShell->GetDocument();
if (doc) {
doc->StyleImageLoader()->SetAnimationMode(aMode);
Element* rootElement = doc->GetRootElement();
if (rootElement) {
SetImgAnimations(rootElement, aMode);
}
SetSMILAnimations(doc, aMode, mImageAnimationMode);
}
}
mImageAnimationMode = aMode;
}
void nsPresContext::SetTextZoom(float aTextZoom) {
float newZoom = aTextZoom;
float minZoom = StaticPrefs::zoom_minPercent() / 100.0f;
float maxZoom = StaticPrefs::zoom_maxPercent() / 100.0f;
if (newZoom < minZoom) {
newZoom = minZoom;
} else if (newZoom > maxZoom) {
newZoom = maxZoom;
}
if (newZoom == mTextZoom) {
return;
}
mTextZoom = newZoom;
// Media queries could have changed, since we changed the meaning
// of 'em' units in them.
MediaFeatureValuesChanged(
{RestyleHint::RecascadeSubtree(), NS_STYLE_HINT_REFLOW,
MediaFeatureChangeReason::ZoomChange},
MediaFeatureChangePropagation::JustThisDocument);
}
void nsPresContext::SetInRDMPane(bool aInRDMPane) {
if (mInRDMPane == aInRDMPane) {
return;
}
mInRDMPane = aInRDMPane;
RecomputeTheme();
if (mPresShell) {
nsContentUtils::AddScriptRunner(NewRunnableMethod<bool>(
"PresShell::MaybeRecreateMobileViewportManager", mPresShell,
&PresShell::MaybeRecreateMobileViewportManager, true));
}
}
float nsPresContext::GetDeviceFullZoom() {
return mDeviceContext->GetFullZoom();
}
void nsPresContext::SetFullZoom(float aZoom) {
if (!mPresShell || mFullZoom == aZoom) {
return;
}
// Re-fetch the view manager's window dimensions in case there's a deferred
// resize which hasn't affected our mVisibleArea yet
nscoord oldWidthAppUnits, oldHeightAppUnits;
mPresShell->GetViewManager()->GetWindowDimensions(&oldWidthAppUnits,
&oldHeightAppUnits);
float oldWidthDevPixels = oldWidthAppUnits / float(mCurAppUnitsPerDevPixel);
float oldHeightDevPixels = oldHeightAppUnits / float(mCurAppUnitsPerDevPixel);
mDeviceContext->SetFullZoom(aZoom);
mFullZoom = aZoom;
AppUnitsPerDevPixelChanged();
mPresShell->GetViewManager()->SetWindowDimensions(
NSToCoordRound(oldWidthDevPixels * AppUnitsPerDevPixel()),
NSToCoordRound(oldHeightDevPixels * AppUnitsPerDevPixel()));
}
void nsPresContext::SetOverrideDPPX(float aDPPX) {
// SetOverrideDPPX is called during navigations, including history
// traversals. In that case, it's typically called with our current value,
// and we don't need to actually do anything.
if (aDPPX == GetOverrideDPPX()) {
return;
}
mMediaEmulationData.mDPPX = aDPPX;
MediaFeatureValuesChanged({MediaFeatureChangeReason::ResolutionChange},
MediaFeatureChangePropagation::JustThisDocument);
}
gfxSize nsPresContext::ScreenSizeInchesForFontInflation(bool* aChanged) {
if (aChanged) {
*aChanged = false;
}
nsDeviceContext* dx = DeviceContext();
nsRect clientRect;
dx->GetClientRect(clientRect); // FIXME: GetClientRect looks expensive
float unitsPerInch = dx->AppUnitsPerPhysicalInch();
gfxSize deviceSizeInches(float(clientRect.width) / unitsPerInch,
float(clientRect.height) / unitsPerInch);
if (mLastFontInflationScreenSize == gfxSize(-1.0, -1.0)) {
mLastFontInflationScreenSize = deviceSizeInches;
}
if (deviceSizeInches != mLastFontInflationScreenSize && aChanged) {
*aChanged = true;
mLastFontInflationScreenSize = deviceSizeInches;
}
return deviceSizeInches;
}
static bool CheckOverflow(const ComputedStyle* aComputedStyle,
ScrollStyles* aStyles) {
// If they're not styled yet, we'll get around to it when constructing frames
// for the element.
if (!aComputedStyle) {
return false;
}
const nsStyleDisplay* display = aComputedStyle->StyleDisplay();
// If they will generate no box, just don't.
if (display->mDisplay == StyleDisplay::None ||
display->mDisplay == StyleDisplay::Contents) {
return false;
}
// NOTE(emilio): This check needs to match the one in
// Document::IsPotentiallyScrollable.
if (display->OverflowIsVisibleInBothAxis()) {
return false;
}
*aStyles =
ScrollStyles(*display, ScrollStyles::MapOverflowToValidScrollStyle);
return true;
}
// https://drafts.csswg.org/css-overflow/#overflow-propagation
//
// NOTE(emilio): We may need to use out-of-date styles for this, since this is
// called from nsCSSFrameConstructor::ContentRemoved. We could refactor this a
// bit to avoid doing that, and also fix correctness issues (we don't invalidate
// properly when we insert a body element and there is a previous one, for
// example).
static Element* GetPropagatedScrollStylesForViewport(
nsPresContext* aPresContext, ScrollStyles* aStyles) {
Document* document = aPresContext->Document();
Element* docElement = document->GetRootElement();
// docElement might be null if we're doing this after removing it.
if (!docElement) {
return nullptr;
}
// Check the style on the document root element
const auto* rootStyle = Servo_Element_GetMaybeOutOfDateStyle(docElement);
if (CheckOverflow(rootStyle, aStyles)) {
// tell caller we stole the overflow style from the root element
return docElement;
}
if (rootStyle && rootStyle->StyleDisplay()->IsContainAny()) {
return nullptr;
}
// Don't look in the BODY for non-HTML documents or HTML documents
// with non-HTML roots.
// XXX this should be earlier; we shouldn't even look at the document root
// for non-HTML documents. Fix this once we support explicit CSS styling
// of the viewport
if (!document->IsHTMLOrXHTML() || !docElement->IsHTMLElement()) {
return nullptr;
}
Element* bodyElement = document->AsHTMLDocument()->GetBodyElement();
if (!bodyElement) {
return nullptr;
}
MOZ_ASSERT(bodyElement->IsHTMLElement(nsGkAtoms::body),
"GetBodyElement returned something bogus");
const auto* bodyStyle = Servo_Element_GetMaybeOutOfDateStyle(bodyElement);
if (bodyStyle && bodyStyle->StyleDisplay()->IsContainAny()) {
return nullptr;
}
if (CheckOverflow(bodyStyle, aStyles)) {
// tell caller we stole the overflow style from the body element
return bodyElement;
}
return nullptr;
}
Element* nsPresContext::UpdateViewportScrollStylesOverride() {
ScrollStyles oldViewportScrollStyles = mViewportScrollStyles;
// Start off with our default styles, and then update them as needed.
mViewportScrollStyles =
ScrollStyles(StyleOverflow::Auto, StyleOverflow::Auto);
mViewportScrollOverrideElement = nullptr;
// Don't propagate the scrollbar state in printing or print preview.
if (!IsPaginated()) {
mViewportScrollOverrideElement =
GetPropagatedScrollStylesForViewport(this, &mViewportScrollStyles);
}
dom::Document* document = Document();
if (Element* fsElement = document->GetUnretargetedFullscreenElement()) {
// If the document is in fullscreen, but the fullscreen element is
// not the root element, we should explicitly suppress the scrollbar
// here. Note that, we still need to return the original element
// the styles are from, so that the state of those elements is not
// affected across fullscreen change.
if (fsElement != document->GetRootElement() &&
fsElement != mViewportScrollOverrideElement) {
mViewportScrollStyles =
ScrollStyles(StyleOverflow::Hidden, StyleOverflow::Hidden);
}
}
if (mViewportScrollStyles != oldViewportScrollStyles) {
if (mPresShell) {
if (nsIFrame* frame = mPresShell->GetRootFrame()) {
frame->SchedulePaint();
}
}
}
return mViewportScrollOverrideElement;
}
bool nsPresContext::ElementWouldPropagateScrollStyles(const Element& aElement) {
if (aElement.GetParent() && !aElement.IsHTMLElement(nsGkAtoms::body)) {
// We certainly won't be propagating from this element.
return false;
}
// Go ahead and just call GetPropagatedScrollStylesForViewport, but update
// a dummy ScrollStyles we don't care about. It'll do a bit of extra work,
// but saves us having to have more complicated code or more code duplication;
// in practice we will make this call quite rarely, because we checked for all
// the common cases above.
ScrollStyles dummy(StyleOverflow::Auto, StyleOverflow::Auto);
return GetPropagatedScrollStylesForViewport(this, &dummy) == &aElement;
}
nsISupports* nsPresContext::GetContainerWeak() const {
return mDocument->GetDocShell();
}
ColorScheme nsPresContext::DefaultBackgroundColorScheme() const {
dom::Document* doc = Document();
// Use a dark background for top-level about:blank that is inaccessible to
// content JS.
if (doc->IsLikelyContentInaccessibleTopLevelAboutBlank()) {
return doc->PreferredColorScheme(Document::IgnoreRFP::Yes);
}
// Prefer the root color-scheme (since generally the default canvas
// background comes from the root element's background-color), and fall back
// to the default color-scheme if not available.
if (auto* frame = FrameConstructor()->GetRootElementStyleFrame()) {
return LookAndFeel::ColorSchemeForFrame(frame);
}
return doc->DefaultColorScheme();
}
nscolor nsPresContext::DefaultBackgroundColor() const {
if (!GetBackgroundColorDraw()) {
return NS_RGB(255, 255, 255);
}
return PrefSheetPrefs()
.ColorsFor(DefaultBackgroundColorScheme())
.mDefaultBackground;
}
nsDocShell* nsPresContext::GetDocShell() const {
return nsDocShell::Cast(mDocument->GetDocShell());
}
bool nsPresContext::BidiEnabled() const { return Document()->GetBidiEnabled(); }
void nsPresContext::SetBidiEnabled() const { Document()->SetBidiEnabled(); }
void nsPresContext::SetBidi(uint32_t aSource) {
// Don't do all this stuff unless the options have changed.
if (aSource == GetBidi()) {
return;
}
Document()->SetBidiOptions(aSource);
if (IBMBIDI_TEXTDIRECTION_RTL == GET_BIDI_OPTION_DIRECTION(aSource) ||
IBMBIDI_NUMERAL_HINDI == GET_BIDI_OPTION_NUMERAL(aSource)) {
SetBidiEnabled();
}
if (IBMBIDI_TEXTTYPE_VISUAL == GET_BIDI_OPTION_TEXTTYPE(aSource)) {
SetVisualMode(true);
} else if (IBMBIDI_TEXTTYPE_LOGICAL == GET_BIDI_OPTION_TEXTTYPE(aSource)) {
SetVisualMode(false);
} else {
SetVisualMode(IsVisualCharset(Document()->GetDocumentCharacterSet()));
}
}
uint32_t nsPresContext::GetBidi() const { return Document()->GetBidiOptions(); }
void nsPresContext::RecordInteractionTime(InteractionType aType,
const TimeStamp& aTimeStamp) {
if (!mInteractionTimeEnabled || aTimeStamp.IsNull()) {
return;
}
// Array of references to the member variable of each time stamp
// for the different interaction types, keyed by InteractionType.
TimeStamp nsPresContext::*interactionTimes[] = {
&nsPresContext::mFirstClickTime, &nsPresContext::mFirstKeyTime,
&nsPresContext::mFirstMouseMoveTime, &nsPresContext::mFirstScrollTime};
// Array of histogram IDs for the different interaction types,
// keyed by InteractionType.
Telemetry::HistogramID histogramIds[] = {
Telemetry::TIME_TO_FIRST_CLICK_MS, Telemetry::TIME_TO_FIRST_KEY_INPUT_MS,
Telemetry::TIME_TO_FIRST_MOUSE_MOVE_MS,
Telemetry::TIME_TO_FIRST_SCROLL_MS};
TimeStamp& interactionTime =
this->*(interactionTimes[static_cast<uint32_t>(aType)]);
if (!interactionTime.IsNull()) {
// We have already recorded an interaction time.
return;
}
// Record the interaction time if it occurs after the first paint
// of the top level content document.
nsPresContext* inProcessRootPresContext =
GetInProcessRootContentDocumentPresContext();
if (!inProcessRootPresContext ||
!inProcessRootPresContext->IsRootContentDocumentCrossProcess()) {
// There is no top content pres context, or we are in a cross process
// document so we don't care about the interaction time. Record a value
// anyways to avoid trying to find the top content pres context in future
// interactions.
interactionTime = TimeStamp::Now();
return;
}
if (inProcessRootPresContext->mFirstNonBlankPaintTime.IsNull() ||
inProcessRootPresContext->mFirstNonBlankPaintTime > aTimeStamp) {
// Top content pres context has not had a non-blank paint yet
// or the event timestamp is before the first non-blank paint,
// so don't record interaction time.
return;
}
// Check if we are recording the first of any of the interaction types.
bool isFirstInteraction = true;
for (TimeStamp nsPresContext::*memberPtr : interactionTimes) {
TimeStamp& timeStamp = this->*(memberPtr);
if (!timeStamp.IsNull()) {
isFirstInteraction = false;
break;
}
}
interactionTime = TimeStamp::Now();
// Only the top level content pres context reports first interaction
// time to telemetry (if it hasn't already done so).
if (this == inProcessRootPresContext) {
if (Telemetry::CanRecordExtended()) {
double millis =
(interactionTime - mFirstNonBlankPaintTime).ToMilliseconds();
Telemetry::Accumulate(histogramIds[static_cast<uint32_t>(aType)], millis);
if (isFirstInteraction) {
Telemetry::Accumulate(Telemetry::TIME_TO_FIRST_INTERACTION_MS, millis);
}
}
} else {
inProcessRootPresContext->RecordInteractionTime(aType, aTimeStamp);
}
}
nsITheme* nsPresContext::Theme() const {
MOZ_ASSERT(mTheme);
return mTheme;
}
void nsPresContext::EnsureTheme() {
MOZ_ASSERT(!mTheme);
if (Document()->ShouldAvoidNativeTheme()) {
if (mInRDMPane) {
mTheme = do_GetRDMThemeDoNotUseDirectly();
} else {
mTheme = do_GetBasicNativeThemeDoNotUseDirectly();
}
} else {
mTheme = do_GetNativeThemeDoNotUseDirectly();
}
MOZ_RELEASE_ASSERT(mTheme);
}
void nsPresContext::RecomputeTheme() {
if (!mTheme) {
return;
}
nsCOMPtr<nsITheme> oldTheme = std::move(mTheme);
EnsureTheme();
if (oldTheme == mTheme) {
return;
}
// Theme affects layout information (as it affects whether we create
// scrollbar buttons for example) and also style (affects the
// scrollbar-inline-size env var).
RebuildAllStyleData(nsChangeHint_ReconstructFrame,
RestyleHint::RecascadeSubtree());
// This is a bit of a lie, but this affects the overlay-scrollbars
// media query and it's the code-path that gets taken for regular system
// metrics changes via ThemeChanged().
MediaFeatureValuesChanged({MediaFeatureChangeReason::SystemMetricsChange},
MediaFeatureChangePropagation::JustThisDocument);
}
bool nsPresContext::UseOverlayScrollbars() const {
return LookAndFeel::GetInt(LookAndFeel::IntID::UseOverlayScrollbars) ||
mInRDMPane;
}
void nsPresContext::ThemeChanged(widget::ThemeChangeKind aKind) {
PROFILER_MARKER_UNTYPED("ThemeChanged", LAYOUT, MarkerStack::Capture());
mPendingThemeChangeKind |= unsigned(aKind);
if (!mPendingThemeChanged) {
nsCOMPtr<nsIRunnable> ev =
new WeakRunnableMethod("nsPresContext::ThemeChangedInternal", this,
&nsPresContext::ThemeChangedInternal);
RefreshDriver()->AddEarlyRunner(ev);
mPendingThemeChanged = true;
}
MOZ_ASSERT(LookAndFeel::HasPendingGlobalThemeChange());
}
void nsPresContext::ThemeChangedInternal() {
MOZ_ASSERT(mPendingThemeChanged);
mPendingThemeChanged = false;
const auto kind = widget::ThemeChangeKind(mPendingThemeChangeKind);
mPendingThemeChangeKind = 0;
LookAndFeel::HandleGlobalThemeChange();
// Full zoom might have changed as a result of the text scale factor.
RecomputeBrowsingContextDependentData();
// Changes to system metrics and other look and feel values can change media
// queries on them.
//
// Changes in theme can change system colors (whose changes are properly
// reflected in computed style data), system fonts (whose changes are
// some reflected (like sizes and such) and some not), and -moz-appearance
// (whose changes are not), so we need to recascade for the first, and reflow
// for the rest.
auto restyleHint = (kind & widget::ThemeChangeKind::Style)
? RestyleHint::RecascadeSubtree()
: RestyleHint{0};
auto changeHint = (kind & widget::ThemeChangeKind::Layout)
? NS_STYLE_HINT_REFLOW
: nsChangeHint(0);
MediaFeatureValuesChanged(
{restyleHint, changeHint, MediaFeatureChangeReason::SystemMetricsChange},
MediaFeatureChangePropagation::All);
if (Document()->IsInChromeDocShell()) {
if (RefPtr<nsPIDOMWindowInner> win = Document()->GetInnerWindow()) {
nsContentUtils::DispatchEventOnlyToChrome(
Document(), nsGlobalWindowInner::Cast(win), u"nativethemechange"_ns,
CanBubble::eYes, Cancelable::eYes, nullptr);
}
}
}
void nsPresContext::UIResolutionChanged() {
if (!mPendingUIResolutionChanged) {
nsCOMPtr<nsIRunnable> ev =
NewRunnableMethod("nsPresContext::UIResolutionChangedInternal", this,
&nsPresContext::UIResolutionChangedInternal);
nsresult rv = Document()->Dispatch(ev.forget());
if (NS_SUCCEEDED(rv)) {
mPendingUIResolutionChanged = true;
}
}
}
void nsPresContext::UIResolutionChangedSync() {
if (!mPendingUIResolutionChanged) {
mPendingUIResolutionChanged = true;
UIResolutionChangedInternal();
}
}
static void NotifyTabUIResolutionChanged(nsIRemoteTab* aTab, void* aArg) {
aTab->NotifyResolutionChanged();
}
static void NotifyChildrenUIResolutionChanged(nsPIDOMWindowOuter* aWindow) {
nsCOMPtr<Document> doc = aWindow->GetExtantDoc();
RefPtr<nsPIWindowRoot> topLevelWin = nsContentUtils::GetWindowRoot(doc);
if (!topLevelWin) {
return;
}
topLevelWin->EnumerateBrowsers(NotifyTabUIResolutionChanged, nullptr);
}
void nsPresContext::UIResolutionChangedInternal() {
mPendingUIResolutionChanged = false;
mDeviceContext->CheckDPIChange();
if (mCurAppUnitsPerDevPixel != mDeviceContext->AppUnitsPerDevPixel()) {
AppUnitsPerDevPixelChanged();
}
if (mPresShell) {
mPresShell->RefreshZoomConstraintsForScreenSizeChange();
if (RefPtr<MobileViewportManager> mvm =
mPresShell->GetMobileViewportManager()) {
mvm->UpdateSizesBeforeReflow();
}
}
// Recursively notify all remote leaf descendants of the change.
if (nsPIDOMWindowOuter* window = mDocument->GetWindow()) {
NotifyChildrenUIResolutionChanged(window);
}
mDocument->EnumerateSubDocuments([](dom::Document& aSubDoc) {
if (nsPresContext* pc = aSubDoc.GetPresContext()) {
pc->UIResolutionChangedInternal();
}
return CallState::Continue;
});
}
void nsPresContext::EmulateMedium(nsAtom* aMediaType) {
MOZ_ASSERT(!aMediaType || aMediaType->IsAsciiLowercase());
RefPtr<const nsAtom> oldMedium = Medium();
auto oldScheme = mDocument->PreferredColorScheme();
mMediaEmulationData.mMedium = aMediaType;
if (Medium() == oldMedium) {
return;
}
MediaFeatureChange change(MediaFeatureChangeReason::MediumChange);
if (oldScheme != mDocument->PreferredColorScheme()) {
change |= MediaFeatureChange::ForPreferredColorSchemeChange();
}
MediaFeatureValuesChanged(change,
MediaFeatureChangePropagation::JustThisDocument);
}
void nsPresContext::ContentLanguageChanged() {
PostRebuildAllStyleDataEvent(nsChangeHint(0),
RestyleHint::RecascadeSubtree());
}
void nsPresContext::RegisterManagedPostRefreshObserver(
ManagedPostRefreshObserver* aObserver) {
if (MOZ_UNLIKELY(!mPresShell)) {
// If we're detached from our pres shell already, refuse to keep observer
// around, as that'd create a cycle.
RefPtr<ManagedPostRefreshObserver> obs = aObserver;
obs->Cancel();
return;
}
RefreshDriver()->AddPostRefreshObserver(
static_cast<nsAPostRefreshObserver*>(aObserver));
mManagedPostRefreshObservers.AppendElement(aObserver);
}
void nsPresContext::UnregisterManagedPostRefreshObserver(
ManagedPostRefreshObserver* aObserver) {
RefreshDriver()->RemovePostRefreshObserver(
static_cast<nsAPostRefreshObserver*>(aObserver));
DebugOnly<bool> removed =
mManagedPostRefreshObservers.RemoveElement(aObserver);
MOZ_ASSERT(removed,
"ManagedPostRefreshObserver should be owned by PresContext");
}
void nsPresContext::CancelManagedPostRefreshObservers() {
auto observers = std::move(mManagedPostRefreshObservers);
nsRefreshDriver* driver = RefreshDriver();
for (const auto& observer : observers) {
observer->Cancel();
driver->RemovePostRefreshObserver(
static_cast<nsAPostRefreshObserver*>(observer));
}
}
void nsPresContext::RebuildAllStyleData(nsChangeHint aExtraHint,
const RestyleHint& aRestyleHint) {
if (!mPresShell) {
// We must have been torn down. Nothing to do here.
return;
}
// TODO(emilio): It's unclear to me why would these three calls below be
// needed. In particular, RebuildAllStyleData doesn't rebuild rules or
// specified style information and such (note the comment in
// RestyleManager::RebuildAllStyleData re. the funny semantics), so I
// don't know why should we rebuild the user font set / counter styles /
// etc...
mDocument->MarkUserFontSetDirty();
MarkCounterStylesDirty();
MarkFontFeatureValuesDirty();
MarkFontPaletteValuesDirty();
PostRebuildAllStyleDataEvent(aExtraHint, aRestyleHint);
}
void nsPresContext::PostRebuildAllStyleDataEvent(
nsChangeHint aExtraHint, const RestyleHint& aRestyleHint) {
if (!mPresShell) {
// We must have been torn down. Nothing to do here.
return;
}
RestyleManager()->RebuildAllStyleData(aExtraHint, aRestyleHint);
}
void nsPresContext::MediaFeatureValuesChanged(
const MediaFeatureChange& aChange,
MediaFeatureChangePropagation aPropagation) {
if (mPresShell) {
mPresShell->EnsureStyleFlush();
}
if (!mDocument->MediaQueryLists().isEmpty()) {
RefreshDriver()->ScheduleMediaQueryListenerUpdate();
}
if (!mPendingMediaFeatureValuesChange) {
mPendingMediaFeatureValuesChange = MakeUnique<MediaFeatureChange>(aChange);
} else {
*mPendingMediaFeatureValuesChange |= aChange;
}
if (aPropagation & MediaFeatureChangePropagation::Images) {
// Propagate the media feature value change down to any SVG images the
// document is using.
mDocument->ImageTracker()->MediaFeatureValuesChangedAllDocuments(aChange);
}
if (aPropagation & MediaFeatureChangePropagation::SubDocuments) {
// And then into any subdocuments.
mDocument->EnumerateSubDocuments(
[&aChange, aPropagation](dom::Document& aSubDoc) {
if (nsPresContext* pc = aSubDoc.GetPresContext()) {
pc->MediaFeatureValuesChanged(aChange, aPropagation);
}
return CallState::Continue;
});
}
// We notify the media feature values changed for the responsive content of
// HTMLImageElements synchronously, so their image sources are always
// up-to-date when running the image load tasks in the microtasks.
mDocument->NotifyMediaFeatureValuesChanged();
}
bool nsPresContext::FlushPendingMediaFeatureValuesChanged() {
if (!mPendingMediaFeatureValuesChange) {
return false;
}
MediaFeatureChange change = *mPendingMediaFeatureValuesChange;
mPendingMediaFeatureValuesChange.reset();
// MediumFeaturesChanged updates the applied rules, so it always gets called.
if (mPresShell) {
change.mRestyleHint |=
mPresShell->StyleSet()->MediumFeaturesChanged(change.mReason);
}
const bool changedStyle = change.mRestyleHint || change.mChangeHint;
if (changedStyle) {
RebuildAllStyleData(change.mChangeHint, change.mRestyleHint);
}
for (MediaQueryList* mql : mDocument->MediaQueryLists()) {
mql->MediaFeatureValuesChanged();
}
return changedStyle;
}
void nsPresContext::SizeModeChanged(nsSizeMode aSizeMode) {
if (nsPIDOMWindowOuter* window = mDocument->GetWindow()) {
nsContentUtils::CallOnAllRemoteChildren(
window, [&aSizeMode](BrowserParent* aBrowserParent) -> CallState {
aBrowserParent->SizeModeChanged(aSizeMode);
return CallState::Continue;
});
}
MediaFeatureValuesChanged({MediaFeatureChangeReason::SizeModeChange},
MediaFeatureChangePropagation::SubDocuments);
}
nsCompatibility nsPresContext::CompatibilityMode() const {
return Document()->GetCompatibilityMode();
}
void nsPresContext::SetPaginatedScrolling(bool aPaginated) {
if (mType == eContext_PrintPreview || mType == eContext_PageLayout) {
mCanPaginatedScroll = aPaginated;
}
}
void nsPresContext::SetPrintSettings(nsIPrintSettings* aPrintSettings) {
if (mMedium != nsGkAtoms::print) {
return;
}
mPrintSettings = aPrintSettings;
mDefaultPageMargin = nsMargin();
if (!mPrintSettings) {
return;
}
// Set the presentation context to the value in the print settings.
mDrawColorBackground = mPrintSettings->GetPrintBGColors();
mDrawImageBackground = mPrintSettings->GetPrintBGImages();
nsIntMargin marginTwips = mPrintSettings->GetMarginInTwips();
if (!mPrintSettings->GetIgnoreUnwriteableMargins()) {
nsIntMargin unwriteableTwips =
mPrintSettings->GetUnwriteableMarginInTwips();
NS_ASSERTION(unwriteableTwips.top >= 0 && unwriteableTwips.right >= 0 &&
unwriteableTwips.bottom >= 0 && unwriteableTwips.left >= 0,
"Unwriteable twips should be non-negative");
marginTwips.EnsureAtLeast(unwriteableTwips);
}
mDefaultPageMargin = nsPresContext::CSSTwipsToAppUnits(marginTwips);
}
bool nsPresContext::EnsureVisible() {
BrowsingContext* browsingContext =
mDocument ? mDocument->GetBrowsingContext() : nullptr;
if (!browsingContext || browsingContext->IsInBFCache()) {
return false;
}
nsCOMPtr<nsIDocShell> docShell(GetDocShell());
if (!docShell) {
return false;
}
nsCOMPtr<nsIDocumentViewer> viewer;
docShell->GetDocViewer(getter_AddRefs(viewer));
// Make sure this is the content viewer we belong with
if (!viewer || viewer->GetPresContext() != this) {
return false;
}
// OK, this is us. We want to call Show() on the content viewer.
nsresult result = viewer->Show();
return NS_SUCCEEDED(result);
}
#ifdef MOZ_REFLOW_PERF
void nsPresContext::CountReflows(const char* aName, nsIFrame* aFrame) {
if (mPresShell) {
mPresShell->CountReflows(aName, aFrame);
}
}
#endif
gfxUserFontSet* nsPresContext::GetUserFontSet() {
return mDocument->GetUserFontSet();
}
void nsPresContext::UserFontSetUpdated(gfxUserFontEntry* aUpdatedFont) {
if (!mPresShell) {
return;
}
// Note: this method is called without a font when rules in the userfont set
// are updated.
//
// We can avoid a full restyle if font-metric-dependent units are not in use,
// since we know there's no style resolution that would depend on this font
// and trigger its load.
//
// TODO(emilio): We could be more granular if we knew which families have
// potentially changed.
if (!aUpdatedFont) {
auto hint = StyleSet()->UsesFontMetrics() ? RestyleHint::RecascadeSubtree()
: RestyleHint{0};
PostRebuildAllStyleDataEvent(NS_STYLE_HINT_REFLOW, hint);
return;
}
// Iterate over the frame tree looking for frames associated with the
// downloadable font family in question. If a frame's nsStyleFont has
// the name, check the font group associated with the metrics to see if
// it contains that specific font (i.e. the one chosen within the family
// given the weight, width, and slant from the nsStyleFont). If it does,
// mark that frame dirty and skip inspecting its descendants.
if (nsIFrame* root = mPresShell->GetRootFrame()) {
nsFontFaceUtils::MarkDirtyForFontChange(root, aUpdatedFont);
}
}
class CounterStyleCleaner final : public nsAPostRefreshObserver {
public:
CounterStyleCleaner(nsRefreshDriver* aRefreshDriver,
CounterStyleManager* aCounterStyleManager)
: mRefreshDriver(aRefreshDriver),
mCounterStyleManager(aCounterStyleManager) {}
virtual ~CounterStyleCleaner() = default;
void DidRefresh() final {
mRefreshDriver->RemovePostRefreshObserver(this);
mCounterStyleManager->CleanRetiredStyles();
delete this;
}
private:
RefPtr<nsRefreshDriver> mRefreshDriver;
RefPtr<CounterStyleManager> mCounterStyleManager;
};
void nsPresContext::FlushCounterStyles() {
if (!mPresShell) {
return; // we've been torn down
}
if (mCounterStyleManager->IsInitial()) {
// Still in its initial state, no need to clean.
return;
}
if (mCounterStylesDirty) {
bool changed = mCounterStyleManager->NotifyRuleChanged();
if (changed) {
PresShell()->NotifyCounterStylesAreDirty();
PostRebuildAllStyleDataEvent(NS_STYLE_HINT_REFLOW, RestyleHint{0});
RefreshDriver()->AddPostRefreshObserver(
new CounterStyleCleaner(RefreshDriver(), mCounterStyleManager));
}
mCounterStylesDirty = false;
}
}
void nsPresContext::MarkCounterStylesDirty() {
if (mCounterStyleManager->IsInitial()) {
// Still in its initial state, no need to touch anything.
return;
}
mCounterStylesDirty = true;
}
void nsPresContext::NotifyMissingFonts() {
if (mMissingFonts) {
mMissingFonts->Flush();
}
}
void nsPresContext::EnsureSafeToHandOutCSSRules() {
if (!mPresShell->StyleSet()->EnsureUniqueInnerOnCSSSheets()) {
// Nothing to do.
return;
}
RebuildAllStyleData(nsChangeHint(0), RestyleHint::RestyleSubtree());
}
void nsPresContext::FireDOMPaintEvent(
nsTArray<nsRect>* aList, TransactionId aTransactionId,
mozilla::TimeStamp aTimeStamp /* = mozilla::TimeStamp() */) {
nsPIDOMWindowInner* ourWindow = mDocument->GetInnerWindow();
if (!ourWindow) return;
nsCOMPtr<EventTarget> dispatchTarget = do_QueryInterface(ourWindow);
nsCOMPtr<EventTarget> eventTarget = dispatchTarget;
if (!IsChrome() && !StaticPrefs::dom_send_after_paint_to_content()) {
// Don't tell the window about this event, it should not know that
// something happened in a subdocument. Tell only the chrome event handler.
// (Events sent to the window get propagated to the chrome event handler
// automatically.)
dispatchTarget = ourWindow->GetParentTarget();
if (!dispatchTarget) {
return;
}
}
if (aTimeStamp.IsNull()) {
aTimeStamp = mozilla::TimeStamp::Now();
}
DOMHighResTimeStamp timeStamp = 0;
if (ourWindow) {
mozilla::dom::Performance* perf = ourWindow->GetPerformance();
if (perf) {
timeStamp = perf->GetDOMTiming()->TimeStampToDOMHighRes(aTimeStamp);
}
}
// Events sent to the window get propagated to the chrome event handler
// automatically.
//
// This will empty our list in case dispatching the event causes more damage
// (hopefully it won't, or we're likely to get an infinite loop! At least
// it won't be blocking app execution though).
RefPtr<NotifyPaintEvent> event =
NS_NewDOMNotifyPaintEvent(eventTarget, this, nullptr, eAfterPaint, aList,
uint64_t(aTransactionId), timeStamp);
// Even if we're not telling the window about the event (so eventTarget is
// the chrome event handler, not the window), the window is still
// logically the event target.
event->SetTarget(eventTarget);
event->SetTrusted(true);
EventDispatcher::DispatchDOMEvent(dispatchTarget, nullptr,
static_cast<Event*>(event), this, nullptr);
}
static bool MayHavePaintEventListener(nsPIDOMWindowInner* aInnerWindow) {
if (!aInnerWindow) return false;
if (aInnerWindow->HasPaintEventListeners()) return true;
EventTarget* parentTarget = aInnerWindow->GetParentTarget();
if (!parentTarget) return false;
EventListenerManager* manager = nullptr;
if ((manager = parentTarget->GetExistingListenerManager()) &&
manager->MayHavePaintEventListener()) {
return true;
}
nsCOMPtr<nsINode> node;
if (parentTarget != aInnerWindow->GetChromeEventHandler()) {
nsCOMPtr<nsIInProcessContentFrameMessageManager> mm =
do_QueryInterface(parentTarget);
if (mm) {
node = mm->GetOwnerContent();
}
}
if (!node) {
node = nsINode::FromEventTarget(parentTarget);
}
if (node) {
return MayHavePaintEventListener(node->OwnerDoc()->GetInnerWindow());
}
if (nsCOMPtr<nsPIDOMWindowInner> window =
nsPIDOMWindowInner::FromEventTarget(parentTarget)) {
return MayHavePaintEventListener(window);
}
if (nsCOMPtr<nsPIWindowRoot> root =
nsPIWindowRoot::FromEventTarget(parentTarget)) {
EventTarget* browserChildGlobal;
return root && (browserChildGlobal = root->GetParentTarget()) &&
(manager = browserChildGlobal->GetExistingListenerManager()) &&
manager->MayHavePaintEventListener();
}
return false;
}
bool nsPresContext::MayHavePaintEventListener() {
return ::MayHavePaintEventListener(mDocument->GetInnerWindow());
}
void nsPresContext::NotifyInvalidation(TransactionId aTransactionId,
const nsIntRect& aRect) {
// Prevent values from overflow after DevPixelsToAppUnits().
//
// DevPixelsTopAppUnits() will multiple a factor (60) to the value,
// it may make the result value over the edge (overflow) of max or
// min value of int32_t. Compute the max sized dev pixel rect that
// we can support and intersect with it.
nsIntRect clampedRect = nsIntRect::MaxIntRect();
clampedRect.ScaleInverseRoundIn(AppUnitsPerDevPixel());
clampedRect = clampedRect.Intersect(aRect);
nsRect rect(DevPixelsToAppUnits(clampedRect.x),
DevPixelsToAppUnits(clampedRect.y),
DevPixelsToAppUnits(clampedRect.width),
DevPixelsToAppUnits(clampedRect.height));
NotifyInvalidation(aTransactionId, rect);
}
nsPresContext::TransactionInvalidations* nsPresContext::GetInvalidations(
TransactionId aTransactionId) {
for (TransactionInvalidations& t : mTransactions) {
if (t.mTransactionId == aTransactionId) {
return &t;
}
}
return nullptr;
}
void nsPresContext::NotifyInvalidation(TransactionId aTransactionId,
const nsRect& aRect) {
MOZ_ASSERT(GetContainerWeak(), "Invalidation in detached pres context");
// If there is no paint event listener, then we don't need to fire
// the asynchronous event. We don't even need to record invalidation.
// MayHavePaintEventListener is pretty cheap and we could make it
// even cheaper by providing a more efficient
// nsPIDOMWindow::GetListenerManager.
nsPresContext* pc;
for (pc = this; pc; pc = pc->GetParentPresContext()) {
TransactionInvalidations* transaction =
pc->GetInvalidations(aTransactionId);
if (transaction) {
break;
} else {
transaction = pc->mTransactions.AppendElement();
transaction->mTransactionId = aTransactionId;
}
}
TransactionInvalidations* transaction = GetInvalidations(aTransactionId);
MOZ_ASSERT(transaction);
transaction->mInvalidations.AppendElement(aRect);
}
class DelayedFireDOMPaintEvent : public Runnable {
public:
DelayedFireDOMPaintEvent(
nsPresContext* aPresContext, nsTArray<nsRect>&& aList,
TransactionId aTransactionId,
const mozilla::TimeStamp& aTimeStamp = mozilla::TimeStamp())
: mozilla::Runnable("DelayedFireDOMPaintEvent"),
mPresContext(aPresContext),
mTransactionId(aTransactionId),
mTimeStamp(aTimeStamp),
mList(std::move(aList)) {
MOZ_ASSERT(mPresContext->GetContainerWeak(),
"DOMPaintEvent requested for a detached pres context");
}
NS_IMETHOD Run() override {
// The pres context might have been detached during the delay -
// that's fine, just don't fire the event.
if (mPresContext->GetContainerWeak()) {
mPresContext->FireDOMPaintEvent(&mList, mTransactionId, mTimeStamp);
}
return NS_OK;
}
RefPtr<nsPresContext> mPresContext;
TransactionId mTransactionId;
const mozilla::TimeStamp mTimeStamp;
nsTArray<nsRect> mList;
};
void nsPresContext::NotifyRevokingDidPaint(TransactionId aTransactionId) {
if ((IsRoot() || !PresShell()->IsVisible()) && mTransactions.IsEmpty()) {
return;
}
TransactionInvalidations* transaction = nullptr;
for (auto& t : mTransactions) {
if (t.mTransactionId == aTransactionId) {
transaction = &t;
break;
}
}
// If there are no transaction invalidations (which imply callers waiting
// on the event) for this revoked id, then we don't need to fire a
// MozAfterPaint.
if (!transaction) {
return;
}
// If there are queued transactions with an earlier id, we can't send
// our event now since it will arrive out of order. Set the waiting for
// previous transaction flag to true, and we'll send the event when
// the others are completed.
// If this is the only transaction, then we can send it immediately.
if (mTransactions.Length() == 1) {
nsCOMPtr<nsIRunnable> ev = new DelayedFireDOMPaintEvent(
this, std::move(transaction->mInvalidations),
transaction->mTransactionId, mozilla::TimeStamp());
nsContentUtils::AddScriptRunner(ev);
mTransactions.RemoveElementAt(0);
} else {
transaction->mIsWaitingForPreviousTransaction = true;
}
mDocument->EnumerateSubDocuments([&aTransactionId](dom::Document& aSubDoc) {
if (nsPresContext* pc = aSubDoc.GetPresContext()) {
pc->NotifyRevokingDidPaint(aTransactionId);
}
return CallState::Continue;
});
}
void nsPresContext::NotifyDidPaintForSubtree(
TransactionId aTransactionId, const mozilla::TimeStamp& aTimeStamp) {
if (mFirstContentfulPaintTransactionId && !mHadContentfulPaintComposite) {
if (aTransactionId >= *mFirstContentfulPaintTransactionId) {
mHadContentfulPaintComposite = true;
RefPtr<nsDOMNavigationTiming> timing = mDocument->GetNavigationTiming();
if (timing && !IsPrintingOrPrintPreview()) {
timing->NotifyContentfulCompositeForRootContentDocument(aTimeStamp);
}
}
}
if (IsRoot() && mTransactions.IsEmpty()) {
return;
}
if (!PresShell()->IsVisible() && mTransactions.IsEmpty()) {
return;
}
// Non-root prescontexts fire MozAfterPaint to all their descendants
// unconditionally, even if no invalidations have been collected. This is
// because we don't want to eat the cost of collecting invalidations for
// every subdocument (which would require putting every subdocument in its
// own layer).
bool sent = false;
uint32_t i = 0;
while (i < mTransactions.Length()) {
if (mTransactions[i].mTransactionId <= aTransactionId) {
if (!mTransactions[i].mInvalidations.IsEmpty()) {
nsCOMPtr<nsIRunnable> ev = new DelayedFireDOMPaintEvent(
this, std::move(mTransactions[i].mInvalidations),
mTransactions[i].mTransactionId, aTimeStamp);
NS_DispatchToCurrentThreadQueue(ev.forget(),
EventQueuePriority::MediumHigh);
sent = true;
}
mTransactions.RemoveElementAt(i);
} else {
// If there are transaction which is waiting for this transaction,
// we should fire a MozAfterPaint immediately.
if (sent && mTransactions[i].mIsWaitingForPreviousTransaction) {
nsCOMPtr<nsIRunnable> ev = new DelayedFireDOMPaintEvent(
this, std::move(mTransactions[i].mInvalidations),
mTransactions[i].mTransactionId, aTimeStamp);
NS_DispatchToCurrentThreadQueue(ev.forget(),
EventQueuePriority::MediumHigh);
sent = true;
mTransactions.RemoveElementAt(i);
continue;
}
i++;
}
}
if (!sent) {
nsTArray<nsRect> dummy;
nsCOMPtr<nsIRunnable> ev = new DelayedFireDOMPaintEvent(
this, std::move(dummy), aTransactionId, aTimeStamp);
NS_DispatchToCurrentThreadQueue(ev.forget(),
EventQueuePriority::MediumHigh);
}
mDocument->EnumerateSubDocuments(
[&aTransactionId, &aTimeStamp](dom::Document& aSubDoc) {
if (nsPresContext* pc = aSubDoc.GetPresContext()) {
pc->NotifyDidPaintForSubtree(aTransactionId, aTimeStamp);
}
return CallState::Continue;
});
}
already_AddRefed<nsITimer> nsPresContext::CreateTimer(
nsTimerCallbackFunc aCallback, const char* aName, uint32_t aDelay) {
nsCOMPtr<nsITimer> timer;
NS_NewTimerWithFuncCallback(getter_AddRefs(timer), aCallback, this, aDelay,
nsITimer::TYPE_ONE_SHOT, aName,
GetMainThreadSerialEventTarget());
return timer.forget();
}
static bool sGotInterruptEnv = false;
enum InterruptMode { ModeRandom, ModeCounter, ModeEvent };
// Controlled by the GECKO_REFLOW_INTERRUPT_MODE env var; allowed values are
// "random" (except on Windows) or "counter". If neither is used, the mode is
// ModeEvent.
static InterruptMode sInterruptMode = ModeEvent;
#ifndef XP_WIN
// Used for the "random" mode. Controlled by the GECKO_REFLOW_INTERRUPT_SEED
// env var.
static uint32_t sInterruptSeed = 1;
#endif
// Used for the "counter" mode. This is the number of unskipped interrupt
// checks that have to happen before we interrupt. Controlled by the
// GECKO_REFLOW_INTERRUPT_FREQUENCY env var.
static uint32_t sInterruptMaxCounter = 10;
// Used for the "counter" mode. This counts up to sInterruptMaxCounter and is
// then reset to 0.
static uint32_t sInterruptCounter;
// Number of interrupt checks to skip before really trying to interrupt.
// Controlled by the GECKO_REFLOW_INTERRUPT_CHECKS_TO_SKIP env var.
static uint32_t sInterruptChecksToSkip = 200;
// Number of milliseconds that a reflow should be allowed to run for before we
// actually allow interruption. Controlled by the
// GECKO_REFLOW_MIN_NOINTERRUPT_DURATION env var. Can't be initialized here,
// because TimeDuration/TimeStamp is not safe to use in static constructors..
static TimeDuration sInterruptTimeout;
static void GetInterruptEnv() {
char* ev = PR_GetEnv("GECKO_REFLOW_INTERRUPT_MODE");
if (ev) {
#ifndef XP_WIN
if (nsCRT::strcasecmp(ev, "random") == 0) {
ev = PR_GetEnv("GECKO_REFLOW_INTERRUPT_SEED");
if (ev) {
sInterruptSeed = atoi(ev);
}
srandom(sInterruptSeed);
sInterruptMode = ModeRandom;
} else
#endif
if (PL_strcasecmp(ev, "counter") == 0) {
ev = PR_GetEnv("GECKO_REFLOW_INTERRUPT_FREQUENCY");
if (ev) {
sInterruptMaxCounter = atoi(ev);
}
sInterruptCounter = 0;
sInterruptMode = ModeCounter;
}
}
ev = PR_GetEnv("GECKO_REFLOW_INTERRUPT_CHECKS_TO_SKIP");
if (ev) {
sInterruptChecksToSkip = atoi(ev);
}
ev = PR_GetEnv("GECKO_REFLOW_MIN_NOINTERRUPT_DURATION");
int duration_ms = ev ? atoi(ev) : 100;
sInterruptTimeout = TimeDuration::FromMilliseconds(duration_ms);
}
bool nsPresContext::HavePendingInputEvent() {
switch (sInterruptMode) {
#ifndef XP_WIN
case ModeRandom:
return (random() & 1);
#endif
case ModeCounter:
if (sInterruptCounter < sInterruptMaxCounter) {
++sInterruptCounter;
return false;
}
sInterruptCounter = 0;
return true;
default:
case ModeEvent: {
nsIFrame* f = PresShell()->GetRootFrame();
if (f) {
nsIWidget* w = f->GetNearestWidget();
if (w) {
return w->HasPendingInputEvent();
}
}
return false;
}
}
}
bool nsPresContext::HasPendingRestyleOrReflow() {
mozilla::PresShell* presShell = PresShell();
return presShell->NeedStyleFlush() || presShell->HasPendingReflow();
}
void nsPresContext::ReflowStarted(bool aInterruptible) {
#ifdef NOISY_INTERRUPTIBLE_REFLOW
if (!aInterruptible) {
printf("STARTING NONINTERRUPTIBLE REFLOW\n");
}
#endif
// We don't support interrupting in paginated contexts, since page
// sequences only handle initial reflow
mInterruptsEnabled = aInterruptible && !IsPaginated() &&
StaticPrefs::layout_interruptible_reflow_enabled();
// Don't set mHasPendingInterrupt based on HavePendingInputEvent() here. If
// we ever change that, then we need to update the code in
// PresShell::DoReflow to only add the just-reflown root to dirty roots if
// it's actually dirty. Otherwise we can end up adding a root that has no
// interruptible descendants, just because we detected an interrupt at reflow
// start.
mHasPendingInterrupt = false;
mInterruptChecksToSkip = sInterruptChecksToSkip;
if (mInterruptsEnabled) {
mReflowStartTime = TimeStamp::Now();
}
}
bool nsPresContext::CheckForInterrupt(nsIFrame* aFrame) {
if (mHasPendingInterrupt) {
mPresShell->FrameNeedsToContinueReflow(aFrame);
return true;
}
if (!sGotInterruptEnv) {
sGotInterruptEnv = true;
GetInterruptEnv();
}
if (!mInterruptsEnabled) {
return false;
}
if (mInterruptChecksToSkip > 0) {
--mInterruptChecksToSkip;
return false;
}
mInterruptChecksToSkip = sInterruptChecksToSkip;
// Don't interrupt if it's been less than sInterruptTimeout since we started
// the reflow.
mHasPendingInterrupt =
TimeStamp::Now() - mReflowStartTime > sInterruptTimeout &&
HavePendingInputEvent() && !IsChrome();
if (mPendingInterruptFromTest) {
mPendingInterruptFromTest = false;
mHasPendingInterrupt = true;
}
if (mHasPendingInterrupt) {
#ifdef NOISY_INTERRUPTIBLE_REFLOW
printf("*** DETECTED pending interrupt (time=%lld)\n", PR_Now());
#endif /* NOISY_INTERRUPTIBLE_REFLOW */
mPresShell->FrameNeedsToContinueReflow(aFrame);
}
return mHasPendingInterrupt;
}
nsIFrame* nsPresContext::GetPrimaryFrameFor(nsIContent* aContent) {
MOZ_ASSERT(aContent, "Don't do that");
if (GetPresShell() &&
GetPresShell()->GetDocument() == aContent->GetComposedDoc()) {
return aContent->GetPrimaryFrame();
}
return nullptr;
}
size_t nsPresContext::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const {
// Measurement may be added later if DMD finds it is worthwhile.
return 0;
}
bool nsPresContext::IsRootContentDocumentInProcess() const {
if (mDocument->IsResourceDoc()) {
return false;
}
if (IsChrome()) {
return false;
}
// We may not have a root frame, so use views.
nsView* view = PresShell()->GetViewManager()->GetRootView();
if (!view) {
return false;
}
view = view->GetParent(); // anonymous inner view
if (!view) {
return true;
}
view = view->GetParent(); // subdocumentframe's view
if (!view) {
return true;
}
nsIFrame* f = view->GetFrame();
return (f && f->PresContext()->IsChrome());
}
bool nsPresContext::IsRootContentDocumentCrossProcess() const {
if (mDocument->IsResourceDoc()) {
return false;
}
if (BrowsingContext* browsingContext = mDocument->GetBrowsingContext()) {
if (browsingContext->GetEmbeddedInContentDocument()) {
return false;
}
}
return mDocument->IsTopLevelContentDocument();
}
void nsPresContext::NotifyNonBlankPaint() {
MOZ_ASSERT(!mHadNonBlankPaint);
mHadNonBlankPaint = true;
if (IsRootContentDocumentCrossProcess()) {
RefPtr<nsDOMNavigationTiming> timing = mDocument->GetNavigationTiming();
if (timing && !IsPrintingOrPrintPreview()) {
timing->NotifyNonBlankPaintForRootContentDocument();
}
mFirstNonBlankPaintTime = TimeStamp::Now();
}
if (IsChrome() && IsRoot()) {
if (nsCOMPtr<nsIWidget> rootWidget = GetRootWidget()) {
rootWidget->DidGetNonBlankPaint();
}
}
}
bool nsPresContext::HasStoppedGeneratingLCP() const {
if (auto* perf = GetPerformanceMainThread()) {
return perf->HasDispatchedInputEvent() || perf->HasDispatchedScrollEvent();
}
return true;
}
void nsPresContext::NotifyContentfulPaint() {
if (mHadFirstContentfulPaint && HasStoppedGeneratingLCP()) {
return;
}
nsRootPresContext* rootPresContext = GetRootPresContext();
if (!rootPresContext) {
return;
}
if (!mHadNonTickContentfulPaint) {
#ifdef MOZ_WIDGET_ANDROID
(new AsyncEventDispatcher(mDocument, u"MozFirstContentfulPaint"_ns,
CanBubble::eYes, ChromeOnlyDispatch::eYes))
->PostDOMEvent();
#endif
}
if (!rootPresContext->RefreshDriver()->IsInRefresh()) {
if (!mHadNonTickContentfulPaint) {
rootPresContext->RefreshDriver()
->AddForceNotifyContentfulPaintPresContext(this);
mHadNonTickContentfulPaint = true;
}
return;
}
if (!mHadFirstContentfulPaint) {
mHadFirstContentfulPaint = true;
mFirstContentfulPaintTransactionId =
Some(rootPresContext->mRefreshDriver->LastTransactionId().Next());
}
if (auto* perf = GetPerformanceMainThread()) {
mMarkPaintTimingStart = TimeStamp::Now();
MOZ_ASSERT(rootPresContext->RefreshDriver()->IsInRefresh(),
"We should only notify contentful paint during refresh "
"driver ticks");
if (!perf->HadFCPTimingEntry()) {
TimeStamp nowTime = rootPresContext->RefreshDriver()->MostRecentRefresh(
/* aEnsureTimerStarted */ false);
MOZ_ASSERT(!nowTime.IsNull(),
"Most recent refresh timestamp should exist since we are in "
"a refresh driver tick");
RefPtr<PerformancePaintTiming> paintTiming = new PerformancePaintTiming(
perf, u"first-contentful-paint"_ns, nowTime);
perf->SetFCPTimingEntry(paintTiming);
if (profiler_thread_is_being_profiled_for_markers()) {
RefPtr<nsDOMNavigationTiming> timing = mDocument->GetNavigationTiming();
if (timing) {
TimeStamp navigationStart = timing->GetNavigationStartTimeStamp();
TimeDuration elapsed = nowTime - navigationStart;
nsIURI* docURI = Document()->GetDocumentURI();
nsPrintfCString marker(
"Contentful paint after %dms for URL %s",
int(elapsed.ToMilliseconds()),
nsContentUtils::TruncatedURLForDisplay(docURI).get());
PROFILER_MARKER_TEXT(
"FirstContentfulPaint", DOM,
MarkerOptions(
MarkerTiming::Interval(navigationStart, nowTime),
MarkerInnerWindowId(mDocument->GetInnerWindow()->WindowID())),
marker);
}
}
}
perf->ProcessElementTiming();
}
}
void nsPresContext::NotifyPaintStatusReset() {
mHadNonBlankPaint = false;
mHadFirstContentfulPaint = false;
#if defined(MOZ_WIDGET_ANDROID)
(new AsyncEventDispatcher(mDocument, u"MozPaintStatusReset"_ns,
CanBubble::eYes, ChromeOnlyDispatch::eYes))
->PostDOMEvent();
#endif
mHadNonTickContentfulPaint = false;
}
void nsPresContext::NotifyDOMContentFlushed() {
NS_ENSURE_TRUE_VOID(mPresShell);
if (IsRootContentDocumentCrossProcess()) {
RefPtr<nsDOMNavigationTiming> timing = mDocument->GetNavigationTiming();
if (timing) {
timing->NotifyDOMContentFlushedForRootContentDocument();
}
}
}
nscoord nsPresContext::GfxUnitsToAppUnits(gfxFloat aGfxUnits) const {
return mDeviceContext->GfxUnitsToAppUnits(aGfxUnits);
}
gfxFloat nsPresContext::AppUnitsToGfxUnits(nscoord aAppUnits) const {
return mDeviceContext->AppUnitsToGfxUnits(aAppUnits);
}
nscoord nsPresContext::PhysicalMillimetersToAppUnits(float aMM) const {
float inches = aMM / MM_PER_INCH_FLOAT;
return NSToCoordFloorClamped(
inches * float(DeviceContext()->AppUnitsPerPhysicalInch()));
}
uint64_t nsPresContext::GetRestyleGeneration() const {
if (!mRestyleManager) {
return 0;
}
return mRestyleManager->GetRestyleGeneration();
}
uint64_t nsPresContext::GetUndisplayedRestyleGeneration() const {
if (!mRestyleManager) {
return 0;
}
return mRestyleManager->GetUndisplayedRestyleGeneration();
}
mozilla::intl::Bidi& nsPresContext::BidiEngine() {
MOZ_ASSERT(NS_IsMainThread());
if (!mBidiEngine) {
mBidiEngine = MakeUnique<mozilla::intl::Bidi>();
}
return *mBidiEngine;
}
void nsPresContext::FlushFontFeatureValues() {
if (!mPresShell) {
return; // we've been torn down
}
if (!mFontFeatureValuesDirty) {
return;
}
ServoStyleSet* styleSet = mPresShell->StyleSet();
mFontFeatureValuesLookup = styleSet->BuildFontFeatureValueSet();
mFontFeatureValuesDirty = false;
}
void nsPresContext::FlushFontPaletteValues() {
if (!mPresShell) {
return; // we've been torn down
}
if (!mFontPaletteValuesDirty) {
return;
}
ServoStyleSet* styleSet = mPresShell->StyleSet();
mFontPaletteValueSet = styleSet->BuildFontPaletteValueSet();
mFontPaletteValuesDirty = false;
if (mFontPaletteCache) {
mFontPaletteCache->SetPaletteValueSet(mFontPaletteValueSet);
}
// Even if we're not reflowing anything, a change to the palette means we
// need to repaint in order to show the new colors.
InvalidatePaintedLayers();
}
gfx::PaletteCache& nsPresContext::FontPaletteCache() {
if (!mFontPaletteCache) {
mFontPaletteCache = MakeUnique<gfx::PaletteCache>(mFontPaletteValueSet);
}
return *mFontPaletteCache.get();
}
void nsPresContext::SetVisibleArea(const nsRect& r) {
if (!r.IsEqualEdges(mVisibleArea)) {
mVisibleArea = r;
mSizeForViewportUnits = mVisibleArea.Size();
if (IsRootContentDocumentCrossProcess()) {
AdjustSizeForViewportUnits();
}
// Visible area does not affect media queries when paginated.
if (!IsRootPaginatedDocument()) {
MediaFeatureValuesChanged(
{mozilla::MediaFeatureChangeReason::ViewportChange},
MediaFeatureChangePropagation::JustThisDocument);
}
}
}
void nsPresContext::SetDynamicToolbarMaxHeight(ScreenIntCoord aHeight) {
MOZ_ASSERT(IsRootContentDocumentCrossProcess());
if (mDynamicToolbarMaxHeight == aHeight) {
return;
}
mDynamicToolbarMaxHeight = aHeight;
mDynamicToolbarHeight = aHeight;
AdjustSizeForViewportUnits();
if (RefPtr<mozilla::PresShell> presShell = mPresShell) {
// Changing the max height of the dynamic toolbar changes the ICB size, we
// need to kick a reflow with the current window dimensions since the max
// height change doesn't change the window dimensions but
// PresShell::ResizeReflow ends up subtracting the new dynamic toolbar
// height from the window dimensions and kick a reflow with the proper ICB
// size.
presShell->ForceResizeReflowWithCurrentDimensions();
}
}
void nsPresContext::AdjustSizeForViewportUnits() {
MOZ_ASSERT(IsRootContentDocumentCrossProcess());
if (mVisibleArea.height == NS_UNCONSTRAINEDSIZE) {
// Ignore `NS_UNCONSTRAINEDSIZE` since it's a temporary state during a
// reflow. We will end up calling this function again with a proper size in
// the same reflow.
return;
}
if (MOZ_UNLIKELY(mVisibleArea.height +
NSIntPixelsToAppUnits(mDynamicToolbarMaxHeight,
mCurAppUnitsPerDevPixel) >
nscoord_MAX)) {
MOZ_ASSERT_UNREACHABLE("The dynamic toolbar max height is probably wrong");
return;
}
mSizeForViewportUnits.height =
mVisibleArea.height +
NSIntPixelsToAppUnits(mDynamicToolbarMaxHeight, mCurAppUnitsPerDevPixel);
}
void nsPresContext::UpdateDynamicToolbarOffset(ScreenIntCoord aOffset) {
MOZ_ASSERT(IsRootContentDocumentCrossProcess());
if (!mPresShell) {
return;
}
if (!HasDynamicToolbar()) {
return;
}
MOZ_ASSERT(-mDynamicToolbarMaxHeight <= aOffset && aOffset <= 0);
if (mDynamicToolbarHeight == mDynamicToolbarMaxHeight + aOffset) {
return;
}
// Forcibly flush position:fixed elements in the case where the dynamic
// toolbar is going to be completely hidden or starts to be visible so that
// %-based style values will be recomputed with the visual viewport size which
// is including the area covered by the dynamic toolbar.
if (mDynamicToolbarHeight == 0 || aOffset == -mDynamicToolbarMaxHeight) {
mPresShell->MarkFixedFramesForReflow(IntrinsicDirty::None);
mPresShell->AddResizeEventFlushObserverIfNeeded();
}
mDynamicToolbarHeight = mDynamicToolbarMaxHeight + aOffset;
if (RefPtr<MobileViewportManager> mvm =
mPresShell->GetMobileViewportManager()) {
mvm->UpdateVisualViewportSizeByDynamicToolbar(-aOffset);
}
mPresShell->StyleSet()->InvalidateForViewportUnits(
ServoStyleSet::OnlyDynamic::Yes);
}
DynamicToolbarState nsPresContext::GetDynamicToolbarState() const {
if (!IsRootContentDocumentCrossProcess() || !HasDynamicToolbar()) {
return DynamicToolbarState::None;
}
if (mDynamicToolbarMaxHeight == mDynamicToolbarHeight) {
return DynamicToolbarState::Expanded;
} else if (mDynamicToolbarHeight == 0) {
return DynamicToolbarState::Collapsed;
}
return DynamicToolbarState::InTransition;
}
void nsPresContext::SetSafeAreaInsets(const ScreenIntMargin& aSafeAreaInsets) {
if (mSafeAreaInsets == aSafeAreaInsets) {
return;
}
mSafeAreaInsets = aSafeAreaInsets;
PostRebuildAllStyleDataEvent(nsChangeHint(0),
RestyleHint::RecascadeSubtree());
}
PerformanceMainThread* nsPresContext::GetPerformanceMainThread() const {
if (nsPIDOMWindowInner* innerWindow = mDocument->GetInnerWindow()) {
if (auto* perf = static_cast<PerformanceMainThread*>(
innerWindow->GetPerformance())) {
return perf;
}
}
return nullptr;
}
void nsPresContext::DoUpdateHiddenByContentVisibilityForAnimations() {
MOZ_ASSERT(NeedsToUpdateHiddenByContentVisibilityForAnimations());
mNeedsToUpdateHiddenByContentVisibilityForAnimations = false;
mDocument->UpdateHiddenByContentVisibilityForAnimations();
TimelineManager()->UpdateHiddenByContentVisibilityForAnimations();
}
#ifdef DEBUG
void nsPresContext::ValidatePresShellAndDocumentReleation() const {
NS_ASSERTION(!mPresShell || !mPresShell->GetDocument() ||
mPresShell->GetDocument() == mDocument,
"nsPresContext doesn't have the same document as nsPresShell!");
}
#endif // #ifdef DEBUG
nsRootPresContext::nsRootPresContext(dom::Document* aDocument,
nsPresContextType aType)
: nsPresContext(aDocument, aType) {}
void nsRootPresContext::AddWillPaintObserver(nsIRunnable* aRunnable) {
if (!mWillPaintFallbackEvent.IsPending()) {
mWillPaintFallbackEvent = new RunWillPaintObservers(this);
Document()->Dispatch(do_AddRef(mWillPaintFallbackEvent));
}
mWillPaintObservers.AppendElement(aRunnable);
}
/**
* Run all runnables that need to get called before the next paint.
*/
void nsRootPresContext::FlushWillPaintObservers() {
mWillPaintFallbackEvent = nullptr;
nsTArray<nsCOMPtr<nsIRunnable>> observers = std::move(mWillPaintObservers);
for (uint32_t i = 0; i < observers.Length(); ++i) {
observers[i]->Run();
}
}
size_t nsRootPresContext::SizeOfExcludingThis(
MallocSizeOf aMallocSizeOf) const {
return nsPresContext::SizeOfExcludingThis(aMallocSizeOf);
// Measurement of the following members may be added later if DMD finds it is
// worthwhile:
// - mWillPaintObservers
// - mWillPaintFallbackEvent
}
|