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, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: David Zeuthen <davidz@redhat.com>
28 #include "gdbusutils.h"
29 #include "gdbusproxy.h"
30 #include "gioenumtypes.h"
31 #include "gdbusconnection.h"
32 #include "gdbuserror.h"
33 #include "gdbusprivate.h"
34 #include "ginitable.h"
35 #include "gasyncinitable.h"
37 #include "gasyncresult.h"
38 #include "gsimpleasyncresult.h"
39 #include "gcancellable.h"
40 #include "gdbusinterface.h"
43 #include "gunixfdlist.h"
50 * @short_description: Client-side D-Bus interface proxy
53 * #GDBusProxy is a base class used for proxies to access a D-Bus
54 * interface on a remote object. A #GDBusProxy can be constructed for
55 * both well-known and unique names.
57 * By default, #GDBusProxy will cache all properties (and listen to
58 * changes) of the remote object, and proxy all signals that gets
59 * emitted. This behaviour can be changed by passing suitable
60 * #GDBusProxyFlags when the proxy is created. If the proxy is for a
61 * well-known name, the property cache is flushed when the name owner
62 * vanishes and reloaded when a name owner appears.
64 * If a #GDBusProxy is used for a well-known name, the owner of the
65 * name is tracked and can be read from
66 * #GDBusProxy:g-name-owner. Connect to the #GObject::notify signal to
67 * get notified of changes. Additionally, only signals and property
68 * changes emitted from the current name owner are considered and
69 * calls are always sent to the current name owner. This avoids a
70 * number of race conditions when the name is lost by one owner and
71 * claimed by another. However, if no name owner currently exists,
72 * then calls will be sent to the well-known name which may result in
73 * the message bus launching an owner (unless
74 * %G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START is set).
76 * The generic #GDBusProxy::g-properties-changed and
77 * #GDBusProxy::g-signal signals are not very convenient to work
78 * with. Therefore, the recommended way of working with proxies is to
79 * subclass #GDBusProxy, and have more natural properties and signals
80 * in your derived class. See <xref linkend="gdbus-example-gdbus-codegen"/>
81 * for how this can easily be done using the
82 * <command><link linkend="gdbus-codegen">gdbus-codegen</link></command>
85 * A #GDBusProxy instance can be used from multiple threads but note
86 * that all signals (e.g. #GDBusProxy::g-signal, #GDBusProxy::g-properties-changed
87 * and #GObject::notify) are emitted in the
88 * <link linkend="g-main-context-push-thread-default">thread-default main loop</link>
89 * of the thread where the instance was constructed.
91 * <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>
94 /* lock protecting the mutable properties: name_owner, timeout_msec,
95 * expected_interface, and the properties hash table
97 G_LOCK_DEFINE_STATIC (properties_lock);
99 /* ---------------------------------------------------------------------------------------------------- */
101 G_LOCK_DEFINE_STATIC (signal_subscription_lock);
105 volatile gint ref_count;
107 } SignalSubscriptionData;
109 static SignalSubscriptionData *
110 signal_subscription_ref (SignalSubscriptionData *data)
112 g_atomic_int_inc (&data->ref_count);
117 signal_subscription_unref (SignalSubscriptionData *data)
119 if (g_atomic_int_dec_and_test (&data->ref_count))
121 g_slice_free (SignalSubscriptionData, data);
125 /* ---------------------------------------------------------------------------------------------------- */
127 struct _GDBusProxyPrivate
130 GDBusProxyFlags flags;
131 GDBusConnection *connection;
134 /* mutable, protected by properties_lock */
137 gchar *interface_name;
138 /* mutable, protected by properties_lock */
141 guint name_owner_changed_subscription_id;
143 GCancellable *get_all_cancellable;
145 /* gchar* -> GVariant*, protected by properties_lock */
146 GHashTable *properties;
148 /* mutable, protected by properties_lock */
149 GDBusInterfaceInfo *expected_interface;
151 guint properties_changed_subscription_id;
152 guint signals_subscription_id;
154 gboolean initialized;
156 /* mutable, protected by properties_lock */
159 SignalSubscriptionData *signal_subscription_data;
171 PROP_G_INTERFACE_NAME,
172 PROP_G_DEFAULT_TIMEOUT,
173 PROP_G_INTERFACE_INFO
178 PROPERTIES_CHANGED_SIGNAL,
183 static guint signals[LAST_SIGNAL] = {0};
185 static void dbus_interface_iface_init (GDBusInterfaceIface *dbus_interface_iface);
186 static void initable_iface_init (GInitableIface *initable_iface);
187 static void async_initable_iface_init (GAsyncInitableIface *async_initable_iface);
189 G_DEFINE_TYPE_WITH_CODE (GDBusProxy, g_dbus_proxy, G_TYPE_OBJECT,
190 G_ADD_PRIVATE (GDBusProxy)
191 G_IMPLEMENT_INTERFACE (G_TYPE_DBUS_INTERFACE, dbus_interface_iface_init)
192 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, initable_iface_init)
193 G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE, async_initable_iface_init))
196 g_dbus_proxy_dispose (GObject *object)
198 GDBusProxy *proxy = G_DBUS_PROXY (object);
199 G_LOCK (signal_subscription_lock);
200 if (proxy->priv->signal_subscription_data != NULL)
202 proxy->priv->signal_subscription_data->proxy = NULL;
203 signal_subscription_unref (proxy->priv->signal_subscription_data);
204 proxy->priv->signal_subscription_data = NULL;
206 G_UNLOCK (signal_subscription_lock);
208 G_OBJECT_CLASS (g_dbus_proxy_parent_class)->dispose (object);
212 g_dbus_proxy_finalize (GObject *object)
214 GDBusProxy *proxy = G_DBUS_PROXY (object);
216 g_warn_if_fail (proxy->priv->get_all_cancellable == NULL);
218 if (proxy->priv->name_owner_changed_subscription_id > 0)
219 g_dbus_connection_signal_unsubscribe (proxy->priv->connection,
220 proxy->priv->name_owner_changed_subscription_id);
222 if (proxy->priv->properties_changed_subscription_id > 0)
223 g_dbus_connection_signal_unsubscribe (proxy->priv->connection,
224 proxy->priv->properties_changed_subscription_id);
226 if (proxy->priv->signals_subscription_id > 0)
227 g_dbus_connection_signal_unsubscribe (proxy->priv->connection,
228 proxy->priv->signals_subscription_id);
230 if (proxy->priv->connection != NULL)
231 g_object_unref (proxy->priv->connection);
232 g_free (proxy->priv->name);
233 g_free (proxy->priv->name_owner);
234 g_free (proxy->priv->object_path);
235 g_free (proxy->priv->interface_name);
236 if (proxy->priv->properties != NULL)
237 g_hash_table_unref (proxy->priv->properties);
239 if (proxy->priv->expected_interface != NULL)
241 g_dbus_interface_info_cache_release (proxy->priv->expected_interface);
242 g_dbus_interface_info_unref (proxy->priv->expected_interface);
245 if (proxy->priv->object != NULL)
246 g_object_remove_weak_pointer (G_OBJECT (proxy->priv->object), (gpointer *) &proxy->priv->object);
248 G_OBJECT_CLASS (g_dbus_proxy_parent_class)->finalize (object);
252 g_dbus_proxy_get_property (GObject *object,
257 GDBusProxy *proxy = G_DBUS_PROXY (object);
261 case PROP_G_CONNECTION:
262 g_value_set_object (value, proxy->priv->connection);
266 g_value_set_flags (value, proxy->priv->flags);
270 g_value_set_string (value, proxy->priv->name);
273 case PROP_G_NAME_OWNER:
274 g_value_take_string (value, g_dbus_proxy_get_name_owner (proxy));
277 case PROP_G_OBJECT_PATH:
278 g_value_set_string (value, proxy->priv->object_path);
281 case PROP_G_INTERFACE_NAME:
282 g_value_set_string (value, proxy->priv->interface_name);
285 case PROP_G_DEFAULT_TIMEOUT:
286 g_value_set_int (value, g_dbus_proxy_get_default_timeout (proxy));
289 case PROP_G_INTERFACE_INFO:
290 g_value_set_boxed (value, g_dbus_proxy_get_interface_info (proxy));
294 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
300 g_dbus_proxy_set_property (GObject *object,
305 GDBusProxy *proxy = G_DBUS_PROXY (object);
309 case PROP_G_CONNECTION:
310 proxy->priv->connection = g_value_dup_object (value);
314 proxy->priv->flags = g_value_get_flags (value);
318 proxy->priv->name = g_value_dup_string (value);
321 case PROP_G_OBJECT_PATH:
322 proxy->priv->object_path = g_value_dup_string (value);
325 case PROP_G_INTERFACE_NAME:
326 proxy->priv->interface_name = g_value_dup_string (value);
329 case PROP_G_DEFAULT_TIMEOUT:
330 g_dbus_proxy_set_default_timeout (proxy, g_value_get_int (value));
333 case PROP_G_INTERFACE_INFO:
334 g_dbus_proxy_set_interface_info (proxy, g_value_get_boxed (value));
337 case PROP_G_BUS_TYPE:
338 proxy->priv->bus_type = g_value_get_enum (value);
342 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
348 g_dbus_proxy_class_init (GDBusProxyClass *klass)
350 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
352 gobject_class->dispose = g_dbus_proxy_dispose;
353 gobject_class->finalize = g_dbus_proxy_finalize;
354 gobject_class->set_property = g_dbus_proxy_set_property;
355 gobject_class->get_property = g_dbus_proxy_get_property;
357 /* Note that all property names are prefixed to avoid collisions with D-Bus property names
358 * in derived classes */
361 * GDBusProxy:g-interface-info:
363 * Ensure that interactions with this proxy conform to the given
364 * interface. This is mainly to ensure that malformed data received
365 * from the other peer is ignored. The given #GDBusInterfaceInfo is
366 * said to be the <emphasis>expected interface</emphasis>.
368 * The checks performed are:
371 * When completing a method call, if the type signature of
372 * the reply message isn't what's expected, the reply is
373 * discarded and the #GError is set to %G_IO_ERROR_INVALID_ARGUMENT.
376 * Received signals that have a type signature mismatch are dropped and
377 * a warning is logged via g_warning().
380 * Properties received via the initial <literal>GetAll()</literal> call
381 * or via the <literal>::PropertiesChanged</literal> signal (on the
382 * <ulink url="http://dbus.freedesktop.org/doc/dbus-specification.html#standard-interfaces-properties">org.freedesktop.DBus.Properties</ulink> interface) or
383 * set using g_dbus_proxy_set_cached_property() with a type signature
384 * mismatch are ignored and a warning is logged via g_warning().
387 * Note that these checks are never done on methods, signals and
388 * properties that are not referenced in the given
389 * #GDBusInterfaceInfo, since extending a D-Bus interface on the
390 * service-side is not considered an ABI break.
394 g_object_class_install_property (gobject_class,
395 PROP_G_INTERFACE_INFO,
396 g_param_spec_boxed ("g-interface-info",
397 P_("Interface Information"),
398 P_("Interface Information"),
399 G_TYPE_DBUS_INTERFACE_INFO,
402 G_PARAM_STATIC_NAME |
403 G_PARAM_STATIC_BLURB |
404 G_PARAM_STATIC_NICK));
407 * GDBusProxy:g-connection:
409 * The #GDBusConnection the proxy is for.
413 g_object_class_install_property (gobject_class,
415 g_param_spec_object ("g-connection",
417 P_("The connection the proxy is for"),
418 G_TYPE_DBUS_CONNECTION,
421 G_PARAM_CONSTRUCT_ONLY |
422 G_PARAM_STATIC_NAME |
423 G_PARAM_STATIC_BLURB |
424 G_PARAM_STATIC_NICK));
427 * GDBusProxy:g-bus-type:
429 * If this property is not %G_BUS_TYPE_NONE, then
430 * #GDBusProxy:g-connection must be %NULL and will be set to the
431 * #GDBusConnection obtained by calling g_bus_get() with the value
436 g_object_class_install_property (gobject_class,
438 g_param_spec_enum ("g-bus-type",
440 P_("The bus to connect to, if any"),
444 G_PARAM_CONSTRUCT_ONLY |
445 G_PARAM_STATIC_NAME |
446 G_PARAM_STATIC_BLURB |
447 G_PARAM_STATIC_NICK));
450 * GDBusProxy:g-flags:
452 * Flags from the #GDBusProxyFlags enumeration.
456 g_object_class_install_property (gobject_class,
458 g_param_spec_flags ("g-flags",
460 P_("Flags for the proxy"),
461 G_TYPE_DBUS_PROXY_FLAGS,
462 G_DBUS_PROXY_FLAGS_NONE,
465 G_PARAM_CONSTRUCT_ONLY |
466 G_PARAM_STATIC_NAME |
467 G_PARAM_STATIC_BLURB |
468 G_PARAM_STATIC_NICK));
473 * The well-known or unique name that the proxy is for.
477 g_object_class_install_property (gobject_class,
479 g_param_spec_string ("g-name",
481 P_("The well-known or unique name that the proxy is for"),
485 G_PARAM_CONSTRUCT_ONLY |
486 G_PARAM_STATIC_NAME |
487 G_PARAM_STATIC_BLURB |
488 G_PARAM_STATIC_NICK));
491 * GDBusProxy:g-name-owner:
493 * The unique name that owns #GDBusProxy:g-name or %NULL if no-one
494 * currently owns that name. You may connect to #GObject::notify signal to
495 * track changes to this property.
499 g_object_class_install_property (gobject_class,
501 g_param_spec_string ("g-name-owner",
503 P_("The unique name for the owner"),
506 G_PARAM_STATIC_NAME |
507 G_PARAM_STATIC_BLURB |
508 G_PARAM_STATIC_NICK));
511 * GDBusProxy:g-object-path:
513 * The object path the proxy is for.
517 g_object_class_install_property (gobject_class,
519 g_param_spec_string ("g-object-path",
521 P_("The object path the proxy is for"),
525 G_PARAM_CONSTRUCT_ONLY |
526 G_PARAM_STATIC_NAME |
527 G_PARAM_STATIC_BLURB |
528 G_PARAM_STATIC_NICK));
531 * GDBusProxy:g-interface-name:
533 * The D-Bus interface name the proxy is for.
537 g_object_class_install_property (gobject_class,
538 PROP_G_INTERFACE_NAME,
539 g_param_spec_string ("g-interface-name",
540 P_("g-interface-name"),
541 P_("The D-Bus interface name the proxy is for"),
545 G_PARAM_CONSTRUCT_ONLY |
546 G_PARAM_STATIC_NAME |
547 G_PARAM_STATIC_BLURB |
548 G_PARAM_STATIC_NICK));
551 * GDBusProxy:g-default-timeout:
553 * The timeout to use if -1 (specifying default timeout) is passed
554 * as @timeout_msec in the g_dbus_proxy_call() and
555 * g_dbus_proxy_call_sync() functions.
557 * This allows applications to set a proxy-wide timeout for all
558 * remote method invocations on the proxy. If this property is -1,
559 * the default timeout (typically 25 seconds) is used. If set to
560 * %G_MAXINT, then no timeout is used.
564 g_object_class_install_property (gobject_class,
565 PROP_G_DEFAULT_TIMEOUT,
566 g_param_spec_int ("g-default-timeout",
567 P_("Default Timeout"),
568 P_("Timeout for remote method invocation"),
575 G_PARAM_STATIC_NAME |
576 G_PARAM_STATIC_BLURB |
577 G_PARAM_STATIC_NICK));
580 * GDBusProxy::g-properties-changed:
581 * @proxy: The #GDBusProxy emitting the signal.
582 * @changed_properties: A #GVariant containing the properties that changed
583 * @invalidated_properties: A %NULL terminated array of properties that was invalidated
585 * Emitted when one or more D-Bus properties on @proxy changes. The
586 * local cache has already been updated when this signal fires. Note
587 * that both @changed_properties and @invalidated_properties are
588 * guaranteed to never be %NULL (either may be empty though).
590 * If the proxy has the flag
591 * %G_DBUS_PROXY_FLAGS_GET_INVALIDATED_PROPERTIES set, then
592 * @invalidated_properties will always be empty.
594 * This signal corresponds to the
595 * <literal>PropertiesChanged</literal> D-Bus signal on the
596 * <literal>org.freedesktop.DBus.Properties</literal> interface.
600 signals[PROPERTIES_CHANGED_SIGNAL] = g_signal_new ("g-properties-changed",
602 G_SIGNAL_RUN_LAST | G_SIGNAL_MUST_COLLECT,
603 G_STRUCT_OFFSET (GDBusProxyClass, g_properties_changed),
610 G_TYPE_STRV | G_SIGNAL_TYPE_STATIC_SCOPE);
613 * GDBusProxy::g-signal:
614 * @proxy: The #GDBusProxy emitting the signal.
615 * @sender_name: (allow-none): The sender of the signal or %NULL if the connection is not a bus connection.
616 * @signal_name: The name of the signal.
617 * @parameters: A #GVariant tuple with parameters for the signal.
619 * Emitted when a signal from the remote object and interface that @proxy is for, has been received.
623 signals[SIGNAL_SIGNAL] = g_signal_new ("g-signal",
625 G_SIGNAL_RUN_LAST | G_SIGNAL_MUST_COLLECT,
626 G_STRUCT_OFFSET (GDBusProxyClass, g_signal),
639 g_dbus_proxy_init (GDBusProxy *proxy)
641 proxy->priv = g_dbus_proxy_get_instance_private (proxy);
642 proxy->priv->signal_subscription_data = g_slice_new0 (SignalSubscriptionData);
643 proxy->priv->signal_subscription_data->ref_count = 1;
644 proxy->priv->signal_subscription_data->proxy = proxy;
645 proxy->priv->properties = g_hash_table_new_full (g_str_hash,
648 (GDestroyNotify) g_variant_unref);
651 /* ---------------------------------------------------------------------------------------------------- */
654 property_name_sort_func (const gchar **a,
657 return g_strcmp0 (*a, *b);
661 * g_dbus_proxy_get_cached_property_names:
662 * @proxy: A #GDBusProxy.
664 * Gets the names of all cached properties on @proxy.
666 * Returns: (transfer full): A %NULL-terminated array of strings or %NULL if
667 * @proxy has no cached properties. Free the returned array with
673 g_dbus_proxy_get_cached_property_names (GDBusProxy *proxy)
680 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
682 G_LOCK (properties_lock);
685 if (g_hash_table_size (proxy->priv->properties) == 0)
688 p = g_ptr_array_new ();
690 g_hash_table_iter_init (&iter, proxy->priv->properties);
691 while (g_hash_table_iter_next (&iter, (gpointer) &key, NULL))
692 g_ptr_array_add (p, g_strdup (key));
693 g_ptr_array_sort (p, (GCompareFunc) property_name_sort_func);
694 g_ptr_array_add (p, NULL);
696 names = (gchar **) g_ptr_array_free (p, FALSE);
699 G_UNLOCK (properties_lock);
703 /* properties_lock must be held for as long as you will keep the
706 static const GDBusPropertyInfo *
707 lookup_property_info (GDBusProxy *proxy,
708 const gchar *property_name)
710 const GDBusPropertyInfo *info = NULL;
712 if (proxy->priv->expected_interface == NULL)
715 info = g_dbus_interface_info_lookup_property (proxy->priv->expected_interface, property_name);
722 * g_dbus_proxy_get_cached_property:
723 * @proxy: A #GDBusProxy.
724 * @property_name: Property name.
726 * Looks up the value for a property from the cache. This call does no
729 * If @proxy has an expected interface (see
730 * #GDBusProxy:g-interface-info) and @property_name is referenced by
731 * it, then @value is checked against the type of the property.
733 * Returns: A reference to the #GVariant instance that holds the value
734 * for @property_name or %NULL if the value is not in the cache. The
735 * returned reference must be freed with g_variant_unref().
740 g_dbus_proxy_get_cached_property (GDBusProxy *proxy,
741 const gchar *property_name)
743 const GDBusPropertyInfo *info;
746 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
747 g_return_val_if_fail (property_name != NULL, NULL);
749 G_LOCK (properties_lock);
751 value = g_hash_table_lookup (proxy->priv->properties, property_name);
755 info = lookup_property_info (proxy, property_name);
758 const gchar *type_string = g_variant_get_type_string (value);
759 if (g_strcmp0 (type_string, info->signature) != 0)
761 g_warning ("Trying to get property %s with type %s but according to the expected "
762 "interface the type is %s",
771 g_variant_ref (value);
774 G_UNLOCK (properties_lock);
779 * g_dbus_proxy_set_cached_property:
780 * @proxy: A #GDBusProxy
781 * @property_name: Property name.
782 * @value: (allow-none): Value for the property or %NULL to remove it from the cache.
784 * If @value is not %NULL, sets the cached value for the property with
785 * name @property_name to the value in @value.
787 * If @value is %NULL, then the cached value is removed from the
790 * If @proxy has an expected interface (see
791 * #GDBusProxy:g-interface-info) and @property_name is referenced by
792 * it, then @value is checked against the type of the property.
794 * If the @value #GVariant is floating, it is consumed. This allows
795 * convenient 'inline' use of g_variant_new(), e.g.
797 * g_dbus_proxy_set_cached_property (proxy,
799 * g_variant_new ("(si)",
804 * Normally you will not need to use this method since @proxy is
805 * tracking changes using the
806 * <literal>org.freedesktop.DBus.Properties.PropertiesChanged</literal>
807 * D-Bus signal. However, for performance reasons an object may decide
808 * to not use this signal for some properties and instead use a
809 * proprietary out-of-band mechanism to transmit changes.
811 * As a concrete example, consider an object with a property
812 * <literal>ChatroomParticipants</literal> which is an array of
813 * strings. Instead of transmitting the same (long) array every time
814 * the property changes, it is more efficient to only transmit the
815 * delta using e.g. signals <literal>ChatroomParticipantJoined(String
816 * name)</literal> and <literal>ChatroomParticipantParted(String
822 g_dbus_proxy_set_cached_property (GDBusProxy *proxy,
823 const gchar *property_name,
826 const GDBusPropertyInfo *info;
828 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
829 g_return_if_fail (property_name != NULL);
831 G_LOCK (properties_lock);
835 info = lookup_property_info (proxy, property_name);
838 if (g_strcmp0 (info->signature, g_variant_get_type_string (value)) != 0)
840 g_warning ("Trying to set property %s of type %s but according to the expected "
841 "interface the type is %s",
843 g_variant_get_type_string (value),
848 g_hash_table_insert (proxy->priv->properties,
849 g_strdup (property_name),
850 g_variant_ref_sink (value));
854 g_hash_table_remove (proxy->priv->properties, property_name);
858 G_UNLOCK (properties_lock);
861 /* ---------------------------------------------------------------------------------------------------- */
864 on_signal_received (GDBusConnection *connection,
865 const gchar *sender_name,
866 const gchar *object_path,
867 const gchar *interface_name,
868 const gchar *signal_name,
869 GVariant *parameters,
872 SignalSubscriptionData *data = user_data;
875 G_LOCK (signal_subscription_lock);
879 G_UNLOCK (signal_subscription_lock);
884 g_object_ref (proxy);
885 G_UNLOCK (signal_subscription_lock);
888 if (!proxy->priv->initialized)
891 G_LOCK (properties_lock);
893 if (proxy->priv->name_owner != NULL && g_strcmp0 (sender_name, proxy->priv->name_owner) != 0)
895 G_UNLOCK (properties_lock);
899 if (proxy->priv->expected_interface != NULL)
901 const GDBusSignalInfo *info;
902 info = g_dbus_interface_info_lookup_signal (proxy->priv->expected_interface, signal_name);
905 GVariantType *expected_type;
906 expected_type = _g_dbus_compute_complete_signature (info->args);
907 if (!g_variant_type_equal (expected_type, g_variant_get_type (parameters)))
909 gchar *expected_type_string = g_variant_type_dup_string (expected_type);
910 g_warning ("Dropping signal %s of type %s since the type from the expected interface is %s",
912 g_variant_get_type_string (parameters),
913 expected_type_string);
914 g_free (expected_type_string);
915 g_variant_type_free (expected_type);
916 G_UNLOCK (properties_lock);
919 g_variant_type_free (expected_type);
923 G_UNLOCK (properties_lock);
925 g_signal_emit (proxy,
926 signals[SIGNAL_SIGNAL],
934 g_object_unref (proxy);
937 /* ---------------------------------------------------------------------------------------------------- */
939 /* must hold properties_lock */
941 insert_property_checked (GDBusProxy *proxy,
942 gchar *property_name,
945 if (proxy->priv->expected_interface != NULL)
947 const GDBusPropertyInfo *info;
948 info = g_dbus_interface_info_lookup_property (proxy->priv->expected_interface, property_name);
949 /* Only check known properties */
952 /* Warn about properties with the wrong type */
953 if (g_strcmp0 (info->signature, g_variant_get_type_string (value)) != 0)
955 g_warning ("Received property %s with type %s does not match expected type "
956 "%s in the expected interface",
958 g_variant_get_type_string (value),
965 g_hash_table_insert (proxy->priv->properties,
966 property_name, /* adopts string */
967 value); /* adopts value */
972 g_variant_unref (value);
973 g_free (property_name);
980 } InvalidatedPropGetData;
983 invalidated_property_get_cb (GDBusConnection *connection,
987 InvalidatedPropGetData *data = user_data;
988 const gchar *invalidated_properties[] = {NULL};
989 GVariantBuilder builder;
990 GVariant *value = NULL;
991 GVariant *unpacked_value = NULL;
993 /* errors are fine, the other end could have disconnected */
994 value = g_dbus_connection_call_finish (connection, res, NULL);
1000 if (!g_variant_is_of_type (value, G_VARIANT_TYPE ("(v)")))
1002 g_warning ("Expected type '(v)' for Get() reply, got '%s'", g_variant_get_type_string (value));
1006 g_variant_get (value, "(v)", &unpacked_value);
1008 /* synthesize the a{sv} in the PropertiesChanged signal */
1009 g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
1010 g_variant_builder_add (&builder, "{sv}", data->prop_name, unpacked_value);
1012 G_LOCK (properties_lock);
1013 insert_property_checked (data->proxy,
1014 data->prop_name, /* adopts string */
1015 unpacked_value); /* adopts value */
1016 data->prop_name = NULL;
1017 G_UNLOCK (properties_lock);
1019 g_signal_emit (data->proxy,
1020 signals[PROPERTIES_CHANGED_SIGNAL], 0,
1021 g_variant_builder_end (&builder), /* consumed */
1022 invalidated_properties);
1027 g_variant_unref (value);
1028 g_object_unref (data->proxy);
1029 g_free (data->prop_name);
1030 g_slice_free (InvalidatedPropGetData, data);
1034 on_properties_changed (GDBusConnection *connection,
1035 const gchar *sender_name,
1036 const gchar *object_path,
1037 const gchar *interface_name,
1038 const gchar *signal_name,
1039 GVariant *parameters,
1042 SignalSubscriptionData *data = user_data;
1043 gboolean emit_g_signal = FALSE;
1045 const gchar *interface_name_for_signal;
1046 GVariant *changed_properties;
1047 gchar **invalidated_properties;
1053 changed_properties = NULL;
1054 invalidated_properties = NULL;
1056 G_LOCK (signal_subscription_lock);
1057 proxy = data->proxy;
1060 G_UNLOCK (signal_subscription_lock);
1065 g_object_ref (proxy);
1066 G_UNLOCK (signal_subscription_lock);
1069 if (!proxy->priv->initialized)
1072 G_LOCK (properties_lock);
1074 if (proxy->priv->name_owner != NULL && g_strcmp0 (sender_name, proxy->priv->name_owner) != 0)
1076 G_UNLOCK (properties_lock);
1080 if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(sa{sv}as)")))
1082 g_warning ("Value for PropertiesChanged signal with type '%s' does not match '(sa{sv}as)'",
1083 g_variant_get_type_string (parameters));
1084 G_UNLOCK (properties_lock);
1088 g_variant_get (parameters,
1090 &interface_name_for_signal,
1091 &changed_properties,
1092 &invalidated_properties);
1094 if (g_strcmp0 (interface_name_for_signal, proxy->priv->interface_name) != 0)
1096 G_UNLOCK (properties_lock);
1100 g_variant_iter_init (&iter, changed_properties);
1101 while (g_variant_iter_next (&iter, "{sv}", &key, &value))
1103 insert_property_checked (proxy,
1104 key, /* adopts string */
1105 value); /* adopts value */
1106 emit_g_signal = TRUE;
1109 if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_GET_INVALIDATED_PROPERTIES)
1111 if (proxy->priv->name_owner != NULL)
1113 for (n = 0; invalidated_properties[n] != NULL; n++)
1115 InvalidatedPropGetData *data;
1116 data = g_slice_new0 (InvalidatedPropGetData);
1117 data->proxy = g_object_ref (proxy);
1118 data->prop_name = g_strdup (invalidated_properties[n]);
1119 g_dbus_connection_call (proxy->priv->connection,
1120 proxy->priv->name_owner,
1121 proxy->priv->object_path,
1122 "org.freedesktop.DBus.Properties",
1124 g_variant_new ("(ss)", proxy->priv->interface_name, data->prop_name),
1125 G_VARIANT_TYPE ("(v)"),
1126 G_DBUS_CALL_FLAGS_NONE,
1128 NULL, /* GCancellable */
1129 (GAsyncReadyCallback) invalidated_property_get_cb,
1136 emit_g_signal = TRUE;
1137 for (n = 0; invalidated_properties[n] != NULL; n++)
1139 g_hash_table_remove (proxy->priv->properties, invalidated_properties[n]);
1143 G_UNLOCK (properties_lock);
1147 g_signal_emit (proxy, signals[PROPERTIES_CHANGED_SIGNAL],
1150 invalidated_properties);
1154 if (changed_properties != NULL)
1155 g_variant_unref (changed_properties);
1156 g_free (invalidated_properties);
1158 g_object_unref (proxy);
1161 /* ---------------------------------------------------------------------------------------------------- */
1164 process_get_all_reply (GDBusProxy *proxy,
1170 guint num_properties;
1172 if (!g_variant_is_of_type (result, G_VARIANT_TYPE ("(a{sv})")))
1174 g_warning ("Value for GetAll reply with type '%s' does not match '(a{sv})'",
1175 g_variant_get_type_string (result));
1179 G_LOCK (properties_lock);
1181 g_variant_get (result, "(a{sv})", &iter);
1182 while (g_variant_iter_next (iter, "{sv}", &key, &value))
1184 insert_property_checked (proxy,
1185 key, /* adopts string */
1186 value); /* adopts value */
1188 g_variant_iter_free (iter);
1190 num_properties = g_hash_table_size (proxy->priv->properties);
1191 G_UNLOCK (properties_lock);
1193 /* Synthesize ::g-properties-changed changed */
1194 if (num_properties > 0)
1196 GVariant *changed_properties;
1197 const gchar *invalidated_properties[1] = {NULL};
1199 g_variant_get (result,
1201 &changed_properties);
1202 g_signal_emit (proxy, signals[PROPERTIES_CHANGED_SIGNAL],
1205 invalidated_properties);
1206 g_variant_unref (changed_properties);
1216 GCancellable *cancellable;
1218 } LoadPropertiesOnNameOwnerChangedData;
1221 on_name_owner_changed_get_all_cb (GDBusConnection *connection,
1225 LoadPropertiesOnNameOwnerChangedData *data = user_data;
1233 result = g_dbus_connection_call_finish (connection,
1238 if (error->domain == G_IO_ERROR && error->code == G_IO_ERROR_CANCELLED)
1240 /* We just ignore if GetAll() is failing. Because this might happen
1241 * if the object has no properties at all. Or if the caller is
1242 * not authorized to see the properties.
1244 * Either way, apps can know about this by using
1245 * get_cached_property_names() or get_cached_property().
1247 * TODO: handle G_DBUS_DEBUG flag 'proxy' and, if enabled, log the
1248 * fact that GetAll() failed
1250 //g_debug ("error: %d %d %s", error->domain, error->code, error->message);
1251 g_error_free (error);
1254 /* and finally we can notify */
1257 G_LOCK (properties_lock);
1258 g_free (data->proxy->priv->name_owner);
1259 data->proxy->priv->name_owner = data->name_owner;
1260 data->name_owner = NULL; /* to avoid an extra copy, we steal the string */
1261 g_hash_table_remove_all (data->proxy->priv->properties);
1262 G_UNLOCK (properties_lock);
1265 process_get_all_reply (data->proxy, result);
1266 g_variant_unref (result);
1269 g_object_notify (G_OBJECT (data->proxy), "g-name-owner");
1272 if (data->cancellable == data->proxy->priv->get_all_cancellable)
1273 data->proxy->priv->get_all_cancellable = NULL;
1275 g_object_unref (data->proxy);
1276 g_object_unref (data->cancellable);
1277 g_free (data->name_owner);
1282 on_name_owner_changed (GDBusConnection *connection,
1283 const gchar *sender_name,
1284 const gchar *object_path,
1285 const gchar *interface_name,
1286 const gchar *signal_name,
1287 GVariant *parameters,
1290 SignalSubscriptionData *data = user_data;
1292 const gchar *old_owner;
1293 const gchar *new_owner;
1295 G_LOCK (signal_subscription_lock);
1296 proxy = data->proxy;
1299 G_UNLOCK (signal_subscription_lock);
1304 g_object_ref (proxy);
1305 G_UNLOCK (signal_subscription_lock);
1308 /* if we are already trying to load properties, cancel that */
1309 if (proxy->priv->get_all_cancellable != NULL)
1311 g_cancellable_cancel (proxy->priv->get_all_cancellable);
1312 proxy->priv->get_all_cancellable = NULL;
1315 g_variant_get (parameters,
1321 if (strlen (new_owner) == 0)
1323 G_LOCK (properties_lock);
1324 g_free (proxy->priv->name_owner);
1325 proxy->priv->name_owner = NULL;
1327 /* Synthesize ::g-properties-changed changed */
1328 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES) &&
1329 g_hash_table_size (proxy->priv->properties) > 0)
1331 GVariantBuilder builder;
1332 GPtrArray *invalidated_properties;
1333 GHashTableIter iter;
1336 /* Build changed_properties (always empty) and invalidated_properties ... */
1337 g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
1339 invalidated_properties = g_ptr_array_new_with_free_func (g_free);
1340 g_hash_table_iter_init (&iter, proxy->priv->properties);
1341 while (g_hash_table_iter_next (&iter, (gpointer) &key, NULL))
1342 g_ptr_array_add (invalidated_properties, g_strdup (key));
1343 g_ptr_array_add (invalidated_properties, NULL);
1345 /* ... throw out the properties ... */
1346 g_hash_table_remove_all (proxy->priv->properties);
1348 G_UNLOCK (properties_lock);
1350 /* ... and finally emit the ::g-properties-changed signal */
1351 g_signal_emit (proxy, signals[PROPERTIES_CHANGED_SIGNAL],
1353 g_variant_builder_end (&builder) /* consumed */,
1354 (const gchar* const *) invalidated_properties->pdata);
1355 g_ptr_array_unref (invalidated_properties);
1359 G_UNLOCK (properties_lock);
1361 g_object_notify (G_OBJECT (proxy), "g-name-owner");
1365 G_LOCK (properties_lock);
1367 /* ignore duplicates - this can happen when activating the service */
1368 if (g_strcmp0 (new_owner, proxy->priv->name_owner) == 0)
1370 G_UNLOCK (properties_lock);
1374 if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES)
1376 g_free (proxy->priv->name_owner);
1377 proxy->priv->name_owner = g_strdup (new_owner);
1379 g_hash_table_remove_all (proxy->priv->properties);
1380 G_UNLOCK (properties_lock);
1381 g_object_notify (G_OBJECT (proxy), "g-name-owner");
1385 LoadPropertiesOnNameOwnerChangedData *data;
1387 G_UNLOCK (properties_lock);
1389 /* start loading properties.. only then emit notify::g-name-owner .. we
1390 * need to be able to cancel this in the event another NameOwnerChanged
1391 * signal suddenly happens
1394 g_assert (proxy->priv->get_all_cancellable == NULL);
1395 proxy->priv->get_all_cancellable = g_cancellable_new ();
1396 data = g_new0 (LoadPropertiesOnNameOwnerChangedData, 1);
1397 data->proxy = g_object_ref (proxy);
1398 data->cancellable = proxy->priv->get_all_cancellable;
1399 data->name_owner = g_strdup (new_owner);
1400 g_dbus_connection_call (proxy->priv->connection,
1402 proxy->priv->object_path,
1403 "org.freedesktop.DBus.Properties",
1405 g_variant_new ("(s)", proxy->priv->interface_name),
1406 G_VARIANT_TYPE ("(a{sv})"),
1407 G_DBUS_CALL_FLAGS_NONE,
1409 proxy->priv->get_all_cancellable,
1410 (GAsyncReadyCallback) on_name_owner_changed_get_all_cb,
1417 g_object_unref (proxy);
1420 /* ---------------------------------------------------------------------------------------------------- */
1425 GCancellable *cancellable;
1426 GSimpleAsyncResult *simple;
1430 async_init_data_free (AsyncInitData *data)
1432 g_object_unref (data->proxy);
1433 if (data->cancellable != NULL)
1434 g_object_unref (data->cancellable);
1435 g_object_unref (data->simple);
1440 async_init_get_all_cb (GDBusConnection *connection,
1444 AsyncInitData *data = user_data;
1449 result = g_dbus_connection_call_finish (connection,
1454 /* We just ignore if GetAll() is failing. Because this might happen
1455 * if the object has no properties at all. Or if the caller is
1456 * not authorized to see the properties.
1458 * Either way, apps can know about this by using
1459 * get_cached_property_names() or get_cached_property().
1461 * TODO: handle G_DBUS_DEBUG flag 'proxy' and, if enabled, log the
1462 * fact that GetAll() failed
1464 //g_debug ("error: %d %d %s", error->domain, error->code, error->message);
1465 g_error_free (error);
1469 g_simple_async_result_set_op_res_gpointer (data->simple,
1471 (GDestroyNotify) g_variant_unref);
1474 g_simple_async_result_complete_in_idle (data->simple);
1475 async_init_data_free (data);
1479 async_init_data_set_name_owner (AsyncInitData *data,
1480 const gchar *name_owner)
1485 if (name_owner != NULL)
1487 /* it starts as NULL anyway */
1488 G_LOCK (properties_lock);
1489 data->proxy->priv->name_owner = g_strdup (name_owner);
1490 G_UNLOCK (properties_lock);
1495 if (data->proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES)
1497 /* Don't load properties if the API user doesn't want them */
1500 else if (name_owner == NULL && data->proxy->priv->name != NULL)
1502 /* Don't attempt to load properties if the name_owner is NULL (which
1503 * usually means the name isn't owned), unless name is also NULL (which
1504 * means we actually wanted to talk to the directly-connected process -
1505 * either dbus-daemon or a peer - instead of going via dbus-daemon)
1512 /* load all properties asynchronously */
1513 g_dbus_connection_call (data->proxy->priv->connection,
1515 data->proxy->priv->object_path,
1516 "org.freedesktop.DBus.Properties",
1518 g_variant_new ("(s)", data->proxy->priv->interface_name),
1519 G_VARIANT_TYPE ("(a{sv})"),
1520 G_DBUS_CALL_FLAGS_NONE,
1523 (GAsyncReadyCallback) async_init_get_all_cb,
1528 g_simple_async_result_complete_in_idle (data->simple);
1529 async_init_data_free (data);
1534 async_init_get_name_owner_cb (GDBusConnection *connection,
1538 AsyncInitData *data = user_data;
1543 result = g_dbus_connection_call_finish (connection,
1548 if (error->domain == G_DBUS_ERROR &&
1549 error->code == G_DBUS_ERROR_NAME_HAS_NO_OWNER)
1551 g_error_free (error);
1552 async_init_data_set_name_owner (data, NULL);
1556 g_simple_async_result_take_error (data->simple, error);
1557 g_simple_async_result_complete_in_idle (data->simple);
1558 async_init_data_free (data);
1563 /* borrowed from result to avoid an extra copy */
1564 const gchar *name_owner;
1566 g_variant_get (result, "(&s)", &name_owner);
1567 async_init_data_set_name_owner (data, name_owner);
1568 g_variant_unref (result);
1573 async_init_call_get_name_owner (AsyncInitData *data)
1575 g_dbus_connection_call (data->proxy->priv->connection,
1576 "org.freedesktop.DBus", /* name */
1577 "/org/freedesktop/DBus", /* object path */
1578 "org.freedesktop.DBus", /* interface */
1580 g_variant_new ("(s)",
1581 data->proxy->priv->name),
1582 G_VARIANT_TYPE ("(s)"),
1583 G_DBUS_CALL_FLAGS_NONE,
1586 (GAsyncReadyCallback) async_init_get_name_owner_cb,
1591 async_init_start_service_by_name_cb (GDBusConnection *connection,
1595 AsyncInitData *data = user_data;
1600 result = g_dbus_connection_call_finish (connection,
1605 /* Errors are not unexpected; the bus will reply e.g.
1607 * org.freedesktop.DBus.Error.ServiceUnknown: The name org.gnome.Epiphany2
1608 * was not provided by any .service files
1612 * org.freedesktop.systemd1.Masked: Unit polkit.service is masked.
1614 * This doesn't mean that the name doesn't have an owner, just
1615 * that it's not provided by a .service file or can't currently
1618 * In particular, in both cases, it could be that a service
1619 * owner will actually appear later. So instead of erroring out,
1620 * we just proceed to invoke GetNameOwner() if dealing with the
1621 * kind of errors above.
1623 if (error->domain == G_DBUS_ERROR && error->code == G_DBUS_ERROR_SERVICE_UNKNOWN)
1625 g_error_free (error);
1629 gchar *remote_error = g_dbus_error_get_remote_error (error);
1630 if (g_strcmp0 (remote_error, "org.freedesktop.systemd1.Masked") == 0)
1632 g_error_free (error);
1633 g_free (remote_error);
1637 g_prefix_error (&error,
1638 _("Error calling StartServiceByName for %s: "),
1639 data->proxy->priv->name);
1640 g_free (remote_error);
1647 guint32 start_service_result;
1648 g_variant_get (result,
1650 &start_service_result);
1651 g_variant_unref (result);
1652 if (start_service_result == 1 || /* DBUS_START_REPLY_SUCCESS */
1653 start_service_result == 2) /* DBUS_START_REPLY_ALREADY_RUNNING */
1655 /* continue to invoke GetNameOwner() */
1659 error = g_error_new (G_IO_ERROR,
1661 _("Unexpected reply %d from StartServiceByName(\"%s\") method"),
1662 start_service_result,
1663 data->proxy->priv->name);
1668 async_init_call_get_name_owner (data);
1672 g_warn_if_fail (error != NULL);
1673 g_simple_async_result_take_error (data->simple, error);
1674 g_simple_async_result_complete_in_idle (data->simple);
1675 async_init_data_free (data);
1679 async_init_call_start_service_by_name (AsyncInitData *data)
1681 g_dbus_connection_call (data->proxy->priv->connection,
1682 "org.freedesktop.DBus", /* name */
1683 "/org/freedesktop/DBus", /* object path */
1684 "org.freedesktop.DBus", /* interface */
1685 "StartServiceByName",
1686 g_variant_new ("(su)",
1687 data->proxy->priv->name,
1689 G_VARIANT_TYPE ("(u)"),
1690 G_DBUS_CALL_FLAGS_NONE,
1693 (GAsyncReadyCallback) async_init_start_service_by_name_cb,
1698 async_initable_init_second_async (GAsyncInitable *initable,
1700 GCancellable *cancellable,
1701 GAsyncReadyCallback callback,
1704 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1705 AsyncInitData *data;
1707 data = g_new0 (AsyncInitData, 1);
1708 data->proxy = g_object_ref (proxy);
1709 data->cancellable = cancellable != NULL ? g_object_ref (cancellable) : NULL;
1710 data->simple = g_simple_async_result_new (G_OBJECT (proxy),
1714 g_simple_async_result_set_check_cancellable (data->simple, cancellable);
1716 /* Check name ownership asynchronously - possibly also start the service */
1717 if (proxy->priv->name == NULL)
1720 async_init_data_set_name_owner (data, NULL);
1722 else if (g_dbus_is_unique_name (proxy->priv->name))
1724 async_init_data_set_name_owner (data, proxy->priv->name);
1728 if ((proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START) ||
1729 (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START_AT_CONSTRUCTION))
1731 async_init_call_get_name_owner (data);
1735 async_init_call_start_service_by_name (data);
1741 async_initable_init_second_finish (GAsyncInitable *initable,
1745 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1746 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1752 if (g_simple_async_result_propagate_error (simple, error))
1755 result = g_simple_async_result_get_op_res_gpointer (simple);
1758 process_get_all_reply (proxy, result);
1764 proxy->priv->initialized = TRUE;
1768 /* ---------------------------------------------------------------------------------------------------- */
1771 async_initable_init_first (GAsyncInitable *initable)
1773 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1775 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES))
1777 /* subscribe to PropertiesChanged() */
1778 proxy->priv->properties_changed_subscription_id =
1779 g_dbus_connection_signal_subscribe (proxy->priv->connection,
1781 "org.freedesktop.DBus.Properties",
1782 "PropertiesChanged",
1783 proxy->priv->object_path,
1784 proxy->priv->interface_name,
1785 G_DBUS_SIGNAL_FLAGS_NONE,
1786 on_properties_changed,
1787 signal_subscription_ref (proxy->priv->signal_subscription_data),
1788 (GDestroyNotify) signal_subscription_unref);
1791 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS))
1793 /* subscribe to all signals for the object */
1794 proxy->priv->signals_subscription_id =
1795 g_dbus_connection_signal_subscribe (proxy->priv->connection,
1797 proxy->priv->interface_name,
1799 proxy->priv->object_path,
1801 G_DBUS_SIGNAL_FLAGS_NONE,
1803 signal_subscription_ref (proxy->priv->signal_subscription_data),
1804 (GDestroyNotify) signal_subscription_unref);
1807 if (proxy->priv->name != NULL && !g_dbus_is_unique_name (proxy->priv->name))
1809 proxy->priv->name_owner_changed_subscription_id =
1810 g_dbus_connection_signal_subscribe (proxy->priv->connection,
1811 "org.freedesktop.DBus", /* name */
1812 "org.freedesktop.DBus", /* interface */
1813 "NameOwnerChanged", /* signal name */
1814 "/org/freedesktop/DBus", /* path */
1815 proxy->priv->name, /* arg0 */
1816 G_DBUS_SIGNAL_FLAGS_NONE,
1817 on_name_owner_changed,
1818 signal_subscription_ref (proxy->priv->signal_subscription_data),
1819 (GDestroyNotify) signal_subscription_unref);
1823 /* ---------------------------------------------------------------------------------------------------- */
1825 /* initialization is split into two parts - the first is the
1826 * non-blocing part that requires the callers GMainContext - the
1827 * second is a blocking part async part that doesn't require the
1828 * callers GMainContext.. we do this split so the code can be reused
1829 * in the GInitable implementation below.
1831 * Note that obtaining a GDBusConnection is not shared between the two
1839 GCancellable *cancellable;
1840 GAsyncReadyCallback callback;
1842 } GetConnectionData;
1845 get_connection_cb (GObject *source_object,
1849 GetConnectionData *data = user_data;
1853 data->proxy->priv->connection = g_bus_get_finish (res, &error);
1854 if (data->proxy->priv->connection == NULL)
1856 GSimpleAsyncResult *simple;
1857 simple = g_simple_async_result_new (G_OBJECT (data->proxy),
1861 g_simple_async_result_set_check_cancellable (simple, data->cancellable);
1862 g_simple_async_result_take_error (simple, error);
1863 g_simple_async_result_complete_in_idle (simple);
1864 g_object_unref (simple);
1868 async_initable_init_first (G_ASYNC_INITABLE (data->proxy));
1869 async_initable_init_second_async (G_ASYNC_INITABLE (data->proxy),
1876 if (data->cancellable != NULL)
1877 g_object_unref (data->cancellable);
1879 g_object_unref (data->proxy);
1884 async_initable_init_async (GAsyncInitable *initable,
1886 GCancellable *cancellable,
1887 GAsyncReadyCallback callback,
1890 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1892 if (proxy->priv->bus_type != G_BUS_TYPE_NONE)
1894 GetConnectionData *data;
1896 g_assert (proxy->priv->connection == NULL);
1898 data = g_new0 (GetConnectionData, 1);
1899 data->proxy = g_object_ref (proxy);
1900 data->io_priority = io_priority;
1901 data->cancellable = cancellable != NULL ? g_object_ref (cancellable) : NULL;
1902 data->callback = callback;
1903 data->user_data = user_data;
1904 g_bus_get (proxy->priv->bus_type,
1911 async_initable_init_first (initable);
1912 async_initable_init_second_async (initable, io_priority, cancellable, callback, user_data);
1917 async_initable_init_finish (GAsyncInitable *initable,
1921 return async_initable_init_second_finish (initable, res, error);
1925 async_initable_iface_init (GAsyncInitableIface *async_initable_iface)
1927 async_initable_iface->init_async = async_initable_init_async;
1928 async_initable_iface->init_finish = async_initable_init_finish;
1931 /* ---------------------------------------------------------------------------------------------------- */
1935 GMainContext *context;
1938 } InitableAsyncInitableData;
1941 async_initable_init_async_cb (GObject *source_object,
1945 InitableAsyncInitableData *data = user_data;
1946 data->res = g_object_ref (res);
1947 g_main_loop_quit (data->loop);
1950 /* Simply reuse the GAsyncInitable implementation but run the first
1951 * part (that is non-blocking and requires the callers GMainContext)
1952 * with the callers GMainContext.. and the second with a private
1953 * GMainContext (bug 621310 is slightly related).
1955 * Note that obtaining a GDBusConnection is not shared between the two
1959 initable_init (GInitable *initable,
1960 GCancellable *cancellable,
1963 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1964 InitableAsyncInitableData *data;
1969 if (proxy->priv->bus_type != G_BUS_TYPE_NONE)
1971 g_assert (proxy->priv->connection == NULL);
1972 proxy->priv->connection = g_bus_get_sync (proxy->priv->bus_type,
1975 if (proxy->priv->connection == NULL)
1979 async_initable_init_first (G_ASYNC_INITABLE (initable));
1981 data = g_new0 (InitableAsyncInitableData, 1);
1982 data->context = g_main_context_new ();
1983 data->loop = g_main_loop_new (data->context, FALSE);
1985 g_main_context_push_thread_default (data->context);
1987 async_initable_init_second_async (G_ASYNC_INITABLE (initable),
1990 async_initable_init_async_cb,
1993 g_main_loop_run (data->loop);
1995 ret = async_initable_init_second_finish (G_ASYNC_INITABLE (initable),
1999 g_main_context_pop_thread_default (data->context);
2001 g_main_context_unref (data->context);
2002 g_main_loop_unref (data->loop);
2003 g_object_unref (data->res);
2012 initable_iface_init (GInitableIface *initable_iface)
2014 initable_iface->init = initable_init;
2017 /* ---------------------------------------------------------------------------------------------------- */
2021 * @connection: A #GDBusConnection.
2022 * @flags: Flags used when constructing the proxy.
2023 * @info: (allow-none): A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
2024 * @name: (allow-none): A bus name (well-known or unique) or %NULL if @connection is not a message bus connection.
2025 * @object_path: An object path.
2026 * @interface_name: A D-Bus interface name.
2027 * @cancellable: (allow-none): A #GCancellable or %NULL.
2028 * @callback: Callback function to invoke when the proxy is ready.
2029 * @user_data: User data to pass to @callback.
2031 * Creates a proxy for accessing @interface_name on the remote object
2032 * at @object_path owned by @name at @connection and asynchronously
2033 * loads D-Bus properties unless the
2034 * %G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES flag is used. Connect to
2035 * the #GDBusProxy::g-properties-changed signal to get notified about
2038 * If the %G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS flag is not set, also sets up
2039 * match rules for signals. Connect to the #GDBusProxy::g-signal signal
2040 * to handle signals from the remote object.
2042 * If @name is a well-known name and the
2043 * %G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START and %G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START_AT_CONSTRUCTION
2044 * flags aren't set and no name owner currently exists, the message bus
2045 * will be requested to launch a name owner for the name.
2047 * This is a failable asynchronous constructor - when the proxy is
2048 * ready, @callback will be invoked and you can use
2049 * g_dbus_proxy_new_finish() to get the result.
2051 * See g_dbus_proxy_new_sync() and for a synchronous version of this constructor.
2053 * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
2058 g_dbus_proxy_new (GDBusConnection *connection,
2059 GDBusProxyFlags flags,
2060 GDBusInterfaceInfo *info,
2062 const gchar *object_path,
2063 const gchar *interface_name,
2064 GCancellable *cancellable,
2065 GAsyncReadyCallback callback,
2068 g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
2069 g_return_if_fail ((name == NULL && g_dbus_connection_get_unique_name (connection) == NULL) || g_dbus_is_name (name));
2070 g_return_if_fail (g_variant_is_object_path (object_path));
2071 g_return_if_fail (g_dbus_is_interface_name (interface_name));
2073 g_async_initable_new_async (G_TYPE_DBUS_PROXY,
2079 "g-interface-info", info,
2081 "g-connection", connection,
2082 "g-object-path", object_path,
2083 "g-interface-name", interface_name,
2088 * g_dbus_proxy_new_finish:
2089 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback function passed to g_dbus_proxy_new().
2090 * @error: Return location for error or %NULL.
2092 * Finishes creating a #GDBusProxy.
2094 * Returns: A #GDBusProxy or %NULL if @error is set. Free with g_object_unref().
2099 g_dbus_proxy_new_finish (GAsyncResult *res,
2103 GObject *source_object;
2105 source_object = g_async_result_get_source_object (res);
2106 g_assert (source_object != NULL);
2108 object = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object),
2111 g_object_unref (source_object);
2114 return G_DBUS_PROXY (object);
2120 * g_dbus_proxy_new_sync:
2121 * @connection: A #GDBusConnection.
2122 * @flags: Flags used when constructing the proxy.
2123 * @info: (allow-none): A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
2124 * @name: (allow-none): A bus name (well-known or unique) or %NULL if @connection is not a message bus connection.
2125 * @object_path: An object path.
2126 * @interface_name: A D-Bus interface name.
2127 * @cancellable: (allow-none): A #GCancellable or %NULL.
2128 * @error: (allow-none): Return location for error or %NULL.
2130 * Creates a proxy for accessing @interface_name on the remote object
2131 * at @object_path owned by @name at @connection and synchronously
2132 * loads D-Bus properties unless the
2133 * %G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES flag is used.
2135 * If the %G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS flag is not set, also sets up
2136 * match rules for signals. Connect to the #GDBusProxy::g-signal signal
2137 * to handle signals from the remote object.
2139 * If @name is a well-known name and the
2140 * %G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START and %G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START_AT_CONSTRUCTION
2141 * flags aren't set and no name owner currently exists, the message bus
2142 * will be requested to launch a name owner for the name.
2144 * This is a synchronous failable constructor. See g_dbus_proxy_new()
2145 * and g_dbus_proxy_new_finish() for the asynchronous version.
2147 * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
2149 * Returns: A #GDBusProxy or %NULL if error is set. Free with g_object_unref().
2154 g_dbus_proxy_new_sync (GDBusConnection *connection,
2155 GDBusProxyFlags flags,
2156 GDBusInterfaceInfo *info,
2158 const gchar *object_path,
2159 const gchar *interface_name,
2160 GCancellable *cancellable,
2163 GInitable *initable;
2165 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
2166 g_return_val_if_fail ((name == NULL && g_dbus_connection_get_unique_name (connection) == NULL) ||
2167 g_dbus_is_name (name), NULL);
2168 g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
2169 g_return_val_if_fail (g_dbus_is_interface_name (interface_name), NULL);
2171 initable = g_initable_new (G_TYPE_DBUS_PROXY,
2175 "g-interface-info", info,
2177 "g-connection", connection,
2178 "g-object-path", object_path,
2179 "g-interface-name", interface_name,
2181 if (initable != NULL)
2182 return G_DBUS_PROXY (initable);
2187 /* ---------------------------------------------------------------------------------------------------- */
2190 * g_dbus_proxy_new_for_bus:
2191 * @bus_type: A #GBusType.
2192 * @flags: Flags used when constructing the proxy.
2193 * @info: (allow-none): A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
2194 * @name: A bus name (well-known or unique).
2195 * @object_path: An object path.
2196 * @interface_name: A D-Bus interface name.
2197 * @cancellable: (allow-none): A #GCancellable or %NULL.
2198 * @callback: Callback function to invoke when the proxy is ready.
2199 * @user_data: User data to pass to @callback.
2201 * Like g_dbus_proxy_new() but takes a #GBusType instead of a #GDBusConnection.
2203 * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
2208 g_dbus_proxy_new_for_bus (GBusType bus_type,
2209 GDBusProxyFlags flags,
2210 GDBusInterfaceInfo *info,
2212 const gchar *object_path,
2213 const gchar *interface_name,
2214 GCancellable *cancellable,
2215 GAsyncReadyCallback callback,
2218 g_return_if_fail (g_dbus_is_name (name));
2219 g_return_if_fail (g_variant_is_object_path (object_path));
2220 g_return_if_fail (g_dbus_is_interface_name (interface_name));
2222 g_async_initable_new_async (G_TYPE_DBUS_PROXY,
2228 "g-interface-info", info,
2230 "g-bus-type", bus_type,
2231 "g-object-path", object_path,
2232 "g-interface-name", interface_name,
2237 * g_dbus_proxy_new_for_bus_finish:
2238 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback function passed to g_dbus_proxy_new_for_bus().
2239 * @error: Return location for error or %NULL.
2241 * Finishes creating a #GDBusProxy.
2243 * Returns: A #GDBusProxy or %NULL if @error is set. Free with g_object_unref().
2248 g_dbus_proxy_new_for_bus_finish (GAsyncResult *res,
2251 return g_dbus_proxy_new_finish (res, error);
2255 * g_dbus_proxy_new_for_bus_sync:
2256 * @bus_type: A #GBusType.
2257 * @flags: Flags used when constructing the proxy.
2258 * @info: (allow-none): A #GDBusInterfaceInfo specifying the minimal interface
2259 * that @proxy conforms to or %NULL.
2260 * @name: A bus name (well-known or unique).
2261 * @object_path: An object path.
2262 * @interface_name: A D-Bus interface name.
2263 * @cancellable: (allow-none): A #GCancellable or %NULL.
2264 * @error: Return location for error or %NULL.
2266 * Like g_dbus_proxy_new_sync() but takes a #GBusType instead of a #GDBusConnection.
2268 * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
2270 * Returns: A #GDBusProxy or %NULL if error is set. Free with g_object_unref().
2275 g_dbus_proxy_new_for_bus_sync (GBusType bus_type,
2276 GDBusProxyFlags flags,
2277 GDBusInterfaceInfo *info,
2279 const gchar *object_path,
2280 const gchar *interface_name,
2281 GCancellable *cancellable,
2284 GInitable *initable;
2286 g_return_val_if_fail (g_dbus_is_name (name), NULL);
2287 g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
2288 g_return_val_if_fail (g_dbus_is_interface_name (interface_name), NULL);
2290 initable = g_initable_new (G_TYPE_DBUS_PROXY,
2294 "g-interface-info", info,
2296 "g-bus-type", bus_type,
2297 "g-object-path", object_path,
2298 "g-interface-name", interface_name,
2300 if (initable != NULL)
2301 return G_DBUS_PROXY (initable);
2306 /* ---------------------------------------------------------------------------------------------------- */
2309 * g_dbus_proxy_get_connection:
2310 * @proxy: A #GDBusProxy.
2312 * Gets the connection @proxy is for.
2314 * Returns: (transfer none): A #GDBusConnection owned by @proxy. Do not free.
2319 g_dbus_proxy_get_connection (GDBusProxy *proxy)
2321 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2322 return proxy->priv->connection;
2326 * g_dbus_proxy_get_flags:
2327 * @proxy: A #GDBusProxy.
2329 * Gets the flags that @proxy was constructed with.
2331 * Returns: Flags from the #GDBusProxyFlags enumeration.
2336 g_dbus_proxy_get_flags (GDBusProxy *proxy)
2338 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), 0);
2339 return proxy->priv->flags;
2343 * g_dbus_proxy_get_name:
2344 * @proxy: A #GDBusProxy.
2346 * Gets the name that @proxy was constructed for.
2348 * Returns: A string owned by @proxy. Do not free.
2353 g_dbus_proxy_get_name (GDBusProxy *proxy)
2355 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2356 return proxy->priv->name;
2360 * g_dbus_proxy_get_name_owner:
2361 * @proxy: A #GDBusProxy.
2363 * The unique name that owns the name that @proxy is for or %NULL if
2364 * no-one currently owns that name. You may connect to the
2365 * #GObject::notify signal to track changes to the
2366 * #GDBusProxy:g-name-owner property.
2368 * Returns: The name owner or %NULL if no name owner exists. Free with g_free().
2373 g_dbus_proxy_get_name_owner (GDBusProxy *proxy)
2377 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2379 G_LOCK (properties_lock);
2380 ret = g_strdup (proxy->priv->name_owner);
2381 G_UNLOCK (properties_lock);
2386 * g_dbus_proxy_get_object_path:
2387 * @proxy: A #GDBusProxy.
2389 * Gets the object path @proxy is for.
2391 * Returns: A string owned by @proxy. Do not free.
2396 g_dbus_proxy_get_object_path (GDBusProxy *proxy)
2398 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2399 return proxy->priv->object_path;
2403 * g_dbus_proxy_get_interface_name:
2404 * @proxy: A #GDBusProxy.
2406 * Gets the D-Bus interface name @proxy is for.
2408 * Returns: A string owned by @proxy. Do not free.
2413 g_dbus_proxy_get_interface_name (GDBusProxy *proxy)
2415 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2416 return proxy->priv->interface_name;
2420 * g_dbus_proxy_get_default_timeout:
2421 * @proxy: A #GDBusProxy.
2423 * Gets the timeout to use if -1 (specifying default timeout) is
2424 * passed as @timeout_msec in the g_dbus_proxy_call() and
2425 * g_dbus_proxy_call_sync() functions.
2427 * See the #GDBusProxy:g-default-timeout property for more details.
2429 * Returns: Timeout to use for @proxy.
2434 g_dbus_proxy_get_default_timeout (GDBusProxy *proxy)
2438 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), -1);
2440 G_LOCK (properties_lock);
2441 ret = proxy->priv->timeout_msec;
2442 G_UNLOCK (properties_lock);
2447 * g_dbus_proxy_set_default_timeout:
2448 * @proxy: A #GDBusProxy.
2449 * @timeout_msec: Timeout in milliseconds.
2451 * Sets the timeout to use if -1 (specifying default timeout) is
2452 * passed as @timeout_msec in the g_dbus_proxy_call() and
2453 * g_dbus_proxy_call_sync() functions.
2455 * See the #GDBusProxy:g-default-timeout property for more details.
2460 g_dbus_proxy_set_default_timeout (GDBusProxy *proxy,
2463 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
2464 g_return_if_fail (timeout_msec == -1 || timeout_msec >= 0);
2466 G_LOCK (properties_lock);
2468 if (proxy->priv->timeout_msec != timeout_msec)
2470 proxy->priv->timeout_msec = timeout_msec;
2471 G_UNLOCK (properties_lock);
2473 g_object_notify (G_OBJECT (proxy), "g-default-timeout");
2477 G_UNLOCK (properties_lock);
2482 * g_dbus_proxy_get_interface_info:
2483 * @proxy: A #GDBusProxy
2485 * Returns the #GDBusInterfaceInfo, if any, specifying the interface
2486 * that @proxy conforms to. See the #GDBusProxy:g-interface-info
2487 * property for more details.
2489 * Returns: A #GDBusInterfaceInfo or %NULL. Do not unref the returned
2490 * object, it is owned by @proxy.
2494 GDBusInterfaceInfo *
2495 g_dbus_proxy_get_interface_info (GDBusProxy *proxy)
2497 GDBusInterfaceInfo *ret;
2499 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2501 G_LOCK (properties_lock);
2502 ret = proxy->priv->expected_interface;
2503 G_UNLOCK (properties_lock);
2504 /* FIXME: returning a borrowed ref with no guarantee that nobody will
2505 * call g_dbus_proxy_set_interface_info() and make it invalid...
2511 * g_dbus_proxy_set_interface_info:
2512 * @proxy: A #GDBusProxy
2513 * @info: (allow-none): Minimum interface this proxy conforms to or %NULL to unset.
2515 * Ensure that interactions with @proxy conform to the given
2516 * interface. See the #GDBusProxy:g-interface-info property for more
2522 g_dbus_proxy_set_interface_info (GDBusProxy *proxy,
2523 GDBusInterfaceInfo *info)
2525 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
2526 G_LOCK (properties_lock);
2528 if (proxy->priv->expected_interface != NULL)
2530 g_dbus_interface_info_cache_release (proxy->priv->expected_interface);
2531 g_dbus_interface_info_unref (proxy->priv->expected_interface);
2533 proxy->priv->expected_interface = info != NULL ? g_dbus_interface_info_ref (info) : NULL;
2534 if (proxy->priv->expected_interface != NULL)
2535 g_dbus_interface_info_cache_build (proxy->priv->expected_interface);
2537 G_UNLOCK (properties_lock);
2540 /* ---------------------------------------------------------------------------------------------------- */
2543 maybe_split_method_name (const gchar *method_name,
2544 gchar **out_interface_name,
2545 const gchar **out_method_name)
2550 g_assert (out_interface_name != NULL);
2551 g_assert (out_method_name != NULL);
2552 *out_interface_name = NULL;
2553 *out_method_name = NULL;
2555 if (strchr (method_name, '.') != NULL)
2560 p = g_strdup (method_name);
2561 last_dot = strrchr (p, '.');
2564 *out_interface_name = p;
2565 *out_method_name = last_dot + 1;
2577 GUnixFDList *fd_list;
2582 reply_data_free (ReplyData *data)
2584 g_variant_unref (data->value);
2586 if (data->fd_list != NULL)
2587 g_object_unref (data->fd_list);
2589 g_slice_free (ReplyData, data);
2593 reply_cb (GDBusConnection *connection,
2597 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
2601 GUnixFDList *fd_list;
2606 value = g_dbus_connection_call_with_unix_fd_list_finish (connection,
2611 value = g_dbus_connection_call_finish (connection,
2617 g_simple_async_result_take_error (simple, error);
2622 data = g_slice_new0 (ReplyData);
2623 data->value = value;
2625 data->fd_list = fd_list;
2627 g_simple_async_result_set_op_res_gpointer (simple, data, (GDestroyNotify) reply_data_free);
2630 /* no need to complete in idle since the method GDBusConnection already does */
2631 g_simple_async_result_complete (simple);
2632 g_object_unref (simple);
2635 /* properties_lock must be held for as long as you will keep the
2638 static const GDBusMethodInfo *
2639 lookup_method_info (GDBusProxy *proxy,
2640 const gchar *method_name)
2642 const GDBusMethodInfo *info = NULL;
2644 if (proxy->priv->expected_interface == NULL)
2647 info = g_dbus_interface_info_lookup_method (proxy->priv->expected_interface, method_name);
2653 /* properties_lock must be held for as long as you will keep the
2656 static const gchar *
2657 get_destination_for_call (GDBusProxy *proxy)
2663 /* If proxy->priv->name is a unique name, then proxy->priv->name_owner
2664 * is never NULL and always the same as proxy->priv->name. We use this
2665 * knowledge to avoid checking if proxy->priv->name is a unique or
2668 ret = proxy->priv->name_owner;
2672 if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START)
2675 ret = proxy->priv->name;
2681 /* ---------------------------------------------------------------------------------------------------- */
2684 g_dbus_proxy_call_internal (GDBusProxy *proxy,
2685 const gchar *method_name,
2686 GVariant *parameters,
2687 GDBusCallFlags flags,
2689 GUnixFDList *fd_list,
2690 GCancellable *cancellable,
2691 GAsyncReadyCallback callback,
2694 GSimpleAsyncResult *simple;
2696 gchar *split_interface_name;
2697 const gchar *split_method_name;
2698 const gchar *target_method_name;
2699 const gchar *target_interface_name;
2701 GVariantType *reply_type;
2702 GAsyncReadyCallback my_callback;
2704 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
2705 g_return_if_fail (g_dbus_is_member_name (method_name) || g_dbus_is_interface_name (method_name));
2706 g_return_if_fail (parameters == NULL || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE));
2707 g_return_if_fail (timeout_msec == -1 || timeout_msec >= 0);
2709 g_return_if_fail (fd_list == NULL || G_IS_UNIX_FD_LIST (fd_list));
2711 g_return_if_fail (fd_list == NULL);
2715 split_interface_name = NULL;
2717 /* g_dbus_connection_call() is optimised for the case of a NULL
2718 * callback. If we get a NULL callback from our user then make sure
2719 * we pass along a NULL callback for ourselves as well.
2721 if (callback != NULL)
2723 my_callback = (GAsyncReadyCallback) reply_cb;
2724 simple = g_simple_async_result_new (G_OBJECT (proxy),
2727 g_dbus_proxy_call_internal);
2728 g_simple_async_result_set_check_cancellable (simple, cancellable);
2736 G_LOCK (properties_lock);
2738 was_split = maybe_split_method_name (method_name, &split_interface_name, &split_method_name);
2739 target_method_name = was_split ? split_method_name : method_name;
2740 target_interface_name = was_split ? split_interface_name : proxy->priv->interface_name;
2742 /* Warn if method is unexpected (cf. :g-interface-info) */
2745 const GDBusMethodInfo *expected_method_info;
2746 expected_method_info = lookup_method_info (proxy, target_method_name);
2747 if (expected_method_info != NULL)
2748 reply_type = _g_dbus_compute_complete_signature (expected_method_info->out_args);
2752 if (proxy->priv->name != NULL)
2754 destination = g_strdup (get_destination_for_call (proxy));
2755 if (destination == NULL)
2759 g_simple_async_result_set_error (simple,
2762 _("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"));
2763 g_simple_async_result_complete_in_idle (simple);
2764 g_object_unref (simple);
2766 G_UNLOCK (properties_lock);
2771 G_UNLOCK (properties_lock);
2774 g_dbus_connection_call_with_unix_fd_list (proxy->priv->connection,
2776 proxy->priv->object_path,
2777 target_interface_name,
2782 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2788 g_dbus_connection_call (proxy->priv->connection,
2790 proxy->priv->object_path,
2791 target_interface_name,
2796 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2803 if (reply_type != NULL)
2804 g_variant_type_free (reply_type);
2806 g_free (destination);
2807 g_free (split_interface_name);
2811 g_dbus_proxy_call_finish_internal (GDBusProxy *proxy,
2812 GUnixFDList **out_fd_list,
2816 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2820 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2821 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2822 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2824 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_dbus_proxy_call_internal);
2828 if (g_simple_async_result_propagate_error (simple, error))
2831 data = g_simple_async_result_get_op_res_gpointer (simple);
2832 value = g_variant_ref (data->value);
2834 if (out_fd_list != NULL)
2835 *out_fd_list = data->fd_list != NULL ? g_object_ref (data->fd_list) : NULL;
2843 g_dbus_proxy_call_sync_internal (GDBusProxy *proxy,
2844 const gchar *method_name,
2845 GVariant *parameters,
2846 GDBusCallFlags flags,
2848 GUnixFDList *fd_list,
2849 GUnixFDList **out_fd_list,
2850 GCancellable *cancellable,
2855 gchar *split_interface_name;
2856 const gchar *split_method_name;
2857 const gchar *target_method_name;
2858 const gchar *target_interface_name;
2860 GVariantType *reply_type;
2862 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2863 g_return_val_if_fail (g_dbus_is_member_name (method_name) || g_dbus_is_interface_name (method_name), NULL);
2864 g_return_val_if_fail (parameters == NULL || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE), NULL);
2865 g_return_val_if_fail (timeout_msec == -1 || timeout_msec >= 0, NULL);
2867 g_return_val_if_fail (fd_list == NULL || G_IS_UNIX_FD_LIST (fd_list), NULL);
2869 g_return_val_if_fail (fd_list == NULL, NULL);
2871 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2875 G_LOCK (properties_lock);
2877 was_split = maybe_split_method_name (method_name, &split_interface_name, &split_method_name);
2878 target_method_name = was_split ? split_method_name : method_name;
2879 target_interface_name = was_split ? split_interface_name : proxy->priv->interface_name;
2881 /* Warn if method is unexpected (cf. :g-interface-info) */
2884 const GDBusMethodInfo *expected_method_info;
2885 expected_method_info = lookup_method_info (proxy, target_method_name);
2886 if (expected_method_info != NULL)
2887 reply_type = _g_dbus_compute_complete_signature (expected_method_info->out_args);
2891 if (proxy->priv->name != NULL)
2893 destination = g_strdup (get_destination_for_call (proxy));
2894 if (destination == NULL)
2896 g_set_error_literal (error,
2899 _("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"));
2901 G_UNLOCK (properties_lock);
2906 G_UNLOCK (properties_lock);
2909 ret = g_dbus_connection_call_with_unix_fd_list_sync (proxy->priv->connection,
2911 proxy->priv->object_path,
2912 target_interface_name,
2917 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2923 ret = g_dbus_connection_call_sync (proxy->priv->connection,
2925 proxy->priv->object_path,
2926 target_interface_name,
2931 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2937 if (reply_type != NULL)
2938 g_variant_type_free (reply_type);
2940 g_free (destination);
2941 g_free (split_interface_name);
2946 /* ---------------------------------------------------------------------------------------------------- */
2949 * g_dbus_proxy_call:
2950 * @proxy: A #GDBusProxy.
2951 * @method_name: Name of method to invoke.
2952 * @parameters: (allow-none): A #GVariant tuple with parameters for the signal or %NULL if not passing parameters.
2953 * @flags: Flags from the #GDBusCallFlags enumeration.
2954 * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
2955 * "infinite") or -1 to use the proxy default timeout.
2956 * @cancellable: (allow-none): A #GCancellable or %NULL.
2957 * @callback: (allow-none): A #GAsyncReadyCallback to call when the request is satisfied or %NULL if you don't
2958 * care about the result of the method invocation.
2959 * @user_data: The data to pass to @callback.
2961 * Asynchronously invokes the @method_name method on @proxy.
2963 * If @method_name contains any dots, then @name is split into interface and
2964 * method name parts. This allows using @proxy for invoking methods on
2967 * If the #GDBusConnection associated with @proxy is closed then
2968 * the operation will fail with %G_IO_ERROR_CLOSED. If
2969 * @cancellable is canceled, the operation will fail with
2970 * %G_IO_ERROR_CANCELLED. If @parameters contains a value not
2971 * compatible with the D-Bus protocol, the operation fails with
2972 * %G_IO_ERROR_INVALID_ARGUMENT.
2974 * If the @parameters #GVariant is floating, it is consumed. This allows
2975 * convenient 'inline' use of g_variant_new(), e.g.:
2977 * g_dbus_proxy_call (proxy,
2979 * g_variant_new ("(ss)",
2982 * G_DBUS_CALL_FLAGS_NONE,
2985 * (GAsyncReadyCallback) two_strings_done,
2989 * If @proxy has an expected interface (see
2990 * #GDBusProxy:g-interface-info) and @method_name is referenced by it,
2991 * then the return value is checked against the return type.
2993 * This is an asynchronous method. When the operation is finished,
2994 * @callback will be invoked in the
2995 * <link linkend="g-main-context-push-thread-default">thread-default main loop</link>
2996 * of the thread you are calling this method from.
2997 * You can then call g_dbus_proxy_call_finish() to get the result of
2998 * the operation. See g_dbus_proxy_call_sync() for the synchronous
2999 * version of this method.
3001 * If @callback is %NULL then the D-Bus method call message will be sent with
3002 * the %G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED flag set.
3007 g_dbus_proxy_call (GDBusProxy *proxy,
3008 const gchar *method_name,
3009 GVariant *parameters,
3010 GDBusCallFlags flags,
3012 GCancellable *cancellable,
3013 GAsyncReadyCallback callback,
3016 g_dbus_proxy_call_internal (proxy, method_name, parameters, flags, timeout_msec, NULL, cancellable, callback, user_data);
3020 * g_dbus_proxy_call_finish:
3021 * @proxy: A #GDBusProxy.
3022 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_proxy_call().
3023 * @error: Return location for error or %NULL.
3025 * Finishes an operation started with g_dbus_proxy_call().
3027 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
3028 * return values. Free with g_variant_unref().
3033 g_dbus_proxy_call_finish (GDBusProxy *proxy,
3037 return g_dbus_proxy_call_finish_internal (proxy, NULL, res, error);
3041 * g_dbus_proxy_call_sync:
3042 * @proxy: A #GDBusProxy.
3043 * @method_name: Name of method to invoke.
3044 * @parameters: (allow-none): A #GVariant tuple with parameters for the signal
3045 * or %NULL if not passing parameters.
3046 * @flags: Flags from the #GDBusCallFlags enumeration.
3047 * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
3048 * "infinite") or -1 to use the proxy default timeout.
3049 * @cancellable: (allow-none): A #GCancellable or %NULL.
3050 * @error: Return location for error or %NULL.
3052 * Synchronously invokes the @method_name method on @proxy.
3054 * If @method_name contains any dots, then @name is split into interface and
3055 * method name parts. This allows using @proxy for invoking methods on
3058 * If the #GDBusConnection associated with @proxy is disconnected then
3059 * the operation will fail with %G_IO_ERROR_CLOSED. If
3060 * @cancellable is canceled, the operation will fail with
3061 * %G_IO_ERROR_CANCELLED. If @parameters contains a value not
3062 * compatible with the D-Bus protocol, the operation fails with
3063 * %G_IO_ERROR_INVALID_ARGUMENT.
3065 * If the @parameters #GVariant is floating, it is consumed. This allows
3066 * convenient 'inline' use of g_variant_new(), e.g.:
3068 * g_dbus_proxy_call_sync (proxy,
3070 * g_variant_new ("(ss)",
3073 * G_DBUS_CALL_FLAGS_NONE,
3079 * The calling thread is blocked until a reply is received. See
3080 * g_dbus_proxy_call() for the asynchronous version of this
3083 * If @proxy has an expected interface (see
3084 * #GDBusProxy:g-interface-info) and @method_name is referenced by it,
3085 * then the return value is checked against the return type.
3087 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
3088 * return values. Free with g_variant_unref().
3093 g_dbus_proxy_call_sync (GDBusProxy *proxy,
3094 const gchar *method_name,
3095 GVariant *parameters,
3096 GDBusCallFlags flags,
3098 GCancellable *cancellable,
3101 return g_dbus_proxy_call_sync_internal (proxy, method_name, parameters, flags, timeout_msec, NULL, NULL, cancellable, error);
3104 /* ---------------------------------------------------------------------------------------------------- */
3109 * g_dbus_proxy_call_with_unix_fd_list:
3110 * @proxy: A #GDBusProxy.
3111 * @method_name: Name of method to invoke.
3112 * @parameters: (allow-none): A #GVariant tuple with parameters for the signal or %NULL if not passing parameters.
3113 * @flags: Flags from the #GDBusCallFlags enumeration.
3114 * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
3115 * "infinite") or -1 to use the proxy default timeout.
3116 * @fd_list: (allow-none): A #GUnixFDList or %NULL.
3117 * @cancellable: (allow-none): A #GCancellable or %NULL.
3118 * @callback: (allow-none): A #GAsyncReadyCallback to call when the request is satisfied or %NULL if you don't
3119 * care about the result of the method invocation.
3120 * @user_data: The data to pass to @callback.
3122 * Like g_dbus_proxy_call() but also takes a #GUnixFDList object.
3124 * This method is only available on UNIX.
3129 g_dbus_proxy_call_with_unix_fd_list (GDBusProxy *proxy,
3130 const gchar *method_name,
3131 GVariant *parameters,
3132 GDBusCallFlags flags,
3134 GUnixFDList *fd_list,
3135 GCancellable *cancellable,
3136 GAsyncReadyCallback callback,
3139 g_dbus_proxy_call_internal (proxy, method_name, parameters, flags, timeout_msec, fd_list, cancellable, callback, user_data);
3143 * g_dbus_proxy_call_with_unix_fd_list_finish:
3144 * @proxy: A #GDBusProxy.
3145 * @out_fd_list: (out) (allow-none): Return location for a #GUnixFDList or %NULL.
3146 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_proxy_call_with_unix_fd_list().
3147 * @error: Return location for error or %NULL.
3149 * Finishes an operation started with g_dbus_proxy_call_with_unix_fd_list().
3151 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
3152 * return values. Free with g_variant_unref().
3157 g_dbus_proxy_call_with_unix_fd_list_finish (GDBusProxy *proxy,
3158 GUnixFDList **out_fd_list,
3162 return g_dbus_proxy_call_finish_internal (proxy, out_fd_list, res, error);
3166 * g_dbus_proxy_call_with_unix_fd_list_sync:
3167 * @proxy: A #GDBusProxy.
3168 * @method_name: Name of method to invoke.
3169 * @parameters: (allow-none): A #GVariant tuple with parameters for the signal
3170 * or %NULL if not passing parameters.
3171 * @flags: Flags from the #GDBusCallFlags enumeration.
3172 * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
3173 * "infinite") or -1 to use the proxy default timeout.
3174 * @fd_list: (allow-none): A #GUnixFDList or %NULL.
3175 * @out_fd_list: (out) (allow-none): Return location for a #GUnixFDList or %NULL.
3176 * @cancellable: (allow-none): A #GCancellable or %NULL.
3177 * @error: Return location for error or %NULL.
3179 * Like g_dbus_proxy_call_sync() but also takes and returns #GUnixFDList objects.
3181 * This method is only available on UNIX.
3183 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
3184 * return values. Free with g_variant_unref().
3189 g_dbus_proxy_call_with_unix_fd_list_sync (GDBusProxy *proxy,
3190 const gchar *method_name,
3191 GVariant *parameters,
3192 GDBusCallFlags flags,
3194 GUnixFDList *fd_list,
3195 GUnixFDList **out_fd_list,
3196 GCancellable *cancellable,
3199 return g_dbus_proxy_call_sync_internal (proxy, method_name, parameters, flags, timeout_msec, fd_list, out_fd_list, cancellable, error);
3202 #endif /* G_OS_UNIX */
3204 /* ---------------------------------------------------------------------------------------------------- */
3206 static GDBusInterfaceInfo *
3207 _g_dbus_proxy_get_info (GDBusInterface *interface)
3209 GDBusProxy *proxy = G_DBUS_PROXY (interface);
3210 return g_dbus_proxy_get_interface_info (proxy);
3213 static GDBusObject *
3214 _g_dbus_proxy_get_object (GDBusInterface *interface)
3216 GDBusProxy *proxy = G_DBUS_PROXY (interface);
3217 return proxy->priv->object;
3220 static GDBusObject *
3221 _g_dbus_proxy_dup_object (GDBusInterface *interface)
3223 GDBusProxy *proxy = G_DBUS_PROXY (interface);
3224 GDBusObject *ret = NULL;
3226 G_LOCK (properties_lock);
3227 if (proxy->priv->object != NULL)
3228 ret = g_object_ref (proxy->priv->object);
3229 G_UNLOCK (properties_lock);
3234 _g_dbus_proxy_set_object (GDBusInterface *interface,
3235 GDBusObject *object)
3237 GDBusProxy *proxy = G_DBUS_PROXY (interface);
3238 G_LOCK (properties_lock);
3239 if (proxy->priv->object != NULL)
3240 g_object_remove_weak_pointer (G_OBJECT (proxy->priv->object), (gpointer *) &proxy->priv->object);
3241 proxy->priv->object = object;
3242 if (proxy->priv->object != NULL)
3243 g_object_add_weak_pointer (G_OBJECT (proxy->priv->object), (gpointer *) &proxy->priv->object);
3244 G_UNLOCK (properties_lock);
3248 dbus_interface_iface_init (GDBusInterfaceIface *dbus_interface_iface)
3250 dbus_interface_iface->get_info = _g_dbus_proxy_get_info;
3251 dbus_interface_iface->get_object = _g_dbus_proxy_get_object;
3252 dbus_interface_iface->dup_object = _g_dbus_proxy_dup_object;
3253 dbus_interface_iface->set_object = _g_dbus_proxy_set_object;
3256 /* ---------------------------------------------------------------------------------------------------- */