• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2012 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_EXTENSIONS_API_INPUT_IME_INPUT_IME_API_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_INPUT_IME_INPUT_IME_API_H_
7 
8 #include <map>
9 #include <string>
10 #include <vector>
11 
12 #include "base/memory/singleton.h"
13 #include "base/scoped_observer.h"
14 #include "base/values.h"
15 #include "chrome/browser/chromeos/input_method/input_method_engine_interface.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "components/keyed_service/core/keyed_service.h"
18 #include "extensions/browser/browser_context_keyed_api_factory.h"
19 #include "extensions/browser/event_router.h"
20 #include "extensions/browser/extension_function.h"
21 #include "extensions/browser/extension_registry_observer.h"
22 #include "extensions/common/extension.h"
23 
24 class Profile;
25 
26 namespace chromeos {
27 class InputMethodEngineInterface;
28 class ImeObserver;
29 }  // namespace chromeos
30 
31 namespace extensions {
32 class ExtensionRegistry;
33 struct InputComponentInfo;
34 
35 class InputImeEventRouter {
36  public:
37   static InputImeEventRouter* GetInstance();
38 
39   bool RegisterIme(Profile*,
40                    const std::string& extension_id,
41                    const extensions::InputComponentInfo& component);
42   void UnregisterAllImes(const std::string& extension_id);
43   chromeos::InputMethodEngineInterface* GetEngine(
44       const std::string& extension_id,
45       const std::string& engine_id);
46   chromeos::InputMethodEngineInterface* GetActiveEngine(
47       const std::string& extension_id);
48 
49 
50   // Called when a key event was handled.
51   void OnKeyEventHandled(const std::string& extension_id,
52                          const std::string& request_id,
53                          bool handled);
54 
55   std::string AddRequest(const std::string& engine_id,
56                          chromeos::input_method::KeyEventHandle* key_data);
57 
58  private:
59   friend struct DefaultSingletonTraits<InputImeEventRouter>;
60   typedef std::map<std::string, std::pair<std::string,
61           chromeos::input_method::KeyEventHandle*> > RequestMap;
62 
63   InputImeEventRouter();
64   ~InputImeEventRouter();
65 
66   // The engine map for event routing.
67   //   { Profile : { extension_id : { engine_id : Engine } } }.
68   // TODO(shuchen): reuse the engine map in InputMethodManagerImpl.
69   typedef std::map<std::string, chromeos::InputMethodEngineInterface*>
70       EngineMap;
71   typedef std::map<std::string, EngineMap> ExtensionMap;
72   typedef std::map<Profile*, ExtensionMap, ProfileCompare>
73       ProfileEngineMap;
74   ProfileEngineMap profile_engine_map_;
75 
76   unsigned int next_request_id_;
77   RequestMap request_map_;
78 
79   DISALLOW_COPY_AND_ASSIGN(InputImeEventRouter);
80 };
81 
82 class InputImeSetCompositionFunction : public SyncExtensionFunction {
83  public:
84   DECLARE_EXTENSION_FUNCTION("input.ime.setComposition",
85                              INPUT_IME_SETCOMPOSITION)
86 
87  protected:
88   virtual ~InputImeSetCompositionFunction() {}
89 
90   // ExtensionFunction:
91   virtual bool RunSync() OVERRIDE;
92 };
93 
94 class InputImeClearCompositionFunction : public SyncExtensionFunction {
95  public:
96   DECLARE_EXTENSION_FUNCTION("input.ime.clearComposition",
97                              INPUT_IME_CLEARCOMPOSITION)
98 
99  protected:
100   virtual ~InputImeClearCompositionFunction() {}
101 
102   // ExtensionFunction:
103   virtual bool RunSync() OVERRIDE;
104 };
105 
106 class InputImeCommitTextFunction : public SyncExtensionFunction {
107  public:
108   DECLARE_EXTENSION_FUNCTION("input.ime.commitText", INPUT_IME_COMMITTEXT)
109 
110  protected:
111   virtual ~InputImeCommitTextFunction() {}
112 
113   // ExtensionFunction:
114   virtual bool RunSync() OVERRIDE;
115 };
116 
117 class InputImeSetCandidateWindowPropertiesFunction
118     : public SyncExtensionFunction {
119  public:
120   DECLARE_EXTENSION_FUNCTION("input.ime.setCandidateWindowProperties",
121                              INPUT_IME_SETCANDIDATEWINDOWPROPERTIES)
122 
123  protected:
124   virtual ~InputImeSetCandidateWindowPropertiesFunction() {}
125 
126   // ExtensionFunction:
127   virtual bool RunSync() OVERRIDE;
128 };
129 
130 class InputImeSetCandidatesFunction : public SyncExtensionFunction {
131  public:
132   DECLARE_EXTENSION_FUNCTION("input.ime.setCandidates", INPUT_IME_SETCANDIDATES)
133 
134  protected:
135   virtual ~InputImeSetCandidatesFunction() {}
136 
137   // ExtensionFunction:
138   virtual bool RunSync() OVERRIDE;
139 };
140 
141 class InputImeSetCursorPositionFunction : public SyncExtensionFunction {
142  public:
143   DECLARE_EXTENSION_FUNCTION("input.ime.setCursorPosition",
144                              INPUT_IME_SETCURSORPOSITION)
145 
146  protected:
147   virtual ~InputImeSetCursorPositionFunction() {}
148 
149   // ExtensionFunction:
150   virtual bool RunSync() OVERRIDE;
151 };
152 
153 class InputImeSetMenuItemsFunction : public SyncExtensionFunction {
154  public:
155   DECLARE_EXTENSION_FUNCTION("input.ime.setMenuItems", INPUT_IME_SETMENUITEMS)
156 
157  protected:
158   virtual ~InputImeSetMenuItemsFunction() {}
159 
160   // ExtensionFunction:
161   virtual bool RunSync() OVERRIDE;
162 };
163 
164 class InputImeUpdateMenuItemsFunction : public SyncExtensionFunction {
165  public:
166   DECLARE_EXTENSION_FUNCTION("input.ime.updateMenuItems",
167                              INPUT_IME_UPDATEMENUITEMS)
168 
169  protected:
170   virtual ~InputImeUpdateMenuItemsFunction() {}
171 
172   // ExtensionFunction:
173   virtual bool RunSync() OVERRIDE;
174 };
175 
176 class InputImeDeleteSurroundingTextFunction : public SyncExtensionFunction {
177  public:
178   DECLARE_EXTENSION_FUNCTION("input.ime.deleteSurroundingText",
179                              INPUT_IME_DELETESURROUNDINGTEXT)
180  protected:
181   virtual ~InputImeDeleteSurroundingTextFunction() {}
182 
183   // ExtensionFunction:
184   virtual bool RunSync() OVERRIDE;
185 };
186 
187 class InputImeKeyEventHandledFunction : public AsyncExtensionFunction {
188  public:
189   DECLARE_EXTENSION_FUNCTION("input.ime.keyEventHandled",
190                              INPUT_IME_KEYEVENTHANDLED)
191 
192  protected:
193   virtual ~InputImeKeyEventHandledFunction() {}
194 
195   // ExtensionFunction:
196   virtual bool RunAsync() OVERRIDE;
197 };
198 
199 class InputImeSendKeyEventsFunction : public AsyncExtensionFunction {
200  public:
201   DECLARE_EXTENSION_FUNCTION("input.ime.sendKeyEvents",
202                              INPUT_IME_SENDKEYEVENTS)
203 
204  protected:
205   virtual ~InputImeSendKeyEventsFunction() {}
206 
207   // ExtensionFunction:
208   virtual bool RunAsync() OVERRIDE;
209 };
210 
211 class InputImeHideInputViewFunction : public AsyncExtensionFunction {
212  public:
213   DECLARE_EXTENSION_FUNCTION("input.ime.hideInputView",
214                              INPUT_IME_HIDEINPUTVIEW)
215 
216  protected:
217   virtual ~InputImeHideInputViewFunction() {}
218 
219   // ExtensionFunction:
220   virtual bool RunAsync() OVERRIDE;
221 };
222 
223 class InputImeAPI : public BrowserContextKeyedAPI,
224                     public ExtensionRegistryObserver,
225                     public EventRouter::Observer {
226  public:
227   explicit InputImeAPI(content::BrowserContext* context);
228   virtual ~InputImeAPI();
229 
230   // BrowserContextKeyedAPI implementation.
231   static BrowserContextKeyedAPIFactory<InputImeAPI>* GetFactoryInstance();
232 
233   // ExtensionRegistryObserver implementation.
234   virtual void OnExtensionLoaded(content::BrowserContext* browser_context,
235                                  const Extension* extension) OVERRIDE;
236   virtual void OnExtensionUnloaded(
237       content::BrowserContext* browser_context,
238       const Extension* extension,
239       UnloadedExtensionInfo::Reason reason) OVERRIDE;
240 
241   // EventRouter::Observer implementation.
242   virtual void OnListenerAdded(const EventListenerInfo& details) OVERRIDE;
243 
244  private:
245   friend class BrowserContextKeyedAPIFactory<InputImeAPI>;
246   InputImeEventRouter* input_ime_event_router();
247 
248   // BrowserContextKeyedAPI implementation.
249   static const char* service_name() {
250     return "InputImeAPI";
251   }
252   static const bool kServiceIsNULLWhileTesting = true;
253 
254   content::BrowserContext* const browser_context_;
255 
256   // Listen to extension load, unloaded notifications.
257   ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
258       extension_registry_observer_;
259 };
260 
261 }  // namespace extensions
262 
263 #endif  // CHROME_BROWSER_EXTENSIONS_API_INPUT_IME_INPUT_IME_API_H_
264