| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/extensions/api/input_ime/input_ime_api.h" | 5 #include "chrome/browser/extensions/api/input_ime/input_ime_api.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include "base/lazy_instance.h" |
| 8 | |
| 9 #include "base/strings/string_number_conversions.h" | |
| 10 #include "chrome/common/extensions/api/input_ime.h" | 8 #include "chrome/common/extensions/api/input_ime.h" |
| 11 #include "chrome/common/extensions/api/input_ime/input_components_handler.h" | |
| 12 #include "extensions/browser/extension_registry.h" | 9 #include "extensions/browser/extension_registry.h" |
| 13 | 10 |
| 14 namespace input_ime = extensions::api::input_ime; | 11 namespace input_ime = extensions::api::input_ime; |
| 15 namespace KeyEventHandled = extensions::api::input_ime::KeyEventHandled; | 12 namespace KeyEventHandled = extensions::api::input_ime::KeyEventHandled; |
| 16 using ui::IMEEngineHandlerInterface; | 13 using ui::IMEEngineHandlerInterface; |
| 17 | 14 |
| 18 namespace ui { | 15 namespace ui { |
| 19 | 16 |
| 20 ImeObserver::ImeObserver(const std::string& extension_id, Profile* profile) | 17 ImeObserver::ImeObserver(const std::string& extension_id, Profile* profile) |
| 21 : extension_id_(extension_id), profile_(profile) {} | 18 : extension_id_(extension_id), profile_(profile) {} |
| (...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 199 | 196 |
| 200 InputImeEventRouter* InputImeEventRouterFactory::GetRouter(Profile* profile) { | 197 InputImeEventRouter* InputImeEventRouterFactory::GetRouter(Profile* profile) { |
| 201 InputImeEventRouter* router = router_map_[profile]; | 198 InputImeEventRouter* router = router_map_[profile]; |
| 202 if (!router) { | 199 if (!router) { |
| 203 router = new InputImeEventRouter(profile); | 200 router = new InputImeEventRouter(profile); |
| 204 router_map_[profile] = router; | 201 router_map_[profile] = router; |
| 205 } | 202 } |
| 206 return router; | 203 return router; |
| 207 } | 204 } |
| 208 | 205 |
| 209 InputImeEventRouter::InputImeEventRouter(Profile* profile) | |
| 210 : next_request_id_(1), profile_(profile) { | |
| 211 } | |
| 212 | |
| 213 InputImeEventRouter::~InputImeEventRouter() { | |
| 214 } | |
| 215 | |
| 216 IMEEngineHandlerInterface* InputImeEventRouter::GetEngine( | |
| 217 const std::string& extension_id, | |
| 218 const std::string& component_id) { | |
| 219 std::map<std::string, IMEEngineHandlerInterface*>::iterator it = | |
| 220 engine_map_.find(extension_id); | |
| 221 if (it != engine_map_.end()) | |
| 222 return it->second; | |
| 223 return NULL; | |
| 224 } | |
| 225 | |
| 226 IMEEngineHandlerInterface* InputImeEventRouter::GetActiveEngine( | |
| 227 const std::string& extension_id) { | |
| 228 std::map<std::string, IMEEngineHandlerInterface*>::iterator it = | |
| 229 engine_map_.find(extension_id); | |
| 230 if (it != engine_map_.end() && it->second->IsActive()) | |
| 231 return it->second; | |
| 232 return NULL; | |
| 233 } | |
| 234 | |
| 235 void InputImeEventRouter::OnKeyEventHandled( | |
| 236 const std::string& extension_id, | |
| 237 const std::string& request_id, | |
| 238 bool handled) { | |
| 239 RequestMap::iterator request = request_map_.find(request_id); | |
| 240 if (request == request_map_.end()) { | |
| 241 LOG(ERROR) << "Request ID not found: " << request_id; | |
| 242 return; | |
| 243 } | |
| 244 | |
| 245 std::string component_id = request->second.first; | |
| 246 (request->second.second).Run(handled); | |
| 247 request_map_.erase(request); | |
| 248 } | |
| 249 | |
| 250 std::string InputImeEventRouter::AddRequest( | |
| 251 const std::string& component_id, | |
| 252 IMEEngineHandlerInterface::KeyEventDoneCallback& key_data) { | |
| 253 std::string request_id = base::IntToString(next_request_id_); | |
| 254 ++next_request_id_; | |
| 255 | |
| 256 request_map_[request_id] = std::make_pair(component_id, key_data); | |
| 257 | |
| 258 return request_id; | |
| 259 } | |
| 260 | |
| 261 bool InputImeKeyEventHandledFunction::RunAsync() { | 206 bool InputImeKeyEventHandledFunction::RunAsync() { |
| 262 scoped_ptr<KeyEventHandled::Params> params( | 207 scoped_ptr<KeyEventHandled::Params> params( |
| 263 KeyEventHandled::Params::Create(*args_)); | 208 KeyEventHandled::Params::Create(*args_)); |
| 264 GetInputImeEventRouter(Profile::FromBrowserContext(browser_context())) | 209 GetInputImeEventRouter(Profile::FromBrowserContext(browser_context())) |
| 265 ->OnKeyEventHandled(extension_id(), params->request_id, params->response); | 210 ->OnKeyEventHandled(extension_id(), params->request_id, params->response); |
| 266 return true; | 211 return true; |
| 267 } | 212 } |
| 268 | 213 |
| 269 InputImeAPI::InputImeAPI(content::BrowserContext* context) | 214 InputImeAPI::InputImeAPI(content::BrowserContext* context) |
| 270 : browser_context_(context), extension_registry_observer_(this) { | 215 : browser_context_(context), extension_registry_observer_(this) { |
| 271 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context_)); | 216 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context_)); |
| 272 | 217 |
| 273 EventRouter* event_router = EventRouter::Get(browser_context_); | 218 EventRouter* event_router = EventRouter::Get(browser_context_); |
| 274 event_router->RegisterObserver(this, input_ime::OnFocus::kEventName); | 219 event_router->RegisterObserver(this, input_ime::OnFocus::kEventName); |
| 275 } | 220 } |
| 276 | 221 |
| 277 InputImeAPI::~InputImeAPI() { | 222 InputImeAPI::~InputImeAPI() { |
| 278 EventRouter::Get(browser_context_)->UnregisterObserver(this); | 223 EventRouter::Get(browser_context_)->UnregisterObserver(this); |
| 279 } | 224 } |
| 280 | 225 |
| 281 static base::LazyInstance<BrowserContextKeyedAPIFactory<InputImeAPI> > | 226 static base::LazyInstance<BrowserContextKeyedAPIFactory<InputImeAPI> > |
| 282 g_factory = LAZY_INSTANCE_INITIALIZER; | 227 g_factory = LAZY_INSTANCE_INITIALIZER; |
| 283 | 228 |
| 284 // static | 229 // static |
| 285 BrowserContextKeyedAPIFactory<InputImeAPI>* InputImeAPI::GetFactoryInstance() { | 230 BrowserContextKeyedAPIFactory<InputImeAPI>* InputImeAPI::GetFactoryInstance() { |
| 286 return g_factory.Pointer(); | 231 return g_factory.Pointer(); |
| 287 } | 232 } |
| 288 | 233 |
| 289 void InputImeAPI::OnExtensionLoaded(content::BrowserContext* browser_context, | |
| 290 const Extension* extension) { | |
| 291 const std::vector<InputComponentInfo>* input_components = | |
| 292 extensions::InputComponents::GetInputComponents(extension); | |
| 293 if (input_components) | |
| 294 GetInputImeEventRouter(Profile::FromBrowserContext(browser_context)) | |
| 295 ->RegisterImeExtension(extension->id(), *input_components); | |
| 296 } | |
| 297 | |
| 298 void InputImeAPI::OnExtensionUnloaded(content::BrowserContext* browser_context, | |
| 299 const Extension* extension, | |
| 300 UnloadedExtensionInfo::Reason reason) { | |
| 301 const std::vector<InputComponentInfo>* input_components = | |
| 302 extensions::InputComponents::GetInputComponents(extension); | |
| 303 if (!input_components) | |
| 304 return; | |
| 305 if (input_components->size() > 0) { | |
| 306 GetInputImeEventRouter(Profile::FromBrowserContext(browser_context)) | |
| 307 ->UnregisterAllImes(extension->id()); | |
| 308 } | |
| 309 } | |
| 310 | |
| 311 void InputImeAPI::OnListenerAdded(const EventListenerInfo& details) { | |
| 312 if (!details.browser_context) | |
| 313 return; | |
| 314 IMEEngineHandlerInterface* engine = | |
| 315 GetInputImeEventRouter( | |
| 316 Profile::FromBrowserContext(details.browser_context)) | |
| 317 ->GetActiveEngine(details.extension_id); | |
| 318 // Notifies the IME extension for IME ready with onActivate/onFocus events. | |
| 319 if (engine) | |
| 320 engine->Enable(engine->GetActiveComponentId()); | |
| 321 } | |
| 322 | |
| 323 InputImeEventRouter* GetInputImeEventRouter(Profile* profile) { | 234 InputImeEventRouter* GetInputImeEventRouter(Profile* profile) { |
| 324 if (profile->HasOffTheRecordProfile()) | 235 if (profile->HasOffTheRecordProfile()) |
| 325 profile = profile->GetOffTheRecordProfile(); | 236 profile = profile->GetOffTheRecordProfile(); |
| 326 return extensions::InputImeEventRouterFactory::GetInstance()->GetRouter( | 237 return extensions::InputImeEventRouterFactory::GetInstance()->GetRouter( |
| 327 profile); | 238 profile); |
| 328 } | 239 } |
| 329 | 240 |
| 330 } // namespace extensions | 241 } // namespace extensions |
| OLD | NEW |