Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(29)

Side by Side Diff: components/autofill/core/browser/autofill_field.cc

Issue 2025063002: [Autofill] Truncate expiration year based on prediction and max length. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | components/autofill/core/browser/autofill_field_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <stdint.h> 7 #include <stdint.h>
8 8
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/i18n/case_conversion.h" 10 #include "base/i18n/case_conversion.h"
(...skipping 422 matching lines...) Expand 10 before | Expand all | Expand 10 after
433 base::string16 abbreviation; 433 base::string16 abbreviation;
434 state_names::GetNameAndAbbreviation(value, nullptr, &abbreviation); 434 state_names::GetNameAndAbbreviation(value, nullptr, &abbreviation);
435 if (!abbreviation.empty() && field->max_length >= abbreviation.size()) { 435 if (!abbreviation.empty() && field->max_length >= abbreviation.size()) {
436 field->value = base::i18n::ToUpper(abbreviation); 436 field->value = base::i18n::ToUpper(abbreviation);
437 return true; 437 return true;
438 } 438 }
439 } 439 }
440 return false; 440 return false;
441 } 441 }
442 442
443 // Fills the expiration year |value| into the |field|. Uses the |field_type|
444 // and the |field|'s max_length attribute to determine if the |value| needs to
445 // be truncated.
446 void FillExpirationYearInput(base::string16 value,
447 ServerFieldType field_type,
448 FormFieldData* field) {
449 // If the |field_type| requires only 2 digits, keep only the last 2 digits of
450 // |value|.
451 if (field_type == CREDIT_CARD_EXP_2_DIGIT_YEAR && value.length() > 2)
452 value = value.substr(value.length() - 2, 2);
453
454 if (field->max_length == 0 || field->max_length >= value.size()) {
455 // No length restrictions, fill the year value directly.
456 field->value = value;
457 } else {
458 // Truncate the front of |value| to keep only the number of characters equal
459 // to the |field|'s max length.
460 field->value =
461 value.substr(value.length() - field->max_length, field->max_length);
462 }
463 }
464
443 std::string Hash32Bit(const std::string& str) { 465 std::string Hash32Bit(const std::string& str) {
444 std::string hash_bin = base::SHA1HashString(str); 466 std::string hash_bin = base::SHA1HashString(str);
445 DCHECK_EQ(base::kSHA1Length, hash_bin.length()); 467 DCHECK_EQ(base::kSHA1Length, hash_bin.length());
446 468
447 uint32_t hash32 = ((hash_bin[0] & 0xFF) << 24) | 469 uint32_t hash32 = ((hash_bin[0] & 0xFF) << 24) |
448 ((hash_bin[1] & 0xFF) << 16) | ((hash_bin[2] & 0xFF) << 8) | 470 ((hash_bin[1] & 0xFF) << 16) | ((hash_bin[2] & 0xFF) << 8) |
449 (hash_bin[3] & 0xFF); 471 (hash_bin[3] & 0xFF);
450 472
451 return base::UintToString(hash32); 473 return base::UintToString(hash32);
452 } 474 }
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
587 } else if (field_data->form_control_type == "month") { 609 } else if (field_data->form_control_type == "month") {
588 return FillMonthControl(value, field_data); 610 return FillMonthControl(value, field_data);
589 } else if (type.GetStorableType() == ADDRESS_HOME_STREET_ADDRESS) { 611 } else if (type.GetStorableType() == ADDRESS_HOME_STREET_ADDRESS) {
590 FillStreetAddress(value, address_language_code, field_data); 612 FillStreetAddress(value, address_language_code, field_data);
591 return true; 613 return true;
592 } else if (type.GetStorableType() == CREDIT_CARD_NUMBER) { 614 } else if (type.GetStorableType() == CREDIT_CARD_NUMBER) {
593 FillCreditCardNumberField(field, value, field_data); 615 FillCreditCardNumberField(field, value, field_data);
594 return true; 616 return true;
595 } else if (type.GetStorableType() == ADDRESS_HOME_STATE) { 617 } else if (type.GetStorableType() == ADDRESS_HOME_STATE) {
596 return FillStateText(value, field_data); 618 return FillStateText(value, field_data);
619 } else if (field_data->form_control_type == "input" &&
620 (type.GetStorableType() == CREDIT_CARD_EXP_2_DIGIT_YEAR ||
621 type.GetStorableType() == CREDIT_CARD_EXP_4_DIGIT_YEAR)) {
622 FillExpirationYearInput(value, type.GetStorableType(), field_data);
623 return true;
597 } 624 }
598 625
599 field_data->value = value; 626 field_data->value = value;
600 return true; 627 return true;
601 } 628 }
602 629
603 // TODO(crbug.com/581514): Add support for filling only the prefix/suffix for 630 // TODO(crbug.com/581514): Add support for filling only the prefix/suffix for
604 // phone numbers with 10 or 11 digits. 631 // phone numbers with 10 or 11 digits.
605 base::string16 AutofillField::GetPhoneNumberValue( 632 base::string16 AutofillField::GetPhoneNumberValue(
606 const AutofillField& field, 633 const AutofillField& field,
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
673 } 700 }
674 return best_match; 701 return best_match;
675 } 702 }
676 703
677 bool AutofillField::IsCreditCardPrediction() const { 704 bool AutofillField::IsCreditCardPrediction() const {
678 return AutofillType(server_type_).group() == CREDIT_CARD || 705 return AutofillType(server_type_).group() == CREDIT_CARD ||
679 AutofillType(heuristic_type_).group() == CREDIT_CARD; 706 AutofillType(heuristic_type_).group() == CREDIT_CARD;
680 } 707 }
681 708
682 } // namespace autofill 709 } // namespace autofill
OLDNEW
« no previous file with comments | « no previous file | components/autofill/core/browser/autofill_field_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698