Chromium Code Reviews| 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 #include "base/message_loop/message_pump_win.h" | |
| 15 | |
| 16 namespace { | |
| 17 // Some helper routines used to construct keyboard event. | |
| 18 | |
| 19 // Return true of WPARAM corresponds to a UP keyboard event. | |
| 20 bool IsKeyUp(WPARAM w_param) { | |
| 21 return (w_param == WM_KEYUP) || (w_param == WM_SYSKEYUP); | |
| 22 } | |
| 23 | |
| 24 // Check if the given bit is set. | |
| 25 bool IsBitSet(ULONG value, ULONG mask) { | |
| 26 return ((value & mask) != 0); | |
| 27 } | |
| 28 | |
| 29 // Return the location independent keycode corresponding to given keycode (e.g. | |
| 30 // return shift when left/right shift is pressed). This is needed as low level | |
| 31 // hooks get location information which is not returned as part of normal window | |
| 32 // keyboard events. | |
| 33 DWORD RemoveLocationOnKeycode(DWORD vk_code) { | |
| 34 // Virtual keycode from low level hook include location while window messages | |
| 35 // does not. So convert them to be without location. | |
| 36 switch (vk_code) { | |
| 37 case VK_LSHIFT: | |
| 38 case VK_RSHIFT: | |
| 39 return VK_SHIFT; | |
| 40 case VK_LCONTROL: | |
| 41 case VK_RCONTROL: | |
| 42 return VK_CONTROL; | |
| 43 case VK_LMENU: | |
| 44 case VK_RMENU: | |
| 45 return VK_MENU; | |
| 46 } | |
| 47 return vk_code; | |
| 48 } | |
| 49 | |
| 50 // Construct LPARAM corresponding to the given low level hook callback | |
| 51 // structure. | |
| 52 LPARAM GetLParamFromHookStruct(WPARAM w_param, KBDLLHOOKSTRUCT* hook_struct) { | |
| 53 ULONG key_state = 0; | |
| 54 // There is no way to get repeat count so always set it to 1. | |
| 55 key_state = 1; | |
| 56 | |
| 57 // Scan code. | |
| 58 key_state |= (hook_struct->scanCode & 0xFF) << 16; | |
| 59 | |
| 60 // Extended key when the event is received as part window event and so skip | |
| 61 // it. | |
| 62 | |
| 63 // Context code. | |
| 64 key_state |= IsBitSet(hook_struct->flags, LLKHF_ALTDOWN) << 29; | |
| 65 | |
| 66 // Previous key state - set to 1 for KEYUP events. | |
| 67 key_state |= IsKeyUp(w_param) << 30; | |
| 68 | |
| 69 // Transition state. | |
| 70 key_state |= IsBitSet(hook_struct->flags, LLKHF_UP) << 31; | |
| 71 | |
| 72 return static_cast<LPARAM>(key_state); | |
| 73 } | |
| 74 | |
| 75 // List of key state that we want to save. | |
| 76 int keys_to_save[] = {VK_SHIFT, VK_CONTROL, VK_MENU}; | |
| 77 | |
| 78 // Make sure that we are not going to run out of bits saving the state. | |
| 79 C_ASSERT((arraysize(keys_to_save) * 2) <= (sizeof(WPARAM) * 8)); | |
| 80 | |
| 81 // Save keyboard state to WPARAM so it can be restored later before the keyboard | |
| 82 // message is processed in the main thread. This is necessary for | |
| 83 // GetKeyboardState() to work as keyboard state will be different by the time | |
| 84 // main thread processes the message. | |
| 85 WPARAM SaveKeyboardState() { | |
| 86 WPARAM value = 0; | |
| 87 | |
| 88 for (int index = 0; index < arraysize(keys_to_save); index++) { | |
| 89 value <<= 2; | |
| 90 SHORT key_state = GetAsyncKeyState(keys_to_save[index]); | |
| 91 value |= ((IsBitSet(key_state, 0x8000) ? 0x2 : 0) | | |
| 92 (IsBitSet(key_state, 0x1) ? 0x1 : 0)); | |
| 93 } | |
| 94 return value; | |
| 95 } | |
| 96 | |
| 97 // Restore keyboard state based on saved values. | |
| 98 bool RestoreKeyboardState(WPARAM w_param) { | |
| 99 const int kKeyboardStateLength = 256; | |
| 100 BYTE keyboard_state[kKeyboardStateLength]; | |
| 101 if (!GetKeyboardState(keyboard_state)) { | |
| 102 DVLOG(ERROR) << "Error getting keyboard state"; | |
| 103 return false; | |
| 104 } | |
| 105 | |
| 106 // restore in the reverse order of what was saved so we have the right bit for | |
| 107 // each key that was saved. | |
| 108 for (int index = arraysize(keys_to_save) - 1; index >= 0; index--) { | |
| 109 int key = keys_to_save[index]; | |
| 110 keyboard_state[key] = | |
| 111 (IsBitSet(w_param, 0x2) ? 0x80 : 0) | (IsBitSet(w_param, 0x1) ? 1 : 0); | |
| 112 w_param >>= 2; | |
| 113 } | |
| 114 | |
| 115 if (!SetKeyboardState(keyboard_state)) { | |
| 116 DVLOG(ERROR) << "Error setting keyboard state"; | |
| 117 return false; | |
| 118 } | |
| 119 | |
| 120 return true; | |
| 121 } | |
| 122 | |
| 123 // Data corresponding to keyboard event. | |
| 124 struct KeyboardEventInfo { | |
| 125 UINT message_id; | |
| 126 WPARAM event_w_param; | |
| 127 LPARAM event_l_param; | |
| 128 WPARAM keyboard_state_to_restore; | |
| 129 }; | |
| 130 | |
| 131 // Maintains low level registration for a window. | |
| 132 class KeyboardInterceptRegistration { | |
| 133 public: | |
| 134 KeyboardInterceptRegistration(); | |
| 135 | |
| 136 ~KeyboardInterceptRegistration(); | |
| 137 | |
| 138 // Register for low level hook. | |
| 139 bool Hook(HOOKPROC callback_function); | |
| 140 | |
| 141 // Unhook registered hook. | |
| 142 bool Unhook(); | |
| 143 | |
| 144 // Are there any keyboard events queued. | |
| 145 bool IsKeyboardEventQueueEmpty(); | |
| 146 | |
| 147 // Insert keyboard event in the queue. | |
| 148 void QueueKeyboardEvent(const KeyboardEventInfo& info); | |
| 149 | |
| 150 KeyboardEventInfo DequeueKeyboardEvent(); | |
| 151 | |
| 152 private: | |
| 153 std::queue<KeyboardEventInfo> keyboard_events_; | |
| 154 | |
| 155 // Hook returned when it was installed. | |
| 156 HHOOK hook_handle_; | |
| 157 | |
| 158 DISALLOW_COPY_AND_ASSIGN(KeyboardInterceptRegistration); | |
| 159 }; | |
| 160 | |
| 161 // Implements low level hook and manages registration for all the windows. | |
| 162 class LowLevelHookHandler { | |
| 163 public: | |
| 164 // Request all keyboard events to be routed to the given window. | |
| 165 void Register(HWND window_handle); | |
| 166 | |
| 167 // Release the request for all keyboard events. | |
| 168 void Deregister(HWND window_handle); | |
| 169 | |
| 170 // Get singleton instance. | |
| 171 static LowLevelHookHandler* GetInstance(); | |
| 172 | |
| 173 private: | |
| 174 // Private constructor/destructor so it is accessible only | |
| 175 // DefaultSingletonTraits. | |
| 176 friend struct DefaultSingletonTraits<LowLevelHookHandler>; | |
| 177 LowLevelHookHandler(); | |
| 178 | |
| 179 ~LowLevelHookHandler(); | |
| 180 | |
| 181 // Low level keyboard hook processing related functions. | |
| 182 // Hook callback called from the OS. | |
| 183 static LRESULT CALLBACK | |
| 184 KeyboardHook(int code, WPARAM w_param, LPARAM l_param); | |
| 185 | |
| 186 // Low level keyboard hook handler. | |
| 187 LRESULT HandleKeyboardHook(int code, WPARAM w_param, LPARAM l_param); | |
| 188 | |
| 189 // Message filter to set keyboard state based on private message. | |
| 190 static LRESULT CALLBACK | |
| 191 MessageFilterHook(int code, WPARAM w_param, LPARAM l_param); | |
| 192 | |
| 193 // Message filter handler. | |
| 194 LRESULT HandleMessageFilterHook(int code, WPARAM w_param, LPARAM l_param); | |
| 195 | |
| 196 bool RegisterMessageFilter(); | |
| 197 void UnregisterMessageFilter(); | |
| 198 | |
| 199 // Hook handle for window message to set keyboard state based on private | |
| 200 // message. | |
| 201 HHOOK message_filter_hook_; | |
| 202 | |
| 203 // Private window message to set keyboard state for the thread. | |
| 204 UINT restore_keyboard_state_message_id_; | |
| 205 | |
| 206 // Private message to inject keyboard event after current injected message is | |
| 207 // processed. | |
| 208 UINT inject_keyboard_event_message_id_; | |
| 209 | |
| 210 // There is no lock protecting this list as the low level hook callbacks are | |
| 211 // executed on same thread that registered the hook and there is only one | |
| 212 // thread | |
| 213 // that execute all view code in browser. | |
| 214 base::ScopedPtrHashMap<HWND, KeyboardInterceptRegistration> registrations_; | |
| 215 }; | |
| 216 | |
| 217 KeyboardInterceptRegistration::KeyboardInterceptRegistration() | |
| 218 : hook_handle_(NULL) { | |
| 219 } | |
| 220 | |
| 221 KeyboardInterceptRegistration::~KeyboardInterceptRegistration() { | |
| 222 if (hook_handle_ != NULL) | |
| 223 Unhook(); | |
| 224 } | |
| 225 | |
| 226 bool KeyboardInterceptRegistration::Hook(HOOKPROC callback_function) { | |
| 227 // Make sure that hook is set from main thread as it has to be valid for | |
| 228 // the lifetime of the registration. | |
| 229 DCHECK(base::MessageLoopForUI::IsCurrent()); | |
| 230 DCHECK(hook_handle_ == NULL) << "Keyboard hook already registered"; | |
| 231 hook_handle_ = SetWindowsHookEx(WH_KEYBOARD_LL, callback_function, NULL, 0); | |
| 232 if (hook_handle_ == NULL) { | |
| 233 DVLOG(ERROR) << "Error calling SetWindowsHookEx() - GLE = " | |
| 234 << GetLastError(); | |
| 235 return false; | |
| 236 } | |
| 237 return true; | |
| 238 } | |
| 239 | |
| 240 bool KeyboardInterceptRegistration::Unhook() { | |
| 241 DCHECK(hook_handle_ != NULL) << "Unhook called without registring hooks"; | |
| 242 BOOL result = UnhookWindowsHookEx(hook_handle_); | |
| 243 if (!result) { | |
| 244 DVLOG(ERROR) << "Error calling UnhookWindowsHookEx() - GLE = " | |
| 245 << GetLastError(); | |
| 246 return false; | |
| 247 } | |
| 248 hook_handle_ = NULL; | |
| 249 return true; | |
| 250 } | |
| 251 | |
| 252 bool KeyboardInterceptRegistration::IsKeyboardEventQueueEmpty() { | |
| 253 return keyboard_events_.empty(); | |
| 254 } | |
| 255 | |
| 256 void KeyboardInterceptRegistration::QueueKeyboardEvent( | |
| 257 const KeyboardEventInfo& info) { | |
| 258 keyboard_events_.push(info); | |
| 259 } | |
| 260 | |
| 261 KeyboardEventInfo KeyboardInterceptRegistration::DequeueKeyboardEvent() { | |
| 262 KeyboardEventInfo info = keyboard_events_.front(); | |
| 263 keyboard_events_.pop(); | |
| 264 return info; | |
| 265 } | |
| 266 | |
| 267 LowLevelHookHandler::LowLevelHookHandler() | |
| 268 : message_filter_hook_(NULL), | |
| 269 restore_keyboard_state_message_id_(0), | |
| 270 inject_keyboard_event_message_id_(0) { | |
| 271 restore_keyboard_state_message_id_ = | |
| 272 RegisterWindowMessage(L"chrome:restore_keyboard_state"); | |
| 273 inject_keyboard_event_message_id_ = | |
| 274 RegisterWindowMessage(L"chrome:inject_keyboard_event"); | |
| 275 } | |
| 276 | |
| 277 LowLevelHookHandler::~LowLevelHookHandler() { | |
| 278 UnregisterMessageFilter(); | |
| 279 } | |
| 280 | |
| 281 // static | |
| 282 LRESULT CALLBACK | |
| 283 LowLevelHookHandler::KeyboardHook(int code, WPARAM w_param, LPARAM l_param) { | |
| 284 return GetInstance()->HandleKeyboardHook(code, w_param, l_param); | |
| 285 } | |
| 286 | |
| 287 // static | |
| 288 LRESULT CALLBACK LowLevelHookHandler::MessageFilterHook(int code, | |
| 289 WPARAM w_param, | |
| 290 LPARAM l_param) { | |
| 291 return GetInstance()->HandleMessageFilterHook(code, w_param, l_param); | |
| 292 } | |
| 293 | |
| 294 // static | |
| 295 LowLevelHookHandler* LowLevelHookHandler::GetInstance() { | |
| 296 return Singleton<LowLevelHookHandler, | |
| 297 DefaultSingletonTraits<LowLevelHookHandler> >::get(); | |
| 298 } | |
| 299 | |
| 300 void LowLevelHookHandler::Register(HWND window_handle) { | |
| 301 if (registrations_.contains(window_handle)) | |
| 302 return; | |
| 303 | |
| 304 if (!RegisterMessageFilter()) | |
| 305 return; | |
| 306 | |
| 307 scoped_ptr<KeyboardInterceptRegistration> registration( | |
| 308 new KeyboardInterceptRegistration()); | |
| 309 if (registration->Hook(KeyboardHook)) { | |
|
ananta
2014/08/12 17:51:49
We can record the window handle but register once
Sriram
2014/08/12 19:02:49
Done.
| |
| 310 registrations_.add(window_handle, registration.Pass()); | |
| 311 } | |
| 312 } | |
| 313 | |
| 314 void LowLevelHookHandler::Deregister(HWND window_handle) { | |
| 315 registrations_.erase(window_handle); | |
| 316 if (registrations_.empty()) | |
| 317 UnregisterMessageFilter(); | |
| 318 | |
| 319 DVLOG(1) << "Keyboard hook unregistered for handle = " << window_handle; | |
| 320 } | |
| 321 | |
| 322 bool LowLevelHookHandler::RegisterMessageFilter() { | |
| 323 if (message_filter_hook_) | |
| 324 return true; | |
| 325 | |
| 326 message_filter_hook_ = SetWindowsHookEx( | |
| 327 WH_MSGFILTER, MessageFilterHook, NULL, GetCurrentThreadId()); | |
| 328 if (message_filter_hook_ == NULL) { | |
| 329 DVLOG(ERROR) << "Error calling SetWindowsHookEx() to set message hook, " | |
| 330 << "gle = " << GetLastError(); | |
| 331 return false; | |
| 332 } | |
| 333 return true; | |
| 334 } | |
| 335 | |
| 336 void LowLevelHookHandler::UnregisterMessageFilter() { | |
| 337 if (message_filter_hook_ != NULL) { | |
| 338 UnhookWindowsHookEx(message_filter_hook_); | |
| 339 message_filter_hook_ = NULL; | |
| 340 } | |
| 341 } | |
| 342 | |
| 343 LRESULT | |
| 344 LowLevelHookHandler::HandleMessageFilterHook(int code, | |
| 345 WPARAM w_param, | |
| 346 LPARAM l_param) { | |
| 347 // Ignore if not called from main message loop. | |
| 348 if (code != base::MessagePumpForUI::kMessageFilterCode) | |
|
ananta
2014/08/12 17:51:49
Shouldn't we be calling CallNextHookEx here?
Sriram
2014/08/12 19:02:49
Done.
| |
| 349 return false; | |
| 350 | |
| 351 MSG* msg = reinterpret_cast<MSG*>(l_param); | |
| 352 if (msg->message == restore_keyboard_state_message_id_) { | |
| 353 RestoreKeyboardState(msg->wParam); | |
| 354 return true; | |
| 355 } else if (msg->message == inject_keyboard_event_message_id_) { | |
| 356 KeyboardInterceptRegistration* registration = registrations_.get(msg->hwnd); | |
| 357 if (registration) { | |
| 358 // Post keyboard state and key event to main thread for processing. | |
| 359 KeyboardEventInfo event_info = registration->DequeueKeyboardEvent(); | |
| 360 PostMessage(msg->hwnd, | |
| 361 restore_keyboard_state_message_id_, | |
| 362 event_info.keyboard_state_to_restore, | |
| 363 static_cast<LPARAM>(0)); | |
| 364 | |
| 365 PostMessage(msg->hwnd, | |
| 366 event_info.message_id, | |
| 367 event_info.event_w_param, | |
| 368 event_info.event_l_param); | |
| 369 | |
| 370 if (!registration->IsKeyboardEventQueueEmpty()) { | |
| 371 // Post another inject keyboard event if there are more key events to | |
| 372 // process after the current injected event is processed. | |
| 373 PostMessage(msg->hwnd, | |
| 374 inject_keyboard_event_message_id_, | |
| 375 static_cast<WPARAM>(0), | |
| 376 static_cast<LPARAM>(0)); | |
| 377 } | |
| 378 | |
| 379 return true; | |
| 380 } | |
| 381 } | |
| 382 | |
| 383 return false; | |
| 384 } | |
| 385 | |
| 386 LRESULT | |
| 387 LowLevelHookHandler::HandleKeyboardHook(int code, | |
| 388 WPARAM w_param, | |
| 389 LPARAM l_param) { | |
| 390 HWND current_active_window = GetActiveWindow(); | |
| 391 | |
| 392 if (code >= 0) { | |
| 393 KeyboardInterceptRegistration* registration = | |
| 394 registrations_.get(current_active_window); | |
| 395 if (registration) { | |
| 396 // Save keyboard state to queue and post message to handle keyboard event | |
| 397 // if the queue is not empty. It is done this way as keyboard state should | |
| 398 // be preserved until char event corresponding to the keyboard event is | |
| 399 // handled (so correct alt/shift/control key state is set). Also | |
| 400 // SendMessage() cannot be used as it would bypass both message loop | |
| 401 // delegates and TransalateMessage() calls (which will inserts char | |
| 402 // events). | |
| 403 PKBDLLHOOKSTRUCT hook_struct = | |
| 404 reinterpret_cast<PKBDLLHOOKSTRUCT>(l_param); | |
| 405 | |
| 406 KeyboardEventInfo event_info = {0}; | |
| 407 event_info.message_id = w_param; | |
| 408 event_info.event_w_param = RemoveLocationOnKeycode(hook_struct->vkCode); | |
| 409 event_info.event_l_param = GetLParamFromHookStruct(w_param, hook_struct); | |
| 410 event_info.keyboard_state_to_restore = SaveKeyboardState(); | |
| 411 | |
| 412 bool should_queue_inject_event = | |
| 413 registration->IsKeyboardEventQueueEmpty(); | |
| 414 | |
| 415 registration->QueueKeyboardEvent(event_info); | |
| 416 if (should_queue_inject_event) { | |
| 417 PostMessage(current_active_window, | |
| 418 inject_keyboard_event_message_id_, | |
| 419 static_cast<WPARAM>(0), | |
| 420 static_cast<LPARAM>(0)); | |
| 421 } | |
| 422 return 1; | |
| 423 } | |
| 424 } | |
| 425 | |
| 426 return CallNextHookEx(NULL, code, w_param, l_param); | |
| 427 } | |
| 428 | |
| 429 } // namespace | |
| 430 | |
| 431 namespace views { | |
| 432 | |
| 433 DesktopKeyboardCaptureWin::DesktopKeyboardCaptureWin(HWND window_handle) | |
| 434 : window_handle_(window_handle) { | |
| 435 LowLevelHookHandler::GetInstance()->Register(window_handle_); | |
|
ananta
2014/08/12 17:51:49
Registering on a per window basis seems like overk
Sriram
2014/08/12 19:02:49
Changed to register low level hook per thread but
| |
| 436 } | |
| 437 | |
| 438 DesktopKeyboardCaptureWin::~DesktopKeyboardCaptureWin() { | |
| 439 LowLevelHookHandler::GetInstance()->Deregister(window_handle_); | |
| 440 } | |
| 441 | |
| 442 } // namespace views | |
| OLD | NEW |