diff options
author | jhawkins@chromium.org <jhawkins@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-07-30 05:35:12 +0000 |
---|---|---|
committer | jhawkins@chromium.org <jhawkins@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-07-30 05:35:12 +0000 |
commit | 587b40b6124aea9759b6b90ffb01e72fc2610935 (patch) | |
tree | 24de276f8e78308d8ba14e2a26db6cf7ad10eb95 /chrome/browser/autofill | |
parent | 4f4ada85e23ba03759aaf617c994bf2efc7e91c4 (diff) | |
download | chromium_src-587b40b6124aea9759b6b90ffb01e72fc2610935.zip chromium_src-587b40b6124aea9759b6b90ffb01e72fc2610935.tar.gz chromium_src-587b40b6124aea9759b6b90ffb01e72fc2610935.tar.bz2 |
AutoFill: Determine credit card type based on the CC number and display the
appropriate CC icon.
BUG=50080
TEST=none
Review URL: http://codereview.chromium.org/3010041
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@54280 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/autofill')
-rw-r--r-- | chrome/browser/autofill/autofill_manager.cc | 23 | ||||
-rw-r--r-- | chrome/browser/autofill/credit_card.cc | 125 | ||||
-rw-r--r-- | chrome/browser/autofill/credit_card.h | 6 |
3 files changed, 117 insertions, 37 deletions
diff --git a/chrome/browser/autofill/autofill_manager.cc b/chrome/browser/autofill/autofill_manager.cc index 5865235..a3a2bfb 100644 --- a/chrome/browser/autofill/autofill_manager.cc +++ b/chrome/browser/autofill/autofill_manager.cc @@ -54,12 +54,6 @@ string16 CombineLabelAndCreditCard(const string16& label, return label + kLabelSeparator + cc->LastFourDigits(); } -// The name of the generic credit card icon, which maps to the image resource ID -// in webkit/glue:WebKitClientImpl. -// TODO(jhawkins): Move the images to chrome/common and implement the resource -// handling in RendererWebKitClientImpl. -const char kGenericCC[] = "genericCC"; - // Removes duplicate elements whilst preserving original order of |elements| and // |unique_ids|. void RemoveDuplicateElements( @@ -505,14 +499,13 @@ void AutoFillManager::GetProfileSuggestions(FormStructure* form, } } - // No CC, so no icons. - icons->resize(values->size()); - AutoFillProfile::CreateInferredLabels(&matched_profiles, labels, 0, type.field_type()); - if (!include_cc_labels || !form->HasBillingFields() || !FormIsHTTPS(form)) + if (!include_cc_labels || !form->HasBillingFields() || !FormIsHTTPS(form)) { + icons->resize(values->size()); // No CC, so no icons. return; + } size_t i = 0; std::vector<string16> expanded_values; @@ -528,13 +521,13 @@ void AutoFillManager::GetProfileSuggestions(FormStructure* form, expanded_values.push_back((*values)[i]); string16 label = CombineLabelAndCreditCard((*labels)[i], *cc); expanded_labels.push_back(label); + icons->push_back((*cc)->type()); expanded_ids.push_back(PackIDs((*cc)->unique_id(), profile->unique_id())); } } + expanded_labels.swap(*labels); expanded_values.swap(*values); - // No CC, so no icons. - icons->resize(values->size()); expanded_ids.swap(*unique_ids); } @@ -605,7 +598,7 @@ void AutoFillManager::GetBillingProfileSuggestions( string16 label = CombineLabelAndCreditCard(inferred_labels[i], matching_creditcards[i]); labels->push_back(label); - icons->push_back(ASCIIToUTF16(kGenericCC)); + icons->push_back(matching_creditcards[i]->type()); unique_ids->push_back(PackIDs(matching_creditcards[i]->unique_id(), matching_profiles[i]->unique_id())); } @@ -637,7 +630,7 @@ void AutoFillManager::GetCreditCardSuggestions(FormStructure* form, if (!form->HasNonBillingFields()) { values->push_back(creditcard_field_value); labels->push_back(CombineLabelAndCreditCard(string16(), credit_card)); - icons->push_back(ASCIIToUTF16(kGenericCC)); + icons->push_back(credit_card->type()); unique_ids->push_back(PackIDs(credit_card->unique_id(), 0)); } else { const std::vector<AutoFillProfile*>& profiles @@ -655,7 +648,7 @@ void AutoFillManager::GetCreditCardSuggestions(FormStructure* form, string16 label = CombineLabelAndCreditCard(inferred_labels[i], credit_card); labels->push_back(label); - icons->push_back(ASCIIToUTF16(kGenericCC)); + icons->push_back(credit_card->type()); unique_ids->push_back( PackIDs(credit_card->unique_id(), profiles[i]->unique_id())); } diff --git a/chrome/browser/autofill/credit_card.cc b/chrome/browser/autofill/credit_card.cc index 7c9bbda..7f3e521 100644 --- a/chrome/browser/autofill/credit_card.cc +++ b/chrome/browser/autofill/credit_card.cc @@ -4,6 +4,8 @@ #include "chrome/browser/autofill/credit_card.h" +#include <string> + #include "app/l10n_util.h" #include "base/basictypes.h" #include "base/string_util.h" @@ -28,6 +30,100 @@ const AutoFillFieldType kAutoFillCreditCardTypes[] = { const int kAutoFillCreditCardLength = arraysize(kAutoFillCreditCardTypes); +// These values must match the values in WebKitClientImpl in webkit/glue. We +// send these strings to WK, which then asks WebKitClientImpl to load the image +// data. +const char* kAmericanExpressCard = "americanExpressCC"; +const char* kDinersCard = "dinersCC"; +const char* kDiscoverCard = "discoverCC"; +const char* kGenericCard = "genericCC"; +const char* kJCBCard = "jcbCC"; +const char* kMasterCard = "masterCardCC"; +const char* kSoloCard = "soloCC"; +const char* kVisaCard = "visaCC"; + +std::string GetCreditCardType(string16 number) { + // Credit card number specifications taken from: + // http://en.wikipedia.org/wiki/Credit_card_numbers and + // http://www.beachnet.com/~hstiles/cardtype.html + // Card Type Prefix(es) Length + // --------------------------------------------------------------- + // Visa 4 13,16 + // American Express 34,37 15 + // Diners Club 300-305,2014,2149,36, 14,15 + // Discover Card 6011,65 16 + // JCB 3 16 + // JCB 2131,1800 15 + // MasterCard 51-55 16 + // Solo (debit card) 6334,6767 16,18,19 + + // We need at least 4 digits to work with. + if (number.length() < 4) + return kGenericCard; + + int first_four_digits = 0; + if (!StringToInt(number.substr(0, 4), &first_four_digits)) + return kGenericCard; + + int first_three_digits = first_four_digits / 10; + int first_two_digits = first_three_digits / 10; + int first_digit = first_two_digits / 10; + + switch (number.length()) { + case 13: + if (first_digit == 4) + return kVisaCard; + + break; + case 14: + if (first_three_digits <= 300 && first_three_digits >=305) + return kDinersCard; + + if (first_digit == 36) + return kDinersCard; + + break; + case 15: + if (first_two_digits == 34 || first_two_digits == 37) + return kAmericanExpressCard; + + if (first_four_digits == 2131 || first_four_digits == 1800) + return kJCBCard; + + if (first_four_digits == 2014 || first_four_digits == 2149) + return kDinersCard; + + break; + case 16: + if (first_four_digits == 6011 || first_two_digits == 65) + return kDiscoverCard; + + if (first_four_digits == 6334 || first_four_digits == 6767) + return kSoloCard; + + if (first_two_digits >= 51 && first_two_digits <= 55) + return kMasterCard; + + if (first_two_digits == 54 || first_two_digits == 55) + + if (first_digit == 3) + return kJCBCard; + + if (first_digit == 4) + return kVisaCard; + + break; + case 18: + case 19: + if (first_four_digits == 6334 || first_four_digits == 6767) + return kSoloCard; + + break; + } + + return kGenericCard; +} + } // namespace CreditCard::CreditCard(const string16& label, int unique_id) @@ -67,9 +163,6 @@ void CreditCard::GetPossibleFieldTypes(const string16& text, if (Is4DigitExpirationYear(text)) possible_types->insert(CREDIT_CARD_EXP_4_DIGIT_YEAR); - - if (IsCardType(text)) - possible_types->insert(CREDIT_CARD_TYPE); } void CreditCard::GetAvailableFieldTypes(FieldTypeSet* available_types) const { @@ -87,9 +180,6 @@ void CreditCard::GetAvailableFieldTypes(FieldTypeSet* available_types) const { if (!Expiration4DigitYearAsString().empty()) available_types->insert(CREDIT_CARD_EXP_4_DIGIT_YEAR); - if (!type().empty()) - available_types->insert(CREDIT_CARD_TYPE); - if (!number().empty()) available_types->insert(CREDIT_CARD_NUMBER); } @@ -160,7 +250,8 @@ string16 CreditCard::GetFieldText(const AutoFillType& type) const { } case CREDIT_CARD_TYPE: - return this->type(); + // We don't handle this case. + return string16(); case CREDIT_CARD_NUMBER: return number(); @@ -204,7 +295,7 @@ void CreditCard::SetInfo(const AutoFillType& type, const string16& value) { break; case CREDIT_CARD_TYPE: - set_type(value); + // We determine the type based on the number. break; case CREDIT_CARD_NUMBER: { @@ -213,6 +304,7 @@ void CreditCard::SetInfo(const AutoFillType& type, const string16& value) { break; } set_number(value); + set_type(ASCIIToUTF16(GetCreditCardType(number()))); // Update last four digits as well. if (value.length() > 4) set_last_four_digits(value.substr(value.length() - 4)); @@ -418,18 +510,21 @@ bool CreditCard::FindInfoMatchesHelper(const AutoFillFieldType& field_type, string16 exp_month(ExpirationMonthAsString()); if (StartsWith(exp_month, info, true)) *match = exp_month; + break; } case CREDIT_CARD_EXP_2_DIGIT_YEAR: { string16 exp_year(Expiration2DigitYearAsString()); if (StartsWith(exp_year, info, true)) *match = exp_year; + break; } case CREDIT_CARD_EXP_4_DIGIT_YEAR: { string16 exp_year(Expiration4DigitYearAsString()); if (StartsWith(exp_year, info, true)) *match = exp_year; + break; } case CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR: { @@ -437,6 +532,7 @@ bool CreditCard::FindInfoMatchesHelper(const AutoFillFieldType& field_type, Expiration2DigitYearAsString()); if (StartsWith(exp_date, info, true)) *match = exp_date; + break; } case CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR: { @@ -444,13 +540,12 @@ bool CreditCard::FindInfoMatchesHelper(const AutoFillFieldType& field_type, Expiration4DigitYearAsString()); if (StartsWith(exp_date, info, true)) *match = exp_date; + break; } - case CREDIT_CARD_TYPE: { - string16 card_type(this->type()); - if (StartsWith(card_type, info, true)) - *match = card_type; - } + case CREDIT_CARD_TYPE: + // We don't handle this case. + break; case CREDIT_CARD_VERIFICATION_CODE: NOTREACHED(); @@ -490,10 +585,6 @@ bool CreditCard::Is4DigitExpirationYear(const string16& text) const { return expiration_year_ == year; } -bool CreditCard::IsCardType(const string16& text) const { - return StringToLowerASCII(text) == StringToLowerASCII(type_); -} - bool CreditCard::ConvertDate(const string16& date, int* num) const { if (!date.empty()) { bool converted = StringToInt(date, num); diff --git a/chrome/browser/autofill/credit_card.h b/chrome/browser/autofill/credit_card.h index 974cbaa5..40b85d5 100644 --- a/chrome/browser/autofill/credit_card.h +++ b/chrome/browser/autofill/credit_card.h @@ -39,6 +39,7 @@ class CreditCard : public FormGroup { // The last four digits of the credit card number. string16 LastFourDigits() const; + const string16& type() const { return type_; } const string16& billing_address() const { return billing_address_; } int unique_id() const { return unique_id_; } @@ -79,7 +80,6 @@ class CreditCard : public FormGroup { const string16& number() const { return number_; } const string16& name_on_card() const { return name_on_card_; } - const string16& type() const { return type_; } const string16& last_four_digits() const { return last_four_digits_; } int expiration_month() const { return expiration_month_; } int expiration_year() const { return expiration_year_; } @@ -119,10 +119,6 @@ class CreditCard : public FormGroup { // year. bool Is4DigitExpirationYear(const string16& text) const; - // Returns true if |text| matches the type of the card. The comparison is - // case-insensitive. - bool IsCardType(const string16& text) const; - // Converts |date| to an integer form. Returns true if the conversion // succeeded. bool ConvertDate(const string16& date, int* num) const; |