2 * AT-SPI - Assistive Technology Service Provider Interface
3 * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
5 * Copyright 2008 Novell, Inc.
6 * Copyright 2008 Codethink Ltd.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
29 #include "droute-pairhash.h"
31 #define CHUNKS_DEFAULT (512)
33 #define oom() g_error ("D-Bus out of memory, this message will fail anyway")
35 #if defined DROUTE_DEBUG
36 #define _DROUTE_DEBUG(format, args...) g_print (format , ## args)
38 #define _DROUTE_DEBUG(format, args...)
43 GPtrArray *registered_paths;
45 gchar *introspect_string;
54 GPtrArray *interfaces;
55 GPtrArray *introspection;
57 GHashTable *properties;
59 DRouteIntrospectChildrenFunction introspect_children_cb;
60 void *introspect_children_data;
62 DRouteGetDatumFunction get_datum;
65 /*---------------------------------------------------------------------------*/
67 typedef struct PropertyPair
69 DRoutePropertyFunction get;
70 DRoutePropertyFunction set;
73 /*---------------------------------------------------------------------------*/
75 static DBusHandlerResult
76 handle_message (DBusConnection *bus, DBusMessage *message, void *user_data);
79 droute_object_does_not_exist_error (DBusMessage *message);
81 /*---------------------------------------------------------------------------*/
84 path_new (DRouteContext *cnx,
88 DRouteIntrospectChildrenFunction introspect_children_cb,
89 void *introspect_children_data,
90 DRouteGetDatumFunction get_datum)
94 new_path = g_new0 (DRoutePath, 1);
96 new_path->path = g_strdup (path);
97 new_path->prefix = prefix;
98 new_path->chunks = g_string_chunk_new (CHUNKS_DEFAULT);
99 new_path->interfaces = g_ptr_array_new ();
100 new_path->introspection = g_ptr_array_new ();
102 new_path->methods = g_hash_table_new_full ((GHashFunc)str_pair_hash,
107 new_path->properties = g_hash_table_new_full ((GHashFunc)str_pair_hash,
112 new_path->introspect_children_cb = introspect_children_cb;
113 new_path->introspect_children_data = introspect_children_data;
114 new_path->user_data = user_data;
115 new_path->get_datum = get_datum;
121 path_free (DRoutePath *path, gpointer user_data)
124 g_string_chunk_free (path->chunks);
125 g_ptr_array_free (path->interfaces, TRUE);
126 g_ptr_array_free (path->introspection, FALSE);
127 g_hash_table_destroy (path->methods);
128 g_hash_table_destroy (path->properties);
132 path_get_datum (DRoutePath *path, const gchar *pathstr)
134 if (path->get_datum != NULL)
135 return (path->get_datum) (pathstr, path->user_data);
137 return path->user_data;
140 /*---------------------------------------------------------------------------*/
147 cnx = g_new0 (DRouteContext, 1);
148 cnx->registered_paths = g_ptr_array_new ();
154 droute_free (DRouteContext *cnx)
156 g_ptr_array_foreach (cnx->registered_paths, (GFunc) path_free, NULL);
160 /*---------------------------------------------------------------------------*/
162 /*---------------------------------------------------------------------------*/
164 static DBusObjectPathVTable droute_vtable =
168 NULL, NULL, NULL, NULL
172 droute_add_one (DRouteContext *cnx,
176 DRoutePath *new_path;
179 new_path = path_new (cnx, path, FALSE, (void *)data, NULL, NULL, NULL);
181 g_ptr_array_add (cnx->registered_paths, new_path);
186 droute_add_many (DRouteContext *cnx,
189 DRouteIntrospectChildrenFunction introspect_children_cb,
190 void *introspect_children_data,
191 const DRouteGetDatumFunction get_datum)
193 DRoutePath *new_path;
195 new_path = path_new (cnx, path, TRUE, (void *) data,
196 introspect_children_cb, introspect_children_data,
199 g_ptr_array_add (cnx->registered_paths, new_path);
203 /*---------------------------------------------------------------------------*/
206 droute_path_add_interface(DRoutePath *path,
208 const char *introspect,
209 const DRouteMethod *methods,
210 const DRouteProperty *properties)
214 g_return_if_fail (name != NULL);
216 itf = g_string_chunk_insert (path->chunks, name);
217 g_ptr_array_add (path->interfaces, itf);
218 g_ptr_array_add (path->introspection, introspect);
220 for (; methods != NULL && methods->name != NULL; methods++)
224 meth = g_string_chunk_insert (path->chunks, methods->name);
225 g_hash_table_insert (path->methods, str_pair_new (itf, meth), methods->func);
228 for (; properties != NULL && properties->name != NULL; properties++)
233 prop = g_string_chunk_insert (path->chunks, properties->name);
234 pair = g_new (PropertyPair, 1);
235 pair->get = properties->get;
236 pair->set = properties->set;
237 g_hash_table_insert (path->properties, str_pair_new (itf, prop), pair);
241 /*---------------------------------------------------------------------------*/
243 /* The data structures don't support an efficient implementation of GetAll
244 * and I don't really care.
247 impl_prop_GetAll (DBusMessage *message,
251 DBusMessageIter iter, iter_dict, iter_dict_entry;
254 GHashTableIter prop_iter;
260 void *datum = path_get_datum (path, pathstr);
262 return droute_object_does_not_exist_error (message);
264 dbus_error_init (&error);
265 if (!dbus_message_get_args
266 (message, &error, DBUS_TYPE_STRING, &iface, DBUS_TYPE_INVALID))
267 return dbus_message_new_error (message, DBUS_ERROR_FAILED, error.message);
269 reply = dbus_message_new_method_return (message);
273 dbus_message_iter_init_append (reply, &iter);
274 if (!dbus_message_iter_open_container
275 (&iter, DBUS_TYPE_ARRAY, "{sv}", &iter_dict))
278 g_hash_table_iter_init (&prop_iter, path->properties);
279 while (g_hash_table_iter_next (&prop_iter, (gpointer*)&key, (gpointer*)&value))
281 if (!g_strcmp0 (key->one, iface))
285 if (!dbus_message_iter_open_container
286 (&iter_dict, DBUS_TYPE_DICT_ENTRY, NULL, &iter_dict_entry))
288 dbus_message_iter_append_basic (&iter_dict_entry, DBUS_TYPE_STRING,
290 (value->get) (&iter_dict_entry, datum);
291 if (!dbus_message_iter_close_container (&iter_dict, &iter_dict_entry))
296 if (!dbus_message_iter_close_container (&iter, &iter_dict))
302 impl_prop_GetSet (DBusMessage *message,
307 DBusMessage *reply = NULL;
311 PropertyPair *prop_funcs = NULL;
315 dbus_error_init (&error);
316 if (!dbus_message_get_args (message,
323 return dbus_message_new_error (message, DBUS_ERROR_FAILED, error.message);
325 _DROUTE_DEBUG ("DRoute (handle prop): %s|%s on %s\n", pair.one, pair.two, pathstr);
327 prop_funcs = (PropertyPair *) g_hash_table_lookup (path->properties, &pair);
329 return dbus_message_new_error (message, DBUS_ERROR_FAILED, "Property unavailable");
331 datum = path_get_datum (path, pathstr);
333 return droute_object_does_not_exist_error (message);
335 if (get && prop_funcs->get)
338 DBusMessageIter iter;
340 _DROUTE_DEBUG ("DRoute (handle prop Get): %s|%s on %s\n", pair.one, pair.two, pathstr);
342 reply = dbus_message_new_method_return (message);
343 dbus_message_iter_init_append (reply, &iter);
344 if (!(prop_funcs->get) (&iter, datum))
346 dbus_message_unref (reply);
347 reply = dbus_message_new_error (message, DBUS_ERROR_FAILED, "Get failed");
350 else if (!get && prop_funcs->set)
352 DBusMessageIter iter;
354 _DROUTE_DEBUG ("DRoute (handle prop Get): %s|%s on %s\n", pair.one, pair.two, pathstr);
356 dbus_message_iter_init (message, &iter);
357 /* Skip the interface and property name */
358 dbus_message_iter_next(&iter);
359 dbus_message_iter_next(&iter);
360 (prop_funcs->set) (&iter, datum);
362 reply = dbus_message_new_method_return (message);
366 reply = dbus_message_new_error (message, DBUS_ERROR_FAILED, "Getter or setter unavailable");
372 static DBusHandlerResult
373 handle_dbus (DBusConnection *bus,
374 DBusMessage *message,
377 const gchar *pathstr)
380 char *id_str = (char *) g_malloc(40);
383 if (strcmp (iface, DBUS_INTERFACE_DBUS) != 0 ||
384 strcmp (member, "Hello") != 0)
387 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
390 /* TODO: Fix this hack (we don't handle wrap-around, for instance) */
391 sprintf (id_str, ":1.%d", id++);
392 reply = dbus_message_new_method_return (message);
393 dbus_message_append_args (reply, DBUS_TYPE_STRING, &id_str, DBUS_TYPE_INVALID);
394 dbus_connection_send (bus, reply, NULL);
395 dbus_connection_flush (bus);
396 dbus_message_unref (reply);
398 return DBUS_HANDLER_RESULT_HANDLED;
401 static DBusHandlerResult
402 handle_properties (DBusConnection *bus,
403 DBusMessage *message,
407 const gchar *pathstr)
409 DBusMessage *reply = NULL;
410 DBusHandlerResult result = DBUS_HANDLER_RESULT_HANDLED;
412 if (!g_strcmp0(member, "GetAll"))
413 reply = impl_prop_GetAll (message, path, pathstr);
414 else if (!g_strcmp0 (member, "Get"))
415 reply = impl_prop_GetSet (message, path, pathstr, TRUE);
416 else if (!g_strcmp0 (member, "Set"))
417 reply = impl_prop_GetSet (message, path, pathstr, FALSE);
419 result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
423 dbus_connection_send (bus, reply, NULL);
424 dbus_message_unref (reply);
430 /*---------------------------------------------------------------------------*/
432 static const char *introspection_header =
433 "<?xml version=\"1.0\"?>\n";
435 static const char *introspection_node_element =
436 "<node name=\"%s\">\n";
438 static const char *introspection_footer =
441 static DBusHandlerResult
442 handle_introspection (DBusConnection *bus,
443 DBusMessage *message,
447 const gchar *pathstr)
455 _DROUTE_DEBUG ("DRoute (handle introspection): %s\n", pathstr);
457 if (g_strcmp0 (member, "Introspect"))
458 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
460 output = g_string_new(introspection_header);
462 g_string_append_printf(output, introspection_node_element, pathstr);
464 if (!path->get_datum || path_get_datum (path, pathstr))
466 for (i=0; i < path->introspection->len; i++)
468 gchar *introspect = (gchar *) g_ptr_array_index (path->introspection, i);
469 g_string_append (output, introspect);
473 if (path->introspect_children_cb)
475 gchar *children = (*path->introspect_children_cb) (pathstr, path->introspect_children_data);
478 g_string_append (output, children);
483 g_string_append(output, introspection_footer);
484 final = g_string_free(output, FALSE);
486 reply = dbus_message_new_method_return (message);
489 dbus_message_append_args(reply, DBUS_TYPE_STRING, &final,
491 dbus_connection_send (bus, reply, NULL);
493 dbus_message_unref (reply);
495 return DBUS_HANDLER_RESULT_HANDLED;
498 /*---------------------------------------------------------------------------*/
500 static DBusHandlerResult
501 handle_other (DBusConnection *bus,
502 DBusMessage *message,
506 const gchar *pathstr)
508 gint result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
512 DBusMessage *reply = NULL;
519 _DROUTE_DEBUG ("DRoute (handle other): %s|%s on %s\n", member, iface, pathstr);
521 func = (DRouteFunction) g_hash_table_lookup (path->methods, &pair);
524 datum = path_get_datum (path, pathstr);
526 reply = droute_object_does_not_exist_error (message);
528 reply = (func) (bus, message, datum);
532 /* All D-Bus method calls must have a reply.
533 * If one is not provided presume that the call has a void
534 * return and no error has occured.
536 reply = dbus_message_new_method_return (message);
538 dbus_connection_send (bus, reply, NULL);
539 dbus_message_unref (reply);
540 result = DBUS_HANDLER_RESULT_HANDLED;
543 _DROUTE_DEBUG ("DRoute (handle other) (reply): type %d\n",
544 dbus_message_get_type(reply));
548 /*---------------------------------------------------------------------------*/
550 static DBusHandlerResult
551 handle_message (DBusConnection *bus, DBusMessage *message, void *user_data)
553 DRoutePath *path = (DRoutePath *) user_data;
554 const gchar *iface = dbus_message_get_interface (message);
555 const gchar *member = dbus_message_get_member (message);
556 const gint type = dbus_message_get_type (message);
557 const gchar *pathstr = dbus_message_get_path (message);
559 DBusHandlerResult result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
561 _DROUTE_DEBUG ("DRoute (handle message): %s|%s of type %d on %s\n", member, iface, type, pathstr);
563 /* Check for basic reasons not to handle */
564 if (type != DBUS_MESSAGE_TYPE_METHOD_CALL ||
569 if (!strcmp (pathstr, DBUS_PATH_DBUS))
570 result = handle_dbus (bus, message, iface, member, pathstr);
571 else if (!strcmp (iface, "org.freedesktop.DBus.Properties"))
572 result = handle_properties (bus, message, path, iface, member, pathstr);
573 else if (!strcmp (iface, "org.freedesktop.DBus.Introspectable"))
574 result = handle_introspection (bus, message, path, iface, member, pathstr);
576 result = handle_other (bus, message, path, iface, member, pathstr);
578 if (result == DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
579 g_print ("DRoute | Unhandled message: %s|%s of type %d on %s\n", member, iface, type, pathstr);
585 /*---------------------------------------------------------------------------*/
588 droute_object_does_not_exist_error (DBusMessage *message)
593 errmsg= g_strdup_printf (
594 "Method \"%s\" with signature \"%s\" on interface \"%s\" could not be processed as object %s does not exist\n",
595 dbus_message_get_member (message),
596 dbus_message_get_signature (message),
597 dbus_message_get_interface (message),
598 dbus_message_get_path (message));
599 reply = dbus_message_new_error (message,
606 /*---------------------------------------------------------------------------*/
609 droute_not_yet_handled_error (DBusMessage *message)
614 errmsg= g_strdup_printf (
615 "Method \"%s\" with signature \"%s\" on interface \"%s\" doesn't exist\n",
616 dbus_message_get_member (message),
617 dbus_message_get_signature (message),
618 dbus_message_get_interface (message));
619 reply = dbus_message_new_error (message,
620 DBUS_ERROR_UNKNOWN_METHOD,
627 droute_out_of_memory_error (DBusMessage *message)
632 errmsg= g_strdup_printf (
633 "Method \"%s\" with signature \"%s\" on interface \"%s\" could not be processed due to lack of memory\n",
634 dbus_message_get_member (message),
635 dbus_message_get_signature (message),
636 dbus_message_get_interface (message));
637 reply = dbus_message_new_error (message,
638 DBUS_ERROR_NO_MEMORY,
645 droute_invalid_arguments_error (DBusMessage *message)
650 errmsg= g_strdup_printf (
651 "Method \"%s\" with signature \"%s\" on interface \"%s\" was supplied with invalid arguments\n",
652 dbus_message_get_member (message),
653 dbus_message_get_signature (message),
654 dbus_message_get_interface (message));
655 reply = dbus_message_new_error (message,
656 DBUS_ERROR_INVALID_ARGS,
663 droute_path_register (DRoutePath *path, DBusConnection *bus)
666 dbus_connection_register_fallback (bus, path->path, &droute_vtable, path);
668 dbus_connection_register_object_path (bus, path->path,
669 &droute_vtable, path);
673 droute_context_register (DRouteContext *cnx, DBusConnection *bus)
675 g_ptr_array_foreach (cnx->registered_paths, (GFunc) droute_path_register,
680 droute_intercept_dbus (DBusConnection *bus)
682 dbus_connection_register_object_path (bus, DBUS_PATH_DBUS,
683 &droute_vtable, NULL);
685 /*END------------------------------------------------------------------------*/