aperezdc / webkit2gtk-python-webextension-example

Example small C shim to load a Python script as a WebKit2 WebExtension
15 stars 4 forks source link

Passing user data #1

Closed jogc closed 8 years ago

jogc commented 8 years ago

Hello! Im creating a restricted web browser in Python and while waiting for integrated support for Python extensions as per Webkit bug #140745, this example has been very useful for me. One thing I can't get to work though is passing user data. Well, I can pass it, but the C loader complains and I can't retrieve it in the extension. If I simply take your browse-with-extension and make this addition:

--- browse-with-extension.orig 2015-11-15 09:40:55.113134204 +0100 +++ browse-with-extension 2015-11-15 09:40:39.737057980 +0100 @@ -15,6 +15,7 @@

ctx = WebKit2.WebContext.get_default() ctx.set_web_extensions_directory(mydir) +ctx.set_web_extensions_initialization_user_data(GLib.Variant.new_string("hello"))

wnd = Gtk.Window() web = WebKit2.WebView.new_with_context(ctx)

I get this when I run it:

sys:1: Warning: g_boxed_copy: assertion 'G_TYPE_IS_BOXED (boxed_type)' failed initialize: extension = <WebExtension object at 0xacb45374 (WebKitWebExtension at 0x9bdeee0)> /usr/lib/python3/dist-packages/gi/overrides/GLib.py:225: Warning: g_variantclassify: assertion 'value != NULL' failed return self.print(True) ** GLib:ERROR:/build/buildd/glib2.0-2.40.2/./glib/gvariant.c:2556:g_variant_print_string: code should not be reached

Same result if I try to pass an integer instead. This is with Webkit 2.6.5, Python 3.4.0, GLib 2.40.2. I'm almost completely new to GTK/Glib/Gobject-programming in Python, so, yeah... Is this supposed to work like this or am I doing it wrong? At the moment I'm working around this by using environment variables, but thats ugly. Thanks

nhoad commented 8 years ago

Hi! Let me know if you want me to submit a pull request, but I just took a look at this, and here's a patch that gets it working for me:

diff --git a/browse-with-extension b/browse-with-extension
index 06106f5..055ee77 100755
--- a/browse-with-extension
+++ b/browse-with-extension
@@ -6,7 +6,7 @@
 #
 # Distributed under terms of the MIT license.

-from gi.repository import WebKit2, Gtk
+from gi.repository import WebKit2, Gtk, GLib
 from os import path
 import sys

@@ -15,6 +15,7 @@ print("Extension directory:", mydir)

 ctx = WebKit2.WebContext.get_default()
 ctx.set_web_extensions_directory(mydir)
+ctx.set_web_extensions_initialization_user_data(GLib.Variant('(si)', ('test string', 5)))

 wnd = Gtk.Window()
 web = WebKit2.WebView.new_with_context(ctx)
diff --git a/pythonloader.c b/pythonloader.c
index f110c77..2ed52b2 100644
--- a/pythonloader.c
+++ b/pythonloader.c
@@ -70,9 +70,9 @@ webkit_web_extension_initialize_with_user_data (WebKitWebExtension *extension,

     PyObject py_auto *py_extension = pygobject_new (G_OBJECT (extension));
     PyObject py_auto *py_extra_args = pyg_boxed_new (G_TYPE_VARIANT,
-                                                     (gpointer) user_data,
-                                                     TRUE,
-                                                     TRUE);
+                                                     g_variant_ref(user_data),
+                                                     FALSE,
+                                                     FALSE);

     PyObject py_auto *py_func_args = PyTuple_New (2);
     PyTuple_SetItem (py_func_args, 0, py_extension);
aperezdc commented 8 years ago

My apologies for taking so much time in reviewing this...

@nathan-hoad: From the looks of it, your patch is almost correct: it is just missing a g_variant_unref() after calling the call to the initialize() function in extension.py returns — to avoid leaking the GVariant. I will double check this (I am writing from the top of my head right now) and then I will be committing your patch with a couple of changes. Thanks a lot for figuring out how to fix the issue :+1:

aperezdc commented 8 years ago

@jogc @nathan-hoad: I have committed the fix after checking that the g_varian_unref() I was telling about is not needed in the end — pyg_boxed_new() arranges for that to be done. Thanks to you both for reporting and the fix.