WebktiGtk not loading .so extensions

335 views Asked by At

I can't seem to get WebkitGTK (2.22.4) to load the .so web extensions for some reason and I am not quite sure why this is the case. I have the following setup (somewhat stripped down) WebkitGtk itself works fine as it loads the page and it fires the initialize-web-extensions signal so the directory is being set.

However, even though the .so file of the extension is located in the working directory (where the executable is also located) it never seems to get loaded. Perhaps I am missing something.

I could also not find that much documentation on specific implementation of this apart from a few very stripped examples. (Which I've looked at, but haven't gotten any further as the example code is exactly the same).

Hoping anyone here has an idea.

main.cpp

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


WebKitWebContext* context;

void initialize_web_extensions(WebKitWebContext* context, gpointer user_data) {
    g_print("[webkit2 web-extensions] initialize-web-extensions\n");
    webkit_web_context_set_web_extensions_directory(context, ".");
    return;
}

int main(int argc, char * argv[])
{
    exec_dir = dirname(realpath(argv[0], NULL));

    gtk_init(&argc, &argv);

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

    GtkWidget* window_widget;
    GtkWindow* window;
    window_widget = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    window = GTK_WINDOW(window_widget);

    WebKitWebView* webview = WEBKIT_WEB_VIEW(webkit_web_view_new());
    GtkWidget* webview_widget = GTK_WIDGET(webview);

    webkit_web_view_load_uri(webview, "https://google.com");
    gtk_container_add(GTK_CONTAINER(window_widget), webview_widget);

    gtk_widget_show_all(window_widget);

    gtk_main();

    return 0;
}

web_extension.cpp

static void
web_page_created_callback (WebKitWebExtension *extension,
                           WebKitWebPage      *web_page,
                           gpointer            user_data)
{
    g_print ("Page %d created for %s\n",
             webkit_web_page_get_id (web_page),
             webkit_web_page_get_uri (web_page));
}

G_MODULE_EXPORT void
webkit_web_extension_initialize (WebKitWebExtension *extension)
{
    g_signal_connect (extension, "page-created",
                      G_CALLBACK (web_page_created_callback),
                      NULL);
}

CMakeLists.txt

set(APP_SRCS main.cpp)
set(EXT_SRCS web_extension.cpp)

# Target executable names.
set(TARGET "app")
set(EXT_TARGET "extension")

# find gtk
PKG_CHECK_MODULES(GTK3 REQUIRED gtk+-3.0)
include_directories(${GTK3_INCLUDE_DIRS})
link_directories(${GTK3_LIBRARY_DIRS})
add_definitions(${GTK3_CFLAGS_OTHER})

# include webkitgtk
PKG_CHECK_MODULES(WEBKIT REQUIRED webkit2gtk-4.0 webkit2gtk-web-extension-4.0)
include_directories(${WEBKIT_INCLUDE_DIRS})
link_directories(${WEBKIT_LIBRARY_DIRS})
add_definitions(${WEBKIT_CFLAGS_OTHER})

# add extension
add_library(${EXT_TARGET} SHARED ${EXT_SRCS})
target_link_libraries(${EXT_TARGET} ${WEBKIT_LIBRARIES})

# Executable target.
add_executable(${TARGET} ${APP_SRCS})
SET_EXECUTABLE_TARGET_PROPERTIES(${TARGET})
target_link_libraries(${TARGET} ${EXT_TARGET})
target_link_libraries(${TARGET} ${GTK3_LIBRARIES})
target_link_libraries(${TARGET} ${WEBKIT_LIBRARIES})
1

There are 1 answers

0
Liam Martens On BEST ANSWER

Apparently it had to do with the symbol mangling in the C++ compiler. Had to add

external "C" {
    ...
}

to explicitly disable mangling so the public symbols could be found by the main application.