| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/autofill/name_field.h" | 5 #include "chrome/browser/autofill/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/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| 11 #include "chrome/browser/autofill/autofill_ecml.h" | |
| 12 #include "chrome/browser/autofill/autofill_scanner.h" | 11 #include "chrome/browser/autofill/autofill_scanner.h" |
| 13 #include "chrome/browser/autofill/autofill_type.h" | 12 #include "chrome/browser/autofill/autofill_type.h" |
| 14 #include "grit/autofill_resources.h" | 13 #include "grit/autofill_resources.h" |
| 15 #include "ui/base/l10n/l10n_util.h" | 14 #include "ui/base/l10n/l10n_util.h" |
| 16 | 15 |
| 17 using autofill::GetEcmlPattern; | |
| 18 | |
| 19 namespace { | 16 namespace { |
| 20 | 17 |
| 21 // A form field that can parse a full name field. | 18 // A form field that can parse a full name field. |
| 22 class FullNameField : public NameField { | 19 class FullNameField : public NameField { |
| 23 public: | 20 public: |
| 24 static FullNameField* Parse(AutofillScanner* scanner); | 21 static FullNameField* Parse(AutofillScanner* scanner); |
| 25 | 22 |
| 26 protected: | 23 protected: |
| 27 // FormField: | 24 // FormField: |
| 28 virtual bool ClassifyField(FieldTypeMap* map) const OVERRIDE; | 25 virtual bool ClassifyField(FieldTypeMap* map) const OVERRIDE; |
| 29 | 26 |
| 30 private: | 27 private: |
| 31 explicit FullNameField(const AutofillField* field); | 28 explicit FullNameField(const AutofillField* field); |
| 32 | 29 |
| 33 const AutofillField* field_; | 30 const AutofillField* field_; |
| 34 | 31 |
| 35 DISALLOW_COPY_AND_ASSIGN(FullNameField); | 32 DISALLOW_COPY_AND_ASSIGN(FullNameField); |
| 36 }; | 33 }; |
| 37 | 34 |
| 38 // A form field that can parse a first and last name field. | 35 // A form field that can parse a first and last name field. |
| 39 class FirstLastNameField : public NameField { | 36 class FirstLastNameField : public NameField { |
| 40 public: | 37 public: |
| 41 static FirstLastNameField* ParseSpecificName(AutofillScanner* scanner); | 38 static FirstLastNameField* ParseSpecificName(AutofillScanner* scanner); |
| 42 static FirstLastNameField* ParseComponentNames(AutofillScanner* scanner); | 39 static FirstLastNameField* ParseComponentNames(AutofillScanner* scanner); |
| 43 static FirstLastNameField* ParseEcmlName(AutofillScanner* scanner); | 40 static FirstLastNameField* Parse(AutofillScanner* scanner); |
| 44 static FirstLastNameField* Parse(AutofillScanner* scanner, bool is_ecml); | |
| 45 | 41 |
| 46 protected: | 42 protected: |
| 47 // FormField: | 43 // FormField: |
| 48 virtual bool ClassifyField(FieldTypeMap* map) const OVERRIDE; | 44 virtual bool ClassifyField(FieldTypeMap* map) const OVERRIDE; |
| 49 | 45 |
| 50 private: | 46 private: |
| 51 FirstLastNameField(); | 47 FirstLastNameField(); |
| 52 | 48 |
| 53 const AutofillField* first_name_; | 49 const AutofillField* first_name_; |
| 54 const AutofillField* middle_name_; // Optional. | 50 const AutofillField* middle_name_; // Optional. |
| 55 const AutofillField* last_name_; | 51 const AutofillField* last_name_; |
| 56 bool middle_initial_; // True if middle_name_ is a middle initial. | 52 bool middle_initial_; // True if middle_name_ is a middle initial. |
| 57 | 53 |
| 58 DISALLOW_COPY_AND_ASSIGN(FirstLastNameField); | 54 DISALLOW_COPY_AND_ASSIGN(FirstLastNameField); |
| 59 }; | 55 }; |
| 60 | 56 |
| 61 } // namespace | 57 } // namespace |
| 62 | 58 |
| 63 FormField* NameField::Parse(AutofillScanner* scanner, bool is_ecml) { | 59 FormField* NameField::Parse(AutofillScanner* scanner) { |
| 64 if (scanner->IsEnd()) | 60 if (scanner->IsEnd()) |
| 65 return NULL; | 61 return NULL; |
| 66 | 62 |
| 67 // Try FirstLastNameField first since it's more specific. | 63 // Try FirstLastNameField first since it's more specific. |
| 68 NameField* field = FirstLastNameField::Parse(scanner, is_ecml); | 64 NameField* field = FirstLastNameField::Parse(scanner); |
| 69 if (!field && !is_ecml) | 65 if (!field) |
| 70 field = FullNameField::Parse(scanner); | 66 field = FullNameField::Parse(scanner); |
| 71 return field; | 67 return field; |
| 72 } | 68 } |
| 73 | 69 |
| 74 // This is overriden in concrete subclasses. | 70 // This is overriden in concrete subclasses. |
| 75 bool NameField::ClassifyField(FieldTypeMap* map) const { | 71 bool NameField::ClassifyField(FieldTypeMap* map) const { |
| 76 return false; | 72 return false; |
| 77 } | 73 } |
| 78 | 74 |
| 79 FullNameField* FullNameField::Parse(AutofillScanner* scanner) { | 75 FullNameField* FullNameField::Parse(AutofillScanner* scanner) { |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 195 | 191 |
| 196 // Consider the match to be successful if we detected both first and last name | 192 // Consider the match to be successful if we detected both first and last name |
| 197 // fields. | 193 // fields. |
| 198 if (v->first_name_ && v->last_name_) | 194 if (v->first_name_ && v->last_name_) |
| 199 return v.release(); | 195 return v.release(); |
| 200 | 196 |
| 201 scanner->Rewind(); | 197 scanner->Rewind(); |
| 202 return NULL; | 198 return NULL; |
| 203 } | 199 } |
| 204 | 200 |
| 205 FirstLastNameField* FirstLastNameField::ParseEcmlName( | 201 FirstLastNameField* FirstLastNameField::Parse(AutofillScanner* scanner) { |
| 206 AutofillScanner* scanner) { | |
| 207 scoped_ptr<FirstLastNameField> field(new FirstLastNameField); | |
| 208 scanner->SaveCursor(); | |
| 209 | |
| 210 string16 pattern = GetEcmlPattern(kEcmlShipToFirstName, | |
| 211 kEcmlBillToFirstName, '|'); | |
| 212 if (!ParseField(scanner, pattern, &field->first_name_)) | |
| 213 return NULL; | |
| 214 | |
| 215 pattern = GetEcmlPattern(kEcmlShipToMiddleName, kEcmlBillToMiddleName, '|'); | |
| 216 ParseField(scanner, pattern, &field->middle_name_); | |
| 217 | |
| 218 pattern = GetEcmlPattern(kEcmlShipToLastName, kEcmlBillToLastName, '|'); | |
| 219 if (ParseField(scanner, pattern, &field->last_name_)) | |
| 220 return field.release(); | |
| 221 | |
| 222 scanner->Rewind(); | |
| 223 return NULL; | |
| 224 } | |
| 225 | |
| 226 FirstLastNameField* FirstLastNameField::Parse(AutofillScanner* scanner, | |
| 227 bool is_ecml) { | |
| 228 if (is_ecml) | |
| 229 return ParseEcmlName(scanner); | |
| 230 | |
| 231 FirstLastNameField* field = ParseSpecificName(scanner); | 202 FirstLastNameField* field = ParseSpecificName(scanner); |
| 232 if (!field) | 203 if (!field) |
| 233 field = ParseComponentNames(scanner); | 204 field = ParseComponentNames(scanner); |
| 234 return field; | 205 return field; |
| 235 } | 206 } |
| 236 | 207 |
| 237 FirstLastNameField::FirstLastNameField() | 208 FirstLastNameField::FirstLastNameField() |
| 238 : first_name_(NULL), | 209 : first_name_(NULL), |
| 239 middle_name_(NULL), | 210 middle_name_(NULL), |
| 240 last_name_(NULL), | 211 last_name_(NULL), |
| 241 middle_initial_(false) { | 212 middle_initial_(false) { |
| 242 } | 213 } |
| 243 | 214 |
| 244 bool FirstLastNameField::ClassifyField(FieldTypeMap* map) const { | 215 bool FirstLastNameField::ClassifyField(FieldTypeMap* map) const { |
| 245 bool ok = AddClassification(first_name_, NAME_FIRST, map); | 216 bool ok = AddClassification(first_name_, NAME_FIRST, map); |
| 246 ok = ok && AddClassification(last_name_, NAME_LAST, map); | 217 ok = ok && AddClassification(last_name_, NAME_LAST, map); |
| 247 AutofillFieldType type = middle_initial_ ? NAME_MIDDLE_INITIAL : NAME_MIDDLE; | 218 AutofillFieldType type = middle_initial_ ? NAME_MIDDLE_INITIAL : NAME_MIDDLE; |
| 248 ok = ok && AddClassification(middle_name_, type, map); | 219 ok = ok && AddClassification(middle_name_, type, map); |
| 249 return ok; | 220 return ok; |
| 250 } | 221 } |
| OLD | NEW |