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/third_party/icu/icu_utf.h"
19 #include "chromeos/ime/composition_text.h"
20 #include "ui/base/ime/text_input_client.h"
21 #include "ui/events/event.h"
22 #include "ui/events/event_constants.h"
23 #include "ui/events/event_utils.h"
24 #include "ui/events/keycodes/keyboard_code_conversion.h"
25 #include "ui/events/keycodes/keyboard_code_conversion_x.h"
26 #include "ui/events/keycodes/keyboard_codes.h"
27 #include "ui/gfx/rect.h"
30 chromeos::IMEEngineHandlerInterface
* GetEngine() {
31 return chromeos::IMEBridge::Get()->GetCurrentEngineHandler();
37 // InputMethodChromeOS implementation -----------------------------------------
38 InputMethodChromeOS::InputMethodChromeOS(
39 internal::InputMethodDelegate
* delegate
)
40 : composing_text_(false),
41 composition_changed_(false),
42 current_keyevent_id_(0),
43 previous_textinput_type_(TEXT_INPUT_TYPE_NONE
),
44 weak_ptr_factory_(this) {
45 SetDelegate(delegate
);
46 chromeos::IMEBridge::Get()->SetInputContextHandler(this);
48 UpdateContextFocusState();
49 OnInputMethodChanged();
52 InputMethodChromeOS::~InputMethodChromeOS() {
53 AbandonAllPendingKeyEvents();
54 ConfirmCompositionText();
55 // We are dead, so we need to ask the client to stop relying on us.
56 OnInputMethodChanged();
58 chromeos::IMEBridge::Get()->SetInputContextHandler(NULL
);
61 void InputMethodChromeOS::OnFocus() {
62 InputMethodBase::OnFocus();
63 OnTextInputTypeChanged(GetTextInputClient());
66 void InputMethodChromeOS::OnBlur() {
67 ConfirmCompositionText();
68 InputMethodBase::OnBlur();
69 OnTextInputTypeChanged(GetTextInputClient());
72 bool InputMethodChromeOS::OnUntranslatedIMEMessage(
73 const base::NativeEvent
& event
,
74 NativeEventResult
* result
) {
78 void InputMethodChromeOS::ProcessKeyEventDone(uint32 id
,
81 if (pending_key_events_
.find(id
) == pending_key_events_
.end())
82 return; // Abandoned key event.
85 if (event
->type() == ET_KEY_PRESSED
) {
87 // IME event has a priority to be handled, so that character composer
89 character_composer_
.Reset();
91 // If IME does not handle key event, passes keyevent to character composer
92 // to be able to compose complex characters.
93 is_handled
= ExecuteCharacterComposer(*event
);
97 if (event
->type() == ET_KEY_PRESSED
|| event
->type() == ET_KEY_RELEASED
)
98 ProcessKeyEventPostIME(*event
, is_handled
);
100 // ProcessKeyEventPostIME may change the |pending_key_events_|.
101 pending_key_events_
.erase(id
);
104 bool InputMethodChromeOS::DispatchKeyEvent(const ui::KeyEvent
& event
) {
105 DCHECK(event
.type() == ET_KEY_PRESSED
|| event
.type() == ET_KEY_RELEASED
);
106 DCHECK(system_toplevel_window_focused());
108 // If |context_| is not usable, then we can only dispatch the key event as is.
109 // We only dispatch the key event to input method when the |context_| is an
110 // normal input field (not a password field).
111 // Note: We need to send the key event to ibus even if the |context_| is not
112 // enabled, so that ibus can have a chance to enable the |context_|.
113 if (!IsInputFieldFocused() || !GetEngine()) {
114 if (event
.type() == ET_KEY_PRESSED
) {
115 if (ExecuteCharacterComposer(event
)) {
116 // Treating as PostIME event if character composer handles key event and
117 // generates some IME event,
118 ProcessKeyEventPostIME(event
, true);
121 ProcessUnfilteredKeyPressEvent(event
);
123 DispatchKeyEventPostIME(event
);
128 pending_key_events_
.insert(current_keyevent_id_
);
130 ui::KeyEvent
* copied_event
= new ui::KeyEvent(event
);
131 GetEngine()->ProcessKeyEvent(
133 base::Bind(&InputMethodChromeOS::ProcessKeyEventDone
,
134 weak_ptr_factory_
.GetWeakPtr(),
135 current_keyevent_id_
,
136 // Pass the ownership of |copied_event|.
137 base::Owned(copied_event
)));
139 ++current_keyevent_id_
;
143 void InputMethodChromeOS::OnTextInputTypeChanged(
144 const TextInputClient
* client
) {
145 if (IsTextInputClientFocused(client
)) {
147 UpdateContextFocusState();
148 if (previous_textinput_type_
!= client
->GetTextInputType())
149 OnInputMethodChanged();
150 previous_textinput_type_
= client
->GetTextInputType();
152 InputMethodBase::OnTextInputTypeChanged(client
);
155 void InputMethodChromeOS::OnCaretBoundsChanged(const TextInputClient
* client
) {
156 if (!IsInputFieldFocused() || !IsTextInputClientFocused(client
))
159 // The current text input type should not be NONE if |context_| is focused.
160 DCHECK(!IsTextInputTypeNone());
161 const gfx::Rect rect
= GetTextInputClient()->GetCaretBounds();
163 gfx::Rect composition_head
;
164 if (!GetTextInputClient()->GetCompositionCharacterBounds(0,
165 &composition_head
)) {
166 composition_head
= rect
;
169 chromeos::IMECandidateWindowHandlerInterface
* candidate_window
=
170 chromeos::IMEBridge::Get()->GetCandidateWindowHandler();
171 if (!candidate_window
)
173 candidate_window
->SetCursorBounds(rect
, composition_head
);
175 gfx::Range text_range
;
176 gfx::Range selection_range
;
177 base::string16 surrounding_text
;
178 if (!GetTextInputClient()->GetTextRange(&text_range
) ||
179 !GetTextInputClient()->GetTextFromRange(text_range
, &surrounding_text
) ||
180 !GetTextInputClient()->GetSelectionRange(&selection_range
)) {
181 previous_surrounding_text_
.clear();
182 previous_selection_range_
= gfx::Range::InvalidRange();
186 if (previous_selection_range_
== selection_range
&&
187 previous_surrounding_text_
== surrounding_text
)
190 previous_selection_range_
= selection_range
;
191 previous_surrounding_text_
= surrounding_text
;
193 if (!selection_range
.IsValid()) {
194 // TODO(nona): Ideally selection_range should not be invalid.
195 // TODO(nona): If javascript changes the focus on page loading, even (0,0)
196 // can not be obtained. Need investigation.
200 // Here SetSurroundingText accepts relative position of |surrounding_text|, so
201 // we have to convert |selection_range| from node coordinates to
202 // |surrounding_text| coordinates.
205 GetEngine()->SetSurroundingText(base::UTF16ToUTF8(surrounding_text
),
206 selection_range
.start() - text_range
.start(),
207 selection_range
.end() - text_range
.start());
210 void InputMethodChromeOS::CancelComposition(const TextInputClient
* client
) {
211 if (IsInputFieldFocused() && IsTextInputClientFocused(client
))
215 void InputMethodChromeOS::OnInputLocaleChanged() {
219 std::string
InputMethodChromeOS::GetInputLocale() {
224 bool InputMethodChromeOS::IsActive() {
228 bool InputMethodChromeOS::IsCandidatePopupOpen() const {
229 // TODO(yukishiino): Implement this method.
233 void InputMethodChromeOS::OnWillChangeFocusedClient(
234 TextInputClient
* focused_before
,
235 TextInputClient
* focused
) {
236 ConfirmCompositionText();
239 void InputMethodChromeOS::OnDidChangeFocusedClient(
240 TextInputClient
* focused_before
,
241 TextInputClient
* focused
) {
242 // Force to update the input type since client's TextInputStateChanged()
243 // function might not be called if text input types before the client loses
244 // focus and after it acquires focus again are the same.
245 OnTextInputTypeChanged(focused
);
247 UpdateContextFocusState();
248 // Force to update caret bounds, in case the client thinks that the caret
249 // bounds has not changed.
250 OnCaretBoundsChanged(focused
);
253 void InputMethodChromeOS::ConfirmCompositionText() {
254 TextInputClient
* client
= GetTextInputClient();
255 if (client
&& client
->HasCompositionText())
256 client
->ConfirmCompositionText();
261 void InputMethodChromeOS::ResetContext() {
262 if (!IsInputFieldFocused() || !GetTextInputClient())
265 DCHECK(system_toplevel_window_focused());
267 composition_
.Clear();
268 result_text_
.clear();
269 composing_text_
= false;
270 composition_changed_
= false;
272 // We need to abandon all pending key events, but as above comment says, there
273 // is no reliable way to abandon all results generated by these abandoned key
275 AbandonAllPendingKeyEvents();
277 // This function runs asynchronously.
278 // Note: some input method engines may not support reset method, such as
279 // ibus-anthy. But as we control all input method engines by ourselves, we can
280 // make sure that all of the engines we are using support it correctly.
282 GetEngine()->Reset();
284 character_composer_
.Reset();
287 void InputMethodChromeOS::UpdateContextFocusState() {
288 // Propagate the focus event to the candidate window handler which also
289 // manages the input method mode indicator.
290 chromeos::IMECandidateWindowHandlerInterface
* candidate_window
=
291 chromeos::IMEBridge::Get()->GetCandidateWindowHandler();
292 if (candidate_window
)
293 candidate_window
->FocusStateChanged(IsInputFieldFocused());
295 const TextInputType current_text_input_type
= GetTextInputType();
296 chromeos::IMEBridge::Get()->SetCurrentTextInputType(current_text_input_type
);
301 // When focus is not changed, a text input type change causes a focus
302 // blink. The focus in to or out from password field should also notify
304 if (previous_textinput_type_
!= TEXT_INPUT_TYPE_NONE
&&
305 previous_textinput_type_
!= current_text_input_type
)
306 GetEngine()->FocusOut();
307 if (current_text_input_type
!= TEXT_INPUT_TYPE_NONE
&&
308 current_text_input_type
!= previous_textinput_type_
) {
309 chromeos::IMEEngineHandlerInterface::InputContext
context(
310 current_text_input_type
, GetTextInputMode());
311 GetEngine()->FocusIn(context
);
312 OnCaretBoundsChanged(GetTextInputClient());
316 void InputMethodChromeOS::ProcessKeyEventPostIME(
317 const ui::KeyEvent
& event
,
319 TextInputClient
* client
= GetTextInputClient();
321 // As ibus works asynchronously, there is a chance that the focused client
322 // loses focus before this method gets called.
323 DispatchKeyEventPostIME(event
);
327 if (event
.type() == ET_KEY_PRESSED
&& handled
)
328 ProcessFilteredKeyPressEvent(event
);
330 // In case the focus was changed by the key event. The |context_| should have
331 // been reset when the focused window changed.
332 if (client
!= GetTextInputClient())
335 if (HasInputMethodResult())
336 ProcessInputMethodResult(event
, handled
);
338 // In case the focus was changed when sending input method results to the
340 if (client
!= GetTextInputClient())
343 if (event
.type() == ET_KEY_PRESSED
&& !handled
)
344 ProcessUnfilteredKeyPressEvent(event
);
345 else if (event
.type() == ET_KEY_RELEASED
)
346 DispatchKeyEventPostIME(event
);
349 void InputMethodChromeOS::ProcessFilteredKeyPressEvent(
350 const ui::KeyEvent
& event
) {
351 if (NeedInsertChar()) {
352 DispatchKeyEventPostIME(event
);
354 const ui::KeyEvent
fabricated_event(ET_KEY_PRESSED
,
358 DispatchKeyEventPostIME(fabricated_event
);
362 void InputMethodChromeOS::ProcessUnfilteredKeyPressEvent(
363 const ui::KeyEvent
& event
) {
364 const TextInputClient
* prev_client
= GetTextInputClient();
365 DispatchKeyEventPostIME(event
);
367 // We shouldn't dispatch the character anymore if the key event dispatch
368 // caused focus change. For example, in the following scenario,
369 // 1. visit a web page which has a <textarea>.
371 // 3. enable Korean IME, press A, then press Tab to move the focus to the web
373 // We should return here not to send the Tab key event to RWHV.
374 TextInputClient
* client
= GetTextInputClient();
375 if (!client
|| client
!= prev_client
)
378 // If a key event was not filtered by |context_| and |character_composer_|,
379 // then it means the key event didn't generate any result text. So we need
380 // to send corresponding character to the focused text input client.
381 const uint32 event_flags
= event
.flags();
383 if (event
.HasNativeEvent()) {
384 const base::NativeEvent
& native_event
= event
.native_event();
386 if (!(event_flags
& ui::EF_CONTROL_DOWN
))
387 ch
= ui::GetCharacterFromXEvent(native_event
);
389 ch
= ui::GetCharacterFromKeyCode(
390 ui::KeyboardCodeFromNative(native_event
), event_flags
);
393 ch
= ui::GetCharacterFromKeyCode(event
.key_code(), event_flags
);
397 client
->InsertChar(ch
, event_flags
);
400 void InputMethodChromeOS::ProcessInputMethodResult(const ui::KeyEvent
& event
,
402 TextInputClient
* client
= GetTextInputClient();
405 if (result_text_
.length()) {
406 if (handled
&& NeedInsertChar()) {
407 for (base::string16::const_iterator i
= result_text_
.begin();
408 i
!= result_text_
.end(); ++i
) {
409 client
->InsertChar(*i
, event
.flags());
412 client
->InsertText(result_text_
);
413 composing_text_
= false;
417 if (composition_changed_
&& !IsTextInputTypeNone()) {
418 if (composition_
.text
.length()) {
419 composing_text_
= true;
420 client
->SetCompositionText(composition_
);
421 } else if (result_text_
.empty()) {
422 client
->ClearCompositionText();
426 // We should not clear composition text here, as it may belong to the next
427 // composition session.
428 result_text_
.clear();
429 composition_changed_
= false;
432 bool InputMethodChromeOS::NeedInsertChar() const {
433 return GetTextInputClient() &&
434 (IsTextInputTypeNone() ||
435 (!composing_text_
&& result_text_
.length() == 1));
438 bool InputMethodChromeOS::HasInputMethodResult() const {
439 return result_text_
.length() || composition_changed_
;
442 void InputMethodChromeOS::AbandonAllPendingKeyEvents() {
443 pending_key_events_
.clear();
446 void InputMethodChromeOS::CommitText(const std::string
& text
) {
450 // We need to receive input method result even if the text input type is
451 // TEXT_INPUT_TYPE_NONE, to make sure we can always send correct
452 // character for each key event to the focused text input client.
453 if (!GetTextInputClient())
456 const base::string16 utf16_text
= base::UTF8ToUTF16(text
);
457 if (utf16_text
.empty())
460 // Append the text to the buffer, because commit signal might be fired
461 // multiple times when processing a key event.
462 result_text_
.append(utf16_text
);
464 // If we are not handling key event, do not bother sending text result if the
465 // focused text input client does not support text input.
466 if (pending_key_events_
.empty() && !IsTextInputTypeNone()) {
467 GetTextInputClient()->InsertText(utf16_text
);
468 result_text_
.clear();
472 void InputMethodChromeOS::UpdateCompositionText(
473 const chromeos::CompositionText
& text
,
476 if (IsTextInputTypeNone())
479 if (!CanComposeInline()) {
480 chromeos::IMECandidateWindowHandlerInterface
* candidate_window
=
481 chromeos::IMEBridge::Get()->GetCandidateWindowHandler();
482 if (candidate_window
)
483 candidate_window
->UpdatePreeditText(text
.text(), cursor_pos
, visible
);
486 // |visible| argument is very confusing. For example, what's the correct
488 // 1. OnUpdatePreeditText() is called with a text and visible == false, then
489 // 2. OnShowPreeditText() is called afterwards.
491 // If it's only for clearing the current preedit text, then why not just use
492 // OnHidePreeditText()?
498 ExtractCompositionText(text
, cursor_pos
, &composition_
);
500 composition_changed_
= true;
502 // In case OnShowPreeditText() is not called.
503 if (composition_
.text
.length())
504 composing_text_
= true;
506 // If we receive a composition text without pending key event, then we need to
507 // send it to the focused text input client directly.
508 if (pending_key_events_
.empty()) {
509 GetTextInputClient()->SetCompositionText(composition_
);
510 composition_changed_
= false;
511 composition_
.Clear();
515 void InputMethodChromeOS::HidePreeditText() {
516 if (composition_
.text
.empty() || IsTextInputTypeNone())
519 // Intentionally leaves |composing_text_| unchanged.
520 composition_changed_
= true;
521 composition_
.Clear();
523 if (pending_key_events_
.empty()) {
524 TextInputClient
* client
= GetTextInputClient();
525 if (client
&& client
->HasCompositionText())
526 client
->ClearCompositionText();
527 composition_changed_
= false;
531 void InputMethodChromeOS::DeleteSurroundingText(int32 offset
, uint32 length
) {
532 if (!composition_
.text
.empty())
533 return; // do nothing if there is ongoing composition.
534 if (offset
< 0 && static_cast<uint32
>(-1 * offset
) != length
)
535 return; // only preceding text can be deletable.
536 if (GetTextInputClient())
537 GetTextInputClient()->ExtendSelectionAndDelete(length
, 0U);
540 bool InputMethodChromeOS::ExecuteCharacterComposer(const ui::KeyEvent
& event
) {
541 if (!character_composer_
.FilterKeyPress(event
))
544 // CharacterComposer consumed the key event. Update the composition text.
545 chromeos::CompositionText preedit
;
546 preedit
.set_text(character_composer_
.preedit_string());
547 UpdateCompositionText(preedit
, preedit
.text().size(),
548 !preedit
.text().empty());
549 std::string commit_text
=
550 base::UTF16ToUTF8(character_composer_
.composed_character());
551 if (!commit_text
.empty()) {
552 CommitText(commit_text
);
557 void InputMethodChromeOS::ExtractCompositionText(
558 const chromeos::CompositionText
& text
,
559 uint32 cursor_position
,
560 CompositionText
* out_composition
) const {
561 out_composition
->Clear();
562 out_composition
->text
= text
.text();
564 if (out_composition
->text
.empty())
567 // ibus uses character index for cursor position and attribute range, but we
568 // use char16 offset for them. So we need to do conversion here.
569 std::vector
<size_t> char16_offsets
;
570 size_t length
= out_composition
->text
.length();
571 base::i18n::UTF16CharIterator
char_iterator(&out_composition
->text
);
573 char16_offsets
.push_back(char_iterator
.array_pos());
574 } while (char_iterator
.Advance());
576 // The text length in Unicode characters.
577 uint32 char_length
= static_cast<uint32
>(char16_offsets
.size());
578 // Make sure we can convert the value of |char_length| as well.
579 char16_offsets
.push_back(length
);
581 size_t cursor_offset
=
582 char16_offsets
[std::min(char_length
, cursor_position
)];
584 out_composition
->selection
= gfx::Range(cursor_offset
);
586 const std::vector
<chromeos::CompositionText::UnderlineAttribute
>&
587 underline_attributes
= text
.underline_attributes();
588 if (!underline_attributes
.empty()) {
589 for (size_t i
= 0; i
< underline_attributes
.size(); ++i
) {
590 const uint32 start
= underline_attributes
[i
].start_index
;
591 const uint32 end
= underline_attributes
[i
].end_index
;
594 CompositionUnderline
underline(
595 char16_offsets
[start
], char16_offsets
[end
],
596 SK_ColorBLACK
, false /* thick */);
597 if (underline_attributes
[i
].type
==
598 chromeos::CompositionText::COMPOSITION_TEXT_UNDERLINE_DOUBLE
)
599 underline
.thick
= true;
600 else if (underline_attributes
[i
].type
==
601 chromeos::CompositionText::COMPOSITION_TEXT_UNDERLINE_ERROR
)
602 underline
.color
= SK_ColorRED
;
603 out_composition
->underlines
.push_back(underline
);
607 DCHECK(text
.selection_start() <= text
.selection_end());
608 if (text
.selection_start() < text
.selection_end()) {
609 const uint32 start
= text
.selection_start();
610 const uint32 end
= text
.selection_end();
611 CompositionUnderline
underline(
612 char16_offsets
[start
], char16_offsets
[end
],
613 SK_ColorBLACK
, true /* thick */);
614 out_composition
->underlines
.push_back(underline
);
616 // If the cursor is at start or end of this underline, then we treat
617 // it as the selection range as well, but make sure to set the cursor
618 // position to the selection end.
619 if (underline
.start_offset
== cursor_offset
) {
620 out_composition
->selection
.set_start(underline
.end_offset
);
621 out_composition
->selection
.set_end(cursor_offset
);
622 } else if (underline
.end_offset
== cursor_offset
) {
623 out_composition
->selection
.set_start(underline
.start_offset
);
624 out_composition
->selection
.set_end(cursor_offset
);
628 // Use a black thin underline by default.
629 if (out_composition
->underlines
.empty()) {
630 out_composition
->underlines
.push_back(CompositionUnderline(
631 0, length
, SK_ColorBLACK
, false /* thick */));
635 bool InputMethodChromeOS::IsInputFieldFocused() {
636 TextInputType type
= GetTextInputType();
637 return (type
!= TEXT_INPUT_TYPE_NONE
) && (type
!= TEXT_INPUT_TYPE_PASSWORD
);