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 "ui/base/ime/chromeos/composition_text_chromeos.h"
21 #include "ui/base/ime/chromeos/ime_keyboard.h"
22 #include "ui/base/ime/chromeos/input_method_manager.h"
23 #include "ui/base/ime/text_input_client.h"
24 #include "ui/events/event.h"
25 #include "ui/gfx/geometry/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 handling_key_event_(false),
41 weak_ptr_factory_(this) {
42 SetDelegate(delegate
);
43 chromeos::IMEBridge::Get()->SetInputContextHandler(this);
45 UpdateContextFocusState();
48 InputMethodChromeOS::~InputMethodChromeOS() {
49 ConfirmCompositionText();
50 // We are dead, so we need to ask the client to stop relying on us.
51 OnInputMethodChanged();
53 if (chromeos::IMEBridge::Get())
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(ui::KeyEvent
* event
,
77 if (event
->type() == ET_KEY_PRESSED
) {
79 // IME event has a priority to be handled, so that character composer
81 character_composer_
.Reset();
83 // If IME does not handle key event, passes keyevent to character composer
84 // to be able to compose complex characters.
85 is_handled
= ExecuteCharacterComposer(*event
);
89 if (event
->type() == ET_KEY_PRESSED
|| event
->type() == ET_KEY_RELEASED
)
90 ProcessKeyEventPostIME(event
, is_handled
);
92 handling_key_event_
= false;
95 void InputMethodChromeOS::DispatchKeyEvent(ui::KeyEvent
* event
) {
96 DCHECK(event
->IsKeyEvent());
97 DCHECK(!(event
->flags() & ui::EF_IS_SYNTHESIZED
));
98 DCHECK(system_toplevel_window_focused());
100 // For linux_chromeos, the ime keyboard cannot track the caps lock state by
101 // itself, so need to call SetCapsLockEnabled() method to reflect the caps
102 // lock state by the key event.
103 if (!base::SysInfo::IsRunningOnChromeOS()) {
104 chromeos::input_method::InputMethodManager
* manager
=
105 chromeos::input_method::InputMethodManager::Get();
107 chromeos::input_method::ImeKeyboard
* keyboard
= manager
->GetImeKeyboard();
108 if (keyboard
&& event
->type() == ui::ET_KEY_PRESSED
) {
109 bool caps
= (event
->key_code() == ui::VKEY_CAPITAL
)
110 ? !keyboard
->CapsLockIsEnabled()
111 : (event
->flags() & EF_CAPS_LOCK_DOWN
);
112 keyboard
->SetCapsLockEnabled(caps
);
117 // If |context_| is not usable, then we can only dispatch the key event as is.
118 // We only dispatch the key event to input method when the |context_| is an
119 // normal input field (not a password field).
120 // Note: We need to send the key event to ibus even if the |context_| is not
121 // enabled, so that ibus can have a chance to enable the |context_|.
122 if (!IsNonPasswordInputFieldFocused() || !GetEngine()) {
123 if (event
->type() == ET_KEY_PRESSED
) {
124 if (ExecuteCharacterComposer(*event
)) {
125 // Treating as PostIME event if character composer handles key event and
126 // generates some IME event,
127 ProcessKeyEventPostIME(event
, true);
130 ProcessUnfilteredKeyPressEvent(event
);
132 ignore_result(DispatchKeyEventPostIME(event
));
137 handling_key_event_
= true;
138 if (GetEngine()->IsInterestedInKeyEvent()) {
139 GetEngine()->ProcessKeyEvent(
141 base::Bind(&InputMethodChromeOS::ProcessKeyEventDone
,
142 weak_ptr_factory_
.GetWeakPtr(),
143 // Pass the ownership of the new copied event.
144 base::Owned(new ui::KeyEvent(*event
))));
146 ProcessKeyEventDone(event
, false);
150 void InputMethodChromeOS::OnTextInputTypeChanged(
151 const TextInputClient
* client
) {
152 if (!IsTextInputClientFocused(client
))
155 UpdateContextFocusState();
157 chromeos::IMEEngineHandlerInterface
* engine
= GetEngine();
159 // When focused input client is not changed, a text input type change should
160 // cause blur/focus events to engine.
161 // The focus in to or out from password field should also notify engine.
163 chromeos::IMEEngineHandlerInterface::InputContext
context(
164 GetTextInputType(), GetTextInputMode(), GetTextInputFlags());
165 engine
->FocusIn(context
);
168 InputMethodBase::OnTextInputTypeChanged(client
);
171 void InputMethodChromeOS::OnCaretBoundsChanged(const TextInputClient
* client
) {
172 if (!IsInputFieldFocused() || !IsTextInputClientFocused(client
))
175 NotifyTextInputCaretBoundsChanged(client
);
177 if (!IsNonPasswordInputFieldFocused())
180 // The current text input type should not be NONE if |context_| is focused.
181 DCHECK(client
== GetTextInputClient());
182 DCHECK(!IsTextInputTypeNone());
183 const gfx::Rect caret_rect
= client
->GetCaretBounds();
185 gfx::Rect composition_head
;
186 std::vector
<gfx::Rect
> rects
;
187 if (client
->HasCompositionText()) {
190 while (client
->GetCompositionCharacterBounds(i
++, &rect
))
191 rects
.push_back(rect
);
194 // Pepper don't support composition bounds, so fallback to caret bounds to
195 // avoid bad user experience (the IME window moved to upper left corner).
196 // For case of no composition at present, also use caret bounds which is
197 // required by the IME extension for certain features (e.g. physical keyboard
200 rects
.push_back(caret_rect
);
202 composition_head
= rects
[0];
204 GetEngine()->SetCompositionBounds(rects
);
206 chromeos::IMECandidateWindowHandlerInterface
* candidate_window
=
207 chromeos::IMEBridge::Get()->GetCandidateWindowHandler();
208 if (!candidate_window
)
210 candidate_window
->SetCursorBounds(caret_rect
, composition_head
);
212 gfx::Range text_range
;
213 gfx::Range selection_range
;
214 base::string16 surrounding_text
;
215 if (!client
->GetTextRange(&text_range
) ||
216 !client
->GetTextFromRange(text_range
, &surrounding_text
) ||
217 !client
->GetSelectionRange(&selection_range
)) {
218 previous_surrounding_text_
.clear();
219 previous_selection_range_
= gfx::Range::InvalidRange();
223 if (previous_selection_range_
== selection_range
&&
224 previous_surrounding_text_
== surrounding_text
)
227 previous_selection_range_
= selection_range
;
228 previous_surrounding_text_
= surrounding_text
;
230 if (!selection_range
.IsValid()) {
231 // TODO(nona): Ideally selection_range should not be invalid.
232 // TODO(nona): If javascript changes the focus on page loading, even (0,0)
233 // can not be obtained. Need investigation.
237 // Here SetSurroundingText accepts relative position of |surrounding_text|, so
238 // we have to convert |selection_range| from node coordinates to
239 // |surrounding_text| coordinates.
242 GetEngine()->SetSurroundingText(base::UTF16ToUTF8(surrounding_text
),
243 selection_range
.start() - text_range
.start(),
244 selection_range
.end() - text_range
.start(),
248 void InputMethodChromeOS::CancelComposition(const TextInputClient
* client
) {
249 if (IsNonPasswordInputFieldFocused() && IsTextInputClientFocused(client
))
253 void InputMethodChromeOS::OnInputLocaleChanged() {
257 std::string
InputMethodChromeOS::GetInputLocale() {
262 bool InputMethodChromeOS::IsCandidatePopupOpen() const {
263 // TODO(yukishiino): Implement this method.
267 void InputMethodChromeOS::OnWillChangeFocusedClient(
268 TextInputClient
* focused_before
,
269 TextInputClient
* focused
) {
270 ConfirmCompositionText();
273 GetEngine()->FocusOut();
276 void InputMethodChromeOS::OnDidChangeFocusedClient(
277 TextInputClient
* focused_before
,
278 TextInputClient
* focused
) {
279 // Force to update the input type since client's TextInputStateChanged()
280 // function might not be called if text input types before the client loses
281 // focus and after it acquires focus again are the same.
282 UpdateContextFocusState();
285 chromeos::IMEEngineHandlerInterface::InputContext
context(
286 GetTextInputType(), GetTextInputMode(), GetTextInputFlags());
287 GetEngine()->FocusIn(context
);
291 void InputMethodChromeOS::ConfirmCompositionText() {
292 TextInputClient
* client
= GetTextInputClient();
293 if (client
&& client
->HasCompositionText())
294 client
->ConfirmCompositionText();
299 void InputMethodChromeOS::ResetContext() {
300 if (!IsNonPasswordInputFieldFocused() || !GetTextInputClient())
303 DCHECK(system_toplevel_window_focused());
305 composition_
.Clear();
306 result_text_
.clear();
307 composing_text_
= false;
308 composition_changed_
= false;
310 // This function runs asynchronously.
311 // Note: some input method engines may not support reset method, such as
312 // ibus-anthy. But as we control all input method engines by ourselves, we can
313 // make sure that all of the engines we are using support it correctly.
315 GetEngine()->Reset();
317 character_composer_
.Reset();
320 void InputMethodChromeOS::UpdateContextFocusState() {
322 OnInputMethodChanged();
324 // Propagate the focus event to the candidate window handler which also
325 // manages the input method mode indicator.
326 chromeos::IMECandidateWindowHandlerInterface
* candidate_window
=
327 chromeos::IMEBridge::Get()->GetCandidateWindowHandler();
328 if (candidate_window
)
329 candidate_window
->FocusStateChanged(IsNonPasswordInputFieldFocused());
331 chromeos::IMEEngineHandlerInterface::InputContext
context(
332 GetTextInputType(), GetTextInputMode(), GetTextInputFlags());
333 chromeos::IMEBridge::Get()->SetCurrentInputContext(context
);
335 if (!IsTextInputTypeNone())
336 OnCaretBoundsChanged(GetTextInputClient());
339 void InputMethodChromeOS::ProcessKeyEventPostIME(ui::KeyEvent
* event
,
341 TextInputClient
* client
= GetTextInputClient();
343 // As ibus works asynchronously, there is a chance that the focused client
344 // loses focus before this method gets called.
345 ignore_result(DispatchKeyEventPostIME(event
));
349 if (event
->type() == ET_KEY_PRESSED
&& handled
) {
350 ProcessFilteredKeyPressEvent(event
);
351 if (event
->stopped_propagation()) {
357 // In case the focus was changed by the key event. The |context_| should have
358 // been reset when the focused window changed.
359 if (client
!= GetTextInputClient())
362 if (HasInputMethodResult())
363 ProcessInputMethodResult(event
, handled
);
365 // In case the focus was changed when sending input method results to the
367 if (client
!= GetTextInputClient())
371 return; // IME handled the key event. do not forward.
373 if (event
->type() == ET_KEY_PRESSED
)
374 ProcessUnfilteredKeyPressEvent(event
);
375 else if (event
->type() == ET_KEY_RELEASED
)
376 ignore_result(DispatchKeyEventPostIME(event
));
379 void InputMethodChromeOS::ProcessFilteredKeyPressEvent(ui::KeyEvent
* event
) {
380 if (NeedInsertChar()) {
381 ignore_result(DispatchKeyEventPostIME(event
));
384 ui::KeyEvent
fabricated_event(ET_KEY_PRESSED
,
387 ignore_result(DispatchKeyEventPostIME(&fabricated_event
));
388 if (fabricated_event
.stopped_propagation())
389 event
->StopPropagation();
392 void InputMethodChromeOS::ProcessUnfilteredKeyPressEvent(
393 ui::KeyEvent
* event
) {
394 const TextInputClient
* prev_client
= GetTextInputClient();
395 ignore_result(DispatchKeyEventPostIME(event
));
396 if (event
->stopped_propagation()) {
401 // We shouldn't dispatch the character anymore if the key event dispatch
402 // caused focus change. For example, in the following scenario,
403 // 1. visit a web page which has a <textarea>.
405 // 3. enable Korean IME, press A, then press Tab to move the focus to the web
407 // We should return here not to send the Tab key event to RWHV.
408 TextInputClient
* client
= GetTextInputClient();
409 if (!client
|| client
!= prev_client
)
412 // If a key event was not filtered by |context_| and |character_composer_|,
413 // then it means the key event didn't generate any result text. So we need
414 // to send corresponding character to the focused text input client.
415 uint16 ch
= event
->GetCharacter();
417 client
->InsertChar(ch
, event
->flags());
420 void InputMethodChromeOS::ProcessInputMethodResult(ui::KeyEvent
* event
,
422 TextInputClient
* client
= GetTextInputClient();
425 if (result_text_
.length()) {
426 if (handled
&& NeedInsertChar()) {
427 for (base::string16::const_iterator i
= result_text_
.begin();
428 i
!= result_text_
.end(); ++i
) {
429 client
->InsertChar(*i
, event
->flags());
432 client
->InsertText(result_text_
);
433 composing_text_
= false;
437 if (composition_changed_
&& !IsTextInputTypeNone()) {
438 if (composition_
.text
.length()) {
439 composing_text_
= true;
440 client
->SetCompositionText(composition_
);
441 } else if (result_text_
.empty()) {
442 client
->ClearCompositionText();
446 // We should not clear composition text here, as it may belong to the next
447 // composition session.
448 result_text_
.clear();
449 composition_changed_
= false;
452 bool InputMethodChromeOS::NeedInsertChar() const {
453 return GetTextInputClient() &&
454 (IsTextInputTypeNone() ||
455 (!composing_text_
&& result_text_
.length() == 1));
458 bool InputMethodChromeOS::HasInputMethodResult() const {
459 return result_text_
.length() || composition_changed_
;
462 bool InputMethodChromeOS::SendFakeProcessKeyEvent(bool pressed
) const {
463 KeyEvent
evt(pressed
? ET_KEY_PRESSED
: ET_KEY_RELEASED
,
464 pressed
? VKEY_PROCESSKEY
: VKEY_UNKNOWN
,
465 EF_IME_FABRICATED_KEY
);
466 ignore_result(DispatchKeyEventPostIME(&evt
));
467 return evt
.stopped_propagation();
470 void InputMethodChromeOS::CommitText(const std::string
& text
) {
474 // We need to receive input method result even if the text input type is
475 // TEXT_INPUT_TYPE_NONE, to make sure we can always send correct
476 // character for each key event to the focused text input client.
477 if (!GetTextInputClient())
480 const base::string16 utf16_text
= base::UTF8ToUTF16(text
);
481 if (utf16_text
.empty())
484 // Append the text to the buffer, because commit signal might be fired
485 // multiple times when processing a key event.
486 result_text_
.append(utf16_text
);
488 // If we are not handling key event, do not bother sending text result if the
489 // focused text input client does not support text input.
490 if (!handling_key_event_
&& !IsTextInputTypeNone()) {
491 if (!SendFakeProcessKeyEvent(true))
492 GetTextInputClient()->InsertText(utf16_text
);
493 SendFakeProcessKeyEvent(false);
494 result_text_
.clear();
498 void InputMethodChromeOS::UpdateCompositionText(
499 const chromeos::CompositionText
& text
,
502 if (IsTextInputTypeNone())
505 if (!CanComposeInline()) {
506 chromeos::IMECandidateWindowHandlerInterface
* candidate_window
=
507 chromeos::IMEBridge::Get()->GetCandidateWindowHandler();
508 if (candidate_window
)
509 candidate_window
->UpdatePreeditText(text
.text(), cursor_pos
, visible
);
512 // |visible| argument is very confusing. For example, what's the correct
514 // 1. OnUpdatePreeditText() is called with a text and visible == false, then
515 // 2. OnShowPreeditText() is called afterwards.
517 // If it's only for clearing the current preedit text, then why not just use
518 // OnHidePreeditText()?
524 ExtractCompositionText(text
, cursor_pos
, &composition_
);
526 composition_changed_
= true;
528 // In case OnShowPreeditText() is not called.
529 if (composition_
.text
.length())
530 composing_text_
= true;
532 if (!handling_key_event_
) {
533 // If we receive a composition text without pending key event, then we need
534 // to send it to the focused text input client directly.
535 if (!SendFakeProcessKeyEvent(true))
536 GetTextInputClient()->SetCompositionText(composition_
);
537 SendFakeProcessKeyEvent(false);
538 composition_changed_
= false;
539 composition_
.Clear();
543 void InputMethodChromeOS::HidePreeditText() {
544 if (composition_
.text
.empty() || IsTextInputTypeNone())
547 // Intentionally leaves |composing_text_| unchanged.
548 composition_changed_
= true;
549 composition_
.Clear();
551 if (!handling_key_event_
) {
552 TextInputClient
* client
= GetTextInputClient();
553 if (client
&& client
->HasCompositionText()) {
554 if (!SendFakeProcessKeyEvent(true))
555 client
->ClearCompositionText();
556 SendFakeProcessKeyEvent(false);
558 composition_changed_
= false;
562 void InputMethodChromeOS::DeleteSurroundingText(int32 offset
, uint32 length
) {
563 if (!composition_
.text
.empty())
564 return; // do nothing if there is ongoing composition.
566 if (GetTextInputClient()) {
567 uint32 before
= offset
>= 0 ? 0U : static_cast<uint32
>(-1 * offset
);
568 GetTextInputClient()->ExtendSelectionAndDelete(before
, length
- before
);
572 bool InputMethodChromeOS::ExecuteCharacterComposer(const ui::KeyEvent
& event
) {
573 if (!character_composer_
.FilterKeyPress(event
))
576 // CharacterComposer consumed the key event. Update the composition text.
577 chromeos::CompositionText preedit
;
578 preedit
.set_text(character_composer_
.preedit_string());
579 UpdateCompositionText(preedit
, preedit
.text().size(),
580 !preedit
.text().empty());
581 std::string commit_text
=
582 base::UTF16ToUTF8(character_composer_
.composed_character());
583 if (!commit_text
.empty()) {
584 CommitText(commit_text
);
589 void InputMethodChromeOS::ExtractCompositionText(
590 const chromeos::CompositionText
& text
,
591 uint32 cursor_position
,
592 CompositionText
* out_composition
) const {
593 out_composition
->Clear();
594 out_composition
->text
= text
.text();
596 if (out_composition
->text
.empty())
599 // ibus uses character index for cursor position and attribute range, but we
600 // use char16 offset for them. So we need to do conversion here.
601 std::vector
<size_t> char16_offsets
;
602 size_t length
= out_composition
->text
.length();
603 base::i18n::UTF16CharIterator
char_iterator(&out_composition
->text
);
605 char16_offsets
.push_back(char_iterator
.array_pos());
606 } while (char_iterator
.Advance());
608 // The text length in Unicode characters.
609 uint32 char_length
= static_cast<uint32
>(char16_offsets
.size());
610 // Make sure we can convert the value of |char_length| as well.
611 char16_offsets
.push_back(length
);
613 size_t cursor_offset
=
614 char16_offsets
[std::min(char_length
, cursor_position
)];
616 out_composition
->selection
= gfx::Range(cursor_offset
);
618 const std::vector
<chromeos::CompositionText::UnderlineAttribute
>&
619 underline_attributes
= text
.underline_attributes();
620 if (!underline_attributes
.empty()) {
621 for (size_t i
= 0; i
< underline_attributes
.size(); ++i
) {
622 const uint32 start
= underline_attributes
[i
].start_index
;
623 const uint32 end
= underline_attributes
[i
].end_index
;
626 CompositionUnderline
underline(char16_offsets
[start
],
630 SK_ColorTRANSPARENT
);
631 if (underline_attributes
[i
].type
==
632 chromeos::CompositionText::COMPOSITION_TEXT_UNDERLINE_DOUBLE
)
633 underline
.thick
= true;
634 else if (underline_attributes
[i
].type
==
635 chromeos::CompositionText::COMPOSITION_TEXT_UNDERLINE_ERROR
)
636 underline
.color
= SK_ColorRED
;
637 else if (underline_attributes
[i
].type
==
638 chromeos::CompositionText::COMPOSITION_TEXT_UNDERLINE_NONE
)
639 underline
.color
= SK_ColorTRANSPARENT
;
640 out_composition
->underlines
.push_back(underline
);
644 DCHECK(text
.selection_start() <= text
.selection_end());
645 if (text
.selection_start() < text
.selection_end()) {
646 const uint32 start
= text
.selection_start();
647 const uint32 end
= text
.selection_end();
648 CompositionUnderline
underline(char16_offsets
[start
],
652 SK_ColorTRANSPARENT
);
653 out_composition
->underlines
.push_back(underline
);
655 // If the cursor is at start or end of this underline, then we treat
656 // it as the selection range as well, but make sure to set the cursor
657 // position to the selection end.
658 if (underline
.start_offset
== cursor_offset
) {
659 out_composition
->selection
.set_start(underline
.end_offset
);
660 out_composition
->selection
.set_end(cursor_offset
);
661 } else if (underline
.end_offset
== cursor_offset
) {
662 out_composition
->selection
.set_start(underline
.start_offset
);
663 out_composition
->selection
.set_end(cursor_offset
);
667 // Use a black thin underline by default.
668 if (out_composition
->underlines
.empty()) {
669 out_composition
->underlines
.push_back(CompositionUnderline(
670 0, length
, SK_ColorBLACK
, false /* thick */, SK_ColorTRANSPARENT
));
674 bool InputMethodChromeOS::IsNonPasswordInputFieldFocused() {
675 TextInputType type
= GetTextInputType();
676 return (type
!= TEXT_INPUT_TYPE_NONE
) && (type
!= TEXT_INPUT_TYPE_PASSWORD
);
679 bool InputMethodChromeOS::IsInputFieldFocused() {
680 return GetTextInputType() != TEXT_INPUT_TYPE_NONE
;