blob: 36fe655787692cdf9019357790096cd168b0f09f [file] [log] [blame]
[email protected]4b02bbca2013-11-22 08:59:031// 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
avi9c81217b2015-12-24 23:40:058#include <stdint.h>
9
[email protected]d4e273462013-12-04 04:37:5810#include <set>
[email protected]4b02bbca2013-11-22 08:59:0311
[email protected]d4e273462013-12-04 04:37:5812#include "base/containers/hash_tables.h"
[email protected]4b02bbca2013-11-22 08:59:0313#include "ui/accessibility/ax_export.h"
dmazzoni329fd012015-10-22 20:05:3514#include "ui/accessibility/ax_node_data.h"
15#include "ui/accessibility/ax_tree_data.h"
[email protected]4b02bbca2013-11-22 08:59:0316#include "ui/accessibility/ax_tree_update.h"
17
18namespace ui {
19
20class AXNode;
dmazzoni09e75912015-06-02 23:31:5621class AXTree;
[email protected]e736e81b2014-02-24 07:15:5822struct AXTreeUpdateState;
23
24// Used when you want to be notified when changes happen to the tree.
[email protected]d96f3842014-04-21 18:07:2925//
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
dmazzonia4b48912015-01-24 00:08:5630// OnSubtreeWillBeDeleted
dtseng5a7b3d92016-09-08 06:35:5831// OnNodeWillBeReparented
32// OnSubtreeWillBeReparented
[email protected]d96f3842014-04-21 18:07:2933// OnNodeCreated
dtseng5a7b3d92016-09-08 06:35:5834// OnNodeReparented
[email protected]d96f3842014-04-21 18:07:2935// OnNodeChanged
36//
dmazzonia4b48912015-01-24 00:08:5637// 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]e736e81b2014-02-24 07:15:5842class AX_EXPORT AXTreeDelegate {
43 public:
44 AXTreeDelegate();
45 virtual ~AXTreeDelegate();
46
dtsengf5424b32016-02-23 03:21:5547 // 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
dmazzoni3ab5385c2017-03-13 18:07:0352 // 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
dmazzoni329fd012015-10-22 20:05:3587 // Called when tree data changes.
dmazzoni5e3acae92017-06-08 04:39:5288 virtual void OnTreeDataChanged(AXTree* tree,
89 const ui::AXTreeData& old_data,
90 const ui::AXTreeData& new_data) = 0;
[email protected]e736e81b2014-02-24 07:15:5891 // 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!
dmazzoni09e75912015-06-02 23:31:5694 virtual void OnNodeWillBeDeleted(AXTree* tree, AXNode* node) = 0;
[email protected]e736e81b2014-02-24 07:15:5895
dmazzonia4b48912015-01-24 00:08:5696 // 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!
dmazzoni09e75912015-06-02 23:31:5699 virtual void OnSubtreeWillBeDeleted(AXTree* tree, AXNode* node) = 0;
dmazzonia4b48912015-01-24 00:08:56100
dtseng5a7b3d92016-09-08 06:35:58101 // 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]d96f3842014-04-21 18:07:29109 // 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.
dmazzoni09e75912015-06-02 23:31:56111 virtual void OnNodeCreated(AXTree* tree, AXNode* node) = 0;
[email protected]e736e81b2014-02-24 07:15:58112
dtseng5a7b3d92016-09-08 06:35:58113 // 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]d96f3842014-04-21 18:07:29117 // 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.
dmazzoni09e75912015-06-02 23:31:56119 virtual void OnNodeChanged(AXTree* tree, AXNode* node) = 0;
[email protected]e736e81b2014-02-24 07:15:58120
dmazzonia4b48912015-01-24 00:08:56121 enum ChangeType {
122 NODE_CREATED,
123 SUBTREE_CREATED,
dtseng54e74742016-05-17 23:20:29124 NODE_CHANGED,
125 NODE_REPARENTED,
126 SUBTREE_REPARENTED
dmazzonia4b48912015-01-24 00:08:56127 };
[email protected]d96f3842014-04-21 18:07:29128
dmazzonia4b48912015-01-24 00:08:56129 struct Change {
130 Change(AXNode* node, ChangeType type) {
131 this->node = node;
132 this->type = type;
133 }
tfarinad0bfb4b62015-02-18 17:20:32134 AXNode* node;
dmazzonia4b48912015-01-24 00:08:56135 ChangeType type;
136 };
[email protected]d96f3842014-04-21 18:07:29137
dmazzonia4b48912015-01-24 00:08:56138 // 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.
dmazzoni09e75912015-06-02 23:31:56143 virtual void OnAtomicUpdateFinished(AXTree* tree,
144 bool root_changed,
dmazzonia4b48912015-01-24 00:08:56145 const std::vector<Change>& changes) = 0;
[email protected]e736e81b2014-02-24 07:15:58146};
[email protected]4b02bbca2013-11-22 08:59:03147
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.
153class AX_EXPORT AXTree {
154 public:
155 AXTree();
dmazzoni329fd012015-10-22 20:05:35156 explicit AXTree(const AXTreeUpdate& initial_state);
[email protected]4b02bbca2013-11-22 08:59:03157 virtual ~AXTree();
158
[email protected]e736e81b2014-02-24 07:15:58159 virtual void SetDelegate(AXTreeDelegate* delegate);
160
tfarina6b1c1e082015-02-20 23:47:07161 AXNode* root() const { return root_; }
162
dmazzoni329fd012015-10-22 20:05:35163 const AXTreeData& data() const { return data_; }
164
tfarina6b1c1e082015-02-20 23:47:07165 // Returns the AXNode with the given |id| if it is part of this AXTree.
avi9c81217b2015-12-24 23:40:05166 AXNode* GetFromId(int32_t id) const;
[email protected]4b02bbca2013-11-22 08:59:03167
[email protected]d4e273462013-12-04 04:37:58168 // 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.
dmazzoni329fd012015-10-22 20:05:35171 virtual bool Unserialize(const AXTreeUpdate& update);
172
173 virtual void UpdateData(const AXTreeData& data);
[email protected]4b02bbca2013-11-22 08:59:03174
Dominic Mazzoni2410fc62017-06-09 22:19:18175 // 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]5eec2f52014-01-06 22:30:54183 // Return a multi-line indented string representation, for logging.
184 std::string ToString() const;
185
[email protected]d4e273462013-12-04 04:37:58186 // A string describing the error from an unsuccessful Unserialize,
187 // for testing and debugging.
tfarinad0bfb4b62015-02-18 17:20:32188 const std::string& error() const { return error_; }
[email protected]d4e273462013-12-04 04:37:58189
dmazzoniee2eaca2015-03-18 18:13:07190 int size() { return static_cast<int>(id_map_.size()); }
191
[email protected]e736e81b2014-02-24 07:15:58192 private:
dtseng5a7b3d92016-09-08 06:35:58193 AXNode* CreateNode(AXNode* parent,
194 int32_t id,
195 int32_t index_in_parent,
196 AXTreeUpdateState* update_state);
[email protected]4b02bbca2013-11-22 08:59:03197
198 // This is called from within Unserialize(), it returns true on success.
dmazzoni67b4db22016-04-23 00:40:04199 bool UpdateNode(const AXNodeData& src,
200 bool is_new_root,
201 AXTreeUpdateState* update_state);
[email protected]4b02bbca2013-11-22 08:59:03202
dmazzoni3ab5385c2017-03-13 18:07:03203 void CallNodeChangeCallbacks(AXNode* node, const AXNodeData& new_data);
204
[email protected]e736e81b2014-02-24 07:15:58205 void OnRootChanged();
[email protected]4b02bbca2013-11-22 08:59:03206
dmazzonia4b48912015-01-24 00:08:56207 // Notify the delegate that the subtree rooted at |node| will be destroyed,
208 // then call DestroyNodeAndSubtree on it.
dmazzonie3b7faf2015-06-01 17:56:36209 void DestroySubtree(AXNode* node, AXTreeUpdateState* update_state);
dmazzonia4b48912015-01-24 00:08:56210
[email protected]4b02bbca2013-11-22 08:59:03211 // Call Destroy() on |node|, and delete it from the id map, and then
212 // call recursively on all nodes in its subtree.
dmazzonie3b7faf2015-06-01 17:56:36213 void DestroyNodeAndSubtree(AXNode* node, AXTreeUpdateState* update_state);
[email protected]4b02bbca2013-11-22 08:59:03214
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,
avi9c81217b2015-12-24 23:40:05219 const std::vector<int32_t>& new_child_ids,
dmazzonie3b7faf2015-06-01 17:56:36220 AXTreeUpdateState* update_state);
[email protected]4b02bbca2013-11-22 08:59:03221
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]e736e81b2014-02-24 07:15:58226 // error. Returns true on success, false on fatal error.
[email protected]4b02bbca2013-11-22 08:59:03227 bool CreateNewChildVector(AXNode* node,
avi9c81217b2015-12-24 23:40:05228 const std::vector<int32_t>& new_child_ids,
[email protected]d4e273462013-12-04 04:37:58229 std::vector<AXNode*>* new_children,
[email protected]e736e81b2014-02-24 07:15:58230 AXTreeUpdateState* update_state);
[email protected]4b02bbca2013-11-22 08:59:03231
[email protected]e736e81b2014-02-24 07:15:58232 AXTreeDelegate* delegate_;
[email protected]4b02bbca2013-11-22 08:59:03233 AXNode* root_;
avi9c81217b2015-12-24 23:40:05234 base::hash_map<int32_t, AXNode*> id_map_;
[email protected]d4e273462013-12-04 04:37:58235 std::string error_;
dmazzoni329fd012015-10-22 20:05:35236 AXTreeData data_;
[email protected]4b02bbca2013-11-22 08:59:03237};
238
239} // namespace ui
240
241#endif // UI_ACCESSIBILITY_AX_TREE_H_