1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
|
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/input_method/input_method_engine_base.h"
#include <memory>
#undef FocusIn
#undef FocusOut
#undef RootWindow
#include <algorithm>
#include <map>
#include "base/logging.h"
#include "base/metrics/histogram_macros.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "ui/aura/window.h"
#include "ui/aura/window_tree_host.h"
#include "ui/base/ime/composition_text.h"
#include "ui/base/ime/ime_bridge.h"
#include "ui/base/ime/text_input_flags.h"
#include "ui/events/event.h"
#include "ui/events/event_processor.h"
#include "ui/events/event_utils.h"
#include "ui/events/keycodes/dom/dom_code.h"
#include "ui/events/keycodes/dom/keycode_converter.h"
#include "ui/keyboard/keyboard_controller.h"
#include "ui/keyboard/keyboard_util.h"
#if defined(OS_CHROMEOS)
#include "ui/base/ime/chromeos/ime_keymap.h"
#elif defined(OS_WIN)
#include "ui/events/keycodes/keyboard_codes_win.h"
#elif defined(OS_LINUX)
#include "ui/events/keycodes/keyboard_codes_posix.h"
#endif
namespace input_method {
namespace {
const char kErrorNotActive[] = "IME is not active";
const char kErrorWrongContext[] = "Context is not active";
#if defined(OS_CHROMEOS)
std::string GetKeyFromEvent(const ui::KeyEvent& event) {
const std::string code = event.GetCodeString();
if (base::StartsWith(code, "Control", base::CompareCase::SENSITIVE))
return "Ctrl";
if (base::StartsWith(code, "Shift", base::CompareCase::SENSITIVE))
return "Shift";
if (base::StartsWith(code, "Alt", base::CompareCase::SENSITIVE))
return "Alt";
if (base::StartsWith(code, "Arrow", base::CompareCase::SENSITIVE))
return code.substr(5);
if (code == "Escape")
return "Esc";
if (code == "Backspace" || code == "Tab" || code == "Enter" ||
code == "CapsLock" || code == "Power")
return code;
// Cases for media keys.
switch (event.key_code()) {
case ui::VKEY_BROWSER_BACK:
case ui::VKEY_F1:
return "HistoryBack";
case ui::VKEY_BROWSER_FORWARD:
case ui::VKEY_F2:
return "HistoryForward";
case ui::VKEY_BROWSER_REFRESH:
case ui::VKEY_F3:
return "BrowserRefresh";
case ui::VKEY_MEDIA_LAUNCH_APP2:
case ui::VKEY_F4:
return "ChromeOSFullscreen";
case ui::VKEY_MEDIA_LAUNCH_APP1:
case ui::VKEY_F5:
return "ChromeOSSwitchWindow";
case ui::VKEY_BRIGHTNESS_DOWN:
case ui::VKEY_F6:
return "BrightnessDown";
case ui::VKEY_BRIGHTNESS_UP:
case ui::VKEY_F7:
return "BrightnessUp";
case ui::VKEY_VOLUME_MUTE:
case ui::VKEY_F8:
return "AudioVolumeMute";
case ui::VKEY_VOLUME_DOWN:
case ui::VKEY_F9:
return "AudioVolumeDown";
case ui::VKEY_VOLUME_UP:
case ui::VKEY_F10:
return "AudioVolumeUp";
default:
break;
}
uint16_t ch = 0;
// Ctrl+? cases, gets key value for Ctrl is not down.
if (event.flags() & ui::EF_CONTROL_DOWN) {
ui::KeyEvent event_no_ctrl(event.type(), event.key_code(),
event.flags() ^ ui::EF_CONTROL_DOWN);
ch = event_no_ctrl.GetCharacter();
} else {
ch = event.GetCharacter();
}
return base::UTF16ToUTF8(base::string16(1, ch));
}
#endif // defined(OS_CHROMEOS)
void GetExtensionKeyboardEventFromKeyEvent(
const ui::KeyEvent& event,
InputMethodEngineBase::KeyboardEvent* ext_event) {
DCHECK(event.type() == ui::ET_KEY_RELEASED ||
event.type() == ui::ET_KEY_PRESSED);
DCHECK(ext_event);
ext_event->type = (event.type() == ui::ET_KEY_RELEASED) ? "keyup" : "keydown";
if (event.code() == ui::DomCode::NONE) {
// TODO(azurewei): Use KeycodeConverter::DomCodeToCodeString on all platforms
#if defined(OS_CHROMEOS)
ext_event->code = ui::KeyboardCodeToDomKeycode(event.key_code());
#else
ext_event->code =
std::string(ui::KeycodeConverter::DomCodeToCodeString(event.code()));
#endif
} else {
ext_event->code = event.GetCodeString();
}
ext_event->key_code = static_cast<int>(event.key_code());
ext_event->alt_key = event.IsAltDown();
ext_event->ctrl_key = event.IsControlDown();
ext_event->shift_key = event.IsShiftDown();
ext_event->caps_lock = event.IsCapsLockOn();
#if defined(OS_CHROMEOS)
ext_event->key = GetKeyFromEvent(event);
#else
ext_event->key = ui::KeycodeConverter::DomKeyToKeyString(event.GetDomKey());
#endif // defined(OS_CHROMEOS)
}
} // namespace
InputMethodEngineBase::KeyboardEvent::KeyboardEvent()
: alt_key(false), ctrl_key(false), shift_key(false), caps_lock(false) {}
InputMethodEngineBase::KeyboardEvent::KeyboardEvent(
const KeyboardEvent& other) = default;
InputMethodEngineBase::KeyboardEvent::~KeyboardEvent() {}
InputMethodEngineBase::InputMethodEngineBase()
: current_input_type_(ui::TEXT_INPUT_TYPE_NONE),
context_id_(0),
next_context_id_(1),
composition_text_(new ui::CompositionText()),
composition_cursor_(0),
sent_key_event_(nullptr),
profile_(nullptr),
next_request_id_(1),
composition_changed_(false),
text_(""),
commit_text_changed_(false),
handling_key_event_(false) {}
InputMethodEngineBase::~InputMethodEngineBase() {}
void InputMethodEngineBase::Initialize(
std::unique_ptr<InputMethodEngineBase::Observer> observer,
const char* extension_id,
Profile* profile) {
DCHECK(observer) << "Observer must not be null.";
// TODO(komatsu): It is probably better to set observer out of Initialize.
observer_ = std::move(observer);
extension_id_ = extension_id;
profile_ = profile;
}
const std::string& InputMethodEngineBase::GetActiveComponentId() const {
return active_component_id_;
}
bool InputMethodEngineBase::SetComposition(
int context_id,
const char* text,
int selection_start,
int selection_end,
int cursor,
const std::vector<SegmentInfo>& segments,
std::string* error) {
if (!IsActive()) {
*error = kErrorNotActive;
return false;
}
if (context_id != context_id_ || context_id_ == -1) {
*error = kErrorWrongContext;
return false;
}
composition_cursor_ = cursor;
composition_text_.reset(new ui::CompositionText());
composition_text_->text = base::UTF8ToUTF16(text);
composition_text_->selection.set_start(selection_start);
composition_text_->selection.set_end(selection_end);
// TODO: Add support for displaying selected text in the composition string.
for (std::vector<SegmentInfo>::const_iterator segment = segments.begin();
segment != segments.end(); ++segment) {
ui::CompositionUnderline underline;
switch (segment->style) {
case SEGMENT_STYLE_UNDERLINE:
underline.color = SK_ColorBLACK;
break;
case SEGMENT_STYLE_DOUBLE_UNDERLINE:
underline.color = SK_ColorBLACK;
underline.thick = true;
break;
case SEGMENT_STYLE_NO_UNDERLINE:
underline.color = SK_ColorTRANSPARENT;
break;
default:
continue;
}
underline.start_offset = segment->start;
underline.end_offset = segment->end;
composition_text_->underlines.push_back(underline);
}
// TODO(nona): Makes focus out mode configuable, if necessary.
UpdateComposition(*composition_text_, composition_cursor_, true);
return true;
}
bool InputMethodEngineBase::ClearComposition(int context_id,
std::string* error) {
if (!IsActive()) {
*error = kErrorNotActive;
return false;
}
if (context_id != context_id_ || context_id_ == -1) {
*error = kErrorWrongContext;
return false;
}
composition_cursor_ = 0;
composition_text_.reset(new ui::CompositionText());
UpdateComposition(*composition_text_, composition_cursor_, false);
return true;
}
bool InputMethodEngineBase::CommitText(int context_id,
const char* text,
std::string* error) {
if (!IsActive()) {
// TODO: Commit the text anyways.
*error = kErrorNotActive;
return false;
}
if (context_id != context_id_ || context_id_ == -1) {
*error = kErrorWrongContext;
return false;
}
CommitTextToInputContext(context_id, std::string(text));
return true;
}
bool InputMethodEngineBase::DeleteSurroundingText(int context_id,
int offset,
size_t number_of_chars,
std::string* error) {
if (!IsActive()) {
*error = kErrorNotActive;
return false;
}
if (context_id != context_id_ || context_id_ == -1) {
*error = kErrorWrongContext;
return false;
}
// TODO(nona): Return false if there is ongoing composition.
ui::IMEInputContextHandlerInterface* input_context =
ui::IMEBridge::Get()->GetInputContextHandler();
if (input_context)
input_context->DeleteSurroundingText(offset, number_of_chars);
return true;
}
void InputMethodEngineBase::SetCompositionBounds(
const std::vector<gfx::Rect>& bounds) {
observer_->OnCompositionBoundsChanged(bounds);
}
void InputMethodEngineBase::FocusIn(
const ui::IMEEngineHandlerInterface::InputContext& input_context) {
current_input_type_ = input_context.type;
if (!IsActive() || current_input_type_ == ui::TEXT_INPUT_TYPE_NONE)
return;
context_id_ = next_context_id_;
++next_context_id_;
observer_->OnFocus(ui::IMEEngineHandlerInterface::InputContext(
context_id_, input_context.type, input_context.mode,
input_context.flags));
}
void InputMethodEngineBase::FocusOut() {
if (!IsActive() || current_input_type_ == ui::TEXT_INPUT_TYPE_NONE)
return;
current_input_type_ = ui::TEXT_INPUT_TYPE_NONE;
int context_id = context_id_;
context_id_ = -1;
observer_->OnBlur(context_id);
}
void InputMethodEngineBase::Enable(const std::string& component_id) {
active_component_id_ = component_id;
observer_->OnActivate(component_id);
const ui::IMEEngineHandlerInterface::InputContext& input_context =
ui::IMEBridge::Get()->GetCurrentInputContext();
current_input_type_ = input_context.type;
FocusIn(input_context);
}
void InputMethodEngineBase::Disable() {
active_component_id_.clear();
if (ui::IMEBridge::Get()->GetInputContextHandler())
ui::IMEBridge::Get()->GetInputContextHandler()->CommitText(
base::UTF16ToUTF8(composition_text_->text));
composition_text_.reset(new ui::CompositionText());
observer_->OnDeactivated(active_component_id_);
}
void InputMethodEngineBase::Reset() {
composition_text_.reset(new ui::CompositionText());
observer_->OnReset(active_component_id_);
}
void InputMethodEngineBase::MaybeSwitchEngine() {
observer_->OnRequestEngineSwitch();
}
bool InputMethodEngineBase::IsInterestedInKeyEvent() const {
return observer_->IsInterestedInKeyEvent();
}
void InputMethodEngineBase::ProcessKeyEvent(const ui::KeyEvent& key_event,
KeyEventDoneCallback& callback) {
// Make true that we don't handle IME API calling of setComposition and
// commitText while the extension is handling key event.
handling_key_event_ = true;
if (key_event.IsCommandDown()) {
callback.Run(false);
return;
}
KeyboardEvent ext_event;
GetExtensionKeyboardEventFromKeyEvent(key_event, &ext_event);
// If the given key event is equal to the key event sent by
// SendKeyEvents, this engine ID is propagated to the extension IME.
// Note, this check relies on that ui::KeyEvent is propagated as
// reference without copying.
if (&key_event == sent_key_event_)
ext_event.extension_id = extension_id_;
// Should not pass key event in password field.
if (current_input_type_ != ui::TEXT_INPUT_TYPE_PASSWORD)
observer_->OnKeyEvent(active_component_id_, ext_event, callback);
}
void InputMethodEngineBase::SetSurroundingText(const std::string& text,
uint32_t cursor_pos,
uint32_t anchor_pos,
uint32_t offset_pos) {
observer_->OnSurroundingTextChanged(
active_component_id_, text, static_cast<int>(cursor_pos),
static_cast<int>(anchor_pos), static_cast<int>(offset_pos));
}
void InputMethodEngineBase::KeyEventHandled(const std::string& extension_id,
const std::string& request_id,
bool handled) {
handling_key_event_ = false;
// When finish handling key event, take care of the unprocessed commitText
// and setComposition calls.
ui::IMEInputContextHandlerInterface* input_context =
ui::IMEBridge::Get()->GetInputContextHandler();
if (commit_text_changed_) {
if (input_context) {
input_context->CommitText(text_);
}
text_ = "";
commit_text_changed_ = false;
}
if (composition_changed_) {
if (input_context) {
input_context->UpdateCompositionText(
composition_, composition_.selection.start(), true);
}
composition_.Clear();
composition_changed_ = false;
}
RequestMap::iterator request = request_map_.find(request_id);
if (request == request_map_.end()) {
LOG(ERROR) << "Request ID not found: " << request_id;
return;
}
request->second.second.Run(handled);
request_map_.erase(request);
}
std::string InputMethodEngineBase::AddRequest(
const std::string& component_id,
ui::IMEEngineHandlerInterface::KeyEventDoneCallback& key_data) {
std::string request_id = base::IntToString(next_request_id_);
++next_request_id_;
request_map_[request_id] = std::make_pair(component_id, key_data);
return request_id;
}
bool InputMethodEngineBase::SendKeyEvents(
int context_id,
const std::vector<KeyboardEvent>& events) {
// context_id == 0, means sending key events to non-input field.
// context_id_ == -1, means the focus is not in an input field.
if (!IsActive() ||
(context_id != 0 && (context_id != context_id_ || context_id_ == -1)))
return false;
for (size_t i = 0; i < events.size(); ++i) {
const KeyboardEvent& event = events[i];
const ui::EventType type =
(event.type == "keyup") ? ui::ET_KEY_RELEASED : ui::ET_KEY_PRESSED;
ui::KeyboardCode key_code = static_cast<ui::KeyboardCode>(event.key_code);
int flags = ui::EF_NONE;
flags |= event.alt_key ? ui::EF_ALT_DOWN : ui::EF_NONE;
flags |= event.ctrl_key ? ui::EF_CONTROL_DOWN : ui::EF_NONE;
flags |= event.shift_key ? ui::EF_SHIFT_DOWN : ui::EF_NONE;
flags |= event.caps_lock ? ui::EF_CAPS_LOCK_ON : ui::EF_NONE;
ui::KeyEvent ui_event(
type, key_code, ui::KeycodeConverter::CodeStringToDomCode(event.code),
flags, ui::KeycodeConverter::KeyStringToDomKey(event.key),
ui::EventTimeForNow());
base::AutoReset<const ui::KeyEvent*> reset_sent_key(&sent_key_event_,
&ui_event);
if (!SendKeyEvent(&ui_event, event.code))
return false;
}
return true;
}
} // namespace input_method
|