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