File: aom_content_ax_tree.cc

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (313 lines) | stat: -rw-r--r-- 10,997 bytes parent folder | download | duplicates (3)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/renderer/accessibility/aom_content_ax_tree.h"

#include <string>

#include "content/renderer/accessibility/ax_tree_snapshotter_impl.h"
#include "third_party/blink/public/web/web_ax_enums.h"
#include "ui/accessibility/ax_enum_util.h"
#include "ui/accessibility/ax_enums.mojom.h"
#include "ui/accessibility/ax_node.h"
#include "ui/accessibility/ax_node_data.h"
#include "ui/accessibility/ax_tree_update.h"

namespace {

ax::mojom::BoolAttribute GetCorrespondingAXAttribute(
    blink::WebAOMBoolAttribute attr) {
  switch (attr) {
    case blink::WebAOMBoolAttribute::AOM_ATTR_ATOMIC:
      return ax::mojom::BoolAttribute::kLiveAtomic;
    case blink::WebAOMBoolAttribute::AOM_ATTR_BUSY:
      return ax::mojom::BoolAttribute::kBusy;
    case blink::WebAOMBoolAttribute::AOM_ATTR_MODAL:
      return ax::mojom::BoolAttribute::kModal;
    case blink::WebAOMBoolAttribute::AOM_ATTR_SELECTED:
      return ax::mojom::BoolAttribute::kSelected;
    default:
      return ax::mojom::BoolAttribute::kNone;
  }
}

ax::mojom::IntAttribute GetCorrespondingAXAttribute(
    blink::WebAOMIntAttribute attr) {
  switch (attr) {
    case blink::WebAOMIntAttribute::AOM_ATTR_COLUMN_COUNT:
      return ax::mojom::IntAttribute::kTableColumnCount;
    case blink::WebAOMIntAttribute::AOM_ATTR_COLUMN_INDEX:
      return ax::mojom::IntAttribute::kTableColumnIndex;
    case blink::WebAOMIntAttribute::AOM_ATTR_COLUMN_SPAN:
      return ax::mojom::IntAttribute::kTableCellColumnSpan;
    case blink::WebAOMIntAttribute::AOM_ATTR_HIERARCHICAL_LEVEL:
      return ax::mojom::IntAttribute::kHierarchicalLevel;
    case blink::WebAOMIntAttribute::AOM_ATTR_POS_IN_SET:
      return ax::mojom::IntAttribute::kPosInSet;
    case blink::WebAOMIntAttribute::AOM_ATTR_ROW_COUNT:
      return ax::mojom::IntAttribute::kTableRowCount;
    case blink::WebAOMIntAttribute::AOM_ATTR_ROW_INDEX:
      return ax::mojom::IntAttribute::kTableRowIndex;
    case blink::WebAOMIntAttribute::AOM_ATTR_ROW_SPAN:
      return ax::mojom::IntAttribute::kTableCellRowSpan;
    case blink::WebAOMIntAttribute::AOM_ATTR_SET_SIZE:
      return ax::mojom::IntAttribute::kSetSize;
    default:
      return ax::mojom::IntAttribute::kNone;
  }
}

ax::mojom::FloatAttribute GetCorrespondingAXAttribute(
    blink::WebAOMFloatAttribute attr) {
  switch (attr) {
    case blink::WebAOMFloatAttribute::AOM_ATTR_VALUE_MIN:
      return ax::mojom::FloatAttribute::kMinValueForRange;
    case blink::WebAOMFloatAttribute::AOM_ATTR_VALUE_MAX:
      return ax::mojom::FloatAttribute::kMaxValueForRange;
    case blink::WebAOMFloatAttribute::AOM_ATTR_VALUE_NOW:
      return ax::mojom::FloatAttribute::kValueForRange;
    default:
      return ax::mojom::FloatAttribute::kNone;
  }
}

ax::mojom::StringAttribute GetCorrespondingAXAttribute(
    blink::WebAOMStringAttribute attr) {
  switch (attr) {
    case blink::WebAOMStringAttribute::AOM_ATTR_AUTOCOMPLETE:
      return ax::mojom::StringAttribute::kAutoComplete;
    case blink::WebAOMStringAttribute::AOM_ATTR_KEY_SHORTCUTS:
      return ax::mojom::StringAttribute::kKeyShortcuts;
    case blink::WebAOMStringAttribute::AOM_ATTR_NAME:
      return ax::mojom::StringAttribute::kName;
    case blink::WebAOMStringAttribute::AOM_ATTR_PLACEHOLDER:
      return ax::mojom::StringAttribute::kPlaceholder;
    case blink::WebAOMStringAttribute::AOM_ATTR_ROLE_DESCRIPTION:
      return ax::mojom::StringAttribute::kRoleDescription;
    case blink::WebAOMStringAttribute::AOM_ATTR_VALUE_TEXT:
      return ax::mojom::StringAttribute::kValue;
    default:
      return ax::mojom::StringAttribute::kNone;
  }
}

ax::mojom::Restriction GetCorrespondingRestrictionFlag(
    blink::WebAOMBoolAttribute attr) {
  switch (attr) {
    case blink::WebAOMBoolAttribute::AOM_ATTR_DISABLED:
      return ax::mojom::Restriction::kDisabled;
    case blink::WebAOMBoolAttribute::AOM_ATTR_READONLY:
      return ax::mojom::Restriction::kReadOnly;
    default:
      return ax::mojom::Restriction::kNone;
  }
}

ax::mojom::State GetCorrespondingStateFlag(blink::WebAOMBoolAttribute attr) {
  switch (attr) {
    case blink::WebAOMBoolAttribute::AOM_ATTR_EXPANDED:
      return ax::mojom::State::kExpanded;
    case blink::WebAOMBoolAttribute::AOM_ATTR_MULTILINE:
      return ax::mojom::State::kMultiline;
    case blink::WebAOMBoolAttribute::AOM_ATTR_MULTISELECTABLE:
      return ax::mojom::State::kMultiselectable;
    case blink::WebAOMBoolAttribute::AOM_ATTR_REQUIRED:
      return ax::mojom::State::kRequired;
    default:
      return ax::mojom::State::kNone;
  }
}

}  // namespace

