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

Side by Side Diff: chrome/browser/chromeos/input_method/input_method_util.cc

Issue 5711001: Add a new GetInstance() method for remaining files with singleton classes under chrome/browser. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 10 years 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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 <map> 8 #include <map>
9 #include <utility> 9 #include <utility>
10 10
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
102 }; 102 };
103 103
104 // Map from language code to associated input method IDs, etc. 104 // Map from language code to associated input method IDs, etc.
105 typedef std::multimap<std::string, std::string> LanguageCodeToIdsMap; 105 typedef std::multimap<std::string, std::string> LanguageCodeToIdsMap;
106 struct IdMaps { 106 struct IdMaps {
107 scoped_ptr<LanguageCodeToIdsMap> language_code_to_ids; 107 scoped_ptr<LanguageCodeToIdsMap> language_code_to_ids;
108 scoped_ptr<std::map<std::string, std::string> > id_to_language_code; 108 scoped_ptr<std::map<std::string, std::string> > id_to_language_code;
109 scoped_ptr<std::map<std::string, std::string> > id_to_display_name; 109 scoped_ptr<std::map<std::string, std::string> > id_to_display_name;
110 scoped_ptr<std::map<std::string, std::string> > id_to_keyboard_overlay_id; 110 scoped_ptr<std::map<std::string, std::string> > id_to_keyboard_overlay_id;
111 111
112 // Returns the singleton instance.
113 static IdMaps* GetInstance() {
114 return Singleton<IdMaps>::get();
115 }
116
112 void ReloadMaps() { 117 void ReloadMaps() {
113 chromeos::InputMethodLibrary* library = 118 chromeos::InputMethodLibrary* library =
114 chromeos::CrosLibrary::Get()->GetInputMethodLibrary(); 119 chromeos::CrosLibrary::Get()->GetInputMethodLibrary();
115 scoped_ptr<chromeos::InputMethodDescriptors> supported_input_methods( 120 scoped_ptr<chromeos::InputMethodDescriptors> supported_input_methods(
116 library->GetSupportedInputMethods()); 121 library->GetSupportedInputMethods());
117 if (supported_input_methods->size() <= 1) { 122 if (supported_input_methods->size() <= 1) {
118 LOG(ERROR) << "GetSupportedInputMethods returned a fallback ID"; 123 LOG(ERROR) << "GetSupportedInputMethods returned a fallback ID";
119 // TODO(yusukes): Handle this error in nicer way. 124 // TODO(yusukes): Handle this error in nicer way.
120 } 125 }
121 126
(...skipping 407 matching lines...) Expand 10 before | Expand all | Expand 10 after
529 } 534 }
530 return language_code; 535 return language_code;
531 } 536 }
532 537
533 std::string GetLanguageCodeFromInputMethodId( 538 std::string GetLanguageCodeFromInputMethodId(
534 const std::string& input_method_id) { 539 const std::string& input_method_id) {
535 // The code should be compatible with one of codes used for UI languages, 540 // The code should be compatible with one of codes used for UI languages,
536 // defined in app/l10_util.cc. 541 // defined in app/l10_util.cc.
537 const char kDefaultLanguageCode[] = "en-US"; 542 const char kDefaultLanguageCode[] = "en-US";
538 std::map<std::string, std::string>::const_iterator iter 543 std::map<std::string, std::string>::const_iterator iter
539 = Singleton<IdMaps>::get()->id_to_language_code->find(input_method_id); 544 = IdMaps::GetInstance()->id_to_language_code->find(input_method_id);
540 return (iter == Singleton<IdMaps>::get()->id_to_language_code->end()) ? 545 return (iter == IdMaps::GetInstance()->id_to_language_code->end()) ?
541 // Returning |kDefaultLanguageCode| here is not for Chrome OS but for 546 // Returning |kDefaultLanguageCode| here is not for Chrome OS but for
542 // Ubuntu where the ibus-xkb-layouts engine could be missing. 547 // Ubuntu where the ibus-xkb-layouts engine could be missing.
543 kDefaultLanguageCode : iter->second; 548 kDefaultLanguageCode : iter->second;
544 } 549 }
545 550
546 std::string GetKeyboardLayoutName(const std::string& input_method_id) { 551 std::string GetKeyboardLayoutName(const std::string& input_method_id) {
547 if (!StartsWithASCII(input_method_id, "xkb:", true)) { 552 if (!StartsWithASCII(input_method_id, "xkb:", true)) {
548 return ""; 553 return "";
549 } 554 }
550 555
551 std::vector<std::string> splitted_id; 556 std::vector<std::string> splitted_id;
552 base::SplitString(input_method_id, ':', &splitted_id); 557 base::SplitString(input_method_id, ':', &splitted_id);
553 return (splitted_id.size() > 1) ? splitted_id[1] : ""; 558 return (splitted_id.size() > 1) ? splitted_id[1] : "";
554 } 559 }
555 560
556 std::string GetKeyboardOverlayId(const std::string& input_method_id) { 561 std::string GetKeyboardOverlayId(const std::string& input_method_id) {
557 const std::map<std::string, std::string>& id_map = 562 const std::map<std::string, std::string>& id_map =
558 *(Singleton<IdMaps>::get()->id_to_keyboard_overlay_id); 563 *(IdMaps::GetInstance()->id_to_keyboard_overlay_id);
559 std::map<std::string, std::string>::const_iterator iter = 564 std::map<std::string, std::string>::const_iterator iter =
560 id_map.find(input_method_id); 565 id_map.find(input_method_id);
561 return (iter == id_map.end() ? "" : iter->second); 566 return (iter == id_map.end() ? "" : iter->second);
562 } 567 }
563 568
564 std::string GetInputMethodDisplayNameFromId( 569 std::string GetInputMethodDisplayNameFromId(
565 const std::string& input_method_id) { 570 const std::string& input_method_id) {
566 static const char kDefaultDisplayName[] = "USA"; 571 static const char kDefaultDisplayName[] = "USA";
567 std::map<std::string, std::string>::const_iterator iter 572 std::map<std::string, std::string>::const_iterator iter
568 = Singleton<IdMaps>::get()->id_to_display_name->find(input_method_id); 573 = IdMaps::GetInstance()->id_to_display_name->find(input_method_id);
569 return (iter == Singleton<IdMaps>::get()->id_to_display_name->end()) ? 574 return (iter == IdMaps::GetInstance()->id_to_display_name->end()) ?
570 kDefaultDisplayName : iter->second; 575 kDefaultDisplayName : iter->second;
571 } 576 }
572 577
573 std::wstring GetLanguageDisplayNameFromCode(const std::string& language_code) { 578 std::wstring GetLanguageDisplayNameFromCode(const std::string& language_code) {
574 if (!g_browser_process) { 579 if (!g_browser_process) {
575 return L""; 580 return L"";
576 } 581 }
577 return UTF16ToWide(l10n_util::GetDisplayNameForLocale( 582 return UTF16ToWide(l10n_util::GetDisplayNameForLocale(
578 language_code, g_browser_process->GetApplicationLocale(), true)); 583 language_code, g_browser_process->GetApplicationLocale(), true));
579 } 584 }
(...skipping 16 matching lines...) Expand all
596 icu::Collator::createInstance(locale, error)); 601 icu::Collator::createInstance(locale, error));
597 if (U_FAILURE(error)) { 602 if (U_FAILURE(error)) {
598 collator.reset(); 603 collator.reset();
599 } 604 }
600 std::sort(language_codes->begin(), language_codes->end(), 605 std::sort(language_codes->begin(), language_codes->end(),
601 CompareLanguageCodesByLanguageName(collator.get())); 606 CompareLanguageCodesByLanguageName(collator.get()));
602 } 607 }
603 608
604 void SortInputMethodIdsByNames(std::vector<std::string>* input_method_ids) { 609 void SortInputMethodIdsByNames(std::vector<std::string>* input_method_ids) {
605 SortInputMethodIdsByNamesInternal( 610 SortInputMethodIdsByNamesInternal(
606 *(Singleton<IdMaps>::get()->id_to_language_code), input_method_ids); 611 *(IdMaps::GetInstance()->id_to_language_code), input_method_ids);
607 } 612 }
608 613
609 void SortInputMethodIdsByNamesInternal( 614 void SortInputMethodIdsByNamesInternal(
610 const std::map<std::string, std::string>& id_to_language_code_map, 615 const std::map<std::string, std::string>& id_to_language_code_map,
611 std::vector<std::string>* input_method_ids) { 616 std::vector<std::string>* input_method_ids) {
612 if (!g_browser_process) { 617 if (!g_browser_process) {
613 return; 618 return;
614 } 619 }
615 UErrorCode error = U_ZERO_ERROR; 620 UErrorCode error = U_ZERO_ERROR;
616 icu::Locale locale(g_browser_process->GetApplicationLocale().c_str()); 621 icu::Locale locale(g_browser_process->GetApplicationLocale().c_str());
617 scoped_ptr<icu::Collator> collator( 622 scoped_ptr<icu::Collator> collator(
618 icu::Collator::createInstance(locale, error)); 623 icu::Collator::createInstance(locale, error));
619 if (U_FAILURE(error)) { 624 if (U_FAILURE(error)) {
620 collator.reset(); 625 collator.reset();
621 } 626 }
622 std::stable_sort(input_method_ids->begin(), input_method_ids->end(), 627 std::stable_sort(input_method_ids->begin(), input_method_ids->end(),
623 CompareInputMethodIdsByLanguageName( 628 CompareInputMethodIdsByLanguageName(
624 collator.get(), id_to_language_code_map)); 629 collator.get(), id_to_language_code_map));
625 } 630 }
626 631
627 bool GetInputMethodIdsFromLanguageCode( 632 bool GetInputMethodIdsFromLanguageCode(
628 const std::string& normalized_language_code, 633 const std::string& normalized_language_code,
629 InputMethodType type, 634 InputMethodType type,
630 std::vector<std::string>* out_input_method_ids) { 635 std::vector<std::string>* out_input_method_ids) {
631 return GetInputMethodIdsFromLanguageCodeInternal( 636 return GetInputMethodIdsFromLanguageCodeInternal(
632 *Singleton<IdMaps>::get()->language_code_to_ids, 637 *IdMaps::GetInstance()->language_code_to_ids,
633 normalized_language_code, type, out_input_method_ids); 638 normalized_language_code, type, out_input_method_ids);
634 } 639 }
635 640
636 bool GetInputMethodIdsFromLanguageCodeInternal( 641 bool GetInputMethodIdsFromLanguageCodeInternal(
637 const std::multimap<std::string, std::string>& language_code_to_ids, 642 const std::multimap<std::string, std::string>& language_code_to_ids,
638 const std::string& normalized_language_code, 643 const std::string& normalized_language_code,
639 InputMethodType type, 644 InputMethodType type,
640 std::vector<std::string>* out_input_method_ids) { 645 std::vector<std::string>* out_input_method_ids) {
641 DCHECK(out_input_method_ids); 646 DCHECK(out_input_method_ids);
642 out_input_method_ids->clear(); 647 out_input_method_ids->clear();
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
680 value.string_list_value = input_method_ids; 685 value.string_list_value = input_method_ids;
681 InputMethodLibrary* library = CrosLibrary::Get()->GetInputMethodLibrary(); 686 InputMethodLibrary* library = CrosLibrary::Get()->GetInputMethodLibrary();
682 library->SetImeConfig(language_prefs::kGeneralSectionName, 687 library->SetImeConfig(language_prefs::kGeneralSectionName,
683 language_prefs::kPreloadEnginesConfigName, value); 688 language_prefs::kPreloadEnginesConfigName, value);
684 if (!initial_input_method_id.empty()) { 689 if (!initial_input_method_id.empty()) {
685 library->ChangeInputMethod(initial_input_method_id); 690 library->ChangeInputMethod(initial_input_method_id);
686 } 691 }
687 } 692 }
688 693
689 void OnLocaleChanged() { 694 void OnLocaleChanged() {
690 Singleton<IdMaps>::get()->ReloadMaps(); 695 IdMaps::GetInstance()->ReloadMaps();
691 } 696 }
692 697
693 } // namespace input_method 698 } // namespace input_method
694 } // namespace chromeos 699 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698