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