| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "chrome/browser/autofill/autofill_country.h" | 5 #include "chrome/browser/autofill/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/utf_string_conversions.h" | 17 #include "base/utf_string_conversions.h" |
| 18 #include "chrome/browser/browser_process.h" | 18 #include "chrome/browser/browser_process.h" |
| 19 #include "content/browser/browser_thread.h" |
| 19 #include "grit/generated_resources.h" | 20 #include "grit/generated_resources.h" |
| 20 #include "ui/base/l10n/l10n_util.h" | 21 #include "ui/base/l10n/l10n_util.h" |
| 21 #include "unicode/coll.h" | 22 #include "unicode/coll.h" |
| 22 #include "unicode/locid.h" | 23 #include "unicode/locid.h" |
| 23 #include "unicode/ucol.h" | 24 #include "unicode/ucol.h" |
| 24 #include "unicode/uloc.h" | 25 #include "unicode/uloc.h" |
| 25 #include "unicode/unistr.h" | 26 #include "unicode/unistr.h" |
| 26 #include "unicode/urename.h" | 27 #include "unicode/urename.h" |
| 27 #include "unicode/utypes.h" | 28 #include "unicode/utypes.h" |
| 28 | 29 |
| (...skipping 319 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 348 const std::string& country_code) { | 349 const std::string& country_code) { |
| 349 return GetInstance()->country_data_.find(country_code); | 350 return GetInstance()->country_data_.find(country_code); |
| 350 } | 351 } |
| 351 | 352 |
| 352 // A singleton class that encapsulates mappings from country names to their | 353 // A singleton class that encapsulates mappings from country names to their |
| 353 // corresponding country codes. | 354 // corresponding country codes. |
| 354 class CountryNames { | 355 class CountryNames { |
| 355 public: | 356 public: |
| 356 static CountryNames* GetInstance(); | 357 static CountryNames* GetInstance(); |
| 357 | 358 |
| 359 // Returns the application locale. |
| 360 const std::string ApplicationLocale(); |
| 361 |
| 358 // Returns the country code corresponding to |country|, which should be a | 362 // Returns the country code corresponding to |country|, which should be a |
| 359 // country code or country name localized to |locale|. | 363 // country code or country name localized to |locale|. |
| 360 const std::string GetCountryCode(const string16& country, | 364 const std::string GetCountryCode(const string16& country, |
| 361 const std::string& locale); | 365 const std::string& locale); |
| 362 | 366 |
| 363 private: | 367 private: |
| 364 CountryNames(); | 368 CountryNames(); |
| 365 ~CountryNames(); | 369 ~CountryNames(); |
| 366 friend struct DefaultSingletonTraits<CountryNames>; | 370 friend struct DefaultSingletonTraits<CountryNames>; |
| 367 | 371 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 397 // The outer map keys are ICU locale identifiers. | 401 // The outer map keys are ICU locale identifiers. |
| 398 // The inner maps map from localized country names to their corresponding | 402 // The inner maps map from localized country names to their corresponding |
| 399 // country codes. The inner map keys are ICU collation sort keys corresponding | 403 // country codes. The inner map keys are ICU collation sort keys corresponding |
| 400 // to the target localized country name. | 404 // to the target localized country name. |
| 401 std::map<std::string, std::map<std::string, std::string> > | 405 std::map<std::string, std::map<std::string, std::string> > |
| 402 locales_to_localized_names_; | 406 locales_to_localized_names_; |
| 403 | 407 |
| 404 // Maps ICU locale names to their corresponding collators. | 408 // Maps ICU locale names to their corresponding collators. |
| 405 std::map<std::string, icu::Collator*> collators_; | 409 std::map<std::string, icu::Collator*> collators_; |
| 406 | 410 |
| 411 // Caches the application locale, for thread-safe access. |
| 412 std::string application_locale_; |
| 413 |
| 407 DISALLOW_COPY_AND_ASSIGN(CountryNames); | 414 DISALLOW_COPY_AND_ASSIGN(CountryNames); |
| 408 }; | 415 }; |
| 409 | 416 |
| 410 // static | 417 // static |
| 411 CountryNames* CountryNames::GetInstance() { | 418 CountryNames* CountryNames::GetInstance() { |
| 412 return Singleton<CountryNames>::get(); | 419 return Singleton<CountryNames>::get(); |
| 413 } | 420 } |
| 414 | 421 |
| 422 const std::string CountryNames::ApplicationLocale() { |
| 423 if (application_locale_.empty()) { |
| 424 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 425 application_locale_ = g_browser_process->GetApplicationLocale(); |
| 426 } |
| 427 |
| 428 return application_locale_; |
| 429 } |
| 430 |
| 415 CountryNames::CountryNames() { | 431 CountryNames::CountryNames() { |
| 416 // Add 2- and 3-letter ISO country codes. | 432 // Add 2- and 3-letter ISO country codes. |
| 417 for (CountryDataMap::Iterator it = CountryDataMap::Begin(); | 433 for (CountryDataMap::Iterator it = CountryDataMap::Begin(); |
| 418 it != CountryDataMap::End(); | 434 it != CountryDataMap::End(); |
| 419 ++it) { | 435 ++it) { |
| 420 const std::string& country_code = it->first; | 436 const std::string& country_code = it->first; |
| 421 std::string iso3_country_code = | 437 std::string iso3_country_code = |
| 422 icu::Locale(NULL, country_code.c_str()).getISO3Country(); | 438 icu::Locale(NULL, country_code.c_str()).getISO3Country(); |
| 423 | 439 |
| 424 common_names_.insert(std::make_pair(country_code, country_code)); | 440 common_names_.insert(std::make_pair(country_code, country_code)); |
| (...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 621 } | 637 } |
| 622 | 638 |
| 623 // static | 639 // static |
| 624 const std::string AutofillCountry::GetCountryCode(const string16& country, | 640 const std::string AutofillCountry::GetCountryCode(const string16& country, |
| 625 const std::string& locale) { | 641 const std::string& locale) { |
| 626 return CountryNames::GetInstance()->GetCountryCode(country, locale); | 642 return CountryNames::GetInstance()->GetCountryCode(country, locale); |
| 627 } | 643 } |
| 628 | 644 |
| 629 // static | 645 // static |
| 630 const std::string AutofillCountry::ApplicationLocale() { | 646 const std::string AutofillCountry::ApplicationLocale() { |
| 631 return g_browser_process->GetApplicationLocale(); | 647 return CountryNames::GetInstance()->ApplicationLocale(); |
| 632 } | 648 } |
| 633 | 649 |
| 634 AutofillCountry::AutofillCountry(const std::string& country_code, | 650 AutofillCountry::AutofillCountry(const std::string& country_code, |
| 635 const string16& name, | 651 const string16& name, |
| 636 const string16& postal_code_label, | 652 const string16& postal_code_label, |
| 637 const string16& state_label) | 653 const string16& state_label) |
| 638 : country_code_(country_code), | 654 : country_code_(country_code), |
| 639 name_(name), | 655 name_(name), |
| 640 postal_code_label_(postal_code_label), | 656 postal_code_label_(postal_code_label), |
| 641 state_label_(state_label) { | 657 state_label_(state_label) { |
| 642 } | 658 } |
| OLD | NEW |