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/values.h"
14 #include "chrome/browser/chromeos/input_method/input_method_engine.h"
15 #include "chrome/browser/extensions/api/profile_keyed_api_factory.h"
16 #include "chrome/browser/extensions/extension_function.h"
17 #include "chrome/common/extensions/extension.h"
18 #include "components/browser_context_keyed_service/browser_context_keyed_service.h"
19 #include "content/public/browser/notification_observer.h"
20 #include "content/public/browser/notification_registrar.h"
25 class InputMethodEngine;
29 namespace extensions {
30 struct InputComponentInfo;
32 class InputImeEventRouter {
34 static InputImeEventRouter* GetInstance();
36 bool RegisterIme(Profile* profile,
37 const std::string& extension_id,
38 const extensions::InputComponentInfo& component);
39 void UnregisterAllImes(Profile* profile, const std::string& extension_id);
40 chromeos::InputMethodEngine* GetEngine(const std::string& extension_id,
41 const std::string& engine_id);
42 chromeos::InputMethodEngine* GetActiveEngine(const std::string& extension_id);
45 // Called when a key event was handled.
46 void OnKeyEventHandled(const std::string& extension_id,
47 const std::string& request_id,
50 std::string AddRequest(const std::string& engine_id,
51 chromeos::input_method::KeyEventHandle* key_data);
54 friend struct DefaultSingletonTraits<InputImeEventRouter>;
55 typedef std::map<std::string, std::pair<std::string,
56 chromeos::input_method::KeyEventHandle*> > RequestMap;
58 InputImeEventRouter();
59 ~InputImeEventRouter();
61 std::map<std::string, std::map<std::string, chromeos::InputMethodEngine*> >
63 std::map<std::string, std::map<std::string, chromeos::ImeObserver*> >
66 unsigned int next_request_id_;
67 RequestMap request_map_;
69 DISALLOW_COPY_AND_ASSIGN(InputImeEventRouter);
72 class InputImeSetCompositionFunction : public SyncExtensionFunction {
74 DECLARE_EXTENSION_FUNCTION("input.ime.setComposition",
75 INPUT_IME_SETCOMPOSITION)
78 virtual ~InputImeSetCompositionFunction() {}
81 virtual bool RunImpl() OVERRIDE;
84 class InputImeClearCompositionFunction : public SyncExtensionFunction {
86 DECLARE_EXTENSION_FUNCTION("input.ime.clearComposition",
87 INPUT_IME_CLEARCOMPOSITION)
90 virtual ~InputImeClearCompositionFunction() {}
93 virtual bool RunImpl() OVERRIDE;
96 class InputImeCommitTextFunction : public SyncExtensionFunction {
98 DECLARE_EXTENSION_FUNCTION("input.ime.commitText", INPUT_IME_COMMITTEXT)
101 virtual ~InputImeCommitTextFunction() {}
103 // ExtensionFunction:
104 virtual bool RunImpl() OVERRIDE;
107 class InputImeSetCandidateWindowPropertiesFunction
108 : public SyncExtensionFunction {
110 DECLARE_EXTENSION_FUNCTION("input.ime.setCandidateWindowProperties",
111 INPUT_IME_SETCANDIDATEWINDOWPROPERTIES)
114 virtual ~InputImeSetCandidateWindowPropertiesFunction() {}
116 // ExtensionFunction:
117 virtual bool RunImpl() OVERRIDE;
120 class InputImeSetCandidatesFunction : public SyncExtensionFunction {
122 DECLARE_EXTENSION_FUNCTION("input.ime.setCandidates", INPUT_IME_SETCANDIDATES)
125 virtual ~InputImeSetCandidatesFunction() {}
127 // ExtensionFunction:
128 virtual bool RunImpl() OVERRIDE;
131 class InputImeSetCursorPositionFunction : public SyncExtensionFunction {
133 DECLARE_EXTENSION_FUNCTION("input.ime.setCursorPosition",
134 INPUT_IME_SETCURSORPOSITION)
137 virtual ~InputImeSetCursorPositionFunction() {}
139 // ExtensionFunction:
140 virtual bool RunImpl() OVERRIDE;
143 class InputImeSetMenuItemsFunction : public SyncExtensionFunction {
145 DECLARE_EXTENSION_FUNCTION("input.ime.setMenuItems", INPUT_IME_SETMENUITEMS)
148 virtual ~InputImeSetMenuItemsFunction() {}
150 // ExtensionFunction:
151 virtual bool RunImpl() OVERRIDE;
154 class InputImeUpdateMenuItemsFunction : public SyncExtensionFunction {
156 DECLARE_EXTENSION_FUNCTION("input.ime.updateMenuItems",
157 INPUT_IME_UPDATEMENUITEMS)
160 virtual ~InputImeUpdateMenuItemsFunction() {}
162 // ExtensionFunction:
163 virtual bool RunImpl() OVERRIDE;
166 class InputImeDeleteSurroundingTextFunction : public SyncExtensionFunction {
168 DECLARE_EXTENSION_FUNCTION("input.ime.deleteSurroundingText",
169 INPUT_IME_DELETESURROUNDINGTEXT)
171 virtual ~InputImeDeleteSurroundingTextFunction() {}
173 // ExtensionFunction:
174 virtual bool RunImpl() OVERRIDE;
177 class InputImeKeyEventHandledFunction : public AsyncExtensionFunction {
179 DECLARE_EXTENSION_FUNCTION("input.ime.keyEventHandled",
180 INPUT_IME_KEYEVENTHANDLED)
183 virtual ~InputImeKeyEventHandledFunction() {}
185 // ExtensionFunction:
186 virtual bool RunImpl() OVERRIDE;
189 class InputImeAPI : public ProfileKeyedAPI,
190 public content::NotificationObserver {
192 explicit InputImeAPI(Profile* profile);
193 virtual ~InputImeAPI();
195 // ProfileKeyedAPI implementation.
196 static ProfileKeyedAPIFactory<InputImeAPI>* GetFactoryInstance();
198 // content::NotificationObserver implementation.
199 virtual void Observe(int type,
200 const content::NotificationSource& source,
201 const content::NotificationDetails& details) OVERRIDE;
204 friend class ProfileKeyedAPIFactory<InputImeAPI>;
205 InputImeEventRouter* input_ime_event_router();
207 // ProfileKeyedAPI implementation.
208 static const char* service_name() {
209 return "InputImeAPI";
211 static const bool kServiceIsNULLWhileTesting = true;
213 Profile* const profile_;
214 content::NotificationRegistrar registrar_;
217 } // namespace extensions
219 #endif // CHROME_BROWSER_EXTENSIONS_API_INPUT_IME_INPUT_IME_API_H_