Commit 3118ff1d authored by zijiehe's avatar zijiehe Committed by Commit Bot

[System-Keyboard-Lock] Add KeycodeConverter::CodeStringToNativeKeycode() function

Native keycode is used in system keyboard lock feature to understand the input
requested keys from Javascript API.

Also for consistency, CodeToUsbKeycode() has been renamed to
CodeStringToUsbKeycode().

You will find the usage of the newly added function in,
https://codereview.chromium.org/2879033002/diff/510001/components/keyboard_lock/keyboard_lock_host.cc
function KeyboardLockHost::SetReservedKeyCodes().

For detail, please refer to the design doc at,
https://docs.google.com/document/d/1T9gJHYdA1VGZ6QHQeOu0hOacWWWJ7yNEt1VDbLju4bs/edit#heading=h.cgwemqs2j4ta

W3C Working Draft: https://garykac.github.io/system-keyboard-lock/
Intent to implement: https://groups.google.com/a/chromium.org/forum/#!msg/blink-dev/9pauQUAvrcw/lfbG7eunCAAJ

A prototype can be found at,
https://codereview.chromium.org/2879033002

BUG=680809

Review-Url: https://codereview.chromium.org/2910873002
Cr-Commit-Position: refs/heads/master@{#476544}
parent 0eacfb43
...@@ -97,7 +97,8 @@ protocol::KeyEvent MakeKeyEvent(const pp::KeyboardInputEvent& pp_key_event) { ...@@ -97,7 +97,8 @@ protocol::KeyEvent MakeKeyEvent(const pp::KeyboardInputEvent& pp_key_event) {
} else if (dom_code == "OSRight") { } else if (dom_code == "OSRight") {
dom_code = "MetaRight"; dom_code = "MetaRight";
} }
key_event.set_usb_keycode(ui::KeycodeConverter::CodeToUsbKeycode(dom_code)); key_event.set_usb_keycode(
ui::KeycodeConverter::CodeStringToUsbKeycode(dom_code));
key_event.set_pressed(pp_key_event.GetType() == PP_INPUTEVENT_TYPE_KEYDOWN); key_event.set_pressed(pp_key_event.GetType() == PP_INPUTEVENT_TYPE_KEYDOWN);
key_event.set_lock_states(MakeLockStates(pp_key_event)); key_event.set_lock_states(MakeLockStates(pp_key_event));
return key_event; return key_event;
......
...@@ -75,6 +75,9 @@ int KeycodeConverter::InvalidNativeKeycode() { ...@@ -75,6 +75,9 @@ int KeycodeConverter::InvalidNativeKeycode() {
return usb_keycode_map[0].native_keycode; return usb_keycode_map[0].native_keycode;
} }
// TODO(zijiehe): Most of the following functions can be optimized by using
// either multiple arrays or unordered_map.
// static // static
DomCode KeycodeConverter::NativeKeycodeToDomCode(int native_keycode) { DomCode KeycodeConverter::NativeKeycodeToDomCode(int native_keycode) {
for (size_t i = 0; i < kKeycodeMapEntries; ++i) { for (size_t i = 0; i < kKeycodeMapEntries; ++i) {
...@@ -297,7 +300,7 @@ uint32_t KeycodeConverter::DomCodeToUsbKeycode(DomCode dom_code) { ...@@ -297,7 +300,7 @@ uint32_t KeycodeConverter::DomCodeToUsbKeycode(DomCode dom_code) {
} }
// static // static
uint32_t KeycodeConverter::CodeToUsbKeycode(const std::string& code) { uint32_t KeycodeConverter::CodeStringToUsbKeycode(const std::string& code) {
if (code.empty()) if (code.empty())
return InvalidUsbKeycode(); return InvalidUsbKeycode();
...@@ -309,4 +312,9 @@ uint32_t KeycodeConverter::CodeToUsbKeycode(const std::string& code) { ...@@ -309,4 +312,9 @@ uint32_t KeycodeConverter::CodeToUsbKeycode(const std::string& code) {
return InvalidUsbKeycode(); return InvalidUsbKeycode();
} }
// static
int KeycodeConverter::CodeStringToNativeKeycode(const std::string& code) {
return UsbKeycodeToNativeKeycode(CodeStringToUsbKeycode(code));
}
} // namespace ui } // namespace ui
...@@ -2,8 +2,8 @@ ...@@ -2,8 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#ifndef UI_EVENTS_KEYCODES_DOM4_KEYCODE_CONVERTER_H_ #ifndef UI_EVENTS_KEYCODES_DOM_KEYCODE_CONVERTER_H_
#define UI_EVENTS_KEYCODES_DOM4_KEYCODE_CONVERTER_H_ #define UI_EVENTS_KEYCODES_DOM_KEYCODE_CONVERTER_H_
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
...@@ -99,8 +99,11 @@ class KeycodeConverter { ...@@ -99,8 +99,11 @@ class KeycodeConverter {
// Convert a DomCode into a USB keycode. // Convert a DomCode into a USB keycode.
static uint32_t DomCodeToUsbKeycode(DomCode dom_code); static uint32_t DomCodeToUsbKeycode(DomCode dom_code);
// Convert a DOM3 Event |code| string into a USB keycode value. // Convert a UI Event |code| string into a USB keycode value.
static uint32_t CodeToUsbKeycode(const std::string& code); static uint32_t CodeStringToUsbKeycode(const std::string& code);
// Convert a UI Event |code| string into a native keycode.
static int CodeStringToNativeKeycode(const std::string& code);
// Static methods to support testing. // Static methods to support testing.
static size_t NumKeycodeMapEntriesForTest(); static size_t NumKeycodeMapEntriesForTest();
...@@ -113,4 +116,4 @@ class KeycodeConverter { ...@@ -113,4 +116,4 @@ class KeycodeConverter {
} // namespace ui } // namespace ui
#endif // UI_EVENTS_KEYCODES_DOM4_KEYCODE_CONVERTER_H_ #endif // UI_EVENTS_KEYCODES_DOM_KEYCODE_CONVERTER_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment