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>
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"
38 * SECTION:gdbusinterfaceskeleton
39 * @short_description: Service-side D-Bus interface
42 * Abstract base class for D-Bus interfaces on the service side.
45 struct _GDBusInterfaceSkeletonPrivate
50 GDBusInterfaceSkeletonFlags flags
;
52 GSList
*connections
; /* List of ConnectionData */
53 gchar
*object_path
; /* The object path for this skeleton */
54 GDBusInterfaceVTable
*hooked_vtable
;
59 GDBusConnection
*connection
;
60 guint registration_id
;
65 G_AUTHORIZE_METHOD_SIGNAL
,
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
,
85 const gchar
*object_path
,
86 const gchar
*interface_name
,
87 const gchar
*method_name
,
89 GDBusMethodInvocation
*invocation
,
93 G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GDBusInterfaceSkeleton
, g_dbus_interface_skeleton
, G_TYPE_OBJECT
,
94 G_ADD_PRIVATE (GDBusInterfaceSkeleton
)
95 G_IMPLEMENT_INTERFACE (G_TYPE_DBUS_INTERFACE
, dbus_interface_interface_init
))
98 g_dbus_interface_skeleton_finalize (GObject
*object
)
100 GDBusInterfaceSkeleton
*interface
= G_DBUS_INTERFACE_SKELETON (object
);
102 /* Hold the lock just incase any code we call verifies that the lock is held */
103 g_mutex_lock (&interface
->priv
->lock
);
105 /* unexport from all connections if we're exported anywhere */
106 while (interface
->priv
->connections
!= NULL
)
108 ConnectionData
*data
= interface
->priv
->connections
->data
;
109 remove_connection_locked (interface
, data
->connection
);
112 set_object_path_locked (interface
, NULL
);
114 g_mutex_unlock (&interface
->priv
->lock
);
116 g_free (interface
->priv
->hooked_vtable
);
118 if (interface
->priv
->object
!= NULL
)
119 g_object_remove_weak_pointer (G_OBJECT (interface
->priv
->object
), (gpointer
*) &interface
->priv
->object
);
121 g_mutex_clear (&interface
->priv
->lock
);
123 G_OBJECT_CLASS (g_dbus_interface_skeleton_parent_class
)->finalize (object
);
127 g_dbus_interface_skeleton_get_property (GObject
*object
,
132 GDBusInterfaceSkeleton
*interface
= G_DBUS_INTERFACE_SKELETON (object
);
137 g_value_set_flags (value
, g_dbus_interface_skeleton_get_flags (interface
));
141 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
147 g_dbus_interface_skeleton_set_property (GObject
*object
,
152 GDBusInterfaceSkeleton
*interface
= G_DBUS_INTERFACE_SKELETON (object
);
157 g_dbus_interface_skeleton_set_flags (interface
, g_value_get_flags (value
));
161 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
167 g_dbus_interface_skeleton_g_authorize_method_default (GDBusInterfaceSkeleton
*interface
,
168 GDBusMethodInvocation
*invocation
)
174 g_dbus_interface_skeleton_class_init (GDBusInterfaceSkeletonClass
*klass
)
176 GObjectClass
*gobject_class
;
178 gobject_class
= G_OBJECT_CLASS (klass
);
179 gobject_class
->finalize
= g_dbus_interface_skeleton_finalize
;
180 gobject_class
->set_property
= g_dbus_interface_skeleton_set_property
;
181 gobject_class
->get_property
= g_dbus_interface_skeleton_get_property
;
183 klass
->g_authorize_method
= g_dbus_interface_skeleton_g_authorize_method_default
;
186 * GDBusInterfaceSkeleton:g-flags:
188 * Flags from the #GDBusInterfaceSkeletonFlags enumeration.
192 g_object_class_install_property (gobject_class
,
194 g_param_spec_flags ("g-flags",
196 "Flags for the interface skeleton",
197 G_TYPE_DBUS_INTERFACE_SKELETON_FLAGS
,
198 G_DBUS_INTERFACE_SKELETON_FLAGS_NONE
,
201 G_PARAM_STATIC_STRINGS
));
204 * GDBusInterfaceSkeleton::g-authorize-method:
205 * @interface: The #GDBusInterfaceSkeleton emitting the signal.
206 * @invocation: A #GDBusMethodInvocation.
208 * Emitted when a method is invoked by a remote caller and used to
209 * determine if the method call is authorized.
211 * Note that this signal is emitted in a thread dedicated to
212 * handling the method call so handlers are allowed to perform
213 * blocking IO. This means that it is appropriate to call
215 * url="http://hal.freedesktop.org/docs/polkit/PolkitAuthority.html#polkit-authority-check-authorization-sync">polkit_authority_check_authorization_sync()</ulink>
217 * 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.
219 * If %FALSE is returned then no further handlers are run and the
220 * signal handler must take a reference to @invocation and finish
221 * handling the call (e.g. return an error via
222 * g_dbus_method_invocation_return_error()).
224 * Otherwise, if %TRUE is returned, signal emission continues. If no
225 * handlers return %FALSE, then the method is dispatched. If
226 * @interface has an enclosing #GDBusObjectSkeleton, then the
227 * #GDBusObjectSkeleton::authorize-method signal handlers run before
228 * the handlers for this signal.
230 * The default class handler just returns %TRUE.
232 * Please note that the common case is optimized: if no signals
233 * handlers are connected and the default class handler isn't
234 * overridden (for both @interface and the enclosing
235 * #GDBusObjectSkeleton, if any) and #GDBusInterfaceSkeleton:g-flags does
237 * %G_DBUS_INTERFACE_SKELETON_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_THREAD
238 * flags set, no dedicated thread is ever used and the call will be
239 * handled in the same thread as the object that @interface belongs
240 * to was exported in.
242 * Returns: %TRUE if the call is authorized, %FALSE otherwise.
246 signals
[G_AUTHORIZE_METHOD_SIGNAL
] =
247 g_signal_new ("g-authorize-method",
248 G_TYPE_DBUS_INTERFACE_SKELETON
,
250 G_STRUCT_OFFSET (GDBusInterfaceSkeletonClass
, g_authorize_method
),
251 _g_signal_accumulator_false_handled
,
256 G_TYPE_DBUS_METHOD_INVOCATION
);
260 g_dbus_interface_skeleton_init (GDBusInterfaceSkeleton
*interface
)
262 interface
->priv
= g_dbus_interface_skeleton_get_instance_private (interface
);
263 g_mutex_init (&interface
->priv
->lock
);
266 /* ---------------------------------------------------------------------------------------------------- */
269 * g_dbus_interface_skeleton_get_flags:
270 * @interface_: A #GDBusInterfaceSkeleton.
272 * Gets the #GDBusInterfaceSkeletonFlags that describes what the behavior
275 * Returns: One or more flags from the #GDBusInterfaceSkeletonFlags enumeration.
279 GDBusInterfaceSkeletonFlags
280 g_dbus_interface_skeleton_get_flags (GDBusInterfaceSkeleton
*interface_
)
282 g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_
), G_DBUS_INTERFACE_SKELETON_FLAGS_NONE
);
283 return interface_
->priv
->flags
;
287 * g_dbus_interface_skeleton_set_flags:
288 * @interface_: A #GDBusInterfaceSkeleton.
289 * @flags: Flags from the #GDBusInterfaceSkeletonFlags enumeration.
291 * Sets flags describing what the behavior of @skeleton should be.
296 g_dbus_interface_skeleton_set_flags (GDBusInterfaceSkeleton
*interface_
,
297 GDBusInterfaceSkeletonFlags flags
)
299 g_return_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_
));
300 g_mutex_lock (&interface_
->priv
->lock
);
301 if (interface_
->priv
->flags
!= flags
)
303 interface_
->priv
->flags
= flags
;
304 g_mutex_unlock (&interface_
->priv
->lock
);
305 g_object_notify (G_OBJECT (interface_
), "g-flags");
309 g_mutex_unlock (&interface_
->priv
->lock
);
314 * g_dbus_interface_skeleton_get_info:
315 * @interface_: A #GDBusInterfaceSkeleton.
317 * Gets D-Bus introspection information for the D-Bus interface
318 * implemented by @interface_.
320 * Returns: (transfer none): A #GDBusInterfaceInfo (never %NULL). Do not free.
325 g_dbus_interface_skeleton_get_info (GDBusInterfaceSkeleton
*interface_
)
327 GDBusInterfaceInfo
*ret
;
328 g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_
), NULL
);
329 ret
= G_DBUS_INTERFACE_SKELETON_GET_CLASS (interface_
)->get_info (interface_
);
330 g_warn_if_fail (ret
!= NULL
);
335 * g_dbus_interface_skeleton_get_vtable: (skip)
336 * @interface_: A #GDBusInterfaceSkeleton.
338 * Gets the interface vtable for the D-Bus interface implemented by
339 * @interface_. The returned function pointers should expect @interface_
340 * itself to be passed as @user_data.
342 * Returns: A #GDBusInterfaceVTable (never %NULL).
346 GDBusInterfaceVTable
*
347 g_dbus_interface_skeleton_get_vtable (GDBusInterfaceSkeleton
*interface_
)
349 GDBusInterfaceVTable
*ret
;
350 g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_
), NULL
);
351 ret
= G_DBUS_INTERFACE_SKELETON_GET_CLASS (interface_
)->get_vtable (interface_
);
352 g_warn_if_fail (ret
!= NULL
);
357 * g_dbus_interface_skeleton_get_properties:
358 * @interface_: A #GDBusInterfaceSkeleton.
360 * Gets all D-Bus properties for @interface_.
362 * Returns: (transfer full): A #GVariant of type <link linkend="G-VARIANT-TYPE-VARDICT:CAPS">'a{sv}'</link>. Free with g_variant_unref().
367 g_dbus_interface_skeleton_get_properties (GDBusInterfaceSkeleton
*interface_
)
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 * <literal>org.freedesktop.DBus.Properties::PropertiesChanged</literal>
385 * signal later (e.g. in an idle handler). This technique is useful
386 * for collapsing multiple property changes into one.
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
);
407 g_dbus_interface_skeleton_get_object (GDBusInterface
*interface_
)
409 GDBusInterfaceSkeleton
*interface
= G_DBUS_INTERFACE_SKELETON (interface_
);
411 g_mutex_lock (&interface
->priv
->lock
);
412 ret
= interface
->priv
->object
;
413 g_mutex_unlock (&interface
->priv
->lock
);
418 g_dbus_interface_skeleton_dup_object (GDBusInterface
*interface_
)
420 GDBusInterfaceSkeleton
*interface
= G_DBUS_INTERFACE_SKELETON (interface_
);
422 g_mutex_lock (&interface
->priv
->lock
);
423 ret
= interface
->priv
->object
;
426 g_mutex_unlock (&interface
->priv
->lock
);
431 g_dbus_interface_skeleton_set_object (GDBusInterface
*interface_
,
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
;
440 g_object_add_weak_pointer (G_OBJECT (interface
->priv
->object
), (gpointer
*) &interface
->priv
->object
);
441 g_mutex_unlock (&interface
->priv
->lock
);
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 /* ---------------------------------------------------------------------------------------------------- */
457 volatile gint ref_count
;
458 GDBusInterfaceSkeleton
*interface
;
459 GDBusInterfaceMethodCallFunc method_call_func
;
460 GDBusMethodInvocation
*invocation
;
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
);
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
),
488 g_dbus_method_invocation_get_user_data (data
->invocation
));
493 dispatch_in_thread_func (GTask
*task
,
494 gpointer source_object
,
496 GCancellable
*cancellable
)
498 DispatchData
*data
= task_data
;
499 GDBusInterfaceSkeletonFlags flags
;
503 g_mutex_lock (&data
->interface
->priv
->lock
);
504 flags
= data
->interface
->priv
->flags
;
505 object
= data
->interface
->priv
->object
;
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 */
514 g_signal_emit_by_name (object
,
522 g_signal_emit (data
->interface
,
523 signals
[G_AUTHORIZE_METHOD_SIGNAL
],
531 gboolean run_in_thread
;
532 run_in_thread
= (flags
& G_DBUS_INTERFACE_SKELETON_FLAGS_HANDLE_METHOD_INVOCATIONS_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
),
543 g_dbus_method_invocation_get_user_data (data
->invocation
));
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
);
561 g_object_unref (object
);
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
;
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
;
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
],
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
)
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
),
616 g_dbus_method_invocation_get_user_data (invocation
));
623 data
= g_slice_new0 (DispatchData
);
624 data
->interface
= interface
;
625 data
->method_call_func
= method_call_func
;
626 data
->invocation
= invocation
;
629 task
= g_task_new (interface
, NULL
, NULL
, NULL
);
630 g_task_set_task_data (task
, data
, (GDestroyNotify
) dispatch_data_unref
);
631 g_task_run_in_thread (task
, dispatch_in_thread_func
);
632 g_object_unref (task
);
636 g_object_unref (object
);
640 skeleton_intercept_handle_method_call (GDBusConnection
*connection
,
642 const gchar
*object_path
,
643 const gchar
*interface_name
,
644 const gchar
*method_name
,
645 GVariant
*parameters
,
646 GDBusMethodInvocation
*invocation
,
649 GDBusInterfaceSkeleton
*interface
= G_DBUS_INTERFACE_SKELETON (user_data
);
650 g_dbus_interface_method_dispatch_helper (interface
,
651 g_dbus_interface_skeleton_get_vtable (interface
)->method_call
,
655 /* ---------------------------------------------------------------------------------------------------- */
657 static ConnectionData
*
658 new_connection (GDBusConnection
*connection
,
659 guint registration_id
)
661 ConnectionData
*data
;
663 data
= g_slice_new0 (ConnectionData
);
664 data
->connection
= g_object_ref (connection
);
665 data
->registration_id
= registration_id
;
671 free_connection (ConnectionData
*data
)
675 g_object_unref (data
->connection
);
676 g_slice_free (ConnectionData
, data
);
681 add_connection_locked (GDBusInterfaceSkeleton
*interface_
,
682 GDBusConnection
*connection
,
685 ConnectionData
*data
;
686 guint registration_id
;
687 gboolean ret
= FALSE
;
689 if (interface_
->priv
->hooked_vtable
== NULL
)
691 /* Hook the vtable since we need to intercept method calls for
692 * ::g-authorize-method and for dispatching in thread vs
695 * We need to wait until subclasses have had time to initialize
696 * properly before building the hooked_vtable, so we create it
697 * once at the last minute.
699 interface_
->priv
->hooked_vtable
= g_memdup (g_dbus_interface_skeleton_get_vtable (interface_
), sizeof (GDBusInterfaceVTable
));
700 interface_
->priv
->hooked_vtable
->method_call
= skeleton_intercept_handle_method_call
;
703 registration_id
= g_dbus_connection_register_object (connection
,
704 interface_
->priv
->object_path
,
705 g_dbus_interface_skeleton_get_info (interface_
),
706 interface_
->priv
->hooked_vtable
,
708 NULL
, /* user_data_free_func */
711 if (registration_id
> 0)
713 data
= new_connection (connection
, registration_id
);
714 interface_
->priv
->connections
= g_slist_append (interface_
->priv
->connections
, data
);
722 remove_connection_locked (GDBusInterfaceSkeleton
*interface_
,
723 GDBusConnection
*connection
)
725 ConnectionData
*data
;
728 /* Get the connection in the list and unregister ... */
729 for (l
= interface_
->priv
->connections
; l
!= NULL
; l
= l
->next
)
732 if (data
->connection
== connection
)
734 g_warn_if_fail (g_dbus_connection_unregister_object (data
->connection
, data
->registration_id
));
735 free_connection (data
);
736 interface_
->priv
->connections
= g_slist_delete_link (interface_
->priv
->connections
, l
);
737 /* we are guaranteed that the connection is only added once, so bail out early */
746 set_object_path_locked (GDBusInterfaceSkeleton
*interface_
,
747 const gchar
*object_path
)
749 if (g_strcmp0 (interface_
->priv
->object_path
, object_path
) != 0)
751 g_free (interface_
->priv
->object_path
);
752 interface_
->priv
->object_path
= g_strdup (object_path
);
756 /* ---------------------------------------------------------------------------------------------------- */
759 * g_dbus_interface_skeleton_get_connection:
760 * @interface_: A #GDBusInterfaceSkeleton.
762 * Gets the first connection that @interface_ is exported on, if any.
764 * Returns: (transfer none): A #GDBusConnection or %NULL if @interface_ is
765 * not exported anywhere. Do not free, the object belongs to @interface_.
770 g_dbus_interface_skeleton_get_connection (GDBusInterfaceSkeleton
*interface_
)
772 ConnectionData
*data
;
773 GDBusConnection
*ret
;
775 g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_
), NULL
);
776 g_mutex_lock (&interface_
->priv
->lock
);
779 if (interface_
->priv
->connections
!= NULL
)
781 data
= interface_
->priv
->connections
->data
;
783 ret
= data
->connection
;
786 g_mutex_unlock (&interface_
->priv
->lock
);
792 * g_dbus_interface_skeleton_get_connections:
793 * @interface_: A #GDBusInterfaceSkeleton.
795 * Gets a list of the connections that @interface_ is exported on.
797 * Returns: (element-type GDBusConnection) (transfer full): A list of
798 * all the connections that @interface_ is exported on. The returned
799 * list should be freed with g_list_free() after each element has
800 * been freed with g_object_unref().
805 g_dbus_interface_skeleton_get_connections (GDBusInterfaceSkeleton
*interface_
)
809 ConnectionData
*data
;
811 g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_
), NULL
);
813 g_mutex_lock (&interface_
->priv
->lock
);
816 for (l
= interface_
->priv
->connections
; l
!= NULL
; l
= l
->next
)
819 connections
= g_list_prepend (connections
,
820 /* Return a reference to each connection */
821 g_object_ref (data
->connection
));
824 g_mutex_unlock (&interface_
->priv
->lock
);
826 return g_list_reverse (connections
);
830 * g_dbus_interface_skeleton_has_connection:
831 * @interface_: A #GDBusInterfaceSkeleton.
832 * @connection: A #GDBusConnection.
834 * Checks if @interface_ is exported on @connection.
836 * Returns: %TRUE if @interface_ is exported on @connection, %FALSE otherwise.
841 g_dbus_interface_skeleton_has_connection (GDBusInterfaceSkeleton
*interface_
,
842 GDBusConnection
*connection
)
845 gboolean ret
= FALSE
;
847 g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_
), FALSE
);
848 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), FALSE
);
850 g_mutex_lock (&interface_
->priv
->lock
);
852 for (l
= interface_
->priv
->connections
; l
!= NULL
; l
= l
->next
)
854 ConnectionData
*data
= l
->data
;
855 if (data
->connection
== connection
)
863 g_mutex_unlock (&interface_
->priv
->lock
);
868 * g_dbus_interface_skeleton_get_object_path:
869 * @interface_: A #GDBusInterfaceSkeleton.
871 * Gets the object path that @interface_ is exported on, if any.
873 * Returns: A string owned by @interface_ or %NULL if @interface_ is not exported
874 * anywhere. Do not free, the string belongs to @interface_.
879 g_dbus_interface_skeleton_get_object_path (GDBusInterfaceSkeleton
*interface_
)
882 g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_
), NULL
);
883 g_mutex_lock (&interface_
->priv
->lock
);
884 ret
= interface_
->priv
->object_path
;
885 g_mutex_unlock (&interface_
->priv
->lock
);
890 * g_dbus_interface_skeleton_export:
891 * @interface_: The D-Bus interface to export.
892 * @connection: A #GDBusConnection to export @interface_ on.
893 * @object_path: The path to export the interface at.
894 * @error: Return location for error or %NULL.
896 * Exports @interface_ at @object_path on @connection.
898 * This can be called multiple times to export the same @interface_
899 * onto multiple connections however the @object_path provided must be
900 * the same for all connections.
902 * Use g_dbus_interface_skeleton_unexport() to unexport the object.
904 * Returns: %TRUE if the interface was exported on @connection, otherwise %FALSE with
910 g_dbus_interface_skeleton_export (GDBusInterfaceSkeleton
*interface_
,
911 GDBusConnection
*connection
,
912 const gchar
*object_path
,
915 gboolean ret
= FALSE
;
917 g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_
), FALSE
);
918 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), FALSE
);
919 g_return_val_if_fail (g_variant_is_object_path (object_path
), FALSE
);
920 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, FALSE
);
922 /* Assert that the object path is the same for multiple connections here */
923 g_return_val_if_fail (interface_
->priv
->object_path
== NULL
||
924 g_strcmp0 (interface_
->priv
->object_path
, object_path
) == 0, FALSE
);
926 g_mutex_lock (&interface_
->priv
->lock
);
928 /* Set the object path */
929 set_object_path_locked (interface_
, object_path
);
931 /* Add the connection */
932 ret
= add_connection_locked (interface_
, connection
, error
);
934 g_mutex_unlock (&interface_
->priv
->lock
);
939 * g_dbus_interface_skeleton_unexport:
940 * @interface_: A #GDBusInterfaceSkeleton.
942 * Stops exporting @interface_ on all connections it is exported on.
944 * To unexport @interface_ from only a single connection, use
945 * g_dbus_interface_skeleton_unexport_from_connection()
950 g_dbus_interface_skeleton_unexport (GDBusInterfaceSkeleton
*interface_
)
952 g_return_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_
));
953 g_return_if_fail (interface_
->priv
->connections
!= NULL
);
955 g_mutex_lock (&interface_
->priv
->lock
);
957 g_assert (interface_
->priv
->object_path
!= NULL
);
958 g_assert (interface_
->priv
->hooked_vtable
!= NULL
);
960 /* Remove all connections */
961 while (interface_
->priv
->connections
!= NULL
)
963 ConnectionData
*data
= interface_
->priv
->connections
->data
;
964 remove_connection_locked (interface_
, data
->connection
);
967 /* Unset the object path since there are no connections left */
968 set_object_path_locked (interface_
, NULL
);
970 g_mutex_unlock (&interface_
->priv
->lock
);
975 * g_dbus_interface_skeleton_unexport_from_connection:
976 * @interface_: A #GDBusInterfaceSkeleton.
977 * @connection: A #GDBusConnection.
979 * Stops exporting @interface_ on @connection.
981 * To stop exporting on all connections the interface is exported on,
982 * use g_dbus_interface_skeleton_unexport().
987 g_dbus_interface_skeleton_unexport_from_connection (GDBusInterfaceSkeleton
*interface_
,
988 GDBusConnection
*connection
)
990 g_return_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_
));
991 g_return_if_fail (G_IS_DBUS_CONNECTION (connection
));
992 g_return_if_fail (interface_
->priv
->connections
!= NULL
);
994 g_mutex_lock (&interface_
->priv
->lock
);
996 g_assert (interface_
->priv
->object_path
!= NULL
);
997 g_assert (interface_
->priv
->hooked_vtable
!= NULL
);
999 remove_connection_locked (interface_
, connection
);
1001 /* Reset the object path if we removed the last connection */
1002 if (interface_
->priv
->connections
== NULL
)
1003 set_object_path_locked (interface_
, NULL
);
1005 g_mutex_unlock (&interface_
->priv
->lock
);
1008 /* ---------------------------------------------------------------------------------------------------- */