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 "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
11 #include "components/autofill/core/browser/autofill_regex_constants.h" | 11 #include "components/autofill/core/browser/autofill_regex_constants.h" |
12 #include "components/autofill/core/browser/autofill_scanner.h" | 12 #include "components/autofill/core/browser/autofill_scanner.h" |
13 #include "components/autofill/core/browser/autofill_type.h" | 13 #include "components/autofill/core/browser/autofill_type.h" |
14 #include "ui/base/l10n/l10n_util.h" | 14 #include "ui/base/l10n/l10n_util.h" |
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 FullNameField* Parse(AutofillScanner* scanner); | 22 static FullNameField* Parse(AutofillScanner* scanner); |
23 | 23 |
24 protected: | 24 protected: |
25 // FormField: | 25 // FormField: |
26 virtual bool ClassifyField(FieldTypeMap* map) const OVERRIDE; | 26 virtual bool ClassifyField(ServerFieldTypeMap* map) const OVERRIDE; |
27 | 27 |
28 private: | 28 private: |
29 explicit FullNameField(const AutofillField* field); | 29 explicit FullNameField(const AutofillField* field); |
30 | 30 |
31 const AutofillField* field_; | 31 const AutofillField* field_; |
32 | 32 |
33 DISALLOW_COPY_AND_ASSIGN(FullNameField); | 33 DISALLOW_COPY_AND_ASSIGN(FullNameField); |
34 }; | 34 }; |
35 | 35 |
36 // A form field that can parse a first and last name field. | 36 // A form field that can parse a first and last name field. |
37 class FirstLastNameField : public NameField { | 37 class FirstLastNameField : public NameField { |
38 public: | 38 public: |
39 static FirstLastNameField* ParseSpecificName(AutofillScanner* scanner); | 39 static FirstLastNameField* ParseSpecificName(AutofillScanner* scanner); |
40 static FirstLastNameField* ParseComponentNames(AutofillScanner* scanner); | 40 static FirstLastNameField* ParseComponentNames(AutofillScanner* scanner); |
41 static FirstLastNameField* Parse(AutofillScanner* scanner); | 41 static FirstLastNameField* Parse(AutofillScanner* scanner); |
42 | 42 |
43 protected: | 43 protected: |
44 // FormField: | 44 // FormField: |
45 virtual bool ClassifyField(FieldTypeMap* map) const OVERRIDE; | 45 virtual bool ClassifyField(ServerFieldTypeMap* map) const OVERRIDE; |
46 | 46 |
47 private: | 47 private: |
48 FirstLastNameField(); | 48 FirstLastNameField(); |
49 | 49 |
50 const AutofillField* first_name_; | 50 const AutofillField* first_name_; |
51 const AutofillField* middle_name_; // Optional. | 51 const AutofillField* middle_name_; // Optional. |
52 const AutofillField* last_name_; | 52 const AutofillField* last_name_; |
53 bool middle_initial_; // True if middle_name_ is a middle initial. | 53 bool middle_initial_; // True if middle_name_ is a middle initial. |
54 | 54 |
55 DISALLOW_COPY_AND_ASSIGN(FirstLastNameField); | 55 DISALLOW_COPY_AND_ASSIGN(FirstLastNameField); |
56 }; | 56 }; |
57 | 57 |
58 } // namespace | 58 } // namespace |
59 | 59 |
60 FormField* NameField::Parse(AutofillScanner* scanner) { | 60 FormField* NameField::Parse(AutofillScanner* scanner) { |
61 if (scanner->IsEnd()) | 61 if (scanner->IsEnd()) |
62 return NULL; | 62 return NULL; |
63 | 63 |
64 // Try FirstLastNameField first since it's more specific. | 64 // Try FirstLastNameField first since it's more specific. |
65 NameField* field = FirstLastNameField::Parse(scanner); | 65 NameField* field = FirstLastNameField::Parse(scanner); |
66 if (!field) | 66 if (!field) |
67 field = FullNameField::Parse(scanner); | 67 field = FullNameField::Parse(scanner); |
68 return field; | 68 return field; |
69 } | 69 } |
70 | 70 |
71 // This is overriden in concrete subclasses. | 71 // This is overriden in concrete subclasses. |
72 bool NameField::ClassifyField(FieldTypeMap* map) const { | 72 bool NameField::ClassifyField(ServerFieldTypeMap* map) const { |
73 return false; | 73 return false; |
74 } | 74 } |
75 | 75 |
76 FullNameField* FullNameField::Parse(AutofillScanner* scanner) { | 76 FullNameField* FullNameField::Parse(AutofillScanner* scanner) { |
77 // Exclude e.g. "username" or "nickname" fields. | 77 // Exclude e.g. "username" or "nickname" fields. |
78 scanner->SaveCursor(); | 78 scanner->SaveCursor(); |
79 bool should_ignore = ParseField(scanner, | 79 bool should_ignore = ParseField(scanner, |
80 UTF8ToUTF16(autofill::kNameIgnoredRe), NULL); | 80 UTF8ToUTF16(autofill::kNameIgnoredRe), NULL); |
81 scanner->Rewind(); | 81 scanner->Rewind(); |
82 if (should_ignore) | 82 if (should_ignore) |
83 return NULL; | 83 return NULL; |
84 | 84 |
85 // Searching for any label containing the word "name" is too general; | 85 // Searching for any label containing the word "name" is too general; |
86 // for example, Travelocity_Edit travel profile.html contains a field | 86 // for example, Travelocity_Edit travel profile.html contains a field |
87 // "Travel Profile Name". | 87 // "Travel Profile Name". |
88 const AutofillField* field = NULL; | 88 const AutofillField* field = NULL; |
89 if (ParseField(scanner, UTF8ToUTF16(autofill::kNameRe), &field)) | 89 if (ParseField(scanner, UTF8ToUTF16(autofill::kNameRe), &field)) |
90 return new FullNameField(field); | 90 return new FullNameField(field); |
91 | 91 |
92 return NULL; | 92 return NULL; |
93 } | 93 } |
94 | 94 |
95 bool FullNameField::ClassifyField(FieldTypeMap* map) const { | 95 bool FullNameField::ClassifyField(ServerFieldTypeMap* map) const { |
96 return AddClassification(field_, NAME_FULL, map); | 96 return AddClassification(field_, NAME_FULL, map); |
97 } | 97 } |
98 | 98 |
99 FullNameField::FullNameField(const AutofillField* field) | 99 FullNameField::FullNameField(const AutofillField* field) |
100 : field_(field) { | 100 : field_(field) { |
101 } | 101 } |
102 | 102 |
103 FirstLastNameField* FirstLastNameField::ParseSpecificName( | 103 FirstLastNameField* FirstLastNameField::ParseSpecificName( |
104 AutofillScanner* scanner) { | 104 AutofillScanner* scanner) { |
105 // Some pages (e.g. Overstock_comBilling.html, SmithsonianCheckout.html) | 105 // Some pages (e.g. Overstock_comBilling.html, SmithsonianCheckout.html) |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
199 return field; | 199 return field; |
200 } | 200 } |
201 | 201 |
202 FirstLastNameField::FirstLastNameField() | 202 FirstLastNameField::FirstLastNameField() |
203 : first_name_(NULL), | 203 : first_name_(NULL), |
204 middle_name_(NULL), | 204 middle_name_(NULL), |
205 last_name_(NULL), | 205 last_name_(NULL), |
206 middle_initial_(false) { | 206 middle_initial_(false) { |
207 } | 207 } |
208 | 208 |
209 bool FirstLastNameField::ClassifyField(FieldTypeMap* map) const { | 209 bool FirstLastNameField::ClassifyField(ServerFieldTypeMap* map) const { |
210 bool ok = AddClassification(first_name_, NAME_FIRST, map); | 210 bool ok = AddClassification(first_name_, NAME_FIRST, map); |
211 ok = ok && AddClassification(last_name_, NAME_LAST, map); | 211 ok = ok && AddClassification(last_name_, NAME_LAST, map); |
212 AutofillFieldType type = middle_initial_ ? NAME_MIDDLE_INITIAL : NAME_MIDDLE; | 212 ServerFieldType type = middle_initial_ ? NAME_MIDDLE_INITIAL : NAME_MIDDLE; |
213 ok = ok && AddClassification(middle_name_, type, map); | 213 ok = ok && AddClassification(middle_name_, type, map); |
214 return ok; | 214 return ok; |
215 } | 215 } |
216 | 216 |
217 } // namespace autofill | 217 } // namespace autofill |
OLD | NEW |