[gnome-builder] devhelp: Use libportal to open remote URIs



commit 9f550fadb0213a5e7426ab1dbf44d73e8771f90d
Author: vanadiae <vanadiae35 gmail com>
Date:   Tue Aug 3 18:27:35 2021 +0200

    devhelp: Use libportal to open remote URIs
    
    Using g_app_info_launch_default_for_uri() didn't work quite as expected
    as it first tried opening from inside the sandbox, which opened Builder
    for HTML pages. That's not what we want here as it's better if it's
    opened in the web browser instead. It also made Builder crash because
    it doesn't support gvfs well. See https://gitlab.gnome.org/GNOME/glib/-/issues/1960
    
    Fixes aa4e56d47d3e665a2bce3b3f78f4ccb80d496eb7

 src/plugins/devhelp/gbp-devhelp-page.c | 42 +++++++++++++++++++++++++++++-----
 src/plugins/devhelp/meson.build        |  5 ++++
 2 files changed, 41 insertions(+), 6 deletions(-)
---
diff --git a/src/plugins/devhelp/gbp-devhelp-page.c b/src/plugins/devhelp/gbp-devhelp-page.c
index 998d21e01..d36a34e71 100644
--- a/src/plugins/devhelp/gbp-devhelp-page.c
+++ b/src/plugins/devhelp/gbp-devhelp-page.c
@@ -24,6 +24,9 @@
 #include <glib/gi18n.h>
 #include <webkit2/webkit2.h>
 
+#include <libportal/portal.h>
+#include <libportal/portal-gtk3.h>
+
 #include "gbp-devhelp-page.h"
 #include "gbp-devhelp-search.h"
 
@@ -251,6 +254,37 @@ gbp_devhelp_page_class_init (GbpDevhelpPageClass *klass)
   g_type_ensure (WEBKIT_TYPE_WEB_VIEW);
 }
 
+static void
+on_uri_opened_with_portal_cb (GObject      *source_object,
+                              GAsyncResult *res,
+                              gpointer      user_data)
+{
+  XdpPortal *portal = (XdpPortal *)source_object;
+  g_autoptr(GError) error = NULL;
+
+  g_assert (XDP_IS_PORTAL (portal));
+
+  if (!xdp_portal_open_uri_finish (portal, res, &error))
+    g_warning ("Couldn't open URI with portal, from devhelp's webview: %s", error->message);
+}
+
+/* We can't use g_app_info_launch_default_for_uri() because of 
https://gitlab.gnome.org/GNOME/glib/-/issues/1960
+ * Builder gets opened for HTML pages if we do so, and crashes because gvfs support in Builder is not really 
working.
+ */
+static void
+open_uri_with_portal (GtkWidget  *widget,
+                      const char *uri)
+{
+  g_autoptr(XdpPortal) portal = xdp_portal_new ();
+  XdpParent *parent = xdp_parent_new_gtk (GTK_WINDOW (gtk_widget_get_ancestor (widget, GTK_TYPE_WINDOW)));
+
+  xdp_portal_open_uri (portal, parent,
+                       uri, XDP_OPEN_URI_FLAG_NONE,
+                       NULL,
+                       on_uri_opened_with_portal_cb, NULL);
+  xdp_parent_free (parent);
+}
+
 static gboolean
 webview_decide_policy_cb (WebKitWebView           *web_view,
                           WebKitPolicyDecision    *decision,
@@ -299,12 +333,8 @@ webview_decide_policy_cb (WebKitWebView           *web_view,
       else
         {
           if (launch_in_browser)
-            {
-              GAppLaunchContext *launch_ctx = G_APP_LAUNCH_CONTEXT (gdk_display_get_app_launch_context 
(gdk_display_get_default ()));
-              g_app_info_launch_default_for_uri (webkit_uri_request_get_uri (request),
-                                                 launch_ctx,
-                                                 NULL);
-            }
+            open_uri_with_portal (GTK_WIDGET (web_view), webkit_uri_request_get_uri (request));
+
           webkit_policy_decision_ignore (decision);
           return TRUE;
         }
diff --git a/src/plugins/devhelp/meson.build b/src/plugins/devhelp/meson.build
index 11d24bdb0..fb01be306 100644
--- a/src/plugins/devhelp/meson.build
+++ b/src/plugins/devhelp/meson.build
@@ -4,8 +4,13 @@ if not libwebkit_dep.found()
   error('WebKit must be enabled for devhelp support')
 endif
 
+if not libportal_dep.found()
+  error('libportal is required for devhelp plugin support')
+endif
+
 plugins_deps += [
   dependency('libdevhelp-3.0', version: '>=3.25.1'),
+  libportal_dep,
 ]
 
 plugins_sources += files([


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]