| 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/form_field.h" | 5 #include "components/autofill/core/browser/form_field.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 bool IsCheckable(const AutofillField* field) { | 30 bool IsCheckable(const AutofillField* field) { |
| 31 return field->is_checkable; | 31 return field->is_checkable; |
| 32 } | 32 } |
| 33 | 33 |
| 34 } // namespace | 34 } // namespace |
| 35 | 35 |
| 36 // static | 36 // static |
| 37 void FormField::ParseFormFields(const std::vector<AutofillField*>& fields, | 37 void FormField::ParseFormFields(const std::vector<AutofillField*>& fields, |
| 38 ServerFieldTypeMap* map) { | 38 ServerFieldTypeMap* map) { |
| 39 // Set up a working copy of the fields to be processed. | 39 // Set up a working copy of the fields to be processed. |
| 40 std::vector<const AutofillField*> remaining_fields(fields.size()); | 40 std::vector<AutofillField*> remaining_fields(fields.size()); |
| 41 std::copy(fields.begin(), fields.end(), remaining_fields.begin()); | 41 std::copy(fields.begin(), fields.end(), remaining_fields.begin()); |
| 42 | 42 |
| 43 // Ignore checkable fields as they interfere with parsers assuming context. | 43 // Ignore checkable fields as they interfere with parsers assuming context. |
| 44 // Eg., while parsing address, "Is PO box" checkbox after ADDRESS_LINE1 | 44 // Eg., while parsing address, "Is PO box" checkbox after ADDRESS_LINE1 |
| 45 // interferes with correctly understanding ADDRESS_LINE2. | 45 // interferes with correctly understanding ADDRESS_LINE2. |
| 46 remaining_fields.erase( | 46 remaining_fields.erase( |
| 47 std::remove_if(remaining_fields.begin(), remaining_fields.end(), | 47 std::remove_if(remaining_fields.begin(), remaining_fields.end(), |
| 48 IsCheckable), | 48 IsCheckable), |
| 49 remaining_fields.end()); | 49 remaining_fields.end()); |
| 50 | 50 |
| 51 // Email pass. | 51 // Email pass. |
| 52 ParseFormFieldsPass(EmailField::Parse, &remaining_fields, map); | 52 ParseFormFieldsPass(EmailField::Parse, &remaining_fields, map); |
| 53 | 53 |
| 54 // Phone pass. | 54 // Phone pass. |
| 55 ParseFormFieldsPass(PhoneField::Parse, &remaining_fields, map); | 55 ParseFormFieldsPass(PhoneField::Parse, &remaining_fields, map); |
| 56 | 56 |
| 57 // Address pass. | 57 // Address pass. |
| 58 ParseFormFieldsPass(AddressField::Parse, &remaining_fields, map); | 58 ParseFormFieldsPass(AddressField::Parse, &remaining_fields, map); |
| 59 | 59 |
| 60 // Credit card pass. | 60 // Credit card pass. |
| 61 ParseFormFieldsPass(CreditCardField::Parse, &remaining_fields, map); | 61 ParseFormFieldsPass(CreditCardField::Parse, &remaining_fields, map); |
| 62 | 62 |
| 63 // Name pass. | 63 // Name pass. |
| 64 ParseFormFieldsPass(NameField::Parse, &remaining_fields, map); | 64 ParseFormFieldsPass(NameField::Parse, &remaining_fields, map); |
| 65 } | 65 } |
| 66 | 66 |
| 67 // static | 67 // static |
| 68 bool FormField::ParseField(AutofillScanner* scanner, | 68 bool FormField::ParseField(AutofillScanner* scanner, |
| 69 const base::string16& pattern, | 69 const base::string16& pattern, |
| 70 const AutofillField** match) { | 70 AutofillField** match) { |
| 71 return ParseFieldSpecifics(scanner, pattern, MATCH_DEFAULT, match); | 71 return ParseFieldSpecifics(scanner, pattern, MATCH_DEFAULT, match); |
| 72 } | 72 } |
| 73 | 73 |
| 74 // static | 74 // static |
| 75 bool FormField::ParseFieldSpecifics(AutofillScanner* scanner, | 75 bool FormField::ParseFieldSpecifics(AutofillScanner* scanner, |
| 76 const base::string16& pattern, | 76 const base::string16& pattern, |
| 77 int match_type, | 77 int match_type, |
| 78 const AutofillField** match) { | 78 AutofillField** match) { |
| 79 if (scanner->IsEnd()) | 79 if (scanner->IsEnd()) |
| 80 return false; | 80 return false; |
| 81 | 81 |
| 82 const AutofillField* field = scanner->Cursor(); | 82 const AutofillField* field = scanner->Cursor(); |
| 83 | 83 |
| 84 if (!MatchesFormControlType(field->form_control_type, match_type)) | 84 if (!MatchesFormControlType(field->form_control_type, match_type)) |
| 85 return false; | 85 return false; |
| 86 | 86 |
| 87 return MatchAndAdvance(scanner, pattern, match_type, match); | 87 return MatchAndAdvance(scanner, pattern, match_type, match); |
| 88 } | 88 } |
| 89 | 89 |
| 90 // static | 90 // static |
| 91 bool FormField::ParseEmptyLabel(AutofillScanner* scanner, | 91 bool FormField::ParseEmptyLabel(AutofillScanner* scanner, |
| 92 const AutofillField** match) { | 92 AutofillField** match) { |
| 93 return ParseFieldSpecifics(scanner, | 93 return ParseFieldSpecifics(scanner, |
| 94 base::ASCIIToUTF16("^$"), | 94 base::ASCIIToUTF16("^$"), |
| 95 MATCH_LABEL | MATCH_ALL_INPUTS, | 95 MATCH_LABEL | MATCH_ALL_INPUTS, |
| 96 match); | 96 match); |
| 97 } | 97 } |
| 98 | 98 |
| 99 // static | 99 // static |
| 100 bool FormField::AddClassification(const AutofillField* field, | 100 bool FormField::AddClassification(const AutofillField* field, |
| 101 ServerFieldType type, | 101 ServerFieldType type, |
| 102 ServerFieldTypeMap* map) { | 102 ServerFieldTypeMap* map) { |
| 103 // Several fields are optional. | 103 // Several fields are optional. |
| 104 if (!field) | 104 if (!field) |
| 105 return true; | 105 return true; |
| 106 | 106 |
| 107 return map->insert(make_pair(field->unique_name(), type)).second; | 107 return map->insert(make_pair(field->unique_name(), type)).second; |
| 108 } | 108 } |
| 109 | 109 |
| 110 // static. | 110 // static. |
| 111 bool FormField::MatchAndAdvance(AutofillScanner* scanner, | 111 bool FormField::MatchAndAdvance(AutofillScanner* scanner, |
| 112 const base::string16& pattern, | 112 const base::string16& pattern, |
| 113 int match_type, | 113 int match_type, |
| 114 const AutofillField** match) { | 114 AutofillField** match) { |
| 115 const AutofillField* field = scanner->Cursor(); | 115 AutofillField* field = scanner->Cursor(); |
| 116 if (FormField::Match(field, pattern, match_type)) { | 116 if (FormField::Match(field, pattern, match_type)) { |
| 117 if (match) | 117 if (match) |
| 118 *match = field; | 118 *match = field; |
| 119 scanner->Advance(); | 119 scanner->Advance(); |
| 120 return true; | 120 return true; |
| 121 } | 121 } |
| 122 | 122 |
| 123 return false; | 123 return false; |
| 124 } | 124 } |
| 125 | 125 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 140 if ((match_type & FormField::MATCH_VALUE) && | 140 if ((match_type & FormField::MATCH_VALUE) && |
| 141 autofill::MatchesPattern(field->value, pattern)) { | 141 autofill::MatchesPattern(field->value, pattern)) { |
| 142 return true; | 142 return true; |
| 143 } | 143 } |
| 144 | 144 |
| 145 return false; | 145 return false; |
| 146 } | 146 } |
| 147 | 147 |
| 148 // static | 148 // static |
| 149 void FormField::ParseFormFieldsPass(ParseFunction parse, | 149 void FormField::ParseFormFieldsPass(ParseFunction parse, |
| 150 std::vector<const AutofillField*>* fields, | 150 std::vector<AutofillField*>* fields, |
| 151 ServerFieldTypeMap* map) { | 151 ServerFieldTypeMap* map) { |
| 152 // Store unmatched fields for further processing by the caller. | 152 // Store unmatched fields for further processing by the caller. |
| 153 std::vector<const AutofillField*> remaining_fields; | 153 std::vector<AutofillField*> remaining_fields; |
| 154 | 154 |
| 155 AutofillScanner scanner(*fields); | 155 AutofillScanner scanner(*fields); |
| 156 while (!scanner.IsEnd()) { | 156 while (!scanner.IsEnd()) { |
| 157 scoped_ptr<FormField> form_field(parse(&scanner)); | 157 scoped_ptr<FormField> form_field(parse(&scanner)); |
| 158 if (!form_field.get()) { | 158 if (!form_field.get()) { |
| 159 remaining_fields.push_back(scanner.Cursor()); | 159 remaining_fields.push_back(scanner.Cursor()); |
| 160 scanner.Advance(); | 160 scanner.Advance(); |
| 161 continue; | 161 continue; |
| 162 } | 162 } |
| 163 | 163 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 183 if ((match_type & MATCH_SELECT) && type == "select-one") | 183 if ((match_type & MATCH_SELECT) && type == "select-one") |
| 184 return true; | 184 return true; |
| 185 | 185 |
| 186 if ((match_type & MATCH_TEXT_AREA) && type == "textarea") | 186 if ((match_type & MATCH_TEXT_AREA) && type == "textarea") |
| 187 return true; | 187 return true; |
| 188 | 188 |
| 189 return false; | 189 return false; |
| 190 } | 190 } |
| 191 | 191 |
| 192 } // namespace autofill | 192 } // namespace autofill |
| OLD | NEW |