1 // Copyright 2014 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 #import "ui/accessibility/platform/ax_platform_node_mac.h"
7 #import <Cocoa/Cocoa.h>
9 #include "base/strings/sys_string_conversions.h"
10 #import "ui/accessibility/ax_node_data.h"
11 #import "ui/accessibility/platform/ax_platform_node_delegate.h"
12 #import "ui/gfx/mac/coordinate_conversion.h"
18 NSString* nativeValue;
21 typedef std::map<ui::AXRole, NSString*> RoleMap;
23 RoleMap BuildRoleMap() {
24 const MapEntry roles[] = {
25 {ui::AX_ROLE_ALERT, NSAccessibilityGroupRole},
26 {ui::AX_ROLE_ALERT_DIALOG, NSAccessibilityGroupRole},
27 {ui::AX_ROLE_ANNOTATION, NSAccessibilityUnknownRole},
28 {ui::AX_ROLE_APPLICATION, NSAccessibilityGroupRole},
29 {ui::AX_ROLE_ARTICLE, NSAccessibilityGroupRole},
30 {ui::AX_ROLE_BANNER, NSAccessibilityGroupRole},
31 {ui::AX_ROLE_BLOCKQUOTE, NSAccessibilityGroupRole},
32 {ui::AX_ROLE_BUSY_INDICATOR, NSAccessibilityBusyIndicatorRole},
33 {ui::AX_ROLE_BUTTON, NSAccessibilityButtonRole},
34 {ui::AX_ROLE_CANVAS, NSAccessibilityImageRole},
35 {ui::AX_ROLE_CAPTION, NSAccessibilityGroupRole},
36 {ui::AX_ROLE_CELL, @"AXCell"},
37 {ui::AX_ROLE_CHECK_BOX, NSAccessibilityCheckBoxRole},
38 {ui::AX_ROLE_COLOR_WELL, NSAccessibilityColorWellRole},
39 {ui::AX_ROLE_COLUMN, NSAccessibilityColumnRole},
40 {ui::AX_ROLE_COLUMN_HEADER, @"AXCell"},
41 {ui::AX_ROLE_COMBO_BOX, NSAccessibilityComboBoxRole},
42 {ui::AX_ROLE_COMPLEMENTARY, NSAccessibilityGroupRole},
43 {ui::AX_ROLE_CONTENT_INFO, NSAccessibilityGroupRole},
44 {ui::AX_ROLE_DATE, @"AXDateField"},
45 {ui::AX_ROLE_DATE_TIME, NSAccessibilityTextFieldRole},
46 {ui::AX_ROLE_DEFINITION, NSAccessibilityGroupRole},
47 {ui::AX_ROLE_DESCRIPTION_LIST_DETAIL, NSAccessibilityGroupRole},
48 {ui::AX_ROLE_DESCRIPTION_LIST, NSAccessibilityListRole},
49 {ui::AX_ROLE_DESCRIPTION_LIST_TERM, NSAccessibilityGroupRole},
50 {ui::AX_ROLE_DIALOG, NSAccessibilityGroupRole},
51 {ui::AX_ROLE_DETAILS, NSAccessibilityGroupRole},
52 {ui::AX_ROLE_DIRECTORY, NSAccessibilityListRole},
53 {ui::AX_ROLE_DISCLOSURE_TRIANGLE, NSAccessibilityDisclosureTriangleRole},
54 {ui::AX_ROLE_DIV, NSAccessibilityGroupRole},
55 {ui::AX_ROLE_DOCUMENT, NSAccessibilityGroupRole},
56 {ui::AX_ROLE_FIGCAPTION, NSAccessibilityGroupRole},
57 {ui::AX_ROLE_FIGURE, NSAccessibilityGroupRole},
58 {ui::AX_ROLE_FOOTER, NSAccessibilityGroupRole},
59 {ui::AX_ROLE_FORM, NSAccessibilityGroupRole},
60 {ui::AX_ROLE_GRID, NSAccessibilityGridRole},
61 {ui::AX_ROLE_GROUP, NSAccessibilityGroupRole},
62 {ui::AX_ROLE_HEADING, @"AXHeading"},
63 {ui::AX_ROLE_IFRAME, NSAccessibilityGroupRole},
64 {ui::AX_ROLE_IFRAME_PRESENTATIONAL, NSAccessibilityGroupRole},
65 {ui::AX_ROLE_IGNORED, NSAccessibilityUnknownRole},
66 {ui::AX_ROLE_IMAGE, NSAccessibilityImageRole},
67 {ui::AX_ROLE_IMAGE_MAP, NSAccessibilityGroupRole},
68 {ui::AX_ROLE_IMAGE_MAP_LINK, NSAccessibilityLinkRole},
69 {ui::AX_ROLE_LABEL_TEXT, NSAccessibilityGroupRole},
70 {ui::AX_ROLE_LEGEND, NSAccessibilityGroupRole},
71 {ui::AX_ROLE_LINK, NSAccessibilityLinkRole},
72 {ui::AX_ROLE_LIST, NSAccessibilityListRole},
73 {ui::AX_ROLE_LIST_BOX, NSAccessibilityListRole},
74 {ui::AX_ROLE_LIST_BOX_OPTION, NSAccessibilityStaticTextRole},
75 {ui::AX_ROLE_LIST_ITEM, NSAccessibilityGroupRole},
76 {ui::AX_ROLE_LIST_MARKER, @"AXListMarker"},
77 {ui::AX_ROLE_LOG, NSAccessibilityGroupRole},
78 {ui::AX_ROLE_MAIN, NSAccessibilityGroupRole},
79 {ui::AX_ROLE_MARQUEE, NSAccessibilityGroupRole},
80 {ui::AX_ROLE_MATH, NSAccessibilityGroupRole},
81 {ui::AX_ROLE_MENU, NSAccessibilityMenuRole},
82 {ui::AX_ROLE_MENU_BAR, NSAccessibilityMenuBarRole},
83 {ui::AX_ROLE_MENU_BUTTON, NSAccessibilityButtonRole},
84 {ui::AX_ROLE_MENU_ITEM, NSAccessibilityMenuItemRole},
85 {ui::AX_ROLE_MENU_ITEM_CHECK_BOX, NSAccessibilityMenuItemRole},
86 {ui::AX_ROLE_MENU_ITEM_RADIO, NSAccessibilityMenuItemRole},
87 {ui::AX_ROLE_MENU_LIST_OPTION, NSAccessibilityMenuItemRole},
88 {ui::AX_ROLE_MENU_LIST_POPUP, NSAccessibilityUnknownRole},
89 {ui::AX_ROLE_METER, NSAccessibilityProgressIndicatorRole},
90 {ui::AX_ROLE_NAVIGATION, NSAccessibilityGroupRole},
91 {ui::AX_ROLE_NONE, NSAccessibilityGroupRole},
92 {ui::AX_ROLE_NOTE, NSAccessibilityGroupRole},
93 {ui::AX_ROLE_OUTLINE, NSAccessibilityOutlineRole},
94 {ui::AX_ROLE_PARAGRAPH, NSAccessibilityGroupRole},
95 {ui::AX_ROLE_POP_UP_BUTTON, NSAccessibilityPopUpButtonRole},
96 {ui::AX_ROLE_PRE, NSAccessibilityGroupRole},
97 {ui::AX_ROLE_PRESENTATIONAL, NSAccessibilityGroupRole},
98 {ui::AX_ROLE_PROGRESS_INDICATOR, NSAccessibilityProgressIndicatorRole},
99 {ui::AX_ROLE_RADIO_BUTTON, NSAccessibilityRadioButtonRole},
100 {ui::AX_ROLE_RADIO_GROUP, NSAccessibilityRadioGroupRole},
101 {ui::AX_ROLE_REGION, NSAccessibilityGroupRole},
102 {ui::AX_ROLE_ROOT_WEB_AREA, @"AXWebArea"},
103 {ui::AX_ROLE_ROW, NSAccessibilityRowRole},
104 {ui::AX_ROLE_ROW_HEADER, @"AXCell"},
105 {ui::AX_ROLE_RULER, NSAccessibilityRulerRole},
106 {ui::AX_ROLE_SCROLL_BAR, NSAccessibilityScrollBarRole},
107 {ui::AX_ROLE_SEARCH, NSAccessibilityGroupRole},
108 {ui::AX_ROLE_SEARCH_BOX, NSAccessibilityTextFieldRole},
109 {ui::AX_ROLE_SLIDER, NSAccessibilitySliderRole},
110 {ui::AX_ROLE_SLIDER_THUMB, NSAccessibilityValueIndicatorRole},
111 {ui::AX_ROLE_SPIN_BUTTON, NSAccessibilityIncrementorRole},
112 {ui::AX_ROLE_SPLITTER, NSAccessibilitySplitterRole},
113 {ui::AX_ROLE_STATIC_TEXT, NSAccessibilityStaticTextRole},
114 {ui::AX_ROLE_STATUS, NSAccessibilityGroupRole},
115 {ui::AX_ROLE_SVG_ROOT, NSAccessibilityGroupRole},
116 {ui::AX_ROLE_SWITCH, NSAccessibilityCheckBoxRole},
117 {ui::AX_ROLE_TAB, NSAccessibilityRadioButtonRole},
118 {ui::AX_ROLE_TABLE, NSAccessibilityTableRole},
119 {ui::AX_ROLE_TABLE_HEADER_CONTAINER, NSAccessibilityGroupRole},
120 {ui::AX_ROLE_TAB_LIST, NSAccessibilityTabGroupRole},
121 {ui::AX_ROLE_TAB_PANEL, NSAccessibilityGroupRole},
122 {ui::AX_ROLE_TEXT_FIELD, NSAccessibilityTextFieldRole},
123 {ui::AX_ROLE_TIME, @"AXTimeField"},
124 {ui::AX_ROLE_TIMER, NSAccessibilityGroupRole},
125 {ui::AX_ROLE_TOGGLE_BUTTON, NSAccessibilityCheckBoxRole},
126 {ui::AX_ROLE_TOOLBAR, NSAccessibilityToolbarRole},
127 {ui::AX_ROLE_TOOLTIP, NSAccessibilityGroupRole},
128 {ui::AX_ROLE_TREE, NSAccessibilityOutlineRole},
129 {ui::AX_ROLE_TREE_GRID, NSAccessibilityTableRole},
130 {ui::AX_ROLE_TREE_ITEM, NSAccessibilityRowRole},
131 {ui::AX_ROLE_WEB_AREA, @"AXWebArea"},
132 {ui::AX_ROLE_WINDOW, NSAccessibilityWindowRole},
134 // TODO(dtseng): we don't correctly support the attributes for these
136 // { ui::AX_ROLE_SCROLL_AREA, NSAccessibilityScrollAreaRole },
140 for (size_t i = 0; i < arraysize(roles); ++i)
141 role_map[roles[i].value] = roles[i].nativeValue;
145 RoleMap BuildSubroleMap() {
146 const MapEntry subroles[] = {
147 {ui::AX_ROLE_ALERT, @"AXApplicationAlert"},
148 {ui::AX_ROLE_ALERT_DIALOG, @"AXApplicationAlertDialog"},
149 {ui::AX_ROLE_APPLICATION, @"AXLandmarkApplication"},
150 {ui::AX_ROLE_ARTICLE, @"AXDocumentArticle"},
151 {ui::AX_ROLE_BANNER, @"AXLandmarkBanner"},
152 {ui::AX_ROLE_COMPLEMENTARY, @"AXLandmarkComplementary"},
153 {ui::AX_ROLE_CONTENT_INFO, @"AXLandmarkContentInfo"},
154 {ui::AX_ROLE_DEFINITION, @"AXDefinition"},
155 {ui::AX_ROLE_DESCRIPTION_LIST_DETAIL, @"AXDefinition"},
156 {ui::AX_ROLE_DESCRIPTION_LIST_TERM, @"AXTerm"},
157 {ui::AX_ROLE_DIALOG, @"AXApplicationDialog"},
158 {ui::AX_ROLE_DOCUMENT, @"AXDocument"},
159 {ui::AX_ROLE_FOOTER, @"AXLandmarkContentInfo"},
160 {ui::AX_ROLE_FORM, @"AXLandmarkForm"},
161 {ui::AX_ROLE_LOG, @"AXApplicationLog"},
162 {ui::AX_ROLE_MAIN, @"AXLandmarkMain"},
163 {ui::AX_ROLE_MARQUEE, @"AXApplicationMarquee"},
164 {ui::AX_ROLE_MATH, @"AXDocumentMath"},
165 {ui::AX_ROLE_NAVIGATION, @"AXLandmarkNavigation"},
166 {ui::AX_ROLE_NOTE, @"AXDocumentNote"},
167 {ui::AX_ROLE_REGION, @"AXDocumentRegion"},
168 {ui::AX_ROLE_SEARCH, @"AXLandmarkSearch"},
169 {ui::AX_ROLE_SEARCH_BOX, @"AXSearchField"},
170 {ui::AX_ROLE_STATUS, @"AXApplicationStatus"},
171 {ui::AX_ROLE_SWITCH, @"AXSwitch"},
172 {ui::AX_ROLE_TAB_PANEL, @"AXTabPanel"},
173 {ui::AX_ROLE_TIMER, @"AXApplicationTimer"},
174 {ui::AX_ROLE_TOGGLE_BUTTON, @"AXToggleButton"},
175 {ui::AX_ROLE_TOOLTIP, @"AXUserInterfaceTooltip"},
176 {ui::AX_ROLE_TREE_ITEM, NSAccessibilityOutlineRowSubrole},
180 for (size_t i = 0; i < arraysize(subroles); ++i)
181 subrole_map[subroles[i].value] = subroles[i].nativeValue;
187 @implementation AXPlatformNodeCocoa
189 // A mapping of AX roles to native roles.
190 + (NSString*)nativeRoleFromAXRole:(ui::AXRole)role {
191 CR_DEFINE_STATIC_LOCAL(RoleMap, role_map, (BuildRoleMap()));
192 RoleMap::iterator it = role_map.find(role);
193 return it != role_map.end() ? it->second : NSAccessibilityUnknownRole;
196 // A mapping of AX roles to native subroles.
197 + (NSString*)nativeSubroleFromAXRole:(ui::AXRole)role {
198 CR_DEFINE_STATIC_LOCAL(RoleMap, subrole_map, (BuildSubroleMap()));
199 RoleMap::iterator it = subrole_map.find(role);
200 return it != subrole_map.end() ? it->second : nil;
203 - (instancetype)initWithNode:(ui::AXPlatformNodeBase*)node {
204 if ((self = [super init])) {
214 - (NSRect)boundsInScreen {
217 return gfx::ScreenRectToNSRect(node_->GetBoundsInScreen());
220 - (NSArray*)AXChildren {
223 int count = node_->GetChildCount();
224 NSMutableArray* children = [NSMutableArray arrayWithCapacity:count];
225 for (int i = 0; i < count; ++i)
226 [children addObject:node_->ChildAtIndex(i)];
227 return NSAccessibilityUnignoredChildren(children);
233 return NSAccessibilityUnignoredAncestor(node_->GetParent());
236 - (NSValue*)AXPosition {
237 return [NSValue valueWithPoint:self.boundsInScreen.origin];
240 - (NSString*)AXRole {
243 return [[self class] nativeRoleFromAXRole:node_->GetData().role];
247 return [NSValue valueWithSize:self.boundsInScreen.size];
250 - (NSString*)AXTitle {
252 if (node_->GetStringAttribute(ui::AX_ATTR_NAME, &value))
253 return base::SysUTF8ToNSString(value);
257 // NSAccessibility informal protocol implementation.
259 - (BOOL)accessibilityIsIgnored {
260 return [[self AXRole] isEqualToString:NSAccessibilityUnknownRole];
263 - (id)accessibilityHitTest:(NSPoint)point {
264 for (AXPlatformNodeCocoa* child in [self AXChildren]) {
265 if (NSPointInRect(point, child.boundsInScreen))
266 return [child accessibilityHitTest:point];
268 return NSAccessibilityUnignoredAncestor(self);
271 - (NSArray*)accessibilityActionNames {
275 - (NSArray*)accessibilityAttributeNames {
276 // These attributes are required on all accessibility objects.
278 NSAccessibilityChildrenAttribute,
279 NSAccessibilityParentAttribute,
280 NSAccessibilityPositionAttribute,
281 NSAccessibilityRoleAttribute,
282 NSAccessibilitySizeAttribute,
284 // Title is required for most elements. Cocoa asks for the value even if it
285 // is omitted here, but won't present it to accessibility APIs without this.
286 NSAccessibilityTitleAttribute,
288 // TODO(tapted): Add additional attributes based on role.
291 - (BOOL)accessibilityIsAttributeSettable:(NSString*)attribute {
295 - (id)accessibilityAttributeValue:(NSString*)attribute {
296 SEL selector = NSSelectorFromString(attribute);
297 if ([self respondsToSelector:selector])
298 return [self performSelector:selector];
307 AXPlatformNode* AXPlatformNode::Create(AXPlatformNodeDelegate* delegate) {
308 AXPlatformNodeBase* node = new AXPlatformNodeMac();
309 node->Init(delegate);
313 AXPlatformNodeMac::AXPlatformNodeMac() {
316 AXPlatformNodeMac::~AXPlatformNodeMac() {
319 void AXPlatformNodeMac::Destroy() {
321 [native_node_ detach];
326 gfx::NativeViewAccessible AXPlatformNodeMac::GetNativeViewAccessible() {
328 native_node_.reset([[AXPlatformNodeCocoa alloc] initWithNode:this]);
329 return native_node_.get();
332 void AXPlatformNodeMac::NotifyAccessibilityEvent(ui::AXEvent event_type) {
333 // TODO(dmazzoni): implement this. http://crbug.com/396137
336 int AXPlatformNodeMac::GetIndexInParent() {
337 // TODO(dmazzoni): implement this. http://crbug.com/396137