Home | History | Annotate | Download | only in ime
      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.
      4 
      5 #include "ui/base/ime/input_method_chromeos.h"
      6 
      7 #include <algorithm>
      8 #include <cstring>
      9 #include <set>
     10 #include <vector>
     11 
     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 "chromeos/ime/composition_text.h"
     21 #include "chromeos/ime/ime_keyboard.h"
     22 #include "chromeos/ime/input_method_manager.h"
     23 #include "ui/base/ime/text_input_client.h"
     24 #include "ui/events/event.h"
     25 #include "ui/gfx/rect.h"
     26 
     27 namespace {
     28 chromeos::IMEEngineHandlerInterface* GetEngine() {
     29   return chromeos::IMEBridge::Get()->GetCurrentEngineHandler();
     30 }
     31 }  // namespace
     32 
     33 namespace ui {
     34 
     35 // InputMethodChromeOS implementation -----------------------------------------
     36 InputMethodChromeOS::InputMethodChromeOS(
     37     internal::InputMethodDelegate* delegate)
     38     : composing_text_(false),
     39       composition_changed_(false),
     40       current_keyevent_id_(0),
     41       weak_ptr_factory_(this) {
     42   SetDelegate(delegate);
     43   chromeos::IMEBridge::Get()->SetInputContextHandler(this);
     44 
     45   UpdateContextFocusState();
     46 }
     47 
     48 InputMethodChromeOS::~InputMethodChromeOS() {
     49   AbandonAllPendingKeyEvents();
     50   ConfirmCompositionText();
     51   // We are dead, so we need to ask the client to stop relying on us.
     52   OnInputMethodChanged();
     53 
     54   chromeos::IMEBridge::Get()->SetInputContextHandler(NULL);
     55 }
     56 
     57 void InputMethodChromeOS::OnFocus() {
     58   InputMethodBase::OnFocus();
     59   OnTextInputTypeChanged(GetTextInputClient());
     60 }
     61 
     62 void InputMethodChromeOS::OnBlur() {
     63   ConfirmCompositionText();
     64   InputMethodBase::OnBlur();
     65   OnTextInputTypeChanged(GetTextInputClient());
     66 }
     67 
     68 bool InputMethodChromeOS::OnUntranslatedIMEMessage(
     69     const base::NativeEvent& event,
     70     NativeEventResult* result) {
     71   return false;
     72 }
     73 
     74 void InputMethodChromeOS::ProcessKeyEventDone(uint32 id,
     75                                               ui::KeyEvent* event,
     76                                               bool is_handled) {
     77   if (pending_key_events_.find(id) == pending_key_events_.end())
     78    return;  // Abandoned key event.
     79 
     80   DCHECK(event);
     81   if (event->type() == ET_KEY_PRESSED) {
     82     if (is_handled) {
     83       // IME event has a priority to be handled, so that character composer
     84       // should be reset.
     85       character_composer_.Reset();
     86     } else {
     87       // If IME does not handle key event, passes keyevent to character composer
     88       // to be able to compose complex characters.
     89       is_handled = ExecuteCharacterComposer(*event);
     90     }
     91   }
     92 
     93   if (event->type() == ET_KEY_PRESSED || event->type() == ET_KEY_RELEASED)
     94     ProcessKeyEventPostIME(*event, is_handled);
     95 
     96   // ProcessKeyEventPostIME may change the |pending_key_events_|.
     97   pending_key_events_.erase(id);
     98 }
     99 
    100 bool InputMethodChromeOS::DispatchKeyEvent(const ui::KeyEvent& event) {
    101   DCHECK(event.type() == ET_KEY_PRESSED || event.type() == ET_KEY_RELEASED);
    102   DCHECK(system_toplevel_window_focused());
    103 
    104   // For linux_chromeos, the ime keyboard cannot track the caps lock state by
    105   // itself, so need to call SetCapsLockEnabled() method to reflect the caps
    106   // lock state by the key event.
    107   if (!base::SysInfo::IsRunningOnChromeOS()) {
    108     chromeos::input_method::InputMethodManager* manager =
    109         chromeos::input_method::InputMethodManager::Get();
    110     if (manager) {
    111       chromeos::input_method::ImeKeyboard* keyboard = manager->GetImeKeyboard();
    112       if (keyboard && event.type() == ui::ET_KEY_PRESSED) {
    113         bool caps = (event.key_code() == ui::VKEY_CAPITAL)
    114             ? !keyboard->CapsLockIsEnabled()
    115             : (event.flags() & EF_CAPS_LOCK_DOWN);
    116         keyboard->SetCapsLockEnabled(caps);
    117       }
    118     }
    119   }
    120 
    121   // If |context_| is not usable, then we can only dispatch the key event as is.
    122   // We only dispatch the key event to input method when the |context_| is an
    123   // normal input field (not a password field).
    124   // Note: We need to send the key event to ibus even if the |context_| is not
    125   // enabled, so that ibus can have a chance to enable the |context_|.
    126   if (!IsInputFieldFocused() || !GetEngine()) {
    127     if (event.type() == ET_KEY_PRESSED) {
    128       if (ExecuteCharacterComposer(event)) {
    129         // Treating as PostIME event if character composer handles key event and
    130         // generates some IME event,
    131         ProcessKeyEventPostIME(event, true);
    132         return true;
    133       }
    134       ProcessUnfilteredKeyPressEvent(event);
    135     } else {
    136       DispatchKeyEventPostIME(event);
    137     }
    138     return true;
    139   }
    140 
    141   pending_key_events_.insert(current_keyevent_id_);
    142 
    143   ui::KeyEvent* copied_event = new ui::KeyEvent(event);
    144   GetEngine()->ProcessKeyEvent(
    145       event,
    146       base::Bind(&InputMethodChromeOS::ProcessKeyEventDone,
    147                  weak_ptr_factory_.GetWeakPtr(),
    148                  current_keyevent_id_,
    149                  // Pass the ownership of |copied_event|.
    150                  base::Owned(copied_event)));
    151 
    152   ++current_keyevent_id_;
    153   return true;
    154 }
    155 
    156 void InputMethodChromeOS::OnTextInputTypeChanged(
    157     const TextInputClient* client) {
    158   if (!IsTextInputClientFocused(client))
    159     return;
    160 
    161   UpdateContextFocusState();
    162 
    163   chromeos::IMEEngineHandlerInterface* engine = GetEngine();
    164   if (engine) {
    165     // When focused input client is not changed, a text input type change should
    166     // cause blur/focus events to engine.
    167     // The focus in to or out from password field should also notify engine.
    168     engine->FocusOut();
    169     chromeos::IMEEngineHandlerInterface::InputContext context(
    170         GetTextInputType(), GetTextInputMode());
    171     engine->FocusIn(context);
    172   }
    173 
    174   InputMethodBase::OnTextInputTypeChanged(client);
    175 }
    176 
    177 void InputMethodChromeOS::OnCaretBoundsChanged(const TextInputClient* client) {
    178   if (!IsInputFieldFocused() || !IsTextInputClientFocused(client))
    179     return;
    180 
    181   // The current text input type should not be NONE if |context_| is focused.
    182   DCHECK(!IsTextInputTypeNone());
    183   const gfx::Rect rect = GetTextInputClient()->GetCaretBounds();
    184 
    185   gfx::Rect composition_head;
    186   if (!GetTextInputClient()->GetCompositionCharacterBounds(0,
    187                                                            &composition_head)) {
    188     composition_head = rect;
    189   }
    190 
    191   chromeos::IMECandidateWindowHandlerInterface* candidate_window =
    192       chromeos::IMEBridge::Get()->GetCandidateWindowHandler();
    193   if (!candidate_window)
    194     return;
    195   candidate_window->SetCursorBounds(rect, composition_head);
    196 
    197   gfx::Range text_range;
    198   gfx::Range selection_range;
    199   base::string16 surrounding_text;
    200   if (!GetTextInputClient()->GetTextRange(&text_range) ||
    201       !GetTextInputClient()->GetTextFromRange(text_range, &surrounding_text) ||
    202       !GetTextInputClient()->GetSelectionRange(&selection_range)) {
    203     previous_surrounding_text_.clear();
    204     previous_selection_range_ = gfx::Range::InvalidRange();
    205     return;
    206   }
    207 
    208   if (previous_selection_range_ == selection_range &&
    209       previous_surrounding_text_ == surrounding_text)
    210     return;
    211 
    212   previous_selection_range_ = selection_range;
    213   previous_surrounding_text_ = surrounding_text;
    214 
    215   if (!selection_range.IsValid()) {
    216     // TODO(nona): Ideally selection_range should not be invalid.
    217     // TODO(nona): If javascript changes the focus on page loading, even (0,0)
    218     //             can not be obtained. Need investigation.
    219     return;
    220   }
    221 
    222   // Here SetSurroundingText accepts relative position of |surrounding_text|, so
    223   // we have to convert |selection_range| from node coordinates to
    224   // |surrounding_text| coordinates.
    225   if (!GetEngine())
    226     return;
    227   GetEngine()->SetSurroundingText(base::UTF16ToUTF8(surrounding_text),
    228                                   selection_range.start() - text_range.start(),
    229                                   selection_range.end() - text_range.start());
    230 }
    231 
    232 void InputMethodChromeOS::CancelComposition(const TextInputClient* client) {
    233   if (IsInputFieldFocused() && IsTextInputClientFocused(client))
    234     ResetContext();
    235 }
    236 
    237 void InputMethodChromeOS::OnInputLocaleChanged() {
    238   // Not supported.
    239 }
    240 
    241 std::string InputMethodChromeOS::GetInputLocale() {
    242   // Not supported.
    243   return "";
    244 }
    245 
    246 bool InputMethodChromeOS::IsActive() {
    247   return true;
    248 }
    249 
    250 bool InputMethodChromeOS::IsCandidatePopupOpen() const {
    251   // TODO(yukishiino): Implement this method.
    252   return false;
    253 }
    254 
    255 void InputMethodChromeOS::OnWillChangeFocusedClient(
    256     TextInputClient* focused_before,
    257     TextInputClient* focused) {
    258   ConfirmCompositionText();
    259 
    260   if (GetEngine())
    261     GetEngine()->FocusOut();
    262 }
    263 
    264 void InputMethodChromeOS::OnDidChangeFocusedClient(
    265     TextInputClient* focused_before,
    266     TextInputClient* focused) {
    267   // Force to update the input type since client's TextInputStateChanged()
    268   // function might not be called if text input types before the client loses
    269   // focus and after it acquires focus again are the same.
    270   UpdateContextFocusState();
    271 
    272   if (GetEngine()) {
    273     chromeos::IMEEngineHandlerInterface::InputContext context(
    274         GetTextInputType(), GetTextInputMode());
    275     GetEngine()->FocusIn(context);
    276   }
    277 }
    278 
    279 void InputMethodChromeOS::ConfirmCompositionText() {
    280   TextInputClient* client = GetTextInputClient();
    281   if (client && client->HasCompositionText())
    282     client->ConfirmCompositionText();
    283 
    284   ResetContext();
    285 }
    286 
    287 void InputMethodChromeOS::ResetContext() {
    288   if (!IsInputFieldFocused() || !GetTextInputClient())
    289     return;
    290 
    291   DCHECK(system_toplevel_window_focused());
    292 
    293   composition_.Clear();
    294   result_text_.clear();
    295   composing_text_ = false;
    296   composition_changed_ = false;
    297 
    298   // We need to abandon all pending key events, but as above comment says, there
    299   // is no reliable way to abandon all results generated by these abandoned key
    300   // events.
    301   AbandonAllPendingKeyEvents();
    302 
    303   // This function runs asynchronously.
    304   // Note: some input method engines may not support reset method, such as
    305   // ibus-anthy. But as we control all input method engines by ourselves, we can
    306   // make sure that all of the engines we are using support it correctly.
    307   if (GetEngine())
    308     GetEngine()->Reset();
    309 
    310   character_composer_.Reset();
    311 }
    312 
    313 void InputMethodChromeOS::UpdateContextFocusState() {
    314   ResetContext();
    315   OnInputMethodChanged();
    316 
    317   // Propagate the focus event to the candidate window handler which also
    318   // manages the input method mode indicator.
    319   chromeos::IMECandidateWindowHandlerInterface* candidate_window =
    320       chromeos::IMEBridge::Get()->GetCandidateWindowHandler();
    321   if (candidate_window)
    322     candidate_window->FocusStateChanged(IsInputFieldFocused());
    323 
    324   chromeos::IMEBridge::Get()->SetCurrentTextInputType(GetTextInputType());
    325 
    326   if (!IsTextInputTypeNone())
    327     OnCaretBoundsChanged(GetTextInputClient());
    328 }
    329 
    330 void InputMethodChromeOS::ProcessKeyEventPostIME(
    331     const ui::KeyEvent& event,
    332     bool handled) {
    333   TextInputClient* client = GetTextInputClient();
    334   if (!client) {
    335     // As ibus works asynchronously, there is a chance that the focused client
    336     // loses focus before this method gets called.
    337     DispatchKeyEventPostIME(event);
    338     return;
    339   }
    340 
    341   if (event.type() == ET_KEY_PRESSED && handled)
    342     ProcessFilteredKeyPressEvent(event);
    343 
    344   // In case the focus was changed by the key event. The |context_| should have
    345   // been reset when the focused window changed.
    346   if (client != GetTextInputClient())
    347     return;
    348 
    349   if (HasInputMethodResult())
    350     ProcessInputMethodResult(event, handled);
    351 
    352   // In case the focus was changed when sending input method results to the
    353   // focused window.
    354   if (client != GetTextInputClient())
    355     return;
    356 
    357   if (event.type() == ET_KEY_PRESSED && !handled)
    358     ProcessUnfilteredKeyPressEvent(event);
    359   else if (event.type() == ET_KEY_RELEASED)
    360     DispatchKeyEventPostIME(event);
    361 }
    362 
    363 void InputMethodChromeOS::ProcessFilteredKeyPressEvent(
    364     const ui::KeyEvent& event) {
    365   if (NeedInsertChar()) {
    366     DispatchKeyEventPostIME(event);
    367   } else {
    368     const ui::KeyEvent fabricated_event(ET_KEY_PRESSED,
    369                                         VKEY_PROCESSKEY,
    370                                         event.flags());
    371     DispatchKeyEventPostIME(fabricated_event);
    372   }
    373 }
    374 
    375 void InputMethodChromeOS::ProcessUnfilteredKeyPressEvent(
    376     const ui::KeyEvent& event) {
    377   const TextInputClient* prev_client = GetTextInputClient();
    378   DispatchKeyEventPostIME(event);
    379 
    380   // We shouldn't dispatch the character anymore if the key event dispatch
    381   // caused focus change. For example, in the following scenario,
    382   // 1. visit a web page which has a <textarea>.
    383   // 2. click Omnibox.
    384   // 3. enable Korean IME, press A, then press Tab to move the focus to the web
    385   //    page.
    386   // We should return here not to send the Tab key event to RWHV.
    387   TextInputClient* client = GetTextInputClient();
    388   if (!client || client != prev_client)
    389     return;
    390 
    391   // If a key event was not filtered by |context_| and |character_composer_|,
    392   // then it means the key event didn't generate any result text. So we need
    393   // to send corresponding character to the focused text input client.
    394   uint16 ch = event.GetCharacter();
    395   if (ch)
    396     client->InsertChar(ch, event.flags());
    397 }
    398 
    399 void InputMethodChromeOS::ProcessInputMethodResult(const ui::KeyEvent& event,
    400                                                bool handled) {
    401   TextInputClient* client = GetTextInputClient();
    402   DCHECK(client);
    403 
    404   if (result_text_.length()) {
    405     if (handled && NeedInsertChar()) {
    406       for (base::string16::const_iterator i = result_text_.begin();
    407            i != result_text_.end(); ++i) {
    408         client->InsertChar(*i, event.flags());
    409       }
    410     } else {
    411       client->InsertText(result_text_);
    412       composing_text_ = false;
    413     }
    414   }
    415 
    416   if (composition_changed_ && !IsTextInputTypeNone()) {
    417     if (composition_.text.length()) {
    418       composing_text_ = true;
    419       client->SetCompositionText(composition_);
    420     } else if (result_text_.empty()) {
    421       client->ClearCompositionText();
    422     }
    423   }
    424 
    425   // We should not clear composition text here, as it may belong to the next
    426   // composition session.
    427   result_text_.clear();
    428   composition_changed_ = false;
    429 }
    430 
    431 bool InputMethodChromeOS::NeedInsertChar() const {
    432   return GetTextInputClient() &&
    433       (IsTextInputTypeNone() ||
    434        (!composing_text_ && result_text_.length() == 1));
    435 }
    436 
    437 bool InputMethodChromeOS::HasInputMethodResult() const {
    438   return result_text_.length() || composition_changed_;
    439 }
    440 
    441 void InputMethodChromeOS::SendFakeProcessKeyEvent(bool pressed) const {
    442   if (!GetTextInputClient())
    443     return;
    444   KeyEvent evt(pressed ? ET_KEY_PRESSED : ET_KEY_RELEASED,
    445                pressed ? VKEY_PROCESSKEY : VKEY_UNKNOWN,
    446                EF_IME_FABRICATED_KEY);
    447   DispatchKeyEventPostIME(evt);
    448 }
    449 
    450 void InputMethodChromeOS::AbandonAllPendingKeyEvents() {
    451   pending_key_events_.clear();
    452 }
    453 
    454 void InputMethodChromeOS::CommitText(const std::string& text) {
    455   if (text.empty())
    456     return;
    457 
    458   // We need to receive input method result even if the text input type is
    459   // TEXT_INPUT_TYPE_NONE, to make sure we can always send correct
    460   // character for each key event to the focused text input client.
    461   if (!GetTextInputClient())
    462     return;
    463 
    464   const base::string16 utf16_text = base::UTF8ToUTF16(text);
    465   if (utf16_text.empty())
    466     return;
    467 
    468   // Append the text to the buffer, because commit signal might be fired
    469   // multiple times when processing a key event.
    470   result_text_.append(utf16_text);
    471 
    472   // If we are not handling key event, do not bother sending text result if the
    473   // focused text input client does not support text input.
    474   if (pending_key_events_.empty() && !IsTextInputTypeNone()) {
    475     SendFakeProcessKeyEvent(true);
    476     GetTextInputClient()->InsertText(utf16_text);
    477     SendFakeProcessKeyEvent(false);
    478     result_text_.clear();
    479   }
    480 }
    481 
    482 void InputMethodChromeOS::UpdateCompositionText(
    483     const chromeos::CompositionText& text,
    484     uint32 cursor_pos,
    485     bool visible) {
    486   if (IsTextInputTypeNone())
    487     return;
    488 
    489   if (!CanComposeInline()) {
    490     chromeos::IMECandidateWindowHandlerInterface* candidate_window =
    491         chromeos::IMEBridge::Get()->GetCandidateWindowHandler();
    492     if (candidate_window)
    493       candidate_window->UpdatePreeditText(text.text(), cursor_pos, visible);
    494   }
    495 
    496   // |visible| argument is very confusing. For example, what's the correct
    497   // behavior when:
    498   // 1. OnUpdatePreeditText() is called with a text and visible == false, then
    499   // 2. OnShowPreeditText() is called afterwards.
    500   //
    501   // If it's only for clearing the current preedit text, then why not just use
    502   // OnHidePreeditText()?
    503   if (!visible) {
    504     HidePreeditText();
    505     return;
    506   }
    507 
    508   ExtractCompositionText(text, cursor_pos, &composition_);
    509 
    510   composition_changed_ = true;
    511 
    512   // In case OnShowPreeditText() is not called.
    513   if (composition_.text.length())
    514     composing_text_ = true;
    515 
    516   // If we receive a composition text without pending key event, then we need to
    517   // send it to the focused text input client directly.
    518   if (pending_key_events_.empty()) {
    519     SendFakeProcessKeyEvent(true);
    520     GetTextInputClient()->SetCompositionText(composition_);
    521     SendFakeProcessKeyEvent(false);
    522     composition_changed_ = false;
    523     composition_.Clear();
    524   }
    525 }
    526 
    527 void InputMethodChromeOS::HidePreeditText() {
    528   if (composition_.text.empty() || IsTextInputTypeNone())
    529     return;
    530 
    531   // Intentionally leaves |composing_text_| unchanged.
    532   composition_changed_ = true;
    533   composition_.Clear();
    534 
    535   if (pending_key_events_.empty()) {
    536     TextInputClient* client = GetTextInputClient();
    537     if (client && client->HasCompositionText()) {
    538       SendFakeProcessKeyEvent(true);
    539       client->ClearCompositionText();
    540       SendFakeProcessKeyEvent(false);
    541     }
    542     composition_changed_ = false;
    543   }
    544 }
    545 
    546 void InputMethodChromeOS::DeleteSurroundingText(int32 offset, uint32 length) {
    547   if (!composition_.text.empty())
    548     return;  // do nothing if there is ongoing composition.
    549   if (offset < 0 && static_cast<uint32>(-1 * offset) != length)
    550     return;  // only preceding text can be deletable.
    551   if (GetTextInputClient())
    552     GetTextInputClient()->ExtendSelectionAndDelete(length, 0U);
    553 }
    554 
    555 bool InputMethodChromeOS::ExecuteCharacterComposer(const ui::KeyEvent& event) {
    556   if (!character_composer_.FilterKeyPress(event))
    557     return false;
    558 
    559   // CharacterComposer consumed the key event.  Update the composition text.
    560   chromeos::CompositionText preedit;
    561   preedit.set_text(character_composer_.preedit_string());
    562   UpdateCompositionText(preedit, preedit.text().size(),
    563                         !preedit.text().empty());
    564   std::string commit_text =
    565       base::UTF16ToUTF8(character_composer_.composed_character());
    566   if (!commit_text.empty()) {
    567     CommitText(commit_text);
    568   }
    569   return true;
    570 }
    571 
    572 void InputMethodChromeOS::ExtractCompositionText(
    573     const chromeos::CompositionText& text,
    574     uint32 cursor_position,
    575     CompositionText* out_composition) const {
    576   out_composition->Clear();
    577   out_composition->text = text.text();
    578 
    579   if (out_composition->text.empty())
    580     return;
    581 
    582   // ibus uses character index for cursor position and attribute range, but we
    583   // use char16 offset for them. So we need to do conversion here.
    584   std::vector<size_t> char16_offsets;
    585   size_t length = out_composition->text.length();
    586   base::i18n::UTF16CharIterator char_iterator(&out_composition->text);
    587   do {
    588     char16_offsets.push_back(char_iterator.array_pos());
    589   } while (char_iterator.Advance());
    590 
    591   // The text length in Unicode characters.
    592   uint32 char_length = static_cast<uint32>(char16_offsets.size());
    593   // Make sure we can convert the value of |char_length| as well.
    594   char16_offsets.push_back(length);
    595 
    596   size_t cursor_offset =
    597       char16_offsets[std::min(char_length, cursor_position)];
    598 
    599   out_composition->selection = gfx::Range(cursor_offset);
    600 
    601   const std::vector<chromeos::CompositionText::UnderlineAttribute>&
    602       underline_attributes = text.underline_attributes();
    603   if (!underline_attributes.empty()) {
    604     for (size_t i = 0; i < underline_attributes.size(); ++i) {
    605       const uint32 start = underline_attributes[i].start_index;
    606       const uint32 end = underline_attributes[i].end_index;
    607       if (start >= end)
    608         continue;
    609       CompositionUnderline underline(char16_offsets[start],
    610                                      char16_offsets[end],
    611                                      SK_ColorBLACK,
    612                                      false /* thick */,
    613                                      SK_ColorTRANSPARENT);
    614       if (underline_attributes[i].type ==
    615           chromeos::CompositionText::COMPOSITION_TEXT_UNDERLINE_DOUBLE)
    616         underline.thick = true;
    617       else if (underline_attributes[i].type ==
    618                chromeos::CompositionText::COMPOSITION_TEXT_UNDERLINE_ERROR)
    619         underline.color = SK_ColorRED;
    620       out_composition->underlines.push_back(underline);
    621     }
    622   }
    623 
    624   DCHECK(text.selection_start() <= text.selection_end());
    625   if (text.selection_start() < text.selection_end()) {
    626     const uint32 start = text.selection_start();
    627     const uint32 end = text.selection_end();
    628     CompositionUnderline underline(char16_offsets[start],
    629                                    char16_offsets[end],
    630                                    SK_ColorBLACK,
    631                                    true /* thick */,
    632                                    SK_ColorTRANSPARENT);
    633     out_composition->underlines.push_back(underline);
    634 
    635     // If the cursor is at start or end of this underline, then we treat
    636     // it as the selection range as well, but make sure to set the cursor
    637     // position to the selection end.
    638     if (underline.start_offset == cursor_offset) {
    639       out_composition->selection.set_start(underline.end_offset);
    640       out_composition->selection.set_end(cursor_offset);
    641     } else if (underline.end_offset == cursor_offset) {
    642       out_composition->selection.set_start(underline.start_offset);
    643       out_composition->selection.set_end(cursor_offset);
    644     }
    645   }
    646 
    647   // Use a black thin underline by default.
    648   if (out_composition->underlines.empty()) {
    649     out_composition->underlines.push_back(CompositionUnderline(
    650         0, length, SK_ColorBLACK, false /* thick */, SK_ColorTRANSPARENT));
    651   }
    652 }
    653 
    654 bool InputMethodChromeOS::IsInputFieldFocused() {
    655   TextInputType type = GetTextInputType();
    656   return (type != TEXT_INPUT_TYPE_NONE) && (type != TEXT_INPUT_TYPE_PASSWORD);
    657 }
    658 
    659 }  // namespace ui
    660