blob: d056df49a3410433f8c287b9e9dd020083d5bf18 [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
Chris Hall34208182019-03-13 02:26:1810#include <memory>
[email protected]d4e273462013-12-04 04:37:5811#include <set>
Takuto Ikutaadf31eb2019-01-05 00:32:4812#include <unordered_map>
[email protected]4b02bbca2013-11-22 08:59:0313
Dominic Mazzoni8549eb682018-12-11 23:48:3214#include "base/observer_list.h"
[email protected]4b02bbca2013-11-22 08:59:0315#include "ui/accessibility/ax_export.h"
Dominic Mazzoniecfb4fd2018-10-23 07:34:2016#include "ui/accessibility/ax_node.h"
dmazzoni329fd012015-10-22 20:05:3517#include "ui/accessibility/ax_node_data.h"
18#include "ui/accessibility/ax_tree_data.h"
[email protected]4b02bbca2013-11-22 08:59:0319#include "ui/accessibility/ax_tree_update.h"
20
21namespace ui {
22
Dominic Mazzoni3d9b5b92018-04-18 21:36:3823class AXTableInfo;
dmazzoni09e75912015-06-02 23:31:5624class AXTree;
Dominic Mazzoni8549eb682018-12-11 23:48:3225class AXTreeObserver;
[email protected]e736e81b2014-02-24 07:15:5826struct AXTreeUpdateState;
Chris Hall697d99b2019-07-09 02:36:1127class AXLanguageDetectionManager;
[email protected]e736e81b2014-02-24 07:15:5828
[email protected]4b02bbca2013-11-22 08:59:0329// AXTree is a live, managed tree of AXNode objects that can receive
30// updates from another AXTreeSource via AXTreeUpdates, and it can be
31// used as a source for sending updates to another client tree.
32// It's designed to be subclassed to implement support for native
33// accessibility APIs on a specific platform.
Dominic Mazzoniecfb4fd2018-10-23 07:34:2034class AX_EXPORT AXTree : public AXNode::OwnerTree {
[email protected]4b02bbca2013-11-22 08:59:0335 public:
David Tsengef6b480d2018-02-19 12:48:4236 typedef std::map<ax::mojom::IntAttribute,
37 std::map<int32_t, std::set<int32_t>>>
38 IntReverseRelationMap;
39 typedef std::map<ax::mojom::IntListAttribute,
40 std::map<int32_t, std::set<int32_t>>>
41 IntListReverseRelationMap;
42
[email protected]4b02bbca2013-11-22 08:59:0343 AXTree();
dmazzoni329fd012015-10-22 20:05:3544 explicit AXTree(const AXTreeUpdate& initial_state);
[email protected]4b02bbca2013-11-22 08:59:0345 virtual ~AXTree();
46
Dominic Mazzoni8549eb682018-12-11 23:48:3247 void AddObserver(AXTreeObserver* observer);
48 bool HasObserver(AXTreeObserver* observer);
49 void RemoveObserver(const AXTreeObserver* observer);
50
51 base::ObserverList<AXTreeObserver>& observers() { return observers_; }
[email protected]e736e81b2014-02-24 07:15:5852
tfarina6b1c1e082015-02-20 23:47:0753 AXNode* root() const { return root_; }
54
dmazzoni329fd012015-10-22 20:05:3555 const AXTreeData& data() const { return data_; }
56
Dominic Mazzoniecfb4fd2018-10-23 07:34:2057 // AXNode::OwnerTree override.
Adam Ettenberger86af2532019-09-17 20:04:4658 // Returns the globally unique ID of this accessibility tree.
59 AXTreeID GetAXTreeID() const override;
60
61 // AXNode::OwnerTree override.
tfarina6b1c1e082015-02-20 23:47:0762 // Returns the AXNode with the given |id| if it is part of this AXTree.
Dominic Mazzoniecfb4fd2018-10-23 07:34:2063 AXNode* GetFromId(int32_t id) const override;
[email protected]4b02bbca2013-11-22 08:59:0364
[email protected]d4e273462013-12-04 04:37:5865 // Returns true on success. If it returns false, it's a fatal error
66 // and this tree should be destroyed, and the source of the tree update
67 // should not be trusted any longer.
dmazzoni329fd012015-10-22 20:05:3568 virtual bool Unserialize(const AXTreeUpdate& update);
69
70 virtual void UpdateData(const AXTreeData& data);
[email protected]4b02bbca2013-11-22 08:59:0371
Dominic Mazzoni2410fc62017-06-09 22:19:1872 // Convert any rectangle from the local coordinate space of one node in
73 // the tree, to bounds in the coordinate space of the tree.
Katie Dektar2c6052d2017-09-27 00:32:3274 // If set, updates |offscreen| boolean to be true if the node is offscreen
75 // relative to its rootWebArea. Callers should initialize |offscreen|
76 // to false: this method may get called multiple times in a row and
77 // |offscreen| will be propagated.
Katie Dektardb71ad942017-11-29 20:07:4878 // If |clip_bounds| is true, result bounds will be clipped.
Dominic Mazzoni2410fc62017-06-09 22:19:1879 gfx::RectF RelativeToTreeBounds(const AXNode* node,
Katie Dektar2c6052d2017-09-27 00:32:3280 gfx::RectF node_bounds,
Katie Dektardb71ad942017-11-29 20:07:4881 bool* offscreen = nullptr,
82 bool clip_bounds = true) const;
Dominic Mazzoni2410fc62017-06-09 22:19:1883
84 // Get the bounds of a node in the coordinate space of the tree.
Katie Dektar2c6052d2017-09-27 00:32:3285 // If set, updates |offscreen| boolean to be true if the node is offscreen
86 // relative to its rootWebArea. Callers should initialize |offscreen|
87 // to false: this method may get called multiple times in a row and
88 // |offscreen| will be propagated.
Katie Dektardb71ad942017-11-29 20:07:4889 // If |clip_bounds| is true, result bounds will be clipped.
90 gfx::RectF GetTreeBounds(const AXNode* node,
91 bool* offscreen = nullptr,
92 bool clip_bounds = true) const;
Dominic Mazzoni2410fc62017-06-09 22:19:1893
Dominic Mazzoni35f2a5252017-09-26 00:56:0494 // Given a node ID attribute (one where IsNodeIdIntAttribute is true),
95 // and a destination node ID, return a set of all source node IDs that
96 // have that relationship attribute between them and the destination.
Dominic Mazzonidcef1b732018-01-26 17:57:0497 std::set<int32_t> GetReverseRelations(ax::mojom::IntAttribute attr,
Dominic Mazzoni94a4da62017-12-22 22:49:3898 int32_t dst_id) const;
Dominic Mazzoni35f2a5252017-09-26 00:56:0499
100 // Given a node ID list attribute (one where
101 // IsNodeIdIntListAttribute is true), and a destination node ID,
102 // return a set of all source node IDs that have that relationship
103 // attribute between them and the destination.
Dominic Mazzonidcef1b732018-01-26 17:57:04104 std::set<int32_t> GetReverseRelations(ax::mojom::IntListAttribute attr,
Dominic Mazzoni94a4da62017-12-22 22:49:38105 int32_t dst_id) const;
Dominic Mazzoni35f2a5252017-09-26 00:56:04106
Dominic Mazzoniaa774822018-08-29 20:33:58107 // Given a child tree ID, return the node IDs of all nodes in the tree who
108 // have a kChildTreeId int attribute with that value.
Dominic Mazzoni336bc0062018-09-23 16:46:43109 std::set<int32_t> GetNodeIdsForChildTreeId(AXTreeID child_tree_id) const;
Dominic Mazzoniaa774822018-08-29 20:33:58110
Dominic Mazzoni748888c2018-11-01 22:34:59111 // Get all of the child tree IDs referenced by any node in this tree.
112 const std::set<AXTreeID> GetAllChildTreeIds() const;
113
David Tsengef6b480d2018-02-19 12:48:42114 // Map from a relation attribute to a map from a target id to source ids.
115 const IntReverseRelationMap& int_reverse_relations() {
116 return int_reverse_relations_;
117 }
118 const IntListReverseRelationMap& intlist_reverse_relations() {
119 return intlist_reverse_relations_;
120 }
Dominic Mazzoni3d9b5b92018-04-18 21:36:38121
[email protected]5eec2f52014-01-06 22:30:54122 // Return a multi-line indented string representation, for logging.
123 std::string ToString() const;
124
[email protected]d4e273462013-12-04 04:37:58125 // A string describing the error from an unsuccessful Unserialize,
126 // for testing and debugging.
tfarinad0bfb4b62015-02-18 17:20:32127 const std::string& error() const { return error_; }
[email protected]d4e273462013-12-04 04:37:58128
dmazzoniee2eaca2015-03-18 18:13:07129 int size() { return static_cast<int>(id_map_.size()); }
130
Dominic Mazzonid42e00a2018-06-27 23:14:23131 // Call this to enable support for extra Mac nodes - for each table,
132 // a table column header and a node for each column.
133 void SetEnableExtraMacNodes(bool enabled);
134 bool enable_extra_mac_nodes() const { return enable_extra_mac_nodes_; }
135
136 // Return a negative number that's suitable to use for a node ID for
137 // internal nodes created automatically by an AXTree, so as not to
138 // conflict with positive-numbered node IDs from tree sources.
139 int32_t GetNextNegativeInternalNodeId();
140
Akihiro Ota886a96d62018-12-18 00:11:48141 // Returns the pos_in_set of node. Looks in ordered_set_info_map_ for cached
142 // value. Calculates pos_in_set and set_size for node (and all other nodes in
Akihiro Ota413ca722018-12-03 23:29:00143 // the same ordered set) if no value is present in the cache.
144 // This function is guaranteed to be only called on nodes that can hold
145 // pos_in_set values, minimizing the size of the cache.
Akihiro Ota886a96d62018-12-18 00:11:48146 int32_t GetPosInSet(const AXNode& node, const AXNode* ordered_set) override;
Akihiro Ota413ca722018-12-03 23:29:00147 // Returns the set_size of node. Looks in ordered_set_info_map_ for cached
148 // value. Calculates pos_inset_set and set_size for node (and all other nodes
149 // in the same ordered set) if no value is present in the cache.
150 // This function is guaranteed to be only called on nodes that can hold
151 // set_size values, minimizing the size of the cache.
Akihiro Ota886a96d62018-12-18 00:11:48152 int32_t GetSetSize(const AXNode& node, const AXNode* ordered_set) override;
Akihiro Ota413ca722018-12-03 23:29:00153
Jacques Newman339afc62019-08-14 00:49:22154 Selection GetUnignoredSelection() const override;
155
Akihiro Otae3e420e2019-04-17 19:57:40156 bool GetTreeUpdateInProgressState() const override;
157 void SetTreeUpdateInProgressState(bool set_tree_update_value);
158
Kurt Catti-Schmidtc8445a12019-08-07 18:52:58159 // AXNode::OwnerTree override.
160 // Returns true if the tree represents a paginated document
161 bool HasPaginationSupport() const override;
162
Chris Hall697d99b2019-07-09 02:36:11163 // Language detection manager, entry point to language detection features.
164 // TODO(chrishall): Should this be stored by pointer or value?
165 // When should we initialize this?
166 std::unique_ptr<AXLanguageDetectionManager> language_detection_manager;
Chris Hall34208182019-03-13 02:26:18167
[email protected]e736e81b2014-02-24 07:15:58168 private:
Dominic Mazzoniecfb4fd2018-10-23 07:34:20169 friend class AXTableInfoTest;
170
171 // AXNode::OwnerTree override.
172 //
173 // Given a node in this accessibility tree that corresponds to a table
174 // or grid, return an object containing information about the
175 // table structure. This object is computed lazily on-demand and
176 // cached until the next time the tree is updated. Clients should
177 // not retain this pointer, they should just request it every time
178 // it's needed.
179 //
180 // Returns nullptr if the node is not a valid table.
181 AXTableInfo* GetTableInfo(const AXNode* table_node) const override;
182
dtseng5a7b3d92016-09-08 06:35:58183 AXNode* CreateNode(AXNode* parent,
Adam Ettenbergerd9d8d58a2019-08-06 16:54:08184 AXNode::AXID id,
Peter Kasting94a07a12019-05-22 19:26:28185 size_t index_in_parent,
dtseng5a7b3d92016-09-08 06:35:58186 AXTreeUpdateState* update_state);
[email protected]4b02bbca2013-11-22 08:59:03187
Adam Ettenbergerd9d8d58a2019-08-06 16:54:08188 // Accumulates the work that will be required to update the AXTree.
189 // This allows us to notify observers of structure changes when the
190 // tree is still in a stable and unchanged state.
191 bool ComputePendingChanges(const AXTreeUpdate& update,
192 AXTreeUpdateState& update_state);
193
194 // Populates |update_state| with information about actions that will
195 // be performed on the tree during the update, such as adding or
196 // removing nodes in the tree. Returns true on success.
197 // Nothing within this call should modify tree structure or node data.
198 bool ComputePendingChangesToNode(const AXNodeData& new_data,
199 bool is_new_root,
200 AXTreeUpdateState* update_state);
201
[email protected]4b02bbca2013-11-22 08:59:03202 // This is called from within Unserialize(), it returns true on success.
dmazzoni67b4db22016-04-23 00:40:04203 bool UpdateNode(const AXNodeData& src,
204 bool is_new_root,
205 AXTreeUpdateState* update_state);
[email protected]4b02bbca2013-11-22 08:59:03206
Adam Ettenbergerd9d8d58a2019-08-06 16:54:08207 // Notify the delegate that the subtree rooted at |node| will be
208 // destroyed or reparented.
209 void NotifySubtreeWillBeReparentedOrDeleted(
210 AXNode* node,
211 const AXTreeUpdateState* update_state);
212
213 // Notify the delegate that |node| will be destroyed or reparented.
214 void NotifyNodeWillBeReparentedOrDeleted(
215 AXNode* node,
216 const AXTreeUpdateState* update_state);
217
218 // Notify the delegate that |node| and all of its descendants will be
219 // destroyed.
220 void RecursivelyNotifyNodeWillBeDeleted(AXNode* node);
221
222 // Notify the delegate that |node| has been created or reparented.
223 void NotifyNodeHasBeenReparentedOrCreated(
224 AXNode* node,
225 const AXTreeUpdateState* update_state);
226
Adam Ettenberger05afcec2019-08-06 17:11:29227 // Notify the delegate that a node will change its data.
228 void NotifyNodeDataWillChange(const AXNodeData& old_data,
229 const AXNodeData& new_data);
230
231 // Notify the delegate that |node| has changed its data.
232 void NotifyNodeDataHasBeenChanged(AXNode* node,
233 const AXNodeData& old_data,
234 const AXNodeData& new_data);
dmazzoni3ab5385c2017-03-13 18:07:03235
Dominic Mazzoni35f2a5252017-09-26 00:56:04236 void UpdateReverseRelations(AXNode* node, const AXNodeData& new_data);
237
Adam Ettenbergerd9d8d58a2019-08-06 16:54:08238 // Returns true if all pending changes in the |update_state| have been
239 // handled. If this returns false, the |error_| message will be populated.
240 // It's a fatal error to have pending changes after exhausting
241 // the AXTreeUpdate.
242 bool ValidatePendingChangesComplete(const AXTreeUpdateState& update_state);
[email protected]4b02bbca2013-11-22 08:59:03243
Adam Ettenbergerd9d8d58a2019-08-06 16:54:08244 // Modifies |update_state| so that it knows what subtree and nodes are
245 // going to be destroyed for the subtree rooted at |node|.
246 void MarkSubtreeForDestruction(AXNode::AXID node_id,
247 AXTreeUpdateState* update_state);
248
249 // Modifies |update_state| so that it knows what nodes are
250 // going to be destroyed for the subtree rooted at |node|.
251 void MarkNodesForDestructionRecursive(AXNode::AXID node_id,
252 AXTreeUpdateState* update_state);
253
254 // Validates that destroying the subtree rooted at |node| has required
255 // information in |update_state|, then calls DestroyNodeAndSubtree on it.
dmazzonie3b7faf2015-06-01 17:56:36256 void DestroySubtree(AXNode* node, AXTreeUpdateState* update_state);
dmazzonia4b48912015-01-24 00:08:56257
[email protected]4b02bbca2013-11-22 08:59:03258 // Call Destroy() on |node|, and delete it from the id map, and then
259 // call recursively on all nodes in its subtree.
dmazzonie3b7faf2015-06-01 17:56:36260 void DestroyNodeAndSubtree(AXNode* node, AXTreeUpdateState* update_state);
[email protected]4b02bbca2013-11-22 08:59:03261
262 // Iterate over the children of |node| and for each child, destroy the
Adam Ettenbergerd9d8d58a2019-08-06 16:54:08263 // child and its subtree if its id is not in |new_child_ids|.
264 void DeleteOldChildren(AXNode* node,
avi9c81217b2015-12-24 23:40:05265 const std::vector<int32_t>& new_child_ids,
dmazzonie3b7faf2015-06-01 17:56:36266 AXTreeUpdateState* update_state);
[email protected]4b02bbca2013-11-22 08:59:03267
268 // Iterate over |new_child_ids| and populate |new_children| with
269 // pointers to child nodes, reusing existing nodes already in the tree
270 // if they exist, and creating otherwise. Reparenting is disallowed, so
271 // if the id already exists as the child of another node, that's an
[email protected]e736e81b2014-02-24 07:15:58272 // error. Returns true on success, false on fatal error.
[email protected]4b02bbca2013-11-22 08:59:03273 bool CreateNewChildVector(AXNode* node,
avi9c81217b2015-12-24 23:40:05274 const std::vector<int32_t>& new_child_ids,
[email protected]d4e273462013-12-04 04:37:58275 std::vector<AXNode*>* new_children,
[email protected]e736e81b2014-02-24 07:15:58276 AXTreeUpdateState* update_state);
[email protected]4b02bbca2013-11-22 08:59:03277
Dominic Mazzonia1bb0d122019-09-26 20:19:59278 // Internal implementation of RelativeToTreeBounds. It calls itself
279 // recursively but ensures that it can only do so exactly once!
280 gfx::RectF RelativeToTreeBoundsInternal(const AXNode* node,
281 gfx::RectF node_bounds,
282 bool* offscreen,
283 bool clip_bounds,
284 bool allow_recursion) const;
285
Dominic Mazzoni8549eb682018-12-11 23:48:32286 base::ObserverList<AXTreeObserver> observers_;
Brett Wilson0feae3a2017-12-06 03:16:56287 AXNode* root_ = nullptr;
Takuto Ikutaadf31eb2019-01-05 00:32:48288 std::unordered_map<int32_t, AXNode*> id_map_;
[email protected]d4e273462013-12-04 04:37:58289 std::string error_;
dmazzoni329fd012015-10-22 20:05:35290 AXTreeData data_;
Dominic Mazzoni35f2a5252017-09-26 00:56:04291
292 // Map from an int attribute (if IsNodeIdIntAttribute is true) to
293 // a reverse mapping from target nodes to source nodes.
David Tsengef6b480d2018-02-19 12:48:42294 IntReverseRelationMap int_reverse_relations_;
Dominic Mazzoni35f2a5252017-09-26 00:56:04295 // Map from an int list attribute (if IsNodeIdIntListAttribute is true) to
296 // a reverse mapping from target nodes to source nodes.
David Tsengef6b480d2018-02-19 12:48:42297 IntListReverseRelationMap intlist_reverse_relations_;
Dominic Mazzoniaa774822018-08-29 20:33:58298 // Map from child tree ID to the set of node IDs that contain that attribute.
Dominic Mazzoni336bc0062018-09-23 16:46:43299 std::map<AXTreeID, std::set<int32_t>> child_tree_id_reverse_map_;
Dominic Mazzoni3d9b5b92018-04-18 21:36:38300
301 // Map from node ID to cached table info, if the given node is a table.
Dominic Mazzonid42e00a2018-06-27 23:14:23302 // Invalidated every time the tree is updated.
Takuto Ikutaadf31eb2019-01-05 00:32:48303 mutable std::unordered_map<int32_t, AXTableInfo*> table_info_map_;
Dominic Mazzonid42e00a2018-06-27 23:14:23304
305 // The next negative node ID to use for internal nodes.
306 int32_t next_negative_internal_node_id_ = -1;
307
308 // Whether we should create extra nodes that
309 // are only useful on macOS. Implemented using this flag to allow
310 // this code to be unit-tested on other platforms (for example, more
311 // code sanitizers run on Linux).
312 bool enable_extra_mac_nodes_ = false;
Akihiro Ota413ca722018-12-03 23:29:00313
314 // Contains pos_in_set and set_size data for an AXNode.
315 struct OrderedSetInfo {
316 int32_t pos_in_set;
317 int32_t set_size;
Alice Boxhallc8ffa6c2019-04-09 02:39:44318 int32_t lowest_hierarchical_level;
Akihiro Ota413ca722018-12-03 23:29:00319 OrderedSetInfo() : pos_in_set(0), set_size(0) {}
320 ~OrderedSetInfo() {}
321 };
322
Akihiro Otab6a8a4d2018-12-04 01:56:39323 // Populates items vector with all items within ordered_set.
Akihiro Ota886a96d62018-12-18 00:11:48324 // Will only add items whose roles match the role of the
325 // ordered_set.
Akihiro Ota413ca722018-12-03 23:29:00326 void PopulateOrderedSetItems(const AXNode* ordered_set,
327 const AXNode* local_parent,
Akihiro Ota886a96d62018-12-18 00:11:48328 std::vector<const AXNode*>& items,
Akihiro Otad659eded2019-02-20 21:44:32329 const AXNode& original_node) const;
Akihiro Ota886a96d62018-12-18 00:11:48330
Akihiro Ota413ca722018-12-03 23:29:00331 // Helper for GetPosInSet and GetSetSize. Computes the pos_in_set and set_size
332 // values of all items in ordered_set and caches those values.
Akihiro Ota886a96d62018-12-18 00:11:48333 void ComputeSetSizePosInSetAndCache(const AXNode& node,
334 const AXNode* ordered_set);
Akihiro Ota413ca722018-12-03 23:29:00335
Akihiro Otab6a8a4d2018-12-04 01:56:39336 // Map from node ID to OrderedSetInfo.
337 // Item-like and ordered-set-like objects will map to populated OrderedSetInfo
338 // objects.
339 // All other objects will map to default-constructed OrderedSetInfo objects.
Akihiro Ota413ca722018-12-03 23:29:00340 // Invalidated every time the tree is updated.
341 mutable std::unordered_map<int32_t, OrderedSetInfo> ordered_set_info_map_;
Chris Hall9b34c2c2018-12-04 01:45:56342
343 // AXTree owns pointers so copying is non-trivial.
344 DISALLOW_COPY_AND_ASSIGN(AXTree);
Akihiro Otae3e420e2019-04-17 19:57:40345
346 // Indicates if the tree is updating.
347 bool tree_update_in_progress_ = false;
Kurt Catti-Schmidtc8445a12019-08-07 18:52:58348
349 // Indicates if the tree represents a paginated document
350 bool has_pagination_support_ = false;
[email protected]4b02bbca2013-11-22 08:59:03351};
352
353} // namespace ui
354
355#endif // UI_ACCESSIBILITY_AX_TREE_H_