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 #ifndef CHROME_BROWSER_CHROMEOS_INPUT_METHOD_INPUT_METHOD_ENGINE_H_ 6 #define CHROME_BROWSER_CHROMEOS_INPUT_METHOD_INPUT_METHOD_ENGINE_H_ 7 8 #include <map> 9 #include <string> 10 #include <vector> 11 #include "base/time/time.h" 12 #include "chrome/browser/chromeos/input_method/input_method_engine_interface.h" 13 #include "chromeos/ime/input_method_descriptor.h" 14 #include "url/gurl.h" 15 16 class Profile; 17 18 namespace ui { 19 class CandidateWindow; 20 class KeyEvent; 21 } // namespace ui 22 23 namespace ash { 24 namespace ime { 25 struct InputMethodMenuItem; 26 } // namespace ime 27 } // namespace ash 28 29 namespace chromeos { 30 31 class CompositionText; 32 33 namespace input_method { 34 struct KeyEventHandle; 35 } // namespace input_method 36 37 class InputMethodEngine : public InputMethodEngineInterface { 38 public: 39 InputMethodEngine(); 40 41 virtual ~InputMethodEngine(); 42 43 void Initialize(scoped_ptr<InputMethodEngineInterface::Observer> observer, 44 const char* extension_id); 45 46 // InputMethodEngineInterface overrides. 47 virtual const std::string& GetActiveComponentId() const OVERRIDE; 48 virtual bool SetComposition(int context_id, 49 const char* text, 50 int selection_start, 51 int selection_end, 52 int cursor, 53 const std::vector<SegmentInfo>& segments, 54 std::string* error) OVERRIDE; 55 virtual bool ClearComposition(int context_id, std::string* error) OVERRIDE; 56 virtual bool CommitText(int context_id, const char* text, 57 std::string* error) OVERRIDE; 58 virtual bool SendKeyEvents(int context_id, 59 const std::vector<KeyboardEvent>& events) OVERRIDE; 60 virtual const CandidateWindowProperty& 61 GetCandidateWindowProperty() const OVERRIDE; 62 virtual void SetCandidateWindowProperty( 63 const CandidateWindowProperty& property) OVERRIDE; 64 virtual bool SetCandidateWindowVisible(bool visible, 65 std::string* error) OVERRIDE; 66 virtual bool SetCandidates(int context_id, 67 const std::vector<Candidate>& candidates, 68 std::string* error) OVERRIDE; 69 virtual bool SetCursorPosition(int context_id, int candidate_id, 70 std::string* error) OVERRIDE; 71 virtual bool SetMenuItems(const std::vector<MenuItem>& items) OVERRIDE; 72 virtual bool UpdateMenuItems(const std::vector<MenuItem>& items) OVERRIDE; 73 virtual bool IsActive() const OVERRIDE; 74 virtual bool DeleteSurroundingText(int context_id, 75 int offset, 76 size_t number_of_chars, 77 std::string* error) OVERRIDE; 78 79 // IMEEngineHandlerInterface overrides. 80 virtual void FocusIn( 81 const IMEEngineHandlerInterface::InputContext& input_context) OVERRIDE; 82 virtual void FocusOut() OVERRIDE; 83 virtual void Enable(const std::string& component_id) OVERRIDE; 84 virtual void Disable() OVERRIDE; 85 virtual void PropertyActivate(const std::string& property_name) OVERRIDE; 86 virtual void Reset() OVERRIDE; 87 virtual void ProcessKeyEvent(const ui::KeyEvent& key_event, 88 const KeyEventDoneCallback& callback) OVERRIDE; 89 virtual void CandidateClicked(uint32 index) OVERRIDE; 90 virtual void SetSurroundingText(const std::string& text, uint32 cursor_pos, 91 uint32 anchor_pos) OVERRIDE; 92 virtual void HideInputView() OVERRIDE; 93 94 int GetCotextIdForTesting() { return context_id_; } 95 96 private: 97 // Converts MenuItem to InputMethodMenuItem. 98 void MenuItemToProperty(const MenuItem& item, 99 ash::ime::InputMethodMenuItem* property); 100 101 // Enables overriding input view page to Virtual Keyboard window. 102 void EnableInputView(); 103 104 ui::TextInputType current_input_type_; 105 106 // ID that is used for the current input context. False if there is no focus. 107 int context_id_; 108 109 // Next id that will be assigned to a context. 110 int next_context_id_; 111 112 // The input_component ID in IME extension's manifest. 113 std::string active_component_id_; 114 115 // The IME extension ID. 116 std::string extension_id_; 117 118 // The observer object recieving events for this IME. 119 scoped_ptr<InputMethodEngineInterface::Observer> observer_; 120 121 // The current preedit text, and it's cursor position. 122 scoped_ptr<CompositionText> composition_text_; 123 int composition_cursor_; 124 125 // The current candidate window. 126 scoped_ptr<ui::CandidateWindow> candidate_window_; 127 128 // The current candidate window property. 129 CandidateWindowProperty candidate_window_property_; 130 131 // Indicates whether the candidate window is visible. 132 bool window_visible_; 133 134 // Mapping of candidate index to candidate id. 135 std::vector<int> candidate_ids_; 136 137 // Mapping of candidate id to index. 138 std::map<int, int> candidate_indexes_; 139 140 // Used with SendKeyEvents and ProcessKeyEvent to check if the key event 141 // sent to ProcessKeyEvent is sent by SendKeyEvents. 142 const ui::KeyEvent* sent_key_event_; 143 }; 144 145 } // namespace chromeos 146 147 #endif // CHROME_BROWSER_CHROMEOS_INPUT_METHOD_INPUT_METHOD_ENGINE_H_ 148