00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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
00095
00096
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
00223
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
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
00377 *exact_match = FALSE;
00378
00379 return find_subtree_recurse (tree->root, path, FALSE, NULL, exact_match);
00380 }
00381
00382 static DBusObjectSubtree*
00383 ensure_subtree (DBusObjectTree *tree,
00384 const char **path)
00385 {
00386 #if VERBOSE_FIND
00387 _dbus_verbose ("Ensuring subtree\n");
00388 #endif
00389 return find_subtree_recurse (tree->root, path, TRUE, NULL, NULL);
00390 }
00391
00402 dbus_bool_t
00403 _dbus_object_tree_register (DBusObjectTree *tree,
00404 dbus_bool_t fallback,
00405 const char **path,
00406 const DBusObjectPathVTable *vtable,
00407 void *user_data)
00408 {
00409 DBusObjectSubtree *subtree;
00410
00411 _dbus_assert (tree != NULL);
00412 _dbus_assert (vtable->message_function != NULL);
00413 _dbus_assert (path != NULL);
00414
00415 subtree = ensure_subtree (tree, path);
00416 if (subtree == NULL)
00417 return FALSE;
00418
00419 #ifndef DBUS_DISABLE_CHECKS
00420 if (subtree->message_function != NULL)
00421 {
00422 _dbus_warn ("A handler is already registered for the path starting with path[0] = \"%s\"\n",
00423 path[0] ? path[0] : "null");
00424 return FALSE;
00425 }
00426 #else
00427 _dbus_assert (subtree->message_function == NULL);
00428 #endif
00429
00430 subtree->message_function = vtable->message_function;
00431 subtree->unregister_function = vtable->unregister_function;
00432 subtree->user_data = user_data;
00433 subtree->invoke_as_fallback = fallback != FALSE;
00434
00435 return TRUE;
00436 }
00437
00445 void
00446 _dbus_object_tree_unregister_and_unlock (DBusObjectTree *tree,
00447 const char **path)
00448 {
00449 int i;
00450 DBusObjectSubtree *subtree;
00451 DBusObjectPathUnregisterFunction unregister_function;
00452 void *user_data;
00453 DBusConnection *connection;
00454
00455 _dbus_assert (path != NULL);
00456
00457 unregister_function = NULL;
00458 user_data = NULL;
00459
00460 subtree = find_subtree (tree, path, &i);
00461
00462 #ifndef DBUS_DISABLE_CHECKS
00463 if (subtree == NULL)
00464 {
00465 _dbus_warn ("Attempted to unregister path (path[0] = %s path[1] = %s) which isn't registered\n",
00466 path[0] ? path[0] : "null",
00467 path[1] ? path[1] : "null");
00468 goto unlock;
00469 }
00470 #else
00471 _dbus_assert (subtree != NULL);
00472 #endif
00473
00474 _dbus_assert (subtree->parent == NULL ||
00475 (i >= 0 && subtree->parent->subtrees[i] == subtree));
00476
00477 subtree->message_function = NULL;
00478
00479 unregister_function = subtree->unregister_function;
00480 user_data = subtree->user_data;
00481
00482 subtree->unregister_function = NULL;
00483 subtree->user_data = NULL;
00484
00485
00486
00487
00488
00489 if (subtree->parent && subtree->n_subtrees == 0)
00490 {
00491
00492 memmove (&subtree->parent->subtrees[i],
00493 &subtree->parent->subtrees[i+1],
00494 (subtree->parent->n_subtrees - i - 1) *
00495 sizeof (subtree->parent->subtrees[0]));
00496 subtree->parent->n_subtrees -= 1;
00497
00498 subtree->parent = NULL;
00499
00500 _dbus_object_subtree_unref (subtree);
00501 }
00502 subtree = NULL;
00503
00504 unlock:
00505 connection = tree->connection;
00506
00507
00508 #ifdef DBUS_BUILD_TESTS
00509 if (connection)
00510 #endif
00511 {
00512 _dbus_connection_ref_unlocked (connection);
00513 _dbus_verbose ("unlock %s\n", _DBUS_FUNCTION_NAME);
00514 _dbus_connection_unlock (connection);
00515 }
00516
00517 if (unregister_function)
00518 (* unregister_function) (connection, user_data);
00519
00520 #ifdef DBUS_BUILD_TESTS
00521 if (connection)
00522 #endif
00523 dbus_connection_unref (connection);
00524 }
00525
00526 static void
00527 free_subtree_recurse (DBusConnection *connection,
00528 DBusObjectSubtree *subtree)
00529 {
00530
00531
00532
00533 while (subtree->n_subtrees > 0)
00534 {
00535 DBusObjectSubtree *child;
00536
00537 child = subtree->subtrees[subtree->n_subtrees - 1];
00538 subtree->subtrees[subtree->n_subtrees - 1] = NULL;
00539 subtree->n_subtrees -= 1;
00540 child->parent = NULL;
00541
00542 free_subtree_recurse (connection, child);
00543 }
00544
00545
00546 if (subtree->unregister_function)
00547 (* subtree->unregister_function) (connection,
00548 subtree->user_data);
00549
00550 subtree->message_function = NULL;
00551 subtree->unregister_function = NULL;
00552 subtree->user_data = NULL;
00553
00554
00555 _dbus_object_subtree_unref (subtree);
00556 }
00557
00564 void
00565 _dbus_object_tree_free_all_unlocked (DBusObjectTree *tree)
00566 {
00567 if (tree->root)
00568 free_subtree_recurse (tree->connection,
00569 tree->root);
00570 tree->root = NULL;
00571 }
00572
00573 static dbus_bool_t
00574 _dbus_object_tree_list_registered_unlocked (DBusObjectTree *tree,
00575 const char **parent_path,
00576 char ***child_entries)
00577 {
00578 DBusObjectSubtree *subtree;
00579 char **retval;
00580
00581 _dbus_assert (parent_path != NULL);
00582 _dbus_assert (child_entries != NULL);
00583
00584 *child_entries = NULL;
00585
00586 subtree = lookup_subtree (tree, parent_path);
00587 if (subtree == NULL)
00588 {
00589 retval = dbus_new0 (char *, 1);
00590 }
00591 else
00592 {
00593 int i;
00594 retval = dbus_new0 (char*, subtree->n_subtrees + 1);
00595 if (retval == NULL)
00596 goto out;
00597 i = 0;
00598 while (i < subtree->n_subtrees)
00599 {
00600 retval[i] = _dbus_strdup (subtree->subtrees[i]->name);
00601 if (retval[i] == NULL)
00602 {
00603 dbus_free_string_array (retval);
00604 retval = NULL;
00605 goto out;
00606 }
00607 ++i;
00608 }
00609 }
00610
00611 out:
00612
00613 *child_entries = retval;
00614 return retval != NULL;
00615 }
00616
00617 static DBusHandlerResult
00618 handle_default_introspect_and_unlock (DBusObjectTree *tree,
00619 DBusMessage *message,
00620 const char **path)
00621 {
00622 DBusString xml;
00623 DBusHandlerResult result;
00624 char **children;
00625 int i;
00626 DBusMessage *reply;
00627 DBusMessageIter iter;
00628 const char *v_STRING;
00629 dbus_bool_t already_unlocked;
00630
00631
00632
00633 already_unlocked = FALSE;
00634
00635 _dbus_verbose (" considering default Introspect() handler...\n");
00636
00637 reply = NULL;
00638
00639 if (!dbus_message_is_method_call (message,
00640 DBUS_INTERFACE_INTROSPECTABLE,
00641 "Introspect"))
00642 {
00643 #ifdef DBUS_BUILD_TESTS
00644 if (tree->connection)
00645 #endif
00646 {
00647 _dbus_verbose ("unlock %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
00648 _dbus_connection_unlock (tree->connection);
00649 }
00650
00651 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
00652 }
00653
00654 _dbus_verbose (" using default Introspect() handler!\n");
00655
00656 if (!_dbus_string_init (&xml))
00657 {
00658 #ifdef DBUS_BUILD_TESTS
00659 if (tree->connection)
00660 #endif
00661 {
00662 _dbus_verbose ("unlock %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
00663 _dbus_connection_unlock (tree->connection);
00664 }
00665
00666 return DBUS_HANDLER_RESULT_NEED_MEMORY;
00667 }
00668
00669 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
00670
00671 children = NULL;
00672 if (!_dbus_object_tree_list_registered_unlocked (tree, path, &children))
00673 goto out;
00674
00675 if (!_dbus_string_append (&xml, DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE))
00676 goto out;
00677
00678 if (!_dbus_string_append (&xml, "<node>\n"))
00679 goto out;
00680
00681 i = 0;
00682 while (children[i] != NULL)
00683 {
00684 if (!_dbus_string_append_printf (&xml, " <node name=\"%s\"/>\n",
00685 children[i]))
00686 goto out;
00687
00688 ++i;
00689 }
00690
00691 if (!_dbus_string_append (&xml, "</node>\n"))
00692 goto out;
00693
00694 reply = dbus_message_new_method_return (message);
00695 if (reply == NULL)
00696 goto out;
00697
00698 dbus_message_iter_init_append (reply, &iter);
00699 v_STRING = _dbus_string_get_const_data (&xml);
00700 if (!dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &v_STRING))
00701 goto out;
00702
00703 #ifdef DBUS_BUILD_TESTS
00704 if (tree->connection)
00705 #endif
00706 {
00707 already_unlocked = TRUE;
00708
00709 if (!_dbus_connection_send_and_unlock (tree->connection, reply, NULL))
00710 goto out;
00711 }
00712
00713 result = DBUS_HANDLER_RESULT_HANDLED;
00714
00715 out:
00716 #ifdef DBUS_BUILD_TESTS
00717 if (tree->connection)
00718 #endif
00719 {
00720 if (!already_unlocked)
00721 {
00722 _dbus_verbose ("unlock %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
00723 _dbus_connection_unlock (tree->connection);
00724 }
00725 }
00726
00727 _dbus_string_free (&xml);
00728 dbus_free_string_array (children);
00729 if (reply)
00730 dbus_message_unref (reply);
00731
00732 return result;
00733 }
00734
00748 DBusHandlerResult
00749 _dbus_object_tree_dispatch_and_unlock (DBusObjectTree *tree,
00750 DBusMessage *message)
00751 {
00752 char **path;
00753 dbus_bool_t exact_match;
00754 DBusList *list;
00755 DBusList *link;
00756 DBusHandlerResult result;
00757 DBusObjectSubtree *subtree;
00758
00759 #if 0
00760 _dbus_verbose ("Dispatch of message by object path\n");
00761 #endif
00762
00763 path = NULL;
00764 if (!dbus_message_get_path_decomposed (message, &path))
00765 {
00766 #ifdef DBUS_BUILD_TESTS
00767 if (tree->connection)
00768 #endif
00769 {
00770 _dbus_verbose ("unlock %s\n", _DBUS_FUNCTION_NAME);
00771 _dbus_connection_unlock (tree->connection);
00772 }
00773
00774 _dbus_verbose ("No memory to get decomposed path\n");
00775
00776 return DBUS_HANDLER_RESULT_NEED_MEMORY;
00777 }
00778
00779 if (path == NULL)
00780 {
00781 #ifdef DBUS_BUILD_TESTS
00782 if (tree->connection)
00783 #endif
00784 {
00785 _dbus_verbose ("unlock %s\n", _DBUS_FUNCTION_NAME);
00786 _dbus_connection_unlock (tree->connection);
00787 }
00788
00789 _dbus_verbose ("No path field in message\n");
00790 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
00791 }
00792
00793
00794 subtree = find_handler (tree, (const char**) path, &exact_match);
00795
00796
00797
00798 list = NULL;
00799
00800 while (subtree != NULL)
00801 {
00802 if (subtree->message_function != NULL && (exact_match || subtree->invoke_as_fallback))
00803 {
00804 _dbus_object_subtree_ref (subtree);
00805
00806
00807 if (!_dbus_list_append (&list, subtree))
00808 {
00809 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
00810 _dbus_object_subtree_unref (subtree);
00811 goto free_and_return;
00812 }
00813 }
00814
00815 exact_match = FALSE;
00816 subtree = subtree->parent;
00817 }
00818
00819 _dbus_verbose ("%d handlers in the path tree for this message\n",
00820 _dbus_list_get_length (&list));
00821
00822
00823
00824 result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
00825
00826 link = _dbus_list_get_first_link (&list);
00827 while (link != NULL)
00828 {
00829 DBusList *next = _dbus_list_get_next_link (&list, link);
00830 subtree = link->data;
00831
00832
00833
00834
00835 if (subtree->message_function)
00836 {
00837 DBusObjectPathMessageFunction message_function;
00838 void *user_data;
00839
00840 message_function = subtree->message_function;
00841 user_data = subtree->user_data;
00842
00843 #if 0
00844 _dbus_verbose (" (invoking a handler)\n");
00845 #endif
00846
00847 #ifdef DBUS_BUILD_TESTS
00848 if (tree->connection)
00849 #endif
00850 {
00851 _dbus_verbose ("unlock %s\n", _DBUS_FUNCTION_NAME);
00852 _dbus_connection_unlock (tree->connection);
00853 }
00854
00855
00856
00857
00858
00859
00860 result = (* message_function) (tree->connection,
00861 message,
00862 user_data);
00863
00864 #ifdef DBUS_BUILD_TESTS
00865 if (tree->connection)
00866 #endif
00867 _dbus_connection_lock (tree->connection);
00868
00869 if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
00870 goto free_and_return;
00871 }
00872
00873 link = next;
00874 }
00875
00876 free_and_return:
00877
00878 if (result == DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
00879 {
00880
00881
00882 result = handle_default_introspect_and_unlock (tree, message,
00883 (const char**) path);
00884 }
00885 else
00886 {
00887 #ifdef DBUS_BUILD_TESTS
00888 if (tree->connection)
00889 #endif
00890 {
00891 _dbus_verbose ("unlock %s\n", _DBUS_FUNCTION_NAME);
00892 _dbus_connection_unlock (tree->connection);
00893 }
00894 }
00895
00896 while (list != NULL)
00897 {
00898 link = _dbus_list_get_first_link (&list);
00899 _dbus_object_subtree_unref (link->data);
00900 _dbus_list_remove_link (&list, link);
00901 }
00902
00903 dbus_free_string_array (path);
00904
00905 return result;
00906 }
00907
00916 void*
00917 _dbus_object_tree_get_user_data_unlocked (DBusObjectTree *tree,
00918 const char **path)
00919 {
00920 dbus_bool_t exact_match;
00921 DBusObjectSubtree *subtree;
00922
00923 _dbus_assert (tree != NULL);
00924 _dbus_assert (path != NULL);
00925
00926
00927 subtree = find_handler (tree, (const char**) path, &exact_match);
00928
00929 if ((subtree == NULL) || !exact_match)
00930 {
00931 _dbus_verbose ("%s: No object at specified path found\n",
00932 _DBUS_FUNCTION_NAME);
00933 return NULL;
00934 }
00935
00936 return subtree->user_data;
00937 }
00938
00945 static DBusObjectSubtree*
00946 allocate_subtree_object (const char *name)
00947 {
00948 int len;
00949 DBusObjectSubtree *subtree;
00950 const size_t front_padding = _DBUS_STRUCT_OFFSET (DBusObjectSubtree, name);
00951
00952 _dbus_assert (name != NULL);
00953
00954 len = strlen (name);
00955
00956 subtree = dbus_malloc (front_padding + (len + 1));
00957
00958 if (subtree == NULL)
00959 return NULL;
00960
00961 memcpy (subtree->name, name, len + 1);
00962
00963 return subtree;
00964 }
00965
00966 static DBusObjectSubtree*
00967 _dbus_object_subtree_new (const char *name,
00968 const DBusObjectPathVTable *vtable,
00969 void *user_data)
00970 {
00971 DBusObjectSubtree *subtree;
00972
00973 subtree = allocate_subtree_object (name);
00974 if (subtree == NULL)
00975 goto oom;
00976
00977 _dbus_assert (name != NULL);
00978
00979 subtree->parent = NULL;
00980
00981 if (vtable)
00982 {
00983 subtree->message_function = vtable->message_function;
00984 subtree->unregister_function = vtable->unregister_function;
00985 }
00986 else
00987 {
00988 subtree->message_function = NULL;
00989 subtree->unregister_function = NULL;
00990 }
00991
00992 subtree->user_data = user_data;
00993 subtree->refcount.value = 1;
00994 subtree->subtrees = NULL;
00995 subtree->n_subtrees = 0;
00996 subtree->subtrees_sorted = TRUE;
00997 subtree->invoke_as_fallback = FALSE;
00998
00999 return subtree;
01000
01001 oom:
01002 if (subtree)
01003 {
01004 dbus_free (subtree);
01005 }
01006
01007 return NULL;
01008 }
01009
01010 static DBusObjectSubtree *
01011 _dbus_object_subtree_ref (DBusObjectSubtree *subtree)
01012 {
01013 _dbus_assert (subtree->refcount.value > 0);
01014 _dbus_atomic_inc (&subtree->refcount);
01015
01016 return subtree;
01017 }
01018
01019 static void
01020 _dbus_object_subtree_unref (DBusObjectSubtree *subtree)
01021 {
01022 _dbus_assert (subtree->refcount.value > 0);
01023
01024 if (_dbus_atomic_dec (&subtree->refcount) == 1)
01025 {
01026 _dbus_assert (subtree->unregister_function == NULL);
01027 _dbus_assert (subtree->message_function == NULL);
01028
01029 dbus_free (subtree->subtrees);
01030 dbus_free (subtree);
01031 }
01032 }
01033
01044 dbus_bool_t
01045 _dbus_object_tree_list_registered_and_unlock (DBusObjectTree *tree,
01046 const char **parent_path,
01047 char ***child_entries)
01048 {
01049 dbus_bool_t result;
01050
01051 result = _dbus_object_tree_list_registered_unlocked (tree,
01052 parent_path,
01053 child_entries);
01054
01055 #ifdef DBUS_BUILD_TESTS
01056 if (tree->connection)
01057 #endif
01058 {
01059 _dbus_verbose ("unlock %s\n", _DBUS_FUNCTION_NAME);
01060 _dbus_connection_unlock (tree->connection);
01061 }
01062
01063 return result;
01064 }
01065
01066
01068 #define VERBOSE_DECOMPOSE 0
01069
01080 dbus_bool_t
01081 _dbus_decompose_path (const char* data,
01082 int len,
01083 char ***path,
01084 int *path_len)
01085 {
01086 char **retval;
01087 int n_components;
01088 int i, j, comp;
01089
01090 _dbus_assert (data != NULL);
01091
01092 #if VERBOSE_DECOMPOSE
01093 _dbus_verbose ("Decomposing path \"%s\"\n",
01094 data);
01095 #endif
01096
01097 n_components = 0;
01098 if (len > 1)
01099 {
01100 i = 0;
01101 while (i < len)
01102 {
01103 if (data[i] == '/')
01104 n_components += 1;
01105 ++i;
01106 }
01107 }
01108
01109 retval = dbus_new0 (char*, n_components + 1);
01110
01111 if (retval == NULL)
01112 return FALSE;
01113
01114 comp = 0;
01115 if (n_components == 0)
01116 i = 1;
01117 else
01118 i = 0;
01119 while (comp < n_components)
01120 {
01121 _dbus_assert (i < len);
01122
01123 if (data[i] == '/')
01124 ++i;
01125 j = i;
01126
01127 while (j < len && data[j] != '/')
01128 ++j;
01129
01130
01131 _dbus_assert (i < j);
01132 _dbus_assert (data[i] != '/');
01133 _dbus_assert (j == len || data[j] == '/');
01134
01135 #if VERBOSE_DECOMPOSE
01136 _dbus_verbose (" (component in [%d,%d))\n",
01137 i, j);
01138 #endif
01139
01140 retval[comp] = _dbus_memdup (&data[i], j - i + 1);
01141 if (retval[comp] == NULL)
01142 {
01143 dbus_free_string_array (retval);
01144 return FALSE;
01145 }
01146 retval[comp][j-i] = '\0';
01147 #if VERBOSE_DECOMPOSE
01148 _dbus_verbose (" (component %d = \"%s\")\n",
01149 comp, retval[comp]);
01150 #endif
01151
01152 ++comp;
01153 i = j;
01154 }
01155 _dbus_assert (i == len);
01156
01157 *path = retval;
01158 if (path_len)
01159 *path_len = n_components;
01160
01161 return TRUE;
01162 }
01163
01166 #ifdef DBUS_BUILD_TESTS
01167 #include "dbus-test.h"
01168 #include <stdio.h>
01169
01170 static char*
01171 flatten_path (const char **path)
01172 {
01173 DBusString str;
01174 char *s;
01175
01176 if (!_dbus_string_init (&str))
01177 return NULL;
01178
01179 if (path[0] == NULL)
01180 {
01181 if (!_dbus_string_append_byte (&str, '/'))
01182 goto nomem;
01183 }
01184 else
01185 {
01186 int i;
01187
01188 i = 0;
01189 while (path[i])
01190 {
01191 if (!_dbus_string_append_byte (&str, '/'))
01192 goto nomem;
01193
01194 if (!_dbus_string_append (&str, path[i]))
01195 goto nomem;
01196
01197 ++i;
01198 }
01199 }
01200
01201 if (!_dbus_string_steal_data (&str, &s))
01202 goto nomem;
01203
01204 _dbus_string_free (&str);
01205
01206 return s;
01207
01208 nomem:
01209 _dbus_string_free (&str);
01210 return NULL;
01211 }
01212
01213
01214 typedef enum
01215 {
01216 STR_EQUAL,
01217 STR_PREFIX,
01218 STR_DIFFERENT
01219 } StrComparison;
01220
01221
01222
01223 static StrComparison
01224 path_contains (const char **container,
01225 const char **child)
01226 {
01227 int i;
01228
01229 i = 0;
01230 while (child[i] != NULL)
01231 {
01232 int v;
01233
01234 if (container[i] == NULL)
01235 return STR_PREFIX;
01236
01237
01238
01239
01240 _dbus_assert (container[i] != NULL);
01241 _dbus_assert (child[i] != NULL);
01242
01243 v = strcmp (container[i], child[i]);
01244
01245 if (v != 0)
01246 return STR_DIFFERENT;
01247
01248
01249
01250 ++i;
01251 }
01252
01253
01254
01255
01256 if (container[i] == NULL)
01257 return STR_EQUAL;
01258 else
01259 return STR_DIFFERENT;
01260 }
01261
01262 #if 0
01263 static void
01264 spew_subtree_recurse (DBusObjectSubtree *subtree,
01265 int indent)
01266 {
01267 int i;
01268
01269 i = 0;
01270 while (i < indent)
01271 {
01272 _dbus_verbose (" ");
01273 ++i;
01274 }
01275
01276 _dbus_verbose ("%s (%d children)\n",
01277 subtree->name, subtree->n_subtrees);
01278
01279 i = 0;
01280 while (i < subtree->n_subtrees)
01281 {
01282 spew_subtree_recurse (subtree->subtrees[i], indent + 2);
01283
01284 ++i;
01285 }
01286 }
01287
01288 static void
01289 spew_tree (DBusObjectTree *tree)
01290 {
01291 spew_subtree_recurse (tree->root, 0);
01292 }
01293 #endif
01294
01298 typedef struct
01299 {
01300 const char **path;
01301 dbus_bool_t handler_fallback;
01302 dbus_bool_t message_handled;
01303 dbus_bool_t handler_unregistered;
01304 } TreeTestData;
01305
01306
01307 static void
01308 test_unregister_function (DBusConnection *connection,
01309 void *user_data)
01310 {
01311 TreeTestData *ttd = user_data;
01312
01313 ttd->handler_unregistered = TRUE;
01314 }
01315
01316 static DBusHandlerResult
01317 test_message_function (DBusConnection *connection,
01318 DBusMessage *message,
01319 void *user_data)
01320 {
01321 TreeTestData *ttd = user_data;
01322
01323 ttd->message_handled = TRUE;
01324
01325 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
01326 }
01327
01328 static dbus_bool_t
01329 do_register (DBusObjectTree *tree,
01330 const char **path,
01331 dbus_bool_t fallback,
01332 int i,
01333 TreeTestData *tree_test_data)
01334 {
01335 DBusObjectPathVTable vtable = { test_unregister_function,
01336 test_message_function, NULL };
01337
01338 tree_test_data[i].message_handled = FALSE;
01339 tree_test_data[i].handler_unregistered = FALSE;
01340 tree_test_data[i].handler_fallback = fallback;
01341 tree_test_data[i].path = path;
01342
01343 if (!_dbus_object_tree_register (tree, fallback, path,
01344 &vtable,
01345 &tree_test_data[i]))
01346 return FALSE;
01347
01348 _dbus_assert (_dbus_object_tree_get_user_data_unlocked (tree, path) ==
01349 &tree_test_data[i]);
01350
01351 return TRUE;
01352 }
01353
01354 static dbus_bool_t
01355 do_test_dispatch (DBusObjectTree *tree,
01356 const char **path,
01357 int i,
01358 TreeTestData *tree_test_data,
01359 int n_test_data)
01360 {
01361 DBusMessage *message;
01362 int j;
01363 DBusHandlerResult result;
01364 char *flat;
01365
01366 message = NULL;
01367
01368 flat = flatten_path (path);
01369 if (flat == NULL)
01370 goto oom;
01371
01372 message = dbus_message_new_method_call (NULL,
01373 flat,
01374 "org.freedesktop.TestInterface",
01375 "Foo");
01376 dbus_free (flat);
01377 if (message == NULL)
01378 goto oom;
01379
01380 j = 0;
01381 while (j < n_test_data)
01382 {
01383 tree_test_data[j].message_handled = FALSE;
01384 ++j;
01385 }
01386
01387 result = _dbus_object_tree_dispatch_and_unlock (tree, message);
01388 if (result == DBUS_HANDLER_RESULT_NEED_MEMORY)
01389 goto oom;
01390
01391 _dbus_assert (tree_test_data[i].message_handled);
01392
01393 j = 0;
01394 while (j < n_test_data)
01395 {
01396 if (tree_test_data[j].message_handled)
01397 {
01398 if (tree_test_data[j].handler_fallback)
01399 _dbus_assert (path_contains (tree_test_data[j].path,
01400 path) != STR_DIFFERENT);
01401 else
01402 _dbus_assert (path_contains (tree_test_data[j].path, path) == STR_EQUAL);
01403 }
01404 else
01405 {
01406 if (tree_test_data[j].handler_fallback)
01407 _dbus_assert (path_contains (tree_test_data[j].path,
01408 path) == STR_DIFFERENT);
01409 else
01410 _dbus_assert (path_contains (tree_test_data[j].path, path) != STR_EQUAL);
01411 }
01412
01413 ++j;
01414 }
01415
01416 dbus_message_unref (message);
01417
01418 return TRUE;
01419
01420 oom:
01421 if (message)
01422 dbus_message_unref (message);
01423 return FALSE;
01424 }
01425
01426 static size_t
01427 string_array_length (const char **array)
01428 {
01429 size_t i;
01430 for (i = 0; array[i]; i++) ;
01431 return i;
01432 }
01433
01434 typedef struct
01435 {
01436 const char *path;
01437 const char *result[20];
01438 } DecomposePathTest;
01439
01440 static DecomposePathTest decompose_tests[] = {
01441 { "/foo", { "foo", NULL } },
01442 { "/foo/bar", { "foo", "bar", NULL } },
01443 { "/", { NULL } },
01444 { "/a/b", { "a", "b", NULL } },
01445 { "/a/b/c", { "a", "b", "c", NULL } },
01446 { "/a/b/c/d", { "a", "b", "c", "d", NULL } },
01447 { "/foo/bar/q", { "foo", "bar", "q", NULL } },
01448 { "/foo/bar/this/is/longer", { "foo", "bar", "this", "is", "longer", NULL } }
01449 };
01450
01451 static dbus_bool_t
01452 run_decompose_tests (void)
01453 {
01454 int i;
01455
01456 i = 0;
01457 while (i < _DBUS_N_ELEMENTS (decompose_tests))
01458 {
01459 char **result;
01460 int result_len;
01461 int expected_len;
01462
01463 if (!_dbus_decompose_path (decompose_tests[i].path,
01464 strlen (decompose_tests[i].path),
01465 &result, &result_len))
01466 return FALSE;
01467
01468 expected_len = string_array_length (decompose_tests[i].result);
01469
01470 if (result_len != (int) string_array_length ((const char**)result) ||
01471 expected_len != result_len ||
01472 path_contains (decompose_tests[i].result,
01473 (const char**) result) != STR_EQUAL)
01474 {
01475 int real_len = string_array_length ((const char**)result);
01476 _dbus_warn ("Expected decompose of %s to have len %d, returned %d, appears to have %d\n",
01477 decompose_tests[i].path, expected_len, result_len,
01478 real_len);
01479 _dbus_warn ("Decompose resulted in elements: { ");
01480 i = 0;
01481 while (i < real_len)
01482 {
01483 _dbus_warn ("\"%s\"%s", result[i],
01484 (i + 1) == real_len ? "" : ", ");
01485 ++i;
01486 }
01487 _dbus_warn ("}\n");
01488 _dbus_assert_not_reached ("path decompose failed\n");
01489 }
01490
01491 dbus_free_string_array (result);
01492
01493 ++i;
01494 }
01495
01496 return TRUE;
01497 }
01498
01499 static dbus_bool_t
01500 object_tree_test_iteration (void *data)
01501 {
01502 const char *path0[] = { NULL };
01503 const char *path1[] = { "foo", NULL };
01504 const char *path2[] = { "foo", "bar", NULL };
01505 const char *path3[] = { "foo", "bar", "baz", NULL };
01506 const char *path4[] = { "foo", "bar", "boo", NULL };
01507 const char *path5[] = { "blah", NULL };
01508 const char *path6[] = { "blah", "boof", NULL };
01509 const char *path7[] = { "blah", "boof", "this", "is", "really", "long", NULL };
01510 const char *path8[] = { "childless", NULL };
01511 DBusObjectTree *tree;
01512 TreeTestData tree_test_data[9];
01513 int i;
01514 dbus_bool_t exact_match;
01515
01516 if (!run_decompose_tests ())
01517 return FALSE;
01518
01519 tree = NULL;
01520
01521 tree = _dbus_object_tree_new (NULL);
01522 if (tree == NULL)
01523 goto out;
01524
01525 if (!do_register (tree, path0, TRUE, 0, tree_test_data))
01526 goto out;
01527
01528 _dbus_assert (find_subtree (tree, path0, NULL));
01529 _dbus_assert (!find_subtree (tree, path1, NULL));
01530 _dbus_assert (!find_subtree (tree, path2, NULL));
01531 _dbus_assert (!find_subtree (tree, path3, NULL));
01532 _dbus_assert (!find_subtree (tree, path4, NULL));
01533 _dbus_assert (!find_subtree (tree, path5, NULL));
01534 _dbus_assert (!find_subtree (tree, path6, NULL));
01535 _dbus_assert (!find_subtree (tree, path7, NULL));
01536 _dbus_assert (!find_subtree (tree, path8, NULL));
01537
01538 _dbus_assert (find_handler (tree, path0, &exact_match) && exact_match);
01539 _dbus_assert (find_handler (tree, path1, &exact_match) == tree->root && !exact_match);
01540 _dbus_assert (find_handler (tree, path2, &exact_match) == tree->root && !exact_match);
01541 _dbus_assert (find_handler (tree, path3, &exact_match) == tree->root && !exact_match);
01542 _dbus_assert (find_handler (tree, path4, &exact_match) == tree->root && !exact_match);
01543 _dbus_assert (find_handler (tree, path5, &exact_match) == tree->root && !exact_match);
01544 _dbus_assert (find_handler (tree, path6, &exact_match) == tree->root && !exact_match);
01545 _dbus_assert (find_handler (tree, path7, &exact_match) == tree->root && !exact_match);
01546 _dbus_assert (find_handler (tree, path8, &exact_match) == tree->root && !exact_match);
01547
01548 if (!do_register (tree, path1, TRUE, 1, tree_test_data))
01549 goto out;
01550
01551 _dbus_assert (find_subtree (tree, path0, NULL));
01552 _dbus_assert (find_subtree (tree, path1, NULL));
01553 _dbus_assert (!find_subtree (tree, path2, NULL));
01554 _dbus_assert (!find_subtree (tree, path3, NULL));
01555 _dbus_assert (!find_subtree (tree, path4, NULL));
01556 _dbus_assert (!find_subtree (tree, path5, NULL));
01557 _dbus_assert (!find_subtree (tree, path6, NULL));
01558 _dbus_assert (!find_subtree (tree, path7, NULL));
01559 _dbus_assert (!find_subtree (tree, path8, NULL));
01560
01561 _dbus_assert (find_handler (tree, path0, &exact_match) && exact_match);
01562 _dbus_assert (find_handler (tree, path1, &exact_match) && exact_match);
01563 _dbus_assert (find_handler (tree, path2, &exact_match) && !exact_match);
01564 _dbus_assert (find_handler (tree, path3, &exact_match) && !exact_match);
01565 _dbus_assert (find_handler (tree, path4, &exact_match) && !exact_match);
01566 _dbus_assert (find_handler (tree, path5, &exact_match) == tree->root && !exact_match);
01567 _dbus_assert (find_handler (tree, path6, &exact_match) == tree->root && !exact_match);
01568 _dbus_assert (find_handler (tree, path7, &exact_match) == tree->root && !exact_match);
01569 _dbus_assert (find_handler (tree, path8, &exact_match) == tree->root && !exact_match);
01570
01571 if (!do_register (tree, path2, TRUE, 2, tree_test_data))
01572 goto out;
01573
01574 _dbus_assert (find_subtree (tree, path1, NULL));
01575 _dbus_assert (find_subtree (tree, path2, NULL));
01576 _dbus_assert (!find_subtree (tree, path3, NULL));
01577 _dbus_assert (!find_subtree (tree, path4, NULL));
01578 _dbus_assert (!find_subtree (tree, path5, NULL));
01579 _dbus_assert (!find_subtree (tree, path6, NULL));
01580 _dbus_assert (!find_subtree (tree, path7, NULL));
01581 _dbus_assert (!find_subtree (tree, path8, NULL));
01582
01583 if (!do_register (tree, path3, TRUE, 3, tree_test_data))
01584 goto out;
01585
01586 _dbus_assert (find_subtree (tree, path0, NULL));
01587 _dbus_assert (find_subtree (tree, path1, NULL));
01588 _dbus_assert (find_subtree (tree, path2, NULL));
01589 _dbus_assert (find_subtree (tree, path3, NULL));
01590 _dbus_assert (!find_subtree (tree, path4, NULL));
01591 _dbus_assert (!find_subtree (tree, path5, NULL));
01592 _dbus_assert (!find_subtree (tree, path6, NULL));
01593 _dbus_assert (!find_subtree (tree, path7, NULL));
01594 _dbus_assert (!find_subtree (tree, path8, NULL));
01595
01596 if (!do_register (tree, path4, TRUE, 4, tree_test_data))
01597 goto out;
01598
01599 _dbus_assert (find_subtree (tree, path0, NULL));
01600 _dbus_assert (find_subtree (tree, path1, NULL));
01601 _dbus_assert (find_subtree (tree, path2, NULL));
01602 _dbus_assert (find_subtree (tree, path3, NULL));
01603 _dbus_assert (find_subtree (tree, path4, NULL));
01604 _dbus_assert (!find_subtree (tree, path5, NULL));
01605 _dbus_assert (!find_subtree (tree, path6, NULL));
01606 _dbus_assert (!find_subtree (tree, path7, NULL));
01607 _dbus_assert (!find_subtree (tree, path8, NULL));
01608
01609 if (!do_register (tree, path5, TRUE, 5, tree_test_data))
01610 goto out;
01611
01612 _dbus_assert (find_subtree (tree, path0, NULL));
01613 _dbus_assert (find_subtree (tree, path1, NULL));
01614 _dbus_assert (find_subtree (tree, path2, NULL));
01615 _dbus_assert (find_subtree (tree, path3, NULL));
01616 _dbus_assert (find_subtree (tree, path4, NULL));
01617 _dbus_assert (find_subtree (tree, path5, NULL));
01618 _dbus_assert (!find_subtree (tree, path6, NULL));
01619 _dbus_assert (!find_subtree (tree, path7, NULL));
01620 _dbus_assert (!find_subtree (tree, path8, NULL));
01621
01622 _dbus_assert (find_handler (tree, path0, &exact_match) == tree->root && exact_match);
01623 _dbus_assert (find_handler (tree, path1, &exact_match) != tree->root && exact_match);
01624 _dbus_assert (find_handler (tree, path2, &exact_match) != tree->root && exact_match);
01625 _dbus_assert (find_handler (tree, path3, &exact_match) != tree->root && exact_match);
01626 _dbus_assert (find_handler (tree, path4, &exact_match) != tree->root && exact_match);
01627 _dbus_assert (find_handler (tree, path5, &exact_match) != tree->root && exact_match);
01628 _dbus_assert (find_handler (tree, path6, &exact_match) != tree->root && !exact_match);
01629 _dbus_assert (find_handler (tree, path7, &exact_match) != tree->root && !exact_match);
01630 _dbus_assert (find_handler (tree, path8, &exact_match) == tree->root && !exact_match);
01631
01632 if (!do_register (tree, path6, TRUE, 6, tree_test_data))
01633 goto out;
01634
01635 _dbus_assert (find_subtree (tree, path0, NULL));
01636 _dbus_assert (find_subtree (tree, path1, NULL));
01637 _dbus_assert (find_subtree (tree, path2, NULL));
01638 _dbus_assert (find_subtree (tree, path3, NULL));
01639 _dbus_assert (find_subtree (tree, path4, NULL));
01640 _dbus_assert (find_subtree (tree, path5, NULL));
01641 _dbus_assert (find_subtree (tree, path6, NULL));
01642 _dbus_assert (!find_subtree (tree, path7, NULL));
01643 _dbus_assert (!find_subtree (tree, path8, NULL));
01644
01645 if (!do_register (tree, path7, TRUE, 7, tree_test_data))
01646 goto out;
01647
01648 _dbus_assert (find_subtree (tree, path0, NULL));
01649 _dbus_assert (find_subtree (tree, path1, NULL));
01650 _dbus_assert (find_subtree (tree, path2, NULL));
01651 _dbus_assert (find_subtree (tree, path3, NULL));
01652 _dbus_assert (find_subtree (tree, path4, NULL));
01653 _dbus_assert (find_subtree (tree, path5, NULL));
01654 _dbus_assert (find_subtree (tree, path6, NULL));
01655 _dbus_assert (find_subtree (tree, path7, NULL));
01656 _dbus_assert (!find_subtree (tree, path8, NULL));
01657
01658 if (!do_register (tree, path8, TRUE, 8, tree_test_data))
01659 goto out;
01660
01661 _dbus_assert (find_subtree (tree, path0, NULL));
01662 _dbus_assert (find_subtree (tree, path1, NULL));
01663 _dbus_assert (find_subtree (tree, path2, NULL));
01664 _dbus_assert (find_subtree (tree, path3, NULL));
01665 _dbus_assert (find_subtree (tree, path4, NULL));
01666 _dbus_assert (find_subtree (tree, path5, NULL));
01667 _dbus_assert (find_subtree (tree, path6, NULL));
01668 _dbus_assert (find_subtree (tree, path7, NULL));
01669 _dbus_assert (find_subtree (tree, path8, NULL));
01670
01671 _dbus_assert (find_handler (tree, path0, &exact_match) == tree->root && exact_match);
01672 _dbus_assert (find_handler (tree, path1, &exact_match) != tree->root && exact_match);
01673 _dbus_assert (find_handler (tree, path2, &exact_match) != tree->root && exact_match);
01674 _dbus_assert (find_handler (tree, path3, &exact_match) != tree->root && exact_match);
01675 _dbus_assert (find_handler (tree, path4, &exact_match) != tree->root && exact_match);
01676 _dbus_assert (find_handler (tree, path5, &exact_match) != tree->root && exact_match);
01677 _dbus_assert (find_handler (tree, path6, &exact_match) != tree->root && exact_match);
01678 _dbus_assert (find_handler (tree, path7, &exact_match) != tree->root && exact_match);
01679 _dbus_assert (find_handler (tree, path8, &exact_match) != tree->root && exact_match);
01680
01681
01682
01683 {
01684 const char *root[] = { NULL };
01685 char **child_entries;
01686 int nb;
01687
01688 _dbus_object_tree_list_registered_unlocked (tree, path1, &child_entries);
01689 if (child_entries != NULL)
01690 {
01691 nb = string_array_length ((const char**)child_entries);
01692 _dbus_assert (nb == 1);
01693 dbus_free_string_array (child_entries);
01694 }
01695
01696 _dbus_object_tree_list_registered_unlocked (tree, path2, &child_entries);
01697 if (child_entries != NULL)
01698 {
01699 nb = string_array_length ((const char**)child_entries);
01700 _dbus_assert (nb == 2);
01701 dbus_free_string_array (child_entries);
01702 }
01703
01704 _dbus_object_tree_list_registered_unlocked (tree, path8, &child_entries);
01705 if (child_entries != NULL)
01706 {
01707 nb = string_array_length ((const char**)child_entries);
01708 _dbus_assert (nb == 0);
01709 dbus_free_string_array (child_entries);
01710 }
01711
01712 _dbus_object_tree_list_registered_unlocked (tree, root, &child_entries);
01713 if (child_entries != NULL)
01714 {
01715 nb = string_array_length ((const char**)child_entries);
01716 _dbus_assert (nb == 3);
01717 dbus_free_string_array (child_entries);
01718 }
01719 }
01720
01721
01722 _dbus_object_tree_unref (tree);
01723
01724 i = 0;
01725 while (i < (int) _DBUS_N_ELEMENTS (tree_test_data))
01726 {
01727 _dbus_assert (tree_test_data[i].handler_unregistered);
01728 _dbus_assert (!tree_test_data[i].message_handled);
01729 ++i;
01730 }
01731
01732
01733 tree = _dbus_object_tree_new (NULL);
01734 if (tree == NULL)
01735 goto out;
01736
01737 if (!do_register (tree, path0, TRUE, 0, tree_test_data))
01738 goto out;
01739 if (!do_register (tree, path1, TRUE, 1, tree_test_data))
01740 goto out;
01741 if (!do_register (tree, path2, TRUE, 2, tree_test_data))
01742 goto out;
01743 if (!do_register (tree, path3, TRUE, 3, tree_test_data))
01744 goto out;
01745 if (!do_register (tree, path4, TRUE, 4, tree_test_data))
01746 goto out;
01747 if (!do_register (tree, path5, TRUE, 5, tree_test_data))
01748 goto out;
01749 if (!do_register (tree, path6, TRUE, 6, tree_test_data))
01750 goto out;
01751 if (!do_register (tree, path7, TRUE, 7, tree_test_data))
01752 goto out;
01753 if (!do_register (tree, path8, TRUE, 8, tree_test_data))
01754 goto out;
01755
01756 _dbus_object_tree_unregister_and_unlock (tree, path0);
01757 _dbus_assert (_dbus_object_tree_get_user_data_unlocked (tree, path0) == NULL);
01758
01759 _dbus_assert (!find_subtree (tree, path0, NULL));
01760 _dbus_assert (find_subtree (tree, path1, NULL));
01761 _dbus_assert (find_subtree (tree, path2, NULL));
01762 _dbus_assert (find_subtree (tree, path3, NULL));
01763 _dbus_assert (find_subtree (tree, path4, NULL));
01764 _dbus_assert (find_subtree (tree, path5, NULL));
01765 _dbus_assert (find_subtree (tree, path6, NULL));
01766 _dbus_assert (find_subtree (tree, path7, NULL));
01767 _dbus_assert (find_subtree (tree, path8, NULL));
01768
01769 _dbus_object_tree_unregister_and_unlock (tree, path1);
01770 _dbus_assert (_dbus_object_tree_get_user_data_unlocked (tree, path1) == NULL);
01771
01772 _dbus_assert (!find_subtree (tree, path0, NULL));
01773 _dbus_assert (!find_subtree (tree, path1, NULL));
01774 _dbus_assert (find_subtree (tree, path2, NULL));
01775 _dbus_assert (find_subtree (tree, path3, NULL));
01776 _dbus_assert (find_subtree (tree, path4, NULL));
01777 _dbus_assert (find_subtree (tree, path5, NULL));
01778 _dbus_assert (find_subtree (tree, path6, NULL));
01779 _dbus_assert (find_subtree (tree, path7, NULL));
01780 _dbus_assert (find_subtree (tree, path8, NULL));
01781
01782 _dbus_object_tree_unregister_and_unlock (tree, path2);
01783 _dbus_assert (_dbus_object_tree_get_user_data_unlocked (tree, path2) == NULL);
01784
01785 _dbus_assert (!find_subtree (tree, path0, NULL));
01786 _dbus_assert (!find_subtree (tree, path1, NULL));
01787 _dbus_assert (!find_subtree (tree, path2, NULL));
01788 _dbus_assert (find_subtree (tree, path3, NULL));
01789 _dbus_assert (find_subtree (tree, path4, NULL));
01790 _dbus_assert (find_subtree (tree, path5, NULL));
01791 _dbus_assert (find_subtree (tree, path6, NULL));
01792 _dbus_assert (find_subtree (tree, path7, NULL));
01793 _dbus_assert (find_subtree (tree, path8, NULL));
01794
01795 _dbus_object_tree_unregister_and_unlock (tree, path3);
01796 _dbus_assert (_dbus_object_tree_get_user_data_unlocked (tree, path3) == NULL);
01797
01798 _dbus_assert (!find_subtree (tree, path0, NULL));
01799 _dbus_assert (!find_subtree (tree, path1, NULL));
01800 _dbus_assert (!find_subtree (tree, path2, NULL));
01801 _dbus_assert (!find_subtree (tree, path3, NULL));
01802 _dbus_assert (find_subtree (tree, path4, NULL));
01803 _dbus_assert (find_subtree (tree, path5, NULL));
01804 _dbus_assert (find_subtree (tree, path6, NULL));
01805 _dbus_assert (find_subtree (tree, path7, NULL));
01806 _dbus_assert (find_subtree (tree, path8, NULL));
01807
01808 _dbus_object_tree_unregister_and_unlock (tree, path4);
01809 _dbus_assert (_dbus_object_tree_get_user_data_unlocked (tree, path4) == NULL);
01810
01811 _dbus_assert (!find_subtree (tree, path0, NULL));
01812 _dbus_assert (!find_subtree (tree, path1, NULL));
01813 _dbus_assert (!find_subtree (tree, path2, NULL));
01814 _dbus_assert (!find_subtree (tree, path3, NULL));
01815 _dbus_assert (!find_subtree (tree, path4, NULL));
01816 _dbus_assert (find_subtree (tree, path5, NULL));
01817 _dbus_assert (find_subtree (tree, path6, NULL));
01818 _dbus_assert (find_subtree (tree, path7, NULL));
01819 _dbus_assert (find_subtree (tree, path8, NULL));
01820
01821 _dbus_object_tree_unregister_and_unlock (tree, path5);
01822 _dbus_assert (_dbus_object_tree_get_user_data_unlocked (tree, path5) == NULL);
01823
01824 _dbus_assert (!find_subtree (tree, path0, NULL));
01825 _dbus_assert (!find_subtree (tree, path1, NULL));
01826 _dbus_assert (!find_subtree (tree, path2, NULL));
01827 _dbus_assert (!find_subtree (tree, path3, NULL));
01828 _dbus_assert (!find_subtree (tree, path4, NULL));
01829 _dbus_assert (!find_subtree (tree, path5, NULL));
01830 _dbus_assert (find_subtree (tree, path6, NULL));
01831 _dbus_assert (find_subtree (tree, path7, NULL));
01832 _dbus_assert (find_subtree (tree, path8, NULL));
01833
01834 _dbus_object_tree_unregister_and_unlock (tree, path6);
01835 _dbus_assert (_dbus_object_tree_get_user_data_unlocked (tree, path6) == NULL);
01836
01837 _dbus_assert (!find_subtree (tree, path0, NULL));
01838 _dbus_assert (!find_subtree (tree, path1, NULL));
01839 _dbus_assert (!find_subtree (tree, path2, NULL));
01840 _dbus_assert (!find_subtree (tree, path3, NULL));
01841 _dbus_assert (!find_subtree (tree, path4, NULL));
01842 _dbus_assert (!find_subtree (tree, path5, NULL));
01843 _dbus_assert (!find_subtree (tree, path6, NULL));
01844 _dbus_assert (find_subtree (tree, path7, NULL));
01845 _dbus_assert (find_subtree (tree, path8, NULL));
01846
01847 _dbus_object_tree_unregister_and_unlock (tree, path7);
01848 _dbus_assert (_dbus_object_tree_get_user_data_unlocked (tree, path7) == NULL);
01849
01850 _dbus_assert (!find_subtree (tree, path0, NULL));
01851 _dbus_assert (!find_subtree (tree, path1, NULL));
01852 _dbus_assert (!find_subtree (tree, path2, NULL));
01853 _dbus_assert (!find_subtree (tree, path3, NULL));
01854 _dbus_assert (!find_subtree (tree, path4, NULL));
01855 _dbus_assert (!find_subtree (tree, path5, NULL));
01856 _dbus_assert (!find_subtree (tree, path6, NULL));
01857 _dbus_assert (!find_subtree (tree, path7, NULL));
01858 _dbus_assert (find_subtree (tree, path8, NULL));
01859
01860 _dbus_object_tree_unregister_and_unlock (tree, path8);
01861 _dbus_assert (_dbus_object_tree_get_user_data_unlocked (tree, path8) == NULL);
01862
01863 _dbus_assert (!find_subtree (tree, path0, NULL));
01864 _dbus_assert (!find_subtree (tree, path1, NULL));
01865 _dbus_assert (!find_subtree (tree, path2, NULL));
01866 _dbus_assert (!find_subtree (tree, path3, NULL));
01867 _dbus_assert (!find_subtree (tree, path4, NULL));
01868 _dbus_assert (!find_subtree (tree, path5, NULL));
01869 _dbus_assert (!find_subtree (tree, path6, NULL));
01870 _dbus_assert (!find_subtree (tree, path7, NULL));
01871 _dbus_assert (!find_subtree (tree, path8, NULL));
01872
01873 i = 0;
01874 while (i < (int) _DBUS_N_ELEMENTS (tree_test_data))
01875 {
01876 _dbus_assert (tree_test_data[i].handler_unregistered);
01877 _dbus_assert (!tree_test_data[i].message_handled);
01878 ++i;
01879 }
01880
01881
01882
01883 if (!do_register (tree, path0, TRUE, 0, tree_test_data))
01884 goto out;
01885 if (!do_register (tree, path1, FALSE, 1, tree_test_data))
01886 goto out;
01887 if (!do_register (tree, path2, TRUE, 2, tree_test_data))
01888 goto out;
01889 if (!do_register (tree, path3, TRUE, 3, tree_test_data))
01890 goto out;
01891 if (!do_register (tree, path4, TRUE, 4, tree_test_data))
01892 goto out;
01893 if (!do_register (tree, path5, TRUE, 5, tree_test_data))
01894 goto out;
01895 if (!do_register (tree, path6, FALSE, 6, tree_test_data))
01896 goto out;
01897 if (!do_register (tree, path7, TRUE, 7, tree_test_data))
01898 goto out;
01899 if (!do_register (tree, path8, TRUE, 8, tree_test_data))
01900 goto out;
01901
01902 #if 0
01903 spew_tree (tree);
01904 #endif
01905
01906 if (!do_test_dispatch (tree, path0, 0, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
01907 goto out;
01908 if (!do_test_dispatch (tree, path1, 1, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
01909 goto out;
01910 if (!do_test_dispatch (tree, path2, 2, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
01911 goto out;
01912 if (!do_test_dispatch (tree, path3, 3, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
01913 goto out;
01914 if (!do_test_dispatch (tree, path4, 4, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
01915 goto out;
01916 if (!do_test_dispatch (tree, path5, 5, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
01917 goto out;
01918 if (!do_test_dispatch (tree, path6, 6, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
01919 goto out;
01920 if (!do_test_dispatch (tree, path7, 7, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
01921 goto out;
01922 if (!do_test_dispatch (tree, path8, 8, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
01923 goto out;
01924
01925 out:
01926 if (tree)
01927 {
01928
01929 _dbus_object_tree_ref (tree);
01930 _dbus_object_tree_unref (tree);
01931 _dbus_object_tree_unref (tree);
01932 }
01933
01934 return TRUE;
01935 }
01936
01942 dbus_bool_t
01943 _dbus_object_tree_test (void)
01944 {
01945 _dbus_test_oom_handling ("object tree",
01946 object_tree_test_iteration,
01947 NULL);
01948
01949 return TRUE;
01950 }
01951
01952 #endif