Fully implement app-menu and menubar properties
[glib.git] / gio / gdbusinterfaceskeleton.c
blobe6c0a49e77b75093e9af101ee2c819d4aee27c84
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, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: David Zeuthen <davidz@redhat.com>
23 #include "config.h"
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"
35 #include "glibintl.h"
37 /**
38 * SECTION:gdbusinterfaceskeleton
39 * @short_description: Service-side D-Bus interface
40 * @include: gio/gio.h
42 * Abstract base class for D-Bus interfaces on the service side.
45 struct _GDBusInterfaceSkeletonPrivate
47 GMutex lock;
49 GDBusObject *object;
50 GDBusInterfaceSkeletonFlags flags;
52 GSList *connections; /* List of ConnectionData */
53 gchar *object_path; /* The object path for this skeleton */
54 GDBusInterfaceVTable *hooked_vtable;
57 typedef struct
59 GDBusConnection *connection;
60 guint registration_id;
61 } ConnectionData;
63 enum
65 G_AUTHORIZE_METHOD_SIGNAL,
66 LAST_SIGNAL
69 enum
71 PROP_0,
72 PROP_G_FLAGS
75 static guint signals[LAST_SIGNAL] = {0};
77 static void dbus_interface_interface_init (GDBusInterfaceIface *iface);
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);
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));
96 static void
97 g_dbus_interface_skeleton_finalize (GObject *object)
99 GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (object);
101 /* Hold the lock just incase any code we call verifies that the lock is held */
102 g_mutex_lock (&interface->priv->lock);
104 /* unexport from all connections if we're exported anywhere */
105 while (interface->priv->connections != NULL)
107 ConnectionData *data = interface->priv->connections->data;
108 remove_connection_locked (interface, data->connection);
111 set_object_path_locked (interface, NULL);
113 g_mutex_unlock (&interface->priv->lock);
115 g_free (interface->priv->hooked_vtable);
117 if (interface->priv->object != NULL)
118 g_object_remove_weak_pointer (G_OBJECT (interface->priv->object), (gpointer *) &interface->priv->object);
120 g_mutex_clear (&interface->priv->lock);
122 G_OBJECT_CLASS (g_dbus_interface_skeleton_parent_class)->finalize (object);
125 static void
126 g_dbus_interface_skeleton_get_property (GObject *object,
127 guint prop_id,
128 GValue *value,
129 GParamSpec *pspec)
131 GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (object);
133 switch (prop_id)
135 case PROP_G_FLAGS:
136 g_value_set_flags (value, g_dbus_interface_skeleton_get_flags (interface));
137 break;
139 default:
140 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
141 break;
145 static void
146 g_dbus_interface_skeleton_set_property (GObject *object,
147 guint prop_id,
148 const GValue *value,
149 GParamSpec *pspec)
151 GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (object);
153 switch (prop_id)
155 case PROP_G_FLAGS:
156 g_dbus_interface_skeleton_set_flags (interface, g_value_get_flags (value));
157 break;
159 default:
160 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
161 break;
165 static gboolean
166 g_dbus_interface_skeleton_g_authorize_method_default (GDBusInterfaceSkeleton *interface,
167 GDBusMethodInvocation *invocation)
169 return TRUE;
172 static void
173 g_dbus_interface_skeleton_class_init (GDBusInterfaceSkeletonClass *klass)
175 GObjectClass *gobject_class;
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;
182 klass->g_authorize_method = g_dbus_interface_skeleton_g_authorize_method_default;
185 * GDBusInterfaceSkeleton:g-flags:
187 * Flags from the #GDBusInterfaceSkeletonFlags enumeration.
189 * Since: 2.30
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));
203 * GDBusInterfaceSkeleton::g-authorize-method:
204 * @interface: The #GDBusInterfaceSkeleton emitting the signal.
205 * @invocation: A #GDBusMethodInvocation.
207 * Emitted when a method is invoked by a remote caller and used to
208 * determine if the method call is authorized.
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.
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()).
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.
229 * The default class handler just returns %TRUE.
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.
241 * Returns: %TRUE if the call is authorized, %FALSE otherwise.
243 * Since: 2.30
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,
255 G_TYPE_DBUS_METHOD_INVOCATION);
257 g_type_class_add_private (klass, sizeof (GDBusInterfaceSkeletonPrivate));
260 static void
261 g_dbus_interface_skeleton_init (GDBusInterfaceSkeleton *interface)
263 interface->priv = G_TYPE_INSTANCE_GET_PRIVATE (interface, G_TYPE_DBUS_INTERFACE_SKELETON, GDBusInterfaceSkeletonPrivate);
264 g_mutex_init (&interface->priv->lock);
267 /* ---------------------------------------------------------------------------------------------------- */
270 * g_dbus_interface_skeleton_get_flags:
271 * @interface_: A #GDBusInterfaceSkeleton.
273 * Gets the #GDBusInterfaceSkeletonFlags that describes what the behavior
274 * of @interface_
276 * Returns: One or more flags from the #GDBusInterfaceSkeletonFlags enumeration.
278 * Since: 2.30
280 GDBusInterfaceSkeletonFlags
281 g_dbus_interface_skeleton_get_flags (GDBusInterfaceSkeleton *interface_)
283 g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), G_DBUS_INTERFACE_SKELETON_FLAGS_NONE);
284 return interface_->priv->flags;
288 * g_dbus_interface_skeleton_set_flags:
289 * @interface_: A #GDBusInterfaceSkeleton.
290 * @flags: Flags from the #GDBusInterfaceSkeletonFlags enumeration.
292 * Sets flags describing what the behavior of @skeleton should be.
294 * Since: 2.30
296 void
297 g_dbus_interface_skeleton_set_flags (GDBusInterfaceSkeleton *interface_,
298 GDBusInterfaceSkeletonFlags flags)
300 g_return_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_));
301 g_mutex_lock (&interface_->priv->lock);
302 if (interface_->priv->flags != flags)
304 interface_->priv->flags = flags;
305 g_mutex_unlock (&interface_->priv->lock);
306 g_object_notify (G_OBJECT (interface_), "g-flags");
308 else
310 g_mutex_unlock (&interface_->priv->lock);
315 * g_dbus_interface_skeleton_get_info:
316 * @interface_: A #GDBusInterfaceSkeleton.
318 * Gets D-Bus introspection information for the D-Bus interface
319 * implemented by @interface_.
321 * Returns: (transfer none): A #GDBusInterfaceInfo (never %NULL). Do not free.
323 * Since: 2.30
325 GDBusInterfaceInfo *
326 g_dbus_interface_skeleton_get_info (GDBusInterfaceSkeleton *interface_)
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;
336 * g_dbus_interface_skeleton_get_vtable: (skip)
337 * @interface_: A #GDBusInterfaceSkeleton.
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.
343 * Returns: A #GDBusInterfaceVTable (never %NULL).
345 * Since: 2.30
347 GDBusInterfaceVTable *
348 g_dbus_interface_skeleton_get_vtable (GDBusInterfaceSkeleton *interface_)
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;
358 * g_dbus_interface_skeleton_get_properties:
359 * @interface_: A #GDBusInterfaceSkeleton.
361 * Gets all D-Bus properties for @interface_.
363 * Returns: (transfer full): A #GVariant of type <link linkend="G-VARIANT-TYPE-VARDICT:CAPS">'a{sv}'</link>. Free with g_variant_unref().
365 * Since: 2.30
367 GVariant *
368 g_dbus_interface_skeleton_get_properties (GDBusInterfaceSkeleton *interface_)
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);
377 * g_dbus_interface_skeleton_flush:
378 * @interface_: A #GDBusInterfaceSkeleton.
380 * If @interface_ has outstanding changes, request for these changes to be
381 * emitted immediately.
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.
389 * Since: 2.30
391 void
392 g_dbus_interface_skeleton_flush (GDBusInterfaceSkeleton *interface_)
394 g_return_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_));
395 G_DBUS_INTERFACE_SKELETON_GET_CLASS (interface_)->flush (interface_);
398 /* ---------------------------------------------------------------------------------------------------- */
400 static GDBusInterfaceInfo *
401 _g_dbus_interface_skeleton_get_info (GDBusInterface *interface_)
403 GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (interface_);
404 return g_dbus_interface_skeleton_get_info (interface);
407 static GDBusObject *
408 g_dbus_interface_skeleton_get_object (GDBusInterface *interface_)
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;
418 static void
419 g_dbus_interface_skeleton_set_object (GDBusInterface *interface_,
420 GDBusObject *object)
422 GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (interface_);
423 g_mutex_lock (&interface->priv->lock);
424 if (interface->priv->object != NULL)
425 g_object_remove_weak_pointer (G_OBJECT (interface->priv->object), (gpointer *) &interface->priv->object);
426 interface->priv->object = object;
427 if (object != NULL)
428 g_object_add_weak_pointer (G_OBJECT (interface->priv->object), (gpointer *) &interface->priv->object);
429 g_mutex_unlock (&interface->priv->lock);
432 static void
433 dbus_interface_interface_init (GDBusInterfaceIface *iface)
435 iface->get_info = _g_dbus_interface_skeleton_get_info;
436 iface->get_object = g_dbus_interface_skeleton_get_object;
437 iface->set_object = g_dbus_interface_skeleton_set_object;
440 /* ---------------------------------------------------------------------------------------------------- */
442 typedef struct
444 volatile gint ref_count;
445 GDBusInterfaceSkeleton *interface;
446 GDBusInterfaceMethodCallFunc method_call_func;
447 GDBusMethodInvocation *invocation;
448 GMainContext *context;
449 } DispatchData;
451 static void
452 dispatch_data_unref (DispatchData *data)
454 if (g_atomic_int_dec_and_test (&data->ref_count))
456 g_main_context_unref (data->context);
457 g_free (data);
461 static DispatchData *
462 dispatch_data_ref (DispatchData *data)
464 g_atomic_int_inc (&data->ref_count);
465 return data;
468 static gboolean
469 dispatch_invoke_in_context_func (gpointer user_data)
471 DispatchData *data = user_data;
472 data->method_call_func (g_dbus_method_invocation_get_connection (data->invocation),
473 g_dbus_method_invocation_get_sender (data->invocation),
474 g_dbus_method_invocation_get_object_path (data->invocation),
475 g_dbus_method_invocation_get_interface_name (data->invocation),
476 g_dbus_method_invocation_get_method_name (data->invocation),
477 g_dbus_method_invocation_get_parameters (data->invocation),
478 data->invocation,
479 g_dbus_method_invocation_get_user_data (data->invocation));
480 return FALSE;
483 static gboolean
484 dispatch_in_thread_func (GIOSchedulerJob *job,
485 GCancellable *cancellable,
486 gpointer user_data)
488 DispatchData *data = user_data;
489 GDBusInterfaceSkeletonFlags flags;
490 GDBusObject *object;
491 gboolean authorized;
493 g_mutex_lock (&data->interface->priv->lock);
494 flags = data->interface->priv->flags;
495 object = data->interface->priv->object;
496 if (object != NULL)
497 g_object_ref (object);
498 g_mutex_unlock (&data->interface->priv->lock);
500 /* first check on the enclosing object (if any), then the interface */
501 authorized = TRUE;
502 if (object != NULL)
504 g_signal_emit_by_name (object,
505 "authorize-method",
506 data->interface,
507 data->invocation,
508 &authorized);
510 if (authorized)
512 g_signal_emit (data->interface,
513 signals[G_AUTHORIZE_METHOD_SIGNAL],
515 data->invocation,
516 &authorized);
519 if (authorized)
521 gboolean run_in_thread;
522 run_in_thread = (flags & G_DBUS_INTERFACE_SKELETON_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_THREAD);
523 if (run_in_thread)
525 /* might as well just re-use the existing thread */
526 data->method_call_func (g_dbus_method_invocation_get_connection (data->invocation),
527 g_dbus_method_invocation_get_sender (data->invocation),
528 g_dbus_method_invocation_get_object_path (data->invocation),
529 g_dbus_method_invocation_get_interface_name (data->invocation),
530 g_dbus_method_invocation_get_method_name (data->invocation),
531 g_dbus_method_invocation_get_parameters (data->invocation),
532 data->invocation,
533 g_dbus_method_invocation_get_user_data (data->invocation));
535 else
537 /* bah, back to original context */
538 g_main_context_invoke_full (data->context,
539 G_PRIORITY_DEFAULT,
540 dispatch_invoke_in_context_func,
541 dispatch_data_ref (data),
542 (GDestroyNotify) dispatch_data_unref);
545 else
547 /* do nothing */
550 if (object != NULL)
551 g_object_unref (object);
553 return FALSE;
556 static void
557 g_dbus_interface_method_dispatch_helper (GDBusInterfaceSkeleton *interface,
558 GDBusInterfaceMethodCallFunc method_call_func,
559 GDBusMethodInvocation *invocation)
561 gboolean has_handlers;
562 gboolean has_default_class_handler;
563 gboolean emit_authorized_signal;
564 gboolean run_in_thread;
565 GDBusInterfaceSkeletonFlags flags;
566 GDBusObject *object;
568 g_return_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface));
569 g_return_if_fail (method_call_func != NULL);
570 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
572 g_mutex_lock (&interface->priv->lock);
573 flags = interface->priv->flags;
574 object = interface->priv->object;
575 if (object != NULL)
576 g_object_ref (object);
577 g_mutex_unlock (&interface->priv->lock);
579 /* optimization for the common case where
581 * a) no handler is connected and class handler is not overridden (both interface and object); and
582 * b) method calls are not dispatched in a thread
584 has_handlers = g_signal_has_handler_pending (interface,
585 signals[G_AUTHORIZE_METHOD_SIGNAL],
587 TRUE);
588 has_default_class_handler = (G_DBUS_INTERFACE_SKELETON_GET_CLASS (interface)->g_authorize_method ==
589 g_dbus_interface_skeleton_g_authorize_method_default);
591 emit_authorized_signal = (has_handlers || !has_default_class_handler);
592 if (!emit_authorized_signal)
594 if (object != NULL)
595 emit_authorized_signal = _g_dbus_object_skeleton_has_authorize_method_handlers (G_DBUS_OBJECT_SKELETON (object));
598 run_in_thread = (flags & G_DBUS_INTERFACE_SKELETON_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_THREAD);
599 if (!emit_authorized_signal && !run_in_thread)
601 method_call_func (g_dbus_method_invocation_get_connection (invocation),
602 g_dbus_method_invocation_get_sender (invocation),
603 g_dbus_method_invocation_get_object_path (invocation),
604 g_dbus_method_invocation_get_interface_name (invocation),
605 g_dbus_method_invocation_get_method_name (invocation),
606 g_dbus_method_invocation_get_parameters (invocation),
607 invocation,
608 g_dbus_method_invocation_get_user_data (invocation));
610 else
612 DispatchData *data;
613 data = g_new0 (DispatchData, 1);
614 data->interface = interface;
615 data->method_call_func = method_call_func;
616 data->invocation = invocation;
617 data->context = g_main_context_ref_thread_default ();
618 data->ref_count = 1;
619 g_io_scheduler_push_job (dispatch_in_thread_func,
620 data,
621 (GDestroyNotify) dispatch_data_unref,
622 G_PRIORITY_DEFAULT,
623 NULL); /* GCancellable* */
626 if (object != NULL)
627 g_object_unref (object);
630 static void
631 skeleton_intercept_handle_method_call (GDBusConnection *connection,
632 const gchar *sender,
633 const gchar *object_path,
634 const gchar *interface_name,
635 const gchar *method_name,
636 GVariant *parameters,
637 GDBusMethodInvocation *invocation,
638 gpointer user_data)
640 GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (user_data);
641 g_dbus_interface_method_dispatch_helper (interface,
642 g_dbus_interface_skeleton_get_vtable (interface)->method_call,
643 invocation);
646 /* ---------------------------------------------------------------------------------------------------- */
648 static ConnectionData *
649 new_connection (GDBusConnection *connection,
650 guint registration_id)
652 ConnectionData *data;
654 data = g_slice_new0 (ConnectionData);
655 data->connection = g_object_ref (connection);
656 data->registration_id = registration_id;
658 return data;
661 static void
662 free_connection (ConnectionData *data)
664 if (data != NULL)
666 g_object_unref (data->connection);
667 g_slice_free (ConnectionData, data);
671 static gboolean
672 add_connection_locked (GDBusInterfaceSkeleton *interface_,
673 GDBusConnection *connection,
674 GError **error)
676 ConnectionData *data;
677 guint registration_id;
678 gboolean ret = FALSE;
680 if (interface_->priv->hooked_vtable == NULL)
682 /* Hook the vtable since we need to intercept method calls for
683 * ::g-authorize-method and for dispatching in thread vs
684 * context
686 * We need to wait until subclasses have had time to initialize
687 * properly before building the hooked_vtable, so we create it
688 * once at the last minute.
690 interface_->priv->hooked_vtable = g_memdup (g_dbus_interface_skeleton_get_vtable (interface_), sizeof (GDBusInterfaceVTable));
691 interface_->priv->hooked_vtable->method_call = skeleton_intercept_handle_method_call;
694 registration_id = g_dbus_connection_register_object (connection,
695 interface_->priv->object_path,
696 g_dbus_interface_skeleton_get_info (interface_),
697 interface_->priv->hooked_vtable,
698 interface_,
699 NULL, /* user_data_free_func */
700 error);
702 if (registration_id > 0)
704 data = new_connection (connection, registration_id);
705 interface_->priv->connections = g_slist_append (interface_->priv->connections, data);
706 ret = TRUE;
709 return ret;
712 static void
713 remove_connection_locked (GDBusInterfaceSkeleton *interface_,
714 GDBusConnection *connection)
716 ConnectionData *data;
717 GSList *l;
719 /* Get the connection in the list and unregister ... */
720 for (l = interface_->priv->connections; l != NULL; l = l->next)
722 data = l->data;
723 if (data->connection == connection)
725 g_warn_if_fail (g_dbus_connection_unregister_object (data->connection, data->registration_id));
726 free_connection (data);
727 interface_->priv->connections = g_slist_delete_link (interface_->priv->connections, l);
728 /* we are guaranteed that the connection is only added once, so bail out early */
729 goto out;
732 out:
736 static void
737 set_object_path_locked (GDBusInterfaceSkeleton *interface_,
738 const gchar *object_path)
740 if (g_strcmp0 (interface_->priv->object_path, object_path) != 0)
742 g_free (interface_->priv->object_path);
743 interface_->priv->object_path = g_strdup (object_path);
747 /* ---------------------------------------------------------------------------------------------------- */
750 * g_dbus_interface_skeleton_get_connection:
751 * @interface_: A #GDBusInterfaceSkeleton.
753 * Gets the first connection that @interface_ is exported on, if any.
755 * Returns: (transfer none): A #GDBusConnection or %NULL if @interface_ is
756 * not exported anywhere. Do not free, the object belongs to @interface_.
758 * Since: 2.30
760 GDBusConnection *
761 g_dbus_interface_skeleton_get_connection (GDBusInterfaceSkeleton *interface_)
763 ConnectionData *data;
764 GDBusConnection *ret;
766 g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), NULL);
767 g_mutex_lock (&interface_->priv->lock);
769 ret = NULL;
770 if (interface_->priv->connections != NULL)
772 data = interface_->priv->connections->data;
773 if (data != NULL)
774 ret = data->connection;
777 g_mutex_unlock (&interface_->priv->lock);
779 return ret;
783 * g_dbus_interface_skeleton_get_connections:
784 * @interface_: A #GDBusInterfaceSkeleton.
786 * Gets a list of the connections that @interface_ is exported on.
788 * Returns: (element-type GDBusConnection) (transfer full): A list of
789 * all the connections that @interface_ is exported on. The returned
790 * list should be freed with g_list_free() after each element has
791 * been freed with g_object_unref().
793 * Since: 2.32
795 GList *
796 g_dbus_interface_skeleton_get_connections (GDBusInterfaceSkeleton *interface_)
798 GList *connections;
799 GSList *l;
800 ConnectionData *data;
802 g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), NULL);
804 g_mutex_lock (&interface_->priv->lock);
805 connections = NULL;
807 for (l = interface_->priv->connections; l != NULL; l = l->next)
809 data = l->data;
810 connections = g_list_prepend (connections,
811 /* Return a reference to each connection */
812 g_object_ref (data->connection));
815 g_mutex_unlock (&interface_->priv->lock);
817 return g_list_reverse (connections);
821 * g_dbus_interface_skeleton_has_connection:
822 * @interface_: A #GDBusInterfaceSkeleton.
823 * @connection: A #GDBusConnection.
825 * Checks if @interface_ is export on @connection.
827 * Returns: %TRUE if @interface_ is exported on @connection, %FALSE otherwise.
829 * Since: 2.32
831 gboolean
832 g_dbus_interface_skeleton_has_connection (GDBusInterfaceSkeleton *interface_,
833 GDBusConnection *connection)
835 GSList *l;
836 gboolean ret = FALSE;
838 g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), FALSE);
839 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE);
841 g_mutex_lock (&interface_->priv->lock);
843 for (l = interface_->priv->connections; l != NULL; l = l->next)
845 ConnectionData *data = l->data;
846 if (data->connection == connection)
848 ret = TRUE;
849 goto out;
853 out:
854 g_mutex_unlock (&interface_->priv->lock);
855 return ret;
859 * g_dbus_interface_skeleton_get_object_path:
860 * @interface_: A #GDBusInterfaceSkeleton.
862 * Gets the object path that @interface_ is exported on, if any.
864 * Returns: A string owned by @interface_ or %NULL if @interface_ is not exported
865 * anywhere. Do not free, the string belongs to @interface_.
867 * Since: 2.30
869 const gchar *
870 g_dbus_interface_skeleton_get_object_path (GDBusInterfaceSkeleton *interface_)
872 const gchar *ret;
873 g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), NULL);
874 g_mutex_lock (&interface_->priv->lock);
875 ret = interface_->priv->object_path;
876 g_mutex_unlock (&interface_->priv->lock);
877 return ret;
881 * g_dbus_interface_skeleton_export:
882 * @interface_: The D-Bus interface to export.
883 * @connection: A #GDBusConnection to export @interface_ on.
884 * @object_path: The path to export the interface at.
885 * @error: Return location for error or %NULL.
887 * Exports @interface_ at @object_path on @connection.
889 * This can be called multiple times to export the same @interface_
890 * onto multiple connections however the @object_path provided must be
891 * the same for all connections.
893 * Use g_dbus_interface_skeleton_unexport() to unexport the object.
895 * Returns: %TRUE if the interface was exported on @connection, otherwise %FALSE with
896 * @error set.
898 * Since: 2.30
900 gboolean
901 g_dbus_interface_skeleton_export (GDBusInterfaceSkeleton *interface_,
902 GDBusConnection *connection,
903 const gchar *object_path,
904 GError **error)
906 gboolean ret = FALSE;
908 g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), FALSE);
909 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE);
910 g_return_val_if_fail (g_variant_is_object_path (object_path), FALSE);
911 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
913 /* Assert that the object path is the same for multiple connections here */
914 g_return_val_if_fail (interface_->priv->object_path == NULL ||
915 g_strcmp0 (interface_->priv->object_path, object_path) == 0, FALSE);
917 g_mutex_lock (&interface_->priv->lock);
919 /* Set the object path */
920 set_object_path_locked (interface_, object_path);
922 /* Add the connection */
923 ret = add_connection_locked (interface_, connection, error);
925 g_mutex_unlock (&interface_->priv->lock);
926 return ret;
930 * g_dbus_interface_skeleton_unexport:
931 * @interface_: A #GDBusInterfaceSkeleton.
933 * Stops exporting @interface_ on all connections it is exported on.
935 * To unexport @interface_ from only a single connection, use
936 * g_dbus_interface_skeleton_export_from_connection()
938 * Since: 2.30
940 void
941 g_dbus_interface_skeleton_unexport (GDBusInterfaceSkeleton *interface_)
943 g_return_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_));
944 g_return_if_fail (interface_->priv->connections != NULL);
946 g_mutex_lock (&interface_->priv->lock);
948 g_assert (interface_->priv->object_path != NULL);
949 g_assert (interface_->priv->hooked_vtable != NULL);
951 /* Remove all connections */
952 while (interface_->priv->connections != NULL)
954 ConnectionData *data = interface_->priv->connections->data;
955 remove_connection_locked (interface_, data->connection);
958 /* Unset the object path since there are no connections left */
959 set_object_path_locked (interface_, NULL);
961 g_mutex_unlock (&interface_->priv->lock);
966 * g_dbus_interface_skeleton_unexport_from_connection:
967 * @interface_: A #GDBusInterfaceSkeleton.
968 * @connection: A #GDBusConnection.
970 * Stops exporting @interface_ on @connection.
972 * To stop exporting on all connections the interface is exported on,
973 * use g_dbus_interface_skeleton_unexport().
975 * Since: 2.32
977 void
978 g_dbus_interface_skeleton_unexport_from_connection (GDBusInterfaceSkeleton *interface_,
979 GDBusConnection *connection)
981 g_return_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_));
982 g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
983 g_return_if_fail (interface_->priv->connections != NULL);
985 g_mutex_lock (&interface_->priv->lock);
987 g_assert (interface_->priv->object_path != NULL);
988 g_assert (interface_->priv->hooked_vtable != NULL);
990 remove_connection_locked (interface_, connection);
992 /* Reset the object path if we removed the last connection */
993 if (interface_->priv->connections == NULL)
994 set_object_path_locked (interface_, NULL);
996 g_mutex_unlock (&interface_->priv->lock);
999 /* ---------------------------------------------------------------------------------------------------- */