[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 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_H_ |
| 6 | #define UI_ACCESSIBILITY_AX_TREE_H_ |
| 7 | |
avi | 9c81217b | 2015-12-24 23:40:05 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
[email protected] | d4e27346 | 2013-12-04 04:37:58 | [diff] [blame] | 10 | #include <set> |
[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 11 | |
[email protected] | d4e27346 | 2013-12-04 04:37:58 | [diff] [blame] | 12 | #include "base/containers/hash_tables.h" |
[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 13 | #include "ui/accessibility/ax_export.h" |
dmazzoni | 329fd01 | 2015-10-22 20:05:35 | [diff] [blame] | 14 | #include "ui/accessibility/ax_node_data.h" |
| 15 | #include "ui/accessibility/ax_tree_data.h" |
[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 16 | #include "ui/accessibility/ax_tree_update.h" |
| 17 | |
| 18 | namespace ui { |
| 19 | |
| 20 | class AXNode; |
dmazzoni | 09e7591 | 2015-06-02 23:31:56 | [diff] [blame] | 21 | class AXTree; |
[email protected] | e736e81b | 2014-02-24 07:15:58 | [diff] [blame] | 22 | struct AXTreeUpdateState; |
| 23 | |
| 24 | // Used when you want to be notified when changes happen to the tree. |
[email protected] | d96f384 | 2014-04-21 18:07:29 | [diff] [blame] | 25 | // |
| 26 | // Some of the notifications are called in the middle of an update operation. |
| 27 | // Be careful, as the tree may be in an inconsistent state at this time; |
| 28 | // don't walk the parents and children at this time: |
| 29 | // OnNodeWillBeDeleted |
dmazzoni | a4b4891 | 2015-01-24 00:08:56 | [diff] [blame] | 30 | // OnSubtreeWillBeDeleted |
dtseng | 5a7b3d9 | 2016-09-08 06:35:58 | [diff] [blame] | 31 | // OnNodeWillBeReparented |
| 32 | // OnSubtreeWillBeReparented |
[email protected] | d96f384 | 2014-04-21 18:07:29 | [diff] [blame] | 33 | // OnNodeCreated |
dtseng | 5a7b3d9 | 2016-09-08 06:35:58 | [diff] [blame] | 34 | // OnNodeReparented |
[email protected] | d96f384 | 2014-04-21 18:07:29 | [diff] [blame] | 35 | // OnNodeChanged |
| 36 | // |
dmazzoni | a4b4891 | 2015-01-24 00:08:56 | [diff] [blame] | 37 | // In addition, one additional notification is fired at the end of an |
| 38 | // atomic update, and it provides a vector of nodes that were added or |
| 39 | // changed, for final postprocessing: |
| 40 | // OnAtomicUpdateFinished |
| 41 | // |
[email protected] | e736e81b | 2014-02-24 07:15:58 | [diff] [blame] | 42 | class AX_EXPORT AXTreeDelegate { |
| 43 | public: |
| 44 | AXTreeDelegate(); |
| 45 | virtual ~AXTreeDelegate(); |
| 46 | |
dtseng | f5424b3 | 2016-02-23 03:21:55 | [diff] [blame] | 47 | // Called before a node's data gets updated. |
| 48 | virtual void OnNodeDataWillChange(AXTree* tree, |
| 49 | const AXNodeData& old_node_data, |
| 50 | const AXNodeData& new_node_data) = 0; |
| 51 | |
dmazzoni | 3ab5385c | 2017-03-13 18:07:03 | [diff] [blame] | 52 | // Individual callbacks for every attribute of AXNodeData that can change. |
| 53 | virtual void OnRoleChanged(AXTree* tree, |
| 54 | AXNode* node, |
| 55 | AXRole old_role, |
| 56 | AXRole new_role) {} |
| 57 | virtual void OnStateChanged(AXTree* tree, |
| 58 | AXNode* node, |
| 59 | AXState state, |
| 60 | bool new_value) {} |
| 61 | virtual void OnStringAttributeChanged(AXTree* tree, |
| 62 | AXNode* node, |
| 63 | AXStringAttribute attr, |
| 64 | const std::string& old_value, |
| 65 | const std::string& new_value) {} |
| 66 | virtual void OnIntAttributeChanged(AXTree* tree, |
| 67 | AXNode* node, |
| 68 | AXIntAttribute attr, |
| 69 | int32_t old_value, |
| 70 | int32_t new_value) {} |
| 71 | virtual void OnFloatAttributeChanged(AXTree* tree, |
| 72 | AXNode* node, |
| 73 | AXFloatAttribute attr, |
| 74 | float old_value, |
| 75 | float new_value) {} |
| 76 | virtual void OnBoolAttributeChanged(AXTree* tree, |
| 77 | AXNode* node, |
| 78 | AXBoolAttribute attr, |
| 79 | bool new_value) {} |
| 80 | virtual void OnIntListAttributeChanged( |
| 81 | AXTree* tree, |
| 82 | AXNode* node, |
| 83 | AXIntListAttribute attr, |
| 84 | const std::vector<int32_t>& old_value, |
| 85 | const std::vector<int32_t>& new_value) {} |
yawano | b87aed4 | 2017-06-26 02:55:29 | [diff] [blame] | 86 | virtual void OnStringListAttributeChanged( |
| 87 | AXTree* tree, |
| 88 | AXNode* node, |
| 89 | AXStringListAttribute attr, |
| 90 | const std::vector<std::string>& old_value, |
| 91 | const std::vector<std::string>& new_value) {} |
dmazzoni | 3ab5385c | 2017-03-13 18:07:03 | [diff] [blame] | 92 | |
dmazzoni | 329fd01 | 2015-10-22 20:05:35 | [diff] [blame] | 93 | // Called when tree data changes. |
dmazzoni | 5e3acae9 | 2017-06-08 04:39:52 | [diff] [blame] | 94 | virtual void OnTreeDataChanged(AXTree* tree, |
| 95 | const ui::AXTreeData& old_data, |
| 96 | const ui::AXTreeData& new_data) = 0; |
[email protected] | e736e81b | 2014-02-24 07:15:58 | [diff] [blame] | 97 | // Called just before a node is deleted. Its id and data will be valid, |
| 98 | // but its links to parents and children are invalid. This is called |
| 99 | // in the middle of an update, the tree may be in an invalid state! |
dmazzoni | 09e7591 | 2015-06-02 23:31:56 | [diff] [blame] | 100 | virtual void OnNodeWillBeDeleted(AXTree* tree, AXNode* node) = 0; |
[email protected] | e736e81b | 2014-02-24 07:15:58 | [diff] [blame] | 101 | |
dmazzoni | a4b4891 | 2015-01-24 00:08:56 | [diff] [blame] | 102 | // Same as OnNodeWillBeDeleted, but only called once for an entire subtree. |
| 103 | // This is called in the middle of an update, the tree may be in an |
| 104 | // invalid state! |
dmazzoni | 09e7591 | 2015-06-02 23:31:56 | [diff] [blame] | 105 | virtual void OnSubtreeWillBeDeleted(AXTree* tree, AXNode* node) = 0; |
dmazzoni | a4b4891 | 2015-01-24 00:08:56 | [diff] [blame] | 106 | |
dtseng | 5a7b3d9 | 2016-09-08 06:35:58 | [diff] [blame] | 107 | // Called just before a node is deleted for reparenting. See |
| 108 | // |OnNodeWillBeDeleted| for additional information. |
| 109 | virtual void OnNodeWillBeReparented(AXTree* tree, AXNode* node) = 0; |
| 110 | |
| 111 | // Called just before a subtree is deleted for reparenting. See |
| 112 | // |OnSubtreeWillBeDeleted| for additional information. |
| 113 | virtual void OnSubtreeWillBeReparented(AXTree* tree, AXNode* node) = 0; |
| 114 | |
[email protected] | d96f384 | 2014-04-21 18:07:29 | [diff] [blame] | 115 | // Called immediately after a new node is created. The tree may be in |
| 116 | // the middle of an update, don't walk the parents and children now. |
dmazzoni | 09e7591 | 2015-06-02 23:31:56 | [diff] [blame] | 117 | virtual void OnNodeCreated(AXTree* tree, AXNode* node) = 0; |
[email protected] | e736e81b | 2014-02-24 07:15:58 | [diff] [blame] | 118 | |
dtseng | 5a7b3d9 | 2016-09-08 06:35:58 | [diff] [blame] | 119 | // Called immediately after a node is reparented. The tree may be in the |
| 120 | // middle of an update, don't walk the parents and children now. |
| 121 | virtual void OnNodeReparented(AXTree* tree, AXNode* node) = 0; |
| 122 | |
[email protected] | d96f384 | 2014-04-21 18:07:29 | [diff] [blame] | 123 | // Called when a node changes its data or children. The tree may be in |
| 124 | // the middle of an update, don't walk the parents and children now. |
dmazzoni | 09e7591 | 2015-06-02 23:31:56 | [diff] [blame] | 125 | virtual void OnNodeChanged(AXTree* tree, AXNode* node) = 0; |
[email protected] | e736e81b | 2014-02-24 07:15:58 | [diff] [blame] | 126 | |
dmazzoni | a4b4891 | 2015-01-24 00:08:56 | [diff] [blame] | 127 | enum ChangeType { |
| 128 | NODE_CREATED, |
| 129 | SUBTREE_CREATED, |
dtseng | 54e7474 | 2016-05-17 23:20:29 | [diff] [blame] | 130 | NODE_CHANGED, |
| 131 | NODE_REPARENTED, |
| 132 | SUBTREE_REPARENTED |
dmazzoni | a4b4891 | 2015-01-24 00:08:56 | [diff] [blame] | 133 | }; |
[email protected] | d96f384 | 2014-04-21 18:07:29 | [diff] [blame] | 134 | |
dmazzoni | a4b4891 | 2015-01-24 00:08:56 | [diff] [blame] | 135 | struct Change { |
| 136 | Change(AXNode* node, ChangeType type) { |
| 137 | this->node = node; |
| 138 | this->type = type; |
| 139 | } |
tfarina | d0bfb4b6 | 2015-02-18 17:20:32 | [diff] [blame] | 140 | AXNode* node; |
dmazzoni | a4b4891 | 2015-01-24 00:08:56 | [diff] [blame] | 141 | ChangeType type; |
| 142 | }; |
[email protected] | d96f384 | 2014-04-21 18:07:29 | [diff] [blame] | 143 | |
dmazzoni | a4b4891 | 2015-01-24 00:08:56 | [diff] [blame] | 144 | // Called at the end of the update operation. Every node that was added |
| 145 | // or changed will be included in |changes|, along with an enum indicating |
| 146 | // the type of change - either (1) a node was created, (2) a node was created |
| 147 | // and it's the root of a new subtree, or (3) a node was changed. Finally, |
| 148 | // a bool indicates if the root of the tree was changed or not. |
dmazzoni | 09e7591 | 2015-06-02 23:31:56 | [diff] [blame] | 149 | virtual void OnAtomicUpdateFinished(AXTree* tree, |
| 150 | bool root_changed, |
dmazzoni | a4b4891 | 2015-01-24 00:08:56 | [diff] [blame] | 151 | const std::vector<Change>& changes) = 0; |
[email protected] | e736e81b | 2014-02-24 07:15:58 | [diff] [blame] | 152 | }; |
[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 153 | |
| 154 | // AXTree is a live, managed tree of AXNode objects that can receive |
| 155 | // updates from another AXTreeSource via AXTreeUpdates, and it can be |
| 156 | // used as a source for sending updates to another client tree. |
| 157 | // It's designed to be subclassed to implement support for native |
| 158 | // accessibility APIs on a specific platform. |
| 159 | class AX_EXPORT AXTree { |
| 160 | public: |
| 161 | AXTree(); |
dmazzoni | 329fd01 | 2015-10-22 20:05:35 | [diff] [blame] | 162 | explicit AXTree(const AXTreeUpdate& initial_state); |
[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 163 | virtual ~AXTree(); |
| 164 | |
[email protected] | e736e81b | 2014-02-24 07:15:58 | [diff] [blame] | 165 | virtual void SetDelegate(AXTreeDelegate* delegate); |
Dominic Mazzoni | 4d523f72 | 2017-08-31 22:29:06 | [diff] [blame] | 166 | AXTreeDelegate* delegate() const { return delegate_; } |
[email protected] | e736e81b | 2014-02-24 07:15:58 | [diff] [blame] | 167 | |
tfarina | 6b1c1e08 | 2015-02-20 23:47:07 | [diff] [blame] | 168 | AXNode* root() const { return root_; } |
| 169 | |
dmazzoni | 329fd01 | 2015-10-22 20:05:35 | [diff] [blame] | 170 | const AXTreeData& data() const { return data_; } |
| 171 | |
tfarina | 6b1c1e08 | 2015-02-20 23:47:07 | [diff] [blame] | 172 | // Returns the AXNode with the given |id| if it is part of this AXTree. |
avi | 9c81217b | 2015-12-24 23:40:05 | [diff] [blame] | 173 | AXNode* GetFromId(int32_t id) const; |
[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 174 | |
[email protected] | d4e27346 | 2013-12-04 04:37:58 | [diff] [blame] | 175 | // Returns true on success. If it returns false, it's a fatal error |
| 176 | // and this tree should be destroyed, and the source of the tree update |
| 177 | // should not be trusted any longer. |
dmazzoni | 329fd01 | 2015-10-22 20:05:35 | [diff] [blame] | 178 | virtual bool Unserialize(const AXTreeUpdate& update); |
| 179 | |
| 180 | virtual void UpdateData(const AXTreeData& data); |
[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 181 | |
Dominic Mazzoni | 2410fc6 | 2017-06-09 22:19:18 | [diff] [blame] | 182 | // Convert any rectangle from the local coordinate space of one node in |
| 183 | // the tree, to bounds in the coordinate space of the tree. |
Katie Dektar | 2c6052d | 2017-09-27 00:32:32 | [diff] [blame] | 184 | // If set, updates |offscreen| boolean to be true if the node is offscreen |
| 185 | // relative to its rootWebArea. Callers should initialize |offscreen| |
| 186 | // to false: this method may get called multiple times in a row and |
| 187 | // |offscreen| will be propagated. |
Katie Dektar | db71ad94 | 2017-11-29 20:07:48 | [diff] [blame^] | 188 | // If |clip_bounds| is true, result bounds will be clipped. |
Dominic Mazzoni | 2410fc6 | 2017-06-09 22:19:18 | [diff] [blame] | 189 | gfx::RectF RelativeToTreeBounds(const AXNode* node, |
Katie Dektar | 2c6052d | 2017-09-27 00:32:32 | [diff] [blame] | 190 | gfx::RectF node_bounds, |
Katie Dektar | db71ad94 | 2017-11-29 20:07:48 | [diff] [blame^] | 191 | bool* offscreen = nullptr, |
| 192 | bool clip_bounds = true) const; |
Dominic Mazzoni | 2410fc6 | 2017-06-09 22:19:18 | [diff] [blame] | 193 | |
| 194 | // Get the bounds of a node in the coordinate space of the tree. |
Katie Dektar | 2c6052d | 2017-09-27 00:32:32 | [diff] [blame] | 195 | // If set, updates |offscreen| boolean to be true if the node is offscreen |
| 196 | // relative to its rootWebArea. Callers should initialize |offscreen| |
| 197 | // to false: this method may get called multiple times in a row and |
| 198 | // |offscreen| will be propagated. |
Katie Dektar | db71ad94 | 2017-11-29 20:07:48 | [diff] [blame^] | 199 | // If |clip_bounds| is true, result bounds will be clipped. |
| 200 | gfx::RectF GetTreeBounds(const AXNode* node, |
| 201 | bool* offscreen = nullptr, |
| 202 | bool clip_bounds = true) const; |
Dominic Mazzoni | 2410fc6 | 2017-06-09 22:19:18 | [diff] [blame] | 203 | |
Dominic Mazzoni | 35f2a525 | 2017-09-26 00:56:04 | [diff] [blame] | 204 | // Given a node ID attribute (one where IsNodeIdIntAttribute is true), |
| 205 | // and a destination node ID, return a set of all source node IDs that |
| 206 | // have that relationship attribute between them and the destination. |
| 207 | std::set<int32_t> GetReverseRelations(AXIntAttribute attr, int32_t dst_id); |
| 208 | |
| 209 | // Given a node ID list attribute (one where |
| 210 | // IsNodeIdIntListAttribute is true), and a destination node ID, |
| 211 | // return a set of all source node IDs that have that relationship |
| 212 | // attribute between them and the destination. |
| 213 | std::set<int32_t> GetReverseRelations(AXIntListAttribute attr, |
| 214 | int32_t dst_id); |
| 215 | |
[email protected] | 5eec2f5 | 2014-01-06 22:30:54 | [diff] [blame] | 216 | // Return a multi-line indented string representation, for logging. |
| 217 | std::string ToString() const; |
| 218 | |
[email protected] | d4e27346 | 2013-12-04 04:37:58 | [diff] [blame] | 219 | // A string describing the error from an unsuccessful Unserialize, |
| 220 | // for testing and debugging. |
tfarina | d0bfb4b6 | 2015-02-18 17:20:32 | [diff] [blame] | 221 | const std::string& error() const { return error_; } |
[email protected] | d4e27346 | 2013-12-04 04:37:58 | [diff] [blame] | 222 | |
dmazzoni | ee2eaca | 2015-03-18 18:13:07 | [diff] [blame] | 223 | int size() { return static_cast<int>(id_map_.size()); } |
| 224 | |
[email protected] | e736e81b | 2014-02-24 07:15:58 | [diff] [blame] | 225 | private: |
dtseng | 5a7b3d9 | 2016-09-08 06:35:58 | [diff] [blame] | 226 | AXNode* CreateNode(AXNode* parent, |
| 227 | int32_t id, |
| 228 | int32_t index_in_parent, |
| 229 | AXTreeUpdateState* update_state); |
[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 230 | |
| 231 | // This is called from within Unserialize(), it returns true on success. |
dmazzoni | 67b4db2 | 2016-04-23 00:40:04 | [diff] [blame] | 232 | bool UpdateNode(const AXNodeData& src, |
| 233 | bool is_new_root, |
| 234 | AXTreeUpdateState* update_state); |
[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 235 | |
dmazzoni | 3ab5385c | 2017-03-13 18:07:03 | [diff] [blame] | 236 | void CallNodeChangeCallbacks(AXNode* node, const AXNodeData& new_data); |
| 237 | |
Dominic Mazzoni | 35f2a525 | 2017-09-26 00:56:04 | [diff] [blame] | 238 | void UpdateReverseRelations(AXNode* node, const AXNodeData& new_data); |
| 239 | |
[email protected] | e736e81b | 2014-02-24 07:15:58 | [diff] [blame] | 240 | void OnRootChanged(); |
[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 241 | |
dmazzoni | a4b4891 | 2015-01-24 00:08:56 | [diff] [blame] | 242 | // Notify the delegate that the subtree rooted at |node| will be destroyed, |
| 243 | // then call DestroyNodeAndSubtree on it. |
dmazzoni | e3b7faf | 2015-06-01 17:56:36 | [diff] [blame] | 244 | void DestroySubtree(AXNode* node, AXTreeUpdateState* update_state); |
dmazzoni | a4b4891 | 2015-01-24 00:08:56 | [diff] [blame] | 245 | |
[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 246 | // Call Destroy() on |node|, and delete it from the id map, and then |
| 247 | // call recursively on all nodes in its subtree. |
dmazzoni | e3b7faf | 2015-06-01 17:56:36 | [diff] [blame] | 248 | void DestroyNodeAndSubtree(AXNode* node, AXTreeUpdateState* update_state); |
[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 249 | |
| 250 | // Iterate over the children of |node| and for each child, destroy the |
| 251 | // child and its subtree if its id is not in |new_child_ids|. Returns |
| 252 | // true on success, false on fatal error. |
| 253 | bool DeleteOldChildren(AXNode* node, |
avi | 9c81217b | 2015-12-24 23:40:05 | [diff] [blame] | 254 | const std::vector<int32_t>& new_child_ids, |
dmazzoni | e3b7faf | 2015-06-01 17:56:36 | [diff] [blame] | 255 | AXTreeUpdateState* update_state); |
[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 256 | |
| 257 | // Iterate over |new_child_ids| and populate |new_children| with |
| 258 | // pointers to child nodes, reusing existing nodes already in the tree |
| 259 | // if they exist, and creating otherwise. Reparenting is disallowed, so |
| 260 | // if the id already exists as the child of another node, that's an |
[email protected] | e736e81b | 2014-02-24 07:15:58 | [diff] [blame] | 261 | // error. Returns true on success, false on fatal error. |
[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 262 | bool CreateNewChildVector(AXNode* node, |
avi | 9c81217b | 2015-12-24 23:40:05 | [diff] [blame] | 263 | const std::vector<int32_t>& new_child_ids, |
[email protected] | d4e27346 | 2013-12-04 04:37:58 | [diff] [blame] | 264 | std::vector<AXNode*>* new_children, |
[email protected] | e736e81b | 2014-02-24 07:15:58 | [diff] [blame] | 265 | AXTreeUpdateState* update_state); |
[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 266 | |
[email protected] | e736e81b | 2014-02-24 07:15:58 | [diff] [blame] | 267 | AXTreeDelegate* delegate_; |
[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 268 | AXNode* root_; |
avi | 9c81217b | 2015-12-24 23:40:05 | [diff] [blame] | 269 | base::hash_map<int32_t, AXNode*> id_map_; |
[email protected] | d4e27346 | 2013-12-04 04:37:58 | [diff] [blame] | 270 | std::string error_; |
dmazzoni | 329fd01 | 2015-10-22 20:05:35 | [diff] [blame] | 271 | AXTreeData data_; |
Dominic Mazzoni | 35f2a525 | 2017-09-26 00:56:04 | [diff] [blame] | 272 | |
| 273 | // Map from an int attribute (if IsNodeIdIntAttribute is true) to |
| 274 | // a reverse mapping from target nodes to source nodes. |
| 275 | std::map<AXIntAttribute, std::map<int32_t, std::set<int32_t>>> |
| 276 | int_reverse_relations_; |
| 277 | // Map from an int list attribute (if IsNodeIdIntListAttribute is true) to |
| 278 | // a reverse mapping from target nodes to source nodes. |
| 279 | std::map<AXIntListAttribute, std::map<int32_t, std::set<int32_t>>> |
| 280 | intlist_reverse_relations_; |
[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 281 | }; |
| 282 | |
| 283 | } // namespace ui |
| 284 | |
| 285 | #endif // UI_ACCESSIBILITY_AX_TREE_H_ |