OLD | NEW |
| (Empty) |
1 // Copyright 2014 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 AccessibilityController::~AccessibilityController() | |
36 { | |
37 } | |
38 | |
39 void AccessibilityController::bindToJavascript(WebFrame* frame, const WebString&
classname) | |
40 { | |
41 WebAXObject::enableAccessibility(); | |
42 WebAXObject::enableInlineTextBoxAccessibility(); | |
43 CppBoundClass::bindToJavascript(frame, classname); | |
44 } | |
45 | |
46 void AccessibilityController::reset() | |
47 { | |
48 m_rootElement = WebAXObject(); | |
49 m_focusedElement = WebAXObject(); | |
50 m_elements.clear(); | |
51 m_notificationCallbacks.clear(); | |
52 | |
53 m_logAccessibilityEvents = false; | |
54 } | |
55 | |
56 void AccessibilityController::setFocusedElement(const WebAXObject& focusedElemen
t) | |
57 { | |
58 m_focusedElement = focusedElement; | |
59 } | |
60 | |
61 WebAXObjectProxy* AccessibilityController::getFocusedElement() | |
62 { | |
63 if (m_focusedElement.isNull()) | |
64 m_focusedElement = m_webView->accessibilityObject(); | |
65 return m_elements.getOrCreate(m_focusedElement); | |
66 } | |
67 | |
68 WebAXObjectProxy* AccessibilityController::getRootElement() | |
69 { | |
70 if (m_rootElement.isNull()) | |
71 m_rootElement = m_webView->accessibilityObject(); | |
72 return m_elements.createRoot(m_rootElement); | |
73 } | |
74 | |
75 WebAXObjectProxy* AccessibilityController::findAccessibleElementByIdRecursive(co
nst WebAXObject& obj, const WebString& id) | |
76 { | |
77 if (obj.isNull() || obj.isDetached()) | |
78 return 0; | |
79 | |
80 WebNode node = obj.node(); | |
81 if (!node.isNull() && node.isElementNode()) { | |
82 WebElement element = node.to<WebElement>(); | |
83 element.getAttribute("id"); | |
84 if (element.getAttribute("id") == id) | |
85 return m_elements.getOrCreate(obj); | |
86 } | |
87 | |
88 unsigned childCount = obj.childCount(); | |
89 for (unsigned i = 0; i < childCount; i++) { | |
90 if (WebAXObjectProxy* result = findAccessibleElementByIdRecursive(obj.ch
ildAt(i), id)) | |
91 return result; | |
92 } | |
93 | |
94 return 0; | |
95 } | |
96 | |
97 WebAXObjectProxy* AccessibilityController::getAccessibleElementById(const std::s
tring& id) | |
98 { | |
99 if (m_rootElement.isNull()) | |
100 m_rootElement = m_webView->accessibilityObject(); | |
101 | |
102 if (!m_rootElement.updateBackingStoreAndCheckValidity()) | |
103 return 0; | |
104 | |
105 return findAccessibleElementByIdRecursive(m_rootElement, WebString::fromUTF8
(id.c_str())); | |
106 } | |
107 | |
108 bool AccessibilityController::shouldLogAccessibilityEvents() | |
109 { | |
110 return m_logAccessibilityEvents; | |
111 } | |
112 | |
113 void AccessibilityController::notificationReceived(const blink::WebAXObject& tar
get, const char* notificationName) | |
114 { | |
115 // Call notification listeners on the element. | |
116 WebAXObjectProxy* element = m_elements.getOrCreate(target); | |
117 element->notificationReceived(notificationName); | |
118 | |
119 // Call global notification listeners. | |
120 size_t callbackCount = m_notificationCallbacks.size(); | |
121 for (size_t i = 0; i < callbackCount; i++) { | |
122 CppVariant arguments[2]; | |
123 arguments[0].set(*element->getAsCppVariant()); | |
124 arguments[1].set(notificationName); | |
125 CppVariant invokeResult; | |
126 m_notificationCallbacks[i].invokeDefault(arguments, 2, invokeResult); | |
127 } | |
128 } | |
129 | |
130 void AccessibilityController::logAccessibilityEventsCallback(const CppArgumentLi
st&, CppVariant* result) | |
131 { | |
132 m_logAccessibilityEvents = true; | |
133 result->setNull(); | |
134 } | |
135 | |
136 void AccessibilityController::addNotificationListenerCallback(const CppArgumentL
ist& arguments, CppVariant* result) | |
137 { | |
138 if (arguments.size() < 1 || !arguments[0].isObject()) { | |
139 result->setNull(); | |
140 return; | |
141 } | |
142 | |
143 m_notificationCallbacks.push_back(arguments[0]); | |
144 result->setNull(); | |
145 } | |
146 | |
147 void AccessibilityController::removeNotificationListenerCallback(const CppArgume
ntList&, CppVariant* result) | |
148 { | |
149 // FIXME: Implement this. | |
150 result->setNull(); | |
151 } | |
152 | |
153 void AccessibilityController::focusedElementGetterCallback(CppVariant* result) | |
154 { | |
155 result->set(*(getFocusedElement()->getAsCppVariant())); | |
156 } | |
157 | |
158 void AccessibilityController::rootElementGetterCallback(CppVariant* result) | |
159 { | |
160 result->set(*(getRootElement()->getAsCppVariant())); | |
161 } | |
162 | |
163 void AccessibilityController::accessibleElementByIdGetterCallback(const CppArgum
entList& arguments, CppVariant* result) | |
164 { | |
165 result->setNull(); | |
166 | |
167 if (arguments.size() < 1 || !arguments[0].isString()) | |
168 return; | |
169 | |
170 std::string id = arguments[0].toString(); | |
171 WebAXObjectProxy* foundElement = getAccessibleElementById(id); | |
172 if (!foundElement) | |
173 return; | |
174 | |
175 result->set(*(foundElement->getAsCppVariant())); | |
176 } | |
177 | |
178 void AccessibilityController::fallbackCallback(const CppArgumentList&, CppVarian
t* result) | |
179 { | |
180 m_delegate->printMessage("CONSOLE MESSAGE: JavaScript ERROR: unknown method
called on AccessibilityController\n"); | |
181 result->setNull(); | |
182 } | |
183 | |
184 } | |
OLD | NEW |