1/*
2 * Copyright (C) 2003, 2004, 2005, 2006, 2008, 2012 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#import "config.h"
27#import "AXObjectCache.h"
28
29#if HAVE(ACCESSIBILITY)
30
31#import "AccessibilityObject.h"
32#import "RenderObject.h"
33#import "WebAccessibilityObjectWrapperMac.h"
34#import "WebCoreSystemInterface.h"
35
36#import <wtf/PassRefPtr.h>
37
38#ifndef NSAccessibilityLiveRegionChangedNotification
39#define NSAccessibilityLiveRegionChangedNotification @"AXLiveRegionChanged"
40#endif
41
42#ifndef NSAccessibilityLiveRegionCreatedNotification
43#define NSAccessibilityLiveRegionCreatedNotification @"AXLiveRegionCreated"
44#endif
45
46// The simple Cocoa calls in this file don't throw exceptions.
47
48namespace WebCore {
49
50void AXObjectCache::detachWrapper(AccessibilityObject* obj, AccessibilityDetachmentType)
51{
52    [obj->wrapper() detach];
53    obj->setWrapper(0);
54}
55
56void AXObjectCache::attachWrapper(AccessibilityObject* obj)
57{
58    RetainPtr<WebAccessibilityObjectWrapper> wrapper = adoptNS([[WebAccessibilityObjectWrapper alloc] initWithAccessibilityObject:obj]);
59    obj->setWrapper(wrapper.get());
60}
61
62void AXObjectCache::postPlatformNotification(AccessibilityObject* obj, AXNotification notification)
63{
64    if (!obj)
65        return;
66
67    // Some notifications are unique to Safari and do not have NSAccessibility equivalents.
68    NSString *macNotification;
69    switch (notification) {
70        case AXActiveDescendantChanged:
71            // An active descendant change for trees means a selected rows change.
72            if (obj->isTree())
73                macNotification = NSAccessibilitySelectedRowsChangedNotification;
74
75            // When a combobox uses active descendant, it means the selected item in its associated
76            // list has changed. In these cases we should use selected children changed, because
77            // we don't want the focus to change away from the combobox where the user is typing.
78            else if (obj->isComboBox())
79                macNotification = NSAccessibilitySelectedChildrenChangedNotification;
80            else
81                macNotification = NSAccessibilityFocusedUIElementChangedNotification;
82            break;
83        case AXAutocorrectionOccured:
84            macNotification = @"AXAutocorrectionOccurred";
85            break;
86        case AXFocusedUIElementChanged:
87            macNotification = NSAccessibilityFocusedUIElementChangedNotification;
88            break;
89        case AXLayoutComplete:
90            macNotification = @"AXLayoutComplete";
91            break;
92        case AXLoadComplete:
93            macNotification = @"AXLoadComplete";
94            break;
95        case AXInvalidStatusChanged:
96            macNotification = @"AXInvalidStatusChanged";
97            break;
98        case AXSelectedChildrenChanged:
99            if (obj->isAccessibilityTable())
100                macNotification = NSAccessibilitySelectedRowsChangedNotification;
101            else
102                macNotification = NSAccessibilitySelectedChildrenChangedNotification;
103            break;
104        case AXSelectedTextChanged:
105            macNotification = NSAccessibilitySelectedTextChangedNotification;
106            break;
107        case AXValueChanged:
108            macNotification = NSAccessibilityValueChangedNotification;
109            break;
110        case AXLiveRegionCreated:
111            macNotification = NSAccessibilityLiveRegionCreatedNotification;
112            break;
113        case AXLiveRegionChanged:
114            macNotification = NSAccessibilityLiveRegionChangedNotification;
115            break;
116        case AXRowCountChanged:
117            macNotification = NSAccessibilityRowCountChangedNotification;
118            break;
119        case AXRowExpanded:
120            macNotification = NSAccessibilityRowExpandedNotification;
121            break;
122        case AXRowCollapsed:
123            macNotification = NSAccessibilityRowCollapsedNotification;
124            break;
125        case AXElementBusyChanged:
126            macNotification = @"AXElementBusyChanged";
127            break;
128        case AXExpandedChanged:
129            macNotification = @"AXExpandedChanged";
130            break;
131        case AXMenuClosed:
132            macNotification = (id)kAXMenuClosedNotification;
133            break;
134        case AXMenuListItemSelected:
135            macNotification = (id)kAXMenuItemSelectedNotification;
136            break;
137        case AXMenuOpened:
138            macNotification = (id)kAXMenuOpenedNotification;
139            break;
140        case AXCheckedStateChanged:
141            // Does not exist on Mac.
142        default:
143            return;
144    }
145
146    // NSAccessibilityPostNotification will call this method, (but not when running DRT), so ASSERT here to make sure it does not crash.
147    // https://bugs.webkit.org/show_bug.cgi?id=46662
148#pragma clang diagnostic push
149#pragma clang diagnostic ignored "-Wdeprecated-declarations"
150    ASSERT([obj->wrapper() accessibilityIsIgnored] || true);
151#pragma clang diagnostic pop
152
153    NSAccessibilityPostNotification(obj->wrapper(), macNotification);
154
155    // Used by DRT to know when notifications are posted.
156    [obj->wrapper() accessibilityPostedNotification:macNotification];
157}
158
159void AXObjectCache::nodeTextChangePlatformNotification(AccessibilityObject*, AXTextChange, unsigned, const String&)
160{
161}
162
163void AXObjectCache::frameLoadingEventPlatformNotification(AccessibilityObject*, AXLoadingEvent)
164{
165}
166
167void AXObjectCache::platformHandleFocusedUIElementChanged(Node*, Node*)
168{
169    wkAccessibilityHandleFocusChanged();
170}
171
172void AXObjectCache::handleScrolledToAnchor(const Node*)
173{
174}
175
176}
177
178#endif // HAVE(ACCESSIBILITY)
179