| OLD | NEW |
| 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/name_field.h" | 5 #include "components/autofill/core/browser/name_field.h" |
| 6 | 6 |
| 7 #include <memory> |
| 8 |
| 7 #include "base/macros.h" | 9 #include "base/macros.h" |
| 8 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/ptr_util.h" |
| 9 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 11 #include "components/autofill/core/browser/autofill_regex_constants.h" | 13 #include "components/autofill/core/browser/autofill_regex_constants.h" |
| 12 #include "components/autofill/core/browser/autofill_scanner.h" | 14 #include "components/autofill/core/browser/autofill_scanner.h" |
| 13 #include "components/autofill/core/browser/autofill_type.h" | 15 #include "components/autofill/core/browser/autofill_type.h" |
| 14 | 16 |
| 15 using base::UTF8ToUTF16; | 17 using base::UTF8ToUTF16; |
| 16 | 18 |
| 17 namespace autofill { | 19 namespace autofill { |
| 18 namespace { | 20 namespace { |
| 19 | 21 |
| 20 // A form field that can parse a full name field. | 22 // A form field that can parse a full name field. |
| 21 class FullNameField : public NameField { | 23 class FullNameField : public NameField { |
| 22 public: | 24 public: |
| 23 static scoped_ptr<FullNameField> Parse(AutofillScanner* scanner); | 25 static std::unique_ptr<FullNameField> Parse(AutofillScanner* scanner); |
| 24 | 26 |
| 25 protected: | 27 protected: |
| 26 void AddClassifications(FieldCandidatesMap* field_candidates) const override; | 28 void AddClassifications(FieldCandidatesMap* field_candidates) const override; |
| 27 | 29 |
| 28 private: | 30 private: |
| 29 explicit FullNameField(AutofillField* field); | 31 explicit FullNameField(AutofillField* field); |
| 30 | 32 |
| 31 AutofillField* field_; | 33 AutofillField* field_; |
| 32 | 34 |
| 33 DISALLOW_COPY_AND_ASSIGN(FullNameField); | 35 DISALLOW_COPY_AND_ASSIGN(FullNameField); |
| 34 }; | 36 }; |
| 35 | 37 |
| 36 // A form field that can parse a first and last name field. | 38 // A form field that can parse a first and last name field. |
| 37 class FirstLastNameField : public NameField { | 39 class FirstLastNameField : public NameField { |
| 38 public: | 40 public: |
| 39 static scoped_ptr<FirstLastNameField> ParseSpecificName( | 41 static std::unique_ptr<FirstLastNameField> ParseSpecificName( |
| 40 AutofillScanner* scanner); | 42 AutofillScanner* scanner); |
| 41 static scoped_ptr<FirstLastNameField> ParseComponentNames( | 43 static std::unique_ptr<FirstLastNameField> ParseComponentNames( |
| 42 AutofillScanner* scanner); | 44 AutofillScanner* scanner); |
| 43 static scoped_ptr<FirstLastNameField> Parse(AutofillScanner* scanner); | 45 static std::unique_ptr<FirstLastNameField> Parse(AutofillScanner* scanner); |
| 44 | 46 |
| 45 protected: | 47 protected: |
| 46 void AddClassifications(FieldCandidatesMap* field_candidates) const override; | 48 void AddClassifications(FieldCandidatesMap* field_candidates) const override; |
| 47 | 49 |
| 48 private: | 50 private: |
| 49 FirstLastNameField(); | 51 FirstLastNameField(); |
| 50 | 52 |
| 51 AutofillField* first_name_; | 53 AutofillField* first_name_; |
| 52 AutofillField* middle_name_; // Optional. | 54 AutofillField* middle_name_; // Optional. |
| 53 AutofillField* last_name_; | 55 AutofillField* last_name_; |
| 54 bool middle_initial_; // True if middle_name_ is a middle initial. | 56 bool middle_initial_; // True if middle_name_ is a middle initial. |
| 55 | 57 |
| 56 DISALLOW_COPY_AND_ASSIGN(FirstLastNameField); | 58 DISALLOW_COPY_AND_ASSIGN(FirstLastNameField); |
| 57 }; | 59 }; |
| 58 | 60 |
| 59 } // namespace | 61 } // namespace |
| 60 | 62 |
| 61 // static | 63 // static |
| 62 scoped_ptr<FormField> NameField::Parse(AutofillScanner* scanner) { | 64 std::unique_ptr<FormField> NameField::Parse(AutofillScanner* scanner) { |
| 63 if (scanner->IsEnd()) | 65 if (scanner->IsEnd()) |
| 64 return NULL; | 66 return NULL; |
| 65 | 67 |
| 66 // Try FirstLastNameField first since it's more specific. | 68 // Try FirstLastNameField first since it's more specific. |
| 67 scoped_ptr<FormField> field = FirstLastNameField::Parse(scanner); | 69 std::unique_ptr<FormField> field = FirstLastNameField::Parse(scanner); |
| 68 if (!field) | 70 if (!field) |
| 69 field = FullNameField::Parse(scanner); | 71 field = FullNameField::Parse(scanner); |
| 70 return field; | 72 return field; |
| 71 } | 73 } |
| 72 | 74 |
| 73 // This is overriden in concrete subclasses. | 75 // This is overriden in concrete subclasses. |
| 74 void NameField::AddClassifications(FieldCandidatesMap* field_candidates) const { | 76 void NameField::AddClassifications(FieldCandidatesMap* field_candidates) const { |
| 75 } | 77 } |
| 76 | 78 |
| 77 // static | 79 // static |
| 78 scoped_ptr<FullNameField> FullNameField::Parse(AutofillScanner* scanner) { | 80 std::unique_ptr<FullNameField> FullNameField::Parse(AutofillScanner* scanner) { |
| 79 // Exclude e.g. "username" or "nickname" fields. | 81 // Exclude e.g. "username" or "nickname" fields. |
| 80 scanner->SaveCursor(); | 82 scanner->SaveCursor(); |
| 81 bool should_ignore = ParseField(scanner, UTF8ToUTF16(kNameIgnoredRe), NULL); | 83 bool should_ignore = ParseField(scanner, UTF8ToUTF16(kNameIgnoredRe), NULL); |
| 82 scanner->Rewind(); | 84 scanner->Rewind(); |
| 83 if (should_ignore) | 85 if (should_ignore) |
| 84 return NULL; | 86 return NULL; |
| 85 | 87 |
| 86 // Searching for any label containing the word "name" is too general; | 88 // Searching for any label containing the word "name" is too general; |
| 87 // for example, Travelocity_Edit travel profile.html contains a field | 89 // for example, Travelocity_Edit travel profile.html contains a field |
| 88 // "Travel Profile Name". | 90 // "Travel Profile Name". |
| 89 AutofillField* field = NULL; | 91 AutofillField* field = NULL; |
| 90 if (ParseField(scanner, UTF8ToUTF16(kNameRe), &field)) | 92 if (ParseField(scanner, UTF8ToUTF16(kNameRe), &field)) |
| 91 return make_scoped_ptr(new FullNameField(field)); | 93 return base::WrapUnique(new FullNameField(field)); |
| 92 | 94 |
| 93 return NULL; | 95 return NULL; |
| 94 } | 96 } |
| 95 | 97 |
| 96 void FullNameField::AddClassifications( | 98 void FullNameField::AddClassifications( |
| 97 FieldCandidatesMap* field_candidates) const { | 99 FieldCandidatesMap* field_candidates) const { |
| 98 AddClassification(field_, NAME_FULL, kBaseNameParserScore, field_candidates); | 100 AddClassification(field_, NAME_FULL, kBaseNameParserScore, field_candidates); |
| 99 } | 101 } |
| 100 | 102 |
| 101 FullNameField::FullNameField(AutofillField* field) : field_(field) { | 103 FullNameField::FullNameField(AutofillField* field) : field_(field) { |
| 102 } | 104 } |
| 103 | 105 |
| 104 scoped_ptr<FirstLastNameField> FirstLastNameField::ParseSpecificName( | 106 std::unique_ptr<FirstLastNameField> FirstLastNameField::ParseSpecificName( |
| 105 AutofillScanner* scanner) { | 107 AutofillScanner* scanner) { |
| 106 // Some pages (e.g. Overstock_comBilling.html, SmithsonianCheckout.html) | 108 // Some pages (e.g. Overstock_comBilling.html, SmithsonianCheckout.html) |
| 107 // have the label "Name" followed by two or three text fields. | 109 // have the label "Name" followed by two or three text fields. |
| 108 scoped_ptr<FirstLastNameField> v(new FirstLastNameField); | 110 std::unique_ptr<FirstLastNameField> v(new FirstLastNameField); |
| 109 scanner->SaveCursor(); | 111 scanner->SaveCursor(); |
| 110 | 112 |
| 111 AutofillField* next = NULL; | 113 AutofillField* next = NULL; |
| 112 if (ParseField(scanner, UTF8ToUTF16(kNameSpecificRe), &v->first_name_) && | 114 if (ParseField(scanner, UTF8ToUTF16(kNameSpecificRe), &v->first_name_) && |
| 113 ParseEmptyLabel(scanner, &next)) { | 115 ParseEmptyLabel(scanner, &next)) { |
| 114 if (ParseEmptyLabel(scanner, &v->last_name_)) { | 116 if (ParseEmptyLabel(scanner, &v->last_name_)) { |
| 115 // There are three name fields; assume that the middle one is a | 117 // There are three name fields; assume that the middle one is a |
| 116 // middle initial (it is, at least, on SmithsonianCheckout.html). | 118 // middle initial (it is, at least, on SmithsonianCheckout.html). |
| 117 v->middle_name_ = next; | 119 v->middle_name_ = next; |
| 118 v->middle_initial_ = true; | 120 v->middle_initial_ = true; |
| 119 } else { // only two name fields | 121 } else { // only two name fields |
| 120 v->last_name_ = next; | 122 v->last_name_ = next; |
| 121 } | 123 } |
| 122 | 124 |
| 123 return v; | 125 return v; |
| 124 } | 126 } |
| 125 | 127 |
| 126 scanner->Rewind(); | 128 scanner->Rewind(); |
| 127 return NULL; | 129 return NULL; |
| 128 } | 130 } |
| 129 | 131 |
| 130 // static | 132 // static |
| 131 scoped_ptr<FirstLastNameField> FirstLastNameField::ParseComponentNames( | 133 std::unique_ptr<FirstLastNameField> FirstLastNameField::ParseComponentNames( |
| 132 AutofillScanner* scanner) { | 134 AutofillScanner* scanner) { |
| 133 scoped_ptr<FirstLastNameField> v(new FirstLastNameField); | 135 std::unique_ptr<FirstLastNameField> v(new FirstLastNameField); |
| 134 scanner->SaveCursor(); | 136 scanner->SaveCursor(); |
| 135 | 137 |
| 136 // A fair number of pages use the names "fname" and "lname" for naming | 138 // A fair number of pages use the names "fname" and "lname" for naming |
| 137 // first and last name fields (examples from the test suite: | 139 // first and last name fields (examples from the test suite: |
| 138 // BESTBUY_COM - Sign In2.html; Crate and Barrel Check Out.html; | 140 // BESTBUY_COM - Sign In2.html; Crate and Barrel Check Out.html; |
| 139 // dell_checkout1.html). At least one UK page (The China Shop2.html) | 141 // dell_checkout1.html). At least one UK page (The China Shop2.html) |
| 140 // asks, in stuffy English style, for just initials and a surname, | 142 // asks, in stuffy English style, for just initials and a surname, |
| 141 // so we match "initials" here (and just fill in a first name there, | 143 // so we match "initials" here (and just fill in a first name there, |
| 142 // American-style). | 144 // American-style). |
| 143 // The ".*first$" matches fields ending in "first" (example in sample8.html). | 145 // The ".*first$" matches fields ending in "first" (example in sample8.html). |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 183 // Consider the match to be successful if we detected both first and last name | 185 // Consider the match to be successful if we detected both first and last name |
| 184 // fields. | 186 // fields. |
| 185 if (v->first_name_ && v->last_name_) | 187 if (v->first_name_ && v->last_name_) |
| 186 return v; | 188 return v; |
| 187 | 189 |
| 188 scanner->Rewind(); | 190 scanner->Rewind(); |
| 189 return NULL; | 191 return NULL; |
| 190 } | 192 } |
| 191 | 193 |
| 192 // static | 194 // static |
| 193 scoped_ptr<FirstLastNameField> FirstLastNameField::Parse( | 195 std::unique_ptr<FirstLastNameField> FirstLastNameField::Parse( |
| 194 AutofillScanner* scanner) { | 196 AutofillScanner* scanner) { |
| 195 scoped_ptr<FirstLastNameField> field = ParseSpecificName(scanner); | 197 std::unique_ptr<FirstLastNameField> field = ParseSpecificName(scanner); |
| 196 if (!field) | 198 if (!field) |
| 197 field = ParseComponentNames(scanner); | 199 field = ParseComponentNames(scanner); |
| 198 return field; | 200 return field; |
| 199 } | 201 } |
| 200 | 202 |
| 201 FirstLastNameField::FirstLastNameField() | 203 FirstLastNameField::FirstLastNameField() |
| 202 : first_name_(NULL), | 204 : first_name_(NULL), |
| 203 middle_name_(NULL), | 205 middle_name_(NULL), |
| 204 last_name_(NULL), | 206 last_name_(NULL), |
| 205 middle_initial_(false) { | 207 middle_initial_(false) { |
| 206 } | 208 } |
| 207 | 209 |
| 208 void FirstLastNameField::AddClassifications( | 210 void FirstLastNameField::AddClassifications( |
| 209 FieldCandidatesMap* field_candidates) const { | 211 FieldCandidatesMap* field_candidates) const { |
| 210 AddClassification(first_name_, NAME_FIRST, kBaseNameParserScore, | 212 AddClassification(first_name_, NAME_FIRST, kBaseNameParserScore, |
| 211 field_candidates); | 213 field_candidates); |
| 212 AddClassification(last_name_, NAME_LAST, kBaseNameParserScore, | 214 AddClassification(last_name_, NAME_LAST, kBaseNameParserScore, |
| 213 field_candidates); | 215 field_candidates); |
| 214 const ServerFieldType type = | 216 const ServerFieldType type = |
| 215 middle_initial_ ? NAME_MIDDLE_INITIAL : NAME_MIDDLE; | 217 middle_initial_ ? NAME_MIDDLE_INITIAL : NAME_MIDDLE; |
| 216 AddClassification(middle_name_, type, kBaseNameParserScore, field_candidates); | 218 AddClassification(middle_name_, type, kBaseNameParserScore, field_candidates); |
| 217 } | 219 } |
| 218 | 220 |
| 219 } // namespace autofill | 221 } // namespace autofill |
| OLD | NEW |