Add InputMethodObserver support into InputMethodBase

This is a preparation CL to fix issue 164964.

Following observer callbacks are defined but not yet
supported by ui::InputMethodBase and its sub classes.
- InputMethodObserver::OnCaretBoundsChanged
- InputMethodObserver::OnInputLocaleChanged

This CL makes these callbacks functional for each
sub class of ui::InputMethodBase.

BUG=164964
TEST=ui_unittests --gtest_filter=InputMethodBaseTest.*

Review URL: https://codereview.chromium.org/48393003

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@231563 0039d316-1c4b-4281-b951-d872f2087c98
bool InputMethodLinuxX11::DispatchKeyEvent(
    const base::NativeEvent& native_key_event) {
  EventType event_type = EventTypeFromNative(native_key_event);
  DCHECK(event_type == ET_KEY_PRESSED || event_type == ET_KEY_RELEASED);
  DCHECK(system_toplevel_window_focused());

  if (!GetTextInputClient())
    return DispatchKeyEventPostIME(native_key_event);

  if (input_method_context_->DispatchKeyEvent(native_key_event)) {
    if (event_type == ET_KEY_PRESSED)
      DispatchFabricatedKeyEventPostIME(ET_KEY_PRESSED, VKEY_PROCESSKEY,
                                        EventFlagsFromNative(native_key_event));
    return true;
  }

  const bool handled = DispatchKeyEventPostIME(native_key_event);
  if (event_type == ET_KEY_PRESSED && GetTextInputClient()) {
    uint16 ch = 0;
    const int flags = EventFlagsFromNative(native_key_event);
    if (!(flags & EF_CONTROL_DOWN))
      ch = GetCharacterFromXEvent(native_key_event);
    if (!ch)
      ch = GetCharacterFromKeyCode(KeyboardCodeFromNative(native_key_event),
                                   flags);
    if (ch) {
      GetTextInputClient()->InsertChar(ch, flags);
      return true;
    }
  }
  return handled;
}

bool InputMethodLinuxX11::DispatchKeyEvent(
    const base::NativeEvent& native_key_event) {
  EventType event_type = EventTypeFromNative(native_key_event);
  DCHECK(event_type == ET_KEY_PRESSED || event_type == ET_KEY_RELEASED);
  DCHECK(system_toplevel_window_focused());

  if (!GetTextInputClient())
    return DispatchKeyEventPostIME(native_key_event);

  if (input_method_context_->DispatchKeyEvent(native_key_event)) {
    if (event_type == ET_KEY_PRESSED)
      DispatchFabricatedKeyEventPostIME(ET_KEY_PRESSED, VKEY_PROCESSKEY,
                                        EventFlagsFromNative(native_key_event));
    return true;
  }

  const bool handled = DispatchKeyEventPostIME(native_key_event);
  if (event_type == ET_KEY_PRESSED && GetTextInputClient()) {
    uint16 ch = 0;
    const int flags = EventFlagsFromNative(native_key_event);
    if (!(flags & EF_CONTROL_DOWN))
      ch = GetCharacterFromXEvent(native_key_event);
    if (!ch)
      ch = GetCharacterFromKeyCode(KeyboardCodeFromNative(native_key_event),
                                   flags);
    if (ch) {
      GetTextInputClient()->InsertChar(ch, flags);
      return true;
    }
  }
  return handled;
}
