1 /* GDBus - GLib D-Bus Library
3 * Copyright (C) 2008-2010 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 * Author: David Zeuthen <davidz@redhat.com>
26 #include "gdbusutils.h"
27 #include "gdbusproxy.h"
28 #include "gioenumtypes.h"
29 #include "gdbusconnection.h"
30 #include "gdbuserror.h"
31 #include "gdbusprivate.h"
32 #include "ginitable.h"
33 #include "gasyncinitable.h"
35 #include "gasyncresult.h"
36 #include "gsimpleasyncresult.h"
37 #include "gcancellable.h"
38 #include "gdbusinterface.h"
41 #include "gunixfdlist.h"
48 * @short_description: Client-side D-Bus interface proxy
51 * #GDBusProxy is a base class used for proxies to access a D-Bus
52 * interface on a remote object. A #GDBusProxy can be constructed for
53 * both well-known and unique names.
55 * By default, #GDBusProxy will cache all properties (and listen to
56 * changes) of the remote object, and proxy all signals that gets
57 * emitted. This behaviour can be changed by passing suitable
58 * #GDBusProxyFlags when the proxy is created. If the proxy is for a
59 * well-known name, the property cache is flushed when the name owner
60 * vanishes and reloaded when a name owner appears.
62 * If a #GDBusProxy is used for a well-known name, the owner of the
63 * name is tracked and can be read from
64 * #GDBusProxy:g-name-owner. Connect to the #GObject::notify signal to
65 * get notified of changes. Additionally, only signals and property
66 * changes emitted from the current name owner are considered and
67 * calls are always sent to the current name owner. This avoids a
68 * number of race conditions when the name is lost by one owner and
69 * claimed by another. However, if no name owner currently exists,
70 * then calls will be sent to the well-known name which may result in
71 * the message bus launching an owner (unless
72 * %G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START is set).
74 * The generic #GDBusProxy::g-properties-changed and
75 * #GDBusProxy::g-signal signals are not very convenient to work with.
76 * Therefore, the recommended way of working with proxies is to subclass
77 * #GDBusProxy, and have more natural properties and signals in your derived
78 * class. This [example][gdbus-example-gdbus-codegen] shows how this can
79 * easily be done using the [gdbus-codegen][gdbus-codegen] tool.
81 * A #GDBusProxy instance can be used from multiple threads but note
82 * that all signals (e.g. #GDBusProxy::g-signal, #GDBusProxy::g-properties-changed
83 * and #GObject::notify) are emitted in the
84 * [thread-default main context][g-main-context-push-thread-default]
85 * of the thread where the instance was constructed.
87 * An example using a proxy for a well-known name can be found in
88 * [gdbus-example-watch-proxy.c](https://git.gnome.org/browse/glib/tree/gio/tests/gdbus-example-watch-proxy.c)
91 /* lock protecting the mutable properties: name_owner, timeout_msec,
92 * expected_interface, and the properties hash table
94 G_LOCK_DEFINE_STATIC (properties_lock);
96 /* ---------------------------------------------------------------------------------------------------- */
98 G_LOCK_DEFINE_STATIC (signal_subscription_lock);
102 volatile gint ref_count;
104 } SignalSubscriptionData;
106 static SignalSubscriptionData *
107 signal_subscription_ref (SignalSubscriptionData *data)
109 g_atomic_int_inc (&data->ref_count);
114 signal_subscription_unref (SignalSubscriptionData *data)
116 if (g_atomic_int_dec_and_test (&data->ref_count))
118 g_slice_free (SignalSubscriptionData, data);
122 /* ---------------------------------------------------------------------------------------------------- */
124 struct _GDBusProxyPrivate
127 GDBusProxyFlags flags;
128 GDBusConnection *connection;
131 /* mutable, protected by properties_lock */
134 gchar *interface_name;
135 /* mutable, protected by properties_lock */
138 guint name_owner_changed_subscription_id;
140 GCancellable *get_all_cancellable;
142 /* gchar* -> GVariant*, protected by properties_lock */
143 GHashTable *properties;
145 /* mutable, protected by properties_lock */
146 GDBusInterfaceInfo *expected_interface;
148 guint properties_changed_subscription_id;
149 guint signals_subscription_id;
151 gboolean initialized;
153 /* mutable, protected by properties_lock */
156 SignalSubscriptionData *signal_subscription_data;
168 PROP_G_INTERFACE_NAME,
169 PROP_G_DEFAULT_TIMEOUT,
170 PROP_G_INTERFACE_INFO
175 PROPERTIES_CHANGED_SIGNAL,
180 static guint signals[LAST_SIGNAL] = {0};
182 static void dbus_interface_iface_init (GDBusInterfaceIface *dbus_interface_iface);
183 static void initable_iface_init (GInitableIface *initable_iface);
184 static void async_initable_iface_init (GAsyncInitableIface *async_initable_iface);
186 G_DEFINE_TYPE_WITH_CODE (GDBusProxy, g_dbus_proxy, G_TYPE_OBJECT,
187 G_ADD_PRIVATE (GDBusProxy)
188 G_IMPLEMENT_INTERFACE (G_TYPE_DBUS_INTERFACE, dbus_interface_iface_init)
189 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, initable_iface_init)
190 G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE, async_initable_iface_init))
193 g_dbus_proxy_dispose (GObject *object)
195 GDBusProxy *proxy = G_DBUS_PROXY (object);
196 G_LOCK (signal_subscription_lock);
197 if (proxy->priv->signal_subscription_data != NULL)
199 proxy->priv->signal_subscription_data->proxy = NULL;
200 signal_subscription_unref (proxy->priv->signal_subscription_data);
201 proxy->priv->signal_subscription_data = NULL;
203 G_UNLOCK (signal_subscription_lock);
205 G_OBJECT_CLASS (g_dbus_proxy_parent_class)->dispose (object);
209 g_dbus_proxy_finalize (GObject *object)
211 GDBusProxy *proxy = G_DBUS_PROXY (object);
213 g_warn_if_fail (proxy->priv->get_all_cancellable == NULL);
215 if (proxy->priv->name_owner_changed_subscription_id > 0)
216 g_dbus_connection_signal_unsubscribe (proxy->priv->connection,
217 proxy->priv->name_owner_changed_subscription_id);
219 if (proxy->priv->properties_changed_subscription_id > 0)
220 g_dbus_connection_signal_unsubscribe (proxy->priv->connection,
221 proxy->priv->properties_changed_subscription_id);
223 if (proxy->priv->signals_subscription_id > 0)
224 g_dbus_connection_signal_unsubscribe (proxy->priv->connection,
225 proxy->priv->signals_subscription_id);
227 if (proxy->priv->connection != NULL)
228 g_object_unref (proxy->priv->connection);
229 g_free (proxy->priv->name);
230 g_free (proxy->priv->name_owner);
231 g_free (proxy->priv->object_path);
232 g_free (proxy->priv->interface_name);
233 if (proxy->priv->properties != NULL)
234 g_hash_table_unref (proxy->priv->properties);
236 if (proxy->priv->expected_interface != NULL)
238 g_dbus_interface_info_cache_release (proxy->priv->expected_interface);
239 g_dbus_interface_info_unref (proxy->priv->expected_interface);
242 if (proxy->priv->object != NULL)
243 g_object_remove_weak_pointer (G_OBJECT (proxy->priv->object), (gpointer *) &proxy->priv->object);
245 G_OBJECT_CLASS (g_dbus_proxy_parent_class)->finalize (object);
249 g_dbus_proxy_get_property (GObject *object,
254 GDBusProxy *proxy = G_DBUS_PROXY (object);
258 case PROP_G_CONNECTION:
259 g_value_set_object (value, proxy->priv->connection);
263 g_value_set_flags (value, proxy->priv->flags);
267 g_value_set_string (value, proxy->priv->name);
270 case PROP_G_NAME_OWNER:
271 g_value_take_string (value, g_dbus_proxy_get_name_owner (proxy));
274 case PROP_G_OBJECT_PATH:
275 g_value_set_string (value, proxy->priv->object_path);
278 case PROP_G_INTERFACE_NAME:
279 g_value_set_string (value, proxy->priv->interface_name);
282 case PROP_G_DEFAULT_TIMEOUT:
283 g_value_set_int (value, g_dbus_proxy_get_default_timeout (proxy));
286 case PROP_G_INTERFACE_INFO:
287 g_value_set_boxed (value, g_dbus_proxy_get_interface_info (proxy));
291 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
297 g_dbus_proxy_set_property (GObject *object,
302 GDBusProxy *proxy = G_DBUS_PROXY (object);
306 case PROP_G_CONNECTION:
307 proxy->priv->connection = g_value_dup_object (value);
311 proxy->priv->flags = g_value_get_flags (value);
315 proxy->priv->name = g_value_dup_string (value);
318 case PROP_G_OBJECT_PATH:
319 proxy->priv->object_path = g_value_dup_string (value);
322 case PROP_G_INTERFACE_NAME:
323 proxy->priv->interface_name = g_value_dup_string (value);
326 case PROP_G_DEFAULT_TIMEOUT:
327 g_dbus_proxy_set_default_timeout (proxy, g_value_get_int (value));
330 case PROP_G_INTERFACE_INFO:
331 g_dbus_proxy_set_interface_info (proxy, g_value_get_boxed (value));
334 case PROP_G_BUS_TYPE:
335 proxy->priv->bus_type = g_value_get_enum (value);
339 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
345 g_dbus_proxy_class_init (GDBusProxyClass *klass)
347 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
349 gobject_class->dispose = g_dbus_proxy_dispose;
350 gobject_class->finalize = g_dbus_proxy_finalize;
351 gobject_class->set_property = g_dbus_proxy_set_property;
352 gobject_class->get_property = g_dbus_proxy_get_property;
354 /* Note that all property names are prefixed to avoid collisions with D-Bus property names
355 * in derived classes */
358 * GDBusProxy:g-interface-info:
360 * Ensure that interactions with this proxy conform to the given
361 * interface. This is mainly to ensure that malformed data received
362 * from the other peer is ignored. The given #GDBusInterfaceInfo is
363 * said to be the "expected interface".
365 * The checks performed are:
366 * - When completing a method call, if the type signature of
367 * the reply message isn't what's expected, the reply is
368 * discarded and the #GError is set to %G_IO_ERROR_INVALID_ARGUMENT.
370 * - Received signals that have a type signature mismatch are dropped and
371 * a warning is logged via g_warning().
373 * - Properties received via the initial `GetAll()` call or via the
374 * `::PropertiesChanged` signal (on the
375 * [org.freedesktop.DBus.Properties](http://dbus.freedesktop.org/doc/dbus-specification.html#standard-interfaces-properties)
376 * interface) or set using g_dbus_proxy_set_cached_property()
377 * with a type signature mismatch are ignored and a warning is
378 * logged via g_warning().
380 * Note that these checks are never done on methods, signals and
381 * properties that are not referenced in the given
382 * #GDBusInterfaceInfo, since extending a D-Bus interface on the
383 * service-side is not considered an ABI break.
387 g_object_class_install_property (gobject_class,
388 PROP_G_INTERFACE_INFO,
389 g_param_spec_boxed ("g-interface-info",
390 P_("Interface Information"),
391 P_("Interface Information"),
392 G_TYPE_DBUS_INTERFACE_INFO,
395 G_PARAM_STATIC_NAME |
396 G_PARAM_STATIC_BLURB |
397 G_PARAM_STATIC_NICK));
400 * GDBusProxy:g-connection:
402 * The #GDBusConnection the proxy is for.
406 g_object_class_install_property (gobject_class,
408 g_param_spec_object ("g-connection",
410 P_("The connection the proxy is for"),
411 G_TYPE_DBUS_CONNECTION,
414 G_PARAM_CONSTRUCT_ONLY |
415 G_PARAM_STATIC_NAME |
416 G_PARAM_STATIC_BLURB |
417 G_PARAM_STATIC_NICK));
420 * GDBusProxy:g-bus-type:
422 * If this property is not %G_BUS_TYPE_NONE, then
423 * #GDBusProxy:g-connection must be %NULL and will be set to the
424 * #GDBusConnection obtained by calling g_bus_get() with the value
429 g_object_class_install_property (gobject_class,
431 g_param_spec_enum ("g-bus-type",
433 P_("The bus to connect to, if any"),
437 G_PARAM_CONSTRUCT_ONLY |
438 G_PARAM_STATIC_NAME |
439 G_PARAM_STATIC_BLURB |
440 G_PARAM_STATIC_NICK));
443 * GDBusProxy:g-flags:
445 * Flags from the #GDBusProxyFlags enumeration.
449 g_object_class_install_property (gobject_class,
451 g_param_spec_flags ("g-flags",
453 P_("Flags for the proxy"),
454 G_TYPE_DBUS_PROXY_FLAGS,
455 G_DBUS_PROXY_FLAGS_NONE,
458 G_PARAM_CONSTRUCT_ONLY |
459 G_PARAM_STATIC_NAME |
460 G_PARAM_STATIC_BLURB |
461 G_PARAM_STATIC_NICK));
466 * The well-known or unique name that the proxy is for.
470 g_object_class_install_property (gobject_class,
472 g_param_spec_string ("g-name",
474 P_("The well-known or unique name that the proxy is for"),
478 G_PARAM_CONSTRUCT_ONLY |
479 G_PARAM_STATIC_NAME |
480 G_PARAM_STATIC_BLURB |
481 G_PARAM_STATIC_NICK));
484 * GDBusProxy:g-name-owner:
486 * The unique name that owns #GDBusProxy:g-name or %NULL if no-one
487 * currently owns that name. You may connect to #GObject::notify signal to
488 * track changes to this property.
492 g_object_class_install_property (gobject_class,
494 g_param_spec_string ("g-name-owner",
496 P_("The unique name for the owner"),
499 G_PARAM_STATIC_NAME |
500 G_PARAM_STATIC_BLURB |
501 G_PARAM_STATIC_NICK));
504 * GDBusProxy:g-object-path:
506 * The object path the proxy is for.
510 g_object_class_install_property (gobject_class,
512 g_param_spec_string ("g-object-path",
514 P_("The object path the proxy is for"),
518 G_PARAM_CONSTRUCT_ONLY |
519 G_PARAM_STATIC_NAME |
520 G_PARAM_STATIC_BLURB |
521 G_PARAM_STATIC_NICK));
524 * GDBusProxy:g-interface-name:
526 * The D-Bus interface name the proxy is for.
530 g_object_class_install_property (gobject_class,
531 PROP_G_INTERFACE_NAME,
532 g_param_spec_string ("g-interface-name",
533 P_("g-interface-name"),
534 P_("The D-Bus interface name the proxy is for"),
538 G_PARAM_CONSTRUCT_ONLY |
539 G_PARAM_STATIC_NAME |
540 G_PARAM_STATIC_BLURB |
541 G_PARAM_STATIC_NICK));
544 * GDBusProxy:g-default-timeout:
546 * The timeout to use if -1 (specifying default timeout) is passed
547 * as @timeout_msec in the g_dbus_proxy_call() and
548 * g_dbus_proxy_call_sync() functions.
550 * This allows applications to set a proxy-wide timeout for all
551 * remote method invocations on the proxy. If this property is -1,
552 * the default timeout (typically 25 seconds) is used. If set to
553 * %G_MAXINT, then no timeout is used.
557 g_object_class_install_property (gobject_class,
558 PROP_G_DEFAULT_TIMEOUT,
559 g_param_spec_int ("g-default-timeout",
560 P_("Default Timeout"),
561 P_("Timeout for remote method invocation"),
568 G_PARAM_STATIC_NAME |
569 G_PARAM_STATIC_BLURB |
570 G_PARAM_STATIC_NICK));
573 * GDBusProxy::g-properties-changed:
574 * @proxy: The #GDBusProxy emitting the signal.
575 * @changed_properties: A #GVariant containing the properties that changed
576 * @invalidated_properties: A %NULL terminated array of properties that was invalidated
578 * Emitted when one or more D-Bus properties on @proxy changes. The
579 * local cache has already been updated when this signal fires. Note
580 * that both @changed_properties and @invalidated_properties are
581 * guaranteed to never be %NULL (either may be empty though).
583 * If the proxy has the flag
584 * %G_DBUS_PROXY_FLAGS_GET_INVALIDATED_PROPERTIES set, then
585 * @invalidated_properties will always be empty.
587 * This signal corresponds to the
588 * `PropertiesChanged` D-Bus signal on the
589 * `org.freedesktop.DBus.Properties` interface.
593 signals[PROPERTIES_CHANGED_SIGNAL] = g_signal_new ("g-properties-changed",
595 G_SIGNAL_RUN_LAST | G_SIGNAL_MUST_COLLECT,
596 G_STRUCT_OFFSET (GDBusProxyClass, g_properties_changed),
603 G_TYPE_STRV | G_SIGNAL_TYPE_STATIC_SCOPE);
606 * GDBusProxy::g-signal:
607 * @proxy: The #GDBusProxy emitting the signal.
608 * @sender_name: (allow-none): The sender of the signal or %NULL if the connection is not a bus connection.
609 * @signal_name: The name of the signal.
610 * @parameters: A #GVariant tuple with parameters for the signal.
612 * Emitted when a signal from the remote object and interface that @proxy is for, has been received.
616 signals[SIGNAL_SIGNAL] = g_signal_new ("g-signal",
618 G_SIGNAL_RUN_LAST | G_SIGNAL_MUST_COLLECT,
619 G_STRUCT_OFFSET (GDBusProxyClass, g_signal),
632 g_dbus_proxy_init (GDBusProxy *proxy)
634 proxy->priv = g_dbus_proxy_get_instance_private (proxy);
635 proxy->priv->signal_subscription_data = g_slice_new0 (SignalSubscriptionData);
636 proxy->priv->signal_subscription_data->ref_count = 1;
637 proxy->priv->signal_subscription_data->proxy = proxy;
638 proxy->priv->properties = g_hash_table_new_full (g_str_hash,
641 (GDestroyNotify) g_variant_unref);
644 /* ---------------------------------------------------------------------------------------------------- */
647 property_name_sort_func (const gchar **a,
650 return g_strcmp0 (*a, *b);
654 * g_dbus_proxy_get_cached_property_names:
655 * @proxy: A #GDBusProxy.
657 * Gets the names of all cached properties on @proxy.
659 * Returns: (transfer full): A %NULL-terminated array of strings or %NULL if
660 * @proxy has no cached properties. Free the returned array with
666 g_dbus_proxy_get_cached_property_names (GDBusProxy *proxy)
673 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
675 G_LOCK (properties_lock);
678 if (g_hash_table_size (proxy->priv->properties) == 0)
681 p = g_ptr_array_new ();
683 g_hash_table_iter_init (&iter, proxy->priv->properties);
684 while (g_hash_table_iter_next (&iter, (gpointer) &key, NULL))
685 g_ptr_array_add (p, g_strdup (key));
686 g_ptr_array_sort (p, (GCompareFunc) property_name_sort_func);
687 g_ptr_array_add (p, NULL);
689 names = (gchar **) g_ptr_array_free (p, FALSE);
692 G_UNLOCK (properties_lock);
696 /* properties_lock must be held for as long as you will keep the
699 static const GDBusPropertyInfo *
700 lookup_property_info (GDBusProxy *proxy,
701 const gchar *property_name)
703 const GDBusPropertyInfo *info = NULL;
705 if (proxy->priv->expected_interface == NULL)
708 info = g_dbus_interface_info_lookup_property (proxy->priv->expected_interface, property_name);
715 * g_dbus_proxy_get_cached_property:
716 * @proxy: A #GDBusProxy.
717 * @property_name: Property name.
719 * Looks up the value for a property from the cache. This call does no
722 * If @proxy has an expected interface (see
723 * #GDBusProxy:g-interface-info) and @property_name is referenced by
724 * it, then @value is checked against the type of the property.
726 * Returns: A reference to the #GVariant instance that holds the value
727 * for @property_name or %NULL if the value is not in the cache. The
728 * returned reference must be freed with g_variant_unref().
733 g_dbus_proxy_get_cached_property (GDBusProxy *proxy,
734 const gchar *property_name)
736 const GDBusPropertyInfo *info;
739 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
740 g_return_val_if_fail (property_name != NULL, NULL);
742 G_LOCK (properties_lock);
744 value = g_hash_table_lookup (proxy->priv->properties, property_name);
748 info = lookup_property_info (proxy, property_name);
751 const gchar *type_string = g_variant_get_type_string (value);
752 if (g_strcmp0 (type_string, info->signature) != 0)
754 g_warning ("Trying to get property %s with type %s but according to the expected "
755 "interface the type is %s",
764 g_variant_ref (value);
767 G_UNLOCK (properties_lock);
772 * g_dbus_proxy_set_cached_property:
773 * @proxy: A #GDBusProxy
774 * @property_name: Property name.
775 * @value: (allow-none): Value for the property or %NULL to remove it from the cache.
777 * If @value is not %NULL, sets the cached value for the property with
778 * name @property_name to the value in @value.
780 * If @value is %NULL, then the cached value is removed from the
783 * If @proxy has an expected interface (see
784 * #GDBusProxy:g-interface-info) and @property_name is referenced by
785 * it, then @value is checked against the type of the property.
787 * If the @value #GVariant is floating, it is consumed. This allows
788 * convenient 'inline' use of g_variant_new(), e.g.
789 * |[<!-- language="C" -->
790 * g_dbus_proxy_set_cached_property (proxy,
792 * g_variant_new ("(si)",
797 * Normally you will not need to use this method since @proxy
798 * is tracking changes using the
799 * `org.freedesktop.DBus.Properties.PropertiesChanged`
800 * D-Bus signal. However, for performance reasons an object may
801 * decide to not use this signal for some properties and instead
802 * use a proprietary out-of-band mechanism to transmit changes.
804 * As a concrete example, consider an object with a property
805 * `ChatroomParticipants` which is an array of strings. Instead of
806 * transmitting the same (long) array every time the property changes,
807 * it is more efficient to only transmit the delta using e.g. signals
808 * `ChatroomParticipantJoined(String name)` and
809 * `ChatroomParticipantParted(String name)`.
814 g_dbus_proxy_set_cached_property (GDBusProxy *proxy,
815 const gchar *property_name,
818 const GDBusPropertyInfo *info;
820 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
821 g_return_if_fail (property_name != NULL);
823 G_LOCK (properties_lock);
827 info = lookup_property_info (proxy, property_name);
830 if (g_strcmp0 (info->signature, g_variant_get_type_string (value)) != 0)
832 g_warning ("Trying to set property %s of type %s but according to the expected "
833 "interface the type is %s",
835 g_variant_get_type_string (value),
840 g_hash_table_insert (proxy->priv->properties,
841 g_strdup (property_name),
842 g_variant_ref_sink (value));
846 g_hash_table_remove (proxy->priv->properties, property_name);
850 G_UNLOCK (properties_lock);
853 /* ---------------------------------------------------------------------------------------------------- */
856 on_signal_received (GDBusConnection *connection,
857 const gchar *sender_name,
858 const gchar *object_path,
859 const gchar *interface_name,
860 const gchar *signal_name,
861 GVariant *parameters,
864 SignalSubscriptionData *data = user_data;
867 G_LOCK (signal_subscription_lock);
871 G_UNLOCK (signal_subscription_lock);
876 g_object_ref (proxy);
877 G_UNLOCK (signal_subscription_lock);
880 if (!proxy->priv->initialized)
883 G_LOCK (properties_lock);
885 if (proxy->priv->name_owner != NULL && g_strcmp0 (sender_name, proxy->priv->name_owner) != 0)
887 G_UNLOCK (properties_lock);
891 if (proxy->priv->expected_interface != NULL)
893 const GDBusSignalInfo *info;
894 info = g_dbus_interface_info_lookup_signal (proxy->priv->expected_interface, signal_name);
897 GVariantType *expected_type;
898 expected_type = _g_dbus_compute_complete_signature (info->args);
899 if (!g_variant_type_equal (expected_type, g_variant_get_type (parameters)))
901 gchar *expected_type_string = g_variant_type_dup_string (expected_type);
902 g_warning ("Dropping signal %s of type %s since the type from the expected interface is %s",
904 g_variant_get_type_string (parameters),
905 expected_type_string);
906 g_free (expected_type_string);
907 g_variant_type_free (expected_type);
908 G_UNLOCK (properties_lock);
911 g_variant_type_free (expected_type);
915 G_UNLOCK (properties_lock);
917 g_signal_emit (proxy,
918 signals[SIGNAL_SIGNAL],
926 g_object_unref (proxy);
929 /* ---------------------------------------------------------------------------------------------------- */
931 /* must hold properties_lock */
933 insert_property_checked (GDBusProxy *proxy,
934 gchar *property_name,
937 if (proxy->priv->expected_interface != NULL)
939 const GDBusPropertyInfo *info;
940 info = g_dbus_interface_info_lookup_property (proxy->priv->expected_interface, property_name);
941 /* Only check known properties */
944 /* Warn about properties with the wrong type */
945 if (g_strcmp0 (info->signature, g_variant_get_type_string (value)) != 0)
947 g_warning ("Received property %s with type %s does not match expected type "
948 "%s in the expected interface",
950 g_variant_get_type_string (value),
957 g_hash_table_insert (proxy->priv->properties,
958 property_name, /* adopts string */
959 value); /* adopts value */
964 g_variant_unref (value);
965 g_free (property_name);
972 } InvalidatedPropGetData;
975 invalidated_property_get_cb (GDBusConnection *connection,
979 InvalidatedPropGetData *data = user_data;
980 const gchar *invalidated_properties[] = {NULL};
981 GVariantBuilder builder;
982 GVariant *value = NULL;
983 GVariant *unpacked_value = NULL;
985 /* errors are fine, the other end could have disconnected */
986 value = g_dbus_connection_call_finish (connection, res, NULL);
992 if (!g_variant_is_of_type (value, G_VARIANT_TYPE ("(v)")))
994 g_warning ("Expected type '(v)' for Get() reply, got '%s'", g_variant_get_type_string (value));
998 g_variant_get (value, "(v)", &unpacked_value);
1000 /* synthesize the a{sv} in the PropertiesChanged signal */
1001 g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
1002 g_variant_builder_add (&builder, "{sv}", data->prop_name, unpacked_value);
1004 G_LOCK (properties_lock);
1005 insert_property_checked (data->proxy,
1006 data->prop_name, /* adopts string */
1007 unpacked_value); /* adopts value */
1008 data->prop_name = NULL;
1009 G_UNLOCK (properties_lock);
1011 g_signal_emit (data->proxy,
1012 signals[PROPERTIES_CHANGED_SIGNAL], 0,
1013 g_variant_builder_end (&builder), /* consumed */
1014 invalidated_properties);
1019 g_variant_unref (value);
1020 g_object_unref (data->proxy);
1021 g_free (data->prop_name);
1022 g_slice_free (InvalidatedPropGetData, data);
1026 on_properties_changed (GDBusConnection *connection,
1027 const gchar *sender_name,
1028 const gchar *object_path,
1029 const gchar *interface_name,
1030 const gchar *signal_name,
1031 GVariant *parameters,
1034 SignalSubscriptionData *data = user_data;
1035 gboolean emit_g_signal = FALSE;
1037 const gchar *interface_name_for_signal;
1038 GVariant *changed_properties;
1039 gchar **invalidated_properties;
1045 changed_properties = NULL;
1046 invalidated_properties = NULL;
1048 G_LOCK (signal_subscription_lock);
1049 proxy = data->proxy;
1052 G_UNLOCK (signal_subscription_lock);
1057 g_object_ref (proxy);
1058 G_UNLOCK (signal_subscription_lock);
1061 if (!proxy->priv->initialized)
1064 G_LOCK (properties_lock);
1066 if (proxy->priv->name_owner != NULL && g_strcmp0 (sender_name, proxy->priv->name_owner) != 0)
1068 G_UNLOCK (properties_lock);
1072 if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(sa{sv}as)")))
1074 g_warning ("Value for PropertiesChanged signal with type '%s' does not match '(sa{sv}as)'",
1075 g_variant_get_type_string (parameters));
1076 G_UNLOCK (properties_lock);
1080 g_variant_get (parameters,
1082 &interface_name_for_signal,
1083 &changed_properties,
1084 &invalidated_properties);
1086 if (g_strcmp0 (interface_name_for_signal, proxy->priv->interface_name) != 0)
1088 G_UNLOCK (properties_lock);
1092 g_variant_iter_init (&iter, changed_properties);
1093 while (g_variant_iter_next (&iter, "{sv}", &key, &value))
1095 insert_property_checked (proxy,
1096 key, /* adopts string */
1097 value); /* adopts value */
1098 emit_g_signal = TRUE;
1101 if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_GET_INVALIDATED_PROPERTIES)
1103 if (proxy->priv->name_owner != NULL)
1105 for (n = 0; invalidated_properties[n] != NULL; n++)
1107 InvalidatedPropGetData *data;
1108 data = g_slice_new0 (InvalidatedPropGetData);
1109 data->proxy = g_object_ref (proxy);
1110 data->prop_name = g_strdup (invalidated_properties[n]);
1111 g_dbus_connection_call (proxy->priv->connection,
1112 proxy->priv->name_owner,
1113 proxy->priv->object_path,
1114 "org.freedesktop.DBus.Properties",
1116 g_variant_new ("(ss)", proxy->priv->interface_name, data->prop_name),
1117 G_VARIANT_TYPE ("(v)"),
1118 G_DBUS_CALL_FLAGS_NONE,
1120 NULL, /* GCancellable */
1121 (GAsyncReadyCallback) invalidated_property_get_cb,
1128 emit_g_signal = TRUE;
1129 for (n = 0; invalidated_properties[n] != NULL; n++)
1131 g_hash_table_remove (proxy->priv->properties, invalidated_properties[n]);
1135 G_UNLOCK (properties_lock);
1139 g_signal_emit (proxy, signals[PROPERTIES_CHANGED_SIGNAL],
1142 invalidated_properties);
1146 if (changed_properties != NULL)
1147 g_variant_unref (changed_properties);
1148 g_free (invalidated_properties);
1150 g_object_unref (proxy);
1153 /* ---------------------------------------------------------------------------------------------------- */
1156 process_get_all_reply (GDBusProxy *proxy,
1162 guint num_properties;
1164 if (!g_variant_is_of_type (result, G_VARIANT_TYPE ("(a{sv})")))
1166 g_warning ("Value for GetAll reply with type '%s' does not match '(a{sv})'",
1167 g_variant_get_type_string (result));
1171 G_LOCK (properties_lock);
1173 g_variant_get (result, "(a{sv})", &iter);
1174 while (g_variant_iter_next (iter, "{sv}", &key, &value))
1176 insert_property_checked (proxy,
1177 key, /* adopts string */
1178 value); /* adopts value */
1180 g_variant_iter_free (iter);
1182 num_properties = g_hash_table_size (proxy->priv->properties);
1183 G_UNLOCK (properties_lock);
1185 /* Synthesize ::g-properties-changed changed */
1186 if (num_properties > 0)
1188 GVariant *changed_properties;
1189 const gchar *invalidated_properties[1] = {NULL};
1191 g_variant_get (result,
1193 &changed_properties);
1194 g_signal_emit (proxy, signals[PROPERTIES_CHANGED_SIGNAL],
1197 invalidated_properties);
1198 g_variant_unref (changed_properties);
1208 GCancellable *cancellable;
1210 } LoadPropertiesOnNameOwnerChangedData;
1213 on_name_owner_changed_get_all_cb (GDBusConnection *connection,
1217 LoadPropertiesOnNameOwnerChangedData *data = user_data;
1225 result = g_dbus_connection_call_finish (connection,
1230 if (error->domain == G_IO_ERROR && error->code == G_IO_ERROR_CANCELLED)
1232 /* We just ignore if GetAll() is failing. Because this might happen
1233 * if the object has no properties at all. Or if the caller is
1234 * not authorized to see the properties.
1236 * Either way, apps can know about this by using
1237 * get_cached_property_names() or get_cached_property().
1239 * TODO: handle G_DBUS_DEBUG flag 'proxy' and, if enabled, log the
1240 * fact that GetAll() failed
1242 //g_debug ("error: %d %d %s", error->domain, error->code, error->message);
1243 g_error_free (error);
1246 /* and finally we can notify */
1249 G_LOCK (properties_lock);
1250 g_free (data->proxy->priv->name_owner);
1251 data->proxy->priv->name_owner = data->name_owner;
1252 data->name_owner = NULL; /* to avoid an extra copy, we steal the string */
1253 g_hash_table_remove_all (data->proxy->priv->properties);
1254 G_UNLOCK (properties_lock);
1257 process_get_all_reply (data->proxy, result);
1258 g_variant_unref (result);
1261 g_object_notify (G_OBJECT (data->proxy), "g-name-owner");
1264 if (data->cancellable == data->proxy->priv->get_all_cancellable)
1265 data->proxy->priv->get_all_cancellable = NULL;
1267 g_object_unref (data->proxy);
1268 g_object_unref (data->cancellable);
1269 g_free (data->name_owner);
1274 on_name_owner_changed (GDBusConnection *connection,
1275 const gchar *sender_name,
1276 const gchar *object_path,
1277 const gchar *interface_name,
1278 const gchar *signal_name,
1279 GVariant *parameters,
1282 SignalSubscriptionData *data = user_data;
1284 const gchar *old_owner;
1285 const gchar *new_owner;
1287 G_LOCK (signal_subscription_lock);
1288 proxy = data->proxy;
1291 G_UNLOCK (signal_subscription_lock);
1296 g_object_ref (proxy);
1297 G_UNLOCK (signal_subscription_lock);
1300 /* if we are already trying to load properties, cancel that */
1301 if (proxy->priv->get_all_cancellable != NULL)
1303 g_cancellable_cancel (proxy->priv->get_all_cancellable);
1304 proxy->priv->get_all_cancellable = NULL;
1307 g_variant_get (parameters,
1313 if (strlen (new_owner) == 0)
1315 G_LOCK (properties_lock);
1316 g_free (proxy->priv->name_owner);
1317 proxy->priv->name_owner = NULL;
1319 /* Synthesize ::g-properties-changed changed */
1320 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES) &&
1321 g_hash_table_size (proxy->priv->properties) > 0)
1323 GVariantBuilder builder;
1324 GPtrArray *invalidated_properties;
1325 GHashTableIter iter;
1328 /* Build changed_properties (always empty) and invalidated_properties ... */
1329 g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
1331 invalidated_properties = g_ptr_array_new_with_free_func (g_free);
1332 g_hash_table_iter_init (&iter, proxy->priv->properties);
1333 while (g_hash_table_iter_next (&iter, (gpointer) &key, NULL))
1334 g_ptr_array_add (invalidated_properties, g_strdup (key));
1335 g_ptr_array_add (invalidated_properties, NULL);
1337 /* ... throw out the properties ... */
1338 g_hash_table_remove_all (proxy->priv->properties);
1340 G_UNLOCK (properties_lock);
1342 /* ... and finally emit the ::g-properties-changed signal */
1343 g_signal_emit (proxy, signals[PROPERTIES_CHANGED_SIGNAL],
1345 g_variant_builder_end (&builder) /* consumed */,
1346 (const gchar* const *) invalidated_properties->pdata);
1347 g_ptr_array_unref (invalidated_properties);
1351 G_UNLOCK (properties_lock);
1353 g_object_notify (G_OBJECT (proxy), "g-name-owner");
1357 G_LOCK (properties_lock);
1359 /* ignore duplicates - this can happen when activating the service */
1360 if (g_strcmp0 (new_owner, proxy->priv->name_owner) == 0)
1362 G_UNLOCK (properties_lock);
1366 if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES)
1368 g_free (proxy->priv->name_owner);
1369 proxy->priv->name_owner = g_strdup (new_owner);
1371 g_hash_table_remove_all (proxy->priv->properties);
1372 G_UNLOCK (properties_lock);
1373 g_object_notify (G_OBJECT (proxy), "g-name-owner");
1377 LoadPropertiesOnNameOwnerChangedData *data;
1379 G_UNLOCK (properties_lock);
1381 /* start loading properties.. only then emit notify::g-name-owner .. we
1382 * need to be able to cancel this in the event another NameOwnerChanged
1383 * signal suddenly happens
1386 g_assert (proxy->priv->get_all_cancellable == NULL);
1387 proxy->priv->get_all_cancellable = g_cancellable_new ();
1388 data = g_new0 (LoadPropertiesOnNameOwnerChangedData, 1);
1389 data->proxy = g_object_ref (proxy);
1390 data->cancellable = proxy->priv->get_all_cancellable;
1391 data->name_owner = g_strdup (new_owner);
1392 g_dbus_connection_call (proxy->priv->connection,
1394 proxy->priv->object_path,
1395 "org.freedesktop.DBus.Properties",
1397 g_variant_new ("(s)", proxy->priv->interface_name),
1398 G_VARIANT_TYPE ("(a{sv})"),
1399 G_DBUS_CALL_FLAGS_NONE,
1401 proxy->priv->get_all_cancellable,
1402 (GAsyncReadyCallback) on_name_owner_changed_get_all_cb,
1409 g_object_unref (proxy);
1412 /* ---------------------------------------------------------------------------------------------------- */
1417 GCancellable *cancellable;
1418 GSimpleAsyncResult *simple;
1422 async_init_data_free (AsyncInitData *data)
1424 g_object_unref (data->proxy);
1425 if (data->cancellable != NULL)
1426 g_object_unref (data->cancellable);
1427 g_object_unref (data->simple);
1432 async_init_get_all_cb (GDBusConnection *connection,
1436 AsyncInitData *data = user_data;
1441 result = g_dbus_connection_call_finish (connection,
1446 /* We just ignore if GetAll() is failing. Because this might happen
1447 * if the object has no properties at all. Or if the caller is
1448 * not authorized to see the properties.
1450 * Either way, apps can know about this by using
1451 * get_cached_property_names() or get_cached_property().
1453 * TODO: handle G_DBUS_DEBUG flag 'proxy' and, if enabled, log the
1454 * fact that GetAll() failed
1456 //g_debug ("error: %d %d %s", error->domain, error->code, error->message);
1457 g_error_free (error);
1461 g_simple_async_result_set_op_res_gpointer (data->simple,
1463 (GDestroyNotify) g_variant_unref);
1466 g_simple_async_result_complete_in_idle (data->simple);
1467 async_init_data_free (data);
1471 async_init_data_set_name_owner (AsyncInitData *data,
1472 const gchar *name_owner)
1477 if (name_owner != NULL)
1479 /* it starts as NULL anyway */
1480 G_LOCK (properties_lock);
1481 data->proxy->priv->name_owner = g_strdup (name_owner);
1482 G_UNLOCK (properties_lock);
1487 if (data->proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES)
1489 /* Don't load properties if the API user doesn't want them */
1492 else if (name_owner == NULL && data->proxy->priv->name != NULL)
1494 /* Don't attempt to load properties if the name_owner is NULL (which
1495 * usually means the name isn't owned), unless name is also NULL (which
1496 * means we actually wanted to talk to the directly-connected process -
1497 * either dbus-daemon or a peer - instead of going via dbus-daemon)
1504 /* load all properties asynchronously */
1505 g_dbus_connection_call (data->proxy->priv->connection,
1507 data->proxy->priv->object_path,
1508 "org.freedesktop.DBus.Properties",
1510 g_variant_new ("(s)", data->proxy->priv->interface_name),
1511 G_VARIANT_TYPE ("(a{sv})"),
1512 G_DBUS_CALL_FLAGS_NONE,
1515 (GAsyncReadyCallback) async_init_get_all_cb,
1520 g_simple_async_result_complete_in_idle (data->simple);
1521 async_init_data_free (data);
1526 async_init_get_name_owner_cb (GDBusConnection *connection,
1530 AsyncInitData *data = user_data;
1535 result = g_dbus_connection_call_finish (connection,
1540 if (error->domain == G_DBUS_ERROR &&
1541 error->code == G_DBUS_ERROR_NAME_HAS_NO_OWNER)
1543 g_error_free (error);
1544 async_init_data_set_name_owner (data, NULL);
1548 g_simple_async_result_take_error (data->simple, error);
1549 g_simple_async_result_complete_in_idle (data->simple);
1550 async_init_data_free (data);
1555 /* borrowed from result to avoid an extra copy */
1556 const gchar *name_owner;
1558 g_variant_get (result, "(&s)", &name_owner);
1559 async_init_data_set_name_owner (data, name_owner);
1560 g_variant_unref (result);
1565 async_init_call_get_name_owner (AsyncInitData *data)
1567 g_dbus_connection_call (data->proxy->priv->connection,
1568 "org.freedesktop.DBus", /* name */
1569 "/org/freedesktop/DBus", /* object path */
1570 "org.freedesktop.DBus", /* interface */
1572 g_variant_new ("(s)",
1573 data->proxy->priv->name),
1574 G_VARIANT_TYPE ("(s)"),
1575 G_DBUS_CALL_FLAGS_NONE,
1578 (GAsyncReadyCallback) async_init_get_name_owner_cb,
1583 async_init_start_service_by_name_cb (GDBusConnection *connection,
1587 AsyncInitData *data = user_data;
1592 result = g_dbus_connection_call_finish (connection,
1597 /* Errors are not unexpected; the bus will reply e.g.
1599 * org.freedesktop.DBus.Error.ServiceUnknown: The name org.gnome.Epiphany2
1600 * was not provided by any .service files
1604 * org.freedesktop.systemd1.Masked: Unit polkit.service is masked.
1606 * This doesn't mean that the name doesn't have an owner, just
1607 * that it's not provided by a .service file or can't currently
1610 * In particular, in both cases, it could be that a service
1611 * owner will actually appear later. So instead of erroring out,
1612 * we just proceed to invoke GetNameOwner() if dealing with the
1613 * kind of errors above.
1615 if (error->domain == G_DBUS_ERROR && error->code == G_DBUS_ERROR_SERVICE_UNKNOWN)
1617 g_error_free (error);
1621 gchar *remote_error = g_dbus_error_get_remote_error (error);
1622 if (g_strcmp0 (remote_error, "org.freedesktop.systemd1.Masked") == 0)
1624 g_error_free (error);
1625 g_free (remote_error);
1629 g_prefix_error (&error,
1630 _("Error calling StartServiceByName for %s: "),
1631 data->proxy->priv->name);
1632 g_free (remote_error);
1639 guint32 start_service_result;
1640 g_variant_get (result,
1642 &start_service_result);
1643 g_variant_unref (result);
1644 if (start_service_result == 1 || /* DBUS_START_REPLY_SUCCESS */
1645 start_service_result == 2) /* DBUS_START_REPLY_ALREADY_RUNNING */
1647 /* continue to invoke GetNameOwner() */
1651 error = g_error_new (G_IO_ERROR,
1653 _("Unexpected reply %d from StartServiceByName(\"%s\") method"),
1654 start_service_result,
1655 data->proxy->priv->name);
1660 async_init_call_get_name_owner (data);
1664 g_warn_if_fail (error != NULL);
1665 g_simple_async_result_take_error (data->simple, error);
1666 g_simple_async_result_complete_in_idle (data->simple);
1667 async_init_data_free (data);
1671 async_init_call_start_service_by_name (AsyncInitData *data)
1673 g_dbus_connection_call (data->proxy->priv->connection,
1674 "org.freedesktop.DBus", /* name */
1675 "/org/freedesktop/DBus", /* object path */
1676 "org.freedesktop.DBus", /* interface */
1677 "StartServiceByName",
1678 g_variant_new ("(su)",
1679 data->proxy->priv->name,
1681 G_VARIANT_TYPE ("(u)"),
1682 G_DBUS_CALL_FLAGS_NONE,
1685 (GAsyncReadyCallback) async_init_start_service_by_name_cb,
1690 async_initable_init_second_async (GAsyncInitable *initable,
1692 GCancellable *cancellable,
1693 GAsyncReadyCallback callback,
1696 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1697 AsyncInitData *data;
1699 data = g_new0 (AsyncInitData, 1);
1700 data->proxy = g_object_ref (proxy);
1701 data->cancellable = cancellable != NULL ? g_object_ref (cancellable) : NULL;
1702 data->simple = g_simple_async_result_new (G_OBJECT (proxy),
1706 g_simple_async_result_set_check_cancellable (data->simple, cancellable);
1708 /* Check name ownership asynchronously - possibly also start the service */
1709 if (proxy->priv->name == NULL)
1712 async_init_data_set_name_owner (data, NULL);
1714 else if (g_dbus_is_unique_name (proxy->priv->name))
1716 async_init_data_set_name_owner (data, proxy->priv->name);
1720 if ((proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START) ||
1721 (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START_AT_CONSTRUCTION))
1723 async_init_call_get_name_owner (data);
1727 async_init_call_start_service_by_name (data);
1733 async_initable_init_second_finish (GAsyncInitable *initable,
1737 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1738 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1744 if (g_simple_async_result_propagate_error (simple, error))
1747 result = g_simple_async_result_get_op_res_gpointer (simple);
1750 process_get_all_reply (proxy, result);
1756 proxy->priv->initialized = TRUE;
1760 /* ---------------------------------------------------------------------------------------------------- */
1763 async_initable_init_first (GAsyncInitable *initable)
1765 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1767 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES))
1769 /* subscribe to PropertiesChanged() */
1770 proxy->priv->properties_changed_subscription_id =
1771 g_dbus_connection_signal_subscribe (proxy->priv->connection,
1773 "org.freedesktop.DBus.Properties",
1774 "PropertiesChanged",
1775 proxy->priv->object_path,
1776 proxy->priv->interface_name,
1777 G_DBUS_SIGNAL_FLAGS_NONE,
1778 on_properties_changed,
1779 signal_subscription_ref (proxy->priv->signal_subscription_data),
1780 (GDestroyNotify) signal_subscription_unref);
1783 if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS))
1785 /* subscribe to all signals for the object */
1786 proxy->priv->signals_subscription_id =
1787 g_dbus_connection_signal_subscribe (proxy->priv->connection,
1789 proxy->priv->interface_name,
1791 proxy->priv->object_path,
1793 G_DBUS_SIGNAL_FLAGS_NONE,
1795 signal_subscription_ref (proxy->priv->signal_subscription_data),
1796 (GDestroyNotify) signal_subscription_unref);
1799 if (proxy->priv->name != NULL && !g_dbus_is_unique_name (proxy->priv->name))
1801 proxy->priv->name_owner_changed_subscription_id =
1802 g_dbus_connection_signal_subscribe (proxy->priv->connection,
1803 "org.freedesktop.DBus", /* name */
1804 "org.freedesktop.DBus", /* interface */
1805 "NameOwnerChanged", /* signal name */
1806 "/org/freedesktop/DBus", /* path */
1807 proxy->priv->name, /* arg0 */
1808 G_DBUS_SIGNAL_FLAGS_NONE,
1809 on_name_owner_changed,
1810 signal_subscription_ref (proxy->priv->signal_subscription_data),
1811 (GDestroyNotify) signal_subscription_unref);
1815 /* ---------------------------------------------------------------------------------------------------- */
1817 /* initialization is split into two parts - the first is the
1818 * non-blocing part that requires the callers GMainContext - the
1819 * second is a blocking part async part that doesn't require the
1820 * callers GMainContext.. we do this split so the code can be reused
1821 * in the GInitable implementation below.
1823 * Note that obtaining a GDBusConnection is not shared between the two
1831 GCancellable *cancellable;
1832 GAsyncReadyCallback callback;
1834 } GetConnectionData;
1837 get_connection_cb (GObject *source_object,
1841 GetConnectionData *data = user_data;
1845 data->proxy->priv->connection = g_bus_get_finish (res, &error);
1846 if (data->proxy->priv->connection == NULL)
1848 GSimpleAsyncResult *simple;
1849 simple = g_simple_async_result_new (G_OBJECT (data->proxy),
1853 g_simple_async_result_set_check_cancellable (simple, data->cancellable);
1854 g_simple_async_result_take_error (simple, error);
1855 g_simple_async_result_complete_in_idle (simple);
1856 g_object_unref (simple);
1860 async_initable_init_first (G_ASYNC_INITABLE (data->proxy));
1861 async_initable_init_second_async (G_ASYNC_INITABLE (data->proxy),
1868 if (data->cancellable != NULL)
1869 g_object_unref (data->cancellable);
1871 g_object_unref (data->proxy);
1876 async_initable_init_async (GAsyncInitable *initable,
1878 GCancellable *cancellable,
1879 GAsyncReadyCallback callback,
1882 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1884 if (proxy->priv->bus_type != G_BUS_TYPE_NONE)
1886 GetConnectionData *data;
1888 g_assert (proxy->priv->connection == NULL);
1890 data = g_new0 (GetConnectionData, 1);
1891 data->proxy = g_object_ref (proxy);
1892 data->io_priority = io_priority;
1893 data->cancellable = cancellable != NULL ? g_object_ref (cancellable) : NULL;
1894 data->callback = callback;
1895 data->user_data = user_data;
1896 g_bus_get (proxy->priv->bus_type,
1903 async_initable_init_first (initable);
1904 async_initable_init_second_async (initable, io_priority, cancellable, callback, user_data);
1909 async_initable_init_finish (GAsyncInitable *initable,
1913 return async_initable_init_second_finish (initable, res, error);
1917 async_initable_iface_init (GAsyncInitableIface *async_initable_iface)
1919 async_initable_iface->init_async = async_initable_init_async;
1920 async_initable_iface->init_finish = async_initable_init_finish;
1923 /* ---------------------------------------------------------------------------------------------------- */
1927 GMainContext *context;
1930 } InitableAsyncInitableData;
1933 async_initable_init_async_cb (GObject *source_object,
1937 InitableAsyncInitableData *data = user_data;
1938 data->res = g_object_ref (res);
1939 g_main_loop_quit (data->loop);
1942 /* Simply reuse the GAsyncInitable implementation but run the first
1943 * part (that is non-blocking and requires the callers GMainContext)
1944 * with the callers GMainContext.. and the second with a private
1945 * GMainContext (bug 621310 is slightly related).
1947 * Note that obtaining a GDBusConnection is not shared between the two
1951 initable_init (GInitable *initable,
1952 GCancellable *cancellable,
1955 GDBusProxy *proxy = G_DBUS_PROXY (initable);
1956 InitableAsyncInitableData *data;
1961 if (proxy->priv->bus_type != G_BUS_TYPE_NONE)
1963 g_assert (proxy->priv->connection == NULL);
1964 proxy->priv->connection = g_bus_get_sync (proxy->priv->bus_type,
1967 if (proxy->priv->connection == NULL)
1971 async_initable_init_first (G_ASYNC_INITABLE (initable));
1973 data = g_new0 (InitableAsyncInitableData, 1);
1974 data->context = g_main_context_new ();
1975 data->loop = g_main_loop_new (data->context, FALSE);
1977 g_main_context_push_thread_default (data->context);
1979 async_initable_init_second_async (G_ASYNC_INITABLE (initable),
1982 async_initable_init_async_cb,
1985 g_main_loop_run (data->loop);
1987 ret = async_initable_init_second_finish (G_ASYNC_INITABLE (initable),
1991 g_main_context_pop_thread_default (data->context);
1993 g_main_context_unref (data->context);
1994 g_main_loop_unref (data->loop);
1995 g_object_unref (data->res);
2004 initable_iface_init (GInitableIface *initable_iface)
2006 initable_iface->init = initable_init;
2009 /* ---------------------------------------------------------------------------------------------------- */
2013 * @connection: A #GDBusConnection.
2014 * @flags: Flags used when constructing the proxy.
2015 * @info: (allow-none): A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
2016 * @name: (allow-none): A bus name (well-known or unique) or %NULL if @connection is not a message bus connection.
2017 * @object_path: An object path.
2018 * @interface_name: A D-Bus interface name.
2019 * @cancellable: (allow-none): A #GCancellable or %NULL.
2020 * @callback: Callback function to invoke when the proxy is ready.
2021 * @user_data: User data to pass to @callback.
2023 * Creates a proxy for accessing @interface_name on the remote object
2024 * at @object_path owned by @name at @connection and asynchronously
2025 * loads D-Bus properties unless the
2026 * %G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES flag is used. Connect to
2027 * the #GDBusProxy::g-properties-changed signal to get notified about
2030 * If the %G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS flag is not set, also sets up
2031 * match rules for signals. Connect to the #GDBusProxy::g-signal signal
2032 * to handle signals from the remote object.
2034 * If @name is a well-known name and the
2035 * %G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START and %G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START_AT_CONSTRUCTION
2036 * flags aren't set and no name owner currently exists, the message bus
2037 * will be requested to launch a name owner for the name.
2039 * This is a failable asynchronous constructor - when the proxy is
2040 * ready, @callback will be invoked and you can use
2041 * g_dbus_proxy_new_finish() to get the result.
2043 * See g_dbus_proxy_new_sync() and for a synchronous version of this constructor.
2045 * #GDBusProxy is used in this [example][gdbus-wellknown-proxy].
2050 g_dbus_proxy_new (GDBusConnection *connection,
2051 GDBusProxyFlags flags,
2052 GDBusInterfaceInfo *info,
2054 const gchar *object_path,
2055 const gchar *interface_name,
2056 GCancellable *cancellable,
2057 GAsyncReadyCallback callback,
2060 g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
2061 g_return_if_fail ((name == NULL && g_dbus_connection_get_unique_name (connection) == NULL) || g_dbus_is_name (name));
2062 g_return_if_fail (g_variant_is_object_path (object_path));
2063 g_return_if_fail (g_dbus_is_interface_name (interface_name));
2065 g_async_initable_new_async (G_TYPE_DBUS_PROXY,
2071 "g-interface-info", info,
2073 "g-connection", connection,
2074 "g-object-path", object_path,
2075 "g-interface-name", interface_name,
2080 * g_dbus_proxy_new_finish:
2081 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback function passed to g_dbus_proxy_new().
2082 * @error: Return location for error or %NULL.
2084 * Finishes creating a #GDBusProxy.
2086 * Returns: A #GDBusProxy or %NULL if @error is set. Free with g_object_unref().
2091 g_dbus_proxy_new_finish (GAsyncResult *res,
2095 GObject *source_object;
2097 source_object = g_async_result_get_source_object (res);
2098 g_assert (source_object != NULL);
2100 object = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object),
2103 g_object_unref (source_object);
2106 return G_DBUS_PROXY (object);
2112 * g_dbus_proxy_new_sync:
2113 * @connection: A #GDBusConnection.
2114 * @flags: Flags used when constructing the proxy.
2115 * @info: (allow-none): A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
2116 * @name: (allow-none): A bus name (well-known or unique) or %NULL if @connection is not a message bus connection.
2117 * @object_path: An object path.
2118 * @interface_name: A D-Bus interface name.
2119 * @cancellable: (allow-none): A #GCancellable or %NULL.
2120 * @error: (allow-none): Return location for error or %NULL.
2122 * Creates a proxy for accessing @interface_name on the remote object
2123 * at @object_path owned by @name at @connection and synchronously
2124 * loads D-Bus properties unless the
2125 * %G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES flag is used.
2127 * If the %G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS flag is not set, also sets up
2128 * match rules for signals. Connect to the #GDBusProxy::g-signal signal
2129 * to handle signals from the remote object.
2131 * If @name is a well-known name and the
2132 * %G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START and %G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START_AT_CONSTRUCTION
2133 * flags aren't set and no name owner currently exists, the message bus
2134 * will be requested to launch a name owner for the name.
2136 * This is a synchronous failable constructor. See g_dbus_proxy_new()
2137 * and g_dbus_proxy_new_finish() for the asynchronous version.
2139 * #GDBusProxy is used in this [example][gdbus-wellknown-proxy].
2141 * Returns: A #GDBusProxy or %NULL if error is set. Free with g_object_unref().
2146 g_dbus_proxy_new_sync (GDBusConnection *connection,
2147 GDBusProxyFlags flags,
2148 GDBusInterfaceInfo *info,
2150 const gchar *object_path,
2151 const gchar *interface_name,
2152 GCancellable *cancellable,
2155 GInitable *initable;
2157 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
2158 g_return_val_if_fail ((name == NULL && g_dbus_connection_get_unique_name (connection) == NULL) ||
2159 g_dbus_is_name (name), NULL);
2160 g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
2161 g_return_val_if_fail (g_dbus_is_interface_name (interface_name), NULL);
2163 initable = g_initable_new (G_TYPE_DBUS_PROXY,
2167 "g-interface-info", info,
2169 "g-connection", connection,
2170 "g-object-path", object_path,
2171 "g-interface-name", interface_name,
2173 if (initable != NULL)
2174 return G_DBUS_PROXY (initable);
2179 /* ---------------------------------------------------------------------------------------------------- */
2182 * g_dbus_proxy_new_for_bus:
2183 * @bus_type: A #GBusType.
2184 * @flags: Flags used when constructing the proxy.
2185 * @info: (allow-none): A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
2186 * @name: A bus name (well-known or unique).
2187 * @object_path: An object path.
2188 * @interface_name: A D-Bus interface name.
2189 * @cancellable: (allow-none): A #GCancellable or %NULL.
2190 * @callback: Callback function to invoke when the proxy is ready.
2191 * @user_data: User data to pass to @callback.
2193 * Like g_dbus_proxy_new() but takes a #GBusType instead of a #GDBusConnection.
2195 * #GDBusProxy is used in this [example][gdbus-wellknown-proxy].
2200 g_dbus_proxy_new_for_bus (GBusType bus_type,
2201 GDBusProxyFlags flags,
2202 GDBusInterfaceInfo *info,
2204 const gchar *object_path,
2205 const gchar *interface_name,
2206 GCancellable *cancellable,
2207 GAsyncReadyCallback callback,
2210 g_return_if_fail (g_dbus_is_name (name));
2211 g_return_if_fail (g_variant_is_object_path (object_path));
2212 g_return_if_fail (g_dbus_is_interface_name (interface_name));
2214 g_async_initable_new_async (G_TYPE_DBUS_PROXY,
2220 "g-interface-info", info,
2222 "g-bus-type", bus_type,
2223 "g-object-path", object_path,
2224 "g-interface-name", interface_name,
2229 * g_dbus_proxy_new_for_bus_finish:
2230 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback function passed to g_dbus_proxy_new_for_bus().
2231 * @error: Return location for error or %NULL.
2233 * Finishes creating a #GDBusProxy.
2235 * Returns: A #GDBusProxy or %NULL if @error is set. Free with g_object_unref().
2240 g_dbus_proxy_new_for_bus_finish (GAsyncResult *res,
2243 return g_dbus_proxy_new_finish (res, error);
2247 * g_dbus_proxy_new_for_bus_sync:
2248 * @bus_type: A #GBusType.
2249 * @flags: Flags used when constructing the proxy.
2250 * @info: (allow-none): A #GDBusInterfaceInfo specifying the minimal interface
2251 * that @proxy conforms to or %NULL.
2252 * @name: A bus name (well-known or unique).
2253 * @object_path: An object path.
2254 * @interface_name: A D-Bus interface name.
2255 * @cancellable: (allow-none): A #GCancellable or %NULL.
2256 * @error: Return location for error or %NULL.
2258 * Like g_dbus_proxy_new_sync() but takes a #GBusType instead of a #GDBusConnection.
2260 * #GDBusProxy is used in this [example][gdbus-wellknown-proxy].
2262 * Returns: A #GDBusProxy or %NULL if error is set. Free with g_object_unref().
2267 g_dbus_proxy_new_for_bus_sync (GBusType bus_type,
2268 GDBusProxyFlags flags,
2269 GDBusInterfaceInfo *info,
2271 const gchar *object_path,
2272 const gchar *interface_name,
2273 GCancellable *cancellable,
2276 GInitable *initable;
2278 g_return_val_if_fail (g_dbus_is_name (name), NULL);
2279 g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
2280 g_return_val_if_fail (g_dbus_is_interface_name (interface_name), NULL);
2282 initable = g_initable_new (G_TYPE_DBUS_PROXY,
2286 "g-interface-info", info,
2288 "g-bus-type", bus_type,
2289 "g-object-path", object_path,
2290 "g-interface-name", interface_name,
2292 if (initable != NULL)
2293 return G_DBUS_PROXY (initable);
2298 /* ---------------------------------------------------------------------------------------------------- */
2301 * g_dbus_proxy_get_connection:
2302 * @proxy: A #GDBusProxy.
2304 * Gets the connection @proxy is for.
2306 * Returns: (transfer none): A #GDBusConnection owned by @proxy. Do not free.
2311 g_dbus_proxy_get_connection (GDBusProxy *proxy)
2313 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2314 return proxy->priv->connection;
2318 * g_dbus_proxy_get_flags:
2319 * @proxy: A #GDBusProxy.
2321 * Gets the flags that @proxy was constructed with.
2323 * Returns: Flags from the #GDBusProxyFlags enumeration.
2328 g_dbus_proxy_get_flags (GDBusProxy *proxy)
2330 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), 0);
2331 return proxy->priv->flags;
2335 * g_dbus_proxy_get_name:
2336 * @proxy: A #GDBusProxy.
2338 * Gets the name that @proxy was constructed for.
2340 * Returns: A string owned by @proxy. Do not free.
2345 g_dbus_proxy_get_name (GDBusProxy *proxy)
2347 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2348 return proxy->priv->name;
2352 * g_dbus_proxy_get_name_owner:
2353 * @proxy: A #GDBusProxy.
2355 * The unique name that owns the name that @proxy is for or %NULL if
2356 * no-one currently owns that name. You may connect to the
2357 * #GObject::notify signal to track changes to the
2358 * #GDBusProxy:g-name-owner property.
2360 * Returns: The name owner or %NULL if no name owner exists. Free with g_free().
2365 g_dbus_proxy_get_name_owner (GDBusProxy *proxy)
2369 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2371 G_LOCK (properties_lock);
2372 ret = g_strdup (proxy->priv->name_owner);
2373 G_UNLOCK (properties_lock);
2378 * g_dbus_proxy_get_object_path:
2379 * @proxy: A #GDBusProxy.
2381 * Gets the object path @proxy is for.
2383 * Returns: A string owned by @proxy. Do not free.
2388 g_dbus_proxy_get_object_path (GDBusProxy *proxy)
2390 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2391 return proxy->priv->object_path;
2395 * g_dbus_proxy_get_interface_name:
2396 * @proxy: A #GDBusProxy.
2398 * Gets the D-Bus interface name @proxy is for.
2400 * Returns: A string owned by @proxy. Do not free.
2405 g_dbus_proxy_get_interface_name (GDBusProxy *proxy)
2407 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2408 return proxy->priv->interface_name;
2412 * g_dbus_proxy_get_default_timeout:
2413 * @proxy: A #GDBusProxy.
2415 * Gets the timeout to use if -1 (specifying default timeout) is
2416 * passed as @timeout_msec in the g_dbus_proxy_call() and
2417 * g_dbus_proxy_call_sync() functions.
2419 * See the #GDBusProxy:g-default-timeout property for more details.
2421 * Returns: Timeout to use for @proxy.
2426 g_dbus_proxy_get_default_timeout (GDBusProxy *proxy)
2430 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), -1);
2432 G_LOCK (properties_lock);
2433 ret = proxy->priv->timeout_msec;
2434 G_UNLOCK (properties_lock);
2439 * g_dbus_proxy_set_default_timeout:
2440 * @proxy: A #GDBusProxy.
2441 * @timeout_msec: Timeout in milliseconds.
2443 * Sets the timeout to use if -1 (specifying default timeout) is
2444 * passed as @timeout_msec in the g_dbus_proxy_call() and
2445 * g_dbus_proxy_call_sync() functions.
2447 * See the #GDBusProxy:g-default-timeout property for more details.
2452 g_dbus_proxy_set_default_timeout (GDBusProxy *proxy,
2455 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
2456 g_return_if_fail (timeout_msec == -1 || timeout_msec >= 0);
2458 G_LOCK (properties_lock);
2460 if (proxy->priv->timeout_msec != timeout_msec)
2462 proxy->priv->timeout_msec = timeout_msec;
2463 G_UNLOCK (properties_lock);
2465 g_object_notify (G_OBJECT (proxy), "g-default-timeout");
2469 G_UNLOCK (properties_lock);
2474 * g_dbus_proxy_get_interface_info:
2475 * @proxy: A #GDBusProxy
2477 * Returns the #GDBusInterfaceInfo, if any, specifying the interface
2478 * that @proxy conforms to. See the #GDBusProxy:g-interface-info
2479 * property for more details.
2481 * Returns: A #GDBusInterfaceInfo or %NULL. Do not unref the returned
2482 * object, it is owned by @proxy.
2486 GDBusInterfaceInfo *
2487 g_dbus_proxy_get_interface_info (GDBusProxy *proxy)
2489 GDBusInterfaceInfo *ret;
2491 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2493 G_LOCK (properties_lock);
2494 ret = proxy->priv->expected_interface;
2495 G_UNLOCK (properties_lock);
2496 /* FIXME: returning a borrowed ref with no guarantee that nobody will
2497 * call g_dbus_proxy_set_interface_info() and make it invalid...
2503 * g_dbus_proxy_set_interface_info:
2504 * @proxy: A #GDBusProxy
2505 * @info: (allow-none): Minimum interface this proxy conforms to or %NULL to unset.
2507 * Ensure that interactions with @proxy conform to the given
2508 * interface. See the #GDBusProxy:g-interface-info property for more
2514 g_dbus_proxy_set_interface_info (GDBusProxy *proxy,
2515 GDBusInterfaceInfo *info)
2517 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
2518 G_LOCK (properties_lock);
2520 if (proxy->priv->expected_interface != NULL)
2522 g_dbus_interface_info_cache_release (proxy->priv->expected_interface);
2523 g_dbus_interface_info_unref (proxy->priv->expected_interface);
2525 proxy->priv->expected_interface = info != NULL ? g_dbus_interface_info_ref (info) : NULL;
2526 if (proxy->priv->expected_interface != NULL)
2527 g_dbus_interface_info_cache_build (proxy->priv->expected_interface);
2529 G_UNLOCK (properties_lock);
2532 /* ---------------------------------------------------------------------------------------------------- */
2535 maybe_split_method_name (const gchar *method_name,
2536 gchar **out_interface_name,
2537 const gchar **out_method_name)
2542 g_assert (out_interface_name != NULL);
2543 g_assert (out_method_name != NULL);
2544 *out_interface_name = NULL;
2545 *out_method_name = NULL;
2547 if (strchr (method_name, '.') != NULL)
2552 p = g_strdup (method_name);
2553 last_dot = strrchr (p, '.');
2556 *out_interface_name = p;
2557 *out_method_name = last_dot + 1;
2569 GUnixFDList *fd_list;
2574 reply_data_free (ReplyData *data)
2576 g_variant_unref (data->value);
2578 if (data->fd_list != NULL)
2579 g_object_unref (data->fd_list);
2581 g_slice_free (ReplyData, data);
2585 reply_cb (GDBusConnection *connection,
2589 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
2593 GUnixFDList *fd_list;
2598 value = g_dbus_connection_call_with_unix_fd_list_finish (connection,
2603 value = g_dbus_connection_call_finish (connection,
2609 g_simple_async_result_take_error (simple, error);
2614 data = g_slice_new0 (ReplyData);
2615 data->value = value;
2617 data->fd_list = fd_list;
2619 g_simple_async_result_set_op_res_gpointer (simple, data, (GDestroyNotify) reply_data_free);
2622 /* no need to complete in idle since the method GDBusConnection already does */
2623 g_simple_async_result_complete (simple);
2624 g_object_unref (simple);
2627 /* properties_lock must be held for as long as you will keep the
2630 static const GDBusMethodInfo *
2631 lookup_method_info (GDBusProxy *proxy,
2632 const gchar *method_name)
2634 const GDBusMethodInfo *info = NULL;
2636 if (proxy->priv->expected_interface == NULL)
2639 info = g_dbus_interface_info_lookup_method (proxy->priv->expected_interface, method_name);
2645 /* properties_lock must be held for as long as you will keep the
2648 static const gchar *
2649 get_destination_for_call (GDBusProxy *proxy)
2655 /* If proxy->priv->name is a unique name, then proxy->priv->name_owner
2656 * is never NULL and always the same as proxy->priv->name. We use this
2657 * knowledge to avoid checking if proxy->priv->name is a unique or
2660 ret = proxy->priv->name_owner;
2664 if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START)
2667 ret = proxy->priv->name;
2673 /* ---------------------------------------------------------------------------------------------------- */
2676 g_dbus_proxy_call_internal (GDBusProxy *proxy,
2677 const gchar *method_name,
2678 GVariant *parameters,
2679 GDBusCallFlags flags,
2681 GUnixFDList *fd_list,
2682 GCancellable *cancellable,
2683 GAsyncReadyCallback callback,
2686 GSimpleAsyncResult *simple;
2688 gchar *split_interface_name;
2689 const gchar *split_method_name;
2690 const gchar *target_method_name;
2691 const gchar *target_interface_name;
2693 GVariantType *reply_type;
2694 GAsyncReadyCallback my_callback;
2696 g_return_if_fail (G_IS_DBUS_PROXY (proxy));
2697 g_return_if_fail (g_dbus_is_member_name (method_name) || g_dbus_is_interface_name (method_name));
2698 g_return_if_fail (parameters == NULL || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE));
2699 g_return_if_fail (timeout_msec == -1 || timeout_msec >= 0);
2701 g_return_if_fail (fd_list == NULL || G_IS_UNIX_FD_LIST (fd_list));
2703 g_return_if_fail (fd_list == NULL);
2707 split_interface_name = NULL;
2709 /* g_dbus_connection_call() is optimised for the case of a NULL
2710 * callback. If we get a NULL callback from our user then make sure
2711 * we pass along a NULL callback for ourselves as well.
2713 if (callback != NULL)
2715 my_callback = (GAsyncReadyCallback) reply_cb;
2716 simple = g_simple_async_result_new (G_OBJECT (proxy),
2719 g_dbus_proxy_call_internal);
2720 g_simple_async_result_set_check_cancellable (simple, cancellable);
2728 G_LOCK (properties_lock);
2730 was_split = maybe_split_method_name (method_name, &split_interface_name, &split_method_name);
2731 target_method_name = was_split ? split_method_name : method_name;
2732 target_interface_name = was_split ? split_interface_name : proxy->priv->interface_name;
2734 /* Warn if method is unexpected (cf. :g-interface-info) */
2737 const GDBusMethodInfo *expected_method_info;
2738 expected_method_info = lookup_method_info (proxy, target_method_name);
2739 if (expected_method_info != NULL)
2740 reply_type = _g_dbus_compute_complete_signature (expected_method_info->out_args);
2744 if (proxy->priv->name != NULL)
2746 destination = g_strdup (get_destination_for_call (proxy));
2747 if (destination == NULL)
2751 g_simple_async_result_set_error (simple,
2754 _("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"));
2755 g_simple_async_result_complete_in_idle (simple);
2756 g_object_unref (simple);
2758 G_UNLOCK (properties_lock);
2763 G_UNLOCK (properties_lock);
2766 g_dbus_connection_call_with_unix_fd_list (proxy->priv->connection,
2768 proxy->priv->object_path,
2769 target_interface_name,
2774 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2780 g_dbus_connection_call (proxy->priv->connection,
2782 proxy->priv->object_path,
2783 target_interface_name,
2788 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2795 if (reply_type != NULL)
2796 g_variant_type_free (reply_type);
2798 g_free (destination);
2799 g_free (split_interface_name);
2803 g_dbus_proxy_call_finish_internal (GDBusProxy *proxy,
2804 GUnixFDList **out_fd_list,
2808 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2812 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2813 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2814 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2816 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_dbus_proxy_call_internal);
2820 if (g_simple_async_result_propagate_error (simple, error))
2823 data = g_simple_async_result_get_op_res_gpointer (simple);
2824 value = g_variant_ref (data->value);
2826 if (out_fd_list != NULL)
2827 *out_fd_list = data->fd_list != NULL ? g_object_ref (data->fd_list) : NULL;
2835 g_dbus_proxy_call_sync_internal (GDBusProxy *proxy,
2836 const gchar *method_name,
2837 GVariant *parameters,
2838 GDBusCallFlags flags,
2840 GUnixFDList *fd_list,
2841 GUnixFDList **out_fd_list,
2842 GCancellable *cancellable,
2847 gchar *split_interface_name;
2848 const gchar *split_method_name;
2849 const gchar *target_method_name;
2850 const gchar *target_interface_name;
2852 GVariantType *reply_type;
2854 g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2855 g_return_val_if_fail (g_dbus_is_member_name (method_name) || g_dbus_is_interface_name (method_name), NULL);
2856 g_return_val_if_fail (parameters == NULL || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE), NULL);
2857 g_return_val_if_fail (timeout_msec == -1 || timeout_msec >= 0, NULL);
2859 g_return_val_if_fail (fd_list == NULL || G_IS_UNIX_FD_LIST (fd_list), NULL);
2861 g_return_val_if_fail (fd_list == NULL, NULL);
2863 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2867 G_LOCK (properties_lock);
2869 was_split = maybe_split_method_name (method_name, &split_interface_name, &split_method_name);
2870 target_method_name = was_split ? split_method_name : method_name;
2871 target_interface_name = was_split ? split_interface_name : proxy->priv->interface_name;
2873 /* Warn if method is unexpected (cf. :g-interface-info) */
2876 const GDBusMethodInfo *expected_method_info;
2877 expected_method_info = lookup_method_info (proxy, target_method_name);
2878 if (expected_method_info != NULL)
2879 reply_type = _g_dbus_compute_complete_signature (expected_method_info->out_args);
2883 if (proxy->priv->name != NULL)
2885 destination = g_strdup (get_destination_for_call (proxy));
2886 if (destination == NULL)
2888 g_set_error_literal (error,
2891 _("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"));
2893 G_UNLOCK (properties_lock);
2898 G_UNLOCK (properties_lock);
2901 ret = g_dbus_connection_call_with_unix_fd_list_sync (proxy->priv->connection,
2903 proxy->priv->object_path,
2904 target_interface_name,
2909 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2915 ret = g_dbus_connection_call_sync (proxy->priv->connection,
2917 proxy->priv->object_path,
2918 target_interface_name,
2923 timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2929 if (reply_type != NULL)
2930 g_variant_type_free (reply_type);
2932 g_free (destination);
2933 g_free (split_interface_name);
2938 /* ---------------------------------------------------------------------------------------------------- */
2941 * g_dbus_proxy_call:
2942 * @proxy: A #GDBusProxy.
2943 * @method_name: Name of method to invoke.
2944 * @parameters: (allow-none): A #GVariant tuple with parameters for the signal or %NULL if not passing parameters.
2945 * @flags: Flags from the #GDBusCallFlags enumeration.
2946 * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
2947 * "infinite") or -1 to use the proxy default timeout.
2948 * @cancellable: (allow-none): A #GCancellable or %NULL.
2949 * @callback: (allow-none): A #GAsyncReadyCallback to call when the request is satisfied or %NULL if you don't
2950 * care about the result of the method invocation.
2951 * @user_data: The data to pass to @callback.
2953 * Asynchronously invokes the @method_name method on @proxy.
2955 * If @method_name contains any dots, then @name is split into interface and
2956 * method name parts. This allows using @proxy for invoking methods on
2959 * If the #GDBusConnection associated with @proxy is closed then
2960 * the operation will fail with %G_IO_ERROR_CLOSED. If
2961 * @cancellable is canceled, the operation will fail with
2962 * %G_IO_ERROR_CANCELLED. If @parameters contains a value not
2963 * compatible with the D-Bus protocol, the operation fails with
2964 * %G_IO_ERROR_INVALID_ARGUMENT.
2966 * If the @parameters #GVariant is floating, it is consumed. This allows
2967 * convenient 'inline' use of g_variant_new(), e.g.:
2968 * |[<!-- language="C" -->
2969 * g_dbus_proxy_call (proxy,
2971 * g_variant_new ("(ss)",
2974 * G_DBUS_CALL_FLAGS_NONE,
2977 * (GAsyncReadyCallback) two_strings_done,
2981 * If @proxy has an expected interface (see
2982 * #GDBusProxy:g-interface-info) and @method_name is referenced by it,
2983 * then the return value is checked against the return type.
2985 * This is an asynchronous method. When the operation is finished,
2986 * @callback will be invoked in the
2987 * [thread-default main context][g-main-context-push-thread-default]
2988 * of the thread you are calling this method from.
2989 * You can then call g_dbus_proxy_call_finish() to get the result of
2990 * the operation. See g_dbus_proxy_call_sync() for the synchronous
2991 * version of this method.
2993 * If @callback is %NULL then the D-Bus method call message will be sent with
2994 * the %G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED flag set.
2999 g_dbus_proxy_call (GDBusProxy *proxy,
3000 const gchar *method_name,
3001 GVariant *parameters,
3002 GDBusCallFlags flags,
3004 GCancellable *cancellable,
3005 GAsyncReadyCallback callback,
3008 g_dbus_proxy_call_internal (proxy, method_name, parameters, flags, timeout_msec, NULL, cancellable, callback, user_data);
3012 * g_dbus_proxy_call_finish:
3013 * @proxy: A #GDBusProxy.
3014 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_proxy_call().
3015 * @error: Return location for error or %NULL.
3017 * Finishes an operation started with g_dbus_proxy_call().
3019 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
3020 * return values. Free with g_variant_unref().
3025 g_dbus_proxy_call_finish (GDBusProxy *proxy,
3029 return g_dbus_proxy_call_finish_internal (proxy, NULL, res, error);
3033 * g_dbus_proxy_call_sync:
3034 * @proxy: A #GDBusProxy.
3035 * @method_name: Name of method to invoke.
3036 * @parameters: (allow-none): A #GVariant tuple with parameters for the signal
3037 * or %NULL if not passing parameters.
3038 * @flags: Flags from the #GDBusCallFlags enumeration.
3039 * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
3040 * "infinite") or -1 to use the proxy default timeout.
3041 * @cancellable: (allow-none): A #GCancellable or %NULL.
3042 * @error: Return location for error or %NULL.
3044 * Synchronously invokes the @method_name method on @proxy.
3046 * If @method_name contains any dots, then @name is split into interface and
3047 * method name parts. This allows using @proxy for invoking methods on
3050 * If the #GDBusConnection associated with @proxy is disconnected then
3051 * the operation will fail with %G_IO_ERROR_CLOSED. If
3052 * @cancellable is canceled, the operation will fail with
3053 * %G_IO_ERROR_CANCELLED. If @parameters contains a value not
3054 * compatible with the D-Bus protocol, the operation fails with
3055 * %G_IO_ERROR_INVALID_ARGUMENT.
3057 * If the @parameters #GVariant is floating, it is consumed. This allows
3058 * convenient 'inline' use of g_variant_new(), e.g.:
3059 * |[<!-- language="C" -->
3060 * g_dbus_proxy_call_sync (proxy,
3062 * g_variant_new ("(ss)",
3065 * G_DBUS_CALL_FLAGS_NONE,
3071 * The calling thread is blocked until a reply is received. See
3072 * g_dbus_proxy_call() for the asynchronous version of this
3075 * If @proxy has an expected interface (see
3076 * #GDBusProxy:g-interface-info) and @method_name is referenced by it,
3077 * then the return value is checked against the return type.
3079 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
3080 * return values. Free with g_variant_unref().
3085 g_dbus_proxy_call_sync (GDBusProxy *proxy,
3086 const gchar *method_name,
3087 GVariant *parameters,
3088 GDBusCallFlags flags,
3090 GCancellable *cancellable,
3093 return g_dbus_proxy_call_sync_internal (proxy, method_name, parameters, flags, timeout_msec, NULL, NULL, cancellable, error);
3096 /* ---------------------------------------------------------------------------------------------------- */
3101 * g_dbus_proxy_call_with_unix_fd_list:
3102 * @proxy: A #GDBusProxy.
3103 * @method_name: Name of method to invoke.
3104 * @parameters: (allow-none): A #GVariant tuple with parameters for the signal or %NULL if not passing parameters.
3105 * @flags: Flags from the #GDBusCallFlags enumeration.
3106 * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
3107 * "infinite") or -1 to use the proxy default timeout.
3108 * @fd_list: (allow-none): A #GUnixFDList or %NULL.
3109 * @cancellable: (allow-none): A #GCancellable or %NULL.
3110 * @callback: (allow-none): A #GAsyncReadyCallback to call when the request is satisfied or %NULL if you don't
3111 * care about the result of the method invocation.
3112 * @user_data: The data to pass to @callback.
3114 * Like g_dbus_proxy_call() but also takes a #GUnixFDList object.
3116 * This method is only available on UNIX.
3121 g_dbus_proxy_call_with_unix_fd_list (GDBusProxy *proxy,
3122 const gchar *method_name,
3123 GVariant *parameters,
3124 GDBusCallFlags flags,
3126 GUnixFDList *fd_list,
3127 GCancellable *cancellable,
3128 GAsyncReadyCallback callback,
3131 g_dbus_proxy_call_internal (proxy, method_name, parameters, flags, timeout_msec, fd_list, cancellable, callback, user_data);
3135 * g_dbus_proxy_call_with_unix_fd_list_finish:
3136 * @proxy: A #GDBusProxy.
3137 * @out_fd_list: (out) (allow-none): Return location for a #GUnixFDList or %NULL.
3138 * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_proxy_call_with_unix_fd_list().
3139 * @error: Return location for error or %NULL.
3141 * Finishes an operation started with g_dbus_proxy_call_with_unix_fd_list().
3143 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
3144 * return values. Free with g_variant_unref().
3149 g_dbus_proxy_call_with_unix_fd_list_finish (GDBusProxy *proxy,
3150 GUnixFDList **out_fd_list,
3154 return g_dbus_proxy_call_finish_internal (proxy, out_fd_list, res, error);
3158 * g_dbus_proxy_call_with_unix_fd_list_sync:
3159 * @proxy: A #GDBusProxy.
3160 * @method_name: Name of method to invoke.
3161 * @parameters: (allow-none): A #GVariant tuple with parameters for the signal
3162 * or %NULL if not passing parameters.
3163 * @flags: Flags from the #GDBusCallFlags enumeration.
3164 * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
3165 * "infinite") or -1 to use the proxy default timeout.
3166 * @fd_list: (allow-none): A #GUnixFDList or %NULL.
3167 * @out_fd_list: (out) (allow-none): Return location for a #GUnixFDList or %NULL.
3168 * @cancellable: (allow-none): A #GCancellable or %NULL.
3169 * @error: Return location for error or %NULL.
3171 * Like g_dbus_proxy_call_sync() but also takes and returns #GUnixFDList objects.
3173 * This method is only available on UNIX.
3175 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
3176 * return values. Free with g_variant_unref().
3181 g_dbus_proxy_call_with_unix_fd_list_sync (GDBusProxy *proxy,
3182 const gchar *method_name,
3183 GVariant *parameters,
3184 GDBusCallFlags flags,
3186 GUnixFDList *fd_list,
3187 GUnixFDList **out_fd_list,
3188 GCancellable *cancellable,
3191 return g_dbus_proxy_call_sync_internal (proxy, method_name, parameters, flags, timeout_msec, fd_list, out_fd_list, cancellable, error);
3194 #endif /* G_OS_UNIX */
3196 /* ---------------------------------------------------------------------------------------------------- */
3198 static GDBusInterfaceInfo *
3199 _g_dbus_proxy_get_info (GDBusInterface *interface)
3201 GDBusProxy *proxy = G_DBUS_PROXY (interface);
3202 return g_dbus_proxy_get_interface_info (proxy);
3205 static GDBusObject *
3206 _g_dbus_proxy_get_object (GDBusInterface *interface)
3208 GDBusProxy *proxy = G_DBUS_PROXY (interface);
3209 return proxy->priv->object;
3212 static GDBusObject *
3213 _g_dbus_proxy_dup_object (GDBusInterface *interface)
3215 GDBusProxy *proxy = G_DBUS_PROXY (interface);
3216 GDBusObject *ret = NULL;
3218 G_LOCK (properties_lock);
3219 if (proxy->priv->object != NULL)
3220 ret = g_object_ref (proxy->priv->object);
3221 G_UNLOCK (properties_lock);
3226 _g_dbus_proxy_set_object (GDBusInterface *interface,
3227 GDBusObject *object)
3229 GDBusProxy *proxy = G_DBUS_PROXY (interface);
3230 G_LOCK (properties_lock);
3231 if (proxy->priv->object != NULL)
3232 g_object_remove_weak_pointer (G_OBJECT (proxy->priv->object), (gpointer *) &proxy->priv->object);
3233 proxy->priv->object = object;
3234 if (proxy->priv->object != NULL)
3235 g_object_add_weak_pointer (G_OBJECT (proxy->priv->object), (gpointer *) &proxy->priv->object);
3236 G_UNLOCK (properties_lock);
3240 dbus_interface_iface_init (GDBusInterfaceIface *dbus_interface_iface)
3242 dbus_interface_iface->get_info = _g_dbus_proxy_get_info;
3243 dbus_interface_iface->get_object = _g_dbus_proxy_get_object;
3244 dbus_interface_iface->dup_object = _g_dbus_proxy_dup_object;
3245 dbus_interface_iface->set_object = _g_dbus_proxy_set_object;
3248 /* ---------------------------------------------------------------------------------------------------- */