• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(Profile* profile,
44                   scoped_ptr<InputMethodEngineInterface::Observer> observer,
45                   const char* engine_name,
46                   const char* extension_id,
47                   const char* engine_id,
48                   const std::vector<std::string>& languages,
49                   const std::vector<std::string>& layouts,
50                   const GURL& options_page,
51                   const GURL& input_view);
52 
53   // InputMethodEngineInterface overrides.
54   virtual const input_method::InputMethodDescriptor& GetDescriptor()
55       const OVERRIDE;
56   virtual void NotifyImeReady() OVERRIDE;
57   virtual bool SetComposition(int context_id,
58                               const char* text,
59                               int selection_start,
60                               int selection_end,
61                               int cursor,
62                               const std::vector<SegmentInfo>& segments,
63                               std::string* error) OVERRIDE;
64   virtual bool ClearComposition(int context_id, std::string* error) OVERRIDE;
65   virtual bool CommitText(int context_id, const char* text,
66                           std::string* error) OVERRIDE;
67   virtual bool SendKeyEvents(int context_id,
68                              const std::vector<KeyboardEvent>& events) OVERRIDE;
69   virtual const CandidateWindowProperty&
70     GetCandidateWindowProperty() const OVERRIDE;
71   virtual void SetCandidateWindowProperty(
72       const CandidateWindowProperty& property) OVERRIDE;
73   virtual bool SetCandidateWindowVisible(bool visible,
74                                          std::string* error) OVERRIDE;
75   virtual bool SetCandidates(int context_id,
76                              const std::vector<Candidate>& candidates,
77                              std::string* error) OVERRIDE;
78   virtual bool SetCursorPosition(int context_id, int candidate_id,
79                                  std::string* error) OVERRIDE;
80   virtual bool SetMenuItems(const std::vector<MenuItem>& items) OVERRIDE;
81   virtual bool UpdateMenuItems(const std::vector<MenuItem>& items) OVERRIDE;
82   virtual bool IsActive() const OVERRIDE;
83   virtual bool DeleteSurroundingText(int context_id,
84                                      int offset,
85                                      size_t number_of_chars,
86                                      std::string* error) OVERRIDE;
87 
88   // IMEEngineHandlerInterface overrides.
89   virtual void FocusIn(
90       const IMEEngineHandlerInterface::InputContext& input_context) OVERRIDE;
91   virtual void FocusOut() OVERRIDE;
92   virtual void Enable() OVERRIDE;
93   virtual void Disable() OVERRIDE;
94   virtual void PropertyActivate(const std::string& property_name) OVERRIDE;
95   virtual void Reset() OVERRIDE;
96   virtual void ProcessKeyEvent(const ui::KeyEvent& key_event,
97                                const KeyEventDoneCallback& callback) OVERRIDE;
98   virtual void CandidateClicked(uint32 index) OVERRIDE;
99   virtual void SetSurroundingText(const std::string& text, uint32 cursor_pos,
100                                   uint32 anchor_pos) OVERRIDE;
101   virtual void HideInputView() OVERRIDE;
102 
103  private:
104   void RecordHistogram(const char* name, int count);
105 
106   // Converts MenuItem to InputMethodMenuItem.
107   void MenuItemToProperty(const MenuItem& item,
108                           ash::ime::InputMethodMenuItem* property);
109 
110   // Enables or disables overriding input view page to Virtual Keyboard window.
111   void EnableInputView(bool enabled);
112 
113   // Descriptor of this input method.
114   input_method::InputMethodDescriptor descriptor_;
115 
116   ui::TextInputType current_input_type_;
117 
118   // True if this engine is active.
119   bool active_;
120 
121   // ID that is used for the current input context.  False if there is no focus.
122   int context_id_;
123 
124   // Next id that will be assigned to a context.
125   int next_context_id_;
126 
127   // This IME ID in Chrome Extension.
128   std::string engine_id_;
129 
130   // This IME's Chrome Extension ID.
131   std::string extension_id_;
132 
133   // This IME ID in InputMethodManager.
134   std::string imm_id_;
135 
136   // The observer object recieving events for this IME.
137   scoped_ptr<InputMethodEngineInterface::Observer> observer_;
138 
139   // The current preedit text, and it's cursor position.
140   scoped_ptr<CompositionText> composition_text_;
141   int composition_cursor_;
142 
143   // The current candidate window.
144   scoped_ptr<ui::CandidateWindow> candidate_window_;
145 
146   // The current candidate window property.
147   CandidateWindowProperty candidate_window_property_;
148 
149   // Indicates whether the candidate window is visible.
150   bool window_visible_;
151 
152   // Mapping of candidate index to candidate id.
153   std::vector<int> candidate_ids_;
154 
155   // Mapping of candidate id to index.
156   std::map<int, int> candidate_indexes_;
157 
158   // Used for input view window.
159   GURL input_view_url_;
160 
161   // Used with SendKeyEvents and ProcessKeyEvent to check if the key event
162   // sent to ProcessKeyEvent is sent by SendKeyEvents.
163   const ui::KeyEvent* sent_key_event_;
164 
165   // The start & end time of using this input method. This is for UMA.
166   base::Time start_time_;
167   base::Time end_time_;
168 
169   // User profile that owns this method.
170   Profile* profile_;
171 };
172 
173 }  // namespace chromeos
174 
175 #endif  // CHROME_BROWSER_CHROMEOS_INPUT_METHOD_INPUT_METHOD_ENGINE_H_
176