diff options
Diffstat (limited to 'chrome/browser/autofill/name_field.cc')
-rw-r--r-- | chrome/browser/autofill/name_field.cc | 128 |
1 files changed, 60 insertions, 68 deletions
diff --git a/chrome/browser/autofill/name_field.cc b/chrome/browser/autofill/name_field.cc index 3c86b39..87f98ce 100644 --- a/chrome/browser/autofill/name_field.cc +++ b/chrome/browser/autofill/name_field.cc @@ -8,60 +8,68 @@ #include "base/memory/scoped_ptr.h" #include "base/string_util.h" #include "base/utf_string_conversions.h" +#include "chrome/browser/autofill/autofill_scanner.h" #include "chrome/browser/autofill/autofill_type.h" #include "grit/autofill_resources.h" #include "ui/base/l10n/l10n_util.h" -NameField* NameField::Parse(std::vector<AutofillField*>::const_iterator* iter, - bool is_ecml) { +NameField* NameField::Parse(AutofillScanner* scanner, bool is_ecml) { + if (scanner->IsEnd()) + return NULL; + // Try FirstLastNameField first since it's more specific. - NameField* field = FirstLastNameField::Parse(iter, is_ecml); - if (field == NULL && !is_ecml) - field = FullNameField::Parse(iter); + NameField* field = FirstLastNameField::Parse(scanner, is_ecml); + if (!field && !is_ecml) + field = FullNameField::Parse(scanner); return field; } bool FullNameField::GetFieldInfo(FieldTypeMap* field_type_map) const { - bool ok = Add(field_type_map, field_, AutofillType(NAME_FULL)); - DCHECK(ok); - return true; + return Add(field_type_map, field_, NAME_FULL); } -FullNameField* FullNameField::Parse( - std::vector<AutofillField*>::const_iterator* iter) { +FullNameField* FullNameField::Parse(AutofillScanner* scanner) { // Exclude labels containing the string "username", which typically // denotes a login ID rather than the user's actual name. - AutofillField* field = **iter; + const AutofillField* field = scanner->Cursor(); if (Match(field, l10n_util::GetStringUTF16(IDS_AUTOFILL_USERNAME_RE), false)) return NULL; // Searching for any label containing the word "name" is too general; // for example, Travelocity_Edit travel profile.html contains a field // "Travel Profile Name". - const string16 name_match = l10n_util::GetStringUTF16(IDS_AUTOFILL_NAME_RE); - if (ParseText(iter, name_match, &field)) + if (ParseText(scanner, l10n_util::GetStringUTF16(IDS_AUTOFILL_NAME_RE), + &field)) return new FullNameField(field); return NULL; } -FullNameField::FullNameField(AutofillField* field) +FullNameField::FullNameField(const AutofillField* field) : field_(field) { } -FirstLastNameField* FirstLastNameField::Parse1( - std::vector<AutofillField*>::const_iterator* iter) { +bool FirstLastNameField::GetFieldInfo(FieldTypeMap* field_type_map) const { + bool ok = Add(field_type_map, first_name_, NAME_FIRST); + ok = ok && Add(field_type_map, last_name_, NAME_LAST); + AutofillFieldType type = middle_initial_ ? NAME_MIDDLE_INITIAL : NAME_MIDDLE; + ok = ok && Add(field_type_map, middle_name_, type); + return ok; +} + +FirstLastNameField* FirstLastNameField::ParseSpecificName( + AutofillScanner* scanner) { // Some pages (e.g. Overstock_comBilling.html, SmithsonianCheckout.html) // have the label "Name" followed by two or three text fields. scoped_ptr<FirstLastNameField> v(new FirstLastNameField); - std::vector<AutofillField*>::const_iterator q = *iter; + scanner->SaveCursor(); - AutofillField* next; - if (ParseText(&q, + const AutofillField* next; + if (ParseText(scanner, l10n_util::GetStringUTF16(IDS_AUTOFILL_NAME_SPECIFIC_RE), &v->first_name_) && - ParseEmptyText(&q, &next)) { - if (ParseEmptyText(&q, &v->last_name_)) { + ParseEmptyText(scanner, &next)) { + if (ParseEmptyText(scanner, &v->last_name_)) { // There are three name fields; assume that the middle one is a // middle initial (it is, at least, on SmithsonianCheckout.html). v->middle_name_ = next; @@ -70,17 +78,17 @@ FirstLastNameField* FirstLastNameField::Parse1( v->last_name_ = next; } - *iter = q; return v.release(); } + scanner->Rewind(); return NULL; } -FirstLastNameField* FirstLastNameField::Parse2( - std::vector<AutofillField*>::const_iterator* iter) { +FirstLastNameField* FirstLastNameField::ParseComponentNames( + AutofillScanner* scanner) { scoped_ptr<FirstLastNameField> v(new FirstLastNameField); - std::vector<AutofillField*>::const_iterator q = *iter; + scanner->SaveCursor(); // A fair number of pages use the names "fname" and "lname" for naming // first and last name fields (examples from the test suite: @@ -90,8 +98,8 @@ FirstLastNameField* FirstLastNameField::Parse2( // so we match "initials" here (and just fill in a first name there, // American-style). // The ".*first$" matches fields ending in "first" (example in sample8.html). - string16 match = l10n_util::GetStringUTF16(IDS_AUTOFILL_FIRST_NAME_RE); - if (!ParseText(&q, match, &v->first_name_)) + if (!ParseText(scanner, l10n_util::GetStringUTF16(IDS_AUTOFILL_FIRST_NAME_RE), + &v->first_name_)) return NULL; // We check for a middle initial before checking for a middle name @@ -99,70 +107,55 @@ FirstLastNameField* FirstLastNameField::Parse2( // as both (the label text is "MI" and the element name is // "txtmiddlename"); such a field probably actually represents a // middle initial. - match = l10n_util::GetStringUTF16(IDS_AUTOFILL_MIDDLE_INITIAL_RE); - if (ParseText(&q, match, &v->middle_name_)) { + if (ParseText(scanner, + l10n_util::GetStringUTF16(IDS_AUTOFILL_MIDDLE_INITIAL_RE), + &v->middle_name_)) { v->middle_initial_ = true; } else { - match = l10n_util::GetStringUTF16(IDS_AUTOFILL_MIDDLE_NAME_RE); - ParseText(&q, match, &v->middle_name_); + ParseText(scanner, l10n_util::GetStringUTF16(IDS_AUTOFILL_MIDDLE_NAME_RE), + &v->middle_name_); } // The ".*last$" matches fields ending in "last" (example in sample8.html). - match = l10n_util::GetStringUTF16(IDS_AUTOFILL_LAST_NAME_RE); - if (!ParseText(&q, match, &v->last_name_)) - return NULL; + if (ParseText(scanner, l10n_util::GetStringUTF16(IDS_AUTOFILL_LAST_NAME_RE), + &v->last_name_)) { + return v.release(); + } - *iter = q; - return v.release(); + scanner->Rewind(); + return NULL; } FirstLastNameField* FirstLastNameField::ParseEcmlName( - std::vector<AutofillField*>::const_iterator* iter) { + AutofillScanner* scanner) { scoped_ptr<FirstLastNameField> field(new FirstLastNameField); - std::vector<AutofillField*>::const_iterator q = *iter; + scanner->SaveCursor(); string16 pattern = GetEcmlPattern(kEcmlShipToFirstName, kEcmlBillToFirstName, '|'); - if (!ParseText(&q, pattern, &field->first_name_)) + if (!ParseText(scanner, pattern, &field->first_name_)) return NULL; pattern = GetEcmlPattern(kEcmlShipToMiddleName, kEcmlBillToMiddleName, '|'); - ParseText(&q, pattern, &field->middle_name_); + ParseText(scanner, pattern, &field->middle_name_); pattern = GetEcmlPattern(kEcmlShipToLastName, kEcmlBillToLastName, '|'); - if (ParseText(&q, pattern, &field->last_name_)) { - *iter = q; + if (ParseText(scanner, pattern, &field->last_name_)) return field.release(); - } + scanner->Rewind(); return NULL; } -FirstLastNameField* FirstLastNameField::Parse( - std::vector<AutofillField*>::const_iterator* iter, - bool is_ecml) { - if (is_ecml) { - return ParseEcmlName(iter); - } else { - FirstLastNameField* v = Parse1(iter); - if (v != NULL) - return v; - - return Parse2(iter); - } -} +FirstLastNameField* FirstLastNameField::Parse(AutofillScanner* scanner, + bool is_ecml) { + if (is_ecml) + return ParseEcmlName(scanner); -bool FirstLastNameField::GetFieldInfo(FieldTypeMap* field_type_map) const { - bool ok = Add(field_type_map, first_name_, AutofillType(NAME_FIRST)); - DCHECK(ok); - ok = ok && Add(field_type_map, last_name_, AutofillType(NAME_LAST)); - DCHECK(ok); - AutofillType type = middle_initial_ ? - AutofillType(NAME_MIDDLE_INITIAL) : AutofillType(NAME_MIDDLE); - ok = ok && Add(field_type_map, middle_name_, type); - DCHECK(ok); - - return ok; + FirstLastNameField* field = ParseSpecificName(scanner); + if (!field) + field = ParseComponentNames(scanner); + return field; } FirstLastNameField::FirstLastNameField() @@ -171,4 +164,3 @@ FirstLastNameField::FirstLastNameField() last_name_(NULL), middle_initial_(false) { } - |