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/browser_accessibility_manager_mac.h"
7 #import "base/logging.h"
8 #import "content/browser/accessibility/browser_accessibility_cocoa.h"
9 #import "content/browser/accessibility/browser_accessibility_mac.h"
10 #include "content/common/accessibility_messages.h"
15 BrowserAccessibilityManager* BrowserAccessibilityManager::Create(
16 const SimpleAXTreeUpdate& initial_tree,
17 BrowserAccessibilityDelegate* delegate,
18 BrowserAccessibilityFactory* factory) {
19 return new BrowserAccessibilityManagerMac(
20 NULL, initial_tree, delegate, factory);
23 BrowserAccessibilityManagerMac::BrowserAccessibilityManagerMac(
25 const SimpleAXTreeUpdate& initial_tree,
26 BrowserAccessibilityDelegate* delegate,
27 BrowserAccessibilityFactory* factory)
28 : BrowserAccessibilityManager(delegate, factory),
29 parent_view_(parent_view) {
30 Initialize(initial_tree);
35 BrowserAccessibilityManagerMac::GetEmptyDocument() {
36 ui::AXNodeData empty_document;
37 empty_document.id = 0;
38 empty_document.role = ui::AX_ROLE_ROOT_WEB_AREA;
39 empty_document.state =
40 1 << ui::AX_STATE_READ_ONLY;
41 SimpleAXTreeUpdate update;
42 update.nodes.push_back(empty_document);
46 BrowserAccessibility* BrowserAccessibilityManagerMac::GetFocus(
47 BrowserAccessibility* root) {
48 // On Mac, list boxes should always get focus on the whole list, otherwise
49 // information about the number of selected items will never be reported.
50 BrowserAccessibility* node = BrowserAccessibilityManager::GetFocus(root);
51 if (node && node->GetRole() == ui::AX_ROLE_LIST_BOX)
54 // For other roles, follow the active descendant.
55 return GetActiveDescendantFocus(root);
58 void BrowserAccessibilityManagerMac::NotifyAccessibilityEvent(
59 ui::AXEvent event_type,
60 BrowserAccessibility* node) {
61 if (!node->IsNative())
64 if (event_type == ui::AX_EVENT_FOCUS &&
65 node->GetRole() == ui::AX_ROLE_LIST_BOX_OPTION &&
66 node->HasState(ui::AX_STATE_SELECTED) &&
68 node->GetParent()->GetRole() == ui::AX_ROLE_LIST_BOX) {
69 node = node->GetParent();
70 SetFocus(node, false);
73 // Refer to AXObjectCache.mm (webkit).
74 NSString* event_id = @"";
76 case ui::AX_EVENT_ACTIVEDESCENDANTCHANGED:
77 if (node->GetRole() == ui::AX_ROLE_TREE) {
78 event_id = NSAccessibilitySelectedRowsChangedNotification;
80 event_id = NSAccessibilityFocusedUIElementChangedNotification;
81 BrowserAccessibility* active_descendant_focus =
82 GetActiveDescendantFocus(GetRoot());
83 if (active_descendant_focus)
84 node = active_descendant_focus;
88 case ui::AX_EVENT_ALERT:
91 case ui::AX_EVENT_BLUR:
94 case ui::AX_EVENT_CHECKED_STATE_CHANGED:
97 case ui::AX_EVENT_CHILDREN_CHANGED:
98 // TODO(dtseng): no clear equivalent on Mac.
100 case ui::AX_EVENT_FOCUS:
101 event_id = NSAccessibilityFocusedUIElementChangedNotification;
103 case ui::AX_EVENT_LAYOUT_COMPLETE:
104 event_id = @"AXLayoutComplete";
106 case ui::AX_EVENT_LIVE_REGION_CHANGED:
107 event_id = @"AXLiveRegionChanged";
109 case ui::AX_EVENT_LOAD_COMPLETE:
110 event_id = @"AXLoadComplete";
112 case ui::AX_EVENT_MENU_LIST_VALUE_CHANGED:
115 case ui::AX_EVENT_ROW_COUNT_CHANGED:
116 event_id = NSAccessibilityRowCountChangedNotification;
118 case ui::AX_EVENT_ROW_COLLAPSED:
119 event_id = @"AXRowCollapsed";
121 case ui::AX_EVENT_ROW_EXPANDED:
122 event_id = @"AXRowExpanded";
124 case ui::AX_EVENT_SCROLLED_TO_ANCHOR:
127 case ui::AX_EVENT_SELECTED_CHILDREN_CHANGED:
128 event_id = NSAccessibilitySelectedChildrenChangedNotification;
130 case ui::AX_EVENT_TEXT_SELECTION_CHANGED:
131 event_id = NSAccessibilitySelectedTextChangedNotification;
133 case ui::AX_EVENT_VALUE_CHANGED:
134 event_id = NSAccessibilityValueChangedNotification;
136 case ui::AX_EVENT_ARIA_ATTRIBUTE_CHANGED:
139 case ui::AX_EVENT_AUTOCORRECTION_OCCURED:
142 case ui::AX_EVENT_INVALID_STATUS_CHANGED:
145 case ui::AX_EVENT_LOCATION_CHANGED:
148 case ui::AX_EVENT_MENU_LIST_ITEM_SELECTED:
151 case ui::AX_EVENT_TEXT_CHANGED:
155 LOG(WARNING) << "Unknown accessibility event: " << event_type;
159 BrowserAccessibilityCocoa* native_node = node->ToBrowserAccessibilityCocoa();
161 NSAccessibilityPostNotification(native_node, event_id);
164 void BrowserAccessibilityManagerMac::OnAtomicUpdateFinished(
167 const std::vector<ui::AXTreeDelegate::Change>& changes) {
168 BrowserAccessibilityManager::OnAtomicUpdateFinished(
169 tree, root_changed, changes);
171 bool created_live_region = false;
172 for (size_t i = 0; i < changes.size(); ++i) {
173 if (changes[i].type != NODE_CREATED && changes[i].type != SUBTREE_CREATED)
175 BrowserAccessibility* obj = GetFromAXNode(changes[i].node);
176 if (obj && obj->HasStringAttribute(ui::AX_ATTR_LIVE_STATUS)) {
177 created_live_region = true;
182 if (!created_live_region)
185 // This code is to work around a bug in VoiceOver, where a new live
186 // region that gets added is ignored. VoiceOver seems to only scan the
187 // page for live regions once. By recreating the NSAccessibility
188 // object for the root of the tree, we force VoiceOver to clear out its
189 // internal state and find newly-added live regions this time.
190 BrowserAccessibilityMac* root =
191 static_cast<BrowserAccessibilityMac*>(GetRoot());
193 root->RecreateNativeObject();
194 NotifyAccessibilityEvent(ui::AX_EVENT_CHILDREN_CHANGED, root);
198 } // namespace content