configure.ac: Remove --disable-znodelete options
[glib.git] / gio / gdbusproxy.c
blobae70583710d865d27611047ce2b15abe01972cff
1 /* GDBus - GLib D-Bus Library
3 * Copyright (C) 2008-2010 Red Hat, Inc.
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.
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.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 * Author: David Zeuthen <davidz@redhat.com>
21 #include "config.h"
23 #include <stdlib.h>
24 #include <string.h>
26 #include "gdbusutils.h"
27 #include "gdbusproxy.h"
28 #include "gioenumtypes.h"
29 #include "gdbusconnection.h"
30 #include "gdbuserror.h"
31 #include "gdbusprivate.h"
32 #include "ginitable.h"
33 #include "gasyncinitable.h"
34 #include "gioerror.h"
35 #include "gtask.h"
36 #include "gcancellable.h"
37 #include "gdbusinterface.h"
38 #include "gasyncresult.h"
40 #ifdef G_OS_UNIX
41 #include "gunixfdlist.h"
42 #endif
44 #include "glibintl.h"
46 /**
47 * SECTION:gdbusproxy
48 * @short_description: Client-side D-Bus interface proxy
49 * @include: gio/gio.h
51 * #GDBusProxy is a base class used for proxies to access a D-Bus
52 * interface on a remote object. A #GDBusProxy can be constructed for
53 * both well-known and unique names.
55 * By default, #GDBusProxy will cache all properties (and listen to
56 * changes) of the remote object, and proxy all signals that get
57 * emitted. This behaviour can be changed by passing suitable
58 * #GDBusProxyFlags when the proxy is created. If the proxy is for a
59 * well-known name, the property cache is flushed when the name owner
60 * vanishes and reloaded when a name owner appears.
62 * If a #GDBusProxy is used for a well-known name, the owner of the
63 * name is tracked and can be read from
64 * #GDBusProxy:g-name-owner. Connect to the #GObject::notify signal to
65 * get notified of changes. Additionally, only signals and property
66 * changes emitted from the current name owner are considered and
67 * calls are always sent to the current name owner. This avoids a
68 * number of race conditions when the name is lost by one owner and
69 * claimed by another. However, if no name owner currently exists,
70 * then calls will be sent to the well-known name which may result in
71 * the message bus launching an owner (unless
72 * %G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START is set).
74 * The generic #GDBusProxy::g-properties-changed and
75 * #GDBusProxy::g-signal signals are not very convenient to work with.
76 * Therefore, the recommended way of working with proxies is to subclass
77 * #GDBusProxy, and have more natural properties and signals in your derived
78 * class. This [example][gdbus-example-gdbus-codegen] shows how this can
79 * easily be done using the [gdbus-codegen][gdbus-codegen] tool.
81 * A #GDBusProxy instance can be used from multiple threads but note
82 * that all signals (e.g. #GDBusProxy::g-signal, #GDBusProxy::g-properties-changed
83 * and #GObject::notify) are emitted in the
84 * [thread-default main context][g-main-context-push-thread-default]
85 * of the thread where the instance was constructed.
87 * An example using a proxy for a well-known name can be found in
88 * [gdbus-example-watch-proxy.c](https://git.gnome.org/browse/glib/tree/gio/tests/gdbus-example-watch-proxy.c)
91 /* lock protecting the mutable properties: name_owner, timeout_msec,
92 * expected_interface, and the properties hash table
94 G_LOCK_DEFINE_STATIC (properties_lock);
96 /* ---------------------------------------------------------------------------------------------------- */
98 G_LOCK_DEFINE_STATIC (signal_subscription_lock);
100 typedef struct
102 volatile gint ref_count;
103 GDBusProxy *proxy;
104 } SignalSubscriptionData;
106 static SignalSubscriptionData *
107 signal_subscription_ref (SignalSubscriptionData *data)
109 g_atomic_int_inc (&data->ref_count);
110 return data;
113 static void
114 signal_subscription_unref (SignalSubscriptionData *data)
116 if (g_atomic_int_dec_and_test (&data->ref_count))
118 g_slice_free (SignalSubscriptionData, data);
122 /* ---------------------------------------------------------------------------------------------------- */
124 struct _GDBusProxyPrivate
126 GBusType bus_type;
127 GDBusProxyFlags flags;
128 GDBusConnection *connection;
130 gchar *name;
131 /* mutable, protected by properties_lock */
132 gchar *name_owner;
133 gchar *object_path;
134 gchar *interface_name;
135 /* mutable, protected by properties_lock */
136 gint timeout_msec;
138 guint name_owner_changed_subscription_id;
140 GCancellable *get_all_cancellable;
142 /* gchar* -> GVariant*, protected by properties_lock */
143 GHashTable *properties;
145 /* mutable, protected by properties_lock */
146 GDBusInterfaceInfo *expected_interface;
148 guint properties_changed_subscription_id;
149 guint signals_subscription_id;
151 gboolean initialized;
153 /* mutable, protected by properties_lock */
154 GDBusObject *object;
156 SignalSubscriptionData *signal_subscription_data;
159 enum
161 PROP_0,
162 PROP_G_CONNECTION,
163 PROP_G_BUS_TYPE,
164 PROP_G_NAME,
165 PROP_G_NAME_OWNER,
166 PROP_G_FLAGS,
167 PROP_G_OBJECT_PATH,
168 PROP_G_INTERFACE_NAME,
169 PROP_G_DEFAULT_TIMEOUT,
170 PROP_G_INTERFACE_INFO
173 enum
175 PROPERTIES_CHANGED_SIGNAL,
176 SIGNAL_SIGNAL,
177 LAST_SIGNAL,
180 static guint signals[LAST_SIGNAL] = {0};
182 static void dbus_interface_iface_init (GDBusInterfaceIface *dbus_interface_iface);
183 static void initable_iface_init (GInitableIface *initable_iface);
184 static void async_initable_iface_init (GAsyncInitableIface *async_initable_iface);
186 G_DEFINE_TYPE_WITH_CODE (GDBusProxy, g_dbus_proxy, G_TYPE_OBJECT,
187 G_ADD_PRIVATE (GDBusProxy)
188 G_IMPLEMENT_INTERFACE (G_TYPE_DBUS_INTERFACE, dbus_interface_iface_init)
189 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, initable_iface_init)
190 G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE, async_initable_iface_init))
192 static void
193 g_dbus_proxy_dispose (GObject *object)
195 GDBusProxy *proxy = G_DBUS_PROXY (object);
196 G_LOCK (signal_subscription_lock);
197 if (proxy->priv->signal_subscription_data != NULL)
199 proxy->priv->signal_subscription_data->proxy = NULL;
200 signal_subscription_unref (proxy->priv->signal_subscription_data);
201 proxy->priv->signal_subscription_data = NULL;
203 G_UNLOCK (signal_subscription_lock);
205 G_OBJECT_CLASS (g_dbus_proxy_parent_class)->dispose (object);
208 static void
209 g_dbus_proxy_finalize (GObject *object)
211 GDBusProxy *proxy = G_DBUS_PROXY (object);
213 g_warn_if_fail (proxy->priv->get_all_cancellable == NULL);
215 if (proxy->priv->name_owner_changed_subscription_id > 0)
216 g_dbus_connection_signal_unsubscribe (proxy->priv->connection,
217 proxy->priv->name_owner_changed_subscription_id);
219 if (proxy->priv->properties_changed_subscription_id > 0)
220 g_dbus_connection_signal_unsubscribe (proxy->priv->connection,
221 proxy->priv->properties_changed_subscription_id);
223 if (proxy->priv->signals_subscription_id > 0)
224 g_dbus_connection_signal_unsubscribe (proxy->priv->connection,
225 proxy->priv->signals_subscription_id);
227 if (proxy->priv->connection != NULL)
228 g_object_unref (proxy->priv->connection);
229 g_free (proxy->priv->name);
230 g_free (proxy->priv->name_owner);
231 g_free (proxy->priv->object_path);
232 g_free (proxy->priv->interface_name);
233 if (proxy->priv->properties != NULL)
234 g_hash_table_unref (proxy->priv->properties);
236 if (proxy->priv->expected_interface != NULL)
238 g_dbus_interface_info_cache_release (proxy->priv->expected_interface);
239 g_dbus_interface_info_unref (proxy->priv->expected_interface);
242 if (proxy->priv->object != NULL)
243 g_object_remove_weak_pointer (G_OBJECT (proxy->priv->object), (gpointer *) &proxy->priv->object);
245 G_OBJECT_CLASS (g_dbus_proxy_parent_class)->finalize (object);
248 static void
249 g_dbus_proxy_get_property (GObject *object,
250 guint prop_id,
251 GValue *value,
252 GParamSpec *pspec)
254 GDBusProxy *proxy = G_DBUS_PROXY (object);
256 switch (prop_id)
258 case PROP_G_CONNECTION:
259 g_value_set_object (value, proxy->priv->connection);
260 break;
262 case PROP_G_FLAGS:
263 g_value_set_flags (value, proxy->priv->flags);
264 break;
266 case PROP_G_NAME:
267 g_value_set_string (value, proxy->priv->name);
268 break;
270 case PROP_G_NAME_OWNER:
271 g_value_take_string (value, g_dbus_proxy_get_name_owner (proxy));
272 break;
274 case PROP_G_OBJECT_PATH:
275 g_value_set_string (value, proxy->priv->object_path);
276 break;
278 case PROP_G_INTERFACE_NAME:
279 g_value_set_string (value, proxy->priv->interface_name);
280 break;
282 case PROP_G_DEFAULT_TIMEOUT:
283 g_value_set_int (value, g_dbus_proxy_get_default_timeout (proxy));
284 break;
286 case PROP_G_INTERFACE_INFO:
287 g_value_set_boxed (value, g_dbus_proxy_get_interface_info (proxy));
288 break;
290 default:
291 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
292 break;
296 static void
297 g_dbus_proxy_set_property (GObject *object,
298 guint prop_id,
299 const GValue *value,
300 GParamSpec *pspec)
302 GDBusProxy *proxy = G_DBUS_PROXY (object);
304 switch (prop_id)
306 case PROP_G_CONNECTION:
307 proxy->priv->connection = g_value_dup_object (value);
308 break;
310 case PROP_G_FLAGS:
311 proxy->priv->flags = g_value_get_flags (value);
312 break;
314 case PROP_G_NAME:
315 proxy->priv->name = g_value_dup_string (value);
316 break;
318 case PROP_G_OBJECT_PATH:
319 proxy->priv->object_path = g_value_dup_string (value);
320 break;
322 case PROP_G_INTERFACE_NAME:
323 proxy->priv->interface_name = g_value_dup_string (value);
324 break;
326 case PROP_G_DEFAULT_TIMEOUT:
327 g_dbus_proxy_set_default_timeout (proxy, g_value_get_int (value));
328 break;
330 case PROP_G_INTERFACE_INFO:
331 g_dbus_proxy_set_interface_info (proxy, g_value_get_boxed (value));
332 break;
334 case PROP_G_BUS_TYPE:
335 proxy->priv->bus_type = g_value_get_enum (value);
336 break;
338 default:
339 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
340 break;
344 static void
345 g_dbus_proxy_class_init (GDBusProxyClass *klass)
347 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
349 gobject_class->dispose = g_dbus_proxy_dispose;
350 gobject_class->finalize = g_dbus_proxy_finalize;
351 gobject_class->set_property = g_dbus_proxy_set_property;
352 gobject_class->get_property = g_dbus_proxy_get_property;
354 /* Note that all property names are prefixed to avoid collisions with D-Bus property names
355 * in derived classes */
358 * GDBusProxy:g-interface-info:
360 * Ensure that interactions with this proxy conform to the given
361 * interface. This is mainly to ensure that malformed data received
362 * from the other peer is ignored. The given #GDBusInterfaceInfo is
363 * said to be the "expected interface".
365 * The checks performed are:
366 * - When completing a method call, if the type signature of
367 * the reply message isn't what's expected, the reply is
368 * discarded and the #GError is set to %G_IO_ERROR_INVALID_ARGUMENT.
370 * - Received signals that have a type signature mismatch are dropped and
371 * a warning is logged via g_warning().
373 * - Properties received via the initial `GetAll()` call or via the
374 * `::PropertiesChanged` signal (on the
375 * [org.freedesktop.DBus.Properties](http://dbus.freedesktop.org/doc/dbus-specification.html#standard-interfaces-properties)
376 * interface) or set using g_dbus_proxy_set_cached_property()
377 * with a type signature mismatch are ignored and a warning is
378 * logged via g_warning().
380 * Note that these checks are never done on methods, signals and
381 * properties that are not referenced in the given
382 * #GDBusInterfaceInfo, since extending a D-Bus interface on the
383 * service-side is not considered an ABI break.
385 * Since: 2.26
387 g_object_class_install_property (gobject_class,
388 PROP_G_INTERFACE_INFO,
389 g_param_spec_boxed ("g-interface-info",
390 P_("Interface Information"),
391 P_("Interface Information"),
392 G_TYPE_DBUS_INTERFACE_INFO,
393 G_PARAM_READABLE |
394 G_PARAM_WRITABLE |
395 G_PARAM_STATIC_NAME |
396 G_PARAM_STATIC_BLURB |
397 G_PARAM_STATIC_NICK));
400 * GDBusProxy:g-connection:
402 * The #GDBusConnection the proxy is for.
404 * Since: 2.26
406 g_object_class_install_property (gobject_class,
407 PROP_G_CONNECTION,
408 g_param_spec_object ("g-connection",
409 P_("g-connection"),
410 P_("The connection the proxy is for"),
411 G_TYPE_DBUS_CONNECTION,
412 G_PARAM_READABLE |
413 G_PARAM_WRITABLE |
414 G_PARAM_CONSTRUCT_ONLY |
415 G_PARAM_STATIC_NAME |
416 G_PARAM_STATIC_BLURB |
417 G_PARAM_STATIC_NICK));
420 * GDBusProxy:g-bus-type:
422 * If this property is not %G_BUS_TYPE_NONE, then
423 * #GDBusProxy:g-connection must be %NULL and will be set to the
424 * #GDBusConnection obtained by calling g_bus_get() with the value
425 * of this property.
427 * Since: 2.26
429 g_object_class_install_property (gobject_class,
430 PROP_G_BUS_TYPE,
431 g_param_spec_enum ("g-bus-type",
432 P_("Bus Type"),
433 P_("The bus to connect to, if any"),
434 G_TYPE_BUS_TYPE,
435 G_BUS_TYPE_NONE,
436 G_PARAM_WRITABLE |
437 G_PARAM_CONSTRUCT_ONLY |
438 G_PARAM_STATIC_NAME |
439 G_PARAM_STATIC_BLURB |
440 G_PARAM_STATIC_NICK));
443 * GDBusProxy:g-flags:
445 * Flags from the #GDBusProxyFlags enumeration.
447 * Since: 2.26
449 g_object_class_install_property (gobject_class,
450 PROP_G_FLAGS,
451 g_param_spec_flags ("g-flags",
452 P_("g-flags"),
453 P_("Flags for the proxy"),
454 G_TYPE_DBUS_PROXY_FLAGS,
455 G_DBUS_PROXY_FLAGS_NONE,
456 G_PARAM_READABLE |
457 G_PARAM_WRITABLE |
458 G_PARAM_CONSTRUCT_ONLY |
459 G_PARAM_STATIC_NAME |
460 G_PARAM_STATIC_BLURB |
461 G_PARAM_STATIC_NICK));
464 * GDBusProxy:g-name:
466 * The well-known or unique name that the proxy is for.
468 * Since: 2.26
470 g_object_class_install_property (gobject_class,
471 PROP_G_NAME,
472 g_param_spec_string ("g-name",
473 P_("g-name"),
474 P_("The well-known or unique name that the proxy is for"),
475 NULL,
476 G_PARAM_READABLE |
477 G_PARAM_WRITABLE |
478 G_PARAM_CONSTRUCT_ONLY |
479 G_PARAM_STATIC_NAME |
480 G_PARAM_STATIC_BLURB |
481 G_PARAM_STATIC_NICK));
484 * GDBusProxy:g-name-owner:
486 * The unique name that owns #GDBusProxy:g-name or %NULL if no-one
487 * currently owns that name. You may connect to #GObject::notify signal to
488 * track changes to this property.
490 * Since: 2.26
492 g_object_class_install_property (gobject_class,
493 PROP_G_NAME_OWNER,
494 g_param_spec_string ("g-name-owner",
495 P_("g-name-owner"),
496 P_("The unique name for the owner"),
497 NULL,
498 G_PARAM_READABLE |
499 G_PARAM_STATIC_NAME |
500 G_PARAM_STATIC_BLURB |
501 G_PARAM_STATIC_NICK));
504 * GDBusProxy:g-object-path:
506 * The object path the proxy is for.
508 * Since: 2.26
510 g_object_class_install_property (gobject_class,
511 PROP_G_OBJECT_PATH,
512 g_param_spec_string ("g-object-path",
513 P_("g-object-path"),
514 P_("The object path the proxy is for"),
515 NULL,
516 G_PARAM_READABLE |
517 G_PARAM_WRITABLE |
518 G_PARAM_CONSTRUCT_ONLY |
519 G_PARAM_STATIC_NAME |
520 G_PARAM_STATIC_BLURB |
521 G_PARAM_STATIC_NICK));
524 * GDBusProxy:g-interface-name:
526 * The D-Bus interface name the proxy is for.
528 * Since: 2.26
530 g_object_class_install_property (gobject_class,
531 PROP_G_INTERFACE_NAME,
532 g_param_spec_string ("g-interface-name",
533 P_("g-interface-name"),
534 P_("The D-Bus interface name the proxy is for"),
535 NULL,
536 G_PARAM_READABLE |
537 G_PARAM_WRITABLE |
538 G_PARAM_CONSTRUCT_ONLY |
539 G_PARAM_STATIC_NAME |
540 G_PARAM_STATIC_BLURB |
541 G_PARAM_STATIC_NICK));
544 * GDBusProxy:g-default-timeout:
546 * The timeout to use if -1 (specifying default timeout) is passed
547 * as @timeout_msec in the g_dbus_proxy_call() and
548 * g_dbus_proxy_call_sync() functions.
550 * This allows applications to set a proxy-wide timeout for all
551 * remote method invocations on the proxy. If this property is -1,
552 * the default timeout (typically 25 seconds) is used. If set to
553 * %G_MAXINT, then no timeout is used.
555 * Since: 2.26
557 g_object_class_install_property (gobject_class,
558 PROP_G_DEFAULT_TIMEOUT,
559 g_param_spec_int ("g-default-timeout",
560 P_("Default Timeout"),
561 P_("Timeout for remote method invocation"),
563 G_MAXINT,
565 G_PARAM_READABLE |
566 G_PARAM_WRITABLE |
567 G_PARAM_CONSTRUCT |
568 G_PARAM_STATIC_NAME |
569 G_PARAM_STATIC_BLURB |
570 G_PARAM_STATIC_NICK));
573 * GDBusProxy::g-properties-changed:
574 * @proxy: The #GDBusProxy emitting the signal.
575 * @changed_properties: A #GVariant containing the properties that changed (type: `a{sv}`)
576 * @invalidated_properties: A %NULL terminated array of properties that was invalidated
578 * Emitted when one or more D-Bus properties on @proxy changes. The
579 * local cache has already been updated when this signal fires. Note
580 * that both @changed_properties and @invalidated_properties are
581 * guaranteed to never be %NULL (either may be empty though).
583 * If the proxy has the flag
584 * %G_DBUS_PROXY_FLAGS_GET_INVALIDATED_PROPERTIES set, then
585 * @invalidated_properties will always be empty.
587 * This signal corresponds to the
588 * `PropertiesChanged` D-Bus signal on the
589 * `org.freedesktop.DBus.Properties` interface.
591 * Since: 2.26
593 signals[PROPERTIES_CHANGED_SIGNAL] = g_signal_new (I_("g-properties-changed"),
594 G_TYPE_DBUS_PROXY,
595 G_SIGNAL_RUN_LAST | G_SIGNAL_MUST_COLLECT,
596 G_STRUCT_OFFSET (GDBusProxyClass, g_properties_changed),
597 NULL,
598 NULL,
599 NULL,
600 G_TYPE_NONE,
602 G_TYPE_VARIANT,
603 G_TYPE_STRV | G_SIGNAL_TYPE_STATIC_SCOPE);
606 * GDBusProxy::g-signal:
607 * @proxy: The #GDBusProxy emitting the signal.
608 * @sender_name: (nullable): The sender of the signal or %NULL if the connection is not a bus connection.
609 * @signal_name: The name of the signal.
610 * @parameters: A #GVariant tuple with parameters for the signal.
612 * Emitted when a signal from the remote object and interface that @proxy is for, has been received.
614 * Since: 2.26
616 signals[SIGNAL_SIGNAL] = g_signal_new (I_("g-signal"),
617 G_TYPE_DBUS_PROXY,
618 G_SIGNAL_RUN_LAST | G_SIGNAL_MUST_COLLECT,
619 G_STRUCT_OFFSET (GDBusProxyClass, g_signal),
620 NULL,
621 NULL,
622 NULL,
623 G_TYPE_NONE,
625 G_TYPE_STRING,
626 G_TYPE_STRING,
627 G_TYPE_VARIANT);
631 static void
632 g_dbus_proxy_init (GDBusProxy *proxy)
634 proxy->priv = g_dbus_proxy_get_instance_private (proxy);
635 proxy->priv->signal_subscription_data = g_slice_new0 (SignalSubscriptionData);
636 proxy->priv->signal_subscription_data->ref_count = 1;
637 proxy->priv->signal_subscription_data->proxy = proxy;
638 proxy->priv->properties = g_hash_table_new_full (g_str_hash,
639 g_str_equal,
640 g_free,
641 (GDestroyNotify) g_variant_unref);
644 /* ---------------------------------------------------------------------------------------------------- */
646 static gint
647 property_name_sort_func (const gchar **a,
648 const gchar **b)
650 return g_strcmp0 (*a, *b);
654 * g_dbus_proxy_get_cached_property_names:
655 * @proxy: A #GDBusProxy.
657 * Gets the names of all cached properties on @proxy.
659 * Returns: (transfer full) (nullable) (array zero-terminated=1): A
660 * %NULL-terminated array of strings or %NULL if
661 * @proxy has no cached properties. Free the returned array with
662 * g_strfreev().
664 * Since: 2.26
666 gchar **
667 g_dbus_proxy_get_cached_property_names (GDBusProxy *proxy)
669 gchar **names;
670 GPtrArray *p;
671 GHashTableIter iter;
672 const gchar *key;
674 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
676 G_LOCK (properties_lock);
678 names = NULL;
679 if (g_hash_table_size (proxy->priv->properties) == 0)
680 goto out;
682 p = g_ptr_array_new ();
684 g_hash_table_iter_init (&iter, proxy->priv->properties);
685 while (g_hash_table_iter_next (&iter, (gpointer) &key, NULL))
686 g_ptr_array_add (p, g_strdup (key));
687 g_ptr_array_sort (p, (GCompareFunc) property_name_sort_func);
688 g_ptr_array_add (p, NULL);
690 names = (gchar **) g_ptr_array_free (p, FALSE);
692 out:
693 G_UNLOCK (properties_lock);
694 return names;
697 /* properties_lock must be held for as long as you will keep the
698 * returned value
700 static const GDBusPropertyInfo *
701 lookup_property_info (GDBusProxy *proxy,
702 const gchar *property_name)
704 const GDBusPropertyInfo *info = NULL;
706 if (proxy->priv->expected_interface == NULL)
707 goto out;
709 info = g_dbus_interface_info_lookup_property (proxy->priv->expected_interface, property_name);
711 out:
712 return info;
716 * g_dbus_proxy_get_cached_property:
717 * @proxy: A #GDBusProxy.
718 * @property_name: Property name.
720 * Looks up the value for a property from the cache. This call does no
721 * blocking IO.
723 * If @proxy has an expected interface (see
724 * #GDBusProxy:g-interface-info) and @property_name is referenced by
725 * it, then @value is checked against the type of the property.
727 * Returns: (transfer full) (nullable): A reference to the #GVariant instance
728 * that holds the value for @property_name or %NULL if the value is not in
729 * the cache. The returned reference must be freed with g_variant_unref().
731 * Since: 2.26
733 GVariant *
734 g_dbus_proxy_get_cached_property (GDBusProxy *proxy,
735 const gchar *property_name)
737 const GDBusPropertyInfo *info;
738 GVariant *value;
740 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
741 g_return_val_if_fail (property_name != NULL, NULL);
743 G_LOCK (properties_lock);
745 value = g_hash_table_lookup (proxy->priv->properties, property_name);
746 if (value == NULL)
747 goto out;
749 info = lookup_property_info (proxy, property_name);
750 if (info != NULL)
752 const gchar *type_string = g_variant_get_type_string (value);
753 if (g_strcmp0 (type_string, info->signature) != 0)
755 g_warning ("Trying to get property %s with type %s but according to the expected "
756 "interface the type is %s",
757 property_name,
758 type_string,
759 info->signature);
760 value = NULL;
761 goto out;
765 g_variant_ref (value);
767 out:
768 G_UNLOCK (properties_lock);
769 return value;
773 * g_dbus_proxy_set_cached_property:
774 * @proxy: A #GDBusProxy
775 * @property_name: Property name.
776 * @value: (nullable): Value for the property or %NULL to remove it from the cache.
778 * If @value is not %NULL, sets the cached value for the property with
779 * name @property_name to the value in @value.
781 * If @value is %NULL, then the cached value is removed from the
782 * property cache.
784 * If @proxy has an expected interface (see
785 * #GDBusProxy:g-interface-info) and @property_name is referenced by
786 * it, then @value is checked against the type of the property.
788 * If the @value #GVariant is floating, it is consumed. This allows
789 * convenient 'inline' use of g_variant_new(), e.g.
790 * |[<!-- language="C" -->
791 * g_dbus_proxy_set_cached_property (proxy,
792 * "SomeProperty",
793 * g_variant_new ("(si)",
794 * "A String",
795 * 42));
796 * ]|
798 * Normally you will not need to use this method since @proxy
799 * is tracking changes using the
800 * `org.freedesktop.DBus.Properties.PropertiesChanged`
801 * D-Bus signal. However, for performance reasons an object may
802 * decide to not use this signal for some properties and instead
803 * use a proprietary out-of-band mechanism to transmit changes.
805 * As a concrete example, consider an object with a property
806 * `ChatroomParticipants` which is an array of strings. Instead of
807 * transmitting the same (long) array every time the property changes,
808 * it is more efficient to only transmit the delta using e.g. signals
809 * `ChatroomParticipantJoined(String name)` and
810 * `ChatroomParticipantParted(String name)`.
812 * Since: 2.26
814 void
815 g_dbus_proxy_set_cached_property (GDBusProxy *proxy,
816 const gchar *property_name,
817 GVariant *value)
819 const GDBusPropertyInfo *info;
821 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
822 g_return_if_fail (property_name != NULL);
824 G_LOCK (properties_lock);
826 if (value != NULL)
828 info = lookup_property_info (proxy, property_name);
829 if (info != NULL)
831 if (g_strcmp0 (info->signature, g_variant_get_type_string (value)) != 0)
833 g_warning ("Trying to set property %s of type %s but according to the expected "
834 "interface the type is %s",
835 property_name,
836 g_variant_get_type_string (value),
837 info->signature);
838 goto out;
841 g_hash_table_insert (proxy->priv->properties,
842 g_strdup (property_name),
843 g_variant_ref_sink (value));
845 else
847 g_hash_table_remove (proxy->priv->properties, property_name);
850 out:
851 G_UNLOCK (properties_lock);
854 /* ---------------------------------------------------------------------------------------------------- */
856 static void
857 on_signal_received (GDBusConnection *connection,
858 const gchar *sender_name,
859 const gchar *object_path,
860 const gchar *interface_name,
861 const gchar *signal_name,
862 GVariant *parameters,
863 gpointer user_data)
865 SignalSubscriptionData *data = user_data;
866 GDBusProxy *proxy;
868 G_LOCK (signal_subscription_lock);
869 proxy = data->proxy;
870 if (proxy == NULL)
872 G_UNLOCK (signal_subscription_lock);
873 return;
875 else
877 g_object_ref (proxy);
878 G_UNLOCK (signal_subscription_lock);
881 if (!proxy->priv->initialized)
882 goto out;
884 G_LOCK (properties_lock);
886 if (proxy->priv->name_owner != NULL && g_strcmp0 (sender_name, proxy->priv->name_owner) != 0)
888 G_UNLOCK (properties_lock);
889 goto out;
892 if (proxy->priv->expected_interface != NULL)
894 const GDBusSignalInfo *info;
895 info = g_dbus_interface_info_lookup_signal (proxy->priv->expected_interface, signal_name);
896 if (info != NULL)
898 GVariantType *expected_type;
899 expected_type = _g_dbus_compute_complete_signature (info->args);
900 if (!g_variant_type_equal (expected_type, g_variant_get_type (parameters)))
902 gchar *expected_type_string = g_variant_type_dup_string (expected_type);
903 g_warning ("Dropping signal %s of type %s since the type from the expected interface is %s",
904 info->name,
905 g_variant_get_type_string (parameters),
906 expected_type_string);
907 g_free (expected_type_string);
908 g_variant_type_free (expected_type);
909 G_UNLOCK (properties_lock);
910 goto out;
912 g_variant_type_free (expected_type);
916 G_UNLOCK (properties_lock);
918 g_signal_emit (proxy,
919 signals[SIGNAL_SIGNAL],
921 sender_name,
922 signal_name,
923 parameters);
925 out:
926 if (proxy != NULL)
927 g_object_unref (proxy);
930 /* ---------------------------------------------------------------------------------------------------- */
932 /* must hold properties_lock */
933 static void
934 insert_property_checked (GDBusProxy *proxy,
935 gchar *property_name,
936 GVariant *value)
938 if (proxy->priv->expected_interface != NULL)
940 const GDBusPropertyInfo *info;
941 info = g_dbus_interface_info_lookup_property (proxy->priv->expected_interface, property_name);
942 /* Only check known properties */
943 if (info != NULL)
945 /* Warn about properties with the wrong type */
946 if (g_strcmp0 (info->signature, g_variant_get_type_string (value)) != 0)
948 g_warning ("Received property %s with type %s does not match expected type "
949 "%s in the expected interface",
950 property_name,
951 g_variant_get_type_string (value),
952 info->signature);
953 goto invalid;
958 g_hash_table_insert (proxy->priv->properties,
959 property_name, /* adopts string */
960 value); /* adopts value */
962 return;
964 invalid:
965 g_variant_unref (value);
966 g_free (property_name);
969 typedef struct
971 GDBusProxy *proxy;
972 gchar *prop_name;
973 } InvalidatedPropGetData;
975 static void
976 invalidated_property_get_cb (GDBusConnection *connection,
977 GAsyncResult *res,
978 gpointer user_data)
980 InvalidatedPropGetData *data = user_data;
981 const gchar *invalidated_properties[] = {NULL};
982 GVariantBuilder builder;
983 GVariant *value = NULL;
984 GVariant *unpacked_value = NULL;
986 /* errors are fine, the other end could have disconnected */
987 value = g_dbus_connection_call_finish (connection, res, NULL);
988 if (value == NULL)
990 goto out;
993 if (!g_variant_is_of_type (value, G_VARIANT_TYPE ("(v)")))
995 g_warning ("Expected type '(v)' for Get() reply, got '%s'", g_variant_get_type_string (value));
996 goto out;
999 g_variant_get (value, "(v)", &unpacked_value);
1001 /* synthesize the a{sv} in the PropertiesChanged signal */
1002 g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
1003 g_variant_builder_add (&builder, "{sv}", data->prop_name, unpacked_value);
1005 G_LOCK (properties_lock);
1006 insert_property_checked (data->proxy,
1007 data->prop_name, /* adopts string */
1008 unpacked_value); /* adopts value */
1009 data->prop_name = NULL;
1010 G_UNLOCK (properties_lock);
1012 g_signal_emit (data->proxy,
1013 signals[PROPERTIES_CHANGED_SIGNAL], 0,
1014 g_variant_builder_end (&builder), /* consumed */
1015 invalidated_properties);
1018 out:
1019 if (value != NULL)
1020 g_variant_unref (value);
1021 g_object_unref (data->proxy);
1022 g_free (data->prop_name);
1023 g_slice_free (InvalidatedPropGetData, data);
1026 static void
1027 on_properties_changed (GDBusConnection *connection,
1028 const gchar *sender_name,
1029 const gchar *object_path,
1030 const gchar *interface_name,
1031 const gchar *signal_name,
1032 GVariant *parameters,
1033 gpointer user_data)
1035 SignalSubscriptionData *data = user_data;
1036 gboolean emit_g_signal = FALSE;
1037 GDBusProxy *proxy;
1038 const gchar *interface_name_for_signal;
1039 GVariant *changed_properties;
1040 gchar **invalidated_properties;
1041 GVariantIter iter;
1042 gchar *key;
1043 GVariant *value;
1044 guint n;
1046 changed_properties = NULL;
1047 invalidated_properties = NULL;
1049 G_LOCK (signal_subscription_lock);
1050 proxy = data->proxy;
1051 if (proxy == NULL)
1053 G_UNLOCK (signal_subscription_lock);
1054 goto out;
1056 else
1058 g_object_ref (proxy);
1059 G_UNLOCK (signal_subscription_lock);
1062 if (!proxy->priv->initialized)
1063 goto out;
1065 G_LOCK (properties_lock);
1067 if (proxy->priv->name_owner != NULL && g_strcmp0 (sender_name, proxy->priv->name_owner) != 0)
1069 G_UNLOCK (properties_lock);
1070 goto out;
1073 if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(sa{sv}as)")))
1075 g_warning ("Value for PropertiesChanged signal with type '%s' does not match '(sa{sv}as)'",
1076 g_variant_get_type_string (parameters));
1077 G_UNLOCK (properties_lock);
1078 goto out;
1081 g_variant_get (parameters,
1082 "(&s@a{sv}^a&s)",
1083 &interface_name_for_signal,
1084 &changed_properties,
1085 &invalidated_properties);
1087 if (g_strcmp0 (interface_name_for_signal, proxy->priv->interface_name) != 0)
1089 G_UNLOCK (properties_lock);
1090 goto out;
1093 g_variant_iter_init (&iter, changed_properties);
1094 while (g_variant_iter_next (&iter, "{sv}", &key, &value))
1096 insert_property_checked (proxy,
1097 key, /* adopts string */
1098 value); /* adopts value */
1099 emit_g_signal = TRUE;
1102 if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_GET_INVALIDATED_PROPERTIES)
1104 if (proxy->priv->name_owner != NULL)
1106 for (n = 0; invalidated_properties[n] != NULL; n++)
1108 InvalidatedPropGetData *data;
1109 data = g_slice_new0 (InvalidatedPropGetData);
1110 data->proxy = g_object_ref (proxy);
1111 data->prop_name = g_strdup (invalidated_properties[n]);
1112 g_dbus_connection_call (proxy->priv->connection,
1113 proxy->priv->name_owner,
1114 proxy->priv->object_path,
1115 "org.freedesktop.DBus.Properties",
1116 "Get",
1117 g_variant_new ("(ss)", proxy->priv->interface_name, data->prop_name),
1118 G_VARIANT_TYPE ("(v)"),
1119 G_DBUS_CALL_FLAGS_NONE,
1120 -1, /* timeout */
1121 NULL, /* GCancellable */
1122 (GAsyncReadyCallback) invalidated_property_get_cb,
1123 data);
1127 else
1129 emit_g_signal = TRUE;
1130 for (n = 0; invalidated_properties[n] != NULL; n++)
1132 g_hash_table_remove (proxy->priv->properties, invalidated_properties[n]);
1136 G_UNLOCK (properties_lock);
1138 if (emit_g_signal)
1140 g_signal_emit (proxy, signals[PROPERTIES_CHANGED_SIGNAL],
1142 changed_properties,
1143 invalidated_properties);
1146 out:
1147 if (changed_properties != NULL)
1148 g_variant_unref (changed_properties);
1149 g_free (invalidated_properties);
1150 if (proxy != NULL)
1151 g_object_unref (proxy);
1154 /* ---------------------------------------------------------------------------------------------------- */
1156 static void
1157 process_get_all_reply (GDBusProxy *proxy,
1158 GVariant *result)
1160 GVariantIter *iter;
1161 gchar *key;
1162 GVariant *value;
1163 guint num_properties;
1165 if (!g_variant_is_of_type (result, G_VARIANT_TYPE ("(a{sv})")))
1167 g_warning ("Value for GetAll reply with type '%s' does not match '(a{sv})'",
1168 g_variant_get_type_string (result));
1169 goto out;
1172 G_LOCK (properties_lock);
1174 g_variant_get (result, "(a{sv})", &iter);
1175 while (g_variant_iter_next (iter, "{sv}", &key, &value))
1177 insert_property_checked (proxy,
1178 key, /* adopts string */
1179 value); /* adopts value */
1181 g_variant_iter_free (iter);
1183 num_properties = g_hash_table_size (proxy->priv->properties);
1184 G_UNLOCK (properties_lock);
1186 /* Synthesize ::g-properties-changed changed */
1187 if (num_properties > 0)
1189 GVariant *changed_properties;
1190 const gchar *invalidated_properties[1] = {NULL};
1192 g_variant_get (result,
1193 "(@a{sv})",
1194 &changed_properties);
1195 g_signal_emit (proxy, signals[PROPERTIES_CHANGED_SIGNAL],
1197 changed_properties,
1198 invalidated_properties);
1199 g_variant_unref (changed_properties);
1202 out:
1206 typedef struct
1208 GDBusProxy *proxy;
1209 GCancellable *cancellable;
1210 gchar *name_owner;
1211 } LoadPropertiesOnNameOwnerChangedData;
1213 static void
1214 on_name_owner_changed_get_all_cb (GDBusConnection *connection,
1215 GAsyncResult *res,
1216 gpointer user_data)
1218 LoadPropertiesOnNameOwnerChangedData *data = user_data;
1219 GVariant *result;
1220 GError *error;
1221 gboolean cancelled;
1223 cancelled = FALSE;
1225 error = NULL;
1226 result = g_dbus_connection_call_finish (connection,
1227 res,
1228 &error);
1229 if (result == NULL)
1231 if (error->domain == G_IO_ERROR && error->code == G_IO_ERROR_CANCELLED)
1232 cancelled = TRUE;
1233 /* We just ignore if GetAll() is failing. Because this might happen
1234 * if the object has no properties at all. Or if the caller is
1235 * not authorized to see the properties.
1237 * Either way, apps can know about this by using
1238 * get_cached_property_names() or get_cached_property().
1240 * TODO: handle G_DBUS_DEBUG flag 'proxy' and, if enabled, log the
1241 * fact that GetAll() failed
1243 //g_debug ("error: %d %d %s", error->domain, error->code, error->message);
1244 g_error_free (error);
1247 /* and finally we can notify */
1248 if (!cancelled)
1250 G_LOCK (properties_lock);
1251 g_free (data->proxy->priv->name_owner);
1252 data->proxy->priv->name_owner = data->name_owner;
1253 data->name_owner = NULL; /* to avoid an extra copy, we steal the string */
1254 g_hash_table_remove_all (data->proxy->priv->properties);
1255 G_UNLOCK (properties_lock);
1256 if (result != NULL)
1258 process_get_all_reply (data->proxy, result);
1259 g_variant_unref (result);
1262 g_object_notify (G_OBJECT (data->proxy), "g-name-owner");
1265 if (data->cancellable == data->proxy->priv->get_all_cancellable)
1266 data->proxy->priv->get_all_cancellable = NULL;
1268 g_object_unref (data->proxy);
1269 g_object_unref (data->cancellable);
1270 g_free (data->name_owner);
1271 g_free (data);
1274 static void
1275 on_name_owner_changed (GDBusConnection *connection,
1276 const gchar *sender_name,
1277 const gchar *object_path,
1278 const gchar *interface_name,
1279 const gchar *signal_name,
1280 GVariant *parameters,
1281 gpointer user_data)
1283 SignalSubscriptionData *data = user_data;
1284 GDBusProxy *proxy;
1285 const gchar *old_owner;
1286 const gchar *new_owner;
1288 G_LOCK (signal_subscription_lock);
1289 proxy = data->proxy;
1290 if (proxy == NULL)
1292 G_UNLOCK (signal_subscription_lock);
1293 goto out;
1295 else
1297 g_object_ref (proxy);
1298 G_UNLOCK (signal_subscription_lock);
1301 /* if we are already trying to load properties, cancel that */
1302 if (proxy->priv->get_all_cancellable != NULL)
1304 g_cancellable_cancel (proxy->priv->get_all_cancellable);
1305 proxy->priv->get_all_cancellable = NULL;
1308 g_variant_get (parameters,
1309 "(&s&s&s)",
1310 NULL,
1311 &old_owner,
1312 &new_owner);
1314 if (strlen (new_owner) == 0)
1316 G_LOCK (properties_lock);
1317 g_free (proxy->priv->name_owner);
1318 proxy->priv->name_owner = NULL;
1320 /* Synthesize ::g-properties-changed changed */
1321 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES) &&
1322 g_hash_table_size (proxy->priv->properties) > 0)
1324 GVariantBuilder builder;
1325 GPtrArray *invalidated_properties;
1326 GHashTableIter iter;
1327 const gchar *key;
1329 /* Build changed_properties (always empty) and invalidated_properties ... */
1330 g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
1332 invalidated_properties = g_ptr_array_new_with_free_func (g_free);
1333 g_hash_table_iter_init (&iter, proxy->priv->properties);
1334 while (g_hash_table_iter_next (&iter, (gpointer) &key, NULL))
1335 g_ptr_array_add (invalidated_properties, g_strdup (key));
1336 g_ptr_array_add (invalidated_properties, NULL);
1338 /* ... throw out the properties ... */
1339 g_hash_table_remove_all (proxy->priv->properties);
1341 G_UNLOCK (properties_lock);
1343 /* ... and finally emit the ::g-properties-changed signal */
1344 g_signal_emit (proxy, signals[PROPERTIES_CHANGED_SIGNAL],
1346 g_variant_builder_end (&builder) /* consumed */,
1347 (const gchar* const *) invalidated_properties->pdata);
1348 g_ptr_array_unref (invalidated_properties);
1350 else
1352 G_UNLOCK (properties_lock);
1354 g_object_notify (G_OBJECT (proxy), "g-name-owner");
1356 else
1358 G_LOCK (properties_lock);
1360 /* ignore duplicates - this can happen when activating the service */
1361 if (g_strcmp0 (new_owner, proxy->priv->name_owner) == 0)
1363 G_UNLOCK (properties_lock);
1364 goto out;
1367 if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES)
1369 g_free (proxy->priv->name_owner);
1370 proxy->priv->name_owner = g_strdup (new_owner);
1372 g_hash_table_remove_all (proxy->priv->properties);
1373 G_UNLOCK (properties_lock);
1374 g_object_notify (G_OBJECT (proxy), "g-name-owner");
1376 else
1378 LoadPropertiesOnNameOwnerChangedData *data;
1380 G_UNLOCK (properties_lock);
1382 /* start loading properties.. only then emit notify::g-name-owner .. we
1383 * need to be able to cancel this in the event another NameOwnerChanged
1384 * signal suddenly happens
1387 g_assert (proxy->priv->get_all_cancellable == NULL);
1388 proxy->priv->get_all_cancellable = g_cancellable_new ();
1389 data = g_new0 (LoadPropertiesOnNameOwnerChangedData, 1);
1390 data->proxy = g_object_ref (proxy);
1391 data->cancellable = proxy->priv->get_all_cancellable;
1392 data->name_owner = g_strdup (new_owner);
1393 g_dbus_connection_call (proxy->priv->connection,
1394 data->name_owner,
1395 proxy->priv->object_path,
1396 "org.freedesktop.DBus.Properties",
1397 "GetAll",
1398 g_variant_new ("(s)", proxy->priv->interface_name),
1399 G_VARIANT_TYPE ("(a{sv})"),
1400 G_DBUS_CALL_FLAGS_NONE,
1401 -1, /* timeout */
1402 proxy->priv->get_all_cancellable,
1403 (GAsyncReadyCallback) on_name_owner_changed_get_all_cb,
1404 data);
1408 out:
1409 if (proxy != NULL)
1410 g_object_unref (proxy);
1413 /* ---------------------------------------------------------------------------------------------------- */
1415 static void
1416 async_init_get_all_cb (GDBusConnection *connection,
1417 GAsyncResult *res,
1418 gpointer user_data)
1420 GTask *task = user_data;
1421 GVariant *result;
1422 GError *error;
1424 error = NULL;
1425 result = g_dbus_connection_call_finish (connection,
1426 res,
1427 &error);
1428 if (result == NULL)
1430 /* We just ignore if GetAll() is failing. Because this might happen
1431 * if the object has no properties at all. Or if the caller is
1432 * not authorized to see the properties.
1434 * Either way, apps can know about this by using
1435 * get_cached_property_names() or get_cached_property().
1437 * TODO: handle G_DBUS_DEBUG flag 'proxy' and, if enabled, log the
1438 * fact that GetAll() failed
1440 //g_debug ("error: %d %d %s", error->domain, error->code, error->message);
1441 g_error_free (error);
1444 g_task_return_pointer (task, result,
1445 (GDestroyNotify) g_variant_unref);
1446 g_object_unref (task);
1449 static void
1450 async_init_data_set_name_owner (GTask *task,
1451 const gchar *name_owner)
1453 GDBusProxy *proxy = g_task_get_source_object (task);
1454 gboolean get_all;
1456 if (name_owner != NULL)
1458 G_LOCK (properties_lock);
1459 /* Must free first, since on_name_owner_changed() could run before us */
1460 g_free (proxy->priv->name_owner);
1461 proxy->priv->name_owner = g_strdup (name_owner);
1462 G_UNLOCK (properties_lock);
1465 get_all = TRUE;
1467 if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES)
1469 /* Don't load properties if the API user doesn't want them */
1470 get_all = FALSE;
1472 else if (name_owner == NULL && proxy->priv->name != NULL)
1474 /* Don't attempt to load properties if the name_owner is NULL (which
1475 * usually means the name isn't owned), unless name is also NULL (which
1476 * means we actually wanted to talk to the directly-connected process -
1477 * either dbus-daemon or a peer - instead of going via dbus-daemon)
1479 get_all = FALSE;
1482 if (get_all)
1484 /* load all properties asynchronously */
1485 g_dbus_connection_call (proxy->priv->connection,
1486 name_owner,
1487 proxy->priv->object_path,
1488 "org.freedesktop.DBus.Properties",
1489 "GetAll",
1490 g_variant_new ("(s)", proxy->priv->interface_name),
1491 G_VARIANT_TYPE ("(a{sv})"),
1492 G_DBUS_CALL_FLAGS_NONE,
1493 -1, /* timeout */
1494 g_task_get_cancellable (task),
1495 (GAsyncReadyCallback) async_init_get_all_cb,
1496 task);
1498 else
1500 g_task_return_pointer (task, NULL, NULL);
1501 g_object_unref (task);
1505 static void
1506 async_init_get_name_owner_cb (GDBusConnection *connection,
1507 GAsyncResult *res,
1508 gpointer user_data)
1510 GTask *task = user_data;
1511 GError *error;
1512 GVariant *result;
1514 error = NULL;
1515 result = g_dbus_connection_call_finish (connection,
1516 res,
1517 &error);
1518 if (result == NULL)
1520 if (error->domain == G_DBUS_ERROR &&
1521 error->code == G_DBUS_ERROR_NAME_HAS_NO_OWNER)
1523 g_error_free (error);
1524 async_init_data_set_name_owner (task, NULL);
1526 else
1528 g_task_return_error (task, error);
1529 g_object_unref (task);
1532 else
1534 /* borrowed from result to avoid an extra copy */
1535 const gchar *name_owner;
1537 g_variant_get (result, "(&s)", &name_owner);
1538 async_init_data_set_name_owner (task, name_owner);
1539 g_variant_unref (result);
1543 static void
1544 async_init_call_get_name_owner (GTask *task)
1546 GDBusProxy *proxy = g_task_get_source_object (task);
1548 g_dbus_connection_call (proxy->priv->connection,
1549 "org.freedesktop.DBus", /* name */
1550 "/org/freedesktop/DBus", /* object path */
1551 "org.freedesktop.DBus", /* interface */
1552 "GetNameOwner",
1553 g_variant_new ("(s)",
1554 proxy->priv->name),
1555 G_VARIANT_TYPE ("(s)"),
1556 G_DBUS_CALL_FLAGS_NONE,
1557 -1, /* timeout */
1558 g_task_get_cancellable (task),
1559 (GAsyncReadyCallback) async_init_get_name_owner_cb,
1560 task);
1563 static void
1564 async_init_start_service_by_name_cb (GDBusConnection *connection,
1565 GAsyncResult *res,
1566 gpointer user_data)
1568 GTask *task = user_data;
1569 GDBusProxy *proxy = g_task_get_source_object (task);
1570 GError *error;
1571 GVariant *result;
1573 error = NULL;
1574 result = g_dbus_connection_call_finish (connection,
1575 res,
1576 &error);
1577 if (result == NULL)
1579 /* Errors are not unexpected; the bus will reply e.g.
1581 * org.freedesktop.DBus.Error.ServiceUnknown: The name org.gnome.Epiphany2
1582 * was not provided by any .service files
1584 * or (see #677718)
1586 * org.freedesktop.systemd1.Masked: Unit polkit.service is masked.
1588 * This doesn't mean that the name doesn't have an owner, just
1589 * that it's not provided by a .service file or can't currently
1590 * be started.
1592 * In particular, in both cases, it could be that a service
1593 * owner will actually appear later. So instead of erroring out,
1594 * we just proceed to invoke GetNameOwner() if dealing with the
1595 * kind of errors above.
1597 if (error->domain == G_DBUS_ERROR && error->code == G_DBUS_ERROR_SERVICE_UNKNOWN)
1599 g_error_free (error);
1601 else
1603 gchar *remote_error = g_dbus_error_get_remote_error (error);
1604 if (g_strcmp0 (remote_error, "org.freedesktop.systemd1.Masked") == 0)
1606 g_error_free (error);
1607 g_free (remote_error);
1609 else
1611 g_prefix_error (&error,
1612 _("Error calling StartServiceByName for %s: "),
1613 proxy->priv->name);
1614 g_free (remote_error);
1615 goto failed;
1619 else
1621 guint32 start_service_result;
1622 g_variant_get (result,
1623 "(u)",
1624 &start_service_result);
1625 g_variant_unref (result);
1626 if (start_service_result == 1 || /* DBUS_START_REPLY_SUCCESS */
1627 start_service_result == 2) /* DBUS_START_REPLY_ALREADY_RUNNING */
1629 /* continue to invoke GetNameOwner() */
1631 else
1633 error = g_error_new (G_IO_ERROR,
1634 G_IO_ERROR_FAILED,
1635 _("Unexpected reply %d from StartServiceByName(\"%s\") method"),
1636 start_service_result,
1637 proxy->priv->name);
1638 goto failed;
1642 async_init_call_get_name_owner (task);
1643 return;
1645 failed:
1646 g_warn_if_fail (error != NULL);
1647 g_task_return_error (task, error);
1648 g_object_unref (task);
1651 static void
1652 async_init_call_start_service_by_name (GTask *task)
1654 GDBusProxy *proxy = g_task_get_source_object (task);
1656 g_dbus_connection_call (proxy->priv->connection,
1657 "org.freedesktop.DBus", /* name */
1658 "/org/freedesktop/DBus", /* object path */
1659 "org.freedesktop.DBus", /* interface */
1660 "StartServiceByName",
1661 g_variant_new ("(su)",
1662 proxy->priv->name,
1664 G_VARIANT_TYPE ("(u)"),
1665 G_DBUS_CALL_FLAGS_NONE,
1666 -1, /* timeout */
1667 g_task_get_cancellable (task),
1668 (GAsyncReadyCallback) async_init_start_service_by_name_cb,
1669 task);
1672 static void
1673 async_initable_init_second_async (GAsyncInitable *initable,
1674 gint io_priority,
1675 GCancellable *cancellable,
1676 GAsyncReadyCallback callback,
1677 gpointer user_data)
1679 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1680 GTask *task;
1682 task = g_task_new (proxy, cancellable, callback, user_data);
1683 g_task_set_source_tag (task, async_initable_init_second_async);
1684 g_task_set_priority (task, io_priority);
1686 /* Check name ownership asynchronously - possibly also start the service */
1687 if (proxy->priv->name == NULL)
1689 /* Do nothing */
1690 async_init_data_set_name_owner (task, NULL);
1692 else if (g_dbus_is_unique_name (proxy->priv->name))
1694 async_init_data_set_name_owner (task, proxy->priv->name);
1696 else
1698 if ((proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START) ||
1699 (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START_AT_CONSTRUCTION))
1701 async_init_call_get_name_owner (task);
1703 else
1705 async_init_call_start_service_by_name (task);
1710 static gboolean
1711 async_initable_init_second_finish (GAsyncInitable *initable,
1712 GAsyncResult *res,
1713 GError **error)
1715 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1716 GTask *task = G_TASK (res);
1717 GVariant *result;
1718 gboolean ret;
1720 ret = !g_task_had_error (task);
1722 result = g_task_propagate_pointer (task, error);
1723 if (result != NULL)
1725 process_get_all_reply (proxy, result);
1726 g_variant_unref (result);
1729 proxy->priv->initialized = TRUE;
1730 return ret;
1733 /* ---------------------------------------------------------------------------------------------------- */
1735 static void
1736 async_initable_init_first (GAsyncInitable *initable)
1738 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1740 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES))
1742 /* subscribe to PropertiesChanged() */
1743 proxy->priv->properties_changed_subscription_id =
1744 g_dbus_connection_signal_subscribe (proxy->priv->connection,
1745 proxy->priv->name,
1746 "org.freedesktop.DBus.Properties",
1747 "PropertiesChanged",
1748 proxy->priv->object_path,
1749 proxy->priv->interface_name,
1750 G_DBUS_SIGNAL_FLAGS_NONE,
1751 on_properties_changed,
1752 signal_subscription_ref (proxy->priv->signal_subscription_data),
1753 (GDestroyNotify) signal_subscription_unref);
1756 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS))
1758 /* subscribe to all signals for the object */
1759 proxy->priv->signals_subscription_id =
1760 g_dbus_connection_signal_subscribe (proxy->priv->connection,
1761 proxy->priv->name,
1762 proxy->priv->interface_name,
1763 NULL, /* member */
1764 proxy->priv->object_path,
1765 NULL, /* arg0 */
1766 G_DBUS_SIGNAL_FLAGS_NONE,
1767 on_signal_received,
1768 signal_subscription_ref (proxy->priv->signal_subscription_data),
1769 (GDestroyNotify) signal_subscription_unref);
1772 if (proxy->priv->name != NULL && !g_dbus_is_unique_name (proxy->priv->name))
1774 proxy->priv->name_owner_changed_subscription_id =
1775 g_dbus_connection_signal_subscribe (proxy->priv->connection,
1776 "org.freedesktop.DBus", /* name */
1777 "org.freedesktop.DBus", /* interface */
1778 "NameOwnerChanged", /* signal name */
1779 "/org/freedesktop/DBus", /* path */
1780 proxy->priv->name, /* arg0 */
1781 G_DBUS_SIGNAL_FLAGS_NONE,
1782 on_name_owner_changed,
1783 signal_subscription_ref (proxy->priv->signal_subscription_data),
1784 (GDestroyNotify) signal_subscription_unref);
1788 /* ---------------------------------------------------------------------------------------------------- */
1790 /* initialization is split into two parts - the first is the
1791 * non-blocking part that requires the callers GMainContext - the
1792 * second is a blocking part async part that doesn't require the
1793 * callers GMainContext.. we do this split so the code can be reused
1794 * in the GInitable implementation below.
1796 * Note that obtaining a GDBusConnection is not shared between the two
1797 * paths.
1800 static void
1801 init_second_async_cb (GObject *source_object,
1802 GAsyncResult *res,
1803 gpointer user_data)
1805 GTask *task = user_data;
1806 GError *error = NULL;
1808 if (async_initable_init_second_finish (G_ASYNC_INITABLE (source_object), res, &error))
1809 g_task_return_boolean (task, TRUE);
1810 else
1811 g_task_return_error (task, error);
1812 g_object_unref (task);
1815 static void
1816 get_connection_cb (GObject *source_object,
1817 GAsyncResult *res,
1818 gpointer user_data)
1820 GTask *task = user_data;
1821 GDBusProxy *proxy = g_task_get_source_object (task);
1822 GError *error;
1824 error = NULL;
1825 proxy->priv->connection = g_bus_get_finish (res, &error);
1826 if (proxy->priv->connection == NULL)
1828 g_task_return_error (task, error);
1829 g_object_unref (task);
1831 else
1833 async_initable_init_first (G_ASYNC_INITABLE (proxy));
1834 async_initable_init_second_async (G_ASYNC_INITABLE (proxy),
1835 g_task_get_priority (task),
1836 g_task_get_cancellable (task),
1837 init_second_async_cb,
1838 task);
1842 static void
1843 async_initable_init_async (GAsyncInitable *initable,
1844 gint io_priority,
1845 GCancellable *cancellable,
1846 GAsyncReadyCallback callback,
1847 gpointer user_data)
1849 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1850 GTask *task;
1852 task = g_task_new (proxy, cancellable, callback, user_data);
1853 g_task_set_source_tag (task, async_initable_init_async);
1854 g_task_set_priority (task, io_priority);
1856 if (proxy->priv->bus_type != G_BUS_TYPE_NONE)
1858 g_assert (proxy->priv->connection == NULL);
1860 g_bus_get (proxy->priv->bus_type,
1861 cancellable,
1862 get_connection_cb,
1863 task);
1865 else
1867 async_initable_init_first (initable);
1868 async_initable_init_second_async (initable, io_priority, cancellable,
1869 init_second_async_cb, task);
1873 static gboolean
1874 async_initable_init_finish (GAsyncInitable *initable,
1875 GAsyncResult *res,
1876 GError **error)
1878 return g_task_propagate_boolean (G_TASK (res), error);
1881 static void
1882 async_initable_iface_init (GAsyncInitableIface *async_initable_iface)
1884 async_initable_iface->init_async = async_initable_init_async;
1885 async_initable_iface->init_finish = async_initable_init_finish;
1888 /* ---------------------------------------------------------------------------------------------------- */
1890 typedef struct
1892 GMainContext *context;
1893 GMainLoop *loop;
1894 GAsyncResult *res;
1895 } InitableAsyncInitableData;
1897 static void
1898 async_initable_init_async_cb (GObject *source_object,
1899 GAsyncResult *res,
1900 gpointer user_data)
1902 InitableAsyncInitableData *data = user_data;
1903 data->res = g_object_ref (res);
1904 g_main_loop_quit (data->loop);
1907 /* Simply reuse the GAsyncInitable implementation but run the first
1908 * part (that is non-blocking and requires the callers GMainContext)
1909 * with the callers GMainContext.. and the second with a private
1910 * GMainContext (bug 621310 is slightly related).
1912 * Note that obtaining a GDBusConnection is not shared between the two
1913 * paths.
1915 static gboolean
1916 initable_init (GInitable *initable,
1917 GCancellable *cancellable,
1918 GError **error)
1920 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1921 InitableAsyncInitableData *data;
1922 gboolean ret;
1924 ret = FALSE;
1926 if (proxy->priv->bus_type != G_BUS_TYPE_NONE)
1928 g_assert (proxy->priv->connection == NULL);
1929 proxy->priv->connection = g_bus_get_sync (proxy->priv->bus_type,
1930 cancellable,
1931 error);
1932 if (proxy->priv->connection == NULL)
1933 goto out;
1936 async_initable_init_first (G_ASYNC_INITABLE (initable));
1938 data = g_new0 (InitableAsyncInitableData, 1);
1939 data->context = g_main_context_new ();
1940 data->loop = g_main_loop_new (data->context, FALSE);
1942 g_main_context_push_thread_default (data->context);
1944 async_initable_init_second_async (G_ASYNC_INITABLE (initable),
1945 G_PRIORITY_DEFAULT,
1946 cancellable,
1947 async_initable_init_async_cb,
1948 data);
1950 g_main_loop_run (data->loop);
1952 ret = async_initable_init_second_finish (G_ASYNC_INITABLE (initable),
1953 data->res,
1954 error);
1956 g_main_context_pop_thread_default (data->context);
1958 g_main_context_unref (data->context);
1959 g_main_loop_unref (data->loop);
1960 g_object_unref (data->res);
1961 g_free (data);
1963 out:
1965 return ret;
1968 static void
1969 initable_iface_init (GInitableIface *initable_iface)
1971 initable_iface->init = initable_init;
1974 /* ---------------------------------------------------------------------------------------------------- */
1977 * g_dbus_proxy_new:
1978 * @connection: A #GDBusConnection.
1979 * @flags: Flags used when constructing the proxy.
1980 * @info: (nullable): A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
1981 * @name: (nullable): A bus name (well-known or unique) or %NULL if @connection is not a message bus connection.
1982 * @object_path: An object path.
1983 * @interface_name: A D-Bus interface name.
1984 * @cancellable: (nullable): A #GCancellable or %NULL.
1985 * @callback: Callback function to invoke when the proxy is ready.
1986 * @user_data: User data to pass to @callback.
1988 * Creates a proxy for accessing @interface_name on the remote object
1989 * at @object_path owned by @name at @connection and asynchronously
1990 * loads D-Bus properties unless the
1991 * %G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES flag is used. Connect to
1992 * the #GDBusProxy::g-properties-changed signal to get notified about
1993 * property changes.
1995 * If the %G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS flag is not set, also sets up
1996 * match rules for signals. Connect to the #GDBusProxy::g-signal signal
1997 * to handle signals from the remote object.
1999 * If @name is a well-known name and the
2000 * %G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START and %G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START_AT_CONSTRUCTION
2001 * flags aren't set and no name owner currently exists, the message bus
2002 * will be requested to launch a name owner for the name.
2004 * This is a failable asynchronous constructor - when the proxy is
2005 * ready, @callback will be invoked and you can use
2006 * g_dbus_proxy_new_finish() to get the result.
2008 * See g_dbus_proxy_new_sync() and for a synchronous version of this constructor.
2010 * #GDBusProxy is used in this [example][gdbus-wellknown-proxy].
2012 * Since: 2.26
2014 void
2015 g_dbus_proxy_new (GDBusConnection *connection,
2016 GDBusProxyFlags flags,
2017 GDBusInterfaceInfo *info,
2018 const gchar *name,
2019 const gchar *object_path,
2020 const gchar *interface_name,
2021 GCancellable *cancellable,
2022 GAsyncReadyCallback callback,
2023 gpointer user_data)
2025 _g_dbus_initialize ();
2027 g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
2028 g_return_if_fail ((name == NULL && g_dbus_connection_get_unique_name (connection) == NULL) || g_dbus_is_name (name));
2029 g_return_if_fail (g_variant_is_object_path (object_path));
2030 g_return_if_fail (g_dbus_is_interface_name (interface_name));
2032 g_async_initable_new_async (G_TYPE_DBUS_PROXY,
2033 G_PRIORITY_DEFAULT,
2034 cancellable,
2035 callback,
2036 user_data,
2037 "g-flags", flags,
2038 "g-interface-info", info,
2039 "g-name", name,
2040 "g-connection", connection,
2041 "g-object-path", object_path,
2042 "g-interface-name", interface_name,
2043 NULL);
2047 * g_dbus_proxy_new_finish:
2048 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback function passed to g_dbus_proxy_new().
2049 * @error: Return location for error or %NULL.
2051 * Finishes creating a #GDBusProxy.
2053 * Returns: (transfer full): A #GDBusProxy or %NULL if @error is set.
2054 * Free with g_object_unref().
2056 * Since: 2.26
2058 GDBusProxy *
2059 g_dbus_proxy_new_finish (GAsyncResult *res,
2060 GError **error)
2062 GObject *object;
2063 GObject *source_object;
2065 source_object = g_async_result_get_source_object (res);
2066 g_assert (source_object != NULL);
2068 object = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object),
2069 res,
2070 error);
2071 g_object_unref (source_object);
2073 if (object != NULL)
2074 return G_DBUS_PROXY (object);
2075 else
2076 return NULL;
2080 * g_dbus_proxy_new_sync:
2081 * @connection: A #GDBusConnection.
2082 * @flags: Flags used when constructing the proxy.
2083 * @info: (nullable): A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
2084 * @name: (nullable): A bus name (well-known or unique) or %NULL if @connection is not a message bus connection.
2085 * @object_path: An object path.
2086 * @interface_name: A D-Bus interface name.
2087 * @cancellable: (nullable): A #GCancellable or %NULL.
2088 * @error: (nullable): Return location for error or %NULL.
2090 * Creates a proxy for accessing @interface_name on the remote object
2091 * at @object_path owned by @name at @connection and synchronously
2092 * loads D-Bus properties unless the
2093 * %G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES flag is used.
2095 * If the %G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS flag is not set, also sets up
2096 * match rules for signals. Connect to the #GDBusProxy::g-signal signal
2097 * to handle signals from the remote object.
2099 * If @name is a well-known name and the
2100 * %G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START and %G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START_AT_CONSTRUCTION
2101 * flags aren't set and no name owner currently exists, the message bus
2102 * will be requested to launch a name owner for the name.
2104 * This is a synchronous failable constructor. See g_dbus_proxy_new()
2105 * and g_dbus_proxy_new_finish() for the asynchronous version.
2107 * #GDBusProxy is used in this [example][gdbus-wellknown-proxy].
2109 * Returns: (transfer full): A #GDBusProxy or %NULL if error is set.
2110 * Free with g_object_unref().
2112 * Since: 2.26
2114 GDBusProxy *
2115 g_dbus_proxy_new_sync (GDBusConnection *connection,
2116 GDBusProxyFlags flags,
2117 GDBusInterfaceInfo *info,
2118 const gchar *name,
2119 const gchar *object_path,
2120 const gchar *interface_name,
2121 GCancellable *cancellable,
2122 GError **error)
2124 GInitable *initable;
2126 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
2127 g_return_val_if_fail ((name == NULL && g_dbus_connection_get_unique_name (connection) == NULL) ||
2128 g_dbus_is_name (name), NULL);
2129 g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
2130 g_return_val_if_fail (g_dbus_is_interface_name (interface_name), NULL);
2132 initable = g_initable_new (G_TYPE_DBUS_PROXY,
2133 cancellable,
2134 error,
2135 "g-flags", flags,
2136 "g-interface-info", info,
2137 "g-name", name,
2138 "g-connection", connection,
2139 "g-object-path", object_path,
2140 "g-interface-name", interface_name,
2141 NULL);
2142 if (initable != NULL)
2143 return G_DBUS_PROXY (initable);
2144 else
2145 return NULL;
2148 /* ---------------------------------------------------------------------------------------------------- */
2151 * g_dbus_proxy_new_for_bus:
2152 * @bus_type: A #GBusType.
2153 * @flags: Flags used when constructing the proxy.
2154 * @info: (nullable): A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
2155 * @name: A bus name (well-known or unique).
2156 * @object_path: An object path.
2157 * @interface_name: A D-Bus interface name.
2158 * @cancellable: (nullable): A #GCancellable or %NULL.
2159 * @callback: Callback function to invoke when the proxy is ready.
2160 * @user_data: User data to pass to @callback.
2162 * Like g_dbus_proxy_new() but takes a #GBusType instead of a #GDBusConnection.
2164 * #GDBusProxy is used in this [example][gdbus-wellknown-proxy].
2166 * Since: 2.26
2168 void
2169 g_dbus_proxy_new_for_bus (GBusType bus_type,
2170 GDBusProxyFlags flags,
2171 GDBusInterfaceInfo *info,
2172 const gchar *name,
2173 const gchar *object_path,
2174 const gchar *interface_name,
2175 GCancellable *cancellable,
2176 GAsyncReadyCallback callback,
2177 gpointer user_data)
2179 _g_dbus_initialize ();
2181 g_return_if_fail (g_dbus_is_name (name));
2182 g_return_if_fail (g_variant_is_object_path (object_path));
2183 g_return_if_fail (g_dbus_is_interface_name (interface_name));
2185 g_async_initable_new_async (G_TYPE_DBUS_PROXY,
2186 G_PRIORITY_DEFAULT,
2187 cancellable,
2188 callback,
2189 user_data,
2190 "g-flags", flags,
2191 "g-interface-info", info,
2192 "g-name", name,
2193 "g-bus-type", bus_type,
2194 "g-object-path", object_path,
2195 "g-interface-name", interface_name,
2196 NULL);
2200 * g_dbus_proxy_new_for_bus_finish:
2201 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback function passed to g_dbus_proxy_new_for_bus().
2202 * @error: Return location for error or %NULL.
2204 * Finishes creating a #GDBusProxy.
2206 * Returns: (transfer full): A #GDBusProxy or %NULL if @error is set.
2207 * Free with g_object_unref().
2209 * Since: 2.26
2211 GDBusProxy *
2212 g_dbus_proxy_new_for_bus_finish (GAsyncResult *res,
2213 GError **error)
2215 return g_dbus_proxy_new_finish (res, error);
2219 * g_dbus_proxy_new_for_bus_sync:
2220 * @bus_type: A #GBusType.
2221 * @flags: Flags used when constructing the proxy.
2222 * @info: (nullable): A #GDBusInterfaceInfo specifying the minimal interface
2223 * that @proxy conforms to or %NULL.
2224 * @name: A bus name (well-known or unique).
2225 * @object_path: An object path.
2226 * @interface_name: A D-Bus interface name.
2227 * @cancellable: (nullable): A #GCancellable or %NULL.
2228 * @error: Return location for error or %NULL.
2230 * Like g_dbus_proxy_new_sync() but takes a #GBusType instead of a #GDBusConnection.
2232 * #GDBusProxy is used in this [example][gdbus-wellknown-proxy].
2234 * Returns: (transfer full): A #GDBusProxy or %NULL if error is set.
2235 * Free with g_object_unref().
2237 * Since: 2.26
2239 GDBusProxy *
2240 g_dbus_proxy_new_for_bus_sync (GBusType bus_type,
2241 GDBusProxyFlags flags,
2242 GDBusInterfaceInfo *info,
2243 const gchar *name,
2244 const gchar *object_path,
2245 const gchar *interface_name,
2246 GCancellable *cancellable,
2247 GError **error)
2249 GInitable *initable;
2251 _g_dbus_initialize ();
2253 g_return_val_if_fail (g_dbus_is_name (name), NULL);
2254 g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
2255 g_return_val_if_fail (g_dbus_is_interface_name (interface_name), NULL);
2257 initable = g_initable_new (G_TYPE_DBUS_PROXY,
2258 cancellable,
2259 error,
2260 "g-flags", flags,
2261 "g-interface-info", info,
2262 "g-name", name,
2263 "g-bus-type", bus_type,
2264 "g-object-path", object_path,
2265 "g-interface-name", interface_name,
2266 NULL);
2267 if (initable != NULL)
2268 return G_DBUS_PROXY (initable);
2269 else
2270 return NULL;
2273 /* ---------------------------------------------------------------------------------------------------- */
2276 * g_dbus_proxy_get_connection:
2277 * @proxy: A #GDBusProxy.
2279 * Gets the connection @proxy is for.
2281 * Returns: (transfer none): A #GDBusConnection owned by @proxy. Do not free.
2283 * Since: 2.26
2285 GDBusConnection *
2286 g_dbus_proxy_get_connection (GDBusProxy *proxy)
2288 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2289 return proxy->priv->connection;
2293 * g_dbus_proxy_get_flags:
2294 * @proxy: A #GDBusProxy.
2296 * Gets the flags that @proxy was constructed with.
2298 * Returns: Flags from the #GDBusProxyFlags enumeration.
2300 * Since: 2.26
2302 GDBusProxyFlags
2303 g_dbus_proxy_get_flags (GDBusProxy *proxy)
2305 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), 0);
2306 return proxy->priv->flags;
2310 * g_dbus_proxy_get_name:
2311 * @proxy: A #GDBusProxy.
2313 * Gets the name that @proxy was constructed for.
2315 * Returns: A string owned by @proxy. Do not free.
2317 * Since: 2.26
2319 const gchar *
2320 g_dbus_proxy_get_name (GDBusProxy *proxy)
2322 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2323 return proxy->priv->name;
2327 * g_dbus_proxy_get_name_owner:
2328 * @proxy: A #GDBusProxy.
2330 * The unique name that owns the name that @proxy is for or %NULL if
2331 * no-one currently owns that name. You may connect to the
2332 * #GObject::notify signal to track changes to the
2333 * #GDBusProxy:g-name-owner property.
2335 * Returns: (transfer full) (nullable): The name owner or %NULL if no name
2336 * owner exists. Free with g_free().
2338 * Since: 2.26
2340 gchar *
2341 g_dbus_proxy_get_name_owner (GDBusProxy *proxy)
2343 gchar *ret;
2345 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2347 G_LOCK (properties_lock);
2348 ret = g_strdup (proxy->priv->name_owner);
2349 G_UNLOCK (properties_lock);
2350 return ret;
2354 * g_dbus_proxy_get_object_path:
2355 * @proxy: A #GDBusProxy.
2357 * Gets the object path @proxy is for.
2359 * Returns: A string owned by @proxy. Do not free.
2361 * Since: 2.26
2363 const gchar *
2364 g_dbus_proxy_get_object_path (GDBusProxy *proxy)
2366 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2367 return proxy->priv->object_path;
2371 * g_dbus_proxy_get_interface_name:
2372 * @proxy: A #GDBusProxy.
2374 * Gets the D-Bus interface name @proxy is for.
2376 * Returns: A string owned by @proxy. Do not free.
2378 * Since: 2.26
2380 const gchar *
2381 g_dbus_proxy_get_interface_name (GDBusProxy *proxy)
2383 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2384 return proxy->priv->interface_name;
2388 * g_dbus_proxy_get_default_timeout:
2389 * @proxy: A #GDBusProxy.
2391 * Gets the timeout to use if -1 (specifying default timeout) is
2392 * passed as @timeout_msec in the g_dbus_proxy_call() and
2393 * g_dbus_proxy_call_sync() functions.
2395 * See the #GDBusProxy:g-default-timeout property for more details.
2397 * Returns: Timeout to use for @proxy.
2399 * Since: 2.26
2401 gint
2402 g_dbus_proxy_get_default_timeout (GDBusProxy *proxy)
2404 gint ret;
2406 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), -1);
2408 G_LOCK (properties_lock);
2409 ret = proxy->priv->timeout_msec;
2410 G_UNLOCK (properties_lock);
2411 return ret;
2415 * g_dbus_proxy_set_default_timeout:
2416 * @proxy: A #GDBusProxy.
2417 * @timeout_msec: Timeout in milliseconds.
2419 * Sets the timeout to use if -1 (specifying default timeout) is
2420 * passed as @timeout_msec in the g_dbus_proxy_call() and
2421 * g_dbus_proxy_call_sync() functions.
2423 * See the #GDBusProxy:g-default-timeout property for more details.
2425 * Since: 2.26
2427 void
2428 g_dbus_proxy_set_default_timeout (GDBusProxy *proxy,
2429 gint timeout_msec)
2431 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
2432 g_return_if_fail (timeout_msec == -1 || timeout_msec >= 0);
2434 G_LOCK (properties_lock);
2436 if (proxy->priv->timeout_msec != timeout_msec)
2438 proxy->priv->timeout_msec = timeout_msec;
2439 G_UNLOCK (properties_lock);
2441 g_object_notify (G_OBJECT (proxy), "g-default-timeout");
2443 else
2445 G_UNLOCK (properties_lock);
2450 * g_dbus_proxy_get_interface_info:
2451 * @proxy: A #GDBusProxy
2453 * Returns the #GDBusInterfaceInfo, if any, specifying the interface
2454 * that @proxy conforms to. See the #GDBusProxy:g-interface-info
2455 * property for more details.
2457 * Returns: (transfer none) (nullable): A #GDBusInterfaceInfo or %NULL.
2458 * Do not unref the returned object, it is owned by @proxy.
2460 * Since: 2.26
2462 GDBusInterfaceInfo *
2463 g_dbus_proxy_get_interface_info (GDBusProxy *proxy)
2465 GDBusInterfaceInfo *ret;
2467 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2469 G_LOCK (properties_lock);
2470 ret = proxy->priv->expected_interface;
2471 G_UNLOCK (properties_lock);
2472 /* FIXME: returning a borrowed ref with no guarantee that nobody will
2473 * call g_dbus_proxy_set_interface_info() and make it invalid...
2475 return ret;
2479 * g_dbus_proxy_set_interface_info:
2480 * @proxy: A #GDBusProxy
2481 * @info: (transfer none) (nullable): Minimum interface this proxy conforms to
2482 * or %NULL to unset.
2484 * Ensure that interactions with @proxy conform to the given
2485 * interface. See the #GDBusProxy:g-interface-info property for more
2486 * details.
2488 * Since: 2.26
2490 void
2491 g_dbus_proxy_set_interface_info (GDBusProxy *proxy,
2492 GDBusInterfaceInfo *info)
2494 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
2495 G_LOCK (properties_lock);
2497 if (proxy->priv->expected_interface != NULL)
2499 g_dbus_interface_info_cache_release (proxy->priv->expected_interface);
2500 g_dbus_interface_info_unref (proxy->priv->expected_interface);
2502 proxy->priv->expected_interface = info != NULL ? g_dbus_interface_info_ref (info) : NULL;
2503 if (proxy->priv->expected_interface != NULL)
2504 g_dbus_interface_info_cache_build (proxy->priv->expected_interface);
2506 G_UNLOCK (properties_lock);
2509 /* ---------------------------------------------------------------------------------------------------- */
2511 static gboolean
2512 maybe_split_method_name (const gchar *method_name,
2513 gchar **out_interface_name,
2514 const gchar **out_method_name)
2516 gboolean was_split;
2518 was_split = FALSE;
2519 g_assert (out_interface_name != NULL);
2520 g_assert (out_method_name != NULL);
2521 *out_interface_name = NULL;
2522 *out_method_name = NULL;
2524 if (strchr (method_name, '.') != NULL)
2526 gchar *p;
2527 gchar *last_dot;
2529 p = g_strdup (method_name);
2530 last_dot = strrchr (p, '.');
2531 *last_dot = '\0';
2533 *out_interface_name = p;
2534 *out_method_name = last_dot + 1;
2536 was_split = TRUE;
2539 return was_split;
2542 typedef struct
2544 GVariant *value;
2545 #ifdef G_OS_UNIX
2546 GUnixFDList *fd_list;
2547 #endif
2548 } ReplyData;
2550 static void
2551 reply_data_free (ReplyData *data)
2553 g_variant_unref (data->value);
2554 #ifdef G_OS_UNIX
2555 if (data->fd_list != NULL)
2556 g_object_unref (data->fd_list);
2557 #endif
2558 g_slice_free (ReplyData, data);
2561 static void
2562 reply_cb (GDBusConnection *connection,
2563 GAsyncResult *res,
2564 gpointer user_data)
2566 GTask *task = user_data;
2567 GVariant *value;
2568 GError *error;
2569 #ifdef G_OS_UNIX
2570 GUnixFDList *fd_list;
2571 #endif
2573 error = NULL;
2574 #ifdef G_OS_UNIX
2575 value = g_dbus_connection_call_with_unix_fd_list_finish (connection,
2576 &fd_list,
2577 res,
2578 &error);
2579 #else
2580 value = g_dbus_connection_call_finish (connection,
2581 res,
2582 &error);
2583 #endif
2584 if (error != NULL)
2586 g_task_return_error (task, error);
2588 else
2590 ReplyData *data;
2591 data = g_slice_new0 (ReplyData);
2592 data->value = value;
2593 #ifdef G_OS_UNIX
2594 data->fd_list = fd_list;
2595 #endif
2596 g_task_return_pointer (task, data, (GDestroyNotify) reply_data_free);
2599 g_object_unref (task);
2602 /* properties_lock must be held for as long as you will keep the
2603 * returned value
2605 static const GDBusMethodInfo *
2606 lookup_method_info (GDBusProxy *proxy,
2607 const gchar *method_name)
2609 const GDBusMethodInfo *info = NULL;
2611 if (proxy->priv->expected_interface == NULL)
2612 goto out;
2614 info = g_dbus_interface_info_lookup_method (proxy->priv->expected_interface, method_name);
2616 out:
2617 return info;
2620 /* properties_lock must be held for as long as you will keep the
2621 * returned value
2623 static const gchar *
2624 get_destination_for_call (GDBusProxy *proxy)
2626 const gchar *ret;
2628 ret = NULL;
2630 /* If proxy->priv->name is a unique name, then proxy->priv->name_owner
2631 * is never NULL and always the same as proxy->priv->name. We use this
2632 * knowledge to avoid checking if proxy->priv->name is a unique or
2633 * well-known name.
2635 ret = proxy->priv->name_owner;
2636 if (ret != NULL)
2637 goto out;
2639 if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START)
2640 goto out;
2642 ret = proxy->priv->name;
2644 out:
2645 return ret;
2648 /* ---------------------------------------------------------------------------------------------------- */
2650 static void
2651 g_dbus_proxy_call_internal (GDBusProxy *proxy,
2652 const gchar *method_name,
2653 GVariant *parameters,
2654 GDBusCallFlags flags,
2655 gint timeout_msec,
2656 GUnixFDList *fd_list,
2657 GCancellable *cancellable,
2658 GAsyncReadyCallback callback,
2659 gpointer user_data)
2661 GTask *task;
2662 gboolean was_split;
2663 gchar *split_interface_name;
2664 const gchar *split_method_name;
2665 const gchar *target_method_name;
2666 const gchar *target_interface_name;
2667 gchar *destination;
2668 GVariantType *reply_type;
2669 GAsyncReadyCallback my_callback;
2671 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
2672 g_return_if_fail (g_dbus_is_member_name (method_name) || g_dbus_is_interface_name (method_name));
2673 g_return_if_fail (parameters == NULL || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE));
2674 g_return_if_fail (timeout_msec == -1 || timeout_msec >= 0);
2675 #ifdef G_OS_UNIX
2676 g_return_if_fail (fd_list == NULL || G_IS_UNIX_FD_LIST (fd_list));
2677 #else
2678 g_return_if_fail (fd_list == NULL);
2679 #endif
2681 reply_type = NULL;
2682 split_interface_name = NULL;
2684 /* g_dbus_connection_call() is optimised for the case of a NULL
2685 * callback. If we get a NULL callback from our user then make sure
2686 * we pass along a NULL callback for ourselves as well.
2688 if (callback != NULL)
2690 my_callback = (GAsyncReadyCallback) reply_cb;
2691 task = g_task_new (proxy, cancellable, callback, user_data);
2692 g_task_set_source_tag (task, g_dbus_proxy_call_internal);
2694 else
2696 my_callback = NULL;
2697 task = NULL;
2700 G_LOCK (properties_lock);
2702 was_split = maybe_split_method_name (method_name, &split_interface_name, &split_method_name);
2703 target_method_name = was_split ? split_method_name : method_name;
2704 target_interface_name = was_split ? split_interface_name : proxy->priv->interface_name;
2706 /* Warn if method is unexpected (cf. :g-interface-info) */
2707 if (!was_split)
2709 const GDBusMethodInfo *expected_method_info;
2710 expected_method_info = lookup_method_info (proxy, target_method_name);
2711 if (expected_method_info != NULL)
2712 reply_type = _g_dbus_compute_complete_signature (expected_method_info->out_args);
2715 destination = NULL;
2716 if (proxy->priv->name != NULL)
2718 destination = g_strdup (get_destination_for_call (proxy));
2719 if (destination == NULL)
2721 if (task != NULL)
2723 g_task_return_new_error (task,
2724 G_IO_ERROR,
2725 G_IO_ERROR_FAILED,
2726 _("Cannot invoke method; proxy is for a well-known name without an owner and proxy was constructed with the G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START flag"));
2727 g_object_unref (task);
2729 G_UNLOCK (properties_lock);
2730 goto out;
2734 G_UNLOCK (properties_lock);
2736 #ifdef G_OS_UNIX
2737 g_dbus_connection_call_with_unix_fd_list (proxy->priv->connection,
2738 destination,
2739 proxy->priv->object_path,
2740 target_interface_name,
2741 target_method_name,
2742 parameters,
2743 reply_type,
2744 flags,
2745 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2746 fd_list,
2747 cancellable,
2748 my_callback,
2749 task);
2750 #else
2751 g_dbus_connection_call (proxy->priv->connection,
2752 destination,
2753 proxy->priv->object_path,
2754 target_interface_name,
2755 target_method_name,
2756 parameters,
2757 reply_type,
2758 flags,
2759 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2760 cancellable,
2761 my_callback,
2762 task);
2763 #endif
2765 out:
2766 if (reply_type != NULL)
2767 g_variant_type_free (reply_type);
2769 g_free (destination);
2770 g_free (split_interface_name);
2773 static GVariant *
2774 g_dbus_proxy_call_finish_internal (GDBusProxy *proxy,
2775 GUnixFDList **out_fd_list,
2776 GAsyncResult *res,
2777 GError **error)
2779 GVariant *value;
2780 ReplyData *data;
2782 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2783 g_return_val_if_fail (g_task_is_valid (res, proxy), NULL);
2784 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2786 value = NULL;
2788 data = g_task_propagate_pointer (G_TASK (res), error);
2789 if (!data)
2790 goto out;
2792 value = g_variant_ref (data->value);
2793 #ifdef G_OS_UNIX
2794 if (out_fd_list != NULL)
2795 *out_fd_list = data->fd_list != NULL ? g_object_ref (data->fd_list) : NULL;
2796 #endif
2797 reply_data_free (data);
2799 out:
2800 return value;
2803 static GVariant *
2804 g_dbus_proxy_call_sync_internal (GDBusProxy *proxy,
2805 const gchar *method_name,
2806 GVariant *parameters,
2807 GDBusCallFlags flags,
2808 gint timeout_msec,
2809 GUnixFDList *fd_list,
2810 GUnixFDList **out_fd_list,
2811 GCancellable *cancellable,
2812 GError **error)
2814 GVariant *ret;
2815 gboolean was_split;
2816 gchar *split_interface_name;
2817 const gchar *split_method_name;
2818 const gchar *target_method_name;
2819 const gchar *target_interface_name;
2820 gchar *destination;
2821 GVariantType *reply_type;
2823 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2824 g_return_val_if_fail (g_dbus_is_member_name (method_name) || g_dbus_is_interface_name (method_name), NULL);
2825 g_return_val_if_fail (parameters == NULL || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE), NULL);
2826 g_return_val_if_fail (timeout_msec == -1 || timeout_msec >= 0, NULL);
2827 #ifdef G_OS_UNIX
2828 g_return_val_if_fail (fd_list == NULL || G_IS_UNIX_FD_LIST (fd_list), NULL);
2829 #else
2830 g_return_val_if_fail (fd_list == NULL, NULL);
2831 #endif
2832 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2834 reply_type = NULL;
2836 G_LOCK (properties_lock);
2838 was_split = maybe_split_method_name (method_name, &split_interface_name, &split_method_name);
2839 target_method_name = was_split ? split_method_name : method_name;
2840 target_interface_name = was_split ? split_interface_name : proxy->priv->interface_name;
2842 /* Warn if method is unexpected (cf. :g-interface-info) */
2843 if (!was_split)
2845 const GDBusMethodInfo *expected_method_info;
2846 expected_method_info = lookup_method_info (proxy, target_method_name);
2847 if (expected_method_info != NULL)
2848 reply_type = _g_dbus_compute_complete_signature (expected_method_info->out_args);
2851 destination = NULL;
2852 if (proxy->priv->name != NULL)
2854 destination = g_strdup (get_destination_for_call (proxy));
2855 if (destination == NULL)
2857 g_set_error_literal (error,
2858 G_IO_ERROR,
2859 G_IO_ERROR_FAILED,
2860 _("Cannot invoke method; proxy is for a well-known name without an owner and proxy was constructed with the G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START flag"));
2861 ret = NULL;
2862 G_UNLOCK (properties_lock);
2863 goto out;
2867 G_UNLOCK (properties_lock);
2869 #ifdef G_OS_UNIX
2870 ret = g_dbus_connection_call_with_unix_fd_list_sync (proxy->priv->connection,
2871 destination,
2872 proxy->priv->object_path,
2873 target_interface_name,
2874 target_method_name,
2875 parameters,
2876 reply_type,
2877 flags,
2878 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2879 fd_list,
2880 out_fd_list,
2881 cancellable,
2882 error);
2883 #else
2884 ret = g_dbus_connection_call_sync (proxy->priv->connection,
2885 destination,
2886 proxy->priv->object_path,
2887 target_interface_name,
2888 target_method_name,
2889 parameters,
2890 reply_type,
2891 flags,
2892 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2893 cancellable,
2894 error);
2895 #endif
2897 out:
2898 if (reply_type != NULL)
2899 g_variant_type_free (reply_type);
2901 g_free (destination);
2902 g_free (split_interface_name);
2904 return ret;
2907 /* ---------------------------------------------------------------------------------------------------- */
2910 * g_dbus_proxy_call:
2911 * @proxy: A #GDBusProxy.
2912 * @method_name: Name of method to invoke.
2913 * @parameters: (nullable): A #GVariant tuple with parameters for the signal or %NULL if not passing parameters.
2914 * @flags: Flags from the #GDBusCallFlags enumeration.
2915 * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
2916 * "infinite") or -1 to use the proxy default timeout.
2917 * @cancellable: (nullable): A #GCancellable or %NULL.
2918 * @callback: (nullable): A #GAsyncReadyCallback to call when the request is satisfied or %NULL if you don't
2919 * care about the result of the method invocation.
2920 * @user_data: The data to pass to @callback.
2922 * Asynchronously invokes the @method_name method on @proxy.
2924 * If @method_name contains any dots, then @name is split into interface and
2925 * method name parts. This allows using @proxy for invoking methods on
2926 * other interfaces.
2928 * If the #GDBusConnection associated with @proxy is closed then
2929 * the operation will fail with %G_IO_ERROR_CLOSED. If
2930 * @cancellable is canceled, the operation will fail with
2931 * %G_IO_ERROR_CANCELLED. If @parameters contains a value not
2932 * compatible with the D-Bus protocol, the operation fails with
2933 * %G_IO_ERROR_INVALID_ARGUMENT.
2935 * If the @parameters #GVariant is floating, it is consumed. This allows
2936 * convenient 'inline' use of g_variant_new(), e.g.:
2937 * |[<!-- language="C" -->
2938 * g_dbus_proxy_call (proxy,
2939 * "TwoStrings",
2940 * g_variant_new ("(ss)",
2941 * "Thing One",
2942 * "Thing Two"),
2943 * G_DBUS_CALL_FLAGS_NONE,
2944 * -1,
2945 * NULL,
2946 * (GAsyncReadyCallback) two_strings_done,
2947 * &data);
2948 * ]|
2950 * If @proxy has an expected interface (see
2951 * #GDBusProxy:g-interface-info) and @method_name is referenced by it,
2952 * then the return value is checked against the return type.
2954 * This is an asynchronous method. When the operation is finished,
2955 * @callback will be invoked in the
2956 * [thread-default main context][g-main-context-push-thread-default]
2957 * of the thread you are calling this method from.
2958 * You can then call g_dbus_proxy_call_finish() to get the result of
2959 * the operation. See g_dbus_proxy_call_sync() for the synchronous
2960 * version of this method.
2962 * If @callback is %NULL then the D-Bus method call message will be sent with
2963 * the %G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED flag set.
2965 * Since: 2.26
2967 void
2968 g_dbus_proxy_call (GDBusProxy *proxy,
2969 const gchar *method_name,
2970 GVariant *parameters,
2971 GDBusCallFlags flags,
2972 gint timeout_msec,
2973 GCancellable *cancellable,
2974 GAsyncReadyCallback callback,
2975 gpointer user_data)
2977 g_dbus_proxy_call_internal (proxy, method_name, parameters, flags, timeout_msec, NULL, cancellable, callback, user_data);
2981 * g_dbus_proxy_call_finish:
2982 * @proxy: A #GDBusProxy.
2983 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_proxy_call().
2984 * @error: Return location for error or %NULL.
2986 * Finishes an operation started with g_dbus_proxy_call().
2988 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
2989 * return values. Free with g_variant_unref().
2991 * Since: 2.26
2993 GVariant *
2994 g_dbus_proxy_call_finish (GDBusProxy *proxy,
2995 GAsyncResult *res,
2996 GError **error)
2998 return g_dbus_proxy_call_finish_internal (proxy, NULL, res, error);
3002 * g_dbus_proxy_call_sync:
3003 * @proxy: A #GDBusProxy.
3004 * @method_name: Name of method to invoke.
3005 * @parameters: (nullable): A #GVariant tuple with parameters for the signal
3006 * or %NULL if not passing parameters.
3007 * @flags: Flags from the #GDBusCallFlags enumeration.
3008 * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
3009 * "infinite") or -1 to use the proxy default timeout.
3010 * @cancellable: (nullable): A #GCancellable or %NULL.
3011 * @error: Return location for error or %NULL.
3013 * Synchronously invokes the @method_name method on @proxy.
3015 * If @method_name contains any dots, then @name is split into interface and
3016 * method name parts. This allows using @proxy for invoking methods on
3017 * other interfaces.
3019 * If the #GDBusConnection associated with @proxy is disconnected then
3020 * the operation will fail with %G_IO_ERROR_CLOSED. If
3021 * @cancellable is canceled, the operation will fail with
3022 * %G_IO_ERROR_CANCELLED. If @parameters contains a value not
3023 * compatible with the D-Bus protocol, the operation fails with
3024 * %G_IO_ERROR_INVALID_ARGUMENT.
3026 * If the @parameters #GVariant is floating, it is consumed. This allows
3027 * convenient 'inline' use of g_variant_new(), e.g.:
3028 * |[<!-- language="C" -->
3029 * g_dbus_proxy_call_sync (proxy,
3030 * "TwoStrings",
3031 * g_variant_new ("(ss)",
3032 * "Thing One",
3033 * "Thing Two"),
3034 * G_DBUS_CALL_FLAGS_NONE,
3035 * -1,
3036 * NULL,
3037 * &error);
3038 * ]|
3040 * The calling thread is blocked until a reply is received. See
3041 * g_dbus_proxy_call() for the asynchronous version of this
3042 * method.
3044 * If @proxy has an expected interface (see
3045 * #GDBusProxy:g-interface-info) and @method_name is referenced by it,
3046 * then the return value is checked against the return type.
3048 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
3049 * return values. Free with g_variant_unref().
3051 * Since: 2.26
3053 GVariant *
3054 g_dbus_proxy_call_sync (GDBusProxy *proxy,
3055 const gchar *method_name,
3056 GVariant *parameters,
3057 GDBusCallFlags flags,
3058 gint timeout_msec,
3059 GCancellable *cancellable,
3060 GError **error)
3062 return g_dbus_proxy_call_sync_internal (proxy, method_name, parameters, flags, timeout_msec, NULL, NULL, cancellable, error);
3065 /* ---------------------------------------------------------------------------------------------------- */
3067 #ifdef G_OS_UNIX
3070 * g_dbus_proxy_call_with_unix_fd_list:
3071 * @proxy: A #GDBusProxy.
3072 * @method_name: Name of method to invoke.
3073 * @parameters: (nullable): A #GVariant tuple with parameters for the signal or %NULL if not passing parameters.
3074 * @flags: Flags from the #GDBusCallFlags enumeration.
3075 * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
3076 * "infinite") or -1 to use the proxy default timeout.
3077 * @fd_list: (nullable): A #GUnixFDList or %NULL.
3078 * @cancellable: (nullable): A #GCancellable or %NULL.
3079 * @callback: (nullable): A #GAsyncReadyCallback to call when the request is satisfied or %NULL if you don't
3080 * care about the result of the method invocation.
3081 * @user_data: The data to pass to @callback.
3083 * Like g_dbus_proxy_call() but also takes a #GUnixFDList object.
3085 * This method is only available on UNIX.
3087 * Since: 2.30
3089 void
3090 g_dbus_proxy_call_with_unix_fd_list (GDBusProxy *proxy,
3091 const gchar *method_name,
3092 GVariant *parameters,
3093 GDBusCallFlags flags,
3094 gint timeout_msec,
3095 GUnixFDList *fd_list,
3096 GCancellable *cancellable,
3097 GAsyncReadyCallback callback,
3098 gpointer user_data)
3100 g_dbus_proxy_call_internal (proxy, method_name, parameters, flags, timeout_msec, fd_list, cancellable, callback, user_data);
3104 * g_dbus_proxy_call_with_unix_fd_list_finish:
3105 * @proxy: A #GDBusProxy.
3106 * @out_fd_list: (out) (optional): Return location for a #GUnixFDList or %NULL.
3107 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_proxy_call_with_unix_fd_list().
3108 * @error: Return location for error or %NULL.
3110 * Finishes an operation started with g_dbus_proxy_call_with_unix_fd_list().
3112 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
3113 * return values. Free with g_variant_unref().
3115 * Since: 2.30
3117 GVariant *
3118 g_dbus_proxy_call_with_unix_fd_list_finish (GDBusProxy *proxy,
3119 GUnixFDList **out_fd_list,
3120 GAsyncResult *res,
3121 GError **error)
3123 return g_dbus_proxy_call_finish_internal (proxy, out_fd_list, res, error);
3127 * g_dbus_proxy_call_with_unix_fd_list_sync:
3128 * @proxy: A #GDBusProxy.
3129 * @method_name: Name of method to invoke.
3130 * @parameters: (nullable): A #GVariant tuple with parameters for the signal
3131 * or %NULL if not passing parameters.
3132 * @flags: Flags from the #GDBusCallFlags enumeration.
3133 * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
3134 * "infinite") or -1 to use the proxy default timeout.
3135 * @fd_list: (nullable): A #GUnixFDList or %NULL.
3136 * @out_fd_list: (out) (optional): Return location for a #GUnixFDList or %NULL.
3137 * @cancellable: (nullable): A #GCancellable or %NULL.
3138 * @error: Return location for error or %NULL.
3140 * Like g_dbus_proxy_call_sync() but also takes and returns #GUnixFDList objects.
3142 * This method is only available on UNIX.
3144 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
3145 * return values. Free with g_variant_unref().
3147 * Since: 2.30
3149 GVariant *
3150 g_dbus_proxy_call_with_unix_fd_list_sync (GDBusProxy *proxy,
3151 const gchar *method_name,
3152 GVariant *parameters,
3153 GDBusCallFlags flags,
3154 gint timeout_msec,
3155 GUnixFDList *fd_list,
3156 GUnixFDList **out_fd_list,
3157 GCancellable *cancellable,
3158 GError **error)
3160 return g_dbus_proxy_call_sync_internal (proxy, method_name, parameters, flags, timeout_msec, fd_list, out_fd_list, cancellable, error);
3163 #endif /* G_OS_UNIX */
3165 /* ---------------------------------------------------------------------------------------------------- */
3167 static GDBusInterfaceInfo *
3168 _g_dbus_proxy_get_info (GDBusInterface *interface)
3170 GDBusProxy *proxy = G_DBUS_PROXY (interface);
3171 return g_dbus_proxy_get_interface_info (proxy);
3174 static GDBusObject *
3175 _g_dbus_proxy_get_object (GDBusInterface *interface)
3177 GDBusProxy *proxy = G_DBUS_PROXY (interface);
3178 return proxy->priv->object;
3181 static GDBusObject *
3182 _g_dbus_proxy_dup_object (GDBusInterface *interface)
3184 GDBusProxy *proxy = G_DBUS_PROXY (interface);
3185 GDBusObject *ret = NULL;
3187 G_LOCK (properties_lock);
3188 if (proxy->priv->object != NULL)
3189 ret = g_object_ref (proxy->priv->object);
3190 G_UNLOCK (properties_lock);
3191 return ret;
3194 static void
3195 _g_dbus_proxy_set_object (GDBusInterface *interface,
3196 GDBusObject *object)
3198 GDBusProxy *proxy = G_DBUS_PROXY (interface);
3199 G_LOCK (properties_lock);
3200 if (proxy->priv->object != NULL)
3201 g_object_remove_weak_pointer (G_OBJECT (proxy->priv->object), (gpointer *) &proxy->priv->object);
3202 proxy->priv->object = object;
3203 if (proxy->priv->object != NULL)
3204 g_object_add_weak_pointer (G_OBJECT (proxy->priv->object), (gpointer *) &proxy->priv->object);
3205 G_UNLOCK (properties_lock);
3208 static void
3209 dbus_interface_iface_init (GDBusInterfaceIface *dbus_interface_iface)
3211 dbus_interface_iface->get_info = _g_dbus_proxy_get_info;
3212 dbus_interface_iface->get_object = _g_dbus_proxy_get_object;
3213 dbus_interface_iface->dup_object = _g_dbus_proxy_dup_object;
3214 dbus_interface_iface->set_object = _g_dbus_proxy_set_object;
3217 /* ---------------------------------------------------------------------------------------------------- */