Add some more cases to the app-id unit tests
[glib.git] / gio / gdbusinterfaceskeleton.c
blobb96be3ff8941615e9c19a112d504153241f986ee
1 /* GDBus - GLib D-Bus Library
3 * Copyright (C) 2008-2010 Red Hat, Inc.
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.
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.
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/>.
18 * Author: David Zeuthen <davidz@redhat.com>
21 #include "config.h"
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"
33 #include "glibintl.h"
35 /**
36 * SECTION:gdbusinterfaceskeleton
37 * @short_description: Service-side D-Bus interface
38 * @include: gio/gio.h
40 * Abstract base class for D-Bus interfaces on the service side.
43 struct _GDBusInterfaceSkeletonPrivate
45 GMutex lock;
47 GDBusObject *object;
48 GDBusInterfaceSkeletonFlags flags;
50 GSList *connections; /* List of ConnectionData */
51 gchar *object_path; /* The object path for this skeleton */
52 GDBusInterfaceVTable *hooked_vtable;
55 typedef struct
57 GDBusConnection *connection;
58 guint registration_id;
59 } ConnectionData;
61 enum
63 G_AUTHORIZE_METHOD_SIGNAL,
64 LAST_SIGNAL
67 enum
69 PROP_0,
70 PROP_G_FLAGS
73 static guint signals[LAST_SIGNAL] = {0};
75 static void dbus_interface_interface_init (GDBusInterfaceIface *iface);
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);
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))
95 static void
96 g_dbus_interface_skeleton_finalize (GObject *object)
98 GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (object);
100 /* Hold the lock just incase any code we call verifies that the lock is held */
101 g_mutex_lock (&interface->priv->lock);
103 /* unexport from all connections if we're exported anywhere */
104 while (interface->priv->connections != NULL)
106 ConnectionData *data = interface->priv->connections->data;
107 remove_connection_locked (interface, data->connection);
110 set_object_path_locked (interface, NULL);
112 g_mutex_unlock (&interface->priv->lock);
114 g_free (interface->priv->hooked_vtable);
116 if (interface->priv->object != NULL)
117 g_object_remove_weak_pointer (G_OBJECT (interface->priv->object), (gpointer *) &interface->priv->object);
119 g_mutex_clear (&interface->priv->lock);
121 G_OBJECT_CLASS (g_dbus_interface_skeleton_parent_class)->finalize (object);
124 static void
125 g_dbus_interface_skeleton_get_property (GObject *object,
126 guint prop_id,
127 GValue *value,
128 GParamSpec *pspec)
130 GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (object);
132 switch (prop_id)
134 case PROP_G_FLAGS:
135 g_value_set_flags (value, g_dbus_interface_skeleton_get_flags (interface));
136 break;
138 default:
139 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
140 break;
144 static void
145 g_dbus_interface_skeleton_set_property (GObject *object,
146 guint prop_id,
147 const GValue *value,
148 GParamSpec *pspec)
150 GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (object);
152 switch (prop_id)
154 case PROP_G_FLAGS:
155 g_dbus_interface_skeleton_set_flags (interface, g_value_get_flags (value));
156 break;
158 default:
159 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
160 break;
164 static gboolean
165 g_dbus_interface_skeleton_g_authorize_method_default (GDBusInterfaceSkeleton *interface,
166 GDBusMethodInvocation *invocation)
168 return TRUE;
171 static void
172 g_dbus_interface_skeleton_class_init (GDBusInterfaceSkeletonClass *klass)
174 GObjectClass *gobject_class;
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;
181 klass->g_authorize_method = g_dbus_interface_skeleton_g_authorize_method_default;
184 * GDBusInterfaceSkeleton:g-flags:
186 * Flags from the #GDBusInterfaceSkeletonFlags enumeration.
188 * Since: 2.30
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));
202 * GDBusInterfaceSkeleton::g-authorize-method:
203 * @interface: The #GDBusInterfaceSkeleton emitting the signal.
204 * @invocation: A #GDBusMethodInvocation.
206 * Emitted when a method is invoked by a remote caller and used to
207 * determine if the method call is authorized.
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.
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()).
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.
228 * The default class handler just returns %TRUE.
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.
240 * Returns: %TRUE if the call is authorized, %FALSE otherwise.
242 * Since: 2.30
244 signals[G_AUTHORIZE_METHOD_SIGNAL] =
245 g_signal_new (I_("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,
254 G_TYPE_DBUS_METHOD_INVOCATION);
257 static void
258 g_dbus_interface_skeleton_init (GDBusInterfaceSkeleton *interface)
260 interface->priv = g_dbus_interface_skeleton_get_instance_private (interface);
261 g_mutex_init (&interface->priv->lock);
264 /* ---------------------------------------------------------------------------------------------------- */
267 * g_dbus_interface_skeleton_get_flags:
268 * @interface_: A #GDBusInterfaceSkeleton.
270 * Gets the #GDBusInterfaceSkeletonFlags that describes what the behavior
271 * of @interface_
273 * Returns: One or more flags from the #GDBusInterfaceSkeletonFlags enumeration.
275 * Since: 2.30
277 GDBusInterfaceSkeletonFlags
278 g_dbus_interface_skeleton_get_flags (GDBusInterfaceSkeleton *interface_)
280 g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), G_DBUS_INTERFACE_SKELETON_FLAGS_NONE);
281 return interface_->priv->flags;
285 * g_dbus_interface_skeleton_set_flags:
286 * @interface_: A #GDBusInterfaceSkeleton.
287 * @flags: Flags from the #GDBusInterfaceSkeletonFlags enumeration.
289 * Sets flags describing what the behavior of @skeleton should be.
291 * Since: 2.30
293 void
294 g_dbus_interface_skeleton_set_flags (GDBusInterfaceSkeleton *interface_,
295 GDBusInterfaceSkeletonFlags flags)
297 g_return_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_));
298 g_mutex_lock (&interface_->priv->lock);
299 if (interface_->priv->flags != flags)
301 interface_->priv->flags = flags;
302 g_mutex_unlock (&interface_->priv->lock);
303 g_object_notify (G_OBJECT (interface_), "g-flags");
305 else
307 g_mutex_unlock (&interface_->priv->lock);
312 * g_dbus_interface_skeleton_get_info:
313 * @interface_: A #GDBusInterfaceSkeleton.
315 * Gets D-Bus introspection information for the D-Bus interface
316 * implemented by @interface_.
318 * Returns: (transfer none): A #GDBusInterfaceInfo (never %NULL). Do not free.
320 * Since: 2.30
322 GDBusInterfaceInfo *
323 g_dbus_interface_skeleton_get_info (GDBusInterfaceSkeleton *interface_)
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;
333 * g_dbus_interface_skeleton_get_vtable: (skip)
334 * @interface_: A #GDBusInterfaceSkeleton.
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.
340 * Returns: A #GDBusInterfaceVTable (never %NULL).
342 * Since: 2.30
344 GDBusInterfaceVTable *
345 g_dbus_interface_skeleton_get_vtable (GDBusInterfaceSkeleton *interface_)
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;
355 * g_dbus_interface_skeleton_get_properties:
356 * @interface_: A #GDBusInterfaceSkeleton.
358 * Gets all D-Bus properties for @interface_.
360 * Returns: (transfer full): A #GVariant of type
361 * ['a{sv}'][G-VARIANT-TYPE-VARDICT:CAPS].
362 * Free with g_variant_unref().
364 * Since: 2.30
366 GVariant *
367 g_dbus_interface_skeleton_get_properties (GDBusInterfaceSkeleton *interface_)
369 GVariant *ret;
370 g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), NULL);
371 ret = G_DBUS_INTERFACE_SKELETON_GET_CLASS (interface_)->get_properties (interface_);
372 return g_variant_take_ref (ret);
376 * g_dbus_interface_skeleton_flush:
377 * @interface_: A #GDBusInterfaceSkeleton.
379 * If @interface_ has outstanding changes, request for these changes to be
380 * emitted immediately.
382 * For example, an exported D-Bus interface may queue up property
383 * changes and emit the
384 * `org.freedesktop.DBus.Properties::Propert``
385 * signal later (e.g. in an idle handler). This technique is useful
386 * for collapsing multiple property changes into one.
388 * Since: 2.30
390 void
391 g_dbus_interface_skeleton_flush (GDBusInterfaceSkeleton *interface_)
393 g_return_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_));
394 G_DBUS_INTERFACE_SKELETON_GET_CLASS (interface_)->flush (interface_);
397 /* ---------------------------------------------------------------------------------------------------- */
399 static GDBusInterfaceInfo *
400 _g_dbus_interface_skeleton_get_info (GDBusInterface *interface_)
402 GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (interface_);
403 return g_dbus_interface_skeleton_get_info (interface);
406 static GDBusObject *
407 g_dbus_interface_skeleton_get_object (GDBusInterface *interface_)
409 GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (interface_);
410 GDBusObject *ret;
411 g_mutex_lock (&interface->priv->lock);
412 ret = interface->priv->object;
413 g_mutex_unlock (&interface->priv->lock);
414 return ret;
417 static GDBusObject *
418 g_dbus_interface_skeleton_dup_object (GDBusInterface *interface_)
420 GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (interface_);
421 GDBusObject *ret;
422 g_mutex_lock (&interface->priv->lock);
423 ret = interface->priv->object;
424 if (ret != NULL)
425 g_object_ref (ret);
426 g_mutex_unlock (&interface->priv->lock);
427 return ret;
430 static void
431 g_dbus_interface_skeleton_set_object (GDBusInterface *interface_,
432 GDBusObject *object)
434 GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (interface_);
435 g_mutex_lock (&interface->priv->lock);
436 if (interface->priv->object != NULL)
437 g_object_remove_weak_pointer (G_OBJECT (interface->priv->object), (gpointer *) &interface->priv->object);
438 interface->priv->object = object;
439 if (object != NULL)
440 g_object_add_weak_pointer (G_OBJECT (interface->priv->object), (gpointer *) &interface->priv->object);
441 g_mutex_unlock (&interface->priv->lock);
444 static void
445 dbus_interface_interface_init (GDBusInterfaceIface *iface)
447 iface->get_info = _g_dbus_interface_skeleton_get_info;
448 iface->get_object = g_dbus_interface_skeleton_get_object;
449 iface->dup_object = g_dbus_interface_skeleton_dup_object;
450 iface->set_object = g_dbus_interface_skeleton_set_object;
453 /* ---------------------------------------------------------------------------------------------------- */
455 typedef struct
457 volatile gint ref_count;
458 GDBusInterfaceSkeleton *interface;
459 GDBusInterfaceMethodCallFunc method_call_func;
460 GDBusMethodInvocation *invocation;
461 } DispatchData;
463 static void
464 dispatch_data_unref (DispatchData *data)
466 if (g_atomic_int_dec_and_test (&data->ref_count))
467 g_slice_free (DispatchData, data);
470 static DispatchData *
471 dispatch_data_ref (DispatchData *data)
473 g_atomic_int_inc (&data->ref_count);
474 return data;
477 static gboolean
478 dispatch_invoke_in_context_func (gpointer user_data)
480 DispatchData *data = user_data;
481 data->method_call_func (g_dbus_method_invocation_get_connection (data->invocation),
482 g_dbus_method_invocation_get_sender (data->invocation),
483 g_dbus_method_invocation_get_object_path (data->invocation),
484 g_dbus_method_invocation_get_interface_name (data->invocation),
485 g_dbus_method_invocation_get_method_name (data->invocation),
486 g_dbus_method_invocation_get_parameters (data->invocation),
487 data->invocation,
488 g_dbus_method_invocation_get_user_data (data->invocation));
489 return FALSE;
492 static void
493 dispatch_in_thread_func (GTask *task,
494 gpointer source_object,
495 gpointer task_data,
496 GCancellable *cancellable)
498 DispatchData *data = task_data;
499 GDBusInterfaceSkeletonFlags flags;
500 GDBusObject *object;
501 gboolean authorized;
503 g_mutex_lock (&data->interface->priv->lock);
504 flags = data->interface->priv->flags;
505 object = data->interface->priv->object;
506 if (object != NULL)
507 g_object_ref (object);
508 g_mutex_unlock (&data->interface->priv->lock);
510 /* first check on the enclosing object (if any), then the interface */
511 authorized = TRUE;
512 if (object != NULL)
514 g_signal_emit_by_name (object,
515 "authorize-method",
516 data->interface,
517 data->invocation,
518 &authorized);
520 if (authorized)
522 g_signal_emit (data->interface,
523 signals[G_AUTHORIZE_METHOD_SIGNAL],
525 data->invocation,
526 &authorized);
529 if (authorized)
531 gboolean run_in_thread;
532 run_in_thread = (flags & G_DBUS_INTERFACE_SKELETON_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_THREAD);
533 if (run_in_thread)
535 /* might as well just re-use the existing thread */
536 data->method_call_func (g_dbus_method_invocation_get_connection (data->invocation),
537 g_dbus_method_invocation_get_sender (data->invocation),
538 g_dbus_method_invocation_get_object_path (data->invocation),
539 g_dbus_method_invocation_get_interface_name (data->invocation),
540 g_dbus_method_invocation_get_method_name (data->invocation),
541 g_dbus_method_invocation_get_parameters (data->invocation),
542 data->invocation,
543 g_dbus_method_invocation_get_user_data (data->invocation));
545 else
547 /* bah, back to original context */
548 g_main_context_invoke_full (g_task_get_context (task),
549 g_task_get_priority (task),
550 dispatch_invoke_in_context_func,
551 dispatch_data_ref (data),
552 (GDestroyNotify) dispatch_data_unref);
555 else
557 /* do nothing */
560 if (object != NULL)
561 g_object_unref (object);
564 static void
565 g_dbus_interface_method_dispatch_helper (GDBusInterfaceSkeleton *interface,
566 GDBusInterfaceMethodCallFunc method_call_func,
567 GDBusMethodInvocation *invocation)
569 gboolean has_handlers;
570 gboolean has_default_class_handler;
571 gboolean emit_authorized_signal;
572 gboolean run_in_thread;
573 GDBusInterfaceSkeletonFlags flags;
574 GDBusObject *object;
576 g_return_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface));
577 g_return_if_fail (method_call_func != NULL);
578 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
580 g_mutex_lock (&interface->priv->lock);
581 flags = interface->priv->flags;
582 object = interface->priv->object;
583 if (object != NULL)
584 g_object_ref (object);
585 g_mutex_unlock (&interface->priv->lock);
587 /* optimization for the common case where
589 * a) no handler is connected and class handler is not overridden (both interface and object); and
590 * b) method calls are not dispatched in a thread
592 has_handlers = g_signal_has_handler_pending (interface,
593 signals[G_AUTHORIZE_METHOD_SIGNAL],
595 TRUE);
596 has_default_class_handler = (G_DBUS_INTERFACE_SKELETON_GET_CLASS (interface)->g_authorize_method ==
597 g_dbus_interface_skeleton_g_authorize_method_default);
599 emit_authorized_signal = (has_handlers || !has_default_class_handler);
600 if (!emit_authorized_signal)
602 if (object != NULL)
603 emit_authorized_signal = _g_dbus_object_skeleton_has_authorize_method_handlers (G_DBUS_OBJECT_SKELETON (object));
606 run_in_thread = (flags & G_DBUS_INTERFACE_SKELETON_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_THREAD);
607 if (!emit_authorized_signal && !run_in_thread)
609 method_call_func (g_dbus_method_invocation_get_connection (invocation),
610 g_dbus_method_invocation_get_sender (invocation),
611 g_dbus_method_invocation_get_object_path (invocation),
612 g_dbus_method_invocation_get_interface_name (invocation),
613 g_dbus_method_invocation_get_method_name (invocation),
614 g_dbus_method_invocation_get_parameters (invocation),
615 invocation,
616 g_dbus_method_invocation_get_user_data (invocation));
618 else
620 GTask *task;
621 DispatchData *data;
623 data = g_slice_new0 (DispatchData);
624 data->interface = interface;
625 data->method_call_func = method_call_func;
626 data->invocation = invocation;
627 data->ref_count = 1;
629 task = g_task_new (interface, NULL, NULL, NULL);
630 g_task_set_source_tag (task, g_dbus_interface_method_dispatch_helper);
631 g_task_set_task_data (task, data, (GDestroyNotify) dispatch_data_unref);
632 g_task_run_in_thread (task, dispatch_in_thread_func);
633 g_object_unref (task);
636 if (object != NULL)
637 g_object_unref (object);
640 static void
641 skeleton_intercept_handle_method_call (GDBusConnection *connection,
642 const gchar *sender,
643 const gchar *object_path,
644 const gchar *interface_name,
645 const gchar *method_name,
646 GVariant *parameters,
647 GDBusMethodInvocation *invocation,
648 gpointer user_data)
650 GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (user_data);
651 g_dbus_interface_method_dispatch_helper (interface,
652 g_dbus_interface_skeleton_get_vtable (interface)->method_call,
653 invocation);
656 /* ---------------------------------------------------------------------------------------------------- */
658 static ConnectionData *
659 new_connection (GDBusConnection *connection,
660 guint registration_id)
662 ConnectionData *data;
664 data = g_slice_new0 (ConnectionData);
665 data->connection = g_object_ref (connection);
666 data->registration_id = registration_id;
668 return data;
671 static void
672 free_connection (ConnectionData *data)
674 if (data != NULL)
676 g_object_unref (data->connection);
677 g_slice_free (ConnectionData, data);
681 static gboolean
682 add_connection_locked (GDBusInterfaceSkeleton *interface_,
683 GDBusConnection *connection,
684 GError **error)
686 ConnectionData *data;
687 guint registration_id;
688 gboolean ret = FALSE;
690 if (interface_->priv->hooked_vtable == NULL)
692 /* Hook the vtable since we need to intercept method calls for
693 * ::g-authorize-method and for dispatching in thread vs
694 * context
696 * We need to wait until subclasses have had time to initialize
697 * properly before building the hooked_vtable, so we create it
698 * once at the last minute.
700 interface_->priv->hooked_vtable = g_memdup (g_dbus_interface_skeleton_get_vtable (interface_), sizeof (GDBusInterfaceVTable));
701 interface_->priv->hooked_vtable->method_call = skeleton_intercept_handle_method_call;
704 registration_id = g_dbus_connection_register_object (connection,
705 interface_->priv->object_path,
706 g_dbus_interface_skeleton_get_info (interface_),
707 interface_->priv->hooked_vtable,
708 interface_,
709 NULL, /* user_data_free_func */
710 error);
712 if (registration_id > 0)
714 data = new_connection (connection, registration_id);
715 interface_->priv->connections = g_slist_append (interface_->priv->connections, data);
716 ret = TRUE;
719 return ret;
722 static void
723 remove_connection_locked (GDBusInterfaceSkeleton *interface_,
724 GDBusConnection *connection)
726 ConnectionData *data;
727 GSList *l;
729 /* Get the connection in the list and unregister ... */
730 for (l = interface_->priv->connections; l != NULL; l = l->next)
732 data = l->data;
733 if (data->connection == connection)
735 g_warn_if_fail (g_dbus_connection_unregister_object (data->connection, data->registration_id));
736 free_connection (data);
737 interface_->priv->connections = g_slist_delete_link (interface_->priv->connections, l);
738 /* we are guaranteed that the connection is only added once, so bail out early */
739 goto out;
742 out:
746 static void
747 set_object_path_locked (GDBusInterfaceSkeleton *interface_,
748 const gchar *object_path)
750 if (g_strcmp0 (interface_->priv->object_path, object_path) != 0)
752 g_free (interface_->priv->object_path);
753 interface_->priv->object_path = g_strdup (object_path);
757 /* ---------------------------------------------------------------------------------------------------- */
760 * g_dbus_interface_skeleton_get_connection:
761 * @interface_: A #GDBusInterfaceSkeleton.
763 * Gets the first connection that @interface_ is exported on, if any.
765 * Returns: (transfer none): A #GDBusConnection or %NULL if @interface_ is
766 * not exported anywhere. Do not free, the object belongs to @interface_.
768 * Since: 2.30
770 GDBusConnection *
771 g_dbus_interface_skeleton_get_connection (GDBusInterfaceSkeleton *interface_)
773 ConnectionData *data;
774 GDBusConnection *ret;
776 g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), NULL);
777 g_mutex_lock (&interface_->priv->lock);
779 ret = NULL;
780 if (interface_->priv->connections != NULL)
782 data = interface_->priv->connections->data;
783 if (data != NULL)
784 ret = data->connection;
787 g_mutex_unlock (&interface_->priv->lock);
789 return ret;
793 * g_dbus_interface_skeleton_get_connections:
794 * @interface_: A #GDBusInterfaceSkeleton.
796 * Gets a list of the connections that @interface_ is exported on.
798 * Returns: (element-type GDBusConnection) (transfer full): A list of
799 * all the connections that @interface_ is exported on. The returned
800 * list should be freed with g_list_free() after each element has
801 * been freed with g_object_unref().
803 * Since: 2.32
805 GList *
806 g_dbus_interface_skeleton_get_connections (GDBusInterfaceSkeleton *interface_)
808 GList *connections;
809 GSList *l;
810 ConnectionData *data;
812 g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), NULL);
814 g_mutex_lock (&interface_->priv->lock);
815 connections = NULL;
817 for (l = interface_->priv->connections; l != NULL; l = l->next)
819 data = l->data;
820 connections = g_list_prepend (connections,
821 /* Return a reference to each connection */
822 g_object_ref (data->connection));
825 g_mutex_unlock (&interface_->priv->lock);
827 return g_list_reverse (connections);
831 * g_dbus_interface_skeleton_has_connection:
832 * @interface_: A #GDBusInterfaceSkeleton.
833 * @connection: A #GDBusConnection.
835 * Checks if @interface_ is exported on @connection.
837 * Returns: %TRUE if @interface_ is exported on @connection, %FALSE otherwise.
839 * Since: 2.32
841 gboolean
842 g_dbus_interface_skeleton_has_connection (GDBusInterfaceSkeleton *interface_,
843 GDBusConnection *connection)
845 GSList *l;
846 gboolean ret = FALSE;
848 g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), FALSE);
849 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE);
851 g_mutex_lock (&interface_->priv->lock);
853 for (l = interface_->priv->connections; l != NULL; l = l->next)
855 ConnectionData *data = l->data;
856 if (data->connection == connection)
858 ret = TRUE;
859 goto out;
863 out:
864 g_mutex_unlock (&interface_->priv->lock);
865 return ret;
869 * g_dbus_interface_skeleton_get_object_path:
870 * @interface_: A #GDBusInterfaceSkeleton.
872 * Gets the object path that @interface_ is exported on, if any.
874 * Returns: A string owned by @interface_ or %NULL if @interface_ is not exported
875 * anywhere. Do not free, the string belongs to @interface_.
877 * Since: 2.30
879 const gchar *
880 g_dbus_interface_skeleton_get_object_path (GDBusInterfaceSkeleton *interface_)
882 const gchar *ret;
883 g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), NULL);
884 g_mutex_lock (&interface_->priv->lock);
885 ret = interface_->priv->object_path;
886 g_mutex_unlock (&interface_->priv->lock);
887 return ret;
891 * g_dbus_interface_skeleton_export:
892 * @interface_: The D-Bus interface to export.
893 * @connection: A #GDBusConnection to export @interface_ on.
894 * @object_path: The path to export the interface at.
895 * @error: Return location for error or %NULL.
897 * Exports @interface_ at @object_path on @connection.
899 * This can be called multiple times to export the same @interface_
900 * onto multiple connections however the @object_path provided must be
901 * the same for all connections.
903 * Use g_dbus_interface_skeleton_unexport() to unexport the object.
905 * Returns: %TRUE if the interface was exported on @connection, otherwise %FALSE with
906 * @error set.
908 * Since: 2.30
910 gboolean
911 g_dbus_interface_skeleton_export (GDBusInterfaceSkeleton *interface_,
912 GDBusConnection *connection,
913 const gchar *object_path,
914 GError **error)
916 gboolean ret = FALSE;
918 g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), FALSE);
919 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE);
920 g_return_val_if_fail (g_variant_is_object_path (object_path), FALSE);
921 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
923 /* Assert that the object path is the same for multiple connections here */
924 g_return_val_if_fail (interface_->priv->object_path == NULL ||
925 g_strcmp0 (interface_->priv->object_path, object_path) == 0, FALSE);
927 g_mutex_lock (&interface_->priv->lock);
929 /* Set the object path */
930 set_object_path_locked (interface_, object_path);
932 /* Add the connection */
933 ret = add_connection_locked (interface_, connection, error);
935 g_mutex_unlock (&interface_->priv->lock);
936 return ret;
940 * g_dbus_interface_skeleton_unexport:
941 * @interface_: A #GDBusInterfaceSkeleton.
943 * Stops exporting @interface_ on all connections it is exported on.
945 * To unexport @interface_ from only a single connection, use
946 * g_dbus_interface_skeleton_unexport_from_connection()
948 * Since: 2.30
950 void
951 g_dbus_interface_skeleton_unexport (GDBusInterfaceSkeleton *interface_)
953 g_return_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_));
954 g_return_if_fail (interface_->priv->connections != NULL);
956 g_mutex_lock (&interface_->priv->lock);
958 g_assert (interface_->priv->object_path != NULL);
959 g_assert (interface_->priv->hooked_vtable != NULL);
961 /* Remove all connections */
962 while (interface_->priv->connections != NULL)
964 ConnectionData *data = interface_->priv->connections->data;
965 remove_connection_locked (interface_, data->connection);
968 /* Unset the object path since there are no connections left */
969 set_object_path_locked (interface_, NULL);
971 g_mutex_unlock (&interface_->priv->lock);
976 * g_dbus_interface_skeleton_unexport_from_connection:
977 * @interface_: A #GDBusInterfaceSkeleton.
978 * @connection: A #GDBusConnection.
980 * Stops exporting @interface_ on @connection.
982 * To stop exporting on all connections the interface is exported on,
983 * use g_dbus_interface_skeleton_unexport().
985 * Since: 2.32
987 void
988 g_dbus_interface_skeleton_unexport_from_connection (GDBusInterfaceSkeleton *interface_,
989 GDBusConnection *connection)
991 g_return_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_));
992 g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
993 g_return_if_fail (interface_->priv->connections != NULL);
995 g_mutex_lock (&interface_->priv->lock);
997 g_assert (interface_->priv->object_path != NULL);
998 g_assert (interface_->priv->hooked_vtable != NULL);
1000 remove_connection_locked (interface_, connection);
1002 /* Reset the object path if we removed the last connection */
1003 if (interface_->priv->connections == NULL)
1004 set_object_path_locked (interface_, NULL);
1006 g_mutex_unlock (&interface_->priv->lock);
1009 /* ---------------------------------------------------------------------------------------------------- */