Coverage Report

Created: 2025-07-01 07:09

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