[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) {} |
| 86 | |
dmazzoni | 329fd01 | 2015-10-22 20:05:35 | [diff] [blame] | 87 | // Called when tree data changes. |
dmazzoni | 5e3acae9 | 2017-06-08 04:39:52 | [diff] [blame] | 88 | virtual void OnTreeDataChanged(AXTree* tree, |
| 89 | const ui::AXTreeData& old_data, |
| 90 | const ui::AXTreeData& new_data) = 0; |
[email protected] | e736e81b | 2014-02-24 07:15:58 | [diff] [blame] | 91 | // Called just before a node is deleted. Its id and data will be valid, |
| 92 | // but its links to parents and children are invalid. This is called |
| 93 | // in the middle of an update, the tree may be in an invalid state! |
dmazzoni | 09e7591 | 2015-06-02 23:31:56 | [diff] [blame] | 94 | virtual void OnNodeWillBeDeleted(AXTree* tree, AXNode* node) = 0; |
[email protected] | e736e81b | 2014-02-24 07:15:58 | [diff] [blame] | 95 | |
dmazzoni | a4b4891 | 2015-01-24 00:08:56 | [diff] [blame] | 96 | // Same as OnNodeWillBeDeleted, but only called once for an entire subtree. |
| 97 | // This is called in the middle of an update, the tree may be in an |
| 98 | // invalid state! |
dmazzoni | 09e7591 | 2015-06-02 23:31:56 | [diff] [blame] | 99 | virtual void OnSubtreeWillBeDeleted(AXTree* tree, AXNode* node) = 0; |
dmazzoni | a4b4891 | 2015-01-24 00:08:56 | [diff] [blame] | 100 | |
dtseng | 5a7b3d9 | 2016-09-08 06:35:58 | [diff] [blame] | 101 | // Called just before a node is deleted for reparenting. See |
| 102 | // |OnNodeWillBeDeleted| for additional information. |
| 103 | virtual void OnNodeWillBeReparented(AXTree* tree, AXNode* node) = 0; |
| 104 | |
| 105 | // Called just before a subtree is deleted for reparenting. See |
| 106 | // |OnSubtreeWillBeDeleted| for additional information. |
| 107 | virtual void OnSubtreeWillBeReparented(AXTree* tree, AXNode* node) = 0; |
| 108 | |
[email protected] | d96f384 | 2014-04-21 18:07:29 | [diff] [blame] | 109 | // Called immediately after a new node is created. The tree may be in |
| 110 | // the middle of an update, don't walk the parents and children now. |
dmazzoni | 09e7591 | 2015-06-02 23:31:56 | [diff] [blame] | 111 | virtual void OnNodeCreated(AXTree* tree, AXNode* node) = 0; |
[email protected] | e736e81b | 2014-02-24 07:15:58 | [diff] [blame] | 112 | |
dtseng | 5a7b3d9 | 2016-09-08 06:35:58 | [diff] [blame] | 113 | // Called immediately after a node is reparented. The tree may be in the |
| 114 | // middle of an update, don't walk the parents and children now. |
| 115 | virtual void OnNodeReparented(AXTree* tree, AXNode* node) = 0; |
| 116 | |
[email protected] | d96f384 | 2014-04-21 18:07:29 | [diff] [blame] | 117 | // Called when a node changes its data or children. The tree may be in |
| 118 | // the middle of an update, don't walk the parents and children now. |
dmazzoni | 09e7591 | 2015-06-02 23:31:56 | [diff] [blame] | 119 | virtual void OnNodeChanged(AXTree* tree, AXNode* node) = 0; |
[email protected] | e736e81b | 2014-02-24 07:15:58 | [diff] [blame] | 120 | |
dmazzoni | a4b4891 | 2015-01-24 00:08:56 | [diff] [blame] | 121 | enum ChangeType { |
| 122 | NODE_CREATED, |
| 123 | SUBTREE_CREATED, |
dtseng | 54e7474 | 2016-05-17 23:20:29 | [diff] [blame] | 124 | NODE_CHANGED, |
| 125 | NODE_REPARENTED, |
| 126 | SUBTREE_REPARENTED |
dmazzoni | a4b4891 | 2015-01-24 00:08:56 | [diff] [blame] | 127 | }; |
[email protected] | d96f384 | 2014-04-21 18:07:29 | [diff] [blame] | 128 | |
dmazzoni | a4b4891 | 2015-01-24 00:08:56 | [diff] [blame] | 129 | struct Change { |
| 130 | Change(AXNode* node, ChangeType type) { |
| 131 | this->node = node; |
| 132 | this->type = type; |
| 133 | } |
tfarina | d0bfb4b6 | 2015-02-18 17:20:32 | [diff] [blame] | 134 | AXNode* node; |
dmazzoni | a4b4891 | 2015-01-24 00:08:56 | [diff] [blame] | 135 | ChangeType type; |
| 136 | }; |
[email protected] | d96f384 | 2014-04-21 18:07:29 | [diff] [blame] | 137 | |
dmazzoni | a4b4891 | 2015-01-24 00:08:56 | [diff] [blame] | 138 | // Called at the end of the update operation. Every node that was added |
| 139 | // or changed will be included in |changes|, along with an enum indicating |
| 140 | // the type of change - either (1) a node was created, (2) a node was created |
| 141 | // and it's the root of a new subtree, or (3) a node was changed. Finally, |
| 142 | // a bool indicates if the root of the tree was changed or not. |
dmazzoni | 09e7591 | 2015-06-02 23:31:56 | [diff] [blame] | 143 | virtual void OnAtomicUpdateFinished(AXTree* tree, |
| 144 | bool root_changed, |
dmazzoni | a4b4891 | 2015-01-24 00:08:56 | [diff] [blame] | 145 | const std::vector<Change>& changes) = 0; |
[email protected] | e736e81b | 2014-02-24 07:15:58 | [diff] [blame] | 146 | }; |
[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 147 | |
| 148 | // AXTree is a live, managed tree of AXNode objects that can receive |
| 149 | // updates from another AXTreeSource via AXTreeUpdates, and it can be |
| 150 | // used as a source for sending updates to another client tree. |
| 151 | // It's designed to be subclassed to implement support for native |
| 152 | // accessibility APIs on a specific platform. |
| 153 | class AX_EXPORT AXTree { |
| 154 | public: |
| 155 | AXTree(); |
dmazzoni | 329fd01 | 2015-10-22 20:05:35 | [diff] [blame] | 156 | explicit AXTree(const AXTreeUpdate& initial_state); |
[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 157 | virtual ~AXTree(); |
| 158 | |
[email protected] | e736e81b | 2014-02-24 07:15:58 | [diff] [blame] | 159 | virtual void SetDelegate(AXTreeDelegate* delegate); |
| 160 | |
tfarina | 6b1c1e08 | 2015-02-20 23:47:07 | [diff] [blame] | 161 | AXNode* root() const { return root_; } |
| 162 | |
dmazzoni | 329fd01 | 2015-10-22 20:05:35 | [diff] [blame] | 163 | const AXTreeData& data() const { return data_; } |
| 164 | |
tfarina | 6b1c1e08 | 2015-02-20 23:47:07 | [diff] [blame] | 165 | // Returns the AXNode with the given |id| if it is part of this AXTree. |
avi | 9c81217b | 2015-12-24 23:40:05 | [diff] [blame] | 166 | AXNode* GetFromId(int32_t id) const; |
[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 167 | |
[email protected] | d4e27346 | 2013-12-04 04:37:58 | [diff] [blame] | 168 | // Returns true on success. If it returns false, it's a fatal error |
| 169 | // and this tree should be destroyed, and the source of the tree update |
| 170 | // should not be trusted any longer. |
dmazzoni | 329fd01 | 2015-10-22 20:05:35 | [diff] [blame] | 171 | virtual bool Unserialize(const AXTreeUpdate& update); |
| 172 | |
| 173 | virtual void UpdateData(const AXTreeData& data); |
[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 174 | |
Dominic Mazzoni | 2410fc6 | 2017-06-09 22:19:18 | [diff] [blame^] | 175 | // Convert any rectangle from the local coordinate space of one node in |
| 176 | // the tree, to bounds in the coordinate space of the tree. |
| 177 | gfx::RectF RelativeToTreeBounds(const AXNode* node, |
| 178 | gfx::RectF node_bounds) const; |
| 179 | |
| 180 | // Get the bounds of a node in the coordinate space of the tree. |
| 181 | gfx::RectF GetTreeBounds(const AXNode* node) const; |
| 182 | |
[email protected] | 5eec2f5 | 2014-01-06 22:30:54 | [diff] [blame] | 183 | // Return a multi-line indented string representation, for logging. |
| 184 | std::string ToString() const; |
| 185 | |
[email protected] | d4e27346 | 2013-12-04 04:37:58 | [diff] [blame] | 186 | // A string describing the error from an unsuccessful Unserialize, |
| 187 | // for testing and debugging. |
tfarina | d0bfb4b6 | 2015-02-18 17:20:32 | [diff] [blame] | 188 | const std::string& error() const { return error_; } |
[email protected] | d4e27346 | 2013-12-04 04:37:58 | [diff] [blame] | 189 | |
dmazzoni | ee2eaca | 2015-03-18 18:13:07 | [diff] [blame] | 190 | int size() { return static_cast<int>(id_map_.size()); } |
| 191 | |
[email protected] | e736e81b | 2014-02-24 07:15:58 | [diff] [blame] | 192 | private: |
dtseng | 5a7b3d9 | 2016-09-08 06:35:58 | [diff] [blame] | 193 | AXNode* CreateNode(AXNode* parent, |
| 194 | int32_t id, |
| 195 | int32_t index_in_parent, |
| 196 | AXTreeUpdateState* update_state); |
[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 197 | |
| 198 | // This is called from within Unserialize(), it returns true on success. |
dmazzoni | 67b4db2 | 2016-04-23 00:40:04 | [diff] [blame] | 199 | bool UpdateNode(const AXNodeData& src, |
| 200 | bool is_new_root, |
| 201 | AXTreeUpdateState* update_state); |
[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 202 | |
dmazzoni | 3ab5385c | 2017-03-13 18:07:03 | [diff] [blame] | 203 | void CallNodeChangeCallbacks(AXNode* node, const AXNodeData& new_data); |
| 204 | |
[email protected] | e736e81b | 2014-02-24 07:15:58 | [diff] [blame] | 205 | void OnRootChanged(); |
[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 206 | |
dmazzoni | a4b4891 | 2015-01-24 00:08:56 | [diff] [blame] | 207 | // Notify the delegate that the subtree rooted at |node| will be destroyed, |
| 208 | // then call DestroyNodeAndSubtree on it. |
dmazzoni | e3b7faf | 2015-06-01 17:56:36 | [diff] [blame] | 209 | void DestroySubtree(AXNode* node, AXTreeUpdateState* update_state); |
dmazzoni | a4b4891 | 2015-01-24 00:08:56 | [diff] [blame] | 210 | |
[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 211 | // Call Destroy() on |node|, and delete it from the id map, and then |
| 212 | // call recursively on all nodes in its subtree. |
dmazzoni | e3b7faf | 2015-06-01 17:56:36 | [diff] [blame] | 213 | void DestroyNodeAndSubtree(AXNode* node, AXTreeUpdateState* update_state); |
[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 214 | |
| 215 | // Iterate over the children of |node| and for each child, destroy the |
| 216 | // child and its subtree if its id is not in |new_child_ids|. Returns |
| 217 | // true on success, false on fatal error. |
| 218 | bool DeleteOldChildren(AXNode* node, |
avi | 9c81217b | 2015-12-24 23:40:05 | [diff] [blame] | 219 | const std::vector<int32_t>& new_child_ids, |
dmazzoni | e3b7faf | 2015-06-01 17:56:36 | [diff] [blame] | 220 | AXTreeUpdateState* update_state); |
[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 221 | |
| 222 | // Iterate over |new_child_ids| and populate |new_children| with |
| 223 | // pointers to child nodes, reusing existing nodes already in the tree |
| 224 | // if they exist, and creating otherwise. Reparenting is disallowed, so |
| 225 | // 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] | 226 | // error. Returns true on success, false on fatal error. |
[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 227 | bool CreateNewChildVector(AXNode* node, |
avi | 9c81217b | 2015-12-24 23:40:05 | [diff] [blame] | 228 | const std::vector<int32_t>& new_child_ids, |
[email protected] | d4e27346 | 2013-12-04 04:37:58 | [diff] [blame] | 229 | std::vector<AXNode*>* new_children, |
[email protected] | e736e81b | 2014-02-24 07:15:58 | [diff] [blame] | 230 | AXTreeUpdateState* update_state); |
[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 231 | |
[email protected] | e736e81b | 2014-02-24 07:15:58 | [diff] [blame] | 232 | AXTreeDelegate* delegate_; |
[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 233 | AXNode* root_; |
avi | 9c81217b | 2015-12-24 23:40:05 | [diff] [blame] | 234 | base::hash_map<int32_t, AXNode*> id_map_; |
[email protected] | d4e27346 | 2013-12-04 04:37:58 | [diff] [blame] | 235 | std::string error_; |
dmazzoni | 329fd01 | 2015-10-22 20:05:35 | [diff] [blame] | 236 | AXTreeData data_; |
[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 237 | }; |
| 238 | |
| 239 | } // namespace ui |
| 240 | |
| 241 | #endif // UI_ACCESSIBILITY_AX_TREE_H_ |