Skip to content

Commit

Permalink
events: keycodes: Remove the stateless singleton instance of KeycodeC…
Browse files Browse the repository at this point in the history
…onverter

All methods can be safely made static and the singleton
removed.

TBR=ben@chromium.org

Review URL: https://codereview.chromium.org/494813002

Cr-Commit-Position: refs/heads/master@{#292070}
  • Loading branch information
mspang authored and Commit bot committed Aug 27, 2014
1 parent 0e014b9 commit ba8794f
Show file tree
Hide file tree
Showing 16 changed files with 114 additions and 134 deletions.
3 changes: 1 addition & 2 deletions chrome/browser/chromeos/input_method/input_method_engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,7 @@ void GetExtensionKeyboardEventFromKeyEvent(
ext_event->type = (event.type() == ui::ET_KEY_RELEASED) ? "keyup" : "keydown";

std::string dom_code = event.code();
if (dom_code ==
ui::KeycodeConverter::GetInstance()->InvalidKeyboardEventCode())
if (dom_code == ui::KeycodeConverter::InvalidKeyboardEventCode())
dom_code = ui::KeyboardCodeToDomKeycode(event.key_code());
ext_event->code = dom_code;
ext_event->key_code = static_cast<int>(event.key_code());
Expand Down
91 changes: 41 additions & 50 deletions content/public/test/browser_test_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -322,51 +322,46 @@ void SimulateKeyPressWithCode(WebContents* web_contents,
bool shift,
bool alt,
bool command) {
ui::KeycodeConverter* key_converter = ui::KeycodeConverter::GetInstance();
int native_key_code = key_converter->CodeToNativeKeycode(code);
int native_key_code = ui::KeycodeConverter::CodeToNativeKeycode(code);

int modifiers = 0;

// The order of these key down events shouldn't matter for our simulation.
// For our simulation we can use either the left keys or the right keys.
if (control) {
modifiers |= blink::WebInputEvent::ControlKey;
InjectRawKeyEvent(
web_contents,
blink::WebInputEvent::RawKeyDown,
ui::VKEY_CONTROL,
key_converter->CodeToNativeKeycode("ControlLeft"),
modifiers);
InjectRawKeyEvent(web_contents,
blink::WebInputEvent::RawKeyDown,
ui::VKEY_CONTROL,
ui::KeycodeConverter::CodeToNativeKeycode("ControlLeft"),
modifiers);
}

if (shift) {
modifiers |= blink::WebInputEvent::ShiftKey;
InjectRawKeyEvent(
web_contents,
blink::WebInputEvent::RawKeyDown,
ui::VKEY_SHIFT,
key_converter->CodeToNativeKeycode("ShiftLeft"),
modifiers);
InjectRawKeyEvent(web_contents,
blink::WebInputEvent::RawKeyDown,
ui::VKEY_SHIFT,
ui::KeycodeConverter::CodeToNativeKeycode("ShiftLeft"),
modifiers);
}

if (alt) {
modifiers |= blink::WebInputEvent::AltKey;
InjectRawKeyEvent(
web_contents,
blink::WebInputEvent::RawKeyDown,
ui::VKEY_MENU,
key_converter->CodeToNativeKeycode("AltLeft"),
modifiers);
InjectRawKeyEvent(web_contents,
blink::WebInputEvent::RawKeyDown,
ui::VKEY_MENU,
ui::KeycodeConverter::CodeToNativeKeycode("AltLeft"),
modifiers);
}

if (command) {
modifiers |= blink::WebInputEvent::MetaKey;
InjectRawKeyEvent(
web_contents,
blink::WebInputEvent::RawKeyDown,
ui::VKEY_COMMAND,
key_converter->CodeToNativeKeycode("OSLeft"),
modifiers);
InjectRawKeyEvent(web_contents,
blink::WebInputEvent::RawKeyDown,
ui::VKEY_COMMAND,
ui::KeycodeConverter::CodeToNativeKeycode("OSLeft"),
modifiers);
}

InjectRawKeyEvent(
Expand All @@ -393,42 +388,38 @@ void SimulateKeyPressWithCode(WebContents* web_contents,
// The order of these key releases shouldn't matter for our simulation.
if (control) {
modifiers &= ~blink::WebInputEvent::ControlKey;
InjectRawKeyEvent(
web_contents,
blink::WebInputEvent::KeyUp,
ui::VKEY_CONTROL,
key_converter->CodeToNativeKeycode("ControlLeft"),
modifiers);
InjectRawKeyEvent(web_contents,
blink::WebInputEvent::KeyUp,
ui::VKEY_CONTROL,
ui::KeycodeConverter::CodeToNativeKeycode("ControlLeft"),
modifiers);
}

if (shift) {
modifiers &= ~blink::WebInputEvent::ShiftKey;
InjectRawKeyEvent(
web_contents,
blink::WebInputEvent::KeyUp,
ui::VKEY_SHIFT,
key_converter->CodeToNativeKeycode("ShiftLeft"),
modifiers);
InjectRawKeyEvent(web_contents,
blink::WebInputEvent::KeyUp,
ui::VKEY_SHIFT,
ui::KeycodeConverter::CodeToNativeKeycode("ShiftLeft"),
modifiers);
}

if (alt) {
modifiers &= ~blink::WebInputEvent::AltKey;
InjectRawKeyEvent(
web_contents,
blink::WebInputEvent::KeyUp,
ui::VKEY_MENU,
key_converter->CodeToNativeKeycode("AltLeft"),
modifiers);
InjectRawKeyEvent(web_contents,
blink::WebInputEvent::KeyUp,
ui::VKEY_MENU,
ui::KeycodeConverter::CodeToNativeKeycode("AltLeft"),
modifiers);
}

if (command) {
modifiers &= ~blink::WebInputEvent::MetaKey;
InjectRawKeyEvent(
web_contents,
blink::WebInputEvent::KeyUp,
ui::VKEY_COMMAND,
key_converter->CodeToNativeKeycode("OSLeft"),
modifiers);
InjectRawKeyEvent(web_contents,
blink::WebInputEvent::KeyUp,
ui::VKEY_COMMAND,
ui::KeycodeConverter::CodeToNativeKeycode("OSLeft"),
modifiers);
}

ASSERT_EQ(modifiers, 0);
Expand Down
7 changes: 3 additions & 4 deletions content/renderer/pepper/usb_key_code_conversion_linux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@ uint32_t UsbKeyCodeForKeyboardEvent(const WebKeyboardEvent& key_event) {
// the XKB driver. We should detect between "XKB", "kbd" and "evdev" at
// run-time and re-map accordingly, but that's not possible here, inside the
// sandbox.
ui::KeycodeConverter* key_converter = ui::KeycodeConverter::GetInstance();
return key_converter->NativeKeycodeToUsbKeycode(key_event.nativeKeyCode);
return ui::KeycodeConverter::NativeKeycodeToUsbKeycode(
key_event.nativeKeyCode);
}

const char* CodeForKeyboardEvent(const WebKeyboardEvent& key_event) {
ui::KeycodeConverter* key_converter = ui::KeycodeConverter::GetInstance();
return key_converter->NativeKeycodeToCode(key_event.nativeKeyCode);
return ui::KeycodeConverter::NativeKeycodeToCode(key_event.nativeKeyCode);
}

} // namespace content
7 changes: 3 additions & 4 deletions content/renderer/pepper/usb_key_code_conversion_mac.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@ using blink::WebKeyboardEvent;
namespace content {

uint32_t UsbKeyCodeForKeyboardEvent(const WebKeyboardEvent& key_event) {
ui::KeycodeConverter* key_converter = ui::KeycodeConverter::GetInstance();
return key_converter->NativeKeycodeToUsbKeycode(key_event.nativeKeyCode);
return ui::KeycodeConverter::NativeKeycodeToUsbKeycode(
key_event.nativeKeyCode);
}

const char* CodeForKeyboardEvent(const WebKeyboardEvent& key_event) {
ui::KeycodeConverter* key_converter = ui::KeycodeConverter::GetInstance();
return key_converter->NativeKeycodeToCode(key_event.nativeKeyCode);
return ui::KeycodeConverter::NativeKeycodeToCode(key_event.nativeKeyCode);
}

} // namespace content
6 changes: 2 additions & 4 deletions content/renderer/pepper/usb_key_code_conversion_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ uint32_t UsbKeyCodeForKeyboardEvent(const WebKeyboardEvent& key_event) {
if ((key_event.nativeKeyCode & (1 << 24)) != 0)
scancode |= 0xe000;

ui::KeycodeConverter* key_converter = ui::KeycodeConverter::GetInstance();
return key_converter->NativeKeycodeToUsbKeycode(scancode);
return ui::KeycodeConverter::NativeKeycodeToUsbKeycode(scancode);
}

const char* CodeForKeyboardEvent(const WebKeyboardEvent& key_event) {
Expand All @@ -28,8 +27,7 @@ const char* CodeForKeyboardEvent(const WebKeyboardEvent& key_event) {
if ((key_event.nativeKeyCode & (1 << 24)) != 0)
scancode |= 0xe000;

ui::KeycodeConverter* key_converter = ui::KeycodeConverter::GetInstance();
return key_converter->NativeKeycodeToCode(scancode);
return ui::KeycodeConverter::NativeKeycodeToCode(scancode);
}

} // namespace content
3 changes: 1 addition & 2 deletions remoting/client/plugin/pepper_input_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ uint32_t GetUsbKeyCode(pp::KeyboardInputEvent pp_key_event) {
std::string codestr = pp_key_event.GetCode().AsString();

// Convert the |code| string into a USB keycode.
ui::KeycodeConverter* key_converter = ui::KeycodeConverter::GetInstance();
return key_converter->CodeToUsbKeycode(codestr.c_str());
return ui::KeycodeConverter::CodeToUsbKeycode(codestr.c_str());
}

bool PepperInputHandler::HandleInputEvent(const pp::InputEvent& event) {
Expand Down
6 changes: 3 additions & 3 deletions remoting/host/input_injector_linux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -276,14 +276,14 @@ void InputInjectorLinux::Core::InjectKeyEvent(const KeyEvent& event) {
return;
}

ui::KeycodeConverter* key_converter = ui::KeycodeConverter::GetInstance();
int keycode = key_converter->UsbKeycodeToNativeKeycode(event.usb_keycode());
int keycode =
ui::KeycodeConverter::UsbKeycodeToNativeKeycode(event.usb_keycode());

VLOG(3) << "Converting USB keycode: " << std::hex << event.usb_keycode()
<< " to keycode: " << keycode << std::dec;

// Ignore events which can't be mapped.
if (keycode == key_converter->InvalidNativeKeycode())
if (keycode == ui::KeycodeConverter::InvalidNativeKeycode())
return;

if (event.pressed()) {
Expand Down
6 changes: 3 additions & 3 deletions remoting/host/input_injector_mac.cc
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,14 @@ void InputInjectorMac::Core::InjectKeyEvent(const KeyEvent& event) {
if (!event.has_pressed() || !event.has_usb_keycode())
return;

ui::KeycodeConverter* key_converter = ui::KeycodeConverter::GetInstance();
int keycode = key_converter->UsbKeycodeToNativeKeycode(event.usb_keycode());
int keycode =
ui::KeycodeConverter::UsbKeycodeToNativeKeycode(event.usb_keycode());

VLOG(3) << "Converting USB keycode: " << std::hex << event.usb_keycode()
<< " to keycode: " << keycode << std::dec;

// If we couldn't determine the Mac virtual key code then ignore the event.
if (keycode == key_converter->InvalidNativeKeycode())
if (keycode == ui::KeycodeConverter::InvalidNativeKeycode())
return;

// If this is a modifier key, remember its new state so that it can be
Expand Down
6 changes: 3 additions & 3 deletions remoting/host/input_injector_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -221,13 +221,13 @@ void InputInjectorWin::Core::HandleKey(const KeyEvent& event) {
// Reset the system idle suspend timeout.
SetThreadExecutionState(ES_SYSTEM_REQUIRED);

ui::KeycodeConverter* key_converter = ui::KeycodeConverter::GetInstance();
int scancode = key_converter->UsbKeycodeToNativeKeycode(event.usb_keycode());
int scancode =
ui::KeycodeConverter::UsbKeycodeToNativeKeycode(event.usb_keycode());
VLOG(3) << "Converting USB keycode: " << std::hex << event.usb_keycode()
<< " to scancode: " << scancode << std::dec;

// Ignore events which can't be mapped.
if (scancode == key_converter->InvalidNativeKeycode())
if (scancode == ui::KeycodeConverter::InvalidNativeKeycode())
return;

uint32_t flags = KEYEVENTF_SCANCODE | (event.pressed() ? 0 : KEYEVENTF_KEYUP);
Expand Down
13 changes: 5 additions & 8 deletions ui/events/event_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -329,11 +329,10 @@ TEST(EventTest, KeyEventCopy) {
}

TEST(EventTest, KeyEventCode) {
KeycodeConverter* conv = KeycodeConverter::GetInstance();

const char kCodeForSpace[] = "Space";
const uint16 kNativeCodeSpace = conv->CodeToNativeKeycode(kCodeForSpace);
ASSERT_NE(conv->InvalidNativeKeycode(), kNativeCodeSpace);
const uint16 kNativeCodeSpace =
ui::KeycodeConverter::CodeToNativeKeycode(kCodeForSpace);
ASSERT_NE(ui::KeycodeConverter::InvalidNativeKeycode(), kNativeCodeSpace);

{
KeyEvent key(ET_KEY_PRESSED, VKEY_SPACE, kCodeForSpace, EF_NONE);
Expand Down Expand Up @@ -392,10 +391,8 @@ TEST(EventTest, KeyEventCode) {

#if defined(USE_X11) || defined(OS_WIN)
TEST(EventTest, AutoRepeat) {
KeycodeConverter* conv = KeycodeConverter::GetInstance();

const uint16 kNativeCodeA = conv->CodeToNativeKeycode("KeyA");
const uint16 kNativeCodeB = conv->CodeToNativeKeycode("KeyB");
const uint16 kNativeCodeA = ui::KeycodeConverter::CodeToNativeKeycode("KeyA");
const uint16 kNativeCodeB = ui::KeycodeConverter::CodeToNativeKeycode("KeyB");
#if defined(USE_X11)
ScopedXI2Event native_event_a_pressed;
native_event_a_pressed.InitKeyEvent(ET_KEY_PRESSED, VKEY_A, kNativeCodeA);
Expand Down
18 changes: 11 additions & 7 deletions ui/events/keycodes/dom4/keycode_converter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,27 @@ const size_t kKeycodeMapEntries = arraysize(usb_keycode_map);

} // namespace

KeycodeConverter::KeycodeConverter() {
}

KeycodeConverter* KeycodeConverter::GetInstance() {
return Singleton<KeycodeConverter>::get();
}

// static
size_t KeycodeConverter::NumKeycodeMapEntriesForTest() {
return kKeycodeMapEntries;
}

// static
const KeycodeMapEntry* KeycodeConverter::GetKeycodeMapForTest() {
return &usb_keycode_map[0];
}

// static
uint16_t KeycodeConverter::InvalidNativeKeycode() {
return usb_keycode_map[0].native_keycode;
}

// static
const char* KeycodeConverter::InvalidKeyboardEventCode() {
return "Unidentified";
}

// static
const char* KeycodeConverter::NativeKeycodeToCode(uint16_t native_keycode) {
for (size_t i = 0; i < kKeycodeMapEntries; ++i) {
if (usb_keycode_map[i].native_keycode == native_keycode) {
Expand All @@ -57,6 +55,7 @@ const char* KeycodeConverter::NativeKeycodeToCode(uint16_t native_keycode) {
return InvalidKeyboardEventCode();
}

// static
uint16_t KeycodeConverter::CodeToNativeKeycode(const char* code) {
if (!code ||
strcmp(code, InvalidKeyboardEventCode()) == 0) {
Expand All @@ -76,10 +75,12 @@ uint16_t KeycodeConverter::CodeToNativeKeycode(const char* code) {
// Note that USB keycodes are not part of any web standard.
// Please don't use USB keycodes in new code.

// static
uint16_t KeycodeConverter::InvalidUsbKeycode() {
return usb_keycode_map[0].usb_keycode;
}

// static
uint16_t KeycodeConverter::UsbKeycodeToNativeKeycode(uint32_t usb_keycode) {
// Deal with some special-cases that don't fit the 1:1 mapping.
if (usb_keycode == 0x070032) // non-US hash.
Expand All @@ -96,6 +97,7 @@ uint16_t KeycodeConverter::UsbKeycodeToNativeKeycode(uint32_t usb_keycode) {
return InvalidNativeKeycode();
}

// static
uint32_t KeycodeConverter::NativeKeycodeToUsbKeycode(uint16_t native_keycode) {
for (size_t i = 0; i < kKeycodeMapEntries; ++i) {
if (usb_keycode_map[i].native_keycode == native_keycode)
Expand All @@ -104,6 +106,7 @@ uint32_t KeycodeConverter::NativeKeycodeToUsbKeycode(uint16_t native_keycode) {
return InvalidUsbKeycode();
}

// static
const char* KeycodeConverter::UsbKeycodeToCode(uint32_t usb_keycode) {
for (size_t i = 0; i < kKeycodeMapEntries; ++i) {
if (usb_keycode_map[i].usb_keycode == usb_keycode)
Expand All @@ -112,6 +115,7 @@ const char* KeycodeConverter::UsbKeycodeToCode(uint32_t usb_keycode) {
return InvalidKeyboardEventCode();
}

// static
uint32_t KeycodeConverter::CodeToUsbKeycode(const char* code) {
if (!code ||
strcmp(code, InvalidKeyboardEventCode()) == 0) {
Expand Down
Loading

0 comments on commit ba8794f

Please sign in to comment.