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

Side by Side Diff: chrome/browser/autofill/credit_card.cc

Issue 11819016: [Autofill] Require callers to pass in locale when parsing named months like "January" (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 11 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 | Annotate | Revision Log
« no previous file with comments | « chrome/browser/autofill/credit_card.h ('k') | chrome/browser/autofill/credit_card_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 8
9 #include <ostream> 9 #include <ostream>
10 #include <string> 10 #include <string>
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
160 } 160 }
161 161
162 // Try parsing the |year| as a number. 162 // Try parsing the |year| as a number.
163 if (base::StringToInt(year, num)) 163 if (base::StringToInt(year, num))
164 return true; 164 return true;
165 165
166 *num = 0; 166 *num = 0;
167 return false; 167 return false;
168 } 168 }
169 169
170 bool ConvertMonth(const string16& month, int* num) { 170 bool ConvertMonth(const string16& month,
171 const std::string& app_locale,
172 int* num) {
171 // If the |month| is empty, clear the stored value. 173 // If the |month| is empty, clear the stored value.
172 if (month.empty()) { 174 if (month.empty()) {
173 *num = 0; 175 *num = 0;
174 return true; 176 return true;
175 } 177 }
176 178
177 // Try parsing the |month| as a number. 179 // Try parsing the |month| as a number.
178 if (base::StringToInt(month, num)) 180 if (base::StringToInt(month, num))
179 return true; 181 return true;
180 182
181 // Try parsing the |month| as a named month, e.g. "January" or "Jan". 183 // If the locale is unknown, give up.
184 if (app_locale.empty())
185 return false;
186
187 // Otherwise, try parsing the |month| as a named month, e.g. "January" or
188 // "Jan".
182 string16 lowercased_month = StringToLowerASCII(month); 189 string16 lowercased_month = StringToLowerASCII(month);
183 190
184 UErrorCode status = U_ZERO_ERROR; 191 UErrorCode status = U_ZERO_ERROR;
185 icu::Locale locale(AutofillCountry::ApplicationLocale().c_str()); 192 icu::Locale locale(app_locale.c_str());
186 icu::DateFormatSymbols date_format_symbols(locale, status); 193 icu::DateFormatSymbols date_format_symbols(locale, status);
187 DCHECK(status == U_ZERO_ERROR || status == U_USING_FALLBACK_WARNING || 194 DCHECK(status == U_ZERO_ERROR || status == U_USING_FALLBACK_WARNING ||
188 status == U_USING_DEFAULT_WARNING); 195 status == U_USING_DEFAULT_WARNING);
189 196
190 int32_t num_months; 197 int32_t num_months;
191 const icu::UnicodeString* months = date_format_symbols.getMonths(num_months); 198 const icu::UnicodeString* months = date_format_symbols.getMonths(num_months);
192 for (int32_t i = 0; i < num_months; ++i) { 199 for (int32_t i = 0; i < num_months; ++i) {
193 const string16 icu_month = string16(months[i].getBuffer(), 200 const string16 icu_month = string16(months[i].getBuffer(),
194 months[i].length()); 201 months[i].length());
195 if (lowercased_month == StringToLowerASCII(icu_month)) { 202 if (lowercased_month == StringToLowerASCII(icu_month)) {
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
284 } 291 }
285 } 292 }
286 293
287 void CreditCard::SetRawInfo(AutofillFieldType type, const string16& value) { 294 void CreditCard::SetRawInfo(AutofillFieldType type, const string16& value) {
288 switch (type) { 295 switch (type) {
289 case CREDIT_CARD_NAME: 296 case CREDIT_CARD_NAME:
290 name_on_card_ = value; 297 name_on_card_ = value;
291 break; 298 break;
292 299
293 case CREDIT_CARD_EXP_MONTH: 300 case CREDIT_CARD_EXP_MONTH:
294 SetExpirationMonthFromString(value); 301 SetExpirationMonthFromString(value, std::string());
295 break; 302 break;
296 303
297 case CREDIT_CARD_EXP_2_DIGIT_YEAR: 304 case CREDIT_CARD_EXP_2_DIGIT_YEAR:
298 // This is a read-only attribute. 305 // This is a read-only attribute.
299 break; 306 break;
300 307
301 case CREDIT_CARD_EXP_4_DIGIT_YEAR: 308 case CREDIT_CARD_EXP_4_DIGIT_YEAR:
302 SetExpirationYearFromString(value); 309 SetExpirationYearFromString(value);
303 break; 310 break;
304 311
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
337 return StripSeparators(number_); 344 return StripSeparators(number_);
338 345
339 return GetRawInfo(type); 346 return GetRawInfo(type);
340 } 347 }
341 348
342 bool CreditCard::SetInfo(AutofillFieldType type, 349 bool CreditCard::SetInfo(AutofillFieldType type,
343 const string16& value, 350 const string16& value,
344 const std::string& app_locale) { 351 const std::string& app_locale) {
345 if (type == CREDIT_CARD_NUMBER) 352 if (type == CREDIT_CARD_NUMBER)
346 SetRawInfo(type, StripSeparators(value)); 353 SetRawInfo(type, StripSeparators(value));
354 else if (type == CREDIT_CARD_EXP_MONTH)
355 SetExpirationMonthFromString(value, app_locale);
347 else 356 else
348 SetRawInfo(type, value); 357 SetRawInfo(type, value);
349 358
350 return true; 359 return true;
351 } 360 }
352 361
353 void CreditCard::GetMatchingTypes(const string16& text, 362 void CreditCard::GetMatchingTypes(const string16& text,
354 const std::string& app_locale, 363 const std::string& app_locale,
355 FieldTypeSet* matching_types) const { 364 FieldTypeSet* matching_types) const {
356 FormGroup::GetMatchingTypes(text, app_locale, matching_types); 365 FormGroup::GetMatchingTypes(text, app_locale, matching_types);
357 366
358 string16 card_number = GetInfo(CREDIT_CARD_NUMBER, app_locale); 367 string16 card_number = GetInfo(CREDIT_CARD_NUMBER, app_locale);
359 if (!card_number.empty() && StripSeparators(text) == card_number) 368 if (!card_number.empty() && StripSeparators(text) == card_number)
360 matching_types->insert(CREDIT_CARD_NUMBER); 369 matching_types->insert(CREDIT_CARD_NUMBER);
361 370
362 int month; 371 int month;
363 if (ConvertMonth(text, &month) && month != 0 && month == expiration_month_) 372 if (ConvertMonth(text, app_locale, &month) && month != 0 &&
373 month == expiration_month_) {
364 matching_types->insert(CREDIT_CARD_EXP_MONTH); 374 matching_types->insert(CREDIT_CARD_EXP_MONTH);
375 }
365 } 376 }
366 377
367 const string16 CreditCard::Label() const { 378 const string16 CreditCard::Label() const {
368 string16 label; 379 string16 label;
369 if (number().empty()) 380 if (number().empty())
370 return name_on_card_; // No CC number, return name only. 381 return name_on_card_; // No CC number, return name only.
371 382
372 string16 obfuscated_cc_number = ObfuscatedNumber(); 383 string16 obfuscated_cc_number = ObfuscatedNumber();
373 if (!expiration_month_ || !expiration_year_) 384 if (!expiration_month_ || !expiration_year_)
374 return obfuscated_cc_number; // No expiration date set. 385 return obfuscated_cc_number; // No expiration date set.
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after
590 return base::IntToString16(Expiration4DigitYear()); 601 return base::IntToString16(Expiration4DigitYear());
591 } 602 }
592 603
593 string16 CreditCard::Expiration2DigitYearAsString() const { 604 string16 CreditCard::Expiration2DigitYearAsString() const {
594 if (expiration_year_ == 0) 605 if (expiration_year_ == 0)
595 return string16(); 606 return string16();
596 607
597 return base::IntToString16(Expiration2DigitYear()); 608 return base::IntToString16(Expiration2DigitYear());
598 } 609 }
599 610
600 void CreditCard::SetExpirationMonthFromString(const string16& text) { 611 void CreditCard::SetExpirationMonthFromString(const string16& text,
612 const std::string& app_locale) {
601 int month; 613 int month;
602 if (!ConvertMonth(text, &month)) 614 if (!ConvertMonth(text, app_locale, &month))
603 return; 615 return;
604 616
605 SetExpirationMonth(month); 617 SetExpirationMonth(month);
606 } 618 }
607 619
608 void CreditCard::SetExpirationYearFromString(const string16& text) { 620 void CreditCard::SetExpirationYearFromString(const string16& text) {
609 int year; 621 int year;
610 if (!ConvertYear(text, &year)) 622 if (!ConvertYear(text, &year))
611 return; 623 return;
612 624
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
656 // webkit/glue. We send these strings to WebKit, which then asks 668 // webkit/glue. We send these strings to WebKit, which then asks
657 // WebKitPlatformSupportImpl to load the image data. 669 // WebKitPlatformSupportImpl to load the image data.
658 const char* const kAmericanExpressCard = "americanExpressCC"; 670 const char* const kAmericanExpressCard = "americanExpressCC";
659 const char* const kDinersCard = "dinersCC"; 671 const char* const kDinersCard = "dinersCC";
660 const char* const kDiscoverCard = "discoverCC"; 672 const char* const kDiscoverCard = "discoverCC";
661 const char* const kGenericCard = "genericCC"; 673 const char* const kGenericCard = "genericCC";
662 const char* const kJCBCard = "jcbCC"; 674 const char* const kJCBCard = "jcbCC";
663 const char* const kMasterCard = "masterCardCC"; 675 const char* const kMasterCard = "masterCardCC";
664 const char* const kSoloCard = "soloCC"; 676 const char* const kSoloCard = "soloCC";
665 const char* const kVisaCard = "visaCC"; 677 const char* const kVisaCard = "visaCC";
OLDNEW
« no previous file with comments | « chrome/browser/autofill/credit_card.h ('k') | chrome/browser/autofill/credit_card_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698