Roll src/third_party/WebKit 0ba31e2:698efc0 (svn 192483:192500)
[chromium-blink-merge.git] / ui / accessibility / ax_tree.cc
blob73b5c6d69765bcb9c3af06e673ec2d8e242fbc49
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 // 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() {
45 AXTree::AXTree()
46 : delegate_(NULL), root_(NULL) {
47 AXNodeData root;
48 root.id = -1;
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();
61 AXTree::~AXTree() {
62 if (root_)
63 DestroyNodeAndSubtree(root_);
66 void AXTree::SetDelegate(AXTreeDelegate* delegate) {
67 delegate_ = delegate;
70 AXNode* AXTree::GetFromId(int32 id) const {
71 base::hash_map<int32, AXNode*>::const_iterator iter = id_map_.find(id);
72 return iter != id_map_.end() ? iter->second : NULL;
75 bool AXTree::Unserialize(const AXTreeUpdate& update) {
76 AXTreeUpdateState update_state;
77 int32 old_root_id = root_ ? root_->id() : 0;
79 if (update.node_id_to_clear != 0) {
80 AXNode* node = GetFromId(update.node_id_to_clear);
81 if (!node) {
82 error_ = base::StringPrintf("Bad node_id_to_clear: %d",
83 update.node_id_to_clear);
84 return false;
86 if (node == root_) {
87 DestroySubtree(root_);
88 root_ = NULL;
89 } else {
90 for (int i = 0; i < node->child_count(); ++i)
91 DestroySubtree(node->ChildAtIndex(i));
92 std::vector<AXNode*> children;
93 node->SwapChildren(children);
94 update_state.pending_nodes.insert(node);
98 for (size_t i = 0; i < update.nodes.size(); ++i) {
99 if (!UpdateNode(update.nodes[i], &update_state))
100 return false;
103 if (!update_state.pending_nodes.empty()) {
104 error_ = "Nodes left pending by the update:";
105 for (std::set<AXNode*>::iterator iter = update_state.pending_nodes.begin();
106 iter != update_state.pending_nodes.end(); ++iter) {
107 error_ += base::StringPrintf(" %d", (*iter)->id());
109 return false;
112 if (delegate_) {
113 std::set<AXNode*>& new_nodes = update_state.new_nodes;
114 std::vector<AXTreeDelegate::Change> changes;
115 changes.reserve(update.nodes.size());
116 for (size_t i = 0; i < update.nodes.size(); ++i) {
117 AXNode* node = GetFromId(update.nodes[i].id);
118 if (new_nodes.find(node) != new_nodes.end()) {
119 if (new_nodes.find(node->parent()) == new_nodes.end()) {
120 changes.push_back(
121 AXTreeDelegate::Change(node, AXTreeDelegate::SUBTREE_CREATED));
122 } else {
123 changes.push_back(
124 AXTreeDelegate::Change(node, AXTreeDelegate::NODE_CREATED));
126 } else {
127 changes.push_back(
128 AXTreeDelegate::Change(node, AXTreeDelegate::NODE_CHANGED));
131 delegate_->OnAtomicUpdateFinished(root_->id() != old_root_id, changes);
134 return true;
137 std::string AXTree::ToString() const {
138 return TreeToStringHelper(root_, 0);
141 AXNode* AXTree::CreateNode(AXNode* parent, int32 id, int32 index_in_parent) {
142 AXNode* new_node = new AXNode(parent, id, index_in_parent);
143 id_map_[new_node->id()] = new_node;
144 if (delegate_)
145 delegate_->OnNodeCreated(new_node);
146 return new_node;
149 bool AXTree::UpdateNode(const AXNodeData& src,
150 AXTreeUpdateState* update_state) {
151 // This method updates one node in the tree based on serialized data
152 // received in an AXTreeUpdate. See AXTreeUpdate for pre and post
153 // conditions.
155 // Look up the node by id. If it's not found, then either the root
156 // of the tree is being swapped, or we're out of sync with the source
157 // and this is a serious error.
158 AXNode* node = GetFromId(src.id);
159 AXNode* new_root = NULL;
160 if (node) {
161 update_state->pending_nodes.erase(node);
162 node->SetData(src);
163 } else {
164 if (src.role != AX_ROLE_ROOT_WEB_AREA) {
165 error_ = base::StringPrintf(
166 "%d is not in the tree and not the new root", src.id);
167 return false;
169 new_root = CreateNode(NULL, src.id, 0);
170 node = new_root;
171 update_state->new_nodes.insert(node);
172 node->SetData(src);
175 if (delegate_)
176 delegate_->OnNodeChanged(node);
178 // First, delete nodes that used to be children of this node but aren't
179 // anymore.
180 if (!DeleteOldChildren(node, src.child_ids)) {
181 if (new_root)
182 DestroySubtree(new_root);
183 return false;
186 // Now build a new children vector, reusing nodes when possible,
187 // and swap it in.
188 std::vector<AXNode*> new_children;
189 bool success = CreateNewChildVector(
190 node, src.child_ids, &new_children, update_state);
191 node->SwapChildren(new_children);
193 // Update the root of the tree if needed.
194 if (src.role == AX_ROLE_ROOT_WEB_AREA &&
195 (!root_ || root_->id() != src.id)) {
196 if (root_)
197 DestroySubtree(root_);
198 root_ = node;
201 return success;
204 void AXTree::DestroySubtree(AXNode* node) {
205 if (delegate_)
206 delegate_->OnSubtreeWillBeDeleted(node);
207 DestroyNodeAndSubtree(node);
210 void AXTree::DestroyNodeAndSubtree(AXNode* node) {
211 id_map_.erase(node->id());
212 for (int i = 0; i < node->child_count(); ++i)
213 DestroyNodeAndSubtree(node->ChildAtIndex(i));
214 if (delegate_)
215 delegate_->OnNodeWillBeDeleted(node);
216 node->Destroy();
219 bool AXTree::DeleteOldChildren(AXNode* node,
220 const std::vector<int32>& new_child_ids) {
221 // Create a set of child ids in |src| for fast lookup, and return false
222 // if a duplicate is found;
223 std::set<int32> new_child_id_set;
224 for (size_t i = 0; i < new_child_ids.size(); ++i) {
225 if (new_child_id_set.find(new_child_ids[i]) != new_child_id_set.end()) {
226 error_ = base::StringPrintf("Node %d has duplicate child id %d",
227 node->id(), new_child_ids[i]);
228 return false;
230 new_child_id_set.insert(new_child_ids[i]);
233 // Delete the old children.
234 const std::vector<AXNode*>& old_children = node->children();
235 for (size_t i = 0; i < old_children.size(); ++i) {
236 int old_id = old_children[i]->id();
237 if (new_child_id_set.find(old_id) == new_child_id_set.end())
238 DestroySubtree(old_children[i]);
241 return true;
244 bool AXTree::CreateNewChildVector(AXNode* node,
245 const std::vector<int32>& new_child_ids,
246 std::vector<AXNode*>* new_children,
247 AXTreeUpdateState* update_state) {
248 bool success = true;
249 for (size_t i = 0; i < new_child_ids.size(); ++i) {
250 int32 child_id = new_child_ids[i];
251 int32 index_in_parent = static_cast<int32>(i);
252 AXNode* child = GetFromId(child_id);
253 if (child) {
254 if (child->parent() != node) {
255 // This is a serious error - nodes should never be reparented.
256 // If this case occurs, continue so this node isn't left in an
257 // inconsistent state, but return failure at the end.
258 error_ = base::StringPrintf(
259 "Node %d reparented from %d to %d",
260 child->id(),
261 child->parent() ? child->parent()->id() : 0,
262 node->id());
263 success = false;
264 continue;
266 child->SetIndexInParent(index_in_parent);
267 } else {
268 child = CreateNode(node, child_id, index_in_parent);
269 update_state->pending_nodes.insert(child);
270 update_state->new_nodes.insert(child);
272 new_children->push_back(child);
275 return success;
278 } // namespace ui