1 /* GDBus - GLib D-Bus Library
3 * Copyright (C) 2008-2010 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 * Author: David Zeuthen <davidz@redhat.com>
26 #include "gdbusutils.h"
27 #include "gdbusproxy.h"
28 #include "gioenumtypes.h"
29 #include "gdbusconnection.h"
30 #include "gdbuserror.h"
31 #include "gdbusprivate.h"
32 #include "ginitable.h"
33 #include "gasyncinitable.h"
35 #include "gasyncresult.h"
36 #include "gsimpleasyncresult.h"
37 #include "gcancellable.h"
38 #include "gdbusinterface.h"
41 #include "gunixfdlist.h"
48 * @short_description: Client-side D-Bus interface proxy
51 * #GDBusProxy is a base class used for proxies to access a D-Bus
52 * interface on a remote object. A #GDBusProxy can be constructed for
53 * both well-known and unique names.
55 * By default, #GDBusProxy will cache all properties (and listen to
56 * changes) of the remote object, and proxy all signals that gets
57 * emitted. This behaviour can be changed by passing suitable
58 * #GDBusProxyFlags when the proxy is created. If the proxy is for a
59 * well-known name, the property cache is flushed when the name owner
60 * vanishes and reloaded when a name owner appears.
62 * If a #GDBusProxy is used for a well-known name, the owner of the
63 * name is tracked and can be read from
64 * #GDBusProxy:g-name-owner. Connect to the #GObject::notify signal to
65 * get notified of changes. Additionally, only signals and property
66 * changes emitted from the current name owner are considered and
67 * calls are always sent to the current name owner. This avoids a
68 * number of race conditions when the name is lost by one owner and
69 * claimed by another. However, if no name owner currently exists,
70 * then calls will be sent to the well-known name which may result in
71 * the message bus launching an owner (unless
72 * %G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START is set).
74 * The generic #GDBusProxy::g-properties-changed and
75 * #GDBusProxy::g-signal signals are not very convenient to work
76 * with. Therefore, the recommended way of working with proxies is to
77 * subclass #GDBusProxy, and have more natural properties and signals
78 * in your derived class. See <xref linkend="gdbus-example-gdbus-codegen"/>
79 * for how this can easily be done using the
80 * <command><link linkend="gdbus-codegen">gdbus-codegen</link></command>
83 * A #GDBusProxy instance can be used from multiple threads but note
84 * that all signals (e.g. #GDBusProxy::g-signal, #GDBusProxy::g-properties-changed
85 * and #GObject::notify) are emitted in the
86 * <link linkend="g-main-context-push-thread-default">thread-default main loop</link>
87 * of the thread where the instance was constructed.
89 * <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>
92 /* lock protecting the mutable properties: name_owner, timeout_msec,
93 * expected_interface, and the properties hash table
95 G_LOCK_DEFINE_STATIC (properties_lock);
97 /* ---------------------------------------------------------------------------------------------------- */
99 G_LOCK_DEFINE_STATIC (signal_subscription_lock);
103 volatile gint ref_count;
105 } SignalSubscriptionData;
107 static SignalSubscriptionData *
108 signal_subscription_ref (SignalSubscriptionData *data)
110 g_atomic_int_inc (&data->ref_count);
115 signal_subscription_unref (SignalSubscriptionData *data)
117 if (g_atomic_int_dec_and_test (&data->ref_count))
119 g_slice_free (SignalSubscriptionData, data);
123 /* ---------------------------------------------------------------------------------------------------- */
125 struct _GDBusProxyPrivate
128 GDBusProxyFlags flags;
129 GDBusConnection *connection;
132 /* mutable, protected by properties_lock */
135 gchar *interface_name;
136 /* mutable, protected by properties_lock */
139 guint name_owner_changed_subscription_id;
141 GCancellable *get_all_cancellable;
143 /* gchar* -> GVariant*, protected by properties_lock */
144 GHashTable *properties;
146 /* mutable, protected by properties_lock */
147 GDBusInterfaceInfo *expected_interface;
149 guint properties_changed_subscription_id;
150 guint signals_subscription_id;
152 gboolean initialized;
154 /* mutable, protected by properties_lock */
157 SignalSubscriptionData *signal_subscription_data;
169 PROP_G_INTERFACE_NAME,
170 PROP_G_DEFAULT_TIMEOUT,
171 PROP_G_INTERFACE_INFO
176 PROPERTIES_CHANGED_SIGNAL,
181 static guint signals[LAST_SIGNAL] = {0};
183 static void dbus_interface_iface_init (GDBusInterfaceIface *dbus_interface_iface);
184 static void initable_iface_init (GInitableIface *initable_iface);
185 static void async_initable_iface_init (GAsyncInitableIface *async_initable_iface);
187 G_DEFINE_TYPE_WITH_CODE (GDBusProxy, g_dbus_proxy, G_TYPE_OBJECT,
188 G_ADD_PRIVATE (GDBusProxy)
189 G_IMPLEMENT_INTERFACE (G_TYPE_DBUS_INTERFACE, dbus_interface_iface_init)
190 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, initable_iface_init)
191 G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE, async_initable_iface_init))
194 g_dbus_proxy_dispose (GObject *object)
196 GDBusProxy *proxy = G_DBUS_PROXY (object);
197 G_LOCK (signal_subscription_lock);
198 if (proxy->priv->signal_subscription_data != NULL)
200 proxy->priv->signal_subscription_data->proxy = NULL;
201 signal_subscription_unref (proxy->priv->signal_subscription_data);
202 proxy->priv->signal_subscription_data = NULL;
204 G_UNLOCK (signal_subscription_lock);
206 G_OBJECT_CLASS (g_dbus_proxy_parent_class)->dispose (object);
210 g_dbus_proxy_finalize (GObject *object)
212 GDBusProxy *proxy = G_DBUS_PROXY (object);
214 g_warn_if_fail (proxy->priv->get_all_cancellable == NULL);
216 if (proxy->priv->name_owner_changed_subscription_id > 0)
217 g_dbus_connection_signal_unsubscribe (proxy->priv->connection,
218 proxy->priv->name_owner_changed_subscription_id);
220 if (proxy->priv->properties_changed_subscription_id > 0)
221 g_dbus_connection_signal_unsubscribe (proxy->priv->connection,
222 proxy->priv->properties_changed_subscription_id);
224 if (proxy->priv->signals_subscription_id > 0)
225 g_dbus_connection_signal_unsubscribe (proxy->priv->connection,
226 proxy->priv->signals_subscription_id);
228 if (proxy->priv->connection != NULL)
229 g_object_unref (proxy->priv->connection);
230 g_free (proxy->priv->name);
231 g_free (proxy->priv->name_owner);
232 g_free (proxy->priv->object_path);
233 g_free (proxy->priv->interface_name);
234 if (proxy->priv->properties != NULL)
235 g_hash_table_unref (proxy->priv->properties);
237 if (proxy->priv->expected_interface != NULL)
239 g_dbus_interface_info_cache_release (proxy->priv->expected_interface);
240 g_dbus_interface_info_unref (proxy->priv->expected_interface);
243 if (proxy->priv->object != NULL)
244 g_object_remove_weak_pointer (G_OBJECT (proxy->priv->object), (gpointer *) &proxy->priv->object);
246 G_OBJECT_CLASS (g_dbus_proxy_parent_class)->finalize (object);
250 g_dbus_proxy_get_property (GObject *object,
255 GDBusProxy *proxy = G_DBUS_PROXY (object);
259 case PROP_G_CONNECTION:
260 g_value_set_object (value, proxy->priv->connection);
264 g_value_set_flags (value, proxy->priv->flags);
268 g_value_set_string (value, proxy->priv->name);
271 case PROP_G_NAME_OWNER:
272 g_value_take_string (value, g_dbus_proxy_get_name_owner (proxy));
275 case PROP_G_OBJECT_PATH:
276 g_value_set_string (value, proxy->priv->object_path);
279 case PROP_G_INTERFACE_NAME:
280 g_value_set_string (value, proxy->priv->interface_name);
283 case PROP_G_DEFAULT_TIMEOUT:
284 g_value_set_int (value, g_dbus_proxy_get_default_timeout (proxy));
287 case PROP_G_INTERFACE_INFO:
288 g_value_set_boxed (value, g_dbus_proxy_get_interface_info (proxy));
292 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
298 g_dbus_proxy_set_property (GObject *object,
303 GDBusProxy *proxy = G_DBUS_PROXY (object);
307 case PROP_G_CONNECTION:
308 proxy->priv->connection = g_value_dup_object (value);
312 proxy->priv->flags = g_value_get_flags (value);
316 proxy->priv->name = g_value_dup_string (value);
319 case PROP_G_OBJECT_PATH:
320 proxy->priv->object_path = g_value_dup_string (value);
323 case PROP_G_INTERFACE_NAME:
324 proxy->priv->interface_name = g_value_dup_string (value);
327 case PROP_G_DEFAULT_TIMEOUT:
328 g_dbus_proxy_set_default_timeout (proxy, g_value_get_int (value));
331 case PROP_G_INTERFACE_INFO:
332 g_dbus_proxy_set_interface_info (proxy, g_value_get_boxed (value));
335 case PROP_G_BUS_TYPE:
336 proxy->priv->bus_type = g_value_get_enum (value);
340 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
346 g_dbus_proxy_class_init (GDBusProxyClass *klass)
348 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
350 gobject_class->dispose = g_dbus_proxy_dispose;
351 gobject_class->finalize = g_dbus_proxy_finalize;
352 gobject_class->set_property = g_dbus_proxy_set_property;
353 gobject_class->get_property = g_dbus_proxy_get_property;
355 /* Note that all property names are prefixed to avoid collisions with D-Bus property names
356 * in derived classes */
359 * GDBusProxy:g-interface-info:
361 * Ensure that interactions with this proxy conform to the given
362 * interface. This is mainly to ensure that malformed data received
363 * from the other peer is ignored. The given #GDBusInterfaceInfo is
364 * said to be the <emphasis>expected interface</emphasis>.
366 * The checks performed are:
369 * When completing a method call, if the type signature of
370 * the reply message isn't what's expected, the reply is
371 * discarded and the #GError is set to %G_IO_ERROR_INVALID_ARGUMENT.
374 * Received signals that have a type signature mismatch are dropped and
375 * a warning is logged via g_warning().
378 * Properties received via the initial <literal>GetAll()</literal> call
379 * or via the <literal>::PropertiesChanged</literal> signal (on the
380 * <ulink url="http://dbus.freedesktop.org/doc/dbus-specification.html#standard-interfaces-properties">org.freedesktop.DBus.Properties</ulink> interface) or
381 * set using g_dbus_proxy_set_cached_property() with a type signature
382 * mismatch are ignored and a warning is logged via g_warning().
385 * Note that these checks are never done on methods, signals and
386 * properties that are not referenced in the given
387 * #GDBusInterfaceInfo, since extending a D-Bus interface on the
388 * service-side is not considered an ABI break.
392 g_object_class_install_property (gobject_class,
393 PROP_G_INTERFACE_INFO,
394 g_param_spec_boxed ("g-interface-info",
395 P_("Interface Information"),
396 P_("Interface Information"),
397 G_TYPE_DBUS_INTERFACE_INFO,
400 G_PARAM_STATIC_NAME |
401 G_PARAM_STATIC_BLURB |
402 G_PARAM_STATIC_NICK));
405 * GDBusProxy:g-connection:
407 * The #GDBusConnection the proxy is for.
411 g_object_class_install_property (gobject_class,
413 g_param_spec_object ("g-connection",
415 P_("The connection the proxy is for"),
416 G_TYPE_DBUS_CONNECTION,
419 G_PARAM_CONSTRUCT_ONLY |
420 G_PARAM_STATIC_NAME |
421 G_PARAM_STATIC_BLURB |
422 G_PARAM_STATIC_NICK));
425 * GDBusProxy:g-bus-type:
427 * If this property is not %G_BUS_TYPE_NONE, then
428 * #GDBusProxy:g-connection must be %NULL and will be set to the
429 * #GDBusConnection obtained by calling g_bus_get() with the value
434 g_object_class_install_property (gobject_class,
436 g_param_spec_enum ("g-bus-type",
438 P_("The bus to connect to, if any"),
442 G_PARAM_CONSTRUCT_ONLY |
443 G_PARAM_STATIC_NAME |
444 G_PARAM_STATIC_BLURB |
445 G_PARAM_STATIC_NICK));
448 * GDBusProxy:g-flags:
450 * Flags from the #GDBusProxyFlags enumeration.
454 g_object_class_install_property (gobject_class,
456 g_param_spec_flags ("g-flags",
458 P_("Flags for the proxy"),
459 G_TYPE_DBUS_PROXY_FLAGS,
460 G_DBUS_PROXY_FLAGS_NONE,
463 G_PARAM_CONSTRUCT_ONLY |
464 G_PARAM_STATIC_NAME |
465 G_PARAM_STATIC_BLURB |
466 G_PARAM_STATIC_NICK));
471 * The well-known or unique name that the proxy is for.
475 g_object_class_install_property (gobject_class,
477 g_param_spec_string ("g-name",
479 P_("The well-known or unique name that the proxy is for"),
483 G_PARAM_CONSTRUCT_ONLY |
484 G_PARAM_STATIC_NAME |
485 G_PARAM_STATIC_BLURB |
486 G_PARAM_STATIC_NICK));
489 * GDBusProxy:g-name-owner:
491 * The unique name that owns #GDBusProxy:g-name or %NULL if no-one
492 * currently owns that name. You may connect to #GObject::notify signal to
493 * track changes to this property.
497 g_object_class_install_property (gobject_class,
499 g_param_spec_string ("g-name-owner",
501 P_("The unique name for the owner"),
504 G_PARAM_STATIC_NAME |
505 G_PARAM_STATIC_BLURB |
506 G_PARAM_STATIC_NICK));
509 * GDBusProxy:g-object-path:
511 * The object path the proxy is for.
515 g_object_class_install_property (gobject_class,
517 g_param_spec_string ("g-object-path",
519 P_("The object path the proxy is for"),
523 G_PARAM_CONSTRUCT_ONLY |
524 G_PARAM_STATIC_NAME |
525 G_PARAM_STATIC_BLURB |
526 G_PARAM_STATIC_NICK));
529 * GDBusProxy:g-interface-name:
531 * The D-Bus interface name the proxy is for.
535 g_object_class_install_property (gobject_class,
536 PROP_G_INTERFACE_NAME,
537 g_param_spec_string ("g-interface-name",
538 P_("g-interface-name"),
539 P_("The D-Bus interface name the proxy is for"),
543 G_PARAM_CONSTRUCT_ONLY |
544 G_PARAM_STATIC_NAME |
545 G_PARAM_STATIC_BLURB |
546 G_PARAM_STATIC_NICK));
549 * GDBusProxy:g-default-timeout:
551 * The timeout to use if -1 (specifying default timeout) is passed
552 * as @timeout_msec in the g_dbus_proxy_call() and
553 * g_dbus_proxy_call_sync() functions.
555 * This allows applications to set a proxy-wide timeout for all
556 * remote method invocations on the proxy. If this property is -1,
557 * the default timeout (typically 25 seconds) is used. If set to
558 * %G_MAXINT, then no timeout is used.
562 g_object_class_install_property (gobject_class,
563 PROP_G_DEFAULT_TIMEOUT,
564 g_param_spec_int ("g-default-timeout",
565 P_("Default Timeout"),
566 P_("Timeout for remote method invocation"),
573 G_PARAM_STATIC_NAME |
574 G_PARAM_STATIC_BLURB |
575 G_PARAM_STATIC_NICK));
578 * GDBusProxy::g-properties-changed:
579 * @proxy: The #GDBusProxy emitting the signal.
580 * @changed_properties: A #GVariant containing the properties that changed
581 * @invalidated_properties: A %NULL terminated array of properties that was invalidated
583 * Emitted when one or more D-Bus properties on @proxy changes. The
584 * local cache has already been updated when this signal fires. Note
585 * that both @changed_properties and @invalidated_properties are
586 * guaranteed to never be %NULL (either may be empty though).
588 * If the proxy has the flag
589 * %G_DBUS_PROXY_FLAGS_GET_INVALIDATED_PROPERTIES set, then
590 * @invalidated_properties will always be empty.
592 * This signal corresponds to the
593 * <literal>PropertiesChanged</literal> D-Bus signal on the
594 * <literal>org.freedesktop.DBus.Properties</literal> interface.
598 signals[PROPERTIES_CHANGED_SIGNAL] = g_signal_new ("g-properties-changed",
600 G_SIGNAL_RUN_LAST | G_SIGNAL_MUST_COLLECT,
601 G_STRUCT_OFFSET (GDBusProxyClass, g_properties_changed),
608 G_TYPE_STRV | G_SIGNAL_TYPE_STATIC_SCOPE);
611 * GDBusProxy::g-signal:
612 * @proxy: The #GDBusProxy emitting the signal.
613 * @sender_name: (allow-none): The sender of the signal or %NULL if the connection is not a bus connection.
614 * @signal_name: The name of the signal.
615 * @parameters: A #GVariant tuple with parameters for the signal.
617 * Emitted when a signal from the remote object and interface that @proxy is for, has been received.
621 signals[SIGNAL_SIGNAL] = g_signal_new ("g-signal",
623 G_SIGNAL_RUN_LAST | G_SIGNAL_MUST_COLLECT,
624 G_STRUCT_OFFSET (GDBusProxyClass, g_signal),
637 g_dbus_proxy_init (GDBusProxy *proxy)
639 proxy->priv = g_dbus_proxy_get_instance_private (proxy);
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);
978 } InvalidatedPropGetData;
981 invalidated_property_get_cb (GDBusConnection *connection,
985 InvalidatedPropGetData *data = user_data;
986 const gchar *invalidated_properties[] = {NULL};
987 GVariantBuilder builder;
988 GVariant *value = NULL;
989 GVariant *unpacked_value = NULL;
991 /* errors are fine, the other end could have disconnected */
992 value = g_dbus_connection_call_finish (connection, res, NULL);
998 if (!g_variant_is_of_type (value, G_VARIANT_TYPE ("(v)")))
1000 g_warning ("Expected type '(v)' for Get() reply, got '%s'", g_variant_get_type_string (value));
1004 g_variant_get (value, "(v)", &unpacked_value);
1006 /* synthesize the a{sv} in the PropertiesChanged signal */
1007 g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
1008 g_variant_builder_add (&builder, "{sv}", data->prop_name, unpacked_value);
1010 G_LOCK (properties_lock);
1011 insert_property_checked (data->proxy,
1012 data->prop_name, /* adopts string */
1013 unpacked_value); /* adopts value */
1014 data->prop_name = NULL;
1015 G_UNLOCK (properties_lock);
1017 g_signal_emit (data->proxy,
1018 signals[PROPERTIES_CHANGED_SIGNAL], 0,
1019 g_variant_builder_end (&builder), /* consumed */
1020 invalidated_properties);
1025 g_variant_unref (value);
1026 g_object_unref (data->proxy);
1027 g_free (data->prop_name);
1028 g_slice_free (InvalidatedPropGetData, data);
1032 on_properties_changed (GDBusConnection *connection,
1033 const gchar *sender_name,
1034 const gchar *object_path,
1035 const gchar *interface_name,
1036 const gchar *signal_name,
1037 GVariant *parameters,
1040 SignalSubscriptionData *data = user_data;
1041 gboolean emit_g_signal = FALSE;
1043 const gchar *interface_name_for_signal;
1044 GVariant *changed_properties;
1045 gchar **invalidated_properties;
1051 changed_properties = NULL;
1052 invalidated_properties = NULL;
1054 G_LOCK (signal_subscription_lock);
1055 proxy = data->proxy;
1058 G_UNLOCK (signal_subscription_lock);
1063 g_object_ref (proxy);
1064 G_UNLOCK (signal_subscription_lock);
1067 if (!proxy->priv->initialized)
1070 G_LOCK (properties_lock);
1072 if (proxy->priv->name_owner != NULL && g_strcmp0 (sender_name, proxy->priv->name_owner) != 0)
1074 G_UNLOCK (properties_lock);
1078 if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(sa{sv}as)")))
1080 g_warning ("Value for PropertiesChanged signal with type '%s' does not match '(sa{sv}as)'",
1081 g_variant_get_type_string (parameters));
1082 G_UNLOCK (properties_lock);
1086 g_variant_get (parameters,
1088 &interface_name_for_signal,
1089 &changed_properties,
1090 &invalidated_properties);
1092 if (g_strcmp0 (interface_name_for_signal, proxy->priv->interface_name) != 0)
1094 G_UNLOCK (properties_lock);
1098 g_variant_iter_init (&iter, changed_properties);
1099 while (g_variant_iter_next (&iter, "{sv}", &key, &value))
1101 insert_property_checked (proxy,
1102 key, /* adopts string */
1103 value); /* adopts value */
1104 emit_g_signal = TRUE;
1107 if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_GET_INVALIDATED_PROPERTIES)
1109 if (proxy->priv->name_owner != NULL)
1111 for (n = 0; invalidated_properties[n] != NULL; n++)
1113 InvalidatedPropGetData *data;
1114 data = g_slice_new0 (InvalidatedPropGetData);
1115 data->proxy = g_object_ref (proxy);
1116 data->prop_name = g_strdup (invalidated_properties[n]);
1117 g_dbus_connection_call (proxy->priv->connection,
1118 proxy->priv->name_owner,
1119 proxy->priv->object_path,
1120 "org.freedesktop.DBus.Properties",
1122 g_variant_new ("(ss)", proxy->priv->interface_name, data->prop_name),
1123 G_VARIANT_TYPE ("(v)"),
1124 G_DBUS_CALL_FLAGS_NONE,
1126 NULL, /* GCancellable */
1127 (GAsyncReadyCallback) invalidated_property_get_cb,
1134 emit_g_signal = TRUE;
1135 for (n = 0; invalidated_properties[n] != NULL; n++)
1137 g_hash_table_remove (proxy->priv->properties, invalidated_properties[n]);
1141 G_UNLOCK (properties_lock);
1145 g_signal_emit (proxy, signals[PROPERTIES_CHANGED_SIGNAL],
1148 invalidated_properties);
1152 if (changed_properties != NULL)
1153 g_variant_unref (changed_properties);
1154 g_free (invalidated_properties);
1156 g_object_unref (proxy);
1159 /* ---------------------------------------------------------------------------------------------------- */
1162 process_get_all_reply (GDBusProxy *proxy,
1168 guint num_properties;
1170 if (!g_variant_is_of_type (result, G_VARIANT_TYPE ("(a{sv})")))
1172 g_warning ("Value for GetAll reply with type '%s' does not match '(a{sv})'",
1173 g_variant_get_type_string (result));
1177 G_LOCK (properties_lock);
1179 g_variant_get (result, "(a{sv})", &iter);
1180 while (g_variant_iter_next (iter, "{sv}", &key, &value))
1182 insert_property_checked (proxy,
1183 key, /* adopts string */
1184 value); /* adopts value */
1186 g_variant_iter_free (iter);
1188 num_properties = g_hash_table_size (proxy->priv->properties);
1189 G_UNLOCK (properties_lock);
1191 /* Synthesize ::g-properties-changed changed */
1192 if (num_properties > 0)
1194 GVariant *changed_properties;
1195 const gchar *invalidated_properties[1] = {NULL};
1197 g_variant_get (result,
1199 &changed_properties);
1200 g_signal_emit (proxy, signals[PROPERTIES_CHANGED_SIGNAL],
1203 invalidated_properties);
1204 g_variant_unref (changed_properties);
1214 GCancellable *cancellable;
1216 } LoadPropertiesOnNameOwnerChangedData;
1219 on_name_owner_changed_get_all_cb (GDBusConnection *connection,
1223 LoadPropertiesOnNameOwnerChangedData *data = user_data;
1231 result = g_dbus_connection_call_finish (connection,
1236 if (error->domain == G_IO_ERROR && error->code == G_IO_ERROR_CANCELLED)
1238 /* We just ignore if GetAll() is failing. Because this might happen
1239 * if the object has no properties at all. Or if the caller is
1240 * not authorized to see the properties.
1242 * Either way, apps can know about this by using
1243 * get_cached_property_names() or get_cached_property().
1245 * TODO: handle G_DBUS_DEBUG flag 'proxy' and, if enabled, log the
1246 * fact that GetAll() failed
1248 //g_debug ("error: %d %d %s", error->domain, error->code, error->message);
1249 g_error_free (error);
1252 /* and finally we can notify */
1255 G_LOCK (properties_lock);
1256 g_free (data->proxy->priv->name_owner);
1257 data->proxy->priv->name_owner = data->name_owner;
1258 data->name_owner = NULL; /* to avoid an extra copy, we steal the string */
1259 g_hash_table_remove_all (data->proxy->priv->properties);
1260 G_UNLOCK (properties_lock);
1263 process_get_all_reply (data->proxy, result);
1264 g_variant_unref (result);
1267 g_object_notify (G_OBJECT (data->proxy), "g-name-owner");
1270 if (data->cancellable == data->proxy->priv->get_all_cancellable)
1271 data->proxy->priv->get_all_cancellable = NULL;
1273 g_object_unref (data->proxy);
1274 g_object_unref (data->cancellable);
1275 g_free (data->name_owner);
1280 on_name_owner_changed (GDBusConnection *connection,
1281 const gchar *sender_name,
1282 const gchar *object_path,
1283 const gchar *interface_name,
1284 const gchar *signal_name,
1285 GVariant *parameters,
1288 SignalSubscriptionData *data = user_data;
1290 const gchar *old_owner;
1291 const gchar *new_owner;
1293 G_LOCK (signal_subscription_lock);
1294 proxy = data->proxy;
1297 G_UNLOCK (signal_subscription_lock);
1302 g_object_ref (proxy);
1303 G_UNLOCK (signal_subscription_lock);
1306 /* if we are already trying to load properties, cancel that */
1307 if (proxy->priv->get_all_cancellable != NULL)
1309 g_cancellable_cancel (proxy->priv->get_all_cancellable);
1310 proxy->priv->get_all_cancellable = NULL;
1313 g_variant_get (parameters,
1319 if (strlen (new_owner) == 0)
1321 G_LOCK (properties_lock);
1322 g_free (proxy->priv->name_owner);
1323 proxy->priv->name_owner = NULL;
1325 /* Synthesize ::g-properties-changed changed */
1326 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES) &&
1327 g_hash_table_size (proxy->priv->properties) > 0)
1329 GVariantBuilder builder;
1330 GPtrArray *invalidated_properties;
1331 GHashTableIter iter;
1334 /* Build changed_properties (always empty) and invalidated_properties ... */
1335 g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
1337 invalidated_properties = g_ptr_array_new_with_free_func (g_free);
1338 g_hash_table_iter_init (&iter, proxy->priv->properties);
1339 while (g_hash_table_iter_next (&iter, (gpointer) &key, NULL))
1340 g_ptr_array_add (invalidated_properties, g_strdup (key));
1341 g_ptr_array_add (invalidated_properties, NULL);
1343 /* ... throw out the properties ... */
1344 g_hash_table_remove_all (proxy->priv->properties);
1346 G_UNLOCK (properties_lock);
1348 /* ... and finally emit the ::g-properties-changed signal */
1349 g_signal_emit (proxy, signals[PROPERTIES_CHANGED_SIGNAL],
1351 g_variant_builder_end (&builder) /* consumed */,
1352 (const gchar* const *) invalidated_properties->pdata);
1353 g_ptr_array_unref (invalidated_properties);
1357 G_UNLOCK (properties_lock);
1359 g_object_notify (G_OBJECT (proxy), "g-name-owner");
1363 G_LOCK (properties_lock);
1365 /* ignore duplicates - this can happen when activating the service */
1366 if (g_strcmp0 (new_owner, proxy->priv->name_owner) == 0)
1368 G_UNLOCK (properties_lock);
1372 if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES)
1374 g_free (proxy->priv->name_owner);
1375 proxy->priv->name_owner = g_strdup (new_owner);
1377 g_hash_table_remove_all (proxy->priv->properties);
1378 G_UNLOCK (properties_lock);
1379 g_object_notify (G_OBJECT (proxy), "g-name-owner");
1383 LoadPropertiesOnNameOwnerChangedData *data;
1385 G_UNLOCK (properties_lock);
1387 /* start loading properties.. only then emit notify::g-name-owner .. we
1388 * need to be able to cancel this in the event another NameOwnerChanged
1389 * signal suddenly happens
1392 g_assert (proxy->priv->get_all_cancellable == NULL);
1393 proxy->priv->get_all_cancellable = g_cancellable_new ();
1394 data = g_new0 (LoadPropertiesOnNameOwnerChangedData, 1);
1395 data->proxy = g_object_ref (proxy);
1396 data->cancellable = proxy->priv->get_all_cancellable;
1397 data->name_owner = g_strdup (new_owner);
1398 g_dbus_connection_call (proxy->priv->connection,
1400 proxy->priv->object_path,
1401 "org.freedesktop.DBus.Properties",
1403 g_variant_new ("(s)", proxy->priv->interface_name),
1404 G_VARIANT_TYPE ("(a{sv})"),
1405 G_DBUS_CALL_FLAGS_NONE,
1407 proxy->priv->get_all_cancellable,
1408 (GAsyncReadyCallback) on_name_owner_changed_get_all_cb,
1415 g_object_unref (proxy);
1418 /* ---------------------------------------------------------------------------------------------------- */
1423 GCancellable *cancellable;
1424 GSimpleAsyncResult *simple;
1428 async_init_data_free (AsyncInitData *data)
1430 g_object_unref (data->proxy);
1431 if (data->cancellable != NULL)
1432 g_object_unref (data->cancellable);
1433 g_object_unref (data->simple);
1438 async_init_get_all_cb (GDBusConnection *connection,
1442 AsyncInitData *data = user_data;
1447 result = g_dbus_connection_call_finish (connection,
1452 /* We just ignore if GetAll() is failing. Because this might happen
1453 * if the object has no properties at all. Or if the caller is
1454 * not authorized to see the properties.
1456 * Either way, apps can know about this by using
1457 * get_cached_property_names() or get_cached_property().
1459 * TODO: handle G_DBUS_DEBUG flag 'proxy' and, if enabled, log the
1460 * fact that GetAll() failed
1462 //g_debug ("error: %d %d %s", error->domain, error->code, error->message);
1463 g_error_free (error);
1467 g_simple_async_result_set_op_res_gpointer (data->simple,
1469 (GDestroyNotify) g_variant_unref);
1472 g_simple_async_result_complete_in_idle (data->simple);
1473 async_init_data_free (data);
1477 async_init_data_set_name_owner (AsyncInitData *data,
1478 const gchar *name_owner)
1483 if (name_owner != NULL)
1485 /* it starts as NULL anyway */
1486 G_LOCK (properties_lock);
1487 data->proxy->priv->name_owner = g_strdup (name_owner);
1488 G_UNLOCK (properties_lock);
1493 if (data->proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES)
1495 /* Don't load properties if the API user doesn't want them */
1498 else if (name_owner == NULL && data->proxy->priv->name != NULL)
1500 /* Don't attempt to load properties if the name_owner is NULL (which
1501 * usually means the name isn't owned), unless name is also NULL (which
1502 * means we actually wanted to talk to the directly-connected process -
1503 * either dbus-daemon or a peer - instead of going via dbus-daemon)
1510 /* load all properties asynchronously */
1511 g_dbus_connection_call (data->proxy->priv->connection,
1513 data->proxy->priv->object_path,
1514 "org.freedesktop.DBus.Properties",
1516 g_variant_new ("(s)", data->proxy->priv->interface_name),
1517 G_VARIANT_TYPE ("(a{sv})"),
1518 G_DBUS_CALL_FLAGS_NONE,
1521 (GAsyncReadyCallback) async_init_get_all_cb,
1526 g_simple_async_result_complete_in_idle (data->simple);
1527 async_init_data_free (data);
1532 async_init_get_name_owner_cb (GDBusConnection *connection,
1536 AsyncInitData *data = user_data;
1541 result = g_dbus_connection_call_finish (connection,
1546 if (error->domain == G_DBUS_ERROR &&
1547 error->code == G_DBUS_ERROR_NAME_HAS_NO_OWNER)
1549 g_error_free (error);
1550 async_init_data_set_name_owner (data, NULL);
1554 g_simple_async_result_take_error (data->simple, error);
1555 g_simple_async_result_complete_in_idle (data->simple);
1556 async_init_data_free (data);
1561 /* borrowed from result to avoid an extra copy */
1562 const gchar *name_owner;
1564 g_variant_get (result, "(&s)", &name_owner);
1565 async_init_data_set_name_owner (data, name_owner);
1566 g_variant_unref (result);
1571 async_init_call_get_name_owner (AsyncInitData *data)
1573 g_dbus_connection_call (data->proxy->priv->connection,
1574 "org.freedesktop.DBus", /* name */
1575 "/org/freedesktop/DBus", /* object path */
1576 "org.freedesktop.DBus", /* interface */
1578 g_variant_new ("(s)",
1579 data->proxy->priv->name),
1580 G_VARIANT_TYPE ("(s)"),
1581 G_DBUS_CALL_FLAGS_NONE,
1584 (GAsyncReadyCallback) async_init_get_name_owner_cb,
1589 async_init_start_service_by_name_cb (GDBusConnection *connection,
1593 AsyncInitData *data = user_data;
1598 result = g_dbus_connection_call_finish (connection,
1603 /* Errors are not unexpected; the bus will reply e.g.
1605 * org.freedesktop.DBus.Error.ServiceUnknown: The name org.gnome.Epiphany2
1606 * was not provided by any .service files
1610 * org.freedesktop.systemd1.Masked: Unit polkit.service is masked.
1612 * This doesn't mean that the name doesn't have an owner, just
1613 * that it's not provided by a .service file or can't currently
1616 * In particular, in both cases, it could be that a service
1617 * owner will actually appear later. So instead of erroring out,
1618 * we just proceed to invoke GetNameOwner() if dealing with the
1619 * kind of errors above.
1621 if (error->domain == G_DBUS_ERROR && error->code == G_DBUS_ERROR_SERVICE_UNKNOWN)
1623 g_error_free (error);
1627 gchar *remote_error = g_dbus_error_get_remote_error (error);
1628 if (g_strcmp0 (remote_error, "org.freedesktop.systemd1.Masked") == 0)
1630 g_error_free (error);
1631 g_free (remote_error);
1635 g_prefix_error (&error,
1636 _("Error calling StartServiceByName for %s: "),
1637 data->proxy->priv->name);
1638 g_free (remote_error);
1645 guint32 start_service_result;
1646 g_variant_get (result,
1648 &start_service_result);
1649 g_variant_unref (result);
1650 if (start_service_result == 1 || /* DBUS_START_REPLY_SUCCESS */
1651 start_service_result == 2) /* DBUS_START_REPLY_ALREADY_RUNNING */
1653 /* continue to invoke GetNameOwner() */
1657 error = g_error_new (G_IO_ERROR,
1659 _("Unexpected reply %d from StartServiceByName(\"%s\") method"),
1660 start_service_result,
1661 data->proxy->priv->name);
1666 async_init_call_get_name_owner (data);
1670 g_warn_if_fail (error != NULL);
1671 g_simple_async_result_take_error (data->simple, error);
1672 g_simple_async_result_complete_in_idle (data->simple);
1673 async_init_data_free (data);
1677 async_init_call_start_service_by_name (AsyncInitData *data)
1679 g_dbus_connection_call (data->proxy->priv->connection,
1680 "org.freedesktop.DBus", /* name */
1681 "/org/freedesktop/DBus", /* object path */
1682 "org.freedesktop.DBus", /* interface */
1683 "StartServiceByName",
1684 g_variant_new ("(su)",
1685 data->proxy->priv->name,
1687 G_VARIANT_TYPE ("(u)"),
1688 G_DBUS_CALL_FLAGS_NONE,
1691 (GAsyncReadyCallback) async_init_start_service_by_name_cb,
1696 async_initable_init_second_async (GAsyncInitable *initable,
1698 GCancellable *cancellable,
1699 GAsyncReadyCallback callback,
1702 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1703 AsyncInitData *data;
1705 data = g_new0 (AsyncInitData, 1);
1706 data->proxy = g_object_ref (proxy);
1707 data->cancellable = cancellable != NULL ? g_object_ref (cancellable) : NULL;
1708 data->simple = g_simple_async_result_new (G_OBJECT (proxy),
1712 g_simple_async_result_set_check_cancellable (data->simple, cancellable);
1714 /* Check name ownership asynchronously - possibly also start the service */
1715 if (proxy->priv->name == NULL)
1718 async_init_data_set_name_owner (data, NULL);
1720 else if (g_dbus_is_unique_name (proxy->priv->name))
1722 async_init_data_set_name_owner (data, proxy->priv->name);
1726 if ((proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START) ||
1727 (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START_AT_CONSTRUCTION))
1729 async_init_call_get_name_owner (data);
1733 async_init_call_start_service_by_name (data);
1739 async_initable_init_second_finish (GAsyncInitable *initable,
1743 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1744 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1750 if (g_simple_async_result_propagate_error (simple, error))
1753 result = g_simple_async_result_get_op_res_gpointer (simple);
1756 process_get_all_reply (proxy, result);
1762 proxy->priv->initialized = TRUE;
1766 /* ---------------------------------------------------------------------------------------------------- */
1769 async_initable_init_first (GAsyncInitable *initable)
1771 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1773 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES))
1775 /* subscribe to PropertiesChanged() */
1776 proxy->priv->properties_changed_subscription_id =
1777 g_dbus_connection_signal_subscribe (proxy->priv->connection,
1779 "org.freedesktop.DBus.Properties",
1780 "PropertiesChanged",
1781 proxy->priv->object_path,
1782 proxy->priv->interface_name,
1783 G_DBUS_SIGNAL_FLAGS_NONE,
1784 on_properties_changed,
1785 signal_subscription_ref (proxy->priv->signal_subscription_data),
1786 (GDestroyNotify) signal_subscription_unref);
1789 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS))
1791 /* subscribe to all signals for the object */
1792 proxy->priv->signals_subscription_id =
1793 g_dbus_connection_signal_subscribe (proxy->priv->connection,
1795 proxy->priv->interface_name,
1797 proxy->priv->object_path,
1799 G_DBUS_SIGNAL_FLAGS_NONE,
1801 signal_subscription_ref (proxy->priv->signal_subscription_data),
1802 (GDestroyNotify) signal_subscription_unref);
1805 if (proxy->priv->name != NULL && !g_dbus_is_unique_name (proxy->priv->name))
1807 proxy->priv->name_owner_changed_subscription_id =
1808 g_dbus_connection_signal_subscribe (proxy->priv->connection,
1809 "org.freedesktop.DBus", /* name */
1810 "org.freedesktop.DBus", /* interface */
1811 "NameOwnerChanged", /* signal name */
1812 "/org/freedesktop/DBus", /* path */
1813 proxy->priv->name, /* arg0 */
1814 G_DBUS_SIGNAL_FLAGS_NONE,
1815 on_name_owner_changed,
1816 signal_subscription_ref (proxy->priv->signal_subscription_data),
1817 (GDestroyNotify) signal_subscription_unref);
1821 /* ---------------------------------------------------------------------------------------------------- */
1823 /* initialization is split into two parts - the first is the
1824 * non-blocing part that requires the callers GMainContext - the
1825 * second is a blocking part async part that doesn't require the
1826 * callers GMainContext.. we do this split so the code can be reused
1827 * in the GInitable implementation below.
1829 * Note that obtaining a GDBusConnection is not shared between the two
1837 GCancellable *cancellable;
1838 GAsyncReadyCallback callback;
1840 } GetConnectionData;
1843 get_connection_cb (GObject *source_object,
1847 GetConnectionData *data = user_data;
1851 data->proxy->priv->connection = g_bus_get_finish (res, &error);
1852 if (data->proxy->priv->connection == NULL)
1854 GSimpleAsyncResult *simple;
1855 simple = g_simple_async_result_new (G_OBJECT (data->proxy),
1859 g_simple_async_result_set_check_cancellable (simple, data->cancellable);
1860 g_simple_async_result_take_error (simple, error);
1861 g_simple_async_result_complete_in_idle (simple);
1862 g_object_unref (simple);
1866 async_initable_init_first (G_ASYNC_INITABLE (data->proxy));
1867 async_initable_init_second_async (G_ASYNC_INITABLE (data->proxy),
1874 if (data->cancellable != NULL)
1875 g_object_unref (data->cancellable);
1877 g_object_unref (data->proxy);
1882 async_initable_init_async (GAsyncInitable *initable,
1884 GCancellable *cancellable,
1885 GAsyncReadyCallback callback,
1888 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1890 if (proxy->priv->bus_type != G_BUS_TYPE_NONE)
1892 GetConnectionData *data;
1894 g_assert (proxy->priv->connection == NULL);
1896 data = g_new0 (GetConnectionData, 1);
1897 data->proxy = g_object_ref (proxy);
1898 data->io_priority = io_priority;
1899 data->cancellable = cancellable != NULL ? g_object_ref (cancellable) : NULL;
1900 data->callback = callback;
1901 data->user_data = user_data;
1902 g_bus_get (proxy->priv->bus_type,
1909 async_initable_init_first (initable);
1910 async_initable_init_second_async (initable, io_priority, cancellable, callback, user_data);
1915 async_initable_init_finish (GAsyncInitable *initable,
1919 return async_initable_init_second_finish (initable, res, error);
1923 async_initable_iface_init (GAsyncInitableIface *async_initable_iface)
1925 async_initable_iface->init_async = async_initable_init_async;
1926 async_initable_iface->init_finish = async_initable_init_finish;
1929 /* ---------------------------------------------------------------------------------------------------- */
1933 GMainContext *context;
1936 } InitableAsyncInitableData;
1939 async_initable_init_async_cb (GObject *source_object,
1943 InitableAsyncInitableData *data = user_data;
1944 data->res = g_object_ref (res);
1945 g_main_loop_quit (data->loop);
1948 /* Simply reuse the GAsyncInitable implementation but run the first
1949 * part (that is non-blocking and requires the callers GMainContext)
1950 * with the callers GMainContext.. and the second with a private
1951 * GMainContext (bug 621310 is slightly related).
1953 * Note that obtaining a GDBusConnection is not shared between the two
1957 initable_init (GInitable *initable,
1958 GCancellable *cancellable,
1961 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1962 InitableAsyncInitableData *data;
1967 if (proxy->priv->bus_type != G_BUS_TYPE_NONE)
1969 g_assert (proxy->priv->connection == NULL);
1970 proxy->priv->connection = g_bus_get_sync (proxy->priv->bus_type,
1973 if (proxy->priv->connection == NULL)
1977 async_initable_init_first (G_ASYNC_INITABLE (initable));
1979 data = g_new0 (InitableAsyncInitableData, 1);
1980 data->context = g_main_context_new ();
1981 data->loop = g_main_loop_new (data->context, FALSE);
1983 g_main_context_push_thread_default (data->context);
1985 async_initable_init_second_async (G_ASYNC_INITABLE (initable),
1988 async_initable_init_async_cb,
1991 g_main_loop_run (data->loop);
1993 ret = async_initable_init_second_finish (G_ASYNC_INITABLE (initable),
1997 g_main_context_pop_thread_default (data->context);
1999 g_main_context_unref (data->context);
2000 g_main_loop_unref (data->loop);
2001 g_object_unref (data->res);
2010 initable_iface_init (GInitableIface *initable_iface)
2012 initable_iface->init = initable_init;
2015 /* ---------------------------------------------------------------------------------------------------- */
2019 * @connection: A #GDBusConnection.
2020 * @flags: Flags used when constructing the proxy.
2021 * @info: (allow-none): A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
2022 * @name: (allow-none): A bus name (well-known or unique) or %NULL if @connection is not a message bus connection.
2023 * @object_path: An object path.
2024 * @interface_name: A D-Bus interface name.
2025 * @cancellable: (allow-none): A #GCancellable or %NULL.
2026 * @callback: Callback function to invoke when the proxy is ready.
2027 * @user_data: User data to pass to @callback.
2029 * Creates a proxy for accessing @interface_name on the remote object
2030 * at @object_path owned by @name at @connection and asynchronously
2031 * loads D-Bus properties unless the
2032 * %G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES flag is used. Connect to
2033 * the #GDBusProxy::g-properties-changed signal to get notified about
2036 * If the %G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS flag is not set, also sets up
2037 * match rules for signals. Connect to the #GDBusProxy::g-signal signal
2038 * to handle signals from the remote object.
2040 * If @name is a well-known name and the
2041 * %G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START and %G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START_AT_CONSTRUCTION
2042 * flags aren't set and no name owner currently exists, the message bus
2043 * will be requested to launch a name owner for the name.
2045 * This is a failable asynchronous constructor - when the proxy is
2046 * ready, @callback will be invoked and you can use
2047 * g_dbus_proxy_new_finish() to get the result.
2049 * See g_dbus_proxy_new_sync() and for a synchronous version of this constructor.
2051 * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
2056 g_dbus_proxy_new (GDBusConnection *connection,
2057 GDBusProxyFlags flags,
2058 GDBusInterfaceInfo *info,
2060 const gchar *object_path,
2061 const gchar *interface_name,
2062 GCancellable *cancellable,
2063 GAsyncReadyCallback callback,
2066 g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
2067 g_return_if_fail ((name == NULL && g_dbus_connection_get_unique_name (connection) == NULL) || g_dbus_is_name (name));
2068 g_return_if_fail (g_variant_is_object_path (object_path));
2069 g_return_if_fail (g_dbus_is_interface_name (interface_name));
2071 g_async_initable_new_async (G_TYPE_DBUS_PROXY,
2077 "g-interface-info", info,
2079 "g-connection", connection,
2080 "g-object-path", object_path,
2081 "g-interface-name", interface_name,
2086 * g_dbus_proxy_new_finish:
2087 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback function passed to g_dbus_proxy_new().
2088 * @error: Return location for error or %NULL.
2090 * Finishes creating a #GDBusProxy.
2092 * Returns: A #GDBusProxy or %NULL if @error is set. Free with g_object_unref().
2097 g_dbus_proxy_new_finish (GAsyncResult *res,
2101 GObject *source_object;
2103 source_object = g_async_result_get_source_object (res);
2104 g_assert (source_object != NULL);
2106 object = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object),
2109 g_object_unref (source_object);
2112 return G_DBUS_PROXY (object);
2118 * g_dbus_proxy_new_sync:
2119 * @connection: A #GDBusConnection.
2120 * @flags: Flags used when constructing the proxy.
2121 * @info: (allow-none): A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
2122 * @name: (allow-none): A bus name (well-known or unique) or %NULL if @connection is not a message bus connection.
2123 * @object_path: An object path.
2124 * @interface_name: A D-Bus interface name.
2125 * @cancellable: (allow-none): A #GCancellable or %NULL.
2126 * @error: (allow-none): Return location for error or %NULL.
2128 * Creates a proxy for accessing @interface_name on the remote object
2129 * at @object_path owned by @name at @connection and synchronously
2130 * loads D-Bus properties unless the
2131 * %G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES flag is used.
2133 * If the %G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS flag is not set, also sets up
2134 * match rules for signals. Connect to the #GDBusProxy::g-signal signal
2135 * to handle signals from the remote object.
2137 * If @name is a well-known name and the
2138 * %G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START and %G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START_AT_CONSTRUCTION
2139 * flags aren't set and no name owner currently exists, the message bus
2140 * will be requested to launch a name owner for the name.
2142 * This is a synchronous failable constructor. See g_dbus_proxy_new()
2143 * and g_dbus_proxy_new_finish() for the asynchronous version.
2145 * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
2147 * Returns: A #GDBusProxy or %NULL if error is set. Free with g_object_unref().
2152 g_dbus_proxy_new_sync (GDBusConnection *connection,
2153 GDBusProxyFlags flags,
2154 GDBusInterfaceInfo *info,
2156 const gchar *object_path,
2157 const gchar *interface_name,
2158 GCancellable *cancellable,
2161 GInitable *initable;
2163 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
2164 g_return_val_if_fail ((name == NULL && g_dbus_connection_get_unique_name (connection) == NULL) ||
2165 g_dbus_is_name (name), NULL);
2166 g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
2167 g_return_val_if_fail (g_dbus_is_interface_name (interface_name), NULL);
2169 initable = g_initable_new (G_TYPE_DBUS_PROXY,
2173 "g-interface-info", info,
2175 "g-connection", connection,
2176 "g-object-path", object_path,
2177 "g-interface-name", interface_name,
2179 if (initable != NULL)
2180 return G_DBUS_PROXY (initable);
2185 /* ---------------------------------------------------------------------------------------------------- */
2188 * g_dbus_proxy_new_for_bus:
2189 * @bus_type: A #GBusType.
2190 * @flags: Flags used when constructing the proxy.
2191 * @info: (allow-none): A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
2192 * @name: A bus name (well-known or unique).
2193 * @object_path: An object path.
2194 * @interface_name: A D-Bus interface name.
2195 * @cancellable: (allow-none): A #GCancellable or %NULL.
2196 * @callback: Callback function to invoke when the proxy is ready.
2197 * @user_data: User data to pass to @callback.
2199 * Like g_dbus_proxy_new() but takes a #GBusType instead of a #GDBusConnection.
2201 * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
2206 g_dbus_proxy_new_for_bus (GBusType bus_type,
2207 GDBusProxyFlags flags,
2208 GDBusInterfaceInfo *info,
2210 const gchar *object_path,
2211 const gchar *interface_name,
2212 GCancellable *cancellable,
2213 GAsyncReadyCallback callback,
2216 g_return_if_fail (g_dbus_is_name (name));
2217 g_return_if_fail (g_variant_is_object_path (object_path));
2218 g_return_if_fail (g_dbus_is_interface_name (interface_name));
2220 g_async_initable_new_async (G_TYPE_DBUS_PROXY,
2226 "g-interface-info", info,
2228 "g-bus-type", bus_type,
2229 "g-object-path", object_path,
2230 "g-interface-name", interface_name,
2235 * g_dbus_proxy_new_for_bus_finish:
2236 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback function passed to g_dbus_proxy_new_for_bus().
2237 * @error: Return location for error or %NULL.
2239 * Finishes creating a #GDBusProxy.
2241 * Returns: A #GDBusProxy or %NULL if @error is set. Free with g_object_unref().
2246 g_dbus_proxy_new_for_bus_finish (GAsyncResult *res,
2249 return g_dbus_proxy_new_finish (res, error);
2253 * g_dbus_proxy_new_for_bus_sync:
2254 * @bus_type: A #GBusType.
2255 * @flags: Flags used when constructing the proxy.
2256 * @info: (allow-none): A #GDBusInterfaceInfo specifying the minimal interface
2257 * that @proxy conforms to or %NULL.
2258 * @name: A bus name (well-known or unique).
2259 * @object_path: An object path.
2260 * @interface_name: A D-Bus interface name.
2261 * @cancellable: (allow-none): A #GCancellable or %NULL.
2262 * @error: Return location for error or %NULL.
2264 * Like g_dbus_proxy_new_sync() but takes a #GBusType instead of a #GDBusConnection.
2266 * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
2268 * Returns: A #GDBusProxy or %NULL if error is set. Free with g_object_unref().
2273 g_dbus_proxy_new_for_bus_sync (GBusType bus_type,
2274 GDBusProxyFlags flags,
2275 GDBusInterfaceInfo *info,
2277 const gchar *object_path,
2278 const gchar *interface_name,
2279 GCancellable *cancellable,
2282 GInitable *initable;
2284 g_return_val_if_fail (g_dbus_is_name (name), NULL);
2285 g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
2286 g_return_val_if_fail (g_dbus_is_interface_name (interface_name), NULL);
2288 initable = g_initable_new (G_TYPE_DBUS_PROXY,
2292 "g-interface-info", info,
2294 "g-bus-type", bus_type,
2295 "g-object-path", object_path,
2296 "g-interface-name", interface_name,
2298 if (initable != NULL)
2299 return G_DBUS_PROXY (initable);
2304 /* ---------------------------------------------------------------------------------------------------- */
2307 * g_dbus_proxy_get_connection:
2308 * @proxy: A #GDBusProxy.
2310 * Gets the connection @proxy is for.
2312 * Returns: (transfer none): A #GDBusConnection owned by @proxy. Do not free.
2317 g_dbus_proxy_get_connection (GDBusProxy *proxy)
2319 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2320 return proxy->priv->connection;
2324 * g_dbus_proxy_get_flags:
2325 * @proxy: A #GDBusProxy.
2327 * Gets the flags that @proxy was constructed with.
2329 * Returns: Flags from the #GDBusProxyFlags enumeration.
2334 g_dbus_proxy_get_flags (GDBusProxy *proxy)
2336 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), 0);
2337 return proxy->priv->flags;
2341 * g_dbus_proxy_get_name:
2342 * @proxy: A #GDBusProxy.
2344 * Gets the name that @proxy was constructed for.
2346 * Returns: A string owned by @proxy. Do not free.
2351 g_dbus_proxy_get_name (GDBusProxy *proxy)
2353 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2354 return proxy->priv->name;
2358 * g_dbus_proxy_get_name_owner:
2359 * @proxy: A #GDBusProxy.
2361 * The unique name that owns the name that @proxy is for or %NULL if
2362 * no-one currently owns that name. You may connect to the
2363 * #GObject::notify signal to track changes to the
2364 * #GDBusProxy:g-name-owner property.
2366 * Returns: The name owner or %NULL if no name owner exists. Free with g_free().
2371 g_dbus_proxy_get_name_owner (GDBusProxy *proxy)
2375 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2377 G_LOCK (properties_lock);
2378 ret = g_strdup (proxy->priv->name_owner);
2379 G_UNLOCK (properties_lock);
2384 * g_dbus_proxy_get_object_path:
2385 * @proxy: A #GDBusProxy.
2387 * Gets the object path @proxy is for.
2389 * Returns: A string owned by @proxy. Do not free.
2394 g_dbus_proxy_get_object_path (GDBusProxy *proxy)
2396 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2397 return proxy->priv->object_path;
2401 * g_dbus_proxy_get_interface_name:
2402 * @proxy: A #GDBusProxy.
2404 * Gets the D-Bus interface name @proxy is for.
2406 * Returns: A string owned by @proxy. Do not free.
2411 g_dbus_proxy_get_interface_name (GDBusProxy *proxy)
2413 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2414 return proxy->priv->interface_name;
2418 * g_dbus_proxy_get_default_timeout:
2419 * @proxy: A #GDBusProxy.
2421 * Gets the timeout to use if -1 (specifying default timeout) is
2422 * passed as @timeout_msec in the g_dbus_proxy_call() and
2423 * g_dbus_proxy_call_sync() functions.
2425 * See the #GDBusProxy:g-default-timeout property for more details.
2427 * Returns: Timeout to use for @proxy.
2432 g_dbus_proxy_get_default_timeout (GDBusProxy *proxy)
2436 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), -1);
2438 G_LOCK (properties_lock);
2439 ret = proxy->priv->timeout_msec;
2440 G_UNLOCK (properties_lock);
2445 * g_dbus_proxy_set_default_timeout:
2446 * @proxy: A #GDBusProxy.
2447 * @timeout_msec: Timeout in milliseconds.
2449 * Sets the timeout to use if -1 (specifying default timeout) is
2450 * passed as @timeout_msec in the g_dbus_proxy_call() and
2451 * g_dbus_proxy_call_sync() functions.
2453 * See the #GDBusProxy:g-default-timeout property for more details.
2458 g_dbus_proxy_set_default_timeout (GDBusProxy *proxy,
2461 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
2462 g_return_if_fail (timeout_msec == -1 || timeout_msec >= 0);
2464 G_LOCK (properties_lock);
2466 if (proxy->priv->timeout_msec != timeout_msec)
2468 proxy->priv->timeout_msec = timeout_msec;
2469 G_UNLOCK (properties_lock);
2471 g_object_notify (G_OBJECT (proxy), "g-default-timeout");
2475 G_UNLOCK (properties_lock);
2480 * g_dbus_proxy_get_interface_info:
2481 * @proxy: A #GDBusProxy
2483 * Returns the #GDBusInterfaceInfo, if any, specifying the interface
2484 * that @proxy conforms to. See the #GDBusProxy:g-interface-info
2485 * property for more details.
2487 * Returns: A #GDBusInterfaceInfo or %NULL. Do not unref the returned
2488 * object, it is owned by @proxy.
2492 GDBusInterfaceInfo *
2493 g_dbus_proxy_get_interface_info (GDBusProxy *proxy)
2495 GDBusInterfaceInfo *ret;
2497 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2499 G_LOCK (properties_lock);
2500 ret = proxy->priv->expected_interface;
2501 G_UNLOCK (properties_lock);
2502 /* FIXME: returning a borrowed ref with no guarantee that nobody will
2503 * call g_dbus_proxy_set_interface_info() and make it invalid...
2509 * g_dbus_proxy_set_interface_info:
2510 * @proxy: A #GDBusProxy
2511 * @info: (allow-none): Minimum interface this proxy conforms to or %NULL to unset.
2513 * Ensure that interactions with @proxy conform to the given
2514 * interface. See the #GDBusProxy:g-interface-info property for more
2520 g_dbus_proxy_set_interface_info (GDBusProxy *proxy,
2521 GDBusInterfaceInfo *info)
2523 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
2524 G_LOCK (properties_lock);
2526 if (proxy->priv->expected_interface != NULL)
2528 g_dbus_interface_info_cache_release (proxy->priv->expected_interface);
2529 g_dbus_interface_info_unref (proxy->priv->expected_interface);
2531 proxy->priv->expected_interface = info != NULL ? g_dbus_interface_info_ref (info) : NULL;
2532 if (proxy->priv->expected_interface != NULL)
2533 g_dbus_interface_info_cache_build (proxy->priv->expected_interface);
2535 G_UNLOCK (properties_lock);
2538 /* ---------------------------------------------------------------------------------------------------- */
2541 maybe_split_method_name (const gchar *method_name,
2542 gchar **out_interface_name,
2543 const gchar **out_method_name)
2548 g_assert (out_interface_name != NULL);
2549 g_assert (out_method_name != NULL);
2550 *out_interface_name = NULL;
2551 *out_method_name = NULL;
2553 if (strchr (method_name, '.') != NULL)
2558 p = g_strdup (method_name);
2559 last_dot = strrchr (p, '.');
2562 *out_interface_name = p;
2563 *out_method_name = last_dot + 1;
2575 GUnixFDList *fd_list;
2580 reply_data_free (ReplyData *data)
2582 g_variant_unref (data->value);
2584 if (data->fd_list != NULL)
2585 g_object_unref (data->fd_list);
2587 g_slice_free (ReplyData, data);
2591 reply_cb (GDBusConnection *connection,
2595 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
2599 GUnixFDList *fd_list;
2604 value = g_dbus_connection_call_with_unix_fd_list_finish (connection,
2609 value = g_dbus_connection_call_finish (connection,
2615 g_simple_async_result_take_error (simple, error);
2620 data = g_slice_new0 (ReplyData);
2621 data->value = value;
2623 data->fd_list = fd_list;
2625 g_simple_async_result_set_op_res_gpointer (simple, data, (GDestroyNotify) reply_data_free);
2628 /* no need to complete in idle since the method GDBusConnection already does */
2629 g_simple_async_result_complete (simple);
2630 g_object_unref (simple);
2633 /* properties_lock must be held for as long as you will keep the
2636 static const GDBusMethodInfo *
2637 lookup_method_info (GDBusProxy *proxy,
2638 const gchar *method_name)
2640 const GDBusMethodInfo *info = NULL;
2642 if (proxy->priv->expected_interface == NULL)
2645 info = g_dbus_interface_info_lookup_method (proxy->priv->expected_interface, method_name);
2651 /* properties_lock must be held for as long as you will keep the
2654 static const gchar *
2655 get_destination_for_call (GDBusProxy *proxy)
2661 /* If proxy->priv->name is a unique name, then proxy->priv->name_owner
2662 * is never NULL and always the same as proxy->priv->name. We use this
2663 * knowledge to avoid checking if proxy->priv->name is a unique or
2666 ret = proxy->priv->name_owner;
2670 if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START)
2673 ret = proxy->priv->name;
2679 /* ---------------------------------------------------------------------------------------------------- */
2682 g_dbus_proxy_call_internal (GDBusProxy *proxy,
2683 const gchar *method_name,
2684 GVariant *parameters,
2685 GDBusCallFlags flags,
2687 GUnixFDList *fd_list,
2688 GCancellable *cancellable,
2689 GAsyncReadyCallback callback,
2692 GSimpleAsyncResult *simple;
2694 gchar *split_interface_name;
2695 const gchar *split_method_name;
2696 const gchar *target_method_name;
2697 const gchar *target_interface_name;
2699 GVariantType *reply_type;
2700 GAsyncReadyCallback my_callback;
2702 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
2703 g_return_if_fail (g_dbus_is_member_name (method_name) || g_dbus_is_interface_name (method_name));
2704 g_return_if_fail (parameters == NULL || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE));
2705 g_return_if_fail (timeout_msec == -1 || timeout_msec >= 0);
2707 g_return_if_fail (fd_list == NULL || G_IS_UNIX_FD_LIST (fd_list));
2709 g_return_if_fail (fd_list == NULL);
2713 split_interface_name = NULL;
2715 /* g_dbus_connection_call() is optimised for the case of a NULL
2716 * callback. If we get a NULL callback from our user then make sure
2717 * we pass along a NULL callback for ourselves as well.
2719 if (callback != NULL)
2721 my_callback = (GAsyncReadyCallback) reply_cb;
2722 simple = g_simple_async_result_new (G_OBJECT (proxy),
2725 g_dbus_proxy_call_internal);
2726 g_simple_async_result_set_check_cancellable (simple, cancellable);
2734 G_LOCK (properties_lock);
2736 was_split = maybe_split_method_name (method_name, &split_interface_name, &split_method_name);
2737 target_method_name = was_split ? split_method_name : method_name;
2738 target_interface_name = was_split ? split_interface_name : proxy->priv->interface_name;
2740 /* Warn if method is unexpected (cf. :g-interface-info) */
2743 const GDBusMethodInfo *expected_method_info;
2744 expected_method_info = lookup_method_info (proxy, target_method_name);
2745 if (expected_method_info != NULL)
2746 reply_type = _g_dbus_compute_complete_signature (expected_method_info->out_args);
2750 if (proxy->priv->name != NULL)
2752 destination = g_strdup (get_destination_for_call (proxy));
2753 if (destination == NULL)
2757 g_simple_async_result_set_error (simple,
2760 _("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"));
2761 g_simple_async_result_complete_in_idle (simple);
2762 g_object_unref (simple);
2764 G_UNLOCK (properties_lock);
2769 G_UNLOCK (properties_lock);
2772 g_dbus_connection_call_with_unix_fd_list (proxy->priv->connection,
2774 proxy->priv->object_path,
2775 target_interface_name,
2780 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2786 g_dbus_connection_call (proxy->priv->connection,
2788 proxy->priv->object_path,
2789 target_interface_name,
2794 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2801 if (reply_type != NULL)
2802 g_variant_type_free (reply_type);
2804 g_free (destination);
2805 g_free (split_interface_name);
2809 g_dbus_proxy_call_finish_internal (GDBusProxy *proxy,
2810 GUnixFDList **out_fd_list,
2814 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2818 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2819 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2820 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2822 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_dbus_proxy_call_internal);
2826 if (g_simple_async_result_propagate_error (simple, error))
2829 data = g_simple_async_result_get_op_res_gpointer (simple);
2830 value = g_variant_ref (data->value);
2832 if (out_fd_list != NULL)
2833 *out_fd_list = data->fd_list != NULL ? g_object_ref (data->fd_list) : NULL;
2841 g_dbus_proxy_call_sync_internal (GDBusProxy *proxy,
2842 const gchar *method_name,
2843 GVariant *parameters,
2844 GDBusCallFlags flags,
2846 GUnixFDList *fd_list,
2847 GUnixFDList **out_fd_list,
2848 GCancellable *cancellable,
2853 gchar *split_interface_name;
2854 const gchar *split_method_name;
2855 const gchar *target_method_name;
2856 const gchar *target_interface_name;
2858 GVariantType *reply_type;
2860 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2861 g_return_val_if_fail (g_dbus_is_member_name (method_name) || g_dbus_is_interface_name (method_name), NULL);
2862 g_return_val_if_fail (parameters == NULL || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE), NULL);
2863 g_return_val_if_fail (timeout_msec == -1 || timeout_msec >= 0, NULL);
2865 g_return_val_if_fail (fd_list == NULL || G_IS_UNIX_FD_LIST (fd_list), NULL);
2867 g_return_val_if_fail (fd_list == NULL, NULL);
2869 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2873 G_LOCK (properties_lock);
2875 was_split = maybe_split_method_name (method_name, &split_interface_name, &split_method_name);
2876 target_method_name = was_split ? split_method_name : method_name;
2877 target_interface_name = was_split ? split_interface_name : proxy->priv->interface_name;
2879 /* Warn if method is unexpected (cf. :g-interface-info) */
2882 const GDBusMethodInfo *expected_method_info;
2883 expected_method_info = lookup_method_info (proxy, target_method_name);
2884 if (expected_method_info != NULL)
2885 reply_type = _g_dbus_compute_complete_signature (expected_method_info->out_args);
2889 if (proxy->priv->name != NULL)
2891 destination = g_strdup (get_destination_for_call (proxy));
2892 if (destination == NULL)
2894 g_set_error_literal (error,
2897 _("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"));
2899 G_UNLOCK (properties_lock);
2904 G_UNLOCK (properties_lock);
2907 ret = g_dbus_connection_call_with_unix_fd_list_sync (proxy->priv->connection,
2909 proxy->priv->object_path,
2910 target_interface_name,
2915 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2921 ret = g_dbus_connection_call_sync (proxy->priv->connection,
2923 proxy->priv->object_path,
2924 target_interface_name,
2929 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2935 if (reply_type != NULL)
2936 g_variant_type_free (reply_type);
2938 g_free (destination);
2939 g_free (split_interface_name);
2944 /* ---------------------------------------------------------------------------------------------------- */
2947 * g_dbus_proxy_call:
2948 * @proxy: A #GDBusProxy.
2949 * @method_name: Name of method to invoke.
2950 * @parameters: (allow-none): A #GVariant tuple with parameters for the signal or %NULL if not passing parameters.
2951 * @flags: Flags from the #GDBusCallFlags enumeration.
2952 * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
2953 * "infinite") or -1 to use the proxy default timeout.
2954 * @cancellable: (allow-none): A #GCancellable or %NULL.
2955 * @callback: (allow-none): A #GAsyncReadyCallback to call when the request is satisfied or %NULL if you don't
2956 * care about the result of the method invocation.
2957 * @user_data: The data to pass to @callback.
2959 * Asynchronously invokes the @method_name method on @proxy.
2961 * If @method_name contains any dots, then @name is split into interface and
2962 * method name parts. This allows using @proxy for invoking methods on
2965 * If the #GDBusConnection associated with @proxy is closed then
2966 * the operation will fail with %G_IO_ERROR_CLOSED. If
2967 * @cancellable is canceled, the operation will fail with
2968 * %G_IO_ERROR_CANCELLED. If @parameters contains a value not
2969 * compatible with the D-Bus protocol, the operation fails with
2970 * %G_IO_ERROR_INVALID_ARGUMENT.
2972 * If the @parameters #GVariant is floating, it is consumed. This allows
2973 * convenient 'inline' use of g_variant_new(), e.g.:
2975 * g_dbus_proxy_call (proxy,
2977 * g_variant_new ("(ss)",
2980 * G_DBUS_CALL_FLAGS_NONE,
2983 * (GAsyncReadyCallback) two_strings_done,
2987 * If @proxy has an expected interface (see
2988 * #GDBusProxy:g-interface-info) and @method_name is referenced by it,
2989 * then the return value is checked against the return type.
2991 * This is an asynchronous method. When the operation is finished,
2992 * @callback will be invoked in the
2993 * <link linkend="g-main-context-push-thread-default">thread-default main loop</link>
2994 * of the thread you are calling this method from.
2995 * You can then call g_dbus_proxy_call_finish() to get the result of
2996 * the operation. See g_dbus_proxy_call_sync() for the synchronous
2997 * version of this method.
2999 * If @callback is %NULL then the D-Bus method call message will be sent with
3000 * the %G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED flag set.
3005 g_dbus_proxy_call (GDBusProxy *proxy,
3006 const gchar *method_name,
3007 GVariant *parameters,
3008 GDBusCallFlags flags,
3010 GCancellable *cancellable,
3011 GAsyncReadyCallback callback,
3014 g_dbus_proxy_call_internal (proxy, method_name, parameters, flags, timeout_msec, NULL, cancellable, callback, user_data);
3018 * g_dbus_proxy_call_finish:
3019 * @proxy: A #GDBusProxy.
3020 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_proxy_call().
3021 * @error: Return location for error or %NULL.
3023 * Finishes an operation started with g_dbus_proxy_call().
3025 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
3026 * return values. Free with g_variant_unref().
3031 g_dbus_proxy_call_finish (GDBusProxy *proxy,
3035 return g_dbus_proxy_call_finish_internal (proxy, NULL, res, error);
3039 * g_dbus_proxy_call_sync:
3040 * @proxy: A #GDBusProxy.
3041 * @method_name: Name of method to invoke.
3042 * @parameters: (allow-none): A #GVariant tuple with parameters for the signal
3043 * or %NULL if not passing parameters.
3044 * @flags: Flags from the #GDBusCallFlags enumeration.
3045 * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
3046 * "infinite") or -1 to use the proxy default timeout.
3047 * @cancellable: (allow-none): A #GCancellable or %NULL.
3048 * @error: Return location for error or %NULL.
3050 * Synchronously invokes the @method_name method on @proxy.
3052 * If @method_name contains any dots, then @name is split into interface and
3053 * method name parts. This allows using @proxy for invoking methods on
3056 * If the #GDBusConnection associated with @proxy is disconnected then
3057 * the operation will fail with %G_IO_ERROR_CLOSED. If
3058 * @cancellable is canceled, the operation will fail with
3059 * %G_IO_ERROR_CANCELLED. If @parameters contains a value not
3060 * compatible with the D-Bus protocol, the operation fails with
3061 * %G_IO_ERROR_INVALID_ARGUMENT.
3063 * If the @parameters #GVariant is floating, it is consumed. This allows
3064 * convenient 'inline' use of g_variant_new(), e.g.:
3066 * g_dbus_proxy_call_sync (proxy,
3068 * g_variant_new ("(ss)",
3071 * G_DBUS_CALL_FLAGS_NONE,
3077 * The calling thread is blocked until a reply is received. See
3078 * g_dbus_proxy_call() for the asynchronous version of this
3081 * If @proxy has an expected interface (see
3082 * #GDBusProxy:g-interface-info) and @method_name is referenced by it,
3083 * then the return value is checked against the return type.
3085 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
3086 * return values. Free with g_variant_unref().
3091 g_dbus_proxy_call_sync (GDBusProxy *proxy,
3092 const gchar *method_name,
3093 GVariant *parameters,
3094 GDBusCallFlags flags,
3096 GCancellable *cancellable,
3099 return g_dbus_proxy_call_sync_internal (proxy, method_name, parameters, flags, timeout_msec, NULL, NULL, cancellable, error);
3102 /* ---------------------------------------------------------------------------------------------------- */
3107 * g_dbus_proxy_call_with_unix_fd_list:
3108 * @proxy: A #GDBusProxy.
3109 * @method_name: Name of method to invoke.
3110 * @parameters: (allow-none): A #GVariant tuple with parameters for the signal or %NULL if not passing parameters.
3111 * @flags: Flags from the #GDBusCallFlags enumeration.
3112 * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
3113 * "infinite") or -1 to use the proxy default timeout.
3114 * @fd_list: (allow-none): A #GUnixFDList or %NULL.
3115 * @cancellable: (allow-none): A #GCancellable or %NULL.
3116 * @callback: (allow-none): A #GAsyncReadyCallback to call when the request is satisfied or %NULL if you don't
3117 * care about the result of the method invocation.
3118 * @user_data: The data to pass to @callback.
3120 * Like g_dbus_proxy_call() but also takes a #GUnixFDList object.
3122 * This method is only available on UNIX.
3127 g_dbus_proxy_call_with_unix_fd_list (GDBusProxy *proxy,
3128 const gchar *method_name,
3129 GVariant *parameters,
3130 GDBusCallFlags flags,
3132 GUnixFDList *fd_list,
3133 GCancellable *cancellable,
3134 GAsyncReadyCallback callback,
3137 g_dbus_proxy_call_internal (proxy, method_name, parameters, flags, timeout_msec, fd_list, cancellable, callback, user_data);
3141 * g_dbus_proxy_call_with_unix_fd_list_finish:
3142 * @proxy: A #GDBusProxy.
3143 * @out_fd_list: (out) (allow-none): Return location for a #GUnixFDList or %NULL.
3144 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_proxy_call_with_unix_fd_list().
3145 * @error: Return location for error or %NULL.
3147 * Finishes an operation started with g_dbus_proxy_call_with_unix_fd_list().
3149 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
3150 * return values. Free with g_variant_unref().
3155 g_dbus_proxy_call_with_unix_fd_list_finish (GDBusProxy *proxy,
3156 GUnixFDList **out_fd_list,
3160 return g_dbus_proxy_call_finish_internal (proxy, out_fd_list, res, error);
3164 * g_dbus_proxy_call_with_unix_fd_list_sync:
3165 * @proxy: A #GDBusProxy.
3166 * @method_name: Name of method to invoke.
3167 * @parameters: (allow-none): A #GVariant tuple with parameters for the signal
3168 * or %NULL if not passing parameters.
3169 * @flags: Flags from the #GDBusCallFlags enumeration.
3170 * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
3171 * "infinite") or -1 to use the proxy default timeout.
3172 * @fd_list: (allow-none): A #GUnixFDList or %NULL.
3173 * @out_fd_list: (out) (allow-none): Return location for a #GUnixFDList or %NULL.
3174 * @cancellable: (allow-none): A #GCancellable or %NULL.
3175 * @error: Return location for error or %NULL.
3177 * Like g_dbus_proxy_call_sync() but also takes and returns #GUnixFDList objects.
3179 * This method is only available on UNIX.
3181 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
3182 * return values. Free with g_variant_unref().
3187 g_dbus_proxy_call_with_unix_fd_list_sync (GDBusProxy *proxy,
3188 const gchar *method_name,
3189 GVariant *parameters,
3190 GDBusCallFlags flags,
3192 GUnixFDList *fd_list,
3193 GUnixFDList **out_fd_list,
3194 GCancellable *cancellable,
3197 return g_dbus_proxy_call_sync_internal (proxy, method_name, parameters, flags, timeout_msec, fd_list, out_fd_list, cancellable, error);
3200 #endif /* G_OS_UNIX */
3202 /* ---------------------------------------------------------------------------------------------------- */
3204 static GDBusInterfaceInfo *
3205 _g_dbus_proxy_get_info (GDBusInterface *interface)
3207 GDBusProxy *proxy = G_DBUS_PROXY (interface);
3208 return g_dbus_proxy_get_interface_info (proxy);
3211 static GDBusObject *
3212 _g_dbus_proxy_get_object (GDBusInterface *interface)
3214 GDBusProxy *proxy = G_DBUS_PROXY (interface);
3215 return proxy->priv->object;
3218 static GDBusObject *
3219 _g_dbus_proxy_dup_object (GDBusInterface *interface)
3221 GDBusProxy *proxy = G_DBUS_PROXY (interface);
3222 GDBusObject *ret = NULL;
3224 G_LOCK (properties_lock);
3225 if (proxy->priv->object != NULL)
3226 ret = g_object_ref (proxy->priv->object);
3227 G_UNLOCK (properties_lock);
3232 _g_dbus_proxy_set_object (GDBusInterface *interface,
3233 GDBusObject *object)
3235 GDBusProxy *proxy = G_DBUS_PROXY (interface);
3236 G_LOCK (properties_lock);
3237 if (proxy->priv->object != NULL)
3238 g_object_remove_weak_pointer (G_OBJECT (proxy->priv->object), (gpointer *) &proxy->priv->object);
3239 proxy->priv->object = object;
3240 if (proxy->priv->object != NULL)
3241 g_object_add_weak_pointer (G_OBJECT (proxy->priv->object), (gpointer *) &proxy->priv->object);
3242 G_UNLOCK (properties_lock);
3246 dbus_interface_iface_init (GDBusInterfaceIface *dbus_interface_iface)
3248 dbus_interface_iface->get_info = _g_dbus_proxy_get_info;
3249 dbus_interface_iface->get_object = _g_dbus_proxy_get_object;
3250 dbus_interface_iface->dup_object = _g_dbus_proxy_dup_object;
3251 dbus_interface_iface->set_object = _g_dbus_proxy_set_object;
3254 /* ---------------------------------------------------------------------------------------------------- */