[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_NODE_DATA_H_ |
| 6 | #define UI_ACCESSIBILITY_AX_NODE_DATA_H_ |
| 7 | |
avi | 9c81217b | 2015-12-24 23:40:05 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 10 | #include <map> |
dcheng | 18ec0b54 | 2016-04-26 19:28:53 | [diff] [blame] | 11 | #include <memory> |
[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 12 | #include <string> |
| 13 | #include <vector> |
| 14 | |
| 15 | #include "base/strings/string16.h" |
anand.ratn | a85c48c | 2014-09-23 16:50:21 | [diff] [blame] | 16 | #include "base/strings/string_split.h" |
[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 17 | #include "ui/accessibility/ax_enums.h" |
| 18 | #include "ui/accessibility/ax_export.h" |
dmazzoni | 7535c4d8 | 2016-07-21 22:12:39 | [diff] [blame] | 19 | #include "ui/gfx/geometry/rect_f.h" |
[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 20 | |
dmazzoni | 0aec386 | 2016-03-28 19:06:56 | [diff] [blame] | 21 | namespace gfx { |
| 22 | class Transform; |
| 23 | }; |
| 24 | |
[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 25 | namespace ui { |
| 26 | |
dmazzoni | ca0b1e8 | 2017-02-10 03:59:09 | [diff] [blame] | 27 | // Return true if |attr| should be interpreted as the id of another node |
| 28 | // in the same tree. |
| 29 | AX_EXPORT bool IsNodeIdIntAttribute(AXIntAttribute attr); |
| 30 | |
| 31 | // Return true if |attr| should be interpreted as a list of ids of |
| 32 | // nodes in the same tree. |
| 33 | AX_EXPORT bool IsNodeIdIntListAttribute(AXIntListAttribute attr); |
| 34 | |
[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 35 | // A compact representation of the accessibility information for a |
dmazzoni | 329fd01 | 2015-10-22 20:05:35 | [diff] [blame] | 36 | // single accessible object, in a form that can be serialized and sent from |
[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 37 | // one process to another. |
| 38 | struct AX_EXPORT AXNodeData { |
[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 39 | AXNodeData(); |
| 40 | virtual ~AXNodeData(); |
| 41 | |
dmazzoni | 729443ae | 2016-02-29 21:17:12 | [diff] [blame] | 42 | AXNodeData(const AXNodeData& other); |
| 43 | AXNodeData& operator=(AXNodeData other); |
| 44 | |
dmazzoni | 53e922ab | 2015-06-03 23:02:25 | [diff] [blame] | 45 | // Accessing accessibility attributes: |
| 46 | // |
| 47 | // There are dozens of possible attributes for an accessibility node, |
| 48 | // but only a few tend to apply to any one object, so we store them |
| 49 | // in sparse arrays of <attribute id, attribute value> pairs, organized |
| 50 | // by type (bool, int, float, string, int list). |
| 51 | // |
| 52 | // There are three accessors for each type of attribute: one that returns |
| 53 | // true if the attribute is present and false if not, one that takes a |
| 54 | // pointer argument and returns true if the attribute is present (if you |
| 55 | // need to distinguish between the default value and a missing attribute), |
| 56 | // and another that returns the default value for that type if the |
| 57 | // attribute is not present. In addition, strings can be returned as |
| 58 | // either std::string or base::string16, for convenience. |
| 59 | |
| 60 | bool HasBoolAttribute(AXBoolAttribute attr) const; |
| 61 | bool GetBoolAttribute(AXBoolAttribute attr) const; |
| 62 | bool GetBoolAttribute(AXBoolAttribute attr, bool* value) const; |
| 63 | |
| 64 | bool HasFloatAttribute(AXFloatAttribute attr) const; |
| 65 | float GetFloatAttribute(AXFloatAttribute attr) const; |
| 66 | bool GetFloatAttribute(AXFloatAttribute attr, float* value) const; |
| 67 | |
| 68 | bool HasIntAttribute(AXIntAttribute attribute) const; |
| 69 | int GetIntAttribute(AXIntAttribute attribute) const; |
| 70 | bool GetIntAttribute(AXIntAttribute attribute, int* value) const; |
| 71 | |
| 72 | bool HasStringAttribute( |
| 73 | AXStringAttribute attribute) const; |
| 74 | const std::string& GetStringAttribute(AXStringAttribute attribute) const; |
| 75 | bool GetStringAttribute(AXStringAttribute attribute, |
| 76 | std::string* value) const; |
| 77 | |
| 78 | bool GetString16Attribute(AXStringAttribute attribute, |
| 79 | base::string16* value) const; |
| 80 | base::string16 GetString16Attribute( |
| 81 | AXStringAttribute attribute) const; |
| 82 | |
| 83 | bool HasIntListAttribute(AXIntListAttribute attribute) const; |
avi | 9c81217b | 2015-12-24 23:40:05 | [diff] [blame] | 84 | const std::vector<int32_t>& GetIntListAttribute( |
dmazzoni | 53e922ab | 2015-06-03 23:02:25 | [diff] [blame] | 85 | AXIntListAttribute attribute) const; |
| 86 | bool GetIntListAttribute(AXIntListAttribute attribute, |
avi | 9c81217b | 2015-12-24 23:40:05 | [diff] [blame] | 87 | std::vector<int32_t>* value) const; |
dmazzoni | 53e922ab | 2015-06-03 23:02:25 | [diff] [blame] | 88 | |
yawano | b87aed4 | 2017-06-26 02:55:29 | [diff] [blame] | 89 | bool HasStringListAttribute(AXStringListAttribute attribute) const; |
| 90 | const std::vector<std::string>& GetStringListAttribute( |
| 91 | AXStringListAttribute attribute) const; |
| 92 | bool GetStringListAttribute(AXStringListAttribute attribute, |
| 93 | std::vector<std::string>* value) const; |
| 94 | |
dmazzoni | 53e922ab | 2015-06-03 23:02:25 | [diff] [blame] | 95 | bool GetHtmlAttribute(const char* attr, base::string16* value) const; |
| 96 | bool GetHtmlAttribute(const char* attr, std::string* value) const; |
| 97 | |
| 98 | // Setting accessibility attributes. |
[email protected] | 5eec2f5 | 2014-01-06 22:30:54 | [diff] [blame] | 99 | void AddStringAttribute(AXStringAttribute attribute, |
[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 100 | const std::string& value); |
[email protected] | 5eec2f5 | 2014-01-06 22:30:54 | [diff] [blame] | 101 | void AddIntAttribute(AXIntAttribute attribute, int value); |
| 102 | void AddFloatAttribute(AXFloatAttribute attribute, float value); |
| 103 | void AddBoolAttribute(AXBoolAttribute attribute, bool value); |
| 104 | void AddIntListAttribute(AXIntListAttribute attribute, |
avi | 9c81217b | 2015-12-24 23:40:05 | [diff] [blame] | 105 | const std::vector<int32_t>& value); |
yawano | b87aed4 | 2017-06-26 02:55:29 | [diff] [blame] | 106 | void AddStringListAttribute(AXStringListAttribute attribute, |
| 107 | const std::vector<std::string>& value); |
[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 108 | |
| 109 | // Convenience functions, mainly for writing unit tests. |
| 110 | // Equivalent to AddStringAttribute(ATTR_NAME, name). |
ki.stfu | e0013b8 | 2015-09-23 18:14:53 | [diff] [blame] | 111 | void SetName(const std::string& name); |
dmazzoni | 13e5258 | 2016-11-04 07:24:21 | [diff] [blame] | 112 | void SetName(const base::string16& name); |
[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 113 | // Equivalent to AddStringAttribute(ATTR_VALUE, value). |
ki.stfu | e0013b8 | 2015-09-23 18:14:53 | [diff] [blame] | 114 | void SetValue(const std::string& value); |
dmazzoni | 13e5258 | 2016-11-04 07:24:21 | [diff] [blame] | 115 | void SetValue(const base::string16& value); |
| 116 | |
patricialor | 979beed | 2017-05-22 06:24:22 | [diff] [blame] | 117 | // Returns true if the given enum bit is 1. |
| 118 | bool HasState(ui::AXState state_enum) const; |
| 119 | bool HasAction(ui::AXAction state_enum) const; |
| 120 | |
| 121 | // Set bits in the given enum's corresponding bitfield. |
| 122 | void AddState(ui::AXState state_enum); |
| 123 | void AddAction(ui::AXAction action_enum); |
[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 124 | |
[email protected] | 5eec2f5 | 2014-01-06 22:30:54 | [diff] [blame] | 125 | // Return a string representation of this data, for debugging. |
dmazzoni | ac6cdd0 | 2015-08-04 21:07:06 | [diff] [blame] | 126 | virtual std::string ToString() const; |
| 127 | |
dmazzoni | 729443ae | 2016-02-29 21:17:12 | [diff] [blame] | 128 | // As much as possible this should behave as a simple, serializable, |
| 129 | // copyable struct. |
avi | 9c81217b | 2015-12-24 23:40:05 | [diff] [blame] | 130 | int32_t id; |
[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 131 | AXRole role; |
avi | 9c81217b | 2015-12-24 23:40:05 | [diff] [blame] | 132 | uint32_t state; |
patricialor | 979beed | 2017-05-22 06:24:22 | [diff] [blame] | 133 | uint32_t actions; |
patricialor | c32a66d | 2016-07-01 00:29:18 | [diff] [blame] | 134 | std::vector<std::pair<AXStringAttribute, std::string>> string_attributes; |
avi | 9c81217b | 2015-12-24 23:40:05 | [diff] [blame] | 135 | std::vector<std::pair<AXIntAttribute, int32_t>> int_attributes; |
patricialor | c32a66d | 2016-07-01 00:29:18 | [diff] [blame] | 136 | std::vector<std::pair<AXFloatAttribute, float>> float_attributes; |
| 137 | std::vector<std::pair<AXBoolAttribute, bool>> bool_attributes; |
avi | 9c81217b | 2015-12-24 23:40:05 | [diff] [blame] | 138 | std::vector<std::pair<AXIntListAttribute, std::vector<int32_t>>> |
[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 139 | intlist_attributes; |
yawano | b87aed4 | 2017-06-26 02:55:29 | [diff] [blame] | 140 | std::vector<std::pair<AXStringListAttribute, std::vector<std::string>>> |
| 141 | stringlist_attributes; |
anand.ratn | a85c48c | 2014-09-23 16:50:21 | [diff] [blame] | 142 | base::StringPairs html_attributes; |
avi | 9c81217b | 2015-12-24 23:40:05 | [diff] [blame] | 143 | std::vector<int32_t> child_ids; |
dmazzoni | 0aec386 | 2016-03-28 19:06:56 | [diff] [blame] | 144 | |
dmazzoni | 051715a | 2016-08-15 21:36:58 | [diff] [blame] | 145 | // TODO(dmazzoni): replace the following three members with a single |
| 146 | // instance of AXRelativeBounds. |
| 147 | |
| 148 | // The id of an ancestor node in the same AXTree that this object's |
| 149 | // bounding box is relative to, or -1 if there's no offset container. |
| 150 | int offset_container_id; |
| 151 | |
| 152 | // The relative bounding box of this node. |
dmazzoni | 7535c4d8 | 2016-07-21 22:12:39 | [diff] [blame] | 153 | gfx::RectF location; |
dmazzoni | 0aec386 | 2016-03-28 19:06:56 | [diff] [blame] | 154 | |
| 155 | // An additional transform to apply to position this object and its subtree. |
dcheng | 18ec0b54 | 2016-04-26 19:28:53 | [diff] [blame] | 156 | // NOTE: this member is a std::unique_ptr because it's rare and gfx::Transform |
dmazzoni | 0aec386 | 2016-03-28 19:06:56 | [diff] [blame] | 157 | // takes up a fair amount of space. The assignment operator and copy |
| 158 | // constructor both make a duplicate of the owned pointer, so it acts more |
| 159 | // like a member than a pointer. |
danakj | 25c52c3 | 2016-04-12 21:51:08 | [diff] [blame] | 160 | std::unique_ptr<gfx::Transform> transform; |
[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 161 | }; |
| 162 | |
| 163 | } // namespace ui |
| 164 | |
| 165 | #endif // UI_ACCESSIBILITY_AX_NODE_DATA_H_ |