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 explicit InputImeEventRouter(Profile
* profile
);
38 ~InputImeEventRouter();
40 bool RegisterImeExtension(
41 const std::string
& extension_id
,
42 const std::vector
<extensions::InputComponentInfo
>& input_components
);
43 void UnregisterAllImes(const std::string
& extension_id
);
45 chromeos::InputMethodEngineInterface
* GetEngine(
46 const std::string
& extension_id
,
47 const std::string
& component_id
);
48 chromeos::InputMethodEngineInterface
* GetActiveEngine(
49 const std::string
& extension_id
);
52 // Called when a key event was handled.
53 void OnKeyEventHandled(const std::string
& extension_id
,
54 const std::string
& request_id
,
57 std::string
AddRequest(const std::string
& component_id
,
58 chromeos::input_method::KeyEventHandle
* key_data
);
61 typedef std::map
<std::string
, std::pair
<std::string
,
62 chromeos::input_method::KeyEventHandle
*> > RequestMap
;
64 // The engine map from extension_id to an engine.
65 std::map
<std::string
, chromeos::InputMethodEngineInterface
*> engine_map_
;
67 unsigned int next_request_id_
;
68 RequestMap request_map_
;
71 DISALLOW_COPY_AND_ASSIGN(InputImeEventRouter
);
74 class InputImeEventRouterFactory
{
76 static InputImeEventRouterFactory
* GetInstance();
77 InputImeEventRouter
* GetRouter(Profile
* profile
);
80 friend struct DefaultSingletonTraits
<InputImeEventRouterFactory
>;
81 InputImeEventRouterFactory();
82 ~InputImeEventRouterFactory();
84 std::map
<Profile
*, InputImeEventRouter
*, ProfileCompare
> router_map_
;
86 DISALLOW_COPY_AND_ASSIGN(InputImeEventRouterFactory
);
89 class InputImeSetCompositionFunction
: public SyncExtensionFunction
{
91 DECLARE_EXTENSION_FUNCTION("input.ime.setComposition",
92 INPUT_IME_SETCOMPOSITION
)
95 ~InputImeSetCompositionFunction() override
{}
98 bool RunSync() override
;
101 class InputImeClearCompositionFunction
: public SyncExtensionFunction
{
103 DECLARE_EXTENSION_FUNCTION("input.ime.clearComposition",
104 INPUT_IME_CLEARCOMPOSITION
)
107 ~InputImeClearCompositionFunction() override
{}
109 // ExtensionFunction:
110 bool RunSync() override
;
113 class InputImeCommitTextFunction
: public SyncExtensionFunction
{
115 DECLARE_EXTENSION_FUNCTION("input.ime.commitText", INPUT_IME_COMMITTEXT
)
118 ~InputImeCommitTextFunction() override
{}
120 // ExtensionFunction:
121 bool RunSync() override
;
124 class InputImeSetCandidateWindowPropertiesFunction
125 : public SyncExtensionFunction
{
127 DECLARE_EXTENSION_FUNCTION("input.ime.setCandidateWindowProperties",
128 INPUT_IME_SETCANDIDATEWINDOWPROPERTIES
)
131 ~InputImeSetCandidateWindowPropertiesFunction() override
{}
133 // ExtensionFunction:
134 bool RunSync() override
;
137 class InputImeSetCandidatesFunction
: public SyncExtensionFunction
{
139 DECLARE_EXTENSION_FUNCTION("input.ime.setCandidates", INPUT_IME_SETCANDIDATES
)
142 ~InputImeSetCandidatesFunction() override
{}
144 // ExtensionFunction:
145 bool RunSync() override
;
148 class InputImeSetCursorPositionFunction
: public SyncExtensionFunction
{
150 DECLARE_EXTENSION_FUNCTION("input.ime.setCursorPosition",
151 INPUT_IME_SETCURSORPOSITION
)
154 ~InputImeSetCursorPositionFunction() override
{}
156 // ExtensionFunction:
157 bool RunSync() override
;
160 class InputImeSetMenuItemsFunction
: public SyncExtensionFunction
{
162 DECLARE_EXTENSION_FUNCTION("input.ime.setMenuItems", INPUT_IME_SETMENUITEMS
)
165 ~InputImeSetMenuItemsFunction() override
{}
167 // ExtensionFunction:
168 bool RunSync() override
;
171 class InputImeUpdateMenuItemsFunction
: public SyncExtensionFunction
{
173 DECLARE_EXTENSION_FUNCTION("input.ime.updateMenuItems",
174 INPUT_IME_UPDATEMENUITEMS
)
177 ~InputImeUpdateMenuItemsFunction() override
{}
179 // ExtensionFunction:
180 bool RunSync() override
;
183 class InputImeDeleteSurroundingTextFunction
: public SyncExtensionFunction
{
185 DECLARE_EXTENSION_FUNCTION("input.ime.deleteSurroundingText",
186 INPUT_IME_DELETESURROUNDINGTEXT
)
188 ~InputImeDeleteSurroundingTextFunction() override
{}
190 // ExtensionFunction:
191 bool RunSync() override
;
194 class InputImeKeyEventHandledFunction
: public AsyncExtensionFunction
{
196 DECLARE_EXTENSION_FUNCTION("input.ime.keyEventHandled",
197 INPUT_IME_KEYEVENTHANDLED
)
200 ~InputImeKeyEventHandledFunction() override
{}
202 // ExtensionFunction:
203 bool RunAsync() override
;
206 class InputImeSendKeyEventsFunction
: public AsyncExtensionFunction
{
208 DECLARE_EXTENSION_FUNCTION("input.ime.sendKeyEvents",
209 INPUT_IME_SENDKEYEVENTS
)
212 ~InputImeSendKeyEventsFunction() override
{}
214 // ExtensionFunction:
215 bool RunAsync() override
;
218 class InputImeHideInputViewFunction
: public AsyncExtensionFunction
{
220 DECLARE_EXTENSION_FUNCTION("input.ime.hideInputView",
221 INPUT_IME_HIDEINPUTVIEW
)
224 ~InputImeHideInputViewFunction() override
{}
226 // ExtensionFunction:
227 bool RunAsync() override
;
230 class InputImeAPI
: public BrowserContextKeyedAPI
,
231 public ExtensionRegistryObserver
,
232 public EventRouter::Observer
{
234 explicit InputImeAPI(content::BrowserContext
* context
);
235 ~InputImeAPI() override
;
237 // BrowserContextKeyedAPI implementation.
238 static BrowserContextKeyedAPIFactory
<InputImeAPI
>* GetFactoryInstance();
240 // ExtensionRegistryObserver implementation.
241 void OnExtensionLoaded(content::BrowserContext
* browser_context
,
242 const Extension
* extension
) override
;
243 void OnExtensionUnloaded(content::BrowserContext
* browser_context
,
244 const Extension
* extension
,
245 UnloadedExtensionInfo::Reason reason
) override
;
247 // EventRouter::Observer implementation.
248 void OnListenerAdded(const EventListenerInfo
& details
) override
;
251 friend class BrowserContextKeyedAPIFactory
<InputImeAPI
>;
252 InputImeEventRouter
* input_ime_event_router();
254 // BrowserContextKeyedAPI implementation.
255 static const char* service_name() {
256 return "InputImeAPI";
258 static const bool kServiceIsNULLWhileTesting
= true;
260 content::BrowserContext
* const browser_context_
;
262 // Listen to extension load, unloaded notifications.
263 ScopedObserver
<ExtensionRegistry
, ExtensionRegistryObserver
>
264 extension_registry_observer_
;
267 } // namespace extensions
269 #endif // CHROME_BROWSER_EXTENSIONS_API_INPUT_IME_INPUT_IME_API_H_