/src/glib/gio/gproxyresolverportal.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /* GIO - GLib Input, Output and Streaming Library  | 
2  |  |  *  | 
3  |  |  * Copyright 2016 Red Hat, Inc.  | 
4  |  |  *  | 
5  |  |  * SPDX-License-Identifier: LGPL-2.1-or-later  | 
6  |  |  *  | 
7  |  |  * This library is free software; you can redistribute it and/or  | 
8  |  |  * modify it under the terms of the GNU Lesser General Public  | 
9  |  |  * License as published by the Free Software Foundation; either  | 
10  |  |  * version 2.1 of the License, or (at your option) any later version.  | 
11  |  |  *  | 
12  |  |  * This library is distributed in the hope that it will be useful,  | 
13  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of  | 
14  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  | 
15  |  |  * Lesser General Public License for more details.  | 
16  |  |  *  | 
17  |  |  * You should have received a copy of the GNU Lesser General  | 
18  |  |  * Public License along with this library; if not, see  | 
19  |  |  * <http://www.gnu.org/licenses/>.  | 
20  |  |  */  | 
21  |  |  | 
22  |  | #include "config.h"  | 
23  |  |  | 
24  |  | #include "xdp-dbus.h"  | 
25  |  | #include "giomodule-priv.h"  | 
26  |  | #include "gportalsupport.h"  | 
27  |  | #include "gproxyresolverportal.h"  | 
28  |  |  | 
29  |  | struct _GProxyResolverPortal { | 
30  |  |   GObject parent_instance;  | 
31  |  |  | 
32  |  |   GXdpProxyResolver *resolver;  | 
33  |  |   gboolean network_available;  | 
34  |  | };  | 
35  |  |  | 
36  |  | static void g_proxy_resolver_portal_iface_init (GProxyResolverInterface *iface);  | 
37  |  |  | 
38  |  | G_DEFINE_TYPE_WITH_CODE (GProxyResolverPortal, g_proxy_resolver_portal, G_TYPE_OBJECT,  | 
39  |  |                          G_IMPLEMENT_INTERFACE (G_TYPE_PROXY_RESOLVER,  | 
40  |  |                                                 g_proxy_resolver_portal_iface_init)  | 
41  |  |                          _g_io_modules_ensure_extension_points_registered ();  | 
42  |  |                          g_io_extension_point_implement (G_PROXY_RESOLVER_EXTENSION_POINT_NAME,  | 
43  |  |                                                          g_define_type_id,  | 
44  |  |                                                          "portal",  | 
45  |  |                                                          90))  | 
46  |  |  | 
47  |  | static gboolean  | 
48  |  | ensure_resolver_proxy (GProxyResolverPortal *resolver)  | 
49  | 0  | { | 
50  | 0  |   if (resolver->resolver)  | 
51  | 0  |     return TRUE;  | 
52  |  |  | 
53  | 0  |   if (!glib_should_use_portal ())  | 
54  | 0  |     return FALSE;  | 
55  |  |  | 
56  | 0  |   resolver->resolver = gxdp_proxy_resolver_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,  | 
57  | 0  |                                                                    G_DBUS_PROXY_FLAGS_NONE,  | 
58  | 0  |                                                                    "org.freedesktop.portal.Desktop",  | 
59  | 0  |                                                                    "/org/freedesktop/portal/desktop",  | 
60  | 0  |                                                                    NULL,  | 
61  | 0  |                                                                    NULL);  | 
62  |  | 
  | 
63  | 0  |   resolver->network_available = glib_network_available_in_sandbox ();  | 
64  |  | 
  | 
