Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(342)

Side by Side Diff: chrome/browser/extensions/api/input_ime/input_ime_api_chromeos.cc

Issue 1570723002: Add class extensions::InputImeEventRouterBase (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed Devlin's comments. Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 <stddef.h> 7 #include <stddef.h>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/macros.h" 10 #include "base/macros.h"
11 #include "chrome/browser/chromeos/input_method/input_method_engine.h" 11 #include "chrome/browser/chromeos/input_method/input_method_engine.h"
12 #include "chrome/browser/chromeos/login/lock/screen_locker.h" 12 #include "chrome/browser/chromeos/login/lock/screen_locker.h"
13 #include "chrome/browser/chromeos/login/session/user_session_manager.h" 13 #include "chrome/browser/chromeos/login/session/user_session_manager.h"
14 #include "chrome/browser/chromeos/login/ui/user_adding_screen.h" 14 #include "chrome/browser/chromeos/login/ui/user_adding_screen.h"
15 #include "chrome/browser/chromeos/profiles/profile_helper.h" 15 #include "chrome/browser/chromeos/profiles/profile_helper.h"
16 #include "chrome/browser/extensions/extension_service.h" 16 #include "chrome/browser/extensions/extension_service.h"
17 #include "chrome/browser/profiles/profile_manager.h" 17 #include "chrome/browser/profiles/profile_manager.h"
18 #include "chrome/common/extensions/api/input_ime.h" 18 #include "chrome/common/extensions/api/input_ime.h"
19 #include "chrome/common/extensions/api/input_ime/input_components_handler.h"
20 #include "extensions/browser/extension_system.h" 19 #include "extensions/browser/extension_system.h"
21 #include "extensions/common/manifest_handlers/background_info.h" 20 #include "extensions/common/manifest_handlers/background_info.h"
22 #include "ui/base/ime/chromeos/component_extension_ime_manager.h" 21 #include "ui/base/ime/chromeos/component_extension_ime_manager.h"
23 #include "ui/base/ime/chromeos/extension_ime_util.h" 22 #include "ui/base/ime/chromeos/extension_ime_util.h"
24 #include "ui/base/ime/chromeos/input_method_manager.h" 23 #include "ui/base/ime/chromeos/input_method_manager.h"
25 #include "ui/base/ime/ime_engine_handler_interface.h" 24 #include "ui/base/ime/ime_engine_handler_interface.h"
26 25
27 namespace input_ime = extensions::api::input_ime; 26 namespace input_ime = extensions::api::input_ime;
28 namespace DeleteSurroundingText = 27 namespace DeleteSurroundingText =
29 extensions::api::input_ime::DeleteSurroundingText; 28 extensions::api::input_ime::DeleteSurroundingText;
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after
279 return "normal"; 278 return "normal";
280 } 279 }
281 280
282 DISALLOW_COPY_AND_ASSIGN(ImeObserverChromeOS); 281 DISALLOW_COPY_AND_ASSIGN(ImeObserverChromeOS);
283 }; 282 };
284 283
285 } // namespace 284 } // namespace
286 285
287 namespace extensions { 286 namespace extensions {
288 287
288 InputImeEventRouter::InputImeEventRouter(Profile* profile)
289 : InputImeEventRouterBase(profile) {}
290
291 InputImeEventRouter::~InputImeEventRouter() {}
292
289 bool InputImeEventRouter::RegisterImeExtension( 293 bool InputImeEventRouter::RegisterImeExtension(
290 const std::string& extension_id, 294 const std::string& extension_id,
291 const std::vector<extensions::InputComponentInfo>& input_components) { 295 const std::vector<InputComponentInfo>& input_components) {
292 VLOG(1) << "RegisterImeExtension: " << extension_id; 296 VLOG(1) << "RegisterImeExtension: " << extension_id;
293 297
294 if (engine_map_[extension_id]) 298 if (engine_map_[extension_id])
295 return false; 299 return false;
296 300
297 chromeos::input_method::InputMethodManager* manager = 301 chromeos::input_method::InputMethodManager* manager =
298 chromeos::input_method::InputMethodManager::Get(); 302 chromeos::input_method::InputMethodManager::Get();
299 chromeos::ComponentExtensionIMEManager* comp_ext_ime_manager = 303 chromeos::ComponentExtensionIMEManager* comp_ext_ime_manager =
300 manager->GetComponentExtensionIMEManager(); 304 manager->GetComponentExtensionIMEManager();
301 305
302 chromeos::input_method::InputMethodDescriptors descriptors; 306 chromeos::input_method::InputMethodDescriptors descriptors;
303 // Only creates descriptors for 3rd party IME extension, because the 307 // Only creates descriptors for 3rd party IME extension, because the
304 // descriptors for component IME extensions are managed by InputMethodUtil. 308 // descriptors for component IME extensions are managed by InputMethodUtil.
305 if (!comp_ext_ime_manager->IsWhitelistedExtension(extension_id)) { 309 if (!comp_ext_ime_manager->IsWhitelistedExtension(extension_id)) {
306 for (std::vector<extensions::InputComponentInfo>::const_iterator it = 310 for (std::vector<InputComponentInfo>::const_iterator it =
307 input_components.begin(); 311 input_components.begin();
308 it != input_components.end(); 312 it != input_components.end();
309 ++it) { 313 ++it) {
310 const extensions::InputComponentInfo& component = *it; 314 const InputComponentInfo& component = *it;
311 DCHECK(component.type == extensions::INPUT_COMPONENT_TYPE_IME); 315 DCHECK(component.type == INPUT_COMPONENT_TYPE_IME);
312 316
313 std::vector<std::string> layouts; 317 std::vector<std::string> layouts;
314 layouts.assign(component.layouts.begin(), component.layouts.end()); 318 layouts.assign(component.layouts.begin(), component.layouts.end());
315 std::vector<std::string> languages; 319 std::vector<std::string> languages;
316 languages.assign(component.languages.begin(), component.languages.end()); 320 languages.assign(component.languages.begin(), component.languages.end());
317 321
318 const std::string& input_method_id = 322 const std::string& input_method_id =
319 chromeos::extension_ime_util::GetInputMethodID(extension_id, 323 chromeos::extension_ime_util::GetInputMethodID(extension_id,
320 component.id); 324 component.id);
321 descriptors.push_back(chromeos::input_method::InputMethodDescriptor( 325 descriptors.push_back(chromeos::input_method::InputMethodDescriptor(
322 input_method_id, 326 input_method_id,
323 component.name, 327 component.name,
324 std::string(), // TODO(uekawa): Set short name. 328 std::string(), // TODO(uekawa): Set short name.
325 layouts, 329 layouts,
326 languages, 330 languages,
327 false, // 3rd party IMEs are always not for login. 331 false, // 3rd party IMEs are always not for login.
328 component.options_page_url, 332 component.options_page_url,
329 component.input_view_url)); 333 component.input_view_url));
330 } 334 }
331 } 335 }
332 336
333 scoped_ptr<ui::IMEEngineObserver> observer( 337 scoped_ptr<ui::IMEEngineObserver> observer(
334 new ImeObserverChromeOS(extension_id, profile_)); 338 new ImeObserverChromeOS(extension_id, profile()));
335 chromeos::InputMethodEngine* engine = new chromeos::InputMethodEngine(); 339 chromeos::InputMethodEngine* engine = new chromeos::InputMethodEngine();
336 engine->Initialize(std::move(observer), extension_id.c_str(), profile_); 340 engine->Initialize(std::move(observer), extension_id.c_str(), profile());
337 engine_map_[extension_id] = engine; 341 engine_map_[extension_id] = engine;
338 chromeos::UserSessionManager::GetInstance() 342 chromeos::UserSessionManager::GetInstance()
339 ->GetDefaultIMEState(profile_) 343 ->GetDefaultIMEState(profile())
340 ->AddInputMethodExtension(extension_id, descriptors, engine); 344 ->AddInputMethodExtension(extension_id, descriptors, engine);
341 345
342 return true; 346 return true;
343 } 347 }
344 348
345 void InputImeEventRouter::UnregisterAllImes(const std::string& extension_id) { 349 void InputImeEventRouter::UnregisterAllImes(const std::string& extension_id) {
346 std::map<std::string, IMEEngineHandlerInterface*>::iterator it = 350 std::map<std::string, IMEEngineHandlerInterface*>::iterator it =
347 engine_map_.find(extension_id); 351 engine_map_.find(extension_id);
348 if (it != engine_map_.end()) { 352 if (it != engine_map_.end()) {
349 chromeos::input_method::InputMethodManager::Get() 353 chromeos::input_method::InputMethodManager::Get()
350 ->GetActiveIMEState() 354 ->GetActiveIMEState()
351 ->RemoveInputMethodExtension(extension_id); 355 ->RemoveInputMethodExtension(extension_id);
352 delete it->second; 356 delete it->second;
353 engine_map_.erase(it); 357 engine_map_.erase(it);
354 } 358 }
355 } 359 }
356 360
361 IMEEngineHandlerInterface* InputImeEventRouter::GetEngine(
362 const std::string& extension_id,
363 const std::string& component_id) {
364 std::map<std::string, IMEEngineHandlerInterface*>::iterator it =
365 engine_map_.find(extension_id);
366 return (it != engine_map_.end()) ? it->second : nullptr;
367 }
368
369 IMEEngineHandlerInterface* InputImeEventRouter::GetActiveEngine(
370 const std::string& extension_id) {
371 std::map<std::string, IMEEngineHandlerInterface*>::iterator it =
372 engine_map_.find(extension_id);
373 return (it != engine_map_.end() && it->second->IsActive()) ? it->second
374 : nullptr;
375 }
376
357 bool InputImeSetCompositionFunction::RunSync() { 377 bool InputImeSetCompositionFunction::RunSync() {
358 IMEEngineHandlerInterface* engine = 378 IMEEngineHandlerInterface* engine =
359 GetInputImeEventRouter(Profile::FromBrowserContext(browser_context())) 379 GetInputImeEventRouter(Profile::FromBrowserContext(browser_context()))
360 ->GetActiveEngine(extension_id()); 380 ->GetActiveEngine(extension_id());
361 if (!engine) { 381 if (!engine) {
362 SetResult(new base::FundamentalValue(false)); 382 SetResult(new base::FundamentalValue(false));
363 return true; 383 return true;
364 } 384 }
365 385
366 scoped_ptr<SetComposition::Params> parent_params( 386 scoped_ptr<SetComposition::Params> parent_params(
(...skipping 318 matching lines...) Expand 10 before | Expand all | Expand 10 after
685 if (!engine) { 705 if (!engine) {
686 error_ = kErrorEngineNotAvailable; 706 error_ = kErrorEngineNotAvailable;
687 return false; 707 return false;
688 } 708 }
689 709
690 engine->DeleteSurroundingText(params.context_id, params.offset, params.length, 710 engine->DeleteSurroundingText(params.context_id, params.offset, params.length,
691 &error_); 711 &error_);
692 return true; 712 return true;
693 } 713 }
694 714
715 void InputImeAPI::OnExtensionLoaded(content::BrowserContext* browser_context,
716 const Extension* extension) {
717 const std::vector<InputComponentInfo>* input_components =
718 InputComponents::GetInputComponents(extension);
719 if (input_components)
720 GetInputImeEventRouter(Profile::FromBrowserContext(browser_context))
721 ->RegisterImeExtension(extension->id(), *input_components);
722 }
723
724 void InputImeAPI::OnExtensionUnloaded(content::BrowserContext* browser_context,
725 const Extension* extension,
726 UnloadedExtensionInfo::Reason reason) {
727 const std::vector<InputComponentInfo>* input_components =
728 InputComponents::GetInputComponents(extension);
729 if (input_components && !input_components->empty()) {
730 GetInputImeEventRouter(Profile::FromBrowserContext(browser_context))
731 ->UnregisterAllImes(extension->id());
732 }
733 }
734
735 void InputImeAPI::OnListenerAdded(const EventListenerInfo& details) {
736 if (!details.browser_context)
737 return;
738 IMEEngineHandlerInterface* engine =
739 GetInputImeEventRouter(
740 Profile::FromBrowserContext(details.browser_context))
741 ->GetActiveEngine(details.extension_id);
742 // Notifies the IME extension for IME ready with onActivate/onFocus events.
743 if (engine)
744 engine->Enable(engine->GetActiveComponentId());
745 }
746
695 } // namespace extensions 747 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698