2005-03-09 Joe Shaw <joeshaw@novell.com>
[platform/upstream/dbus.git] / dbus / dbus-object-tree.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-object-tree.c  DBusObjectTree (internals of DBusConnection)
3  *
4  * Copyright (C) 2003, 2005  Red Hat Inc.
5  *
6  * Licensed under the Academic Free License version 2.1
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program 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
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23 #include "dbus-object-tree.h"
24 #include "dbus-connection-internal.h"
25 #include "dbus-internals.h"
26 #include "dbus-hash.h"
27 #include "dbus-protocol.h"
28 #include "dbus-string.h"
29 #include <string.h>
30 #include <stdlib.h>
31
32 /**
33  * @defgroup DBusObjectTree A hierarchy of objects with container-contained relationship
34  * @ingroup  DBusInternals
35  * @brief DBusObjectTree is used by DBusConnection to track the object tree
36  *
37  * Types and functions related to DBusObjectTree. These
38  * are all library-internal.
39  *
40  * @{
41  */
42
43 /** Subnode of the object hierarchy */
44 typedef struct DBusObjectSubtree DBusObjectSubtree;
45
46 static DBusObjectSubtree* _dbus_object_subtree_new   (const char                  *name,
47                                                       const DBusObjectPathVTable  *vtable,
48                                                       void                        *user_data);
49 static DBusObjectSubtree* _dbus_object_subtree_ref   (DBusObjectSubtree           *subtree);
50 static void               _dbus_object_subtree_unref (DBusObjectSubtree           *subtree);
51
52 /**
53  * Internals of DBusObjectTree
54  */
55 struct DBusObjectTree
56 {
57   int                 refcount;   /**< Reference count */
58   DBusConnection     *connection; /**< Connection this tree belongs to */
59
60   DBusObjectSubtree  *root;       /**< Root of the tree ("/" node) */
61 };
62
63 /**
64  * Struct representing a single registered subtree handler, or node
65  * that's a parent of a registered subtree handler. If
66  * message_function != NULL there's actually a handler at this node.
67  */
68 struct DBusObjectSubtree
69 {
70   DBusAtomic                         refcount;            /**< Reference count */
71   DBusObjectSubtree                 *parent;              /**< Parent node */
72   DBusObjectPathUnregisterFunction   unregister_function; /**< Function to call on unregister */
73   DBusObjectPathMessageFunction      message_function;    /**< Function to handle messages */
74   void                              *user_data;           /**< Data for functions */
75   DBusObjectSubtree                **subtrees;            /**< Child nodes */
76   int                                n_subtrees;          /**< Number of child nodes */
77   unsigned int                       subtrees_sorted : 1; /**< Whether children are sorted */
78   unsigned int                       invoke_as_fallback : 1; /**< Whether to invoke message_function when child nodes don't handle the message */
79   char                               name[1]; /**< Allocated as large as necessary */
80 };
81
82 /**
83  * Creates a new object tree, representing a mapping from paths
84  * to handler vtables.
85  *
86  * @param connection the connection this tree belongs to
87  * @returns the new tree or #NULL if no memory
88  */
89 DBusObjectTree*
90 _dbus_object_tree_new (DBusConnection *connection)
91 {
92   DBusObjectTree *tree;
93
94   /* the connection passed in here isn't fully constructed,
95    * so don't do anything more than store a pointer to
96    * it
97    */
98
99   tree = dbus_new0 (DBusObjectTree, 1);
100   if (tree == NULL)
101     goto oom;
102
103   tree->refcount = 1;
104   tree->connection = connection;
105   tree->root = _dbus_object_subtree_new ("/", NULL, NULL);
106   if (tree->root == NULL)
107     goto oom;
108   tree->root->invoke_as_fallback = TRUE;
109   
110   return tree;
111
112  oom:
113   if (tree)
114     {
115       dbus_free (tree);
116     }
117
118   return NULL;
119 }
120
121 /**
122  * Increment the reference count
123  * @param tree the object tree
124  * @returns the object tree
125  */
126 DBusObjectTree *
127 _dbus_object_tree_ref (DBusObjectTree *tree)
128 {
129   _dbus_assert (tree->refcount > 0);
130
131   tree->refcount += 1;
132
133   return tree;
134 }
135
136 /**
137  * Decrement the reference count
138  * @param tree the object tree
139  */
140 void
141 _dbus_object_tree_unref (DBusObjectTree *tree)
142 {
143   _dbus_assert (tree->refcount > 0);
144
145   tree->refcount -= 1;
146
147   if (tree->refcount == 0)
148     {
149       _dbus_object_tree_free_all_unlocked (tree);
150
151       dbus_free (tree);
152     }
153 }
154
155 static int
156 subtree_cmp (DBusObjectSubtree *subtree_a,
157              DBusObjectSubtree *subtree_b)
158 {
159   return strcmp (subtree_a->name, subtree_b->name);
160 }
161
162 static int
163 subtree_qsort_cmp (const void *a,
164                    const void *b)
165 {
166   DBusObjectSubtree **subtree_a_p = (void*) a;
167   DBusObjectSubtree **subtree_b_p = (void*) b;
168
169   return subtree_cmp (*subtree_a_p, *subtree_b_p);
170 }
171
172 static void
173 ensure_sorted (DBusObjectSubtree *subtree)
174 {
175   if (subtree->subtrees && !subtree->subtrees_sorted)
176     {
177       qsort (subtree->subtrees,
178              subtree->n_subtrees,
179              sizeof (DBusObjectSubtree*),
180              subtree_qsort_cmp);
181       subtree->subtrees_sorted = TRUE;
182     }
183 }
184
185 /** Set to 1 to get a bunch of debug spew about finding the
186  * subtree nodes
187  */
188 #define VERBOSE_FIND 0
189
190 static DBusObjectSubtree*
191 find_subtree_recurse (DBusObjectSubtree  *subtree,
192                       const char        **path,
193                       dbus_bool_t         create_if_not_found,
194                       int                *index_in_parent,
195                       dbus_bool_t        *exact_match)
196 {
197   int i;
198   dbus_bool_t return_deepest_match;
199
200   return_deepest_match = exact_match != NULL;
201
202   _dbus_assert (!(return_deepest_match && create_if_not_found));
203
204   if (path[0] == NULL)
205     {
206 #if VERBOSE_FIND
207       _dbus_verbose ("  path exhausted, returning %s\n",
208                      subtree->name);
209 #endif
210       if (exact_match != NULL)
211         *exact_match = TRUE;
212       return subtree;
213     }
214
215 #if VERBOSE_FIND
216   _dbus_verbose ("  searching children of %s for %s\n",
217                  subtree->name, path[0]);
218 #endif
219   
220   ensure_sorted (subtree);
221
222   /* FIXME we should do a binary search here instead
223    * of O(n)
224    */
225
226   i = 0;
227   while (i < subtree->n_subtrees)
228     {
229       int v;
230
231       v = strcmp (path[0], subtree->subtrees[i]->name);
232
233 #if VERBOSE_FIND
234       _dbus_verbose ("  %s cmp %s = %d\n",
235                      path[0], subtree->subtrees[i]->name,
236                      v);
237 #endif
238       
239       if (v == 0)
240         {
241           if (index_in_parent)
242             {
243 #if VERBOSE_FIND
244               _dbus_verbose ("  storing parent index %d\n", i);
245 #endif
246               *index_in_parent = i;
247             }
248
249           if (return_deepest_match)
250             {
251               DBusObjectSubtree *next;
252
253               next = find_subtree_recurse (subtree->subtrees[i],
254                                            &path[1], create_if_not_found, 
255                                            index_in_parent, exact_match);
256               if (next == NULL &&
257                   subtree->invoke_as_fallback)
258                 {
259 #if VERBOSE_FIND
260                   _dbus_verbose ("  no deeper match found, returning %s\n",
261                                  subtree->name);
262 #endif
263                   if (exact_match != NULL)
264                     *exact_match = FALSE;
265                   return subtree;
266                 }
267               else
268                 return next;
269             }
270           else
271             return find_subtree_recurse (subtree->subtrees[i],
272                                          &path[1], create_if_not_found, 
273                                          index_in_parent, exact_match);
274         }
275       else if (v < 0)
276         {
277           goto not_found;
278         }
279
280       ++i;
281     }
282
283  not_found:
284 #if VERBOSE_FIND
285   _dbus_verbose ("  no match found, current tree %s, create_if_not_found = %d\n",
286                  subtree->name, create_if_not_found);
287 #endif
288   
289   if (create_if_not_found)
290     {
291       DBusObjectSubtree* child;
292       DBusObjectSubtree **new_subtrees;
293       int new_n_subtrees;
294
295 #if VERBOSE_FIND
296       _dbus_verbose ("  creating subtree %s\n",
297                      path[0]);
298 #endif
299       
300       child = _dbus_object_subtree_new (path[0],
301                                         NULL, NULL);
302       if (child == NULL)
303         return NULL;
304
305       /* FIXME we should do the "double alloc each time" standard thing */
306       new_n_subtrees = subtree->n_subtrees + 1;
307       new_subtrees = dbus_realloc (subtree->subtrees,
308                                    new_n_subtrees * sizeof (DBusObjectSubtree*));
309       if (new_subtrees == NULL)
310         {
311           child->unregister_function = NULL;
312           child->message_function = NULL;
313           _dbus_object_subtree_unref (child);
314           return NULL;
315         }
316
317       new_subtrees[subtree->n_subtrees] = child;
318       if (index_in_parent)
319         *index_in_parent = subtree->n_subtrees;
320       subtree->subtrees_sorted = FALSE;
321       subtree->n_subtrees = new_n_subtrees;
322       subtree->subtrees = new_subtrees;
323
324       child->parent = subtree;
325
326       return find_subtree_recurse (child,
327                                    &path[1], create_if_not_found, 
328                                    index_in_parent, exact_match);
329     }
330   else
331     {
332       if (exact_match != NULL)
333         *exact_match = FALSE;
334       return (return_deepest_match && subtree->invoke_as_fallback) ? subtree : NULL;
335     }
336 }
337
338 static DBusObjectSubtree*
339 find_subtree (DBusObjectTree *tree,
340               const char    **path,
341               int            *index_in_parent)
342 {
343   DBusObjectSubtree *subtree;
344
345 #if VERBOSE_FIND
346   _dbus_verbose ("Looking for exact registered subtree\n");
347 #endif
348   
349   subtree = find_subtree_recurse (tree->root, path, FALSE, index_in_parent, NULL);
350
351   if (subtree && subtree->message_function == NULL)
352     return NULL;
353   else
354     return subtree;
355 }
356
357 static DBusObjectSubtree*
358 lookup_subtree (DBusObjectTree *tree,
359                 const char    **path)
360 {
361 #if VERBOSE_FIND
362   _dbus_verbose ("Looking for subtree\n");
363 #endif
364   return find_subtree_recurse (tree->root, path, FALSE, NULL, NULL);
365 }
366
367 static DBusObjectSubtree*
368 find_handler (DBusObjectTree *tree,
369               const char    **path,
370               dbus_bool_t    *exact_match)
371 {
372 #if VERBOSE_FIND
373   _dbus_verbose ("Looking for deepest handler\n");
374 #endif
375   _dbus_assert (exact_match != NULL);
376   return find_subtree_recurse (tree->root, path, FALSE, NULL, exact_match);
377 }
378
379 static DBusObjectSubtree*
380 ensure_subtree (DBusObjectTree *tree,
381                 const char    **path)
382 {
383 #if VERBOSE_FIND
384   _dbus_verbose ("Ensuring subtree\n");
385 #endif
386   return find_subtree_recurse (tree->root, path, TRUE, NULL, NULL);
387 }
388
389 /**
390  * Registers a new subtree in the global object tree.
391  *
392  * @param tree the global object tree
393  * @param fallback #TRUE to handle messages to children of this path
394  * @param path NULL-terminated array of path elements giving path to subtree
395  * @param vtable the vtable used to traverse this subtree
396  * @param user_data user data to pass to methods in the vtable
397  * @returns #FALSE if not enough memory
398  */
399 dbus_bool_t
400 _dbus_object_tree_register (DBusObjectTree              *tree,
401                             dbus_bool_t                  fallback,
402                             const char                 **path,
403                             const DBusObjectPathVTable  *vtable,
404                             void                        *user_data)
405 {
406   DBusObjectSubtree  *subtree;
407
408   _dbus_assert (tree != NULL);
409   _dbus_assert (vtable->message_function != NULL);
410   _dbus_assert (path != NULL);
411
412   subtree = ensure_subtree (tree, path);
413   if (subtree == NULL)
414     return FALSE;
415
416 #ifndef DBUS_DISABLE_CHECKS
417   if (subtree->message_function != NULL)
418     {
419       _dbus_warn ("A handler is already registered for the path starting with path[0] = \"%s\"\n",
420                   path[0] ? path[0] : "null");
421       return FALSE;
422     }
423 #else
424   _dbus_assert (subtree->message_function == NULL);
425 #endif
426
427   subtree->message_function = vtable->message_function;
428   subtree->unregister_function = vtable->unregister_function;
429   subtree->user_data = user_data;
430   subtree->invoke_as_fallback = fallback != FALSE;
431   
432   return TRUE;
433 }
434
435 /**
436  * Unregisters an object subtree that was registered with the
437  * same path.
438  *
439  * @param tree the global object tree
440  * @param path path to the subtree (same as the one passed to _dbus_object_tree_register())
441  */
442 void
443 _dbus_object_tree_unregister_and_unlock (DBusObjectTree          *tree,
444                                          const char             **path)
445 {
446   int i;
447   DBusObjectSubtree *subtree;
448   DBusObjectPathUnregisterFunction unregister_function;
449   void *user_data;
450   DBusConnection *connection;
451
452   _dbus_assert (path != NULL);
453
454   unregister_function = NULL;
455   user_data = NULL;
456
457   subtree = find_subtree (tree, path, &i);
458
459 #ifndef DBUS_DISABLE_CHECKS
460   if (subtree == NULL)
461     {
462       _dbus_warn ("Attempted to unregister path (path[0] = %s path[1] = %s) which isn't registered\n",
463                   path[0] ? path[0] : "null",
464                   path[1] ? path[1] : "null");
465       goto unlock;    
466     }
467 #else
468   _dbus_assert (subtree != NULL);
469 #endif
470
471   _dbus_assert (subtree->parent == NULL ||
472                 (i >= 0 && subtree->parent->subtrees[i] == subtree));
473
474   subtree->message_function = NULL;
475
476   unregister_function = subtree->unregister_function;
477   user_data = subtree->user_data;
478
479   subtree->unregister_function = NULL;
480   subtree->user_data = NULL;
481
482   /* If we have no subtrees of our own, remove from
483    * our parent (FIXME could also be more aggressive
484    * and remove our parent if it becomes empty)
485    */
486   if (subtree->parent && subtree->n_subtrees == 0)
487     {
488       /* assumes a 0-byte memmove is OK */
489       memmove (&subtree->parent->subtrees[i],
490                &subtree->parent->subtrees[i+1],
491                (subtree->parent->n_subtrees - i - 1) *
492                sizeof (subtree->parent->subtrees[0]));
493       subtree->parent->n_subtrees -= 1;
494
495       subtree->parent = NULL;
496
497       _dbus_object_subtree_unref (subtree);
498     }
499   subtree = NULL;
500
501 unlock:
502   connection = tree->connection;
503
504   /* Unlock and call application code */
505 #ifdef DBUS_BUILD_TESTS
506   if (connection)
507 #endif
508     {
509       _dbus_connection_ref_unlocked (connection);
510       _dbus_verbose ("unlock %s\n", _DBUS_FUNCTION_NAME);
511       _dbus_connection_unlock (connection);
512     }
513
514   if (unregister_function)
515     (* unregister_function) (connection, user_data);
516
517 #ifdef DBUS_BUILD_TESTS
518   if (connection)
519 #endif
520     dbus_connection_unref (connection);
521 }
522
523 static void
524 free_subtree_recurse (DBusConnection    *connection,
525                       DBusObjectSubtree *subtree)
526 {
527   /* Delete them from the end, for slightly
528    * more robustness against odd reentrancy.
529    */
530   while (subtree->n_subtrees > 0)
531     {
532       DBusObjectSubtree *child;
533
534       child = subtree->subtrees[subtree->n_subtrees - 1];
535       subtree->subtrees[subtree->n_subtrees - 1] = NULL;
536       subtree->n_subtrees -= 1;
537       child->parent = NULL;
538
539       free_subtree_recurse (connection, child);
540     }
541
542   /* Call application code */
543   if (subtree->unregister_function)
544     (* subtree->unregister_function) (connection,
545                                       subtree->user_data);
546
547   subtree->message_function = NULL;
548   subtree->unregister_function = NULL;
549   subtree->user_data = NULL;
550
551   /* Now free ourselves */
552   _dbus_object_subtree_unref (subtree);
553 }
554
555 /**
556  * Free all the handlers in the tree. Lock on tree's connection
557  * must not be held.
558  *
559  * @param tree the object tree
560  */
561 void
562 _dbus_object_tree_free_all_unlocked (DBusObjectTree *tree)
563 {
564   if (tree->root)
565     free_subtree_recurse (tree->connection,
566                           tree->root);
567   tree->root = NULL;
568 }
569
570 static dbus_bool_t
571 _dbus_object_tree_list_registered_unlocked (DBusObjectTree *tree,
572                                             const char    **parent_path,
573                                             char         ***child_entries)
574 {
575   DBusObjectSubtree *subtree;
576   char **retval;
577   
578   _dbus_assert (parent_path != NULL);
579   _dbus_assert (child_entries != NULL);
580
581   *child_entries = NULL;
582   
583   subtree = lookup_subtree (tree, parent_path);
584   if (subtree == NULL)
585     {
586       retval = dbus_new0 (char *, 1);
587     }
588   else
589     {
590       int i;
591       retval = dbus_new0 (char*, subtree->n_subtrees + 1);
592       if (retval == NULL)
593         goto out;
594       i = 0;
595       while (i < subtree->n_subtrees)
596         {
597           retval[i] = _dbus_strdup (subtree->subtrees[i]->name);
598           if (retval[i] == NULL)
599             {
600               dbus_free_string_array (retval);
601               retval = NULL;
602               goto out;
603             }
604           ++i;
605         }
606     }
607
608  out:
609     
610   *child_entries = retval;
611   return retval != NULL;
612 }
613
614 static DBusHandlerResult
615 handle_default_introspect_and_unlock (DBusObjectTree          *tree,
616                                       DBusMessage             *message,
617                                       const char             **path)
618 {
619   DBusString xml;
620   DBusHandlerResult result;
621   char **children;
622   int i;
623   DBusMessage *reply;
624   DBusMessageIter iter;
625   const char *v_STRING;
626   dbus_bool_t already_unlocked;
627
628   /* We have the connection lock here */
629
630   already_unlocked = FALSE;
631   
632   _dbus_verbose (" considering default Introspect() handler...\n");
633
634   reply = NULL;
635   
636   if (!dbus_message_is_method_call (message,
637                                     DBUS_INTERFACE_INTROSPECTABLE,
638                                     "Introspect"))
639     {
640 #ifdef DBUS_BUILD_TESTS
641       if (tree->connection)
642 #endif
643         {
644           _dbus_verbose ("unlock %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
645           _dbus_connection_unlock (tree->connection);
646         }
647       
648       return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
649     }
650
651   _dbus_verbose (" using default Introspect() handler!\n");
652   
653   if (!_dbus_string_init (&xml))
654     {
655 #ifdef DBUS_BUILD_TESTS
656       if (tree->connection)
657 #endif
658         {
659           _dbus_verbose ("unlock %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
660           _dbus_connection_unlock (tree->connection);
661         }
662
663       return DBUS_HANDLER_RESULT_NEED_MEMORY;
664     }
665
666   result = DBUS_HANDLER_RESULT_NEED_MEMORY;
667
668   children = NULL;
669   if (!_dbus_object_tree_list_registered_unlocked (tree, path, &children))
670     goto out;
671
672   if (!_dbus_string_append (&xml, DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE))
673     goto out;
674   
675   if (!_dbus_string_append (&xml, "<node>\n"))
676     goto out;
677
678   i = 0;
679   while (children[i] != NULL)
680     {
681       if (!_dbus_string_append_printf (&xml, "  <node name=\"%s\"/>\n",
682                                        children[i]))
683         goto out;
684
685       ++i;
686     }
687
688   if (!_dbus_string_append (&xml, "</node>\n"))
689     goto out;
690
691   reply = dbus_message_new_method_return (message);
692   if (reply == NULL)
693     goto out;
694
695   dbus_message_iter_init_append (reply, &iter);
696   v_STRING = _dbus_string_get_const_data (&xml);
697   if (!dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &v_STRING))
698     goto out;
699   
700 #ifdef DBUS_BUILD_TESTS
701   if (tree->connection)
702 #endif
703     {
704       already_unlocked = TRUE;
705       
706       if (!_dbus_connection_send_and_unlock (tree->connection, reply, NULL))
707         goto out;
708     }
709   
710   result = DBUS_HANDLER_RESULT_HANDLED;
711   
712  out:
713 #ifdef DBUS_BUILD_TESTS
714   if (tree->connection)
715 #endif
716     {
717       if (!already_unlocked)
718         {
719           _dbus_verbose ("unlock %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
720           _dbus_connection_unlock (tree->connection);
721         }
722     }
723   
724   _dbus_string_free (&xml);
725   dbus_free_string_array (children);
726   if (reply)
727     dbus_message_unref (reply);
728   
729   return result;
730 }
731
732 /**
733  * Tries to dispatch a message by directing it to handler for the
734  * object path listed in the message header, if any. Messages are
735  * dispatched first to the registered handler that matches the largest
736  * number of path elements; that is, message to /foo/bar/baz would go
737  * to the handler for /foo/bar before the one for /foo.
738  *
739  * @todo thread problems
740  *
741  * @param tree the global object tree
742  * @param message the message to dispatch
743  * @returns whether message was handled successfully
744  */
745 DBusHandlerResult
746 _dbus_object_tree_dispatch_and_unlock (DBusObjectTree          *tree,
747                                        DBusMessage             *message)
748 {
749   char **path;
750   dbus_bool_t exact_match;
751   DBusList *list;
752   DBusList *link;
753   DBusHandlerResult result;
754   DBusObjectSubtree *subtree;
755   
756 #if 0
757   _dbus_verbose ("Dispatch of message by object path\n");
758 #endif
759   
760   path = NULL;
761   if (!dbus_message_get_path_decomposed (message, &path))
762     {
763 #ifdef DBUS_BUILD_TESTS
764       if (tree->connection)
765 #endif
766         {
767           _dbus_verbose ("unlock %s\n", _DBUS_FUNCTION_NAME);
768           _dbus_connection_unlock (tree->connection);
769         }
770       
771       _dbus_verbose ("No memory to get decomposed path\n");
772
773       return DBUS_HANDLER_RESULT_NEED_MEMORY;
774     }
775
776   if (path == NULL)
777     {
778 #ifdef DBUS_BUILD_TESTS
779       if (tree->connection)
780 #endif
781         {
782           _dbus_verbose ("unlock %s\n", _DBUS_FUNCTION_NAME);
783           _dbus_connection_unlock (tree->connection);
784         }
785       
786       _dbus_verbose ("No path field in message\n");
787       return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
788     }
789   
790   /* Find the deepest path that covers the path in the message */
791   subtree = find_handler (tree, (const char**) path, &exact_match);
792   
793   /* Build a list of all paths that cover the path in the message */
794
795   list = NULL;
796
797   while (subtree != NULL)
798     {
799       if (subtree->message_function != NULL && (exact_match || subtree->invoke_as_fallback))
800         {
801           _dbus_object_subtree_ref (subtree);
802
803           /* run deepest paths first */
804           if (!_dbus_list_append (&list, subtree))
805             {
806               result = DBUS_HANDLER_RESULT_NEED_MEMORY;
807               _dbus_object_subtree_unref (subtree);
808               goto free_and_return;
809             }
810         }
811
812       exact_match = FALSE;
813       subtree = subtree->parent;
814     }
815
816   _dbus_verbose ("%d handlers in the path tree for this message\n",
817                  _dbus_list_get_length (&list));
818
819   /* Invoke each handler in the list */
820
821   result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
822
823   link = _dbus_list_get_first_link (&list);
824   while (link != NULL)
825     {
826       DBusList *next = _dbus_list_get_next_link (&list, link);
827       subtree = link->data;
828
829       /* message_function is NULL if we're unregistered
830        * due to reentrancy
831        */
832       if (subtree->message_function)
833         {
834           DBusObjectPathMessageFunction message_function;
835           void *user_data;
836
837           message_function = subtree->message_function;
838           user_data = subtree->user_data;
839
840 #if 0
841           _dbus_verbose ("  (invoking a handler)\n");
842 #endif
843           
844 #ifdef DBUS_BUILD_TESTS
845           if (tree->connection)
846 #endif
847             {
848               _dbus_verbose ("unlock %s\n", _DBUS_FUNCTION_NAME);
849               _dbus_connection_unlock (tree->connection);
850             }
851
852           /* FIXME you could unregister the subtree in another thread
853            * before we invoke the callback, and I can't figure out a
854            * good way to solve this.
855            */
856
857           result = (* message_function) (tree->connection,
858                                          message,
859                                          user_data);
860
861 #ifdef DBUS_BUILD_TESTS
862           if (tree->connection)
863 #endif
864             _dbus_connection_lock (tree->connection);
865
866           if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
867             goto free_and_return;
868         }
869
870       link = next;
871     }
872
873  free_and_return:
874
875   if (result == DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
876     {
877       /* This hardcoded default handler does a minimal Introspect()
878        */
879       result = handle_default_introspect_and_unlock (tree, message,
880                                                      (const char**) path);
881     }
882   else
883     {
884 #ifdef DBUS_BUILD_TESTS
885       if (tree->connection)
886 #endif
887         {
888           _dbus_verbose ("unlock %s\n", _DBUS_FUNCTION_NAME);
889           _dbus_connection_unlock (tree->connection);
890         }
891     }
892   
893   while (list != NULL)
894     {
895       link = _dbus_list_get_first_link (&list);
896       _dbus_object_subtree_unref (link->data);
897       _dbus_list_remove_link (&list, link);
898     }
899   
900   dbus_free_string_array (path);
901
902   return result;
903 }
904
905 /**
906  * Allocates a subtree object.
907  *
908  * @param name name to duplicate.
909  * @returns newly-allocated subtree
910  */
911 static DBusObjectSubtree*
912 allocate_subtree_object (const char *name)
913 {
914   int len;
915   DBusObjectSubtree *subtree;
916   const size_t front_padding = _DBUS_STRUCT_OFFSET (DBusObjectSubtree, name);
917
918   _dbus_assert (name != NULL);
919
920   len = strlen (name);
921
922   subtree = dbus_malloc (front_padding + (len + 1));
923
924   if (subtree == NULL)
925     return NULL;
926
927   memcpy (subtree->name, name, len + 1);
928
929   return subtree;
930 }
931
932 static DBusObjectSubtree*
933 _dbus_object_subtree_new (const char                  *name,
934                           const DBusObjectPathVTable  *vtable,
935                           void                        *user_data)
936 {
937   DBusObjectSubtree *subtree;
938
939   subtree = allocate_subtree_object (name);
940   if (subtree == NULL)
941     goto oom;
942
943   _dbus_assert (name != NULL);
944
945   subtree->parent = NULL;
946
947   if (vtable)
948     {
949       subtree->message_function = vtable->message_function;
950       subtree->unregister_function = vtable->unregister_function;
951     }
952   else
953     {
954       subtree->message_function = NULL;
955       subtree->unregister_function = NULL;
956     }
957
958   subtree->user_data = user_data;
959   subtree->refcount.value = 1;
960   subtree->subtrees = NULL;
961   subtree->n_subtrees = 0;
962   subtree->subtrees_sorted = TRUE;
963   subtree->invoke_as_fallback = FALSE;
964
965   return subtree;
966
967  oom:
968   if (subtree)
969     {
970       dbus_free (subtree);
971     }
972
973   return NULL;
974 }
975
976 static DBusObjectSubtree *
977 _dbus_object_subtree_ref (DBusObjectSubtree *subtree)
978 {
979   _dbus_assert (subtree->refcount.value > 0);
980   _dbus_atomic_inc (&subtree->refcount);
981
982   return subtree;
983 }
984
985 static void
986 _dbus_object_subtree_unref (DBusObjectSubtree *subtree)
987 {
988   _dbus_assert (subtree->refcount.value > 0);
989
990   if (_dbus_atomic_dec (&subtree->refcount) == 1)
991     {
992       _dbus_assert (subtree->unregister_function == NULL);
993       _dbus_assert (subtree->message_function == NULL);
994
995       dbus_free (subtree->subtrees);
996       dbus_free (subtree);
997     }
998 }
999
1000 /**
1001  * Lists the registered fallback handlers and object path handlers at
1002  * the given parent_path. The returned array should be freed with
1003  * dbus_free_string_array().
1004  *
1005  * @param tree the object tree
1006  * @param parent_path the path to list the child handlers of
1007  * @param child_entries returns #NULL-terminated array of children
1008  * @returns #FALSE if no memory to allocate the child entries
1009  */
1010 dbus_bool_t
1011 _dbus_object_tree_list_registered_and_unlock (DBusObjectTree *tree,
1012                                               const char    **parent_path,
1013                                               char         ***child_entries)
1014 {
1015   dbus_bool_t result;
1016
1017   result = _dbus_object_tree_list_registered_unlocked (tree,
1018                                                        parent_path,
1019                                                        child_entries);
1020   
1021 #ifdef DBUS_BUILD_TESTS
1022   if (tree->connection)
1023 #endif
1024     {
1025       _dbus_verbose ("unlock %s\n", _DBUS_FUNCTION_NAME);
1026       _dbus_connection_unlock (tree->connection);
1027     }
1028
1029   return result;
1030 }
1031
1032
1033 /** Set to 1 to get a bunch of spew about disassembling the path string */
1034 #define VERBOSE_DECOMPOSE 0
1035
1036 /**
1037  * Decompose an object path.  A path of just "/" is
1038  * represented as an empty vector of strings.
1039  * The path need not be nul terminated.
1040  * 
1041  * @param data the path data
1042  * @param len  the length of the path string
1043  * @param path address to store new object path
1044  * @param path_len length of stored path
1045  */
1046 dbus_bool_t
1047 _dbus_decompose_path (const char*     data,
1048                       int             len,
1049                       char         ***path,
1050                       int            *path_len)
1051 {
1052   char **retval;
1053   int n_components;
1054   int i, j, comp;
1055
1056   _dbus_assert (data != NULL);
1057   
1058 #if VERBOSE_DECOMPOSE
1059   _dbus_verbose ("Decomposing path \"%s\"\n",
1060                  data);
1061 #endif
1062   
1063   n_components = 0;
1064   if (len > 1) /* if path is not just "/" */
1065     {
1066       i = 0;
1067       while (i < len)
1068         {
1069           if (data[i] == '/')
1070             n_components += 1;
1071           ++i;
1072         }
1073     }
1074   
1075   retval = dbus_new0 (char*, n_components + 1);
1076
1077   if (retval == NULL)
1078     return FALSE;
1079
1080   comp = 0;
1081   if (n_components == 0)
1082     i = 1;
1083   else
1084     i = 0;
1085   while (comp < n_components)
1086     {
1087       _dbus_assert (i < len);
1088       
1089       if (data[i] == '/')
1090         ++i;
1091       j = i;
1092
1093       while (j < len && data[j] != '/')
1094         ++j;
1095
1096       /* Now [i, j) is the path component */
1097       _dbus_assert (i < j);
1098       _dbus_assert (data[i] != '/');
1099       _dbus_assert (j == len || data[j] == '/');
1100
1101 #if VERBOSE_DECOMPOSE
1102       _dbus_verbose ("  (component in [%d,%d))\n",
1103                      i, j);
1104 #endif
1105       
1106       retval[comp] = _dbus_memdup (&data[i], j - i + 1);
1107       if (retval[comp] == NULL)
1108         {
1109           dbus_free_string_array (retval);
1110           return FALSE;
1111         }
1112       retval[comp][j-i] = '\0';
1113 #if VERBOSE_DECOMPOSE
1114       _dbus_verbose ("  (component %d = \"%s\")\n",
1115                      comp, retval[comp]);
1116 #endif
1117
1118       ++comp;
1119       i = j;
1120     }
1121   _dbus_assert (i == len);
1122   
1123   *path = retval;
1124   if (path_len)
1125     *path_len = n_components;
1126   
1127   return TRUE;
1128 }
1129
1130 /** @} */
1131
1132 #ifdef DBUS_BUILD_TESTS
1133 #include "dbus-test.h"
1134 #include <stdio.h>
1135
1136 static char*
1137 flatten_path (const char **path)
1138 {
1139   DBusString str;
1140   char *s;
1141
1142   if (!_dbus_string_init (&str))
1143     return NULL;
1144
1145   if (path[0] == NULL)
1146     {
1147       if (!_dbus_string_append_byte (&str, '/'))
1148         goto nomem;
1149     }
1150   else
1151     {
1152       int i;
1153       
1154       i = 0;
1155       while (path[i])
1156         {
1157           if (!_dbus_string_append_byte (&str, '/'))
1158             goto nomem;
1159           
1160           if (!_dbus_string_append (&str, path[i]))
1161             goto nomem;
1162           
1163           ++i;
1164         }
1165     }
1166
1167   if (!_dbus_string_steal_data (&str, &s))
1168     goto nomem;
1169
1170   _dbus_string_free (&str);
1171
1172   return s;
1173
1174  nomem:
1175   _dbus_string_free (&str);
1176   return NULL;
1177 }
1178
1179
1180 typedef enum 
1181 {
1182   STR_EQUAL,
1183   STR_PREFIX,
1184   STR_DIFFERENT
1185 } StrComparison;
1186
1187 /* Returns TRUE if container is a parent of child
1188  */
1189 static StrComparison
1190 path_contains (const char **container,
1191                const char **child)
1192 {
1193   int i;
1194
1195   i = 0;
1196   while (child[i] != NULL)
1197     {
1198       int v;
1199
1200       if (container[i] == NULL)
1201         return STR_PREFIX; /* container ran out, child continues;
1202                         * thus the container is a parent of the
1203                         * child.
1204                         */
1205
1206       _dbus_assert (container[i] != NULL);
1207       _dbus_assert (child[i] != NULL);
1208
1209       v = strcmp (container[i], child[i]);
1210
1211       if (v != 0)
1212         return STR_DIFFERENT; /* they overlap until here and then are different,
1213                            * not overlapping
1214                            */
1215
1216       ++i;
1217     }
1218
1219   /* Child ran out; if container also did, they are equal;
1220    * otherwise, the child is a parent of the container.
1221    */
1222   if (container[i] == NULL)
1223     return STR_EQUAL;
1224   else
1225     return STR_DIFFERENT;
1226 }
1227
1228 #if 0
1229 static void
1230 spew_subtree_recurse (DBusObjectSubtree *subtree,
1231                       int                indent)
1232 {
1233   int i;
1234
1235   i = 0;
1236   while (i < indent)
1237     {
1238       _dbus_verbose (" ");
1239       ++i;
1240     }
1241
1242   _dbus_verbose ("%s (%d children)\n",
1243                  subtree->name, subtree->n_subtrees);
1244
1245   i = 0;
1246   while (i < subtree->n_subtrees)
1247     {
1248       spew_subtree_recurse (subtree->subtrees[i], indent + 2);
1249
1250       ++i;
1251     }
1252 }
1253
1254 static void
1255 spew_tree (DBusObjectTree *tree)
1256 {
1257   spew_subtree_recurse (tree->root, 0);
1258 }
1259 #endif
1260
1261 /**
1262  * Callback data used in tests
1263  */
1264 typedef struct
1265 {
1266   const char **path; /**< Path */
1267   dbus_bool_t handler_fallback; /**< true if the handler may be called as fallback */
1268   dbus_bool_t message_handled; /**< Gets set to true if message handler called */
1269   dbus_bool_t handler_unregistered; /**< gets set to true if handler is unregistered */
1270 } TreeTestData;
1271
1272
1273 static void
1274 test_unregister_function (DBusConnection  *connection,
1275                           void            *user_data)
1276 {
1277   TreeTestData *ttd = user_data;
1278
1279   ttd->handler_unregistered = TRUE;
1280 }
1281
1282 static DBusHandlerResult
1283 test_message_function (DBusConnection  *connection,
1284                        DBusMessage     *message,
1285                        void            *user_data)
1286 {
1287   TreeTestData *ttd = user_data;
1288
1289   ttd->message_handled = TRUE;
1290
1291   return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1292 }
1293
1294 static dbus_bool_t
1295 do_register (DBusObjectTree *tree,
1296              const char    **path,
1297              dbus_bool_t     fallback,
1298              int             i,
1299              TreeTestData   *tree_test_data)
1300 {
1301   DBusObjectPathVTable vtable = { test_unregister_function,
1302                                   test_message_function, NULL };
1303   
1304   tree_test_data[i].message_handled = FALSE;
1305   tree_test_data[i].handler_unregistered = FALSE;
1306   tree_test_data[i].handler_fallback = fallback;
1307   tree_test_data[i].path = path;
1308
1309   if (!_dbus_object_tree_register (tree, fallback, path,
1310                                    &vtable,
1311                                    &tree_test_data[i]))
1312     return FALSE;
1313
1314   return TRUE;
1315 }
1316
1317 static dbus_bool_t
1318 do_test_dispatch (DBusObjectTree *tree,
1319                   const char    **path,
1320                   int             i,
1321                   TreeTestData   *tree_test_data,
1322                   int             n_test_data)
1323 {
1324   DBusMessage *message;
1325   int j;
1326   DBusHandlerResult result;
1327   char *flat;
1328
1329   message = NULL;
1330   
1331   flat = flatten_path (path);
1332   if (flat == NULL)
1333     goto oom;
1334
1335   message = dbus_message_new_method_call (NULL,
1336                                           flat,
1337                                           "org.freedesktop.TestInterface",
1338                                           "Foo");
1339   dbus_free (flat);
1340   if (message == NULL)
1341     goto oom;
1342
1343   j = 0;
1344   while (j < n_test_data)
1345     {
1346       tree_test_data[j].message_handled = FALSE;
1347       ++j;
1348     }
1349
1350   result = _dbus_object_tree_dispatch_and_unlock (tree, message);
1351   if (result == DBUS_HANDLER_RESULT_NEED_MEMORY)
1352     goto oom;
1353
1354   _dbus_assert (tree_test_data[i].message_handled);
1355
1356   j = 0;
1357   while (j < n_test_data)
1358     {
1359       if (tree_test_data[j].message_handled)
1360         {
1361           if (tree_test_data[j].handler_fallback)
1362             _dbus_assert (path_contains (tree_test_data[j].path,
1363                                          path) != STR_DIFFERENT);
1364           else
1365             _dbus_assert (path_contains (tree_test_data[j].path, path) == STR_EQUAL);
1366         }
1367       else
1368         {
1369           if (tree_test_data[j].handler_fallback)
1370             _dbus_assert (path_contains (tree_test_data[j].path,
1371                                          path) == STR_DIFFERENT);
1372           else
1373             _dbus_assert (path_contains (tree_test_data[j].path, path) != STR_EQUAL);
1374         }
1375
1376       ++j;
1377     }
1378
1379   dbus_message_unref (message);
1380
1381   return TRUE;
1382
1383  oom:
1384   if (message)
1385     dbus_message_unref (message);
1386   return FALSE;
1387 }
1388
1389 static size_t
1390 string_array_length (const char **array)
1391 {
1392   size_t i;
1393   for (i = 0; array[i]; i++) ;
1394   return i;
1395 }
1396
1397 typedef struct
1398 {
1399   const char *path;
1400   const char *result[20];
1401 } DecomposePathTest;
1402
1403 static DecomposePathTest decompose_tests[] = {
1404   { "/foo", { "foo", NULL } },
1405   { "/foo/bar", { "foo", "bar", NULL } },
1406   { "/", { NULL } },
1407   { "/a/b", { "a", "b", NULL } },
1408   { "/a/b/c", { "a", "b", "c", NULL } },
1409   { "/a/b/c/d", { "a", "b", "c", "d", NULL } },
1410   { "/foo/bar/q", { "foo", "bar", "q", NULL } },
1411   { "/foo/bar/this/is/longer", { "foo", "bar", "this", "is", "longer", NULL } }
1412 };
1413
1414 static dbus_bool_t
1415 run_decompose_tests (void)
1416 {
1417   int i;
1418
1419   i = 0;
1420   while (i < _DBUS_N_ELEMENTS (decompose_tests))
1421     {
1422       char **result;
1423       int    result_len;
1424       int    expected_len;
1425
1426       if (!_dbus_decompose_path (decompose_tests[i].path,
1427                                  strlen (decompose_tests[i].path),
1428                                  &result, &result_len))
1429         return FALSE;
1430
1431       expected_len = string_array_length (decompose_tests[i].result);
1432       
1433       if (result_len != (int) string_array_length ((const char**)result) ||
1434           expected_len != result_len ||
1435           path_contains (decompose_tests[i].result,
1436                          (const char**) result) != STR_EQUAL)
1437         {
1438           int real_len = string_array_length ((const char**)result);
1439           _dbus_warn ("Expected decompose of %s to have len %d, returned %d, appears to have %d\n",
1440                       decompose_tests[i].path, expected_len, result_len,
1441                       real_len);
1442           _dbus_warn ("Decompose resulted in elements: { ");
1443           i = 0;
1444           while (i < real_len)
1445             {
1446               _dbus_warn ("\"%s\"%s", result[i],
1447                           (i + 1) == real_len ? "" : ", ");
1448               ++i;
1449             }
1450           _dbus_warn ("}\n");
1451           _dbus_assert_not_reached ("path decompose failed\n");
1452         }
1453
1454       dbus_free_string_array (result);
1455
1456       ++i;
1457     }
1458   
1459   return TRUE;
1460 }
1461
1462 static dbus_bool_t
1463 object_tree_test_iteration (void *data)
1464 {
1465   const char *path0[] = { NULL };
1466   const char *path1[] = { "foo", NULL };
1467   const char *path2[] = { "foo", "bar", NULL };
1468   const char *path3[] = { "foo", "bar", "baz", NULL };
1469   const char *path4[] = { "foo", "bar", "boo", NULL };
1470   const char *path5[] = { "blah", NULL };
1471   const char *path6[] = { "blah", "boof", NULL };
1472   const char *path7[] = { "blah", "boof", "this", "is", "really", "long", NULL };
1473   const char *path8[] = { "childless", NULL };
1474   DBusObjectTree *tree;
1475   TreeTestData tree_test_data[9];
1476   int i;
1477   dbus_bool_t exact_match;
1478
1479   if (!run_decompose_tests ())
1480     return FALSE;
1481   
1482   tree = NULL;
1483
1484   tree = _dbus_object_tree_new (NULL);
1485   if (tree == NULL)
1486     goto out;
1487
1488   if (!do_register (tree, path0, TRUE, 0, tree_test_data))
1489     goto out;
1490
1491   _dbus_assert (find_subtree (tree, path0, NULL));
1492   _dbus_assert (!find_subtree (tree, path1, NULL));
1493   _dbus_assert (!find_subtree (tree, path2, NULL));
1494   _dbus_assert (!find_subtree (tree, path3, NULL));
1495   _dbus_assert (!find_subtree (tree, path4, NULL));
1496   _dbus_assert (!find_subtree (tree, path5, NULL));
1497   _dbus_assert (!find_subtree (tree, path6, NULL));
1498   _dbus_assert (!find_subtree (tree, path7, NULL));
1499   _dbus_assert (!find_subtree (tree, path8, NULL));
1500
1501   _dbus_assert (find_handler (tree, path0, &exact_match) && exact_match);
1502   _dbus_assert (find_handler (tree, path1, &exact_match) == tree->root && !exact_match);
1503   _dbus_assert (find_handler (tree, path2, &exact_match) == tree->root && !exact_match);
1504   _dbus_assert (find_handler (tree, path3, &exact_match) == tree->root && !exact_match);
1505   _dbus_assert (find_handler (tree, path4, &exact_match) == tree->root && !exact_match);
1506   _dbus_assert (find_handler (tree, path5, &exact_match) == tree->root && !exact_match);
1507   _dbus_assert (find_handler (tree, path6, &exact_match) == tree->root && !exact_match);
1508   _dbus_assert (find_handler (tree, path7, &exact_match) == tree->root && !exact_match);
1509   _dbus_assert (find_handler (tree, path8, &exact_match) == tree->root && !exact_match);
1510   
1511   if (!do_register (tree, path1, TRUE, 1, tree_test_data))
1512     goto out;
1513
1514   _dbus_assert (find_subtree (tree, path0, NULL));
1515   _dbus_assert (find_subtree (tree, path1, NULL));
1516   _dbus_assert (!find_subtree (tree, path2, NULL));
1517   _dbus_assert (!find_subtree (tree, path3, NULL));
1518   _dbus_assert (!find_subtree (tree, path4, NULL));
1519   _dbus_assert (!find_subtree (tree, path5, NULL));
1520   _dbus_assert (!find_subtree (tree, path6, NULL));
1521   _dbus_assert (!find_subtree (tree, path7, NULL));
1522   _dbus_assert (!find_subtree (tree, path8, NULL));
1523
1524   _dbus_assert (find_handler (tree, path0, &exact_match) &&  exact_match);
1525   _dbus_assert (find_handler (tree, path1, &exact_match) &&  exact_match);
1526   _dbus_assert (find_handler (tree, path2, &exact_match) && !exact_match);
1527   _dbus_assert (find_handler (tree, path3, &exact_match) && !exact_match);
1528   _dbus_assert (find_handler (tree, path4, &exact_match) && !exact_match);
1529   _dbus_assert (find_handler (tree, path5, &exact_match) == tree->root && !exact_match);
1530   _dbus_assert (find_handler (tree, path6, &exact_match) == tree->root && !exact_match);
1531   _dbus_assert (find_handler (tree, path7, &exact_match) == tree->root && !exact_match);
1532   _dbus_assert (find_handler (tree, path8, &exact_match) == tree->root && !exact_match);
1533
1534   if (!do_register (tree, path2, TRUE, 2, tree_test_data))
1535     goto out;
1536
1537   _dbus_assert (find_subtree (tree, path1, NULL));
1538   _dbus_assert (find_subtree (tree, path2, NULL));
1539   _dbus_assert (!find_subtree (tree, path3, NULL));
1540   _dbus_assert (!find_subtree (tree, path4, NULL));
1541   _dbus_assert (!find_subtree (tree, path5, NULL));
1542   _dbus_assert (!find_subtree (tree, path6, NULL));
1543   _dbus_assert (!find_subtree (tree, path7, NULL));
1544   _dbus_assert (!find_subtree (tree, path8, NULL));
1545
1546   if (!do_register (tree, path3, TRUE, 3, tree_test_data))
1547     goto out;
1548
1549   _dbus_assert (find_subtree (tree, path0, NULL));
1550   _dbus_assert (find_subtree (tree, path1, NULL));
1551   _dbus_assert (find_subtree (tree, path2, NULL));
1552   _dbus_assert (find_subtree (tree, path3, NULL));
1553   _dbus_assert (!find_subtree (tree, path4, NULL));
1554   _dbus_assert (!find_subtree (tree, path5, NULL));
1555   _dbus_assert (!find_subtree (tree, path6, NULL));
1556   _dbus_assert (!find_subtree (tree, path7, NULL));
1557   _dbus_assert (!find_subtree (tree, path8, NULL));
1558   
1559   if (!do_register (tree, path4, TRUE, 4, tree_test_data))
1560     goto out;
1561
1562   _dbus_assert (find_subtree (tree, path0, NULL));
1563   _dbus_assert (find_subtree (tree, path1, NULL));
1564   _dbus_assert (find_subtree (tree, path2, NULL));
1565   _dbus_assert (find_subtree (tree, path3, NULL));  
1566   _dbus_assert (find_subtree (tree, path4, NULL));
1567   _dbus_assert (!find_subtree (tree, path5, NULL));
1568   _dbus_assert (!find_subtree (tree, path6, NULL));
1569   _dbus_assert (!find_subtree (tree, path7, NULL));
1570   _dbus_assert (!find_subtree (tree, path8, NULL));
1571   
1572   if (!do_register (tree, path5, TRUE, 5, tree_test_data))
1573     goto out;
1574
1575   _dbus_assert (find_subtree (tree, path0, NULL));
1576   _dbus_assert (find_subtree (tree, path1, NULL));
1577   _dbus_assert (find_subtree (tree, path2, NULL));
1578   _dbus_assert (find_subtree (tree, path3, NULL));
1579   _dbus_assert (find_subtree (tree, path4, NULL));
1580   _dbus_assert (find_subtree (tree, path5, NULL));
1581   _dbus_assert (!find_subtree (tree, path6, NULL));
1582   _dbus_assert (!find_subtree (tree, path7, NULL));
1583   _dbus_assert (!find_subtree (tree, path8, NULL));
1584
1585   _dbus_assert (find_handler (tree, path0, &exact_match) == tree->root &&  exact_match);
1586   _dbus_assert (find_handler (tree, path1, &exact_match) != tree->root &&  exact_match);
1587   _dbus_assert (find_handler (tree, path2, &exact_match) != tree->root &&  exact_match);
1588   _dbus_assert (find_handler (tree, path3, &exact_match) != tree->root &&  exact_match);
1589   _dbus_assert (find_handler (tree, path4, &exact_match) != tree->root &&  exact_match);
1590   _dbus_assert (find_handler (tree, path5, &exact_match) != tree->root &&  exact_match);
1591   _dbus_assert (find_handler (tree, path6, &exact_match) != tree->root && !exact_match);
1592   _dbus_assert (find_handler (tree, path7, &exact_match) != tree->root && !exact_match);
1593   _dbus_assert (find_handler (tree, path8, &exact_match) == tree->root && !exact_match);
1594
1595   if (!do_register (tree, path6, TRUE, 6, tree_test_data))
1596     goto out;
1597
1598   _dbus_assert (find_subtree (tree, path0, NULL));
1599   _dbus_assert (find_subtree (tree, path1, NULL));
1600   _dbus_assert (find_subtree (tree, path2, NULL));
1601   _dbus_assert (find_subtree (tree, path3, NULL));
1602   _dbus_assert (find_subtree (tree, path4, NULL));
1603   _dbus_assert (find_subtree (tree, path5, NULL));
1604   _dbus_assert (find_subtree (tree, path6, NULL));
1605   _dbus_assert (!find_subtree (tree, path7, NULL));
1606   _dbus_assert (!find_subtree (tree, path8, NULL));
1607
1608   if (!do_register (tree, path7, TRUE, 7, tree_test_data))
1609     goto out;
1610
1611   _dbus_assert (find_subtree (tree, path0, NULL));
1612   _dbus_assert (find_subtree (tree, path1, NULL));
1613   _dbus_assert (find_subtree (tree, path2, NULL));
1614   _dbus_assert (find_subtree (tree, path3, NULL));
1615   _dbus_assert (find_subtree (tree, path4, NULL));
1616   _dbus_assert (find_subtree (tree, path5, NULL));
1617   _dbus_assert (find_subtree (tree, path6, NULL));
1618   _dbus_assert (find_subtree (tree, path7, NULL));
1619   _dbus_assert (!find_subtree (tree, path8, NULL));
1620
1621   if (!do_register (tree, path8, TRUE, 8, tree_test_data))
1622     goto out;
1623
1624   _dbus_assert (find_subtree (tree, path0, NULL));
1625   _dbus_assert (find_subtree (tree, path1, NULL));
1626   _dbus_assert (find_subtree (tree, path2, NULL));
1627   _dbus_assert (find_subtree (tree, path3, NULL));
1628   _dbus_assert (find_subtree (tree, path4, NULL));
1629   _dbus_assert (find_subtree (tree, path5, NULL));
1630   _dbus_assert (find_subtree (tree, path6, NULL));
1631   _dbus_assert (find_subtree (tree, path7, NULL));
1632   _dbus_assert (find_subtree (tree, path8, NULL));
1633
1634   _dbus_assert (find_handler (tree, path0, &exact_match) == tree->root &&  exact_match);
1635   _dbus_assert (find_handler (tree, path1, &exact_match) != tree->root && exact_match);
1636   _dbus_assert (find_handler (tree, path2, &exact_match) != tree->root && exact_match);
1637   _dbus_assert (find_handler (tree, path3, &exact_match) != tree->root && exact_match);
1638   _dbus_assert (find_handler (tree, path4, &exact_match) != tree->root && exact_match);
1639   _dbus_assert (find_handler (tree, path5, &exact_match) != tree->root && exact_match);
1640   _dbus_assert (find_handler (tree, path6, &exact_match) != tree->root && exact_match);
1641   _dbus_assert (find_handler (tree, path7, &exact_match) != tree->root && exact_match);
1642   _dbus_assert (find_handler (tree, path8, &exact_match) != tree->root && exact_match);
1643   
1644   /* test the list_registered function */
1645
1646   {
1647     const char *root[] = { NULL };
1648     char **child_entries;
1649     int nb;
1650
1651     _dbus_object_tree_list_registered_unlocked (tree, path1, &child_entries);
1652     if (child_entries != NULL)
1653       {
1654         nb = string_array_length ((const char**)child_entries);
1655         _dbus_assert (nb == 1);
1656         dbus_free_string_array (child_entries);
1657       }
1658
1659     _dbus_object_tree_list_registered_unlocked (tree, path2, &child_entries);
1660     if (child_entries != NULL)
1661       {
1662         nb = string_array_length ((const char**)child_entries);
1663         _dbus_assert (nb == 2);
1664         dbus_free_string_array (child_entries);
1665       }
1666
1667     _dbus_object_tree_list_registered_unlocked (tree, path8, &child_entries);
1668     if (child_entries != NULL)
1669       {
1670         nb = string_array_length ((const char**)child_entries);
1671         _dbus_assert (nb == 0);
1672         dbus_free_string_array (child_entries);
1673       }
1674
1675     _dbus_object_tree_list_registered_unlocked (tree, root, &child_entries);
1676     if (child_entries != NULL)
1677       {
1678         nb = string_array_length ((const char**)child_entries);
1679         _dbus_assert (nb == 3);
1680         dbus_free_string_array (child_entries);
1681       }
1682   }
1683
1684   /* Check that destroying tree calls unregister funcs */
1685   _dbus_object_tree_unref (tree);
1686
1687   i = 0;
1688   while (i < (int) _DBUS_N_ELEMENTS (tree_test_data))
1689     {
1690       _dbus_assert (tree_test_data[i].handler_unregistered);
1691       _dbus_assert (!tree_test_data[i].message_handled);
1692       ++i;
1693     }
1694
1695   /* Now start again and try the individual unregister function */
1696   tree = _dbus_object_tree_new (NULL);
1697   if (tree == NULL)
1698     goto out;
1699
1700   if (!do_register (tree, path0, TRUE, 0, tree_test_data))
1701     goto out;
1702   if (!do_register (tree, path1, TRUE, 1, tree_test_data))
1703     goto out;
1704   if (!do_register (tree, path2, TRUE, 2, tree_test_data))
1705     goto out;
1706   if (!do_register (tree, path3, TRUE, 3, tree_test_data))
1707     goto out;
1708   if (!do_register (tree, path4, TRUE, 4, tree_test_data))
1709     goto out;
1710   if (!do_register (tree, path5, TRUE, 5, tree_test_data))
1711     goto out;
1712   if (!do_register (tree, path6, TRUE, 6, tree_test_data))
1713     goto out;
1714   if (!do_register (tree, path7, TRUE, 7, tree_test_data))
1715     goto out;
1716   if (!do_register (tree, path8, TRUE, 8, tree_test_data))
1717     goto out;
1718
1719   _dbus_object_tree_unregister_and_unlock (tree, path0);
1720
1721   _dbus_assert (!find_subtree (tree, path0, NULL));
1722   _dbus_assert (find_subtree (tree, path1, NULL));
1723   _dbus_assert (find_subtree (tree, path2, NULL));
1724   _dbus_assert (find_subtree (tree, path3, NULL));
1725   _dbus_assert (find_subtree (tree, path4, NULL));
1726   _dbus_assert (find_subtree (tree, path5, NULL));
1727   _dbus_assert (find_subtree (tree, path6, NULL));
1728   _dbus_assert (find_subtree (tree, path7, NULL));
1729   _dbus_assert (find_subtree (tree, path8, NULL));
1730   
1731   _dbus_object_tree_unregister_and_unlock (tree, path1);
1732
1733   _dbus_assert (!find_subtree (tree, path0, NULL));
1734   _dbus_assert (!find_subtree (tree, path1, NULL));
1735   _dbus_assert (find_subtree (tree, path2, NULL));
1736   _dbus_assert (find_subtree (tree, path3, NULL));
1737   _dbus_assert (find_subtree (tree, path4, NULL));
1738   _dbus_assert (find_subtree (tree, path5, NULL));
1739   _dbus_assert (find_subtree (tree, path6, NULL));
1740   _dbus_assert (find_subtree (tree, path7, NULL));
1741   _dbus_assert (find_subtree (tree, path8, NULL));
1742
1743   _dbus_object_tree_unregister_and_unlock (tree, path2);
1744
1745   _dbus_assert (!find_subtree (tree, path0, NULL));
1746   _dbus_assert (!find_subtree (tree, path1, NULL));
1747   _dbus_assert (!find_subtree (tree, path2, NULL));
1748   _dbus_assert (find_subtree (tree, path3, NULL));
1749   _dbus_assert (find_subtree (tree, path4, NULL));
1750   _dbus_assert (find_subtree (tree, path5, NULL));
1751   _dbus_assert (find_subtree (tree, path6, NULL));
1752   _dbus_assert (find_subtree (tree, path7, NULL));
1753   _dbus_assert (find_subtree (tree, path8, NULL));
1754   
1755   _dbus_object_tree_unregister_and_unlock (tree, path3);
1756
1757   _dbus_assert (!find_subtree (tree, path0, NULL));
1758   _dbus_assert (!find_subtree (tree, path1, NULL));
1759   _dbus_assert (!find_subtree (tree, path2, NULL));
1760   _dbus_assert (!find_subtree (tree, path3, NULL));
1761   _dbus_assert (find_subtree (tree, path4, NULL));
1762   _dbus_assert (find_subtree (tree, path5, NULL));
1763   _dbus_assert (find_subtree (tree, path6, NULL));
1764   _dbus_assert (find_subtree (tree, path7, NULL));
1765   _dbus_assert (find_subtree (tree, path8, NULL));
1766   
1767   _dbus_object_tree_unregister_and_unlock (tree, path4);
1768
1769   _dbus_assert (!find_subtree (tree, path0, NULL));
1770   _dbus_assert (!find_subtree (tree, path1, NULL));
1771   _dbus_assert (!find_subtree (tree, path2, NULL));
1772   _dbus_assert (!find_subtree (tree, path3, NULL));
1773   _dbus_assert (!find_subtree (tree, path4, NULL));
1774   _dbus_assert (find_subtree (tree, path5, NULL));
1775   _dbus_assert (find_subtree (tree, path6, NULL));
1776   _dbus_assert (find_subtree (tree, path7, NULL));
1777   _dbus_assert (find_subtree (tree, path8, NULL));
1778   
1779   _dbus_object_tree_unregister_and_unlock (tree, path5);
1780
1781   _dbus_assert (!find_subtree (tree, path0, NULL));
1782   _dbus_assert (!find_subtree (tree, path1, NULL));
1783   _dbus_assert (!find_subtree (tree, path2, NULL));
1784   _dbus_assert (!find_subtree (tree, path3, NULL));
1785   _dbus_assert (!find_subtree (tree, path4, NULL));
1786   _dbus_assert (!find_subtree (tree, path5, NULL));
1787   _dbus_assert (find_subtree (tree, path6, NULL));
1788   _dbus_assert (find_subtree (tree, path7, NULL));
1789   _dbus_assert (find_subtree (tree, path8, NULL));
1790   
1791   _dbus_object_tree_unregister_and_unlock (tree, path6);
1792
1793   _dbus_assert (!find_subtree (tree, path0, NULL));
1794   _dbus_assert (!find_subtree (tree, path1, NULL));
1795   _dbus_assert (!find_subtree (tree, path2, NULL));
1796   _dbus_assert (!find_subtree (tree, path3, NULL));
1797   _dbus_assert (!find_subtree (tree, path4, NULL));
1798   _dbus_assert (!find_subtree (tree, path5, NULL));
1799   _dbus_assert (!find_subtree (tree, path6, NULL));
1800   _dbus_assert (find_subtree (tree, path7, NULL));
1801   _dbus_assert (find_subtree (tree, path8, NULL));
1802
1803   _dbus_object_tree_unregister_and_unlock (tree, path7);
1804
1805   _dbus_assert (!find_subtree (tree, path0, NULL));
1806   _dbus_assert (!find_subtree (tree, path1, NULL));
1807   _dbus_assert (!find_subtree (tree, path2, NULL));
1808   _dbus_assert (!find_subtree (tree, path3, NULL));
1809   _dbus_assert (!find_subtree (tree, path4, NULL));
1810   _dbus_assert (!find_subtree (tree, path5, NULL));
1811   _dbus_assert (!find_subtree (tree, path6, NULL));
1812   _dbus_assert (!find_subtree (tree, path7, NULL));
1813   _dbus_assert (find_subtree (tree, path8, NULL));
1814
1815   _dbus_object_tree_unregister_and_unlock (tree, path8);
1816
1817   _dbus_assert (!find_subtree (tree, path0, NULL));
1818   _dbus_assert (!find_subtree (tree, path1, NULL));
1819   _dbus_assert (!find_subtree (tree, path2, NULL));
1820   _dbus_assert (!find_subtree (tree, path3, NULL));
1821   _dbus_assert (!find_subtree (tree, path4, NULL));
1822   _dbus_assert (!find_subtree (tree, path5, NULL));
1823   _dbus_assert (!find_subtree (tree, path6, NULL));
1824   _dbus_assert (!find_subtree (tree, path7, NULL));
1825   _dbus_assert (!find_subtree (tree, path8, NULL));
1826   
1827   i = 0;
1828   while (i < (int) _DBUS_N_ELEMENTS (tree_test_data))
1829     {
1830       _dbus_assert (tree_test_data[i].handler_unregistered);
1831       _dbus_assert (!tree_test_data[i].message_handled);
1832       ++i;
1833     }
1834
1835   /* Register it all again, and test dispatch */
1836   
1837   if (!do_register (tree, path0, TRUE, 0, tree_test_data))
1838     goto out;
1839   if (!do_register (tree, path1, FALSE, 1, tree_test_data))
1840     goto out;
1841   if (!do_register (tree, path2, TRUE, 2, tree_test_data))
1842     goto out;
1843   if (!do_register (tree, path3, TRUE, 3, tree_test_data))
1844     goto out;
1845   if (!do_register (tree, path4, TRUE, 4, tree_test_data))
1846     goto out;
1847   if (!do_register (tree, path5, TRUE, 5, tree_test_data))
1848     goto out;
1849   if (!do_register (tree, path6, FALSE, 6, tree_test_data))
1850     goto out;
1851   if (!do_register (tree, path7, TRUE, 7, tree_test_data))
1852     goto out;
1853   if (!do_register (tree, path8, TRUE, 8, tree_test_data))
1854     goto out;
1855
1856 #if 0
1857   spew_tree (tree);
1858 #endif
1859
1860   if (!do_test_dispatch (tree, path0, 0, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1861     goto out;
1862   if (!do_test_dispatch (tree, path1, 1, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1863     goto out;
1864   if (!do_test_dispatch (tree, path2, 2, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1865     goto out;
1866   if (!do_test_dispatch (tree, path3, 3, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1867     goto out;
1868   if (!do_test_dispatch (tree, path4, 4, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1869     goto out;
1870   if (!do_test_dispatch (tree, path5, 5, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1871     goto out;
1872   if (!do_test_dispatch (tree, path6, 6, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1873     goto out;
1874   if (!do_test_dispatch (tree, path7, 7, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1875     goto out;
1876   if (!do_test_dispatch (tree, path8, 8, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1877     goto out;
1878   
1879  out:
1880   if (tree)
1881     {
1882       /* test ref */
1883       _dbus_object_tree_ref (tree);
1884       _dbus_object_tree_unref (tree);
1885       _dbus_object_tree_unref (tree);
1886     }
1887
1888   return TRUE;
1889 }
1890
1891 /**
1892  * @ingroup DBusObjectTree
1893  * Unit test for DBusObjectTree
1894  * @returns #TRUE on success.
1895  */
1896 dbus_bool_t
1897 _dbus_object_tree_test (void)
1898 {
1899   _dbus_test_oom_handling ("object tree",
1900                            object_tree_test_iteration,
1901                            NULL);
1902
1903   return TRUE;
1904 }
1905
1906 #endif /* DBUS_BUILD_TESTS */