[hackers] [surf/surf-webkit2] webext: Exchange fd over webkit messages || Quentin Rameau

From: <git_AT_suckless.org>
Date: Sun, 16 Oct 2022 17:43:22 +0200 (CEST)

commit 665a709b522a6fa18c671f1fc41297603292d0e8
Author: Quentin Rameau <quinq_AT_fifth.space>
AuthorDate: Sun Oct 16 17:39:05 2022 +0200
Commit: Quentin Rameau <quinq_AT_fifth.space>
CommitDate: Sun Oct 16 17:39:05 2022 +0200

    webext: Exchange fd over webkit messages
    
    This is more complex, but webkit2gtk prevents passing file descriptors
    to processes in 2.38.0.

diff --git a/surf.c b/surf.c
index 474c01b..001f776 100644
--- a/surf.c
+++ b/surf.c
_AT_@ -214,6 +214,8 @@ static void downloadstarted(WebKitWebContext *wc, WebKitDownload *d,
                             Client *c);
 static void responsereceived(WebKitDownload *d, GParamSpec *ps, Client *c);
 static void download(Client *c, WebKitURIResponse *r);
+static gboolean viewusrmsgrcv(WebKitWebView *v, WebKitUserMessage *m,
+ gpointer u);
 static void webprocessterminated(WebKitWebView *v,
                                  WebKitWebProcessTerminationReason r,
                                  Client *c);
_AT_@ -1224,6 +1226,8 @@ newview(Client *c, WebKitWebView *rv)
                          G_CALLBACK(permissionrequested), c);
         g_signal_connect(G_OBJECT(v), "ready-to-show",
                          G_CALLBACK(showview), c);
+ g_signal_connect(G_OBJECT(v), "user-message-received",
+ G_CALLBACK(viewusrmsgrcv), c);
         g_signal_connect(G_OBJECT(v), "web-process-terminated",
                          G_CALLBACK(webprocessterminated), c);
 
_AT_@ -1262,14 +1266,6 @@ readsock(GIOChannel *s, GIOCondition ioc, gpointer unused)
 void
 initwebextensions(WebKitWebContext *wc, Client *c)
 {
- GVariant *gv;
-
- if (spair[1] < 0)
- return;
-
- gv = g_variant_new("i", spair[1]);
-
- webkit_web_context_set_web_extensions_initialization_user_data(wc, gv);
         webkit_web_context_set_web_extensions_directory(wc, WEBEXTDIR);
 }
 
_AT_@ -1572,6 +1568,30 @@ titlechanged(WebKitWebView *view, GParamSpec *ps, Client *c)
         updatetitle(c);
 }
 
+gboolean
+viewusrmsgrcv(WebKitWebView *v, WebKitUserMessage *m, gpointer unused)
+{
+ WebKitUserMessage *r;
+ GUnixFDList *gfd;
+ const char *name;
+
+ name = webkit_user_message_get_name(m);
+ if (strcmp(name, "page-created") != 0) {
+ fprintf(stderr, "surf: Unknown UserMessage: %s\n", name);
+ return TRUE;
+ }
+
+ if (spair[1] < 0)
+ return TRUE;
+
+ gfd = g_unix_fd_list_new_from_array(&spair[1], 1);
+ r = webkit_user_message_new_with_fd_list("surf-pipe", NULL, gfd);
+
+ webkit_user_message_send_reply(m, r);
+
+ return TRUE;
+}
+
 void
 mousetargetchanged(WebKitWebView *v, WebKitHitTestResult *h, guint modifiers,
     Client *c)
diff --git a/webext-surf.c b/webext-surf.c
index d087219..69fd04b 100644
--- a/webext-surf.c
+++ b/webext-surf.c
_AT_@ -87,15 +87,29 @@ readsock(GIOChannel *s, GIOCondition c, gpointer unused)
         return TRUE;
 }
 
-G_MODULE_EXPORT void
-webkit_web_extension_initialize_with_user_data(WebKitWebExtension *e,
- const GVariant *gv)
+static void
+pageusermessagereply(GObject *o, GAsyncResult *r, gpointer page)
 {
+ WebKitUserMessage *m;
+ GUnixFDList *gfd;
         GIOChannel *gchansock;
+ const char *name;
+ int nfd;
 
- webext = e;
+ m = webkit_web_page_send_message_to_view_finish(page, r, NULL);
+ name = webkit_user_message_get_name(m);
+ if (strcmp(name, "surf-pipe") != 0) {
+ fprintf(stderr, "webext-surf: Unknown User Reply: %s\n", name);
+ return;
+ }
 
- g_variant_get(gv, "i", &sock);
+ gfd = webkit_user_message_get_fd_list(m);
+ if ((nfd = g_unix_fd_list_get_length(gfd)) != 1) {
+ fprintf(stderr, "webext-surf: Too many file-descriptors: %d\n", nfd);
+ return;
+ }
+
+ sock = g_unix_fd_list_get(gfd, 0, NULL);
 
         gchansock = g_io_channel_unix_new(sock);
         g_io_channel_set_encoding(gchansock, NULL, NULL);
_AT_@ -104,3 +118,21 @@ webkit_web_extension_initialize_with_user_data(WebKitWebExtension *e,
         g_io_channel_set_close_on_unref(gchansock, TRUE);
         g_io_add_watch(gchansock, G_IO_IN, readsock, NULL);
 }
+
+void
+pagecreated(WebKitWebExtension *e, WebKitWebPage *p, gpointer unused)
+{
+ WebKitUserMessage *msg;
+
+ msg = webkit_user_message_new("page-created", NULL);
+ webkit_web_page_send_message_to_view(p, msg, NULL, pageusermessagereply, p);
+}
+
+G_MODULE_EXPORT void
+webkit_web_extension_initialize(WebKitWebExtension *e)
+{
+ webext = e;
+
+ g_signal_connect(G_OBJECT(e), "page-created",
+ G_CALLBACK(pagecreated), NULL);
+}
Received on Sun Oct 16 2022 - 17:43:22 CEST

This archive was generated by hypermail 2.3.0 : Sun Oct 16 2022 - 17:48:36 CEST