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

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

Issue 7125004: Support named months for Autofill implicit learning (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: From one, two Created 9 years, 6 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 | « no previous file | 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) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 #include <string> 8 #include <string>
9 9
10 #include "base/basictypes.h" 10 #include "base/basictypes.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/string16.h" 12 #include "base/string16.h"
13 #include "base/string_number_conversions.h" 13 #include "base/string_number_conversions.h"
14 #include "base/string_split.h" 14 #include "base/string_split.h"
15 #include "base/string_util.h" 15 #include "base/string_util.h"
16 #include "base/utf_string_conversions.h" 16 #include "base/utf_string_conversions.h"
17 #include "chrome/browser/autofill/autofill_country.h"
17 #include "chrome/browser/autofill/autofill_regexes.h" 18 #include "chrome/browser/autofill/autofill_regexes.h"
18 #include "chrome/browser/autofill/autofill_type.h" 19 #include "chrome/browser/autofill/autofill_type.h"
19 #include "chrome/browser/autofill/field_types.h" 20 #include "chrome/browser/autofill/field_types.h"
20 #include "chrome/common/guid.h" 21 #include "chrome/common/guid.h"
21 #include "grit/generated_resources.h" 22 #include "grit/generated_resources.h"
22 #include "ui/base/l10n/l10n_util.h" 23 #include "ui/base/l10n/l10n_util.h"
24 #include "unicode/dtfmtsym.h"
25 #include "unicode/uloc.h"
23 26
24 namespace { 27 namespace {
25 28
26 const char16 kCreditCardObfuscationSymbol = '*'; 29 const char16 kCreditCardObfuscationSymbol = '*';
27 30
28 const AutofillFieldType kAutofillCreditCardTypes[] = { 31 const AutofillFieldType kAutofillCreditCardTypes[] = {
29 CREDIT_CARD_NAME, 32 CREDIT_CARD_NAME,
30 CREDIT_CARD_NUMBER, 33 CREDIT_CARD_NUMBER,
31 CREDIT_CARD_TYPE, 34 CREDIT_CARD_TYPE,
32 CREDIT_CARD_EXP_MONTH, 35 CREDIT_CARD_EXP_MONTH,
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
112 case 19: 115 case 19:
113 if (first_four_digits == 6334 || first_four_digits == 6767) 116 if (first_four_digits == 6334 || first_four_digits == 6767)
114 return kSoloCard; 117 return kSoloCard;
115 118
116 break; 119 break;
117 } 120 }
118 121
119 return kGenericCard; 122 return kGenericCard;
120 } 123 }
121 124
122 bool ConvertDate(const string16& date, int* num) { 125 bool ConvertYear(const string16& year, int* num) {
123 if (!date.empty()) { 126 // If the |year| is empty, clear the stored value.
124 bool converted = base::StringToInt(date, num); 127 if (year.empty()) {
125 DCHECK(converted);
126 if (!converted)
127 return false;
128 } else {
129 // Clear the value.
130 *num = 0; 128 *num = 0;
129 return true;
131 } 130 }
132 131
133 return true; 132 // Try parsing the |year| as a number.
133 if (base::StringToInt(year, num))
134 return true;
135
136 NOTREACHED();
137 *num = 0;
138 return false;
139 }
140
141 bool ConvertMonth(const string16& month, int* num) {
142 // If the |month| is empty, clear the stored value.
143 if (month.empty()) {
144 *num = 0;
145 return true;
146 }
147
148 // Try parsing the |month| as a number.
149 if (base::StringToInt(month, num))
150 return true;
151
152 // Try parsing the |month| as a named month, e.g. "January" or "Jan".
153 string16 lowercased_month = StringToLowerASCII(month);
154
155 UErrorCode status = U_ZERO_ERROR;
156 icu::Locale locale(AutofillCountry::ApplicationLocale().c_str());
157 icu::DateFormatSymbols date_format_symbols(locale, status);
158 DCHECK(status == U_ZERO_ERROR || status == U_USING_FALLBACK_WARNING ||
159 status == U_USING_DEFAULT_WARNING);
160
161 int32_t num_months;
162 const icu::UnicodeString* months = date_format_symbols.getMonths(num_months);
163 for (int32_t i = 0; i < num_months; ++i) {
164 const string16 icu_month = string16(months[i].getBuffer(),
165 months[i].length());
166 if (lowercased_month == StringToLowerASCII(icu_month)) {
167 *num = i + 1; // Adjust from 0-indexed to 1-indexed.
168 return true;
169 }
170 }
171
172 months = date_format_symbols.getShortMonths(num_months);
173 for (int32_t i = 0; i < num_months; ++i) {
174 const string16 icu_month = string16(months[i].getBuffer(),
175 months[i].length());
176 if (lowercased_month == StringToLowerASCII(icu_month)) {
177 *num = i + 1; // Adjust from 0-indexed to 1-indexed.
178 return true;
179 }
180 }
181
182 NOTREACHED();
183 *num = 0;
184 return false;
134 } 185 }
135 186
136 } // namespace 187 } // namespace
137 188
138 CreditCard::CreditCard(const std::string& guid) 189 CreditCard::CreditCard(const std::string& guid)
139 : type_(kGenericCard), 190 : type_(kGenericCard),
140 expiration_month_(0), 191 expiration_month_(0),
141 expiration_year_(0), 192 expiration_year_(0),
142 guid_(guid) { 193 guid_(guid) {
143 } 194 }
(...skipping 331 matching lines...) Expand 10 before | Expand all | Expand 10 after
475 526
476 string16 CreditCard::Expiration2DigitYearAsString() const { 527 string16 CreditCard::Expiration2DigitYearAsString() const {
477 if (expiration_year_ == 0) 528 if (expiration_year_ == 0)
478 return string16(); 529 return string16();
479 530
480 return base::IntToString16(Expiration2DigitYear()); 531 return base::IntToString16(Expiration2DigitYear());
481 } 532 }
482 533
483 void CreditCard::SetExpirationMonthFromString(const string16& text) { 534 void CreditCard::SetExpirationMonthFromString(const string16& text) {
484 int month; 535 int month;
485 if (!ConvertDate(text, &month)) 536 if (!ConvertMonth(text, &month))
486 return; 537 return;
487 538
488 SetExpirationMonth(month); 539 SetExpirationMonth(month);
489 } 540 }
490 541
491 void CreditCard::SetExpirationYearFromString(const string16& text) { 542 void CreditCard::SetExpirationYearFromString(const string16& text) {
492 int year; 543 int year;
493 if (!ConvertDate(text, &year)) 544 if (!ConvertYear(text, &year))
494 return; 545 return;
495 546
496 SetExpirationYear(year); 547 SetExpirationYear(year);
497 } 548 }
498 549
499 void CreditCard::SetNumber(const string16& number) { 550 void CreditCard::SetNumber(const string16& number) {
500 number_ = number; 551 number_ = number;
501 type_ = GetCreditCardType(StripSeparators(number_)); 552 type_ = GetCreditCardType(StripSeparators(number_));
502 } 553 }
503 554
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
571 // send these strings to WK, which then asks WebKitClientImpl to load the image 622 // send these strings to WK, which then asks WebKitClientImpl to load the image
572 // data. 623 // data.
573 const char* const kAmericanExpressCard = "americanExpressCC"; 624 const char* const kAmericanExpressCard = "americanExpressCC";
574 const char* const kDinersCard = "dinersCC"; 625 const char* const kDinersCard = "dinersCC";
575 const char* const kDiscoverCard = "discoverCC"; 626 const char* const kDiscoverCard = "discoverCC";
576 const char* const kGenericCard = "genericCC"; 627 const char* const kGenericCard = "genericCC";
577 const char* const kJCBCard = "jcbCC"; 628 const char* const kJCBCard = "jcbCC";
578 const char* const kMasterCard = "masterCardCC"; 629 const char* const kMasterCard = "masterCardCC";
579 const char* const kSoloCard = "soloCC"; 630 const char* const kSoloCard = "soloCC";
580 const char* const kVisaCard = "visaCC"; 631 const char* const kVisaCard = "visaCC";
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/autofill/credit_card_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698