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>
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"
35 #include "gasyncresult.h"
36 #include "gsimpleasyncresult.h"
37 #include "gcancellable.h"
38 #include "gdbusinterface.h"
41 #include "gunixfdlist.h"
48 * @short_description: Client-side D-Bus interface proxy
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 * <example id="gdbus-wellknown-proxy"><title>GDBusProxy for a well-known-name</title><programlisting><xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/tests/gdbus-example-watch-proxy.c"><xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback></xi:include></programlisting></example>
90 /* lock protecting the mutable properties: name_owner, timeout_msec,
91 * expected_interface, and the properties hash table
93 G_LOCK_DEFINE_STATIC (properties_lock);
95 /* ---------------------------------------------------------------------------------------------------- */
97 G_LOCK_DEFINE_STATIC (signal_subscription_lock);
101 volatile gint ref_count;
103 } SignalSubscriptionData;
105 static SignalSubscriptionData *
106 signal_subscription_ref (SignalSubscriptionData *data)
108 g_atomic_int_inc (&data->ref_count);
113 signal_subscription_unref (SignalSubscriptionData *data)
115 if (g_atomic_int_dec_and_test (&data->ref_count))
117 g_slice_free (SignalSubscriptionData, data);
121 /* ---------------------------------------------------------------------------------------------------- */
123 struct _GDBusProxyPrivate
126 GDBusProxyFlags flags;
127 GDBusConnection *connection;
130 /* mutable, protected by properties_lock */
133 gchar *interface_name;
134 /* mutable, protected by properties_lock */
137 guint name_owner_changed_subscription_id;
139 GCancellable *get_all_cancellable;
141 /* gchar* -> GVariant*, protected by properties_lock */
142 GHashTable *properties;
144 /* mutable, protected by properties_lock */
145 GDBusInterfaceInfo *expected_interface;
147 guint properties_changed_subscription_id;
148 guint signals_subscription_id;
150 gboolean initialized;
152 /* mutable, protected by properties_lock */
155 SignalSubscriptionData *signal_subscription_data;
167 PROP_G_INTERFACE_NAME,
168 PROP_G_DEFAULT_TIMEOUT,
169 PROP_G_INTERFACE_INFO
174 PROPERTIES_CHANGED_SIGNAL,
179 static guint signals[LAST_SIGNAL] = {0};
181 static void dbus_interface_iface_init (GDBusInterfaceIface *dbus_interface_iface);
182 static void initable_iface_init (GInitableIface *initable_iface);
183 static void async_initable_iface_init (GAsyncInitableIface *async_initable_iface);
185 G_DEFINE_TYPE_WITH_CODE (GDBusProxy, g_dbus_proxy, G_TYPE_OBJECT,
186 G_ADD_PRIVATE (GDBusProxy)
187 G_IMPLEMENT_INTERFACE (G_TYPE_DBUS_INTERFACE, dbus_interface_iface_init)
188 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, initable_iface_init)
189 G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE, async_initable_iface_init))
192 g_dbus_proxy_dispose (GObject *object)
194 GDBusProxy *proxy = G_DBUS_PROXY (object);
195 G_LOCK (signal_subscription_lock);
196 if (proxy->priv->signal_subscription_data != NULL)
198 proxy->priv->signal_subscription_data->proxy = NULL;
199 signal_subscription_unref (proxy->priv->signal_subscription_data);
200 proxy->priv->signal_subscription_data = NULL;
202 G_UNLOCK (signal_subscription_lock);
204 G_OBJECT_CLASS (g_dbus_proxy_parent_class)->dispose (object);
208 g_dbus_proxy_finalize (GObject *object)
210 GDBusProxy *proxy = G_DBUS_PROXY (object);
212 g_warn_if_fail (proxy->priv->get_all_cancellable == NULL);
214 if (proxy->priv->name_owner_changed_subscription_id > 0)
215 g_dbus_connection_signal_unsubscribe (proxy->priv->connection,
216 proxy->priv->name_owner_changed_subscription_id);
218 if (proxy->priv->properties_changed_subscription_id > 0)
219 g_dbus_connection_signal_unsubscribe (proxy->priv->connection,
220 proxy->priv->properties_changed_subscription_id);
222 if (proxy->priv->signals_subscription_id > 0)
223 g_dbus_connection_signal_unsubscribe (proxy->priv->connection,
224 proxy->priv->signals_subscription_id);
226 if (proxy->priv->connection != NULL)
227 g_object_unref (proxy->priv->connection);
228 g_free (proxy->priv->name);
229 g_free (proxy->priv->name_owner);
230 g_free (proxy->priv->object_path);
231 g_free (proxy->priv->interface_name);
232 if (proxy->priv->properties != NULL)
233 g_hash_table_unref (proxy->priv->properties);
235 if (proxy->priv->expected_interface != NULL)
237 g_dbus_interface_info_cache_release (proxy->priv->expected_interface);
238 g_dbus_interface_info_unref (proxy->priv->expected_interface);
241 if (proxy->priv->object != NULL)
242 g_object_remove_weak_pointer (G_OBJECT (proxy->priv->object), (gpointer *) &proxy->priv->object);
244 G_OBJECT_CLASS (g_dbus_proxy_parent_class)->finalize (object);
248 g_dbus_proxy_get_property (GObject *object,
253 GDBusProxy *proxy = G_DBUS_PROXY (object);
257 case PROP_G_CONNECTION:
258 g_value_set_object (value, proxy->priv->connection);
262 g_value_set_flags (value, proxy->priv->flags);
266 g_value_set_string (value, proxy->priv->name);
269 case PROP_G_NAME_OWNER:
270 g_value_take_string (value, g_dbus_proxy_get_name_owner (proxy));
273 case PROP_G_OBJECT_PATH:
274 g_value_set_string (value, proxy->priv->object_path);
277 case PROP_G_INTERFACE_NAME:
278 g_value_set_string (value, proxy->priv->interface_name);
281 case PROP_G_DEFAULT_TIMEOUT:
282 g_value_set_int (value, g_dbus_proxy_get_default_timeout (proxy));
285 case PROP_G_INTERFACE_INFO:
286 g_value_set_boxed (value, g_dbus_proxy_get_interface_info (proxy));
290 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
296 g_dbus_proxy_set_property (GObject *object,
301 GDBusProxy *proxy = G_DBUS_PROXY (object);
305 case PROP_G_CONNECTION:
306 proxy->priv->connection = g_value_dup_object (value);
310 proxy->priv->flags = g_value_get_flags (value);
314 proxy->priv->name = g_value_dup_string (value);
317 case PROP_G_OBJECT_PATH:
318 proxy->priv->object_path = g_value_dup_string (value);
321 case PROP_G_INTERFACE_NAME:
322 proxy->priv->interface_name = g_value_dup_string (value);
325 case PROP_G_DEFAULT_TIMEOUT:
326 g_dbus_proxy_set_default_timeout (proxy, g_value_get_int (value));
329 case PROP_G_INTERFACE_INFO:
330 g_dbus_proxy_set_interface_info (proxy, g_value_get_boxed (value));
333 case PROP_G_BUS_TYPE:
334 proxy->priv->bus_type = g_value_get_enum (value);
338 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
344 g_dbus_proxy_class_init (GDBusProxyClass *klass)
346 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
348 gobject_class->dispose = g_dbus_proxy_dispose;
349 gobject_class->finalize = g_dbus_proxy_finalize;
350 gobject_class->set_property = g_dbus_proxy_set_property;
351 gobject_class->get_property = g_dbus_proxy_get_property;
353 /* Note that all property names are prefixed to avoid collisions with D-Bus property names
354 * in derived classes */
357 * GDBusProxy:g-interface-info:
359 * Ensure that interactions with this proxy conform to the given
360 * interface. This is mainly to ensure that malformed data received
361 * from the other peer is ignored. The given #GDBusInterfaceInfo is
362 * said to be the "expected interface".
364 * The checks performed are:
365 * - When completing a method call, if the type signature of
366 * the reply message isn't what's expected, the reply is
367 * discarded and the #GError is set to %G_IO_ERROR_INVALID_ARGUMENT.
369 * - Received signals that have a type signature mismatch are dropped and
370 * a warning is logged via g_warning().
372 * - Properties received via the initial `GetAll()` call or via the
373 * `::PropertiesChanged` signal (on the
374 * [org.freedesktop.DBus.Properties](http://dbus.freedesktop.org/doc/dbus-specification.html#standard-interfaces-properties)
375 * interface) or set using g_dbus_proxy_set_cached_property()
376 * with a type signature mismatch are ignored and a warning is
377 * logged via g_warning().
379 * Note that these checks are never done on methods, signals and
380 * properties that are not referenced in the given
381 * #GDBusInterfaceInfo, since extending a D-Bus interface on the
382 * service-side is not considered an ABI break.
386 g_object_class_install_property (gobject_class,
387 PROP_G_INTERFACE_INFO,
388 g_param_spec_boxed ("g-interface-info",
389 P_("Interface Information"),
390 P_("Interface Information"),
391 G_TYPE_DBUS_INTERFACE_INFO,
394 G_PARAM_STATIC_NAME |
395 G_PARAM_STATIC_BLURB |
396 G_PARAM_STATIC_NICK));
399 * GDBusProxy:g-connection:
401 * The #GDBusConnection the proxy is for.
405 g_object_class_install_property (gobject_class,
407 g_param_spec_object ("g-connection",
409 P_("The connection the proxy is for"),
410 G_TYPE_DBUS_CONNECTION,
413 G_PARAM_CONSTRUCT_ONLY |
414 G_PARAM_STATIC_NAME |
415 G_PARAM_STATIC_BLURB |
416 G_PARAM_STATIC_NICK));
419 * GDBusProxy:g-bus-type:
421 * If this property is not %G_BUS_TYPE_NONE, then
422 * #GDBusProxy:g-connection must be %NULL and will be set to the
423 * #GDBusConnection obtained by calling g_bus_get() with the value
428 g_object_class_install_property (gobject_class,
430 g_param_spec_enum ("g-bus-type",
432 P_("The bus to connect to, if any"),
436 G_PARAM_CONSTRUCT_ONLY |
437 G_PARAM_STATIC_NAME |
438 G_PARAM_STATIC_BLURB |
439 G_PARAM_STATIC_NICK));
442 * GDBusProxy:g-flags:
444 * Flags from the #GDBusProxyFlags enumeration.
448 g_object_class_install_property (gobject_class,
450 g_param_spec_flags ("g-flags",
452 P_("Flags for the proxy"),
453 G_TYPE_DBUS_PROXY_FLAGS,
454 G_DBUS_PROXY_FLAGS_NONE,
457 G_PARAM_CONSTRUCT_ONLY |
458 G_PARAM_STATIC_NAME |
459 G_PARAM_STATIC_BLURB |
460 G_PARAM_STATIC_NICK));
465 * The well-known or unique name that the proxy is for.
469 g_object_class_install_property (gobject_class,
471 g_param_spec_string ("g-name",
473 P_("The well-known or unique name that the proxy is for"),
477 G_PARAM_CONSTRUCT_ONLY |
478 G_PARAM_STATIC_NAME |
479 G_PARAM_STATIC_BLURB |
480 G_PARAM_STATIC_NICK));
483 * GDBusProxy:g-name-owner:
485 * The unique name that owns #GDBusProxy:g-name or %NULL if no-one
486 * currently owns that name. You may connect to #GObject::notify signal to
487 * track changes to this property.
491 g_object_class_install_property (gobject_class,
493 g_param_spec_string ("g-name-owner",
495 P_("The unique name for the owner"),
498 G_PARAM_STATIC_NAME |
499 G_PARAM_STATIC_BLURB |
500 G_PARAM_STATIC_NICK));
503 * GDBusProxy:g-object-path:
505 * The object path the proxy is for.
509 g_object_class_install_property (gobject_class,
511 g_param_spec_string ("g-object-path",
513 P_("The object path the proxy is for"),
517 G_PARAM_CONSTRUCT_ONLY |
518 G_PARAM_STATIC_NAME |
519 G_PARAM_STATIC_BLURB |
520 G_PARAM_STATIC_NICK));
523 * GDBusProxy:g-interface-name:
525 * The D-Bus interface name the proxy is for.
529 g_object_class_install_property (gobject_class,
530 PROP_G_INTERFACE_NAME,
531 g_param_spec_string ("g-interface-name",
532 P_("g-interface-name"),
533 P_("The D-Bus interface name the proxy is for"),
537 G_PARAM_CONSTRUCT_ONLY |
538 G_PARAM_STATIC_NAME |
539 G_PARAM_STATIC_BLURB |
540 G_PARAM_STATIC_NICK));
543 * GDBusProxy:g-default-timeout:
545 * The timeout to use if -1 (specifying default timeout) is passed
546 * as @timeout_msec in the g_dbus_proxy_call() and
547 * g_dbus_proxy_call_sync() functions.
549 * This allows applications to set a proxy-wide timeout for all
550 * remote method invocations on the proxy. If this property is -1,
551 * the default timeout (typically 25 seconds) is used. If set to
552 * %G_MAXINT, then no timeout is used.
556 g_object_class_install_property (gobject_class,
557 PROP_G_DEFAULT_TIMEOUT,
558 g_param_spec_int ("g-default-timeout",
559 P_("Default Timeout"),
560 P_("Timeout for remote method invocation"),
567 G_PARAM_STATIC_NAME |
568 G_PARAM_STATIC_BLURB |
569 G_PARAM_STATIC_NICK));
572 * GDBusProxy::g-properties-changed:
573 * @proxy: The #GDBusProxy emitting the signal.
574 * @changed_properties: A #GVariant containing the properties that changed
575 * @invalidated_properties: A %NULL terminated array of properties that was invalidated
577 * Emitted when one or more D-Bus properties on @proxy changes. The
578 * local cache has already been updated when this signal fires. Note
579 * that both @changed_properties and @invalidated_properties are
580 * guaranteed to never be %NULL (either may be empty though).
582 * If the proxy has the flag
583 * %G_DBUS_PROXY_FLAGS_GET_INVALIDATED_PROPERTIES set, then
584 * @invalidated_properties will always be empty.
586 * This signal corresponds to the
587 * `PropertiesChanged` D-Bus signal on the
588 * `org.freedesktop.DBus.Properties` interface.
592 signals[PROPERTIES_CHANGED_SIGNAL] = g_signal_new ("g-properties-changed",
594 G_SIGNAL_RUN_LAST | G_SIGNAL_MUST_COLLECT,
595 G_STRUCT_OFFSET (GDBusProxyClass, g_properties_changed),
602 G_TYPE_STRV | G_SIGNAL_TYPE_STATIC_SCOPE);
605 * GDBusProxy::g-signal:
606 * @proxy: The #GDBusProxy emitting the signal.
607 * @sender_name: (allow-none): The sender of the signal or %NULL if the connection is not a bus connection.
608 * @signal_name: The name of the signal.
609 * @parameters: A #GVariant tuple with parameters for the signal.
611 * Emitted when a signal from the remote object and interface that @proxy is for, has been received.
615 signals[SIGNAL_SIGNAL] = g_signal_new ("g-signal",
617 G_SIGNAL_RUN_LAST | G_SIGNAL_MUST_COLLECT,
618 G_STRUCT_OFFSET (GDBusProxyClass, g_signal),
631 g_dbus_proxy_init (GDBusProxy *proxy)
633 proxy->priv = g_dbus_proxy_get_instance_private (proxy);
634 proxy->priv->signal_subscription_data = g_slice_new0 (SignalSubscriptionData);
635 proxy->priv->signal_subscription_data->ref_count = 1;
636 proxy->priv->signal_subscription_data->proxy = proxy;
637 proxy->priv->properties = g_hash_table_new_full (g_str_hash,
640 (GDestroyNotify) g_variant_unref);
643 /* ---------------------------------------------------------------------------------------------------- */
646 property_name_sort_func (const gchar **a,
649 return g_strcmp0 (*a, *b);
653 * g_dbus_proxy_get_cached_property_names:
654 * @proxy: A #GDBusProxy.
656 * Gets the names of all cached properties on @proxy.
658 * Returns: (transfer full): A %NULL-terminated array of strings or %NULL if
659 * @proxy has no cached properties. Free the returned array with
665 g_dbus_proxy_get_cached_property_names (GDBusProxy *proxy)
672 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
674 G_LOCK (properties_lock);
677 if (g_hash_table_size (proxy->priv->properties) == 0)
680 p = g_ptr_array_new ();
682 g_hash_table_iter_init (&iter, proxy->priv->properties);
683 while (g_hash_table_iter_next (&iter, (gpointer) &key, NULL))
684 g_ptr_array_add (p, g_strdup (key));
685 g_ptr_array_sort (p, (GCompareFunc) property_name_sort_func);
686 g_ptr_array_add (p, NULL);
688 names = (gchar **) g_ptr_array_free (p, FALSE);
691 G_UNLOCK (properties_lock);
695 /* properties_lock must be held for as long as you will keep the
698 static const GDBusPropertyInfo *
699 lookup_property_info (GDBusProxy *proxy,
700 const gchar *property_name)
702 const GDBusPropertyInfo *info = NULL;
704 if (proxy->priv->expected_interface == NULL)
707 info = g_dbus_interface_info_lookup_property (proxy->priv->expected_interface, property_name);
714 * g_dbus_proxy_get_cached_property:
715 * @proxy: A #GDBusProxy.
716 * @property_name: Property name.
718 * Looks up the value for a property from the cache. This call does no
721 * If @proxy has an expected interface (see
722 * #GDBusProxy:g-interface-info) and @property_name is referenced by
723 * it, then @value is checked against the type of the property.
725 * Returns: A reference to the #GVariant instance that holds the value
726 * for @property_name or %NULL if the value is not in the cache. The
727 * returned reference must be freed with g_variant_unref().
732 g_dbus_proxy_get_cached_property (GDBusProxy *proxy,
733 const gchar *property_name)
735 const GDBusPropertyInfo *info;
738 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
739 g_return_val_if_fail (property_name != NULL, NULL);
741 G_LOCK (properties_lock);
743 value = g_hash_table_lookup (proxy->priv->properties, property_name);
747 info = lookup_property_info (proxy, property_name);
750 const gchar *type_string = g_variant_get_type_string (value);
751 if (g_strcmp0 (type_string, info->signature) != 0)
753 g_warning ("Trying to get property %s with type %s but according to the expected "
754 "interface the type is %s",
763 g_variant_ref (value);
766 G_UNLOCK (properties_lock);
771 * g_dbus_proxy_set_cached_property:
772 * @proxy: A #GDBusProxy
773 * @property_name: Property name.
774 * @value: (allow-none): Value for the property or %NULL to remove it from the cache.
776 * If @value is not %NULL, sets the cached value for the property with
777 * name @property_name to the value in @value.
779 * If @value is %NULL, then the cached value is removed from the
782 * If @proxy has an expected interface (see
783 * #GDBusProxy:g-interface-info) and @property_name is referenced by
784 * it, then @value is checked against the type of the property.
786 * If the @value #GVariant is floating, it is consumed. This allows
787 * convenient 'inline' use of g_variant_new(), e.g.
788 * |[<!-- language="C" -->
789 * g_dbus_proxy_set_cached_property (proxy,
791 * g_variant_new ("(si)",
796 * Normally you will not need to use this method since @proxy
797 * is tracking changes using the
798 * `org.freedesktop.DBus.Properties.PropertiesChanged`
799 * D-Bus signal. However, for performance reasons an object may
800 * decide to not use this signal for some properties and instead
801 * use a proprietary out-of-band mechanism to transmit changes.
803 * As a concrete example, consider an object with a property
804 * `ChatroomParticipants` which is an array of strings. Instead of
805 * transmitting the same (long) array every time the property changes,
806 * it is more efficient to only transmit the delta using e.g. signals
807 * `ChatroomParticipantJoined(String name)` and
808 * `ChatroomParticipantParted(String name)`.
813 g_dbus_proxy_set_cached_property (GDBusProxy *proxy,
814 const gchar *property_name,
817 const GDBusPropertyInfo *info;
819 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
820 g_return_if_fail (property_name != NULL);
822 G_LOCK (properties_lock);
826 info = lookup_property_info (proxy, property_name);
829 if (g_strcmp0 (info->signature, g_variant_get_type_string (value)) != 0)
831 g_warning ("Trying to set property %s of type %s but according to the expected "
832 "interface the type is %s",
834 g_variant_get_type_string (value),
839 g_hash_table_insert (proxy->priv->properties,
840 g_strdup (property_name),
841 g_variant_ref_sink (value));
845 g_hash_table_remove (proxy->priv->properties, property_name);
849 G_UNLOCK (properties_lock);
852 /* ---------------------------------------------------------------------------------------------------- */
855 on_signal_received (GDBusConnection *connection,
856 const gchar *sender_name,
857 const gchar *object_path,
858 const gchar *interface_name,
859 const gchar *signal_name,
860 GVariant *parameters,
863 SignalSubscriptionData *data = user_data;
866 G_LOCK (signal_subscription_lock);
870 G_UNLOCK (signal_subscription_lock);
875 g_object_ref (proxy);
876 G_UNLOCK (signal_subscription_lock);
879 if (!proxy->priv->initialized)
882 G_LOCK (properties_lock);
884 if (proxy->priv->name_owner != NULL && g_strcmp0 (sender_name, proxy->priv->name_owner) != 0)
886 G_UNLOCK (properties_lock);
890 if (proxy->priv->expected_interface != NULL)
892 const GDBusSignalInfo *info;
893 info = g_dbus_interface_info_lookup_signal (proxy->priv->expected_interface, signal_name);
896 GVariantType *expected_type;
897 expected_type = _g_dbus_compute_complete_signature (info->args);
898 if (!g_variant_type_equal (expected_type, g_variant_get_type (parameters)))
900 gchar *expected_type_string = g_variant_type_dup_string (expected_type);
901 g_warning ("Dropping signal %s of type %s since the type from the expected interface is %s",
903 g_variant_get_type_string (parameters),
904 expected_type_string);
905 g_free (expected_type_string);
906 g_variant_type_free (expected_type);
907 G_UNLOCK (properties_lock);
910 g_variant_type_free (expected_type);
914 G_UNLOCK (properties_lock);
916 g_signal_emit (proxy,
917 signals[SIGNAL_SIGNAL],
925 g_object_unref (proxy);
928 /* ---------------------------------------------------------------------------------------------------- */
930 /* must hold properties_lock */
932 insert_property_checked (GDBusProxy *proxy,
933 gchar *property_name,
936 if (proxy->priv->expected_interface != NULL)
938 const GDBusPropertyInfo *info;
939 info = g_dbus_interface_info_lookup_property (proxy->priv->expected_interface, property_name);
940 /* Only check known properties */
943 /* Warn about properties with the wrong type */
944 if (g_strcmp0 (info->signature, g_variant_get_type_string (value)) != 0)
946 g_warning ("Received property %s with type %s does not match expected type "
947 "%s in the expected interface",
949 g_variant_get_type_string (value),
956 g_hash_table_insert (proxy->priv->properties,
957 property_name, /* adopts string */
958 value); /* adopts value */
963 g_variant_unref (value);
964 g_free (property_name);
971 } InvalidatedPropGetData;
974 invalidated_property_get_cb (GDBusConnection *connection,
978 InvalidatedPropGetData *data = user_data;
979 const gchar *invalidated_properties[] = {NULL};
980 GVariantBuilder builder;
981 GVariant *value = NULL;
982 GVariant *unpacked_value = NULL;
984 /* errors are fine, the other end could have disconnected */
985 value = g_dbus_connection_call_finish (connection, res, NULL);
991 if (!g_variant_is_of_type (value, G_VARIANT_TYPE ("(v)")))
993 g_warning ("Expected type '(v)' for Get() reply, got '%s'", g_variant_get_type_string (value));
997 g_variant_get (value, "(v)", &unpacked_value);
999 /* synthesize the a{sv} in the PropertiesChanged signal */
1000 g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
1001 g_variant_builder_add (&builder, "{sv}", data->prop_name, unpacked_value);
1003 G_LOCK (properties_lock);
1004 insert_property_checked (data->proxy,
1005 data->prop_name, /* adopts string */
1006 unpacked_value); /* adopts value */
1007 data->prop_name = NULL;
1008 G_UNLOCK (properties_lock);
1010 g_signal_emit (data->proxy,
1011 signals[PROPERTIES_CHANGED_SIGNAL], 0,
1012 g_variant_builder_end (&builder), /* consumed */
1013 invalidated_properties);
1018 g_variant_unref (value);
1019 g_object_unref (data->proxy);
1020 g_free (data->prop_name);
1021 g_slice_free (InvalidatedPropGetData, data);
1025 on_properties_changed (GDBusConnection *connection,
1026 const gchar *sender_name,
1027 const gchar *object_path,
1028 const gchar *interface_name,
1029 const gchar *signal_name,
1030 GVariant *parameters,
1033 SignalSubscriptionData *data = user_data;
1034 gboolean emit_g_signal = FALSE;
1036 const gchar *interface_name_for_signal;
1037 GVariant *changed_properties;
1038 gchar **invalidated_properties;
1044 changed_properties = NULL;
1045 invalidated_properties = NULL;
1047 G_LOCK (signal_subscription_lock);
1048 proxy = data->proxy;
1051 G_UNLOCK (signal_subscription_lock);
1056 g_object_ref (proxy);
1057 G_UNLOCK (signal_subscription_lock);
1060 if (!proxy->priv->initialized)
1063 G_LOCK (properties_lock);
1065 if (proxy->priv->name_owner != NULL && g_strcmp0 (sender_name, proxy->priv->name_owner) != 0)
1067 G_UNLOCK (properties_lock);
1071 if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(sa{sv}as)")))
1073 g_warning ("Value for PropertiesChanged signal with type '%s' does not match '(sa{sv}as)'",
1074 g_variant_get_type_string (parameters));
1075 G_UNLOCK (properties_lock);
1079 g_variant_get (parameters,
1081 &interface_name_for_signal,
1082 &changed_properties,
1083 &invalidated_properties);
1085 if (g_strcmp0 (interface_name_for_signal, proxy->priv->interface_name) != 0)
1087 G_UNLOCK (properties_lock);
1091 g_variant_iter_init (&iter, changed_properties);
1092 while (g_variant_iter_next (&iter, "{sv}", &key, &value))
1094 insert_property_checked (proxy,
1095 key, /* adopts string */
1096 value); /* adopts value */
1097 emit_g_signal = TRUE;
1100 if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_GET_INVALIDATED_PROPERTIES)
1102 if (proxy->priv->name_owner != NULL)
1104 for (n = 0; invalidated_properties[n] != NULL; n++)
1106 InvalidatedPropGetData *data;
1107 data = g_slice_new0 (InvalidatedPropGetData);
1108 data->proxy = g_object_ref (proxy);
1109 data->prop_name = g_strdup (invalidated_properties[n]);
1110 g_dbus_connection_call (proxy->priv->connection,
1111 proxy->priv->name_owner,
1112 proxy->priv->object_path,
1113 "org.freedesktop.DBus.Properties",
1115 g_variant_new ("(ss)", proxy->priv->interface_name, data->prop_name),
1116 G_VARIANT_TYPE ("(v)"),
1117 G_DBUS_CALL_FLAGS_NONE,
1119 NULL, /* GCancellable */
1120 (GAsyncReadyCallback) invalidated_property_get_cb,
1127 emit_g_signal = TRUE;
1128 for (n = 0; invalidated_properties[n] != NULL; n++)
1130 g_hash_table_remove (proxy->priv->properties, invalidated_properties[n]);
1134 G_UNLOCK (properties_lock);
1138 g_signal_emit (proxy, signals[PROPERTIES_CHANGED_SIGNAL],
1141 invalidated_properties);
1145 if (changed_properties != NULL)
1146 g_variant_unref (changed_properties);
1147 g_free (invalidated_properties);
1149 g_object_unref (proxy);
1152 /* ---------------------------------------------------------------------------------------------------- */
1155 process_get_all_reply (GDBusProxy *proxy,
1161 guint num_properties;
1163 if (!g_variant_is_of_type (result, G_VARIANT_TYPE ("(a{sv})")))
1165 g_warning ("Value for GetAll reply with type '%s' does not match '(a{sv})'",
1166 g_variant_get_type_string (result));
1170 G_LOCK (properties_lock);
1172 g_variant_get (result, "(a{sv})", &iter);
1173 while (g_variant_iter_next (iter, "{sv}", &key, &value))
1175 insert_property_checked (proxy,
1176 key, /* adopts string */
1177 value); /* adopts value */
1179 g_variant_iter_free (iter);
1181 num_properties = g_hash_table_size (proxy->priv->properties);
1182 G_UNLOCK (properties_lock);
1184 /* Synthesize ::g-properties-changed changed */
1185 if (num_properties > 0)
1187 GVariant *changed_properties;
1188 const gchar *invalidated_properties[1] = {NULL};
1190 g_variant_get (result,
1192 &changed_properties);
1193 g_signal_emit (proxy, signals[PROPERTIES_CHANGED_SIGNAL],
1196 invalidated_properties);
1197 g_variant_unref (changed_properties);
1207 GCancellable *cancellable;
1209 } LoadPropertiesOnNameOwnerChangedData;
1212 on_name_owner_changed_get_all_cb (GDBusConnection *connection,
1216 LoadPropertiesOnNameOwnerChangedData *data = user_data;
1224 result = g_dbus_connection_call_finish (connection,
1229 if (error->domain == G_IO_ERROR && error->code == G_IO_ERROR_CANCELLED)
1231 /* We just ignore if GetAll() is failing. Because this might happen
1232 * if the object has no properties at all. Or if the caller is
1233 * not authorized to see the properties.
1235 * Either way, apps can know about this by using
1236 * get_cached_property_names() or get_cached_property().
1238 * TODO: handle G_DBUS_DEBUG flag 'proxy' and, if enabled, log the
1239 * fact that GetAll() failed
1241 //g_debug ("error: %d %d %s", error->domain, error->code, error->message);
1242 g_error_free (error);
1245 /* and finally we can notify */
1248 G_LOCK (properties_lock);
1249 g_free (data->proxy->priv->name_owner);
1250 data->proxy->priv->name_owner = data->name_owner;
1251 data->name_owner = NULL; /* to avoid an extra copy, we steal the string */
1252 g_hash_table_remove_all (data->proxy->priv->properties);
1253 G_UNLOCK (properties_lock);
1256 process_get_all_reply (data->proxy, result);
1257 g_variant_unref (result);
1260 g_object_notify (G_OBJECT (data->proxy), "g-name-owner");
1263 if (data->cancellable == data->proxy->priv->get_all_cancellable)
1264 data->proxy->priv->get_all_cancellable = NULL;
1266 g_object_unref (data->proxy);
1267 g_object_unref (data->cancellable);
1268 g_free (data->name_owner);
1273 on_name_owner_changed (GDBusConnection *connection,
1274 const gchar *sender_name,
1275 const gchar *object_path,
1276 const gchar *interface_name,
1277 const gchar *signal_name,
1278 GVariant *parameters,
1281 SignalSubscriptionData *data = user_data;
1283 const gchar *old_owner;
1284 const gchar *new_owner;
1286 G_LOCK (signal_subscription_lock);
1287 proxy = data->proxy;
1290 G_UNLOCK (signal_subscription_lock);
1295 g_object_ref (proxy);
1296 G_UNLOCK (signal_subscription_lock);
1299 /* if we are already trying to load properties, cancel that */
1300 if (proxy->priv->get_all_cancellable != NULL)
1302 g_cancellable_cancel (proxy->priv->get_all_cancellable);
1303 proxy->priv->get_all_cancellable = NULL;
1306 g_variant_get (parameters,
1312 if (strlen (new_owner) == 0)
1314 G_LOCK (properties_lock);
1315 g_free (proxy->priv->name_owner);
1316 proxy->priv->name_owner = NULL;
1318 /* Synthesize ::g-properties-changed changed */
1319 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES) &&
1320 g_hash_table_size (proxy->priv->properties) > 0)
1322 GVariantBuilder builder;
1323 GPtrArray *invalidated_properties;
1324 GHashTableIter iter;
1327 /* Build changed_properties (always empty) and invalidated_properties ... */
1328 g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
1330 invalidated_properties = g_ptr_array_new_with_free_func (g_free);
1331 g_hash_table_iter_init (&iter, proxy->priv->properties);
1332 while (g_hash_table_iter_next (&iter, (gpointer) &key, NULL))
1333 g_ptr_array_add (invalidated_properties, g_strdup (key));
1334 g_ptr_array_add (invalidated_properties, NULL);
1336 /* ... throw out the properties ... */
1337 g_hash_table_remove_all (proxy->priv->properties);
1339 G_UNLOCK (properties_lock);
1341 /* ... and finally emit the ::g-properties-changed signal */
1342 g_signal_emit (proxy, signals[PROPERTIES_CHANGED_SIGNAL],
1344 g_variant_builder_end (&builder) /* consumed */,
1345 (const gchar* const *) invalidated_properties->pdata);
1346 g_ptr_array_unref (invalidated_properties);
1350 G_UNLOCK (properties_lock);
1352 g_object_notify (G_OBJECT (proxy), "g-name-owner");
1356 G_LOCK (properties_lock);
1358 /* ignore duplicates - this can happen when activating the service */
1359 if (g_strcmp0 (new_owner, proxy->priv->name_owner) == 0)
1361 G_UNLOCK (properties_lock);
1365 if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES)
1367 g_free (proxy->priv->name_owner);
1368 proxy->priv->name_owner = g_strdup (new_owner);
1370 g_hash_table_remove_all (proxy->priv->properties);
1371 G_UNLOCK (properties_lock);
1372 g_object_notify (G_OBJECT (proxy), "g-name-owner");
1376 LoadPropertiesOnNameOwnerChangedData *data;
1378 G_UNLOCK (properties_lock);
1380 /* start loading properties.. only then emit notify::g-name-owner .. we
1381 * need to be able to cancel this in the event another NameOwnerChanged
1382 * signal suddenly happens
1385 g_assert (proxy->priv->get_all_cancellable == NULL);
1386 proxy->priv->get_all_cancellable = g_cancellable_new ();
1387 data = g_new0 (LoadPropertiesOnNameOwnerChangedData, 1);
1388 data->proxy = g_object_ref (proxy);
1389 data->cancellable = proxy->priv->get_all_cancellable;
1390 data->name_owner = g_strdup (new_owner);
1391 g_dbus_connection_call (proxy->priv->connection,
1393 proxy->priv->object_path,
1394 "org.freedesktop.DBus.Properties",
1396 g_variant_new ("(s)", proxy->priv->interface_name),
1397 G_VARIANT_TYPE ("(a{sv})"),
1398 G_DBUS_CALL_FLAGS_NONE,
1400 proxy->priv->get_all_cancellable,
1401 (GAsyncReadyCallback) on_name_owner_changed_get_all_cb,
1408 g_object_unref (proxy);
1411 /* ---------------------------------------------------------------------------------------------------- */
1416 GCancellable *cancellable;
1417 GSimpleAsyncResult *simple;
1421 async_init_data_free (AsyncInitData *data)
1423 g_object_unref (data->proxy);
1424 if (data->cancellable != NULL)
1425 g_object_unref (data->cancellable);
1426 g_object_unref (data->simple);
1431 async_init_get_all_cb (GDBusConnection *connection,
1435 AsyncInitData *data = user_data;
1440 result = g_dbus_connection_call_finish (connection,
1445 /* We just ignore if GetAll() is failing. Because this might happen
1446 * if the object has no properties at all. Or if the caller is
1447 * not authorized to see the properties.
1449 * Either way, apps can know about this by using
1450 * get_cached_property_names() or get_cached_property().
1452 * TODO: handle G_DBUS_DEBUG flag 'proxy' and, if enabled, log the
1453 * fact that GetAll() failed
1455 //g_debug ("error: %d %d %s", error->domain, error->code, error->message);
1456 g_error_free (error);
1460 g_simple_async_result_set_op_res_gpointer (data->simple,
1462 (GDestroyNotify) g_variant_unref);
1465 g_simple_async_result_complete_in_idle (data->simple);
1466 async_init_data_free (data);
1470 async_init_data_set_name_owner (AsyncInitData *data,
1471 const gchar *name_owner)
1476 if (name_owner != NULL)
1478 /* it starts as NULL anyway */
1479 G_LOCK (properties_lock);
1480 data->proxy->priv->name_owner = g_strdup (name_owner);
1481 G_UNLOCK (properties_lock);
1486 if (data->proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES)
1488 /* Don't load properties if the API user doesn't want them */
1491 else if (name_owner == NULL && data->proxy->priv->name != NULL)
1493 /* Don't attempt to load properties if the name_owner is NULL (which
1494 * usually means the name isn't owned), unless name is also NULL (which
1495 * means we actually wanted to talk to the directly-connected process -
1496 * either dbus-daemon or a peer - instead of going via dbus-daemon)
1503 /* load all properties asynchronously */
1504 g_dbus_connection_call (data->proxy->priv->connection,
1506 data->proxy->priv->object_path,
1507 "org.freedesktop.DBus.Properties",
1509 g_variant_new ("(s)", data->proxy->priv->interface_name),
1510 G_VARIANT_TYPE ("(a{sv})"),
1511 G_DBUS_CALL_FLAGS_NONE,
1514 (GAsyncReadyCallback) async_init_get_all_cb,
1519 g_simple_async_result_complete_in_idle (data->simple);
1520 async_init_data_free (data);
1525 async_init_get_name_owner_cb (GDBusConnection *connection,
1529 AsyncInitData *data = user_data;
1534 result = g_dbus_connection_call_finish (connection,
1539 if (error->domain == G_DBUS_ERROR &&
1540 error->code == G_DBUS_ERROR_NAME_HAS_NO_OWNER)
1542 g_error_free (error);
1543 async_init_data_set_name_owner (data, NULL);
1547 g_simple_async_result_take_error (data->simple, error);
1548 g_simple_async_result_complete_in_idle (data->simple);
1549 async_init_data_free (data);
1554 /* borrowed from result to avoid an extra copy */
1555 const gchar *name_owner;
1557 g_variant_get (result, "(&s)", &name_owner);
1558 async_init_data_set_name_owner (data, name_owner);
1559 g_variant_unref (result);
1564 async_init_call_get_name_owner (AsyncInitData *data)
1566 g_dbus_connection_call (data->proxy->priv->connection,
1567 "org.freedesktop.DBus", /* name */
1568 "/org/freedesktop/DBus", /* object path */
1569 "org.freedesktop.DBus", /* interface */
1571 g_variant_new ("(s)",
1572 data->proxy->priv->name),
1573 G_VARIANT_TYPE ("(s)"),
1574 G_DBUS_CALL_FLAGS_NONE,
1577 (GAsyncReadyCallback) async_init_get_name_owner_cb,
1582 async_init_start_service_by_name_cb (GDBusConnection *connection,
1586 AsyncInitData *data = user_data;
1591 result = g_dbus_connection_call_finish (connection,
1596 /* Errors are not unexpected; the bus will reply e.g.
1598 * org.freedesktop.DBus.Error.ServiceUnknown: The name org.gnome.Epiphany2
1599 * was not provided by any .service files
1603 * org.freedesktop.systemd1.Masked: Unit polkit.service is masked.
1605 * This doesn't mean that the name doesn't have an owner, just
1606 * that it's not provided by a .service file or can't currently
1609 * In particular, in both cases, it could be that a service
1610 * owner will actually appear later. So instead of erroring out,
1611 * we just proceed to invoke GetNameOwner() if dealing with the
1612 * kind of errors above.
1614 if (error->domain == G_DBUS_ERROR && error->code == G_DBUS_ERROR_SERVICE_UNKNOWN)
1616 g_error_free (error);
1620 gchar *remote_error = g_dbus_error_get_remote_error (error);
1621 if (g_strcmp0 (remote_error, "org.freedesktop.systemd1.Masked") == 0)
1623 g_error_free (error);
1624 g_free (remote_error);
1628 g_prefix_error (&error,
1629 _("Error calling StartServiceByName for %s: "),
1630 data->proxy->priv->name);
1631 g_free (remote_error);
1638 guint32 start_service_result;
1639 g_variant_get (result,
1641 &start_service_result);
1642 g_variant_unref (result);
1643 if (start_service_result == 1 || /* DBUS_START_REPLY_SUCCESS */
1644 start_service_result == 2) /* DBUS_START_REPLY_ALREADY_RUNNING */
1646 /* continue to invoke GetNameOwner() */
1650 error = g_error_new (G_IO_ERROR,
1652 _("Unexpected reply %d from StartServiceByName(\"%s\") method"),
1653 start_service_result,
1654 data->proxy->priv->name);
1659 async_init_call_get_name_owner (data);
1663 g_warn_if_fail (error != NULL);
1664 g_simple_async_result_take_error (data->simple, error);
1665 g_simple_async_result_complete_in_idle (data->simple);
1666 async_init_data_free (data);
1670 async_init_call_start_service_by_name (AsyncInitData *data)
1672 g_dbus_connection_call (data->proxy->priv->connection,
1673 "org.freedesktop.DBus", /* name */
1674 "/org/freedesktop/DBus", /* object path */
1675 "org.freedesktop.DBus", /* interface */
1676 "StartServiceByName",
1677 g_variant_new ("(su)",
1678 data->proxy->priv->name,
1680 G_VARIANT_TYPE ("(u)"),
1681 G_DBUS_CALL_FLAGS_NONE,
1684 (GAsyncReadyCallback) async_init_start_service_by_name_cb,
1689 async_initable_init_second_async (GAsyncInitable *initable,
1691 GCancellable *cancellable,
1692 GAsyncReadyCallback callback,
1695 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1696 AsyncInitData *data;
1698 data = g_new0 (AsyncInitData, 1);
1699 data->proxy = g_object_ref (proxy);
1700 data->cancellable = cancellable != NULL ? g_object_ref (cancellable) : NULL;
1701 data->simple = g_simple_async_result_new (G_OBJECT (proxy),
1705 g_simple_async_result_set_check_cancellable (data->simple, cancellable);
1707 /* Check name ownership asynchronously - possibly also start the service */
1708 if (proxy->priv->name == NULL)
1711 async_init_data_set_name_owner (data, NULL);
1713 else if (g_dbus_is_unique_name (proxy->priv->name))
1715 async_init_data_set_name_owner (data, proxy->priv->name);
1719 if ((proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START) ||
1720 (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START_AT_CONSTRUCTION))
1722 async_init_call_get_name_owner (data);
1726 async_init_call_start_service_by_name (data);
1732 async_initable_init_second_finish (GAsyncInitable *initable,
1736 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1737 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1743 if (g_simple_async_result_propagate_error (simple, error))
1746 result = g_simple_async_result_get_op_res_gpointer (simple);
1749 process_get_all_reply (proxy, result);
1755 proxy->priv->initialized = TRUE;
1759 /* ---------------------------------------------------------------------------------------------------- */
1762 async_initable_init_first (GAsyncInitable *initable)
1764 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1766 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES))
1768 /* subscribe to PropertiesChanged() */
1769 proxy->priv->properties_changed_subscription_id =
1770 g_dbus_connection_signal_subscribe (proxy->priv->connection,
1772 "org.freedesktop.DBus.Properties",
1773 "PropertiesChanged",
1774 proxy->priv->object_path,
1775 proxy->priv->interface_name,
1776 G_DBUS_SIGNAL_FLAGS_NONE,
1777 on_properties_changed,
1778 signal_subscription_ref (proxy->priv->signal_subscription_data),
1779 (GDestroyNotify) signal_subscription_unref);
1782 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS))
1784 /* subscribe to all signals for the object */
1785 proxy->priv->signals_subscription_id =
1786 g_dbus_connection_signal_subscribe (proxy->priv->connection,
1788 proxy->priv->interface_name,
1790 proxy->priv->object_path,
1792 G_DBUS_SIGNAL_FLAGS_NONE,
1794 signal_subscription_ref (proxy->priv->signal_subscription_data),
1795 (GDestroyNotify) signal_subscription_unref);
1798 if (proxy->priv->name != NULL && !g_dbus_is_unique_name (proxy->priv->name))
1800 proxy->priv->name_owner_changed_subscription_id =
1801 g_dbus_connection_signal_subscribe (proxy->priv->connection,
1802 "org.freedesktop.DBus", /* name */
1803 "org.freedesktop.DBus", /* interface */
1804 "NameOwnerChanged", /* signal name */
1805 "/org/freedesktop/DBus", /* path */
1806 proxy->priv->name, /* arg0 */
1807 G_DBUS_SIGNAL_FLAGS_NONE,
1808 on_name_owner_changed,
1809 signal_subscription_ref (proxy->priv->signal_subscription_data),
1810 (GDestroyNotify) signal_subscription_unref);
1814 /* ---------------------------------------------------------------------------------------------------- */
1816 /* initialization is split into two parts - the first is the
1817 * non-blocing part that requires the callers GMainContext - the
1818 * second is a blocking part async part that doesn't require the
1819 * callers GMainContext.. we do this split so the code can be reused
1820 * in the GInitable implementation below.
1822 * Note that obtaining a GDBusConnection is not shared between the two
1830 GCancellable *cancellable;
1831 GAsyncReadyCallback callback;
1833 } GetConnectionData;
1836 get_connection_cb (GObject *source_object,
1840 GetConnectionData *data = user_data;
1844 data->proxy->priv->connection = g_bus_get_finish (res, &error);
1845 if (data->proxy->priv->connection == NULL)
1847 GSimpleAsyncResult *simple;
1848 simple = g_simple_async_result_new (G_OBJECT (data->proxy),
1852 g_simple_async_result_set_check_cancellable (simple, data->cancellable);
1853 g_simple_async_result_take_error (simple, error);
1854 g_simple_async_result_complete_in_idle (simple);
1855 g_object_unref (simple);
1859 async_initable_init_first (G_ASYNC_INITABLE (data->proxy));
1860 async_initable_init_second_async (G_ASYNC_INITABLE (data->proxy),
1867 if (data->cancellable != NULL)
1868 g_object_unref (data->cancellable);
1870 g_object_unref (data->proxy);
1875 async_initable_init_async (GAsyncInitable *initable,
1877 GCancellable *cancellable,
1878 GAsyncReadyCallback callback,
1881 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1883 if (proxy->priv->bus_type != G_BUS_TYPE_NONE)
1885 GetConnectionData *data;
1887 g_assert (proxy->priv->connection == NULL);
1889 data = g_new0 (GetConnectionData, 1);
1890 data->proxy = g_object_ref (proxy);
1891 data->io_priority = io_priority;
1892 data->cancellable = cancellable != NULL ? g_object_ref (cancellable) : NULL;
1893 data->callback = callback;
1894 data->user_data = user_data;
1895 g_bus_get (proxy->priv->bus_type,
1902 async_initable_init_first (initable);
1903 async_initable_init_second_async (initable, io_priority, cancellable, callback, user_data);
1908 async_initable_init_finish (GAsyncInitable *initable,
1912 return async_initable_init_second_finish (initable, res, error);
1916 async_initable_iface_init (GAsyncInitableIface *async_initable_iface)
1918 async_initable_iface->init_async = async_initable_init_async;
1919 async_initable_iface->init_finish = async_initable_init_finish;
1922 /* ---------------------------------------------------------------------------------------------------- */
1926 GMainContext *context;
1929 } InitableAsyncInitableData;
1932 async_initable_init_async_cb (GObject *source_object,
1936 InitableAsyncInitableData *data = user_data;
1937 data->res = g_object_ref (res);
1938 g_main_loop_quit (data->loop);
1941 /* Simply reuse the GAsyncInitable implementation but run the first
1942 * part (that is non-blocking and requires the callers GMainContext)
1943 * with the callers GMainContext.. and the second with a private
1944 * GMainContext (bug 621310 is slightly related).
1946 * Note that obtaining a GDBusConnection is not shared between the two
1950 initable_init (GInitable *initable,
1951 GCancellable *cancellable,
1954 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1955 InitableAsyncInitableData *data;
1960 if (proxy->priv->bus_type != G_BUS_TYPE_NONE)
1962 g_assert (proxy->priv->connection == NULL);
1963 proxy->priv->connection = g_bus_get_sync (proxy->priv->bus_type,
1966 if (proxy->priv->connection == NULL)
1970 async_initable_init_first (G_ASYNC_INITABLE (initable));
1972 data = g_new0 (InitableAsyncInitableData, 1);
1973 data->context = g_main_context_new ();
1974 data->loop = g_main_loop_new (data->context, FALSE);
1976 g_main_context_push_thread_default (data->context);
1978 async_initable_init_second_async (G_ASYNC_INITABLE (initable),
1981 async_initable_init_async_cb,
1984 g_main_loop_run (data->loop);
1986 ret = async_initable_init_second_finish (G_ASYNC_INITABLE (initable),
1990 g_main_context_pop_thread_default (data->context);
1992 g_main_context_unref (data->context);
1993 g_main_loop_unref (data->loop);
1994 g_object_unref (data->res);
2003 initable_iface_init (GInitableIface *initable_iface)
2005 initable_iface->init = initable_init;
2008 /* ---------------------------------------------------------------------------------------------------- */
2012 * @connection: A #GDBusConnection.
2013 * @flags: Flags used when constructing the proxy.
2014 * @info: (allow-none): A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
2015 * @name: (allow-none): A bus name (well-known or unique) or %NULL if @connection is not a message bus connection.
2016 * @object_path: An object path.
2017 * @interface_name: A D-Bus interface name.
2018 * @cancellable: (allow-none): A #GCancellable or %NULL.
2019 * @callback: Callback function to invoke when the proxy is ready.
2020 * @user_data: User data to pass to @callback.
2022 * Creates a proxy for accessing @interface_name on the remote object
2023 * at @object_path owned by @name at @connection and asynchronously
2024 * loads D-Bus properties unless the
2025 * %G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES flag is used. Connect to
2026 * the #GDBusProxy::g-properties-changed signal to get notified about
2029 * If the %G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS flag is not set, also sets up
2030 * match rules for signals. Connect to the #GDBusProxy::g-signal signal
2031 * to handle signals from the remote object.
2033 * If @name is a well-known name and the
2034 * %G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START and %G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START_AT_CONSTRUCTION
2035 * flags aren't set and no name owner currently exists, the message bus
2036 * will be requested to launch a name owner for the name.
2038 * This is a failable asynchronous constructor - when the proxy is
2039 * ready, @callback will be invoked and you can use
2040 * g_dbus_proxy_new_finish() to get the result.
2042 * See g_dbus_proxy_new_sync() and for a synchronous version of this constructor.
2044 * #GDBusProxy is used in this [example][gdbus-wellknown-proxy].
2049 g_dbus_proxy_new (GDBusConnection *connection,
2050 GDBusProxyFlags flags,
2051 GDBusInterfaceInfo *info,
2053 const gchar *object_path,
2054 const gchar *interface_name,
2055 GCancellable *cancellable,
2056 GAsyncReadyCallback callback,
2059 g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
2060 g_return_if_fail ((name == NULL && g_dbus_connection_get_unique_name (connection) == NULL) || g_dbus_is_name (name));
2061 g_return_if_fail (g_variant_is_object_path (object_path));
2062 g_return_if_fail (g_dbus_is_interface_name (interface_name));
2064 g_async_initable_new_async (G_TYPE_DBUS_PROXY,
2070 "g-interface-info", info,
2072 "g-connection", connection,
2073 "g-object-path", object_path,
2074 "g-interface-name", interface_name,
2079 * g_dbus_proxy_new_finish:
2080 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback function passed to g_dbus_proxy_new().
2081 * @error: Return location for error or %NULL.
2083 * Finishes creating a #GDBusProxy.
2085 * Returns: A #GDBusProxy or %NULL if @error is set. Free with g_object_unref().
2090 g_dbus_proxy_new_finish (GAsyncResult *res,
2094 GObject *source_object;
2096 source_object = g_async_result_get_source_object (res);
2097 g_assert (source_object != NULL);
2099 object = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object),
2102 g_object_unref (source_object);
2105 return G_DBUS_PROXY (object);
2111 * g_dbus_proxy_new_sync:
2112 * @connection: A #GDBusConnection.
2113 * @flags: Flags used when constructing the proxy.
2114 * @info: (allow-none): A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
2115 * @name: (allow-none): A bus name (well-known or unique) or %NULL if @connection is not a message bus connection.
2116 * @object_path: An object path.
2117 * @interface_name: A D-Bus interface name.
2118 * @cancellable: (allow-none): A #GCancellable or %NULL.
2119 * @error: (allow-none): Return location for error or %NULL.
2121 * Creates a proxy for accessing @interface_name on the remote object
2122 * at @object_path owned by @name at @connection and synchronously
2123 * loads D-Bus properties unless the
2124 * %G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES flag is used.
2126 * If the %G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS flag is not set, also sets up
2127 * match rules for signals. Connect to the #GDBusProxy::g-signal signal
2128 * to handle signals from the remote object.
2130 * If @name is a well-known name and the
2131 * %G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START and %G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START_AT_CONSTRUCTION
2132 * flags aren't set and no name owner currently exists, the message bus
2133 * will be requested to launch a name owner for the name.
2135 * This is a synchronous failable constructor. See g_dbus_proxy_new()
2136 * and g_dbus_proxy_new_finish() for the asynchronous version.
2138 * #GDBusProxy is used in this [example][gdbus-wellknown-proxy].
2140 * Returns: A #GDBusProxy or %NULL if error is set. Free with g_object_unref().
2145 g_dbus_proxy_new_sync (GDBusConnection *connection,
2146 GDBusProxyFlags flags,
2147 GDBusInterfaceInfo *info,
2149 const gchar *object_path,
2150 const gchar *interface_name,
2151 GCancellable *cancellable,
2154 GInitable *initable;
2156 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
2157 g_return_val_if_fail ((name == NULL && g_dbus_connection_get_unique_name (connection) == NULL) ||
2158 g_dbus_is_name (name), NULL);
2159 g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
2160 g_return_val_if_fail (g_dbus_is_interface_name (interface_name), NULL);
2162 initable = g_initable_new (G_TYPE_DBUS_PROXY,
2166 "g-interface-info", info,
2168 "g-connection", connection,
2169 "g-object-path", object_path,
2170 "g-interface-name", interface_name,
2172 if (initable != NULL)
2173 return G_DBUS_PROXY (initable);
2178 /* ---------------------------------------------------------------------------------------------------- */
2181 * g_dbus_proxy_new_for_bus:
2182 * @bus_type: A #GBusType.
2183 * @flags: Flags used when constructing the proxy.
2184 * @info: (allow-none): A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
2185 * @name: A bus name (well-known or unique).
2186 * @object_path: An object path.
2187 * @interface_name: A D-Bus interface name.
2188 * @cancellable: (allow-none): A #GCancellable or %NULL.
2189 * @callback: Callback function to invoke when the proxy is ready.
2190 * @user_data: User data to pass to @callback.
2192 * Like g_dbus_proxy_new() but takes a #GBusType instead of a #GDBusConnection.
2194 * #GDBusProxy is used in this [example][gdbus-wellknown-proxy].
2199 g_dbus_proxy_new_for_bus (GBusType bus_type,
2200 GDBusProxyFlags flags,
2201 GDBusInterfaceInfo *info,
2203 const gchar *object_path,
2204 const gchar *interface_name,
2205 GCancellable *cancellable,
2206 GAsyncReadyCallback callback,
2209 g_return_if_fail (g_dbus_is_name (name));
2210 g_return_if_fail (g_variant_is_object_path (object_path));
2211 g_return_if_fail (g_dbus_is_interface_name (interface_name));
2213 g_async_initable_new_async (G_TYPE_DBUS_PROXY,
2219 "g-interface-info", info,
2221 "g-bus-type", bus_type,
2222 "g-object-path", object_path,
2223 "g-interface-name", interface_name,
2228 * g_dbus_proxy_new_for_bus_finish:
2229 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback function passed to g_dbus_proxy_new_for_bus().
2230 * @error: Return location for error or %NULL.
2232 * Finishes creating a #GDBusProxy.
2234 * Returns: A #GDBusProxy or %NULL if @error is set. Free with g_object_unref().
2239 g_dbus_proxy_new_for_bus_finish (GAsyncResult *res,
2242 return g_dbus_proxy_new_finish (res, error);
2246 * g_dbus_proxy_new_for_bus_sync:
2247 * @bus_type: A #GBusType.
2248 * @flags: Flags used when constructing the proxy.
2249 * @info: (allow-none): A #GDBusInterfaceInfo specifying the minimal interface
2250 * that @proxy conforms to or %NULL.
2251 * @name: A bus name (well-known or unique).
2252 * @object_path: An object path.
2253 * @interface_name: A D-Bus interface name.
2254 * @cancellable: (allow-none): A #GCancellable or %NULL.
2255 * @error: Return location for error or %NULL.
2257 * Like g_dbus_proxy_new_sync() but takes a #GBusType instead of a #GDBusConnection.
2259 * #GDBusProxy is used in this [example][gdbus-wellknown-proxy].
2261 * Returns: A #GDBusProxy or %NULL if error is set. Free with g_object_unref().
2266 g_dbus_proxy_new_for_bus_sync (GBusType bus_type,
2267 GDBusProxyFlags flags,
2268 GDBusInterfaceInfo *info,
2270 const gchar *object_path,
2271 const gchar *interface_name,
2272 GCancellable *cancellable,
2275 GInitable *initable;
2277 g_return_val_if_fail (g_dbus_is_name (name), NULL);
2278 g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
2279 g_return_val_if_fail (g_dbus_is_interface_name (interface_name), NULL);
2281 initable = g_initable_new (G_TYPE_DBUS_PROXY,
2285 "g-interface-info", info,
2287 "g-bus-type", bus_type,
2288 "g-object-path", object_path,
2289 "g-interface-name", interface_name,
2291 if (initable != NULL)
2292 return G_DBUS_PROXY (initable);
2297 /* ---------------------------------------------------------------------------------------------------- */
2300 * g_dbus_proxy_get_connection:
2301 * @proxy: A #GDBusProxy.
2303 * Gets the connection @proxy is for.
2305 * Returns: (transfer none): A #GDBusConnection owned by @proxy. Do not free.
2310 g_dbus_proxy_get_connection (GDBusProxy *proxy)
2312 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2313 return proxy->priv->connection;
2317 * g_dbus_proxy_get_flags:
2318 * @proxy: A #GDBusProxy.
2320 * Gets the flags that @proxy was constructed with.
2322 * Returns: Flags from the #GDBusProxyFlags enumeration.
2327 g_dbus_proxy_get_flags (GDBusProxy *proxy)
2329 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), 0);
2330 return proxy->priv->flags;
2334 * g_dbus_proxy_get_name:
2335 * @proxy: A #GDBusProxy.
2337 * Gets the name that @proxy was constructed for.
2339 * Returns: A string owned by @proxy. Do not free.
2344 g_dbus_proxy_get_name (GDBusProxy *proxy)
2346 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2347 return proxy->priv->name;
2351 * g_dbus_proxy_get_name_owner:
2352 * @proxy: A #GDBusProxy.
2354 * The unique name that owns the name that @proxy is for or %NULL if
2355 * no-one currently owns that name. You may connect to the
2356 * #GObject::notify signal to track changes to the
2357 * #GDBusProxy:g-name-owner property.
2359 * Returns: The name owner or %NULL if no name owner exists. Free with g_free().
2364 g_dbus_proxy_get_name_owner (GDBusProxy *proxy)
2368 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2370 G_LOCK (properties_lock);
2371 ret = g_strdup (proxy->priv->name_owner);
2372 G_UNLOCK (properties_lock);
2377 * g_dbus_proxy_get_object_path:
2378 * @proxy: A #GDBusProxy.
2380 * Gets the object path @proxy is for.
2382 * Returns: A string owned by @proxy. Do not free.
2387 g_dbus_proxy_get_object_path (GDBusProxy *proxy)
2389 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2390 return proxy->priv->object_path;
2394 * g_dbus_proxy_get_interface_name:
2395 * @proxy: A #GDBusProxy.
2397 * Gets the D-Bus interface name @proxy is for.
2399 * Returns: A string owned by @proxy. Do not free.
2404 g_dbus_proxy_get_interface_name (GDBusProxy *proxy)
2406 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2407 return proxy->priv->interface_name;
2411 * g_dbus_proxy_get_default_timeout:
2412 * @proxy: A #GDBusProxy.
2414 * Gets the timeout to use if -1 (specifying default timeout) is
2415 * passed as @timeout_msec in the g_dbus_proxy_call() and
2416 * g_dbus_proxy_call_sync() functions.
2418 * See the #GDBusProxy:g-default-timeout property for more details.
2420 * Returns: Timeout to use for @proxy.
2425 g_dbus_proxy_get_default_timeout (GDBusProxy *proxy)
2429 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), -1);
2431 G_LOCK (properties_lock);
2432 ret = proxy->priv->timeout_msec;
2433 G_UNLOCK (properties_lock);
2438 * g_dbus_proxy_set_default_timeout:
2439 * @proxy: A #GDBusProxy.
2440 * @timeout_msec: Timeout in milliseconds.
2442 * Sets the timeout to use if -1 (specifying default timeout) is
2443 * passed as @timeout_msec in the g_dbus_proxy_call() and
2444 * g_dbus_proxy_call_sync() functions.
2446 * See the #GDBusProxy:g-default-timeout property for more details.
2451 g_dbus_proxy_set_default_timeout (GDBusProxy *proxy,
2454 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
2455 g_return_if_fail (timeout_msec == -1 || timeout_msec >= 0);
2457 G_LOCK (properties_lock);
2459 if (proxy->priv->timeout_msec != timeout_msec)
2461 proxy->priv->timeout_msec = timeout_msec;
2462 G_UNLOCK (properties_lock);
2464 g_object_notify (G_OBJECT (proxy), "g-default-timeout");
2468 G_UNLOCK (properties_lock);
2473 * g_dbus_proxy_get_interface_info:
2474 * @proxy: A #GDBusProxy
2476 * Returns the #GDBusInterfaceInfo, if any, specifying the interface
2477 * that @proxy conforms to. See the #GDBusProxy:g-interface-info
2478 * property for more details.
2480 * Returns: A #GDBusInterfaceInfo or %NULL. Do not unref the returned
2481 * object, it is owned by @proxy.
2485 GDBusInterfaceInfo *
2486 g_dbus_proxy_get_interface_info (GDBusProxy *proxy)
2488 GDBusInterfaceInfo *ret;
2490 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2492 G_LOCK (properties_lock);
2493 ret = proxy->priv->expected_interface;
2494 G_UNLOCK (properties_lock);
2495 /* FIXME: returning a borrowed ref with no guarantee that nobody will
2496 * call g_dbus_proxy_set_interface_info() and make it invalid...
2502 * g_dbus_proxy_set_interface_info:
2503 * @proxy: A #GDBusProxy
2504 * @info: (allow-none): Minimum interface this proxy conforms to or %NULL to unset.
2506 * Ensure that interactions with @proxy conform to the given
2507 * interface. See the #GDBusProxy:g-interface-info property for more
2513 g_dbus_proxy_set_interface_info (GDBusProxy *proxy,
2514 GDBusInterfaceInfo *info)
2516 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
2517 G_LOCK (properties_lock);
2519 if (proxy->priv->expected_interface != NULL)
2521 g_dbus_interface_info_cache_release (proxy->priv->expected_interface);
2522 g_dbus_interface_info_unref (proxy->priv->expected_interface);
2524 proxy->priv->expected_interface = info != NULL ? g_dbus_interface_info_ref (info) : NULL;
2525 if (proxy->priv->expected_interface != NULL)
2526 g_dbus_interface_info_cache_build (proxy->priv->expected_interface);
2528 G_UNLOCK (properties_lock);
2531 /* ---------------------------------------------------------------------------------------------------- */
2534 maybe_split_method_name (const gchar *method_name,
2535 gchar **out_interface_name,
2536 const gchar **out_method_name)
2541 g_assert (out_interface_name != NULL);
2542 g_assert (out_method_name != NULL);
2543 *out_interface_name = NULL;
2544 *out_method_name = NULL;
2546 if (strchr (method_name, '.') != NULL)
2551 p = g_strdup (method_name);
2552 last_dot = strrchr (p, '.');
2555 *out_interface_name = p;
2556 *out_method_name = last_dot + 1;
2568 GUnixFDList *fd_list;
2573 reply_data_free (ReplyData *data)
2575 g_variant_unref (data->value);
2577 if (data->fd_list != NULL)
2578 g_object_unref (data->fd_list);
2580 g_slice_free (ReplyData, data);
2584 reply_cb (GDBusConnection *connection,
2588 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
2592 GUnixFDList *fd_list;
2597 value = g_dbus_connection_call_with_unix_fd_list_finish (connection,
2602 value = g_dbus_connection_call_finish (connection,
2608 g_simple_async_result_take_error (simple, error);
2613 data = g_slice_new0 (ReplyData);
2614 data->value = value;
2616 data->fd_list = fd_list;
2618 g_simple_async_result_set_op_res_gpointer (simple, data, (GDestroyNotify) reply_data_free);
2621 /* no need to complete in idle since the method GDBusConnection already does */
2622 g_simple_async_result_complete (simple);
2623 g_object_unref (simple);
2626 /* properties_lock must be held for as long as you will keep the
2629 static const GDBusMethodInfo *
2630 lookup_method_info (GDBusProxy *proxy,
2631 const gchar *method_name)
2633 const GDBusMethodInfo *info = NULL;
2635 if (proxy->priv->expected_interface == NULL)
2638 info = g_dbus_interface_info_lookup_method (proxy->priv->expected_interface, method_name);
2644 /* properties_lock must be held for as long as you will keep the
2647 static const gchar *
2648 get_destination_for_call (GDBusProxy *proxy)
2654 /* If proxy->priv->name is a unique name, then proxy->priv->name_owner
2655 * is never NULL and always the same as proxy->priv->name. We use this
2656 * knowledge to avoid checking if proxy->priv->name is a unique or
2659 ret = proxy->priv->name_owner;
2663 if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START)
2666 ret = proxy->priv->name;
2672 /* ---------------------------------------------------------------------------------------------------- */
2675 g_dbus_proxy_call_internal (GDBusProxy *proxy,
2676 const gchar *method_name,
2677 GVariant *parameters,
2678 GDBusCallFlags flags,
2680 GUnixFDList *fd_list,
2681 GCancellable *cancellable,
2682 GAsyncReadyCallback callback,
2685 GSimpleAsyncResult *simple;
2687 gchar *split_interface_name;
2688 const gchar *split_method_name;
2689 const gchar *target_method_name;
2690 const gchar *target_interface_name;
2692 GVariantType *reply_type;
2693 GAsyncReadyCallback my_callback;
2695 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
2696 g_return_if_fail (g_dbus_is_member_name (method_name) || g_dbus_is_interface_name (method_name));
2697 g_return_if_fail (parameters == NULL || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE));
2698 g_return_if_fail (timeout_msec == -1 || timeout_msec >= 0);
2700 g_return_if_fail (fd_list == NULL || G_IS_UNIX_FD_LIST (fd_list));
2702 g_return_if_fail (fd_list == NULL);
2706 split_interface_name = NULL;
2708 /* g_dbus_connection_call() is optimised for the case of a NULL
2709 * callback. If we get a NULL callback from our user then make sure
2710 * we pass along a NULL callback for ourselves as well.
2712 if (callback != NULL)
2714 my_callback = (GAsyncReadyCallback) reply_cb;
2715 simple = g_simple_async_result_new (G_OBJECT (proxy),
2718 g_dbus_proxy_call_internal);
2719 g_simple_async_result_set_check_cancellable (simple, cancellable);
2727 G_LOCK (properties_lock);
2729 was_split = maybe_split_method_name (method_name, &split_interface_name, &split_method_name);
2730 target_method_name = was_split ? split_method_name : method_name;
2731 target_interface_name = was_split ? split_interface_name : proxy->priv->interface_name;
2733 /* Warn if method is unexpected (cf. :g-interface-info) */
2736 const GDBusMethodInfo *expected_method_info;
2737 expected_method_info = lookup_method_info (proxy, target_method_name);
2738 if (expected_method_info != NULL)
2739 reply_type = _g_dbus_compute_complete_signature (expected_method_info->out_args);
2743 if (proxy->priv->name != NULL)
2745 destination = g_strdup (get_destination_for_call (proxy));
2746 if (destination == NULL)
2750 g_simple_async_result_set_error (simple,
2753 _("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"));
2754 g_simple_async_result_complete_in_idle (simple);
2755 g_object_unref (simple);
2757 G_UNLOCK (properties_lock);
2762 G_UNLOCK (properties_lock);
2765 g_dbus_connection_call_with_unix_fd_list (proxy->priv->connection,
2767 proxy->priv->object_path,
2768 target_interface_name,
2773 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2779 g_dbus_connection_call (proxy->priv->connection,
2781 proxy->priv->object_path,
2782 target_interface_name,
2787 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2794 if (reply_type != NULL)
2795 g_variant_type_free (reply_type);
2797 g_free (destination);
2798 g_free (split_interface_name);
2802 g_dbus_proxy_call_finish_internal (GDBusProxy *proxy,
2803 GUnixFDList **out_fd_list,
2807 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2811 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2812 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2813 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2815 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_dbus_proxy_call_internal);
2819 if (g_simple_async_result_propagate_error (simple, error))
2822 data = g_simple_async_result_get_op_res_gpointer (simple);
2823 value = g_variant_ref (data->value);
2825 if (out_fd_list != NULL)
2826 *out_fd_list = data->fd_list != NULL ? g_object_ref (data->fd_list) : NULL;
2834 g_dbus_proxy_call_sync_internal (GDBusProxy *proxy,
2835 const gchar *method_name,
2836 GVariant *parameters,
2837 GDBusCallFlags flags,
2839 GUnixFDList *fd_list,
2840 GUnixFDList **out_fd_list,
2841 GCancellable *cancellable,
2846 gchar *split_interface_name;
2847 const gchar *split_method_name;
2848 const gchar *target_method_name;
2849 const gchar *target_interface_name;
2851 GVariantType *reply_type;
2853 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2854 g_return_val_if_fail (g_dbus_is_member_name (method_name) || g_dbus_is_interface_name (method_name), NULL);
2855 g_return_val_if_fail (parameters == NULL || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE), NULL);
2856 g_return_val_if_fail (timeout_msec == -1 || timeout_msec >= 0, NULL);
2858 g_return_val_if_fail (fd_list == NULL || G_IS_UNIX_FD_LIST (fd_list), NULL);
2860 g_return_val_if_fail (fd_list == NULL, NULL);
2862 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2866 G_LOCK (properties_lock);
2868 was_split = maybe_split_method_name (method_name, &split_interface_name, &split_method_name);
2869 target_method_name = was_split ? split_method_name : method_name;
2870 target_interface_name = was_split ? split_interface_name : proxy->priv->interface_name;
2872 /* Warn if method is unexpected (cf. :g-interface-info) */
2875 const GDBusMethodInfo *expected_method_info;
2876 expected_method_info = lookup_method_info (proxy, target_method_name);
2877 if (expected_method_info != NULL)
2878 reply_type = _g_dbus_compute_complete_signature (expected_method_info->out_args);
2882 if (proxy->priv->name != NULL)
2884 destination = g_strdup (get_destination_for_call (proxy));
2885 if (destination == NULL)
2887 g_set_error_literal (error,
2890 _("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"));
2892 G_UNLOCK (properties_lock);
2897 G_UNLOCK (properties_lock);
2900 ret = g_dbus_connection_call_with_unix_fd_list_sync (proxy->priv->connection,
2902 proxy->priv->object_path,
2903 target_interface_name,
2908 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2914 ret = g_dbus_connection_call_sync (proxy->priv->connection,
2916 proxy->priv->object_path,
2917 target_interface_name,
2922 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2928 if (reply_type != NULL)
2929 g_variant_type_free (reply_type);
2931 g_free (destination);
2932 g_free (split_interface_name);
2937 /* ---------------------------------------------------------------------------------------------------- */
2940 * g_dbus_proxy_call:
2941 * @proxy: A #GDBusProxy.
2942 * @method_name: Name of method to invoke.
2943 * @parameters: (allow-none): A #GVariant tuple with parameters for the signal or %NULL if not passing parameters.
2944 * @flags: Flags from the #GDBusCallFlags enumeration.
2945 * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
2946 * "infinite") or -1 to use the proxy default timeout.
2947 * @cancellable: (allow-none): A #GCancellable or %NULL.
2948 * @callback: (allow-none): A #GAsyncReadyCallback to call when the request is satisfied or %NULL if you don't
2949 * care about the result of the method invocation.
2950 * @user_data: The data to pass to @callback.
2952 * Asynchronously invokes the @method_name method on @proxy.
2954 * If @method_name contains any dots, then @name is split into interface and
2955 * method name parts. This allows using @proxy for invoking methods on
2958 * If the #GDBusConnection associated with @proxy is closed then
2959 * the operation will fail with %G_IO_ERROR_CLOSED. If
2960 * @cancellable is canceled, the operation will fail with
2961 * %G_IO_ERROR_CANCELLED. If @parameters contains a value not
2962 * compatible with the D-Bus protocol, the operation fails with
2963 * %G_IO_ERROR_INVALID_ARGUMENT.
2965 * If the @parameters #GVariant is floating, it is consumed. This allows
2966 * convenient 'inline' use of g_variant_new(), e.g.:
2967 * |[<!-- language="C" -->
2968 * g_dbus_proxy_call (proxy,
2970 * g_variant_new ("(ss)",
2973 * G_DBUS_CALL_FLAGS_NONE,
2976 * (GAsyncReadyCallback) two_strings_done,
2980 * If @proxy has an expected interface (see
2981 * #GDBusProxy:g-interface-info) and @method_name is referenced by it,
2982 * then the return value is checked against the return type.
2984 * This is an asynchronous method. When the operation is finished,
2985 * @callback will be invoked in the
2986 * [thread-default main context][g-main-context-push-thread-default]
2987 * of the thread you are calling this method from.
2988 * You can then call g_dbus_proxy_call_finish() to get the result of
2989 * the operation. See g_dbus_proxy_call_sync() for the synchronous
2990 * version of this method.
2992 * If @callback is %NULL then the D-Bus method call message will be sent with
2993 * the %G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED flag set.
2998 g_dbus_proxy_call (GDBusProxy *proxy,
2999 const gchar *method_name,
3000 GVariant *parameters,
3001 GDBusCallFlags flags,
3003 GCancellable *cancellable,
3004 GAsyncReadyCallback callback,
3007 g_dbus_proxy_call_internal (proxy, method_name, parameters, flags, timeout_msec, NULL, cancellable, callback, user_data);
3011 * g_dbus_proxy_call_finish:
3012 * @proxy: A #GDBusProxy.
3013 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_proxy_call().
3014 * @error: Return location for error or %NULL.
3016 * Finishes an operation started with g_dbus_proxy_call().
3018 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
3019 * return values. Free with g_variant_unref().
3024 g_dbus_proxy_call_finish (GDBusProxy *proxy,
3028 return g_dbus_proxy_call_finish_internal (proxy, NULL, res, error);
3032 * g_dbus_proxy_call_sync:
3033 * @proxy: A #GDBusProxy.
3034 * @method_name: Name of method to invoke.
3035 * @parameters: (allow-none): A #GVariant tuple with parameters for the signal
3036 * or %NULL if not passing parameters.
3037 * @flags: Flags from the #GDBusCallFlags enumeration.
3038 * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
3039 * "infinite") or -1 to use the proxy default timeout.
3040 * @cancellable: (allow-none): A #GCancellable or %NULL.
3041 * @error: Return location for error or %NULL.
3043 * Synchronously invokes the @method_name method on @proxy.
3045 * If @method_name contains any dots, then @name is split into interface and
3046 * method name parts. This allows using @proxy for invoking methods on
3049 * If the #GDBusConnection associated with @proxy is disconnected then
3050 * the operation will fail with %G_IO_ERROR_CLOSED. If
3051 * @cancellable is canceled, the operation will fail with
3052 * %G_IO_ERROR_CANCELLED. If @parameters contains a value not
3053 * compatible with the D-Bus protocol, the operation fails with
3054 * %G_IO_ERROR_INVALID_ARGUMENT.
3056 * If the @parameters #GVariant is floating, it is consumed. This allows
3057 * convenient 'inline' use of g_variant_new(), e.g.:
3058 * |[<!-- language="C" -->
3059 * g_dbus_proxy_call_sync (proxy,
3061 * g_variant_new ("(ss)",
3064 * G_DBUS_CALL_FLAGS_NONE,
3070 * The calling thread is blocked until a reply is received. See
3071 * g_dbus_proxy_call() for the asynchronous version of this
3074 * If @proxy has an expected interface (see
3075 * #GDBusProxy:g-interface-info) and @method_name is referenced by it,
3076 * then the return value is checked against the return type.
3078 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
3079 * return values. Free with g_variant_unref().
3084 g_dbus_proxy_call_sync (GDBusProxy *proxy,
3085 const gchar *method_name,
3086 GVariant *parameters,
3087 GDBusCallFlags flags,
3089 GCancellable *cancellable,
3092 return g_dbus_proxy_call_sync_internal (proxy, method_name, parameters, flags, timeout_msec, NULL, NULL, cancellable, error);
3095 /* ---------------------------------------------------------------------------------------------------- */
3100 * g_dbus_proxy_call_with_unix_fd_list:
3101 * @proxy: A #GDBusProxy.
3102 * @method_name: Name of method to invoke.
3103 * @parameters: (allow-none): A #GVariant tuple with parameters for the signal or %NULL if not passing parameters.
3104 * @flags: Flags from the #GDBusCallFlags enumeration.
3105 * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
3106 * "infinite") or -1 to use the proxy default timeout.
3107 * @fd_list: (allow-none): A #GUnixFDList or %NULL.
3108 * @cancellable: (allow-none): A #GCancellable or %NULL.
3109 * @callback: (allow-none): A #GAsyncReadyCallback to call when the request is satisfied or %NULL if you don't
3110 * care about the result of the method invocation.
3111 * @user_data: The data to pass to @callback.
3113 * Like g_dbus_proxy_call() but also takes a #GUnixFDList object.
3115 * This method is only available on UNIX.
3120 g_dbus_proxy_call_with_unix_fd_list (GDBusProxy *proxy,
3121 const gchar *method_name,
3122 GVariant *parameters,
3123 GDBusCallFlags flags,
3125 GUnixFDList *fd_list,
3126 GCancellable *cancellable,
3127 GAsyncReadyCallback callback,
3130 g_dbus_proxy_call_internal (proxy, method_name, parameters, flags, timeout_msec, fd_list, cancellable, callback, user_data);
3134 * g_dbus_proxy_call_with_unix_fd_list_finish:
3135 * @proxy: A #GDBusProxy.
3136 * @out_fd_list: (out) (allow-none): Return location for a #GUnixFDList or %NULL.
3137 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_proxy_call_with_unix_fd_list().
3138 * @error: Return location for error or %NULL.
3140 * Finishes an operation started with g_dbus_proxy_call_with_unix_fd_list().
3142 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
3143 * return values. Free with g_variant_unref().
3148 g_dbus_proxy_call_with_unix_fd_list_finish (GDBusProxy *proxy,
3149 GUnixFDList **out_fd_list,
3153 return g_dbus_proxy_call_finish_internal (proxy, out_fd_list, res, error);
3157 * g_dbus_proxy_call_with_unix_fd_list_sync:
3158 * @proxy: A #GDBusProxy.
3159 * @method_name: Name of method to invoke.
3160 * @parameters: (allow-none): A #GVariant tuple with parameters for the signal
3161 * or %NULL if not passing parameters.
3162 * @flags: Flags from the #GDBusCallFlags enumeration.
3163 * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
3164 * "infinite") or -1 to use the proxy default timeout.
3165 * @fd_list: (allow-none): A #GUnixFDList or %NULL.
3166 * @out_fd_list: (out) (allow-none): Return location for a #GUnixFDList or %NULL.
3167 * @cancellable: (allow-none): A #GCancellable or %NULL.
3168 * @error: Return location for error or %NULL.
3170 * Like g_dbus_proxy_call_sync() but also takes and returns #GUnixFDList objects.
3172 * This method is only available on UNIX.
3174 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
3175 * return values. Free with g_variant_unref().
3180 g_dbus_proxy_call_with_unix_fd_list_sync (GDBusProxy *proxy,
3181 const gchar *method_name,
3182 GVariant *parameters,
3183 GDBusCallFlags flags,
3185 GUnixFDList *fd_list,
3186 GUnixFDList **out_fd_list,
3187 GCancellable *cancellable,
3190 return g_dbus_proxy_call_sync_internal (proxy, method_name, parameters, flags, timeout_msec, fd_list, out_fd_list, cancellable, error);
3193 #endif /* G_OS_UNIX */
3195 /* ---------------------------------------------------------------------------------------------------- */
3197 static GDBusInterfaceInfo *
3198 _g_dbus_proxy_get_info (GDBusInterface *interface)
3200 GDBusProxy *proxy = G_DBUS_PROXY (interface);
3201 return g_dbus_proxy_get_interface_info (proxy);
3204 static GDBusObject *
3205 _g_dbus_proxy_get_object (GDBusInterface *interface)
3207 GDBusProxy *proxy = G_DBUS_PROXY (interface);
3208 return proxy->priv->object;
3211 static GDBusObject *
3212 _g_dbus_proxy_dup_object (GDBusInterface *interface)
3214 GDBusProxy *proxy = G_DBUS_PROXY (interface);
3215 GDBusObject *ret = NULL;
3217 G_LOCK (properties_lock);
3218 if (proxy->priv->object != NULL)
3219 ret = g_object_ref (proxy->priv->object);
3220 G_UNLOCK (properties_lock);
3225 _g_dbus_proxy_set_object (GDBusInterface *interface,
3226 GDBusObject *object)
3228 GDBusProxy *proxy = G_DBUS_PROXY (interface);
3229 G_LOCK (properties_lock);
3230 if (proxy->priv->object != NULL)
3231 g_object_remove_weak_pointer (G_OBJECT (proxy->priv->object), (gpointer *) &proxy->priv->object);
3232 proxy->priv->object = object;
3233 if (proxy->priv->object != NULL)
3234 g_object_add_weak_pointer (G_OBJECT (proxy->priv->object), (gpointer *) &proxy->priv->object);
3235 G_UNLOCK (properties_lock);
3239 dbus_interface_iface_init (GDBusInterfaceIface *dbus_interface_iface)
3241 dbus_interface_iface->get_info = _g_dbus_proxy_get_info;
3242 dbus_interface_iface->get_object = _g_dbus_proxy_get_object;
3243 dbus_interface_iface->dup_object = _g_dbus_proxy_dup_object;
3244 dbus_interface_iface->set_object = _g_dbus_proxy_set_object;
3247 /* ---------------------------------------------------------------------------------------------------- */