Coverage Report

Created: 2025-07-01 07:09

/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
 * 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.
9
 *
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.
14
 *
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/>.
18
 */
19
20
#include "config.h"
21
22
#include "xdp-dbus.h"
23
#include "giomodule-priv.h"
24
#include "gportalsupport.h"
25
#include "gproxyresolverportal.h"
26
27
struct _GProxyResolverPortal {
28
  GObject parent_instance;
29
30
  GXdpProxyResolver *resolver;
31
  gboolean network_available;
32
};
33
34
static void g_proxy_resolver_portal_iface_init (GProxyResolverInterface *iface);
35
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,
41
                                                         g_define_type_id,
42
                                                         "portal",
43
                                                         90))
44
45
static gboolean
46
ensure_resolver_proxy (GProxyResolverPortal *resolver)
47
0
{
48
0
  if (resolver->resolver)
49
0
    return TRUE;
50
51
0
  if (!glib_should_use_portal ())
52
0
    return FALSE;
53
54
0
  resolver->resolver = gxdp_proxy_resolver_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
55
0
                                                                   G_DBUS_PROXY_FLAGS_NONE,
56
0
                                                                   "org.freedesktop.portal.Desktop",
57
0
                                                                   "/org/freedesktop/portal/desktop",
58
0
                                                                   NULL,
59
0
                                                                   NULL);
60
61
0
  resolver->network_available = glib_network_available_in_sandbox ();
62
63
0
  return resolver->resolver != NULL;
64
0
}
65
66
static void
67
g_proxy_resolver_portal_init (GProxyResolverPortal *resolver)
68
0
{
69
0
}
70
71
static gboolean
72
g_proxy_resolver_portal_is_supported (GProxyResolver *object)
73
0
{
74
0
  GProxyResolverPortal *resolver = G_PROXY_RESOLVER_PORTAL (object);
75
0
  char *name_owner;
76
0
  gboolean has_portal;
77
78
0
  if (!ensure_resolver_proxy (resolver))
79
0
    return FALSE;
80
81
0
  name_owner = g_dbus_proxy_get_name_owner (G_DBUS_PROXY (resolver->resolver));
82
0
  has_portal = name_owner != NULL;
83
0
  g_free (name_owner);
84
85
0
  return has_portal;
86
0
}
87
88
static const char *no_proxy[2] = { "direct://", NULL };
89
90
static gchar **
91
g_proxy_resolver_portal_lookup (GProxyResolver *proxy_resolver,
92
                                const gchar     *uri,
93
                                GCancellable    *cancellable,
94
                                GError         **error)
95
0
{
96
0
  GProxyResolverPortal *resolver = G_PROXY_RESOLVER_PORTAL (proxy_resolver);
97
0
  char **proxy = NULL;
98
99
0
  ensure_resolver_proxy (resolver);
100
0
  g_assert (resolver->resolver);
101
102
0
  if (!gxdp_proxy_resolver_call_lookup_sync (resolver->resolver,
103
0
                                             uri,
104
0
                                             &proxy,
105
0
                                             cancellable,
106
0
                                             error))
107
0
    return NULL;
108
109
0
  if (!resolver->network_available)
110
0
    {
111
0
      g_strfreev (proxy);
112
0
      proxy = g_strdupv ((gchar **)no_proxy);
113
0
    }
114
115
0
  return proxy;
116
0
}
117
118
static void
119
lookup_done (GObject      *source,
120
             GAsyncResult *result,
121
             gpointer      data)
122
0
{
123
0
  GTask *task = data;
124
0
  GError *error = NULL;
125
0
  gchar **proxies = NULL;
126
127
0
  if (!gxdp_proxy_resolver_call_lookup_finish (GXDP_PROXY_RESOLVER (source),
128
0
                                               &proxies,
129
0
                                               result,
130
0
                                               &error))
131
0
    g_task_return_error (task, error);
132
0
  else
133
0
    g_task_return_pointer (task, proxies, NULL);
134
135
0
  g_object_unref (task);
136
0
}
137
138
static void
139
g_proxy_resolver_portal_lookup_async (GProxyResolver      *proxy_resolver,
140
                                      const gchar         *uri,
141
                                      GCancellable        *cancellable,
142
                                      GAsyncReadyCallback  callback,
143
                                      gpointer             user_data)
144
0
{
145
0
  GProxyResolverPortal *resolver = G_PROXY_RESOLVER_PORTAL (proxy_resolver);
146
0
  GTask *task;
147
148
0
  ensure_resolver_proxy (resolver);
149
0
  g_assert (resolver->resolver);
150
151
0
  task = g_task_new (proxy_resolver, cancellable, callback, user_data);
152
0
  gxdp_proxy_resolver_call_lookup (resolver->resolver,
153
0
                                   uri,
154
0
                                   cancellable,
155
0
                                   lookup_done,
156
0
                                   g_object_ref (task));
157
0
  g_object_unref (task);
158
0
}
159
160
static gchar **
161
g_proxy_resolver_portal_lookup_finish (GProxyResolver  *proxy_resolver,
162
                                       GAsyncResult    *result,
163
                                       GError         **error)
164
0
{
165
0
  GProxyResolverPortal *resolver = G_PROXY_RESOLVER_PORTAL (proxy_resolver);
166
0
  GTask *task = G_TASK (result);
167
0
  char **proxies;
168
169
0
  proxies = g_task_propagate_pointer (task, error);
170
0
  if (proxies == NULL)
171
0
    return NULL;
172
173
0
  if (!resolver->network_available)
174
0
    {
175
0
      g_strfreev (proxies);
176
0
      proxies = g_strdupv ((gchar **)no_proxy);
177
0
    }
178
179
0
  return proxies;
180
0
}
181
182
static void
183
g_proxy_resolver_portal_finalize (GObject *object)
184
0
{
185
0
  GProxyResolverPortal *resolver = G_PROXY_RESOLVER_PORTAL (object);
186
187
0
  g_clear_object (&resolver->resolver);
188
189
0
  G_OBJECT_CLASS (g_proxy_resolver_portal_parent_class)->finalize (object);
190
0
}
191
192
static void
193
g_proxy_resolver_portal_class_init (GProxyResolverPortalClass *resolver_class)
194
0
{
195
0
  GObjectClass *object_class;
196
 
197
0
  object_class = G_OBJECT_CLASS (resolver_class);
198
0
  object_class->finalize = g_proxy_resolver_portal_finalize;
199
0
}
200
201
static void
202
g_proxy_resolver_portal_iface_init (GProxyResolverInterface *iface)
203
0
{
204
0
  iface->is_supported = g_proxy_resolver_portal_is_supported;
205
0
  iface->lookup = g_proxy_resolver_portal_lookup;
206
0
  iface->lookup_async = g_proxy_resolver_portal_lookup_async;
207
0
  iface->lookup_finish = g_proxy_resolver_portal_lookup_finish;
208
0
}