c++gtkwebkitwebkit2-webextension

How to debug WebKit2GTK+ extensions


I am trying to get WebKit2GTK+ Extensions to work, it is a simple extension that will just print into the console when a page is created. Here is my project structure:

-bin
-images
-include
-lib
--webextension
---libwebextension.so
---libwebextension.so.1
---libwebextension.so.1.0
---webextension.cpp
-src
--gtk
---gtk_manager.cpp
--main.cpp

The gtk_manager.cpp files contains the implementation of the header file, only the init() function will matter (will get to that in a little bit)

webextension.cpp

#include <webkit2/webkit-web-extension.h>
#include <iostream>

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)
{
    std::cout << "extension hi\n";
    g_signal_connect (extension, "page-created",
                      G_CALLBACK (web_page_created_callback),
                      NULL);
}

void hi()
{
  g_print("hi");
}

The exportation and dynamic linking during runtime work, as I can call hi() from gtk_manager.cpp's init() method. webkit_web_extension_initialize() is not showing any signs of working/being called, because "extension hi" is not printed into the console.

gtk_manager.cpp (gtk/gtk.h, glib.h, and webkit2/webkit2.h are being included from gtk_manager.h in include folder)

#include "gtk/gtk_manager.h"
#include <iostream>

void initialize_web_extensions(WebKitWebContext*, gpointer);

void GTKManager::init(int argc, char* args[])
{
  g_signal_connect(webkit_web_context_get_default(), "initialize-web-extensions", G_CALLBACK(initialize_web_extensions), NULL);
  gtk_init(&argc, &args);
  /* other code */
}

/* other methods / functions */

void initialize_web_extensions(WebKitWebContext* context, gpointer userData)
{
  static guint uniqueId = 0;
  webkit_web_context_set_web_extensions_directory(context, "/abs/path/to/app/lib/webextension");
  webkit_web_context_set_web_extensions_initialization_user_data(context, g_variant_new_uint32(uniqueId++));
  hi(); // This is from webextension.cpp, it is called successfully
}

I will edit this question if more information is needed to find a solution.

These are the resources I am using:


Solution

  • Since it's C++ it could be due to name mangling. Try to prefix the webkit_web_extension_initialize function with extern "C". For example:

    extern "C" G_MODULE_EXPORT void
    webkit_web_extension_initialize (WebKitWebExtension *extension)
    {
        std::cout << "extension hi\n";
        /* your code */
    }
    

    You can use readelf or objdump to list all exported symbols and see if they have mangled names.