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/shell.h"
13 #include "base/logging.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/metrics/histogram.h"
16 #include "base/strings/string_number_conversions.h"
17 #include "base/strings/string_util.h"
18 #include "base/strings/stringprintf.h"
19 #include "base/strings/utf_string_conversions.h"
20 #include "chrome/browser/profiles/profile_manager.h"
21 #include "ui/aura/window.h"
22 #include "ui/aura/window_tree_host.h"
23 #include "ui/base/ime/candidate_window.h"
24 #include "ui/base/ime/chromeos/component_extension_ime_manager.h"
25 #include "ui/base/ime/chromeos/composition_text_chromeos.h"
26 #include "ui/base/ime/chromeos/extension_ime_util.h"
27 #include "ui/base/ime/chromeos/ime_keymap.h"
28 #include "ui/base/ime/chromeos/input_method_manager.h"
29 #include "ui/base/ime/input_method.h"
30 #include "ui/base/ime/text_input_flags.h"
31 #include "ui/chromeos/ime/input_method_menu_item.h"
32 #include "ui/chromeos/ime/input_method_menu_manager.h"
33 #include "ui/events/event.h"
34 #include "ui/events/event_processor.h"
35 #include "ui/events/event_utils.h"
36 #include "ui/events/keycodes/dom/dom_code.h"
37 #include "ui/events/keycodes/dom/keycode_converter.h"
38 #include "ui/keyboard/keyboard_controller.h"
39 #include "ui/keyboard/keyboard_util.h"
45 const char kErrorNotActive
[] = "IME is not active";
46 const char kErrorWrongContext
[] = "Context is not active";
47 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)
73 std::string
GetKeyFromEvent(const ui::KeyEvent
& event
) {
74 const std::string code
= event
.GetCodeString();
75 if (base::StartsWith(code
, "Control", base::CompareCase::SENSITIVE
))
77 if (base::StartsWith(code
, "Shift", base::CompareCase::SENSITIVE
))
79 if (base::StartsWith(code
, "Alt", base::CompareCase::SENSITIVE
))
81 if (base::StartsWith(code
, "Arrow", base::CompareCase::SENSITIVE
))
82 return code
.substr(5);
85 if (code
== "Backspace" || code
== "Tab" ||
86 code
== "Enter" || code
== "CapsLock" ||
89 // Cases for media keys.
90 switch (event
.key_code()) {
91 case ui::VKEY_BROWSER_BACK
:
94 case ui::VKEY_BROWSER_FORWARD
:
96 return "HistoryForward";
97 case ui::VKEY_BROWSER_REFRESH
:
99 return "BrowserRefresh";
100 case ui::VKEY_MEDIA_LAUNCH_APP2
:
102 return "ChromeOSFullscreen";
103 case ui::VKEY_MEDIA_LAUNCH_APP1
:
105 return "ChromeOSSwitchWindow";
106 case ui::VKEY_BRIGHTNESS_DOWN
:
108 return "BrightnessDown";
109 case ui::VKEY_BRIGHTNESS_UP
:
111 return "BrightnessUp";
112 case ui::VKEY_VOLUME_MUTE
:
114 return "AudioVolumeMute";
115 case ui::VKEY_VOLUME_DOWN
:
117 return "AudioVolumeDown";
118 case ui::VKEY_VOLUME_UP
:
120 return "AudioVolumeUp";
125 // Ctrl+? cases, gets key value for Ctrl is not down.
126 if (event
.flags() & ui::EF_CONTROL_DOWN
) {
127 ui::KeyEvent
event_no_ctrl(event
.type(),
129 event
.flags() ^ ui::EF_CONTROL_DOWN
);
130 ch
= event_no_ctrl
.GetCharacter();
132 ch
= event
.GetCharacter();
134 return base::UTF16ToUTF8(base::string16(1, ch
));
137 void GetExtensionKeyboardEventFromKeyEvent(
138 const ui::KeyEvent
& event
,
139 InputMethodEngine::KeyboardEvent
* ext_event
) {
140 DCHECK(event
.type() == ui::ET_KEY_RELEASED
||
141 event
.type() == ui::ET_KEY_PRESSED
);
143 ext_event
->type
= (event
.type() == ui::ET_KEY_RELEASED
) ? "keyup" : "keydown";
145 if (event
.code() == ui::DomCode::NONE
)
146 ext_event
->code
= ui::KeyboardCodeToDomKeycode(event
.key_code());
148 ext_event
->code
= event
.GetCodeString();
149 ext_event
->key_code
= static_cast<int>(event
.key_code());
150 ext_event
->alt_key
= event
.IsAltDown();
151 ext_event
->ctrl_key
= event
.IsControlDown();
152 ext_event
->shift_key
= event
.IsShiftDown();
153 ext_event
->caps_lock
= event
.IsCapsLockDown();
154 ext_event
->key
= GetKeyFromEvent(event
);
159 InputMethodEngine::InputMethodEngine()
160 : current_input_type_(ui::TEXT_INPUT_TYPE_NONE
),
163 composition_text_(new CompositionText()),
164 composition_cursor_(0),
165 candidate_window_(new ui::CandidateWindow()),
166 window_visible_(false),
167 sent_key_event_(NULL
),
171 InputMethodEngine::~InputMethodEngine() {
174 void InputMethodEngine::Initialize(
175 scoped_ptr
<InputMethodEngineInterface::Observer
> observer
,
176 const char* extension_id
,
178 DCHECK(observer
) << "Observer must not be null.";
180 // TODO(komatsu): It is probably better to set observer out of Initialize.
181 observer_
= observer
.Pass();
182 extension_id_
= extension_id
;
186 const std::string
& InputMethodEngine::GetActiveComponentId() const {
187 return active_component_id_
;
190 bool InputMethodEngine::SetComposition(
196 const std::vector
<SegmentInfo
>& segments
,
197 std::string
* error
) {
199 *error
= kErrorNotActive
;
202 if (context_id
!= context_id_
|| context_id_
== -1) {
203 *error
= kErrorWrongContext
;
207 composition_cursor_
= cursor
;
208 composition_text_
.reset(new CompositionText());
209 composition_text_
->set_text(base::UTF8ToUTF16(text
));
211 composition_text_
->set_selection_start(selection_start
);
212 composition_text_
->set_selection_end(selection_end
);
214 // TODO: Add support for displaying selected text in the composition string.
215 for (std::vector
<SegmentInfo
>::const_iterator segment
= segments
.begin();
216 segment
!= segments
.end(); ++segment
) {
217 CompositionText::UnderlineAttribute underline
;
219 switch (segment
->style
) {
220 case SEGMENT_STYLE_UNDERLINE
:
221 underline
.type
= CompositionText::COMPOSITION_TEXT_UNDERLINE_SINGLE
;
223 case SEGMENT_STYLE_DOUBLE_UNDERLINE
:
224 underline
.type
= CompositionText::COMPOSITION_TEXT_UNDERLINE_DOUBLE
;
226 case SEGMENT_STYLE_NO_UNDERLINE
:
227 underline
.type
= CompositionText::COMPOSITION_TEXT_UNDERLINE_NONE
;
233 underline
.start_index
= segment
->start
;
234 underline
.end_index
= segment
->end
;
235 composition_text_
->mutable_underline_attributes()->push_back(underline
);
238 // TODO(nona): Makes focus out mode configuable, if necessary.
239 UpdateComposition(*composition_text_
, composition_cursor_
, true);
243 bool InputMethodEngine::ClearComposition(int context_id
,
244 std::string
* error
) {
246 *error
= kErrorNotActive
;
249 if (context_id
!= context_id_
|| context_id_
== -1) {
250 *error
= kErrorWrongContext
;
254 composition_cursor_
= 0;
255 composition_text_
.reset(new CompositionText());
256 UpdateComposition(*composition_text_
, composition_cursor_
, false);
260 bool InputMethodEngine::CommitText(int context_id
, const char* text
,
261 std::string
* error
) {
263 // TODO: Commit the text anyways.
264 *error
= kErrorNotActive
;
267 if (context_id
!= context_id_
|| context_id_
== -1) {
268 *error
= kErrorWrongContext
;
272 IMEBridge::Get()->GetInputContextHandler()->CommitText(text
);
274 // Records histograms for committed characters.
275 if (!composition_text_
->text().empty()) {
276 size_t len
= GetUtf8StringLength(text
);
277 UMA_HISTOGRAM_CUSTOM_COUNTS("InputMethod.CommitLength",
283 bool InputMethodEngine::SendKeyEvents(
285 const std::vector
<KeyboardEvent
>& events
) {
289 // context_id == 0, means sending key events to non-input field.
290 // context_id_ == -1, means the focus is not in an input field.
291 if (context_id
!= 0 && (context_id
!= context_id_
|| context_id_
== -1)) {
295 ui::InputMethod
* input_method
=
296 ash::Shell::GetTargetRootWindow()->GetHost()->GetInputMethod();
298 for (size_t i
= 0; i
< events
.size(); ++i
) {
299 const KeyboardEvent
& event
= events
[i
];
300 const ui::EventType type
=
301 (event
.type
== "keyup") ? ui::ET_KEY_RELEASED
: ui::ET_KEY_PRESSED
;
302 ui::KeyboardCode key_code
= static_cast<ui::KeyboardCode
>(event
.key_code
);
303 if (key_code
== ui::VKEY_UNKNOWN
)
304 key_code
= ui::DomKeycodeToKeyboardCode(event
.code
);
306 int flags
= ui::EF_NONE
;
307 flags
|= event
.alt_key
? ui::EF_ALT_DOWN
: ui::EF_NONE
;
308 flags
|= event
.ctrl_key
? ui::EF_CONTROL_DOWN
: ui::EF_NONE
;
309 flags
|= event
.shift_key
? ui::EF_SHIFT_DOWN
: ui::EF_NONE
;
310 flags
|= event
.caps_lock
? ui::EF_CAPS_LOCK_DOWN
: ui::EF_NONE
;
313 // 4-bytes UTF-8 string is at least 2-characters UTF-16 string.
314 // And Key char can only be single UTF-16 character.
315 if (!event
.key
.empty() && event
.key
.size() < 4) {
316 base::string16 key_char
= base::UTF8ToUTF16(event
.key
);
317 if (key_char
.size() == 1)
320 ui::KeyEvent
ui_event(
322 ui::KeycodeConverter::CodeStringToDomCode(event
.code
.c_str()), flags
,
323 ui::KeycodeConverter::KeyStringToDomKey(event
.key
.c_str()), ch
,
324 ui::EventTimeForNow());
325 base::AutoReset
<const ui::KeyEvent
*> reset_sent_key(&sent_key_event_
,
327 input_method
->DispatchKeyEvent(ui_event
);
333 const InputMethodEngine::CandidateWindowProperty
&
334 InputMethodEngine::GetCandidateWindowProperty() const {
335 return candidate_window_property_
;
338 void InputMethodEngine::SetCandidateWindowProperty(
339 const CandidateWindowProperty
& property
) {
340 // Type conversion from InputMethodEngineInterface::CandidateWindowProperty to
341 // CandidateWindow::CandidateWindowProperty defined in chromeos/ime/.
342 ui::CandidateWindow::CandidateWindowProperty dest_property
;
343 dest_property
.page_size
= property
.page_size
;
344 dest_property
.is_cursor_visible
= property
.is_cursor_visible
;
345 dest_property
.is_vertical
= property
.is_vertical
;
346 dest_property
.show_window_at_composition
=
347 property
.show_window_at_composition
;
348 dest_property
.cursor_position
=
349 candidate_window_
->GetProperty().cursor_position
;
350 dest_property
.auxiliary_text
= property
.auxiliary_text
;
351 dest_property
.is_auxiliary_text_visible
= property
.is_auxiliary_text_visible
;
353 candidate_window_
->SetProperty(dest_property
);
354 candidate_window_property_
= property
;
357 IMECandidateWindowHandlerInterface
* cw_handler
=
358 IMEBridge::Get()->GetCandidateWindowHandler();
360 cw_handler
->UpdateLookupTable(*candidate_window_
, window_visible_
);
364 bool InputMethodEngine::SetCandidateWindowVisible(bool visible
,
365 std::string
* error
) {
367 *error
= kErrorNotActive
;
371 window_visible_
= visible
;
372 IMECandidateWindowHandlerInterface
* cw_handler
=
373 IMEBridge::Get()->GetCandidateWindowHandler();
375 cw_handler
->UpdateLookupTable(*candidate_window_
, window_visible_
);
379 bool InputMethodEngine::SetCandidates(
381 const std::vector
<Candidate
>& candidates
,
382 std::string
* error
) {
384 *error
= kErrorNotActive
;
387 if (context_id
!= context_id_
|| context_id_
== -1) {
388 *error
= kErrorWrongContext
;
392 // TODO: Nested candidates
393 candidate_ids_
.clear();
394 candidate_indexes_
.clear();
395 candidate_window_
->mutable_candidates()->clear();
396 for (std::vector
<Candidate
>::const_iterator ix
= candidates
.begin();
397 ix
!= candidates
.end(); ++ix
) {
398 ui::CandidateWindow::Entry entry
;
399 entry
.value
= base::UTF8ToUTF16(ix
->value
);
400 entry
.label
= base::UTF8ToUTF16(ix
->label
);
401 entry
.annotation
= base::UTF8ToUTF16(ix
->annotation
);
402 entry
.description_title
= base::UTF8ToUTF16(ix
->usage
.title
);
403 entry
.description_body
= base::UTF8ToUTF16(ix
->usage
.body
);
405 // Store a mapping from the user defined ID to the candidate index.
406 candidate_indexes_
[ix
->id
] = candidate_ids_
.size();
407 candidate_ids_
.push_back(ix
->id
);
409 candidate_window_
->mutable_candidates()->push_back(entry
);
412 IMECandidateWindowHandlerInterface
* cw_handler
=
413 IMEBridge::Get()->GetCandidateWindowHandler();
415 cw_handler
->UpdateLookupTable(*candidate_window_
, window_visible_
);
420 bool InputMethodEngine::SetCursorPosition(int context_id
, int candidate_id
,
421 std::string
* error
) {
423 *error
= kErrorNotActive
;
426 if (context_id
!= context_id_
|| context_id_
== -1) {
427 *error
= kErrorWrongContext
;
431 std::map
<int, int>::const_iterator position
=
432 candidate_indexes_
.find(candidate_id
);
433 if (position
== candidate_indexes_
.end()) {
434 *error
= kCandidateNotFound
;
438 candidate_window_
->set_cursor_position(position
->second
);
439 IMECandidateWindowHandlerInterface
* cw_handler
=
440 IMEBridge::Get()->GetCandidateWindowHandler();
442 cw_handler
->UpdateLookupTable(*candidate_window_
, window_visible_
);
446 bool InputMethodEngine::SetMenuItems(const std::vector
<MenuItem
>& items
) {
447 return UpdateMenuItems(items
);
450 bool InputMethodEngine::UpdateMenuItems(
451 const std::vector
<MenuItem
>& items
) {
455 ui::ime::InputMethodMenuItemList menu_item_list
;
456 for (std::vector
<MenuItem
>::const_iterator item
= items
.begin();
457 item
!= items
.end(); ++item
) {
458 ui::ime::InputMethodMenuItem property
;
459 MenuItemToProperty(*item
, &property
);
460 menu_item_list
.push_back(property
);
463 ui::ime::InputMethodMenuManager::GetInstance()->
464 SetCurrentInputMethodMenuItemList(
469 bool InputMethodEngine::IsActive() const {
470 return !active_component_id_
.empty();
473 bool InputMethodEngine::DeleteSurroundingText(int context_id
,
475 size_t number_of_chars
,
476 std::string
* error
) {
478 *error
= kErrorNotActive
;
481 if (context_id
!= context_id_
|| context_id_
== -1) {
482 *error
= kErrorWrongContext
;
486 // TODO(nona): Return false if there is ongoing composition.
488 IMEInputContextHandlerInterface
* input_context
=
489 IMEBridge::Get()->GetInputContextHandler();
491 input_context
->DeleteSurroundingText(offset
, number_of_chars
);
496 void InputMethodEngine::HideInputView() {
497 keyboard::KeyboardController
* keyboard_controller
=
498 keyboard::KeyboardController::GetInstance();
499 if (keyboard_controller
) {
500 keyboard_controller
->HideKeyboard(
501 keyboard::KeyboardController::HIDE_REASON_MANUAL
);
505 void InputMethodEngine::SetCompositionBounds(
506 const std::vector
<gfx::Rect
>& bounds
) {
509 observer_
->OnCompositionBoundsChanged(bounds
);
512 void InputMethodEngine::EnableInputView() {
513 keyboard::SetOverrideContentUrl(input_method::InputMethodManager::Get()
514 ->GetActiveIMEState()
515 ->GetCurrentInputMethod()
517 keyboard::KeyboardController
* keyboard_controller
=
518 keyboard::KeyboardController::GetInstance();
519 if (keyboard_controller
)
520 keyboard_controller
->Reload();
523 void InputMethodEngine::FocusIn(
524 const IMEEngineHandlerInterface::InputContext
& input_context
) {
527 current_input_type_
= input_context
.type
;
529 if (!IsActive() || current_input_type_
== ui::TEXT_INPUT_TYPE_NONE
)
532 context_id_
= next_context_id_
;
535 InputMethodEngineInterface::InputContext context
;
536 context
.id
= context_id_
;
537 switch (current_input_type_
) {
538 case ui::TEXT_INPUT_TYPE_SEARCH
:
539 context
.type
= "search";
541 case ui::TEXT_INPUT_TYPE_TELEPHONE
:
542 context
.type
= "tel";
544 case ui::TEXT_INPUT_TYPE_URL
:
545 context
.type
= "url";
547 case ui::TEXT_INPUT_TYPE_EMAIL
:
548 context
.type
= "email";
550 case ui::TEXT_INPUT_TYPE_NUMBER
:
551 context
.type
= "number";
553 case ui::TEXT_INPUT_TYPE_PASSWORD
:
554 context
.type
= "password";
557 context
.type
= "text";
561 context
.auto_correct
=
562 !(input_context
.flags
& ui::TEXT_INPUT_FLAG_AUTOCORRECT_OFF
);
563 context
.auto_complete
=
564 !(input_context
.flags
& ui::TEXT_INPUT_FLAG_AUTOCOMPLETE_OFF
);
565 context
.spell_check
=
566 !(input_context
.flags
& ui::TEXT_INPUT_FLAG_SPELLCHECK_OFF
);
568 observer_
->OnFocus(context
);
571 void InputMethodEngine::FocusOut() {
574 if (!IsActive() || current_input_type_
== ui::TEXT_INPUT_TYPE_NONE
)
577 current_input_type_
= ui::TEXT_INPUT_TYPE_NONE
;
579 int context_id
= context_id_
;
581 observer_
->OnBlur(context_id
);
584 void InputMethodEngine::Enable(const std::string
& component_id
) {
587 DCHECK(!component_id
.empty());
588 active_component_id_
= component_id
;
589 observer_
->OnActivate(component_id
);
590 const IMEEngineHandlerInterface::InputContext
& input_context
=
591 IMEBridge::Get()->GetCurrentInputContext();
592 current_input_type_
= input_context
.type
;
593 FocusIn(input_context
);
597 void InputMethodEngine::Disable() {
600 active_component_id_
.clear();
601 IMEBridge::Get()->GetInputContextHandler()->CommitText(
602 base::UTF16ToUTF8(composition_text_
->text()));
603 observer_
->OnDeactivated(active_component_id_
);
606 void InputMethodEngine::PropertyActivate(const std::string
& property_name
) {
609 observer_
->OnMenuItemActivated(active_component_id_
, property_name
);
612 void InputMethodEngine::Reset() {
615 observer_
->OnReset(active_component_id_
);
618 bool InputMethodEngine::IsInterestedInKeyEvent() const {
619 return observer_
->IsInterestedInKeyEvent();
622 void InputMethodEngine::ProcessKeyEvent(
623 const ui::KeyEvent
& key_event
,
624 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(
642 active_component_id_
,
644 reinterpret_cast<input_method::KeyEventHandle
*>(handler
));
647 void InputMethodEngine::CandidateClicked(uint32 index
) {
650 if (index
> candidate_ids_
.size()) {
654 // Only left button click is supported at this moment.
655 observer_
->OnCandidateClicked(
656 active_component_id_
, candidate_ids_
.at(index
), MOUSE_BUTTON_LEFT
);
659 void InputMethodEngine::SetSurroundingText(const std::string
& text
,
664 observer_
->OnSurroundingTextChanged(active_component_id_
,
666 static_cast<int>(cursor_pos
),
667 static_cast<int>(anchor_pos
));
670 bool InputMethodEngine::CheckProfile() const {
671 Profile
* active_profile
= ProfileManager::GetActiveUserProfile();
672 return active_profile
== profile_
|| profile_
->IsSameProfile(active_profile
);
675 // TODO(uekawa): rename this method to a more reasonable name.
676 void InputMethodEngine::MenuItemToProperty(
677 const MenuItem
& item
,
678 ui::ime::InputMethodMenuItem
* property
) {
679 property
->key
= item
.id
;
681 if (item
.modified
& MENU_ITEM_MODIFIED_LABEL
) {
682 property
->label
= item
.label
;
684 if (item
.modified
& MENU_ITEM_MODIFIED_VISIBLE
) {
685 // TODO(nona): Implement it.
687 if (item
.modified
& MENU_ITEM_MODIFIED_CHECKED
) {
688 property
->is_selection_item_checked
= item
.checked
;
690 if (item
.modified
& MENU_ITEM_MODIFIED_ENABLED
) {
691 // TODO(nona): implement sensitive entry(crbug.com/140192).
693 if (item
.modified
& MENU_ITEM_MODIFIED_STYLE
) {
694 if (!item
.children
.empty()) {
695 // TODO(nona): Implement it.
697 switch (item
.style
) {
698 case MENU_ITEM_STYLE_NONE
:
701 case MENU_ITEM_STYLE_CHECK
:
702 // TODO(nona): Implement it.
704 case MENU_ITEM_STYLE_RADIO
:
705 property
->is_selection_item
= true;
707 case MENU_ITEM_STYLE_SEPARATOR
:
708 // TODO(nona): Implement it.
714 // TODO(nona): Support item.children.
717 } // namespace chromeos