// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "ui/base/ime/input_method_ibus.h" #include #include #include #include #include "base/basictypes.h" #include "base/bind.h" #include "base/i18n/char_iterator.h" #include "base/logging.h" #include "base/strings/string_util.h" #include "base/strings/utf_string_conversions.h" #include "base/third_party/icu/icu_utf.h" #include "chromeos/ime/ibus_text.h" #include "chromeos/ime/input_method_descriptor.h" #include "chromeos/ime/input_method_manager.h" #include "ui/base/ime/text_input_client.h" #include "ui/events/event.h" #include "ui/events/event_constants.h" #include "ui/events/event_utils.h" #include "ui/events/keycodes/keyboard_code_conversion.h" #include "ui/events/keycodes/keyboard_code_conversion_x.h" #include "ui/events/keycodes/keyboard_codes.h" #include "ui/gfx/rect.h" namespace { chromeos::IBusEngineHandlerInterface* GetEngine() { return chromeos::IBusBridge::Get()->GetCurrentEngineHandler(); } } // namespace namespace ui { // InputMethodIBus implementation ----------------------------------------- InputMethodIBus::InputMethodIBus( internal::InputMethodDelegate* delegate) : context_focused_(false), composing_text_(false), composition_changed_(false), suppress_next_result_(false), current_keyevent_id_(0), previous_textinput_type_(TEXT_INPUT_TYPE_NONE), weak_ptr_factory_(this) { SetDelegate(delegate); chromeos::IBusBridge::Get()->SetInputContextHandler(this); UpdateContextFocusState(); OnInputMethodChanged(); } InputMethodIBus::~InputMethodIBus() { AbandonAllPendingKeyEvents(); context_focused_ = false; ConfirmCompositionText(); // We are dead, so we need to ask the client to stop relying on us. OnInputMethodChanged(); chromeos::IBusBridge::Get()->SetInputContextHandler(NULL); } void InputMethodIBus::OnFocus() { InputMethodBase::OnFocus(); UpdateContextFocusState(); } void InputMethodIBus::OnBlur() { ConfirmCompositionText(); InputMethodBase::OnBlur(); UpdateContextFocusState(); } bool InputMethodIBus::OnUntranslatedIMEMessage(const base::NativeEvent& event, NativeEventResult* result) { return false; } void InputMethodIBus::ProcessKeyEventDone(uint32 id, ui::KeyEvent* event, bool is_handled) { if (pending_key_events_.find(id) == pending_key_events_.end()) return; // Abandoned key event. DCHECK(event); if (event->type() == ET_KEY_PRESSED) { if (is_handled) { // IME event has a priority to be handled, so that character composer // should be reset. character_composer_.Reset(); } else { // If IME does not handle key event, passes keyevent to character composer // to be able to compose complex characters. is_handled = ExecuteCharacterComposer(*event); } } if (event->type() == ET_KEY_PRESSED || event->type() == ET_KEY_RELEASED) ProcessKeyEventPostIME(*event, is_handled); // ProcessKeyEventPostIME may change the |pending_key_events_|. pending_key_events_.erase(id); } bool InputMethodIBus::DispatchKeyEvent(const ui::KeyEvent& event) { DCHECK(event.type() == ET_KEY_PRESSED || event.type() == ET_KEY_RELEASED); DCHECK(system_toplevel_window_focused()); // If |context_| is not usable, then we can only dispatch the key event as is. // We also dispatch the key event directly if the current text input type is // TEXT_INPUT_TYPE_PASSWORD, to bypass the input method. // Note: We need to send the key event to ibus even if the |context_| is not // enabled, so that ibus can have a chance to enable the |context_|. if (!context_focused_ || !GetEngine() || GetTextInputType() == TEXT_INPUT_TYPE_PASSWORD ) { if (event.type() == ET_KEY_PRESSED) { if (ExecuteCharacterComposer(event)) { // Treating as PostIME event if character composer handles key event and // generates some IME event, ProcessKeyEventPostIME(event, true); return true; } ProcessUnfilteredKeyPressEvent(event); } else { DispatchKeyEventPostIME(event); } return true; } pending_key_events_.insert(current_keyevent_id_); ui::KeyEvent* copied_event = new ui::KeyEvent(event); GetEngine()->ProcessKeyEvent( event, base::Bind(&InputMethodIBus::ProcessKeyEventDone, weak_ptr_factory_.GetWeakPtr(), current_keyevent_id_, // Pass the ownership of |copied_event|. base::Owned(copied_event))); ++current_keyevent_id_; // We don't want to suppress the result generated by this key event, but it // may cause problem. See comment in ResetContext() method. suppress_next_result_ = false; return true; } void InputMethodIBus::OnTextInputTypeChanged(const TextInputClient* client) { if (IsTextInputClientFocused(client)) { ResetContext(); UpdateContextFocusState(); if (previous_textinput_type_ != client->GetTextInputType()) OnInputMethodChanged(); previous_textinput_type_ = client->GetTextInputType(); } InputMethodBase::OnTextInputTypeChanged(client); } void InputMethodIBus::OnCaretBoundsChanged(const TextInputClient* client) { if (!context_focused_ || !IsTextInputClientFocused(client)) return; // The current text input type should not be NONE if |context_| is focused. DCHECK(!IsTextInputTypeNone()); const gfx::Rect rect = GetTextInputClient()->GetCaretBounds(); gfx::Rect composition_head; if (!GetTextInputClient()->GetCompositionCharacterBounds(0, &composition_head)) { composition_head = rect; } chromeos::IBusPanelCandidateWindowHandlerInterface* candidate_window = chromeos::IBusBridge::Get()->GetCandidateWindowHandler(); if (!candidate_window) return; candidate_window->SetCursorBounds(rect, composition_head); gfx::Range text_range; gfx::Range selection_range; string16 surrounding_text; if (!GetTextInputClient()->GetTextRange(&text_range) || !GetTextInputClient()->GetTextFromRange(text_range, &surrounding_text) || !GetTextInputClient()->GetSelectionRange(&selection_range)) { previous_surrounding_text_.clear(); previous_selection_range_ = gfx::Range::InvalidRange(); return; } if (previous_selection_range_ == selection_range && previous_surrounding_text_ == surrounding_text) return; previous_selection_range_ = selection_range; previous_surrounding_text_ = surrounding_text; if (!selection_range.IsValid()) { // TODO(nona): Ideally selection_range should not be invalid. // TODO(nona): If javascript changes the focus on page loading, even (0,0) // can not be obtained. Need investigation. return; } // Here SetSurroundingText accepts relative position of |surrounding_text|, so // we have to convert |selection_range| from node coordinates to // |surrounding_text| coordinates. if (!GetEngine()) return; GetEngine()->SetSurroundingText(UTF16ToUTF8(surrounding_text), selection_range.start() - text_range.start(), selection_range.end() - text_range.start()); } void InputMethodIBus::CancelComposition(const TextInputClient* client) { if (context_focused_ && IsTextInputClientFocused(client)) ResetContext(); } void InputMethodIBus::OnInputLocaleChanged() { // Not supported. } std::string InputMethodIBus::GetInputLocale() { // Not supported. return ""; } base::i18n::TextDirection InputMethodIBus::GetInputTextDirection() { // Not supported. return base::i18n::UNKNOWN_DIRECTION; } bool InputMethodIBus::IsActive() { return true; } bool InputMethodIBus::IsCandidatePopupOpen() const { // TODO(yukishiino): Implement this method. return false; } void InputMethodIBus::OnWillChangeFocusedClient(TextInputClient* focused_before, TextInputClient* focused) { ConfirmCompositionText(); } void InputMethodIBus::OnDidChangeFocusedClient(TextInputClient* focused_before, TextInputClient* focused) { // Force to update the input type since client's TextInputStateChanged() // function might not be called if text input types before the client loses // focus and after it acquires focus again are the same. OnTextInputTypeChanged(focused); UpdateContextFocusState(); // Force to update caret bounds, in case the client thinks that the caret // bounds has not changed. OnCaretBoundsChanged(focused); } void InputMethodIBus::ConfirmCompositionText() { TextInputClient* client = GetTextInputClient(); if (client && client->HasCompositionText()) client->ConfirmCompositionText(); ResetContext(); } void InputMethodIBus::ResetContext() { if (!context_focused_ || !GetTextInputClient()) return; DCHECK(system_toplevel_window_focused()); // Because ibus runs in asynchronous mode, the input method may still send us // results after sending out the reset request, so we use a flag to discard // all results generated by previous key events. But because ibus does not // have a mechanism to identify each key event and corresponding results, this // approach will not work for some corner cases. For example if the user types // very fast, then the next key event may come in before the |context_| is // really reset. Then we actually cannot know whether or not the next // result should be discard. suppress_next_result_ = true; composition_.Clear(); result_text_.clear(); composing_text_ = false; composition_changed_ = false; // We need to abandon all pending key events, but as above comment says, there // is no reliable way to abandon all results generated by these abandoned key // events. AbandonAllPendingKeyEvents(); // This function runs asynchronously. // Note: some input method engines may not support reset method, such as // ibus-anthy. But as we control all input method engines by ourselves, we can // make sure that all of the engines we are using support it correctly. if (GetEngine()) GetEngine()->Reset(); character_composer_.Reset(); } void InputMethodIBus::UpdateContextFocusState() { const bool old_context_focused = context_focused_; const TextInputType current_text_input_type = GetTextInputType(); // Use switch here in case we are going to add more text input types. switch (current_text_input_type) { case TEXT_INPUT_TYPE_NONE: case TEXT_INPUT_TYPE_PASSWORD: context_focused_ = false; break; default: context_focused_ = true; break; } // Propagate the focus event to the candidate window handler which also // manages the input method mode indicator. chromeos::IBusPanelCandidateWindowHandlerInterface* candidate_window = chromeos::IBusBridge::Get()->GetCandidateWindowHandler(); if (candidate_window) candidate_window->FocusStateChanged(context_focused_); if (!GetEngine()) return; // We only focus in |context_| when the focus is in a normal textfield. // Even if focus is not changed, a text input type change causes a focus // blink. // ibus_input_context_focus_{in|out}() run asynchronously. bool input_type_change = (current_text_input_type != previous_textinput_type_); if (old_context_focused && (!context_focused_ || input_type_change)) GetEngine()->FocusOut(); if (context_focused_ && (!old_context_focused || input_type_change)) { chromeos::IBusEngineHandlerInterface::InputContext context( current_text_input_type, GetTextInputMode()); GetEngine()->FocusIn(context); OnCaretBoundsChanged(GetTextInputClient()); } } void InputMethodIBus::ProcessKeyEventPostIME( const ui::KeyEvent& event, bool handled) { TextInputClient* client = GetTextInputClient(); if (!client) { // As ibus works asynchronously, there is a chance that the focused client // loses focus before this method gets called. DispatchKeyEventPostIME(event); return; } if (event.type() == ET_KEY_PRESSED && handled) ProcessFilteredKeyPressEvent(event); // In case the focus was changed by the key event. The |context_| should have // been reset when the focused window changed. if (client != GetTextInputClient()) return; if (HasInputMethodResult()) ProcessInputMethodResult(event, handled); // In case the focus was changed when sending input method results to the // focused window. if (client != GetTextInputClient()) return; if (event.type() == ET_KEY_PRESSED && !handled) ProcessUnfilteredKeyPressEvent(event); else if (event.type() == ET_KEY_RELEASED) DispatchKeyEventPostIME(event); } void InputMethodIBus::ProcessFilteredKeyPressEvent(const ui::KeyEvent& event) { if (NeedInsertChar()) { DispatchKeyEventPostIME(event); } else { const ui::KeyEvent fabricated_event(ET_KEY_PRESSED, VKEY_PROCESSKEY, event.flags(), false); // is_char DispatchKeyEventPostIME(fabricated_event); } } void InputMethodIBus::ProcessUnfilteredKeyPressEvent( const ui::KeyEvent& event) { const TextInputClient* prev_client = GetTextInputClient(); DispatchKeyEventPostIME(event); // We shouldn't dispatch the character anymore if the key event dispatch // caused focus change. For example, in the following scenario, // 1. visit a web page which has a