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 "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"
37 * SECTION:gdbusobjectskeleton
38 * @short_description: Service-side D-Bus object
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
51 GHashTable *map_name_to_iface;
62 AUTHORIZE_METHOD_SIGNAL,
66 static guint signals[LAST_SIGNAL] = {0};
68 static void dbus_object_interface_init (GDBusObjectIface *iface);
70 G_DEFINE_TYPE_WITH_CODE (GDBusObjectSkeleton, g_dbus_object_skeleton, G_TYPE_OBJECT,
71 G_IMPLEMENT_INTERFACE (G_TYPE_DBUS_OBJECT, dbus_object_interface_init));
75 g_dbus_object_skeleton_finalize (GObject *_object)
77 GDBusObjectSkeleton *object = G_DBUS_OBJECT_SKELETON (_object);
79 g_free (object->priv->object_path);
80 g_hash_table_unref (object->priv->map_name_to_iface);
82 if (G_OBJECT_CLASS (g_dbus_object_skeleton_parent_class)->finalize != NULL)
83 G_OBJECT_CLASS (g_dbus_object_skeleton_parent_class)->finalize (_object);
87 g_dbus_object_skeleton_get_property (GObject *_object,
92 GDBusObjectSkeleton *object = G_DBUS_OBJECT_SKELETON (_object);
96 case PROP_OBJECT_PATH:
97 g_value_take_string (value, object->priv->object_path);
101 G_OBJECT_WARN_INVALID_PROPERTY_ID (_object, prop_id, pspec);
107 g_dbus_object_skeleton_set_property (GObject *_object,
112 GDBusObjectSkeleton *object = G_DBUS_OBJECT_SKELETON (_object);
116 case PROP_OBJECT_PATH:
117 g_dbus_object_skeleton_set_object_path (object, g_value_get_string (value));
121 G_OBJECT_WARN_INVALID_PROPERTY_ID (_object, prop_id, pspec);
127 g_dbus_object_skeleton_authorize_method_default (GDBusObjectSkeleton *object,
128 GDBusInterfaceSkeleton *interface,
129 GDBusMethodInvocation *invocation)
135 g_dbus_object_skeleton_class_init (GDBusObjectSkeletonClass *klass)
137 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
139 gobject_class->finalize = g_dbus_object_skeleton_finalize;
140 gobject_class->set_property = g_dbus_object_skeleton_set_property;
141 gobject_class->get_property = g_dbus_object_skeleton_get_property;
143 klass->authorize_method = g_dbus_object_skeleton_authorize_method_default;
146 * GDBusObjectSkeleton:object-path:
148 * The object path where the object is exported.
152 g_object_class_install_property (gobject_class,
154 g_param_spec_string ("object-path",
156 "The object path where the object is exported",
161 G_PARAM_STATIC_STRINGS));
164 * GDBusObjectSkeleton::authorize-method:
165 * @object: The #GDBusObjectSkeleton emitting the signal.
166 * @interface: The #GDBusInterfaceSkeleton that @invocation is for.
167 * @invocation: A #GDBusMethodInvocation.
169 * Emitted when a method is invoked by a remote caller and used to
170 * determine if the method call is authorized.
172 * This signal is like #GDBusInterfaceSkeleton<!-- -->'s
173 * #GDBusInterfaceSkeleton::g-authorize-method signal, except that it is
174 * for the enclosing object.
176 * The default class handler just returns %TRUE.
178 * Returns: %TRUE if the call is authorized, %FALSE otherwise.
182 signals[AUTHORIZE_METHOD_SIGNAL] =
183 g_signal_new ("authorize-method",
184 G_TYPE_DBUS_OBJECT_SKELETON,
186 G_STRUCT_OFFSET (GDBusObjectSkeletonClass, authorize_method),
187 _g_signal_accumulator_false_handled,
192 G_TYPE_DBUS_INTERFACE_SKELETON,
193 G_TYPE_DBUS_METHOD_INVOCATION);
195 g_type_class_add_private (klass, sizeof (GDBusObjectSkeletonPrivate));
199 g_dbus_object_skeleton_init (GDBusObjectSkeleton *object)
201 object->priv = G_TYPE_INSTANCE_GET_PRIVATE (object, G_TYPE_DBUS_OBJECT_SKELETON, GDBusObjectSkeletonPrivate);
202 object->priv->map_name_to_iface = g_hash_table_new_full (g_str_hash,
205 (GDestroyNotify) g_object_unref);
209 * g_dbus_object_skeleton_new:
210 * @object_path: An object path.
212 * Creates a new #GDBusObjectSkeleton.
214 * Returns: A #GDBusObjectSkeleton. Free with g_object_unref().
218 GDBusObjectSkeleton *
219 g_dbus_object_skeleton_new (const gchar *object_path)
221 g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
222 return G_DBUS_OBJECT_SKELETON (g_object_new (G_TYPE_DBUS_OBJECT_SKELETON,
223 "object-path", object_path,
228 * g_dbus_object_skeleton_set_object_path:
229 * @object: A #GDBusObjectSkeleton.
230 * @object_path: A valid D-Bus object path.
232 * Sets the object path for @object.
237 g_dbus_object_skeleton_set_object_path (GDBusObjectSkeleton *object,
238 const gchar *object_path)
240 g_return_if_fail (G_IS_DBUS_OBJECT_SKELETON (object));
241 g_return_if_fail (object_path == NULL || g_variant_is_object_path (object_path));
242 /* TODO: fail if object is currently exported */
243 if (g_strcmp0 (object->priv->object_path, object_path) != 0)
245 g_free (object->priv->object_path);
246 object->priv->object_path = g_strdup (object_path);
247 g_object_notify (G_OBJECT (object), "object-path");
252 g_dbus_object_skeleton_get_object_path (GDBusObject *_object)
254 GDBusObjectSkeleton *object = G_DBUS_OBJECT_SKELETON (_object);
255 return object->priv->object_path;
259 * g_dbus_object_skeleton_add_interface:
260 * @object: A #GDBusObjectSkeleton.
261 * @interface_: A #GDBusInterfaceSkeleton.
263 * Adds @interface_ to @object.
265 * If @object already contains a #GDBusInterfaceSkeleton with the same
266 * interface name, it is removed before @interface_ is added.
268 * Note that @object takes its own reference on @interface_ and holds
274 g_dbus_object_skeleton_add_interface (GDBusObjectSkeleton *object,
275 GDBusInterfaceSkeleton *interface_)
277 GDBusInterfaceInfo *info;
279 g_return_if_fail (G_IS_DBUS_OBJECT_SKELETON (object));
280 g_return_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_));
282 info = g_dbus_interface_skeleton_get_info (interface_);
283 g_object_ref (interface_);
284 g_dbus_object_skeleton_remove_interface_by_name (object, info->name);
285 g_hash_table_insert (object->priv->map_name_to_iface,
286 g_strdup (info->name),
288 g_dbus_interface_set_object (G_DBUS_INTERFACE (interface_), G_DBUS_OBJECT (object));
289 g_signal_emit_by_name (object,
295 * g_dbus_object_skeleton_remove_interface:
296 * @object: A #GDBusObjectSkeleton.
297 * @interface_: A #GDBusInterfaceSkeleton.
299 * Removes @interface_ from @object.
304 g_dbus_object_skeleton_remove_interface (GDBusObjectSkeleton *object,
305 GDBusInterfaceSkeleton *interface_)
307 GDBusInterfaceSkeleton *other_interface;
308 GDBusInterfaceInfo *info;
310 g_return_if_fail (G_IS_DBUS_OBJECT_SKELETON (object));
311 g_return_if_fail (G_IS_DBUS_INTERFACE (interface_));
313 info = g_dbus_interface_skeleton_get_info (interface_);
315 other_interface = g_hash_table_lookup (object->priv->map_name_to_iface, info->name);
316 if (other_interface == NULL)
318 g_warning ("Tried to remove interface with name %s from object "
319 "at path %s but no such interface exists",
321 object->priv->object_path);
323 else if (other_interface != interface_)
325 g_warning ("Tried to remove interface %p with name %s from object "
326 "at path %s but the object has the interface %p",
329 object->priv->object_path,
334 g_object_ref (interface_);
335 g_warn_if_fail (g_hash_table_remove (object->priv->map_name_to_iface, info->name));
336 g_dbus_interface_set_object (G_DBUS_INTERFACE (interface_), NULL);
337 g_signal_emit_by_name (object,
340 g_object_unref (interface_);
346 * g_dbus_object_skeleton_remove_interface_by_name:
347 * @object: A #GDBusObjectSkeleton.
348 * @interface_name: A D-Bus interface name.
350 * Removes the #GDBusInterface with @interface_name from @object.
352 * If no D-Bus interface of the given interface exists, this function
358 g_dbus_object_skeleton_remove_interface_by_name (GDBusObjectSkeleton *object,
359 const gchar *interface_name)
361 GDBusInterface *interface;
363 g_return_if_fail (G_IS_DBUS_OBJECT_SKELETON (object));
364 g_return_if_fail (g_dbus_is_interface_name (interface_name));
366 interface = g_hash_table_lookup (object->priv->map_name_to_iface, interface_name);
367 if (interface != NULL)
369 g_object_ref (interface);
370 g_warn_if_fail (g_hash_table_remove (object->priv->map_name_to_iface, interface_name));
371 g_dbus_interface_set_object (interface, NULL);
372 g_signal_emit_by_name (object,
375 g_object_unref (interface);
379 static GDBusInterface *
380 g_dbus_object_skeleton_get_interface (GDBusObject *_object,
381 const gchar *interface_name)
383 GDBusObjectSkeleton *object = G_DBUS_OBJECT_SKELETON (_object);
386 g_return_val_if_fail (G_IS_DBUS_OBJECT_SKELETON (object), NULL);
387 g_return_val_if_fail (g_dbus_is_interface_name (interface_name), NULL);
389 ret = g_hash_table_lookup (object->priv->map_name_to_iface, interface_name);
396 g_dbus_object_skeleton_get_interfaces (GDBusObject *_object)
398 GDBusObjectSkeleton *object = G_DBUS_OBJECT_SKELETON (_object);
401 GDBusInterface *interface;
403 g_return_val_if_fail (G_IS_DBUS_OBJECT_SKELETON (object), NULL);
407 g_hash_table_iter_init (&iter, object->priv->map_name_to_iface);
408 while (g_hash_table_iter_next (&iter, NULL, (gpointer) &interface))
409 ret = g_list_prepend (ret, g_object_ref (interface));
415 * g_dbus_object_skeleton_flush:
416 * @object: A #GDBusObjectSkeleton.
418 * This method simply calls g_dbus_interface_skeleton_flush() on all
419 * interfaces belonging to @object. See that method for when flushing
425 g_dbus_object_skeleton_flush (GDBusObjectSkeleton *object)
428 GDBusInterfaceSkeleton *interface_skeleton;
430 g_hash_table_iter_init (&iter, object->priv->map_name_to_iface);
431 while (g_hash_table_iter_next (&iter, NULL, (gpointer) &interface_skeleton))
433 g_dbus_interface_skeleton_flush (interface_skeleton);
438 dbus_object_interface_init (GDBusObjectIface *iface)
440 iface->get_object_path = g_dbus_object_skeleton_get_object_path;
441 iface->get_interfaces = g_dbus_object_skeleton_get_interfaces;
442 iface->get_interface = g_dbus_object_skeleton_get_interface;
446 _g_dbus_object_skeleton_has_authorize_method_handlers (GDBusObjectSkeleton *object)
448 gboolean has_handlers;
449 gboolean has_default_class_handler;
451 has_handlers = g_signal_has_handler_pending (object,
452 signals[AUTHORIZE_METHOD_SIGNAL],
455 has_default_class_handler = (G_DBUS_OBJECT_SKELETON_GET_CLASS (object)->authorize_method ==
456 g_dbus_object_skeleton_authorize_method_default);
458 return has_handlers || !has_default_class_handler;