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