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/validation.h" | 5 #include "components/autofill/core/browser/validation.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
| 9 #include "base/logging.h" |
9 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
10 #include "base/strings/string_piece.h" | 11 #include "base/strings/string_piece.h" |
11 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
12 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
13 #include "base/time/time.h" | 14 #include "base/time/time.h" |
| 15 #include "components/autofill/core/browser/autofill_data_util.h" |
14 #include "components/autofill/core/browser/credit_card.h" | 16 #include "components/autofill/core/browser/credit_card.h" |
15 #include "components/autofill/core/browser/state_names.h" | 17 #include "components/autofill/core/browser/state_names.h" |
16 #include "components/autofill/core/common/autofill_clock.h" | 18 #include "components/autofill/core/common/autofill_clock.h" |
17 #include "components/autofill/core/common/autofill_regexes.h" | 19 #include "components/autofill/core/common/autofill_regexes.h" |
18 #include "grit/components_strings.h" | 20 #include "grit/components_strings.h" |
19 #include "ui/base/l10n/l10n_util.h" | 21 #include "ui/base/l10n/l10n_util.h" |
20 | 22 |
21 namespace autofill { | 23 namespace autofill { |
22 | 24 |
23 bool IsValidCreditCardExpirationDate(int year, | 25 bool IsValidCreditCardExpirationDate(int year, |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 return (sum % 10) == 0; | 92 return (sum % 10) == 0; |
91 } | 93 } |
92 | 94 |
93 bool IsValidCreditCardSecurityCode(const base::string16& code, | 95 bool IsValidCreditCardSecurityCode(const base::string16& code, |
94 const base::StringPiece card_type) { | 96 const base::StringPiece card_type) { |
95 size_t required_length = card_type == kAmericanExpressCard ? 4 : 3; | 97 size_t required_length = card_type == kAmericanExpressCard ? 4 : 3; |
96 return code.length() == required_length && | 98 return code.length() == required_length && |
97 base::ContainsOnlyChars(code, base::ASCIIToUTF16("0123456789")); | 99 base::ContainsOnlyChars(code, base::ASCIIToUTF16("0123456789")); |
98 } | 100 } |
99 | 101 |
| 102 bool IsValidCreditCardNumberForBasicCardNetworks( |
| 103 const base::string16& text, |
| 104 const std::set<std::string>& supported_basic_card_networks, |
| 105 base::string16* error_message) { |
| 106 DCHECK(error_message); |
| 107 |
| 108 // The type check is cheaper than the credit card number check. |
| 109 const std::string basic_card_payment_type = |
| 110 autofill::data_util::GetPaymentRequestData( |
| 111 CreditCard::GetCreditCardType(text)) |
| 112 .basic_card_payment_type; |
| 113 if (!supported_basic_card_networks.count(basic_card_payment_type)) { |
| 114 *error_message = l10n_util::GetStringUTF16( |
| 115 IDS_PAYMENTS_VALIDATION_UNSUPPORTED_CREDIT_CARD_TYPE); |
| 116 return false; |
| 117 } |
| 118 |
| 119 if (IsValidCreditCardNumber(text)) |
| 120 return true; |
| 121 |
| 122 *error_message = l10n_util::GetStringUTF16( |
| 123 IDS_PAYMENTS_CARD_NUMBER_INVALID_VALIDATION_MESSAGE); |
| 124 return false; |
| 125 } |
| 126 |
100 bool IsValidEmailAddress(const base::string16& text) { | 127 bool IsValidEmailAddress(const base::string16& text) { |
101 // E-Mail pattern as defined by the WhatWG. (4.10.7.1.5 E-Mail state) | 128 // E-Mail pattern as defined by the WhatWG. (4.10.7.1.5 E-Mail state) |
102 const base::string16 kEmailPattern = base::ASCIIToUTF16( | 129 const base::string16 kEmailPattern = base::ASCIIToUTF16( |
103 "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@" | 130 "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@" |
104 "[a-zA-Z0-9-]+(?:\\.[a-zA-Z0-9-]+)*$"); | 131 "[a-zA-Z0-9-]+(?:\\.[a-zA-Z0-9-]+)*$"); |
105 return MatchesPattern(text, kEmailPattern); | 132 return MatchesPattern(text, kEmailPattern); |
106 } | 133 } |
107 | 134 |
108 bool IsValidState(const base::string16& text) { | 135 bool IsValidState(const base::string16& text) { |
109 return !state_names::GetAbbreviationForName(text).empty() || | 136 return !state_names::GetAbbreviationForName(text).empty() || |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
261 } | 288 } |
262 | 289 |
263 if (error_message) { | 290 if (error_message) { |
264 *error_message = l10n_util::GetStringUTF16( | 291 *error_message = l10n_util::GetStringUTF16( |
265 IDS_PAYMENTS_VALIDATION_INVALID_CREDIT_CARD_EXPIRED); | 292 IDS_PAYMENTS_VALIDATION_INVALID_CREDIT_CARD_EXPIRED); |
266 } | 293 } |
267 break; | 294 break; |
268 } | 295 } |
269 | 296 |
270 case CREDIT_CARD_NUMBER: | 297 case CREDIT_CARD_NUMBER: |
271 if (IsValidCreditCardNumber(value)) | 298 NOTREACHED() << "IsValidCreditCardNumberForBasicCardNetworks should be " |
272 return true; | 299 << "used to validate credit card numbers"; |
273 | |
274 if (error_message) { | |
275 *error_message = l10n_util::GetStringUTF16( | |
276 IDS_PAYMENTS_CARD_NUMBER_INVALID_VALIDATION_MESSAGE); | |
277 } | |
278 break; | 300 break; |
279 | 301 |
280 default: | 302 default: |
281 // Other types such as CREDIT_CARD_TYPE and CREDIT_CARD_VERIFICATION_CODE | 303 // Other types such as CREDIT_CARD_TYPE and CREDIT_CARD_VERIFICATION_CODE |
282 // are not validated for now. | 304 // are not validated for now. |
283 NOTREACHED() << "Attempting to validate unsupported type " << type; | 305 NOTREACHED() << "Attempting to validate unsupported type " << type; |
284 break; | 306 break; |
285 } | 307 } |
286 return false; | 308 return false; |
287 } | 309 } |
288 | 310 |
289 } // namespace autofill | 311 } // namespace autofill |
OLD | NEW |