Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / browser / accessibility / accessibility_tree_formatter_utils_win.cc
blob9dd9e196c93eb25c5e8bc8c272f08b95475a63a2
1 // Copyright (c) 2012 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.
5 #include "content/browser/accessibility/accessibility_tree_formatter_utils_win.h"
7 #include <oleacc.h>
9 #include <map>
10 #include <string>
12 #include "base/memory/singleton.h"
13 #include "base/strings/string_util.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "third_party/iaccessible2/ia2_api_all.h"
17 namespace content {
18 namespace {
20 class AccessibilityEnumMap {
21 public:
22 static AccessibilityEnumMap* GetInstance();
24 std::map<int32, base::string16> ia_role_string_map;
25 std::map<int32, base::string16> ia2_role_string_map;
26 std::map<int32, base::string16> ia_state_string_map;
27 std::map<int32, base::string16> ia2_state_string_map;
28 std::map<int32, base::string16> event_string_map;
30 private:
31 AccessibilityEnumMap();
32 virtual ~AccessibilityEnumMap() {}
34 friend struct base::DefaultSingletonTraits<AccessibilityEnumMap>;
36 DISALLOW_COPY_AND_ASSIGN(AccessibilityEnumMap);
39 // static
40 AccessibilityEnumMap* AccessibilityEnumMap::GetInstance() {
41 return base::Singleton<
42 AccessibilityEnumMap,
43 base::LeakySingletonTraits<AccessibilityEnumMap>>::get();
46 AccessibilityEnumMap::AccessibilityEnumMap() {
47 // Convenience macros for generating readable strings.
48 #define IA_ROLE_MAP(x) ia_role_string_map[x] = L###x; \
49 ia2_role_string_map[x] = L###x;
50 #define IA2_ROLE_MAP(x) ia2_role_string_map[x] = L###x;
51 #define IA_STATE_MAP(x) ia_state_string_map[STATE_SYSTEM_##x] = L###x;
52 #define IA2_STATE_MAP(x) ia2_state_string_map[x] = L###x;
53 #define EVENT_MAP(x) event_string_map[x] = L###x;
55 // MSAA / IAccessible roles. Each one of these is also a valid
56 // IAccessible2 role, the IA_ROLE_MAP macro adds it to both.
57 IA_ROLE_MAP(ROLE_S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117 IA_ROLE_MAP(ROLE_SYSTEM_TOOLBAR)
118 IA_ROLE_MAP(ROLE_SYSTEM_TOOLTIP)
119 IA_ROLE_MAP(ROLE_SYSTEM_WHITESPACE)
120 IA_ROLE_MAP(ROLE_SYSTEM_WINDOW)
122 // IAccessible2 roles.
123 IA2_ROLE_MAP(IA2_ROLE_CANVAS)
124 IA2_ROLE_MAP(IA2_ROLE_CAPTION)
125 IA2_ROLE_MAP(IA2_ROLE_CHECK_MENU_ITEM)
126 IA2_ROLE_MAP(IA2_ROLE_COLOR_CHOOSER)
127 IA2_ROLE_MAP(IA2_ROLE_DATE_EDITOR)
128 IA2_ROLE_MAP(IA2_ROLE_DESKTOP_ICON)
129 IA2_ROLE_MAP(IA2_ROLE_DESKTOP_PANE)
130 IA2_ROLE_MAP(IA2_ROLE_DIRECTORY_PANE)
131 IA2_ROLE_MAP(IA2_ROLE_EDITBAR)
132 IA2_ROLE_MAP(IA2_ROLE_EMBEDDED_OBJECT)
133 IA2_ROLE_MAP(IA2_ROLE_ENDNOTE)
134 IA2_ROLE_MAP(IA2_ROLE_FILE_CHOOSER)
135 IA2_ROLE_MAP(IA2_ROLE_FONT_CHOOSER)
136 IA2_ROLE_MAP(IA2_ROLE_FOOTER)
137 IA2_ROLE_MAP(IA2_ROLE_FOOTNOTE)
138 IA2_ROLE_MAP(IA2_ROLE_FORM)
139 IA2_ROLE_MAP(IA2_ROLE_FRAME)
140 IA2_ROLE_MAP(IA2_ROLE_GLASS_PANE)
141 IA2_ROLE_MAP(IA2_ROLE_HEADER)
142 IA2_ROLE_MAP(IA2_ROLE_HEADING)
143 IA2_ROLE_MAP(IA2_ROLE_ICON)
144 IA2_ROLE_MAP(IA2_ROLE_IMAGE_MAP)
145 IA2_ROLE_MAP(IA2_ROLE_INPUT_METHOD_WINDOW)
146 IA2_ROLE_MAP(IA2_ROLE_INTERNAL_FRAME)
147 IA2_ROLE_MAP(IA2_ROLE_LABEL)
148 IA2_ROLE_MAP(IA2_ROLE_LAYERED_PANE)
149 IA2_ROLE_MAP(IA2_ROLE_NOTE)
150 IA2_ROLE_MAP(IA2_ROLE_OPTION_PANE)
151 IA2_ROLE_MAP(IA2_ROLE_PAGE)
152 IA2_ROLE_MAP(IA2_ROLE_PARAGRAPH)
153 IA2_ROLE_MAP(IA2_ROLE_RADIO_MENU_ITEM)
154 IA2_ROLE_MAP(IA2_ROLE_REDUNDANT_OBJECT)
155 IA2_ROLE_MAP(IA2_ROLE_ROOT_PANE)
156 IA2_ROLE_MAP(IA2_ROLE_RULER)
157 IA2_ROLE_MAP(IA2_ROLE_SCROLL_PANE)
158 IA2_ROLE_MAP(IA2_ROLE_SECTION)
159 IA2_ROLE_MAP(IA2_ROLE_SHAPE)
160 IA2_ROLE_MAP(IA2_ROLE_SPLIT_PANE)
161 IA2_ROLE_MAP(IA2_ROLE_TEAR_OFF_MENU)
162 IA2_ROLE_MAP(IA2_ROLE_TERMINAL)
163 IA2_ROLE_MAP(IA2_ROLE_TEXT_FRAME)
164 IA2_ROLE_MAP(IA2_ROLE_TOGGLE_BUTTON)
165 IA2_ROLE_MAP(IA2_ROLE_UNKNOWN)
166 IA2_ROLE_MAP(IA2_ROLE_VIEW_PORT)
168 // MSAA / IAccessible states. Unlike roles, these are not also IA2 states.
169 IA_STATE_MAP(ALERT_HIGH)
170 IA_STATE_MAP(ALERT_LOW)
171 IA_STATE_MAP(ALERT_MEDIUM)
172 IA_STATE_MAP(ANIMATED)
173 IA_STATE_MAP(BUSY)
174 IA_STATE_MAP(CHECKED)
175 IA_STATE_MAP(COLLAPSED)
176 IA_STATE_MAP(DEFAULT)
177 IA_STATE_MAP(EXPANDED)
178 IA_STATE_MAP(EXTSELECTABLE)
179 IA_STATE_MAP(FLOATING)
180 IA_STATE_MAP(FOCUSABLE)
181 IA_STATE_MAP(FOCUSED)
182 IA_STATE_MAP(HASPOPUP)
183 IA_STATE_MAP(HOTTRACKED)
184 IA_STATE_MAP(INVISIBLE)
185 IA_STATE_MAP(LINKED)
186 IA_STATE_MAP(MARQUEED)
187 IA_STATE_MAP(MIXED)
188 IA_STATE_MAP(MOVEABLE)
189 IA_STATE_MAP(MULTISELECTABLE)
190 IA_STATE_MAP(OFFSCREEN)
191 IA_STATE_MAP(PRESSED)
192 IA_STATE_MAP(PROTECTED)
193 IA_STATE_MAP(READONLY)
194 IA_STATE_MAP(SELECTABLE)
195 IA_STATE_MAP(SELECTED)
196 IA_STATE_MAP(SELFVOICING)
197 IA_STATE_MAP(SIZEABLE)
198 IA_STATE_MAP(TRAVERSED)
199 IA_STATE_MAP(UNAVAILABLE)
201 // IAccessible2 states.
202 IA2_STATE_MAP(IA2_STATE_ACTIVE)
203 IA2_STATE_MAP(IA2_STATE_ARMED)
204 IA2_STATE_MAP(IA2_STATE_CHECKABLE)
205 IA2_STATE_MAP(IA2_STATE_DEFUNCT)
206 IA2_STATE_MAP(IA2_STATE_EDITABLE)
207 IA2_STATE_MAP(IA2_STATE_HORIZONTAL)
208 IA2_STATE_MAP(IA2_STATE_ICONIFIED)
209 IA2_STATE_MAP(IA2_STATE_INVALID_ENTRY)
210 IA2_STATE_MAP(IA2_STATE_MANAGES_DESCENDANTS)
211 IA2_STATE_MAP(IA2_STATE_MODAL)
212 IA2_STATE_MAP(IA2_STATE_MULTI_LINE)
213 IA2_STATE_MAP(IA2_STATE_REQUIRED)
214 IA2_STATE_MAP(IA2_STATE_SELECTABLE_TEXT)
215 IA2_STATE_MAP(IA2_STATE_SINGLE_LINE)
216 IA2_STATE_MAP(IA2_STATE_STALE)
217 IA2_STATE_MAP(IA2_STATE_SUPPORTS_AUTOCOMPLETION)
218 IA2_STATE_MAP(IA2_STATE_TRANSIENT)
219 IA2_STATE_MAP(IA2_STATE_VERTICAL)
221 // Untested states include those that would be repeated on nearly every node,
222 // or would vary based on window size.
223 // IA2_STATE_MAP(IA2_STATE_OPAQUE) // Untested.
225 EVENT_MAP(EVENT_OBJECT_CREATE)
226 EVENT_MAP(EVENT_OBJECT_DESTROY)
227 EVENT_MAP(EVENT_OBJECT_SHOW)
228 EVENT_MAP(EVENT_OBJECT_HIDE)
229 EVENT_MAP(EVENT_OBJECT_REORDER)
230 EVENT_MAP(EVENT_OBJECT_FOCUS)
231 EVENT_MAP(EVENT_OBJECT_SELECTION)
232 EVENT_MAP(EVENT_OBJECT_SELECTIONADD)
233 EVENT_MAP(EVENT_OBJECT_SELECTIONREMOVE)
234 EVENT_MAP(EVENT_OBJECT_SELECTIONWITHIN)
235 EVENT_MAP(EVENT_OBJECT_STATECHANGE)
236 EVENT_MAP(EVENT_OBJECT_LOCATIONCHANGE)
237 EVENT_MAP(EVENT_OBJECT_NAMECHANGE)
238 EVENT_MAP(EVENT_OBJECT_DESCRIPTIONCHANGE)
239 EVENT_MAP(EVENT_OBJECT_VALUECHANGE)
240 EVENT_MAP(EVENT_OBJECT_PARENTCHANGE)
241 EVENT_MAP(EVENT_OBJECT_HELPCHANGE)
242 EVENT_MAP(EVENT_OBJECT_DEFACTIONCHANGE)
243 EVENT_MAP(EVENT_OBJECT_ACCELERATORCHANGE)
244 EVENT_MAP(EVENT_OBJECT_INVOKED)
245 EVENT_MAP(EVENT_OBJECT_TEXTSELECTIONCHANGED)
246 EVENT_MAP(EVENT_OBJECT_CONTENTSCROLLED)
247 EVENT_MAP(EVENT_OBJECT_LIVEREGIONCHANGED)
248 EVENT_MAP(EVENT_OBJECT_HOSTEDOBJECTSINVALIDATED)
249 EVENT_MAP(EVENT_OBJECT_DRAGSTART)
250 EVENT_MAP(EVENT_OBJECT_DRAGCANCEL)
251 EVENT_MAP(EVENT_OBJECT_DRAGCOMPLETE)
252 EVENT_MAP(EVENT_OBJECT_DRAGENTER)
253 EVENT_MAP(EVENT_OBJECT_DRAGLEAVE)
254 EVENT_MAP(EVENT_OBJECT_DRAGDROPPED)
255 EVENT_MAP(EVENT_SYSTEM_ALERT)
256 EVENT_MAP(EVENT_SYSTEM_SCROLLINGSTART)
257 EVENT_MAP(EVENT_SYSTEM_SCROLLINGEND)
258 EVENT_MAP(IA2_EVENT_ACTION_CHANGED)
259 EVENT_MAP(IA2_EVENT_ACTIVE_DECENDENT_CHANGED)
260 EVENT_MAP(IA2_EVENT_ACTIVE_DESCENDANT_CHANGED)
261 EVENT_MAP(IA2_EVENT_DOCUMENT_ATTRIBUTE_CHANGED)
262 EVENT_MAP(IA2_EVENT_DOCUMENT_CONTENT_CHANGED)
263 EVENT_MAP(IA2_EVENT_DOCUMENT_LOAD_COMPLETE)
264 EVENT_MAP(IA2_EVENT_DOCUMENT_LOAD_STOPPED)
265 EVENT_MAP(IA2_EVENT_DOCUMENT_RELOAD)
266 EVENT_MAP(IA2_EVENT_HYPERLINK_END_INDEX_CHANGED)
267 EVENT_MAP(IA2_EVENT_HYPERLINK_NUMBER_OF_ANCHORS_CHANGED)
268 EVENT_MAP(IA2_EVENT_HYPERLINK_SELECTED_LINK_CHANGED)
269 EVENT_MAP(IA2_EVENT_HYPERTEXT_LINK_ACTIVATED)
270 EVENT_MAP(IA2_EVENT_HYPERTEXT_LINK_SELECTED)
271 EVENT_MAP(IA2_EVENT_HYPERLINK_START_INDEX_CHANGED)
272 EVENT_MAP(IA2_EVENT_HYPERTEXT_CHANGED)
273 EVENT_MAP(IA2_EVENT_HYPERTEXT_NLINKS_CHANGED)
274 EVENT_MAP(IA2_EVENT_OBJECT_ATTRIBUTE_CHANGED)
275 EVENT_MAP(IA2_EVENT_PAGE_CHANGED)
276 EVENT_MAP(IA2_EVENT_SECTION_CHANGED)
277 EVENT_MAP(IA2_EVENT_TABLE_CAPTION_CHANGED)
278 EVENT_MAP(IA2_EVENT_TABLE_COLUMN_DESCRIPTION_CHANGED)
279 EVENT_MAP(IA2_EVENT_TABLE_COLUMN_HEADER_CHANGED)
280 EVENT_MAP(IA2_EVENT_TABLE_MODEL_CHANGED)
281 EVENT_MAP(IA2_EVENT_TABLE_ROW_DESCRIPTION_CHANGED)
282 EVENT_MAP(IA2_EVENT_TABLE_ROW_HEADER_CHANGED)
283 EVENT_MAP(IA2_EVENT_TABLE_SUMMARY_CHANGED)
284 EVENT_MAP(IA2_EVENT_TEXT_ATTRIBUTE_CHANGED)
285 EVENT_MAP(IA2_EVENT_TEXT_CARET_MOVED)
286 EVENT_MAP(IA2_EVENT_TEXT_CHANGED)
287 EVENT_MAP(IA2_EVENT_TEXT_COLUMN_CHANGED)
288 EVENT_MAP(IA2_EVENT_TEXT_INSERTED)
289 EVENT_MAP(IA2_EVENT_TEXT_REMOVED)
290 EVENT_MAP(IA2_EVENT_TEXT_UPDATED)
291 EVENT_MAP(IA2_EVENT_TEXT_SELECTION_CHANGED)
292 EVENT_MAP(IA2_EVENT_VISIBLE_DATA_CHANGED)
295 } // namespace.
297 base::string16 IAccessibleRoleToString(int32 ia_role) {
298 return AccessibilityEnumMap::GetInstance()->ia_role_string_map[ia_role];
301 base::string16 IAccessible2RoleToString(int32 ia_role) {
302 return AccessibilityEnumMap::GetInstance()->ia2_role_string_map[ia_role];
305 void IAccessibleStateToStringVector(int32 ia_state,
306 std::vector<base::string16>* result) {
307 const std::map<int32, base::string16>& state_string_map =
308 AccessibilityEnumMap::GetInstance()->ia_state_string_map;
309 std::map<int32, base::string16>::const_iterator it;
310 for (it = state_string_map.begin(); it != state_string_map.end(); ++it) {
311 if (it->first & ia_state)
312 result->push_back(it->second);
316 base::string16 IAccessibleStateToString(int32 ia_state) {
317 std::vector<base::string16> strings;
318 IAccessibleStateToStringVector(ia_state, &strings);
319 return base::JoinString(strings, base::ASCIIToUTF16(","));
322 void IAccessible2StateToStringVector(int32 ia2_state,
323 std::vector<base::string16>* result) {
324 const std::map<int32, base::string16>& state_string_map =
325 AccessibilityEnumMap::GetInstance()->ia2_state_string_map;
326 std::map<int32, base::string16>::const_iterator it;
327 for (it = state_string_map.begin(); it != state_string_map.end(); ++it) {
328 if (it->first & ia2_state)
329 result->push_back(it->second);
333 base::string16 IAccessible2StateToString(int32 ia2_state) {
334 std::vector<base::string16> strings;
335 IAccessible2StateToStringVector(ia2_state, &strings);
336 return base::JoinString(strings, base::ASCIIToUTF16(","));
339 base::string16 AccessibilityEventToString(int32 event_id) {
340 return AccessibilityEnumMap::GetInstance()->event_string_map[event_id];
343 } // namespace content