| OLD | NEW |
| 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" |
| (...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 279 return "normal"; | 279 return "normal"; |
| 280 } | 280 } |
| 281 | 281 |
| 282 DISALLOW_COPY_AND_ASSIGN(ImeObserverChromeOS); | 282 DISALLOW_COPY_AND_ASSIGN(ImeObserverChromeOS); |
| 283 }; | 283 }; |
| 284 | 284 |
| 285 } // namespace | 285 } // namespace |
| 286 | 286 |
| 287 namespace extensions { | 287 namespace extensions { |
| 288 | 288 |
| 289 bool InputImeEventRouter::RegisterImeExtension( | |
| 290 const std::string& extension_id, | |
| 291 const std::vector<extensions::InputComponentInfo>& input_components) { | |
| 292 VLOG(1) << "RegisterImeExtension: " << extension_id; | |
| 293 | |
| 294 if (engine_map_[extension_id]) | |
| 295 return false; | |
| 296 | |
| 297 chromeos::input_method::InputMethodManager* manager = | |
| 298 chromeos::input_method::InputMethodManager::Get(); | |
| 299 chromeos::ComponentExtensionIMEManager* comp_ext_ime_manager = | |
| 300 manager->GetComponentExtensionIMEManager(); | |
| 301 | |
| 302 chromeos::input_method::InputMethodDescriptors descriptors; | |
| 303 // Only creates descriptors for 3rd party IME extension, because the | |
| 304 // descriptors for component IME extensions are managed by InputMethodUtil. | |
| 305 if (!comp_ext_ime_manager->IsWhitelistedExtension(extension_id)) { | |
| 306 for (std::vector<extensions::InputComponentInfo>::const_iterator it = | |
| 307 input_components.begin(); | |
| 308 it != input_components.end(); | |
| 309 ++it) { | |
| 310 const extensions::InputComponentInfo& component = *it; | |
| 311 DCHECK(component.type == extensions::INPUT_COMPONENT_TYPE_IME); | |
| 312 | |
| 313 std::vector<std::string> layouts; | |
| 314 layouts.assign(component.layouts.begin(), component.layouts.end()); | |
| 315 std::vector<std::string> languages; | |
| 316 languages.assign(component.languages.begin(), component.languages.end()); | |
| 317 | |
| 318 const std::string& input_method_id = | |
| 319 chromeos::extension_ime_util::GetInputMethodID(extension_id, | |
| 320 component.id); | |
| 321 descriptors.push_back(chromeos::input_method::InputMethodDescriptor( | |
| 322 input_method_id, | |
| 323 component.name, | |
| 324 std::string(), // TODO(uekawa): Set short name. | |
| 325 layouts, | |
| 326 languages, | |
| 327 false, // 3rd party IMEs are always not for login. | |
| 328 component.options_page_url, | |
| 329 component.input_view_url)); | |
| 330 } | |
| 331 } | |
| 332 | |
| 333 scoped_ptr<ui::IMEEngineObserver> observer( | |
| 334 new ImeObserverChromeOS(extension_id, profile_)); | |
| 335 chromeos::InputMethodEngine* engine = new chromeos::InputMethodEngine(); | |
| 336 engine->Initialize(std::move(observer), extension_id.c_str(), profile_); | |
| 337 engine_map_[extension_id] = engine; | |
| 338 chromeos::UserSessionManager::GetInstance() | |
| 339 ->GetDefaultIMEState(profile_) | |
| 340 ->AddInputMethodExtension(extension_id, descriptors, engine); | |
| 341 | |
| 342 return true; | |
| 343 } | |
| 344 | |
| 345 void InputImeEventRouter::UnregisterAllImes(const std::string& extension_id) { | |
| 346 std::map<std::string, IMEEngineHandlerInterface*>::iterator it = | |
| 347 engine_map_.find(extension_id); | |
| 348 if (it != engine_map_.end()) { | |
| 349 chromeos::input_method::InputMethodManager::Get() | |
| 350 ->GetActiveIMEState() | |
| 351 ->RemoveInputMethodExtension(extension_id); | |
| 352 delete it->second; | |
| 353 engine_map_.erase(it); | |
| 354 } | |
| 355 } | |
| 356 | |
| 357 bool InputImeSetCompositionFunction::RunSync() { | 289 bool InputImeSetCompositionFunction::RunSync() { |
| 358 IMEEngineHandlerInterface* engine = | 290 IMEEngineHandlerInterface* engine = |
| 359 GetInputImeEventRouter(Profile::FromBrowserContext(browser_context())) | 291 GetInputImeEventRouter(Profile::FromBrowserContext(browser_context())) |
| 360 ->GetActiveEngine(extension_id()); | 292 ->GetActiveEngine(extension_id()); |
| 361 if (!engine) { | 293 if (!engine) { |
| 362 SetResult(new base::FundamentalValue(false)); | 294 SetResult(new base::FundamentalValue(false)); |
| 363 return true; | 295 return true; |
| 364 } | 296 } |
| 365 | 297 |
| 366 scoped_ptr<SetComposition::Params> parent_params( | 298 scoped_ptr<SetComposition::Params> parent_params( |
| (...skipping 318 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 685 if (!engine) { | 617 if (!engine) { |
| 686 error_ = kErrorEngineNotAvailable; | 618 error_ = kErrorEngineNotAvailable; |
| 687 return false; | 619 return false; |
| 688 } | 620 } |
| 689 | 621 |
| 690 engine->DeleteSurroundingText(params.context_id, params.offset, params.length, | 622 engine->DeleteSurroundingText(params.context_id, params.offset, params.length, |
| 691 &error_); | 623 &error_); |
| 692 return true; | 624 return true; |
| 693 } | 625 } |
| 694 | 626 |
| 627 bool InputImeEventRouter::RegisterImeExtension( |
| 628 const std::string& extension_id, |
| 629 const std::vector<extensions::InputComponentInfo>& input_components) { |
| 630 VLOG(1) << "RegisterImeExtension: " << extension_id; |
| 631 |
| 632 if (engine_map_[extension_id]) |
| 633 return false; |
| 634 |
| 635 chromeos::input_method::InputMethodManager* manager = |
| 636 chromeos::input_method::InputMethodManager::Get(); |
| 637 chromeos::ComponentExtensionIMEManager* comp_ext_ime_manager = |
| 638 manager->GetComponentExtensionIMEManager(); |
| 639 |
| 640 chromeos::input_method::InputMethodDescriptors descriptors; |
| 641 // Only creates descriptors for 3rd party IME extension, because the |
| 642 // descriptors for component IME extensions are managed by InputMethodUtil. |
| 643 if (!comp_ext_ime_manager->IsWhitelistedExtension(extension_id)) { |
| 644 for (std::vector<extensions::InputComponentInfo>::const_iterator it = |
| 645 input_components.begin(); |
| 646 it != input_components.end(); |
| 647 ++it) { |
| 648 const extensions::InputComponentInfo& component = *it; |
| 649 DCHECK(component.type == extensions::INPUT_COMPONENT_TYPE_IME); |
| 650 |
| 651 std::vector<std::string> layouts; |
| 652 layouts.assign(component.layouts.begin(), component.layouts.end()); |
| 653 std::vector<std::string> languages; |
| 654 languages.assign(component.languages.begin(), component.languages.end()); |
| 655 |
| 656 const std::string& input_method_id = |
| 657 chromeos::extension_ime_util::GetInputMethodID(extension_id, |
| 658 component.id); |
| 659 descriptors.push_back(chromeos::input_method::InputMethodDescriptor( |
| 660 input_method_id, |
| 661 component.name, |
| 662 std::string(), // TODO(uekawa): Set short name. |
| 663 layouts, |
| 664 languages, |
| 665 false, // 3rd party IMEs are always not for login. |
| 666 component.options_page_url, |
| 667 component.input_view_url)); |
| 668 } |
| 669 } |
| 670 |
| 671 scoped_ptr<ui::IMEEngineObserver> observer( |
| 672 new ImeObserverChromeOS(extension_id, profile_)); |
| 673 chromeos::InputMethodEngine* engine = new chromeos::InputMethodEngine(); |
| 674 engine->Initialize(std::move(observer), extension_id.c_str(), profile_); |
| 675 engine_map_[extension_id] = engine; |
| 676 chromeos::UserSessionManager::GetInstance() |
| 677 ->GetDefaultIMEState(profile_) |
| 678 ->AddInputMethodExtension(extension_id, descriptors, engine); |
| 679 |
| 680 return true; |
| 681 } |
| 682 |
| 683 void InputImeEventRouter::UnregisterAllImes(const std::string& extension_id) { |
| 684 std::map<std::string, IMEEngineHandlerInterface*>::iterator it = |
| 685 engine_map_.find(extension_id); |
| 686 if (it != engine_map_.end()) { |
| 687 chromeos::input_method::InputMethodManager::Get() |
| 688 ->GetActiveIMEState() |
| 689 ->RemoveInputMethodExtension(extension_id); |
| 690 delete it->second; |
| 691 engine_map_.erase(it); |
| 692 } |
| 693 } |
| 694 |
| 695 IMEEngineHandlerInterface* InputImeEventRouter::GetEngine( |
| 696 const std::string& extension_id, |
| 697 const std::string& component_id) { |
| 698 std::map<std::string, IMEEngineHandlerInterface*>::iterator it = |
| 699 engine_map_.find(extension_id); |
| 700 if (it != engine_map_.end()) |
| 701 return it->second; |
| 702 return NULL; |
| 703 } |
| 704 |
| 705 IMEEngineHandlerInterface* InputImeEventRouter::GetActiveEngine( |
| 706 const std::string& extension_id) { |
| 707 std::map<std::string, IMEEngineHandlerInterface*>::iterator it = |
| 708 engine_map_.find(extension_id); |
| 709 if (it != engine_map_.end() && it->second->IsActive()) |
| 710 return it->second; |
| 711 return NULL; |
| 712 } |
| 713 |
| 714 void InputImeAPI::OnExtensionLoaded(content::BrowserContext* browser_context, |
| 715 const Extension* extension) { |
| 716 const std::vector<InputComponentInfo>* input_components = |
| 717 extensions::InputComponents::GetInputComponents(extension); |
| 718 if (input_components) |
| 719 GetInputImeEventRouter(Profile::FromBrowserContext(browser_context)) |
| 720 ->RegisterImeExtension(extension->id(), *input_components); |
| 721 } |
| 722 |
| 723 void InputImeAPI::OnExtensionUnloaded(content::BrowserContext* browser_context, |
| 724 const Extension* extension, |
| 725 UnloadedExtensionInfo::Reason reason) { |
| 726 const std::vector<InputComponentInfo>* input_components = |
| 727 extensions::InputComponents::GetInputComponents(extension); |
| 728 if (!input_components) |
| 729 return; |
| 730 if (input_components->size() > 0) { |
| 731 GetInputImeEventRouter(Profile::FromBrowserContext(browser_context)) |
| 732 ->UnregisterAllImes(extension->id()); |
| 733 } |
| 734 } |
| 735 |
| 736 void InputImeAPI::OnListenerAdded(const EventListenerInfo& details) { |
| 737 if (!details.browser_context) |
| 738 return; |
| 739 IMEEngineHandlerInterface* engine = |
| 740 GetInputImeEventRouter( |
| 741 Profile::FromBrowserContext(details.browser_context)) |
| 742 ->GetActiveEngine(details.extension_id); |
| 743 // Notifies the IME extension for IME ready with onActivate/onFocus events. |
| 744 if (engine) |
| 745 engine->Enable(engine->GetActiveComponentId()); |
| 746 } |
| 747 |
| 695 } // namespace extensions | 748 } // namespace extensions |
| OLD | NEW |