| 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 "components/autofill/browser/autofill_country.h" | 5 #include "components/autofill/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 825 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 836 } | 836 } |
| 837 | 837 |
| 838 // A singleton class that encapsulates mappings from country names to their | 838 // A singleton class that encapsulates mappings from country names to their |
| 839 // corresponding country codes. | 839 // corresponding country codes. |
| 840 class CountryNames { | 840 class CountryNames { |
| 841 public: | 841 public: |
| 842 static CountryNames* GetInstance(); | 842 static CountryNames* GetInstance(); |
| 843 | 843 |
| 844 // Returns the country code corresponding to |country|, which should be a | 844 // Returns the country code corresponding to |country|, which should be a |
| 845 // country code or country name localized to |locale|. | 845 // country code or country name localized to |locale|. |
| 846 const std::string GetCountryCode(const string16& country, | 846 const std::string GetCountryCode(const base::string16& country, |
| 847 const std::string& locale); | 847 const std::string& locale); |
| 848 | 848 |
| 849 private: | 849 private: |
| 850 CountryNames(); | 850 CountryNames(); |
| 851 ~CountryNames(); | 851 ~CountryNames(); |
| 852 friend struct DefaultSingletonTraits<CountryNames>; | 852 friend struct DefaultSingletonTraits<CountryNames>; |
| 853 | 853 |
| 854 // Populates |locales_to_localized_names_| with the mapping of country names | 854 // Populates |locales_to_localized_names_| with the mapping of country names |
| 855 // localized to |locale| to their corresponding country codes. | 855 // localized to |locale| to their corresponding country codes. |
| 856 void AddLocalizedNamesForLocale(const std::string& locale); | 856 void AddLocalizedNamesForLocale(const std::string& locale); |
| 857 | 857 |
| 858 // Interprets |country_name| as a full country name localized to the given | 858 // Interprets |country_name| as a full country name localized to the given |
| 859 // |locale| and returns the corresponding country code stored in | 859 // |locale| and returns the corresponding country code stored in |
| 860 // |locales_to_localized_names_|, or an empty string if there is none. | 860 // |locales_to_localized_names_|, or an empty string if there is none. |
| 861 const std::string GetCountryCodeForLocalizedName(const string16& country_name, | 861 const std::string GetCountryCodeForLocalizedName( |
| 862 const std::string& locale); | 862 const base::string16& country_name, |
| 863 const std::string& locale); |
| 863 | 864 |
| 864 // Returns an ICU collator -- i.e. string comparator -- appropriate for the | 865 // Returns an ICU collator -- i.e. string comparator -- appropriate for the |
| 865 // given |locale|. | 866 // given |locale|. |
| 866 icu::Collator* GetCollatorForLocale(const std::string& locale); | 867 icu::Collator* GetCollatorForLocale(const std::string& locale); |
| 867 | 868 |
| 868 // Returns the ICU sort key corresponding to |str| for the given |collator|. | 869 // Returns the ICU sort key corresponding to |str| for the given |collator|. |
| 869 // Uses |buffer| as temporary storage, and might resize |buffer| as a side- | 870 // Uses |buffer| as temporary storage, and might resize |buffer| as a side- |
| 870 // effect. |buffer_size| should specify the |buffer|'s size, and is updated if | 871 // effect. |buffer_size| should specify the |buffer|'s size, and is updated if |
| 871 // the |buffer| is resized. | 872 // the |buffer| is resized. |
| 872 const std::string GetSortKey(const icu::Collator& collator, | 873 const std::string GetSortKey(const icu::Collator& collator, |
| 873 const string16& str, | 874 const base::string16& str, |
| 874 scoped_ptr<uint8_t[]>* buffer, | 875 scoped_ptr<uint8_t[]>* buffer, |
| 875 int32_t* buffer_size) const; | 876 int32_t* buffer_size) const; |
| 876 | 877 |
| 877 // Maps from common country names, including 2- and 3-letter country codes, | 878 // Maps from common country names, including 2- and 3-letter country codes, |
| 878 // to the corresponding 2-letter country codes. The keys are uppercase ASCII | 879 // to the corresponding 2-letter country codes. The keys are uppercase ASCII |
| 879 // strings. | 880 // strings. |
| 880 std::map<std::string, std::string> common_names_; | 881 std::map<std::string, std::string> common_names_; |
| 881 | 882 |
| 882 // The outer map keys are ICU locale identifiers. | 883 // The outer map keys are ICU locale identifiers. |
| 883 // The inner maps map from localized country names to their corresponding | 884 // The inner maps map from localized country names to their corresponding |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 920 common_names_.insert(std::make_pair("UK", "GB")); | 921 common_names_.insert(std::make_pair("UK", "GB")); |
| 921 common_names_.insert(std::make_pair("BRASIL", "BR")); | 922 common_names_.insert(std::make_pair("BRASIL", "BR")); |
| 922 common_names_.insert(std::make_pair("DEUTSCHLAND", "DE")); | 923 common_names_.insert(std::make_pair("DEUTSCHLAND", "DE")); |
| 923 } | 924 } |
| 924 | 925 |
| 925 CountryNames::~CountryNames() { | 926 CountryNames::~CountryNames() { |
| 926 STLDeleteContainerPairSecondPointers(collators_.begin(), | 927 STLDeleteContainerPairSecondPointers(collators_.begin(), |
| 927 collators_.end()); | 928 collators_.end()); |
| 928 } | 929 } |
| 929 | 930 |
| 930 const std::string CountryNames::GetCountryCode(const string16& country, | 931 const std::string CountryNames::GetCountryCode(const base::string16& country, |
| 931 const std::string& locale) { | 932 const std::string& locale) { |
| 932 // First, check common country names, including 2- and 3-letter country codes. | 933 // First, check common country names, including 2- and 3-letter country codes. |
| 933 std::string country_utf8 = UTF16ToUTF8(StringToUpperASCII(country)); | 934 std::string country_utf8 = UTF16ToUTF8(StringToUpperASCII(country)); |
| 934 std::map<std::string, std::string>::const_iterator result = | 935 std::map<std::string, std::string>::const_iterator result = |
| 935 common_names_.find(country_utf8); | 936 common_names_.find(country_utf8); |
| 936 if (result != common_names_.end()) | 937 if (result != common_names_.end()) |
| 937 return result->second; | 938 return result->second; |
| 938 | 939 |
| 939 // Next, check country names localized to |locale|. | 940 // Next, check country names localized to |locale|. |
| 940 std::string country_code = GetCountryCodeForLocalizedName(country, locale); | 941 std::string country_code = GetCountryCodeForLocalizedName(country, locale); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 953 | 954 |
| 954 std::map<std::string, std::string> localized_names; | 955 std::map<std::string, std::string> localized_names; |
| 955 const icu::Collator* collator = GetCollatorForLocale(locale); | 956 const icu::Collator* collator = GetCollatorForLocale(locale); |
| 956 int32_t buffer_size = 1000; | 957 int32_t buffer_size = 1000; |
| 957 scoped_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]); | 958 scoped_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]); |
| 958 | 959 |
| 959 for (CountryDataMap::Iterator it = CountryDataMap::Begin(); | 960 for (CountryDataMap::Iterator it = CountryDataMap::Begin(); |
| 960 it != CountryDataMap::End(); | 961 it != CountryDataMap::End(); |
| 961 ++it) { | 962 ++it) { |
| 962 const std::string& country_code = it->first; | 963 const std::string& country_code = it->first; |
| 963 string16 country_name = l10n_util::GetDisplayNameForCountry(country_code, | 964 base::string16 country_name = l10n_util::GetDisplayNameForCountry( |
| 964 locale); | 965 country_code, locale); |
| 965 std::string sort_key = GetSortKey(*collator, | 966 std::string sort_key = GetSortKey(*collator, |
| 966 country_name, | 967 country_name, |
| 967 &buffer, | 968 &buffer, |
| 968 &buffer_size); | 969 &buffer_size); |
| 969 | 970 |
| 970 localized_names.insert(std::make_pair(sort_key, country_code)); | 971 localized_names.insert(std::make_pair(sort_key, country_code)); |
| 971 } | 972 } |
| 972 | 973 |
| 973 locales_to_localized_names_.insert(std::make_pair(locale, localized_names)); | 974 locales_to_localized_names_.insert(std::make_pair(locale, localized_names)); |
| 974 } | 975 } |
| 975 | 976 |
| 976 const std::string CountryNames::GetCountryCodeForLocalizedName( | 977 const std::string CountryNames::GetCountryCodeForLocalizedName( |
| 977 const string16& country_name, | 978 const base::string16& country_name, |
| 978 const std::string& locale) { | 979 const std::string& locale) { |
| 979 AddLocalizedNamesForLocale(locale); | 980 AddLocalizedNamesForLocale(locale); |
| 980 | 981 |
| 981 icu::Collator* collator = GetCollatorForLocale(locale); | 982 icu::Collator* collator = GetCollatorForLocale(locale); |
| 982 | 983 |
| 983 // As recommended[1] by ICU, initialize the buffer size to four times the | 984 // As recommended[1] by ICU, initialize the buffer size to four times the |
| 984 // source string length. | 985 // source string length. |
| 985 // [1] http://userguide.icu-project.org/collation/api#TOC-Examples | 986 // [1] http://userguide.icu-project.org/collation/api#TOC-Examples |
| 986 int32_t buffer_size = country_name.size() * 4; | 987 int32_t buffer_size = country_name.size() * 4; |
| 987 scoped_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]); | 988 scoped_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 1013 ignored = U_ZERO_ERROR; | 1014 ignored = U_ZERO_ERROR; |
| 1014 collator->setAttribute(UCOL_ALTERNATE_HANDLING, UCOL_SHIFTED, ignored); | 1015 collator->setAttribute(UCOL_ALTERNATE_HANDLING, UCOL_SHIFTED, ignored); |
| 1015 | 1016 |
| 1016 collators_.insert(std::make_pair(locale, collator)); | 1017 collators_.insert(std::make_pair(locale, collator)); |
| 1017 } | 1018 } |
| 1018 | 1019 |
| 1019 return collators_[locale]; | 1020 return collators_[locale]; |
| 1020 } | 1021 } |
| 1021 | 1022 |
| 1022 const std::string CountryNames::GetSortKey(const icu::Collator& collator, | 1023 const std::string CountryNames::GetSortKey(const icu::Collator& collator, |
| 1023 const string16& str, | 1024 const base::string16& str, |
| 1024 scoped_ptr<uint8_t[]>* buffer, | 1025 scoped_ptr<uint8_t[]>* buffer, |
| 1025 int32_t* buffer_size) const { | 1026 int32_t* buffer_size) const { |
| 1026 DCHECK(buffer); | 1027 DCHECK(buffer); |
| 1027 DCHECK(buffer_size); | 1028 DCHECK(buffer_size); |
| 1028 | 1029 |
| 1029 icu::UnicodeString icu_str(str.c_str(), str.length()); | 1030 icu::UnicodeString icu_str(str.c_str(), str.length()); |
| 1030 int32_t expected_size = collator.getSortKey(icu_str, buffer->get(), | 1031 int32_t expected_size = collator.getSortKey(icu_str, buffer->get(), |
| 1031 *buffer_size); | 1032 *buffer_size); |
| 1032 if (expected_size > *buffer_size) { | 1033 if (expected_size > *buffer_size) { |
| 1033 // If there wasn't enough space, grow the buffer and try again. | 1034 // If there wasn't enough space, grow the buffer and try again. |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1088 std::string country_code = icu::Locale(likely_locale.c_str()).getCountry(); | 1089 std::string country_code = icu::Locale(likely_locale.c_str()).getCountry(); |
| 1089 | 1090 |
| 1090 // Default to the United States if we have no better guess. | 1091 // Default to the United States if we have no better guess. |
| 1091 if (CountryDataMap::Find(country_code) == CountryDataMap::End()) | 1092 if (CountryDataMap::Find(country_code) == CountryDataMap::End()) |
| 1092 return "US"; | 1093 return "US"; |
| 1093 | 1094 |
| 1094 return country_code; | 1095 return country_code; |
| 1095 } | 1096 } |
| 1096 | 1097 |
| 1097 // static | 1098 // static |
| 1098 const std::string AutofillCountry::GetCountryCode(const string16& country, | 1099 const std::string AutofillCountry::GetCountryCode(const base::string16& country, |
| 1099 const std::string& locale) { | 1100 const std::string& locale) { |
| 1100 return CountryNames::GetInstance()->GetCountryCode(country, locale); | 1101 return CountryNames::GetInstance()->GetCountryCode(country, locale); |
| 1101 } | 1102 } |
| 1102 | 1103 |
| 1103 AutofillCountry::AutofillCountry(const std::string& country_code, | 1104 AutofillCountry::AutofillCountry(const std::string& country_code, |
| 1104 const string16& name, | 1105 const base::string16& name, |
| 1105 const string16& postal_code_label, | 1106 const base::string16& postal_code_label, |
| 1106 const string16& state_label) | 1107 const base::string16& state_label) |
| 1107 : country_code_(country_code), | 1108 : country_code_(country_code), |
| 1108 name_(name), | 1109 name_(name), |
| 1109 postal_code_label_(postal_code_label), | 1110 postal_code_label_(postal_code_label), |
| 1110 state_label_(state_label) { | 1111 state_label_(state_label) { |
| 1111 } | 1112 } |
| OLD | NEW |