Flutter Engine
The Flutter Engine
ax_tree_update.h
Go to the documentation of this file.
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.
4
5#ifndef UI_ACCESSIBILITY_AX_TREE_UPDATE_H_
6#define UI_ACCESSIBILITY_AX_TREE_UPDATE_H_
7
8#include <cstddef>
9#include <cstdint>
10#include <string>
11#include <unordered_map>
12#include <vector>
13
14#include "ax_enum_util.h"
15#include "ax_enums.h"
16#include "ax_event_intent.h"
17#include "ax_node_data.h"
18#include "ax_tree_data.h"
19#include "base/string_utils.h"
20
21namespace ui {
22
23// An AXTreeUpdate is a serialized representation of an atomic change
24// to an AXTree. The sender and receiver must be in sync; the update
25// is only meant to bring the tree from a specific previous state into
26// its next state. Trying to apply it to the wrong tree should immediately
27// die with a fatal assertion.
28//
29// An AXTreeUpdate consists of an optional node id to clear (meaning
30// that all of that node's children and their descendants are deleted),
31// followed by an ordered vector of zero or more AXNodeData structures to
32// be applied to the tree in order. An update may also include an optional
33// update to the AXTreeData structure that applies to the tree as a whole.
34//
35// Suppose that the next AXNodeData to be applied is |node|. The following
36// invariants must hold:
37// 1. Either
38// a) |node.id| is already in the tree, or
39// b) the tree is empty, and
40// |node| is the new root of the tree, and
41// |node.role| == WebAXRoleRootWebArea.
42// 2. Every child id in |node.child_ids| must either be already a child
43// of this node, or a new id not previously in the tree. It is not
44// allowed to "reparent" a child to this node without first removing
45// that child from its previous parent.
46// 3. When a new id appears in |node.child_ids|, the tree should create a
47// new uninitialized placeholder node for it immediately. That
48// placeholder must be updated within the same AXTreeUpdate, otherwise
49// it's a fatal error. This guarantees the tree is always complete
50// before or after an AXTreeUpdate.
51template <typename AXNodeData, typename AXTreeData>
53 // If |has_tree_data| is true, the value of |tree_data| should be used
54 // to update the tree data, otherwise it should be ignored.
55 bool has_tree_data = false;
57
58 // The id of a node to clear, before applying any updates,
59 // or AXNode::kInvalidAXID if no nodes should be cleared. Clearing a node
60 // means deleting all of its children and their descendants, but leaving that
61 // node in the tree. It's an error to clear a node but not subsequently update
62 // it as part of the tree update.
64
65 // The id of the root of the tree, if the root is changing. This is
66 // required to be set if the root of the tree is changing or Unserialize
67 // will fail. If the root of the tree is not changing this is optional
68 // and it is allowed to pass 0.
69 int root_id = 0;
70
71 // A vector of nodes to update, according to the rules above.
72 std::vector<AXNodeData> nodes;
73
74 // The source of the event which generated this tree update.
76
77 // The event intents associated with this tree update.
78 std::vector<AXEventIntent> event_intents;
79
80 // Return a multi-line indented string representation, for logging.
81 std::string ToString() const;
82
83 // TODO(dmazzoni): location changes
84};
85
87
88template <typename AXNodeData, typename AXTreeData>
90 std::string result;
91
92 if (has_tree_data) {
93 result += "AXTreeUpdate tree data:" + tree_data.ToString() + "\n";
94 }
95
96 if (node_id_to_clear != AXNode::kInvalidAXID) {
97 result += "AXTreeUpdate: clear node " +
98 base::NumberToString(node_id_to_clear) + "\n";
99 }
100
101 if (root_id != AXNode::kInvalidAXID) {
102 result += "AXTreeUpdate: root id " + base::NumberToString(root_id) + "\n";
103 }
104
105 if (event_from != ax::mojom::EventFrom::kNone)
106 result += "event_from=" + std::string(ui::ToString(event_from)) + "\n";
107
108 if (!event_intents.empty()) {
109 result += "event_intents=[\n";
110 for (const auto& event_intent : event_intents)
111 result += " " + event_intent.ToString() + "\n";
112 result += "]\n";
113 }
114
115 // The challenge here is that we want to indent the nodes being updated
116 // so that parent/child relationships are clear, but we don't have access
117 // to the rest of the tree for context, so we have to try to show the
118 // relative indentation of child nodes in this update relative to their
119 // parents.
120 std::unordered_map<int32_t, int> id_to_indentation;
121 for (size_t i = 0; i < nodes.size(); ++i) {
122 int indent = id_to_indentation[nodes[i].id];
123 result += std::string(2 * indent, ' ');
124 result += nodes[i].ToString() + "\n";
125 for (size_t j = 0; j < nodes[i].child_ids.size(); ++j)
126 id_to_indentation[nodes[i].child_ids[j]] = indent + 1;
127 }
128
129 return result;
130}
131
132// Two tree updates can be merged into one if the second one
133// doesn't clear a subtree, doesn't have new tree data, and
134// doesn't have a new root id - in other words the second tree
135// update consists of only changes to nodes.
136template <typename AXNodeData, typename AXTreeData>
141 return false;
142
143 if (u2.has_tree_data && u2.tree_data != u1.tree_data)
144 return false;
145
146 if (u2.root_id != u1.root_id)
147 return false;
148
149 return true;
150}
151
152} // namespace ui
153
154#endif // UI_ACCESSIBILITY_AX_TREE_UPDATE_H_
static constexpr AXID kInvalidAXID
Definition: ax_node.h:41
GAsyncResult * result
std::string NumberToString(int32_t number)
Definition: string_utils.cc:91
bool TreeUpdatesCanBeMerged(const AXTreeUpdateBase< AXNodeData, AXTreeData > &u1, const AXTreeUpdateBase< AXNodeData, AXTreeData > &u2)
const char * ToString(ax::mojom::Event event)
Definition: ax_enum_util.cc:9
ax::mojom::EventFrom event_from
std::vector< AXEventIntent > event_intents
std::string ToString() const
std::vector< AXNodeData > nodes