blob: 80c689c68b2359d83601633768a8176c717797a0 [file] [log] [blame]
bdakin@apple.come1d57db2008-06-24 21:02:38 +00001/*
jhoneycutt@apple.comcd3d45c2010-01-05 01:12:13 +00002 * Copyright (C) 2008, 2009, 2010 Apple Inc. All Rights Reserved.
bdakin@apple.come1d57db2008-06-24 21:02:38 +00003 *
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
darin@apple.coma6bd5092008-10-14 18:43:24 +000026#include "config.h"
commit-queue@webkit.orgd036cc82013-04-07 16:42:28 +000027
28#if HAVE(ACCESSIBILITY)
29
bdakin@apple.come1d57db2008-06-24 21:02:38 +000030#include "AccessibilityController.h"
31
weinig@apple.come876c602008-07-16 04:53:29 +000032#include "AccessibilityUIElement.h"
33#include <JavaScriptCore/JSRetainPtr.h>
bdakin@apple.come1d57db2008-06-24 21:02:38 +000034
weinig@apple.come876c602008-07-16 04:53:29 +000035// Static Value Getters
bdakin@apple.come1d57db2008-06-24 21:02:38 +000036
weinig@apple.come876c602008-07-16 04:53:29 +000037static JSValueRef getFocusedElementCallback(JSContextRef context, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef* exception)
bdakin@apple.come1d57db2008-06-24 21:02:38 +000038{
darin@apple.com2c430082009-03-27 23:55:04 +000039 AccessibilityController* controller = static_cast<AccessibilityController*>(JSObjectGetPrivate(thisObject));
weinig@apple.come876c602008-07-16 04:53:29 +000040 return AccessibilityUIElement::makeJSAccessibilityUIElement(context, controller->focusedElement());
bdakin@apple.come1d57db2008-06-24 21:02:38 +000041}
42
weinig@apple.come876c602008-07-16 04:53:29 +000043static JSValueRef getRootElementCallback(JSContextRef context, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef* exception)
bdakin@apple.comb8ae4a52008-06-26 00:22:59 +000044{
darin@apple.com2c430082009-03-27 23:55:04 +000045 AccessibilityController* controller = static_cast<AccessibilityController*>(JSObjectGetPrivate(thisObject));
weinig@apple.come876c602008-07-16 04:53:29 +000046 return AccessibilityUIElement::makeJSAccessibilityUIElement(context, controller->rootElement());
bdakin@apple.comfe260cb2008-07-07 23:26:05 +000047}
48
bdakin@apple.come1d57db2008-06-24 21:02:38 +000049// Object Creation
50
51void AccessibilityController::makeWindowObject(JSContextRef context, JSObjectRef windowObject, JSValueRef* exception)
52{
53 JSRetainPtr<JSStringRef> accessibilityControllerStr(Adopt, JSStringCreateWithUTF8CString("accessibilityController"));
weinig@apple.com22c5a162009-12-03 21:25:24 +000054
55 JSClassRef classRef = getJSClass();
56 JSValueRef accessibilityControllerObject = JSObjectMake(context, classRef, this);
57 JSClassRelease(classRef);
58
bdakin@apple.come1d57db2008-06-24 21:02:38 +000059 JSObjectSetProperty(context, windowObject, accessibilityControllerStr.get(), accessibilityControllerObject, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete, exception);
60}
61
jhoneycutt@apple.comfa9b19d2009-08-15 06:15:05 +000062static JSValueRef logFocusEventsCallback(JSContextRef ctx, JSObjectRef, JSObjectRef thisObject, size_t, const JSValueRef[], JSValueRef*)
63{
64 AccessibilityController* controller = static_cast<AccessibilityController*>(JSObjectGetPrivate(thisObject));
jhoneycutt@apple.com739aa7b2009-08-15 11:25:19 +000065 controller->setLogFocusEvents(true);
jhoneycutt@apple.comfa9b19d2009-08-15 06:15:05 +000066 return JSValueMakeUndefined(ctx);
67}
68
jhoneycutt@apple.comcd3d45c2010-01-05 01:12:13 +000069static JSValueRef logValueChangeEventsCallback(JSContextRef ctx, JSObjectRef, JSObjectRef thisObject, size_t, const JSValueRef[], JSValueRef*)
70{
71 AccessibilityController* controller = static_cast<AccessibilityController*>(JSObjectGetPrivate(thisObject));
72 controller->setLogValueChangeEvents(true);
73 return JSValueMakeUndefined(ctx);
74}
75
jhoneycutt@apple.comf1115ef52009-09-14 21:25:24 +000076static JSValueRef logScrollingStartEventsCallback(JSContextRef ctx, JSObjectRef, JSObjectRef thisObject, size_t, const JSValueRef[], JSValueRef*)
77{
78 AccessibilityController* controller = static_cast<AccessibilityController*>(JSObjectGetPrivate(thisObject));
79 controller->setLogScrollingStartEvents(true);
80 return JSValueMakeUndefined(ctx);
81}
82
mario@webkit.org19d34742011-02-10 09:03:30 +000083static JSValueRef logAccessibilityEventsCallback(JSContextRef ctx, JSObjectRef, JSObjectRef thisObject, size_t, const JSValueRef[], JSValueRef*)
84{
85 AccessibilityController* controller = static_cast<AccessibilityController*>(JSObjectGetPrivate(thisObject));
86 controller->setLogAccessibilityEvents(true);
87 return JSValueMakeUndefined(ctx);
88}
89
cfleizach@apple.com0c8875a2010-03-09 17:42:29 +000090static JSValueRef getElementAtPointCallback(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
91{
92 int x = 0;
93 int y = 0;
94 if (argumentCount == 2) {
95 x = JSValueToNumber(context, arguments[0], exception);
96 y = JSValueToNumber(context, arguments[1], exception);
97 }
98
99 AccessibilityController* controller = static_cast<AccessibilityController*>(JSObjectGetPrivate(thisObject));
100 return AccessibilityUIElement::makeJSAccessibilityUIElement(context, controller->elementAtPoint(x, y));
101}
102
dmazzoni@google.com8dea0ad02012-09-22 16:17:57 +0000103static JSValueRef getAccessibleElementByIdCallback(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
104{
105 JSStringRef idAttribute = 0;
106 if (argumentCount == 1)
107 idAttribute = JSValueToStringCopy(context, arguments[0], exception);
108 AccessibilityController* controller = static_cast<AccessibilityController*>(JSObjectGetPrivate(thisObject));
109 JSValueRef result = AccessibilityUIElement::makeJSAccessibilityUIElement(context, controller->accessibleElementById(idAttribute));
110 if (idAttribute)
111 JSStringRelease(idAttribute);
112 return result;
113}
114
dmazzoni@google.com1851ba02011-12-08 21:27:16 +0000115static JSValueRef addNotificationListenerCallback(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
116{
117 if (argumentCount != 1)
118 return JSValueMakeBoolean(context, false);
119
120 AccessibilityController* controller = static_cast<AccessibilityController*>(JSObjectGetPrivate(thisObject));
121 JSObjectRef callback = JSValueToObject(context, arguments[0], exception);
122 bool succeeded = controller->addNotificationListener(callback);
123 return JSValueMakeBoolean(context, succeeded);
124}
125
126static JSValueRef removeNotificationListenerCallback(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
127{
128 AccessibilityController* controller = static_cast<AccessibilityController*>(JSObjectGetPrivate(thisObject));
129 controller->removeNotificationListener();
130 return JSValueMakeUndefined(context);
131}
132
bdakin@apple.come1d57db2008-06-24 21:02:38 +0000133JSClassRef AccessibilityController::getJSClass()
134{
jhoneycutt@apple.comfa9b19d2009-08-15 06:15:05 +0000135 static JSStaticFunction staticFunctions[] = {
136 { "logFocusEvents", logFocusEventsCallback, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
jhoneycutt@apple.comcd3d45c2010-01-05 01:12:13 +0000137 { "logValueChangeEvents", logValueChangeEventsCallback, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
jhoneycutt@apple.comf1115ef52009-09-14 21:25:24 +0000138 { "logScrollingStartEvents", logScrollingStartEventsCallback, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
mario@webkit.org19d34742011-02-10 09:03:30 +0000139 { "logAccessibilityEvents", logAccessibilityEventsCallback, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
cfleizach@apple.com0c8875a2010-03-09 17:42:29 +0000140 { "elementAtPoint", getElementAtPointCallback, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
dmazzoni@google.com8dea0ad02012-09-22 16:17:57 +0000141 { "accessibleElementById", getAccessibleElementByIdCallback, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
dmazzoni@google.com1851ba02011-12-08 21:27:16 +0000142 { "addNotificationListener", addNotificationListenerCallback, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
143 { "removeNotificationListener", removeNotificationListenerCallback, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
jhoneycutt@apple.comfa9b19d2009-08-15 06:15:05 +0000144 { 0, 0, 0 }
145 };
146
weinig@apple.come876c602008-07-16 04:53:29 +0000147 static JSStaticValue staticValues[] = {
148 { "focusedElement", getFocusedElementCallback, 0, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
149 { "rootElement", getRootElementCallback, 0, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
150 { 0, 0, 0, 0 }
bdakin@apple.come1d57db2008-06-24 21:02:38 +0000151 };
152
weinig@apple.come876c602008-07-16 04:53:29 +0000153 static JSClassDefinition classDefinition = {
jhoneycutt@apple.comfa9b19d2009-08-15 06:15:05 +0000154 0, kJSClassAttributeNone, "AccessibilityController", 0, staticValues, staticFunctions,
weinig@apple.come876c602008-07-16 04:53:29 +0000155 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
156 };
157
weinig@apple.combb567532009-12-01 01:18:50 +0000158 return JSClassCreate(&classDefinition);
bdakin@apple.come1d57db2008-06-24 21:02:38 +0000159}
jhoneycutt@apple.com739aa7b2009-08-15 11:25:19 +0000160
161void AccessibilityController::resetToConsistentState()
162{
163 setLogFocusEvents(false);
jhoneycutt@apple.comcd3d45c2010-01-05 01:12:13 +0000164 setLogValueChangeEvents(false);
jhoneycutt@apple.comf1115ef52009-09-14 21:25:24 +0000165 setLogScrollingStartEvents(false);
mario@webkit.org19d34742011-02-10 09:03:30 +0000166 setLogAccessibilityEvents(false);
jhoneycutt@apple.com739aa7b2009-08-15 11:25:19 +0000167}
commit-queue@webkit.orgd036cc82013-04-07 16:42:28 +0000168#endif // HAVE(ACCESSIBILITY)