| 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/phone_field.h" | 5 #include "chrome/browser/autofill/phone_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/string16.h" | 9 #include "base/string16.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
| 12 #include "chrome/browser/autofill/autofill_ecml.h" | |
| 13 #include "chrome/browser/autofill/autofill_field.h" | 12 #include "chrome/browser/autofill/autofill_field.h" |
| 14 #include "chrome/browser/autofill/autofill_scanner.h" | 13 #include "chrome/browser/autofill/autofill_scanner.h" |
| 15 #include "chrome/browser/autofill/fax_number.h" | 14 #include "chrome/browser/autofill/fax_number.h" |
| 16 #include "chrome/browser/autofill/home_phone_number.h" | 15 #include "chrome/browser/autofill/home_phone_number.h" |
| 17 #include "grit/autofill_resources.h" | 16 #include "grit/autofill_resources.h" |
| 18 #include "ui/base/l10n/l10n_util.h" | 17 #include "ui/base/l10n/l10n_util.h" |
| 19 | 18 |
| 20 using autofill::GetEcmlPattern; | |
| 21 | |
| 22 // Phone field grammars - first matched grammar will be parsed. Grammars are | 19 // Phone field grammars - first matched grammar will be parsed. Grammars are |
| 23 // separated by { REGEX_SEPARATOR, FIELD_NONE, 0 }. Suffix and extension are | 20 // separated by { REGEX_SEPARATOR, FIELD_NONE, 0 }. Suffix and extension are |
| 24 // parsed separately unless they are necessary parts of the match. | 21 // parsed separately unless they are necessary parts of the match. |
| 25 // The following notation is used to describe the patterns: | 22 // The following notation is used to describe the patterns: |
| 26 // <cc> - country code field. | 23 // <cc> - country code field. |
| 27 // <ac> - area code field. | 24 // <ac> - area code field. |
| 28 // <phone> - phone or prefix. | 25 // <phone> - phone or prefix. |
| 29 // <suffix> - suffix. | 26 // <suffix> - suffix. |
| 30 // <ext> - extension. | 27 // <ext> - extension. |
| 31 // :N means field is limited to N characters, otherwise it is unlimited. | 28 // :N means field is limited to N characters, otherwise it is unlimited. |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 { PhoneField::REGEX_PHONE, PhoneField::FIELD_PHONE, 10 }, | 100 { PhoneField::REGEX_PHONE, PhoneField::FIELD_PHONE, 10 }, |
| 104 { PhoneField::REGEX_SEPARATOR, FIELD_NONE, 0 }, | 101 { PhoneField::REGEX_SEPARATOR, FIELD_NONE, 0 }, |
| 105 // Phone: <phone> (Ext: <ext>)? | 102 // Phone: <phone> (Ext: <ext>)? |
| 106 { PhoneField::REGEX_PHONE, PhoneField::FIELD_PHONE, 0 }, | 103 { PhoneField::REGEX_PHONE, PhoneField::FIELD_PHONE, 0 }, |
| 107 { PhoneField::REGEX_SEPARATOR, FIELD_NONE, 0 }, | 104 { PhoneField::REGEX_SEPARATOR, FIELD_NONE, 0 }, |
| 108 }; | 105 }; |
| 109 | 106 |
| 110 PhoneField::~PhoneField() {} | 107 PhoneField::~PhoneField() {} |
| 111 | 108 |
| 112 // static | 109 // static |
| 113 FormField* PhoneField::Parse(AutofillScanner* scanner, bool is_ecml) { | 110 FormField* PhoneField::Parse(AutofillScanner* scanner) { |
| 114 if (scanner->IsEnd()) | 111 if (scanner->IsEnd()) |
| 115 return NULL; | 112 return NULL; |
| 116 | 113 |
| 117 if (is_ecml) | |
| 118 return ParseECML(scanner); | |
| 119 | |
| 120 scoped_ptr<PhoneField> phone_field(new PhoneField); | 114 scoped_ptr<PhoneField> phone_field(new PhoneField); |
| 121 | 115 |
| 122 // Go through the phones in order HOME, FAX, attempting to match. HOME should | 116 // Go through the phones in order HOME, FAX, attempting to match. HOME should |
| 123 // be the last as it is a catch all case ("fax" and "faxarea" parsed as FAX, | 117 // be the last as it is a catch all case ("fax" and "faxarea" parsed as FAX, |
| 124 // but "area" and "someotherarea" parsed as HOME, for example). | 118 // but "area" and "someotherarea" parsed as HOME, for example). |
| 125 for (int i = PHONE_TYPE_MAX - 1; i >= PHONE_TYPE_FIRST; --i) { | 119 for (int i = PHONE_TYPE_MAX - 1; i >= PHONE_TYPE_FIRST; --i) { |
| 126 phone_field->SetPhoneType(static_cast<PhoneField::PhoneType>(i)); | 120 phone_field->SetPhoneType(static_cast<PhoneField::PhoneType>(i)); |
| 127 if (ParseInternal(phone_field.get(), scanner, i == HOME_PHONE)) | 121 if (ParseInternal(phone_field.get(), scanner, i == HOME_PHONE)) |
| 128 return phone_field.release(); | 122 return phone_field.release(); |
| 129 } | 123 } |
| 130 | 124 |
| 131 return NULL; | 125 return NULL; |
| 132 } | 126 } |
| 133 | 127 |
| 134 // static | |
| 135 FormField* PhoneField::ParseECML(AutofillScanner* scanner) { | |
| 136 string16 pattern(GetEcmlPattern(kEcmlShipToPhone, kEcmlBillToPhone, '|')); | |
| 137 | |
| 138 const AutofillField* field; | |
| 139 if (ParseField(scanner, pattern, &field)) { | |
| 140 PhoneField* phone_field = new PhoneField(); | |
| 141 phone_field->parsed_phone_fields_[FIELD_PHONE] = field; | |
| 142 return phone_field; | |
| 143 } | |
| 144 | |
| 145 return NULL; | |
| 146 } | |
| 147 | |
| 148 PhoneField::PhoneField() { | 128 PhoneField::PhoneField() { |
| 149 memset(parsed_phone_fields_, 0, sizeof(parsed_phone_fields_)); | 129 memset(parsed_phone_fields_, 0, sizeof(parsed_phone_fields_)); |
| 150 SetPhoneType(HOME_PHONE); | 130 SetPhoneType(HOME_PHONE); |
| 151 } | 131 } |
| 152 | 132 |
| 153 bool PhoneField::ClassifyField(FieldTypeMap* map) const { | 133 bool PhoneField::ClassifyField(FieldTypeMap* map) const { |
| 154 bool ok = true; | 134 bool ok = true; |
| 155 | 135 |
| 156 DCHECK(parsed_phone_fields_[FIELD_PHONE]); // Phone was correctly parsed. | 136 DCHECK(parsed_phone_fields_[FIELD_PHONE]); // Phone was correctly parsed. |
| 157 | 137 |
| (...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 347 void PhoneField::SetPhoneType(PhoneType phone_type) { | 327 void PhoneField::SetPhoneType(PhoneType phone_type) { |
| 348 // Field types are different as well, so we create a temporary phone number, | 328 // Field types are different as well, so we create a temporary phone number, |
| 349 // to get relevant field types. | 329 // to get relevant field types. |
| 350 if (phone_type == HOME_PHONE) | 330 if (phone_type == HOME_PHONE) |
| 351 number_.reset(new PhoneNumber(AutofillType::PHONE_HOME, NULL)); | 331 number_.reset(new PhoneNumber(AutofillType::PHONE_HOME, NULL)); |
| 352 else | 332 else |
| 353 number_.reset(new PhoneNumber(AutofillType::PHONE_FAX, NULL)); | 333 number_.reset(new PhoneNumber(AutofillType::PHONE_FAX, NULL)); |
| 354 phone_type_ = phone_type; | 334 phone_type_ = phone_type; |
| 355 } | 335 } |
| 356 | 336 |
| OLD | NEW |