1 // Copyright 2013 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.
5 #include "chrome/browser/chromeos/input_method/input_method_engine.h"
8 #include <X11/keysymdef.h>
11 #include <X11/Xutil.h>
17 #include "ash/ime/input_method_menu_item.h"
18 #include "ash/ime/input_method_menu_manager.h"
19 #include "ash/shell.h"
20 #include "base/logging.h"
21 #include "base/memory/scoped_ptr.h"
22 #include "base/metrics/histogram.h"
23 #include "base/strings/string_number_conversions.h"
24 #include "base/strings/string_util.h"
25 #include "base/strings/stringprintf.h"
26 #include "base/strings/utf_string_conversions.h"
27 #include "chromeos/ime/component_extension_ime_manager.h"
28 #include "chromeos/ime/composition_text.h"
29 #include "chromeos/ime/extension_ime_util.h"
30 #include "chromeos/ime/input_method_manager.h"
31 #include "ui/aura/window.h"
32 #include "ui/aura/window_tree_host.h"
33 #include "ui/base/ime/candidate_window.h"
34 #include "ui/base/ime/chromeos/ime_keymap.h"
35 #include "ui/events/event.h"
36 #include "ui/events/event_processor.h"
37 #include "ui/events/keycodes/dom4/keycode_converter.h"
38 #include "ui/events/keycodes/keyboard_code_conversion_x.h"
39 #include "ui/keyboard/keyboard_controller.h"
40 #include "ui/keyboard/keyboard_util.h"
43 const char* kErrorNotActive = "IME is not active";
44 const char* kErrorWrongContext = "Context is not active";
45 const char* kCandidateNotFound = "Candidate not found";
49 // Notifies InputContextHandler that the composition is changed.
50 void UpdateComposition(const CompositionText& composition_text,
53 IMEInputContextHandlerInterface* input_context =
54 IMEBridge::Get()->GetInputContextHandler();
56 input_context->UpdateCompositionText(
57 composition_text, cursor_pos, is_visible);
60 // Returns the length of characters of a UTF-8 string with unknown string
61 // length. Cannot apply faster algorithm to count characters in an utf-8
62 // string without knowing the string length, so just does a full scan.
63 size_t GetUtf8StringLength(const char* s) {
66 if ((*s & 0xC0) != 0x80)
75 InputMethodEngine::InputMethodEngine()
76 : current_input_type_(ui::TEXT_INPUT_TYPE_NONE),
80 composition_text_(new CompositionText()),
81 composition_cursor_(0),
82 candidate_window_(new ui::CandidateWindow()),
83 window_visible_(false),
84 sent_key_event_(NULL) {}
86 InputMethodEngine::~InputMethodEngine() {
87 if (start_time_.ToInternalValue())
88 RecordHistogram("WorkingTime", (end_time_ - start_time_).InSeconds());
89 input_method::InputMethodManager::Get()->RemoveInputMethodExtension(imm_id_);
92 void InputMethodEngine::Initialize(
93 scoped_ptr<InputMethodEngineInterface::Observer> observer,
94 const char* engine_name,
95 const char* extension_id,
96 const char* engine_id,
97 const std::vector<std::string>& languages,
98 const std::vector<std::string>& layouts,
99 const GURL& options_page,
100 const GURL& input_view) {
101 DCHECK(observer) << "Observer must not be null.";
103 // TODO(komatsu): It is probably better to set observer out of Initialize.
104 observer_ = observer.Pass();
105 engine_id_ = engine_id;
106 extension_id_ = extension_id;
108 input_method::InputMethodManager* manager =
109 input_method::InputMethodManager::Get();
110 ComponentExtensionIMEManager* comp_ext_ime_manager =
111 manager->GetComponentExtensionIMEManager();
113 if (comp_ext_ime_manager && comp_ext_ime_manager->IsInitialized() &&
114 comp_ext_ime_manager->IsWhitelistedExtension(extension_id)) {
115 imm_id_ = comp_ext_ime_manager->GetId(extension_id, engine_id);
117 imm_id_ = extension_ime_util::GetInputMethodID(extension_id, engine_id);
120 input_view_url_ = input_view;
121 descriptor_ = input_method::InputMethodDescriptor(
124 std::string(), // TODO(uekawa): Set short name.
127 extension_ime_util::IsKeyboardLayoutExtension(
128 imm_id_), // is_login_keyboard
132 // TODO(komatsu): It is probably better to call AddInputMethodExtension
133 // out of Initialize.
134 manager->AddInputMethodExtension(imm_id_, this);
137 const input_method::InputMethodDescriptor& InputMethodEngine::GetDescriptor()
142 void InputMethodEngine::RecordHistogram(const char* name, int count) {
143 std::string histo_name =
144 base::StringPrintf("InputMethod.%s.%s", name, engine_id_.c_str());
145 base::HistogramBase* counter = base::Histogram::FactoryGet(
146 histo_name, 0, 1000000, 50, base::HistogramBase::kNoFlags);
151 void InputMethodEngine::NotifyImeReady() {
152 input_method::InputMethodManager* manager =
153 input_method::InputMethodManager::Get();
154 if (manager && imm_id_ == manager->GetCurrentInputMethod().id())
158 bool InputMethodEngine::SetComposition(
164 const std::vector<SegmentInfo>& segments,
165 std::string* error) {
167 *error = kErrorNotActive;
170 if (context_id != context_id_ || context_id_ == -1) {
171 *error = kErrorWrongContext;
175 composition_cursor_ = cursor;
176 composition_text_.reset(new CompositionText());
177 composition_text_->set_text(base::UTF8ToUTF16(text));
179 composition_text_->set_selection_start(selection_start);
180 composition_text_->set_selection_end(selection_end);
182 // TODO: Add support for displaying selected text in the composition string.
183 for (std::vector<SegmentInfo>::const_iterator segment = segments.begin();
184 segment != segments.end(); ++segment) {
185 CompositionText::UnderlineAttribute underline;
187 switch (segment->style) {
188 case SEGMENT_STYLE_UNDERLINE:
189 underline.type = CompositionText::COMPOSITION_TEXT_UNDERLINE_SINGLE;
191 case SEGMENT_STYLE_DOUBLE_UNDERLINE:
192 underline.type = CompositionText::COMPOSITION_TEXT_UNDERLINE_DOUBLE;
198 underline.start_index = segment->start;
199 underline.end_index = segment->end;
200 composition_text_->mutable_underline_attributes()->push_back(underline);
203 // TODO(nona): Makes focus out mode configuable, if necessary.
204 UpdateComposition(*composition_text_, composition_cursor_, true);
208 bool InputMethodEngine::ClearComposition(int context_id,
209 std::string* error) {
211 *error = kErrorNotActive;
214 if (context_id != context_id_ || context_id_ == -1) {
215 *error = kErrorWrongContext;
219 composition_cursor_ = 0;
220 composition_text_.reset(new CompositionText());
221 UpdateComposition(*composition_text_, composition_cursor_, false);
225 bool InputMethodEngine::CommitText(int context_id, const char* text,
226 std::string* error) {
228 // TODO: Commit the text anyways.
229 *error = kErrorNotActive;
232 if (context_id != context_id_ || context_id_ == -1) {
233 *error = kErrorWrongContext;
237 IMEBridge::Get()->GetInputContextHandler()->CommitText(text);
239 // Records times for using input method.
240 if (!start_time_.ToInternalValue())
241 start_time_ = base::Time::Now();
242 end_time_ = base::Time::Now();
243 // Records histograms for counts of commits and committed characters.
244 RecordHistogram("Commit", 1);
245 RecordHistogram("CommitCharacter", GetUtf8StringLength(text));
249 bool InputMethodEngine::SendKeyEvents(
251 const std::vector<KeyboardEvent>& events) {
255 // context_id == 0, means sending key events to non-input field.
256 // context_id_ == -1, means the focus is not in an input field.
257 if (context_id != 0 && (context_id != context_id_ || context_id_ == -1)) {
261 ui::EventProcessor* dispatcher =
262 ash::Shell::GetPrimaryRootWindow()->GetHost()->event_processor();
264 for (size_t i = 0; i < events.size(); ++i) {
265 const KeyboardEvent& event = events[i];
266 const ui::EventType type =
267 (event.type == "keyup") ? ui::ET_KEY_RELEASED : ui::ET_KEY_PRESSED;
269 // KeyboardCodeFromXKyeSym assumes US keyboard layout.
270 ui::KeycodeConverter* conv = ui::KeycodeConverter::GetInstance();
273 // DOM code (KeyA) -> XKB -> XKeySym (XK_A) -> KeyboardCode (VKEY_A)
274 const uint16 native_keycode =
275 conv->CodeToNativeKeycode(event.code.c_str());
276 const uint xkeysym = ui::DefaultXKeysymFromHardwareKeycode(native_keycode);
277 const ui::KeyboardCode key_code = ui::KeyboardCodeFromXKeysym(xkeysym);
279 const std::string code = event.code;
280 int flags = ui::EF_NONE;
281 flags |= event.alt_key ? ui::EF_ALT_DOWN : ui::EF_NONE;
282 flags |= event.ctrl_key ? ui::EF_CONTROL_DOWN : ui::EF_NONE;
283 flags |= event.shift_key ? ui::EF_SHIFT_DOWN : ui::EF_NONE;
284 flags |= event.caps_lock ? ui::EF_CAPS_LOCK_DOWN : ui::EF_NONE;
286 ui::KeyEvent ui_event(type, key_code, code, flags, false /* is_char */);
287 base::AutoReset<const ui::KeyEvent*> reset_sent_key(&sent_key_event_,
289 ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&ui_event);
290 if (details.dispatcher_destroyed)
296 const InputMethodEngine::CandidateWindowProperty&
297 InputMethodEngine::GetCandidateWindowProperty() const {
298 return candidate_window_property_;
301 void InputMethodEngine::SetCandidateWindowProperty(
302 const CandidateWindowProperty& property) {
303 // Type conversion from InputMethodEngineInterface::CandidateWindowProperty to
304 // CandidateWindow::CandidateWindowProperty defined in chromeos/ime/.
305 ui::CandidateWindow::CandidateWindowProperty dest_property;
306 dest_property.page_size = property.page_size;
307 dest_property.is_cursor_visible = property.is_cursor_visible;
308 dest_property.is_vertical = property.is_vertical;
309 dest_property.show_window_at_composition =
310 property.show_window_at_composition;
311 dest_property.cursor_position =
312 candidate_window_->GetProperty().cursor_position;
313 dest_property.auxiliary_text = property.auxiliary_text;
314 dest_property.is_auxiliary_text_visible = property.is_auxiliary_text_visible;
316 candidate_window_->SetProperty(dest_property);
317 candidate_window_property_ = property;
320 IMECandidateWindowHandlerInterface* cw_handler =
321 IMEBridge::Get()->GetCandidateWindowHandler();
323 cw_handler->UpdateLookupTable(*candidate_window_, window_visible_);
327 bool InputMethodEngine::SetCandidateWindowVisible(bool visible,
328 std::string* error) {
330 *error = kErrorNotActive;
334 window_visible_ = visible;
335 IMECandidateWindowHandlerInterface* cw_handler =
336 IMEBridge::Get()->GetCandidateWindowHandler();
338 cw_handler->UpdateLookupTable(*candidate_window_, window_visible_);
342 bool InputMethodEngine::SetCandidates(
344 const std::vector<Candidate>& candidates,
345 std::string* error) {
347 *error = kErrorNotActive;
350 if (context_id != context_id_ || context_id_ == -1) {
351 *error = kErrorWrongContext;
355 // TODO: Nested candidates
356 candidate_ids_.clear();
357 candidate_indexes_.clear();
358 candidate_window_->mutable_candidates()->clear();
359 for (std::vector<Candidate>::const_iterator ix = candidates.begin();
360 ix != candidates.end(); ++ix) {
361 ui::CandidateWindow::Entry entry;
362 entry.value = base::UTF8ToUTF16(ix->value);
363 entry.label = base::UTF8ToUTF16(ix->label);
364 entry.annotation = base::UTF8ToUTF16(ix->annotation);
365 entry.description_title = base::UTF8ToUTF16(ix->usage.title);
366 entry.description_body = base::UTF8ToUTF16(ix->usage.body);
368 // Store a mapping from the user defined ID to the candidate index.
369 candidate_indexes_[ix->id] = candidate_ids_.size();
370 candidate_ids_.push_back(ix->id);
372 candidate_window_->mutable_candidates()->push_back(entry);
375 IMECandidateWindowHandlerInterface* cw_handler =
376 IMEBridge::Get()->GetCandidateWindowHandler();
378 cw_handler->UpdateLookupTable(*candidate_window_, window_visible_);
383 bool InputMethodEngine::SetCursorPosition(int context_id, int candidate_id,
384 std::string* error) {
386 *error = kErrorNotActive;
389 if (context_id != context_id_ || context_id_ == -1) {
390 *error = kErrorWrongContext;
394 std::map<int, int>::const_iterator position =
395 candidate_indexes_.find(candidate_id);
396 if (position == candidate_indexes_.end()) {
397 *error = kCandidateNotFound;
401 candidate_window_->set_cursor_position(position->second);
402 IMECandidateWindowHandlerInterface* cw_handler =
403 IMEBridge::Get()->GetCandidateWindowHandler();
405 cw_handler->UpdateLookupTable(*candidate_window_, window_visible_);
409 bool InputMethodEngine::SetMenuItems(const std::vector<MenuItem>& items) {
410 return UpdateMenuItems(items);
413 bool InputMethodEngine::UpdateMenuItems(
414 const std::vector<MenuItem>& items) {
418 ash::ime::InputMethodMenuItemList menu_item_list;
419 for (std::vector<MenuItem>::const_iterator item = items.begin();
420 item != items.end(); ++item) {
421 ash::ime::InputMethodMenuItem property;
422 MenuItemToProperty(*item, &property);
423 menu_item_list.push_back(property);
426 ash::ime::InputMethodMenuManager::GetInstance()->
427 SetCurrentInputMethodMenuItemList(
432 bool InputMethodEngine::IsActive() const {
436 void InputMethodEngine::KeyEventDone(input_method::KeyEventHandle* key_data,
438 KeyEventDoneCallback* callback =
439 reinterpret_cast<KeyEventDoneCallback*>(key_data);
440 callback->Run(handled);
444 bool InputMethodEngine::DeleteSurroundingText(int context_id,
446 size_t number_of_chars,
447 std::string* error) {
449 *error = kErrorNotActive;
452 if (context_id != context_id_ || context_id_ == -1) {
453 *error = kErrorWrongContext;
457 if (offset < 0 && static_cast<size_t>(-1 * offset) != size_t(number_of_chars))
458 return false; // Currently we can only support preceding text.
460 // TODO(nona): Return false if there is ongoing composition.
462 IMEInputContextHandlerInterface* input_context =
463 IMEBridge::Get()->GetInputContextHandler();
465 input_context->DeleteSurroundingText(offset, number_of_chars);
470 void InputMethodEngine::HideInputView() {
471 keyboard::KeyboardController* keyboard_controller =
472 keyboard::KeyboardController::GetInstance();
473 if (keyboard_controller) {
474 keyboard_controller->HideKeyboard(
475 keyboard::KeyboardController::HIDE_REASON_MANUAL);
479 void InputMethodEngine::EnableInputView(bool enabled) {
480 const GURL& url = enabled ? input_view_url_ : GURL();
481 keyboard::SetOverrideContentUrl(url);
482 keyboard::KeyboardController* keyboard_controller =
483 keyboard::KeyboardController::GetInstance();
484 if (keyboard_controller)
485 keyboard_controller->Reload();
488 void InputMethodEngine::FocusIn(
489 const IMEEngineHandlerInterface::InputContext& input_context) {
490 current_input_type_ = input_context.type;
492 if (!active_ || current_input_type_ == ui::TEXT_INPUT_TYPE_NONE)
495 // Prevent sending events on password field to 3rd-party IME extensions.
496 // And also make sure the VK fallback to system VK.
497 // TODO(shuchen): for password field, forcibly switch/lock the IME to the XKB
498 // keyboard related to the current IME.
499 if (current_input_type_ == ui::TEXT_INPUT_TYPE_PASSWORD &&
500 !extension_ime_util::IsComponentExtensionIME(GetDescriptor().id())) {
501 EnableInputView(false);
505 context_id_ = next_context_id_;
508 InputMethodEngineInterface::InputContext context;
509 context.id = context_id_;
510 switch (current_input_type_) {
511 case ui::TEXT_INPUT_TYPE_SEARCH:
512 context.type = "search";
514 case ui::TEXT_INPUT_TYPE_TELEPHONE:
515 context.type = "tel";
517 case ui::TEXT_INPUT_TYPE_URL:
518 context.type = "url";
520 case ui::TEXT_INPUT_TYPE_EMAIL:
521 context.type = "email";
523 case ui::TEXT_INPUT_TYPE_NUMBER:
524 context.type = "number";
526 case ui::TEXT_INPUT_TYPE_PASSWORD:
527 context.type = "password";
530 context.type = "text";
534 observer_->OnFocus(context);
537 void InputMethodEngine::FocusOut() {
538 if (!active_ || current_input_type_ == ui::TEXT_INPUT_TYPE_NONE)
541 ui::TextInputType previous_input_type = current_input_type_;
542 current_input_type_ = ui::TEXT_INPUT_TYPE_NONE;
544 // Prevent sending events on password field to 3rd-party IME extensions.
545 // And also make sure the VK restore to IME input view.
546 if (previous_input_type == ui::TEXT_INPUT_TYPE_PASSWORD &&
547 !extension_ime_util::IsComponentExtensionIME(GetDescriptor().id())) {
548 EnableInputView(true);
552 int context_id = context_id_;
554 observer_->OnBlur(context_id);
557 void InputMethodEngine::Enable() {
559 observer_->OnActivate(engine_id_);
560 current_input_type_ = IMEBridge::Get()->GetCurrentTextInputType();
561 FocusIn(IMEEngineHandlerInterface::InputContext(
562 current_input_type_, ui::TEXT_INPUT_MODE_DEFAULT));
563 EnableInputView(true);
565 start_time_ = base::Time();
566 end_time_ = base::Time();
567 RecordHistogram("Enable", 1);
570 void InputMethodEngine::Disable() {
572 observer_->OnDeactivated(engine_id_);
574 if (start_time_.ToInternalValue())
575 RecordHistogram("WorkingTime", (end_time_ - start_time_).InSeconds());
578 void InputMethodEngine::PropertyActivate(const std::string& property_name) {
579 observer_->OnMenuItemActivated(engine_id_, property_name);
582 void InputMethodEngine::Reset() {
583 observer_->OnReset(engine_id_);
587 void GetExtensionKeyboardEventFromKeyEvent(
588 const ui::KeyEvent& event,
589 InputMethodEngine::KeyboardEvent* ext_event) {
590 DCHECK(event.type() == ui::ET_KEY_RELEASED ||
591 event.type() == ui::ET_KEY_PRESSED);
593 ext_event->type = (event.type() == ui::ET_KEY_RELEASED) ? "keyup" : "keydown";
595 ext_event->code = event.code();
596 ext_event->alt_key = event.IsAltDown();
597 ext_event->ctrl_key = event.IsControlDown();
598 ext_event->shift_key = event.IsShiftDown();
599 ext_event->caps_lock = event.IsCapsLockDown();
601 uint32 x11_keysym = 0;
602 if (event.HasNativeEvent()) {
603 const base::NativeEvent& native_event = event.native_event();
604 DCHECK(native_event);
606 XKeyEvent* x_key = &(static_cast<XEvent*>(native_event)->xkey);
607 KeySym keysym = NoSymbol;
608 ::XLookupString(x_key, NULL, 0, &keysym, NULL);
611 // Convert ui::KeyEvent.key_code to DOM UIEvent key.
612 // XKeysymForWindowsKeyCode converts key_code to XKeySym, but it
613 // assumes US layout and does not care about CapLock state.
615 // TODO(komatsu): Support CapsLock states.
616 // TODO(komatsu): Support non-us keyboard layouts.
617 x11_keysym = ui::XKeysymForWindowsKeyCode(event.key_code(),
618 event.IsShiftDown());
620 ext_event->key = ui::FromXKeycodeToKeyValue(x11_keysym);
624 void InputMethodEngine::ProcessKeyEvent(
625 const ui::KeyEvent& key_event,
626 const KeyEventDoneCallback& callback) {
628 KeyEventDoneCallback *handler = new KeyEventDoneCallback();
631 KeyboardEvent ext_event;
632 GetExtensionKeyboardEventFromKeyEvent(key_event, &ext_event);
634 // If the given key event is equal to the key event sent by
635 // SendKeyEvents, this engine ID is propagated to the extension IME.
636 // Note, this check relies on that ui::KeyEvent is propagated as
637 // reference without copying.
638 if (&key_event == sent_key_event_)
639 ext_event.extension_id = extension_id_;
641 observer_->OnKeyEvent(
644 reinterpret_cast<input_method::KeyEventHandle*>(handler));
647 void InputMethodEngine::CandidateClicked(uint32 index) {
648 if (index > candidate_ids_.size()) {
652 // Only left button click is supported at this moment.
653 observer_->OnCandidateClicked(
654 engine_id_, candidate_ids_.at(index), MOUSE_BUTTON_LEFT);
657 void InputMethodEngine::SetSurroundingText(const std::string& text,
660 observer_->OnSurroundingTextChanged(engine_id_,
662 static_cast<int>(cursor_pos),
663 static_cast<int>(anchor_pos));
666 // TODO(uekawa): rename this method to a more reasonable name.
667 void InputMethodEngine::MenuItemToProperty(
668 const MenuItem& item,
669 ash::ime::InputMethodMenuItem* property) {
670 property->key = item.id;
672 if (item.modified & MENU_ITEM_MODIFIED_LABEL) {
673 property->label = item.label;
675 if (item.modified & MENU_ITEM_MODIFIED_VISIBLE) {
676 // TODO(nona): Implement it.
678 if (item.modified & MENU_ITEM_MODIFIED_CHECKED) {
679 property->is_selection_item_checked = item.checked;
681 if (item.modified & MENU_ITEM_MODIFIED_ENABLED) {
682 // TODO(nona): implement sensitive entry(crbug.com/140192).
684 if (item.modified & MENU_ITEM_MODIFIED_STYLE) {
685 if (!item.children.empty()) {
686 // TODO(nona): Implement it.
688 switch (item.style) {
689 case MENU_ITEM_STYLE_NONE:
692 case MENU_ITEM_STYLE_CHECK:
693 // TODO(nona): Implement it.
695 case MENU_ITEM_STYLE_RADIO:
696 property->is_selection_item = true;
698 case MENU_ITEM_STYLE_SEPARATOR:
699 // TODO(nona): Implement it.
705 // TODO(nona): Support item.children.
708 } // namespace chromeos