[glib/portal: 10/17] Add a portalized proxy resolver implementation



commit 4521e5c40d05741b347bf259f2f47f007c19a98b
Author: Matthias Clasen <mclasen redhat com>
Date:   Wed Jun 22 07:03:24 2016 -0400

    Add a portalized proxy resolver implementation
    
    The backend for this lives in xdg-desktop-portal, and is in turn
    using GProxyResolver.

 gio/Makefile.am                              |   14 ++-
 gio/giomodule.c                              |    2 +
 gio/gproxyresolverportal.c                   |  152 ++++++++++++++++++++++++++
 gio/gproxyresolverportal.h                   |   47 ++++++++
 gio/org.freedesktop.portal.ProxyResolver.xml |   29 +++++
 5 files changed, 240 insertions(+), 4 deletions(-)
---
diff --git a/gio/Makefile.am b/gio/Makefile.am
index dd50399..9ed4466 100644
--- a/gio/Makefile.am
+++ b/gio/Makefile.am
@@ -341,23 +341,29 @@ endif
 xdp_dbus_built_sources = xdp-dbus.c xdp-dbus.h
 BUILT_SOURCES += $(xdp_dbus_built_sources)
 
-EXTRA_DIST += org.freedesktop.portal.NetworkMonitor.xml
+portal_interfaces = \
+       org.freedesktop.portal.NetworkMonitor.xml \
+       org.freedesktop.portal.ProxyResolver.xml \
+       $(NULL)
+
+EXTRA_DIST += $(portal_interfaces)
 
-$(xdp_dbus_built_sources) : $(srcdir)/org.freedesktop.portal.NetworkMonitor.xml
+$(xdp_dbus_built_sources) : $(portal_interfaces)
        $(AM_V_GEN) UNINSTALLED_GLIB_SRCDIR=$(top_srcdir)               \
                UNINSTALLED_GLIB_BUILDDIR=$(top_builddir)               \
                $(PYTHON) $(srcdir)/gdbus-2.0/codegen/gdbus-codegen.in  \
                --interface-prefix org.freedesktop.portal.              \
                --c-namespace Xdp                                       \
                --generate-c-code $(builddir)/xdp-dbus                  \
-               $(srcdir)/org.freedesktop.portal.NetworkMonitor.xml     \
-               $(NULL)
+               $^
 
 portal_sources = \
        gportalsupport.c                \
        gportalsupport.h                \
        gnetworkmonitorportal.c         \
        gnetworkmonitorportal.h         \
+       gproxyresolverportal.c          \
+       gproxyresolverportal.h          \
        $(xdp_dbus_built_sources)       \
        $(NULL)
 
diff --git a/gio/giomodule.c b/gio/giomodule.c
index f84e174..b181e84 100644
--- a/gio/giomodule.c
+++ b/gio/giomodule.c
@@ -903,6 +903,7 @@ extern GType _g_win32_volume_monitor_get_type (void);
 extern GType _g_winhttp_vfs_get_type (void);
 
 extern GType _g_dummy_proxy_resolver_get_type (void);
+extern GType g_proxy_resolver_portal_get_type (void);
 extern GType _g_dummy_tls_backend_get_type (void);
 extern GType g_network_monitor_base_get_type (void);
 extern GType g_network_monitor_portal_get_type (void);
@@ -1124,6 +1125,7 @@ _g_io_modules_ensure_loaded (void)
 #endif
       g_type_ensure (_g_local_vfs_get_type ());
       g_type_ensure (_g_dummy_proxy_resolver_get_type ());
+      g_type_ensure (g_proxy_resolver_portal_get_type ());
       g_type_ensure (_g_http_proxy_get_type ());
       g_type_ensure (_g_https_proxy_get_type ());
       g_type_ensure (_g_socks4a_proxy_get_type ());
