gosxappinfo: fix typo in url_escape_hostname
[glib.git] / gio / gdbusproxy.c
blob96c761bb15c7bd1817f2ec2217e1ac6c46480c53
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 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 gets
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
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): A %NULL-terminated array of strings or %NULL if
660 * @proxy has no cached properties. Free the returned array with
661 * g_strfreev().
663 * Since: 2.26
665 gchar **
666 g_dbus_proxy_get_cached_property_names (GDBusProxy *proxy)
668 gchar **names;
669 GPtrArray *p;
670 GHashTableIter iter;
671 const gchar *key;
673 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
675 G_LOCK (properties_lock);
677 names = NULL;
678 if (g_hash_table_size (proxy->priv->properties) == 0)
679 goto out;
681 p = g_ptr_array_new ();
683 g_hash_table_iter_init (&iter, proxy->priv->properties);
684 while (g_hash_table_iter_next (&iter, (gpointer) &key, NULL))
685 g_ptr_array_add (p, g_strdup (key));
686 g_ptr_array_sort (p, (GCompareFunc) property_name_sort_func);
687 g_ptr_array_add (p, NULL);
689 names = (gchar **) g_ptr_array_free (p, FALSE);
691 out:
692 G_UNLOCK (properties_lock);
693 return names;
696 /* properties_lock must be held for as long as you will keep the
697 * returned value
699 static const GDBusPropertyInfo *
700 lookup_property_info (GDBusProxy *proxy,
701 const gchar *property_name)
703 const GDBusPropertyInfo *info = NULL;
705 if (proxy->priv->expected_interface == NULL)
706 goto out;
708 info = g_dbus_interface_info_lookup_property (proxy->priv->expected_interface, property_name);
710 out:
711 return info;
715 * g_dbus_proxy_get_cached_property:
716 * @proxy: A #GDBusProxy.
717 * @property_name: Property name.
719 * Looks up the value for a property from the cache. This call does no
720 * blocking IO.
722 * If @proxy has an expected interface (see
723 * #GDBusProxy:g-interface-info) and @property_name is referenced by
724 * it, then @value is checked against the type of the property.
726 * Returns: A reference to the #GVariant instance that holds the value
727 * for @property_name or %NULL if the value is not in the cache. The
728 * returned reference must be freed with g_variant_unref().
730 * Since: 2.26
732 GVariant *
733 g_dbus_proxy_get_cached_property (GDBusProxy *proxy,
734 const gchar *property_name)
736 const GDBusPropertyInfo *info;
737 GVariant *value;
739 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
740 g_return_val_if_fail (property_name != NULL, NULL);
742 G_LOCK (properties_lock);
744 value = g_hash_table_lookup (proxy->priv->properties, property_name);
745 if (value == NULL)
746 goto out;
748 info = lookup_property_info (proxy, property_name);
749 if (info != NULL)
751 const gchar *type_string = g_variant_get_type_string (value);
752 if (g_strcmp0 (type_string, info->signature) != 0)
754 g_warning ("Trying to get property %s with type %s but according to the expected "
755 "interface the type is %s",
756 property_name,
757 type_string,
758 info->signature);
759 value = NULL;
760 goto out;
764 g_variant_ref (value);
766 out:
767 G_UNLOCK (properties_lock);
768 return value;
772 * g_dbus_proxy_set_cached_property:
773 * @proxy: A #GDBusProxy
774 * @property_name: Property name.
775 * @value: (nullable): Value for the property or %NULL to remove it from the cache.
777 * If @value is not %NULL, sets the cached value for the property with
778 * name @property_name to the value in @value.
780 * If @value is %NULL, then the cached value is removed from the
781 * property cache.
783 * If @proxy has an expected interface (see
784 * #GDBusProxy:g-interface-info) and @property_name is referenced by
785 * it, then @value is checked against the type of the property.
787 * If the @value #GVariant is floating, it is consumed. This allows
788 * convenient 'inline' use of g_variant_new(), e.g.
789 * |[<!-- language="C" -->
790 * g_dbus_proxy_set_cached_property (proxy,
791 * "SomeProperty",
792 * g_variant_new ("(si)",
793 * "A String",
794 * 42));
795 * ]|
797 * Normally you will not need to use this method since @proxy
798 * is tracking changes using the
799 * `org.freedesktop.DBus.Properties.PropertiesChanged`
800 * D-Bus signal. However, for performance reasons an object may
801 * decide to not use this signal for some properties and instead
802 * use a proprietary out-of-band mechanism to transmit changes.
804 * As a concrete example, consider an object with a property
805 * `ChatroomParticipants` which is an array of strings. Instead of
806 * transmitting the same (long) array every time the property changes,
807 * it is more efficient to only transmit the delta using e.g. signals
808 * `ChatroomParticipantJoined(String name)` and
809 * `ChatroomParticipantParted(String name)`.
811 * Since: 2.26
813 void
814 g_dbus_proxy_set_cached_property (GDBusProxy *proxy,
815 const gchar *property_name,
816 GVariant *value)
818 const GDBusPropertyInfo *info;
820 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
821 g_return_if_fail (property_name != NULL);
823 G_LOCK (properties_lock);
825 if (value != NULL)
827 info = lookup_property_info (proxy, property_name);
828 if (info != NULL)
830 if (g_strcmp0 (info->signature, g_variant_get_type_string (value)) != 0)
832 g_warning ("Trying to set property %s of type %s but according to the expected "
833 "interface the type is %s",
834 property_name,
835 g_variant_get_type_string (value),
836 info->signature);
837 goto out;
840 g_hash_table_insert (proxy->priv->properties,
841 g_strdup (property_name),
842 g_variant_ref_sink (value));
844 else
846 g_hash_table_remove (proxy->priv->properties, property_name);
849 out:
850 G_UNLOCK (properties_lock);
853 /* ---------------------------------------------------------------------------------------------------- */
855 static void
856 on_signal_received (GDBusConnection *connection,
857 const gchar *sender_name,
858 const gchar *object_path,
859 const gchar *interface_name,
860 const gchar *signal_name,
861 GVariant *parameters,
862 gpointer user_data)
864 SignalSubscriptionData *data = user_data;
865 GDBusProxy *proxy;
867 G_LOCK (signal_subscription_lock);
868 proxy = data->proxy;
869 if (proxy == NULL)
871 G_UNLOCK (signal_subscription_lock);
872 return;
874 else
876 g_object_ref (proxy);
877 G_UNLOCK (signal_subscription_lock);
880 if (!proxy->priv->initialized)
881 goto out;
883 G_LOCK (properties_lock);
885 if (proxy->priv->name_owner != NULL && g_strcmp0 (sender_name, proxy->priv->name_owner) != 0)
887 G_UNLOCK (properties_lock);
888 goto out;
891 if (proxy->priv->expected_interface != NULL)
893 const GDBusSignalInfo *info;
894 info = g_dbus_interface_info_lookup_signal (proxy->priv->expected_interface, signal_name);
895 if (info != NULL)
897 GVariantType *expected_type;
898 expected_type = _g_dbus_compute_complete_signature (info->args);
899 if (!g_variant_type_equal (expected_type, g_variant_get_type (parameters)))
901 gchar *expected_type_string = g_variant_type_dup_string (expected_type);
902 g_warning ("Dropping signal %s of type %s since the type from the expected interface is %s",
903 info->name,
904 g_variant_get_type_string (parameters),
905 expected_type_string);
906 g_free (expected_type_string);
907 g_variant_type_free (expected_type);
908 G_UNLOCK (properties_lock);
909 goto out;
911 g_variant_type_free (expected_type);
915 G_UNLOCK (properties_lock);
917 g_signal_emit (proxy,
918 signals[SIGNAL_SIGNAL],
920 sender_name,
921 signal_name,
922 parameters);
924 out:
925 if (proxy != NULL)
926 g_object_unref (proxy);
929 /* ---------------------------------------------------------------------------------------------------- */
931 /* must hold properties_lock */
932 static void
933 insert_property_checked (GDBusProxy *proxy,
934 gchar *property_name,
935 GVariant *value)
937 if (proxy->priv->expected_interface != NULL)
939 const GDBusPropertyInfo *info;
940 info = g_dbus_interface_info_lookup_property (proxy->priv->expected_interface, property_name);
941 /* Only check known properties */
942 if (info != NULL)
944 /* Warn about properties with the wrong type */
945 if (g_strcmp0 (info->signature, g_variant_get_type_string (value)) != 0)
947 g_warning ("Received property %s with type %s does not match expected type "
948 "%s in the expected interface",
949 property_name,
950 g_variant_get_type_string (value),
951 info->signature);
952 goto invalid;
957 g_hash_table_insert (proxy->priv->properties,
958 property_name, /* adopts string */
959 value); /* adopts value */
961 return;
963 invalid:
964 g_variant_unref (value);
965 g_free (property_name);
968 typedef struct
970 GDBusProxy *proxy;
971 gchar *prop_name;
972 } InvalidatedPropGetData;
974 static void
975 invalidated_property_get_cb (GDBusConnection *connection,
976 GAsyncResult *res,
977 gpointer user_data)
979 InvalidatedPropGetData *data = user_data;
980 const gchar *invalidated_properties[] = {NULL};
981 GVariantBuilder builder;
982 GVariant *value = NULL;
983 GVariant *unpacked_value = NULL;
985 /* errors are fine, the other end could have disconnected */
986 value = g_dbus_connection_call_finish (connection, res, NULL);
987 if (value == NULL)
989 goto out;
992 if (!g_variant_is_of_type (value, G_VARIANT_TYPE ("(v)")))
994 g_warning ("Expected type '(v)' for Get() reply, got '%s'", g_variant_get_type_string (value));
995 goto out;
998 g_variant_get (value, "(v)", &unpacked_value);
1000 /* synthesize the a{sv} in the PropertiesChanged signal */
1001 g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
1002 g_variant_builder_add (&builder, "{sv}", data->prop_name, unpacked_value);
1004 G_LOCK (properties_lock);
1005 insert_property_checked (data->proxy,
1006 data->prop_name, /* adopts string */
1007 unpacked_value); /* adopts value */
1008 data->prop_name = NULL;
1009 G_UNLOCK (properties_lock);
1011 g_signal_emit (data->proxy,
1012 signals[PROPERTIES_CHANGED_SIGNAL], 0,
1013 g_variant_builder_end (&builder), /* consumed */
1014 invalidated_properties);
1017 out:
1018 if (value != NULL)
1019 g_variant_unref (value);
1020 g_object_unref (data->proxy);
1021 g_free (data->prop_name);
1022 g_slice_free (InvalidatedPropGetData, data);
1025 static void
1026 on_properties_changed (GDBusConnection *connection,
1027 const gchar *sender_name,
1028 const gchar *object_path,
1029 const gchar *interface_name,
1030 const gchar *signal_name,
1031 GVariant *parameters,
1032 gpointer user_data)
1034 SignalSubscriptionData *data = user_data;
1035 gboolean emit_g_signal = FALSE;
1036 GDBusProxy *proxy;
1037 const gchar *interface_name_for_signal;
1038 GVariant *changed_properties;
1039 gchar **invalidated_properties;
1040 GVariantIter iter;
1041 gchar *key;
1042 GVariant *value;
1043 guint n;
1045 changed_properties = NULL;
1046 invalidated_properties = NULL;
1048 G_LOCK (signal_subscription_lock);
1049 proxy = data->proxy;
1050 if (proxy == NULL)
1052 G_UNLOCK (signal_subscription_lock);
1053 goto out;
1055 else
1057 g_object_ref (proxy);
1058 G_UNLOCK (signal_subscription_lock);
1061 if (!proxy->priv->initialized)
1062 goto out;
1064 G_LOCK (properties_lock);
1066 if (proxy->priv->name_owner != NULL && g_strcmp0 (sender_name, proxy->priv->name_owner) != 0)
1068 G_UNLOCK (properties_lock);
1069 goto out;
1072 if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(sa{sv}as)")))
1074 g_warning ("Value for PropertiesChanged signal with type '%s' does not match '(sa{sv}as)'",
1075 g_variant_get_type_string (parameters));
1076 G_UNLOCK (properties_lock);
1077 goto out;
1080 g_variant_get (parameters,
1081 "(&s@a{sv}^a&s)",
1082 &interface_name_for_signal,
1083 &changed_properties,
1084 &invalidated_properties);
1086 if (g_strcmp0 (interface_name_for_signal, proxy->priv->interface_name) != 0)
1088 G_UNLOCK (properties_lock);
1089 goto out;
1092 g_variant_iter_init (&iter, changed_properties);
1093 while (g_variant_iter_next (&iter, "{sv}", &key, &value))
1095 insert_property_checked (proxy,
1096 key, /* adopts string */
1097 value); /* adopts value */
1098 emit_g_signal = TRUE;
1101 if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_GET_INVALIDATED_PROPERTIES)
1103 if (proxy->priv->name_owner != NULL)
1105 for (n = 0; invalidated_properties[n] != NULL; n++)
1107 InvalidatedPropGetData *data;
1108 data = g_slice_new0 (InvalidatedPropGetData);
1109 data->proxy = g_object_ref (proxy);
1110 data->prop_name = g_strdup (invalidated_properties[n]);
1111 g_dbus_connection_call (proxy->priv->connection,
1112 proxy->priv->name_owner,
1113 proxy->priv->object_path,
1114 "org.freedesktop.DBus.Properties",
1115 "Get",
1116 g_variant_new ("(ss)", proxy->priv->interface_name, data->prop_name),
1117 G_VARIANT_TYPE ("(v)"),
1118 G_DBUS_CALL_FLAGS_NONE,
1119 -1, /* timeout */
1120 NULL, /* GCancellable */
1121 (GAsyncReadyCallback) invalidated_property_get_cb,
1122 data);
1126 else
1128 emit_g_signal = TRUE;
1129 for (n = 0; invalidated_properties[n] != NULL; n++)
1131 g_hash_table_remove (proxy->priv->properties, invalidated_properties[n]);
1135 G_UNLOCK (properties_lock);
1137 if (emit_g_signal)
1139 g_signal_emit (proxy, signals[PROPERTIES_CHANGED_SIGNAL],
1141 changed_properties,
1142 invalidated_properties);
1145 out:
1146 if (changed_properties != NULL)
1147 g_variant_unref (changed_properties);
1148 g_free (invalidated_properties);
1149 if (proxy != NULL)
1150 g_object_unref (proxy);
1153 /* ---------------------------------------------------------------------------------------------------- */
1155 static void
1156 process_get_all_reply (GDBusProxy *proxy,
1157 GVariant *result)
1159 GVariantIter *iter;
1160 gchar *key;
1161 GVariant *value;
1162 guint num_properties;
1164 if (!g_variant_is_of_type (result, G_VARIANT_TYPE ("(a{sv})")))
1166 g_warning ("Value for GetAll reply with type '%s' does not match '(a{sv})'",
1167 g_variant_get_type_string (result));
1168 goto out;
1171 G_LOCK (properties_lock);
1173 g_variant_get (result, "(a{sv})", &iter);
1174 while (g_variant_iter_next (iter, "{sv}", &key, &value))
1176 insert_property_checked (proxy,
1177 key, /* adopts string */
1178 value); /* adopts value */
1180 g_variant_iter_free (iter);
1182 num_properties = g_hash_table_size (proxy->priv->properties);
1183 G_UNLOCK (properties_lock);
1185 /* Synthesize ::g-properties-changed changed */
1186 if (num_properties > 0)
1188 GVariant *changed_properties;
1189 const gchar *invalidated_properties[1] = {NULL};
1191 g_variant_get (result,
1192 "(@a{sv})",
1193 &changed_properties);
1194 g_signal_emit (proxy, signals[PROPERTIES_CHANGED_SIGNAL],
1196 changed_properties,
1197 invalidated_properties);
1198 g_variant_unref (changed_properties);
1201 out:
1205 typedef struct
1207 GDBusProxy *proxy;
1208 GCancellable *cancellable;
1209 gchar *name_owner;
1210 } LoadPropertiesOnNameOwnerChangedData;
1212 static void
1213 on_name_owner_changed_get_all_cb (GDBusConnection *connection,
1214 GAsyncResult *res,
1215 gpointer user_data)
1217 LoadPropertiesOnNameOwnerChangedData *data = user_data;
1218 GVariant *result;
1219 GError *error;
1220 gboolean cancelled;
1222 cancelled = FALSE;
1224 error = NULL;
1225 result = g_dbus_connection_call_finish (connection,
1226 res,
1227 &error);
1228 if (result == NULL)
1230 if (error->domain == G_IO_ERROR && error->code == G_IO_ERROR_CANCELLED)
1231 cancelled = TRUE;
1232 /* We just ignore if GetAll() is failing. Because this might happen
1233 * if the object has no properties at all. Or if the caller is
1234 * not authorized to see the properties.
1236 * Either way, apps can know about this by using
1237 * get_cached_property_names() or get_cached_property().
1239 * TODO: handle G_DBUS_DEBUG flag 'proxy' and, if enabled, log the
1240 * fact that GetAll() failed
1242 //g_debug ("error: %d %d %s", error->domain, error->code, error->message);
1243 g_error_free (error);
1246 /* and finally we can notify */
1247 if (!cancelled)
1249 G_LOCK (properties_lock);
1250 g_free (data->proxy->priv->name_owner);
1251 data->proxy->priv->name_owner = data->name_owner;
1252 data->name_owner = NULL; /* to avoid an extra copy, we steal the string */
1253 g_hash_table_remove_all (data->proxy->priv->properties);
1254 G_UNLOCK (properties_lock);
1255 if (result != NULL)
1257 process_get_all_reply (data->proxy, result);
1258 g_variant_unref (result);
1261 g_object_notify (G_OBJECT (data->proxy), "g-name-owner");
1264 if (data->cancellable == data->proxy->priv->get_all_cancellable)
1265 data->proxy->priv->get_all_cancellable = NULL;
1267 g_object_unref (data->proxy);
1268 g_object_unref (data->cancellable);
1269 g_free (data->name_owner);
1270 g_free (data);
1273 static void
1274 on_name_owner_changed (GDBusConnection *connection,
1275 const gchar *sender_name,
1276 const gchar *object_path,
1277 const gchar *interface_name,
1278 const gchar *signal_name,
1279 GVariant *parameters,
1280 gpointer user_data)
1282 SignalSubscriptionData *data = user_data;
1283 GDBusProxy *proxy;
1284 const gchar *old_owner;
1285 const gchar *new_owner;
1287 G_LOCK (signal_subscription_lock);
1288 proxy = data->proxy;
1289 if (proxy == NULL)
1291 G_UNLOCK (signal_subscription_lock);
1292 goto out;
1294 else
1296 g_object_ref (proxy);
1297 G_UNLOCK (signal_subscription_lock);
1300 /* if we are already trying to load properties, cancel that */
1301 if (proxy->priv->get_all_cancellable != NULL)
1303 g_cancellable_cancel (proxy->priv->get_all_cancellable);
1304 proxy->priv->get_all_cancellable = NULL;
1307 g_variant_get (parameters,
1308 "(&s&s&s)",
1309 NULL,
1310 &old_owner,
1311 &new_owner);
1313 if (strlen (new_owner) == 0)
1315 G_LOCK (properties_lock);
1316 g_free (proxy->priv->name_owner);
1317 proxy->priv->name_owner = NULL;
1319 /* Synthesize ::g-properties-changed changed */
1320 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES) &&
1321 g_hash_table_size (proxy->priv->properties) > 0)
1323 GVariantBuilder builder;
1324 GPtrArray *invalidated_properties;
1325 GHashTableIter iter;
1326 const gchar *key;
1328 /* Build changed_properties (always empty) and invalidated_properties ... */
1329 g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
1331 invalidated_properties = g_ptr_array_new_with_free_func (g_free);
1332 g_hash_table_iter_init (&iter, proxy->priv->properties);
1333 while (g_hash_table_iter_next (&iter, (gpointer) &key, NULL))
1334 g_ptr_array_add (invalidated_properties, g_strdup (key));
1335 g_ptr_array_add (invalidated_properties, NULL);
1337 /* ... throw out the properties ... */
1338 g_hash_table_remove_all (proxy->priv->properties);
1340 G_UNLOCK (properties_lock);
1342 /* ... and finally emit the ::g-properties-changed signal */
1343 g_signal_emit (proxy, signals[PROPERTIES_CHANGED_SIGNAL],
1345 g_variant_builder_end (&builder) /* consumed */,
1346 (const gchar* const *) invalidated_properties->pdata);
1347 g_ptr_array_unref (invalidated_properties);
1349 else
1351 G_UNLOCK (properties_lock);
1353 g_object_notify (G_OBJECT (proxy), "g-name-owner");
1355 else
1357 G_LOCK (properties_lock);
1359 /* ignore duplicates - this can happen when activating the service */
1360 if (g_strcmp0 (new_owner, proxy->priv->name_owner) == 0)
1362 G_UNLOCK (properties_lock);
1363 goto out;
1366 if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES)
1368 g_free (proxy->priv->name_owner);
1369 proxy->priv->name_owner = g_strdup (new_owner);
1371 g_hash_table_remove_all (proxy->priv->properties);
1372 G_UNLOCK (properties_lock);
1373 g_object_notify (G_OBJECT (proxy), "g-name-owner");
1375 else
1377 LoadPropertiesOnNameOwnerChangedData *data;
1379 G_UNLOCK (properties_lock);
1381 /* start loading properties.. only then emit notify::g-name-owner .. we
1382 * need to be able to cancel this in the event another NameOwnerChanged
1383 * signal suddenly happens
1386 g_assert (proxy->priv->get_all_cancellable == NULL);
1387 proxy->priv->get_all_cancellable = g_cancellable_new ();
1388 data = g_new0 (LoadPropertiesOnNameOwnerChangedData, 1);
1389 data->proxy = g_object_ref (proxy);
1390 data->cancellable = proxy->priv->get_all_cancellable;
1391 data->name_owner = g_strdup (new_owner);
1392 g_dbus_connection_call (proxy->priv->connection,
1393 data->name_owner,
1394 proxy->priv->object_path,
1395 "org.freedesktop.DBus.Properties",
1396 "GetAll",
1397 g_variant_new ("(s)", proxy->priv->interface_name),
1398 G_VARIANT_TYPE ("(a{sv})"),
1399 G_DBUS_CALL_FLAGS_NONE,
1400 -1, /* timeout */
1401 proxy->priv->get_all_cancellable,
1402 (GAsyncReadyCallback) on_name_owner_changed_get_all_cb,
1403 data);
1407 out:
1408 if (proxy != NULL)
1409 g_object_unref (proxy);
1412 /* ---------------------------------------------------------------------------------------------------- */
1414 static void
1415 async_init_get_all_cb (GDBusConnection *connection,
1416 GAsyncResult *res,
1417 gpointer user_data)
1419 GTask *task = user_data;
1420 GVariant *result;
1421 GError *error;
1423 error = NULL;
1424 result = g_dbus_connection_call_finish (connection,
1425 res,
1426 &error);
1427 if (result == NULL)
1429 /* We just ignore if GetAll() is failing. Because this might happen
1430 * if the object has no properties at all. Or if the caller is
1431 * not authorized to see the properties.
1433 * Either way, apps can know about this by using
1434 * get_cached_property_names() or get_cached_property().
1436 * TODO: handle G_DBUS_DEBUG flag 'proxy' and, if enabled, log the
1437 * fact that GetAll() failed
1439 //g_debug ("error: %d %d %s", error->domain, error->code, error->message);
1440 g_error_free (error);
1443 g_task_return_pointer (task, result,
1444 (GDestroyNotify) g_variant_unref);
1445 g_object_unref (task);
1448 static void
1449 async_init_data_set_name_owner (GTask *task,
1450 const gchar *name_owner)
1452 GDBusProxy *proxy = g_task_get_source_object (task);
1453 gboolean get_all;
1455 if (name_owner != NULL)
1457 G_LOCK (properties_lock);
1458 /* Must free first, since on_name_owner_changed() could run before us */
1459 g_free (proxy->priv->name_owner);
1460 proxy->priv->name_owner = g_strdup (name_owner);
1461 G_UNLOCK (properties_lock);
1464 get_all = TRUE;
1466 if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES)
1468 /* Don't load properties if the API user doesn't want them */
1469 get_all = FALSE;
1471 else if (name_owner == NULL && proxy->priv->name != NULL)
1473 /* Don't attempt to load properties if the name_owner is NULL (which
1474 * usually means the name isn't owned), unless name is also NULL (which
1475 * means we actually wanted to talk to the directly-connected process -
1476 * either dbus-daemon or a peer - instead of going via dbus-daemon)
1478 get_all = FALSE;
1481 if (get_all)
1483 /* load all properties asynchronously */
1484 g_dbus_connection_call (proxy->priv->connection,
1485 name_owner,
1486 proxy->priv->object_path,
1487 "org.freedesktop.DBus.Properties",
1488 "GetAll",
1489 g_variant_new ("(s)", proxy->priv->interface_name),
1490 G_VARIANT_TYPE ("(a{sv})"),
1491 G_DBUS_CALL_FLAGS_NONE,
1492 -1, /* timeout */
1493 g_task_get_cancellable (task),
1494 (GAsyncReadyCallback) async_init_get_all_cb,
1495 task);
1497 else
1499 g_task_return_pointer (task, NULL, NULL);
1500 g_object_unref (task);
1504 static void
1505 async_init_get_name_owner_cb (GDBusConnection *connection,
1506 GAsyncResult *res,
1507 gpointer user_data)
1509 GTask *task = user_data;
1510 GError *error;
1511 GVariant *result;
1513 error = NULL;
1514 result = g_dbus_connection_call_finish (connection,
1515 res,
1516 &error);
1517 if (result == NULL)
1519 if (error->domain == G_DBUS_ERROR &&
1520 error->code == G_DBUS_ERROR_NAME_HAS_NO_OWNER)
1522 g_error_free (error);
1523 async_init_data_set_name_owner (task, NULL);
1525 else
1527 g_task_return_error (task, error);
1528 g_object_unref (task);
1531 else
1533 /* borrowed from result to avoid an extra copy */
1534 const gchar *name_owner;
1536 g_variant_get (result, "(&s)", &name_owner);
1537 async_init_data_set_name_owner (task, name_owner);
1538 g_variant_unref (result);
1542 static void
1543 async_init_call_get_name_owner (GTask *task)
1545 GDBusProxy *proxy = g_task_get_source_object (task);
1547 g_dbus_connection_call (proxy->priv->connection,
1548 "org.freedesktop.DBus", /* name */
1549 "/org/freedesktop/DBus", /* object path */
1550 "org.freedesktop.DBus", /* interface */
1551 "GetNameOwner",
1552 g_variant_new ("(s)",
1553 proxy->priv->name),
1554 G_VARIANT_TYPE ("(s)"),
1555 G_DBUS_CALL_FLAGS_NONE,
1556 -1, /* timeout */
1557 g_task_get_cancellable (task),
1558 (GAsyncReadyCallback) async_init_get_name_owner_cb,
1559 task);
1562 static void
1563 async_init_start_service_by_name_cb (GDBusConnection *connection,
1564 GAsyncResult *res,
1565 gpointer user_data)
1567 GTask *task = user_data;
1568 GDBusProxy *proxy = g_task_get_source_object (task);
1569 GError *error;
1570 GVariant *result;
1572 error = NULL;
1573 result = g_dbus_connection_call_finish (connection,
1574 res,
1575 &error);
1576 if (result == NULL)
1578 /* Errors are not unexpected; the bus will reply e.g.
1580 * org.freedesktop.DBus.Error.ServiceUnknown: The name org.gnome.Epiphany2
1581 * was not provided by any .service files
1583 * or (see #677718)
1585 * org.freedesktop.systemd1.Masked: Unit polkit.service is masked.
1587 * This doesn't mean that the name doesn't have an owner, just
1588 * that it's not provided by a .service file or can't currently
1589 * be started.
1591 * In particular, in both cases, it could be that a service
1592 * owner will actually appear later. So instead of erroring out,
1593 * we just proceed to invoke GetNameOwner() if dealing with the
1594 * kind of errors above.
1596 if (error->domain == G_DBUS_ERROR && error->code == G_DBUS_ERROR_SERVICE_UNKNOWN)
1598 g_error_free (error);
1600 else
1602 gchar *remote_error = g_dbus_error_get_remote_error (error);
1603 if (g_strcmp0 (remote_error, "org.freedesktop.systemd1.Masked") == 0)
1605 g_error_free (error);
1606 g_free (remote_error);
1608 else
1610 g_prefix_error (&error,
1611 _("Error calling StartServiceByName for %s: "),
1612 proxy->priv->name);
1613 g_free (remote_error);
1614 goto failed;
1618 else
1620 guint32 start_service_result;
1621 g_variant_get (result,
1622 "(u)",
1623 &start_service_result);
1624 g_variant_unref (result);
1625 if (start_service_result == 1 || /* DBUS_START_REPLY_SUCCESS */
1626 start_service_result == 2) /* DBUS_START_REPLY_ALREADY_RUNNING */
1628 /* continue to invoke GetNameOwner() */
1630 else
1632 error = g_error_new (G_IO_ERROR,
1633 G_IO_ERROR_FAILED,
1634 _("Unexpected reply %d from StartServiceByName(\"%s\") method"),
1635 start_service_result,
1636 proxy->priv->name);
1637 goto failed;
1641 async_init_call_get_name_owner (task);
1642 return;
1644 failed:
1645 g_warn_if_fail (error != NULL);
1646 g_task_return_error (task, error);
1647 g_object_unref (task);
1650 static void
1651 async_init_call_start_service_by_name (GTask *task)
1653 GDBusProxy *proxy = g_task_get_source_object (task);
1655 g_dbus_connection_call (proxy->priv->connection,
1656 "org.freedesktop.DBus", /* name */
1657 "/org/freedesktop/DBus", /* object path */
1658 "org.freedesktop.DBus", /* interface */
1659 "StartServiceByName",
1660 g_variant_new ("(su)",
1661 proxy->priv->name,
1663 G_VARIANT_TYPE ("(u)"),
1664 G_DBUS_CALL_FLAGS_NONE,
1665 -1, /* timeout */
1666 g_task_get_cancellable (task),
1667 (GAsyncReadyCallback) async_init_start_service_by_name_cb,
1668 task);
1671 static void
1672 async_initable_init_second_async (GAsyncInitable *initable,
1673 gint io_priority,
1674 GCancellable *cancellable,
1675 GAsyncReadyCallback callback,
1676 gpointer user_data)
1678 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1679 GTask *task;
1681 task = g_task_new (proxy, cancellable, callback, user_data);
1682 g_task_set_source_tag (task, async_initable_init_second_async);
1683 g_task_set_priority (task, io_priority);
1685 /* Check name ownership asynchronously - possibly also start the service */
1686 if (proxy->priv->name == NULL)
1688 /* Do nothing */
1689 async_init_data_set_name_owner (task, NULL);
1691 else if (g_dbus_is_unique_name (proxy->priv->name))
1693 async_init_data_set_name_owner (task, proxy->priv->name);
1695 else
1697 if ((proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START) ||
1698 (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START_AT_CONSTRUCTION))
1700 async_init_call_get_name_owner (task);
1702 else
1704 async_init_call_start_service_by_name (task);
1709 static gboolean
1710 async_initable_init_second_finish (GAsyncInitable *initable,
1711 GAsyncResult *res,
1712 GError **error)
1714 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1715 GTask *task = G_TASK (res);
1716 GVariant *result;
1717 gboolean ret;
1719 ret = !g_task_had_error (task);
1721 result = g_task_propagate_pointer (task, error);
1722 if (result != NULL)
1724 process_get_all_reply (proxy, result);
1725 g_variant_unref (result);
1728 proxy->priv->initialized = TRUE;
1729 return ret;
1732 /* ---------------------------------------------------------------------------------------------------- */
1734 static void
1735 async_initable_init_first (GAsyncInitable *initable)
1737 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1739 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES))
1741 /* subscribe to PropertiesChanged() */
1742 proxy->priv->properties_changed_subscription_id =
1743 g_dbus_connection_signal_subscribe (proxy->priv->connection,
1744 proxy->priv->name,
1745 "org.freedesktop.DBus.Properties",
1746 "PropertiesChanged",
1747 proxy->priv->object_path,
1748 proxy->priv->interface_name,
1749 G_DBUS_SIGNAL_FLAGS_NONE,
1750 on_properties_changed,
1751 signal_subscription_ref (proxy->priv->signal_subscription_data),
1752 (GDestroyNotify) signal_subscription_unref);
1755 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS))
1757 /* subscribe to all signals for the object */
1758 proxy->priv->signals_subscription_id =
1759 g_dbus_connection_signal_subscribe (proxy->priv->connection,
1760 proxy->priv->name,
1761 proxy->priv->interface_name,
1762 NULL, /* member */
1763 proxy->priv->object_path,
1764 NULL, /* arg0 */
1765 G_DBUS_SIGNAL_FLAGS_NONE,
1766 on_signal_received,
1767 signal_subscription_ref (proxy->priv->signal_subscription_data),
1768 (GDestroyNotify) signal_subscription_unref);
1771 if (proxy->priv->name != NULL && !g_dbus_is_unique_name (proxy->priv->name))
1773 proxy->priv->name_owner_changed_subscription_id =
1774 g_dbus_connection_signal_subscribe (proxy->priv->connection,
1775 "org.freedesktop.DBus", /* name */
1776 "org.freedesktop.DBus", /* interface */
1777 "NameOwnerChanged", /* signal name */
1778 "/org/freedesktop/DBus", /* path */
1779 proxy->priv->name, /* arg0 */
1780 G_DBUS_SIGNAL_FLAGS_NONE,
1781 on_name_owner_changed,
1782 signal_subscription_ref (proxy->priv->signal_subscription_data),
1783 (GDestroyNotify) signal_subscription_unref);
1787 /* ---------------------------------------------------------------------------------------------------- */
1789 /* initialization is split into two parts - the first is the
1790 * non-blocking part that requires the callers GMainContext - the
1791 * second is a blocking part async part that doesn't require the
1792 * callers GMainContext.. we do this split so the code can be reused
1793 * in the GInitable implementation below.
1795 * Note that obtaining a GDBusConnection is not shared between the two
1796 * paths.
1799 static void
1800 init_second_async_cb (GObject *source_object,
1801 GAsyncResult *res,
1802 gpointer user_data)
1804 GTask *task = user_data;
1805 GError *error = NULL;
1807 if (async_initable_init_second_finish (G_ASYNC_INITABLE (source_object), res, &error))
1808 g_task_return_boolean (task, TRUE);
1809 else
1810 g_task_return_error (task, error);
1811 g_object_unref (task);
1814 static void
1815 get_connection_cb (GObject *source_object,
1816 GAsyncResult *res,
1817 gpointer user_data)
1819 GTask *task = user_data;
1820 GDBusProxy *proxy = g_task_get_source_object (task);
1821 GError *error;
1823 error = NULL;
1824 proxy->priv->connection = g_bus_get_finish (res, &error);
1825 if (proxy->priv->connection == NULL)
1827 g_task_return_error (task, error);
1828 g_object_unref (task);
1830 else
1832 async_initable_init_first (G_ASYNC_INITABLE (proxy));
1833 async_initable_init_second_async (G_ASYNC_INITABLE (proxy),
1834 g_task_get_priority (task),
1835 g_task_get_cancellable (task),
1836 init_second_async_cb,
1837 task);
1841 static void
1842 async_initable_init_async (GAsyncInitable *initable,
1843 gint io_priority,
1844 GCancellable *cancellable,
1845 GAsyncReadyCallback callback,
1846 gpointer user_data)
1848 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1849 GTask *task;
1851 task = g_task_new (proxy, cancellable, callback, user_data);
1852 g_task_set_source_tag (task, async_initable_init_async);
1853 g_task_set_priority (task, io_priority);
1855 if (proxy->priv->bus_type != G_BUS_TYPE_NONE)
1857 g_assert (proxy->priv->connection == NULL);
1859 g_bus_get (proxy->priv->bus_type,
1860 cancellable,
1861 get_connection_cb,
1862 task);
1864 else
1866 async_initable_init_first (initable);
1867 async_initable_init_second_async (initable, io_priority, cancellable,
1868 init_second_async_cb, task);
1872 static gboolean
1873 async_initable_init_finish (GAsyncInitable *initable,
1874 GAsyncResult *res,
1875 GError **error)
1877 return g_task_propagate_boolean (G_TASK (res), error);
1880 static void
1881 async_initable_iface_init (GAsyncInitableIface *async_initable_iface)
1883 async_initable_iface->init_async = async_initable_init_async;
1884 async_initable_iface->init_finish = async_initable_init_finish;
1887 /* ---------------------------------------------------------------------------------------------------- */
1889 typedef struct
1891 GMainContext *context;
1892 GMainLoop *loop;
1893 GAsyncResult *res;
1894 } InitableAsyncInitableData;
1896 static void
1897 async_initable_init_async_cb (GObject *source_object,
1898 GAsyncResult *res,
1899 gpointer user_data)
1901 InitableAsyncInitableData *data = user_data;
1902 data->res = g_object_ref (res);
1903 g_main_loop_quit (data->loop);
1906 /* Simply reuse the GAsyncInitable implementation but run the first
1907 * part (that is non-blocking and requires the callers GMainContext)
1908 * with the callers GMainContext.. and the second with a private
1909 * GMainContext (bug 621310 is slightly related).
1911 * Note that obtaining a GDBusConnection is not shared between the two
1912 * paths.
1914 static gboolean
1915 initable_init (GInitable *initable,
1916 GCancellable *cancellable,
1917 GError **error)
1919 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1920 InitableAsyncInitableData *data;
1921 gboolean ret;
1923 ret = FALSE;
1925 if (proxy->priv->bus_type != G_BUS_TYPE_NONE)
1927 g_assert (proxy->priv->connection == NULL);
1928 proxy->priv->connection = g_bus_get_sync (proxy->priv->bus_type,
1929 cancellable,
1930 error);
1931 if (proxy->priv->connection == NULL)
1932 goto out;
1935 async_initable_init_first (G_ASYNC_INITABLE (initable));
1937 data = g_new0 (InitableAsyncInitableData, 1);
1938 data->context = g_main_context_new ();
1939 data->loop = g_main_loop_new (data->context, FALSE);
1941 g_main_context_push_thread_default (data->context);
1943 async_initable_init_second_async (G_ASYNC_INITABLE (initable),
1944 G_PRIORITY_DEFAULT,
1945 cancellable,
1946 async_initable_init_async_cb,
1947 data);
1949 g_main_loop_run (data->loop);
1951 ret = async_initable_init_second_finish (G_ASYNC_INITABLE (initable),
1952 data->res,
1953 error);
1955 g_main_context_pop_thread_default (data->context);
1957 g_main_context_unref (data->context);
1958 g_main_loop_unref (data->loop);
1959 g_object_unref (data->res);
1960 g_free (data);
1962 out:
1964 return ret;
1967 static void
1968 initable_iface_init (GInitableIface *initable_iface)
1970 initable_iface->init = initable_init;
1973 /* ---------------------------------------------------------------------------------------------------- */
1976 * g_dbus_proxy_new:
1977 * @connection: A #GDBusConnection.
1978 * @flags: Flags used when constructing the proxy.
1979 * @info: (nullable): A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
1980 * @name: (nullable): A bus name (well-known or unique) or %NULL if @connection is not a message bus connection.
1981 * @object_path: An object path.
1982 * @interface_name: A D-Bus interface name.
1983 * @cancellable: (nullable): A #GCancellable or %NULL.
1984 * @callback: Callback function to invoke when the proxy is ready.
1985 * @user_data: User data to pass to @callback.
1987 * Creates a proxy for accessing @interface_name on the remote object
1988 * at @object_path owned by @name at @connection and asynchronously
1989 * loads D-Bus properties unless the
1990 * %G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES flag is used. Connect to
1991 * the #GDBusProxy::g-properties-changed signal to get notified about
1992 * property changes.
1994 * If the %G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS flag is not set, also sets up
1995 * match rules for signals. Connect to the #GDBusProxy::g-signal signal
1996 * to handle signals from the remote object.
1998 * If @name is a well-known name and the
1999 * %G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START and %G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START_AT_CONSTRUCTION
2000 * flags aren't set and no name owner currently exists, the message bus
2001 * will be requested to launch a name owner for the name.
2003 * This is a failable asynchronous constructor - when the proxy is
2004 * ready, @callback will be invoked and you can use
2005 * g_dbus_proxy_new_finish() to get the result.
2007 * See g_dbus_proxy_new_sync() and for a synchronous version of this constructor.
2009 * #GDBusProxy is used in this [example][gdbus-wellknown-proxy].
2011 * Since: 2.26
2013 void
2014 g_dbus_proxy_new (GDBusConnection *connection,
2015 GDBusProxyFlags flags,
2016 GDBusInterfaceInfo *info,
2017 const gchar *name,
2018 const gchar *object_path,
2019 const gchar *interface_name,
2020 GCancellable *cancellable,
2021 GAsyncReadyCallback callback,
2022 gpointer user_data)
2024 _g_dbus_initialize ();
2026 g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
2027 g_return_if_fail ((name == NULL && g_dbus_connection_get_unique_name (connection) == NULL) || g_dbus_is_name (name));
2028 g_return_if_fail (g_variant_is_object_path (object_path));
2029 g_return_if_fail (g_dbus_is_interface_name (interface_name));
2031 g_async_initable_new_async (G_TYPE_DBUS_PROXY,
2032 G_PRIORITY_DEFAULT,
2033 cancellable,
2034 callback,
2035 user_data,
2036 "g-flags", flags,
2037 "g-interface-info", info,
2038 "g-name", name,
2039 "g-connection", connection,
2040 "g-object-path", object_path,
2041 "g-interface-name", interface_name,
2042 NULL);
2046 * g_dbus_proxy_new_finish:
2047 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback function passed to g_dbus_proxy_new().
2048 * @error: Return location for error or %NULL.
2050 * Finishes creating a #GDBusProxy.
2052 * Returns: A #GDBusProxy or %NULL if @error is set. Free with g_object_unref().
2054 * Since: 2.26
2056 GDBusProxy *
2057 g_dbus_proxy_new_finish (GAsyncResult *res,
2058 GError **error)
2060 GObject *object;
2061 GObject *source_object;
2063 source_object = g_async_result_get_source_object (res);
2064 g_assert (source_object != NULL);
2066 object = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object),
2067 res,
2068 error);
2069 g_object_unref (source_object);
2071 if (object != NULL)
2072 return G_DBUS_PROXY (object);
2073 else
2074 return NULL;
2078 * g_dbus_proxy_new_sync:
2079 * @connection: A #GDBusConnection.
2080 * @flags: Flags used when constructing the proxy.
2081 * @info: (nullable): A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
2082 * @name: (nullable): A bus name (well-known or unique) or %NULL if @connection is not a message bus connection.
2083 * @object_path: An object path.
2084 * @interface_name: A D-Bus interface name.
2085 * @cancellable: (nullable): A #GCancellable or %NULL.
2086 * @error: (nullable): Return location for error or %NULL.
2088 * Creates a proxy for accessing @interface_name on the remote object
2089 * at @object_path owned by @name at @connection and synchronously
2090 * loads D-Bus properties unless the
2091 * %G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES flag is used.
2093 * If the %G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS flag is not set, also sets up
2094 * match rules for signals. Connect to the #GDBusProxy::g-signal signal
2095 * to handle signals from the remote object.
2097 * If @name is a well-known name and the
2098 * %G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START and %G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START_AT_CONSTRUCTION
2099 * flags aren't set and no name owner currently exists, the message bus
2100 * will be requested to launch a name owner for the name.
2102 * This is a synchronous failable constructor. See g_dbus_proxy_new()
2103 * and g_dbus_proxy_new_finish() for the asynchronous version.
2105 * #GDBusProxy is used in this [example][gdbus-wellknown-proxy].
2107 * Returns: A #GDBusProxy or %NULL if error is set. Free with g_object_unref().
2109 * Since: 2.26
2111 GDBusProxy *
2112 g_dbus_proxy_new_sync (GDBusConnection *connection,
2113 GDBusProxyFlags flags,
2114 GDBusInterfaceInfo *info,
2115 const gchar *name,
2116 const gchar *object_path,
2117 const gchar *interface_name,
2118 GCancellable *cancellable,
2119 GError **error)
2121 GInitable *initable;
2123 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
2124 g_return_val_if_fail ((name == NULL && g_dbus_connection_get_unique_name (connection) == NULL) ||
2125 g_dbus_is_name (name), NULL);
2126 g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
2127 g_return_val_if_fail (g_dbus_is_interface_name (interface_name), NULL);
2129 initable = g_initable_new (G_TYPE_DBUS_PROXY,
2130 cancellable,
2131 error,
2132 "g-flags", flags,
2133 "g-interface-info", info,
2134 "g-name", name,
2135 "g-connection", connection,
2136 "g-object-path", object_path,
2137 "g-interface-name", interface_name,
2138 NULL);
2139 if (initable != NULL)
2140 return G_DBUS_PROXY (initable);
2141 else
2142 return NULL;
2145 /* ---------------------------------------------------------------------------------------------------- */
2148 * g_dbus_proxy_new_for_bus:
2149 * @bus_type: A #GBusType.
2150 * @flags: Flags used when constructing the proxy.
2151 * @info: (nullable): A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
2152 * @name: A bus name (well-known or unique).
2153 * @object_path: An object path.
2154 * @interface_name: A D-Bus interface name.
2155 * @cancellable: (nullable): A #GCancellable or %NULL.
2156 * @callback: Callback function to invoke when the proxy is ready.
2157 * @user_data: User data to pass to @callback.
2159 * Like g_dbus_proxy_new() but takes a #GBusType instead of a #GDBusConnection.
2161 * #GDBusProxy is used in this [example][gdbus-wellknown-proxy].
2163 * Since: 2.26
2165 void
2166 g_dbus_proxy_new_for_bus (GBusType bus_type,
2167 GDBusProxyFlags flags,
2168 GDBusInterfaceInfo *info,
2169 const gchar *name,
2170 const gchar *object_path,
2171 const gchar *interface_name,
2172 GCancellable *cancellable,
2173 GAsyncReadyCallback callback,
2174 gpointer user_data)
2176 _g_dbus_initialize ();
2178 g_return_if_fail (g_dbus_is_name (name));
2179 g_return_if_fail (g_variant_is_object_path (object_path));
2180 g_return_if_fail (g_dbus_is_interface_name (interface_name));
2182 g_async_initable_new_async (G_TYPE_DBUS_PROXY,
2183 G_PRIORITY_DEFAULT,
2184 cancellable,
2185 callback,
2186 user_data,
2187 "g-flags", flags,
2188 "g-interface-info", info,
2189 "g-name", name,
2190 "g-bus-type", bus_type,
2191 "g-object-path", object_path,
2192 "g-interface-name", interface_name,
2193 NULL);
2197 * g_dbus_proxy_new_for_bus_finish:
2198 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback function passed to g_dbus_proxy_new_for_bus().
2199 * @error: Return location for error or %NULL.
2201 * Finishes creating a #GDBusProxy.
2203 * Returns: A #GDBusProxy or %NULL if @error is set. Free with g_object_unref().
2205 * Since: 2.26
2207 GDBusProxy *
2208 g_dbus_proxy_new_for_bus_finish (GAsyncResult *res,
2209 GError **error)
2211 return g_dbus_proxy_new_finish (res, error);
2215 * g_dbus_proxy_new_for_bus_sync:
2216 * @bus_type: A #GBusType.
2217 * @flags: Flags used when constructing the proxy.
2218 * @info: (nullable): A #GDBusInterfaceInfo specifying the minimal interface
2219 * that @proxy conforms to or %NULL.
2220 * @name: A bus name (well-known or unique).
2221 * @object_path: An object path.
2222 * @interface_name: A D-Bus interface name.
2223 * @cancellable: (nullable): A #GCancellable or %NULL.
2224 * @error: Return location for error or %NULL.
2226 * Like g_dbus_proxy_new_sync() but takes a #GBusType instead of a #GDBusConnection.
2228 * #GDBusProxy is used in this [example][gdbus-wellknown-proxy].
2230 * Returns: A #GDBusProxy or %NULL if error is set. Free with g_object_unref().
2232 * Since: 2.26
2234 GDBusProxy *
2235 g_dbus_proxy_new_for_bus_sync (GBusType bus_type,
2236 GDBusProxyFlags flags,
2237 GDBusInterfaceInfo *info,
2238 const gchar *name,
2239 const gchar *object_path,
2240 const gchar *interface_name,
2241 GCancellable *cancellable,
2242 GError **error)
2244 GInitable *initable;
2246 _g_dbus_initialize ();
2248 g_return_val_if_fail (g_dbus_is_name (name), NULL);
2249 g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
2250 g_return_val_if_fail (g_dbus_is_interface_name (interface_name), NULL);
2252 initable = g_initable_new (G_TYPE_DBUS_PROXY,
2253 cancellable,
2254 error,
2255 "g-flags", flags,
2256 "g-interface-info", info,
2257 "g-name", name,
2258 "g-bus-type", bus_type,
2259 "g-object-path", object_path,
2260 "g-interface-name", interface_name,
2261 NULL);
2262 if (initable != NULL)
2263 return G_DBUS_PROXY (initable);
2264 else
2265 return NULL;
2268 /* ---------------------------------------------------------------------------------------------------- */
2271 * g_dbus_proxy_get_connection:
2272 * @proxy: A #GDBusProxy.
2274 * Gets the connection @proxy is for.
2276 * Returns: (transfer none): A #GDBusConnection owned by @proxy. Do not free.
2278 * Since: 2.26
2280 GDBusConnection *
2281 g_dbus_proxy_get_connection (GDBusProxy *proxy)
2283 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2284 return proxy->priv->connection;
2288 * g_dbus_proxy_get_flags:
2289 * @proxy: A #GDBusProxy.
2291 * Gets the flags that @proxy was constructed with.
2293 * Returns: Flags from the #GDBusProxyFlags enumeration.
2295 * Since: 2.26
2297 GDBusProxyFlags
2298 g_dbus_proxy_get_flags (GDBusProxy *proxy)
2300 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), 0);
2301 return proxy->priv->flags;
2305 * g_dbus_proxy_get_name:
2306 * @proxy: A #GDBusProxy.
2308 * Gets the name that @proxy was constructed for.
2310 * Returns: A string owned by @proxy. Do not free.
2312 * Since: 2.26
2314 const gchar *
2315 g_dbus_proxy_get_name (GDBusProxy *proxy)
2317 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2318 return proxy->priv->name;
2322 * g_dbus_proxy_get_name_owner:
2323 * @proxy: A #GDBusProxy.
2325 * The unique name that owns the name that @proxy is for or %NULL if
2326 * no-one currently owns that name. You may connect to the
2327 * #GObject::notify signal to track changes to the
2328 * #GDBusProxy:g-name-owner property.
2330 * Returns: The name owner or %NULL if no name owner exists. Free with g_free().
2332 * Since: 2.26
2334 gchar *
2335 g_dbus_proxy_get_name_owner (GDBusProxy *proxy)
2337 gchar *ret;
2339 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2341 G_LOCK (properties_lock);
2342 ret = g_strdup (proxy->priv->name_owner);
2343 G_UNLOCK (properties_lock);
2344 return ret;
2348 * g_dbus_proxy_get_object_path:
2349 * @proxy: A #GDBusProxy.
2351 * Gets the object path @proxy is for.
2353 * Returns: A string owned by @proxy. Do not free.
2355 * Since: 2.26
2357 const gchar *
2358 g_dbus_proxy_get_object_path (GDBusProxy *proxy)
2360 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2361 return proxy->priv->object_path;
2365 * g_dbus_proxy_get_interface_name:
2366 * @proxy: A #GDBusProxy.
2368 * Gets the D-Bus interface name @proxy is for.
2370 * Returns: A string owned by @proxy. Do not free.
2372 * Since: 2.26
2374 const gchar *
2375 g_dbus_proxy_get_interface_name (GDBusProxy *proxy)
2377 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2378 return proxy->priv->interface_name;
2382 * g_dbus_proxy_get_default_timeout:
2383 * @proxy: A #GDBusProxy.
2385 * Gets the timeout to use if -1 (specifying default timeout) is
2386 * passed as @timeout_msec in the g_dbus_proxy_call() and
2387 * g_dbus_proxy_call_sync() functions.
2389 * See the #GDBusProxy:g-default-timeout property for more details.
2391 * Returns: Timeout to use for @proxy.
2393 * Since: 2.26
2395 gint
2396 g_dbus_proxy_get_default_timeout (GDBusProxy *proxy)
2398 gint ret;
2400 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), -1);
2402 G_LOCK (properties_lock);
2403 ret = proxy->priv->timeout_msec;
2404 G_UNLOCK (properties_lock);
2405 return ret;
2409 * g_dbus_proxy_set_default_timeout:
2410 * @proxy: A #GDBusProxy.
2411 * @timeout_msec: Timeout in milliseconds.
2413 * Sets the timeout to use if -1 (specifying default timeout) is
2414 * passed as @timeout_msec in the g_dbus_proxy_call() and
2415 * g_dbus_proxy_call_sync() functions.
2417 * See the #GDBusProxy:g-default-timeout property for more details.
2419 * Since: 2.26
2421 void
2422 g_dbus_proxy_set_default_timeout (GDBusProxy *proxy,
2423 gint timeout_msec)
2425 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
2426 g_return_if_fail (timeout_msec == -1 || timeout_msec >= 0);
2428 G_LOCK (properties_lock);
2430 if (proxy->priv->timeout_msec != timeout_msec)
2432 proxy->priv->timeout_msec = timeout_msec;
2433 G_UNLOCK (properties_lock);
2435 g_object_notify (G_OBJECT (proxy), "g-default-timeout");
2437 else
2439 G_UNLOCK (properties_lock);
2444 * g_dbus_proxy_get_interface_info:
2445 * @proxy: A #GDBusProxy
2447 * Returns the #GDBusInterfaceInfo, if any, specifying the interface
2448 * that @proxy conforms to. See the #GDBusProxy:g-interface-info
2449 * property for more details.
2451 * Returns: A #GDBusInterfaceInfo or %NULL. Do not unref the returned
2452 * object, it is owned by @proxy.
2454 * Since: 2.26
2456 GDBusInterfaceInfo *
2457 g_dbus_proxy_get_interface_info (GDBusProxy *proxy)
2459 GDBusInterfaceInfo *ret;
2461 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2463 G_LOCK (properties_lock);
2464 ret = proxy->priv->expected_interface;
2465 G_UNLOCK (properties_lock);
2466 /* FIXME: returning a borrowed ref with no guarantee that nobody will
2467 * call g_dbus_proxy_set_interface_info() and make it invalid...
2469 return ret;
2473 * g_dbus_proxy_set_interface_info:
2474 * @proxy: A #GDBusProxy
2475 * @info: (nullable): Minimum interface this proxy conforms to or %NULL to unset.
2477 * Ensure that interactions with @proxy conform to the given
2478 * interface. See the #GDBusProxy:g-interface-info property for more
2479 * details.
2481 * Since: 2.26
2483 void
2484 g_dbus_proxy_set_interface_info (GDBusProxy *proxy,
2485 GDBusInterfaceInfo *info)
2487 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
2488 G_LOCK (properties_lock);
2490 if (proxy->priv->expected_interface != NULL)
2492 g_dbus_interface_info_cache_release (proxy->priv->expected_interface);
2493 g_dbus_interface_info_unref (proxy->priv->expected_interface);
2495 proxy->priv->expected_interface = info != NULL ? g_dbus_interface_info_ref (info) : NULL;
2496 if (proxy->priv->expected_interface != NULL)
2497 g_dbus_interface_info_cache_build (proxy->priv->expected_interface);
2499 G_UNLOCK (properties_lock);
2502 /* ---------------------------------------------------------------------------------------------------- */
2504 static gboolean
2505 maybe_split_method_name (const gchar *method_name,
2506 gchar **out_interface_name,
2507 const gchar **out_method_name)
2509 gboolean was_split;
2511 was_split = FALSE;
2512 g_assert (out_interface_name != NULL);
2513 g_assert (out_method_name != NULL);
2514 *out_interface_name = NULL;
2515 *out_method_name = NULL;
2517 if (strchr (method_name, '.') != NULL)
2519 gchar *p;
2520 gchar *last_dot;
2522 p = g_strdup (method_name);
2523 last_dot = strrchr (p, '.');
2524 *last_dot = '\0';
2526 *out_interface_name = p;
2527 *out_method_name = last_dot + 1;
2529 was_split = TRUE;
2532 return was_split;
2535 typedef struct
2537 GVariant *value;
2538 #ifdef G_OS_UNIX
2539 GUnixFDList *fd_list;
2540 #endif
2541 } ReplyData;
2543 static void
2544 reply_data_free (ReplyData *data)
2546 g_variant_unref (data->value);
2547 #ifdef G_OS_UNIX
2548 if (data->fd_list != NULL)
2549 g_object_unref (data->fd_list);
2550 #endif
2551 g_slice_free (ReplyData, data);
2554 static void
2555 reply_cb (GDBusConnection *connection,
2556 GAsyncResult *res,
2557 gpointer user_data)
2559 GTask *task = user_data;
2560 GVariant *value;
2561 GError *error;
2562 #ifdef G_OS_UNIX
2563 GUnixFDList *fd_list;
2564 #endif
2566 error = NULL;
2567 #ifdef G_OS_UNIX
2568 value = g_dbus_connection_call_with_unix_fd_list_finish (connection,
2569 &fd_list,
2570 res,
2571 &error);
2572 #else
2573 value = g_dbus_connection_call_finish (connection,
2574 res,
2575 &error);
2576 #endif
2577 if (error != NULL)
2579 g_task_return_error (task, error);
2581 else
2583 ReplyData *data;
2584 data = g_slice_new0 (ReplyData);
2585 data->value = value;
2586 #ifdef G_OS_UNIX
2587 data->fd_list = fd_list;
2588 #endif
2589 g_task_return_pointer (task, data, (GDestroyNotify) reply_data_free);
2592 g_object_unref (task);
2595 /* properties_lock must be held for as long as you will keep the
2596 * returned value
2598 static const GDBusMethodInfo *
2599 lookup_method_info (GDBusProxy *proxy,
2600 const gchar *method_name)
2602 const GDBusMethodInfo *info = NULL;
2604 if (proxy->priv->expected_interface == NULL)
2605 goto out;
2607 info = g_dbus_interface_info_lookup_method (proxy->priv->expected_interface, method_name);
2609 out:
2610 return info;
2613 /* properties_lock must be held for as long as you will keep the
2614 * returned value
2616 static const gchar *
2617 get_destination_for_call (GDBusProxy *proxy)
2619 const gchar *ret;
2621 ret = NULL;
2623 /* If proxy->priv->name is a unique name, then proxy->priv->name_owner
2624 * is never NULL and always the same as proxy->priv->name. We use this
2625 * knowledge to avoid checking if proxy->priv->name is a unique or
2626 * well-known name.
2628 ret = proxy->priv->name_owner;
2629 if (ret != NULL)
2630 goto out;
2632 if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START)
2633 goto out;
2635 ret = proxy->priv->name;
2637 out:
2638 return ret;
2641 /* ---------------------------------------------------------------------------------------------------- */
2643 static void
2644 g_dbus_proxy_call_internal (GDBusProxy *proxy,
2645 const gchar *method_name,
2646 GVariant *parameters,
2647 GDBusCallFlags flags,
2648 gint timeout_msec,
2649 GUnixFDList *fd_list,
2650 GCancellable *cancellable,
2651 GAsyncReadyCallback callback,
2652 gpointer user_data)
2654 GTask *task;
2655 gboolean was_split;
2656 gchar *split_interface_name;
2657 const gchar *split_method_name;
2658 const gchar *target_method_name;
2659 const gchar *target_interface_name;
2660 gchar *destination;
2661 GVariantType *reply_type;
2662 GAsyncReadyCallback my_callback;
2664 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
2665 g_return_if_fail (g_dbus_is_member_name (method_name) || g_dbus_is_interface_name (method_name));
2666 g_return_if_fail (parameters == NULL || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE));
2667 g_return_if_fail (timeout_msec == -1 || timeout_msec >= 0);
2668 #ifdef G_OS_UNIX
2669 g_return_if_fail (fd_list == NULL || G_IS_UNIX_FD_LIST (fd_list));
2670 #else
2671 g_return_if_fail (fd_list == NULL);
2672 #endif
2674 reply_type = NULL;
2675 split_interface_name = NULL;
2677 /* g_dbus_connection_call() is optimised for the case of a NULL
2678 * callback. If we get a NULL callback from our user then make sure
2679 * we pass along a NULL callback for ourselves as well.
2681 if (callback != NULL)
2683 my_callback = (GAsyncReadyCallback) reply_cb;
2684 task = g_task_new (proxy, cancellable, callback, user_data);
2685 g_task_set_source_tag (task, g_dbus_proxy_call_internal);
2687 else
2689 my_callback = NULL;
2690 task = NULL;
2693 G_LOCK (properties_lock);
2695 was_split = maybe_split_method_name (method_name, &split_interface_name, &split_method_name);
2696 target_method_name = was_split ? split_method_name : method_name;
2697 target_interface_name = was_split ? split_interface_name : proxy->priv->interface_name;
2699 /* Warn if method is unexpected (cf. :g-interface-info) */
2700 if (!was_split)
2702 const GDBusMethodInfo *expected_method_info;
2703 expected_method_info = lookup_method_info (proxy, target_method_name);
2704 if (expected_method_info != NULL)
2705 reply_type = _g_dbus_compute_complete_signature (expected_method_info->out_args);
2708 destination = NULL;
2709 if (proxy->priv->name != NULL)
2711 destination = g_strdup (get_destination_for_call (proxy));
2712 if (destination == NULL)
2714 if (task != NULL)
2716 g_task_return_new_error (task,
2717 G_IO_ERROR,
2718 G_IO_ERROR_FAILED,
2719 _("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"));
2720 g_object_unref (task);
2722 G_UNLOCK (properties_lock);
2723 goto out;
2727 G_UNLOCK (properties_lock);
2729 #ifdef G_OS_UNIX
2730 g_dbus_connection_call_with_unix_fd_list (proxy->priv->connection,
2731 destination,
2732 proxy->priv->object_path,
2733 target_interface_name,
2734 target_method_name,
2735 parameters,
2736 reply_type,
2737 flags,
2738 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2739 fd_list,
2740 cancellable,
2741 my_callback,
2742 task);
2743 #else
2744 g_dbus_connection_call (proxy->priv->connection,
2745 destination,
2746 proxy->priv->object_path,
2747 target_interface_name,
2748 target_method_name,
2749 parameters,
2750 reply_type,
2751 flags,
2752 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2753 cancellable,
2754 my_callback,
2755 task);
2756 #endif
2758 out:
2759 if (reply_type != NULL)
2760 g_variant_type_free (reply_type);
2762 g_free (destination);
2763 g_free (split_interface_name);
2766 static GVariant *
2767 g_dbus_proxy_call_finish_internal (GDBusProxy *proxy,
2768 GUnixFDList **out_fd_list,
2769 GAsyncResult *res,
2770 GError **error)
2772 GVariant *value;
2773 ReplyData *data;
2775 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2776 g_return_val_if_fail (g_task_is_valid (res, proxy), NULL);
2777 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2779 value = NULL;
2781 data = g_task_propagate_pointer (G_TASK (res), error);
2782 if (!data)
2783 goto out;
2785 value = g_variant_ref (data->value);
2786 #ifdef G_OS_UNIX
2787 if (out_fd_list != NULL)
2788 *out_fd_list = data->fd_list != NULL ? g_object_ref (data->fd_list) : NULL;
2789 #endif
2790 reply_data_free (data);
2792 out:
2793 return value;
2796 static GVariant *
2797 g_dbus_proxy_call_sync_internal (GDBusProxy *proxy,
2798 const gchar *method_name,
2799 GVariant *parameters,
2800 GDBusCallFlags flags,
2801 gint timeout_msec,
2802 GUnixFDList *fd_list,
2803 GUnixFDList **out_fd_list,
2804 GCancellable *cancellable,
2805 GError **error)
2807 GVariant *ret;
2808 gboolean was_split;
2809 gchar *split_interface_name;
2810 const gchar *split_method_name;
2811 const gchar *target_method_name;
2812 const gchar *target_interface_name;
2813 gchar *destination;
2814 GVariantType *reply_type;
2816 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2817 g_return_val_if_fail (g_dbus_is_member_name (method_name) || g_dbus_is_interface_name (method_name), NULL);
2818 g_return_val_if_fail (parameters == NULL || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE), NULL);
2819 g_return_val_if_fail (timeout_msec == -1 || timeout_msec >= 0, NULL);
2820 #ifdef G_OS_UNIX
2821 g_return_val_if_fail (fd_list == NULL || G_IS_UNIX_FD_LIST (fd_list), NULL);
2822 #else
2823 g_return_val_if_fail (fd_list == NULL, NULL);
2824 #endif
2825 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2827 reply_type = NULL;
2829 G_LOCK (properties_lock);
2831 was_split = maybe_split_method_name (method_name, &split_interface_name, &split_method_name);
2832 target_method_name = was_split ? split_method_name : method_name;
2833 target_interface_name = was_split ? split_interface_name : proxy->priv->interface_name;
2835 /* Warn if method is unexpected (cf. :g-interface-info) */
2836 if (!was_split)
2838 const GDBusMethodInfo *expected_method_info;
2839 expected_method_info = lookup_method_info (proxy, target_method_name);
2840 if (expected_method_info != NULL)
2841 reply_type = _g_dbus_compute_complete_signature (expected_method_info->out_args);
2844 destination = NULL;
2845 if (proxy->priv->name != NULL)
2847 destination = g_strdup (get_destination_for_call (proxy));
2848 if (destination == NULL)
2850 g_set_error_literal (error,
2851 G_IO_ERROR,
2852 G_IO_ERROR_FAILED,
2853 _("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"));
2854 ret = NULL;
2855 G_UNLOCK (properties_lock);
2856 goto out;
2860 G_UNLOCK (properties_lock);
2862 #ifdef G_OS_UNIX
2863 ret = g_dbus_connection_call_with_unix_fd_list_sync (proxy->priv->connection,
2864 destination,
2865 proxy->priv->object_path,
2866 target_interface_name,
2867 target_method_name,
2868 parameters,
2869 reply_type,
2870 flags,
2871 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2872 fd_list,
2873 out_fd_list,
2874 cancellable,
2875 error);
2876 #else
2877 ret = g_dbus_connection_call_sync (proxy->priv->connection,
2878 destination,
2879 proxy->priv->object_path,
2880 target_interface_name,
2881 target_method_name,
2882 parameters,
2883 reply_type,
2884 flags,
2885 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2886 cancellable,
2887 error);
2888 #endif
2890 out:
2891 if (reply_type != NULL)
2892 g_variant_type_free (reply_type);
2894 g_free (destination);
2895 g_free (split_interface_name);
2897 return ret;
2900 /* ---------------------------------------------------------------------------------------------------- */
2903 * g_dbus_proxy_call:
2904 * @proxy: A #GDBusProxy.
2905 * @method_name: Name of method to invoke.
2906 * @parameters: (nullable): A #GVariant tuple with parameters for the signal or %NULL if not passing parameters.
2907 * @flags: Flags from the #GDBusCallFlags enumeration.
2908 * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
2909 * "infinite") or -1 to use the proxy default timeout.
2910 * @cancellable: (nullable): A #GCancellable or %NULL.
2911 * @callback: (nullable): A #GAsyncReadyCallback to call when the request is satisfied or %NULL if you don't
2912 * care about the result of the method invocation.
2913 * @user_data: The data to pass to @callback.
2915 * Asynchronously invokes the @method_name method on @proxy.
2917 * If @method_name contains any dots, then @name is split into interface and
2918 * method name parts. This allows using @proxy for invoking methods on
2919 * other interfaces.
2921 * If the #GDBusConnection associated with @proxy is closed then
2922 * the operation will fail with %G_IO_ERROR_CLOSED. If
2923 * @cancellable is canceled, the operation will fail with
2924 * %G_IO_ERROR_CANCELLED. If @parameters contains a value not
2925 * compatible with the D-Bus protocol, the operation fails with
2926 * %G_IO_ERROR_INVALID_ARGUMENT.
2928 * If the @parameters #GVariant is floating, it is consumed. This allows
2929 * convenient 'inline' use of g_variant_new(), e.g.:
2930 * |[<!-- language="C" -->
2931 * g_dbus_proxy_call (proxy,
2932 * "TwoStrings",
2933 * g_variant_new ("(ss)",
2934 * "Thing One",
2935 * "Thing Two"),
2936 * G_DBUS_CALL_FLAGS_NONE,
2937 * -1,
2938 * NULL,
2939 * (GAsyncReadyCallback) two_strings_done,
2940 * &data);
2941 * ]|
2943 * If @proxy has an expected interface (see
2944 * #GDBusProxy:g-interface-info) and @method_name is referenced by it,
2945 * then the return value is checked against the return type.
2947 * This is an asynchronous method. When the operation is finished,
2948 * @callback will be invoked in the
2949 * [thread-default main context][g-main-context-push-thread-default]
2950 * of the thread you are calling this method from.
2951 * You can then call g_dbus_proxy_call_finish() to get the result of
2952 * the operation. See g_dbus_proxy_call_sync() for the synchronous
2953 * version of this method.
2955 * If @callback is %NULL then the D-Bus method call message will be sent with
2956 * the %G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED flag set.
2958 * Since: 2.26
2960 void
2961 g_dbus_proxy_call (GDBusProxy *proxy,
2962 const gchar *method_name,
2963 GVariant *parameters,
2964 GDBusCallFlags flags,
2965 gint timeout_msec,
2966 GCancellable *cancellable,
2967 GAsyncReadyCallback callback,
2968 gpointer user_data)
2970 g_dbus_proxy_call_internal (proxy, method_name, parameters, flags, timeout_msec, NULL, cancellable, callback, user_data);
2974 * g_dbus_proxy_call_finish:
2975 * @proxy: A #GDBusProxy.
2976 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_proxy_call().
2977 * @error: Return location for error or %NULL.
2979 * Finishes an operation started with g_dbus_proxy_call().
2981 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
2982 * return values. Free with g_variant_unref().
2984 * Since: 2.26
2986 GVariant *
2987 g_dbus_proxy_call_finish (GDBusProxy *proxy,
2988 GAsyncResult *res,
2989 GError **error)
2991 return g_dbus_proxy_call_finish_internal (proxy, NULL, res, error);
2995 * g_dbus_proxy_call_sync:
2996 * @proxy: A #GDBusProxy.
2997 * @method_name: Name of method to invoke.
2998 * @parameters: (nullable): A #GVariant tuple with parameters for the signal
2999 * or %NULL if not passing parameters.
3000 * @flags: Flags from the #GDBusCallFlags enumeration.
3001 * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
3002 * "infinite") or -1 to use the proxy default timeout.
3003 * @cancellable: (nullable): A #GCancellable or %NULL.
3004 * @error: Return location for error or %NULL.
3006 * Synchronously invokes the @method_name method on @proxy.
3008 * If @method_name contains any dots, then @name is split into interface and
3009 * method name parts. This allows using @proxy for invoking methods on
3010 * other interfaces.
3012 * If the #GDBusConnection associated with @proxy is disconnected then
3013 * the operation will fail with %G_IO_ERROR_CLOSED. If
3014 * @cancellable is canceled, the operation will fail with
3015 * %G_IO_ERROR_CANCELLED. If @parameters contains a value not
3016 * compatible with the D-Bus protocol, the operation fails with
3017 * %G_IO_ERROR_INVALID_ARGUMENT.
3019 * If the @parameters #GVariant is floating, it is consumed. This allows
3020 * convenient 'inline' use of g_variant_new(), e.g.:
3021 * |[<!-- language="C" -->
3022 * g_dbus_proxy_call_sync (proxy,
3023 * "TwoStrings",
3024 * g_variant_new ("(ss)",
3025 * "Thing One",
3026 * "Thing Two"),
3027 * G_DBUS_CALL_FLAGS_NONE,
3028 * -1,
3029 * NULL,
3030 * &error);
3031 * ]|
3033 * The calling thread is blocked until a reply is received. See
3034 * g_dbus_proxy_call() for the asynchronous version of this
3035 * method.
3037 * If @proxy has an expected interface (see
3038 * #GDBusProxy:g-interface-info) and @method_name is referenced by it,
3039 * then the return value is checked against the return type.
3041 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
3042 * return values. Free with g_variant_unref().
3044 * Since: 2.26
3046 GVariant *
3047 g_dbus_proxy_call_sync (GDBusProxy *proxy,
3048 const gchar *method_name,
3049 GVariant *parameters,
3050 GDBusCallFlags flags,
3051 gint timeout_msec,
3052 GCancellable *cancellable,
3053 GError **error)
3055 return g_dbus_proxy_call_sync_internal (proxy, method_name, parameters, flags, timeout_msec, NULL, NULL, cancellable, error);
3058 /* ---------------------------------------------------------------------------------------------------- */
3060 #ifdef G_OS_UNIX
3063 * g_dbus_proxy_call_with_unix_fd_list:
3064 * @proxy: A #GDBusProxy.
3065 * @method_name: Name of method to invoke.
3066 * @parameters: (nullable): A #GVariant tuple with parameters for the signal or %NULL if not passing parameters.
3067 * @flags: Flags from the #GDBusCallFlags enumeration.
3068 * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
3069 * "infinite") or -1 to use the proxy default timeout.
3070 * @fd_list: (nullable): A #GUnixFDList or %NULL.
3071 * @cancellable: (nullable): A #GCancellable or %NULL.
3072 * @callback: (nullable): A #GAsyncReadyCallback to call when the request is satisfied or %NULL if you don't
3073 * care about the result of the method invocation.
3074 * @user_data: The data to pass to @callback.
3076 * Like g_dbus_proxy_call() but also takes a #GUnixFDList object.
3078 * This method is only available on UNIX.
3080 * Since: 2.30
3082 void
3083 g_dbus_proxy_call_with_unix_fd_list (GDBusProxy *proxy,
3084 const gchar *method_name,
3085 GVariant *parameters,
3086 GDBusCallFlags flags,
3087 gint timeout_msec,
3088 GUnixFDList *fd_list,
3089 GCancellable *cancellable,
3090 GAsyncReadyCallback callback,
3091 gpointer user_data)
3093 g_dbus_proxy_call_internal (proxy, method_name, parameters, flags, timeout_msec, fd_list, cancellable, callback, user_data);
3097 * g_dbus_proxy_call_with_unix_fd_list_finish:
3098 * @proxy: A #GDBusProxy.
3099 * @out_fd_list: (out) (optional): Return location for a #GUnixFDList or %NULL.
3100 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_proxy_call_with_unix_fd_list().
3101 * @error: Return location for error or %NULL.
3103 * Finishes an operation started with g_dbus_proxy_call_with_unix_fd_list().
3105 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
3106 * return values. Free with g_variant_unref().
3108 * Since: 2.30
3110 GVariant *
3111 g_dbus_proxy_call_with_unix_fd_list_finish (GDBusProxy *proxy,
3112 GUnixFDList **out_fd_list,
3113 GAsyncResult *res,
3114 GError **error)
3116 return g_dbus_proxy_call_finish_internal (proxy, out_fd_list, res, error);
3120 * g_dbus_proxy_call_with_unix_fd_list_sync:
3121 * @proxy: A #GDBusProxy.
3122 * @method_name: Name of method to invoke.
3123 * @parameters: (nullable): A #GVariant tuple with parameters for the signal
3124 * or %NULL if not passing parameters.
3125 * @flags: Flags from the #GDBusCallFlags enumeration.
3126 * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
3127 * "infinite") or -1 to use the proxy default timeout.
3128 * @fd_list: (nullable): A #GUnixFDList or %NULL.
3129 * @out_fd_list: (out) (optional): Return location for a #GUnixFDList or %NULL.
3130 * @cancellable: (nullable): A #GCancellable or %NULL.
3131 * @error: Return location for error or %NULL.
3133 * Like g_dbus_proxy_call_sync() but also takes and returns #GUnixFDList objects.
3135 * This method is only available on UNIX.
3137 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
3138 * return values. Free with g_variant_unref().
3140 * Since: 2.30
3142 GVariant *
3143 g_dbus_proxy_call_with_unix_fd_list_sync (GDBusProxy *proxy,
3144 const gchar *method_name,
3145 GVariant *parameters,
3146 GDBusCallFlags flags,
3147 gint timeout_msec,
3148 GUnixFDList *fd_list,
3149 GUnixFDList **out_fd_list,
3150 GCancellable *cancellable,
3151 GError **error)
3153 return g_dbus_proxy_call_sync_internal (proxy, method_name, parameters, flags, timeout_msec, fd_list, out_fd_list, cancellable, error);
3156 #endif /* G_OS_UNIX */
3158 /* ---------------------------------------------------------------------------------------------------- */
3160 static GDBusInterfaceInfo *
3161 _g_dbus_proxy_get_info (GDBusInterface *interface)
3163 GDBusProxy *proxy = G_DBUS_PROXY (interface);
3164 return g_dbus_proxy_get_interface_info (proxy);
3167 static GDBusObject *
3168 _g_dbus_proxy_get_object (GDBusInterface *interface)
3170 GDBusProxy *proxy = G_DBUS_PROXY (interface);
3171 return proxy->priv->object;
3174 static GDBusObject *
3175 _g_dbus_proxy_dup_object (GDBusInterface *interface)
3177 GDBusProxy *proxy = G_DBUS_PROXY (interface);
3178 GDBusObject *ret = NULL;
3180 G_LOCK (properties_lock);
3181 if (proxy->priv->object != NULL)
3182 ret = g_object_ref (proxy->priv->object);
3183 G_UNLOCK (properties_lock);
3184 return ret;
3187 static void
3188 _g_dbus_proxy_set_object (GDBusInterface *interface,
3189 GDBusObject *object)
3191 GDBusProxy *proxy = G_DBUS_PROXY (interface);
3192 G_LOCK (properties_lock);
3193 if (proxy->priv->object != NULL)
3194 g_object_remove_weak_pointer (G_OBJECT (proxy->priv->object), (gpointer *) &proxy->priv->object);
3195 proxy->priv->object = object;
3196 if (proxy->priv->object != NULL)
3197 g_object_add_weak_pointer (G_OBJECT (proxy->priv->object), (gpointer *) &proxy->priv->object);
3198 G_UNLOCK (properties_lock);
3201 static void
3202 dbus_interface_iface_init (GDBusInterfaceIface *dbus_interface_iface)
3204 dbus_interface_iface->get_info = _g_dbus_proxy_get_info;
3205 dbus_interface_iface->get_object = _g_dbus_proxy_get_object;
3206 dbus_interface_iface->dup_object = _g_dbus_proxy_dup_object;
3207 dbus_interface_iface->set_object = _g_dbus_proxy_set_object;
3210 /* ---------------------------------------------------------------------------------------------------- */