1 // Copyright 2014 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 "ui/base/ime/input_method_chromeos.h"
12 #include "base/basictypes.h"
13 #include "base/bind.h"
14 #include "base/i18n/char_iterator.h"
15 #include "base/logging.h"
16 #include "base/strings/string_util.h"
17 #include "base/strings/utf_string_conversions.h"
18 #include "base/sys_info.h"
19 #include "base/third_party/icu/icu_utf.h"
20 #include "chromeos/ime/composition_text.h"
21 #include "chromeos/ime/ime_keyboard.h"
22 #include "chromeos/ime/input_method_manager.h"
23 #include "ui/base/ime/text_input_client.h"
24 #include "ui/events/event.h"
25 #include "ui/gfx/rect.h"
28 chromeos::IMEEngineHandlerInterface
* GetEngine() {
29 return chromeos::IMEBridge::Get()->GetCurrentEngineHandler();
35 // InputMethodChromeOS implementation -----------------------------------------
36 InputMethodChromeOS::InputMethodChromeOS(
37 internal::InputMethodDelegate
* delegate
)
38 : composing_text_(false),
39 composition_changed_(false),
40 current_keyevent_id_(0),
41 weak_ptr_factory_(this) {
42 SetDelegate(delegate
);
43 chromeos::IMEBridge::Get()->SetInputContextHandler(this);
45 UpdateContextFocusState();
48 InputMethodChromeOS::~InputMethodChromeOS() {
49 AbandonAllPendingKeyEvents();
50 ConfirmCompositionText();
51 // We are dead, so we need to ask the client to stop relying on us.
52 OnInputMethodChanged();
54 chromeos::IMEBridge::Get()->SetInputContextHandler(NULL
);
57 void InputMethodChromeOS::OnFocus() {
58 InputMethodBase::OnFocus();
59 OnTextInputTypeChanged(GetTextInputClient());
62 void InputMethodChromeOS::OnBlur() {
63 ConfirmCompositionText();
64 InputMethodBase::OnBlur();
65 OnTextInputTypeChanged(GetTextInputClient());
68 bool InputMethodChromeOS::OnUntranslatedIMEMessage(
69 const base::NativeEvent
& event
,
70 NativeEventResult
* result
) {
74 void InputMethodChromeOS::ProcessKeyEventDone(uint32 id
,
77 if (pending_key_events_
.find(id
) == pending_key_events_
.end())
78 return; // Abandoned key event.
81 if (event
->type() == ET_KEY_PRESSED
) {
83 // IME event has a priority to be handled, so that character composer
85 character_composer_
.Reset();
87 // If IME does not handle key event, passes keyevent to character composer
88 // to be able to compose complex characters.
89 is_handled
= ExecuteCharacterComposer(*event
);
93 if (event
->type() == ET_KEY_PRESSED
|| event
->type() == ET_KEY_RELEASED
)
94 ProcessKeyEventPostIME(*event
, is_handled
);
96 // ProcessKeyEventPostIME may change the |pending_key_events_|.
97 pending_key_events_
.erase(id
);
100 bool InputMethodChromeOS::DispatchKeyEvent(const ui::KeyEvent
& event
) {
101 DCHECK(event
.type() == ET_KEY_PRESSED
|| event
.type() == ET_KEY_RELEASED
);
102 DCHECK(system_toplevel_window_focused());
104 // For linux_chromeos, the ime keyboard cannot track the caps lock state by
105 // itself, so need to call SetCapsLockEnabled() method to reflect the caps
106 // lock state by the key event.
107 if (!base::SysInfo::IsRunningOnChromeOS()) {
108 chromeos::input_method::InputMethodManager
* manager
=
109 chromeos::input_method::InputMethodManager::Get();
111 chromeos::input_method::ImeKeyboard
* keyboard
= manager
->GetImeKeyboard();
112 if (keyboard
&& event
.type() == ui::ET_KEY_PRESSED
) {
113 bool caps
= (event
.key_code() == ui::VKEY_CAPITAL
)
114 ? !keyboard
->CapsLockIsEnabled()
115 : (event
.flags() & EF_CAPS_LOCK_DOWN
);
116 keyboard
->SetCapsLockEnabled(caps
);
121 // If |context_| is not usable, then we can only dispatch the key event as is.
122 // We only dispatch the key event to input method when the |context_| is an
123 // normal input field (not a password field).
124 // Note: We need to send the key event to ibus even if the |context_| is not
125 // enabled, so that ibus can have a chance to enable the |context_|.
126 if (!IsInputFieldFocused() || !GetEngine()) {
127 if (event
.type() == ET_KEY_PRESSED
) {
128 if (ExecuteCharacterComposer(event
)) {
129 // Treating as PostIME event if character composer handles key event and
130 // generates some IME event,
131 ProcessKeyEventPostIME(event
, true);
134 ProcessUnfilteredKeyPressEvent(event
);
136 DispatchKeyEventPostIME(event
);
141 pending_key_events_
.insert(current_keyevent_id_
);
143 ui::KeyEvent
* copied_event
= new ui::KeyEvent(event
);
144 GetEngine()->ProcessKeyEvent(
146 base::Bind(&InputMethodChromeOS::ProcessKeyEventDone
,
147 weak_ptr_factory_
.GetWeakPtr(),
148 current_keyevent_id_
,
149 // Pass the ownership of |copied_event|.
150 base::Owned(copied_event
)));
152 ++current_keyevent_id_
;
156 void InputMethodChromeOS::OnTextInputTypeChanged(
157 const TextInputClient
* client
) {
158 if (!IsTextInputClientFocused(client
))
161 UpdateContextFocusState();
163 chromeos::IMEEngineHandlerInterface
* engine
= GetEngine();
165 // When focused input client is not changed, a text input type change should
166 // cause blur/focus events to engine.
167 // The focus in to or out from password field should also notify engine.
169 chromeos::IMEEngineHandlerInterface::InputContext
context(
170 GetTextInputType(), GetTextInputMode());
171 engine
->FocusIn(context
);
174 InputMethodBase::OnTextInputTypeChanged(client
);
177 void InputMethodChromeOS::OnCaretBoundsChanged(const TextInputClient
* client
) {
178 if (!IsInputFieldFocused() || !IsTextInputClientFocused(client
))
181 // The current text input type should not be NONE if |context_| is focused.
182 DCHECK(!IsTextInputTypeNone());
183 const gfx::Rect rect
= GetTextInputClient()->GetCaretBounds();
185 gfx::Rect composition_head
;
186 if (!GetTextInputClient()->GetCompositionCharacterBounds(0,
187 &composition_head
)) {
188 composition_head
= rect
;
191 chromeos::IMECandidateWindowHandlerInterface
* candidate_window
=
192 chromeos::IMEBridge::Get()->GetCandidateWindowHandler();
193 if (!candidate_window
)
195 candidate_window
->SetCursorBounds(rect
, composition_head
);
197 gfx::Range text_range
;
198 gfx::Range selection_range
;
199 base::string16 surrounding_text
;
200 if (!GetTextInputClient()->GetTextRange(&text_range
) ||
201 !GetTextInputClient()->GetTextFromRange(text_range
, &surrounding_text
) ||
202 !GetTextInputClient()->GetSelectionRange(&selection_range
)) {
203 previous_surrounding_text_
.clear();
204 previous_selection_range_
= gfx::Range::InvalidRange();
208 if (previous_selection_range_
== selection_range
&&
209 previous_surrounding_text_
== surrounding_text
)
212 previous_selection_range_
= selection_range
;
213 previous_surrounding_text_
= surrounding_text
;
215 if (!selection_range
.IsValid()) {
216 // TODO(nona): Ideally selection_range should not be invalid.
217 // TODO(nona): If javascript changes the focus on page loading, even (0,0)
218 // can not be obtained. Need investigation.
222 // Here SetSurroundingText accepts relative position of |surrounding_text|, so
223 // we have to convert |selection_range| from node coordinates to
224 // |surrounding_text| coordinates.
227 GetEngine()->SetSurroundingText(base::UTF16ToUTF8(surrounding_text
),
228 selection_range
.start() - text_range
.start(),
229 selection_range
.end() - text_range
.start());
232 void InputMethodChromeOS::CancelComposition(const TextInputClient
* client
) {
233 if (IsInputFieldFocused() && IsTextInputClientFocused(client
))
237 void InputMethodChromeOS::OnInputLocaleChanged() {
241 std::string
InputMethodChromeOS::GetInputLocale() {
246 bool InputMethodChromeOS::IsActive() {
250 bool InputMethodChromeOS::IsCandidatePopupOpen() const {
251 // TODO(yukishiino): Implement this method.
255 void InputMethodChromeOS::OnWillChangeFocusedClient(
256 TextInputClient
* focused_before
,
257 TextInputClient
* focused
) {
258 ConfirmCompositionText();
261 GetEngine()->FocusOut();
264 void InputMethodChromeOS::OnDidChangeFocusedClient(
265 TextInputClient
* focused_before
,
266 TextInputClient
* focused
) {
267 // Force to update the input type since client's TextInputStateChanged()
268 // function might not be called if text input types before the client loses
269 // focus and after it acquires focus again are the same.
270 UpdateContextFocusState();
273 chromeos::IMEEngineHandlerInterface::InputContext
context(
274 GetTextInputType(), GetTextInputMode());
275 GetEngine()->FocusIn(context
);
279 void InputMethodChromeOS::ConfirmCompositionText() {
280 TextInputClient
* client
= GetTextInputClient();
281 if (client
&& client
->HasCompositionText())
282 client
->ConfirmCompositionText();
287 void InputMethodChromeOS::ResetContext() {
288 if (!IsInputFieldFocused() || !GetTextInputClient())
291 DCHECK(system_toplevel_window_focused());
293 composition_
.Clear();
294 result_text_
.clear();
295 composing_text_
= false;
296 composition_changed_
= false;
298 // We need to abandon all pending key events, but as above comment says, there
299 // is no reliable way to abandon all results generated by these abandoned key
301 AbandonAllPendingKeyEvents();
303 // This function runs asynchronously.
304 // Note: some input method engines may not support reset method, such as
305 // ibus-anthy. But as we control all input method engines by ourselves, we can
306 // make sure that all of the engines we are using support it correctly.
308 GetEngine()->Reset();
310 character_composer_
.Reset();
313 void InputMethodChromeOS::UpdateContextFocusState() {
315 OnInputMethodChanged();
317 // Propagate the focus event to the candidate window handler which also
318 // manages the input method mode indicator.
319 chromeos::IMECandidateWindowHandlerInterface
* candidate_window
=
320 chromeos::IMEBridge::Get()->GetCandidateWindowHandler();
321 if (candidate_window
)
322 candidate_window
->FocusStateChanged(IsInputFieldFocused());
324 chromeos::IMEBridge::Get()->SetCurrentTextInputType(GetTextInputType());
326 if (!IsTextInputTypeNone())
327 OnCaretBoundsChanged(GetTextInputClient());
330 void InputMethodChromeOS::ProcessKeyEventPostIME(
331 const ui::KeyEvent
& event
,
333 TextInputClient
* client
= GetTextInputClient();
335 // As ibus works asynchronously, there is a chance that the focused client
336 // loses focus before this method gets called.
337 DispatchKeyEventPostIME(event
);
341 if (event
.type() == ET_KEY_PRESSED
&& handled
)
342 ProcessFilteredKeyPressEvent(event
);
344 // In case the focus was changed by the key event. The |context_| should have
345 // been reset when the focused window changed.
346 if (client
!= GetTextInputClient())
349 if (HasInputMethodResult())
350 ProcessInputMethodResult(event
, handled
);
352 // In case the focus was changed when sending input method results to the
354 if (client
!= GetTextInputClient())
357 if (event
.type() == ET_KEY_PRESSED
&& !handled
)
358 ProcessUnfilteredKeyPressEvent(event
);
359 else if (event
.type() == ET_KEY_RELEASED
)
360 DispatchKeyEventPostIME(event
);
363 void InputMethodChromeOS::ProcessFilteredKeyPressEvent(
364 const ui::KeyEvent
& event
) {
365 if (NeedInsertChar()) {
366 DispatchKeyEventPostIME(event
);
368 const ui::KeyEvent
fabricated_event(ET_KEY_PRESSED
,
371 DispatchKeyEventPostIME(fabricated_event
);
375 void InputMethodChromeOS::ProcessUnfilteredKeyPressEvent(
376 const ui::KeyEvent
& event
) {
377 const TextInputClient
* prev_client
= GetTextInputClient();
378 DispatchKeyEventPostIME(event
);
380 // We shouldn't dispatch the character anymore if the key event dispatch
381 // caused focus change. For example, in the following scenario,
382 // 1. visit a web page which has a <textarea>.
384 // 3. enable Korean IME, press A, then press Tab to move the focus to the web
386 // We should return here not to send the Tab key event to RWHV.
387 TextInputClient
* client
= GetTextInputClient();
388 if (!client
|| client
!= prev_client
)
391 // If a key event was not filtered by |context_| and |character_composer_|,
392 // then it means the key event didn't generate any result text. So we need
393 // to send corresponding character to the focused text input client.
394 uint16 ch
= event
.GetCharacter();
396 client
->InsertChar(ch
, event
.flags());
399 void InputMethodChromeOS::ProcessInputMethodResult(const ui::KeyEvent
& event
,
401 TextInputClient
* client
= GetTextInputClient();
404 if (result_text_
.length()) {
405 if (handled
&& NeedInsertChar()) {
406 for (base::string16::const_iterator i
= result_text_
.begin();
407 i
!= result_text_
.end(); ++i
) {
408 client
->InsertChar(*i
, event
.flags());
411 client
->InsertText(result_text_
);
412 composing_text_
= false;
416 if (composition_changed_
&& !IsTextInputTypeNone()) {
417 if (composition_
.text
.length()) {
418 composing_text_
= true;
419 client
->SetCompositionText(composition_
);
420 } else if (result_text_
.empty()) {
421 client
->ClearCompositionText();
425 // We should not clear composition text here, as it may belong to the next
426 // composition session.
427 result_text_
.clear();
428 composition_changed_
= false;
431 bool InputMethodChromeOS::NeedInsertChar() const {
432 return GetTextInputClient() &&
433 (IsTextInputTypeNone() ||
434 (!composing_text_
&& result_text_
.length() == 1));
437 bool InputMethodChromeOS::HasInputMethodResult() const {
438 return result_text_
.length() || composition_changed_
;
441 void InputMethodChromeOS::SendFakeProcessKeyEvent(bool pressed
) const {
442 if (!GetTextInputClient())
444 KeyEvent
evt(pressed
? ET_KEY_PRESSED
: ET_KEY_RELEASED
,
445 pressed
? VKEY_PROCESSKEY
: VKEY_UNKNOWN
,
446 EF_IME_FABRICATED_KEY
);
447 DispatchKeyEventPostIME(evt
);
450 void InputMethodChromeOS::AbandonAllPendingKeyEvents() {
451 pending_key_events_
.clear();
454 void InputMethodChromeOS::CommitText(const std::string
& text
) {
458 // We need to receive input method result even if the text input type is
459 // TEXT_INPUT_TYPE_NONE, to make sure we can always send correct
460 // character for each key event to the focused text input client.
461 if (!GetTextInputClient())
464 const base::string16 utf16_text
= base::UTF8ToUTF16(text
);
465 if (utf16_text
.empty())
468 // Append the text to the buffer, because commit signal might be fired
469 // multiple times when processing a key event.
470 result_text_
.append(utf16_text
);
472 // If we are not handling key event, do not bother sending text result if the
473 // focused text input client does not support text input.
474 if (pending_key_events_
.empty() && !IsTextInputTypeNone()) {
475 SendFakeProcessKeyEvent(true);
476 GetTextInputClient()->InsertText(utf16_text
);
477 SendFakeProcessKeyEvent(false);
478 result_text_
.clear();
482 void InputMethodChromeOS::UpdateCompositionText(
483 const chromeos::CompositionText
& text
,
486 if (IsTextInputTypeNone())
489 if (!CanComposeInline()) {
490 chromeos::IMECandidateWindowHandlerInterface
* candidate_window
=
491 chromeos::IMEBridge::Get()->GetCandidateWindowHandler();
492 if (candidate_window
)
493 candidate_window
->UpdatePreeditText(text
.text(), cursor_pos
, visible
);
496 // |visible| argument is very confusing. For example, what's the correct
498 // 1. OnUpdatePreeditText() is called with a text and visible == false, then
499 // 2. OnShowPreeditText() is called afterwards.
501 // If it's only for clearing the current preedit text, then why not just use
502 // OnHidePreeditText()?
508 ExtractCompositionText(text
, cursor_pos
, &composition_
);
510 composition_changed_
= true;
512 // In case OnShowPreeditText() is not called.
513 if (composition_
.text
.length())
514 composing_text_
= true;
516 // If we receive a composition text without pending key event, then we need to
517 // send it to the focused text input client directly.
518 if (pending_key_events_
.empty()) {
519 SendFakeProcessKeyEvent(true);
520 GetTextInputClient()->SetCompositionText(composition_
);
521 SendFakeProcessKeyEvent(false);
522 composition_changed_
= false;
523 composition_
.Clear();
527 void InputMethodChromeOS::HidePreeditText() {
528 if (composition_
.text
.empty() || IsTextInputTypeNone())
531 // Intentionally leaves |composing_text_| unchanged.
532 composition_changed_
= true;
533 composition_
.Clear();
535 if (pending_key_events_
.empty()) {
536 TextInputClient
* client
= GetTextInputClient();
537 if (client
&& client
->HasCompositionText()) {
538 SendFakeProcessKeyEvent(true);
539 client
->ClearCompositionText();
540 SendFakeProcessKeyEvent(false);
542 composition_changed_
= false;
546 void InputMethodChromeOS::DeleteSurroundingText(int32 offset
, uint32 length
) {
547 if (!composition_
.text
.empty())
548 return; // do nothing if there is ongoing composition.
549 if (offset
< 0 && static_cast<uint32
>(-1 * offset
) != length
)
550 return; // only preceding text can be deletable.
551 if (GetTextInputClient())
552 GetTextInputClient()->ExtendSelectionAndDelete(length
, 0U);
555 bool InputMethodChromeOS::ExecuteCharacterComposer(const ui::KeyEvent
& event
) {
556 if (!character_composer_
.FilterKeyPress(event
))
559 // CharacterComposer consumed the key event. Update the composition text.
560 chromeos::CompositionText preedit
;
561 preedit
.set_text(character_composer_
.preedit_string());
562 UpdateCompositionText(preedit
, preedit
.text().size(),
563 !preedit
.text().empty());
564 std::string commit_text
=
565 base::UTF16ToUTF8(character_composer_
.composed_character());
566 if (!commit_text
.empty()) {
567 CommitText(commit_text
);
572 void InputMethodChromeOS::ExtractCompositionText(
573 const chromeos::CompositionText
& text
,
574 uint32 cursor_position
,
575 CompositionText
* out_composition
) const {
576 out_composition
->Clear();
577 out_composition
->text
= text
.text();
579 if (out_composition
->text
.empty())
582 // ibus uses character index for cursor position and attribute range, but we
583 // use char16 offset for them. So we need to do conversion here.
584 std::vector
<size_t> char16_offsets
;
585 size_t length
= out_composition
->text
.length();
586 base::i18n::UTF16CharIterator
char_iterator(&out_composition
->text
);
588 char16_offsets
.push_back(char_iterator
.array_pos());
589 } while (char_iterator
.Advance());
591 // The text length in Unicode characters.
592 uint32 char_length
= static_cast<uint32
>(char16_offsets
.size());
593 // Make sure we can convert the value of |char_length| as well.
594 char16_offsets
.push_back(length
);
596 size_t cursor_offset
=
597 char16_offsets
[std::min(char_length
, cursor_position
)];
599 out_composition
->selection
= gfx::Range(cursor_offset
);
601 const std::vector
<chromeos::CompositionText::UnderlineAttribute
>&
602 underline_attributes
= text
.underline_attributes();
603 if (!underline_attributes
.empty()) {
604 for (size_t i
= 0; i
< underline_attributes
.size(); ++i
) {
605 const uint32 start
= underline_attributes
[i
].start_index
;
606 const uint32 end
= underline_attributes
[i
].end_index
;
609 CompositionUnderline
underline(char16_offsets
[start
],
613 SK_ColorTRANSPARENT
);
614 if (underline_attributes
[i
].type
==
615 chromeos::CompositionText::COMPOSITION_TEXT_UNDERLINE_DOUBLE
)
616 underline
.thick
= true;
617 else if (underline_attributes
[i
].type
==
618 chromeos::CompositionText::COMPOSITION_TEXT_UNDERLINE_ERROR
)
619 underline
.color
= SK_ColorRED
;
620 out_composition
->underlines
.push_back(underline
);
624 DCHECK(text
.selection_start() <= text
.selection_end());
625 if (text
.selection_start() < text
.selection_end()) {
626 const uint32 start
= text
.selection_start();
627 const uint32 end
= text
.selection_end();
628 CompositionUnderline
underline(char16_offsets
[start
],
632 SK_ColorTRANSPARENT
);
633 out_composition
->underlines
.push_back(underline
);
635 // If the cursor is at start or end of this underline, then we treat
636 // it as the selection range as well, but make sure to set the cursor
637 // position to the selection end.
638 if (underline
.start_offset
== cursor_offset
) {
639 out_composition
->selection
.set_start(underline
.end_offset
);
640 out_composition
->selection
.set_end(cursor_offset
);
641 } else if (underline
.end_offset
== cursor_offset
) {
642 out_composition
->selection
.set_start(underline
.start_offset
);
643 out_composition
->selection
.set_end(cursor_offset
);
647 // Use a black thin underline by default.
648 if (out_composition
->underlines
.empty()) {
649 out_composition
->underlines
.push_back(CompositionUnderline(
650 0, length
, SK_ColorBLACK
, false /* thick */, SK_ColorTRANSPARENT
));
654 bool InputMethodChromeOS::IsInputFieldFocused() {
655 TextInputType type
= GetTextInputType();
656 return (type
!= TEXT_INPUT_TYPE_NONE
) && (type
!= TEXT_INPUT_TYPE_PASSWORD
);