// Copyright 2013 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "components/autofill/content/renderer/form_cache.h" #include "base/logging.h" #include "base/strings/utf_string_conversions.h" #include "components/autofill/content/renderer/form_autofill_util.h" #include "components/autofill/core/common/autofill_constants.h" #include "components/autofill/core/common/form_data.h" #include "components/autofill/core/common/form_data_predictions.h" #include "components/autofill/core/common/form_field_data.h" #include "components/autofill/core/common/form_field_data_predictions.h" #include "grit/component_strings.h" #include "third_party/WebKit/public/platform/WebString.h" #include "third_party/WebKit/public/platform/WebVector.h" #include "third_party/WebKit/public/web/WebDocument.h" #include "third_party/WebKit/public/web/WebFormControlElement.h" #include "third_party/WebKit/public/web/WebFormElement.h" #include "third_party/WebKit/public/web/WebFrame.h" #include "third_party/WebKit/public/web/WebInputElement.h" #include "third_party/WebKit/public/web/WebSelectElement.h" #include "third_party/WebKit/public/web/WebTextAreaElement.h" #include "ui/base/l10n/l10n_util.h" using blink::WebDocument; using blink::WebFormControlElement; using blink::WebFormElement; using blink::WebFrame; using blink::WebInputElement; using blink::WebSelectElement; using blink::WebTextAreaElement; using blink::WebString; using blink::WebVector; namespace autofill { // Helper function to discard state of various WebFormElements when they go out // of web frame's scope. This is done to release memory that we no longer need // to hold. // K should inherit from WebFormControlElement as the function looks to extract // WebFormElement for K.form(). template void RemoveOldElements(const WebFrame& frame, std::map* states) { std::vector to_remove; for (typename std::map::const_iterator it = states->begin(); it != states->end(); ++it) { WebFormElement form_element = it->first.form(); if (form_element.isNull()) { to_remove.push_back(it->first); } else { const WebFrame* element_frame = form_element.document().frame(); if (!element_frame || element_frame == &frame) to_remove.push_back(it->first); } } for (typename std::vector::const_iterator it = to_remove.begin(); it != to_remove.end(); ++it) { states->erase(*it); } } FormCache::FormCache() { } FormCache::~FormCache() { } void FormCache::ExtractForms(const WebFrame& frame, std::vector* forms) { ExtractFormsAndFormElements(frame, kRequiredAutofillFields, forms, NULL); } bool FormCache::ExtractFormsAndFormElements( const WebFrame& frame, size_t minimum_required_fields, std::vector* forms, std::vector* web_form_elements) { // Reset the cache for this frame. ResetFrame(frame); WebDocument document = frame.document(); if (document.isNull()) return false; web_documents_.insert(document); WebVector web_forms; document.forms(web_forms); size_t num_fields_seen = 0; bool has_skipped_forms = false; for (size_t i = 0; i < web_forms.size(); ++i) { WebFormElement form_element = web_forms[i]; std::vector control_elements; ExtractAutofillableElements(form_element, autofill::REQUIRE_NONE, &control_elements); size_t num_editable_elements = 0; for (size_t j = 0; j < control_elements.size(); ++j) { WebFormControlElement element = control_elements[j]; // Save original values of