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

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

Issue 1012363002: Autofill: Recognize month/year selects when searching for credit cards. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address comments Created 5 years, 9 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/credit_card_field.h" 5 #include "components/autofill/core/browser/credit_card_field.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "base/stl_util.h"
10 #include "base/strings/string16.h" 11 #include "base/strings/string16.h"
12 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/string_util.h" 13 #include "base/strings/string_util.h"
12 #include "base/strings/utf_string_conversions.h" 14 #include "base/strings/utf_string_conversions.h"
15 #include "base/time/time.h"
13 #include "components/autofill/core/browser/autofill_field.h" 16 #include "components/autofill/core/browser/autofill_field.h"
14 #include "components/autofill/core/browser/autofill_regex_constants.h" 17 #include "components/autofill/core/browser/autofill_regex_constants.h"
18 #include "components/autofill/core/browser/autofill_regexes.h"
15 #include "components/autofill/core/browser/autofill_scanner.h" 19 #include "components/autofill/core/browser/autofill_scanner.h"
16 #include "components/autofill/core/browser/field_types.h" 20 #include "components/autofill/core/browser/field_types.h"
17 21
18 namespace autofill { 22 namespace autofill {
19 23
24 namespace {
25
20 // Credit card numbers are at most 19 digits in length. 26 // Credit card numbers are at most 19 digits in length.
21 // [Ref: http://en.wikipedia.org/wiki/Bank_card_number] 27 // [Ref: http://en.wikipedia.org/wiki/Bank_card_number]
22 static const size_t kMaxValidCardNumberSize = 19; 28 const size_t kMaxValidCardNumberSize = 19;
29
30 // Look for the vector |regex_needles| in |haystack|. Returns true if a
31 // consecutive section of |haystack| matches |regex_needles|.
32 bool FindConsecutiveStrings(const std::vector<base::string16>& regex_needles,
33 const std::vector<base::string16>& haystack) {
34 if (regex_needles.empty() || haystack.empty())
35 return false;
36
37 for (size_t i = 0; i < haystack.size() - regex_needles.size() + 1; ++i) {
38 auto haystack_it = haystack.begin() + i;
39 bool found_match = true;
40 for (const base::string16& regex : regex_needles) {
Evan Stade 2015/03/18 20:41:24 for (int j = 0; j < regex_needles.size(); ++j) {
Lei Zhang 2015/03/19 02:33:28 Done.
41 if (!MatchesPattern(*haystack_it, regex)) {
42 found_match = false;
43 break;
44 }
45 ++haystack_it;
46 }
47 if (found_match)
48 return true;
49 }
50 return false;
51 }
52
53 } // namespace
23 54
24 // static 55 // static
25 scoped_ptr<FormField> CreditCardField::Parse(AutofillScanner* scanner) { 56 scoped_ptr<FormField> CreditCardField::Parse(AutofillScanner* scanner) {
26 if (scanner->IsEnd()) 57 if (scanner->IsEnd())
27 return nullptr; 58 return nullptr;
28 59
29 scoped_ptr<CreditCardField> credit_card_field(new CreditCardField); 60 scoped_ptr<CreditCardField> credit_card_field(new CreditCardField);
30 size_t saved_cursor = scanner->SaveCursor(); 61 size_t saved_cursor = scanner->SaveCursor();
31 62
32 // Credit card fields can appear in many different orders. 63 // Credit card fields can appear in many different orders.
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 bool has_date_or_mm_yy = (credit_card_field->expiration_date_ || 181 bool has_date_or_mm_yy = (credit_card_field->expiration_date_ ||
151 (credit_card_field->expiration_month_ && 182 (credit_card_field->expiration_month_ &&
152 credit_card_field->expiration_year_)); 183 credit_card_field->expiration_year_));
153 if (has_cc_number_or_verification && has_date_or_mm_yy) 184 if (has_cc_number_or_verification && has_date_or_mm_yy)
154 return credit_card_field.Pass(); 185 return credit_card_field.Pass();
155 186
156 scanner->RewindTo(saved_cursor); 187 scanner->RewindTo(saved_cursor);
157 return nullptr; 188 return nullptr;
158 } 189 }
159 190
191 // static
192 bool CreditCardField::LikelyCardMonthSelectField(AutofillScanner* scanner) {
193 if (scanner->IsEnd())
194 return false;
195
196 AutofillField* field = scanner->Cursor();
197 if (!MatchesFormControlType(field->form_control_type, MATCH_SELECT))
198 return false;
199
200 if (field->option_values.size() < 12 || field->option_values.size() > 13)
201 return false;
202
203 // Filter out years.
204 const base::string16 kNumericalYearRe =
205 base::ASCIIToUTF16("[1-9][0-9][0-9][0-9]");
206 for (const auto& value : field->option_values) {
207 if (MatchesPattern(value, kNumericalYearRe))
208 return false;
209 }
210 for (const auto& value : field->option_contents) {
211 if (MatchesPattern(value, kNumericalYearRe))
212 return false;
213 }
214
215 // Look for numerical months.
216 const base::string16 kNumericalMonthRe = base::ASCIIToUTF16("12");
217 if (MatchesPattern(field->option_values.back(), kNumericalMonthRe) ||
218 MatchesPattern(field->option_contents.back(), kNumericalMonthRe)) {
219 return true;
220 }
221
222 // Maybe do more matches here. e.g. look for (translated) December.
223
224 // Unsure? Return false.
225 return false;
226 }
227
228 // static
229 bool CreditCardField::LikelyCardYearSelectField(AutofillScanner* scanner) {
230 if (scanner->IsEnd())
231 return false;
232
233 AutofillField* field = scanner->Cursor();
234 if (!MatchesFormControlType(field->form_control_type, MATCH_SELECT))
235 return false;
236
237 const base::Time time_now = base::Time::Now();
238 base::Time::Exploded time_exploded;
239 time_now.UTCExplode(&time_exploded);
240
241 const int kYearsToMatch = 3;
242 std::vector<base::string16> years_to_check;
243 for (int year = time_exploded.year;
244 year < time_exploded.year + kYearsToMatch;
245 ++year) {
246 years_to_check.push_back(base::IntToString16(year));
247 }
248 return (FindConsecutiveStrings(years_to_check, field->option_values) ||
249 FindConsecutiveStrings(years_to_check, field->option_contents));
250 }
251
160 CreditCardField::CreditCardField() 252 CreditCardField::CreditCardField()
161 : cardholder_(nullptr), 253 : cardholder_(nullptr),
162 cardholder_last_(nullptr), 254 cardholder_last_(nullptr),
163 type_(nullptr), 255 type_(nullptr),
164 verification_(nullptr), 256 verification_(nullptr),
165 expiration_month_(nullptr), 257 expiration_month_(nullptr),
166 expiration_year_(nullptr), 258 expiration_year_(nullptr),
167 expiration_date_(nullptr), 259 expiration_date_(nullptr),
168 exp_year_type_(CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR) { 260 exp_year_type_(CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR) {
169 } 261 }
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
208 expiration_date_ = scanner->Cursor(); 300 expiration_date_ = scanner->Cursor();
209 expiration_month_ = nullptr; 301 expiration_month_ = nullptr;
210 expiration_year_ = nullptr; 302 expiration_year_ = nullptr;
211 scanner->Advance(); 303 scanner->Advance();
212 return true; 304 return true;
213 } 305 }
214 306
215 if (expiration_month_ || expiration_date_) 307 if (expiration_month_ || expiration_date_)
216 return false; 308 return false;
217 309
218 // First try to parse split month/year expiration fields. 310 // First try to parse split month/year expiration fields by looking for a
311 // pair of select fields that look like month/year.
219 size_t month_year_saved_cursor = scanner->SaveCursor(); 312 size_t month_year_saved_cursor = scanner->SaveCursor();
313
314 if (LikelyCardMonthSelectField(scanner)) {
315 expiration_month_ = scanner->Cursor();
316 scanner->Advance();
317 if (LikelyCardYearSelectField(scanner)) {
318 expiration_year_ = scanner->Cursor();
319 scanner->Advance();
320 return true;
321 }
322 expiration_month_ = nullptr;
323 expiration_year_ = nullptr;
324 }
325
326 // If that fails, do a general regex search.
327 scanner->RewindTo(month_year_saved_cursor);
220 const int kMatchTelAndSelect = MATCH_DEFAULT | MATCH_TELEPHONE | MATCH_SELECT; 328 const int kMatchTelAndSelect = MATCH_DEFAULT | MATCH_TELEPHONE | MATCH_SELECT;
221 if (ParseFieldSpecifics(scanner, 329 if (ParseFieldSpecifics(scanner,
222 base::UTF8ToUTF16(kExpirationMonthRe), 330 base::UTF8ToUTF16(kExpirationMonthRe),
223 kMatchTelAndSelect, 331 kMatchTelAndSelect,
224 &expiration_month_) && 332 &expiration_month_) &&
225 ParseFieldSpecifics(scanner, 333 ParseFieldSpecifics(scanner,
226 base::UTF8ToUTF16(kExpirationYearRe), 334 base::UTF8ToUTF16(kExpirationYearRe),
227 kMatchTelAndSelect, 335 kMatchTelAndSelect,
228 &expiration_year_)) { 336 &expiration_year_)) {
229 return true; 337 return true;
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
268 376
269 ServerFieldType CreditCardField::GetExpirationYearType() const { 377 ServerFieldType CreditCardField::GetExpirationYearType() const {
270 return (expiration_date_ 378 return (expiration_date_
271 ? exp_year_type_ 379 ? exp_year_type_
272 : ((expiration_year_ && expiration_year_->max_length == 2) 380 : ((expiration_year_ && expiration_year_->max_length == 2)
273 ? CREDIT_CARD_EXP_2_DIGIT_YEAR 381 ? CREDIT_CARD_EXP_2_DIGIT_YEAR
274 : CREDIT_CARD_EXP_4_DIGIT_YEAR)); 382 : CREDIT_CARD_EXP_4_DIGIT_YEAR));
275 } 383 }
276 384
277 } // namespace autofill 385 } // namespace autofill
OLDNEW
« no previous file with comments | « components/autofill/core/browser/credit_card_field.h ('k') | components/autofill/core/browser/form_field.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698