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 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 g_proxy_resolver_portal_init (GProxyResolverPortal
*resolver
)
48 resolver
->resolver
= gxdp_proxy_resolver_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION
,
49 G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START
,
50 "org.freedesktop.portal.Desktop",
51 "/org/freedesktop/portal/desktop",
55 resolver
->network_available
= glib_network_available_in_sandbox ();
59 g_proxy_resolver_portal_is_supported (GProxyResolver
*object
)
61 GProxyResolverPortal
*resolver
= G_PROXY_RESOLVER_PORTAL (object
);
65 if (!glib_should_use_portal () || !resolver
->resolver
)
68 name_owner
= g_dbus_proxy_get_name_owner (G_DBUS_PROXY (resolver
->resolver
));
69 has_portal
= name_owner
!= NULL
;
75 static const char *no_proxy
[2] = { "direct://", NULL
};
78 g_proxy_resolver_portal_lookup (GProxyResolver
*proxy_resolver
,
80 GCancellable
*cancellable
,
83 GProxyResolverPortal
*resolver
= G_PROXY_RESOLVER_PORTAL (proxy_resolver
);
86 if (!gxdp_proxy_resolver_call_lookup_sync (resolver
->resolver
,
93 if (!resolver
->network_available
)
96 proxy
= g_strdupv ((gchar
**)no_proxy
);
103 lookup_done (GObject
*source
,
104 GAsyncResult
*result
,
108 GError
*error
= NULL
;
109 gchar
**proxies
= NULL
;
111 if (!gxdp_proxy_resolver_call_lookup_finish (GXDP_PROXY_RESOLVER (source
),
115 g_task_return_error (task
, error
);
117 g_task_return_pointer (task
, proxies
, NULL
);
119 g_object_unref (task
);
123 g_proxy_resolver_portal_lookup_async (GProxyResolver
*proxy_resolver
,
125 GCancellable
*cancellable
,
126 GAsyncReadyCallback callback
,
129 GProxyResolverPortal
*resolver
= G_PROXY_RESOLVER_PORTAL (proxy_resolver
);
132 task
= g_task_new (proxy_resolver
, cancellable
, callback
, user_data
);
133 gxdp_proxy_resolver_call_lookup (resolver
->resolver
,
137 g_object_ref (task
));
138 g_object_unref (task
);
142 g_proxy_resolver_portal_lookup_finish (GProxyResolver
*proxy_resolver
,
143 GAsyncResult
*result
,
146 GProxyResolverPortal
*resolver
= G_PROXY_RESOLVER_PORTAL (proxy_resolver
);
147 GTask
*task
= G_TASK (result
);
150 proxies
= g_task_propagate_pointer (task
, error
);
154 if (!resolver
->network_available
)
156 g_strfreev (proxies
);
157 proxies
= g_strdupv ((gchar
**)no_proxy
);
164 g_proxy_resolver_portal_finalize (GObject
*object
)
166 GProxyResolverPortal
*resolver
= G_PROXY_RESOLVER_PORTAL (object
);
168 g_clear_object (&resolver
->resolver
);
170 G_OBJECT_CLASS (g_proxy_resolver_portal_parent_class
)->finalize (object
);
174 g_proxy_resolver_portal_class_init (GProxyResolverPortalClass
*resolver_class
)
176 GObjectClass
*object_class
;
178 object_class
= G_OBJECT_CLASS (resolver_class
);
179 object_class
->finalize
= g_proxy_resolver_portal_finalize
;
183 g_proxy_resolver_portal_iface_init (GProxyResolverInterface
*iface
)
185 iface
->is_supported
= g_proxy_resolver_portal_is_supported
;
186 iface
->lookup
= g_proxy_resolver_portal_lookup
;
187 iface
->lookup_async
= g_proxy_resolver_portal_lookup_async
;
188 iface
->lookup_finish
= g_proxy_resolver_portal_lookup_finish
;