More fixes for non-Unix builds
[platform/upstream/glib.git] / gio / gdbusproxy.c
1 /* GDBus - GLib D-Bus Library
2  *
3  * Copyright (C) 2008-2010 Red Hat, Inc.
4  *
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.
9  *
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.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Author: David Zeuthen <davidz@redhat.com>
21  */
22
23 #include "config.h"
24
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include "gdbusutils.h"
29 #include "gdbusproxy.h"
30 #include "gioenumtypes.h"
31 #include "gdbusconnection.h"
32 #include "gdbuserror.h"
33 #include "gdbusprivate.h"
34 #include "ginitable.h"
35 #include "gasyncinitable.h"
36 #include "gioerror.h"
37 #include "gasyncresult.h"
38 #include "gsimpleasyncresult.h"
39 #include "gcancellable.h"
40 #include "gdbusinterface.h"
41
42 #ifdef G_OS_UNIX
43 #include "gunixfdlist.h"
44 #endif
45
46 #include "glibintl.h"
47
48 /**
49  * SECTION:gdbusproxy
50  * @short_description: Client-side D-Bus interface proxy
51  * @include: gio/gio.h
52  *
53  * #GDBusProxy is a base class used for proxies to access a D-Bus
54  * interface on a remote object. A #GDBusProxy can be constructed for
55  * both well-known and unique names.
56  *
57  * By default, #GDBusProxy will cache all properties (and listen to
58  * changes) of the remote object, and proxy all signals that gets
59  * emitted. This behaviour can be changed by passing suitable
60  * #GDBusProxyFlags when the proxy is created. If the proxy is for a
61  * well-known name, the property cache is flushed when the name owner
62  * vanishes and reloaded when a name owner appears.
63  *
64  * If a #GDBusProxy is used for a well-known name, the owner of the
65  * name is tracked and can be read from
66  * #GDBusProxy:g-name-owner. Connect to the #GObject::notify signal to
67  * get notified of changes. Additionally, only signals and property
68  * changes emitted from the current name owner are considered and
69  * calls are always sent to the current name owner. This avoids a
70  * number of race conditions when the name is lost by one owner and
71  * claimed by another. However, if no name owner currently exists,
72  * then calls will be sent to the well-known name which may result in
73  * the message bus launching an owner (unless
74  * %G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START is set).
75  *
76  * The generic #GDBusProxy::g-properties-changed and
77  * #GDBusProxy::g-signal signals are not very convenient to work
78  * with. Therefore, the recommended way of working with proxies is to
79  * subclass #GDBusProxy, and have more natural properties and signals
80  * in your derived class. See <xref linkend="gdbus-example-gdbus-codegen"/>
81  * for how this can easily be done using the
82  * <command><link linkend="gdbus-codegen">gdbus-codegen</link></command>
83  * tool.
84  *
85  * A #GDBusProxy instance can be used from multiple threads but note
86  * that all signals (e.g. #GDBusProxy::g-signal, #GDBusProxy::g-properties-changed
87  * and #GObject::notify) are emitted in the
88  * <link linkend="g-main-context-push-thread-default">thread-default main loop</link>
89  * of the thread where the instance was constructed.
90  *
91  * <example id="gdbus-wellknown-proxy"><title>GDBusProxy for a well-known-name</title><programlisting><xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/tests/gdbus-example-watch-proxy.c"><xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback></xi:include></programlisting></example>
92  */
93
94 /* lock protecting the properties GHashTable */
95 G_LOCK_DEFINE_STATIC (properties_lock);
96
97 /* ---------------------------------------------------------------------------------------------------- */
98
99 G_LOCK_DEFINE_STATIC (signal_subscription_lock);
100
101 typedef struct
102 {
103   volatile gint ref_count;
104   GDBusProxy *proxy;
105 } SignalSubscriptionData;
106
107 static SignalSubscriptionData *
108 signal_subscription_ref (SignalSubscriptionData *data)
109 {
110   g_atomic_int_inc (&data->ref_count);
111   return data;
112 }
113
114 static void
115 signal_subscription_unref (SignalSubscriptionData *data)
116 {
117   if (g_atomic_int_dec_and_test (&data->ref_count))
118     {
119       g_slice_free (SignalSubscriptionData, data);
120     }
121 }
122
123 /* ---------------------------------------------------------------------------------------------------- */
124
125 struct _GDBusProxyPrivate
126 {
127   GBusType bus_type;
128   GDBusProxyFlags flags;
129   GDBusConnection *connection;
130
131   gchar *name;
132   gchar *name_owner;
133   gchar *object_path;
134   gchar *interface_name;
135   gint timeout_msec;
136
137   guint name_owner_changed_subscription_id;
138
139   GCancellable *get_all_cancellable;
140
141   /* gchar* -> GVariant* */
142   GHashTable *properties;
143
144   GDBusInterfaceInfo *expected_interface;
145
146   guint properties_changed_subscription_id;
147   guint signals_subscription_id;
148
149   gboolean initialized;
150
151   GDBusObject *object;
152
153   SignalSubscriptionData *signal_subscription_data;
154 };
155
156 enum
157 {
158   PROP_0,
159   PROP_G_CONNECTION,
160   PROP_G_BUS_TYPE,
161   PROP_G_NAME,
162   PROP_G_NAME_OWNER,
163   PROP_G_FLAGS,
164   PROP_G_OBJECT_PATH,
165   PROP_G_INTERFACE_NAME,
166   PROP_G_DEFAULT_TIMEOUT,
167   PROP_G_INTERFACE_INFO
168 };
169
170 enum
171 {
172   PROPERTIES_CHANGED_SIGNAL,
173   SIGNAL_SIGNAL,
174   LAST_SIGNAL,
175 };
176
177 guint signals[LAST_SIGNAL] = {0};
178
179 static void dbus_interface_iface_init (GDBusInterfaceIface *dbus_interface_iface);
180 static void initable_iface_init       (GInitableIface *initable_iface);
181 static void async_initable_iface_init (GAsyncInitableIface *async_initable_iface);
182
183 G_DEFINE_TYPE_WITH_CODE (GDBusProxy, g_dbus_proxy, G_TYPE_OBJECT,
184                          G_IMPLEMENT_INTERFACE (G_TYPE_DBUS_INTERFACE, dbus_interface_iface_init)
185                          G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, initable_iface_init)
186                          G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE, async_initable_iface_init)
187                          );
188
189 static void
190 g_dbus_proxy_dispose (GObject *object)
191 {
192   GDBusProxy *proxy = G_DBUS_PROXY (object);
193   G_LOCK (signal_subscription_lock);
194   if (proxy->priv->signal_subscription_data != NULL)
195     {
196       proxy->priv->signal_subscription_data->proxy = NULL;
197       signal_subscription_unref (proxy->priv->signal_subscription_data);
198       proxy->priv->signal_subscription_data = NULL;
199     }
200   G_UNLOCK (signal_subscription_lock);
201
202   G_OBJECT_CLASS (g_dbus_proxy_parent_class)->dispose (object);
203 }
204
205 static void
206 g_dbus_proxy_finalize (GObject *object)
207 {
208   GDBusProxy *proxy = G_DBUS_PROXY (object);
209
210   g_warn_if_fail (proxy->priv->get_all_cancellable == NULL);
211
212   if (proxy->priv->name_owner_changed_subscription_id > 0)
213     g_dbus_connection_signal_unsubscribe (proxy->priv->connection,
214                                           proxy->priv->name_owner_changed_subscription_id);
215
216   if (proxy->priv->properties_changed_subscription_id > 0)
217     g_dbus_connection_signal_unsubscribe (proxy->priv->connection,
218                                           proxy->priv->properties_changed_subscription_id);
219
220   if (proxy->priv->signals_subscription_id > 0)
221     g_dbus_connection_signal_unsubscribe (proxy->priv->connection,
222                                           proxy->priv->signals_subscription_id);
223
224   if (proxy->priv->connection != NULL)
225     g_object_unref (proxy->priv->connection);
226   g_free (proxy->priv->name);
227   g_free (proxy->priv->name_owner);
228   g_free (proxy->priv->object_path);
229   g_free (proxy->priv->interface_name);
230   if (proxy->priv->properties != NULL)
231     g_hash_table_unref (proxy->priv->properties);
232
233   if (proxy->priv->expected_interface != NULL)
234     {
235       g_dbus_interface_info_cache_release (proxy->priv->expected_interface);
236       g_dbus_interface_info_unref (proxy->priv->expected_interface);
237     }
238
239   if (proxy->priv->object != NULL)
240     g_object_remove_weak_pointer (G_OBJECT (proxy->priv->object), (gpointer *) &proxy->priv->object);
241
242   G_OBJECT_CLASS (g_dbus_proxy_parent_class)->finalize (object);
243 }
244
245 static void
246 g_dbus_proxy_get_property (GObject    *object,
247                            guint       prop_id,
248                            GValue     *value,
249                            GParamSpec *pspec)
250 {
251   GDBusProxy *proxy = G_DBUS_PROXY (object);
252
253   switch (prop_id)
254     {
255     case PROP_G_CONNECTION:
256       g_value_set_object (value, proxy->priv->connection);
257       break;
258
259     case PROP_G_FLAGS:
260       g_value_set_flags (value, proxy->priv->flags);
261       break;
262
263     case PROP_G_NAME:
264       g_value_set_string (value, proxy->priv->name);
265       break;
266
267     case PROP_G_NAME_OWNER:
268       g_value_set_string (value, proxy->priv->name_owner);
269       break;
270
271     case PROP_G_OBJECT_PATH:
272       g_value_set_string (value, proxy->priv->object_path);
273       break;
274
275     case PROP_G_INTERFACE_NAME:
276       g_value_set_string (value, proxy->priv->interface_name);
277       break;
278
279     case PROP_G_DEFAULT_TIMEOUT:
280       g_value_set_int (value, proxy->priv->timeout_msec);
281       break;
282
283     case PROP_G_INTERFACE_INFO:
284       g_value_set_boxed (value, g_dbus_proxy_get_interface_info (proxy));
285       break;
286
287     default:
288       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
289       break;
290     }
291 }
292
293 static void
294 g_dbus_proxy_set_property (GObject      *object,
295                            guint         prop_id,
296                            const GValue *value,
297                            GParamSpec   *pspec)
298 {
299   GDBusProxy *proxy = G_DBUS_PROXY (object);
300
301   switch (prop_id)
302     {
303     case PROP_G_CONNECTION:
304       proxy->priv->connection = g_value_dup_object (value);
305       break;
306
307     case PROP_G_FLAGS:
308       proxy->priv->flags = g_value_get_flags (value);
309       break;
310
311     case PROP_G_NAME:
312       proxy->priv->name = g_value_dup_string (value);
313       break;
314
315     case PROP_G_OBJECT_PATH:
316       proxy->priv->object_path = g_value_dup_string (value);
317       break;
318
319     case PROP_G_INTERFACE_NAME:
320       proxy->priv->interface_name = g_value_dup_string (value);
321       break;
322
323     case PROP_G_DEFAULT_TIMEOUT:
324       g_dbus_proxy_set_default_timeout (proxy, g_value_get_int (value));
325       break;
326
327     case PROP_G_INTERFACE_INFO:
328       g_dbus_proxy_set_interface_info (proxy, g_value_get_boxed (value));
329       break;
330
331     case PROP_G_BUS_TYPE:
332       proxy->priv->bus_type = g_value_get_enum (value);
333       break;
334
335     default:
336       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
337       break;
338     }
339 }
340
341 static void
342 g_dbus_proxy_class_init (GDBusProxyClass *klass)
343 {
344   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
345
346   gobject_class->dispose      = g_dbus_proxy_dispose;
347   gobject_class->finalize     = g_dbus_proxy_finalize;
348   gobject_class->set_property = g_dbus_proxy_set_property;
349   gobject_class->get_property = g_dbus_proxy_get_property;
350
351   /* Note that all property names are prefixed to avoid collisions with D-Bus property names
352    * in derived classes */
353
354   /**
355    * GDBusProxy:g-interface-info:
356    *
357    * Ensure that interactions with this proxy conform to the given
358    * interface.  For example, when completing a method call, if the
359    * type signature of the message isn't what's expected, the given
360    * #GError is set.  Signals that have a type signature mismatch are
361    * simply dropped.
362    *
363    * Since: 2.26
364    */
365   g_object_class_install_property (gobject_class,
366                                    PROP_G_INTERFACE_INFO,
367                                    g_param_spec_boxed ("g-interface-info",
368                                                        P_("Interface Information"),
369                                                        P_("Interface Information"),
370                                                        G_TYPE_DBUS_INTERFACE_INFO,
371                                                        G_PARAM_READABLE |
372                                                        G_PARAM_WRITABLE |
373                                                        G_PARAM_STATIC_NAME |
374                                                        G_PARAM_STATIC_BLURB |
375                                                        G_PARAM_STATIC_NICK));
376
377   /**
378    * GDBusProxy:g-connection:
379    *
380    * The #GDBusConnection the proxy is for.
381    *
382    * Since: 2.26
383    */
384   g_object_class_install_property (gobject_class,
385                                    PROP_G_CONNECTION,
386                                    g_param_spec_object ("g-connection",
387                                                         P_("g-connection"),
388                                                         P_("The connection the proxy is for"),
389                                                         G_TYPE_DBUS_CONNECTION,
390                                                         G_PARAM_READABLE |
391                                                         G_PARAM_WRITABLE |
392                                                         G_PARAM_CONSTRUCT_ONLY |
393                                                         G_PARAM_STATIC_NAME |
394                                                         G_PARAM_STATIC_BLURB |
395                                                         G_PARAM_STATIC_NICK));
396
397   /**
398    * GDBusProxy:g-bus-type:
399    *
400    * If this property is not %G_BUS_TYPE_NONE, then
401    * #GDBusProxy:g-connection must be %NULL and will be set to the
402    * #GDBusConnection obtained by calling g_bus_get() with the value
403    * of this property.
404    *
405    * Since: 2.26
406    */
407   g_object_class_install_property (gobject_class,
408                                    PROP_G_BUS_TYPE,
409                                    g_param_spec_enum ("g-bus-type",
410                                                       P_("Bus Type"),
411                                                       P_("The bus to connect to, if any"),
412                                                       G_TYPE_BUS_TYPE,
413                                                       G_BUS_TYPE_NONE,
414                                                       G_PARAM_WRITABLE |
415                                                       G_PARAM_CONSTRUCT_ONLY |
416                                                       G_PARAM_STATIC_NAME |
417                                                       G_PARAM_STATIC_BLURB |
418                                                       G_PARAM_STATIC_NICK));
419
420   /**
421    * GDBusProxy:g-flags:
422    *
423    * Flags from the #GDBusProxyFlags enumeration.
424    *
425    * Since: 2.26
426    */
427   g_object_class_install_property (gobject_class,
428                                    PROP_G_FLAGS,
429                                    g_param_spec_flags ("g-flags",
430                                                        P_("g-flags"),
431                                                        P_("Flags for the proxy"),
432                                                        G_TYPE_DBUS_PROXY_FLAGS,
433                                                        G_DBUS_PROXY_FLAGS_NONE,
434                                                        G_PARAM_READABLE |
435                                                        G_PARAM_WRITABLE |
436                                                        G_PARAM_CONSTRUCT_ONLY |
437                                                        G_PARAM_STATIC_NAME |
438                                                        G_PARAM_STATIC_BLURB |
439                                                        G_PARAM_STATIC_NICK));
440
441   /**
442    * GDBusProxy:g-name:
443    *
444    * The well-known or unique name that the proxy is for.
445    *
446    * Since: 2.26
447    */
448   g_object_class_install_property (gobject_class,
449                                    PROP_G_NAME,
450                                    g_param_spec_string ("g-name",
451                                                         P_("g-name"),
452                                                         P_("The well-known or unique name that the proxy is for"),
453                                                         NULL,
454                                                         G_PARAM_READABLE |
455                                                         G_PARAM_WRITABLE |
456                                                         G_PARAM_CONSTRUCT_ONLY |
457                                                         G_PARAM_STATIC_NAME |
458                                                         G_PARAM_STATIC_BLURB |
459                                                         G_PARAM_STATIC_NICK));
460
461   /**
462    * GDBusProxy:g-name-owner:
463    *
464    * The unique name that owns #GDBusProxy:name or %NULL if no-one
465    * currently owns that name. You may connect to #GObject::notify signal to
466    * track changes to this property.
467    *
468    * Since: 2.26
469    */
470   g_object_class_install_property (gobject_class,
471                                    PROP_G_NAME_OWNER,
472                                    g_param_spec_string ("g-name-owner",
473                                                         P_("g-name-owner"),
474                                                         P_("The unique name for the owner"),
475                                                         NULL,
476                                                         G_PARAM_READABLE |
477                                                         G_PARAM_STATIC_NAME |
478                                                         G_PARAM_STATIC_BLURB |
479                                                         G_PARAM_STATIC_NICK));
480
481   /**
482    * GDBusProxy:g-object-path:
483    *
484    * The object path the proxy is for.
485    *
486    * Since: 2.26
487    */
488   g_object_class_install_property (gobject_class,
489                                    PROP_G_OBJECT_PATH,
490                                    g_param_spec_string ("g-object-path",
491                                                         P_("g-object-path"),
492                                                         P_("The object path the proxy is for"),
493                                                         NULL,
494                                                         G_PARAM_READABLE |
495                                                         G_PARAM_WRITABLE |
496                                                         G_PARAM_CONSTRUCT_ONLY |
497                                                         G_PARAM_STATIC_NAME |
498                                                         G_PARAM_STATIC_BLURB |
499                                                         G_PARAM_STATIC_NICK));
500
501   /**
502    * GDBusProxy:g-interface-name:
503    *
504    * The D-Bus interface name the proxy is for.
505    *
506    * Since: 2.26
507    */
508   g_object_class_install_property (gobject_class,
509                                    PROP_G_INTERFACE_NAME,
510                                    g_param_spec_string ("g-interface-name",
511                                                         P_("g-interface-name"),
512                                                         P_("The D-Bus interface name the proxy is for"),
513                                                         NULL,
514                                                         G_PARAM_READABLE |
515                                                         G_PARAM_WRITABLE |
516                                                         G_PARAM_CONSTRUCT_ONLY |
517                                                         G_PARAM_STATIC_NAME |
518                                                         G_PARAM_STATIC_BLURB |
519                                                         G_PARAM_STATIC_NICK));
520
521   /**
522    * GDBusProxy:g-default-timeout:
523    *
524    * The timeout to use if -1 (specifying default timeout) is passed
525    * as @timeout_msec in the g_dbus_proxy_call() and
526    * g_dbus_proxy_call_sync() functions.
527    *
528    * This allows applications to set a proxy-wide timeout for all
529    * remote method invocations on the proxy. If this property is -1,
530    * the default timeout (typically 25 seconds) is used. If set to
531    * %G_MAXINT, then no timeout is used.
532    *
533    * Since: 2.26
534    */
535   g_object_class_install_property (gobject_class,
536                                    PROP_G_DEFAULT_TIMEOUT,
537                                    g_param_spec_int ("g-default-timeout",
538                                                      P_("Default Timeout"),
539                                                      P_("Timeout for remote method invocation"),
540                                                      -1,
541                                                      G_MAXINT,
542                                                      -1,
543                                                      G_PARAM_READABLE |
544                                                      G_PARAM_WRITABLE |
545                                                      G_PARAM_CONSTRUCT |
546                                                      G_PARAM_STATIC_NAME |
547                                                      G_PARAM_STATIC_BLURB |
548                                                      G_PARAM_STATIC_NICK));
549
550   /**
551    * GDBusProxy::g-properties-changed:
552    * @proxy: The #GDBusProxy emitting the signal.
553    * @changed_properties: A #GVariant containing the properties that changed
554    * @invalidated_properties: A %NULL terminated array of properties that was invalidated
555    *
556    * Emitted when one or more D-Bus properties on @proxy changes. The
557    * local cache has already been updated when this signal fires. Note
558    * that both @changed_properties and @invalidated_properties are
559    * guaranteed to never be %NULL (either may be empty though).
560    *
561    * This signal corresponds to the
562    * <literal>PropertiesChanged</literal> D-Bus signal on the
563    * <literal>org.freedesktop.DBus.Properties</literal> interface.
564    *
565    * Since: 2.26
566    */
567   signals[PROPERTIES_CHANGED_SIGNAL] = g_signal_new ("g-properties-changed",
568                                                      G_TYPE_DBUS_PROXY,
569                                                      G_SIGNAL_RUN_LAST | G_SIGNAL_MUST_COLLECT,
570                                                      G_STRUCT_OFFSET (GDBusProxyClass, g_properties_changed),
571                                                      NULL,
572                                                      NULL,
573                                                      NULL,
574                                                      G_TYPE_NONE,
575                                                      2,
576                                                      G_TYPE_VARIANT,
577                                                      G_TYPE_STRV | G_SIGNAL_TYPE_STATIC_SCOPE);
578
579   /**
580    * GDBusProxy::g-signal:
581    * @proxy: The #GDBusProxy emitting the signal.
582    * @sender_name: The sender of the signal or %NULL if the connection is not a bus connection.
583    * @signal_name: The name of the signal.
584    * @parameters: A #GVariant tuple with parameters for the signal.
585    *
586    * Emitted when a signal from the remote object and interface that @proxy is for, has been received.
587    *
588    * Since: 2.26
589    */
590   signals[SIGNAL_SIGNAL] = g_signal_new ("g-signal",
591                                          G_TYPE_DBUS_PROXY,
592                                          G_SIGNAL_RUN_LAST | G_SIGNAL_MUST_COLLECT,
593                                          G_STRUCT_OFFSET (GDBusProxyClass, g_signal),
594                                          NULL,
595                                          NULL,
596                                          NULL,
597                                          G_TYPE_NONE,
598                                          3,
599                                          G_TYPE_STRING,
600                                          G_TYPE_STRING,
601                                          G_TYPE_VARIANT);
602
603
604   g_type_class_add_private (klass, sizeof (GDBusProxyPrivate));
605 }
606
607 static void
608 g_dbus_proxy_init (GDBusProxy *proxy)
609 {
610   proxy->priv = G_TYPE_INSTANCE_GET_PRIVATE (proxy, G_TYPE_DBUS_PROXY, GDBusProxyPrivate);
611   proxy->priv->signal_subscription_data = g_slice_new0 (SignalSubscriptionData);
612   proxy->priv->signal_subscription_data->ref_count = 1;
613   proxy->priv->signal_subscription_data->proxy = proxy;
614   proxy->priv->properties = g_hash_table_new_full (g_str_hash,
615                                                    g_str_equal,
616                                                    g_free,
617                                                    (GDestroyNotify) g_variant_unref);
618 }
619
620 /* ---------------------------------------------------------------------------------------------------- */
621
622 static gint
623 property_name_sort_func (const gchar **a,
624                          const gchar **b)
625 {
626   return g_strcmp0 (*a, *b);
627 }
628
629 /**
630  * g_dbus_proxy_get_cached_property_names:
631  * @proxy: A #GDBusProxy.
632  *
633  * Gets the names of all cached properties on @proxy.
634  *
635  * Returns: A %NULL-terminated array of strings or %NULL if @proxy has
636  * no cached properties. Free the returned array with g_strfreev().
637  *
638  * Since: 2.26
639  */
640 gchar **
641 g_dbus_proxy_get_cached_property_names (GDBusProxy  *proxy)
642 {
643   gchar **names;
644   GPtrArray *p;
645   GHashTableIter iter;
646   const gchar *key;
647
648   g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
649
650   G_LOCK (properties_lock);
651
652   names = NULL;
653   if (g_hash_table_size (proxy->priv->properties) == 0)
654     goto out;
655
656   p = g_ptr_array_new ();
657
658   g_hash_table_iter_init (&iter, proxy->priv->properties);
659   while (g_hash_table_iter_next (&iter, (gpointer) &key, NULL))
660     g_ptr_array_add (p, g_strdup (key));
661   g_ptr_array_sort (p, (GCompareFunc) property_name_sort_func);
662   g_ptr_array_add (p, NULL);
663
664   names = (gchar **) g_ptr_array_free (p, FALSE);
665
666  out:
667   G_UNLOCK (properties_lock);
668   return names;
669 }
670
671 static const GDBusPropertyInfo *
672 lookup_property_info_or_warn (GDBusProxy  *proxy,
673                               const gchar *property_name)
674 {
675   const GDBusPropertyInfo *info;
676
677   if (proxy->priv->expected_interface == NULL)
678     return NULL;
679
680   info = g_dbus_interface_info_lookup_property (proxy->priv->expected_interface, property_name);
681   if (info == NULL)
682     {
683       g_warning ("Trying to lookup property %s which isn't in expected interface %s",
684                  property_name,
685                  proxy->priv->expected_interface->name);
686     }
687
688   return info;
689 }
690
691 /**
692  * g_dbus_proxy_get_cached_property:
693  * @proxy: A #GDBusProxy.
694  * @property_name: Property name.
695  *
696  * Looks up the value for a property from the cache. This call does no
697  * blocking IO.
698  *
699  * If @proxy has an expected interface (see
700  * #GDBusProxy:g-interface-info), then @property_name (for existence)
701  * is checked against it.
702  *
703  * Returns: A reference to the #GVariant instance that holds the value
704  * for @property_name or %NULL if the value is not in the cache. The
705  * returned reference must be freed with g_variant_unref().
706  *
707  * Since: 2.26
708  */
709 GVariant *
710 g_dbus_proxy_get_cached_property (GDBusProxy   *proxy,
711                                   const gchar  *property_name)
712 {
713   GVariant *value;
714
715   g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
716   g_return_val_if_fail (property_name != NULL, NULL);
717
718   G_LOCK (properties_lock);
719
720   value = g_hash_table_lookup (proxy->priv->properties, property_name);
721   if (value == NULL)
722     {
723       lookup_property_info_or_warn (proxy, property_name);
724       /* no difference */
725       goto out;
726     }
727
728   g_variant_ref (value);
729
730  out:
731   G_UNLOCK (properties_lock);
732   return value;
733 }
734
735 /**
736  * g_dbus_proxy_set_cached_property:
737  * @proxy: A #GDBusProxy
738  * @property_name: Property name.
739  * @value: (allow-none): Value for the property or %NULL to remove it from the cache.
740  *
741  * If @value is not %NULL, sets the cached value for the property with
742  * name @property_name to the value in @value.
743  *
744  * If @value is %NULL, then the cached value is removed from the
745  * property cache.
746  *
747  * If @proxy has an expected interface (see
748  * #GDBusProxy:g-interface-info), then @property_name (for existence)
749  * and @value (for the type) is checked against it.
750  *
751  * If the @value #GVariant is floating, it is consumed. This allows
752  * convenient 'inline' use of g_variant_new(), e.g.
753  * |[
754  *  g_dbus_proxy_set_cached_property (proxy,
755  *                                    "SomeProperty",
756  *                                    g_variant_new ("(si)",
757  *                                                  "A String",
758  *                                                  42));
759  * ]|
760  *
761  * Normally you will not need to use this method since @proxy is
762  * tracking changes using the
763  * <literal>org.freedesktop.DBus.Properties.PropertiesChanged</literal>
764  * D-Bus signal. However, for performance reasons an object may decide
765  * to not use this signal for some properties and instead use a
766  * proprietary out-of-band mechanism to transmit changes.
767  *
768  * As a concrete example, consider an object with a property
769  * <literal>ChatroomParticipants</literal> which is an array of
770  * strings. Instead of transmitting the same (long) array every time
771  * the property changes, it is more efficient to only transmit the
772  * delta using e.g. signals <literal>ChatroomParticipantJoined(String
773  * name)</literal> and <literal>ChatroomParticipantParted(String
774  * name)</literal>.
775  *
776  * Since: 2.26
777  */
778 void
779 g_dbus_proxy_set_cached_property (GDBusProxy   *proxy,
780                                   const gchar  *property_name,
781                                   GVariant     *value)
782 {
783   const GDBusPropertyInfo *info;
784
785   g_return_if_fail (G_IS_DBUS_PROXY (proxy));
786   g_return_if_fail (property_name != NULL);
787
788   G_LOCK (properties_lock);
789
790   if (value != NULL)
791     {
792       info = lookup_property_info_or_warn (proxy, property_name);
793       if (info != NULL)
794         {
795           if (g_strcmp0 (info->signature, g_variant_get_type_string (value)) != 0)
796             {
797               g_warning ("Trying to set property %s of type %s but according to the expected "
798                          "interface the type is %s",
799                          property_name,
800                          g_variant_get_type_string (value),
801                          info->signature);
802               goto out;
803             }
804         }
805       g_hash_table_insert (proxy->priv->properties,
806                            g_strdup (property_name),
807                            g_variant_ref_sink (value));
808     }
809   else
810     {
811       g_hash_table_remove (proxy->priv->properties, property_name);
812     }
813
814  out:
815   G_UNLOCK (properties_lock);
816 }
817
818 /* ---------------------------------------------------------------------------------------------------- */
819
820 static void
821 on_signal_received (GDBusConnection *connection,
822                     const gchar     *sender_name,
823                     const gchar     *object_path,
824                     const gchar     *interface_name,
825                     const gchar     *signal_name,
826                     GVariant        *parameters,
827                     gpointer         user_data)
828 {
829   SignalSubscriptionData *data = user_data;
830   GDBusProxy *proxy;
831
832   G_LOCK (signal_subscription_lock);
833   proxy = data->proxy;
834   if (proxy == NULL)
835     {
836       G_UNLOCK (signal_subscription_lock);
837       goto out;
838     }
839   else
840     {
841       g_object_ref (proxy);
842       G_UNLOCK (signal_subscription_lock);
843     }
844
845   if (!proxy->priv->initialized)
846     goto out;
847
848   if (proxy->priv->name_owner != NULL && g_strcmp0 (sender_name, proxy->priv->name_owner) != 0)
849     goto out;
850
851   if (proxy->priv->expected_interface != NULL)
852     {
853       const GDBusSignalInfo *info;
854       GVariantType *expected_type;
855       info = g_dbus_interface_info_lookup_signal (proxy->priv->expected_interface, signal_name);
856       if (info == NULL)
857         goto out;
858       expected_type = _g_dbus_compute_complete_signature (info->args);
859       if (!g_variant_type_equal (expected_type, g_variant_get_type (parameters)))
860         {
861           g_variant_type_free (expected_type);
862           goto out;
863         }
864       g_variant_type_free (expected_type);
865     }
866
867   g_signal_emit (proxy,
868                  signals[SIGNAL_SIGNAL],
869                  0,
870                  sender_name,
871                  signal_name,
872                  parameters);
873  out:
874   if (proxy != NULL)
875     g_object_unref (proxy);
876 }
877
878 /* ---------------------------------------------------------------------------------------------------- */
879
880 /* must hold properties_lock */
881 static void
882 insert_property_checked (GDBusProxy  *proxy,
883                          gchar *property_name,
884                          GVariant *value)
885 {
886   if (proxy->priv->expected_interface != NULL)
887     {
888       const GDBusPropertyInfo *info;
889
890       info = g_dbus_interface_info_lookup_property (proxy->priv->expected_interface, property_name);
891       /* Ignore unknown properties */
892       if (info == NULL)
893         goto invalid;
894
895       /* Ignore properties with the wrong type */
896       if (g_strcmp0 (info->signature, g_variant_get_type_string (value)) != 0)
897         goto invalid;
898     }
899
900   g_hash_table_insert (proxy->priv->properties,
901                        property_name, /* adopts string */
902                        value); /* adopts value */
903
904   return;
905
906  invalid:
907   g_variant_unref (value);
908   g_free (property_name);
909 }
910
911 static void
912 on_properties_changed (GDBusConnection *connection,
913                        const gchar     *sender_name,
914                        const gchar     *object_path,
915                        const gchar     *interface_name,
916                        const gchar     *signal_name,
917                        GVariant        *parameters,
918                        gpointer         user_data)
919 {
920   SignalSubscriptionData *data = user_data;
921   GDBusProxy *proxy;
922   const gchar *interface_name_for_signal;
923   GVariant *changed_properties;
924   gchar **invalidated_properties;
925   GVariantIter iter;
926   gchar *key;
927   GVariant *value;
928   guint n;
929
930   G_LOCK (signal_subscription_lock);
931   proxy = data->proxy;
932   if (proxy == NULL)
933     {
934       G_UNLOCK (signal_subscription_lock);
935       goto out;
936     }
937   else
938     {
939       g_object_ref (proxy);
940       G_UNLOCK (signal_subscription_lock);
941     }
942
943   changed_properties = NULL;
944   invalidated_properties = NULL;
945
946   if (!proxy->priv->initialized)
947     goto out;
948
949   if (proxy->priv->name_owner != NULL && g_strcmp0 (sender_name, proxy->priv->name_owner) != 0)
950     goto out;
951
952   if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(sa{sv}as)")))
953     {
954       g_warning ("Value for PropertiesChanged signal with type `%s' does not match `(sa{sv}as)'",
955                  g_variant_get_type_string (parameters));
956       goto out;
957     }
958
959   g_variant_get (parameters,
960                  "(&s@a{sv}^a&s)",
961                  &interface_name_for_signal,
962                  &changed_properties,
963                  &invalidated_properties);
964
965   if (g_strcmp0 (interface_name_for_signal, proxy->priv->interface_name) != 0)
966     goto out;
967
968   G_LOCK (properties_lock);
969
970   g_variant_iter_init (&iter, changed_properties);
971   while (g_variant_iter_next (&iter, "{sv}", &key, &value))
972     {
973       insert_property_checked (proxy,
974                                key, /* adopts string */
975                                value); /* adopts value */
976     }
977
978   for (n = 0; invalidated_properties[n] != NULL; n++)
979     {
980       g_hash_table_remove (proxy->priv->properties, invalidated_properties[n]);
981     }
982
983   G_UNLOCK (properties_lock);
984
985   /* emit signal */
986   g_signal_emit (proxy, signals[PROPERTIES_CHANGED_SIGNAL],
987                  0,
988                  changed_properties,
989                  invalidated_properties);
990
991  out:
992   if (changed_properties != NULL)
993     g_variant_unref (changed_properties);
994   g_free (invalidated_properties);
995   if (proxy != NULL)
996     g_object_unref (proxy);
997 }
998
999 /* ---------------------------------------------------------------------------------------------------- */
1000
1001 static void
1002 process_get_all_reply (GDBusProxy *proxy,
1003                        GVariant   *result)
1004 {
1005   GVariantIter *iter;
1006   gchar *key;
1007   GVariant *value;
1008   guint num_properties;
1009
1010   if (!g_variant_is_of_type (result, G_VARIANT_TYPE ("(a{sv})")))
1011     {
1012       g_warning ("Value for GetAll reply with type `%s' does not match `(a{sv})'",
1013                  g_variant_get_type_string (result));
1014       goto out;
1015     }
1016
1017   G_LOCK (properties_lock);
1018
1019   g_variant_get (result, "(a{sv})", &iter);
1020   while (g_variant_iter_next (iter, "{sv}", &key, &value))
1021     {
1022       insert_property_checked (proxy,
1023                                key, /* adopts string */
1024                                value); /* adopts value */
1025     }
1026   g_variant_iter_free (iter);
1027
1028   num_properties = g_hash_table_size (proxy->priv->properties);
1029   G_UNLOCK (properties_lock);
1030
1031   /* Synthesize ::g-properties-changed changed */
1032   if (num_properties > 0)
1033     {
1034       GVariant *changed_properties;
1035       const gchar *invalidated_properties[1] = {NULL};
1036
1037       g_variant_get (result,
1038                      "(@a{sv})",
1039                      &changed_properties);
1040       g_signal_emit (proxy, signals[PROPERTIES_CHANGED_SIGNAL],
1041                      0,
1042                      changed_properties,
1043                      invalidated_properties);
1044       g_variant_unref (changed_properties);
1045     }
1046
1047  out:
1048   ;
1049 }
1050
1051 typedef struct
1052 {
1053   GDBusProxy *proxy;
1054   GCancellable *cancellable;
1055   gchar *name_owner;
1056 } LoadPropertiesOnNameOwnerChangedData;
1057
1058 static void
1059 on_name_owner_changed_get_all_cb (GDBusConnection *connection,
1060                                   GAsyncResult    *res,
1061                                   gpointer         user_data)
1062 {
1063   LoadPropertiesOnNameOwnerChangedData *data = user_data;
1064   GVariant *result;
1065   GError *error;
1066   gboolean cancelled;
1067
1068   cancelled = FALSE;
1069
1070   error = NULL;
1071   result = g_dbus_connection_call_finish (connection,
1072                                           res,
1073                                           &error);
1074   if (result == NULL)
1075     {
1076       if (error->domain == G_IO_ERROR && error->code == G_IO_ERROR_CANCELLED)
1077         cancelled = TRUE;
1078       /* We just ignore if GetAll() is failing. Because this might happen
1079        * if the object has no properties at all. Or if the caller is
1080        * not authorized to see the properties.
1081        *
1082        * Either way, apps can know about this by using
1083        * get_cached_property_names() or get_cached_property().
1084        *
1085        * TODO: handle G_DBUS_DEBUG flag 'proxy' and, if enabled, log the
1086        * fact that GetAll() failed
1087        */
1088       //g_debug ("error: %d %d %s", error->domain, error->code, error->message);
1089       g_error_free (error);
1090     }
1091
1092   /* and finally we can notify */
1093   if (!cancelled)
1094     {
1095       g_free (data->proxy->priv->name_owner);
1096       data->proxy->priv->name_owner = data->name_owner;
1097       data->name_owner = NULL; /* to avoid an extra copy, we steal the string */
1098
1099       G_LOCK (properties_lock);
1100       g_hash_table_remove_all (data->proxy->priv->properties);
1101       G_UNLOCK (properties_lock);
1102       if (result != NULL)
1103         {
1104           process_get_all_reply (data->proxy, result);
1105           g_variant_unref (result);
1106         }
1107
1108       g_object_notify (G_OBJECT (data->proxy), "g-name-owner");
1109     }
1110
1111   if (data->cancellable == data->proxy->priv->get_all_cancellable)
1112     data->proxy->priv->get_all_cancellable = NULL;
1113
1114   g_object_unref (data->proxy);
1115   g_object_unref (data->cancellable);
1116   g_free (data->name_owner);
1117   g_free (data);
1118 }
1119
1120 static void
1121 on_name_owner_changed (GDBusConnection *connection,
1122                        const gchar      *sender_name,
1123                        const gchar      *object_path,
1124                        const gchar      *interface_name,
1125                        const gchar      *signal_name,
1126                        GVariant         *parameters,
1127                        gpointer          user_data)
1128 {
1129   SignalSubscriptionData *data = user_data;
1130   GDBusProxy *proxy;
1131   const gchar *old_owner;
1132   const gchar *new_owner;
1133
1134   G_LOCK (signal_subscription_lock);
1135   proxy = data->proxy;
1136   if (proxy == NULL)
1137     {
1138       G_UNLOCK (signal_subscription_lock);
1139       goto out;
1140     }
1141   else
1142     {
1143       g_object_ref (proxy);
1144       G_UNLOCK (signal_subscription_lock);
1145     }
1146
1147   /* if we are already trying to load properties, cancel that */
1148   if (proxy->priv->get_all_cancellable != NULL)
1149     {
1150       g_cancellable_cancel (proxy->priv->get_all_cancellable);
1151       proxy->priv->get_all_cancellable = NULL;
1152     }
1153
1154   g_variant_get (parameters,
1155                  "(&s&s&s)",
1156                  NULL,
1157                  &old_owner,
1158                  &new_owner);
1159
1160   if (strlen (new_owner) == 0)
1161     {
1162       g_free (proxy->priv->name_owner);
1163       proxy->priv->name_owner = NULL;
1164
1165       G_LOCK (properties_lock);
1166
1167       /* Synthesize ::g-properties-changed changed */
1168       if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES) &&
1169           g_hash_table_size (proxy->priv->properties) > 0)
1170         {
1171           GVariantBuilder builder;
1172           GPtrArray *invalidated_properties;
1173           GHashTableIter iter;
1174           const gchar *key;
1175
1176           /* Build changed_properties (always empty) and invalidated_properties ... */
1177           g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
1178
1179           invalidated_properties = g_ptr_array_new_with_free_func (g_free);
1180           g_hash_table_iter_init (&iter, proxy->priv->properties);
1181           while (g_hash_table_iter_next (&iter, (gpointer) &key, NULL))
1182             g_ptr_array_add (invalidated_properties, g_strdup (key));
1183           g_ptr_array_add (invalidated_properties, NULL);
1184
1185           /* ... throw out the properties ... */
1186           g_hash_table_remove_all (proxy->priv->properties);
1187
1188           G_UNLOCK (properties_lock);
1189
1190           /* ... and finally emit the ::g-properties-changed signal */
1191           g_signal_emit (proxy, signals[PROPERTIES_CHANGED_SIGNAL],
1192                          0,
1193                          g_variant_builder_end (&builder) /* consumed */,
1194                          (const gchar* const *) invalidated_properties->pdata);
1195           g_ptr_array_unref (invalidated_properties);
1196         }
1197       else
1198         {
1199           G_UNLOCK (properties_lock);
1200         }
1201       g_object_notify (G_OBJECT (proxy), "g-name-owner");
1202     }
1203   else
1204     {
1205       /* ignore duplicates - this can happen when activating the service */
1206       if (g_strcmp0 (new_owner, proxy->priv->name_owner) == 0)
1207         goto out;
1208
1209       if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES)
1210         {
1211           g_free (proxy->priv->name_owner);
1212           proxy->priv->name_owner = g_strdup (new_owner);
1213           G_LOCK (properties_lock);
1214           g_hash_table_remove_all (proxy->priv->properties);
1215           G_UNLOCK (properties_lock);
1216           g_object_notify (G_OBJECT (proxy), "g-name-owner");
1217         }
1218       else
1219         {
1220           LoadPropertiesOnNameOwnerChangedData *data;
1221
1222           /* start loading properties.. only then emit notify::g-name-owner .. we
1223            * need to be able to cancel this in the event another NameOwnerChanged
1224            * signal suddenly happens
1225            */
1226
1227           g_assert (proxy->priv->get_all_cancellable == NULL);
1228           proxy->priv->get_all_cancellable = g_cancellable_new ();
1229           data = g_new0 (LoadPropertiesOnNameOwnerChangedData, 1);
1230           data->proxy = g_object_ref (proxy);
1231           data->cancellable = proxy->priv->get_all_cancellable;
1232           data->name_owner = g_strdup (new_owner);
1233           g_dbus_connection_call (proxy->priv->connection,
1234                                   data->name_owner,
1235                                   proxy->priv->object_path,
1236                                   "org.freedesktop.DBus.Properties",
1237                                   "GetAll",
1238                                   g_variant_new ("(s)", proxy->priv->interface_name),
1239                                   G_VARIANT_TYPE ("(a{sv})"),
1240                                   G_DBUS_CALL_FLAGS_NONE,
1241                                   -1,           /* timeout */
1242                                   proxy->priv->get_all_cancellable,
1243                                   (GAsyncReadyCallback) on_name_owner_changed_get_all_cb,
1244                                   data);
1245         }
1246     }
1247
1248  out:
1249   if (proxy != NULL)
1250     g_object_unref (proxy);
1251 }
1252
1253 /* ---------------------------------------------------------------------------------------------------- */
1254
1255 typedef struct
1256 {
1257   GDBusProxy *proxy;
1258   GCancellable *cancellable;
1259   GSimpleAsyncResult *simple;
1260 } AsyncInitData;
1261
1262 static void
1263 async_init_data_free (AsyncInitData *data)
1264 {
1265   g_object_unref (data->proxy);
1266   if (data->cancellable != NULL)
1267     g_object_unref (data->cancellable);
1268   g_object_unref (data->simple);
1269   g_free (data);
1270 }
1271
1272 static void
1273 async_init_get_all_cb (GDBusConnection *connection,
1274                        GAsyncResult    *res,
1275                        gpointer         user_data)
1276 {
1277   AsyncInitData *data = user_data;
1278   GVariant *result;
1279   GError *error;
1280
1281   error = NULL;
1282   result = g_dbus_connection_call_finish (connection,
1283                                           res,
1284                                           &error);
1285   if (result == NULL)
1286     {
1287       /* We just ignore if GetAll() is failing. Because this might happen
1288        * if the object has no properties at all. Or if the caller is
1289        * not authorized to see the properties.
1290        *
1291        * Either way, apps can know about this by using
1292        * get_cached_property_names() or get_cached_property().
1293        *
1294        * TODO: handle G_DBUS_DEBUG flag 'proxy' and, if enabled, log the
1295        * fact that GetAll() failed
1296        */
1297       //g_debug ("error: %d %d %s", error->domain, error->code, error->message);
1298       g_error_free (error);
1299     }
1300   else
1301     {
1302       g_simple_async_result_set_op_res_gpointer (data->simple,
1303                                                  result,
1304                                                  (GDestroyNotify) g_variant_unref);
1305     }
1306
1307   g_simple_async_result_complete_in_idle (data->simple);
1308   async_init_data_free (data);
1309 }
1310
1311
1312 static void
1313 async_init_get_name_owner_cb (GDBusConnection *connection,
1314                               GAsyncResult    *res,
1315                               gpointer         user_data)
1316 {
1317   AsyncInitData *data = user_data;
1318
1319   if (res != NULL)
1320     {
1321       GError *error;
1322       GVariant *result;
1323
1324       error = NULL;
1325       result = g_dbus_connection_call_finish (connection,
1326                                               res,
1327                                               &error);
1328       if (result == NULL)
1329         {
1330           if (error->domain == G_DBUS_ERROR &&
1331               error->code == G_DBUS_ERROR_NAME_HAS_NO_OWNER)
1332             {
1333               g_error_free (error);
1334             }
1335           else
1336             {
1337               g_simple_async_result_take_error (data->simple, error);
1338               g_simple_async_result_complete_in_idle (data->simple);
1339               async_init_data_free (data);
1340               goto out;
1341             }
1342         }
1343       else
1344         {
1345           g_variant_get (result,
1346                          "(s)",
1347                          &data->proxy->priv->name_owner);
1348           g_variant_unref (result);
1349         }
1350     }
1351
1352   if (!(data->proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES))
1353     {
1354       /* load all properties asynchronously */
1355       g_dbus_connection_call (data->proxy->priv->connection,
1356                               data->proxy->priv->name_owner,
1357                               data->proxy->priv->object_path,
1358                               "org.freedesktop.DBus.Properties",
1359                               "GetAll",
1360                               g_variant_new ("(s)", data->proxy->priv->interface_name),
1361                               G_VARIANT_TYPE ("(a{sv})"),
1362                               G_DBUS_CALL_FLAGS_NONE,
1363                               -1,           /* timeout */
1364                               data->cancellable,
1365                               (GAsyncReadyCallback) async_init_get_all_cb,
1366                               data);
1367     }
1368   else
1369     {
1370       g_simple_async_result_complete_in_idle (data->simple);
1371       async_init_data_free (data);
1372     }
1373
1374  out:
1375   ;
1376 }
1377
1378 static void
1379 async_init_call_get_name_owner (AsyncInitData *data)
1380 {
1381   g_dbus_connection_call (data->proxy->priv->connection,
1382                           "org.freedesktop.DBus",  /* name */
1383                           "/org/freedesktop/DBus", /* object path */
1384                           "org.freedesktop.DBus",  /* interface */
1385                           "GetNameOwner",
1386                           g_variant_new ("(s)",
1387                                          data->proxy->priv->name),
1388                           G_VARIANT_TYPE ("(s)"),
1389                           G_DBUS_CALL_FLAGS_NONE,
1390                           -1,           /* timeout */
1391                           data->cancellable,
1392                           (GAsyncReadyCallback) async_init_get_name_owner_cb,
1393                           data);
1394 }
1395
1396 static void
1397 async_init_start_service_by_name_cb (GDBusConnection *connection,
1398                                      GAsyncResult    *res,
1399                                      gpointer         user_data)
1400 {
1401   AsyncInitData *data = user_data;
1402   GError *error;
1403   GVariant *result;
1404
1405   error = NULL;
1406   result = g_dbus_connection_call_finish (connection,
1407                                           res,
1408                                           &error);
1409   if (result == NULL)
1410     {
1411       /* Errors are not unexpected; the bus will reply e.g.
1412        *
1413        *   org.freedesktop.DBus.Error.ServiceUnknown: The name org.gnome.Epiphany2
1414        *   was not provided by any .service files
1415        *
1416        * This doesn't mean that the name doesn't have an owner, just
1417        * that it's not provided by a .service file. So just proceed to
1418        * invoke GetNameOwner() if dealing with that error.
1419        */
1420       if (error->domain == G_DBUS_ERROR &&
1421           error->code == G_DBUS_ERROR_SERVICE_UNKNOWN)
1422         {
1423           g_error_free (error);
1424         }
1425       else
1426         {
1427           g_prefix_error (&error,
1428                           _("Error calling StartServiceByName for %s: "),
1429                           data->proxy->priv->name);
1430           goto failed;
1431         }
1432     }
1433   else
1434     {
1435       guint32 start_service_result;
1436       g_variant_get (result,
1437                      "(u)",
1438                      &start_service_result);
1439       g_variant_unref (result);
1440       if (start_service_result == 1 ||  /* DBUS_START_REPLY_SUCCESS */
1441           start_service_result == 2)    /* DBUS_START_REPLY_ALREADY_RUNNING */
1442         {
1443           /* continue to invoke GetNameOwner() */
1444         }
1445       else
1446         {
1447           error = g_error_new (G_IO_ERROR,
1448                                G_IO_ERROR_FAILED,
1449                                _("Unexpected reply %d from StartServiceByName(\"%s\") method"),
1450                                start_service_result,
1451                                data->proxy->priv->name);
1452           goto failed;
1453         }
1454     }
1455
1456   async_init_call_get_name_owner (data);
1457   return;
1458
1459  failed:
1460   g_warn_if_fail (error != NULL);
1461   g_simple_async_result_take_error (data->simple, error);
1462   g_simple_async_result_complete_in_idle (data->simple);
1463   async_init_data_free (data);
1464 }
1465
1466 static void
1467 async_init_call_start_service_by_name (AsyncInitData *data)
1468 {
1469   g_dbus_connection_call (data->proxy->priv->connection,
1470                           "org.freedesktop.DBus",  /* name */
1471                           "/org/freedesktop/DBus", /* object path */
1472                           "org.freedesktop.DBus",  /* interface */
1473                           "StartServiceByName",
1474                           g_variant_new ("(su)",
1475                                          data->proxy->priv->name,
1476                                          0),
1477                           G_VARIANT_TYPE ("(u)"),
1478                           G_DBUS_CALL_FLAGS_NONE,
1479                           -1,           /* timeout */
1480                           data->cancellable,
1481                           (GAsyncReadyCallback) async_init_start_service_by_name_cb,
1482                           data);
1483 }
1484
1485 static void
1486 async_initable_init_second_async (GAsyncInitable      *initable,
1487                                   gint                 io_priority,
1488                                   GCancellable        *cancellable,
1489                                   GAsyncReadyCallback  callback,
1490                                   gpointer             user_data)
1491 {
1492   GDBusProxy *proxy = G_DBUS_PROXY (initable);
1493   AsyncInitData *data;
1494
1495   data = g_new0 (AsyncInitData, 1);
1496   data->proxy = g_object_ref (proxy);
1497   data->cancellable = cancellable != NULL ? g_object_ref (cancellable) : NULL;
1498   data->simple = g_simple_async_result_new (G_OBJECT (proxy),
1499                                             callback,
1500                                             user_data,
1501                                             NULL);
1502
1503   /* Check name ownership asynchronously - possibly also start the service */
1504   if (proxy->priv->name == NULL)
1505     {
1506       /* Do nothing */
1507       async_init_get_name_owner_cb (proxy->priv->connection, NULL, data);
1508     }
1509   else if (g_dbus_is_unique_name (proxy->priv->name))
1510     {
1511       proxy->priv->name_owner = g_strdup (proxy->priv->name);
1512       async_init_get_name_owner_cb (proxy->priv->connection, NULL, data);
1513     }
1514   else
1515     {
1516       if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START)
1517         {
1518           async_init_call_get_name_owner (data);
1519         }
1520       else
1521         {
1522           async_init_call_start_service_by_name (data);
1523         }
1524     }
1525 }
1526
1527 static gboolean
1528 async_initable_init_second_finish (GAsyncInitable  *initable,
1529                                    GAsyncResult    *res,
1530                                    GError         **error)
1531 {
1532   GDBusProxy *proxy = G_DBUS_PROXY (initable);
1533   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1534   GVariant *result;
1535   gboolean ret;
1536
1537   ret = FALSE;
1538
1539   if (g_simple_async_result_propagate_error (simple, error))
1540     goto out;
1541
1542   result = g_simple_async_result_get_op_res_gpointer (simple);
1543   if (result != NULL)
1544     {
1545       process_get_all_reply (proxy, result);
1546     }
1547
1548   ret = TRUE;
1549
1550  out:
1551   proxy->priv->initialized = TRUE;
1552   return ret;
1553 }
1554
1555 /* ---------------------------------------------------------------------------------------------------- */
1556
1557 static void
1558 async_initable_init_first (GAsyncInitable *initable)
1559 {
1560   GDBusProxy *proxy = G_DBUS_PROXY (initable);
1561
1562   if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES))
1563     {
1564       /* subscribe to PropertiesChanged() */
1565       proxy->priv->properties_changed_subscription_id =
1566         g_dbus_connection_signal_subscribe (proxy->priv->connection,
1567                                             proxy->priv->name,
1568                                             "org.freedesktop.DBus.Properties",
1569                                             "PropertiesChanged",
1570                                             proxy->priv->object_path,
1571                                             proxy->priv->interface_name,
1572                                             G_DBUS_SIGNAL_FLAGS_NONE,
1573                                             on_properties_changed,
1574                                             signal_subscription_ref (proxy->priv->signal_subscription_data),
1575                                             (GDestroyNotify) signal_subscription_unref);
1576     }
1577
1578   if (!(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS))
1579     {
1580       /* subscribe to all signals for the object */
1581       proxy->priv->signals_subscription_id =
1582         g_dbus_connection_signal_subscribe (proxy->priv->connection,
1583                                             proxy->priv->name,
1584                                             proxy->priv->interface_name,
1585                                             NULL,                        /* member */
1586                                             proxy->priv->object_path,
1587                                             NULL,                        /* arg0 */
1588                                             G_DBUS_SIGNAL_FLAGS_NONE,
1589                                             on_signal_received,
1590                                             signal_subscription_ref (proxy->priv->signal_subscription_data),
1591                                             (GDestroyNotify) signal_subscription_unref);
1592     }
1593
1594   if (proxy->priv->name != NULL && !g_dbus_is_unique_name (proxy->priv->name))
1595     {
1596       proxy->priv->name_owner_changed_subscription_id =
1597         g_dbus_connection_signal_subscribe (proxy->priv->connection,
1598                                             "org.freedesktop.DBus",  /* name */
1599                                             "org.freedesktop.DBus",  /* interface */
1600                                             "NameOwnerChanged",      /* signal name */
1601                                             "/org/freedesktop/DBus", /* path */
1602                                             proxy->priv->name,       /* arg0 */
1603                                             G_DBUS_SIGNAL_FLAGS_NONE,
1604                                             on_name_owner_changed,
1605                                             signal_subscription_ref (proxy->priv->signal_subscription_data),
1606                                             (GDestroyNotify) signal_subscription_unref);
1607     }
1608 }
1609
1610 /* ---------------------------------------------------------------------------------------------------- */
1611
1612 /* initialization is split into two parts - the first is the
1613  * non-blocing part that requires the callers GMainContext - the
1614  * second is a blocking part async part that doesn't require the
1615  * callers GMainContext.. we do this split so the code can be reused
1616  * in the GInitable implementation below.
1617  *
1618  * Note that obtaining a GDBusConnection is not shared between the two
1619  * paths.
1620  */
1621
1622 typedef struct
1623 {
1624   GDBusProxy          *proxy;
1625   gint                 io_priority;
1626   GCancellable        *cancellable;
1627   GAsyncReadyCallback  callback;
1628   gpointer             user_data;
1629 } GetConnectionData;
1630
1631 static void
1632 get_connection_cb (GObject       *source_object,
1633                    GAsyncResult  *res,
1634                    gpointer       user_data)
1635 {
1636   GetConnectionData *data = user_data;
1637   GError *error;
1638
1639   error = NULL;
1640   data->proxy->priv->connection = g_bus_get_finish (res, &error);
1641   if (data->proxy->priv->connection == NULL)
1642     {
1643       GSimpleAsyncResult *simple;
1644       simple = g_simple_async_result_new (G_OBJECT (data->proxy),
1645                                           data->callback,
1646                                           data->user_data,
1647                                           NULL);
1648       g_simple_async_result_take_error (simple, error);
1649       g_simple_async_result_complete_in_idle (simple);
1650       g_object_unref (simple);
1651     }
1652   else
1653     {
1654       async_initable_init_first (G_ASYNC_INITABLE (data->proxy));
1655       async_initable_init_second_async (G_ASYNC_INITABLE (data->proxy),
1656                                         data->io_priority,
1657                                         data->cancellable,
1658                                         data->callback,
1659                                         data->user_data);
1660     }
1661
1662   if (data->cancellable != NULL)
1663     g_object_unref (data->cancellable);
1664
1665   g_object_unref (data->proxy);
1666   g_free (data);
1667 }
1668
1669 static void
1670 async_initable_init_async (GAsyncInitable      *initable,
1671                            gint                 io_priority,
1672                            GCancellable        *cancellable,
1673                            GAsyncReadyCallback  callback,
1674                            gpointer             user_data)
1675 {
1676   GDBusProxy *proxy = G_DBUS_PROXY (initable);
1677
1678   if (proxy->priv->bus_type != G_BUS_TYPE_NONE)
1679     {
1680       GetConnectionData *data;
1681
1682       g_assert (proxy->priv->connection == NULL);
1683
1684       data = g_new0 (GetConnectionData, 1);
1685       data->proxy = g_object_ref (proxy);
1686       data->io_priority = io_priority;
1687       data->cancellable = cancellable != NULL ? g_object_ref (cancellable) : NULL;
1688       data->callback = callback;
1689       data->user_data = user_data;
1690       g_bus_get (proxy->priv->bus_type,
1691                  cancellable,
1692                  get_connection_cb,
1693                  data);
1694     }
1695   else
1696     {
1697       async_initable_init_first (initable);
1698       async_initable_init_second_async (initable, io_priority, cancellable, callback, user_data);
1699     }
1700 }
1701
1702 static gboolean
1703 async_initable_init_finish (GAsyncInitable  *initable,
1704                             GAsyncResult    *res,
1705                             GError         **error)
1706 {
1707   return async_initable_init_second_finish (initable, res, error);
1708 }
1709
1710 static void
1711 async_initable_iface_init (GAsyncInitableIface *async_initable_iface)
1712 {
1713   async_initable_iface->init_async = async_initable_init_async;
1714   async_initable_iface->init_finish = async_initable_init_finish;
1715 }
1716
1717 /* ---------------------------------------------------------------------------------------------------- */
1718
1719 typedef struct
1720 {
1721   GMainContext *context;
1722   GMainLoop *loop;
1723   GAsyncResult *res;
1724 } InitableAsyncInitableData;
1725
1726 static void
1727 async_initable_init_async_cb (GObject      *source_object,
1728                               GAsyncResult *res,
1729                               gpointer      user_data)
1730 {
1731   InitableAsyncInitableData *data = user_data;
1732   data->res = g_object_ref (res);
1733   g_main_loop_quit (data->loop);
1734 }
1735
1736 /* Simply reuse the GAsyncInitable implementation but run the first
1737  * part (that is non-blocking and requires the callers GMainContext)
1738  * with the callers GMainContext.. and the second with a private
1739  * GMainContext (bug 621310 is slightly related).
1740  *
1741  * Note that obtaining a GDBusConnection is not shared between the two
1742  * paths.
1743  */
1744 static gboolean
1745 initable_init (GInitable     *initable,
1746                GCancellable  *cancellable,
1747                GError       **error)
1748 {
1749   GDBusProxy *proxy = G_DBUS_PROXY (initable);
1750   InitableAsyncInitableData *data;
1751   gboolean ret;
1752
1753   ret = FALSE;
1754
1755   if (proxy->priv->bus_type != G_BUS_TYPE_NONE)
1756     {
1757       g_assert (proxy->priv->connection == NULL);
1758       proxy->priv->connection = g_bus_get_sync (proxy->priv->bus_type,
1759                                                 cancellable,
1760                                                 error);
1761       if (proxy->priv->connection == NULL)
1762         goto out;
1763     }
1764
1765   async_initable_init_first (G_ASYNC_INITABLE (initable));
1766
1767   data = g_new0 (InitableAsyncInitableData, 1);
1768   data->context = g_main_context_new ();
1769   data->loop = g_main_loop_new (data->context, FALSE);
1770
1771   g_main_context_push_thread_default (data->context);
1772
1773   async_initable_init_second_async (G_ASYNC_INITABLE (initable),
1774                                     G_PRIORITY_DEFAULT,
1775                                     cancellable,
1776                                     async_initable_init_async_cb,
1777                                     data);
1778
1779   g_main_loop_run (data->loop);
1780
1781   ret = async_initable_init_second_finish (G_ASYNC_INITABLE (initable),
1782                                            data->res,
1783                                            error);
1784
1785   g_main_context_pop_thread_default (data->context);
1786
1787   g_main_context_unref (data->context);
1788   g_main_loop_unref (data->loop);
1789   g_object_unref (data->res);
1790   g_free (data);
1791
1792  out:
1793
1794   return ret;
1795 }
1796
1797 static void
1798 initable_iface_init (GInitableIface *initable_iface)
1799 {
1800   initable_iface->init = initable_init;
1801 }
1802
1803 /* ---------------------------------------------------------------------------------------------------- */
1804
1805 /**
1806  * g_dbus_proxy_new:
1807  * @connection: A #GDBusConnection.
1808  * @flags: Flags used when constructing the proxy.
1809  * @info: (allow-none): A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
1810  * @name: (allow-none): A bus name (well-known or unique) or %NULL if @connection is not a message bus connection.
1811  * @object_path: An object path.
1812  * @interface_name: A D-Bus interface name.
1813  * @cancellable: A #GCancellable or %NULL.
1814  * @callback: Callback function to invoke when the proxy is ready.
1815  * @user_data: User data to pass to @callback.
1816  *
1817  * Creates a proxy for accessing @interface_name on the remote object
1818  * at @object_path owned by @name at @connection and asynchronously
1819  * loads D-Bus properties unless the
1820  * %G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES flag is used. Connect to
1821  * the #GDBusProxy::g-properties-changed signal to get notified about
1822  * property changes.
1823  *
1824  * If the %G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS flag is not set, also sets up
1825  * match rules for signals. Connect to the #GDBusProxy::g-signal signal
1826  * to handle signals from the remote object.
1827  *
1828  * If @name is a well-known name and the
1829  * %G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START flag isn't set and no name
1830  * owner currently exists, the message bus will be requested to launch
1831  * a name owner for the name.
1832  *
1833  * This is a failable asynchronous constructor - when the proxy is
1834  * ready, @callback will be invoked and you can use
1835  * g_dbus_proxy_new_finish() to get the result.
1836  *
1837  * See g_dbus_proxy_new_sync() and for a synchronous version of this constructor.
1838  *
1839  * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
1840  *
1841  * Since: 2.26
1842  */
1843 void
1844 g_dbus_proxy_new (GDBusConnection     *connection,
1845                   GDBusProxyFlags      flags,
1846                   GDBusInterfaceInfo  *info,
1847                   const gchar         *name,
1848                   const gchar         *object_path,
1849                   const gchar         *interface_name,
1850                   GCancellable        *cancellable,
1851                   GAsyncReadyCallback  callback,
1852                   gpointer             user_data)
1853 {
1854   g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
1855   g_return_if_fail ((name == NULL && g_dbus_connection_get_unique_name (connection) == NULL) || g_dbus_is_name (name));
1856   g_return_if_fail (g_variant_is_object_path (object_path));
1857   g_return_if_fail (g_dbus_is_interface_name (interface_name));
1858
1859   g_async_initable_new_async (G_TYPE_DBUS_PROXY,
1860                               G_PRIORITY_DEFAULT,
1861                               cancellable,
1862                               callback,
1863                               user_data,
1864                               "g-flags", flags,
1865                               "g-interface-info", info,
1866                               "g-name", name,
1867                               "g-connection", connection,
1868                               "g-object-path", object_path,
1869                               "g-interface-name", interface_name,
1870                               NULL);
1871 }
1872
1873 /**
1874  * g_dbus_proxy_new_finish:
1875  * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback function passed to g_dbus_proxy_new().
1876  * @error: Return location for error or %NULL.
1877  *
1878  * Finishes creating a #GDBusProxy.
1879  *
1880  * Returns: A #GDBusProxy or %NULL if @error is set. Free with g_object_unref().
1881  *
1882  * Since: 2.26
1883  */
1884 GDBusProxy *
1885 g_dbus_proxy_new_finish (GAsyncResult  *res,
1886                          GError       **error)
1887 {
1888   GObject *object;
1889   GObject *source_object;
1890
1891   source_object = g_async_result_get_source_object (res);
1892   g_assert (source_object != NULL);
1893
1894   object = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object),
1895                                         res,
1896                                         error);
1897   g_object_unref (source_object);
1898
1899   if (object != NULL)
1900     return G_DBUS_PROXY (object);
1901   else
1902     return NULL;
1903 }
1904
1905 /**
1906  * g_dbus_proxy_new_sync:
1907  * @connection: A #GDBusConnection.
1908  * @flags: Flags used when constructing the proxy.
1909  * @info: (allow-none): A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
1910  * @name: (allow-none): A bus name (well-known or unique) or %NULL if @connection is not a message bus connection.
1911  * @object_path: An object path.
1912  * @interface_name: A D-Bus interface name.
1913  * @cancellable: (allow-none): A #GCancellable or %NULL.
1914  * @error: (allow-none): Return location for error or %NULL.
1915  *
1916  * Creates a proxy for accessing @interface_name on the remote object
1917  * at @object_path owned by @name at @connection and synchronously
1918  * loads D-Bus properties unless the
1919  * %G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES flag is used.
1920  *
1921  * If the %G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS flag is not set, also sets up
1922  * match rules for signals. Connect to the #GDBusProxy::g-signal signal
1923  * to handle signals from the remote object.
1924  *
1925  * If @name is a well-known name and the
1926  * %G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START flag isn't set and no name
1927  * owner currently exists, the message bus will be requested to launch
1928  * a name owner for the name.
1929  *
1930  * This is a synchronous failable constructor. See g_dbus_proxy_new()
1931  * and g_dbus_proxy_new_finish() for the asynchronous version.
1932  *
1933  * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
1934  *
1935  * Returns: A #GDBusProxy or %NULL if error is set. Free with g_object_unref().
1936  *
1937  * Since: 2.26
1938  */
1939 GDBusProxy *
1940 g_dbus_proxy_new_sync (GDBusConnection     *connection,
1941                        GDBusProxyFlags      flags,
1942                        GDBusInterfaceInfo  *info,
1943                        const gchar         *name,
1944                        const gchar         *object_path,
1945                        const gchar         *interface_name,
1946                        GCancellable        *cancellable,
1947                        GError             **error)
1948 {
1949   GInitable *initable;
1950
1951   g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
1952   g_return_val_if_fail ((name == NULL && g_dbus_connection_get_unique_name (connection) == NULL) ||
1953                         g_dbus_is_name (name), NULL);
1954   g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
1955   g_return_val_if_fail (g_dbus_is_interface_name (interface_name), NULL);
1956
1957   initable = g_initable_new (G_TYPE_DBUS_PROXY,
1958                              cancellable,
1959                              error,
1960                              "g-flags", flags,
1961                              "g-interface-info", info,
1962                              "g-name", name,
1963                              "g-connection", connection,
1964                              "g-object-path", object_path,
1965                              "g-interface-name", interface_name,
1966                              NULL);
1967   if (initable != NULL)
1968     return G_DBUS_PROXY (initable);
1969   else
1970     return NULL;
1971 }
1972
1973 /* ---------------------------------------------------------------------------------------------------- */
1974
1975 /**
1976  * g_dbus_proxy_new_for_bus:
1977  * @bus_type: A #GBusType.
1978  * @flags: Flags used when constructing the proxy.
1979  * @info: (allow-none): A #GDBusInterfaceInfo specifying the minimal interface that @proxy conforms to or %NULL.
1980  * @name: A bus name (well-known or unique).
1981  * @object_path: An object path.
1982  * @interface_name: A D-Bus interface name.
1983  * @cancellable: A #GCancellable or %NULL.
1984  * @callback: Callback function to invoke when the proxy is ready.
1985  * @user_data: User data to pass to @callback.
1986  *
1987  * Like g_dbus_proxy_new() but takes a #GBusType instead of a #GDBusConnection.
1988  *
1989  * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
1990  *
1991  * Since: 2.26
1992  */
1993 void
1994 g_dbus_proxy_new_for_bus (GBusType             bus_type,
1995                           GDBusProxyFlags      flags,
1996                           GDBusInterfaceInfo  *info,
1997                           const gchar         *name,
1998                           const gchar         *object_path,
1999                           const gchar         *interface_name,
2000                           GCancellable        *cancellable,
2001                           GAsyncReadyCallback  callback,
2002                           gpointer             user_data)
2003 {
2004   g_return_if_fail (g_dbus_is_name (name));
2005   g_return_if_fail (g_variant_is_object_path (object_path));
2006   g_return_if_fail (g_dbus_is_interface_name (interface_name));
2007
2008   g_async_initable_new_async (G_TYPE_DBUS_PROXY,
2009                               G_PRIORITY_DEFAULT,
2010                               cancellable,
2011                               callback,
2012                               user_data,
2013                               "g-flags", flags,
2014                               "g-interface-info", info,
2015                               "g-name", name,
2016                               "g-bus-type", bus_type,
2017                               "g-object-path", object_path,
2018                               "g-interface-name", interface_name,
2019                               NULL);
2020 }
2021
2022 /**
2023  * g_dbus_proxy_new_for_bus_finish:
2024  * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback function passed to g_dbus_proxy_new_for_bus().
2025  * @error: Return location for error or %NULL.
2026  *
2027  * Finishes creating a #GDBusProxy.
2028  *
2029  * Returns: A #GDBusProxy or %NULL if @error is set. Free with g_object_unref().
2030  *
2031  * Since: 2.26
2032  */
2033 GDBusProxy *
2034 g_dbus_proxy_new_for_bus_finish (GAsyncResult  *res,
2035                                  GError       **error)
2036 {
2037   return g_dbus_proxy_new_finish (res, error);
2038 }
2039
2040 /**
2041  * g_dbus_proxy_new_for_bus_sync:
2042  * @bus_type: A #GBusType.
2043  * @flags: Flags used when constructing the proxy.
2044  * @info: (allow-none): A #GDBusInterfaceInfo specifying the minimal interface
2045  *        that @proxy conforms to or %NULL.
2046  * @name: A bus name (well-known or unique).
2047  * @object_path: An object path.
2048  * @interface_name: A D-Bus interface name.
2049  * @cancellable: A #GCancellable or %NULL.
2050  * @error: Return location for error or %NULL.
2051  *
2052  * Like g_dbus_proxy_new_sync() but takes a #GBusType instead of a #GDBusConnection.
2053  *
2054  * See <xref linkend="gdbus-wellknown-proxy"/> for an example of how #GDBusProxy can be used.
2055  *
2056  * Returns: A #GDBusProxy or %NULL if error is set. Free with g_object_unref().
2057  *
2058  * Since: 2.26
2059  */
2060 GDBusProxy *
2061 g_dbus_proxy_new_for_bus_sync (GBusType             bus_type,
2062                                GDBusProxyFlags      flags,
2063                                GDBusInterfaceInfo  *info,
2064                                const gchar         *name,
2065                                const gchar         *object_path,
2066                                const gchar         *interface_name,
2067                                GCancellable        *cancellable,
2068                                GError             **error)
2069 {
2070   GInitable *initable;
2071
2072   g_return_val_if_fail (g_dbus_is_name (name), NULL);
2073   g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
2074   g_return_val_if_fail (g_dbus_is_interface_name (interface_name), NULL);
2075
2076   initable = g_initable_new (G_TYPE_DBUS_PROXY,
2077                              cancellable,
2078                              error,
2079                              "g-flags", flags,
2080                              "g-interface-info", info,
2081                              "g-name", name,
2082                              "g-bus-type", bus_type,
2083                              "g-object-path", object_path,
2084                              "g-interface-name", interface_name,
2085                              NULL);
2086   if (initable != NULL)
2087     return G_DBUS_PROXY (initable);
2088   else
2089     return NULL;
2090 }
2091
2092 /* ---------------------------------------------------------------------------------------------------- */
2093
2094 /**
2095  * g_dbus_proxy_get_connection:
2096  * @proxy: A #GDBusProxy.
2097  *
2098  * Gets the connection @proxy is for.
2099  *
2100  * Returns: (transfer none): A #GDBusConnection owned by @proxy. Do not free.
2101  *
2102  * Since: 2.26
2103  */
2104 GDBusConnection *
2105 g_dbus_proxy_get_connection (GDBusProxy *proxy)
2106 {
2107   g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2108   return proxy->priv->connection;
2109 }
2110
2111 /**
2112  * g_dbus_proxy_get_flags:
2113  * @proxy: A #GDBusProxy.
2114  *
2115  * Gets the flags that @proxy was constructed with.
2116  *
2117  * Returns: Flags from the #GDBusProxyFlags enumeration.
2118  *
2119  * Since: 2.26
2120  */
2121 GDBusProxyFlags
2122 g_dbus_proxy_get_flags (GDBusProxy *proxy)
2123 {
2124   g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), 0);
2125   return proxy->priv->flags;
2126 }
2127
2128 /**
2129  * g_dbus_proxy_get_name:
2130  * @proxy: A #GDBusProxy.
2131  *
2132  * Gets the name that @proxy was constructed for.
2133  *
2134  * Returns: A string owned by @proxy. Do not free.
2135  *
2136  * Since: 2.26
2137  */
2138 const gchar *
2139 g_dbus_proxy_get_name (GDBusProxy *proxy)
2140 {
2141   g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2142   return proxy->priv->name;
2143 }
2144
2145 /**
2146  * g_dbus_proxy_get_name_owner:
2147  * @proxy: A #GDBusProxy.
2148  *
2149  * The unique name that owns the name that @proxy is for or %NULL if
2150  * no-one currently owns that name. You may connect to the
2151  * #GObject::notify signal to track changes to the
2152  * #GDBusProxy:g-name-owner property.
2153  *
2154  * Returns: The name owner or %NULL if no name owner exists. Free with g_free().
2155  *
2156  * Since: 2.26
2157  */
2158 gchar *
2159 g_dbus_proxy_get_name_owner (GDBusProxy *proxy)
2160 {
2161   g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2162   return g_strdup (proxy->priv->name_owner);
2163 }
2164
2165 /**
2166  * g_dbus_proxy_get_object_path:
2167  * @proxy: A #GDBusProxy.
2168  *
2169  * Gets the object path @proxy is for.
2170  *
2171  * Returns: A string owned by @proxy. Do not free.
2172  *
2173  * Since: 2.26
2174  */
2175 const gchar *
2176 g_dbus_proxy_get_object_path (GDBusProxy *proxy)
2177 {
2178   g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2179   return proxy->priv->object_path;
2180 }
2181
2182 /**
2183  * g_dbus_proxy_get_interface_name:
2184  * @proxy: A #GDBusProxy.
2185  *
2186  * Gets the D-Bus interface name @proxy is for.
2187  *
2188  * Returns: A string owned by @proxy. Do not free.
2189  *
2190  * Since: 2.26
2191  */
2192 const gchar *
2193 g_dbus_proxy_get_interface_name (GDBusProxy *proxy)
2194 {
2195   g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2196   return proxy->priv->interface_name;
2197 }
2198
2199 /**
2200  * g_dbus_proxy_get_default_timeout:
2201  * @proxy: A #GDBusProxy.
2202  *
2203  * Gets the timeout to use if -1 (specifying default timeout) is
2204  * passed as @timeout_msec in the g_dbus_proxy_call() and
2205  * g_dbus_proxy_call_sync() functions.
2206  *
2207  * See the #GDBusProxy:g-default-timeout property for more details.
2208  *
2209  * Returns: Timeout to use for @proxy.
2210  *
2211  * Since: 2.26
2212  */
2213 gint
2214 g_dbus_proxy_get_default_timeout (GDBusProxy *proxy)
2215 {
2216   g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), -1);
2217   return proxy->priv->timeout_msec;
2218 }
2219
2220 /**
2221  * g_dbus_proxy_set_default_timeout:
2222  * @proxy: A #GDBusProxy.
2223  * @timeout_msec: Timeout in milliseconds.
2224  *
2225  * Sets the timeout to use if -1 (specifying default timeout) is
2226  * passed as @timeout_msec in the g_dbus_proxy_call() and
2227  * g_dbus_proxy_call_sync() functions.
2228  *
2229  * See the #GDBusProxy:g-default-timeout property for more details.
2230  *
2231  * Since: 2.26
2232  */
2233 void
2234 g_dbus_proxy_set_default_timeout (GDBusProxy *proxy,
2235                                   gint        timeout_msec)
2236 {
2237   g_return_if_fail (G_IS_DBUS_PROXY (proxy));
2238   g_return_if_fail (timeout_msec == -1 || timeout_msec >= 0);
2239
2240   /* TODO: locking? */
2241   if (proxy->priv->timeout_msec != timeout_msec)
2242     {
2243       proxy->priv->timeout_msec = timeout_msec;
2244       g_object_notify (G_OBJECT (proxy), "g-default-timeout");
2245     }
2246 }
2247
2248 /**
2249  * g_dbus_proxy_get_interface_info:
2250  * @proxy: A #GDBusProxy
2251  *
2252  * Returns the #GDBusInterfaceInfo, if any, specifying the minimal
2253  * interface that @proxy conforms to.
2254  *
2255  * See the #GDBusProxy:g-interface-info property for more details.
2256  *
2257  * Returns: A #GDBusInterfaceInfo or %NULL. Do not unref the returned
2258  * object, it is owned by @proxy.
2259  *
2260  * Since: 2.26
2261  */
2262 GDBusInterfaceInfo *
2263 g_dbus_proxy_get_interface_info (GDBusProxy *proxy)
2264 {
2265   g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2266   return proxy->priv->expected_interface;
2267 }
2268
2269 /**
2270  * g_dbus_proxy_set_interface_info:
2271  * @proxy: A #GDBusProxy
2272  * @info: (allow-none): Minimum interface this proxy conforms to or %NULL to unset.
2273  *
2274  * Ensure that interactions with @proxy conform to the given
2275  * interface.  For example, when completing a method call, if the type
2276  * signature of the message isn't what's expected, the given #GError
2277  * is set.  Signals that have a type signature mismatch are simply
2278  * dropped.
2279  *
2280  * See the #GDBusProxy:g-interface-info property for more details.
2281  *
2282  * Since: 2.26
2283  */
2284 void
2285 g_dbus_proxy_set_interface_info (GDBusProxy         *proxy,
2286                                  GDBusInterfaceInfo *info)
2287 {
2288   g_return_if_fail (G_IS_DBUS_PROXY (proxy));
2289   if (proxy->priv->expected_interface != NULL)
2290     {
2291       g_dbus_interface_info_cache_release (proxy->priv->expected_interface);
2292       g_dbus_interface_info_unref (proxy->priv->expected_interface);
2293     }
2294   proxy->priv->expected_interface = info != NULL ? g_dbus_interface_info_ref (info) : NULL;
2295   if (proxy->priv->expected_interface != NULL)
2296     g_dbus_interface_info_cache_build (proxy->priv->expected_interface);
2297 }
2298
2299 /* ---------------------------------------------------------------------------------------------------- */
2300
2301 static gboolean
2302 maybe_split_method_name (const gchar  *method_name,
2303                          gchar       **out_interface_name,
2304                          const gchar **out_method_name)
2305 {
2306   gboolean was_split;
2307
2308   was_split = FALSE;
2309   g_assert (out_interface_name != NULL);
2310   g_assert (out_method_name != NULL);
2311   *out_interface_name = NULL;
2312   *out_method_name = NULL;
2313
2314   if (strchr (method_name, '.') != NULL)
2315     {
2316       gchar *p;
2317       gchar *last_dot;
2318
2319       p = g_strdup (method_name);
2320       last_dot = strrchr (p, '.');
2321       *last_dot = '\0';
2322
2323       *out_interface_name = p;
2324       *out_method_name = last_dot + 1;
2325
2326       was_split = TRUE;
2327     }
2328
2329   return was_split;
2330 }
2331
2332 typedef struct
2333 {
2334   GVariant *value;
2335 #ifdef G_OS_UNIX
2336   GUnixFDList *fd_list;
2337 #endif
2338 } ReplyData;
2339
2340 static void
2341 reply_data_free (ReplyData *data)
2342 {
2343   g_variant_unref (data->value);
2344 #ifdef G_OS_UNIX
2345   if (data->fd_list != NULL)
2346     g_object_unref (data->fd_list);
2347 #endif
2348   g_slice_free (ReplyData, data);
2349 }
2350
2351 static void
2352 reply_cb (GDBusConnection *connection,
2353           GAsyncResult    *res,
2354           gpointer         user_data)
2355 {
2356   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
2357   GVariant *value;
2358   GError *error;
2359 #ifdef G_OS_UNIX
2360   GUnixFDList *fd_list;
2361 #endif
2362
2363   error = NULL;
2364 #ifdef G_OS_UNIX
2365   value = g_dbus_connection_call_with_unix_fd_list_finish (connection,
2366                                                            &fd_list,
2367                                                            res,
2368                                                            &error);
2369 #else
2370   value = g_dbus_connection_call_finish (connection,
2371                                          res,
2372                                          &error);
2373 #endif
2374   if (error != NULL)
2375     {
2376       g_simple_async_result_take_error (simple, error);
2377     }
2378   else
2379     {
2380       ReplyData *data;
2381       data = g_slice_new0 (ReplyData);
2382       data->value = value;
2383 #ifdef G_OS_UNIX
2384       data->fd_list = fd_list;
2385 #endif
2386       g_simple_async_result_set_op_res_gpointer (simple, data, (GDestroyNotify) reply_data_free);
2387     }
2388
2389   /* no need to complete in idle since the method GDBusConnection already does */
2390   g_simple_async_result_complete (simple);
2391   g_object_unref (simple);
2392 }
2393
2394 static const GDBusMethodInfo *
2395 lookup_method_info_or_warn (GDBusProxy  *proxy,
2396                             const gchar *method_name)
2397 {
2398   const GDBusMethodInfo *info;
2399
2400   if (proxy->priv->expected_interface == NULL)
2401     return NULL;
2402
2403   info = g_dbus_interface_info_lookup_method (proxy->priv->expected_interface, method_name);
2404   if (info == NULL)
2405     {
2406       g_warning ("Trying to invoke method %s which isn't in expected interface %s",
2407                  method_name, proxy->priv->expected_interface->name);
2408     }
2409
2410   return info;
2411 }
2412
2413 static const gchar *
2414 get_destination_for_call (GDBusProxy *proxy)
2415 {
2416   const gchar *ret;
2417
2418   ret = NULL;
2419
2420   /* If proxy->priv->name is a unique name, then proxy->priv->name_owner
2421    * is never NULL and always the same as proxy->priv->name. We use this
2422    * knowledge to avoid checking if proxy->priv->name is a unique or
2423    * well-known name.
2424    */
2425   ret = proxy->priv->name_owner;
2426   if (ret != NULL)
2427     goto out;
2428
2429   if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START)
2430     goto out;
2431
2432   ret = proxy->priv->name;
2433
2434  out:
2435   return ret;
2436 }
2437
2438 /* ---------------------------------------------------------------------------------------------------- */
2439
2440 static void
2441 g_dbus_proxy_call_internal (GDBusProxy          *proxy,
2442                             const gchar         *method_name,
2443                             GVariant            *parameters,
2444                             GDBusCallFlags       flags,
2445                             gint                 timeout_msec,
2446                             GUnixFDList         *fd_list,
2447                             GCancellable        *cancellable,
2448                             GAsyncReadyCallback  callback,
2449                             gpointer             user_data)
2450 {
2451   GSimpleAsyncResult *simple;
2452   gboolean was_split;
2453   gchar *split_interface_name;
2454   const gchar *split_method_name;
2455   const gchar *target_method_name;
2456   const gchar *target_interface_name;
2457   const gchar *destination;
2458   GVariantType *reply_type;
2459
2460   g_return_if_fail (G_IS_DBUS_PROXY (proxy));
2461   g_return_if_fail (g_dbus_is_member_name (method_name) || g_dbus_is_interface_name (method_name));
2462   g_return_if_fail (parameters == NULL || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE));
2463   g_return_if_fail (timeout_msec == -1 || timeout_msec >= 0);
2464 #ifdef G_OS_UNIX
2465   g_return_if_fail (fd_list == NULL || G_IS_UNIX_FD_LIST (fd_list));
2466 #else
2467   g_return_if_fail (fd_list == NULL);
2468 #endif
2469
2470   reply_type = NULL;
2471   split_interface_name = NULL;
2472
2473   simple = g_simple_async_result_new (G_OBJECT (proxy),
2474                                       callback,
2475                                       user_data,
2476                                       g_dbus_proxy_call_internal);
2477
2478   was_split = maybe_split_method_name (method_name, &split_interface_name, &split_method_name);
2479   target_method_name = was_split ? split_method_name : method_name;
2480   target_interface_name = was_split ? split_interface_name : proxy->priv->interface_name;
2481
2482   /* Warn if method is unexpected (cf. :g-interface-info) */
2483   if (!was_split)
2484     {
2485       const GDBusMethodInfo *expected_method_info;
2486       expected_method_info = lookup_method_info_or_warn (proxy, target_method_name);
2487       if (expected_method_info != NULL)
2488         reply_type = _g_dbus_compute_complete_signature (expected_method_info->out_args);
2489     }
2490
2491   destination = NULL;
2492   if (proxy->priv->name != NULL)
2493     {
2494       destination = get_destination_for_call (proxy);
2495       if (destination == NULL)
2496         {
2497           g_simple_async_result_set_error (simple,
2498                                            G_IO_ERROR,
2499                                            G_IO_ERROR_FAILED,
2500                                            _("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"));
2501           goto out;
2502         }
2503     }
2504
2505 #ifdef G_OS_UNIX
2506   g_dbus_connection_call_with_unix_fd_list (proxy->priv->connection,
2507                                             destination,
2508                                             proxy->priv->object_path,
2509                                             target_interface_name,
2510                                             target_method_name,
2511                                             parameters,
2512                                             reply_type,
2513                                             flags,
2514                                             timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2515                                             fd_list,
2516                                             cancellable,
2517                                             (GAsyncReadyCallback) reply_cb,
2518                                             simple);
2519 #else
2520   g_dbus_connection_call (proxy->priv->connection,
2521                           destination,
2522                           proxy->priv->object_path,
2523                           target_interface_name,
2524                           target_method_name,
2525                           parameters,
2526                           reply_type,
2527                           flags,
2528                           timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2529                           cancellable,
2530                           (GAsyncReadyCallback) reply_cb,
2531                           simple);
2532 #endif
2533
2534  out:
2535   if (reply_type != NULL)
2536     g_variant_type_free (reply_type);
2537
2538   g_free (split_interface_name);
2539 }
2540
2541 static GVariant *
2542 g_dbus_proxy_call_finish_internal (GDBusProxy    *proxy,
2543                                    GUnixFDList  **out_fd_list,
2544                                    GAsyncResult  *res,
2545                                    GError       **error)
2546 {
2547   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2548   GVariant *value;
2549   ReplyData *data;
2550
2551   g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2552   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2553   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2554
2555   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_dbus_proxy_call_internal);
2556
2557   value = NULL;
2558
2559   if (g_simple_async_result_propagate_error (simple, error))
2560     goto out;
2561
2562   data = g_simple_async_result_get_op_res_gpointer (simple);
2563   value = g_variant_ref (data->value);
2564 #ifdef G_OS_UNIX
2565   if (out_fd_list != NULL)
2566     *out_fd_list = data->fd_list != NULL ? g_object_ref (data->fd_list) : NULL;
2567 #endif
2568
2569  out:
2570   return value;
2571 }
2572
2573 static GVariant *
2574 g_dbus_proxy_call_sync_internal (GDBusProxy      *proxy,
2575                                  const gchar     *method_name,
2576                                  GVariant        *parameters,
2577                                  GDBusCallFlags   flags,
2578                                  gint             timeout_msec,
2579                                  GUnixFDList     *fd_list,
2580                                  GUnixFDList    **out_fd_list,
2581                                  GCancellable    *cancellable,
2582                                  GError         **error)
2583 {
2584   GVariant *ret;
2585   gboolean was_split;
2586   gchar *split_interface_name;
2587   const gchar *split_method_name;
2588   const gchar *target_method_name;
2589   const gchar *target_interface_name;
2590   const gchar *destination;
2591   GVariantType *reply_type;
2592
2593   g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
2594   g_return_val_if_fail (g_dbus_is_member_name (method_name) || g_dbus_is_interface_name (method_name), NULL);
2595   g_return_val_if_fail (parameters == NULL || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE), NULL);
2596   g_return_val_if_fail (timeout_msec == -1 || timeout_msec >= 0, NULL);
2597 #ifdef G_OS_UNIX
2598   g_return_val_if_fail (fd_list == NULL || G_IS_UNIX_FD_LIST (fd_list), NULL);
2599 #else
2600   g_return_val_if_fail (fd_list == NULL, NULL);
2601 #endif
2602   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2603
2604   reply_type = NULL;
2605
2606   was_split = maybe_split_method_name (method_name, &split_interface_name, &split_method_name);
2607   target_method_name = was_split ? split_method_name : method_name;
2608   target_interface_name = was_split ? split_interface_name : proxy->priv->interface_name;
2609
2610   /* Warn if method is unexpected (cf. :g-interface-info) */
2611   if (!was_split)
2612     {
2613       const GDBusMethodInfo *expected_method_info;
2614       expected_method_info = lookup_method_info_or_warn (proxy, target_method_name);
2615       if (expected_method_info != NULL)
2616         reply_type = _g_dbus_compute_complete_signature (expected_method_info->out_args);
2617     }
2618
2619   destination = NULL;
2620   if (proxy->priv->name != NULL)
2621     {
2622       destination = get_destination_for_call (proxy);
2623       if (destination == NULL)
2624         {
2625           g_set_error_literal (error,
2626                                G_IO_ERROR,
2627                                G_IO_ERROR_FAILED,
2628                                _("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"));
2629           ret = NULL;
2630           goto out;
2631         }
2632     }
2633
2634 #ifdef G_OS_UNIX
2635   ret = g_dbus_connection_call_with_unix_fd_list_sync (proxy->priv->connection,
2636                                                        destination,
2637                                                        proxy->priv->object_path,
2638                                                        target_interface_name,
2639                                                        target_method_name,
2640                                                        parameters,
2641                                                        reply_type,
2642                                                        flags,
2643                                                        timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2644                                                        fd_list,
2645                                                        out_fd_list,
2646                                                        cancellable,
2647                                                        error);
2648 #else
2649   ret = g_dbus_connection_call_sync (proxy->priv->connection,
2650                                      destination,
2651                                      proxy->priv->object_path,
2652                                      target_interface_name,
2653                                      target_method_name,
2654                                      parameters,
2655                                      reply_type,
2656                                      flags,
2657                                      timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
2658                                      cancellable,
2659                                      error);
2660 #endif
2661
2662  out:
2663   if (reply_type != NULL)
2664     g_variant_type_free (reply_type);
2665
2666   g_free (split_interface_name);
2667
2668   return ret;
2669 }
2670
2671 /* ---------------------------------------------------------------------------------------------------- */
2672
2673 /**
2674  * g_dbus_proxy_call:
2675  * @proxy: A #GDBusProxy.
2676  * @method_name: Name of method to invoke.
2677  * @parameters: (allow-none): A #GVariant tuple with parameters for the signal or %NULL if not passing parameters.
2678  * @flags: Flags from the #GDBusCallFlags enumeration.
2679  * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
2680  *                "infinite") or -1 to use the proxy default timeout.
2681  * @cancellable: A #GCancellable or %NULL.
2682  * @callback: A #GAsyncReadyCallback to call when the request is satisfied or %NULL if you don't
2683  * care about the result of the method invocation.
2684  * @user_data: The data to pass to @callback.
2685  *
2686  * Asynchronously invokes the @method_name method on @proxy.
2687  *
2688  * If @method_name contains any dots, then @name is split into interface and
2689  * method name parts. This allows using @proxy for invoking methods on
2690  * other interfaces.
2691  *
2692  * If the #GDBusConnection associated with @proxy is closed then
2693  * the operation will fail with %G_IO_ERROR_CLOSED. If
2694  * @cancellable is canceled, the operation will fail with
2695  * %G_IO_ERROR_CANCELLED. If @parameters contains a value not
2696  * compatible with the D-Bus protocol, the operation fails with
2697  * %G_IO_ERROR_INVALID_ARGUMENT.
2698  *
2699  * If the @parameters #GVariant is floating, it is consumed. This allows
2700  * convenient 'inline' use of g_variant_new(), e.g.:
2701  * |[
2702  *  g_dbus_proxy_call (proxy,
2703  *                     "TwoStrings",
2704  *                     g_variant_new ("(ss)",
2705  *                                    "Thing One",
2706  *                                    "Thing Two"),
2707  *                     G_DBUS_CALL_FLAGS_NONE,
2708  *                     -1,
2709  *                     NULL,
2710  *                     (GAsyncReadyCallback) two_strings_done,
2711  *                     &amp;data);
2712  * ]|
2713  *
2714  * This is an asynchronous method. When the operation is finished,
2715  * @callback will be invoked in the
2716  * <link linkend="g-main-context-push-thread-default">thread-default main loop</link>
2717  * of the thread you are calling this method from.
2718  * You can then call g_dbus_proxy_call_finish() to get the result of
2719  * the operation. See g_dbus_proxy_call_sync() for the synchronous
2720  * version of this method.
2721  *
2722  * Since: 2.26
2723  */
2724 void
2725 g_dbus_proxy_call (GDBusProxy          *proxy,
2726                    const gchar         *method_name,
2727                    GVariant            *parameters,
2728                    GDBusCallFlags       flags,
2729                    gint                 timeout_msec,
2730                    GCancellable        *cancellable,
2731                    GAsyncReadyCallback  callback,
2732                    gpointer             user_data)
2733 {
2734   return g_dbus_proxy_call_internal (proxy, method_name, parameters, flags, timeout_msec, NULL, cancellable, callback, user_data);
2735 }
2736
2737 /**
2738  * g_dbus_proxy_call_finish:
2739  * @proxy: A #GDBusProxy.
2740  * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_proxy_call().
2741  * @error: Return location for error or %NULL.
2742  *
2743  * Finishes an operation started with g_dbus_proxy_call().
2744  *
2745  * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
2746  * return values. Free with g_variant_unref().
2747  *
2748  * Since: 2.26
2749  */
2750 GVariant *
2751 g_dbus_proxy_call_finish (GDBusProxy    *proxy,
2752                           GAsyncResult  *res,
2753                           GError       **error)
2754 {
2755   return g_dbus_proxy_call_finish_internal (proxy, NULL, res, error);
2756 }
2757
2758 /**
2759  * g_dbus_proxy_call_sync:
2760  * @proxy: A #GDBusProxy.
2761  * @method_name: Name of method to invoke.
2762  * @parameters: (allow-none): A #GVariant tuple with parameters for the signal
2763  *              or %NULL if not passing parameters.
2764  * @flags: Flags from the #GDBusCallFlags enumeration.
2765  * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
2766  *                "infinite") or -1 to use the proxy default timeout.
2767  * @cancellable: A #GCancellable or %NULL.
2768  * @error: Return location for error or %NULL.
2769  *
2770  * Synchronously invokes the @method_name method on @proxy.
2771  *
2772  * If @method_name contains any dots, then @name is split into interface and
2773  * method name parts. This allows using @proxy for invoking methods on
2774  * other interfaces.
2775  *
2776  * If the #GDBusConnection associated with @proxy is disconnected then
2777  * the operation will fail with %G_IO_ERROR_CLOSED. If
2778  * @cancellable is canceled, the operation will fail with
2779  * %G_IO_ERROR_CANCELLED. If @parameters contains a value not
2780  * compatible with the D-Bus protocol, the operation fails with
2781  * %G_IO_ERROR_INVALID_ARGUMENT.
2782  *
2783  * If the @parameters #GVariant is floating, it is consumed. This allows
2784  * convenient 'inline' use of g_variant_new(), e.g.:
2785  * |[
2786  *  g_dbus_proxy_call_sync (proxy,
2787  *                          "TwoStrings",
2788  *                          g_variant_new ("(ss)",
2789  *                                         "Thing One",
2790  *                                         "Thing Two"),
2791  *                          G_DBUS_CALL_FLAGS_NONE,
2792  *                          -1,
2793  *                          NULL,
2794  *                          &amp;error);
2795  * ]|
2796  *
2797  * The calling thread is blocked until a reply is received. See
2798  * g_dbus_proxy_call() for the asynchronous version of this
2799  * method.
2800  *
2801  * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
2802  * return values. Free with g_variant_unref().
2803  *
2804  * Since: 2.26
2805  */
2806 GVariant *
2807 g_dbus_proxy_call_sync (GDBusProxy      *proxy,
2808                         const gchar     *method_name,
2809                         GVariant        *parameters,
2810                         GDBusCallFlags   flags,
2811                         gint             timeout_msec,
2812                         GCancellable    *cancellable,
2813                         GError         **error)
2814 {
2815   return g_dbus_proxy_call_sync_internal (proxy, method_name, parameters, flags, timeout_msec, NULL, NULL, cancellable, error);
2816 }
2817
2818 /* ---------------------------------------------------------------------------------------------------- */
2819
2820 #ifdef G_OS_UNIX
2821
2822 /**
2823  * g_dbus_proxy_call_with_unix_fd_list:
2824  * @proxy: A #GDBusProxy.
2825  * @method_name: Name of method to invoke.
2826  * @parameters: (allow-none): A #GVariant tuple with parameters for the signal or %NULL if not passing parameters.
2827  * @flags: Flags from the #GDBusCallFlags enumeration.
2828  * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
2829  *                "infinite") or -1 to use the proxy default timeout.
2830  * @fd_list: (allow-none): A #GUnixFDList or %NULL.
2831  * @cancellable: A #GCancellable or %NULL.
2832  * @callback: A #GAsyncReadyCallback to call when the request is satisfied or %NULL if you don't
2833  * care about the result of the method invocation.
2834  * @user_data: The data to pass to @callback.
2835  *
2836  * Like g_dbus_proxy_call() but also takes a #GUnixFDList object.
2837  *
2838  * This method is only available on UNIX.
2839  *
2840  * Since: 2.30
2841  */
2842 void
2843 g_dbus_proxy_call_with_unix_fd_list (GDBusProxy          *proxy,
2844                                      const gchar         *method_name,
2845                                      GVariant            *parameters,
2846                                      GDBusCallFlags       flags,
2847                                      gint                 timeout_msec,
2848                                      GUnixFDList         *fd_list,
2849                                      GCancellable        *cancellable,
2850                                      GAsyncReadyCallback  callback,
2851                                      gpointer             user_data)
2852 {
2853   return g_dbus_proxy_call_internal (proxy, method_name, parameters, flags, timeout_msec, fd_list, cancellable, callback, user_data);
2854 }
2855
2856 /**
2857  * g_dbus_proxy_call_with_unix_fd_list_finish:
2858  * @proxy: A #GDBusProxy.
2859  * @out_fd_list: (out): Return location for a #GUnixFDList or %NULL.
2860  * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_proxy_call_with_unix_fd_list().
2861  * @error: Return location for error or %NULL.
2862  *
2863  * Finishes an operation started with g_dbus_proxy_call_with_unix_fd_list().
2864  *
2865  * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
2866  * return values. Free with g_variant_unref().
2867  *
2868  * Since: 2.30
2869  */
2870 GVariant *
2871 g_dbus_proxy_call_with_unix_fd_list_finish (GDBusProxy    *proxy,
2872                                             GUnixFDList  **out_fd_list,
2873                                             GAsyncResult  *res,
2874                                             GError       **error)
2875 {
2876   return g_dbus_proxy_call_finish_internal (proxy, out_fd_list, res, error);
2877 }
2878
2879 /**
2880  * g_dbus_proxy_call_with_unix_fd_list_sync:
2881  * @proxy: A #GDBusProxy.
2882  * @method_name: Name of method to invoke.
2883  * @parameters: (allow-none): A #GVariant tuple with parameters for the signal
2884  *              or %NULL if not passing parameters.
2885  * @flags: Flags from the #GDBusCallFlags enumeration.
2886  * @timeout_msec: The timeout in milliseconds (with %G_MAXINT meaning
2887  *                "infinite") or -1 to use the proxy default timeout.
2888  * @fd_list: (allow-none): A #GUnixFDList or %NULL.
2889  * @out_fd_list: (out): Return location for a #GUnixFDList or %NULL.
2890  * @cancellable: A #GCancellable or %NULL.
2891  * @error: Return location for error or %NULL.
2892  *
2893  * Like g_dbus_proxy_call_sync() but also takes and returns #GUnixFDList objects.
2894  *
2895  * This method is only available on UNIX.
2896  *
2897  * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
2898  * return values. Free with g_variant_unref().
2899  *
2900  * Since: 2.30
2901  */
2902 GVariant *
2903 g_dbus_proxy_call_with_unix_fd_list_sync (GDBusProxy      *proxy,
2904                                           const gchar     *method_name,
2905                                           GVariant        *parameters,
2906                                           GDBusCallFlags   flags,
2907                                           gint             timeout_msec,
2908                                           GUnixFDList     *fd_list,
2909                                           GUnixFDList    **out_fd_list,
2910                                           GCancellable    *cancellable,
2911                                           GError         **error)
2912 {
2913   return g_dbus_proxy_call_sync_internal (proxy, method_name, parameters, flags, timeout_msec, fd_list, out_fd_list, cancellable, error);
2914 }
2915
2916 #endif /* G_OS_UNIX */
2917
2918 /* ---------------------------------------------------------------------------------------------------- */
2919
2920 static GDBusInterfaceInfo *
2921 _g_dbus_proxy_get_info (GDBusInterface *interface)
2922 {
2923   GDBusProxy *proxy = G_DBUS_PROXY (interface);
2924   return g_dbus_proxy_get_interface_info (proxy);
2925 }
2926
2927 static GDBusObject *
2928 _g_dbus_proxy_get_object (GDBusInterface *interface)
2929 {
2930   GDBusProxy *proxy = G_DBUS_PROXY (interface);
2931   return proxy->priv->object;
2932 }
2933
2934 static void
2935 _g_dbus_proxy_set_object (GDBusInterface *interface,
2936                           GDBusObject    *object)
2937 {
2938   GDBusProxy *proxy = G_DBUS_PROXY (interface);
2939   if (proxy->priv->object != NULL)
2940     g_object_remove_weak_pointer (G_OBJECT (proxy->priv->object), (gpointer *) &proxy->priv->object);
2941   proxy->priv->object = object;
2942   if (proxy->priv->object != NULL)
2943     g_object_add_weak_pointer (G_OBJECT (proxy->priv->object), (gpointer *) &proxy->priv->object);
2944 }
2945
2946 static void
2947 dbus_interface_iface_init (GDBusInterfaceIface *dbus_interface_iface)
2948 {
2949   dbus_interface_iface->get_info   = _g_dbus_proxy_get_info;
2950   dbus_interface_iface->get_object = _g_dbus_proxy_get_object;
2951   dbus_interface_iface->set_object = _g_dbus_proxy_set_object;
2952 }
2953
2954 /* ---------------------------------------------------------------------------------------------------- */