| 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/chromeos/input_method/input_method_util.h" | 5 #include "chrome/browser/chromeos/input_method/input_method_util.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <functional> | 8 #include <functional> |
| 9 #include <map> | 9 #include <map> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 441 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 452 // Special case for German, French and Dutch: these languages have multiple | 452 // Special case for German, French and Dutch: these languages have multiple |
| 453 // keyboard layouts and share the same layout of keyboard (Belgian). We need | 453 // keyboard layouts and share the same layout of keyboard (Belgian). We need |
| 454 // to show explicitly the language for the layout. For Arabic, Amharic, and | 454 // to show explicitly the language for the layout. For Arabic, Amharic, and |
| 455 // Indic languages: they share "Standard Input Method". | 455 // Indic languages: they share "Standard Input Method". |
| 456 const base::string16 standard_input_method_text = | 456 const base::string16 standard_input_method_text = |
| 457 delegate_->GetLocalizedString( | 457 delegate_->GetLocalizedString( |
| 458 IDS_OPTIONS_SETTINGS_LANGUAGES_M17N_STANDARD_INPUT_METHOD); | 458 IDS_OPTIONS_SETTINGS_LANGUAGES_M17N_STANDARD_INPUT_METHOD); |
| 459 DCHECK(!input_method.language_codes().empty()); | 459 DCHECK(!input_method.language_codes().empty()); |
| 460 const std::string language_code = input_method.language_codes().at(0); | 460 const std::string language_code = input_method.language_codes().at(0); |
| 461 | 461 |
| 462 base::string16 text = TranslateString(input_method.id()); | 462 // Before translate the string, convert the input method id to legacy xkb id |
| 463 // if possible. |
| 464 // TODO(shuchen): the GetInputMethodLongName() method should be removed when |
| 465 // finish the wrapping of xkb to extension. |
| 466 base::string16 text = TranslateString( |
| 467 extension_ime_util::MaybeGetLegacyXkbId(input_method.id())); |
| 463 if (text == standard_input_method_text || | 468 if (text == standard_input_method_text || |
| 464 language_code == "de" || | 469 language_code == "de" || |
| 465 language_code == "fr" || | 470 language_code == "fr" || |
| 466 language_code == "nl") { | 471 language_code == "nl") { |
| 467 const base::string16 language_name = delegate_->GetDisplayLanguageName( | 472 const base::string16 language_name = delegate_->GetDisplayLanguageName( |
| 468 language_code); | 473 language_code); |
| 469 | 474 |
| 470 text = language_name + base::UTF8ToUTF16(" - ") + text; | 475 text = language_name + base::UTF8ToUTF16(" - ") + text; |
| 471 } | 476 } |
| 472 | 477 |
| 473 DCHECK(!text.empty()); | 478 DCHECK(!text.empty()); |
| 474 return text; | 479 return text; |
| 475 } | 480 } |
| 476 | 481 |
| 477 const InputMethodDescriptor* InputMethodUtil::GetInputMethodDescriptorFromId( | 482 const InputMethodDescriptor* InputMethodUtil::GetInputMethodDescriptorFromId( |
| 478 const std::string& input_method_id) const { | 483 const std::string& input_method_id) const { |
| 479 InputMethodIdToDescriptorMap::const_iterator iter | 484 InputMethodIdToDescriptorMap::const_iterator iter |
| 480 = id_to_descriptor_.find(input_method_id); | 485 = id_to_descriptor_.find(input_method_id); |
| 481 return (iter == id_to_descriptor_.end()) ? NULL : &(iter->second); | 486 if (iter == id_to_descriptor_.end()) { |
| 487 // If failed to find the descriptor for given id, it may because of the id |
| 488 // is a component extension xkb id (_comp_ime_...xkb:...). |
| 489 // So try to convert it to legacy xkb id and find again. |
| 490 // This hack is mainly for OOBE session, which requires a sync call to get |
| 491 // the input method descriptor for extension xkb id. |
| 492 // TODO(shuchen): need to support async wait for component extension |
| 493 // loading in OOBE session. This hack won't be needed when it's been done. |
| 494 iter = id_to_descriptor_.find( |
| 495 extension_ime_util::MaybeGetLegacyXkbId(input_method_id)); |
| 496 if (iter == id_to_descriptor_.end()) |
| 497 return NULL; |
| 498 } |
| 499 return &(iter->second); |
| 482 } | 500 } |
| 483 | 501 |
| 484 bool InputMethodUtil::GetInputMethodIdsFromLanguageCode( | 502 bool InputMethodUtil::GetInputMethodIdsFromLanguageCode( |
| 485 const std::string& normalized_language_code, | 503 const std::string& normalized_language_code, |
| 486 InputMethodType type, | 504 InputMethodType type, |
| 487 std::vector<std::string>* out_input_method_ids) const { | 505 std::vector<std::string>* out_input_method_ids) const { |
| 488 return GetInputMethodIdsFromLanguageCodeInternal( | 506 return GetInputMethodIdsFromLanguageCodeInternal( |
| 489 language_code_to_ids_, | 507 language_code_to_ids_, |
| 490 normalized_language_code, type, out_input_method_ids); | 508 normalized_language_code, type, out_input_method_ids); |
| 491 } | 509 } |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 604 const std::string& language_code) { | 622 const std::string& language_code) { |
| 605 std::vector<std::string> candidates; | 623 std::vector<std::string> candidates; |
| 606 GetInputMethodIdsFromLanguageCode( | 624 GetInputMethodIdsFromLanguageCode( |
| 607 language_code, input_method::kKeyboardLayoutsOnly, &candidates); | 625 language_code, input_method::kKeyboardLayoutsOnly, &candidates); |
| 608 if (candidates.size()) | 626 if (candidates.size()) |
| 609 return candidates.front(); | 627 return candidates.front(); |
| 610 | 628 |
| 611 return std::string(); | 629 return std::string(); |
| 612 } | 630 } |
| 613 | 631 |
| 632 bool InputMethodUtil::MigrateXkbInputMethods( |
| 633 std::vector<std::string>* input_method_ids) { |
| 634 bool rewritten = false; |
| 635 std::vector<std::string>& ids = *input_method_ids; |
| 636 for (size_t i = 0; i < ids.size(); ++i) { |
| 637 std::string id = |
| 638 extension_ime_util::GetInputMethodIDByKeyboardLayout(ids[i]); |
| 639 if (id != ids[i]) { |
| 640 ids[i] = id; |
| 641 rewritten = true; |
| 642 } |
| 643 } |
| 644 if (rewritten) { |
| 645 // Removes the duplicates. |
| 646 std::vector<std::string> new_ids; |
| 647 for (size_t i = 0; i < ids.size(); ++i) { |
| 648 if (std::find(new_ids.begin(), new_ids.end(), ids[i]) == new_ids.end()) |
| 649 new_ids.push_back(ids[i]); |
| 650 } |
| 651 ids.swap(new_ids); |
| 652 } |
| 653 return rewritten; |
| 654 } |
| 655 |
| 614 void InputMethodUtil::UpdateHardwareLayoutCache() { | 656 void InputMethodUtil::UpdateHardwareLayoutCache() { |
| 615 DCHECK(thread_checker_.CalledOnValidThread()); | 657 DCHECK(thread_checker_.CalledOnValidThread()); |
| 616 hardware_layouts_.clear(); | 658 hardware_layouts_.clear(); |
| 617 hardware_login_layouts_.clear(); | 659 hardware_login_layouts_.clear(); |
| 618 Tokenize(delegate_->GetHardwareKeyboardLayouts(), ",", &hardware_layouts_); | 660 Tokenize(delegate_->GetHardwareKeyboardLayouts(), ",", &hardware_layouts_); |
| 661 MigrateXkbInputMethods(&hardware_layouts_); |
| 619 | 662 |
| 620 for (size_t i = 0; i < hardware_layouts_.size(); ++i) { | 663 for (size_t i = 0; i < hardware_layouts_.size(); ++i) { |
| 621 if (IsLoginKeyboard(hardware_layouts_[i])) | 664 if (IsLoginKeyboard(hardware_layouts_[i])) |
| 622 hardware_login_layouts_.push_back(hardware_layouts_[i]); | 665 hardware_login_layouts_.push_back(hardware_layouts_[i]); |
| 623 } | 666 } |
| 624 if (hardware_layouts_.empty()) { | 667 if (hardware_layouts_.empty()) { |
| 625 // This is totally fine if it's empty. The hardware keyboard layout is | 668 // This is totally fine if it's empty. The hardware keyboard layout is |
| 626 // not stored if startup_manifest.json (OEM customization data) is not | 669 // not stored if startup_manifest.json (OEM customization data) is not |
| 627 // present (ex. Cr48 doen't have that file). | 670 // present (ex. Cr48 doen't have that file). |
| 628 hardware_layouts_.push_back(GetFallbackInputMethodDescriptor().id()); | 671 hardware_layouts_.push_back(GetFallbackInputMethodDescriptor().id()); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 675 id_to_descriptor_.insert( | 718 id_to_descriptor_.insert( |
| 676 std::make_pair(input_method.id(), input_method)); | 719 std::make_pair(input_method.id(), input_method)); |
| 677 } | 720 } |
| 678 } | 721 } |
| 679 | 722 |
| 680 InputMethodDescriptor InputMethodUtil::GetFallbackInputMethodDescriptor() { | 723 InputMethodDescriptor InputMethodUtil::GetFallbackInputMethodDescriptor() { |
| 681 std::vector<std::string> layouts; | 724 std::vector<std::string> layouts; |
| 682 layouts.push_back("us"); | 725 layouts.push_back("us"); |
| 683 std::vector<std::string> languages; | 726 std::vector<std::string> languages; |
| 684 languages.push_back("en-US"); | 727 languages.push_back("en-US"); |
| 685 return InputMethodDescriptor("xkb:us::eng", | 728 return InputMethodDescriptor( |
| 686 "", | 729 extension_ime_util::GetInputMethodIDByKeyboardLayout("xkb:us::eng"), |
| 687 "US", | 730 "", |
| 688 layouts, | 731 "US", |
| 689 languages, | 732 layouts, |
| 690 true, // login keyboard. | 733 languages, |
| 691 GURL(), // options page, not available. | 734 true, // login keyboard. |
| 692 GURL()); // input view page, not available. | 735 GURL(), // options page, not available. |
| 736 GURL()); // input view page, not available. |
| 693 } | 737 } |
| 694 | 738 |
| 695 void InputMethodUtil::ReloadInternalMaps() { | 739 void InputMethodUtil::ReloadInternalMaps() { |
| 696 if (supported_input_methods_->size() <= 1) { | 740 if (supported_input_methods_->size() <= 1) { |
| 697 DVLOG(1) << "GetSupportedInputMethods returned a fallback ID"; | 741 DVLOG(1) << "GetSupportedInputMethods returned a fallback ID"; |
| 698 // TODO(yusukes): Handle this error in nicer way. | 742 // TODO(yusukes): Handle this error in nicer way. |
| 699 } | 743 } |
| 700 | 744 |
| 701 // Clear the existing maps. | 745 // Clear the existing maps. |
| 702 language_code_to_ids_.clear(); | 746 language_code_to_ids_.clear(); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 721 if (IsKeyboardLayout(input_method.id())) { | 765 if (IsKeyboardLayout(input_method.id())) { |
| 722 xkb_id_to_descriptor_.insert( | 766 xkb_id_to_descriptor_.insert( |
| 723 std::make_pair(input_method.GetPreferredKeyboardLayout(), | 767 std::make_pair(input_method.GetPreferredKeyboardLayout(), |
| 724 input_method)); | 768 input_method)); |
| 725 } | 769 } |
| 726 } | 770 } |
| 727 } | 771 } |
| 728 | 772 |
| 729 } // namespace input_method | 773 } // namespace input_method |
| 730 } // namespace chromeos | 774 } // namespace chromeos |
| OLD | NEW |