nexmon – Blame information for rev 1

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