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/credit_card.h" | 5 #include "chrome/browser/autofill/credit_card.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/string16.h" | 12 #include "base/string16.h" |
| 13 #include "base/string_number_conversions.h" | 13 #include "base/string_number_conversions.h" |
| 14 #include "base/string_split.h" | 14 #include "base/string_split.h" |
| 15 #include "base/string_util.h" | 15 #include "base/string_util.h" |
| 16 #include "base/utf_string_conversions.h" | 16 #include "base/utf_string_conversions.h" |
| 17 #include "chrome/browser/autofill/autofill_country.h" | |
| 17 #include "chrome/browser/autofill/autofill_regexes.h" | 18 #include "chrome/browser/autofill/autofill_regexes.h" |
| 18 #include "chrome/browser/autofill/autofill_type.h" | 19 #include "chrome/browser/autofill/autofill_type.h" |
| 19 #include "chrome/browser/autofill/field_types.h" | 20 #include "chrome/browser/autofill/field_types.h" |
| 20 #include "chrome/common/guid.h" | 21 #include "chrome/common/guid.h" |
| 21 #include "grit/generated_resources.h" | 22 #include "grit/generated_resources.h" |
| 22 #include "ui/base/l10n/l10n_util.h" | 23 #include "ui/base/l10n/l10n_util.h" |
| 24 #include "unicode/dtfmtsym.h" | |
| 25 #include "unicode/uloc.h" | |
| 23 | 26 |
| 24 namespace { | 27 namespace { |
| 25 | 28 |
| 26 const char16 kCreditCardObfuscationSymbol = '*'; | 29 const char16 kCreditCardObfuscationSymbol = '*'; |
| 27 | 30 |
| 28 const AutofillFieldType kAutofillCreditCardTypes[] = { | 31 const AutofillFieldType kAutofillCreditCardTypes[] = { |
| 29 CREDIT_CARD_NAME, | 32 CREDIT_CARD_NAME, |
| 30 CREDIT_CARD_NUMBER, | 33 CREDIT_CARD_NUMBER, |
| 31 CREDIT_CARD_TYPE, | 34 CREDIT_CARD_TYPE, |
| 32 CREDIT_CARD_EXP_MONTH, | 35 CREDIT_CARD_EXP_MONTH, |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 112 case 19: | 115 case 19: |
| 113 if (first_four_digits == 6334 || first_four_digits == 6767) | 116 if (first_four_digits == 6334 || first_four_digits == 6767) |
| 114 return kSoloCard; | 117 return kSoloCard; |
| 115 | 118 |
| 116 break; | 119 break; |
| 117 } | 120 } |
| 118 | 121 |
| 119 return kGenericCard; | 122 return kGenericCard; |
| 120 } | 123 } |
| 121 | 124 |
| 122 bool ConvertDate(const string16& date, int* num) { | 125 bool ConvertDate(const string16& date, bool is_month, int* num) { |
|
dhollowa
2011/06/07 16:33:00
This feels like it is really two separate function
Ilya Sherman
2011/06/08 00:10:12
Done.
| |
| 123 if (!date.empty()) { | 126 // If the |date| is empty, clear the value. |
| 124 bool converted = base::StringToInt(date, num); | 127 if (date.empty()) { |
| 125 DCHECK(converted); | |
| 126 if (!converted) | |
| 127 return false; | |
| 128 } else { | |
| 129 // Clear the value. | |
| 130 *num = 0; | 128 *num = 0; |
| 129 return true; | |
| 131 } | 130 } |
| 132 | 131 |
| 133 return true; | 132 // Try parsing the |date| as a number. |
| 133 if (base::StringToInt(date, num)) | |
| 134 return true; | |
| 135 | |
| 136 // Try parsing the |date| as a named month, e.g. "January" or "Jan". | |
| 137 if (is_month) { | |
| 138 string16 lowercased_date = StringToLowerASCII(date); | |
| 139 | |
| 140 UErrorCode status = U_ZERO_ERROR; | |
| 141 icu::Locale locale(AutofillCountry::ApplicationLocale().c_str()); | |
| 142 icu::DateFormatSymbols date_format_symbols(locale, status); | |
| 143 DCHECK(status == U_ZERO_ERROR || status == U_USING_FALLBACK_WARNING || | |
| 144 status == U_USING_DEFAULT_WARNING); | |
| 145 | |
| 146 int32_t num_months; | |
| 147 const icu::UnicodeString* months = | |
| 148 date_format_symbols.getMonths(num_months); | |
| 149 for (int32_t i = 0; i < num_months; ++i) { | |
| 150 const string16 month = string16(months[i].getBuffer(), | |
| 151 months[i].length()); | |
| 152 if (lowercased_date == StringToLowerASCII(month)) { | |
| 153 *num = i + 1; // Adjust from 0-indexed to 1-indexed. | |
| 154 return true; | |
| 155 } | |
| 156 } | |
| 157 | |
| 158 months = date_format_symbols.getShortMonths(num_months); | |
| 159 for (int32_t i = 0; i < num_months; ++i) { | |
| 160 const string16 month = string16(months[i].getBuffer(), | |
| 161 months[i].length()); | |
| 162 if (lowercased_date == StringToLowerASCII(month)) { | |
| 163 *num = i + 1; // Adjust from 0-indexed to 1-indexed. | |
| 164 return true; | |
| 165 } | |
| 166 } | |
| 167 } | |
| 168 | |
| 169 | |
|
dhollowa
2011/06/07 16:33:00
nit: extra space.
Ilya Sherman
2011/06/08 00:10:12
Done.
| |
| 170 | |
| 171 NOTREACHED(); | |
| 172 *num = 0; | |
| 173 return false; | |
| 134 } | 174 } |
| 135 | 175 |
| 136 } // namespace | 176 } // namespace |
| 137 | 177 |
| 138 CreditCard::CreditCard(const std::string& guid) | 178 CreditCard::CreditCard(const std::string& guid) |
| 139 : type_(kGenericCard), | 179 : type_(kGenericCard), |
| 140 expiration_month_(0), | 180 expiration_month_(0), |
| 141 expiration_year_(0), | 181 expiration_year_(0), |
| 142 guid_(guid) { | 182 guid_(guid) { |
| 143 } | 183 } |
| (...skipping 331 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 475 | 515 |
| 476 string16 CreditCard::Expiration2DigitYearAsString() const { | 516 string16 CreditCard::Expiration2DigitYearAsString() const { |
| 477 if (expiration_year_ == 0) | 517 if (expiration_year_ == 0) |
| 478 return string16(); | 518 return string16(); |
| 479 | 519 |
| 480 return base::IntToString16(Expiration2DigitYear()); | 520 return base::IntToString16(Expiration2DigitYear()); |
| 481 } | 521 } |
| 482 | 522 |
| 483 void CreditCard::SetExpirationMonthFromString(const string16& text) { | 523 void CreditCard::SetExpirationMonthFromString(const string16& text) { |
| 484 int month; | 524 int month; |
| 485 if (!ConvertDate(text, &month)) | 525 if (!ConvertDate(text, true, &month)) |
| 486 return; | 526 return; |
| 487 | 527 |
| 488 SetExpirationMonth(month); | 528 SetExpirationMonth(month); |
| 489 } | 529 } |
| 490 | 530 |
| 491 void CreditCard::SetExpirationYearFromString(const string16& text) { | 531 void CreditCard::SetExpirationYearFromString(const string16& text) { |
| 492 int year; | 532 int year; |
| 493 if (!ConvertDate(text, &year)) | 533 if (!ConvertDate(text, false, &year)) |
| 494 return; | 534 return; |
| 495 | 535 |
| 496 SetExpirationYear(year); | 536 SetExpirationYear(year); |
| 497 } | 537 } |
| 498 | 538 |
| 499 void CreditCard::SetNumber(const string16& number) { | 539 void CreditCard::SetNumber(const string16& number) { |
| 500 number_ = number; | 540 number_ = number; |
| 501 type_ = GetCreditCardType(StripSeparators(number_)); | 541 type_ = GetCreditCardType(StripSeparators(number_)); |
| 502 } | 542 } |
| 503 | 543 |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 571 // send these strings to WK, which then asks WebKitClientImpl to load the image | 611 // send these strings to WK, which then asks WebKitClientImpl to load the image |
| 572 // data. | 612 // data. |
| 573 const char* const kAmericanExpressCard = "americanExpressCC"; | 613 const char* const kAmericanExpressCard = "americanExpressCC"; |
| 574 const char* const kDinersCard = "dinersCC"; | 614 const char* const kDinersCard = "dinersCC"; |
| 575 const char* const kDiscoverCard = "discoverCC"; | 615 const char* const kDiscoverCard = "discoverCC"; |
| 576 const char* const kGenericCard = "genericCC"; | 616 const char* const kGenericCard = "genericCC"; |
| 577 const char* const kJCBCard = "jcbCC"; | 617 const char* const kJCBCard = "jcbCC"; |
| 578 const char* const kMasterCard = "masterCardCC"; | 618 const char* const kMasterCard = "masterCardCC"; |
| 579 const char* const kSoloCard = "soloCC"; | 619 const char* const kSoloCard = "soloCC"; |
| 580 const char* const kVisaCard = "visaCC"; | 620 const char* const kVisaCard = "visaCC"; |
| OLD | NEW |