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/credit_card_field.h" | 5 #include "components/autofill/core/browser/credit_card_field.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 if (!MatchesPattern(haystack[i + j], regex_needles[j])) | 43 if (!MatchesPattern(haystack[i + j], regex_needles[j])) |
44 break; | 44 break; |
45 | 45 |
46 if (j == regex_needles.size() - 1) | 46 if (j == regex_needles.size() - 1) |
47 return true; | 47 return true; |
48 } | 48 } |
49 } | 49 } |
50 return false; | 50 return false; |
51 } | 51 } |
52 | 52 |
| 53 // Returns true if a field that has |max_length| can fit the data for a field of |
| 54 // |type|. |
| 55 bool FieldCanFitDataForFieldType(int max_length, ServerFieldType type) { |
| 56 if (max_length == 0) |
| 57 return true; |
| 58 |
| 59 switch (type) { |
| 60 case CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR: { |
| 61 static int kMinimum2YearCcExpLength = strlen("12/14"); |
| 62 return max_length >= kMinimum2YearCcExpLength; |
| 63 } |
| 64 case CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR: { |
| 65 static int kMinimum4YearCcExpLength = strlen("12/2014"); |
| 66 return max_length >= kMinimum4YearCcExpLength; |
| 67 } |
| 68 default: |
| 69 NOTREACHED(); |
| 70 return false; |
| 71 } |
| 72 } |
| 73 |
| 74 |
53 } // namespace | 75 } // namespace |
54 | 76 |
55 // static | 77 // static |
56 scoped_ptr<FormField> CreditCardField::Parse(AutofillScanner* scanner) { | 78 scoped_ptr<FormField> CreditCardField::Parse(AutofillScanner* scanner) { |
57 if (scanner->IsEnd()) | 79 if (scanner->IsEnd()) |
58 return nullptr; | 80 return nullptr; |
59 | 81 |
60 scoped_ptr<CreditCardField> credit_card_field(new CreditCardField); | 82 scoped_ptr<CreditCardField> credit_card_field(new CreditCardField); |
61 size_t saved_cursor = scanner->SaveCursor(); | 83 size_t saved_cursor = scanner->SaveCursor(); |
62 | 84 |
(...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
352 kMatchTelAndSelect, | 374 kMatchTelAndSelect, |
353 &expiration_month_) && | 375 &expiration_month_) && |
354 ParseFieldSpecifics(scanner, | 376 ParseFieldSpecifics(scanner, |
355 base::ASCIIToUTF16("^(yy|yyyy)$"), | 377 base::ASCIIToUTF16("^(yy|yyyy)$"), |
356 kMatchTelAndSelect, | 378 kMatchTelAndSelect, |
357 &expiration_year_)) { | 379 &expiration_year_)) { |
358 return true; | 380 return true; |
359 } | 381 } |
360 | 382 |
361 // If that fails, try to parse a combined expiration field. | 383 // If that fails, try to parse a combined expiration field. |
362 // Look for a 2-digit year first. | |
363 // We allow <select> fields, because they're used e.g. on qvc.com. | 384 // We allow <select> fields, because they're used e.g. on qvc.com. |
364 scanner->RewindTo(month_year_saved_cursor); | 385 scanner->RewindTo(month_year_saved_cursor); |
365 | 386 |
366 static int kMinimum2YearCcExpLength = strlen("12/14"); | 387 // Bail out if the field cannot fit a 2-digit year expiration date. |
367 int current_field_max_length = scanner->Cursor()->max_length; | 388 const int current_field_max_length = scanner->Cursor()->max_length; |
368 if (current_field_max_length > 0 && | 389 if (!FieldCanFitDataForFieldType(current_field_max_length, |
369 current_field_max_length < kMinimum2YearCcExpLength) { | 390 CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR)) { |
370 return false; | 391 return false; |
371 } | 392 } |
372 | 393 |
| 394 // Try to look for a 2-digit year expiration date. |
373 if (ParseFieldSpecifics(scanner, | 395 if (ParseFieldSpecifics(scanner, |
374 base::UTF8ToUTF16(kExpirationDate2DigitYearRe), | 396 base::UTF8ToUTF16(kExpirationDate2DigitYearRe), |
375 kMatchTelAndSelect, | 397 kMatchTelAndSelect, |
376 &expiration_date_)) { | 398 &expiration_date_)) { |
377 exp_year_type_ = CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR; | 399 exp_year_type_ = CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR; |
378 expiration_month_ = nullptr; | 400 expiration_month_ = nullptr; |
379 return true; | 401 return true; |
380 } | 402 } |
381 | 403 |
| 404 // Try to look for a generic expiration date field. (2 or 4 digit year) |
382 if (ParseFieldSpecifics(scanner, | 405 if (ParseFieldSpecifics(scanner, |
383 base::UTF8ToUTF16(kExpirationDateRe), | 406 base::UTF8ToUTF16(kExpirationDateRe), |
384 kMatchTelAndSelect, | 407 kMatchTelAndSelect, |
385 &expiration_date_)) { | 408 &expiration_date_)) { |
386 static int kMinimum4YearCcExpLength = strlen("12/2014"); | 409 // If such a field exists, but it cannot fit a 4-digit year expiration |
387 if (current_field_max_length > 0 && | 410 // date, then the likely possibility is that it is a 2-digit year expiration |
388 current_field_max_length < kMinimum4YearCcExpLength) { | 411 // date. |
| 412 if (!FieldCanFitDataForFieldType(current_field_max_length, |
| 413 CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR)) { |
389 exp_year_type_ = CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR; | 414 exp_year_type_ = CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR; |
390 } | 415 } |
391 expiration_month_ = nullptr; | 416 expiration_month_ = nullptr; |
392 return true; | 417 return true; |
393 } | 418 } |
394 | 419 |
| 420 // Try to look for a 4-digit year expiration date. |
| 421 if (FieldCanFitDataForFieldType(current_field_max_length, |
| 422 CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR) && |
| 423 ParseFieldSpecifics(scanner, |
| 424 base::UTF8ToUTF16(kExpirationDate4DigitYearRe), |
| 425 kMatchTelAndSelect, |
| 426 &expiration_date_)) { |
| 427 expiration_month_ = nullptr; |
| 428 return true; |
| 429 } |
| 430 |
395 return false; | 431 return false; |
396 } | 432 } |
397 | 433 |
398 ServerFieldType CreditCardField::GetExpirationYearType() const { | 434 ServerFieldType CreditCardField::GetExpirationYearType() const { |
399 return (expiration_date_ | 435 return (expiration_date_ |
400 ? exp_year_type_ | 436 ? exp_year_type_ |
401 : ((expiration_year_ && expiration_year_->max_length == 2) | 437 : ((expiration_year_ && expiration_year_->max_length == 2) |
402 ? CREDIT_CARD_EXP_2_DIGIT_YEAR | 438 ? CREDIT_CARD_EXP_2_DIGIT_YEAR |
403 : CREDIT_CARD_EXP_4_DIGIT_YEAR)); | 439 : CREDIT_CARD_EXP_4_DIGIT_YEAR)); |
404 } | 440 } |
405 | 441 |
406 } // namespace autofill | 442 } // namespace autofill |
OLD | NEW |