AccessibilityController.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Copyright (C) 2008, 2009, 2010 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. #include "config.h"
  26. #if HAVE(ACCESSIBILITY)
  27. #include "AccessibilityController.h"
  28. #include "AccessibilityUIElement.h"
  29. #include <JavaScriptCore/JSRetainPtr.h>
  30. // Static Value Getters
  31. static JSValueRef getFocusedElementCallback(JSContextRef context, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef* exception)
  32. {
  33. AccessibilityController* controller = static_cast<AccessibilityController*>(JSObjectGetPrivate(thisObject));
  34. return AccessibilityUIElement::makeJSAccessibilityUIElement(context, controller->focusedElement());
  35. }
  36. static JSValueRef getRootElementCallback(JSContextRef context, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef* exception)
  37. {
  38. AccessibilityController* controller = static_cast<AccessibilityController*>(JSObjectGetPrivate(thisObject));
  39. return AccessibilityUIElement::makeJSAccessibilityUIElement(context, controller->rootElement());
  40. }
  41. // Object Creation
  42. void AccessibilityController::makeWindowObject(JSContextRef context, JSObjectRef windowObject, JSValueRef* exception)
  43. {
  44. JSRetainPtr<JSStringRef> accessibilityControllerStr(Adopt, JSStringCreateWithUTF8CString("accessibilityController"));
  45. JSClassRef classRef = getJSClass();
  46. JSValueRef accessibilityControllerObject = JSObjectMake(context, classRef, this);
  47. JSClassRelease(classRef);
  48. JSObjectSetProperty(context, windowObject, accessibilityControllerStr.get(), accessibilityControllerObject, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete, exception);
  49. }
  50. static JSValueRef logFocusEventsCallback(JSContextRef ctx, JSObjectRef, JSObjectRef thisObject, size_t, const JSValueRef[], JSValueRef*)
  51. {
  52. AccessibilityController* controller = static_cast<AccessibilityController*>(JSObjectGetPrivate(thisObject));
  53. controller->setLogFocusEvents(true);
  54. return JSValueMakeUndefined(ctx);
  55. }
  56. static JSValueRef logValueChangeEventsCallback(JSContextRef ctx, JSObjectRef, JSObjectRef thisObject, size_t, const JSValueRef[], JSValueRef*)
  57. {
  58. AccessibilityController* controller = static_cast<AccessibilityController*>(JSObjectGetPrivate(thisObject));
  59. controller->setLogValueChangeEvents(true);
  60. return JSValueMakeUndefined(ctx);
  61. }
  62. static JSValueRef logScrollingStartEventsCallback(JSContextRef ctx, JSObjectRef, JSObjectRef thisObject, size_t, const JSValueRef[], JSValueRef*)
  63. {
  64. AccessibilityController* controller = static_cast<AccessibilityController*>(JSObjectGetPrivate(thisObject));
  65. controller->setLogScrollingStartEvents(true);
  66. return JSValueMakeUndefined(ctx);
  67. }
  68. static JSValueRef logAccessibilityEventsCallback(JSContextRef ctx, JSObjectRef, JSObjectRef thisObject, size_t, const JSValueRef[], JSValueRef*)
  69. {
  70. AccessibilityController* controller = static_cast<AccessibilityController*>(JSObjectGetPrivate(thisObject));
  71. controller->setLogAccessibilityEvents(true);
  72. return JSValueMakeUndefined(ctx);
  73. }
  74. static JSValueRef getElementAtPointCallback(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
  75. {
  76. int x = 0;
  77. int y = 0;
  78. if (argumentCount == 2) {
  79. x = JSValueToNumber(context, arguments[0], exception);
  80. y = JSValueToNumber(context, arguments[1], exception);
  81. }
  82. AccessibilityController* controller = static_cast<AccessibilityController*>(JSObjectGetPrivate(thisObject));
  83. return AccessibilityUIElement::makeJSAccessibilityUIElement(context, controller->elementAtPoint(x, y));
  84. }
  85. static JSValueRef getAccessibleElementByIdCallback(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
  86. {
  87. JSStringRef idAttribute = 0;
  88. if (argumentCount == 1)
  89. idAttribute = JSValueToStringCopy(context, arguments[0], exception);
  90. AccessibilityController* controller = static_cast<AccessibilityController*>(JSObjectGetPrivate(thisObject));
  91. JSValueRef result = AccessibilityUIElement::makeJSAccessibilityUIElement(context, controller->accessibleElementById(idAttribute));
  92. if (idAttribute)
  93. JSStringRelease(idAttribute);
  94. return result;
  95. }
  96. static JSValueRef addNotificationListenerCallback(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
  97. {
  98. if (argumentCount != 1)
  99. return JSValueMakeBoolean(context, false);
  100. AccessibilityController* controller = static_cast<AccessibilityController*>(JSObjectGetPrivate(thisObject));
  101. JSObjectRef callback = JSValueToObject(context, arguments[0], exception);
  102. bool succeeded = controller->addNotificationListener(callback);
  103. return JSValueMakeBoolean(context, succeeded);
  104. }
  105. static JSValueRef removeNotificationListenerCallback(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
  106. {
  107. AccessibilityController* controller = static_cast<AccessibilityController*>(JSObjectGetPrivate(thisObject));
  108. controller->removeNotificationListener();
  109. return JSValueMakeUndefined(context);
  110. }
  111. JSClassRef AccessibilityController::getJSClass()
  112. {
  113. static JSStaticFunction staticFunctions[] = {
  114. { "logFocusEvents", logFocusEventsCallback, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
  115. { "logValueChangeEvents", logValueChangeEventsCallback, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
  116. { "logScrollingStartEvents", logScrollingStartEventsCallback, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
  117. { "logAccessibilityEvents", logAccessibilityEventsCallback, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
  118. { "elementAtPoint", getElementAtPointCallback, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
  119. { "accessibleElementById", getAccessibleElementByIdCallback, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
  120. { "addNotificationListener", addNotificationListenerCallback, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
  121. { "removeNotificationListener", removeNotificationListenerCallback, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
  122. { 0, 0, 0 }
  123. };
  124. static JSStaticValue staticValues[] = {
  125. { "focusedElement", getFocusedElementCallback, 0, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
  126. { "rootElement", getRootElementCallback, 0, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
  127. { 0, 0, 0, 0 }
  128. };
  129. static JSClassDefinition classDefinition = {
  130. 0, kJSClassAttributeNone, "AccessibilityController", 0, staticValues, staticFunctions,
  131. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  132. };
  133. return JSClassCreate(&classDefinition);
  134. }
  135. void AccessibilityController::resetToConsistentState()
  136. {
  137. setLogFocusEvents(false);
  138. setLogValueChangeEvents(false);
  139. setLogScrollingStartEvents(false);
  140. setLogAccessibilityEvents(false);
  141. }
  142. #endif // HAVE(ACCESSIBILITY)