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
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include <array>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include "base/auto_reset.h"
#include "base/command_line.h"
#include "base/functional/bind.h"
#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/simple_test_clock.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "chrome/browser/content_settings/cookie_settings_factory.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/content_settings/mock_settings_observer.h"
#include "chrome/common/url_constants.h"
#include "chrome/test/base/testing_profile.h"
#include "components/client_hints/common/client_hints.h"
#include "components/content_settings/core/browser/content_settings_mock_observer.h"
#include "components/content_settings/core/browser/content_settings_pref_provider.h"
#include "components/content_settings/core/browser/content_settings_registry.h"
#include "components/content_settings/core/browser/content_settings_rule.h"
#include "components/content_settings/core/browser/content_settings_uma_util.h"
#include "components/content_settings/core/browser/content_settings_utils.h"
#include "components/content_settings/core/browser/cookie_settings.h"
#include "components/content_settings/core/browser/user_modifiable_provider.h"
#include "components/content_settings/core/browser/website_settings_info.h"
#include "components/content_settings/core/browser/website_settings_registry.h"
#include "components/content_settings/core/common/content_settings.h"
#include "components/content_settings/core/common/content_settings_constraints.h"
#include "components/content_settings/core/common/content_settings_pattern.h"
#include "components/content_settings/core/common/content_settings_types.h"
#include "components/content_settings/core/common/content_settings_utils.h"
#include "components/content_settings/core/common/features.h"
#include "components/content_settings/core/common/host_indexed_content_settings.h"
#include "components/content_settings/core/common/pref_names.h"
#include "components/content_settings/core/test/content_settings_mock_provider.h"
#include "components/content_settings/core/test/content_settings_test_utils.h"
#include "components/keyed_service/core/refcounted_keyed_service.h"
#include "components/permissions/features.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/scoped_user_pref_update.h"
#include "components/safe_browsing/core/common/features.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "content/public/test/browser_task_environment.h"
#include "net/base/schemeful_site.h"
#include "net/cookies/site_for_cookies.h"
#include "net/cookies/static_cookie_policy.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/permissions/permission_utils.h"
#include "url/gurl.h"
using ::testing::_;
using ::testing::MockFunction;
using ::testing::Return;
namespace {
base::Time GetSettingLastModifiedDate(HostContentSettingsMap* map,
GURL primary_url,
GURL secondary_url,
ContentSettingsType type) {
content_settings::SettingInfo info;
map->GetWebsiteSetting(primary_url, secondary_url, type, &info);
return info.metadata.last_modified();
}
} // namespace
class MockUserModifiableProvider
: public content_settings::UserModifiableProvider {
public:
~MockUserModifiableProvider() override = default;
MOCK_CONST_METHOD3(GetRuleIterator,
std::unique_ptr<content_settings::RuleIterator>(
ContentSettingsType,
bool,
const content_settings::PartitionKey&));
MOCK_METHOD6(SetWebsiteSetting,
bool(const ContentSettingsPattern&,
const ContentSettingsPattern&,
ContentSettingsType,
base::Value&&,
const content_settings::ContentSettingConstraints&,
const content_settings::PartitionKey&));
MOCK_METHOD2(ClearAllContentSettingsRules,
void(ContentSettingsType,
const content_settings::PartitionKey&));
MOCK_METHOD0(ShutdownOnUIThread, void());
MOCK_METHOD5(UpdateLastUsedTime,
bool(const GURL& primary_url,
const GURL& secondary_url,
ContentSettingsType content_type,
const base::Time time,
const content_settings::PartitionKey& partition_key));
MOCK_METHOD4(UpdateLastVisitTime,
bool(const ContentSettingsPattern& primary_pattern,
const ContentSettingsPattern& secondary_pattern,
ContentSettingsType content_type,
const content_settings::PartitionKey& partition_key));
MOCK_METHOD4(ResetLastVisitTime,
bool(const ContentSettingsPattern& primary_pattern,
const ContentSettingsPattern& secondary_pattern,
ContentSettingsType content_type,
const content_settings::PartitionKey& partition_key));
MOCK_METHOD5(RenewContentSetting,
std::optional<base::TimeDelta>(
const GURL& primary_url,
const GURL& secondary_url,
ContentSettingsType content_type,
std::optional<ContentSetting> setting_to_match,
const content_settings::PartitionKey& partition_key));
MOCK_METHOD1(SetClockForTesting, void(const base::Clock*));
};
class HostContentSettingsMapTest : public testing::Test {
public:
HostContentSettingsMapTest()
: task_environment_(base::test::TaskEnvironment::TimeSource::MOCK_TIME) {
}
void FastForwardTime(base::TimeDelta delta) {
task_environment_.FastForwardBy(delta);
}
protected:
const std::string& GetPrefName(ContentSettingsType type) {
return content_settings::WebsiteSettingsRegistry::GetInstance()
->Get(type)
->pref_name();
}
content::BrowserTaskEnvironment task_environment_;
base::test::ScopedFeatureList feature_list_;
};
// Wrapper to TestingProfile to reduce test boilerplates, by keeping a fixed
// |content_type| so caller only need to specify it once.
class TesterForType {
public:
TesterForType(TestingProfile *profile, ContentSettingsType content_type)
: prefs_(profile->GetTestingPrefService()),
host_content_settings_map_(
HostContentSettingsMapFactory::GetForProfile(profile)),
content_type_(content_type) {
switch (content_type_) {
case ContentSettingsType::COOKIES:
policy_default_setting_ = prefs::kManagedDefaultCookiesSetting;
break;
case ContentSettingsType::POPUPS:
policy_default_setting_ = prefs::kManagedDefaultPopupsSetting;
break;
case ContentSettingsType::ADS:
policy_default_setting_ = prefs::kManagedDefaultAdsSetting;
break;
default:
// Add support as needed.
NOTREACHED();
}
}
TesterForType(const TesterForType&) = delete;
TesterForType& operator=(const TesterForType&) = delete;
void ClearPolicyDefault() {
prefs_->RemoveManagedPref(policy_default_setting_);
}
void SetPolicyDefault(ContentSetting setting) {
prefs_->SetManagedPref(policy_default_setting_,
std::make_unique<base::Value>(setting));
}
void AddUserException(std::string exception,
ContentSetting content_settings) {
ContentSettingsPattern pattern =
ContentSettingsPattern::FromString(exception);
host_content_settings_map_->SetContentSettingCustomScope(
pattern, ContentSettingsPattern::Wildcard(), content_type_,
content_settings);
}
// Wrapper to query GetWebsiteSetting(), and only return the source.
content_settings::SettingSource GetSettingSourceForURL(
const std::string& url_str) {
GURL url(url_str);
content_settings::SettingInfo setting_info;
base::Value result = host_content_settings_map_->GetWebsiteSetting(
url, url, content_type_, &setting_info);
return setting_info.source;
}
private:
raw_ptr<sync_preferences::TestingPrefServiceSyncable> prefs_;
raw_ptr<HostContentSettingsMap> host_content_settings_map_;
ContentSettingsType content_type_;
const char* policy_default_setting_;
};
// For building HostContentSettingsMap without HostContentSettingsMapFactory.
// Shuts down HostContentSettingsMap properly on destruction.
class ScopedMapNoFactoryTester {
public:
scoped_refptr<HostContentSettingsMap> map;
explicit ScopedMapNoFactoryTester(TestingProfile& profile) {
map = base::MakeRefCounted<HostContentSettingsMap>(
profile.GetPrefs(),
/*is_off_the_record=*/false,
/*store_last_modified=*/false,
/*restore_session=*/false,
/*should_record_metrics=*/false);
}
~ScopedMapNoFactoryTester() { map->ShutdownOnUIThread(); }
};
TEST_F(HostContentSettingsMapTest, DefaultValues) {
TestingProfile profile;
HostContentSettingsMap* host_content_settings_map =
HostContentSettingsMapFactory::GetForProfile(&profile);
// Check setting defaults.
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetDefaultContentSetting(
ContentSettingsType::JAVASCRIPT));
host_content_settings_map->SetDefaultContentSetting(
ContentSettingsType::JAVASCRIPT, CONTENT_SETTING_BLOCK);
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetDefaultContentSetting(
ContentSettingsType::JAVASCRIPT));
EXPECT_EQ(CONTENT_SETTING_ALLOW, host_content_settings_map->GetContentSetting(
GURL(chrome::kChromeUINewTabURL),
GURL(chrome::kChromeUINewTabURL),
ContentSettingsType::JAVASCRIPT));
host_content_settings_map->SetDefaultContentSetting(
ContentSettingsType::POPUPS, CONTENT_SETTING_ALLOW);
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetDefaultContentSetting(
ContentSettingsType::POPUPS));
}
TEST_F(HostContentSettingsMapTest, IndividualSettings) {
TestingProfile profile;
HostContentSettingsMap* host_content_settings_map =
HostContentSettingsMapFactory::GetForProfile(&profile);
// Check returning individual settings.
GURL host("http://example.com/");
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetContentSetting(
host, host, ContentSettingsType::COOKIES));
host_content_settings_map->SetContentSettingDefaultScope(
host, GURL(), ContentSettingsType::COOKIES, CONTENT_SETTING_DEFAULT);
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetContentSetting(
host, host, ContentSettingsType::COOKIES));
host_content_settings_map->SetContentSettingDefaultScope(
host, GURL(), ContentSettingsType::COOKIES, CONTENT_SETTING_BLOCK);
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetContentSetting(
host, host, ContentSettingsType::COOKIES));
// Check returning all settings for a host.
host_content_settings_map->SetContentSettingDefaultScope(
host, GURL(), ContentSettingsType::JAVASCRIPT, CONTENT_SETTING_BLOCK);
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetContentSetting(
host, host, ContentSettingsType::JAVASCRIPT));
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetContentSetting(
host, host, ContentSettingsType::POPUPS));
EXPECT_EQ(CONTENT_SETTING_ASK,
host_content_settings_map->GetContentSetting(
host, host, ContentSettingsType::GEOLOCATION));
EXPECT_EQ(CONTENT_SETTING_ASK,
host_content_settings_map->GetContentSetting(
host, host, ContentSettingsType::NOTIFICATIONS));
host_content_settings_map->SetContentSettingDefaultScope(
host, GURL(), ContentSettingsType::AUTOPLAY, CONTENT_SETTING_BLOCK);
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetContentSetting(
host, host, ContentSettingsType::AUTOPLAY));
// Check returning all hosts for a setting.
GURL host2("http://example.org/");
host_content_settings_map->SetContentSettingDefaultScope(
host2, GURL(), ContentSettingsType::JAVASCRIPT, CONTENT_SETTING_BLOCK);
ContentSettingsForOneType host_settings =
host_content_settings_map->GetSettingsForOneType(
ContentSettingsType::JAVASCRIPT);
// |host_settings| contains the default setting and 2 exception.
EXPECT_EQ(3U, host_settings.size());
host_settings = host_content_settings_map->GetSettingsForOneType(
ContentSettingsType::POPUPS);
// |host_settings| contains only the default setting.
EXPECT_EQ(1U, host_settings.size());
}
TEST_F(HostContentSettingsMapTest, GetWebsiteSettingsForOneType) {
TestingProfile profile;
auto hosts = std::to_array<GURL>(
{GURL("https://example1.com/"), GURL("https://example2.com/")});
HostContentSettingsMap* host_content_settings_map =
HostContentSettingsMapFactory::GetForProfile(&profile);
ContentSettingsForOneType client_hints_settings =
host_content_settings_map->GetSettingsForOneType(
ContentSettingsType::CLIENT_HINTS);
EXPECT_EQ(0U, client_hints_settings.size());
// Add setting for hosts[0].
base::Value client_hint_value(42);
base::Value::Dict client_hints_dictionary;
client_hints_dictionary.Set(client_hints::kClientHintsSettingKey,
{std::move(client_hint_value)});
host_content_settings_map->SetWebsiteSettingDefaultScope(
hosts[0], GURL(), ContentSettingsType::CLIENT_HINTS,
base::Value(client_hints_dictionary.Clone()));
// Reading the settings should now return one setting.
client_hints_settings = host_content_settings_map->GetSettingsForOneType(
ContentSettingsType::CLIENT_HINTS);
EXPECT_EQ(1U, client_hints_settings.size());
for (size_t i = 0; i < client_hints_settings.size(); ++i) {
EXPECT_EQ(ContentSettingsPattern::FromURLNoWildcard(hosts[i]),
client_hints_settings.at(i).primary_pattern);
EXPECT_EQ(ContentSettingsPattern::Wildcard(),
client_hints_settings.at(i).secondary_pattern);
EXPECT_EQ(client_hints_dictionary,
client_hints_settings.at(i).setting_value);
}
// Add setting for hosts[1].
host_content_settings_map->SetWebsiteSettingDefaultScope(
hosts[1], GURL(), ContentSettingsType::CLIENT_HINTS,
base::Value(client_hints_dictionary.Clone()));
// Reading the settings should now return two settings.
client_hints_settings = host_content_settings_map->GetSettingsForOneType(
ContentSettingsType::CLIENT_HINTS);
EXPECT_EQ(2U, client_hints_settings.size());
for (size_t i = 0; i < client_hints_settings.size(); ++i) {
EXPECT_EQ(ContentSettingsPattern::FromURLNoWildcard(hosts[i]),
client_hints_settings.at(i).primary_pattern);
EXPECT_EQ(ContentSettingsPattern::Wildcard(),
client_hints_settings.at(i).secondary_pattern);
EXPECT_EQ(client_hints_dictionary,
client_hints_settings.at(i).setting_value);
}
// Add settings again for hosts[0].
host_content_settings_map->SetWebsiteSettingDefaultScope(
hosts[0], GURL(), ContentSettingsType::CLIENT_HINTS,
base::Value(client_hints_dictionary.Clone()));
// Reading the settings should still return two settings.
client_hints_settings = host_content_settings_map->GetSettingsForOneType(
ContentSettingsType::CLIENT_HINTS);
EXPECT_EQ(2U, client_hints_settings.size());
for (size_t i = 0; i < client_hints_settings.size(); ++i) {
EXPECT_EQ(ContentSettingsPattern::FromURLNoWildcard(hosts[i]),
client_hints_settings.at(i).primary_pattern);
EXPECT_EQ(ContentSettingsPattern::Wildcard(),
client_hints_settings.at(i).secondary_pattern);
EXPECT_EQ(client_hints_dictionary,
client_hints_settings.at(i).setting_value);
}
}
TEST_F(HostContentSettingsMapTest, Clear) {
TestingProfile profile;
HostContentSettingsMap* host_content_settings_map =
HostContentSettingsMapFactory::GetForProfile(&profile);
// Check clearing one type.
GURL host("http://example.org/");
GURL host2("http://example.net/");
host_content_settings_map->SetContentSettingDefaultScope(
host2, GURL(), ContentSettingsType::COOKIES, CONTENT_SETTING_BLOCK);
host_content_settings_map->SetContentSettingDefaultScope(
host, GURL(), ContentSettingsType::COOKIES, CONTENT_SETTING_BLOCK);
host_content_settings_map->SetContentSettingDefaultScope(
host2, GURL(), ContentSettingsType::COOKIES, CONTENT_SETTING_BLOCK);
host_content_settings_map->ClearSettingsForOneType(
ContentSettingsType::COOKIES);
ContentSettingsForOneType host_settings =
host_content_settings_map->GetSettingsForOneType(
ContentSettingsType::COOKIES);
// |host_settings| contains only the default setting.
EXPECT_EQ(1U, host_settings.size());
}
TEST_F(HostContentSettingsMapTest, Patterns) {
TestingProfile profile;
HostContentSettingsMap* host_content_settings_map =
HostContentSettingsMapFactory::GetForProfile(&profile);
GURL host1("http://example.com/");
GURL host2("http://www.example.com/");
GURL host3("http://example.org/");
ContentSettingsPattern pattern1 =
ContentSettingsPattern::FromString("[*.]example.com");
ContentSettingsPattern pattern2 =
ContentSettingsPattern::FromString("example.org");
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetContentSetting(
host1, host1, ContentSettingsType::COOKIES));
host_content_settings_map->SetContentSettingCustomScope(
pattern1, ContentSettingsPattern::Wildcard(),
ContentSettingsType::COOKIES, CONTENT_SETTING_BLOCK);
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetContentSetting(
host1, host1, ContentSettingsType::COOKIES));
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetContentSetting(
host2, host2, ContentSettingsType::COOKIES));
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetContentSetting(
host3, host3, ContentSettingsType::COOKIES));
host_content_settings_map->SetContentSettingCustomScope(
pattern2, ContentSettingsPattern::Wildcard(),
ContentSettingsType::COOKIES, CONTENT_SETTING_BLOCK);
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetContentSetting(
host3, host3, ContentSettingsType::COOKIES));
}
// Changing a setting for one origin doesn't affect subdomains.
TEST_F(HostContentSettingsMapTest, Origins) {
TestingProfile profile;
HostContentSettingsMap* host_content_settings_map =
HostContentSettingsMapFactory::GetForProfile(&profile);
GURL host1("http://example.com/");
GURL host2("http://www.example.com/");
GURL host3("http://example.org/");
GURL host4("http://example.com:8080");
ContentSettingsPattern pattern =
ContentSettingsPattern::FromURLNoWildcard(host1);
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetContentSetting(
host1, host1, ContentSettingsType::COOKIES));
host_content_settings_map->SetContentSettingCustomScope(
pattern, ContentSettingsPattern::Wildcard(), ContentSettingsType::COOKIES,
CONTENT_SETTING_BLOCK);
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetContentSetting(
host1, host1, ContentSettingsType::COOKIES));
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetContentSetting(
host2, host2, ContentSettingsType::COOKIES));
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetContentSetting(
host3, host3, ContentSettingsType::COOKIES));
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetContentSetting(
host4, host4, ContentSettingsType::COOKIES));
}
TEST_F(HostContentSettingsMapTest, Observer) {
TestingProfile profile;
// Use ScopedMapNoFactoryTester in order to enable the test to ignore
// RevokedPermissionsService and
// ContentSettingsType::REVOKED_UNUSED_SITE_PERMISSIONS.
ScopedMapNoFactoryTester map_tester(profile);
HostContentSettingsMap* host_content_settings_map = map_tester.map.get();
MockSettingsObserver observer(host_content_settings_map);
GURL host("http://example.com/");
ContentSettingsPattern primary_pattern =
ContentSettingsPattern::FromString("[*.]example.com");
ContentSettingsPattern secondary_pattern =
ContentSettingsPattern::Wildcard();
EXPECT_CALL(observer, OnContentSettingsChanged(host_content_settings_map,
ContentSettingsType::COOKIES,
false, primary_pattern,
secondary_pattern, false));
host_content_settings_map->SetContentSettingDefaultScope(
host, GURL(), ContentSettingsType::COOKIES, CONTENT_SETTING_ALLOW);
::testing::Mock::VerifyAndClearExpectations(&observer);
EXPECT_CALL(observer, OnContentSettingsChanged(host_content_settings_map,
ContentSettingsType::COOKIES,
false, _, _, true));
host_content_settings_map->ClearSettingsForOneType(
ContentSettingsType::COOKIES);
::testing::Mock::VerifyAndClearExpectations(&observer);
EXPECT_CALL(observer, OnContentSettingsChanged(host_content_settings_map,
ContentSettingsType::COOKIES,
false, _, _, true));
host_content_settings_map->SetDefaultContentSetting(
ContentSettingsType::COOKIES, CONTENT_SETTING_BLOCK);
}
TEST_F(HostContentSettingsMapTest, ObserveDefaultPref) {
TestingProfile profile;
HostContentSettingsMap* host_content_settings_map =
HostContentSettingsMapFactory::GetForProfile(&profile);
PrefService* prefs = profile.GetPrefs();
GURL host("http://example.com");
host_content_settings_map->SetDefaultContentSetting(
ContentSettingsType::COOKIES, CONTENT_SETTING_BLOCK);
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetContentSetting(
host, host, ContentSettingsType::COOKIES));
const content_settings::WebsiteSettingsInfo* info =
content_settings::WebsiteSettingsRegistry::GetInstance()->Get(
ContentSettingsType::COOKIES);
// Clearing the backing pref should also clear the internal cache.
prefs->ClearPref(info->default_value_pref_name());
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetContentSetting(
host, host, ContentSettingsType::COOKIES));
// Resetting the pref to its previous value should update the cache.
prefs->SetInteger(info->default_value_pref_name(), CONTENT_SETTING_BLOCK);
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetContentSetting(
host, host, ContentSettingsType::COOKIES));
}
TEST_F(HostContentSettingsMapTest, ObserveExceptionPref) {
TestingProfile profile;
HostContentSettingsMap* host_content_settings_map =
HostContentSettingsMapFactory::GetForProfile(&profile);
PrefService* prefs = profile.GetPrefs();
// Make a copy of the default pref value so we can reset it later.
base::Value default_value =
prefs->FindPreference(GetPrefName(ContentSettingsType::COOKIES))
->GetValue()
->Clone();
GURL host("http://example.com");
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetContentSetting(
host, host, ContentSettingsType::COOKIES));
host_content_settings_map->SetContentSettingDefaultScope(
host, GURL(), ContentSettingsType::COOKIES, CONTENT_SETTING_BLOCK);
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetContentSetting(
host, host, ContentSettingsType::COOKIES));
// Make a copy of the pref's new value so we can reset it later.
base::Value new_value =
prefs->FindPreference(GetPrefName(ContentSettingsType::COOKIES))
->GetValue()
->Clone();
// Clearing the backing pref should also clear the internal cache.
prefs->Set(GetPrefName(ContentSettingsType::COOKIES), default_value);
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetContentSetting(
host, host, ContentSettingsType::COOKIES));
// Resetting the pref to its previous value should update the cache.
prefs->Set(GetPrefName(ContentSettingsType::COOKIES), new_value);
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetContentSetting(
host, host, ContentSettingsType::COOKIES));
}
TEST_F(HostContentSettingsMapTest, HostTrimEndingDotCheck) {
TestingProfile profile;
HostContentSettingsMap* host_content_settings_map =
HostContentSettingsMapFactory::GetForProfile(&profile);
content_settings::CookieSettings* cookie_settings =
CookieSettingsFactory::GetForProfile(&profile).get();
GURL host_ending_with_dot("http://example.com./");
url::Origin origin = url::Origin::Create(host_ending_with_dot);
net::SiteForCookies site_for_cookies =
net::SiteForCookies::FromOrigin(origin);
EXPECT_TRUE(cookie_settings->IsFullCookieAccessAllowed(
host_ending_with_dot, site_for_cookies, origin,
net::CookieSettingOverrides(), /*cookie_partition_key=*/std::nullopt));
host_content_settings_map->SetContentSettingDefaultScope(
host_ending_with_dot, GURL(), ContentSettingsType::COOKIES,
CONTENT_SETTING_DEFAULT);
EXPECT_TRUE(cookie_settings->IsFullCookieAccessAllowed(
host_ending_with_dot, site_for_cookies, origin,
net::CookieSettingOverrides(), /*cookie_partition_key=*/std::nullopt));
host_content_settings_map->SetContentSettingDefaultScope(
host_ending_with_dot, GURL(), ContentSettingsType::COOKIES,
CONTENT_SETTING_BLOCK);
EXPECT_FALSE(cookie_settings->IsFullCookieAccessAllowed(
host_ending_with_dot, site_for_cookies, origin,
net::CookieSettingOverrides(), /*cookie_partition_key=*/std::nullopt));
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetContentSetting(
host_ending_with_dot, host_ending_with_dot,
ContentSettingsType::JAVASCRIPT));
host_content_settings_map->SetContentSettingDefaultScope(
host_ending_with_dot, GURL(), ContentSettingsType::JAVASCRIPT,
CONTENT_SETTING_DEFAULT);
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetContentSetting(
host_ending_with_dot, host_ending_with_dot,
ContentSettingsType::JAVASCRIPT));
host_content_settings_map->SetContentSettingDefaultScope(
host_ending_with_dot, GURL(), ContentSettingsType::JAVASCRIPT,
CONTENT_SETTING_BLOCK);
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetContentSetting(
host_ending_with_dot, host_ending_with_dot,
ContentSettingsType::JAVASCRIPT));
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetContentSetting(
host_ending_with_dot, host_ending_with_dot,
ContentSettingsType::POPUPS));
host_content_settings_map->SetContentSettingDefaultScope(
host_ending_with_dot, GURL(), ContentSettingsType::POPUPS,
CONTENT_SETTING_DEFAULT);
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetContentSetting(
host_ending_with_dot, host_ending_with_dot,
ContentSettingsType::POPUPS));
host_content_settings_map->SetContentSettingDefaultScope(
host_ending_with_dot, GURL(), ContentSettingsType::POPUPS,
CONTENT_SETTING_ALLOW);
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetContentSetting(
host_ending_with_dot, host_ending_with_dot,
ContentSettingsType::POPUPS));
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetContentSetting(
host_ending_with_dot, host_ending_with_dot,
ContentSettingsType::AUTOPLAY));
host_content_settings_map->SetContentSettingDefaultScope(
host_ending_with_dot, GURL(), ContentSettingsType::AUTOPLAY,
CONTENT_SETTING_BLOCK);
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetContentSetting(
host_ending_with_dot, host_ending_with_dot,
ContentSettingsType::AUTOPLAY));
host_content_settings_map->SetContentSettingDefaultScope(
host_ending_with_dot, GURL(), ContentSettingsType::AUTOPLAY,
CONTENT_SETTING_DEFAULT);
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetContentSetting(
host_ending_with_dot, host_ending_with_dot,
ContentSettingsType::AUTOPLAY));
}
TEST_F(HostContentSettingsMapTest, NestedSettings) {
TestingProfile profile;
HostContentSettingsMap* host_content_settings_map =
HostContentSettingsMapFactory::GetForProfile(&profile);
GURL host1("http://example.com/");
GURL host2("http://b.example.com/");
GURL host3("http://a.b.example.com/");
GURL host4("http://a.example.com/");
GURL host5("http://b.b.example.com/");
ContentSettingsPattern pattern1 =
ContentSettingsPattern::FromString("[*.]example.com");
ContentSettingsPattern pattern2 =
ContentSettingsPattern::FromString("[*.]b.example.com");
ContentSettingsPattern pattern3 =
ContentSettingsPattern::FromString("a.b.example.com");
// Test nested patterns for one type.
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetDefaultContentSetting(
ContentSettingsType::COOKIES));
host_content_settings_map->SetContentSettingCustomScope(
pattern1, ContentSettingsPattern::Wildcard(),
ContentSettingsType::COOKIES, CONTENT_SETTING_BLOCK);
host_content_settings_map->SetContentSettingCustomScope(
pattern2, ContentSettingsPattern::Wildcard(),
ContentSettingsType::COOKIES, CONTENT_SETTING_ALLOW);
host_content_settings_map->SetContentSettingCustomScope(
pattern3, ContentSettingsPattern::Wildcard(),
ContentSettingsType::COOKIES, CONTENT_SETTING_BLOCK);
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetContentSetting(
host1, host1, ContentSettingsType::COOKIES));
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetContentSetting(
host2, host2, ContentSettingsType::COOKIES));
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetContentSetting(
host3, host3, ContentSettingsType::COOKIES));
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetContentSetting(
host4, host4, ContentSettingsType::COOKIES));
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetContentSetting(
host5, host5, ContentSettingsType::COOKIES));
host_content_settings_map->ClearSettingsForOneType(
ContentSettingsType::COOKIES);
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetDefaultContentSetting(
ContentSettingsType::COOKIES));
GURL https_host1("https://b.example.com/");
GURL https_host2("https://a.b.example.com/");
ContentSettingsPattern pattern4 =
ContentSettingsPattern::FromString("b.example.com");
host_content_settings_map->SetContentSettingCustomScope(
pattern4, ContentSettingsPattern::Wildcard(),
ContentSettingsType::COOKIES, CONTENT_SETTING_BLOCK);
// Pattern "b.example.com" will affect (http|https)://b.example.com
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetContentSetting(
host2, host2, ContentSettingsType::COOKIES));
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetContentSetting(
https_host1, https_host1, ContentSettingsType::COOKIES));
// Pattern "b.example.com" will not affect (http|https)://a.b.example.com
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetContentSetting(
host3, host3, ContentSettingsType::COOKIES));
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetContentSetting(
https_host2, https_host2, ContentSettingsType::COOKIES));
}
TEST_F(HostContentSettingsMapTest, TypeIsolatedSettings) {
TestingProfile profile;
HostContentSettingsMap* host_content_settings_map =
HostContentSettingsMapFactory::GetForProfile(&profile);
GURL host("http://example.com/");
host_content_settings_map->SetContentSettingDefaultScope(
host, GURL(), ContentSettingsType::COOKIES, CONTENT_SETTING_BLOCK);
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetContentSetting(
host, host, ContentSettingsType::COOKIES));
EXPECT_EQ(CONTENT_SETTING_ASK,
host_content_settings_map->GetContentSetting(
host, host, ContentSettingsType::GEOLOCATION));
}
TEST_F(HostContentSettingsMapTest, IncognitoInheritInitialAllow) {
// The cookie setting has an initial value of ALLOW, so all changes should be
// inherited from regular to incognito mode.
TestingProfile profile;
Profile* otr_profile =
profile.GetPrimaryOTRProfile(/*create_if_needed=*/true);
HostContentSettingsMap* host_content_settings_map =
HostContentSettingsMapFactory::GetForProfile(&profile);
HostContentSettingsMap* otr_map =
HostContentSettingsMapFactory::GetForProfile(otr_profile);
GURL host("http://example.com/");
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetContentSetting(
host, host, ContentSettingsType::COOKIES));
EXPECT_EQ(
CONTENT_SETTING_ALLOW,
otr_map->GetContentSetting(host, host, ContentSettingsType::COOKIES));
// The inherited setting should be included in ContentSettingsForOneType
// table as well.
{
ContentSettingsForOneType otr_settings =
otr_map->GetSettingsForOneType(ContentSettingsType::COOKIES);
ASSERT_EQ(1u, otr_settings.size());
EXPECT_FALSE(otr_settings[0].incognito);
EXPECT_EQ(CONTENT_SETTING_ALLOW, content_settings::ValueToContentSetting(
otr_settings[0].setting_value));
}
// Changing content settings on the main map should also affect the
// incognito map.
host_content_settings_map->SetContentSettingDefaultScope(
host, GURL(), ContentSettingsType::COOKIES, CONTENT_SETTING_SESSION_ONLY);
EXPECT_EQ(CONTENT_SETTING_SESSION_ONLY,
host_content_settings_map->GetContentSetting(
host, host, ContentSettingsType::COOKIES));
EXPECT_EQ(
CONTENT_SETTING_SESSION_ONLY,
otr_map->GetContentSetting(host, host, ContentSettingsType::COOKIES));
// Inherited setting + default in ContentSettingsForOneType
{
ContentSettingsForOneType otr_settings =
otr_map->GetSettingsForOneType(ContentSettingsType::COOKIES);
ASSERT_EQ(2u, otr_settings.size());
EXPECT_FALSE(otr_settings[0].incognito);
EXPECT_EQ(
CONTENT_SETTING_SESSION_ONLY,
content_settings::ValueToContentSetting(otr_settings[0].setting_value));
EXPECT_FALSE(otr_settings[1].incognito);
EXPECT_EQ(CONTENT_SETTING_ALLOW, content_settings::ValueToContentSetting(
otr_settings[1].setting_value));
}
host_content_settings_map->SetContentSettingDefaultScope(
host, GURL(), ContentSettingsType::COOKIES, CONTENT_SETTING_BLOCK);
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetContentSetting(
host, host, ContentSettingsType::COOKIES));
EXPECT_EQ(
CONTENT_SETTING_BLOCK,
otr_map->GetContentSetting(host, host, ContentSettingsType::COOKIES));
// Changing content settings on the incognito map should NOT affect the
// main map.
otr_map->SetContentSettingDefaultScope(
host, GURL(), ContentSettingsType::COOKIES, CONTENT_SETTING_ALLOW);
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetContentSetting(
host, host, ContentSettingsType::COOKIES));
EXPECT_EQ(
CONTENT_SETTING_ALLOW,
otr_map->GetContentSetting(host, host, ContentSettingsType::COOKIES));
// The GetSettingsForOneType includes incognito setting first, but also
// inherited ones.
{
ContentSettingsForOneType otr_settings =
otr_map->GetSettingsForOneType(ContentSettingsType::COOKIES);
ASSERT_EQ(3u, otr_settings.size());
EXPECT_TRUE(otr_settings[0].incognito);
EXPECT_EQ(CONTENT_SETTING_ALLOW, content_settings::ValueToContentSetting(
otr_settings[0].setting_value));
EXPECT_FALSE(otr_settings[1].incognito);
EXPECT_EQ(CONTENT_SETTING_BLOCK, content_settings::ValueToContentSetting(
otr_settings[1].setting_value));
EXPECT_FALSE(otr_settings[2].incognito);
EXPECT_EQ(CONTENT_SETTING_ALLOW, content_settings::ValueToContentSetting(
otr_settings[2].setting_value));
}
}
TEST_F(HostContentSettingsMapTest, IncognitoInheritPopups) {
// The popup setting has an initial value of BLOCK, but it is allowed
// to inherit ALLOW settings because it doesn't provide access to user data.
TestingProfile profile;
Profile* otr_profile =
profile.GetPrimaryOTRProfile(/*create_if_needed=*/true);
HostContentSettingsMap* host_content_settings_map =
HostContentSettingsMapFactory::GetForProfile(&profile);
HostContentSettingsMap* otr_map =
HostContentSettingsMapFactory::GetForProfile(otr_profile);
GURL host("http://example.com/");
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetContentSetting(
host, host, ContentSettingsType::POPUPS));
EXPECT_EQ(
CONTENT_SETTING_BLOCK,
otr_map->GetContentSetting(host, host, ContentSettingsType::POPUPS));
// Changing content settings on the main map should affect the
// incognito map.
host_content_settings_map->SetContentSettingDefaultScope(
host, GURL(), ContentSettingsType::POPUPS, CONTENT_SETTING_ALLOW);
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetContentSetting(
host, host, ContentSettingsType::POPUPS));
EXPECT_EQ(
CONTENT_SETTING_ALLOW,
otr_map->GetContentSetting(host, host, ContentSettingsType::POPUPS));
// Changing content settings on the incognito map should NOT affect the
// main map.
host_content_settings_map->SetContentSettingDefaultScope(
host, GURL(), ContentSettingsType::POPUPS, CONTENT_SETTING_BLOCK);
otr_map->SetContentSettingDefaultScope(
host, GURL(), ContentSettingsType::POPUPS, CONTENT_SETTING_ALLOW);
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetContentSetting(
host, host, ContentSettingsType::POPUPS));
EXPECT_EQ(
CONTENT_SETTING_ALLOW,
otr_map->GetContentSetting(host, host, ContentSettingsType::POPUPS));
}
TEST_F(HostContentSettingsMapTest, IncognitoPartialInheritPref) {
// Permissions marked INHERIT_IF_LESS_PERMISSIVE in
// ContentSettingsRegistry only inherit BLOCK and ASK settings from regular
// to incognito if the initial value is ASK.
TestingProfile profile;
Profile* otr_profile =
profile.GetPrimaryOTRProfile(/*create_if_needed=*/true);
HostContentSettingsMap* host_content_settings_map =
HostContentSettingsMapFactory::GetForProfile(&profile);
HostContentSettingsMap* otr_map =
HostContentSettingsMapFactory::GetForProfile(otr_profile);
GURL host("http://example.com/");
EXPECT_EQ(CONTENT_SETTING_ASK,
host_content_settings_map->GetContentSetting(
host, host, ContentSettingsType::MEDIASTREAM_MIC));
EXPECT_EQ(CONTENT_SETTING_ASK,
otr_map->GetContentSetting(host, host,
ContentSettingsType::MEDIASTREAM_MIC));
{
ContentSettingsForOneType otr_settings =
otr_map->GetSettingsForOneType(ContentSettingsType::MEDIASTREAM_MIC);
ASSERT_EQ(1u, otr_settings.size());
EXPECT_FALSE(otr_settings[0].incognito);
EXPECT_EQ(CONTENT_SETTING_ASK, content_settings::ValueToContentSetting(
otr_settings[0].setting_value));
}
// BLOCK should be inherited from the main map to the incognito map.
host_content_settings_map->SetContentSettingDefaultScope(
host, GURL(), ContentSettingsType::MEDIASTREAM_MIC,
CONTENT_SETTING_BLOCK);
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetContentSetting(
host, host, ContentSettingsType::MEDIASTREAM_MIC));
EXPECT_EQ(CONTENT_SETTING_BLOCK,
otr_map->GetContentSetting(host, host,
ContentSettingsType::MEDIASTREAM_MIC));
// GetSettingsForOneType should return preference followed by default, both inherited.
{
ContentSettingsForOneType otr_settings =
otr_map->GetSettingsForOneType(ContentSettingsType::MEDIASTREAM_MIC);
ASSERT_EQ(2u, otr_settings.size());
EXPECT_FALSE(otr_settings[0].incognito);
EXPECT_EQ(CONTENT_SETTING_BLOCK, content_settings::ValueToContentSetting(
otr_settings[0].setting_value));
EXPECT_FALSE(otr_settings[1].incognito);
EXPECT_EQ(CONTENT_SETTING_ASK, content_settings::ValueToContentSetting(
otr_settings[1].setting_value));
}
// ALLOW should not be inherited from the main map to the incognito map (but
// it still overwrites the BLOCK, hence incognito reverts to ASK).
host_content_settings_map->SetContentSettingDefaultScope(
host, GURL(), ContentSettingsType::MEDIASTREAM_MIC,
CONTENT_SETTING_ALLOW);
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetContentSetting(
host, host, ContentSettingsType::MEDIASTREAM_MIC));
EXPECT_EQ(CONTENT_SETTING_ASK,
otr_map->GetContentSetting(host, host,
ContentSettingsType::MEDIASTREAM_MIC));
// The inherited ALLOW gets turned back into ASK in GetSettingsForOneType,
// mirroring the reverting to ASK behavior above.
{
ContentSettingsForOneType otr_settings =
otr_map->GetSettingsForOneType(ContentSettingsType::MEDIASTREAM_MIC);
ASSERT_EQ(2u, otr_settings.size());
EXPECT_FALSE(otr_settings[0].incognito);
EXPECT_EQ(CONTENT_SETTING_ASK, content_settings::ValueToContentSetting(
otr_settings[0].setting_value));
EXPECT_FALSE(otr_settings[1].incognito);
EXPECT_EQ(CONTENT_SETTING_ASK, content_settings::ValueToContentSetting(
otr_settings[1].setting_value));
}
}
TEST_F(HostContentSettingsMapTest, IncognitoPartialInheritDefault) {
// Permissions marked INHERIT_IF_LESS_PERMISSIVE in
// ContentSettingsRegistry only inherit BLOCK and ASK settings from regular
// to incognito if the initial value is ASK.
TestingProfile profile;
Profile* otr_profile =
profile.GetPrimaryOTRProfile(/*create_if_needed=*/true);
HostContentSettingsMap* host_content_settings_map =
HostContentSettingsMapFactory::GetForProfile(&profile);
HostContentSettingsMap* otr_map =
HostContentSettingsMapFactory::GetForProfile(otr_profile);
GURL host("http://example.com/");
EXPECT_EQ(CONTENT_SETTING_ASK,
host_content_settings_map->GetDefaultContentSetting(
ContentSettingsType::GEOLOCATION));
EXPECT_EQ(CONTENT_SETTING_ASK,
host_content_settings_map->GetContentSetting(
host, host, ContentSettingsType::GEOLOCATION));
EXPECT_EQ(CONTENT_SETTING_ASK, otr_map->GetDefaultContentSetting(
ContentSettingsType::GEOLOCATION));
EXPECT_EQ(
CONTENT_SETTING_ASK,
otr_map->GetContentSetting(host, host, ContentSettingsType::GEOLOCATION));
// BLOCK should be inherited from the main map to the incognito map.
host_content_settings_map->SetDefaultContentSetting(
ContentSettingsType::GEOLOCATION, CONTENT_SETTING_BLOCK);
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetDefaultContentSetting(
ContentSettingsType::GEOLOCATION));
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetContentSetting(
host, host, ContentSettingsType::GEOLOCATION));
EXPECT_EQ(CONTENT_SETTING_BLOCK, otr_map->GetDefaultContentSetting(
ContentSettingsType::GEOLOCATION));
EXPECT_EQ(
CONTENT_SETTING_BLOCK,
otr_map->GetContentSetting(host, host, ContentSettingsType::GEOLOCATION));
// ALLOW should not be inherited from the main map to the incognito map (but
// it still overwrites the BLOCK, hence incognito reverts to ASK).
host_content_settings_map->SetDefaultContentSetting(
ContentSettingsType::GEOLOCATION, CONTENT_SETTING_ALLOW);
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetDefaultContentSetting(
ContentSettingsType::GEOLOCATION));
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetContentSetting(
host, host, ContentSettingsType::GEOLOCATION));
EXPECT_EQ(CONTENT_SETTING_ASK, otr_map->GetDefaultContentSetting(
ContentSettingsType::GEOLOCATION));
EXPECT_EQ(
CONTENT_SETTING_ASK,
otr_map->GetContentSetting(host, host, ContentSettingsType::GEOLOCATION));
}
TEST_F(HostContentSettingsMapTest, IncognitoInheritCookies) {
TestingProfile profile;
Profile* otr_profile =
profile.GetPrimaryOTRProfile(/*create_if_needed=*/true);
HostContentSettingsMap* map =
HostContentSettingsMapFactory::GetForProfile(&profile);
HostContentSettingsMap* otr_map =
HostContentSettingsMapFactory::GetForProfile(otr_profile);
GURL url("http://example.com/");
map->SetContentSettingCustomScope(
ContentSettingsPattern::FromURLNoWildcard(url),
ContentSettingsPattern::FromURLNoWildcard(url),
ContentSettingsType::COOKIES, CONTENT_SETTING_SESSION_ONLY);
otr_map->SetContentSettingCustomScope(ContentSettingsPattern::FromURL(url),
ContentSettingsPattern::FromURL(url),
ContentSettingsType::COOKIES,
CONTENT_SETTING_BLOCK);
EXPECT_EQ(CONTENT_SETTING_SESSION_ONLY,
map->GetContentSetting(url, url, ContentSettingsType::COOKIES));
EXPECT_EQ(CONTENT_SETTING_BLOCK,
otr_map->GetContentSetting(url, url, ContentSettingsType::COOKIES));
auto indices = content_settings::HostIndexedContentSettings::Create(
otr_map->GetSettingsForOneType(ContentSettingsType::COOKIES));
ContentSetting result = CONTENT_SETTING_ALLOW;
for (const auto& index : indices) {
auto* found = index.Find(url, url);
if (found) {
result = content_settings::ValueToContentSetting(found->second.value);
break;
}
}
EXPECT_EQ(CONTENT_SETTING_BLOCK, result);
}
TEST_F(HostContentSettingsMapTest, IncognitoDontInheritWebsiteSetting) {
// Website settings marked DONT_INHERIT_IN_INCOGNITO in
// WebsiteSettingsRegistry (e.g. usb chooser data) don't inherit any values
// from from regular to incognito.
TestingProfile profile;
Profile* otr_profile =
profile.GetPrimaryOTRProfile(/*create_if_needed=*/true);
HostContentSettingsMap* host_content_settings_map =
HostContentSettingsMapFactory::GetForProfile(&profile);
HostContentSettingsMap* otr_map =
HostContentSettingsMapFactory::GetForProfile(otr_profile);
GURL host("http://example.com/");
// USB chooser data defaults to |nullptr|.
EXPECT_EQ(base::Value(),
host_content_settings_map->GetWebsiteSetting(
host, host, ContentSettingsType::USB_CHOOSER_DATA));
EXPECT_EQ(base::Value(),
otr_map->GetWebsiteSetting(host, host,
ContentSettingsType::USB_CHOOSER_DATA));
base::Value::Dict test_dict;
test_dict.Set("test", "value");
host_content_settings_map->SetWebsiteSettingDefaultScope(
host, host, ContentSettingsType::USB_CHOOSER_DATA,
base::Value(test_dict.Clone()));
// The setting is not inherited by |otr_map|.
base::Value stored_value = host_content_settings_map->GetWebsiteSetting(
host, host, ContentSettingsType::USB_CHOOSER_DATA);
EXPECT_EQ(stored_value, test_dict);
EXPECT_EQ(base::Value(),
otr_map->GetWebsiteSetting(host, host,
ContentSettingsType::USB_CHOOSER_DATA));
{
ContentSettingsForOneType otr_settings =
otr_map->GetSettingsForOneType(ContentSettingsType::USB_CHOOSER_DATA);
// Nothing gets inherited here, and there is no default.
ASSERT_EQ(0u, otr_settings.size());
}
}
TEST_F(HostContentSettingsMapTest, IncognitoDontInheritContentSetting) {
// Content settings marked DONT_INHERIT_IN_INCOGNITO in
// ContentSettingsRegistry (e.g. top-level scoped 3pcd, which is a special
// case) don't inherit any values from from regular to incognito.
TestingProfile profile;
Profile* otr_profile =
profile.GetPrimaryOTRProfile(/*create_if_needed=*/true);
HostContentSettingsMap* map =
HostContentSettingsMapFactory::GetForProfile(&profile);
HostContentSettingsMap* otr_map =
HostContentSettingsMapFactory::GetForProfile(otr_profile);
GURL host("https://example.com/");
// top-level scoped 3pcd defaults to ALLOW.
EXPECT_EQ(CONTENT_SETTING_ALLOW,
map->GetContentSetting(
host, host, ContentSettingsType::TOP_LEVEL_TPCD_ORIGIN_TRIAL));
EXPECT_EQ(CONTENT_SETTING_ALLOW,
otr_map->GetContentSetting(
host, host, ContentSettingsType::TOP_LEVEL_TPCD_ORIGIN_TRIAL));
map->SetContentSettingDefaultScope(
host, GURL(), ContentSettingsType::TOP_LEVEL_TPCD_ORIGIN_TRIAL,
CONTENT_SETTING_BLOCK);
// The setting is not inherited by |otr_map|.
EXPECT_EQ(CONTENT_SETTING_BLOCK,
map->GetContentSetting(
host, host, ContentSettingsType::TOP_LEVEL_TPCD_ORIGIN_TRIAL));
EXPECT_EQ(CONTENT_SETTING_ALLOW,
otr_map->GetContentSetting(
host, host, ContentSettingsType::TOP_LEVEL_TPCD_ORIGIN_TRIAL));
}
TEST_F(HostContentSettingsMapTest, PrefExceptionsOperation) {
using content_settings::SettingSource;
const char kUrl1[] = "http://user_exception_allow.com";
const char kUrl2[] = "http://user_exception_block.com";
const char kUrl3[] = "http://non_exception.com";
TestingProfile profile;
// Arbitrarily using cookies as content type to test.
TesterForType tester(&profile, ContentSettingsType::COOKIES);
// Add |kUrl1| and |kUrl2| only.
tester.AddUserException(kUrl1, CONTENT_SETTING_ALLOW);
tester.AddUserException(kUrl2, CONTENT_SETTING_BLOCK);
// No policy setting: follow users settings.
tester.ClearPolicyDefault();
// User exceptions.
EXPECT_EQ(SettingSource::kUser, tester.GetSettingSourceForURL(kUrl1));
EXPECT_EQ(SettingSource::kUser, tester.GetSettingSourceForURL(kUrl2));
// User default.
EXPECT_EQ(SettingSource::kUser, tester.GetSettingSourceForURL(kUrl3));
// Policy overrides users always.
tester.SetPolicyDefault(CONTENT_SETTING_ALLOW);
EXPECT_EQ(SettingSource::kPolicy, tester.GetSettingSourceForURL(kUrl1));
EXPECT_EQ(SettingSource::kPolicy, tester.GetSettingSourceForURL(kUrl2));
EXPECT_EQ(SettingSource::kPolicy, tester.GetSettingSourceForURL(kUrl3));
tester.SetPolicyDefault(CONTENT_SETTING_BLOCK);
EXPECT_EQ(SettingSource::kPolicy, tester.GetSettingSourceForURL(kUrl1));
EXPECT_EQ(SettingSource::kPolicy, tester.GetSettingSourceForURL(kUrl2));
EXPECT_EQ(SettingSource::kPolicy, tester.GetSettingSourceForURL(kUrl3));
}
TEST_F(HostContentSettingsMapTest, GetUserModifiableContentSetting) {
GURL url("http://user_exception_allow.com");
TestingProfile profile;
// Arbitrarily using cookies as content type to test.
profile.GetTestingPrefService()->SetManagedPref(
prefs::kManagedDefaultCookiesSetting,
std::make_unique<base::Value>(CONTENT_SETTING_BLOCK));
HostContentSettingsMap* map =
HostContentSettingsMapFactory::GetForProfile(&profile);
map->SetContentSettingDefaultScope(url, url, ContentSettingsType::COOKIES,
CONTENT_SETTING_ALLOW);
EXPECT_EQ(CONTENT_SETTING_ALLOW, map->GetUserModifiableContentSetting(
url, url, ContentSettingsType::COOKIES));
EXPECT_EQ(CONTENT_SETTING_BLOCK,
map->GetContentSetting(url, url, ContentSettingsType::COOKIES));
}
/**
* Test that HostContentSettingsMap::GetUserModifiableContentSetting() ignores
* the relative int value of the providers when determining whether a provider
* is user-modifiable.
*/
TEST_F(HostContentSettingsMapTest,
GetUserModifiableContentSetting_IndependentOfProviderEnumValues) {
EXPECT_LT(content_settings::ProviderType::kPolicyProvider,
content_settings::ProviderType::kPrefProvider);
EXPECT_LT(content_settings::ProviderType::kPrefProvider,
content_settings::ProviderType::kProviderForTests);
// Arbitrarily using cookies as content type to test.
GURL url("http://user_exception_allow.com");
{
TestingProfile profile;
ScopedMapNoFactoryTester map_tester(profile);
profile.GetTestingPrefService()->SetManagedPref(
prefs::kManagedDefaultCookiesSetting,
std::make_unique<base::Value>(CONTENT_SETTING_BLOCK));
scoped_refptr<HostContentSettingsMap> map = map_tester.map;
map->SetContentSettingDefaultScope(url, url, ContentSettingsType::COOKIES,
CONTENT_SETTING_ALLOW);
EXPECT_EQ(CONTENT_SETTING_ALLOW,
map->GetUserModifiableContentSetting(
url, url, ContentSettingsType::COOKIES));
content_settings::SettingInfo info;
EXPECT_EQ(
CONTENT_SETTING_BLOCK,
map->GetContentSetting(url, url, ContentSettingsType::COOKIES, &info));
// Check that the setting came from ProviderType::kPolicyProvider.
EXPECT_EQ(content_settings::SettingSource::kPolicy, info.source);
}
{
TestingProfile profile;
// Use ScopedMapNoFactoryTester so that
// HostContentSettingsMap::RegisterProvider() is called immediately after
// building the HostContentSettingsMap and avoid any calls on
// HostContentSettingsMap from TestingProfile initializing. This is required
// due to the thread assertions in
// HostContentSettingsMap::RegisterProvider().
ScopedMapNoFactoryTester map_tester(profile);
auto mock_provider = std::make_unique<content_settings::MockProvider>(
/*read_only=*/false);
mock_provider->SetWebsiteSetting(
ContentSettingsPattern::Wildcard(), ContentSettingsPattern::Wildcard(),
ContentSettingsType::COOKIES, base::Value(CONTENT_SETTING_BLOCK));
map_tester.map->RegisterProvider(
content_settings::ProviderType::kProviderForTests,
std::move(mock_provider));
EXPECT_EQ(CONTENT_SETTING_ALLOW,
map_tester.map->GetUserModifiableContentSetting(
url, url, ContentSettingsType::COOKIES));
content_settings::SettingInfo info;
EXPECT_EQ(CONTENT_SETTING_BLOCK,
map_tester.map->GetContentSetting(
url, url, ContentSettingsType::COOKIES, &info));
// Check that the setting came from ProviderType::kProviderForTests.
EXPECT_EQ(content_settings::SettingSource::kTest, info.source);
}
}
// For a single Unicode encoded pattern, check if it gets converted to punycode
// and old pattern gets deleted.
TEST_F(HostContentSettingsMapTest, CanonicalizeExceptionsUnicodeOnly) {
TestingProfile profile;
PrefService* prefs = profile.GetPrefs();
// Set utf-8 data.
{
ScopedDictPrefUpdate update(prefs,
GetPrefName(ContentSettingsType::COOKIES));
base::Value::Dict& all_settings_dictionary = update.Get();
base::Value::Dict dummy_payload;
dummy_payload.Set("setting", CONTENT_SETTING_ALLOW);
all_settings_dictionary.Set("[*.]\xC4\x87ira.com,*",
std::move(dummy_payload));
}
HostContentSettingsMapFactory::GetForProfile(&profile);
const base::Value::Dict& all_settings_dictionary =
prefs->GetDict(GetPrefName(ContentSettingsType::COOKIES));
EXPECT_FALSE(all_settings_dictionary.FindDict("[*.]\xC4\x87ira.com,*"));
EXPECT_TRUE(all_settings_dictionary.FindDict("[*.]xn--ira-ppa.com,*"));
}
// If both Unicode and its punycode pattern exist, make sure we don't touch the
// settings for the punycode, and that Unicode pattern gets deleted.
TEST_F(HostContentSettingsMapTest, CanonicalizeExceptionsUnicodeAndPunycode) {
TestingProfile profile;
base::Value value =
base::JSONReader::Read("{\"[*.]\\xC4\\x87ira.com,*\":{\"setting\":1}}")
.value();
profile.GetPrefs()->Set(GetPrefName(ContentSettingsType::COOKIES), value);
// Set punycode equivalent, with different setting.
base::Value puny_value =
base::JSONReader::Read("{\"[*.]xn--ira-ppa.com,*\":{\"setting\":2}}")
.value();
profile.GetPrefs()->Set(GetPrefName(ContentSettingsType::COOKIES),
puny_value);
// Initialize the content map.
HostContentSettingsMapFactory::GetForProfile(&profile);
const base::Value& content_setting_prefs =
profile.GetPrefs()->GetValue(GetPrefName(ContentSettingsType::COOKIES));
std::string prefs_as_json;
base::JSONWriter::Write(content_setting_prefs, &prefs_as_json);
EXPECT_STREQ("{\"[*.]xn--ira-ppa.com,*\":{\"setting\":2}}",
prefs_as_json.c_str());
}
// If a default-content-setting is managed, the managed value should be used
// instead of the default value.
TEST_F(HostContentSettingsMapTest, ManagedDefaultContentSetting) {
TestingProfile profile;
HostContentSettingsMap* host_content_settings_map =
HostContentSettingsMapFactory::GetForProfile(&profile);
sync_preferences::TestingPrefServiceSyncable* prefs =
profile.GetTestingPrefService();
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetDefaultContentSetting(
ContentSettingsType::JAVASCRIPT));
// Set managed-default-content-setting through the coresponding preferences.
prefs->SetManagedPref(prefs::kManagedDefaultJavaScriptSetting,
std::make_unique<base::Value>(CONTENT_SETTING_BLOCK));
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetDefaultContentSetting(
ContentSettingsType::JAVASCRIPT));
// Remove managed-default-content-settings-preferences.
prefs->RemoveManagedPref(prefs::kManagedDefaultJavaScriptSetting);
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetDefaultContentSetting(
ContentSettingsType::JAVASCRIPT));
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetDefaultContentSetting(
ContentSettingsType::ADS));
// Set managed-default-content-setting through the coresponding preferences.
prefs->SetManagedPref(prefs::kManagedDefaultAdsSetting,
std::make_unique<base::Value>(CONTENT_SETTING_ALLOW));
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetDefaultContentSetting(
ContentSettingsType::ADS));
// Remove managed-default-content-settings-preferences.
prefs->RemoveManagedPref(prefs::kManagedDefaultAdsSetting);
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetDefaultContentSetting(
ContentSettingsType::ADS));
}
TEST_F(HostContentSettingsMapTest, GetNonDefaultContentSettingsIfTypeManaged) {
TestingProfile profile;
HostContentSettingsMap* host_content_settings_map =
HostContentSettingsMapFactory::GetForProfile(&profile);
sync_preferences::TestingPrefServiceSyncable* prefs =
profile.GetTestingPrefService();
// Set url for JavaScript setting.
GURL host("http://example.com/");
host_content_settings_map->SetContentSettingDefaultScope(
host, GURL(), ContentSettingsType::JAVASCRIPT, CONTENT_SETTING_BLOCK);
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetDefaultContentSetting(
ContentSettingsType::JAVASCRIPT));
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetContentSetting(
host, host, ContentSettingsType::JAVASCRIPT));
// Set managed-default-content-setting for content-settings-type JavaScript.
prefs->SetManagedPref(prefs::kManagedDefaultJavaScriptSetting,
std::make_unique<base::Value>(CONTENT_SETTING_ALLOW));
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetContentSetting(
host, host, ContentSettingsType::JAVASCRIPT));
}
// Managed default content setting should have higher priority
// than user defined patterns.
TEST_F(HostContentSettingsMapTest,
ManagedDefaultContentSettingIgnoreUserPattern) {
TestingProfile profile;
HostContentSettingsMap* host_content_settings_map =
HostContentSettingsMapFactory::GetForProfile(&profile);
sync_preferences::TestingPrefServiceSyncable* prefs =
profile.GetTestingPrefService();
// Block all JavaScript.
host_content_settings_map->SetDefaultContentSetting(
ContentSettingsType::JAVASCRIPT, CONTENT_SETTING_BLOCK);
// Set an exception to allow "[*.]example.com"
GURL host("http://example.com/");
host_content_settings_map->SetContentSettingDefaultScope(
host, GURL(), ContentSettingsType::JAVASCRIPT, CONTENT_SETTING_ALLOW);
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetDefaultContentSetting(
ContentSettingsType::JAVASCRIPT));
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetContentSetting(
host, host, ContentSettingsType::JAVASCRIPT));
// Set managed-default-content-settings-preferences.
prefs->SetManagedPref(prefs::kManagedDefaultJavaScriptSetting,
std::make_unique<base::Value>(CONTENT_SETTING_BLOCK));
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetContentSetting(
host, host, ContentSettingsType::JAVASCRIPT));
// Remove managed-default-content-settings-preferences.
prefs->RemoveManagedPref(prefs::kManagedDefaultJavaScriptSetting);
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetContentSetting(
host, host, ContentSettingsType::JAVASCRIPT));
}
// If a default-content-setting is set to managed setting, the user defined
// setting should be preserved.
TEST_F(HostContentSettingsMapTest, OverwrittenDefaultContentSetting) {
TestingProfile profile;
HostContentSettingsMap* host_content_settings_map =
HostContentSettingsMapFactory::GetForProfile(&profile);
sync_preferences::TestingPrefServiceSyncable* prefs =
profile.GetTestingPrefService();
// Set user defined default-content-setting for Cookies.
host_content_settings_map->SetDefaultContentSetting(
ContentSettingsType::COOKIES, CONTENT_SETTING_BLOCK);
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetDefaultContentSetting(
ContentSettingsType::COOKIES));
// Set preference to manage the default-content-setting for Cookies.
prefs->SetManagedPref(prefs::kManagedDefaultCookiesSetting,
std::make_unique<base::Value>(CONTENT_SETTING_ALLOW));
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetDefaultContentSetting(
ContentSettingsType::COOKIES));
// Remove the preference to manage the default-content-setting for Cookies.
prefs->RemoveManagedPref(prefs::kManagedDefaultCookiesSetting);
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetDefaultContentSetting(
ContentSettingsType::COOKIES));
}
// If a setting for a default-content-setting-type is set while the type is
// managed, then the new setting should be preserved and used after the
// default-content-setting-type is not managed anymore.
TEST_F(HostContentSettingsMapTest, SettingDefaultContentSettingsWhenManaged) {
TestingProfile profile;
HostContentSettingsMap* host_content_settings_map =
HostContentSettingsMapFactory::GetForProfile(&profile);
sync_preferences::TestingPrefServiceSyncable* prefs =
profile.GetTestingPrefService();
prefs->SetManagedPref(prefs::kManagedDefaultCookiesSetting,
std::make_unique<base::Value>(CONTENT_SETTING_ALLOW));
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetDefaultContentSetting(
ContentSettingsType::COOKIES));
host_content_settings_map->SetDefaultContentSetting(
ContentSettingsType::COOKIES, CONTENT_SETTING_BLOCK);
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetDefaultContentSetting(
ContentSettingsType::COOKIES));
prefs->RemoveManagedPref(prefs::kManagedDefaultCookiesSetting);
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetDefaultContentSetting(
ContentSettingsType::COOKIES));
}
TEST_F(HostContentSettingsMapTest, GetContentSetting) {
TestingProfile profile;
HostContentSettingsMap* host_content_settings_map =
HostContentSettingsMapFactory::GetForProfile(&profile);
GURL host("http://example.com/");
GURL embedder("chrome://foo");
host_content_settings_map->SetContentSettingDefaultScope(
host, GURL(), ContentSettingsType::COOKIES, CONTENT_SETTING_BLOCK);
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetContentSetting(
host, host, ContentSettingsType::COOKIES));
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetContentSetting(
embedder, host, ContentSettingsType::COOKIES));
}
TEST_F(HostContentSettingsMapTest, AddContentSettingsObserver) {
TestingProfile profile;
HostContentSettingsMap* host_content_settings_map =
HostContentSettingsMapFactory::GetForProfile(&profile);
content_settings::MockObserver mock_observer;
GURL host("http://example.com/");
ContentSettingsPattern pattern =
ContentSettingsPattern::FromString("[*.]example.com");
EXPECT_CALL(mock_observer, OnContentSettingChanged(
pattern, ContentSettingsPattern::Wildcard(),
ContentSettingsType::COOKIES));
host_content_settings_map->AddObserver(&mock_observer);
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetContentSetting(
host, host, ContentSettingsType::COOKIES));
host_content_settings_map->SetContentSettingDefaultScope(
host, GURL(), ContentSettingsType::COOKIES, CONTENT_SETTING_BLOCK);
}
// Guest profiles do not exist on Android, so don't run these tests there.
#if !BUILDFLAG(IS_ANDROID)
TEST_F(HostContentSettingsMapTest, GuestProfile) {
TestingProfile::Builder profile_builder;
profile_builder.SetGuestSession();
std::unique_ptr<Profile> profile = profile_builder.Build();
HostContentSettingsMap* host_content_settings_map =
HostContentSettingsMapFactory::GetForProfile(profile.get());
GURL host("http://example.com/");
ContentSettingsPattern pattern =
ContentSettingsPattern::FromString("[*.]example.com");
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetContentSetting(
host, host, ContentSettingsType::COOKIES));
// Changing content settings should not result in any prefs being stored
// however the value should be set in memory for OTR-Guest profiles.
host_content_settings_map->SetContentSettingDefaultScope(
host, GURL(), ContentSettingsType::COOKIES, CONTENT_SETTING_BLOCK);
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetContentSetting(
host, host, ContentSettingsType::COOKIES));
const base::Value::Dict& all_settings_dictionary =
profile->GetPrefs()->GetDict(GetPrefName(ContentSettingsType::COOKIES));
EXPECT_TRUE(all_settings_dictionary.empty());
}
// Default settings should not be modifiable for Guest profile (there is no UI
// to do this).
TEST_F(HostContentSettingsMapTest, GuestProfileDefaultSetting) {
TestingProfile::Builder profile_builder;
profile_builder.SetGuestSession();
std::unique_ptr<Profile> profile = profile_builder.Build();
HostContentSettingsMap* host_content_settings_map =
HostContentSettingsMapFactory::GetForProfile(profile.get());
GURL host("http://example.com/");
// There are no custom rules, so this should be the default.
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetContentSetting(
host, host, ContentSettingsType::COOKIES));
host_content_settings_map->SetDefaultContentSetting(
ContentSettingsType::COOKIES, CONTENT_SETTING_BLOCK);
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetContentSetting(
host, host, ContentSettingsType::COOKIES));
}
#endif // !BUILDFLAG(IS_ANDROID)
TEST_F(HostContentSettingsMapTest, InvalidPattern) {
// This is a regression test for crbug.com/618529, which fixed a memory leak
// when a website setting was set under a URL that mapped to an invalid
// pattern.
TestingProfile profile;
HostContentSettingsMap* host_content_settings_map =
HostContentSettingsMapFactory::GetForProfile(&profile);
GURL unsupported_url = GURL("view-source:http://www.google.com");
base::Value::Dict test_dict;
test_dict.Set("test", "value");
host_content_settings_map->SetWebsiteSettingDefaultScope(
unsupported_url, unsupported_url, ContentSettingsType::APP_BANNER,
base::Value(std::move(test_dict)));
EXPECT_EQ(base::Value(), host_content_settings_map->GetWebsiteSetting(
unsupported_url, unsupported_url,
ContentSettingsType::APP_BANNER));
}
TEST_F(HostContentSettingsMapTest, ClearSettingsForOneTypeWithPredicate) {
TestingProfile profile;
HostContentSettingsMap* host_content_settings_map =
HostContentSettingsMapFactory::GetForProfile(&profile);
// Patterns with wildcards.
ContentSettingsPattern pattern =
ContentSettingsPattern::FromString("[*.]example.org");
ContentSettingsPattern pattern2 =
ContentSettingsPattern::FromString("[*.]example.net");
// Patterns without wildcards.
GURL url1("https://www.google.com/");
GURL url2("https://www.google.com/maps");
GURL url3("http://www.google.com/maps");
GURL url3_origin_only("http://www.google.com/");
host_content_settings_map->SetContentSettingCustomScope(
pattern2, ContentSettingsPattern::Wildcard(),
ContentSettingsType::COOKIES, CONTENT_SETTING_BLOCK);
host_content_settings_map->SetContentSettingCustomScope(
pattern, ContentSettingsPattern::Wildcard(), ContentSettingsType::COOKIES,
CONTENT_SETTING_BLOCK);
host_content_settings_map->SetWebsiteSettingCustomScope(
pattern2, ContentSettingsPattern::Wildcard(),
ContentSettingsType::APP_BANNER, base::Value(base::Value::Type::DICT));
// First, test that we clear only COOKIES (not APP_BANNER), and pattern2.
host_content_settings_map->ClearSettingsForOneTypeWithPredicate(
ContentSettingsType::COOKIES,
[&](const ContentSettingPatternSource& setting) {
return setting.primary_pattern == pattern2;
});
ContentSettingsForOneType host_settings =
host_content_settings_map->GetSettingsForOneType(
ContentSettingsType::COOKIES);
// |host_settings| contains default & block.
EXPECT_EQ(2U, host_settings.size());
EXPECT_EQ(pattern, host_settings[0].primary_pattern);
EXPECT_EQ("*", host_settings[0].secondary_pattern.ToString());
EXPECT_EQ("*", host_settings[1].primary_pattern.ToString());
EXPECT_EQ("*", host_settings[1].secondary_pattern.ToString());
host_settings = host_content_settings_map->GetSettingsForOneType(
ContentSettingsType::APP_BANNER);
// |host_settings| still contains the value for APP_BANNER.
EXPECT_EQ(1U, host_settings.size());
EXPECT_EQ(pattern2, host_settings[0].primary_pattern);
EXPECT_EQ("*", host_settings[0].secondary_pattern.ToString());
// Next, test that we do correct pattern matching w/ an origin policy item.
// We verify that we have no settings stored.
host_settings = host_content_settings_map->GetSettingsForOneType(
ContentSettingsType::SITE_ENGAGEMENT);
EXPECT_EQ(0u, host_settings.size());
// Add settings.
host_content_settings_map->SetWebsiteSettingDefaultScope(
url1, GURL(), ContentSettingsType::SITE_ENGAGEMENT,
base::Value(base::Value::Type::DICT));
// This setting should override the one above, as it's the same origin.
host_content_settings_map->SetWebsiteSettingDefaultScope(
url2, GURL(), ContentSettingsType::SITE_ENGAGEMENT,
base::Value(base::Value::Type::DICT));
host_content_settings_map->SetWebsiteSettingDefaultScope(
url3, GURL(), ContentSettingsType::SITE_ENGAGEMENT,
base::Value(base::Value::Type::DICT));
// Verify we only have two.
host_settings = host_content_settings_map->GetSettingsForOneType(
ContentSettingsType::SITE_ENGAGEMENT);
EXPECT_EQ(2u, host_settings.size());
// Clear the http one, which we should be able to do w/ the origin only, as
// the scope of ContentSettingsType::SITE_ENGAGEMENT is
// REQUESTING_ORIGIN_ONLY_SCOPE.
ContentSettingsPattern http_pattern =
ContentSettingsPattern::FromURLNoWildcard(url3_origin_only);
host_content_settings_map->ClearSettingsForOneTypeWithPredicate(
ContentSettingsType::SITE_ENGAGEMENT,
[&](const ContentSettingPatternSource& setting) {
return setting.primary_pattern == http_pattern;
});
// Verify we only have one, and it's url1.
host_settings = host_content_settings_map->GetSettingsForOneType(
ContentSettingsType::SITE_ENGAGEMENT);
EXPECT_EQ(1u, host_settings.size());
EXPECT_EQ(ContentSettingsPattern::FromURLNoWildcard(url1),
host_settings[0].primary_pattern);
}
TEST_F(HostContentSettingsMapTest, ClearSettingsWithTimePredicate) {
TestingProfile profile;
auto* map = HostContentSettingsMapFactory::GetForProfile(&profile);
base::Time now = base::Time::Now();
base::Time back_1_hour = now - base::Hours(1);
base::Time back_30_days = now - base::Days(30);
base::Time back_31_days = now - base::Days(31);
base::SimpleTestClock test_clock;
test_clock.SetNow(now);
map->SetClockForTesting(&test_clock);
GURL url1("https://www.google.com/");
GURL url2("https://maps.google.com/");
GURL url3("https://photos.google.com");
// Add setting for url1.
map->SetContentSettingDefaultScope(url1, GURL(), ContentSettingsType::POPUPS,
CONTENT_SETTING_BLOCK);
// Add setting for url2.
test_clock.SetNow(back_1_hour);
map->SetContentSettingDefaultScope(url2, GURL(), ContentSettingsType::POPUPS,
CONTENT_SETTING_BLOCK);
// Add setting for url3 with the timestamp of 31 days old.
test_clock.SetNow(back_31_days);
map->SetContentSettingDefaultScope(url3, GURL(), ContentSettingsType::POPUPS,
CONTENT_SETTING_BLOCK);
// Verify we have three pattern and the default.
ContentSettingsForOneType host_settings =
map->GetSettingsForOneType(ContentSettingsType::POPUPS);
EXPECT_EQ(4u, host_settings.size());
// Clear all settings since |now|.
map->ClearSettingsForOneTypeWithPredicate(
ContentSettingsType::POPUPS, now, base::Time::Max(),
HostContentSettingsMap::PatternSourcePredicate());
// Verify we have two pattern (url2, url3) and the default.
host_settings = map->GetSettingsForOneType(ContentSettingsType::POPUPS);
EXPECT_EQ(3u, host_settings.size());
EXPECT_EQ("https://maps.google.com:443",
host_settings[0].primary_pattern.ToString());
EXPECT_EQ("https://photos.google.com:443",
host_settings[1].primary_pattern.ToString());
EXPECT_EQ("*", host_settings[2].primary_pattern.ToString());
// Clear all settings since the beginning of time to 30 days old.
map->ClearSettingsForOneTypeWithPredicate(
ContentSettingsType::POPUPS, base::Time(), back_30_days,
HostContentSettingsMap::PatternSourcePredicate());
// Verify we only have one pattern (url2) and the default.
host_settings = map->GetSettingsForOneType(ContentSettingsType::POPUPS);
EXPECT_EQ(2u, host_settings.size());
EXPECT_EQ("https://maps.google.com:443",
host_settings[0].primary_pattern.ToString());
EXPECT_EQ("*", host_settings[1].primary_pattern.ToString());
// Clear all settings since the beginning of time.
map->ClearSettingsForOneTypeWithPredicate(
ContentSettingsType::POPUPS, base::Time(), base::Time::Max(),
HostContentSettingsMap::PatternSourcePredicate());
// Verify we only have the default setting.
host_settings = map->GetSettingsForOneType(ContentSettingsType::POPUPS);
EXPECT_EQ(1u, host_settings.size());
EXPECT_EQ("*", host_settings[0].primary_pattern.ToString());
}
TEST_F(HostContentSettingsMapTest, GetSettingLastModified) {
TestingProfile profile;
auto* map = HostContentSettingsMapFactory::GetForProfile(&profile);
base::SimpleTestClock test_clock;
test_clock.SetNow(base::Time::Now());
map->SetClockForTesting(&test_clock);
ContentSettingsForOneType host_settings;
GURL url("https://www.google.com/");
// Last modified date for non existent settings should be base::Time().
base::Time t =
GetSettingLastModifiedDate(map, url, url, ContentSettingsType::POPUPS);
EXPECT_EQ(base::Time(), t);
// Add setting for url.
map->SetContentSettingDefaultScope(url, GURL(), ContentSettingsType::POPUPS,
CONTENT_SETTING_BLOCK);
t = GetSettingLastModifiedDate(map, url, url, ContentSettingsType::POPUPS);
EXPECT_EQ(t, test_clock.Now());
test_clock.Advance(base::Seconds(1));
// Modify setting.
map->SetContentSettingDefaultScope(url, GURL(), ContentSettingsType::POPUPS,
CONTENT_SETTING_ALLOW);
t = GetSettingLastModifiedDate(map, url, url, ContentSettingsType::POPUPS);
EXPECT_EQ(t, test_clock.Now());
}
TEST_F(HostContentSettingsMapTest, IsRestrictedToSecureOrigins) {
TestingProfile profile;
const auto* map = HostContentSettingsMapFactory::GetForProfile(&profile);
EXPECT_TRUE(
map->IsRestrictedToSecureOrigins(ContentSettingsType::GEOLOCATION));
EXPECT_FALSE(
map->IsRestrictedToSecureOrigins(ContentSettingsType::JAVASCRIPT));
}
TEST_F(HostContentSettingsMapTest, CanSetNarrowestSetting) {
TestingProfile profile;
const auto* map = HostContentSettingsMapFactory::GetForProfile(&profile);
GURL valid_url("http://google.com");
EXPECT_TRUE(map->CanSetNarrowestContentSetting(valid_url, valid_url,
ContentSettingsType::POPUPS));
GURL invalid_url("about:blank");
EXPECT_FALSE(map->CanSetNarrowestContentSetting(invalid_url, invalid_url,
ContentSettingsType::POPUPS));
}
TEST_F(HostContentSettingsMapTest, MigrateRequestingAndTopLevelOriginSettings) {
TestingProfile profile;
HostContentSettingsMap* map =
HostContentSettingsMapFactory::GetForProfile(&profile);
GURL requesting_origin("https://requester.com");
GURL embedding_origin("https://embedder.com");
ContentSettingsPattern requesting_pattern =
ContentSettingsPattern::FromURLNoWildcard(requesting_origin);
ContentSettingsPattern embedding_pattern =
ContentSettingsPattern::FromURLNoWildcard(embedding_origin);
map->AllowInvalidSecondaryPatternForTesting(true);
// Set content settings for 2 types that use requesting and top level
// origin as well as one for a type that doesn't.
map->SetContentSettingCustomScope(requesting_pattern, embedding_pattern,
ContentSettingsType::GEOLOCATION,
CONTENT_SETTING_ALLOW);
map->SetContentSettingCustomScope(requesting_pattern, embedding_pattern,
ContentSettingsType::MIDI_SYSEX,
CONTENT_SETTING_ALLOW);
map->SetContentSettingCustomScope(requesting_pattern, embedding_pattern,
ContentSettingsType::COOKIES,
CONTENT_SETTING_ALLOW);
map->MigrateSettingsPrecedingPermissionDelegationActivation();
map->AllowInvalidSecondaryPatternForTesting(false);
// Verify that all the settings are deleted except the cookies setting.
ContentSettingsForOneType host_settings =
map->GetSettingsForOneType(ContentSettingsType::GEOLOCATION);
EXPECT_EQ(1u, host_settings.size());
EXPECT_EQ(ContentSettingsPattern::Wildcard(),
host_settings[0].primary_pattern);
EXPECT_EQ(ContentSettingsPattern::Wildcard(),
host_settings[0].secondary_pattern);
host_settings = map->GetSettingsForOneType(ContentSettingsType::MIDI_SYSEX);
EXPECT_EQ(1u, host_settings.size());
EXPECT_EQ(ContentSettingsPattern::Wildcard(),
host_settings[0].primary_pattern);
EXPECT_EQ(ContentSettingsPattern::Wildcard(),
host_settings[0].secondary_pattern);
host_settings = map->GetSettingsForOneType(ContentSettingsType::COOKIES);
EXPECT_EQ(2u, host_settings.size());
EXPECT_EQ(requesting_pattern, host_settings[0].primary_pattern);
EXPECT_EQ(embedding_pattern, host_settings[0].secondary_pattern);
EXPECT_EQ(ContentSettingsPattern::Wildcard(),
host_settings[1].primary_pattern);
EXPECT_EQ(ContentSettingsPattern::Wildcard(),
host_settings[1].secondary_pattern);
}
TEST_F(HostContentSettingsMapTest,
MigrateRequestingAndTopLevelOriginSettingsResetsEmbeddedSetting) {
TestingProfile profile;
HostContentSettingsMap* map =
HostContentSettingsMapFactory::GetForProfile(&profile);
GURL requesting_origin("https://requester.com");
GURL embedding_origin("https://embedder.com");
ContentSettingsPattern requesting_pattern =
ContentSettingsPattern::FromURLNoWildcard(requesting_origin);
ContentSettingsPattern embedding_pattern =
ContentSettingsPattern::FromURLNoWildcard(embedding_origin);
map->AllowInvalidSecondaryPatternForTesting(true);
map->SetContentSettingCustomScope(requesting_pattern, embedding_pattern,
ContentSettingsType::GEOLOCATION,
CONTENT_SETTING_BLOCK);
map->SetContentSettingCustomScope(embedding_pattern, embedding_pattern,
ContentSettingsType::GEOLOCATION,
CONTENT_SETTING_ALLOW);
map->MigrateSettingsPrecedingPermissionDelegationActivation();
map->AllowInvalidSecondaryPatternForTesting(false);
// Verify that all settings for the embedding origin are deleted. This is
// important so that a user is repropmted if a permission request from an
// embedded site they had previously blocked makes a new request.
ContentSettingsForOneType host_settings =
map->GetSettingsForOneType(ContentSettingsType::GEOLOCATION);
EXPECT_EQ(1u, host_settings.size());
EXPECT_EQ(ContentSettingsPattern::Wildcard(),
host_settings[0].primary_pattern);
EXPECT_EQ(ContentSettingsPattern::Wildcard(),
host_settings[0].secondary_pattern);
}
// Creates new instance of PrefProvider and overrides it in
// |host_content_settings_map|.
void ReloadProviders(PrefService* pref_service,
HostContentSettingsMap* host_content_settings_map) {
auto pref_provider = std::make_unique<content_settings::PrefProvider>(
pref_service, false, true, false);
content_settings::TestUtils::OverrideProvider(
host_content_settings_map, std::move(pref_provider),
content_settings::ProviderType::kPrefProvider);
}
TEST_F(HostContentSettingsMapTest, GetPatternsFromScopingType) {
const GURL primary_url("http://a.b.example1.com:8080");
const GURL secondary_url("http://a.b.example2.com:8080");
TestingProfile profile;
HostContentSettingsMap* host_content_settings_map =
HostContentSettingsMapFactory::GetForProfile(&profile);
// Testing case:
// WebsiteSettingsInfo::REQUESTING_ORIGIN_WITH_TOP_ORIGIN_EXCEPTIONS_SCOPE.
host_content_settings_map->SetContentSettingDefaultScope(
primary_url, secondary_url, ContentSettingsType::COOKIES,
CONTENT_SETTING_ALLOW);
ContentSettingsForOneType settings =
host_content_settings_map->GetSettingsForOneType(
ContentSettingsType::COOKIES);
EXPECT_EQ(settings[0].primary_pattern,
ContentSettingsPattern::FromURL(primary_url));
EXPECT_EQ(settings[0].secondary_pattern, ContentSettingsPattern::Wildcard());
// Testing cases:
// WebsiteSettingsInfo::REQUESTING_AND_TOP_SCHEMEFUL_SITE_SCOPE,
host_content_settings_map->SetContentSettingDefaultScope(
primary_url, secondary_url, ContentSettingsType::STORAGE_ACCESS,
CONTENT_SETTING_ALLOW);
settings = host_content_settings_map->GetSettingsForOneType(
ContentSettingsType::STORAGE_ACCESS);
EXPECT_EQ(settings[0].primary_pattern,
ContentSettingsPattern::FromURLToSchemefulSitePattern(primary_url));
EXPECT_EQ(
settings[0].secondary_pattern,
ContentSettingsPattern::FromURLToSchemefulSitePattern(secondary_url));
// Testing cases:
// WebsiteSettingsInfo::REQUESTING_SCHEMEFUL_SITE_ONLY_SCOPE,
host_content_settings_map->SetWebsiteSettingDefaultScope(
primary_url, secondary_url, ContentSettingsType::COOKIE_CONTROLS_METADATA,
base::Value(base::Value::Dict()));
settings = host_content_settings_map->GetSettingsForOneType(
ContentSettingsType::COOKIE_CONTROLS_METADATA);
EXPECT_EQ(settings[0].primary_pattern,
ContentSettingsPattern::FromURLToSchemefulSitePattern(primary_url));
EXPECT_EQ(settings[0].secondary_pattern, ContentSettingsPattern::Wildcard());
// Testing cases:
// WebsiteSettingsInfo::REQUESTING_ORIGIN_AND_TOP_SCHEMEFUL_SITE_SCOPE,
host_content_settings_map->SetContentSettingDefaultScope(
primary_url, secondary_url, ContentSettingsType::TPCD_TRIAL,
CONTENT_SETTING_ALLOW);
settings = host_content_settings_map->GetSettingsForOneType(
ContentSettingsType::TPCD_TRIAL);
EXPECT_EQ(settings[0].primary_pattern,
ContentSettingsPattern::FromURLNoWildcard(primary_url));
EXPECT_EQ(
settings[0].secondary_pattern,
ContentSettingsPattern::FromURLToSchemefulSitePattern(secondary_url));
// Testing cases:
// WebsiteSettingsInfo::REQUESTING_ORIGIN_ONLY_SCOPE,
// WebsiteSettingsInfo::TOP_ORIGIN_ONLY_SCOPE,
// WebsiteSettingsInfo::GENERIC_SINGLE_ORIGIN_SCOPE.
for (const auto& kContentSetting :
{ContentSettingsType::JAVASCRIPT, ContentSettingsType::NOTIFICATIONS,
ContentSettingsType::GEOLOCATION,
ContentSettingsType::FEDERATED_IDENTITY_API}) {
host_content_settings_map->SetContentSettingDefaultScope(
primary_url, secondary_url, kContentSetting, CONTENT_SETTING_ALLOW);
settings =
host_content_settings_map->GetSettingsForOneType(kContentSetting);
EXPECT_EQ(settings[0].primary_pattern,
ContentSettingsPattern::FromURLNoWildcard(primary_url));
EXPECT_EQ(settings[0].secondary_pattern,
ContentSettingsPattern::Wildcard());
}
}
TEST_F(HostContentSettingsMapTest, GetPatternsForContentSettingsType) {
const GURL primary_url("http://a.b.example1.com:8080");
const GURL secondary_url("http://a.b.example2.com:8080");
TestingProfile profile;
HostContentSettingsMapFactory::GetForProfile(&profile);
// Testing case:
// WebsiteSettingsInfo::REQUESTING_ORIGIN_WITH_TOP_ORIGIN_EXCEPTIONS_SCOPE.
content_settings::PatternPair patterns =
HostContentSettingsMap::GetPatternsForContentSettingsType(
primary_url, secondary_url, ContentSettingsType::COOKIES);
EXPECT_EQ(patterns.first, ContentSettingsPattern::FromURL(primary_url));
EXPECT_EQ(patterns.second, ContentSettingsPattern::Wildcard());
// Testing cases:
// WebsiteSettingsInfo::REQUESTING_AND_TOP_SCHEMEFUL_SITE_SCOPE,
patterns = HostContentSettingsMap::GetPatternsForContentSettingsType(
primary_url, secondary_url, ContentSettingsType::STORAGE_ACCESS);
EXPECT_EQ(patterns.first,
ContentSettingsPattern::FromURLToSchemefulSitePattern(primary_url));
EXPECT_EQ(
patterns.second,
ContentSettingsPattern::FromURLToSchemefulSitePattern(secondary_url));
// Testing cases:
// WebsiteSettingsInfo::REQUESTING_SCHEMEFUL_SITE_ONLY_SCOPE,
patterns = HostContentSettingsMap::GetPatternsForContentSettingsType(
primary_url, secondary_url,
ContentSettingsType::COOKIE_CONTROLS_METADATA);
EXPECT_EQ(patterns.first,
ContentSettingsPattern::FromURLToSchemefulSitePattern(primary_url));
EXPECT_EQ(patterns.second, ContentSettingsPattern::Wildcard());
// Testing cases:
// WebsiteSettingsInfo::REQUESTING_ORIGIN_AND_TOP_SCHEMEFUL_SITE_SCOPE,
patterns = HostContentSettingsMap::GetPatternsForContentSettingsType(
primary_url, secondary_url, ContentSettingsType::TPCD_TRIAL);
EXPECT_EQ(patterns.first,
ContentSettingsPattern::FromURLNoWildcard(primary_url));
EXPECT_EQ(
patterns.second,
ContentSettingsPattern::FromURLToSchemefulSitePattern(secondary_url));
// Testing cases:
// WebsiteSettingsInfo::TOP_ORIGIN_WITH_RESOURCE_EXCEPTIONS_SCOPE,
// WebsiteSettingsInfo::REQUESTING_ORIGIN_ONLY_SCOPE,
// WebsiteSettingsInfo::TOP_ORIGIN_ONLY_SCOPE,
// WebsiteSettingsInfo::GENERIC_SINGLE_ORIGIN_SCOPE.
for (const auto& kContentSetting :
{ContentSettingsType::JAVASCRIPT, ContentSettingsType::NOTIFICATIONS,
ContentSettingsType::GEOLOCATION,
ContentSettingsType::FEDERATED_IDENTITY_API}) {
patterns = HostContentSettingsMap::GetPatternsForContentSettingsType(
primary_url, secondary_url, kContentSetting);
EXPECT_EQ(patterns.first,
ContentSettingsPattern::FromURLNoWildcard(primary_url));
EXPECT_EQ(patterns.second, ContentSettingsPattern::Wildcard());
}
}
// Tests if changing a settings in incognito mode does not affects the regular
// mode.
TEST_F(HostContentSettingsMapTest, IncognitoChangesDoNotPersist) {
TestingProfile profile;
auto* regular_map = HostContentSettingsMapFactory::GetForProfile(&profile);
auto* incognito_map = HostContentSettingsMapFactory::GetForProfile(
profile.GetPrimaryOTRProfile(/*create_if_needed=*/true));
auto* registry = content_settings::WebsiteSettingsRegistry::GetInstance();
auto* content_setting_registry =
content_settings::ContentSettingsRegistry::GetInstance();
GURL url("https://example.com");
ContentSettingsPattern pattern = ContentSettingsPattern::FromURL(url);
content_settings::SettingInfo setting_info;
for (const content_settings::WebsiteSettingsInfo* info : *registry) {
SCOPED_TRACE(info->name());
// Get regular profile default value.
base::Value original_value =
regular_map->GetWebsiteSetting(url, url, info->type(), &setting_info);
// Get a different valid value for incognito mode.
base::Value new_value;
if (content_setting_registry->Get(info->type())) {
// If no original value is available, the settings does not have any valid
// values and no more steps are required.
if (!original_value.is_int())
continue;
for (int another_value = 0;
another_value < ContentSetting::CONTENT_SETTING_NUM_SETTINGS;
another_value++) {
if (another_value != original_value.GetInt() &&
content_setting_registry->Get(info->type())
->IsSettingValid(static_cast<ContentSetting>(another_value))) {
new_value = base::Value(another_value);
break;
}
}
ASSERT_NE(new_value.type(), base::Value::Type::NONE)
<< "Every content setting should have at least two values.";
} else {
base::Value::Dict dict;
dict.SetByDottedPath("foo.bar", 0);
new_value = base::Value(std::move(dict));
}
// Ensure a different value is received.
ASSERT_NE(original_value, new_value);
// Set the different value in incognito mode.
base::Value incognito_value = new_value.Clone();
incognito_map->SetWebsiteSettingCustomScope(
pattern, ContentSettingsPattern::Wildcard(), info->type(),
std::move(new_value));
// Ensure incognito mode value is changed.
EXPECT_EQ(incognito_value, incognito_map->GetWebsiteSetting(
url, url, info->type(), &setting_info));
// Ensure regular mode value is not changed.
base::Value regular_mode_value =
regular_map->GetWebsiteSetting(url, url, info->type(), &setting_info);
EXPECT_EQ(original_value, regular_mode_value);
}
}
// Validate that a content setting that uses a different scope/constraint can
// co-exist with another setting
// TODO(crbug.com/398993133): Fix flakes on some Android builders.
#if BUILDFLAG(IS_ANDROID)
#define MAYBE_MixedScopeSettings DISABLED_MixedScopeSettings
#else
#define MAYBE_MixedScopeSettings MixedScopeSettings
#endif
TEST_F(HostContentSettingsMapTest, MAYBE_MixedScopeSettings) {
TestingProfile profile;
HostContentSettingsMap* map =
HostContentSettingsMapFactory::GetForProfile(&profile);
content_settings::ContentSettingsRegistry::GetInstance()->ResetForTest();
ReloadProviders(profile.GetPrefs(), map);
// The following type is used as a sample of the persistent permission type.
// It can be replaced with any other type if required.
const ContentSettingsType persistent_type =
ContentSettingsType::STORAGE_ACCESS;
const GURL example_url1("https://example.com");
const GURL example_url2("https://other_site.example");
// Set default permission to ASK and expect it for a website.
map->SetDefaultContentSetting(persistent_type, CONTENT_SETTING_ASK);
EXPECT_EQ(
CONTENT_SETTING_ASK,
map->GetContentSetting(example_url1, example_url1, persistent_type));
EXPECT_EQ(
CONTENT_SETTING_ASK,
map->GetContentSetting(example_url2, example_url2, persistent_type));
// Set permission and expect to retrieve it correctly. This will default to a
// never expiring Durable permission.
map->SetContentSettingDefaultScope(example_url1, example_url1,
persistent_type, CONTENT_SETTING_BLOCK);
// Set a Session only permission for our second url and we expect it should
// co-exist with the other permission just fine.
content_settings::ContentSettingConstraints constraints;
constraints.set_session_model(
content_settings::mojom::SessionModel::USER_SESSION);
map->SetContentSettingDefaultScope(example_url2, example_url2,
persistent_type, CONTENT_SETTING_ALLOW,
constraints);
EXPECT_EQ(
CONTENT_SETTING_BLOCK,
map->GetContentSetting(example_url1, example_url1, persistent_type));
EXPECT_EQ(
CONTENT_SETTING_ALLOW,
map->GetContentSetting(example_url2, example_url2, persistent_type));
// Restart and expect reset of Session scoped permission to ASK, while keeping
// the permission of Durable scope.
ReloadProviders(profile.GetPrefs(), map);
EXPECT_EQ(
CONTENT_SETTING_BLOCK,
map->GetContentSetting(example_url1, example_url1, persistent_type));
EXPECT_EQ(
CONTENT_SETTING_ASK,
map->GetContentSetting(example_url2, example_url2, persistent_type));
}
// Validate that GetSettingsForOneType works with a SessionModel specified.
// We should act like no preference is specified if the value is
// SessionModel::None; otherwise, only the preferences from the specified
// scope should be returned (if any).
// TODO(crbug.com/399254058): flaky on Android
#if BUILDFLAG(IS_ANDROID)
#define MAYBE_GetSettingsForOneTypeWithSessionModel \
DISABLED_GetSettingsForOneTypeWithSessionModel
#else
#define MAYBE_GetSettingsForOneTypeWithSessionModel \
GetSettingsForOneTypeWithSessionModel
#endif
TEST_F(HostContentSettingsMapTest,
MAYBE_GetSettingsForOneTypeWithSessionModel) {
TestingProfile profile;
HostContentSettingsMap* map =
HostContentSettingsMapFactory::GetForProfile(&profile);
content_settings::ContentSettingsRegistry::GetInstance()->ResetForTest();
ReloadProviders(profile.GetPrefs(), map);
// The following type is used as a sample of the persistent permission type.
// It can be replaced with any other type if required.
const ContentSettingsType persistent_type =
ContentSettingsType::STORAGE_ACCESS;
const GURL example_url1("https://example.com");
const GURL example_url2("https://other_site.example");
// Set default permission to ASK and expect it for a website.
map->SetDefaultContentSetting(persistent_type, CONTENT_SETTING_ASK);
EXPECT_EQ(
CONTENT_SETTING_ASK,
map->GetContentSetting(example_url1, example_url1, persistent_type));
EXPECT_EQ(
CONTENT_SETTING_ASK,
map->GetContentSetting(example_url2, example_url2, persistent_type));
// Set permissions in two different scopes.
content_settings::ContentSettingConstraints constraints;
constraints.set_session_model(content_settings::mojom::SessionModel::DURABLE);
map->SetContentSettingDefaultScope(example_url1, example_url1,
persistent_type, CONTENT_SETTING_BLOCK,
constraints);
constraints.set_session_model(
content_settings::mojom::SessionModel::USER_SESSION);
map->SetContentSettingDefaultScope(example_url2, example_url2,
persistent_type, CONTENT_SETTING_ALLOW,
constraints);
// Validate that if we retrieve all our settings we should see both settings
// and the default values returned.
ContentSettingsForOneType settings =
map->GetSettingsForOneType(persistent_type);
ASSERT_EQ(3u, settings.size());
// Validate that using no SessionModel functions the exact same way.
settings = map->GetSettingsForOneType(persistent_type, std::nullopt);
ASSERT_EQ(3u, settings.size());
// Each one/type of settings we set should be retrievable by specifying the
// specific scope without getting any of the other results. For Durable we
// should see our set value and the default value.
settings = map->GetSettingsForOneType(
persistent_type, content_settings::mojom::SessionModel::DURABLE);
ASSERT_EQ(2u, settings.size());
settings = map->GetSettingsForOneType(
persistent_type, content_settings::mojom::SessionModel::USER_SESSION);
ASSERT_EQ(1u, settings.size());
// Reload to clear our session settings.
ReloadProviders(profile.GetPrefs(), map);
// If a scope is specified that has no settings, we should get an empty set
// returned.
settings = map->GetSettingsForOneType(
persistent_type, content_settings::mojom::SessionModel::USER_SESSION);
ASSERT_EQ(0u, settings.size());
}
class HostContentSettingsMapActiveExpirationTest
: public HostContentSettingsMapTest,
public ::testing::WithParamInterface<bool> {
public:
HostContentSettingsMapActiveExpirationTest() {
feature_list_.InitWithFeatureState(
content_settings::features::kActiveContentSettingExpiry, GetParam());
}
HostContentSettingsMapActiveExpirationTest(
const HostContentSettingsMapActiveExpirationTest&) = delete;
HostContentSettingsMapActiveExpirationTest& operator=(
const HostContentSettingsMapActiveExpirationTest&) = delete;
void RunExpirationMechanismIfFeatureEnabled(HostContentSettingsMap* map,
ContentSettingsType type) {
if (GetParam()) {
map->DeleteNearlyExpiredSettingsAndMaybeScheduleNextRun(type);
}
}
private:
base::test::ScopedFeatureList feature_list_;
};
INSTANTIATE_TEST_SUITE_P(All,
HostContentSettingsMapActiveExpirationTest,
testing::Bool());
// Validate that the settings array retrieved correctly carries the expiry data
// for settings and they can detect if and when they expire.
// GetSettingsForOneType should also omit any settings that are already expired.
// TODO(crbug.com/398993133): Fix flakes on some Android builders.
#if BUILDFLAG(IS_ANDROID)
#define MAYBE_GetSettingsForOneTypeWithExpiryAndVerifyUmaHistograms DISABLED_GetSettingsForOneTypeWithExpiryAndVerifyUmaHistograms
#else
#define MAYBE_GetSettingsForOneTypeWithExpiryAndVerifyUmaHistograms GetSettingsForOneTypeWithExpiryAndVerifyUmaHistograms
#endif
TEST_P(HostContentSettingsMapActiveExpirationTest,
MAYBE_GetSettingsForOneTypeWithExpiryAndVerifyUmaHistograms) {
base::HistogramTester t;
TestingProfile profile;
HostContentSettingsMap* map =
HostContentSettingsMapFactory::GetForProfile(&profile);
content_settings::ContentSettingsRegistry::GetInstance()->ResetForTest();
ReloadProviders(profile.GetPrefs(), map);
// The following type is used as a sample of the persistent permission
// type. It can be replaced with any other type if required.
const ContentSettingsType persistent_type =
ContentSettingsType::STORAGE_ACCESS;
const GURL example_url1("https://example.com");
const GURL example_url2("https://other_site.example");
const GURL example_url3("https://third_site.example");
// Set default permission to ASK and expect it for a website.
map->SetDefaultContentSetting(persistent_type, CONTENT_SETTING_ASK);
EXPECT_EQ(
CONTENT_SETTING_ASK,
map->GetContentSetting(example_url1, example_url1, persistent_type));
EXPECT_EQ(
CONTENT_SETTING_ASK,
map->GetContentSetting(example_url2, example_url2, persistent_type));
EXPECT_EQ(
CONTENT_SETTING_ASK,
map->GetContentSetting(example_url3, example_url3, persistent_type));
// Set permissions with our first two urls with different expiry times and our
// third with no expiration.
content_settings::ContentSettingConstraints constraints;
constraints.set_lifetime(base::Seconds(100));
constraints.set_session_model(
content_settings::mojom::SessionModel::USER_SESSION);
map->SetContentSettingDefaultScope(example_url1, example_url1,
persistent_type, CONTENT_SETTING_BLOCK,
constraints);
constraints.set_lifetime(base::Seconds(200));
map->SetContentSettingDefaultScope(example_url2, example_url2,
persistent_type, CONTENT_SETTING_ALLOW,
constraints);
constraints.set_lifetime(base::TimeDelta());
map->SetContentSettingDefaultScope(example_url3, example_url3,
persistent_type, CONTENT_SETTING_ALLOW,
constraints);
// Validate that we can retrieve all our settings and none of them are
// expired.
ContentSettingsForOneType settings = map->GetSettingsForOneType(
persistent_type, content_settings::mojom::SessionModel::USER_SESSION);
ASSERT_EQ(3u, settings.size());
// None of our current settings should be expired, but we'll keep each one to
// check the validity later on.
ContentSettingPatternSource url1_setting;
ContentSettingPatternSource url2_setting;
ContentSettingPatternSource url3_setting;
for (const auto& entry : settings) {
ASSERT_FALSE(entry.IsExpired());
// Store the relevant settings to check on later.
if (entry.primary_pattern.Matches(example_url1)) {
url1_setting = entry;
} else if (entry.primary_pattern.Matches(example_url2)) {
url2_setting = entry;
} else if (entry.primary_pattern.Matches(example_url3)) {
url3_setting = entry;
}
}
RunExpirationMechanismIfFeatureEnabled(map,
ContentSettingsType::STORAGE_ACCESS);
// If we Fastforward by 101 seconds we should see only our first setting is
// expired, we now retrieve 1 less setting and the rest are okay.
FastForwardTime(base::Seconds(101));
RunExpirationMechanismIfFeatureEnabled(map,
ContentSettingsType::STORAGE_ACCESS);
ASSERT_TRUE(url1_setting.IsExpired());
ASSERT_FALSE(url2_setting.IsExpired());
ASSERT_FALSE(url3_setting.IsExpired());
settings = map->GetSettingsForOneType(
persistent_type, content_settings::mojom::SessionModel::USER_SESSION);
ASSERT_EQ(2u, settings.size());
// If we fast forward again we should expire our second setting and drop if
// from our retrieval list now.
FastForwardTime(base::Seconds(101));
RunExpirationMechanismIfFeatureEnabled(map,
ContentSettingsType::STORAGE_ACCESS);
ASSERT_TRUE(url1_setting.IsExpired());
ASSERT_TRUE(url2_setting.IsExpired());
ASSERT_FALSE(url3_setting.IsExpired());
settings = map->GetSettingsForOneType(
persistent_type, content_settings::mojom::SessionModel::USER_SESSION);
ASSERT_EQ(1u, settings.size());
// If we fast forwarding much further it shouldn't make a difference as our
// last setting and the default setting should never expire.
FastForwardTime(base::Minutes(100));
RunExpirationMechanismIfFeatureEnabled(map,
ContentSettingsType::STORAGE_ACCESS);
ASSERT_TRUE(url1_setting.IsExpired());
ASSERT_TRUE(url2_setting.IsExpired());
ASSERT_FALSE(url3_setting.IsExpired());
settings = map->GetSettingsForOneType(
persistent_type, content_settings::mojom::SessionModel::USER_SESSION);
ASSERT_EQ(1u, settings.size());
}
TEST_F(HostContentSettingsMapTest, StorageAccessMetrics) {
const ContentSettingsType type = ContentSettingsType::STORAGE_ACCESS;
const GURL url1("https://example1.com");
const GURL url2("https://example2.com");
const GURL url3("https://example3.com");
const GURL url4("https://example4.com");
TestingProfile profile;
auto* map = HostContentSettingsMapFactory::GetForProfile(&profile);
map->SetContentSettingDefaultScope(url1, url1, type, CONTENT_SETTING_ALLOW);
map->SetContentSettingDefaultScope(url2, url1, type, CONTENT_SETTING_ALLOW);
map->SetContentSettingDefaultScope(url2, url2, type, CONTENT_SETTING_ALLOW);
map->SetContentSettingDefaultScope(url3, url1, type, CONTENT_SETTING_ALLOW);
map->SetContentSettingDefaultScope(url3, url2, type, CONTENT_SETTING_ALLOW);
map->SetContentSettingDefaultScope(url3, url3, type, CONTENT_SETTING_BLOCK);
map->SetContentSettingDefaultScope(url4, url1, type, CONTENT_SETTING_BLOCK);
base::HistogramTester t;
auto map2 = base::MakeRefCounted<HostContentSettingsMap>(
profile.GetPrefs(), false, true, true, true);
map2->ShutdownOnUIThread();
std::string base_histogram =
"ContentSettings.RegularProfile.Exceptions.storage-access";
t.ExpectUniqueSample(base_histogram, 7, 1);
t.ExpectUniqueSample(base_histogram + ".Allow", 5, 1);
t.ExpectUniqueSample(base_histogram + ".Block", 2, 1);
t.ExpectUniqueSample(base_histogram + ".MaxRequester", 3, 1);
t.ExpectUniqueSample(base_histogram + ".MaxTopLevel", 4, 1);
}
// TODO(crbug.com/398993133): Fix flakes on some Android builders.
#if BUILDFLAG(IS_ANDROID)
#define MAYBE_RenewContentSetting DISABLED_RenewContentSetting
#else
#define MAYBE_RenewContentSetting RenewContentSetting
#endif
TEST_F(HostContentSettingsMapTest, MAYBE_RenewContentSetting) {
TestingProfile profile;
const base::Time now = base::Time::Now();
const base::Time plus_1_hour = now + base::Hours(1);
const base::TimeDelta lifetime = base::Days(30);
const GURL primary_url("https://primary.com");
const GURL secondary_url("https://secondary.com");
base::SimpleTestClock test_clock;
test_clock.SetNow(now);
auto* map = HostContentSettingsMapFactory::GetForProfile(&profile);
map->SetClockForTesting(&test_clock);
content_settings::ContentSettingConstraints constraints(plus_1_hour -
lifetime);
constraints.set_lifetime(lifetime);
map->SetContentSettingDefaultScope(
primary_url, secondary_url, ContentSettingsType::STORAGE_ACCESS,
ContentSetting::CONTENT_SETTING_ALLOW, constraints);
EXPECT_EQ(map->RenewContentSetting(primary_url, secondary_url,
ContentSettingsType::STORAGE_ACCESS,
ContentSetting::CONTENT_SETTING_ALLOW),
std::make_optional(base::Hours(1)));
}
TEST_F(HostContentSettingsMapTest, Increments3pcSettingsMetrics) {
TestingProfile profile;
ContentSettingsPattern wildcard_pattern =
ContentSettingsPattern::FromString("[*.]foo.com");
ContentSettingsPattern literal_pattern =
ContentSettingsPattern::FromString("bar.com");
ContentSettingsPattern literal_pattern2 =
ContentSettingsPattern::FromString("baz.com");
auto* map = HostContentSettingsMapFactory::GetForProfile(&profile);
content_settings::ContentSettingConstraints constraints;
constraints.set_lifetime(base::Seconds(100));
map->SetContentSettingCustomScope(
ContentSettingsPattern::Wildcard(), literal_pattern,
ContentSettingsType::COOKIES, CONTENT_SETTING_ALLOW, constraints);
map->SetContentSettingCustomScope(
ContentSettingsPattern::Wildcard(), literal_pattern2,
ContentSettingsType::COOKIES, CONTENT_SETTING_ALLOW);
map->SetContentSettingCustomScope(
ContentSettingsPattern::Wildcard(), wildcard_pattern,
ContentSettingsType::COOKIES, CONTENT_SETTING_ALLOW);
base::HistogramTester t;
auto map2 = base::MakeRefCounted<HostContentSettingsMap>(
profile.GetPrefs(), false, true, true, true);
map2->ShutdownOnUIThread();
std::string base_histogram =
"ContentSettings.RegularProfile.Exceptions.cookies";
t.ExpectUniqueSample(base_histogram + ".AllowThirdParty", 3, 1);
t.ExpectUniqueSample(base_histogram + ".TemporaryAllowThirdParty", 1, 1);
t.ExpectUniqueSample(base_histogram + ".DomainWildcardAllowThirdParty", 1, 1);
}
// Regression test for https://crbug.com/1497777.
TEST_F(HostContentSettingsMapTest, IncognitoInheritSaaAndRenew) {
TestingProfile profile;
GURL host("https://example.com/");
auto type = ContentSettingsType::STORAGE_ACCESS;
// Create StorageAccess permission in regular profile.
auto* map = HostContentSettingsMapFactory::GetForProfile(&profile);
content_settings::ContentSettingConstraints constraint;
constraint.set_lifetime(base::Hours(1));
map->SetContentSettingDefaultScope(host, host, type, CONTENT_SETTING_ALLOW,
constraint);
// Create OTR profile.
Profile* otr_profile =
profile.GetPrimaryOTRProfile(/*create_if_needed=*/true);
auto* otr_map = HostContentSettingsMapFactory::GetForProfile(otr_profile);
// Check that only the regular profile is allowed.
EXPECT_EQ(CONTENT_SETTING_ALLOW, map->GetContentSetting(host, host, type));
EXPECT_EQ(CONTENT_SETTING_ASK, otr_map->GetContentSetting(host, host, type));
// Renew the setting on the OTR profile and check that it still returns ASK.
otr_map->RenewContentSetting(host, host, type,
ContentSetting::CONTENT_SETTING_ALLOW);
EXPECT_EQ(CONTENT_SETTING_ASK, otr_map->GetContentSetting(host, host, type));
}
TEST_F(HostContentSettingsMapTest, ShutdownDuringExpirationAsanTest) {
TestingProfile profile;
auto host_content_settings_map = base::MakeRefCounted<HostContentSettingsMap>(
profile.GetPrefs(), false, true, true, true);
ContentSettingsPattern pattern =
ContentSettingsPattern::FromString("[*.]example.com");
base::TimeDelta ttl = base::Seconds(1);
content_settings::ContentSettingConstraints constraints;
constraints.set_lifetime(ttl);
host_content_settings_map->SetContentSettingCustomScope(
pattern, ContentSettingsPattern::Wildcard(),
ContentSettingsType::GEOLOCATION, CONTENT_SETTING_ALLOW, constraints);
host_content_settings_map->ShutdownOnUIThread();
FastForwardTime(ttl);
}
TEST_F(HostContentSettingsMapTest, TrackingProtectionMetrics) {
const ContentSettingsType type = ContentSettingsType::TRACKING_PROTECTION;
TestingProfile profile;
auto* map = HostContentSettingsMapFactory::GetForProfile(&profile);
map->SetContentSettingCustomScope(
ContentSettingsPattern::Wildcard(),
ContentSettingsPattern::FromString("https://example1.com"), type,
CONTENT_SETTING_ALLOW);
map->SetContentSettingCustomScope(
ContentSettingsPattern::Wildcard(),
ContentSettingsPattern::FromString("https://example2.com"), type,
CONTENT_SETTING_ALLOW);
map->SetContentSettingCustomScope(
ContentSettingsPattern::Wildcard(),
ContentSettingsPattern::FromString("https://example3.com"), type,
CONTENT_SETTING_ALLOW);
base::HistogramTester t;
auto map2 = base::MakeRefCounted<HostContentSettingsMap>(
profile.GetPrefs(), false, true, true, true);
map2->ShutdownOnUIThread();
t.ExpectUniqueSample(
"ContentSettings.RegularProfile.Exceptions.tracking-protection", 3, 1);
}
// File access is not implemented on Android. Luckily we don't need it for
// DevTools.
#if !BUILDFLAG(IS_ANDROID)
TEST_F(HostContentSettingsMapTest, DevToolsFileAccess) {
TestingProfile profile;
HostContentSettingsMap* host_content_settings_map =
HostContentSettingsMapFactory::GetForProfile(&profile);
GURL devtools_host("devtools://devtools/bundled/devtools_app.html");
GURL example_host("https://example.com");
host_content_settings_map->SetDefaultContentSetting(
ContentSettingsType::FILE_SYSTEM_WRITE_GUARD, CONTENT_SETTING_BLOCK);
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetContentSetting(
devtools_host, devtools_host,
ContentSettingsType::FILE_SYSTEM_WRITE_GUARD));
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetContentSetting(
example_host, example_host,
ContentSettingsType::FILE_SYSTEM_WRITE_GUARD));
}
#endif // !BUILDFLAG(IS_ANDROID)
|