forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ax_node_data.h
365 lines (304 loc) · 15.9 KB
/
ax_node_data.h
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// Copyright 2013 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_AX_NODE_DATA_H_
#define UI_ACCESSIBILITY_AX_NODE_DATA_H_
#include <stdint.h>
#include <map>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "base/strings/string_split.h"
#include "ui/accessibility/ax_base_export.h"
#include "ui/accessibility/ax_enums.mojom-forward.h"
#include "ui/accessibility/ax_node_id_forward.h"
#include "ui/accessibility/ax_relative_bounds.h"
#include "ui/accessibility/ax_text_attributes.h"
#include "ui/gfx/geometry/rect_f.h"
namespace ui {
class AXTreeID;
// Return true if |attr| should be interpreted as the id of another node
// in the same tree.
AX_BASE_EXPORT bool IsNodeIdIntAttribute(ax::mojom::IntAttribute attr);
// Return true if |attr| should be interpreted as a list of ids of
// nodes in the same tree.
AX_BASE_EXPORT bool IsNodeIdIntListAttribute(ax::mojom::IntListAttribute attr);
// A compact representation of the accessibility information for a
// single accessible object, in a form that can be serialized and sent from
// one process to another.
struct AX_BASE_EXPORT AXNodeData {
// Defines the type used for AXNode IDs.
using AXID = AXNodeID;
// If a node is not yet or no longer valid, its ID should have a value of
// kInvalidAXID.
static constexpr AXID kInvalidAXID = kInvalidAXNodeID;
AXNodeData();
virtual ~AXNodeData();
AXNodeData(const AXNodeData& other);
AXNodeData(AXNodeData&& other);
AXNodeData& operator=(const AXNodeData& other);
// Accessing accessibility attributes:
//
// There are dozens of possible attributes for an accessibility node,
// but only a few tend to apply to any one object, so we store them
// in sparse arrays of <attribute id, attribute value> pairs, organized
// by type (bool, int, float, string, int list).
//
// There are three accessors for each type of attribute: one that returns
// true if the attribute is present and false if not, one that takes a
// pointer argument and returns true if the attribute is present (if you
// need to distinguish between the default value and a missing attribute),
// and another that returns the default value for that type if the
// attribute is not present. In addition, strings can be returned as
// either std::string or std::u16string, for convenience.
bool HasBoolAttribute(ax::mojom::BoolAttribute attribute) const;
bool GetBoolAttribute(ax::mojom::BoolAttribute attribute) const;
bool GetBoolAttribute(ax::mojom::BoolAttribute attribute, bool* value) const;
bool HasFloatAttribute(ax::mojom::FloatAttribute attribute) const;
float GetFloatAttribute(ax::mojom::FloatAttribute attribute) const;
bool GetFloatAttribute(ax::mojom::FloatAttribute attribute,
float* value) const;
bool HasIntAttribute(ax::mojom::IntAttribute attribute) const;
int GetIntAttribute(ax::mojom::IntAttribute attribute) const;
bool GetIntAttribute(ax::mojom::IntAttribute attribute, int* value) const;
bool HasStringAttribute(ax::mojom::StringAttribute attribute) const;
const std::string& GetStringAttribute(
ax::mojom::StringAttribute attribute) const;
bool GetStringAttribute(ax::mojom::StringAttribute attribute,
std::string* value) const;
std::u16string GetString16Attribute(
ax::mojom::StringAttribute attribute) const;
bool GetString16Attribute(ax::mojom::StringAttribute attribute,
std::u16string* value) const;
bool HasIntListAttribute(ax::mojom::IntListAttribute attribute) const;
const std::vector<int32_t>& GetIntListAttribute(
ax::mojom::IntListAttribute attribute) const;
bool GetIntListAttribute(ax::mojom::IntListAttribute attribute,
std::vector<int32_t>* value) const;
bool HasStringListAttribute(ax::mojom::StringListAttribute attribute) const;
const std::vector<std::string>& GetStringListAttribute(
ax::mojom::StringListAttribute attribute) const;
bool GetStringListAttribute(ax::mojom::StringListAttribute attribute,
std::vector<std::string>* value) const;
bool HasHtmlAttribute(const char* attribute) const;
bool GetHtmlAttribute(const char* attribute, std::string* value) const;
std::u16string GetHtmlAttribute(const char* attribute) const;
bool GetHtmlAttribute(const char* attribute, std::u16string* value) const;
//
// Setting accessibility attributes.
//
// Replaces an attribute if present. This is safer than crashing via a DCHECK
// or doing nothing, because most likely replacing is what the caller would
// have wanted or what existing code already assumes.
//
void AddBoolAttribute(ax::mojom::BoolAttribute attribute, bool value);
void AddChildTreeId(const ui::AXTreeID& tree_id);
void AddIntAttribute(ax::mojom::IntAttribute attribute, int32_t value);
void AddFloatAttribute(ax::mojom::FloatAttribute attribute, float value);
// This method cannot be used to set kChildTreeId due to a common
// misuse of base::UnguessableToken serialization. Use AddChildTreeId instead.
void AddStringAttribute(ax::mojom::StringAttribute attribute,
const std::string& value);
void AddIntListAttribute(ax::mojom::IntListAttribute attribute,
const std::vector<int32_t>& value);
void AddStringListAttribute(ax::mojom::StringListAttribute attribute,
const std::vector<std::string>& value);
//
// Removing accessibility attributes.
//
void RemoveBoolAttribute(ax::mojom::BoolAttribute attribute);
void RemoveIntAttribute(ax::mojom::IntAttribute attribute);
void RemoveFloatAttribute(ax::mojom::FloatAttribute attribute);
void RemoveStringAttribute(ax::mojom::StringAttribute attribute);
void RemoveIntListAttribute(ax::mojom::IntListAttribute attribute);
void RemoveStringListAttribute(ax::mojom::StringListAttribute attribute);
//
// Text attributes, such as spelling markers and style information.
//
AXTextAttributes GetTextAttributes() const;
//
// Convenience functions.
//
// Adds the name attribute or replaces it if already present. Also sets the
// NameFrom attribute if not already set.
//
// [[deprecated("Replaced by `SetNameChecked` and `SetNameExplicitlyEmpty`")]]
// See `SetNameChecked` and `SetNameExplicitlyEmpty` which have DCHECKs for
// conditions expected to be true, which in reality are not always true.
// Tracked by crbug.com/1348081.
void SetName(const std::string& name);
// [[deprecated("Replaced by `SetNameChecked` and `SetNameExplicitlyEmpty`")]]
// See `SetNameChecked` and `SetNameExplicitlyEmpty` which have DCHECKs for
// conditions expected to be true, which in reality are not always true.
// Tracked by crbug.com/1348081.
void SetName(const std::u16string& name);
// Adds the accessible name attribute or replaces it if already present, and
// also sets the NameFrom attribute if not already set.
//
// The value of the accessible name is a localized, end-user-consumable string
// which may be derived from visible information (e.g. the text on a button)
// or invisible information (e.g. the alternative text describing an icon).
// In the case of focusable objects, the name will be presented by the screen
// reader when that object gains focus and is critical to understanding the
// purpose of that object non-visually.
//
// Note that `SetNameChecked` must only be used to set a non-empty name, a
// condition enforced by a DCHECK. This is done to prevent UI from
// accidentally being given an empty name because, as a general rule, nameless
// controls tend to be inaccessible. However, because there can be valid
// reasons to remove or prevent naming of an item `SetNameExplicitlyEmpty`
// provides a means for developers to do so.
void SetNameChecked(const std::string& name);
void SetNameChecked(const std::u16string& name);
// Indicates this object should not have an accessible name. One use case is
// to prevent screen readers from speaking redundant information, for instance
// if the parent View has the same name as this View, causing the screen
// reader to speak the name twice. This function can also be used to allow
// focusable nameless objects to pass accessibility checks in tests, a
// practice that should not be applied in production code.
void SetNameExplicitlyEmpty();
// Adds the description attribute or replaces it if already present.
void SetDescription(const std::string& description);
void SetDescription(const std::u16string& description);
// Adds the value attribute or replaces it if already present.
void SetValue(const std::string& value);
void SetValue(const std::u16string& value);
// Returns true if the given enum bit is 1.
bool HasState(ax::mojom::State state) const;
bool HasAction(ax::mojom::Action action) const;
bool HasTextStyle(ax::mojom::TextStyle text_style) const;
// aria-dropeffect is deprecated in WAI-ARIA 1.1.
bool HasDropeffect(ax::mojom::Dropeffect dropeffect) const;
// Set or remove bits in the given enum's corresponding bitfield.
void AddState(ax::mojom::State state);
void RemoveState(ax::mojom::State state);
void AddAction(ax::mojom::Action action);
void AddTextStyle(ax::mojom::TextStyle text_style);
// aria-dropeffect is deprecated in WAI-ARIA 1.1.
void AddDropeffect(ax::mojom::Dropeffect dropeffect);
// Helper functions to get or set some common int attributes with some
// specific enum types. To remove an attribute, set it to None.
//
// Please keep in alphabetic order.
ax::mojom::CheckedState GetCheckedState() const;
void SetCheckedState(ax::mojom::CheckedState checked_state);
bool HasCheckedState() const;
ax::mojom::DefaultActionVerb GetDefaultActionVerb() const;
void SetDefaultActionVerb(ax::mojom::DefaultActionVerb default_action_verb);
ax::mojom::HasPopup GetHasPopup() const;
void SetHasPopup(ax::mojom::HasPopup has_popup);
ax::mojom::InvalidState GetInvalidState() const;
void SetInvalidState(ax::mojom::InvalidState invalid_state);
ax::mojom::NameFrom GetNameFrom() const;
void SetNameFrom(ax::mojom::NameFrom name_from);
ax::mojom::DescriptionFrom GetDescriptionFrom() const;
void SetDescriptionFrom(ax::mojom::DescriptionFrom description_from);
ax::mojom::TextPosition GetTextPosition() const;
void SetTextPosition(ax::mojom::TextPosition text_position);
ax::mojom::Restriction GetRestriction() const;
void SetRestriction(ax::mojom::Restriction restriction);
ax::mojom::ListStyle GetListStyle() const;
void SetListStyle(ax::mojom::ListStyle list_style);
ax::mojom::TextAlign GetTextAlign() const;
void SetTextAlign(ax::mojom::TextAlign text_align);
ax::mojom::WritingDirection GetTextDirection() const;
void SetTextDirection(ax::mojom::WritingDirection text_direction);
ax::mojom::ImageAnnotationStatus GetImageAnnotationStatus() const;
void SetImageAnnotationStatus(ax::mojom::ImageAnnotationStatus status);
// Helper to determine if the data belongs to a node that gains focus when
// clicked, such as a text field or a native HTML list box.
bool IsActivatable() const;
// Helper to determine if the data belongs to a node that is at the root of an
// ARIA live region that is active, i.e. its status is not set to "off".
bool IsActiveLiveRegionRoot() const;
// Helper to determine if the data belongs to a node that is a native button
// or ARIA role="button" in a pressed state.
bool IsButtonPressed() const;
// Helper to determine if the data belongs to a node that can respond to
// clicks.
bool IsClickable() const;
// Helper to determine if the data belongs to a node that is part of an active
// ARIA live region, and for which live announcements should be made.
bool IsContainedInActiveLiveRegion() const;
// Helper to determine if the object is selectable.
bool IsSelectable() const;
// Helper to determine if the data has the ignored state or ignored role.
bool IsIgnored() const;
// Helper to determine if the data has the invisible state.
bool IsInvisible() const;
// Helper to determine if the data has the ignored state, the invisible state
// or the ignored role.
bool IsInvisibleOrIgnored() const;
// Helper to determine if the data belongs to a node that is invocable.
bool IsInvocable() const;
// Helper to determine if the data belongs to a node that is a menu button.
bool IsMenuButton() const;
// This data belongs to a text field. This is any widget in which the user
// should be able to enter and edit text.
//
// Examples include <input type="text">, <input type="password">, <textarea>,
// <div contenteditable="true">, <div role="textbox">, <div role="searchbox">
// and <div role="combobox">. Note that when an ARIA role that indicates that
// the widget is editable is used, such as "role=textbox", the element doesn't
// need to be contenteditable for this method to return true, as in theory
// JavaScript could be used to implement editing functionality. In practice,
// this situation should be rare.
bool IsTextField() const;
// This data belongs to a text field that is used for entering passwords.
bool IsPasswordField() const;
// This data belongs to an atomic text field. An atomic text field does not
// expose its internal implementation to assistive software, appearing as a
// single leaf node in the accessibility tree. Examples include: An <input> or
// a <textarea> on the Web, a text field in a PDF form, a Views-based text
// field, or a native Android one.
bool IsAtomicTextField() const;
// This data belongs to a text field whose value is exposed both on the field
// itself as well as on descendant nodes which are expose to platform
// accessibility APIs. A non-native text field also exposes stylistic and
// document marker information on descendant nodes. Examples include fields
// created using the CSS "user-modify" property, or the "contenteditable"
// attribute.
bool IsNonAtomicTextField() const;
// Some spinners are text fields, and some are not. For example, an ordinary
// <input type="number"> allows caret movement and behaves like a textfield,
// but the <input type="number"> used inside date, datetime, datetime-local,
// month, time, and week types does not allow this. In either type, pressing
// up/down arrow will change the value to the previous/next allowed value.
bool IsSpinnerTextField() const;
// Helper to determine if the data belongs to a node that supports
// range-based values.
bool IsRangeValueSupported() const;
// Helper to determine if the data belongs to a node that supports
// expand/collapse.
bool SupportsExpandCollapse() const;
// Return a string representation of this data, for debugging.
virtual std::string ToString() const;
// Returns the approximate size in bytes.
size_t ByteSize() const;
// Return a string representation of |aria-dropeffect| values, for testing
// and debugging.
// aria-dropeffect is deprecated in WAI-ARIA 1.1.
std::string DropeffectBitfieldToString() const;
// As much as possible this should behave as a simple, serializable,
// copyable struct.
AXNodeID id = kInvalidAXNodeID;
ax::mojom::Role role;
uint32_t state = 0U;
uint64_t actions = 0ULL;
std::vector<std::pair<ax::mojom::StringAttribute, std::string>>
string_attributes;
std::vector<std::pair<ax::mojom::IntAttribute, int32_t>> int_attributes;
std::vector<std::pair<ax::mojom::FloatAttribute, float>> float_attributes;
std::vector<std::pair<ax::mojom::BoolAttribute, bool>> bool_attributes;
std::vector<std::pair<ax::mojom::IntListAttribute, std::vector<int32_t>>>
intlist_attributes;
std::vector<
std::pair<ax::mojom::StringListAttribute, std::vector<std::string>>>
stringlist_attributes;
base::StringPairs html_attributes;
std::vector<int32_t> child_ids;
AXRelativeBounds relative_bounds;
};
} // namespace ui
#endif // UI_ACCESSIBILITY_AX_NODE_DATA_H_