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(const std::string& extension_id,
40 const extensions::InputComponentInfo& component);
41 void UnregisterAllImes(const std::string& extension_id);
42 chromeos::InputMethodEngineInterface* GetEngine(
43 const std::string& extension_id,
44 const std::string& engine_id);
45 chromeos::InputMethodEngineInterface* GetActiveEngine(
46 const std::string& extension_id);
49 // Called when a key event was handled.
50 void OnKeyEventHandled(const std::string& extension_id,
51 const std::string& request_id,
54 std::string AddRequest(const std::string& engine_id,
55 chromeos::input_method::KeyEventHandle* key_data);
58 friend struct DefaultSingletonTraits<InputImeEventRouter>;
59 typedef std::map<std::string, std::pair<std::string,
60 chromeos::input_method::KeyEventHandle*> > RequestMap;
62 InputImeEventRouter();
63 ~InputImeEventRouter();
65 // The engine map for event routing.
66 // { Profile : { extension_id : { engine_id : Engine } } }.
67 // TODO(shuchen): reuse the engine map in InputMethodManagerImpl.
68 typedef std::map<std::string, chromeos::InputMethodEngineInterface*>
70 typedef std::map<std::string, EngineMap> ExtensionMap;
71 typedef std::map<Profile*, ExtensionMap, ProfileCompare>
73 ProfileEngineMap profile_engine_map_;
75 unsigned int next_request_id_;
76 RequestMap request_map_;
78 DISALLOW_COPY_AND_ASSIGN(InputImeEventRouter);
81 class InputImeSetCompositionFunction : public SyncExtensionFunction {
83 DECLARE_EXTENSION_FUNCTION("input.ime.setComposition",
84 INPUT_IME_SETCOMPOSITION)
87 virtual ~InputImeSetCompositionFunction() {}
90 virtual bool RunSync() OVERRIDE;
93 class InputImeClearCompositionFunction : public SyncExtensionFunction {
95 DECLARE_EXTENSION_FUNCTION("input.ime.clearComposition",
96 INPUT_IME_CLEARCOMPOSITION)
99 virtual ~InputImeClearCompositionFunction() {}
101 // ExtensionFunction:
102 virtual bool RunSync() OVERRIDE;
105 class InputImeCommitTextFunction : public SyncExtensionFunction {
107 DECLARE_EXTENSION_FUNCTION("input.ime.commitText", INPUT_IME_COMMITTEXT)
110 virtual ~InputImeCommitTextFunction() {}
112 // ExtensionFunction:
113 virtual bool RunSync() OVERRIDE;
116 class InputImeSetCandidateWindowPropertiesFunction
117 : public SyncExtensionFunction {
119 DECLARE_EXTENSION_FUNCTION("input.ime.setCandidateWindowProperties",
120 INPUT_IME_SETCANDIDATEWINDOWPROPERTIES)
123 virtual ~InputImeSetCandidateWindowPropertiesFunction() {}
125 // ExtensionFunction:
126 virtual bool RunSync() OVERRIDE;
129 class InputImeSetCandidatesFunction : public SyncExtensionFunction {
131 DECLARE_EXTENSION_FUNCTION("input.ime.setCandidates", INPUT_IME_SETCANDIDATES)
134 virtual ~InputImeSetCandidatesFunction() {}
136 // ExtensionFunction:
137 virtual bool RunSync() OVERRIDE;
140 class InputImeSetCursorPositionFunction : public SyncExtensionFunction {
142 DECLARE_EXTENSION_FUNCTION("input.ime.setCursorPosition",
143 INPUT_IME_SETCURSORPOSITION)
146 virtual ~InputImeSetCursorPositionFunction() {}
148 // ExtensionFunction:
149 virtual bool RunSync() OVERRIDE;
152 class InputImeSetMenuItemsFunction : public SyncExtensionFunction {
154 DECLARE_EXTENSION_FUNCTION("input.ime.setMenuItems", INPUT_IME_SETMENUITEMS)
157 virtual ~InputImeSetMenuItemsFunction() {}
159 // ExtensionFunction:
160 virtual bool RunSync() OVERRIDE;
163 class InputImeUpdateMenuItemsFunction : public SyncExtensionFunction {
165 DECLARE_EXTENSION_FUNCTION("input.ime.updateMenuItems",
166 INPUT_IME_UPDATEMENUITEMS)
169 virtual ~InputImeUpdateMenuItemsFunction() {}
171 // ExtensionFunction:
172 virtual bool RunSync() OVERRIDE;
175 class InputImeDeleteSurroundingTextFunction : public SyncExtensionFunction {
177 DECLARE_EXTENSION_FUNCTION("input.ime.deleteSurroundingText",
178 INPUT_IME_DELETESURROUNDINGTEXT)
180 virtual ~InputImeDeleteSurroundingTextFunction() {}
182 // ExtensionFunction:
183 virtual bool RunSync() OVERRIDE;
186 class InputImeKeyEventHandledFunction : public AsyncExtensionFunction {
188 DECLARE_EXTENSION_FUNCTION("input.ime.keyEventHandled",
189 INPUT_IME_KEYEVENTHANDLED)
192 virtual ~InputImeKeyEventHandledFunction() {}
194 // ExtensionFunction:
195 virtual bool RunAsync() OVERRIDE;
198 class InputImeSendKeyEventsFunction : public AsyncExtensionFunction {
200 DECLARE_EXTENSION_FUNCTION("input.ime.sendKeyEvents",
201 INPUT_IME_SENDKEYEVENTS)
204 virtual ~InputImeSendKeyEventsFunction() {}
206 // ExtensionFunction:
207 virtual bool RunAsync() OVERRIDE;
210 class InputImeHideInputViewFunction : public AsyncExtensionFunction {
212 DECLARE_EXTENSION_FUNCTION("input.ime.hideInputView",
213 INPUT_IME_HIDEINPUTVIEW)
216 virtual ~InputImeHideInputViewFunction() {}
218 // ExtensionFunction:
219 virtual bool RunAsync() OVERRIDE;
222 class InputImeAPI : public BrowserContextKeyedAPI,
223 public ExtensionRegistryObserver,
224 public EventRouter::Observer {
226 explicit InputImeAPI(content::BrowserContext* context);
227 virtual ~InputImeAPI();
229 // BrowserContextKeyedAPI implementation.
230 static BrowserContextKeyedAPIFactory<InputImeAPI>* GetFactoryInstance();
232 // ExtensionRegistryObserver implementation.
233 virtual void OnExtensionLoaded(content::BrowserContext* browser_context,
234 const Extension* extension) OVERRIDE;
235 virtual void OnExtensionUnloaded(
236 content::BrowserContext* browser_context,
237 const Extension* extension,
238 UnloadedExtensionInfo::Reason reason) OVERRIDE;
240 // EventRouter::Observer implementation.
241 virtual void OnListenerAdded(const EventListenerInfo& details) OVERRIDE;
244 friend class BrowserContextKeyedAPIFactory<InputImeAPI>;
245 InputImeEventRouter* input_ime_event_router();
247 // BrowserContextKeyedAPI implementation.
248 static const char* service_name() {
249 return "InputImeAPI";
251 static const bool kServiceIsNULLWhileTesting = true;
253 content::BrowserContext* const browser_context_;
255 // Listen to extension load, unloaded notifications.
256 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
257 extension_registry_observer_;
260 } // namespace extensions
262 #endif // CHROME_BROWSER_EXTENSIONS_API_INPUT_IME_INPUT_IME_API_H_