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