Chromium Code Reviews| OLD | NEW |
|---|---|
| 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> |
|
Ilya Sherman
2014/08/12 04:23:42
nit: Please include a blank line after this one, t
Pritam Nikam
2014/08/12 14:00:37
Done.
| |
| 8 #include <algorithm> | |
| 8 | 9 |
| 9 #include "base/logging.h" | 10 #include "base/logging.h" |
| 10 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/strings/string16.h" | 12 #include "base/strings/string16.h" |
| 12 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
| 13 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
| 14 #include "components/autofill/core/browser/autofill_field.h" | 15 #include "components/autofill/core/browser/autofill_field.h" |
| 15 #include "components/autofill/core/browser/autofill_regex_constants.h" | 16 #include "components/autofill/core/browser/autofill_regex_constants.h" |
| 16 #include "components/autofill/core/browser/autofill_scanner.h" | 17 #include "components/autofill/core/browser/autofill_scanner.h" |
| 17 #include "components/autofill/core/browser/field_types.h" | 18 #include "components/autofill/core/browser/field_types.h" |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 82 // has a plethora of names; we've seen "verification #", | 83 // has a plethora of names; we've seen "verification #", |
| 83 // "verification number", "card identification number" and others listed | 84 // "verification number", "card identification number" and others listed |
| 84 // in the |pattern| below. | 85 // in the |pattern| below. |
| 85 base::string16 pattern = base::UTF8ToUTF16(autofill::kCardCvcRe); | 86 base::string16 pattern = base::UTF8ToUTF16(autofill::kCardCvcRe); |
| 86 if (!credit_card_field->verification_ && | 87 if (!credit_card_field->verification_ && |
| 87 ParseField(scanner, pattern, &credit_card_field->verification_)) { | 88 ParseField(scanner, pattern, &credit_card_field->verification_)) { |
| 88 continue; | 89 continue; |
| 89 } | 90 } |
| 90 | 91 |
| 91 pattern = base::UTF8ToUTF16(autofill::kCardNumberRe); | 92 pattern = base::UTF8ToUTF16(autofill::kCardNumberRe); |
| 92 if (!credit_card_field->number_ && | 93 const AutofillField* cc_number_field; |
| 93 ParseField(scanner, pattern, &credit_card_field->number_)) { | 94 if (ParseField(scanner, pattern, &cc_number_field)) { |
| 95 size_t start_index = 0; | |
| 96 AutofillField* current_number_field = | |
| 97 const_cast<AutofillField*>(cc_number_field); | |
|
Pritam Nikam
2014/08/12 14:00:38
I didn't get any alternative approach to fix const
| |
| 98 // Avoid having very large start_index. | |
| 99 if (!credit_card_field->numbers_.empty()) | |
| 100 start_index = | |
| 101 std::min(std::numeric_limits<size_t>::max(), | |
|
Ilya Sherman
2014/08/12 04:23:42
This std::min call does not do anything, since the
Pritam Nikam
2014/08/12 14:00:37
Done.
| |
| 102 credit_card_field->numbers_.back() | |
| 103 ->credit_card_number_start_index() + | |
| 104 credit_card_field->numbers_.back()->max_length); | |
|
Ilya Sherman
2014/08/12 04:23:42
You should probably check whether the resulting st
Pritam Nikam
2014/08/12 14:00:37
Done.
| |
| 105 | |
| 106 current_number_field->set_credit_card_number_start_index(start_index); | |
| 107 credit_card_field->numbers_.push_back(current_number_field); | |
| 94 continue; | 108 continue; |
| 95 } | 109 } |
| 96 | 110 |
| 97 if (LowerCaseEqualsASCII(scanner->Cursor()->form_control_type, "month")) { | 111 if (LowerCaseEqualsASCII(scanner->Cursor()->form_control_type, "month")) { |
| 98 credit_card_field->expiration_date_ = scanner->Cursor(); | 112 credit_card_field->expiration_date_ = scanner->Cursor(); |
| 99 scanner->Advance(); | 113 scanner->Advance(); |
| 100 } else { | 114 } else { |
| 101 // First try to parse split month/year expiration fields. | 115 // First try to parse split month/year expiration fields. |
| 102 scanner->SaveCursor(); | 116 scanner->SaveCursor(); |
| 103 pattern = base::UTF8ToUTF16(autofill::kExpirationMonthRe); | 117 pattern = base::UTF8ToUTF16(autofill::kExpirationMonthRe); |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 160 if (credit_card_field->cardholder_) | 174 if (credit_card_field->cardholder_) |
| 161 return credit_card_field.release(); | 175 return credit_card_field.release(); |
| 162 | 176 |
| 163 // On some pages, the user selects a card type using radio buttons | 177 // On some pages, the user selects a card type using radio buttons |
| 164 // (e.g. test page Apple Store Billing.html). We can't handle that yet, | 178 // (e.g. test page Apple Store Billing.html). We can't handle that yet, |
| 165 // so we treat the card type as optional for now. | 179 // so we treat the card type as optional for now. |
| 166 // The existence of a number or cvc in combination with expiration date is | 180 // The existence of a number or cvc in combination with expiration date is |
| 167 // a strong enough signal that this is a credit card. It is possible that | 181 // a strong enough signal that this is a credit card. It is possible that |
| 168 // the number and name were parsed in a separate part of the form. So if | 182 // the number and name were parsed in a separate part of the form. So if |
| 169 // the cvc and date were found independently they are returned. | 183 // the cvc and date were found independently they are returned. |
| 170 if ((credit_card_field->number_ || credit_card_field->verification_) && | 184 if ((!credit_card_field->numbers_.empty() || |
| 185 credit_card_field->verification_) && | |
| 171 (credit_card_field->expiration_date_ || | 186 (credit_card_field->expiration_date_ || |
| 172 (credit_card_field->expiration_month_ && | 187 (credit_card_field->expiration_month_ && |
| 173 credit_card_field->expiration_year_))) { | 188 credit_card_field->expiration_year_))) { |
| 174 return credit_card_field.release(); | 189 return credit_card_field.release(); |
| 175 } | 190 } |
| 176 | 191 |
| 177 scanner->RewindTo(saved_cursor); | 192 scanner->RewindTo(saved_cursor); |
| 178 return NULL; | 193 return NULL; |
| 179 } | 194 } |
| 180 | 195 |
| 181 CreditCardField::CreditCardField() | 196 CreditCardField::CreditCardField() |
| 182 : cardholder_(NULL), | 197 : cardholder_(NULL), |
| 183 cardholder_last_(NULL), | 198 cardholder_last_(NULL), |
| 184 type_(NULL), | 199 type_(NULL), |
| 185 number_(NULL), | |
| 186 verification_(NULL), | 200 verification_(NULL), |
| 187 expiration_month_(NULL), | 201 expiration_month_(NULL), |
| 188 expiration_year_(NULL), | 202 expiration_year_(NULL), |
| 189 expiration_date_(NULL), | 203 expiration_date_(NULL), |
| 190 is_two_digit_year_(false) { | 204 is_two_digit_year_(false) { |
| 191 } | 205 } |
| 192 | 206 |
| 207 CreditCardField::~CreditCardField() { | |
| 208 } | |
| 209 | |
| 193 bool CreditCardField::ClassifyField(ServerFieldTypeMap* map) const { | 210 bool CreditCardField::ClassifyField(ServerFieldTypeMap* map) const { |
| 194 bool ok = AddClassification(number_, CREDIT_CARD_NUMBER, map); | 211 bool ok = true; |
| 212 for (std::vector<const AutofillField*>::const_iterator it = numbers_.begin(); | |
| 213 it != numbers_.end(); | |
| 214 ++it) | |
| 215 ok = ok && AddClassification(*it, CREDIT_CARD_NUMBER, map); | |
|
Ilya Sherman
2014/08/12 04:23:42
nit: Please add curly braces.
Pritam Nikam
2014/08/12 14:00:37
Done.
| |
| 216 | |
| 195 ok = ok && AddClassification(type_, CREDIT_CARD_TYPE, map); | 217 ok = ok && AddClassification(type_, CREDIT_CARD_TYPE, map); |
| 196 ok = ok && AddClassification(verification_, CREDIT_CARD_VERIFICATION_CODE, | 218 ok = ok && AddClassification(verification_, CREDIT_CARD_VERIFICATION_CODE, |
| 197 map); | 219 map); |
| 198 | 220 |
| 199 // If the heuristics detected first and last name in separate fields, | 221 // If the heuristics detected first and last name in separate fields, |
| 200 // then ignore both fields. Putting them into separate fields is probably | 222 // then ignore both fields. Putting them into separate fields is probably |
| 201 // wrong, because the credit card can also contain a middle name or middle | 223 // wrong, because the credit card can also contain a middle name or middle |
| 202 // initial. | 224 // initial. |
| 203 if (cardholder_last_ == NULL) | 225 if (cardholder_last_ == NULL) |
| 204 ok = ok && AddClassification(cardholder_, CREDIT_CARD_NAME, map); | 226 ok = ok && AddClassification(cardholder_, CREDIT_CARD_NAME, map); |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 221 ok = ok && AddClassification(expiration_year_, | 243 ok = ok && AddClassification(expiration_year_, |
| 222 CREDIT_CARD_EXP_4_DIGIT_YEAR, | 244 CREDIT_CARD_EXP_4_DIGIT_YEAR, |
| 223 map); | 245 map); |
| 224 } | 246 } |
| 225 } | 247 } |
| 226 | 248 |
| 227 return ok; | 249 return ok; |
| 228 } | 250 } |
| 229 | 251 |
| 230 } // namespace autofill | 252 } // namespace autofill |
| OLD | NEW |