Chromium Code Reviews| 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 // Tells CountryNames, what is the application locale. Only the first supplied | |
| 26 // value is used, further calls result in no changes. Call this on the UI | |
| 27 // thread, before first using CountryNames. | |
| 28 void SetLocaleString(std::string locale); | |
|
Ilya Sherman
2016/01/21 02:07:57
nit: Pass by reference.
Ilya Sherman
2016/01/21 02:07:57
Please define this as a static method on CountryNa
vabr (Chromium)
2016/01/22 17:36:42
Good call!
Done.
vabr (Chromium)
2016/01/22 17:36:42
This is by value on purpose. Looking in the SetLoc
Ilya Sherman
2016/01/23 01:41:04
Boy, this seems like a super duper micro-optimizat
vabr (Chromium)
2016/01/25 10:36:44
E-mailing chromium-dev seems like a good idea, I'l
vabr (Chromium)
2016/01/26 09:13:11
Follow-up filed in https://codereview.chromium.org
| |
| 29 | |
| 25 // A singleton class that encapsulates mappings from country names to their | 30 // A singleton class that encapsulates mappings from country names to their |
| 26 // corresponding country codes. | 31 // corresponding country codes. |
| 27 class CountryNames { | 32 class CountryNames { |
| 28 public: | 33 public: |
| 29 static CountryNames* GetInstance(); | 34 static CountryNames* GetInstance(); |
| 30 | 35 |
| 31 // Returns the country code corresponding to |country|, which should be a | 36 // Returns the country code corresponding to |country|, which should be a |
| 32 // country code or country name localized to |locale_name|. This function | 37 // country code or country name localized to |locale_name|. This function |
| 33 // can be expensive so use judiciously. | 38 // can be expensive so use judiciously. |
|
Ilya Sherman
2016/01/21 02:07:57
Can this function still be expensive? It's probab
vabr (Chromium)
2016/01/22 17:36:42
Correct, GetCountryCode is nowhere as expensive as
| |
| 34 const std::string GetCountryCode(const base::string16& country, | 39 const std::string GetCountryCode(const base::string16& country); |
| 35 const std::string& locale_name); | 40 |
| 41 protected: | |
| 42 // Create CountryNames for |locale_name|. Protected for testing. | |
| 43 explicit CountryNames(const std::string& locale_name); | |
| 44 | |
| 45 // Protected for testing. | |
| 46 ~CountryNames(); | |
| 36 | 47 |
| 37 private: | 48 private: |
| 49 // Create CountryNames for the default locale. | |
| 38 CountryNames(); | 50 CountryNames(); |
| 39 ~CountryNames(); | 51 |
| 40 friend struct base::DefaultSingletonTraits<CountryNames>; | 52 friend struct base::DefaultSingletonTraits<CountryNames>; |
| 41 | 53 |
| 42 // Populates |locales_to_localized_names_| with the mapping of country names | 54 // Looks up |country_name| in |localized_names|, using |collator| andd |
|
Ilya Sherman
2016/01/21 02:07:56
nit: s/andd/and
vabr (Chromium)
2016/01/22 17:36:42
Done.
| |
| 43 // localized to |locale| to their corresponding country codes. Uses a | 55 // returns the corresponding country code or an empty string if there is |
| 44 // |collator| which is suitable for the locale. | 56 // 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( | 57 const std::string GetCountryCodeForLocalizedName( |
| 52 const base::string16& country_name, | 58 const base::string16& country_name, |
| 53 const icu::Locale& locale); | 59 const std::map<std::string, std::string>& localized_names, |
| 60 const icu::Collator& collator); | |
| 54 | 61 |
| 55 // Returns an ICU collator -- i.e. string comparator -- appropriate for the | 62 // Returns an ICU collator -- i.e. string comparator -- appropriate for the |
| 56 // given |locale|, or null if no collator is available. | 63 // given |locale|, or null if no collator is available. |
| 57 const icu::Collator* GetCollatorForLocale(const icu::Locale& locale); | 64 const icu::Collator* GetCollatorForLocale(const icu::Locale& locale); |
| 58 | 65 |
| 66 // The locale object for the application locale string. | |
| 67 const icu::Locale locale_; | |
| 68 | |
| 69 // Collator for the application locale. | |
| 70 const scoped_ptr<icu::Collator> collator_; | |
| 71 | |
| 72 // Collator for the "en_US" locale, if different from the application | |
| 73 // locale, null otherwise. | |
| 74 const scoped_ptr<icu::Collator> default_collator_; | |
| 75 | |
| 59 // Maps from common country names, including 2- and 3-letter country codes, | 76 // 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 | 77 // to the corresponding 2-letter country codes. The keys are uppercase ASCII |
| 61 // strings. | 78 // strings. |
| 62 const std::map<std::string, std::string> common_names_; | 79 const std::map<std::string, std::string> common_names_; |
| 63 | 80 |
| 64 // The outer map keys are ICU locale identifiers. | 81 // Maps from localized country names (in the application locale) to their |
| 65 // The inner maps map from localized country names to their corresponding | 82 // corresponding country codes. The keys are ICU collation sort keys |
| 66 // country codes. The inner map keys are ICU collation sort keys corresponding | 83 // corresponding to the target localized country name. |
| 67 // to the target localized country name. | 84 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 | 85 |
| 71 // Maps ICU locale names to their corresponding collators. | 86 // The same as |localized_names_| but for the "en_US" locale. Empty if |
| 72 std::map<std::string, scoped_ptr<icu::Collator>> collators_; | 87 // "en_US" is the application locale already. |
| 88 const std::map<std::string, std::string> default_localized_names_; | |
| 73 | 89 |
| 74 DISALLOW_COPY_AND_ASSIGN(CountryNames); | 90 DISALLOW_COPY_AND_ASSIGN(CountryNames); |
| 75 }; | 91 }; |
| 76 | 92 |
| 77 } // namespace autofill | 93 } // namespace autofill |
| 78 | 94 |
| 79 #endif // COMPONENTS_AUTOFILL_CORE_BROWSER_COUNTRY_NAMES_H_ | 95 #endif // COMPONENTS_AUTOFILL_CORE_BROWSER_COUNTRY_NAMES_H_ |
| OLD | NEW |