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