OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | |
David Tseng
2014/12/16 16:53:05
Ditto.
dmazzoni
2014/12/16 23:26:32
Done.
| |
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/browser/accessibility/accessibility_event_recorder.h" | |
6 | |
7 #include <oleacc.h> | |
8 | |
9 #include <string> | |
10 | |
11 #include "base/strings/string_number_conversions.h" | |
12 #include "base/strings/string_util.h" | |
13 #include "base/strings/utf_string_conversions.h" | |
14 #include "base/win/scoped_bstr.h" | |
15 #include "base/win/scoped_comptr.h" | |
16 #include "base/win/scoped_variant.h" | |
17 #include "content/browser/accessibility/accessibility_tree_formatter_utils_win.h " | |
18 #include "content/browser/accessibility/browser_accessibility_manager.h" | |
19 #include "content/browser/accessibility/browser_accessibility_win.h" | |
20 #include "third_party/iaccessible2/ia2_api_all.h" | |
21 #include "ui/base/win/atl_module.h" | |
22 | |
23 namespace content { | |
24 | |
25 namespace { | |
26 | |
27 std::string RoleVariantToString(const base::win::ScopedVariant& role) { | |
28 if (role.type() == VT_I4) { | |
29 return base::UTF16ToUTF8(IAccessibleRoleToString(V_I4(&role))); | |
30 } else if (role.type() == VT_BSTR) { | |
31 return base::UTF16ToUTF8( | |
32 base::string16(V_BSTR(&role), SysStringLen(V_BSTR(&role)))); | |
33 } | |
34 return std::string(); | |
35 } | |
36 | |
37 HRESULT QueryIAccessible2(IAccessible* accessible, IAccessible2** accessible2) { | |
38 base::win::ScopedComPtr<IServiceProvider> service_provider; | |
39 HRESULT hr = accessible->QueryInterface(service_provider.Receive()); | |
40 return SUCCEEDED(hr) ? | |
41 service_provider->QueryService(IID_IAccessible2, accessible2) : hr; | |
42 } | |
43 | |
44 } | |
David Tseng
2014/12/16 16:53:05
// namespace
dmazzoni
2014/12/16 23:26:32
Done.
| |
45 | |
46 class AccessibilityEventRecorderWin : public AccessibilityEventRecorder { | |
47 public: | |
48 explicit AccessibilityEventRecorderWin(BrowserAccessibilityManager* manager); | |
49 virtual ~AccessibilityEventRecorderWin(); | |
50 | |
51 static void CALLBACK WinEventHookThunk( | |
David Tseng
2014/12/16 16:53:05
nit: document
dmazzoni
2014/12/16 23:26:32
Done.
| |
52 HWINEVENTHOOK handle, | |
53 DWORD event, | |
54 HWND hwnd, | |
55 LONG obj_id, | |
56 LONG child_id, | |
57 DWORD event_thread, | |
58 DWORD event_time); | |
59 | |
60 private: | |
61 void OnWinEventHook(HWINEVENTHOOK handle, | |
62 DWORD event, | |
63 HWND hwnd, | |
64 LONG obj_id, | |
65 LONG child_id, | |
66 DWORD event_thread, | |
67 DWORD event_time); | |
68 | |
69 HWINEVENTHOOK win_event_hook_handle_; | |
70 static AccessibilityEventRecorderWin* instance_; | |
71 }; | |
72 | |
73 // static | |
74 AccessibilityEventRecorderWin* | |
75 AccessibilityEventRecorderWin::instance_ = nullptr; | |
76 | |
77 // static | |
78 AccessibilityEventRecorder* AccessibilityEventRecorder::Create( | |
79 BrowserAccessibilityManager* manager) { | |
80 return new AccessibilityEventRecorderWin(manager); | |
81 } | |
82 | |
83 // static | |
84 void CALLBACK AccessibilityEventRecorderWin::WinEventHookThunk( | |
85 HWINEVENTHOOK handle, | |
86 DWORD event, | |
87 HWND hwnd, | |
88 LONG obj_id, | |
89 LONG child_id, | |
90 DWORD event_thread, | |
91 DWORD event_time) { | |
92 if (instance_) { | |
93 instance_->OnWinEventHook(handle, event, hwnd, obj_id, child_id, | |
94 event_thread, event_time); | |
95 } | |
96 } | |
97 | |
98 AccessibilityEventRecorderWin::AccessibilityEventRecorderWin( | |
99 BrowserAccessibilityManager* manager) | |
100 : AccessibilityEventRecorder(manager) { | |
101 CHECK(!instance_) << "There can be only one instance of" | |
102 << " WinAccessibilityEventMonitor at a time."; | |
103 instance_ = this; | |
104 win_event_hook_handle_ = SetWinEventHook( | |
105 EVENT_MIN, | |
106 EVENT_MAX, | |
107 GetModuleHandle(NULL), | |
108 &AccessibilityEventRecorderWin::WinEventHookThunk, | |
109 GetCurrentProcessId(), | |
110 0, // Hook all threads | |
111 WINEVENT_INCONTEXT); | |
112 } | |
113 | |
114 AccessibilityEventRecorderWin::~AccessibilityEventRecorderWin() { | |
115 UnhookWinEvent(win_event_hook_handle_); | |
116 instance_ = NULL; | |
117 } | |
118 | |
119 void AccessibilityEventRecorderWin::OnWinEventHook( | |
120 HWINEVENTHOOK handle, | |
121 DWORD event, | |
122 HWND hwnd, | |
123 LONG obj_id, | |
124 LONG child_id, | |
125 DWORD event_thread, | |
126 DWORD event_time) { | |
127 base::win::ScopedComPtr<IAccessible> browser_accessible; | |
128 HRESULT hr = AccessibleObjectFromWindow( | |
129 hwnd, | |
130 obj_id, | |
131 IID_IAccessible, | |
132 reinterpret_cast<void**>(browser_accessible.Receive())); | |
133 CHECK(SUCCEEDED(hr)); | |
David Tseng
2014/12/16 16:53:05
Maybe print out a message on failure with the abov
dmazzoni
2014/12/16 23:26:32
Done.
| |
134 | |
135 base::win::ScopedVariant childid_variant(child_id); | |
136 base::win::ScopedComPtr<IDispatch> dispatch; | |
137 hr = browser_accessible->get_accChild(childid_variant, dispatch.Receive()); | |
138 CHECK(SUCCEEDED(hr)); | |
139 base::win::ScopedComPtr<IAccessible> iaccessible; | |
140 dispatch.QueryInterface(iaccessible.Receive()); | |
141 | |
142 base::win::ScopedVariant childid_self(CHILDID_SELF); | |
143 base::win::ScopedVariant role; | |
144 iaccessible->get_accRole(childid_self, role.Receive()); | |
145 base::win::ScopedBstr name_bstr; | |
146 iaccessible->get_accName(childid_self, name_bstr.Receive()); | |
147 base::win::ScopedBstr value_bstr; | |
148 iaccessible->get_accValue(childid_self, value_bstr.Receive()); | |
149 | |
150 std::string log = | |
151 base::UTF16ToUTF8(AccessibilityEventToString(event)) + | |
152 " on role=" + | |
153 RoleVariantToString(role); | |
154 base::string16 name(name_bstr, SysStringLen(name_bstr)); | |
155 if (!name.empty()) | |
156 log += " name=\"" + base::UTF16ToUTF8(name) + "\""; | |
157 base::string16 value(value_bstr, SysStringLen(value_bstr)); | |
158 if (!value.empty()) | |
159 log += " value=\"" + base::UTF16ToUTF8(value) + "\""; | |
David Tseng
2014/12/16 16:53:05
Could you do a string printf for each of these (or
dmazzoni
2014/12/16 23:26:32
Done.
| |
160 | |
161 event_logs_.push_back(log); | |
David Tseng
2014/12/16 16:53:05
Can you get these events out of order?
dmazzoni
2014/12/16 23:26:32
No, this is called synchronously.
| |
162 } | |
163 | |
164 } // namespace content | |
OLD | NEW |