Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / input_method / input_method_engine.cc
1 // Copyright 2013 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 "chrome/browser/chromeos/input_method/input_method_engine.h"
6
7 #define XK_MISCELLANY
8 #include <X11/keysymdef.h>
9 #include <X11/X.h>
10 #include <X11/Xlib.h>
11 #include <X11/Xutil.h>
12 #undef FocusIn
13 #undef FocusOut
14 #undef RootWindow
15 #include <map>
16
17 #include "ash/ime/input_method_menu_item.h"
18 #include "ash/ime/input_method_menu_manager.h"
19 #include "ash/shell.h"
20 #include "base/logging.h"
21 #include "base/memory/scoped_ptr.h"
22 #include "base/metrics/histogram.h"
23 #include "base/strings/string_number_conversions.h"
24 #include "base/strings/string_util.h"
25 #include "base/strings/stringprintf.h"
26 #include "base/strings/utf_string_conversions.h"
27 #include "chromeos/ime/component_extension_ime_manager.h"
28 #include "chromeos/ime/composition_text.h"
29 #include "chromeos/ime/extension_ime_util.h"
30 #include "chromeos/ime/input_method_manager.h"
31 #include "ui/aura/window.h"
32 #include "ui/aura/window_tree_host.h"
33 #include "ui/base/ime/candidate_window.h"
34 #include "ui/base/ime/chromeos/ime_keymap.h"
35 #include "ui/events/event.h"
36 #include "ui/events/event_processor.h"
37 #include "ui/events/keycodes/dom4/keycode_converter.h"
38 #include "ui/events/keycodes/keyboard_code_conversion_x.h"
39 #include "ui/keyboard/keyboard_controller.h"
40 #include "ui/keyboard/keyboard_util.h"
41
42 namespace chromeos {
43 const char* kErrorNotActive = "IME is not active";
44 const char* kErrorWrongContext = "Context is not active";
45 const char* kCandidateNotFound = "Candidate not found";
46
47 namespace {
48
49 // Notifies InputContextHandler that the composition is changed.
50 void UpdateComposition(const CompositionText& composition_text,
51                        uint32 cursor_pos,
52                        bool is_visible) {
53   IMEInputContextHandlerInterface* input_context =
54       IMEBridge::Get()->GetInputContextHandler();
55   if (input_context)
56     input_context->UpdateCompositionText(
57         composition_text, cursor_pos, is_visible);
58 }
59
60 // Returns the length of characters of a UTF-8 string with unknown string
61 // length. Cannot apply faster algorithm to count characters in an utf-8
62 // string without knowing the string length,  so just does a full scan.
63 size_t GetUtf8StringLength(const char* s) {
64   size_t ret = 0;
65   while (*s) {
66     if ((*s & 0xC0) != 0x80)
67       ret++;
68     ++s;
69   }
70   return ret;
71 }
72
73 }  // namespace
74
75 InputMethodEngine::InputMethodEngine()
76     : current_input_type_(ui::TEXT_INPUT_TYPE_NONE),
77       active_(false),
78       context_id_(0),
79       next_context_id_(1),
80       composition_text_(new CompositionText()),
81       composition_cursor_(0),
82       candidate_window_(new ui::CandidateWindow()),
83       window_visible_(false),
84       sent_key_event_(NULL) {}
85
86 InputMethodEngine::~InputMethodEngine() {
87   if (start_time_.ToInternalValue())
88     RecordHistogram("WorkingTime", (end_time_ - start_time_).InSeconds());
89   input_method::InputMethodManager::Get()->RemoveInputMethodExtension(imm_id_);
90 }
91
92 void InputMethodEngine::Initialize(
93     scoped_ptr<InputMethodEngineInterface::Observer> observer,
94     const char* engine_name,
95     const char* extension_id,
96     const char* engine_id,
97     const std::vector<std::string>& languages,
98     const std::vector<std::string>& layouts,
99     const GURL& options_page,
100     const GURL& input_view) {
101   DCHECK(observer) << "Observer must not be null.";
102
103   // TODO(komatsu): It is probably better to set observer out of Initialize.
104   observer_ = observer.Pass();
105   engine_id_ = engine_id;
106   extension_id_ = extension_id;
107
108   input_method::InputMethodManager* manager =
109       input_method::InputMethodManager::Get();
110   ComponentExtensionIMEManager* comp_ext_ime_manager =
111       manager->GetComponentExtensionIMEManager();
112
113   if (comp_ext_ime_manager && comp_ext_ime_manager->IsInitialized() &&
114       comp_ext_ime_manager->IsWhitelistedExtension(extension_id)) {
115     imm_id_ = comp_ext_ime_manager->GetId(extension_id, engine_id);
116   } else {
117     imm_id_ = extension_ime_util::GetInputMethodID(extension_id, engine_id);
118   }
119
120   input_view_url_ = input_view;
121   descriptor_ = input_method::InputMethodDescriptor(
122       imm_id_,
123       engine_name,
124       std::string(), // TODO(uekawa): Set short name.
125       layouts,
126       languages,
127       extension_ime_util::IsKeyboardLayoutExtension(
128           imm_id_), // is_login_keyboard
129       options_page,
130       input_view);
131
132   // TODO(komatsu): It is probably better to call AddInputMethodExtension
133   // out of Initialize.
134   manager->AddInputMethodExtension(imm_id_, this);
135 }
136
137 const input_method::InputMethodDescriptor& InputMethodEngine::GetDescriptor()
138     const {
139   return descriptor_;
140 }
141
142 void InputMethodEngine::RecordHistogram(const char* name, int count) {
143   std::string histo_name =
144       base::StringPrintf("InputMethod.%s.%s", name, engine_id_.c_str());
145   base::HistogramBase* counter = base::Histogram::FactoryGet(
146       histo_name, 0, 1000000, 50, base::HistogramBase::kNoFlags);
147   if (counter)
148     counter->Add(count);
149 }
150
151 void InputMethodEngine::NotifyImeReady() {
152   input_method::InputMethodManager* manager =
153       input_method::InputMethodManager::Get();
154   if (manager && imm_id_ == manager->GetCurrentInputMethod().id())
155     Enable();
156 }
157
158 bool InputMethodEngine::SetComposition(
159     int context_id,
160     const char* text,
161     int selection_start,
162     int selection_end,
163     int cursor,
164     const std::vector<SegmentInfo>& segments,
165     std::string* error) {
166   if (!active_) {
167     *error = kErrorNotActive;
168     return false;
169   }
170   if (context_id != context_id_ || context_id_ == -1) {
171     *error = kErrorWrongContext;
172     return false;
173   }
174
175   composition_cursor_ = cursor;
176   composition_text_.reset(new CompositionText());
177   composition_text_->set_text(base::UTF8ToUTF16(text));
178
179   composition_text_->set_selection_start(selection_start);
180   composition_text_->set_selection_end(selection_end);
181
182   // TODO: Add support for displaying selected text in the composition string.
183   for (std::vector<SegmentInfo>::const_iterator segment = segments.begin();
184        segment != segments.end(); ++segment) {
185     CompositionText::UnderlineAttribute underline;
186
187     switch (segment->style) {
188       case SEGMENT_STYLE_UNDERLINE:
189         underline.type = CompositionText::COMPOSITION_TEXT_UNDERLINE_SINGLE;
190         break;
191       case SEGMENT_STYLE_DOUBLE_UNDERLINE:
192         underline.type = CompositionText::COMPOSITION_TEXT_UNDERLINE_DOUBLE;
193         break;
194       default:
195         continue;
196     }
197
198     underline.start_index = segment->start;
199     underline.end_index = segment->end;
200     composition_text_->mutable_underline_attributes()->push_back(underline);
201   }
202
203   // TODO(nona): Makes focus out mode configuable, if necessary.
204   UpdateComposition(*composition_text_, composition_cursor_, true);
205   return true;
206 }
207
208 bool InputMethodEngine::ClearComposition(int context_id,
209                                          std::string* error)  {
210   if (!active_) {
211     *error = kErrorNotActive;
212     return false;
213   }
214   if (context_id != context_id_ || context_id_ == -1) {
215     *error = kErrorWrongContext;
216     return false;
217   }
218
219   composition_cursor_ = 0;
220   composition_text_.reset(new CompositionText());
221   UpdateComposition(*composition_text_, composition_cursor_, false);
222   return true;
223 }
224
225 bool InputMethodEngine::CommitText(int context_id, const char* text,
226                                    std::string* error) {
227   if (!active_) {
228     // TODO: Commit the text anyways.
229     *error = kErrorNotActive;
230     return false;
231   }
232   if (context_id != context_id_ || context_id_ == -1) {
233     *error = kErrorWrongContext;
234     return false;
235   }
236
237   IMEBridge::Get()->GetInputContextHandler()->CommitText(text);
238
239   // Records times for using input method.
240   if (!start_time_.ToInternalValue())
241     start_time_ = base::Time::Now();
242   end_time_ = base::Time::Now();
243   // Records histograms for counts of commits and committed characters.
244   RecordHistogram("Commit", 1);
245   RecordHistogram("CommitCharacter", GetUtf8StringLength(text));
246   return true;
247 }
248
249 bool InputMethodEngine::SendKeyEvents(
250     int context_id,
251     const std::vector<KeyboardEvent>& events) {
252   if (!active_) {
253     return false;
254   }
255   // context_id  ==  0, means sending key events to non-input field.
256   // context_id_ == -1, means the focus is not in an input field.
257   if (context_id != 0 && (context_id != context_id_ || context_id_ == -1)) {
258     return false;
259   }
260
261   ui::EventProcessor* dispatcher =
262       ash::Shell::GetPrimaryRootWindow()->GetHost()->event_processor();
263
264   for (size_t i = 0; i < events.size(); ++i) {
265     const KeyboardEvent& event = events[i];
266     const ui::EventType type =
267         (event.type == "keyup") ? ui::ET_KEY_RELEASED : ui::ET_KEY_PRESSED;
268
269     // KeyboardCodeFromXKyeSym assumes US keyboard layout.
270     ui::KeycodeConverter* conv = ui::KeycodeConverter::GetInstance();
271     DCHECK(conv);
272
273      // DOM code (KeyA) -> XKB -> XKeySym (XK_A) -> KeyboardCode (VKEY_A)
274     const uint16 native_keycode =
275         conv->CodeToNativeKeycode(event.code.c_str());
276     const uint xkeysym = ui::DefaultXKeysymFromHardwareKeycode(native_keycode);
277     const ui::KeyboardCode key_code = ui::KeyboardCodeFromXKeysym(xkeysym);
278
279     const std::string code = event.code;
280     int flags = ui::EF_NONE;
281     flags |= event.alt_key   ? ui::EF_ALT_DOWN       : ui::EF_NONE;
282     flags |= event.ctrl_key  ? ui::EF_CONTROL_DOWN   : ui::EF_NONE;
283     flags |= event.shift_key ? ui::EF_SHIFT_DOWN     : ui::EF_NONE;
284     flags |= event.caps_lock ? ui::EF_CAPS_LOCK_DOWN : ui::EF_NONE;
285
286     ui::KeyEvent ui_event(type, key_code, code, flags, false /* is_char */);
287     base::AutoReset<const ui::KeyEvent*> reset_sent_key(&sent_key_event_,
288                                                         &ui_event);
289     ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&ui_event);
290     if (details.dispatcher_destroyed)
291       break;
292   }
293   return true;
294 }
295
296 const InputMethodEngine::CandidateWindowProperty&
297 InputMethodEngine::GetCandidateWindowProperty() const {
298   return candidate_window_property_;
299 }
300
301 void InputMethodEngine::SetCandidateWindowProperty(
302     const CandidateWindowProperty& property) {
303   // Type conversion from InputMethodEngineInterface::CandidateWindowProperty to
304   // CandidateWindow::CandidateWindowProperty defined in chromeos/ime/.
305   ui::CandidateWindow::CandidateWindowProperty dest_property;
306   dest_property.page_size = property.page_size;
307   dest_property.is_cursor_visible = property.is_cursor_visible;
308   dest_property.is_vertical = property.is_vertical;
309   dest_property.show_window_at_composition =
310       property.show_window_at_composition;
311   dest_property.cursor_position =
312       candidate_window_->GetProperty().cursor_position;
313   dest_property.auxiliary_text = property.auxiliary_text;
314   dest_property.is_auxiliary_text_visible = property.is_auxiliary_text_visible;
315
316   candidate_window_->SetProperty(dest_property);
317   candidate_window_property_ = property;
318
319   if (active_) {
320     IMECandidateWindowHandlerInterface* cw_handler =
321         IMEBridge::Get()->GetCandidateWindowHandler();
322     if (cw_handler)
323       cw_handler->UpdateLookupTable(*candidate_window_, window_visible_);
324   }
325 }
326
327 bool InputMethodEngine::SetCandidateWindowVisible(bool visible,
328                                                   std::string* error) {
329   if (!active_) {
330     *error = kErrorNotActive;
331     return false;
332   }
333
334   window_visible_ = visible;
335   IMECandidateWindowHandlerInterface* cw_handler =
336       IMEBridge::Get()->GetCandidateWindowHandler();
337   if (cw_handler)
338     cw_handler->UpdateLookupTable(*candidate_window_, window_visible_);
339   return true;
340 }
341
342 bool InputMethodEngine::SetCandidates(
343     int context_id,
344     const std::vector<Candidate>& candidates,
345     std::string* error) {
346   if (!active_) {
347     *error = kErrorNotActive;
348     return false;
349   }
350   if (context_id != context_id_ || context_id_ == -1) {
351     *error = kErrorWrongContext;
352     return false;
353   }
354
355   // TODO: Nested candidates
356   candidate_ids_.clear();
357   candidate_indexes_.clear();
358   candidate_window_->mutable_candidates()->clear();
359   for (std::vector<Candidate>::const_iterator ix = candidates.begin();
360        ix != candidates.end(); ++ix) {
361     ui::CandidateWindow::Entry entry;
362     entry.value = base::UTF8ToUTF16(ix->value);
363     entry.label = base::UTF8ToUTF16(ix->label);
364     entry.annotation = base::UTF8ToUTF16(ix->annotation);
365     entry.description_title = base::UTF8ToUTF16(ix->usage.title);
366     entry.description_body = base::UTF8ToUTF16(ix->usage.body);
367
368     // Store a mapping from the user defined ID to the candidate index.
369     candidate_indexes_[ix->id] = candidate_ids_.size();
370     candidate_ids_.push_back(ix->id);
371
372     candidate_window_->mutable_candidates()->push_back(entry);
373   }
374   if (active_) {
375     IMECandidateWindowHandlerInterface* cw_handler =
376         IMEBridge::Get()->GetCandidateWindowHandler();
377     if (cw_handler)
378       cw_handler->UpdateLookupTable(*candidate_window_, window_visible_);
379   }
380   return true;
381 }
382
383 bool InputMethodEngine::SetCursorPosition(int context_id, int candidate_id,
384                                           std::string* error) {
385   if (!active_) {
386     *error = kErrorNotActive;
387     return false;
388   }
389   if (context_id != context_id_ || context_id_ == -1) {
390     *error = kErrorWrongContext;
391     return false;
392   }
393
394   std::map<int, int>::const_iterator position =
395       candidate_indexes_.find(candidate_id);
396   if (position == candidate_indexes_.end()) {
397     *error = kCandidateNotFound;
398     return false;
399   }
400
401   candidate_window_->set_cursor_position(position->second);
402   IMECandidateWindowHandlerInterface* cw_handler =
403       IMEBridge::Get()->GetCandidateWindowHandler();
404   if (cw_handler)
405     cw_handler->UpdateLookupTable(*candidate_window_, window_visible_);
406   return true;
407 }
408
409 bool InputMethodEngine::SetMenuItems(const std::vector<MenuItem>& items) {
410   return UpdateMenuItems(items);
411 }
412
413 bool InputMethodEngine::UpdateMenuItems(
414     const std::vector<MenuItem>& items) {
415   if (!active_)
416     return false;
417
418   ash::ime::InputMethodMenuItemList menu_item_list;
419   for (std::vector<MenuItem>::const_iterator item = items.begin();
420        item != items.end(); ++item) {
421     ash::ime::InputMethodMenuItem property;
422     MenuItemToProperty(*item, &property);
423     menu_item_list.push_back(property);
424   }
425
426   ash::ime::InputMethodMenuManager::GetInstance()->
427       SetCurrentInputMethodMenuItemList(
428           menu_item_list);
429   return true;
430 }
431
432 bool InputMethodEngine::IsActive() const {
433   return active_;
434 }
435
436 void InputMethodEngine::KeyEventDone(input_method::KeyEventHandle* key_data,
437                                      bool handled) {
438   KeyEventDoneCallback* callback =
439       reinterpret_cast<KeyEventDoneCallback*>(key_data);
440   callback->Run(handled);
441   delete callback;
442 }
443
444 bool InputMethodEngine::DeleteSurroundingText(int context_id,
445                                               int offset,
446                                               size_t number_of_chars,
447                                               std::string* error) {
448   if (!active_) {
449     *error = kErrorNotActive;
450     return false;
451   }
452   if (context_id != context_id_ || context_id_ == -1) {
453     *error = kErrorWrongContext;
454     return false;
455   }
456
457   if (offset < 0 && static_cast<size_t>(-1 * offset) != size_t(number_of_chars))
458     return false;  // Currently we can only support preceding text.
459
460   // TODO(nona): Return false if there is ongoing composition.
461
462   IMEInputContextHandlerInterface* input_context =
463       IMEBridge::Get()->GetInputContextHandler();
464   if (input_context)
465     input_context->DeleteSurroundingText(offset, number_of_chars);
466
467   return true;
468 }
469
470 void InputMethodEngine::HideInputView() {
471   keyboard::KeyboardController* keyboard_controller =
472     keyboard::KeyboardController::GetInstance();
473   if (keyboard_controller) {
474     keyboard_controller->HideKeyboard(
475         keyboard::KeyboardController::HIDE_REASON_MANUAL);
476   }
477 }
478
479 void InputMethodEngine::EnableInputView(bool enabled) {
480   const GURL& url = enabled ? input_view_url_ : GURL();
481   keyboard::SetOverrideContentUrl(url);
482   keyboard::KeyboardController* keyboard_controller =
483       keyboard::KeyboardController::GetInstance();
484   if (keyboard_controller)
485     keyboard_controller->Reload();
486 }
487
488 void InputMethodEngine::FocusIn(
489     const IMEEngineHandlerInterface::InputContext& input_context) {
490   current_input_type_ = input_context.type;
491
492   if (!active_ || current_input_type_ == ui::TEXT_INPUT_TYPE_NONE)
493     return;
494
495   // Prevent sending events on password field to 3rd-party IME extensions.
496   // And also make sure the VK fallback to system VK.
497   // TODO(shuchen): for password field, forcibly switch/lock the IME to the XKB
498   // keyboard related to the current IME.
499   if (current_input_type_ == ui::TEXT_INPUT_TYPE_PASSWORD &&
500       !extension_ime_util::IsComponentExtensionIME(GetDescriptor().id())) {
501     EnableInputView(false);
502     return;
503   }
504
505   context_id_ = next_context_id_;
506   ++next_context_id_;
507
508   InputMethodEngineInterface::InputContext context;
509   context.id = context_id_;
510   switch (current_input_type_) {
511     case ui::TEXT_INPUT_TYPE_SEARCH:
512       context.type = "search";
513       break;
514     case ui::TEXT_INPUT_TYPE_TELEPHONE:
515       context.type = "tel";
516       break;
517     case ui::TEXT_INPUT_TYPE_URL:
518       context.type = "url";
519       break;
520     case ui::TEXT_INPUT_TYPE_EMAIL:
521       context.type = "email";
522       break;
523     case ui::TEXT_INPUT_TYPE_NUMBER:
524       context.type = "number";
525       break;
526     case ui::TEXT_INPUT_TYPE_PASSWORD:
527       context.type = "password";
528       break;
529     default:
530       context.type = "text";
531       break;
532   }
533
534   observer_->OnFocus(context);
535 }
536
537 void InputMethodEngine::FocusOut() {
538   if (!active_ || current_input_type_ == ui::TEXT_INPUT_TYPE_NONE)
539     return;
540
541   ui::TextInputType previous_input_type = current_input_type_;
542   current_input_type_ = ui::TEXT_INPUT_TYPE_NONE;
543
544   // Prevent sending events on password field to 3rd-party IME extensions.
545   // And also make sure the VK restore to IME input view.
546   if (previous_input_type == ui::TEXT_INPUT_TYPE_PASSWORD &&
547       !extension_ime_util::IsComponentExtensionIME(GetDescriptor().id())) {
548     EnableInputView(true);
549     return;
550   }
551
552   int context_id = context_id_;
553   context_id_ = -1;
554   observer_->OnBlur(context_id);
555 }
556
557 void InputMethodEngine::Enable() {
558   active_ = true;
559   observer_->OnActivate(engine_id_);
560   current_input_type_ = IMEBridge::Get()->GetCurrentTextInputType();
561   FocusIn(IMEEngineHandlerInterface::InputContext(
562       current_input_type_, ui::TEXT_INPUT_MODE_DEFAULT));
563   EnableInputView(true);
564
565   start_time_ = base::Time();
566   end_time_ = base::Time();
567   RecordHistogram("Enable", 1);
568 }
569
570 void InputMethodEngine::Disable() {
571   active_ = false;
572   observer_->OnDeactivated(engine_id_);
573
574   if (start_time_.ToInternalValue())
575     RecordHistogram("WorkingTime", (end_time_ - start_time_).InSeconds());
576 }
577
578 void InputMethodEngine::PropertyActivate(const std::string& property_name) {
579   observer_->OnMenuItemActivated(engine_id_, property_name);
580 }
581
582 void InputMethodEngine::Reset() {
583   observer_->OnReset(engine_id_);
584 }
585
586 namespace {
587 void GetExtensionKeyboardEventFromKeyEvent(
588     const ui::KeyEvent& event,
589     InputMethodEngine::KeyboardEvent* ext_event) {
590   DCHECK(event.type() == ui::ET_KEY_RELEASED ||
591          event.type() == ui::ET_KEY_PRESSED);
592   DCHECK(ext_event);
593   ext_event->type = (event.type() == ui::ET_KEY_RELEASED) ? "keyup" : "keydown";
594
595   ext_event->code = event.code();
596   ext_event->alt_key = event.IsAltDown();
597   ext_event->ctrl_key = event.IsControlDown();
598   ext_event->shift_key = event.IsShiftDown();
599   ext_event->caps_lock = event.IsCapsLockDown();
600
601   uint32 x11_keysym = 0;
602   if (event.HasNativeEvent()) {
603     const base::NativeEvent& native_event = event.native_event();
604     DCHECK(native_event);
605
606     XKeyEvent* x_key = &(static_cast<XEvent*>(native_event)->xkey);
607     KeySym keysym = NoSymbol;
608     ::XLookupString(x_key, NULL, 0, &keysym, NULL);
609     x11_keysym = keysym;
610   } else {
611     // Convert ui::KeyEvent.key_code to DOM UIEvent key.
612     // XKeysymForWindowsKeyCode converts key_code to XKeySym, but it
613     // assumes US layout and does not care about CapLock state.
614     //
615     // TODO(komatsu): Support CapsLock states.
616     // TODO(komatsu): Support non-us keyboard layouts.
617     x11_keysym = ui::XKeysymForWindowsKeyCode(event.key_code(),
618                                               event.IsShiftDown());
619   }
620   ext_event->key = ui::FromXKeycodeToKeyValue(x11_keysym);
621 }
622 }  // namespace
623
624 void InputMethodEngine::ProcessKeyEvent(
625     const ui::KeyEvent& key_event,
626     const KeyEventDoneCallback& callback) {
627
628   KeyEventDoneCallback *handler = new KeyEventDoneCallback();
629   *handler = callback;
630
631   KeyboardEvent ext_event;
632   GetExtensionKeyboardEventFromKeyEvent(key_event, &ext_event);
633
634   // If the given key event is equal to the key event sent by
635   // SendKeyEvents, this engine ID is propagated to the extension IME.
636   // Note, this check relies on that ui::KeyEvent is propagated as
637   // reference without copying.
638   if (&key_event == sent_key_event_)
639     ext_event.extension_id = extension_id_;
640
641   observer_->OnKeyEvent(
642       engine_id_,
643       ext_event,
644       reinterpret_cast<input_method::KeyEventHandle*>(handler));
645 }
646
647 void InputMethodEngine::CandidateClicked(uint32 index) {
648   if (index > candidate_ids_.size()) {
649     return;
650   }
651
652   // Only left button click is supported at this moment.
653   observer_->OnCandidateClicked(
654       engine_id_, candidate_ids_.at(index), MOUSE_BUTTON_LEFT);
655 }
656
657 void InputMethodEngine::SetSurroundingText(const std::string& text,
658                                            uint32 cursor_pos,
659                                            uint32 anchor_pos) {
660   observer_->OnSurroundingTextChanged(engine_id_,
661                                       text,
662                                       static_cast<int>(cursor_pos),
663                                       static_cast<int>(anchor_pos));
664 }
665
666 // TODO(uekawa): rename this method to a more reasonable name.
667 void InputMethodEngine::MenuItemToProperty(
668     const MenuItem& item,
669     ash::ime::InputMethodMenuItem* property) {
670   property->key = item.id;
671
672   if (item.modified & MENU_ITEM_MODIFIED_LABEL) {
673     property->label = item.label;
674   }
675   if (item.modified & MENU_ITEM_MODIFIED_VISIBLE) {
676     // TODO(nona): Implement it.
677   }
678   if (item.modified & MENU_ITEM_MODIFIED_CHECKED) {
679     property->is_selection_item_checked = item.checked;
680   }
681   if (item.modified & MENU_ITEM_MODIFIED_ENABLED) {
682     // TODO(nona): implement sensitive entry(crbug.com/140192).
683   }
684   if (item.modified & MENU_ITEM_MODIFIED_STYLE) {
685     if (!item.children.empty()) {
686       // TODO(nona): Implement it.
687     } else {
688       switch (item.style) {
689         case MENU_ITEM_STYLE_NONE:
690           NOTREACHED();
691           break;
692         case MENU_ITEM_STYLE_CHECK:
693           // TODO(nona): Implement it.
694           break;
695         case MENU_ITEM_STYLE_RADIO:
696           property->is_selection_item = true;
697           break;
698         case MENU_ITEM_STYLE_SEPARATOR:
699           // TODO(nona): Implement it.
700           break;
701       }
702     }
703   }
704
705   // TODO(nona): Support item.children.
706 }
707
708 }  // namespace chromeos