Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / ui / base / ime / input_method_chromeos.cc
index 10b1b48..c951b43 100644 (file)
@@ -17,8 +17,6 @@
 #include "base/strings/utf_string_conversions.h"
 #include "base/third_party/icu/icu_utf.h"
 #include "chromeos/ime/composition_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"
@@ -39,22 +37,18 @@ namespace ui {
 // InputMethodChromeOS implementation -----------------------------------------
 InputMethodChromeOS::InputMethodChromeOS(
     internal::InputMethodDelegate* delegate)
-    : context_focused_(false),
-      composing_text_(false),
+    : composing_text_(false),
       composition_changed_(false),
       current_keyevent_id_(0),
-      previous_textinput_type_(TEXT_INPUT_TYPE_NONE),
       weak_ptr_factory_(this) {
   SetDelegate(delegate);
   chromeos::IMEBridge::Get()->SetInputContextHandler(this);
 
   UpdateContextFocusState();
-  OnInputMethodChanged();
 }
 
 InputMethodChromeOS::~InputMethodChromeOS() {
   AbandonAllPendingKeyEvents();
-  context_focused_ = false;
   ConfirmCompositionText();
   // We are dead, so we need to ask the client to stop relying on us.
   OnInputMethodChanged();
@@ -64,13 +58,13 @@ InputMethodChromeOS::~InputMethodChromeOS() {
 
 void InputMethodChromeOS::OnFocus() {
   InputMethodBase::OnFocus();
-  UpdateContextFocusState();
+  OnTextInputTypeChanged(GetTextInputClient());
 }
 
 void InputMethodChromeOS::OnBlur() {
   ConfirmCompositionText();
   InputMethodBase::OnBlur();
-  UpdateContextFocusState();
+  OnTextInputTypeChanged(GetTextInputClient());
 }
 
 bool InputMethodChromeOS::OnUntranslatedIMEMessage(
@@ -110,12 +104,11 @@ bool InputMethodChromeOS::DispatchKeyEvent(const ui::KeyEvent& event) {
   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.
+  // We only dispatch the key event to input method when the |context_| is an
+  // normal input field (not a password field).
   // 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 (!IsInputFieldFocused() || !GetEngine()) {
     if (event.type() == ET_KEY_PRESSED) {
       if (ExecuteCharacterComposer(event)) {
         // Treating as PostIME event if character composer handles key event and
@@ -147,18 +140,27 @@ bool InputMethodChromeOS::DispatchKeyEvent(const ui::KeyEvent& event) {
 
 void InputMethodChromeOS::OnTextInputTypeChanged(
     const TextInputClient* client) {
-  if (IsTextInputClientFocused(client)) {
-    ResetContext();
-    UpdateContextFocusState();
-    if (previous_textinput_type_ != client->GetTextInputType())
-      OnInputMethodChanged();
-    previous_textinput_type_ = client->GetTextInputType();
+  if (!IsTextInputClientFocused(client))
+    return;
+
+  UpdateContextFocusState();
+
+  chromeos::IMEEngineHandlerInterface* engine = GetEngine();
+  if (engine) {
+    // When focused input client is not changed, a text input type change should
+    // cause blur/focus events to engine.
+    // The focus in to or out from password field should also notify engine.
+    engine->FocusOut();
+    chromeos::IMEEngineHandlerInterface::InputContext context(
+        GetTextInputType(), GetTextInputMode());
+    engine->FocusIn(context);
   }
+
   InputMethodBase::OnTextInputTypeChanged(client);
 }
 
 void InputMethodChromeOS::OnCaretBoundsChanged(const TextInputClient* client) {
-  if (!context_focused_ || !IsTextInputClientFocused(client))
+  if (!IsInputFieldFocused() || !IsTextInputClientFocused(client))
     return;
 
   // The current text input type should not be NONE if |context_| is focused.
@@ -213,7 +215,7 @@ void InputMethodChromeOS::OnCaretBoundsChanged(const TextInputClient* client) {
 }
 
 void InputMethodChromeOS::CancelComposition(const TextInputClient* client) {
-  if (context_focused_ && IsTextInputClientFocused(client))
+  if (IsInputFieldFocused() && IsTextInputClientFocused(client))
     ResetContext();
 }
 
@@ -239,6 +241,9 @@ void InputMethodChromeOS::OnWillChangeFocusedClient(
     TextInputClient* focused_before,
     TextInputClient* focused) {
   ConfirmCompositionText();
+
+  if (GetEngine())
+    GetEngine()->FocusOut();
 }
 
 void InputMethodChromeOS::OnDidChangeFocusedClient(
@@ -247,12 +252,13 @@ void InputMethodChromeOS::OnDidChangeFocusedClient(
   // 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);
+
+  if (GetEngine()) {
+    chromeos::IMEEngineHandlerInterface::InputContext context(
+        GetTextInputType(), GetTextInputMode());
+    GetEngine()->FocusIn(context);
+  }
 }
 
 void InputMethodChromeOS::ConfirmCompositionText() {
@@ -264,7 +270,7 @@ void InputMethodChromeOS::ConfirmCompositionText() {
 }
 
 void InputMethodChromeOS::ResetContext() {
-  if (!context_focused_ || !GetTextInputClient())
+  if (!IsInputFieldFocused() || !GetTextInputClient())
     return;
 
   DCHECK(system_toplevel_window_focused());
@@ -290,43 +296,20 @@ void InputMethodChromeOS::ResetContext() {
 }
 
 void InputMethodChromeOS::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;
-  }
+  ResetContext();
+  OnInputMethodChanged();
 
   // Propagate the focus event to the candidate window handler which also
   // manages the input method mode indicator.
   chromeos::IMECandidateWindowHandlerInterface* candidate_window =
       chromeos::IMEBridge::Get()->GetCandidateWindowHandler();
   if (candidate_window)
-    candidate_window->FocusStateChanged(context_focused_);
+    candidate_window->FocusStateChanged(IsInputFieldFocused());
 
-  if (!GetEngine())
-    return;
+  chromeos::IMEBridge::Get()->SetCurrentTextInputType(GetTextInputType());
 
-  // 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::IMEEngineHandlerInterface::InputContext context(
-        current_text_input_type, GetTextInputMode());
-    GetEngine()->FocusIn(context);
+  if (!IsTextInputTypeNone())
     OnCaretBoundsChanged(GetTextInputClient());
-  }
 }
 
 void InputMethodChromeOS::ProcessKeyEventPostIME(
@@ -648,4 +631,9 @@ void InputMethodChromeOS::ExtractCompositionText(
   }
 }
 
+bool InputMethodChromeOS::IsInputFieldFocused() {
+  TextInputType type = GetTextInputType();
+  return (type != TEXT_INPUT_TYPE_NONE) && (type != TEXT_INPUT_TYPE_PASSWORD);
+}
+
 }  // namespace ui