glib/tests: Fix non-debug build of slice test
[glib.git] / gio / gdbusobjectskeleton.c
blob063d4e852d60ce609011da87343d086fb0764dc6
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 "gdbusobject.h"
26 #include "gdbusobjectskeleton.h"
27 #include "gdbusinterfaceskeleton.h"
28 #include "gdbusprivate.h"
29 #include "gdbusmethodinvocation.h"
30 #include "gdbusintrospection.h"
31 #include "gdbusinterface.h"
32 #include "gdbusutils.h"
34 #include "glibintl.h"
36 /**
37 * SECTION:gdbusobjectskeleton
38 * @short_description: Service-side D-Bus object
39 * @include: gio/gio.h
41 * A #GDBusObjectSkeleton instance is essentially a group of D-Bus
42 * interfaces. The set of exported interfaces on the object may be
43 * dynamic and change at runtime.
45 * This type is intended to be used with #GDBusObjectManager.
48 struct _GDBusObjectSkeletonPrivate
50 GMutex lock;
51 gchar *object_path;
52 GHashTable *map_name_to_iface;
55 enum
57 PROP_0,
58 PROP_G_OBJECT_PATH
61 enum
63 AUTHORIZE_METHOD_SIGNAL,
64 LAST_SIGNAL,
67 static guint signals[LAST_SIGNAL] = {0};
69 static void dbus_object_interface_init (GDBusObjectIface *iface);
71 G_DEFINE_TYPE_WITH_CODE (GDBusObjectSkeleton, g_dbus_object_skeleton, G_TYPE_OBJECT,
72 G_ADD_PRIVATE (GDBusObjectSkeleton)
73 G_IMPLEMENT_INTERFACE (G_TYPE_DBUS_OBJECT, dbus_object_interface_init))
76 static void
77 g_dbus_object_skeleton_finalize (GObject *_object)
79 GDBusObjectSkeleton *object = G_DBUS_OBJECT_SKELETON (_object);
81 g_free (object->priv->object_path);
82 g_hash_table_unref (object->priv->map_name_to_iface);
84 g_mutex_clear (&object->priv->lock);
86 if (G_OBJECT_CLASS (g_dbus_object_skeleton_parent_class)->finalize != NULL)
87 G_OBJECT_CLASS (g_dbus_object_skeleton_parent_class)->finalize (_object);
90 static void
91 g_dbus_object_skeleton_get_property (GObject *_object,
92 guint prop_id,
93 GValue *value,
94 GParamSpec *pspec)
96 GDBusObjectSkeleton *object = G_DBUS_OBJECT_SKELETON (_object);
98 switch (prop_id)
100 case PROP_G_OBJECT_PATH:
101 g_mutex_lock (&object->priv->lock);
102 g_value_set_string (value, object->priv->object_path);
103 g_mutex_unlock (&object->priv->lock);
104 break;
106 default:
107 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
108 break;
112 static void
113 g_dbus_object_skeleton_set_property (GObject *_object,
114 guint prop_id,
115 const GValue *value,
116 GParamSpec *pspec)
118 GDBusObjectSkeleton *object = G_DBUS_OBJECT_SKELETON (_object);
120 switch (prop_id)
122 case PROP_G_OBJECT_PATH:
123 g_dbus_object_skeleton_set_object_path (object, g_value_get_string (value));
124 break;
126 default:
127 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
128 break;
132 static gboolean
133 g_dbus_object_skeleton_authorize_method_default (GDBusObjectSkeleton *object,
134 GDBusInterfaceSkeleton *interface,
135 GDBusMethodInvocation *invocation)
137 return TRUE;
140 static void
141 g_dbus_object_skeleton_class_init (GDBusObjectSkeletonClass *klass)
143 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
145 gobject_class->finalize = g_dbus_object_skeleton_finalize;
146 gobject_class->set_property = g_dbus_object_skeleton_set_property;
147 gobject_class->get_property = g_dbus_object_skeleton_get_property;
149 klass->authorize_method = g_dbus_object_skeleton_authorize_method_default;
152 * GDBusObjectSkeleton:g-object-path:
154 * The object path where the object is exported.
156 * Since: 2.30
158 g_object_class_install_property (gobject_class,
159 PROP_G_OBJECT_PATH,
160 g_param_spec_string ("g-object-path",
161 "Object Path",
162 "The object path where the object is exported",
163 NULL,
164 G_PARAM_READABLE |
165 G_PARAM_WRITABLE |
166 G_PARAM_CONSTRUCT |
167 G_PARAM_STATIC_STRINGS));
170 * GDBusObjectSkeleton::authorize-method:
171 * @object: The #GDBusObjectSkeleton emitting the signal.
172 * @interface: The #GDBusInterfaceSkeleton that @invocation is for.
173 * @invocation: A #GDBusMethodInvocation.
175 * Emitted when a method is invoked by a remote caller and used to
176 * determine if the method call is authorized.
178 * This signal is like #GDBusInterfaceSkeleton<!-- -->'s
179 * #GDBusInterfaceSkeleton::g-authorize-method signal, except that it is
180 * for the enclosing object.
182 * The default class handler just returns %TRUE.
184 * Returns: %TRUE if the call is authorized, %FALSE otherwise.
186 * Since: 2.30
188 signals[AUTHORIZE_METHOD_SIGNAL] =
189 g_signal_new ("authorize-method",
190 G_TYPE_DBUS_OBJECT_SKELETON,
191 G_SIGNAL_RUN_LAST,
192 G_STRUCT_OFFSET (GDBusObjectSkeletonClass, authorize_method),
193 _g_signal_accumulator_false_handled,
194 NULL,
195 NULL,
196 G_TYPE_BOOLEAN,
198 G_TYPE_DBUS_INTERFACE_SKELETON,
199 G_TYPE_DBUS_METHOD_INVOCATION);
202 static void
203 g_dbus_object_skeleton_init (GDBusObjectSkeleton *object)
205 object->priv = g_dbus_object_skeleton_get_instance_private (object);
206 g_mutex_init (&object->priv->lock);
207 object->priv->map_name_to_iface = g_hash_table_new_full (g_str_hash,
208 g_str_equal,
209 g_free,
210 (GDestroyNotify) g_object_unref);
214 * g_dbus_object_skeleton_new:
215 * @object_path: An object path.
217 * Creates a new #GDBusObjectSkeleton.
219 * Returns: A #GDBusObjectSkeleton. Free with g_object_unref().
221 * Since: 2.30
223 GDBusObjectSkeleton *
224 g_dbus_object_skeleton_new (const gchar *object_path)
226 g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
227 return G_DBUS_OBJECT_SKELETON (g_object_new (G_TYPE_DBUS_OBJECT_SKELETON,
228 "g-object-path", object_path,
229 NULL));
233 * g_dbus_object_skeleton_set_object_path:
234 * @object: A #GDBusObjectSkeleton.
235 * @object_path: A valid D-Bus object path.
237 * Sets the object path for @object.
239 * Since: 2.30
241 void
242 g_dbus_object_skeleton_set_object_path (GDBusObjectSkeleton *object,
243 const gchar *object_path)
245 g_return_if_fail (G_IS_DBUS_OBJECT_SKELETON (object));
246 g_return_if_fail (object_path == NULL || g_variant_is_object_path (object_path));
247 g_mutex_lock (&object->priv->lock);
248 /* TODO: fail if object is currently exported */
249 if (g_strcmp0 (object->priv->object_path, object_path) != 0)
251 g_free (object->priv->object_path);
252 object->priv->object_path = g_strdup (object_path);
253 g_mutex_unlock (&object->priv->lock);
254 g_object_notify (G_OBJECT (object), "g-object-path");
256 else
258 g_mutex_unlock (&object->priv->lock);
262 static const gchar *
263 g_dbus_object_skeleton_get_object_path (GDBusObject *_object)
265 GDBusObjectSkeleton *object = G_DBUS_OBJECT_SKELETON (_object);
266 const gchar *ret;
267 g_mutex_lock (&object->priv->lock);
268 ret = object->priv->object_path;
269 g_mutex_unlock (&object->priv->lock);
270 return ret;
274 * g_dbus_object_skeleton_add_interface:
275 * @object: A #GDBusObjectSkeleton.
276 * @interface_: A #GDBusInterfaceSkeleton.
278 * Adds @interface_ to @object.
280 * If @object already contains a #GDBusInterfaceSkeleton with the same
281 * interface name, it is removed before @interface_ is added.
283 * Note that @object takes its own reference on @interface_ and holds
284 * it until removed.
286 * Since: 2.30
288 void
289 g_dbus_object_skeleton_add_interface (GDBusObjectSkeleton *object,
290 GDBusInterfaceSkeleton *interface_)
292 GDBusInterfaceInfo *info;
293 GDBusInterface *interface_to_remove;
295 g_return_if_fail (G_IS_DBUS_OBJECT_SKELETON (object));
296 g_return_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_));
298 g_mutex_lock (&object->priv->lock);
300 info = g_dbus_interface_skeleton_get_info (interface_);
301 g_object_ref (interface_);
303 interface_to_remove = g_hash_table_lookup (object->priv->map_name_to_iface, info->name);
304 if (interface_to_remove != NULL)
306 g_object_ref (interface_to_remove);
307 g_warn_if_fail (g_hash_table_remove (object->priv->map_name_to_iface, info->name));
309 g_hash_table_insert (object->priv->map_name_to_iface,
310 g_strdup (info->name),
311 g_object_ref (interface_));
312 g_dbus_interface_set_object (G_DBUS_INTERFACE (interface_), G_DBUS_OBJECT (object));
314 g_mutex_unlock (&object->priv->lock);
316 if (interface_to_remove != NULL)
318 g_dbus_interface_set_object (interface_to_remove, NULL);
319 g_signal_emit_by_name (object,
320 "interface-removed",
321 interface_to_remove);
322 g_object_unref (interface_to_remove);
325 g_signal_emit_by_name (object,
326 "interface-added",
327 interface_);
328 g_object_unref (interface_);
332 * g_dbus_object_skeleton_remove_interface:
333 * @object: A #GDBusObjectSkeleton.
334 * @interface_: A #GDBusInterfaceSkeleton.
336 * Removes @interface_ from @object.
338 * Since: 2.30
340 void
341 g_dbus_object_skeleton_remove_interface (GDBusObjectSkeleton *object,
342 GDBusInterfaceSkeleton *interface_)
344 GDBusInterfaceSkeleton *other_interface;
345 GDBusInterfaceInfo *info;
347 g_return_if_fail (G_IS_DBUS_OBJECT_SKELETON (object));
348 g_return_if_fail (G_IS_DBUS_INTERFACE (interface_));
350 g_mutex_lock (&object->priv->lock);
352 info = g_dbus_interface_skeleton_get_info (interface_);
354 other_interface = g_hash_table_lookup (object->priv->map_name_to_iface, info->name);
355 if (other_interface == NULL)
357 g_mutex_unlock (&object->priv->lock);
358 g_warning ("Tried to remove interface with name %s from object "
359 "at path %s but no such interface exists",
360 info->name,
361 object->priv->object_path);
363 else if (other_interface != interface_)
365 g_mutex_unlock (&object->priv->lock);
366 g_warning ("Tried to remove interface %p with name %s from object "
367 "at path %s but the object has the interface %p",
368 interface_,
369 info->name,
370 object->priv->object_path,
371 other_interface);
373 else
375 g_object_ref (interface_);
376 g_warn_if_fail (g_hash_table_remove (object->priv->map_name_to_iface, info->name));
377 g_mutex_unlock (&object->priv->lock);
378 g_dbus_interface_set_object (G_DBUS_INTERFACE (interface_), NULL);
379 g_signal_emit_by_name (object,
380 "interface-removed",
381 interface_);
382 g_object_unref (interface_);
388 * g_dbus_object_skeleton_remove_interface_by_name:
389 * @object: A #GDBusObjectSkeleton.
390 * @interface_name: A D-Bus interface name.
392 * Removes the #GDBusInterface with @interface_name from @object.
394 * If no D-Bus interface of the given interface exists, this function
395 * does nothing.
397 * Since: 2.30
399 void
400 g_dbus_object_skeleton_remove_interface_by_name (GDBusObjectSkeleton *object,
401 const gchar *interface_name)
403 GDBusInterface *interface;
405 g_return_if_fail (G_IS_DBUS_OBJECT_SKELETON (object));
406 g_return_if_fail (g_dbus_is_interface_name (interface_name));
408 g_mutex_lock (&object->priv->lock);
409 interface = g_hash_table_lookup (object->priv->map_name_to_iface, interface_name);
410 if (interface != NULL)
412 g_object_ref (interface);
413 g_warn_if_fail (g_hash_table_remove (object->priv->map_name_to_iface, interface_name));
414 g_mutex_unlock (&object->priv->lock);
415 g_dbus_interface_set_object (interface, NULL);
416 g_signal_emit_by_name (object,
417 "interface-removed",
418 interface);
419 g_object_unref (interface);
421 else
423 g_mutex_unlock (&object->priv->lock);
427 static GDBusInterface *
428 g_dbus_object_skeleton_get_interface (GDBusObject *_object,
429 const gchar *interface_name)
431 GDBusObjectSkeleton *object = G_DBUS_OBJECT_SKELETON (_object);
432 GDBusInterface *ret;
434 g_return_val_if_fail (G_IS_DBUS_OBJECT_SKELETON (object), NULL);
435 g_return_val_if_fail (g_dbus_is_interface_name (interface_name), NULL);
437 g_mutex_lock (&object->priv->lock);
438 ret = g_hash_table_lookup (object->priv->map_name_to_iface, interface_name);
439 if (ret != NULL)
440 g_object_ref (ret);
441 g_mutex_unlock (&object->priv->lock);
442 return ret;
445 static GList *
446 g_dbus_object_skeleton_get_interfaces (GDBusObject *_object)
448 GDBusObjectSkeleton *object = G_DBUS_OBJECT_SKELETON (_object);
449 GList *ret;
451 g_return_val_if_fail (G_IS_DBUS_OBJECT_SKELETON (object), NULL);
453 ret = NULL;
455 g_mutex_lock (&object->priv->lock);
456 ret = g_hash_table_get_values (object->priv->map_name_to_iface);
457 g_list_foreach (ret, (GFunc) g_object_ref, NULL);
458 g_mutex_unlock (&object->priv->lock);
460 return ret;
464 * g_dbus_object_skeleton_flush:
465 * @object: A #GDBusObjectSkeleton.
467 * This method simply calls g_dbus_interface_skeleton_flush() on all
468 * interfaces belonging to @object. See that method for when flushing
469 * is useful.
471 * Since: 2.30
473 void
474 g_dbus_object_skeleton_flush (GDBusObjectSkeleton *object)
476 GList *to_flush, *l;
478 g_mutex_lock (&object->priv->lock);
479 to_flush = g_hash_table_get_values (object->priv->map_name_to_iface);
480 g_list_foreach (to_flush, (GFunc) g_object_ref, NULL);
481 g_mutex_unlock (&object->priv->lock);
483 for (l = to_flush; l != NULL; l = l->next)
484 g_dbus_interface_skeleton_flush (G_DBUS_INTERFACE_SKELETON (l->data));
486 g_list_free_full (to_flush, g_object_unref);
489 static void
490 dbus_object_interface_init (GDBusObjectIface *iface)
492 iface->get_object_path = g_dbus_object_skeleton_get_object_path;
493 iface->get_interfaces = g_dbus_object_skeleton_get_interfaces;
494 iface->get_interface = g_dbus_object_skeleton_get_interface;
497 gboolean
498 _g_dbus_object_skeleton_has_authorize_method_handlers (GDBusObjectSkeleton *object)
500 gboolean has_handlers;
501 gboolean has_default_class_handler;
503 has_handlers = g_signal_has_handler_pending (object,
504 signals[AUTHORIZE_METHOD_SIGNAL],
506 TRUE);
507 has_default_class_handler = (G_DBUS_OBJECT_SKELETON_GET_CLASS (object)->authorize_method ==
508 g_dbus_object_skeleton_authorize_method_default);
510 return has_handlers || !has_default_class_handler;