Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(151)

Side by Side Diff: components/autofill/core/browser/name_field.cc

Issue 853523004: Autofill: Set requirements for number of recognized fields in an autofillable form (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: More relaxed check for form tag Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "base/memory/scoped_ptr.h" 7 #include "base/memory/scoped_ptr.h"
8 #include "base/strings/string_util.h" 8 #include "base/strings/string_util.h"
9 #include "base/strings/utf_string_conversions.h" 9 #include "base/strings/utf_string_conversions.h"
10 #include "components/autofill/core/browser/autofill_regex_constants.h" 10 #include "components/autofill/core/browser/autofill_regex_constants.h"
11 #include "components/autofill/core/browser/autofill_scanner.h" 11 #include "components/autofill/core/browser/autofill_scanner.h"
12 #include "components/autofill/core/browser/autofill_type.h" 12 #include "components/autofill/core/browser/autofill_type.h"
13 13
14 using base::UTF8ToUTF16; 14 using base::UTF8ToUTF16;
15 15
16 namespace autofill { 16 namespace autofill {
17 namespace { 17 namespace {
18 18
19 // A form field that can parse a full name field. 19 // A form field that can parse a full name field.
20 class FullNameField : public NameField { 20 class FullNameField : public NameField {
21 public: 21 public:
22 static scoped_ptr<FullNameField> Parse(AutofillScanner* scanner); 22 static scoped_ptr<FullNameField> Parse(AutofillScanner* scanner);
23 23
24 protected: 24 protected:
25 // FormField: 25 // FormField:
26 bool ClassifyField(ServerFieldTypeMap* map) const override; 26 bool ClassifyField(ServerFieldTypeMap* map) const override;
27 size_t FieldCount() const override;
27 28
28 private: 29 private:
29 explicit FullNameField(AutofillField* field); 30 explicit FullNameField(AutofillField* field);
30 31
31 AutofillField* field_; 32 AutofillField* field_;
32 33
33 DISALLOW_COPY_AND_ASSIGN(FullNameField); 34 DISALLOW_COPY_AND_ASSIGN(FullNameField);
34 }; 35 };
35 36
36 // A form field that can parse a first and last name field. 37 // A form field that can parse a first and last name field.
37 class FirstLastNameField : public NameField { 38 class FirstLastNameField : public NameField {
38 public: 39 public:
39 static scoped_ptr<FirstLastNameField> ParseSpecificName( 40 static scoped_ptr<FirstLastNameField> ParseSpecificName(
40 AutofillScanner* scanner); 41 AutofillScanner* scanner);
41 static scoped_ptr<FirstLastNameField> ParseComponentNames( 42 static scoped_ptr<FirstLastNameField> ParseComponentNames(
42 AutofillScanner* scanner); 43 AutofillScanner* scanner);
43 static scoped_ptr<FirstLastNameField> Parse(AutofillScanner* scanner); 44 static scoped_ptr<FirstLastNameField> Parse(AutofillScanner* scanner);
44 45
45 protected: 46 protected:
46 // FormField: 47 // FormField:
47 bool ClassifyField(ServerFieldTypeMap* map) const override; 48 bool ClassifyField(ServerFieldTypeMap* map) const override;
49 size_t FieldCount() const override;
48 50
49 private: 51 private:
50 FirstLastNameField(); 52 FirstLastNameField();
51 53
52 AutofillField* first_name_; 54 AutofillField* first_name_;
53 AutofillField* middle_name_; // Optional. 55 AutofillField* middle_name_; // Optional.
54 AutofillField* last_name_; 56 AutofillField* last_name_;
55 bool middle_initial_; // True if middle_name_ is a middle initial. 57 bool middle_initial_; // True if middle_name_ is a middle initial.
56 58
57 DISALLOW_COPY_AND_ASSIGN(FirstLastNameField); 59 DISALLOW_COPY_AND_ASSIGN(FirstLastNameField);
58 }; 60 };
59 61
60 } // namespace 62 } // namespace
61 63
62 // static 64 // static
63 scoped_ptr<FormField> NameField::Parse(AutofillScanner* scanner) { 65 scoped_ptr<FormField> NameField::Parse(AutofillScanner* scanner) {
64 if (scanner->IsEnd()) 66 if (scanner->IsEnd())
65 return NULL; 67 return NULL;
66 68
67 // Try FirstLastNameField first since it's more specific. 69 // Try FirstLastNameField first since it's more specific.
68 scoped_ptr<FormField> field = FirstLastNameField::Parse(scanner); 70 scoped_ptr<FormField> field = FirstLastNameField::Parse(scanner);
69 if (!field) 71 if (!field)
70 field = FullNameField::Parse(scanner); 72 field = FullNameField::Parse(scanner);
71 return field; 73 return field;
72 } 74 }
73 75
74 // This is overriden in concrete subclasses. 76 // This is overriden in concrete subclasses.
75 bool NameField::ClassifyField(ServerFieldTypeMap* map) const { 77 bool NameField::ClassifyField(ServerFieldTypeMap* map) const {
78 NOTREACHED();
76 return false; 79 return false;
77 } 80 }
78 81
82 // This is overriden in concrete subclasses.
Evan Stade 2015/01/21 22:52:50 nit: spelling
Lei Zhang 2015/01/22 08:07:37 Done.
83 size_t NameField::FieldCount() const {
84 NOTREACHED();
85 return 0;
86 }
87
79 // static 88 // static
80 scoped_ptr<FullNameField> FullNameField::Parse(AutofillScanner* scanner) { 89 scoped_ptr<FullNameField> FullNameField::Parse(AutofillScanner* scanner) {
81 // Exclude e.g. "username" or "nickname" fields. 90 // Exclude e.g. "username" or "nickname" fields.
82 scanner->SaveCursor(); 91 scanner->SaveCursor();
83 bool should_ignore = ParseField(scanner, UTF8ToUTF16(kNameIgnoredRe), NULL); 92 bool should_ignore = ParseField(scanner, UTF8ToUTF16(kNameIgnoredRe), NULL);
84 scanner->Rewind(); 93 scanner->Rewind();
85 if (should_ignore) 94 if (should_ignore)
86 return NULL; 95 return NULL;
87 96
88 // Searching for any label containing the word "name" is too general; 97 // Searching for any label containing the word "name" is too general;
89 // for example, Travelocity_Edit travel profile.html contains a field 98 // for example, Travelocity_Edit travel profile.html contains a field
90 // "Travel Profile Name". 99 // "Travel Profile Name".
91 AutofillField* field = NULL; 100 AutofillField* field = NULL;
92 if (ParseField(scanner, UTF8ToUTF16(kNameRe), &field)) 101 if (ParseField(scanner, UTF8ToUTF16(kNameRe), &field))
93 return make_scoped_ptr(new FullNameField(field)); 102 return make_scoped_ptr(new FullNameField(field));
94 103
95 return NULL; 104 return NULL;
96 } 105 }
97 106
98 bool FullNameField::ClassifyField(ServerFieldTypeMap* map) const { 107 bool FullNameField::ClassifyField(ServerFieldTypeMap* map) const {
99 return AddClassification(field_, NAME_FULL, map); 108 return AddClassification(field_, NAME_FULL, map);
100 } 109 }
101 110
111 size_t FullNameField::FieldCount() const {
112 return 1;
113 }
114
102 FullNameField::FullNameField(AutofillField* field) : field_(field) { 115 FullNameField::FullNameField(AutofillField* field) : field_(field) {
116 DCHECK(field);
103 } 117 }
104 118
105 scoped_ptr<FirstLastNameField> FirstLastNameField::ParseSpecificName( 119 scoped_ptr<FirstLastNameField> FirstLastNameField::ParseSpecificName(
106 AutofillScanner* scanner) { 120 AutofillScanner* scanner) {
107 // Some pages (e.g. Overstock_comBilling.html, SmithsonianCheckout.html) 121 // Some pages (e.g. Overstock_comBilling.html, SmithsonianCheckout.html)
108 // have the label "Name" followed by two or three text fields. 122 // have the label "Name" followed by two or three text fields.
109 scoped_ptr<FirstLastNameField> v(new FirstLastNameField); 123 scoped_ptr<FirstLastNameField> v(new FirstLastNameField);
110 scanner->SaveCursor(); 124 scanner->SaveCursor();
111 125
112 AutofillField* next = NULL; 126 AutofillField* next = NULL;
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
207 } 221 }
208 222
209 bool FirstLastNameField::ClassifyField(ServerFieldTypeMap* map) const { 223 bool FirstLastNameField::ClassifyField(ServerFieldTypeMap* map) const {
210 bool ok = AddClassification(first_name_, NAME_FIRST, map); 224 bool ok = AddClassification(first_name_, NAME_FIRST, map);
211 ok = ok && AddClassification(last_name_, NAME_LAST, map); 225 ok = ok && AddClassification(last_name_, NAME_LAST, map);
212 ServerFieldType type = middle_initial_ ? NAME_MIDDLE_INITIAL : NAME_MIDDLE; 226 ServerFieldType type = middle_initial_ ? NAME_MIDDLE_INITIAL : NAME_MIDDLE;
213 ok = ok && AddClassification(middle_name_, type, map); 227 ok = ok && AddClassification(middle_name_, type, map);
214 return ok; 228 return ok;
215 } 229 }
216 230
231 size_t FirstLastNameField::FieldCount() const {
232 size_t count = 0;
233
234 if (first_name_)
235 ++count;
236 if (middle_name_)
237 ++count;
238 if (last_name_)
239 ++count;
240
241 return count;
242 }
243
217 } // namespace autofill 244 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698