diff options
Diffstat (limited to 'chrome/browser/autofill/credit_card_field.cc')
-rw-r--r-- | chrome/browser/autofill/credit_card_field.cc | 92 |
1 files changed, 41 insertions, 51 deletions
diff --git a/chrome/browser/autofill/credit_card_field.cc b/chrome/browser/autofill/credit_card_field.cc index 9e31e32..b322574 100644 --- a/chrome/browser/autofill/credit_card_field.cc +++ b/chrome/browser/autofill/credit_card_field.cc @@ -12,52 +12,41 @@ #include "base/string_util.h" #include "base/utf_string_conversions.h" #include "chrome/browser/autofill/autofill_field.h" +#include "chrome/browser/autofill/autofill_scanner.h" #include "chrome/browser/autofill/field_types.h" #include "grit/autofill_resources.h" #include "ui/base/l10n/l10n_util.h" bool CreditCardField::GetFieldInfo(FieldTypeMap* field_type_map) const { - bool ok = Add(field_type_map, number_, AutofillType(CREDIT_CARD_NUMBER)); - DCHECK(ok); + bool ok = Add(field_type_map, number_, CREDIT_CARD_NUMBER); // If the heuristics detected first and last name in separate fields, // then ignore both fields. Putting them into separate fields is probably // wrong, because the credit card can also contain a middle name or middle // initial. - if (cardholder_last_ == NULL) { - // Add() will check if cardholder_ is != NULL. - ok = ok && Add(field_type_map, cardholder_, AutofillType(CREDIT_CARD_NAME)); - DCHECK(ok); - } + if (cardholder_last_ == NULL) + ok = ok && Add(field_type_map, cardholder_, CREDIT_CARD_NAME); - ok = ok && Add(field_type_map, type_, AutofillType(CREDIT_CARD_TYPE)); - DCHECK(ok); - ok = ok && Add(field_type_map, expiration_month_, - AutofillType(CREDIT_CARD_EXP_MONTH)); - DCHECK(ok); + ok = ok && Add(field_type_map, type_, CREDIT_CARD_TYPE); + ok = ok && Add(field_type_map, expiration_month_, CREDIT_CARD_EXP_MONTH); ok = ok && Add(field_type_map, expiration_year_, - AutofillType(CREDIT_CARD_EXP_4_DIGIT_YEAR)); - DCHECK(ok); - + CREDIT_CARD_EXP_4_DIGIT_YEAR); return ok; } -FormFieldType CreditCardField::GetFormFieldType() const { - return kCreditCardType; -} - // static -CreditCardField* CreditCardField::Parse( - std::vector<AutofillField*>::const_iterator* iter, - bool is_ecml) { +CreditCardField* CreditCardField::Parse(AutofillScanner* scanner, + bool is_ecml) { + if (scanner->IsEnd()) + return NULL; + scoped_ptr<CreditCardField> credit_card_field(new CreditCardField); - std::vector<AutofillField*>::const_iterator q = *iter; - string16 pattern; + scanner->SaveCursor(); // Credit card fields can appear in many different orders. // We loop until no more credit card related fields are found, see |break| at // bottom of the loop. - for (int fields = 0; true; ++fields) { + for (int fields = 0; !scanner->IsEnd(); ++fields) { // Sometimes the cardholder field is just labeled "name". Unfortunately this // is a dangerously generic word to search for, since it will often match a // name (not cardholder name) field before or after credit card fields. So @@ -79,22 +68,22 @@ CreditCardField* CreditCardField::Parse( } } - if (ParseText(&q, name_pattern, &credit_card_field->cardholder_)) + if (ParseText(scanner, name_pattern, &credit_card_field->cardholder_)) continue; // As a hard-coded hack for Expedia's billing pages (expedia_checkout.html // and ExpediaBilling.html in our test suite), recognize separate fields // for the cardholder's first and last name if they have the labels "cfnm" // and "clnm". - std::vector<AutofillField*>::const_iterator p = q; - AutofillField* first; - if (!is_ecml && ParseText(&p, ASCIIToUTF16("^cfnm"), &first) && - ParseText(&p, ASCIIToUTF16("^clnm"), + scanner->SaveCursor(); + const AutofillField* first; + if (!is_ecml && ParseText(scanner, ASCIIToUTF16("^cfnm"), &first) && + ParseText(scanner, ASCIIToUTF16("^clnm"), &credit_card_field->cardholder_last_)) { credit_card_field->cardholder_ = first; - q = p; continue; } + scanner->Rewind(); } // We look for a card security code before we look for a credit @@ -102,14 +91,14 @@ CreditCardField* CreditCardField::Parse( // has a plethora of names; we've seen "verification #", // "verification number", "card identification number" and others listed // in the |pattern| below. - if (is_ecml) { + string16 pattern; + if (is_ecml) pattern = GetEcmlPattern(kEcmlCardVerification); - } else { + else pattern = l10n_util::GetStringUTF16(IDS_AUTOFILL_CARD_CVC_RE); - } - if (credit_card_field->verification_ == NULL && - ParseText(&q, pattern, &credit_card_field->verification_)) + if (!credit_card_field->verification_ && + ParseText(scanner, pattern, &credit_card_field->verification_)) continue; // TODO(jhawkins): Parse the type select control. @@ -119,12 +108,13 @@ CreditCardField* CreditCardField::Parse( else pattern = l10n_util::GetStringUTF16(IDS_AUTOFILL_CARD_NUMBER_RE); - if (credit_card_field->number_ == NULL && ParseText(&q, pattern, - &credit_card_field->number_)) + if (!credit_card_field->number_ && + ParseText(scanner, pattern, &credit_card_field->number_)) continue; - if ((*q) && LowerCaseEqualsASCII((*q)->form_control_type, "month")) { - credit_card_field->expiration_month_ = *q++; + if (LowerCaseEqualsASCII(scanner->Cursor()->form_control_type, "month")) { + credit_card_field->expiration_month_ = scanner->Cursor(); + scanner->Advance(); } else { // "Expiration date" is the most common label here, but some pages have // "Expires", "exp. date" or "exp. month" and "exp. year". We also look @@ -144,22 +134,23 @@ CreditCardField* CreditCardField::Parse( pattern = l10n_util::GetStringUTF16(IDS_AUTOFILL_EXPIRATION_MONTH_RE); if ((!credit_card_field->expiration_month_ || - credit_card_field->expiration_month_->IsEmpty()) && - ParseText(&q, pattern, &credit_card_field->expiration_month_)) { - + credit_card_field->expiration_month_->IsEmpty()) && + ParseText(scanner, pattern, &credit_card_field->expiration_month_)) { if (is_ecml) pattern = GetEcmlPattern(kEcmlCardExpireYear); else pattern = l10n_util::GetStringUTF16(IDS_AUTOFILL_EXPIRATION_DATE_RE); - if (!ParseText(&q, pattern, &credit_card_field->expiration_year_)) { + if (!ParseText(scanner, pattern, + &credit_card_field->expiration_year_)) { + scanner->Rewind(); return NULL; } continue; } } - if (ParseText(&q, GetEcmlPattern(kEcmlCardExpireDay))) + if (ParseText(scanner, GetEcmlPattern(kEcmlCardExpireDay))) continue; // Some pages (e.g. ExpediaBilling.html) have a "card description" @@ -167,7 +158,8 @@ CreditCardField* CreditCardField::Parse( // We also ignore any other fields within a credit card block that // start with "card", under the assumption that they are related to // the credit card section being processed but are uninteresting to us. - if (ParseText(&q, l10n_util::GetStringUTF16(IDS_AUTOFILL_CARD_IGNORED_RE))) + if (ParseText(scanner, + l10n_util::GetStringUTF16(IDS_AUTOFILL_CARD_IGNORED_RE))) continue; break; @@ -176,10 +168,8 @@ CreditCardField* CreditCardField::Parse( // Some pages have a billing address field after the cardholder name field. // For that case, allow only just the cardholder name field. The remaining // CC fields will be picked up in a following CreditCardField. - if (credit_card_field->cardholder_) { - *iter = q; + if (credit_card_field->cardholder_) return credit_card_field.release(); - } // On some pages, the user selects a card type using radio buttons // (e.g. test page Apple Store Billing.html). We can't handle that yet, @@ -194,10 +184,10 @@ CreditCardField* CreditCardField::Parse( (LowerCaseEqualsASCII( credit_card_field->expiration_month_->form_control_type, "month")))) { - *iter = q; - return credit_card_field.release(); + return credit_card_field.release(); } + scanner->Rewind(); return NULL; } |