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

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