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
;
312 ui::KeyEvent
ui_event(
314 ui::KeycodeConverter::CodeStringToDomCode(event
.code
.c_str()), flags
,
315 ui::KeycodeConverter::KeyStringToDomKey(event
.key
.c_str()),
316 ui::EventTimeForNow());
317 base::AutoReset
<const ui::KeyEvent
*> reset_sent_key(&sent_key_event_
,
319 ui::EventDispatchDetails details
= dispatcher
->OnEventFromSource(&ui_event
);
320 if (details
.dispatcher_destroyed
)
327 const InputMethodEngine::CandidateWindowProperty
&
328 InputMethodEngine::GetCandidateWindowProperty() const {
329 return candidate_window_property_
;
332 void InputMethodEngine::SetCandidateWindowProperty(
333 const CandidateWindowProperty
& property
) {
334 // Type conversion from InputMethodEngineInterface::CandidateWindowProperty to
335 // CandidateWindow::CandidateWindowProperty defined in chromeos/ime/.
336 ui::CandidateWindow::CandidateWindowProperty dest_property
;
337 dest_property
.page_size
= property
.page_size
;
338 dest_property
.is_cursor_visible
= property
.is_cursor_visible
;
339 dest_property
.is_vertical
= property
.is_vertical
;
340 dest_property
.show_window_at_composition
=
341 property
.show_window_at_composition
;
342 dest_property
.cursor_position
=
343 candidate_window_
->GetProperty().cursor_position
;
344 dest_property
.auxiliary_text
= property
.auxiliary_text
;
345 dest_property
.is_auxiliary_text_visible
= property
.is_auxiliary_text_visible
;
347 candidate_window_
->SetProperty(dest_property
);
348 candidate_window_property_
= property
;
351 IMECandidateWindowHandlerInterface
* cw_handler
=
352 IMEBridge::Get()->GetCandidateWindowHandler();
354 cw_handler
->UpdateLookupTable(*candidate_window_
, window_visible_
);
358 bool InputMethodEngine::SetCandidateWindowVisible(bool visible
,
359 std::string
* error
) {
361 *error
= kErrorNotActive
;
365 window_visible_
= visible
;
366 IMECandidateWindowHandlerInterface
* cw_handler
=
367 IMEBridge::Get()->GetCandidateWindowHandler();
369 cw_handler
->UpdateLookupTable(*candidate_window_
, window_visible_
);
373 bool InputMethodEngine::SetCandidates(
375 const std::vector
<Candidate
>& candidates
,
376 std::string
* error
) {
378 *error
= kErrorNotActive
;
381 if (context_id
!= context_id_
|| context_id_
== -1) {
382 *error
= kErrorWrongContext
;
386 // TODO: Nested candidates
387 candidate_ids_
.clear();
388 candidate_indexes_
.clear();
389 candidate_window_
->mutable_candidates()->clear();
390 for (std::vector
<Candidate
>::const_iterator ix
= candidates
.begin();
391 ix
!= candidates
.end(); ++ix
) {
392 ui::CandidateWindow::Entry entry
;
393 entry
.value
= base::UTF8ToUTF16(ix
->value
);
394 entry
.label
= base::UTF8ToUTF16(ix
->label
);
395 entry
.annotation
= base::UTF8ToUTF16(ix
->annotation
);
396 entry
.description_title
= base::UTF8ToUTF16(ix
->usage
.title
);
397 entry
.description_body
= base::UTF8ToUTF16(ix
->usage
.body
);
399 // Store a mapping from the user defined ID to the candidate index.
400 candidate_indexes_
[ix
->id
] = candidate_ids_
.size();
401 candidate_ids_
.push_back(ix
->id
);
403 candidate_window_
->mutable_candidates()->push_back(entry
);
406 IMECandidateWindowHandlerInterface
* cw_handler
=
407 IMEBridge::Get()->GetCandidateWindowHandler();
409 cw_handler
->UpdateLookupTable(*candidate_window_
, window_visible_
);
414 bool InputMethodEngine::SetCursorPosition(int context_id
, int candidate_id
,
415 std::string
* error
) {
417 *error
= kErrorNotActive
;
420 if (context_id
!= context_id_
|| context_id_
== -1) {
421 *error
= kErrorWrongContext
;
425 std::map
<int, int>::const_iterator position
=
426 candidate_indexes_
.find(candidate_id
);
427 if (position
== candidate_indexes_
.end()) {
428 *error
= kCandidateNotFound
;
432 candidate_window_
->set_cursor_position(position
->second
);
433 IMECandidateWindowHandlerInterface
* cw_handler
=
434 IMEBridge::Get()->GetCandidateWindowHandler();
436 cw_handler
->UpdateLookupTable(*candidate_window_
, window_visible_
);
440 bool InputMethodEngine::SetMenuItems(const std::vector
<MenuItem
>& items
) {
441 return UpdateMenuItems(items
);
444 bool InputMethodEngine::UpdateMenuItems(
445 const std::vector
<MenuItem
>& items
) {
449 ui::ime::InputMethodMenuItemList menu_item_list
;
450 for (std::vector
<MenuItem
>::const_iterator item
= items
.begin();
451 item
!= items
.end(); ++item
) {
452 ui::ime::InputMethodMenuItem property
;
453 MenuItemToProperty(*item
, &property
);
454 menu_item_list
.push_back(property
);
457 ui::ime::InputMethodMenuManager::GetInstance()->
458 SetCurrentInputMethodMenuItemList(
463 bool InputMethodEngine::IsActive() const {
464 return !active_component_id_
.empty();
467 bool InputMethodEngine::DeleteSurroundingText(int context_id
,
469 size_t number_of_chars
,
470 std::string
* error
) {
472 *error
= kErrorNotActive
;
475 if (context_id
!= context_id_
|| context_id_
== -1) {
476 *error
= kErrorWrongContext
;
480 // TODO(nona): Return false if there is ongoing composition.
482 IMEInputContextHandlerInterface
* input_context
=
483 IMEBridge::Get()->GetInputContextHandler();
485 input_context
->DeleteSurroundingText(offset
, number_of_chars
);
490 void InputMethodEngine::HideInputView() {
491 keyboard::KeyboardController
* keyboard_controller
=
492 keyboard::KeyboardController::GetInstance();
493 if (keyboard_controller
) {
494 keyboard_controller
->HideKeyboard(
495 keyboard::KeyboardController::HIDE_REASON_MANUAL
);
499 void InputMethodEngine::SetCompositionBounds(
500 const std::vector
<gfx::Rect
>& bounds
) {
503 observer_
->OnCompositionBoundsChanged(bounds
);
506 void InputMethodEngine::EnableInputView() {
507 keyboard::SetOverrideContentUrl(input_method::InputMethodManager::Get()
508 ->GetActiveIMEState()
509 ->GetCurrentInputMethod()
511 keyboard::KeyboardController
* keyboard_controller
=
512 keyboard::KeyboardController::GetInstance();
513 if (keyboard_controller
)
514 keyboard_controller
->Reload();
517 void InputMethodEngine::FocusIn(
518 const IMEEngineHandlerInterface::InputContext
& input_context
) {
521 current_input_type_
= input_context
.type
;
523 if (!IsActive() || current_input_type_
== ui::TEXT_INPUT_TYPE_NONE
)
526 context_id_
= next_context_id_
;
529 InputMethodEngineInterface::InputContext context
;
530 context
.id
= context_id_
;
531 switch (current_input_type_
) {
532 case ui::TEXT_INPUT_TYPE_SEARCH
:
533 context
.type
= "search";
535 case ui::TEXT_INPUT_TYPE_TELEPHONE
:
536 context
.type
= "tel";
538 case ui::TEXT_INPUT_TYPE_URL
:
539 context
.type
= "url";
541 case ui::TEXT_INPUT_TYPE_EMAIL
:
542 context
.type
= "email";
544 case ui::TEXT_INPUT_TYPE_NUMBER
:
545 context
.type
= "number";
547 case ui::TEXT_INPUT_TYPE_PASSWORD
:
548 context
.type
= "password";
551 context
.type
= "text";
555 context
.auto_correct
=
556 !(input_context
.flags
& ui::TEXT_INPUT_FLAG_AUTOCORRECT_OFF
);
557 context
.auto_complete
=
558 !(input_context
.flags
& ui::TEXT_INPUT_FLAG_AUTOCOMPLETE_OFF
);
559 context
.spell_check
=
560 !(input_context
.flags
& ui::TEXT_INPUT_FLAG_SPELLCHECK_OFF
);
562 observer_
->OnFocus(context
);
565 void InputMethodEngine::FocusOut() {
568 if (!IsActive() || 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(const std::string
& component_id
) {
581 DCHECK(!component_id
.empty());
582 active_component_id_
= component_id
;
583 observer_
->OnActivate(component_id
);
584 const IMEEngineHandlerInterface::InputContext
& input_context
=
585 IMEBridge::Get()->GetCurrentInputContext();
586 current_input_type_
= input_context
.type
;
587 FocusIn(input_context
);
591 void InputMethodEngine::Disable() {
594 active_component_id_
.clear();
595 IMEBridge::Get()->GetInputContextHandler()->CommitText(
596 base::UTF16ToUTF8(composition_text_
->text()));
597 composition_text_
.reset(new CompositionText());
598 observer_
->OnDeactivated(active_component_id_
);
601 void InputMethodEngine::PropertyActivate(const std::string
& property_name
) {
604 observer_
->OnMenuItemActivated(active_component_id_
, property_name
);
607 void InputMethodEngine::Reset() {
610 composition_text_
.reset(new CompositionText());
611 observer_
->OnReset(active_component_id_
);
614 bool InputMethodEngine::IsInterestedInKeyEvent() const {
615 return observer_
->IsInterestedInKeyEvent();
618 void InputMethodEngine::ProcessKeyEvent(
619 const ui::KeyEvent
& key_event
,
620 const KeyEventDoneCallback
& callback
) {
624 KeyEventDoneCallback
* handler
= new KeyEventDoneCallback();
627 KeyboardEvent ext_event
;
628 GetExtensionKeyboardEventFromKeyEvent(key_event
, &ext_event
);
630 // If the given key event is equal to the key event sent by
631 // SendKeyEvents, this engine ID is propagated to the extension IME.
632 // Note, this check relies on that ui::KeyEvent is propagated as
633 // reference without copying.
634 if (&key_event
== sent_key_event_
)
635 ext_event
.extension_id
= extension_id_
;
637 observer_
->OnKeyEvent(
638 active_component_id_
,
640 reinterpret_cast<input_method::KeyEventHandle
*>(handler
));
643 void InputMethodEngine::CandidateClicked(uint32 index
) {
646 if (index
> candidate_ids_
.size()) {
650 // Only left button click is supported at this moment.
651 observer_
->OnCandidateClicked(
652 active_component_id_
, candidate_ids_
.at(index
), MOUSE_BUTTON_LEFT
);
655 void InputMethodEngine::SetSurroundingText(const std::string
& text
,
661 observer_
->OnSurroundingTextChanged(
662 active_component_id_
, text
, static_cast<int>(cursor_pos
),
663 static_cast<int>(anchor_pos
), static_cast<int>(offset_pos
));
666 bool InputMethodEngine::CheckProfile() const {
667 Profile
* active_profile
= ProfileManager::GetActiveUserProfile();
668 return active_profile
== profile_
|| profile_
->IsSameProfile(active_profile
);
671 // TODO(uekawa): rename this method to a more reasonable name.
672 void InputMethodEngine::MenuItemToProperty(
673 const MenuItem
& item
,
674 ui::ime::InputMethodMenuItem
* property
) {
675 property
->key
= item
.id
;
677 if (item
.modified
& MENU_ITEM_MODIFIED_LABEL
) {
678 property
->label
= item
.label
;
680 if (item
.modified
& MENU_ITEM_MODIFIED_VISIBLE
) {
681 // TODO(nona): Implement it.
683 if (item
.modified
& MENU_ITEM_MODIFIED_CHECKED
) {
684 property
->is_selection_item_checked
= item
.checked
;
686 if (item
.modified
& MENU_ITEM_MODIFIED_ENABLED
) {
687 // TODO(nona): implement sensitive entry(crbug.com/140192).
689 if (item
.modified
& MENU_ITEM_MODIFIED_STYLE
) {
690 if (!item
.children
.empty()) {
691 // TODO(nona): Implement it.
693 switch (item
.style
) {
694 case MENU_ITEM_STYLE_NONE
:
697 case MENU_ITEM_STYLE_CHECK
:
698 // TODO(nona): Implement it.
700 case MENU_ITEM_STYLE_RADIO
:
701 property
->is_selection_item
= true;
703 case MENU_ITEM_STYLE_SEPARATOR
:
704 // TODO(nona): Implement it.
710 // TODO(nona): Support item.children.
713 } // namespace chromeos