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 |
|
26 #include "config.h" |
|
27 #include "AccessibilityController.h" |
|
28 |
|
29 #include "AccessibilityUIElement.h" |
|
30 #include "DumpRenderTree.h" |
|
31 #include "FrameLoadDelegate.h" |
|
32 #include <JavaScriptCore/Assertions.h> |
|
33 #include <JavaScriptCore/JSRetainPtr.h> |
|
34 #include <JavaScriptCore/JSStringRef.h> |
|
35 #include <WebCore/COMPtr.h> |
|
36 #include <WebKit/WebKit.h> |
|
37 #include <oleacc.h> |
|
38 #include <string> |
|
39 |
|
40 using namespace std; |
|
41 |
|
42 AccessibilityController::AccessibilityController() |
|
43 : m_focusEventHook(0) |
|
44 , m_scrollingStartEventHook(0) |
|
45 , m_valueChangeEventHook(0) |
|
46 , m_allEventsHook(0) |
|
47 { |
|
48 } |
|
49 |
|
50 AccessibilityController::~AccessibilityController() |
|
51 { |
|
52 setLogFocusEvents(false); |
|
53 setLogValueChangeEvents(false); |
|
54 |
|
55 if (m_allEventsHook) |
|
56 UnhookWinEvent(m_allEventsHook); |
|
57 |
|
58 for (HashMap<PlatformUIElement, JSObjectRef>::iterator it = m_notificationListeners.begin(); it != m_notificationListeners.end(); ++it) |
|
59 JSValueUnprotect(frame->globalContext(), it->second); |
|
60 } |
|
61 |
|
62 AccessibilityUIElement AccessibilityController::elementAtPoint(int x, int y) |
|
63 { |
|
64 // FIXME: implement |
|
65 return 0; |
|
66 } |
|
67 |
|
68 AccessibilityUIElement AccessibilityController::focusedElement() |
|
69 { |
|
70 COMPtr<IAccessible> rootAccessible = rootElement().platformUIElement(); |
|
71 |
|
72 VARIANT vFocus; |
|
73 if (FAILED(rootAccessible->get_accFocus(&vFocus))) |
|
74 return 0; |
|
75 |
|
76 if (V_VT(&vFocus) == VT_I4) { |
|
77 ASSERT(V_I4(&vFocus) == CHILDID_SELF); |
|
78 // The root accessible object is the focused object. |
|
79 return rootAccessible; |
|
80 } |
|
81 |
|
82 ASSERT(V_VT(&vFocus) == VT_DISPATCH); |
|
83 // We have an IDispatch; query for IAccessible. |
|
84 return COMPtr<IAccessible>(Query, V_DISPATCH(&vFocus)); |
|
85 } |
|
86 |
|
87 AccessibilityUIElement AccessibilityController::rootElement() |
|
88 { |
|
89 COMPtr<IWebView> view; |
|
90 if (FAILED(frame->webView(&view))) |
|
91 return 0; |
|
92 |
|
93 COMPtr<IWebViewPrivate> viewPrivate(Query, view); |
|
94 if (!viewPrivate) |
|
95 return 0; |
|
96 |
|
97 HWND webViewWindow; |
|
98 if (FAILED(viewPrivate->viewWindow((OLE_HANDLE*)&webViewWindow))) |
|
99 return 0; |
|
100 |
|
101 // Get the root accessible object by querying for the accessible object for the |
|
102 // WebView's window. |
|
103 COMPtr<IAccessible> rootAccessible; |
|
104 if (FAILED(AccessibleObjectFromWindow(webViewWindow, static_cast<DWORD>(OBJID_CLIENT), __uuidof(IAccessible), reinterpret_cast<void**>(&rootAccessible)))) |
|
105 return 0; |
|
106 |
|
107 return rootAccessible; |
|
108 } |
|
109 |
|
110 static void CALLBACK logEventProc(HWINEVENTHOOK, DWORD event, HWND hwnd, LONG idObject, LONG idChild, DWORD, DWORD) |
|
111 { |
|
112 // Get the accessible object for this event. |
|
113 COMPtr<IAccessible> parentObject; |
|
114 |
|
115 VARIANT vChild; |
|
116 VariantInit(&vChild); |
|
117 |
|
118 HRESULT hr = AccessibleObjectFromEvent(hwnd, idObject, idChild, &parentObject, &vChild); |
|
119 ASSERT(SUCCEEDED(hr)); |
|
120 |
|
121 // Get the name of the focused element, and log it to stdout. |
|
122 BSTR nameBSTR; |
|
123 hr = parentObject->get_accName(vChild, &nameBSTR); |
|
124 ASSERT(SUCCEEDED(hr)); |
|
125 wstring name(nameBSTR, ::SysStringLen(nameBSTR)); |
|
126 SysFreeString(nameBSTR); |
|
127 |
|
128 switch (event) { |
|
129 case EVENT_OBJECT_FOCUS: |
|
130 printf("Received focus event for object '%S'.\n", name.c_str()); |
|
131 break; |
|
132 |
|
133 case EVENT_OBJECT_VALUECHANGE: { |
|
134 BSTR valueBSTR; |
|
135 hr = parentObject->get_accValue(vChild, &valueBSTR); |
|
136 ASSERT(SUCCEEDED(hr)); |
|
137 wstring value(valueBSTR, ::SysStringLen(valueBSTR)); |
|
138 SysFreeString(valueBSTR); |
|
139 |
|
140 printf("Received value change event for object '%S', value '%S'.\n", name.c_str(), value.c_str()); |
|
141 break; |
|
142 } |
|
143 |
|
144 case EVENT_SYSTEM_SCROLLINGSTART: |
|
145 printf("Received scrolling start event for object '%S'.\n", name.c_str()); |
|
146 break; |
|
147 |
|
148 default: |
|
149 printf("Received unknown event for object '%S'.\n", name.c_str()); |
|
150 break; |
|
151 } |
|
152 |
|
153 VariantClear(&vChild); |
|
154 } |
|
155 |
|
156 void AccessibilityController::setLogFocusEvents(bool logFocusEvents) |
|
157 { |
|
158 if (!!m_focusEventHook == logFocusEvents) |
|
159 return; |
|
160 |
|
161 if (!logFocusEvents) { |
|
162 UnhookWinEvent(m_focusEventHook); |
|
163 m_focusEventHook = 0; |
|
164 return; |
|
165 } |
|
166 |
|
167 // Ensure that accessibility is initialized for the WebView by querying for |
|
168 // the root accessible object. |
|
169 rootElement(); |
|
170 |
|
171 m_focusEventHook = SetWinEventHook(EVENT_OBJECT_FOCUS, EVENT_OBJECT_FOCUS, GetModuleHandle(0), logEventProc, GetCurrentProcessId(), 0, WINEVENT_INCONTEXT); |
|
172 |
|
173 ASSERT(m_focusEventHook); |
|
174 } |
|
175 |
|
176 void AccessibilityController::setLogValueChangeEvents(bool logValueChangeEvents) |
|
177 { |
|
178 if (!!m_valueChangeEventHook == logValueChangeEvents) |
|
179 return; |
|
180 |
|
181 if (!logValueChangeEvents) { |
|
182 UnhookWinEvent(m_valueChangeEventHook); |
|
183 m_valueChangeEventHook = 0; |
|
184 return; |
|
185 } |
|
186 |
|
187 // Ensure that accessibility is initialized for the WebView by querying for |
|
188 // the root accessible object. |
|
189 rootElement(); |
|
190 |
|
191 m_valueChangeEventHook = SetWinEventHook(EVENT_OBJECT_VALUECHANGE, EVENT_OBJECT_VALUECHANGE, GetModuleHandle(0), logEventProc, GetCurrentProcessId(), 0, WINEVENT_INCONTEXT); |
|
192 |
|
193 ASSERT(m_valueChangeEventHook); |
|
194 } |
|
195 |
|
196 void AccessibilityController::setLogScrollingStartEvents(bool logScrollingStartEvents) |
|
197 { |
|
198 if (!!m_scrollingStartEventHook == logScrollingStartEvents) |
|
199 return; |
|
200 |
|
201 if (!logScrollingStartEvents) { |
|
202 UnhookWinEvent(m_scrollingStartEventHook); |
|
203 m_scrollingStartEventHook = 0; |
|
204 return; |
|
205 } |
|
206 |
|
207 // Ensure that accessibility is initialized for the WebView by querying for |
|
208 // the root accessible object. |
|
209 rootElement(); |
|
210 |
|
211 m_scrollingStartEventHook = SetWinEventHook(EVENT_SYSTEM_SCROLLINGSTART, EVENT_SYSTEM_SCROLLINGSTART, GetModuleHandle(0), logEventProc, GetCurrentProcessId(), 0, WINEVENT_INCONTEXT); |
|
212 |
|
213 ASSERT(m_scrollingStartEventHook); |
|
214 } |
|
215 |
|
216 static string stringEvent(DWORD event) |
|
217 { |
|
218 switch(event) { |
|
219 case EVENT_OBJECT_VALUECHANGE: |
|
220 return "value change event"; |
|
221 default: |
|
222 return "unknown event"; |
|
223 } |
|
224 } |
|
225 |
|
226 static void CALLBACK notificationListenerProc(HWINEVENTHOOK, DWORD event, HWND hwnd, LONG idObject, LONG idChild, DWORD, DWORD) |
|
227 { |
|
228 // Get the accessible object for this event. |
|
229 COMPtr<IAccessible> parentObject; |
|
230 |
|
231 VARIANT vChild; |
|
232 VariantInit(&vChild); |
|
233 |
|
234 HRESULT hr = AccessibleObjectFromEvent(hwnd, idObject, idChild, &parentObject, &vChild); |
|
235 ASSERT(SUCCEEDED(hr)); |
|
236 |
|
237 COMPtr<IDispatch> childDispatch; |
|
238 if (FAILED(parentObject->get_accChild(vChild, &childDispatch))) { |
|
239 VariantClear(&vChild); |
|
240 return; |
|
241 } |
|
242 |
|
243 COMPtr<IAccessible> childAccessible(Query, childDispatch); |
|
244 |
|
245 sharedFrameLoadDelegate->accessibilityController()->notificationReceived(childAccessible, stringEvent(event)); |
|
246 |
|
247 VariantClear(&vChild); |
|
248 } |
|
249 |
|
250 static COMPtr<IAccessibleComparable> comparableObject(const COMPtr<IServiceProvider>& serviceProvider) |
|
251 { |
|
252 COMPtr<IAccessibleComparable> comparable; |
|
253 serviceProvider->QueryService(SID_AccessibleComparable, __uuidof(IAccessibleComparable), reinterpret_cast<void**>(&comparable)); |
|
254 return comparable; |
|
255 } |
|
256 |
|
257 void AccessibilityController::notificationReceived(PlatformUIElement element, const string& eventName) |
|
258 { |
|
259 for (HashMap<PlatformUIElement, JSObjectRef>::iterator it = m_notificationListeners.begin(); it != m_notificationListeners.end(); ++it) { |
|
260 COMPtr<IServiceProvider> thisServiceProvider(Query, it->first); |
|
261 if (!thisServiceProvider) |
|
262 continue; |
|
263 |
|
264 COMPtr<IAccessibleComparable> thisComparable = comparableObject(thisServiceProvider); |
|
265 if (!thisComparable) |
|
266 continue; |
|
267 |
|
268 COMPtr<IServiceProvider> elementServiceProvider(Query, element); |
|
269 if (!elementServiceProvider) |
|
270 continue; |
|
271 |
|
272 COMPtr<IAccessibleComparable> elementComparable = comparableObject(elementServiceProvider); |
|
273 if (!elementComparable) |
|
274 continue; |
|
275 |
|
276 BOOL isSame = FALSE; |
|
277 thisComparable->isSameObject(elementComparable.get(), &isSame); |
|
278 if (!isSame) |
|
279 continue; |
|
280 |
|
281 JSRetainPtr<JSStringRef> jsNotification(Adopt, JSStringCreateWithUTF8CString(eventName.c_str())); |
|
282 JSValueRef argument = JSValueMakeString(frame->globalContext(), jsNotification.get()); |
|
283 JSObjectCallAsFunction(frame->globalContext(), it->second, NULL, 1, &argument, NULL); |
|
284 } |
|
285 } |
|
286 |
|
287 void AccessibilityController::addNotificationListener(PlatformUIElement element, JSObjectRef functionCallback) |
|
288 { |
|
289 if (!m_allEventsHook) |
|
290 m_allEventsHook = SetWinEventHook(EVENT_MIN, EVENT_MAX, GetModuleHandle(0), notificationListenerProc, GetCurrentProcessId(), 0, WINEVENT_INCONTEXT); |
|
291 |
|
292 JSValueProtect(frame->globalContext(), functionCallback); |
|
293 m_notificationListeners.add(element, functionCallback); |
|
294 } |
|