773d13b4ae0b877af329677d7d181475161db73e
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / input_ime / input_ime_api.h
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(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);
47
48
49   // Called when a key event was handled.
50   void OnKeyEventHandled(const std::string& extension_id,
51                          const std::string& request_id,
52                          bool handled);
53
54   std::string AddRequest(const std::string& engine_id,
55                          chromeos::input_method::KeyEventHandle* key_data);
56
57  private:
58   friend struct DefaultSingletonTraits<InputImeEventRouter>;
59   typedef std::map<std::string, std::pair<std::string,
60           chromeos::input_method::KeyEventHandle*> > RequestMap;
61
62   InputImeEventRouter();
63   ~InputImeEventRouter();
64
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*>
69       EngineMap;
70   typedef std::map<std::string, EngineMap> ExtensionMap;
71   typedef std::map<Profile*, ExtensionMap, ProfileCompare>
72       ProfileEngineMap;
73   ProfileEngineMap profile_engine_map_;
74
75   unsigned int next_request_id_;
76   RequestMap request_map_;
77
78   DISALLOW_COPY_AND_ASSIGN(InputImeEventRouter);
79 };
80
81 class InputImeSetCompositionFunction : public SyncExtensionFunction {
82  public:
83   DECLARE_EXTENSION_FUNCTION("input.ime.setComposition",
84                              INPUT_IME_SETCOMPOSITION)
85
86  protected:
87   virtual ~InputImeSetCompositionFunction() {}
88
89   // ExtensionFunction:
90   virtual bool RunSync() OVERRIDE;
91 };
92
93 class InputImeClearCompositionFunction : public SyncExtensionFunction {
94  public:
95   DECLARE_EXTENSION_FUNCTION("input.ime.clearComposition",
96                              INPUT_IME_CLEARCOMPOSITION)
97
98  protected:
99   virtual ~InputImeClearCompositionFunction() {}
100
101   // ExtensionFunction:
102   virtual bool RunSync() OVERRIDE;
103 };
104
105 class InputImeCommitTextFunction : public SyncExtensionFunction {
106  public:
107   DECLARE_EXTENSION_FUNCTION("input.ime.commitText", INPUT_IME_COMMITTEXT)
108
109  protected:
110   virtual ~InputImeCommitTextFunction() {}
111
112   // ExtensionFunction:
113   virtual bool RunSync() OVERRIDE;
114 };
115
116 class InputImeSetCandidateWindowPropertiesFunction
117     : public SyncExtensionFunction {
118  public:
119   DECLARE_EXTENSION_FUNCTION("input.ime.setCandidateWindowProperties",
120                              INPUT_IME_SETCANDIDATEWINDOWPROPERTIES)
121
122  protected:
123   virtual ~InputImeSetCandidateWindowPropertiesFunction() {}
124
125   // ExtensionFunction:
126   virtual bool RunSync() OVERRIDE;
127 };
128
129 class InputImeSetCandidatesFunction : public SyncExtensionFunction {
130  public:
131   DECLARE_EXTENSION_FUNCTION("input.ime.setCandidates", INPUT_IME_SETCANDIDATES)
132
133  protected:
134   virtual ~InputImeSetCandidatesFunction() {}
135
136   // ExtensionFunction:
137   virtual bool RunSync() OVERRIDE;
138 };
139
140 class InputImeSetCursorPositionFunction : public SyncExtensionFunction {
141  public:
142   DECLARE_EXTENSION_FUNCTION("input.ime.setCursorPosition",
143                              INPUT_IME_SETCURSORPOSITION)
144
145  protected:
146   virtual ~InputImeSetCursorPositionFunction() {}
147
148   // ExtensionFunction:
149   virtual bool RunSync() OVERRIDE;
150 };
151
152 class InputImeSetMenuItemsFunction : public SyncExtensionFunction {
153  public:
154   DECLARE_EXTENSION_FUNCTION("input.ime.setMenuItems", INPUT_IME_SETMENUITEMS)
155
156  protected:
157   virtual ~InputImeSetMenuItemsFunction() {}
158
159   // ExtensionFunction:
160   virtual bool RunSync() OVERRIDE;
161 };
162
163 class InputImeUpdateMenuItemsFunction : public SyncExtensionFunction {
164  public:
165   DECLARE_EXTENSION_FUNCTION("input.ime.updateMenuItems",
166                              INPUT_IME_UPDATEMENUITEMS)
167
168  protected:
169   virtual ~InputImeUpdateMenuItemsFunction() {}
170
171   // ExtensionFunction:
172   virtual bool RunSync() OVERRIDE;
173 };
174
175 class InputImeDeleteSurroundingTextFunction : public SyncExtensionFunction {
176  public:
177   DECLARE_EXTENSION_FUNCTION("input.ime.deleteSurroundingText",
178                              INPUT_IME_DELETESURROUNDINGTEXT)
179  protected:
180   virtual ~InputImeDeleteSurroundingTextFunction() {}
181
182   // ExtensionFunction:
183   virtual bool RunSync() OVERRIDE;
184 };
185
186 class InputImeKeyEventHandledFunction : public AsyncExtensionFunction {
187  public:
188   DECLARE_EXTENSION_FUNCTION("input.ime.keyEventHandled",
189                              INPUT_IME_KEYEVENTHANDLED)
190
191  protected:
192   virtual ~InputImeKeyEventHandledFunction() {}
193
194   // ExtensionFunction:
195   virtual bool RunAsync() OVERRIDE;
196 };
197
198 class InputImeSendKeyEventsFunction : public AsyncExtensionFunction {
199  public:
200   DECLARE_EXTENSION_FUNCTION("input.ime.sendKeyEvents",
201                              INPUT_IME_SENDKEYEVENTS)
202
203  protected:
204   virtual ~InputImeSendKeyEventsFunction() {}
205
206   // ExtensionFunction:
207   virtual bool RunAsync() OVERRIDE;
208 };
209
210 class InputImeHideInputViewFunction : public AsyncExtensionFunction {
211  public:
212   DECLARE_EXTENSION_FUNCTION("input.ime.hideInputView",
213                              INPUT_IME_HIDEINPUTVIEW)
214
215  protected:
216   virtual ~InputImeHideInputViewFunction() {}
217
218   // ExtensionFunction:
219   virtual bool RunAsync() OVERRIDE;
220 };
221
222 class InputImeAPI : public BrowserContextKeyedAPI,
223                     public ExtensionRegistryObserver,
224                     public EventRouter::Observer {
225  public:
226   explicit InputImeAPI(content::BrowserContext* context);
227   virtual ~InputImeAPI();
228
229   // BrowserContextKeyedAPI implementation.
230   static BrowserContextKeyedAPIFactory<InputImeAPI>* GetFactoryInstance();
231
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;
239
240   // EventRouter::Observer implementation.
241   virtual void OnListenerAdded(const EventListenerInfo& details) OVERRIDE;
242
243  private:
244   friend class BrowserContextKeyedAPIFactory<InputImeAPI>;
245   InputImeEventRouter* input_ime_event_router();
246
247   // BrowserContextKeyedAPI implementation.
248   static const char* service_name() {
249     return "InputImeAPI";
250   }
251   static const bool kServiceIsNULLWhileTesting = true;
252
253   content::BrowserContext* const browser_context_;
254
255   // Listen to extension load, unloaded notifications.
256   ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
257       extension_registry_observer_;
258 };
259
260 }  // namespace extensions
261
262 #endif  // CHROME_BROWSER_EXTENSIONS_API_INPUT_IME_INPUT_IME_API_H_