From 01ee1dfd2a1af1f7ccd897fd657f9fa6b8aba3c4 Mon Sep 17 00:00:00 2001 From: Patrick Griffis Date: Sat, 17 Oct 2026 16:43:43 -0500 Subject: [PATCH] [GLib] Don't initialize GStreamer in the Network process for file loads https://bugs.webkit.org/show_bug.cgi?id=327967 Reviewed by NOBODY (OOPS!). For file:// loads, NetworkDataTaskSoup::didGetFileInfo() chooses between the MIME type from the file's extension and the one GIO sniffed from its content with MIMETypeRegistry::canShowMIMEType(). For audio, video and application types that asks the media engines, which initializes GStreamer and reads its registry in the Network process: about 4 MB of memory and 10 ms of CPU for loading a file of an unknown type. Keep audio and video types from the extension as they are, and check other types without the media engines, with the new MIMETypeRegistry::canShowNonMediaMIMEType(). The extension's type is kept rather than the sniffed one because GIO sniffs Ogg files as subtypes such as audio/x-opus+ogg and video/x-theora+ogg, which GStreamer doesn't register. The Network process now release-asserts that it never initializes GStreamer. * Source/WebCore/platform/MIMETypeRegistry.cpp: (WebCore::MIMETypeRegistry::canShowMIMEType): (WebCore::MIMETypeRegistry::canShowNonMediaMIMEType): * Source/WebCore/platform/MIMETypeRegistry.h: * Source/WebCore/platform/graphics/gstreamer/GStreamerCommon.cpp: (WebCore::ensureGStreamerInitializedNonWebProcess): * Source/WebKit/NetworkProcess/soup/NetworkDataTaskSoup.cpp: (WebKit::NetworkDataTaskSoup::didGetFileInfo): --- Source/WebCore/platform/MIMETypeRegistry.cpp | 7 ++++++- Source/WebCore/platform/MIMETypeRegistry.h | 3 +++ .../platform/graphics/gstreamer/GStreamerCommon.cpp | 1 + Source/WebKit/NetworkProcess/soup/NetworkDataTaskSoup.cpp | 3 ++- 4 files changed, 12 insertions(+), 2 deletions(-) diff --git a/Source/WebCore/platform/MIMETypeRegistry.cpp b/Source/WebCore/platform/MIMETypeRegistry.cpp index 849810df668fd..e1cd6179fdd80 100644 --- a/Source/WebCore/platform/MIMETypeRegistry.cpp +++ b/Source/WebCore/platform/MIMETypeRegistry.cpp @@ -624,7 +624,12 @@ bool MIMETypeRegistry::isPDFMIMEType(const String& mimeType) bool MIMETypeRegistry::canShowMIMEType(const String& mimeType) { - if (isSupportedImageMIMEType(mimeType) || isSupportedNonImageMIMEType(mimeType) || isSupportedMediaMIMEType(mimeType)) + return canShowNonMediaMIMEType(mimeType) || isSupportedMediaMIMEType(mimeType); +} + +bool MIMETypeRegistry::canShowNonMediaMIMEType(const String& mimeType) +{ + if (isSupportedImageMIMEType(mimeType) || isSupportedNonImageMIMEType(mimeType)) return true; if (isSupportedJavaScriptMIMEType(mimeType) || isSupportedJSONMIMEType(mimeType)) diff --git a/Source/WebCore/platform/MIMETypeRegistry.h b/Source/WebCore/platform/MIMETypeRegistry.h index de4af675d92fe..c6af28785ca17 100644 --- a/Source/WebCore/platform/MIMETypeRegistry.h +++ b/Source/WebCore/platform/MIMETypeRegistry.h @@ -118,6 +118,9 @@ class MIMETypeRegistry { // isUnsupportedTextMIMEType() returns false. WEBCORE_EXPORT static bool canShowMIMEType(const String& mimeType); + // Like canShowMIMEType(), without isSupportedMediaMIMEType(), which asks the media engines. + WEBCORE_EXPORT static bool canShowNonMediaMIMEType(const String& mimeType); + // Check to see if a MIME type is one where an XML document should be created // rather than an HTML document. WEBCORE_EXPORT static bool isXMLMIMEType(const String& mimeType); diff --git a/Source/WebCore/platform/graphics/gstreamer/GStreamerCommon.cpp b/Source/WebCore/platform/graphics/gstreamer/GStreamerCommon.cpp index ff69778d58932..f60a46b166caa 100644 --- a/Source/WebCore/platform/graphics/gstreamer/GStreamerCommon.cpp +++ b/Source/WebCore/platform/graphics/gstreamer/GStreamerCommon.cpp @@ -443,6 +443,7 @@ Vector extractGStreamerOptionsFromCommandLine() bool ensureGStreamerInitializedNonWebProcess() { RELEASE_ASSERT(!isInWebProcess()); + RELEASE_ASSERT(!isInNetworkProcess()); static std::once_flag onceFlag; static bool isGStreamerInitialized; diff --git a/Source/WebKit/NetworkProcess/soup/NetworkDataTaskSoup.cpp b/Source/WebKit/NetworkProcess/soup/NetworkDataTaskSoup.cpp index 60c49e285b5fc..9ca58dd245a7f 100644 --- a/Source/WebKit/NetworkProcess/soup/NetworkDataTaskSoup.cpp +++ b/Source/WebKit/NetworkProcess/soup/NetworkDataTaskSoup.cpp @@ -1548,7 +1548,8 @@ void NetworkDataTaskSoup::didGetFileInfo(GFileInfo* info) auto mimeTypeFromContent = extractMIMETypeFromMediaType(contentType); // If an application calls g_app_info_get_default_for_type($ext) then Glib will return "application/x-extension-$ext" in mimeTypeFromExtension which WebKit doesn't know how to handle. // So, only prefer mimeTypeFromExtension if that is a mimeType that WebKit recognizes internally. See: https://gitlab.gnome.org/GNOME/glib/-/issues/2511 - if (MIMETypeRegistry::canShowMIMEType(mimeTypeFromExtension) || mimeTypeFromContent.isEmpty()) + bool isMediaType = startsWithLettersIgnoringASCIICase(mimeTypeFromExtension, "audio/"_s) || startsWithLettersIgnoringASCIICase(mimeTypeFromExtension, "video/"_s); + if (isMediaType || MIMETypeRegistry::canShowNonMediaMIMEType(mimeTypeFromExtension) || mimeTypeFromContent.isEmpty()) m_response.setMimeType(WTF::move(mimeTypeFromExtension)); else m_response.setMimeType(WTF::move(mimeTypeFromContent));