blob: 085bf7e1254fd7c5f888b6452f62c64ea9355ece [file] [log] [blame]
Avi Drissman3e1a26c2022-09-15 20:26:031// Copyright 2013 The Chromium Authors
[email protected]4b02bbca2013-11-22 08:59:032// 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
Lei Zhanga06144782020-03-13 09:28:4710#include <map>
Chris Hall34208182019-03-13 02:26:1811#include <memory>
[email protected]d4e273462013-12-04 04:37:5812#include <set>
Lei Zhanga06144782020-03-13 09:28:4713#include <string>
Bruce Dawson9d565ec2022-10-31 17:42:3414#include <unordered_map>
Lei Zhanga06144782020-03-13 09:28:4715#include <vector>
[email protected]4b02bbca2013-11-22 08:59:0316
Aaron Leventhalbc649ff2022-08-11 18:53:2317#include "base/debug/crash_logging.h"
Keishi Hattori0e45c022021-11-27 09:25:5218#include "base/memory/raw_ptr.h"
Jacques Newman5846e622021-01-15 02:15:5919#include "base/metrics/histogram_functions.h"
Dominic Mazzoni8549eb682018-12-11 23:48:3220#include "base/observer_list.h"
Nektarios Paisios77c422a2021-10-19 10:37:0021#include "third_party/abseil-cpp/absl/types/optional.h"
James Cook36cab7c2019-10-29 23:26:4022#include "ui/accessibility/ax_enums.mojom-forward.h"
[email protected]4b02bbca2013-11-22 08:59:0323#include "ui/accessibility/ax_export.h"
dmazzoni329fd012015-10-22 20:05:3524#include "ui/accessibility/ax_tree_data.h"
[email protected]4b02bbca2013-11-22 08:59:0325#include "ui/accessibility/ax_tree_update.h"
26
27namespace ui {
28
David Tseng41f13cbd2021-11-06 18:39:0329struct AXEvent;
Alexander Surkov4ab64cf2022-09-01 20:07:4630class AXLanguageDetectionManager;
31class AXNode;
32struct AXNodeData;
Dominic Mazzoni3d9b5b92018-04-18 21:36:3833class AXTableInfo;
Dominic Mazzoni8549eb682018-12-11 23:48:3234class AXTreeObserver;
[email protected]e736e81b2014-02-24 07:15:5835struct AXTreeUpdateState;
Alexander Surkov253235e2022-08-23 01:52:1236class AXSelection;
[email protected]e736e81b2014-02-24 07:15:5837
Jacques Newman5846e622021-01-15 02:15:5938// These values are persisted to logs. Entries should not be renumbered and
39// numeric values should never be reused.
40enum class AXTreeUnserializeError {
41 // Tree has no root.
42 kNoRoot = 0,
43 // Node will not be in the tree and is not the new root.
44 kNotInTree = 1,
45 // Node is already pending for creation, cannot be the new root
46 kCreationPending = 2,
47 // Node has duplicate child.
48 kDuplicateChild = 3,
49 // Node is already pending for creation, cannot be a new child.
50 kCreationPendingForChild = 4,
51 // Node is not marked for destruction, would be reparented.
52 kReparent = 5,
53 // Nodes are left pending by the update.
54 kPendingNodes = 6,
55 // Changes left pending by the update;
56 kPendingChanges = 7,
57 // This must always be the last enum. It's okay for its value to
58 // increase, but none of the other enum values may change.
59 kMaxValue = kPendingChanges
60};
61
62#define ACCESSIBILITY_TREE_UNSERIALIZE_ERROR_HISTOGRAM(enum_value) \
63 base::UmaHistogramEnumeration( \
64 "Accessibility.Reliability.Tree.UnserializeError", enum_value)
65
[email protected]4b02bbca2013-11-22 08:59:0366// AXTree is a live, managed tree of AXNode objects that can receive
67// updates from another AXTreeSource via AXTreeUpdates, and it can be
68// used as a source for sending updates to another client tree.
69// It's designed to be subclassed to implement support for native
70// accessibility APIs on a specific platform.
Alexander Surkov4ab64cf2022-09-01 20:07:4671class AX_EXPORT AXTree {
[email protected]4b02bbca2013-11-22 08:59:0372 public:
Lei Zhanga06144782020-03-13 09:28:4773 using IntReverseRelationMap =
Nektarios Paisios527d33fb52021-02-23 19:23:2874 std::map<ax::mojom::IntAttribute, std::map<AXNodeID, std::set<AXNodeID>>>;
Lei Zhanga06144782020-03-13 09:28:4775 using IntListReverseRelationMap =
76 std::map<ax::mojom::IntListAttribute,
Nektarios Paisios527d33fb52021-02-23 19:23:2877 std::map<AXNodeID, std::set<AXNodeID>>>;
David Tsengef6b480d2018-02-19 12:48:4278
Nektarios Paisios77c422a2021-10-19 10:37:0079 // If called, the focused node in this tree will never be ignored, even if it
80 // has the ignored state set. For now, this boolean will be set to false for
81 // all trees except in test scenarios, in order to thoroughly test the
82 // relevant code without causing any potential regressions. Ultimately, we
83 // want to expose all focused nodes so that a user of an assistive technology
84 // will be able to interact with the application / website, even if there is
85 // an authoring error, e.g. the aria-hidden attribute has been applied to the
86 // focused element.
87 // TODO(nektar): Removed once the feature has been fully tested.
88 static void SetFocusedNodeShouldNeverBeIgnored();
89
90 // Determines the ignored state of a node, given information about the node
91 // and the tree.
92 static bool ComputeNodeIsIgnored(const AXTreeData* optional_tree_data,
93 const AXNodeData& node_data);
94
95 // Determines whether a node has flipped its ignored state, given information
96 // about the previous and current state of the node / tree.
97 static bool ComputeNodeIsIgnoredChanged(
98 const AXTreeData* optional_old_tree_data,
99 const AXNodeData& old_node_data,
100 const AXTreeData* optional_new_tree_data,
101 const AXNodeData& new_node_data);
102
[email protected]4b02bbca2013-11-22 08:59:03103 AXTree();
dmazzoni329fd012015-10-22 20:05:35104 explicit AXTree(const AXTreeUpdate& initial_state);
[email protected]4b02bbca2013-11-22 08:59:03105 virtual ~AXTree();
106
Lei Zhanga06144782020-03-13 09:28:47107 // AXTree owns pointers so copying is non-trivial.
108 AXTree(const AXTree&) = delete;
109 AXTree& operator=(const AXTree&) = delete;
110
Dominic Mazzoni8549eb682018-12-11 23:48:32111 void AddObserver(AXTreeObserver* observer);
112 bool HasObserver(AXTreeObserver* observer);
Nektarios Paisiosdb7b5ee2020-02-18 21:28:11113 void RemoveObserver(AXTreeObserver* observer);
Dominic Mazzoni8549eb682018-12-11 23:48:32114
115 base::ObserverList<AXTreeObserver>& observers() { return observers_; }
[email protected]e736e81b2014-02-24 07:15:58116
tfarina6b1c1e082015-02-20 23:47:07117 AXNode* root() const { return root_; }
118
Alexander Surkov4ab64cf2022-09-01 20:07:46119 const AXTreeData& data() const;
dmazzoni329fd012015-10-22 20:05:35120
Nektarios Paisiosdb7b5ee2020-02-18 21:28:11121 // Destroys the tree and notifies all observers.
122 void Destroy();
123
Adam Ettenberger86af2532019-09-17 20:04:46124 // Returns the globally unique ID of this accessibility tree.
Alexander Surkov4ab64cf2022-09-01 20:07:46125 const AXTreeID& GetAXTreeID() const;
Adam Ettenberger86af2532019-09-17 20:04:46126
Alexander Surkov4ab64cf2022-09-01 20:07:46127 // Given a node in this accessibility tree that corresponds to a table
128 // or grid, return an object containing information about the
129 // table structure. This object is computed lazily on-demand and
130 // cached until the next time the tree is updated. Clients should
131 // not retain this pointer, they should just request it every time
132 // it's needed.
133 //
134 // Returns nullptr if the node is not a valid table.
135 AXTableInfo* GetTableInfo(const AXNode* table_node) const;
136
tfarina6b1c1e082015-02-20 23:47:07137 // Returns the AXNode with the given |id| if it is part of this AXTree.
Alexander Surkov4ab64cf2022-09-01 20:07:46138 AXNode* GetFromId(AXNodeID id) const;
[email protected]4b02bbca2013-11-22 08:59:03139
[email protected]d4e273462013-12-04 04:37:58140 // Returns true on success. If it returns false, it's a fatal error
141 // and this tree should be destroyed, and the source of the tree update
142 // should not be trusted any longer.
dmazzoni329fd012015-10-22 20:05:35143 virtual bool Unserialize(const AXTreeUpdate& update);
144
Nektarios Paisios26ac2b42021-06-02 18:24:19145 // Used by tests to update the tree data without changing any of the nodes in
146 // the tree, notifying all tree observers in the process.
147 virtual void UpdateDataForTesting(const AXTreeData& data);
[email protected]4b02bbca2013-11-22 08:59:03148
Dominic Mazzoni2410fc62017-06-09 22:19:18149 // Convert any rectangle from the local coordinate space of one node in
150 // the tree, to bounds in the coordinate space of the tree.
Katie Dektar2c6052d2017-09-27 00:32:32151 // If set, updates |offscreen| boolean to be true if the node is offscreen
152 // relative to its rootWebArea. Callers should initialize |offscreen|
153 // to false: this method may get called multiple times in a row and
154 // |offscreen| will be propagated.
Katie Dektardb71ad942017-11-29 20:07:48155 // If |clip_bounds| is true, result bounds will be clipped.
Dominic Mazzoni2410fc62017-06-09 22:19:18156 gfx::RectF RelativeToTreeBounds(const AXNode* node,
Katie Dektar2c6052d2017-09-27 00:32:32157 gfx::RectF node_bounds,
Katie Dektardb71ad942017-11-29 20:07:48158 bool* offscreen = nullptr,
David Tseng7bf30c3c2022-07-22 15:13:37159 bool clip_bounds = true,
160 bool skip_container_offset = false) const;
Dominic Mazzoni2410fc62017-06-09 22:19:18161
162 // Get the bounds of a node in the coordinate space of the tree.
Katie Dektar2c6052d2017-09-27 00:32:32163 // If set, updates |offscreen| boolean to be true if the node is offscreen
164 // relative to its rootWebArea. Callers should initialize |offscreen|
165 // to false: this method may get called multiple times in a row and
166 // |offscreen| will be propagated.
Katie Dektardb71ad942017-11-29 20:07:48167 // If |clip_bounds| is true, result bounds will be clipped.
168 gfx::RectF GetTreeBounds(const AXNode* node,
169 bool* offscreen = nullptr,
170 bool clip_bounds = true) const;
Dominic Mazzoni2410fc62017-06-09 22:19:18171
Dominic Mazzoni35f2a5252017-09-26 00:56:04172 // Given a node ID attribute (one where IsNodeIdIntAttribute is true),
173 // and a destination node ID, return a set of all source node IDs that
174 // have that relationship attribute between them and the destination.
Nektarios Paisios527d33fb52021-02-23 19:23:28175 std::set<AXNodeID> GetReverseRelations(ax::mojom::IntAttribute attr,
176 AXNodeID dst_id) const;
Dominic Mazzoni35f2a5252017-09-26 00:56:04177
178 // Given a node ID list attribute (one where
179 // IsNodeIdIntListAttribute is true), and a destination node ID,
180 // return a set of all source node IDs that have that relationship
181 // attribute between them and the destination.
Nektarios Paisios527d33fb52021-02-23 19:23:28182 std::set<AXNodeID> GetReverseRelations(ax::mojom::IntListAttribute attr,
183 AXNodeID dst_id) const;
Dominic Mazzoni35f2a5252017-09-26 00:56:04184
Dominic Mazzoniaa774822018-08-29 20:33:58185 // Given a child tree ID, return the node IDs of all nodes in the tree who
186 // have a kChildTreeId int attribute with that value.
Nektarios Paisios50e579f12022-05-19 19:22:39187 //
188 // TODO(accessibility): There should really be only one host node per child
189 // tree, so the return value should not be a set but a single node ID or
190 // `kInvalidAXNodeID`.
Nektarios Paisios527d33fb52021-02-23 19:23:28191 std::set<AXNodeID> GetNodeIdsForChildTreeId(AXTreeID child_tree_id) const;
Dominic Mazzoniaa774822018-08-29 20:33:58192
Dominic Mazzoni748888c2018-11-01 22:34:59193 // Get all of the child tree IDs referenced by any node in this tree.
194 const std::set<AXTreeID> GetAllChildTreeIds() const;
195
David Tsengef6b480d2018-02-19 12:48:42196 // Map from a relation attribute to a map from a target id to source ids.
197 const IntReverseRelationMap& int_reverse_relations() {
198 return int_reverse_relations_;
199 }
200 const IntListReverseRelationMap& intlist_reverse_relations() {
201 return intlist_reverse_relations_;
202 }
Dominic Mazzoni3d9b5b92018-04-18 21:36:38203
[email protected]5eec2f52014-01-06 22:30:54204 // Return a multi-line indented string representation, for logging.
Aaron Leventhal763d46a2022-12-21 19:16:22205 std::string ToString(bool verbose = true) const;
[email protected]5eec2f52014-01-06 22:30:54206
[email protected]d4e273462013-12-04 04:37:58207 // A string describing the error from an unsuccessful Unserialize,
208 // for testing and debugging.
tfarinad0bfb4b62015-02-18 17:20:32209 const std::string& error() const { return error_; }
[email protected]d4e273462013-12-04 04:37:58210
Aaron Leventhal0ebbf7c2022-08-24 14:29:41211 void DisallowFailFastForFuzzing() { disallow_fail_fast_ = true; }
212
dmazzoniee2eaca2015-03-18 18:13:07213 int size() { return static_cast<int>(id_map_.size()); }
214
Dominic Mazzonid42e00a2018-06-27 23:14:23215 // Return a negative number that's suitable to use for a node ID for
216 // internal nodes created automatically by an AXTree, so as not to
217 // conflict with positive-numbered node IDs from tree sources.
Nektarios Paisios527d33fb52021-02-23 19:23:28218 AXNodeID GetNextNegativeInternalNodeId();
Dominic Mazzonid42e00a2018-06-27 23:14:23219
Akihiro Otaf42a7d02020-06-12 19:07:56220 // Returns the PosInSet of |node|. Looks in node_set_size_pos_in_set_info_map_
221 // for cached value. Calls |ComputeSetSizePosInSetAndCache|if no value is
222 // present in the cache.
Alexander Surkov4ab64cf2022-09-01 20:07:46223 absl::optional<int> GetPosInSet(const AXNode& node);
224
Akihiro Otaf42a7d02020-06-12 19:07:56225 // Returns the SetSize of |node|. Looks in node_set_size_pos_in_set_info_map_
226 // for cached value. Calls |ComputeSetSizePosInSetAndCache|if no value is
227 // present in the cache.
Alexander Surkov4ab64cf2022-09-01 20:07:46228 absl::optional<int> GetSetSize(const AXNode& node);
Akihiro Ota413ca722018-12-03 23:29:00229
Nektarios Paisiosd3e82c6d2022-08-12 19:06:55230 // Returns the part of the current selection that falls within this
231 // accessibility tree, if any.
Alexander Surkov4ab64cf2022-09-01 20:07:46232 AXSelection GetSelection() const;
Nektarios Paisiosd3e82c6d2022-08-12 19:06:55233
234 // Returns the part of the current selection that falls within this
235 // accessibility tree, if any, adjusting its endpoints to be within unignored
236 // nodes. (An "ignored" node is a node that is not exposed to platform APIs:
237 // See `AXNode::IsIgnored`.)
Alexander Surkov4ab64cf2022-09-01 20:07:46238 AXSelection GetUnignoredSelection() const;
Jacques Newman339afc62019-08-14 00:49:22239
Alexander Surkov4ab64cf2022-09-01 20:07:46240 bool GetTreeUpdateInProgressState() const;
Akihiro Otae3e420e2019-04-17 19:57:40241
Kurt Catti-Schmidtc8445a12019-08-07 18:52:58242 // Returns true if the tree represents a paginated document
Alexander Surkov4ab64cf2022-09-01 20:07:46243 bool HasPaginationSupport() const;
Kurt Catti-Schmidtc8445a12019-08-07 18:52:58244
Chris Hall697d99b2019-07-09 02:36:11245 // Language detection manager, entry point to language detection features.
246 // TODO(chrishall): Should this be stored by pointer or value?
247 // When should we initialize this?
248 std::unique_ptr<AXLanguageDetectionManager> language_detection_manager;
Chris Hall34208182019-03-13 02:26:18249
David Tseng41f13cbd2021-11-06 18:39:03250 // Event metadata while applying a tree update during unserialization.
251 AXEvent* event_data() const { return event_data_.get(); }
David Tsengb0d43662020-05-20 20:47:28252
Benjamin Beaudry710df4d2023-02-08 23:47:56253 // Notify the delegate that the tree manager for |previous_tree_id| will be
254 // removed from the AXTreeManagerMap. Because we sometimes remove the tree
255 // manager after the tree's id has been modified, we need to pass the (old)
256 // tree id associated with the manager we are removing even though it is the
257 // same tree.
258 void NotifyTreeManagerWillBeRemoved(AXTreeID previous_tree_id);
Benjamin Beaudrye8f23a22020-12-17 20:08:02259
[email protected]e736e81b2014-02-24 07:15:58260 private:
Nektarios Paisios8eb31fc2022-02-18 16:37:58261 friend class ScopedTreeUpdateInProgressStateSetter;
Dominic Mazzoniecfb4fd2018-10-23 07:34:20262 friend class AXTableInfoTest;
263
Nektarios Paisios77c422a2021-10-19 10:37:00264 // Indicates if the node with the focus should never be ignored, (see
265 // `SetFocusedNodeShouldNeverBeIgnored` above).
266 static bool is_focused_node_always_unignored_;
267
Aaron Leventhala674d632020-09-30 07:05:36268 // Accumulate errors as there can be more than one before Chrome is crashed
269 // via AccessibilityFatalError();
Aaron Leventhale12416e2022-12-23 16:08:17270 // In an AX_FAIL_FAST_BUILD or if |is_fatal|, will assert/crash immediately.
Aaron Leventhalbc649ff2022-08-11 18:53:23271 void RecordError(const AXTreeUpdateState& update_state,
Aaron Leventhale12416e2022-12-23 16:08:17272 std::string new_error,
273 bool is_fatal = false);
Aaron Leventhala674d632020-09-30 07:05:36274
dtseng5a7b3d92016-09-08 06:35:58275 AXNode* CreateNode(AXNode* parent,
Dominic Mazzoni9ccdedb22021-01-30 17:59:42276 AXNodeID id,
Peter Kasting94a07a12019-05-22 19:26:28277 size_t index_in_parent,
dtseng5a7b3d92016-09-08 06:35:58278 AXTreeUpdateState* update_state);
[email protected]4b02bbca2013-11-22 08:59:03279
Adam Ettenbergerd9d8d58a2019-08-06 16:54:08280 // Accumulates the work that will be required to update the AXTree.
281 // This allows us to notify observers of structure changes when the
282 // tree is still in a stable and unchanged state.
283 bool ComputePendingChanges(const AXTreeUpdate& update,
Lei Zhanga06144782020-03-13 09:28:47284 AXTreeUpdateState* update_state);
Adam Ettenbergerd9d8d58a2019-08-06 16:54:08285
286 // Populates |update_state| with information about actions that will
287 // be performed on the tree during the update, such as adding or
288 // removing nodes in the tree. Returns true on success.
289 // Nothing within this call should modify tree structure or node data.
290 bool ComputePendingChangesToNode(const AXNodeData& new_data,
291 bool is_new_root,
292 AXTreeUpdateState* update_state);
293
[email protected]4b02bbca2013-11-22 08:59:03294 // This is called from within Unserialize(), it returns true on success.
dmazzoni67b4db22016-04-23 00:40:04295 bool UpdateNode(const AXNodeData& src,
296 bool is_new_root,
297 AXTreeUpdateState* update_state);
[email protected]4b02bbca2013-11-22 08:59:03298
Adam Ettenbergerd9d8d58a2019-08-06 16:54:08299 // Notify the delegate that the subtree rooted at |node| will be
300 // destroyed or reparented.
301 void NotifySubtreeWillBeReparentedOrDeleted(
302 AXNode* node,
303 const AXTreeUpdateState* update_state);
304
305 // Notify the delegate that |node| will be destroyed or reparented.
306 void NotifyNodeWillBeReparentedOrDeleted(
307 AXNode* node,
308 const AXTreeUpdateState* update_state);
309
310 // Notify the delegate that |node| and all of its descendants will be
Victor Feie3cce4c2019-11-14 18:35:41311 // destroyed. This function is called during AXTree teardown.
312 void RecursivelyNotifyNodeDeletedForTreeTeardown(AXNode* node);
313
314 // Notify the delegate that the node marked by |node_id| has been deleted.
315 // We are passing the node id instead of ax node is because by the time this
316 // function is called, the ax node in the tree will already have been
Adam Ettenbergerd9d8d58a2019-08-06 16:54:08317 // destroyed.
Dominic Mazzoni9ccdedb22021-01-30 17:59:42318 void NotifyNodeHasBeenDeleted(AXNodeID node_id);
Adam Ettenbergerd9d8d58a2019-08-06 16:54:08319
320 // Notify the delegate that |node| has been created or reparented.
321 void NotifyNodeHasBeenReparentedOrCreated(
322 AXNode* node,
323 const AXTreeUpdateState* update_state);
324
Nektarios Paisios77c422a2021-10-19 10:37:00325 // Notify the delegate that `node` will change its data attributes, including
326 // its ignored state.
327 void NotifyNodeAttributesWillChange(AXNode* node,
Aaron Leventhal06f376bf2022-12-01 02:15:07328 AXTreeUpdateState& update_state,
Nektarios Paisios77c422a2021-10-19 10:37:00329 const AXTreeData* optional_old_tree_data,
330 const AXNodeData& old_data,
331 const AXTreeData* new_tree_data,
332 const AXNodeData& new_data);
Adam Ettenberger05afcec2019-08-06 17:11:29333
Aaron Leventhal06f376bf2022-12-01 02:15:07334 // Notify the delegate that `node` will change its its ignored state.
335 void NotifyNodeIgnoredStateWillChange(
336 AXNode* node,
337 const AXTreeData* optional_old_tree_data,
338 const AXNodeData& old_data,
339 const AXTreeData* new_tree_data,
340 const AXNodeData& new_data);
341
Nektarios Paisios77c422a2021-10-19 10:37:00342 // Notify the delegate that `node` has changed its data attributes, including
343 // its ignored state.
344 void NotifyNodeAttributesHaveBeenChanged(
345 AXNode* node,
Aaron Leventhal06f376bf2022-12-01 02:15:07346 AXTreeUpdateState& update_state,
Nektarios Paisios77c422a2021-10-19 10:37:00347 const AXTreeData* optional_old_tree_data,
348 const AXNodeData& old_data,
349 const AXTreeData* new_tree_data,
350 const AXNodeData& new_data);
dmazzoni3ab5385c2017-03-13 18:07:03351
Dominic Mazzoni35f2a5252017-09-26 00:56:04352 void UpdateReverseRelations(AXNode* node, const AXNodeData& new_data);
353
Nektarios Paisios8eb31fc2022-02-18 16:37:58354 // Sets a flag indicating whether the tree is currently being updated or not.
355 // If the tree is being updated, then its internal pointers might be invalid
356 // and the tree should not be traversed.
357 void SetTreeUpdateInProgressState(bool set_tree_update_value);
358
Adam Ettenbergerd9d8d58a2019-08-06 16:54:08359 // Returns true if all pending changes in the |update_state| have been
360 // handled. If this returns false, the |error_| message will be populated.
361 // It's a fatal error to have pending changes after exhausting
362 // the AXTreeUpdate.
363 bool ValidatePendingChangesComplete(const AXTreeUpdateState& update_state);
[email protected]4b02bbca2013-11-22 08:59:03364
Adam Ettenbergerd9d8d58a2019-08-06 16:54:08365 // Modifies |update_state| so that it knows what subtree and nodes are
366 // going to be destroyed for the subtree rooted at |node|.
Dominic Mazzoni9ccdedb22021-01-30 17:59:42367 void MarkSubtreeForDestruction(AXNodeID node_id,
Adam Ettenbergerd9d8d58a2019-08-06 16:54:08368 AXTreeUpdateState* update_state);
369
370 // Modifies |update_state| so that it knows what nodes are
371 // going to be destroyed for the subtree rooted at |node|.
Dominic Mazzoni9ccdedb22021-01-30 17:59:42372 void MarkNodesForDestructionRecursive(AXNodeID node_id,
Adam Ettenbergerd9d8d58a2019-08-06 16:54:08373 AXTreeUpdateState* update_state);
374
375 // Validates that destroying the subtree rooted at |node| has required
376 // information in |update_state|, then calls DestroyNodeAndSubtree on it.
dmazzonie3b7faf2015-06-01 17:56:36377 void DestroySubtree(AXNode* node, AXTreeUpdateState* update_state);
dmazzonia4b48912015-01-24 00:08:56378
[email protected]4b02bbca2013-11-22 08:59:03379 // Call Destroy() on |node|, and delete it from the id map, and then
380 // call recursively on all nodes in its subtree.
dmazzonie3b7faf2015-06-01 17:56:36381 void DestroyNodeAndSubtree(AXNode* node, AXTreeUpdateState* update_state);
[email protected]4b02bbca2013-11-22 08:59:03382
383 // Iterate over the children of |node| and for each child, destroy the
Adam Ettenbergerd9d8d58a2019-08-06 16:54:08384 // child and its subtree if its id is not in |new_child_ids|.
385 void DeleteOldChildren(AXNode* node,
Nektarios Paisios527d33fb52021-02-23 19:23:28386 const std::vector<AXNodeID>& new_child_ids,
dmazzonie3b7faf2015-06-01 17:56:36387 AXTreeUpdateState* update_state);
[email protected]4b02bbca2013-11-22 08:59:03388
389 // Iterate over |new_child_ids| and populate |new_children| with
390 // pointers to child nodes, reusing existing nodes already in the tree
391 // if they exist, and creating otherwise. Reparenting is disallowed, so
392 // if the id already exists as the child of another node, that's an
[email protected]e736e81b2014-02-24 07:15:58393 // error. Returns true on success, false on fatal error.
[email protected]4b02bbca2013-11-22 08:59:03394 bool CreateNewChildVector(AXNode* node,
Nektarios Paisios527d33fb52021-02-23 19:23:28395 const std::vector<AXNodeID>& new_child_ids,
[email protected]d4e273462013-12-04 04:37:58396 std::vector<AXNode*>* new_children,
[email protected]e736e81b2014-02-24 07:15:58397 AXTreeUpdateState* update_state);
[email protected]4b02bbca2013-11-22 08:59:03398
Nektarios Paisios77c422a2021-10-19 10:37:00399 // Returns the lowest unignored ancestor of the node with the given ID. If the
400 // node is not ignored, it returns the node.
401 AXNode* GetUnignoredAncestorFromId(AXNodeID node_id) const;
402
Dominic Mazzonia1bb0d122019-09-26 20:19:59403 // Internal implementation of RelativeToTreeBounds. It calls itself
404 // recursively but ensures that it can only do so exactly once!
405 gfx::RectF RelativeToTreeBoundsInternal(const AXNode* node,
406 gfx::RectF node_bounds,
407 bool* offscreen,
408 bool clip_bounds,
David Tseng7bf30c3c2022-07-22 15:13:37409 bool skip_container_offset,
Dominic Mazzonia1bb0d122019-09-26 20:19:59410 bool allow_recursion) const;
411
Dominic Mazzoni8549eb682018-12-11 23:48:32412 base::ObserverList<AXTreeObserver> observers_;
Ali Hijazi2fe3e402022-08-05 16:09:31413 raw_ptr<AXNode> root_ = nullptr;
Bruce Dawson9d565ec2022-10-31 17:42:34414 std::unordered_map<AXNodeID, std::unique_ptr<AXNode>> id_map_;
[email protected]d4e273462013-12-04 04:37:58415 std::string error_;
Aaron Leventhal0ebbf7c2022-08-24 14:29:41416 bool disallow_fail_fast_ = false;
dmazzoni329fd012015-10-22 20:05:35417 AXTreeData data_;
Dominic Mazzoni35f2a5252017-09-26 00:56:04418
419 // Map from an int attribute (if IsNodeIdIntAttribute is true) to
420 // a reverse mapping from target nodes to source nodes.
David Tsengef6b480d2018-02-19 12:48:42421 IntReverseRelationMap int_reverse_relations_;
Dominic Mazzoni35f2a5252017-09-26 00:56:04422 // Map from an int list attribute (if IsNodeIdIntListAttribute is true) to
423 // a reverse mapping from target nodes to source nodes.
David Tsengef6b480d2018-02-19 12:48:42424 IntListReverseRelationMap intlist_reverse_relations_;
Dominic Mazzoniaa774822018-08-29 20:33:58425 // Map from child tree ID to the set of node IDs that contain that attribute.
Nektarios Paisios527d33fb52021-02-23 19:23:28426 std::map<AXTreeID, std::set<AXNodeID>> child_tree_id_reverse_map_;
Dominic Mazzoni3d9b5b92018-04-18 21:36:38427
428 // Map from node ID to cached table info, if the given node is a table.
Dominic Mazzonid42e00a2018-06-27 23:14:23429 // Invalidated every time the tree is updated.
Bruce Dawson9d565ec2022-10-31 17:42:34430 mutable std::unordered_map<AXNodeID, std::unique_ptr<AXTableInfo>>
Aaron Leventhal80797182020-02-25 18:50:31431 table_info_map_;
Dominic Mazzonid42e00a2018-06-27 23:14:23432
433 // The next negative node ID to use for internal nodes.
Nektarios Paisios527d33fb52021-02-23 19:23:28434 AXNodeID next_negative_internal_node_id_ = -1;
Dominic Mazzonid42e00a2018-06-27 23:14:23435
Akihiro Ota413ca722018-12-03 23:29:00436 // Contains pos_in_set and set_size data for an AXNode.
Stephan Hartmannaeef6882020-04-20 18:21:43437 struct NodeSetSizePosInSetInfo {
438 NodeSetSizePosInSetInfo();
439 ~NodeSetSizePosInSetInfo();
440
Anton Bikineeveed0b26b2021-05-16 03:16:48441 absl::optional<int> pos_in_set;
442 absl::optional<int> set_size;
443 absl::optional<int> lowest_hierarchical_level;
Stephan Hartmannaeef6882020-04-20 18:21:43444 };
Akihiro Ota413ca722018-12-03 23:29:00445
Victor Fei5eea952e2020-02-28 01:43:09446 // Represents the content of an ordered set which includes the ordered set
447 // items and the ordered set container if it exists.
448 struct OrderedSetContent;
449
450 // Maps a particular hierarchical level to a list of OrderedSetContents.
451 // Represents all ordered set items/container on a particular hierarchical
452 // level.
453 struct OrderedSetItemsMap;
454
455 // Populates |items_map_to_be_populated| with all items associated with
Victor Feid95130c2020-02-03 21:42:54456 // |original_node| and within |ordered_set|. Only items whose roles match the
457 // role of the |ordered_set| will be added.
Victor Fei5eea952e2020-02-28 01:43:09458 void PopulateOrderedSetItemsMap(
Victor Feid95130c2020-02-03 21:42:54459 const AXNode& original_node,
460 const AXNode* ordered_set,
Lei Zhanga06144782020-03-13 09:28:47461 OrderedSetItemsMap* items_map_to_be_populated) const;
Victor Feid95130c2020-02-03 21:42:54462
Victor Fei5eea952e2020-02-28 01:43:09463 // Helper function for recursively populating ordered sets items map with
Victor Feid95130c2020-02-03 21:42:54464 // all items associated with |original_node| and |ordered_set|. |local_parent|
465 // tracks the recursively passed in child nodes of |ordered_set|.
Victor Fei5eea952e2020-02-28 01:43:09466 void RecursivelyPopulateOrderedSetItemsMap(
Victor Feid95130c2020-02-03 21:42:54467 const AXNode& original_node,
468 const AXNode* ordered_set,
469 const AXNode* local_parent,
Anton Bikineeveed0b26b2021-05-16 03:16:48470 absl::optional<int> ordered_set_min_level,
471 absl::optional<int> prev_level,
Lei Zhanga06144782020-03-13 09:28:47472 OrderedSetItemsMap* items_map_to_be_populated) const;
Akihiro Ota886a96d62018-12-18 00:11:48473
Victor Fei5eea952e2020-02-28 01:43:09474 // Computes the pos_in_set and set_size values of all items in ordered_set and
475 // caches those values. Called by GetPosInSet and GetSetSize.
Akihiro Ota886a96d62018-12-18 00:11:48476 void ComputeSetSizePosInSetAndCache(const AXNode& node,
477 const AXNode* ordered_set);
Akihiro Ota413ca722018-12-03 23:29:00478
Victor Fei5eea952e2020-02-28 01:43:09479 // Helper for ComputeSetSizePosInSetAndCache. Computes and caches the
480 // pos_in_set and set_size values for a given OrderedSetContent.
481 void ComputeSetSizePosInSetAndCacheHelper(
482 const OrderedSetContent& ordered_set_content);
483
Akihiro Otab6a8a4d2018-12-04 01:56:39484 // Map from node ID to OrderedSetInfo.
485 // Item-like and ordered-set-like objects will map to populated OrderedSetInfo
486 // objects.
487 // All other objects will map to default-constructed OrderedSetInfo objects.
Akihiro Ota413ca722018-12-03 23:29:00488 // Invalidated every time the tree is updated.
Bruce Dawson9d565ec2022-10-31 17:42:34489 mutable std::unordered_map<AXNodeID, NodeSetSizePosInSetInfo>
Victor Fei5eea952e2020-02-28 01:43:09490 node_set_size_pos_in_set_info_map_;
Chris Hall9b34c2c2018-12-04 01:45:56491
Akihiro Otae3e420e2019-04-17 19:57:40492 // Indicates if the tree is updating.
493 bool tree_update_in_progress_ = false;
Kurt Catti-Schmidtc8445a12019-08-07 18:52:58494
495 // Indicates if the tree represents a paginated document
496 bool has_pagination_support_ = false;
David Tsengb0d43662020-05-20 20:47:28497
David Tseng41f13cbd2021-11-06 18:39:03498 std::unique_ptr<AXEvent> event_data_;
[email protected]4b02bbca2013-11-22 08:59:03499};
500
Nektarios Paisios8eb31fc2022-02-18 16:37:58501// Sets the flag that indicates whether the accessibility tree is currently
502// being updated, and ensures that it is reset to its previous value when the
503// instance is destructed. An accessibility tree that is being updated is
504// unstable and should not be traversed.
505class AX_EXPORT ScopedTreeUpdateInProgressStateSetter {
506 public:
507 explicit ScopedTreeUpdateInProgressStateSetter(AXTree& tree)
508 : tree_(&tree),
509 last_tree_update_in_progress_(tree.GetTreeUpdateInProgressState()) {
510 tree_->SetTreeUpdateInProgressState(true);
511 }
512
513 ~ScopedTreeUpdateInProgressStateSetter() {
514 tree_->SetTreeUpdateInProgressState(last_tree_update_in_progress_);
515 }
516
517 ScopedTreeUpdateInProgressStateSetter(
518 const ScopedTreeUpdateInProgressStateSetter&) = delete;
519 ScopedTreeUpdateInProgressStateSetter& operator=(
520 const ScopedTreeUpdateInProgressStateSetter&) = delete;
521
522 private:
Keishi Hattorie175ac52022-06-07 06:24:57523 const raw_ptr<AXTree> tree_;
Nektarios Paisios8eb31fc2022-02-18 16:37:58524 bool last_tree_update_in_progress_;
525};
526
[email protected]4b02bbca2013-11-22 08:59:03527} // namespace ui
528
529#endif // UI_ACCESSIBILITY_AX_TREE_H_