Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/browser/autofill_field.h" | 5 #include "components/autofill/core/browser/autofill_field.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/i18n/string_compare.h" | |
| 8 #include "base/logging.h" | 9 #include "base/logging.h" |
| 9 #include "base/metrics/field_trial.h" | 10 #include "base/metrics/field_trial.h" |
| 10 #include "base/sha1.h" | 11 #include "base/sha1.h" |
| 11 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 12 #include "base/strings/string_split.h" | 13 #include "base/strings/string_split.h" |
| 13 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
| 14 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
| 15 #include "components/autofill/core/browser/autofill_country.h" | 16 #include "components/autofill/core/browser/autofill_country.h" |
| 16 #include "components/autofill/core/browser/autofill_type.h" | 17 #include "components/autofill/core/browser/autofill_type.h" |
| 17 #include "components/autofill/core/browser/phone_number.h" | 18 #include "components/autofill/core/browser/phone_number.h" |
| (...skipping 354 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 372 DCHECK_EQ(20U, hash_bin.length()); | 373 DCHECK_EQ(20U, hash_bin.length()); |
| 373 | 374 |
| 374 uint32 hash32 = ((hash_bin[0] & 0xFF) << 24) | | 375 uint32 hash32 = ((hash_bin[0] & 0xFF) << 24) | |
| 375 ((hash_bin[1] & 0xFF) << 16) | | 376 ((hash_bin[1] & 0xFF) << 16) | |
| 376 ((hash_bin[2] & 0xFF) << 8) | | 377 ((hash_bin[2] & 0xFF) << 8) | |
| 377 (hash_bin[3] & 0xFF); | 378 (hash_bin[3] & 0xFF); |
| 378 | 379 |
| 379 return base::UintToString(hash32); | 380 return base::UintToString(hash32); |
| 380 } | 381 } |
| 381 | 382 |
| 383 scoped_ptr<icu::Collator> CreateCaseInsensitiveCollator() { | |
| 384 UErrorCode error = U_ZERO_ERROR; | |
| 385 scoped_ptr<icu::Collator> collator(icu::Collator::createInstance(error)); | |
| 386 DCHECK(U_SUCCESS(error)); | |
| 387 collator->setStrength(icu::Collator::PRIMARY); | |
| 388 return collator; | |
| 389 } | |
| 390 | |
| 391 base::string16 RemoveWhitespace(const base::string16& value) { | |
| 392 base::string16 stripped_value; | |
| 393 base::RemoveChars(value, base::kWhitespaceUTF16, &stripped_value); | |
| 394 return stripped_value; | |
| 395 } | |
| 396 | |
| 397 bool StringsEqualWithCollator(const base::string16& lhs, | |
| 398 const base::string16& rhs, | |
| 399 icu::Collator* collator) { | |
| 400 return base::i18n::CompareString16WithCollator(collator, lhs, rhs) == | |
|
Evan Stade
2015/04/03 22:02:51
I was expecting this code to use base::CaseInsensi
| |
| 401 UCOL_EQUAL; | |
| 402 } | |
| 403 | |
| 382 } // namespace | 404 } // namespace |
| 383 | 405 |
| 384 AutofillField::AutofillField() | 406 AutofillField::AutofillField() |
| 385 : server_type_(NO_SERVER_DATA), | 407 : server_type_(NO_SERVER_DATA), |
| 386 heuristic_type_(UNKNOWN_TYPE), | 408 heuristic_type_(UNKNOWN_TYPE), |
| 387 html_type_(HTML_TYPE_UNKNOWN), | 409 html_type_(HTML_TYPE_UNKNOWN), |
| 388 html_mode_(HTML_MODE_NONE), | 410 html_mode_(HTML_MODE_NONE), |
| 389 phone_part_(IGNORED), | 411 phone_part_(IGNORED), |
| 390 credit_card_number_offset_(0) { | 412 credit_card_number_offset_(0) { |
| 391 } | 413 } |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 522 number.substr(PhoneNumber::kSuffixOffset, PhoneNumber::kSuffixLength); | 544 number.substr(PhoneNumber::kSuffixOffset, PhoneNumber::kSuffixLength); |
| 523 } | 545 } |
| 524 | 546 |
| 525 return number; | 547 return number; |
| 526 } | 548 } |
| 527 | 549 |
| 528 // static | 550 // static |
| 529 bool AutofillField::FindValueInSelectControl(const FormFieldData& field, | 551 bool AutofillField::FindValueInSelectControl(const FormFieldData& field, |
| 530 const base::string16& value, | 552 const base::string16& value, |
| 531 size_t* index) { | 553 size_t* index) { |
| 532 // TODO(thestig): Improve this. See http://crbug.com/470726) | 554 scoped_ptr<icu::Collator> collator = CreateCaseInsensitiveCollator(); |
| 533 // Try stripping off spaces. | 555 |
| 534 base::string16 value_stripped; | 556 // Strip off spaces for all values in the comparisons. |
| 535 base::RemoveChars(base::StringToLowerASCII(value), base::kWhitespaceUTF16, | 557 const base::string16 value_stripped = RemoveWhitespace(value); |
| 536 &value_stripped); | 558 |
| 537 for (size_t i = 0; i < field.option_values.size(); ++i) { | 559 for (size_t i = 0; i < field.option_values.size(); ++i) { |
| 538 base::string16 option_value_lowercase; | 560 base::string16 option_value = RemoveWhitespace(field.option_values[i]); |
| 539 base::RemoveChars(base::StringToLowerASCII(field.option_values[i]), | 561 if (StringsEqualWithCollator(value_stripped, option_value, |
| 540 base::kWhitespaceUTF16, &option_value_lowercase); | 562 collator.get())) { |
| 541 base::string16 option_contents_lowercase; | |
| 542 base::RemoveChars(base::StringToLowerASCII(field.option_contents[i]), | |
| 543 base::kWhitespaceUTF16, &option_contents_lowercase); | |
| 544 | |
| 545 // Perform a case-insensitive comparison. | |
| 546 if (value_stripped == option_value_lowercase || | |
| 547 value_stripped == option_contents_lowercase) { | |
| 548 if (index) | 563 if (index) |
| 549 *index = i; | 564 *index = i; |
| 550 return true; | 565 return true; |
| 566 } | |
| 567 | |
| 568 base::string16 option_contents = RemoveWhitespace(field.option_contents[i]); | |
| 569 if (StringsEqualWithCollator(value_stripped, option_contents, | |
| 570 collator.get())) { | |
| 571 if (index) | |
| 572 *index = i; | |
| 573 return true; | |
| 551 } | 574 } |
| 552 } | 575 } |
| 553 return false; | 576 return false; |
| 554 } | 577 } |
| 555 | 578 |
| 556 } // namespace autofill | 579 } // namespace autofill |
| OLD | NEW |