[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef UI_ACCESSIBILITY_AX_TREE_H_ |
| 6 | #define UI_ACCESSIBILITY_AX_TREE_H_ |
| 7 | |
avi | 9c81217b | 2015-12-24 23:40:05 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
Lei Zhang | a0614478 | 2020-03-13 09:28:47 | [diff] [blame] | 10 | #include <map> |
Chris Hall | 3420818 | 2019-03-13 02:26:18 | [diff] [blame] | 11 | #include <memory> |
[email protected] | d4e27346 | 2013-12-04 04:37:58 | [diff] [blame] | 12 | #include <set> |
Lei Zhang | a0614478 | 2020-03-13 09:28:47 | [diff] [blame] | 13 | #include <string> |
Lei Zhang | a0614478 | 2020-03-13 09:28:47 | [diff] [blame] | 14 | #include <vector> |
[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 15 | |
Jacob Francis | 78735e3 | 2022-06-30 22:02:52 | [diff] [blame] | 16 | #include "base/containers/flat_map.h" |
Aaron Leventhal | bc649ff | 2022-08-11 18:53:23 | [diff] [blame] | 17 | #include "base/debug/crash_logging.h" |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 18 | #include "base/memory/raw_ptr.h" |
Jacques Newman | 5846e62 | 2021-01-15 02:15:59 | [diff] [blame] | 19 | #include "base/metrics/histogram_functions.h" |
Dominic Mazzoni | 8549eb68 | 2018-12-11 23:48:32 | [diff] [blame] | 20 | #include "base/observer_list.h" |
Nektarios Paisios | 77c422a | 2021-10-19 10:37:00 | [diff] [blame] | 21 | #include "third_party/abseil-cpp/absl/types/optional.h" |
James Cook | 36cab7c | 2019-10-29 23:26:40 | [diff] [blame] | 22 | #include "ui/accessibility/ax_enums.mojom-forward.h" |
[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 23 | #include "ui/accessibility/ax_export.h" |
dmazzoni | 329fd01 | 2015-10-22 20:05:35 | [diff] [blame] | 24 | #include "ui/accessibility/ax_tree_data.h" |
[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 25 | #include "ui/accessibility/ax_tree_update.h" |
| 26 | |
| 27 | namespace ui { |
| 28 | |
David Tseng | 41f13cbd | 2021-11-06 18:39:03 | [diff] [blame] | 29 | struct AXEvent; |
Alexander Surkov | 4ab64cf | 2022-09-01 20:07:46 | [diff] [blame^] | 30 | class AXLanguageDetectionManager; |
| 31 | class AXNode; |
| 32 | struct AXNodeData; |
Dominic Mazzoni | 3d9b5b9 | 2018-04-18 21:36:38 | [diff] [blame] | 33 | class AXTableInfo; |
Dominic Mazzoni | 8549eb68 | 2018-12-11 23:48:32 | [diff] [blame] | 34 | class AXTreeObserver; |
[email protected] | e736e81b | 2014-02-24 07:15:58 | [diff] [blame] | 35 | struct AXTreeUpdateState; |
Alexander Surkov | 253235e | 2022-08-23 01:52:12 | [diff] [blame] | 36 | class AXSelection; |
[email protected] | e736e81b | 2014-02-24 07:15:58 | [diff] [blame] | 37 | |
Jacques Newman | 5846e62 | 2021-01-15 02:15:59 | [diff] [blame] | 38 | // These values are persisted to logs. Entries should not be renumbered and |
| 39 | // numeric values should never be reused. |
| 40 | enum 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] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 66 | // 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 Surkov | 4ab64cf | 2022-09-01 20:07:46 | [diff] [blame^] | 71 | class AX_EXPORT AXTree { |
[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 72 | public: |
Lei Zhang | a0614478 | 2020-03-13 09:28:47 | [diff] [blame] | 73 | using IntReverseRelationMap = |
Nektarios Paisios | 527d33fb5 | 2021-02-23 19:23:28 | [diff] [blame] | 74 | std::map<ax::mojom::IntAttribute, std::map<AXNodeID, std::set<AXNodeID>>>; |
Lei Zhang | a0614478 | 2020-03-13 09:28:47 | [diff] [blame] | 75 | using IntListReverseRelationMap = |
| 76 | std::map<ax::mojom::IntListAttribute, |
Nektarios Paisios | 527d33fb5 | 2021-02-23 19:23:28 | [diff] [blame] | 77 | std::map<AXNodeID, std::set<AXNodeID>>>; |
David Tseng | ef6b480d | 2018-02-19 12:48:42 | [diff] [blame] | 78 | |
Nektarios Paisios | 77c422a | 2021-10-19 10:37:00 | [diff] [blame] | 79 | // 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] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 103 | AXTree(); |
dmazzoni | 329fd01 | 2015-10-22 20:05:35 | [diff] [blame] | 104 | explicit AXTree(const AXTreeUpdate& initial_state); |
[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 105 | virtual ~AXTree(); |
| 106 | |
Lei Zhang | a0614478 | 2020-03-13 09:28:47 | [diff] [blame] | 107 | // AXTree owns pointers so copying is non-trivial. |
| 108 | AXTree(const AXTree&) = delete; |
| 109 | AXTree& operator=(const AXTree&) = delete; |
| 110 | |
Dominic Mazzoni | 8549eb68 | 2018-12-11 23:48:32 | [diff] [blame] | 111 | void AddObserver(AXTreeObserver* observer); |
| 112 | bool HasObserver(AXTreeObserver* observer); |
Nektarios Paisios | db7b5ee | 2020-02-18 21:28:11 | [diff] [blame] | 113 | void RemoveObserver(AXTreeObserver* observer); |
Dominic Mazzoni | 8549eb68 | 2018-12-11 23:48:32 | [diff] [blame] | 114 | |
| 115 | base::ObserverList<AXTreeObserver>& observers() { return observers_; } |
[email protected] | e736e81b | 2014-02-24 07:15:58 | [diff] [blame] | 116 | |
tfarina | 6b1c1e08 | 2015-02-20 23:47:07 | [diff] [blame] | 117 | AXNode* root() const { return root_; } |
| 118 | |
Alexander Surkov | 4ab64cf | 2022-09-01 20:07:46 | [diff] [blame^] | 119 | const AXTreeData& data() const; |
dmazzoni | 329fd01 | 2015-10-22 20:05:35 | [diff] [blame] | 120 | |
Nektarios Paisios | db7b5ee | 2020-02-18 21:28:11 | [diff] [blame] | 121 | // Destroys the tree and notifies all observers. |
| 122 | void Destroy(); |
| 123 | |
Adam Ettenberger | 86af253 | 2019-09-17 20:04:46 | [diff] [blame] | 124 | // Returns the globally unique ID of this accessibility tree. |
Alexander Surkov | 4ab64cf | 2022-09-01 20:07:46 | [diff] [blame^] | 125 | const AXTreeID& GetAXTreeID() const; |
Adam Ettenberger | 86af253 | 2019-09-17 20:04:46 | [diff] [blame] | 126 | |
Alexander Surkov | 4ab64cf | 2022-09-01 20:07:46 | [diff] [blame^] | 127 | // 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 | |
tfarina | 6b1c1e08 | 2015-02-20 23:47:07 | [diff] [blame] | 137 | // Returns the AXNode with the given |id| if it is part of this AXTree. |
Alexander Surkov | 4ab64cf | 2022-09-01 20:07:46 | [diff] [blame^] | 138 | AXNode* GetFromId(AXNodeID id) const; |
[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 139 | |
[email protected] | d4e27346 | 2013-12-04 04:37:58 | [diff] [blame] | 140 | // 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. |
dmazzoni | 329fd01 | 2015-10-22 20:05:35 | [diff] [blame] | 143 | virtual bool Unserialize(const AXTreeUpdate& update); |
| 144 | |
Nektarios Paisios | 26ac2b4 | 2021-06-02 18:24:19 | [diff] [blame] | 145 | // 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] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 148 | |
Dominic Mazzoni | 2410fc6 | 2017-06-09 22:19:18 | [diff] [blame] | 149 | // 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 Dektar | 2c6052d | 2017-09-27 00:32:32 | [diff] [blame] | 151 | // 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 Dektar | db71ad94 | 2017-11-29 20:07:48 | [diff] [blame] | 155 | // If |clip_bounds| is true, result bounds will be clipped. |
Dominic Mazzoni | 2410fc6 | 2017-06-09 22:19:18 | [diff] [blame] | 156 | gfx::RectF RelativeToTreeBounds(const AXNode* node, |
Katie Dektar | 2c6052d | 2017-09-27 00:32:32 | [diff] [blame] | 157 | gfx::RectF node_bounds, |
Katie Dektar |
|