OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 #ifndef COMPONENTS_AUTOFILL_CORE_BROWSER_COUNTRY_NAMES_H_ | 5 #ifndef COMPONENTS_AUTOFILL_CORE_BROWSER_COUNTRY_NAMES_H_ |
6 #define COMPONENTS_AUTOFILL_CORE_BROWSER_COUNTRY_NAMES_H_ | 6 #define COMPONENTS_AUTOFILL_CORE_BROWSER_COUNTRY_NAMES_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "base/strings/string16.h" | 14 #include "base/strings/string16.h" |
15 #include "third_party/icu/source/common/unicode/locid.h" | 15 #include "third_party/icu/source/common/unicode/locid.h" |
16 #include "third_party/icu/source/i18n/unicode/coll.h" | 16 #include "third_party/icu/source/i18n/unicode/coll.h" |
17 | 17 |
18 namespace base { | 18 namespace base { |
19 template <typename T> | 19 template <typename T> |
20 struct DefaultSingletonTraits; | 20 struct DefaultSingletonTraits; |
21 } | 21 } |
22 | 22 |
23 namespace autofill { | 23 namespace autofill { |
24 | 24 |
25 // A singleton class that encapsulates mappings from country names to their | 25 // A singleton class that encapsulates mappings from country names to their |
26 // corresponding country codes. | 26 // corresponding country codes. |
27 class CountryNames { | 27 class CountryNames { |
28 public: | 28 public: |
| 29 // The first call to this function, causing the creation of CountryNames, |
| 30 // is expensive. |
29 static CountryNames* GetInstance(); | 31 static CountryNames* GetInstance(); |
30 | 32 |
| 33 // Tells CountryNames, what is the application locale. Only the first supplied |
| 34 // value is used, further calls result in no changes. Call this on the UI |
| 35 // thread, before first using CountryNames. |locale| must not be empty. |
| 36 static void SetLocaleString(std::string locale); |
| 37 |
31 // Returns the country code corresponding to |country|, which should be a | 38 // Returns the country code corresponding to |country|, which should be a |
32 // country code or country name localized to |locale_name|. This function | 39 // country code or country name localized to |locale_name|. |
33 // can be expensive so use judiciously. | 40 const std::string GetCountryCode(const base::string16& country); |
34 const std::string GetCountryCode(const base::string16& country, | 41 |
35 const std::string& locale_name); | 42 protected: |
| 43 // Create CountryNames for |locale_name|. Protected for testing. |
| 44 explicit CountryNames(const std::string& locale_name); |
| 45 |
| 46 // Protected for testing. |
| 47 ~CountryNames(); |
36 | 48 |
37 private: | 49 private: |
| 50 // Create CountryNames for the default locale. |
38 CountryNames(); | 51 CountryNames(); |
39 ~CountryNames(); | 52 |
40 friend struct base::DefaultSingletonTraits<CountryNames>; | 53 friend struct base::DefaultSingletonTraits<CountryNames>; |
41 | 54 |
42 // Populates |locales_to_localized_names_| with the mapping of country names | 55 // Looks up |country_name| in |localized_names|, using |collator| and |
43 // localized to |locale| to their corresponding country codes. Uses a | 56 // returns the corresponding country code or an empty string if there is |
44 // |collator| which is suitable for the locale. | 57 // none. |
45 void AddLocalizedNamesForLocale(const std::string& locale, | |
46 const icu::Collator& collator); | |
47 | |
48 // Interprets |country_name| as a full country name localized to the given | |
49 // |locale| and returns the corresponding country code stored in | |
50 // |locales_to_localized_names_|, or an empty string if there is none. | |
51 const std::string GetCountryCodeForLocalizedName( | 58 const std::string GetCountryCodeForLocalizedName( |
52 const base::string16& country_name, | 59 const base::string16& country_name, |
53 const icu::Locale& locale); | 60 const std::map<std::string, std::string>& localized_names, |
| 61 const icu::Collator& collator); |
54 | 62 |
55 // Returns an ICU collator -- i.e. string comparator -- appropriate for the | 63 // Returns an ICU collator -- i.e. string comparator -- appropriate for the |
56 // given |locale|, or null if no collator is available. | 64 // given |locale|, or null if no collator is available. |
57 const icu::Collator* GetCollatorForLocale(const icu::Locale& locale); | 65 const icu::Collator* GetCollatorForLocale(const icu::Locale& locale); |
58 | 66 |
| 67 // The locale object for the application locale string. |
| 68 const icu::Locale locale_; |
| 69 |
| 70 // Collator for the application locale. |
| 71 const scoped_ptr<icu::Collator> collator_; |
| 72 |
| 73 // Collator for the "en_US" locale, if different from the application |
| 74 // locale, null otherwise. |
| 75 const scoped_ptr<icu::Collator> default_collator_; |
| 76 |
59 // Maps from common country names, including 2- and 3-letter country codes, | 77 // Maps from common country names, including 2- and 3-letter country codes, |
60 // to the corresponding 2-letter country codes. The keys are uppercase ASCII | 78 // to the corresponding 2-letter country codes. The keys are uppercase ASCII |
61 // strings. | 79 // strings. |
62 const std::map<std::string, std::string> common_names_; | 80 const std::map<std::string, std::string> common_names_; |
63 | 81 |
64 // The outer map keys are ICU locale identifiers. | 82 // Maps from localized country names (in the application locale) to their |
65 // The inner maps map from localized country names to their corresponding | 83 // corresponding country codes. The keys are ICU collation sort keys |
66 // country codes. The inner map keys are ICU collation sort keys corresponding | 84 // corresponding to the target localized country name. |
67 // to the target localized country name. | 85 const std::map<std::string, std::string> localized_names_; |
68 std::map<std::string, std::map<std::string, std::string>> | |
69 locales_to_localized_names_; | |
70 | 86 |
71 // Maps ICU locale names to their corresponding collators. | 87 // The same as |localized_names_| but for the "en_US" locale. Empty if |
72 std::map<std::string, scoped_ptr<icu::Collator>> collators_; | 88 // "en_US" is the application locale already. |
| 89 const std::map<std::string, std::string> default_localized_names_; |
73 | 90 |
74 DISALLOW_COPY_AND_ASSIGN(CountryNames); | 91 DISALLOW_COPY_AND_ASSIGN(CountryNames); |
75 }; | 92 }; |
76 | 93 |
77 } // namespace autofill | 94 } // namespace autofill |
78 | 95 |
79 #endif // COMPONENTS_AUTOFILL_CORE_BROWSER_COUNTRY_NAMES_H_ | 96 #endif // COMPONENTS_AUTOFILL_CORE_BROWSER_COUNTRY_NAMES_H_ |
OLD | NEW |