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.
28 #include "droute-pairhash.h"
30 #define CHUNKS_DEFAULT (512)
32 #define oom() g_error ("D-Bus out of memory, this message will fail anyway")
34 #if defined DROUTE_DEBUG
35 #define _DROUTE_DEBUG(format, args...) g_print (format , ## args)
37 #define _DROUTE_DEBUG(format, args...)
42 GPtrArray *registered_paths;
44 gchar *introspect_string;
53 GPtrArray *interfaces;
54 GPtrArray *introspection;
56 GHashTable *properties;
58 DRouteIntrospectChildrenFunction introspect_children_cb;
59 void *introspect_children_data;
61 DRouteGetDatumFunction get_datum;
64 /*---------------------------------------------------------------------------*/
66 typedef struct PropertyPair
68 DRoutePropertyFunction get;
69 DRoutePropertyFunction set;
72 /*---------------------------------------------------------------------------*/
74 static DBusHandlerResult
75 handle_message (DBusConnection *bus, DBusMessage *message, void *user_data);
78 droute_object_does_not_exist_error (DBusMessage *message);
80 /*---------------------------------------------------------------------------*/
83 path_new (DRouteContext *cnx,
87 DRouteIntrospectChildrenFunction introspect_children_cb,
88 void *introspect_children_data,
89 DRouteGetDatumFunction get_datum)
93 new_path = g_new0 (DRoutePath, 1);
95 new_path->path = g_strdup (path);
96 new_path->prefix = prefix;
97 new_path->chunks = g_string_chunk_new (CHUNKS_DEFAULT);
98 new_path->interfaces = g_ptr_array_new ();
99 new_path->introspection = g_ptr_array_new ();
101 new_path->methods = g_hash_table_new_full ((GHashFunc)str_pair_hash,
106 new_path->properties = g_hash_table_new_full ((GHashFunc)str_pair_hash,
111 new_path->introspect_children_cb = introspect_children_cb;
112 new_path->introspect_children_data = introspect_children_data;
113 new_path->user_data = user_data;
114 new_path->get_datum = get_datum;
120 path_free (DRoutePath *path, gpointer user_data)
123 g_string_chunk_free (path->chunks);
124 g_ptr_array_free (path->interfaces, TRUE);
125 g_ptr_array_free (path->introspection, FALSE);
126 g_hash_table_destroy (path->methods);
127 g_hash_table_destroy (path->properties);
131 path_get_datum (DRoutePath *path, const gchar *pathstr)
133 if (path->get_datum != NULL)
134 return (path->get_datum) (pathstr, path->user_data);
136 return path->user_data;
139 /*---------------------------------------------------------------------------*/
146 cnx = g_new0 (DRouteContext, 1);
147 cnx->registered_paths = g_ptr_array_new ();
153 droute_free (DRouteContext *cnx)
155 g_ptr_array_foreach (cnx->registered_paths, (GFunc) path_free, NULL);
159 /*---------------------------------------------------------------------------*/
161 /*---------------------------------------------------------------------------*/
163 static DBusObjectPathVTable droute_vtable =
167 NULL, NULL, NULL, NULL
171 droute_add_one (DRouteContext *cnx,
175 DRoutePath *new_path;
178 new_path = path_new (cnx, path, FALSE, (void *)data, NULL, NULL, NULL);
180 g_ptr_array_add (cnx->registered_paths, new_path);
185 droute_add_many (DRouteContext *cnx,
188 DRouteIntrospectChildrenFunction introspect_children_cb,
189 void *introspect_children_data,
190 const DRouteGetDatumFunction get_datum)
192 DRoutePath *new_path;
194 new_path = path_new (cnx, path, TRUE, (void *) data,
195 introspect_children_cb, introspect_children_data,
198 g_ptr_array_add (cnx->registered_paths, new_path);
202 /*---------------------------------------------------------------------------*/
205 droute_path_add_interface(DRoutePath *path,
207 const char *introspect,
208 const DRouteMethod *methods,
209 const DRouteProperty *properties)
213 g_return_if_fail (name != NULL);
215 itf = g_string_chunk_insert (path->chunks, name);
216 g_ptr_array_add (path->interfaces, itf);
217 g_ptr_array_add (path->introspection, introspect);
219 for (; methods != NULL && methods->name != NULL; methods++)
223 meth = g_string_chunk_insert (path->chunks, methods->name);
224 g_hash_table_insert (path->methods, str_pair_new (itf, meth), methods->func);
227 for (; properties != NULL && properties->name != NULL; properties++)
232 prop = g_string_chunk_insert (path->chunks, properties->name);
233 pair = g_new (PropertyPair, 1);
234 pair->get = properties->get;
235 pair->set = properties->set;
236 g_hash_table_insert (path->properties, str_pair_new (itf, prop), pair);
240 /*---------------------------------------------------------------------------*/
242 /* The data structures don't support an efficient implementation of GetAll
243 * and I don't really care.
246 impl_prop_GetAll (DBusMessage *message,
250 DBusMessageIter iter, iter_dict, iter_dict_entry;
253 GHashTableIter prop_iter;
259 void *datum = path_get_datum (path, pathstr);
261 return droute_object_does_not_exist_error (message);
263 dbus_error_init (&error);
264 if (!dbus_message_get_args
265 (message, &error, DBUS_TYPE_STRING, &iface, DBUS_TYPE_INVALID))
266 return dbus_message_new_error (message, DBUS_ERROR_FAILED, error.message);
268 reply = dbus_message_new_method_return (message);
272 dbus_message_iter_init_append (reply, &iter);
273 if (!dbus_message_iter_open_container
274 (&iter, DBUS_TYPE_ARRAY, "{sv}", &iter_dict))
277 g_hash_table_iter_init (&prop_iter, path->properties);
278 while (g_hash_table_iter_next (&prop_iter, (gpointer*)&key, (gpointer*)&value))
280 if (!g_strcmp0 (key->one, iface))
284 if (!dbus_message_iter_open_container
285 (&iter_dict, DBUS_TYPE_DICT_ENTRY, NULL, &iter_dict_entry))
287 dbus_message_iter_append_basic (&iter_dict_entry, DBUS_TYPE_STRING,
289 (value->get) (&iter_dict_entry, datum);
290 if (!dbus_message_iter_close_container (&iter_dict, &iter_dict_entry))
295 if (!dbus_message_iter_close_container (&iter, &iter_dict))
301 impl_prop_GetSet (DBusMessage *message,
306 DBusMessage *reply = NULL;
310 PropertyPair *prop_funcs = NULL;
314 dbus_error_init (&error);
315 if (!dbus_message_get_args (message,
322 return dbus_message_new_error (message, DBUS_ERROR_FAILED, error.message);
324 _DROUTE_DEBUG ("DRoute (handle prop): %s|%s on %s\n", pair.one, pair.two, pathstr);
326 prop_funcs = (PropertyPair *) g_hash_table_lookup (path->properties, &pair);
328 return dbus_message_new_error (message, DBUS_ERROR_FAILED, "Property unavailable");
330 datum = path_get_datum (path, pathstr);
332 return droute_object_does_not_exist_error (message);
334 if (get && prop_funcs->get)
337 DBusMessageIter iter;
339 _DROUTE_DEBUG ("DRoute (handle prop Get): %s|%s on %s\n", pair.one, pair.two, pathstr);
341 reply = dbus_message_new_method_return (message);
342 dbus_message_iter_init_append (reply, &iter);
343 if (!(prop_funcs->get) (&iter, datum))
345 dbus_message_unref (reply);
346 reply = dbus_message_new_error (message, DBUS_ERROR_FAILED, "Get failed");
349 else if (!get && prop_funcs->set)
351 DBusMessageIter iter;
353 _DROUTE_DEBUG ("DRoute (handle prop Get): %s|%s on %s\n", pair.one, pair.two, pathstr);
355 dbus_message_iter_init (message, &iter);
356 /* Skip the interface and property name */
357 dbus_message_iter_next(&iter);
358 dbus_message_iter_next(&iter);
359 (prop_funcs->set) (&iter, datum);
361 reply = dbus_message_new_method_return (message);
365 reply = dbus_message_new_error (message, DBUS_ERROR_FAILED, "Getter or setter unavailable");
371 static DBusHandlerResult
372 handle_dbus (DBusConnection *bus,
373 DBusMessage *message,
376 const gchar *pathstr)
379 char *id_str = (char *) g_malloc(40);
382 if (strcmp (iface, DBUS_INTERFACE_DBUS) != 0 ||
383 strcmp (member, "Hello") != 0)
384 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
386 /* TODO: Fix this hack (we don't handle wrap-around, for instance) */
387 sprintf (id_str, ":1.%d", id++);
388 reply = dbus_message_new_method_return (message);
389 dbus_message_append_args (reply, DBUS_TYPE_STRING, &id_str, DBUS_TYPE_INVALID);
390 dbus_connection_send (bus, reply, NULL);
391 dbus_connection_flush (bus);
392 dbus_message_unref (reply);
394 return DBUS_HANDLER_RESULT_HANDLED;
397 static DBusHandlerResult
398 handle_properties (DBusConnection *bus,
399 DBusMessage *message,
403 const gchar *pathstr)
406 DBusHandlerResult result = DBUS_HANDLER_RESULT_HANDLED;
408 if (!g_strcmp0(member, "GetAll"))
409 reply = impl_prop_GetAll (message, path, pathstr);
410 else if (!g_strcmp0 (member, "Get"))
411 reply = impl_prop_GetSet (message, path, pathstr, TRUE);
412 else if (!g_strcmp0 (member, "Set"))
413 reply = impl_prop_GetSet (message, path, pathstr, FALSE);
415 result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
419 dbus_connection_send (bus, reply, NULL);
420 dbus_message_unref (reply);
426 /*---------------------------------------------------------------------------*/
428 static const char *introspection_header =
429 "<?xml version=\"1.0\"?>\n";
431 static const char *introspection_node_element =
432 "<node name=\"%s\">\n";
434 static const char *introspection_footer =
437 static DBusHandlerResult
438 handle_introspection (DBusConnection *bus,
439 DBusMessage *message,
443 const gchar *pathstr)
451 _DROUTE_DEBUG ("DRoute (handle introspection): %s\n", pathstr);
453 if (g_strcmp0 (member, "Introspect"))
454 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
456 output = g_string_new(introspection_header);
458 g_string_append_printf(output, introspection_node_element, pathstr);
460 if (!path->get_datum || path_get_datum (path, pathstr))
462 for (i=0; i < path->introspection->len; i++)
464 gchar *introspect = (gchar *) g_ptr_array_index (path->introspection, i);
465 g_string_append (output, introspect);
469 if (path->introspect_children_cb)
471 gchar *children = (*path->introspect_children_cb) (pathstr, path->introspect_children_data);
474 g_string_append (output, children);
479 g_string_append(output, introspection_footer);
480 final = g_string_free(output, FALSE);
482 reply = dbus_message_new_method_return (message);
485 dbus_message_append_args(reply, DBUS_TYPE_STRING, &final,
487 dbus_connection_send (bus, reply, NULL);
489 dbus_message_unref (reply);
491 return DBUS_HANDLER_RESULT_HANDLED;
494 /*---------------------------------------------------------------------------*/
496 static DBusHandlerResult
497 handle_other (DBusConnection *bus,
498 DBusMessage *message,
502 const gchar *pathstr)
504 gint result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
508 DBusMessage *reply = NULL;
515 _DROUTE_DEBUG ("DRoute (handle other): %s|%s on %s\n", member, iface, pathstr);
517 func = (DRouteFunction) g_hash_table_lookup (path->methods, &pair);
520 datum = path_get_datum (path, pathstr);
522 reply = droute_object_does_not_exist_error (message);
524 reply = (func) (bus, message, datum);
528 /* All D-Bus method calls must have a reply.
529 * If one is not provided presume that the call has a void
530 * return and no error has occured.
532 reply = dbus_message_new_method_return (message);
534 dbus_connection_send (bus, reply, NULL);
535 dbus_message_unref (reply);
536 result = DBUS_HANDLER_RESULT_HANDLED;
539 _DROUTE_DEBUG ("DRoute (handle other) (reply): type %d\n",
540 dbus_message_get_type(reply));
544 /*---------------------------------------------------------------------------*/
546 static DBusHandlerResult
547 handle_message (DBusConnection *bus, DBusMessage *message, void *user_data)
549 DRoutePath *path = (DRoutePath *) user_data;
550 const gchar *iface = dbus_message_get_interface (message);
551 const gchar *member = dbus_message_get_member (message);
552 const gint type = dbus_message_get_type (message);
553 const gchar *pathstr = dbus_message_get_path (message);
555 DBusHandlerResult result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
557 _DROUTE_DEBUG ("DRoute (handle message): %s|%s of type %d on %s\n", member, iface, type, pathstr);
559 /* Check for basic reasons not to handle */
560 if (type != DBUS_MESSAGE_TYPE_METHOD_CALL ||
565 if (!strcmp (pathstr, DBUS_PATH_DBUS))
566 result = handle_dbus (bus, message, iface, member, pathstr);
567 else if (!strcmp (iface, "org.freedesktop.DBus.Properties"))
568 result = handle_properties (bus, message, path, iface, member, pathstr);
569 else if (!strcmp (iface, "org.freedesktop.DBus.Introspectable"))
570 result = handle_introspection (bus, message, path, iface, member, pathstr);
572 result = handle_other (bus, message, path, iface, member, pathstr);
574 if (result == DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
575 g_print ("DRoute | Unhandled message: %s|%s of type %d on %s\n", member, iface, type, pathstr);
581 /*---------------------------------------------------------------------------*/
584 droute_object_does_not_exist_error (DBusMessage *message)
589 errmsg= g_strdup_printf (
590 "Method \"%s\" with signature \"%s\" on interface \"%s\" could not be processed as object %s does not exist\n",
591 dbus_message_get_member (message),
592 dbus_message_get_signature (message),
593 dbus_message_get_interface (message),
594 dbus_message_get_path (message));
595 reply = dbus_message_new_error (message,
602 /*---------------------------------------------------------------------------*/
605 droute_not_yet_handled_error (DBusMessage *message)
610 errmsg= g_strdup_printf (
611 "Method \"%s\" with signature \"%s\" on interface \"%s\" doesn't exist\n",
612 dbus_message_get_member (message),
613 dbus_message_get_signature (message),
614 dbus_message_get_interface (message));
615 reply = dbus_message_new_error (message,
616 DBUS_ERROR_UNKNOWN_METHOD,
623 droute_out_of_memory_error (DBusMessage *message)
628 errmsg= g_strdup_printf (
629 "Method \"%s\" with signature \"%s\" on interface \"%s\" could not be processed due to lack of memory\n",
630 dbus_message_get_member (message),
631 dbus_message_get_signature (message),
632 dbus_message_get_interface (message));
633 reply = dbus_message_new_error (message,
634 DBUS_ERROR_NO_MEMORY,
641 droute_invalid_arguments_error (DBusMessage *message)
646 errmsg= g_strdup_printf (
647 "Method \"%s\" with signature \"%s\" on interface \"%s\" was supplied with invalid arguments\n",
648 dbus_message_get_member (message),
649 dbus_message_get_signature (message),
650 dbus_message_get_interface (message));
651 reply = dbus_message_new_error (message,
652 DBUS_ERROR_INVALID_ARGS,
659 droute_path_register (DRoutePath *path, DBusConnection *bus)
662 dbus_connection_register_fallback (bus, path->path, &droute_vtable, path);
664 dbus_connection_register_object_path (bus, path->path,
665 &droute_vtable, path);
669 droute_context_register (DRouteContext *cnx, DBusConnection *bus)
671 g_ptr_array_foreach (cnx->registered_paths, (GFunc) droute_path_register,
676 droute_intercept_dbus (DBusConnection *bus)
678 dbus_connection_register_object_path (bus, DBUS_PATH_DBUS,
679 &droute_vtable, NULL);
681 /*END------------------------------------------------------------------------*/