// 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/components_strings.h" #include "third_party/WebKit/public/platform/WebString.h" #include "third_party/WebKit/public/platform/WebVector.h" #include "third_party/WebKit/public/web/WebConsoleMessage.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/WebInputElement.h" #include "third_party/WebKit/public/web/WebLocalFrame.h" #include "third_party/WebKit/public/web/WebNodeList.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::WebConsoleMessage; 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::ExtractNewForms(const WebFrame& frame, std::vector* forms) { WebDocument document = frame.document(); if (document.isNull()) return; web_documents_.insert(document); WebVector web_forms; document.forms(web_forms); // Log an error message for deprecated attributes, but only the first time // the form is parsed. bool log_deprecation_messages = parsed_forms_.find(&frame) == parsed_forms_.end(); size_t num_fields_seen = 0; 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]; if (log_deprecation_messages) { std::string autocomplete_attribute = base::UTF16ToUTF8(element.getAttribute("autocomplete")); static const char* const deprecated[] = { "region", "locality" }; for (size_t i = 0; i < arraysize(deprecated); ++i) { if (autocomplete_attribute.find(deprecated[i]) != std::string::npos) { WebConsoleMessage console_message = WebConsoleMessage( WebConsoleMessage::LevelWarning, WebString(base::ASCIIToUTF16(std::string("autocomplete='") + deprecated[i] + "' is deprecated and will soon be ignored. " "See http://goo.gl/YjeSsW"))); element.document().frame()->addMessageToConsole(console_message); } } } // Save original values of