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