Main Page | Modules | Data Structures | Directories | File List | Data Fields | Related Pages

dbus-object-tree.c

00001 /* -*- mode: C; c-file-style: "gnu" -*- */
00002 /* dbus-object-tree.c  DBusObjectTree (internals of DBusConnection)
00003  *
00004  * Copyright (C) 2003  Red Hat Inc.
00005  *
00006  * Licensed under the Academic Free License version 2.1
00007  *
00008  * This program is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * This program is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program; if not, write to the Free Software
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  *
00022  */
00023 #include "dbus-object-tree.h"
00024 #include "dbus-connection-internal.h"
00025 #include "dbus-internals.h"
00026 #include "dbus-hash.h"
00027 #include "dbus-protocol.h"
00028 #include "dbus-string.h"
00029 #include <string.h>
00030 #include <stdlib.h>
00031 
00044 typedef struct DBusObjectSubtree DBusObjectSubtree;
00045 
00046 static DBusObjectSubtree* _dbus_object_subtree_new   (const char                  *name,
00047                                                       const DBusObjectPathVTable  *vtable,
00048                                                       void                        *user_data);
00049 static DBusObjectSubtree* _dbus_object_subtree_ref   (DBusObjectSubtree           *subtree);
00050 static void               _dbus_object_subtree_unref (DBusObjectSubtree           *subtree);
00051 
00055 struct DBusObjectTree
00056 {
00057   int                 refcount;   
00058   DBusConnection     *connection; 
00060   DBusObjectSubtree  *root;       
00061 };
00062 
00068 struct DBusObjectSubtree
00069 {
00070   DBusAtomic                         refcount;            
00071   DBusObjectSubtree                 *parent;              
00072   DBusObjectPathUnregisterFunction   unregister_function; 
00073   DBusObjectPathMessageFunction      message_function;    
00074   void                              *user_data;           
00075   DBusObjectSubtree                **subtrees;            
00076   int                                n_subtrees;          
00077   unsigned int                       subtrees_sorted : 1; 
00078   unsigned int                       invoke_as_fallback : 1; 
00079   char                               name[1]; 
00080 };
00081 
00089 DBusObjectTree*
00090 _dbus_object_tree_new (DBusConnection *connection)
00091 {
00092   DBusObjectTree *tree;
00093 
00094   /* the connection passed in here isn't fully constructed,
00095    * so don't do anything more than store a pointer to
00096    * it
00097    */
00098 
00099   tree = dbus_new0 (DBusObjectTree, 1);
00100   if (tree == NULL)
00101     goto oom;
00102 
00103   tree->refcount = 1;
00104   tree->connection = connection;
00105   tree->root = _dbus_object_subtree_new ("/", NULL, NULL);
00106   if (tree->root == NULL)
00107     goto oom;
00108   tree->root->invoke_as_fallback = TRUE;
00109   
00110   return tree;
00111 
00112  oom:
00113   if (tree)
00114     {
00115       dbus_free (tree);
00116     }
00117 
00118   return NULL;
00119 }
00120 
00126 DBusObjectTree *
00127 _dbus_object_tree_ref (DBusObjectTree *tree)
00128 {
00129   _dbus_assert (tree->refcount > 0);
00130 
00131   tree->refcount += 1;
00132 
00133   return tree;
00134 }
00135 
00140 void
00141 _dbus_object_tree_unref (DBusObjectTree *tree)
00142 {
00143   _dbus_assert (tree->refcount > 0);
00144 
00145   tree->refcount -= 1;
00146 
00147   if (tree->refcount == 0)
00148     {
00149       _dbus_object_tree_free_all_unlocked (tree);
00150 
00151       dbus_free (tree);
00152     }
00153 }
00154 
00155 static int
00156 subtree_cmp (DBusObjectSubtree *subtree_a,
00157              DBusObjectSubtree *subtree_b)
00158 {
00159   return strcmp (subtree_a->name, subtree_b->name);
00160 }
00161 
00162 static int
00163 subtree_qsort_cmp (const void *a,
00164                    const void *b)
00165 {
00166   DBusObjectSubtree **subtree_a_p = (void*) a;
00167   DBusObjectSubtree **subtree_b_p = (void*) b;
00168 
00169   return subtree_cmp (*subtree_a_p, *subtree_b_p);
00170 }
00171 
00172 static void
00173 ensure_sorted (DBusObjectSubtree *subtree)
00174 {
00175   if (subtree->subtrees && !subtree->subtrees_sorted)
00176     {
00177       qsort (subtree->subtrees,
00178              subtree->n_subtrees,
00179              sizeof (DBusObjectSubtree*),
00180              subtree_qsort_cmp);
00181       subtree->subtrees_sorted = TRUE;
00182     }
00183 }
00184 
00188 #define VERBOSE_FIND 0
00189 
00190 static DBusObjectSubtree*
00191 find_subtree_recurse (DBusObjectSubtree  *subtree,
00192                       const char        **path,
00193                       dbus_bool_t         create_if_not_found,
00194                       int                *index_in_parent,
00195                       dbus_bool_t        *exact_match)
00196 {
00197   int i;
00198   dbus_bool_t return_deepest_match;
00199 
00200   return_deepest_match = exact_match != NULL;
00201 
00202   _dbus_assert (!(return_deepest_match && create_if_not_found));
00203 
00204   if (path[0] == NULL)
00205     {
00206 #if VERBOSE_FIND
00207       _dbus_verbose ("  path exhausted, returning %s\n",
00208                      subtree->name);
00209 #endif
00210       if (exact_match != NULL)
00211         *exact_match = TRUE;
00212       return subtree;
00213     }
00214 
00215 #if VERBOSE_FIND
00216   _dbus_verbose ("  searching children of %s for %s\n",
00217                  subtree->name, path[0]);
00218 #endif
00219   
00220   ensure_sorted (subtree);
00221 
00222   /* FIXME we should do a binary search here instead
00223    * of O(n)
00224    */
00225 
00226   i = 0;
00227   while (i < subtree->n_subtrees)
00228     {
00229       int v;
00230 
00231       v = strcmp (path[0], subtree->subtrees[i]->name);
00232 
00233 #if VERBOSE_FIND
00234       _dbus_verbose ("  %s cmp %s = %d\n",
00235                      path[0], subtree->subtrees[i]->name,
00236                      v);
00237 #endif
00238       
00239       if (v == 0)
00240         {
00241           if (index_in_parent)
00242             {
00243 #if VERBOSE_FIND
00244               _dbus_verbose ("  storing parent index %d\n", i);
00245 #endif
00246               *index_in_parent = i;
00247             }
00248 
00249           if (return_deepest_match)
00250             {
00251               DBusObjectSubtree *next;
00252 
00253               next = find_subtree_recurse (subtree->subtrees[i],
00254                                            &path[1], create_if_not_found, 
00255                                            index_in_parent, exact_match);
00256               if (next == NULL &&
00257                   subtree->invoke_as_fallback)
00258                 {
00259 #if VERBOSE_FIND
00260                   _dbus_verbose ("  no deeper match found, returning %s\n",
00261                                  subtree->name);
00262 #endif
00263                   if (exact_match != NULL)
00264                     *exact_match = FALSE;
00265                   return subtree;
00266                 }
00267               else
00268                 return next;
00269             }
00270           else
00271             return find_subtree_recurse (subtree->subtrees[i],
00272                                          &path[1], create_if_not_found, 
00273                                          index_in_parent, exact_match);
00274         }
00275       else if (v < 0)
00276         {
00277           goto not_found;
00278         }
00279 
00280       ++i;
00281     }
00282 
00283  not_found:
00284 #if VERBOSE_FIND
00285   _dbus_verbose ("  no match found, current tree %s, create_if_not_found = %d\n",
00286                  subtree->name, create_if_not_found);
00287 #endif
00288   
00289   if (create_if_not_found)
00290     {
00291       DBusObjectSubtree* child;
00292       DBusObjectSubtree **new_subtrees;
00293       int new_n_subtrees;
00294 
00295 #if VERBOSE_FIND
00296       _dbus_verbose ("  creating subtree %s\n",
00297                      path[0]);
00298 #endif
00299       
00300       child = _dbus_object_subtree_new (path[0],
00301                                         NULL, NULL);
00302       if (child == NULL)
00303         return NULL;
00304 
00305       /* FIXME we should do the "double alloc each time" standard thing */
00306       new_n_subtrees = subtree->n_subtrees + 1;
00307       new_subtrees = dbus_realloc (subtree->subtrees,
00308                                    new_n_subtrees * sizeof (DBusObjectSubtree*));
00309       if (new_subtrees == NULL)
00310         {
00311           child->unregister_function = NULL;
00312           child->message_function = NULL;
00313           _dbus_object_subtree_unref (child);
00314           return NULL;
00315         }
00316 
00317       new_subtrees[subtree->n_subtrees] = child;
00318       if (index_in_parent)
00319         *index_in_parent = subtree->n_subtrees;
00320       subtree->subtrees_sorted = FALSE;
00321       subtree->n_subtrees = new_n_subtrees;
00322       subtree->subtrees = new_subtrees;
00323 
00324       child->parent = subtree;
00325 
00326       return find_subtree_recurse (child,
00327                                    &path[1], create_if_not_found, 
00328                                    index_in_parent, exact_match);
00329     }
00330   else
00331     {
00332       if (exact_match != NULL)
00333         *exact_match = FALSE;
00334       return (return_deepest_match && subtree->invoke_as_fallback) ? subtree : NULL;
00335     }
00336 }
00337 
00338 static DBusObjectSubtree*
00339 find_subtree (DBusObjectTree *tree,
00340               const char    **path,
00341               int            *index_in_parent)
00342 {
00343   DBusObjectSubtree *subtree;
00344 
00345 #if VERBOSE_FIND
00346   _dbus_verbose ("Looking for exact registered subtree\n");
00347 #endif
00348   
00349   subtree = find_subtree_recurse (tree->root, path, FALSE, index_in_parent, NULL);
00350 
00351   if (subtree && subtree->message_function == NULL)
00352     return NULL;
00353   else
00354     return subtree;
00355 }
00356 
00357 static DBusObjectSubtree*
00358 lookup_subtree (DBusObjectTree *tree,
00359                 const char    **path)
00360 {
00361 #if VERBOSE_FIND
00362   _dbus_verbose ("Looking for subtree\n");
00363 #endif
00364   return find_subtree_recurse (tree->root, path, FALSE, NULL, NULL);
00365 }
00366 
00367 static DBusObjectSubtree*
00368 find_handler (DBusObjectTree *tree,
00369               const char    **path,
00370               dbus_bool_t    *exact_match)
00371 {
00372 #if VERBOSE_FIND
00373   _dbus_verbose ("Looking for deepest handler\n");
00374 #endif
00375   _dbus_assert (exact_match != NULL);
00376   return find_subtree_recurse (tree->root, path, FALSE, NULL, exact_match);
00377 }
00378 
00379 static DBusObjectSubtree*
00380 ensure_subtree (DBusObjectTree *tree,
00381                 const char    **path)
00382 {
00383 #if VERBOSE_FIND
00384   _dbus_verbose ("Ensuring subtree\n");
00385 #endif
00386   return find_subtree_recurse (tree->root, path, TRUE, NULL, NULL);
00387 }
00388 
00399 dbus_bool_t
00400 _dbus_object_tree_register (DBusObjectTree              *tree,
00401                             dbus_bool_t                  fallback,
00402                             const char                 **path,
00403                             const DBusObjectPathVTable  *vtable,
00404                             void                        *user_data)
00405 {
00406   DBusObjectSubtree  *subtree;
00407 
00408   _dbus_assert (tree != NULL);
00409   _dbus_assert (vtable->message_function != NULL);
00410   _dbus_assert (path != NULL);
00411 
00412   subtree = ensure_subtree (tree, path);
00413   if (subtree == NULL)
00414     return FALSE;
00415 
00416 #ifndef DBUS_DISABLE_CHECKS
00417   if (subtree->message_function != NULL)
00418     {
00419       _dbus_warn ("A handler is already registered for the path starting with path[0] = \"%s\"\n",
00420                   path[0] ? path[0] : "null");
00421       return FALSE;
00422     }
00423 #else
00424   _dbus_assert (subtree->message_function == NULL);
00425 #endif
00426 
00427   subtree->message_function = vtable->message_function;
00428   subtree->unregister_function = vtable->unregister_function;
00429   subtree->user_data = user_data;
00430   subtree->invoke_as_fallback = fallback != FALSE;
00431   
00432   return TRUE;
00433 }
00434 
00442 void
00443 _dbus_object_tree_unregister_and_unlock (DBusObjectTree          *tree,
00444                                          const char             **path)
00445 {
00446   int i;
00447   DBusObjectSubtree *subtree;
00448   DBusObjectPathUnregisterFunction unregister_function;
00449   void *user_data;
00450   DBusConnection *connection;
00451 
00452   _dbus_assert (path != NULL);
00453 
00454   unregister_function = NULL;
00455   user_data = NULL;
00456 
00457   subtree = find_subtree (tree, path, &i);
00458 
00459 #ifndef DBUS_DISABLE_CHECKS
00460   if (subtree == NULL)
00461     {
00462       _dbus_warn ("Attempted to unregister path (path[0] = %s path[1] = %s) which isn't registered\n",
00463                   path[0] ? path[0] : "null",
00464                   path[1] ? path[1] : "null");
00465       goto unlock;
00466     }
00467 #else
00468   _dbus_assert (subtree != NULL);
00469 #endif
00470 
00471   _dbus_assert (subtree->parent == NULL ||
00472                 (i >= 0 && subtree->parent->subtrees[i] == subtree));
00473 
00474   subtree->message_function = NULL;
00475 
00476   unregister_function = subtree->unregister_function;
00477   user_data = subtree->user_data;
00478 
00479   subtree->unregister_function = NULL;
00480   subtree->user_data = NULL;
00481 
00482   /* If we have no subtrees of our own, remove from
00483    * our parent (FIXME could also be more aggressive
00484    * and remove our parent if it becomes empty)
00485    */
00486   if (subtree->parent && subtree->n_subtrees == 0)
00487     {
00488       /* assumes a 0-byte memmove is OK */
00489       memmove (&subtree->parent->subtrees[i],
00490                &subtree->parent->subtrees[i+1],
00491                (subtree->parent->n_subtrees - i - 1) *
00492                sizeof (subtree->parent->subtrees[0]));
00493       subtree->parent->n_subtrees -= 1;
00494 
00495       subtree->parent = NULL;
00496 
00497       _dbus_object_subtree_unref (subtree);
00498     }
00499   subtree = NULL;
00500 
00501 unlock:
00502   connection = tree->connection;
00503 
00504   /* Unlock and call application code */
00505 #ifdef DBUS_BUILD_TESTS
00506   if (connection)
00507 #endif
00508     {
00509       _dbus_connection_ref_unlocked (connection);
00510       _dbus_connection_unlock (connection);
00511     }
00512 
00513   if (unregister_function)
00514     (* unregister_function) (connection, user_data);
00515 
00516 #ifdef DBUS_BUILD_TESTS
00517   if (connection)
00518 #endif
00519     dbus_connection_unref (connection);
00520 }
00521 
00522 static void
00523 free_subtree_recurse (DBusConnection    *connection,
00524                       DBusObjectSubtree *subtree)
00525 {
00526   /* Delete them from the end, for slightly
00527    * more robustness against odd reentrancy.
00528    */
00529   while (subtree->n_subtrees > 0)
00530     {
00531       DBusObjectSubtree *child;
00532 
00533       child = subtree->subtrees[subtree->n_subtrees - 1];
00534       subtree->subtrees[subtree->n_subtrees - 1] = NULL;
00535       subtree->n_subtrees -= 1;
00536       child->parent = NULL;
00537 
00538       free_subtree_recurse (connection, child);
00539     }
00540 
00541   /* Call application code */
00542   if (subtree->unregister_function)
00543     (* subtree->unregister_function) (connection,
00544                                       subtree->user_data);
00545 
00546   subtree->message_function = NULL;
00547   subtree->unregister_function = NULL;
00548   subtree->user_data = NULL;
00549 
00550   /* Now free ourselves */
00551   _dbus_object_subtree_unref (subtree);
00552 }
00553 
00560 void
00561 _dbus_object_tree_free_all_unlocked (DBusObjectTree *tree)
00562 {
00563   if (tree->root)
00564     free_subtree_recurse (tree->connection,
00565                           tree->root);
00566   tree->root = NULL;
00567 }
00568 
00569 static dbus_bool_t
00570 _dbus_object_tree_list_registered_unlocked (DBusObjectTree *tree,
00571                                             const char    **parent_path,
00572                                             char         ***child_entries)
00573 {
00574   DBusObjectSubtree *subtree;
00575   char **retval;
00576   
00577   _dbus_assert (parent_path != NULL);
00578   _dbus_assert (child_entries != NULL);
00579 
00580   *child_entries = NULL;
00581   
00582   subtree = lookup_subtree (tree, parent_path);
00583   if (subtree == NULL)
00584     {
00585       retval = dbus_new0 (char *, 1);
00586     }
00587   else
00588     {
00589       int i;
00590       retval = dbus_new0 (char*, subtree->n_subtrees + 1);
00591       if (retval == NULL)
00592         goto out;
00593       i = 0;
00594       while (i < subtree->n_subtrees)
00595         {
00596           retval[i] = _dbus_strdup (subtree->subtrees[i]->name);
00597           if (retval[i] == NULL)
00598             {
00599               dbus_free_string_array (retval);
00600               retval = NULL;
00601               goto out;
00602             }
00603           ++i;
00604         }
00605     }
00606 
00607  out:
00608     
00609   *child_entries = retval;
00610   return retval != NULL;
00611 }
00612 
00613 static DBusHandlerResult
00614 handle_default_introspect_unlocked (DBusObjectTree          *tree,
00615                                     DBusMessage             *message,
00616                                     const char             **path)
00617 {
00618   DBusString xml;
00619   DBusHandlerResult result;
00620   char **children;
00621   int i;
00622 
00623   if (!dbus_message_is_method_call (message,
00624                                     DBUS_INTERFACE_ORG_FREEDESKTOP_INTROSPECTABLE,
00625                                     "Introspect"))
00626     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
00627   
00628   if (!_dbus_string_init (&xml))
00629     return DBUS_HANDLER_RESULT_NEED_MEMORY;
00630 
00631   result = DBUS_HANDLER_RESULT_NEED_MEMORY;
00632 
00633   children = NULL;
00634   if (!_dbus_object_tree_list_registered_unlocked (tree, path, &children))
00635     goto out;
00636 
00637   if (!_dbus_string_append (&xml, "<node>\n"))
00638     goto out;
00639 
00640   i = 0;
00641   while (children[i] != NULL)
00642     {
00643       if (!_dbus_string_append_printf (&xml, "  <node name=\"%s\"/>\n",
00644                                        children[i]))
00645         goto out;
00646 
00647       ++i;
00648     }
00649 
00650   if (!_dbus_string_append (&xml, "</node>\n"))
00651     goto out;
00652   
00653   result = DBUS_HANDLER_RESULT_HANDLED;
00654   
00655  out:
00656   _dbus_string_free (&xml);
00657   dbus_free_string_array (children);
00658   
00659   return result;
00660 }
00661 
00675 DBusHandlerResult
00676 _dbus_object_tree_dispatch_and_unlock (DBusObjectTree          *tree,
00677                                        DBusMessage             *message)
00678 {
00679   char **path;
00680   dbus_bool_t exact_match;
00681   DBusList *list;
00682   DBusList *link;
00683   DBusHandlerResult result;
00684   DBusObjectSubtree *subtree;
00685   
00686 #if 0
00687   _dbus_verbose ("Dispatch of message by object path\n");
00688 #endif
00689   
00690   path = NULL;
00691   if (!dbus_message_get_path_decomposed (message, &path))
00692     {
00693 #ifdef DBUS_BUILD_TESTS
00694       if (tree->connection)
00695 #endif
00696         _dbus_connection_unlock (tree->connection);
00697       
00698       _dbus_verbose ("No memory to get decomposed path\n");
00699 
00700       return DBUS_HANDLER_RESULT_NEED_MEMORY;
00701     }
00702 
00703   if (path == NULL)
00704     {
00705 #ifdef DBUS_BUILD_TESTS
00706       if (tree->connection)
00707 #endif
00708         _dbus_connection_unlock (tree->connection);
00709       
00710       _dbus_verbose ("No path field in message\n");
00711       return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
00712     }
00713   
00714   /* Find the deepest path that covers the path in the message */
00715   subtree = find_handler (tree, (const char**) path, &exact_match);
00716   
00717   /* Build a list of all paths that cover the path in the message */
00718 
00719   list = NULL;
00720 
00721   while (subtree != NULL)
00722     {
00723       if (subtree->message_function != NULL && (exact_match || subtree->invoke_as_fallback))
00724         {
00725           _dbus_object_subtree_ref (subtree);
00726 
00727           /* run deepest paths first */
00728           if (!_dbus_list_append (&list, subtree))
00729             {
00730               result = DBUS_HANDLER_RESULT_NEED_MEMORY;
00731               _dbus_object_subtree_unref (subtree);
00732               goto free_and_return;
00733             }
00734         }
00735 
00736       exact_match = FALSE;
00737       subtree = subtree->parent;
00738     }
00739 
00740   _dbus_verbose ("%d handlers in the path tree for this message\n",
00741                  _dbus_list_get_length (&list));
00742 
00743   /* Invoke each handler in the list */
00744 
00745   result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
00746 
00747   link = _dbus_list_get_first_link (&list);
00748   while (link != NULL)
00749     {
00750       DBusList *next = _dbus_list_get_next_link (&list, link);
00751       subtree = link->data;
00752 
00753       /* message_function is NULL if we're unregistered
00754        * due to reentrancy
00755        */
00756       if (subtree->message_function)
00757         {
00758           DBusObjectPathMessageFunction message_function;
00759           void *user_data;
00760 
00761           message_function = subtree->message_function;
00762           user_data = subtree->user_data;
00763 
00764 #if 0
00765           _dbus_verbose ("  (invoking a handler)\n");
00766 #endif
00767           
00768 #ifdef DBUS_BUILD_TESTS
00769           if (tree->connection)
00770 #endif
00771             _dbus_connection_unlock (tree->connection);
00772 
00773           /* FIXME you could unregister the subtree in another thread
00774            * before we invoke the callback, and I can't figure out a
00775            * good way to solve this.
00776            */
00777 
00778           result = (* message_function) (tree->connection,
00779                                          message,
00780                                          user_data);
00781 
00782 #ifdef DBUS_BUILD_TESTS
00783           if (tree->connection)
00784 #endif
00785             _dbus_connection_lock (tree->connection);
00786 
00787           if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
00788             goto free_and_return;
00789         }
00790 
00791       link = next;
00792     }
00793 
00794  free_and_return:
00795 
00796   if (result == DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
00797     {
00798       /* This hardcoded default handler does a minimal Introspect()
00799        */
00800       result = handle_default_introspect_unlocked (tree, message,
00801                                                    (const char**) path);
00802     }
00803 
00804 #ifdef DBUS_BUILD_TESTS
00805   if (tree->connection)
00806 #endif
00807     _dbus_connection_unlock (tree->connection);
00808   
00809   while (list != NULL)
00810     {
00811       link = _dbus_list_get_first_link (&list);
00812       _dbus_object_subtree_unref (link->data);
00813       _dbus_list_remove_link (&list, link);
00814     }
00815   
00816   dbus_free_string_array (path);
00817 
00818   return result;
00819 }
00820 
00827 static DBusObjectSubtree*
00828 allocate_subtree_object (const char *name)
00829 {
00830   int len;
00831   DBusObjectSubtree *subtree;
00832   const size_t front_padding = _DBUS_STRUCT_OFFSET (DBusObjectSubtree, name);
00833 
00834   _dbus_assert (name != NULL);
00835 
00836   len = strlen (name);
00837 
00838   subtree = dbus_malloc (front_padding + (len + 1));
00839 
00840   if (subtree == NULL)
00841     return NULL;
00842 
00843   memcpy (subtree->name, name, len + 1);
00844 
00845   return subtree;
00846 }
00847 
00848 static DBusObjectSubtree*
00849 _dbus_object_subtree_new (const char                  *name,
00850                           const DBusObjectPathVTable  *vtable,
00851                           void                        *user_data)
00852 {
00853   DBusObjectSubtree *subtree;
00854 
00855   subtree = allocate_subtree_object (name);
00856   if (subtree == NULL)
00857     goto oom;
00858 
00859   _dbus_assert (name != NULL);
00860 
00861   subtree->parent = NULL;
00862 
00863   if (vtable)
00864     {
00865       subtree->message_function = vtable->message_function;
00866       subtree->unregister_function = vtable->unregister_function;
00867     }
00868   else
00869     {
00870       subtree->message_function = NULL;
00871       subtree->unregister_function = NULL;
00872     }
00873 
00874   subtree->user_data = user_data;
00875   subtree->refcount.value = 1;
00876   subtree->subtrees = NULL;
00877   subtree->n_subtrees = 0;
00878   subtree->subtrees_sorted = TRUE;
00879   subtree->invoke_as_fallback = FALSE;
00880 
00881   return subtree;
00882 
00883  oom:
00884   if (subtree)
00885     {
00886       dbus_free (subtree);
00887     }
00888 
00889   return NULL;
00890 }
00891 
00892 static DBusObjectSubtree *
00893 _dbus_object_subtree_ref (DBusObjectSubtree *subtree)
00894 {
00895   _dbus_assert (subtree->refcount.value > 0);
00896   _dbus_atomic_inc (&subtree->refcount);
00897 
00898   return subtree;
00899 }
00900 
00901 static void
00902 _dbus_object_subtree_unref (DBusObjectSubtree *subtree)
00903 {
00904   _dbus_assert (subtree->refcount.value > 0);
00905 
00906   if (_dbus_atomic_dec (&subtree->refcount) == 1)
00907     {
00908       _dbus_assert (subtree->unregister_function == NULL);
00909       _dbus_assert (subtree->message_function == NULL);
00910 
00911       dbus_free (subtree->subtrees);
00912       dbus_free (subtree);
00913     }
00914 }
00915 
00926 dbus_bool_t
00927 _dbus_object_tree_list_registered_and_unlock (DBusObjectTree *tree,
00928                                               const char    **parent_path,
00929                                               char         ***child_entries)
00930 {
00931   dbus_bool_t result;
00932 
00933   result = _dbus_object_tree_list_registered_unlocked (tree,
00934                                                        parent_path,
00935                                                        child_entries);
00936   
00937 #ifdef DBUS_BUILD_TESTS
00938   if (tree->connection)
00939 #endif
00940     _dbus_connection_unlock (tree->connection);
00941 
00942   return result;
00943 }
00944      
00947 #ifdef DBUS_BUILD_TESTS
00948 #include "dbus-test.h"
00949 #include <stdio.h>
00950 
00951 static char*
00952 flatten_path (const char **path)
00953 {
00954   DBusString str;
00955   int i;
00956   char *s;
00957 
00958   if (!_dbus_string_init (&str))
00959     return NULL;
00960 
00961   i = 0;
00962   while (path[i])
00963     {
00964       if (!_dbus_string_append_byte (&str, '/'))
00965         goto nomem;
00966 
00967       if (!_dbus_string_append (&str, path[i]))
00968         goto nomem;
00969 
00970       ++i;
00971     }
00972 
00973   if (!_dbus_string_steal_data (&str, &s))
00974     goto nomem;
00975 
00976   _dbus_string_free (&str);
00977 
00978   return s;
00979 
00980  nomem:
00981   _dbus_string_free (&str);
00982   return NULL;
00983 }
00984 
00985 
00986 typedef enum 
00987 {
00988   STR_EQUAL,
00989   STR_PREFIX,
00990   STR_DIFFERENT
00991 } StrComparison;
00992 
00993 /* Returns TRUE if container is a parent of child
00994  */
00995 static StrComparison
00996 path_contains (const char **container,
00997                const char **child)
00998 {
00999   int i;
01000 
01001   i = 0;
01002   while (child[i] != NULL)
01003     {
01004       int v;
01005 
01006       if (container[i] == NULL)
01007         return STR_PREFIX; /* container ran out, child continues;
01008                         * thus the container is a parent of the
01009                         * child.
01010                         */
01011 
01012       _dbus_assert (container[i] != NULL);
01013       _dbus_assert (child[i] != NULL);
01014 
01015       v = strcmp (container[i], child[i]);
01016 
01017       if (v != 0)
01018         return STR_DIFFERENT; /* they overlap until here and then are different,
01019                            * not overlapping
01020                            */
01021 
01022       ++i;
01023     }
01024 
01025   /* Child ran out; if container also did, they are equal;
01026    * otherwise, the child is a parent of the container.
01027    */
01028   if (container[i] == NULL)
01029     return STR_EQUAL;
01030   else
01031     return STR_DIFFERENT;
01032 }
01033 
01034 #if 0
01035 static void
01036 spew_subtree_recurse (DBusObjectSubtree *subtree,
01037                       int                indent)
01038 {
01039   int i;
01040 
01041   i = 0;
01042   while (i < indent)
01043     {
01044       _dbus_verbose (" ");
01045       ++i;
01046     }
01047 
01048   _dbus_verbose ("%s (%d children)\n",
01049                  subtree->name, subtree->n_subtrees);
01050 
01051   i = 0;
01052   while (i < subtree->n_subtrees)
01053     {
01054       spew_subtree_recurse (subtree->subtrees[i], indent + 2);
01055 
01056       ++i;
01057     }
01058 }
01059 
01060 static void
01061 spew_tree (DBusObjectTree *tree)
01062 {
01063   spew_subtree_recurse (tree->root, 0);
01064 }
01065 #endif
01066 
01070 typedef struct
01071 {
01072   const char **path; 
01073   dbus_bool_t handler_fallback; 
01074   dbus_bool_t message_handled; 
01075   dbus_bool_t handler_unregistered; 
01076 } TreeTestData;
01077 
01078 
01079 static void
01080 test_unregister_function (DBusConnection  *connection,
01081                           void            *user_data)
01082 {
01083   TreeTestData *ttd = user_data;
01084 
01085   ttd->handler_unregistered = TRUE;
01086 }
01087 
01088 static DBusHandlerResult
01089 test_message_function (DBusConnection  *connection,
01090                        DBusMessage     *message,
01091                        void            *user_data)
01092 {
01093   TreeTestData *ttd = user_data;
01094 
01095   ttd->message_handled = TRUE;
01096 
01097   return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
01098 }
01099 
01100 static dbus_bool_t
01101 do_register (DBusObjectTree *tree,
01102              const char    **path,
01103              dbus_bool_t     fallback,
01104              int             i,
01105              TreeTestData   *tree_test_data)
01106 {
01107   DBusObjectPathVTable vtable = { test_unregister_function,
01108                                   test_message_function, NULL };
01109 
01110   tree_test_data[i].message_handled = FALSE;
01111   tree_test_data[i].handler_unregistered = FALSE;
01112   tree_test_data[i].handler_fallback = fallback;
01113   tree_test_data[i].path = path;
01114 
01115   if (!_dbus_object_tree_register (tree, fallback, path,
01116                                    &vtable,
01117                                    &tree_test_data[i]))
01118     return FALSE;
01119 
01120   return TRUE;
01121 }
01122 
01123 static dbus_bool_t
01124 do_test_dispatch (DBusObjectTree *tree,
01125                   const char    **path,
01126                   int             i,
01127                   TreeTestData   *tree_test_data,
01128                   int             n_test_data)
01129 {
01130   DBusMessage *message;
01131   int j;
01132   DBusHandlerResult result;
01133   char *flat;
01134 
01135   message = NULL;
01136   
01137   flat = flatten_path (path);
01138   if (flat == NULL)
01139     goto oom;
01140 
01141   message = dbus_message_new_method_call (NULL,
01142                                           flat,
01143                                           "org.freedesktop.TestInterface",
01144                                           "Foo");
01145   dbus_free (flat);
01146   if (message == NULL)
01147     goto oom;
01148 
01149   j = 0;
01150   while (j < n_test_data)
01151     {
01152       tree_test_data[j].message_handled = FALSE;
01153       ++j;
01154     }
01155 
01156   result = _dbus_object_tree_dispatch_and_unlock (tree, message);
01157   if (result == DBUS_HANDLER_RESULT_NEED_MEMORY)
01158     goto oom;
01159 
01160   _dbus_assert (tree_test_data[i].message_handled);
01161 
01162   j = 0;
01163   while (j < n_test_data)
01164     {
01165       if (tree_test_data[j].message_handled)
01166         {
01167           if (tree_test_data[j].handler_fallback)
01168             _dbus_assert (path_contains (tree_test_data[j].path,
01169                                          path) != STR_DIFFERENT);
01170           else
01171             _dbus_assert (path_contains (tree_test_data[j].path, path) == STR_EQUAL);
01172         }
01173       else
01174         {
01175           if (tree_test_data[j].handler_fallback)
01176             _dbus_assert (path_contains (tree_test_data[j].path,
01177                                          path) == STR_DIFFERENT);
01178           else
01179             _dbus_assert (path_contains (tree_test_data[j].path, path) != STR_EQUAL);
01180         }
01181 
01182       ++j;
01183     }
01184 
01185   dbus_message_unref (message);
01186 
01187   return TRUE;
01188 
01189  oom:
01190   if (message)
01191     dbus_message_unref (message);
01192   return FALSE;
01193 }
01194 
01195 static size_t
01196 string_array_length (char **array)
01197 {
01198   size_t i;
01199   for (i = 0; array[i]; i++) ;
01200   return i;
01201 }
01202 
01203 
01204 static dbus_bool_t
01205 object_tree_test_iteration (void *data)
01206 {
01207   const char *path1[] = { "foo", NULL };
01208   const char *path2[] = { "foo", "bar", NULL };
01209   const char *path3[] = { "foo", "bar", "baz", NULL };
01210   const char *path4[] = { "foo", "bar", "boo", NULL };
01211   const char *path5[] = { "blah", NULL };
01212   const char *path6[] = { "blah", "boof", NULL };
01213   const char *path7[] = { "blah", "boof", "this", "is", "really", "long", NULL };
01214   const char *path8[] = { "childless", NULL };
01215   DBusObjectTree *tree;
01216   TreeTestData tree_test_data[8];
01217   int i;
01218   dbus_bool_t exact_match;
01219 
01220   tree = NULL;
01221 
01222   tree = _dbus_object_tree_new (NULL);
01223   if (tree == NULL)
01224     goto out;
01225 
01226   if (!do_register (tree, path1, TRUE, 0, tree_test_data))
01227     goto out;
01228 
01229   _dbus_assert (find_subtree (tree, path1, NULL));
01230   _dbus_assert (!find_subtree (tree, path2, NULL));
01231   _dbus_assert (!find_subtree (tree, path3, NULL));
01232   _dbus_assert (!find_subtree (tree, path4, NULL));
01233   _dbus_assert (!find_subtree (tree, path5, NULL));
01234   _dbus_assert (!find_subtree (tree, path6, NULL));
01235   _dbus_assert (!find_subtree (tree, path7, NULL));
01236   _dbus_assert (!find_subtree (tree, path8, NULL));
01237 
01238   _dbus_assert (find_handler (tree, path1, &exact_match) &&  exact_match);
01239   _dbus_assert (find_handler (tree, path2, &exact_match) && !exact_match);
01240   _dbus_assert (find_handler (tree, path3, &exact_match) && !exact_match);
01241   _dbus_assert (find_handler (tree, path4, &exact_match) && !exact_match);
01242   _dbus_assert (find_handler (tree, path5, &exact_match) == tree->root && !exact_match);
01243   _dbus_assert (find_handler (tree, path6, &exact_match) == tree->root && !exact_match);
01244   _dbus_assert (find_handler (tree, path7, &exact_match) == tree->root && !exact_match);
01245   _dbus_assert (find_handler (tree, path8, &exact_match) == tree->root && !exact_match);
01246 
01247   if (!do_register (tree, path2, TRUE, 1, tree_test_data))
01248     goto out;
01249 
01250   _dbus_assert (find_subtree (tree, path1, NULL));
01251   _dbus_assert (find_subtree (tree, path2, NULL));
01252   _dbus_assert (!find_subtree (tree, path3, NULL));
01253   _dbus_assert (!find_subtree (tree, path4, NULL));
01254   _dbus_assert (!find_subtree (tree, path5, NULL));
01255   _dbus_assert (!find_subtree (tree, path6, NULL));
01256   _dbus_assert (!find_subtree (tree, path7, NULL));
01257   _dbus_assert (!find_subtree (tree, path8, NULL));
01258 
01259   if (!do_register (tree, path3, TRUE, 2, tree_test_data))
01260     goto out;
01261 
01262   _dbus_assert (find_subtree (tree, path1, NULL));
01263   _dbus_assert (find_subtree (tree, path2, NULL));
01264   _dbus_assert (find_subtree (tree, path3, NULL));
01265   _dbus_assert (!find_subtree (tree, path4, NULL));
01266   _dbus_assert (!find_subtree (tree, path5, NULL));
01267   _dbus_assert (!find_subtree (tree, path6, NULL));
01268   _dbus_assert (!find_subtree (tree, path7, NULL));
01269   _dbus_assert (!find_subtree (tree, path8, NULL));
01270   
01271   if (!do_register (tree, path4, TRUE, 3, tree_test_data))
01272     goto out;
01273 
01274   _dbus_assert (find_subtree (tree, path1, NULL));
01275   _dbus_assert (find_subtree (tree, path2, NULL));
01276   _dbus_assert (find_subtree (tree, path3, NULL));  
01277   _dbus_assert (find_subtree (tree, path4, NULL));
01278   _dbus_assert (!find_subtree (tree, path5, NULL));
01279   _dbus_assert (!find_subtree (tree, path6, NULL));
01280   _dbus_assert (!find_subtree (tree, path7, NULL));
01281   _dbus_assert (!find_subtree (tree, path8, NULL));
01282   
01283   if (!do_register (tree, path5, TRUE, 4, tree_test_data))
01284     goto out;
01285 
01286   _dbus_assert (find_subtree (tree, path1, NULL));
01287   _dbus_assert (find_subtree (tree, path2, NULL));
01288   _dbus_assert (find_subtree (tree, path3, NULL));
01289   _dbus_assert (find_subtree (tree, path4, NULL));
01290   _dbus_assert (find_subtree (tree, path5, NULL));
01291   _dbus_assert (!find_subtree (tree, path6, NULL));
01292   _dbus_assert (!find_subtree (tree, path7, NULL));
01293   _dbus_assert (!find_subtree (tree, path8, NULL));
01294   
01295   _dbus_assert (find_handler (tree, path1, &exact_match) != tree->root &&  exact_match);
01296   _dbus_assert (find_handler (tree, path2, &exact_match) != tree->root &&  exact_match);
01297   _dbus_assert (find_handler (tree, path3, &exact_match) != tree->root &&  exact_match);
01298   _dbus_assert (find_handler (tree, path4, &exact_match) != tree->root &&  exact_match);
01299   _dbus_assert (find_handler (tree, path5, &exact_match) != tree->root &&  exact_match);
01300   _dbus_assert (find_handler (tree, path6, &exact_match) != tree->root && !exact_match);
01301   _dbus_assert (find_handler (tree, path7, &exact_match) != tree->root && !exact_match);
01302   _dbus_assert (find_handler (tree, path8, &exact_match) == tree->root && !exact_match);
01303 
01304   if (!do_register (tree, path6, TRUE, 5, tree_test_data))
01305     goto out;
01306 
01307   _dbus_assert (find_subtree (tree, path1, NULL));
01308   _dbus_assert (find_subtree (tree, path2, NULL));
01309   _dbus_assert (find_subtree (tree, path3, NULL));
01310   _dbus_assert (find_subtree (tree, path4, NULL));
01311   _dbus_assert (find_subtree (tree, path5, NULL));
01312   _dbus_assert (find_subtree (tree, path6, NULL));
01313   _dbus_assert (!find_subtree (tree, path7, NULL));
01314   _dbus_assert (!find_subtree (tree, path8, NULL));
01315 
01316   if (!do_register (tree, path7, TRUE, 6, tree_test_data))
01317     goto out;
01318 
01319   _dbus_assert (find_subtree (tree, path1, NULL));
01320   _dbus_assert (find_subtree (tree, path2, NULL));
01321   _dbus_assert (find_subtree (tree, path3, NULL));
01322   _dbus_assert (find_subtree (tree, path4, NULL));
01323   _dbus_assert (find_subtree (tree, path5, NULL));
01324   _dbus_assert (find_subtree (tree, path6, NULL));
01325   _dbus_assert (find_subtree (tree, path7, NULL));
01326   _dbus_assert (!find_subtree (tree, path8, NULL));
01327 
01328   if (!do_register (tree, path8, TRUE, 7, tree_test_data))
01329     goto out;
01330 
01331   _dbus_assert (find_subtree (tree, path1, NULL));
01332   _dbus_assert (find_subtree (tree, path2, NULL));
01333   _dbus_assert (find_subtree (tree, path3, NULL));
01334   _dbus_assert (find_subtree (tree, path4, NULL));
01335   _dbus_assert (find_subtree (tree, path5, NULL));
01336   _dbus_assert (find_subtree (tree, path6, NULL));
01337   _dbus_assert (find_subtree (tree, path7, NULL));
01338   _dbus_assert (find_subtree (tree, path8, NULL));
01339   
01340   _dbus_assert (find_handler (tree, path1, &exact_match) != tree->root && exact_match);
01341   _dbus_assert (find_handler (tree, path2, &exact_match) != tree->root && exact_match);
01342   _dbus_assert (find_handler (tree, path3, &exact_match) != tree->root && exact_match);
01343   _dbus_assert (find_handler (tree, path4, &exact_match) != tree->root && exact_match);
01344   _dbus_assert (find_handler (tree, path5, &exact_match) != tree->root && exact_match);
01345   _dbus_assert (find_handler (tree, path6, &exact_match) != tree->root && exact_match);
01346   _dbus_assert (find_handler (tree, path7, &exact_match) != tree->root && exact_match);
01347   _dbus_assert (find_handler (tree, path8, &exact_match) != tree->root && exact_match);
01348   
01349   /* test the list_registered function */
01350 
01351   {
01352     const char *root[] = { NULL };
01353     char **child_entries;
01354     int nb;
01355 
01356     _dbus_object_tree_list_registered_unlocked (tree, path1, &child_entries);
01357     if (child_entries != NULL)
01358       {
01359         nb = string_array_length (child_entries);
01360         _dbus_assert (nb == 1);
01361         dbus_free_string_array (child_entries);
01362       }
01363 
01364     _dbus_object_tree_list_registered_unlocked (tree, path2, &child_entries);
01365     if (child_entries != NULL)
01366       {
01367         nb = string_array_length (child_entries);
01368         _dbus_assert (nb == 2);
01369         dbus_free_string_array (child_entries);
01370       }
01371 
01372     _dbus_object_tree_list_registered_unlocked (tree, path8, &child_entries);
01373     if (child_entries != NULL)
01374       {
01375         nb = string_array_length (child_entries);
01376         _dbus_assert (nb == 0);
01377         dbus_free_string_array (child_entries);
01378       }
01379 
01380     _dbus_object_tree_list_registered_unlocked (tree, root, &child_entries);
01381     if (child_entries != NULL)
01382       {
01383         nb = string_array_length (child_entries);
01384         _dbus_assert (nb == 3);
01385         dbus_free_string_array (child_entries);
01386       }
01387   }
01388 
01389   /* Check that destroying tree calls unregister funcs */
01390   _dbus_object_tree_unref (tree);
01391 
01392   i = 0;
01393   while (i < (int) _DBUS_N_ELEMENTS (tree_test_data))
01394     {
01395       _dbus_assert (tree_test_data[i].handler_unregistered);
01396       _dbus_assert (!tree_test_data[i].message_handled);
01397       ++i;
01398     }
01399 
01400   /* Now start again and try the individual unregister function */
01401   tree = _dbus_object_tree_new (NULL);
01402   if (tree == NULL)
01403     goto out;
01404 
01405   if (!do_register (tree, path1, TRUE, 0, tree_test_data))
01406     goto out;
01407   if (!do_register (tree, path2, TRUE, 1, tree_test_data))
01408     goto out;
01409   if (!do_register (tree, path3, TRUE, 2, tree_test_data))
01410     goto out;
01411   if (!do_register (tree, path4, TRUE, 3, tree_test_data))
01412     goto out;
01413   if (!do_register (tree, path5, TRUE, 4, tree_test_data))
01414     goto out;
01415   if (!do_register (tree, path6, TRUE, 5, tree_test_data))
01416     goto out;
01417   if (!do_register (tree, path7, TRUE, 6, tree_test_data))
01418     goto out;
01419   if (!do_register (tree, path8, TRUE, 7, tree_test_data))
01420     goto out;
01421   
01422   _dbus_object_tree_unregister_and_unlock (tree, path1);
01423 
01424   _dbus_assert (!find_subtree (tree, path1, NULL));
01425   _dbus_assert (find_subtree (tree, path2, NULL));
01426   _dbus_assert (find_subtree (tree, path3, NULL));
01427   _dbus_assert (find_subtree (tree, path4, NULL));
01428   _dbus_assert (find_subtree (tree, path5, NULL));
01429   _dbus_assert (find_subtree (tree, path6, NULL));
01430   _dbus_assert (find_subtree (tree, path7, NULL));
01431   _dbus_assert (find_subtree (tree, path8, NULL));
01432 
01433   _dbus_object_tree_unregister_and_unlock (tree, path2);
01434 
01435   _dbus_assert (!find_subtree (tree, path1, NULL));
01436   _dbus_assert (!find_subtree (tree, path2, NULL));
01437   _dbus_assert (find_subtree (tree, path3, NULL));
01438   _dbus_assert (find_subtree (tree, path4, NULL));
01439   _dbus_assert (find_subtree (tree, path5, NULL));
01440   _dbus_assert (find_subtree (tree, path6, NULL));
01441   _dbus_assert (find_subtree (tree, path7, NULL));
01442   _dbus_assert (find_subtree (tree, path8, NULL));
01443   
01444   _dbus_object_tree_unregister_and_unlock (tree, path3);
01445 
01446   _dbus_assert (!find_subtree (tree, path1, NULL));
01447   _dbus_assert (!find_subtree (tree, path2, NULL));
01448   _dbus_assert (!find_subtree (tree, path3, NULL));
01449   _dbus_assert (find_subtree (tree, path4, NULL));
01450   _dbus_assert (find_subtree (tree, path5, NULL));
01451   _dbus_assert (find_subtree (tree, path6, NULL));
01452   _dbus_assert (find_subtree (tree, path7, NULL));
01453   _dbus_assert (find_subtree (tree, path8, NULL));
01454   
01455   _dbus_object_tree_unregister_and_unlock (tree, path4);
01456 
01457   _dbus_assert (!find_subtree (tree, path1, NULL));
01458   _dbus_assert (!find_subtree (tree, path2, NULL));
01459   _dbus_assert (!find_subtree (tree, path3, NULL));
01460   _dbus_assert (!find_subtree (tree, path4, NULL));
01461   _dbus_assert (find_subtree (tree, path5, NULL));
01462   _dbus_assert (find_subtree (tree, path6, NULL));
01463   _dbus_assert (find_subtree (tree, path7, NULL));
01464   _dbus_assert (find_subtree (tree, path8, NULL));
01465   
01466   _dbus_object_tree_unregister_and_unlock (tree, path5);
01467 
01468   _dbus_assert (!find_subtree (tree, path1, NULL));
01469   _dbus_assert (!find_subtree (tree, path2, NULL));
01470   _dbus_assert (!find_subtree (tree, path3, NULL));
01471   _dbus_assert (!find_subtree (tree, path4, NULL));
01472   _dbus_assert (!find_subtree (tree, path5, NULL));
01473   _dbus_assert (find_subtree (tree, path6, NULL));
01474   _dbus_assert (find_subtree (tree, path7, NULL));
01475   _dbus_assert (find_subtree (tree, path8, NULL));
01476   
01477   _dbus_object_tree_unregister_and_unlock (tree, path6);
01478 
01479   _dbus_assert (!find_subtree (tree, path1, NULL));
01480   _dbus_assert (!find_subtree (tree, path2, NULL));
01481   _dbus_assert (!find_subtree (tree, path3, NULL));
01482   _dbus_assert (!find_subtree (tree, path4, NULL));
01483   _dbus_assert (!find_subtree (tree, path5, NULL));
01484   _dbus_assert (!find_subtree (tree, path6, NULL));
01485   _dbus_assert (find_subtree (tree, path7, NULL));
01486   _dbus_assert (find_subtree (tree, path8, NULL));
01487 
01488   _dbus_object_tree_unregister_and_unlock (tree, path7);
01489 
01490   _dbus_assert (!find_subtree (tree, path1, NULL));
01491   _dbus_assert (!find_subtree (tree, path2, NULL));
01492   _dbus_assert (!find_subtree (tree, path3, NULL));
01493   _dbus_assert (!find_subtree (tree, path4, NULL));
01494   _dbus_assert (!find_subtree (tree, path5, NULL));
01495   _dbus_assert (!find_subtree (tree, path6, NULL));
01496   _dbus_assert (!find_subtree (tree, path7, NULL));
01497   _dbus_assert (find_subtree (tree, path8, NULL));
01498 
01499   _dbus_object_tree_unregister_and_unlock (tree, path8);
01500 
01501   _dbus_assert (!find_subtree (tree, path1, NULL));
01502   _dbus_assert (!find_subtree (tree, path2, NULL));
01503   _dbus_assert (!find_subtree (tree, path3, NULL));
01504   _dbus_assert (!find_subtree (tree, path4, NULL));
01505   _dbus_assert (!find_subtree (tree, path5, NULL));
01506   _dbus_assert (!find_subtree (tree, path6, NULL));
01507   _dbus_assert (!find_subtree (tree, path7, NULL));
01508   _dbus_assert (!find_subtree (tree, path8, NULL));
01509   
01510   i = 0;
01511   while (i < (int) _DBUS_N_ELEMENTS (tree_test_data))
01512     {
01513       _dbus_assert (tree_test_data[i].handler_unregistered);
01514       _dbus_assert (!tree_test_data[i].message_handled);
01515       ++i;
01516     }
01517 
01518   /* Register it all again, and test dispatch */
01519 
01520   if (!do_register (tree, path1, FALSE, 0, tree_test_data))
01521     goto out;
01522   if (!do_register (tree, path2, TRUE, 1, tree_test_data))
01523     goto out;
01524   if (!do_register (tree, path3, TRUE, 2, tree_test_data))
01525     goto out;
01526   if (!do_register (tree, path4, TRUE, 3, tree_test_data))
01527     goto out;
01528   if (!do_register (tree, path5, TRUE, 4, tree_test_data))
01529     goto out;
01530   if (!do_register (tree, path6, FALSE, 5, tree_test_data))
01531     goto out;
01532   if (!do_register (tree, path7, TRUE, 6, tree_test_data))
01533     goto out;
01534   if (!do_register (tree, path8, TRUE, 7, tree_test_data))
01535     goto out;
01536 
01537 #if 0
01538   spew_tree (tree);
01539 #endif
01540   
01541   if (!do_test_dispatch (tree, path1, 0, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
01542     goto out;
01543   if (!do_test_dispatch (tree, path2, 1, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
01544     goto out;
01545   if (!do_test_dispatch (tree, path3, 2, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
01546     goto out;
01547   if (!do_test_dispatch (tree, path4, 3, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
01548     goto out;
01549   if (!do_test_dispatch (tree, path5, 4, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
01550     goto out;
01551   if (!do_test_dispatch (tree, path6, 5, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
01552     goto out;
01553   if (!do_test_dispatch (tree, path7, 6, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
01554     goto out;
01555   if (!do_test_dispatch (tree, path8, 7, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
01556     goto out;
01557   
01558  out:
01559   if (tree)
01560     {
01561       /* test ref */
01562       _dbus_object_tree_ref (tree);
01563       _dbus_object_tree_unref (tree);
01564       _dbus_object_tree_unref (tree);
01565     }
01566 
01567   return TRUE;
01568 }
01569 
01575 dbus_bool_t
01576 _dbus_object_tree_test (void)
01577 {
01578   _dbus_test_oom_handling ("object tree",
01579                            object_tree_test_iteration,
01580                            NULL);
01581 
01582   return TRUE;
01583 }
01584 
01585 #endif /* DBUS_BUILD_TESTS */

Generated on Wed Mar 30 21:15:22 2005 for D-BUS by  doxygen 1.4.1