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.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> |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 if (type == kAmericanExpressCard) | 143 if (type == kAmericanExpressCard) |
144 return l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_AMEX); | 144 return l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_AMEX); |
145 if (type == kDinersCard) | 145 if (type == kDinersCard) |
146 return l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_DINERS); | 146 return l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_DINERS); |
147 if (type == kDiscoverCard) | 147 if (type == kDiscoverCard) |
148 return l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_DISCOVER); | 148 return l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_DISCOVER); |
149 if (type == kJCBCard) | 149 if (type == kJCBCard) |
150 return l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_JCB); | 150 return l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_JCB); |
151 if (type == kMasterCard) | 151 if (type == kMasterCard) |
152 return l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_MASTERCARD); | 152 return l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_MASTERCARD); |
153 if (type == kSoloCard) | |
154 return l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_SOLO); | |
155 if (type == kVisaCard) | 153 if (type == kVisaCard) |
156 return l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_VISA); | 154 return l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_VISA); |
157 | 155 |
158 // If you hit this DCHECK, the above list of cases needs to be updated to | 156 // If you hit this DCHECK, the above list of cases needs to be updated to |
159 // include a new card. | 157 // include a new card. |
160 DCHECK_EQ(kGenericCard, type); | 158 DCHECK_EQ(kGenericCard, type); |
161 return base::string16(); | 159 return base::string16(); |
162 } | 160 } |
163 | 161 |
164 // static | 162 // static |
165 int CreditCard::IconResourceId(const std::string& type) { | 163 int CreditCard::IconResourceId(const std::string& type) { |
166 if (type == kAmericanExpressCard) | 164 if (type == kAmericanExpressCard) |
167 return IDR_AUTOFILL_CC_AMEX; | 165 return IDR_AUTOFILL_CC_AMEX; |
168 if (type == kDinersCard) | 166 if (type == kDinersCard) |
169 return IDR_AUTOFILL_CC_DINERS; | 167 return IDR_AUTOFILL_CC_DINERS; |
170 if (type == kDiscoverCard) | 168 if (type == kDiscoverCard) |
171 return IDR_AUTOFILL_CC_DISCOVER; | 169 return IDR_AUTOFILL_CC_DISCOVER; |
172 if (type == kJCBCard) | 170 if (type == kJCBCard) |
173 return IDR_AUTOFILL_CC_JCB; | 171 return IDR_AUTOFILL_CC_JCB; |
174 if (type == kMasterCard) | 172 if (type == kMasterCard) |
175 return IDR_AUTOFILL_CC_MASTERCARD; | 173 return IDR_AUTOFILL_CC_MASTERCARD; |
176 if (type == kSoloCard) | |
177 return IDR_AUTOFILL_CC_SOLO; | |
178 if (type == kVisaCard) | 174 if (type == kVisaCard) |
179 return IDR_AUTOFILL_CC_VISA; | 175 return IDR_AUTOFILL_CC_VISA; |
180 | 176 |
181 // If you hit this DCHECK, the above list of cases needs to be updated to | 177 // If you hit this DCHECK, the above list of cases needs to be updated to |
182 // include a new card. | 178 // include a new card. |
183 DCHECK_EQ(kGenericCard, type); | 179 DCHECK_EQ(kGenericCard, type); |
184 return IDR_AUTOFILL_CC_GENERIC; | 180 return IDR_AUTOFILL_CC_GENERIC; |
185 } | 181 } |
186 | 182 |
187 // static | 183 // static |
188 std::string CreditCard::GetCreditCardType(const base::string16& number) { | 184 std::string CreditCard::GetCreditCardType(const base::string16& number) { |
189 // Don't check for a specific type if this is not a credit card number. | |
190 if (!autofill::IsValidCreditCardNumber(number)) | |
191 return kGenericCard; | |
192 | |
193 // Credit card number specifications taken from: | 185 // Credit card number specifications taken from: |
194 // http://en.wikipedia.org/wiki/Credit_card_numbers and | 186 // http://en.wikipedia.org/wiki/Credit_card_numbers, |
| 187 // http://www.discovernetwork.com/merchants/images/Merchant_Marketing_PDF.pdf, |
| 188 // http://www.regular-expressions.info/creditcard.html, and |
195 // http://www.beachnet.com/~hstiles/cardtype.html | 189 // http://www.beachnet.com/~hstiles/cardtype.html |
| 190 // |
| 191 // The last site is currently unavailable, but a cached version remains at |
| 192 // http://web.archive.org/web/20120923111349/http://www.beachnet.com/~hstiles/
cardtype.html |
| 193 // |
196 // Card Type Prefix(es) Length | 194 // Card Type Prefix(es) Length |
197 // --------------------------------------------------------------- | 195 // --------------------------------------------------------------- |
198 // Visa 4 13,16 | 196 // Visa 4 13,16 |
199 // American Express 34,37 15 | 197 // American Express 34,37 15 |
200 // Diners Club 300-305,2014,2149,36, 14,15 | 198 // Diners Club 300-305,3095,36,38-39 14 |
201 // Discover Card 6011,65 16 | 199 // Discover Card 6011,644-649,65 16 |
202 // JCB 3 16 | 200 // JCB 3528-3589 16 |
203 // JCB 2131,1800 15 | |
204 // MasterCard 51-55 16 | 201 // MasterCard 51-55 16 |
205 // Solo (debit card) 6334,6767 16,18,19 | |
206 | 202 |
207 // We need at least 4 digits to work with. | 203 // Check for prefixes of length 1. |
208 if (number.length() < 4) | 204 if (number.empty()) |
| 205 return kGenericCard; |
| 206 |
| 207 if (number[0] == '4') |
| 208 return kVisaCard; |
| 209 |
| 210 // Check for prefixes of length 2. |
| 211 if (number.size() < 2) |
| 212 return kGenericCard; |
| 213 |
| 214 int first_two_digits = 0; |
| 215 if (!base::StringToInt(number.substr(0, 2), &first_two_digits)) |
| 216 return kGenericCard; |
| 217 |
| 218 if (first_two_digits == 34 || first_two_digits == 37) |
| 219 return kAmericanExpressCard; |
| 220 |
| 221 if (first_two_digits == 36 || |
| 222 first_two_digits == 38 || |
| 223 first_two_digits == 39) |
| 224 return kDinersCard; |
| 225 |
| 226 if (first_two_digits >= 51 && first_two_digits <= 55) |
| 227 return kMasterCard; |
| 228 |
| 229 if (first_two_digits == 65) |
| 230 return kDiscoverCard; |
| 231 |
| 232 // Check for prefixes of length 3. |
| 233 if (number.size() < 3) |
| 234 return kGenericCard; |
| 235 |
| 236 int first_three_digits = 0; |
| 237 if (!base::StringToInt(number.substr(0, 3), &first_three_digits)) |
| 238 return kGenericCard; |
| 239 |
| 240 if (first_three_digits >= 300 && first_three_digits <= 305) |
| 241 return kDinersCard; |
| 242 |
| 243 if (first_three_digits >= 644 && first_three_digits <= 649) |
| 244 return kDiscoverCard; |
| 245 |
| 246 // Check for prefixes of length 4. |
| 247 if (number.size() < 4) |
209 return kGenericCard; | 248 return kGenericCard; |
210 | 249 |
211 int first_four_digits = 0; | 250 int first_four_digits = 0; |
212 if (!base::StringToInt(number.substr(0, 4), &first_four_digits)) | 251 if (!base::StringToInt(number.substr(0, 4), &first_four_digits)) |
213 return kGenericCard; | 252 return kGenericCard; |
214 | 253 |
215 int first_three_digits = first_four_digits / 10; | 254 if (first_four_digits == 3095) |
216 int first_two_digits = first_three_digits / 10; | 255 return kDinersCard; |
217 int first_digit = first_two_digits / 10; | |
218 | 256 |
219 switch (number.length()) { | 257 if (first_four_digits >= 3528 && first_four_digits <= 3589) |
220 case 13: | 258 return kJCBCard; |
221 if (first_digit == 4) | |
222 return kVisaCard; | |
223 | 259 |
224 break; | 260 if (first_four_digits == 6011) |
225 case 14: | 261 return kDiscoverCard; |
226 if (first_three_digits >= 300 && first_three_digits <= 305) | |
227 return kDinersCard; | |
228 | |
229 if (first_digit == 36) | |
230 return kDinersCard; | |
231 | |
232 break; | |
233 case 15: | |
234 if (first_two_digits == 34 || first_two_digits == 37) | |
235 return kAmericanExpressCard; | |
236 | |
237 if (first_four_digits == 2131 || first_four_digits == 1800) | |
238 return kJCBCard; | |
239 | |
240 if (first_four_digits == 2014 || first_four_digits == 2149) | |
241 return kDinersCard; | |
242 | |
243 break; | |
244 case 16: | |
245 if (first_four_digits == 6011 || first_two_digits == 65) | |
246 return kDiscoverCard; | |
247 | |
248 if (first_four_digits == 6334 || first_four_digits == 6767) | |
249 return kSoloCard; | |
250 | |
251 if (first_two_digits >= 51 && first_two_digits <= 55) | |
252 return kMasterCard; | |
253 | |
254 if (first_digit == 3) | |
255 return kJCBCard; | |
256 | |
257 if (first_digit == 4) | |
258 return kVisaCard; | |
259 | |
260 break; | |
261 case 18: | |
262 case 19: | |
263 if (first_four_digits == 6334 || first_four_digits == 6767) | |
264 return kSoloCard; | |
265 | |
266 break; | |
267 } | |
268 | 262 |
269 return kGenericCard; | 263 return kGenericCard; |
270 } | 264 } |
271 | 265 |
272 base::string16 CreditCard::GetRawInfo(AutofillFieldType type) const { | 266 base::string16 CreditCard::GetRawInfo(AutofillFieldType type) const { |
273 switch (type) { | 267 switch (type) { |
274 case CREDIT_CARD_NAME: | 268 case CREDIT_CARD_NAME: |
275 return name_on_card_; | 269 return name_on_card_; |
276 | 270 |
277 case CREDIT_CARD_EXP_MONTH: | 271 case CREDIT_CARD_EXP_MONTH: |
(...skipping 406 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
684 | 678 |
685 // These values must match the values in WebKitPlatformSupportImpl in | 679 // These values must match the values in WebKitPlatformSupportImpl in |
686 // webkit/glue. We send these strings to WebKit, which then asks | 680 // webkit/glue. We send these strings to WebKit, which then asks |
687 // WebKitPlatformSupportImpl to load the image data. | 681 // WebKitPlatformSupportImpl to load the image data. |
688 const char* const kAmericanExpressCard = "americanExpressCC"; | 682 const char* const kAmericanExpressCard = "americanExpressCC"; |
689 const char* const kDinersCard = "dinersCC"; | 683 const char* const kDinersCard = "dinersCC"; |
690 const char* const kDiscoverCard = "discoverCC"; | 684 const char* const kDiscoverCard = "discoverCC"; |
691 const char* const kGenericCard = "genericCC"; | 685 const char* const kGenericCard = "genericCC"; |
692 const char* const kJCBCard = "jcbCC"; | 686 const char* const kJCBCard = "jcbCC"; |
693 const char* const kMasterCard = "masterCardCC"; | 687 const char* const kMasterCard = "masterCardCC"; |
694 const char* const kSoloCard = "soloCC"; | |
695 const char* const kVisaCard = "visaCC"; | 688 const char* const kVisaCard = "visaCC"; |
696 | 689 |
697 } // namespace autofill | 690 } // namespace autofill |
OLD | NEW |