Commit 725ec64d authored by Matthias Körber's avatar Matthias Körber Committed by Commit Bot

Exchanged storing WebFormElements and WebFormControlElements by storing only...

Exchanged storing WebFormElements and WebFormControlElements by storing only their unique renderer ids.

Change-Id: I9ca873a285d327001e4f29a828c0eb02e97fe48d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1663414
Commit-Queue: Matthias Körber <koerber@google.com>
Reviewed-by: default avatarVadym Doroshenko <dvadym@chromium.org>
Cr-Commit-Position: refs/heads/master@{#676070}
parent c3d1a530
...@@ -4,7 +4,10 @@ ...@@ -4,7 +4,10 @@
#include "components/autofill/content/renderer/page_passwords_analyser.h" #include "components/autofill/content/renderer/page_passwords_analyser.h"
#include <map>
#include <stack> #include <stack>
#include <string>
#include <vector>
#include "base/lazy_instance.h" #include "base/lazy_instance.h"
#include "base/strings/stringprintf.h" #include "base/strings/stringprintf.h"
...@@ -111,7 +114,8 @@ struct InputHint { ...@@ -111,7 +114,8 @@ struct InputHint {
const re2::RE2* regex; const re2::RE2* regex;
size_t match; size_t match;
InputHint(const re2::RE2* regex) : regex(regex), match(std::string::npos) {} explicit InputHint(const re2::RE2* regex)
: regex(regex), match(std::string::npos) {}
void MatchLabel(std::string& label_content, size_t index) { void MatchLabel(std::string& label_content, size_t index) {
if (re2::RE2::FullMatch(label_content, *regex)) if (re2::RE2::FullMatch(label_content, *regex))
...@@ -151,15 +155,15 @@ void TrackElementId( ...@@ -151,15 +155,15 @@ void TrackElementId(
// example if an invalid attribute is added), but these cases are assumed // example if an invalid attribute is added), but these cases are assumed
// to be rare, and are ignored for the sake of simplicity. // to be rare, and are ignored for the sake of simplicity.
// The id of |node| will additionally be added to the corresponding |ids| set. // The id of |node| will additionally be added to the corresponding |ids| set.
bool TrackElementIfUntracked( bool TrackElementByRendererIdIfUntracked(
const blink::WebElement& node, const blink::WebElement& element,
std::set<blink::WebNode>* skip_nodes, const uint32_t renderer_id,
std::set<uint32_t>* skip_renderer_ids,
std::map<std::string, std::vector<blink::WebNode>>* nodes_for_id) { std::map<std::string, std::vector<blink::WebNode>>* nodes_for_id) {
if (skip_nodes->count(node)) if (skip_renderer_ids->count(renderer_id))
return true; return true;
skip_nodes->insert(node); skip_renderer_ids->insert(renderer_id);
// If we don't skip the node, we want to make sure its id is tracked. TrackElementId(element, nodes_for_id);
TrackElementId(node, nodes_for_id);
return false; return false;
} }
...@@ -168,7 +172,8 @@ bool TrackElementIfUntracked( ...@@ -168,7 +172,8 @@ bool TrackElementIfUntracked(
// analysis. // analysis.
std::vector<FormInputCollection> ExtractFormsForAnalysis( std::vector<FormInputCollection> ExtractFormsForAnalysis(
const blink::WebDocument& document, const blink::WebDocument& document,
std::set<blink::WebNode>* skip_nodes, std::set<uint32_t>* skip_form_ids,
std::set<uint32_t>* skip_control_ids,
PageFormAnalyserLogger* logger) { PageFormAnalyserLogger* logger) {
std::vector<FormInputCollection> form_input_collections; std::vector<FormInputCollection> form_input_collections;
...@@ -185,7 +190,9 @@ std::vector<FormInputCollection> ExtractFormsForAnalysis( ...@@ -185,7 +190,9 @@ std::vector<FormInputCollection> ExtractFormsForAnalysis(
blink::WebVector<blink::WebFormControlElement> form_control_elements; blink::WebVector<blink::WebFormControlElement> form_control_elements;
form.GetFormControlElements(form_control_elements); form.GetFormControlElements(form_control_elements);
for (const blink::WebFormControlElement& input : form_control_elements) { for (const blink::WebFormControlElement& input : form_control_elements) {
if (TrackElementIfUntracked(input, skip_nodes, &nodes_for_id)) if (TrackElementByRendererIdIfUntracked(
input, input.UniqueRendererFormControlId(), skip_control_ids,
&nodes_for_id))
continue; continue;
// We are only interested in a subset of input elements -- those likely // We are only interested in a subset of input elements -- those likely
// to be username or password fields. // to be username or password fields.
...@@ -197,14 +204,18 @@ std::vector<FormInputCollection> ExtractFormsForAnalysis( ...@@ -197,14 +204,18 @@ std::vector<FormInputCollection> ExtractFormsForAnalysis(
inputs_with_forms.insert(input); inputs_with_forms.insert(input);
} }
} }
TrackElementByRendererIdIfUntracked(form, form.UniqueRendererFormId(),
TrackElementIfUntracked(form, skip_nodes, &nodes_for_id); skip_form_ids, &nodes_for_id);
} }
// Check for password fields that are not contained inside forms. // Check for password fields that are not contained inside forms.
auto password_inputs = document.QuerySelectorAll("input[type=\"password\"]"); auto password_inputs = document.QuerySelectorAll("input[type=\"password\"]");
for (unsigned i = 0; i < password_inputs.size(); ++i) { for (unsigned i = 0; i < password_inputs.size(); ++i) {
if (TrackElementIfUntracked(password_inputs[i], skip_nodes, &nodes_for_id)) if (TrackElementByRendererIdIfUntracked(
password_inputs[i],
blink::ToWebInputElement(&password_inputs[i])
->UniqueRendererFormControlId(),
skip_control_ids, &nodes_for_id))
continue; continue;
// Any password fields inside <form> elements will have been skipped, // Any password fields inside <form> elements will have been skipped,
// leaving just those without associated forms. // leaving just those without associated forms.
...@@ -221,7 +232,10 @@ std::vector<FormInputCollection> ExtractFormsForAnalysis( ...@@ -221,7 +232,10 @@ std::vector<FormInputCollection> ExtractFormsForAnalysis(
auto text_inputs = auto text_inputs =
document.QuerySelectorAll(blink::WebString::FromUTF8(selector)); document.QuerySelectorAll(blink::WebString::FromUTF8(selector));
for (const blink::WebElement& text_input : text_inputs) for (const blink::WebElement& text_input : text_inputs)
TrackElementIfUntracked(text_input, skip_nodes, &nodes_for_id); TrackElementByRendererIdIfUntracked(
text_input,
blink::ToWebInputElement(&text_input)->UniqueRendererFormControlId(),
skip_control_ids, &nodes_for_id);
// Warn against elements sharing an id attribute. Duplicate id attributes both // Warn against elements sharing an id attribute. Duplicate id attributes both
// are against the HTML specification and can cause issues with password // are against the HTML specification and can cause issues with password
...@@ -419,7 +433,8 @@ PagePasswordsAnalyser::PagePasswordsAnalyser() {} ...@@ -419,7 +433,8 @@ PagePasswordsAnalyser::PagePasswordsAnalyser() {}
PagePasswordsAnalyser::~PagePasswordsAnalyser() {} PagePasswordsAnalyser::~PagePasswordsAnalyser() {}
void PagePasswordsAnalyser::Reset() { void PagePasswordsAnalyser::Reset() {
skip_nodes_.clear(); skip_control_element_renderer_ids_.clear();
skip_form_element_renderer_ids_.clear();
} }
void PagePasswordsAnalyser::AnalyseDocumentDOM(blink::WebLocalFrame* frame, void PagePasswordsAnalyser::AnalyseDocumentDOM(blink::WebLocalFrame* frame,
...@@ -429,7 +444,8 @@ void PagePasswordsAnalyser::AnalyseDocumentDOM(blink::WebLocalFrame* frame, ...@@ -429,7 +444,8 @@ void PagePasswordsAnalyser::AnalyseDocumentDOM(blink::WebLocalFrame* frame,
blink::WebDocument document(frame->GetDocument()); blink::WebDocument document(frame->GetDocument());
// Extract all the forms from the DOM, and provide relevant warnings. // Extract all the forms from the DOM, and provide relevant warnings.
std::vector<FormInputCollection> forms( std::vector<FormInputCollection> forms(
ExtractFormsForAnalysis(document, &skip_nodes_, logger)); ExtractFormsForAnalysis(document, &skip_form_element_renderer_ids_,
&skip_control_element_renderer_ids_, logger));
// Analyse each form in turn, for example with respect to autocomplete // Analyse each form in turn, for example with respect to autocomplete
// attributes. // attributes.
......
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
#ifndef COMPONENTS_AUTOFILL_CONTENT_RENDERER_PAGE_PASSWORDS_ANALYSER_H_ #ifndef COMPONENTS_AUTOFILL_CONTENT_RENDERER_PAGE_PASSWORDS_ANALYSER_H_
#define COMPONENTS_AUTOFILL_CONTENT_RENDERER_PAGE_PASSWORDS_ANALYSER_H_ #define COMPONENTS_AUTOFILL_CONTENT_RENDERER_PAGE_PASSWORDS_ANALYSER_H_
#include <set>
#include "third_party/blink/public/web/web_console_message.h" #include "third_party/blink/public/web/web_console_message.h"
#include "third_party/blink/public/web/web_local_frame.h" #include "third_party/blink/public/web/web_local_frame.h"
...@@ -40,7 +42,9 @@ class PagePasswordsAnalyser { ...@@ -40,7 +42,9 @@ class PagePasswordsAnalyser {
// By default, the analyser will log to the DevTools console. // By default, the analyser will log to the DevTools console.
void AnalyseDocumentDOM(blink::WebLocalFrame* frame); void AnalyseDocumentDOM(blink::WebLocalFrame* frame);
std::set<blink::WebNode> skip_nodes_; // A set of renderer_ids which have already been analyzed.
std::set<uint32_t> skip_control_element_renderer_ids_;
std::set<uint32_t> skip_form_element_renderer_ids_;
// This is true when new DOM content is available since the last time // This is true when new DOM content is available since the last time
// the page was analysed, meaning the page needs to be reanalysed. // the page was analysed, meaning the page needs to be reanalysed.
bool page_dirty_; bool page_dirty_;
...@@ -48,5 +52,4 @@ class PagePasswordsAnalyser { ...@@ -48,5 +52,4 @@ class PagePasswordsAnalyser {
} // namespace autofill } // namespace autofill
#endif #endif // COMPONENTS_AUTOFILL_CONTENT_RENDERER_PAGE_PASSWORDS_ANALYSER_H_
// COMPONENTS_AUTOFILL_CONTENT_RENDERER_PAGE_PASSWORDS_ANALYSER_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment