OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 "components/keyboard_lock/platform_key_hook.h" |
| 6 |
| 7 #include <windows.h> |
| 8 |
| 9 #include <utility> |
| 10 |
| 11 #include "base/bind.h" |
| 12 #include "base/callback.h" |
| 13 #include "base/logging.h" |
| 14 #include "components/keyboard_lock/register_once_key_hook.h" |
| 15 #include "ui/events/platform/platform_event_types.h" |
| 16 |
| 17 namespace { |
| 18 |
| 19 class WinKeyHook final : public keyboard_lock::RegisterOnceKeyHook { |
| 20 public: |
| 21 WinKeyHook(base::Callback<bool(const ui::PlatformEvent&)>&& callback); |
| 22 ~WinKeyHook() = default; |
| 23 |
| 24 static WinKeyHook* instance(); |
| 25 |
| 26 private: |
| 27 static LRESULT ProcessKeyEvent(int code, WPARAM w_param, LPARAM l_param); |
| 28 bool Register() override; |
| 29 bool Unregister() override; |
| 30 |
| 31 static WinKeyHook* me; |
| 32 const base::Callback<bool(const ui::PlatformEvent&)> callback_; |
| 33 HHOOK hook_ = nullptr; |
| 34 }; |
| 35 |
| 36 // static |
| 37 WinKeyHook* WinKeyHook::me = nullptr; |
| 38 |
| 39 WinKeyHook::WinKeyHook( |
| 40 base::Callback<bool(const ui::PlatformEvent&)>&& callback) |
| 41 : callback_(std::move(callback)) { |
| 42 DCHECK(callback_); |
| 43 DCHECK(me == nullptr); |
| 44 me = this; |
| 45 } |
| 46 |
| 47 // static |
| 48 WinKeyHook* WinKeyHook::instance() { |
| 49 return me; |
| 50 } |
| 51 |
| 52 bool WinKeyHook::Register() { |
| 53 LOG(ERROR) << "Going to SetWindowsHookEx"; |
| 54 hook_ = SetWindowsHookEx( |
| 55 WH_KEYBOARD_LL, |
| 56 static_cast<HOOKPROC>(&WinKeyHook::ProcessKeyEvent), |
| 57 NULL, |
| 58 NULL); |
| 59 return hook_ != NULL; |
| 60 } |
| 61 |
| 62 bool WinKeyHook::Unregister() { |
| 63 LOG(ERROR) << "Going to UnhookWindowsHookEx"; |
| 64 if (hook_) { |
| 65 return UnhookWindowsHookEx(hook_) != 0; |
| 66 } |
| 67 return true; |
| 68 } |
| 69 |
| 70 // static |
| 71 LRESULT WinKeyHook::ProcessKeyEvent(int code, WPARAM w_param, LPARAM l_param) { |
| 72 DCHECK(me); |
| 73 KBDLLHOOKSTRUCT* ll_hooks = reinterpret_cast<KBDLLHOOKSTRUCT*>(l_param); |
| 74 MSG msg = { nullptr, |
| 75 w_param, |
| 76 ll_hooks->vkCode, |
| 77 (ll_hooks->scanCode << 16) | (ll_hooks->flags & 0xFFFF), |
| 78 ll_hooks->time }; |
| 79 if (code == 0 && me->callback_.Run(msg)) { |
| 80 return 1; |
| 81 } |
| 82 return CallNextHookEx(nullptr, code, w_param, l_param); |
| 83 } |
| 84 |
| 85 } // namespace |
| 86 |
| 87 namespace keyboard_lock { |
| 88 |
| 89 PlatformKeyHook::PlatformKeyHook(Browser* owner, KeyEventFilter* filter) |
| 90 : owner_(owner), |
| 91 filter_(filter) { |
| 92 if (!WinKeyHook::instance()) { |
| 93 new WinKeyHook(base::Bind(&PlatformKeyEventFilter::OnPlatformEvent, |
| 94 base::Unretained(&filter_))); |
| 95 } |
| 96 } |
| 97 |
| 98 PlatformKeyHook::~PlatformKeyHook() = default; |
| 99 |
| 100 bool PlatformKeyHook::RegisterKey(const std::vector<ui::KeyboardCode>& codes) { |
| 101 LOG(ERROR) << "Register " << codes.size() << " key codes"; |
| 102 return WinKeyHook::instance()->RegisterKey(codes); |
| 103 } |
| 104 |
| 105 bool PlatformKeyHook::UnregisterKey( |
| 106 const std::vector<ui::KeyboardCode>& codes) { |
| 107 LOG(ERROR) << "Unregister " << codes.size() << " key codes"; |
| 108 return WinKeyHook::instance()->UnregisterKey(codes); |
| 109 } |
| 110 |
| 111 } // namespace keyboard_lock |
OLD | NEW |