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/text_input_flags.h"
30 #include "ui/chromeos/ime/input_method_menu_item.h"
31 #include "ui/chromeos/ime/input_method_menu_manager.h"
32 #include "ui/events/event.h"
33 #include "ui/events/event_processor.h"
34 #include "ui/events/event_utils.h"
35 #include "ui/events/keycodes/dom/dom_code.h"
36 #include "ui/events/keycodes/dom/keycode_converter.h"
37 #include "ui/keyboard/keyboard_controller.h"
38 #include "ui/keyboard/keyboard_util.h"
44 const char kErrorNotActive
[] = "IME is not active";
45 const char kErrorWrongContext
[] = "Context is not active";
46 const char kCandidateNotFound
[] = "Candidate not found";
48 // Notifies InputContextHandler that the composition is changed.
49 void UpdateComposition(const CompositionText
& composition_text
,
52 IMEInputContextHandlerInterface
* input_context
=
53 IMEBridge::Get()->GetInputContextHandler();
55 input_context
->UpdateCompositionText(
56 composition_text
, cursor_pos
, is_visible
);
59 // Returns the length of characters of a UTF-8 string with unknown string
60 // length. Cannot apply faster algorithm to count characters in an utf-8
61 // string without knowing the string length, so just does a full scan.
62 size_t GetUtf8StringLength(const char* s
) {
65 if ((*s
& 0xC0) != 0x80)
72 std::string
GetKeyFromEvent(const ui::KeyEvent
& event
) {
73 const std::string code
= event
.GetCodeString();
74 if (base::StartsWith(code
, "Control", base::CompareCase::SENSITIVE
))
76 if (base::StartsWith(code
, "Shift", base::CompareCase::SENSITIVE
))
78 if (base::StartsWith(code
, "Alt", base::CompareCase::SENSITIVE
))
80 if (base::StartsWith(code
, "Arrow", base::CompareCase::SENSITIVE
))
81 return code
.substr(5);
84 if (code
== "Backspace" || code
== "Tab" ||
85 code
== "Enter" || code
== "CapsLock" ||
88 // Cases for media keys.
89 switch (event
.key_code()) {
90 case ui::VKEY_BROWSER_BACK
:
93 case ui::VKEY_BROWSER_FORWARD
:
95 return "HistoryForward";
96 case ui::VKEY_BROWSER_REFRESH
:
98 return "BrowserRefresh";
99 case ui::VKEY_MEDIA_LAUNCH_APP2
:
101 return "ChromeOSFullscreen";
102 case ui::VKEY_MEDIA_LAUNCH_APP1
:
104 return "ChromeOSSwitchWindow";
105 case ui::VKEY_BRIGHTNESS_DOWN
:
107 return "BrightnessDown";
108 case ui::VKEY_BRIGHTNESS_UP
:
110 return "BrightnessUp";
111 case ui::VKEY_VOLUME_MUTE
:
113 return "AudioVolumeMute";
114 case ui::VKEY_VOLUME_DOWN
:
116 return "AudioVolumeDown";
117 case ui::VKEY_VOLUME_UP
:
119 return "AudioVolumeUp";
124 // Ctrl+? cases, gets key value for Ctrl is not down.
125 if (event
.flags() & ui::EF_CONTROL_DOWN
) {
126 ui::KeyEvent
event_no_ctrl(event
.type(),
128 event
.flags() ^ ui::EF_CONTROL_DOWN
);
129 ch
= event_no_ctrl
.GetCharacter();
131 ch
= event
.GetCharacter();
133 return base::UTF16ToUTF8(base::string16(1, ch
));
136 void GetExtensionKeyboardEventFromKeyEvent(
137 const ui::KeyEvent
& event
,
138 InputMethodEngine::KeyboardEvent
* ext_event
) {
139 DCHECK(event
.type() == ui::ET_KEY_RELEASED
||
140 event
.type() == ui::ET_KEY_PRESSED
);
142 ext_event
->type
= (event
.type() == ui::ET_KEY_RELEASED
) ? "keyup" : "keydown";
144 if (event
.code() == ui::DomCode::NONE
)
145 ext_event
->code
= ui::KeyboardCodeToDomKeycode(event
.key_code());
147 ext_event
->code
= event
.GetCodeString();
148 ext_event
->key_code
= static_cast<int>(event
.key_code());
149 ext_event
->alt_key
= event
.IsAltDown();
150 ext_event
->ctrl_key
= event
.IsControlDown();
151 ext_event
->shift_key
= event
.IsShiftDown();
152 ext_event
->caps_lock
= event
.IsCapsLockDown();
153 ext_event
->key
= GetKeyFromEvent(event
);
158 InputMethodEngine::InputMethodEngine()
159 : current_input_type_(ui::TEXT_INPUT_TYPE_NONE
),
162 composition_text_(new CompositionText()),
163 composition_cursor_(0),
164 candidate_window_(new ui::CandidateWindow()),
165 window_visible_(false),
166 sent_key_event_(NULL
),
170 InputMethodEngine::~InputMethodEngine() {
173 void InputMethodEngine::Initialize(
174 scoped_ptr
<InputMethodEngineInterface::Observer
> observer
,
175 const char* extension_id
,
177 DCHECK(observer
) << "Observer must not be null.";
179 // TODO(komatsu): It is probably better to set observer out of Initialize.
180 observer_
= observer
.Pass();
181 extension_id_
= extension_id
;
185 const std::string
& InputMethodEngine::GetActiveComponentId() const {
186 return active_component_id_
;
189 bool InputMethodEngine::SetComposition(
195 const std::vector
<SegmentInfo
>& segments
,
196 std::string
* error
) {
198 *error
= kErrorNotActive
;
201 if (context_id
!= context_id_
|| context_id_
== -1) {
202 *error
= kErrorWrongContext
;
206 composition_cursor_
= cursor
;
207 composition_text_
.reset(new CompositionText());
208 composition_text_
->set_text(base::UTF8ToUTF16(text
));
210 composition_text_
->set_selection_start(selection_start
);
211 composition_text_
->set_selection_end(selection_end
);
213 // TODO: Add support for displaying selected text in the composition string.
214 for (std::vector
<SegmentInfo
>::const_iterator segment
= segments
.begin();
215 segment
!= segments
.end(); ++segment
) {
216 CompositionText::UnderlineAttribute underline
;
218 switch (segment
->style
) {
219 case SEGMENT_STYLE_UNDERLINE
:
220 underline
.type
= CompositionText::COMPOSITION_TEXT_UNDERLINE_SINGLE
;
222 case SEGMENT_STYLE_DOUBLE_UNDERLINE
:
223 underline
.type
= CompositionText::COMPOSITION_TEXT_UNDERLINE_DOUBLE
;
225 case SEGMENT_STYLE_NO_UNDERLINE
:
226 underline
.type
= CompositionText::COMPOSITION_TEXT_UNDERLINE_NONE
;
232 underline
.start_index
= segment
->start
;
233 underline
.end_index
= segment
->end
;
234 composition_text_
->mutable_underline_attributes()->push_back(underline
);
237 // TODO(nona): Makes focus out mode configuable, if necessary.
238 UpdateComposition(*composition_text_
, composition_cursor_
, true);
242 bool InputMethodEngine::ClearComposition(int context_id
,
243 std::string
* error
) {
245 *error
= kErrorNotActive
;
248 if (context_id
!= context_id_
|| context_id_
== -1) {
249 *error
= kErrorWrongContext
;
253 composition_cursor_
= 0;
254 composition_text_
.reset(new CompositionText());
255 UpdateComposition(*composition_text_
, composition_cursor_
, false);
259 bool InputMethodEngine::CommitText(int context_id
, const char* text
,
260 std::string
* error
) {
262 // TODO: Commit the text anyways.
263 *error
= kErrorNotActive
;
266 if (context_id
!= context_id_
|| context_id_
== -1) {
267 *error
= kErrorWrongContext
;
271 IMEBridge::Get()->GetInputContextHandler()->CommitText(text
);
273 // Records histograms for committed characters.
274 if (!composition_text_
->text().empty()) {
275 size_t len
= GetUtf8StringLength(text
);
276 UMA_HISTOGRAM_CUSTOM_COUNTS("InputMethod.CommitLength",
278 composition_text_
.reset(new CompositionText());
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::EventProcessor
* dispatcher
=
296 ash::Shell::GetPrimaryRootWindow()->GetHost()->event_processor();
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 ui::EventDispatchDetails details
= dispatcher
->OnEventFromSource(&ui_event
);
328 if (details
.dispatcher_destroyed
)
335 const InputMethodEngine::CandidateWindowProperty
&
336 InputMethodEngine::GetCandidateWindowProperty() const {
337 return candidate_window_property_
;
340 void InputMethodEngine::SetCandidateWindowProperty(
341 const CandidateWindowProperty
& property
) {
342 // Type conversion from InputMethodEngineInterface::CandidateWindowProperty to
343 // CandidateWindow::CandidateWindowProperty defined in chromeos/ime/.
344 ui::CandidateWindow::CandidateWindowProperty dest_property
;
345 dest_property
.page_size
= property
.page_size
;
346 dest_property
.is_cursor_visible
= property
.is_cursor_visible
;
347 dest_property
.is_vertical
= property
.is_vertical
;
348 dest_property
.show_window_at_composition
=
349 property
.show_window_at_composition
;
350 dest_property
.cursor_position
=
351 candidate_window_
->GetProperty().cursor_position
;
352 dest_property
.auxiliary_text
= property
.auxiliary_text
;
353 dest_property
.is_auxiliary_text_visible
= property
.is_auxiliary_text_visible
;
355 candidate_window_
->SetProperty(dest_property
);
356 candidate_window_property_
= property
;
359 IMECandidateWindowHandlerInterface
* cw_handler
=
360 IMEBridge::Get()->GetCandidateWindowHandler();
362 cw_handler
->UpdateLookupTable(*candidate_window_
, window_visible_
);
366 bool InputMethodEngine::SetCandidateWindowVisible(bool visible
,
367 std::string
* error
) {
369 *error
= kErrorNotActive
;
373 window_visible_
= visible
;
374 IMECandidateWindowHandlerInterface
* cw_handler
=
375 IMEBridge::Get()->GetCandidateWindowHandler();
377 cw_handler
->UpdateLookupTable(*candidate_window_
, window_visible_
);
381 bool InputMethodEngine::SetCandidates(
383 const std::vector
<Candidate
>& candidates
,
384 std::string
* error
) {
386 *error
= kErrorNotActive
;
389 if (context_id
!= context_id_
|| context_id_
== -1) {
390 *error
= kErrorWrongContext
;
394 // TODO: Nested candidates
395 candidate_ids_
.clear();
396 candidate_indexes_
.clear();
397 candidate_window_
->mutable_candidates()->clear();
398 for (std::vector
<Candidate
>::const_iterator ix
= candidates
.begin();
399 ix
!= candidates
.end(); ++ix
) {
400 ui::CandidateWindow::Entry entry
;
401 entry
.value
= base::UTF8ToUTF16(ix
->value
);
402 entry
.label
= base::UTF8ToUTF16(ix
->label
);
403 entry
.annotation
= base::UTF8ToUTF16(ix
->annotation
);
404 entry
.description_title
= base::UTF8ToUTF16(ix
->usage
.title
);
405 entry
.description_body
= base::UTF8ToUTF16(ix
->usage
.body
);
407 // Store a mapping from the user defined ID to the candidate index.
408 candidate_indexes_
[ix
->id
] = candidate_ids_
.size();
409 candidate_ids_
.push_back(ix
->id
);
411 candidate_window_
->mutable_candidates()->push_back(entry
);
414 IMECandidateWindowHandlerInterface
* cw_handler
=
415 IMEBridge::Get()->GetCandidateWindowHandler();
417 cw_handler
->UpdateLookupTable(*candidate_window_
, window_visible_
);
422 bool InputMethodEngine::SetCursorPosition(int context_id
, int candidate_id
,
423 std::string
* error
) {
425 *error
= kErrorNotActive
;
428 if (context_id
!= context_id_
|| context_id_
== -1) {
429 *error
= kErrorWrongContext
;
433 std::map
<int, int>::const_iterator position
=
434 candidate_indexes_
.find(candidate_id
);
435 if (position
== candidate_indexes_
.end()) {
436 *error
= kCandidateNotFound
;
440 candidate_window_
->set_cursor_position(position
->second
);
441 IMECandidateWindowHandlerInterface
* cw_handler
=
442 IMEBridge::Get()->GetCandidateWindowHandler();
444 cw_handler
->UpdateLookupTable(*candidate_window_
, window_visible_
);
448 bool InputMethodEngine::SetMenuItems(const std::vector
<MenuItem
>& items
) {
449 return UpdateMenuItems(items
);
452 bool InputMethodEngine::UpdateMenuItems(
453 const std::vector
<MenuItem
>& items
) {
457 ui::ime::InputMethodMenuItemList menu_item_list
;
458 for (std::vector
<MenuItem
>::const_iterator item
= items
.begin();
459 item
!= items
.end(); ++item
) {
460 ui::ime::InputMethodMenuItem property
;
461 MenuItemToProperty(*item
, &property
);
462 menu_item_list
.push_back(property
);
465 ui::ime::InputMethodMenuManager::GetInstance()->
466 SetCurrentInputMethodMenuItemList(
471 bool InputMethodEngine::IsActive() const {
472 return !active_component_id_
.empty();
475 bool InputMethodEngine::DeleteSurroundingText(int context_id
,
477 size_t number_of_chars
,
478 std::string
* error
) {
480 *error
= kErrorNotActive
;
483 if (context_id
!= context_id_
|| context_id_
== -1) {
484 *error
= kErrorWrongContext
;
488 // TODO(nona): Return false if there is ongoing composition.
490 IMEInputContextHandlerInterface
* input_context
=
491 IMEBridge::Get()->GetInputContextHandler();
493 input_context
->DeleteSurroundingText(offset
, number_of_chars
);
498 void InputMethodEngine::HideInputView() {
499 keyboard::KeyboardController
* keyboard_controller
=
500 keyboard::KeyboardController::GetInstance();
501 if (keyboard_controller
) {
502 keyboard_controller
->HideKeyboard(
503 keyboard::KeyboardController::HIDE_REASON_MANUAL
);
507 void InputMethodEngine::SetCompositionBounds(
508 const std::vector
<gfx::Rect
>& bounds
) {
511 observer_
->OnCompositionBoundsChanged(bounds
);
514 void InputMethodEngine::EnableInputView() {
515 keyboard::SetOverrideContentUrl(input_method::InputMethodManager::Get()
516 ->GetActiveIMEState()
517 ->GetCurrentInputMethod()
519 keyboard::KeyboardController
* keyboard_controller
=
520 keyboard::KeyboardController::GetInstance();
521 if (keyboard_controller
)
522 keyboard_controller
->Reload();
525 void InputMethodEngine::FocusIn(
526 const IMEEngineHandlerInterface::InputContext
& input_context
) {
529 current_input_type_
= input_context
.type
;
531 if (!IsActive() || current_input_type_
== ui::TEXT_INPUT_TYPE_NONE
)
534 context_id_
= next_context_id_
;
537 InputMethodEngineInterface::InputContext context
;
538 context
.id
= context_id_
;
539 switch (current_input_type_
) {
540 case ui::TEXT_INPUT_TYPE_SEARCH
:
541 context
.type
= "search";
543 case ui::TEXT_INPUT_TYPE_TELEPHONE
:
544 context
.type
= "tel";
546 case ui::TEXT_INPUT_TYPE_URL
:
547 context
.type
= "url";
549 case ui::TEXT_INPUT_TYPE_EMAIL
:
550 context
.type
= "email";
552 case ui::TEXT_INPUT_TYPE_NUMBER
:
553 context
.type
= "number";
555 case ui::TEXT_INPUT_TYPE_PASSWORD
:
556 context
.type
= "password";
559 context
.type
= "text";
563 context
.auto_correct
=
564 !(input_context
.flags
& ui::TEXT_INPUT_FLAG_AUTOCORRECT_OFF
);
565 context
.auto_complete
=
566 !(input_context
.flags
& ui::TEXT_INPUT_FLAG_AUTOCOMPLETE_OFF
);
567 context
.spell_check
=
568 !(input_context
.flags
& ui::TEXT_INPUT_FLAG_SPELLCHECK_OFF
);
570 observer_
->OnFocus(context
);
573 void InputMethodEngine::FocusOut() {
576 if (!IsActive() || current_input_type_
== ui::TEXT_INPUT_TYPE_NONE
)
579 current_input_type_
= ui::TEXT_INPUT_TYPE_NONE
;
581 int context_id
= context_id_
;
583 observer_
->OnBlur(context_id
);
586 void InputMethodEngine::Enable(const std::string
& component_id
) {
589 DCHECK(!component_id
.empty());
590 active_component_id_
= component_id
;
591 observer_
->OnActivate(component_id
);
592 const IMEEngineHandlerInterface::InputContext
& input_context
=
593 IMEBridge::Get()->GetCurrentInputContext();
594 current_input_type_
= input_context
.type
;
595 FocusIn(input_context
);
599 void InputMethodEngine::Disable() {
602 active_component_id_
.clear();
603 IMEBridge::Get()->GetInputContextHandler()->CommitText(
604 base::UTF16ToUTF8(composition_text_
->text()));
605 observer_
->OnDeactivated(active_component_id_
);
608 void InputMethodEngine::PropertyActivate(const std::string
& property_name
) {
611 observer_
->OnMenuItemActivated(active_component_id_
, property_name
);
614 void InputMethodEngine::Reset() {
617 composition_text_
.reset(new CompositionText());
618 observer_
->OnReset(active_component_id_
);
621 bool InputMethodEngine::IsInterestedInKeyEvent() const {
622 return observer_
->IsInterestedInKeyEvent();
625 void InputMethodEngine::ProcessKeyEvent(
626 const ui::KeyEvent
& key_event
,
627 const KeyEventDoneCallback
& callback
) {
631 KeyEventDoneCallback
* handler
= new KeyEventDoneCallback();
634 KeyboardEvent ext_event
;
635 GetExtensionKeyboardEventFromKeyEvent(key_event
, &ext_event
);
637 // If the given key event is equal to the key event sent by
638 // SendKeyEvents, this engine ID is propagated to the extension IME.
639 // Note, this check relies on that ui::KeyEvent is propagated as
640 // reference without copying.
641 if (&key_event
== sent_key_event_
)
642 ext_event
.extension_id
= extension_id_
;
644 observer_
->OnKeyEvent(
645 active_component_id_
,
647 reinterpret_cast<input_method::KeyEventHandle
*>(handler
));
650 void InputMethodEngine::CandidateClicked(uint32 index
) {
653 if (index
> candidate_ids_
.size()) {
657 // Only left button click is supported at this moment.
658 observer_
->OnCandidateClicked(
659 active_component_id_
, candidate_ids_
.at(index
), MOUSE_BUTTON_LEFT
);
662 void InputMethodEngine::SetSurroundingText(const std::string
& text
,
668 observer_
->OnSurroundingTextChanged(
669 active_component_id_
, text
, static_cast<int>(cursor_pos
),
670 static_cast<int>(anchor_pos
), static_cast<int>(offset_pos
));
673 bool InputMethodEngine::CheckProfile() const {
674 Profile
* active_profile
= ProfileManager::GetActiveUserProfile();
675 return active_profile
== profile_
|| profile_
->IsSameProfile(active_profile
);
678 // TODO(uekawa): rename this method to a more reasonable name.
679 void InputMethodEngine::MenuItemToProperty(
680 const MenuItem
& item
,
681 ui::ime::InputMethodMenuItem
* property
) {
682 property
->key
= item
.id
;
684 if (item
.modified
& MENU_ITEM_MODIFIED_LABEL
) {
685 property
->label
= item
.label
;
687 if (item
.modified
& MENU_ITEM_MODIFIED_VISIBLE
) {
688 // TODO(nona): Implement it.
690 if (item
.modified
& MENU_ITEM_MODIFIED_CHECKED
) {
691 property
->is_selection_item_checked
= item
.checked
;
693 if (item
.modified
& MENU_ITEM_MODIFIED_ENABLED
) {
694 // TODO(nona): implement sensitive entry(crbug.com/140192).
696 if (item
.modified
& MENU_ITEM_MODIFIED_STYLE
) {
697 if (!item
.children
.empty()) {
698 // TODO(nona): Implement it.
700 switch (item
.style
) {
701 case MENU_ITEM_STYLE_NONE
:
704 case MENU_ITEM_STYLE_CHECK
:
705 // TODO(nona): Implement it.
707 case MENU_ITEM_STYLE_RADIO
:
708 property
->is_selection_item
= true;
710 case MENU_ITEM_STYLE_SEPARATOR
:
711 // TODO(nona): Implement it.
717 // TODO(nona): Support item.children.
720 } // namespace chromeos