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 <stddef.h>
9 #include <stdint.h>
10 
11 #include <map>
12 #include <string>
13 #include <vector>
14 
15 #include "chrome/browser/chromeos/input_method/assistive_window_properties.h"
16 #include "chrome/browser/chromeos/input_method/input_method_engine_base.h"
17 #include "chrome/browser/chromeos/input_method/suggestion_handler_interface.h"
18 #include "ui/base/ime/candidate_window.h"
19 #include "ui/base/ime/chromeos/ime_engine_handler_interface.h"
20 #include "ui/base/ime/chromeos/input_method_descriptor.h"
21 #include "ui/base/ime/chromeos/input_method_manager.h"
22 #include "url/gurl.h"
23 
24 namespace ui {
25 struct CompositionText;
26 class KeyEvent;
27 
28 namespace ime {
29 struct AssistiveWindowButton;
30 struct InputMethodMenuItem;
31 struct SuggestionDetails;
32 }  // namespace ime
33 }  // namespace ui
34 
35 namespace chromeos {
36 
37 struct AssistiveWindowProperties;
38 
39 class InputMethodEngine : public InputMethodEngineBase,
40                           public SuggestionHandlerInterface {
41  public:
42   enum {
43     MENU_ITEM_MODIFIED_LABEL = 0x0001,
44     MENU_ITEM_MODIFIED_STYLE = 0x0002,
45     MENU_ITEM_MODIFIED_VISIBLE = 0x0004,
46     MENU_ITEM_MODIFIED_ENABLED = 0x0008,
47     MENU_ITEM_MODIFIED_CHECKED = 0x0010,
48     MENU_ITEM_MODIFIED_ICON = 0x0020,
49   };
50 
51   enum CandidateWindowPosition {
52     WINDOW_POS_CURSOR,
53     WINDOW_POS_COMPOSITTION,
54   };
55 
56   struct UsageEntry {
57     std::string title;
58     std::string body;
59   };
60 
61   struct Candidate {
62     Candidate();
63     Candidate(const Candidate& other);
64     virtual ~Candidate();
65 
66     std::string value;
67     int id;
68     std::string label;
69     std::string annotation;
70     UsageEntry usage;
71     std::vector<Candidate> candidates;
72   };
73 
74   struct CandidateWindowProperty {
75     CandidateWindowProperty();
76     virtual ~CandidateWindowProperty();
77     CandidateWindowProperty(const CandidateWindowProperty& other);
78     int page_size;
79     bool is_cursor_visible;
80     bool is_vertical;
81     bool show_window_at_composition;
82 
83     // Auxiliary text is typically displayed in the footer of the candidate
84     // window.
85     std::string auxiliary_text;
86     bool is_auxiliary_text_visible;
87 
88     // The index of the current chosen candidate out of total candidates.
89     // value is -1 if there is no current chosen candidate.
90     int current_candidate_index = -1;
91     int total_candidates = 0;
92   };
93 
94   InputMethodEngine();
95 
96   ~InputMethodEngine() override;
97 
98   // InputMethodEngineBase overrides.
99   void Enable(const std::string& component_id) override;
100   bool IsActive() const override;
101 
102   // ui::IMEEngineHandlerInterface overrides.
103   void PropertyActivate(const std::string& property_name) override;
104   void CandidateClicked(uint32_t index) override;
105   void AssistiveWindowButtonClicked(
106       const ui::ime::AssistiveWindowButton& button) override;
107   void SetMirroringEnabled(bool mirroring_enabled) override;
108   void SetCastingEnabled(bool casting_enabled) override;
109   ui::InputMethodKeyboardController* GetInputMethodKeyboardController()
110       const override;
111 
112   // SuggestionHandlerInterface overrides.
113   bool DismissSuggestion(int context_id, std::string* error) override;
114   bool SetSuggestion(int context_id,
115                      const ui::ime::SuggestionDetails& details,
116                      std::string* error) override;
117   bool AcceptSuggestion(int context_id, std::string* error) override;
118   void OnSuggestionsChanged(
119       const std::vector<std::string>& suggestions) override;
120 
121   bool SetButtonHighlighted(int context_id,
122                             const ui::ime::AssistiveWindowButton& button,
123                             bool highlighted,
124                             std::string* error) override;
125 
126   void ClickButton(const ui::ime::AssistiveWindowButton& button) override;
127 
128   bool AcceptSuggestionCandidate(int context_id,
129                                  const base::string16& candidate,
130                                  std::string* error) override;
131 
132   bool SetAssistiveWindowProperties(
133       int context_id,
134       const AssistiveWindowProperties& assistive_window,
135       std::string* error) override;
136 
137   // This function returns the current property of the candidate window of the
138   // corresponding engine_id. If the CandidateWindowProperty is not set for the
139   // engine_id, a default value is set. The caller can use the returned value as
140   // the default property and modify some of specified items.
141   const CandidateWindowProperty& GetCandidateWindowProperty(
142       const std::string& engine_id);
143 
144   // Changes the property of the candidate window of the given engine_id and
145   // repaints the candidate window widget.
146   void SetCandidateWindowProperty(const std::string& engine_id,
147                                   const CandidateWindowProperty& property);
148 
149   // Show or hide the candidate window.
150   bool SetCandidateWindowVisible(bool visible, std::string* error);
151 
152   // Set the list of entries displayed in the candidate window.
153   bool SetCandidates(int context_id,
154                      const std::vector<Candidate>& candidates,
155                      std::string* error);
156 
157   // Set the position of the cursor in the candidate window.
158   bool SetCursorPosition(int context_id, int candidate_id, std::string* error);
159 
160   // Set the list of items that appears in the language menu when this IME is
161   // active.
162   bool SetMenuItems(
163       const std::vector<input_method::InputMethodManager::MenuItem>& items,
164       std::string* error);
165 
166   // Update the state of the menu items.
167   bool UpdateMenuItems(
168       const std::vector<input_method::InputMethodManager::MenuItem>& items,
169       std::string* error);
170 
171   // Hides the input view window (from API call).
172   void HideInputView();
173 
174   // Determine if the key event should be processed by the key
175   // event handler.
176   bool IsValidKeyEvent(const ui::KeyEvent* ui_event) override;
177 
178   // Sets a range as autocorrected to display a special dashed underline.  Start
179   // and end are code point offsets in the surroundingTextInfo which control the
180   // start and end point of the underline which is added to the text to show a
181   // word was autocorrected.
182   // TODO(b/171924748): Improve documentation for this function all the way down
183   // the stack.
184   bool SetAutocorrectRange(const base::string16& autocorrect_text,
185                            uint32_t start,
186                            uint32_t end) override;
187 
188   gfx::Range GetAutocorrectRange() override;
189 
190  private:
191   // InputMethodEngineBase:
192   void UpdateComposition(const ui::CompositionText& composition_text,
193                          uint32_t cursor_pos,
194                          bool is_visible) override;
195   bool SetCompositionRange(
196       uint32_t before,
197       uint32_t after,
198       const std::vector<ui::ImeTextSpan>& text_spans) override;
199   bool SetComposingRange(
200       uint32_t start,
201       uint32_t end,
202       const std::vector<ui::ImeTextSpan>& text_spans) override;
203 
204 
205   gfx::Rect GetAutocorrectCharacterBounds() override;
206 
207 
208   bool SetSelectionRange(uint32_t start, uint32_t end) override;
209 
210   void CommitTextToInputContext(int context_id,
211                                 const std::string& text) override;
212 
213   bool SendKeyEvent(ui::KeyEvent* event,
214                     const std::string& code,
215                     std::string* error) override;
216 
217   // Enables overriding input view page to Virtual Keyboard window.
218   void EnableInputView();
219 
220   // Converts MenuItem to InputMethodMenuItem.
221   void MenuItemToProperty(
222       const input_method::InputMethodManager::MenuItem& item,
223       ui::ime::InputMethodMenuItem* property);
224 
225   // The current candidate window.
226   ui::CandidateWindow candidate_window_;
227 
228   // The candidate window property of the current engine_id.
229   std::pair<std::string, CandidateWindowProperty> candidate_window_property_;
230 
231   // Indicates whether the candidate window is visible.
232   bool window_visible_ = false;
233 
234   // Mapping of candidate index to candidate id.
235   std::vector<int> candidate_ids_;
236 
237   // Mapping of candidate id to index.
238   std::map<int, int> candidate_indexes_;
239 
240   // Whether the screen is in mirroring mode.
241   bool is_mirroring_ = false;
242 
243   // Whether the desktop is being casted.
244   bool is_casting_ = false;
245 
246   DISALLOW_COPY_AND_ASSIGN(InputMethodEngine);
247 };
248 
249 }  // namespace chromeos
250 
251 #endif  // CHROME_BROWSER_CHROMEOS_INPUT_METHOD_INPUT_METHOD_ENGINE_H_
252