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.
5 #include "content/browser/accessibility/accessibility_event_recorder.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/string_util.h"
13 #include "base/strings/stringprintf.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "base/win/scoped_bstr.h"
16 #include "base/win/scoped_comptr.h"
17 #include "base/win/scoped_variant.h"
18 #include "content/browser/accessibility/accessibility_tree_formatter_utils_win.h"
19 #include "content/browser/accessibility/browser_accessibility_manager.h"
20 #include "content/browser/accessibility/browser_accessibility_win.h"
21 #include "third_party/iaccessible2/ia2_api_all.h"
22 #include "ui/base/win/atl_module.h"
28 std::string
RoleVariantToString(const base::win::ScopedVariant
& role
) {
29 if (role
.type() == VT_I4
) {
30 return base::UTF16ToUTF8(IAccessibleRoleToString(V_I4(&role
)));
31 } else if (role
.type() == VT_BSTR
) {
32 return base::UTF16ToUTF8(
33 base::string16(V_BSTR(&role
), SysStringLen(V_BSTR(&role
))));
38 HRESULT
QueryIAccessible2(IAccessible
* accessible
, IAccessible2
** accessible2
) {
39 base::win::ScopedComPtr
<IServiceProvider
> service_provider
;
40 HRESULT hr
= accessible
->QueryInterface(service_provider
.Receive());
41 return SUCCEEDED(hr
) ?
42 service_provider
->QueryService(IID_IAccessible2
, accessible2
) : hr
;
45 HRESULT
QueryIAccessibleText(IAccessible
* accessible
,
46 IAccessibleText
** accessible_text
) {
47 base::win::ScopedComPtr
<IServiceProvider
> service_provider
;
48 HRESULT hr
= accessible
->QueryInterface(service_provider
.Receive());
49 return SUCCEEDED(hr
) ?
50 service_provider
->QueryService(IID_IAccessibleText
, accessible_text
) : hr
;
53 std::string
BstrToUTF8(BSTR bstr
) {
54 base::string16
str16(bstr
, SysStringLen(bstr
));
56 // IAccessibleText returns the text you get by appending all static text
57 // children, with an "embedded object character" for each non-text child.
58 // Pretty-print the embedded object character as <obj> so that test output
60 base::ReplaceChars(str16
, L
"\xfffc", L
"<obj>", &str16
);
62 return base::UTF16ToUTF8(str16
);
65 std::string
AccessibilityEventToStringUTF8(int32 event_id
) {
66 return base::UTF16ToUTF8(AccessibilityEventToString(event_id
));
71 class AccessibilityEventRecorderWin
: public AccessibilityEventRecorder
{
73 explicit AccessibilityEventRecorderWin(BrowserAccessibilityManager
* manager
);
74 virtual ~AccessibilityEventRecorderWin();
76 // Callback registered by SetWinEventHook. Just calls OnWinEventHook.
77 static void CALLBACK
WinEventHookThunk(
87 // Called by the thunk registered by SetWinEventHook. Retrives accessibility
88 // info about the node the event was fired on and appends a string to
90 void OnWinEventHook(HWINEVENTHOOK handle
,
98 // Wrapper around AccessibleObjectFromWindow because the function call
99 // inexplicably flakes sometimes on build/trybots.
100 HRESULT
AccessibleObjectFromWindowWrapper(
101 HWND hwnd
, DWORD dwId
, REFIID riid
, void **ppvObject
);
103 HWINEVENTHOOK win_event_hook_handle_
;
104 static AccessibilityEventRecorderWin
* instance_
;
108 AccessibilityEventRecorderWin
*
109 AccessibilityEventRecorderWin::instance_
= nullptr;
112 AccessibilityEventRecorder
* AccessibilityEventRecorder::Create(
113 BrowserAccessibilityManager
* manager
) {
114 return new AccessibilityEventRecorderWin(manager
);
118 void CALLBACK
AccessibilityEventRecorderWin::WinEventHookThunk(
119 HWINEVENTHOOK handle
,
127 instance_
->OnWinEventHook(handle
, event
, hwnd
, obj_id
, child_id
,
128 event_thread
, event_time
);
132 AccessibilityEventRecorderWin::AccessibilityEventRecorderWin(
133 BrowserAccessibilityManager
* manager
)
134 : AccessibilityEventRecorder(manager
) {
135 CHECK(!instance_
) << "There can be only one instance of"
136 << " WinAccessibilityEventMonitor at a time.";
138 win_event_hook_handle_
= SetWinEventHook(
141 GetModuleHandle(NULL
),
142 &AccessibilityEventRecorderWin::WinEventHookThunk
,
143 GetCurrentProcessId(),
144 0, // Hook all threads
146 CHECK(win_event_hook_handle_
);
149 AccessibilityEventRecorderWin::~AccessibilityEventRecorderWin() {
150 UnhookWinEvent(win_event_hook_handle_
);
154 void AccessibilityEventRecorderWin::OnWinEventHook(
155 HWINEVENTHOOK handle
,
162 base::win::ScopedComPtr
<IAccessible
> browser_accessible
;
163 HRESULT hr
= AccessibleObjectFromWindowWrapper(
167 reinterpret_cast<void**>(browser_accessible
.Receive()));
168 if (!SUCCEEDED(hr
)) {
169 // Note: our event hook will pick up some superfluous events we
170 // don't care about, so it's safe to just ignore these failures.
171 // Same below for other HRESULT checks.
172 VLOG(1) << "Ignoring result " << hr
<< " from AccessibleObjectFromWindow";
176 base::win::ScopedVariant
childid_variant(child_id
);
177 base::win::ScopedComPtr
<IDispatch
> dispatch
;
178 hr
= browser_accessible
->get_accChild(childid_variant
, dispatch
.Receive());
179 if (!SUCCEEDED(hr
) || !dispatch
) {
180 VLOG(1) << "Ignoring result " << hr
<< " and result " << dispatch
181 << " from get_accChild";
185 base::win::ScopedComPtr
<IAccessible
> iaccessible
;
186 hr
= dispatch
.QueryInterface(iaccessible
.Receive());
187 if (!SUCCEEDED(hr
)) {
188 VLOG(1) << "Ignoring result " << hr
<< " from QueryInterface";
192 std::string event_str
= AccessibilityEventToStringUTF8(event
);
193 if (event_str
.empty()) {
194 VLOG(1) << "Ignoring event " << event
;
198 base::win::ScopedVariant
childid_self(CHILDID_SELF
);
199 base::win::ScopedVariant role
;
200 iaccessible
->get_accRole(childid_self
, role
.Receive());
201 base::win::ScopedBstr name_bstr
;
202 iaccessible
->get_accName(childid_self
, name_bstr
.Receive());
203 base::win::ScopedBstr value_bstr
;
204 iaccessible
->get_accValue(childid_self
, value_bstr
.Receive());
206 std::string log
= base::StringPrintf(
207 "%s on role=%s", event_str
.c_str(), RoleVariantToString(role
).c_str());
208 if (name_bstr
.Length() > 0)
209 log
+= base::StringPrintf(" name=\"%s\"", BstrToUTF8(name_bstr
).c_str());
210 if (value_bstr
.Length() > 0)
211 log
+= base::StringPrintf(" value=\"%s\"", BstrToUTF8(value_bstr
).c_str());
213 // For TEXT_REMOVED and TEXT_INSERTED events, query the text that was
214 // inserted or removed and include that in the log.
215 IAccessibleText
* accessible_text
;
216 hr
= QueryIAccessibleText(iaccessible
.get(), &accessible_text
);
218 if (event
== IA2_EVENT_TEXT_REMOVED
) {
219 IA2TextSegment old_text
;
220 if (SUCCEEDED(accessible_text
->get_oldText(&old_text
))) {
221 log
+= base::StringPrintf(" old_text={'%s' start=%d end=%d}",
222 BstrToUTF8(old_text
.text
).c_str(),
227 if (event
== IA2_EVENT_TEXT_INSERTED
) {
228 IA2TextSegment new_text
;
229 if (SUCCEEDED(accessible_text
->get_newText(&new_text
))) {
230 log
+= base::StringPrintf(" new_text={'%s' start=%d end=%d}",
231 BstrToUTF8(new_text
.text
).c_str(),
238 event_logs_
.push_back(log
);
241 HRESULT
AccessibilityEventRecorderWin::AccessibleObjectFromWindowWrapper(
242 HWND hwnd
, DWORD dw_id
, REFIID riid
, void** ppv_object
) {
243 HRESULT hr
= ::AccessibleObjectFromWindow(hwnd
, dw_id
, riid
, ppv_object
);
247 // The above call to ::AccessibleObjectFromWindow fails for unknown
248 // reasons every once in a while on the bots. Work around it by grabbing
249 // the object directly from the BrowserAccessibilityManager.
250 HWND accessibility_hwnd
=
251 manager_
->delegate()->AccessibilityGetAcceleratedWidget();
252 if (accessibility_hwnd
!= hwnd
)
255 IAccessible
* obj
= manager_
->GetRoot()->ToBrowserAccessibilityWin();
261 } // namespace content