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/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/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" |
| 23 |
| 24 namespace content { |
| 25 |
| 26 namespace { |
| 27 |
| 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)))); |
| 34 } |
| 35 return std::string(); |
| 36 } |
| 37 |
| 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; |
| 43 } |
| 44 |
| 45 std::string BstrToUTF8(BSTR bstr) { |
| 46 base::string16 str16(bstr, SysStringLen(bstr)); |
| 47 return base::UTF16ToUTF8(str16); |
| 48 } |
| 49 |
| 50 std::string AccessibilityEventToStringUTF8(int32 event_id) { |
| 51 return base::UTF16ToUTF8(AccessibilityEventToString(event_id)); |
| 52 } |
| 53 |
| 54 } // namespace |
| 55 |
| 56 class AccessibilityEventRecorderWin : public AccessibilityEventRecorder { |
| 57 public: |
| 58 explicit AccessibilityEventRecorderWin(BrowserAccessibilityManager* manager); |
| 59 virtual ~AccessibilityEventRecorderWin(); |
| 60 |
| 61 // Callback registered by SetWinEventHook. Just calls OnWinEventHook. |
| 62 static void CALLBACK WinEventHookThunk( |
| 63 HWINEVENTHOOK handle, |
| 64 DWORD event, |
| 65 HWND hwnd, |
| 66 LONG obj_id, |
| 67 LONG child_id, |
| 68 DWORD event_thread, |
| 69 DWORD event_time); |
| 70 |
| 71 private: |
| 72 // Called by the thunk registered by SetWinEventHook. Retrives accessibility |
| 73 // info about the node the event was fired on and appends a string to |
| 74 // the event log. |
| 75 void OnWinEventHook(HWINEVENTHOOK handle, |
| 76 DWORD event, |
| 77 HWND hwnd, |
| 78 LONG obj_id, |
| 79 LONG child_id, |
| 80 DWORD event_thread, |
| 81 DWORD event_time); |
| 82 |
| 83 HWINEVENTHOOK win_event_hook_handle_; |
| 84 static AccessibilityEventRecorderWin* instance_; |
| 85 }; |
| 86 |
| 87 // static |
| 88 AccessibilityEventRecorderWin* |
| 89 AccessibilityEventRecorderWin::instance_ = nullptr; |
| 90 |
| 91 // static |
| 92 AccessibilityEventRecorder* AccessibilityEventRecorder::Create( |
| 93 BrowserAccessibilityManager* manager) { |
| 94 return new AccessibilityEventRecorderWin(manager); |
| 95 } |
| 96 |
| 97 // static |
| 98 void CALLBACK AccessibilityEventRecorderWin::WinEventHookThunk( |
| 99 HWINEVENTHOOK handle, |
| 100 DWORD event, |
| 101 HWND hwnd, |
| 102 LONG obj_id, |
| 103 LONG child_id, |
| 104 DWORD event_thread, |
| 105 DWORD event_time) { |
| 106 if (instance_) { |
| 107 instance_->OnWinEventHook(handle, event, hwnd, obj_id, child_id, |
| 108 event_thread, event_time); |
| 109 } |
| 110 } |
| 111 |
| 112 AccessibilityEventRecorderWin::AccessibilityEventRecorderWin( |
| 113 BrowserAccessibilityManager* manager) |
| 114 : AccessibilityEventRecorder(manager) { |
| 115 CHECK(!instance_) << "There can be only one instance of" |
| 116 << " WinAccessibilityEventMonitor at a time."; |
| 117 instance_ = this; |
| 118 win_event_hook_handle_ = SetWinEventHook( |
| 119 EVENT_MIN, |
| 120 EVENT_MAX, |
| 121 GetModuleHandle(NULL), |
| 122 &AccessibilityEventRecorderWin::WinEventHookThunk, |
| 123 GetCurrentProcessId(), |
| 124 0, // Hook all threads |
| 125 WINEVENT_INCONTEXT); |
| 126 } |
| 127 |
| 128 AccessibilityEventRecorderWin::~AccessibilityEventRecorderWin() { |
| 129 UnhookWinEvent(win_event_hook_handle_); |
| 130 instance_ = NULL; |
| 131 } |
| 132 |
| 133 void AccessibilityEventRecorderWin::OnWinEventHook( |
| 134 HWINEVENTHOOK handle, |
| 135 DWORD event, |
| 136 HWND hwnd, |
| 137 LONG obj_id, |
| 138 LONG child_id, |
| 139 DWORD event_thread, |
| 140 DWORD event_time) { |
| 141 base::win::ScopedComPtr<IAccessible> browser_accessible; |
| 142 HRESULT hr = AccessibleObjectFromWindow( |
| 143 hwnd, |
| 144 obj_id, |
| 145 IID_IAccessible, |
| 146 reinterpret_cast<void**>(browser_accessible.Receive())); |
| 147 CHECK(SUCCEEDED(hr)) << "Failure accessing accessible object from hwnd " |
| 148 << hwnd << " obj_id=" << obj_id << " child_id=" |
| 149 << child_id; |
| 150 |
| 151 base::win::ScopedVariant childid_variant(child_id); |
| 152 base::win::ScopedComPtr<IDispatch> dispatch; |
| 153 hr = browser_accessible->get_accChild(childid_variant, dispatch.Receive()); |
| 154 CHECK(SUCCEEDED(hr)); |
| 155 base::win::ScopedComPtr<IAccessible> iaccessible; |
| 156 dispatch.QueryInterface(iaccessible.Receive()); |
| 157 |
| 158 base::win::ScopedVariant childid_self(CHILDID_SELF); |
| 159 base::win::ScopedVariant role; |
| 160 iaccessible->get_accRole(childid_self, role.Receive()); |
| 161 base::win::ScopedBstr name_bstr; |
| 162 iaccessible->get_accName(childid_self, name_bstr.Receive()); |
| 163 base::win::ScopedBstr value_bstr; |
| 164 iaccessible->get_accValue(childid_self, value_bstr.Receive()); |
| 165 |
| 166 std::string log = base::StringPrintf( |
| 167 "%s on role=%s", |
| 168 AccessibilityEventToStringUTF8(event).c_str(), |
| 169 RoleVariantToString(role).c_str()); |
| 170 if (name_bstr.Length() > 0) |
| 171 log += base::StringPrintf(" name=\"%s\"", BstrToUTF8(name_bstr).c_str()); |
| 172 if (value_bstr.Length() > 0) |
| 173 log += base::StringPrintf(" value=\"%s\"", BstrToUTF8(value_bstr).c_str()); |
| 174 |
| 175 event_logs_.push_back(log); |
| 176 } |
| 177 |
| 178 } // namespace content |
OLD | NEW |