1// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/autofill/name_field.h"
6
7#include "base/logging.h"
8#include "base/memory/scoped_ptr.h"
9#include "base/string_util.h"
10#include "base/utf_string_conversions.h"
11#include "chrome/browser/autofill/autofill_type.h"
12#include "grit/autofill_resources.h"
13#include "ui/base/l10n/l10n_util.h"
14
15NameField* NameField::Parse(std::vector<AutofillField*>::const_iterator* iter,
16                            bool is_ecml) {
17  // Try FirstLastNameField first since it's more specific.
18  NameField* field = FirstLastNameField::Parse(iter, is_ecml);
19  if (field == NULL && !is_ecml)
20    field = FullNameField::Parse(iter);
21  return field;
22}
23
24bool FullNameField::GetFieldInfo(FieldTypeMap* field_type_map) const {
25  bool ok = Add(field_type_map, field_, AutofillType(NAME_FULL));
26  DCHECK(ok);
27  return true;
28}
29
30FullNameField* FullNameField::Parse(
31    std::vector<AutofillField*>::const_iterator* iter) {
32  // Exclude labels containing the string "username", which typically
33  // denotes a login ID rather than the user's actual name.
34  AutofillField* field = **iter;
35  if (Match(field, l10n_util::GetStringUTF16(IDS_AUTOFILL_USERNAME_RE), false))
36    return NULL;
37
38  // Searching for any label containing the word "name" is too general;
39  // for example, Travelocity_Edit travel profile.html contains a field
40  // "Travel Profile Name".
41  const string16 name_match = l10n_util::GetStringUTF16(IDS_AUTOFILL_NAME_RE);
42  if (ParseText(iter, name_match, &field))
43    return new FullNameField(field);
44
45  return NULL;
46}
47
48FullNameField::FullNameField(AutofillField* field)
49    : field_(field) {
50}
51
52FirstLastNameField* FirstLastNameField::Parse1(
53    std::vector<AutofillField*>::const_iterator* iter) {
54  // Some pages (e.g. Overstock_comBilling.html, SmithsonianCheckout.html)
55  // have the label "Name" followed by two or three text fields.
56  scoped_ptr<FirstLastNameField> v(new FirstLastNameField);
57  std::vector<AutofillField*>::const_iterator q = *iter;
58
59  AutofillField* next;
60  if (ParseText(&q,
61                l10n_util::GetStringUTF16(IDS_AUTOFILL_NAME_SPECIFIC_RE),
62                &v->first_name_) &&
63      ParseEmptyText(&q, &next)) {
64    if (ParseEmptyText(&q, &v->last_name_)) {
65      // There are three name fields; assume that the middle one is a
66      // middle initial (it is, at least, on SmithsonianCheckout.html).
67      v->middle_name_ = next;
68      v->middle_initial_ = true;
69    } else {  // only two name fields
70      v->last_name_ = next;
71    }
72
73    *iter = q;
74    return v.release();
75  }
76
77  return NULL;
78}
79
80FirstLastNameField* FirstLastNameField::Parse2(
81    std::vector<AutofillField*>::const_iterator* iter) {
82  scoped_ptr<FirstLastNameField> v(new FirstLastNameField);
83  std::vector<AutofillField*>::const_iterator q = *iter;
84
85  // A fair number of pages use the names "fname" and "lname" for naming
86  // first and last name fields (examples from the test suite:
87  // BESTBUY_COM - Sign In2.html; Crate and Barrel Check Out.html;
88  // dell_checkout1.html).  At least one UK page (The China Shop2.html)
89  // asks, in stuffy English style, for just initials and a surname,
90  // so we match "initials" here (and just fill in a first name there,
91  // American-style).
92  // The ".*first$" matches fields ending in "first" (example in sample8.html).
93  string16 match = l10n_util::GetStringUTF16(IDS_AUTOFILL_FIRST_NAME_RE);
94  if (!ParseText(&q, match, &v->first_name_))
95    return NULL;
96
97  // We check for a middle initial before checking for a middle name
98  // because at least one page (PC Connection.html) has a field marked
99  // as both (the label text is "MI" and the element name is
100  // "txtmiddlename"); such a field probably actually represents a
101  // middle initial.
102  match = l10n_util::GetStringUTF16(IDS_AUTOFILL_MIDDLE_INITIAL_RE);
103  if (ParseText(&q, match, &v->middle_name_)) {
104    v->middle_initial_ = true;
105  } else {
106    match = l10n_util::GetStringUTF16(IDS_AUTOFILL_MIDDLE_NAME_RE);
107    ParseText(&q, match, &v->middle_name_);
108  }
109
110  // The ".*last$" matches fields ending in "last" (example in sample8.html).
111  match = l10n_util::GetStringUTF16(IDS_AUTOFILL_LAST_NAME_RE);
112  if (!ParseText(&q, match, &v->last_name_))
113    return NULL;
114
115  *iter = q;
116  return v.release();
117}
118
119FirstLastNameField* FirstLastNameField::ParseEcmlName(
120    std::vector<AutofillField*>::const_iterator* iter) {
121  scoped_ptr<FirstLastNameField> field(new FirstLastNameField);
122  std::vector<AutofillField*>::const_iterator q = *iter;
123
124  string16 pattern = GetEcmlPattern(kEcmlShipToFirstName,
125                                    kEcmlBillToFirstName, '|');
126  if (!ParseText(&q, pattern, &field->first_name_))
127    return NULL;
128
129  pattern = GetEcmlPattern(kEcmlShipToMiddleName, kEcmlBillToMiddleName, '|');
130  ParseText(&q, pattern, &field->middle_name_);
131
132  pattern = GetEcmlPattern(kEcmlShipToLastName, kEcmlBillToLastName, '|');
133  if (ParseText(&q, pattern, &field->last_name_)) {
134    *iter = q;
135    return field.release();
136  }
137
138  return NULL;
139}
140
141FirstLastNameField* FirstLastNameField::Parse(
142    std::vector<AutofillField*>::const_iterator* iter,
143    bool is_ecml) {
144  if (is_ecml) {
145    return ParseEcmlName(iter);
146  } else {
147    FirstLastNameField* v = Parse1(iter);
148    if (v != NULL)
149      return v;
150
151    return Parse2(iter);
152  }
153}
154
155bool FirstLastNameField::GetFieldInfo(FieldTypeMap* field_type_map) const {
156  bool ok = Add(field_type_map, first_name_, AutofillType(NAME_FIRST));
157  DCHECK(ok);
158  ok = ok && Add(field_type_map, last_name_, AutofillType(NAME_LAST));
159  DCHECK(ok);
160  AutofillType type = middle_initial_ ?
161      AutofillType(NAME_MIDDLE_INITIAL) : AutofillType(NAME_MIDDLE);
162  ok = ok && Add(field_type_map, middle_name_, type);
163  DCHECK(ok);
164
165  return ok;
166}
167
168FirstLastNameField::FirstLastNameField()
169    : first_name_(NULL),
170      middle_name_(NULL),
171      last_name_(NULL),
172      middle_initial_(false) {
173}
174
175