namespace content {

AomContentAxTree::AomContentAxTree(RenderFrameImpl* render_frame)
    : render_frame_(render_frame) {}

bool AomContentAxTree::ComputeAccessibilityTree() {
  ui::AXTreeUpdate tree_update;
  AXTreeSnapshotterImpl snapshotter(render_frame_, ui::kAXModeComplete);
  snapshotter.Snapshot(/* max_node_count= */ 0,
                       /* timeout= */ {}, &tree_update);
  // Computed AOM trees/updates do not use a tree id.
  DCHECK(tree_.data().tree_id == ui::AXTreeIDUnknown());
  tree_update.tree_data.tree_id = ui::AXTreeIDUnknown();
  CHECK(tree_.Unserialize(tree_update)) << tree_.error();
  DCHECK(tree_.data().tree_id == ui::AXTreeIDUnknown());
  return true;
}

bool AomContentAxTree::GetBoolAttributeForAXNode(
    int32_t ax_id,
    blink::WebAOMBoolAttribute attr,
    bool* out_param) {
  ui::AXNode* node = tree_.GetFromId(ax_id);
  if (!node)
    return false;
  if (GetCorrespondingRestrictionFlag(attr) != ax::mojom::Restriction::kNone) {
    return GetRestrictionAttributeForAXNode(ax_id, attr, out_param);
  } else if (GetCorrespondingStateFlag(attr) != ax::mojom::State::kNone) {
    return GetStateAttributeForAXNode(ax_id, attr, out_param);
  }

  ax::mojom::BoolAttribute ax_attr = GetCorrespondingAXAttribute(attr);
  return node->GetBoolAttribute(ax_attr, out_param);
}

bool AomContentAxTree::GetCheckedStateForAXNode(int32_t ax_id,
                                                blink::WebString* out_param) {
  ui::AXNode* node = tree_.GetFromId(ax_id);
  if (!node)
    return false;
  ax::mojom::CheckedState checked_state = static_cast<ax::mojom::CheckedState>(
      node->GetIntAttribute(ax::mojom::IntAttribute::kCheckedState));
  *out_param = blink::WebString::FromUTF8(ui::ToString(checked_state));
  return true;
}

bool AomContentAxTree::GetIntAttributeForAXNode(int32_t ax_id,
                                                blink::WebAOMIntAttribute attr,
                                                int32_t* out_param) {
  ui::AXNode* node = tree_.GetFromId(ax_id);
  if (!node)
    return false;
  ax::mojom::IntAttribute ax_attr = GetCorrespondingAXAttribute(attr);
  return node->GetIntAttribute(ax_attr, out_param);
}

bool AomContentAxTree::GetRestrictionAttributeForAXNode(
    int32_t ax_id,
    blink::WebAOMBoolAttribute attr,
    bool* out_param) {
  ui::AXNode* node = tree_.GetFromId(ax_id);
  if (!node)
    return false;

  // Disabled and readOnly are stored on the node data as an int attribute,
  // which indicates which type of kRestriction applies.
  ax::mojom::Restriction restriction = static_cast<ax::mojom::Restriction>(
      node->GetIntAttribute(ax::mojom::IntAttribute::kRestriction));
  ax::mojom::Restriction ax_attr = GetCorrespondingRestrictionFlag(attr);
  *out_param = (restriction == ax_attr);
  return true;
}

bool AomContentAxTree::GetFloatAttributeForAXNode(
    int32_t ax_id,
    blink::WebAOMFloatAttribute attr,
    float* out_param) {
  ui::AXNode* node = tree_.GetFromId(ax_id);
  if (!node)
    return false;
  ax::mojom::FloatAttribute ax_attr = GetCorrespondingAXAttribute(attr);
  return node->GetFloatAttribute(ax_attr, out_param);
}

bool AomContentAxTree::GetStateAttributeForAXNode(
    int32_t ax_id,
    blink::WebAOMBoolAttribute attr,
    bool* out_param) {
  ui::AXNode* node = tree_.GetFromId(ax_id);
  if (!node)
    return false;

  *out_param = node->HasState(GetCorrespondingStateFlag(attr));
  return true;
}

bool AomContentAxTree::GetStringAttributeForAXNode(
    int32_t ax_id,
    blink::WebAOMStringAttribute attr,
    blink::WebString* out_param) {
  ui::AXNode* node = tree_.GetFromId(ax_id);
  std::string out_string;

  if (node && node->GetStringAttribute(GetCorrespondingAXAttribute(attr),
                                       &out_string)) {
    *out_param = blink::WebString::FromUTF8(out_string.c_str());
    return true;
  }

  return false;
}

bool AomContentAxTree::GetRoleForAXNode(int32_t ax_id,
                                        blink::WebString* out_param) {
  ui::AXNode* node = tree_.GetFromId(ax_id);
  if (!node)
    return false;
  *out_param = blink::WebString::FromUTF8(ui::ToString(node->GetRole()));
  return true;
}

bool AomContentAxTree::GetParentIdForAXNode(int32_t ax_id, int32_t* out_param) {
  ui::AXNode* node = tree_.GetFromId(ax_id);
  if (!node || !node->parent())
    return false;
  *out_param = node->parent()->id();
  return true;
}

bool AomContentAxTree::GetFirstChildIdForAXNode(int32_t ax_id,
                                                int32_t* out_param) {
  ui::AXNode* node = tree_.GetFromId(ax_id);
  if (!node || node->children().empty())
    return false;

  ui::AXNode* child = node->children().front();
  DCHECK(child);
  *out_param = child->id();
  return true;
}

bool AomContentAxTree::GetLastChildIdForAXNode(int32_t ax_id,
                                               int32_t* out_param) {
  ui::AXNode* node = tree_.GetFromId(ax_id);
  if (!node || node->children().empty())
    return false;

  ui::AXNode* child = node->children().back();
  DCHECK(child);
  *out_param = child->id();
  return true;
}

bool AomContentAxTree::GetPreviousSiblingIdForAXNode(int32_t ax_id,
                                                     int32_t* out_param) {
  ui::AXNode* node = tree_.GetFromId(ax_id);
  if (!node)
    return false;
  size_t index_in_parent = node->index_in_parent();

  // Assumption: only when this node is the first child, does it not have a
  // previous sibling.
  if (index_in_parent == 0)
    return false;

  ui::AXNode* sibling = node->parent()->children()[index_in_parent - 1];
  DCHECK(sibling);
  *out_param = sibling->id();
  return true;
}

bool AomContentAxTree::GetNextSiblingIdForAXNode(int32_t ax_id,
                                                 int32_t* out_param) {
  ui::AXNode* node = tree_.GetFromId(ax_id);
  if (!node)
    return false;
  size_t next_index_in_parent = node->index_in_parent() + 1;

  // Assumption: When this node is the last child, it does not have a next
  // sibling.
  if (next_index_in_parent == node->parent()->children().size())
    return false;

  ui::AXNode* sibling = node->parent()->children()[next_index_in_parent];
  DCHECK(sibling);
  *out_param = sibling->id();
  return true;
}

}  // namespace content