Modify the droute tests to use the new introspection scheme.
[platform/core/uifw/at-spi2-atk.git] / droute / droute.c
1 /*
2  * AT-SPI - Assistive Technology Service Provider Interface
3  * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
4  *
5  * Copyright 2008 Novell, Inc.
6  * Copyright 2008 Codethink Ltd.
7  *
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.
12  *
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.
17  *
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.
22  */
23
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "droute.h"
28 #include "droute-pairhash.h"
29
30 #define CHUNKS_DEFAULT (512)
31
32 #define oom() g_error ("D-Bus out of memory, this message will fail anyway")
33
34 #if defined DROUTE_DEBUG
35     #define _DROUTE_DEBUG(format, args...) g_print (format , ## args)
36 #else
37     #define _DROUTE_DEBUG(format, args...)
38 #endif
39
40 struct _DRouteContext
41 {
42     DBusConnection       *bus;
43     GPtrArray            *registered_paths;
44
45     gchar                *introspect_string;
46 };
47
48 struct _DRoutePath
49 {
50     DRouteContext        *cnx;
51     GStringChunk         *chunks;
52     GPtrArray            *interfaces;
53     GPtrArray            *introspection;
54     GHashTable           *methods;
55     GHashTable           *properties;
56
57     void                   *user_data;
58     DRouteGetDatumFunction  get_datum;
59 };
60
61 /*---------------------------------------------------------------------------*/
62
63 typedef struct PropertyPair
64 {
65     DRoutePropertyFunction get;
66     DRoutePropertyFunction set;
67 } PropertyPair;
68
69 /*---------------------------------------------------------------------------*/
70
71 static DBusHandlerResult
72 handle_message (DBusConnection *bus, DBusMessage *message, void *user_data);
73
74 static DBusMessage *
75 droute_object_does_not_exist_error (DBusMessage *message);
76
77 /*---------------------------------------------------------------------------*/
78
79 static DRoutePath *
80 path_new (DRouteContext *cnx,
81           void    *user_data,
82           DRouteGetDatumFunction get_datum)
83 {
84     DRoutePath *new_path;
85
86     new_path = g_new0 (DRoutePath, 1);
87     new_path->cnx = cnx;
88     new_path->chunks = g_string_chunk_new (CHUNKS_DEFAULT);
89     new_path->interfaces = g_ptr_array_new ();
90     new_path->introspection = g_ptr_array_new ();
91
92     new_path->methods = g_hash_table_new_full ((GHashFunc)str_pair_hash,
93                                                str_pair_equal,
94                                                g_free,
95                                                NULL);
96
97     new_path->properties = g_hash_table_new_full ((GHashFunc)str_pair_hash,
98                                                   str_pair_equal,
99                                                   g_free,
100                                                   NULL);
101
102     new_path->user_data = user_data;
103     new_path->get_datum = get_datum;
104
105     return new_path;
106 }
107
108 static void
109 path_free (DRoutePath *path, gpointer user_data)
110 {
111     g_string_chunk_free  (path->chunks);
112     g_ptr_array_free     (path->interfaces, TRUE);
113     g_ptr_array_free     (path->introspection, FALSE);
114     g_hash_table_destroy (path->methods);
115     g_hash_table_destroy (path->properties);
116 }
117
118 static void *
119 path_get_datum (DRoutePath *path, const gchar *pathstr)
120 {
121     if (path->get_datum != NULL)
122         return (path->get_datum) (pathstr, path->user_data);
123     else
124         return path->user_data;
125 }
126
127 /*---------------------------------------------------------------------------*/
128
129 DRouteContext *
130 droute_new (DBusConnection *bus)
131 {
132     DRouteContext *cnx;
133
134     cnx = g_new0 (DRouteContext, 1);
135     cnx->bus = bus;
136     cnx->registered_paths = g_ptr_array_new ();
137
138     return cnx;
139 }
140
141 void
142 droute_free (DRouteContext *cnx)
143 {
144     g_ptr_array_foreach (cnx->registered_paths, (GFunc) path_free, NULL);
145     g_free (cnx);
146 }
147
148 /*---------------------------------------------------------------------------*/
149
150 DBusConnection *
151 droute_get_bus (DRouteContext *cnx)
152 {
153     return cnx->bus;
154 }
155
156 /*---------------------------------------------------------------------------*/
157
158 static DBusObjectPathVTable droute_vtable =
159 {
160   NULL,
161   &handle_message,
162   NULL, NULL, NULL, NULL
163 };
164
165 DRoutePath *
166 droute_add_one (DRouteContext *cnx,
167                 const char    *path,
168                 const void    *data)
169 {
170     DRoutePath *new_path;
171     gboolean registered;
172
173     new_path = path_new (cnx, (void *) data, NULL);
174
175     registered = dbus_connection_register_object_path (cnx->bus, path, &droute_vtable, new_path);
176     if (!registered)
177         oom();
178
179     g_ptr_array_add (cnx->registered_paths, new_path);
180     return new_path;
181 }
182
183 DRoutePath *
184 droute_add_many (DRouteContext *cnx,
185                  const char    *path,
186                  const void    *data,
187                  const DRouteGetDatumFunction get_datum)
188 {
189     DRoutePath *new_path;
190
191     new_path = path_new (cnx, (void *) data, get_datum);
192
193     if (!dbus_connection_register_fallback (cnx->bus, path, &droute_vtable, new_path))
194         oom();
195
196     g_ptr_array_add (cnx->registered_paths, new_path);
197     return new_path;
198 }
199
200 /*---------------------------------------------------------------------------*/
201
202 void
203 droute_path_add_interface(DRoutePath *path,
204                           const char *name,
205                           const char *introspect,
206                           const DRouteMethod   *methods,
207                           const DRouteProperty *properties)
208 {
209     gchar *itf;
210
211     g_return_if_fail (name != NULL);
212
213     itf = g_string_chunk_insert (path->chunks, name);
214     g_ptr_array_add (path->interfaces, itf);
215     g_ptr_array_add (path->introspection, introspect);
216
217     for (; methods != NULL && methods->name != NULL; methods++)
218       {
219         gchar *meth;
220
221         meth = g_string_chunk_insert (path->chunks, methods->name);
222         g_hash_table_insert (path->methods, str_pair_new (itf, meth), methods->func);
223       }
224
225     for (; properties != NULL && properties->name != NULL; properties++)
226       {
227         gchar *prop;
228         PropertyPair *pair;
229
230         prop = g_string_chunk_insert (path->chunks, properties->name);
231         pair = g_new (PropertyPair, 1);
232         pair->get = properties->get;
233         pair->set = properties->set;
234         g_hash_table_insert (path->properties, str_pair_new (itf, prop), pair);
235       }
236 }
237
238 /*---------------------------------------------------------------------------*/
239
240 /* The data structures don't support an efficient implementation of GetAll
241  * and I don't really care.
242  */
243 static DBusMessage *
244 impl_prop_GetAll (DBusMessage *message,
245                   DRoutePath  *path,
246                   const char  *pathstr)
247 {
248     DBusMessageIter iter, iter_dict, iter_dict_entry;
249     DBusMessage *reply;
250     DBusError error;
251     GHashTableIter prop_iter;
252
253     StrPair *key;
254     PropertyPair *value;
255     gchar *iface;
256
257     void  *datum = path_get_datum (path, pathstr);
258     if (!datum)
259         return droute_object_does_not_exist_error (message);
260
261     dbus_error_init (&error);
262     if (!dbus_message_get_args
263                 (message, &error, DBUS_TYPE_STRING, &iface, DBUS_TYPE_INVALID))
264         return dbus_message_new_error (message, DBUS_ERROR_FAILED, error.message);
265
266     reply = dbus_message_new_method_return (message);
267     if (!reply)
268         oom ();
269
270     dbus_message_iter_init_append (reply, &iter);
271     if (!dbus_message_iter_open_container
272                 (&iter, DBUS_TYPE_ARRAY, "{sv}", &iter_dict))
273         oom ();
274
275     g_hash_table_iter_init (&prop_iter, path->properties);
276     while (g_hash_table_iter_next (&prop_iter, (gpointer*)&key, (gpointer*)&value))
277       {
278         if (!g_strcmp0 (key->one, iface))
279          {
280            if (!value->get)
281               continue;
282            if (!dbus_message_iter_open_container
283                         (&iter_dict, DBUS_TYPE_DICT_ENTRY, NULL, &iter_dict_entry))
284               oom ();
285            dbus_message_iter_append_basic (&iter_dict_entry, DBUS_TYPE_STRING,
286                                            key->two);
287            (value->get) (&iter_dict_entry, datum);
288            if (!dbus_message_iter_close_container (&iter_dict, &iter_dict_entry))
289                oom ();
290          }
291       }
292
293     if (!dbus_message_iter_close_container (&iter, &iter_dict))
294         oom ();
295     return reply;
296 }
297
298 static DBusMessage *
299 impl_prop_GetSet (DBusMessage *message,
300                   DRoutePath  *path,
301                   const char  *pathstr,
302                   gboolean     get)
303 {
304     DBusMessage *reply = NULL;
305     DBusError error;
306
307     StrPair pair;
308     PropertyPair *prop_funcs = NULL;
309
310     void *datum;
311
312     dbus_error_init (&error);
313     if (!dbus_message_get_args (message,
314                                 &error,
315                                 DBUS_TYPE_STRING,
316                                 &(pair.one),
317                                 DBUS_TYPE_STRING,
318                                 &(pair.two),
319                                 DBUS_TYPE_INVALID))
320         return dbus_message_new_error (message, DBUS_ERROR_FAILED, error.message);
321
322     _DROUTE_DEBUG ("DRoute (handle prop): %s|%s on %s\n", pair.one, pair.two, pathstr);
323
324     prop_funcs = (PropertyPair *) g_hash_table_lookup (path->properties, &pair);
325     if (!prop_funcs)
326         return dbus_message_new_error (message, DBUS_ERROR_FAILED, "Property unavailable");
327
328     datum = path_get_datum (path, pathstr);
329     if (!datum)
330         return droute_object_does_not_exist_error (message);
331
332     if (get && prop_funcs->get)
333       {
334         
335         DBusMessageIter iter;
336
337         _DROUTE_DEBUG ("DRoute (handle prop Get): %s|%s on %s\n", pair.one, pair.two, pathstr);
338
339         reply = dbus_message_new_method_return (message);
340         dbus_message_iter_init_append (reply, &iter);
341         if (!(prop_funcs->get) (&iter, datum))
342           {
343             dbus_message_unref (reply);
344             reply = dbus_message_new_error (message, DBUS_ERROR_FAILED, "Get failed");
345           }
346       }
347     else if (!get && prop_funcs->set)
348       {
349         DBusMessageIter iter;
350
351         _DROUTE_DEBUG ("DRoute (handle prop Get): %s|%s on %s\n", pair.one, pair.two, pathstr);
352
353         dbus_message_iter_init (message, &iter);
354         /* Skip the interface and property name */
355         dbus_message_iter_next(&iter);
356         dbus_message_iter_next(&iter);
357         (prop_funcs->set) (&iter, datum);
358
359         reply = dbus_message_new_method_return (message);
360       }
361     else
362       {
363         reply = dbus_message_new_error (message, DBUS_ERROR_FAILED, "Getter or setter unavailable");
364       }
365
366     return reply;
367 }
368
369 static DBusHandlerResult
370 handle_properties (DBusConnection *bus,
371                    DBusMessage    *message,
372                    DRoutePath     *path,
373                    const gchar    *iface,
374                    const gchar    *member,
375                    const gchar    *pathstr)
376 {
377     DBusMessage *reply;
378     DBusHandlerResult result = DBUS_HANDLER_RESULT_HANDLED;
379
380     if (!g_strcmp0(member, "GetAll"))
381        reply = impl_prop_GetAll (message, path, pathstr);
382     else if (!g_strcmp0 (member, "Get"))
383        reply = impl_prop_GetSet (message, path, pathstr, TRUE);
384     else if (!g_strcmp0 (member, "Set"))
385        reply = impl_prop_GetSet (message, path, pathstr, FALSE);
386     else
387        result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
388
389     if (reply)
390       {
391         dbus_connection_send (bus, reply, NULL);
392         dbus_message_unref (reply);
393       }
394
395     return result;
396 }
397
398 /*---------------------------------------------------------------------------*/
399
400 static const char *introspection_header =
401 "<?xml version=\"1.0\"?>\n";
402
403 static const char *introspection_node_element =
404 "<node name=\"%s\">\n";
405
406 static const char *introspection_footer =
407 "</node>";
408
409 static DBusHandlerResult
410 handle_introspection (DBusConnection *bus,
411                       DBusMessage    *message,
412                       DRoutePath     *path,
413                       const gchar    *iface,
414                       const gchar    *member,
415                       const gchar    *pathstr)
416 {
417     GString *output;
418     gchar *final;
419     gint i;
420
421     DBusMessage *reply;
422
423     _DROUTE_DEBUG ("DRoute (handle introspection): %s\n", pathstr);
424
425     if (g_strcmp0 (member, "Introspect"))
426         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
427
428     output = g_string_new(introspection_header);
429
430     g_string_append_printf(output, introspection_node_element, pathstr);
431
432     for (i=0; i < path->introspection->len; i++)
433       {
434         gchar *introspect = (gchar *) g_ptr_array_index (path->introspection, i);
435         g_string_append (output, introspect);
436       }
437
438     g_string_append(output, introspection_footer);
439     final = g_string_free(output, FALSE);
440
441     reply = dbus_message_new_method_return (message);
442     if (!reply)
443         oom ();
444     dbus_message_append_args(reply, DBUS_TYPE_STRING, &final,
445                              DBUS_TYPE_INVALID);
446     dbus_connection_send (bus, reply, NULL);
447
448     dbus_message_unref (reply);
449     g_free(final);
450     return DBUS_HANDLER_RESULT_HANDLED;
451 }
452
453 /*---------------------------------------------------------------------------*/
454
455 static DBusHandlerResult
456 handle_other (DBusConnection *bus,
457               DBusMessage    *message,
458               DRoutePath     *path,
459               const gchar    *iface,
460               const gchar    *member,
461               const gchar    *pathstr)
462 {
463     gint result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
464
465     StrPair pair;
466     DRouteFunction func;
467     DBusMessage *reply = NULL;
468
469     void *datum;
470
471     pair.one = iface;
472     pair.two = member;
473
474     _DROUTE_DEBUG ("DRoute (handle other): %s|%s on %s\n", member, iface, pathstr);
475
476     func = (DRouteFunction) g_hash_table_lookup (path->methods, &pair);
477     if (func != NULL)
478       {
479         datum = path_get_datum (path, pathstr);
480         if (!datum)
481             reply = droute_object_does_not_exist_error (message);
482         else
483             reply = (func) (bus, message, datum);
484
485         if (!reply)
486           {
487             /* All D-Bus method calls must have a reply.
488              * If one is not provided presume that the call has a void
489              * return and no error has occured.
490              */
491             reply = dbus_message_new_method_return (message);
492           }
493         dbus_connection_send (bus, reply, NULL);
494         dbus_message_unref (reply);
495         result = DBUS_HANDLER_RESULT_HANDLED;
496       }
497
498     _DROUTE_DEBUG ("DRoute (handle other) (reply): type %d\n",
499                    dbus_message_get_type(reply));
500     return result;
501 }
502
503 /*---------------------------------------------------------------------------*/
504
505 static DBusHandlerResult
506 handle_message (DBusConnection *bus, DBusMessage *message, void *user_data)
507 {
508     DRoutePath *path = (DRoutePath *) user_data;
509     const gchar *iface   = dbus_message_get_interface (message);
510     const gchar *member  = dbus_message_get_member (message);
511     const gint   type    = dbus_message_get_type (message);
512     const gchar *pathstr = dbus_message_get_path (message);
513
514     DBusHandlerResult result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
515
516     _DROUTE_DEBUG ("DRoute (handle message): %s|%s of type %d on %s\n", member, iface, type, pathstr);
517
518     /* Check for basic reasons not to handle */
519     if (type   != DBUS_MESSAGE_TYPE_METHOD_CALL ||
520         member == NULL ||
521         iface  == NULL)
522         return result;
523
524     if (!strcmp (iface, "org.freedesktop.DBus.Properties"))
525         result = handle_properties (bus, message, path, iface, member, pathstr);
526     else if (!strcmp (iface, "org.freedesktop.DBus.Introspectable"))
527         result = handle_introspection (bus, message, path, iface, member, pathstr);
528     else
529         result = handle_other (bus, message, path, iface, member, pathstr);
530 #if 0
531     if (result == DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
532         g_print ("DRoute | Unhandled message: %s|%s of type %d on %s\n", member, iface, type, pathstr);
533 #endif
534       
535     return result;
536 }
537
538 /*---------------------------------------------------------------------------*/
539
540 static DBusMessage *
541 droute_object_does_not_exist_error (DBusMessage *message)
542 {
543     DBusMessage *reply;
544     gchar       *errmsg;
545
546     errmsg= g_strdup_printf (
547             "Method \"%s\" with signature \"%s\" on interface \"%s\" could not be processed as object %s does not exist\n",
548             dbus_message_get_member (message),
549             dbus_message_get_signature (message),
550             dbus_message_get_interface (message),
551             dbus_message_get_path (message));
552     reply = dbus_message_new_error (message,
553                                     DBUS_ERROR_FAILED,
554                                     errmsg);
555     g_free (errmsg);
556     return reply;
557 }
558
559 /*---------------------------------------------------------------------------*/
560
561 DBusMessage *
562 droute_not_yet_handled_error (DBusMessage *message)
563 {
564     DBusMessage *reply;
565     gchar       *errmsg;
566
567     errmsg= g_strdup_printf (
568             "Method \"%s\" with signature \"%s\" on interface \"%s\" doesn't exist\n",
569             dbus_message_get_member (message),
570             dbus_message_get_signature (message),
571             dbus_message_get_interface (message));
572     reply = dbus_message_new_error (message,
573                                     DBUS_ERROR_UNKNOWN_METHOD,
574                                     errmsg);
575     g_free (errmsg);
576     return reply;
577 }
578
579 DBusMessage *
580 droute_out_of_memory_error (DBusMessage *message)
581 {
582     DBusMessage *reply;
583     gchar       *errmsg;
584
585     errmsg= g_strdup_printf (
586             "Method \"%s\" with signature \"%s\" on interface \"%s\" could not be processed due to lack of memory\n",
587             dbus_message_get_member (message),
588             dbus_message_get_signature (message),
589             dbus_message_get_interface (message));
590     reply = dbus_message_new_error (message,
591                                     DBUS_ERROR_NO_MEMORY,
592                                     errmsg);
593     g_free (errmsg);
594     return reply;
595 }
596
597 DBusMessage *
598 droute_invalid_arguments_error (DBusMessage *message)
599 {
600     DBusMessage *reply;
601     gchar       *errmsg;
602
603     errmsg= g_strdup_printf (
604             "Method \"%s\" with signature \"%s\" on interface \"%s\" was supplied with invalid arguments\n",
605             dbus_message_get_member (message),
606             dbus_message_get_signature (message),
607             dbus_message_get_interface (message));
608     reply = dbus_message_new_error (message,
609                                     DBUS_ERROR_INVALID_ARGS,
610                                     errmsg);
611     g_free (errmsg);
612     return reply;
613 }
614
615 /*END------------------------------------------------------------------------*/