Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(66)

Side by Side Diff: components/keyboard_lock/key_hook_state_keeper.cc

Issue 2879033002: Keyboard Lock Host implementation
Patch Set: Remove useless files Created 3 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/key_hook_state_keeper.h"
6
7 #include <utility>
8
9 #include "base/logging.h"
10
11 namespace keyboard_lock {
12
13 KeyHookStateKeeper::KeyHookStateKeeper(std::unique_ptr<KeyEventFilter> filter,
14 std::unique_ptr<KeyHook> key_hook)
15 : filter_(std::move(filter)),
16 key_hook_(std::move(key_hook)),
17 states_(256),
18 active_(false) {
19 DCHECK(filter_);
20 DCHECK(key_hook_);
21 }
22
23 KeyHookStateKeeper::~KeyHookStateKeeper() {
24 // Ensure all keyboard lock are correctly deactivated.
25 Deactivate(base::Callback<void(bool)>());
26 }
27
28 bool KeyHookStateKeeper::OnKeyDown(ui::KeyboardCode code, int flags) {
29 if (code < 0 || static_cast<size_t>(code) >= states_.size()) {
30 return false;
31 }
32 LOG(ERROR) << "Received OnKeyDown " << code << " - " << (states_[code] ? "Acce pt" : "Reject");
33 return states_[code] && filter_->OnKeyDown(code, flags);
34 }
35
36 bool KeyHookStateKeeper::OnKeyUp(ui::KeyboardCode code, int flags) {
37 if (code < 0 || static_cast<size_t>(code) >= states_.size()) {
38 return false;
39 }
40 LOG(ERROR) << "Received OnKeyUp " << code << " - " << (states_[code] ? "Accept " : "Reject");
41 return states_[code] && filter_->OnKeyUp(code, flags);
42 }
43
44 void KeyHookStateKeeper::RegisterKey(const std::vector<ui::KeyboardCode>& codes,
45 base::Callback<void(bool)> on_result) {
46 if (active_) {
47 // Registers unregisted keyboard codes.
48 std::vector<ui::KeyboardCode> unregistered_codes;
49 for (const ui::KeyboardCode code : codes) {
50 if (!states_[code]) {
51 unregistered_codes.push_back(code);
52 states_[code] = true;
53 LOG(ERROR) << "Enable " << code;
54 }
55 }
56
57 if (unregistered_codes.empty()) {
58 if (on_result) {
59 on_result.Run(true);
60 }
61 } else {
62 key_hook_->RegisterKey(unregistered_codes, on_result);
63 }
64 return;
65 }
66
67 for (const ui::KeyboardCode code : codes) {
68 states_[code] = true;
69 }
70 if (on_result) {
71 on_result.Run(true);
72 }
73 }
74
75 void KeyHookStateKeeper::UnregisterKey(
76 const std::vector<ui::KeyboardCode>& codes,
77 base::Callback<void(bool)> on_result) {
78 if (active_) {
79 // Unregistes registered keyboard codes.
80 std::vector<ui::KeyboardCode> registered_codes;
81 for (const ui::KeyboardCode code : codes) {
82 if (states_[code]) {
83 registered_codes.push_back(code);
84 states_[code] = false;
85 LOG(ERROR) << "Disable " << code;
86 }
87 }
88
89 if (registered_codes.empty()) {
90 if (on_result) {
91 on_result.Run(true);
92 }
93 } else {
94 key_hook_->UnregisterKey(registered_codes, on_result);
95 }
96 return;
97 }
98
99 for (const ui::KeyboardCode code : codes) {
100 states_[code] = false;
101 }
102 if (on_result) {
103 on_result.Run(true);
104 }
105 }
106
107 void KeyHookStateKeeper::Activate(base::Callback<void(bool)> on_result) {
108 if (active_) {
109 if (on_result) {
110 on_result.Run(true);
111 }
112 return;
113 }
114
115 std::vector<ui::KeyboardCode> registered_codes;
116 for (size_t i = 0; i < states_.size(); i++) {
117 if (states_[i]) {
118 registered_codes.push_back(static_cast<ui::KeyboardCode>(i));
119 }
120 }
121
122 if (registered_codes.empty()) {
123 if (on_result) {
124 on_result.Run(true);
125 }
126 } else {
127 key_hook_->RegisterKey(registered_codes, on_result);
128 }
129 active_ = true;
130 }
131
132 void KeyHookStateKeeper::Deactivate(base::Callback<void(bool)> on_result) {
133 if (!active_) {
134 if (on_result) {
135 on_result.Run(true);
136 }
137 return;
138 }
139
140 std::vector<ui::KeyboardCode> registered_codes;
141 for (size_t i = 0; i < states_.size(); i++) {
142 if (states_[i]) {
143 registered_codes.push_back(static_cast<ui::KeyboardCode>(i));
144 }
145 }
146
147 if (registered_codes.empty()) {
148 if (on_result) {
149 on_result.Run(true);
150 }
151 } else {
152 key_hook_->UnregisterKey(registered_codes, on_result);
153 }
154 active_ = false;
155 }
156
157 } // namespace keyboard_lock
OLDNEW
« no previous file with comments | « components/keyboard_lock/key_hook_state_keeper.h ('k') | components/keyboard_lock/key_hook_state_keeper_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698