| 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 | #include "config.h" |
| 26 | #include "AccessibilityController.h" |
| 27 | #include "AccessibilityUIElement.h" |
| 28 | #include "JSAccessibilityController.h" |
| 29 | #include "JSAccessibilityUIElement.h" |
| 30 | #include <JavaScriptCore/JSRetainPtr.h> |
| 31 | #include <wtf/GetPtr.h> |
| 32 | |
| 33 | namespace WTR { |
| 34 | |
| 35 | AccessibilityController* toAccessibilityController(JSContextRef context, JSValueRef value) |
| 36 | { |
| 37 | if (!context || !value || !JSAccessibilityController::accessibilityControllerClass() || !JSValueIsObjectOfClass(context, value, JSAccessibilityController::accessibilityControllerClass())) |
| 38 | return 0; |
| 39 | return static_cast<AccessibilityController*>(JSWrapper::unwrap(context, value)); |
| 40 | } |
| 41 | |
| 42 | JSClassRef JSAccessibilityController::accessibilityControllerClass() |
| 43 | { |
| 44 | static JSClassRef jsClass; |
| 45 | if (!jsClass) { |
| 46 | JSClassDefinition definition = kJSClassDefinitionEmpty; |
| 47 | definition.className = "AccessibilityController" ; |
| 48 | definition.parentClass = 0; |
| 49 | definition.staticValues = staticValues(); |
| 50 | definition.staticFunctions = staticFunctions(); |
| 51 | definition.initialize = initialize; |
| 52 | definition.finalize = finalize; |
| 53 | jsClass = JSClassCreate(&definition); |
| 54 | } |
| 55 | return jsClass; |
| 56 | } |
| 57 | |
| 58 | const JSStaticFunction* JSAccessibilityController::staticFunctions() |
| 59 | { |
| 60 | static const JSStaticFunction functions[] = { |
| 61 | { "enableEnhancedAccessibility" , enableEnhancedAccessibility, kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly }, |
| 62 | { "elementAtPoint" , elementAtPoint, kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly }, |
| 63 | { "accessibleElementById" , accessibleElementById, kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly }, |
| 64 | { "addNotificationListener" , addNotificationListener, kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly }, |
| 65 | { "removeNotificationListener" , removeNotificationListener, kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly }, |
| 66 | { "logFocusEvents" , logFocusEvents, kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly }, |
| 67 | { "logValueChangeEvents" , logValueChangeEvents, kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly }, |
| 68 | { "logScrollingStartEvents" , logScrollingStartEvents, kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly }, |
| 69 | { "logAccessibilityEvents" , logAccessibilityEvents, kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly }, |
| 70 | { "resetToConsistentState" , resetToConsistentState, kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly }, |
| 71 | { 0, 0, 0 } |
| 72 | }; |
| 73 | return functions; |
| 74 | } |
| 75 | |
| 76 | const JSStaticValue* JSAccessibilityController::staticValues() |
| 77 | { |
| 78 | static const JSStaticValue values[] = { |
| 79 | { "enhancedAccessibilityEnabled" , enhancedAccessibilityEnabled, 0, kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly }, |
| 80 | { "platformName" , platformName, 0, kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly }, |
| 81 | { "rootElement" , rootElement, 0, kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly }, |
| 82 | { "focusedElement" , focusedElement, 0, kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly }, |
| 83 | { 0, 0, 0, 0 } |
| 84 | }; |
| 85 | return values; |
| 86 | } |
| 87 | |
| 88 | // Functions |
| 89 | |
| 90 | JSValueRef JSAccessibilityController::enableEnhancedAccessibility(JSContextRef context, JSObjectRef, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) |
| 91 | { |
| 92 | AccessibilityController* impl = toAccessibilityController(context, thisObject); |
| 93 | if (!impl) |
| 94 | return JSValueMakeUndefined(context); |
| 95 | |
| 96 | bool enable = argumentCount > 0 && JSValueToBoolean(context, arguments[0]); |
| 97 | impl->enableEnhancedAccessibility(enable); |
| 98 | |
| 99 | return JSValueMakeUndefined(context); |
| 100 | } |
| 101 | |
| 102 | JSValueRef JSAccessibilityController::elementAtPoint(JSContextRef context, JSObjectRef, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) |
| 103 | { |
| 104 | AccessibilityController* impl = toAccessibilityController(context, thisObject); |
| 105 | if (!impl) |
| 106 | return JSValueMakeUndefined(context); |
| 107 | |
| 108 | double x = argumentCount > 0 ? JSValueToNumber(context, arguments[0], nullptr) : 0; |
| 109 | double y = argumentCount > 1 ? JSValueToNumber(context, arguments[1], nullptr) : 0; |
| 110 | return toJS(context, WTF::getPtr(impl->elementAtPoint(x, y))); |
| 111 | } |
| 112 | |
| 113 | JSValueRef JSAccessibilityController::accessibleElementById(JSContextRef context, JSObjectRef, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) |
| 114 | { |
| 115 | AccessibilityController* impl = toAccessibilityController(context, thisObject); |
| 116 | if (!impl) |
| 117 | return JSValueMakeUndefined(context); |
| 118 | |
| 119 | JSRetainPtr<JSStringRef> id = argumentCount > 0 ? adopt(JSValueToStringCopy(context, arguments[0], nullptr)) : JSRetainPtr<JSStringRef>(); |
| 120 | return toJS(context, WTF::getPtr(impl->accessibleElementById(id.get()))); |
| 121 | } |
| 122 | |
| 123 | JSValueRef JSAccessibilityController::addNotificationListener(JSContextRef context, JSObjectRef, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) |
| 124 | { |
| 125 | AccessibilityController* impl = toAccessibilityController(context, thisObject); |
| 126 | if (!impl) |
| 127 | return JSValueMakeUndefined(context); |
| 128 | |
| 129 | JSValueRef functionCallback = argumentCount > 0 ? arguments[0] : JSValueMakeUndefined(context); |
| 130 | return JSValueMakeBoolean(context, impl->addNotificationListener(functionCallback)); |
| 131 | } |
| 132 | |
| 133 | JSValueRef JSAccessibilityController::removeNotificationListener(JSContextRef context, JSObjectRef, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) |
| 134 | { |
| 135 | AccessibilityController* impl = toAccessibilityController(context, thisObject); |
| 136 | if (!impl) |
| 137 | return JSValueMakeUndefined(context); |
| 138 | |
| 139 | return JSValueMakeBoolean(context, impl->removeNotificationListener()); |
| 140 | } |
| 141 | |
| 142 | JSValueRef JSAccessibilityController::logFocusEvents(JSContextRef context, JSObjectRef, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) |
| 143 | { |
| 144 | AccessibilityController* impl = toAccessibilityController(context, thisObject); |
| 145 | if (!impl) |
| 146 | return JSValueMakeUndefined(context); |
| 147 | |
| 148 | impl->logFocusEvents(); |
| 149 | |
| 150 | return JSValueMakeUndefined(context); |
| 151 | } |
| 152 | |
| 153 | JSValueRef JSAccessibilityController::logValueChangeEvents(JSContextRef context, JSObjectRef, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) |
| 154 | { |
| 155 | AccessibilityController* impl = toAccessibilityController(context, thisObject); |
| 156 | if (!impl) |
| 157 | return JSValueMakeUndefined(context); |
| 158 | |
| 159 | impl->logValueChangeEvents(); |
| 160 | |
| 161 | return JSValueMakeUndefined(context); |
| 162 | } |
| 163 | |
| 164 | JSValueRef JSAccessibilityController::logScrollingStartEvents(JSContextRef context, JSObjectRef, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) |
| 165 | { |
| 166 | AccessibilityController* impl = toAccessibilityController(context, thisObject); |
| 167 | if (!impl) |
| 168 | return JSValueMakeUndefined(context); |
| 169 | |
| 170 | impl->logScrollingStartEvents(); |
| 171 | |
| 172 | return JSValueMakeUndefined(context); |
| 173 | } |
| 174 | |
| 175 | JSValueRef JSAccessibilityController::logAccessibilityEvents(JSContextRef context, JSObjectRef, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) |
| 176 | { |
| 177 | AccessibilityController* impl = toAccessibilityController(context, thisObject); |
| 178 | if (!impl) |
| 179 | return JSValueMakeUndefined(context); |
| 180 | |
| 181 | impl->logAccessibilityEvents(); |
| 182 | |
| 183 | return JSValueMakeUndefined(context); |
| 184 | } |
| 185 | |
| 186 | JSValueRef JSAccessibilityController::resetToConsistentState(JSContextRef context, JSObjectRef, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) |
| 187 | { |
| 188 | AccessibilityController* impl = toAccessibilityController(context, thisObject); |
| 189 | if (!impl) |
| 190 | return JSValueMakeUndefined(context); |
| 191 | |
| 192 | impl->resetToConsistentState(); |
| 193 | |
| 194 | return JSValueMakeUndefined(context); |
| 195 | } |
| 196 | |
| 197 | // Attributes |
| 198 | |
| 199 | JSValueRef JSAccessibilityController::enhancedAccessibilityEnabled(JSContextRef context, JSObjectRef object, JSStringRef, JSValueRef* exception) |
| 200 | { |
| 201 | AccessibilityController* impl = toAccessibilityController(context, object); |
| 202 | if (!impl) |
| 203 | return JSValueMakeUndefined(context); |
| 204 | |
| 205 | return JSValueMakeBoolean(context, impl->enhancedAccessibilityEnabled()); |
| 206 | } |
| 207 | |
| 208 | JSValueRef JSAccessibilityController::platformName(JSContextRef context, JSObjectRef object, JSStringRef, JSValueRef* exception) |
| 209 | { |
| 210 | AccessibilityController* impl = toAccessibilityController(context, object); |
| 211 | if (!impl) |
| 212 | return JSValueMakeUndefined(context); |
| 213 | |
| 214 | return JSValueMakeStringOrNull(context, impl->platformName().get()); |
| 215 | } |
| 216 | |
| 217 | JSValueRef JSAccessibilityController::rootElement(JSContextRef context, JSObjectRef object, JSStringRef, JSValueRef* exception) |
| 218 | { |
| 219 | AccessibilityController* impl = toAccessibilityController(context, object); |
| 220 | if (!impl) |
| 221 | return JSValueMakeUndefined(context); |
| 222 | |
| 223 | return toJS(context, WTF::getPtr(impl->rootElement())); |
| 224 | } |
| 225 | |
| 226 | JSValueRef JSAccessibilityController::focusedElement(JSContextRef context, JSObjectRef object, JSStringRef, JSValueRef* exception) |
| 227 | { |
| 228 | AccessibilityController* impl = toAccessibilityController(context, object); |
| 229 | if (!impl) |
| 230 | return JSValueMakeUndefined(context); |
| 231 | |
| 232 | return toJS(context, WTF::getPtr(impl->focusedElement())); |
| 233 | } |
| 234 | |
| 235 | } // namespace WTR |
| 236 | |
| 237 | |