OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/core/common/autofill_l10n_util.h" | 5 #include "components/autofill/core/common/autofill_l10n_util.h" |
6 | 6 |
7 #include "base/i18n/string_compare.h" | 7 #include "base/i18n/string_compare.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/metrics/histogram_macros.h" |
9 | 10 |
10 namespace autofill { | 11 namespace autofill { |
11 namespace l10n { | 12 namespace l10n { |
12 | 13 |
13 CaseInsensitiveCompare::CaseInsensitiveCompare() { | 14 CaseInsensitiveCompare::CaseInsensitiveCompare() |
| 15 : CaseInsensitiveCompare(icu::Locale::getDefault()) {} |
| 16 |
| 17 CaseInsensitiveCompare::CaseInsensitiveCompare(const icu::Locale& locale) { |
14 UErrorCode error = U_ZERO_ERROR; | 18 UErrorCode error = U_ZERO_ERROR; |
15 collator_.reset(icu::Collator::createInstance(error)); | 19 collator_.reset(icu::Collator::createInstance(locale, error)); |
16 DCHECK(U_SUCCESS(error)); | 20 if (!collator_) { |
17 collator_->setStrength(icu::Collator::PRIMARY); | 21 // On some systems, the default locale is invalid to the eyes of the ICU |
| 22 // library. This could be due to a device-specific issue (has been seen in |
| 23 // the wild on Android devices). In the failure case, |collator_| will be |
| 24 // null. See http://crbug.com/558625. |
| 25 icu_54::UnicodeString name; |
| 26 std::string locale_name; |
| 27 locale.getDisplayName(name).toUTF8String(locale_name); |
| 28 LOG(ERROR) << "Failed to initialize the ICU Collator for " |
| 29 << "CaseInsensitiveCompare with locale" << locale_name; |
| 30 // Attempt to load the English locale. |
| 31 collator_.reset(icu::Collator::createInstance(icu::Locale::getEnglish(), |
| 32 error)); |
| 33 } |
| 34 |
| 35 if (collator_) { |
| 36 collator_->setStrength(icu::Collator::PRIMARY); |
| 37 } else { |
| 38 LOG(ERROR) << "Failed to initialize the ICU Collator for " |
| 39 << "CaseInsensitiveCompare with the English locale."; |
| 40 } |
| 41 UMA_HISTOGRAM_BOOLEAN("Autofill.IcuCollatorCreationSuccess", !!collator_); |
18 } | 42 } |
19 | 43 |
20 CaseInsensitiveCompare::~CaseInsensitiveCompare() { | 44 CaseInsensitiveCompare::~CaseInsensitiveCompare() { |
21 } | 45 } |
22 | 46 |
23 bool CaseInsensitiveCompare::StringsEqual(const base::string16& lhs, | 47 bool CaseInsensitiveCompare::StringsEqual(const base::string16& lhs, |
24 const base::string16& rhs) const { | 48 const base::string16& rhs) const { |
25 return base::i18n::CompareString16WithCollator(*collator_, lhs, rhs) == | 49 if (collator_) { |
26 UCOL_EQUAL; | 50 return base::i18n::CompareString16WithCollator(*collator_, lhs, rhs) == |
| 51 UCOL_EQUAL; |
| 52 } |
| 53 return lhs == rhs; |
27 } | 54 } |
28 | 55 |
29 } // namespace l10n | 56 } // namespace l10n |
30 } // namespace autofill | 57 } // namespace autofill |
OLD | NEW |