1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright 2016 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, see
17 * <http://www.gnu.org/licenses/>.
23 #include "giomodule-priv.h"
24 #include "gportalsupport.h"
25 #include "gproxyresolverportal.h"
27 struct _GProxyResolverPortal
{
28 GObject parent_instance
;
30 GXdpProxyResolver
*resolver
;
31 gboolean network_available
;
34 static void g_proxy_resolver_portal_iface_init (GProxyResolverInterface
*iface
);
36 G_DEFINE_TYPE_WITH_CODE (GProxyResolverPortal
, g_proxy_resolver_portal
, G_TYPE_OBJECT
,
37 G_IMPLEMENT_INTERFACE (G_TYPE_PROXY_RESOLVER
,
38 g_proxy_resolver_portal_iface_init
)
39 _g_io_modules_ensure_extension_points_registered ();
40 g_io_extension_point_implement (G_PROXY_RESOLVER_EXTENSION_POINT_NAME
,
46 ensure_resolver_proxy (GProxyResolverPortal
*resolver
)
48 if (resolver
->resolver
)
51 if (!glib_should_use_portal ())
54 resolver
->resolver
= gxdp_proxy_resolver_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION
,
55 G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START
,
56 "org.freedesktop.portal.Desktop",
57 "/org/freedesktop/portal/desktop",
61 resolver
->network_available
= glib_network_available_in_sandbox ();
63 return resolver
->resolver
!= NULL
;
67 g_proxy_resolver_portal_init (GProxyResolverPortal
*resolver
)
72 g_proxy_resolver_portal_is_supported (GProxyResolver
*object
)
74 GProxyResolverPortal
*resolver
= G_PROXY_RESOLVER_PORTAL (object
);
78 if (!ensure_resolver_proxy (resolver
))
81 name_owner
= g_dbus_proxy_get_name_owner (G_DBUS_PROXY (resolver
->resolver
));
82 has_portal
= name_owner
!= NULL
;
88 static const char *no_proxy
[2] = { "direct://", NULL
};
91 g_proxy_resolver_portal_lookup (GProxyResolver
*proxy_resolver
,
93 GCancellable
*cancellable
,
96 GProxyResolverPortal
*resolver
= G_PROXY_RESOLVER_PORTAL (proxy_resolver
);
99 ensure_resolver_proxy (resolver
);
100 g_assert (resolver
->resolver
);
102 if (!gxdp_proxy_resolver_call_lookup_sync (resolver
->resolver
,
109 if (!resolver
->network_available
)
112 proxy
= g_strdupv ((gchar
**)no_proxy
);
119 lookup_done (GObject
*source
,
120 GAsyncResult
*result
,
124 GError
*error
= NULL
;
125 gchar
**proxies
= NULL
;
127 if (!gxdp_proxy_resolver_call_lookup_finish (GXDP_PROXY_RESOLVER (source
),
131 g_task_return_error (task
, error
);
133 g_task_return_pointer (task
, proxies
, NULL
);
135 g_object_unref (task
);
139 g_proxy_resolver_portal_lookup_async (GProxyResolver
*proxy_resolver
,
141 GCancellable
*cancellable
,
142 GAsyncReadyCallback callback
,
145 GProxyResolverPortal
*resolver
= G_PROXY_RESOLVER_PORTAL (proxy_resolver
);
148 ensure_resolver_proxy (resolver
);
149 g_assert (resolver
->resolver
);
151 task
= g_task_new (proxy_resolver
, cancellable
, callback
, user_data
);
152 gxdp_proxy_resolver_call_lookup (resolver
->resolver
,
156 g_object_ref (task
));
157 g_object_unref (task
);
161 g_proxy_resolver_portal_lookup_finish (GProxyResolver
*proxy_resolver
,
162 GAsyncResult
*result
,
165 GProxyResolverPortal
*resolver
= G_PROXY_RESOLVER_PORTAL (proxy_resolver
);
166 GTask
*task
= G_TASK (result
);
169 proxies
= g_task_propagate_pointer (task
, error
);
173 if (!resolver
->network_available
)
175 g_strfreev (proxies
);
176 proxies
= g_strdupv ((gchar
**)no_proxy
);
183 g_proxy_resolver_portal_finalize (GObject
*object
)
185 GProxyResolverPortal
*resolver
= G_PROXY_RESOLVER_PORTAL (object
);
187 g_clear_object (&resolver
->resolver
);
189 G_OBJECT_CLASS (g_proxy_resolver_portal_parent_class
)->finalize (object
);
193 g_proxy_resolver_portal_class_init (GProxyResolverPortalClass
*resolver_class
)
195 GObjectClass
*object_class
;
197 object_class
= G_OBJECT_CLASS (resolver_class
);
198 object_class
->finalize
= g_proxy_resolver_portal_finalize
;
202 g_proxy_resolver_portal_iface_init (GProxyResolverInterface
*iface
)
204 iface
->is_supported
= g_proxy_resolver_portal_is_supported
;
205 iface
->lookup
= g_proxy_resolver_portal_lookup
;
206 iface
->lookup_async
= g_proxy_resolver_portal_lookup_async
;
207 iface
->lookup_finish
= g_proxy_resolver_portal_lookup_finish
;