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 "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/threading/thread_checker.h" |
17 #include "base/utf_string_conversions.h" | 18 #include "base/utf_string_conversions.h" |
18 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
19 #include "content/public/browser/content_browser_client.h" | 20 #include "content/public/browser/content_browser_client.h" |
20 #include "grit/generated_resources.h" | 21 #include "grit/generated_resources.h" |
21 #include "third_party/icu/public/common/unicode/locid.h" | 22 #include "third_party/icu/public/common/unicode/locid.h" |
22 #include "third_party/icu/public/common/unicode/uloc.h" | 23 #include "third_party/icu/public/common/unicode/uloc.h" |
23 #include "third_party/icu/public/common/unicode/unistr.h" | 24 #include "third_party/icu/public/common/unicode/unistr.h" |
24 #include "third_party/icu/public/common/unicode/urename.h" | 25 #include "third_party/icu/public/common/unicode/urename.h" |
25 #include "third_party/icu/public/common/unicode/utypes.h" | 26 #include "third_party/icu/public/common/unicode/utypes.h" |
26 #include "third_party/icu/public/i18n/unicode/coll.h" | 27 #include "third_party/icu/public/i18n/unicode/coll.h" |
(...skipping 601 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
628 | 629 |
629 // Returns the ICU sort key corresponding to |str| for the given |collator|. | 630 // Returns the ICU sort key corresponding to |str| for the given |collator|. |
630 // Uses |buffer| as temporary storage, and might resize |buffer| as a side- | 631 // Uses |buffer| as temporary storage, and might resize |buffer| as a side- |
631 // effect. |buffer_size| should specify the |buffer|'s size, and is updated if | 632 // effect. |buffer_size| should specify the |buffer|'s size, and is updated if |
632 // the |buffer| is resized. | 633 // the |buffer| is resized. |
633 const std::string GetSortKey(const icu::Collator& collator, | 634 const std::string GetSortKey(const icu::Collator& collator, |
634 const string16& str, | 635 const string16& str, |
635 scoped_array<uint8_t>* buffer, | 636 scoped_array<uint8_t>* buffer, |
636 int32_t* buffer_size) const; | 637 int32_t* buffer_size) const; |
637 | 638 |
638 | |
639 // Maps from common country names, including 2- and 3-letter country codes, | 639 // Maps from common country names, including 2- and 3-letter country codes, |
640 // to the corresponding 2-letter country codes. The keys are uppercase ASCII | 640 // to the corresponding 2-letter country codes. The keys are uppercase ASCII |
641 // strings. | 641 // strings. |
642 std::map<std::string, std::string> common_names_; | 642 std::map<std::string, std::string> common_names_; |
643 | 643 |
644 // The outer map keys are ICU locale identifiers. | 644 // The outer map keys are ICU locale identifiers. |
645 // The inner maps map from localized country names to their corresponding | 645 // The inner maps map from localized country names to their corresponding |
646 // country codes. The inner map keys are ICU collation sort keys corresponding | 646 // country codes. The inner map keys are ICU collation sort keys corresponding |
647 // to the target localized country name. | 647 // to the target localized country name. |
648 std::map<std::string, std::map<std::string, std::string> > | 648 std::map<std::string, std::map<std::string, std::string> > |
649 locales_to_localized_names_; | 649 locales_to_localized_names_; |
650 | 650 |
651 // Maps ICU locale names to their corresponding collators. | 651 // Maps ICU locale names to their corresponding collators. |
652 std::map<std::string, icu::Collator*> collators_; | 652 std::map<std::string, icu::Collator*> collators_; |
653 | 653 |
| 654 // Verifies thread-safety of accesses to the application locale. |
| 655 base::ThreadChecker thread_checker_; |
| 656 |
654 // Caches the application locale, for thread-safe access. | 657 // Caches the application locale, for thread-safe access. |
655 std::string application_locale_; | 658 std::string application_locale_; |
656 | 659 |
657 DISALLOW_COPY_AND_ASSIGN(CountryNames); | 660 DISALLOW_COPY_AND_ASSIGN(CountryNames); |
658 }; | 661 }; |
659 | 662 |
660 // static | 663 // static |
661 CountryNames* CountryNames::GetInstance() { | 664 CountryNames* CountryNames::GetInstance() { |
662 return Singleton<CountryNames>::get(); | 665 return Singleton<CountryNames>::get(); |
663 } | 666 } |
664 | 667 |
665 const std::string CountryNames::ApplicationLocale() { | 668 const std::string CountryNames::ApplicationLocale() { |
666 if (application_locale_.empty()) { | 669 if (application_locale_.empty()) { |
667 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 670 // In production code, this class is always constructed on the UI thread, so |
| 671 // the two conditions in the below DCHECK are identical. In test code, |
| 672 // sometimes there is a UI thread, and sometimes there is just the unnamed |
| 673 // main thread. Since this class is a singleton, it needs to support both |
| 674 // cases. Hence, the somewhat strange looking DCHECK below. |
| 675 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || |
| 676 thread_checker_.CalledOnValidThread()); |
668 application_locale_ = | 677 application_locale_ = |
669 content::GetContentClient()->browser()->GetApplicationLocale(); | 678 content::GetContentClient()->browser()->GetApplicationLocale(); |
670 } | 679 } |
671 | 680 |
672 return application_locale_; | 681 return application_locale_; |
673 } | 682 } |
674 | 683 |
675 CountryNames::CountryNames() { | 684 CountryNames::CountryNames() { |
676 // Add 2- and 3-letter ISO country codes. | 685 // Add 2- and 3-letter ISO country codes. |
677 for (CountryDataMap::Iterator it = CountryDataMap::Begin(); | 686 for (CountryDataMap::Iterator it = CountryDataMap::Begin(); |
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
878 | 887 |
879 AutofillCountry::AutofillCountry(const std::string& country_code, | 888 AutofillCountry::AutofillCountry(const std::string& country_code, |
880 const string16& name, | 889 const string16& name, |
881 const string16& postal_code_label, | 890 const string16& postal_code_label, |
882 const string16& state_label) | 891 const string16& state_label) |
883 : country_code_(country_code), | 892 : country_code_(country_code), |
884 name_(name), | 893 name_(name), |
885 postal_code_label_(postal_code_label), | 894 postal_code_label_(postal_code_label), |
886 state_label_(state_label) { | 895 state_label_(state_label) { |
887 } | 896 } |
OLD | NEW |