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
) {
18 std::string result
= std::string(2 * indent
, ' ');
19 result
+= node
->data().ToString() + "\n";
20 for (int i
= 0; i
< node
->child_count(); ++i
)
21 result
+= TreeToStringHelper(node
->ChildAtIndex(i
), indent
+ 1);
25 } // anonymous namespace
27 // Intermediate state to keep track of during a tree update.
28 struct AXTreeUpdateState
{
29 // During an update, this keeps track of all nodes that have been
30 // implicitly referenced as part of this update, but haven't been
31 // updated yet. It's an error if there are any pending nodes at the
32 // end of Unserialize.
33 std::set
<AXNode
*> pending_nodes
;
35 // Keeps track of new nodes created during this update.
36 std::set
<AXNode
*> new_nodes
;
39 AXTreeDelegate::AXTreeDelegate() {
42 AXTreeDelegate::~AXTreeDelegate() {
46 : delegate_(NULL
), root_(NULL
) {
49 root
.role
= AX_ROLE_ROOT_WEB_AREA
;
51 AXTreeUpdate initial_state
;
52 initial_state
.nodes
.push_back(root
);
53 CHECK(Unserialize(initial_state
)) << error();
56 AXTree::AXTree(const AXTreeUpdate
& initial_state
)
57 : delegate_(NULL
), root_(NULL
) {
58 CHECK(Unserialize(initial_state
)) << error();
63 DestroyNodeAndSubtree(root_
);
66 void AXTree::SetDelegate(AXTreeDelegate
* delegate
) {
70 AXNode
* AXTree::GetRoot() const {
74 AXNode
* AXTree::GetFromId(int32 id
) const {
75 base::hash_map
<int32
, AXNode
*>::const_iterator iter
= id_map_
.find(id
);
76 return iter
!= id_map_
.end() ? (iter
->second
) : NULL
;
79 bool AXTree::Unserialize(const AXTreeUpdate
& update
) {
80 AXTreeUpdateState update_state
;
81 int32 old_root_id
= root_
? root_
->id() : 0;
83 if (update
.node_id_to_clear
!= 0) {
84 AXNode
* node
= GetFromId(update
.node_id_to_clear
);
86 error_
= base::StringPrintf("Bad node_id_to_clear: %d",
87 update
.node_id_to_clear
);
91 DestroyNodeAndSubtree(root_
);
94 for (int i
= 0; i
< node
->child_count(); ++i
)
95 DestroyNodeAndSubtree(node
->ChildAtIndex(i
));
96 std::vector
<AXNode
*> children
;
97 node
->SwapChildren(children
);
98 update_state
.pending_nodes
.insert(node
);
102 for (size_t i
= 0; i
< update
.nodes
.size(); ++i
) {
103 if (!UpdateNode(update
.nodes
[i
], &update_state
))
107 if (!update_state
.pending_nodes
.empty()) {
108 error_
= "Nodes left pending by the update:";
109 for (std::set
<AXNode
*>::iterator iter
= update_state
.pending_nodes
.begin();
110 iter
!= update_state
.pending_nodes
.end(); ++iter
) {
111 error_
+= base::StringPrintf(" %d", (*iter
)->id());
117 for (size_t i
= 0; i
< update
.nodes
.size(); ++i
) {
118 AXNode
* node
= GetFromId(update
.nodes
[i
].id
);
119 if (update_state
.new_nodes
.find(node
) != update_state
.new_nodes
.end()) {
120 delegate_
->OnNodeCreationFinished(node
);
121 update_state
.new_nodes
.erase(node
);
123 delegate_
->OnNodeChangeFinished(node
);
126 if (root_
->id() != old_root_id
)
127 delegate_
->OnRootChanged(root_
);
133 std::string
AXTree::ToString() const {
134 return TreeToStringHelper(root_
, 0);
137 AXNode
* AXTree::CreateNode(
138 AXNode
* parent
, int32 id
, int32 index_in_parent
) {
139 AXNode
* new_node
= new AXNode(parent
, id
, index_in_parent
);
140 id_map_
[new_node
->id()] = new_node
;
142 delegate_
->OnNodeCreated(new_node
);
146 bool AXTree::UpdateNode(
147 const AXNodeData
& src
, AXTreeUpdateState
* update_state
) {
148 // This method updates one node in the tree based on serialized data
149 // received in an AXTreeUpdate. See AXTreeUpdate for pre and post
152 // Look up the node by id. If it's not found, then either the root
153 // of the tree is being swapped, or we're out of sync with the source
154 // and this is a serious error.
155 AXNode
* node
= GetFromId(src
.id
);
156 AXNode
* new_node
= NULL
;
158 update_state
->pending_nodes
.erase(node
);
161 delegate_
->OnNodeChanged(node
);
163 if (src
.role
!= AX_ROLE_ROOT_WEB_AREA
) {
164 error_
= base::StringPrintf(
165 "%d is not in the tree and not the new root", src
.id
);
168 new_node
= CreateNode(NULL
, src
.id
, 0);
170 update_state
->new_nodes
.insert(node
);
174 // First, delete nodes that used to be children of this node but aren't
176 if (!DeleteOldChildren(node
, src
.child_ids
)) {
178 DestroyNodeAndSubtree(new_node
);
182 // Now build a new children vector, reusing nodes when possible,
184 std::vector
<AXNode
*> new_children
;
185 bool success
= CreateNewChildVector(
186 node
, src
.child_ids
, &new_children
, update_state
);
187 node
->SwapChildren(new_children
);
189 // Update the root of the tree if needed.
190 if (src
.role
== AX_ROLE_ROOT_WEB_AREA
&&
191 (!root_
|| root_
->id() != src
.id
)) {
193 DestroyNodeAndSubtree(root_
);
200 void AXTree::DestroyNodeAndSubtree(AXNode
* node
) {
201 id_map_
.erase(node
->id());
202 for (int i
= 0; i
< node
->child_count(); ++i
)
203 DestroyNodeAndSubtree(node
->ChildAtIndex(i
));
205 delegate_
->OnNodeWillBeDeleted(node
);
209 bool AXTree::DeleteOldChildren(AXNode
* node
,
210 const std::vector
<int32
> new_child_ids
) {
211 // Create a set of child ids in |src| for fast lookup, and return false
212 // if a duplicate is found;
213 std::set
<int32
> new_child_id_set
;
214 for (size_t i
= 0; i
< new_child_ids
.size(); ++i
) {
215 if (new_child_id_set
.find(new_child_ids
[i
]) != new_child_id_set
.end()) {
216 error_
= base::StringPrintf("Node %d has duplicate child id %d",
217 node
->id(), new_child_ids
[i
]);
220 new_child_id_set
.insert(new_child_ids
[i
]);
223 // Delete the old children.
224 const std::vector
<AXNode
*>& old_children
= node
->children();
225 for (size_t i
= 0; i
< old_children
.size(); ++i
) {
226 int old_id
= old_children
[i
]->id();
227 if (new_child_id_set
.find(old_id
) == new_child_id_set
.end())
228 DestroyNodeAndSubtree(old_children
[i
]);
234 bool AXTree::CreateNewChildVector(AXNode
* node
,
235 const std::vector
<int32
> new_child_ids
,
236 std::vector
<AXNode
*>* new_children
,
237 AXTreeUpdateState
* update_state
) {
239 for (size_t i
= 0; i
< new_child_ids
.size(); ++i
) {
240 int32 child_id
= new_child_ids
[i
];
241 int32 index_in_parent
= static_cast<int32
>(i
);
242 AXNode
* child
= GetFromId(child_id
);
244 if (child
->parent() != node
) {
245 // This is a serious error - nodes should never be reparented.
246 // If this case occurs, continue so this node isn't left in an
247 // inconsistent state, but return failure at the end.
248 error_
= base::StringPrintf(
249 "Node %d reparented from %d to %d",
251 child
->parent() ? child
->parent()->id() : 0,
256 child
->SetIndexInParent(index_in_parent
);
258 child
= CreateNode(node
, child_id
, index_in_parent
);
259 update_state
->pending_nodes
.insert(child
);
260 update_state
->new_nodes
.insert(child
);
262 new_children
->push_back(child
);