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"
12 #include "ash/ime/input_method_menu_item.h"
13 #include "ash/ime/input_method_menu_manager.h"
14 #include "ash/shell.h"
15 #include "base/logging.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/metrics/histogram.h"
18 #include "base/strings/string_number_conversions.h"
19 #include "base/strings/string_util.h"
20 #include "base/strings/stringprintf.h"
21 #include "base/strings/utf_string_conversions.h"
22 #include "chromeos/ime/component_extension_ime_manager.h"
23 #include "chromeos/ime/composition_text.h"
24 #include "chromeos/ime/extension_ime_util.h"
25 #include "chromeos/ime/input_method_manager.h"
26 #include "ui/aura/window.h"
27 #include "ui/aura/window_tree_host.h"
28 #include "ui/base/ime/candidate_window.h"
29 #include "ui/base/ime/chromeos/ime_keymap.h"
30 #include "ui/events/event.h"
31 #include "ui/events/event_processor.h"
32 #include "ui/keyboard/keyboard_controller.h"
33 #include "ui/keyboard/keyboard_util.h"
36 const char* kErrorNotActive = "IME is not active";
37 const char* kErrorWrongContext = "Context is not active";
38 const char* kCandidateNotFound = "Candidate not found";
42 // Notifies InputContextHandler that the composition is changed.
43 void UpdateComposition(const CompositionText& composition_text,
46 IMEInputContextHandlerInterface* input_context =
47 IMEBridge::Get()->GetInputContextHandler();
49 input_context->UpdateCompositionText(
50 composition_text, cursor_pos, is_visible);
53 // Returns the length of characters of a UTF-8 string with unknown string
54 // length. Cannot apply faster algorithm to count characters in an utf-8
55 // string without knowing the string length, so just does a full scan.
56 size_t GetUtf8StringLength(const char* s) {
59 if ((*s & 0xC0) != 0x80)
66 std::string GetKeyFromEvent(const ui::KeyEvent& event) {
67 const std::string& code = event.code();
68 if (StartsWithASCII(code, "Control", true))
70 if (StartsWithASCII(code, "Shift", true))
72 if (StartsWithASCII(code, "Alt", true))
74 if (StartsWithASCII(code, "Arrow", true))
75 return code.substr(5);
78 if (code == "Backspace" || code == "Tab" ||
79 code == "Enter" || code == "CapsLock")
82 // Ctrl+? cases, gets key value for Ctrl is not down.
83 if (event.flags() & ui::EF_CONTROL_DOWN) {
84 ui::KeyEvent event_no_ctrl(event.type(),
86 event.flags() ^ ui::EF_CONTROL_DOWN,
88 ch = event_no_ctrl.GetCharacter();
90 ch = event.GetCharacter();
92 return base::UTF16ToUTF8(base::string16(1, ch));
95 void GetExtensionKeyboardEventFromKeyEvent(
96 const ui::KeyEvent& event,
97 InputMethodEngine::KeyboardEvent* ext_event) {
98 DCHECK(event.type() == ui::ET_KEY_RELEASED ||
99 event.type() == ui::ET_KEY_PRESSED);
101 ext_event->type = (event.type() == ui::ET_KEY_RELEASED) ? "keyup" : "keydown";
103 ext_event->code = event.code();
104 ext_event->key_code = static_cast<int>(event.key_code());
105 ext_event->alt_key = event.IsAltDown();
106 ext_event->ctrl_key = event.IsControlDown();
107 ext_event->shift_key = event.IsShiftDown();
108 ext_event->caps_lock = event.IsCapsLockDown();
109 ext_event->key = GetKeyFromEvent(event);
114 InputMethodEngine::InputMethodEngine()
115 : current_input_type_(ui::TEXT_INPUT_TYPE_NONE),
119 composition_text_(new CompositionText()),
120 composition_cursor_(0),
121 candidate_window_(new ui::CandidateWindow()),
122 window_visible_(false),
123 sent_key_event_(NULL),
127 InputMethodEngine::~InputMethodEngine() {
128 if (start_time_.ToInternalValue())
129 RecordHistogram("WorkingTime", (end_time_ - start_time_).InSeconds());
130 input_method::InputMethodManager::Get()->RemoveInputMethodExtension(profile_,
134 void InputMethodEngine::Initialize(
136 scoped_ptr<InputMethodEngineInterface::Observer> observer,
137 const char* engine_name,
138 const char* extension_id,
139 const char* engine_id,
140 const std::vector<std::string>& languages,
141 const std::vector<std::string>& layouts,
142 const GURL& options_page,
143 const GURL& input_view) {
144 DCHECK(observer) << "Observer must not be null.";
148 // TODO(komatsu): It is probably better to set observer out of Initialize.
149 observer_ = observer.Pass();
150 engine_id_ = engine_id;
151 extension_id_ = extension_id;
153 input_method::InputMethodManager* manager =
154 input_method::InputMethodManager::Get();
155 ComponentExtensionIMEManager* comp_ext_ime_manager =
156 manager->GetComponentExtensionIMEManager();
158 if (comp_ext_ime_manager && comp_ext_ime_manager->IsInitialized() &&
159 comp_ext_ime_manager->IsWhitelistedExtension(extension_id)) {
160 imm_id_ = comp_ext_ime_manager->GetId(extension_id, engine_id);
162 imm_id_ = extension_ime_util::GetInputMethodID(extension_id, engine_id);
165 input_view_url_ = input_view;
166 descriptor_ = input_method::InputMethodDescriptor(
169 std::string(), // TODO(uekawa): Set short name.
172 extension_ime_util::IsKeyboardLayoutExtension(
173 imm_id_), // is_login_keyboard
177 // TODO(komatsu): It is probably better to call AddInputMethodExtension
178 // out of Initialize.
179 manager->AddInputMethodExtension(profile, imm_id_, this);
182 const input_method::InputMethodDescriptor& InputMethodEngine::GetDescriptor()
187 void InputMethodEngine::RecordHistogram(const char* name, int count) {
188 std::string histo_name =
189 base::StringPrintf("InputMethod.%s.%s", name, engine_id_.c_str());
190 base::HistogramBase* counter = base::Histogram::FactoryGet(
191 histo_name, 0, 1000000, 50, base::HistogramBase::kNoFlags);
196 void InputMethodEngine::NotifyImeReady() {
197 input_method::InputMethodManager* manager =
198 input_method::InputMethodManager::Get();
199 if (manager && imm_id_ == manager->GetCurrentInputMethod().id())
203 bool InputMethodEngine::SetComposition(
209 const std::vector<SegmentInfo>& segments,
210 std::string* error) {
212 *error = kErrorNotActive;
215 if (context_id != context_id_ || context_id_ == -1) {
216 *error = kErrorWrongContext;
220 composition_cursor_ = cursor;
221 composition_text_.reset(new CompositionText());
222 composition_text_->set_text(base::UTF8ToUTF16(text));
224 composition_text_->set_selection_start(selection_start);
225 composition_text_->set_selection_end(selection_end);
227 // TODO: Add support for displaying selected text in the composition string.
228 for (std::vector<SegmentInfo>::const_iterator segment = segments.begin();
229 segment != segments.end(); ++segment) {
230 CompositionText::UnderlineAttribute underline;
232 switch (segment->style) {
233 case SEGMENT_STYLE_UNDERLINE:
234 underline.type = CompositionText::COMPOSITION_TEXT_UNDERLINE_SINGLE;
236 case SEGMENT_STYLE_DOUBLE_UNDERLINE:
237 underline.type = CompositionText::COMPOSITION_TEXT_UNDERLINE_DOUBLE;
243 underline.start_index = segment->start;
244 underline.end_index = segment->end;
245 composition_text_->mutable_underline_attributes()->push_back(underline);
248 // TODO(nona): Makes focus out mode configuable, if necessary.
249 UpdateComposition(*composition_text_, composition_cursor_, true);
253 bool InputMethodEngine::ClearComposition(int context_id,
254 std::string* error) {
256 *error = kErrorNotActive;
259 if (context_id != context_id_ || context_id_ == -1) {
260 *error = kErrorWrongContext;
264 composition_cursor_ = 0;
265 composition_text_.reset(new CompositionText());
266 UpdateComposition(*composition_text_, composition_cursor_, false);
270 bool InputMethodEngine::CommitText(int context_id, const char* text,
271 std::string* error) {
273 // TODO: Commit the text anyways.
274 *error = kErrorNotActive;
277 if (context_id != context_id_ || context_id_ == -1) {
278 *error = kErrorWrongContext;
282 IMEBridge::Get()->GetInputContextHandler()->CommitText(text);
284 // Records times for using input method.
285 if (!start_time_.ToInternalValue())
286 start_time_ = base::Time::Now();
287 end_time_ = base::Time::Now();
288 // Records histograms for counts of commits and committed characters.
289 RecordHistogram("Commit", 1);
290 RecordHistogram("CommitCharacter", GetUtf8StringLength(text));
294 bool InputMethodEngine::SendKeyEvents(
296 const std::vector<KeyboardEvent>& events) {
300 // context_id == 0, means sending key events to non-input field.
301 // context_id_ == -1, means the focus is not in an input field.
302 if (context_id != 0 && (context_id != context_id_ || context_id_ == -1)) {
306 ui::EventProcessor* dispatcher =
307 ash::Shell::GetPrimaryRootWindow()->GetHost()->event_processor();
309 for (size_t i = 0; i < events.size(); ++i) {
310 const KeyboardEvent& event = events[i];
311 const ui::EventType type =
312 (event.type == "keyup") ? ui::ET_KEY_RELEASED : ui::ET_KEY_PRESSED;
313 ui::KeyboardCode key_code = static_cast<ui::KeyboardCode>(event.key_code);
314 if (key_code == ui::VKEY_UNKNOWN)
315 key_code = ui::DomKeycodeToKeyboardCode(event.code);
317 int flags = ui::EF_NONE;
318 flags |= event.alt_key ? ui::EF_ALT_DOWN : ui::EF_NONE;
319 flags |= event.ctrl_key ? ui::EF_CONTROL_DOWN : ui::EF_NONE;
320 flags |= event.shift_key ? ui::EF_SHIFT_DOWN : ui::EF_NONE;
321 flags |= event.caps_lock ? ui::EF_CAPS_LOCK_DOWN : ui::EF_NONE;
323 ui::KeyEvent ui_event(type,
327 false /* is_char */);
328 // 4-bytes UTF-8 string is at least 2-characters UTF-16 string.
329 // And Key char can only be single UTF-16 character.
330 if (!event.key.empty() && event.key.size() < 4) {
331 base::string16 key_char = base::UTF8ToUTF16(event.key);
332 if (key_char.size() == 1)
333 ui_event.set_character(key_char[0]);
335 base::AutoReset<const ui::KeyEvent*> reset_sent_key(&sent_key_event_,
337 ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&ui_event);
338 if (details.dispatcher_destroyed)
344 const InputMethodEngine::CandidateWindowProperty&
345 InputMethodEngine::GetCandidateWindowProperty() const {
346 return candidate_window_property_;
349 void InputMethodEngine::SetCandidateWindowProperty(
350 const CandidateWindowProperty& property) {
351 // Type conversion from InputMethodEngineInterface::CandidateWindowProperty to
352 // CandidateWindow::CandidateWindowProperty defined in chromeos/ime/.
353 ui::CandidateWindow::CandidateWindowProperty dest_property;
354 dest_property.page_size = property.page_size;
355 dest_property.is_cursor_visible = property.is_cursor_visible;
356 dest_property.is_vertical = property.is_vertical;
357 dest_property.show_window_at_composition =
358 property.show_window_at_composition;
359 dest_property.cursor_position =
360 candidate_window_->GetProperty().cursor_position;
361 dest_property.auxiliary_text = property.auxiliary_text;
362 dest_property.is_auxiliary_text_visible = property.is_auxiliary_text_visible;
364 candidate_window_->SetProperty(dest_property);
365 candidate_window_property_ = property;
368 IMECandidateWindowHandlerInterface* cw_handler =
369 IMEBridge::Get()->GetCandidateWindowHandler();
371 cw_handler->UpdateLookupTable(*candidate_window_, window_visible_);
375 bool InputMethodEngine::SetCandidateWindowVisible(bool visible,
376 std::string* error) {
378 *error = kErrorNotActive;
382 window_visible_ = visible;
383 IMECandidateWindowHandlerInterface* cw_handler =
384 IMEBridge::Get()->GetCandidateWindowHandler();
386 cw_handler->UpdateLookupTable(*candidate_window_, window_visible_);
390 bool InputMethodEngine::SetCandidates(
392 const std::vector<Candidate>& candidates,
393 std::string* error) {
395 *error = kErrorNotActive;
398 if (context_id != context_id_ || context_id_ == -1) {
399 *error = kErrorWrongContext;
403 // TODO: Nested candidates
404 candidate_ids_.clear();
405 candidate_indexes_.clear();
406 candidate_window_->mutable_candidates()->clear();
407 for (std::vector<Candidate>::const_iterator ix = candidates.begin();
408 ix != candidates.end(); ++ix) {
409 ui::CandidateWindow::Entry entry;
410 entry.value = base::UTF8ToUTF16(ix->value);
411 entry.label = base::UTF8ToUTF16(ix->label);
412 entry.annotation = base::UTF8ToUTF16(ix->annotation);
413 entry.description_title = base::UTF8ToUTF16(ix->usage.title);
414 entry.description_body = base::UTF8ToUTF16(ix->usage.body);
416 // Store a mapping from the user defined ID to the candidate index.
417 candidate_indexes_[ix->id] = candidate_ids_.size();
418 candidate_ids_.push_back(ix->id);
420 candidate_window_->mutable_candidates()->push_back(entry);
423 IMECandidateWindowHandlerInterface* cw_handler =
424 IMEBridge::Get()->GetCandidateWindowHandler();
426 cw_handler->UpdateLookupTable(*candidate_window_, window_visible_);
431 bool InputMethodEngine::SetCursorPosition(int context_id, int candidate_id,
432 std::string* error) {
434 *error = kErrorNotActive;
437 if (context_id != context_id_ || context_id_ == -1) {
438 *error = kErrorWrongContext;
442 std::map<int, int>::const_iterator position =
443 candidate_indexes_.find(candidate_id);
444 if (position == candidate_indexes_.end()) {
445 *error = kCandidateNotFound;
449 candidate_window_->set_cursor_position(position->second);
450 IMECandidateWindowHandlerInterface* cw_handler =
451 IMEBridge::Get()->GetCandidateWindowHandler();
453 cw_handler->UpdateLookupTable(*candidate_window_, window_visible_);
457 bool InputMethodEngine::SetMenuItems(const std::vector<MenuItem>& items) {
458 return UpdateMenuItems(items);
461 bool InputMethodEngine::UpdateMenuItems(
462 const std::vector<MenuItem>& items) {
466 ash::ime::InputMethodMenuItemList menu_item_list;
467 for (std::vector<MenuItem>::const_iterator item = items.begin();
468 item != items.end(); ++item) {
469 ash::ime::InputMethodMenuItem property;
470 MenuItemToProperty(*item, &property);
471 menu_item_list.push_back(property);
474 ash::ime::InputMethodMenuManager::GetInstance()->
475 SetCurrentInputMethodMenuItemList(
480 bool InputMethodEngine::IsActive() const {
484 bool InputMethodEngine::DeleteSurroundingText(int context_id,
486 size_t number_of_chars,
487 std::string* error) {
489 *error = kErrorNotActive;
492 if (context_id != context_id_ || context_id_ == -1) {
493 *error = kErrorWrongContext;
497 if (offset < 0 && static_cast<size_t>(-1 * offset) != size_t(number_of_chars))
498 return false; // Currently we can only support preceding text.
500 // TODO(nona): Return false if there is ongoing composition.
502 IMEInputContextHandlerInterface* input_context =
503 IMEBridge::Get()->GetInputContextHandler();
505 input_context->DeleteSurroundingText(offset, number_of_chars);
510 void InputMethodEngine::HideInputView() {
511 keyboard::KeyboardController* keyboard_controller =
512 keyboard::KeyboardController::GetInstance();
513 if (keyboard_controller) {
514 keyboard_controller->HideKeyboard(
515 keyboard::KeyboardController::HIDE_REASON_MANUAL);
519 void InputMethodEngine::EnableInputView(bool enabled) {
520 const GURL& url = enabled ? input_view_url_ : GURL();
521 keyboard::SetOverrideContentUrl(url);
522 keyboard::KeyboardController* keyboard_controller =
523 keyboard::KeyboardController::GetInstance();
524 if (keyboard_controller)
525 keyboard_controller->Reload();
528 void InputMethodEngine::FocusIn(
529 const IMEEngineHandlerInterface::InputContext& input_context) {
530 current_input_type_ = input_context.type;
532 if (!active_ || current_input_type_ == ui::TEXT_INPUT_TYPE_NONE)
535 context_id_ = next_context_id_;
538 InputMethodEngineInterface::InputContext context;
539 context.id = context_id_;
540 switch (current_input_type_) {
541 case ui::TEXT_INPUT_TYPE_SEARCH:
542 context.type = "search";
544 case ui::TEXT_INPUT_TYPE_TELEPHONE:
545 context.type = "tel";
547 case ui::TEXT_INPUT_TYPE_URL:
548 context.type = "url";
550 case ui::TEXT_INPUT_TYPE_EMAIL:
551 context.type = "email";
553 case ui::TEXT_INPUT_TYPE_NUMBER:
554 context.type = "number";
556 case ui::TEXT_INPUT_TYPE_PASSWORD:
557 context.type = "password";
560 context.type = "text";
564 observer_->OnFocus(context);
567 void InputMethodEngine::FocusOut() {
568 if (!active_ || current_input_type_ == ui::TEXT_INPUT_TYPE_NONE)
571 current_input_type_ = ui::TEXT_INPUT_TYPE_NONE;
573 int context_id = context_id_;
575 observer_->OnBlur(context_id);
578 void InputMethodEngine::Enable() {
580 observer_->OnActivate(engine_id_);
581 current_input_type_ = IMEBridge::Get()->GetCurrentTextInputType();
582 FocusIn(IMEEngineHandlerInterface::InputContext(
583 current_input_type_, ui::TEXT_INPUT_MODE_DEFAULT));
584 EnableInputView(true);
586 start_time_ = base::Time();
587 end_time_ = base::Time();
588 RecordHistogram("Enable", 1);
591 void InputMethodEngine::Disable() {
593 observer_->OnDeactivated(engine_id_);
595 if (start_time_.ToInternalValue())
596 RecordHistogram("WorkingTime", (end_time_ - start_time_).InSeconds());
599 void InputMethodEngine::PropertyActivate(const std::string& property_name) {
600 observer_->OnMenuItemActivated(engine_id_, property_name);
603 void InputMethodEngine::Reset() {
604 observer_->OnReset(engine_id_);
607 void InputMethodEngine::ProcessKeyEvent(
608 const ui::KeyEvent& key_event,
609 const KeyEventDoneCallback& callback) {
611 KeyEventDoneCallback *handler = new KeyEventDoneCallback();
614 KeyboardEvent ext_event;
615 GetExtensionKeyboardEventFromKeyEvent(key_event, &ext_event);
617 // If the given key event is equal to the key event sent by
618 // SendKeyEvents, this engine ID is propagated to the extension IME.
619 // Note, this check relies on that ui::KeyEvent is propagated as
620 // reference without copying.
621 if (&key_event == sent_key_event_)
622 ext_event.extension_id = extension_id_;
624 observer_->OnKeyEvent(
627 reinterpret_cast<input_method::KeyEventHandle*>(handler));
630 void InputMethodEngine::CandidateClicked(uint32 index) {
631 if (index > candidate_ids_.size()) {
635 // Only left button click is supported at this moment.
636 observer_->OnCandidateClicked(
637 engine_id_, candidate_ids_.at(index), MOUSE_BUTTON_LEFT);
640 void InputMethodEngine::SetSurroundingText(const std::string& text,
643 observer_->OnSurroundingTextChanged(engine_id_,
645 static_cast<int>(cursor_pos),
646 static_cast<int>(anchor_pos));
649 // TODO(uekawa): rename this method to a more reasonable name.
650 void InputMethodEngine::MenuItemToProperty(
651 const MenuItem& item,
652 ash::ime::InputMethodMenuItem* property) {
653 property->key = item.id;
655 if (item.modified & MENU_ITEM_MODIFIED_LABEL) {
656 property->label = item.label;
658 if (item.modified & MENU_ITEM_MODIFIED_VISIBLE) {
659 // TODO(nona): Implement it.
661 if (item.modified & MENU_ITEM_MODIFIED_CHECKED) {
662 property->is_selection_item_checked = item.checked;
664 if (item.modified & MENU_ITEM_MODIFIED_ENABLED) {
665 // TODO(nona): implement sensitive entry(crbug.com/140192).
667 if (item.modified & MENU_ITEM_MODIFIED_STYLE) {
668 if (!item.children.empty()) {
669 // TODO(nona): Implement it.
671 switch (item.style) {
672 case MENU_ITEM_STYLE_NONE:
675 case MENU_ITEM_STYLE_CHECK:
676 // TODO(nona): Implement it.
678 case MENU_ITEM_STYLE_RADIO:
679 property->is_selection_item = true;
681 case MENU_ITEM_STYLE_SEPARATOR:
682 // TODO(nona): Implement it.
688 // TODO(nona): Support item.children.
691 } // namespace chromeos