Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(237)

Side by Side Diff: components/autofill/core/browser/credit_card.cc

Issue 22009003: [Autofill] Distinguish between native field types and potentially HTML field types. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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.h" 5 #include "components/autofill/core/browser/credit_card.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <ostream> 9 #include <ostream>
10 #include <string> 10 #include <string>
11 11
12 #include "base/basictypes.h" 12 #include "base/basictypes.h"
13 #include "base/guid.h" 13 #include "base/guid.h"
14 #include "base/logging.h" 14 #include "base/logging.h"
15 #include "base/strings/string16.h" 15 #include "base/strings/string16.h"
16 #include "base/strings/string_number_conversions.h" 16 #include "base/strings/string_number_conversions.h"
17 #include "base/strings/string_split.h" 17 #include "base/strings/string_split.h"
18 #include "base/strings/string_util.h" 18 #include "base/strings/string_util.h"
19 #include "base/strings/utf_string_conversions.h" 19 #include "base/strings/utf_string_conversions.h"
20 #include "base/time/time.h" 20 #include "base/time/time.h"
21 #include "components/autofill/core/browser/autofill_field.h" 21 #include "components/autofill/core/browser/autofill_field.h"
22 #include "components/autofill/core/browser/autofill_regexes.h" 22 #include "components/autofill/core/browser/autofill_regexes.h"
23 #include "components/autofill/core/browser/autofill_type.h" 23 #include "components/autofill/core/browser/autofill_type.h"
24 #include "components/autofill/core/browser/field_types.h"
25 #include "components/autofill/core/browser/validation.h" 24 #include "components/autofill/core/browser/validation.h"
26 #include "components/autofill/core/common/form_field_data.h" 25 #include "components/autofill/core/common/form_field_data.h"
27 #include "grit/component_strings.h" 26 #include "grit/component_strings.h"
28 #include "grit/webkit_resources.h" 27 #include "grit/webkit_resources.h"
29 #include "third_party/icu/source/common/unicode/uloc.h" 28 #include "third_party/icu/source/common/unicode/uloc.h"
30 #include "third_party/icu/source/i18n/unicode/dtfmtsym.h" 29 #include "third_party/icu/source/i18n/unicode/dtfmtsym.h"
31 #include "ui/base/l10n/l10n_util.h" 30 #include "ui/base/l10n/l10n_util.h"
32 31
33 namespace autofill { 32 namespace autofill {
34 33
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after
269 268
270 if (first_four_digits >= 3528 && first_four_digits <= 3589) 269 if (first_four_digits >= 3528 && first_four_digits <= 3589)
271 return kJCBCard; 270 return kJCBCard;
272 271
273 if (first_four_digits == 6011) 272 if (first_four_digits == 6011)
274 return kDiscoverCard; 273 return kDiscoverCard;
275 274
276 return kGenericCard; 275 return kGenericCard;
277 } 276 }
278 277
279 base::string16 CreditCard::GetRawInfo(AutofillFieldType type) const { 278 base::string16 CreditCard::GetRawInfo(ServerFieldType type) const {
280 switch (type) { 279 switch (type) {
281 case CREDIT_CARD_NAME: 280 case CREDIT_CARD_NAME:
282 return name_on_card_; 281 return name_on_card_;
283 282
284 case CREDIT_CARD_EXP_MONTH: 283 case CREDIT_CARD_EXP_MONTH:
285 return ExpirationMonthAsString(); 284 return ExpirationMonthAsString();
286 285
287 case CREDIT_CARD_EXP_2_DIGIT_YEAR: 286 case CREDIT_CARD_EXP_2_DIGIT_YEAR:
288 return Expiration2DigitYearAsString(); 287 return Expiration2DigitYearAsString();
289 288
(...skipping 25 matching lines...) Expand all
315 case CREDIT_CARD_VERIFICATION_CODE: 314 case CREDIT_CARD_VERIFICATION_CODE:
316 // Chrome doesn't store credit card verification codes. 315 // Chrome doesn't store credit card verification codes.
317 return base::string16(); 316 return base::string16();
318 317
319 default: 318 default:
320 // ComputeDataPresentForArray will hit this repeatedly. 319 // ComputeDataPresentForArray will hit this repeatedly.
321 return base::string16(); 320 return base::string16();
322 } 321 }
323 } 322 }
324 323
325 void CreditCard::SetRawInfo(AutofillFieldType type, 324 void CreditCard::SetRawInfo(ServerFieldType type,
326 const base::string16& value) { 325 const base::string16& value) {
327 switch (type) { 326 switch (type) {
328 case CREDIT_CARD_NAME: 327 case CREDIT_CARD_NAME:
329 name_on_card_ = value; 328 name_on_card_ = value;
330 break; 329 break;
331 330
332 case CREDIT_CARD_EXP_MONTH: 331 case CREDIT_CARD_EXP_MONTH:
333 SetExpirationMonthFromString(value, std::string()); 332 SetExpirationMonthFromString(value, std::string());
334 break; 333 break;
335 334
(...skipping 27 matching lines...) Expand all
363 case CREDIT_CARD_VERIFICATION_CODE: 362 case CREDIT_CARD_VERIFICATION_CODE:
364 // Chrome doesn't store the credit card verification code. 363 // Chrome doesn't store the credit card verification code.
365 break; 364 break;
366 365
367 default: 366 default:
368 NOTREACHED() << "Attempting to set unknown info-type " << type; 367 NOTREACHED() << "Attempting to set unknown info-type " << type;
369 break; 368 break;
370 } 369 }
371 } 370 }
372 371
373 base::string16 CreditCard::GetInfo(AutofillFieldType type, 372 base::string16 CreditCard::GetInfo(const AutofillType& type,
374 const std::string& app_locale) const { 373 const std::string& app_locale) const {
375 if (type == CREDIT_CARD_NUMBER) 374 if (type.server_type() == CREDIT_CARD_NUMBER)
376 return StripSeparators(number_); 375 return StripSeparators(number_);
377 376
378 return GetRawInfo(type); 377 return GetRawInfo(type.server_type());
379 } 378 }
380 379
381 bool CreditCard::SetInfo(AutofillFieldType type, 380 bool CreditCard::SetInfo(const AutofillType& type,
382 const base::string16& value, 381 const base::string16& value,
383 const std::string& app_locale) { 382 const std::string& app_locale) {
384 if (type == CREDIT_CARD_NUMBER) 383 ServerFieldType server_type = type.server_type();
385 SetRawInfo(type, StripSeparators(value)); 384 if (server_type == CREDIT_CARD_NUMBER)
386 else if (type == CREDIT_CARD_EXP_MONTH) 385 SetRawInfo(server_type, StripSeparators(value));
386 else if (server_type == CREDIT_CARD_EXP_MONTH)
387 SetExpirationMonthFromString(value, app_locale); 387 SetExpirationMonthFromString(value, app_locale);
388 else 388 else
389 SetRawInfo(type, value); 389 SetRawInfo(server_type, value);
390 390
391 return true; 391 return true;
392 } 392 }
393 393
394 void CreditCard::GetMatchingTypes(const base::string16& text, 394 void CreditCard::GetMatchingTypes(const base::string16& text,
395 const std::string& app_locale, 395 const std::string& app_locale,
396 FieldTypeSet* matching_types) const { 396 ServerFieldTypeSet* matching_types) const {
397 FormGroup::GetMatchingTypes(text, app_locale, matching_types); 397 FormGroup::GetMatchingTypes(text, app_locale, matching_types);
398 398
399 base::string16 card_number = GetInfo(CREDIT_CARD_NUMBER, app_locale); 399 base::string16 card_number =
400 GetInfo(AutofillType(CREDIT_CARD_NUMBER), app_locale);
400 if (!card_number.empty() && StripSeparators(text) == card_number) 401 if (!card_number.empty() && StripSeparators(text) == card_number)
401 matching_types->insert(CREDIT_CARD_NUMBER); 402 matching_types->insert(CREDIT_CARD_NUMBER);
402 403
403 int month; 404 int month;
404 if (ConvertMonth(text, app_locale, &month) && month != 0 && 405 if (ConvertMonth(text, app_locale, &month) && month != 0 &&
405 month == expiration_month_) { 406 month == expiration_month_) {
406 matching_types->insert(CREDIT_CARD_EXP_MONTH); 407 matching_types->insert(CREDIT_CARD_EXP_MONTH);
407 } 408 }
408 } 409 }
409 410
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
494 type_ = credit_card.type_; 495 type_ = credit_card.type_;
495 expiration_month_ = credit_card.expiration_month_; 496 expiration_month_ = credit_card.expiration_month_;
496 expiration_year_ = credit_card.expiration_year_; 497 expiration_year_ = credit_card.expiration_year_;
497 498
498 set_guid(credit_card.guid()); 499 set_guid(credit_card.guid());
499 set_origin(credit_card.origin()); 500 set_origin(credit_card.origin());
500 } 501 }
501 502
502 bool CreditCard::UpdateFromImportedCard(const CreditCard& imported_card, 503 bool CreditCard::UpdateFromImportedCard(const CreditCard& imported_card,
503 const std::string& app_locale) { 504 const std::string& app_locale) {
504 if (this->GetInfo(CREDIT_CARD_NUMBER, app_locale) != 505 if (this->GetInfo(AutofillType(CREDIT_CARD_NUMBER), app_locale) !=
505 imported_card.GetInfo(CREDIT_CARD_NUMBER, app_locale)) { 506 imported_card.GetInfo(AutofillType(CREDIT_CARD_NUMBER), app_locale)) {
506 return false; 507 return false;
507 } 508 }
508 509
509 // Heuristically aggregated data should never overwrite verified data. 510 // Heuristically aggregated data should never overwrite verified data.
510 // Instead, discard any heuristically aggregated credit cards that disagree 511 // Instead, discard any heuristically aggregated credit cards that disagree
511 // with explicitly entered data, so that the UI is not cluttered with 512 // with explicitly entered data, so that the UI is not cluttered with
512 // duplicate cards. 513 // duplicate cards.
513 if (this->IsVerified() && !imported_card.IsVerified()) 514 if (this->IsVerified() && !imported_card.IsVerified())
514 return true; 515 return true;
515 516
(...skipping 10 matching lines...) Expand all
526 expiration_month_ = imported_card.expiration_month_; 527 expiration_month_ = imported_card.expiration_month_;
527 expiration_year_ = imported_card.expiration_year_; 528 expiration_year_ = imported_card.expiration_year_;
528 529
529 return true; 530 return true;
530 } 531 }
531 532
532 void CreditCard::FillFormField(const AutofillField& field, 533 void CreditCard::FillFormField(const AutofillField& field,
533 size_t /*variant*/, 534 size_t /*variant*/,
534 const std::string& app_locale, 535 const std::string& app_locale,
535 FormFieldData* field_data) const { 536 FormFieldData* field_data) const {
536 DCHECK_EQ(CREDIT_CARD, AutofillType(field.type()).group()); 537 DCHECK_EQ(CREDIT_CARD, field.Type().group());
537 DCHECK(field_data); 538 DCHECK(field_data);
538 539
539 if (field_data->form_control_type == "select-one") { 540 if (field_data->form_control_type == "select-one") {
540 FillSelectControl(field.type(), app_locale, field_data); 541 FillSelectControl(field.Type(), app_locale, field_data);
541 } else if (field_data->form_control_type == "month") { 542 } else if (field_data->form_control_type == "month") {
542 // HTML5 input="month" consists of year-month. 543 // HTML5 input="month" consists of year-month.
543 base::string16 year = GetInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, app_locale); 544 base::string16 year =
544 base::string16 month = GetInfo(CREDIT_CARD_EXP_MONTH, app_locale); 545 GetInfo(AutofillType(CREDIT_CARD_EXP_4_DIGIT_YEAR), app_locale);
546 base::string16 month =
547 GetInfo(AutofillType(CREDIT_CARD_EXP_MONTH), app_locale);
545 if (!year.empty() && !month.empty()) { 548 if (!year.empty() && !month.empty()) {
546 // Fill the value only if |this| includes both year and month 549 // Fill the value only if |this| includes both year and month
547 // information. 550 // information.
548 field_data->value = year + ASCIIToUTF16("-") + month; 551 field_data->value = year + ASCIIToUTF16("-") + month;
549 } 552 }
550 } else { 553 } else {
551 field_data->value = GetInfo(field.type(), app_locale); 554 field_data->value = GetInfo(field.Type(), app_locale);
552 } 555 }
553 } 556 }
554 557
555 int CreditCard::Compare(const CreditCard& credit_card) const { 558 int CreditCard::Compare(const CreditCard& credit_card) const {
556 // The following CreditCard field types are the only types we store in the 559 // The following CreditCard field types are the only types we store in the
557 // WebDB so far, so we're only concerned with matching these types in the 560 // WebDB so far, so we're only concerned with matching these types in the
558 // credit card. 561 // credit card.
559 const AutofillFieldType types[] = { CREDIT_CARD_NAME, 562 const ServerFieldType types[] = { CREDIT_CARD_NAME,
560 CREDIT_CARD_NUMBER, 563 CREDIT_CARD_NUMBER,
561 CREDIT_CARD_EXP_MONTH, 564 CREDIT_CARD_EXP_MONTH,
562 CREDIT_CARD_EXP_4_DIGIT_YEAR }; 565 CREDIT_CARD_EXP_4_DIGIT_YEAR };
563 for (size_t index = 0; index < arraysize(types); ++index) { 566 for (size_t i = 0; i < arraysize(types); ++i) {
564 int comparison = GetRawInfo(types[index]).compare( 567 int comparison =
565 credit_card.GetRawInfo(types[index])); 568 GetRawInfo(types[i]).compare(credit_card.GetRawInfo(types[i]));
566 if (comparison != 0) 569 if (comparison != 0)
567 return comparison; 570 return comparison;
568 } 571 }
569 572
570 return 0; 573 return 0;
571 } 574 }
572 575
573 bool CreditCard::operator==(const CreditCard& credit_card) const { 576 bool CreditCard::operator==(const CreditCard& credit_card) const {
574 return guid() == credit_card.guid() && 577 return guid() == credit_card.guid() &&
575 origin() == credit_card.origin() && 578 origin() == credit_card.origin() &&
576 Compare(credit_card) == 0; 579 Compare(credit_card) == 0;
577 } 580 }
578 581
579 bool CreditCard::operator!=(const CreditCard& credit_card) const { 582 bool CreditCard::operator!=(const CreditCard& credit_card) const {
580 return !operator==(credit_card); 583 return !operator==(credit_card);
581 } 584 }
582 585
583 bool CreditCard::IsEmpty(const std::string& app_locale) const { 586 bool CreditCard::IsEmpty(const std::string& app_locale) const {
584 FieldTypeSet types; 587 ServerFieldTypeSet types;
585 GetNonEmptyTypes(app_locale, &types); 588 GetNonEmptyTypes(app_locale, &types);
586 return types.empty(); 589 return types.empty();
587 } 590 }
588 591
589 bool CreditCard::IsComplete() const { 592 bool CreditCard::IsComplete() const {
590 return 593 return
591 autofill::IsValidCreditCardNumber(number_) && 594 autofill::IsValidCreditCardNumber(number_) &&
592 expiration_month_ != 0 && 595 expiration_month_ != 0 &&
593 expiration_year_ != 0; 596 expiration_year_ != 0;
594 } 597 }
595 598
596 bool CreditCard::IsValid() const { 599 bool CreditCard::IsValid() const {
597 return autofill::IsValidCreditCardNumber(number_) && 600 return autofill::IsValidCreditCardNumber(number_) &&
598 autofill::IsValidCreditCardExpirationDate( 601 autofill::IsValidCreditCardExpirationDate(
599 expiration_year_, expiration_month_, base::Time::Now()); 602 expiration_year_, expiration_month_, base::Time::Now());
600 } 603 }
601 604
602 void CreditCard::GetSupportedTypes(FieldTypeSet* supported_types) const { 605 void CreditCard::GetSupportedTypes(ServerFieldTypeSet* supported_types) const {
603 supported_types->insert(CREDIT_CARD_NAME); 606 supported_types->insert(CREDIT_CARD_NAME);
604 supported_types->insert(CREDIT_CARD_NUMBER); 607 supported_types->insert(CREDIT_CARD_NUMBER);
605 supported_types->insert(CREDIT_CARD_TYPE); 608 supported_types->insert(CREDIT_CARD_TYPE);
606 supported_types->insert(CREDIT_CARD_EXP_MONTH); 609 supported_types->insert(CREDIT_CARD_EXP_MONTH);
607 supported_types->insert(CREDIT_CARD_EXP_2_DIGIT_YEAR); 610 supported_types->insert(CREDIT_CARD_EXP_2_DIGIT_YEAR);
608 supported_types->insert(CREDIT_CARD_EXP_4_DIGIT_YEAR); 611 supported_types->insert(CREDIT_CARD_EXP_4_DIGIT_YEAR);
609 supported_types->insert(CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR); 612 supported_types->insert(CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR);
610 supported_types->insert(CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR); 613 supported_types->insert(CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR);
611 } 614 }
612 615
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
701 const char* const kAmericanExpressCard = "americanExpressCC"; 704 const char* const kAmericanExpressCard = "americanExpressCC";
702 const char* const kDinersCard = "dinersCC"; 705 const char* const kDinersCard = "dinersCC";
703 const char* const kDiscoverCard = "discoverCC"; 706 const char* const kDiscoverCard = "discoverCC";
704 const char* const kGenericCard = "genericCC"; 707 const char* const kGenericCard = "genericCC";
705 const char* const kJCBCard = "jcbCC"; 708 const char* const kJCBCard = "jcbCC";
706 const char* const kMasterCard = "masterCardCC"; 709 const char* const kMasterCard = "masterCardCC";
707 const char* const kUnionPay = "unionPayCC"; 710 const char* const kUnionPay = "unionPayCC";
708 const char* const kVisaCard = "visaCC"; 711 const char* const kVisaCard = "visaCC";
709 712
710 } // namespace autofill 713 } // namespace autofill
OLDNEW
« no previous file with comments | « components/autofill/core/browser/credit_card.h ('k') | components/autofill/core/browser/credit_card_field.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698