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