Revert "Fix broken channel icon in chrome://help on CrOS" and try again
[chromium-blink-merge.git] / ui / accessibility / ax_tree_update.h
blob7fd94ec498ee9077ffbfdc181518cec51540c23b
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_UPDATE_H_
6 #define UI_ACCESSIBILITY_AX_TREE_UPDATE_H_
8 #include <string>
9 #include <vector>
11 #include "base/strings/string_number_conversions.h"
12 #include "ui/accessibility/ax_node_data.h"
14 namespace ui {
16 // An AXTreeUpdate is a serialized representation of an atomic change
17 // to an AXTree. The sender and receiver must be in sync; the update
18 // is only meant to bring the tree from a specific previous state into
19 // its next state. Trying to apply it to the wrong tree should immediately
20 // die with a fatal assertion.
22 // An AXTreeUpdate consists of an optional node id to clear (meaning
23 // that all of that node's children and their descendants are deleted),
24 // followed by an ordered vector of AXNodeData structures to be applied
25 // to the tree in order.
27 // Suppose that the next AXNodeData to be applied is |node|. The following
28 // invariants must hold:
29 // 1. Either |node.id| is already in the tree, or else the tree is empty,
30 // |node| is the new root of the tree, and
31 // |node.role| == WebAXRoleRootWebArea.
32 // 2. Every child id in |node.child_ids| must either be already a child
33 // of this node, or a new id not previously in the tree. It is not
34 // allowed to "reparent" a child to this node without first removing
35 // that child from its previous parent.
36 // 3. When a new id appears in |node.child_ids|, the tree should create a
37 // new uninitialized placeholder node for it immediately. That
38 // placeholder must be updated within the same AXTreeUpdate, otherwise
39 // it's a fatal error. This guarantees the tree is always complete
40 // before or after an AXTreeUpdate.
41 template<typename AXNodeData> struct AXTreeUpdate {
42 AXTreeUpdate();
43 ~AXTreeUpdate();
45 // The id of a node to clear, before applying any updates,
46 // or 0 if no nodes should be cleared. Clearing a node means deleting
47 // all of its children and their descendants, but leaving that node in
48 // the tree. It's an error to clear a node but not subsequently update it
49 // as part of the tree update.
50 int node_id_to_clear;
52 // A vector of nodes to update, according to the rules above.
53 std::vector<AXNodeData> nodes;
55 // Return a multi-line indented string representation, for logging.
56 std::string ToString() const;
58 // TODO(dmazzoni): location changes
61 template<typename AXNodeData>
62 AXTreeUpdate<AXNodeData>::AXTreeUpdate() : node_id_to_clear(0) {
65 template<typename AXNodeData>
66 AXTreeUpdate<AXNodeData>::~AXTreeUpdate() {
69 template<typename AXNodeData>
70 std::string AXTreeUpdate<AXNodeData>::ToString() const {
71 std::string result;
72 if (node_id_to_clear != 0) {
73 result += "AXTreeUpdate: clear node " +
74 base::IntToString(node_id_to_clear) + "\n";
77 // The challenge here is that we want to indent the nodes being updated
78 // so that parent/child relationships are clear, but we don't have access
79 // to the rest of the tree for context, so we have to try to show the
80 // relative indentation of child nodes in this update relative to their
81 // parents.
82 base::hash_map<int32, int> id_to_indentation;
83 for (size_t i = 0; i < nodes.size(); ++i) {
84 int indent = id_to_indentation[nodes[i].id];
85 result += std::string(2 * indent, ' ');
86 result += nodes[i].ToString() + "\n";
87 for (size_t j = 0; j < nodes[i].child_ids.size(); ++j)
88 id_to_indentation[nodes[i].child_ids[j]] = indent + 1;
91 return result;
94 } // namespace ui
96 #endif // UI_ACCESSIBILITY_AX_TREE_UPDATE_H_