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
|
// 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/page_info/page_info.h"
#include <memory>
#include <optional>
#include <set>
#include <string>
#include <utility>
#include <vector>
#include "base/at_exit.h"
#include "base/functional/bind.h"
#include "base/run_loop.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "build/build_config.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/content_settings/page_specific_content_settings_delegate.h"
#include "chrome/browser/file_system_access/file_system_access_features.h"
#include "chrome/browser/ssl/stateful_ssl_host_state_delegate_factory.h"
#include "chrome/browser/subresource_filter/subresource_filter_profile_context_factory.h"
#include "chrome/browser/ui/page_info/chrome_page_info_delegate.h"
#include "chrome/browser/ui/page_info/chrome_page_info_ui_delegate.h"
#include "chrome/browser/usb/usb_chooser_context.h"
#include "chrome/browser/usb/usb_chooser_context_factory.h"
#include "chrome/common/chrome_features.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "chrome/test/base/scoped_testing_local_state.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "components/content_settings/core/browser/host_content_settings_map.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_types.h"
#include "components/content_settings/core/common/cookie_blocking_3pcd_status.h"
#include "components/infobars/content/content_infobar_manager.h"
#include "components/infobars/core/infobar.h"
#include "components/page_info/core/features.h"
#include "components/page_info/page_info_ui.h"
#include "components/permissions/features.h"
#include "components/permissions/permission_recovery_success_rate_tracker.h"
#include "components/safe_browsing/buildflags.h"
#include "components/security_interstitials/content/stateful_ssl_host_state_delegate.h"
#include "components/strings/grit/components_strings.h"
#include "components/subresource_filter/content/browser/subresource_filter_content_settings_manager.h"
#include "components/subresource_filter/content/browser/subresource_filter_profile_context.h"
#include "content/public/browser/ssl_host_state_delegate.h"
#include "content/public/browser/ssl_status.h"
#include "content/public/common/content_switches.h"
#include "content/public/test/web_contents_tester.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "net/base/features.h"
#include "net/base/net_errors.h"
#include "net/cert/cert_status_flags.h"
#include "net/cert/x509_certificate.h"
#include "net/ssl/ssl_connection_status_flags.h"
#include "net/test/cert_test_util.h"
#include "net/test/test_certificate_data.h"
#include "net/test/test_data_directory.h"
#include "ppapi/buildflags/buildflags.h"
#include "services/device/public/cpp/test/fake_usb_device_manager.h"
#include "services/device/public/mojom/usb_device.mojom.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/l10n/l10n_util.h"
#include "url/origin.h"
#if BUILDFLAG(IS_ANDROID)
#include "chrome/browser/android/android_theme_resources.h"
#else
#include "chrome/browser/ui/views/chrome_layout_provider.h"
#include "chrome/common/pref_names.h"
#include "components/prefs/pref_service.h"
#include "media/base/media_switches.h"
#endif
#if BUILDFLAG(IS_CHROMEOS)
#include "chrome/browser/ash/login/users/fake_chrome_user_manager.h"
#include "components/user_manager/scoped_user_manager.h"
#endif // BUILDFLAG(IS_CHROMEOS)
using content::SSLStatus;
using content_settings::SettingSource;
using testing::_;
using testing::AnyNumber;
using testing::Invoke;
using testing::Mock;
using testing::NiceMock;
using testing::Return;
using testing::SetArgPointee;
namespace {
// SSL cipher suite like specified in RFC5246 Appendix A.5. "The Cipher Suite".
// Without the CR_ prefix, this clashes with the OS X 10.8 headers.
int CR_TLS_RSA_WITH_AES_256_CBC_SHA256 = 0x3D;
int SetSSLVersion(int connection_status, int version) {
// Clear SSL version bits (Bits 20, 21 and 22).
connection_status &=
~(net::SSL_CONNECTION_VERSION_MASK << net::SSL_CONNECTION_VERSION_SHIFT);
int bitmask = version << net::SSL_CONNECTION_VERSION_SHIFT;
return bitmask | connection_status;
}
int SetSSLCipherSuite(int connection_status, int cipher_suite) {
// Clear cipher suite bits (the 16 lowest bits).
connection_status &= ~net::SSL_CONNECTION_CIPHERSUITE_MASK;
return cipher_suite | connection_status;
}
class MockPageInfoUI : public PageInfoUI {
public:
~MockPageInfoUI() override = default;
MOCK_METHOD(void, SetCookieInfo, (const CookiesNewInfo& cookie_info));
MOCK_METHOD(void, SetPermissionInfoStub, ());
MOCK_METHOD(void, SetIdentityInfo, (const IdentityInfo& identity_info));
MOCK_METHOD(void, SetPageFeatureInfo, (const PageFeatureInfo& info));
MOCK_METHOD(void,
SetAdPersonalizationInfo,
(const AdPersonalizationInfo& info));
void SetPermissionInfo(
const PermissionInfoList& permission_info_list,
ChosenObjectInfoList chosen_object_info_list) override {
SetPermissionInfoStub();
if (set_permission_info_callback_) {
set_permission_info_callback_.Run(permission_info_list,
std::move(chosen_object_info_list));
}
}
base::RepeatingCallback<void(const PermissionInfoList& permission_info_list,
ChosenObjectInfoList chosen_object_info_list)>
set_permission_info_callback_;
};
class PageInfoTest : public ChromeRenderViewHostTestHarness {
public:
PageInfoTest() : testing_local_state_(TestingBrowserProcess::GetGlobal()) {
SetURL("http://www.example.com");
}
~PageInfoTest() override = default;
void SetUp() override {
// TODO(crbug.com/40231917): Fix tests and enable the feature.
scoped_feature_list_.InitWithFeatures(
{
#if !BUILDFLAG(IS_ANDROID)
features::kFileSystemAccessPersistentPermissions,
#endif
},
{});
ChromeRenderViewHostTestHarness::SetUp();
// Setup stub security info.
security_level_ = security_state::NONE;
// Create the certificate.
cert_ =
net::ImportCertFromFile(net::GetTestCertsDirectory(), "ok_cert.pem");
ASSERT_TRUE(cert_);
#if BUILDFLAG(IS_CHROMEOS)
fake_user_manager_->AddUserWithAffiliation(
AccountId::FromUserEmail(profile()->GetProfileUserName()),
/*is_affiliated=*/true);
#endif // BUILDFLAG(IS_CHROMEOS)
CreateWebContentsUserData(web_contents());
// Setup mock ui.
ResetMockUI();
}
void TearDown() override {
ASSERT_TRUE(page_info_ || incognito_page_info_)
<< "No PageInfo instance created.";
incognito_web_contents_.reset();
page_info_.reset();
incognito_page_info_.reset();
ChromeRenderViewHostTestHarness::TearDown();
}
TestingProfile::TestingFactories GetTestingFactories() const override {
return {TestingProfile::TestingFactory{
StatefulSSLHostStateDelegateFactory::GetInstance(),
StatefulSSLHostStateDelegateFactory::GetDefaultFactoryForTesting()}};
}
void SetDefaultUIExpectations(MockPageInfoUI* mock_ui) {
// During creation |PageInfo| makes the following calls to the ui.
EXPECT_CALL(*mock_ui, SetPermissionInfoStub());
EXPECT_CALL(*mock_ui, SetIdentityInfo(_));
ExpectInitialSetCookieInfoCall(mock_ui);
}
void ExpectInitialSetCookieInfoCall(MockPageInfoUI* mock_ui) {
#if !BUILDFLAG(IS_ANDROID)
EXPECT_CALL(*mock_ui, SetCookieInfo(_)).Times(1);
#else
EXPECT_CALL(*mock_ui, SetCookieInfo(_));
#endif
}
void SetURL(const std::string& url) {
url_ = GURL(url);
origin_ = url::Origin::Create(url_);
}
void SetPermissionInfo(const PermissionInfoList& permission_info_list,
ChosenObjectInfoList chosen_object_info_list) {
last_chosen_object_info_.clear();
for (auto& chosen_object_info : chosen_object_info_list) {
last_chosen_object_info_.push_back(std::move(chosen_object_info));
}
last_permission_info_list_ = permission_info_list;
}
void ResetMockUI() {
mock_ui_ = std::make_unique<NiceMock<MockPageInfoUI>>();
// Use this rather than gmock's ON_CALL.WillByDefault(Invoke(... because
// gmock doesn't handle move-only types well.
mock_ui_->set_permission_info_callback_ = base::BindRepeating(
&PageInfoTest::SetPermissionInfo, base::Unretained(this));
}
void ClearPageInfo() { page_info_.reset(nullptr); }
void SetCertToSHA1() {
cert_ =
net::ImportCertFromFile(net::GetTestCertsDirectory(), "sha1_leaf.pem");
ASSERT_TRUE(cert_);
}
const GURL& url() const { return url_; }
const url::Origin& origin() const { return origin_; }
scoped_refptr<net::X509Certificate> cert() { return cert_; }
MockPageInfoUI* mock_ui() { return mock_ui_.get(); }
const std::vector<std::unique_ptr<PageInfoUI::ChosenObjectInfo>>&
last_chosen_object_info() {
return last_chosen_object_info_;
}
const PermissionInfoList& last_permission_info_list() {
return last_permission_info_list_;
}
infobars::ContentInfoBarManager* infobar_manager() {
return infobars::ContentInfoBarManager::FromWebContents(web_contents());
}
PageInfo* page_info() {
if (!page_info_.get()) {
auto delegate = std::make_unique<ChromePageInfoDelegate>(web_contents());
delegate->SetSecurityStateForTests(security_level_,
visible_security_state_);
page_info_ = std::make_unique<PageInfo>(std::move(delegate),
web_contents(), url());
base::RunLoop run_loop;
page_info_->InitializeUiState(mock_ui(), run_loop.QuitClosure());
run_loop.Run();
}
return page_info_.get();
}
PageInfo* incognito_page_info() {
if (!incognito_page_info_.get()) {
// Build the incognito profile manually in order to override testing
// factories.
TestingProfile::Builder incognito_profile_builder;
incognito_profile_builder.AddTestingFactories(GetTestingFactories());
incognito_profile_builder.BuildIncognito(profile());
incognito_web_contents_ =
content::WebContentsTester::CreateTestWebContents(
profile()->GetPrimaryOTRProfile(/*create_if_needed=*/true),
nullptr);
CreateWebContentsUserData(incognito_web_contents_.get());
incognito_mock_ui_ = std::make_unique<NiceMock<MockPageInfoUI>>();
incognito_mock_ui_->set_permission_info_callback_ = base::BindRepeating(
&PageInfoTest::SetPermissionInfo, base::Unretained(this));
auto delegate = std::make_unique<ChromePageInfoDelegate>(
incognito_web_contents_.get());
delegate->SetSecurityStateForTests(security_level_,
visible_security_state_);
incognito_page_info_ = std::make_unique<PageInfo>(
std::move(delegate), incognito_web_contents_.get(), url());
base::RunLoop run_loop;
incognito_page_info_->InitializeUiState(incognito_mock_ui_.get(),
run_loop.QuitClosure());
run_loop.Run();
}
return incognito_page_info_.get();
}
void CreateWebContentsUserData(content::WebContents* contents) {
// The test WebContents don't have all the helpers attached, so add in the
// missing ones needed by these tests.
infobars::ContentInfoBarManager::CreateForWebContents(contents);
content_settings::PageSpecificContentSettings::CreateForWebContents(
contents,
std::make_unique<PageSpecificContentSettingsDelegate>(contents));
permissions::PermissionRecoverySuccessRateTracker::CreateForWebContents(
contents);
}
security_state::SecurityLevel security_level_;
security_state::VisibleSecurityState visible_security_state_;
private:
ScopedTestingLocalState testing_local_state_;
std::unique_ptr<PageInfo> page_info_;
std::unique_ptr<MockPageInfoUI> mock_ui_;
std::unique_ptr<content::WebContents> incognito_web_contents_;
std::unique_ptr<PageInfo> incognito_page_info_;
std::unique_ptr<NiceMock<MockPageInfoUI>> incognito_mock_ui_;
#if !BUILDFLAG(IS_ANDROID)
ChromeLayoutProvider layout_provider_;
#endif
#if BUILDFLAG(IS_CHROMEOS)
user_manager::TypedScopedUserManager<ash::FakeChromeUserManager>
fake_user_manager_{std::make_unique<ash::FakeChromeUserManager>()};
#endif
scoped_refptr<net::X509Certificate> cert_;
GURL url_;
url::Origin origin_;
std::vector<std::unique_ptr<PageInfoUI::ChosenObjectInfo>>
last_chosen_object_info_;
PermissionInfoList last_permission_info_list_;
base::test::ScopedFeatureList scoped_feature_list_;
};
void ExpectPermissionInfoList(
const std::set<ContentSettingsType>& expected_types,
const PermissionInfoList& permissions,
const base::Location& location = FROM_HERE) {
std::set<ContentSettingsType> actual_types;
std::ranges::transform(permissions,
std::inserter(actual_types, actual_types.end()),
[](const auto& p) { return p.type; });
EXPECT_THAT(actual_types, expected_types)
<< "(expected at " << location.ToString() << ")";
}
} // namespace
TEST_F(PageInfoTest, PermissionStringsHaveMidSentenceVersion) {
page_info();
for (const PageInfoUI::PermissionUIInfo& info :
PageInfoUI::GetContentSettingsUIInfoForTesting()) {
std::u16string normal = l10n_util::GetStringUTF16(info.string_id);
std::u16string mid_sentence =
l10n_util::GetStringUTF16(info.string_id_mid_sentence);
switch (info.type) {
case ContentSettingsType::MIDI_SYSEX:
case ContentSettingsType::NFC:
case ContentSettingsType::USB_GUARD:
#if !BUILDFLAG(IS_ANDROID)
case ContentSettingsType::HID_GUARD:
#endif
EXPECT_EQ(normal, mid_sentence);
break;
#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_WIN)
case ContentSettingsType::PROTECTED_MEDIA_IDENTIFIER:
EXPECT_NE(normal, mid_sentence);
EXPECT_EQ(base::ToLowerASCII(normal), base::ToLowerASCII(mid_sentence));
break;
#endif
default:
EXPECT_NE(normal, mid_sentence);
EXPECT_EQ(base::ToLowerASCII(normal), mid_sentence);
break;
}
}
}
TEST_F(PageInfoTest, NonFactoryDefaultAndRecentlyChangedPermissionsShown) {
GURL kEmbedded1("https://embedded1.com");
GURL kEmbedded2("https://embedded2.com");
page_info()->PresentSitePermissionsForTesting();
std::set<ContentSettingsType> expected_visible_permissions;
#if BUILDFLAG(IS_ANDROID)
// Geolocation is always allowed to pass through to Android-specific logic to
// check for DSE settings (so expect 1 item), but isn't actually shown later
// on because this test isn't testing with a default search engine origin.
expected_visible_permissions.insert(ContentSettingsType::GEOLOCATION);
#endif
ExpectPermissionInfoList(expected_visible_permissions,
last_permission_info_list());
// Change some default-ask settings away from the default.
page_info()->OnSitePermissionChanged(ContentSettingsType::GEOLOCATION,
CONTENT_SETTING_ALLOW,
/*requesting_origin=*/std::nullopt,
/*is_one_time=*/false);
expected_visible_permissions.insert(ContentSettingsType::GEOLOCATION);
page_info()->OnSitePermissionChanged(ContentSettingsType::NOTIFICATIONS,
CONTENT_SETTING_ALLOW,
/*requesting_origin=*/std::nullopt,
/*is_one_time=*/false);
expected_visible_permissions.insert(ContentSettingsType::NOTIFICATIONS);
page_info()->OnSitePermissionChanged(ContentSettingsType::MEDIASTREAM_MIC,
CONTENT_SETTING_ALLOW,
/*requesting_origin=*/std::nullopt,
/*is_one_time=*/false);
expected_visible_permissions.insert(ContentSettingsType::MEDIASTREAM_MIC);
page_info()->OnSitePermissionChanged(ContentSettingsType::STORAGE_ACCESS,
CONTENT_SETTING_ALLOW,
url::Origin::Create(kEmbedded1),
/*is_one_time=*/false);
expected_visible_permissions.insert(ContentSettingsType::STORAGE_ACCESS);
#if !BUILDFLAG(IS_ANDROID)
page_info()->OnSitePermissionChanged(
ContentSettingsType::FILE_SYSTEM_WRITE_GUARD, CONTENT_SETTING_ALLOW,
url::Origin::Create(kEmbedded1),
/*is_one_time=*/false);
expected_visible_permissions.insert(
ContentSettingsType::FILE_SYSTEM_WRITE_GUARD);
#endif
ExpectPermissionInfoList(expected_visible_permissions,
last_permission_info_list());
expected_visible_permissions.insert(ContentSettingsType::POPUPS);
// Change a default-block setting to a user-preference block instead.
page_info()->OnSitePermissionChanged(ContentSettingsType::POPUPS,
CONTENT_SETTING_BLOCK,
/*requesting_origin=*/std::nullopt,
/*is_one_time=*/false);
ExpectPermissionInfoList(expected_visible_permissions,
last_permission_info_list());
expected_visible_permissions.insert(ContentSettingsType::JAVASCRIPT);
// Change a default-allow setting away from the default.
page_info()->OnSitePermissionChanged(ContentSettingsType::JAVASCRIPT,
CONTENT_SETTING_BLOCK,
/*requesting_origin=*/std::nullopt,
/*is_one_time=*/false);
ExpectPermissionInfoList(expected_visible_permissions,
last_permission_info_list());
// Make sure changing a setting to its default causes it to show up, since it
// has been recently changed.
expected_visible_permissions.insert(ContentSettingsType::MEDIASTREAM_CAMERA);
page_info()->OnSitePermissionChanged(ContentSettingsType::MEDIASTREAM_CAMERA,
CONTENT_SETTING_DEFAULT,
/*requesting_origin=*/std::nullopt,
/*is_one_time=*/false);
ExpectPermissionInfoList(expected_visible_permissions,
last_permission_info_list());
// Set the Javascript setting to default should keep it shown.
page_info()->OnSitePermissionChanged(ContentSettingsType::JAVASCRIPT,
CONTENT_SETTING_DEFAULT,
/*requesting_origin=*/std::nullopt,
/*is_one_time=*/false);
ExpectPermissionInfoList(expected_visible_permissions,
last_permission_info_list());
// Change the default setting for Javascript away from the factory default.
HostContentSettingsMapFactory::GetForProfile(profile())
->SetDefaultContentSetting(ContentSettingsType::JAVASCRIPT,
CONTENT_SETTING_BLOCK);
page_info()->PresentSitePermissionsForTesting();
ExpectPermissionInfoList(expected_visible_permissions,
last_permission_info_list());
// Change it back to ALLOW, which is its factory default, but has a source
// from the user preference (i.e. it counts as non-factory default).
page_info()->OnSitePermissionChanged(ContentSettingsType::JAVASCRIPT,
CONTENT_SETTING_ALLOW,
/*requesting_origin=*/std::nullopt,
/*is_one_time=*/false);
ExpectPermissionInfoList(expected_visible_permissions,
last_permission_info_list());
// Adding another storage access permission for a different embedded origin
// adds an additional entry.
page_info()->OnSitePermissionChanged(ContentSettingsType::STORAGE_ACCESS,
CONTENT_SETTING_ALLOW,
url::Origin::Create(kEmbedded2),
/*is_one_time=*/false);
EXPECT_EQ(expected_visible_permissions.size() + 1,
last_permission_info_list().size());
}
TEST_F(PageInfoTest, StorageAccessGrantsAreFiltered) {
GURL kEmbedded1("https://embedded1.com");
ContentSettingsType type = ContentSettingsType::STORAGE_ACCESS;
std::set<ContentSettingsType> expected_visible_permissions;
auto* map = HostContentSettingsMapFactory::GetForProfile(profile());
// First-party exceptions are hidden.
map->SetContentSettingDefaultScope(url(), url(), type, CONTENT_SETTING_ALLOW);
// First-party-set exceptions are hidden based on their
// `decided_by_related_website_sets`.
content_settings::ContentSettingConstraints constraint;
constraint.set_session_model(content_settings::mojom::SessionModel::DURABLE);
constraint.set_decided_by_related_website_sets(true);
map->SetContentSettingDefaultScope(kEmbedded1, url(), type,
CONTENT_SETTING_ALLOW, constraint);
page_info()->PresentSitePermissionsForTesting();
#if BUILDFLAG(IS_ANDROID)
// Geolocation is always allowed to pass through to Android-specific logic to
// check for DSE settings (so expect 1 item), but isn't actually shown later
// on because this test isn't testing with a default search engine origin.
expected_visible_permissions.insert(ContentSettingsType::GEOLOCATION);
#endif
ExpectPermissionInfoList(expected_visible_permissions,
last_permission_info_list());
map->SetContentSettingDefaultScope(kEmbedded1, url(), type,
CONTENT_SETTING_ALLOW);
page_info()->PresentSitePermissionsForTesting();
expected_visible_permissions.insert(type);
ExpectPermissionInfoList(expected_visible_permissions,
last_permission_info_list());
}
TEST_F(PageInfoTest, StorageAccessGrantsDisplayedWhenDefaultBlocked) {
GURL kEmbedded1("https://embedded1.com");
ContentSettingsType type = ContentSettingsType::STORAGE_ACCESS;
std::set<ContentSettingsType> expected_visible_permissions;
auto* map = HostContentSettingsMapFactory::GetForProfile(profile());
// Nothing is displayed for default permissions.
map->SetDefaultContentSetting(type, CONTENT_SETTING_BLOCK);
page_info()->PresentSitePermissionsForTesting();
#if BUILDFLAG(IS_ANDROID)
// Geolocation is always allowed to pass through to Android-specific logic to
// check for DSE settings (so expect 1 item), but isn't actually shown later
// on because this test isn't testing with a default search engine origin.
expected_visible_permissions.insert(ContentSettingsType::GEOLOCATION);
#endif
ExpectPermissionInfoList(expected_visible_permissions,
last_permission_info_list());
// Until the permission is accessed and blocked.
auto* pscs = content_settings::PageSpecificContentSettings::GetForFrame(
web_contents()->GetPrimaryMainFrame());
pscs->OnTwoSitePermissionChanged(ContentSettingsType::STORAGE_ACCESS,
net::SchemefulSite(kEmbedded1),
CONTENT_SETTING_BLOCK);
page_info()->PresentSitePermissionsForTesting();
expected_visible_permissions.insert(type);
ExpectPermissionInfoList(expected_visible_permissions,
last_permission_info_list());
}
TEST_F(PageInfoTest, ShowAutograntedRWSPermissions) {
std::set<ContentSettingsType> expected_visible_permissions;
base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(
permissions::features::kShowRelatedWebsiteSetsPermissionGrants);
SetURL("https://firstparty.com");
constexpr char kToplevelURL[] = "https://firstparty.com";
constexpr char kEmbeddedURL[] = "https://embedded.com";
auto* map = HostContentSettingsMapFactory::GetForProfile(profile());
content_settings::ContentSettingConstraints constraint;
constraint.set_session_model(content_settings::mojom::SessionModel::DURABLE);
constraint.set_decided_by_related_website_sets(true);
map->SetContentSettingDefaultScope(GURL(kEmbeddedURL), GURL(kToplevelURL),
ContentSettingsType::STORAGE_ACCESS,
CONTENT_SETTING_BLOCK, constraint);
page_info()->PresentSitePermissionsForTesting();
expected_visible_permissions.insert(ContentSettingsType::STORAGE_ACCESS);
#if BUILDFLAG(IS_ANDROID)
// Geolocation is always allowed to pass through to Android-specific logic to
// check for DSE settings (so expect 1 item), but isn't actually shown later
// on because this test isn't testing with a default search engine origin.
expected_visible_permissions.insert(ContentSettingsType::GEOLOCATION);
#endif
ExpectPermissionInfoList(expected_visible_permissions,
last_permission_info_list());
}
TEST_F(PageInfoTest, HideAutograntedRWSPermissions) {
std::set<ContentSettingsType> expected_visible_permissions;
base::test::ScopedFeatureList feature_list;
feature_list.InitAndDisableFeature(
permissions::features::kShowRelatedWebsiteSetsPermissionGrants);
SetURL("https://firstparty.com");
constexpr char kToplevelURL[] = "https://firstparty.com";
constexpr char kEmbeddedURL[] = "https://embedded.com";
auto* map = HostContentSettingsMapFactory::GetForProfile(profile());
content_settings::ContentSettingConstraints constraint;
constraint.set_session_model(content_settings::mojom::SessionModel::DURABLE);
constraint.set_decided_by_related_website_sets(true);
map->SetContentSettingDefaultScope(GURL(kEmbeddedURL), GURL(kToplevelURL),
ContentSettingsType::STORAGE_ACCESS,
CONTENT_SETTING_ALLOW, constraint);
page_info()->PresentSitePermissionsForTesting();
#if BUILDFLAG(IS_ANDROID)
// Geolocation is always allowed to pass through to Android-specific logic to
// check for DSE settings (so expect 1 item), but isn't actually shown later
// on because this test isn't testing with a default search engine origin.
expected_visible_permissions.insert(ContentSettingsType::GEOLOCATION);
#endif
ExpectPermissionInfoList(expected_visible_permissions,
last_permission_info_list());
}
TEST_F(PageInfoTest, IncognitoPermissionsEmptyByDefault) {
incognito_page_info()->PresentSitePermissionsForTesting();
EXPECT_EQ(0u, last_permission_info_list().size());
}
TEST_F(PageInfoTest, IncognitoPermissionsDontShowAsk) {
page_info()->PresentSitePermissionsForTesting();
std::set<ContentSettingsType> expected_permissions;
std::set<ContentSettingsType> expected_incognito_permissions;
#if BUILDFLAG(IS_ANDROID)
// Geolocation is always allowed to pass through to Android-specific logic to
// check for DSE settings (so expect 1 item), but isn't actually shown later
// on because this test isn't testing with a default search engine origin.
expected_permissions.insert(ContentSettingsType::GEOLOCATION);
#endif
ExpectPermissionInfoList(expected_permissions, last_permission_info_list());
// Add some permissions to regular page info.
page_info()->OnSitePermissionChanged(ContentSettingsType::GEOLOCATION,
CONTENT_SETTING_ALLOW,
/*requesting_origin=*/std::nullopt,
/*is_one_time=*/false);
page_info()->OnSitePermissionChanged(ContentSettingsType::MEDIASTREAM_MIC,
CONTENT_SETTING_BLOCK,
/*requesting_origin=*/std::nullopt,
/*is_one_time=*/false);
expected_permissions.insert(ContentSettingsType::MEDIASTREAM_MIC);
expected_incognito_permissions.insert(ContentSettingsType::MEDIASTREAM_MIC);
// Both permissions should show in regular page info.
EXPECT_EQ(2u, last_permission_info_list().size());
// Only the block permissions should show in incognito mode as ALLOW
// permissions are inherited as ASK.
incognito_page_info()->PresentSitePermissionsForTesting();
ExpectPermissionInfoList(expected_incognito_permissions,
last_permission_info_list());
// Changing the permission to BLOCK should show it.
incognito_page_info()->OnSitePermissionChanged(
ContentSettingsType::GEOLOCATION, CONTENT_SETTING_BLOCK,
/*requesting_origin=*/std::nullopt,
/*is_one_time=*/false);
expected_incognito_permissions.insert(ContentSettingsType::GEOLOCATION);
ExpectPermissionInfoList(expected_incognito_permissions,
last_permission_info_list());
// Switching a permission back to default should not hide the permission.
incognito_page_info()->OnSitePermissionChanged(
ContentSettingsType::GEOLOCATION, CONTENT_SETTING_DEFAULT,
/*requesting_origin=*/std::nullopt,
/*is_one_time=*/false);
ExpectPermissionInfoList(expected_incognito_permissions,
last_permission_info_list());
}
TEST_F(PageInfoTest, OnPermissionsChanged) {
GURL kEmbedded("https://embedded.com");
// Setup site permissions.
HostContentSettingsMap* content_settings =
HostContentSettingsMapFactory::GetForProfile(profile());
ContentSetting setting = content_settings->GetContentSetting(
url(), url(), ContentSettingsType::POPUPS);
EXPECT_EQ(setting, CONTENT_SETTING_BLOCK);
setting = content_settings->GetContentSetting(
url(), url(), ContentSettingsType::GEOLOCATION);
EXPECT_EQ(setting, CONTENT_SETTING_ASK);
setting = content_settings->GetContentSetting(
url(), url(), ContentSettingsType::NOTIFICATIONS);
EXPECT_EQ(setting, CONTENT_SETTING_ASK);
setting = content_settings->GetContentSetting(
url(), url(), ContentSettingsType::MEDIASTREAM_MIC);
EXPECT_EQ(setting, CONTENT_SETTING_ASK);
setting = content_settings->GetContentSetting(
url(), url(), ContentSettingsType::MEDIASTREAM_CAMERA);
EXPECT_EQ(setting, CONTENT_SETTING_ASK);
setting = content_settings->GetContentSetting(
kEmbedded, url(), ContentSettingsType::STORAGE_ACCESS);
EXPECT_EQ(setting, CONTENT_SETTING_ASK);
#if !BUILDFLAG(IS_ANDROID)
setting = content_settings->GetContentSetting(
kEmbedded, url(), ContentSettingsType::FILE_SYSTEM_WRITE_GUARD);
EXPECT_EQ(setting, CONTENT_SETTING_ASK);
#endif
EXPECT_CALL(*mock_ui(), SetIdentityInfo(_));
ExpectInitialSetCookieInfoCall(mock_ui());
// SetPermissionInfo() is called once initially, and then again every time
// OnSitePermissionChanged() is called.
#if !BUILDFLAG(IS_ANDROID)
EXPECT_CALL(*mock_ui(), SetPermissionInfoStub()).Times(8);
#else
EXPECT_CALL(*mock_ui(), SetPermissionInfoStub()).Times(7);
#endif
// Execute code under tests.
page_info()->OnSitePermissionChanged(ContentSettingsType::POPUPS,
CONTENT_SETTING_ALLOW,
/*requesting_origin=*/std::nullopt,
/*is_one_time=*/false);
page_info()->OnSitePermissionChanged(ContentSettingsType::GEOLOCATION,
CONTENT_SETTING_ALLOW,
/*requesting_origin=*/std::nullopt,
/*is_one_time=*/false);
page_info()->OnSitePermissionChanged(ContentSettingsType::NOTIFICATIONS,
CONTENT_SETTING_ALLOW,
/*requesting_origin=*/std::nullopt,
/*is_one_time=*/false);
page_info()->OnSitePermissionChanged(ContentSettingsType::MEDIASTREAM_MIC,
CONTENT_SETTING_ALLOW,
/*requesting_origin=*/std::nullopt,
/*is_one_time=*/false);
page_info()->OnSitePermissionChanged(ContentSettingsType::MEDIASTREAM_CAMERA,
CONTENT_SETTING_ALLOW,
/*requesting_origin=*/std::nullopt,
/*is_one_time=*/false);
page_info()->OnSitePermissionChanged(ContentSettingsType::STORAGE_ACCESS,
CONTENT_SETTING_ALLOW,
url::Origin::Create(kEmbedded),
/*is_one_time=*/false);
#if !BUILDFLAG(IS_ANDROID)
page_info()->OnSitePermissionChanged(
ContentSettingsType::FILE_SYSTEM_WRITE_GUARD, CONTENT_SETTING_ALLOW,
url::Origin::Create(kEmbedded),
/*is_one_time=*/false);
#endif
// Verify that the site permissions were changed correctly.
setting = content_settings->GetContentSetting(url(), url(),
ContentSettingsType::POPUPS);
EXPECT_EQ(setting, CONTENT_SETTING_ALLOW);
setting = content_settings->GetContentSetting(
url(), url(), ContentSettingsType::GEOLOCATION);
EXPECT_EQ(setting, CONTENT_SETTING_ALLOW);
setting = content_settings->GetContentSetting(
url(), url(), ContentSettingsType::NOTIFICATIONS);
EXPECT_EQ(setting, CONTENT_SETTING_ALLOW);
setting = content_settings->GetContentSetting(
url(), url(), ContentSettingsType::MEDIASTREAM_MIC);
EXPECT_EQ(setting, CONTENT_SETTING_ALLOW);
setting = content_settings->GetContentSetting(
url(), url(), ContentSettingsType::MEDIASTREAM_CAMERA);
EXPECT_EQ(setting, CONTENT_SETTING_ALLOW);
setting = content_settings->GetContentSetting(
kEmbedded, url(), ContentSettingsType::STORAGE_ACCESS);
EXPECT_EQ(setting, CONTENT_SETTING_ALLOW);
#if !BUILDFLAG(IS_ANDROID)
setting = content_settings->GetContentSetting(
kEmbedded, url(), ContentSettingsType::FILE_SYSTEM_WRITE_GUARD);
EXPECT_EQ(setting, CONTENT_SETTING_ALLOW);
#endif
}
TEST_F(PageInfoTest, OnChosenObjectDeleted) {
// Connect the UsbChooserContext with FakeUsbDeviceManager.
device::FakeUsbDeviceManager usb_device_manager;
mojo::PendingRemote<device::mojom::UsbDeviceManager> device_manager;
usb_device_manager.AddReceiver(
device_manager.InitWithNewPipeAndPassReceiver());
UsbChooserContext* store = UsbChooserContextFactory::GetForProfile(profile());
store->SetDeviceManagerForTesting(std::move(device_manager));
auto device_info = usb_device_manager.CreateAndAddDevice(
0, 0, "Google", "Gizmo", "1234567890");
store->GrantDevicePermission(origin(), *device_info);
EXPECT_CALL(*mock_ui(), SetIdentityInfo(_));
ExpectInitialSetCookieInfoCall(mock_ui());
// Access PageInfo so that SetPermissionInfo is called once to populate
// |last_chosen_object_info_|. It will be called again by
// OnSiteChosenObjectDeleted.
EXPECT_CALL(*mock_ui(), SetPermissionInfoStub()).Times(2);
page_info();
ASSERT_EQ(1u, last_chosen_object_info().size());
const PageInfoUI::ChosenObjectInfo* info = last_chosen_object_info()[0].get();
page_info()->OnSiteChosenObjectDeleted(
*info->ui_info, base::Value(info->chooser_object->value.Clone()));
EXPECT_FALSE(store->HasDevicePermission(origin(), *device_info));
EXPECT_EQ(0u, last_chosen_object_info().size());
}
TEST_F(PageInfoTest, Malware) {
security_level_ = security_state::DANGEROUS;
visible_security_state_.malicious_content_status =
security_state::MALICIOUS_CONTENT_STATUS_MALWARE;
SetDefaultUIExpectations(mock_ui());
EXPECT_EQ(PageInfo::SITE_CONNECTION_STATUS_UNENCRYPTED,
page_info()->site_connection_status());
EXPECT_EQ(PageInfo::SAFE_BROWSING_STATUS_MALWARE,
page_info()->safe_browsing_status());
}
TEST_F(PageInfoTest, ManagedPolicyBlock) {
security_level_ = security_state::DANGEROUS;
visible_security_state_.malicious_content_status =
security_state::MALICIOUS_CONTENT_STATUS_MANAGED_POLICY_BLOCK;
SetDefaultUIExpectations(mock_ui());
EXPECT_EQ(PageInfo::SITE_CONNECTION_STATUS_UNENCRYPTED,
page_info()->site_connection_status());
EXPECT_EQ(PageInfo::SAFE_BROWSING_STATUS_MANAGED_POLICY_BLOCK,
page_info()->safe_browsing_status());
}
TEST_F(PageInfoTest, ManagedPolicyWarn) {
security_level_ = security_state::DANGEROUS;
visible_security_state_.malicious_content_status =
security_state::MALICIOUS_CONTENT_STATUS_MANAGED_POLICY_WARN;
SetDefaultUIExpectations(mock_ui());
EXPECT_EQ(PageInfo::SITE_CONNECTION_STATUS_UNENCRYPTED,
page_info()->site_connection_status());
EXPECT_EQ(PageInfo::SAFE_BROWSING_STATUS_MANAGED_POLICY_WARN,
page_info()->safe_browsing_status());
}
TEST_F(PageInfoTest, SocialEngineering) {
security_level_ = security_state::DANGEROUS;
visible_security_state_.malicious_content_status =
security_state::MALICIOUS_CONTENT_STATUS_SOCIAL_ENGINEERING;
SetDefaultUIExpectations(mock_ui());
EXPECT_EQ(PageInfo::SITE_CONNECTION_STATUS_UNENCRYPTED,
page_info()->site_connection_status());
EXPECT_EQ(PageInfo::SAFE_BROWSING_STATUS_SOCIAL_ENGINEERING,
page_info()->safe_browsing_status());
}
TEST_F(PageInfoTest, UnwantedSoftware) {
security_level_ = security_state::DANGEROUS;
visible_security_state_.malicious_content_status =
security_state::MALICIOUS_CONTENT_STATUS_UNWANTED_SOFTWARE;
SetDefaultUIExpectations(mock_ui());
EXPECT_EQ(PageInfo::SITE_CONNECTION_STATUS_UNENCRYPTED,
page_info()->site_connection_status());
EXPECT_EQ(PageInfo::SAFE_BROWSING_STATUS_UNWANTED_SOFTWARE,
page_info()->safe_browsing_status());
}
#if BUILDFLAG(FULL_SAFE_BROWSING)
TEST_F(PageInfoTest, SignInPasswordReuse) {
security_level_ = security_state::DANGEROUS;
visible_security_state_.malicious_content_status =
security_state::MALICIOUS_CONTENT_STATUS_SIGNED_IN_SYNC_PASSWORD_REUSE;
SetDefaultUIExpectations(mock_ui());
EXPECT_EQ(PageInfo::SITE_CONNECTION_STATUS_UNENCRYPTED,
page_info()->site_connection_status());
EXPECT_EQ(PageInfo::SAFE_BROWSING_STATUS_SIGNED_IN_SYNC_PASSWORD_REUSE,
page_info()->safe_browsing_status());
}
TEST_F(PageInfoTest, SavedPasswordReuse) {
security_level_ = security_state::DANGEROUS;
visible_security_state_.malicious_content_status =
security_state::MALICIOUS_CONTENT_STATUS_SAVED_PASSWORD_REUSE;
SetDefaultUIExpectations(mock_ui());
EXPECT_EQ(PageInfo::SITE_CONNECTION_STATUS_UNENCRYPTED,
page_info()->site_connection_status());
EXPECT_EQ(PageInfo::SAFE_BROWSING_STATUS_SAVED_PASSWORD_REUSE,
page_info()->safe_browsing_status());
}
TEST_F(PageInfoTest, EnterprisePasswordReuse) {
security_level_ = security_state::DANGEROUS;
visible_security_state_.malicious_content_status =
security_state::MALICIOUS_CONTENT_STATUS_ENTERPRISE_PASSWORD_REUSE;
SetDefaultUIExpectations(mock_ui());
EXPECT_EQ(PageInfo::SITE_CONNECTION_STATUS_UNENCRYPTED,
page_info()->site_connection_status());
EXPECT_EQ(PageInfo::SAFE_BROWSING_STATUS_ENTERPRISE_PASSWORD_REUSE,
page_info()->safe_browsing_status());
}
#endif
TEST_F(PageInfoTest, HTTPConnection) {
SetDefaultUIExpectations(mock_ui());
EXPECT_EQ(PageInfo::SITE_CONNECTION_STATUS_UNENCRYPTED,
page_info()->site_connection_status());
EXPECT_EQ(PageInfo::SITE_IDENTITY_STATUS_NO_CERT,
page_info()->site_identity_status());
}
TEST_F(PageInfoTest, HTTPSConnection) {
security_level_ = security_state::SECURE;
visible_security_state_.url = GURL("https://scheme-is-cryptographic.test");
visible_security_state_.certificate = cert();
visible_security_state_.cert_status = 0;
int status = 0;
status = SetSSLVersion(status, net::SSL_CONNECTION_VERSION_TLS1);
status = SetSSLCipherSuite(status, CR_TLS_RSA_WITH_AES_256_CBC_SHA256);
visible_security_state_.connection_status = status;
visible_security_state_.connection_info_initialized = true;
SetDefaultUIExpectations(mock_ui());
EXPECT_EQ(PageInfo::SITE_CONNECTION_STATUS_ENCRYPTED,
page_info()->site_connection_status());
EXPECT_EQ(PageInfo::SITE_IDENTITY_STATUS_CERT,
page_info()->site_identity_status());
}
// Define some dummy constants for Android-only resources.
#if !BUILDFLAG(IS_ANDROID)
#define IDR_PAGEINFO_BAD 0
#define IDR_PAGEINFO_GOOD 0
#endif
TEST_F(PageInfoTest, InsecureContent) {
struct TestCase {
security_state::SecurityLevel security_level;
net::CertStatus cert_status;
bool ran_mixed_content;
bool displayed_mixed_content;
bool contained_mixed_form;
bool ran_content_with_cert_errors;
bool displayed_content_with_cert_errors;
PageInfo::SiteConnectionStatus expected_site_connection_status;
PageInfo::SiteIdentityStatus expected_site_identity_status;
int expected_connection_icon_id;
};
const TestCase kTestCases[] = {
// Passive mixed content.
{security_state::NONE, 0, false /* ran_mixed_content */,
true /* displayed_mixed_content */, false,
false /* ran_content_with_cert_errors */,
false /* displayed_content_with_cert_errors */,
PageInfo::SITE_CONNECTION_STATUS_INSECURE_PASSIVE_SUBRESOURCE,
PageInfo::SITE_IDENTITY_STATUS_CERT, IDR_PAGEINFO_BAD},
// Passive mixed content with a nonsecure form. The nonsecure form is the
// more severe problem.
{security_state::NONE, 0, false /* ran_mixed_content */,
true /* displayed_mixed_content */, true,
false /* ran_content_with_cert_errors */,
false /* displayed_content_with_cert_errors */,
PageInfo::SITE_CONNECTION_STATUS_INSECURE_FORM_ACTION,
PageInfo::SITE_IDENTITY_STATUS_CERT, IDR_PAGEINFO_BAD},
// Only nonsecure form.
{security_state::NONE, 0, false /* ran_mixed_content */,
false /* displayed_mixed_content */, true,
false /* ran_content_with_cert_errors */,
false /* displayed_content_with_cert_errors */,
PageInfo::SITE_CONNECTION_STATUS_INSECURE_FORM_ACTION,
PageInfo::SITE_IDENTITY_STATUS_CERT, IDR_PAGEINFO_BAD},
// Passive mixed content with a cert error on the main resource.
{security_state::DANGEROUS, net::CERT_STATUS_DATE_INVALID,
false /* ran_mixed_content */, true /* displayed_mixed_content */, false,
false /* ran_content_with_cert_errors */,
false /* displayed_content_with_cert_errors */,
PageInfo::SITE_CONNECTION_STATUS_INSECURE_PASSIVE_SUBRESOURCE,
PageInfo::SITE_IDENTITY_STATUS_ERROR, IDR_PAGEINFO_BAD},
// Active and passive mixed content.
{security_state::DANGEROUS, 0, true /* ran_mixed_content */,
true /* displayed_mixed_content */, false,
false /* ran_content_with_cert_errors */,
false /* displayed_content_with_cert_errors */,
PageInfo::SITE_CONNECTION_STATUS_INSECURE_ACTIVE_SUBRESOURCE,
PageInfo::SITE_IDENTITY_STATUS_CERT, IDR_PAGEINFO_BAD},
// Active mixed content and nonsecure form.
{security_state::DANGEROUS, 0, true /* ran_mixed_content */,
true /* displayed_mixed_content */, true,
false /* ran_content_with_cert_errors */,
false /* displayed_content_with_cert_errors */,
PageInfo::SITE_CONNECTION_STATUS_INSECURE_ACTIVE_SUBRESOURCE,
PageInfo::SITE_IDENTITY_STATUS_CERT, IDR_PAGEINFO_BAD},
// Active and passive mixed content with a cert error on the main
// resource.
{security_state::DANGEROUS, net::CERT_STATUS_DATE_INVALID,
true /* ran_mixed_content */, true /* displayed_mixed_content */, false,
false /* ran_content_with_cert_errors */,
false /* displayed_content_with_cert_errors */,
PageInfo::SITE_CONNECTION_STATUS_INSECURE_ACTIVE_SUBRESOURCE,
PageInfo::SITE_IDENTITY_STATUS_ERROR, IDR_PAGEINFO_BAD},
// Active mixed content.
{security_state::DANGEROUS, 0, true /* ran_mixed_content */,
false /* displayed_mixed_content */, false,
false /* ran_content_with_cert_errors */,
false /* displayed_content_with_cert_errors */,
PageInfo::SITE_CONNECTION_STATUS_INSECURE_ACTIVE_SUBRESOURCE,
PageInfo::SITE_IDENTITY_STATUS_CERT, IDR_PAGEINFO_BAD},
// Active mixed content with a cert error on the main resource.
{security_state::DANGEROUS, net::CERT_STATUS_DATE_INVALID,
true /* ran_mixed_content */, false /* displayed_mixed_content */, false,
false /* ran_content_with_cert_errors */,
false /* displayed_content_with_cert_errors */,
PageInfo::SITE_CONNECTION_STATUS_INSECURE_ACTIVE_SUBRESOURCE,
PageInfo::SITE_IDENTITY_STATUS_ERROR, IDR_PAGEINFO_BAD},
// Passive subresources with cert errors.
{security_state::NONE, 0, false /* ran_mixed_content */,
false /* displayed_mixed_content */, false,
false /* ran_content_with_cert_errors */,
true /* displayed_content_with_cert_errors */,
PageInfo::SITE_CONNECTION_STATUS_INSECURE_PASSIVE_SUBRESOURCE,
PageInfo::SITE_IDENTITY_STATUS_CERT, IDR_PAGEINFO_BAD},
// Passive subresources with cert errors, with a cert error on the
// main resource also. In this case, the subresources with
// certificate errors are ignored: if the main resource had a cert
// error, it's not that useful to warn about subresources with cert
// errors as well.
{security_state::DANGEROUS, net::CERT_STATUS_DATE_INVALID,
false /* ran_mixed_content */, false /* displayed_mixed_content */,
false, false /* ran_content_with_cert_errors */,
true /* displayed_content_with_cert_errors */,
PageInfo::SITE_CONNECTION_STATUS_ENCRYPTED,
PageInfo::SITE_IDENTITY_STATUS_ERROR, IDR_PAGEINFO_GOOD},
// Passive and active subresources with cert errors.
{security_state::DANGEROUS, 0, false /* ran_mixed_content */,
false /* displayed_mixed_content */, false,
true /* ran_content_with_cert_errors */,
true /* displayed_content_with_cert_errors */,
PageInfo::SITE_CONNECTION_STATUS_INSECURE_ACTIVE_SUBRESOURCE,
PageInfo::SITE_IDENTITY_STATUS_CERT, IDR_PAGEINFO_BAD},
// Passive and active subresources with cert errors, with a cert
// error on the main resource also.
{security_state::DANGEROUS, net::CERT_STATUS_DATE_INVALID,
false /* ran_mixed_content */, false /* displayed_mixed_content */,
false, true /* ran_content_with_cert_errors */,
true /* displayed_content_with_cert_errors */,
PageInfo::SITE_CONNECTION_STATUS_ENCRYPTED,
PageInfo::SITE_IDENTITY_STATUS_ERROR, IDR_PAGEINFO_GOOD},
// Active subresources with cert errors.
{security_state::DANGEROUS, 0, false /* ran_content_with_cert_errors */,
false /* displayed_content_with_cert_errors */, false,
true /* ran_mixed_content */, false /* displayed_mixed_content */,
PageInfo::SITE_CONNECTION_STATUS_INSECURE_ACTIVE_SUBRESOURCE,
PageInfo::SITE_IDENTITY_STATUS_CERT, IDR_PAGEINFO_BAD},
// Active subresources with cert errors, with a cert error on the main
// resource also.
{security_state::DANGEROUS, net::CERT_STATUS_DATE_INVALID,
false /* ran_mixed_content */, false /* displayed_mixed_content */,
false, true /* ran_content_with_cert_errors */,
false /* displayed_content_with_cert_errors */,
PageInfo::SITE_CONNECTION_STATUS_ENCRYPTED,
PageInfo::SITE_IDENTITY_STATUS_ERROR, IDR_PAGEINFO_GOOD},
// Passive mixed content and subresources with cert errors.
{security_state::NONE, 0, false /* ran_content_with_cert_errors */,
true /* displayed_content_with_cert_errors */, false,
false /* ran_mixed_content */, true /* displayed_mixed_content */,
PageInfo::SITE_CONNECTION_STATUS_INSECURE_PASSIVE_SUBRESOURCE,
PageInfo::SITE_IDENTITY_STATUS_CERT, IDR_PAGEINFO_BAD},
// Passive mixed content, a nonsecure form, and subresources with cert
// errors.
{security_state::NONE, 0, false /* ran_mixed_content */,
true /* displayed_mixed_content */, true,
false /* ran_content_with_cert_errors */,
true /* displayed_content_with_cert_errors */,
PageInfo::SITE_CONNECTION_STATUS_INSECURE_FORM_ACTION,
PageInfo::SITE_IDENTITY_STATUS_CERT, IDR_PAGEINFO_BAD},
// Passive mixed content and active subresources with cert errors.
{security_state::DANGEROUS, 0, false /* ran_mixed_content */,
true /* displayed_mixed_content */, false,
true /* ran_content_with_cert_errors */,
false /* displayed_content_with_cert_errors */,
PageInfo::SITE_CONNECTION_STATUS_INSECURE_ACTIVE_SUBRESOURCE,
PageInfo::SITE_IDENTITY_STATUS_CERT, IDR_PAGEINFO_BAD},
// Active mixed content and passive subresources with cert errors.
{security_state::DANGEROUS, 0, true /* ran_mixed_content */,
false /* displayed_mixed_content */, false,
false /* ran_content_with_cert_errors */,
true /* displayed_content_with_cert_errors */,
PageInfo::SITE_CONNECTION_STATUS_INSECURE_ACTIVE_SUBRESOURCE,
PageInfo::SITE_IDENTITY_STATUS_CERT, IDR_PAGEINFO_BAD},
// Passive mixed content, active subresources with cert errors, and a cert
// error on the main resource.
{security_state::DANGEROUS, net::CERT_STATUS_DATE_INVALID,
false /* ran_mixed_content */, true /* displayed_mixed_content */, false,
true /* ran_content_with_cert_errors */,
false /* displayed_content_with_cert_errors */,
PageInfo::SITE_CONNECTION_STATUS_INSECURE_PASSIVE_SUBRESOURCE,
PageInfo::SITE_IDENTITY_STATUS_ERROR, IDR_PAGEINFO_BAD},
};
for (const auto& test : kTestCases) {
ClearPageInfo();
ResetMockUI();
security_level_ = test.security_level;
visible_security_state_.url = GURL("https://scheme-is-cryptographic.test");
visible_security_state_.certificate = cert();
visible_security_state_.cert_status = test.cert_status;
visible_security_state_.displayed_mixed_content =
test.displayed_mixed_content;
visible_security_state_.ran_mixed_content = test.ran_mixed_content;
visible_security_state_.contained_mixed_form = test.contained_mixed_form;
visible_security_state_.displayed_content_with_cert_errors =
test.displayed_content_with_cert_errors;
visible_security_state_.ran_content_with_cert_errors =
test.ran_content_with_cert_errors;
int status = 0;
status = SetSSLVersion(status, net::SSL_CONNECTION_VERSION_TLS1);
status = SetSSLCipherSuite(status, CR_TLS_RSA_WITH_AES_256_CBC_SHA256);
visible_security_state_.connection_status = status;
visible_security_state_.connection_info_initialized = true;
SetDefaultUIExpectations(mock_ui());
EXPECT_EQ(test.expected_site_connection_status,
page_info()->site_connection_status());
EXPECT_EQ(test.expected_site_identity_status,
page_info()->site_identity_status());
#if BUILDFLAG(IS_ANDROID)
EXPECT_EQ(
test.expected_connection_icon_id,
PageInfoUI::GetConnectionIconID(page_info()->site_connection_status()));
#endif
}
}
TEST_F(PageInfoTest, HTTPSEVCert) {
scoped_refptr<net::X509Certificate> ev_cert =
net::X509Certificate::CreateFromBytes(google_der);
ASSERT_TRUE(ev_cert);
security_level_ = security_state::NONE;
visible_security_state_.url = GURL("https://scheme-is-cryptographic.test");
visible_security_state_.certificate = ev_cert;
visible_security_state_.cert_status = net::CERT_STATUS_IS_EV;
visible_security_state_.displayed_mixed_content = true;
int status = 0;
status = SetSSLVersion(status, net::SSL_CONNECTION_VERSION_TLS1);
status = SetSSLCipherSuite(status, CR_TLS_RSA_WITH_AES_256_CBC_SHA256);
visible_security_state_.connection_status = status;
visible_security_state_.connection_info_initialized = true;
SetDefaultUIExpectations(mock_ui());
EXPECT_EQ(PageInfo::SITE_CONNECTION_STATUS_INSECURE_PASSIVE_SUBRESOURCE,
page_info()->site_connection_status());
EXPECT_EQ(PageInfo::SITE_IDENTITY_STATUS_EV_CERT,
page_info()->site_identity_status());
}
TEST_F(PageInfoTest, HTTPSConnectionError) {
security_level_ = security_state::SECURE;
visible_security_state_.url = GURL("https://scheme-is-cryptographic.test");
visible_security_state_.certificate = cert();
visible_security_state_.cert_status = 0;
int status = 0;
status = SetSSLVersion(status, net::SSL_CONNECTION_VERSION_TLS1);
status = SetSSLCipherSuite(status, CR_TLS_RSA_WITH_AES_256_CBC_SHA256);
visible_security_state_.connection_status = status;
// Simulate a failed connection.
visible_security_state_.connection_info_initialized = false;
SetDefaultUIExpectations(mock_ui());
EXPECT_EQ(PageInfo::SITE_CONNECTION_STATUS_ENCRYPTED_ERROR,
page_info()->site_connection_status());
EXPECT_EQ(PageInfo::SITE_IDENTITY_STATUS_CERT,
page_info()->site_identity_status());
}
TEST_F(PageInfoTest, HTTPSSHA1) {
SetCertToSHA1();
security_level_ = security_state::NONE;
visible_security_state_.url = GURL("https://scheme-is-cryptographic.test");
visible_security_state_.certificate = cert();
visible_security_state_.cert_status = net::CERT_STATUS_SHA1_SIGNATURE_PRESENT;
int status = 0;
status = SetSSLVersion(status, net::SSL_CONNECTION_VERSION_TLS1);
status = SetSSLCipherSuite(status, CR_TLS_RSA_WITH_AES_256_CBC_SHA256);
visible_security_state_.connection_status = status;
visible_security_state_.connection_info_initialized = true;
SetDefaultUIExpectations(mock_ui());
EXPECT_EQ(PageInfo::SITE_CONNECTION_STATUS_ENCRYPTED,
page_info()->site_connection_status());
EXPECT_EQ(PageInfo::SITE_IDENTITY_STATUS_DEPRECATED_SIGNATURE_ALGORITHM,
page_info()->site_identity_status());
#if BUILDFLAG(IS_ANDROID)
EXPECT_EQ(IDR_PAGEINFO_BAD,
PageInfoUI::GetIdentityIconID(page_info()->site_identity_status()));
#endif
}
#if !BUILDFLAG(IS_ANDROID)
TEST_F(PageInfoTest, NoInfoBar) {
SetDefaultUIExpectations(mock_ui());
EXPECT_EQ(0u, infobar_manager()->infobars().size());
bool unused;
page_info()->OnUIClosing(&unused);
EXPECT_EQ(0u, infobar_manager()->infobars().size());
}
TEST_F(PageInfoTest, ShowInfoBar) {
EXPECT_CALL(*mock_ui(), SetIdentityInfo(_));
ExpectInitialSetCookieInfoCall(mock_ui());
EXPECT_CALL(*mock_ui(), SetPermissionInfoStub()).Times(2);
EXPECT_EQ(0u, infobar_manager()->infobars().size());
page_info()->OnSitePermissionChanged(ContentSettingsType::GEOLOCATION,
CONTENT_SETTING_ALLOW,
/*requesting_origin=*/std::nullopt,
/*is_one_time=*/false);
bool unused;
page_info()->OnUIClosing(&unused);
ASSERT_EQ(1u, infobar_manager()->infobars().size());
infobar_manager()->RemoveInfoBar(infobar_manager()->infobars()[0]);
}
TEST_F(PageInfoTest, NoInfoBarWhenSoundSettingChanged) {
EXPECT_EQ(0u, infobar_manager()->infobars().size());
page_info()->OnSitePermissionChanged(
ContentSettingsType::SOUND, CONTENT_SETTING_BLOCK,
/*requesting_origin=*/std::nullopt, /*is_one_time=*/false);
bool unused;
page_info()->OnUIClosing(&unused);
EXPECT_EQ(0u, infobar_manager()->infobars().size());
}
TEST_F(PageInfoTest, ShowInfoBarWhenSoundSettingAndAnotherSettingChanged) {
EXPECT_EQ(0u, infobar_manager()->infobars().size());
page_info()->OnSitePermissionChanged(ContentSettingsType::JAVASCRIPT,
CONTENT_SETTING_BLOCK,
/*requesting_origin=*/std::nullopt,
/*is_one_time=*/false);
page_info()->OnSitePermissionChanged(
ContentSettingsType::SOUND, CONTENT_SETTING_BLOCK,
/*requesting_origin=*/std::nullopt, /*is_one_time=*/false);
bool unused;
page_info()->OnUIClosing(&unused);
EXPECT_EQ(1u, infobar_manager()->infobars().size());
infobar_manager()->RemoveInfoBar(infobar_manager()->infobars()[0]);
}
TEST_F(PageInfoTest, ShowInfobarWhenMediaChanged) {
base::HistogramTester histograms;
ASSERT_EQ(0u, infobar_manager()->infobars().size());
HostContentSettingsMap* map =
HostContentSettingsMapFactory::GetForProfile(profile());
map->SetContentSettingDefaultScope(url(), url(),
ContentSettingsType::MEDIASTREAM_MIC,
CONTENT_SETTING_BLOCK);
map->SetContentSettingDefaultScope(url(), url(),
ContentSettingsType::MEDIASTREAM_CAMERA,
CONTENT_SETTING_BLOCK);
page_info()->OnSitePermissionChanged(ContentSettingsType::MEDIASTREAM_CAMERA,
CONTENT_SETTING_ALLOW,
/*requesting_origin=*/std::nullopt,
/*is_one_time=*/false);
page_info()->OnSitePermissionChanged(ContentSettingsType::MEDIASTREAM_MIC,
CONTENT_SETTING_ALLOW,
/*requesting_origin=*/std::nullopt,
/*is_one_time=*/false);
page_info()->OnUIClosing(nullptr);
EXPECT_EQ(1u, infobar_manager()->infobars().size());
histograms.ExpectUniqueSample(
"Permissions.PageInfo.Changed.VideoCapture.ReloadInfobarShown",
permissions::PermissionChangeAction::REALLOWED, 1);
histograms.ExpectUniqueSample(
"Permissions.PageInfo.Changed.AudioCapture.ReloadInfobarShown",
permissions::PermissionChangeAction::REALLOWED, 1);
}
// Camera and Mic permissions should suppress the infobar when changed to BLOCK.
TEST_F(PageInfoTest, SuppressInfobarWhenMediaChangedToBlock) {
base::HistogramTester histograms;
ASSERT_EQ(0u, infobar_manager()->infobars().size());
// The infobar can be suppressed only if an origin subscribed to permission
// status change.
page_info()->SetSubscribedToPermissionChangeForTesting();
HostContentSettingsMap* map =
HostContentSettingsMapFactory::GetForProfile(profile());
map->SetContentSettingDefaultScope(url(), url(),
ContentSettingsType::MEDIASTREAM_MIC,
CONTENT_SETTING_BLOCK);
map->SetContentSettingDefaultScope(url(), url(),
ContentSettingsType::MEDIASTREAM_CAMERA,
CONTENT_SETTING_BLOCK);
page_info()->OnSitePermissionChanged(ContentSettingsType::MEDIASTREAM_CAMERA,
CONTENT_SETTING_ALLOW,
/*requesting_origin=*/std::nullopt,
/*is_one_time=*/false);
page_info()->OnSitePermissionChanged(ContentSettingsType::MEDIASTREAM_MIC,
CONTENT_SETTING_ALLOW,
/*requesting_origin=*/std::nullopt,
/*is_one_time=*/false);
page_info()->OnUIClosing(nullptr);
EXPECT_EQ(0u, infobar_manager()->infobars().size());
histograms.ExpectUniqueSample(
"Permissions.PageInfo.Changed.VideoCapture.ReloadInfobarNotShown",
permissions::PermissionChangeAction::REALLOWED, 1);
histograms.ExpectUniqueSample(
"Permissions.PageInfo.Changed.AudioCapture.ReloadInfobarNotShown",
permissions::PermissionChangeAction::REALLOWED, 1);
}
TEST_F(PageInfoTest, SuppressInfobarWhenMediaChangedToAllow) {
base::HistogramTester histograms;
ASSERT_EQ(0u, infobar_manager()->infobars().size());
// The infobar can be suppressed only if an origin subscribed to permission
// status change.
page_info()->SetSubscribedToPermissionChangeForTesting();
HostContentSettingsMap* map =
HostContentSettingsMapFactory::GetForProfile(profile());
map->SetContentSettingDefaultScope(url(), url(),
ContentSettingsType::MEDIASTREAM_CAMERA,
CONTENT_SETTING_BLOCK);
map->SetContentSettingDefaultScope(url(), url(),
ContentSettingsType::MEDIASTREAM_MIC,
CONTENT_SETTING_BLOCK);
page_info()->OnSitePermissionChanged(ContentSettingsType::MEDIASTREAM_CAMERA,
CONTENT_SETTING_ALLOW,
/*requesting_origin=*/std::nullopt,
/*is_one_time=*/false);
page_info()->OnSitePermissionChanged(ContentSettingsType::MEDIASTREAM_MIC,
CONTENT_SETTING_ALLOW,
/*requesting_origin=*/std::nullopt,
/*is_one_time=*/false);
page_info()->OnUIClosing(nullptr);
EXPECT_EQ(0u, infobar_manager()->infobars().size());
histograms.ExpectUniqueSample(
"Permissions.PageInfo.Changed.VideoCapture.ReloadInfobarNotShown",
permissions::PermissionChangeAction::REALLOWED, 1);
histograms.ExpectUniqueSample(
"Permissions.PageInfo.Changed.AudioCapture.ReloadInfobarNotShown",
permissions::PermissionChangeAction::REALLOWED, 1);
}
// Camera and Mic permissions should suppress the infobar when changed to
// DEFAULT.
TEST_F(PageInfoTest, SuppressInfobarWhenMediaChangedToDefault) {
base::HistogramTester histograms;
ASSERT_EQ(0u, infobar_manager()->infobars().size());
HostContentSettingsMap* map =
HostContentSettingsMapFactory::GetForProfile(profile());
map->SetContentSettingDefaultScope(url(), url(),
ContentSettingsType::MEDIASTREAM_CAMERA,
CONTENT_SETTING_BLOCK);
map->SetContentSettingDefaultScope(url(), url(),
ContentSettingsType::MEDIASTREAM_MIC,
CONTENT_SETTING_BLOCK);
// The infobar can be suppressed only if an origin subscribed to permission
// status change.
page_info()->SetSubscribedToPermissionChangeForTesting();
page_info()->OnSitePermissionChanged(ContentSettingsType::MEDIASTREAM_CAMERA,
CONTENT_SETTING_DEFAULT,
/*requesting_origin=*/std::nullopt,
/*is_one_time=*/false);
page_info()->OnSitePermissionChanged(ContentSettingsType::MEDIASTREAM_MIC,
CONTENT_SETTING_DEFAULT,
/*requesting_origin=*/std::nullopt,
/*is_one_time=*/false);
page_info()->OnUIClosing(nullptr);
EXPECT_EQ(0u, infobar_manager()->infobars().size());
histograms.ExpectUniqueSample(
"Permissions.PageInfo.Changed.VideoCapture.ReloadInfobarNotShown",
permissions::PermissionChangeAction::RESET_FROM_DENIED, 1);
histograms.ExpectUniqueSample(
"Permissions.PageInfo.Changed.AudioCapture.ReloadInfobarNotShown",
permissions::PermissionChangeAction::RESET_FROM_DENIED, 1);
}
// Only Camera and Mic permissions can suppress the infobar.
TEST_F(PageInfoTest, ShowInfobarWhenGeolocationChangedToAllow) {
base::HistogramTester histograms;
ASSERT_EQ(0u, infobar_manager()->infobars().size());
HostContentSettingsMap* map =
HostContentSettingsMapFactory::GetForProfile(profile());
map->SetContentSettingDefaultScope(
url(), url(), ContentSettingsType::GEOLOCATION, CONTENT_SETTING_BLOCK);
// The infobar can be suppressed only if an origin subscribed to permission
// status change.
page_info()->SetSubscribedToPermissionChangeForTesting();
page_info()->OnSitePermissionChanged(ContentSettingsType::GEOLOCATION,
CONTENT_SETTING_ALLOW,
/*requesting_origin=*/std::nullopt,
/*is_one_time=*/false);
page_info()->OnUIClosing(nullptr);
EXPECT_EQ(1u, infobar_manager()->infobars().size());
histograms.ExpectUniqueSample(
"Permissions.PageInfo.Changed.Geolocation.ReloadInfobarShown",
permissions::PermissionChangeAction::REALLOWED, 0);
}
// Geolocation permission change to BLOCK should show the infobar. Only
// Camera & Mic should suppress the infobar when changed.
TEST_F(PageInfoTest, NotSuppressedInfobarWhenGeolocationChangedToBlock) {
base::HistogramTester histograms;
ASSERT_EQ(0u, infobar_manager()->infobars().size());
// The infobar can be suppressed only if an origin subscribed to permission
// status change.
page_info()->SetSubscribedToPermissionChangeForTesting();
page_info()->OnSitePermissionChanged(ContentSettingsType::GEOLOCATION,
CONTENT_SETTING_BLOCK,
/*requesting_origin=*/std::nullopt,
/*is_one_time=*/false);
page_info()->OnUIClosing(nullptr);
EXPECT_EQ(1u, infobar_manager()->infobars().size());
histograms.ExpectUniqueSample(
"Permissions.PageInfo.Changed.Geolocation.ReloadInfobarShown",
permissions::PermissionChangeAction::REVOKED, 0);
}
TEST_F(PageInfoTest, ShowInfobarWhenGeolocationChangedToDefault) {
base::HistogramTester histograms;
ASSERT_EQ(0u, infobar_manager()->infobars().size());
// The infobar can be suppressed only if an origin subscribed to permission
// status change.
page_info()->SetSubscribedToPermissionChangeForTesting();
page_info()->OnSitePermissionChanged(ContentSettingsType::GEOLOCATION,
CONTENT_SETTING_DEFAULT,
/*requesting_origin=*/std::nullopt,
/*is_one_time=*/false);
page_info()->OnUIClosing(nullptr);
EXPECT_EQ(1u, infobar_manager()->infobars().size());
histograms.ExpectUniqueSample(
"Permissions.PageInfo.Changed.Geolocation.ReloadInfobarShown",
permissions::PermissionChangeAction::RESET_FROM_ALLOWED, 0);
}
// If at least one permission requires to show the infobar, we show it.
TEST_F(PageInfoTest, ShowInfobarWhenGeolocationAndMediaChangedToBlock) {
base::HistogramTester histograms;
ASSERT_EQ(0u, infobar_manager()->infobars().size());
HostContentSettingsMap* map =
HostContentSettingsMapFactory::GetForProfile(profile());
map->SetContentSettingDefaultScope(url(), url(),
ContentSettingsType::MEDIASTREAM_MIC,
CONTENT_SETTING_BLOCK);
map->SetContentSettingDefaultScope(url(), url(),
ContentSettingsType::MEDIASTREAM_CAMERA,
CONTENT_SETTING_BLOCK);
// The infobar can be suppressed only if an origin subscribed to permission
// status change.
page_info()->SetSubscribedToPermissionChangeForTesting();
page_info()->OnSitePermissionChanged(ContentSettingsType::GEOLOCATION,
CONTENT_SETTING_BLOCK,
/*requesting_origin=*/std::nullopt,
/*is_one_time=*/false);
page_info()->OnSitePermissionChanged(ContentSettingsType::MEDIASTREAM_CAMERA,
CONTENT_SETTING_ALLOW,
/*requesting_origin=*/std::nullopt,
/*is_one_time=*/false);
page_info()->OnSitePermissionChanged(ContentSettingsType::MEDIASTREAM_MIC,
CONTENT_SETTING_ALLOW,
/*requesting_origin=*/std::nullopt,
/*is_one_time=*/false);
page_info()->OnUIClosing(nullptr);
EXPECT_EQ(1u, infobar_manager()->infobars().size());
histograms.ExpectUniqueSample(
"Permissions.PageInfo.Changed.Geolocation.ReloadInfobarShown",
permissions::PermissionChangeAction::REVOKED, 0);
histograms.ExpectUniqueSample(
"Permissions.PageInfo.Changed.VideoCapture.ReloadInfobarNotShown",
permissions::PermissionChangeAction::REALLOWED, 1);
histograms.ExpectUniqueSample(
"Permissions.PageInfo.Changed.AudioCapture.ReloadInfobarNotShown",
permissions::PermissionChangeAction::REALLOWED, 1);
}
TEST_F(PageInfoTest, ShowInfoBarWhenAllowingThirdPartyCookies) {
SetDefaultUIExpectations(mock_ui());
NavigateAndCommit(url());
// Calls to `PresentSiteDataInternal` from `PresentSiteData` are synchronous
// which makes calls to `SetCookieInfo` appear as they're called.
// This call is needed to satisfy the default expectations after navigation.
page_info();
Mock::VerifyAndClearExpectations(mock_ui());
// `SetCookieInfo` is called once through `OnStatusChanged` and another time
// through `OnThirdPartyToggleClicked` which calls `OnStatusChanged` down
// its call chain.
EXPECT_CALL(*mock_ui(), SetCookieInfo(_)).Times(2);
page_info()->OnStatusChanged(CookieControlsState::kBlocked3pc,
CookieControlsEnforcement::kNoEnforcement,
CookieBlocking3pcdStatus::kNotIn3pcd,
base::Time());
EXPECT_EQ(0u, infobar_manager()->infobars().size());
page_info()->OnThirdPartyToggleClicked(/*block_third_party_cookies=*/false);
page_info()->OnUIClosing(nullptr);
ASSERT_EQ(1u, infobar_manager()->infobars().size());
infobar_manager()->RemoveInfoBar(infobar_manager()->infobars()[0]);
}
TEST_F(PageInfoTest, ShowInfoBarWhenBlockingThirdPartyCookies) {
SetDefaultUIExpectations(mock_ui());
NavigateAndCommit(url());
// As in `ShowInfoBarWhenAllowingThirdPartyCookies` above, expectations need
// to be cleared.
page_info();
Mock::VerifyAndClearExpectations(mock_ui());
EXPECT_CALL(*mock_ui(), SetCookieInfo(_)).Times(2);
page_info()->OnStatusChanged(CookieControlsState::kAllowed3pc,
CookieControlsEnforcement::kNoEnforcement,
CookieBlocking3pcdStatus::kNotIn3pcd,
base::Time());
EXPECT_EQ(0u, infobar_manager()->infobars().size());
page_info()->OnThirdPartyToggleClicked(/*block_third_party_cookies=*/true);
page_info()->OnUIClosing(nullptr);
ASSERT_EQ(1u, infobar_manager()->infobars().size());
infobar_manager()->RemoveInfoBar(infobar_manager()->infobars()[0]);
}
#endif
TEST_F(PageInfoTest, AboutBlankPage) {
SetURL(url::kAboutBlankURL);
SetDefaultUIExpectations(mock_ui());
EXPECT_EQ(PageInfo::SITE_CONNECTION_STATUS_UNENCRYPTED,
page_info()->site_connection_status());
EXPECT_EQ(PageInfo::SITE_IDENTITY_STATUS_NO_CERT,
page_info()->site_identity_status());
}
// On desktop, internal URLs aren't handled by PageInfo class. Instead, a
// custom and simpler bubble is shown, so no need to test.
#if BUILDFLAG(IS_ANDROID)
TEST_F(PageInfoTest, InternalPage) {
SetURL("chrome://bookmarks");
SetDefaultUIExpectations(mock_ui());
EXPECT_EQ(PageInfo::SITE_CONNECTION_STATUS_INTERNAL_PAGE,
page_info()->site_connection_status());
EXPECT_EQ(PageInfo::SITE_IDENTITY_STATUS_INTERNAL_PAGE,
page_info()->site_identity_status());
}
#endif
// Tests that "Re-Enable Warnings" button on PageInfo both removes certificate
// exceptions and logs metrics correctly.
TEST_F(PageInfoTest, ReEnableWarnings) {
struct TestCase {
const std::string url;
const bool button_visible;
const bool button_clicked;
};
const TestCase kTestCases[] = {
{"https://example.test", false, false},
{"https://example.test", true, false},
{"https://example.test", true, true},
};
const char kGenericHistogram[] =
"interstitial.ssl.did_user_revoke_decisions2";
auto* storage_partition =
web_contents()->GetPrimaryMainFrame()->GetStoragePartition();
for (const auto& test : kTestCases) {
base::HistogramTester histograms;
StatefulSSLHostStateDelegate* ssl_state =
StatefulSSLHostStateDelegateFactory::GetForProfile(profile());
const std::string host = GURL(test.url).host();
ssl_state->RevokeUserAllowExceptionsHard(host);
ResetMockUI();
SetURL(test.url);
if (test.button_visible) {
// In the case where the button should be visible, add an exception to
// the profile settings for the site (since the exception is what
// will make the button visible).
ssl_state->AllowCert(host, *cert(), net::ERR_CERT_DATE_INVALID,
storage_partition);
page_info();
if (test.button_clicked) {
page_info()->OnRevokeSSLErrorBypassButtonPressed();
EXPECT_FALSE(ssl_state->HasAllowException(host, storage_partition));
ClearPageInfo();
histograms.ExpectTotalCount(kGenericHistogram, 1);
histograms.ExpectBucketCount(
kGenericHistogram,
PageInfo::SSLCertificateDecisionsDidRevoke::
USER_CERT_DECISIONS_REVOKED,
1);
} else { // Case where button is visible but not clicked.
ClearPageInfo();
EXPECT_TRUE(ssl_state->HasAllowException(host, storage_partition));
histograms.ExpectTotalCount(kGenericHistogram, 1);
histograms.ExpectBucketCount(
kGenericHistogram,
PageInfo::SSLCertificateDecisionsDidRevoke::
USER_CERT_DECISIONS_NOT_REVOKED,
1);
}
} else {
page_info();
ClearPageInfo();
EXPECT_FALSE(ssl_state->HasAllowException(host, storage_partition));
// Button is not visible, so check histogram is empty after opening and
// closing page info.
histograms.ExpectTotalCount(kGenericHistogram, 0);
}
}
// Test class expects PageInfo to exist during Teardown.
page_info();
}
// Tests that the duration of time the PageInfo is open is recorded for pages
// with various security levels.
TEST_F(PageInfoTest, TimeOpenMetrics) {
struct TestCase {
const std::string url;
const security_state::SecurityLevel security_level;
const std::string security_level_name;
const page_info::PageInfoAction action;
};
const std::string kHistogramPrefix("Security.PageInfo.TimeOpen.");
const TestCase kTestCases[] = {
// PAGE_INFO_OPENED used as shorthand for "take no action".
{"https://example.test", security_state::SECURE, "SECURE",
page_info::PAGE_INFO_OPENED},
{"http://example.test", security_state::NONE, "NONE",
page_info::PAGE_INFO_OPENED},
{"https://example.test", security_state::SECURE, "SECURE",
page_info::PAGE_INFO_SITE_SETTINGS_OPENED},
{"http://example.test", security_state::NONE, "NONE",
page_info::PAGE_INFO_SITE_SETTINGS_OPENED},
};
for (const auto& test : kTestCases) {
base::HistogramTester histograms;
SetURL(test.url);
security_level_ = test.security_level;
ResetMockUI();
ClearPageInfo();
SetDefaultUIExpectations(mock_ui());
histograms.ExpectTotalCount(kHistogramPrefix + test.security_level_name, 0);
histograms.ExpectTotalCount(
kHistogramPrefix + "Action." + test.security_level_name, 0);
histograms.ExpectTotalCount(
kHistogramPrefix + "NoAction." + test.security_level_name, 0);
PageInfo* test_page_info = page_info();
if (test.action != page_info::PAGE_INFO_OPENED) {
test_page_info->RecordPageInfoAction(test.action);
}
ClearPageInfo();
histograms.ExpectTotalCount(kHistogramPrefix + test.security_level_name, 1);
if (test.action != page_info::PAGE_INFO_OPENED) {
histograms.ExpectTotalCount(
kHistogramPrefix + "Action." + test.security_level_name, 1);
} else {
histograms.ExpectTotalCount(
kHistogramPrefix + "NoAction." + test.security_level_name, 1);
}
}
// PageInfoTest expects a valid PageInfo instance to exist at end of test.
ResetMockUI();
SetDefaultUIExpectations(mock_ui());
page_info();
}
TEST_F(PageInfoTest, AdPersonalization) {
constexpr int kTaxonomyVersion = 1;
privacy_sandbox::CanonicalTopic kFirstTopic(
browsing_topics::Topic(24), // "Blues"
kTaxonomyVersion);
privacy_sandbox::CanonicalTopic kSecondTopic(
browsing_topics::Topic(23), // "Music & audio"
kTaxonomyVersion);
std::vector<privacy_sandbox::CanonicalTopic> accessed_topics = {kFirstTopic,
kSecondTopic};
EXPECT_CALL(*mock_ui(),
SetAdPersonalizationInfo(::testing::Field(
&PageInfoUI::AdPersonalizationInfo::accessed_topics,
accessed_topics)));
content_settings::PageSpecificContentSettings* pscs =
content_settings::PageSpecificContentSettings::GetForFrame(
web_contents()->GetPrimaryMainFrame());
EXPECT_FALSE(pscs->HasAccessedTopics());
EXPECT_THAT(pscs->GetAccessedTopics(), testing::IsEmpty());
pscs->OnTopicAccessed(url::Origin::Create(GURL("https://foo.com")), false,
kSecondTopic);
pscs->OnTopicAccessed(url::Origin::Create(GURL("https://foo.com")), false,
kFirstTopic);
page_info();
}
// Tests that metrics are recorded on a PageInfo for pages with
// various Safety Tip statuses.
// See https://crbug.com/1114659 for why the test is disabled on Android.
#if BUILDFLAG(IS_ANDROID)
#define MAYBE_SafetyTipMetrics DISABLED_SafetyTipMetrics
#else
#define MAYBE_SafetyTipMetrics SafetyTipMetrics
#endif
TEST_F(PageInfoTest, MAYBE_SafetyTipMetrics) {
struct TestCase {
const security_state::SafetyTipInfo safety_tip_info;
};
const char kGenericHistogram[] = "WebsiteSettings.Action";
const TestCase kTestCases[] = {
{{security_state::SafetyTipStatus::kNone, GURL()}},
{{security_state::SafetyTipStatus::kLookalike, GURL()}},
};
for (const auto& test : kTestCases) {
base::HistogramTester histograms;
SetURL("https://example.test");
visible_security_state_.safety_tip_info = test.safety_tip_info;
ClearPageInfo();
ResetMockUI();
SetDefaultUIExpectations(mock_ui());
histograms.ExpectTotalCount(kGenericHistogram, 0);
page_info()->RecordPageInfoAction(page_info::PAGE_INFO_OPENED);
// RecordPageInfoAction() is called during PageInfo
// creation in addition to the explicit RecordPageInfoAction()
// call, so it is called twice in total.
histograms.ExpectTotalCount(kGenericHistogram, 2);
histograms.ExpectBucketCount(kGenericHistogram, page_info::PAGE_INFO_OPENED,
2);
}
}
// Tests that the duration of time the PageInfo is open is recorded for pages
// with various Safety Tip statuses.
TEST_F(PageInfoTest, SafetyTipTimeOpenMetrics) {
struct TestCase {
const security_state::SafetyTipStatus safety_tip_status;
const std::string safety_tip_status_name;
const page_info::PageInfoAction action;
};
const std::string kHistogramPrefix("Security.PageInfo.TimeOpen.");
const TestCase kTestCases[] = {
// PAGE_INFO_COUNT used as shorthand for "take no action".
{security_state::SafetyTipStatus::kNone, "SafetyTip_None",
page_info::PAGE_INFO_OPENED},
{security_state::SafetyTipStatus::kLookalike, "SafetyTip_Lookalike",
page_info::PAGE_INFO_OPENED},
{security_state::SafetyTipStatus::kNone, "SafetyTip_None",
page_info::PAGE_INFO_SITE_SETTINGS_OPENED},
{security_state::SafetyTipStatus::kLookalike, "SafetyTip_Lookalike",
page_info::PAGE_INFO_SITE_SETTINGS_OPENED},
};
for (const auto& test : kTestCases) {
base::HistogramTester histograms;
SetURL("https://example.test");
visible_security_state_.safety_tip_info.status = test.safety_tip_status;
ResetMockUI();
ClearPageInfo();
SetDefaultUIExpectations(mock_ui());
histograms.ExpectTotalCount(kHistogramPrefix + test.safety_tip_status_name,
0);
histograms.ExpectTotalCount(
kHistogramPrefix + "Action." + test.safety_tip_status_name, 0);
histograms.ExpectTotalCount(
kHistogramPrefix + "NoAction." + test.safety_tip_status_name, 0);
PageInfo* test_page_info = page_info();
if (test.action != page_info::PAGE_INFO_OPENED) {
test_page_info->RecordPageInfoAction(test.action);
}
ClearPageInfo();
histograms.ExpectTotalCount(kHistogramPrefix + test.safety_tip_status_name,
1);
if (test.action != page_info::PAGE_INFO_OPENED) {
histograms.ExpectTotalCount(
kHistogramPrefix + "Action." + test.safety_tip_status_name, 1);
} else {
histograms.ExpectTotalCount(
kHistogramPrefix + "NoAction." + test.safety_tip_status_name, 1);
}
}
// PageInfoTest expects a valid PageInfo instance to exist at end of test.
ResetMockUI();
SetDefaultUIExpectations(mock_ui());
page_info();
}
// Tests that the SubresourceFilter setting is omitted correctly.
TEST_F(PageInfoTest, SubresourceFilterSetting_MatchesActivation) {
auto showing_setting = [](const PermissionInfoList& permissions) {
return base::Contains(
permissions, ContentSettingsType::ADS,
[](const auto& permission) { return permission.type; });
};
// By default, the setting should not appear at all.
SetURL("https://example.test/");
SetDefaultUIExpectations(mock_ui());
page_info();
EXPECT_FALSE(showing_setting(last_permission_info_list()));
// Reset state.
ClearPageInfo();
ResetMockUI();
SetDefaultUIExpectations(mock_ui());
// Now, explicitly set site activation metadata to simulate activation on
// that origin, which is encoded by the existence of the website setting. The
// setting should then appear in page_info.
subresource_filter::SubresourceFilterContentSettingsManager*
settings_manager =
SubresourceFilterProfileContextFactory::GetForProfile(profile())
->settings_manager();
settings_manager->SetSiteMetadataBasedOnActivation(
url(), true,
subresource_filter::SubresourceFilterContentSettingsManager::
ActivationSource::kSafeBrowsing);
page_info();
EXPECT_TRUE(showing_setting(last_permission_info_list()));
}
// Tests that permissions substring is empty if permission is blocked.
TEST_F(PageInfoTest, PermissionBlockedStrings) {
SetURL("https://example.com/");
page_info();
auto web_contents =
content::WebContentsTester::CreateTestWebContents(profile(), nullptr);
ChromePageInfoUiDelegate delegate(web_contents.get(),
GURL("http://www.example.com"));
PageInfo::PermissionInfo camera_permission;
camera_permission.type = ContentSettingsType::MEDIASTREAM_CAMERA;
camera_permission.setting = CONTENT_SETTING_BLOCK;
camera_permission.default_setting = CONTENT_SETTING_ASK;
camera_permission.source = SettingSource::kUser;
camera_permission.is_one_time = false;
EXPECT_EQ(std::u16string(), PageInfoUI::PermissionMainPageStateToUIString(
&delegate, camera_permission));
}
// Tests that permissions substring says "Using now" if permission is in use.
TEST_F(PageInfoTest, PermissionUsingNowStrings) {
SetURL("https://example.com/");
page_info();
auto web_contents =
content::WebContentsTester::CreateTestWebContents(profile(), nullptr);
ChromePageInfoUiDelegate delegate(web_contents.get(),
GURL("http://www.example.com"));
PageInfo::PermissionInfo camera_permission;
camera_permission.type = ContentSettingsType::MEDIASTREAM_CAMERA;
camera_permission.setting = CONTENT_SETTING_ALLOW;
camera_permission.default_setting = CONTENT_SETTING_ASK;
camera_permission.source = SettingSource::kUser;
camera_permission.is_one_time = false;
camera_permission.is_in_use = true;
EXPECT_EQ(l10n_util::GetStringUTF16(IDS_PAGE_INFO_PERMISSION_USING_NOW),
PageInfoUI::PermissionMainPageStateToUIString(&delegate,
camera_permission));
}
// Tests that permissions substring says "Recently used" if permission was used
// less than 1 minute ago.
TEST_F(PageInfoTest, PermissionRecentlyUsedStrings) {
SetURL("https://example.com/");
page_info();
auto web_contents =
content::WebContentsTester::CreateTestWebContents(profile(), nullptr);
ChromePageInfoUiDelegate delegate(web_contents.get(),
GURL("http://www.example.com"));
PageInfo::PermissionInfo camera_permission;
camera_permission.type = ContentSettingsType::MEDIASTREAM_CAMERA;
camera_permission.setting = CONTENT_SETTING_ALLOW;
camera_permission.default_setting = CONTENT_SETTING_ASK;
camera_permission.source = SettingSource::kUser;
camera_permission.is_one_time = false;
camera_permission.is_in_use = false;
camera_permission.last_used = base::Time::Now() - base::Seconds(30);
EXPECT_EQ(l10n_util::GetStringUTF16(IDS_PAGE_INFO_PERMISSION_RECENTLY_USED),
PageInfoUI::PermissionMainPageStateToUIString(&delegate,
camera_permission));
}
// Tests that permissions substring says "Used X minutes/hours ago" if
// permission was used more than 1 minute ago.
TEST_F(PageInfoTest, PermissionUsed30MinutesAgoStrings) {
SetURL("https://example.com/");
page_info();
auto web_contents =
content::WebContentsTester::CreateTestWebContents(profile(), nullptr);
ChromePageInfoUiDelegate delegate(web_contents.get(),
GURL("http://www.example.com"));
PageInfo::PermissionInfo camera_permission;
camera_permission.type = ContentSettingsType::MEDIASTREAM_CAMERA;
camera_permission.setting = CONTENT_SETTING_ALLOW;
camera_permission.default_setting = CONTENT_SETTING_ASK;
camera_permission.source = SettingSource::kUser;
camera_permission.is_one_time = false;
camera_permission.is_in_use = false;
camera_permission.last_used = base::Time::Now() - base::Minutes(30);
EXPECT_EQ(l10n_util::GetStringFUTF16(IDS_PAGE_INFO_PERMISSION_USED_TIME_AGO,
u"30 minutes"),
PageInfoUI::PermissionMainPageStateToUIString(&delegate,
camera_permission));
}
#if !BUILDFLAG(IS_ANDROID)
// Unit tests with the unified autoplay sound settings UI enabled. When enabled
// the sound settings dropdown on the page info UI will have custom wording.
class UnifiedAutoplaySoundSettingsPageInfoTest
: public ChromeRenderViewHostTestHarness {
public:
~UnifiedAutoplaySoundSettingsPageInfoTest() override = default;
void SetUp() override {
scoped_feature_list_.InitWithFeatures({media::kAutoplayDisableSettings},
{});
ChromeRenderViewHostTestHarness::SetUp();
}
void SetAutoplayPrefValue(bool value) {
profile()->GetPrefs()->SetBoolean(prefs::kBlockAutoplayEnabled, value);
}
void SetDefaultSoundContentSetting(ContentSetting default_setting) {
default_setting_ = default_setting;
}
std::u16string GetDefaultSoundSettingString() {
auto web_contents =
content::WebContentsTester::CreateTestWebContents(profile(), nullptr);
ChromePageInfoUiDelegate delegate(web_contents.get(),
GURL("http://www.example.com"));
return PageInfoUI::PermissionActionToUIString(
&delegate, ContentSettingsType::SOUND, CONTENT_SETTING_DEFAULT,
default_setting_, SettingSource::kUser,
/*is_one_time=*/false);
}
std::u16string GetSoundSettingString(ContentSetting setting) {
auto web_contents =
content::WebContentsTester::CreateTestWebContents(profile(), nullptr);
ChromePageInfoUiDelegate delegate(web_contents.get(),
GURL("http://www.example.com"));
return PageInfoUI::PermissionActionToUIString(
&delegate, ContentSettingsType::SOUND, setting, default_setting_,
SettingSource::kUser,
/*is_one_time=*/false);
}
private:
ContentSetting default_setting_ = CONTENT_SETTING_DEFAULT;
base::test::ScopedFeatureList scoped_feature_list_;
};
// This test checks that the strings for the sound settings dropdown when
// block autoplay is enabled and the default sound setting is allow.
// The three options should be Automatic (default), Allow and Mute.
TEST_F(UnifiedAutoplaySoundSettingsPageInfoTest, DefaultAllow_PrefOn) {
SetDefaultSoundContentSetting(CONTENT_SETTING_ALLOW);
SetAutoplayPrefValue(true);
EXPECT_EQ(
l10n_util::GetStringUTF16(IDS_PAGE_INFO_BUTTON_TEXT_AUTOMATIC_BY_DEFAULT),
GetDefaultSoundSettingString());
EXPECT_EQ(
l10n_util::GetStringUTF16(IDS_PAGE_INFO_BUTTON_TEXT_ALLOWED_BY_USER),
GetSoundSettingString(CONTENT_SETTING_ALLOW));
EXPECT_EQ(l10n_util::GetStringUTF16(IDS_PAGE_INFO_BUTTON_TEXT_MUTED_BY_USER),
GetSoundSettingString(CONTENT_SETTING_BLOCK));
}
// This test checks that the strings for the sound settings dropdown when
// block autoplay is disabled and the default sound setting is allow.
// The three options should be Allow (default), Allow and Mute.
TEST_F(UnifiedAutoplaySoundSettingsPageInfoTest, DefaultAllow_PrefOff) {
SetDefaultSoundContentSetting(CONTENT_SETTING_ALLOW);
SetAutoplayPrefValue(false);
EXPECT_EQ(
l10n_util::GetStringUTF16(IDS_PAGE_INFO_BUTTON_TEXT_ALLOWED_BY_DEFAULT),
GetDefaultSoundSettingString());
EXPECT_EQ(
l10n_util::GetStringUTF16(IDS_PAGE_INFO_BUTTON_TEXT_ALLOWED_BY_USER),
GetSoundSettingString(CONTENT_SETTING_ALLOW));
EXPECT_EQ(l10n_util::GetStringUTF16(IDS_PAGE_INFO_BUTTON_TEXT_MUTED_BY_USER),
GetSoundSettingString(CONTENT_SETTING_BLOCK));
}
// This test checks the strings for the sound settings dropdown when
// the default sound setting is block. The three options should be
// Block (default), Allow and Mute.
TEST_F(UnifiedAutoplaySoundSettingsPageInfoTest, DefaultBlock_PrefOn) {
SetDefaultSoundContentSetting(CONTENT_SETTING_BLOCK);
SetAutoplayPrefValue(true);
EXPECT_EQ(
l10n_util::GetStringUTF16(IDS_PAGE_INFO_BUTTON_TEXT_MUTED_BY_DEFAULT),
GetDefaultSoundSettingString());
EXPECT_EQ(
l10n_util::GetStringUTF16(IDS_PAGE_INFO_BUTTON_TEXT_ALLOWED_BY_USER),
GetSoundSettingString(CONTENT_SETTING_ALLOW));
EXPECT_EQ(l10n_util::GetStringUTF16(IDS_PAGE_INFO_BUTTON_TEXT_MUTED_BY_USER),
GetSoundSettingString(CONTENT_SETTING_BLOCK));
}
// This test checks the strings for the sound settings dropdown when
// the default sound setting is block. The three options should be
// Block (default), Allow and Mute.
TEST_F(UnifiedAutoplaySoundSettingsPageInfoTest, DefaultBlock_PrefOff) {
SetDefaultSoundContentSetting(CONTENT_SETTING_BLOCK);
SetAutoplayPrefValue(false);
EXPECT_EQ(
l10n_util::GetStringUTF16(IDS_PAGE_INFO_BUTTON_TEXT_MUTED_BY_DEFAULT),
GetDefaultSoundSettingString());
EXPECT_EQ(
l10n_util::GetStringUTF16(IDS_PAGE_INFO_BUTTON_TEXT_ALLOWED_BY_USER),
GetSoundSettingString(CONTENT_SETTING_ALLOW));
EXPECT_EQ(l10n_util::GetStringUTF16(IDS_PAGE_INFO_BUTTON_TEXT_MUTED_BY_USER),
GetSoundSettingString(CONTENT_SETTING_BLOCK));
}
// This test checks that the string for a permission dropdown that is not the
// sound setting is unaffected.
TEST_F(UnifiedAutoplaySoundSettingsPageInfoTest, NotSoundSetting_Noop) {
auto web_contents =
content::WebContentsTester::CreateTestWebContents(profile(), nullptr);
ChromePageInfoUiDelegate delegate(web_contents.get(),
GURL("http://www.example.com"));
EXPECT_EQ(
l10n_util::GetStringUTF16(IDS_PAGE_INFO_BUTTON_TEXT_ALLOWED_BY_DEFAULT),
PageInfoUI::PermissionActionToUIString(
&delegate, ContentSettingsType::ADS, CONTENT_SETTING_DEFAULT,
CONTENT_SETTING_ALLOW, SettingSource::kUser,
/*is_one_time=*/false));
}
#endif // !BUILDFLAG(IS_ANDROID)
// Unit tests for logic in the PageInfoUI that toggles permission between
// allow/block and remember/forget.
class PageInfoToggleStatesUnitTest : public ::testing::Test {
};
// Testing all possible state transitions for a permission that doesn't
// support allow once.
TEST_F(PageInfoToggleStatesUnitTest,
TogglePermissionWithoutAllowOnceDefaultAskTest) {
PageInfo::PermissionInfo camera_permission;
camera_permission.type = ContentSettingsType::MEDIASTREAM_CAMERA;
camera_permission.setting = CONTENT_SETTING_ALLOW;
camera_permission.default_setting = CONTENT_SETTING_ASK;
camera_permission.source = SettingSource::kUser;
camera_permission.is_one_time = false;
// Allow -> Block
PageInfoUI::ToggleBetweenAllowAndBlock(camera_permission);
EXPECT_EQ(camera_permission.setting, CONTENT_SETTING_BLOCK);
// Block -> Allow
PageInfoUI::ToggleBetweenAllowAndBlock(camera_permission);
EXPECT_EQ(camera_permission.setting, CONTENT_SETTING_ALLOW);
}
TEST_F(PageInfoToggleStatesUnitTest,
TogglePermissionWithoutAllowOnceDefaultBlockTest) {
PageInfo::PermissionInfo camera_permission;
camera_permission.type = ContentSettingsType::MEDIASTREAM_CAMERA;
camera_permission.setting = CONTENT_SETTING_ALLOW;
camera_permission.default_setting = CONTENT_SETTING_BLOCK;
camera_permission.source = SettingSource::kUser;
camera_permission.is_one_time = false;
// Allow -> Block
PageInfoUI::ToggleBetweenAllowAndBlock(camera_permission);
EXPECT_EQ(camera_permission.setting, CONTENT_SETTING_BLOCK);
// Block -> Allow
PageInfoUI::ToggleBetweenAllowAndBlock(camera_permission);
EXPECT_EQ(camera_permission.setting, CONTENT_SETTING_ALLOW);
// Allow -> Block
PageInfoUI::ToggleBetweenAllowAndBlock(camera_permission);
EXPECT_EQ(camera_permission.setting, CONTENT_SETTING_BLOCK);
// Block (default) -> Allow
camera_permission.setting = CONTENT_SETTING_DEFAULT;
PageInfoUI::ToggleBetweenAllowAndBlock(camera_permission);
EXPECT_EQ(camera_permission.setting, CONTENT_SETTING_ALLOW);
}
// Testing all possible state transitions for a permission that supports
// allow once and default setting ask.
TEST_F(PageInfoToggleStatesUnitTest,
TogglePermissionWithAllowOnceDefaultAskTest) {
PageInfo::PermissionInfo location_permission;
location_permission.type = ContentSettingsType::GEOLOCATION;
location_permission.setting = CONTENT_SETTING_ALLOW;
location_permission.default_setting = CONTENT_SETTING_ASK;
location_permission.source = SettingSource::kUser;
location_permission.is_one_time = false;
// Allow -> Block
PageInfoUI::ToggleBetweenAllowAndBlock(location_permission);
EXPECT_EQ(location_permission.setting, CONTENT_SETTING_BLOCK);
// Block -> Allow
PageInfoUI::ToggleBetweenAllowAndBlock(location_permission);
EXPECT_EQ(location_permission.setting, CONTENT_SETTING_ALLOW);
// Allow -> Allow once
PageInfoUI::ToggleBetweenRememberAndForget(location_permission);
EXPECT_EQ(location_permission.setting, CONTENT_SETTING_ALLOW);
EXPECT_EQ(location_permission.is_one_time, true);
// Allow once -> Allow
PageInfoUI::ToggleBetweenRememberAndForget(location_permission);
EXPECT_EQ(location_permission.setting, CONTENT_SETTING_ALLOW);
EXPECT_EQ(location_permission.is_one_time, false);
}
// Testing all possible state transitions for a permission that supports
// allow once and default setting block.
TEST_F(PageInfoToggleStatesUnitTest,
TogglePermissionWithAllowOnceDefaultBlockTest) {
PageInfo::PermissionInfo location_permission;
location_permission.type = ContentSettingsType::GEOLOCATION;
location_permission.setting = CONTENT_SETTING_DEFAULT;
location_permission.default_setting = CONTENT_SETTING_BLOCK;
location_permission.source = SettingSource::kUser;
location_permission.is_one_time = false;
// Block (default) -> Allow once
PageInfoUI::ToggleBetweenAllowAndBlock(location_permission);
EXPECT_EQ(location_permission.setting, CONTENT_SETTING_ALLOW);
EXPECT_EQ(location_permission.is_one_time, true);
// Allow once -> Allow
PageInfoUI::ToggleBetweenRememberAndForget(location_permission);
EXPECT_EQ(location_permission.setting, CONTENT_SETTING_ALLOW);
EXPECT_EQ(location_permission.is_one_time, false);
// Allow -> Block
PageInfoUI::ToggleBetweenAllowAndBlock(location_permission);
EXPECT_EQ(location_permission.setting, CONTENT_SETTING_BLOCK);
// Block -> Allow
PageInfoUI::ToggleBetweenAllowAndBlock(location_permission);
EXPECT_EQ(location_permission.setting, CONTENT_SETTING_ALLOW);
EXPECT_EQ(location_permission.is_one_time, false);
// Allow -> Allow once
PageInfoUI::ToggleBetweenRememberAndForget(location_permission);
EXPECT_EQ(location_permission.setting, CONTENT_SETTING_ALLOW);
EXPECT_EQ(location_permission.is_one_time, true);
// Allow once -> Block
PageInfoUI::ToggleBetweenAllowAndBlock(location_permission);
EXPECT_EQ(location_permission.setting, CONTENT_SETTING_BLOCK);
EXPECT_EQ(location_permission.is_one_time, false);
}
// Testing all possible state transitions for a content settings with a default
// setting allow.
TEST_F(PageInfoToggleStatesUnitTest, TogglePermissionDefaultAllowTest) {
PageInfo::PermissionInfo images_permission;
images_permission.type = ContentSettingsType::IMAGES;
images_permission.setting = CONTENT_SETTING_DEFAULT;
images_permission.default_setting = CONTENT_SETTING_ALLOW;
images_permission.source = SettingSource::kUser;
images_permission.is_one_time = false;
// Allow (default) -> Block
PageInfoUI::ToggleBetweenAllowAndBlock(images_permission);
EXPECT_EQ(images_permission.setting, CONTENT_SETTING_BLOCK);
// Block -> Allow
PageInfoUI::ToggleBetweenAllowAndBlock(images_permission);
EXPECT_EQ(images_permission.setting, CONTENT_SETTING_ALLOW);
// Allow -> Block
PageInfoUI::ToggleBetweenAllowAndBlock(images_permission);
EXPECT_EQ(images_permission.setting, CONTENT_SETTING_BLOCK);
}
// Testing all possible state transitions for a content settings with a default
// setting block.
TEST_F(PageInfoToggleStatesUnitTest, TogglePermissionDefaultBlockTest) {
PageInfo::PermissionInfo popups_permission;
popups_permission.type = ContentSettingsType::POPUPS;
popups_permission.setting = CONTENT_SETTING_DEFAULT;
popups_permission.default_setting = CONTENT_SETTING_BLOCK;
popups_permission.source = SettingSource::kUser;
popups_permission.is_one_time = false;
// Block (default) -> Allow
PageInfoUI::ToggleBetweenAllowAndBlock(popups_permission);
EXPECT_EQ(popups_permission.setting, CONTENT_SETTING_ALLOW);
// Allow -> Block
// The page info always creates a site exception even if the target setting
// matches the default.
PageInfoUI::ToggleBetweenAllowAndBlock(popups_permission);
EXPECT_EQ(popups_permission.setting, CONTENT_SETTING_BLOCK);
// Block -> Allow
PageInfoUI::ToggleBetweenAllowAndBlock(popups_permission);
EXPECT_EQ(popups_permission.setting, CONTENT_SETTING_ALLOW);
}
// Testing all possible state transitions for a guard content settings with a
// default setting ask.
TEST_F(PageInfoToggleStatesUnitTest, ToggleGuardPermissionDefaultAskTest) {
PageInfo::PermissionInfo usb_guard;
usb_guard.type = ContentSettingsType::USB_GUARD;
usb_guard.setting = CONTENT_SETTING_DEFAULT;
usb_guard.default_setting = CONTENT_SETTING_ASK;
usb_guard.source = SettingSource::kUser;
usb_guard.is_one_time = false;
// Ask (default) -> Block
PageInfoUI::ToggleBetweenAllowAndBlock(usb_guard);
EXPECT_EQ(usb_guard.setting, CONTENT_SETTING_BLOCK);
// Block -> Ask
// The page info always creates a site exception even if the target setting
// matches the default.
PageInfoUI::ToggleBetweenAllowAndBlock(usb_guard);
EXPECT_EQ(usb_guard.setting, CONTENT_SETTING_ASK);
// Ask -> Block
PageInfoUI::ToggleBetweenAllowAndBlock(usb_guard);
EXPECT_EQ(usb_guard.setting, CONTENT_SETTING_BLOCK);
}
// Testing all possible state transitions for a guard content settings with a
// default setting block.
TEST_F(PageInfoToggleStatesUnitTest, ToggleGuardPermissionDefaultBlockTest) {
PageInfo::PermissionInfo hid_guard;
hid_guard.type = ContentSettingsType::HID_GUARD;
hid_guard.setting = CONTENT_SETTING_DEFAULT;
hid_guard.default_setting = CONTENT_SETTING_BLOCK;
hid_guard.source = SettingSource::kUser;
hid_guard.is_one_time = false;
// Block (default) -> Ask
PageInfoUI::ToggleBetweenAllowAndBlock(hid_guard);
EXPECT_EQ(hid_guard.setting, CONTENT_SETTING_ASK);
// Ask -> Block
// The page info always creates a site exception even if the target setting
// matches the default.
PageInfoUI::ToggleBetweenAllowAndBlock(hid_guard);
EXPECT_EQ(hid_guard.setting, CONTENT_SETTING_BLOCK);
// Block -> Ask
PageInfoUI::ToggleBetweenAllowAndBlock(hid_guard);
EXPECT_EQ(hid_guard.setting, CONTENT_SETTING_ASK);
}
TEST_F(PageInfoTest, WithoutPageSpecificContentSettings) {
SetContents(CreateTestWebContents());
EXPECT_FALSE(content_settings::PageSpecificContentSettings::GetForPage(
web_contents()->GetPrimaryPage()));
page_info();
}
TEST_F(PageInfoTest, MidiGrantsAreFilteredWhenAllowSysex) {
std::set<ContentSettingsType> expected_visible_permissions;
auto* map = HostContentSettingsMapFactory::GetForProfile(profile());
page_info()->PresentSitePermissionsForTesting();
#if BUILDFLAG(IS_ANDROID)
// Geolocation is always allowed to pass through to Android-specific logic to
// check for DSE settings (so expect 1 item), but isn't actually shown later
// on because this test isn't testing with a default search engine origin.
expected_visible_permissions.insert(ContentSettingsType::GEOLOCATION);
#endif
ExpectPermissionInfoList(expected_visible_permissions,
last_permission_info_list());
map->SetContentSettingDefaultScope(
url(), url(), ContentSettingsType::MIDI_SYSEX, CONTENT_SETTING_ALLOW);
page_info()->PresentSitePermissionsForTesting();
expected_visible_permissions.insert(ContentSettingsType::MIDI_SYSEX);
ExpectPermissionInfoList(expected_visible_permissions,
last_permission_info_list());
}
TEST_F(PageInfoTest, OriginLevelExceptionScope) {
SetURL("https://www.example.com");
auto* map = HostContentSettingsMapFactory::GetForProfile(profile());
page_info()->PresentSitePermissionsForTesting();
map->SetContentSettingDefaultScope(url(), url(), ContentSettingsType::POPUPS,
CONTENT_SETTING_ALLOW);
page_info()->OnSitePermissionChanged(ContentSettingsType::POPUPS,
CONTENT_SETTING_BLOCK,
/*requesting_origin=*/std::nullopt,
/*is_one_time=*/false);
// Page info has created an allow origin-level exception.
content_settings::SettingInfo info;
ContentSetting setting =
map->GetContentSetting(url(), url(), ContentSettingsType::POPUPS, &info);
EXPECT_EQ(setting, CONTENT_SETTING_BLOCK);
EXPECT_EQ(info.primary_pattern,
ContentSettingsPattern::FromURLNoWildcard(url()));
EXPECT_EQ(info.secondary_pattern, ContentSettingsPattern::Wildcard());
page_info()->OnSitePermissionChanged(ContentSettingsType::POPUPS,
CONTENT_SETTING_ALLOW,
/*requesting_origin=*/std::nullopt,
/*is_one_time=*/false);
// Page info has created a block origin-level exception.
setting =
map->GetContentSetting(url(), url(), ContentSettingsType::POPUPS, &info);
EXPECT_EQ(setting, CONTENT_SETTING_ALLOW);
EXPECT_EQ(info.primary_pattern,
ContentSettingsPattern::FromURLNoWildcard(url()));
EXPECT_EQ(info.secondary_pattern, ContentSettingsPattern::Wildcard());
}
TEST_F(PageInfoTest, CustomExceptionScope) {
SetURL("https://www.example.com");
auto* map = HostContentSettingsMapFactory::GetForProfile(profile());
page_info()->PresentSitePermissionsForTesting();
map->SetContentSettingCustomScope(
ContentSettingsPattern::FromString("https://*"),
ContentSettingsPattern::Wildcard(), ContentSettingsType::JAVASCRIPT,
CONTENT_SETTING_BLOCK);
page_info()->OnSitePermissionChanged(ContentSettingsType::JAVASCRIPT,
CONTENT_SETTING_ALLOW,
/*requesting_origin=*/std::nullopt,
/*is_one_time=*/false);
// Page info has created an allow origin-level exception.
content_settings::SettingInfo info;
ContentSetting setting = map->GetContentSetting(
url(), url(), ContentSettingsType::JAVASCRIPT, &info);
EXPECT_EQ(setting, CONTENT_SETTING_ALLOW);
EXPECT_EQ(info.primary_pattern,
ContentSettingsPattern::FromURLNoWildcard(url()));
EXPECT_EQ(info.secondary_pattern, ContentSettingsPattern::Wildcard());
// Other origins matching the pattern are not affected.
GURL another_url = GURL("https://www.another.com");
EXPECT_EQ(CONTENT_SETTING_BLOCK,
map->GetContentSetting(another_url, another_url,
ContentSettingsType::JAVASCRIPT));
page_info()->OnSitePermissionChanged(ContentSettingsType::JAVASCRIPT,
CONTENT_SETTING_BLOCK,
/*requesting_origin=*/std::nullopt,
/*is_one_time=*/false);
// Page info has created a block origin-level exception.
setting = map->GetContentSetting(url(), url(),
ContentSettingsType::JAVASCRIPT, &info);
EXPECT_EQ(setting, CONTENT_SETTING_BLOCK);
EXPECT_EQ(info.primary_pattern,
ContentSettingsPattern::FromURLNoWildcard(url()));
EXPECT_EQ(info.secondary_pattern, ContentSettingsPattern::Wildcard());
}
TEST_F(PageInfoTest, SiteExceptionScopeTypeMetrics) {
SetURL("https://www.example.com:443");
constexpr char kScopeTypeHistogram[] =
"Privacy.PageInfo.SiteExceptionsScopeType";
base::HistogramTester tester;
tester.ExpectTotalCount(kScopeTypeHistogram, 0);
auto* map = HostContentSettingsMapFactory::GetForProfile(profile());
map->SetContentSettingCustomScope(
ContentSettingsPattern::FromString("https://www.example.com"),
ContentSettingsPattern::Wildcard(), ContentSettingsType::JAVASCRIPT,
CONTENT_SETTING_BLOCK);
map->SetContentSettingCustomScope(
ContentSettingsPattern::FromString("https://*"),
ContentSettingsPattern::Wildcard(), ContentSettingsType::MIDI_SYSEX,
CONTENT_SETTING_ALLOW);
page_info()->PresentSitePermissionsForTesting();
tester.ExpectBucketCount(kScopeTypeHistogram,
ContentSettingsPattern::Scope::kWithPortWildcard,
1 /* expected_count */);
tester.ExpectBucketCount(kScopeTypeHistogram,
ContentSettingsPattern::Scope::kCustomScope,
1 /* expected_count */);
// Repeated calls of PresentSitePermissions won't rerecord metrics within the
// same page info instance.
page_info()->PresentSitePermissionsForTesting();
tester.ExpectBucketCount(kScopeTypeHistogram,
ContentSettingsPattern::Scope::kWithPortWildcard,
1 /* expected_count */);
tester.ExpectBucketCount(kScopeTypeHistogram,
ContentSettingsPattern::Scope::kCustomScope,
1 /* expected_count */);
}
|