1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/autofill/phone_field.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/string16.h"
#include "base/string_util.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/autofill/autofill_ecml.h"
#include "chrome/browser/autofill/autofill_field.h"
#include "chrome/browser/autofill/autofill_scanner.h"
#include "chrome/browser/autofill/fax_number.h"
#include "chrome/browser/autofill/home_phone_number.h"
#include "grit/autofill_resources.h"
#include "ui/base/l10n/l10n_util.h"
using autofill::GetEcmlPattern;
// Phone field grammars - first matched grammar will be parsed. Grammars are
// separated by { REGEX_SEPARATOR, FIELD_NONE, 0 }. Suffix and extension are
// parsed separately unless they are necessary parts of the match.
// The following notation is used to describe the patterns:
// <cc> - country code field.
// <ac> - area code field.
// <phone> - phone or prefix.
// <suffix> - suffix.
// <ext> - extension.
// :N means field is limited to N characters, otherwise it is unlimited.
// (pattern <field>)? means pattern is optional and matched separately.
PhoneField::Parser PhoneField::phone_field_grammars_[] = {
// Country code: <cc> Area Code: <ac> Phone: <phone> (- <suffix>
// (Ext: <ext>)?)?
{ PhoneField::REGEX_COUNTRY, PhoneField::FIELD_COUNTRY_CODE, 0 },
{ PhoneField::REGEX_AREA, PhoneField::FIELD_AREA_CODE, 0 },
{ PhoneField::REGEX_PHONE, PhoneField::FIELD_PHONE, 0 },
{ PhoneField::REGEX_SEPARATOR, FIELD_NONE, 0 },
// \( <ac> \) <phone>:3 <suffix>:4 (Ext: <ext>)?
{ PhoneField::REGEX_AREA_NOTEXT, PhoneField::FIELD_AREA_CODE, 3 },
{ PhoneField::REGEX_PREFIX_SEPARATOR, PhoneField::FIELD_PHONE, 3 },
{ PhoneField::REGEX_PHONE, PhoneField::FIELD_SUFFIX, 4 },
{ PhoneField::REGEX_SEPARATOR, FIELD_NONE, 0 },
// Phone: <cc> <ac>:3 - <phone>:3 - <suffix>:4 (Ext: <ext>)?
{ PhoneField::REGEX_PHONE, PhoneField::FIELD_COUNTRY_CODE, 0 },
{ PhoneField::REGEX_PHONE, PhoneField::FIELD_AREA_CODE, 3 },
{ PhoneField::REGEX_PREFIX_SEPARATOR, PhoneField::FIELD_PHONE, 3 },
{ PhoneField::REGEX_SUFFIX_SEPARATOR, PhoneField::FIELD_SUFFIX, 4 },
{ PhoneField::REGEX_SEPARATOR, FIELD_NONE, 0 },
// Phone: <cc>:3 <ac>:3 <phone>:3 <suffix>:4 (Ext: <ext>)?
{ PhoneField::REGEX_PHONE, PhoneField::FIELD_COUNTRY_CODE, 3 },
{ PhoneField::REGEX_PHONE, PhoneField::FIELD_AREA_CODE, 3 },
{ PhoneField::REGEX_PHONE, PhoneField::FIELD_PHONE, 3 },
{ PhoneField::REGEX_PHONE, PhoneField::FIELD_SUFFIX, 4 },
{ PhoneField::REGEX_SEPARATOR, FIELD_NONE, 0 },
// Area Code: <ac> Phone: <phone> (- <suffix> (Ext: <ext>)?)?
{ PhoneField::REGEX_AREA, PhoneField::FIELD_AREA_CODE, 0 },
{ PhoneField::REGEX_PHONE, PhoneField::FIELD_PHONE, 0 },
{ PhoneField::REGEX_SEPARATOR, FIELD_NONE, 0 },
// Phone: <ac> <phone>:3 <suffix>:4 (Ext: <ext>)?
{ PhoneField::REGEX_PHONE, PhoneField::FIELD_AREA_CODE, 0 },
{ PhoneField::REGEX_PHONE, PhoneField::FIELD_PHONE, 3 },
{ PhoneField::REGEX_PHONE, PhoneField::FIELD_SUFFIX, 4 },
{ PhoneField::REGEX_SEPARATOR, FIELD_NONE, 0 },
// Phone: <cc> \( <ac> \) <phone> (- <suffix> (Ext: <ext>)?)?
{ PhoneField::REGEX_PHONE, PhoneField::FIELD_COUNTRY_CODE, 0 },
{ PhoneField::REGEX_AREA_NOTEXT, PhoneField::FIELD_AREA_CODE, 0 },
{ PhoneField::REGEX_PREFIX_SEPARATOR, PhoneField::FIELD_PHONE, 0 },
{ PhoneField::REGEX_SEPARATOR, FIELD_NONE, 0 },
// Phone: \( <ac> \) <phone> (- <suffix> (Ext: <ext>)?)?
{ PhoneField::REGEX_PHONE, PhoneField::FIELD_COUNTRY_CODE, 0 },
{ PhoneField::REGEX_AREA_NOTEXT, PhoneField::FIELD_AREA_CODE, 0 },
{ PhoneField::REGEX_PREFIX_SEPARATOR, PhoneField::FIELD_PHONE, 0 },
{ PhoneField::REGEX_SEPARATOR, FIELD_NONE, 0 },
// Phone: <cc> - <ac> - <phone> - <suffix> (Ext: <ext>)?
{ PhoneField::REGEX_PHONE, PhoneField::FIELD_COUNTRY_CODE, 0 },
{ PhoneField::REGEX_PREFIX_SEPARATOR, PhoneField::FIELD_AREA_CODE, 0 },
{ PhoneField::REGEX_PREFIX_SEPARATOR, PhoneField::FIELD_PHONE, 0 },
{ PhoneField::REGEX_SUFFIX_SEPARATOR, PhoneField::FIELD_SUFFIX, 0 },
{ PhoneField::REGEX_SEPARATOR, FIELD_NONE, 0 },
// Phone: <ac> Prefix: <phone> Suffix: <suffix> (Ext: <ext>)?
{ PhoneField::REGEX_PHONE, PhoneField::FIELD_AREA_CODE, 0 },
{ PhoneField::REGEX_PREFIX, PhoneField::FIELD_PHONE, 0 },
{ PhoneField::REGEX_SUFFIX, PhoneField::FIELD_SUFFIX, 0 },
{ PhoneField::REGEX_SEPARATOR, FIELD_NONE, 0 },
// Phone: <ac> - <phone>:3 - <suffix>:4 (Ext: <ext>)?
{ PhoneField::REGEX_PHONE, PhoneField::FIELD_AREA_CODE, 0 },
{ PhoneField::REGEX_PREFIX_SEPARATOR, PhoneField::FIELD_PHONE, 3 },
{ PhoneField::REGEX_SUFFIX_SEPARATOR, PhoneField::FIELD_SUFFIX, 4 },
{ PhoneField::REGEX_SEPARATOR, FIELD_NONE, 0 },
// Phone: <cc> - <ac> - <phone> (Ext: <ext>)?
{ PhoneField::REGEX_PHONE, PhoneField::FIELD_COUNTRY_CODE, 0 },
{ PhoneField::REGEX_PREFIX_SEPARATOR, PhoneField::FIELD_AREA_CODE, 0 },
{ PhoneField::REGEX_SUFFIX_SEPARATOR, PhoneField::FIELD_PHONE, 0 },
{ PhoneField::REGEX_SEPARATOR, FIELD_NONE, 0 },
// Phone: <ac> - <phone> (Ext: <ext>)?
{ PhoneField::REGEX_AREA, PhoneField::FIELD_AREA_CODE, 0 },
{ PhoneField::REGEX_PHONE, PhoneField::FIELD_PHONE, 0 },
{ PhoneField::REGEX_SEPARATOR, FIELD_NONE, 0 },
// Phone: <cc>:3 - <phone>:10 (Ext: <ext>)?
{ PhoneField::REGEX_PHONE, PhoneField::FIELD_COUNTRY_CODE, 3 },
{ PhoneField::REGEX_PHONE, PhoneField::FIELD_PHONE, 10 },
{ PhoneField::REGEX_SEPARATOR, FIELD_NONE, 0 },
// Phone: <phone> (Ext: <ext>)?
{ PhoneField::REGEX_PHONE, PhoneField::FIELD_PHONE, 0 },
{ PhoneField::REGEX_SEPARATOR, FIELD_NONE, 0 },
};
PhoneField::~PhoneField() {}
// static
FormField* PhoneField::Parse(AutofillScanner* scanner, bool is_ecml) {
if (scanner->IsEnd())
return NULL;
if (is_ecml)
return ParseECML(scanner);
scoped_ptr<PhoneField> phone_field(new PhoneField);
// Go through the phones in order HOME, FAX, attempting to match. HOME should
// be the last as it is a catch all case ("fax" and "faxarea" parsed as FAX,
// but "area" and "someotherarea" parsed as HOME, for example).
for (int i = PHONE_TYPE_MAX - 1; i >= PHONE_TYPE_FIRST; --i) {
phone_field->SetPhoneType(static_cast<PhoneField::PhoneType>(i));
if (ParseInternal(phone_field.get(), scanner, i == HOME_PHONE))
return phone_field.release();
}
return NULL;
}
// static
FormField* PhoneField::ParseECML(AutofillScanner* scanner) {
string16 pattern(GetEcmlPattern(kEcmlShipToPhone, kEcmlBillToPhone, '|'));
const AutofillField* field;
if (ParseField(scanner, pattern, &field)) {
PhoneField* phone_field = new PhoneField();
phone_field->parsed_phone_fields_[FIELD_PHONE] = field;
return phone_field;
}
return NULL;
}
PhoneField::PhoneField() {
memset(parsed_phone_fields_, 0, sizeof(parsed_phone_fields_));
SetPhoneType(HOME_PHONE);
}
bool PhoneField::ClassifyField(FieldTypeMap* map) const {
bool ok = true;
DCHECK(parsed_phone_fields_[FIELD_PHONE]); // Phone was correctly parsed.
if ((parsed_phone_fields_[FIELD_COUNTRY_CODE] != NULL) ||
(parsed_phone_fields_[FIELD_AREA_CODE] != NULL) ||
(parsed_phone_fields_[FIELD_SUFFIX] != NULL)) {
if (parsed_phone_fields_[FIELD_COUNTRY_CODE] != NULL) {
ok = ok && AddClassification(parsed_phone_fields_[FIELD_COUNTRY_CODE],
number_->GetCountryCodeType(),
map);
}
AutofillFieldType field_number_type = number_->GetNumberType();
if (parsed_phone_fields_[FIELD_AREA_CODE] != NULL) {
ok = ok && AddClassification(parsed_phone_fields_[FIELD_AREA_CODE],
number_->GetCityCodeType(),
map);
} else if (parsed_phone_fields_[FIELD_COUNTRY_CODE] != NULL) {
// Only if we can find country code without city code, it means the phone
// number include city code.
field_number_type = number_->GetCityAndNumberType();
}
// We tag the prefix as PHONE_HOME_NUMBER, then when filling the form
// we fill only the prefix depending on the size of the input field.
ok = ok && AddClassification(parsed_phone_fields_[FIELD_PHONE],
field_number_type,
map);
// We tag the suffix as PHONE_HOME_NUMBER, then when filling the form
// we fill only the suffix depending on the size of the input field.
if (parsed_phone_fields_[FIELD_SUFFIX] != NULL) {
ok = ok && AddClassification(parsed_phone_fields_[FIELD_SUFFIX],
number_->GetNumberType(),
map);
}
} else {
ok = AddClassification(parsed_phone_fields_[FIELD_PHONE],
number_->GetWholeNumberType(),
map);
}
return ok;
}
string16 PhoneField::GetCountryRegex() const {
// This one is the same for Home and Fax numbers.
return l10n_util::GetStringUTF16(IDS_AUTOFILL_COUNTRY_CODE_RE);
}
string16 PhoneField::GetAreaRegex() const {
// This one is the same for Home and Fax numbers.
string16 area_code = l10n_util::GetStringUTF16(IDS_AUTOFILL_AREA_CODE_RE);
area_code.append(ASCIIToUTF16("|")); // Regexp separator.
area_code.append(GetAreaNoTextRegex());
return area_code;
}
string16 PhoneField::GetAreaNoTextRegex() const {
// This one is the same for Home and Fax numbers.
return l10n_util::GetStringUTF16(IDS_AUTOFILL_AREA_CODE_NOTEXT_RE);
}
string16 PhoneField::GetPhoneRegex() const {
if (phone_type_ == HOME_PHONE)
return l10n_util::GetStringUTF16(IDS_AUTOFILL_PHONE_RE);
else if (phone_type_ == FAX_PHONE)
return l10n_util::GetStringUTF16(IDS_AUTOFILL_FAX_RE);
else
NOTREACHED();
return string16();
}
string16 PhoneField::GetPrefixSeparatorRegex() const {
// This one is the same for Home and Fax numbers.
return l10n_util::GetStringUTF16(IDS_AUTOFILL_PHONE_PREFIX_SEPARATOR_RE);
}
string16 PhoneField::GetPrefixRegex() const {
// This one is the same for Home and Fax numbers.
return l10n_util::GetStringUTF16(IDS_AUTOFILL_PHONE_PREFIX_RE);
}
string16 PhoneField::GetSuffixSeparatorRegex() const {
// This one is the same for Home and Fax numbers.
return l10n_util::GetStringUTF16(IDS_AUTOFILL_PHONE_SUFFIX_SEPARATOR_RE);
}
string16 PhoneField::GetSuffixRegex() const {
// This one is the same for Home and Fax numbers.
return l10n_util::GetStringUTF16(IDS_AUTOFILL_PHONE_SUFFIX_RE);
}
string16 PhoneField::GetExtensionRegex() const {
// This one is the same for Home and Fax numbers.
return l10n_util::GetStringUTF16(IDS_AUTOFILL_PHONE_EXTENSION_RE);
}
string16 PhoneField::GetRegExp(RegexType regex_id) const {
switch (regex_id) {
case REGEX_COUNTRY: return GetCountryRegex();
case REGEX_AREA: return GetAreaRegex();
case REGEX_AREA_NOTEXT: return GetAreaNoTextRegex();
case REGEX_PHONE: return GetPhoneRegex();
case REGEX_PREFIX_SEPARATOR: return GetPrefixSeparatorRegex();
case REGEX_PREFIX: return GetPrefixRegex();
case REGEX_SUFFIX_SEPARATOR: return GetSuffixSeparatorRegex();
case REGEX_SUFFIX: return GetSuffixRegex();
case REGEX_EXTENSION: return GetExtensionRegex();
default:
NOTREACHED();
break;
}
return string16();
}
// static
bool PhoneField::ParseInternal(PhoneField *phone_field,
AutofillScanner* scanner,
bool regular_phone) {
DCHECK(phone_field);
scanner->SaveCursor();
// The form owns the following variables, so they should not be deleted.
const AutofillField* parsed_fields[FIELD_MAX];
for (size_t i = 0; i < arraysize(phone_field_grammars_); ++i) {
memset(parsed_fields, 0, sizeof(parsed_fields));
scanner->SaveCursor();
// Attempt to parse according to the next grammar.
for (; i < arraysize(phone_field_grammars_) &&
phone_field_grammars_[i].regex != REGEX_SEPARATOR; ++i) {
if (!ParseFieldSpecifics(
scanner,
phone_field->GetRegExp(phone_field_grammars_[i].regex),
MATCH_DEFAULT | MATCH_TELEPHONE,
&parsed_fields[phone_field_grammars_[i].phone_part]))
break;
if (phone_field_grammars_[i].max_size &&
(!parsed_fields[phone_field_grammars_[i].phone_part]->max_length ||
phone_field_grammars_[i].max_size <
parsed_fields[phone_field_grammars_[i].phone_part]->max_length)) {
break;
}
}
if (i >= arraysize(phone_field_grammars_)) {
scanner->Rewind();
return false; // Parsing failed.
}
if (phone_field_grammars_[i].regex == REGEX_SEPARATOR)
break; // Parsing succeeded.
// Proceed to the next grammar.
do {
++i;
} while (i < arraysize(phone_field_grammars_) &&
phone_field_grammars_[i].regex != REGEX_SEPARATOR);
if (i + 1 == arraysize(phone_field_grammars_)) {
scanner->Rewind();
return false; // Tried through all the possibilities - did not match.
}
scanner->Rewind();
}
if (!parsed_fields[FIELD_PHONE]) {
scanner->Rewind();
return false;
}
for (int i = 0; i < FIELD_MAX; ++i)
phone_field->parsed_phone_fields_[i] = parsed_fields[i];
// Look for optional fields.
// Look for a third text box.
if (!phone_field->parsed_phone_fields_[FIELD_SUFFIX]) {
if (!ParseField(scanner, phone_field->GetSuffixRegex(),
&phone_field->parsed_phone_fields_[FIELD_SUFFIX])) {
ParseField(scanner, phone_field->GetSuffixSeparatorRegex(),
&phone_field->parsed_phone_fields_[FIELD_SUFFIX]);
}
}
// Now look for an extension.
ParseField(scanner, phone_field->GetExtensionRegex(),
&phone_field->parsed_phone_fields_[FIELD_EXTENSION]);
return true;
}
void PhoneField::SetPhoneType(PhoneType phone_type) {
// Field types are different as well, so we create a temporary phone number,
// to get relevant field types.
if (phone_type == HOME_PHONE)
number_.reset(new PhoneNumber(AutofillType::PHONE_HOME, NULL));
else
number_.reset(new PhoneNumber(AutofillType::PHONE_FAX, NULL));
phone_type_ = phone_type;
}
|