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.
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_INPUT_IME_INPUT_IME_API_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_INPUT_IME_INPUT_IME_API_H_
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"
27 class InputMethodEngineInterface;
29 } // namespace chromeos
31 namespace extensions {
32 class ExtensionRegistry;
33 struct InputComponentInfo;
35 class InputImeEventRouter {
37 static InputImeEventRouter* GetInstance();
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);
50 // Called when a key event was handled.
51 void OnKeyEventHandled(const std::string& extension_id,
52 const std::string& request_id,
55 std::string AddRequest(const std::string& engine_id,
56 chromeos::input_method::KeyEventHandle* key_data);
59 friend struct DefaultSingletonTraits<InputImeEventRouter>;
60 typedef std::map<std::string, std::pair<std::string,
61 chromeos::input_method::KeyEventHandle*> > RequestMap;
63 InputImeEventRouter();
64 ~InputImeEventRouter();
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*>
71 typedef std::map<std::string, EngineMap> ExtensionMap;
72 typedef std::map<Profile*, ExtensionMap, ProfileCompare>
74 ProfileEngineMap profile_engine_map_;
76 unsigned int next_request_id_;
77 RequestMap request_map_;
79 DISALLOW_COPY_AND_ASSIGN(InputImeEventRouter);
82 class InputImeSetCompositionFunction : public SyncExtensionFunction {
84 DECLARE_EXTENSION_FUNCTION("input.ime.setComposition",
85 INPUT_IME_SETCOMPOSITION)
88 virtual ~InputImeSetCompositionFunction() {}
91 virtual bool RunSync() OVERRIDE;
94 class InputImeClearCompositionFunction : public SyncExtensionFunction {
96 DECLARE_EXTENSION_FUNCTION("input.ime.clearComposition",
97 INPUT_IME_CLEARCOMPOSITION)
100 virtual ~InputImeClearCompositionFunction() {}
102 // ExtensionFunction:
103 virtual bool RunSync() OVERRIDE;
106 class InputImeCommitTextFunction : public SyncExtensionFunction {
108 DECLARE_EXTENSION_FUNCTION("input.ime.commitText", INPUT_IME_COMMITTEXT)
111 virtual ~InputImeCommitTextFunction() {}
113 // ExtensionFunction:
114 virtual bool RunSync() OVERRIDE;
117 class InputImeSetCandidateWindowPropertiesFunction
118 : public SyncExtensionFunction {
120 DECLARE_EXTENSION_FUNCTION("input.ime.setCandidateWindowProperties",
121 INPUT_IME_SETCANDIDATEWINDOWPROPERTIES)
124 virtual ~InputImeSetCandidateWindowPropertiesFunction() {}
126 // ExtensionFunction:
127 virtual bool RunSync() OVERRIDE;
130 class InputImeSetCandidatesFunction : public SyncExtensionFunction {
132 DECLARE_EXTENSION_FUNCTION("input.ime.setCandidates", INPUT_IME_SETCANDIDATES)
135 virtual ~InputImeSetCandidatesFunction() {}
137 // ExtensionFunction:
138 virtual bool RunSync() OVERRIDE;
141 class InputImeSetCursorPositionFunction : public SyncExtensionFunction {
143 DECLARE_EXTENSION_FUNCTION("input.ime.setCursorPosition",
144 INPUT_IME_SETCURSORPOSITION)
147 virtual ~InputImeSetCursorPositionFunction() {}
149 // ExtensionFunction:
150 virtual bool RunSync() OVERRIDE;
153 class InputImeSetMenuItemsFunction : public SyncExtensionFunction {
155 DECLARE_EXTENSION_FUNCTION("input.ime.setMenuItems", INPUT_IME_SETMENUITEMS)
158 virtual ~InputImeSetMenuItemsFunction() {}
160 // ExtensionFunction:
161 virtual bool RunSync() OVERRIDE;
164 class InputImeUpdateMenuItemsFunction : public SyncExtensionFunction {
166 DECLARE_EXTENSION_FUNCTION("input.ime.updateMenuItems",
167 INPUT_IME_UPDATEMENUITEMS)
170 virtual ~InputImeUpdateMenuItemsFunction() {}
172 // ExtensionFunction:
173 virtual bool RunSync() OVERRIDE;
176 class InputImeDeleteSurroundingTextFunction : public SyncExtensionFunction {
178 DECLARE_EXTENSION_FUNCTION("input.ime.deleteSurroundingText",
179 INPUT_IME_DELETESURROUNDINGTEXT)
181 virtual ~InputImeDeleteSurroundingTextFunction() {}
183 // ExtensionFunction:
184 virtual bool RunSync() OVERRIDE;
187 class InputImeKeyEventHandledFunction : public AsyncExtensionFunction {
189 DECLARE_EXTENSION_FUNCTION("input.ime.keyEventHandled",
190 INPUT_IME_KEYEVENTHANDLED)
193 virtual ~InputImeKeyEventHandledFunction() {}
195 // ExtensionFunction:
196 virtual bool RunAsync() OVERRIDE;
199 class InputImeSendKeyEventsFunction : public AsyncExtensionFunction {
201 DECLARE_EXTENSION_FUNCTION("input.ime.sendKeyEvents",
202 INPUT_IME_SENDKEYEVENTS)
205 virtual ~InputImeSendKeyEventsFunction() {}
207 // ExtensionFunction:
208 virtual bool RunAsync() OVERRIDE;
211 class InputImeHideInputViewFunction : public AsyncExtensionFunction {
213 DECLARE_EXTENSION_FUNCTION("input.ime.hideInputView",
214 INPUT_IME_HIDEINPUTVIEW)
217 virtual ~InputImeHideInputViewFunction() {}
219 // ExtensionFunction:
220 virtual bool RunAsync() OVERRIDE;
223 class InputImeAPI : public BrowserContextKeyedAPI,
224 public ExtensionRegistryObserver,
225 public EventRouter::Observer {
227 explicit InputImeAPI(content::BrowserContext* context);
228 virtual ~InputImeAPI();
230 // BrowserContextKeyedAPI implementation.
231 static BrowserContextKeyedAPIFactory<InputImeAPI>* GetFactoryInstance();
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;
241 // EventRouter::Observer implementation.
242 virtual void OnListenerAdded(const EventListenerInfo& details) OVERRIDE;
245 friend class BrowserContextKeyedAPIFactory<InputImeAPI>;
246 InputImeEventRouter* input_ime_event_router();
248 // BrowserContextKeyedAPI implementation.
249 static const char* service_name() {
250 return "InputImeAPI";
252 static const bool kServiceIsNULLWhileTesting = true;
254 content::BrowserContext* const browser_context_;
256 // Listen to extension load, unloaded notifications.
257 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
258 extension_registry_observer_;
261 } // namespace extensions
263 #endif // CHROME_BROWSER_EXTENSIONS_API_INPUT_IME_INPUT_IME_API_H_