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