| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "components/autofill/core/browser/autofill_country.h" | 5 #include "components/autofill/core/browser/autofill_country.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 #include <map> | 9 #include <map> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 853 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 864 | 864 |
| 865 // Interprets |country_name| as a full country name localized to the given | 865 // Interprets |country_name| as a full country name localized to the given |
| 866 // |locale| and returns the corresponding country code stored in | 866 // |locale| and returns the corresponding country code stored in |
| 867 // |locales_to_localized_names_|, or an empty string if there is none. | 867 // |locales_to_localized_names_|, or an empty string if there is none. |
| 868 const std::string GetCountryCodeForLocalizedName( | 868 const std::string GetCountryCodeForLocalizedName( |
| 869 const base::string16& country_name, | 869 const base::string16& country_name, |
| 870 const std::string& locale); | 870 const std::string& locale); |
| 871 | 871 |
| 872 // Returns an ICU collator -- i.e. string comparator -- appropriate for the | 872 // Returns an ICU collator -- i.e. string comparator -- appropriate for the |
| 873 // given |locale|, or null if no collator is available. | 873 // given |locale|, or null if no collator is available. |
| 874 icu::Collator* GetCollatorForLocale(const std::string& locale); | 874 const icu::Collator* GetCollatorForLocale(const std::string& locale); |
| 875 | 875 |
| 876 // Returns the ICU sort key corresponding to |str| for the given |collator|. | 876 // Returns the ICU sort key corresponding to |str| for the given |collator|. |
| 877 // Uses |buffer| as temporary storage, and might resize |buffer| as a side- | 877 // Uses |buffer| as temporary storage, and might resize |buffer| as a side- |
| 878 // effect. |buffer_size| should specify the |buffer|'s size, and is updated if | 878 // effect. |buffer_size| should specify the |buffer|'s size, and is updated if |
| 879 // the |buffer| is resized. | 879 // the |buffer| is resized. |
| 880 const std::string GetSortKey(const icu::Collator& collator, | 880 const std::string GetSortKey(const icu::Collator& collator, |
| 881 const base::string16& str, | 881 const base::string16& str, |
| 882 scoped_ptr<uint8_t[]>* buffer, | 882 scoped_ptr<uint8_t[]>* buffer, |
| 883 int32_t* buffer_size) const; | 883 int32_t* buffer_size) const; |
| 884 | 884 |
| 885 // Maps from common country names, including 2- and 3-letter country codes, | 885 // Maps from common country names, including 2- and 3-letter country codes, |
| 886 // to the corresponding 2-letter country codes. The keys are uppercase ASCII | 886 // to the corresponding 2-letter country codes. The keys are uppercase ASCII |
| 887 // strings. | 887 // strings. |
| 888 std::map<std::string, std::string> common_names_; | 888 std::map<std::string, std::string> common_names_; |
| 889 | 889 |
| 890 // The outer map keys are ICU locale identifiers. | 890 // The outer map keys are ICU locale identifiers. |
| 891 // The inner maps map from localized country names to their corresponding | 891 // The inner maps map from localized country names to their corresponding |
| 892 // country codes. The inner map keys are ICU collation sort keys corresponding | 892 // country codes. The inner map keys are ICU collation sort keys corresponding |
| 893 // to the target localized country name. | 893 // to the target localized country name. |
| 894 std::map<std::string, std::map<std::string, std::string> > | 894 std::map<std::string, std::map<std::string, std::string> > |
| 895 locales_to_localized_names_; | 895 locales_to_localized_names_; |
| 896 | 896 |
| 897 // Maps ICU locale names to their corresponding collators. | 897 // Maps ICU locale names to their corresponding collators. |
| 898 std::map<std::string, icu::Collator*> collators_; | 898 std::map<std::string, scoped_ptr<icu::Collator>> collators_; |
| 899 | 899 |
| 900 DISALLOW_COPY_AND_ASSIGN(CountryNames); | 900 DISALLOW_COPY_AND_ASSIGN(CountryNames); |
| 901 }; | 901 }; |
| 902 | 902 |
| 903 // static | 903 // static |
| 904 CountryNames* CountryNames::GetInstance() { | 904 CountryNames* CountryNames::GetInstance() { |
| 905 return base::Singleton<CountryNames>::get(); | 905 return base::Singleton<CountryNames>::get(); |
| 906 } | 906 } |
| 907 | 907 |
| 908 CountryNames::CountryNames() { | 908 CountryNames::CountryNames() { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 923 | 923 |
| 924 // Add a few other common synonyms. | 924 // Add a few other common synonyms. |
| 925 common_names_.insert(std::make_pair("UNITED STATES OF AMERICA", "US")); | 925 common_names_.insert(std::make_pair("UNITED STATES OF AMERICA", "US")); |
| 926 common_names_.insert(std::make_pair("U.S.A.", "US")); | 926 common_names_.insert(std::make_pair("U.S.A.", "US")); |
| 927 common_names_.insert(std::make_pair("GREAT BRITAIN", "GB")); | 927 common_names_.insert(std::make_pair("GREAT BRITAIN", "GB")); |
| 928 common_names_.insert(std::make_pair("UK", "GB")); | 928 common_names_.insert(std::make_pair("UK", "GB")); |
| 929 common_names_.insert(std::make_pair("BRASIL", "BR")); | 929 common_names_.insert(std::make_pair("BRASIL", "BR")); |
| 930 common_names_.insert(std::make_pair("DEUTSCHLAND", "DE")); | 930 common_names_.insert(std::make_pair("DEUTSCHLAND", "DE")); |
| 931 } | 931 } |
| 932 | 932 |
| 933 CountryNames::~CountryNames() { | 933 CountryNames::~CountryNames() {} |
| 934 STLDeleteContainerPairSecondPointers(collators_.begin(), | |
| 935 collators_.end()); | |
| 936 } | |
| 937 | 934 |
| 938 const std::string CountryNames::GetCountryCode(const base::string16& country, | 935 const std::string CountryNames::GetCountryCode(const base::string16& country, |
| 939 const std::string& locale) { | 936 const std::string& locale) { |
| 940 // First, check common country names, including 2- and 3-letter country codes. | 937 // First, check common country names, including 2- and 3-letter country codes. |
| 941 std::string country_utf8 = base::UTF16ToUTF8(base::ToUpperASCII(country)); | 938 std::string country_utf8 = base::UTF16ToUTF8(base::ToUpperASCII(country)); |
| 942 const auto result = common_names_.find(country_utf8); | 939 const auto result = common_names_.find(country_utf8); |
| 943 if (result != common_names_.end()) | 940 if (result != common_names_.end()) |
| 944 return result->second; | 941 return result->second; |
| 945 | 942 |
| 946 // Next, check country names localized to |locale|. | 943 // Next, check country names localized to |locale|. |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 978 } | 975 } |
| 979 | 976 |
| 980 locales_to_localized_names_.insert(std::make_pair(locale, localized_names)); | 977 locales_to_localized_names_.insert(std::make_pair(locale, localized_names)); |
| 981 } | 978 } |
| 982 | 979 |
| 983 const std::string CountryNames::GetCountryCodeForLocalizedName( | 980 const std::string CountryNames::GetCountryCodeForLocalizedName( |
| 984 const base::string16& country_name, | 981 const base::string16& country_name, |
| 985 const std::string& locale) { | 982 const std::string& locale) { |
| 986 AddLocalizedNamesForLocale(locale); | 983 AddLocalizedNamesForLocale(locale); |
| 987 | 984 |
| 988 icu::Collator* collator = GetCollatorForLocale(locale); | 985 const icu::Collator* collator = GetCollatorForLocale(locale); |
| 989 // In very rare cases, the collator fails to initialize. | 986 // In very rare cases, the collator fails to initialize. |
| 990 if (!collator) | 987 if (!collator) |
| 991 return std::string(); | 988 return std::string(); |
| 992 | 989 |
| 993 // As recommended[1] by ICU, initialize the buffer size to four times the | 990 // As recommended[1] by ICU, initialize the buffer size to four times the |
| 994 // source string length. | 991 // source string length. |
| 995 // [1] http://userguide.icu-project.org/collation/api#TOC-Examples | 992 // [1] http://userguide.icu-project.org/collation/api#TOC-Examples |
| 996 int32_t buffer_size = country_name.size() * 4; | 993 int32_t buffer_size = country_name.size() * 4; |
| 997 scoped_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]); | 994 scoped_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]); |
| 998 std::string sort_key = GetSortKey(*collator, | 995 std::string sort_key = GetSortKey(*collator, |
| 999 country_name, | 996 country_name, |
| 1000 &buffer, | 997 &buffer, |
| 1001 &buffer_size); | 998 &buffer_size); |
| 1002 | 999 |
| 1003 const std::map<std::string, std::string>& localized_names = | 1000 const std::map<std::string, std::string>& localized_names = |
| 1004 locales_to_localized_names_[locale]; | 1001 locales_to_localized_names_[locale]; |
| 1005 std::map<std::string, std::string>::const_iterator result = | 1002 std::map<std::string, std::string>::const_iterator result = |
| 1006 localized_names.find(sort_key); | 1003 localized_names.find(sort_key); |
| 1007 | 1004 |
| 1008 if (result != localized_names.end()) | 1005 if (result != localized_names.end()) |
| 1009 return result->second; | 1006 return result->second; |
| 1010 | 1007 |
| 1011 return std::string(); | 1008 return std::string(); |
| 1012 } | 1009 } |
| 1013 | 1010 |
| 1014 icu::Collator* CountryNames::GetCollatorForLocale(const std::string& locale) { | 1011 const icu::Collator* CountryNames::GetCollatorForLocale( |
| 1015 if (!collators_.count(locale)) { | 1012 const std::string& locale) { |
| 1016 icu::Collator* collator( | 1013 if (!ContainsKey(collators_, locale)) { |
| 1014 scoped_ptr<icu::Collator> collator( |
| 1017 autofill::l10n::GetCollatorForLocale(icu::Locale(locale.c_str()))); | 1015 autofill::l10n::GetCollatorForLocale(icu::Locale(locale.c_str()))); |
| 1018 if (!collator) | 1016 if (!collator) |
| 1019 return nullptr; | 1017 return nullptr; |
| 1020 | 1018 |
| 1021 // Compare case-insensitively and ignoring punctuation. | 1019 // Compare case-insensitively and ignoring punctuation. |
| 1022 UErrorCode ignored = U_ZERO_ERROR; | 1020 UErrorCode ignored = U_ZERO_ERROR; |
| 1023 collator->setAttribute(UCOL_STRENGTH, UCOL_SECONDARY, ignored); | 1021 collator->setAttribute(UCOL_STRENGTH, UCOL_SECONDARY, ignored); |
| 1024 ignored = U_ZERO_ERROR; | 1022 ignored = U_ZERO_ERROR; |
| 1025 collator->setAttribute(UCOL_ALTERNATE_HANDLING, UCOL_SHIFTED, ignored); | 1023 collator->setAttribute(UCOL_ALTERNATE_HANDLING, UCOL_SHIFTED, ignored); |
| 1026 | 1024 |
| 1027 collators_.insert(std::make_pair(locale, collator)); | 1025 collators_[locale] = std::move(collator); |
| 1028 } | 1026 } |
| 1029 | 1027 |
| 1030 return collators_[locale]; | 1028 return collators_[locale].get(); |
| 1031 } | 1029 } |
| 1032 | 1030 |
| 1033 const std::string CountryNames::GetSortKey(const icu::Collator& collator, | 1031 const std::string CountryNames::GetSortKey(const icu::Collator& collator, |
| 1034 const base::string16& str, | 1032 const base::string16& str, |
| 1035 scoped_ptr<uint8_t[]>* buffer, | 1033 scoped_ptr<uint8_t[]>* buffer, |
| 1036 int32_t* buffer_size) const { | 1034 int32_t* buffer_size) const { |
| 1037 DCHECK(buffer); | 1035 DCHECK(buffer); |
| 1038 DCHECK(buffer_size); | 1036 DCHECK(buffer_size); |
| 1039 | 1037 |
| 1040 icu::UnicodeString icu_str(str.c_str(), str.length()); | 1038 icu::UnicodeString icu_str(str.c_str(), str.length()); |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1115 const base::string16& name, | 1113 const base::string16& name, |
| 1116 const base::string16& postal_code_label, | 1114 const base::string16& postal_code_label, |
| 1117 const base::string16& state_label) | 1115 const base::string16& state_label) |
| 1118 : country_code_(country_code), | 1116 : country_code_(country_code), |
| 1119 name_(name), | 1117 name_(name), |
| 1120 postal_code_label_(postal_code_label), | 1118 postal_code_label_(postal_code_label), |
| 1121 state_label_(state_label) { | 1119 state_label_(state_label) { |
| 1122 } | 1120 } |
| 1123 | 1121 |
| 1124 } // namespace autofill | 1122 } // namespace autofill |
| OLD | NEW |