Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(195)

Side by Side Diff: components/autofill/content/renderer/autofill_agent.cc

Issue 140093005: Add supports that allow Autofill to be initiated from textarea field (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/autofill/content/renderer/autofill_agent.h" 5 #include "components/autofill/content/renderer/autofill_agent.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/command_line.h" 8 #include "base/command_line.h"
9 #include "base/message_loop/message_loop.h" 9 #include "base/message_loop/message_loop.h"
10 #include "base/strings/string_split.h" 10 #include "base/strings/string_split.h"
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
48 using blink::WebAutofillClient; 48 using blink::WebAutofillClient;
49 using blink::WebFormControlElement; 49 using blink::WebFormControlElement;
50 using blink::WebFormElement; 50 using blink::WebFormElement;
51 using blink::WebFrame; 51 using blink::WebFrame;
52 using blink::WebInputElement; 52 using blink::WebInputElement;
53 using blink::WebKeyboardEvent; 53 using blink::WebKeyboardEvent;
54 using blink::WebNode; 54 using blink::WebNode;
55 using blink::WebNodeCollection; 55 using blink::WebNodeCollection;
56 using blink::WebOptionElement; 56 using blink::WebOptionElement;
57 using blink::WebString; 57 using blink::WebString;
58 using blink::WebTextAreaElement;
58 59
59 namespace autofill { 60 namespace autofill {
60 61
61 namespace { 62 namespace {
62 63
63 // Gets all the data list values (with corresponding label) for the given 64 // Gets all the data list values (with corresponding label) for the given
64 // element. 65 // element.
65 void GetDataListSuggestions(const blink::WebInputElement& element, 66 void GetDataListSuggestions(const blink::WebInputElement& element,
66 bool ignore_current_value, 67 bool ignore_current_value,
67 std::vector<base::string16>* values, 68 std::vector<base::string16>* values,
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after
286 ignore_text_changes_ = ignore; 287 ignore_text_changes_ = ignore;
287 } 288 }
288 289
289 void AutofillAgent::InputElementClicked(const WebInputElement& element, 290 void AutofillAgent::InputElementClicked(const WebInputElement& element,
290 bool was_focused, 291 bool was_focused,
291 bool is_focused) { 292 bool is_focused) {
292 if (was_focused) 293 if (was_focused)
293 ShowSuggestions(element, true, false, true, false); 294 ShowSuggestions(element, true, false, true, false);
294 } 295 }
295 296
296 void AutofillAgent::InputElementLostFocus() { 297 void AutofillAgent::TextAreaElementClicked(const WebTextAreaElement& element,
298 bool was_focused,
299 bool is_focused) {
300 if (was_focused)
301 ShowSuggestions(element, true, false, true, false);
302 }
303
304 // This applies to both input and textarea elements
305 void AutofillAgent::FormControlElementLostFocus() {
297 HideAutofillUI(); 306 HideAutofillUI();
298 } 307 }
299 308
300 void AutofillAgent::textFieldDidEndEditing(const WebInputElement& element) { 309 void AutofillAgent::textFieldDidEndEditing(const WebInputElement& element) {
301 password_autofill_agent_->TextFieldDidEndEditing(element); 310 password_autofill_agent_->TextFieldDidEndEditing(element);
302 has_shown_autofill_popup_for_current_edit_ = false; 311 has_shown_autofill_popup_for_current_edit_ = false;
303 Send(new AutofillHostMsg_DidEndTextFieldEditing(routing_id())); 312 Send(new AutofillHostMsg_DidEndTextFieldEditing(routing_id()));
304 } 313 }
305 314
306 void AutofillAgent::textFieldDidChange(const WebInputElement& element) { 315 // Text changes in input text field or textarea field
316 void AutofillAgent::textFieldDidChange(const WebFormControlElement& element) {
307 if (ignore_text_changes_) 317 if (ignore_text_changes_)
308 return; 318 return;
309 319
310 if (did_set_node_text_) { 320 if (did_set_node_text_) {
311 did_set_node_text_ = false; 321 did_set_node_text_ = false;
312 return; 322 return;
313 } 323 }
314 324
315 // We post a task for doing the Autofill as the caret position is not set 325 // We post a task for doing the Autofill as the caret position is not set
316 // properly at this point (http://bugs.webkit.org/show_bug.cgi?id=16976) and 326 // properly at this point (http://bugs.webkit.org/show_bug.cgi?id=16976) and
317 // it is needed to trigger autofill. 327 // it is needed to trigger autofill.
318 weak_ptr_factory_.InvalidateWeakPtrs(); 328 weak_ptr_factory_.InvalidateWeakPtrs();
319 base::MessageLoop::current()->PostTask( 329 base::MessageLoop::current()->PostTask(
320 FROM_HERE, 330 FROM_HERE,
321 base::Bind(&AutofillAgent::TextFieldDidChangeImpl, 331 base::Bind(&AutofillAgent::TextFieldDidChangeImpl,
322 weak_ptr_factory_.GetWeakPtr(), 332 weak_ptr_factory_.GetWeakPtr(),
323 element)); 333 element));
324 } 334 }
325 335
326 void AutofillAgent::TextFieldDidChangeImpl(const WebInputElement& element) { 336 void AutofillAgent::TextFieldDidChangeImpl(const WebFormControlElement& element)
337 {
327 // If the element isn't focused then the changes don't matter. This check is 338 // If the element isn't focused then the changes don't matter. This check is
328 // required to properly handle IME interactions. 339 // required to properly handle IME interactions.
329 if (!element.focused()) 340 if (!element.focused())
330 return; 341 return;
331 342
332 if (password_generation_agent_ && 343 const WebInputElement* input_element = toWebInputElement(&element);
333 password_generation_agent_->TextDidChangeInTextField(element)) { 344 if (IsAutofillableInputElement(input_element)) {
334 return; 345 if (password_generation_agent_ &&
335 } 346 password_generation_agent_->TextDidChangeInTextField(*input_element)) {
347 return;
348 }
336 349
337 if (password_autofill_agent_->TextDidChangeInTextField(element)) { 350 if (password_autofill_agent_->TextDidChangeInTextField(*input_element)) {
338 element_ = element; 351 element_ = element;
339 return; 352 return;
353 }
340 } 354 }
341 355
342 ShowSuggestions(element, false, true, false, false); 356 ShowSuggestions(element, false, true, false, false);
343 357
344 FormData form; 358 FormData form;
345 FormFieldData field; 359 FormFieldData field;
346 if (FindFormAndFieldForInputElement(element, &form, &field, REQUIRE_NONE)) { 360 if ((IsAutofillableInputElement(input_element) ||
361 IsTextAreaElement(element)) &&
362 FindFormAndFieldForFormControlElement(element,
363 &form,
364 &field,
365 REQUIRE_NONE)) {
347 Send(new AutofillHostMsg_TextFieldDidChange(routing_id(), form, field, 366 Send(new AutofillHostMsg_TextFieldDidChange(routing_id(), form, field,
348 base::TimeTicks::Now())); 367 base::TimeTicks::Now()));
349 } 368 }
350 } 369 }
351 370
352 void AutofillAgent::textFieldDidReceiveKeyDown(const WebInputElement& element, 371 void AutofillAgent::textFieldDidReceiveKeyDown(const WebInputElement& element,
353 const WebKeyboardEvent& event) { 372 const WebKeyboardEvent& event) {
354 if (password_autofill_agent_->TextFieldHandlingKeyDown(element, event)) { 373 if (password_autofill_agent_->TextFieldHandlingKeyDown(element, event)) {
355 element_ = element; 374 element_ = element;
356 return; 375 return;
357 } 376 }
358 377
359 if (event.windowsKeyCode == ui::VKEY_DOWN || 378 if (event.windowsKeyCode == ui::VKEY_DOWN ||
360 event.windowsKeyCode == ui::VKEY_UP) 379 event.windowsKeyCode == ui::VKEY_UP)
361 ShowSuggestions(element, true, true, true, false); 380 ShowSuggestions(element, true, true, true, false);
362 } 381 }
363 382
364 void AutofillAgent::openTextDataListChooser(const WebInputElement& element) { 383 void AutofillAgent::openTextDataListChooser(const WebInputElement& element) {
365 ShowSuggestions(element, true, false, false, true); 384 ShowSuggestions(element, true, false, false, true);
366 } 385 }
367 386
368 void AutofillAgent::AcceptDataListSuggestion( 387 void AutofillAgent::AcceptDataListSuggestion(
369 const base::string16& suggested_value) { 388 const base::string16& suggested_value) {
389 WebInputElement* input_element = toWebInputElement(&element_);
370 base::string16 new_value = suggested_value; 390 base::string16 new_value = suggested_value;
371 // If this element takes multiple values then replace the last part with 391 // If this element takes multiple values then replace the last part with
372 // the suggestion. 392 // the suggestion.
373 if (element_.isMultiple() && 393 if (input_element->isMultiple() &&
374 element_.formControlType() == WebString::fromUTF8("email")) { 394 input_element->formControlType() == WebString::fromUTF8("email")) {
375 std::vector<base::string16> parts; 395 std::vector<base::string16> parts;
376 396
377 base::SplitStringDontTrim(element_.editingValue(), ',', &parts); 397 base::SplitStringDontTrim(input_element->editingValue(), ',', &parts);
378 if (parts.size() == 0) 398 if (parts.size() == 0)
379 parts.push_back(base::string16()); 399 parts.push_back(base::string16());
380 400
381 base::string16 last_part = parts.back(); 401 base::string16 last_part = parts.back();
382 // We want to keep just the leading whitespace. 402 // We want to keep just the leading whitespace.
383 for (size_t i = 0; i < last_part.size(); ++i) { 403 for (size_t i = 0; i < last_part.size(); ++i) {
384 if (!IsWhitespace(last_part[i])) { 404 if (!IsWhitespace(last_part[i])) {
385 last_part = last_part.substr(0, i); 405 last_part = last_part.substr(0, i);
386 break; 406 break;
387 } 407 }
388 } 408 }
389 last_part.append(suggested_value); 409 last_part.append(suggested_value);
390 parts[parts.size() - 1] = last_part; 410 parts[parts.size() - 1] = last_part;
391 411
392 new_value = JoinString(parts, ','); 412 new_value = JoinString(parts, ',');
393 } 413 }
394 SetNodeText(new_value, &element_); 414 SetNodeText(new_value, input_element);
395 } 415 }
396 416
397 void AutofillAgent::OnFormDataFilled(int query_id, 417 void AutofillAgent::OnFormDataFilled(int query_id,
398 const FormData& form) { 418 const FormData& form) {
399 if (!render_view()->GetWebView() || query_id != autofill_query_id_) 419 if (!render_view()->GetWebView() || query_id != autofill_query_id_)
400 return; 420 return;
401 421
402 was_query_node_autofilled_ = element_.isAutofilled(); 422 was_query_node_autofilled_ = element_.isAutofilled();
403 423
404 switch (autofill_action_) { 424 switch (autofill_action_) {
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
446 // TODO(isherman): There seem to be rare cases where this code *is* 466 // TODO(isherman): There seem to be rare cases where this code *is*
447 // reachable: see [ http://crbug.com/96321#c6 ]. Ideally we would 467 // reachable: see [ http://crbug.com/96321#c6 ]. Ideally we would
448 // understand those cases and fix the code to avoid them. However, so far I 468 // understand those cases and fix the code to avoid them. However, so far I
449 // have been unable to reproduce such a case locally. If you hit this 469 // have been unable to reproduce such a case locally. If you hit this
450 // NOTREACHED(), please file a bug against me. 470 // NOTREACHED(), please file a bug against me.
451 NOTREACHED(); 471 NOTREACHED();
452 } 472 }
453 } 473 }
454 474
455 void AutofillAgent::OnSetNodeText(const base::string16& value) { 475 void AutofillAgent::OnSetNodeText(const base::string16& value) {
456 SetNodeText(value, &element_); 476 WebInputElement* input_element = toWebInputElement(&element_);
477 SetNodeText(value, input_element);
457 } 478 }
458 479
459 void AutofillAgent::OnAcceptDataListSuggestion(const base::string16& value) { 480 void AutofillAgent::OnAcceptDataListSuggestion(const base::string16& value) {
460 AcceptDataListSuggestion(value); 481 AcceptDataListSuggestion(value);
461 } 482 }
462 483
463 void AutofillAgent::OnAcceptPasswordAutofillSuggestion( 484 void AutofillAgent::OnAcceptPasswordAutofillSuggestion(
464 const base::string16& username) { 485 const base::string16& username) {
465 // We need to make sure this is handled here because the browser process 486 // We need to make sure this is handled here because the browser process
466 // skipped it handling because it believed it would be handled here. If it 487 // skipped it handling because it believed it would be handled here. If it
(...skipping 15 matching lines...) Expand all
482 result = WebFormElement::AutocompleteResultErrorInvalid; 503 result = WebFormElement::AutocompleteResultErrorInvalid;
483 } 504 }
484 505
485 in_flight_request_form_.finishRequestAutocomplete(result); 506 in_flight_request_form_.finishRequestAutocomplete(result);
486 in_flight_request_form_.reset(); 507 in_flight_request_form_.reset();
487 } 508 }
488 509
489 void AutofillAgent::OnPageShown() { 510 void AutofillAgent::OnPageShown() {
490 } 511 }
491 512
492 void AutofillAgent::ShowSuggestions(const WebInputElement& element, 513 void AutofillAgent::ShowSuggestions(const WebFormControlElement& element,
493 bool autofill_on_empty_values, 514 bool autofill_on_empty_values,
494 bool requires_caret_at_end, 515 bool requires_caret_at_end,
495 bool display_warning_if_disabled, 516 bool display_warning_if_disabled,
496 bool datalist_only) { 517 bool datalist_only) {
497 if (!element.isEnabled() || element.isReadOnly() || !element.isTextField() || 518 const WebInputElement* input_element = toWebInputElement(&element);
498 element.isPasswordField()) 519 if (!element.isEnabled() || element.isReadOnly())
499 return;
500 if (!datalist_only && !element.suggestedValue().isEmpty())
501 return; 520 return;
502 521
503 // Don't attempt to autofill with values that are too large or if filling 522 if (IsAutofillableInputElement(input_element)) {
504 // criteria are not met. 523 if (!input_element->isTextField() || input_element->isPasswordField())
505 WebString value = element.editingValue(); 524 return;
506 if (!datalist_only && 525 if (!datalist_only && !input_element->suggestedValue().isEmpty())
507 (value.length() > kMaxDataLength || 526 return;
508 (!autofill_on_empty_values && value.isEmpty()) || 527 // Don't attempt to autofill with values that are too large or if filling
509 (requires_caret_at_end && 528 // criteria are not met.
510 (element.selectionStart() != element.selectionEnd() || 529 WebString value = input_element->editingValue();
511 element.selectionEnd() != static_cast<int>(value.length()))))) { 530 if (!datalist_only &&
512 // Any popup currently showing is obsolete. 531 (value.length() > kMaxDataLength ||
513 HideAutofillUI(); 532 (!autofill_on_empty_values && value.isEmpty()) ||
533 (requires_caret_at_end &&
534 (input_element->selectionStart() != input_element->selectionEnd() ||
535 input_element->selectionEnd() !=
536 static_cast<int>(value.length()))))) {
537 // Any popup currently showing is obsolete.
538 HideAutofillUI();
539 return;
540 }
541
542 if (password_autofill_agent_->ShowSuggestions(*input_element))
543 return;
544 } else if (IsTextAreaElement(element) &&
545 !element.toConst<WebTextAreaElement>().suggestedValue().isEmpty())
514 return; 546 return;
515 }
516 547
517 element_ = element; 548 element_ = element;
518 if (password_autofill_agent_->ShowSuggestions(element))
519 return;
520 549
521 // If autocomplete is disabled at the field level, ensure that the native 550 // If autocomplete is disabled at the field level, ensure that the native
522 // UI won't try to show a warning, since that may conflict with a custom 551 // UI won't try to show a warning, since that may conflict with a custom
523 // popup. Note that we cannot use the WebKit method element.autoComplete() 552 // popup. Note that we cannot use the WebKit method element.autoComplete()
524 // as it does not allow us to distinguish the case where autocomplete is 553 // as it does not allow us to distinguish the case where autocomplete is
525 // disabled for *both* the element and for the form. 554 // disabled for *both* the element and for the form.
526 const base::string16 autocomplete_attribute = 555 const base::string16 autocomplete_attribute =
527 element.getAttribute("autocomplete"); 556 element.getAttribute("autocomplete");
528 if (LowerCaseEqualsASCII(autocomplete_attribute, "off")) 557 if (LowerCaseEqualsASCII(autocomplete_attribute, "off"))
529 display_warning_if_disabled = false; 558 display_warning_if_disabled = false;
530 559
531 QueryAutofillSuggestions(element, 560 QueryAutofillSuggestions(element,
532 display_warning_if_disabled, 561 display_warning_if_disabled,
533 datalist_only); 562 datalist_only);
534 } 563 }
535 564
536 void AutofillAgent::QueryAutofillSuggestions(const WebInputElement& element, 565 void AutofillAgent::QueryAutofillSuggestions(
537 bool display_warning_if_disabled, 566 const WebFormControlElement& element,
538 bool datalist_only) { 567 bool display_warning_if_disabled,
568 bool datalist_only) {
539 if (!element.document().frame()) 569 if (!element.document().frame())
540 return; 570 return;
541 571
542 static int query_counter = 0; 572 static int query_counter = 0;
543 autofill_query_id_ = query_counter++; 573 autofill_query_id_ = query_counter++;
544 display_warning_if_disabled_ = display_warning_if_disabled; 574 display_warning_if_disabled_ = display_warning_if_disabled;
575 const WebInputElement* input_element = toWebInputElement(&element);
545 576
546 // If autocomplete is disabled at the form level, we want to see if there 577 // If autocomplete is disabled at the form level, we want to see if there
547 // would have been any suggestions were it enabled, so that we can show a 578 // would have been any suggestions were it enabled, so that we can show a
548 // warning. Otherwise, we want to ignore fields that disable autocomplete, so 579 // warning. Otherwise, we want to ignore fields that disable autocomplete, so
549 // that the suggestions list does not include suggestions for these form 580 // that the suggestions list does not include suggestions for these form
550 // fields -- see comment 1 on http://crbug.com/69914 581 // fields -- see comment 1 on http://crbug.com/69914
551 const RequirementsMask requirements = 582 RequirementsMask requirements = REQUIRE_NONE;
552 element.autoComplete() ? REQUIRE_AUTOCOMPLETE : REQUIRE_NONE; 583 if (IsAutofillableInputElement(input_element))
584 requirements =
585 input_element->autoComplete() ? REQUIRE_AUTOCOMPLETE : REQUIRE_NONE;
553 586
554 FormData form; 587 FormData form;
555 FormFieldData field; 588 FormFieldData field;
556 if (!FindFormAndFieldForInputElement(element, &form, &field, requirements)) { 589 if (!FindFormAndFieldForFormControlElement(element, &form,
590 &field, requirements)) {
557 // If we didn't find the cached form, at least let autocomplete have a shot 591 // If we didn't find the cached form, at least let autocomplete have a shot
558 // at providing suggestions. 592 // at providing suggestions.
559 WebFormControlElementToFormField(element, EXTRACT_VALUE, &field); 593 WebFormControlElementToFormField(element, EXTRACT_VALUE, &field);
560 } 594 }
561 if (datalist_only) 595 if (datalist_only)
562 field.should_autocomplete = false; 596 field.should_autocomplete = false;
563 597
564 gfx::RectF bounding_box_scaled = 598 gfx::RectF bounding_box_scaled =
565 GetScaledBoundingBox(web_view_->pageScaleFactor(), &element_); 599 GetScaledBoundingBox(web_view_->pageScaleFactor(), &element_);
566 600
567 // Find the datalist values and send them to the browser process. 601 if (IsAutofillableInputElement(input_element)) {
568 std::vector<base::string16> data_list_values; 602 // Find the datalist values and send them to the browser process.
569 std::vector<base::string16> data_list_labels; 603 std::vector<base::string16> data_list_values;
570 GetDataListSuggestions(element_, 604 std::vector<base::string16> data_list_labels;
605 GetDataListSuggestions(*input_element,
571 datalist_only, 606 datalist_only,
572 &data_list_values, 607 &data_list_values,
573 &data_list_labels); 608 &data_list_labels);
574 TrimStringVectorForIPC(&data_list_values); 609 TrimStringVectorForIPC(&data_list_values);
575 TrimStringVectorForIPC(&data_list_labels); 610 TrimStringVectorForIPC(&data_list_labels);
576 611
577 Send(new AutofillHostMsg_SetDataList(routing_id(), 612 Send(new AutofillHostMsg_SetDataList(routing_id(),
578 data_list_values, 613 data_list_values,
579 data_list_labels)); 614 data_list_labels));
580 615 }
581 Send(new AutofillHostMsg_QueryFormFieldAutofill(routing_id(), 616 Send(new AutofillHostMsg_QueryFormFieldAutofill(routing_id(),
582 autofill_query_id_, 617 autofill_query_id_,
583 form, 618 form,
584 field, 619 field,
585 bounding_box_scaled, 620 bounding_box_scaled,
586 display_warning_if_disabled)); 621 display_warning_if_disabled));
587 } 622 }
588 623
589 void AutofillAgent::FillAutofillFormData(const WebNode& node, 624 void AutofillAgent::FillAutofillFormData(const WebNode& node,
590 int unique_id, 625 int unique_id,
591 AutofillAction action) { 626 AutofillAction action) {
592 DCHECK_GT(unique_id, 0); 627 DCHECK_GT(unique_id, 0);
593 628
594 static int query_counter = 0; 629 static int query_counter = 0;
595 autofill_query_id_ = query_counter++; 630 autofill_query_id_ = query_counter++;
596 631
597 FormData form; 632 FormData form;
598 FormFieldData field; 633 FormFieldData field;
599 if (!FindFormAndFieldForInputElement(node.toConst<WebInputElement>(), &form, 634 if (!FindFormAndFieldForFormControlElement(
600 &field, REQUIRE_AUTOCOMPLETE)) { 635 node.toConst<WebFormControlElement>(),
636 &form,
637 &field,
638 REQUIRE_AUTOCOMPLETE)) {
601 return; 639 return;
602 } 640 }
603 641
604 autofill_action_ = action; 642 autofill_action_ = action;
605 Send(new AutofillHostMsg_FillAutofillFormData( 643 Send(new AutofillHostMsg_FillAutofillFormData(
606 routing_id(), autofill_query_id_, form, field, unique_id)); 644 routing_id(), autofill_query_id_, form, field, unique_id));
607 } 645 }
608 646
609 void AutofillAgent::SetNodeText(const base::string16& value, 647 void AutofillAgent::SetNodeText(const base::string16& value,
610 blink::WebInputElement* node) { 648 blink::WebInputElement* node) {
(...skipping 19 matching lines...) Expand all
630 // Only monitors dynamic forms created in the top frame. Dynamic forms 668 // Only monitors dynamic forms created in the top frame. Dynamic forms
631 // inserted in iframes are not captured yet. 669 // inserted in iframes are not captured yet.
632 if (!frame->parent()) { 670 if (!frame->parent()) {
633 password_autofill_agent_->OnDynamicFormsSeen(frame); 671 password_autofill_agent_->OnDynamicFormsSeen(frame);
634 return; 672 return;
635 } 673 }
636 } 674 }
637 } 675 }
638 676
639 } // namespace autofill 677 } // namespace autofill
OLDNEW
« no previous file with comments | « components/autofill/content/renderer/autofill_agent.h ('k') | components/autofill/content/renderer/form_autofill_util.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698