blob: 75f26e6f6d0d21a97d2011b12e04d9d385f87c67 [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>
Arthur Sonzogni3eb9fd512024-02-09 12:20:4312#include <optional>
[email protected]d4e273462013-12-04 04:37:5813#include <set>
Lei Zhanga06144782020-03-13 09:28:4714#include <string>
Bruce Dawson9d565ec2022-10-31 17:42:3415#include <unordered_map>
Lei Zhanga06144782020-03-13 09:28:4716#include <vector>
[email protected]4b02bbca2013-11-22 08:59:0317
Aaron Leventhalbc649ff2022-08-11 18:53:2318#include "base/debug/crash_logging.h"
Keishi Hattori0e45c022021-11-27 09:25:5219#include "base/memory/raw_ptr.h"
Dominic Mazzoni8549eb682018-12-11 23:48:3220#include "base/observer_list.h"
Aaron Leventhal25ccd512024-12-04 00:57:4821#include "ui/accessibility/ax_common.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.
Mark Schillaci99be8c92024-08-16 21:24:3340//
41// LINT.IfChange(AXTreeUnserializeError)
Jacques Newman5846e622021-01-15 02:15:5942enum class AXTreeUnserializeError {
43 // Tree has no root.
44 kNoRoot = 0,
45 // Node will not be in the tree and is not the new root.
46 kNotInTree = 1,
47 // Node is already pending for creation, cannot be the new root
48 kCreationPending = 2,
49 // Node has duplicate child.
50 kDuplicateChild = 3,
51 // Node is already pending for creation, cannot be a new child.
52 kCreationPendingForChild = 4,
53 // Node is not marked for destruction, would be reparented.
54 kReparent = 5,
55 // Nodes are left pending by the update.
56 kPendingNodes = 6,
57 // Changes left pending by the update;
58 kPendingChanges = 7,
59 // This must always be the last enum. It's okay for its value to
60 // increase, but none of the other enum values may change.
61 kMaxValue = kPendingChanges
62};
Mark Schillaci99be8c92024-08-16 21:24:3363// LINT.ThenChange(/tools/metrics/histograms/metadata/accessibility/enums.xml:AccessibilityTreeUnserializeError)
Jacques Newman5846e622021-01-15 02:15:5964
Evelynn Kaplan6cd535ce2025-03-20 23:58:1865#if BUILDFLAG(IS_LINUX)
66// To support AriaNotify on older versions of ATK, we need to use the ATK
67// signal "Text::text-insert". This signal requires a node that is a
68// text type, and it needs to have aria-live properties set in order for
69// Orca to make announcements. We create 2 extra "dummy" nodes that can be
70// used for firing these signals when there is an AriaNotify event. One node
71// will have `aria-live: assertive` and the other will have `aria-live:
72// polite`.
73class ExtraAnnouncementNodes {
74 public:
75 explicit ExtraAnnouncementNodes(AXNode* root);
76 ~ExtraAnnouncementNodes();
77
78 AXNode& AssertiveNode() const { return *assertive_node_; }
79 AXNode& PoliteNode() const { return *polite_node_; }
80 int Count() const {
81 return (assertive_node_ ? 1 : 0) + (polite_node_ ? 1 : 0);
82 }
83
84 static constexpr int kHighPriorityIndex = 0;
85 static constexpr int kNormalPriorityIndex = 1;
86
87 private:
88 std::unique_ptr<AXNode> CreateNode(const std::string& live_status,
89 AXNode* root);
90
91 std::unique_ptr<AXNode> assertive_node_;
92 std::unique_ptr<AXNode> polite_node_;
93};
94#endif // BUILDFLAG(IS_LINUX)
95
[email protected]4b02bbca2013-11-22 08:59:0396// AXTree is a live, managed tree of AXNode objects that can receive
97// updates from another AXTreeSource via AXTreeUpdates, and it can be
98// used as a source for sending updates to another client tree.
99// It's designed to be subclassed to implement support for native
100// accessibility APIs on a specific platform.
Alexander Surkov4ab64cf2022-09-01 20:07:46101class AX_EXPORT AXTree {
[email protected]4b02bbca2013-11-22 08:59:03102 public:
Lei Zhanga06144782020-03-13 09:28:47103 using IntReverseRelationMap =
Nektarios Paisios527d33fb52021-02-23 19:23:28104 std::map<ax::mojom::IntAttribute, std::map<AXNodeID, std::set<AXNodeID>>>;
Lei Zhanga06144782020-03-13 09:28:47105 using IntListReverseRelationMap =
106 std::map<ax::mojom::IntListAttribute,
Nektarios Paisios527d33fb52021-02-23 19:23:28107 std::map<AXNodeID, std::set<AXNodeID>>>;
David Tsengef6b480d2018-02-19 12:48:42108
Nektarios Paisios77c422a2021-10-19 10:37:00109 // If called, the focused node in this tree will never be ignored, even if it
110 // has the ignored state set. For now, this boolean will be set to false for
111 // all trees except in test scenarios, in order to thoroughly test the
112 // relevant code without causing any potential regressions. Ultimately, we
113 // want to expose all focused nodes so that a user of an assistive technology
114 // will be able to interact with the application / website, even if there is
115 // an authoring error, e.g. the aria-hidden attribute has been applied to the
116 // focused element.
117 // TODO(nektar): Removed once the feature has been fully tested.
118 static void SetFocusedNodeShouldNeverBeIgnored();
119
120 // Determines the ignored state of a node, given information about the node
121 // and the tree.
122 static bool ComputeNodeIsIgnored(const AXTreeData* optional_tree_data,
123 const AXNodeData& node_data);
124
125 // Determines whether a node has flipped its ignored state, given information
126 // about the previous and current state of the node / tree.
127 static bool ComputeNodeIsIgnoredChanged(
128 const AXTreeData* optional_old_tree_data,
129 const AXNodeData& old_node_data,
130 const AXTreeData* optional_new_tree_data,
131 const AXNodeData& new_node_data);
132
[email protected]4b02bbca2013-11-22 08:59:03133 AXTree();
dmazzoni329fd012015-10-22 20:05:35134 explicit AXTree(const AXTreeUpdate& initial_state);
[email protected]4b02bbca2013-11-22 08:59:03135 virtual ~AXTree();
136
Lei Zhanga06144782020-03-13 09:28:47137 // AXTree owns pointers so copying is non-trivial.
138 AXTree(const AXTree&) = delete;
139 AXTree& operator=(const AXTree&) = delete;
140
Dominic Mazzoni8549eb682018-12-11 23:48:32141 void AddObserver(AXTreeObserver* observer);
142 bool HasObserver(AXTreeObserver* observer);
Nektarios Paisiosdb7b5ee2020-02-18 21:28:11143 void RemoveObserver(AXTreeObserver* observer);
Dominic Mazzoni8549eb682018-12-11 23:48:32144
145 base::ObserverList<AXTreeObserver>& observers() { return observers_; }
[email protected]e736e81b2014-02-24 07:15:58146
tfarina6b1c1e082015-02-20 23:47:07147 AXNode* root() const { return root_; }
148
Alexander Surkov4ab64cf2022-09-01 20:07:46149 const AXTreeData& data() const;
dmazzoni329fd012015-10-22 20:05:35150
Nektarios Paisiosdb7b5ee2020-02-18 21:28:11151 // Destroys the tree and notifies all observers.
152 void Destroy();
153
Adam Ettenberger86af2532019-09-17 20:04:46154 // Returns the globally unique ID of this accessibility tree.
Alexander Surkov4ab64cf2022-09-01 20:07:46155 const AXTreeID& GetAXTreeID() const;
Adam Ettenberger86af2532019-09-17 20:04:46156
Alexander Surkov4ab64cf2022-09-01 20:07:46157 // Given a node in this accessibility tree that corresponds to a table
158 // or grid, return an object containing information about the
159 // table structure. This object is computed lazily on-demand and
160 // cached until the next time the tree is updated. Clients should
161 // not retain this pointer, they should just request it every time
162 // it's needed.
163 //
164 // Returns nullptr if the node is not a valid table.
165 AXTableInfo* GetTableInfo(const AXNode* table_node) const;
166
tfarina6b1c1e082015-02-20 23:47:07167 // Returns the AXNode with the given |id| if it is part of this AXTree.
Alexander Surkov4ab64cf2022-09-01 20:07:46168 AXNode* GetFromId(AXNodeID id) const;
[email protected]4b02bbca2013-11-22 08:59:03169
[email protected]d4e273462013-12-04 04:37:58170 // Returns true on success. If it returns false, it's a fatal error
171 // and this tree should be destroyed, and the source of the tree update
172 // should not be trusted any longer.
dmazzoni329fd012015-10-22 20:05:35173 virtual bool Unserialize(const AXTreeUpdate& update);
174
Nektarios Paisios26ac2b42021-06-02 18:24:19175 // Used by tests to update the tree data without changing any of the nodes in
176 // the tree, notifying all tree observers in the process.
177 virtual void UpdateDataForTesting(const AXTreeData& data);
[email protected]4b02bbca2013-11-22 08:59:03178
Dominic Mazzoni2410fc62017-06-09 22:19:18179 // Convert any rectangle from the local coordinate space of one node in
180 // the tree, to bounds in the coordinate space of the tree.
Katie Dektar2c6052d2017-09-27 00:32:32181 // If set, updates |offscreen| boolean to be true if the node is offscreen
182 // relative to its rootWebArea. Callers should initialize |offscreen|
183 // to false: this method may get called multiple times in a row and
184 // |offscreen| will be propagated.
Katie Dektardb71ad942017-11-29 20:07:48185 // If |clip_bounds| is true, result bounds will be clipped.
Dominic Mazzoni2410fc62017-06-09 22:19:18186 gfx::RectF RelativeToTreeBounds(const AXNode* node,
Katie Dektar2c6052d2017-09-27 00:32:32187 gfx::RectF node_bounds,
Katie Dektardb71ad942017-11-29 20:07:48188 bool* offscreen = nullptr,
David Tseng7bf30c3c2022-07-22 15:13:37189 bool clip_bounds = true,
190 bool skip_container_offset = false) const;
Dominic Mazzoni2410fc62017-06-09 22:19:18191
192 // Get the bounds of a node in the coordinate space of the tree.
Katie Dektar2c6052d2017-09-27 00:32:32193 // If set, updates |offscreen| boolean to be true if the node is offscreen
194 // relative to its rootWebArea. Callers should initialize |offscreen|
195 // to false: this method may get called multiple times in a row and
196 // |offscreen| will be propagated.
Katie Dektardb71ad942017-11-29 20:07:48197 // If |clip_bounds| is true, result bounds will be clipped.
198 gfx::RectF GetTreeBounds(const AXNode* node,
199 bool* offscreen = nullptr,
200 bool clip_bounds = true) const;
Dominic Mazzoni2410fc62017-06-09 22:19:18201
Dominic Mazzoni35f2a5252017-09-26 00:56:04202 // Given a node ID attribute (one where IsNodeIdIntAttribute is true),
203 // and a destination node ID, return a set of all source node IDs that
204 // have that relationship attribute between them and the destination.
Nektarios Paisios527d33fb52021-02-23 19:23:28205 std::set<AXNodeID> GetReverseRelations(ax::mojom::IntAttribute attr,
206 AXNodeID dst_id) const;
Dominic Mazzoni35f2a5252017-09-26 00:56:04207
208 // Given a node ID list attribute (one where
209 // IsNodeIdIntListAttribute is true), and a destination node ID,
210 // return a set of all source node IDs that have that relationship
211 // attribute between them and the destination.
Nektarios Paisios527d33fb52021-02-23 19:23:28212 std::set<AXNodeID> GetReverseRelations(ax::mojom::IntListAttribute attr,
213 AXNodeID dst_id) const;
Dominic Mazzoni35f2a5252017-09-26 00:56:04214
Dominic Mazzoniaa774822018-08-29 20:33:58215 // Given a child tree ID, return the node IDs of all nodes in the tree who
216 // have a kChildTreeId int attribute with that value.
Nektarios Paisios50e579f12022-05-19 19:22:39217 //
218 // TODO(accessibility): There should really be only one host node per child
219 // tree, so the return value should not be a set but a single node ID or
220 // `kInvalidAXNodeID`.
Nektarios Paisios527d33fb52021-02-23 19:23:28221 std::set<AXNodeID> GetNodeIdsForChildTreeId(AXTreeID child_tree_id) const;
Dominic Mazzoniaa774822018-08-29 20:33:58222
Dominic Mazzoni748888c2018-11-01 22:34:59223 // Get all of the child tree IDs referenced by any node in this tree.
224 const std::set<AXTreeID> GetAllChildTreeIds() const;
225
David Tsengef6b480d2018-02-19 12:48:42226 // Map from a relation attribute to a map from a target id to source ids.
227 const IntReverseRelationMap& int_reverse_relations() {
228 return int_reverse_relations_;
229 }
230 const IntListReverseRelationMap& intlist_reverse_relations() {
231 return intlist_reverse_relations_;
232 }
Dominic Mazzoni3d9b5b92018-04-18 21:36:38233
[email protected]5eec2f52014-01-06 22:30:54234 // Return a multi-line indented string representation, for logging.
Aaron Leventhal763d46a2022-12-21 19:16:22235 std::string ToString(bool verbose = true) const;
[email protected]5eec2f52014-01-06 22:30:54236
[email protected]d4e273462013-12-04 04:37:58237 // A string describing the error from an unsuccessful Unserialize,
238 // for testing and debugging.
tfarinad0bfb4b62015-02-18 17:20:32239 const std::string& error() const { return error_; }
[email protected]d4e273462013-12-04 04:37:58240
dmazzoniee2eaca2015-03-18 18:13:07241 int size() { return static_cast<int>(id_map_.size()); }
242
Dominic Mazzonid42e00a2018-06-27 23:14:23243 // Return a negative number that's suitable to use for a node ID for
244 // internal nodes created automatically by an AXTree, so as not to
245 // conflict with positive-numbered node IDs from tree sources.
Nektarios Paisios527d33fb52021-02-23 19:23:28246 AXNodeID GetNextNegativeInternalNodeId();
Dominic Mazzonid42e00a2018-06-27 23:14:23247
Akihiro Otaf42a7d02020-06-12 19:07:56248 // Returns the PosInSet of |node|. Looks in node_set_size_pos_in_set_info_map_
249 // for cached value. Calls |ComputeSetSizePosInSetAndCache|if no value is
250 // present in the cache.
Arthur Sonzogni3eb9fd512024-02-09 12:20:43251 std::optional<int> GetPosInSet(const AXNode& node);
Alexander Surkov4ab64cf2022-09-01 20:07:46252
Akihiro Otaf42a7d02020-06-12 19:07:56253 // Returns the SetSize of |node|. Looks in node_set_size_pos_in_set_info_map_
254 // for cached value. Calls |ComputeSetSizePosInSetAndCache|if no value is
255 // present in the cache.
Arthur Sonzogni3eb9fd512024-02-09 12:20:43256 std::optional<int> GetSetSize(const AXNode& node);
Akihiro Ota413ca722018-12-03 23:29:00257
Nektarios Paisiosd3e82c6d2022-08-12 19:06:55258 // Returns the part of the current selection that falls within this
259 // accessibility tree, if any.
Alexander Surkov4ab64cf2022-09-01 20:07:46260 AXSelection GetSelection() const;
Nektarios Paisiosd3e82c6d2022-08-12 19:06:55261
262 // Returns the part of the current selection that falls within this
263 // accessibility tree, if any, adjusting its endpoints to be within unignored
264 // nodes. (An "ignored" node is a node that is not exposed to platform APIs:
265 // See `AXNode::IsIgnored`.)
Aaron leventhalebac41bf2025-04-17 22:40:29266 AXSelection GetUnignoredSelection() const;
Jacques Newman339afc62019-08-14 00:49:22267
Alexander Surkov4ab64cf2022-09-01 20:07:46268 bool GetTreeUpdateInProgressState() const;
Akihiro Otae3e420e2019-04-17 19:57:40269
Kurt Catti-Schmidtc8445a12019-08-07 18:52:58270 // Returns true if the tree represents a paginated document
Alexander Surkov4ab64cf2022-09-01 20:07:46271 bool HasPaginationSupport() const;
Kurt Catti-Schmidtc8445a12019-08-07 18:52:58272
Chris Hall697d99b2019-07-09 02:36:11273 // Language detection manager, entry point to language detection features.
274 // TODO(chrishall): Should this be stored by pointer or value?
275 // When should we initialize this?
276 std::unique_ptr<AXLanguageDetectionManager> language_detection_manager;
Chris Hall34208182019-03-13 02:26:18277
David Tseng41f13cbd2021-11-06 18:39:03278 // Event metadata while applying a tree update during unserialization.
279 AXEvent* event_data() const { return event_data_.get(); }
David Tsengb0d43662020-05-20 20:47:28280
Benjamin Beaudry710df4d2023-02-08 23:47:56281 // Notify the delegate that the tree manager for |previous_tree_id| will be
282 // removed from the AXTreeManagerMap. Because we sometimes remove the tree
283 // manager after the tree's id has been modified, we need to pass the (old)
284 // tree id associated with the manager we are removing even though it is the
285 // same tree.
286 void NotifyTreeManagerWillBeRemoved(AXTreeID previous_tree_id);
Benjamin Beaudrye8f23a22020-12-17 20:08:02287
Stefan Zager3a5aabb2023-03-28 22:53:20288 void NotifyChildTreeConnectionChanged(AXNode* node, AXTree* child_tree);
289
Evelynn Kaplan6cd535ce2025-03-20 23:58:18290#if BUILDFLAG(IS_LINUX)
291 void ClearExtraAnnouncementNodes();
292 void CreateExtraAnnouncementNodes();
293 ExtraAnnouncementNodes* extra_announcement_nodes() const {
294 return extra_announcement_nodes_.get();
295 }
296#endif // BUILDFLAG(IS_LINUX)
297
[email protected]e736e81b2014-02-24 07:15:58298 private:
Nektarios Paisios8eb31fc2022-02-18 16:37:58299 friend class ScopedTreeUpdateInProgressStateSetter;
Dominic Mazzoniecfb4fd2018-10-23 07:34:20300 friend class AXTableInfoTest;
301
Nektarios Paisios77c422a2021-10-19 10:37:00302 // Indicates if the node with the focus should never be ignored, (see
303 // `SetFocusedNodeShouldNeverBeIgnored` above).
304 static bool is_focused_node_always_unignored_;
305
Aaron Leventhalf92d9972024-12-05 20:58:38306#if AX_FAIL_FAST_BUILD()
Aaron Leventhalc0bc6e92024-02-02 01:09:13307 void CheckTreeConsistency(const AXTreeUpdate& update);
308#endif
309
Aaron Leventhala674d632020-09-30 07:05:36310 // Accumulate errors as there can be more than one before Chrome is crashed
Aaron Leventhal6ceb5882024-04-17 15:19:38311 // via UnrecoverableAccessibilityError();
Aaron Leventhale12416e2022-12-23 16:08:17312 // In an AX_FAIL_FAST_BUILD or if |is_fatal|, will assert/crash immediately.
Aaron Leventhalbc649ff2022-08-11 18:53:23313 void RecordError(const AXTreeUpdateState& update_state,
Aaron Leventhale12416e2022-12-23 16:08:17314 std::string new_error,
315 bool is_fatal = false);
Aaron Leventhala674d632020-09-30 07:05:36316
dtseng5a7b3d92016-09-08 06:35:58317 AXNode* CreateNode(AXNode* parent,
Dominic Mazzoni9ccdedb22021-01-30 17:59:42318 AXNodeID id,
Peter Kasting94a07a12019-05-22 19:26:28319 size_t index_in_parent,
dtseng5a7b3d92016-09-08 06:35:58320 AXTreeUpdateState* update_state);
[email protected]4b02bbca2013-11-22 08:59:03321
Adam Ettenbergerd9d8d58a2019-08-06 16:54:08322 // Accumulates the work that will be required to update the AXTree.
323 // This allows us to notify observers of structure changes when the
324 // tree is still in a stable and unchanged state.
325 bool ComputePendingChanges(const AXTreeUpdate& update,
Aaron Leventhal051c14312023-09-01 00:17:01326 AXTreeUpdateState* update_state);
Adam Ettenbergerd9d8d58a2019-08-06 16:54:08327
328 // Populates |update_state| with information about actions that will
329 // be performed on the tree during the update, such as adding or
330 // removing nodes in the tree. Returns true on success.
331 // Nothing within this call should modify tree structure or node data.
332 bool ComputePendingChangesToNode(const AXNodeData& new_data,
333 bool is_new_root,
Aaron Leventhal051c14312023-09-01 00:17:01334 AXTreeUpdateState* update_state);
Adam Ettenbergerd9d8d58a2019-08-06 16:54:08335
[email protected]4b02bbca2013-11-22 08:59:03336 // This is called from within Unserialize(), it returns true on success.
dmazzoni67b4db22016-04-23 00:40:04337 bool UpdateNode(const AXNodeData& src,
338 bool is_new_root,
339 AXTreeUpdateState* update_state);
[email protected]4b02bbca2013-11-22 08:59:03340
Adam Ettenbergerd9d8d58a2019-08-06 16:54:08341 // Notify the delegate that the subtree rooted at |node| will be
342 // destroyed or reparented.
343 void NotifySubtreeWillBeReparentedOrDeleted(
344 AXNode* node,
345 const AXTreeUpdateState* update_state);
346
347 // Notify the delegate that |node| will be destroyed or reparented.
348 void NotifyNodeWillBeReparentedOrDeleted(
Lucas Radaelli36b18752024-09-17 18:37:54349 AXNode& node,
350 const AXTreeUpdateState& update_state);
Adam Ettenbergerd9d8d58a2019-08-06 16:54:08351
352 // Notify the delegate that |node| and all of its descendants will be
Victor Feie3cce4c2019-11-14 18:35:41353 // destroyed. This function is called during AXTree teardown.
Lucas Radaelli36b18752024-09-17 18:37:54354 void RecursivelyNotifyNodeWillBeDeletedForTreeTeardown(
355 AXNode& node,
David Tsengd87e7632024-10-09 17:17:51356 std::set<AXNodeID>& deleted_nodes);
Victor Feie3cce4c2019-11-14 18:35:41357
358 // Notify the delegate that the node marked by |node_id| has been deleted.
359 // We are passing the node id instead of ax node is because by the time this
360 // function is called, the ax node in the tree will already have been
Adam Ettenbergerd9d8d58a2019-08-06 16:54:08361 // destroyed.
Dominic Mazzoni9ccdedb22021-01-30 17:59:42362 void NotifyNodeHasBeenDeleted(AXNodeID node_id);
Adam Ettenbergerd9d8d58a2019-08-06 16:54:08363
364 // Notify the delegate that |node| has been created or reparented.
365 void NotifyNodeHasBeenReparentedOrCreated(
366 AXNode* node,
367 const AXTreeUpdateState* update_state);
368
Nektarios Paisios77c422a2021-10-19 10:37:00369 // Notify the delegate that `node` will change its data attributes, including
370 // its ignored state.
371 void NotifyNodeAttributesWillChange(AXNode* node,
Aaron Leventhal06f376bf2022-12-01 02:15:07372 AXTreeUpdateState& update_state,
Nektarios Paisios77c422a2021-10-19 10:37:00373 const AXTreeData* optional_old_tree_data,
374 const AXNodeData& old_data,
375 const AXTreeData* new_tree_data,
376 const AXNodeData& new_data);
Adam Ettenberger05afcec2019-08-06 17:11:29377
Aaron Leventhal06f376bf2022-12-01 02:15:07378 // Notify the delegate that `node` will change its its ignored state.
379 void NotifyNodeIgnoredStateWillChange(
380 AXNode* node,
381 const AXTreeData* optional_old_tree_data,
382 const AXNodeData& old_data,
383 const AXTreeData* new_tree_data,
384 const AXNodeData& new_data);
385
Nektarios Paisios77c422a2021-10-19 10:37:00386 // Notify the delegate that `node` has changed its data attributes, including
387 // its ignored state.
388 void NotifyNodeAttributesHaveBeenChanged(
389 AXNode* node,
Aaron Leventhal06f376bf2022-12-01 02:15:07390 AXTreeUpdateState& update_state,
Nektarios Paisios77c422a2021-10-19 10:37:00391 const AXTreeData* optional_old_tree_data,
392 const AXNodeData& old_data,
393 const AXTreeData* new_tree_data,
394 const AXNodeData& new_data);
dmazzoni3ab5385c2017-03-13 18:07:03395
Aaron Leventhal32de9b92024-03-12 15:55:38396 // Update maps that track which relations are pointing to |node|.
397 void UpdateReverseRelations(AXNode* node,
398 const AXNodeData& new_data,
399 bool is_new_node = false);
Dominic Mazzoni35f2a5252017-09-26 00:56:04400
Nektarios Paisios8eb31fc2022-02-18 16:37:58401 // Sets a flag indicating whether the tree is currently being updated or not.
402 // If the tree is being updated, then its internal pointers might be invalid
403 // and the tree should not be traversed.
404 void SetTreeUpdateInProgressState(bool set_tree_update_value);
405
Adam Ettenbergerd9d8d58a2019-08-06 16:54:08406 // Returns true if all pending changes in the |update_state| have been
407 // handled. If this returns false, the |error_| message will be populated.
408 // It's a fatal error to have pending changes after exhausting
409 // the AXTreeUpdate.
410 bool ValidatePendingChangesComplete(const AXTreeUpdateState& update_state);
[email protected]4b02bbca2013-11-22 08:59:03411
Adam Ettenbergerd9d8d58a2019-08-06 16:54:08412 // Modifies |update_state| so that it knows what subtree and nodes are
413 // going to be destroyed for the subtree rooted at |node|.
Dominic Mazzoni9ccdedb22021-01-30 17:59:42414 void MarkSubtreeForDestruction(AXNodeID node_id,
Adam Ettenbergerd9d8d58a2019-08-06 16:54:08415 AXTreeUpdateState* update_state);
416
417 // Modifies |update_state| so that it knows what nodes are
418 // going to be destroyed for the subtree rooted at |node|.
Dominic Mazzoni9ccdedb22021-01-30 17:59:42419 void MarkNodesForDestructionRecursive(AXNodeID node_id,
Adam Ettenbergerd9d8d58a2019-08-06 16:54:08420 AXTreeUpdateState* update_state);
421
422 // Validates that destroying the subtree rooted at |node| has required
423 // information in |update_state|, then calls DestroyNodeAndSubtree on it.
dmazzonie3b7faf2015-06-01 17:56:36424 void DestroySubtree(AXNode* node, AXTreeUpdateState* update_state);
dmazzonia4b48912015-01-24 00:08:56425
[email protected]4b02bbca2013-11-22 08:59:03426 // Call Destroy() on |node|, and delete it from the id map, and then
427 // call recursively on all nodes in its subtree.
dmazzonie3b7faf2015-06-01 17:56:36428 void DestroyNodeAndSubtree(AXNode* node, AXTreeUpdateState* update_state);
[email protected]4b02bbca2013-11-22 08:59:03429
430 // Iterate over the children of |node| and for each child, destroy the
Adam Ettenbergerd9d8d58a2019-08-06 16:54:08431 // child and its subtree if its id is not in |new_child_ids|.
432 void DeleteOldChildren(AXNode* node,
Nektarios Paisios527d33fb52021-02-23 19:23:28433 const std::vector<AXNodeID>& new_child_ids,
dmazzonie3b7faf2015-06-01 17:56:36434 AXTreeUpdateState* update_state);
[email protected]4b02bbca2013-11-22 08:59:03435
436 // Iterate over |new_child_ids| and populate |new_children| with
437 // pointers to child nodes, reusing existing nodes already in the tree
438 // if they exist, and creating otherwise. Reparenting is disallowed, so
439 // if the id already exists as the child of another node, that's an
[email protected]e736e81b2014-02-24 07:15:58440 // error. Returns true on success, false on fatal error.
Ali Hijazie63cbaf62023-12-20 19:29:35441 bool CreateNewChildVector(
442 AXNode* node,
443 const std::vector<AXNodeID>& new_child_ids,
444 std::vector<raw_ptr<AXNode, VectorExperimental>>* new_children,
445 AXTreeUpdateState* update_state);
[email protected]4b02bbca2013-11-22 08:59:03446
Nektarios Paisios77c422a2021-10-19 10:37:00447 // Returns the lowest unignored ancestor of the node with the given ID. If the
448 // node is not ignored, it returns the node.
449 AXNode* GetUnignoredAncestorFromId(AXNodeID node_id) const;
450
Dominic Mazzonia1bb0d122019-09-26 20:19:59451 // Internal implementation of RelativeToTreeBounds. It calls itself
452 // recursively but ensures that it can only do so exactly once!
453 gfx::RectF RelativeToTreeBoundsInternal(const AXNode* node,
454 gfx::RectF node_bounds,
455 bool* offscreen,
456 bool clip_bounds,
David Tseng7bf30c3c2022-07-22 15:13:37457 bool skip_container_offset,
Dominic Mazzonia1bb0d122019-09-26 20:19:59458 bool allow_recursion) const;
459
Dominic Mazzoni8549eb682018-12-11 23:48:32460 base::ObserverList<AXTreeObserver> observers_;
Ali Hijazi2fe3e402022-08-05 16:09:31461 raw_ptr<AXNode> root_ = nullptr;
Bruce Dawson9d565ec2022-10-31 17:42:34462 std::unordered_map<AXNodeID, std::unique_ptr<AXNode>> id_map_;
[email protected]d4e273462013-12-04 04:37:58463 std::string error_;
dmazzoni329fd012015-10-22 20:05:35464 AXTreeData data_;
Dominic Mazzoni35f2a5252017-09-26 00:56:04465
466 // Map from an int attribute (if IsNodeIdIntAttribute is true) to
467 // a reverse mapping from target nodes to source nodes.
David Tsengef6b480d2018-02-19 12:48:42468 IntReverseRelationMap int_reverse_relations_;
Dominic Mazzoni35f2a5252017-09-26 00:56:04469 // Map from an int list attribute (if IsNodeIdIntListAttribute is true) to
470 // a reverse mapping from target nodes to source nodes.
David Tsengef6b480d2018-02-19 12:48:42471 IntListReverseRelationMap intlist_reverse_relations_;
Dominic Mazzoniaa774822018-08-29 20:33:58472 // Map from child tree ID to the set of node IDs that contain that attribute.
Nektarios Paisios527d33fb52021-02-23 19:23:28473 std::map<AXTreeID, std::set<AXNodeID>> child_tree_id_reverse_map_;
Dominic Mazzoni3d9b5b92018-04-18 21:36:38474
475 // Map from node ID to cached table info, if the given node is a table.
Dominic Mazzonid42e00a2018-06-27 23:14:23476 // Invalidated every time the tree is updated.
Bruce Dawson9d565ec2022-10-31 17:42:34477 mutable std::unordered_map<AXNodeID, std::unique_ptr<AXTableInfo>>
Aaron Leventhal80797182020-02-25 18:50:31478 table_info_map_;
Dominic Mazzonid42e00a2018-06-27 23:14:23479
480 // The next negative node ID to use for internal nodes.
Nektarios Paisios527d33fb52021-02-23 19:23:28481 AXNodeID next_negative_internal_node_id_ = -1;
Dominic Mazzonid42e00a2018-06-27 23:14:23482
Akihiro Ota413ca722018-12-03 23:29:00483 // Contains pos_in_set and set_size data for an AXNode.
Stephan Hartmannaeef6882020-04-20 18:21:43484 struct NodeSetSizePosInSetInfo {
485 NodeSetSizePosInSetInfo();
486 ~NodeSetSizePosInSetInfo();
487
Arthur Sonzogni3eb9fd512024-02-09 12:20:43488 std::optional<int> pos_in_set;
489 std::optional<int> set_size;
490 std::optional<int> lowest_hierarchical_level;
Stephan Hartmannaeef6882020-04-20 18:21:43491 };
Akihiro Ota413ca722018-12-03 23:29:00492
Victor Fei5eea952e2020-02-28 01:43:09493 // Represents the content of an ordered set which includes the ordered set
494 // items and the ordered set container if it exists.
495 struct OrderedSetContent;
496
497 // Maps a particular hierarchical level to a list of OrderedSetContents.
498 // Represents all ordered set items/container on a particular hierarchical
499 // level.
500 struct OrderedSetItemsMap;
501
502 // Populates |items_map_to_be_populated| with all items associated with
Victor Feid95130c2020-02-03 21:42:54503 // |original_node| and within |ordered_set|. Only items whose roles match the
504 // role of the |ordered_set| will be added.
Victor Fei5eea952e2020-02-28 01:43:09505 void PopulateOrderedSetItemsMap(
Victor Feid95130c2020-02-03 21:42:54506 const AXNode& original_node,
507 const AXNode* ordered_set,
Lei Zhanga06144782020-03-13 09:28:47508 OrderedSetItemsMap* items_map_to_be_populated) const;
Victor Feid95130c2020-02-03 21:42:54509
Victor Fei5eea952e2020-02-28 01:43:09510 // Helper function for recursively populating ordered sets items map with
Victor Feid95130c2020-02-03 21:42:54511 // all items associated with |original_node| and |ordered_set|. |local_parent|
512 // tracks the recursively passed in child nodes of |ordered_set|.
Victor Fei5eea952e2020-02-28 01:43:09513 void RecursivelyPopulateOrderedSetItemsMap(
Victor Feid95130c2020-02-03 21:42:54514 const AXNode& original_node,
515 const AXNode* ordered_set,
516 const AXNode* local_parent,
Arthur Sonzogni3eb9fd512024-02-09 12:20:43517 std::optional<int> ordered_set_min_level,
518 std::optional<int> prev_level,
Lei Zhanga06144782020-03-13 09:28:47519 OrderedSetItemsMap* items_map_to_be_populated) const;
Akihiro Ota886a96d62018-12-18 00:11:48520
Victor Fei5eea952e2020-02-28 01:43:09521 // Computes the pos_in_set and set_size values of all items in ordered_set and
522 // caches those values. Called by GetPosInSet and GetSetSize.
Akihiro Ota886a96d62018-12-18 00:11:48523 void ComputeSetSizePosInSetAndCache(const AXNode& node,
524 const AXNode* ordered_set);
Akihiro Ota413ca722018-12-03 23:29:00525
Victor Fei5eea952e2020-02-28 01:43:09526 // Helper for ComputeSetSizePosInSetAndCache. Computes and caches the
527 // pos_in_set and set_size values for a given OrderedSetContent.
528 void ComputeSetSizePosInSetAndCacheHelper(
529 const OrderedSetContent& ordered_set_content);
530
Akihiro Otab6a8a4d2018-12-04 01:56:39531 // Map from node ID to OrderedSetInfo.
532 // Item-like and ordered-set-like objects will map to populated OrderedSetInfo
533 // objects.
534 // All other objects will map to default-constructed OrderedSetInfo objects.
Akihiro Ota413ca722018-12-03 23:29:00535 // Invalidated every time the tree is updated.
Bruce Dawson9d565ec2022-10-31 17:42:34536 mutable std::unordered_map<AXNodeID, NodeSetSizePosInSetInfo>
Victor Fei5eea952e2020-02-28 01:43:09537 node_set_size_pos_in_set_info_map_;
Chris Hall9b34c2c2018-12-04 01:45:56538
Akihiro Otae3e420e2019-04-17 19:57:40539 // Indicates if the tree is updating.
540 bool tree_update_in_progress_ = false;
Kurt Catti-Schmidtc8445a12019-08-07 18:52:58541
542 // Indicates if the tree represents a paginated document
543 bool has_pagination_support_ = false;
David Tsengb0d43662020-05-20 20:47:28544
David Tseng41f13cbd2021-11-06 18:39:03545 std::unique_ptr<AXEvent> event_data_;
Evelynn Kaplan6cd535ce2025-03-20 23:58:18546
547#if BUILDFLAG(IS_LINUX)
548 std::unique_ptr<ExtraAnnouncementNodes> extra_announcement_nodes_ = nullptr;
549#endif // BUILDFLAG(IS_LINUX)
[email protected]4b02bbca2013-11-22 08:59:03550};
551
Nektarios Paisios8eb31fc2022-02-18 16:37:58552// Sets the flag that indicates whether the accessibility tree is currently
553// being updated, and ensures that it is reset to its previous value when the
554// instance is destructed. An accessibility tree that is being updated is
555// unstable and should not be traversed.
556class AX_EXPORT ScopedTreeUpdateInProgressStateSetter {
557 public:
558 explicit ScopedTreeUpdateInProgressStateSetter(AXTree& tree)
559 : tree_(&tree),
560 last_tree_update_in_progress_(tree.GetTreeUpdateInProgressState()) {
561 tree_->SetTreeUpdateInProgressState(true);
562 }
563
564 ~ScopedTreeUpdateInProgressStateSetter() {
565 tree_->SetTreeUpdateInProgressState(last_tree_update_in_progress_);
566 }
567
568 ScopedTreeUpdateInProgressStateSetter(
569 const ScopedTreeUpdateInProgressStateSetter&) = delete;
570 ScopedTreeUpdateInProgressStateSetter& operator=(
571 const ScopedTreeUpdateInProgressStateSetter&) = delete;
572
573 private:
Keishi Hattorie175ac52022-06-07 06:24:57574 const raw_ptr<AXTree> tree_;
Nektarios Paisios8eb31fc2022-02-18 16:37:58575 bool last_tree_update_in_progress_;
576};
577
[email protected]4b02bbca2013-11-22 08:59:03578} // namespace ui
579
580#endif // UI_ACCESSIBILITY_AX_TREE_H_