| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 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 "ui/views/widget/desktop_aura/desktop_keyboard_capture_win.h" |
| 6 |
| 7 #include <map> |
| 8 |
| 9 #include "base/containers/scoped_ptr_hash_map.h" |
| 10 #include "base/logging.h" |
| 11 #include "base/macros.h" |
| 12 #include "base/memory/singleton.h" |
| 13 #include "base/message_loop/message_loop.h" |
| 14 |
| 15 namespace { |
| 16 // Some helper routines used to construct keyboard event. |
| 17 |
| 18 // Return true of WPARAM corresponds to a UP keyboard event. |
| 19 bool IsKeyUp(WPARAM w_param) { |
| 20 return (w_param == WM_KEYUP) || (w_param == WM_SYSKEYUP); |
| 21 } |
| 22 |
| 23 // Check if the given bit is set. |
| 24 bool IsBitSet(ULONG value, ULONG mask) { |
| 25 return ((value & mask) != 0); |
| 26 } |
| 27 |
| 28 // Construct LPARAM corresponding to the given low level hook callback |
| 29 // structure. |
| 30 LPARAM GetLParamFromHookStruct(WPARAM w_param, KBDLLHOOKSTRUCT hook_struct) { |
| 31 ULONG key_state = 0; |
| 32 // There is no way to get repeat count so always set it to 1. |
| 33 key_state = 1; |
| 34 |
| 35 // Scan code. |
| 36 key_state |= (hook_struct.scanCode & 0xFF) << 16; |
| 37 |
| 38 // Extended key when the event is received as part window event and so skip |
| 39 // it. |
| 40 |
| 41 // Context code. |
| 42 key_state |= IsBitSet(hook_struct.flags, LLKHF_ALTDOWN) << 29; |
| 43 |
| 44 // Previous key state - set to 1 for KEYUP events. |
| 45 key_state |= IsKeyUp(w_param) << 30; |
| 46 |
| 47 // Transition state. |
| 48 key_state |= IsBitSet(hook_struct.flags, LLKHF_UP) << 31; |
| 49 return static_cast<LPARAM>(key_state); |
| 50 } |
| 51 |
| 52 // Return the location independent keycode corresponding to given keycode (e.g. |
| 53 // return shift when left/right shift is pressed). This is needed as low level |
| 54 // hooks get location information which is not returned as part of normal window |
| 55 // keyboard events. |
| 56 DWORD RemoveLocationOnKeycode(DWORD vk_code) { |
| 57 // Virtual keycode from low level hook include location while window messages |
| 58 // does not. So convert them to be without location. |
| 59 switch (vk_code) { |
| 60 case VK_LSHIFT: |
| 61 case VK_RSHIFT: |
| 62 return VK_SHIFT; |
| 63 case VK_LCONTROL: |
| 64 case VK_RCONTROL: |
| 65 return VK_CONTROL; |
| 66 case VK_LMENU: |
| 67 case VK_RMENU: |
| 68 return VK_MENU; |
| 69 } |
| 70 return vk_code; |
| 71 } |
| 72 |
| 73 // Update thread keyboard state so GetKeyboardStatus() works correctly. Thread |
| 74 // keyboard state is not updated when low level hook is used to intercept |
| 75 // keyboard events. |
| 76 bool UpdateThreadKeyboardState() { |
| 77 const int kKeyboardStateLength = 256; |
| 78 BYTE keyboard_state[kKeyboardStateLength]; |
| 79 if (!GetKeyboardState(keyboard_state)) { |
| 80 DVLOG(ERROR) << "Error getting keyboard state"; |
| 81 return false; |
| 82 } |
| 83 |
| 84 int keys_to_update[] = {VK_SHIFT, VK_CONTROL, VK_MENU}; |
| 85 for (int index = 0; index < arraysize(keys_to_update); index++) { |
| 86 int key = keys_to_update[index]; |
| 87 SHORT key_state = GetAsyncKeyState(key); |
| 88 keyboard_state[key] = (IsBitSet(key_state, 0x8000) ? 0x80 : 0) | |
| 89 (IsBitSet(key_state, 0x1) ? 1 : 0); |
| 90 } |
| 91 |
| 92 if (!SetKeyboardState(keyboard_state)) { |
| 93 DVLOG(ERROR) << "Error setting keyboard state"; |
| 94 return false; |
| 95 } |
| 96 |
| 97 return true; |
| 98 } |
| 99 } |
| 100 |
| 101 namespace views { |
| 102 |
| 103 // Maintains low level registration for a window. |
| 104 class KeyboardInterceptRegistration { |
| 105 public: |
| 106 KeyboardInterceptRegistration() : hook_handle_(NULL) {} |
| 107 |
| 108 ~KeyboardInterceptRegistration() { |
| 109 if (hook_handle_ != NULL) |
| 110 Unhook(); |
| 111 } |
| 112 |
| 113 // Register for low level hook. |
| 114 bool Hook(HOOKPROC callback_function) { |
| 115 // Make sure that hook is set from main thread as it has to be valid for |
| 116 // the lifetime of the registration. |
| 117 DCHECK(base::MessageLoopForUI::IsCurrent()); |
| 118 DCHECK(hook_handle_ == NULL) << "Keyboard hook already registered"; |
| 119 hook_handle_ = SetWindowsHookEx(WH_KEYBOARD_LL, callback_function, NULL, 0); |
| 120 if (hook_handle_ == NULL) { |
| 121 DVLOG(ERROR) << "Error calling SetWindowsHookEx() - GLE = " |
| 122 << GetLastError(); |
| 123 return false; |
| 124 } |
| 125 return true; |
| 126 } |
| 127 |
| 128 // Unhook registered hook. |
| 129 bool Unhook() { |
| 130 DCHECK(hook_handle_ != NULL) << "Unhook called without registring hooks"; |
| 131 BOOL result = UnhookWindowsHookEx(hook_handle_); |
| 132 if (!result) { |
| 133 DVLOG(ERROR) << "Error calling UnhookWindowsHookEx() - GLE = " |
| 134 << GetLastError(); |
| 135 return false; |
| 136 } |
| 137 hook_handle_ = NULL; |
| 138 return true; |
| 139 } |
| 140 |
| 141 private: |
| 142 // Hook returned when it was installed. |
| 143 HHOOK hook_handle_; |
| 144 |
| 145 DISALLOW_COPY_AND_ASSIGN(KeyboardInterceptRegistration); |
| 146 }; |
| 147 |
| 148 // Implements low level hook and manages registration for all the windows. |
| 149 class LowLevelHookHandler { |
| 150 public: |
| 151 // Request all keyboard events to be routed to the given window. |
| 152 bool Register(HWND window_handle); |
| 153 |
| 154 // Release the request for all keyboard events. |
| 155 void Deregister(HWND window_handle); |
| 156 |
| 157 // Get singleton instance. |
| 158 static LowLevelHookHandler* GetInstance(); |
| 159 |
| 160 private: |
| 161 // Private constructor/destructor so it is accessible only |
| 162 // DefaultSingletonTraits. |
| 163 friend struct DefaultSingletonTraits<LowLevelHookHandler>; |
| 164 LowLevelHookHandler() {} |
| 165 ~LowLevelHookHandler() {} |
| 166 |
| 167 // Check if window handle is registered to intercept keyboard events. |
| 168 bool IsRegistered(HWND handle); |
| 169 |
| 170 // Low level keyboard hook processing related functions. |
| 171 // Hook callback called from the OS. |
| 172 static LRESULT CALLBACK |
| 173 KeyboardHook(int code, WPARAM w_param, LPARAM l_param); |
| 174 |
| 175 // There is no lock protecting this list as the low level hook callbacks are |
| 176 // executed on same thread that registered the hook and there is only one |
| 177 // thread |
| 178 // that execute all view code in browser. |
| 179 base::ScopedPtrHashMap<HWND, KeyboardInterceptRegistration> registrations_; |
| 180 }; |
| 181 |
| 182 // static |
| 183 LowLevelHookHandler* LowLevelHookHandler::GetInstance() { |
| 184 return Singleton<LowLevelHookHandler, |
| 185 DefaultSingletonTraits<LowLevelHookHandler> >::get(); |
| 186 } |
| 187 |
| 188 bool LowLevelHookHandler::Register(HWND window_handle) { |
| 189 if (registrations_.contains(window_handle)) |
| 190 return false; |
| 191 |
| 192 scoped_ptr<KeyboardInterceptRegistration> registration( |
| 193 new KeyboardInterceptRegistration()); |
| 194 if (registration->Hook(KeyboardHook)) { |
| 195 if (registrations_.add(window_handle, registration.Pass()).second) |
| 196 return true; |
| 197 } |
| 198 return false; |
| 199 } |
| 200 |
| 201 void LowLevelHookHandler::Deregister(HWND window_handle) { |
| 202 registrations_.erase(window_handle); |
| 203 DVLOG(1) << "Keyboard hook unregistered for handle = " << window_handle; |
| 204 } |
| 205 |
| 206 bool LowLevelHookHandler::IsRegistered(HWND handle) { |
| 207 return registrations_.contains(handle); |
| 208 } |
| 209 |
| 210 // static |
| 211 LRESULT CALLBACK |
| 212 LowLevelHookHandler::KeyboardHook(int code, WPARAM w_param, LPARAM l_param) { |
| 213 HWND current_active_window = GetActiveWindow(); |
| 214 if ((code >= 0) && GetInstance()->IsRegistered(current_active_window)) { |
| 215 UpdateThreadKeyboardState(); |
| 216 |
| 217 KBDLLHOOKSTRUCT hook_struct = *reinterpret_cast<KBDLLHOOKSTRUCT*>(l_param); |
| 218 PostMessage(current_active_window, |
| 219 w_param, |
| 220 RemoveLocationOnKeycode(hook_struct.vkCode), |
| 221 GetLParamFromHookStruct(w_param, hook_struct)); |
| 222 |
| 223 return 1; |
| 224 } |
| 225 |
| 226 return CallNextHookEx(NULL, code, w_param, l_param); |
| 227 } |
| 228 |
| 229 DesktopKeyboardCaptureWin::DesktopKeyboardCaptureWin(HWND window_handle) |
| 230 : window_handle_(window_handle) { |
| 231 LowLevelHookHandler::GetInstance()->Register(window_handle_); |
| 232 } |
| 233 |
| 234 DesktopKeyboardCaptureWin::~DesktopKeyboardCaptureWin() { |
| 235 LowLevelHookHandler::GetInstance()->Deregister(window_handle_); |
| 236 } |
| 237 |
| 238 } // namespace views |
| OLD | NEW |