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 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_IMPLEMENT_INTERFACE (G_TYPE_DBUS_INTERFACE, dbus_interface_iface_init)
191 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, initable_iface_init)
192 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: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 * This signal corresponds to the
591 * <literal>PropertiesChanged</literal> D-Bus signal on the
592 * <literal>org.freedesktop.DBus.Properties</literal> interface.
596 signals[PROPERTIES_CHANGED_SIGNAL] = g_signal_new ("g-properties-changed",
598 G_SIGNAL_RUN_LAST | G_SIGNAL_MUST_COLLECT,
599 G_STRUCT_OFFSET (GDBusProxyClass, g_properties_changed),
606 G_TYPE_STRV | G_SIGNAL_TYPE_STATIC_SCOPE);
609 * GDBusProxy::g-signal:
610 * @proxy: The #GDBusProxy emitting the signal.
611 * @sender_name: The sender of the signal or %NULL if the connection is not a bus connection.
612 * @signal_name: The name of the signal.
613 * @parameters: A #GVariant tuple with parameters for the signal.
615 * Emitted when a signal from the remote object and interface that @proxy is for, has been received.
619 signals[SIGNAL_SIGNAL] = g_signal_new ("g-signal",
621 G_SIGNAL_RUN_LAST | G_SIGNAL_MUST_COLLECT,
622 G_STRUCT_OFFSET (GDBusProxyClass, g_signal),
633 g_type_class_add_private (klass, sizeof (GDBusProxyPrivate));
637 g_dbus_proxy_init (GDBusProxy *proxy)
639 proxy->priv = G_TYPE_INSTANCE_GET_PRIVATE (proxy, G_TYPE_DBUS_PROXY, GDBusProxyPrivate);
640 proxy->priv->signal_subscription_data = g_slice_new0 (SignalSubscriptionData);
641 proxy->priv->signal_subscription_data->ref_count = 1;
642 proxy->priv->signal_subscription_data->proxy = proxy;
643 proxy->priv->properties = g_hash_table_new_full (g_str_hash,
646 (GDestroyNotify) g_variant_unref);
649 /* ---------------------------------------------------------------------------------------------------- */
652 property_name_sort_func (const gchar **a,
655 return g_strcmp0 (*a, *b);
659 * g_dbus_proxy_get_cached_property_names:
660 * @proxy: A #GDBusProxy.
662 * Gets the names of all cached properties on @proxy.
664 * Returns: (transfer full): A %NULL-terminated array of strings or %NULL if
665 * @proxy has no cached properties. Free the returned array with
671 g_dbus_proxy_get_cached_property_names (GDBusProxy *proxy)
678 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
680 G_LOCK (properties_lock);
683 if (g_hash_table_size (proxy->priv->properties) == 0)
686 p = g_ptr_array_new ();
688 g_hash_table_iter_init (&iter, proxy->priv->properties);
689 while (g_hash_table_iter_next (&iter, (gpointer) &key, NULL))
690 g_ptr_array_add (p, g_strdup (key));
691 g_ptr_array_sort (p, (GCompareFunc) property_name_sort_func);
692 g_ptr_array_add (p, NULL);
694 names = (gchar **) g_ptr_array_free (p, FALSE);
697 G_UNLOCK (properties_lock);
701 /* properties_lock must be held for as long as you will keep the
704 static const GDBusPropertyInfo *
705 lookup_property_info (GDBusProxy *proxy,
706 const gchar *property_name)
708 const GDBusPropertyInfo *info = NULL;
710 if (proxy->priv->expected_interface == NULL)
713 info = g_dbus_interface_info_lookup_property (proxy->priv->expected_interface, property_name);
720 * g_dbus_proxy_get_cached_property:
721 * @proxy: A #GDBusProxy.
722 * @property_name: Property name.
724 * Looks up the value for a property from the cache. This call does no
727 * If @proxy has an expected interface (see
728 * #GDBusProxy:g-interface-info) and @property_name is referenced by
729 * it, then @value is checked against the type of the property.
731 * Returns: A reference to the #GVariant instance that holds the value
732 * for @property_name or %NULL if the value is not in the cache. The
733 * returned reference must be freed with g_variant_unref().
738 g_dbus_proxy_get_cached_property (GDBusProxy *proxy,
739 const gchar *property_name)
741 const GDBusPropertyInfo *info;
744 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
745 g_return_val_if_fail (property_name != NULL, NULL);
747 G_LOCK (properties_lock);
749 value = g_hash_table_lookup (proxy->priv->properties, property_name);
753 info = lookup_property_info (proxy, property_name);
756 const gchar *type_string = g_variant_get_type_string (value);
757 if (g_strcmp0 (type_string, info->signature) != 0)
759 g_warning ("Trying to get property %s with type %s but according to the expected "
760 "interface the type is %s",
769 g_variant_ref (value);
772 G_UNLOCK (properties_lock);
777 * g_dbus_proxy_set_cached_property:
778 * @proxy: A #GDBusProxy
779 * @property_name: Property name.
780 * @value: (allow-none): Value for the property or %NULL to remove it from the cache.
782 * If @value is not %NULL, sets the cached value for the property with
783 * name @property_name to the value in @value.
785 * If @value is %NULL, then the cached value is removed from the
788 * If @proxy has an expected interface (see
789 * #GDBusProxy:g-interface-info) and @property_name is referenced by
790 * it, then @value is checked against the type of the property.
792 * If the @value #GVariant is floating, it is consumed. This allows
793 * convenient 'inline' use of g_variant_new(), e.g.
795 * g_dbus_proxy_set_cached_property (proxy,
797 * g_variant_new ("(si)",
802 * Normally you will not need to use this method since @proxy is
803 * tracking changes using the
804 * <literal>org.freedesktop.DBus.Properties.PropertiesChanged</literal>
805 * D-Bus signal. However, for performance reasons an object may decide
806 * to not use this signal for some properties and instead use a
807 * proprietary out-of-band mechanism to transmit changes.
809 * As a concrete example, consider an object with a property
810 * <literal>ChatroomParticipants</literal> which is an array of
811 * strings. Instead of transmitting the same (long) array every time
812 * the property changes, it is more efficient to only transmit the
813 * delta using e.g. signals <literal>ChatroomParticipantJoined(String
814 * name)</literal> and <literal>ChatroomParticipantParted(String
820 g_dbus_proxy_set_cached_property (GDBusProxy *proxy,
821 const gchar *property_name,
824 const GDBusPropertyInfo *info;
826 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
827 g_return_if_fail (property_name != NULL);
829 G_LOCK (properties_lock);
833 info = lookup_property_info (proxy, property_name);
836 if (g_strcmp0 (info->signature, g_variant_get_type_string (value)) != 0)
838 g_warning ("Trying to set property %s of type %s but according to the expected "
839 "interface the type is %s",
841 g_variant_get_type_string (value),
846 g_hash_table_insert (proxy->priv->properties,
847 g_strdup (property_name),
848 g_variant_ref_sink (value));
852 g_hash_table_remove (proxy->priv->properties, property_name);
856 G_UNLOCK (properties_lock);
859 /* ---------------------------------------------------------------------------------------------------- */
862 on_signal_received (GDBusConnection *connection,
863 const gchar *sender_name,
864 const gchar *object_path,
865 const gchar *interface_name,
866 const gchar *signal_name,
867 GVariant *parameters,
870 SignalSubscriptionData *data = user_data;
873 G_LOCK (signal_subscription_lock);
877 G_UNLOCK (signal_subscription_lock);
882 g_object_ref (proxy);
883 G_UNLOCK (signal_subscription_lock);
886 if (!proxy->priv->initialized)
889 G_LOCK (properties_lock);
891 if (proxy->priv->name_owner != NULL && g_strcmp0 (sender_name, proxy->priv->name_owner) != 0)
893 G_UNLOCK (properties_lock);
897 if (proxy->priv->expected_interface != NULL)
899 const GDBusSignalInfo *info;
900 info = g_dbus_interface_info_lookup_signal (proxy->priv->expected_interface, signal_name);
903 GVariantType *expected_type;
904 expected_type = _g_dbus_compute_complete_signature (info->args);
905 if (!g_variant_type_equal (expected_type, g_variant_get_type (parameters)))
907 gchar *expected_type_string = g_variant_type_dup_string (expected_type);
908 g_warning ("Dropping signal %s of type %s since the type from the expected interface is %s",
910 g_variant_get_type_string (parameters),
911 expected_type_string);
912 g_free (expected_type_string);
913 g_variant_type_free (expected_type);
914 G_UNLOCK (properties_lock);
917 g_variant_type_free (expected_type);
921 G_UNLOCK (properties_lock);
923 g_signal_emit (proxy,
924 signals[SIGNAL_SIGNAL],
932 g_object_unref (proxy);
935 /* ---------------------------------------------------------------------------------------------------- */
937 /* must hold properties_lock */
939 insert_property_checked (GDBusProxy *proxy,
940 gchar *property_name,
943 if (proxy->priv->expected_interface != NULL)
945 const GDBusPropertyInfo *info;
946 info = g_dbus_interface_info_lookup_property (proxy->priv->expected_interface, property_name);
947 /* Only check known properties */
950 /* Warn about properties with the wrong type */
951 if (g_strcmp0 (info->signature, g_variant_get_type_string (value)) != 0)
953 g_warning ("Received property %s with type %s does not match expected type "
954 "%s in the expected interface",
956 g_variant_get_type_string (value),
963 g_hash_table_insert (proxy->priv->properties,
964 property_name, /* adopts string */
965 value); /* adopts value */
970 g_variant_unref (value);
971 g_free (property_name);
975 on_properties_changed (GDBusConnection *connection,
976 const gchar *sender_name,
977 const gchar *object_path,
978 const gchar *interface_name,
979 const gchar *signal_name,
980 GVariant *parameters,
983 SignalSubscriptionData *data = user_data;
985 const gchar *interface_name_for_signal;
986 GVariant *changed_properties;
987 gchar **invalidated_properties;
993 changed_properties = NULL;
994 invalidated_properties = NULL;
996 G_LOCK (signal_subscription_lock);
1000 G_UNLOCK (signal_subscription_lock);
1005 g_object_ref (proxy);
1006 G_UNLOCK (signal_subscription_lock);
1009 if (!proxy->priv->initialized)
1012 G_LOCK (properties_lock);
1014 if (proxy->priv->name_owner != NULL && g_strcmp0 (sender_name, proxy->priv->name_owner) != 0)
1016 G_UNLOCK (properties_lock);
1020 if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(sa{sv}as)")))
1022 g_warning ("Value for PropertiesChanged signal with type `%s' does not match `(sa{sv}as)'",
1023 g_variant_get_type_string (parameters));
1024 G_UNLOCK (properties_lock);
1028 g_variant_get (parameters,
1030 &interface_name_for_signal,
1031 &changed_properties,
1032 &invalidated_properties);
1034 if (g_strcmp0 (interface_name_for_signal, proxy->priv->interface_name) != 0)
1036 G_UNLOCK (properties_lock);
1040 g_variant_iter_init (&iter, changed_properties);
1041 while (g_variant_iter_next (&iter, "{sv}", &key, &value))
1043 insert_property_checked (proxy,
1044 key, /* adopts string */
1045 value); /* adopts value */
1048 for (n = 0; invalidated_properties[n] != NULL; n++)
1050 g_hash_table_remove (proxy->priv->properties, invalidated_properties[n]);
1053 G_UNLOCK (properties_lock);
1056 g_signal_emit (proxy, signals[PROPERTIES_CHANGED_SIGNAL],
1059 invalidated_properties);
1062 if (changed_properties != NULL)
1063 g_variant_unref (changed_properties);
1064 g_free (invalidated_properties);
1066 g_object_unref (proxy);
1069 /* ---------------------------------------------------------------------------------------------------- */
1072 process_get_all_reply (GDBusProxy *proxy,
1078 guint num_properties;
1080 if (!g_variant_is_of_type (result, G_VARIANT_TYPE ("(a{sv})")))
1082 g_warning ("Value for GetAll reply with type `%s' does not match `(a{sv})'",
1083 g_variant_get_type_string (result));
1087 G_LOCK (properties_lock);
1089 g_variant_get (result, "(a{sv})", &iter);
1090 while (g_variant_iter_next (iter, "{sv}", &key, &value))
1092 insert_property_checked (proxy,
1093 key, /* adopts string */
1094 value); /* adopts value */
1096 g_variant_iter_free (iter);
1098 num_properties = g_hash_table_size (proxy->priv->properties);
1099 G_UNLOCK (properties_lock);
1101 /* Synthesize ::g-properties-changed changed */
1102 if (num_properties > 0)
1104 GVariant *changed_properties;
1105 const gchar *invalidated_properties[1] = {NULL};
1107 g_variant_get (result,
1109 &changed_properties);
1110 g_signal_emit (proxy, signals[PROPERTIES_CHANGED_SIGNAL],
1113 invalidated_properties);
1114 g_variant_unref (changed_properties);
1124 GCancellable *cancellable;
1126 } LoadPropertiesOnNameOwnerChangedData;
1129 on_name_owner_changed_get_all_cb (GDBusConnection *connection,
1133 LoadPropertiesOnNameOwnerChangedData *data = user_data;
1141 result = g_dbus_connection_call_finish (connection,
1146 if (error->domain == G_IO_ERROR && error->code == G_IO_ERROR_CANCELLED)
1148 /* We just ignore if GetAll() is failing. Because this might happen
1149 * if the object has no properties at all. Or if the caller is
1150 * not authorized to see the properties.
1152 * Either way, apps can know about this by using
1153 * get_cached_property_names() or get_cached_property().
1155 * TODO: handle G_DBUS_DEBUG flag 'proxy' and, if enabled, log the
1156 * fact that GetAll() failed
1158 //g_debug ("error: %d %d %s", error->domain, error->code, error->message);
1159 g_error_free (error);
1162 /* and finally we can notify */
1165 G_LOCK (properties_lock);
1166 g_free (data->proxy->priv->name_owner);
1167 data->proxy->priv->name_owner = data->name_owner;
1168 data->name_owner = NULL; /* to avoid an extra copy, we steal the string */
1169 g_hash_table_remove_all (data->proxy->priv->properties);
1170 G_UNLOCK (properties_lock);
1173 process_get_all_reply (data->proxy, result);
1174 g_variant_unref (result);
1177 g_object_notify (G_OBJECT (data->proxy), "g-name-owner");
1180 if (data->cancellable == data->proxy->priv->get_all_cancellable)
1181 data->proxy->priv->get_all_cancellable = NULL;
1183 g_object_unref (data->proxy);
1184 g_object_unref (data->cancellable);
1185 g_free (data->name_owner);
1190 on_name_owner_changed (GDBusConnection *connection,
1191 const gchar *sender_name,
1192 const gchar *object_path,
1193 const gchar *interface_name,
1194 const gchar *signal_name,
1195 GVariant *parameters,
1198 SignalSubscriptionData *data = user_data;
1200 const gchar *old_owner;
1201 const gchar *new_owner;
1203 G_LOCK (signal_subscription_lock);
1204 proxy = data->proxy;
1207 G_UNLOCK (signal_subscription_lock);
1212 g_object_ref (proxy);
1213 G_UNLOCK (signal_subscription_lock);
1216 /* if we are already trying to load properties, cancel that */
1217 if (proxy->priv->get_all_cancellable != NULL)
1219 g_cancellable_cancel (proxy->priv->get_all_cancellable);
1220 proxy->priv->get_all_cancellable = NULL;
1223 g_variant_get (parameters,
1229 if (strlen (new_owner) == 0)
1231 G_LOCK (properties_lock);
1232 g_free (proxy->priv->name_owner);
1233 proxy->priv->name_owner = NULL;
1235 /* Synthesize ::g-properties-changed changed */
1236 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES) &&
1237 g_hash_table_size (proxy->priv->properties) > 0)
1239 GVariantBuilder builder;
1240 GPtrArray *invalidated_properties;
1241 GHashTableIter iter;
1244 /* Build changed_properties (always empty) and invalidated_properties ... */
1245 g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
1247 invalidated_properties = g_ptr_array_new_with_free_func (g_free);
1248 g_hash_table_iter_init (&iter, proxy->priv->properties);
1249 while (g_hash_table_iter_next (&iter, (gpointer) &key, NULL))
1250 g_ptr_array_add (invalidated_properties, g_strdup (key));
1251 g_ptr_array_add (invalidated_properties, NULL);
1253 /* ... throw out the properties ... */
1254 g_hash_table_remove_all (proxy->priv->properties);
1256 G_UNLOCK (properties_lock);
1258 /* ... and finally emit the ::g-properties-changed signal */
1259 g_signal_emit (proxy, signals[PROPERTIES_CHANGED_SIGNAL],
1261 g_variant_builder_end (&builder) /* consumed */,
1262 (const gchar* const *) invalidated_properties->pdata);
1263 g_ptr_array_unref (invalidated_properties);
1267 G_UNLOCK (properties_lock);
1269 g_object_notify (G_OBJECT (proxy), "g-name-owner");
1273 G_LOCK (properties_lock);
1275 /* ignore duplicates - this can happen when activating the service */
1276 if (g_strcmp0 (new_owner, proxy->priv->name_owner) == 0)
1278 G_UNLOCK (properties_lock);
1282 if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES)
1284 g_free (proxy->priv->name_owner);
1285 proxy->priv->name_owner = g_strdup (new_owner);
1287 g_hash_table_remove_all (proxy->priv->properties);
1288 G_UNLOCK (properties_lock);
1289 g_object_notify (G_OBJECT (proxy), "g-name-owner");
1293 LoadPropertiesOnNameOwnerChangedData *data;
1295 G_UNLOCK (properties_lock);
1297 /* start loading properties.. only then emit notify::g-name-owner .. we
1298 * need to be able to cancel this in the event another NameOwnerChanged
1299 * signal suddenly happens
1302 g_assert (proxy->priv->get_all_cancellable == NULL);
1303 proxy->priv->get_all_cancellable = g_cancellable_new ();
1304 data = g_new0 (LoadPropertiesOnNameOwnerChangedData, 1);
1305 data->proxy = g_object_ref (proxy);
1306 data->cancellable = proxy->priv->get_all_cancellable;
1307 data->name_owner = g_strdup (new_owner);
1308 g_dbus_connection_call (proxy->priv->connection,
1310 proxy->priv->object_path,
1311 "org.freedesktop.DBus.Properties",
1313 g_variant_new ("(s)", proxy->priv->interface_name),
1314 G_VARIANT_TYPE ("(a{sv})"),
1315 G_DBUS_CALL_FLAGS_NONE,
1317 proxy->priv->get_all_cancellable,
1318 (GAsyncReadyCallback) on_name_owner_changed_get_all_cb,
1325 g_object_unref (proxy);
1328 /* ---------------------------------------------------------------------------------------------------- */
1333 GCancellable *cancellable;
1334 GSimpleAsyncResult *simple;
1338 async_init_data_free (AsyncInitData *data)
1340 g_object_unref (data->proxy);
1341 if (data->cancellable != NULL)
1342 g_object_unref (data->cancellable);
1343 g_object_unref (data->simple);
1348 async_init_get_all_cb (GDBusConnection *connection,
1352 AsyncInitData *data = user_data;
1357 result = g_dbus_connection_call_finish (connection,
1362 /* We just ignore if GetAll() is failing. Because this might happen
1363 * if the object has no properties at all. Or if the caller is
1364 * not authorized to see the properties.
1366 * Either way, apps can know about this by using
1367 * get_cached_property_names() or get_cached_property().
1369 * TODO: handle G_DBUS_DEBUG flag 'proxy' and, if enabled, log the
1370 * fact that GetAll() failed
1372 //g_debug ("error: %d %d %s", error->domain, error->code, error->message);
1373 g_error_free (error);
1377 g_simple_async_result_set_op_res_gpointer (data->simple,
1379 (GDestroyNotify) g_variant_unref);
1382 g_simple_async_result_complete_in_idle (data->simple);
1383 async_init_data_free (data);
1387 async_init_data_set_name_owner (AsyncInitData *data,
1388 const gchar *name_owner)
1393 if (name_owner != NULL)
1395 /* it starts as NULL anyway */
1396 G_LOCK (properties_lock);
1397 data->proxy->priv->name_owner = g_strdup (name_owner);
1398 G_UNLOCK (properties_lock);
1403 if (data->proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES)
1405 /* Don't load properties if the API user doesn't want them */
1408 else if (name_owner == NULL && data->proxy->priv->name != NULL)
1410 /* Don't attempt to load properties if the name_owner is NULL (which
1411 * usually means the name isn't owned), unless name is also NULL (which
1412 * means we actually wanted to talk to the directly-connected process -
1413 * either dbus-daemon or a peer - instead of going via dbus-daemon)
1420 /* load all properties asynchronously */
1421 g_dbus_connection_call (data->proxy->priv->connection,
1423 data->proxy->priv->object_path,
1424 "org.freedesktop.DBus.Properties",
1426 g_variant_new ("(s)", data->proxy->priv->interface_name),
1427 G_VARIANT_TYPE ("(a{sv})"),
1428 G_DBUS_CALL_FLAGS_NONE,
1431 (GAsyncReadyCallback) async_init_get_all_cb,
1436 g_simple_async_result_complete_in_idle (data->simple);
1437 async_init_data_free (data);
1442 async_init_get_name_owner_cb (GDBusConnection *connection,
1446 AsyncInitData *data = user_data;
1451 result = g_dbus_connection_call_finish (connection,
1456 if (error->domain == G_DBUS_ERROR &&
1457 error->code == G_DBUS_ERROR_NAME_HAS_NO_OWNER)
1459 g_error_free (error);
1460 async_init_data_set_name_owner (data, NULL);
1464 g_simple_async_result_take_error (data->simple, error);
1465 g_simple_async_result_complete_in_idle (data->simple);
1466 async_init_data_free (data);
1471 /* borrowed from result to avoid an extra copy */
1472 const gchar *name_owner;
1474 g_variant_get (result, "(&s)", &name_owner);
1475 async_init_data_set_name_owner (data, name_owner);
1476 g_variant_unref (result);
1481 async_init_call_get_name_owner (AsyncInitData *data)
1483 g_dbus_connection_call (data->proxy->priv->connection,
1484 "org.freedesktop.DBus", /* name */
1485 "/org/freedesktop/DBus", /* object path */
1486 "org.freedesktop.DBus", /* interface */
1488 g_variant_new ("(s)",
1489 data->proxy->priv->name),
1490 G_VARIANT_TYPE ("(s)"),
1491 G_DBUS_CALL_FLAGS_NONE,
1494 (GAsyncReadyCallback) async_init_get_name_owner_cb,
1499 async_init_start_service_by_name_cb (GDBusConnection *connection,
1503 AsyncInitData *data = user_data;
1508 result = g_dbus_connection_call_finish (connection,
1513 /* Errors are not unexpected; the bus will reply e.g.
1515 * org.freedesktop.DBus.Error.ServiceUnknown: The name org.gnome.Epiphany2
1516 * was not provided by any .service files
1518 * This doesn't mean that the name doesn't have an owner, just
1519 * that it's not provided by a .service file. So just proceed to
1520 * invoke GetNameOwner() if dealing with that error.
1522 if (error->domain == G_DBUS_ERROR &&
1523 error->code == G_DBUS_ERROR_SERVICE_UNKNOWN)
1525 g_error_free (error);
1529 g_prefix_error (&error,
1530 _("Error calling StartServiceByName for %s: "),
1531 data->proxy->priv->name);
1537 guint32 start_service_result;
1538 g_variant_get (result,
1540 &start_service_result);
1541 g_variant_unref (result);
1542 if (start_service_result == 1 || /* DBUS_START_REPLY_SUCCESS */
1543 start_service_result == 2) /* DBUS_START_REPLY_ALREADY_RUNNING */
1545 /* continue to invoke GetNameOwner() */
1549 error = g_error_new (G_IO_ERROR,
1551 _("Unexpected reply %d from StartServiceByName(\"%s\") method"),
1552 start_service_result,
1553 data->proxy->priv->name);
1558 async_init_call_get_name_owner (data);
1562 g_warn_if_fail (error != NULL);
1563 g_simple_async_result_take_error (data->simple, error);
1564 g_simple_async_result_complete_in_idle (data->simple);
1565 async_init_data_free (data);
1569 async_init_call_start_service_by_name (AsyncInitData *data)
1571 g_dbus_connection_call (data->proxy->priv->connection,
1572 "org.freedesktop.DBus", /* name */
1573 "/org/freedesktop/DBus", /* object path */
1574 "org.freedesktop.DBus", /* interface */
1575 "StartServiceByName",
1576 g_variant_new ("(su)",
1577 data->proxy->priv->name,
1579 G_VARIANT_TYPE ("(u)"),
1580 G_DBUS_CALL_FLAGS_NONE,
1583 (GAsyncReadyCallback) async_init_start_service_by_name_cb,
1588 async_initable_init_second_async (GAsyncInitable *initable,
1590 GCancellable *cancellable,
1591 GAsyncReadyCallback callback,
1594 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1595 AsyncInitData *data;
1597 data = g_new0 (AsyncInitData, 1);
1598 data->proxy = g_object_ref (proxy);
1599 data->cancellable = cancellable != NULL ? g_object_ref (cancellable) : NULL;
1600 data->simple = g_simple_async_result_new (G_OBJECT (proxy),
1605 /* Check name ownership asynchronously - possibly also start the service */
1606 if (proxy->priv->name == NULL)
1609 async_init_data_set_name_owner (data, NULL);
1611 else if (g_dbus_is_unique_name (proxy->priv->name))
1613 async_init_data_set_name_owner (data, proxy->priv->name);
1617 if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START)
1619 async_init_call_get_name_owner (data);
1623 async_init_call_start_service_by_name (data);
1629 async_initable_init_second_finish (GAsyncInitable *initable,
1633 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1634 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1640 if (g_simple_async_result_propagate_error (simple, error))
1643 result = g_simple_async_result_get_op_res_gpointer (simple);
1646 process_get_all_reply (proxy, result);
1652 proxy->priv->initialized = TRUE;
1656 /* ---------------------------------------------------------------------------------------------------- */
1659 async_initable_init_first (GAsyncInitable *initable)
1661 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1663 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES))
1665 /* subscribe to PropertiesChanged() */
1666 proxy->priv->properties_changed_subscription_id =
1667 g_dbus_connection_signal_subscribe (proxy->priv->connection,
1669 "org.freedesktop.DBus.Properties",
1670 "PropertiesChanged",
1671 proxy->priv->object_path,
1672 proxy->priv->interface_name,
1673 G_DBUS_SIGNAL_FLAGS_NONE,
1674 on_properties_changed,
1675 signal_subscription_ref (proxy->priv->signal_subscription_data),
1676 (GDestroyNotify) signal_subscription_unref);
1679 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS))
1681 /* subscribe to all signals for the object */
1682 proxy->priv->signals_subscription_id =
1683 g_dbus_connection_signal_subscribe (proxy->priv->connection,
1685 proxy->priv->interface_name,
1687 proxy->priv->object_path,
1689 G_DBUS_SIGNAL_FLAGS_NONE,
1691 signal_subscription_ref (proxy->priv->signal_subscription_data),
1692 (GDestroyNotify) signal_subscription_unref);
1695 if (proxy->priv->name != NULL && !g_dbus_is_unique_name (proxy->priv->name))
1697 proxy->priv->name_owner_changed_subscription_id =
1698 g_dbus_connection_signal_subscribe (proxy->priv->connection,
1699 "org.freedesktop.DBus", /* name */
1700 "org.freedesktop.DBus", /* interface */
1701 "NameOwnerChanged", /* signal name */
1702 "/org/freedesktop/DBus", /* path */
1703 proxy->priv->name, /* arg0 */
1704 G_DBUS_SIGNAL_FLAGS_NONE,
1705 on_name_owner_changed,
1706 signal_subscription_ref (proxy->priv->signal_subscription_data),
1707 (GDestroyNotify) signal_subscription_unref);
1711 /* ---------------------------------------------------------------------------------------------------- */
1713 /* initialization is split into two parts - the first is the
1714 * non-blocing part that requires the callers GMainContext - the
1715 * second is a blocking part async part that doesn't require the
1716 * callers GMainContext.. we do this split so the code can be reused
1717 * in the GInitable implementation below.
1719 * Note that obtaining a GDBusConnection is not shared between the two
1727 GCancellable *cancellable;
1728 GAsyncReadyCallback callback;
1730 } GetConnectionData;
1733 get_connection_cb (GObject *source_object,
1737 GetConnectionData *data = user_data;
1741 data->proxy->priv->connection = g_bus_get_finish (res, &error);
1742 if (data->proxy->priv->connection == NULL)
1744 GSimpleAsyncResult *simple;
1745 simple = g_simple_async_result_new (G_OBJECT (data->proxy),
1749 g_simple_async_result_take_error (simple, error);
1750 g_simple_async_result_complete_in_idle (simple);
1751 g_object_unref (simple);
1755 async_initable_init_first (G_ASYNC_INITABLE (data->proxy));
1756 async_initable_init_second_async (G_ASYNC_INITABLE (data->proxy),
1763 if (data->cancellable != NULL)
1764 g_object_unref (data->cancellable);
1766 g_object_unref (data->proxy);
1771 async_initable_init_async (GAsyncInitable *initable,
1773 GCancellable *cancellable,
1774 GAsyncReadyCallback callback,
1777 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1779 if (proxy->priv->bus_type != G_BUS_TYPE_NONE)
1781 GetConnectionData *data;
1783 g_assert (proxy->priv->connection == NULL);
1785 data = g_new0 (GetConnectionData, 1);
1786 data->proxy = g_object_ref (proxy);
1787 data->io_priority = io_priority;
1788 data->cancellable = cancellable != NULL ? g_object_ref (cancellable) : NULL;
1789 data->callback = callback;
1790 data->user_data = user_data;
1791 g_bus_get (proxy->priv->bus_type,
1798 async_initable_init_first (initable);
1799 async_initable_init_second_async (initable, io_priority, cancellable, callback, user_data);
1804 async_initable_init_finish (GAsyncInitable *initable,
1808 return async_initable_init_second_finish (initable, res, error);
1812 async_initable_iface_init (GAsyncInitableIface *async_initable_iface)
1814 async_initable_iface->init_async = async_initable_init_async;
1815 async_initable_iface->init_finish = async_initable_init_finish;
1818 /* ---------------------------------------------------------------------------------------------------- */
1822 GMainContext *context;
1825 } InitableAsyncInitableData;
1828 async_initable_init_async_cb (GObject *source_object,
1832 InitableAsyncInitableData *data = user_data;
1833 data->res = g_object_ref (res);
1834 g_main_loop_quit (data->loop);
1837 /* Simply reuse the GAsyncInitable implementation but run the first
1838 * part (that is non-blocking and requires the callers GMainContext)
1839 * with the callers GMainContext.. and the second with a private
1840 * GMainContext (bug 621310 is slightly related).
1842 * Note that obtaining a GDBusConnection is not shared between the two
1846 initable_init (GInitable *initable,
1847 GCancellable *cancellable,
1850 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1851 InitableAsyncInitableData *data;
1856 if (proxy->priv->bus_type != G_BUS_TYPE_NONE)
1858 g_assert (proxy->priv->connection == NULL);
1859 proxy->priv->connection = g_bus_get_sync (proxy->priv->bus_type,
1862 if (proxy->priv->connection == NULL)
1866 async_initable_init_first (G_ASYNC_INITABLE (initable));
1868 data = g_new0 (InitableAsyncInitableData, 1);
1869 data->context = g_main_context_new ();
1870 data->loop = g_main_loop_new (data->context, FALSE);
1872 g_main_context_push_thread_default (data->context);
1874 async_initable_init_second_async (G_ASYNC_INITABLE (initable),
1877 async_initable_init_async_cb,
1880 g_main_loop_run (data->loop);
1882 ret = async_initable_init_second_finish (G_ASYNC_INITABLE (initable),
1886 g_main_context_pop_thread_default (data->context);
1888 g_main_context_unref (data->context);
1889 g_main_loop_unref (data->loop);
1890 g_object_unref (data->res);
1899 initable_iface_init (GInitableIface *initable_iface)
1901 initable_iface->init = initable_init;
1904 /* ---------------------------------------------------------------------------------------------------- */
1908 * @connection: A #GDBusConnection.
1909 * @flags: Flags used when constructing the proxy.
1910 * @info: (allow-none): A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
1911 * @name: (allow-none): A bus name (well-known or unique) or %NULL if @connection is not a message bus connection.
1912 * @object_path: An object path.
1913 * @interface_name: A D-Bus interface name.
1914 * @cancellable: A #GCancellable or %NULL.
1915 * @callback: Callback function to invoke when the proxy is ready.
1916 * @user_data: User data to pass to @callback.
1918 * Creates a proxy for accessing @interface_name on the remote object
1919 * at @object_path owned by @name at @connection and asynchronously
1920 * loads D-Bus properties unless the
1921 * %G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES flag is used. Connect to
1922 * the #GDBusProxy::g-properties-changed signal to get notified about
1925 * If the %G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS flag is not set, also sets up
1926 * match rules for signals. Connect to the #GDBusProxy::g-signal signal
1927 * to handle signals from the remote object.
1929 * If @name is a well-known name and the
1930 * %G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START flag isn't set and no name
1931 * owner currently exists, the message bus will be requested to launch
1932 * a name owner for the name.
1934 * This is a failable asynchronous constructor - when the proxy is
1935 * ready, @callback will be invoked and you can use
1936 * g_dbus_proxy_new_finish() to get the result.
1938 * See g_dbus_proxy_new_sync() and for a synchronous version of this constructor.
1940 * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
1945 g_dbus_proxy_new (GDBusConnection *connection,
1946 GDBusProxyFlags flags,
1947 GDBusInterfaceInfo *info,
1949 const gchar *object_path,
1950 const gchar *interface_name,
1951 GCancellable *cancellable,
1952 GAsyncReadyCallback callback,
1955 g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
1956 g_return_if_fail ((name == NULL && g_dbus_connection_get_unique_name (connection) == NULL) || g_dbus_is_name (name));
1957 g_return_if_fail (g_variant_is_object_path (object_path));
1958 g_return_if_fail (g_dbus_is_interface_name (interface_name));
1960 g_async_initable_new_async (G_TYPE_DBUS_PROXY,
1966 "g-interface-info", info,
1968 "g-connection", connection,
1969 "g-object-path", object_path,
1970 "g-interface-name", interface_name,
1975 * g_dbus_proxy_new_finish:
1976 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback function passed to g_dbus_proxy_new().
1977 * @error: Return location for error or %NULL.
1979 * Finishes creating a #GDBusProxy.
1981 * Returns: A #GDBusProxy or %NULL if @error is set. Free with g_object_unref().
1986 g_dbus_proxy_new_finish (GAsyncResult *res,
1990 GObject *source_object;
1992 source_object = g_async_result_get_source_object (res);
1993 g_assert (source_object != NULL);
1995 object = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object),
1998 g_object_unref (source_object);
2001 return G_DBUS_PROXY (object);
2007 * g_dbus_proxy_new_sync:
2008 * @connection: A #GDBusConnection.
2009 * @flags: Flags used when constructing the proxy.
2010 * @info: (allow-none): A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
2011 * @name: (allow-none): A bus name (well-known or unique) or %NULL if @connection is not a message bus connection.
2012 * @object_path: An object path.
2013 * @interface_name: A D-Bus interface name.
2014 * @cancellable: (allow-none): A #GCancellable or %NULL.
2015 * @error: (allow-none): Return location for error or %NULL.
2017 * Creates a proxy for accessing @interface_name on the remote object
2018 * at @object_path owned by @name at @connection and synchronously
2019 * loads D-Bus properties unless the
2020 * %G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES flag is used.
2022 * If the %G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS flag is not set, also sets up
2023 * match rules for signals. Connect to the #GDBusProxy::g-signal signal
2024 * to handle signals from the remote object.
2026 * If @name is a well-known name and the
2027 * %G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START flag isn't set and no name
2028 * owner currently exists, the message bus will be requested to launch
2029 * a name owner for the name.
2031 * This is a synchronous failable constructor. See g_dbus_proxy_new()
2032 * and g_dbus_proxy_new_finish() for the asynchronous version.
2034 * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
2036 * Returns: A #GDBusProxy or %NULL if error is set. Free with g_object_unref().
2041 g_dbus_proxy_new_sync (GDBusConnection *connection,
2042 GDBusProxyFlags flags,
2043 GDBusInterfaceInfo *info,
2045 const gchar *object_path,
2046 const gchar *interface_name,
2047 GCancellable *cancellable,
2050 GInitable *initable;
2052 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
2053 g_return_val_if_fail ((name == NULL && g_dbus_connection_get_unique_name (connection) == NULL) ||
2054 g_dbus_is_name (name), NULL);
2055 g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
2056 g_return_val_if_fail (g_dbus_is_interface_name (interface_name), NULL);
2058 initable = g_initable_new (G_TYPE_DBUS_PROXY,
2062 "g-interface-info", info,
2064 "g-connection", connection,
2065 "g-object-path", object_path,
2066 "g-interface-name", interface_name,
2068 if (initable != NULL)
2069 return G_DBUS_PROXY (initable);
2074 /* ---------------------------------------------------------------------------------------------------- */
2077 * g_dbus_proxy_new_for_bus:
2078 * @bus_type: A #GBusType.
2079 * @flags: Flags used when constructing the proxy.
2080 * @info: (allow-none): A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
2081 * @name: A bus name (well-known or unique).
2082 * @object_path: An object path.
2083 * @interface_name: A D-Bus interface name.
2084 * @cancellable: A #GCancellable or %NULL.
2085 * @callback: Callback function to invoke when the proxy is ready.
2086 * @user_data: User data to pass to @callback.
2088 * Like g_dbus_proxy_new() but takes a #GBusType instead of a #GDBusConnection.
2090 * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
2095 g_dbus_proxy_new_for_bus (GBusType bus_type,
2096 GDBusProxyFlags flags,
2097 GDBusInterfaceInfo *info,
2099 const gchar *object_path,
2100 const gchar *interface_name,
2101 GCancellable *cancellable,
2102 GAsyncReadyCallback callback,
2105 g_return_if_fail (g_dbus_is_name (name));
2106 g_return_if_fail (g_variant_is_object_path (object_path));
2107 g_return_if_fail (g_dbus_is_interface_name (interface_name));
2109 g_async_initable_new_async (G_TYPE_DBUS_PROXY,
2115 "g-interface-info", info,
2117 "g-bus-type", bus_type,
2118 "g-object-path", object_path,
2119 "g-interface-name", interface_name,
2124 * g_dbus_proxy_new_for_bus_finish:
2125 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback function passed to g_dbus_proxy_new_for_bus().
2126 * @error: Return location for error or %NULL.
2128 * Finishes creating a #GDBusProxy.
2130 * Returns: A #GDBusProxy or %NULL if @error is set. Free with g_object_unref().
2135 g_dbus_proxy_new_for_bus_finish (GAsyncResult *res,
2138 return g_dbus_proxy_new_finish (res, error);
2142 * g_dbus_proxy_new_for_bus_sync:
2143 * @bus_type: A #GBusType.
2144 * @flags: Flags used when constructing the proxy.
2145 * @info: (allow-none): A #GDBusInterfaceInfo specifying the minimal interface
2146 * that @proxy conforms to or %NULL.
2147 * @name: A bus name (well-known or unique).
2148 * @object_path: An object path.
2149 * @interface_name: A D-Bus interface name.
2150 * @cancellable: A #GCancellable or %NULL.
2151 * @error: Return location for error or %NULL.
2153 * Like g_dbus_proxy_new_sync() but takes a #GBusType instead of a #GDBusConnection.
2155 * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
2157 * Returns: A #GDBusProxy or %NULL if error is set. Free with g_object_unref().
2162 g_dbus_proxy_new_for_bus_sync (GBusType bus_type,
2163 GDBusProxyFlags flags,
2164 GDBusInterfaceInfo *info,
2166 const gchar *object_path,
2167 const gchar *interface_name,
2168 GCancellable *cancellable,
2171 GInitable *initable;
2173 g_return_val_if_fail (g_dbus_is_name (name), NULL);
2174 g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
2175 g_return_val_if_fail (g_dbus_is_interface_name (interface_name), NULL);
2177 initable = g_initable_new (G_TYPE_DBUS_PROXY,
2181 "g-interface-info", info,
2183 "g-bus-type", bus_type,
2184 "g-object-path", object_path,
2185 "g-interface-name", interface_name,
2187 if (initable != NULL)
2188 return G_DBUS_PROXY (initable);
2193 /* ---------------------------------------------------------------------------------------------------- */
2196 * g_dbus_proxy_get_connection:
2197 * @proxy: A #GDBusProxy.
2199 * Gets the connection @proxy is for.
2201 * Returns: (transfer none): A #GDBusConnection owned by @proxy. Do not free.
2206 g_dbus_proxy_get_connection (GDBusProxy *proxy)
2208 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2209 return proxy->priv->connection;
2213 * g_dbus_proxy_get_flags:
2214 * @proxy: A #GDBusProxy.
2216 * Gets the flags that @proxy was constructed with.
2218 * Returns: Flags from the #GDBusProxyFlags enumeration.
2223 g_dbus_proxy_get_flags (GDBusProxy *proxy)
2225 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), 0);
2226 return proxy->priv->flags;
2230 * g_dbus_proxy_get_name:
2231 * @proxy: A #GDBusProxy.
2233 * Gets the name that @proxy was constructed for.
2235 * Returns: A string owned by @proxy. Do not free.
2240 g_dbus_proxy_get_name (GDBusProxy *proxy)
2242 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2243 return proxy->priv->name;
2247 * g_dbus_proxy_get_name_owner:
2248 * @proxy: A #GDBusProxy.
2250 * The unique name that owns the name that @proxy is for or %NULL if
2251 * no-one currently owns that name. You may connect to the
2252 * #GObject::notify signal to track changes to the
2253 * #GDBusProxy:g-name-owner property.
2255 * Returns: The name owner or %NULL if no name owner exists. Free with g_free().
2260 g_dbus_proxy_get_name_owner (GDBusProxy *proxy)
2264 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2266 G_LOCK (properties_lock);
2267 ret = g_strdup (proxy->priv->name_owner);
2268 G_UNLOCK (properties_lock);
2273 * g_dbus_proxy_get_object_path:
2274 * @proxy: A #GDBusProxy.
2276 * Gets the object path @proxy is for.
2278 * Returns: A string owned by @proxy. Do not free.
2283 g_dbus_proxy_get_object_path (GDBusProxy *proxy)
2285 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2286 return proxy->priv->object_path;
2290 * g_dbus_proxy_get_interface_name:
2291 * @proxy: A #GDBusProxy.
2293 * Gets the D-Bus interface name @proxy is for.
2295 * Returns: A string owned by @proxy. Do not free.
2300 g_dbus_proxy_get_interface_name (GDBusProxy *proxy)
2302 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2303 return proxy->priv->interface_name;
2307 * g_dbus_proxy_get_default_timeout:
2308 * @proxy: A #GDBusProxy.
2310 * Gets the timeout to use if -1 (specifying default timeout) is
2311 * passed as @timeout_msec in the g_dbus_proxy_call() and
2312 * g_dbus_proxy_call_sync() functions.
2314 * See the #GDBusProxy:g-default-timeout property for more details.
2316 * Returns: Timeout to use for @proxy.
2321 g_dbus_proxy_get_default_timeout (GDBusProxy *proxy)
2325 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), -1);
2327 G_LOCK (properties_lock);
2328 ret = proxy->priv->timeout_msec;
2329 G_UNLOCK (properties_lock);
2334 * g_dbus_proxy_set_default_timeout:
2335 * @proxy: A #GDBusProxy.
2336 * @timeout_msec: Timeout in milliseconds.
2338 * Sets the timeout to use if -1 (specifying default timeout) is
2339 * passed as @timeout_msec in the g_dbus_proxy_call() and
2340 * g_dbus_proxy_call_sync() functions.
2342 * See the #GDBusProxy:g-default-timeout property for more details.
2347 g_dbus_proxy_set_default_timeout (GDBusProxy *proxy,
2350 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
2351 g_return_if_fail (timeout_msec == -1 || timeout_msec >= 0);
2353 G_LOCK (properties_lock);
2355 if (proxy->priv->timeout_msec != timeout_msec)
2357 proxy->priv->timeout_msec = timeout_msec;
2358 G_UNLOCK (properties_lock);
2360 g_object_notify (G_OBJECT (proxy), "g-default-timeout");
2364 G_UNLOCK (properties_lock);
2369 * g_dbus_proxy_get_interface_info:
2370 * @proxy: A #GDBusProxy
2372 * Returns the #GDBusInterfaceInfo, if any, specifying the interface
2373 * that @proxy conforms to. See the #GDBusProxy:g-interface-info
2374 * property for more details.
2376 * Returns: A #GDBusInterfaceInfo or %NULL. Do not unref the returned
2377 * object, it is owned by @proxy.
2381 GDBusInterfaceInfo *
2382 g_dbus_proxy_get_interface_info (GDBusProxy *proxy)
2384 GDBusInterfaceInfo *ret;
2386 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2388 G_LOCK (properties_lock);
2389 ret = proxy->priv->expected_interface;
2390 G_UNLOCK (properties_lock);
2391 /* FIXME: returning a borrowed ref with no guarantee that nobody will
2392 * call g_dbus_proxy_set_interface_info() and make it invalid...
2398 * g_dbus_proxy_set_interface_info:
2399 * @proxy: A #GDBusProxy
2400 * @info: (allow-none): Minimum interface this proxy conforms to or %NULL to unset.
2402 * Ensure that interactions with @proxy conform to the given
2403 * interface. See the #GDBusProxy:g-interface-info property for more
2409 g_dbus_proxy_set_interface_info (GDBusProxy *proxy,
2410 GDBusInterfaceInfo *info)
2412 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
2413 G_LOCK (properties_lock);
2415 if (proxy->priv->expected_interface != NULL)
2417 g_dbus_interface_info_cache_release (proxy->priv->expected_interface);
2418 g_dbus_interface_info_unref (proxy->priv->expected_interface);
2420 proxy->priv->expected_interface = info != NULL ? g_dbus_interface_info_ref (info) : NULL;
2421 if (proxy->priv->expected_interface != NULL)
2422 g_dbus_interface_info_cache_build (proxy->priv->expected_interface);
2424 G_UNLOCK (properties_lock);
2427 /* ---------------------------------------------------------------------------------------------------- */
2430 maybe_split_method_name (const gchar *method_name,
2431 gchar **out_interface_name,
2432 const gchar **out_method_name)
2437 g_assert (out_interface_name != NULL);
2438 g_assert (out_method_name != NULL);
2439 *out_interface_name = NULL;
2440 *out_method_name = NULL;
2442 if (strchr (method_name, '.') != NULL)
2447 p = g_strdup (method_name);
2448 last_dot = strrchr (p, '.');
2451 *out_interface_name = p;
2452 *out_method_name = last_dot + 1;
2464 GUnixFDList *fd_list;
2469 reply_data_free (ReplyData *data)
2471 g_variant_unref (data->value);
2473 if (data->fd_list != NULL)
2474 g_object_unref (data->fd_list);
2476 g_slice_free (ReplyData, data);
2480 reply_cb (GDBusConnection *connection,
2484 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
2488 GUnixFDList *fd_list;
2493 value = g_dbus_connection_call_with_unix_fd_list_finish (connection,
2498 value = g_dbus_connection_call_finish (connection,
2504 g_simple_async_result_take_error (simple, error);
2509 data = g_slice_new0 (ReplyData);
2510 data->value = value;
2512 data->fd_list = fd_list;
2514 g_simple_async_result_set_op_res_gpointer (simple, data, (GDestroyNotify) reply_data_free);
2517 /* no need to complete in idle since the method GDBusConnection already does */
2518 g_simple_async_result_complete (simple);
2519 g_object_unref (simple);
2522 /* properties_lock must be held for as long as you will keep the
2525 static const GDBusMethodInfo *
2526 lookup_method_info (GDBusProxy *proxy,
2527 const gchar *method_name)
2529 const GDBusMethodInfo *info = NULL;
2531 if (proxy->priv->expected_interface == NULL)
2534 info = g_dbus_interface_info_lookup_method (proxy->priv->expected_interface, method_name);
2540 /* properties_lock must be held for as long as you will keep the
2543 static const gchar *
2544 get_destination_for_call (GDBusProxy *proxy)
2550 /* If proxy->priv->name is a unique name, then proxy->priv->name_owner
2551 * is never NULL and always the same as proxy->priv->name. We use this
2552 * knowledge to avoid checking if proxy->priv->name is a unique or
2555 ret = proxy->priv->name_owner;
2559 if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START)
2562 ret = proxy->priv->name;
2568 /* ---------------------------------------------------------------------------------------------------- */
2571 g_dbus_proxy_call_internal (GDBusProxy *proxy,
2572 const gchar *method_name,
2573 GVariant *parameters,
2574 GDBusCallFlags flags,
2576 GUnixFDList *fd_list,
2577 GCancellable *cancellable,
2578 GAsyncReadyCallback callback,
2581 GSimpleAsyncResult *simple;
2583 gchar *split_interface_name;
2584 const gchar *split_method_name;
2585 const gchar *target_method_name;
2586 const gchar *target_interface_name;
2588 GVariantType *reply_type;
2590 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
2591 g_return_if_fail (g_dbus_is_member_name (method_name) || g_dbus_is_interface_name (method_name));
2592 g_return_if_fail (parameters == NULL || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE));
2593 g_return_if_fail (timeout_msec == -1 || timeout_msec >= 0);
2595 g_return_if_fail (fd_list == NULL || G_IS_UNIX_FD_LIST (fd_list));
2597 g_return_if_fail (fd_list == NULL);
2601 split_interface_name = NULL;
2603 simple = g_simple_async_result_new (G_OBJECT (proxy),
2606 g_dbus_proxy_call_internal);
2608 G_LOCK (properties_lock);
2610 was_split = maybe_split_method_name (method_name, &split_interface_name, &split_method_name);
2611 target_method_name = was_split ? split_method_name : method_name;
2612 target_interface_name = was_split ? split_interface_name : proxy->priv->interface_name;
2614 /* Warn if method is unexpected (cf. :g-interface-info) */
2617 const GDBusMethodInfo *expected_method_info;
2618 expected_method_info = lookup_method_info (proxy, target_method_name);
2619 if (expected_method_info != NULL)
2620 reply_type = _g_dbus_compute_complete_signature (expected_method_info->out_args);
2624 if (proxy->priv->name != NULL)
2626 destination = g_strdup (get_destination_for_call (proxy));
2627 if (destination == NULL)
2629 g_simple_async_result_set_error (simple,
2632 _("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"));
2633 G_UNLOCK (properties_lock);
2638 G_UNLOCK (properties_lock);
2641 g_dbus_connection_call_with_unix_fd_list (proxy->priv->connection,
2643 proxy->priv->object_path,
2644 target_interface_name,
2649 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2652 (GAsyncReadyCallback) reply_cb,
2655 g_dbus_connection_call (proxy->priv->connection,
2657 proxy->priv->object_path,
2658 target_interface_name,
2663 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2665 (GAsyncReadyCallback) reply_cb,
2670 if (reply_type != NULL)
2671 g_variant_type_free (reply_type);
2673 g_free (destination);
2674 g_free (split_interface_name);
2678 g_dbus_proxy_call_finish_internal (GDBusProxy *proxy,
2679 GUnixFDList **out_fd_list,
2683 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2687 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2688 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2689 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2691 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_dbus_proxy_call_internal);
2695 if (g_simple_async_result_propagate_error (simple, error))
2698 data = g_simple_async_result_get_op_res_gpointer (simple);
2699 value = g_variant_ref (data->value);
2701 if (out_fd_list != NULL)
2702 *out_fd_list = data->fd_list != NULL ? g_object_ref (data->fd_list) : NULL;
2710 g_dbus_proxy_call_sync_internal (GDBusProxy *proxy,
2711 const gchar *method_name,
2712 GVariant *parameters,
2713 GDBusCallFlags flags,
2715 GUnixFDList *fd_list,
2716 GUnixFDList **out_fd_list,
2717 GCancellable *cancellable,
2722 gchar *split_interface_name;
2723 const gchar *split_method_name;
2724 const gchar *target_method_name;
2725 const gchar *target_interface_name;
2727 GVariantType *reply_type;
2729 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2730 g_return_val_if_fail (g_dbus_is_member_name (method_name) || g_dbus_is_interface_name (method_name), NULL);
2731 g_return_val_if_fail (parameters == NULL || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE), NULL);
2732 g_return_val_if_fail (timeout_msec == -1 || timeout_msec >= 0, NULL);
2734 g_return_val_if_fail (fd_list == NULL || G_IS_UNIX_FD_LIST (fd_list), NULL);
2736 g_return_val_if_fail (fd_list == NULL, NULL);
2738 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2742 G_LOCK (properties_lock);
2744 was_split = maybe_split_method_name (method_name, &split_interface_name, &split_method_name);
2745 target_method_name = was_split ? split_method_name : method_name;
2746 target_interface_name = was_split ? split_interface_name : proxy->priv->interface_name;
2748 /* Warn if method is unexpected (cf. :g-interface-info) */
2751 const GDBusMethodInfo *expected_method_info;
2752 expected_method_info = lookup_method_info (proxy, target_method_name);
2753 if (expected_method_info != NULL)
2754 reply_type = _g_dbus_compute_complete_signature (expected_method_info->out_args);
2758 if (proxy->priv->name != NULL)
2760 destination = g_strdup (get_destination_for_call (proxy));
2761 if (destination == NULL)
2763 g_set_error_literal (error,
2766 _("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"));
2768 G_UNLOCK (properties_lock);
2773 G_UNLOCK (properties_lock);
2776 ret = g_dbus_connection_call_with_unix_fd_list_sync (proxy->priv->connection,
2778 proxy->priv->object_path,
2779 target_interface_name,
2784 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2790 ret = g_dbus_connection_call_sync (proxy->priv->connection,
2792 proxy->priv->object_path,
2793 target_interface_name,
2798 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2804 if (reply_type != NULL)
2805 g_variant_type_free (reply_type);
2807 g_free (destination);
2808 g_free (split_interface_name);
2813 /* ---------------------------------------------------------------------------------------------------- */
2816 * g_dbus_proxy_call:
2817 * @proxy: A #GDBusProxy.
2818 * @method_name: Name of method to invoke.
2819 * @parameters: (allow-none): A #GVariant tuple with parameters for the signal or %NULL if not passing parameters.
2820 * @flags: Flags from the #GDBusCallFlags enumeration.
2821 * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
2822 * "infinite") or -1 to use the proxy default timeout.
2823 * @cancellable: A #GCancellable or %NULL.
2824 * @callback: A #GAsyncReadyCallback to call when the request is satisfied or %NULL if you don't
2825 * care about the result of the method invocation.
2826 * @user_data: The data to pass to @callback.
2828 * Asynchronously invokes the @method_name method on @proxy.
2830 * If @method_name contains any dots, then @name is split into interface and
2831 * method name parts. This allows using @proxy for invoking methods on
2834 * If the #GDBusConnection associated with @proxy is closed then
2835 * the operation will fail with %G_IO_ERROR_CLOSED. If
2836 * @cancellable is canceled, the operation will fail with
2837 * %G_IO_ERROR_CANCELLED. If @parameters contains a value not
2838 * compatible with the D-Bus protocol, the operation fails with
2839 * %G_IO_ERROR_INVALID_ARGUMENT.
2841 * If the @parameters #GVariant is floating, it is consumed. This allows
2842 * convenient 'inline' use of g_variant_new(), e.g.:
2844 * g_dbus_proxy_call (proxy,
2846 * g_variant_new ("(ss)",
2849 * G_DBUS_CALL_FLAGS_NONE,
2852 * (GAsyncReadyCallback) two_strings_done,
2856 * If @proxy has an expected interface (see
2857 * #GDBusProxy:g-interface-info) and @method_name is referenced by it,
2858 * then the return value is checked against the return type.
2860 * This is an asynchronous method. When the operation is finished,
2861 * @callback will be invoked in the
2862 * <link linkend="g-main-context-push-thread-default">thread-default main loop</link>
2863 * of the thread you are calling this method from.
2864 * You can then call g_dbus_proxy_call_finish() to get the result of
2865 * the operation. See g_dbus_proxy_call_sync() for the synchronous
2866 * version of this method.
2871 g_dbus_proxy_call (GDBusProxy *proxy,
2872 const gchar *method_name,
2873 GVariant *parameters,
2874 GDBusCallFlags flags,
2876 GCancellable *cancellable,
2877 GAsyncReadyCallback callback,
2880 g_dbus_proxy_call_internal (proxy, method_name, parameters, flags, timeout_msec, NULL, cancellable, callback, user_data);
2884 * g_dbus_proxy_call_finish:
2885 * @proxy: A #GDBusProxy.
2886 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_proxy_call().
2887 * @error: Return location for error or %NULL.
2889 * Finishes an operation started with g_dbus_proxy_call().
2891 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
2892 * return values. Free with g_variant_unref().
2897 g_dbus_proxy_call_finish (GDBusProxy *proxy,
2901 return g_dbus_proxy_call_finish_internal (proxy, NULL, res, error);
2905 * g_dbus_proxy_call_sync:
2906 * @proxy: A #GDBusProxy.
2907 * @method_name: Name of method to invoke.
2908 * @parameters: (allow-none): A #GVariant tuple with parameters for the signal
2909 * or %NULL if not passing parameters.
2910 * @flags: Flags from the #GDBusCallFlags enumeration.
2911 * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
2912 * "infinite") or -1 to use the proxy default timeout.
2913 * @cancellable: A #GCancellable or %NULL.
2914 * @error: Return location for error or %NULL.
2916 * Synchronously invokes the @method_name method on @proxy.
2918 * If @method_name contains any dots, then @name is split into interface and
2919 * method name parts. This allows using @proxy for invoking methods on
2922 * If the #GDBusConnection associated with @proxy is disconnected then
2923 * the operation will fail with %G_IO_ERROR_CLOSED. If
2924 * @cancellable is canceled, the operation will fail with
2925 * %G_IO_ERROR_CANCELLED. If @parameters contains a value not
2926 * compatible with the D-Bus protocol, the operation fails with
2927 * %G_IO_ERROR_INVALID_ARGUMENT.
2929 * If the @parameters #GVariant is floating, it is consumed. This allows
2930 * convenient 'inline' use of g_variant_new(), e.g.:
2932 * g_dbus_proxy_call_sync (proxy,
2934 * g_variant_new ("(ss)",
2937 * G_DBUS_CALL_FLAGS_NONE,
2943 * The calling thread is blocked until a reply is received. See
2944 * g_dbus_proxy_call() for the asynchronous version of this
2947 * If @proxy has an expected interface (see
2948 * #GDBusProxy:g-interface-info) and @method_name is referenced by it,
2949 * then the return value is checked against the return type.
2951 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
2952 * return values. Free with g_variant_unref().
2957 g_dbus_proxy_call_sync (GDBusProxy *proxy,
2958 const gchar *method_name,
2959 GVariant *parameters,
2960 GDBusCallFlags flags,
2962 GCancellable *cancellable,
2965 return g_dbus_proxy_call_sync_internal (proxy, method_name, parameters, flags, timeout_msec, NULL, NULL, cancellable, error);
2968 /* ---------------------------------------------------------------------------------------------------- */
2973 * g_dbus_proxy_call_with_unix_fd_list:
2974 * @proxy: A #GDBusProxy.
2975 * @method_name: Name of method to invoke.
2976 * @parameters: (allow-none): A #GVariant tuple with parameters for the signal or %NULL if not passing parameters.
2977 * @flags: Flags from the #GDBusCallFlags enumeration.
2978 * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
2979 * "infinite") or -1 to use the proxy default timeout.
2980 * @fd_list: (allow-none): A #GUnixFDList or %NULL.
2981 * @cancellable: A #GCancellable or %NULL.
2982 * @callback: A #GAsyncReadyCallback to call when the request is satisfied or %NULL if you don't
2983 * care about the result of the method invocation.
2984 * @user_data: The data to pass to @callback.
2986 * Like g_dbus_proxy_call() but also takes a #GUnixFDList object.
2988 * This method is only available on UNIX.
2993 g_dbus_proxy_call_with_unix_fd_list (GDBusProxy *proxy,
2994 const gchar *method_name,
2995 GVariant *parameters,
2996 GDBusCallFlags flags,
2998 GUnixFDList *fd_list,
2999 GCancellable *cancellable,
3000 GAsyncReadyCallback callback,
3003 g_dbus_proxy_call_internal (proxy, method_name, parameters, flags, timeout_msec, fd_list, cancellable, callback, user_data);
3007 * g_dbus_proxy_call_with_unix_fd_list_finish:
3008 * @proxy: A #GDBusProxy.
3009 * @out_fd_list: (out): Return location for a #GUnixFDList or %NULL.
3010 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_proxy_call_with_unix_fd_list().
3011 * @error: Return location for error or %NULL.
3013 * Finishes an operation started with g_dbus_proxy_call_with_unix_fd_list().
3015 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
3016 * return values. Free with g_variant_unref().
3021 g_dbus_proxy_call_with_unix_fd_list_finish (GDBusProxy *proxy,
3022 GUnixFDList **out_fd_list,
3026 return g_dbus_proxy_call_finish_internal (proxy, out_fd_list, res, error);
3030 * g_dbus_proxy_call_with_unix_fd_list_sync:
3031 * @proxy: A #GDBusProxy.
3032 * @method_name: Name of method to invoke.
3033 * @parameters: (allow-none): A #GVariant tuple with parameters for the signal
3034 * or %NULL if not passing parameters.
3035 * @flags: Flags from the #GDBusCallFlags enumeration.
3036 * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
3037 * "infinite") or -1 to use the proxy default timeout.
3038 * @fd_list: (allow-none): A #GUnixFDList or %NULL.
3039 * @out_fd_list: (out): Return location for a #GUnixFDList or %NULL.
3040 * @cancellable: A #GCancellable or %NULL.
3041 * @error: Return location for error or %NULL.
3043 * Like g_dbus_proxy_call_sync() but also takes and returns #GUnixFDList objects.
3045 * This method is only available on UNIX.
3047 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
3048 * return values. Free with g_variant_unref().
3053 g_dbus_proxy_call_with_unix_fd_list_sync (GDBusProxy *proxy,
3054 const gchar *method_name,
3055 GVariant *parameters,
3056 GDBusCallFlags flags,
3058 GUnixFDList *fd_list,
3059 GUnixFDList **out_fd_list,
3060 GCancellable *cancellable,
3063 return g_dbus_proxy_call_sync_internal (proxy, method_name, parameters, flags, timeout_msec, fd_list, out_fd_list, cancellable, error);
3066 #endif /* G_OS_UNIX */
3068 /* ---------------------------------------------------------------------------------------------------- */
3070 static GDBusInterfaceInfo *
3071 _g_dbus_proxy_get_info (GDBusInterface *interface)
3073 GDBusProxy *proxy = G_DBUS_PROXY (interface);
3074 return g_dbus_proxy_get_interface_info (proxy);
3077 static GDBusObject *
3078 _g_dbus_proxy_get_object (GDBusInterface *interface)
3080 GDBusProxy *proxy = G_DBUS_PROXY (interface);
3081 return proxy->priv->object;
3084 static GDBusObject *
3085 _g_dbus_proxy_dup_object (GDBusInterface *interface)
3087 GDBusProxy *proxy = G_DBUS_PROXY (interface);
3088 GDBusObject *ret = NULL;
3090 G_LOCK (properties_lock);
3091 if (proxy->priv->object != NULL)
3092 ret = g_object_ref (proxy->priv->object);
3093 G_UNLOCK (properties_lock);
3098 _g_dbus_proxy_set_object (GDBusInterface *interface,
3099 GDBusObject *object)
3101 GDBusProxy *proxy = G_DBUS_PROXY (interface);
3102 G_LOCK (properties_lock);
3103 if (proxy->priv->object != NULL)
3104 g_object_remove_weak_pointer (G_OBJECT (proxy->priv->object), (gpointer *) &proxy->priv->object);
3105 proxy->priv->object = object;
3106 if (proxy->priv->object != NULL)
3107 g_object_add_weak_pointer (G_OBJECT (proxy->priv->object), (gpointer *) &proxy->priv->object);
3108 G_UNLOCK (properties_lock);
3112 dbus_interface_iface_init (GDBusInterfaceIface *dbus_interface_iface)
3114 dbus_interface_iface->get_info = _g_dbus_proxy_get_info;
3115 dbus_interface_iface->get_object = _g_dbus_proxy_get_object;
3116 dbus_interface_iface->dup_object = _g_dbus_proxy_dup_object;
3117 dbus_interface_iface->set_object = _g_dbus_proxy_set_object;
3120 /* ---------------------------------------------------------------------------------------------------- */