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...)
43 GPtrArray *registered_paths;
45 gchar *introspect_string;
52 GPtrArray *interfaces;
53 GPtrArray *introspection;
55 GHashTable *properties;
57 DRouteIntrospectChildrenFunction introspect_children_cb;
58 void *introspect_children_data;
60 DRouteGetDatumFunction get_datum;
63 /*---------------------------------------------------------------------------*/
65 typedef struct PropertyPair
67 DRoutePropertyFunction get;
68 DRoutePropertyFunction set;
71 /*---------------------------------------------------------------------------*/
73 static DBusHandlerResult
74 handle_message (DBusConnection *bus, DBusMessage *message, void *user_data);
77 droute_object_does_not_exist_error (DBusMessage *message);
79 /*---------------------------------------------------------------------------*/
82 path_new (DRouteContext *cnx,
84 DRouteIntrospectChildrenFunction introspect_children_cb,
85 void *introspect_children_data,
86 DRouteGetDatumFunction get_datum)
90 new_path = g_new0 (DRoutePath, 1);
92 new_path->chunks = g_string_chunk_new (CHUNKS_DEFAULT);
93 new_path->interfaces = g_ptr_array_new ();
94 new_path->introspection = g_ptr_array_new ();
96 new_path->methods = g_hash_table_new_full ((GHashFunc)str_pair_hash,
101 new_path->properties = g_hash_table_new_full ((GHashFunc)str_pair_hash,
106 new_path->introspect_children_cb = introspect_children_cb;
107 new_path->introspect_children_data = introspect_children_data;
108 new_path->user_data = user_data;
109 new_path->get_datum = get_datum;
115 path_free (DRoutePath *path, gpointer user_data)
117 g_string_chunk_free (path->chunks);
118 g_ptr_array_free (path->interfaces, TRUE);
119 g_ptr_array_free (path->introspection, FALSE);
120 g_hash_table_destroy (path->methods);
121 g_hash_table_destroy (path->properties);
125 path_get_datum (DRoutePath *path, const gchar *pathstr)
127 if (path->get_datum != NULL)
128 return (path->get_datum) (pathstr, path->user_data);
130 return path->user_data;
133 /*---------------------------------------------------------------------------*/
136 droute_new (DBusConnection *bus)
140 cnx = g_new0 (DRouteContext, 1);
142 cnx->registered_paths = g_ptr_array_new ();
148 droute_free (DRouteContext *cnx)
150 g_ptr_array_foreach (cnx->registered_paths, (GFunc) path_free, NULL);
154 /*---------------------------------------------------------------------------*/
157 droute_get_bus (DRouteContext *cnx)
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, NULL, NULL, (void *) data, NULL);
181 registered = dbus_connection_register_object_path (cnx->bus, path, &droute_vtable, new_path);
184 path_free (new_path, NULL);
188 g_ptr_array_add (cnx->registered_paths, new_path);
193 droute_add_many (DRouteContext *cnx,
196 DRouteIntrospectChildrenFunction introspect_children_cb,
197 void *introspect_children_data,
198 const DRouteGetDatumFunction get_datum)
200 DRoutePath *new_path;
202 new_path = path_new (cnx, (void *) data, introspect_children_cb, introspect_children_data, get_datum);
204 if (!dbus_connection_register_fallback (cnx->bus, path, &droute_vtable, new_path))
207 g_ptr_array_add (cnx->registered_paths, new_path);
211 /*---------------------------------------------------------------------------*/
214 droute_path_add_interface(DRoutePath *path,
216 const char *introspect,
217 const DRouteMethod *methods,
218 const DRouteProperty *properties)
222 g_return_if_fail (name != NULL);
224 itf = g_string_chunk_insert (path->chunks, name);
225 g_ptr_array_add (path->interfaces, itf);
226 g_ptr_array_add (path->introspection, introspect);
228 for (; methods != NULL && methods->name != NULL; methods++)
232 meth = g_string_chunk_insert (path->chunks, methods->name);
233 g_hash_table_insert (path->methods, str_pair_new (itf, meth), methods->func);
236 for (; properties != NULL && properties->name != NULL; properties++)
241 prop = g_string_chunk_insert (path->chunks, properties->name);
242 pair = g_new (PropertyPair, 1);
243 pair->get = properties->get;
244 pair->set = properties->set;
245 g_hash_table_insert (path->properties, str_pair_new (itf, prop), pair);
249 /*---------------------------------------------------------------------------*/
251 /* The data structures don't support an efficient implementation of GetAll
252 * and I don't really care.
255 impl_prop_GetAll (DBusMessage *message,
259 DBusMessageIter iter, iter_dict, iter_dict_entry;
262 GHashTableIter prop_iter;
268 void *datum = path_get_datum (path, pathstr);
270 return droute_object_does_not_exist_error (message);
272 dbus_error_init (&error);
273 if (!dbus_message_get_args
274 (message, &error, DBUS_TYPE_STRING, &iface, DBUS_TYPE_INVALID))
275 return dbus_message_new_error (message, DBUS_ERROR_FAILED, error.message);
277 reply = dbus_message_new_method_return (message);
281 dbus_message_iter_init_append (reply, &iter);
282 if (!dbus_message_iter_open_container
283 (&iter, DBUS_TYPE_ARRAY, "{sv}", &iter_dict))
286 g_hash_table_iter_init (&prop_iter, path->properties);
287 while (g_hash_table_iter_next (&prop_iter, (gpointer*)&key, (gpointer*)&value))
289 if (!g_strcmp0 (key->one, iface))
293 if (!dbus_message_iter_open_container
294 (&iter_dict, DBUS_TYPE_DICT_ENTRY, NULL, &iter_dict_entry))
296 dbus_message_iter_append_basic (&iter_dict_entry, DBUS_TYPE_STRING,
298 (value->get) (&iter_dict_entry, datum);
299 if (!dbus_message_iter_close_container (&iter_dict, &iter_dict_entry))
304 if (!dbus_message_iter_close_container (&iter, &iter_dict))
310 impl_prop_GetSet (DBusMessage *message,
315 DBusMessage *reply = NULL;
319 PropertyPair *prop_funcs = NULL;
323 dbus_error_init (&error);
324 if (!dbus_message_get_args (message,
331 return dbus_message_new_error (message, DBUS_ERROR_FAILED, error.message);
333 _DROUTE_DEBUG ("DRoute (handle prop): %s|%s on %s\n", pair.one, pair.two, pathstr);
335 prop_funcs = (PropertyPair *) g_hash_table_lookup (path->properties, &pair);
337 return dbus_message_new_error (message, DBUS_ERROR_FAILED, "Property unavailable");
339 datum = path_get_datum (path, pathstr);
341 return droute_object_does_not_exist_error (message);
343 if (get && prop_funcs->get)
346 DBusMessageIter iter;
348 _DROUTE_DEBUG ("DRoute (handle prop Get): %s|%s on %s\n", pair.one, pair.two, pathstr);
350 reply = dbus_message_new_method_return (message);
351 dbus_message_iter_init_append (reply, &iter);
352 if (!(prop_funcs->get) (&iter, datum))
354 dbus_message_unref (reply);
355 reply = dbus_message_new_error (message, DBUS_ERROR_FAILED, "Get failed");
358 else if (!get && prop_funcs->set)
360 DBusMessageIter iter;
362 _DROUTE_DEBUG ("DRoute (handle prop Get): %s|%s on %s\n", pair.one, pair.two, pathstr);
364 dbus_message_iter_init (message, &iter);
365 /* Skip the interface and property name */
366 dbus_message_iter_next(&iter);
367 dbus_message_iter_next(&iter);
368 (prop_funcs->set) (&iter, datum);
370 reply = dbus_message_new_method_return (message);
374 reply = dbus_message_new_error (message, DBUS_ERROR_FAILED, "Getter or setter unavailable");
380 static DBusHandlerResult
381 handle_properties (DBusConnection *bus,
382 DBusMessage *message,
386 const gchar *pathstr)
389 DBusHandlerResult result = DBUS_HANDLER_RESULT_HANDLED;
391 if (!g_strcmp0(member, "GetAll"))
392 reply = impl_prop_GetAll (message, path, pathstr);
393 else if (!g_strcmp0 (member, "Get"))
394 reply = impl_prop_GetSet (message, path, pathstr, TRUE);
395 else if (!g_strcmp0 (member, "Set"))
396 reply = impl_prop_GetSet (message, path, pathstr, FALSE);
398 result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
402 dbus_connection_send (bus, reply, NULL);
403 dbus_message_unref (reply);
409 /*---------------------------------------------------------------------------*/
411 static const char *introspection_header =
412 "<?xml version=\"1.0\"?>\n";
414 static const char *introspection_node_element =
415 "<node name=\"%s\">\n";
417 static const char *introspection_footer =
420 static DBusHandlerResult
421 handle_introspection (DBusConnection *bus,
422 DBusMessage *message,
426 const gchar *pathstr)
434 _DROUTE_DEBUG ("DRoute (handle introspection): %s\n", pathstr);
436 if (g_strcmp0 (member, "Introspect"))
437 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
439 output = g_string_new(introspection_header);
441 g_string_append_printf(output, introspection_node_element, pathstr);
443 if (!path->get_datum || path_get_datum (path, pathstr))
445 for (i=0; i < path->introspection->len; i++)
447 gchar *introspect = (gchar *) g_ptr_array_index (path->introspection, i);
448 g_string_append (output, introspect);
452 if (path->introspect_children_cb)
454 gchar *children = (*path->introspect_children_cb) (pathstr, path->introspect_children_data);
457 g_string_append (output, children);
462 g_string_append(output, introspection_footer);
463 final = g_string_free(output, FALSE);
465 reply = dbus_message_new_method_return (message);
468 dbus_message_append_args(reply, DBUS_TYPE_STRING, &final,
470 dbus_connection_send (bus, reply, NULL);
472 dbus_message_unref (reply);
474 return DBUS_HANDLER_RESULT_HANDLED;
477 /*---------------------------------------------------------------------------*/
479 static DBusHandlerResult
480 handle_other (DBusConnection *bus,
481 DBusMessage *message,
485 const gchar *pathstr)
487 gint result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
491 DBusMessage *reply = NULL;
498 _DROUTE_DEBUG ("DRoute (handle other): %s|%s on %s\n", member, iface, pathstr);
500 func = (DRouteFunction) g_hash_table_lookup (path->methods, &pair);
503 datum = path_get_datum (path, pathstr);
505 reply = droute_object_does_not_exist_error (message);
507 reply = (func) (bus, message, datum);
511 /* All D-Bus method calls must have a reply.
512 * If one is not provided presume that the call has a void
513 * return and no error has occured.
515 reply = dbus_message_new_method_return (message);
517 dbus_connection_send (bus, reply, NULL);
518 dbus_message_unref (reply);
519 result = DBUS_HANDLER_RESULT_HANDLED;
522 _DROUTE_DEBUG ("DRoute (handle other) (reply): type %d\n",
523 dbus_message_get_type(reply));
527 /*---------------------------------------------------------------------------*/
529 static DBusHandlerResult
530 handle_message (DBusConnection *bus, DBusMessage *message, void *user_data)
532 DRoutePath *path = (DRoutePath *) user_data;
533 const gchar *iface = dbus_message_get_interface (message);
534 const gchar *member = dbus_message_get_member (message);
535 const gint type = dbus_message_get_type (message);
536 const gchar *pathstr = dbus_message_get_path (message);
538 DBusHandlerResult result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
540 _DROUTE_DEBUG ("DRoute (handle message): %s|%s of type %d on %s\n", member, iface, type, pathstr);
542 /* Check for basic reasons not to handle */
543 if (type != DBUS_MESSAGE_TYPE_METHOD_CALL ||
548 if (!strcmp (iface, "org.freedesktop.DBus.Properties"))
549 result = handle_properties (bus, message, path, iface, member, pathstr);
550 else if (!strcmp (iface, "org.freedesktop.DBus.Introspectable"))
551 result = handle_introspection (bus, message, path, iface, member, pathstr);
553 result = handle_other (bus, message, path, iface, member, pathstr);
555 if (result == DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
556 g_print ("DRoute | Unhandled message: %s|%s of type %d on %s\n", member, iface, type, pathstr);
562 /*---------------------------------------------------------------------------*/
565 droute_object_does_not_exist_error (DBusMessage *message)
570 errmsg= g_strdup_printf (
571 "Method \"%s\" with signature \"%s\" on interface \"%s\" could not be processed as object %s does not exist\n",
572 dbus_message_get_member (message),
573 dbus_message_get_signature (message),
574 dbus_message_get_interface (message),
575 dbus_message_get_path (message));
576 reply = dbus_message_new_error (message,
583 /*---------------------------------------------------------------------------*/
586 droute_not_yet_handled_error (DBusMessage *message)
591 errmsg= g_strdup_printf (
592 "Method \"%s\" with signature \"%s\" on interface \"%s\" doesn't exist\n",
593 dbus_message_get_member (message),
594 dbus_message_get_signature (message),
595 dbus_message_get_interface (message));
596 reply = dbus_message_new_error (message,
597 DBUS_ERROR_UNKNOWN_METHOD,
604 droute_out_of_memory_error (DBusMessage *message)
609 errmsg= g_strdup_printf (
610 "Method \"%s\" with signature \"%s\" on interface \"%s\" could not be processed due to lack of memory\n",
611 dbus_message_get_member (message),
612 dbus_message_get_signature (message),
613 dbus_message_get_interface (message));
614 reply = dbus_message_new_error (message,
615 DBUS_ERROR_NO_MEMORY,
622 droute_invalid_arguments_error (DBusMessage *message)
627 errmsg= g_strdup_printf (
628 "Method \"%s\" with signature \"%s\" on interface \"%s\" was supplied with invalid arguments\n",
629 dbus_message_get_member (message),
630 dbus_message_get_signature (message),
631 dbus_message_get_interface (message));
632 reply = dbus_message_new_error (message,
633 DBUS_ERROR_INVALID_ARGS,
639 /*END------------------------------------------------------------------------*/