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. The second | |
395 // ParseFieldSpecifics() call is separate to make it clearer what it is | |
396 // trying to match. | |
Evan Stade
2015/04/03 02:04:57
This logic confuses me. I think they should be one
| |
373 if (ParseFieldSpecifics(scanner, | 397 if (ParseFieldSpecifics(scanner, |
374 base::UTF8ToUTF16(kExpirationDate2DigitYearRe), | 398 base::UTF8ToUTF16(kExpirationDate2DigitYearRe), |
375 kMatchTelAndSelect, | 399 kMatchTelAndSelect, |
400 &expiration_date_) || | |
401 ParseFieldSpecifics(scanner, | |
402 base::ASCIIToUTF16("^mm\\s*[-/]\\syy$"), | |
403 kMatchTelAndSelect, | |
376 &expiration_date_)) { | 404 &expiration_date_)) { |
377 exp_year_type_ = CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR; | 405 exp_year_type_ = CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR; |
378 expiration_month_ = nullptr; | 406 expiration_month_ = nullptr; |
379 return true; | 407 return true; |
380 } | 408 } |
381 | 409 |
410 // Try to look for a generic expiration date field. (2 or 4 digit year) | |
382 if (ParseFieldSpecifics(scanner, | 411 if (ParseFieldSpecifics(scanner, |
383 base::UTF8ToUTF16(kExpirationDateRe), | 412 base::UTF8ToUTF16(kExpirationDateRe), |
384 kMatchTelAndSelect, | 413 kMatchTelAndSelect, |
385 &expiration_date_)) { | 414 &expiration_date_)) { |
386 static int kMinimum4YearCcExpLength = strlen("12/2014"); | 415 // If such a field exists, bit it cannot fit a 4-digit year expiration |
Evan Stade
2015/04/03 02:04:57
s/bit/but
| |
387 if (current_field_max_length > 0 && | 416 // date, then the likely possibility is that it is a 2-digit year expiration |
388 current_field_max_length < kMinimum4YearCcExpLength) { | 417 // date. |
418 if (!FieldCanFitDataForFieldType(current_field_max_length, | |
419 CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR)) { | |
389 exp_year_type_ = CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR; | 420 exp_year_type_ = CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR; |
390 } | 421 } |
391 expiration_month_ = nullptr; | 422 expiration_month_ = nullptr; |
392 return true; | 423 return true; |
393 } | 424 } |
394 | 425 |
426 // Try to look for a 4-digit year expiration date. | |
427 if (FieldCanFitDataForFieldType(current_field_max_length, | |
428 CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR) && | |
429 ParseFieldSpecifics(scanner, | |
430 base::ASCIIToUTF16("^mm\\s*[-/]\\syyyy$"), | |
Evan Stade
2015/04/03 02:04:57
I think this can go in the utf8 file. mm/yyyy is E
| |
431 kMatchTelAndSelect, | |
432 &expiration_date_)) { | |
433 expiration_month_ = nullptr; | |
434 return true; | |
435 } | |
436 | |
395 return false; | 437 return false; |
396 } | 438 } |
397 | 439 |
398 ServerFieldType CreditCardField::GetExpirationYearType() const { | 440 ServerFieldType CreditCardField::GetExpirationYearType() const { |
399 return (expiration_date_ | 441 return (expiration_date_ |
400 ? exp_year_type_ | 442 ? exp_year_type_ |
401 : ((expiration_year_ && expiration_year_->max_length == 2) | 443 : ((expiration_year_ && expiration_year_->max_length == 2) |
402 ? CREDIT_CARD_EXP_2_DIGIT_YEAR | 444 ? CREDIT_CARD_EXP_2_DIGIT_YEAR |
403 : CREDIT_CARD_EXP_4_DIGIT_YEAR)); | 445 : CREDIT_CARD_EXP_4_DIGIT_YEAR)); |
404 } | 446 } |
405 | 447 |
406 } // namespace autofill | 448 } // namespace autofill |
OLD | NEW |