Coverage Report

Created: 2025-06-13 06:55

/src/glib/gio/gopenuriportal.c
Line
Count
Source (jump to first uncovered line)
1
/* GIO - GLib Input, Output and Streaming Library
2
 *
3
 * Copyright 2017 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 Public
18
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
#include "config.h"
22
23
#include <sys/stat.h>
24
#include <fcntl.h>
25
#include <errno.h>
26
#include <string.h>
27
28
#include "gopenuriportal.h"
29
#include "xdp-dbus.h"
30
#include "gstdio.h"
31
32
#ifdef G_OS_UNIX
33
#include "gunixfdlist.h"
34
#endif
35
36
#ifndef O_CLOEXEC
37
#define O_CLOEXEC 0
38
#else
39
#define HAVE_O_CLOEXEC 1
40
#endif
41
42
43
static GXdpOpenURI *openuri;
44
45
static gboolean
46
init_openuri_portal (void)
47
0
{
48
0
  static gsize openuri_inited = 0;
49
50
0
  if (g_once_init_enter (&openuri_inited))
51
0
    {
52
0
      GError *error = NULL;
53
0
      GDBusConnection *connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
54
55
0
      if (connection != NULL)
56
0
        {
57
0
          openuri = gxdp_open_uri_proxy_new_sync (connection, 0,
58
0
                                                  "org.freedesktop.portal.Desktop",
59
0
                                                  "/org/freedesktop/portal/desktop",
60
0
                                                  NULL, &error);
61
0
          if (openuri == NULL)
62
0
            {
63
0
              g_warning ("Cannot create document portal proxy: %s", error->message);
64
0
              g_error_free (error);
65
0
            }
66
67
0
          g_object_unref (connection);
68
0
        }
69
0
      else
70
0
        {
71
0
          g_warning ("Cannot connect to session bus when initializing document portal: %s",
72
0
                     error->message);
73
0
          g_error_free (error);
74
0
        }
75
76
0
      g_once_init_leave (&openuri_inited, 1);
77
0
    }
78
79
0
  return openuri != NULL;
80
0
}
81
82
gboolean
83
g_openuri_portal_open_uri (const char  *uri,
84
                           const char  *parent_window,
85
                           GError     **error)
86
0
{
87
0
  GFile *file = NULL;
88
0
  GVariantBuilder opt_builder;
89
0
  gboolean res;
90
91
0
  if (!init_openuri_portal ())
92
0
    {
93
0
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_INITIALIZED,
94
0
                   "OpenURI portal is not available");
95
0
      return FALSE;
96
0
    }
97
98
0
  g_variant_builder_init (&opt_builder, G_VARIANT_TYPE_VARDICT);
99
100
0
  file = g_file_new_for_uri (uri);
101
0
  if (g_file_is_native (file))
102
0
    {
103
0
      char *path = NULL;
104
0
      GUnixFDList *fd_list = NULL;
105
0
      int fd, fd_id, errsv;
106
107
0
      path = g_file_get_path (file);
108
109
0
      fd = g_open (path, O_RDONLY | O_CLOEXEC);
110
0
      errsv = errno;
111
0
      if (fd == -1)
112
0
        {
113
0
          g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
114
0
                       "Failed to open '%s'", path);
115
0
          g_free (path);
116
0
          g_variant_builder_clear (&opt_builder);
117
0
          return FALSE;
118
0
        }
119
120
#ifndef HAVE_O_CLOEXEC
121
      fcntl (fd, F_SETFD, FD_CLOEXEC);
122
#endif
123
0
      fd_list = g_unix_fd_list_new_from_array (&fd, 1);
124
0
      fd = -1;
125
0
      fd_id = 0;
126
127
0
      res = gxdp_open_uri_call_open_file_sync (openuri,
128
0
                                               parent_window ? parent_window : "",
129
0
                                               g_variant_new ("h", fd_id),
130
0
                                               g_variant_builder_end (&opt_builder),
131
0
                                               fd_list,
132
0
                                               NULL,
133
0
                                               NULL,
134
0
                                               NULL,
135
0
                                               error);
136
0
      g_free (path);
137
0
      g_object_unref (fd_list);
138
0
    }
139
0
  else
140
0
    {
141
0
      res = gxdp_open_uri_call_open_uri_sync (openuri,
142
0
                                              parent_window ? parent_window : "",
143
0
                                              uri,
144
0
                                              g_variant_builder_end (&opt_builder),
145
0
                                              NULL,
146
0
                                              NULL,
147
0
                                              error);
148
0
    }
149
150
0
  g_object_unref (file);
151
152
0
  return res;
153
0
}
154
155
enum {
156
  XDG_DESKTOP_PORTAL_SUCCESS   = 0,
157
  XDG_DESKTOP_PORTAL_CANCELLED = 1,
158
  XDG_DESKTOP_PORTAL_FAILED    = 2
159
};
160
161
static void
162
response_received (GDBusConnection *connection,
163
                   const char      *sender_name,
164
                   const char      *object_path,
165
                   const char      *interface_name,
166
                   const char      *signal_name,
167
                   GVariant        *parameters,
168
                   gpointer         user_data)
169
0
{
170
0
  GTask *task = user_data;
171
0
  guint32 response;
172
0
  guint signal_id;
173
174
0
  signal_id = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (task), "signal-id"));
175
0
  g_dbus_connection_signal_unsubscribe (connection, signal_id);
176
177
0
  g_variant_get (parameters, "(u@a{sv})", &response, NULL);
178
179
0
  switch (response)
180
0
    {
181
0
    case XDG_DESKTOP_PORTAL_SUCCESS:
182
0
      g_task_return_boolean (task, TRUE);
183
0
      break;
184
0
    case XDG_DESKTOP_PORTAL_CANCELLED:
185
0
      g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_CANCELLED, "Launch cancelled");
186
0
      break;
187
0
    case XDG_DESKTOP_PORTAL_FAILED:
188
0
    default:
189
0
      g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_FAILED, "Launch failed");
190
0
      break;
191
0
    }
192
193
0
  g_object_unref (task);
194
0
}
195
196
static void
197
open_call_done (GObject      *source,
198
                GAsyncResult *result,
199
                gpointer      user_data)
200
0
{
201
0
  GXdpOpenURI *openuri = GXDP_OPEN_URI (source);
202
0
  GDBusConnection *connection;
203
0
  GTask *task = user_data;
204
0
  GError *error = NULL;
205
0
  gboolean open_file;
206
0
  gboolean res;
207
0
  char *path = NULL;
208
0
  const char *handle;
209
0
  guint signal_id;
210
211
0
  connection = g_dbus_proxy_get_connection (G_DBUS_PROXY (openuri));
212
0
  open_file = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (task), "open-file"));
213
214
0
  if (open_file)
215
0
    res = gxdp_open_uri_call_open_file_finish (openuri, &path, NULL, result, &error);
216
0
  else
217
0
    res = gxdp_open_uri_call_open_uri_finish (openuri, &path, result, &error);
218
219
0
  if (!res)
220
0
    {
221
0
      g_task_return_error (task, error);
222
0
      g_object_unref (task);
223
0
      g_free (path);
224
0
      return;
225
0
    }
226
227
0
  handle = (const char *)g_object_get_data (G_OBJECT (task), "handle");
228
0
  if (g_strcmp0 (handle, path) != 0)
229
0
    {
230
0
      signal_id = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (task), "signal-id"));
231
0
      g_dbus_connection_signal_unsubscribe (connection, signal_id);
232
233
0
      signal_id = g_dbus_connection_signal_subscribe (connection,
234
0
                                                      "org.freedesktop.portal.Desktop",
235
0
                                                      "org.freedesktop.portal.Request",
236
0
                                                      "Response",
237
0
                                                      path,
238
0
                                                      NULL,
239
0
                                                      G_DBUS_SIGNAL_FLAGS_NO_MATCH_RULE,
240
0
                                                      response_received,
241
0
                                                      task,
242
0
                                                      NULL);
243
0
      g_object_set_data (G_OBJECT (task), "signal-id", GINT_TO_POINTER (signal_id));
244
0
    }
245
0
}
246
247
void
248
g_openuri_portal_open_uri_async (const char          *uri,
249
                                 const char          *parent_window,
250
                                 GCancellable        *cancellable,
251
                                 GAsyncReadyCallback  callback,
252
                                 gpointer             user_data)
253
0
{
254
0
  GDBusConnection *connection;
255
0
  GTask *task;
256
0
  GFile *file;
257
0
  GVariant *opts = NULL;
258
0
  int i;
259
0
  guint signal_id;
260
261
0
  if (!init_openuri_portal ())
262
0
    {
263
0
      g_task_report_new_error (NULL, callback, user_data, NULL,
264
0
                               G_IO_ERROR, G_IO_ERROR_NOT_INITIALIZED,
265
0
                               "OpenURI portal is not available");
266
0
      return;
267
0
    }
268
269
0
  connection = g_dbus_proxy_get_connection (G_DBUS_PROXY (openuri));
270
271
0
  if (callback)
272
0
    {
273
0
      GVariantBuilder opt_builder;
274
0
      char *token;
275
0
      char *sender;
276
0
      char *handle;
277
278
0
      task = g_task_new (NULL, cancellable, callback, user_data);
279
280
0
      token = g_strdup_printf ("gio%d", g_random_int_range (0, G_MAXINT));
281
0
      sender = g_strdup (g_dbus_connection_get_unique_name (connection) + 1);
282
0
      for (i = 0; sender[i]; i++)
283
0
        if (sender[i] == '.')
284
0
          sender[i] = '_';
285
286
0
      handle = g_strdup_printf ("/org/freedesktop/portal/desktop/request/%s/%s", sender, token);
287
0
      g_object_set_data_full (G_OBJECT (task), "handle", handle, g_free);
288
0
      g_free (sender);
289
290
0
      signal_id = g_dbus_connection_signal_subscribe (connection,
291
0
                                                      "org.freedesktop.portal.Desktop",
292
0
                                                      "org.freedesktop.portal.Request",
293
0
                                                      "Response",
294
0
                                                      handle,
295
0
                                                      NULL,
296
0
                                                      G_DBUS_SIGNAL_FLAGS_NO_MATCH_RULE,
297
0
                                                      response_received,
298
0
                                                      task,
299
0
                                                      NULL);
300
0
      g_object_set_data (G_OBJECT (task), "signal-id", GINT_TO_POINTER (signal_id));
301
302
0
      g_variant_builder_init (&opt_builder, G_VARIANT_TYPE_VARDICT);
303
0
      g_variant_builder_add (&opt_builder, "{sv}", "handle_token", g_variant_new_string (token));
304
0
      g_free (token);
305
306
0
      opts = g_variant_builder_end (&opt_builder);
307
0
    }
308
0
  else
309
0
    task = NULL;
310
311
0
  file = g_file_new_for_uri (uri);
312
0
  if (g_file_is_native (file))
313
0
    {
314
0
      char *path = NULL;
315
0
      GUnixFDList *fd_list = NULL;
316
0
      int fd, fd_id, errsv;
317
318
0
      if (task)
319
0
        g_object_set_data (G_OBJECT (task), "open-file", GINT_TO_POINTER (TRUE));
320
321
0
      path = g_file_get_path (file);
322
0
      fd = g_open (path, O_RDONLY | O_CLOEXEC);
323
0
      errsv = errno;
324
0
      if (fd == -1)
325
0
        {
326
0
          g_task_report_new_error (NULL, callback, user_data, NULL,
327
0
                                   G_IO_ERROR, g_io_error_from_errno (errsv),
328
0
                                   "OpenURI portal is not available");
329
0
          return;
330
0
        }
331
332
#ifndef HAVE_O_CLOEXEC
333
      fcntl (fd, F_SETFD, FD_CLOEXEC);
334
#endif
335
0
      fd_list = g_unix_fd_list_new_from_array (&fd, 1);
336
0
      fd = -1;
337
0
      fd_id = 0;
338
339
0
      gxdp_open_uri_call_open_file (openuri,
340
0
                                    parent_window ? parent_window : "",
341
0
                                    g_variant_new ("h", fd_id),
342
0
                                    opts,
343
0
                                    fd_list,
344
0
                                    cancellable,
345
0
                                    task ? open_call_done : NULL,
346
0
                                    task);
347
0
      g_object_unref (fd_list);
348
0
      g_free (path);
349
0
    }
350
0
  else
351
0
    {
352
0
      gxdp_open_uri_call_open_uri (openuri,
353
0
                                   parent_window ? parent_window : "",
354
0
                                   uri,
355
0
                                   opts,
356
0
                                   cancellable,
357
0
                                   task ? open_call_done : NULL,
358
0
                                   task);
359
0
    }
360
361
0
  g_object_unref (file);
362
0
}
363
364
gboolean
365
g_openuri_portal_open_uri_finish (GAsyncResult  *result,
366
                                  GError       **error)
367
0
{
368
0
  return g_task_propagate_boolean (G_TASK (result), error);
369
0
}