Chromium Code Reviews| 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 <utility> |
| 8 | 8 |
| 9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "chrome/common/extensions/api/input_ime.h" | 10 #include "chrome/common/extensions/api/input_ime.h" |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 179 } | 179 } |
| 180 | 180 |
| 181 bool ImeObserver::ConvertInputContextSpellCheck( | 181 bool ImeObserver::ConvertInputContextSpellCheck( |
| 182 ui::IMEEngineHandlerInterface::InputContext input_context) { | 182 ui::IMEEngineHandlerInterface::InputContext input_context) { |
| 183 return !(input_context.flags & ui::TEXT_INPUT_FLAG_SPELLCHECK_OFF); | 183 return !(input_context.flags & ui::TEXT_INPUT_FLAG_SPELLCHECK_OFF); |
| 184 } | 184 } |
| 185 | 185 |
| 186 } // namespace ui | 186 } // namespace ui |
| 187 | 187 |
| 188 namespace extensions { | 188 namespace extensions { |
| 189 bool InputImeKeyEventHandledFunction::RunAsync() { | |
|
Shu Chen
2016/01/05 15:10:47
nit: add blank line above.
also, please move this
Azure Wei
2016/01/06 06:53:47
Done.
| |
| 190 scoped_ptr<KeyEventHandled::Params> params( | |
| 191 KeyEventHandled::Params::Create(*args_)); | |
| 192 GetInputImeEventRouter(Profile::FromBrowserContext(browser_context())) | |
| 193 ->OnKeyEventHandled(extension_id(), params->request_id, params->response); | |
| 194 return true; | |
| 195 } | |
| 189 | 196 |
| 190 InputImeEventRouterFactory* InputImeEventRouterFactory::GetInstance() { | 197 InputImeEventRouterFactory* InputImeEventRouterFactory::GetInstance() { |
| 191 return base::Singleton<InputImeEventRouterFactory>::get(); | 198 return base::Singleton<InputImeEventRouterFactory>::get(); |
| 192 } | 199 } |
| 193 | 200 |
| 194 InputImeEventRouterFactory::InputImeEventRouterFactory() { | 201 InputImeEventRouterFactory::InputImeEventRouterFactory() { |
| 195 } | 202 } |
| 196 | 203 |
| 197 InputImeEventRouterFactory::~InputImeEventRouterFactory() { | 204 InputImeEventRouterFactory::~InputImeEventRouterFactory() { |
| 198 } | 205 } |
| 199 | 206 |
| 200 InputImeEventRouter* InputImeEventRouterFactory::GetRouter(Profile* profile) { | 207 InputImeEventRouter* InputImeEventRouterFactory::GetRouter(Profile* profile) { |
| 201 InputImeEventRouter* router = router_map_[profile]; | 208 InputImeEventRouter* router = router_map_[profile]; |
| 202 if (!router) { | 209 if (!router) { |
| 203 router = new InputImeEventRouter(profile); | 210 router = new InputImeEventRouter(profile); |
| 204 router_map_[profile] = router; | 211 router_map_[profile] = router; |
| 205 } | 212 } |
| 206 return router; | 213 return router; |
| 207 } | 214 } |
| 208 | 215 |
| 209 InputImeEventRouter::InputImeEventRouter(Profile* profile) | 216 InputImeEventRouter::InputImeEventRouter(Profile* profile) |
| 210 : next_request_id_(1), profile_(profile) { | 217 : next_request_id_(1), profile_(profile) { |
| 211 } | 218 } |
| 212 | 219 |
| 213 InputImeEventRouter::~InputImeEventRouter() { | 220 InputImeEventRouter::~InputImeEventRouter() { |
| 214 } | 221 } |
| 215 | 222 |
| 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( | 223 void InputImeEventRouter::OnKeyEventHandled( |
| 236 const std::string& extension_id, | 224 const std::string& extension_id, |
| 237 const std::string& request_id, | 225 const std::string& request_id, |
| 238 bool handled) { | 226 bool handled) { |
| 239 RequestMap::iterator request = request_map_.find(request_id); | 227 RequestMap::iterator request = request_map_.find(request_id); |
| 240 if (request == request_map_.end()) { | 228 if (request == request_map_.end()) { |
| 241 LOG(ERROR) << "Request ID not found: " << request_id; | 229 LOG(ERROR) << "Request ID not found: " << request_id; |
| 242 return; | 230 return; |
| 243 } | 231 } |
| 244 | 232 |
| 245 std::string component_id = request->second.first; | 233 std::string component_id = request->second.first; |
| 246 (request->second.second).Run(handled); | 234 (request->second.second).Run(handled); |
| 247 request_map_.erase(request); | 235 request_map_.erase(request); |
| 248 } | 236 } |
| 249 | 237 |
| 250 std::string InputImeEventRouter::AddRequest( | 238 std::string InputImeEventRouter::AddRequest( |
| 251 const std::string& component_id, | 239 const std::string& component_id, |
| 252 IMEEngineHandlerInterface::KeyEventDoneCallback& key_data) { | 240 IMEEngineHandlerInterface::KeyEventDoneCallback& key_data) { |
| 253 std::string request_id = base::IntToString(next_request_id_); | 241 std::string request_id = base::IntToString(next_request_id_); |
| 254 ++next_request_id_; | 242 ++next_request_id_; |
| 255 | 243 |
| 256 request_map_[request_id] = std::make_pair(component_id, key_data); | 244 request_map_[request_id] = std::make_pair(component_id, key_data); |
| 257 | 245 |
| 258 return request_id; | 246 return request_id; |
| 259 } | 247 } |
| 260 | 248 |
| 261 bool InputImeKeyEventHandledFunction::RunAsync() { | |
| 262 scoped_ptr<KeyEventHandled::Params> params( | |
| 263 KeyEventHandled::Params::Create(*args_)); | |
| 264 GetInputImeEventRouter(Profile::FromBrowserContext(browser_context())) | |
| 265 ->OnKeyEventHandled(extension_id(), params->request_id, params->response); | |
| 266 return true; | |
| 267 } | |
| 268 | |
| 269 InputImeAPI::InputImeAPI(content::BrowserContext* context) | 249 InputImeAPI::InputImeAPI(content::BrowserContext* context) |
| 270 : browser_context_(context), extension_registry_observer_(this) { | 250 : browser_context_(context), extension_registry_observer_(this) { |
| 271 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context_)); | 251 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context_)); |
| 272 | 252 |
| 273 EventRouter* event_router = EventRouter::Get(browser_context_); | 253 EventRouter* event_router = EventRouter::Get(browser_context_); |
| 274 event_router->RegisterObserver(this, input_ime::OnFocus::kEventName); | 254 event_router->RegisterObserver(this, input_ime::OnFocus::kEventName); |
| 275 } | 255 } |
| 276 | 256 |
| 277 InputImeAPI::~InputImeAPI() { | 257 InputImeAPI::~InputImeAPI() { |
| 278 EventRouter::Get(browser_context_)->UnregisterObserver(this); | 258 EventRouter::Get(browser_context_)->UnregisterObserver(this); |
| 279 } | 259 } |
| 280 | 260 |
| 281 static base::LazyInstance<BrowserContextKeyedAPIFactory<InputImeAPI> > | 261 static base::LazyInstance<BrowserContextKeyedAPIFactory<InputImeAPI> > |
| 282 g_factory = LAZY_INSTANCE_INITIALIZER; | 262 g_factory = LAZY_INSTANCE_INITIALIZER; |
| 283 | 263 |
| 284 // static | 264 // static |
| 285 BrowserContextKeyedAPIFactory<InputImeAPI>* InputImeAPI::GetFactoryInstance() { | 265 BrowserContextKeyedAPIFactory<InputImeAPI>* InputImeAPI::GetFactoryInstance() { |
| 286 return g_factory.Pointer(); | 266 return g_factory.Pointer(); |
| 287 } | 267 } |
| 288 | 268 |
| 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) { | 269 InputImeEventRouter* GetInputImeEventRouter(Profile* profile) { |
| 324 if (profile->HasOffTheRecordProfile()) | 270 if (profile->HasOffTheRecordProfile()) |
| 325 profile = profile->GetOffTheRecordProfile(); | 271 profile = profile->GetOffTheRecordProfile(); |
| 326 return extensions::InputImeEventRouterFactory::GetInstance()->GetRouter( | 272 return extensions::InputImeEventRouterFactory::GetInstance()->GetRouter( |
| 327 profile); | 273 profile); |
| 328 } | 274 } |
| 329 | 275 |
| 330 } // namespace extensions | 276 } // namespace extensions |
| OLD | NEW |