1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/accessibility/ax_tree.h"
9 #include "base/logging.h"
10 #include "base/strings/stringprintf.h"
11 #include "ui/accessibility/ax_node.h"
17 std::string
TreeToStringHelper(AXNode
* node
, int indent
) {
19 for (int i
= 0; i
< indent
; i
++)
21 result
+= node
->data().ToString() + "\n";
22 for (int i
= 0; i
< node
->child_count(); ++i
)
23 result
+= TreeToStringHelper(node
->ChildAtIndex(i
), indent
+ 1);
27 } // anonymous namespace
33 root
.role
= AX_ROLE_ROOT_WEB_AREA
;
35 AXTreeUpdate initial_state
;
36 initial_state
.nodes
.push_back(root
);
37 CHECK(Unserialize(initial_state
)) << error();
40 AXTree::AXTree(const AXTreeUpdate
& initial_state
)
42 CHECK(Unserialize(initial_state
)) << error();
47 DestroyNodeAndSubtree(root_
);
50 AXNode
* AXTree::GetRoot() const {
54 AXNode
* AXTree::GetFromId(int32 id
) const {
55 base::hash_map
<int32
, AXNode
*>::const_iterator iter
= id_map_
.find(id
);
56 return iter
!= id_map_
.end() ? (iter
->second
) : NULL
;
59 bool AXTree::Unserialize(const AXTreeUpdate
& update
) {
60 std::set
<AXNode
*> pending_nodes
;
62 if (update
.node_id_to_clear
!= 0) {
63 AXNode
* node
= GetFromId(update
.node_id_to_clear
);
65 error_
= base::StringPrintf("Bad node_id_to_clear: %d",
66 update
.node_id_to_clear
);
70 DestroyNodeAndSubtree(root_
);
73 for (int i
= 0; i
< node
->child_count(); ++i
)
74 DestroyNodeAndSubtree(node
->ChildAtIndex(i
));
75 std::vector
<AXNode
*> children
;
76 node
->SwapChildren(children
);
77 pending_nodes
.insert(node
);
81 for (size_t i
= 0; i
< update
.nodes
.size(); ++i
) {
82 if (!UpdateNode(update
.nodes
[i
], &pending_nodes
))
86 if (!pending_nodes
.empty()) {
87 error_
= "Nodes left pending by the update:";
88 for (std::set
<AXNode
*>::iterator iter
= pending_nodes
.begin();
89 iter
!= pending_nodes
.end(); ++iter
) {
90 error_
+= base::StringPrintf(" %d", (*iter
)->id());
98 std::string
AXTree::ToString() const {
99 return TreeToStringHelper(root_
, 0);
102 AXNode
* AXTree::CreateNode(AXNode
* parent
, int32 id
, int32 index_in_parent
) {
103 return new AXNode(parent
, id
, index_in_parent
);
106 bool AXTree::UpdateNode(
107 const AXNodeData
& src
, std::set
<AXNode
*>* pending_nodes
) {
108 // This method updates one node in the tree based on serialized data
109 // received in an AXTreeUpdate. See AXTreeUpdate for pre and post
112 // Look up the node by id. If it's not found, then either the root
113 // of the tree is being swapped, or we're out of sync with the source
114 // and this is a serious error.
115 AXNode
* node
= GetFromId(src
.id
);
117 pending_nodes
->erase(node
);
119 if (src
.role
!= AX_ROLE_ROOT_WEB_AREA
) {
120 error_
= base::StringPrintf(
121 "%d is not in the tree and not the new root", src
.id
);
124 node
= CreateAndInitializeNode(NULL
, src
.id
, 0);
127 // Set the node's data.
130 // First, delete nodes that used to be children of this node but aren't
132 if (!DeleteOldChildren(node
, src
.child_ids
))
135 // Now build a new children vector, reusing nodes when possible,
137 std::vector
<AXNode
*> new_children
;
138 bool success
= CreateNewChildVector(
139 node
, src
.child_ids
, &new_children
, pending_nodes
);
140 node
->SwapChildren(new_children
);
142 // Update the root of the tree if needed.
143 if (src
.role
== AX_ROLE_ROOT_WEB_AREA
&&
144 (!root_
|| root_
->id() != src
.id
)) {
146 DestroyNodeAndSubtree(root_
);
154 void AXTree::OnRootChanged() {
157 AXNode
* AXTree::CreateAndInitializeNode(
158 AXNode
* parent
, int32 id
, int32 index_in_parent
) {
159 AXNode
* node
= CreateNode(parent
, id
, index_in_parent
);
160 id_map_
[node
->id()] = node
;
164 void AXTree::DestroyNodeAndSubtree(AXNode
* node
) {
165 id_map_
.erase(node
->id());
166 for (int i
= 0; i
< node
->child_count(); ++i
)
167 DestroyNodeAndSubtree(node
->ChildAtIndex(i
));
171 bool AXTree::DeleteOldChildren(AXNode
* node
,
172 const std::vector
<int32
> new_child_ids
) {
173 // Create a set of child ids in |src| for fast lookup, and return false
174 // if a duplicate is found;
175 std::set
<int32
> new_child_id_set
;
176 for (size_t i
= 0; i
< new_child_ids
.size(); ++i
) {
177 if (new_child_id_set
.find(new_child_ids
[i
]) != new_child_id_set
.end()) {
178 error_
= base::StringPrintf("Node %d has duplicate child id %d",
179 node
->id(), new_child_ids
[i
]);
182 new_child_id_set
.insert(new_child_ids
[i
]);
185 // Delete the old children.
186 const std::vector
<AXNode
*>& old_children
= node
->children();
187 for (size_t i
= 0; i
< old_children
.size(); ++i
) {
188 int old_id
= old_children
[i
]->id();
189 if (new_child_id_set
.find(old_id
) == new_child_id_set
.end())
190 DestroyNodeAndSubtree(old_children
[i
]);
196 bool AXTree::CreateNewChildVector(AXNode
* node
,
197 const std::vector
<int32
> new_child_ids
,
198 std::vector
<AXNode
*>* new_children
,
199 std::set
<AXNode
*>* pending_nodes
) {
201 for (size_t i
= 0; i
< new_child_ids
.size(); ++i
) {
202 int32 child_id
= new_child_ids
[i
];
203 int32 index_in_parent
= static_cast<int32
>(i
);
204 AXNode
* child
= GetFromId(child_id
);
206 if (child
->parent() != node
) {
207 // This is a serious error - nodes should never be reparented.
208 // If this case occurs, continue so this node isn't left in an
209 // inconsistent state, but return failure at the end.
210 error_
= base::StringPrintf(
211 "Node %d reparented from %d to %d",
213 child
->parent() ? child
->parent()->id() : 0,
218 child
->SetIndexInParent(index_in_parent
);
220 child
= CreateAndInitializeNode(node
, child_id
, index_in_parent
);
221 pending_nodes
->insert(child
);
223 new_children
->push_back(child
);