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
|
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/webui/settings/site_settings_helper.h"
#include <algorithm>
#include <array>
#include <functional>
#include <set>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
#include "base/command_line.h"
#include "base/containers/adapters.h"
#include "base/containers/contains.h"
#include "base/feature_list.h"
#include "base/json/values_util.h"
#include "base/no_destructor.h"
#include "base/notreached.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "build/build_config.h"
#include "chrome/browser/bluetooth/bluetooth_chooser_context_factory.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/file_system_access/chrome_file_system_access_permission_context.h"
#include "chrome/browser/file_system_access/file_system_access_features.h"
#include "chrome/browser/file_system_access/file_system_access_permission_context_factory.h"
#include "chrome/browser/hid/hid_chooser_context.h"
#include "chrome/browser/hid/hid_chooser_context_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/serial/serial_chooser_context.h"
#include "chrome/browser/serial/serial_chooser_context_factory.h"
#include "chrome/browser/subresource_filter/subresource_filter_profile_context_factory.h"
#include "chrome/browser/ui/ui_features.h"
#include "chrome/browser/ui/url_identity.h"
#include "chrome/browser/usb/usb_chooser_context.h"
#include "chrome/browser/usb/usb_chooser_context_factory.h"
#include "chrome/browser/web_applications/isolated_web_apps/isolated_web_app_url_info.h"
#include "chrome/browser/web_applications/web_app.h"
#include "chrome/browser/web_applications/web_app_provider.h"
#include "chrome/browser/web_applications/web_app_registrar.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h"
#include "chrome/grit/generated_resources.h"
#include "components/content_settings/core/browser/content_settings_provider.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_pattern.h"
#include "components/content_settings/core/common/content_settings_types.h"
#include "components/content_settings/core/common/content_settings_utils.h"
#include "components/permissions/contexts/bluetooth_chooser_context.h"
#include "components/permissions/object_permission_context_base.h"
#include "components/permissions/permission_decision_auto_blocker.h"
#include "components/permissions/permission_util.h"
#include "components/permissions/permissions_client.h"
#include "components/prefs/pref_service.h"
#include "components/privacy_sandbox/privacy_sandbox_features.h"
#include "components/strings/grit/components_strings.h"
#include "components/strings/grit/privacy_sandbox_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 "components/subresource_filter/core/browser/subresource_filter_features.h"
#include "components/url_formatter/elide_url.h"
#include "components/url_formatter/url_formatter.h"
#include "content/public/browser/permission_controller.h"
#include "content/public/browser/permission_descriptor_util.h"
#include "content/public/browser/permission_result.h"
#include "content/public/common/content_features.h"
#include "content/public/common/content_switches.h"
#include "content/public/common/url_utils.h"
#include "device/vr/buildflags/buildflags.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/common/constants.h"
#include "services/network/public/cpp/features.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/common/features_generated.h"
#include "third_party/blink/public/common/permissions/permission_utils.h"
#include "ui/base/l10n/l10n_util.h"
#include "url/origin.h"
#if BUILDFLAG(IS_CHROMEOS)
#include "chrome/browser/smart_card/smart_card_permission_context.h"
#include "chrome/browser/smart_card/smart_card_permission_context_factory.h"
#endif // BUILDFLAG(IS_CHROMEOS)
#if BUILDFLAG(ENABLE_VR)
#include "device/vr/public/cpp/features.h"
#endif
namespace site_settings {
constexpr char kAppName[] = "appName";
constexpr char kAppId[] = "appId";
namespace {
using PermissionStatus = blink::mojom::PermissionStatus;
using ::content_settings::ProviderType;
using ::content_settings::SettingSource;
// Chooser data group names.
const char kUsbChooserDataGroupType[] = "usb-devices-data";
const char kSerialChooserDataGroupType[] = "serial-ports-data";
const char kHidChooserDataGroupType[] = "hid-devices-data";
const char kBluetoothChooserDataGroupType[] = "bluetooth-devices-data";
const char kSmartCardChooserDataGroupType[] = "smart-card-readers-data";
constexpr auto kContentSettingsTypeGroupNames = std::to_array<
const ContentSettingsTypeNameEntry>({
// The following ContentSettingsTypes have UI in Content Settings
// and require a mapping from their Javascript string representation in
// chrome/browser/resources/settings/site_settings/constants.ts to their C++
// ContentSettingsType provided here. These group names are only used by
// desktop webui.
{ContentSettingsType::COOKIES, "cookies"},
{ContentSettingsType::IMAGES, "images"},
{ContentSettingsType::JAVASCRIPT, "javascript"},
{ContentSettingsType::JAVASCRIPT_JIT, "javascript-jit"},
{ContentSettingsType::JAVASCRIPT_OPTIMIZER, "javascript-optimizer"},
{ContentSettingsType::POPUPS, "popups"},
{ContentSettingsType::GEOLOCATION, "location"},
{ContentSettingsType::NOTIFICATIONS, "notifications"},
{ContentSettingsType::MEDIASTREAM_MIC, "media-stream-mic"},
{ContentSettingsType::MEDIASTREAM_CAMERA, "media-stream-camera"},
{ContentSettingsType::PROTOCOL_HANDLERS, "register-protocol-handler"},
{ContentSettingsType::AUTOMATIC_DOWNLOADS, "multiple-automatic-downloads"},
{ContentSettingsType::MIDI_SYSEX, "midi-sysex"},
{ContentSettingsType::PROTECTED_MEDIA_IDENTIFIER, "protected-content"},
{ContentSettingsType::BACKGROUND_SYNC, "background-sync"},
{ContentSettingsType::ADS, "ads"},
{ContentSettingsType::SOUND, "sound"},
{ContentSettingsType::CLIPBOARD_READ_WRITE, "clipboard"},
{ContentSettingsType::SENSORS, "sensors"},
{ContentSettingsType::PAYMENT_HANDLER, "payment-handler"},
{ContentSettingsType::USB_GUARD, "usb-devices"},
{ContentSettingsType::USB_CHOOSER_DATA, kUsbChooserDataGroupType},
{ContentSettingsType::IDLE_DETECTION, "idle-detection"},
{ContentSettingsType::SERIAL_GUARD, "serial-ports"},
{ContentSettingsType::SERIAL_CHOOSER_DATA, kSerialChooserDataGroupType},
{ContentSettingsType::BLUETOOTH_SCANNING, "bluetooth-scanning"},
{ContentSettingsType::HID_GUARD, "hid-devices"},
{ContentSettingsType::HID_CHOOSER_DATA, kHidChooserDataGroupType},
{ContentSettingsType::FILE_SYSTEM_WRITE_GUARD, "file-system-write"},
{ContentSettingsType::MIXEDSCRIPT, "mixed-script"},
{ContentSettingsType::VR, "vr"},
{ContentSettingsType::AR, "ar"},
{ContentSettingsType::HAND_TRACKING, "hand-tracking"},
{ContentSettingsType::BLUETOOTH_GUARD, "bluetooth-devices"},
{ContentSettingsType::BLUETOOTH_CHOOSER_DATA,
kBluetoothChooserDataGroupType},
{ContentSettingsType::WINDOW_MANAGEMENT, "window-management"},
{ContentSettingsType::LOCAL_FONTS, "local-fonts"},
{ContentSettingsType::FILE_SYSTEM_ACCESS_CHOOSER_DATA,
"file-system-access-handles-data"},
{ContentSettingsType::FEDERATED_IDENTITY_API, "federated-identity-api"},
{ContentSettingsType::PRIVATE_NETWORK_GUARD, "private-network-devices"},
{ContentSettingsType::PRIVATE_NETWORK_CHOOSER_DATA,
"private-network-devices-data"},
{ContentSettingsType::ANTI_ABUSE, "anti-abuse"},
{ContentSettingsType::STORAGE_ACCESS, "storage-access"},
{ContentSettingsType::AUTO_PICTURE_IN_PICTURE, "auto-picture-in-picture"},
{ContentSettingsType::CAPTURED_SURFACE_CONTROL, "captured-surface-control"},
{ContentSettingsType::WEB_PRINTING, "web-printing"},
{ContentSettingsType::SPEAKER_SELECTION, "speaker-selection"},
{ContentSettingsType::AUTOMATIC_FULLSCREEN, "automatic-fullscreen"},
{ContentSettingsType::KEYBOARD_LOCK, "keyboard-lock"},
{ContentSettingsType::TRACKING_PROTECTION, "tracking-protection"},
{ContentSettingsType::TOP_LEVEL_STORAGE_ACCESS, "top-level-storage-access"},
{ContentSettingsType::WEB_APP_INSTALLATION, "web-app-installation"},
{ContentSettingsType::SMART_CARD_GUARD, "smart-card-readers"},
{ContentSettingsType::SMART_CARD_DATA, kSmartCardChooserDataGroupType},
{ContentSettingsType::LOCAL_NETWORK_ACCESS, "local-network-access"},
// Add new content settings here if a corresponding Javascript string
// representation for it is not required, for example if the content setting
// is not used for desktop. Note some exceptions do have UI in Content
// Settings but do not require a separate string.
{ContentSettingsType::DEFAULT, nullptr},
{ContentSettingsType::AUTO_SELECT_CERTIFICATE, nullptr},
{ContentSettingsType::SSL_CERT_DECISIONS, nullptr},
{ContentSettingsType::APP_BANNER, nullptr},
{ContentSettingsType::SITE_ENGAGEMENT, nullptr},
{ContentSettingsType::DURABLE_STORAGE, nullptr},
{ContentSettingsType::AUTOPLAY, nullptr},
{ContentSettingsType::IMPORTANT_SITE_INFO, nullptr},
{ContentSettingsType::PERMISSION_AUTOBLOCKER_DATA, nullptr},
{ContentSettingsType::ADS_DATA, nullptr},
{ContentSettingsType::MIDI, nullptr},
{ContentSettingsType::PASSWORD_PROTECTION, nullptr},
{ContentSettingsType::MEDIA_ENGAGEMENT, nullptr},
{ContentSettingsType::CLIENT_HINTS, nullptr},
{ContentSettingsType::DEPRECATED_ACCESSIBILITY_EVENTS, nullptr},
{ContentSettingsType::CLIPBOARD_SANITIZED_WRITE, nullptr},
{ContentSettingsType::BACKGROUND_FETCH, nullptr},
{ContentSettingsType::INTENT_PICKER_DISPLAY, nullptr},
{ContentSettingsType::PERIODIC_BACKGROUND_SYNC, nullptr},
{ContentSettingsType::WAKE_LOCK_SCREEN, nullptr},
{ContentSettingsType::WAKE_LOCK_SYSTEM, nullptr},
{ContentSettingsType::LEGACY_COOKIE_ACCESS, nullptr},
{ContentSettingsType::NFC, nullptr},
{ContentSettingsType::SAFE_BROWSING_URL_CHECK_DATA, nullptr},
{ContentSettingsType::FILE_SYSTEM_READ_GUARD, nullptr},
{ContentSettingsType::CAMERA_PAN_TILT_ZOOM, nullptr},
{ContentSettingsType::PERMISSION_AUTOREVOCATION_DATA, nullptr},
{ContentSettingsType::FILE_SYSTEM_LAST_PICKED_DIRECTORY, nullptr},
{ContentSettingsType::DISPLAY_CAPTURE, nullptr},
{ContentSettingsType::FEDERATED_IDENTITY_SHARING, nullptr},
{ContentSettingsType::HTTP_ALLOWED, nullptr},
{ContentSettingsType::HTTPS_ENFORCED, nullptr},
{ContentSettingsType::FORMFILL_METADATA, nullptr},
{ContentSettingsType::DEPRECATED_FEDERATED_IDENTITY_ACTIVE_SESSION,
nullptr},
{ContentSettingsType::AUTO_DARK_WEB_CONTENT, nullptr},
{ContentSettingsType::REQUEST_DESKTOP_SITE, nullptr},
{ContentSettingsType::NOTIFICATION_INTERACTIONS, nullptr},
{ContentSettingsType::REDUCED_ACCEPT_LANGUAGE, nullptr},
{ContentSettingsType::NOTIFICATION_PERMISSION_REVIEW, nullptr},
{ContentSettingsType::FEDERATED_IDENTITY_IDENTITY_PROVIDER_SIGNIN_STATUS,
nullptr},
// PPAPI_BROKER has been deprecated. The content setting is not used or
// called from UI, so we don't need a representation JS string.
{ContentSettingsType::DEPRECATED_PPAPI_BROKER, nullptr},
{ContentSettingsType::REVOKED_UNUSED_SITE_PERMISSIONS, nullptr},
// TODO(crbug.com/40253587): Update JavaScript string representation when
// desktop UI is implemented.
{ContentSettingsType::FEDERATED_IDENTITY_AUTO_REAUTHN_PERMISSION, nullptr},
{ContentSettingsType::FEDERATED_IDENTITY_IDENTITY_PROVIDER_REGISTRATION,
nullptr},
{ContentSettingsType::THIRD_PARTY_STORAGE_PARTITIONING, nullptr},
{ContentSettingsType::ALL_SCREEN_CAPTURE, nullptr},
{ContentSettingsType::COOKIE_CONTROLS_METADATA, nullptr},
{ContentSettingsType::TPCD_TRIAL, nullptr},
{ContentSettingsType::TPCD_METADATA_GRANTS, nullptr},
// TODO(crbug.com/40101962): Update the name once the design is finalized
// for the integration with Safety Hub.
{ContentSettingsType::FILE_SYSTEM_ACCESS_EXTENDED_PERMISSION, nullptr},
{ContentSettingsType::TPCD_HEURISTICS_GRANTS, nullptr},
{ContentSettingsType::FILE_SYSTEM_ACCESS_RESTORE_PERMISSION, nullptr},
{ContentSettingsType::TOP_LEVEL_TPCD_TRIAL, nullptr},
{ContentSettingsType::SUB_APP_INSTALLATION_PROMPTS, nullptr},
{ContentSettingsType::DIRECT_SOCKETS, nullptr},
{ContentSettingsType::REVOKED_ABUSIVE_NOTIFICATION_PERMISSIONS, nullptr},
{ContentSettingsType::TOP_LEVEL_TPCD_ORIGIN_TRIAL, nullptr},
{ContentSettingsType::DISPLAY_MEDIA_SYSTEM_AUDIO, nullptr},
{ContentSettingsType::STORAGE_ACCESS_HEADER_ORIGIN_TRIAL, nullptr},
// TODO(crbug.com/368266658): Implement the UI for Direct Sockets PNA.
{ContentSettingsType::DIRECT_SOCKETS_PRIVATE_NETWORK_ACCESS, nullptr},
{ContentSettingsType::LEGACY_COOKIE_SCOPE, nullptr},
{ContentSettingsType::ARE_SUSPICIOUS_NOTIFICATIONS_ALLOWLISTED_BY_USER,
nullptr},
{ContentSettingsType::CONTROLLED_FRAME, nullptr},
// POINTER_LOCK has been deprecated.
{ContentSettingsType::POINTER_LOCK, nullptr},
{ContentSettingsType::REVOKED_DISRUPTIVE_NOTIFICATION_PERMISSIONS, nullptr},
{ContentSettingsType::ON_DEVICE_SPEECH_RECOGNITION_LANGUAGES_DOWNLOADED,
nullptr},
{ContentSettingsType::INITIALIZED_TRANSLATIONS, nullptr},
{ContentSettingsType::SUSPICIOUS_NOTIFICATION_IDS, nullptr},
});
static_assert(
kContentSettingsTypeGroupNames.size() ==
// Add one since the sequence is kMinValue = -1, 0, ..., kMaxValue
1 + static_cast<int32_t>(ContentSettingsType::kMaxValue) -
static_cast<int32_t>(ContentSettingsType::kMinValue),
"kContentSettingsTypeGroupNames should have the correct number "
"of elements");
struct SiteSettingSourceStringMapping {
SiteSettingSource source;
const char* source_str;
};
// Determines whether an IWA-specific `content_setting` should be shown for a
// particular `origin`.
bool ShouldShowIwaContentSettingForOrigin(Profile* profile,
std::string_view origin,
ContentSettingsType content_setting) {
// Show for non-origin-specific lists, IWAs, and non-default values.
if (origin.empty() || GURL(origin).SchemeIs(chrome::kIsolatedAppScheme)) {
return true;
}
if (!profile) {
return false;
}
SiteSettingSource source;
GetContentSettingForOrigin(
profile, HostContentSettingsMapFactory::GetForProfile(profile),
GURL(origin), content_setting, &source);
return source != SiteSettingSource::kDefault;
}
// Retrieves the corresponding string, according to the following precedence
// order from highest to lowest priority:
// 1. Allowlisted WebUI content setting.
// 2. Kill-switch.
// 3. Insecure origins (some permissions are denied to insecure origins).
// 4. Enterprise policy.
// 5. Extensions.
// 6. Activated for ads filtering (for Ads ContentSettingsType only).
// 7. User-set per-origin setting.
// 8. Embargo.
// 9. User-set patterns.
// 10. User-set global default for a ContentSettingsType.
// 11. Chrome's built-in default.
SiteSettingSource CalculateSiteSettingSource(
Profile* profile,
const ContentSettingsType content_type,
const GURL& origin,
const content_settings::SettingInfo& info,
const content::PermissionResult result) {
if (info.source == SettingSource::kAllowList) {
return SiteSettingSource::kAllowlist; // Source #1.
}
if (result.source == content::PermissionStatusSource::KILL_SWITCH) {
return SiteSettingSource::kKillSwitch; // Source #2.
}
if (result.source == content::PermissionStatusSource::INSECURE_ORIGIN) {
return SiteSettingSource::kInsecureOrigin; // Source #3.
}
if (info.source == SettingSource::kPolicy ||
info.source == SettingSource::kSupervised) {
return SiteSettingSource::kPolicy; // Source #4.
}
if (info.source == SettingSource::kExtension) {
return SiteSettingSource::kExtension; // Source #5.
}
if (content_type == ContentSettingsType::ADS &&
base::FeatureList::IsEnabled(
subresource_filter::kSafeBrowsingSubresourceFilter)) {
subresource_filter::SubresourceFilterContentSettingsManager*
settings_manager =
SubresourceFilterProfileContextFactory::GetForProfile(profile)
->settings_manager();
if (settings_manager->GetSiteActivationFromMetadata(origin)) {
return SiteSettingSource::kAdsFilterBlocklist; // Source #6.
}
}
DCHECK_NE(SettingSource::kNone, info.source);
if (info.source == SettingSource::kUser) {
if (result.source == content::PermissionStatusSource::MULTIPLE_DISMISSALS ||
result.source == content::PermissionStatusSource::MULTIPLE_IGNORES) {
return SiteSettingSource::kEmbargo; // Source #8.
}
if (info.primary_pattern == ContentSettingsPattern::Wildcard() &&
info.secondary_pattern == ContentSettingsPattern::Wildcard()) {
return SiteSettingSource::kDefault; // Source #10, #11.
}
// Source #7, #9. When #7 is the source, |result.source| won't be set to
// any of the source #7 enum values, as PermissionManager is aware of the
// difference between these two sources internally. The subtlety here should
// go away when PermissionManager can handle all content settings and all
// possible sources.
return SiteSettingSource::kPreference;
}
NOTREACHED();
}
bool IsFromWebUIAllowlistSource(const ContentSettingPatternSource& pattern) {
return pattern.source == ProviderType::kWebuiAllowlistProvider;
}
// If the given |pattern| represents an individual origin, Isolated Web App, or
// extension, retrieve a string to display it as such. If not, return the
// pattern as a string.
std::string GetDisplayNameForPattern(Profile* profile,
const ContentSettingsPattern& pattern) {
GURL url(pattern.ToString());
if (url.is_valid() && (url.SchemeIs(extensions::kExtensionScheme) ||
url.SchemeIs(chrome::kIsolatedAppScheme))) {
return GetDisplayNameForGURL(profile, url, /*hostname_only=*/false);
}
return pattern.ToString();
}
// Returns exceptions constructed from the policy-set allowed URLs
// for the content settings |type| mic or camera.
void GetPolicyAllowedUrls(ContentSettingsType type,
std::vector<base::Value::Dict>* exceptions,
content::WebUI* web_ui,
bool incognito) {
DCHECK(type == ContentSettingsType::MEDIASTREAM_MIC ||
type == ContentSettingsType::MEDIASTREAM_CAMERA);
Profile* profile = Profile::FromWebUI(web_ui);
PrefService* prefs = profile->GetPrefs();
const base::Value::List& policy_urls =
prefs->GetList(type == ContentSettingsType::MEDIASTREAM_MIC
? prefs::kAudioCaptureAllowedUrls
: prefs::kVideoCaptureAllowedUrls);
// Convert the URLs to |ContentSettingsPattern|s. Ignore any invalid ones.
std::vector<ContentSettingsPattern> patterns;
for (const auto& entry : policy_urls) {
const std::string* url = entry.GetIfString();
if (!url) {
continue;
}
ContentSettingsPattern pattern = ContentSettingsPattern::FromString(*url);
if (!pattern.IsValid()) {
continue;
}
patterns.push_back(pattern);
}
// The patterns are shown in the UI in a reverse order defined by
// |ContentSettingsPattern::operator<|.
std::sort(patterns.begin(), patterns.end(),
std::greater<ContentSettingsPattern>());
for (const ContentSettingsPattern& pattern : patterns) {
std::string display_name = GetDisplayNameForPattern(profile, pattern);
exceptions->push_back(GetExceptionForPage(
type, profile, pattern, ContentSettingsPattern(), display_name,
CONTENT_SETTING_ALLOW, SiteSettingSource::kPolicy,
// Pass base::Time() to indicate the exceptions do not expire.
base::Time(), incognito));
}
}
// Retrieves the source of a chooser exception as a string. This method uses the
// CalculateSiteSettingSource method above to calculate the correct string to
// use.
SiteSettingSource GetSourceForChooserException(Profile* profile,
ContentSettingsType content_type,
SettingSource source) {
// Prepare the parameters needed by CalculateSiteSettingSource
content_settings::SettingInfo info;
info.source = source;
// Chooser exceptions do not use a ContentSettingPermissionContextBase for
// their permissions.
content::PermissionResult permission_result(
PermissionStatus::ASK, content::PermissionStatusSource::UNSPECIFIED);
// The |origin| parameter is only used for |ContentSettingsType::ADS| with
// the |kSafeBrowsingSubresourceFilter| feature flag enabled, so an empty GURL
// is used.
SiteSettingSource calculated_source = CalculateSiteSettingSource(
profile, content_type, /*origin=*/GURL(), info, permission_result);
DCHECK(calculated_source == SiteSettingSource::kPolicy ||
calculated_source == SiteSettingSource::kPreference);
return calculated_source;
}
permissions::ObjectPermissionContextBase* GetUsbChooserContext(
Profile* profile) {
return UsbChooserContextFactory::GetForProfile(profile);
}
permissions::ObjectPermissionContextBase* GetSerialChooserContext(
Profile* profile) {
return SerialChooserContextFactory::GetForProfile(profile);
}
permissions::ObjectPermissionContextBase* GetHidChooserContext(
Profile* profile) {
return HidChooserContextFactory::GetForProfile(profile);
}
// The BluetoothChooserContext is only available when the
// WebBluetoothNewPermissionsBackend flag is enabled.
// TODO(crbug.com/40458188): Remove the feature check when it is enabled
// by default.
permissions::ObjectPermissionContextBase* GetBluetoothChooserContext(
Profile* profile) {
if (base::FeatureList::IsEnabled(
features::kWebBluetoothNewPermissionsBackend)) {
return BluetoothChooserContextFactory::GetForProfile(profile);
}
return nullptr;
}
#if BUILDFLAG(IS_CHROMEOS)
permissions::ObjectPermissionContextBase* GetSmartCardChooserContext(
Profile* profile) {
if (base::FeatureList::IsEnabled(blink::features::kSmartCard)) {
return &SmartCardPermissionContextFactory::GetForProfile(*profile);
}
return nullptr;
}
#endif // BUILDFLAG(IS_CHROMEOS)
const ChooserTypeNameEntry kChooserTypeGroupNames[] = {
{&GetUsbChooserContext, kUsbChooserDataGroupType},
{&GetSerialChooserContext, kSerialChooserDataGroupType},
{&GetHidChooserContext, kHidChooserDataGroupType},
{&GetBluetoothChooserContext, kBluetoothChooserDataGroupType},
#if BUILDFLAG(IS_CHROMEOS)
{&GetSmartCardChooserContext, kSmartCardChooserDataGroupType}
#endif // BUILDFLAG(IS_CHROMEOS)
};
// These variables represent different formatting options for default (i.e. not
// extension or IWA) URLs as well as fallbacks for when the IWA/extension is not
// found in the registry.
constexpr UrlIdentity::FormatOptions kUrlIdentityOptionsOmitHttps = {
.default_options = {
UrlIdentity::DefaultFormatOptions::kOmitCryptographicScheme}};
constexpr UrlIdentity::FormatOptions kUrlIdentityOptionsHostOnly = {
.default_options = {UrlIdentity::DefaultFormatOptions::kHostname}};
constexpr UrlIdentity::FormatOptions kUrlIdentityOptionsRawSpec = {
.default_options = {UrlIdentity::DefaultFormatOptions::kRawSpec}};
constexpr UrlIdentity::TypeSet kUrlIdentityAllowedTypes = {
UrlIdentity::Type::kDefault, UrlIdentity::Type::kFile,
UrlIdentity::Type::kIsolatedWebApp, UrlIdentity::Type::kChromeExtension};
} // namespace
bool HasRegisteredGroupName(ContentSettingsType type) {
for (auto kContentSettingsTypeGroupName : kContentSettingsTypeGroupNames) {
if (type == kContentSettingsTypeGroupName.type &&
kContentSettingsTypeGroupName.name) {
return true;
}
}
return false;
}
ContentSettingsType ContentSettingsTypeFromGroupName(std::string_view name) {
for (const auto& entry : kContentSettingsTypeGroupNames) {
// Content setting types that aren't represented in the settings UI
// will have `nullptr` as their `name`. However, converting `nullptr`
// to a std::string_view will crash, so we have to handle it explicitly
// before comparing.
if (entry.name != nullptr && entry.name == name) {
return entry.type;
}
}
return ContentSettingsType::DEFAULT;
}
std::string_view ContentSettingsTypeToGroupName(ContentSettingsType type) {
for (const auto& entry : kContentSettingsTypeGroupNames) {
if (type == entry.type) {
// Content setting types that aren't represented in the settings UI
// will have `nullptr` as their `name`. Although they are valid content
// settings types, they don't have a readable name.
// TODO(crbug.com/40066645): Replace LOG with CHECK.
if (!entry.name) {
LOG(ERROR) << static_cast<int32_t>(type)
<< " does not have a readable name.";
}
return entry.name ? entry.name : std::string_view();
}
}
NOTREACHED() << static_cast<int32_t>(type)
<< " is not a recognized content settings type.";
}
std::vector<ContentSettingsType> GetVisiblePermissionCategories(
const std::string& origin,
Profile* profile) {
// First build the list of permissions that will be shown regardless of
// `origin`. Some categories such as COOKIES store their data in a custom way,
// so are not included here.
static base::NoDestructor<std::vector<ContentSettingsType>> base_types{{
ContentSettingsType::AR,
ContentSettingsType::AUTOMATIC_DOWNLOADS,
ContentSettingsType::BACKGROUND_SYNC,
ContentSettingsType::CLIPBOARD_READ_WRITE,
ContentSettingsType::FILE_SYSTEM_WRITE_GUARD,
ContentSettingsType::GEOLOCATION,
ContentSettingsType::HID_GUARD,
ContentSettingsType::IDLE_DETECTION,
ContentSettingsType::IMAGES,
ContentSettingsType::JAVASCRIPT,
ContentSettingsType::JAVASCRIPT_OPTIMIZER,
ContentSettingsType::LOCAL_FONTS,
ContentSettingsType::MEDIASTREAM_CAMERA,
ContentSettingsType::MEDIASTREAM_MIC,
ContentSettingsType::MIDI_SYSEX,
ContentSettingsType::MIXEDSCRIPT,
ContentSettingsType::JAVASCRIPT_JIT,
ContentSettingsType::NOTIFICATIONS,
ContentSettingsType::POPUPS,
#if BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_WIN)
ContentSettingsType::PROTECTED_MEDIA_IDENTIFIER,
#endif
ContentSettingsType::SENSORS,
ContentSettingsType::SERIAL_GUARD,
#if BUILDFLAG(IS_CHROMEOS)
ContentSettingsType::SMART_CARD_GUARD,
#endif
ContentSettingsType::SOUND,
ContentSettingsType::STORAGE_ACCESS,
ContentSettingsType::TOP_LEVEL_STORAGE_ACCESS,
ContentSettingsType::USB_GUARD,
ContentSettingsType::VR,
ContentSettingsType::WINDOW_MANAGEMENT,
}};
static bool initialized = false;
if (!initialized) {
// The permission categories in this block are only shown when running with
// certain flags/switches.
if (base::CommandLine::ForCurrentProcess()->HasSwitch(
::switches::kEnableExperimentalWebPlatformFeatures)) {
base_types->push_back(ContentSettingsType::BLUETOOTH_SCANNING);
}
if (base::FeatureList::IsEnabled(::features::kServiceWorkerPaymentApps)) {
base_types->push_back(ContentSettingsType::PAYMENT_HANDLER);
}
if (base::FeatureList::IsEnabled(features::kFedCm)) {
base_types->push_back(ContentSettingsType::FEDERATED_IDENTITY_API);
}
if (base::FeatureList::IsEnabled(
features::kWebBluetoothNewPermissionsBackend)) {
base_types->push_back(ContentSettingsType::BLUETOOTH_GUARD);
}
if (base::FeatureList::IsEnabled(
subresource_filter::kSafeBrowsingSubresourceFilter)) {
base_types->push_back(ContentSettingsType::ADS);
}
if (base::FeatureList::IsEnabled(
network::features::kPrivateNetworkAccessPermissionPrompt)) {
base_types->push_back(ContentSettingsType::PRIVATE_NETWORK_GUARD);
}
if (base::FeatureList::IsEnabled(
blink::features::kMediaSessionEnterPictureInPicture)) {
base_types->push_back(ContentSettingsType::AUTO_PICTURE_IN_PICTURE);
}
if (base::FeatureList::IsEnabled(blink::features::kSpeakerSelection)) {
base_types->push_back(ContentSettingsType::SPEAKER_SELECTION);
}
if (base::FeatureList::IsEnabled(
features::kCapturedSurfaceControlKillswitch)) {
base_types->push_back(ContentSettingsType::CAPTURED_SURFACE_CONTROL);
}
if (base::FeatureList::IsEnabled(
permissions::features::kKeyboardLockPrompt)) {
base_types->push_back(ContentSettingsType::KEYBOARD_LOCK);
}
#if BUILDFLAG(ENABLE_VR)
if (device::features::IsHandTrackingEnabled()) {
base_types->push_back(ContentSettingsType::HAND_TRACKING);
}
#endif
if (base::FeatureList::IsEnabled(blink::features::kWebAppInstallation)) {
base_types->push_back(ContentSettingsType::WEB_APP_INSTALLATION);
}
if (base::FeatureList::IsEnabled(
network::features::kLocalNetworkAccessChecks)) {
base_types->push_back(ContentSettingsType::LOCAL_NETWORK_ACCESS);
}
initialized = true;
}
// The permission categories below are only shown for certain origins.
std::vector<ContentSettingsType> types_for_origin = *base_types;
if (base::FeatureList::IsEnabled(
features::kAutomaticFullscreenContentSetting) &&
ShouldShowIwaContentSettingForOrigin(
profile, origin, ContentSettingsType::AUTOMATIC_FULLSCREEN)) {
types_for_origin.push_back(ContentSettingsType::AUTOMATIC_FULLSCREEN);
}
#if BUILDFLAG(IS_CHROMEOS)
if (base::FeatureList::IsEnabled(blink::features::kWebPrinting) &&
ShouldShowIwaContentSettingForOrigin(profile, origin,
ContentSettingsType::WEB_PRINTING)) {
types_for_origin.push_back(ContentSettingsType::WEB_PRINTING);
}
#endif
return types_for_origin;
}
std::string SiteSettingSourceToString(const SiteSettingSource source) {
switch (source) {
case SiteSettingSource::kAllowlist:
return "allowlist";
case SiteSettingSource::kAdsFilterBlocklist:
return "ads-filter-blacklist";
case SiteSettingSource::kDefault:
return "default";
case SiteSettingSource::kEmbargo:
return "embargo";
case SiteSettingSource::kExtension:
return "extension";
case SiteSettingSource::kHostedApp:
return "HostedApp";
case SiteSettingSource::kInsecureOrigin:
return "insecure-origin";
case SiteSettingSource::kKillSwitch:
return "kill-switch";
case SiteSettingSource::kPolicy:
return "policy";
case SiteSettingSource::kPreference:
return "preference";
case SiteSettingSource::kNumSources:
NOTREACHED();
}
}
SiteSettingSource ProviderTypeToSiteSettingsSource(
const ProviderType provider_type) {
switch (provider_type) {
case ProviderType::kWebuiAllowlistProvider:
case ProviderType::kComponentExtensionProvider:
return SiteSettingSource::kAllowlist;
case ProviderType::kPolicyProvider:
case ProviderType::kSupervisedProvider:
return SiteSettingSource::kPolicy;
case ProviderType::kCustomExtensionProvider:
return SiteSettingSource::kExtension;
case ProviderType::kInstalledWebappProvider:
return SiteSettingSource::kHostedApp;
case ProviderType::kOneTimePermissionProvider:
case ProviderType::kPrefProvider:
return SiteSettingSource::kPreference;
case ProviderType::kDefaultProvider:
return SiteSettingSource::kDefault;
case ProviderType::kJavascriptOptimizerAndroidProvider:
case ProviderType::kNone:
case ProviderType::kNotificationAndroidProvider:
case ProviderType::kProviderForTests:
case ProviderType::kOtherProviderForTests:
NOTREACHED();
}
}
std::string ProviderToDefaultSettingSourceString(const ProviderType provider) {
switch (provider) {
case ProviderType::kPolicyProvider:
return "policy";
case ProviderType::kSupervisedProvider:
return "supervised_user";
case ProviderType::kCustomExtensionProvider:
return "extension";
case ProviderType::kOneTimePermissionProvider:
case ProviderType::kPrefProvider:
return "preference";
case ProviderType::kInstalledWebappProvider:
case ProviderType::kWebuiAllowlistProvider:
case ProviderType::kComponentExtensionProvider:
case ProviderType::kDefaultProvider:
return "default";
case ProviderType::kJavascriptOptimizerAndroidProvider:
case ProviderType::kNone:
case ProviderType::kNotificationAndroidProvider:
case ProviderType::kProviderForTests:
case ProviderType::kOtherProviderForTests:
NOTREACHED();
}
}
// Add an "Allow"-entry to the list of |exceptions| for a |url_pattern| from
// the web extent of a hosted |app|.
void AddExceptionForHostedApp(const std::string& url_pattern,
const extensions::Extension& app,
base::Value::List* exceptions) {
base::Value::Dict exception;
std::string setting_string =
content_settings::ContentSettingToString(CONTENT_SETTING_ALLOW);
DCHECK(!setting_string.empty());
exception.Set(kSetting, setting_string);
exception.Set(kOrigin, url_pattern);
exception.Set(kDisplayName, url_pattern);
exception.Set(kEmbeddingOrigin, url_pattern);
exception.Set(kSource,
SiteSettingSourceToString(SiteSettingSource::kHostedApp));
exception.Set(kIncognito, false);
exception.Set(kAppName, app.name());
exception.Set(kAppId, app.id());
exceptions->Append(std::move(exception));
}
// Create a base::Value::Dict that will act as a data source for a single row
// for a File System Access permission grant.
base::Value::Dict GetFileSystemExceptionForPage(
ContentSettingsType content_type,
Profile* profile,
const std::string& origin,
const base::FilePath& file_path,
const ContentSetting& setting,
SiteSettingSource source,
bool incognito,
bool is_embargoed) {
base::Value::Dict exception;
exception.Set(kOrigin, origin);
// TODO(crbug.com/40101962): Replace `LossyDisplayName` method with a
// new method that returns the full file path in a human-readable format.
exception.Set(kDisplayName, file_path.LossyDisplayName());
std::string setting_string =
content_settings::ContentSettingToString(setting);
DCHECK(!setting_string.empty());
exception.Set(kSetting, setting_string);
exception.Set(kSource, SiteSettingSourceToString(source));
exception.Set(kIncognito, incognito);
exception.Set(kIsEmbargoed, is_embargoed);
return exception;
}
std::u16string GetExpirationDescription(const base::Time& expiration) {
CHECK(!expiration.is_null());
const base::TimeDelta time_diff =
expiration.LocalMidnight() - base::Time::Now().LocalMidnight();
// Only exceptions that haven't expired should reach this function.
// However, there is an edge case where an exception could expire between
// being fetched and this calculation. So let's always return a valid
// number, zero.
int days = std::max(time_diff.InDays(), 0);
return l10n_util::GetPluralStringFUTF16(IDS_SETTINGS_EXPIRES_AFTER_TIME_LABEL,
days);
}
// Create a base::Value::Dict that will act as a data source for a single row
// in a HostContentSettingsMap-controlled exceptions table (e.g., cookies).
base::Value::Dict GetExceptionForPage(
ContentSettingsType content_type,
Profile* profile,
const ContentSettingsPattern& pattern,
const ContentSettingsPattern& secondary_pattern,
const std::string& display_name,
const ContentSetting& setting,
const SiteSettingSource source,
const base::Time& expiration,
bool incognito,
bool is_embargoed) {
base::Value::Dict exception;
exception.Set(kType, ContentSettingsTypeToGroupName(content_type));
exception.Set(kOrigin, pattern.ToString());
exception.Set(kDisplayName, display_name);
exception.Set(kEmbeddingOrigin,
secondary_pattern == ContentSettingsPattern::Wildcard()
? std::string()
: secondary_pattern.ToString());
std::string setting_string =
content_settings::ContentSettingToString(setting);
DCHECK(!setting_string.empty());
exception.Set(kSetting, setting_string);
// Cookie exception types may have an expiration that should be shown.
if ((content_type == ContentSettingsType::COOKIES ||
content_type == ContentSettingsType::TRACKING_PROTECTION) &&
!expiration.is_null() && !incognito) {
exception.Set(kDescription, GetExpirationDescription(expiration));
}
exception.Set(kSource, SiteSettingSourceToString(source));
exception.Set(kIncognito, incognito);
exception.Set(kIsEmbargoed, is_embargoed);
return exception;
}
std::u16string GetStorageAccessEmbeddingDescription(
StorageAccessEmbeddingException embedding_sa_exception) {
if (embedding_sa_exception.is_embargoed) {
return l10n_util::GetStringUTF16(
IDS_PAGE_INFO_PERMISSION_AUTOMATICALLY_BLOCKED);
}
if (embedding_sa_exception.expiration.is_null()) {
return std::u16string();
}
return GetExpirationDescription(embedding_sa_exception.expiration);
}
// If the given `pattern` represents an individual origin, Isolated Web App, or
// extension, retrieve a string to display it as such. If not, return the
// pattern without wildcards as a string.
std::string GetStorageAccessDisplayNameForPattern(
Profile* profile,
ContentSettingsPattern pattern) {
GURL url(pattern.ToString());
if (url.is_valid() && (url.SchemeIs(extensions::kExtensionScheme) ||
url.SchemeIs(chrome::kIsolatedAppScheme))) {
return GetDisplayNameForGURL(profile, url, /*hostname_only=*/false);
}
GURL url2 = pattern.ToRepresentativeUrl();
if (url2.is_valid()) {
return base::UTF16ToUTF8(FormatUrlForSecurityDisplay(
url2, url_formatter::SchemeDisplay::OMIT_CRYPTOGRAPHIC));
}
return pattern.ToString();
}
base::Value::Dict GetStorageAccessExceptionForPage(
Profile* profile,
const ContentSettingsPattern& pattern,
const std::string& display_name,
ContentSetting setting,
const std::vector<StorageAccessEmbeddingException>& exceptions) {
CHECK(!exceptions.empty());
base::Value::Dict exception;
exception.Set(kOrigin, pattern.ToString());
exception.Set(kDisplayName, display_name);
std::string setting_string =
content_settings::ContentSettingToString(setting);
DCHECK(!setting_string.empty());
exception.Set(kSetting, setting_string);
// If there is only one exception and that exception applies everywhere,
// i.e. `secondary_pattern` is empty, then don't return exceptions and a
// static row should be displayed. In practice, this only applies to embargoed
// sites.
if (exceptions.size() == 1 &&
exceptions[0].secondary_pattern == ContentSettingsPattern::Wildcard()) {
auto& embedding_sa_exception = exceptions[0];
std::u16string description =
GetStorageAccessEmbeddingDescription(embedding_sa_exception);
if (!description.empty()) {
exception.Set(kDescription, description);
}
exception.Set(kIncognito, embedding_sa_exception.is_incognito);
exception.Set(kExceptions, base::Value::List());
return exception;
}
exception.Set(kCloseDescription,
l10n_util::GetPluralStringFUTF16(IDS_DEL_SITE_SETTINGS_COUNTER,
exceptions.size()));
const int open_description_id =
(setting == ContentSetting::CONTENT_SETTING_ALLOW)
? IDS_SETTINGS_STORAGE_ACCESS_ALLOWED_SITE_LABEL
: IDS_SETTINGS_STORAGE_ACCESS_BLOCKED_SITE_LABEL;
exception.Set(kOpenDescription,
l10n_util::GetStringUTF16(open_description_id));
base::Value::List embedding_origins;
for (auto& embedding_sa_exception : exceptions) {
ContentSettingsPattern secondary_pattern =
embedding_sa_exception.secondary_pattern;
base::Value::Dict embedding_exception;
embedding_exception.Set(
kEmbeddingOrigin,
secondary_pattern == ContentSettingsPattern::Wildcard()
? std::string()
: secondary_pattern.ToString());
embedding_exception.Set(
kEmbeddingDisplayName,
GetStorageAccessDisplayNameForPattern(profile, secondary_pattern));
std::u16string description =
GetStorageAccessEmbeddingDescription(embedding_sa_exception);
if (!description.empty()) {
embedding_exception.Set(kDescription, description);
}
embedding_exception.Set(kIncognito, embedding_sa_exception.is_incognito);
embedding_origins.Append(std::move(embedding_exception));
}
exception.Set(kExceptions, std::move(embedding_origins));
return exception;
}
UrlIdentity GetUrlIdentityForGURL(Profile* profile,
const GURL& url,
bool hostname_only) {
auto origin = url::Origin::Create(url);
if (origin.opaque()) {
return {.type = UrlIdentity::Type::kDefault,
.name = base::UTF8ToUTF16(url.spec())};
}
return UrlIdentity::CreateFromUrl(
profile, origin.GetURL(), kUrlIdentityAllowedTypes,
hostname_only ? kUrlIdentityOptionsHostOnly
: kUrlIdentityOptionsOmitHttps);
}
std::string GetDisplayNameForGURL(Profile* profile,
const GURL& url,
bool hostname_only) {
return base::UTF16ToUTF8(
GetUrlIdentityForGURL(profile, url, hostname_only).name);
}
using RawPatternSettings =
std::map<std::pair<ContentSettingsPattern, ProviderType>,
OnePatternSettings,
std::greater<>>;
// Fills in `all_patterns_settings` with site exceptions information for the
// given `type` from `profile`.
void GetRawExceptionsForContentSettingsType(
ContentSettingsType type,
Profile* profile,
content::WebUI* web_ui,
RawPatternSettings& all_patterns_settings) {
HostContentSettingsMap* map =
HostContentSettingsMapFactory::GetForProfile(profile);
for (const auto& setting : map->GetSettingsForOneType(type)) {
// Don't add default settings.
if (setting.primary_pattern == ContentSettingsPattern::Wildcard() &&
setting.secondary_pattern == ContentSettingsPattern::Wildcard() &&
setting.source != ProviderType::kPrefProvider) {
continue;
}
// Off-the-record HostContentSettingsMap contains incognito content settings
// as well as normal content settings. Here, we use the incognito settings
// only, excluding policy-source exceptions as policies cannot specify
// incognito-only exceptions, meaning these are necesssarily duplicates.
if (map->IsOffTheRecord() &&
(!setting.incognito ||
setting.source == ProviderType::kPolicyProvider)) {
continue;
}
// Don't add allowlisted settings.
if (IsFromWebUIAllowlistSource(setting)) {
continue;
}
// Don't add auto-granted permissions for storage access exceptions.
if (setting.metadata.decided_by_related_website_sets() &&
!base::FeatureList::IsEnabled(
permissions::features::kShowRelatedWebsiteSetsPermissionGrants)) {
continue;
}
auto content_setting = setting.GetContentSetting();
// There is no user-facing concept of SESSION_ONLY cookie exceptions that
// use secondary patterns. These are instead presented as ALLOW.
// TODO(crbug.com/40251893): Perform a one time migration of the actual
// content settings when the extension API no-longer allows them to be
// created.
if (type == ContentSettingsType::COOKIES &&
content_setting == ContentSetting::CONTENT_SETTING_SESSION_ONLY &&
setting.secondary_pattern != ContentSettingsPattern::Wildcard()) {
content_setting = ContentSetting::CONTENT_SETTING_ALLOW;
}
all_patterns_settings[{setting.primary_pattern, setting.source}][{
setting.secondary_pattern, setting.incognito}] = {
content_setting, /*is_embargoed=*/false, setting.metadata.expiration()};
}
permissions::PermissionDecisionAutoBlocker* auto_blocker =
permissions::PermissionsClient::Get()->GetPermissionDecisionAutoBlocker(
profile);
for (const auto& setting : map->GetSettingsForOneType(
ContentSettingsType::PERMISSION_AUTOBLOCKER_DATA)) {
// Off-the-record HostContentSettingsMap contains incognito content
// settings as well as normal content settings. Here, we use the
// incognito settings only.
if (map->IsOffTheRecord() && !setting.incognito) {
continue;
}
if (!permissions::PermissionDecisionAutoBlocker::IsEnabledForContentSetting(
type)) {
continue;
}
if (auto_blocker->IsEmbargoed(GURL(setting.primary_pattern.ToString()),
type)) {
all_patterns_settings[{setting.primary_pattern, setting.source}]
[{setting.secondary_pattern, setting.incognito}] = {
CONTENT_SETTING_BLOCK, /*is_embargoed=*/true,
setting.metadata.expiration()};
}
}
}
void GetExceptionsForContentType(ContentSettingsType type,
Profile* profile,
content::WebUI* web_ui,
bool incognito,
base::Value::List* exceptions) {
// Group settings by primary_pattern.
RawPatternSettings all_patterns_settings;
GetRawExceptionsForContentSettingsType(type, profile, web_ui,
all_patterns_settings);
// Keep the exceptions sorted by provider so they will be displayed in
// precedence order.
std::map<ProviderType, std::vector<base::Value::Dict>>
all_provider_exceptions;
for (const auto& [primary_pattern_and_source, one_settings] :
all_patterns_settings) {
const auto& [primary_pattern, source] = primary_pattern_and_source;
const std::string display_name =
GetDisplayNameForPattern(profile, primary_pattern);
auto& this_provider_exceptions = all_provider_exceptions[source];
for (const auto& secondary_setting : one_settings) {
const SiteExceptionInfo& site_exception_info = secondary_setting.second;
const auto& [secondary_pattern, is_incognito] = secondary_setting.first;
this_provider_exceptions.push_back(
GetExceptionForPage(type, profile, primary_pattern, secondary_pattern,
display_name, site_exception_info.content_setting,
ProviderTypeToSiteSettingsSource(source),
site_exception_info.expiration, is_incognito,
site_exception_info.is_embargoed));
}
}
// For camera and microphone, we do not have policy exceptions, but we do have
// the policy-set allowed URLs, which should be displayed in the same manner.
if (type == ContentSettingsType::MEDIASTREAM_MIC ||
type == ContentSettingsType::MEDIASTREAM_CAMERA) {
auto& policy_exceptions =
all_provider_exceptions[ProviderType::kPolicyProvider];
DCHECK(policy_exceptions.empty());
GetPolicyAllowedUrls(type, &policy_exceptions, web_ui, incognito);
}
// Display the URLs with File System entries that are granted
// permissions via File System Access Persistent Permissions.
if (base::FeatureList::IsEnabled(
features::kFileSystemAccessPersistentPermissions) &&
(type == ContentSettingsType::FILE_SYSTEM_READ_GUARD ||
type == ContentSettingsType::FILE_SYSTEM_WRITE_GUARD)) {
auto& urls_with_granted_entries =
all_provider_exceptions[ProviderType::kDefaultProvider];
GetFileSystemGrantedEntries(&urls_with_granted_entries, profile, incognito);
}
for (auto& one_provider_exceptions : all_provider_exceptions) {
for (auto& exception : one_provider_exceptions.second) {
exceptions->Append(std::move(exception));
}
}
}
void GetStorageAccessExceptions(ContentSetting content_setting,
Profile* profile,
Profile* incognito_profile,
content::WebUI* web_ui,
base::Value::List* exceptions) {
ContentSettingsType type = ContentSettingsType::STORAGE_ACCESS;
// Group settings by primary_pattern.
RawPatternSettings all_patterns_settings;
GetRawExceptionsForContentSettingsType(type, profile, web_ui,
all_patterns_settings);
if (incognito_profile) {
GetRawExceptionsForContentSettingsType(type, incognito_profile, web_ui,
all_patterns_settings);
}
for (const auto& [primary_pattern_and_source, one_settings] :
all_patterns_settings) {
const auto& [primary_pattern, source] = primary_pattern_and_source;
std::vector<StorageAccessEmbeddingException> sa_exceptions;
for (const auto& secondary_setting : one_settings) {
const SiteExceptionInfo& site_exception_info = secondary_setting.second;
const auto& [secondary_pattern, is_incognito] = secondary_setting.first;
if (site_exception_info.content_setting != content_setting) {
continue;
}
sa_exceptions.push_back({secondary_pattern, is_incognito,
site_exception_info.is_embargoed,
site_exception_info.expiration});
}
if (sa_exceptions.empty()) {
continue;
}
// TODO(http://b/289788055): Remove wildcards.
const std::string display_name =
GetStorageAccessDisplayNameForPattern(profile, primary_pattern);
exceptions->Append(GetStorageAccessExceptionForPage(
profile, primary_pattern, std::move(display_name), content_setting,
sa_exceptions));
}
}
void GetContentCategorySetting(const HostContentSettingsMap* map,
ContentSettingsType content_type,
base::Value::Dict* object) {
auto provider = ProviderType::kDefaultProvider;
std::string setting = content_settings::ContentSettingToString(
map->GetDefaultContentSetting(content_type, &provider));
DCHECK(!setting.empty());
object->Set(kSetting, setting);
if (provider != ProviderType::kDefaultProvider) {
object->Set(kSource, ProviderToDefaultSettingSourceString(provider));
}
}
ContentSetting GetContentSettingForOrigin(Profile* profile,
const HostContentSettingsMap* map,
const GURL& origin,
ContentSettingsType content_type,
SiteSettingSource* source) {
// TODO(patricialor): In future, PermissionManager should know about all
// content settings, not just the permissions, plus all the possible sources,
// and the calls to HostContentSettingsMap should be removed.
content_settings::SettingInfo info;
ContentSetting setting =
map->GetContentSetting(origin, origin, content_type, &info);
// Retrieve the content setting.
content::PermissionResult result(
permissions::PermissionUtil::ContentSettingToPermissionStatus(setting),
content::PermissionStatusSource::UNSPECIFIED);
if (permissions::PermissionDecisionAutoBlocker::IsEnabledForContentSetting(
content_type)) {
if (permissions::PermissionUtil::IsPermission(content_type)) {
result = profile->GetPermissionController()
->GetPermissionResultForOriginWithoutContext(
content::PermissionDescriptorUtil::
CreatePermissionDescriptorForPermissionType(
permissions::PermissionUtil::
ContentSettingsTypeToPermissionType(
content_type)),
url::Origin::Create(origin));
} else {
permissions::PermissionDecisionAutoBlocker* auto_blocker =
permissions::PermissionsClient::Get()
->GetPermissionDecisionAutoBlocker(profile);
std::optional<content::PermissionResult> embargo_result =
auto_blocker->GetEmbargoResult(origin, content_type);
if (embargo_result) {
result = embargo_result.value();
}
}
}
// Retrieve the source of the content setting.
*source =
CalculateSiteSettingSource(profile, content_type, origin, info, result);
if (info.metadata.session_model() ==
content_settings::mojom::SessionModel::ONE_TIME) {
DCHECK(
permissions::PermissionUtil::DoesSupportTemporaryGrants(content_type));
DCHECK_EQ(result.status, PermissionStatus::GRANTED);
return CONTENT_SETTING_DEFAULT;
}
return permissions::PermissionUtil::PermissionStatusToContentSetting(
result.status);
}
std::vector<ContentSettingPatternSource>
GetSingleOriginExceptionsForContentType(HostContentSettingsMap* map,
ContentSettingsType content_type) {
ContentSettingsForOneType entries = map->GetSettingsForOneType(content_type);
// Exclude any entries that are allowlisted or don't represent a single
// top-frame origin.
std::erase_if(entries, [](const ContentSettingPatternSource& e) {
return !content_settings::PatternAppliesToSingleOrigin(
e.primary_pattern, e.secondary_pattern) ||
IsFromWebUIAllowlistSource(e);
});
return entries;
}
void GetFileSystemGrantedEntries(std::vector<base::Value::Dict>* exceptions,
Profile* profile,
bool incognito) {
ChromeFileSystemAccessPermissionContext* permission_context =
FileSystemAccessPermissionContextFactory::GetForProfile(profile);
std::vector<std::unique_ptr<permissions::ObjectPermissionContextBase::Object>>
grants = permission_context->GetAllGrantedObjects();
for (const auto& grant : grants) {
const std::string url = grant->origin.spec();
auto* const optional_path = grant->value.Find(
ChromeFileSystemAccessPermissionContext::kPermissionPathKey);
// Ensure that the file path is found for the given kPermissionPathKey.
if (optional_path) {
const base::FilePath file_path =
base::ValueToFilePath(optional_path).value();
exceptions->push_back(GetFileSystemExceptionForPage(
ContentSettingsType::FILE_SYSTEM_WRITE_GUARD, profile, url, file_path,
CONTENT_SETTING_ALLOW, SiteSettingSource::kDefault, incognito));
}
}
// Sort exceptions by origin name, alphabetically.
std::ranges::sort(*exceptions, [](const base::Value::Dict& lhs,
const base::Value::Dict& rhs) {
return lhs.Find(kOrigin)->GetString() < rhs.Find(kOrigin)->GetString();
});
}
const ChooserTypeNameEntry* ChooserTypeFromGroupName(std::string_view name) {
for (const auto& chooser_type : kChooserTypeGroupNames) {
if (chooser_type.name == name) {
return &chooser_type;
}
}
return nullptr;
}
// Create a base::Value::Dict that will act as a data source for a single row
// in a chooser permission exceptions table. The chooser permission will contain
// a list of site exceptions that correspond to the exception.
base::Value::Dict CreateChooserExceptionObject(
const std::u16string& display_name,
const base::Value& object,
const std::string& chooser_type,
const ChooserExceptionDetails& chooser_exception_details,
Profile* profile) {
base::Value::Dict exception;
std::string setting_string =
content_settings::ContentSettingToString(CONTENT_SETTING_DEFAULT);
DCHECK(!setting_string.empty());
exception.Set(kDisplayName, display_name);
exception.Set(kObject, object.Clone());
exception.Set(kChooserType, chooser_type);
// Order the sites by the provider precedence order.
std::map<SiteSettingSource, std::vector<base::Value::Dict>>
all_provider_sites;
for (const auto& details : chooser_exception_details) {
const GURL& origin = std::get<0>(details);
const SiteSettingSource source = std::get<1>(details);
const bool incognito = std::get<2>(details);
std::string site_display_name = base::UTF16ToUTF8(
UrlIdentity::CreateFromUrl(profile, origin, kUrlIdentityAllowedTypes,
kUrlIdentityOptionsRawSpec)
.name);
auto& this_provider_sites = all_provider_sites[source];
base::Value::Dict site;
site.Set(kOrigin, origin.spec());
site.Set(kDisplayName, site_display_name);
site.Set(kSetting, setting_string);
site.Set(kSource, SiteSettingSourceToString(source));
site.Set(kIncognito, incognito);
this_provider_sites.push_back(std::move(site));
}
base::Value::List sites;
for (auto& one_provider_sites : all_provider_sites) {
for (auto& site : one_provider_sites.second) {
sites.Append(std::move(site));
}
}
exception.Set(kSites, std::move(sites));
return exception;
}
base::Value::List GetChooserExceptionListFromProfile(
Profile* profile,
const ChooserTypeNameEntry& chooser_type) {
base::Value::List exceptions;
ContentSettingsType content_type =
ContentSettingsTypeFromGroupName(std::string(chooser_type.name));
DCHECK(content_type != ContentSettingsType::DEFAULT);
// The BluetoothChooserContext is only available when the
// WebBluetoothNewPermissionsBackend flag is enabled.
// TODO(crbug.com/40458188): Remove the nullptr check when it is enabled
// by default.
permissions::ObjectPermissionContextBase* chooser_context =
chooser_type.get_context(profile);
if (!chooser_context) {
return exceptions;
}
std::vector<std::unique_ptr<permissions::ObjectPermissionContextBase::Object>>
objects = chooser_context->GetAllGrantedObjects();
if (profile->HasPrimaryOTRProfile()) {
Profile* incognito_profile =
profile->GetPrimaryOTRProfile(/*create_if_needed=*/true);
permissions::ObjectPermissionContextBase* incognito_chooser_context =
chooser_type.get_context(incognito_profile);
std::vector<
std::unique_ptr<permissions::ObjectPermissionContextBase::Object>>
incognito_objects = incognito_chooser_context->GetAllGrantedObjects();
objects.insert(objects.end(),
std::make_move_iterator(incognito_objects.begin()),
std::make_move_iterator(incognito_objects.end()));
}
// Maps from a chooser exception name/object pair to a
// ChooserExceptionDetails. This will group and sort the exceptions by the UI
// string and object for display.
std::map<std::pair<std::u16string, base::Value>, ChooserExceptionDetails>
all_chooser_objects;
for (const auto& object : objects) {
// Don't include WebUI settings.
if (content::HasWebUIScheme(object->origin)) {
continue;
}
std::u16string name = chooser_context->GetObjectDisplayName(object->value);
auto& chooser_exception_details = all_chooser_objects[std::make_pair(
name, base::Value(object->value.Clone()))];
SiteSettingSource source =
GetSourceForChooserException(profile, content_type, object->source);
chooser_exception_details.insert(
{object->origin, source, object->incognito});
}
for (const auto& all_chooser_objects_entry : all_chooser_objects) {
const std::u16string& name = all_chooser_objects_entry.first.first;
const base::Value& object = all_chooser_objects_entry.first.second;
const ChooserExceptionDetails& chooser_exception_details =
all_chooser_objects_entry.second;
exceptions.Append(CreateChooserExceptionObject(
name, object, chooser_type.name, chooser_exception_details, profile));
}
return exceptions;
}
std::vector<web_app::IsolatedWebAppUrlInfo> GetInstalledIsolatedWebApps(
Profile* profile) {
auto* web_app_provider = web_app::WebAppProvider::GetForWebApps(profile);
if (!web_app_provider) {
return {};
}
std::vector<web_app::IsolatedWebAppUrlInfo> iwas;
web_app::WebAppRegistrar& registrar = web_app_provider->registrar_unsafe();
for (const web_app::WebApp& web_app : registrar.GetApps()) {
if (!registrar.IsIsolated(web_app.app_id())) {
continue;
}
base::expected<web_app::IsolatedWebAppUrlInfo, std::string> url_info =
web_app::IsolatedWebAppUrlInfo::Create(web_app.scope());
if (url_info.has_value()) {
iwas.push_back(*url_info);
}
}
return iwas;
}
} // namespace site_settings
|