Chromium Code Reviews| 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/address_field.h" | 5 #include "chrome/browser/autofill/address_field.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/string16.h" | 11 #include "base/string16.h" |
| 12 #include "base/string_util.h" | 12 #include "base/string_util.h" |
| 13 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
| 14 #include "chrome/browser/autofill/autofill_field.h" | 14 #include "chrome/browser/autofill/autofill_field.h" |
| 15 #include "chrome/browser/autofill/autofill_scanner.h" | 15 #include "chrome/browser/autofill/autofill_scanner.h" |
| 16 #include "chrome/browser/autofill/email_field.h" | |
| 17 #include "chrome/browser/autofill/phone_field.h" | |
| 16 #include "grit/autofill_resources.h" | 18 #include "grit/autofill_resources.h" |
| 17 #include "ui/base/l10n/l10n_util.h" | 19 #include "ui/base/l10n/l10n_util.h" |
| 18 | 20 |
| 19 bool AddressField::GetFieldInfo(FieldTypeMap* field_type_map) const { | 21 bool AddressField::GetFieldInfo(FieldTypeMap* field_type_map) const { |
| 20 AutofillFieldType address_company; | 22 AutofillFieldType address_company; |
| 21 AutofillFieldType address_line1; | 23 AutofillFieldType address_line1; |
| 22 AutofillFieldType address_line2; | 24 AutofillFieldType address_line2; |
| 23 AutofillFieldType address_city; | 25 AutofillFieldType address_city; |
| 24 AutofillFieldType address_state; | 26 AutofillFieldType address_state; |
| 25 AutofillFieldType address_zip; | 27 AutofillFieldType address_zip; |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 71 const AutofillField* initial_field = scanner->Cursor(); | 73 const AutofillField* initial_field = scanner->Cursor(); |
| 72 scanner->SaveCursor(); | 74 scanner->SaveCursor(); |
| 73 | 75 |
| 74 string16 attention_ignored = | 76 string16 attention_ignored = |
| 75 l10n_util::GetStringUTF16(IDS_AUTOFILL_ATTENTION_IGNORED_RE); | 77 l10n_util::GetStringUTF16(IDS_AUTOFILL_ATTENTION_IGNORED_RE); |
| 76 string16 region_ignored = | 78 string16 region_ignored = |
| 77 l10n_util::GetStringUTF16(IDS_AUTOFILL_REGION_IGNORED_RE); | 79 l10n_util::GetStringUTF16(IDS_AUTOFILL_REGION_IGNORED_RE); |
| 78 | 80 |
| 79 // Allow address fields to appear in any order. | 81 // Allow address fields to appear in any order. |
| 80 while (!scanner->IsEnd()) { | 82 while (!scanner->IsEnd()) { |
| 83 // Every loop, we have to parse email and phone, and if we find it, we | |
| 84 // break the loop even while continuing address. Because we want to give | |
| 85 // email and phone more priority than address. We have to rewind |scanner| | |
| 86 // position after the email and phone parsing. | |
| 87 size_t cursor_position = scanner->SaveCursor(); | |
| 88 bool is_email = EmailField::Parse(scanner, is_ecml); | |
| 89 scanner->RewindTo(cursor_position); | |
| 90 if (is_email) | |
| 91 break; | |
| 92 | |
| 93 cursor_position = scanner->SaveCursor(); | |
| 94 bool is_phone = PhoneField::Parse(scanner, is_ecml); | |
| 95 scanner->RewindTo(cursor_position); | |
| 96 if (is_phone) | |
| 97 break; | |
|
Ilya Sherman
2011/05/14 06:59:12
David and I discussed this a bit offline today --
Ilya Sherman
2011/05/14 07:01:33
Whoops, didn't mean to send this comment out -- wa
honten.org
2011/05/14 07:41:20
I see, it might be better idea.
But, at first, I
| |
| 98 | |
| 81 if (ParseCompany(scanner, is_ecml, address_field.get()) || | 99 if (ParseCompany(scanner, is_ecml, address_field.get()) || |
| 82 ParseAddressLines(scanner, is_ecml, address_field.get()) || | 100 ParseAddressLines(scanner, is_ecml, address_field.get()) || |
| 83 ParseCity(scanner, is_ecml, address_field.get()) || | 101 ParseCity(scanner, is_ecml, address_field.get()) || |
| 84 ParseState(scanner, is_ecml, address_field.get()) || | 102 ParseState(scanner, is_ecml, address_field.get()) || |
| 85 ParseZipCode(scanner, is_ecml, address_field.get()) || | 103 ParseZipCode(scanner, is_ecml, address_field.get()) || |
| 86 ParseCountry(scanner, is_ecml, address_field.get())) { | 104 ParseCountry(scanner, is_ecml, address_field.get())) { |
| 87 continue; | 105 continue; |
| 88 } else if (ParseText(scanner, attention_ignored) || | 106 } else if (ParseText(scanner, attention_ignored) || |
| 89 ParseText(scanner, region_ignored)) { | 107 ParseText(scanner, region_ignored)) { |
| 90 // We ignore the following: | 108 // We ignore the following: |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 239 // Note: ECML standard uses 2 letter country code (ISO 3166) | 257 // Note: ECML standard uses 2 letter country code (ISO 3166) |
| 240 if (address_field->country_ && !address_field->country_->IsEmpty()) | 258 if (address_field->country_ && !address_field->country_->IsEmpty()) |
| 241 return false; | 259 return false; |
| 242 | 260 |
| 243 string16 pattern; | 261 string16 pattern; |
| 244 if (is_ecml) | 262 if (is_ecml) |
| 245 pattern = GetEcmlPattern(kEcmlShipToCountry, kEcmlBillToCountry, '|'); | 263 pattern = GetEcmlPattern(kEcmlShipToCountry, kEcmlBillToCountry, '|'); |
| 246 else | 264 else |
| 247 pattern = l10n_util::GetStringUTF16(IDS_AUTOFILL_COUNTRY_RE); | 265 pattern = l10n_util::GetStringUTF16(IDS_AUTOFILL_COUNTRY_RE); |
| 248 | 266 |
| 249 return ParseText(scanner, pattern, &address_field->country_); | 267 return ParseText(scanner, FormField::Pattern(pattern, true), |
| 268 &address_field->country_); | |
| 250 } | 269 } |
| 251 | 270 |
| 252 // static | 271 // static |
| 253 bool AddressField::ParseZipCode(AutofillScanner* scanner, | 272 bool AddressField::ParseZipCode(AutofillScanner* scanner, |
| 254 bool is_ecml, | 273 bool is_ecml, |
| 255 AddressField* address_field) { | 274 AddressField* address_field) { |
| 256 // Parse a zip code. On some UK pages (e.g. The China Shop2.html) this | 275 // Parse a zip code. On some UK pages (e.g. The China Shop2.html) this |
| 257 // is called a "post code". | 276 // is called a "post code". |
| 258 // | 277 // |
| 259 // HACK: Just for the MapQuest driving directions page we match the | 278 // HACK: Just for the MapQuest driving directions page we match the |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 308 // the term "town". | 327 // the term "town". |
| 309 if (address_field->city_) | 328 if (address_field->city_) |
| 310 return false; | 329 return false; |
| 311 | 330 |
| 312 string16 pattern; | 331 string16 pattern; |
| 313 if (is_ecml) | 332 if (is_ecml) |
| 314 pattern = GetEcmlPattern(kEcmlShipToCity, kEcmlBillToCity, '|'); | 333 pattern = GetEcmlPattern(kEcmlShipToCity, kEcmlBillToCity, '|'); |
| 315 else | 334 else |
| 316 pattern = l10n_util::GetStringUTF16(IDS_AUTOFILL_CITY_RE); | 335 pattern = l10n_util::GetStringUTF16(IDS_AUTOFILL_CITY_RE); |
| 317 | 336 |
| 318 return ParseText(scanner, pattern, &address_field->city_); | 337 return ParseText(scanner, FormField::Pattern(pattern, true), |
| 338 &address_field->city_); | |
| 319 } | 339 } |
| 320 | 340 |
| 321 // static | 341 // static |
| 322 bool AddressField::ParseState(AutofillScanner* scanner, | 342 bool AddressField::ParseState(AutofillScanner* scanner, |
| 323 bool is_ecml, | 343 bool is_ecml, |
| 324 AddressField* address_field) { | 344 AddressField* address_field) { |
| 325 if (address_field->state_) | 345 if (address_field->state_) |
| 326 return false; | 346 return false; |
| 327 | 347 |
| 328 string16 pattern; | 348 string16 pattern; |
| 329 if (is_ecml) | 349 if (is_ecml) |
| 330 pattern = GetEcmlPattern(kEcmlShipToStateProv, kEcmlBillToStateProv, '|'); | 350 pattern = GetEcmlPattern(kEcmlShipToStateProv, kEcmlBillToStateProv, '|'); |
| 331 else | 351 else |
| 332 pattern = l10n_util::GetStringUTF16(IDS_AUTOFILL_STATE_RE); | 352 pattern = l10n_util::GetStringUTF16(IDS_AUTOFILL_STATE_RE); |
| 333 | 353 |
| 334 return ParseText(scanner, pattern, &address_field->state_); | 354 return ParseText(scanner, FormField::Pattern(pattern, true), |
| 355 &address_field->state_); | |
| 335 } | 356 } |
| 336 | 357 |
| 337 AddressType AddressField::AddressTypeFromText(const string16 &text) { | 358 AddressType AddressField::AddressTypeFromText(const string16 &text) { |
| 338 if (text.find(l10n_util::GetStringUTF16(IDS_AUTOFILL_ADDRESS_TYPE_SAME_AS_RE)) | 359 if (text.find(l10n_util::GetStringUTF16(IDS_AUTOFILL_ADDRESS_TYPE_SAME_AS_RE)) |
| 339 != string16::npos || | 360 != string16::npos || |
| 340 text.find(l10n_util::GetStringUTF16(IDS_AUTOFILL_ADDRESS_TYPE_USE_MY_RE)) | 361 text.find(l10n_util::GetStringUTF16(IDS_AUTOFILL_ADDRESS_TYPE_USE_MY_RE)) |
| 341 != string16::npos) | 362 != string16::npos) |
| 342 // This text could be a checkbox label such as "same as my billing | 363 // This text could be a checkbox label such as "same as my billing |
| 343 // address" or "use my shipping address". | 364 // address" or "use my shipping address". |
| 344 // ++ It would help if we generally skipped all text that appears | 365 // ++ It would help if we generally skipped all text that appears |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 360 return kBillingAddress; | 381 return kBillingAddress; |
| 361 | 382 |
| 362 if (bill == string16::npos && ship != string16::npos) | 383 if (bill == string16::npos && ship != string16::npos) |
| 363 return kShippingAddress; | 384 return kShippingAddress; |
| 364 | 385 |
| 365 if (bill > ship) | 386 if (bill > ship) |
| 366 return kBillingAddress; | 387 return kBillingAddress; |
| 367 | 388 |
| 368 return kShippingAddress; | 389 return kShippingAddress; |
| 369 } | 390 } |
| OLD | NEW |