| 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/content/renderer/form_cache.h" | 5 #include "components/autofill/content/renderer/form_cache.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "components/autofill/content/renderer/form_autofill_util.h" | 9 #include "components/autofill/content/renderer/form_autofill_util.h" |
| 10 #include "components/autofill/core/common/autofill_constants.h" | 10 #include "components/autofill/core/common/autofill_constants.h" |
| 11 #include "components/autofill/core/common/form_data.h" | 11 #include "components/autofill/core/common/form_data.h" |
| 12 #include "components/autofill/core/common/form_data_predictions.h" | 12 #include "components/autofill/core/common/form_data_predictions.h" |
| 13 #include "components/autofill/core/common/form_field_data.h" | 13 #include "components/autofill/core/common/form_field_data.h" |
| 14 #include "components/autofill/core/common/form_field_data_predictions.h" | 14 #include "components/autofill/core/common/form_field_data_predictions.h" |
| 15 #include "grit/component_strings.h" | 15 #include "grit/component_strings.h" |
| 16 #include "third_party/WebKit/public/platform/WebString.h" | 16 #include "third_party/WebKit/public/platform/WebString.h" |
| 17 #include "third_party/WebKit/public/platform/WebVector.h" | 17 #include "third_party/WebKit/public/platform/WebVector.h" |
| 18 #include "third_party/WebKit/public/web/WebDocument.h" | 18 #include "third_party/WebKit/public/web/WebDocument.h" |
| 19 #include "third_party/WebKit/public/web/WebFormControlElement.h" | 19 #include "third_party/WebKit/public/web/WebFormControlElement.h" |
| 20 #include "third_party/WebKit/public/web/WebFormElement.h" | 20 #include "third_party/WebKit/public/web/WebFormElement.h" |
| 21 #include "third_party/WebKit/public/web/WebInputElement.h" | 21 #include "third_party/WebKit/public/web/WebInputElement.h" |
| 22 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 22 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
| 23 #include "third_party/WebKit/public/web/WebNodeList.h" |
| 23 #include "third_party/WebKit/public/web/WebSelectElement.h" | 24 #include "third_party/WebKit/public/web/WebSelectElement.h" |
| 24 #include "third_party/WebKit/public/web/WebTextAreaElement.h" | 25 #include "third_party/WebKit/public/web/WebTextAreaElement.h" |
| 25 #include "ui/base/l10n/l10n_util.h" | 26 #include "ui/base/l10n/l10n_util.h" |
| 26 | 27 |
| 27 using blink::WebDocument; | 28 using blink::WebDocument; |
| 28 using blink::WebFormControlElement; | 29 using blink::WebFormControlElement; |
| 29 using blink::WebFormElement; | 30 using blink::WebFormElement; |
| 30 using blink::WebFrame; | 31 using blink::WebFrame; |
| 31 using blink::WebInputElement; | 32 using blink::WebInputElement; |
| 32 using blink::WebSelectElement; | 33 using blink::WebSelectElement; |
| (...skipping 28 matching lines...) Expand all Loading... |
| 61 states->erase(*it); | 62 states->erase(*it); |
| 62 } | 63 } |
| 63 } | 64 } |
| 64 | 65 |
| 65 FormCache::FormCache() { | 66 FormCache::FormCache() { |
| 66 } | 67 } |
| 67 | 68 |
| 68 FormCache::~FormCache() { | 69 FormCache::~FormCache() { |
| 69 } | 70 } |
| 70 | 71 |
| 71 void FormCache::ExtractForms(const WebFrame& frame, | 72 void FormCache::ExtractNewForms(const WebFrame& frame, |
| 72 std::vector<FormData>* forms) { | 73 std::vector<FormData>* forms) { |
| 73 ExtractFormsAndFormElements(frame, kRequiredAutofillFields, forms, NULL); | |
| 74 } | |
| 75 | |
| 76 bool FormCache::ExtractFormsAndFormElements( | |
| 77 const WebFrame& frame, | |
| 78 size_t minimum_required_fields, | |
| 79 std::vector<FormData>* forms, | |
| 80 std::vector<WebFormElement>* web_form_elements) { | |
| 81 // Reset the cache for this frame. | |
| 82 ResetFrame(frame); | |
| 83 | |
| 84 WebDocument document = frame.document(); | 74 WebDocument document = frame.document(); |
| 85 if (document.isNull()) | 75 if (document.isNull()) |
| 86 return false; | 76 return; |
| 87 | 77 |
| 88 web_documents_.insert(document); | 78 web_documents_.insert(document); |
| 89 | 79 |
| 90 WebVector<WebFormElement> web_forms; | 80 WebVector<WebFormElement> web_forms; |
| 91 document.forms(web_forms); | 81 document.forms(web_forms); |
| 92 | 82 |
| 93 size_t num_fields_seen = 0; | 83 size_t num_fields_seen = 0; |
| 94 bool has_skipped_forms = false; | |
| 95 for (size_t i = 0; i < web_forms.size(); ++i) { | 84 for (size_t i = 0; i < web_forms.size(); ++i) { |
| 96 WebFormElement form_element = web_forms[i]; | 85 WebFormElement form_element = web_forms[i]; |
| 97 | 86 |
| 98 std::vector<WebFormControlElement> control_elements; | 87 std::vector<WebFormControlElement> control_elements; |
| 99 ExtractAutofillableElements(form_element, autofill::REQUIRE_NONE, | 88 ExtractAutofillableElements(form_element, autofill::REQUIRE_NONE, |
| 100 &control_elements); | 89 &control_elements); |
| 101 | 90 |
| 102 size_t num_editable_elements = 0; | 91 size_t num_editable_elements = 0; |
| 103 for (size_t j = 0; j < control_elements.size(); ++j) { | 92 for (size_t j = 0; j < control_elements.size(); ++j) { |
| 104 WebFormControlElement element = control_elements[j]; | 93 WebFormControlElement element = control_elements[j]; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 121 std::make_pair(input_element, input_element.isChecked())); | 110 std::make_pair(input_element, input_element.isChecked())); |
| 122 } else { | 111 } else { |
| 123 ++num_editable_elements; | 112 ++num_editable_elements; |
| 124 } | 113 } |
| 125 } | 114 } |
| 126 } | 115 } |
| 127 | 116 |
| 128 // To avoid overly expensive computation, we impose a minimum number of | 117 // To avoid overly expensive computation, we impose a minimum number of |
| 129 // allowable fields. The corresponding maximum number of allowable fields | 118 // allowable fields. The corresponding maximum number of allowable fields |
| 130 // is imposed by WebFormElementToFormData(). | 119 // is imposed by WebFormElementToFormData(). |
| 131 if (num_editable_elements < minimum_required_fields && | 120 if (num_editable_elements < kRequiredAutofillFields && |
| 132 control_elements.size() > 0) { | 121 control_elements.size() > 0) { |
| 133 has_skipped_forms = true; | |
| 134 continue; | 122 continue; |
| 135 } | 123 } |
| 136 | 124 |
| 137 FormData form; | 125 FormData form; |
| 138 ExtractMask extract_mask = | 126 ExtractMask extract_mask = |
| 139 static_cast<ExtractMask>(EXTRACT_VALUE | EXTRACT_OPTIONS); | 127 static_cast<ExtractMask>(EXTRACT_VALUE | EXTRACT_OPTIONS); |
| 140 | 128 |
| 141 if (!WebFormElementToFormData(form_element, WebFormControlElement(), | 129 if (!WebFormElementToFormData(form_element, WebFormControlElement(), |
| 142 REQUIRE_NONE, extract_mask, &form, NULL)) { | 130 REQUIRE_NONE, extract_mask, &form, NULL)) { |
| 143 continue; | 131 continue; |
| 144 } | 132 } |
| 145 | 133 |
| 146 num_fields_seen += form.fields.size(); | 134 num_fields_seen += form.fields.size(); |
| 147 if (num_fields_seen > kMaxParseableFields) | 135 if (num_fields_seen > kMaxParseableFields) |
| 148 break; | 136 break; |
| 149 | 137 |
| 150 if (form.fields.size() >= minimum_required_fields) { | 138 if (form.fields.size() >= kRequiredAutofillFields && |
| 139 !parsed_forms_[&frame].count(form)) { |
| 151 forms->push_back(form); | 140 forms->push_back(form); |
| 152 if (web_form_elements) | 141 parsed_forms_[&frame].insert(form); |
| 153 web_form_elements->push_back(form_element); | |
| 154 } else { | |
| 155 has_skipped_forms = true; | |
| 156 } | 142 } |
| 157 } | 143 } |
| 158 | |
| 159 // Return true if there are any WebFormElements skipped, else false. | |
| 160 return has_skipped_forms; | |
| 161 } | 144 } |
| 162 | 145 |
| 163 void FormCache::ResetFrame(const WebFrame& frame) { | 146 void FormCache::ResetFrame(const WebFrame& frame) { |
| 164 std::vector<WebDocument> documents_to_delete; | 147 std::vector<WebDocument> documents_to_delete; |
| 165 for (std::set<WebDocument>::const_iterator it = web_documents_.begin(); | 148 for (std::set<WebDocument>::const_iterator it = web_documents_.begin(); |
| 166 it != web_documents_.end(); ++it) { | 149 it != web_documents_.end(); ++it) { |
| 167 const WebFrame* document_frame = it->frame(); | 150 const WebFrame* document_frame = it->frame(); |
| 168 if (!document_frame || document_frame == &frame) | 151 if (!document_frame || document_frame == &frame) |
| 169 documents_to_delete.push_back(*it); | 152 documents_to_delete.push_back(*it); |
| 170 } | 153 } |
| 171 | 154 |
| 172 for (std::vector<WebDocument>::const_iterator it = | 155 for (std::vector<WebDocument>::const_iterator it = |
| 173 documents_to_delete.begin(); | 156 documents_to_delete.begin(); |
| 174 it != documents_to_delete.end(); ++it) { | 157 it != documents_to_delete.end(); ++it) { |
| 175 web_documents_.erase(*it); | 158 web_documents_.erase(*it); |
| 176 } | 159 } |
| 177 | 160 |
| 161 parsed_forms_[&frame].clear(); |
| 178 RemoveOldElements(frame, &initial_select_values_); | 162 RemoveOldElements(frame, &initial_select_values_); |
| 179 RemoveOldElements(frame, &initial_checked_state_); | 163 RemoveOldElements(frame, &initial_checked_state_); |
| 180 } | 164 } |
| 181 | 165 |
| 182 bool FormCache::ClearFormWithElement(const WebFormControlElement& element) { | 166 bool FormCache::ClearFormWithElement(const WebFormControlElement& element) { |
| 183 WebFormElement form_element = element.form(); | 167 WebFormElement form_element = element.form(); |
| 184 if (form_element.isNull()) | 168 if (form_element.isNull()) |
| 185 return false; | 169 return false; |
| 186 | 170 |
| 187 std::vector<WebFormControlElement> control_elements; | 171 std::vector<WebFormControlElement> control_elements; |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 296 element->setAttribute("placeholder", | 280 element->setAttribute("placeholder", |
| 297 WebString(base::UTF8ToUTF16(placeholder))); | 281 WebString(base::UTF8ToUTF16(placeholder))); |
| 298 } | 282 } |
| 299 element->setAttribute("title", WebString(title)); | 283 element->setAttribute("title", WebString(title)); |
| 300 } | 284 } |
| 301 | 285 |
| 302 return true; | 286 return true; |
| 303 } | 287 } |
| 304 | 288 |
| 305 } // namespace autofill | 289 } // namespace autofill |
| OLD | NEW |