Fix for crash in AppKit accessibility.
[WebKit-https.git] / Source / WebKit / WebProcess / WebPage / mac / WKAccessibilityWebPageObjectMac.mm
1 /*
2  * Copyright (C) 2011 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. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #import "config.h"
27 #import "WKAccessibilityWebPageObjectMac.h"
28
29 #if PLATFORM(MAC)
30
31 #import "ApplicationServicesSPI.h"
32 #import "PluginView.h"
33 #import "WebFrame.h"
34 #import "WebPage.h"
35 #import "WKArray.h"
36 #import "WKNumber.h"
37 #import "WKRetainPtr.h"
38 #import "WKSharedAPICast.h"
39 #import "WKString.h"
40 #import "WKStringCF.h"
41 #import <WebCore/AXObjectCache.h>
42 #import <WebCore/Frame.h>
43 #import <WebCore/FrameView.h>
44 #import <WebCore/Page.h>
45 #import <WebCore/PageOverlayController.h>
46 #import <WebCore/ScrollView.h>
47 #import <WebCore/Scrollbar.h>
48 #import <WebCore/WebAccessibilityObjectWrapperMac.h>
49 #import <pal/spi/cocoa/NSAccessibilitySPI.h>
50 #import <wtf/ObjCRuntimeExtras.h>
51
52 namespace ax = WebCore::Accessibility;
53
54 @implementation WKAccessibilityWebPageObject
55
56 #define PROTECTED_SELF protectedSelf = RetainPtr<WKAccessibilityWebPageObject>(self)
57
58 - (void)dealloc
59 {
60     NSAccessibilityUnregisterUniqueIdForUIElement(self);
61     [m_parent release];
62     [super dealloc];
63 }
64
65 ALLOW_DEPRECATED_IMPLEMENTATIONS_BEGIN
66 - (BOOL)accessibilityIsIgnored
67 ALLOW_DEPRECATED_IMPLEMENTATIONS_END
68 {
69     return NO;
70 }
71
72 ALLOW_DEPRECATED_IMPLEMENTATIONS_BEGIN
73 - (NSArray *)accessibilityAttributeNames
74 ALLOW_DEPRECATED_IMPLEMENTATIONS_END
75 {
76     if (!m_attributeNames)
77         m_attributeNames = adoptNS([[NSArray alloc] initWithObjects:
78                             NSAccessibilityRoleAttribute, NSAccessibilityRoleDescriptionAttribute, NSAccessibilityFocusedAttribute,
79                             NSAccessibilityParentAttribute, NSAccessibilityWindowAttribute, NSAccessibilityTopLevelUIElementAttribute,
80                             NSAccessibilityPositionAttribute, NSAccessibilitySizeAttribute, NSAccessibilityChildrenAttribute, NSAccessibilityPrimaryScreenHeightAttribute, nil]);
81     
82     return m_attributeNames.get();
83 }
84
85 ALLOW_DEPRECATED_IMPLEMENTATIONS_BEGIN
86 - (NSArray *)accessibilityParameterizedAttributeNames
87 ALLOW_DEPRECATED_IMPLEMENTATIONS_END
88 {
89     return ax::retrieveValueFromMainThread<RetainPtr<id>>([PROTECTED_SELF] () -> RetainPtr<id> {
90         NSMutableArray *names = [NSMutableArray array];
91         if (!protectedSelf->m_page)
92             return names;
93         
94         if (auto corePage = protectedSelf->m_page->corePage()) {
95             for (auto& name : corePage->pageOverlayController().copyAccessibilityAttributesNames(true))
96                 [names addObject:(NSString *)name];
97         }
98         return names;
99     }).autorelease();
100 }
101
102 ALLOW_DEPRECATED_IMPLEMENTATIONS_BEGIN
103 - (BOOL)accessibilityIsAttributeSettable:(NSString *)attribute
104 ALLOW_DEPRECATED_IMPLEMENTATIONS_END
105 {
106     return NO;
107 }
108
109 ALLOW_DEPRECATED_IMPLEMENTATIONS_BEGIN
110 - (void)accessibilitySetValue:(id)value forAttribute:(NSString *)attribute
111 ALLOW_DEPRECATED_IMPLEMENTATIONS_END
112 {
113 }
114
115 - (NSPoint)convertScreenPointToRootView:(NSPoint)point
116 {
117     return ax::retrieveValueFromMainThread<NSPoint>([&point, PROTECTED_SELF] () -> NSPoint {
118         if (!protectedSelf->m_page)
119             return point;
120         return protectedSelf->m_page->screenToRootView(WebCore::IntPoint(point.x, point.y));
121     });
122 }
123
124 ALLOW_DEPRECATED_IMPLEMENTATIONS_BEGIN
125 - (NSArray *)accessibilityActionNames
126 ALLOW_DEPRECATED_IMPLEMENTATIONS_END
127 {
128     return [NSArray array];
129 }
130
131 - (NSArray *)accessibilityChildren
132 {
133     id wrapper = [self accessibilityRootObjectWrapper];
134     if (!wrapper)
135         return [NSArray array];
136     
137     return [NSArray arrayWithObject:wrapper];
138 }
139
140 ALLOW_DEPRECATED_IMPLEMENTATIONS_BEGIN
141 - (id)accessibilityAttributeValue:(NSString *)attribute
142 ALLOW_DEPRECATED_IMPLEMENTATIONS_END
143 {
144     if (!WebCore::AXObjectCache::accessibilityEnabled())
145         WebCore::AXObjectCache::enableAccessibility();
146     
147     if ([attribute isEqualToString:NSAccessibilityParentAttribute])
148         return m_parent;
149     
150     if ([attribute isEqualToString:NSAccessibilityWindowAttribute])
151         return [m_parent accessibilityAttributeValue:NSAccessibilityWindowAttribute];
152     
153     if ([attribute isEqualToString:NSAccessibilityTopLevelUIElementAttribute])
154         return [m_parent accessibilityAttributeValue:NSAccessibilityTopLevelUIElementAttribute];
155     
156     if ([attribute isEqualToString:NSAccessibilityRoleAttribute])
157         return NSAccessibilityGroupRole;
158     
159     if ([attribute isEqualToString:NSAccessibilityRoleDescriptionAttribute])
160         return NSAccessibilityRoleDescription(NSAccessibilityGroupRole, nil);
161     
162     if ([attribute isEqualToString:NSAccessibilityFocusedAttribute])
163         return @NO;
164     
165     if (!m_pageID)
166         return nil;
167     
168     if ([attribute isEqualToString:NSAccessibilityPositionAttribute])
169         return [self accessibilityAttributePositionValue];
170     
171     if ([attribute isEqualToString:NSAccessibilityPrimaryScreenHeightAttribute])
172         return [[self accessibilityRootObjectWrapper] accessibilityAttributeValue:attribute];
173     
174     if ([attribute isEqualToString:NSAccessibilitySizeAttribute])
175         return [self accessibilityAttributeSizeValue];
176     
177     if ([attribute isEqualToString:NSAccessibilityChildrenAttribute])
178         return [self accessibilityChildren];
179     
180     return nil;
181 }
182
183 - (NSValue *)accessibilityAttributeSizeValue
184 {
185     return ax::retrieveValueFromMainThread<RetainPtr<id>>([PROTECTED_SELF] () -> RetainPtr<id> {
186         if (!protectedSelf->m_page)
187             return nil;
188         return [NSValue valueWithSize:(NSSize)protectedSelf->m_page->size()];
189     }).autorelease();
190 }
191
192 - (NSValue *)accessibilityAttributePositionValue
193 {
194     return ax::retrieveValueFromMainThread<RetainPtr<id>>([PROTECTED_SELF] () -> RetainPtr<id> {
195         if (!protectedSelf->m_page)
196             return nil;
197         return [NSValue valueWithPoint:(NSPoint)protectedSelf->m_page->accessibilityPosition()];
198     }).autorelease();
199 }
200
201 - (id)accessibilityDataDetectorValue:(NSString *)attribute point:(WebCore::FloatPoint&)point
202 {
203     return ax::retrieveValueFromMainThread<RetainPtr<id>>([&attribute, &point, PROTECTED_SELF] () -> RetainPtr<id> {
204         if (!protectedSelf->m_page)
205             return nil;
206         id value = nil;
207         if ([attribute isEqualToString:@"AXDataDetectorExistsAtPoint"] || [attribute isEqualToString:@"AXDidShowDataDetectorMenuAtPoint"]) {
208             bool boolValue;
209             if (protectedSelf->m_page->corePage()->pageOverlayController().copyAccessibilityAttributeBoolValueForPoint(attribute, point, boolValue))
210                 value = [NSNumber numberWithBool:boolValue];
211         }
212         if ([attribute isEqualToString:@"AXDataDetectorTypeAtPoint"]) {
213             String stringValue;
214             if (protectedSelf->m_page->corePage()->pageOverlayController().copyAccessibilityAttributeStringValueForPoint(attribute, point, stringValue))
215                 value = [NSString stringWithString:stringValue];
216         }
217         return value;
218     }).autorelease();
219 }
220
221 ALLOW_DEPRECATED_IMPLEMENTATIONS_BEGIN
222 - (id)accessibilityAttributeValue:(NSString *)attribute forParameter:(id)parameter
223 ALLOW_DEPRECATED_IMPLEMENTATIONS_END
224 {
225     WebCore::FloatPoint pageOverlayPoint;
226     if ([parameter isKindOfClass:[NSValue class]] && !strcmp([(NSValue *)parameter objCType], @encode(NSPoint)))
227         pageOverlayPoint = [self convertScreenPointToRootView:[(NSValue *)parameter pointValue]];
228     else
229         return nil;
230
231     if ([attribute isEqualToString:@"AXDataDetectorExistsAtPoint"] || [attribute isEqualToString:@"AXDidShowDataDetectorMenuAtPoint"] || [attribute isEqualToString:@"AXDataDetectorTypeAtPoint"])
232         return [self accessibilityDataDetectorValue:attribute point:pageOverlayPoint];
233
234     return nil;
235 }
236
237 - (BOOL)accessibilityShouldUseUniqueId
238 {
239     return YES;
240 }
241
242 ALLOW_DEPRECATED_DECLARATIONS_BEGIN
243 - (id)accessibilityHitTest:(NSPoint)point
244 {
245     auto convertedPoint = ax::retrieveValueFromMainThread<WebCore::IntPoint>([&point, PROTECTED_SELF] () -> WebCore::IntPoint {
246         if (!protectedSelf->m_page)
247             return WebCore::IntPoint(point);
248
249         auto convertedPoint = protectedSelf->m_page->screenToRootView(WebCore::IntPoint(point));
250
251         // Some plugins may be able to figure out the scroll position and inset on their own.
252         bool applyContentOffset = true;
253
254         // Isolated tree frames have the offset encoded into them so we don't need to undo here.
255 #if ENABLE(ACCESSIBILITY_ISOLATED_TREE)
256         bool queryingIsolatedTree = WebCore::AXObjectCache::clientSupportsIsolatedTree() && _AXUIElementRequestServicedBySecondaryAXThread();
257         applyContentOffset = !queryingIsolatedTree;
258 #endif
259         if (auto pluginView = WebKit::WebPage::pluginViewForFrame(protectedSelf->m_page->mainFrame()))
260             applyContentOffset = !pluginView->plugin()->pluginHandlesContentOffsetForAccessibilityHitTest();
261         
262         if (!applyContentOffset)
263             return convertedPoint;
264
265         if (auto* frameView = protectedSelf->m_page->mainFrameView())
266             convertedPoint.moveBy(frameView->scrollPosition());
267         if (auto* page = protectedSelf->m_page->corePage())
268             convertedPoint.move(0, -page->topContentInset());
269         return convertedPoint;
270     });
271     
272     return [[self accessibilityRootObjectWrapper] accessibilityHitTest:convertedPoint];
273 }
274 ALLOW_DEPRECATED_DECLARATIONS_END
275
276 @end
277
278 #endif // PLATFORM(MAC)
279