Main Page   Modules   Data Structures   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.0
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   subtree = find_subtree (tree, path, &i);
00455 
00456 #ifndef DBUS_DISABLE_CHECKS
00457   if (subtree == NULL)
00458     {
00459       _dbus_warn ("Attempted to unregister path (path[0] = %s path[1] = %s) which isn't registered\n",
00460                   path[0] ? path[0] : "null",
00461                   path[1] ? path[1] : "null");
00462       return;
00463     }
00464 #else
00465   _dbus_assert (subtree != NULL);
00466 #endif
00467 
00468   _dbus_assert (subtree->parent == NULL ||
00469                 (i >= 0 && subtree->parent->subtrees[i] == subtree));
00470 
00471   subtree->message_function = NULL;
00472 
00473   unregister_function = subtree->unregister_function;
00474   user_data = subtree->user_data;
00475 
00476   subtree->unregister_function = NULL;
00477   subtree->user_data = NULL;
00478 
00479   /* If we have no subtrees of our own, remove from
00480    * our parent (FIXME could also be more aggressive
00481    * and remove our parent if it becomes empty)
00482    */
00483   if (subtree->parent && subtree->n_subtrees == 0)
00484     {
00485       /* assumes a 0-byte memmove is OK */
00486       memmove (&subtree->parent->subtrees[i],
00487                &subtree->parent->subtrees[i+1],
00488                (subtree->parent->n_subtrees - i - 1) *
00489                sizeof (subtree->parent->subtrees[0]));
00490       subtree->parent->n_subtrees -= 1;
00491 
00492       subtree->parent = NULL;
00493 
00494       _dbus_object_subtree_unref (subtree);
00495     }
00496   subtree = NULL;
00497 
00498   connection = tree->connection;
00499 
00500   /* Unlock and call application code */
00501 #ifdef DBUS_BUILD_TESTS
00502   if (connection)
00503 #endif
00504     {
00505       _dbus_connection_ref_unlocked (connection);
00506       _dbus_connection_unlock (connection);
00507     }
00508 
00509   if (unregister_function)
00510     (* unregister_function) (connection, user_data);
00511 
00512 #ifdef DBUS_BUILD_TESTS
00513   if (connection)
00514 #endif
00515     dbus_connection_unref (connection);
00516 }
00517 
00518 static void
00519 free_subtree_recurse (DBusConnection    *connection,
00520                       DBusObjectSubtree *subtree)
00521 {
00522   /* Delete them from the end, for slightly
00523    * more robustness against odd reentrancy.
00524    */
00525   while (subtree->n_subtrees > 0)
00526     {
00527       DBusObjectSubtree *child;
00528 
00529       child = subtree->subtrees[subtree->n_subtrees - 1];
00530       subtree->subtrees[subtree->n_subtrees - 1] = NULL;
00531       subtree->n_subtrees -= 1;
00532       child->parent = NULL;
00533 
00534       free_subtree_recurse (connection, child);
00535     }
00536 
00537   /* Call application code */
00538   if (subtree->unregister_function)
00539     {
00540       (* subtree->unregister_function) (connection,
00541                                         subtree->user_data);
00542       subtree->message_function = NULL;
00543       subtree->unregister_function = NULL;
00544       subtree->user_data = NULL;
00545     }
00546 
00547   /* Now free ourselves */
00548   _dbus_object_subtree_unref (subtree);
00549 }
00550 
00557 void
00558 _dbus_object_tree_free_all_unlocked (DBusObjectTree *tree)
00559 {
00560   if (tree->root)
00561     free_subtree_recurse (tree->connection,
00562                           tree->root);
00563   tree->root = NULL;
00564 }
00565 
00566 static dbus_bool_t
00567 _dbus_object_tree_list_registered_unlocked (DBusObjectTree *tree,
00568                                             const char    **parent_path,
00569                                             char         ***child_entries)
00570 {
00571   DBusObjectSubtree *subtree;
00572   char **retval;
00573   
00574   _dbus_assert (parent_path != NULL);
00575   _dbus_assert (child_entries != NULL);
00576 
00577   *child_entries = NULL;
00578   
00579   subtree = lookup_subtree (tree, parent_path);
00580   if (subtree == NULL)
00581     {
00582       retval = dbus_new0 (char *, 1);
00583     }
00584   else
00585     {
00586       int i;
00587       retval = dbus_new0 (char*, subtree->n_subtrees + 1);
00588       if (retval == NULL)
00589         goto out;
00590       i = 0;
00591       while (i < subtree->n_subtrees)
00592         {
00593           retval[i] = _dbus_strdup (subtree->subtrees[i]->name);
00594           if (retval[i] == NULL)
00595             {
00596               dbus_free_string_array (retval);
00597               retval = NULL;
00598               goto out;
00599             }
00600           ++i;
00601         }
00602     }
00603 
00604  out:
00605     
00606   *child_entries = retval;
00607   return retval != NULL;
00608 }
00609 
00610 static DBusHandlerResult
00611 handle_default_introspect_unlocked (DBusObjectTree          *tree,
00612                                     DBusMessage             *message,
00613                                     const char             **path)
00614 {
00615   DBusString xml;
00616   DBusHandlerResult result;
00617   char **children;
00618   int i;
00619 
00620   if (!dbus_message_is_method_call (message,
00621                                     DBUS_INTERFACE_ORG_FREEDESKTOP_INTROSPECTABLE,
00622                                     "Introspect"))
00623     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
00624   
00625   if (!_dbus_string_init (&xml))
00626     return DBUS_HANDLER_RESULT_NEED_MEMORY;
00627 
00628   result = DBUS_HANDLER_RESULT_NEED_MEMORY;
00629 
00630   children = NULL;
00631   if (!_dbus_object_tree_list_registered_unlocked (tree, path, &children))
00632     goto out;
00633 
00634   if (!_dbus_string_append (&xml, "<node>\n"))
00635     goto out;
00636 
00637   i = 0;
00638   while (children[i] != NULL)
00639     {
00640       if (!_dbus_string_append_printf (&xml, "  <node name=\"%s\"/>\n",
00641                                        children[i]))
00642         goto out;
00643 
00644       ++i;
00645     }
00646 
00647   if (!_dbus_string_append (&xml, "</node>\n"))
00648     goto out;
00649   
00650   result = DBUS_HANDLER_RESULT_HANDLED;
00651   
00652  out:
00653   _dbus_string_free (&xml);
00654   dbus_free_string_array (children);
00655   
00656   return result;
00657 }
00658 
00672 DBusHandlerResult
00673 _dbus_object_tree_dispatch_and_unlock (DBusObjectTree          *tree,
00674                                        DBusMessage             *message)
00675 {
00676   char **path;
00677   dbus_bool_t exact_match;
00678   DBusList *list;
00679   DBusList *link;
00680   DBusHandlerResult result;
00681   DBusObjectSubtree *subtree;
00682   
00683 #if 0
00684   _dbus_verbose ("Dispatch of message by object path\n");
00685 #endif
00686   
00687   path = NULL;
00688   if (!dbus_message_get_path_decomposed (message, &path))
00689     {
00690 #ifdef DBUS_BUILD_TESTS
00691       if (tree->connection)
00692 #endif
00693         _dbus_connection_unlock (tree->connection);
00694       
00695       _dbus_verbose ("No memory to get decomposed path\n");
00696 
00697       return DBUS_HANDLER_RESULT_NEED_MEMORY;
00698     }
00699 
00700   if (path == NULL)
00701     {
00702 #ifdef DBUS_BUILD_TESTS
00703       if (tree->connection)
00704 #endif
00705         _dbus_connection_unlock (tree->connection);
00706       
00707       _dbus_verbose ("No path field in message\n");
00708       return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
00709     }
00710   
00711   /* Find the deepest path that covers the path in the message */
00712   subtree = find_handler (tree, (const char**) path, &exact_match);
00713   
00714   /* Build a list of all paths that cover the path in the message */
00715 
00716   list = NULL;
00717 
00718   while (subtree != NULL)
00719     {
00720       if (subtree->message_function != NULL && (exact_match || subtree->invoke_as_fallback))
00721         {
00722           _dbus_object_subtree_ref (subtree);
00723 
00724           /* run deepest paths first */
00725           if (!_dbus_list_append (&list, subtree))
00726             {
00727               result = DBUS_HANDLER_RESULT_NEED_MEMORY;
00728               _dbus_object_subtree_unref (subtree);
00729               goto free_and_return;
00730             }
00731         }
00732 
00733       exact_match = FALSE;
00734       subtree = subtree->parent;
00735     }
00736 
00737   _dbus_verbose ("%d handlers in the path tree for this message\n",
00738                  _dbus_list_get_length (&list));
00739 
00740   /* Invoke each handler in the list */
00741 
00742   result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
00743 
00744   link = _dbus_list_get_first_link (&list);
00745   while (link != NULL)
00746     {
00747       DBusList *next = _dbus_list_get_next_link (&list, link);
00748       subtree = link->data;
00749 
00750       /* message_function is NULL if we're unregistered
00751        * due to reentrancy
00752        */
00753       if (subtree->message_function)
00754         {
00755           DBusObjectPathMessageFunction message_function;
00756           void *user_data;
00757 
00758           message_function = subtree->message_function;
00759           user_data = subtree->user_data;
00760 
00761 #if 0
00762           _dbus_verbose ("  (invoking a handler)\n");
00763 #endif
00764           
00765 #ifdef DBUS_BUILD_TESTS
00766           if (tree->connection)
00767 #endif
00768             _dbus_connection_unlock (tree->connection);
00769 
00770           /* FIXME you could unregister the subtree in another thread
00771            * before we invoke the callback, and I can't figure out a
00772            * good way to solve this.
00773            */
00774 
00775           result = (* message_function) (tree->connection,
00776                                          message,
00777                                          user_data);
00778 
00779 #ifdef DBUS_BUILD_TESTS
00780           if (tree->connection)
00781 #endif
00782             _dbus_connection_lock (tree->connection);
00783 
00784           if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
00785             goto free_and_return;
00786         }
00787 
00788       link = next;
00789     }
00790 
00791  free_and_return:
00792 
00793   if (result == DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
00794     {
00795       /* This hardcoded default handler does a minimal Introspect()
00796        */
00797       result = handle_default_introspect_unlocked (tree, message,
00798                                                    (const char**) path);
00799     }
00800 
00801 #ifdef DBUS_BUILD_TESTS
00802   if (tree->connection)
00803 #endif
00804     _dbus_connection_unlock (tree->connection);
00805   
00806   while (list != NULL)
00807     {
00808       link = _dbus_list_get_first_link (&list);
00809       _dbus_object_subtree_unref (link->data);
00810       _dbus_list_remove_link (&list, link);
00811     }
00812   
00813   dbus_free_string_array (path);
00814 
00815   return result;
00816 }
00817 
00824 static DBusObjectSubtree*
00825 allocate_subtree_object (const char *name)
00826 {
00827   int len;
00828   DBusObjectSubtree *subtree;
00829   const size_t front_padding = _DBUS_STRUCT_OFFSET (DBusObjectSubtree, name);
00830 
00831   _dbus_assert (name != NULL);
00832 
00833   len = strlen (name);
00834 
00835   subtree = dbus_malloc (front_padding + (len + 1));
00836 
00837   if (subtree == NULL)
00838     return NULL;
00839 
00840   memcpy (subtree->name, name, len + 1);
00841 
00842   return subtree;
00843 }
00844 
00845 static DBusObjectSubtree*
00846 _dbus_object_subtree_new (const char                  *name,
00847                           const DBusObjectPathVTable  *vtable,
00848                           void                        *user_data)
00849 {
00850   DBusObjectSubtree *subtree;
00851 
00852   subtree = allocate_subtree_object (name);
00853   if (subtree == NULL)
00854     goto oom;
00855 
00856   _dbus_assert (name != NULL);
00857 
00858   subtree->parent = NULL;
00859 
00860   if (vtable)
00861     {
00862       subtree->message_function = vtable->message_function;
00863       subtree->unregister_function = vtable->unregister_function;
00864     }
00865   else
00866     {
00867       subtree->message_function = NULL;
00868       subtree->unregister_function = NULL;
00869     }
00870 
00871   subtree->user_data = user_data;
00872   subtree->refcount.value = 1;
00873   subtree->subtrees = NULL;
00874   subtree->n_subtrees = 0;
00875   subtree->subtrees_sorted = TRUE;
00876   subtree->invoke_as_fallback = FALSE;
00877 
00878   return subtree;
00879 
00880  oom:
00881   if (subtree)
00882     {
00883       dbus_free (subtree);
00884     }
00885 
00886   return NULL;
00887 }
00888 
00889 static DBusObjectSubtree *
00890 _dbus_object_subtree_ref (DBusObjectSubtree *subtree)
00891 {
00892   _dbus_assert (subtree->refcount.value > 0);
00893   _dbus_atomic_inc (&subtree->refcount);
00894 
00895   return subtree;
00896 }
00897 
00898 static void
00899 _dbus_object_subtree_unref (DBusObjectSubtree *subtree)
00900 {
00901   _dbus_assert (subtree->refcount.value > 0);
00902 
00903   if (_dbus_atomic_dec (&subtree->refcount) == 1)
00904     {
00905       _dbus_assert (subtree->unregister_function == NULL);
00906       _dbus_assert (subtree->message_function == NULL);
00907 
00908       dbus_free (subtree->subtrees);
00909       dbus_free (subtree);
00910     }
00911 }
00912 
00923 dbus_bool_t
00924 _dbus_object_tree_list_registered_and_unlock (DBusObjectTree *tree,
00925                                               const char    **parent_path,
00926                                               char         ***child_entries)
00927 {
00928   dbus_bool_t result;
00929 
00930   result = _dbus_object_tree_list_registered_unlocked (tree,
00931                                                        parent_path,
00932                                                        child_entries);
00933   
00934 #ifdef DBUS_BUILD_TESTS
00935   if (tree->connection)
00936 #endif
00937     _dbus_connection_unlock (tree->connection);
00938 
00939   return result;
00940 }
00941      
00944 #ifdef DBUS_BUILD_TESTS
00945 #include "dbus-test.h"
00946 #include <stdio.h>
00947 
00948 static char*
00949 flatten_path (const char **path)
00950 {
00951   DBusString str;
00952   int i;
00953   char *s;
00954 
00955   if (!_dbus_string_init (&str))
00956     return NULL;
00957 
00958   i = 0;
00959   while (path[i])
00960     {
00961       if (!_dbus_string_append_byte (&str, '/'))
00962         goto nomem;
00963 
00964       if (!_dbus_string_append (&str, path[i]))
00965         goto nomem;
00966 
00967       ++i;
00968     }
00969 
00970   if (!_dbus_string_steal_data (&str, &s))
00971     goto nomem;
00972 
00973   _dbus_string_free (&str);
00974 
00975   return s;
00976 
00977  nomem:
00978   _dbus_string_free (&str);
00979   return NULL;
00980 }
00981 
00982 
00983 typedef enum 
00984 {
00985   STR_EQUAL,
00986   STR_PREFIX,
00987   STR_DIFFERENT
00988 } StrComparison;
00989 
00990 /* Returns TRUE if container is a parent of child
00991  */
00992 static StrComparison
00993 path_contains (const char **container,
00994                const char **child)
00995 {
00996   int i;
00997 
00998   i = 0;
00999   while (child[i] != NULL)
01000     {
01001       int v;
01002 
01003       if (container[i] == NULL)
01004         return STR_PREFIX; /* container ran out, child continues;
01005                         * thus the container is a parent of the
01006                         * child.
01007                         */
01008 
01009       _dbus_assert (container[i] != NULL);
01010       _dbus_assert (child[i] != NULL);
01011 
01012       v = strcmp (container[i], child[i]);
01013 
01014       if (v != 0)
01015         return STR_DIFFERENT; /* they overlap until here and then are different,
01016                            * not overlapping
01017                            */
01018 
01019       ++i;
01020     }
01021 
01022   /* Child ran out; if container also did, they are equal;
01023    * otherwise, the child is a parent of the container.
01024    */
01025   if (container[i] == NULL)
01026     return STR_EQUAL;
01027   else
01028     return STR_DIFFERENT;
01029 }
01030 
01031 #if 0
01032 static void
01033 spew_subtree_recurse (DBusObjectSubtree *subtree,
01034                       int                indent)
01035 {
01036   int i;
01037 
01038   i = 0;
01039   while (i < indent)
01040     {
01041       _dbus_verbose (" ");
01042       ++i;
01043     }
01044 
01045   _dbus_verbose ("%s (%d children)\n",
01046                  subtree->name, subtree->n_subtrees);
01047 
01048   i = 0;
01049   while (i < subtree->n_subtrees)
01050     {
01051       spew_subtree_recurse (subtree->subtrees[i], indent + 2);
01052 
01053       ++i;
01054     }
01055 }
01056 
01057 static void
01058 spew_tree (DBusObjectTree *tree)
01059 {
01060   spew_subtree_recurse (tree->root, 0);
01061 }
01062 #endif
01063 
01067 typedef struct
01068 {
01069   const char **path; 
01070   dbus_bool_t handler_fallback; 
01071   dbus_bool_t message_handled; 
01072   dbus_bool_t handler_unregistered; 
01073 } TreeTestData;
01074 
01075 
01076 static void
01077 test_unregister_function (DBusConnection  *connection,
01078                           void            *user_data)
01079 {
01080   TreeTestData *ttd = user_data;
01081 
01082   ttd->handler_unregistered = TRUE;
01083 }
01084 
01085 static DBusHandlerResult
01086 test_message_function (DBusConnection  *connection,
01087                        DBusMessage     *message,
01088                        void            *user_data)
01089 {
01090   TreeTestData *ttd = user_data;
01091 
01092   ttd->message_handled = TRUE;
01093 
01094   return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
01095 }
01096 
01097 static dbus_bool_t
01098 do_register (DBusObjectTree *tree,
01099              const char    **path,
01100              dbus_bool_t     fallback,
01101              int             i,
01102              TreeTestData   *tree_test_data)
01103 {
01104   DBusObjectPathVTable vtable = { test_unregister_function,
01105                                   test_message_function, NULL };
01106 
01107   tree_test_data[i].message_handled = FALSE;
01108   tree_test_data[i].handler_unregistered = FALSE;
01109   tree_test_data[i].handler_fallback = fallback;
01110   tree_test_data[i].path = path;
01111 
01112   if (!_dbus_object_tree_register (tree, fallback, path,
01113                                    &vtable,
01114                                    &tree_test_data[i]))
01115     return FALSE;
01116 
01117   return TRUE;
01118 }
01119 
01120 static dbus_bool_t
01121 do_test_dispatch (DBusObjectTree *tree,
01122                   const char    **path,
01123                   int             i,
01124                   TreeTestData   *tree_test_data,
01125                   int             n_test_data)
01126 {
01127   DBusMessage *message;
01128   int j;
01129   DBusHandlerResult result;
01130   char *flat;
01131 
01132   message = NULL;
01133   
01134   flat = flatten_path (path);
01135   if (flat == NULL)
01136     goto oom;
01137 
01138   message = dbus_message_new_method_call (NULL,
01139                                           flat,
01140                                           "org.freedesktop.TestInterface",
01141                                           "Foo");
01142   dbus_free (flat);
01143   if (message == NULL)
01144     goto oom;
01145 
01146   j = 0;
01147   while (j < n_test_data)
01148     {
01149       tree_test_data[j].message_handled = FALSE;
01150       ++j;
01151     }
01152 
01153   result = _dbus_object_tree_dispatch_and_unlock (tree, message);
01154   if (result == DBUS_HANDLER_RESULT_NEED_MEMORY)
01155     goto oom;
01156 
01157   _dbus_assert (tree_test_data[i].message_handled);
01158 
01159   j = 0;
01160   while (j < n_test_data)
01161     {
01162       if (tree_test_data[j].message_handled)
01163         {
01164           if (tree_test_data[j].handler_fallback)
01165             _dbus_assert (path_contains (tree_test_data[j].path,
01166                                          path) != STR_DIFFERENT);
01167           else
01168             _dbus_assert (path_contains (tree_test_data[j].path, path) == STR_EQUAL);
01169         }
01170       else
01171         {
01172           if (tree_test_data[j].handler_fallback)
01173             _dbus_assert (path_contains (tree_test_data[j].path,
01174                                          path) == STR_DIFFERENT);
01175           else
01176             _dbus_assert (path_contains (tree_test_data[j].path, path) != STR_EQUAL);
01177         }
01178 
01179       ++j;
01180     }
01181 
01182   dbus_message_unref (message);
01183 
01184   return TRUE;
01185 
01186  oom:
01187   if (message)
01188     dbus_message_unref (message);
01189   return FALSE;
01190 }
01191 
01192 static size_t
01193 string_array_length (char **array)
01194 {
01195   size_t i;
01196   for (i = 0; array[i]; i++) ;
01197   return i;
01198 }
01199 
01200 
01201 static dbus_bool_t
01202 object_tree_test_iteration (void *data)
01203 {
01204   const char *path1[] = { "foo", NULL };
01205   const char *path2[] = { "foo", "bar", NULL };
01206   const char *path3[] = { "foo", "bar", "baz", NULL };
01207   const char *path4[] = { "foo", "bar", "boo", NULL };
01208   const char *path5[] = { "blah", NULL };
01209   const char *path6[] = { "blah", "boof", NULL };
01210   const char *path7[] = { "blah", "boof", "this", "is", "really", "long", NULL };
01211   const char *path8[] = { "childless", NULL };
01212   DBusObjectTree *tree;
01213   TreeTestData tree_test_data[8];
01214   int i;
01215   dbus_bool_t exact_match;
01216 
01217   tree = NULL;
01218 
01219   tree = _dbus_object_tree_new (NULL);
01220   if (tree == NULL)
01221     goto out;
01222 
01223   if (!do_register (tree, path1, TRUE, 0, tree_test_data))
01224     goto out;
01225 
01226   _dbus_assert (find_subtree (tree, path1, NULL));
01227   _dbus_assert (!find_subtree (tree, path2, NULL));
01228   _dbus_assert (!find_subtree (tree, path3, NULL));
01229   _dbus_assert (!find_subtree (tree, path4, NULL));
01230   _dbus_assert (!find_subtree (tree, path5, NULL));
01231   _dbus_assert (!find_subtree (tree, path6, NULL));
01232   _dbus_assert (!find_subtree (tree, path7, NULL));
01233   _dbus_assert (!find_subtree (tree, path8, NULL));
01234 
01235   _dbus_assert (find_handler (tree, path1, &exact_match) &&  exact_match);
01236   _dbus_assert (find_handler (tree, path2, &exact_match) && !exact_match);
01237   _dbus_assert (find_handler (tree, path3, &exact_match) && !exact_match);
01238   _dbus_assert (find_handler (tree, path4, &exact_match) && !exact_match);
01239   _dbus_assert (find_handler (tree, path5, &exact_match) == tree->root && !exact_match);
01240   _dbus_assert (find_handler (tree, path6, &exact_match) == tree->root && !exact_match);
01241   _dbus_assert (find_handler (tree, path7, &exact_match) == tree->root && !exact_match);
01242   _dbus_assert (find_handler (tree, path8, &exact_match) == tree->root && !exact_match);
01243 
01244   if (!do_register (tree, path2, TRUE, 1, tree_test_data))
01245     goto out;
01246 
01247   _dbus_assert (find_subtree (tree, path1, NULL));
01248   _dbus_assert (find_subtree (tree, path2, NULL));
01249   _dbus_assert (!find_subtree (tree, path3, NULL));
01250   _dbus_assert (!find_subtree (tree, path4, NULL));
01251   _dbus_assert (!find_subtree (tree, path5, NULL));
01252   _dbus_assert (!find_subtree (tree, path6, NULL));
01253   _dbus_assert (!find_subtree (tree, path7, NULL));
01254   _dbus_assert (!find_subtree (tree, path8, NULL));
01255 
01256   if (!do_register (tree, path3, TRUE, 2, tree_test_data))
01257     goto out;
01258 
01259   _dbus_assert (find_subtree (tree, path1, NULL));
01260   _dbus_assert (find_subtree (tree, path2, NULL));
01261   _dbus_assert (find_subtree (tree, path3, NULL));
01262   _dbus_assert (!find_subtree (tree, path4, NULL));
01263   _dbus_assert (!find_subtree (tree, path5, NULL));
01264   _dbus_assert (!find_subtree (tree, path6, NULL));
01265   _dbus_assert (!find_subtree (tree, path7, NULL));
01266   _dbus_assert (!find_subtree (tree, path8, NULL));
01267   
01268   if (!do_register (tree, path4, TRUE, 3, tree_test_data))
01269     goto out;
01270 
01271   _dbus_assert (find_subtree (tree, path1, NULL));
01272   _dbus_assert (find_subtree (tree, path2, NULL));
01273   _dbus_assert (find_subtree (tree, path3, NULL));  
01274   _dbus_assert (find_subtree (tree, path4, NULL));
01275   _dbus_assert (!find_subtree (tree, path5, NULL));
01276   _dbus_assert (!find_subtree (tree, path6, NULL));
01277   _dbus_assert (!find_subtree (tree, path7, NULL));
01278   _dbus_assert (!find_subtree (tree, path8, NULL));
01279   
01280   if (!do_register (tree, path5, TRUE, 4, tree_test_data))
01281     goto out;
01282 
01283   _dbus_assert (find_subtree (tree, path1, NULL));
01284   _dbus_assert (find_subtree (tree, path2, NULL));
01285   _dbus_assert (find_subtree (tree, path3, NULL));
01286   _dbus_assert (find_subtree (tree, path4, NULL));
01287   _dbus_assert (find_subtree (tree, path5, NULL));
01288   _dbus_assert (!find_subtree (tree, path6, NULL));
01289   _dbus_assert (!find_subtree (tree, path7, NULL));
01290   _dbus_assert (!find_subtree (tree, path8, NULL));
01291   
01292   _dbus_assert (find_handler (tree, path1, &exact_match) != tree->root &&  exact_match);
01293   _dbus_assert (find_handler (tree, path2, &exact_match) != tree->root &&  exact_match);
01294   _dbus_assert (find_handler (tree, path3, &exact_match) != tree->root &&  exact_match);
01295   _dbus_assert (find_handler (tree, path4, &exact_match) != tree->root &&  exact_match);
01296   _dbus_assert (find_handler (tree, path5, &exact_match) != tree->root &&  exact_match);
01297   _dbus_assert (find_handler (tree, path6, &exact_match) != tree->root && !exact_match);
01298   _dbus_assert (find_handler (tree, path7, &exact_match) != tree->root && !exact_match);
01299   _dbus_assert (find_handler (tree, path8, &exact_match) == tree->root && !exact_match);
01300 
01301   if (!do_register (tree, path6, TRUE, 5, tree_test_data))
01302     goto out;
01303 
01304   _dbus_assert (find_subtree (tree, path1, NULL));
01305   _dbus_assert (find_subtree (tree, path2, NULL));
01306   _dbus_assert (find_subtree (tree, path3, NULL));
01307   _dbus_assert (find_subtree (tree, path4, NULL));
01308   _dbus_assert (find_subtree (tree, path5, NULL));
01309   _dbus_assert (find_subtree (tree, path6, NULL));
01310   _dbus_assert (!find_subtree (tree, path7, NULL));
01311   _dbus_assert (!find_subtree (tree, path8, NULL));
01312 
01313   if (!do_register (tree, path7, TRUE, 6, tree_test_data))
01314     goto out;
01315 
01316   _dbus_assert (find_subtree (tree, path1, NULL));
01317   _dbus_assert (find_subtree (tree, path2, NULL));
01318   _dbus_assert (find_subtree (tree, path3, NULL));
01319   _dbus_assert (find_subtree (tree, path4, NULL));
01320   _dbus_assert (find_subtree (tree, path5, NULL));
01321   _dbus_assert (find_subtree (tree, path6, NULL));
01322   _dbus_assert (find_subtree (tree, path7, NULL));
01323   _dbus_assert (!find_subtree (tree, path8, NULL));
01324 
01325   if (!do_register (tree, path8, TRUE, 7, tree_test_data))
01326     goto out;
01327 
01328   _dbus_assert (find_subtree (tree, path1, NULL));
01329   _dbus_assert (find_subtree (tree, path2, NULL));
01330   _dbus_assert (find_subtree (tree, path3, NULL));
01331   _dbus_assert (find_subtree (tree, path4, NULL));
01332   _dbus_assert (find_subtree (tree, path5, NULL));
01333   _dbus_assert (find_subtree (tree, path6, NULL));
01334   _dbus_assert (find_subtree (tree, path7, NULL));
01335   _dbus_assert (find_subtree (tree, path8, NULL));
01336   
01337   _dbus_assert (find_handler (tree, path1, &exact_match) != tree->root && exact_match);
01338   _dbus_assert (find_handler (tree, path2, &exact_match) != tree->root && exact_match);
01339   _dbus_assert (find_handler (tree, path3, &exact_match) != tree->root && exact_match);
01340   _dbus_assert (find_handler (tree, path4, &exact_match) != tree->root && exact_match);
01341   _dbus_assert (find_handler (tree, path5, &exact_match) != tree->root && exact_match);
01342   _dbus_assert (find_handler (tree, path6, &exact_match) != tree->root && exact_match);
01343   _dbus_assert (find_handler (tree, path7, &exact_match) != tree->root && exact_match);
01344   _dbus_assert (find_handler (tree, path8, &exact_match) != tree->root && exact_match);
01345   
01346   /* test the list_registered function */
01347 
01348   {
01349     const char *root[] = { NULL };
01350     char **child_entries;
01351     int nb;
01352 
01353     _dbus_object_tree_list_registered_unlocked (tree, path1, &child_entries);
01354     if (child_entries != NULL)
01355       {
01356         nb = string_array_length (child_entries);
01357         _dbus_assert (nb == 1);
01358         dbus_free_string_array (child_entries);
01359       }
01360 
01361     _dbus_object_tree_list_registered_unlocked (tree, path2, &child_entries);
01362     if (child_entries != NULL)
01363       {
01364         nb = string_array_length (child_entries);
01365         _dbus_assert (nb == 2);
01366         dbus_free_string_array (child_entries);
01367       }
01368 
01369     _dbus_object_tree_list_registered_unlocked (tree, path8, &child_entries);
01370     if (child_entries != NULL)
01371       {
01372         nb = string_array_length (child_entries);
01373         _dbus_assert (nb == 0);
01374         dbus_free_string_array (child_entries);
01375       }
01376 
01377     _dbus_object_tree_list_registered_unlocked (tree, root, &child_entries);
01378     if (child_entries != NULL)
01379       {
01380         nb = string_array_length (child_entries);
01381         _dbus_assert (nb == 3);
01382         dbus_free_string_array (child_entries);
01383       }
01384   }
01385 
01386   /* Check that destroying tree calls unregister funcs */
01387   _dbus_object_tree_unref (tree);
01388 
01389   i = 0;
01390   while (i < (int) _DBUS_N_ELEMENTS (tree_test_data))
01391     {
01392       _dbus_assert (tree_test_data[i].handler_unregistered);
01393       _dbus_assert (!tree_test_data[i].message_handled);
01394       ++i;
01395     }
01396 
01397   /* Now start again and try the individual unregister function */
01398   tree = _dbus_object_tree_new (NULL);
01399   if (tree == NULL)
01400     goto out;
01401 
01402   if (!do_register (tree, path1, TRUE, 0, tree_test_data))
01403     goto out;
01404   if (!do_register (tree, path2, TRUE, 1, tree_test_data))
01405     goto out;
01406   if (!do_register (tree, path3, TRUE, 2, tree_test_data))
01407     goto out;
01408   if (!do_register (tree, path4, TRUE, 3, tree_test_data))
01409     goto out;
01410   if (!do_register (tree, path5, TRUE, 4, tree_test_data))
01411     goto out;
01412   if (!do_register (tree, path6, TRUE, 5, tree_test_data))
01413     goto out;
01414   if (!do_register (tree, path7, TRUE, 6, tree_test_data))
01415     goto out;
01416   if (!do_register (tree, path8, TRUE, 7, tree_test_data))
01417     goto out;
01418   
01419   _dbus_object_tree_unregister_and_unlock (tree, path1);
01420 
01421   _dbus_assert (!find_subtree (tree, path1, NULL));
01422   _dbus_assert (find_subtree (tree, path2, NULL));
01423   _dbus_assert (find_subtree (tree, path3, NULL));
01424   _dbus_assert (find_subtree (tree, path4, NULL));
01425   _dbus_assert (find_subtree (tree, path5, NULL));
01426   _dbus_assert (find_subtree (tree, path6, NULL));
01427   _dbus_assert (find_subtree (tree, path7, NULL));
01428   _dbus_assert (find_subtree (tree, path8, NULL));
01429 
01430   _dbus_object_tree_unregister_and_unlock (tree, path2);
01431 
01432   _dbus_assert (!find_subtree (tree, path1, NULL));
01433   _dbus_assert (!find_subtree (tree, path2, NULL));
01434   _dbus_assert (find_subtree (tree, path3, NULL));
01435   _dbus_assert (find_subtree (tree, path4, NULL));
01436   _dbus_assert (find_subtree (tree, path5, NULL));
01437   _dbus_assert (find_subtree (tree, path6, NULL));
01438   _dbus_assert (find_subtree (tree, path7, NULL));
01439   _dbus_assert (find_subtree (tree, path8, NULL));
01440   
01441   _dbus_object_tree_unregister_and_unlock (tree, path3);
01442 
01443   _dbus_assert (!find_subtree (tree, path1, NULL));
01444   _dbus_assert (!find_subtree (tree, path2, NULL));
01445   _dbus_assert (!find_subtree (tree, path3, NULL));
01446   _dbus_assert (find_subtree (tree, path4, NULL));
01447   _dbus_assert (find_subtree (tree, path5, NULL));
01448   _dbus_assert (find_subtree (tree, path6, NULL));
01449   _dbus_assert (find_subtree (tree, path7, NULL));
01450   _dbus_assert (find_subtree (tree, path8, NULL));
01451   
01452   _dbus_object_tree_unregister_and_unlock (tree, path4);
01453 
01454   _dbus_assert (!find_subtree (tree, path1, NULL));
01455   _dbus_assert (!find_subtree (tree, path2, NULL));
01456   _dbus_assert (!find_subtree (tree, path3, NULL));
01457   _dbus_assert (!find_subtree (tree, path4, NULL));
01458   _dbus_assert (find_subtree (tree, path5, NULL));
01459   _dbus_assert (find_subtree (tree, path6, NULL));
01460   _dbus_assert (find_subtree (tree, path7, NULL));
01461   _dbus_assert (find_subtree (tree, path8, NULL));
01462   
01463   _dbus_object_tree_unregister_and_unlock (tree, path5);
01464 
01465   _dbus_assert (!find_subtree (tree, path1, NULL));
01466   _dbus_assert (!find_subtree (tree, path2, NULL));
01467   _dbus_assert (!find_subtree (tree, path3, NULL));
01468   _dbus_assert (!find_subtree (tree, path4, NULL));
01469   _dbus_assert (!find_subtree (tree, path5, NULL));
01470   _dbus_assert (find_subtree (tree, path6, NULL));
01471   _dbus_assert (find_subtree (tree, path7, NULL));
01472   _dbus_assert (find_subtree (tree, path8, NULL));
01473   
01474   _dbus_object_tree_unregister_and_unlock (tree, path6);
01475 
01476   _dbus_assert (!find_subtree (tree, path1, NULL));
01477   _dbus_assert (!find_subtree (tree, path2, NULL));
01478   _dbus_assert (!find_subtree (tree, path3, NULL));
01479   _dbus_assert (!find_subtree (tree, path4, NULL));
01480   _dbus_assert (!find_subtree (tree, path5, NULL));
01481   _dbus_assert (!find_subtree (tree, path6, NULL));
01482   _dbus_assert (find_subtree (tree, path7, NULL));
01483   _dbus_assert (find_subtree (tree, path8, NULL));
01484 
01485   _dbus_object_tree_unregister_and_unlock (tree, path7);
01486 
01487   _dbus_assert (!find_subtree (tree, path1, NULL));
01488   _dbus_assert (!find_subtree (tree, path2, NULL));
01489   _dbus_assert (!find_subtree (tree, path3, NULL));
01490   _dbus_assert (!find_subtree (tree, path4, NULL));
01491   _dbus_assert (!find_subtree (tree, path5, NULL));
01492   _dbus_assert (!find_subtree (tree, path6, NULL));
01493   _dbus_assert (!find_subtree (tree, path7, NULL));
01494   _dbus_assert (find_subtree (tree, path8, NULL));
01495 
01496   _dbus_object_tree_unregister_and_unlock (tree, path8);
01497 
01498   _dbus_assert (!find_subtree (tree, path1, NULL));
01499   _dbus_assert (!find_subtree (tree, path2, NULL));
01500   _dbus_assert (!find_subtree (tree, path3, NULL));
01501   _dbus_assert (!find_subtree (tree, path4, NULL));
01502   _dbus_assert (!find_subtree (tree, path5, NULL));
01503   _dbus_assert (!find_subtree (tree, path6, NULL));
01504   _dbus_assert (!find_subtree (tree, path7, NULL));
01505   _dbus_assert (!find_subtree (tree, path8, NULL));
01506   
01507   i = 0;
01508   while (i < (int) _DBUS_N_ELEMENTS (tree_test_data))
01509     {
01510       _dbus_assert (tree_test_data[i].handler_unregistered);
01511       _dbus_assert (!tree_test_data[i].message_handled);
01512       ++i;
01513     }
01514 
01515   /* Register it all again, and test dispatch */
01516 
01517   if (!do_register (tree, path1, FALSE, 0, tree_test_data))
01518     goto out;
01519   if (!do_register (tree, path2, TRUE, 1, tree_test_data))
01520     goto out;
01521   if (!do_register (tree, path3, TRUE, 2, tree_test_data))
01522     goto out;
01523   if (!do_register (tree, path4, TRUE, 3, tree_test_data))
01524     goto out;
01525   if (!do_register (tree, path5, TRUE, 4, tree_test_data))
01526     goto out;
01527   if (!do_register (tree, path6, FALSE, 5, tree_test_data))
01528     goto out;
01529   if (!do_register (tree, path7, TRUE, 6, tree_test_data))
01530     goto out;
01531   if (!do_register (tree, path8, TRUE, 7, tree_test_data))
01532     goto out;
01533 
01534 #if 0
01535   spew_tree (tree);
01536 #endif
01537   
01538   if (!do_test_dispatch (tree, path1, 0, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
01539     goto out;
01540   if (!do_test_dispatch (tree, path2, 1, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
01541     goto out;
01542   if (!do_test_dispatch (tree, path3, 2, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
01543     goto out;
01544   if (!do_test_dispatch (tree, path4, 3, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
01545     goto out;
01546   if (!do_test_dispatch (tree, path5, 4, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
01547     goto out;
01548   if (!do_test_dispatch (tree, path6, 5, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
01549     goto out;
01550   if (!do_test_dispatch (tree, path7, 6, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
01551     goto out;
01552   if (!do_test_dispatch (tree, path8, 7, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
01553     goto out;
01554   
01555  out:
01556   if (tree)
01557     {
01558       /* test ref */
01559       _dbus_object_tree_ref (tree);
01560       _dbus_object_tree_unref (tree);
01561       _dbus_object_tree_unref (tree);
01562     }
01563 
01564   return TRUE;
01565 }
01566 
01572 dbus_bool_t
01573 _dbus_object_tree_test (void)
01574 {
01575   _dbus_test_oom_handling ("object tree",
01576                            object_tree_test_iteration,
01577                            NULL);
01578 
01579   return TRUE;
01580 }
01581 
01582 #endif /* DBUS_BUILD_TESTS */

Generated on Wed Jun 9 05:01:26 2004 for D-BUS by doxygen1.2.15