Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / ui / accessibility / ax_tree_serializer.h
blob19f15a3a63f56e58d3d8537a246a43c6e5dd9464
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 #ifndef UI_ACCESSIBILITY_AX_TREE_SERIALIZER_H_
6 #define UI_ACCESSIBILITY_AX_TREE_SERIALIZER_H_
8 #include <set>
10 #include "base/containers/hash_tables.h"
11 #include "base/logging.h"
12 #include "base/stl_util.h"
13 #include "ui/accessibility/ax_tree_source.h"
14 #include "ui/accessibility/ax_tree_update.h"
16 namespace ui {
18 struct ClientTreeNode;
20 // AXTreeSerializer is a helper class that serializes incremental
21 // updates to an AXTreeSource as a AXTreeUpdate struct.
22 // These structs can be unserialized by a client object such as an
23 // AXTree. An AXTreeSerializer keeps track of the tree of node ids that its
24 // client is aware of so that it will never generate an AXTreeUpdate that
25 // results in an invalid tree.
27 // Every node in the source tree must have an id that's a unique positive
28 // integer, the same node must not appear twice.
30 // Usage:
32 // You must call SerializeChanges() every time a node in the tree changes,
33 // and send the generated AXTreeUpdate to the client.
35 // If a node is added, call SerializeChanges on its parent.
36 // If a node is removed, call SerializeChanges on its parent.
37 // If a whole new subtree is added, just call SerializeChanges on its root.
38 // If the root of the tree changes, call SerializeChanges on the new root.
40 // AXTreeSerializer will avoid re-serializing nodes that do not change.
41 // For example, if node 1 has children 2, 3, 4, 5 and then child 2 is
42 // removed and a new child 6 is added, the AXTreeSerializer will only
43 // update nodes 1 and 6 (and any children of node 6 recursively). It will
44 // assume that nodes 3, 4, and 5 are not modified unless you explicitly
45 // call SerializeChanges() on them.
47 // As long as the source tree has unique ids for every node and no loops,
48 // and as long as every update is applied to the client tree, AXTreeSerializer
49 // will continue to work. If the source tree makes a change but fails to
50 // call SerializeChanges properly, the trees may get out of sync - but
51 // because AXTreeSerializer always keeps track of what updates it's sent,
52 // it will never send an invalid update and the client tree will not break,
53 // it just may not contain all of the changes.
54 template<typename AXSourceNode>
55 class AXTreeSerializer {
56 public:
57 explicit AXTreeSerializer(AXTreeSource<AXSourceNode>* tree);
58 ~AXTreeSerializer();
60 // Throw out the internal state that keeps track of the nodes the client
61 // knows about. This has the effect that the next update will send the
62 // entire tree over because it assumes the client knows nothing.
63 void Reset();
65 // Sets the maximum number of nodes that will be serialized, or zero
66 // for no maximum. This is not a hard maximum - once it hits or
67 // exceeds this maximum it stops walking the children of nodes, but
68 // it may exceed this value a bit in order to create a consistent
69 // tree.
70 void set_max_node_count(size_t max_node_count) {
71 max_node_count_ = max_node_count;
74 // Serialize all changes to |node| and append them to |out_update|.
75 void SerializeChanges(AXSourceNode node,
76 AXTreeUpdate* out_update);
78 // Delete the client subtree for this node, ensuring that the subtree
79 // is re-serialized.
80 void DeleteClientSubtree(AXSourceNode node);
82 // Only for unit testing. Normally this class relies on getting a call
83 // to SerializeChanges() every time the source tree changes. For unit
84 // testing, it's convenient to create a static AXTree for the initial
85 // state and then call ChangeTreeSourceForTesting and then SerializeChanges
86 // to simulate the changes you'd get if a tree changed from the initial
87 // state to the second tree's state.
88 void ChangeTreeSourceForTesting(AXTreeSource<AXSourceNode>* new_tree);
90 private:
91 // Return the least common ancestor of a node in the source tree
92 // and a node in the client tree, or NULL if there is no such node.
93 // The least common ancestor is the closest ancestor to |node| (which
94 // may be |node| itself) that's in both the source tree and client tree,
95 // and for which both the source and client tree agree on their ancestor
96 // chain up to the root.
98 // Example 1:
100 // Client Tree Source tree |
101 // 1 1 |
102 // / \ / \ |
103 // 2 3 2 4 |
105 // LCA(source node 2, client node 2) is node 2.
106 // LCA(source node 3, client node 4) is node 1.
108 // Example 2:
110 // Client Tree Source tree |
111 // 1 1 |
112 // / \ / \ |
113 // 2 3 2 3 |
114 // / \ / / |
115 // 4 7 8 4 |
116 // / \ / \ |
117 // 5 6 5 6 |
119 // LCA(source node 8, client node 7) is node 2.
120 // LCA(source node 5, client node 5) is node 1.
121 // It's not node 5, because the two trees disagree on the parent of
122 // node 4, so the LCA is the first ancestor both trees agree on.
123 AXSourceNode LeastCommonAncestor(AXSourceNode node,
124 ClientTreeNode* client_node);
126 // Return the least common ancestor of |node| that's in the client tree.
127 // This just walks up the ancestors of |node| until it finds a node that's
128 // also in the client tree, and then calls LeastCommonAncestor on the
129 // source node and client node.
130 AXSourceNode LeastCommonAncestor(AXSourceNode node);
132 // Walk the subtree rooted at |node| and return true if any nodes that
133 // would be updated are being reparented. If so, update |out_lca| to point
134 // to the least common ancestor of the previous LCA and the previous
135 // parent of the node being reparented.
136 bool AnyDescendantWasReparented(AXSourceNode node,
137 AXSourceNode* out_lca);
139 ClientTreeNode* ClientTreeNodeById(int32 id);
141 // Delete the given client tree node and recursively delete all of its
142 // descendants.
143 void DeleteClientSubtree(ClientTreeNode* client_node);
145 // Helper function, called recursively with each new node to serialize.
146 void SerializeChangedNodes(AXSourceNode node,
147 AXTreeUpdate* out_update);
149 // Visit all of the descendants of |node| once.
150 void WalkAllDescendants(AXSourceNode node);
152 // The tree source.
153 AXTreeSource<AXSourceNode>* tree_;
155 // Our representation of the client tree.
156 ClientTreeNode* client_root_;
158 // A map from IDs to nodes in the client tree.
159 base::hash_map<int32, ClientTreeNode*> client_id_map_;
161 // The maximum number of nodes to serialize in a given call to
162 // SerializeChanges, or 0 if there's no maximum.
163 size_t max_node_count_;
166 // In order to keep track of what nodes the client knows about, we keep a
167 // representation of the client tree - just IDs and parent/child
168 // relationships.
169 struct AX_EXPORT ClientTreeNode {
170 ClientTreeNode();
171 virtual ~ClientTreeNode();
172 int32 id;
173 ClientTreeNode* parent;
174 std::vector<ClientTreeNode*> children;
177 template<typename AXSourceNode>
178 AXTreeSerializer<AXSourceNode>::AXTreeSerializer(
179 AXTreeSource<AXSourceNode>* tree)
180 : tree_(tree),
181 client_root_(NULL),
182 max_node_count_(0) {
185 template<typename AXSourceNode>
186 AXTreeSerializer<AXSourceNode>::~AXTreeSerializer() {
187 Reset();
190 template<typename AXSourceNode>
191 void AXTreeSerializer<AXSourceNode>::Reset() {
192 if (!client_root_)
193 return;
195 DeleteClientSubtree(client_root_);
196 client_id_map_.erase(client_root_->id);
197 delete client_root_;
198 client_root_ = NULL;
201 template<typename AXSourceNode>
202 void AXTreeSerializer<AXSourceNode>::ChangeTreeSourceForTesting(
203 AXTreeSource<AXSourceNode>* new_tree) {
204 tree_ = new_tree;
207 template<typename AXSourceNode>
208 AXSourceNode AXTreeSerializer<AXSourceNode>::LeastCommonAncestor(
209 AXSourceNode node, ClientTreeNode* client_node) {
210 if (!tree_->IsValid(node) || client_node == NULL)
211 return tree_->GetNull();
213 std::vector<AXSourceNode> ancestors;
214 while (tree_->IsValid(node)) {
215 ancestors.push_back(node);
216 node = tree_->GetParent(node);
219 std::vector<ClientTreeNode*> client_ancestors;
220 while (client_node) {
221 client_ancestors.push_back(client_node);
222 client_node = client_node->parent;
225 // Start at the root. Keep going until the source ancestor chain and
226 // client ancestor chain disagree. The last node before they disagree
227 // is the LCA.
228 AXSourceNode lca = tree_->GetNull();
229 int source_index = static_cast<int>(ancestors.size() - 1);
230 int client_index = static_cast<int>(client_ancestors.size() - 1);
231 while (source_index >= 0 && client_index >= 0) {
232 if (tree_->GetId(ancestors[source_index]) !=
233 client_ancestors[client_index]->id) {
234 return lca;
236 lca = ancestors[source_index];
237 source_index--;
238 client_index--;
240 return lca;
243 template<typename AXSourceNode>
244 AXSourceNode AXTreeSerializer<AXSourceNode>::LeastCommonAncestor(
245 AXSourceNode node) {
246 // Walk up the tree until the source node's id also exists in the
247 // client tree, then call LeastCommonAncestor on those two nodes.
248 ClientTreeNode* client_node = ClientTreeNodeById(tree_->GetId(node));
249 while (tree_->IsValid(node) && !client_node) {
250 node = tree_->GetParent(node);
251 if (tree_->IsValid(node))
252 client_node = ClientTreeNodeById(tree_->GetId(node));
254 return LeastCommonAncestor(node, client_node);
257 template<typename AXSourceNode>
258 bool AXTreeSerializer<AXSourceNode>::AnyDescendantWasReparented(
259 AXSourceNode node, AXSourceNode* out_lca) {
260 bool result = false;
261 int id = tree_->GetId(node);
262 std::vector<AXSourceNode> children;
263 tree_->GetChildren(node, &children);
264 for (size_t i = 0; i < children.size(); ++i) {
265 AXSourceNode& child = children[i];
266 int child_id = tree_->GetId(child);
267 ClientTreeNode* client_child = ClientTreeNodeById(child_id);
268 if (client_child) {
269 if (!client_child->parent) {
270 // If the client child has no parent, it must have been the
271 // previous root node, so there is no LCA and we can exit early.
272 *out_lca = tree_->GetNull();
273 return true;
274 } else if (client_child->parent->id != id) {
275 // If the client child's parent is not this node, update the LCA
276 // and return true (reparenting was found).
277 *out_lca = LeastCommonAncestor(*out_lca, client_child);
278 result = true;
279 } else {
280 // This child is already in the client tree, we won't
281 // recursively serialize it so we don't need to check this
282 // subtree recursively for reparenting.
283 continue;
287 // This is a new child or reparented child, check it recursively.
288 if (AnyDescendantWasReparented(child, out_lca))
289 result = true;
291 return result;
294 template<typename AXSourceNode>
295 ClientTreeNode* AXTreeSerializer<AXSourceNode>::ClientTreeNodeById(int32 id) {
296 base::hash_map<int32, ClientTreeNode*>::iterator iter =
297 client_id_map_.find(id);
298 if (iter != client_id_map_.end())
299 return iter->second;
300 else
301 return NULL;
304 template<typename AXSourceNode>
305 void AXTreeSerializer<AXSourceNode>::SerializeChanges(
306 AXSourceNode node,
307 AXTreeUpdate* out_update) {
308 // If the node isn't in the client tree, we need to serialize starting
309 // with the LCA.
310 AXSourceNode lca = LeastCommonAncestor(node);
312 // This loop computes the least common ancestor that includes the old
313 // and new parents of any nodes that have been reparented, and clears the
314 // whole client subtree of that LCA if necessary. If we do end up clearing
315 // any client nodes, keep looping because we have to search for more
316 // nodes that may have been reparented from this new LCA.
317 bool need_delete;
318 do {
319 need_delete = false;
320 if (client_root_) {
321 if (tree_->IsValid(lca)) {
322 // Check for any reparenting within this subtree - if there is
323 // any, we need to delete and reserialize the whole subtree
324 // that contains the old and new parents of the reparented node.
325 if (AnyDescendantWasReparented(lca, &lca))
326 need_delete = true;
329 if (!tree_->IsValid(lca)) {
330 // If there's no LCA, just tell the client to destroy the whole
331 // tree and then we'll serialize everything from the new root.
332 out_update->node_id_to_clear = client_root_->id;
333 Reset();
334 } else if (need_delete) {
335 // Otherwise, if we need to reserialize a subtree, first we need
336 // to delete those nodes in our client tree so that
337 // SerializeChangedNodes() will be sure to send them again.
338 out_update->node_id_to_clear = tree_->GetId(lca);
339 ClientTreeNode* client_lca = ClientTreeNodeById(tree_->GetId(lca));
340 CHECK(client_lca);
341 DeleteClientSubtree(client_lca);
344 } while (need_delete);
346 // Serialize from the LCA, or from the root if there isn't one.
347 if (!tree_->IsValid(lca))
348 lca = tree_->GetRoot();
350 // Work around flaky source trees where nodes don't figure out their
351 // correct parent/child relationships until you walk the whole tree once.
352 // Covered by this test in the content_browsertests suite:
353 // DumpAccessibilityTreeTest.AccessibilityAriaOwns.
354 WalkAllDescendants(lca);
356 SerializeChangedNodes(lca, out_update);
359 template<typename AXSourceNode>
360 void AXTreeSerializer<AXSourceNode>::DeleteClientSubtree(AXSourceNode node) {
361 ClientTreeNode* client_node = ClientTreeNodeById(tree_->GetId(node));
362 if (client_node)
363 DeleteClientSubtree(client_node);
366 template<typename AXSourceNode>
367 void AXTreeSerializer<AXSourceNode>::DeleteClientSubtree(
368 ClientTreeNode* client_node) {
369 for (size_t i = 0; i < client_node->children.size(); ++i) {
370 client_id_map_.erase(client_node->children[i]->id);
371 DeleteClientSubtree(client_node->children[i]);
372 delete client_node->children[i];
374 client_node->children.clear();
377 template<typename AXSourceNode>
378 void AXTreeSerializer<AXSourceNode>::SerializeChangedNodes(
379 AXSourceNode node,
380 AXTreeUpdate* out_update) {
381 // This method has three responsibilities:
382 // 1. Serialize |node| into an AXNodeData, and append it to
383 // the AXTreeUpdate to be sent to the client.
384 // 2. Determine if |node| has any new children that the client doesn't
385 // know about yet, and call SerializeChangedNodes recursively on those.
386 // 3. Update our internal data structure that keeps track of what nodes
387 // the client knows about.
389 // First, find the ClientTreeNode for this id in our data structure where
390 // we keep track of what accessibility objects the client already knows
391 // about. If we don't find it, then this must be the new root of the
392 // accessibility tree.
393 int id = tree_->GetId(node);
394 ClientTreeNode* client_node = ClientTreeNodeById(id);
395 if (!client_node) {
396 Reset();
397 client_root_ = new ClientTreeNode();
398 client_node = client_root_;
399 client_node->id = id;
400 client_node->parent = NULL;
401 client_id_map_[client_node->id] = client_node;
404 // Iterate over the ids of the children of |node|.
405 // Create a set of the child ids so we can quickly look
406 // up which children are new and which ones were there before.
407 // If we've hit the maximum number of serialized nodes, pretend
408 // this node has no children but keep going so that we get
409 // consistent results.
410 base::hash_set<int32> new_child_ids;
411 std::vector<AXSourceNode> children;
412 if (max_node_count_ == 0 || out_update->nodes.size() < max_node_count_) {
413 tree_->GetChildren(node, &children);
414 } else if (max_node_count_ > 0) {
415 static bool logged_once = false;
416 if (!logged_once) {
417 LOG(WARNING) << "Warning: not serializing AX nodes after a max of "
418 << max_node_count_;
419 logged_once = true;
422 for (size_t i = 0; i < children.size(); ++i) {
423 AXSourceNode& child = children[i];
424 int new_child_id = tree_->GetId(child);
425 new_child_ids.insert(new_child_id);
427 // This is a sanity check - there shouldn't be any reparenting
428 // because we've already handled it above.
429 ClientTreeNode* client_child = client_id_map_[new_child_id];
430 CHECK(!client_child || client_child->parent == client_node);
433 // Go through the old children and delete subtrees for child
434 // ids that are no longer present, and create a map from
435 // id to ClientTreeNode for the rest. It's important to delete
436 // first in a separate pass so that nodes that are reparented
437 // don't end up children of two different parents in the middle
438 // of an update, which can lead to a double-free.
439 base::hash_map<int32, ClientTreeNode*> client_child_id_map;
440 std::vector<ClientTreeNode*> old_children;
441 old_children.swap(client_node->children);
442 for (size_t i = 0; i < old_children.size(); ++i) {
443 ClientTreeNode* old_child = old_children[i];
444 int old_child_id = old_child->id;
445 if (new_child_ids.find(old_child_id) == new_child_ids.end()) {
446 client_id_map_.erase(old_child_id);
447 DeleteClientSubtree(old_child);
448 delete old_child;
449 } else {
450 client_child_id_map[old_child_id] = old_child;
454 // Serialize this node. This fills in all of the fields in
455 // AXNodeData except child_ids, which we handle below.
456 size_t serialized_node_index = out_update->nodes.size();
457 out_update->nodes.push_back(AXNodeData());
459 // Take the address of an element in a vector only within a limited
460 // scope because otherwise the pointer can become invalid if the
461 // vector is resized.
462 AXNodeData* serialized_node = &out_update->nodes[serialized_node_index];
464 tree_->SerializeNode(node, serialized_node);
465 // TODO(dmazzoni/dtseng): Make the serializer not depend on roles to
466 // identify the root.
467 if (serialized_node->id == client_root_->id &&
468 (serialized_node->role != AX_ROLE_ROOT_WEB_AREA &&
469 serialized_node->role != AX_ROLE_DESKTOP)) {
470 serialized_node->role = AX_ROLE_ROOT_WEB_AREA;
474 // Iterate over the children, serialize them, and update the ClientTreeNode
475 // data structure to reflect the new tree.
476 std::vector<int32> actual_serialized_node_child_ids;
477 client_node->children.reserve(children.size());
478 for (size_t i = 0; i < children.size(); ++i) {
479 AXSourceNode& child = children[i];
480 int child_id = tree_->GetId(child);
482 // Skip if the child isn't valid.
483 if (!tree_->IsValid(child))
484 continue;
486 // Skip if the same child is included more than once.
487 if (new_child_ids.find(child_id) == new_child_ids.end())
488 continue;
490 new_child_ids.erase(child_id);
491 actual_serialized_node_child_ids.push_back(child_id);
492 if (client_child_id_map.find(child_id) != client_child_id_map.end()) {
493 ClientTreeNode* reused_child = client_child_id_map[child_id];
494 client_node->children.push_back(reused_child);
495 } else {
496 ClientTreeNode* new_child = new ClientTreeNode();
497 new_child->id = child_id;
498 new_child->parent = client_node;
499 client_node->children.push_back(new_child);
500 client_id_map_[child_id] = new_child;
501 SerializeChangedNodes(child, out_update);
505 // Finally, update the child ids of this node to reflect the actual child
506 // ids that were valid during serialization.
507 out_update->nodes[serialized_node_index].child_ids.swap(
508 actual_serialized_node_child_ids);
511 template<typename AXSourceNode>
512 void AXTreeSerializer<AXSourceNode>::WalkAllDescendants(
513 AXSourceNode node) {
514 std::vector<AXSourceNode> children;
515 tree_->GetChildren(node, &children);
516 for (size_t i = 0; i < children.size(); ++i)
517 WalkAllDescendants(children[i]);
520 } // namespace ui
522 #endif // UI_ACCESSIBILITY_AX_TREE_SERIALIZER_H_