File: ax_tree_formatter_base.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (146 lines) | stat: -rw-r--r-- 5,891 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef UI_ACCESSIBILITY_PLATFORM_INSPECT_AX_TREE_FORMATTER_BASE_H_
#define UI_ACCESSIBILITY_PLATFORM_INSPECT_AX_TREE_FORMATTER_BASE_H_

#include <vector>

#include "base/component_export.h"
#include "base/values.h"
#include "ui/accessibility/platform/inspect/ax_tree_formatter.h"

namespace ui {

class AXPropertyNode;

// A utility class for formatting platform-specific accessibility information,
// for use in testing, debugging, and developer tools.
// This is extended by a subclass for each platform where accessibility is
// implemented.
class COMPONENT_EXPORT(AX_PLATFORM) AXTreeFormatterBase
    : public AXTreeFormatter {
 public:
  AXTreeFormatterBase();

  AXTreeFormatterBase(const AXTreeFormatterBase&) = delete;
  AXTreeFormatterBase& operator=(const AXTreeFormatterBase&) = delete;

  ~AXTreeFormatterBase() override;

  bool ShouldDumpNode(const AXPlatformNodeDelegate& node) const;
  bool ShouldDumpChildren(const AXPlatformNodeDelegate& node) const;

  // Build an accessibility tree for the current Chrome app.
  virtual base::Value::Dict BuildTree(AXPlatformNodeDelegate* root) const = 0;

  // AXTreeFormatter overrides.
  std::string Format(AXPlatformNodeDelegate* root) const override;
  std::string Format(const AXTreeSelector&) const override;
  std::string FormatNode(AXPlatformNodeDelegate* node) const override;
  std::string FormatNode(const AXTreeSelector&) const override;
  std::string FormatTree(const base::Value::Dict& tree_node) const override;
  base::Value::Dict BuildTreeForNode(AXNode* root) const override;
  std::string EvaluateScript(const AXTreeSelector& selector,
                             const AXInspectScenario& scenario) const override;
  std::string EvaluateScript(
      AXPlatformNodeDelegate* root,
      const std::vector<AXScriptInstruction>& instructions,
      size_t start_index,
      size_t end_index) const override;
  void SetPropertyFilters(const std::vector<AXPropertyFilter>& property_filters,
                          PropertyFilterSet default_filters_set) override;
  void SetNodeFilters(const std::vector<AXNodeFilter>& node_filters) override;
  void set_show_ids(bool show_ids) override;
  std::string DumpInternalAccessibilityTree(
      AXTreeID tree_id,
      const std::vector<AXPropertyFilter>& property_filters) override;

 protected:
  static const char kChildrenDictAttr[];

  //
  // Overridden by platform subclasses.
  //

  // Appends default filters of the formatter.
  virtual void AddDefaultFilters(
      std::vector<AXPropertyFilter>* property_filters);

  // Returns property nodes complying to the line index filter for all
  // allow/allow_empty property filters.
  std::vector<AXPropertyNode> PropertyFilterNodesFor(
      const std::string& line_index) const;

  // Returns a list of script property nodes.
  std::vector<AXPropertyNode> ScriptPropertyNodes() const;

  // Return true if match-all filter is present.
  bool HasMatchAllPropertyFilter() const;

  // Process accessibility tree with filters for output.
  // Given a dictionary that contains a platform-specific dictionary
  // representing an accessibility tree, and utilizing property_filters_ and
  // node_filters_:
  // - Returns a filtered text view as one large string.
  // - Provides a filtered version of the dictionary in an out param,
  //   (only if the out param is provided).
  virtual std::string ProcessTreeForOutput(
      const base::Value::Dict& node) const = 0;

  //
  // Utility functions to be used by each platform.
  //

  std::string FormatCoordinates(const base::Value::Dict& dict,
                                const std::string& name,
                                const std::string& x_name,
                                const std::string& y_name) const;

  std::string FormatRectangle(const base::Value::Dict& dict,
                              const std::string& name,
                              const std::string& left_name,
                              const std::string& top_name,
                              const std::string& width_name,
                              const std::string& height_name) const;

  // Writes the given attribute string out to |line| if it matches the property
  // filters.
  // Returns false if the attribute was filtered out.
  bool WriteAttribute(bool include_by_default,
                      const std::string& attr,
                      std::string* line) const;
  void AddPropertyFilter(std::vector<AXPropertyFilter>* property_filters,
                         std::string filter,
                         AXPropertyFilter::Type type = AXPropertyFilter::ALLOW);
  bool show_ids() const { return show_ids_; }

  base::Value::Dict BuildNode(AXPlatformNodeDelegate* node) const override;
  base::Value::Dict BuildNodeForSelector(const AXTreeSelector&) const override;

 private:
  void RecursiveFormatTree(const base::Value::Dict& tree_node,
                           std::string* contents,
                           int depth = 0) const;

  bool MatchesPropertyFilters(const std::string& text,
                              bool default_result) const;
  bool MatchesNodeFilters(const base::Value::Dict& dict) const;

  // Property filters used when formatting the accessibility tree as text.
  // Any property which matches a property filter will be skipped.
  std::vector<AXPropertyFilter> property_filters_;

  // Node filters used when formatting the accessibility tree as text.
  // Any node which matches a node wilder will be skipped, along with all its
  // children.
  std::vector<AXNodeFilter> node_filters_;

  // Whether or not node ids should be included in the dump.
  bool show_ids_ = false;
};

}  // namespace ui

#endif  // UI_ACCESSIBILITY_PLATFORM_INSPECT_AX_TREE_FORMATTER_BASE_H_