This source file includes following definitions.
- Parse
- is_two_digit_year_
- ClassifyField
#include "components/autofill/core/browser/credit_card_field.h"
#include <stddef.h>
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/strings/string16.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "components/autofill/core/browser/autofill_field.h"
#include "components/autofill/core/browser/autofill_regex_constants.h"
#include "components/autofill/core/browser/autofill_scanner.h"
#include "components/autofill/core/browser/field_types.h"
#include "ui/base/l10n/l10n_util.h"
namespace autofill {
FormField* CreditCardField::Parse(AutofillScanner* scanner) {
if (scanner->IsEnd())
return NULL;
scoped_ptr<CreditCardField> credit_card_field(new CreditCardField);
size_t saved_cursor = scanner->SaveCursor();
for (int fields = 0; !scanner->IsEnd(); ++fields) {
if (ParseField(scanner, base::UTF8ToUTF16(autofill::kGiftCardRe), NULL))
break;
if (credit_card_field->cardholder_ == NULL) {
base::string16 name_pattern;
if (fields == 0 || credit_card_field->expiration_month_) {
name_pattern = base::UTF8ToUTF16(autofill::kNameOnCardRe);
} else {
name_pattern = base::UTF8ToUTF16(autofill::kNameOnCardContextualRe);
}
if (ParseField(scanner, name_pattern, &credit_card_field->cardholder_))
continue;
scanner->SaveCursor();
const AutofillField* first;
if (ParseField(scanner, base::ASCIIToUTF16("^cfnm"), &first) &&
ParseField(scanner, base::ASCIIToUTF16("^clnm"),
&credit_card_field->cardholder_last_)) {
credit_card_field->cardholder_ = first;
continue;
}
scanner->Rewind();
}
base::string16 type_pattern = base::UTF8ToUTF16(autofill::kCardTypeRe);
if (!credit_card_field->type_ &&
ParseFieldSpecifics(scanner, type_pattern,
MATCH_DEFAULT | MATCH_SELECT,
&credit_card_field->type_)) {
continue;
}
base::string16 pattern = base::UTF8ToUTF16(autofill::kCardCvcRe);
if (!credit_card_field->verification_ &&
ParseField(scanner, pattern, &credit_card_field->verification_)) {
continue;
}
pattern = base::UTF8ToUTF16(autofill::kCardNumberRe);
if (!credit_card_field->number_ &&
ParseField(scanner, pattern, &credit_card_field->number_)) {
continue;
}
if (LowerCaseEqualsASCII(scanner->Cursor()->form_control_type, "month")) {
credit_card_field->expiration_date_ = scanner->Cursor();
scanner->Advance();
} else {
scanner->SaveCursor();
pattern = base::UTF8ToUTF16(autofill::kExpirationMonthRe);
if (!credit_card_field->expiration_month_ &&
ParseFieldSpecifics(scanner, pattern, MATCH_DEFAULT | MATCH_SELECT,
&credit_card_field->expiration_month_)) {
pattern = base::UTF8ToUTF16(autofill::kExpirationYearRe);
if (ParseFieldSpecifics(scanner, pattern, MATCH_DEFAULT | MATCH_SELECT,
&credit_card_field->expiration_year_)) {
continue;
}
}
if (!credit_card_field->expiration_date_) {
scanner->Rewind();
pattern = base::UTF8ToUTF16(autofill::kExpirationDate2DigitYearRe);
if (ParseFieldSpecifics(scanner, pattern,
MATCH_LABEL | MATCH_VALUE | MATCH_TEXT |
MATCH_SELECT,
&credit_card_field->expiration_date_)) {
credit_card_field->is_two_digit_year_ = true;
continue;
}
pattern = base::UTF8ToUTF16(autofill::kExpirationDateRe);
if (ParseFieldSpecifics(scanner, pattern,
MATCH_LABEL | MATCH_VALUE | MATCH_TEXT |
MATCH_SELECT,
&credit_card_field->expiration_date_)) {
continue;
}
}
if (credit_card_field->expiration_month_ &&
!credit_card_field->expiration_year_ &&
!credit_card_field->expiration_date_) {
scanner->RewindTo(saved_cursor);
return NULL;
}
}
if (ParseField(scanner, base::UTF8ToUTF16(autofill::kCardIgnoredRe), NULL))
continue;
break;
}
if (credit_card_field->cardholder_)
return credit_card_field.release();
if ((credit_card_field->number_ || credit_card_field->verification_) &&
(credit_card_field->expiration_date_ ||
(credit_card_field->expiration_month_ &&
credit_card_field->expiration_year_))) {
return credit_card_field.release();
}
scanner->RewindTo(saved_cursor);
return NULL;
}
CreditCardField::CreditCardField()
: cardholder_(NULL),
cardholder_last_(NULL),
type_(NULL),
number_(NULL),
verification_(NULL),
expiration_month_(NULL),
expiration_year_(NULL),
expiration_date_(NULL),
is_two_digit_year_(false) {
}
bool CreditCardField::ClassifyField(ServerFieldTypeMap* map) const {
bool ok = AddClassification(number_, CREDIT_CARD_NUMBER, map);
ok = ok && AddClassification(type_, CREDIT_CARD_TYPE, map);
ok = ok && AddClassification(verification_, CREDIT_CARD_VERIFICATION_CODE,
map);
if (cardholder_last_ == NULL)
ok = ok && AddClassification(cardholder_, CREDIT_CARD_NAME, map);
if (expiration_date_) {
if (is_two_digit_year_) {
ok = ok && AddClassification(expiration_date_,
CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR, map);
} else {
ok = ok && AddClassification(expiration_date_,
CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR, map);
}
} else {
ok = ok && AddClassification(expiration_month_, CREDIT_CARD_EXP_MONTH, map);
if (is_two_digit_year_) {
ok = ok && AddClassification(expiration_year_,
CREDIT_CARD_EXP_2_DIGIT_YEAR,
map);
} else {
ok = ok && AddClassification(expiration_year_,
CREDIT_CARD_EXP_4_DIGIT_YEAR,
map);
}
}
return ok;
}
}