Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(253)

Side by Side Diff: components/autofill/core/browser/validation.cc

Issue 2673753005: [Payments] Basic validation in the credit card editor. (Closed)
Patch Set: more tests Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/credit_card.h" 14 #include "components/autofill/core/browser/credit_card.h"
15 #include "components/autofill/core/browser/state_names.h" 15 #include "components/autofill/core/browser/state_names.h"
16 #include "components/autofill/core/common/autofill_clock.h"
16 #include "components/autofill/core/common/autofill_regexes.h" 17 #include "components/autofill/core/common/autofill_regexes.h"
18 #include "grit/components_strings.h"
19 #include "ui/base/l10n/l10n_util.h"
17 20
18 namespace autofill { 21 namespace autofill {
19 22
20 bool IsValidCreditCardExpirationDate(int year, 23 bool IsValidCreditCardExpirationDate(int year,
21 int month, 24 int month,
22 const base::Time& now) { 25 const base::Time& now) {
23 if (month < 1 || month > 12) 26 if (month < 1 || month > 12)
24 return false; 27 return false;
25 28
26 base::Time::Exploded now_exploded; 29 base::Time::Exploded now_exploded;
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
167 if (!base::StringToInt(base::StringPiece16(number_string.begin() + 5, 170 if (!base::StringToInt(base::StringPiece16(number_string.begin() + 5,
168 number_string.begin() + 9), 171 number_string.begin() + 9),
169 &serial) 172 &serial)
170 || serial == 0) { 173 || serial == 0) {
171 return false; 174 return false;
172 } 175 }
173 176
174 return true; 177 return true;
175 } 178 }
176 179
180 bool IsValidForType(const base::string16& value,
181 ServerFieldType type,
182 base::string16* error_message) {
183 switch (type) {
184 case CREDIT_CARD_NAME_FULL:
185 if (!value.empty())
186 return true;
187
188 if (error_message) {
189 *error_message =
190 l10n_util::GetStringUTF16(IDS_AUTOFILL_VALIDATION_INVALID_NAME);
191 }
192 break;
193
194 case CREDIT_CARD_EXP_MONTH: {
195 CreditCard temp;
196 // Expiration month was in an invalid format.
197 temp.SetExpirationMonthFromString(value, /* app_locale= */ std::string());
198 if (temp.expiration_month() == 0) {
199 if (error_message) {
200 *error_message = l10n_util::GetStringUTF16(
201 IDS_AUTOFILL_VALIDATION_INVALID_CREDIT_CARD_EXPIRATION_MONTH);
202 }
203 break;
204 }
205 return true;
206 }
207
208 case CREDIT_CARD_EXP_2_DIGIT_YEAR:
209 case CREDIT_CARD_EXP_4_DIGIT_YEAR: {
210 CreditCard temp;
211 temp.SetExpirationYearFromString(value);
212 // Expiration year was in an invalid format.
213 if ((temp.expiration_year() == 0) ||
214 (type == CREDIT_CARD_EXP_2_DIGIT_YEAR && value.size() != 2u) ||
215 (type == CREDIT_CARD_EXP_4_DIGIT_YEAR && value.size() != 4u)) {
216 if (error_message) {
217 *error_message = l10n_util::GetStringUTF16(
218 IDS_AUTOFILL_VALIDATION_INVALID_CREDIT_CARD_EXPIRATION_YEAR);
219 }
220 break;
221 }
222
223 base::Time::Exploded now_exploded;
224 AutofillClock::Now().LocalExplode(&now_exploded);
225 if (temp.expiration_year() >= now_exploded.year)
226 return true;
227
228 // If the year is before this year, it's expired.
229 if (error_message) {
230 *error_message = l10n_util::GetStringUTF16(
231 IDS_AUTOFILL_VALIDATION_INVALID_CREDIT_CARD_EXPIRED);
232 }
233 break;
234 }
235
236 case CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR:
237 case CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR: {
238 const base::string16 pattern =
239 type == CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR
240 ? base::UTF8ToUTF16("^[0-9]{1,2}[-/|]?[0-9]{2}$")
241 : base::UTF8ToUTF16("^[0-9]{1,2}[-/|]?[0-9]{4}$");
242
243 CreditCard temp;
244 temp.SetExpirationDateFromString(value);
245
246 // Expiration date was in an invalid format.
247 if (temp.expiration_month() == 0 || temp.expiration_year() == 0 ||
248 !MatchesPattern(value, pattern)) {
249 if (error_message) {
250 *error_message = l10n_util::GetStringUTF16(
251 IDS_AUTOFILL_VALIDATION_INVALID_CREDIT_CARD_EXPIRATION_DATE);
252 }
253 break;
254 }
255
256 // Checking for card expiration.
257 if (IsValidCreditCardExpirationDate(temp.expiration_year(),
258 temp.expiration_month(),
259 AutofillClock::Now())) {
260 return true;
261 }
262
263 if (error_message) {
264 *error_message = l10n_util::GetStringUTF16(
265 IDS_AUTOFILL_VALIDATION_INVALID_CREDIT_CARD_EXPIRED);
266 }
267 break;
268 }
269
270 case CREDIT_CARD_NUMBER:
271 if (IsValidCreditCardNumber(value))
272 return true;
273
274 if (error_message)
275 *error_message = l10n_util::GetStringUTF16(
276 IDS_AUTOFILL_VALIDATION_INVALID_CREDIT_CARD_NUMBER);
277 break;
278
279 default:
280 // Other types such as CREDIT_CARD_TYPE and CREDIT_CARD_VERIFICATION_CODE
281 // are not validated for now.
282 NOTREACHED() << "Attempting to validate unsupported type " << type;
283 break;
284 }
285 return false;
286 }
287
177 } // namespace autofill 288 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698