This source file includes following definitions.
- IsOptionElement
- IsScriptElement
- IsNoScriptElement
- HasTagName
- IsAutofillableElement
- SatisfiesRequireAutocomplete
- CombineAndCollapseWhitespace
- FindChildTextInner
- FindChildText
- InferLabelFromPrevious
- InferLabelFromListItem
- InferLabelFromTableColumn
- InferLabelFromTableRow
- InferLabelFromDivTable
- InferLabelFromDefinitionList
- InferLabelForElement
- GetOptionStringsFromElement
- ForEachMatchingFormField
- FillFormField
- PreviewFormField
- RetrievalMethodToString
- IsWebNodeVisibleImpl
- IsMonthInput
- IsTextInput
- IsSelectElement
- IsTextAreaElement
- IsCheckableElement
- IsAutofillableInputElement
- GetFormIdentifier
- IsWebNodeVisible
- ClickElement
- ExtractAutofillableElements
- WebFormControlElementToFormField
- WebFormElementToFormData
- FindFormAndFieldForFormControlElement
- FillForm
- FillFormIncludingNonFocusableElements
- FillFormForAllElements
- PreviewForm
- ClearPreviewedFormWithElement
- FormWithElementIsAutofilled
- IsWebpageEmpty
- IsWebElementEmpty
- GetScaledBoundingBox
#include "components/autofill/content/renderer/form_autofill_util.h"
#include <map>
#include "base/command_line.h"
#include "base/logging.h"
#include "base/memory/scoped_vector.h"
#include "base/metrics/field_trial.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "components/autofill/core/common/autofill_data_validation.h"
#include "components/autofill/core/common/autofill_switches.h"
#include "components/autofill/core/common/form_data.h"
#include "components/autofill/core/common/form_field_data.h"
#include "components/autofill/core/common/web_element_descriptor.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/WebElement.h"
#include "third_party/WebKit/public/web/WebElementCollection.h"
#include "third_party/WebKit/public/web/WebExceptionCode.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/WebLabelElement.h"
#include "third_party/WebKit/public/web/WebNode.h"
#include "third_party/WebKit/public/web/WebNodeList.h"
#include "third_party/WebKit/public/web/WebOptionElement.h"
#include "third_party/WebKit/public/web/WebSelectElement.h"
#include "third_party/WebKit/public/web/WebTextAreaElement.h"
using blink::WebDocument;
using blink::WebElement;
using blink::WebElementCollection;
using blink::WebExceptionCode;
using blink::WebFormControlElement;
using blink::WebFormElement;
using blink::WebFrame;
using blink::WebInputElement;
using blink::WebLabelElement;
using blink::WebNode;
using blink::WebNodeList;
using blink::WebOptionElement;
using blink::WebSelectElement;
using blink::WebTextAreaElement;
using blink::WebString;
using blink::WebVector;
namespace autofill {
namespace {
enum FieldFilterMask {
FILTER_NONE = 0,
FILTER_DISABLED_ELEMENTS = 1 << 0,
FILTER_READONLY_ELEMENTS = 1 << 1,
FILTER_NON_FOCUSABLE_ELEMENTS = 1 << 2,
FILTER_ALL_NON_EDITIABLE_ELEMENTS = FILTER_DISABLED_ELEMENTS |
FILTER_READONLY_ELEMENTS |
FILTER_NON_FOCUSABLE_ELEMENTS,
};
bool IsOptionElement(const WebElement& element) {
CR_DEFINE_STATIC_LOCAL(WebString, kOption, ("option"));
return element.hasTagName(kOption);
}
bool IsScriptElement(const WebElement& element) {
CR_DEFINE_STATIC_LOCAL(WebString, kScript, ("script"));
return element.hasTagName(kScript);
}
bool IsNoScriptElement(const WebElement& element) {
CR_DEFINE_STATIC_LOCAL(WebString, kNoScript, ("noscript"));
return element.hasTagName(kNoScript);
}
bool HasTagName(const WebNode& node, const blink::WebString& tag) {
return node.isElementNode() && node.toConst<WebElement>().hasHTMLTagName(tag);
}
bool IsAutofillableElement(const WebFormControlElement& element) {
const WebInputElement* input_element = toWebInputElement(&element);
return IsAutofillableInputElement(input_element) ||
IsSelectElement(element) ||
IsTextAreaElement(element);
}
bool SatisfiesRequireAutocomplete(const WebInputElement& input_element) {
return input_element.autoComplete();
}
const base::string16 CombineAndCollapseWhitespace(
const base::string16& prefix,
const base::string16& suffix,
bool force_whitespace) {
base::string16 prefix_trimmed;
base::TrimPositions prefix_trailing_whitespace =
base::TrimWhitespace(prefix, base::TRIM_TRAILING, &prefix_trimmed);
base::string16 suffix_trimmed;
base::TrimPositions suffix_leading_whitespace =
base::TrimWhitespace(suffix, base::TRIM_LEADING, &suffix_trimmed);
if (prefix_trailing_whitespace || suffix_leading_whitespace ||
force_whitespace) {
return prefix_trimmed + base::ASCIIToUTF16(" ") + suffix_trimmed;
} else {
return prefix_trimmed + suffix_trimmed;
}
}
base::string16 FindChildTextInner(const WebNode& node, int depth) {
if (depth <= 0 || node.isNull())
return base::string16();
if (node.nodeType() == WebNode::CommentNode)
return FindChildTextInner(node.nextSibling(), depth - 1);
if (node.nodeType() != WebNode::ElementNode &&
node.nodeType() != WebNode::TextNode)
return base::string16();
if (node.isElementNode()) {
const WebElement element = node.toConst<WebElement>();
if (IsOptionElement(element) ||
IsScriptElement(element) ||
IsNoScriptElement(element) ||
(element.isFormControlElement() &&
IsAutofillableElement(element.toConst<WebFormControlElement>()))) {
return base::string16();
}
}
base::string16 node_text = node.nodeValue();
base::string16 child_text = FindChildTextInner(node.firstChild(), depth - 1);
bool add_space = node.nodeType() == WebNode::TextNode && node_text.empty();
node_text = CombineAndCollapseWhitespace(node_text, child_text, add_space);
base::string16 sibling_text =
FindChildTextInner(node.nextSibling(), depth - 1);
add_space = node.nodeType() == WebNode::TextNode && node_text.empty();
node_text = CombineAndCollapseWhitespace(node_text, sibling_text, add_space);
return node_text;
}
base::string16 FindChildText(const WebNode& node) {
if (node.isTextNode())
return node.nodeValue();
WebNode child = node.firstChild();
const int kChildSearchDepth = 10;
base::string16 node_text = FindChildTextInner(child, kChildSearchDepth);
base::TrimWhitespace(node_text, base::TRIM_ALL, &node_text);
return node_text;
}
base::string16 InferLabelFromPrevious(const WebFormControlElement& element) {
base::string16 inferred_label;
WebNode previous = element;
while (true) {
previous = previous.previousSibling();
if (previous.isNull())
break;
WebNode::NodeType node_type = previous.nodeType();
if (node_type == WebNode::CommentNode)
continue;
if (node_type != WebNode::TextNode &&
node_type != WebNode::ElementNode)
break;
CR_DEFINE_STATIC_LOCAL(WebString, kBold, ("b"));
CR_DEFINE_STATIC_LOCAL(WebString, kStrong, ("strong"));
CR_DEFINE_STATIC_LOCAL(WebString, kSpan, ("span"));
CR_DEFINE_STATIC_LOCAL(WebString, kFont, ("font"));
if (previous.isTextNode() ||
HasTagName(previous, kBold) || HasTagName(previous, kStrong) ||
HasTagName(previous, kSpan) || HasTagName(previous, kFont)) {
base::string16 value = FindChildText(previous);
bool add_space = previous.isTextNode() && value.empty();
inferred_label =
CombineAndCollapseWhitespace(value, inferred_label, add_space);
continue;
}
base::string16 trimmed_label;
base::TrimWhitespace(inferred_label, base::TRIM_ALL, &trimmed_label);
if (!trimmed_label.empty())
break;
CR_DEFINE_STATIC_LOCAL(WebString, kImage, ("img"));
CR_DEFINE_STATIC_LOCAL(WebString, kBreak, ("br"));
if (HasTagName(previous, kImage) || HasTagName(previous, kBreak))
continue;
CR_DEFINE_STATIC_LOCAL(WebString, kPage, ("p"));
CR_DEFINE_STATIC_LOCAL(WebString, kLabel, ("label"));
if (HasTagName(previous, kPage) || HasTagName(previous, kLabel))
inferred_label = FindChildText(previous);
break;
}
base::TrimWhitespace(inferred_label, base::TRIM_ALL, &inferred_label);
return inferred_label;
}
base::string16 InferLabelFromListItem(const WebFormControlElement& element) {
WebNode parent = element.parentNode();
CR_DEFINE_STATIC_LOCAL(WebString, kListItem, ("li"));
while (!parent.isNull() && parent.isElementNode() &&
!parent.to<WebElement>().hasTagName(kListItem)) {
parent = parent.parentNode();
}
if (!parent.isNull() && HasTagName(parent, kListItem))
return FindChildText(parent);
return base::string16();
}
base::string16 InferLabelFromTableColumn(const WebFormControlElement& element) {
CR_DEFINE_STATIC_LOCAL(WebString, kTableCell, ("td"));
WebNode parent = element.parentNode();
while (!parent.isNull() && parent.isElementNode() &&
!parent.to<WebElement>().hasTagName(kTableCell)) {
parent = parent.parentNode();
}
if (parent.isNull())
return base::string16();
base::string16 inferred_label;
WebNode previous = parent.previousSibling();
CR_DEFINE_STATIC_LOCAL(WebString, kTableHeader, ("th"));
while (inferred_label.empty() && !previous.isNull()) {
if (HasTagName(previous, kTableCell) || HasTagName(previous, kTableHeader))
inferred_label = FindChildText(previous);
previous = previous.previousSibling();
}
return inferred_label;
}
base::string16 InferLabelFromTableRow(const WebFormControlElement& element) {
CR_DEFINE_STATIC_LOCAL(WebString, kTableRow, ("tr"));
WebNode parent = element.parentNode();
while (!parent.isNull() && parent.isElementNode() &&
!parent.to<WebElement>().hasTagName(kTableRow)) {
parent = parent.parentNode();
}
if (parent.isNull())
return base::string16();
base::string16 inferred_label;
WebNode previous = parent.previousSibling();
while (inferred_label.empty() && !previous.isNull()) {
if (HasTagName(previous, kTableRow))
inferred_label = FindChildText(previous);
previous = previous.previousSibling();
}
return inferred_label;
}
base::string16 InferLabelFromDivTable(const WebFormControlElement& element) {
WebNode node = element.parentNode();
bool looking_for_parent = true;
base::string16 inferred_label;
CR_DEFINE_STATIC_LOCAL(WebString, kDiv, ("div"));
CR_DEFINE_STATIC_LOCAL(WebString, kTable, ("table"));
CR_DEFINE_STATIC_LOCAL(WebString, kFieldSet, ("fieldset"));
while (inferred_label.empty() && !node.isNull()) {
if (HasTagName(node, kDiv)) {
looking_for_parent = false;
inferred_label = FindChildText(node);
} else if (looking_for_parent &&
(HasTagName(node, kTable) || HasTagName(node, kFieldSet))) {
break;
}
if (node.previousSibling().isNull()) {
looking_for_parent = true;
}
if (looking_for_parent)
node = node.parentNode();
else
node = node.previousSibling();
}
return inferred_label;
}
base::string16 InferLabelFromDefinitionList(
const WebFormControlElement& element) {
CR_DEFINE_STATIC_LOCAL(WebString, kDefinitionData, ("dd"));
WebNode parent = element.parentNode();
while (!parent.isNull() && parent.isElementNode() &&
!parent.to<WebElement>().hasTagName(kDefinitionData))
parent = parent.parentNode();
if (parent.isNull() || !HasTagName(parent, kDefinitionData))
return base::string16();
WebNode previous = parent.previousSibling();
while (!previous.isNull() && previous.isTextNode())
previous = previous.previousSibling();
CR_DEFINE_STATIC_LOCAL(WebString, kDefinitionTag, ("dt"));
if (previous.isNull() || !HasTagName(previous, kDefinitionTag))
return base::string16();
return FindChildText(previous);
}
base::string16 InferLabelForElement(const WebFormControlElement& element) {
base::string16 inferred_label = InferLabelFromPrevious(element);
if (!inferred_label.empty())
return inferred_label;
inferred_label = InferLabelFromListItem(element);
if (!inferred_label.empty())
return inferred_label;
inferred_label = InferLabelFromTableColumn(element);
if (!inferred_label.empty())
return inferred_label;
inferred_label = InferLabelFromTableRow(element);
if (!inferred_label.empty())
return inferred_label;
inferred_label = InferLabelFromDefinitionList(element);
if (!inferred_label.empty())
return inferred_label;
return InferLabelFromDivTable(element);
}
void GetOptionStringsFromElement(const WebSelectElement& select_element,
std::vector<base::string16>* option_values,
std::vector<base::string16>* option_contents) {
DCHECK(!select_element.isNull());
option_values->clear();
option_contents->clear();
WebVector<WebElement> list_items = select_element.listItems();
option_values->reserve(list_items.size());
option_contents->reserve(list_items.size());
for (size_t i = 0; i < list_items.size(); ++i) {
if (IsOptionElement(list_items[i])) {
const WebOptionElement option = list_items[i].toConst<WebOptionElement>();
option_values->push_back(option.value());
option_contents->push_back(option.text());
}
}
}
typedef void (*Callback)(const FormFieldData&,
bool,
blink::WebFormControlElement*);
void ForEachMatchingFormField(const WebFormElement& form_element,
const WebElement& initiating_element,
const FormData& data,
FieldFilterMask filters,
bool force_override,
Callback callback) {
std::vector<WebFormControlElement> control_elements;
ExtractAutofillableElements(form_element, REQUIRE_AUTOCOMPLETE,
&control_elements);
if (control_elements.size() != data.fields.size()) {
return;
}
for (size_t i = 0; i < control_elements.size(); ++i) {
WebFormControlElement* element = &control_elements[i];
if (base::string16(element->nameForAutofill()) != data.fields[i].name) {
NOTREACHED();
continue;
}
bool is_initiating_element = (*element == initiating_element);
const WebInputElement* input_element = toWebInputElement(element);
if (!force_override && !is_initiating_element &&
((IsAutofillableInputElement(input_element) ||
IsTextAreaElement(*element)) &&
!element->value().isEmpty()))
continue;
if (((filters & FILTER_DISABLED_ELEMENTS) && !element->isEnabled()) ||
((filters & FILTER_READONLY_ELEMENTS) && element->isReadOnly()) ||
((filters & FILTER_NON_FOCUSABLE_ELEMENTS) && !element->isFocusable()))
continue;
callback(data.fields[i], is_initiating_element, element);
}
}
void FillFormField(const FormFieldData& data,
bool is_initiating_node,
blink::WebFormControlElement* field) {
if (data.value.empty())
return;
field->setAutofilled(true);
WebInputElement* input_element = toWebInputElement(field);
if (IsCheckableElement(input_element)) {
input_element->setChecked(data.is_checked, true);
} else {
base::string16 value = data.value;
if (IsTextInput(input_element) || IsMonthInput(input_element)) {
value = value.substr(0, input_element->maxLength());
}
field->setValue(value, true);
}
if (is_initiating_node &&
((IsTextInput(input_element) || IsMonthInput(input_element)) ||
IsTextAreaElement(*field))) {
int length = field->value().length();
field->setSelectionRange(length, length);
field->document().frame()->unmarkText();
}
}
void PreviewFormField(const FormFieldData& data,
bool is_initiating_node,
blink::WebFormControlElement* field) {
if (data.value.empty())
return;
WebInputElement* input_element = toWebInputElement(field);
if (IsTextInput(input_element) || IsMonthInput(input_element)) {
input_element->setSuggestedValue(
data.value.substr(0, input_element->maxLength()));
input_element->setAutofilled(true);
} else if (IsTextAreaElement(*field) || IsSelectElement(*field)) {
field->setSuggestedValue(data.value);
field->setAutofilled(true);
}
if (is_initiating_node &&
(IsTextInput(input_element) || IsTextAreaElement(*field))) {
int start = field->value().length();
int end = field->suggestedValue().length();
field->setSelectionRange(start, end);
}
}
std::string RetrievalMethodToString(
const WebElementDescriptor::RetrievalMethod& method) {
switch (method) {
case WebElementDescriptor::CSS_SELECTOR:
return "CSS_SELECTOR";
case WebElementDescriptor::ID:
return "ID";
case WebElementDescriptor::NONE:
return "NONE";
}
NOTREACHED();
return "UNKNOWN";
}
bool IsWebNodeVisibleImpl(const blink::WebNode& node, const int depth) {
if (depth < 0)
return false;
if (node.hasNonEmptyBoundingBox())
return true;
const blink::WebNodeList& children =
const_cast<blink::WebNode&>(node).childNodes();
size_t length = children.length();
for (size_t i = 0; i < length; ++i) {
const blink::WebNode& item = children.item(i);
if (IsWebNodeVisibleImpl(item, depth - 1))
return true;
}
return false;
}
}
const size_t kMaxParseableFields = 200;
bool IsMonthInput(const WebInputElement* element) {
CR_DEFINE_STATIC_LOCAL(WebString, kMonth, ("month"));
return element && element->formControlType() == kMonth;
}
bool IsTextInput(const WebInputElement* element) {
return element && element->isTextField();
}
bool IsSelectElement(const WebFormControlElement& element) {
CR_DEFINE_STATIC_LOCAL(WebString, kSelectOne, ("select-one"));
return element.formControlType() == kSelectOne;
}
bool IsTextAreaElement(const WebFormControlElement& element) {
CR_DEFINE_STATIC_LOCAL(WebString, kTextArea, ("textarea"));
return element.formControlType() == kTextArea;
}
bool IsCheckableElement(const WebInputElement* element) {
if (!element)
return false;
return element->isCheckbox() || element->isRadioButton();
}
bool IsAutofillableInputElement(const WebInputElement* element) {
return IsTextInput(element) ||
IsMonthInput(element) ||
IsCheckableElement(element);
}
const base::string16 GetFormIdentifier(const WebFormElement& form) {
base::string16 identifier = form.name();
CR_DEFINE_STATIC_LOCAL(WebString, kId, ("id"));
if (identifier.empty())
identifier = form.getAttribute(kId);
return identifier;
}
bool IsWebNodeVisible(const blink::WebNode& node) {
int kNodeSearchDepth = 2;
return IsWebNodeVisibleImpl(node, kNodeSearchDepth);
}
bool ClickElement(const WebDocument& document,
const WebElementDescriptor& element_descriptor) {
WebString web_descriptor = WebString::fromUTF8(element_descriptor.descriptor);
blink::WebElement element;
switch (element_descriptor.retrieval_method) {
case WebElementDescriptor::CSS_SELECTOR: {
WebExceptionCode ec = 0;
element = document.querySelector(web_descriptor, ec);
if (ec)
DVLOG(1) << "Query selector failed. Error code: " << ec << ".";
break;
}
case WebElementDescriptor::ID:
element = document.getElementById(web_descriptor);
break;
case WebElementDescriptor::NONE:
return true;
}
if (element.isNull()) {
DVLOG(1) << "Could not find "
<< element_descriptor.descriptor
<< " by "
<< RetrievalMethodToString(element_descriptor.retrieval_method)
<< ".";
return false;
}
element.simulateClick();
return true;
}
void ExtractAutofillableElements(
const WebFormElement& form_element,
RequirementsMask requirements,
std::vector<WebFormControlElement>* autofillable_elements) {
WebVector<WebFormControlElement> control_elements;
form_element.getFormControlElements(control_elements);
autofillable_elements->clear();
for (size_t i = 0; i < control_elements.size(); ++i) {
WebFormControlElement element = control_elements[i];
if (!IsAutofillableElement(element))
continue;
if (requirements & REQUIRE_AUTOCOMPLETE) {
WebInputElement* input_element = toWebInputElement(&control_elements[i]);
if (IsAutofillableInputElement(input_element) &&
!SatisfiesRequireAutocomplete(*input_element))
continue;
}
autofillable_elements->push_back(element);
}
}
void WebFormControlElementToFormField(const WebFormControlElement& element,
ExtractMask extract_mask,
FormFieldData* field) {
DCHECK(field);
DCHECK(!element.isNull());
CR_DEFINE_STATIC_LOCAL(WebString, kAutocomplete, ("autocomplete"));
field->name = element.nameForAutofill();
field->form_control_type = base::UTF16ToUTF8(element.formControlType());
field->autocomplete_attribute =
base::UTF16ToUTF8(element.getAttribute(kAutocomplete));
if (field->autocomplete_attribute.size() > kMaxDataLength) {
field->autocomplete_attribute = "x-max-data-length-exceeded";
}
if (!IsAutofillableElement(element))
return;
const WebInputElement* input_element = toWebInputElement(&element);
if (IsAutofillableInputElement(input_element) ||
IsTextAreaElement(element)) {
field->is_autofilled = element.isAutofilled();
field->is_focusable = element.isFocusable();
field->should_autocomplete = element.autoComplete();
field->text_direction = element.directionForFormData() ==
"rtl" ? base::i18n::RIGHT_TO_LEFT : base::i18n::LEFT_TO_RIGHT;
}
if (IsAutofillableInputElement(input_element)) {
if (IsTextInput(input_element))
field->max_length = input_element->maxLength();
field->is_checkable = IsCheckableElement(input_element);
field->is_checked = input_element->isChecked();
} else if (IsTextAreaElement(element)) {
} else if (extract_mask & EXTRACT_OPTIONS) {
DCHECK(IsSelectElement(element));
const WebSelectElement select_element = element.toConst<WebSelectElement>();
GetOptionStringsFromElement(select_element,
&field->option_values,
&field->option_contents);
}
if (!(extract_mask & EXTRACT_VALUE))
return;
base::string16 value = element.value();
if (IsSelectElement(element)) {
const WebSelectElement select_element = element.toConst<WebSelectElement>();
if (extract_mask & EXTRACT_OPTION_TEXT) {
WebVector<WebElement> list_items = select_element.listItems();
for (size_t i = 0; i < list_items.size(); ++i) {
if (IsOptionElement(list_items[i])) {
const WebOptionElement option_element =
list_items[i].toConst<WebOptionElement>();
if (option_element.value() == value) {
value = option_element.text();
break;
}
}
}
}
}
if (value.size() > kMaxDataLength)
value = value.substr(0, kMaxDataLength);
field->value = value;
}
bool WebFormElementToFormData(
const blink::WebFormElement& form_element,
const blink::WebFormControlElement& form_control_element,
RequirementsMask requirements,
ExtractMask extract_mask,
FormData* form,
FormFieldData* field) {
CR_DEFINE_STATIC_LOCAL(WebString, kLabel, ("label"));
CR_DEFINE_STATIC_LOCAL(WebString, kFor, ("for"));
CR_DEFINE_STATIC_LOCAL(WebString, kHidden, ("hidden"));
const WebFrame* frame = form_element.document().frame();
if (!frame)
return false;
if (requirements & REQUIRE_AUTOCOMPLETE && !form_element.autoComplete())
return false;
form->name = GetFormIdentifier(form_element);
form->method = form_element.method();
form->origin = frame->document().url();
form->action = frame->document().completeURL(form_element.action());
form->user_submitted = form_element.wasUserSubmitted();
if (!form->action.is_valid())
form->action = GURL(form_element.action());
std::map<base::string16, FormFieldData*> name_map;
ScopedVector<FormFieldData> form_fields;
WebVector<WebFormControlElement> control_elements;
form_element.getFormControlElements(control_elements);
std::vector<bool> fields_extracted(control_elements.size(), false);
for (size_t i = 0; i < control_elements.size(); ++i) {
const WebFormControlElement& control_element = control_elements[i];
if (!IsAutofillableElement(control_element))
continue;
const WebInputElement* input_element = toWebInputElement(&control_element);
if (requirements & REQUIRE_AUTOCOMPLETE &&
IsAutofillableInputElement(input_element) &&
!SatisfiesRequireAutocomplete(*input_element))
continue;
FormFieldData* form_field = new FormFieldData;
WebFormControlElementToFormField(control_element, extract_mask, form_field);
form_fields.push_back(form_field);
name_map[form_field->name] = form_field;
fields_extracted[i] = true;
}
if (form_fields.empty() || form_fields.size() > kMaxParseableFields)
return false;
WebElementCollection labels = form_element.getElementsByTagName(kLabel);
DCHECK(!labels.isNull());
for (WebElement item = labels.firstItem(); !item.isNull();
item = labels.nextItem()) {
WebLabelElement label = item.to<WebLabelElement>();
WebFormControlElement field_element =
label.correspondingControl().to<WebFormControlElement>();
base::string16 element_name;
if (field_element.isNull()) {
element_name = label.getAttribute(kFor);
} else if (
!field_element.isFormControlElement() ||
field_element.formControlType() == kHidden) {
continue;
} else {
element_name = field_element.nameForAutofill();
}
std::map<base::string16, FormFieldData*>::iterator iter =
name_map.find(element_name);
if (iter != name_map.end()) {
base::string16 label_text = FindChildText(label);
if (!iter->second->label.empty() && !label_text.empty())
iter->second->label += base::ASCIIToUTF16(" ");
iter->second->label += label_text;
}
}
for (size_t i = 0, field_idx = 0;
i < control_elements.size() && field_idx < form_fields.size(); ++i) {
if (!fields_extracted[i])
continue;
const WebFormControlElement& control_element = control_elements[i];
if (form_fields[field_idx]->label.empty())
form_fields[field_idx]->label = InferLabelForElement(control_element);
if (field && form_control_element == control_element)
*field = *form_fields[field_idx];
++field_idx;
}
for (ScopedVector<FormFieldData>::const_iterator iter = form_fields.begin();
iter != form_fields.end(); ++iter) {
form->fields.push_back(**iter);
}
return true;
}
bool FindFormAndFieldForFormControlElement(const WebFormControlElement& element,
FormData* form,
FormFieldData* field,
RequirementsMask requirements) {
if (!IsAutofillableElement(element))
return false;
const WebFormElement form_element = element.form();
if (form_element.isNull())
return false;
ExtractMask extract_mask =
static_cast<ExtractMask>(EXTRACT_VALUE | EXTRACT_OPTIONS);
return WebFormElementToFormData(form_element,
element,
requirements,
extract_mask,
form,
field);
}
void FillForm(const FormData& form, const WebFormControlElement& element) {
WebFormElement form_element = element.form();
if (form_element.isNull())
return;
ForEachMatchingFormField(form_element,
element,
form,
FILTER_ALL_NON_EDITIABLE_ELEMENTS,
false,
&FillFormField);
}
void FillFormIncludingNonFocusableElements(const FormData& form_data,
const WebFormElement& form_element) {
if (form_element.isNull())
return;
FieldFilterMask filter_mask = static_cast<FieldFilterMask>(
FILTER_DISABLED_ELEMENTS | FILTER_READONLY_ELEMENTS);
ForEachMatchingFormField(form_element,
WebInputElement(),
form_data,
filter_mask,
true,
&FillFormField);
}
void FillFormForAllElements(const FormData& form_data,
const WebFormElement& form_element) {
if (form_element.isNull())
return;
ForEachMatchingFormField(form_element,
WebInputElement(),
form_data,
FILTER_NONE,
true,
&FillFormField);
}
void PreviewForm(const FormData& form, const WebFormControlElement& element) {
WebFormElement form_element = element.form();
if (form_element.isNull())
return;
ForEachMatchingFormField(form_element,
element,
form,
FILTER_ALL_NON_EDITIABLE_ELEMENTS,
false,
&PreviewFormField);
}
bool ClearPreviewedFormWithElement(const WebFormControlElement& element,
bool was_autofilled) {
WebFormElement form_element = element.form();
if (form_element.isNull())
return false;
std::vector<WebFormControlElement> control_elements;
ExtractAutofillableElements(form_element, REQUIRE_AUTOCOMPLETE,
&control_elements);
for (size_t i = 0; i < control_elements.size(); ++i) {
WebFormControlElement control_element = control_elements[i];
WebInputElement* input_element = toWebInputElement(&control_element);
if (!IsTextInput(input_element) &&
!IsMonthInput(input_element) &&
!IsTextAreaElement(control_element) &&
!IsSelectElement(control_element))
continue;
if(!control_element.isAutofilled())
continue;
if ((IsTextInput(input_element) ||
IsMonthInput(input_element) ||
IsTextAreaElement(control_element) ||
IsSelectElement(control_element)) &&
control_element.suggestedValue().isEmpty())
continue;
if (IsTextInput(input_element) || IsMonthInput(input_element) ||
IsTextAreaElement(control_element)) {
control_element.setSuggestedValue(WebString());
bool is_initiating_node = (element == control_element);
if (is_initiating_node) {
control_element.setAutofilled(was_autofilled);
int length = control_element.value().length();
control_element.setSelectionRange(length, length);
} else {
control_element.setAutofilled(false);
}
} else if (IsSelectElement(control_element)) {
control_element.setSuggestedValue(WebString());
control_element.setAutofilled(false);
}
}
return true;
}
bool FormWithElementIsAutofilled(const WebInputElement& element) {
WebFormElement form_element = element.form();
if (form_element.isNull())
return false;
std::vector<WebFormControlElement> control_elements;
ExtractAutofillableElements(form_element, REQUIRE_AUTOCOMPLETE,
&control_elements);
for (size_t i = 0; i < control_elements.size(); ++i) {
WebInputElement* input_element = toWebInputElement(&control_elements[i]);
if (!IsAutofillableInputElement(input_element))
continue;
if (input_element->isAutofilled())
return true;
}
return false;
}
bool IsWebpageEmpty(const blink::WebFrame* frame) {
blink::WebDocument document = frame->document();
return IsWebElementEmpty(document.head()) &&
IsWebElementEmpty(document.body());
}
bool IsWebElementEmpty(const blink::WebElement& element) {
const char* const kAllowedValue[] = {
"script",
"meta",
"title",
};
const size_t kAllowedValueLength = arraysize(kAllowedValue);
if (element.isNull())
return true;
const blink::WebNodeList& children =
const_cast<blink::WebElement&>(element).childNodes();
for (size_t i = 0; i < children.length(); ++i) {
const blink::WebNode& item = children.item(i);
if (item.isTextNode() &&
!base::ContainsOnlyChars(item.nodeValue().utf8(),
base::kWhitespaceASCII))
return false;
if (item.nodeName().utf8()[0] == '#')
continue;
bool tag_is_allowed = false;
for (size_t allowed_value_index = 0;
allowed_value_index < kAllowedValueLength; ++allowed_value_index) {
if (HasTagName(item,
WebString::fromUTF8(kAllowedValue[allowed_value_index]))) {
tag_is_allowed = true;
break;
}
}
if (!tag_is_allowed)
return false;
}
return true;
}
gfx::RectF GetScaledBoundingBox(float scale, WebFormControlElement* element) {
gfx::Rect bounding_box(element->boundsInViewportSpace());
return gfx::RectF(bounding_box.x() * scale,
bounding_box.y() * scale,
bounding_box.width() * scale,
bounding_box.height() * scale);
}
}