65  | 0  |   return resolver->resolver != NULL;  | 
66  | 0  | }  | 
67  |  |  | 
68  |  | static void  | 
69  |  | g_proxy_resolver_portal_init (GProxyResolverPortal *resolver)  | 
70  | 0  | { | 
71  | 0  | }  | 
72  |  |  | 
73  |  | static gboolean  | 
74  |  | g_proxy_resolver_portal_is_supported (GProxyResolver *object)  | 
75  | 0  | { | 
76  | 0  |   GProxyResolverPortal *resolver = G_PROXY_RESOLVER_PORTAL (object);  | 
77  | 0  |   char *name_owner;  | 
78  | 0  |   gboolean has_portal;  | 
79  |  | 
  | 
80  | 0  |   if (!ensure_resolver_proxy (resolver))  | 
81  | 0  |     return FALSE;  | 
82  |  |  | 
83  | 0  |   name_owner = g_dbus_proxy_get_name_owner (G_DBUS_PROXY (resolver->resolver));  | 
84  | 0  |   has_portal = name_owner != NULL;  | 
85  | 0  |   g_free (name_owner);  | 
86  |  | 
  | 
87  | 0  |   return has_portal;  | 
88  | 0  | }  | 
89  |  |  | 
90  |  | static const char *no_proxy[2] = { "direct://", NULL }; | 
91  |  |  | 
92  |  | static gchar **  | 
93  |  | g_proxy_resolver_portal_lookup (GProxyResolver *proxy_resolver,  | 
94  |  |                                 const gchar     *uri,  | 
95  |  |                                 GCancellable    *cancellable,  | 
96  |  |                                 GError         **error)  | 
97  | 0  | { | 
98  | 0  |   GProxyResolverPortal *resolver = G_PROXY_RESOLVER_PORTAL (proxy_resolver);  | 
99  | 0  |   char **proxy = NULL;  | 
100  |  | 
  | 
101  | 0  |   ensure_resolver_proxy (resolver);  | 
102  | 0  |   g_assert (resolver->resolver);  | 
103  |  |  | 
104  | 0  |   if (!gxdp_proxy_resolver_call_lookup_sync (resolver->resolver,  | 
105  | 0  |                                              uri,  | 
106  | 0  |                                              &proxy,  | 
107  | 0  |                                              cancellable,  | 
108  | 0  |                                              error))  | 
109  | 0  |     return NULL;  | 
110  |  |  | 
111  | 0  |   if (!resolver->network_available)  | 
112  | 0  |     { | 
113  | 0  |       g_strfreev (proxy);  | 
114  | 0  |       proxy = g_strdupv ((gchar **)no_proxy);  | 
115  | 0  |     }  | 
116  |  | 
  | 
117  | 0  |   return proxy;  | 
118  | 0  | }  | 
119  |  |  | 
120  |  | static void  | 
121  |  | lookup_done (GObject      *source,  | 
122  |  |              GAsyncResult *result,  | 
123  |  |              gpointer      data)  | 
124  | 0  | { | 
125  | 0  |   GTask *task = data;  | 
126  | 0  |   GError *error = NULL;  | 
127  | 0  |   gchar **proxies = NULL;  | 
128  |  | 
  | 
129  | 0  |   if (!gxdp_proxy_resolver_call_lookup_finish (GXDP_PROXY_RESOLVER (source),  | 
130  | 0  |                                                &proxies,  | 
131  | 0  |                                                result,  | 
132  | 0  |                                                &error))  | 
133  | 0  |     g_task_return_error (task, error);  | 
134  | 0  |   else  | 
135  | 0  |     g_task_return_pointer (task, proxies, NULL);  | 
136  |  | 
  | 
137  | 0  |   g_object_unref (task);  | 
138  | 0  | }  | 
139  |  |  | 
140  |  | static void  | 
141  |  | g_proxy_resolver_portal_lookup_async (GProxyResolver      *proxy_resolver,  | 
142  |  |                                       const gchar         *uri,  | 
143  |  |                                       GCancellable        *cancellable,  | 
144  |  |                                       GAsyncReadyCallback  callback,  | 
145  |  |                                       gpointer             user_data)  | 
146  | 0  | { | 
147  | 0  |   GProxyResolverPortal *resolver = G_PROXY_RESOLVER_PORTAL (proxy_resolver);  | 
148  | 0  |   GTask *task;  | 
149  |  | 
  | 
150  | 0  |   ensure_resolver_proxy (resolver);  | 
151  | 0  |   g_assert (resolver->resolver);  | 
152  |  |  | 
153  | 0  |   task = g_task_new (proxy_resolver, cancellable, callback, user_data);  | 
154  | 0  |   gxdp_proxy_resolver_call_lookup (resolver->resolver,  | 
155  | 0  |                                    uri,  | 
156  | 0  |                                    cancellable,  | 
157  | 0  |                                    lookup_done,  | 
158  | 0  |                                    g_object_ref (task));  | 
159  | 0  |   g_object_unref (task);  | 
160  | 0  | }  | 
161  |  |  | 
162  |  | static gchar **  | 
163  |  | g_proxy_resolver_portal_lookup_finish (GProxyResolver  *proxy_resolver,  | 
164  |  |                                        GAsyncResult    *result,  | 
165  |  |                                        GError         **error)  | 
166  | 0  | { | 
167  | 0  |   GProxyResolverPortal *resolver = G_PROXY_RESOLVER_PORTAL (proxy_resolver);  | 
168  | 0  |   GTask *task = G_TASK (result);  | 
169  | 0  |   char **proxies;  | 
170  |  | 
  | 
171  | 0  |   proxies = g_task_propagate_pointer (task, error);  | 
172  | 0  |   if (proxies == NULL)  | 
173  | 0  |     return NULL;  | 
174  |  |  | 
175  | 0  |   if (!resolver->network_available)  | 
176  | 0  |     { | 
177  | 0  |       g_strfreev (proxies);  | 
178  | 0  |       proxies = g_strdupv ((gchar **)no_proxy);  | 
179  | 0  |     }  | 
180  |  | 
  | 
181  | 0  |   return proxies;  | 
182  | 0  | }  | 
183  |  |  | 
184  |  | static void  | 
185  |  | g_proxy_resolver_portal_finalize (GObject *object)  | 
186  | 0  | { | 
187  | 0  |   GProxyResolverPortal *resolver = G_PROXY_RESOLVER_PORTAL (object);  | 
188  |  | 
  | 
189  | 0  |   g_clear_object (&resolver->resolver);  | 
190  |  | 
  | 
191  | 0  |   G_OBJECT_CLASS (g_proxy_resolver_portal_parent_class)->finalize (object);  | 
192  | 0  | }  | 
193  |  |  | 
194  |  | static void  | 
195  |  | g_proxy_resolver_portal_class_init (GProxyResolverPortalClass *resolver_class)  | 
196  | 0  | { | 
197  | 0  |   GObjectClass *object_class;  | 
198  |  |    | 
199  | 0  |   object_class = G_OBJECT_CLASS (resolver_class);  | 
200  | 0  |   object_class->finalize = g_proxy_resolver_portal_finalize;  | 
201  | 0  | }  | 
202  |  |  | 
203  |  | static void  | 
204  |  | g_proxy_resolver_portal_iface_init (GProxyResolverInterface *iface)  | 
205  | 0  | { | 
206  | 0  |   iface->is_supported = g_proxy_resolver_portal_is_supported;  | 
207  | 0  |   iface->lookup = g_proxy_resolver_portal_lookup;  | 
208  | 0  |   iface->lookup_async = g_proxy_resolver_portal_lookup_async;  | 
209  | 0  |   iface->lookup_finish = g_proxy_resolver_portal_lookup_finish;  | 
210  | 0  | }  |