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> |
11 | 11 |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "base/memory/singleton.h" | 14 #include "base/memory/singleton.h" |
15 #include "base/stl_util.h" | 15 #include "base/stl_util.h" |
16 #include "base/string_util.h" | 16 #include "base/string_util.h" |
17 #include "base/threading/thread_checker.h" | 17 #include "base/threading/thread_checker.h" |
18 #include "base/utf_string_conversions.h" | 18 #include "base/utf_string_conversions.h" |
19 #include "content/public/browser/browser_thread.h" | |
20 #include "content/public/browser/content_browser_client.h" | |
21 #include "grit/generated_resources.h" | 19 #include "grit/generated_resources.h" |
22 #include "third_party/icu/public/common/unicode/locid.h" | 20 #include "third_party/icu/public/common/unicode/locid.h" |
23 #include "third_party/icu/public/common/unicode/uloc.h" | 21 #include "third_party/icu/public/common/unicode/uloc.h" |
24 #include "third_party/icu/public/common/unicode/unistr.h" | 22 #include "third_party/icu/public/common/unicode/unistr.h" |
25 #include "third_party/icu/public/common/unicode/urename.h" | 23 #include "third_party/icu/public/common/unicode/urename.h" |
26 #include "third_party/icu/public/common/unicode/utypes.h" | 24 #include "third_party/icu/public/common/unicode/utypes.h" |
27 #include "third_party/icu/public/i18n/unicode/coll.h" | 25 #include "third_party/icu/public/i18n/unicode/coll.h" |
28 #include "third_party/icu/public/i18n/unicode/ucol.h" | 26 #include "third_party/icu/public/i18n/unicode/ucol.h" |
29 #include "ui/base/l10n/l10n_util.h" | 27 #include "ui/base/l10n/l10n_util.h" |
30 | 28 |
31 using content::BrowserThread; | |
32 | |
33 namespace { | 29 namespace { |
34 | 30 |
35 // The maximum capacity needed to store a locale up to the country code. | 31 // The maximum capacity needed to store a locale up to the country code. |
36 const size_t kLocaleCapacity = | 32 const size_t kLocaleCapacity = |
37 ULOC_LANG_CAPACITY + ULOC_SCRIPT_CAPACITY + ULOC_COUNTRY_CAPACITY + 1; | 33 ULOC_LANG_CAPACITY + ULOC_SCRIPT_CAPACITY + ULOC_COUNTRY_CAPACITY + 1; |
38 | 34 |
39 struct CountryData { | 35 struct CountryData { |
40 int postal_code_label_id; | 36 int postal_code_label_id; |
41 int state_label_id; | 37 int state_label_id; |
42 AddressRequiredFields address_required_fields; | 38 AddressRequiredFields address_required_fields; |
(...skipping 795 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
838 const std::string& country_code) { | 834 const std::string& country_code) { |
839 return GetInstance()->country_data_.find(country_code); | 835 return GetInstance()->country_data_.find(country_code); |
840 } | 836 } |
841 | 837 |
842 // A singleton class that encapsulates mappings from country names to their | 838 // A singleton class that encapsulates mappings from country names to their |
843 // corresponding country codes. | 839 // corresponding country codes. |
844 class CountryNames { | 840 class CountryNames { |
845 public: | 841 public: |
846 static CountryNames* GetInstance(); | 842 static CountryNames* GetInstance(); |
847 | 843 |
848 // Returns the application locale. | |
849 const std::string ApplicationLocale(); | |
850 | |
851 // Returns the country code corresponding to |country|, which should be a | 844 // Returns the country code corresponding to |country|, which should be a |
852 // country code or country name localized to |locale|. | 845 // country code or country name localized to |locale|. |
853 const std::string GetCountryCode(const string16& country, | 846 const std::string GetCountryCode(const string16& country, |
854 const std::string& locale); | 847 const std::string& locale); |
855 | 848 |
856 private: | 849 private: |
857 CountryNames(); | 850 CountryNames(); |
858 ~CountryNames(); | 851 ~CountryNames(); |
859 friend struct DefaultSingletonTraits<CountryNames>; | 852 friend struct DefaultSingletonTraits<CountryNames>; |
860 | 853 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
892 // to the target localized country name. | 885 // to the target localized country name. |
893 std::map<std::string, std::map<std::string, std::string> > | 886 std::map<std::string, std::map<std::string, std::string> > |
894 locales_to_localized_names_; | 887 locales_to_localized_names_; |
895 | 888 |
896 // Maps ICU locale names to their corresponding collators. | 889 // Maps ICU locale names to their corresponding collators. |
897 std::map<std::string, icu::Collator*> collators_; | 890 std::map<std::string, icu::Collator*> collators_; |
898 | 891 |
899 // Verifies thread-safety of accesses to the application locale. | 892 // Verifies thread-safety of accesses to the application locale. |
900 base::ThreadChecker thread_checker_; | 893 base::ThreadChecker thread_checker_; |
901 | 894 |
902 // Caches the application locale, for thread-safe access. | |
903 std::string application_locale_; | |
904 | |
905 DISALLOW_COPY_AND_ASSIGN(CountryNames); | 895 DISALLOW_COPY_AND_ASSIGN(CountryNames); |
906 }; | 896 }; |
907 | 897 |
908 // static | 898 // static |
909 CountryNames* CountryNames::GetInstance() { | 899 CountryNames* CountryNames::GetInstance() { |
910 return Singleton<CountryNames>::get(); | 900 return Singleton<CountryNames>::get(); |
911 } | 901 } |
912 | 902 |
913 const std::string CountryNames::ApplicationLocale() { | |
914 if (application_locale_.empty()) { | |
915 // In production code, this class is always constructed on the UI thread, so | |
916 // the two conditions in the below DCHECK are identical. In test code, | |
917 // sometimes there is a UI thread, and sometimes there is just the unnamed | |
918 // main thread. Since this class is a singleton, it needs to support both | |
919 // cases. Hence, the somewhat strange looking DCHECK below. | |
920 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || | |
921 thread_checker_.CalledOnValidThread()); | |
922 application_locale_ = | |
923 content::GetContentClient()->browser()->GetApplicationLocale(); | |
924 } | |
925 | |
926 return application_locale_; | |
927 } | |
928 | |
929 CountryNames::CountryNames() { | 903 CountryNames::CountryNames() { |
930 // Add 2- and 3-letter ISO country codes. | 904 // Add 2- and 3-letter ISO country codes. |
931 for (CountryDataMap::Iterator it = CountryDataMap::Begin(); | 905 for (CountryDataMap::Iterator it = CountryDataMap::Begin(); |
932 it != CountryDataMap::End(); | 906 it != CountryDataMap::End(); |
933 ++it) { | 907 ++it) { |
934 const std::string& country_code = it->first; | 908 const std::string& country_code = it->first; |
935 std::string iso3_country_code = | 909 std::string iso3_country_code = |
936 icu::Locale(NULL, country_code.c_str()).getISO3Country(); | 910 icu::Locale(NULL, country_code.c_str()).getISO3Country(); |
937 | 911 |
938 common_names_.insert(std::make_pair(country_code, country_code)); | 912 common_names_.insert(std::make_pair(country_code, country_code)); |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1119 | 1093 |
1120 return country_code; | 1094 return country_code; |
1121 } | 1095 } |
1122 | 1096 |
1123 // static | 1097 // static |
1124 const std::string AutofillCountry::GetCountryCode(const string16& country, | 1098 const std::string AutofillCountry::GetCountryCode(const string16& country, |
1125 const std::string& locale) { | 1099 const std::string& locale) { |
1126 return CountryNames::GetInstance()->GetCountryCode(country, locale); | 1100 return CountryNames::GetInstance()->GetCountryCode(country, locale); |
1127 } | 1101 } |
1128 | 1102 |
1129 // static | |
1130 const std::string AutofillCountry::ApplicationLocale() { | |
1131 return CountryNames::GetInstance()->ApplicationLocale(); | |
1132 } | |
1133 | |
1134 AutofillCountry::AutofillCountry(const std::string& country_code, | 1103 AutofillCountry::AutofillCountry(const std::string& country_code, |
1135 const string16& name, | 1104 const string16& name, |
1136 const string16& postal_code_label, | 1105 const string16& postal_code_label, |
1137 const string16& state_label) | 1106 const string16& state_label) |
1138 : country_code_(country_code), | 1107 : country_code_(country_code), |
1139 name_(name), | 1108 name_(name), |
1140 postal_code_label_(postal_code_label), | 1109 postal_code_label_(postal_code_label), |
1141 state_label_(state_label) { | 1110 state_label_(state_label) { |
1142 } | 1111 } |
OLD | NEW |