Make |track_| in MediaStreamTrack const. and a couple of other cosmetic changes.
[chromium-blink-merge.git] / components / autofill / content / renderer / form_autofill_util.cc
blob88694a72f8f43f7f928e2abbb1bf476a2604f6f9
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
3 // found in the LICENSE file.
5 #include "components/autofill/content/renderer/form_autofill_util.h"
7 #include <map>
9 #include "base/command_line.h"
10 #include "base/logging.h"
11 #include "base/memory/scoped_vector.h"
12 #include "base/metrics/field_trial.h"
13 #include "base/strings/string_util.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "components/autofill/core/common/autofill_data_validation.h"
16 #include "components/autofill/core/common/autofill_switches.h"
17 #include "components/autofill/core/common/form_data.h"
18 #include "components/autofill/core/common/form_field_data.h"
19 #include "components/autofill/core/common/web_element_descriptor.h"
20 #include "third_party/WebKit/public/platform/WebString.h"
21 #include "third_party/WebKit/public/platform/WebVector.h"
22 #include "third_party/WebKit/public/web/WebDocument.h"
23 #include "third_party/WebKit/public/web/WebElement.h"
24 #include "third_party/WebKit/public/web/WebElementCollection.h"
25 #include "third_party/WebKit/public/web/WebExceptionCode.h"
26 #include "third_party/WebKit/public/web/WebFormControlElement.h"
27 #include "third_party/WebKit/public/web/WebFormElement.h"
28 #include "third_party/WebKit/public/web/WebInputElement.h"
29 #include "third_party/WebKit/public/web/WebLabelElement.h"
30 #include "third_party/WebKit/public/web/WebLocalFrame.h"
31 #include "third_party/WebKit/public/web/WebNode.h"
32 #include "third_party/WebKit/public/web/WebNodeList.h"
33 #include "third_party/WebKit/public/web/WebOptionElement.h"
34 #include "third_party/WebKit/public/web/WebSelectElement.h"
35 #include "third_party/WebKit/public/web/WebTextAreaElement.h"
37 using blink::WebDocument;
38 using blink::WebElement;
39 using blink::WebElementCollection;
40 using blink::WebExceptionCode;
41 using blink::WebFormControlElement;
42 using blink::WebFormElement;
43 using blink::WebFrame;
44 using blink::WebInputElement;
45 using blink::WebLabelElement;
46 using blink::WebNode;
47 using blink::WebNodeList;
48 using blink::WebOptionElement;
49 using blink::WebSelectElement;
50 using blink::WebTextAreaElement;
51 using blink::WebString;
52 using blink::WebVector;
54 namespace autofill {
55 namespace {
57 // A bit field mask for FillForm functions to not fill some fields.
58 enum FieldFilterMask {
59 FILTER_NONE = 0,
60 FILTER_DISABLED_ELEMENTS = 1 << 0,
61 FILTER_READONLY_ELEMENTS = 1 << 1,
62 FILTER_NON_FOCUSABLE_ELEMENTS = 1 << 2,
63 FILTER_ALL_NON_EDITABLE_ELEMENTS = FILTER_DISABLED_ELEMENTS |
64 FILTER_READONLY_ELEMENTS |
65 FILTER_NON_FOCUSABLE_ELEMENTS,
68 RequirementsMask ExtractionRequirements() {
69 return base::CommandLine::ForCurrentProcess()->HasSwitch(
70 switches::kIgnoreAutocompleteOffForAutofill)
71 ? REQUIRE_NONE
72 : REQUIRE_AUTOCOMPLETE;
75 bool IsOptionElement(const WebElement& element) {
76 CR_DEFINE_STATIC_LOCAL(WebString, kOption, ("option"));
77 return element.hasHTMLTagName(kOption);
80 bool IsScriptElement(const WebElement& element) {
81 CR_DEFINE_STATIC_LOCAL(WebString, kScript, ("script"));
82 return element.hasHTMLTagName(kScript);
85 bool IsNoScriptElement(const WebElement& element) {
86 CR_DEFINE_STATIC_LOCAL(WebString, kNoScript, ("noscript"));
87 return element.hasHTMLTagName(kNoScript);
90 bool HasTagName(const WebNode& node, const blink::WebString& tag) {
91 return node.isElementNode() && node.toConst<WebElement>().hasHTMLTagName(tag);
94 bool IsAutofillableElement(const WebFormControlElement& element) {
95 const WebInputElement* input_element = toWebInputElement(&element);
96 return IsAutofillableInputElement(input_element) ||
97 IsSelectElement(element) ||
98 IsTextAreaElement(element);
101 // Check whether the given field satisfies the REQUIRE_AUTOCOMPLETE requirement.
102 bool SatisfiesRequireAutocomplete(const WebInputElement& input_element) {
103 return input_element.autoComplete();
106 // Appends |suffix| to |prefix| so that any intermediary whitespace is collapsed
107 // to a single space. If |force_whitespace| is true, then the resulting string
108 // is guaranteed to have a space between |prefix| and |suffix|. Otherwise, the
109 // result includes a space only if |prefix| has trailing whitespace or |suffix|
110 // has leading whitespace.
111 // A few examples:
112 // * CombineAndCollapseWhitespace("foo", "bar", false) -> "foobar"
113 // * CombineAndCollapseWhitespace("foo", "bar", true) -> "foo bar"
114 // * CombineAndCollapseWhitespace("foo ", "bar", false) -> "foo bar"
115 // * CombineAndCollapseWhitespace("foo", " bar", false) -> "foo bar"
116 // * CombineAndCollapseWhitespace("foo", " bar", true) -> "foo bar"
117 // * CombineAndCollapseWhitespace("foo ", " bar", false) -> "foo bar"
118 // * CombineAndCollapseWhitespace(" foo", "bar ", false) -> " foobar "
119 // * CombineAndCollapseWhitespace(" foo", "bar ", true) -> " foo bar "
120 const base::string16 CombineAndCollapseWhitespace(
121 const base::string16& prefix,
122 const base::string16& suffix,
123 bool force_whitespace) {
124 base::string16 prefix_trimmed;
125 base::TrimPositions prefix_trailing_whitespace =
126 base::TrimWhitespace(prefix, base::TRIM_TRAILING, &prefix_trimmed);
128 // Recursively compute the children's text.
129 base::string16 suffix_trimmed;
130 base::TrimPositions suffix_leading_whitespace =
131 base::TrimWhitespace(suffix, base::TRIM_LEADING, &suffix_trimmed);
133 if (prefix_trailing_whitespace || suffix_leading_whitespace ||
134 force_whitespace) {
135 return prefix_trimmed + base::ASCIIToUTF16(" ") + suffix_trimmed;
136 } else {
137 return prefix_trimmed + suffix_trimmed;
141 // This is a helper function for the FindChildText() function (see below).
142 // Search depth is limited with the |depth| parameter.
143 base::string16 FindChildTextInner(const WebNode& node, int depth) {
144 if (depth <= 0 || node.isNull())
145 return base::string16();
147 // Skip over comments.
148 if (node.nodeType() == WebNode::CommentNode)
149 return FindChildTextInner(node.nextSibling(), depth - 1);
151 if (node.nodeType() != WebNode::ElementNode &&
152 node.nodeType() != WebNode::TextNode)
153 return base::string16();
155 // Ignore elements known not to contain inferable labels.
156 if (node.isElementNode()) {
157 const WebElement element = node.toConst<WebElement>();
158 if (IsOptionElement(element) ||
159 IsScriptElement(element) ||
160 IsNoScriptElement(element) ||
161 (element.isFormControlElement() &&
162 IsAutofillableElement(element.toConst<WebFormControlElement>()))) {
163 return base::string16();
167 // Extract the text exactly at this node.
168 base::string16 node_text = node.nodeValue();
170 // Recursively compute the children's text.
171 // Preserve inter-element whitespace separation.
172 base::string16 child_text = FindChildTextInner(node.firstChild(), depth - 1);
173 bool add_space = node.nodeType() == WebNode::TextNode && node_text.empty();
174 node_text = CombineAndCollapseWhitespace(node_text, child_text, add_space);
176 // Recursively compute the siblings' text.
177 // Again, preserve inter-element whitespace separation.
178 base::string16 sibling_text =
179 FindChildTextInner(node.nextSibling(), depth - 1);
180 add_space = node.nodeType() == WebNode::TextNode && node_text.empty();
181 node_text = CombineAndCollapseWhitespace(node_text, sibling_text, add_space);
183 return node_text;
186 // Returns the aggregated values of the descendants of |element| that are
187 // non-empty text nodes. This is a faster alternative to |innerText()| for
188 // performance critical operations. It does a full depth-first search so can be
189 // used when the structure is not directly known. However, unlike with
190 // |innerText()|, the search depth and breadth are limited to a fixed threshold.
191 // Whitespace is trimmed from text accumulated at descendant nodes.
192 base::string16 FindChildText(const WebNode& node) {
193 if (node.isTextNode())
194 return node.nodeValue();
196 WebNode child = node.firstChild();
198 const int kChildSearchDepth = 10;
199 base::string16 node_text = FindChildTextInner(child, kChildSearchDepth);
200 base::TrimWhitespace(node_text, base::TRIM_ALL, &node_text);
201 return node_text;
204 // Helper for |InferLabelForElement()| that infers a label, if possible, from
205 // a previous sibling of |element|,
206 // e.g. Some Text <input ...>
207 // or Some <span>Text</span> <input ...>
208 // or <p>Some Text</p><input ...>
209 // or <label>Some Text</label> <input ...>
210 // or Some Text <img><input ...>
211 // or <b>Some Text</b><br/> <input ...>.
212 base::string16 InferLabelFromPrevious(const WebFormControlElement& element) {
213 base::string16 inferred_label;
214 WebNode previous = element;
215 while (true) {
216 previous = previous.previousSibling();
217 if (previous.isNull())
218 break;
220 // Skip over comments.
221 WebNode::NodeType node_type = previous.nodeType();
222 if (node_type == WebNode::CommentNode)
223 continue;
225 // Otherwise, only consider normal HTML elements and their contents.
226 if (node_type != WebNode::TextNode &&
227 node_type != WebNode::ElementNode)
228 break;
230 // A label might be split across multiple "lightweight" nodes.
231 // Coalesce any text contained in multiple consecutive
232 // (a) plain text nodes or
233 // (b) inline HTML elements that are essentially equivalent to text nodes.
234 CR_DEFINE_STATIC_LOCAL(WebString, kBold, ("b"));
235 CR_DEFINE_STATIC_LOCAL(WebString, kStrong, ("strong"));
236 CR_DEFINE_STATIC_LOCAL(WebString, kSpan, ("span"));
237 CR_DEFINE_STATIC_LOCAL(WebString, kFont, ("font"));
238 if (previous.isTextNode() ||
239 HasTagName(previous, kBold) || HasTagName(previous, kStrong) ||
240 HasTagName(previous, kSpan) || HasTagName(previous, kFont)) {
241 base::string16 value = FindChildText(previous);
242 // A text node's value will be empty if it is for a line break.
243 bool add_space = previous.isTextNode() && value.empty();
244 inferred_label =
245 CombineAndCollapseWhitespace(value, inferred_label, add_space);
246 continue;
249 // If we have identified a partial label and have reached a non-lightweight
250 // element, consider the label to be complete.
251 base::string16 trimmed_label;
252 base::TrimWhitespace(inferred_label, base::TRIM_ALL, &trimmed_label);
253 if (!trimmed_label.empty())
254 break;
256 // <img> and <br> tags often appear between the input element and its
257 // label text, so skip over them.
258 CR_DEFINE_STATIC_LOCAL(WebString, kImage, ("img"));
259 CR_DEFINE_STATIC_LOCAL(WebString, kBreak, ("br"));
260 if (HasTagName(previous, kImage) || HasTagName(previous, kBreak))
261 continue;
263 // We only expect <p> and <label> tags to contain the full label text.
264 CR_DEFINE_STATIC_LOCAL(WebString, kPage, ("p"));
265 CR_DEFINE_STATIC_LOCAL(WebString, kLabel, ("label"));
266 if (HasTagName(previous, kPage) || HasTagName(previous, kLabel))
267 inferred_label = FindChildText(previous);
269 break;
272 base::TrimWhitespace(inferred_label, base::TRIM_ALL, &inferred_label);
273 return inferred_label;
276 // Helper for |InferLabelForElement()| that infers a label, if possible, from
277 // placeholder text,
278 base::string16 InferLabelFromPlaceholder(const WebFormControlElement& element) {
279 CR_DEFINE_STATIC_LOCAL(WebString, kPlaceholder, ("placeholder"));
280 if (element.hasAttribute(kPlaceholder))
281 return element.getAttribute(kPlaceholder);
283 return base::string16();
286 // Helper for |InferLabelForElement()| that infers a label, if possible, from
287 // enclosing list item,
288 // e.g. <li>Some Text<input ...><input ...><input ...></tr>
289 base::string16 InferLabelFromListItem(const WebFormControlElement& element) {
290 WebNode parent = element.parentNode();
291 CR_DEFINE_STATIC_LOCAL(WebString, kListItem, ("li"));
292 while (!parent.isNull() && parent.isElementNode() &&
293 !parent.to<WebElement>().hasHTMLTagName(kListItem)) {
294 parent = parent.parentNode();
297 if (!parent.isNull() && HasTagName(parent, kListItem))
298 return FindChildText(parent);
300 return base::string16();
303 // Helper for |InferLabelForElement()| that infers a label, if possible, from
304 // surrounding table structure,
305 // e.g. <tr><td>Some Text</td><td><input ...></td></tr>
306 // or <tr><th>Some Text</th><td><input ...></td></tr>
307 // or <tr><td><b>Some Text</b></td><td><b><input ...></b></td></tr>
308 // or <tr><th><b>Some Text</b></th><td><b><input ...></b></td></tr>
309 base::string16 InferLabelFromTableColumn(const WebFormControlElement& element) {
310 CR_DEFINE_STATIC_LOCAL(WebString, kTableCell, ("td"));
311 WebNode parent = element.parentNode();
312 while (!parent.isNull() && parent.isElementNode() &&
313 !parent.to<WebElement>().hasHTMLTagName(kTableCell)) {
314 parent = parent.parentNode();
317 if (parent.isNull())
318 return base::string16();
320 // Check all previous siblings, skipping non-element nodes, until we find a
321 // non-empty text block.
322 base::string16 inferred_label;
323 WebNode previous = parent.previousSibling();
324 CR_DEFINE_STATIC_LOCAL(WebString, kTableHeader, ("th"));
325 while (inferred_label.empty() && !previous.isNull()) {
326 if (HasTagName(previous, kTableCell) || HasTagName(previous, kTableHeader))
327 inferred_label = FindChildText(previous);
329 previous = previous.previousSibling();
332 return inferred_label;
335 // Helper for |InferLabelForElement()| that infers a label, if possible, from
336 // surrounding table structure,
337 // e.g. <tr><td>Some Text</td></tr><tr><td><input ...></td></tr>
338 base::string16 InferLabelFromTableRow(const WebFormControlElement& element) {
339 CR_DEFINE_STATIC_LOCAL(WebString, kTableRow, ("tr"));
340 WebNode parent = element.parentNode();
341 while (!parent.isNull() && parent.isElementNode() &&
342 !parent.to<WebElement>().hasHTMLTagName(kTableRow)) {
343 parent = parent.parentNode();
346 if (parent.isNull())
347 return base::string16();
349 // Check all previous siblings, skipping non-element nodes, until we find a
350 // non-empty text block.
351 base::string16 inferred_label;
352 WebNode previous = parent.previousSibling();
353 while (inferred_label.empty() && !previous.isNull()) {
354 if (HasTagName(previous, kTableRow))
355 inferred_label = FindChildText(previous);
357 previous = previous.previousSibling();
360 return inferred_label;
363 // Helper for |InferLabelForElement()| that infers a label, if possible, from
364 // a surrounding div table,
365 // e.g. <div>Some Text<span><input ...></span></div>
366 // e.g. <div>Some Text</div><div><input ...></div>
367 base::string16 InferLabelFromDivTable(const WebFormControlElement& element) {
368 WebNode node = element.parentNode();
369 bool looking_for_parent = true;
371 // Search the sibling and parent <div>s until we find a candidate label.
372 base::string16 inferred_label;
373 CR_DEFINE_STATIC_LOCAL(WebString, kDiv, ("div"));
374 CR_DEFINE_STATIC_LOCAL(WebString, kTable, ("table"));
375 CR_DEFINE_STATIC_LOCAL(WebString, kFieldSet, ("fieldset"));
376 while (inferred_label.empty() && !node.isNull()) {
377 if (HasTagName(node, kDiv)) {
378 looking_for_parent = false;
379 inferred_label = FindChildText(node);
380 } else if (looking_for_parent &&
381 (HasTagName(node, kTable) || HasTagName(node, kFieldSet))) {
382 // If the element is in a table or fieldset, its label most likely is too.
383 break;
386 if (node.previousSibling().isNull()) {
387 // If there are no more siblings, continue walking up the tree.
388 looking_for_parent = true;
391 if (looking_for_parent)
392 node = node.parentNode();
393 else
394 node = node.previousSibling();
397 return inferred_label;
400 // Helper for |InferLabelForElement()| that infers a label, if possible, from
401 // a surrounding definition list,
402 // e.g. <dl><dt>Some Text</dt><dd><input ...></dd></dl>
403 // e.g. <dl><dt><b>Some Text</b></dt><dd><b><input ...></b></dd></dl>
404 base::string16 InferLabelFromDefinitionList(
405 const WebFormControlElement& element) {
406 CR_DEFINE_STATIC_LOCAL(WebString, kDefinitionData, ("dd"));
407 WebNode parent = element.parentNode();
408 while (!parent.isNull() && parent.isElementNode() &&
409 !parent.to<WebElement>().hasHTMLTagName(kDefinitionData))
410 parent = parent.parentNode();
412 if (parent.isNull() || !HasTagName(parent, kDefinitionData))
413 return base::string16();
415 // Skip by any intervening text nodes.
416 WebNode previous = parent.previousSibling();
417 while (!previous.isNull() && previous.isTextNode())
418 previous = previous.previousSibling();
420 CR_DEFINE_STATIC_LOCAL(WebString, kDefinitionTag, ("dt"));
421 if (previous.isNull() || !HasTagName(previous, kDefinitionTag))
422 return base::string16();
424 return FindChildText(previous);
427 // Infers corresponding label for |element| from surrounding context in the DOM,
428 // e.g. the contents of the preceding <p> tag or text element.
429 base::string16 InferLabelForElement(const WebFormControlElement& element) {
430 base::string16 inferred_label = InferLabelFromPrevious(element);
431 if (!inferred_label.empty())
432 return inferred_label;
434 // If we didn't find a label, check for placeholder text.
435 inferred_label = InferLabelFromPlaceholder(element);
436 if (!inferred_label.empty())
437 return inferred_label;
439 // If we didn't find a label, check for list item case.
440 inferred_label = InferLabelFromListItem(element);
441 if (!inferred_label.empty())
442 return inferred_label;
444 // If we didn't find a label, check for table cell case.
445 inferred_label = InferLabelFromTableColumn(element);
446 if (!inferred_label.empty())
447 return inferred_label;
449 // If we didn't find a label, check for table row case.
450 inferred_label = InferLabelFromTableRow(element);
451 if (!inferred_label.empty())
452 return inferred_label;
454 // If we didn't find a label, check for definition list case.
455 inferred_label = InferLabelFromDefinitionList(element);
456 if (!inferred_label.empty())
457 return inferred_label;
459 // If we didn't find a label, check for div table case.
460 return InferLabelFromDivTable(element);
463 // Fills |option_strings| with the values of the <option> elements present in
464 // |select_element|.
465 void GetOptionStringsFromElement(const WebSelectElement& select_element,
466 std::vector<base::string16>* option_values,
467 std::vector<base::string16>* option_contents) {
468 DCHECK(!select_element.isNull());
470 option_values->clear();
471 option_contents->clear();
472 WebVector<WebElement> list_items = select_element.listItems();
474 // Constrain the maximum list length to prevent a malicious site from DOS'ing
475 // the browser, without entirely breaking autocomplete for some extreme
476 // legitimate sites: http://crbug.com/49332 and http://crbug.com/363094
477 if (list_items.size() > kMaxListSize)
478 return;
480 option_values->reserve(list_items.size());
481 option_contents->reserve(list_items.size());
482 for (size_t i = 0; i < list_items.size(); ++i) {
483 if (IsOptionElement(list_items[i])) {
484 const WebOptionElement option = list_items[i].toConst<WebOptionElement>();
485 option_values->push_back(option.value());
486 option_contents->push_back(option.text());
491 // The callback type used by |ForEachMatchingFormField()|.
492 typedef void (*Callback)(const FormFieldData&,
493 bool, /* is_initiating_element */
494 blink::WebFormControlElement*);
496 // For each autofillable field in |data| that matches a field in the |form|,
497 // the |callback| is invoked with the corresponding |form| field data.
498 void ForEachMatchingFormField(const WebFormElement& form_element,
499 const WebElement& initiating_element,
500 const FormData& data,
501 FieldFilterMask filters,
502 bool force_override,
503 Callback callback) {
504 std::vector<WebFormControlElement> control_elements;
505 ExtractAutofillableElements(
506 form_element, ExtractionRequirements(), &control_elements);
508 if (control_elements.size() != data.fields.size()) {
509 // This case should be reachable only for pathological websites and tests,
510 // which add or remove form fields while the user is interacting with the
511 // Autofill popup.
512 return;
515 // It's possible that the site has injected fields into the form after the
516 // page has loaded, so we can't assert that the size of the cached control
517 // elements is equal to the size of the fields in |form|. Fortunately, the
518 // one case in the wild where this happens, paypal.com signup form, the fields
519 // are appended to the end of the form and are not visible.
520 for (size_t i = 0; i < control_elements.size(); ++i) {
521 WebFormControlElement* element = &control_elements[i];
523 if (base::string16(element->nameForAutofill()) != data.fields[i].name) {
524 // This case should be reachable only for pathological websites, which
525 // rename form fields while the user is interacting with the Autofill
526 // popup. I (isherman) am not aware of any such websites, and so am
527 // optimistically including a NOTREACHED(). If you ever trip this check,
528 // please file a bug against me.
529 NOTREACHED();
530 continue;
533 bool is_initiating_element = (*element == initiating_element);
535 // Only autofill empty fields and the field that initiated the filling,
536 // i.e. the field the user is currently editing and interacting with.
537 const WebInputElement* input_element = toWebInputElement(element);
538 if (!force_override && !is_initiating_element &&
539 ((IsAutofillableInputElement(input_element) ||
540 IsTextAreaElement(*element)) &&
541 !element->value().isEmpty()))
542 continue;
544 if (((filters & FILTER_DISABLED_ELEMENTS) && !element->isEnabled()) ||
545 ((filters & FILTER_READONLY_ELEMENTS) && element->isReadOnly()) ||
546 ((filters & FILTER_NON_FOCUSABLE_ELEMENTS) && !element->isFocusable()))
547 continue;
549 callback(data.fields[i], is_initiating_element, element);
553 // Sets the |field|'s value to the value in |data|.
554 // Also sets the "autofilled" attribute, causing the background to be yellow.
555 void FillFormField(const FormFieldData& data,
556 bool is_initiating_node,
557 blink::WebFormControlElement* field) {
558 // Nothing to fill.
559 if (data.value.empty())
560 return;
562 if (!data.is_autofilled)
563 return;
565 WebInputElement* input_element = toWebInputElement(field);
566 if (IsCheckableElement(input_element)) {
567 input_element->setChecked(data.is_checked, true);
568 } else {
569 base::string16 value = data.value;
570 if (IsTextInput(input_element) || IsMonthInput(input_element)) {
571 // If the maxlength attribute contains a negative value, maxLength()
572 // returns the default maxlength value.
573 value = value.substr(0, input_element->maxLength());
575 field->setValue(value, true);
578 field->setAutofilled(true);
580 if (is_initiating_node &&
581 ((IsTextInput(input_element) || IsMonthInput(input_element)) ||
582 IsTextAreaElement(*field))) {
583 int length = field->value().length();
584 field->setSelectionRange(length, length);
585 // Clear the current IME composition (the underline), if there is one.
586 field->document().frame()->unmarkText();
590 // Sets the |field|'s "suggested" (non JS visible) value to the value in |data|.
591 // Also sets the "autofilled" attribute, causing the background to be yellow.
592 void PreviewFormField(const FormFieldData& data,
593 bool is_initiating_node,
594 blink::WebFormControlElement* field) {
595 // Nothing to preview.
596 if (data.value.empty())
597 return;
599 if (!data.is_autofilled)
600 return;
602 // Preview input, textarea and select fields. For input fields, excludes
603 // checkboxes and radio buttons, as there is no provision for
604 // setSuggestedCheckedValue in WebInputElement.
605 WebInputElement* input_element = toWebInputElement(field);
606 if (IsTextInput(input_element) || IsMonthInput(input_element)) {
607 // If the maxlength attribute contains a negative value, maxLength()
608 // returns the default maxlength value.
609 input_element->setSuggestedValue(
610 data.value.substr(0, input_element->maxLength()));
611 input_element->setAutofilled(true);
612 } else if (IsTextAreaElement(*field) || IsSelectElement(*field)) {
613 field->setSuggestedValue(data.value);
614 field->setAutofilled(true);
617 if (is_initiating_node &&
618 (IsTextInput(input_element) || IsTextAreaElement(*field))) {
619 // Select the part of the text that the user didn't type.
620 int start = field->value().length();
621 int end = field->suggestedValue().length();
622 field->setSelectionRange(start, end);
626 std::string RetrievalMethodToString(
627 const WebElementDescriptor::RetrievalMethod& method) {
628 switch (method) {
629 case WebElementDescriptor::CSS_SELECTOR:
630 return "CSS_SELECTOR";
631 case WebElementDescriptor::ID:
632 return "ID";
633 case WebElementDescriptor::NONE:
634 return "NONE";
636 NOTREACHED();
637 return "UNKNOWN";
640 // Recursively checks whether |node| or any of its children have a non-empty
641 // bounding box. The recursion depth is bounded by |depth|.
642 bool IsWebNodeVisibleImpl(const blink::WebNode& node, const int depth) {
643 if (depth < 0)
644 return false;
645 if (node.hasNonEmptyBoundingBox())
646 return true;
648 // The childNodes method is not a const method. Therefore it cannot be called
649 // on a const reference. Therefore we need a const cast.
650 const blink::WebNodeList& children =
651 const_cast<blink::WebNode&>(node).childNodes();
652 size_t length = children.length();
653 for (size_t i = 0; i < length; ++i) {
654 const blink::WebNode& item = children.item(i);
655 if (IsWebNodeVisibleImpl(item, depth - 1))
656 return true;
658 return false;
661 } // namespace
663 const size_t kMaxParseableFields = 200;
665 bool IsMonthInput(const WebInputElement* element) {
666 CR_DEFINE_STATIC_LOCAL(WebString, kMonth, ("month"));
667 return element && !element->isNull() && element->formControlType() == kMonth;
670 // All text fields, including password fields, should be extracted.
671 bool IsTextInput(const WebInputElement* element) {
672 return element && !element->isNull() && element->isTextField();
675 bool IsSelectElement(const WebFormControlElement& element) {
676 // Static for improved performance.
677 CR_DEFINE_STATIC_LOCAL(WebString, kSelectOne, ("select-one"));
678 return !element.isNull() && element.formControlType() == kSelectOne;
681 bool IsTextAreaElement(const WebFormControlElement& element) {
682 // Static for improved performance.
683 CR_DEFINE_STATIC_LOCAL(WebString, kTextArea, ("textarea"));
684 return !element.isNull() && element.formControlType() == kTextArea;
687 bool IsCheckableElement(const WebInputElement* element) {
688 if (!element || element->isNull())
689 return false;
691 return element->isCheckbox() || element->isRadioButton();
694 bool IsAutofillableInputElement(const WebInputElement* element) {
695 return IsTextInput(element) ||
696 IsMonthInput(element) ||
697 IsCheckableElement(element);
700 const base::string16 GetFormIdentifier(const WebFormElement& form) {
701 base::string16 identifier = form.name();
702 CR_DEFINE_STATIC_LOCAL(WebString, kId, ("id"));
703 if (identifier.empty())
704 identifier = form.getAttribute(kId);
706 return identifier;
709 bool IsWebNodeVisible(const blink::WebNode& node) {
710 // In the bug http://crbug.com/237216 the form's bounding box is empty
711 // however the form has non empty children. Thus we need to look at the
712 // form's children.
713 int kNodeSearchDepth = 2;
714 return IsWebNodeVisibleImpl(node, kNodeSearchDepth);
717 bool ClickElement(const WebDocument& document,
718 const WebElementDescriptor& element_descriptor) {
719 WebString web_descriptor = WebString::fromUTF8(element_descriptor.descriptor);
720 blink::WebElement element;
722 switch (element_descriptor.retrieval_method) {
723 case WebElementDescriptor::CSS_SELECTOR: {
724 WebExceptionCode ec = 0;
725 element = document.querySelector(web_descriptor, ec);
726 if (ec)
727 DVLOG(1) << "Query selector failed. Error code: " << ec << ".";
728 break;
730 case WebElementDescriptor::ID:
731 element = document.getElementById(web_descriptor);
732 break;
733 case WebElementDescriptor::NONE:
734 return true;
737 if (element.isNull()) {
738 DVLOG(1) << "Could not find "
739 << element_descriptor.descriptor
740 << " by "
741 << RetrievalMethodToString(element_descriptor.retrieval_method)
742 << ".";
743 return false;
746 element.simulateClick();
747 return true;
750 // Fills |autofillable_elements| with all the auto-fillable form control
751 // elements in |form_element|.
752 void ExtractAutofillableElements(
753 const WebFormElement& form_element,
754 RequirementsMask requirements,
755 std::vector<WebFormControlElement>* autofillable_elements) {
756 WebVector<WebFormControlElement> control_elements;
757 form_element.getFormControlElements(control_elements);
759 autofillable_elements->clear();
760 for (size_t i = 0; i < control_elements.size(); ++i) {
761 WebFormControlElement element = control_elements[i];
762 if (!IsAutofillableElement(element))
763 continue;
765 if (requirements & REQUIRE_AUTOCOMPLETE) {
766 // TODO(isherman): WebKit currently doesn't handle the autocomplete
767 // attribute for select or textarea elements, but it probably should.
768 WebInputElement* input_element = toWebInputElement(&control_elements[i]);
769 if (IsAutofillableInputElement(input_element) &&
770 !SatisfiesRequireAutocomplete(*input_element))
771 continue;
774 autofillable_elements->push_back(element);
778 void WebFormControlElementToFormField(const WebFormControlElement& element,
779 ExtractMask extract_mask,
780 FormFieldData* field) {
781 DCHECK(field);
782 DCHECK(!element.isNull());
783 CR_DEFINE_STATIC_LOCAL(WebString, kAutocomplete, ("autocomplete"));
785 // The label is not officially part of a WebFormControlElement; however, the
786 // labels for all form control elements are scraped from the DOM and set in
787 // WebFormElementToFormData.
788 field->name = element.nameForAutofill();
789 field->form_control_type = base::UTF16ToUTF8(element.formControlType());
790 field->autocomplete_attribute =
791 base::UTF16ToUTF8(element.getAttribute(kAutocomplete));
792 if (field->autocomplete_attribute.size() > kMaxDataLength) {
793 // Discard overly long attribute values to avoid DOS-ing the browser
794 // process. However, send over a default string to indicate that the
795 // attribute was present.
796 field->autocomplete_attribute = "x-max-data-length-exceeded";
799 if (!IsAutofillableElement(element))
800 return;
802 const WebInputElement* input_element = toWebInputElement(&element);
803 if (IsAutofillableInputElement(input_element) ||
804 IsTextAreaElement(element)) {
805 field->is_autofilled = element.isAutofilled();
806 field->is_focusable = element.isFocusable();
807 field->should_autocomplete = element.autoComplete();
808 field->text_direction = element.directionForFormData() ==
809 "rtl" ? base::i18n::RIGHT_TO_LEFT : base::i18n::LEFT_TO_RIGHT;
812 if (IsAutofillableInputElement(input_element)) {
813 if (IsTextInput(input_element))
814 field->max_length = input_element->maxLength();
816 field->is_checkable = IsCheckableElement(input_element);
817 field->is_checked = input_element->isChecked();
818 } else if (IsTextAreaElement(element)) {
819 // Nothing more to do in this case.
820 } else if (extract_mask & EXTRACT_OPTIONS) {
821 // Set option strings on the field if available.
822 DCHECK(IsSelectElement(element));
823 const WebSelectElement select_element = element.toConst<WebSelectElement>();
824 GetOptionStringsFromElement(select_element,
825 &field->option_values,
826 &field->option_contents);
829 if (!(extract_mask & EXTRACT_VALUE))
830 return;
832 base::string16 value = element.value();
834 if (IsSelectElement(element) && (extract_mask & EXTRACT_OPTION_TEXT)) {
835 const WebSelectElement select_element = element.toConst<WebSelectElement>();
836 // Convert the |select_element| value to text if requested.
837 WebVector<WebElement> list_items = select_element.listItems();
838 for (size_t i = 0; i < list_items.size(); ++i) {
839 if (IsOptionElement(list_items[i])) {
840 const WebOptionElement option_element =
841 list_items[i].toConst<WebOptionElement>();
842 if (option_element.value() == value) {
843 value = option_element.text();
844 break;
850 // Constrain the maximum data length to prevent a malicious site from DOS'ing
851 // the browser: http://crbug.com/49332
852 if (value.size() > kMaxDataLength)
853 value = value.substr(0, kMaxDataLength);
855 field->value = value;
858 bool WebFormElementToFormData(
859 const blink::WebFormElement& form_element,
860 const blink::WebFormControlElement& form_control_element,
861 RequirementsMask requirements,
862 ExtractMask extract_mask,
863 FormData* form,
864 FormFieldData* field) {
865 CR_DEFINE_STATIC_LOCAL(WebString, kLabel, ("label"));
866 CR_DEFINE_STATIC_LOCAL(WebString, kFor, ("for"));
867 CR_DEFINE_STATIC_LOCAL(WebString, kHidden, ("hidden"));
869 const WebFrame* frame = form_element.document().frame();
870 if (!frame)
871 return false;
873 if (requirements & REQUIRE_AUTOCOMPLETE && !form_element.autoComplete())
874 return false;
876 form->name = GetFormIdentifier(form_element);
877 form->origin = frame->document().url();
878 form->action = frame->document().completeURL(form_element.action());
879 form->user_submitted = form_element.wasUserSubmitted();
881 // If the completed URL is not valid, just use the action we get from
882 // WebKit.
883 if (!form->action.is_valid())
884 form->action = GURL(form_element.action());
886 // A map from a FormFieldData's name to the FormFieldData itself.
887 std::map<base::string16, FormFieldData*> name_map;
889 // The extracted FormFields. We use pointers so we can store them in
890 // |name_map|.
891 ScopedVector<FormFieldData> form_fields;
893 WebVector<WebFormControlElement> control_elements;
894 form_element.getFormControlElements(control_elements);
896 // A vector of bools that indicate whether each field in the form meets the
897 // requirements and thus will be in the resulting |form|.
898 std::vector<bool> fields_extracted(control_elements.size(), false);
900 for (size_t i = 0; i < control_elements.size(); ++i) {
901 const WebFormControlElement& control_element = control_elements[i];
903 if (!IsAutofillableElement(control_element))
904 continue;
906 const WebInputElement* input_element = toWebInputElement(&control_element);
907 if (requirements & REQUIRE_AUTOCOMPLETE &&
908 IsAutofillableInputElement(input_element) &&
909 !SatisfiesRequireAutocomplete(*input_element))
910 continue;
912 // Create a new FormFieldData, fill it out and map it to the field's name.
913 FormFieldData* form_field = new FormFieldData;
914 WebFormControlElementToFormField(control_element, extract_mask, form_field);
915 form_fields.push_back(form_field);
916 // TODO(jhawkins): A label element is mapped to a form control element's id.
917 // field->name() will contain the id only if the name does not exist. Add
918 // an id() method to WebFormControlElement and use that here.
919 name_map[form_field->name] = form_field;
920 fields_extracted[i] = true;
923 // If we failed to extract any fields, give up. Also, to avoid overly
924 // expensive computation, we impose a maximum number of allowable fields.
925 if (form_fields.empty() || form_fields.size() > kMaxParseableFields)
926 return false;
928 // Loop through the label elements inside the form element. For each label
929 // element, get the corresponding form control element, use the form control
930 // element's name as a key into the <name, FormFieldData> map to find the
931 // previously created FormFieldData and set the FormFieldData's label to the
932 // label.firstChild().nodeValue() of the label element.
933 WebElementCollection labels = form_element.getElementsByHTMLTagName(kLabel);
934 DCHECK(!labels.isNull());
935 for (WebElement item = labels.firstItem(); !item.isNull();
936 item = labels.nextItem()) {
937 WebLabelElement label = item.to<WebLabelElement>();
938 WebFormControlElement field_element =
939 label.correspondingControl().to<WebFormControlElement>();
941 base::string16 element_name;
942 if (field_element.isNull()) {
943 // Sometimes site authors will incorrectly specify the corresponding
944 // field element's name rather than its id, so we compensate here.
945 element_name = label.getAttribute(kFor);
946 } else if (
947 !field_element.isFormControlElement() ||
948 field_element.formControlType() == kHidden) {
949 continue;
950 } else {
951 element_name = field_element.nameForAutofill();
954 std::map<base::string16, FormFieldData*>::iterator iter =
955 name_map.find(element_name);
956 if (iter != name_map.end()) {
957 base::string16 label_text = FindChildText(label);
959 // Concatenate labels because some sites might have multiple label
960 // candidates.
961 if (!iter->second->label.empty() && !label_text.empty())
962 iter->second->label += base::ASCIIToUTF16(" ");
963 iter->second->label += label_text;
967 // Loop through the form control elements, extracting the label text from
968 // the DOM. We use the |fields_extracted| vector to make sure we assign the
969 // extracted label to the correct field, as it's possible |form_fields| will
970 // not contain all of the elements in |control_elements|.
971 for (size_t i = 0, field_idx = 0;
972 i < control_elements.size() && field_idx < form_fields.size(); ++i) {
973 // This field didn't meet the requirements, so don't try to find a label
974 // for it.
975 if (!fields_extracted[i])
976 continue;
978 const WebFormControlElement& control_element = control_elements[i];
979 if (form_fields[field_idx]->label.empty())
980 form_fields[field_idx]->label = InferLabelForElement(control_element);
982 if (field && form_control_element == control_element)
983 *field = *form_fields[field_idx];
985 ++field_idx;
988 // Copy the created FormFields into the resulting FormData object.
989 for (ScopedVector<FormFieldData>::const_iterator iter = form_fields.begin();
990 iter != form_fields.end(); ++iter) {
991 form->fields.push_back(**iter);
994 return true;
997 bool FindFormAndFieldForFormControlElement(const WebFormControlElement& element,
998 FormData* form,
999 FormFieldData* field,
1000 RequirementsMask requirements) {
1001 if (!IsAutofillableElement(element))
1002 return false;
1004 const WebFormElement form_element = element.form();
1005 if (form_element.isNull())
1006 return false;
1008 ExtractMask extract_mask =
1009 static_cast<ExtractMask>(EXTRACT_VALUE | EXTRACT_OPTIONS);
1010 return WebFormElementToFormData(form_element,
1011 element,
1012 requirements,
1013 extract_mask,
1014 form,
1015 field);
1018 void FillForm(const FormData& form, const WebFormControlElement& element) {
1019 WebFormElement form_element = element.form();
1020 if (form_element.isNull())
1021 return;
1023 ForEachMatchingFormField(form_element,
1024 element,
1025 form,
1026 FILTER_ALL_NON_EDITABLE_ELEMENTS,
1027 false, /* dont force override */
1028 &FillFormField);
1031 void FillFormIncludingNonFocusableElements(const FormData& form_data,
1032 const WebFormElement& form_element) {
1033 if (form_element.isNull())
1034 return;
1036 FieldFilterMask filter_mask = static_cast<FieldFilterMask>(
1037 FILTER_DISABLED_ELEMENTS | FILTER_READONLY_ELEMENTS);
1038 ForEachMatchingFormField(form_element,
1039 WebInputElement(),
1040 form_data,
1041 filter_mask,
1042 true, /* force override */
1043 &FillFormField);
1046 void FillFormForAllElements(const FormData& form_data,
1047 const WebFormElement& form_element) {
1048 if (form_element.isNull())
1049 return;
1051 ForEachMatchingFormField(form_element,
1052 WebInputElement(),
1053 form_data,
1054 FILTER_NONE,
1055 true, /* force override */
1056 &FillFormField);
1059 void PreviewForm(const FormData& form, const WebFormControlElement& element) {
1060 WebFormElement form_element = element.form();
1061 if (form_element.isNull())
1062 return;
1064 ForEachMatchingFormField(form_element,
1065 element,
1066 form,
1067 FILTER_ALL_NON_EDITABLE_ELEMENTS,
1068 false, /* dont force override */
1069 &PreviewFormField);
1072 bool ClearPreviewedFormWithElement(const WebFormControlElement& element,
1073 bool was_autofilled) {
1074 WebFormElement form_element = element.form();
1075 if (form_element.isNull())
1076 return false;
1078 std::vector<WebFormControlElement> control_elements;
1079 ExtractAutofillableElements(
1080 form_element, ExtractionRequirements(), &control_elements);
1081 for (size_t i = 0; i < control_elements.size(); ++i) {
1082 // There might be unrelated elements in this form which have already been
1083 // auto-filled. For example, the user might have already filled the address
1084 // part of a form and now be dealing with the credit card section. We only
1085 // want to reset the auto-filled status for fields that were previewed.
1086 WebFormControlElement control_element = control_elements[i];
1088 // Only text input, textarea and select elements can be previewed.
1089 WebInputElement* input_element = toWebInputElement(&control_element);
1090 if (!IsTextInput(input_element) &&
1091 !IsMonthInput(input_element) &&
1092 !IsTextAreaElement(control_element) &&
1093 !IsSelectElement(control_element))
1094 continue;
1096 // If the element is not auto-filled, we did not preview it,
1097 // so there is nothing to reset.
1098 if(!control_element.isAutofilled())
1099 continue;
1101 if ((IsTextInput(input_element) ||
1102 IsMonthInput(input_element) ||
1103 IsTextAreaElement(control_element) ||
1104 IsSelectElement(control_element)) &&
1105 control_element.suggestedValue().isEmpty())
1106 continue;
1108 // Clear the suggested value. For the initiating node, also restore the
1109 // original value.
1110 if (IsTextInput(input_element) || IsMonthInput(input_element) ||
1111 IsTextAreaElement(control_element)) {
1112 control_element.setSuggestedValue(WebString());
1113 bool is_initiating_node = (element == control_element);
1114 if (is_initiating_node) {
1115 control_element.setAutofilled(was_autofilled);
1116 // Clearing the suggested value in the focused node (above) can cause
1117 // selection to be lost. We force selection range to restore the text
1118 // cursor.
1119 int length = control_element.value().length();
1120 control_element.setSelectionRange(length, length);
1121 } else {
1122 control_element.setAutofilled(false);
1124 } else if (IsSelectElement(control_element)) {
1125 control_element.setSuggestedValue(WebString());
1126 control_element.setAutofilled(false);
1130 return true;
1133 bool FormWithElementIsAutofilled(const WebInputElement& element) {
1134 WebFormElement form_element = element.form();
1135 if (form_element.isNull())
1136 return false;
1138 std::vector<WebFormControlElement> control_elements;
1139 ExtractAutofillableElements(
1140 form_element, ExtractionRequirements(), &control_elements);
1141 for (size_t i = 0; i < control_elements.size(); ++i) {
1142 WebInputElement* input_element = toWebInputElement(&control_elements[i]);
1143 if (!IsAutofillableInputElement(input_element))
1144 continue;
1146 if (input_element->isAutofilled())
1147 return true;
1150 return false;
1153 bool IsWebpageEmpty(const blink::WebFrame* frame) {
1154 blink::WebDocument document = frame->document();
1156 return IsWebElementEmpty(document.head()) &&
1157 IsWebElementEmpty(document.body());
1160 bool IsWebElementEmpty(const blink::WebElement& element) {
1161 // This array contains all tags which can be present in an empty page.
1162 const char* const kAllowedValue[] = {
1163 "script",
1164 "meta",
1165 "title",
1167 const size_t kAllowedValueLength = arraysize(kAllowedValue);
1169 if (element.isNull())
1170 return true;
1171 // The childNodes method is not a const method. Therefore it cannot be called
1172 // on a const reference. Therefore we need a const cast.
1173 const blink::WebNodeList& children =
1174 const_cast<blink::WebElement&>(element).childNodes();
1175 for (size_t i = 0; i < children.length(); ++i) {
1176 const blink::WebNode& item = children.item(i);
1178 if (item.isTextNode() &&
1179 !base::ContainsOnlyChars(item.nodeValue().utf8(),
1180 base::kWhitespaceASCII))
1181 return false;
1183 // We ignore all other items with names which begin with
1184 // the character # because they are not html tags.
1185 if (item.nodeName().utf8()[0] == '#')
1186 continue;
1188 bool tag_is_allowed = false;
1189 // Test if the item name is in the kAllowedValue array
1190 for (size_t allowed_value_index = 0;
1191 allowed_value_index < kAllowedValueLength; ++allowed_value_index) {
1192 if (HasTagName(item,
1193 WebString::fromUTF8(kAllowedValue[allowed_value_index]))) {
1194 tag_is_allowed = true;
1195 break;
1198 if (!tag_is_allowed)
1199 return false;
1201 return true;
1204 gfx::RectF GetScaledBoundingBox(float scale, WebFormControlElement* element) {
1205 gfx::Rect bounding_box(element->boundsInViewportSpace());
1206 return gfx::RectF(bounding_box.x() * scale,
1207 bounding_box.y() * scale,
1208 bounding_box.width() * scale,
1209 bounding_box.height() * scale);
1212 } // namespace autofill