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
|
/*
* Copyright (C) 2011 Igalia S.L.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include "WebKitWebContext.h"
#include "APIAutomationClient.h"
#include "APIInjectedBundleClient.h"
#include "APIPageConfiguration.h"
#include "APIProcessPoolConfiguration.h"
#include "APIString.h"
#include "LegacyGlobalSettings.h"
#include "NetworkProcessMessages.h"
#include "TextChecker.h"
#include "TextCheckerState.h"
#include "WebAutomationSession.h"
#include "WebKitAutomationSessionPrivate.h"
#include "WebKitDownloadPrivate.h"
#include "WebKitGeolocationManagerPrivate.h"
#include "WebKitInitialize.h"
#include "WebKitInjectedBundleClient.h"
#include "WebKitMemoryPressureSettings.h"
#include "WebKitMemoryPressureSettingsPrivate.h"
#include "WebKitNotificationProvider.h"
#include "WebKitPrivate.h"
#include "WebKitProtocolHandler.h"
#include "WebKitSecurityManagerPrivate.h"
#include "WebKitSecurityOriginPrivate.h"
#include "WebKitSettingsPrivate.h"
#include "WebKitURISchemeRequestPrivate.h"
#include "WebKitUserContentManagerPrivate.h"
#include "WebKitUserMessagePrivate.h"
#include "WebKitWebContextPrivate.h"
#include "WebKitWebViewPrivate.h"
#include "WebKitWebsiteDataManagerPrivate.h"
#include "WebKitWebsitePoliciesPrivate.h"
#include "WebNotificationManagerProxy.h"
#include "WebProcessMessages.h"
#include "WebURLSchemeHandler.h"
#include "WebsiteDataStore.h"
#include "WebsiteDataType.h"
#include <JavaScriptCore/RemoteInspector.h>
#include <WebCore/ContentSecurityPolicy.h>
#include <WebCore/ResourceLoaderIdentifier.h>
#include <cstdlib>
#include <glib/gi18n-lib.h>
#include <libintl.h>
#include <memory>
#include <pal/HysteresisActivity.h>
#include <wtf/DateMath.h>
#include <wtf/FileSystem.h>
#include <wtf/HashMap.h>
#include <wtf/HashSet.h>
#include <wtf/NeverDestroyed.h>
#include <wtf/RefCounted.h>
#include <wtf/RefPtr.h>
#include <wtf/URLParser.h>
#include <wtf/glib/GRefPtr.h>
#include <wtf/glib/GUniquePtr.h>
#include <wtf/glib/WTFGType.h>
#include <wtf/text/CString.h>
#if PLATFORM(GTK)
#include "WebKitRemoteInspectorProtocolHandler.h"
#endif
#if ENABLE(2022_GLIB_API)
#include "WebKitNetworkSession.h"
#endif
#if !ENABLE(2022_GLIB_API)
#include "WebKitFaviconDatabasePrivate.h"
#endif
using namespace WebKit;
/**
* WebKitWebContext:
*
* Manages aspects common to all #WebKitWebView<!-- -->s
*
* The #WebKitWebContext manages all aspects common to all
* #WebKitWebView<!-- -->s.
*
* You can define the #WebKitCacheModel with
* webkit_web_context_set_cache_model(), depending on the needs of
* your application. You can access the #WebKitSecurityManager to specify
* the behaviour of your application regarding security using
* webkit_web_context_get_security_manager().
*
* It is also possible to change your preferred language or enable
* spell checking, using webkit_web_context_set_preferred_languages(),
* webkit_web_context_set_spell_checking_languages() and
* webkit_web_context_set_spell_checking_enabled().
*
* You can use webkit_web_context_register_uri_scheme() to register
* custom URI schemes, and manage several other settings.
*
* TLS certificate validation failure is now treated as a transport
* error by default. To handle TLS failures differently, you can
* connect to #WebKitWebView::load-failed-with-tls-errors.
* Alternatively, you can use webkit_web_context_set_tls_errors_policy()
* to set the policy %WEBKIT_TLS_ERRORS_POLICY_IGNORE; however, this is
* not appropriate for Internet applications.
*/
enum {
PROP_0,
#if PLATFORM(GTK) && !USE(GTK4)
PROP_LOCAL_STORAGE_DIRECTORY,
#endif
#if !ENABLE(2022_GLIB_API)
PROP_WEBSITE_DATA_MANAGER,
#endif
#if PLATFORM(GTK) && !USE(GTK4)
PROP_PSON_ENABLED,
PROP_USE_SYSTEM_APPEARANCE_FOR_SCROLLBARS,
#endif
PROP_MEMORY_PRESSURE_SETTINGS,
PROP_TIME_ZONE_OVERRIDE,
N_PROPERTIES,
};
static GParamSpec* sObjProperties[N_PROPERTIES] = { nullptr, };
enum {
#if !ENABLE(2022_GLIB_API)
DOWNLOAD_STARTED,
#endif
INITIALIZE_WEB_PROCESS_EXTENSIONS,
INITIALIZE_NOTIFICATION_PERMISSIONS,
AUTOMATION_STARTED,
USER_MESSAGE_RECEIVED,
LAST_SIGNAL
};
class WebKitURISchemeHandler final : public WebURLSchemeHandler {
public:
static Ref<WebKitURISchemeHandler> create(WebKitWebContext* context, WebKitURISchemeRequestCallback callback, void* userData, GDestroyNotify destroyNotify)
{
return adoptRef(*new WebKitURISchemeHandler(context, callback, userData, destroyNotify));
}
~WebKitURISchemeHandler()
{
if (m_destroyNotify)
m_destroyNotify(m_userData);
}
private:
WebKitURISchemeHandler(WebKitWebContext* context, WebKitURISchemeRequestCallback callback, void* userData, GDestroyNotify destroyNotify)
: m_context(context)
, m_callback(callback)
, m_userData(userData)
, m_destroyNotify(destroyNotify)
{
}
void platformStartTask(WebPageProxy& page, WebURLSchemeTask& task) final
{
if (!m_callback)
return;
GRefPtr<WebKitURISchemeRequest> request = adoptGRef(webkitURISchemeRequestCreate(m_context, page, task));
auto addResult = m_requests.add({ task.resourceLoaderID(), task.pageProxyID() }, WTFMove(request));
ASSERT(addResult.isNewEntry);
m_callback(addResult.iterator->value.get(), m_userData);
}
void platformStopTask(WebPageProxy&, WebURLSchemeTask& task) final
{
auto it = m_requests.find({ task.resourceLoaderID(), task.pageProxyID() });
if (it == m_requests.end())
return;
webkitURISchemeRequestCancel(it->value.get());
m_requests.remove(it);
}
void platformTaskCompleted(WebURLSchemeTask& task) final
{
m_requests.remove({ task.resourceLoaderID(), task.pageProxyID() });
}
WebKitWebContext* m_context { nullptr };
WebKitURISchemeRequestCallback m_callback { nullptr };
void* m_userData { nullptr };
GDestroyNotify m_destroyNotify { nullptr };
HashMap<std::pair<WebCore::ResourceLoaderIdentifier, WebPageProxyIdentifier>, GRefPtr<WebKitURISchemeRequest>> m_requests;
};
typedef HashMap<String, RefPtr<WebKitURISchemeHandler> > URISchemeHandlerMap;
class WebKitAutomationClient;
struct _WebKitWebContextPrivate {
#if !ENABLE(2022_GLIB_API)
_WebKitWebContextPrivate()
: dnsPrefetchHystereris([this](PAL::HysteresisState state) { if (state == PAL::HysteresisState::Stopped) dnsPrefetchedHosts.clear(); })
{
}
#endif
RefPtr<WebProcessPool> processPool;
bool clientsDetached;
#if PLATFORM(GTK) && !USE(GTK4)
bool psonEnabled;
bool useSystemAppearanceForScrollbars;
#endif
#if !ENABLE(2022_GLIB_API)
GRefPtr<WebKitFaviconDatabase> faviconDatabase;
CString faviconDatabaseDirectory;
#endif
GRefPtr<WebKitSecurityManager> securityManager;
URISchemeHandlerMap uriSchemeHandlers;
GRefPtr<WebKitGeolocationManager> geolocationManager;
std::unique_ptr<WebKitNotificationProvider> notificationProvider;
#if !ENABLE(2022_GLIB_API)
GRefPtr<WebKitWebsiteDataManager> websiteDataManager;
#endif
HashMap<WebPageProxyIdentifier, WebKitWebView*> webViews;
CString webProcessExtensionsDirectory;
GRefPtr<GVariant> webProcessExtensionsInitializationUserData;
CString localStorageDirectory;
#if ENABLE(REMOTE_INSPECTOR)
#if PLATFORM(GTK)
std::unique_ptr<RemoteInspectorProtocolHandler> remoteInspectorProtocolHandler;
#endif
std::unique_ptr<WebKitAutomationClient> automationClient;
GRefPtr<WebKitAutomationSession> automationSession;
#if ENABLE(2022_GLIB_API)
GRefPtr<WebKitNetworkSession> automationNetworkSession;
#endif
#endif
std::unique_ptr<WebKitProtocolHandler> webkitProtocolHandler;
#if !ENABLE(2022_GLIB_API)
HashSet<String> dnsPrefetchedHosts;
PAL::HysteresisActivity dnsPrefetchHystereris;
#endif
WebKitMemoryPressureSettings* memoryPressureSettings;
CString timeZoneOverride;
};
static guint signals[LAST_SIGNAL] = { 0, };
WEBKIT_DEFINE_FINAL_TYPE(WebKitWebContext, webkit_web_context, G_TYPE_OBJECT, GObject)
std::unique_ptr<WebKitNotificationProvider> s_serviceWorkerNotificationProvider;
#if ENABLE(REMOTE_INSPECTOR)
class WebKitAutomationClient final : Inspector::RemoteInspector::Client {
WTF_MAKE_FAST_ALLOCATED;
public:
explicit WebKitAutomationClient(WebKitWebContext* context)
: m_webContext(context)
{
Inspector::RemoteInspector::singleton().setClient(this);
}
~WebKitAutomationClient()
{
Inspector::RemoteInspector::singleton().setClient(nullptr);
}
private:
bool remoteAutomationAllowed() const override { return true; }
String browserName() const override
{
if (!m_webContext->priv->automationSession)
return { };
return webkitAutomationSessionGetBrowserName(m_webContext->priv->automationSession.get());
}
String browserVersion() const override
{
if (!m_webContext->priv->automationSession)
return { };
return webkitAutomationSessionGetBrowserVersion(m_webContext->priv->automationSession.get());
}
void requestAutomationSession(const String& sessionIdentifier, const Inspector::RemoteInspector::Client::SessionCapabilities& capabilities) override
{
ASSERT(!m_webContext->priv->automationSession);
m_webContext->priv->automationSession = adoptGRef(webkitAutomationSessionCreate(m_webContext, sessionIdentifier.utf8().data(), capabilities));
g_signal_emit(m_webContext, signals[AUTOMATION_STARTED], 0, m_webContext->priv->automationSession.get());
m_webContext->priv->processPool->setAutomationSession(&webkitAutomationSessionGetSession(m_webContext->priv->automationSession.get()));
}
WebKitWebContext* m_webContext;
};
void webkitWebContextWillCloseAutomationSession(WebKitWebContext* webContext)
{
webContext->priv->processPool->setAutomationSession(nullptr);
webContext->priv->automationSession = nullptr;
}
#endif
#if PLATFORM(GTK)
#define INJECTED_BUNDLE_FILENAME "libwebkit" WEBKITGTK_API_INFIX "gtkinjectedbundle.so"
#elif PLATFORM(WPE)
#define INJECTED_BUNDLE_FILENAME "libWPEInjectedBundle.so"
#endif
static const char* injectedBundleDirectory()
{
#if ENABLE(DEVELOPER_MODE)
const char* bundleDirectory = g_getenv("WEBKIT_INJECTED_BUNDLE_PATH");
if (bundleDirectory && g_file_test(bundleDirectory, G_FILE_TEST_IS_DIR))
return bundleDirectory;
#endif
static const char* injectedBundlePath = PKGLIBDIR G_DIR_SEPARATOR_S "injected-bundle" G_DIR_SEPARATOR_S;
return injectedBundlePath;
}
static void webkitWebContextGetProperty(GObject* object, guint propID, GValue* value, GParamSpec* paramSpec)
{
WebKitWebContext* context = WEBKIT_WEB_CONTEXT(object);
switch (propID) {
#if PLATFORM(GTK) && !USE(GTK4)
case PROP_LOCAL_STORAGE_DIRECTORY:
g_value_set_string(value, context->priv->localStorageDirectory.data());
break;
#endif
#if !ENABLE(2022_GLIB_API)
case PROP_WEBSITE_DATA_MANAGER:
g_value_set_object(value, webkit_web_context_get_website_data_manager(context));
break;
#endif
#if PLATFORM(GTK) && !USE(GTK4)
case PROP_PSON_ENABLED:
g_value_set_boolean(value, context->priv->psonEnabled);
break;
case PROP_USE_SYSTEM_APPEARANCE_FOR_SCROLLBARS:
g_value_set_boolean(value, webkit_web_context_get_use_system_appearance_for_scrollbars(context));
break;
#endif
case PROP_TIME_ZONE_OVERRIDE:
g_value_set_string(value, webkit_web_context_get_time_zone_override(context));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propID, paramSpec);
}
}
static void webkitWebContextSetProperty(GObject* object, guint propID, const GValue* value, GParamSpec* paramSpec)
{
WebKitWebContext* context = WEBKIT_WEB_CONTEXT(object);
switch (propID) {
#if PLATFORM(GTK) && !USE(GTK4)
case PROP_LOCAL_STORAGE_DIRECTORY:
context->priv->localStorageDirectory = g_value_get_string(value);
break;
#endif
#if !ENABLE(2022_GLIB_API)
case PROP_WEBSITE_DATA_MANAGER: {
gpointer manager = g_value_get_object(value);
context->priv->websiteDataManager = manager ? WEBKIT_WEBSITE_DATA_MANAGER(manager) : nullptr;
break;
}
#endif
#if PLATFORM(GTK) && !USE(GTK4)
case PROP_PSON_ENABLED:
context->priv->psonEnabled = g_value_get_boolean(value);
break;
case PROP_USE_SYSTEM_APPEARANCE_FOR_SCROLLBARS:
webkit_web_context_set_use_system_appearance_for_scrollbars(context, g_value_get_boolean(value));
break;
#endif
case PROP_MEMORY_PRESSURE_SETTINGS: {
gpointer settings = g_value_get_boxed(value);
context->priv->memoryPressureSettings = settings ? webkit_memory_pressure_settings_copy(static_cast<WebKitMemoryPressureSettings*>(settings)) : nullptr;
break;
}
case PROP_TIME_ZONE_OVERRIDE: {
const auto* timeZone = g_value_get_string(value);
if (isTimeZoneValid(StringView::fromLatin1(timeZone)))
context->priv->timeZoneOverride = timeZone;
break;
}
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propID, paramSpec);
}
}
static void webkitWebContextConstructed(GObject* object)
{
G_OBJECT_CLASS(webkit_web_context_parent_class)->constructed(object);
GUniquePtr<char> bundleFilename(g_build_filename(injectedBundleDirectory(), INJECTED_BUNDLE_FILENAME, nullptr));
WebKitWebContext* webContext = WEBKIT_WEB_CONTEXT(object);
WebKitWebContextPrivate* priv = webContext->priv;
API::ProcessPoolConfiguration configuration;
configuration.setInjectedBundlePath(FileSystem::stringFromFileSystemRepresentation(bundleFilename.get()));
configuration.setUsesWebProcessCache(true);
#if PLATFORM(GTK) && !USE(GTK4)
configuration.setProcessSwapsOnNavigation(priv->psonEnabled);
configuration.setUseSystemAppearanceForScrollbars(priv->useSystemAppearanceForScrollbars);
#else
configuration.setProcessSwapsOnNavigation(true);
#endif
if (priv->memoryPressureSettings) {
configuration.setMemoryPressureHandlerConfiguration(webkitMemoryPressureSettingsGetMemoryPressureHandlerConfiguration(priv->memoryPressureSettings));
// Once the settings have been passed to the ProcessPoolConfiguration, we don't need them anymore so we can free them.
g_clear_pointer(&priv->memoryPressureSettings, webkit_memory_pressure_settings_free);
}
configuration.setTimeZoneOverride(String::fromUTF8(priv->timeZoneOverride.data(), priv->timeZoneOverride.length()));
#if !ENABLE(2022_GLIB_API)
if (!priv->websiteDataManager)
priv->websiteDataManager = adoptGRef(webkit_website_data_manager_new("local-storage-directory", priv->localStorageDirectory.data(), nullptr));
#endif
priv->processPool = WebProcessPool::create(configuration);
priv->processPool->setUserMessageHandler([webContext](UserMessage&& message, CompletionHandler<void(UserMessage&&)>&& completionHandler) {
// Sink the floating ref.
GRefPtr<WebKitUserMessage> userMessage = webkitUserMessageCreate(WTFMove(message), WTFMove(completionHandler));
gboolean returnValue;
g_signal_emit(webContext, signals[USER_MESSAGE_RECEIVED], 0, userMessage.get(), &returnValue);
});
#if ENABLE(2022_GLIB_API)
priv->processPool->setSandboxEnabled(true);
#endif
priv->processPool->addSandboxPath(injectedBundleDirectory(), SandboxPermission::ReadOnly);
#if ENABLE(MEMORY_SAMPLER)
if (getenv("WEBKIT_SAMPLE_MEMORY"))
priv->processPool->startMemorySampler(0);
#endif
attachInjectedBundleClientToContext(webContext);
priv->geolocationManager = adoptGRef(webkitGeolocationManagerCreate(priv->processPool->supplement<WebGeolocationManagerProxy>()));
priv->notificationProvider = makeUnique<WebKitNotificationProvider>(priv->processPool->supplement<WebNotificationManagerProxy>(), webContext);
#if PLATFORM(GTK) && ENABLE(REMOTE_INSPECTOR)
priv->remoteInspectorProtocolHandler = makeUnique<RemoteInspectorProtocolHandler>(webContext);
#endif
priv->webkitProtocolHandler = makeUnique<WebKitProtocolHandler>(webContext);
}
static void webkitWebContextDispose(GObject* object)
{
WebKitWebContextPrivate* priv = WEBKIT_WEB_CONTEXT(object)->priv;
if (!priv->clientsDetached) {
priv->clientsDetached = true;
priv->processPool->setInjectedBundleClient(nullptr);
}
#if !ENABLE(2022_GLIB_API)
if (priv->faviconDatabase) {
webkitFaviconDatabaseClose(priv->faviconDatabase.get());
priv->faviconDatabase = nullptr;
}
#endif
if (priv->processPool) {
priv->processPool->setUserMessageHandler(nullptr);
priv->processPool = nullptr;
}
G_OBJECT_CLASS(webkit_web_context_parent_class)->dispose(object);
}
static void webkit_web_context_class_init(WebKitWebContextClass* webContextClass)
{
webkitInitialize();
GObjectClass* gObjectClass = G_OBJECT_CLASS(webContextClass);
bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR);
bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
s_serviceWorkerNotificationProvider = makeUnique<WebKitNotificationProvider>(&WebNotificationManagerProxy::sharedServiceWorkerManager(), nullptr);
gObjectClass->get_property = webkitWebContextGetProperty;
gObjectClass->set_property = webkitWebContextSetProperty;
gObjectClass->constructed = webkitWebContextConstructed;
gObjectClass->dispose = webkitWebContextDispose;
#if PLATFORM(GTK) && !USE(GTK4)
/**
* WebKitWebContext:local-storage-directory:
*
* The directory where local storage data will be saved.
*
* Since: 2.8
*
* Deprecated: 2.10. Use #WebKitWebsiteDataManager:local-storage-directory instead.
*/
sObjProperties[PROP_LOCAL_STORAGE_DIRECTORY] =
g_param_spec_string(
"local-storage-directory",
nullptr, nullptr,
nullptr,
static_cast<GParamFlags>(WEBKIT_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
#endif
#if !ENABLE(2022_GLIB_API)
/**
* WebKitWebContext:website-data-manager:
*
* The #WebKitWebsiteDataManager associated with this context.
*
* Since: 2.10
*/
sObjProperties[PROP_WEBSITE_DATA_MANAGER] =
g_param_spec_object(
"website-data-manager",
nullptr, nullptr,
WEBKIT_TYPE_WEBSITE_DATA_MANAGER,
static_cast<GParamFlags>(WEBKIT_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
#endif
#if PLATFORM(GTK) && !USE(GTK4)
/**
* WebKitWebContext:process-swap-on-cross-site-navigation-enabled:
*
* Whether swap Web processes on cross-site navigations is enabled.
*
* When enabled, pages from each security origin will be handled by
* their own separate Web processes, which are started (and
* terminated) on demand as the user navigates across different
* domains. This is an important security measure which helps prevent
* websites stealing data from other visited pages.
*
* Since: 2.28
*/
sObjProperties[PROP_PSON_ENABLED] =
g_param_spec_boolean(
"process-swap-on-cross-site-navigation-enabled",
nullptr, nullptr,
FALSE,
static_cast<GParamFlags>(WEBKIT_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
/**
* WebKitWebContext:use-system-appearance-for-scrollbars:
*
* Whether to use system appearance for rendering scrollbars.
*
* This is enabled by default for backwards compatibility, but it's only
* recommened to use when the application includes other widgets to ensure
* consistency, or when consistency with other applications is required too.
*
* Since: 2.30
*/
sObjProperties[PROP_USE_SYSTEM_APPEARANCE_FOR_SCROLLBARS] =
g_param_spec_boolean(
"use-system-appearance-for-scrollbars",
nullptr, nullptr,
TRUE,
static_cast<GParamFlags>(WEBKIT_PARAM_READWRITE | G_PARAM_CONSTRUCT));
#endif
/**
* WebKitWebContext:memory-pressure-settings:
*
* The #WebKitMemoryPressureSettings applied to the web processes created by this context.
*
* Since: 2.34
*/
sObjProperties[PROP_MEMORY_PRESSURE_SETTINGS] =
g_param_spec_boxed(
"memory-pressure-settings",
nullptr, nullptr,
WEBKIT_TYPE_MEMORY_PRESSURE_SETTINGS,
static_cast<GParamFlags>(WEBKIT_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
/**
* WebKitWebContext:time-zone-override:
*
* The timezone override for this web context. Setting this property provides a better
* alternative to configure the timezone information for all webviews managed by the WebContext.
* The other, less optimal, approach is to globally set the TZ environment variable in the
* process before creating the context. However this approach might not be very convenient and
* can have side-effects in your application.
*
* The expected values for this property are defined in the IANA timezone database. See this
* wikipedia page for instance, https://en.wikipedia.org/wiki/List_of_tz_database_time_zones.
*
* Since: 2.38
*/
sObjProperties[PROP_TIME_ZONE_OVERRIDE] =
g_param_spec_string(
"time-zone-override",
nullptr, nullptr,
nullptr,
static_cast<GParamFlags>(WEBKIT_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_properties(gObjectClass, N_PROPERTIES, sObjProperties);
#if !ENABLE(2022_GLIB_API)
/**
* WebKitWebContext::download-started:
* @context: the #WebKitWebContext
* @download: the #WebKitDownload associated with this event
*
* This signal is emitted when a new download request is made.
*/
signals[DOWNLOAD_STARTED] =
g_signal_new("download-started",
G_TYPE_FROM_CLASS(gObjectClass),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET(WebKitWebContextClass, download_started),
nullptr, nullptr,
g_cclosure_marshal_VOID__OBJECT,
G_TYPE_NONE, 1,
WEBKIT_TYPE_DOWNLOAD);
#endif
signals[INITIALIZE_WEB_PROCESS_EXTENSIONS] =
#if ENABLE(2022_GLIB_API)
g_signal_new("initialize-web-process-extensions",
#else
g_signal_new("initialize-web-extensions",
#endif
G_TYPE_FROM_CLASS(gObjectClass),
G_SIGNAL_RUN_LAST,
#if ENABLE(2022_GLIB_API)
0,
#else
G_STRUCT_OFFSET(WebKitWebContextClass, initialize_web_extensions),
#endif
nullptr, nullptr,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0);
/**
* WebKitWebContext::initialize-notification-permissions:
* @context: the #WebKitWebContext
*
* This signal is emitted when a #WebKitWebContext needs to set
* initial notification permissions for a web process. It is emitted
* when a new web process is about to be launched, and signals the
* most appropriate moment to use
* webkit_web_context_initialize_notification_permissions(). If no
* notification permissions have changed since the last time this
* signal was emitted, then there is no need to call
* webkit_web_context_initialize_notification_permissions() again.
*
* Since: 2.16
*/
signals[INITIALIZE_NOTIFICATION_PERMISSIONS] =
g_signal_new("initialize-notification-permissions",
G_TYPE_FROM_CLASS(gObjectClass),
G_SIGNAL_RUN_LAST,
#if ENABLE(2022_GLIB_API)
0,
#else
G_STRUCT_OFFSET(WebKitWebContextClass, initialize_notification_permissions),
#endif
nullptr, nullptr,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0);
/**
* WebKitWebContext::automation-started:
* @context: the #WebKitWebContext
* @session: the #WebKitAutomationSession associated with this event
*
* This signal is emitted when a new automation request is made.
* Note that it will never be emitted if automation is not enabled in @context,
* see webkit_web_context_set_automation_allowed() for more details.
*
* Since: 2.18
*/
signals[AUTOMATION_STARTED] =
g_signal_new("automation-started",
G_TYPE_FROM_CLASS(gObjectClass),
G_SIGNAL_RUN_LAST,
#if ENABLE(2022_GLIB_API)
0,
#else
G_STRUCT_OFFSET(WebKitWebContextClass, automation_started),
#endif
nullptr, nullptr,
g_cclosure_marshal_VOID__OBJECT,
G_TYPE_NONE, 1,
WEBKIT_TYPE_AUTOMATION_SESSION);
/**
* WebKitWebContext::user-message-received:
* @context: the #WebKitWebContext
* @message: the #WebKitUserMessage received
*
* This signal is emitted when a #WebKitUserMessage is received from a
* web process extension. You can reply to the message using
* webkit_user_message_send_reply().
*
* You can handle the user message asynchronously by calling g_object_ref() on
* @message and returning %TRUE.
*
* Returns: %TRUE if the message was handled, or %FALSE otherwise.
*
* Since: 2.28
*/
signals[USER_MESSAGE_RECEIVED] = g_signal_new(
"user-message-received",
G_TYPE_FROM_CLASS(gObjectClass),
G_SIGNAL_RUN_LAST,
#if ENABLE(2022_GLIB_API)
0,
#else
G_STRUCT_OFFSET(WebKitWebContextClass, user_message_received),
#endif
g_signal_accumulator_true_handled, nullptr,
g_cclosure_marshal_generic,
G_TYPE_BOOLEAN, 1,
WEBKIT_TYPE_USER_MESSAGE);
}
static gpointer createDefaultWebContext(gpointer)
{
static GRefPtr<WebKitWebContext> webContext = adoptGRef(WEBKIT_WEB_CONTEXT(g_object_new(WEBKIT_TYPE_WEB_CONTEXT, nullptr)));
return webContext.get();
}
/**
* webkit_web_context_get_default:
*
* Gets the default web context.
*
* Returns: (transfer none): a #WebKitWebContext
*/
WebKitWebContext* webkit_web_context_get_default(void)
{
static GOnce onceInit = G_ONCE_INIT;
return WEBKIT_WEB_CONTEXT(g_once(&onceInit, createDefaultWebContext, 0));
}
/**
* webkit_web_context_new:
*
* Create a new #WebKitWebContext.
*
* Returns: (transfer full): a newly created #WebKitWebContext
*
* Since: 2.8
*/
WebKitWebContext* webkit_web_context_new(void)
{
return WEBKIT_WEB_CONTEXT(g_object_new(WEBKIT_TYPE_WEB_CONTEXT, nullptr));
}
#if !ENABLE(2022_GLIB_API)
/**
* webkit_web_context_new_ephemeral:
*
* Create a new ephemeral #WebKitWebContext.
*
* An ephemeral #WebKitWebContext is a context
* created with an ephemeral #WebKitWebsiteDataManager. This is just a convenient method
* to create ephemeral contexts without having to create your own #WebKitWebsiteDataManager.
* All #WebKitWebView<!-- -->s associated with this context will also be ephemeral. Websites will
* not store any data in the client storage.
* This is normally used to implement private instances.
*
* Returns: (transfer full): a new ephemeral #WebKitWebContext.
*
* Since: 2.16
*/
WebKitWebContext* webkit_web_context_new_ephemeral()
{
GRefPtr<WebKitWebsiteDataManager> manager = adoptGRef(webkit_website_data_manager_new_ephemeral());
return WEBKIT_WEB_CONTEXT(g_object_new(WEBKIT_TYPE_WEB_CONTEXT, "website-data-manager", manager.get(), nullptr));
}
/**
* webkit_web_context_new_with_website_data_manager:
* @manager: a #WebKitWebsiteDataManager
*
* Create a new #WebKitWebContext with a #WebKitWebsiteDataManager.
*
* Returns: (transfer full): a newly created #WebKitWebContext
*
* Since: 2.10
*/
WebKitWebContext* webkit_web_context_new_with_website_data_manager(WebKitWebsiteDataManager* manager)
{
g_return_val_if_fail(WEBKIT_IS_WEBSITE_DATA_MANAGER(manager), nullptr);
return WEBKIT_WEB_CONTEXT(g_object_new(WEBKIT_TYPE_WEB_CONTEXT, "website-data-manager", manager, nullptr));
}
/**
* webkit_web_context_get_website_data_manager:
* @context: the #WebKitWebContext
*
* Get the #WebKitWebsiteDataManager of @context.
*
* Returns: (transfer none): a #WebKitWebsiteDataManager
*
* Since: 2.10
*/
WebKitWebsiteDataManager* webkit_web_context_get_website_data_manager(WebKitWebContext* context)
{
g_return_val_if_fail(WEBKIT_IS_WEB_CONTEXT(context), nullptr);
return context->priv->websiteDataManager.get();
}
/**
* webkit_web_context_is_ephemeral:
* @context: the #WebKitWebContext
*
* Get whether a #WebKitWebContext is ephemeral.
*
* Returns: %TRUE if @context is ephemeral or %FALSE otherwise.
*
* Since: 2.16
*/
gboolean webkit_web_context_is_ephemeral(WebKitWebContext* context)
{
g_return_val_if_fail(WEBKIT_IS_WEB_CONTEXT(context), FALSE);
return webkit_website_data_manager_is_ephemeral(context->priv->websiteDataManager.get());
}
#endif
/**
* webkit_web_context_is_automation_allowed:
* @context: the #WebKitWebContext
*
* Get whether automation is allowed in @context.
*
* See also webkit_web_context_set_automation_allowed().
*
* Returns: %TRUE if automation is allowed or %FALSE otherwise.
*
* Since: 2.18
*/
gboolean webkit_web_context_is_automation_allowed(WebKitWebContext* context)
{
g_return_val_if_fail(WEBKIT_IS_WEB_CONTEXT(context), FALSE);
#if ENABLE(REMOTE_INSPECTOR)
return !!context->priv->automationClient;
#else
return FALSE;
#endif
}
/**
* webkit_web_context_set_automation_allowed:
* @context: the #WebKitWebContext
* @allowed: value to set
*
* Set whether automation is allowed in @context.
*
* When automation is enabled the browser could
* be controlled by another process by requesting an automation session. When a new automation
* session is requested the signal #WebKitWebContext::automation-started is emitted.
* Automation is disabled by default, so you need to explicitly call this method passing %TRUE
* to enable it.
*
* Note that only one #WebKitWebContext can have automation enabled, so this will do nothing
* if there's another #WebKitWebContext with automation already enabled.
*
* Since: 2.18
*/
void webkit_web_context_set_automation_allowed(WebKitWebContext* context, gboolean allowed)
{
g_return_if_fail(WEBKIT_IS_WEB_CONTEXT(context));
if (webkit_web_context_is_automation_allowed(context) == allowed)
return;
#if ENABLE(REMOTE_INSPECTOR)
if (allowed) {
if (Inspector::RemoteInspector::singleton().client()) {
g_warning("Not enabling automation on WebKitWebContext because there's another context with automation enabled, only one is allowed");
return;
}
context->priv->automationClient = makeUnique<WebKitAutomationClient>(context);
} else
context->priv->automationClient = nullptr;
#endif
}
#if ENABLE(2022_GLIB_API)
/**
* webkit_web_context_get_network_session_for_automation:
* @context: the #WebKitWebContext
*
* Get the #WebKitNetworkSession used for automation sessions started in @context.
*
* Returns: (transfer none) (nullable): a #WebKitNetworkSession, or %NULL if automation is not enabled
*
* Since: 2.40
*/
WebKitNetworkSession* webkit_web_context_get_network_session_for_automation(WebKitWebContext* context)
{
g_return_val_if_fail(WEBKIT_IS_WEB_CONTEXT(context), nullptr);
#if ENABLE(REMOTE_INSPECTOR)
if (!context->priv->automationNetworkSession && context->priv->automationClient)
context->priv->automationNetworkSession = adoptGRef(webkit_network_session_new_ephemeral());
return context->priv->automationNetworkSession.get();
#else
return nullptr;
#endif
}
#endif
/**
* webkit_web_context_set_cache_model:
* @context: the #WebKitWebContext
* @cache_model: a #WebKitCacheModel
*
* Specifies a usage model for WebViews.
*
* Specifies a usage model for WebViews, which WebKit will use to
* determine its caching behavior. All web views follow the cache
* model. This cache model determines the RAM and disk space to use
* for caching previously viewed content .
*
* Research indicates that users tend to browse within clusters of
* documents that hold resources in common, and to revisit previously
* visited documents. WebKit and the frameworks below it include
* built-in caches that take advantage of these patterns,
* substantially improving document load speed in browsing
* situations. The WebKit cache model controls the behaviors of all of
* these caches, including various WebCore caches.
*
* Browsers can improve document load speed substantially by
* specifying %WEBKIT_CACHE_MODEL_WEB_BROWSER. Applications without a
* browsing interface can reduce memory usage substantially by
* specifying %WEBKIT_CACHE_MODEL_DOCUMENT_VIEWER. The default value is
* %WEBKIT_CACHE_MODEL_WEB_BROWSER.
*/
void webkit_web_context_set_cache_model(WebKitWebContext*, WebKitCacheModel model)
{
CacheModel cacheModel;
switch (model) {
case WEBKIT_CACHE_MODEL_DOCUMENT_VIEWER:
cacheModel = CacheModel::DocumentViewer;
break;
case WEBKIT_CACHE_MODEL_WEB_BROWSER:
cacheModel = CacheModel::PrimaryWebBrowser;
break;
case WEBKIT_CACHE_MODEL_DOCUMENT_BROWSER:
cacheModel = CacheModel::DocumentBrowser;
break;
default:
g_assert_not_reached();
}
if (cacheModel != LegacyGlobalSettings::singleton().cacheModel())
LegacyGlobalSettings::singleton().setCacheModel(cacheModel);
}
/**
* webkit_web_context_get_cache_model:
* @context: the #WebKitWebContext
*
* Returns the current cache model.
*
* For more information about this
* value check the documentation of the function
* webkit_web_context_set_cache_model().
*
* Returns: the current #WebKitCacheModel
*/
WebKitCacheModel webkit_web_context_get_cache_model(WebKitWebContext* context)
{
g_return_val_if_fail(WEBKIT_IS_WEB_CONTEXT(context), WEBKIT_CACHE_MODEL_WEB_BROWSER);
switch (LegacyGlobalSettings::singleton().cacheModel()) {
case CacheModel::DocumentViewer:
return WEBKIT_CACHE_MODEL_DOCUMENT_VIEWER;
case CacheModel::PrimaryWebBrowser:
return WEBKIT_CACHE_MODEL_WEB_BROWSER;
case CacheModel::DocumentBrowser:
return WEBKIT_CACHE_MODEL_DOCUMENT_BROWSER;
default:
g_assert_not_reached();
}
return WEBKIT_CACHE_MODEL_WEB_BROWSER;
}
#if !ENABLE(2022_GLIB_API)
/**
* webkit_web_context_clear_cache:
* @context: a #WebKitWebContext
*
* Clears all resources currently cached.
*
* See also webkit_web_context_set_cache_model().
*/
void webkit_web_context_clear_cache(WebKitWebContext* context)
{
g_return_if_fail(WEBKIT_IS_WEB_CONTEXT(context));
OptionSet<WebsiteDataType> websiteDataTypes;
websiteDataTypes.add(WebsiteDataType::MemoryCache);
websiteDataTypes.add(WebsiteDataType::DiskCache);
auto& websiteDataStore = webkitWebsiteDataManagerGetDataStore(context->priv->websiteDataManager.get());
websiteDataStore.removeData(websiteDataTypes, -WallTime::infinity(), [] { });
}
/**
* webkit_web_context_set_network_proxy_settings:
* @context: a #WebKitWebContext
* @proxy_mode: a #WebKitNetworkProxyMode
* @proxy_settings: (allow-none): a #WebKitNetworkProxySettings, or %NULL
*
* Set the network proxy settings to be used by connections started in @context.
*
* By default %WEBKIT_NETWORK_PROXY_MODE_DEFAULT is used, which means that the
* system settings will be used (g_proxy_resolver_get_default()).
* If you want to override the system default settings, you can either use
* %WEBKIT_NETWORK_PROXY_MODE_NO_PROXY to make sure no proxies are used at all,
* or %WEBKIT_NETWORK_PROXY_MODE_CUSTOM to provide your own proxy settings.
* When @proxy_mode is %WEBKIT_NETWORK_PROXY_MODE_CUSTOM @proxy_settings must be
* a valid #WebKitNetworkProxySettings; otherwise, @proxy_settings must be %NULL.
*
* Since: 2.16
*
* Deprecated: 2.32. Use webkit_website_data_manager_set_network_proxy_settings() instead.
*/
void webkit_web_context_set_network_proxy_settings(WebKitWebContext* context, WebKitNetworkProxyMode proxyMode, WebKitNetworkProxySettings* proxySettings)
{
g_return_if_fail(WEBKIT_IS_WEB_CONTEXT(context));
webkit_website_data_manager_set_network_proxy_settings(context->priv->websiteDataManager.get(), proxyMode, proxySettings);
}
/**
* webkit_web_context_download_uri:
* @context: a #WebKitWebContext
* @uri: the URI to download
*
* Requests downloading of the specified URI string.
*
* The download operation will not be associated to any #WebKitWebView,
* if you are interested in starting a download from a particular #WebKitWebView use
* webkit_web_view_download_uri() instead.
*
* Returns: (transfer full): a new #WebKitDownload representing
* the download operation.
*/
WebKitDownload* webkit_web_context_download_uri(WebKitWebContext* context, const gchar* uri)
{
g_return_val_if_fail(WEBKIT_IS_WEB_CONTEXT(context), nullptr);
g_return_val_if_fail(uri, nullptr);
WebCore::ResourceRequest request(String::fromUTF8(uri));
auto& websiteDataStore = webkitWebsiteDataManagerGetDataStore(context->priv->websiteDataManager.get());
auto downloadProxy = context->priv->processPool->download(websiteDataStore, nullptr, request);
auto download = webkitDownloadCreate(downloadProxy.get());
downloadProxy->setDidStartCallback([context = GRefPtr<WebKitWebContext> { context }, download = download.get()](auto* downloadProxy) {
if (!downloadProxy)
return;
webkitDownloadStarted(download);
webkitWebContextDownloadStarted(context.get(), download);
});
return download.leakRef();
}
/**
* webkit_web_context_get_cookie_manager:
* @context: a #WebKitWebContext
*
* Get the #WebKitCookieManager of the @context's #WebKitWebsiteDataManager.
*
* Returns: (transfer none): the #WebKitCookieManager of @context.
*/
WebKitCookieManager* webkit_web_context_get_cookie_manager(WebKitWebContext* context)
{
g_return_val_if_fail(WEBKIT_IS_WEB_CONTEXT(context), nullptr);
return webkit_website_data_manager_get_cookie_manager(context->priv->websiteDataManager.get());
}
#endif
/**
* webkit_web_context_get_geolocation_manager:
* @context: a #WebKitWebContext
*
* Get the #WebKitGeolocationManager of @context.
*
* Returns: (transfer none): the #WebKitGeolocationManager of @context.
*
* Since: 2.26
*/
WebKitGeolocationManager* webkit_web_context_get_geolocation_manager(WebKitWebContext* context)
{
g_return_val_if_fail(WEBKIT_IS_WEB_CONTEXT(context), nullptr);
return context->priv->geolocationManager.get();
}
#if !ENABLE(2022_GLIB_API)
static void ensureFaviconDatabase(WebKitWebContext* context)
{
WebKitWebContextPrivate* priv = context->priv;
if (priv->faviconDatabase)
return;
priv->faviconDatabase = adoptGRef(webkitFaviconDatabaseCreate());
}
/**
* webkit_web_context_set_favicon_database_directory:
* @context: a #WebKitWebContext
* @path: (allow-none): an absolute path to the icon database
* directory or %NULL to use the defaults
*
* Set the directory path to store the favicons database.
*
* Set the directory path to be used to store the favicons database
* for @context on disk. Passing %NULL as @path means using the
* default directory for the platform (see g_get_user_cache_dir()).
*
* Calling this method also means enabling the favicons database for
* its use from the applications, so that's why it's expected to be
* called only once. Further calls for the same instance of
* #WebKitWebContext won't cause any effect.
*/
void webkit_web_context_set_favicon_database_directory(WebKitWebContext* context, const gchar* path)
{
g_return_if_fail(WEBKIT_IS_WEB_CONTEXT(context));
WebKitWebContextPrivate* priv = context->priv;
ensureFaviconDatabase(context);
String directoryPath = FileSystem::stringFromFileSystemRepresentation(path);
// Use default if nullptr is passed as parameter.
if (directoryPath.isEmpty()) {
#if PLATFORM(GTK)
const char* portDirectory = "webkitgtk";
#elif PLATFORM(WPE)
const char* portDirectory = "wpe";
#endif
GUniquePtr<gchar> databaseDirectory(g_build_filename(g_get_user_cache_dir(), portDirectory, "icondatabase", nullptr));
directoryPath = FileSystem::stringFromFileSystemRepresentation(databaseDirectory.get());
}
priv->faviconDatabaseDirectory = directoryPath.utf8();
// Build the full path to the icon database file on disk.
GUniquePtr<gchar> faviconDatabasePath(g_build_filename(priv->faviconDatabaseDirectory.data(),
"WebpageIcons.db", nullptr));
// Setting the path will cause the icon database to be opened.
webkitFaviconDatabaseOpen(priv->faviconDatabase.get(), FileSystem::stringFromFileSystemRepresentation(faviconDatabasePath.get()), webkit_web_context_is_ephemeral(context));
}
/**
* webkit_web_context_get_favicon_database_directory:
* @context: a #WebKitWebContext
*
* Get the directory path to store the favicons database.
*
* Get the directory path being used to store the favicons database
* for @context, or %NULL if
* webkit_web_context_set_favicon_database_directory() hasn't been
* called yet.
*
* This function will always return the same path after having called
* webkit_web_context_set_favicon_database_directory() for the first
* time.
*
* Returns: (transfer none): the path of the directory of the favicons
* database associated with @context, or %NULL.
*/
const gchar* webkit_web_context_get_favicon_database_directory(WebKitWebContext *context)
{
g_return_val_if_fail(WEBKIT_IS_WEB_CONTEXT(context), 0);
WebKitWebContextPrivate* priv = context->priv;
if (priv->faviconDatabaseDirectory.isNull())
return 0;
return priv->faviconDatabaseDirectory.data();
}
/**
* webkit_web_context_get_favicon_database:
* @context: a #WebKitWebContext
*
* Get the #WebKitFaviconDatabase associated with @context.
*
* To initialize the database you need to call
* webkit_web_context_set_favicon_database_directory().
*
* Returns: (transfer none): the #WebKitFaviconDatabase of @context.
*/
WebKitFaviconDatabase* webkit_web_context_get_favicon_database(WebKitWebContext* context)
{
g_return_val_if_fail(WEBKIT_IS_WEB_CONTEXT(context), 0);
ensureFaviconDatabase(context);
return context->priv->faviconDatabase.get();
}
#endif
/**
* webkit_web_context_get_security_manager:
* @context: a #WebKitWebContext
*
* Get the #WebKitSecurityManager of @context.
*
* Returns: (transfer none): the #WebKitSecurityManager of @context.
*/
WebKitSecurityManager* webkit_web_context_get_security_manager(WebKitWebContext* context)
{
g_return_val_if_fail(WEBKIT_IS_WEB_CONTEXT(context), 0);
WebKitWebContextPrivate* priv = context->priv;
if (!priv->securityManager)
priv->securityManager = adoptGRef(webkitSecurityManagerCreate(context));
return priv->securityManager.get();
}
#if !ENABLE(2022_GLIB_API)
/**
* webkit_web_context_set_additional_plugins_directory:
* @context: a #WebKitWebContext
* @directory: the directory to add
*
* Set an additional directory where WebKit will look for plugins.
*
* Deprecated: 2.32
*/
void webkit_web_context_set_additional_plugins_directory(WebKitWebContext*, const char*)
{
g_warning("webkit_web_context_set_additional_plugins_directory is deprecated and does nothing. Netscape plugins are no longer supported.");
}
/**
* webkit_web_context_get_plugins:
* @context: a #WebKitWebContext
* @cancellable: (allow-none): a #GCancellable or %NULL to ignore
* @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied
* @user_data: the data to pass to callback function
*
* Asynchronously get the list of installed plugins.
*
* When the operation is finished, @callback will be called. You can then call
* webkit_web_context_get_plugins_finish() to get the result of the operation.
*
* Deprecated: 2.32
*/
void webkit_web_context_get_plugins(WebKitWebContext* context, GCancellable* cancellable, GAsyncReadyCallback callback, gpointer userData)
{
g_return_if_fail(WEBKIT_IS_WEB_CONTEXT(context));
g_warning("webkit_web_context_get_plugins is deprecated and always returns an empty list. Netscape plugins are no longer supported.");
GRefPtr<GTask> task = adoptGRef(g_task_new(context, cancellable, callback, userData));
g_task_return_pointer(task.get(), nullptr, nullptr);
}
/**
* webkit_web_context_get_plugins_finish:
* @context: a #WebKitWebContext
* @result: a #GAsyncResult
* @error: return location for error or %NULL to ignore
*
* Finish an asynchronous operation started with webkit_web_context_get_plugins.
*
* Returns: (element-type WebKitPlugin) (transfer full): a #GList of #WebKitPlugin. You must free the #GList with
* g_list_free() and unref the #WebKitPlugin<!-- -->s with g_object_unref() when you're done with them.
*
* Deprecated: 2.32
*/
GList* webkit_web_context_get_plugins_finish(WebKitWebContext* context, GAsyncResult* result, GError** error)
{
g_return_val_if_fail(WEBKIT_IS_WEB_CONTEXT(context), nullptr);
g_return_val_if_fail(g_task_is_valid(result, context), nullptr);
return static_cast<GList*>(g_task_propagate_pointer(G_TASK(result), error));
}
#endif
/**
* webkit_web_context_register_uri_scheme:
* @context: a #WebKitWebContext
* @scheme: the network scheme to register
* @callback: (scope async): a #WebKitURISchemeRequestCallback
* @user_data: data to pass to callback function
* @user_data_destroy_func: destroy notify for @user_data
*
* Register @scheme in @context.
*
* Register @scheme in @context, so that when an URI request with @scheme is made in the
* #WebKitWebContext, the #WebKitURISchemeRequestCallback registered will be called with a
* #WebKitURISchemeRequest.
* It is possible to handle URI scheme requests asynchronously, by calling g_object_ref() on the
* #WebKitURISchemeRequest and calling webkit_uri_scheme_request_finish() later
* when the data of the request is available or
* webkit_uri_scheme_request_finish_error() in case of error.
*
* ```c
* static void
* about_uri_scheme_request_cb (WebKitURISchemeRequest *request,
* gpointer user_data)
* {
* GInputStream *stream;
* gsize stream_length;
* const gchar *path = webkit_uri_scheme_request_get_path (request);
*
* if (!g_strcmp0 (path, "memory")) {
* // Create a GInputStream with the contents of memory about page, and set its length to stream_length
* } else if (!g_strcmp0 (path, "applications")) {
* // Create a GInputStream with the contents of applications about page, and set its length to stream_length
* } else if (!g_strcmp0 (path, "example")) {
* gchar *contents = g_strdup_printf ("<html><body><p>Example about page</p></body></html>");
* stream_length = strlen (contents);
* stream = g_memory_input_stream_new_from_data (contents, stream_length, g_free);
* } else {
* GError *error = g_error_new (ABOUT_HANDLER_ERROR, ABOUT_HANDLER_ERROR_INVALID, "Invalid about:%s page.", path);
* webkit_uri_scheme_request_finish_error (request, error);
* g_error_free (error);
* return;
* }
* webkit_uri_scheme_request_finish (request, stream, stream_length, "text/html");
* g_object_unref (stream);
* }
* ```
*/
void webkit_web_context_register_uri_scheme(WebKitWebContext* context, const char* scheme, WebKitURISchemeRequestCallback callback, gpointer userData, GDestroyNotify destroyNotify)
{
g_return_if_fail(WEBKIT_IS_WEB_CONTEXT(context));
g_return_if_fail(scheme);
g_return_if_fail(callback);
auto canonicalizedScheme = WTF::URLParser::maybeCanonicalizeScheme(StringView::fromLatin1(scheme));
if (!canonicalizedScheme) {
g_critical("Cannot register invalid URI scheme %s", scheme);
return;
}
if (WTF::URLParser::isSpecialScheme(canonicalizedScheme.value())) {
g_warning("Registering special URI scheme %s is no longer allowed", scheme);
return;
}
auto handler = WebKitURISchemeHandler::create(context, callback, userData, destroyNotify);
auto addResult = context->priv->uriSchemeHandlers.add(String::fromUTF8(scheme), WTFMove(handler));
if (addResult.isNewEntry) {
for (auto* webView : context->priv->webViews.values())
webkitWebViewGetPage(webView).setURLSchemeHandlerForScheme(*addResult.iterator->value, String::fromUTF8(scheme));
} else
g_critical("Cannot register URI scheme %s more than once", scheme);
}
#if !ENABLE(2022_GLIB_API)
/**
* webkit_web_context_set_sandbox_enabled:
* @context: a #WebKitWebContext
* @enabled: if %TRUE enable sandboxing
*
* Set whether WebKit subprocesses will be sandboxed.
*
* Set whether WebKit subprocesses will be sandboxed, limiting access to the system.
* This method **must be called before any web process has been created**,
* as early as possible in your application. Calling it later is a fatal error.
*
* This is only implemented on Linux and is a no-op otherwise.
*
* Since: 2.26
*/
void webkit_web_context_set_sandbox_enabled(WebKitWebContext* context, gboolean enabled)
{
g_return_if_fail(WEBKIT_IS_WEB_CONTEXT(context));
if (context->priv->processPool->processes().size())
g_error("Sandboxing cannot be changed after subprocesses were spawned.");
context->priv->processPool->setSandboxEnabled(enabled);
}
#endif
static bool pathIsHomeDirectory(const char* path)
{
std::unique_ptr<char, decltype(free)*> resolvedPath(realpath(path, nullptr), free);
if (!resolvedPath) {
g_warning("Failed to canonicalize path %s: %s", path, g_strerror(errno));
return true;
}
if (!strcmp(resolvedPath.get(), "/home"))
return true;
std::unique_ptr<char, decltype(free)*> resolvedHomeDirectory(realpath(g_get_home_dir(), nullptr), free);
if (!resolvedPath) {
g_warning("Failed to canonicalize path %s: %s", g_get_home_dir(), g_strerror(errno));
return true;
}
return !strcmp(resolvedPath.get(), resolvedHomeDirectory.get());
}
static bool pathIsBlocked(const char* path)
{
static const Vector<CString, 4> blockedPrefixes = {
// These are recreated by bwrap and it doesn't make sense to try and rebind them.
"sys", "proc", "dev",
"", // All of `/` isn't acceptable.
};
if (!g_path_is_absolute(path))
return true;
if (pathIsHomeDirectory(path))
return true;
GUniquePtr<char*> splitPath(g_strsplit(path, G_DIR_SEPARATOR_S, 3));
return blockedPrefixes.contains(splitPath.get()[1]);
}
/**
* webkit_web_context_add_path_to_sandbox:
* @context: a #WebKitWebContext
* @path: (type filename): an absolute path to mount in the sandbox
* @read_only: if %TRUE the path will be read-only
*
* Adds a path to be mounted in the sandbox.
*
* @path must exist before any web process has been created; otherwise,
* it will be silently ignored. It is a fatal error to add paths after
* a web process has been spawned.
*
* Paths under `/sys`, `/proc`, and `/dev` are invalid. Attempting to
* add all of `/` is not valid. Since 2.40, adding the user's entire
* home directory or /home is also not valid.
*
* See also webkit_web_context_set_sandbox_enabled()
*
* Since: 2.26
*/
void webkit_web_context_add_path_to_sandbox(WebKitWebContext* context, const char* path, gboolean readOnly)
{
g_return_if_fail(WEBKIT_IS_WEB_CONTEXT(context));
if (pathIsBlocked(path)) {
g_critical("Attempted to add disallowed path to sandbox: %s", path);
return;
}
if (context->priv->processPool->processes().size())
g_error("Sandbox paths cannot be changed after subprocesses were spawned.");
auto permission = readOnly ? SandboxPermission::ReadOnly : SandboxPermission::ReadWrite;
context->priv->processPool->addSandboxPath(path, permission);
}
#if !ENABLE(2022_GLIB_API)
/**
* webkit_web_context_get_sandbox_enabled:
* @context: a #WebKitWebContext
*
* Get whether sandboxing is currently enabled.
*
* Returns: %TRUE if sandboxing is enabled, or %FALSE otherwise.
*
* Since: 2.26
*/
gboolean webkit_web_context_get_sandbox_enabled(WebKitWebContext* context)
{
g_return_val_if_fail(WEBKIT_IS_WEB_CONTEXT(context), FALSE);
return context->priv->processPool->sandboxEnabled();
}
#endif
/**
* webkit_web_context_get_spell_checking_enabled:
* @context: a #WebKitWebContext
*
* Get whether spell checking feature is currently enabled.
*
* Returns: %TRUE If spell checking is enabled, or %FALSE otherwise.
*/
gboolean webkit_web_context_get_spell_checking_enabled(WebKitWebContext* context)
{
g_return_val_if_fail(WEBKIT_IS_WEB_CONTEXT(context), FALSE);
#if ENABLE(SPELLCHECK)
return TextChecker::state().isContinuousSpellCheckingEnabled;
#else
return false;
#endif
}
/**
* webkit_web_context_set_spell_checking_enabled:
* @context: a #WebKitWebContext
* @enabled: Value to be set
*
* Enable or disable the spell checking feature.
*/
void webkit_web_context_set_spell_checking_enabled(WebKitWebContext* context, gboolean enabled)
{
g_return_if_fail(WEBKIT_IS_WEB_CONTEXT(context));
#if ENABLE(SPELLCHECK)
TextChecker::setContinuousSpellCheckingEnabled(enabled);
#endif
}
/**
* webkit_web_context_get_spell_checking_languages:
* @context: a #WebKitWebContext
*
* Get the the list of spell checking languages.
*
* Get the the list of spell checking languages associated with
* @context, or %NULL if no languages have been previously set.
*
* See webkit_web_context_set_spell_checking_languages() for more
* details on the format of the languages in the list.
*
* Returns: (array zero-terminated=1) (element-type utf8) (transfer none): A %NULL-terminated
* array of languages if available, or %NULL otherwise.
*/
const gchar* const* webkit_web_context_get_spell_checking_languages(WebKitWebContext* context)
{
g_return_val_if_fail(WEBKIT_IS_WEB_CONTEXT(context), nullptr);
#if ENABLE(SPELLCHECK)
Vector<String> spellCheckingLanguages = TextChecker::loadedSpellCheckingLanguages();
if (spellCheckingLanguages.isEmpty())
return nullptr;
static GRefPtr<GPtrArray> languagesToReturn;
languagesToReturn = adoptGRef(g_ptr_array_new_with_free_func(g_free));
for (const auto& language : spellCheckingLanguages)
g_ptr_array_add(languagesToReturn.get(), g_strdup(language.utf8().data()));
g_ptr_array_add(languagesToReturn.get(), nullptr);
return reinterpret_cast<char**>(languagesToReturn->pdata);
#else
return 0;
#endif
}
/**
* webkit_web_context_set_spell_checking_languages:
* @context: a #WebKitWebContext
* @languages: (array zero-terminated=1) (transfer none): a %NULL-terminated list of spell checking languages
*
* Set the list of spell checking languages to be used for spell
* checking.
*
* The locale string typically is in the form lang_COUNTRY, where lang
* is an ISO-639 language code, and COUNTRY is an ISO-3166 country code.
* For instance, sv_FI for Swedish as written in Finland or pt_BR
* for Portuguese as written in Brazil.
*
* You need to call this function with a valid list of languages at
* least once in order to properly enable the spell checking feature
* in WebKit.
*/
void webkit_web_context_set_spell_checking_languages(WebKitWebContext* context, const gchar* const* languages)
{
g_return_if_fail(WEBKIT_IS_WEB_CONTEXT(context));
g_return_if_fail(languages);
#if ENABLE(SPELLCHECK)
Vector<String> spellCheckingLanguages;
for (size_t i = 0; languages[i]; ++i)
spellCheckingLanguages.append(String::fromUTF8(languages[i]));
TextChecker::setSpellCheckingLanguages(spellCheckingLanguages);
#endif
}
/**
* webkit_web_context_set_preferred_languages:
* @context: a #WebKitWebContext
* @languages: (allow-none) (array zero-terminated=1) (element-type utf8) (transfer none): a %NULL-terminated list of language identifiers
*
* Set the list of preferred languages.
*
* Set the list of preferred languages, sorted from most desirable
* to least desirable. The list will be used in the following ways:
*
* - Determining how to build the `Accept-Language` HTTP header that will be
* included in the network requests started by the #WebKitWebContext.
* - Setting the values of `navigator.language` and `navigator.languages`.
* - The first item in the list sets the default locale for JavaScript
* `Intl` functions.
*/
void webkit_web_context_set_preferred_languages(WebKitWebContext* context, const gchar* const* languageList)
{
g_return_if_fail(WEBKIT_IS_WEB_CONTEXT(context));
if (!languageList || !g_strv_length(const_cast<char**>(languageList)))
return;
Vector<String> languages;
for (size_t i = 0; languageList[i]; ++i) {
// Do not propagate the C locale to WebCore.
if (!g_ascii_strcasecmp(languageList[i], "C") || !g_ascii_strcasecmp(languageList[i], "POSIX"))
languages.append("en-US"_s);
else
languages.append(makeStringByReplacingAll(String::fromUTF8(languageList[i]), '_', '-'));
}
context->priv->processPool->setOverrideLanguages(WTFMove(languages));
}
#if !ENABLE(2022_GLIB_API)
/**
* webkit_web_context_set_tls_errors_policy:
* @context: a #WebKitWebContext
* @policy: a #WebKitTLSErrorsPolicy
*
* Set the TLS errors policy of @context as @policy.
*
* Deprecated: 2.32. Use webkit_website_data_manager_set_tls_errors_policy() instead.
*/
void webkit_web_context_set_tls_errors_policy(WebKitWebContext* context, WebKitTLSErrorsPolicy policy)
{
g_return_if_fail(WEBKIT_IS_WEB_CONTEXT(context));
webkit_website_data_manager_set_tls_errors_policy(context->priv->websiteDataManager.get(), policy);
}
/**
* webkit_web_context_get_tls_errors_policy:
* @context: a #WebKitWebContext
*
* Get the TLS errors policy of @context.
*
* Returns: a #WebKitTLSErrorsPolicy
*
* Deprecated: 2.32. Use webkit_website_data_manager_get_tls_errors_policy() instead.
*/
WebKitTLSErrorsPolicy webkit_web_context_get_tls_errors_policy(WebKitWebContext* context)
{
g_return_val_if_fail(WEBKIT_IS_WEB_CONTEXT(context), WEBKIT_TLS_ERRORS_POLICY_IGNORE);
return webkit_website_data_manager_get_tls_errors_policy(context->priv->websiteDataManager.get());
}
#endif
#if ENABLE(2022_GLIB_API)
void webkit_web_context_set_web_process_extensions_directory(WebKitWebContext* context, const char* directory)
#else
void webkit_web_context_set_web_extensions_directory(WebKitWebContext* context, const char* directory)
#endif
{
g_return_if_fail(WEBKIT_IS_WEB_CONTEXT(context));
g_return_if_fail(directory);
context->priv->webProcessExtensionsDirectory = directory;
context->priv->processPool->addSandboxPath(directory, SandboxPermission::ReadOnly);
}
#if ENABLE(2022_GLIB_API)
void webkit_web_context_set_web_process_extensions_initialization_user_data(WebKitWebContext* context, GVariant* userData)
#else
void webkit_web_context_set_web_extensions_initialization_user_data(WebKitWebContext* context, GVariant* userData)
#endif
{
g_return_if_fail(WEBKIT_IS_WEB_CONTEXT(context));
g_return_if_fail(userData);
context->priv->webProcessExtensionsInitializationUserData = userData;
}
#if PLATFORM(GTK) && !USE(GTK4)
/**
* webkit_web_context_set_disk_cache_directory:
* @context: a #WebKitWebContext
* @directory: the directory to set
*
* Set the directory where disk cache files will be stored.
*
* This method must be called before loading anything in this context, otherwise
* it will not have any effect.
*
* Note that this method overrides the directory set in the #WebKitWebsiteDataManager,
* but it doesn't change the value returned by webkit_website_data_manager_get_disk_cache_directory()
* since the #WebKitWebsiteDataManager is immutable.
*
* Deprecated: 2.10. Use webkit_web_context_new_with_website_data_manager() instead.
*/
void webkit_web_context_set_disk_cache_directory(WebKitWebContext*, const char*)
{
g_warning("webkit_web_context_set_disk_cache_directory is deprecated and does nothing, use WebKitWebsiteDataManager instead");
}
#endif
#if !ENABLE(2022_GLIB_API)
/**
* webkit_web_context_prefetch_dns:
* @context: a #WebKitWebContext
* @hostname: a hostname to be resolved
*
* Resolve the domain name of the given @hostname in advance.
*
* Resolve the domain name of the given @hostname in advance, so that if a URI
* of @hostname is requested the load will be performed more quickly.
*/
void webkit_web_context_prefetch_dns(WebKitWebContext* context, const char* hostname)
{
g_return_if_fail(WEBKIT_IS_WEB_CONTEXT(context));
g_return_if_fail(hostname);
if (context->priv->dnsPrefetchedHosts.add(String::fromUTF8(hostname)).isNewEntry) {
auto& websiteDataStore = webkitWebsiteDataManagerGetDataStore(context->priv->websiteDataManager.get());
websiteDataStore.networkProcess().send(Messages::NetworkProcess::PrefetchDNS(String::fromUTF8(hostname)), 0);
}
context->priv->dnsPrefetchHystereris.impulse();
}
/**
* webkit_web_context_allow_tls_certificate_for_host:
* @context: a #WebKitWebContext
* @certificate: a #GTlsCertificate
* @host: the host for which a certificate is to be allowed
*
* Ignore further TLS errors on the @host for the certificate present in @info.
*
* Since: 2.6
*/
void webkit_web_context_allow_tls_certificate_for_host(WebKitWebContext* context, GTlsCertificate* certificate, const gchar* host)
{
g_return_if_fail(WEBKIT_IS_WEB_CONTEXT(context));
g_return_if_fail(G_IS_TLS_CERTIFICATE(certificate));
g_return_if_fail(host);
auto certificateInfo = WebCore::CertificateInfo(certificate, static_cast<GTlsCertificateFlags>(0));
auto& websiteDataStore = webkitWebsiteDataManagerGetDataStore(context->priv->websiteDataManager.get());
websiteDataStore.allowSpecificHTTPSCertificateForHost(certificateInfo, String::fromUTF8(host));
}
#endif
#if !ENABLE(2022_GLIB_API)
/**
* webkit_web_context_set_process_model:
* @context: the #WebKitWebContext
* @process_model: a #WebKitProcessModel
*
* This function previously allowed specifying the process model to use.
* However, since 2.26, the only allowed process model is
* %WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES, so this function
* does nothing.
*
* Since: 2.4
*
* Deprecated: 2.40
*/
void webkit_web_context_set_process_model(WebKitWebContext* context, WebKitProcessModel processModel)
{
g_return_if_fail(WEBKIT_IS_WEB_CONTEXT(context));
if (processModel == WEBKIT_PROCESS_MODEL_SHARED_SECONDARY_PROCESS)
g_warning("WEBKIT_PROCESS_MODEL_SHARED_SECONDARY_PROCESS is deprecated and has no effect");
}
/**
* webkit_web_context_get_process_model:
* @context: the #WebKitWebContext
*
* Returns %WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES.
*
* For more information about why this function is deprecated,
* see webkit_web_context_set_process_model().
*
* Returns: %WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES
*
* Since: 2.4
*
* Deprecated: 2.40
*/
WebKitProcessModel webkit_web_context_get_process_model(WebKitWebContext* context)
{
g_return_val_if_fail(WEBKIT_IS_WEB_CONTEXT(context), WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
return WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES;
}
/**
* webkit_web_context_set_web_process_count_limit:
* @context: the #WebKitWebContext
* @limit: the maximum number of web processes
*
* Sets the maximum number of web processes.
*
* Sets the maximum number of web processes that can be created at the same time for the @context.
* The default value is 0 and means no limit.
*
* This function is now deprecated and does nothing for security reasons.
*
* Since: 2.10
*
* Deprecated: 2.26
*/
void webkit_web_context_set_web_process_count_limit(WebKitWebContext* context, guint limit)
{
g_return_if_fail(WEBKIT_IS_WEB_CONTEXT(context));
g_warning("webkit_web_context_set_web_process_count_limit is deprecated and does nothing. Limiting the number of web processes is no longer possible for security reasons");
}
/**
* webkit_web_context_get_web_process_count_limit:
* @context: the #WebKitWebContext
*
* Gets the maximum number of web processes that can be created at the same time for the @context.
*
* This function is now deprecated and always returns 0 (no limit). See also webkit_web_context_set_web_process_count_limit().
*
* Returns: the maximum limit of web processes, or 0 if there isn't a limit.
*
* Since: 2.10
*
* Deprecated: 2.26
*/
guint webkit_web_context_get_web_process_count_limit(WebKitWebContext* context)
{
g_return_val_if_fail(WEBKIT_IS_WEB_CONTEXT(context), 0);
return 0;
}
#endif
static void addOriginToMap(WebKitSecurityOrigin* origin, HashMap<String, bool>* map, bool allowed)
{
String string = webkitSecurityOriginGetSecurityOriginData(origin).toString();
if (string != "null"_s)
map->set(string, allowed);
}
/**
* webkit_web_context_initialize_notification_permissions:
* @context: the #WebKitWebContext
* @allowed_origins: (element-type WebKitSecurityOrigin): a #GList of security origins
* @disallowed_origins: (element-type WebKitSecurityOrigin): a #GList of security origins
*
* Sets initial desktop notification permissions for the @context.
*
* @allowed_origins and @disallowed_origins must each be #GList of
* #WebKitSecurityOrigin objects representing origins that will,
* respectively, either always or never have permission to show desktop
* notifications. No #WebKitNotificationPermissionRequest will ever be
* generated for any of the security origins represented in
* @allowed_origins or @disallowed_origins. This function is necessary
* because some webpages proactively check whether they have permission
* to display notifications without ever creating a permission request.
*
* This function only affects web processes that have not already been
* created. The best time to call it is when handling
* #WebKitWebContext::initialize-notification-permissions so as to
* ensure that new web processes receive the most recent set of
* permissions.
*
* Since: 2.16
*/
void webkit_web_context_initialize_notification_permissions(WebKitWebContext* context, GList* allowedOrigins, GList* disallowedOrigins)
{
HashMap<String, bool> map;
g_list_foreach(allowedOrigins, [](gpointer data, gpointer userData) {
addOriginToMap(static_cast<WebKitSecurityOrigin*>(data), static_cast<HashMap<String, bool>*>(userData), true);
}, &map);
g_list_foreach(disallowedOrigins, [](gpointer data, gpointer userData) {
addOriginToMap(static_cast<WebKitSecurityOrigin*>(data), static_cast<HashMap<String, bool>*>(userData), false);
}, &map);
context->priv->notificationProvider->setNotificationPermissions(WTFMove(map));
}
/**
* webkit_web_context_send_message_to_all_extensions:
* @context: the #WebKitWebContext
* @message: a #WebKitUserMessage
*
* Send @message to all web process extensions associated to @context.
*
* If @message is floating, it's consumed.
*
* Since: 2.28
*/
void webkit_web_context_send_message_to_all_extensions(WebKitWebContext* context, WebKitUserMessage* message)
{
g_return_if_fail(WEBKIT_IS_WEB_CONTEXT(context));
g_return_if_fail(WEBKIT_IS_USER_MESSAGE(message));
// We sink the reference in case of being floating.
GRefPtr<WebKitUserMessage> adoptedMessage = message;
for (auto& process : context->priv->processPool->processes())
process->send(Messages::WebProcess::SendMessageToWebProcessExtension(webkitUserMessageGetMessage(message)), 0);
}
#if PLATFORM(GTK) && !USE(GTK4)
/**
* webkit_web_context_set_use_system_appearance_for_scrollbars:
* @context: a #WebKitWebContext
* @enabled: value to set
*
* Set the #WebKitWebContext:use-system-appearance-for-scrollbars property.
*
* Since: 2.30
*/
void webkit_web_context_set_use_system_appearance_for_scrollbars(WebKitWebContext* context, gboolean enabled)
{
g_return_if_fail(WEBKIT_IS_WEB_CONTEXT(context));
if (context->priv->useSystemAppearanceForScrollbars == enabled)
return;
context->priv->useSystemAppearanceForScrollbars = enabled;
g_object_notify_by_pspec(G_OBJECT(context), sObjProperties[PROP_USE_SYSTEM_APPEARANCE_FOR_SCROLLBARS]);
if (!context->priv->processPool)
return;
context->priv->processPool->configuration().setUseSystemAppearanceForScrollbars(enabled);
context->priv->processPool->sendToAllProcesses(Messages::WebProcess::SetUseSystemAppearanceForScrollbars(enabled));
}
/**
* webkit_web_context_get_use_system_appearance_for_scrollbars:
* @context: a #WebKitWebContext
*
* Get the #WebKitWebContext:use-system-appearance-for-scrollbars property.
*
* Returns: %TRUE if scrollbars are rendering using the system appearance, or %FALSE otherwise
*
* Since: 2.30
*/
gboolean webkit_web_context_get_use_system_appearance_for_scrollbars(WebKitWebContext* context)
{
g_return_val_if_fail(WEBKIT_IS_WEB_CONTEXT(context), TRUE);
return context->priv->useSystemAppearanceForScrollbars;
}
#endif
/**
* webkit_web_context_get_time_zone_override:
* @context: a #WebKitWebContext
*
* Get the #WebKitWebContext:time-zone-override property.
*
* Since: 2.38
*/
const gchar* webkit_web_context_get_time_zone_override(WebKitWebContext* context)
{
g_return_val_if_fail(WEBKIT_IS_WEB_CONTEXT(context), nullptr);
return context->priv->timeZoneOverride.data();
}
void webkitWebContextInitializeNotificationPermissions(WebKitWebContext* context)
{
g_signal_emit(context, signals[INITIALIZE_NOTIFICATION_PERMISSIONS], 0);
}
#if !ENABLE(2022_GLIB_API)
void webkitWebContextDownloadStarted(WebKitWebContext* context, WebKitDownload* download)
{
g_signal_emit(context, signals[DOWNLOAD_STARTED], 0, download);
}
#endif
GVariant* webkitWebContextInitializeWebProcessExtensions(WebKitWebContext* context)
{
g_signal_emit(context, signals[INITIALIZE_WEB_PROCESS_EXTENSIONS], 0);
return g_variant_new("(msmv)",
context->priv->webProcessExtensionsDirectory.data(),
context->priv->webProcessExtensionsInitializationUserData.get());
}
WebProcessPool& webkitWebContextGetProcessPool(WebKitWebContext* context)
{
g_assert(WEBKIT_IS_WEB_CONTEXT(context));
return *context->priv->processPool;
}
void webkitWebContextCreatePageForWebView(WebKitWebContext* context, WebKitWebView* webView, WebKitUserContentManager* userContentManager, WebKitWebView* relatedView, WebKitWebsitePolicies* defaultWebsitePolicies)
{
auto pageConfiguration = API::PageConfiguration::create();
pageConfiguration->setProcessPool(context->priv->processPool.get());
pageConfiguration->setPreferences(webkitSettingsGetPreferences(webkit_web_view_get_settings(webView)));
pageConfiguration->setRelatedPage(relatedView ? &webkitWebViewGetPage(relatedView) : nullptr);
pageConfiguration->setUserContentController(userContentManager ? webkitUserContentManagerGetUserContentControllerProxy(userContentManager) : nullptr);
pageConfiguration->setControlledByAutomation(webkit_web_view_is_controlled_by_automation(webView));
WebKitWebExtensionMode webExtensionMode = webkit_web_view_get_web_extension_mode(webView);
const char* defaultContentSecurityPolicy = webkit_web_view_get_default_content_security_policy(webView);
if (webExtensionMode == WEBKIT_WEB_EXTENSION_MODE_MANIFESTV3)
pageConfiguration->setContentSecurityPolicyModeForExtension(WebCore::ContentSecurityPolicyModeForExtension::ManifestV3);
else if (webExtensionMode == WEBKIT_WEB_EXTENSION_MODE_MANIFESTV2)
pageConfiguration->setContentSecurityPolicyModeForExtension(WebCore::ContentSecurityPolicyModeForExtension::ManifestV2);
if (defaultContentSecurityPolicy)
pageConfiguration->setOverrideContentSecurityPolicy(String::fromUTF8(defaultContentSecurityPolicy));
WebKitWebsiteDataManager* manager = webkitWebViewGetWebsiteDataManager(webView);
#if !ENABLE(2022_GLIB_API)
if (!manager)
manager = context->priv->websiteDataManager.get();
#endif
pageConfiguration->setWebsiteDataStore(&webkitWebsiteDataManagerGetDataStore(manager));
pageConfiguration->setDefaultWebsitePolicies(webkitWebsitePoliciesGetWebsitePolicies(defaultWebsitePolicies));
webkitWebViewCreatePage(webView, WTFMove(pageConfiguration));
auto& page = webkitWebViewGetPage(webView);
for (auto& it : context->priv->uriSchemeHandlers) {
Ref<WebURLSchemeHandler> handler(*it.value);
page.setURLSchemeHandlerForScheme(WTFMove(handler), it.key);
}
context->priv->webViews.set(page.identifier(), webView);
}
void webkitWebContextWebViewDestroyed(WebKitWebContext* context, WebKitWebView* webView)
{
context->priv->webViews.remove(webkitWebViewGetPage(webView).identifier());
}
WebKitWebView* webkitWebContextGetWebViewForPage(WebKitWebContext* context, WebPageProxy* page)
{
return page ? context->priv->webViews.get(page->identifier()) : nullptr;
}
|