WebKitGTK Web Process Extension not loading

Hello,

This is a cross post with WebKit’s mailing list where I haven’t gotten any help (yet), hoping someone here might point me in the right direction. Original message is here (but I’m going to copy/paste and add some more info below): [webkit-help] [WebKitGTK] Issue with web process extensions

I’m on Ubuntu Linux 24.04 Noble Numbat, with the following packages installed:

  • libwebkitgtk-6.0-4 2.46.1-0ubuntu0.24.04.1 amd64
  • libwebkitgtk-6.0-dev 2.46.1-0ubuntu0.24.04.1 amd64

I’m trying to enable web process extensions in a mostly C++ written application. I’ve spent some hours going through the documentation and searching over the internet for a solution, but I don’t find any issue with my code (although, I may be overlooking a small detail).

The main symptom of my issue is that WebKit looks like it never loads my plugin (the extension’s g_print line’s output never appears on standard output of terminal).

Below, there’s a minimal reproducing sample. Is anything wrong with my code? If not, how should I proceed to figure out where the issue is?

Based on the code below, here’s a few things I’ve tried already:

  • moving the libextension.so file to another path than ./ or specifying an absolute path to webkit_web_context_set_web_process_extensions_directory.
  • writing a tool using GModule functions that loads libextension.so and checks webkit_web_process_extension_initialize symbol can be reached with g_module_symbol().
  • running inside gdb and placing a breakpoint on WebKit::WebProcessExtensionManager::initialize from Source/WebKit/WebProcess/InjectedBundle/API/glib/WebProcessExtensionManager.cpp. That breakpoint is never triggered.
  • building WebKitGTK from source following instructions in Readme.md file. Even with 30G memory, compiler runs out of memory when trying to compile “unified source” files.

CMakeLists.txt:

cmake_minimum_required(VERSION 3.28.3)

project(
        IssueSample
        VERSION 1.0.0
        LANGUAGES C CXX)

find_package(PkgConfig REQUIRED)
pkg_check_modules(WebkitGTK REQUIRED IMPORTED_TARGET glib-2.0>=2.74 gtk4>=4.14.2 webkitgtk-6.0)

add_executable(browser main.cpp)
target_link_libraries(browser PkgConfig::WebkitGTK)

add_library(extension MODULE extension.c)
target_link_libraries(extension PkgConfig::WebkitGTK)

extension.c:

#include <gtk/gtk.h>
#include <webkit/webkit-web-process-extension.h>

#ifdef __cplusplus
#error Must be compiled as C
#endif

static void web_page_created_cb(WebKitWebProcessExtension *extension, WebKitWebPage *web_page, gpointer /* unused */)
{
        g_print("Page %ld created for %s with table\n", webkit_web_page_get_id(web_page),
                webkit_web_page_get_uri(web_page));
}

G_MODULE_EXPORT void webkit_web_process_extension_initialize(WebKitWebProcessExtension *extension)
{
        g_print("Inside webkit_web_extension_initialize %p\n", extension);
        g_signal_connect(extension, "page-created", G_CALLBACK(web_page_created_cb), NULL);
}

main.cpp:

#include <gtk/gtk.h>
#include <webkit/webkit.h>

void initialize_web_extensions(WebKitWebContext *context, gpointer /*unused*/)
{
        webkit_web_context_set_web_process_extensions_directory(context, "./");
}

int main()
{
        gtk_init();

        g_signal_connect(webkit_web_context_get_default(), "initialize-web-process-extensions",
                G_CALLBACK(initialize_web_extensions), NULL);

        GtkWindow * window = GTK_WINDOW(gtk_window_new());

        WebKitWebView * web_view = WEBKIT_WEB_VIEW(webkit_web_view_new());

        webkit_web_view_load_uri(web_view, "https://linux.org/");

        gtk_window_set_child(window, GTK_WIDGET(web_view));

        gtk_window_present(window);

        while (g_list_model_get_n_items(gtk_window_get_toplevels()) > 0)
        {
                g_main_context_iteration(NULL, TRUE);
        }
}

I don’t see any obvious problems here, so I actually tested this and was able to reproduce the problem using Fedora’s system WebKitGTK. I decided next step was to add printfs into WebKit to see what is going wrong, so I switched from system WebKitGTK to jhbuild.

And then it worked.

Now I am mystified. Pretty sure there is some sort of WebKit bug. Unfortunately, the fact that it actually works in my test environment is probably going to make it harder to figure out what is wrong.

OK, the problem is the directory does not exist in the sandbox. You need to also call webkit_web_context_add_path_to_sandbox(). We should add a warning about this to our documentation.

Note: it won’t accept ./ or ~/ so you’ll have to specify a normal expected install location for the extension library.

1 Like

https://bugs.webkit.org/show_bug.cgi?id=282389

Thanks a lot for your time @mcatanzaro and this great help!

I tried your suggestion; this was indeed the root cause of my issue.

I have two closely related questions left:

  1. If I understand correctly, the path passed to webkit_web_context_set_web_process_extensions_directory needs to be a subpath to one of those passed to webkit_web_context_add_path_to_sandbox, right?
  2. I have noticed a few other issues with the documentation of Web Process Extensions for GTK. It looks to me like not everything was updated between GTK 3 and 4. I can either contribute on that bug’s page. Or open different bugs. Or I could contribute documentation changes via a PR. What would be best?

Not quite, because most reasonable installation locations for your web process extension are already going to be mounted in the sandbox by default, e.g. all of /usr/lib and /usr/lib64, which is normally where you’ll be installing to.

But if you’re installing to a location that’s not included in the sandbox, such as your home directory, then yes.

Open one or more additional bug reports. Bonus points for sending a pull request, but the pull request has to link to an existing bug and the contribution rules are slightly finicky. It’s not too hard to create a pull request, just got to look over the guidelines first.