ax_computed_node_data.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // Copyright 2021 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. #ifndef UI_ACCESSIBILITY_AX_COMPUTED_NODE_DATA_H_
  5. #define UI_ACCESSIBILITY_AX_COMPUTED_NODE_DATA_H_
  6. #include <stdint.h>
  7. #include <string>
  8. #include <vector>
  9. #include "base/memory/raw_ptr.h"
  10. #include "third_party/abseil-cpp/absl/types/optional.h"
  11. #include "ui/accessibility/ax_enums.mojom-forward.h"
  12. #include "ui/accessibility/ax_export.h"
  13. #include "ui/accessibility/ax_node_data.h"
  14. #include "ui/accessibility/ax_node_id_forward.h"
  15. namespace ui {
  16. class AXNode;
  17. // Computes and stores information about an `AXNode` that is slow or error-prone
  18. // to compute in the tree's source, e.g. in Blink. This class holds cached
  19. // values that should be re-computed when the associated `AXNode` is in any way
  20. // modified.
  21. class AX_EXPORT AXComputedNodeData final {
  22. public:
  23. explicit AXComputedNodeData(const AXNode& node);
  24. virtual ~AXComputedNodeData();
  25. AXComputedNodeData(const AXComputedNodeData& other) = delete;
  26. AXComputedNodeData& operator=(const AXComputedNodeData& other) = delete;
  27. // If the associated node is unignored, i.e. exposed to the platform's
  28. // assistive software, its position in the list of unignored children of its
  29. // lowest unignored parent. Naturally, this value is not defined when the
  30. // associated node is ignored.
  31. int GetOrComputeUnignoredIndexInParent() const;
  32. // The lowest unignored parent. This value should be computed for all
  33. // associated nodes, ignored and unignored. Only the rootnode should not have
  34. // an unignored parent.
  35. AXNodeID GetOrComputeUnignoredParentID() const;
  36. AXNode* GetOrComputeUnignoredParent() const;
  37. // If the associated node is unignored, i.e. exposed to the platform's
  38. // assistive software, the number of its children that are also unignored.
  39. // Naturally, this value is not defined when the associated node is ignored.
  40. int GetOrComputeUnignoredChildCount() const;
  41. // If the associated node is unignored, i.e. exposed to the platform's
  42. // assistive software, the IDs of its children that are also unignored.
  43. const std::vector<AXNodeID>& GetOrComputeUnignoredChildIDs() const;
  44. // Whether the associated node is a descendant of a platform leaf. The set of
  45. // platform leaves are the lowest nodes that are exposed to the platform's
  46. // assistive software.
  47. bool GetOrComputeIsDescendantOfPlatformLeaf() const;
  48. // Given an accessibility attribute, returns whether the attribute is
  49. // currently present in the node's data, or if it can always be computed on
  50. // demand.
  51. bool HasOrCanComputeAttribute(
  52. const ax::mojom::StringAttribute attribute) const;
  53. bool HasOrCanComputeAttribute(
  54. const ax::mojom::IntListAttribute attribute) const;
  55. // Given an accessibility attribute, returns the attribute's value. The
  56. // attribute is computed if not provided by the tree's source, otherwise it is
  57. // simply returned from the node's data. String and intlist attributes are
  58. // potentially the slowest to compute at the tree's source, e.g. in Blink.
  59. const std::string& GetOrComputeAttributeUTF8(
  60. const ax::mojom::StringAttribute attribute) const;
  61. std::u16string GetOrComputeAttributeUTF16(
  62. const ax::mojom::StringAttribute attribute) const;
  63. const std::vector<int32_t>& GetOrComputeAttribute(
  64. const ax::mojom::IntListAttribute attribute) const;
  65. // Retrieves from the cache or computes the on-screen text that is found
  66. // inside the associated node and all its descendants, caches the result, and
  67. // returns a reference to the cached text.
  68. //
  69. // Takes into account any formatting changes, such as paragraph breaks, that
  70. // have been introduced by layout. For example, in the Web context,
  71. // "A<div>B</div>C" would produce "A\nB\nC". Note that since hidden elements
  72. // are not in the accessibility tree, they are not included in the result.
  73. const std::string& GetOrComputeTextContentWithParagraphBreaksUTF8() const;
  74. const std::u16string& GetOrComputeTextContentWithParagraphBreaksUTF16() const;
  75. // Retrieves from the cache or computes the on-screen text that is found
  76. // inside the associated node and all its descendants, caches the result, and
  77. // returns a reference to the cached text.
  78. //
  79. // Does not take into account any formatting changes, such as extra paragraph
  80. // breaks, that have been introduced by layout. For example, in the Web
  81. // context, "A<div>B</div>C" would produce "ABC".
  82. const std::string& GetOrComputeTextContentUTF8() const;
  83. const std::u16string& GetOrComputeTextContentUTF16() const;
  84. // Returns the length of the on-screen text that is found inside the
  85. // associated node and all its descendants. The text is either retrieved from
  86. // the cache, or computed and then cached.
  87. //
  88. // Does not take into account line breaks that have been introduced by layout.
  89. // For example, in the Web context, "A<div>B</div>C" would produce 3 and
  90. // not 5.
  91. int GetOrComputeTextContentLengthUTF8() const;
  92. int GetOrComputeTextContentLengthUTF16() const;
  93. private:
  94. // Computes and caches the `unignored_index_in_parent_`, `unignored_parent_`,
  95. // `unignored_child_count_` and `unignored_child_ids_` for the associated
  96. // node.
  97. void ComputeUnignoredValues(AXNodeID unignored_parent_id = kInvalidAXNodeID,
  98. int starting_index_in_parent = 0) const;
  99. // Walks up the accessibility tree from the associated node until it finds the
  100. // lowest unignored ancestor.
  101. AXNode* SlowGetUnignoredParent() const;
  102. // Computes and caches (if not already in the cache) whether the associated
  103. // node is a descendant of a platform leaf. The set of platform leaves are the
  104. // lowest nodes that are exposed to the platform's assistive software.
  105. void ComputeIsDescendantOfPlatformLeaf() const;
  106. // Computes and caches (if not already in the cache) the character offsets
  107. // where each line in the associated node's on-screen text starts and ends.
  108. void ComputeLineOffsetsIfNeeded() const;
  109. // Computes and caches (if not already in the cache) the character offsets
  110. // where each sentence in the associated node's on-screen text starts and
  111. // ends.
  112. void ComputeSentenceOffsetsIfNeeded() const;
  113. // Computes and caches (if not already in the cache) the character offsets
  114. // where each word in the associated node's on-screen text starts and ends.
  115. // Note that the end offsets are always after the last character of each word,
  116. // so e.g. the end offset for the word "Hello" is 5, not 4.
  117. void ComputeWordOffsetsIfNeeded() const;
  118. // Computes the on-screen text that is found inside the associated node and
  119. // all its descendants.
  120. std::string ComputeTextContentUTF8() const;
  121. std::u16string ComputeTextContentUTF16() const;
  122. // The node that is associated with this instance. Weak, owns us.
  123. const raw_ptr<const AXNode> owner_;
  124. mutable absl::optional<int> unignored_index_in_parent_;
  125. mutable absl::optional<AXNodeID> unignored_parent_id_;
  126. mutable absl::optional<int> unignored_child_count_;
  127. mutable absl::optional<std::vector<AXNodeID>> unignored_child_ids_;
  128. mutable absl::optional<bool> is_descendant_of_leaf_;
  129. mutable absl::optional<std::vector<int32_t>> line_starts_;
  130. mutable absl::optional<std::vector<int32_t>> line_ends_;
  131. mutable absl::optional<std::vector<int32_t>> sentence_starts_;
  132. mutable absl::optional<std::vector<int32_t>> sentence_ends_;
  133. mutable absl::optional<std::vector<int32_t>> word_starts_;
  134. mutable absl::optional<std::vector<int32_t>> word_ends_;
  135. // There are two types of "text content". The first takes into
  136. // account any formatting changes, such as paragraph breaks, that have been
  137. // introduced by layout, whilst the other doesn't.
  138. //
  139. // Only one copy (either UTF8 or UTF16) should be cached as each platform
  140. // should only need one of the encodings. This applies to both text content as
  141. // well as text content with paragraph breaks.
  142. mutable absl::optional<std::string> text_content_with_paragraph_breaks_utf8_;
  143. mutable absl::optional<std::u16string>
  144. text_content_with_paragraph_breaks_utf16_;
  145. mutable absl::optional<std::string> text_content_utf8_;
  146. mutable absl::optional<std::u16string> text_content_utf16_;
  147. };
  148. } // namespace ui
  149. #endif // UI_ACCESSIBILITY_AX_COMPUTED_NODE_DATA_H_