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

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: 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 auto haystack_remaining_it = haystack.begin();
38 size_t haystack_remaining_size = haystack.size();
39 while (regex_needles.size() <= haystack_remaining_size) {
Evan Stade 2015/03/18 04:29:25 for (size_t i = 0; i < haystack.size() - regex_nee
Lei Zhang 2015/03/18 18:08:56 Done.
40 auto haystack_it = haystack_remaining_it;
41 bool found_match = true;
42 for (const base::string16& regex : regex_needles) {
43 if (!MatchesPattern(*haystack_it, regex)) {
44 found_match = false;
45 break;
46 }
47 ++haystack_it;
48 }
49 if (found_match)
50 return true;
51
52 ++haystack_remaining_it;
53 --haystack_remaining_size;
54 }
55 return false;
56 }
57
58 }
23 59
24 // static 60 // static
25 scoped_ptr<FormField> CreditCardField::Parse(AutofillScanner* scanner) { 61 scoped_ptr<FormField> CreditCardField::Parse(AutofillScanner* scanner) {
26 if (scanner->IsEnd()) 62 if (scanner->IsEnd())
27 return nullptr; 63 return nullptr;
28 64
29 scoped_ptr<CreditCardField> credit_card_field(new CreditCardField); 65 scoped_ptr<CreditCardField> credit_card_field(new CreditCardField);
30 size_t saved_cursor = scanner->SaveCursor(); 66 size_t saved_cursor = scanner->SaveCursor();
31 67
32 // Credit card fields can appear in many different orders. 68 // 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_ || 186 bool has_date_or_mm_yy = (credit_card_field->expiration_date_ ||
151 (credit_card_field->expiration_month_ && 187 (credit_card_field->expiration_month_ &&
152 credit_card_field->expiration_year_)); 188 credit_card_field->expiration_year_));
153 if (has_cc_number_or_verification && has_date_or_mm_yy) 189 if (has_cc_number_or_verification && has_date_or_mm_yy)
154 return credit_card_field.Pass(); 190 return credit_card_field.Pass();
155 191
156 scanner->RewindTo(saved_cursor); 192 scanner->RewindTo(saved_cursor);
157 return nullptr; 193 return nullptr;
158 } 194 }
159 195
196 // static
197 bool CreditCardField::LikelyCardMonthSelectField(AutofillScanner* scanner) {
198 if (scanner->IsEnd())
199 return false;
200
201 AutofillField* field = scanner->Cursor();
202 if (!MatchesFormControlType(field->form_control_type, MATCH_SELECT))
203 return false;
204
205 if (field->option_values.size() < 12 || field->option_values.size() > 13)
206 return false;
207
208 // Filter out years.
209 const base::string16 kNumericalYearRe =
210 base::ASCIIToUTF16("[1-9][0-9][0-9][0-9]");
211 for (const auto& value : field->option_values) {
212 if (MatchesPattern(value, kNumericalYearRe))
213 return false;
214 }
215 for (const auto& value : field->option_contents) {
216 if (MatchesPattern(value, kNumericalYearRe))
217 return false;
218 }
219
220 // Look for numerical months.
221 const base::string16 kNumericalMonthRe = base::ASCIIToUTF16("12");
222 if (MatchesPattern(field->option_values.back(), kNumericalMonthRe) ||
223 MatchesPattern(field->option_contents.back(), kNumericalMonthRe)) {
224 return true;
225 }
226
227 // Maybe do more matches here. e.g. look for (translated) December.
228
229 // Unsure? Return false.
230 return false;
231 }
232
233 // static
234 bool CreditCardField::LikelyCardYearSelectField(AutofillScanner* scanner) {
235 if (scanner->IsEnd())
236 return false;
237
238 AutofillField* field = scanner->Cursor();
239 if (!MatchesFormControlType(field->form_control_type, MATCH_SELECT))
240 return false;
241
242 const base::Time time_now = base::Time::Now();
243 base::Time::Exploded time_exploded;
244 time_now.UTCExplode(&time_exploded);
245
246 const int kYearsToMatch = 3;
247 std::vector<base::string16> years_to_check;
248 for (int year = time_exploded.year;
249 year < time_exploded.year + kYearsToMatch;
250 ++year) {
251 years_to_check.push_back(base::IntToString16(year));
252 }
253 if (FindConsecutiveStrings(years_to_check, field->option_values))
Evan Stade 2015/03/18 04:29:25 if X return true return Y is equivalent to ret
Lei Zhang 2015/03/18 18:08:56 Done.
254 return true;
255 return FindConsecutiveStrings(years_to_check, field->option_contents);
256 }
257
160 CreditCardField::CreditCardField() 258 CreditCardField::CreditCardField()
161 : cardholder_(nullptr), 259 : cardholder_(nullptr),
162 cardholder_last_(nullptr), 260 cardholder_last_(nullptr),
163 type_(nullptr), 261 type_(nullptr),
164 verification_(nullptr), 262 verification_(nullptr),
165 expiration_month_(nullptr), 263 expiration_month_(nullptr),
166 expiration_year_(nullptr), 264 expiration_year_(nullptr),
167 expiration_date_(nullptr), 265 expiration_date_(nullptr),
168 exp_year_type_(CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR) { 266 exp_year_type_(CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR) {
169 } 267 }
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
208 expiration_date_ = scanner->Cursor(); 306 expiration_date_ = scanner->Cursor();
209 expiration_month_ = nullptr; 307 expiration_month_ = nullptr;
210 expiration_year_ = nullptr; 308 expiration_year_ = nullptr;
211 scanner->Advance(); 309 scanner->Advance();
212 return true; 310 return true;
213 } 311 }
214 312
215 if (expiration_month_ || expiration_date_) 313 if (expiration_month_ || expiration_date_)
216 return false; 314 return false;
217 315
218 // First try to parse split month/year expiration fields. 316 // First try to parse split month/year expiration fields by looking for a
317 // pair of select fields that look like month/year.
219 size_t month_year_saved_cursor = scanner->SaveCursor(); 318 size_t month_year_saved_cursor = scanner->SaveCursor();
319
320 if (LikelyCardMonthSelectField(scanner)) {
321 expiration_month_ = scanner->Cursor();
322 scanner->Advance();
323 if (LikelyCardYearSelectField(scanner)) {
324 expiration_year_ = scanner->Cursor();
325 scanner->Advance();
326 return true;
327 }
328 expiration_month_ = nullptr;
329 expiration_year_ = nullptr;
330 }
331
332 // If that fails, do a general regex search.
333 scanner->RewindTo(month_year_saved_cursor);
220 const int kMatchTelAndSelect = MATCH_DEFAULT | MATCH_TELEPHONE | MATCH_SELECT; 334 const int kMatchTelAndSelect = MATCH_DEFAULT | MATCH_TELEPHONE | MATCH_SELECT;
221 if (ParseFieldSpecifics(scanner, 335 if (ParseFieldSpecifics(scanner,
222 base::UTF8ToUTF16(kExpirationMonthRe), 336 base::UTF8ToUTF16(kExpirationMonthRe),
223 kMatchTelAndSelect, 337 kMatchTelAndSelect,
224 &expiration_month_) && 338 &expiration_month_) &&
225 ParseFieldSpecifics(scanner, 339 ParseFieldSpecifics(scanner,
226 base::UTF8ToUTF16(kExpirationYearRe), 340 base::UTF8ToUTF16(kExpirationYearRe),
227 kMatchTelAndSelect, 341 kMatchTelAndSelect,
228 &expiration_year_)) { 342 &expiration_year_)) {
229 return true; 343 return true;
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
268 382
269 ServerFieldType CreditCardField::GetExpirationYearType() const { 383 ServerFieldType CreditCardField::GetExpirationYearType() const {
270 return (expiration_date_ 384 return (expiration_date_
271 ? exp_year_type_ 385 ? exp_year_type_
272 : ((expiration_year_ && expiration_year_->max_length == 2) 386 : ((expiration_year_ && expiration_year_->max_length == 2)
273 ? CREDIT_CARD_EXP_2_DIGIT_YEAR 387 ? CREDIT_CARD_EXP_2_DIGIT_YEAR
274 : CREDIT_CARD_EXP_4_DIGIT_YEAR)); 388 : CREDIT_CARD_EXP_4_DIGIT_YEAR));
275 } 389 }
276 390
277 } // namespace autofill 391 } // 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