Commit dee45135 authored by Steven Bennetts's avatar Steven Bennetts Committed by Commit Bot

Move keyboard::SendKeyEvent to ChromeVirtualKeyboardDelegate

This CL moves SendKeyEvent to ChromeVirtualKeyboardDelegate
which is the only place it is used.

It also:
* Uses aura::EventInjector instead of EventSink::OnEventFromSource so
  that events will be injected properly in Mash.
* Removes the undocumented histogram (without documentation it is very
  unlikely that it is used): VirtualKeyboard.KeystrokesBetweenBackspaces

Bug: 876138
Change-Id: I39836f5593587c32ccb229ca32a2126199500533
Reviewed-on: https://chromium-review.googlesource.com/c/1313208
Commit-Queue: Steven Bennetts <stevenjb@chromium.org>
Reviewed-by: default avatarDarren Shen <shend@chromium.org>
Reviewed-by: default avatarXiyuan Xia <xiyuan@chromium.org>
Reviewed-by: default avatarShu Chen <shuchen@chromium.org>
Cr-Commit-Position: refs/heads/master@{#606899}
parent f947cd08
...@@ -12,7 +12,6 @@ ...@@ -12,7 +12,6 @@
#include "base/bind.h" #include "base/bind.h"
#include "base/command_line.h" #include "base/command_line.h"
#include "base/feature_list.h" #include "base/feature_list.h"
#include "base/metrics/histogram_macros.h"
#include "base/metrics/user_metrics.h" #include "base/metrics/user_metrics.h"
#include "base/metrics/user_metrics_action.h" #include "base/metrics/user_metrics_action.h"
#include "base/strings/string16.h" #include "base/strings/string16.h"
...@@ -32,11 +31,18 @@ ...@@ -32,11 +31,18 @@
#include "media/audio/audio_system.h" #include "media/audio/audio_system.h"
#include "services/audio/public/cpp/audio_system_factory.h" #include "services/audio/public/cpp/audio_system_factory.h"
#include "services/service_manager/public/cpp/connector.h" #include "services/service_manager/public/cpp/connector.h"
#include "ui/aura/event_injector.h"
#include "ui/aura/window_tree_host.h" #include "ui/aura/window_tree_host.h"
#include "ui/base/ime/constants.h"
#include "ui/base/ime/ime_bridge.h" #include "ui/base/ime/ime_bridge.h"
#include "ui/base/ime/input_method.h" #include "ui/base/ime/input_method.h"
#include "ui/base/ime/text_input_client.h" #include "ui/base/ime/text_input_client.h"
#include "ui/base/ui_base_features.h" #include "ui/base/ui_base_features.h"
#include "ui/events/event_utils.h"
#include "ui/events/keycodes/dom/dom_code.h"
#include "ui/events/keycodes/dom/dom_key.h"
#include "ui/events/keycodes/dom/keycode_converter.h"
#include "ui/events/keycodes/keyboard_code_conversion.h"
#include "ui/keyboard/keyboard_controller.h" #include "ui/keyboard/keyboard_controller.h"
#include "ui/keyboard/keyboard_switches.h" #include "ui/keyboard/keyboard_switches.h"
#include "ui/keyboard/keyboard_util.h" #include "ui/keyboard/keyboard_util.h"
...@@ -58,6 +64,77 @@ std::string GenerateFeatureFlag(const std::string& feature, bool enabled) { ...@@ -58,6 +64,77 @@ std::string GenerateFeatureFlag(const std::string& feature, bool enabled) {
return feature + (enabled ? "-enabled" : "-disabled"); return feature + (enabled ? "-enabled" : "-disabled");
} }
const char kKeyDown[] = "keydown";
const char kKeyUp[] = "keyup";
void SendProcessKeyEvent(ui::EventType type, aura::WindowTreeHost* host) {
ui::KeyEvent event(type, ui::VKEY_PROCESSKEY, ui::DomCode::NONE,
ui::EF_IS_SYNTHESIZED, ui::DomKey::PROCESS,
ui::EventTimeForNow());
ui::EventDispatchDetails details = aura::EventInjector().Inject(host, &event);
CHECK(!details.dispatcher_destroyed);
}
// Sends a fabricated key event, where |type| is the event type (which must be
// "keydown" or "keyup"), |key_value| is the unicode value of the character,
// |key_code| is the legacy key code value, |key_name| is the name of the key as
// defined in the DOM3 key event specification, and |modifier| indicates if any
// modifier keys are being virtually pressed. The event is dispatched to the
// active TextInputClient associated with |host|.
bool SendKeyEventImpl(const std::string& type,
int key_value,
int key_code,
const std::string& key_name,
int modifiers,
aura::WindowTreeHost* host) {
ui::EventType event_type;
if (type == kKeyDown)
event_type = ui::ET_KEY_PRESSED;
else if (type == kKeyUp)
event_type = ui::ET_KEY_RELEASED;
else
return false;
ui::KeyboardCode code = static_cast<ui::KeyboardCode>(key_code);
ui::InputMethod* input_method = host->GetInputMethod();
if (code == ui::VKEY_UNKNOWN) {
// Handling of special printable characters (e.g. accented characters) for
// which there is no key code.
if (event_type == ui::ET_KEY_RELEASED) {
if (!input_method)
return false;
// This can be null if no text input field is focused.
ui::TextInputClient* tic = input_method->GetTextInputClient();
SendProcessKeyEvent(ui::ET_KEY_PRESSED, host);
ui::KeyEvent char_event(key_value, code, ui::DomCode::NONE, ui::EF_NONE);
if (tic)
tic->InsertChar(char_event);
SendProcessKeyEvent(ui::ET_KEY_RELEASED, host);
}
return true;
}
ui::DomCode dom_code = ui::KeycodeConverter::CodeStringToDomCode(key_name);
if (dom_code == ui::DomCode::NONE)
dom_code = ui::UsLayoutKeyboardCodeToDomCode(code);
CHECK(dom_code != ui::DomCode::NONE);
ui::KeyEvent event(event_type, code, dom_code, modifiers);
// Indicate that the simulated key event is from the Virtual Keyboard.
ui::Event::Properties properties;
properties[ui::kPropertyFromVK] = std::vector<uint8_t>();
event.SetProperties(properties);
ui::EventDispatchDetails details = aura::EventInjector().Inject(host, &event);
CHECK(!details.dispatcher_destroyed);
return true;
}
} // namespace } // namespace
namespace extensions { namespace extensions {
...@@ -151,8 +228,8 @@ bool ChromeVirtualKeyboardDelegate::SendKeyEvent(const std::string& type, ...@@ -151,8 +228,8 @@ bool ChromeVirtualKeyboardDelegate::SendKeyEvent(const std::string& type,
int modifiers) { int modifiers) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI); DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
aura::Window* window = GetKeyboardWindow(); aura::Window* window = GetKeyboardWindow();
return window && keyboard::SendKeyEvent(type, char_value, key_code, key_name, return window && SendKeyEventImpl(type, char_value, key_code, key_name,
modifiers, window->GetHost()); modifiers, window->GetHost());
} }
bool ChromeVirtualKeyboardDelegate::ShowLanguageSettings() { bool ChromeVirtualKeyboardDelegate::ShowLanguageSettings() {
......
...@@ -8,16 +8,6 @@ ...@@ -8,16 +8,6 @@
#include "base/command_line.h" #include "base/command_line.h"
#include "base/metrics/histogram_macros.h" #include "base/metrics/histogram_macros.h"
#include "ui/aura/window_tree_host.h"
#include "ui/base/ime/constants.h"
#include "ui/base/ime/input_method.h"
#include "ui/base/ime/text_input_client.h"
#include "ui/events/event_sink.h"
#include "ui/events/event_utils.h"
#include "ui/events/keycodes/dom/dom_code.h"
#include "ui/events/keycodes/dom/dom_key.h"
#include "ui/events/keycodes/dom/keycode_converter.h"
#include "ui/events/keycodes/keyboard_code_conversion.h"
#include "ui/keyboard/keyboard_controller.h" #include "ui/keyboard/keyboard_controller.h"
#include "ui/keyboard/keyboard_switches.h" #include "ui/keyboard/keyboard_switches.h"
...@@ -25,18 +15,6 @@ namespace keyboard { ...@@ -25,18 +15,6 @@ namespace keyboard {
namespace { namespace {
const char kKeyDown[] = "keydown";
const char kKeyUp[] = "keyup";
void SendProcessKeyEvent(ui::EventType type, aura::WindowTreeHost* host) {
ui::KeyEvent event(type, ui::VKEY_PROCESSKEY, ui::DomCode::NONE,
ui::EF_IS_SYNTHESIZED, ui::DomKey::PROCESS,
ui::EventTimeForNow());
ui::EventDispatchDetails details =
host->event_sink()->OnEventFromSource(&event);
CHECK(!details.dispatcher_destroyed);
}
// Until src/chrome is fully transitioned to use ChromeKeyboardControllerClient // Until src/chrome is fully transitioned to use ChromeKeyboardControllerClient
// we need to test whether KeyboardController exists; it is null in OopMash. // we need to test whether KeyboardController exists; it is null in OopMash.
// TODO(stevenjb): Remove remaining calls from src/chrome. // TODO(stevenjb): Remove remaining calls from src/chrome.
...@@ -95,72 +73,4 @@ bool IsKeyboardEnabled() { ...@@ -95,72 +73,4 @@ bool IsKeyboardEnabled() {
return KeyboardController::Get()->IsKeyboardEnableRequested(); return KeyboardController::Get()->IsKeyboardEnableRequested();
} }
bool SendKeyEvent(const std::string type,
int key_value,
int key_code,
std::string key_name,
int modifiers,
aura::WindowTreeHost* host) {
ui::EventType event_type = ui::ET_UNKNOWN;
if (type == kKeyDown)
event_type = ui::ET_KEY_PRESSED;
else if (type == kKeyUp)
event_type = ui::ET_KEY_RELEASED;
if (event_type == ui::ET_UNKNOWN)
return false;
ui::KeyboardCode code = static_cast<ui::KeyboardCode>(key_code);
ui::InputMethod* input_method = host->GetInputMethod();
if (code == ui::VKEY_UNKNOWN) {
// Handling of special printable characters (e.g. accented characters) for
// which there is no key code.
if (event_type == ui::ET_KEY_RELEASED) {
if (!input_method)
return false;
// This can be null if no text input field is not focused.
ui::TextInputClient* tic = input_method->GetTextInputClient();
SendProcessKeyEvent(ui::ET_KEY_PRESSED, host);
ui::KeyEvent char_event(key_value, code, ui::DomCode::NONE, ui::EF_NONE);
if (tic)
tic->InsertChar(char_event);
SendProcessKeyEvent(ui::ET_KEY_RELEASED, host);
}
} else {
if (event_type == ui::ET_KEY_RELEASED) {
// The number of key press events seen since the last backspace.
static int keys_seen = 0;
if (code == ui::VKEY_BACK) {
// Log the rough lengths of characters typed between backspaces. This
// metric will be used to determine the error rate for the keyboard.
UMA_HISTOGRAM_CUSTOM_COUNTS(
"VirtualKeyboard.KeystrokesBetweenBackspaces", keys_seen, 1, 1000,
50);
keys_seen = 0;
} else {
++keys_seen;
}
}
ui::DomCode dom_code = ui::KeycodeConverter::CodeStringToDomCode(key_name);
if (dom_code == ui::DomCode::NONE)
dom_code = ui::UsLayoutKeyboardCodeToDomCode(code);
CHECK(dom_code != ui::DomCode::NONE);
ui::KeyEvent event(event_type, code, dom_code, modifiers);
// Marks the simulated key event is from the Virtual Keyboard.
ui::Event::Properties properties;
properties[ui::kPropertyFromVK] = std::vector<uint8_t>();
event.SetProperties(properties);
ui::EventDispatchDetails details =
host->event_sink()->OnEventFromSource(&event);
CHECK(!details.dispatcher_destroyed);
}
return true;
}
} // namespace keyboard } // namespace keyboard
...@@ -14,22 +14,8 @@ ...@@ -14,22 +14,8 @@
// TODO(stevenjb/shuchen/shend): Many of these are accessed from both Chrome // TODO(stevenjb/shuchen/shend): Many of these are accessed from both Chrome
// and Ash. We need to remove any Chrome dependencies. htpps://crbug.com/843332 // and Ash. We need to remove any Chrome dependencies. htpps://crbug.com/843332
namespace aura {
class WindowTreeHost;
}
namespace keyboard { namespace keyboard {
// An enumeration of keyboard states.
enum KeyboardState {
// Default state. System decides whether to show the keyboard or not.
KEYBOARD_STATE_AUTO = 0,
// Request virtual keyboard be deployed.
KEYBOARD_STATE_ENABLED,
// Request virtual keyboard be suppressed.
KEYBOARD_STATE_DISABLED,
};
// Sets the state of the a11y onscreen keyboard. // Sets the state of the a11y onscreen keyboard.
KEYBOARD_EXPORT void SetAccessibilityKeyboardEnabled(bool enabled); KEYBOARD_EXPORT void SetAccessibilityKeyboardEnabled(bool enabled);
...@@ -54,19 +40,6 @@ KEYBOARD_EXPORT std::string GetKeyboardLayout(); ...@@ -54,19 +40,6 @@ KEYBOARD_EXPORT std::string GetKeyboardLayout();
// Returns true if the virtual keyboard is enabled. // Returns true if the virtual keyboard is enabled.
KEYBOARD_EXPORT bool IsKeyboardEnabled(); KEYBOARD_EXPORT bool IsKeyboardEnabled();
// Sends a fabricated key event, where |type| is the event type, |key_value|
// is the unicode value of the character, |key_code| is the legacy key code
// value, |key_name| is the name of the key as defined in the DOM3 key event
// specification, and |modifier| indicates if any modifier keys are being
// virtually pressed. The event is dispatched to the active TextInputClient
// associated with |root_window|. The type may be "keydown" or "keyup".
KEYBOARD_EXPORT bool SendKeyEvent(std::string type,
int key_value,
int key_code,
std::string key_name,
int modifiers,
aura::WindowTreeHost* host);
} // namespace keyboard } // namespace keyboard
#endif // UI_KEYBOARD_KEYBOARD_UTIL_H_ #endif // UI_KEYBOARD_KEYBOARD_UTIL_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