1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147
|
/*
* Copyright (C) 1998, 1999 Torben Weis <weis@kde.org>
* 1999 Lars Knoll <knoll@kde.org>
* 1999 Antti Koivisto <koivisto@kde.org>
* 2000 Simon Hausmann <hausmann@kde.org>
* 2000 Stefan Schimanski <1Stein@gmx.de>
* 2001 George Staikos <staikos@kde.org>
* Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All
* rights reserved.
* Copyright (C) 2005 Alexey Proskuryakov <ap@nypop.com>
* Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
* Copyright (C) 2008 Eric Seidel <eric@webkit.org>
* Copyright (C) 2008 Google Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "third_party/blink/renderer/core/frame/local_frame.h"
#include <cstdint>
#include <limits>
#include <memory>
#include <utility>
#include <vector>
#include "base/check_deref.h"
#include "base/debug/dump_without_crashing.h"
#include "base/metrics/histogram_functions.h"
#include "base/numerics/safe_conversions.h"
#include "base/task/single_thread_task_runner.h"
#include "base/time/time.h"
#include "base/unguessable_token.h"
#include "base/values.h"
#include "build/build_config.h"
#include "mojo/public/cpp/bindings/self_owned_receiver.h"
#include "mojo/public/cpp/system/message_pipe.h"
#include "services/network/public/cpp/features.h"
#include "services/network/public/mojom/content_security_policy.mojom-blink.h"
#include "services/network/public/mojom/source_location.mojom-blink.h"
#include "skia/public/mojom/skcolor.mojom-blink.h"
#include "third_party/abseil-cpp/absl/cleanup/cleanup.h"
#include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h"
#include "third_party/blink/public/common/chrome_debug_urls.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/common/input/web_input_event_attribution.h"
#include "third_party/blink/public/common/loader/lcp_critical_path_predictor_util.h"
#include "third_party/blink/public/common/storage_key/storage_key.h"
#include "third_party/blink/public/common/thread_safe_browser_interface_broker_proxy.h"
#include "third_party/blink/public/mojom/back_forward_cache_not_restored_reasons.mojom-blink.h"
#include "third_party/blink/public/mojom/blob/blob_url_store.mojom-blink.h"
#include "third_party/blink/public/mojom/favicon/favicon_url.mojom-blink.h"
#include "third_party/blink/public/mojom/fetch/fetch_api_request.mojom-blink.h"
#include "third_party/blink/public/mojom/frame/back_forward_cache_controller.mojom-blink.h"
#include "third_party/blink/public/mojom/frame/blocked_navigation_types.mojom-blink.h"
#include "third_party/blink/public/mojom/frame/frame.mojom-blink.h"
#include "third_party/blink/public/mojom/frame/frame_owner_properties.mojom-blink.h"
#include "third_party/blink/public/mojom/frame/lifecycle.mojom-blink.h"
#include "third_party/blink/public/mojom/frame/media_player_action.mojom-blink.h"
#include "third_party/blink/public/mojom/frame/reporting_observer.mojom-blink.h"
#include "third_party/blink/public/mojom/frame/user_activation_notification_type.mojom-shared.h"
#include "third_party/blink/public/mojom/lcp_critical_path_predictor/lcp_critical_path_predictor.mojom-blink.h"
#include "third_party/blink/public/mojom/script_source_location.mojom-blink.h"
#include "third_party/blink/public/mojom/scroll/scrollbar_mode.mojom-blink.h"
#include "third_party/blink/public/platform/browser_interface_broker_proxy.h"
#include "third_party/blink/public/platform/interface_registry.h"
#include "third_party/blink/public/platform/platform.h"
#include "third_party/blink/public/platform/url_conversion.h"
#include "third_party/blink/public/platform/web_background_resource_fetch_assets.h"
#include "third_party/blink/public/platform/web_content_settings_client.h"
#include "third_party/blink/public/platform/web_string.h"
#include "third_party/blink/public/platform/web_url_request.h"
#include "third_party/blink/public/web/web_content_capture_client.h"
#include "third_party/blink/public/web/web_frame.h"
#include "third_party/blink/public/web/web_link_preview_triggerer.h"
#include "third_party/blink/public/web/web_local_frame_client.h"
#include "third_party/blink/renderer/bindings/core/v8/script_controller.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_binding_for_core.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_local_compile_hints_producer.h"
#include "third_party/blink/renderer/bindings/core/v8/window_proxy_manager.h"
#include "third_party/blink/renderer/core/clipboard/system_clipboard.h"
#include "third_party/blink/renderer/core/content_capture/content_capture_manager.h"
#include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/core/core_initializer.h"
#include "third_party/blink/renderer/core/core_probe_sink.h"
#include "third_party/blink/renderer/core/css/background_color_paint_image_generator.h"
#include "third_party/blink/renderer/core/css/box_shadow_paint_image_generator.h"
#include "third_party/blink/renderer/core/css/clip_path_paint_image_generator.h"
#include "third_party/blink/renderer/core/css/css_default_style_sheets.h"
#include "third_party/blink/renderer/core/css/document_style_environment_variables.h"
#include "third_party/blink/renderer/core/css/style_change_reason.h"
#include "third_party/blink/renderer/core/dom/child_frame_disconnector.h"
#include "third_party/blink/renderer/core/dom/document_init.h"
#include "third_party/blink/renderer/core/dom/document_parser.h"
#include "third_party/blink/renderer/core/dom/document_type.h"
#include "third_party/blink/renderer/core/dom/dom_node_ids.h"
#include "third_party/blink/renderer/core/dom/events/event.h"
#include "third_party/blink/renderer/core/dom/focus_params.h"
#include "third_party/blink/renderer/core/dom/ignore_opens_during_unload_count_incrementer.h"
#include "third_party/blink/renderer/core/editing/editing_utilities.h"
#include "third_party/blink/renderer/core/editing/editor.h"
#include "third_party/blink/renderer/core/editing/frame_selection.h"
#include "third_party/blink/renderer/core/editing/ime/input_method_controller.h"
#include "third_party/blink/renderer/core/editing/serializers/create_markup_options.h"
#include "third_party/blink/renderer/core/editing/serializers/serialization.h"
#include "third_party/blink/renderer/core/editing/spellcheck/spell_checker.h"
#include "third_party/blink/renderer/core/editing/suggestion/text_suggestion_controller.h"
#include "third_party/blink/renderer/core/editing/surrounding_text.h"
#include "third_party/blink/renderer/core/editing/visible_position.h"
#include "third_party/blink/renderer/core/event_type_names.h"
#include "third_party/blink/renderer/core/events/message_event.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.h"
#include "third_party/blink/renderer/core/execution_context/security_context.h"
#include "third_party/blink/renderer/core/execution_context/window_agent.h"
#include "third_party/blink/renderer/core/exported/web_plugin_container_impl.h"
#include "third_party/blink/renderer/core/fileapi/public_url_manager.h"
#include "third_party/blink/renderer/core/fragment_directive/text_fragment_handler.h"
#include "third_party/blink/renderer/core/frame/ad_tracker.h"
#include "third_party/blink/renderer/core/frame/attribution_src_loader.h"
#include "third_party/blink/renderer/core/frame/context_menu_insets_changed_observer.h"
#include "third_party/blink/renderer/core/frame/csp/content_security_policy.h"
#include "third_party/blink/renderer/core/frame/event_handler_registry.h"
#include "third_party/blink/renderer/core/frame/frame_console.h"
#include "third_party/blink/renderer/core/frame/frame_overlay.h"
#include "third_party/blink/renderer/core/frame/frame_serializer.h"
#include "third_party/blink/renderer/core/frame/local_dom_window.h"
#include "third_party/blink/renderer/core/frame/local_frame_client.h"
#include "third_party/blink/renderer/core/frame/local_frame_mojo_handler.h"
#include "third_party/blink/renderer/core/frame/local_frame_view.h"
#include "third_party/blink/renderer/core/frame/page_scale_constraints_set.h"
#include "third_party/blink/renderer/core/frame/pausable_script_executor.h"
#include "third_party/blink/renderer/core/frame/performance_monitor.h"
#include "third_party/blink/renderer/core/frame/picture_in_picture_controller.h"
#include "third_party/blink/renderer/core/frame/remote_frame.h"
#include "third_party/blink/renderer/core/frame/remote_frame_owner.h"
#include "third_party/blink/renderer/core/frame/report.h"
#include "third_party/blink/renderer/core/frame/reporting_context.h"
#include "third_party/blink/renderer/core/frame/root_frame_viewport.h"
#include "third_party/blink/renderer/core/frame/settings.h"
#include "third_party/blink/renderer/core/frame/smart_clip.h"
#include "third_party/blink/renderer/core/frame/user_activation.h"
#include "third_party/blink/renderer/core/frame/virtual_keyboard_overlay_changed_observer.h"
#include "third_party/blink/renderer/core/frame/visual_viewport.h"
#include "third_party/blink/renderer/core/frame/web_frame_widget_impl.h"
#include "third_party/blink/renderer/core/frame/web_local_frame_impl.h"
#include "third_party/blink/renderer/core/fullscreen/fullscreen.h"
#include "third_party/blink/renderer/core/fullscreen/scoped_allow_fullscreen.h"
#include "third_party/blink/renderer/core/html/canvas/html_canvas_element.h"
#include "third_party/blink/renderer/core/html/html_frame_element_base.h"
#include "third_party/blink/renderer/core/html/html_image_element.h"
#include "third_party/blink/renderer/core/html/html_link_element.h"
#include "third_party/blink/renderer/core/html/html_object_element.h"
#include "third_party/blink/renderer/core/html/html_plugin_element.h"
#include "third_party/blink/renderer/core/html/media/html_audio_element.h"
#include "third_party/blink/renderer/core/html/media/html_media_element.h"
#include "third_party/blink/renderer/core/html/media/html_video_element.h"
#include "third_party/blink/renderer/core/html/plugin_document.h"
#include "third_party/blink/renderer/core/input/event_handler.h"
#include "third_party/blink/renderer/core/inspector/console_message.h"
#include "third_party/blink/renderer/core/inspector/inspector_audits_issue.h"
#include "third_party/blink/renderer/core/inspector/inspector_issue_reporter.h"
#include "third_party/blink/renderer/core/inspector/inspector_issue_storage.h"
#include "third_party/blink/renderer/core/inspector/inspector_task_runner.h"
#include "third_party/blink/renderer/core/inspector/inspector_trace_events.h"
#include "third_party/blink/renderer/core/intersection_observer/intersection_observer_controller.h"
#include "third_party/blink/renderer/core/layout/anchor_position_scroll_data.h"
#include "third_party/blink/renderer/core/layout/anchor_position_visibility_observer.h"
#include "third_party/blink/renderer/core/layout/hit_test_result.h"
#include "third_party/blink/renderer/core/layout/layout_view.h"
#include "third_party/blink/renderer/core/layout/text_autosizer.h"
#include "third_party/blink/renderer/core/lcp_critical_path_predictor/lcp_critical_path_predictor.h"
#include "third_party/blink/renderer/core/loader/document_loader.h"
#include "third_party/blink/renderer/core/loader/frame_load_request.h"
#include "third_party/blink/renderer/core/loader/idleness_detector.h"
#include "third_party/blink/renderer/core/loader/prerender_handle.h"
#include "third_party/blink/renderer/core/messaging/message_port.h"
#include "third_party/blink/renderer/core/page/chrome_client.h"
#include "third_party/blink/renderer/core/page/drag_controller.h"
#include "third_party/blink/renderer/core/page/focus_controller.h"
#include "third_party/blink/renderer/core/page/page.h"
#include "third_party/blink/renderer/core/page/page_animator.h"
#include "third_party/blink/renderer/core/page/plugin_data.h"
#include "third_party/blink/renderer/core/page/plugin_script_forbidden_scope.h"
#include "third_party/blink/renderer/core/page/pointer_lock_controller.h"
#include "third_party/blink/renderer/core/page/scrolling/scrolling_coordinator.h"
#include "third_party/blink/renderer/core/paint/object_painter.h"
#include "third_party/blink/renderer/core/paint/paint_auto_dark_mode.h"
#include "third_party/blink/renderer/core/paint/paint_layer_scrollable_area.h"
#include "third_party/blink/renderer/core/probe/core_probes.h"
#include "third_party/blink/renderer/core/script/classic_script.h"
#include "third_party/blink/renderer/core/scroll/scroll_snapshot_client.h"
#include "third_party/blink/renderer/core/svg/svg_document_extensions.h"
#include "third_party/blink/renderer/core/timing/dom_window_performance.h"
#include "third_party/blink/renderer/platform/back_forward_cache_utils.h"
#include "third_party/blink/renderer/platform/bindings/script_forbidden_scope.h"
#include "third_party/blink/renderer/platform/bindings/source_location.h"
#include "third_party/blink/renderer/platform/bindings/v8_binding.h"
#include "third_party/blink/renderer/platform/bindings/v8_histogram_accumulator.h"
#include "third_party/blink/renderer/platform/blob/blob_data.h"
#include "third_party/blink/renderer/platform/graphics/image_data_buffer.h"
#include "third_party/blink/renderer/platform/graphics/paint/drawing_recorder.h"
#include "third_party/blink/renderer/platform/graphics/paint/paint_canvas.h"
#include "third_party/blink/renderer/platform/graphics/paint/paint_controller.h"
#include "third_party/blink/renderer/platform/graphics/paint/scoped_paint_chunk_properties.h"
#include "third_party/blink/renderer/platform/heap/collection_support/heap_hash_set.h"
#include "third_party/blink/renderer/platform/heap/collection_support/heap_vector.h"
#include "third_party/blink/renderer/platform/heap/disallow_new_wrapper.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"
#include "third_party/blink/renderer/platform/heap/persistent.h"
#include "third_party/blink/renderer/platform/image-encoders/image_encoder_utils.h"
#include "third_party/blink/renderer/platform/instrumentation/instance_counters.h"
#include "third_party/blink/renderer/platform/instrumentation/resource_coordinator/document_resource_coordinator.h"
#include "third_party/blink/renderer/platform/instrumentation/tracing/trace_event.h"
#include "third_party/blink/renderer/platform/instrumentation/use_counter.h"
#include "third_party/blink/renderer/platform/json/json_values.h"
#include "third_party/blink/renderer/platform/loader/fetch/resource_fetcher.h"
#include "third_party/blink/renderer/platform/loader/fetch/resource_request.h"
#include "third_party/blink/renderer/platform/mhtml/serialized_resource.h"
#include "third_party/blink/renderer/platform/network/network_utils.h"
#include "third_party/blink/renderer/platform/runtime_enabled_features.h"
#include "third_party/blink/renderer/platform/scheduler/public/frame_scheduler.h"
#include "third_party/blink/renderer/platform/weborigin/scheme_registry.h"
#include "third_party/blink/renderer/platform/wtf/casting.h"
#include "third_party/blink/renderer/platform/wtf/std_lib_extras.h"
#include "third_party/blink/renderer/platform/wtf/vector.h"
#include "ui/gfx/geometry/point.h"
#include "ui/gfx/geometry/point_conversions.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/rect_f.h"
#include "ui/gfx/geometry/transform.h"
#if BUILDFLAG(IS_MAC)
#include "third_party/blink/renderer/core/editing/ephemeral_range.h"
#include "third_party/blink/renderer/core/editing/substring_util.h"
#include "third_party/blink/renderer/platform/fonts/mac/attributed_string_type_converter.h"
#include "ui/base/mojom/attributed_string.mojom-blink.h"
#include "ui/gfx/range/range.h"
#endif
#if !BUILDFLAG(IS_ANDROID)
#include "third_party/blink/renderer/core/frame/window_controls_overlay_changed_delegate.h"
#endif
namespace blink {
namespace {
// Max size in bytes of the Vector used in ForceSynchronousDocumentInstall to
// buffer data before sending it to the HTML parser.
constexpr unsigned kMaxDocumentChunkSize = 1000000;
// Maintain a global (statically-allocated) hash map indexed by the the result
// of hashing the |frame_token| passed on creation of a LocalFrame object.
using LocalFramesByTokenMap = HeapHashMap<uint64_t, WeakMember<LocalFrame>>;
static LocalFramesByTokenMap& GetLocalFramesMap() {
using LocalFramesByTokenMapHolder = DisallowNewWrapper<LocalFramesByTokenMap>;
DEFINE_STATIC_LOCAL(Persistent<LocalFramesByTokenMapHolder>, holder,
(MakeGarbageCollected<LocalFramesByTokenMapHolder>()));
return holder->Value();
}
// Maximum number of burst download requests allowed.
const int kBurstDownloadLimit = 10;
inline float ParentLayoutZoomFactor(LocalFrame* frame) {
auto* parent_local_frame = DynamicTo<LocalFrame>(frame->Tree().Parent());
return parent_local_frame ? parent_local_frame->LayoutZoomFactor() : 1;
}
inline float ParentTextZoomFactor(LocalFrame* frame) {
auto* parent_local_frame = DynamicTo<LocalFrame>(frame->Tree().Parent());
return parent_local_frame ? parent_local_frame->TextZoomFactor() : 1;
}
inline float ParentCssZoomFactor(LocalFrame* frame) {
auto* parent_local_frame = DynamicTo<LocalFrame>(frame->Tree().Parent());
return parent_local_frame ? parent_local_frame->CssZoomFactor() : 1;
}
// Convert a data url to a message pipe handle that corresponds to a remote
// blob, so that it can be passed across processes.
mojo::PendingRemote<mojom::blink::Blob> DataURLToBlob(const String& data_url) {
auto blob_data = std::make_unique<BlobData>();
StringUTF8Adaptor data_url_utf8(data_url);
blob_data->AppendBytes(base::as_byte_span(data_url_utf8));
scoped_refptr<BlobDataHandle> blob_data_handle =
BlobDataHandle::Create(std::move(blob_data), data_url_utf8.size());
return blob_data_handle->CloneBlobRemote();
}
RemoteFrame* SourceFrameForOptionalToken(
const std::optional<RemoteFrameToken>& source_frame_token) {
if (!source_frame_token)
return nullptr;
return RemoteFrame::FromFrameToken(source_frame_token.value());
}
void SetViewportSegmentVariablesForRect(StyleEnvironmentVariables& vars,
gfx::Rect segment_rect,
unsigned first_dimension,
unsigned second_dimension,
const ExecutionContext* context) {
vars.SetVariable(UADefinedTwoDimensionalVariable::kViewportSegmentTop,
first_dimension, second_dimension,
StyleEnvironmentVariables::FormatPx(segment_rect.y()),
context);
vars.SetVariable(UADefinedTwoDimensionalVariable::kViewportSegmentRight,
first_dimension, second_dimension,
StyleEnvironmentVariables::FormatPx(segment_rect.right()),
context);
vars.SetVariable(UADefinedTwoDimensionalVariable::kViewportSegmentBottom,
first_dimension, second_dimension,
StyleEnvironmentVariables::FormatPx(segment_rect.bottom()),
context);
vars.SetVariable(UADefinedTwoDimensionalVariable::kViewportSegmentLeft,
first_dimension, second_dimension,
StyleEnvironmentVariables::FormatPx(segment_rect.x()),
context);
vars.SetVariable(UADefinedTwoDimensionalVariable::kViewportSegmentWidth,
first_dimension, second_dimension,
StyleEnvironmentVariables::FormatPx(segment_rect.width()),
context);
vars.SetVariable(UADefinedTwoDimensionalVariable::kViewportSegmentHeight,
first_dimension, second_dimension,
StyleEnvironmentVariables::FormatPx(segment_rect.height()),
context);
}
mojom::blink::BlockingDetailsPtr CreateBlockingDetailsMojom(
const FeatureAndJSLocationBlockingBFCache& blocking_details) {
auto feature_location_to_report = mojom::blink::BlockingDetails::New();
feature_location_to_report->feature =
static_cast<uint32_t>(blocking_details.Feature());
// Zero line number and column number means no source location found.
if (blocking_details.LineNumber() > 0 &&
blocking_details.ColumnNumber() > 0) {
// `Url()` and `Function()` may return nullptr.
auto source_location = mojom::blink::ScriptSourceLocation::New(
blocking_details.Url() ? KURL(blocking_details.Url()) : KURL(),
blocking_details.Function() ? blocking_details.Function() : "",
blocking_details.LineNumber(), blocking_details.ColumnNumber());
feature_location_to_report->source = std::move(source_location);
}
return feature_location_to_report;
}
// TODO: b/338175253 - remove the need for this conversion
mojom::blink::StorageTypeAccessed ToMojoStorageType(
blink::WebContentSettingsClient::StorageType storage_type) {
switch (storage_type) {
case blink::WebContentSettingsClient::StorageType::kCacheStorage:
return mojom::blink::StorageTypeAccessed::kCacheStorage;
case blink::WebContentSettingsClient::StorageType::kIndexedDB:
return mojom::blink::StorageTypeAccessed::kIndexedDB;
case blink::WebContentSettingsClient::StorageType::kFileSystem:
return mojom::blink::StorageTypeAccessed::kFileSystem;
case blink::WebContentSettingsClient::StorageType::kWebLocks:
return mojom::blink::StorageTypeAccessed::kWebLocks;
case blink::WebContentSettingsClient::StorageType::kLocalStorage:
return mojom::blink::StorageTypeAccessed::kLocalStorage;
case blink::WebContentSettingsClient::StorageType::kSessionStorage:
return mojom::blink::StorageTypeAccessed::kSessionStorage;
}
}
} // namespace
template class CORE_TEMPLATE_EXPORT Supplement<LocalFrame>;
// static
LocalFrame* LocalFrame::FromFrameToken(const LocalFrameToken& frame_token) {
LocalFramesByTokenMap& local_frames_map = GetLocalFramesMap();
auto it = local_frames_map.find(LocalFrameToken::Hasher()(frame_token));
return it == local_frames_map.end() ? nullptr : it->value.Get();
}
void LocalFrame::Init(Frame* opener,
const DocumentToken& document_token,
std::unique_ptr<PolicyContainer> policy_container,
const StorageKey& storage_key,
ukm::SourceId document_ukm_source_id,
const KURL& creator_base_url) {
if (!policy_container)
policy_container = PolicyContainer::CreateEmpty();
CoreInitializer::GetInstance().InitLocalFrame(*this);
GetInterfaceRegistry()->AddInterface(WTF::BindRepeating(
&LocalFrame::BindTextFragmentReceiver, WrapWeakPersistent(this)));
DCHECK(!mojo_handler_);
mojo_handler_ = MakeGarbageCollected<LocalFrameMojoHandler>(*this);
SetOpenerDoNotNotify(opener);
loader_.Init(document_token, std::move(policy_container), storage_key,
document_ukm_source_id, creator_base_url);
}
void LocalFrame::SetView(LocalFrameView* view) {
DCHECK(!view_ || view_ != view);
DCHECK(!GetDocument() || !GetDocument()->IsActive());
if (view_)
view_->WillBeRemovedFromFrame();
view_ = view;
}
void LocalFrame::CreateView(const gfx::Size& viewport_size,
const Color& background_color) {
DCHECK(this);
DCHECK(GetPage());
bool is_local_root = IsLocalRoot();
if (is_local_root && View())
View()->SetParentVisible(false);
SetView(nullptr);
LocalFrameView* frame_view = nullptr;
if (is_local_root) {
frame_view = MakeGarbageCollected<LocalFrameView>(*this, viewport_size);
// The layout size is set by WebViewImpl to support meta viewport
frame_view->SetLayoutSizeFixedToFrameSize(false);
} else {
frame_view = MakeGarbageCollected<LocalFrameView>(*this);
}
SetView(frame_view);
frame_view->UpdateBaseBackgroundColorRecursively(background_color);
if (is_local_root)
frame_view->SetParentVisible(true);
// FIXME: Not clear what the right thing for OOPI is here.
if (OwnerLayoutObject()) {
HTMLFrameOwnerElement* owner = DeprecatedLocalOwner();
DCHECK(owner);
// FIXME: OOPI might lead to us temporarily lying to a frame and telling it
// that it's owned by a FrameOwner that knows nothing about it. If we're
// lying to this frame, don't let it clobber the existing
// EmbeddedContentView.
if (owner->ContentFrame() == this)
owner->SetEmbeddedContentView(frame_view);
}
if (Owner()) {
View()->SetCanHaveScrollbars(Owner()->ScrollbarMode() !=
mojom::blink::ScrollbarMode::kAlwaysOff);
}
}
LocalFrame::~LocalFrame() {
// Verify that the LocalFrameView has been cleared as part of detaching
// the frame owner.
DCHECK(!view_);
DCHECK(!frame_color_overlay_);
if (IsAdFrame())
InstanceCounters::DecrementCounter(InstanceCounters::kAdSubframeCounter);
// Before this destructor runs, `DetachImpl()` must have been run.
CHECK(did_run_detach_impl_);
}
void LocalFrame::Trace(Visitor* visitor) const {
visitor->Trace(ad_tracker_);
visitor->Trace(script_observer_);
visitor->Trace(attribution_src_loader_);
visitor->Trace(probe_sink_);
visitor->Trace(performance_monitor_);
visitor->Trace(idleness_detector_);
visitor->Trace(inspector_issue_reporter_);
visitor->Trace(inspector_trace_events_);
visitor->Trace(loader_);
visitor->Trace(view_);
visitor->Trace(dom_window_);
visitor->Trace(page_popup_owner_);
visitor->Trace(editor_);
visitor->Trace(selection_);
visitor->Trace(event_handler_);
visitor->Trace(console_);
visitor->Trace(content_capture_manager_);
visitor->Trace(system_clipboard_);
visitor->Trace(virtual_keyboard_overlay_changed_observers_);
visitor->Trace(context_menu_insets_changed_observers_);
visitor->Trace(widget_creation_observers_);
visitor->Trace(pause_handle_receivers_);
visitor->Trace(frame_color_overlay_);
visitor->Trace(mojo_handler_);
visitor->Trace(text_fragment_handler_);
visitor->Trace(scroll_snapshot_clients_);
visitor->Trace(saved_scroll_offsets_);
visitor->Trace(background_color_paint_image_generator_);
visitor->Trace(box_shadow_paint_image_generator_);
visitor->Trace(clip_path_paint_image_generator_);
visitor->Trace(lcpp_);
visitor->Trace(v8_local_compile_hints_producer_);
visitor->Trace(browser_interface_broker_proxy_);
visitor->Trace(frame_visibility_observers_);
#if !BUILDFLAG(IS_ANDROID)
visitor->Trace(window_controls_overlay_changed_delegate_);
#endif
Frame::Trace(visitor);
Supplementable<LocalFrame>::Trace(visitor);
}
bool LocalFrame::IsLocalRoot() const {
if (!Tree().Parent())
return true;
return Tree().Parent()->IsRemoteFrame();
}
void LocalFrame::Navigate(FrameLoadRequest& request,
WebFrameLoadType frame_load_type) {
if (HTMLFrameOwnerElement* element = DeprecatedLocalOwner())
element->CancelPendingLazyLoad();
if (!navigation_rate_limiter().CanProceed())
return;
TRACE_EVENT2("navigation", "LocalFrame::Navigate", "url",
request.GetResourceRequest().Url().GetString().Utf8(),
"load_type", static_cast<int>(frame_load_type));
if (request.GetClientNavigationReason() != ClientNavigationReason::kNone &&
request.GetClientNavigationReason() !=
ClientNavigationReason::kInitialFrameNavigation) {
probe::FrameScheduledNavigation(this, request.GetResourceRequest().Url(),
base::TimeDelta(),
request.GetClientNavigationReason());
}
if (NavigationShouldReplaceCurrentHistoryEntry(request, frame_load_type))
frame_load_type = WebFrameLoadType::kReplaceCurrentItem;
const ClientNavigationReason client_redirect_reason =
request.GetClientNavigationReason();
loader_.StartNavigation(request, frame_load_type);
if (client_redirect_reason != ClientNavigationReason::kNone &&
client_redirect_reason !=
ClientNavigationReason::kInitialFrameNavigation) {
probe::FrameClearedScheduledNavigation(this);
}
}
// Much of this function is redundant with the browser process
// (NavigationRequest::ShouldReplaceCurrentEntryForSameUrlNavigation), but in
// the event that this navigation is handled synchronously because it is
// same-document, we need to apply it immediately. Also, we will synchronously
// fire the NavigateEvent, which exposes whether the navigation will push or
// replace to JS.
bool LocalFrame::ShouldReplaceForSameUrlNavigation(
const FrameLoadRequest& request) {
const KURL& request_url = request.GetResourceRequest().Url();
if (request_url != GetDocument()->Url()) {
return false;
}
// Forms should push even to the same URL.
if (request.Form()) {
return false;
}
// Don't replace if the navigation originated from a cross-origin iframe (so
// that cross-origin iframes can't guess the URL of this frame based on
// whether a history entry was added).
if (request.GetOriginWindow() &&
!request.GetOriginWindow()->GetSecurityOrigin()->CanAccess(
DomWindow()->GetSecurityOrigin())) {
return false;
}
// WebUI URLs and non-current-tab navigations go through the OpenURL path
// rather than the BeginNavigation path, which converts same-URL navigations
// to reloads if not already marked replacing. Defer to the browser process
// in those cases.
if (SchemeRegistry::IsWebUIScheme(request_url.Protocol()) ||
request.GetNavigationPolicy() != kNavigationPolicyCurrentTab) {
return false;
}
return true;
}
bool LocalFrame::NavigationShouldReplaceCurrentHistoryEntry(
const FrameLoadRequest& request,
WebFrameLoadType frame_load_type) {
if (frame_load_type != WebFrameLoadType::kStandard) {
return false;
}
// When a navigation is requested via the navigation API with
// { history: "push" } specified, this should override all implicit
// conversions to a replacing navigation.
if (request.ForceHistoryPush() == mojom::blink::ForceHistoryPush::kYes) {
CHECK(!ShouldMaintainTrivialSessionHistory());
return false;
}
if (ShouldMaintainTrivialSessionHistory()) {
// TODO(http://crbug.com/1197384): We may want to assert that
// WebFrameLoadType is never kStandard in prerendered pages before
// commit. DCHECK can be in FrameLoader::CommitNavigation or somewhere
// similar.
return true;
}
// In most cases, we will treat a navigation to the current URL as replacing.
if (ShouldReplaceForSameUrlNavigation(request)) {
return true;
}
// Form submissions targeting another window should not replace.
if (request.Form() && request.GetOriginWindow() != DomWindow()) {
return false;
}
// If the load event has finished or the user initiated the navigation,
// don't replace.
if (GetDocument()->LoadEventFinished() || HasTransientUserActivation(this)) {
return false;
}
// Most non-user-initiated navigations before the load event replace. The
// exceptions are "internal" navigations (e.g., drag-and-drop triggered
// navigations), and anchor clicks.
if (request.GetClientNavigationReason() == ClientNavigationReason::kNone ||
request.GetClientNavigationReason() ==
ClientNavigationReason::kAnchorClick) {
return false;
}
return true;
}
bool LocalFrame::ShouldMaintainTrivialSessionHistory() const {
// This should be kept in sync with
// NavigationControllerImpl::ShouldMaintainTrivialSessionHistory.
return GetDocument()->IsPrerendering() || IsInFencedFrameTree();
}
bool LocalFrame::DetachImpl(FrameDetachType type) {
TRACE_EVENT1("navigation", "LocalFrame::DetachImpl", "detach_type",
static_cast<int>(type));
std::string_view histogram_suffix =
(type == FrameDetachType::kRemove) ? "Remove" : "Swap";
base::ScopedUmaHistogramTimer histogram_timer(
base::StrCat({"Navigation.LocalFrame.DetachImpl.", histogram_suffix}));
absl::Cleanup check_post_condition = [this] {
// This method must shutdown objects associated with it (such as
// the `PerformanceMonitor` for local roots).
CHECK(did_run_detach_impl_);
};
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// BEGIN REENTRANCY SAFE BLOCK
// Starting here, the code must be safe against reentrancy. Dispatching
// events, et cetera can run Javascript, which can reenter Detach().
//
// Most cleanup code should *not* be in inside the reentrancy safe block.
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
if (IsProvisional()) {
Frame* provisional_owner = GetProvisionalOwnerFrame();
// Having multiple provisional frames somehow associated with the same frame
// to potentially replace is a logic error.
DCHECK_EQ(provisional_owner->ProvisionalFrame(), this);
provisional_owner->SetProvisionalFrame(nullptr);
}
PluginScriptForbiddenScope forbid_plugin_destructor_scripting;
// In a kSwap detach, if we have a navigation going, its moved to the frame
// being swapped in, so we don't need to notify the client about the
// navigation stopping here. That will be up to the provisional frame being
// swapped in, which knows the actual state of the navigation.
loader_.StopAllLoaders(/*abort_client=*/type == FrameDetachType::kRemove);
// Don't allow any new child frames to load in this frame: attaching a new
// child frame during or after detaching children results in an attached
// frame on a detached DOM tree, which is bad.
SubframeLoadingDisabler disabler(*GetDocument());
// https://html.spec.whatwg.org/C/browsing-the-web.html#unload-a-document
// The ignore-opens-during-unload counter of a Document must be incremented
// both when unloading itself and when unloading its descendants.
IgnoreOpensDuringUnloadCountIncrementer ignore_opens_during_unload(
GetDocument());
loader_.DispatchUnloadEventAndFillOldDocumentInfoIfNeeded(
type != FrameDetachType::kRemove);
if (evict_cached_session_storage_on_freeze_or_unload_) {
// Evicts the cached data of Session Storage to avoid reusing old data in
// the cache after the session storage has been modified by another renderer
// process.
CoreInitializer::GetInstance().EvictSessionStorageCachedData(
GetDocument()->GetPage());
}
if (!Client())
return false;
if (!DetachChildren())
return false;
// Detach() needs to be called after detachChildren(), because
// detachChildren() will trigger the unload event handlers of any child
// frames, and those event handlers might start a new subresource load in this
// frame which should be stopped by Detach.
loader_.Detach();
DomWindow()->FrameDestroyed();
// Verify here that any LocalFrameView has been detached by now.
if (view_ && view_->IsAttached()) {
DCHECK(DeprecatedLocalOwner());
DCHECK(DeprecatedLocalOwner()->OwnedEmbeddedContentView());
DCHECK_EQ(view_, DeprecatedLocalOwner()->OwnedEmbeddedContentView());
}
DCHECK(!view_ || !view_->IsAttached());
// This is the earliest that scripting can be disabled:
// - FrameLoader::Detach() can fire XHR abort events
// - Document::Shutdown() can dispose plugins which can run script.
ScriptForbiddenScope forbid_script;
if (!Client())
return false;
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// END REENTRANCY SAFE BLOCK
// Past this point, no script should be executed. If this method was
// reentered, then a check for a null Client() above should have already
// returned false.
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
DCHECK(!IsDetached());
if (frame_color_overlay_)
frame_color_overlay_.Release()->Destroy();
if (IsLocalRoot()) {
performance_monitor_->Shutdown();
if (ad_tracker_)
ad_tracker_->Shutdown();
// Unregister only if this is LocalRoot because the paint_image_generator_
// was created on LocalRoot.
if (background_color_paint_image_generator_)
background_color_paint_image_generator_->Shutdown();
if (box_shadow_paint_image_generator_)
box_shadow_paint_image_generator_->Shutdown();
if (clip_path_paint_image_generator_)
clip_path_paint_image_generator_->Shutdown();
if (script_observer_) {
script_observer_->Shutdown();
}
}
idleness_detector_->Shutdown();
if (inspector_issue_reporter_)
probe_sink_->RemoveInspectorIssueReporter(inspector_issue_reporter_);
if (inspector_trace_events_)
probe_sink_->RemoveInspectorTraceEvents(inspector_trace_events_);
inspector_task_runner_->Dispose();
if (content_capture_manager_) {
content_capture_manager_->Shutdown();
content_capture_manager_ = nullptr;
}
if (text_fragment_handler_)
text_fragment_handler_->DidDetachDocumentOrFrame();
frame_visibility_observers_.clear();
not_restored_reasons_.reset();
DCHECK(!view_->IsAttached());
Client()->WillBeDetached();
// TODO(crbug.com/729196): Trace why LocalFrameView::DetachFromLayout crashes.
CHECK(!view_->IsAttached());
SetView(nullptr);
GetEventHandlerRegistry().DidRemoveAllEventHandlers(*DomWindow());
probe::FrameDetachedFromParent(this, type);
supplements_.clear();
frame_scheduler_.reset();
mojo_handler_->DidDetachFrame();
WeakIdentifierMap<LocalFrame>::NotifyObjectDestroyed(this);
LocalFramesByTokenMap& local_frames_map = GetLocalFramesMap();
auto it =
local_frames_map.find(LocalFrameToken::Hasher()(GetLocalFrameToken()));
CHECK(it != local_frames_map.end());
local_frames_map.erase(it);
did_run_detach_impl_ = true;
return true;
}
bool LocalFrame::DetachDocument() {
return Loader().DetachDocument();
}
void LocalFrame::CheckCompleted() {
GetDocument()->CheckCompleted();
}
BackgroundColorPaintImageGenerator*
LocalFrame::GetBackgroundColorPaintImageGenerator() {
LocalFrame& local_root = LocalFrameRoot();
// One background color paint worklet per root frame.
// There is no compositor thread in certain testing environment, and we
// should not composite background color animation in those cases.
if (Thread::CompositorThread() &&
!local_root.background_color_paint_image_generator_) {
local_root.background_color_paint_image_generator_ =
BackgroundColorPaintImageGenerator::Create(local_root);
}
return local_root.background_color_paint_image_generator_.Get();
}
void LocalFrame::SetBackgroundColorPaintImageGeneratorForTesting(
BackgroundColorPaintImageGenerator* generator_for_testing) {
LocalFrame& local_root = LocalFrameRoot();
local_root.background_color_paint_image_generator_ = generator_for_testing;
}
BoxShadowPaintImageGenerator* LocalFrame::GetBoxShadowPaintImageGenerator() {
// There is no compositor thread in certain testing environment, and we should
// not composite background color animation in those cases.
if (!Thread::CompositorThread())
return nullptr;
LocalFrame& local_root = LocalFrameRoot();
// One box shadow paint worklet per root frame.
if (!local_root.box_shadow_paint_image_generator_) {
local_root.box_shadow_paint_image_generator_ =
BoxShadowPaintImageGenerator::Create(local_root);
}
return local_root.box_shadow_paint_image_generator_.Get();
}
ClipPathPaintImageGenerator* LocalFrame::GetClipPathPaintImageGenerator() {
LocalFrame& local_root = LocalFrameRoot();
// One clip path paint worklet per root frame.
// TODO(kevers|clchambers): Like other native paint worklets, we should not
// have a generator in test environments that lack a compositor thread since
// we obviously can't composite the animation. Presently, some tests rely on
// the generator even in a non-threaded environment. These tests should be
// fixed.
if (!local_root.clip_path_paint_image_generator_) {
local_root.clip_path_paint_image_generator_ =
ClipPathPaintImageGenerator::Create(local_root);
}
return local_root.clip_path_paint_image_generator_.Get();
}
void LocalFrame::SetClipPathPaintImageGeneratorForTesting(
ClipPathPaintImageGenerator* generator) {
LocalFrame& local_root = LocalFrameRoot();
local_root.clip_path_paint_image_generator_ = generator;
}
LCPCriticalPathPredictor* LocalFrame::GetLCPP() {
if (!LcppEnabled()) {
return nullptr;
}
// For now, we only attach LCPP to the outermost main frames.
if (!IsOutermostMainFrame()) {
return nullptr;
}
if (!lcpp_) {
lcpp_ = MakeGarbageCollected<LCPCriticalPathPredictor>(*this);
}
return lcpp_.Get();
}
const SecurityContext* LocalFrame::GetSecurityContext() const {
return DomWindow() ? &DomWindow()->GetSecurityContext() : nullptr;
}
// Provides a string description of the Frame as either its URL or origin if
// remote.
static String FrameDescription(const Frame& frame) {
// URLs aren't available for RemoteFrames, so the error message uses their
// origin instead.
const LocalFrame* local_frame = DynamicTo<LocalFrame>(&frame);
return local_frame
? "with URL '" +
local_frame->GetDocument()->Url().GetString().GetString() +
"'"
: "with origin '" +
frame.GetSecurityContext()->GetSecurityOrigin()->ToString() +
"'";
}
void LocalFrame::PrintNavigationErrorMessage(const Frame& target_frame,
const String& reason) {
String message = "Unsafe attempt to initiate navigation for frame " +
FrameDescription(target_frame) + " from frame with URL '" +
GetDocument()->Url().GetString() + "'. " + reason + "\n";
DomWindow()->PrintErrorMessage(message);
}
void LocalFrame::PrintNavigationWarning(const String& message) {
console_->AddMessage(MakeGarbageCollected<ConsoleMessage>(
mojom::ConsoleMessageSource::kJavaScript,
mojom::ConsoleMessageLevel::kWarning, message));
}
bool LocalFrame::ShouldClose() {
// TODO(crbug.com/1407078): This should be fixed to dispatch beforeunload
// events to both local and remote frames.
return loader_.ShouldClose();
}
bool LocalFrame::DetachChildren() {
DCHECK(GetDocument());
ChildFrameDisconnector(
*GetDocument(),
ChildFrameDisconnector::DisconnectReason::kDisconnectParent)
.Disconnect();
return !!Client();
}
void LocalFrame::DidAttachDocument() {
Document* document = GetDocument();
DCHECK(document);
GetEditor().Clear();
// Clearing the event handler clears many events, but notably can ensure that
// for a drag started on an element in a frame that was moved (likely via
// appendChild()), the drag source will detach and stop firing drag events
// even after the frame reattaches.
GetEventHandler().Clear();
Selection().DidAttachDocument(document);
notified_color_scheme_ = false;
#if !BUILDFLAG(IS_ANDROID)
// For PWAs with display_override "window-controls-overlay", titlebar area
// rect bounds sent from the browser need to persist on navigation to keep the
// UI consistent. The titlebar area rect values are set in |LocalFrame| before
// the new document is attached. The css environment variables are needed to
// be set for the new document.
if (is_window_controls_overlay_visible_) {
DocumentStyleEnvironmentVariables& vars =
GetDocument()->GetStyleEngine().EnsureEnvironmentVariables();
DCHECK(!vars.ResolveVariable(
StyleEnvironmentVariables::GetVariableName(
UADefinedVariable::kTitlebarAreaX, document->GetExecutionContext()),
{}, false /* record_metrics */));
SetTitlebarAreaDocumentStyleEnvironmentVariables();
}
#endif
}
void LocalFrame::OnFirstPaint(bool text_painted, bool image_painted) {
if (notified_color_scheme_)
return;
if (text_painted || image_painted) {
// Infer the document's color scheme according to the background color, this
// approach assumes that the background won't be changed after the first
// text or image is painted, otherwise, the document will have a jarring
// flash which should be avoid by most pages.
const float l =
View()->DocumentBackgroundColor().GetLightness(Color::ColorSpace::kHSL);
GetLocalFrameHostRemote().DidInferColorScheme(
l < 0.5f ? mojom::blink::PreferredColorScheme::kDark
: mojom::blink::PreferredColorScheme::kLight);
notified_color_scheme_ = true;
}
}
void LocalFrame::OnFirstContentfulPaint() {
if (IsOutermostMainFrame()) {
GetPage()->GetChromeClient().OnFirstContentfulPaint();
}
}
bool LocalFrame::CanAccessEvent(
const WebInputEventAttribution& attribution) const {
switch (attribution.type()) {
case WebInputEventAttribution::kTargetedFrame: {
auto* frame_document = GetDocument();
if (!frame_document)
return false;
Document* target_document = nullptr;
if (auto* page = frame_document->GetPage()) {
auto& pointer_lock_controller = page->GetPointerLockController();
if (auto* element = pointer_lock_controller.GetElement()) {
// If a pointer lock is held, we can expect all events to be
// dispatched to the frame containing the locked element.
target_document = &element->GetDocument();
} else if (cc::ElementId element_id = attribution.target_frame_id()) {
DOMNodeId target_document_id =
DOMNodeIdFromCompositorElementId(element_id);
target_document =
DynamicTo<Document>(DOMNodeIds::NodeForId(target_document_id));
}
}
if (!target_document || !target_document->domWindow())
return false;
return GetSecurityContext()->GetSecurityOrigin()->CanAccess(
target_document->domWindow()->GetSecurityOrigin());
}
case WebInputEventAttribution::kFocusedFrame:
return GetPage() &&
GetPage()->GetFocusController().FocusedFrame() == this;
case WebInputEventAttribution::kUnknown:
return false;
}
}
void LocalFrame::Reload(WebFrameLoadType load_type) {
DCHECK(IsReloadLoadType(load_type));
if (!loader_.GetDocumentLoader()->GetHistoryItem())
return;
TRACE_EVENT1("navigation", "LocalFrame::Reload", "load_type",
static_cast<int>(load_type));
FrameLoadRequest request(
DomWindow(), loader_.ResourceRequestForReload(
load_type, ClientRedirectPolicy::kClientRedirect));
request.SetClientNavigationReason(ClientNavigationReason::kReload);
probe::FrameScheduledNavigation(this, request.GetResourceRequest().Url(),
base::TimeDelta(),
ClientNavigationReason::kReload);
loader_.StartNavigation(request, load_type);
probe::FrameClearedScheduledNavigation(this);
}
LocalWindowProxy* LocalFrame::WindowProxy(DOMWrapperWorld& world) {
return To<LocalWindowProxy>(Frame::GetWindowProxy(world));
}
LocalWindowProxy* LocalFrame::WindowProxyMaybeUninitialized(
DOMWrapperWorld& world) {
return To<LocalWindowProxy>(Frame::GetWindowProxyMaybeUninitialized(world));
}
LocalDOMWindow* LocalFrame::DomWindow() {
return To<LocalDOMWindow>(dom_window_.Get());
}
const LocalDOMWindow* LocalFrame::DomWindow() const {
return To<LocalDOMWindow>(dom_window_.Get());
}
void LocalFrame::SetDOMWindow(LocalDOMWindow* dom_window) {
DCHECK(dom_window);
if (DomWindow()) {
DomWindow()->Reset();
// SystemClipboard uses HeapMojo wrappers. HeapMojo
// wrappers uses LocalDOMWindow (ExecutionContext) to reset the mojo
// objects when the ExecutionContext was destroyed. So when new
// LocalDOMWindow was set, we need to create new SystemClipboard.
system_clipboard_ = nullptr;
}
GetWindowProxyManager()->ClearForNavigation();
dom_window_ = dom_window;
dom_window->Initialize();
GetFrameScheduler()->SetAgentClusterId(GetAgentClusterId());
}
Document* LocalFrame::GetDocument() const {
return DomWindow() ? DomWindow()->document() : nullptr;
}
void LocalFrame::DocumentDetached() {
// Resets WebLinkPreviewTrigerer when the document detached as
// WebLinkPreviewInitiator depends on document.
is_link_preivew_triggerer_initialized_ = false;
link_preview_triggerer_.reset();
if (LocalFrameView* view = View()) {
// Pagination layout may hold on to layout objects that are not part of the
// Document's DOM. Destroy them now.
view->DestroyPaginationLayout();
}
}
void LocalFrame::SetPagePopupOwner(Element& owner) {
page_popup_owner_ = &owner;
}
LayoutView* LocalFrame::ContentLayoutObject() const {
return GetDocument() ? GetDocument()->GetLayoutView() : nullptr;
}
void LocalFrame::DidChangeVisibilityState() {
if (GetDocument())
GetDocument()->DidChangeVisibilityState();
Frame::DidChangeVisibilityState();
}
void LocalFrame::AddWidgetCreationObserver(WidgetCreationObserver* observer) {
CHECK(IsLocalRoot());
CHECK(!GetWidgetForLocalRoot());
widget_creation_observers_.insert(observer);
}
void LocalFrame::NotifyFrameWidgetCreated() {
CHECK(IsLocalRoot());
CHECK(GetWidgetForLocalRoot());
// No need to copy `widget_creation_observers_` since we don't permit adding
// new observers after this point.
for (WidgetCreationObserver* observer : widget_creation_observers_) {
observer->OnLocalRootWidgetCreated();
}
widget_creation_observers_.clear();
}
bool LocalFrame::IsCaretBrowsingEnabled() const {
return GetSettings() && GetSettings()->GetCaretBrowsingEnabled();
}
void LocalFrame::HookBackForwardCacheEviction() {
TRACE_EVENT0("blink", "LocalFrame::HookBackForwardCacheEviction");
// Register a callback dispatched when JavaScript is executed on the frame.
// The callback evicts the frame. If a frame is frozen by BackForwardCache,
// the frame must not be mutated e.g., by JavaScript execution, then the
// frame must be evicted in such cases.
DCHECK(RuntimeEnabledFeatures::BackForwardCacheEnabled());
static_cast<LocalWindowProxyManager*>(GetWindowProxyManager())
->SetAbortScriptExecution(
[](v8::Isolate* isolate, v8::Local<v8::Context> context) {
ScriptState* script_state = ScriptState::From(isolate, context);
LocalDOMWindow* window = LocalDOMWindow::From(script_state);
DCHECK(window);
LocalFrame* frame = window->GetFrame();
if (frame) {
std::unique_ptr<SourceLocation> source_location = nullptr;
if (base::FeatureList::IsEnabled(
features::kCaptureJSExecutionLocation)) {
// Capture the source location of the JS execution if the flag
// is enabled.
source_location = CaptureSourceLocation();
}
frame->EvictFromBackForwardCache(
mojom::blink::RendererEvictionReason::kJavaScriptExecution,
std::move(source_location));
if (base::FeatureList::IsEnabled(
features::kBackForwardCacheDWCOnJavaScriptExecution)) {
// Adding |DumpWithoutCrashing()| here to make sure this is not
// happening in any tests, except for when this is expected.
base::debug::DumpWithoutCrashing();
}
}
});
}
void LocalFrame::RemoveBackForwardCacheEviction() {
TRACE_EVENT0("blink", "LocalFrame::RemoveBackForwardCacheEviction");
DCHECK(RuntimeEnabledFeatures::BackForwardCacheEnabled());
static_cast<LocalWindowProxyManager*>(GetWindowProxyManager())
->SetAbortScriptExecution(nullptr);
// The page is being restored, and from this point eviction should not happen
// for any reason. Change the deferring state from |kBufferIncoming| to
// |kStrict| so that network related eviction cannot happen.
GetDocument()->Fetcher()->SetDefersLoading(LoaderFreezeMode::kStrict);
}
void LocalFrame::SetTextDirection(base::i18n::TextDirection direction) {
// The Editor::SetBaseWritingDirection() function checks if we can change
// the text direction of the selected node and updates its DOM "dir"
// attribute and its CSS "direction" property.
// So, we just call the function as Safari does.
Editor& editor = GetEditor();
if (!editor.CanEdit())
return;
switch (direction) {
case base::i18n::TextDirection::UNKNOWN_DIRECTION:
editor.SetBaseWritingDirection(
mojo_base::mojom::blink::TextDirection::UNKNOWN_DIRECTION);
break;
case base::i18n::TextDirection::LEFT_TO_RIGHT:
editor.SetBaseWritingDirection(
mojo_base::mojom::blink::TextDirection::LEFT_TO_RIGHT);
break;
case base::i18n::TextDirection::RIGHT_TO_LEFT:
editor.SetBaseWritingDirection(
mojo_base::mojom::blink::TextDirection::RIGHT_TO_LEFT);
break;
default:
NOTIMPLEMENTED();
break;
}
}
void LocalFrame::SetIsInert(bool inert) {
if (is_inert_ == inert)
return;
is_inert_ = inert;
// Propagate inert to child frames
for (Frame* child = Tree().FirstChild(); child;
child = child->Tree().NextSibling()) {
child->UpdateInertIfPossible();
}
// Nodes all over the accessibility tree can change inertness which means they
// must be added or removed from the tree.
if (GetDocument()) {
GetDocument()->RefreshAccessibilityTree();
}
}
void LocalFrame::SetInheritedEffectiveTouchAction(TouchAction touch_action) {
if (inherited_effective_touch_action_ == touch_action)
return;
inherited_effective_touch_action_ = touch_action;
GetDocument()->GetStyleEngine().MarkAllElementsForStyleRecalc(
StyleChangeReasonForTracing::Create(
style_change_reason::kInheritedStyleChangeFromParentFrame));
}
bool LocalFrame::BubbleLogicalScrollInParentFrame(
mojom::blink::ScrollDirection direction,
ui::ScrollGranularity granularity) {
bool is_embedded_main_frame = IsMainFrame() && !IsOutermostMainFrame();
if (is_embedded_main_frame || IsA<RemoteFrame>(Parent())) {
GetLocalFrameHostRemote().BubbleLogicalScrollInParentFrame(direction,
granularity);
return false;
} else if (auto* local_parent = DynamicTo<LocalFrame>(Parent())) {
return local_parent->BubbleLogicalScrollFromChildFrame(direction,
granularity, this);
}
DCHECK(IsOutermostMainFrame());
return false;
}
bool LocalFrame::BubbleLogicalScrollFromChildFrame(
mojom::blink::ScrollDirection direction,
ui::ScrollGranularity granularity,
Frame* child) {
FrameOwner* owner = child->Owner();
auto* owner_element = DynamicTo<HTMLFrameOwnerElement>(owner);
DCHECK(owner_element);
return GetEventHandler().BubblingScroll(direction, granularity,
owner_element);
}
mojom::blink::SuddenTerminationDisablerType
SuddenTerminationDisablerTypeForEventType(const AtomicString& event_type) {
if (event_type == event_type_names::kUnload) {
return mojom::blink::SuddenTerminationDisablerType::kUnloadHandler;
}
if (event_type == event_type_names::kBeforeunload) {
return mojom::blink::SuddenTerminationDisablerType::kBeforeUnloadHandler;
}
if (event_type == event_type_names::kPagehide) {
return mojom::blink::SuddenTerminationDisablerType::kPageHideHandler;
}
if (event_type == event_type_names::kVisibilitychange) {
return mojom::blink::SuddenTerminationDisablerType::
kVisibilityChangeHandler;
}
NOTREACHED();
}
int NumberOfSuddenTerminationEventListeners(const EventTarget& event_target,
const AtomicString& event_type) {
if (event_type != event_type_names::kVisibilitychange)
return event_target.NumberOfEventListeners(event_type);
// For visibilitychange, we need to count the number of event listeners that
// are registered on the document and the window, as the event is initially
// dispatched on the document but might bubble up to the window.
// The other events (beforeunload, unload, pagehide) are dispatched on the
// window and won't bubble up anywhere, so we don't need to check for
// listeners the document for those events.
int total_listeners_count = event_target.NumberOfEventListeners(event_type);
if (auto* dom_window = event_target.ToLocalDOMWindow()) {
// |event_target| is the window, so get the count for listeners registered
// on the document.
total_listeners_count +=
dom_window->document()->NumberOfEventListeners(event_type);
} else {
auto* node = const_cast<EventTarget*>(&event_target)->ToNode();
DCHECK(node);
DCHECK(node->IsDocumentNode());
DCHECK(node->GetDocument().domWindow());
// |event_target| is the document, so get the count for listeners registered
// on the window.
total_listeners_count +=
node->GetDocument().domWindow()->NumberOfEventListeners(event_type);
}
return total_listeners_count;
}
void LocalFrame::UpdateSuddenTerminationStatus(
bool added_listener,
mojom::blink::SuddenTerminationDisablerType disabler_type) {
Platform::Current()->SuddenTerminationChanged(!added_listener);
if (features::IsUnloadBlocklisted()) {
// Block BFCache for using the unload handler. Originally unload handler was
// not a blocklisted feature, but we make them blocklisted so the source
// location will be captured. See https://crbug.com/1513120 for details.
if (disabler_type ==
mojom::blink::SuddenTerminationDisablerType::kUnloadHandler) {
if (added_listener) {
if (feature_handle_for_scheduler_) {
return;
}
feature_handle_for_scheduler_ = GetFrameScheduler()->RegisterFeature(
SchedulingPolicy::Feature::kUnloadHandler,
{SchedulingPolicy::DisableBackForwardCache()});
} else {
feature_handle_for_scheduler_.reset();
}
}
}
GetLocalFrameHostRemote().SuddenTerminationDisablerChanged(added_listener,
disabler_type);
}
void LocalFrame::AddedSuddenTerminationDisablerListener(
const EventTarget& event_target,
const AtomicString& event_type) {
if (NumberOfSuddenTerminationEventListeners(event_target, event_type) == 1) {
// The first handler of this type was added.
UpdateSuddenTerminationStatus(
true, SuddenTerminationDisablerTypeForEventType(event_type));
}
}
void LocalFrame::RemovedSuddenTerminationDisablerListener(
const EventTarget& event_target,
const AtomicString& event_type) {
if (NumberOfSuddenTerminationEventListeners(event_target, event_type) == 0) {
// The last handler of this type was removed.
UpdateSuddenTerminationStatus(
false, SuddenTerminationDisablerTypeForEventType(event_type));
}
}
void LocalFrame::DidFocus() {
GetLocalFrameHostRemote().DidFocusFrame();
}
void LocalFrame::DidChangeThemeColor(bool update_theme_color_cache) {
if (Tree().Parent())
return;
if (update_theme_color_cache)
GetDocument()->UpdateThemeColorCache();
std::optional<Color> color = GetDocument()->ThemeColor();
std::optional<SkColor> sk_color;
if (color)
sk_color = color->Rgb();
GetLocalFrameHostRemote().DidChangeThemeColor(sk_color);
}
void LocalFrame::DidChangeBackgroundColor(SkColor4f background_color,
bool color_adjust) {
DCHECK(!Tree().Parent());
GetLocalFrameHostRemote().DidChangeBackgroundColor(background_color,
color_adjust);
}
LocalFrame& LocalFrame::LocalFrameRoot() const {
const LocalFrame* cur_frame = this;
while (cur_frame && IsA<LocalFrame>(cur_frame->Parent()))
cur_frame = To<LocalFrame>(cur_frame->Parent());
return const_cast<LocalFrame&>(*cur_frame);
}
scoped_refptr<InspectorTaskRunner> LocalFrame::GetInspectorTaskRunner() {
return inspector_task_runner_;
}
void LocalFrame::StartPrinting(const WebPrintParams& print_params,
float maximum_shrink_ratio) {
DCHECK(!saved_scroll_offsets_);
print_params_ = print_params;
if (!print_params_.use_paginated_layout) {
// Not laying out for pagination (e.g. this is a subframe, or a special
// headers/footers document, which is generated once per page). Just set the
// initial containing block to the default page size from print parameters.
if (LayoutView* layout_view = View()->GetLayoutView()) {
auto size = PhysicalSize::FromSizeFRound(
print_params_.default_page_description.size);
layout_view->SetInitialContainingBlockSizeForPrinting(size);
}
}
SetPrinting(true, maximum_shrink_ratio);
}
void LocalFrame::StartPrintingSubLocalFrame() {
gfx::SizeF page_size;
// This is a subframe. Use the non-printing layout size as "pagination" size.
if (const LayoutView* layout_view = View()->GetLayoutView()) {
page_size =
gfx::SizeF(layout_view->GetNonPrintingLayoutSize(kIncludeScrollbars));
}
WebPrintParams print_params(page_size);
// Only the root frame is paginated.
print_params.use_paginated_layout = false;
StartPrinting(print_params);
}
void LocalFrame::EndPrinting() {
RestoreScrollOffsets();
SetPrinting(false, 0);
}
void LocalFrame::SetPrinting(bool printing, float maximum_shrink_ratio) {
// In setting printing, we should not validate resources already cached for
// the document. See https://bugs.webkit.org/show_bug.cgi?id=43704
ResourceCacheValidationSuppressor validation_suppressor(
GetDocument()->Fetcher());
GetDocument()->SetPrinting(printing ? Document::kPrinting
: Document::kFinishingPrinting);
View()->AdjustMediaTypeForPrinting(printing);
if (TextAutosizer* text_autosizer = GetDocument()->GetTextAutosizer())
text_autosizer->UpdatePageInfo();
if (ShouldUsePaginatedLayout()) {
View()->ForceLayoutForPagination(maximum_shrink_ratio);
} else {
if (LayoutView* layout_view = View()->GetLayoutView()) {
layout_view->SetIntrinsicLogicalWidthsDirty();
layout_view->SetNeedsLayout(layout_invalidation_reason::kPrintingChanged);
layout_view->InvalidatePaintForViewAndDescendants();
}
GetDocument()->UpdateStyleAndLayout(DocumentUpdateReason::kPrinting);
View()->AdjustViewSize();
View()->DestroyPaginationLayout();
}
// Subframes of the one we're printing don't lay out to the page size.
for (Frame* child = Tree().FirstChild(); child;
child = child->Tree().NextSibling()) {
if (auto* child_local_frame = DynamicTo<LocalFrame>(child)) {
if (printing) {
child_local_frame->StartPrintingSubLocalFrame();
} else {
child_local_frame->EndPrinting();
}
}
}
if (auto* layout_view = View()->GetLayoutView()) {
layout_view->AddSubtreePaintPropertyUpdateReason(
SubtreePaintPropertyUpdateReason::kPrinting);
}
if (!printing)
GetDocument()->SetPrinting(Document::kNotPrinting);
}
bool LocalFrame::ShouldUsePaginatedLayout() const {
if (!GetDocument()->Printing())
return false;
// Only the top frame being printed may be fitted to page size.
// Subframes should be constrained by parents only.
// This function considers the following two kinds of frames as top frames:
// -- frame with no parent;
// -- frame's parent is not in printing mode.
// For the second type, it is a bit complicated when its parent is a remote
// frame. In such case, we can not check its document or other internal
// status. However, if the parent is in printing mode, this frame's printing
// must have started with |use_paginated_layout| as false in print context.
if (auto* local_parent = DynamicTo<LocalFrame>(Tree().Parent())) {
return !local_parent->GetDocument()->Printing();
}
return print_params_.use_paginated_layout;
}
void LocalFrame::StartPaintPreview() {
SetInvalidationForCapture(true);
}
void LocalFrame::EndPaintPreview() {
SetInvalidationForCapture(false);
}
void LocalFrame::SetInvalidationForCapture(bool capturing) {
if (!capturing)
RestoreScrollOffsets();
ResourceCacheValidationSuppressor validation_suppressor(
GetDocument()->Fetcher());
// Subframes of the captured content should be updated.
for (Frame* child = Tree().FirstChild(); child;
child = child->Tree().NextSibling()) {
if (auto* child_local_frame = DynamicTo<LocalFrame>(child)) {
child_local_frame->SetInvalidationForCapture(capturing);
}
}
auto* layout_view = View()->GetLayoutView();
if (!layout_view) {
return;
}
// Trigger a paint property update to ensure the unclipped behavior is
// applied to the frame level scroller.
layout_view->SetNeedsPaintPropertyUpdate();
if (!GetDocument()->AreScrollbarsAllowedInPaintPreview()) {
// During CapturePaintPreview, the LayoutView thinks it should not have
// scrollbars. So if scrollbars affect layout, we should force relayout
// when entering and exiting paint preview.
layout_view->SetNeedsLayout(layout_invalidation_reason::kPaintPreview);
}
}
void LocalFrame::EnsureSaveScrollOffset(Node& node) {
const auto* scrollable_area = PaintLayerScrollableArea::FromNode(node);
if (!scrollable_area)
return;
if (!saved_scroll_offsets_)
saved_scroll_offsets_ = MakeGarbageCollected<SavedScrollOffsets>();
// Retain the first scroll offset saved for each scrollable area.
if (!saved_scroll_offsets_->Contains(&node))
saved_scroll_offsets_->Set(&node, scrollable_area->GetScrollOffset());
}
void LocalFrame::RestoreScrollOffsets() {
if (!saved_scroll_offsets_)
return;
// Restore scroll offsets unconditionally (i.e. without clamping) in case
// layout or view sizes haven't been updated yet.
for (auto& entry : *saved_scroll_offsets_) {
auto* scrollable_area = PaintLayerScrollableArea::FromNode(*entry.key);
if (!scrollable_area)
continue;
scrollable_area->SetScrollOffsetUnconditionally(
entry.value, mojom::blink::ScrollType::kProgrammatic);
}
saved_scroll_offsets_ = nullptr;
}
void LocalFrame::SetLayoutZoomFactor(float factor) {
SetZoomFactors(factor, text_zoom_factor_, css_zoom_factor_);
}
void LocalFrame::SetTextZoomFactor(float factor) {
SetZoomFactors(layout_zoom_factor_, factor, css_zoom_factor_);
}
void LocalFrame::SetCssZoomFactor(float factor) {
SetZoomFactors(layout_zoom_factor_, text_zoom_factor_, factor);
}
void LocalFrame::SetZoomFactors(float layout_zoom_factor,
float text_zoom_factor,
float css_zoom_factor) {
if (layout_zoom_factor_ == layout_zoom_factor &&
text_zoom_factor_ == text_zoom_factor &&
css_zoom_factor_ == css_zoom_factor) {
return;
}
Page* page = GetPage();
if (!page)
return;
Document* document = GetDocument();
if (!document)
return;
// Respect SVGs zoomAndPan="disabled" property in standalone SVG documents.
// FIXME: How to handle compound documents + zoomAndPan="disabled"? Needs SVG
// WG clarification.
if (document->IsSVGDocument()) {
if (!document->AccessSVGExtensions().ZoomAndPanEnabled())
return;
}
bool layout_zoom_changed = (layout_zoom_factor != layout_zoom_factor_);
layout_zoom_factor_ = layout_zoom_factor;
text_zoom_factor_ = text_zoom_factor;
css_zoom_factor_ = css_zoom_factor;
if (!GetDocument()->StandardizedBrowserZoomEnabled()) {
// Zoom factor will not be propagated via style resolution, it must be
// propagated here.
for (Frame* child = Tree().FirstChild(); child;
child = child->Tree().NextSibling()) {
if (auto* child_local_frame = DynamicTo<LocalFrame>(child)) {
child_local_frame->SetZoomFactors(layout_zoom_factor_,
text_zoom_factor_, css_zoom_factor_);
} else {
DynamicTo<RemoteFrame>(child)->ZoomFactorChanged(layout_zoom_factor);
}
}
}
if (layout_zoom_changed) {
#if !BUILDFLAG(IS_ANDROID)
MaybeUpdateWindowControlsOverlayWithNewZoomLevel();
#endif
document->LayoutViewportWasResized();
document->MediaQueryAffectingValueChanged(MediaValueChange::kOther);
}
document->GetStyleEngine().MarkViewportStyleDirty();
document->GetStyleEngine().MarkAllElementsForStyleRecalc(
StyleChangeReasonForTracing::Create(style_change_reason::kZoom));
if (View())
View()->SetNeedsLayout();
}
void LocalFrame::MediaQueryAffectingValueChangedForLocalSubtree(
MediaValueChange value) {
GetDocument()->MediaQueryAffectingValueChanged(value);
for (Frame* child = Tree().FirstChild(); child;
child = child->Tree().NextSibling()) {
if (auto* child_local_frame = DynamicTo<LocalFrame>(child))
child_local_frame->MediaQueryAffectingValueChangedForLocalSubtree(value);
}
}
void LocalFrame::ViewportSegmentsChanged(
const std::vector<gfx::Rect>& viewport_segments) {
if (!RuntimeEnabledFeatures::ViewportSegmentsEnabled(
GetDocument()->GetExecutionContext())) {
return;
}
DCHECK(IsLocalRoot());
// A change in the viewport segments requires re-evaluation of media queries
// for the local frame subtree (the segments affect the
// "horizontal-viewport-segments" and "vertical-viewport-segments" features).
MediaQueryAffectingValueChangedForLocalSubtree(MediaValueChange::kOther);
// Fullscreen element has its own document and uses the viewport media queries,
// so we need to make sure the media queries are re-evaluated.
if (Element* fullscreen = Fullscreen::FullscreenElementFrom(*GetDocument())) {
GetDocument()->GetStyleEngine().MarkAllElementsForStyleRecalc(
StyleChangeReasonForTracing::Create(style_change_reason::kFullscreen));
CSSDefaultStyleSheets::Instance()
.RebuildFullscreenRuleSetIfMediaQueriesChanged(*fullscreen);
}
// Also need to update the environment variables related to viewport segments.
UpdateViewportSegmentCSSEnvironmentVariables(viewport_segments);
}
void LocalFrame::UpdateViewportSegmentCSSEnvironmentVariables(
const std::vector<gfx::Rect>& viewport_segments) {
DCHECK(RuntimeEnabledFeatures::ViewportSegmentsEnabled(
GetDocument()->GetExecutionContext()));
// Update the variable values on the root instance so that documents that
// are created after the values change automatically have the right values.
UpdateViewportSegmentCSSEnvironmentVariables(
StyleEnvironmentVariables::GetRootInstance(), viewport_segments);
if (Element* fullscreen = Fullscreen::FullscreenElementFrom(*GetDocument())) {
// Fullscreen has its own document so we need to update its variables as
// well.
UpdateViewportSegmentCSSEnvironmentVariables(
fullscreen->GetDocument().GetStyleEngine().EnsureEnvironmentVariables(),
viewport_segments);
}
}
void LocalFrame::UpdateViewportSegmentCSSEnvironmentVariables(
StyleEnvironmentVariables& vars,
const std::vector<gfx::Rect>& viewport_segments) {
// Unset all variables, since they will be set as a whole by the code below.
// Since the number and configurations of the segments can change, and
// removing variables clears all values that have previously been set,
// we will recalculate all the values on each change.
const UADefinedTwoDimensionalVariable vars_to_remove[] = {
UADefinedTwoDimensionalVariable::kViewportSegmentTop,
UADefinedTwoDimensionalVariable::kViewportSegmentRight,
UADefinedTwoDimensionalVariable::kViewportSegmentBottom,
UADefinedTwoDimensionalVariable::kViewportSegmentLeft,
UADefinedTwoDimensionalVariable::kViewportSegmentWidth,
UADefinedTwoDimensionalVariable::kViewportSegmentHeight,
};
ExecutionContext* context = GetDocument()->GetExecutionContext();
for (auto var : vars_to_remove) {
vars.RemoveVariable(var, context);
}
// Per [css-env-1], only set the segment variables if there is more than one.
if (viewport_segments.size() >= 2) {
// Iterate the segments in row-major order, setting the segment variables
// based on x and y index.
int current_y_position = viewport_segments[0].y();
unsigned x_index = 0;
unsigned y_index = 0;
SetViewportSegmentVariablesForRect(vars, viewport_segments[0], x_index,
y_index, context);
for (size_t i = 1; i < viewport_segments.size(); i++) {
if (viewport_segments[i].y() == current_y_position) {
x_index++;
SetViewportSegmentVariablesForRect(vars, viewport_segments[i], x_index,
y_index, context);
} else {
// If there is a different y value, this is the next row so increase
// y index and start again from 0 for x.
y_index++;
x_index = 0;
current_y_position = viewport_segments[i].y();
SetViewportSegmentVariablesForRect(vars, viewport_segments[i], x_index,
y_index, context);
}
}
}
}
void LocalFrame::OverrideDevicePostureForEmulation(
mojom::blink::DevicePostureType device_posture_param) {
mojo_handler_->OverrideDevicePostureForEmulation(device_posture_param);
}
void LocalFrame::DisableDevicePostureOverrideForEmulation() {
mojo_handler_->DisableDevicePostureOverrideForEmulation();
}
mojom::blink::DevicePostureType LocalFrame::GetDevicePosture() {
return mojo_handler_->GetDevicePosture();
}
double LocalFrame::DevicePixelRatio() const {
if (!page_)
return 0;
double ratio = page_->InspectorDeviceScaleFactorOverride();
ratio *= LayoutZoomFactor();
return ratio;
}
String LocalFrame::SelectedText() const {
return Selection().SelectedText();
}
String LocalFrame::SelectedText(const TextIteratorBehavior& behavior) const {
return Selection().SelectedText(behavior);
}
String LocalFrame::SelectedTextForClipboard() const {
if (!GetDocument())
return g_empty_string;
DCHECK(!GetDocument()->NeedsLayoutTreeUpdate());
return Selection().SelectedTextForClipboard();
}
void LocalFrame::TextSelectionChanged(const WTF::String& selection_text,
uint32_t offset,
const gfx::Range& range) const {
GetLocalFrameHostRemote().TextSelectionChanged(selection_text, offset, range);
}
PositionWithAffinity LocalFrame::PositionForPoint(
const PhysicalOffset& frame_point) {
HitTestLocation location(frame_point);
HitTestResult result = GetEventHandler().HitTestResultAtLocation(location);
return result.GetPositionForInnerNodeOrImageMapImage();
}
Document* LocalFrame::DocumentAtPoint(
const PhysicalOffset& point_in_root_frame) {
if (!View())
return nullptr;
HitTestLocation location(View()->ConvertFromRootFrame(point_in_root_frame));
if (!ContentLayoutObject())
return nullptr;
HitTestResult result = GetEventHandler().HitTestResultAtLocation(
location, HitTestRequest::kReadOnly | HitTestRequest::kActive);
return result.InnerNode() ? &result.InnerNode()->GetDocument() : nullptr;
}
void LocalFrame::RemoveSpellingMarkersUnderWords(const Vector<String>& words) {
GetSpellChecker().RemoveSpellingMarkersUnderWords(words);
}
String LocalFrame::GetLayerTreeAsTextForTesting(unsigned flags) const {
if (!ContentLayoutObject())
return String();
std::unique_ptr<JSONObject> layers;
if (!(flags & kOutputAsLayerTree)) {
layers = View()->CompositedLayersAsJSON(static_cast<LayerTreeFlags>(flags));
}
return layers ? layers->ToPrettyJSONString() : String();
}
bool LocalFrame::ShouldThrottleRendering() const {
return View() && View()->ShouldThrottleRendering();
}
LocalFrame::LocalFrame(
LocalFrameClient* client,
Page& page,
FrameOwner* owner,
Frame* parent,
Frame* previous_sibling,
FrameInsertType insert_type,
const LocalFrameToken& frame_token,
WindowAgentFactory* inheriting_agent_factory,
InterfaceRegistry* interface_registry,
mojo::PendingRemote<mojom::blink::BrowserInterfaceBroker> interface_broker,
const base::TickClock* clock)
: Frame(client,
page,
owner,
parent,
previous_sibling,
insert_type,
frame_token,
client->GetDevToolsFrameToken(),
MakeGarbageCollected<LocalWindowProxyManager>(
page.GetAgentGroupScheduler().Isolate(),
*this),
inheriting_agent_factory),
frame_scheduler_(page.GetPageScheduler()->CreateFrameScheduler(
this,
IsInFencedFrameTree(),
IsMainFrame() ? FrameScheduler::FrameType::kMainFrame
: FrameScheduler::FrameType::kSubframe)),
loader_(this),
editor_(MakeGarbageCollected<Editor>(*this)),
selection_(MakeGarbageCollected<FrameSelection>(*this)),
event_handler_(MakeGarbageCollected<EventHandler>(*this)),
console_(MakeGarbageCollected<FrameConsole>(*this)),
navigation_disable_count_(0),
in_view_source_mode_(false),
frozen_(false),
paused_(false),
hidden_(false),
layout_zoom_factor_(ParentLayoutZoomFactor(this)),
text_zoom_factor_(ParentTextZoomFactor(this)),
css_zoom_factor_(ParentCssZoomFactor(this)),
inspector_task_runner_(InspectorTaskRunner::Create(
GetTaskRunner(TaskType::kInternalInspector))),
interface_registry_(interface_registry
? interface_registry
: InterfaceRegistry::GetEmptyInterfaceRegistry()),
v8_local_compile_hints_producer_(
MakeGarbageCollected<v8_compile_hints::V8LocalCompileHintsProducer>(
this)),
// TODO(https://crbug.com/352165586): Give non-null context to the proxy.
browser_interface_broker_proxy_(nullptr /* No LocalDOMWindow yet... */) {
TRACE_EVENT("navigation", "LocalFrame");
auto frame_tracking_result =
GetLocalFramesMap().insert(FrameToken::Hasher()(GetFrameToken()), this);
CHECK(frame_tracking_result.stored_value) << "Inserting a duplicate item.";
v8::Isolate* isolate = page.GetAgentGroupScheduler().Isolate();
if (interface_broker.is_valid()) { // This may be invalid in unit tests.
browser_interface_broker_proxy_.Bind(
std::move(interface_broker),
page.GetAgentGroupScheduler().DefaultTaskRunner());
}
// There is generally one probe sink per local frame tree, so for root frames
// we create a new child sink and for child frames we propagate one from root.
// However, if local frame swap is performed, we don't want both frames to be
// active at once, so a dummy probe sink is created for provisional frame and
// swapped for that of the frame being swapped on in `SwapIn()`. Since we can
// only know whether the frame is provisional upon `Initialize()` call which
// does a lot of things that may potentially lead to instrumentation calls,
// we set provisional probe sink unconditionally here, then possibly replace
// it with that of the local root after `Initialize()`.
probe_sink_ = MakeGarbageCollected<CoreProbeSink>();
if (IsLocalRoot()) {
performance_monitor_ =
MakeGarbageCollected<PerformanceMonitor>(this, isolate);
inspector_issue_reporter_ = MakeGarbageCollected<InspectorIssueReporter>(
&page.GetInspectorIssueStorage());
probe_sink_->AddInspectorIssueReporter(inspector_issue_reporter_);
inspector_trace_events_ = MakeGarbageCollected<InspectorTraceEvents>();
probe_sink_->AddInspectorTraceEvents(inspector_trace_events_);
if (RuntimeEnabledFeatures::AdTaggingEnabled()) {
ad_tracker_ = MakeGarbageCollected<AdTracker>(this);
}
if (blink::LcppScriptObserverEnabled()) {
script_observer_ = MakeGarbageCollected<LCPScriptObserver>(this);
}
} else {
// Inertness only needs to be updated if this frame might inherit the
// inert state from a higher-level frame. If this is an OOPIF local root,
// it will be updated later.
UpdateInertIfPossible();
UpdateInheritedEffectiveTouchActionIfPossible();
ad_tracker_ = LocalFrameRoot().ad_tracker_;
performance_monitor_ = LocalFrameRoot().performance_monitor_;
script_observer_ = LocalFrameRoot().script_observer_;
}
idleness_detector_ = MakeGarbageCollected<IdlenessDetector>(this, clock);
attribution_src_loader_ = MakeGarbageCollected<AttributionSrcLoader>(this);
inspector_task_runner_->InitIsolate(isolate);
if (IsOutermostMainFrame()) {
intersection_state_.occlusion_state =
mojom::blink::FrameOcclusionState::kGuaranteedNotOccluded;
}
DCHECK(ad_tracker_ ? RuntimeEnabledFeatures::AdTaggingEnabled()
: !RuntimeEnabledFeatures::AdTaggingEnabled());
// See SubresourceFilterAgent::Initialize for why we don't set this here for
// fenced frames.
is_frame_created_by_ad_script_ =
!IsMainFrame() && ad_tracker_ &&
ad_tracker_->IsAdScriptInStack(AdTracker::StackType::kBottomAndTop,
&provisional_ad_script_ancestry_);
Initialize();
// Now that we know whether the frame is provisional, inherit the probe
// sink from parent if appropriate. See comment above for more details.
if (!IsLocalRoot() && !IsProvisional()) {
probe_sink_ = LocalFrameRoot().probe_sink_;
probe::FrameAttachedToParent(this, provisional_ad_script_ancestry_);
provisional_ad_script_ancestry_.clear();
}
}
FrameScheduler* LocalFrame::GetFrameScheduler() {
return frame_scheduler_.get();
}
EventHandlerRegistry& LocalFrame::GetEventHandlerRegistry() const {
return event_handler_->GetEventHandlerRegistry();
}
scoped_refptr<base::SingleThreadTaskRunner> LocalFrame::GetTaskRunner(
TaskType type) {
DCHECK(IsMainThread());
return frame_scheduler_->GetTaskRunner(type);
}
void LocalFrame::ScheduleVisualUpdateUnlessThrottled() {
if (ShouldThrottleRendering())
return;
GetPage()->Animator().ScheduleVisualUpdate(this);
}
static bool CanAccessAncestor(const SecurityOrigin& active_security_origin,
const Frame* target_frame) {
// targetFrame can be 0 when we're trying to navigate a top-level frame
// that has a 0 opener.
if (!target_frame)
return false;
const bool is_local_active_origin = active_security_origin.IsLocal();
for (const Frame* ancestor_frame = target_frame; ancestor_frame;
ancestor_frame = ancestor_frame->Tree().Parent()) {
const SecurityOrigin* ancestor_security_origin =
ancestor_frame->GetSecurityContext()->GetSecurityOrigin();
if (active_security_origin.CanAccess(ancestor_security_origin))
return true;
// Allow file URL descendant navigation even when
// allowFileAccessFromFileURLs is false.
// FIXME: It's a bit strange to special-case local origins here. Should we
// be doing something more general instead?
if (is_local_active_origin && ancestor_security_origin->IsLocal())
return true;
}
return false;
}
bool LocalFrame::CanNavigate(const Frame& target_frame,
const KURL& destination_url) {
// https://html.spec.whatwg.org/multipage/browsers.html#allowed-to-navigate
// If source is target, then return true.
if (&target_frame == this)
return true;
// Navigating window.opener cross origin, without user activation. See
// https://crbug.com/813643.
if (Opener() == target_frame && !HasTransientUserActivation(this) &&
!target_frame.GetSecurityContext()->GetSecurityOrigin()->CanAccess(
SecurityOrigin::Create(destination_url).get())) {
UseCounter::Count(GetDocument(),
WebFeature::kOpenerNavigationWithoutGesture);
}
if (destination_url.ProtocolIsJavaScript() &&
(!GetSecurityContext()->GetSecurityOrigin()->CanAccess(
target_frame.GetSecurityContext()->GetSecurityOrigin()))) {
PrintNavigationErrorMessage(
target_frame,
"The frame attempting navigation must be same-origin with the target "
"if navigating to a javascript: url");
return false;
}
if (GetSecurityContext()->IsSandboxed(
network::mojom::blink::WebSandboxFlags::kNavigation)) {
// 'allow-top-navigation' and 'allow-top-navigation-by-user-activation'
// allow the outermost frame navigations. They don't allow root fenced frame
// navigations from the descendant frames.
const bool target_is_outermost_frame =
target_frame.IsMainFrame() &&
!target_frame.GetPage()->IsMainFrameFencedFrameRoot();
if (!target_frame.Tree().IsDescendantOf(this) &&
!target_is_outermost_frame) {
PrintNavigationErrorMessage(
target_frame,
IsInFencedFrameTree()
? "The frame attempting navigation is in a fenced frame tree, "
"and is therefore disallowed from navigating its ancestors."
: "The frame attempting navigation is sandboxed, and is "
"therefore "
"disallowed from navigating its ancestors.");
return false;
}
// Sandboxed frames can also navigate popups, if the
// 'allow-sandbox-escape-via-popup' flag is specified, or if
// 'allow-popups' flag is specified and the popup's opener is the frame.
if (target_is_outermost_frame && target_frame != Tree().Top() &&
GetSecurityContext()->IsSandboxed(
network::mojom::blink::WebSandboxFlags::
kPropagatesToAuxiliaryBrowsingContexts) &&
(GetSecurityContext()->IsSandboxed(
network::mojom::blink::WebSandboxFlags::kPopups) ||
target_frame.Opener() != this)) {
PrintNavigationErrorMessage(
target_frame,
"The frame attempting navigation is sandboxed and is trying "
"to navigate a popup, but is not the popup's opener and is not "
"set to propagate sandboxing to popups.");
return false;
}
// Top navigation is forbidden in sandboxed frames unless opted-in, and only
// then if the ancestor chain allowed to navigate the top frame.
// Note: We don't check root fenced frames for kTop* flags since the kTop*
// flags imply the actual top-level page.
if ((target_frame == Tree().Top()) &&
!target_frame.GetPage()->IsMainFrameFencedFrameRoot()) {
if (GetSecurityContext()->IsSandboxed(
network::mojom::blink::WebSandboxFlags::kTopNavigation) &&
GetSecurityContext()->IsSandboxed(
network::mojom::blink::WebSandboxFlags::
kTopNavigationByUserActivation)) {
PrintNavigationErrorMessage(
target_frame,
"The frame attempting navigation of the top-level window is "
"sandboxed, but the flag of 'allow-top-navigation' or "
"'allow-top-navigation-by-user-activation' is not set.");
return false;
}
// With only 'allow-top-navigation-by-user-activation' (but not
// 'allow-top-navigation'), top navigation requires a user gesture.
if (GetSecurityContext()->IsSandboxed(
network::mojom::blink::WebSandboxFlags::kTopNavigation) &&
!GetSecurityContext()->IsSandboxed(
network::mojom::blink::WebSandboxFlags::
kTopNavigationByUserActivation)) {
// If there is no user activation, fail.
if (!HasTransientUserActivation(this)) {
GetLocalFrameHostRemote().DidBlockNavigation(
destination_url, mojom::blink::NavigationBlockedReason::
kRedirectWithNoUserGestureSandbox);
PrintNavigationErrorMessage(
target_frame,
"The frame attempting navigation of the top-level window is "
"sandboxed with the 'allow-top-navigation-by-user-activation' "
"flag, but has no user activation (aka gesture). See "
"https://www.chromestatus.com/feature/5629582019395584.");
return false;
}
}
// With only 'allow-top-navigation':
// This is a "last line of defense" to prevent a cross-origin document
// from escalating its own top-navigation privileges. See
// `PolicyContainerPolicies::can_navigate_top_without_user_gesture`
// for the cases where this would be allowed or disallowed.
// See (crbug.com/1145553) and (crbug.com/1251790).
if (!DomWindow()
->GetExecutionContext()
->GetPolicyContainer()
->GetPolicies()
.can_navigate_top_without_user_gesture &&
!HasStickyUserActivation()) {
String message =
"The frame attempting to navigate the top-level window is "
"cross-origin and either it or one of its ancestors is not "
"allowed to navigate the top frame.\n";
PrintNavigationErrorMessage(target_frame, message);
return false;
}
return true;
}
}
DCHECK(GetSecurityContext()->GetSecurityOrigin());
const SecurityOrigin& origin = *GetSecurityContext()->GetSecurityOrigin();
// This is the normal case. A document can navigate its decendant frames,
// or, more generally, a document can navigate a frame if the document is
// in the same origin as any of that frame's ancestors (in the frame
// hierarchy).
//
// See http://www.adambarth.com/papers/2008/barth-jackson-mitchell.pdf for
// historical information about this security check.
if (CanAccessAncestor(origin, &target_frame))
return true;
// Top-level frames are easier to navigate than other frames because they
// display their URLs in the address bar (in most browsers). However, there
// are still some restrictions on navigation to avoid nuisance attacks.
// Specifically, a document can navigate a top-level frame if that frame
// opened the document or if the document is the same-origin with any of
// the top-level frame's opener's ancestors (in the frame hierarchy).
//
// In both of these cases, the document performing the navigation is in
// some way related to the frame being navigate (e.g., by the "opener"
// and/or "parent" relation). Requiring some sort of relation prevents a
// document from navigating arbitrary, unrelated top-level frames.
if (!target_frame.Tree().Parent()) {
if (target_frame == Opener())
return true;
if (CanAccessAncestor(origin, target_frame.Opener()))
return true;
}
if (target_frame == Tree().Top()) {
// A frame navigating its top may blocked if the document initiating
// the navigation has never received a user gesture and the navigation
// isn't same-origin with the target.
if (HasStickyUserActivation() ||
target_frame.GetSecurityContext()->GetSecurityOrigin()->CanAccess(
SecurityOrigin::Create(destination_url).get())) {
return true;
}
String target_domain = network_utils::GetDomainAndRegistry(
target_frame.GetSecurityContext()->GetSecurityOrigin()->Domain(),
network_utils::kIncludePrivateRegistries);
String destination_domain = network_utils::GetDomainAndRegistry(
destination_url.Host(), network_utils::kIncludePrivateRegistries);
if (!target_domain.empty() && !destination_domain.empty() &&
target_domain == destination_domain &&
(target_frame.GetSecurityContext()->GetSecurityOrigin()->Protocol() ==
destination_url.Protocol())) {
return true;
}
if (loader_.GetDocumentLoader()->GetContentSettings()->allow_popup) {
return true;
}
PrintNavigationErrorMessage(
target_frame,
"The frame attempting navigation is targeting its top-level window, "
"but is neither same-origin with its target nor has it received a "
"user gesture. See "
"https://www.chromestatus.com/feature/5851021045661696.");
GetLocalFrameHostRemote().DidBlockNavigation(
destination_url,
mojom::blink::NavigationBlockedReason::kRedirectWithNoUserGesture);
} else {
PrintNavigationErrorMessage(
target_frame,
"The frame attempting navigation is neither same-origin with the "
"target, nor is it the target's parent or opener.");
}
return false;
}
void LocalFrame::MaybeStartOutermostMainFrameNavigation(
const Vector<KURL>& urls) const {
TRACE_EVENT0("navigation",
"LocalFrame::MaybeStartOutermostMainFrameNavigation");
mojo_handler_->NonAssociatedLocalFrameHostRemote()
.MaybeStartOutermostMainFrameNavigation(urls);
}
ContentCaptureManager* LocalFrame::GetOrResetContentCaptureManager() {
DCHECK(Client());
if (!IsLocalRoot())
return nullptr;
// WebContentCaptureClient is set on each navigation and it could become null
// because the url is in disallowed list, so ContentCaptureManager
// is created or released as needed to save the resources.
// It is a little bit odd that ContentCaptureManager is created or released on
// demand, and that this is something that could be improved with an explicit
// signal for creating / destroying content capture managers.
if (Client()->GetWebContentCaptureClient()) {
if (!content_capture_manager_) {
content_capture_manager_ =
MakeGarbageCollected<ContentCaptureManager>(*this);
}
} else if (content_capture_manager_) {
content_capture_manager_->Shutdown();
content_capture_manager_ = nullptr;
}
return content_capture_manager_.Get();
}
BrowserInterfaceBrokerProxy& LocalFrame::GetBrowserInterfaceBroker() {
if (!browser_interface_broker_proxy_.is_bound()) {
// This branch is taken in unit tests.
return GetEmptyBrowserInterfaceBroker();
}
return browser_interface_broker_proxy_;
}
AssociatedInterfaceProvider*
LocalFrame::GetRemoteNavigationAssociatedInterfaces() {
DCHECK(Client());
return Client()->GetRemoteNavigationAssociatedInterfaces();
}
LocalFrameClient* LocalFrame::Client() const {
return static_cast<LocalFrameClient*>(Frame::Client());
}
FrameWidget* LocalFrame::GetWidgetForLocalRoot() {
WebLocalFrameImpl* web_frame = WebLocalFrameImpl::FromFrame(this);
if (!web_frame)
return nullptr;
// This WebFrameWidgetImpl upcasts to a FrameWidget which is the interface
// exposed to Blink core.
return web_frame->LocalRootFrameWidget();
}
WebContentSettingsClient* LocalFrame::GetContentSettingsClient() {
return Client() ? Client()->GetContentSettingsClient() : nullptr;
}
const mojom::RendererContentSettingsPtr& LocalFrame::GetContentSettings()
const {
return Loader().GetDocumentLoader()->GetContentSettings();
}
PluginData* LocalFrame::GetPluginData() const {
if (!Loader().AllowPlugins())
return nullptr;
return GetPage()->GetPluginData();
}
void LocalFrame::SetAdTrackerForTesting(AdTracker* ad_tracker) {
if (ad_tracker_)
ad_tracker_->Shutdown();
ad_tracker_ = ad_tracker;
}
DEFINE_WEAK_IDENTIFIER_MAP(LocalFrame)
FrameNavigationDisabler::FrameNavigationDisabler(LocalFrame& frame)
: frame_(&frame) {
frame_->DisableNavigation();
}
FrameNavigationDisabler::~FrameNavigationDisabler() {
frame_->EnableNavigation();
}
LocalFrame::LazyLoadImageSetting LocalFrame::GetLazyLoadImageSetting() const {
DCHECK(GetSettings());
if (!GetSettings()->GetLazyLoadEnabled()) {
return LocalFrame::LazyLoadImageSetting::kDisabled;
}
if (GetDocument()->IsPageVisible()) {
return LocalFrame::LazyLoadImageSetting::kEnabledExplicit;
}
if (base::FeatureList::IsEnabled(
features::kEnableLazyLoadImageForInvisiblePage)) {
switch (features::kEnableLazyLoadImageForInvisiblePageTypeParam.Get()) {
case features::EnableLazyLoadImageForInvisiblePageType::kAllInvisiblePage:
return LocalFrame::LazyLoadImageSetting::kEnabledExplicit;
case features::EnableLazyLoadImageForInvisiblePageType::kPrerenderPage:
if (GetDocument()->IsPrerendering()) {
return LocalFrame::LazyLoadImageSetting::kEnabledExplicit;
}
return LocalFrame::LazyLoadImageSetting::kDisabled;
}
}
// Disable lazyload for backgrounded pages including NoStatePrefetch and
// Prerender.
return LocalFrame::LazyLoadImageSetting::kDisabled;
}
scoped_refptr<network::SharedURLLoaderFactory>
LocalFrame::GetURLLoaderFactory() {
return Client()->GetURLLoaderFactory();
}
std::unique_ptr<URLLoader> LocalFrame::CreateURLLoaderForTesting() {
return Client()->CreateURLLoaderForTesting();
}
scoped_refptr<WebBackgroundResourceFetchAssets>
LocalFrame::MaybeGetBackgroundResourceFetchAssets() {
return Client()->MaybeGetBackgroundResourceFetchAssets();
}
WebPluginContainerImpl* LocalFrame::GetWebPluginContainer(Node* node) const {
if (auto* plugin_document = DynamicTo<PluginDocument>(GetDocument())) {
return plugin_document->GetPluginView();
}
if (!node) {
DCHECK(GetDocument());
node = GetDocument()->FocusedElement();
}
if (node) {
return node->GetWebPluginContainer();
}
return nullptr;
}
void LocalFrame::WasHidden() {
if (hidden_)
return;
hidden_ = true;
if (auto* content_capture_manager = GetOrResetContentCaptureManager()) {
content_capture_manager->OnFrameWasHidden();
}
// An iframe may get a "was hidden" notification before it has been attached
// to the frame tree; in that case, skip further processing.
if (!Owner() || IsProvisional())
return;
// Mark intersections as dirty, so that child frames will reevaluate their
// render throttling status on the next lifecycle update.
LocalFrameView* frame_view = View();
if (frame_view)
frame_view->SetIntersectionObservationState(LocalFrameView::kDesired);
// If we are tracking occlusion for this frame, and it was not previously
// known to be occluded, then we need to force "not visible" notifications to
// be sent, since it's unknown whether this frame will run lifecycle updates.
// Frame was already occluded, nothing more to do.
if (intersection_state_.occlusion_state ==
mojom::blink::FrameOcclusionState::kPossiblyOccluded) {
return;
}
Document* document = GetDocument();
if (frame_view && document && document->IsActive()) {
if (auto* controller = GetDocument()->GetIntersectionObserverController()) {
if (controller->NeedsOcclusionTracking()) {
View()->ForceUpdateViewportIntersections();
}
}
}
}
void LocalFrame::WasShown() {
if (!hidden_)
return;
hidden_ = false;
if (LocalFrameView* frame_view = View())
frame_view->ScheduleAnimation();
if (auto* content_capture_manager = GetOrResetContentCaptureManager()) {
content_capture_manager->OnFrameWasShown();
}
}
bool LocalFrame::ClipsContent() const {
// A paint preview shouldn't clip to the viewport. Each frame paints to a
// separate canvas in full to allow scrolling.
if (GetDocument()->GetPaintPreviewState() != Document::kNotPaintingPreview) {
return false;
}
if (ShouldUsePaginatedLayout()) {
return false;
}
if (IsOutermostMainFrame()) {
return GetSettings()->GetMainFrameClipsContent();
}
// By default clip to viewport.
return true;
}
void LocalFrame::SetViewportIntersectionFromParent(
const mojom::blink::ViewportIntersectionState& intersection_state) {
DCHECK(IsLocalRoot());
DCHECK(!IsOutermostMainFrame());
// Notify the render frame observers when the main frame intersection or the
// transform changes.
if (intersection_state_.main_frame_intersection !=
intersection_state.main_frame_intersection ||
intersection_state_.main_frame_transform !=
intersection_state.main_frame_transform) {
gfx::Rect rect = intersection_state.main_frame_transform.MapRect(
intersection_state.main_frame_intersection);
// Return <0, 0, 0, 0> if there is no area.
if (rect.IsEmpty())
rect.set_origin(gfx::Point(0, 0));
Client()->OnMainFrameIntersectionChanged(rect);
}
// Viewport intersection state needs to be updated when remote ancestor
// frames and their respective scroll positions, clips, etc change.
if (intersection_state_.viewport_intersection !=
intersection_state.viewport_intersection ||
intersection_state_.outermost_main_frame_size !=
intersection_state.outermost_main_frame_size) {
int viewport_intersect_area =
intersection_state.viewport_intersection.size()
.GetCheckedArea()
.ValueOrDefault(INT_MAX);
int outermost_main_frame_area =
intersection_state.outermost_main_frame_size.GetCheckedArea()
.ValueOrDefault(INT_MAX);
float ratio = 1.0f * viewport_intersect_area / outermost_main_frame_area;
const float ratio_threshold =
1.0f * features::kLargeFrameSizePercentThreshold.Get() / 100;
GetFrameScheduler()->SetVisibleAreaLarge(ratio > ratio_threshold);
}
// We only schedule an update if the viewport intersection or occlusion state
// has changed; neither the viewport offset nor the compositing bounds will
// affect IntersectionObserver.
bool needs_update =
intersection_state_.viewport_intersection !=
intersection_state.viewport_intersection ||
intersection_state_.occlusion_state != intersection_state.occlusion_state;
intersection_state_ = intersection_state;
if (needs_update) {
if (LocalFrameView* frame_view = View()) {
frame_view->SetIntersectionObservationState(LocalFrameView::kRequired);
frame_view->ScheduleAnimation();
}
}
}
gfx::Size LocalFrame::GetOutermostMainFrameSize() const {
LocalFrame& local_root = LocalFrameRoot();
return local_root.IsOutermostMainFrame()
? local_root.View()->LayoutViewport()->VisibleContentRect().size()
: local_root.intersection_state_.outermost_main_frame_size;
}
gfx::Point LocalFrame::GetOutermostMainFrameScrollPosition() const {
LocalFrame& local_root = LocalFrameRoot();
return local_root.IsOutermostMainFrame()
? gfx::ToFlooredPoint(
local_root.View()->LayoutViewport()->ScrollPosition())
: local_root.intersection_state_
.outermost_main_frame_scroll_position;
}
void LocalFrame::SetOpener(Frame* opener_frame) {
// Only a local frame should be able to update another frame's opener.
DCHECK(!opener_frame || opener_frame->IsLocalFrame());
auto* web_frame = WebFrame::FromCoreFrame(this);
if (web_frame && Opener() != opener_frame) {
GetLocalFrameHostRemote().DidChangeOpener(
opener_frame
? std::optional<blink::LocalFrameToken>(
opener_frame->GetFrameToken().GetAs<LocalFrameToken>())
: std::nullopt);
}
SetOpenerDoNotNotify(opener_frame);
}
mojom::blink::FrameOcclusionState LocalFrame::GetOcclusionState() const {
if (hidden_)
return mojom::blink::FrameOcclusionState::kPossiblyOccluded;
if (IsLocalRoot())
return intersection_state_.occlusion_state;
return LocalFrameRoot().GetOcclusionState();
}
bool LocalFrame::NeedsOcclusionTracking() const {
if (Document* document = GetDocument()) {
if (IntersectionObserverController* controller =
document->GetIntersectionObserverController()) {
return controller->NeedsOcclusionTracking();
}
}
return false;
}
void LocalFrame::ForceSynchronousDocumentInstall(const AtomicString& mime_type,
const SegmentedBuffer& data) {
CHECK(GetDocument()->IsInitialEmptyDocument());
DCHECK(!Client()->IsLocalFrameClientImpl());
DCHECK(GetPage());
// Any Document requires Shutdown() before detach, even the initial empty
// document.
GetDocument()->Shutdown();
DomWindow()->ClearForReuse();
Document* document = DomWindow()->InstallNewDocument(
DocumentInit::Create()
.WithWindow(DomWindow(), nullptr)
.WithTypeFrom(mime_type)
.ForPrerendering(GetPage()->IsPrerendering()));
DCHECK_EQ(document, GetDocument());
DocumentParser* parser = document->OpenForNavigation(
kForceSynchronousParsing, mime_type, AtomicString("UTF-8"));
// Some code creates a very large number of tiny chunks that show up in
// |data|, such as InternalPopupMenu. Calling parser->AppendBytes() with
// each tiny piece dramatically slows down document loading. By combining
// these chunks in a Vector before passing it to parser->AppendBytes() gets
// around this problem.
Vector<char> current_chunk;
for (const auto& segment : data) {
current_chunk.AppendSpan(base::span(segment));
if (current_chunk.size() > kMaxDocumentChunkSize) {
parser->AppendBytes(base::as_byte_span(current_chunk));
current_chunk.clear();
}
}
parser->AppendBytes(base::as_byte_span(current_chunk));
current_chunk.clear();
parser->Finish();
// Upon loading of SVGImages, log PageVisits in UseCounter if we did not
// replace the document in `parser->Finish()`, which may happen when XSLT
// finishes processing.
// Do not track PageVisits for inspector, web page popups, and validation
// message overlays (the other callers of this method).
if (document == GetDocument() && document->IsSVGDocument())
loader_.GetDocumentLoader()->GetUseCounter().DidCommitLoad(this);
}
bool LocalFrame::IsProvisional() const {
// Calling this after the frame is marked as completely detached is a bug, as
// this state can no longer be accurately calculated.
CHECK(!IsDetached());
if (IsMainFrame()) {
return GetPage()->MainFrame() != this;
}
DCHECK(Owner());
return Owner()->ContentFrame() != this;
}
bool LocalFrame::IsAdFrame() const {
return ad_evidence_ && ad_evidence_->IndicatesAdFrame();
}
bool LocalFrame::IsAdRoot() const {
return IsAdFrame() && !ad_evidence_->parent_is_ad();
}
void LocalFrame::SetAdEvidence(const FrameAdEvidence& ad_evidence) {
DCHECK(!IsMainFrame() || IsInFencedFrameTree());
DCHECK(ad_evidence.is_complete());
// Once set, `is_frame_created_by_ad_script_` should not be unset.
DCHECK(!is_frame_created_by_ad_script_ ||
ad_evidence.created_by_ad_script() ==
blink::mojom::FrameCreationStackEvidence::kCreatedByAdScript);
is_frame_created_by_ad_script_ =
ad_evidence.created_by_ad_script() ==
blink::mojom::FrameCreationStackEvidence::kCreatedByAdScript;
if (ad_evidence_.has_value()) {
// Check that replacing with the new ad evidence doesn't violate invariants.
// The parent frame's ad status should not change as it can only change due
// to a cross-document commit, which would remove this child frame.
DCHECK_EQ(ad_evidence_->parent_is_ad(), ad_evidence.parent_is_ad());
// The most restrictive filter list result cannot become less restrictive,
// by definition.
DCHECK_LE(ad_evidence_->most_restrictive_filter_list_result(),
ad_evidence.most_restrictive_filter_list_result());
}
bool was_ad_frame = IsAdFrame();
bool is_ad_frame = ad_evidence.IndicatesAdFrame();
ad_evidence_ = ad_evidence;
if (was_ad_frame == is_ad_frame)
return;
if (auto* document = GetDocument()) {
// TODO(fdoray): It is possible for the document not to be installed when
// this method is called. Consider inheriting frame bit in the graph instead
// of sending an IPC.
auto* document_resource_coordinator = document->GetResourceCoordinator();
if (document_resource_coordinator)
document_resource_coordinator->SetIsAdFrame(is_ad_frame);
}
UpdateAdHighlight();
frame_scheduler_->SetIsAdFrame(is_ad_frame);
if (is_ad_frame) {
UseCounter::Count(DomWindow(), WebFeature::kAdFrameDetected);
InstanceCounters::IncrementCounter(InstanceCounters::kAdSubframeCounter);
} else {
InstanceCounters::DecrementCounter(InstanceCounters::kAdSubframeCounter);
}
}
bool LocalFrame::IsAdScriptInStack() const {
return ad_tracker_ &&
ad_tracker_->IsAdScriptInStack(AdTracker::StackType::kBottomAndTop);
}
void LocalFrame::UpdateAdHighlight() {
if (IsMainFrame() && !IsInFencedFrameTree())
return;
// TODO(bokan): Fenced frames may need some work to propagate the ad
// highlighting setting to the inner tree.
if (IsAdRoot() && GetPage()->GetSettings().GetHighlightAds())
SetSubframeColorOverlay(SkColorSetARGB(128, 255, 0, 0));
else
SetSubframeColorOverlay(SK_ColorTRANSPARENT);
}
void LocalFrame::PauseSubresourceLoading(
mojo::PendingReceiver<mojom::blink::PauseSubresourceLoadingHandle>
receiver) {
auto handle = GetFrameScheduler()->GetPauseSubresourceLoadingHandle();
if (!handle)
return;
pause_handle_receivers_.Add(std::move(handle), std::move(receiver),
GetTaskRunner(blink::TaskType::kInternalDefault));
}
void LocalFrame::ResumeSubresourceLoading() {
pause_handle_receivers_.Clear();
}
void LocalFrame::UpdateTaskTime(base::TimeDelta time) {
Client()->DidChangeCpuTiming(time);
}
void LocalFrame::UpdateBackForwardCacheDisablingFeatures(
BlockingDetails details) {
auto mojom_details = ConvertFeatureAndLocationToMojomStruct(
*details.non_sticky_features_and_js_locations,
*details.sticky_features_and_js_locations);
GetBackForwardCacheControllerHostRemote()
.DidChangeBackForwardCacheDisablingFeatures(std::move(mojom_details));
}
using BlockingDetailsList = Vector<mojom::blink::BlockingDetailsPtr>;
BlockingDetailsList LocalFrame::ConvertFeatureAndLocationToMojomStruct(
const BFCacheBlockingFeatureAndLocations& non_sticky,
const BFCacheBlockingFeatureAndLocations& sticky) {
BlockingDetailsList blocking_details_list;
for (auto feature : non_sticky.details_list) {
auto blocking_details = CreateBlockingDetailsMojom(feature);
blocking_details_list.push_back(std::move(blocking_details));
}
for (auto feature : sticky.details_list) {
auto blocking_details = CreateBlockingDetailsMojom(feature);
blocking_details_list.push_back(std::move(blocking_details));
}
return blocking_details_list;
}
const base::UnguessableToken& LocalFrame::GetAgentClusterId() const {
if (const LocalDOMWindow* window = DomWindow()) {
return window->GetAgentClusterID();
}
return base::UnguessableToken::Null();
}
void LocalFrame::OnTaskCompleted(base::TimeTicks start_time,
base::TimeTicks end_time) {
if (FrameWidget* widget = GetWidgetForLocalRoot()) {
widget->OnTaskCompletedForFrame(start_time, end_time, this);
}
}
void LocalFrame::MainFrameInteractive() {
if (Page* page = GetPage()) {
page->GetV8CrowdsourcedCompileHintsProducer().GenerateData();
}
constexpr bool kIsFinalData = true;
v8_local_compile_hints_producer_->GenerateData(kIsFinalData);
V8HistogramAccumulator::GetInstance()->GenerateDataInteractive();
}
void LocalFrame::MainFrameFirstMeaningfulPaint() {
// Generate local compile hints early (the user might navigate away before the
// page turns interactive). If we still reach interactive, we replace the
// compile hints with new data.
constexpr bool kIsFinalData = false;
v8_local_compile_hints_producer_->GenerateData(kIsFinalData);
}
DocumentResourceCoordinator* LocalFrame::GetDocumentResourceCoordinator() {
return CHECK_DEREF(GetDocument()).GetResourceCoordinator();
}
mojom::blink::ReportingServiceProxy* LocalFrame::GetReportingService() {
return mojo_handler_->ReportingService();
}
mojom::blink::DevicePostureProvider* LocalFrame::GetDevicePostureProvider() {
return mojo_handler_->DevicePostureProvider();
}
// static
void LocalFrame::NotifyUserActivation(
LocalFrame* frame,
mojom::blink::UserActivationNotificationType notification_type) {
if (frame) {
frame->NotifyUserActivation(notification_type);
}
}
// static
bool LocalFrame::HasTransientUserActivation(LocalFrame* frame) {
return frame && frame->Frame::HasTransientUserActivation();
}
// static
bool LocalFrame::ConsumeTransientUserActivation(
LocalFrame* frame,
UserActivationUpdateSource update_source) {
return frame && frame->ConsumeTransientUserActivation(update_source);
}
void LocalFrame::NotifyUserActivation(
mojom::blink::UserActivationNotificationType notification_type) {
GetLocalFrameHostRemote().UpdateUserActivationState(
mojom::blink::UserActivationUpdateType::kNotifyActivation,
notification_type);
Client()->NotifyUserActivation();
NotifyUserActivationInFrameTree(notification_type);
}
bool LocalFrame::ConsumeTransientUserActivation(
UserActivationUpdateSource update_source) {
if (update_source == UserActivationUpdateSource::kRenderer) {
GetLocalFrameHostRemote().UpdateUserActivationState(
mojom::blink::UserActivationUpdateType::kConsumeTransientActivation,
mojom::blink::UserActivationNotificationType::kNone);
}
return ConsumeTransientUserActivationInFrameTree();
}
void LocalFrame::ConsumeHistoryUserActivation() {
// Notify the frame in the browser process, which will consume the activation
// in all frames of the page (consistent with the loop below).
GetLocalFrameHostRemote().DidConsumeHistoryUserActivation();
for (Frame* node = &Tree().Top(); node; node = node->Tree().TraverseNext()) {
if (LocalFrame* local_frame_node = DynamicTo<LocalFrame>(node)) {
local_frame_node->history_user_activation_state_.Consume();
}
}
}
void LocalFrame::SetHadUserInteraction(bool had_user_interaction) {
if (had_user_interaction) {
history_user_activation_state_.Activate();
} else {
history_user_activation_state_.Clear();
}
DomWindow()->closewatcher_stack()->SetHadUserInteraction(
had_user_interaction);
GetFrameScheduler()->SetHadUserActivation(had_user_interaction);
}
void LocalFrame::SetStorageAccessApiStatus(net::StorageAccessApiStatus status) {
GetLocalFrameHostRemote().SetStorageAccessApiStatus(status);
}
namespace {
class FrameColorOverlay final : public FrameOverlay::Delegate {
public:
explicit FrameColorOverlay(LocalFrame* frame, SkColor color)
: color_(color), frame_(frame) {}
SkColor GetColorForTesting() const { return color_; }
private:
void PaintFrameOverlay(const FrameOverlay& frame_overlay,
GraphicsContext& graphics_context,
const gfx::Size&) const override {
const auto* view = frame_->View();
DCHECK(view);
if (view->Width() == 0 || view->Height() == 0)
return;
ScopedPaintChunkProperties properties(
graphics_context.GetPaintController(),
view->GetLayoutView()->FirstFragment().LocalBorderBoxProperties(),
frame_overlay, DisplayItem::kFrameOverlay);
if (DrawingRecorder::UseCachedDrawingIfPossible(
graphics_context, frame_overlay, DisplayItem::kFrameOverlay))
return;
DrawingRecorder recorder(graphics_context, frame_overlay,
DisplayItem::kFrameOverlay,
gfx::Rect(view->Size()));
gfx::RectF rect(0, 0, view->Width(), view->Height());
graphics_context.FillRect(
rect, Color::FromSkColor(color_),
PaintAutoDarkMode(view->GetLayoutView()->StyleRef(),
DarkModeFilter::ElementRole::kBackground));
}
// TODO(https://crbug.com/1351544): This should be an SkColor4f or a Color.
SkColor color_;
Persistent<LocalFrame> frame_;
};
} // namespace
void LocalFrame::SetReducedAcceptLanguage(
const AtomicString& reduced_accept_language) {
reduced_accept_language_ = reduced_accept_language;
}
template <>
struct DowncastTraits<FrameColorOverlay> {
static bool AllowFrom(const FrameOverlay::Delegate& frame_overlay) {
return true;
}
};
std::optional<SkColor> LocalFrame::GetFrameOverlayColorForTesting() const {
if (!frame_color_overlay_)
return std::nullopt;
return DynamicTo<FrameColorOverlay>(frame_color_overlay_->GetDelegate())
->GetColorForTesting();
}
void LocalFrame::SetMainFrameColorOverlay(SkColor color) {
DCHECK(IsMainFrame() && !IsInFencedFrameTree());
SetFrameColorOverlay(color);
}
void LocalFrame::SetSubframeColorOverlay(SkColor color) {
DCHECK(!IsMainFrame() || IsInFencedFrameTree());
SetFrameColorOverlay(color);
}
void LocalFrame::SetFrameColorOverlay(SkColor color) {
if (frame_color_overlay_)
frame_color_overlay_.Release()->Destroy();
if (color == SK_ColorTRANSPARENT)
return;
frame_color_overlay_ = MakeGarbageCollected<FrameOverlay>(
this, std::make_unique<FrameColorOverlay>(this, color));
}
void LocalFrame::UpdateFrameColorOverlayPrePaint() {
if (frame_color_overlay_)
frame_color_overlay_->UpdatePrePaint();
}
void LocalFrame::PaintFrameColorOverlay(GraphicsContext& context) {
if (frame_color_overlay_)
frame_color_overlay_->Paint(context);
}
void LocalFrame::ForciblyPurgeV8Memory() {
DomWindow()->NotifyContextDestroyed();
WindowProxyManager* window_proxy_manager = GetWindowProxyManager();
window_proxy_manager->ClearForV8MemoryPurge();
Loader().StopAllLoaders(/*abort_client=*/true);
}
void LocalFrame::OnPageLifecycleStateUpdated() {
if (frozen_ != GetPage()->Frozen()) {
frozen_ = GetPage()->Frozen();
if (frozen_) {
DidFreeze();
} else {
DidResume();
}
// The event handlers might have detached the frame.
if (!IsAttached())
return;
}
SetContextPaused(GetPage()->Paused());
mojom::blink::FrameLifecycleState frame_lifecycle_state =
mojom::blink::FrameLifecycleState::kRunning;
if (GetPage()->Paused()) {
frame_lifecycle_state = mojom::blink::FrameLifecycleState::kPaused;
} else if (GetPage()->Frozen()) {
frame_lifecycle_state = mojom::blink::FrameLifecycleState::kFrozen;
}
DomWindow()->SetLifecycleState(frame_lifecycle_state);
}
void LocalFrame::SetContextPaused(bool is_paused) {
TRACE_EVENT0("blink", "LocalFrame::SetContextPaused");
if (is_paused == paused_)
return;
paused_ = is_paused;
if (IsLocalRoot() && (!is_paused || GetPage()->ShowPausedHudOverlay())) {
auto* widget = GetWidgetForLocalRoot();
if (widget) {
const auto* debug_state = widget->GetLayerTreeDebugState();
if (debug_state) {
cc::LayerTreeDebugState new_debug_state = *debug_state;
new_debug_state.debugger_paused = is_paused;
widget->SetLayerTreeDebugState(new_debug_state);
}
}
}
GetDocument()->Fetcher()->SetDefersLoading(GetLoaderFreezeMode());
Loader().SetDefersLoading(GetLoaderFreezeMode());
// TODO(altimin): Move this to PageScheduler level.
GetFrameScheduler()->SetPaused(is_paused);
}
LocalFrame* LocalFrame::GetPreviousLocalFrameForLocalSwap() {
CHECK(IsProvisional());
if (auto* previous_main_frame =
GetPage()->GetPreviousMainFrameForLocalSwap()) {
return previous_main_frame;
}
return DynamicTo<LocalFrame>(GetProvisionalOwnerFrame());
}
bool LocalFrame::SwapIn() {
TRACE_EVENT0("navigation", "LocalFrame::SwapIn");
base::ScopedUmaHistogramTimer histogram_timer("Navigation.LocalFrame.SwapIn");
DCHECK(IsProvisional());
WebLocalFrameClient* client = Client()->GetWebFrame()->Client();
// Swap in `this`, which is a provisional frame to an existing frame.
Frame* provisional_owner_frame = GetProvisionalOwnerFrame();
// First, check if there's a previous main frame to be used for a main frame
// LocalFrame <-> LocalFrame swap.
Frame* previous_local_main_frame =
GetPage()->GetPreviousMainFrameForLocalSwap();
if (previous_local_main_frame && !previous_local_main_frame->IsDetached()) {
// We're about to do a LocalFrame <-> LocalFrame swap for a provisional
// main frame, where the previous main frame and the provisional main frame
// are in different Pages. The provisional frame's owner is set to the
// placeholder main RemoteFrame for the new Page, but we should trigger the
// swapping out of the previous Page's main frame instead here.
// This is because we want to preserve the behavior before RenderDocument,
// where we would unload the previous document before the next document on
// same-LocalFrame cross-document navigation, and also transfer some state
// from the previous document to the new one.
// The placeholder main RemoteFrame for the new Page will also get detached
// so that the new main LocalFrame can be swapped in, but that will be done
// a bit later on in `Frame::SwapImpl()`, as we don't need to transfer any
// data from the placeholder RemoteFrame.
CHECK(IsMainFrame());
CHECK(previous_local_main_frame->IsLocalFrame());
CHECK_NE(previous_local_main_frame->GetPage(), GetPage());
CHECK(provisional_owner_frame->IsRemoteFrame());
CHECK(!DynamicTo<RemoteFrame>(provisional_owner_frame)
->IsRemoteFrameHostRemoteBound());
GetPage()->SetPreviousMainFrameForLocalSwap(nullptr);
return client->SwapIn(WebFrame::FromCoreFrame(previous_local_main_frame));
}
// In all other cases, the LocalFrame would be swapped in with the provisional
// owner frame which belongs to the same Page as `this`. The provisional owner
// frame can be a RemoteFrame or a LocalFrame (for non-main frame
// LocalFrame <-> LocalFrame swap cases).
CHECK_EQ(provisional_owner_frame->GetPage(), GetPage());
// When creating a provisional LocalFrame, a new provisional probe sink is
// created. Whether that probe sink is going to be used differs depending
// on the situation:
// - For local roots, that provisional probe sink should be used, as
// local roots needs new probe sinks. So nothing needs to be done here.
// - For non-local-root LocalFrame <-> LocalFrame swap, reuse the previous
// LocalFrame's probe sink.
// - For other cases, reuse the local root's probe sink.
// Note that the probes dispatched to provisional sink are lost, so no
// events are sent before swap in or after swap out.
if (!IsLocalRoot()) {
if (auto* local_provisional_owner =
DynamicTo<LocalFrame>(provisional_owner_frame)) {
// This is doing a LocalFrame <-> LocalFrame swap, so reuse the previous
// LocalFrame's probe sink through swapping below. The detaching/unloading
// of the previous document is done before we swap the probe sinks. This
// is to ensure that resources from the old document won't stay around and
// thus won't be be captured in the newly committed document's probe sink.
bool swap_result =
client->SwapIn(WebFrame::FromCoreFrame(provisional_owner_frame));
std::swap(probe_sink_, local_provisional_owner->probe_sink_);
return swap_result;
}
// This is a remote -> local swap, so just use the local root's probe sink.
probe_sink_ = LocalFrameRoot().probe_sink_;
// For remote -> local swap, Send a frameAttached event to keep the legacy
// behavior where we fire the frameAttached event on cross-site navigations.
probe::FrameAttachedToParent(this, provisional_ad_script_ancestry_);
provisional_ad_script_ancestry_.clear();
}
return client->SwapIn(WebFrame::FromCoreFrame(provisional_owner_frame));
}
void LocalFrame::Discard() {
DomWindow()->GetScriptController().DiscardFrame();
}
void LocalFrame::LoadJavaScriptURL(const KURL& url) {
// Protect privileged pages against bookmarklets and other JavaScript
// manipulations.
if (SchemeRegistry::ShouldTreatURLSchemeAsNotAllowingJavascriptURLs(
GetSecurityContext()
->GetSecurityOrigin()
->GetOriginOrPrecursorOriginIfOpaque()
->Protocol())) {
return;
}
// TODO(mustaq): This is called only through the user typing a javascript URL
// into the omnibox. See https://crbug.com/1082900
NotifyUserActivation(
mojom::blink::UserActivationNotificationType::kInteraction);
auto* window = DomWindow();
window->GetScriptController().ExecuteJavaScriptURL(
url, network::mojom::CSPDisposition::DO_NOT_CHECK,
&DOMWrapperWorld::MainWorld(window->GetIsolate()));
}
void LocalFrame::RequestExecuteScript(
int32_t world_id,
base::span<const WebScriptSource> sources,
mojom::blink::UserActivationOption user_gesture,
mojom::blink::EvaluationTiming evaluation_timing,
mojom::blink::LoadEventBlockingOption blocking_option,
WebScriptExecutionCallback callback,
BackForwardCacheAware back_forward_cache_aware,
mojom::blink::WantResultOption want_result_option,
mojom::blink::PromiseResultOption promise_behavior) {
DOMWrapperWorld* world;
ExecuteScriptPolicy execute_script_policy;
CHECK(!IsProvisional());
if (world_id == DOMWrapperWorld::kMainWorldId) {
world = &DOMWrapperWorld::MainWorld(ToIsolate(this));
execute_script_policy =
ExecuteScriptPolicy::kDoNotExecuteScriptWhenScriptsDisabled;
} else {
world = DOMWrapperWorld::EnsureIsolatedWorld(ToIsolate(this), world_id);
// This is to preserve the existing behavior.
execute_script_policy =
ExecuteScriptPolicy::kExecuteScriptWhenScriptsDisabled;
}
if (back_forward_cache_aware == BackForwardCacheAware::kPossiblyDisallow) {
GetFrameScheduler()->RegisterStickyFeature(
SchedulingPolicy::Feature::kInjectedJavascript,
{SchedulingPolicy::DisableBackForwardCache()});
}
Vector<WebScriptSource> script_sources;
script_sources.AppendSpan(sources);
ScriptState* script_state = ToScriptState(this, *world);
CHECK(script_state);
PausableScriptExecutor::CreateAndRun(
script_state, std::move(script_sources), execute_script_policy,
user_gesture, evaluation_timing, blocking_option, want_result_option,
promise_behavior, std::move(callback));
}
void LocalFrame::SetEvictCachedSessionStorageOnFreezeOrUnload() {
evict_cached_session_storage_on_freeze_or_unload_ = true;
}
LocalFrameToken LocalFrame::GetLocalFrameToken() const {
return GetFrameToken().GetAs<LocalFrameToken>();
}
LoaderFreezeMode LocalFrame::GetLoaderFreezeMode() {
if (paused_ || frozen_) {
if (GetPage()->GetPageScheduler()->IsInBackForwardCache() &&
IsInflightNetworkRequestBackForwardCacheSupportEnabled()) {
return LoaderFreezeMode::kBufferIncoming;
}
return LoaderFreezeMode::kStrict;
}
return LoaderFreezeMode::kNone;
}
void LocalFrame::DidFreeze() {
TRACE_EVENT0("blink", "LocalFrame::DidFreeze");
DCHECK(IsAttached());
GetDocument()->DispatchFreezeEvent();
if (evict_cached_session_storage_on_freeze_or_unload_) {
// Evicts the cached data of Session Storage to avoid reusing old data in
// the cache after the session storage has been modified by another renderer
// process.
CoreInitializer::GetInstance().EvictSessionStorageCachedData(
GetDocument()->GetPage());
}
// DispatchFreezeEvent dispatches JS events, which may detach |this|.
if (!IsAttached())
return;
// TODO(fmeawad): Move the following logic to the page once we have a
// PageResourceCoordinator in Blink. http://crbug.com/838415
if (auto* document_resource_coordinator =
GetDocument()->GetResourceCoordinator()) {
document_resource_coordinator->SetLifecycleState(
performance_manager::mojom::LifecycleState::kFrozen);
}
if (GetPage()->GetPageScheduler()->IsInBackForwardCache()) {
DomWindow()->SetIsInBackForwardCache(true);
}
LoaderFreezeMode freeze_mode = GetLoaderFreezeMode();
GetDocument()->Fetcher()->SetDefersLoading(freeze_mode);
Loader().SetDefersLoading(freeze_mode);
}
void LocalFrame::DidResume() {
TRACE_EVENT0("blink", "LocalFrame::DidResume");
DCHECK(IsAttached());
// Before doing anything, set the "is in BFCache" state to false. This might
// affect calculations of other states triggered by the code below, e.g. the
// LoaderFreezeMode.
DomWindow()->SetIsInBackForwardCache(false);
// TODO(yuzus): Figure out if we should call GetLoaderFreezeMode().
GetDocument()->Fetcher()->SetDefersLoading(LoaderFreezeMode::kNone);
Loader().SetDefersLoading(LoaderFreezeMode::kNone);
GetDocument()->DispatchEvent(*Event::Create(event_type_names::kResume));
// TODO(fmeawad): Move the following logic to the page once we have a
// PageResourceCoordinator in Blink
if (auto* document_resource_coordinator =
GetDocument()->GetResourceCoordinator()) {
document_resource_coordinator->SetLifecycleState(
performance_manager::mojom::LifecycleState::kRunning);
}
// TODO(yuzus): Figure out where these calls should really belong.
GetDocument()->DispatchHandleLoadStart();
GetDocument()->DispatchHandleLoadComplete();
}
void LocalFrame::CountUseIfFeatureWouldBeBlockedByPermissionsPolicy(
mojom::WebFeature blocked_cross_origin,
mojom::WebFeature blocked_same_origin) {
// Get the origin of the top-level document
const SecurityOrigin* topOrigin =
Tree().Top().GetSecurityContext()->GetSecurityOrigin();
// Check if this frame is same-origin with the top-level or is in
// a fenced frame tree.
if (!GetSecurityContext()->GetSecurityOrigin()->CanAccess(topOrigin) ||
IsInFencedFrameTree()) {
// This frame is cross-origin with the top-level frame, and so would be
// blocked without a permissions policy.
UseCounter::Count(GetDocument(), blocked_cross_origin);
return;
}
// Walk up the frame tree looking for any cross-origin embeds. Even if this
// frame is same-origin with the top-level, if it is embedded by a cross-
// origin frame (like A->B->A) it would be blocked without a permissions
// policy.
const Frame* f = this;
while (!f->IsMainFrame()) {
if (!f->GetSecurityContext()->GetSecurityOrigin()->CanAccess(topOrigin)) {
UseCounter::Count(GetDocument(), blocked_same_origin);
return;
}
f = f->Tree().Parent();
}
}
void LocalFrame::FinishedLoading(FrameLoader::NavigationFinishState state) {
DomWindow()->FinishedLoading(state);
}
void LocalFrame::UpdateFaviconURL() {
if (!IsMainFrame())
return;
// The URL to the icon may be in the header. As such, only
// ask the loader for the icon if it's finished loading.
if (!GetDocument()->LoadEventFinished())
return;
int icon_types_mask =
1 << static_cast<int>(mojom::blink::FaviconIconType::kFavicon) |
1 << static_cast<int>(mojom::blink::FaviconIconType::kTouchIcon) |
1 << static_cast<int>(
mojom::blink::FaviconIconType::kTouchPrecomposedIcon);
Vector<IconURL> icon_urls = GetDocument()->IconURLs(icon_types_mask);
if (icon_urls.empty())
return;
Vector<mojom::blink::FaviconURLPtr> urls;
urls.reserve(icon_urls.size());
for (const auto& icon_url : icon_urls) {
urls.push_back(mojom::blink::FaviconURL::New(
icon_url.icon_url_, icon_url.icon_type_, icon_url.sizes_,
icon_url.is_default_icon_));
}
DCHECK_EQ(icon_urls.size(), urls.size());
GetLocalFrameHostRemote().UpdateFaviconURL(std::move(urls));
if (GetPage())
GetPage()->GetPageScheduler()->OnTitleOrFaviconUpdated();
}
void LocalFrame::SetIsCapturingMediaCallback(
IsCapturingMediaCallback callback) {
is_capturing_media_callback_ = std::move(callback);
}
bool LocalFrame::IsCapturingMedia() const {
return is_capturing_media_callback_ && is_capturing_media_callback_.Run();
}
SystemClipboard* LocalFrame::GetSystemClipboard() {
if (!system_clipboard_)
system_clipboard_ = MakeGarbageCollected<SystemClipboard>(this);
return system_clipboard_.Get();
}
void LocalFrame::WasAttachedAsLocalMainFrame() {
mojo_handler_->WasAttachedAsLocalMainFrame();
}
void LocalFrame::EvictFromBackForwardCache(
mojom::blink::RendererEvictionReason reason,
std::unique_ptr<SourceLocation> source_location) {
if (!GetPage()->GetPageScheduler()->IsInBackForwardCache())
return;
UMA_HISTOGRAM_ENUMERATION("BackForwardCache.Eviction.Renderer", reason);
mojom::blink::ScriptSourceLocationPtr source = nullptr;
if (source_location) {
source = mojom::blink::ScriptSourceLocation::New(
source_location->Url() ? KURL(source_location->Url()) : KURL(),
source_location->Function() ? source_location->Function() : "",
source_location->LineNumber(), source_location->ColumnNumber());
}
GetBackForwardCacheControllerHostRemote().EvictFromBackForwardCache(
std::move(reason), std::move(source));
}
void LocalFrame::DidBufferLoadWhileInBackForwardCache(
bool update_process_wide_count,
size_t num_bytes) {
DomWindow()->DidBufferLoadWhileInBackForwardCache(update_process_wide_count,
num_bytes);
}
void LocalFrame::SetScaleFactor(float scale_factor) {
DCHECK(!GetDocument() || !GetDocument()->Printing());
DCHECK(IsMainFrame());
const PageScaleConstraints& constraints =
GetPage()->GetPageScaleConstraintsSet().FinalConstraints();
scale_factor = constraints.ClampToConstraints(scale_factor);
if (scale_factor == GetPage()->GetVisualViewport().Scale())
return;
GetPage()->GetVisualViewport().SetScale(scale_factor);
}
void LocalFrame::ClosePageForTesting() {
mojo_handler_->ClosePageForTesting();
}
void LocalFrame::SetInitialFocus(bool reverse) {
GetDocument()->ClearFocusedElement();
GetPage()->GetFocusController().SetInitialFocus(
reverse ? mojom::blink::FocusType::kBackward
: mojom::blink::FocusType::kForward);
}
#if BUILDFLAG(IS_MAC)
void LocalFrame::GetCharacterIndexAtPoint(const gfx::Point& point) {
HitTestLocation location(View()->ViewportToFrame(gfx::Point(point)));
HitTestResult result = GetEventHandler().HitTestResultAtLocation(
location, HitTestRequest::kReadOnly | HitTestRequest::kActive);
uint32_t index =
Selection().CharacterIndexForPoint(result.RoundedPointInInnerNodeFrame());
mojo_handler_->TextInputHost().GotCharacterIndexAtPoint(index);
}
#endif
#if !BUILDFLAG(IS_ANDROID)
void LocalFrame::UpdateWindowControlsOverlay(
const gfx::Rect& bounding_rect_in_dips) {
// The rect passed to us from content is in DIP screen space, relative to the
// main frame, and needs to move to CSS space. This doesn't take the page's
// zoom factor into account so we must scale by the inverse of the page zoom
// in order to get correct CSS space coordinates. Note that when
// use-zoom-for-dsf is enabled, WindowToViewportScalar will be the true device
// scale factor, and LayoutZoomFactor will be the combination of the device
// scale factor and the zoom percent of the page. It is preferable to compute
// a rect that is slightly larger than one that would render smaller than the
// window control overlay.
LocalFrame& local_frame_root = LocalFrameRoot();
const float window_to_viewport_factor =
GetPage()->GetChromeClient().WindowToViewportScalar(&local_frame_root,
1.0f);
const float zoom_factor = local_frame_root.LayoutZoomFactor();
const float scale_factor = zoom_factor / window_to_viewport_factor;
gfx::Rect window_controls_overlay_rect =
gfx::ScaleToEnclosingRect(bounding_rect_in_dips, 1.0f / scale_factor);
bool fire_event =
(window_controls_overlay_rect != window_controls_overlay_rect_);
is_window_controls_overlay_visible_ = !window_controls_overlay_rect.IsEmpty();
window_controls_overlay_rect_ = window_controls_overlay_rect;
window_controls_overlay_rect_in_dips_ = bounding_rect_in_dips;
DocumentStyleEnvironmentVariables& vars =
GetDocument()->GetStyleEngine().EnsureEnvironmentVariables();
if (is_window_controls_overlay_visible_) {
SetTitlebarAreaDocumentStyleEnvironmentVariables();
} else {
const UADefinedVariable vars_to_remove[] = {
UADefinedVariable::kTitlebarAreaX,
UADefinedVariable::kTitlebarAreaY,
UADefinedVariable::kTitlebarAreaWidth,
UADefinedVariable::kTitlebarAreaHeight,
};
for (auto var_to_remove : vars_to_remove) {
vars.RemoveVariable(var_to_remove);
}
}
if (fire_event && window_controls_overlay_changed_delegate_) {
window_controls_overlay_changed_delegate_->WindowControlsOverlayChanged(
window_controls_overlay_rect_);
}
}
void LocalFrame::RegisterWindowControlsOverlayChangedDelegate(
WindowControlsOverlayChangedDelegate* delegate) {
window_controls_overlay_changed_delegate_ = delegate;
}
#endif
HitTestResult LocalFrame::HitTestResultForVisualViewportPos(
const gfx::Point& pos_in_viewport) {
gfx::Point root_frame_point(
GetPage()->GetVisualViewport().ViewportToRootFrame(pos_in_viewport));
HitTestLocation location(View()->ConvertFromRootFrame(root_frame_point));
HitTestResult result = GetEventHandler().HitTestResultAtLocation(
location, HitTestRequest::kReadOnly | HitTestRequest::kActive);
result.SetToShadowHostIfInUAShadowRoot();
return result;
}
void LocalFrame::DidChangeVisibleToHitTesting() {
// LayoutEmbeddedContent does not propagate style updates to descendants.
// Need to update the field manually.
for (Frame* child = Tree().FirstChild(); child;
child = child->Tree().NextSibling()) {
child->UpdateVisibleToHitTesting();
}
// The transform property tree node depends on visibility.
if (auto* view = View()->GetLayoutView()) {
view->SetNeedsPaintPropertyUpdate();
}
}
WebPrescientNetworking* LocalFrame::PrescientNetworking() {
if (!prescient_networking_) {
WebLocalFrameImpl* web_local_frame = WebLocalFrameImpl::FromFrame(this);
// There is no valid WebLocalFrame, return a nullptr to ignore pre* hints.
if (!web_local_frame)
return nullptr;
prescient_networking_ =
web_local_frame->Client()->CreatePrescientNetworking();
}
return prescient_networking_.get();
}
void LocalFrame::SetPrescientNetworkingForTesting(
std::unique_ptr<WebPrescientNetworking> prescient_networking) {
prescient_networking_ = std::move(prescient_networking);
}
mojom::blink::LocalFrameHost& LocalFrame::GetLocalFrameHostRemote() const {
return mojo_handler_->LocalFrameHostRemote();
}
mojom::blink::BackForwardCacheControllerHost&
LocalFrame::GetBackForwardCacheControllerHostRemote() {
return mojo_handler_->BackForwardCacheControllerHostRemote();
}
void LocalFrame::RegisterVirtualKeyboardOverlayChangedObserver(
VirtualKeyboardOverlayChangedObserver* observer) {
virtual_keyboard_overlay_changed_observers_.insert(observer);
}
void LocalFrame::NotifyVirtualKeyboardOverlayRectObservers(
const gfx::Rect& rect) const {
HeapVector<Member<VirtualKeyboardOverlayChangedObserver>, 32> observers(
virtual_keyboard_overlay_changed_observers_);
for (VirtualKeyboardOverlayChangedObserver* observer : observers)
observer->VirtualKeyboardOverlayChanged(rect);
}
void LocalFrame::RegisterContextMenuInsetsChangedObserver(
ContextMenuInsetsChangedObserver* observer) {
context_menu_insets_changed_observers_.insert(observer);
}
void LocalFrame::NotifyContextMenuInsetsObservers(
const gfx::Rect& safe_area) const {
gfx::Size viewport =
gfx::ScaleToEnclosingRect(gfx::Rect(GetOutermostMainFrameSize()),
1.0 / DevicePixelRatio())
.size();
// Convert from a rect within the window to viewport insets.
auto insets = gfx::Insets::TLBR(safe_area.y(), safe_area.x(),
viewport.height() - safe_area.bottom(),
viewport.width() - safe_area.right());
HeapVector<Member<ContextMenuInsetsChangedObserver>, 32> observers(
context_menu_insets_changed_observers_);
for (ContextMenuInsetsChangedObserver* observer : observers) {
observer->ContextMenuInsetsChanged(safe_area.IsEmpty() ? nullptr : &insets);
}
}
void LocalFrame::ShowInterestInElement(int nodeID) const {
Element* element = DynamicTo<Element>(DOMNodeIds::NodeForId(nodeID));
if (!element) {
return;
}
element->ShowInterestNow();
}
void LocalFrame::AddInspectorIssue(AuditsIssue info) {
if (GetPage()) {
GetPage()->GetInspectorIssueStorage().AddInspectorIssue(DomWindow(),
std::move(info));
}
}
void LocalFrame::CopyImageAtViewportPoint(const gfx::Point& viewport_point) {
HitTestResult result = HitTestResultForVisualViewportPos(viewport_point);
if (!IsA<HTMLCanvasElement>(result.InnerNodeOrImageMapImage()) &&
result.AbsoluteImageURL().IsEmpty()) {
// There isn't actually an image at these coordinates. Might be because
// the window scrolled while the context menu was open or because the page
// changed itself between when we thought there was an image here and when
// we actually tried to retrieve the image.
//
// FIXME: implement a cache of the most recent HitTestResult to avoid having
// to do two hit tests.
return;
}
// TODO(editing-dev): The use of UpdateStyleAndLayout
// needs to be audited. See http://crbug.com/590369 for more details.
GetDocument()->UpdateStyleAndLayout(DocumentUpdateReason::kEditing);
GetEditor().CopyImage(result);
}
void LocalFrame::SaveImageAt(const gfx::Point& window_point) {
gfx::Point viewport_position =
GetWidgetForLocalRoot()->DIPsToRoundedBlinkSpace(window_point);
Node* node = HitTestResultForVisualViewportPos(viewport_position)
.InnerNodeOrImageMapImage();
if (!node || !(IsA<HTMLCanvasElement>(*node) || IsA<HTMLImageElement>(*node)))
return;
String url = To<Element>(*node).ImageSourceURL();
if (!KURL(NullURL(), url).ProtocolIsData())
return;
auto params = mojom::blink::DownloadURLParams::New();
params->is_context_menu_save = true;
params->data_url_blob = DataURLToBlob(url);
GetLocalFrameHostRemote().DownloadURL(std::move(params));
}
void LocalFrame::MediaPlayerActionAtViewportPoint(
const gfx::Point& viewport_position,
const mojom::blink::MediaPlayerActionType type,
bool enable) {
HitTestResult result = HitTestResultForVisualViewportPos(viewport_position);
Node* node = result.InnerNode();
if (!IsA<HTMLVideoElement>(*node) && !IsA<HTMLAudioElement>(*node))
return;
auto* media_element = To<HTMLMediaElement>(node);
switch (type) {
case mojom::blink::MediaPlayerActionType::kLoop:
media_element->SetLoop(enable);
break;
case mojom::blink::MediaPlayerActionType::kControls:
media_element->SetUserWantsControlsVisible(enable);
break;
case mojom::blink::MediaPlayerActionType::kSaveVideoFrameAs:
if (auto* video = DynamicTo<HTMLVideoElement>(media_element); video) {
auto image = video->CreateStaticBitmapImage();
if (!image) {
return;
}
auto data_buffer = ImageDataBuffer::Create(image);
if (!data_buffer) {
return;
}
ImageEncodingMimeType encoding_mime_type =
ImageEncoderUtils::ToEncodingMimeType(
"image/png", ImageEncoderUtils::kEncodeReasonToDataURL);
String data_url =
data_buffer->ToDataURL(encoding_mime_type, /*quality=*/0);
auto params = mojom::blink::DownloadURLParams::New();
params->is_context_menu_save = true;
// Suggested name always starts with "videoframe_", plus the timestamp
// of the video frame in milliseconds.
auto timestamp_ms = base::saturated_cast<uint32_t>(
media_element->currentTime() * base::Time::kMillisecondsPerSecond);
params->suggested_name = "videoframe_" + String::Number(timestamp_ms);
params->data_url_blob = DataURLToBlob(data_url);
GetLocalFrameHostRemote().DownloadURL(std::move(params));
}
break;
case mojom::blink::MediaPlayerActionType::kCopyVideoFrame:
if (auto* video = DynamicTo<HTMLVideoElement>(media_element); video) {
auto image = video->CreateStaticBitmapImage();
if (image) {
GetEditor().CopyImage(result, image);
}
}
break;
case mojom::blink::MediaPlayerActionType::kPictureInPicture:
if (auto* video = DynamicTo<HTMLVideoElement>(media_element); video) {
if (enable) {
PictureInPictureController::From(node->GetDocument())
.EnterPictureInPicture(video, /*promise=*/nullptr);
} else {
PictureInPictureController::From(node->GetDocument())
.ExitPictureInPicture(video, nullptr);
}
}
break;
}
}
void LocalFrame::RequestVideoFrameAtWithBoundsHint(
const gfx::Point& viewport_position,
const gfx::Size& max_size,
int max_area,
base::OnceCallback<void(const SkBitmap&, const gfx::Rect&)> callback) {
HitTestResult result = HitTestResultForVisualViewportPos(viewport_position);
Node* node = result.InnerNode();
auto* video = DynamicTo<HTMLVideoElement>(node);
if (!video) {
std::move(callback).Run(SkBitmap(), gfx::Rect());
return;
}
// Scale to match the max dimensions if needed, to reduce data sent over IPC.
// This is to match the algorithm in gfx::ResizedImageForMaxDimensions().
// TODO(crbug.com/1508722): Revisit to see whether we need both `max_size` and
// `max_area`, which seems redundant.
gfx::Size size(video->videoWidth(), video->videoHeight());
if ((size.width() > max_size.width() || size.height() > max_size.height()) &&
size.GetArea() > max_area) {
double scale =
std::min(static_cast<double>(max_size.width()) / size.width(),
static_cast<double>(max_size.height()) / size.height());
int width = std::clamp<int>(scale * size.width(), 1, max_size.width());
int height = std::clamp<int>(scale * size.height(), 1, max_size.height());
size = gfx::Size(width, height);
}
auto image =
video->CreateStaticBitmapImage(/*allow_accelerated_images=*/true, size);
if (!image) {
std::move(callback).Run(SkBitmap(), gfx::Rect());
return;
}
auto bitmap = image->AsSkBitmapForCurrentFrame(kRespectImageOrientation);
// Only kN32_SkColorType bitmaps can be sent across IPC, so convert if
// necessary.
SkBitmap converted_bitmap;
if (bitmap.colorType() == kN32_SkColorType) {
converted_bitmap = bitmap;
} else {
SkImageInfo info = bitmap.info().makeColorType(kN32_SkColorType);
if (converted_bitmap.tryAllocPixels(info)) {
bitmap.readPixels(info, converted_bitmap.getPixels(),
converted_bitmap.rowBytes(), 0, 0);
}
}
// Get the bounds of the video element.
WebNode web_node(node);
WebElement web_element = web_node.To<WebElement>();
auto bounds = web_element.BoundsInWidget();
std::move(callback).Run(converted_bitmap, bounds);
}
void LocalFrame::DownloadURL(
const ResourceRequest& request,
network::mojom::blink::RedirectMode cross_origin_redirect_behavior) {
mojo::PendingRemote<mojom::blink::BlobURLToken> blob_url_token;
if (request.Url().ProtocolIs("blob")) {
DomWindow()->GetPublicURLManager().ResolveAsBlobURLToken(
request.Url(), blob_url_token.InitWithNewPipeAndPassReceiver(),
/*is_top_level_navigation=*/false);
}
DownloadURL(request, cross_origin_redirect_behavior,
std::move(blob_url_token));
}
void LocalFrame::DownloadURL(
const ResourceRequest& request,
network::mojom::blink::RedirectMode cross_origin_redirect_behavior,
mojo::PendingRemote<mojom::blink::BlobURLToken> blob_url_token) {
if (ShouldThrottleDownload())
return;
auto params = mojom::blink::DownloadURLParams::New();
const KURL& url = request.Url();
// Pass data URL through blob.
if (url.ProtocolIs("data")) {
params->url = KURL();
params->data_url_blob = DataURLToBlob(url.GetString());
} else {
params->url = url;
}
params->referrer = mojom::blink::Referrer::New();
params->referrer->url = KURL(request.ReferrerString());
params->referrer->policy = request.GetReferrerPolicy();
params->initiator_origin = request.RequestorOrigin();
if (request.GetSuggestedFilename().has_value())
params->suggested_name = *request.GetSuggestedFilename();
params->cross_origin_redirects = cross_origin_redirect_behavior;
params->blob_url_token = std::move(blob_url_token);
params->has_user_gesture = request.HasUserGesture();
GetLocalFrameHostRemote().DownloadURL(std::move(params));
}
void LocalFrame::AdvanceFocusForIME(mojom::blink::FocusType focus_type) {
auto* focused_frame = GetPage()->GetFocusController().FocusedFrame();
if (focused_frame != this)
return;
DCHECK(GetDocument());
Element* element = GetDocument()->FocusedElement();
if (!element)
return;
Element* next_element =
GetPage()->GetFocusController().NextFocusableElementForImeAndAutofill(
element, focus_type);
if (!next_element)
return;
next_element->scrollIntoViewIfNeeded(true /*centerIfNeeded*/);
next_element->Focus(FocusParams(FocusTrigger::kUserGesture));
}
void LocalFrame::PostMessageEvent(
const std::optional<RemoteFrameToken>& source_frame_token,
const String& source_origin,
const String& target_origin,
BlinkTransferableMessage message) {
probe::FrameRelatedTask probe(DomWindow());
TRACE_EVENT0("blink", "LocalFrame::PostMessageEvent");
RemoteFrame* source_frame = SourceFrameForOptionalToken(source_frame_token);
// We must pass in the target_origin to do the security check on this side,
// since it may have changed since the original postMessage call was made.
scoped_refptr<SecurityOrigin> target_security_origin;
if (!target_origin.empty()) {
target_security_origin = SecurityOrigin::CreateFromString(target_origin);
}
// Preparation of the MessageEvent.
MessageEvent* message_event = MessageEvent::Create();
DOMWindow* window = nullptr;
if (source_frame)
window = source_frame->DomWindow();
GCedMessagePortArray* ports = nullptr;
if (GetDocument()) {
ports = MessagePort::EntanglePorts(*GetDocument()->GetExecutionContext(),
std::move(message.ports));
}
// The |message.user_activation| only conveys the sender |Frame|'s user
// activation state to receiver JS. This is never used for activating the
// receiver (or any other) |Frame|.
UserActivation* user_activation = nullptr;
if (message.user_activation) {
user_activation = MakeGarbageCollected<UserActivation>(
message.user_activation->has_been_active,
message.user_activation->was_active);
}
message_event->initMessageEvent(
event_type_names::kMessage, false, false, std::move(message.message),
source_origin, "" /*lastEventId*/, window, ports, user_activation,
message.delegated_capability);
// If the agent cluster id had a value it means this was locked when it
// was serialized.
if (message.locked_to_sender_agent_cluster)
message_event->LockToAgentCluster();
// Finally dispatch the message to the DOM Window.
DomWindow()->DispatchMessageEventWithOriginCheck(
target_security_origin.get(), message_event,
std::make_unique<SourceLocation>(String(), String(), 0, 0, nullptr),
message.sender_agent_cluster_id);
}
bool LocalFrame::ShouldThrottleDownload() {
const auto now = base::TimeTicks::Now();
if (num_burst_download_requests_ == 0) {
burst_download_start_time_ = now;
} else if (num_burst_download_requests_ >= kBurstDownloadLimit) {
static constexpr auto kBurstDownloadLimitResetInterval = base::Seconds(1);
if (now - burst_download_start_time_ > kBurstDownloadLimitResetInterval) {
num_burst_download_requests_ = 1;
burst_download_start_time_ = now;
return false;
}
return true;
}
num_burst_download_requests_++;
return false;
}
#if BUILDFLAG(IS_MAC)
void LocalFrame::ResetTextInputHostForTesting() {
mojo_handler_->ResetTextInputHostForTesting();
}
void LocalFrame::RebindTextInputHostForTesting() {
mojo_handler_->RebindTextInputHostForTesting();
}
#endif
Frame* LocalFrame::GetProvisionalOwnerFrame() {
DCHECK(IsProvisional());
if (Owner()) {
// Since `this` is a provisional frame, its owner's `ContentFrame()` will
// be the old LocalFrame.
return Owner()->ContentFrame();
}
return GetPage()->MainFrame();
}
namespace {
// TODO(editing-dev): We should move |CreateMarkupInRect()| to
// "core/editing/serializers/Serialization.cpp".
String CreateMarkupInRect(LocalFrame* frame,
const gfx::Point& start_point,
const gfx::Point& end_point) {
VisiblePosition start_visible_position = CreateVisiblePosition(
PositionForContentsPointRespectingEditingBoundary(start_point, frame));
VisiblePosition end_visible_position = CreateVisiblePosition(
PositionForContentsPointRespectingEditingBoundary(end_point, frame));
Position start_position = start_visible_position.DeepEquivalent();
Position end_position = end_visible_position.DeepEquivalent();
// document() will return null if -webkit-user-select is set to none.
if (!start_position.GetDocument() || !end_position.GetDocument())
return String();
const CreateMarkupOptions create_markup_options =
CreateMarkupOptions::Builder()
.SetShouldAnnotateForInterchange(true)
.SetShouldResolveURLs(kResolveNonLocalURLs)
.Build();
if (start_position.CompareTo(end_position) <= 0) {
return CreateMarkup(start_position, end_position, create_markup_options);
}
return CreateMarkup(end_position, start_position, create_markup_options);
}
} // namespace
void LocalFrame::ExtractSmartClipDataInternal(const gfx::Rect& rect_in_viewport,
String& clip_text,
String& clip_html,
gfx::Rect& clip_rect) {
// TODO(mahesh.ma): Check clip_data even after use-zoom-for-dsf is enabled.
SmartClipData clip_data = SmartClip(this).DataForRect(rect_in_viewport);
clip_text = clip_data.ClipData();
clip_rect = clip_data.RectInViewport();
gfx::Point start_point(rect_in_viewport.x(), rect_in_viewport.y());
gfx::Point end_point(rect_in_viewport.x() + rect_in_viewport.width(),
rect_in_viewport.y() + rect_in_viewport.height());
clip_html = CreateMarkupInRect(this, View()->ViewportToFrame(start_point),
View()->ViewportToFrame(end_point));
}
void LocalFrame::CreateTextFragmentHandler() {
text_fragment_handler_ = MakeGarbageCollected<TextFragmentHandler>(this);
}
void LocalFrame::BindTextFragmentReceiver(
mojo::PendingReceiver<mojom::blink::TextFragmentReceiver> receiver) {
if (IsDetached())
return;
if (!text_fragment_handler_)
CreateTextFragmentHandler();
text_fragment_handler_->BindTextFragmentReceiver(std::move(receiver));
}
SpellChecker& LocalFrame::GetSpellChecker() const {
DCHECK(DomWindow());
return DomWindow()->GetSpellChecker();
}
InputMethodController& LocalFrame::GetInputMethodController() const {
DCHECK(DomWindow());
return DomWindow()->GetInputMethodController();
}
TextSuggestionController& LocalFrame::GetTextSuggestionController() const {
DCHECK(DomWindow());
return DomWindow()->GetTextSuggestionController();
}
void LocalFrame::WriteIntoTrace(perfetto::TracedValue ctx) const {
perfetto::TracedDictionary dict = std::move(ctx).WriteDictionary();
dict.Add("document", GetDocument());
dict.Add("is_main_frame", IsMainFrame());
dict.Add("is_outermost_main_frame", IsOutermostMainFrame());
dict.Add("is_cross_origin_to_parent", IsCrossOriginToParentOrOuterDocument());
dict.Add("is_cross_origin_to_outermost_main_frame",
IsCrossOriginToOutermostMainFrame());
}
mojo::PendingRemote<mojom::blink::BlobURLStore>
LocalFrame::GetBlobUrlStorePendingRemote() {
mojo::PendingRemote<mojom::blink::BlobURLStore> pending_remote;
GetBrowserInterfaceBroker().GetInterface(
pending_remote.InitWithNewPipeAndPassReceiver());
return pending_remote;
}
#if !BUILDFLAG(IS_ANDROID)
void LocalFrame::SetTitlebarAreaDocumentStyleEnvironmentVariables() const {
DCHECK(is_window_controls_overlay_visible_);
DocumentStyleEnvironmentVariables& vars =
GetDocument()->GetStyleEngine().EnsureEnvironmentVariables();
vars.SetVariable(
UADefinedVariable::kTitlebarAreaX,
StyleEnvironmentVariables::FormatPx(window_controls_overlay_rect_.x()));
vars.SetVariable(
UADefinedVariable::kTitlebarAreaY,
StyleEnvironmentVariables::FormatPx(window_controls_overlay_rect_.y()));
vars.SetVariable(UADefinedVariable::kTitlebarAreaWidth,
StyleEnvironmentVariables::FormatPx(
window_controls_overlay_rect_.width()));
vars.SetVariable(UADefinedVariable::kTitlebarAreaHeight,
StyleEnvironmentVariables::FormatPx(
window_controls_overlay_rect_.height()));
}
void LocalFrame::MaybeUpdateWindowControlsOverlayWithNewZoomLevel() {
// |window_controls_overlay_rect_| is only set for local root.
if (!is_window_controls_overlay_visible_ || !IsLocalRoot())
return;
DCHECK(!window_controls_overlay_rect_in_dips_.IsEmpty());
UpdateWindowControlsOverlay(window_controls_overlay_rect_in_dips_);
}
#endif // !BUILDFLAG(IS_ANDROID)
void LocalFrame::SetNotRestoredReasons(
mojom::blink::BackForwardCacheNotRestoredReasonsPtr not_restored_reasons) {
// Back/forward cache is only enabled for outermost main frame.
DCHECK(IsOutermostMainFrame());
not_restored_reasons_ = mojo::Clone(not_restored_reasons);
}
const mojom::blink::BackForwardCacheNotRestoredReasonsPtr&
LocalFrame::GetNotRestoredReasons() {
// Back/forward cache is only enabled for the outermost main frames, and the
// web exposed API returns non-null values only for the outermost main frames.
DCHECK(IsOutermostMainFrame());
return not_restored_reasons_;
}
void LocalFrame::SetNavigationConfidence(
double randomized_trigger_rate,
mojom::blink::ConfidenceLevel confidence) {
DCHECK(IsOutermostMainFrame());
loader_.GetDocumentLoader()->GetTiming().SetRandomizedConfidence(
std::make_pair(randomized_trigger_rate, confidence));
}
void LocalFrame::AddScrollSnapshotClient(ScrollSnapshotClient& client) {
scroll_snapshot_clients_.insert(&client);
}
void LocalFrame::UpdateScrollSnapshots() {
// TODO(xiaochengh): Can we DCHECK that is is done at the beginning of a frame
// and is done exactly once?
for (auto& client : scroll_snapshot_clients_)
client->UpdateSnapshot();
}
bool LocalFrame::ValidateScrollSnapshotClients() {
bool valid = true;
for (auto& client : scroll_snapshot_clients_) {
valid &= client->ValidateSnapshot();
}
return valid;
}
void LocalFrame::ClearScrollSnapshotClients() {
scroll_snapshot_clients_.clear();
}
void LocalFrame::ScheduleNextServiceForScrollSnapshotClients() {
for (auto& client : scroll_snapshot_clients_) {
if (client->ShouldScheduleNextService()) {
View()->ScheduleAnimation();
return;
}
}
}
void LocalFrame::CheckPositionAnchorsForCssVisibilityChanges() {
for (auto& client : scroll_snapshot_clients_) {
if (AnchorPositionScrollData* scroll_data =
DynamicTo<AnchorPositionScrollData>(client.Get())) {
if (auto* observer = scroll_data->GetAnchorPositionVisibilityObserver()) {
observer->UpdateForCssAnchorVisibility();
}
}
}
}
void LocalFrame::CheckPositionAnchorsForChainedVisibilityChanges() {
AnchorPositionVisibilityObserver::UpdateForChainedAnchorVisibility(
scroll_snapshot_clients_);
}
bool LocalFrame::IsSameOrigin() {
const SecurityOrigin* security_origin =
GetSecurityContext()->GetSecurityOrigin();
const SecurityOrigin* top_security_origin =
Tree().Top().GetSecurityContext()->GetSecurityOrigin();
return security_origin->IsSameOriginWith(top_security_origin);
}
bool LocalFrame::ImagesEnabled() {
DCHECK(!IsDetached());
// If this is called in the middle of detach, GetDocumentLoader() might
// already be nullptr.
if (!loader_.GetDocumentLoader()) {
return false;
}
bool allow_image_renderer = GetSettings()->GetImagesEnabled();
bool allow_image_content_setting =
loader_.GetDocumentLoader()->GetContentSettings()->allow_image;
return allow_image_renderer && allow_image_content_setting;
}
bool LocalFrame::ScriptEnabled() {
DCHECK(!IsDetached());
// If this is called in the middle of detach, GetDocumentLoader() might
// already be nullptr.
if (!loader_.GetDocumentLoader()) {
return false;
}
bool allow_script_renderer = GetSettings()->GetScriptEnabled();
bool allow_script_content_setting =
loader_.GetDocumentLoader()->GetContentSettings()->allow_script;
return allow_script_renderer && allow_script_content_setting;
}
const WebPrintParams& LocalFrame::GetPrintParams() const {
// If this fails, it's probably because nobody called StartPrinting().
DCHECK(GetDocument()->Printing());
return print_params_;
}
mojo::PendingRemote<mojom::blink::NavigationStateKeepAliveHandle>
LocalFrame::IssueKeepAliveHandle() {
mojo::PendingRemote<mojom::blink::NavigationStateKeepAliveHandle>
keep_alive_remote;
GetLocalFrameHostRemote().IssueKeepAliveHandle(
keep_alive_remote.InitWithNewPipeAndPassReceiver());
return keep_alive_remote;
}
WebLinkPreviewTriggerer* LocalFrame::GetOrCreateLinkPreviewTriggerer() {
EnsureLinkPreviewTriggererInitialized();
return link_preview_triggerer_.get();
}
void LocalFrame::EnsureLinkPreviewTriggererInitialized() {
if (is_link_preivew_triggerer_initialized_) {
return;
}
CHECK(!link_preview_triggerer_);
WebLocalFrameImpl* web_local_frame = WebLocalFrameImpl::FromFrame(this);
if (!web_local_frame) {
return;
}
link_preview_triggerer_ =
web_local_frame->Client()->CreateLinkPreviewTriggerer();
is_link_preivew_triggerer_initialized_ = true;
}
void LocalFrame::SetLinkPreviewTriggererForTesting(
std::unique_ptr<WebLinkPreviewTriggerer> trigger) {
link_preview_triggerer_ = std::move(trigger);
is_link_preivew_triggerer_initialized_ = true;
}
void LocalFrame::AllowStorageAccessAndNotify(
blink::WebContentSettingsClient::StorageType storage_type,
base::OnceCallback<void(bool)> callback) {
mojom::blink::StorageTypeAccessed mojo_storage_type =
ToMojoStorageType(storage_type);
auto wrapped_callback = WTF::BindOnce(&LocalFrame::OnStorageAccessCallback,
WrapWeakPersistent(this),
std::move(callback), mojo_storage_type);
if (WebContentSettingsClient* content_settings_client =
GetContentSettingsClient()) {
content_settings_client->AllowStorageAccess(storage_type,
std::move(wrapped_callback));
} else {
std::move(wrapped_callback).Run(true);
}
}
bool LocalFrame::AllowStorageAccessSyncAndNotify(
blink::WebContentSettingsClient::StorageType storage_type) {
bool allowed = true;
if (WebContentSettingsClient* content_settings_client =
GetContentSettingsClient()) {
allowed = content_settings_client->AllowStorageAccessSync(storage_type);
}
GetLocalFrameHostRemote().NotifyStorageAccessed(
ToMojoStorageType(storage_type), !allowed);
return allowed;
}
void LocalFrame::OnStorageAccessCallback(
base::OnceCallback<void(bool)> callback,
mojom::blink::StorageTypeAccessed storage_type,
bool is_allowed) {
GetLocalFrameHostRemote().NotifyStorageAccessed(storage_type, !is_allowed);
std::move(callback).Run(is_allowed);
}
void LocalFrame::NotifyFrameVisibilityChanged(
mojom::blink::FrameVisibility visibility) {
// Iterate on a copy of the vector to avoid invalidating the iterator if
// `FrameVisibilityChanged` happens to remove the observer from
// `frame_visibility_observers_`.
HeapVector<Member<FrameVisibilityObserver>>
frame_visibility_observers_as_vector(frame_visibility_observers_);
for (auto observer : frame_visibility_observers_as_vector) {
observer->FrameVisibilityChanged(visibility);
}
}
} // namespace blink
|