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                                         false);  // is_char
    372     DispatchKeyEventPostIME(fabricated_event);
    373   }
    374 }
    375 
    376 void InputMethodChromeOS::ProcessUnfilteredKeyPressEvent(
    377     const ui::KeyEvent& event) {
    378   const TextInputClient* prev_client = GetTextInputClient();
    379   DispatchKeyEventPostIME(event);
    380 
    381   // We shouldn't dispatch the character anymore if the key event dispatch
    382   // caused focus change. For example, in the following scenario,
    383   // 1. visit a web page which has a <textarea>.
    384   // 2. click Omnibox.
    385   // 3. enable Korean IME, press A, then press Tab to move the focus to the web
    386   //    page.
    387   // We should return here not to send the Tab key event to RWHV.
    388   TextInputClient* client = GetTextInputClient();
    389   if (!client || client != prev_client)
    390     return;
    391 
    392   // If a key event was not filtered by |context_| and |character_composer_|,
    393   // then it means the key event didn't generate any result text. So we need
    394   // to send corresponding character to the focused text input client.
    395   uint16 ch = event.GetCharacter();
    396   if (ch)
    397     client->InsertChar(ch, event.flags());
    398 }
    399 
    400 void InputMethodChromeOS::ProcessInputMethodResult(const ui::KeyEvent& event,
    401                                                bool handled) {
    402   TextInputClient* client = GetTextInputClient();
    403   DCHECK(client);
    404 
    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());
    410       }
    411     } else {
    412       client->InsertText(result_text_);
    413       composing_text_ = false;
    414     }
    415   }
    416 
    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();
    423     }
    424   }
    425 
    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;
    430 }
    431 
    432 bool InputMethodChromeOS::NeedInsertChar() const {
    433   return GetTextInputClient() &&
    434       (IsTextInputTypeNone() ||
    435        (!composing_text_ && result_text_.length() == 1));
    436 }
    437 
    438 bool InputMethodChromeOS::HasInputMethodResult() const {
    439   return result_text_.length() || composition_changed_;
    440 }
    441 
    442 void InputMethodChromeOS::SendFakeProcessKeyEvent(bool pressed) const {
    443   if (!GetTextInputClient())
    444     return;
    445   KeyEvent evt(pressed ? ET_KEY_PRESSED : ET_KEY_RELEASED,
    446                pressed ? VKEY_PROCESSKEY : VKEY_UNKNOWN,
    447                EF_IME_FABRICATED_KEY,
    448                false);  // is_char
    449   DispatchKeyEventPostIME(evt);
    450 }
    451 
    452 void InputMethodChromeOS::AbandonAllPendingKeyEvents() {
    453   pending_key_events_.clear();
    454 }
    455 
    456 void InputMethodChromeOS::CommitText(const std::string& text) {
    457   if (text.empty())
    458     return;
    459 
    460   // We need to receive input method result even if the text input type is
    461   // TEXT_INPUT_TYPE_NONE, to make sure we can always send correct
    462   // character for each key event to the focused text input client.
    463   if (!GetTextInputClient())
    464     return;
    465 
    466   const base::string16 utf16_text = base::UTF8ToUTF16(text);
    467   if (utf16_text.empty())
    468     return;
    469 
    470   // Append the text to the buffer, because commit signal might be fired
    471   // multiple times when processing a key event.
    472   result_text_.append(utf16_text);
    473 
    474   // If we are not handling key event, do not bother sending text result if the
    475   // focused text input client does not support text input.
    476   if (pending_key_events_.empty() && !IsTextInputTypeNone()) {
    477     SendFakeProcessKeyEvent(true);
    478     GetTextInputClient()->InsertText(utf16_text);
    479     SendFakeProcessKeyEvent(false);
    480     result_text_.clear();
    481   }
    482 }
    483 
    484 void InputMethodChromeOS::UpdateCompositionText(
    485     const chromeos::CompositionText& text,
    486     uint32 cursor_pos,
    487     bool visible) {
    488   if (IsTextInputTypeNone())
    489     return;
    490 
    491   if (!CanComposeInline()) {
    492     chromeos::IMECandidateWindowHandlerInterface* candidate_window =
    493         chromeos::IMEBridge::Get()->GetCandidateWindowHandler();
    494     if (candidate_window)
    495       candidate_window->UpdatePreeditText(text.text(), cursor_pos, visible);
    496   }
    497 
    498   // |visible| argument is very confusing. For example, what's the correct
    499   // behavior when:
    500   // 1. OnUpdatePreeditText() is called with a text and visible == false, then
    501   // 2. OnShowPreeditText() is called afterwards.
    502   //
    503   // If it's only for clearing the current preedit text, then why not just use
    504   // OnHidePreeditText()?
    505   if (!visible) {
    506     HidePreeditText();
    507     return;
    508   }
    509 
    510   ExtractCompositionText(text, cursor_pos, &composition_);
    511 
    512   composition_changed_ = true;
    513 
    514   // In case OnShowPreeditText() is not called.
    515   if (composition_.text.length())
    516     composing_text_ = true;
    517 
    518   // If we receive a composition text without pending key event, then we need to
    519   // send it to the focused text input client directly.
    520   if (pending_key_events_.empty()) {
    521     SendFakeProcessKeyEvent(true);
    522     GetTextInputClient()->SetCompositionText(composition_);
    523     SendFakeProcessKeyEvent(false);
    524     composition_changed_ = false;
    525     composition_.Clear();
    526   }
    527 }
    528 
    529 void InputMethodChromeOS::HidePreeditText() {
    530   if (composition_.text.empty() || IsTextInputTypeNone())
    531     return;
    532 
    533   // Intentionally leaves |composing_text_| unchanged.
    534   composition_changed_ = true;
    535   composition_.Clear();
    536 
    537   if (pending_key_events_.empty()) {
    538     TextInputClient* client = GetTextInputClient();
    539     if (client && client->HasCompositionText()) {
    540       SendFakeProcessKeyEvent(true);
    541       client->ClearCompositionText();
    542       SendFakeProcessKeyEvent(false);
    543     }
    544     composition_changed_ = false;
    545   }
    546 }
    547 
    548 void InputMethodChromeOS::DeleteSurroundingText(int32 offset, uint32 length) {
    549   if (!composition_.text.empty())
    550     return;  // do nothing if there is ongoing composition.
    551   if (offset < 0 && static_cast<uint32>(-1 * offset) != length)
    552     return;  // only preceding text can be deletable.
    553   if (GetTextInputClient())
    554     GetTextInputClient()->ExtendSelectionAndDelete(length, 0U);
    555 }
    556 
    557 bool InputMethodChromeOS::ExecuteCharacterComposer(const ui::KeyEvent& event) {
    558   if (!character_composer_.FilterKeyPress(event))
    559     return false;
    560 
    561   // CharacterComposer consumed the key event.  Update the composition text.
    562   chromeos::CompositionText preedit;
    563   preedit.set_text(character_composer_.preedit_string());
    564   UpdateCompositionText(preedit, preedit.text().size(),
    565                         !preedit.text().empty());
    566   std::string commit_text =
    567       base::UTF16ToUTF8(character_composer_.composed_character());
    568   if (!commit_text.empty()) {
    569     CommitText(commit_text);
    570   }
    571   return true;
    572 }
    573 
    574 void InputMethodChromeOS::ExtractCompositionText(
    575     const chromeos::CompositionText& text,
    576     uint32 cursor_position,
    577     CompositionText* out_composition) const {
    578   out_composition->Clear();
    579   out_composition->text = text.text();
    580 
    581   if (out_composition->text.empty())
    582     return;
    583 
    584   // ibus uses character index for cursor position and attribute range, but we
    585   // use char16 offset for them. So we need to do conversion here.
    586   std::vector<size_t> char16_offsets;
    587   size_t length = out_composition->text.length();
    588   base::i18n::UTF16CharIterator char_iterator(&out_composition->text);
    589   do {
    590     char16_offsets.push_back(char_iterator.array_pos());
    591   } while (char_iterator.Advance());
    592 
    593   // The text length in Unicode characters.
    594   uint32 char_length = static_cast<uint32>(char16_offsets.size());
    595   // Make sure we can convert the value of |char_length| as well.
    596   char16_offsets.push_back(length);
    597 
    598   size_t cursor_offset =
    599       char16_offsets[std::min(char_length, cursor_position)];
    600 
    601   out_composition->selection = gfx::Range(cursor_offset);
    602 
    603   const std::vector<chromeos::CompositionText::UnderlineAttribute>&
    604       underline_attributes = text.underline_attributes();
    605   if (!underline_attributes.empty()) {
    606     for (size_t i = 0; i < underline_attributes.size(); ++i) {
    607       const uint32 start = underline_attributes[i].start_index;
    608       const uint32 end = underline_attributes[i].end_index;
    609       if (start >= end)
    610         continue;
    611       CompositionUnderline underline(char16_offsets[start],
    612                                      char16_offsets[end],
    613                                      SK_ColorBLACK,
    614                                      false /* thick */,
    615                                      SK_ColorTRANSPARENT);
    616       if (underline_attributes[i].type ==
    617           chromeos::CompositionText::COMPOSITION_TEXT_UNDERLINE_DOUBLE)
    618         underline.thick = true;
    619       else if (underline_attributes[i].type ==
    620                chromeos::CompositionText::COMPOSITION_TEXT_UNDERLINE_ERROR)
    621         underline.color = SK_ColorRED;
    622       out_composition->underlines.push_back(underline);
    623     }
    624   }
    625 
    626   DCHECK(text.selection_start() <= text.selection_end());
    627   if (text.selection_start() < text.selection_end()) {
    628     const uint32 start = text.selection_start();
    629     const uint32 end = text.selection_end();
    630     CompositionUnderline underline(char16_offsets[start],
    631                                    char16_offsets[end],
    632                                    SK_ColorBLACK,
    633                                    true /* thick */,
    634                                    SK_ColorTRANSPARENT);
    635     out_composition->underlines.push_back(underline);
    636 
    637     // If the cursor is at start or end of this underline, then we treat
    638     // it as the selection range as well, but make sure to set the cursor
    639     // position to the selection end.
    640     if (underline.start_offset == cursor_offset) {
    641       out_composition->selection.set_start(underline.end_offset);
    642       out_composition->selection.set_end(cursor_offset);
    643     } else if (underline.end_offset == cursor_offset) {
    644       out_composition->selection.set_start(underline.start_offset);
    645       out_composition->selection.set_end(cursor_offset);
    646     }
    647   }
    648 
    649   // Use a black thin underline by default.
    650   if (out_composition->underlines.empty()) {
    651     out_composition->underlines.push_back(CompositionUnderline(
    652         0, length, SK_ColorBLACK, false /* thick */, SK_ColorTRANSPARENT));
    653   }
    654 }
    655 
    656 bool InputMethodChromeOS::IsInputFieldFocused() {
    657   TextInputType type = GetTextInputType();
    658   return (type != TEXT_INPUT_TYPE_NONE) && (type != TEXT_INPUT_TYPE_PASSWORD);
    659 }
    660 
    661 }  // namespace ui
    662