NaCl cleanup: Stop linking the old, glibc-based Non-SFI runtime into nacl_helper
[chromium-blink-merge.git] / ui / accessibility / ax_tree.cc
blobd07f239b79d9a292b1221239067b71ee2b715e03
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"
7 #include <set>
9 #include "base/logging.h"
10 #include "base/strings/stringprintf.h"
11 #include "ui/accessibility/ax_node.h"
13 namespace ui {
15 namespace {
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);
22 return result;
25 } // namespace
27 // Intermediate state to keep track of during a tree update.
28 struct AXTreeUpdateState {
29 AXTreeUpdateState() : new_root(nullptr) {}
31 // During an update, this keeps track of all nodes that have been
32 // implicitly referenced as part of this update, but haven't been
33 // updated yet. It's an error if there are any pending nodes at the
34 // end of Unserialize.
35 std::set<AXNode*> pending_nodes;
37 // Keeps track of new nodes created during this update.
38 std::set<AXNode*> new_nodes;
40 // The new root in this update, if any.
41 AXNode* new_root;
44 AXTreeDelegate::AXTreeDelegate() {
47 AXTreeDelegate::~AXTreeDelegate() {
50 AXTree::AXTree()
51 : delegate_(NULL), root_(NULL) {
52 AXNodeData root;
53 root.id = -1;
54 root.role = AX_ROLE_ROOT_WEB_AREA;
56 AXTreeUpdate initial_state;
57 initial_state.nodes.push_back(root);
58 CHECK(Unserialize(initial_state)) << error();
61 AXTree::AXTree(const AXTreeUpdate& initial_state)
62 : delegate_(NULL), root_(NULL) {
63 CHECK(Unserialize(initial_state)) << error();
66 AXTree::~AXTree() {
67 if (root_)
68 DestroyNodeAndSubtree(root_, nullptr);
71 void AXTree::SetDelegate(AXTreeDelegate* delegate) {
72 delegate_ = delegate;
75 AXNode* AXTree::GetFromId(int32 id) const {
76 base::hash_map<int32, AXNode*>::const_iterator iter = id_map_.find(id);
77 return iter != id_map_.end() ? iter->second : NULL;
80 bool AXTree::Unserialize(const AXTreeUpdate& update) {
81 AXTreeUpdateState update_state;
82 int32 old_root_id = root_ ? root_->id() : 0;
84 if (update.node_id_to_clear != 0) {
85 AXNode* node = GetFromId(update.node_id_to_clear);
86 if (!node) {
87 error_ = base::StringPrintf("Bad node_id_to_clear: %d",
88 update.node_id_to_clear);
89 return false;
91 if (node == root_) {
92 DestroySubtree(root_, &update_state);
93 root_ = NULL;
94 } else {
95 for (int i = 0; i < node->child_count(); ++i)
96 DestroySubtree(node->ChildAtIndex(i), &update_state);
97 std::vector<AXNode*> children;
98 node->SwapChildren(children);
99 update_state.pending_nodes.insert(node);
103 for (size_t i = 0; i < update.nodes.size(); ++i) {
104 if (!UpdateNode(update.nodes[i], &update_state))
105 return false;
108 if (!update_state.pending_nodes.empty()) {
109 error_ = "Nodes left pending by the update:";
110 for (std::set<AXNode*>::iterator iter = update_state.pending_nodes.begin();
111 iter != update_state.pending_nodes.end(); ++iter) {
112 error_ += base::StringPrintf(" %d", (*iter)->id());
114 return false;
117 if (delegate_) {
118 std::set<AXNode*>& new_nodes = update_state.new_nodes;
119 std::vector<AXTreeDelegate::Change> changes;
120 changes.reserve(update.nodes.size());
121 for (size_t i = 0; i < update.nodes.size(); ++i) {
122 AXNode* node = GetFromId(update.nodes[i].id);
123 if (new_nodes.find(node) != new_nodes.end()) {
124 if (new_nodes.find(node->parent()) == new_nodes.end()) {
125 changes.push_back(
126 AXTreeDelegate::Change(node, AXTreeDelegate::SUBTREE_CREATED));
127 } else {
128 changes.push_back(
129 AXTreeDelegate::Change(node, AXTreeDelegate::NODE_CREATED));
131 } else {
132 changes.push_back(
133 AXTreeDelegate::Change(node, AXTreeDelegate::NODE_CHANGED));
136 delegate_->OnAtomicUpdateFinished(
137 this, root_->id() != old_root_id, changes);
140 return true;
143 std::string AXTree::ToString() const {
144 return TreeToStringHelper(root_, 0);
147 AXNode* AXTree::CreateNode(AXNode* parent, int32 id, int32 index_in_parent) {
148 AXNode* new_node = new AXNode(parent, id, index_in_parent);
149 id_map_[new_node->id()] = new_node;
150 if (delegate_)
151 delegate_->OnNodeCreated(this, new_node);
152 return new_node;
155 bool AXTree::UpdateNode(const AXNodeData& src,
156 AXTreeUpdateState* update_state) {
157 // This method updates one node in the tree based on serialized data
158 // received in an AXTreeUpdate. See AXTreeUpdate for pre and post
159 // conditions.
161 // Look up the node by id. If it's not found, then either the root
162 // of the tree is being swapped, or we're out of sync with the source
163 // and this is a serious error.
164 AXNode* node = GetFromId(src.id);
165 if (node) {
166 update_state->pending_nodes.erase(node);
167 node->SetData(src);
168 } else {
169 if (src.role != AX_ROLE_ROOT_WEB_AREA &&
170 src.role != AX_ROLE_DESKTOP) {
171 error_ = base::StringPrintf(
172 "%d is not in the tree and not the new root", src.id);
173 return false;
175 if (update_state->new_root) {
176 error_ = "Tree update contains two new roots";
177 return false;
180 update_state->new_root = CreateNode(NULL, src.id, 0);
181 node = update_state->new_root;
182 update_state->new_nodes.insert(node);
183 node->SetData(src);
186 if (delegate_)
187 delegate_->OnNodeChanged(this, node);
189 // First, delete nodes that used to be children of this node but aren't
190 // anymore.
191 if (!DeleteOldChildren(node, src.child_ids, update_state)) {
192 if (update_state->new_root)
193 DestroySubtree(update_state->new_root, update_state);
194 return false;
197 // Now build a new children vector, reusing nodes when possible,
198 // and swap it in.
199 std::vector<AXNode*> new_children;
200 bool success = CreateNewChildVector(
201 node, src.child_ids, &new_children, update_state);
202 node->SwapChildren(new_children);
204 // Update the root of the tree if needed.
205 if ((src.role == AX_ROLE_ROOT_WEB_AREA || src.role == AX_ROLE_DESKTOP) &&
206 (!root_ || root_->id() != src.id)) {
207 if (root_)
208 DestroySubtree(root_, update_state);
209 root_ = node;
212 return success;
215 void AXTree::DestroySubtree(AXNode* node,
216 AXTreeUpdateState* update_state) {
217 if (delegate_)
218 delegate_->OnSubtreeWillBeDeleted(this, node);
219 DestroyNodeAndSubtree(node, update_state);
222 void AXTree::DestroyNodeAndSubtree(AXNode* node,
223 AXTreeUpdateState* update_state) {
224 id_map_.erase(node->id());
225 for (int i = 0; i < node->child_count(); ++i)
226 DestroyNodeAndSubtree(node->ChildAtIndex(i), update_state);
227 if (delegate_)
228 delegate_->OnNodeWillBeDeleted(this, node);
229 if (update_state) {
230 update_state->pending_nodes.erase(node);
232 node->Destroy();
235 bool AXTree::DeleteOldChildren(AXNode* node,
236 const std::vector<int32>& new_child_ids,
237 AXTreeUpdateState* update_state) {
238 // Create a set of child ids in |src| for fast lookup, and return false
239 // if a duplicate is found;
240 std::set<int32> new_child_id_set;
241 for (size_t i = 0; i < new_child_ids.size(); ++i) {
242 if (new_child_id_set.find(new_child_ids[i]) != new_child_id_set.end()) {
243 error_ = base::StringPrintf("Node %d has duplicate child id %d",
244 node->id(), new_child_ids[i]);
245 return false;
247 new_child_id_set.insert(new_child_ids[i]);
250 // Delete the old children.
251 const std::vector<AXNode*>& old_children = node->children();
252 for (size_t i = 0; i < old_children.size(); ++i) {
253 int old_id = old_children[i]->id();
254 if (new_child_id_set.find(old_id) == new_child_id_set.end())
255 DestroySubtree(old_children[i], update_state);
258 return true;
261 bool AXTree::CreateNewChildVector(AXNode* node,
262 const std::vector<int32>& new_child_ids,
263 std::vector<AXNode*>* new_children,
264 AXTreeUpdateState* update_state) {
265 bool success = true;
266 for (size_t i = 0; i < new_child_ids.size(); ++i) {
267 int32 child_id = new_child_ids[i];
268 int32 index_in_parent = static_cast<int32>(i);
269 AXNode* child = GetFromId(child_id);
270 if (child) {
271 if (child->parent() != node) {
272 // This is a serious error - nodes should never be reparented.
273 // If this case occurs, continue so this node isn't left in an
274 // inconsistent state, but return failure at the end.
275 error_ = base::StringPrintf(
276 "Node %d reparented from %d to %d",
277 child->id(),
278 child->parent() ? child->parent()->id() : 0,
279 node->id());
280 success = false;
281 continue;
283 child->SetIndexInParent(index_in_parent);
284 } else {
285 child = CreateNode(node, child_id, index_in_parent);
286 update_state->pending_nodes.insert(child);
287 update_state->new_nodes.insert(child);
289 new_children->push_back(child);
292 return success;
295 } // namespace ui