OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/shell/renderer/test_runner/AccessibilityController.h" |
| 6 |
| 7 #include "content/shell/renderer/test_runner/WebTestDelegate.h" |
| 8 #include "third_party/WebKit/public/platform/WebCString.h" |
| 9 #include "third_party/WebKit/public/web/WebAXObject.h" |
| 10 #include "third_party/WebKit/public/web/WebElement.h" |
| 11 #include "third_party/WebKit/public/web/WebFrame.h" |
| 12 #include "third_party/WebKit/public/web/WebNode.h" |
| 13 #include "third_party/WebKit/public/web/WebView.h" |
| 14 |
| 15 using namespace blink; |
| 16 |
| 17 namespace WebTestRunner { |
| 18 |
| 19 AccessibilityController::AccessibilityController() |
| 20 : m_logAccessibilityEvents(false) |
| 21 { |
| 22 |
| 23 bindMethod("logAccessibilityEvents", &AccessibilityController::logAccessibil
ityEventsCallback); |
| 24 bindMethod("addNotificationListener", &AccessibilityController::addNotificat
ionListenerCallback); |
| 25 bindMethod("removeNotificationListener", &AccessibilityController::removeNot
ificationListenerCallback); |
| 26 |
| 27 bindProperty("focusedElement", &AccessibilityController::focusedElementGette
rCallback); |
| 28 bindProperty("rootElement", &AccessibilityController::rootElementGetterCallb
ack); |
| 29 |
| 30 bindMethod("accessibleElementById", &AccessibilityController::accessibleElem
entByIdGetterCallback); |
| 31 |
| 32 bindFallbackMethod(&AccessibilityController::fallbackCallback); |
| 33 } |
| 34 |
| 35 void AccessibilityController::bindToJavascript(WebFrame* frame, const WebString&
classname) |
| 36 { |
| 37 WebAXObject::enableAccessibility(); |
| 38 WebAXObject::enableInlineTextBoxAccessibility(); |
| 39 CppBoundClass::bindToJavascript(frame, classname); |
| 40 } |
| 41 |
| 42 void AccessibilityController::reset() |
| 43 { |
| 44 m_rootElement = WebAXObject(); |
| 45 m_focusedElement = WebAXObject(); |
| 46 m_elements.clear(); |
| 47 m_notificationCallbacks.clear(); |
| 48 |
| 49 m_logAccessibilityEvents = false; |
| 50 } |
| 51 |
| 52 void AccessibilityController::setFocusedElement(const WebAXObject& focusedElemen
t) |
| 53 { |
| 54 m_focusedElement = focusedElement; |
| 55 } |
| 56 |
| 57 WebAXObjectProxy* AccessibilityController::getFocusedElement() |
| 58 { |
| 59 if (m_focusedElement.isNull()) |
| 60 m_focusedElement = m_webView->accessibilityObject(); |
| 61 return m_elements.getOrCreate(m_focusedElement); |
| 62 } |
| 63 |
| 64 WebAXObjectProxy* AccessibilityController::getRootElement() |
| 65 { |
| 66 if (m_rootElement.isNull()) |
| 67 m_rootElement = m_webView->accessibilityObject(); |
| 68 return m_elements.createRoot(m_rootElement); |
| 69 } |
| 70 |
| 71 WebAXObjectProxy* AccessibilityController::findAccessibleElementByIdRecursive(co
nst WebAXObject& obj, const WebString& id) |
| 72 { |
| 73 if (obj.isNull() || obj.isDetached()) |
| 74 return 0; |
| 75 |
| 76 WebNode node = obj.node(); |
| 77 if (!node.isNull() && node.isElementNode()) { |
| 78 WebElement element = node.to<WebElement>(); |
| 79 element.getAttribute("id"); |
| 80 if (element.getAttribute("id") == id) |
| 81 return m_elements.getOrCreate(obj); |
| 82 } |
| 83 |
| 84 unsigned childCount = obj.childCount(); |
| 85 for (unsigned i = 0; i < childCount; i++) { |
| 86 if (WebAXObjectProxy* result = findAccessibleElementByIdRecursive(obj.ch
ildAt(i), id)) |
| 87 return result; |
| 88 } |
| 89 |
| 90 return 0; |
| 91 } |
| 92 |
| 93 WebAXObjectProxy* AccessibilityController::getAccessibleElementById(const std::s
tring& id) |
| 94 { |
| 95 if (m_rootElement.isNull()) |
| 96 m_rootElement = m_webView->accessibilityObject(); |
| 97 |
| 98 if (!m_rootElement.updateBackingStoreAndCheckValidity()) |
| 99 return 0; |
| 100 |
| 101 return findAccessibleElementByIdRecursive(m_rootElement, WebString::fromUTF8
(id.c_str())); |
| 102 } |
| 103 |
| 104 bool AccessibilityController::shouldLogAccessibilityEvents() |
| 105 { |
| 106 return m_logAccessibilityEvents; |
| 107 } |
| 108 |
| 109 void AccessibilityController::notificationReceived(const blink::WebAXObject& tar
get, const char* notificationName) |
| 110 { |
| 111 // Call notification listeners on the element. |
| 112 WebAXObjectProxy* element = m_elements.getOrCreate(target); |
| 113 element->notificationReceived(notificationName); |
| 114 |
| 115 // Call global notification listeners. |
| 116 size_t callbackCount = m_notificationCallbacks.size(); |
| 117 for (size_t i = 0; i < callbackCount; i++) { |
| 118 CppVariant arguments[2]; |
| 119 arguments[0].set(*element->getAsCppVariant()); |
| 120 arguments[1].set(notificationName); |
| 121 CppVariant invokeResult; |
| 122 m_notificationCallbacks[i].invokeDefault(arguments, 2, invokeResult); |
| 123 } |
| 124 } |
| 125 |
| 126 void AccessibilityController::logAccessibilityEventsCallback(const CppArgumentLi
st&, CppVariant* result) |
| 127 { |
| 128 m_logAccessibilityEvents = true; |
| 129 result->setNull(); |
| 130 } |
| 131 |
| 132 void AccessibilityController::addNotificationListenerCallback(const CppArgumentL
ist& arguments, CppVariant* result) |
| 133 { |
| 134 if (arguments.size() < 1 || !arguments[0].isObject()) { |
| 135 result->setNull(); |
| 136 return; |
| 137 } |
| 138 |
| 139 m_notificationCallbacks.push_back(arguments[0]); |
| 140 result->setNull(); |
| 141 } |
| 142 |
| 143 void AccessibilityController::removeNotificationListenerCallback(const CppArgume
ntList&, CppVariant* result) |
| 144 { |
| 145 // FIXME: Implement this. |
| 146 result->setNull(); |
| 147 } |
| 148 |
| 149 void AccessibilityController::focusedElementGetterCallback(CppVariant* result) |
| 150 { |
| 151 result->set(*(getFocusedElement()->getAsCppVariant())); |
| 152 } |
| 153 |
| 154 void AccessibilityController::rootElementGetterCallback(CppVariant* result) |
| 155 { |
| 156 result->set(*(getRootElement()->getAsCppVariant())); |
| 157 } |
| 158 |
| 159 void AccessibilityController::accessibleElementByIdGetterCallback(const CppArgum
entList& arguments, CppVariant* result) |
| 160 { |
| 161 result->setNull(); |
| 162 |
| 163 if (arguments.size() < 1 || !arguments[0].isString()) |
| 164 return; |
| 165 |
| 166 std::string id = arguments[0].toString(); |
| 167 WebAXObjectProxy* foundElement = getAccessibleElementById(id); |
| 168 if (!foundElement) |
| 169 return; |
| 170 |
| 171 result->set(*(foundElement->getAsCppVariant())); |
| 172 } |
| 173 |
| 174 void AccessibilityController::fallbackCallback(const CppArgumentList&, CppVarian
t* result) |
| 175 { |
| 176 m_delegate->printMessage("CONSOLE MESSAGE: JavaScript ERROR: unknown method
called on AccessibilityController\n"); |
| 177 result->setNull(); |
| 178 } |
| 179 |
| 180 } |
OLD | NEW |