Debian Codegraph @

src/dex-gdbus.c

raw symbols SCIP
1 /*
2 * dex-gdbus.c
3 *
4 * Copyright 2025 Red Hat, Inc.
5 *
6 * This library is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of the
9 * License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * SPDX-License-Identifier: LGPL-2.1-or-later
20 */
21
22 #include "config.h"
23
24 #include "dex-async-pair-private.h"
25 #include "dex-cancellable.h"
26 #include "dex-future-private.h"
27 #include "dex-future-set.h"
28 #include "dex-promise.h"
29 #include "dex-scheduler.h"
30
31 #include "dex-gdbus.h"
32
33 #ifdef DEX_FEATURE_GDBUS_CODEGEN
34 typedef struct _DexDBusInterfaceSkeletonPrivate
35 {
36 GCancellable *cancellable;
37 DexDBusInterfaceSkeletonFlags flags;
38 } DexDBusInterfaceSkeletonPrivate;
39
40 /**
41 * DexDBusInterfaceSkeleton:
42 *
43 * #DexDBusInterfaceSkeleton provides integration between libdex and the GDBus
44 * codegen. If the gdbus-codegen dex extension is used, all generated
45 * InterfaceSkeletons inherit from #DexDBusInterfaceSkeleton instead of
46 * #GDBusInterfaceSkeleton, which allows the use of the API exposed here.
47 */
48
49 G_DEFINE_ABSTRACT_TYPE_WITH_CODE (DexDBusInterfaceSkeleton,
50 dex_dbus_interface_skeleton,
51 G_TYPE_DBUS_INTERFACE_SKELETON,
52 G_ADD_PRIVATE (DexDBusInterfaceSkeleton))
53
54 static DexFuture *
55 dispatch_in_fiber (GDBusInterfaceSkeleton *_interface,
56 GDBusInterfaceMethodCallFunc func,
57 GDBusMethodInvocation *invocation,
58 GDBusObject *object)
59 {
60 gboolean authorized = TRUE;
61
62 if (object != NULL)
63 {
64 g_signal_emit_by_name (object,
65 "authorize-method",
66 _interface,
67 invocation,
68 &authorized);
69 }
70 if (authorized)
71 {
72 g_signal_emit_by_name (_interface,
73 "g-authorize-method",
74 invocation,
75 &authorized);
76 }
77
78 g_clear_object (&_interface);
79 g_clear_object (&object);
80
81 if (authorized)
82 {
83 func (g_dbus_method_invocation_get_connection (invocation),
84 g_dbus_method_invocation_get_sender (invocation),
85 g_dbus_method_invocation_get_object_path (invocation),
86 g_dbus_method_invocation_get_interface_name (invocation),
87 g_dbus_method_invocation_get_method_name (invocation),
88 g_dbus_method_invocation_get_parameters (invocation),
89 invocation,
90 g_dbus_method_invocation_get_user_data (invocation));
91 }
92
93 g_clear_object (&invocation);
94
95 return dex_future_new_true ();
96 }
97
98 static void
99 dex_dbus_interface_skeleton_method_dispatch (GDBusInterfaceSkeleton *_interface,
100 GDBusInterfaceMethodCallFunc method_call_func,
101 GDBusMethodInvocation *invocation,
102 GDBusInterfaceSkeletonFlags flags,
103 GDBusObject *object)
104 {
105 DexDBusInterfaceSkeleton *dex_interface_ = DEX_DBUS_INTERFACE_SKELETON (_interface);
106 DexDBusInterfaceSkeletonPrivate *priv =
107 dex_dbus_interface_skeleton_get_instance_private (dex_interface_);
108 DexFuture *future;
109
110 if ((priv->flags & DEX_DBUS_INTERFACE_SKELETON_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_FIBER) == 0)
111 {
112 GDBusInterfaceSkeletonClass *skeleton_class =
113 G_DBUS_INTERFACE_SKELETON_CLASS (dex_dbus_interface_skeleton_parent_class);
114
115 skeleton_class->method_dispatch (_interface, method_call_func, invocation, flags, object);
116 return;
117 }
118
119 g_return_if_fail ((flags & G_DBUS_INTERFACE_SKELETON_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_THREAD) == 0);
120
121 future = dex_scheduler_spawnv (NULL, 0,
122 G_CALLBACK (dispatch_in_fiber),
123 4,
124 G_TYPE_DBUS_INTERFACE_SKELETON, g_object_ref (_interface),
125 G_TYPE_POINTER, method_call_func,
126 G_TYPE_DBUS_METHOD_INVOCATION, g_object_ref (invocation),
127 G_TYPE_DBUS_OBJECT, object ? g_object_ref (object) : NULL);
128
129 dex_future_disown (dex_future_first (future,
130 dex_cancellable_new_from_cancellable (priv->cancellable),
131 NULL));
132 }
133
134 static void
135 dex_dbus_interface_skeleton_dispose (GObject *object)
136 {
137 DexDBusInterfaceSkeleton *interface_ = DEX_DBUS_INTERFACE_SKELETON (object);
138 DexDBusInterfaceSkeletonPrivate *priv =
139 dex_dbus_interface_skeleton_get_instance_private (interface_);
140
141 g_cancellable_cancel (priv->cancellable);
142 g_clear_object (&priv->cancellable);
143
144 G_OBJECT_CLASS (dex_dbus_interface_skeleton_parent_class)->dispose (object);
145 }
146
147 static void
148 dex_dbus_interface_skeleton_class_init (DexDBusInterfaceSkeletonClass *klass)
149 {
150 GObjectClass *object_class = G_OBJECT_CLASS (klass);
151 GDBusInterfaceSkeletonClass *skeleton_class = G_DBUS_INTERFACE_SKELETON_CLASS (klass);
152
153 object_class->dispose = dex_dbus_interface_skeleton_dispose;
154
155 skeleton_class->method_dispatch = dex_dbus_interface_skeleton_method_dispatch;
156 }
157
158 static void
159 dex_dbus_interface_skeleton_init (DexDBusInterfaceSkeleton *interface_)
160 {
161 DexDBusInterfaceSkeletonPrivate *priv =
162 dex_dbus_interface_skeleton_get_instance_private (interface_);
163
164 priv->cancellable = g_cancellable_new ();
165 }
166
167 /**
168 * dex_dbus_interface_skeleton_cancel:
169 * @interface_: a #DexDBusInterfaceSkeleton
170 *
171 * Cancels all in-flight fibers.
172 *
173 * Since: 1.1
174 */
175 void
176 dex_dbus_interface_skeleton_cancel (DexDBusInterfaceSkeleton *interface_)
177 {
178 DexDBusInterfaceSkeletonPrivate *priv;
179
180 g_return_if_fail (DEX_IS_DBUS_INTERFACE_SKELETON (interface_));
181
182 priv = dex_dbus_interface_skeleton_get_instance_private (interface_);
183
184 g_cancellable_cancel (priv->cancellable);
185 g_clear_object (&priv->cancellable);
186 priv->cancellable = g_cancellable_new ();
187 }
188
189 /**
190 * dex_dbus_interface_skeleton_get_flags:
191 * @interface_: a #DexDBusInterfaceSkeleton
192 *
193 * Gets the #DexDBusInterfaceSkeletonFlags that describes the behavior
194 * of @interface_
195 *
196 * Returns: One or more flags from the #DexDBusInterfaceSkeletonFlags enumeration.
197 *
198 * Since: 1.1
199 */
200 DexDBusInterfaceSkeletonFlags
201 dex_dbus_interface_skeleton_get_flags (DexDBusInterfaceSkeleton *interface_)
202 {
203 DexDBusInterfaceSkeletonPrivate *priv;
204
205 g_return_val_if_fail (DEX_IS_DBUS_INTERFACE_SKELETON (interface_),
206 DEX_DBUS_INTERFACE_SKELETON_FLAGS_NONE);
207
208 priv = dex_dbus_interface_skeleton_get_instance_private (interface_);
209 return priv->flags;
210 }
211
212 /**
213 * dex_dbus_interface_skeleton_set_flags:
214 * @interface_: a #DexDBusInterfaceSkeleton
215 * @flags: Flags from the #DexDBusInterfaceSkeletonFlags enumeration.
216 *
217 * Sets flags describing what the behavior of @interface_ should be.
218 *
219 * Since: 1.1
220 */
221 void
222 dex_dbus_interface_skeleton_set_flags (DexDBusInterfaceSkeleton *interface_,
223 DexDBusInterfaceSkeletonFlags flags)
224 {
225 DexDBusInterfaceSkeletonPrivate *priv;
226
227 g_return_if_fail (DEX_IS_DBUS_INTERFACE_SKELETON (interface_));
228
229 priv = dex_dbus_interface_skeleton_get_instance_private (interface_);
230
231 priv->flags = flags;
232 }
233 #endif /* DEX_FEATURE_GDBUS_CODEGEN */
234
235 static inline DexAsyncPair *
236 create_async_pair (const char *name)
237 {
238 DexAsyncPair *async_pair;
239
240 async_pair = (DexAsyncPair *)dex_object_create_instance (DEX_TYPE_ASYNC_PAIR);
241 dex_future_set_static_name (DEX_FUTURE (async_pair), name);
242
243 return async_pair;
244 }
245
246 static void
247 dex_dbus_connection_send_message_with_reply_cb (GObject *object,
248 GAsyncResult *result,
249 gpointer user_data)
250 {
251 DexAsyncPair *async_pair = user_data;
252 GDBusMessage *message = NULL;
253 GError *error = NULL;
254
255 message = g_dbus_connection_send_message_with_reply_finish (G_DBUS_CONNECTION (object), result, &error);
256
257 if (error == NULL)
258 dex_async_pair_return_object (async_pair, message);
259 else
260 dex_async_pair_return_error (async_pair, error);
261
262 dex_unref (async_pair);
263 }
264
265 /**
266 * dex_dbus_connection_send_message_with_reply:
267 * @connection: a [[email protected]]
268 * @message: a [[email protected]]
269 * @flags: a set of [[email protected]]
270 * @timeout_msec: timeout in milliseconds, or -1 for default, or %G_MAXINT
271 * for no timeout.
272 * @out_serial: (out) (optional): a location for the message serial number
273 *
274 * Wrapper for [[email protected]_message_with_reply].
275 *
276 * Returns: (transfer full): a [[email protected]] that will resolve to a
277 * [[email protected]] or reject with failure.
278 *
279 * Since: 0.4
280 */
281 DexFuture *
282 dex_dbus_connection_send_message_with_reply (GDBusConnection *connection,
283 GDBusMessage *message,
284 GDBusSendMessageFlags flags,
285 int timeout_msec,
286 guint32 *out_serial)
287 {
288 DexAsyncPair *async_pair;
289
290 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
291 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
292
293 async_pair = create_async_pair (G_STRFUNC);
294
295 g_dbus_connection_send_message_with_reply (connection,
296 message,
297 flags,
298 timeout_msec,
299 out_serial,
300 async_pair->cancellable,
301 dex_dbus_connection_send_message_with_reply_cb,
302 dex_ref (async_pair));
303
304 return DEX_FUTURE (async_pair);
305 }
306
307 static void
308 dex_dbus_connection_call_cb (GObject *object,
309 GAsyncResult *result,
310 gpointer user_data)
311 {
312 DexAsyncPair *async_pair = user_data;
313 GVariant *reply = NULL;
314 GError *error = NULL;
315
316 reply = g_dbus_connection_call_finish (G_DBUS_CONNECTION (object), result, &error);
317
318 if (error == NULL)
319 dex_async_pair_return_variant (async_pair, reply);
320 else
321 dex_async_pair_return_error (async_pair, error);
322
323 dex_unref (async_pair);
324 }
325
326 /**
327 * dex_dbus_connection_call:
328 * @connection: a [[email protected]]
329 * @bus_name: (nullable): a unique or well-known bus name or %NULL if
330 * @connection is not a message bus connection
331 * @object_path: path of remote object
332 * @interface_name: D-Bus interface to invoke method on
333 * @method_name: the name of the method to invoke
334 * @parameters: (nullable): a [[email protected]] tuple with parameters for
335 * the method or %NULL if not passing parameters
336 * @reply_type: (nullable): the expected type of the reply (which will be a
337 * tuple), or %NULL
338 * @flags: flags from the [[email protected]] enumeration
339 * @timeout_msec: the timeout in milliseconds, -1 to use the default
340 * timeout or %G_MAXINT for no timeout
341 *
342 * Wrapper for [[email protected]].
343 *
344 * Returns: (transfer full): a [[email protected]] that resolves to a
345 * [[email protected]] or rejects with error.
346 *
347 * Since: 0.4
348 */
349 DexFuture *
350 dex_dbus_connection_call (GDBusConnection *connection,
351 const char *bus_name,
352 const char *object_path,
353 const char *interface_name,
354 const char *method_name,
355 GVariant *parameters,
356 const GVariantType *reply_type,
357 GDBusCallFlags flags,
358 int timeout_msec)
359 {
360 DexAsyncPair *async_pair;
361
362 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
363
364 async_pair = create_async_pair (G_STRFUNC);
365
366 g_dbus_connection_call (connection,
367 bus_name,
368 object_path,
369 interface_name,
370 method_name,
371 parameters,
372 reply_type,
373 flags,
374 timeout_msec,
375 async_pair->cancellable,
376 dex_dbus_connection_call_cb,
377 dex_ref (async_pair));
378
379 return DEX_FUTURE (async_pair);
380 }
381
382 #ifdef G_OS_UNIX
383 static void
384 dex_dbus_connection_call_with_unix_fd_list_cb (GObject *object,
385 GAsyncResult *result,
386 gpointer user_data)
387 {
388 DexFutureSet *future_set = user_data;
389 DexAsyncPair *async_pair;
390 DexPromise *promise;
391 GUnixFDList *fd_list = NULL;
392 GVariant *reply = NULL;
393 GError *error = NULL;
394
395 g_assert (G_IS_DBUS_CONNECTION (object));
396 g_assert (DEX_IS_FUTURE_SET (future_set));
397
398 async_pair = DEX_ASYNC_PAIR (dex_future_set_get_future_at (future_set, 0));
399 promise = DEX_PROMISE (dex_future_set_get_future_at (future_set, 1));
400
401 g_assert (DEX_IS_ASYNC_PAIR (async_pair));
402 g_assert (DEX_IS_PROMISE (promise));
403
404 reply = g_dbus_connection_call_with_unix_fd_list_finish (G_DBUS_CONNECTION (object), &fd_list, result, &error);
405
406 g_assert (!fd_list || G_IS_UNIX_FD_LIST (fd_list));
407 g_assert (reply != NULL || error != NULL);
408
409 if (error == NULL)
410 {
411 dex_promise_resolve_object (promise, fd_list);
412 dex_async_pair_return_variant (async_pair, reply);
413 }
414 else
415 {
416 dex_promise_reject (promise, g_error_copy (error));
417 dex_async_pair_return_error (async_pair, error);
418 }
419
420 dex_unref (future_set);
421 }
422
423 /**
424 * dex_dbus_connection_call_with_unix_fd_list:
425 * @connection: a [[email protected]]
426 * @bus_name: (nullable): a unique or well-known bus name or %NULL if
427 * @connection is not a message bus connection
428 * @object_path: path of remote object
429 * @interface_name: D-Bus interface to invoke method on
430 * @method_name: the name of the method to invoke
431 * @parameters: (nullable): a [[email protected]] tuple with parameters for
432 * the method or %NULL if not passing parameters
433 * @reply_type: (nullable): the expected type of the reply (which will be a
434 * tuple), or %NULL
435 * @flags: flags from the [[email protected]] enumeration
436 * @timeout_msec: the timeout in milliseconds, -1 to use the default
437 * timeout or %G_MAXINT for no timeout
438 * @fd_list: (nullable): a [[email protected]]
439 *
440 * Wrapper for [[email protected]_with_unix_fd_list].
441 *
442 * Returns: (transfer full): a [[email protected]] that resolves to a
443 * [[email protected]].
444 *
445 * The [[email protected]] containing the resulting [[email protected]] can
446 * be retrieved with [[email protected]_future_at] with an index of 1.
447 *
448 * Since: 0.4
449 */
450 DexFuture *
451 dex_dbus_connection_call_with_unix_fd_list (GDBusConnection *connection,
452 const char *bus_name,
453 const char *object_path,
454 const char *interface_name,
455 const char *method_name,
456 GVariant *parameters,
457 const GVariantType *reply_type,
458 GDBusCallFlags flags,
459 int timeout_msec,
460 GUnixFDList *fd_list)
461 {
462 DexAsyncPair *async_pair;
463 DexPromise *promise;
464 DexFuture *ret;
465
466 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
467 g_return_val_if_fail (!fd_list || G_IS_UNIX_FD_LIST (fd_list), NULL);
468
469 /* Will hold our GVariant result */
470 async_pair = create_async_pair (G_STRFUNC);
471
472 /* Will hold our GUnixFDList result */
473 promise = dex_promise_new ();
474
475 /* Sent to user. Resolving will contain variant. */
476 ret = dex_future_all (DEX_FUTURE (async_pair), DEX_FUTURE (promise), NULL);
477
478 g_dbus_connection_call_with_unix_fd_list (connection,
479 bus_name,
480 object_path,
481 interface_name,
482 method_name,
483 parameters,
484 reply_type,
485 flags,
486 timeout_msec,
487 fd_list,
488 async_pair->cancellable,
489 dex_dbus_connection_call_with_unix_fd_list_cb,
490 dex_ref (ret));
491
492 return ret;
493 }
494 #endif
495
496 static void
497 dex_bus_get_cb (GObject *object,
498 GAsyncResult *result,
499 gpointer user_data)
500 {
501 DexAsyncPair *async_pair = user_data;
502 GDBusConnection *bus;
503 GError *error = NULL;
504
505 bus = g_bus_get_finish (result, &error);
506
507 if (error == NULL)
508 dex_async_pair_return_object (async_pair, bus);
509 else
510 dex_async_pair_return_error (async_pair, error);
511
512 dex_unref (async_pair);
513 }
514
515 /**
516 * dex_bus_get:
517 * @bus_type: the [[email protected]]
518 *
519 * Wrapper for [[email protected]_get].
520 *
521 * Returns: (transfer full): a [[email protected]] that resolves to a
522 * [[email protected]] or rejects with error.
523 *
524 * Since: 0.4
525 */
526 DexFuture *
527 dex_bus_get (GBusType bus_type)
528 {
529 DexAsyncPair *async_pair;
530
531 async_pair = create_async_pair (G_STRFUNC);
532
533 g_bus_get (bus_type,
534 async_pair->cancellable,
535 dex_bus_get_cb,
536 dex_ref (async_pair));
537
538 return DEX_FUTURE (async_pair);
539 }
540
541 typedef struct
542 {
543 DexPromise *name_acquired;
544 gulong name_acquired_cancelled_id;
545 DexPromise *name_lost;
546 gulong name_lost_cancelled_id;
547 guint own_name_id;
548 } BusOwnNameData;
549
550 static void
551 dex_bus_own_name_data_free (BusOwnNameData *data)
552 {
553 g_clear_pointer (&data->name_acquired, dex_unref);
554 g_clear_pointer (&data->name_lost, dex_unref);
555
556 free (data);
557 }
558
559 static void
560 dex_bus_name_acquired_cb (GDBusConnection *connection,
561 const char *name,
562 gpointer user_data)
563 {
564 BusOwnNameData *data = user_data;
565
566 g_cancellable_disconnect (dex_promise_get_cancellable (data->name_acquired),
567 data->name_acquired_cancelled_id);
568 data->name_acquired_cancelled_id = 0;
569
570 dex_promise_resolve_boolean (data->name_acquired, TRUE);
571 }
572
573 static void
574 dex_bus_name_lost_cb (GDBusConnection *connection,
575 const char *name,
576 gpointer user_data)
577 {
578 BusOwnNameData *data = user_data;
579
580 if (dex_future_is_pending (DEX_FUTURE (data->name_acquired)))
581 {
582 g_cancellable_disconnect (dex_promise_get_cancellable (data->name_acquired),
583 data->name_acquired_cancelled_id);
584 data->name_acquired_cancelled_id = 0;
585
586 dex_promise_reject (data->name_acquired,
587 g_error_new_literal (G_IO_ERROR, G_IO_ERROR_FAILED,
588 "Failed to acquire dbus name"));
589 }
590
591 g_cancellable_disconnect (dex_promise_get_cancellable (data->name_lost),
592 data->name_lost_cancelled_id);
593 data->name_lost_cancelled_id = 0;
594
595 dex_promise_reject (data->name_lost,
596 g_error_new_literal (G_IO_ERROR, G_IO_ERROR_FAILED,
597 "Lost dbus name"));
598
599 g_clear_handle_id (&data->own_name_id, g_bus_unown_name);
600 }
601
602 static void
603 dex_bus_name_cancelled_cb (GCancellable *cancellable,
604 gpointer user_data)
605 {
606 BusOwnNameData *data = user_data;
607
608 /* Disconnect the other cancellable. This way, neither will signal anymore, and the remaining one
609 * will get cleaned up when the respective future gets cleaned up. */
610 if (dex_promise_get_cancellable (data->name_acquired) != cancellable &&
611 data->name_acquired_cancelled_id)
612 {
613 g_cancellable_disconnect (dex_promise_get_cancellable (data->name_acquired),
614 data->name_acquired_cancelled_id);
615 data->name_acquired_cancelled_id = 0;
616 }
617 else if (dex_promise_get_cancellable (data->name_lost) != cancellable &&
618 data->name_lost_cancelled_id)
619 {
620 g_cancellable_disconnect (dex_promise_get_cancellable (data->name_lost),
621 data->name_lost_cancelled_id);
622 data->name_lost_cancelled_id = 0;
623 }
624
625 if (dex_future_is_pending (DEX_FUTURE (data->name_acquired)))
626 {
627 dex_promise_reject (data->name_acquired,
628 g_error_new_literal (G_IO_ERROR, G_IO_ERROR_CANCELLED,
629 "Cancelled"));
630 }
631
632 if (dex_future_is_pending (DEX_FUTURE (data->name_lost)))
633 {
634 dex_promise_reject (data->name_lost,
635 g_error_new_literal (G_IO_ERROR, G_IO_ERROR_CANCELLED,
636 "Cancelled"));
637 }
638
639 g_clear_handle_id (&data->own_name_id, g_bus_unown_name);
640 }
641
642 /**
643 * dex_bus_own_name_on_connection:
644 * @connection: The [[email protected]] to own a name on.
645 * @name: The well-known name to own.
646 * @flags: a set of flags with ownership options.
647 * @out_name_acquired_future: (out) (optional): a location for the name acquired future
648 * @out_name_lost_future: (out) (optional): a location for the name lost future
649 *
650 * Wrapper for [[email protected]_own_name].
651 *
652 * Asks the D-Bus broker to own the well-known name @name on the connection @connection.
653 *
654 * @out_name_acquired_future is a future that awaits owning the name and either
655 * resolves to true, or rejects with an error.
656 *
657 * @out_name_lost_future is a future that rejects when the name was lost.
658 *
659 * If either future is canceled, the name will be unowned.
660 *
661 * Since: 1.1
662 */
663 void
664 dex_bus_own_name_on_connection (GDBusConnection *connection,
665 const char *name,
666 GBusNameOwnerFlags flags,
667 DexFuture **out_name_acquired_future,
668 DexFuture **out_name_lost_future)
669 {
670 BusOwnNameData *data = g_new0 (BusOwnNameData, 1);
671
672 data->name_acquired = dex_promise_new_cancellable ();
673 data->name_lost = dex_promise_new_cancellable ();
674
675 data->name_acquired_cancelled_id =
676 g_cancellable_connect (dex_promise_get_cancellable (data->name_acquired),
677 G_CALLBACK (dex_bus_name_cancelled_cb),
678 data, NULL);
679
680 data->name_lost_cancelled_id =
681 g_cancellable_connect (dex_promise_get_cancellable (data->name_lost),
682 G_CALLBACK (dex_bus_name_cancelled_cb),
683 data, NULL);
684
685 data->own_name_id =
686 g_bus_own_name_on_connection (connection,
687 name,
688 flags,
689 dex_bus_name_acquired_cb,
690 dex_bus_name_lost_cb,
691 data,
692 (GDestroyNotify) dex_bus_own_name_data_free);
693
694 if (out_name_acquired_future)
695 *out_name_acquired_future = DEX_FUTURE (dex_ref (data->name_acquired));
696 if (out_name_lost_future)
697 *out_name_lost_future = DEX_FUTURE (dex_ref (data->name_lost));
698 }
699
700 static void
701 dex_dbus_connection_close_cb (GObject *object,
702 GAsyncResult *result,
703 gpointer user_data)
704 {
705 DexPromise *promise = user_data;
706 GError *error = NULL;
707
708 if (!g_dbus_connection_close_finish (G_DBUS_CONNECTION (object), result, &error))
709 dex_promise_reject (promise, g_steal_pointer (&error));
710 else
711 dex_promise_resolve_boolean (promise, TRUE);
712
713 dex_unref (promise);
714 }
715
716 /**
717 * dex_dbus_connection_close:
718 * @connection: a [[email protected]]
719 *
720 * Asynchronously closes a connection.
721 *
722 * Returns: (transfer full): a [[email protected]] that resolves
723 * to `true` or rejects with error.
724 *
725 * Since: 1.0
726 */
727 DexFuture *
728 dex_dbus_connection_close (GDBusConnection *connection)
729 {
730 DexPromise *promise;
731
732 dex_return_error_if_fail (G_IS_DBUS_CONNECTION (connection));
733
734 promise = dex_promise_new_cancellable ();
735 g_dbus_connection_close (connection,
736 dex_promise_get_cancellable (promise),
737 dex_dbus_connection_close_cb,
738 dex_ref (promise));
739 return DEX_FUTURE (promise);
740 }