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