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_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: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),
637 g_type_class_add_private (klass, sizeof (GDBusProxyPrivate));
641 g_dbus_proxy_init (GDBusProxy *proxy)
643 proxy->priv = G_TYPE_INSTANCE_GET_PRIVATE (proxy, G_TYPE_DBUS_PROXY, GDBusProxyPrivate);
644 proxy->priv->signal_subscription_data = g_slice_new0 (SignalSubscriptionData);
645 proxy->priv->signal_subscription_data->ref_count = 1;
646 proxy->priv->signal_subscription_data->proxy = proxy;
647 proxy->priv->properties = g_hash_table_new_full (g_str_hash,
650 (GDestroyNotify) g_variant_unref);
653 /* ---------------------------------------------------------------------------------------------------- */
656 property_name_sort_func (const gchar **a,
659 return g_strcmp0 (*a, *b);
663 * g_dbus_proxy_get_cached_property_names:
664 * @proxy: A #GDBusProxy.
666 * Gets the names of all cached properties on @proxy.
668 * Returns: (transfer full): A %NULL-terminated array of strings or %NULL if
669 * @proxy has no cached properties. Free the returned array with
675 g_dbus_proxy_get_cached_property_names (GDBusProxy *proxy)
682 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
684 G_LOCK (properties_lock);
687 if (g_hash_table_size (proxy->priv->properties) == 0)
690 p = g_ptr_array_new ();
692 g_hash_table_iter_init (&iter, proxy->priv->properties);
693 while (g_hash_table_iter_next (&iter, (gpointer) &key, NULL))
694 g_ptr_array_add (p, g_strdup (key));
695 g_ptr_array_sort (p, (GCompareFunc) property_name_sort_func);
696 g_ptr_array_add (p, NULL);
698 names = (gchar **) g_ptr_array_free (p, FALSE);
701 G_UNLOCK (properties_lock);
705 /* properties_lock must be held for as long as you will keep the
708 static const GDBusPropertyInfo *
709 lookup_property_info (GDBusProxy *proxy,
710 const gchar *property_name)
712 const GDBusPropertyInfo *info = NULL;
714 if (proxy->priv->expected_interface == NULL)
717 info = g_dbus_interface_info_lookup_property (proxy->priv->expected_interface, property_name);
724 * g_dbus_proxy_get_cached_property:
725 * @proxy: A #GDBusProxy.
726 * @property_name: Property name.
728 * Looks up the value for a property from the cache. This call does no
731 * If @proxy has an expected interface (see
732 * #GDBusProxy:g-interface-info) and @property_name is referenced by
733 * it, then @value is checked against the type of the property.
735 * Returns: A reference to the #GVariant instance that holds the value
736 * for @property_name or %NULL if the value is not in the cache. The
737 * returned reference must be freed with g_variant_unref().
742 g_dbus_proxy_get_cached_property (GDBusProxy *proxy,
743 const gchar *property_name)
745 const GDBusPropertyInfo *info;
748 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
749 g_return_val_if_fail (property_name != NULL, NULL);
751 G_LOCK (properties_lock);
753 value = g_hash_table_lookup (proxy->priv->properties, property_name);
757 info = lookup_property_info (proxy, property_name);
760 const gchar *type_string = g_variant_get_type_string (value);
761 if (g_strcmp0 (type_string, info->signature) != 0)
763 g_warning ("Trying to get property %s with type %s but according to the expected "
764 "interface the type is %s",
773 g_variant_ref (value);
776 G_UNLOCK (properties_lock);
781 * g_dbus_proxy_set_cached_property:
782 * @proxy: A #GDBusProxy
783 * @property_name: Property name.
784 * @value: (allow-none): Value for the property or %NULL to remove it from the cache.
786 * If @value is not %NULL, sets the cached value for the property with
787 * name @property_name to the value in @value.
789 * If @value is %NULL, then the cached value is removed from the
792 * If @proxy has an expected interface (see
793 * #GDBusProxy:g-interface-info) and @property_name is referenced by
794 * it, then @value is checked against the type of the property.
796 * If the @value #GVariant is floating, it is consumed. This allows
797 * convenient 'inline' use of g_variant_new(), e.g.
799 * g_dbus_proxy_set_cached_property (proxy,
801 * g_variant_new ("(si)",
806 * Normally you will not need to use this method since @proxy is
807 * tracking changes using the
808 * <literal>org.freedesktop.DBus.Properties.PropertiesChanged</literal>
809 * D-Bus signal. However, for performance reasons an object may decide
810 * to not use this signal for some properties and instead use a
811 * proprietary out-of-band mechanism to transmit changes.
813 * As a concrete example, consider an object with a property
814 * <literal>ChatroomParticipants</literal> which is an array of
815 * strings. Instead of transmitting the same (long) array every time
816 * the property changes, it is more efficient to only transmit the
817 * delta using e.g. signals <literal>ChatroomParticipantJoined(String
818 * name)</literal> and <literal>ChatroomParticipantParted(String
824 g_dbus_proxy_set_cached_property (GDBusProxy *proxy,
825 const gchar *property_name,
828 const GDBusPropertyInfo *info;
830 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
831 g_return_if_fail (property_name != NULL);
833 G_LOCK (properties_lock);
837 info = lookup_property_info (proxy, property_name);
840 if (g_strcmp0 (info->signature, g_variant_get_type_string (value)) != 0)
842 g_warning ("Trying to set property %s of type %s but according to the expected "
843 "interface the type is %s",
845 g_variant_get_type_string (value),
850 g_hash_table_insert (proxy->priv->properties,
851 g_strdup (property_name),
852 g_variant_ref_sink (value));
856 g_hash_table_remove (proxy->priv->properties, property_name);
860 G_UNLOCK (properties_lock);
863 /* ---------------------------------------------------------------------------------------------------- */
866 on_signal_received (GDBusConnection *connection,
867 const gchar *sender_name,
868 const gchar *object_path,
869 const gchar *interface_name,
870 const gchar *signal_name,
871 GVariant *parameters,
874 SignalSubscriptionData *data = user_data;
877 G_LOCK (signal_subscription_lock);
881 G_UNLOCK (signal_subscription_lock);
886 g_object_ref (proxy);
887 G_UNLOCK (signal_subscription_lock);
890 if (!proxy->priv->initialized)
893 G_LOCK (properties_lock);
895 if (proxy->priv->name_owner != NULL && g_strcmp0 (sender_name, proxy->priv->name_owner) != 0)
897 G_UNLOCK (properties_lock);
901 if (proxy->priv->expected_interface != NULL)
903 const GDBusSignalInfo *info;
904 info = g_dbus_interface_info_lookup_signal (proxy->priv->expected_interface, signal_name);
907 GVariantType *expected_type;
908 expected_type = _g_dbus_compute_complete_signature (info->args);
909 if (!g_variant_type_equal (expected_type, g_variant_get_type (parameters)))
911 gchar *expected_type_string = g_variant_type_dup_string (expected_type);
912 g_warning ("Dropping signal %s of type %s since the type from the expected interface is %s",
914 g_variant_get_type_string (parameters),
915 expected_type_string);
916 g_free (expected_type_string);
917 g_variant_type_free (expected_type);
918 G_UNLOCK (properties_lock);
921 g_variant_type_free (expected_type);
925 G_UNLOCK (properties_lock);
927 g_signal_emit (proxy,
928 signals[SIGNAL_SIGNAL],
936 g_object_unref (proxy);
939 /* ---------------------------------------------------------------------------------------------------- */
941 /* must hold properties_lock */
943 insert_property_checked (GDBusProxy *proxy,
944 gchar *property_name,
947 if (proxy->priv->expected_interface != NULL)
949 const GDBusPropertyInfo *info;
950 info = g_dbus_interface_info_lookup_property (proxy->priv->expected_interface, property_name);
951 /* Only check known properties */
954 /* Warn about properties with the wrong type */
955 if (g_strcmp0 (info->signature, g_variant_get_type_string (value)) != 0)
957 g_warning ("Received property %s with type %s does not match expected type "
958 "%s in the expected interface",
960 g_variant_get_type_string (value),
967 g_hash_table_insert (proxy->priv->properties,
968 property_name, /* adopts string */
969 value); /* adopts value */
974 g_variant_unref (value);
975 g_free (property_name);
982 } InvalidatedPropGetData;
985 invalidated_property_get_cb (GDBusConnection *connection,
989 InvalidatedPropGetData *data = user_data;
990 const gchar *invalidated_properties[] = {NULL};
991 GVariantBuilder builder;
992 GVariant *value = NULL;
993 GVariant *unpacked_value = NULL;
995 /* errors are fine, the other end could have disconnected */
996 value = g_dbus_connection_call_finish (connection, res, NULL);
1002 if (!g_variant_is_of_type (value, G_VARIANT_TYPE ("(v)")))
1004 g_warning ("Expected type `(v)' for Get() reply, got `%s'", g_variant_get_type_string (value));
1008 g_variant_get (value, "(v)", &unpacked_value);
1010 /* synthesize the a{sv} in the PropertiesChanged signal */
1011 g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
1012 g_variant_builder_add (&builder, "{sv}", data->prop_name, unpacked_value);
1014 G_LOCK (properties_lock);
1015 insert_property_checked (data->proxy,
1016 data->prop_name, /* adopts string */
1017 unpacked_value); /* adopts value */
1018 data->prop_name = NULL;
1019 G_UNLOCK (properties_lock);
1021 g_signal_emit (data->proxy,
1022 signals[PROPERTIES_CHANGED_SIGNAL], 0,
1023 g_variant_builder_end (&builder), /* consumed */
1024 invalidated_properties);
1029 g_variant_unref (value);
1030 g_object_unref (data->proxy);
1031 g_free (data->prop_name);
1032 g_slice_free (InvalidatedPropGetData, data);
1036 on_properties_changed (GDBusConnection *connection,
1037 const gchar *sender_name,
1038 const gchar *object_path,
1039 const gchar *interface_name,
1040 const gchar *signal_name,
1041 GVariant *parameters,
1044 SignalSubscriptionData *data = user_data;
1045 gboolean emit_g_signal = FALSE;
1047 const gchar *interface_name_for_signal;
1048 GVariant *changed_properties;
1049 gchar **invalidated_properties;
1055 changed_properties = NULL;
1056 invalidated_properties = NULL;
1058 G_LOCK (signal_subscription_lock);
1059 proxy = data->proxy;
1062 G_UNLOCK (signal_subscription_lock);
1067 g_object_ref (proxy);
1068 G_UNLOCK (signal_subscription_lock);
1071 if (!proxy->priv->initialized)
1074 G_LOCK (properties_lock);
1076 if (proxy->priv->name_owner != NULL && g_strcmp0 (sender_name, proxy->priv->name_owner) != 0)
1078 G_UNLOCK (properties_lock);
1082 if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(sa{sv}as)")))
1084 g_warning ("Value for PropertiesChanged signal with type `%s' does not match `(sa{sv}as)'",
1085 g_variant_get_type_string (parameters));
1086 G_UNLOCK (properties_lock);
1090 g_variant_get (parameters,
1092 &interface_name_for_signal,
1093 &changed_properties,
1094 &invalidated_properties);
1096 if (g_strcmp0 (interface_name_for_signal, proxy->priv->interface_name) != 0)
1098 G_UNLOCK (properties_lock);
1102 g_variant_iter_init (&iter, changed_properties);
1103 while (g_variant_iter_next (&iter, "{sv}", &key, &value))
1105 insert_property_checked (proxy,
1106 key, /* adopts string */
1107 value); /* adopts value */
1108 emit_g_signal = TRUE;
1111 if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_GET_INVALIDATED_PROPERTIES)
1113 if (proxy->priv->name_owner != NULL)
1115 for (n = 0; invalidated_properties[n] != NULL; n++)
1117 InvalidatedPropGetData *data;
1118 data = g_slice_new0 (InvalidatedPropGetData);
1119 data->proxy = g_object_ref (proxy);
1120 data->prop_name = g_strdup (invalidated_properties[n]);
1121 g_dbus_connection_call (proxy->priv->connection,
1122 proxy->priv->name_owner,
1123 proxy->priv->object_path,
1124 "org.freedesktop.DBus.Properties",
1126 g_variant_new ("(ss)", proxy->priv->interface_name, data->prop_name),
1127 G_VARIANT_TYPE ("(v)"),
1128 G_DBUS_CALL_FLAGS_NONE,
1130 NULL, /* GCancellable */
1131 (GAsyncReadyCallback) invalidated_property_get_cb,
1138 emit_g_signal = TRUE;
1139 for (n = 0; invalidated_properties[n] != NULL; n++)
1141 g_hash_table_remove (proxy->priv->properties, invalidated_properties[n]);
1145 G_UNLOCK (properties_lock);
1149 g_signal_emit (proxy, signals[PROPERTIES_CHANGED_SIGNAL],
1152 invalidated_properties);
1156 if (changed_properties != NULL)
1157 g_variant_unref (changed_properties);
1158 g_free (invalidated_properties);
1160 g_object_unref (proxy);
1163 /* ---------------------------------------------------------------------------------------------------- */
1166 process_get_all_reply (GDBusProxy *proxy,
1172 guint num_properties;
1174 if (!g_variant_is_of_type (result, G_VARIANT_TYPE ("(a{sv})")))
1176 g_warning ("Value for GetAll reply with type `%s' does not match `(a{sv})'",
1177 g_variant_get_type_string (result));
1181 G_LOCK (properties_lock);
1183 g_variant_get (result, "(a{sv})", &iter);
1184 while (g_variant_iter_next (iter, "{sv}", &key, &value))
1186 insert_property_checked (proxy,
1187 key, /* adopts string */
1188 value); /* adopts value */
1190 g_variant_iter_free (iter);
1192 num_properties = g_hash_table_size (proxy->priv->properties);
1193 G_UNLOCK (properties_lock);
1195 /* Synthesize ::g-properties-changed changed */
1196 if (num_properties > 0)
1198 GVariant *changed_properties;
1199 const gchar *invalidated_properties[1] = {NULL};
1201 g_variant_get (result,
1203 &changed_properties);
1204 g_signal_emit (proxy, signals[PROPERTIES_CHANGED_SIGNAL],
1207 invalidated_properties);
1208 g_variant_unref (changed_properties);
1218 GCancellable *cancellable;
1220 } LoadPropertiesOnNameOwnerChangedData;
1223 on_name_owner_changed_get_all_cb (GDBusConnection *connection,
1227 LoadPropertiesOnNameOwnerChangedData *data = user_data;
1235 result = g_dbus_connection_call_finish (connection,
1240 if (error->domain == G_IO_ERROR && error->code == G_IO_ERROR_CANCELLED)
1242 /* We just ignore if GetAll() is failing. Because this might happen
1243 * if the object has no properties at all. Or if the caller is
1244 * not authorized to see the properties.
1246 * Either way, apps can know about this by using
1247 * get_cached_property_names() or get_cached_property().
1249 * TODO: handle G_DBUS_DEBUG flag 'proxy' and, if enabled, log the
1250 * fact that GetAll() failed
1252 //g_debug ("error: %d %d %s", error->domain, error->code, error->message);
1253 g_error_free (error);
1256 /* and finally we can notify */
1259 G_LOCK (properties_lock);
1260 g_free (data->proxy->priv->name_owner);
1261 data->proxy->priv->name_owner = data->name_owner;
1262 data->name_owner = NULL; /* to avoid an extra copy, we steal the string */
1263 g_hash_table_remove_all (data->proxy->priv->properties);
1264 G_UNLOCK (properties_lock);
1267 process_get_all_reply (data->proxy, result);
1268 g_variant_unref (result);
1271 g_object_notify (G_OBJECT (data->proxy), "g-name-owner");
1274 if (data->cancellable == data->proxy->priv->get_all_cancellable)
1275 data->proxy->priv->get_all_cancellable = NULL;
1277 g_object_unref (data->proxy);
1278 g_object_unref (data->cancellable);
1279 g_free (data->name_owner);
1284 on_name_owner_changed (GDBusConnection *connection,
1285 const gchar *sender_name,
1286 const gchar *object_path,
1287 const gchar *interface_name,
1288 const gchar *signal_name,
1289 GVariant *parameters,
1292 SignalSubscriptionData *data = user_data;
1294 const gchar *old_owner;
1295 const gchar *new_owner;
1297 G_LOCK (signal_subscription_lock);
1298 proxy = data->proxy;
1301 G_UNLOCK (signal_subscription_lock);
1306 g_object_ref (proxy);
1307 G_UNLOCK (signal_subscription_lock);
1310 /* if we are already trying to load properties, cancel that */
1311 if (proxy->priv->get_all_cancellable != NULL)
1313 g_cancellable_cancel (proxy->priv->get_all_cancellable);
1314 proxy->priv->get_all_cancellable = NULL;
1317 g_variant_get (parameters,
1323 if (strlen (new_owner) == 0)
1325 G_LOCK (properties_lock);
1326 g_free (proxy->priv->name_owner);
1327 proxy->priv->name_owner = NULL;
1329 /* Synthesize ::g-properties-changed changed */
1330 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES) &&
1331 g_hash_table_size (proxy->priv->properties) > 0)
1333 GVariantBuilder builder;
1334 GPtrArray *invalidated_properties;
1335 GHashTableIter iter;
1338 /* Build changed_properties (always empty) and invalidated_properties ... */
1339 g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
1341 invalidated_properties = g_ptr_array_new_with_free_func (g_free);
1342 g_hash_table_iter_init (&iter, proxy->priv->properties);
1343 while (g_hash_table_iter_next (&iter, (gpointer) &key, NULL))
1344 g_ptr_array_add (invalidated_properties, g_strdup (key));
1345 g_ptr_array_add (invalidated_properties, NULL);
1347 /* ... throw out the properties ... */
1348 g_hash_table_remove_all (proxy->priv->properties);
1350 G_UNLOCK (properties_lock);
1352 /* ... and finally emit the ::g-properties-changed signal */
1353 g_signal_emit (proxy, signals[PROPERTIES_CHANGED_SIGNAL],
1355 g_variant_builder_end (&builder) /* consumed */,
1356 (const gchar* const *) invalidated_properties->pdata);
1357 g_ptr_array_unref (invalidated_properties);
1361 G_UNLOCK (properties_lock);
1363 g_object_notify (G_OBJECT (proxy), "g-name-owner");
1367 G_LOCK (properties_lock);
1369 /* ignore duplicates - this can happen when activating the service */
1370 if (g_strcmp0 (new_owner, proxy->priv->name_owner) == 0)
1372 G_UNLOCK (properties_lock);
1376 if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES)
1378 g_free (proxy->priv->name_owner);
1379 proxy->priv->name_owner = g_strdup (new_owner);
1381 g_hash_table_remove_all (proxy->priv->properties);
1382 G_UNLOCK (properties_lock);
1383 g_object_notify (G_OBJECT (proxy), "g-name-owner");
1387 LoadPropertiesOnNameOwnerChangedData *data;
1389 G_UNLOCK (properties_lock);
1391 /* start loading properties.. only then emit notify::g-name-owner .. we
1392 * need to be able to cancel this in the event another NameOwnerChanged
1393 * signal suddenly happens
1396 g_assert (proxy->priv->get_all_cancellable == NULL);
1397 proxy->priv->get_all_cancellable = g_cancellable_new ();
1398 data = g_new0 (LoadPropertiesOnNameOwnerChangedData, 1);
1399 data->proxy = g_object_ref (proxy);
1400 data->cancellable = proxy->priv->get_all_cancellable;
1401 data->name_owner = g_strdup (new_owner);
1402 g_dbus_connection_call (proxy->priv->connection,
1404 proxy->priv->object_path,
1405 "org.freedesktop.DBus.Properties",
1407 g_variant_new ("(s)", proxy->priv->interface_name),
1408 G_VARIANT_TYPE ("(a{sv})"),
1409 G_DBUS_CALL_FLAGS_NONE,
1411 proxy->priv->get_all_cancellable,
1412 (GAsyncReadyCallback) on_name_owner_changed_get_all_cb,
1419 g_object_unref (proxy);
1422 /* ---------------------------------------------------------------------------------------------------- */
1427 GCancellable *cancellable;
1428 GSimpleAsyncResult *simple;
1432 async_init_data_free (AsyncInitData *data)
1434 g_object_unref (data->proxy);
1435 if (data->cancellable != NULL)
1436 g_object_unref (data->cancellable);
1437 g_object_unref (data->simple);
1442 async_init_get_all_cb (GDBusConnection *connection,
1446 AsyncInitData *data = user_data;
1451 result = g_dbus_connection_call_finish (connection,
1456 /* We just ignore if GetAll() is failing. Because this might happen
1457 * if the object has no properties at all. Or if the caller is
1458 * not authorized to see the properties.
1460 * Either way, apps can know about this by using
1461 * get_cached_property_names() or get_cached_property().
1463 * TODO: handle G_DBUS_DEBUG flag 'proxy' and, if enabled, log the
1464 * fact that GetAll() failed
1466 //g_debug ("error: %d %d %s", error->domain, error->code, error->message);
1467 g_error_free (error);
1471 g_simple_async_result_set_op_res_gpointer (data->simple,
1473 (GDestroyNotify) g_variant_unref);
1476 g_simple_async_result_complete_in_idle (data->simple);
1477 async_init_data_free (data);
1481 async_init_data_set_name_owner (AsyncInitData *data,
1482 const gchar *name_owner)
1487 if (name_owner != NULL)
1489 /* it starts as NULL anyway */
1490 G_LOCK (properties_lock);
1491 data->proxy->priv->name_owner = g_strdup (name_owner);
1492 G_UNLOCK (properties_lock);
1497 if (data->proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES)
1499 /* Don't load properties if the API user doesn't want them */
1502 else if (name_owner == NULL && data->proxy->priv->name != NULL)
1504 /* Don't attempt to load properties if the name_owner is NULL (which
1505 * usually means the name isn't owned), unless name is also NULL (which
1506 * means we actually wanted to talk to the directly-connected process -
1507 * either dbus-daemon or a peer - instead of going via dbus-daemon)
1514 /* load all properties asynchronously */
1515 g_dbus_connection_call (data->proxy->priv->connection,
1517 data->proxy->priv->object_path,
1518 "org.freedesktop.DBus.Properties",
1520 g_variant_new ("(s)", data->proxy->priv->interface_name),
1521 G_VARIANT_TYPE ("(a{sv})"),
1522 G_DBUS_CALL_FLAGS_NONE,
1525 (GAsyncReadyCallback) async_init_get_all_cb,
1530 g_simple_async_result_complete_in_idle (data->simple);
1531 async_init_data_free (data);
1536 async_init_get_name_owner_cb (GDBusConnection *connection,
1540 AsyncInitData *data = user_data;
1545 result = g_dbus_connection_call_finish (connection,
1550 if (error->domain == G_DBUS_ERROR &&
1551 error->code == G_DBUS_ERROR_NAME_HAS_NO_OWNER)
1553 g_error_free (error);
1554 async_init_data_set_name_owner (data, NULL);
1558 g_simple_async_result_take_error (data->simple, error);
1559 g_simple_async_result_complete_in_idle (data->simple);
1560 async_init_data_free (data);
1565 /* borrowed from result to avoid an extra copy */
1566 const gchar *name_owner;
1568 g_variant_get (result, "(&s)", &name_owner);
1569 async_init_data_set_name_owner (data, name_owner);
1570 g_variant_unref (result);
1575 async_init_call_get_name_owner (AsyncInitData *data)
1577 g_dbus_connection_call (data->proxy->priv->connection,
1578 "org.freedesktop.DBus", /* name */
1579 "/org/freedesktop/DBus", /* object path */
1580 "org.freedesktop.DBus", /* interface */
1582 g_variant_new ("(s)",
1583 data->proxy->priv->name),
1584 G_VARIANT_TYPE ("(s)"),
1585 G_DBUS_CALL_FLAGS_NONE,
1588 (GAsyncReadyCallback) async_init_get_name_owner_cb,
1593 async_init_start_service_by_name_cb (GDBusConnection *connection,
1597 AsyncInitData *data = user_data;
1602 result = g_dbus_connection_call_finish (connection,
1607 /* Errors are not unexpected; the bus will reply e.g.
1609 * org.freedesktop.DBus.Error.ServiceUnknown: The name org.gnome.Epiphany2
1610 * was not provided by any .service files
1614 * org.freedesktop.systemd1.Masked: Unit polkit.service is masked.
1616 * This doesn't mean that the name doesn't have an owner, just
1617 * that it's not provided by a .service file or can't currently
1620 * In particular, in both cases, it could be that a service
1621 * owner will actually appear later. So instead of erroring out,
1622 * we just proceed to invoke GetNameOwner() if dealing with the
1623 * kind of errors above.
1625 if (error->domain == G_DBUS_ERROR && error->code == G_DBUS_ERROR_SERVICE_UNKNOWN)
1627 g_error_free (error);
1631 gchar *remote_error = g_dbus_error_get_remote_error (error);
1632 if (g_strcmp0 (remote_error, "org.freedesktop.systemd1.Masked") == 0)
1634 g_error_free (error);
1635 g_free (remote_error);
1639 g_prefix_error (&error,
1640 _("Error calling StartServiceByName for %s: "),
1641 data->proxy->priv->name);
1642 g_free (remote_error);
1649 guint32 start_service_result;
1650 g_variant_get (result,
1652 &start_service_result);
1653 g_variant_unref (result);
1654 if (start_service_result == 1 || /* DBUS_START_REPLY_SUCCESS */
1655 start_service_result == 2) /* DBUS_START_REPLY_ALREADY_RUNNING */
1657 /* continue to invoke GetNameOwner() */
1661 error = g_error_new (G_IO_ERROR,
1663 _("Unexpected reply %d from StartServiceByName(\"%s\") method"),
1664 start_service_result,
1665 data->proxy->priv->name);
1670 async_init_call_get_name_owner (data);
1674 g_warn_if_fail (error != NULL);
1675 g_simple_async_result_take_error (data->simple, error);
1676 g_simple_async_result_complete_in_idle (data->simple);
1677 async_init_data_free (data);
1681 async_init_call_start_service_by_name (AsyncInitData *data)
1683 g_dbus_connection_call (data->proxy->priv->connection,
1684 "org.freedesktop.DBus", /* name */
1685 "/org/freedesktop/DBus", /* object path */
1686 "org.freedesktop.DBus", /* interface */
1687 "StartServiceByName",
1688 g_variant_new ("(su)",
1689 data->proxy->priv->name,
1691 G_VARIANT_TYPE ("(u)"),
1692 G_DBUS_CALL_FLAGS_NONE,
1695 (GAsyncReadyCallback) async_init_start_service_by_name_cb,
1700 async_initable_init_second_async (GAsyncInitable *initable,
1702 GCancellable *cancellable,
1703 GAsyncReadyCallback callback,
1706 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1707 AsyncInitData *data;
1709 data = g_new0 (AsyncInitData, 1);
1710 data->proxy = g_object_ref (proxy);
1711 data->cancellable = cancellable != NULL ? g_object_ref (cancellable) : NULL;
1712 data->simple = g_simple_async_result_new (G_OBJECT (proxy),
1716 g_simple_async_result_set_check_cancellable (data->simple, cancellable);
1718 /* Check name ownership asynchronously - possibly also start the service */
1719 if (proxy->priv->name == NULL)
1722 async_init_data_set_name_owner (data, NULL);
1724 else if (g_dbus_is_unique_name (proxy->priv->name))
1726 async_init_data_set_name_owner (data, proxy->priv->name);
1730 if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START)
1732 async_init_call_get_name_owner (data);
1736 async_init_call_start_service_by_name (data);
1742 async_initable_init_second_finish (GAsyncInitable *initable,
1746 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1747 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1753 if (g_simple_async_result_propagate_error (simple, error))
1756 result = g_simple_async_result_get_op_res_gpointer (simple);
1759 process_get_all_reply (proxy, result);
1765 proxy->priv->initialized = TRUE;
1769 /* ---------------------------------------------------------------------------------------------------- */
1772 async_initable_init_first (GAsyncInitable *initable)
1774 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1776 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES))
1778 /* subscribe to PropertiesChanged() */
1779 proxy->priv->properties_changed_subscription_id =
1780 g_dbus_connection_signal_subscribe (proxy->priv->connection,
1782 "org.freedesktop.DBus.Properties",
1783 "PropertiesChanged",
1784 proxy->priv->object_path,
1785 proxy->priv->interface_name,
1786 G_DBUS_SIGNAL_FLAGS_NONE,
1787 on_properties_changed,
1788 signal_subscription_ref (proxy->priv->signal_subscription_data),
1789 (GDestroyNotify) signal_subscription_unref);
1792 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS))
1794 /* subscribe to all signals for the object */
1795 proxy->priv->signals_subscription_id =
1796 g_dbus_connection_signal_subscribe (proxy->priv->connection,
1798 proxy->priv->interface_name,
1800 proxy->priv->object_path,
1802 G_DBUS_SIGNAL_FLAGS_NONE,
1804 signal_subscription_ref (proxy->priv->signal_subscription_data),
1805 (GDestroyNotify) signal_subscription_unref);
1808 if (proxy->priv->name != NULL && !g_dbus_is_unique_name (proxy->priv->name))
1810 proxy->priv->name_owner_changed_subscription_id =
1811 g_dbus_connection_signal_subscribe (proxy->priv->connection,
1812 "org.freedesktop.DBus", /* name */
1813 "org.freedesktop.DBus", /* interface */
1814 "NameOwnerChanged", /* signal name */
1815 "/org/freedesktop/DBus", /* path */
1816 proxy->priv->name, /* arg0 */
1817 G_DBUS_SIGNAL_FLAGS_NONE,
1818 on_name_owner_changed,
1819 signal_subscription_ref (proxy->priv->signal_subscription_data),
1820 (GDestroyNotify) signal_subscription_unref);
1824 /* ---------------------------------------------------------------------------------------------------- */
1826 /* initialization is split into two parts - the first is the
1827 * non-blocing part that requires the callers GMainContext - the
1828 * second is a blocking part async part that doesn't require the
1829 * callers GMainContext.. we do this split so the code can be reused
1830 * in the GInitable implementation below.
1832 * Note that obtaining a GDBusConnection is not shared between the two
1840 GCancellable *cancellable;
1841 GAsyncReadyCallback callback;
1843 } GetConnectionData;
1846 get_connection_cb (GObject *source_object,
1850 GetConnectionData *data = user_data;
1854 data->proxy->priv->connection = g_bus_get_finish (res, &error);
1855 if (data->proxy->priv->connection == NULL)
1857 GSimpleAsyncResult *simple;
1858 simple = g_simple_async_result_new (G_OBJECT (data->proxy),
1862 g_simple_async_result_set_check_cancellable (simple, data->cancellable);
1863 g_simple_async_result_take_error (simple, error);
1864 g_simple_async_result_complete_in_idle (simple);
1865 g_object_unref (simple);
1869 async_initable_init_first (G_ASYNC_INITABLE (data->proxy));
1870 async_initable_init_second_async (G_ASYNC_INITABLE (data->proxy),
1877 if (data->cancellable != NULL)
1878 g_object_unref (data->cancellable);
1880 g_object_unref (data->proxy);
1885 async_initable_init_async (GAsyncInitable *initable,
1887 GCancellable *cancellable,
1888 GAsyncReadyCallback callback,
1891 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1893 if (proxy->priv->bus_type != G_BUS_TYPE_NONE)
1895 GetConnectionData *data;
1897 g_assert (proxy->priv->connection == NULL);
1899 data = g_new0 (GetConnectionData, 1);
1900 data->proxy = g_object_ref (proxy);
1901 data->io_priority = io_priority;
1902 data->cancellable = cancellable != NULL ? g_object_ref (cancellable) : NULL;
1903 data->callback = callback;
1904 data->user_data = user_data;
1905 g_bus_get (proxy->priv->bus_type,
1912 async_initable_init_first (initable);
1913 async_initable_init_second_async (initable, io_priority, cancellable, callback, user_data);
1918 async_initable_init_finish (GAsyncInitable *initable,
1922 return async_initable_init_second_finish (initable, res, error);
1926 async_initable_iface_init (GAsyncInitableIface *async_initable_iface)
1928 async_initable_iface->init_async = async_initable_init_async;
1929 async_initable_iface->init_finish = async_initable_init_finish;
1932 /* ---------------------------------------------------------------------------------------------------- */
1936 GMainContext *context;
1939 } InitableAsyncInitableData;
1942 async_initable_init_async_cb (GObject *source_object,
1946 InitableAsyncInitableData *data = user_data;
1947 data->res = g_object_ref (res);
1948 g_main_loop_quit (data->loop);
1951 /* Simply reuse the GAsyncInitable implementation but run the first
1952 * part (that is non-blocking and requires the callers GMainContext)
1953 * with the callers GMainContext.. and the second with a private
1954 * GMainContext (bug 621310 is slightly related).
1956 * Note that obtaining a GDBusConnection is not shared between the two
1960 initable_init (GInitable *initable,
1961 GCancellable *cancellable,
1964 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1965 InitableAsyncInitableData *data;
1970 if (proxy->priv->bus_type != G_BUS_TYPE_NONE)
1972 g_assert (proxy->priv->connection == NULL);
1973 proxy->priv->connection = g_bus_get_sync (proxy->priv->bus_type,
1976 if (proxy->priv->connection == NULL)
1980 async_initable_init_first (G_ASYNC_INITABLE (initable));
1982 data = g_new0 (InitableAsyncInitableData, 1);
1983 data->context = g_main_context_new ();
1984 data->loop = g_main_loop_new (data->context, FALSE);
1986 g_main_context_push_thread_default (data->context);
1988 async_initable_init_second_async (G_ASYNC_INITABLE (initable),
1991 async_initable_init_async_cb,
1994 g_main_loop_run (data->loop);
1996 ret = async_initable_init_second_finish (G_ASYNC_INITABLE (initable),
2000 g_main_context_pop_thread_default (data->context);
2002 g_main_context_unref (data->context);
2003 g_main_loop_unref (data->loop);
2004 g_object_unref (data->res);
2013 initable_iface_init (GInitableIface *initable_iface)
2015 initable_iface->init = initable_init;
2018 /* ---------------------------------------------------------------------------------------------------- */
2022 * @connection: A #GDBusConnection.
2023 * @flags: Flags used when constructing the proxy.
2024 * @info: (allow-none): A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
2025 * @name: (allow-none): A bus name (well-known or unique) or %NULL if @connection is not a message bus connection.
2026 * @object_path: An object path.
2027 * @interface_name: A D-Bus interface name.
2028 * @cancellable: (allow-none): A #GCancellable or %NULL.
2029 * @callback: Callback function to invoke when the proxy is ready.
2030 * @user_data: User data to pass to @callback.
2032 * Creates a proxy for accessing @interface_name on the remote object
2033 * at @object_path owned by @name at @connection and asynchronously
2034 * loads D-Bus properties unless the
2035 * %G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES flag is used. Connect to
2036 * the #GDBusProxy::g-properties-changed signal to get notified about
2039 * If the %G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS flag is not set, also sets up
2040 * match rules for signals. Connect to the #GDBusProxy::g-signal signal
2041 * to handle signals from the remote object.
2043 * If @name is a well-known name and the
2044 * %G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START flag isn't set and no name
2045 * owner currently exists, the message bus will be requested to launch
2046 * a name owner for the name.
2048 * This is a failable asynchronous constructor - when the proxy is
2049 * ready, @callback will be invoked and you can use
2050 * g_dbus_proxy_new_finish() to get the result.
2052 * See g_dbus_proxy_new_sync() and for a synchronous version of this constructor.
2054 * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
2059 g_dbus_proxy_new (GDBusConnection *connection,
2060 GDBusProxyFlags flags,
2061 GDBusInterfaceInfo *info,
2063 const gchar *object_path,
2064 const gchar *interface_name,
2065 GCancellable *cancellable,
2066 GAsyncReadyCallback callback,
2069 g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
2070 g_return_if_fail ((name == NULL && g_dbus_connection_get_unique_name (connection) == NULL) || g_dbus_is_name (name));
2071 g_return_if_fail (g_variant_is_object_path (object_path));
2072 g_return_if_fail (g_dbus_is_interface_name (interface_name));
2074 g_async_initable_new_async (G_TYPE_DBUS_PROXY,
2080 "g-interface-info", info,
2082 "g-connection", connection,
2083 "g-object-path", object_path,
2084 "g-interface-name", interface_name,
2089 * g_dbus_proxy_new_finish:
2090 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback function passed to g_dbus_proxy_new().
2091 * @error: Return location for error or %NULL.
2093 * Finishes creating a #GDBusProxy.
2095 * Returns: A #GDBusProxy or %NULL if @error is set. Free with g_object_unref().
2100 g_dbus_proxy_new_finish (GAsyncResult *res,
2104 GObject *source_object;
2106 source_object = g_async_result_get_source_object (res);
2107 g_assert (source_object != NULL);
2109 object = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object),
2112 g_object_unref (source_object);
2115 return G_DBUS_PROXY (object);
2121 * g_dbus_proxy_new_sync:
2122 * @connection: A #GDBusConnection.
2123 * @flags: Flags used when constructing the proxy.
2124 * @info: (allow-none): A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
2125 * @name: (allow-none): A bus name (well-known or unique) or %NULL if @connection is not a message bus connection.
2126 * @object_path: An object path.
2127 * @interface_name: A D-Bus interface name.
2128 * @cancellable: (allow-none): A #GCancellable or %NULL.
2129 * @error: (allow-none): Return location for error or %NULL.
2131 * Creates a proxy for accessing @interface_name on the remote object
2132 * at @object_path owned by @name at @connection and synchronously
2133 * loads D-Bus properties unless the
2134 * %G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES flag is used.
2136 * If the %G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS flag is not set, also sets up
2137 * match rules for signals. Connect to the #GDBusProxy::g-signal signal
2138 * to handle signals from the remote object.
2140 * If @name is a well-known name and the
2141 * %G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START flag isn't set and no name
2142 * owner currently exists, the message bus will be requested to launch
2143 * a name owner for the name.
2145 * This is a synchronous failable constructor. See g_dbus_proxy_new()
2146 * and g_dbus_proxy_new_finish() for the asynchronous version.
2148 * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
2150 * Returns: A #GDBusProxy or %NULL if error is set. Free with g_object_unref().
2155 g_dbus_proxy_new_sync (GDBusConnection *connection,
2156 GDBusProxyFlags flags,
2157 GDBusInterfaceInfo *info,
2159 const gchar *object_path,
2160 const gchar *interface_name,
2161 GCancellable *cancellable,
2164 GInitable *initable;
2166 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
2167 g_return_val_if_fail ((name == NULL && g_dbus_connection_get_unique_name (connection) == NULL) ||
2168 g_dbus_is_name (name), NULL);
2169 g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
2170 g_return_val_if_fail (g_dbus_is_interface_name (interface_name), NULL);
2172 initable = g_initable_new (G_TYPE_DBUS_PROXY,
2176 "g-interface-info", info,
2178 "g-connection", connection,
2179 "g-object-path", object_path,
2180 "g-interface-name", interface_name,
2182 if (initable != NULL)
2183 return G_DBUS_PROXY (initable);
2188 /* ---------------------------------------------------------------------------------------------------- */
2191 * g_dbus_proxy_new_for_bus:
2192 * @bus_type: A #GBusType.
2193 * @flags: Flags used when constructing the proxy.
2194 * @info: (allow-none): A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
2195 * @name: A bus name (well-known or unique).
2196 * @object_path: An object path.
2197 * @interface_name: A D-Bus interface name.
2198 * @cancellable: (allow-none): A #GCancellable or %NULL.
2199 * @callback: Callback function to invoke when the proxy is ready.
2200 * @user_data: User data to pass to @callback.
2202 * Like g_dbus_proxy_new() but takes a #GBusType instead of a #GDBusConnection.
2204 * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
2209 g_dbus_proxy_new_for_bus (GBusType bus_type,
2210 GDBusProxyFlags flags,
2211 GDBusInterfaceInfo *info,
2213 const gchar *object_path,
2214 const gchar *interface_name,
2215 GCancellable *cancellable,
2216 GAsyncReadyCallback callback,
2219 g_return_if_fail (g_dbus_is_name (name));
2220 g_return_if_fail (g_variant_is_object_path (object_path));
2221 g_return_if_fail (g_dbus_is_interface_name (interface_name));
2223 g_async_initable_new_async (G_TYPE_DBUS_PROXY,
2229 "g-interface-info", info,
2231 "g-bus-type", bus_type,
2232 "g-object-path", object_path,
2233 "g-interface-name", interface_name,
2238 * g_dbus_proxy_new_for_bus_finish:
2239 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback function passed to g_dbus_proxy_new_for_bus().
2240 * @error: Return location for error or %NULL.
2242 * Finishes creating a #GDBusProxy.
2244 * Returns: A #GDBusProxy or %NULL if @error is set. Free with g_object_unref().
2249 g_dbus_proxy_new_for_bus_finish (GAsyncResult *res,
2252 return g_dbus_proxy_new_finish (res, error);
2256 * g_dbus_proxy_new_for_bus_sync:
2257 * @bus_type: A #GBusType.
2258 * @flags: Flags used when constructing the proxy.
2259 * @info: (allow-none): A #GDBusInterfaceInfo specifying the minimal interface
2260 * that @proxy conforms to or %NULL.
2261 * @name: A bus name (well-known or unique).
2262 * @object_path: An object path.
2263 * @interface_name: A D-Bus interface name.
2264 * @cancellable: (allow-none): A #GCancellable or %NULL.
2265 * @error: Return location for error or %NULL.
2267 * Like g_dbus_proxy_new_sync() but takes a #GBusType instead of a #GDBusConnection.
2269 * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
2271 * Returns: A #GDBusProxy or %NULL if error is set. Free with g_object_unref().
2276 g_dbus_proxy_new_for_bus_sync (GBusType bus_type,
2277 GDBusProxyFlags flags,
2278 GDBusInterfaceInfo *info,
2280 const gchar *object_path,
2281 const gchar *interface_name,
2282 GCancellable *cancellable,
2285 GInitable *initable;
2287 g_return_val_if_fail (g_dbus_is_name (name), NULL);
2288 g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
2289 g_return_val_if_fail (g_dbus_is_interface_name (interface_name), NULL);
2291 initable = g_initable_new (G_TYPE_DBUS_PROXY,
2295 "g-interface-info", info,
2297 "g-bus-type", bus_type,
2298 "g-object-path", object_path,
2299 "g-interface-name", interface_name,
2301 if (initable != NULL)
2302 return G_DBUS_PROXY (initable);
2307 /* ---------------------------------------------------------------------------------------------------- */
2310 * g_dbus_proxy_get_connection:
2311 * @proxy: A #GDBusProxy.
2313 * Gets the connection @proxy is for.
2315 * Returns: (transfer none): A #GDBusConnection owned by @proxy. Do not free.
2320 g_dbus_proxy_get_connection (GDBusProxy *proxy)
2322 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2323 return proxy->priv->connection;
2327 * g_dbus_proxy_get_flags:
2328 * @proxy: A #GDBusProxy.
2330 * Gets the flags that @proxy was constructed with.
2332 * Returns: Flags from the #GDBusProxyFlags enumeration.
2337 g_dbus_proxy_get_flags (GDBusProxy *proxy)
2339 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), 0);
2340 return proxy->priv->flags;
2344 * g_dbus_proxy_get_name:
2345 * @proxy: A #GDBusProxy.
2347 * Gets the name that @proxy was constructed for.
2349 * Returns: A string owned by @proxy. Do not free.
2354 g_dbus_proxy_get_name (GDBusProxy *proxy)
2356 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2357 return proxy->priv->name;
2361 * g_dbus_proxy_get_name_owner:
2362 * @proxy: A #GDBusProxy.
2364 * The unique name that owns the name that @proxy is for or %NULL if
2365 * no-one currently owns that name. You may connect to the
2366 * #GObject::notify signal to track changes to the
2367 * #GDBusProxy:g-name-owner property.
2369 * Returns: The name owner or %NULL if no name owner exists. Free with g_free().
2374 g_dbus_proxy_get_name_owner (GDBusProxy *proxy)
2378 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2380 G_LOCK (properties_lock);
2381 ret = g_strdup (proxy->priv->name_owner);
2382 G_UNLOCK (properties_lock);
2387 * g_dbus_proxy_get_object_path:
2388 * @proxy: A #GDBusProxy.
2390 * Gets the object path @proxy is for.
2392 * Returns: A string owned by @proxy. Do not free.
2397 g_dbus_proxy_get_object_path (GDBusProxy *proxy)
2399 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2400 return proxy->priv->object_path;
2404 * g_dbus_proxy_get_interface_name:
2405 * @proxy: A #GDBusProxy.
2407 * Gets the D-Bus interface name @proxy is for.
2409 * Returns: A string owned by @proxy. Do not free.
2414 g_dbus_proxy_get_interface_name (GDBusProxy *proxy)
2416 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2417 return proxy->priv->interface_name;
2421 * g_dbus_proxy_get_default_timeout:
2422 * @proxy: A #GDBusProxy.
2424 * Gets the timeout to use if -1 (specifying default timeout) is
2425 * passed as @timeout_msec in the g_dbus_proxy_call() and
2426 * g_dbus_proxy_call_sync() functions.
2428 * See the #GDBusProxy:g-default-timeout property for more details.
2430 * Returns: Timeout to use for @proxy.
2435 g_dbus_proxy_get_default_timeout (GDBusProxy *proxy)
2439 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), -1);
2441 G_LOCK (properties_lock);
2442 ret = proxy->priv->timeout_msec;
2443 G_UNLOCK (properties_lock);
2448 * g_dbus_proxy_set_default_timeout:
2449 * @proxy: A #GDBusProxy.
2450 * @timeout_msec: Timeout in milliseconds.
2452 * Sets the timeout to use if -1 (specifying default timeout) is
2453 * passed as @timeout_msec in the g_dbus_proxy_call() and
2454 * g_dbus_proxy_call_sync() functions.
2456 * See the #GDBusProxy:g-default-timeout property for more details.
2461 g_dbus_proxy_set_default_timeout (GDBusProxy *proxy,
2464 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
2465 g_return_if_fail (timeout_msec == -1 || timeout_msec >= 0);
2467 G_LOCK (properties_lock);
2469 if (proxy->priv->timeout_msec != timeout_msec)
2471 proxy->priv->timeout_msec = timeout_msec;
2472 G_UNLOCK (properties_lock);
2474 g_object_notify (G_OBJECT (proxy), "g-default-timeout");
2478 G_UNLOCK (properties_lock);
2483 * g_dbus_proxy_get_interface_info:
2484 * @proxy: A #GDBusProxy
2486 * Returns the #GDBusInterfaceInfo, if any, specifying the interface
2487 * that @proxy conforms to. See the #GDBusProxy:g-interface-info
2488 * property for more details.
2490 * Returns: A #GDBusInterfaceInfo or %NULL. Do not unref the returned
2491 * object, it is owned by @proxy.
2495 GDBusInterfaceInfo *
2496 g_dbus_proxy_get_interface_info (GDBusProxy *proxy)
2498 GDBusInterfaceInfo *ret;
2500 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2502 G_LOCK (properties_lock);
2503 ret = proxy->priv->expected_interface;
2504 G_UNLOCK (properties_lock);
2505 /* FIXME: returning a borrowed ref with no guarantee that nobody will
2506 * call g_dbus_proxy_set_interface_info() and make it invalid...
2512 * g_dbus_proxy_set_interface_info:
2513 * @proxy: A #GDBusProxy
2514 * @info: (allow-none): Minimum interface this proxy conforms to or %NULL to unset.
2516 * Ensure that interactions with @proxy conform to the given
2517 * interface. See the #GDBusProxy:g-interface-info property for more
2523 g_dbus_proxy_set_interface_info (GDBusProxy *proxy,
2524 GDBusInterfaceInfo *info)
2526 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
2527 G_LOCK (properties_lock);
2529 if (proxy->priv->expected_interface != NULL)
2531 g_dbus_interface_info_cache_release (proxy->priv->expected_interface);
2532 g_dbus_interface_info_unref (proxy->priv->expected_interface);
2534 proxy->priv->expected_interface = info != NULL ? g_dbus_interface_info_ref (info) : NULL;
2535 if (proxy->priv->expected_interface != NULL)
2536 g_dbus_interface_info_cache_build (proxy->priv->expected_interface);
2538 G_UNLOCK (properties_lock);
2541 /* ---------------------------------------------------------------------------------------------------- */
2544 maybe_split_method_name (const gchar *method_name,
2545 gchar **out_interface_name,
2546 const gchar **out_method_name)
2551 g_assert (out_interface_name != NULL);
2552 g_assert (out_method_name != NULL);
2553 *out_interface_name = NULL;
2554 *out_method_name = NULL;
2556 if (strchr (method_name, '.') != NULL)
2561 p = g_strdup (method_name);
2562 last_dot = strrchr (p, '.');
2565 *out_interface_name = p;
2566 *out_method_name = last_dot + 1;
2578 GUnixFDList *fd_list;
2583 reply_data_free (ReplyData *data)
2585 g_variant_unref (data->value);
2587 if (data->fd_list != NULL)
2588 g_object_unref (data->fd_list);
2590 g_slice_free (ReplyData, data);
2594 reply_cb (GDBusConnection *connection,
2598 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
2602 GUnixFDList *fd_list;
2607 value = g_dbus_connection_call_with_unix_fd_list_finish (connection,
2612 value = g_dbus_connection_call_finish (connection,
2618 g_simple_async_result_take_error (simple, error);
2623 data = g_slice_new0 (ReplyData);
2624 data->value = value;
2626 data->fd_list = fd_list;
2628 g_simple_async_result_set_op_res_gpointer (simple, data, (GDestroyNotify) reply_data_free);
2631 /* no need to complete in idle since the method GDBusConnection already does */
2632 g_simple_async_result_complete (simple);
2633 g_object_unref (simple);
2636 /* properties_lock must be held for as long as you will keep the
2639 static const GDBusMethodInfo *
2640 lookup_method_info (GDBusProxy *proxy,
2641 const gchar *method_name)
2643 const GDBusMethodInfo *info = NULL;
2645 if (proxy->priv->expected_interface == NULL)
2648 info = g_dbus_interface_info_lookup_method (proxy->priv->expected_interface, method_name);
2654 /* properties_lock must be held for as long as you will keep the
2657 static const gchar *
2658 get_destination_for_call (GDBusProxy *proxy)
2664 /* If proxy->priv->name is a unique name, then proxy->priv->name_owner
2665 * is never NULL and always the same as proxy->priv->name. We use this
2666 * knowledge to avoid checking if proxy->priv->name is a unique or
2669 ret = proxy->priv->name_owner;
2673 if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START)
2676 ret = proxy->priv->name;
2682 /* ---------------------------------------------------------------------------------------------------- */
2685 g_dbus_proxy_call_internal (GDBusProxy *proxy,
2686 const gchar *method_name,
2687 GVariant *parameters,
2688 GDBusCallFlags flags,
2690 GUnixFDList *fd_list,
2691 GCancellable *cancellable,
2692 GAsyncReadyCallback callback,
2695 GSimpleAsyncResult *simple;
2697 gchar *split_interface_name;
2698 const gchar *split_method_name;
2699 const gchar *target_method_name;
2700 const gchar *target_interface_name;
2702 GVariantType *reply_type;
2703 GAsyncReadyCallback my_callback;
2705 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
2706 g_return_if_fail (g_dbus_is_member_name (method_name) || g_dbus_is_interface_name (method_name));
2707 g_return_if_fail (parameters == NULL || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE));
2708 g_return_if_fail (timeout_msec == -1 || timeout_msec >= 0);
2710 g_return_if_fail (fd_list == NULL || G_IS_UNIX_FD_LIST (fd_list));
2712 g_return_if_fail (fd_list == NULL);
2716 split_interface_name = NULL;
2718 /* g_dbus_connection_call() is optimised for the case of a NULL
2719 * callback. If we get a NULL callback from our user then make sure
2720 * we pass along a NULL callback for ourselves as well.
2722 if (callback != NULL)
2724 my_callback = (GAsyncReadyCallback) reply_cb;
2725 simple = g_simple_async_result_new (G_OBJECT (proxy),
2728 g_dbus_proxy_call_internal);
2729 g_simple_async_result_set_check_cancellable (simple, cancellable);
2737 G_LOCK (properties_lock);
2739 was_split = maybe_split_method_name (method_name, &split_interface_name, &split_method_name);
2740 target_method_name = was_split ? split_method_name : method_name;
2741 target_interface_name = was_split ? split_interface_name : proxy->priv->interface_name;
2743 /* Warn if method is unexpected (cf. :g-interface-info) */
2746 const GDBusMethodInfo *expected_method_info;
2747 expected_method_info = lookup_method_info (proxy, target_method_name);
2748 if (expected_method_info != NULL)
2749 reply_type = _g_dbus_compute_complete_signature (expected_method_info->out_args);
2753 if (proxy->priv->name != NULL)
2755 destination = g_strdup (get_destination_for_call (proxy));
2756 if (destination == NULL)
2760 g_simple_async_result_set_error (simple,
2763 _("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"));
2764 g_simple_async_result_complete_in_idle (simple);
2765 g_object_unref (simple);
2767 G_UNLOCK (properties_lock);
2772 G_UNLOCK (properties_lock);
2775 g_dbus_connection_call_with_unix_fd_list (proxy->priv->connection,
2777 proxy->priv->object_path,
2778 target_interface_name,
2783 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2789 g_dbus_connection_call (proxy->priv->connection,
2791 proxy->priv->object_path,
2792 target_interface_name,
2797 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);
2812 g_dbus_proxy_call_finish_internal (GDBusProxy *proxy,
2813 GUnixFDList **out_fd_list,
2817 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2821 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2822 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2823 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2825 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_dbus_proxy_call_internal);
2829 if (g_simple_async_result_propagate_error (simple, error))
2832 data = g_simple_async_result_get_op_res_gpointer (simple);
2833 value = g_variant_ref (data->value);
2835 if (out_fd_list != NULL)
2836 *out_fd_list = data->fd_list != NULL ? g_object_ref (data->fd_list) : NULL;
2844 g_dbus_proxy_call_sync_internal (GDBusProxy *proxy,
2845 const gchar *method_name,
2846 GVariant *parameters,
2847 GDBusCallFlags flags,
2849 GUnixFDList *fd_list,
2850 GUnixFDList **out_fd_list,
2851 GCancellable *cancellable,
2856 gchar *split_interface_name;
2857 const gchar *split_method_name;
2858 const gchar *target_method_name;
2859 const gchar *target_interface_name;
2861 GVariantType *reply_type;
2863 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2864 g_return_val_if_fail (g_dbus_is_member_name (method_name) || g_dbus_is_interface_name (method_name), NULL);
2865 g_return_val_if_fail (parameters == NULL || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE), NULL);
2866 g_return_val_if_fail (timeout_msec == -1 || timeout_msec >= 0, NULL);
2868 g_return_val_if_fail (fd_list == NULL || G_IS_UNIX_FD_LIST (fd_list), NULL);
2870 g_return_val_if_fail (fd_list == NULL, NULL);
2872 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2876 G_LOCK (properties_lock);
2878 was_split = maybe_split_method_name (method_name, &split_interface_name, &split_method_name);
2879 target_method_name = was_split ? split_method_name : method_name;
2880 target_interface_name = was_split ? split_interface_name : proxy->priv->interface_name;
2882 /* Warn if method is unexpected (cf. :g-interface-info) */
2885 const GDBusMethodInfo *expected_method_info;
2886 expected_method_info = lookup_method_info (proxy, target_method_name);
2887 if (expected_method_info != NULL)
2888 reply_type = _g_dbus_compute_complete_signature (expected_method_info->out_args);
2892 if (proxy->priv->name != NULL)
2894 destination = g_strdup (get_destination_for_call (proxy));
2895 if (destination == NULL)
2897 g_set_error_literal (error,
2900 _("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"));
2902 G_UNLOCK (properties_lock);
2907 G_UNLOCK (properties_lock);
2910 ret = g_dbus_connection_call_with_unix_fd_list_sync (proxy->priv->connection,
2912 proxy->priv->object_path,
2913 target_interface_name,
2918 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2924 ret = g_dbus_connection_call_sync (proxy->priv->connection,
2926 proxy->priv->object_path,
2927 target_interface_name,
2932 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2938 if (reply_type != NULL)
2939 g_variant_type_free (reply_type);
2941 g_free (destination);
2942 g_free (split_interface_name);
2947 /* ---------------------------------------------------------------------------------------------------- */
2950 * g_dbus_proxy_call:
2951 * @proxy: A #GDBusProxy.
2952 * @method_name: Name of method to invoke.
2953 * @parameters: (allow-none): A #GVariant tuple with parameters for the signal or %NULL if not passing parameters.
2954 * @flags: Flags from the #GDBusCallFlags enumeration.
2955 * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
2956 * "infinite") or -1 to use the proxy default timeout.
2957 * @cancellable: (allow-none): A #GCancellable or %NULL.
2958 * @callback: (allow-none): A #GAsyncReadyCallback to call when the request is satisfied or %NULL if you don't
2959 * care about the result of the method invocation.
2960 * @user_data: The data to pass to @callback.
2962 * Asynchronously invokes the @method_name method on @proxy.
2964 * If @method_name contains any dots, then @name is split into interface and
2965 * method name parts. This allows using @proxy for invoking methods on
2968 * If the #GDBusConnection associated with @proxy is closed then
2969 * the operation will fail with %G_IO_ERROR_CLOSED. If
2970 * @cancellable is canceled, the operation will fail with
2971 * %G_IO_ERROR_CANCELLED. If @parameters contains a value not
2972 * compatible with the D-Bus protocol, the operation fails with
2973 * %G_IO_ERROR_INVALID_ARGUMENT.
2975 * If the @parameters #GVariant is floating, it is consumed. This allows
2976 * convenient 'inline' use of g_variant_new(), e.g.:
2978 * g_dbus_proxy_call (proxy,
2980 * g_variant_new ("(ss)",
2983 * G_DBUS_CALL_FLAGS_NONE,
2986 * (GAsyncReadyCallback) two_strings_done,
2990 * If @proxy has an expected interface (see
2991 * #GDBusProxy:g-interface-info) and @method_name is referenced by it,
2992 * then the return value is checked against the return type.
2994 * This is an asynchronous method. When the operation is finished,
2995 * @callback will be invoked in the
2996 * <link linkend="g-main-context-push-thread-default">thread-default main loop</link>
2997 * of the thread you are calling this method from.
2998 * You can then call g_dbus_proxy_call_finish() to get the result of
2999 * the operation. See g_dbus_proxy_call_sync() for the synchronous
3000 * version of this method.
3002 * If @callback is %NULL then the D-Bus method call message will be sent with
3003 * the %G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED flag set.
3008 g_dbus_proxy_call (GDBusProxy *proxy,
3009 const gchar *method_name,
3010 GVariant *parameters,
3011 GDBusCallFlags flags,
3013 GCancellable *cancellable,
3014 GAsyncReadyCallback callback,
3017 g_dbus_proxy_call_internal (proxy, method_name, parameters, flags, timeout_msec, NULL, cancellable, callback, user_data);
3021 * g_dbus_proxy_call_finish:
3022 * @proxy: A #GDBusProxy.
3023 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_proxy_call().
3024 * @error: Return location for error or %NULL.
3026 * Finishes an operation started with g_dbus_proxy_call().
3028 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
3029 * return values. Free with g_variant_unref().
3034 g_dbus_proxy_call_finish (GDBusProxy *proxy,
3038 return g_dbus_proxy_call_finish_internal (proxy, NULL, res, error);
3042 * g_dbus_proxy_call_sync:
3043 * @proxy: A #GDBusProxy.
3044 * @method_name: Name of method to invoke.
3045 * @parameters: (allow-none): A #GVariant tuple with parameters for the signal
3046 * or %NULL if not passing parameters.
3047 * @flags: Flags from the #GDBusCallFlags enumeration.
3048 * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
3049 * "infinite") or -1 to use the proxy default timeout.
3050 * @cancellable: (allow-none): A #GCancellable or %NULL.
3051 * @error: Return location for error or %NULL.
3053 * Synchronously invokes the @method_name method on @proxy.
3055 * If @method_name contains any dots, then @name is split into interface and
3056 * method name parts. This allows using @proxy for invoking methods on
3059 * If the #GDBusConnection associated with @proxy is disconnected then
3060 * the operation will fail with %G_IO_ERROR_CLOSED. If
3061 * @cancellable is canceled, the operation will fail with
3062 * %G_IO_ERROR_CANCELLED. If @parameters contains a value not
3063 * compatible with the D-Bus protocol, the operation fails with
3064 * %G_IO_ERROR_INVALID_ARGUMENT.
3066 * If the @parameters #GVariant is floating, it is consumed. This allows
3067 * convenient 'inline' use of g_variant_new(), e.g.:
3069 * g_dbus_proxy_call_sync (proxy,
3071 * g_variant_new ("(ss)",
3074 * G_DBUS_CALL_FLAGS_NONE,
3080 * The calling thread is blocked until a reply is received. See
3081 * g_dbus_proxy_call() for the asynchronous version of this
3084 * If @proxy has an expected interface (see
3085 * #GDBusProxy:g-interface-info) and @method_name is referenced by it,
3086 * then the return value is checked against the return type.
3088 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
3089 * return values. Free with g_variant_unref().
3094 g_dbus_proxy_call_sync (GDBusProxy *proxy,
3095 const gchar *method_name,
3096 GVariant *parameters,
3097 GDBusCallFlags flags,
3099 GCancellable *cancellable,
3102 return g_dbus_proxy_call_sync_internal (proxy, method_name, parameters, flags, timeout_msec, NULL, NULL, cancellable, error);
3105 /* ---------------------------------------------------------------------------------------------------- */
3110 * g_dbus_proxy_call_with_unix_fd_list:
3111 * @proxy: A #GDBusProxy.
3112 * @method_name: Name of method to invoke.
3113 * @parameters: (allow-none): A #GVariant tuple with parameters for the signal or %NULL if not passing parameters.
3114 * @flags: Flags from the #GDBusCallFlags enumeration.
3115 * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
3116 * "infinite") or -1 to use the proxy default timeout.
3117 * @fd_list: (allow-none): A #GUnixFDList or %NULL.
3118 * @cancellable: (allow-none): A #GCancellable or %NULL.
3119 * @callback: (allow-none): A #GAsyncReadyCallback to call when the request is satisfied or %NULL if you don't
3120 * care about the result of the method invocation.
3121 * @user_data: The data to pass to @callback.
3123 * Like g_dbus_proxy_call() but also takes a #GUnixFDList object.
3125 * This method is only available on UNIX.
3130 g_dbus_proxy_call_with_unix_fd_list (GDBusProxy *proxy,
3131 const gchar *method_name,
3132 GVariant *parameters,
3133 GDBusCallFlags flags,
3135 GUnixFDList *fd_list,
3136 GCancellable *cancellable,
3137 GAsyncReadyCallback callback,
3140 g_dbus_proxy_call_internal (proxy, method_name, parameters, flags, timeout_msec, fd_list, cancellable, callback, user_data);
3144 * g_dbus_proxy_call_with_unix_fd_list_finish:
3145 * @proxy: A #GDBusProxy.
3146 * @out_fd_list: (out) (allow-none): Return location for a #GUnixFDList or %NULL.
3147 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_proxy_call_with_unix_fd_list().
3148 * @error: Return location for error or %NULL.
3150 * Finishes an operation started with g_dbus_proxy_call_with_unix_fd_list().
3152 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
3153 * return values. Free with g_variant_unref().
3158 g_dbus_proxy_call_with_unix_fd_list_finish (GDBusProxy *proxy,
3159 GUnixFDList **out_fd_list,
3163 return g_dbus_proxy_call_finish_internal (proxy, out_fd_list, res, error);
3167 * g_dbus_proxy_call_with_unix_fd_list_sync:
3168 * @proxy: A #GDBusProxy.
3169 * @method_name: Name of method to invoke.
3170 * @parameters: (allow-none): A #GVariant tuple with parameters for the signal
3171 * or %NULL if not passing parameters.
3172 * @flags: Flags from the #GDBusCallFlags enumeration.
3173 * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
3174 * "infinite") or -1 to use the proxy default timeout.
3175 * @fd_list: (allow-none): A #GUnixFDList or %NULL.
3176 * @out_fd_list: (out) (allow-none): Return location for a #GUnixFDList or %NULL.
3177 * @cancellable: (allow-none): A #GCancellable or %NULL.
3178 * @error: Return location for error or %NULL.
3180 * Like g_dbus_proxy_call_sync() but also takes and returns #GUnixFDList objects.
3182 * This method is only available on UNIX.
3184 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
3185 * return values. Free with g_variant_unref().
3190 g_dbus_proxy_call_with_unix_fd_list_sync (GDBusProxy *proxy,
3191 const gchar *method_name,
3192 GVariant *parameters,
3193 GDBusCallFlags flags,
3195 GUnixFDList *fd_list,
3196 GUnixFDList **out_fd_list,
3197 GCancellable *cancellable,
3200 return g_dbus_proxy_call_sync_internal (proxy, method_name, parameters, flags, timeout_msec, fd_list, out_fd_list, cancellable, error);
3203 #endif /* G_OS_UNIX */
3205 /* ---------------------------------------------------------------------------------------------------- */
3207 static GDBusInterfaceInfo *
3208 _g_dbus_proxy_get_info (GDBusInterface *interface)
3210 GDBusProxy *proxy = G_DBUS_PROXY (interface);
3211 return g_dbus_proxy_get_interface_info (proxy);
3214 static GDBusObject *
3215 _g_dbus_proxy_get_object (GDBusInterface *interface)
3217 GDBusProxy *proxy = G_DBUS_PROXY (interface);
3218 return proxy->priv->object;
3221 static GDBusObject *
3222 _g_dbus_proxy_dup_object (GDBusInterface *interface)
3224 GDBusProxy *proxy = G_DBUS_PROXY (interface);
3225 GDBusObject *ret = NULL;
3227 G_LOCK (properties_lock);
3228 if (proxy->priv->object != NULL)
3229 ret = g_object_ref (proxy->priv->object);
3230 G_UNLOCK (properties_lock);
3235 _g_dbus_proxy_set_object (GDBusInterface *interface,
3236 GDBusObject *object)
3238 GDBusProxy *proxy = G_DBUS_PROXY (interface);
3239 G_LOCK (properties_lock);
3240 if (proxy->priv->object != NULL)
3241 g_object_remove_weak_pointer (G_OBJECT (proxy->priv->object), (gpointer *) &proxy->priv->object);
3242 proxy->priv->object = object;
3243 if (proxy->priv->object != NULL)
3244 g_object_add_weak_pointer (G_OBJECT (proxy->priv->object), (gpointer *) &proxy->priv->object);
3245 G_UNLOCK (properties_lock);
3249 dbus_interface_iface_init (GDBusInterfaceIface *dbus_interface_iface)
3251 dbus_interface_iface->get_info = _g_dbus_proxy_get_info;
3252 dbus_interface_iface->get_object = _g_dbus_proxy_get_object;
3253 dbus_interface_iface->dup_object = _g_dbus_proxy_dup_object;
3254 dbus_interface_iface->set_object = _g_dbus_proxy_set_object;
3257 /* ---------------------------------------------------------------------------------------------------- */