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/values.h" 14 #include "chrome/browser/chromeos/input_method/input_method_engine_interface.h" 15 #include "chrome/browser/extensions/api/profile_keyed_api_factory.h" 16 #include "components/browser_context_keyed_service/browser_context_keyed_service.h" 17 #include "content/public/browser/notification_observer.h" 18 #include "content/public/browser/notification_registrar.h" 19 #include "extensions/browser/extension_function.h" 20 #include "extensions/common/extension.h" 21 22 class Profile; 23 24 namespace chromeos { 25 class InputMethodEngineInterface; 26 class ImeObserver; 27 } 28 29 namespace extensions { 30 struct InputComponentInfo; 31 32 class InputImeEventRouter { 33 public: 34 static InputImeEventRouter* GetInstance(); 35 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::InputMethodEngineInterface* GetEngine( 41 const std::string& extension_id, 42 const std::string& engine_id); 43 chromeos::InputMethodEngineInterface* GetActiveEngine( 44 const std::string& extension_id); 45 46 47 // Called when a key event was handled. 48 void OnKeyEventHandled(const std::string& extension_id, 49 const std::string& request_id, 50 bool handled); 51 52 std::string AddRequest(const std::string& engine_id, 53 chromeos::input_method::KeyEventHandle* key_data); 54 55 private: 56 friend struct DefaultSingletonTraits<InputImeEventRouter>; 57 typedef std::map<std::string, std::pair<std::string, 58 chromeos::input_method::KeyEventHandle*> > RequestMap; 59 60 InputImeEventRouter(); 61 ~InputImeEventRouter(); 62 63 std::map<std::string, std::map<std::string, 64 chromeos::InputMethodEngineInterface*> > 65 engines_; 66 std::map<std::string, std::map<std::string, chromeos::ImeObserver*> > 67 observers_; 68 69 unsigned int next_request_id_; 70 RequestMap request_map_; 71 72 DISALLOW_COPY_AND_ASSIGN(InputImeEventRouter); 73 }; 74 75 class InputImeSetCompositionFunction : public SyncExtensionFunction { 76 public: 77 DECLARE_EXTENSION_FUNCTION("input.ime.setComposition", 78 INPUT_IME_SETCOMPOSITION) 79 80 protected: 81 virtual ~InputImeSetCompositionFunction() {} 82 83 // ExtensionFunction: 84 virtual bool RunImpl() OVERRIDE; 85 }; 86 87 class InputImeClearCompositionFunction : public SyncExtensionFunction { 88 public: 89 DECLARE_EXTENSION_FUNCTION("input.ime.clearComposition", 90 INPUT_IME_CLEARCOMPOSITION) 91 92 protected: 93 virtual ~InputImeClearCompositionFunction() {} 94 95 // ExtensionFunction: 96 virtual bool RunImpl() OVERRIDE; 97 }; 98 99 class InputImeCommitTextFunction : public SyncExtensionFunction { 100 public: 101 DECLARE_EXTENSION_FUNCTION("input.ime.commitText", INPUT_IME_COMMITTEXT) 102 103 protected: 104 virtual ~InputImeCommitTextFunction() {} 105 106 // ExtensionFunction: 107 virtual bool RunImpl() OVERRIDE; 108 }; 109 110 class InputImeSetCandidateWindowPropertiesFunction 111 : public SyncExtensionFunction { 112 public: 113 DECLARE_EXTENSION_FUNCTION("input.ime.setCandidateWindowProperties", 114 INPUT_IME_SETCANDIDATEWINDOWPROPERTIES) 115 116 protected: 117 virtual ~InputImeSetCandidateWindowPropertiesFunction() {} 118 119 // ExtensionFunction: 120 virtual bool RunImpl() OVERRIDE; 121 }; 122 123 class InputImeSetCandidatesFunction : public SyncExtensionFunction { 124 public: 125 DECLARE_EXTENSION_FUNCTION("input.ime.setCandidates", INPUT_IME_SETCANDIDATES) 126 127 protected: 128 virtual ~InputImeSetCandidatesFunction() {} 129 130 // ExtensionFunction: 131 virtual bool RunImpl() OVERRIDE; 132 }; 133 134 class InputImeSetCursorPositionFunction : public SyncExtensionFunction { 135 public: 136 DECLARE_EXTENSION_FUNCTION("input.ime.setCursorPosition", 137 INPUT_IME_SETCURSORPOSITION) 138 139 protected: 140 virtual ~InputImeSetCursorPositionFunction() {} 141 142 // ExtensionFunction: 143 virtual bool RunImpl() OVERRIDE; 144 }; 145 146 class InputImeSetMenuItemsFunction : public SyncExtensionFunction { 147 public: 148 DECLARE_EXTENSION_FUNCTION("input.ime.setMenuItems", INPUT_IME_SETMENUITEMS) 149 150 protected: 151 virtual ~InputImeSetMenuItemsFunction() {} 152 153 // ExtensionFunction: 154 virtual bool RunImpl() OVERRIDE; 155 }; 156 157 class InputImeUpdateMenuItemsFunction : public SyncExtensionFunction { 158 public: 159 DECLARE_EXTENSION_FUNCTION("input.ime.updateMenuItems", 160 INPUT_IME_UPDATEMENUITEMS) 161 162 protected: 163 virtual ~InputImeUpdateMenuItemsFunction() {} 164 165 // ExtensionFunction: 166 virtual bool RunImpl() OVERRIDE; 167 }; 168 169 class InputImeDeleteSurroundingTextFunction : public SyncExtensionFunction { 170 public: 171 DECLARE_EXTENSION_FUNCTION("input.ime.deleteSurroundingText", 172 INPUT_IME_DELETESURROUNDINGTEXT) 173 protected: 174 virtual ~InputImeDeleteSurroundingTextFunction() {} 175 176 // ExtensionFunction: 177 virtual bool RunImpl() OVERRIDE; 178 }; 179 180 class InputImeKeyEventHandledFunction : public AsyncExtensionFunction { 181 public: 182 DECLARE_EXTENSION_FUNCTION("input.ime.keyEventHandled", 183 INPUT_IME_KEYEVENTHANDLED) 184 185 protected: 186 virtual ~InputImeKeyEventHandledFunction() {} 187 188 // ExtensionFunction: 189 virtual bool RunImpl() OVERRIDE; 190 }; 191 192 class InputImeSendKeyEventsFunction : public AsyncExtensionFunction { 193 public: 194 DECLARE_EXTENSION_FUNCTION("input.ime.sendKeyEvents", 195 INPUT_IME_SENDKEYEVENTS) 196 197 protected: 198 virtual ~InputImeSendKeyEventsFunction() {} 199 200 // ExtensionFunction: 201 virtual bool RunImpl() OVERRIDE; 202 }; 203 204 class InputImeAPI : public ProfileKeyedAPI, 205 public content::NotificationObserver { 206 public: 207 explicit InputImeAPI(Profile* profile); 208 virtual ~InputImeAPI(); 209 210 // ProfileKeyedAPI implementation. 211 static ProfileKeyedAPIFactory<InputImeAPI>* GetFactoryInstance(); 212 213 // content::NotificationObserver implementation. 214 virtual void Observe(int type, 215 const content::NotificationSource& source, 216 const content::NotificationDetails& details) OVERRIDE; 217 218 private: 219 friend class ProfileKeyedAPIFactory<InputImeAPI>; 220 InputImeEventRouter* input_ime_event_router(); 221 222 // ProfileKeyedAPI implementation. 223 static const char* service_name() { 224 return "InputImeAPI"; 225 } 226 static const bool kServiceIsNULLWhileTesting = true; 227 228 Profile* const profile_; 229 content::NotificationRegistrar registrar_; 230 }; 231 232 } // namespace extensions 233 234 #endif // CHROME_BROWSER_EXTENSIONS_API_INPUT_IME_INPUT_IME_API_H_ 235