Skip to content

Commit

Permalink
Updating everything to the WebKit API version 2.42
Browse files Browse the repository at this point in the history
  • Loading branch information
VeithMetro committed Oct 23, 2023
1 parent 7e1149d commit 3fbe86d
Showing 1 changed file with 69 additions and 10 deletions.
79 changes: 69 additions & 10 deletions WebKitBrowser/WebKitImplementation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ WK_EXPORT void WKPreferencesSetPageCacheEnabled(WKPreferencesRef preferences, bo
#if !WEBKIT_GLIB_API
#define HAS_MEMORY_PRESSURE_SETTINGS_API 0
#else
#define HAS_MEMORY_PRESSURE_SETTINGS_API WEBKIT_CHECK_VERSION(2, 38, 0)
#define HAS_MEMORY_PRESSURE_SETTINGS_API WEBKIT_CHECK_VERSION(2, 42, 0)
#endif


Expand Down Expand Up @@ -593,6 +593,7 @@ static GSourceFuncs _handlerIntervention =
, IndexedDBEnabled(false)
, IndexedDBPath()
, IndexedDBSize()
, IndexedDBTotalSize()
, Secure(false)
, InjectedBundle()
, Transparent(false)
Expand Down Expand Up @@ -661,6 +662,7 @@ static GSourceFuncs _handlerIntervention =
Add(_T("indexeddbenabled"), &IndexedDBEnabled);
Add(_T("indexeddbpath"), &IndexedDBPath);
Add(_T("indexeddbsize"), &IndexedDBSize);
Add(_T("indexeddbtotalsize"), &IndexedDBTotalSize);
Add(_T("secure"), &Secure);
Add(_T("injectedbundle"), &InjectedBundle);
Add(_T("transparent"), &Transparent);
Expand Down Expand Up @@ -735,7 +737,8 @@ static GSourceFuncs _handlerIntervention =
Core::JSON::DecUInt16 LocalStorageSize;
Core::JSON::Boolean IndexedDBEnabled;
Core::JSON::String IndexedDBPath;
Core::JSON::DecUInt16 IndexedDBSize; // [KB]
Core::JSON::Double IndexedDBSize; // [percentage of volume space for each domain]
Core::JSON::Double IndexedDBTotalSize; // [percentage of volume space for all domains]
Core::JSON::Boolean Secure;
Core::JSON::String InjectedBundle;
Core::JSON::Boolean Transparent;
Expand Down Expand Up @@ -1055,7 +1058,6 @@ static GSourceFuncs _handlerIntervention =
auto* data = new SetUserAgentData(this, useragent);

TRACE(Trace::Information, (_T("New user agent: %s"), useragent.c_str()));

g_main_context_invoke_full(
_context,
G_PRIORITY_DEFAULT,
Expand Down Expand Up @@ -1337,7 +1339,14 @@ static GSourceFuncs _handlerIntervention =
WebKitImplementation* object = std::get<0>(data);
auto& script = std::get<1>(data);
#ifdef WEBKIT_GLIB_API

#if WEBKIT_CHECK_VERSION(2, 40, 0)
// @length: length of @script, or -1 if @script is a nul-terminated string
webkit_web_view_evaluate_javascript(object->_view, script.c_str(), -1, nullptr, nullptr, nullptr, nullptr, nullptr);
#else
webkit_web_view_run_javascript(object->_view, script.c_str(), nullptr, nullptr, nullptr);
#endif

#else
auto scriptRef = WKStringCreateWithUTF8CString(script.c_str());
WKPageRunJavaScriptInMainFrame(object->_page, scriptRef, nullptr, [](WKSerializedScriptValueRef, WKErrorRef, void*){});
Expand Down Expand Up @@ -1522,6 +1531,25 @@ static GSourceFuncs _handlerIntervention =
#ifdef WEBKIT_GLIB_API
WebKitWebContext* context = webkit_web_view_get_context(_view);
WebKitCookieManager* manager = webkit_web_context_get_cookie_manager(context);
#if WEBKIT_CHECK_VERSION(2, 42, 0)
webkit_cookie_manager_get_all_cookies(manager, NULL, [](GObject* object, GAsyncResult* result, gpointer user_data) {
GList* cookies_list = webkit_cookie_manager_get_all_cookies_finish(WEBKIT_COOKIE_MANAGER(object), result, nullptr);

std::vector<std::string> cookieVector;
cookieVector.reserve(g_list_length(cookies_list));
for (GList* it = cookies_list; it != NULL; it = g_list_next(it)) {
SoupCookie* soupCookie = (SoupCookie*)it->data;
gchar *cookieHeader = soup_cookie_to_set_cookie_header(soupCookie);
cookieVector.push_back(cookieHeader);
g_free(cookieHeader);
}

WebKitImplementation& browser = *static_cast< WebKitImplementation*>(user_data);
browser._adminLock.Lock();
browser._cookieJar.SetCookies(std::move(cookieVector));
browser._adminLock.Unlock();
}, this);
#else
webkit_cookie_manager_get_cookie_jar(manager, NULL, [](GObject* object, GAsyncResult* result, gpointer user_data) {
GList* cookies_list = webkit_cookie_manager_get_cookie_jar_finish(WEBKIT_COOKIE_MANAGER(object), result, nullptr);

Expand All @@ -1539,6 +1567,7 @@ static GSourceFuncs _handlerIntervention =
browser._cookieJar.SetCookies(std::move(cookieVector));
browser._adminLock.Unlock();
}, this);
#endif
#else
static const auto toSoupCookie = [](WKCookieRef cookie) -> SoupCookie*
{
Expand Down Expand Up @@ -1640,7 +1669,11 @@ static GSourceFuncs _handlerIntervention =

WebKitWebContext* context = webkit_web_view_get_context(_view);
WebKitCookieManager* manager = webkit_web_context_get_cookie_manager(context);
#if WEBKIT_CHECK_VERSION(2, 42, 0)
webkit_cookie_manager_replace_cookies(manager, g_list_reverse(cookies_list), nullptr, nullptr, nullptr);
#else
webkit_cookie_manager_set_cookie_jar(manager, g_list_reverse(cookies_list), nullptr, nullptr, nullptr);
#endif

g_list_free_full(cookies_list, reinterpret_cast<GDestroyNotify>(soup_cookie_free));
#else
Expand Down Expand Up @@ -2877,11 +2910,6 @@ static GSourceFuncs _handlerIntervention =
}
g_mkdir_with_parents(indexedDBPath, 0700);

uint64_t indexedDBSizeBytes = 0; // No limit by default, use WebKit defaults (1G at the moment of writing)
if (_config.IndexedDBSize.IsSet() && _config.IndexedDBSize.Value() != 0) {
indexedDBSizeBytes = _config.IndexedDBSize.Value() * 1024;
}

#if HAS_MEMORY_PRESSURE_SETTINGS_API
if ((_config.Memory.IsSet() == true) && (_config.Memory.NetworkProcessSettings.IsSet() == true)) {
WebKitMemoryPressureSettings* memoryPressureSettings = webkit_memory_pressure_settings_new();
Expand All @@ -2895,19 +2923,46 @@ static GSourceFuncs _handlerIntervention =
webkit_memory_pressure_settings_free(memoryPressureSettings);
}
#endif

#if WEBKIT_CHECK_VERSION(2, 42, 0)
double originStorageRatio = -1.0; // -1.0 means WebKit will use the default quota (1GB)
if (_config.IndexedDBSize.IsSet() && _config.IndexedDBSize.Value() != -1.0) {
originStorageRatio = _config.IndexedDBSize.Value();
}

double totalStorageRatio = -1.0; // -1.0 means there's no limit for the total storage
if (_config.IndexedDBTotalSize.IsSet() && _config.IndexedDBTotalSize.Value() != -1.0) {
totalStorageRatio = _config.IndexedDBTotalSize.Value();
}

auto* websiteDataManager = webkit_website_data_manager_new(
"local-storage-directory", wpeStoragePath,
"disk-cache-directory", wpeDiskCachePath,
"local-storage-quota", localStorageDatabaseQuotaInBytes,
"indexeddb-directory", indexedDBPath,
"origin-storage-ratio", originStorageRatio,
"total-storage-ratio", totalStorageRatio,
nullptr);
#else
uint64_t indexedDBSizeBytes = 0; // No limit by default, use WebKit defaults (1G at the moment of writing)
if (_config.IndexedDBSize.IsSet() && _config.IndexedDBSize.Value() != -1.0) {
indexedDBSizeBytes = static_cast<uint64_t>(localStorageDatabaseQuotaInBytes * _config.IndexedDBSize.Value());
}

auto* websiteDataManager = webkit_website_data_manager_new(
"local-storage-directory", wpeStoragePath,
"disk-cache-directory", wpeDiskCachePath,
"local-storage-quota", localStorageDatabaseQuotaInBytes,
"indexeddb-directory", indexedDBPath,
"per-origin-storage-quota", indexedDBSizeBytes,
nullptr);
#endif
g_free(wpeStoragePath);
g_free(wpeDiskCachePath);
g_free(indexedDBPath);

#if HAS_MEMORY_PRESSURE_SETTINGS_API
if ((_config.Memory.IsSet() == true) && (_config.Memory.WebProcessSettings.IsSet() == true))
if ((_config.Memory.IsSet() == true) && (_config.Memory.WebProcessSettings.IsSet() == true)) {
WebKitMemoryPressureSettings* memoryPressureSettings = webkit_memory_pressure_settings_new();
if (_config.Memory.WebProcessSettings.Limit.IsSet() == true) {
webkit_memory_pressure_settings_set_memory_limit(memoryPressureSettings, _config.Memory.WebProcessSettings.Limit.Value());
Expand Down Expand Up @@ -2964,7 +3019,11 @@ static GSourceFuncs _handlerIntervention =
}

if (!_config.CertificateCheck) {
#if WEBKIT_CHECK_VERSION(2, 32, 0)
webkit_website_data_manager_set_tls_errors_policy(webkit_web_context_get_website_data_manager(wkContext), WEBKIT_TLS_ERRORS_POLICY_IGNORE);
#else
webkit_web_context_set_tls_errors_policy(wkContext, WEBKIT_TLS_ERRORS_POLICY_IGNORE);
#endif
}

auto* languages = static_cast<char**>(g_new0(char*, _config.Languages.Length() + 1));
Expand Down Expand Up @@ -3028,7 +3087,7 @@ static GSourceFuncs _handlerIntervention =

// Allow mixed content.
bool enableWebSecurity = _config.Secure.Value();
#if WEBKIT_CHECK_VERSION(2, 38, 0)
#if WEBKIT_CHECK_VERSION(2, 40, 0)
g_object_set(G_OBJECT(preferences),
"disable-web-security", !enableWebSecurity,
"allow-running-of-insecure-content", !enableWebSecurity,
Expand Down

0 comments on commit 3fbe86d

Please sign in to comment.