diff --git a/gio/gproxyresolverportal.c b/gio/gproxyresolverportal.c
new file mode 100644
index 0000000..e2c3caa
--- /dev/null
+++ b/gio/gproxyresolverportal.c
@@ -0,0 +1,152 @@
+/* GIO - GLib Input, Output and Streaming Library
+ *
+ * Copyright 2016 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, see
+ * <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+
+#include "xdp-dbus.h"
+#include "giomodule-priv.h"
+#include "gportalsupport.h"
+#include "gproxyresolverportal.h"
+
+struct _GProxyResolverPortal {
+  GObject parent_instance;
+
+  XdpProxyResolver *resolver;
+};
+
+static void g_proxy_resolver_portal_iface_init (GProxyResolverInterface *iface);
+
+G_DEFINE_TYPE_WITH_CODE (GProxyResolverPortal, g_proxy_resolver_portal, G_TYPE_OBJECT,
+                         G_IMPLEMENT_INTERFACE (G_TYPE_PROXY_RESOLVER,
+                                                g_proxy_resolver_portal_iface_init)
+                         _g_io_modules_ensure_extension_points_registered ();
+                         g_io_extension_point_implement (G_PROXY_RESOLVER_EXTENSION_POINT_NAME,
+                                                         g_define_type_id,
+                                                         "portal",
+                                                         90))
+
+static void
+g_proxy_resolver_portal_init (GProxyResolverPortal *resolver)
+{
+  resolver->resolver = xdp_proxy_resolver_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
+                                                                G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,
+                                                                "org.freedesktop.portal.Desktop",
+                                                                "/org/freedesktop/portal/desktop",
+                                                                NULL,
+                                                                NULL);
+}
+
+static gboolean
+g_proxy_resolver_portal_is_supported (GProxyResolver *object)
+{
+  GProxyResolverPortal *resolver = G_PROXY_RESOLVER_PORTAL (object);
+  char *name_owner;
+  gboolean has_portal;
+
+  if (!glib_should_use_portal () ||
+      !glib_network_available_in_sandbox () ||
+      !resolver->resolver)
+    return FALSE;
+
+  name_owner = g_dbus_proxy_get_name_owner (G_DBUS_PROXY (resolver->resolver));
+  has_portal = name_owner != NULL;
+  g_free (name_owner);
+
+  return has_portal;
+}
+
+static gchar **
+g_proxy_resolver_portal_lookup (GProxyResolver *proxy_resolver,
+                                const gchar     *uri,
+                                GCancellable    *cancellable,
+                                GError         **error)
+{
+  GProxyResolverPortal *resolver = G_PROXY_RESOLVER_PORTAL (proxy_resolver);
+  char **proxy = NULL;
+
+  if (!xdp_proxy_resolver_call_lookup_sync (resolver->resolver,
+                                            uri,
+                                            &proxy,
+                                            cancellable,
+                                            error))
+    return NULL;
+
+  return proxy;
+}
+
+static void
+g_proxy_resolver_portal_lookup_async (GProxyResolver      *proxy_resolver,
+                                      const gchar         *uri,
+                                      GCancellable        *cancellable,
+                                      GAsyncReadyCallback  callback,
+                                      gpointer             user_data)
+{
+  GProxyResolverPortal *resolver = G_PROXY_RESOLVER_PORTAL (proxy_resolver);
+
+  xdp_proxy_resolver_call_lookup (resolver->resolver,
+                                  uri,
+                                  cancellable,
+                                  callback,
+                                  user_data);
+}
+
+static gchar **
+g_proxy_resolver_portal_lookup_finish (GProxyResolver  *proxy_resolver,
+                                       GAsyncResult    *result,
+                                       GError         **error)
+{
+  GProxyResolverPortal *resolver = G_PROXY_RESOLVER_PORTAL (proxy_resolver);
+  char **proxy = NULL;
+
+  if (!xdp_proxy_resolver_call_lookup_finish (resolver->resolver,
+                                              &proxy,
+                                              result,
+                                              error))
+    return NULL;
+
+  return proxy;
+}
+
+static void
+g_proxy_resolver_portal_finalize (GObject *object)
+{
+  GProxyResolverPortal *resolver = G_PROXY_RESOLVER_PORTAL (object);
+
+  g_clear_object (&resolver->resolver);
+
+  G_OBJECT_CLASS (g_proxy_resolver_portal_parent_class)->finalize (object);
+}
+
+static void
+g_proxy_resolver_portal_class_init (GProxyResolverPortalClass *resolver_class)
+{
+  GObjectClass *object_class;
+ 
+  object_class = G_OBJECT_CLASS (resolver_class);
+  object_class->finalize = g_proxy_resolver_portal_finalize;
+}
+
+static void
+g_proxy_resolver_portal_iface_init (GProxyResolverInterface *iface)
+{
+  iface->is_supported = g_proxy_resolver_portal_is_supported;
+  iface->lookup = g_proxy_resolver_portal_lookup;
+  iface->lookup_async = g_proxy_resolver_portal_lookup_async;
+  iface->lookup_finish = g_proxy_resolver_portal_lookup_finish;
+}
diff --git a/gio/gproxyresolverportal.h b/gio/gproxyresolverportal.h
new file mode 100644
index 0000000..378a680
--- /dev/null
+++ b/gio/gproxyresolverportal.h
@@ -0,0 +1,47 @@
+/* GIO - GLib Input, Output and Streaming Library
+ *
+ * Copyright 2016 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, see
+ * <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __G_PROXY_RESOLVER_PORTAL_H__
+#define __G_PROXY_RESOLVER_PORTAL_H__
+
+#include <glib-object.h>
+#include <gio/gio.h>
+
+G_BEGIN_DECLS
+
+#define G_TYPE_PROXY_RESOLVER_PORTAL         (g_proxy_resolver_portal_get_type ())
+#define G_PROXY_RESOLVER_PORTAL(o)           (G_TYPE_CHECK_INSTANCE_CAST ((o), G_TYPE_PROXY_RESOLVER_PORTAL, 
GProxyResolverPortal))
+#define G_PROXY_RESOLVER_PORTAL_CLASS(k)     (G_TYPE_CHECK_CLASS_CAST((k), G_TYPE_PROXY_RESOLVER_PORTAL, 
GProxyResolverPortalClass))
+#define G_IS_PROXY_RESOLVER_PORTAL(o)        (G_TYPE_CHECK_INSTANCE_TYPE ((o), G_TYPE_PROXY_RESOLVER_PORTAL))
+#define G_IS_PROXY_RESOLVER_PORTAL_CLASS(k)  (G_TYPE_CHECK_CLASS_TYPE ((k), G_TYPE_PROXY_RESOLVER_PORTAL))
+#define G_PROXY_RESOLVER_PORTAL_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), G_TYPE_PROXY_RESOLVER_PORTAL, 
GProxyResolverPortalClass))
+
+typedef struct _GProxyResolverPortal       GProxyResolverPortal;
+typedef struct _GProxyResolverPortalClass  GProxyResolverPortalClass;
+
+struct _GProxyResolverPortalClass {
+  GObjectClass parent_class;
+};
+
+GType g_proxy_resolver_portal_get_type (void);
+
+G_END_DECLS
+
+#endif /* __G_PROXY_RESOLVER_PORTAL_H__ */
+
diff --git a/gio/org.freedesktop.portal.ProxyResolver.xml b/gio/org.freedesktop.portal.ProxyResolver.xml
new file mode 100644
index 0000000..b274f00
--- /dev/null
+++ b/gio/org.freedesktop.portal.ProxyResolver.xml
@@ -0,0 +1,29 @@
+<?xml version="1.0"?>
+<!--
+ Copyright (C) 2016 Red Hat, Inc.
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General
+ Public License along with this library; if not, write to the
+ Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ Boston, MA 02110-1301, USA.
+
+ Author: Matthias Clasen <mclasen redhat com>
+-->
+<node xmlns:doc="http://www.freedesktop.org/dbus/1.0/doc.dtd"; name="/">
+  <interface name="org.freedesktop.portal.ProxyResolver">
+    <method name="Lookup">
+      <arg type="s" name="uri" direction="in"/>
+      <arg type="as" name="proxies" direction="out"/>
+    </method>
+  </interface>
+</node>


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