Docs: replace <literal> by `
[platform/upstream/glib.git] / gio / gdbusinterfaceskeleton.c
1 /* GDBus - GLib D-Bus Library
2  *
3  * Copyright (C) 2008-2010 Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
17  *
18  * Author: David Zeuthen <davidz@redhat.com>
19  */
20
21 #include "config.h"
22
23 #include "gdbusinterface.h"
24 #include "gdbusinterfaceskeleton.h"
25 #include "gdbusobjectskeleton.h"
26 #include "gioenumtypes.h"
27 #include "gdbusprivate.h"
28 #include "gdbusmethodinvocation.h"
29 #include "gdbusconnection.h"
30 #include "gtask.h"
31 #include "gioerror.h"
32
33 #include "glibintl.h"
34
35 /**
36  * SECTION:gdbusinterfaceskeleton
37  * @short_description: Service-side D-Bus interface
38  * @include: gio/gio.h
39  *
40  * Abstract base class for D-Bus interfaces on the service side.
41  */
42
43 struct _GDBusInterfaceSkeletonPrivate
44 {
45   GMutex                      lock;
46
47   GDBusObject                *object;
48   GDBusInterfaceSkeletonFlags flags;
49
50   GSList                     *connections;   /* List of ConnectionData */
51   gchar                      *object_path;   /* The object path for this skeleton */
52   GDBusInterfaceVTable       *hooked_vtable;
53 };
54
55 typedef struct
56 {
57   GDBusConnection *connection;
58   guint            registration_id;
59 } ConnectionData;
60
61 enum
62 {
63   G_AUTHORIZE_METHOD_SIGNAL,
64   LAST_SIGNAL
65 };
66
67 enum
68 {
69   PROP_0,
70   PROP_G_FLAGS
71 };
72
73 static guint signals[LAST_SIGNAL] = {0};
74
75 static void     dbus_interface_interface_init                      (GDBusInterfaceIface    *iface);
76
77 static void     set_object_path_locked                             (GDBusInterfaceSkeleton *interface_,
78                                                                     const gchar            *object_path);
79 static void     remove_connection_locked                           (GDBusInterfaceSkeleton *interface_,
80                                                                     GDBusConnection        *connection);
81 static void     skeleton_intercept_handle_method_call              (GDBusConnection        *connection,
82                                                                     const gchar            *sender,
83                                                                     const gchar            *object_path,
84                                                                     const gchar            *interface_name,
85                                                                     const gchar            *method_name,
86                                                                     GVariant               *parameters,
87                                                                     GDBusMethodInvocation  *invocation,
88                                                                     gpointer                user_data);
89
90
91 G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GDBusInterfaceSkeleton, g_dbus_interface_skeleton, G_TYPE_OBJECT,
92                                   G_ADD_PRIVATE (GDBusInterfaceSkeleton)
93                                   G_IMPLEMENT_INTERFACE (G_TYPE_DBUS_INTERFACE, dbus_interface_interface_init))
94
95 static void
96 g_dbus_interface_skeleton_finalize (GObject *object)
97 {
98   GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (object);
99
100   /* Hold the lock just incase any code we call verifies that the lock is held */
101   g_mutex_lock (&interface->priv->lock);
102
103   /* unexport from all connections if we're exported anywhere */
104   while (interface->priv->connections != NULL)
105     {
106       ConnectionData *data = interface->priv->connections->data;
107       remove_connection_locked (interface, data->connection);
108     }
109
110   set_object_path_locked (interface, NULL);
111
112   g_mutex_unlock (&interface->priv->lock);
113
114   g_free (interface->priv->hooked_vtable);
115
116   if (interface->priv->object != NULL)
117     g_object_remove_weak_pointer (G_OBJECT (interface->priv->object), (gpointer *) &interface->priv->object);
118
119   g_mutex_clear (&interface->priv->lock);
120
121   G_OBJECT_CLASS (g_dbus_interface_skeleton_parent_class)->finalize (object);
122 }
123
124 static void
125 g_dbus_interface_skeleton_get_property (GObject      *object,
126                                         guint         prop_id,
127                                         GValue       *value,
128                                         GParamSpec   *pspec)
129 {
130   GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (object);
131
132   switch (prop_id)
133     {
134     case PROP_G_FLAGS:
135       g_value_set_flags (value, g_dbus_interface_skeleton_get_flags (interface));
136       break;
137
138     default:
139       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
140       break;
141     }
142 }
143
144 static void
145 g_dbus_interface_skeleton_set_property (GObject      *object,
146                                         guint         prop_id,
147                                         const GValue *value,
148                                         GParamSpec   *pspec)
149 {
150   GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (object);
151
152   switch (prop_id)
153     {
154     case PROP_G_FLAGS:
155       g_dbus_interface_skeleton_set_flags (interface, g_value_get_flags (value));
156       break;
157
158     default:
159       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
160       break;
161     }
162 }
163
164 static gboolean
165 g_dbus_interface_skeleton_g_authorize_method_default (GDBusInterfaceSkeleton    *interface,
166                                                       GDBusMethodInvocation *invocation)
167 {
168   return TRUE;
169 }
170
171 static void
172 g_dbus_interface_skeleton_class_init (GDBusInterfaceSkeletonClass *klass)
173 {
174   GObjectClass *gobject_class;
175
176   gobject_class = G_OBJECT_CLASS (klass);
177   gobject_class->finalize     = g_dbus_interface_skeleton_finalize;
178   gobject_class->set_property = g_dbus_interface_skeleton_set_property;
179   gobject_class->get_property = g_dbus_interface_skeleton_get_property;
180
181   klass->g_authorize_method = g_dbus_interface_skeleton_g_authorize_method_default;
182
183   /**
184    * GDBusInterfaceSkeleton:g-flags:
185    *
186    * Flags from the #GDBusInterfaceSkeletonFlags enumeration.
187    *
188    * Since: 2.30
189    */
190   g_object_class_install_property (gobject_class,
191                                    PROP_G_FLAGS,
192                                    g_param_spec_flags ("g-flags",
193                                                        "g-flags",
194                                                        "Flags for the interface skeleton",
195                                                        G_TYPE_DBUS_INTERFACE_SKELETON_FLAGS,
196                                                        G_DBUS_INTERFACE_SKELETON_FLAGS_NONE,
197                                                        G_PARAM_READABLE |
198                                                        G_PARAM_WRITABLE |
199                                                        G_PARAM_STATIC_STRINGS));
200
201   /**
202    * GDBusInterfaceSkeleton::g-authorize-method:
203    * @interface: The #GDBusInterfaceSkeleton emitting the signal.
204    * @invocation: A #GDBusMethodInvocation.
205    *
206    * Emitted when a method is invoked by a remote caller and used to
207    * determine if the method call is authorized.
208    *
209    * Note that this signal is emitted in a thread dedicated to
210    * handling the method call so handlers are allowed to perform
211    * blocking IO. This means that it is appropriate to call e.g.
212    * [polkit_authority_check_authorization_sync()](http://hal.freedesktop.org/docs/polkit/PolkitAuthority.html#polkit-authority-check-authorization-sync)
213    * with the
214    * [POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION](http://hal.freedesktop.org/docs/polkit/PolkitAuthority.html#POLKIT-CHECK-AUTHORIZATION-FLAGS-ALLOW-USER-INTERACTION:CAPS)
215    * flag set.
216    *
217    * If %FALSE is returned then no further handlers are run and the
218    * signal handler must take a reference to @invocation and finish
219    * handling the call (e.g. return an error via
220    * g_dbus_method_invocation_return_error()).
221    *
222    * Otherwise, if %TRUE is returned, signal emission continues. If no
223    * handlers return %FALSE, then the method is dispatched. If
224    * @interface has an enclosing #GDBusObjectSkeleton, then the
225    * #GDBusObjectSkeleton::authorize-method signal handlers run before
226    * the handlers for this signal.
227    *
228    * The default class handler just returns %TRUE.
229    *
230    * Please note that the common case is optimized: if no signals
231    * handlers are connected and the default class handler isn't
232    * overridden (for both @interface and the enclosing
233    * #GDBusObjectSkeleton, if any) and #GDBusInterfaceSkeleton:g-flags does
234    * not have the
235    * %G_DBUS_INTERFACE_SKELETON_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_THREAD
236    * flags set, no dedicated thread is ever used and the call will be
237    * handled in the same thread as the object that @interface belongs
238    * to was exported in.
239    *
240    * Returns: %TRUE if the call is authorized, %FALSE otherwise.
241    *
242    * Since: 2.30
243    */
244   signals[G_AUTHORIZE_METHOD_SIGNAL] =
245     g_signal_new ("g-authorize-method",
246                   G_TYPE_DBUS_INTERFACE_SKELETON,
247                   G_SIGNAL_RUN_LAST,
248                   G_STRUCT_OFFSET (GDBusInterfaceSkeletonClass, g_authorize_method),
249                   _g_signal_accumulator_false_handled,
250                   NULL,
251                   NULL,
252                   G_TYPE_BOOLEAN,
253                   1,
254                   G_TYPE_DBUS_METHOD_INVOCATION);
255 }
256
257 static void
258 g_dbus_interface_skeleton_init (GDBusInterfaceSkeleton *interface)
259 {
260   interface->priv = g_dbus_interface_skeleton_get_instance_private (interface);
261   g_mutex_init (&interface->priv->lock);
262 }
263
264 /* ---------------------------------------------------------------------------------------------------- */
265
266 /**
267  * g_dbus_interface_skeleton_get_flags:
268  * @interface_: A #GDBusInterfaceSkeleton.
269  *
270  * Gets the #GDBusInterfaceSkeletonFlags that describes what the behavior
271  * of @interface_
272  *
273  * Returns: One or more flags from the #GDBusInterfaceSkeletonFlags enumeration.
274  *
275  * Since: 2.30
276  */
277 GDBusInterfaceSkeletonFlags
278 g_dbus_interface_skeleton_get_flags (GDBusInterfaceSkeleton  *interface_)
279 {
280   g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), G_DBUS_INTERFACE_SKELETON_FLAGS_NONE);
281   return interface_->priv->flags;
282 }
283
284 /**
285  * g_dbus_interface_skeleton_set_flags:
286  * @interface_: A #GDBusInterfaceSkeleton.
287  * @flags: Flags from the #GDBusInterfaceSkeletonFlags enumeration.
288  *
289  * Sets flags describing what the behavior of @skeleton should be.
290  *
291  * Since: 2.30
292  */
293 void
294 g_dbus_interface_skeleton_set_flags (GDBusInterfaceSkeleton      *interface_,
295                                      GDBusInterfaceSkeletonFlags  flags)
296 {
297   g_return_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_));
298   g_mutex_lock (&interface_->priv->lock);
299   if (interface_->priv->flags != flags)
300     {
301       interface_->priv->flags = flags;
302       g_mutex_unlock (&interface_->priv->lock);
303       g_object_notify (G_OBJECT (interface_), "g-flags");
304     }
305   else
306     {
307       g_mutex_unlock (&interface_->priv->lock);
308     }
309 }
310
311 /**
312  * g_dbus_interface_skeleton_get_info:
313  * @interface_: A #GDBusInterfaceSkeleton.
314  *
315  * Gets D-Bus introspection information for the D-Bus interface
316  * implemented by @interface_.
317  *
318  * Returns: (transfer none): A #GDBusInterfaceInfo (never %NULL). Do not free.
319  *
320  * Since: 2.30
321  */
322 GDBusInterfaceInfo *
323 g_dbus_interface_skeleton_get_info (GDBusInterfaceSkeleton *interface_)
324 {
325   GDBusInterfaceInfo *ret;
326   g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), NULL);
327   ret = G_DBUS_INTERFACE_SKELETON_GET_CLASS (interface_)->get_info (interface_);
328   g_warn_if_fail (ret != NULL);
329   return ret;
330 }
331
332 /**
333  * g_dbus_interface_skeleton_get_vtable: (skip)
334  * @interface_: A #GDBusInterfaceSkeleton.
335  *
336  * Gets the interface vtable for the D-Bus interface implemented by
337  * @interface_. The returned function pointers should expect @interface_
338  * itself to be passed as @user_data.
339  *
340  * Returns: A #GDBusInterfaceVTable (never %NULL).
341  *
342  * Since: 2.30
343  */
344 GDBusInterfaceVTable *
345 g_dbus_interface_skeleton_get_vtable (GDBusInterfaceSkeleton *interface_)
346 {
347   GDBusInterfaceVTable *ret;
348   g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), NULL);
349   ret = G_DBUS_INTERFACE_SKELETON_GET_CLASS (interface_)->get_vtable (interface_);
350   g_warn_if_fail (ret != NULL);
351   return ret;
352 }
353
354 /**
355  * g_dbus_interface_skeleton_get_properties:
356  * @interface_: A #GDBusInterfaceSkeleton.
357  *
358  * Gets all D-Bus properties for @interface_.
359  *
360  * Returns: (transfer full): A #GVariant of type <link linkend="G-VARIANT-TYPE-VARDICT:CAPS">'a{sv}'</link>. Free with g_variant_unref().
361  *
362  * Since: 2.30
363  */
364 GVariant *
365 g_dbus_interface_skeleton_get_properties (GDBusInterfaceSkeleton *interface_)
366 {
367   GVariant *ret;
368   g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), NULL);
369   ret = G_DBUS_INTERFACE_SKELETON_GET_CLASS (interface_)->get_properties (interface_);
370   return g_variant_take_ref (ret);
371 }
372
373 /**
374  * g_dbus_interface_skeleton_flush:
375  * @interface_: A #GDBusInterfaceSkeleton.
376  *
377  * If @interface_ has outstanding changes, request for these changes to be
378  * emitted immediately.
379  *
380  * For example, an exported D-Bus interface may queue up property
381  * changes and emit the
382  * `org.freedesktop.DBus.Properties::Propert``
383  * signal later (e.g. in an idle handler). This technique is useful
384  * for collapsing multiple property changes into one.
385  *
386  * Since: 2.30
387  */
388 void
389 g_dbus_interface_skeleton_flush (GDBusInterfaceSkeleton *interface_)
390 {
391   g_return_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_));
392   G_DBUS_INTERFACE_SKELETON_GET_CLASS (interface_)->flush (interface_);
393 }
394
395 /* ---------------------------------------------------------------------------------------------------- */
396
397 static GDBusInterfaceInfo *
398 _g_dbus_interface_skeleton_get_info (GDBusInterface *interface_)
399 {
400   GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (interface_);
401   return g_dbus_interface_skeleton_get_info (interface);
402 }
403
404 static GDBusObject *
405 g_dbus_interface_skeleton_get_object (GDBusInterface *interface_)
406 {
407   GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (interface_);
408   GDBusObject *ret;
409   g_mutex_lock (&interface->priv->lock);
410   ret = interface->priv->object;
411   g_mutex_unlock (&interface->priv->lock);
412   return ret;
413 }
414
415 static GDBusObject *
416 g_dbus_interface_skeleton_dup_object (GDBusInterface *interface_)
417 {
418   GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (interface_);
419   GDBusObject *ret;
420   g_mutex_lock (&interface->priv->lock);
421   ret = interface->priv->object;
422   if (ret != NULL)
423     g_object_ref (ret);
424   g_mutex_unlock (&interface->priv->lock);
425   return ret;
426 }
427
428 static void
429 g_dbus_interface_skeleton_set_object (GDBusInterface *interface_,
430                                       GDBusObject    *object)
431 {
432   GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (interface_);
433   g_mutex_lock (&interface->priv->lock);
434   if (interface->priv->object != NULL)
435     g_object_remove_weak_pointer (G_OBJECT (interface->priv->object), (gpointer *) &interface->priv->object);
436   interface->priv->object = object;
437   if (object != NULL)
438     g_object_add_weak_pointer (G_OBJECT (interface->priv->object), (gpointer *) &interface->priv->object);
439   g_mutex_unlock (&interface->priv->lock);
440 }
441
442 static void
443 dbus_interface_interface_init (GDBusInterfaceIface *iface)
444 {
445   iface->get_info    = _g_dbus_interface_skeleton_get_info;
446   iface->get_object  = g_dbus_interface_skeleton_get_object;
447   iface->dup_object  = g_dbus_interface_skeleton_dup_object;
448   iface->set_object  = g_dbus_interface_skeleton_set_object;
449 }
450
451 /* ---------------------------------------------------------------------------------------------------- */
452
453 typedef struct
454 {
455   volatile gint ref_count;
456   GDBusInterfaceSkeleton       *interface;
457   GDBusInterfaceMethodCallFunc  method_call_func;
458   GDBusMethodInvocation        *invocation;
459 } DispatchData;
460
461 static void
462 dispatch_data_unref (DispatchData *data)
463 {
464   if (g_atomic_int_dec_and_test (&data->ref_count))
465     g_slice_free (DispatchData, data);
466 }
467
468 static DispatchData *
469 dispatch_data_ref (DispatchData *data)
470 {
471   g_atomic_int_inc (&data->ref_count);
472   return data;
473 }
474
475 static gboolean
476 dispatch_invoke_in_context_func (gpointer user_data)
477 {
478   DispatchData *data = user_data;
479   data->method_call_func (g_dbus_method_invocation_get_connection (data->invocation),
480                           g_dbus_method_invocation_get_sender (data->invocation),
481                           g_dbus_method_invocation_get_object_path (data->invocation),
482                           g_dbus_method_invocation_get_interface_name (data->invocation),
483                           g_dbus_method_invocation_get_method_name (data->invocation),
484                           g_dbus_method_invocation_get_parameters (data->invocation),
485                           data->invocation,
486                           g_dbus_method_invocation_get_user_data (data->invocation));
487   return FALSE;
488 }
489
490 static void
491 dispatch_in_thread_func (GTask        *task,
492                          gpointer      source_object,
493                          gpointer      task_data,
494                          GCancellable *cancellable)
495 {
496   DispatchData *data = task_data;
497   GDBusInterfaceSkeletonFlags flags;
498   GDBusObject *object;
499   gboolean authorized;
500
501   g_mutex_lock (&data->interface->priv->lock);
502   flags = data->interface->priv->flags;
503   object = data->interface->priv->object;
504   if (object != NULL)
505     g_object_ref (object);
506   g_mutex_unlock (&data->interface->priv->lock);
507
508   /* first check on the enclosing object (if any), then the interface */
509   authorized = TRUE;
510   if (object != NULL)
511     {
512       g_signal_emit_by_name (object,
513                              "authorize-method",
514                              data->interface,
515                              data->invocation,
516                              &authorized);
517     }
518   if (authorized)
519     {
520       g_signal_emit (data->interface,
521                      signals[G_AUTHORIZE_METHOD_SIGNAL],
522                      0,
523                      data->invocation,
524                      &authorized);
525     }
526
527   if (authorized)
528     {
529       gboolean run_in_thread;
530       run_in_thread = (flags & G_DBUS_INTERFACE_SKELETON_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_THREAD);
531       if (run_in_thread)
532         {
533           /* might as well just re-use the existing thread */
534           data->method_call_func (g_dbus_method_invocation_get_connection (data->invocation),
535                                   g_dbus_method_invocation_get_sender (data->invocation),
536                                   g_dbus_method_invocation_get_object_path (data->invocation),
537                                   g_dbus_method_invocation_get_interface_name (data->invocation),
538                                   g_dbus_method_invocation_get_method_name (data->invocation),
539                                   g_dbus_method_invocation_get_parameters (data->invocation),
540                                   data->invocation,
541                                   g_dbus_method_invocation_get_user_data (data->invocation));
542         }
543       else
544         {
545           /* bah, back to original context */
546           g_main_context_invoke_full (g_task_get_context (task),
547                                       g_task_get_priority (task),
548                                       dispatch_invoke_in_context_func,
549                                       dispatch_data_ref (data),
550                                       (GDestroyNotify) dispatch_data_unref);
551         }
552     }
553   else
554     {
555       /* do nothing */
556     }
557
558   if (object != NULL)
559     g_object_unref (object);
560 }
561
562 static void
563 g_dbus_interface_method_dispatch_helper (GDBusInterfaceSkeleton       *interface,
564                                          GDBusInterfaceMethodCallFunc  method_call_func,
565                                          GDBusMethodInvocation        *invocation)
566 {
567   gboolean has_handlers;
568   gboolean has_default_class_handler;
569   gboolean emit_authorized_signal;
570   gboolean run_in_thread;
571   GDBusInterfaceSkeletonFlags flags;
572   GDBusObject *object;
573
574   g_return_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface));
575   g_return_if_fail (method_call_func != NULL);
576   g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
577
578   g_mutex_lock (&interface->priv->lock);
579   flags = interface->priv->flags;
580   object = interface->priv->object;
581   if (object != NULL)
582     g_object_ref (object);
583   g_mutex_unlock (&interface->priv->lock);
584
585   /* optimization for the common case where
586    *
587    *  a) no handler is connected and class handler is not overridden (both interface and object); and
588    *  b) method calls are not dispatched in a thread
589    */
590   has_handlers = g_signal_has_handler_pending (interface,
591                                                signals[G_AUTHORIZE_METHOD_SIGNAL],
592                                                0,
593                                                TRUE);
594   has_default_class_handler = (G_DBUS_INTERFACE_SKELETON_GET_CLASS (interface)->g_authorize_method ==
595                                g_dbus_interface_skeleton_g_authorize_method_default);
596
597   emit_authorized_signal = (has_handlers || !has_default_class_handler);
598   if (!emit_authorized_signal)
599     {
600       if (object != NULL)
601         emit_authorized_signal = _g_dbus_object_skeleton_has_authorize_method_handlers (G_DBUS_OBJECT_SKELETON (object));
602     }
603
604   run_in_thread = (flags & G_DBUS_INTERFACE_SKELETON_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_THREAD);
605   if (!emit_authorized_signal && !run_in_thread)
606     {
607       method_call_func (g_dbus_method_invocation_get_connection (invocation),
608                         g_dbus_method_invocation_get_sender (invocation),
609                         g_dbus_method_invocation_get_object_path (invocation),
610                         g_dbus_method_invocation_get_interface_name (invocation),
611                         g_dbus_method_invocation_get_method_name (invocation),
612                         g_dbus_method_invocation_get_parameters (invocation),
613                         invocation,
614                         g_dbus_method_invocation_get_user_data (invocation));
615     }
616   else
617     {
618       GTask *task;
619       DispatchData *data;
620
621       data = g_slice_new0 (DispatchData);
622       data->interface = interface;
623       data->method_call_func = method_call_func;
624       data->invocation = invocation;
625       data->ref_count = 1;
626
627       task = g_task_new (interface, NULL, NULL, NULL);
628       g_task_set_task_data (task, data, (GDestroyNotify) dispatch_data_unref);
629       g_task_run_in_thread (task, dispatch_in_thread_func);
630       g_object_unref (task);
631     }
632
633   if (object != NULL)
634     g_object_unref (object);
635 }
636
637 static void
638 skeleton_intercept_handle_method_call (GDBusConnection       *connection,
639                                        const gchar           *sender,
640                                        const gchar           *object_path,
641                                        const gchar           *interface_name,
642                                        const gchar           *method_name,
643                                        GVariant              *parameters,
644                                        GDBusMethodInvocation *invocation,
645                                        gpointer               user_data)
646 {
647   GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (user_data);
648   g_dbus_interface_method_dispatch_helper (interface,
649                                            g_dbus_interface_skeleton_get_vtable (interface)->method_call,
650                                            invocation);
651 }
652
653 /* ---------------------------------------------------------------------------------------------------- */
654
655 static ConnectionData *
656 new_connection (GDBusConnection *connection,
657                 guint            registration_id)
658 {
659   ConnectionData *data;
660
661   data = g_slice_new0 (ConnectionData);
662   data->connection      = g_object_ref (connection);
663   data->registration_id = registration_id;
664
665   return data;
666 }
667
668 static void
669 free_connection (ConnectionData *data)
670 {
671   if (data != NULL)
672     {
673       g_object_unref (data->connection);
674       g_slice_free (ConnectionData, data);
675     }
676 }
677
678 static gboolean
679 add_connection_locked (GDBusInterfaceSkeleton *interface_,
680                        GDBusConnection        *connection,
681                        GError                **error)
682 {
683   ConnectionData *data;
684   guint registration_id;
685   gboolean ret = FALSE;
686
687   if (interface_->priv->hooked_vtable == NULL)
688     {
689       /* Hook the vtable since we need to intercept method calls for
690        * ::g-authorize-method and for dispatching in thread vs
691        * context
692        *
693        * We need to wait until subclasses have had time to initialize
694        * properly before building the hooked_vtable, so we create it
695        * once at the last minute.
696        */
697       interface_->priv->hooked_vtable = g_memdup (g_dbus_interface_skeleton_get_vtable (interface_), sizeof (GDBusInterfaceVTable));
698       interface_->priv->hooked_vtable->method_call = skeleton_intercept_handle_method_call;
699     }
700
701   registration_id = g_dbus_connection_register_object (connection,
702                                                        interface_->priv->object_path,
703                                                        g_dbus_interface_skeleton_get_info (interface_),
704                                                        interface_->priv->hooked_vtable,
705                                                        interface_,
706                                                        NULL, /* user_data_free_func */
707                                                        error);
708
709   if (registration_id > 0)
710     {
711       data = new_connection (connection, registration_id);
712       interface_->priv->connections = g_slist_append (interface_->priv->connections, data);
713       ret = TRUE;
714     }
715
716   return ret;
717 }
718
719 static void
720 remove_connection_locked (GDBusInterfaceSkeleton *interface_,
721                           GDBusConnection        *connection)
722 {
723   ConnectionData *data;
724   GSList *l;
725
726   /* Get the connection in the list and unregister ... */
727   for (l = interface_->priv->connections; l != NULL; l = l->next)
728     {
729       data = l->data;
730       if (data->connection == connection)
731         {
732           g_warn_if_fail (g_dbus_connection_unregister_object (data->connection, data->registration_id));
733           free_connection (data);
734           interface_->priv->connections = g_slist_delete_link (interface_->priv->connections, l);
735           /* we are guaranteed that the connection is only added once, so bail out early */
736           goto out;
737         }
738     }
739  out:
740   ;
741 }
742
743 static void
744 set_object_path_locked (GDBusInterfaceSkeleton *interface_,
745                         const gchar            *object_path)
746 {
747   if (g_strcmp0 (interface_->priv->object_path, object_path) != 0)
748     {
749       g_free (interface_->priv->object_path);
750       interface_->priv->object_path = g_strdup (object_path);
751     }
752 }
753
754 /* ---------------------------------------------------------------------------------------------------- */
755
756 /**
757  * g_dbus_interface_skeleton_get_connection:
758  * @interface_: A #GDBusInterfaceSkeleton.
759  *
760  * Gets the first connection that @interface_ is exported on, if any.
761  *
762  * Returns: (transfer none): A #GDBusConnection or %NULL if @interface_ is
763  * not exported anywhere. Do not free, the object belongs to @interface_.
764  *
765  * Since: 2.30
766  */
767 GDBusConnection *
768 g_dbus_interface_skeleton_get_connection (GDBusInterfaceSkeleton *interface_)
769 {
770   ConnectionData  *data;
771   GDBusConnection *ret;
772
773   g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), NULL);
774   g_mutex_lock (&interface_->priv->lock);
775
776   ret = NULL;
777   if (interface_->priv->connections != NULL)
778     {
779       data = interface_->priv->connections->data;
780       if (data != NULL)
781         ret = data->connection;
782     }
783
784   g_mutex_unlock (&interface_->priv->lock);
785
786   return ret;
787 }
788
789 /**
790  * g_dbus_interface_skeleton_get_connections:
791  * @interface_: A #GDBusInterfaceSkeleton.
792  *
793  * Gets a list of the connections that @interface_ is exported on.
794  *
795  * Returns: (element-type GDBusConnection) (transfer full): A list of
796  *   all the connections that @interface_ is exported on. The returned
797  *   list should be freed with g_list_free() after each element has
798  *   been freed with g_object_unref().
799  *
800  * Since: 2.32
801  */
802 GList *
803 g_dbus_interface_skeleton_get_connections (GDBusInterfaceSkeleton *interface_)
804 {
805   GList           *connections;
806   GSList          *l;
807   ConnectionData  *data;
808
809   g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), NULL);
810
811   g_mutex_lock (&interface_->priv->lock);
812   connections = NULL;
813
814   for (l = interface_->priv->connections; l != NULL; l = l->next)
815     {
816       data        = l->data;
817       connections = g_list_prepend (connections,
818                                     /* Return a reference to each connection */
819                                     g_object_ref (data->connection));
820     }
821
822   g_mutex_unlock (&interface_->priv->lock);
823
824   return g_list_reverse (connections);
825 }
826
827 /**
828  * g_dbus_interface_skeleton_has_connection:
829  * @interface_: A #GDBusInterfaceSkeleton.
830  * @connection: A #GDBusConnection.
831  *
832  * Checks if @interface_ is exported on @connection.
833  *
834  * Returns: %TRUE if @interface_ is exported on @connection, %FALSE otherwise.
835  *
836  * Since: 2.32
837  */
838 gboolean
839 g_dbus_interface_skeleton_has_connection (GDBusInterfaceSkeleton     *interface_,
840                                           GDBusConnection            *connection)
841 {
842   GSList *l;
843   gboolean ret = FALSE;
844
845   g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), FALSE);
846   g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE);
847
848   g_mutex_lock (&interface_->priv->lock);
849
850   for (l = interface_->priv->connections; l != NULL; l = l->next)
851     {
852       ConnectionData *data = l->data;
853       if (data->connection == connection)
854         {
855           ret = TRUE;
856           goto out;
857         }
858     }
859
860  out:
861   g_mutex_unlock (&interface_->priv->lock);
862   return ret;
863 }
864
865 /**
866  * g_dbus_interface_skeleton_get_object_path:
867  * @interface_: A #GDBusInterfaceSkeleton.
868  *
869  * Gets the object path that @interface_ is exported on, if any.
870  *
871  * Returns: A string owned by @interface_ or %NULL if @interface_ is not exported
872  * anywhere. Do not free, the string belongs to @interface_.
873  *
874  * Since: 2.30
875  */
876 const gchar *
877 g_dbus_interface_skeleton_get_object_path (GDBusInterfaceSkeleton *interface_)
878 {
879   const gchar *ret;
880   g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), NULL);
881   g_mutex_lock (&interface_->priv->lock);
882   ret = interface_->priv->object_path;
883   g_mutex_unlock (&interface_->priv->lock);
884   return ret;
885 }
886
887 /**
888  * g_dbus_interface_skeleton_export:
889  * @interface_: The D-Bus interface to export.
890  * @connection: A #GDBusConnection to export @interface_ on.
891  * @object_path: The path to export the interface at.
892  * @error: Return location for error or %NULL.
893  *
894  * Exports @interface_ at @object_path on @connection.
895  *
896  * This can be called multiple times to export the same @interface_
897  * onto multiple connections however the @object_path provided must be
898  * the same for all connections.
899  *
900  * Use g_dbus_interface_skeleton_unexport() to unexport the object.
901  *
902  * Returns: %TRUE if the interface was exported on @connection, otherwise %FALSE with
903  * @error set.
904  *
905  * Since: 2.30
906  */
907 gboolean
908 g_dbus_interface_skeleton_export (GDBusInterfaceSkeleton  *interface_,
909                                   GDBusConnection         *connection,
910                                   const gchar             *object_path,
911                                   GError                 **error)
912 {
913   gboolean ret = FALSE;
914
915   g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), FALSE);
916   g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE);
917   g_return_val_if_fail (g_variant_is_object_path (object_path), FALSE);
918   g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
919
920   /* Assert that the object path is the same for multiple connections here */
921   g_return_val_if_fail (interface_->priv->object_path == NULL ||
922                         g_strcmp0 (interface_->priv->object_path, object_path) == 0, FALSE);
923
924   g_mutex_lock (&interface_->priv->lock);
925
926   /* Set the object path */
927   set_object_path_locked (interface_, object_path);
928
929   /* Add the connection */
930   ret = add_connection_locked (interface_, connection, error);
931
932   g_mutex_unlock (&interface_->priv->lock);
933   return ret;
934 }
935
936 /**
937  * g_dbus_interface_skeleton_unexport:
938  * @interface_: A #GDBusInterfaceSkeleton.
939  *
940  * Stops exporting @interface_ on all connections it is exported on.
941  *
942  * To unexport @interface_ from only a single connection, use
943  * g_dbus_interface_skeleton_unexport_from_connection()
944  *
945  * Since: 2.30
946  */
947 void
948 g_dbus_interface_skeleton_unexport (GDBusInterfaceSkeleton *interface_)
949 {
950   g_return_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_));
951   g_return_if_fail (interface_->priv->connections != NULL);
952
953   g_mutex_lock (&interface_->priv->lock);
954
955   g_assert (interface_->priv->object_path != NULL);
956   g_assert (interface_->priv->hooked_vtable != NULL);
957
958   /* Remove all connections */
959   while (interface_->priv->connections != NULL)
960     {
961       ConnectionData *data = interface_->priv->connections->data;
962       remove_connection_locked (interface_, data->connection);
963     }
964
965   /* Unset the object path since there are no connections left */
966   set_object_path_locked (interface_, NULL);
967
968   g_mutex_unlock (&interface_->priv->lock);
969 }
970
971
972 /**
973  * g_dbus_interface_skeleton_unexport_from_connection:
974  * @interface_: A #GDBusInterfaceSkeleton.
975  * @connection: A #GDBusConnection.
976  *
977  * Stops exporting @interface_ on @connection.
978  *
979  * To stop exporting on all connections the interface is exported on,
980  * use g_dbus_interface_skeleton_unexport().
981  *
982  * Since: 2.32
983  */
984 void
985 g_dbus_interface_skeleton_unexport_from_connection (GDBusInterfaceSkeleton *interface_,
986                                                     GDBusConnection        *connection)
987 {
988   g_return_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_));
989   g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
990   g_return_if_fail (interface_->priv->connections != NULL);
991
992   g_mutex_lock (&interface_->priv->lock);
993
994   g_assert (interface_->priv->object_path != NULL);
995   g_assert (interface_->priv->hooked_vtable != NULL);
996
997   remove_connection_locked (interface_, connection);
998
999   /* Reset the object path if we removed the last connection */
1000   if (interface_->priv->connections == NULL)
1001     set_object_path_locked (interface_, NULL);
1002
1003   g_mutex_unlock (&interface_->priv->lock);
1004 }
1005
1006 /* ---------------------------------------------------------------------------------------------------- */