1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/autofill/content/renderer/autofill_agent.h"
7 #include "base/auto_reset.h"
9 #include "base/command_line.h"
10 #include "base/i18n/case_conversion.h"
11 #include "base/location.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/strings/string_split.h"
14 #include "base/strings/string_util.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "base/thread_task_runner_handle.h"
17 #include "base/time/time.h"
18 #include "components/autofill/content/common/autofill_messages.h"
19 #include "components/autofill/content/renderer/form_autofill_util.h"
20 #include "components/autofill/content/renderer/page_click_tracker.h"
21 #include "components/autofill/content/renderer/password_autofill_agent.h"
22 #include "components/autofill/content/renderer/password_generation_agent.h"
23 #include "components/autofill/core/common/autofill_constants.h"
24 #include "components/autofill/core/common/autofill_data_validation.h"
25 #include "components/autofill/core/common/autofill_switches.h"
26 #include "components/autofill/core/common/form_data.h"
27 #include "components/autofill/core/common/form_data_predictions.h"
28 #include "components/autofill/core/common/form_field_data.h"
29 #include "components/autofill/core/common/password_form.h"
30 #include "components/autofill/core/common/web_element_descriptor.h"
31 #include "content/public/common/content_switches.h"
32 #include "content/public/common/ssl_status.h"
33 #include "content/public/common/url_constants.h"
34 #include "content/public/renderer/render_frame.h"
35 #include "content/public/renderer/render_view.h"
36 #include "net/cert/cert_status_flags.h"
37 #include "third_party/WebKit/public/platform/WebRect.h"
38 #include "third_party/WebKit/public/platform/WebURLRequest.h"
39 #include "third_party/WebKit/public/web/WebConsoleMessage.h"
40 #include "third_party/WebKit/public/web/WebDataSource.h"
41 #include "third_party/WebKit/public/web/WebDocument.h"
42 #include "third_party/WebKit/public/web/WebElementCollection.h"
43 #include "third_party/WebKit/public/web/WebFormControlElement.h"
44 #include "third_party/WebKit/public/web/WebFormElement.h"
45 #include "third_party/WebKit/public/web/WebInputEvent.h"
46 #include "third_party/WebKit/public/web/WebLocalFrame.h"
47 #include "third_party/WebKit/public/web/WebNode.h"
48 #include "third_party/WebKit/public/web/WebOptionElement.h"
49 #include "third_party/WebKit/public/web/WebTextAreaElement.h"
50 #include "third_party/WebKit/public/web/WebUserGestureIndicator.h"
51 #include "third_party/WebKit/public/web/WebView.h"
52 #include "ui/base/l10n/l10n_util.h"
53 #include "ui/events/keycodes/keyboard_codes.h"
55 using blink::WebAutofillClient
;
56 using blink::WebConsoleMessage
;
57 using blink::WebDocument
;
58 using blink::WebElement
;
59 using blink::WebElementCollection
;
60 using blink::WebFormControlElement
;
61 using blink::WebFormElement
;
62 using blink::WebFrame
;
63 using blink::WebInputElement
;
64 using blink::WebKeyboardEvent
;
65 using blink::WebLocalFrame
;
67 using blink::WebOptionElement
;
68 using blink::WebString
;
69 using blink::WebTextAreaElement
;
70 using blink::WebUserGestureIndicator
;
71 using blink::WebVector
;
77 // Gets all the data list values (with corresponding label) for the given
79 void GetDataListSuggestions(const WebInputElement
& element
,
80 bool ignore_current_value
,
81 std::vector
<base::string16
>* values
,
82 std::vector
<base::string16
>* labels
) {
83 WebElementCollection options
= element
.dataListOptions();
87 base::string16 prefix
;
88 if (!ignore_current_value
) {
89 prefix
= element
.editingValue();
90 if (element
.isMultiple() && element
.isEmailField()) {
91 const base::char16 comma
[2] = { ',', 0 };
92 std::vector
<base::string16
> parts
= base::SplitString(
93 prefix
, comma
, base::TRIM_WHITESPACE
, base::SPLIT_WANT_ALL
);
94 if (parts
.size() > 0) {
95 base::TrimWhitespace(parts
[parts
.size() - 1], base::TRIM_LEADING
,
100 prefix
= base::i18n::ToLower(prefix
);
101 for (WebOptionElement option
= options
.firstItem().to
<WebOptionElement
>();
102 !option
.isNull(); option
= options
.nextItem().to
<WebOptionElement
>()) {
103 if (!base::StartsWith(base::i18n::ToLower(base::string16(option
.value())),
104 prefix
, base::CompareCase::SENSITIVE
) ||
105 !element
.isValidValue(option
.value()))
108 values
->push_back(option
.value());
109 if (option
.value() != option
.label())
110 labels
->push_back(option
.label());
112 labels
->push_back(base::string16());
116 // Trim the vector before sending it to the browser process to ensure we
117 // don't send too much data through the IPC.
118 void TrimStringVectorForIPC(std::vector
<base::string16
>* strings
) {
119 // Limit the size of the vector.
120 if (strings
->size() > kMaxListSize
)
121 strings
->resize(kMaxListSize
);
123 // Limit the size of the strings in the vector.
124 for (size_t i
= 0; i
< strings
->size(); ++i
) {
125 if ((*strings
)[i
].length() > kMaxDataLength
)
126 (*strings
)[i
].resize(kMaxDataLength
);
130 // Extract FormData from the form element and return whether the operation was
132 bool ExtractFormDataOnSave(const WebFormElement
& form_element
, FormData
* data
) {
133 return WebFormElementToFormData(
134 form_element
, WebFormControlElement(),
135 static_cast<ExtractMask
>(EXTRACT_VALUE
| EXTRACT_OPTION_TEXT
), data
,
141 AutofillAgent::ShowSuggestionsOptions::ShowSuggestionsOptions()
142 : autofill_on_empty_values(false),
143 requires_caret_at_end(false),
144 datalist_only(false),
145 show_full_suggestion_list(false),
146 show_password_suggestions_only(false) {
149 AutofillAgent::AutofillAgent(content::RenderFrame
* render_frame
,
150 PasswordAutofillAgent
* password_autofill_agent
,
151 PasswordGenerationAgent
* password_generation_agent
)
152 : content::RenderFrameObserver(render_frame
),
153 form_cache_(*render_frame
->GetWebFrame()),
154 password_autofill_agent_(password_autofill_agent
),
155 password_generation_agent_(password_generation_agent
),
156 legacy_(render_frame
->GetRenderView(), this),
157 autofill_query_id_(0),
158 was_query_node_autofilled_(false),
159 has_shown_autofill_popup_for_current_edit_(false),
160 ignore_text_changes_(false),
161 is_popup_possibly_visible_(false),
162 is_generation_popup_possibly_visible_(false),
163 weak_ptr_factory_(this) {
164 render_frame
->GetWebFrame()->setAutofillClient(this);
166 // This owns itself, and will delete itself when |render_frame| is destructed
167 // (same as AutofillAgent). This object must be constructed after
168 // AutofillAgent so that password generation UI is shown before password
169 // manager UI (see https://crbug.com/498545).
170 new PageClickTracker(render_frame
, this);
173 AutofillAgent::~AutofillAgent() {}
175 bool AutofillAgent::FormDataCompare::operator()(const FormData
& lhs
,
176 const FormData
& rhs
) const {
177 if (lhs
.name
!= rhs
.name
)
178 return lhs
.name
< rhs
.name
;
179 if (lhs
.origin
!= rhs
.origin
)
180 return lhs
.origin
< rhs
.origin
;
181 if (lhs
.action
!= rhs
.action
)
182 return lhs
.action
< rhs
.action
;
183 if (lhs
.is_form_tag
!= rhs
.is_form_tag
)
184 return lhs
.is_form_tag
< rhs
.is_form_tag
;
185 return lhs
.fields
< rhs
.fields
;
188 bool AutofillAgent::OnMessageReceived(const IPC::Message
& message
) {
190 IPC_BEGIN_MESSAGE_MAP(AutofillAgent
, message
)
191 IPC_MESSAGE_HANDLER(AutofillMsg_FirstUserGestureObservedInTab
,
192 OnFirstUserGestureObservedInTab
)
193 IPC_MESSAGE_HANDLER(AutofillMsg_Ping
, OnPing
)
194 IPC_MESSAGE_HANDLER(AutofillMsg_FillForm
, OnFillForm
)
195 IPC_MESSAGE_HANDLER(AutofillMsg_PreviewForm
, OnPreviewForm
)
196 IPC_MESSAGE_HANDLER(AutofillMsg_FieldTypePredictionsAvailable
,
197 OnFieldTypePredictionsAvailable
)
198 IPC_MESSAGE_HANDLER(AutofillMsg_ClearForm
, OnClearForm
)
199 IPC_MESSAGE_HANDLER(AutofillMsg_ClearPreviewedForm
, OnClearPreviewedForm
)
200 IPC_MESSAGE_HANDLER(AutofillMsg_FillFieldWithValue
, OnFillFieldWithValue
)
201 IPC_MESSAGE_HANDLER(AutofillMsg_PreviewFieldWithValue
,
202 OnPreviewFieldWithValue
)
203 IPC_MESSAGE_HANDLER(AutofillMsg_AcceptDataListSuggestion
,
204 OnAcceptDataListSuggestion
)
205 IPC_MESSAGE_HANDLER(AutofillMsg_FillPasswordSuggestion
,
206 OnFillPasswordSuggestion
)
207 IPC_MESSAGE_HANDLER(AutofillMsg_PreviewPasswordSuggestion
,
208 OnPreviewPasswordSuggestion
)
209 IPC_MESSAGE_HANDLER(AutofillMsg_RequestAutocompleteResult
,
210 OnRequestAutocompleteResult
)
211 IPC_MESSAGE_UNHANDLED(handled
= false)
212 IPC_END_MESSAGE_MAP()
216 void AutofillAgent::DidCommitProvisionalLoad(bool is_new_navigation
,
217 bool is_same_page_navigation
) {
219 submitted_forms_
.clear();
222 void AutofillAgent::DidFinishDocumentLoad() {
226 void AutofillAgent::WillSendSubmitEvent(const WebFormElement
& form
) {
228 if (!ExtractFormDataOnSave(form
, &form_data
))
231 // The WillSendSubmitEvent function is called when there is a submit handler
232 // on the form, such as in the case of (but not restricted to)
233 // JavaScript-submitted forms. Sends a WillSubmitForm message to the browser
234 // and remembers for which form it did that in the current frame load, so that
235 // no additional message is sent if AutofillAgent::WillSubmitForm() is called
236 // (which is itself not guaranteed if the submit event is prevented by
238 Send(new AutofillHostMsg_WillSubmitForm(routing_id(), form_data
,
239 base::TimeTicks::Now()));
240 submitted_forms_
.insert(form_data
);
243 void AutofillAgent::WillSubmitForm(const WebFormElement
& form
) {
245 if (!ExtractFormDataOnSave(form
, &form_data
))
248 // If WillSubmitForm message had not been sent for this form, send it.
249 if (!submitted_forms_
.count(form_data
)) {
250 Send(new AutofillHostMsg_WillSubmitForm(routing_id(), form_data
,
251 base::TimeTicks::Now()));
254 Send(new AutofillHostMsg_FormSubmitted(routing_id(), form_data
));
257 void AutofillAgent::DidChangeScrollOffset() {
258 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
259 switches::kEnableAccessorySuggestionView
)) {
265 void AutofillAgent::FocusedNodeChanged(const WebNode
& node
) {
268 if (node
.isNull() || !node
.isElementNode())
271 WebElement web_element
= node
.toConst
<WebElement
>();
272 const WebInputElement
* element
= toWebInputElement(&web_element
);
274 if (!element
|| !element
->isEnabled() || element
->isReadOnly() ||
275 !element
->isTextField())
281 void AutofillAgent::FocusChangeComplete() {
282 WebDocument doc
= render_frame()->GetWebFrame()->document();
283 WebElement focused_element
;
285 focused_element
= doc
.focusedElement();
287 if (!focused_element
.isNull() && password_generation_agent_
&&
288 password_generation_agent_
->FocusedNodeHasChanged(focused_element
)) {
289 is_generation_popup_possibly_visible_
= true;
290 is_popup_possibly_visible_
= true;
294 void AutofillAgent::didRequestAutocomplete(
295 const WebFormElement
& form
) {
296 DCHECK_EQ(form
.document().frame(), render_frame()->GetWebFrame());
298 // Disallow the dialog over non-https or broken https, except when the
299 // ignore SSL flag is passed. See http://crbug.com/272512.
300 // TODO(palmer): this should be moved to the browser process after frames
301 // get their own processes.
302 GURL
url(form
.document().url());
303 content::SSLStatus ssl_status
=
304 render_frame()->GetRenderView()->GetSSLStatusOfFrame(
305 form
.document().frame());
306 bool is_safe
= url
.SchemeIsCryptographic() &&
307 !net::IsCertStatusError(ssl_status
.cert_status
);
308 bool allow_unsafe
= base::CommandLine::ForCurrentProcess()->HasSwitch(
309 ::switches::kReduceSecurityForTesting
);
311 std::string error_message
;
312 if (!in_flight_request_form_
.isNull()) {
313 error_message
= "already active.";
314 } else if (!is_safe
&& !allow_unsafe
) {
316 "must use a secure connection or --reduce-security-for-testing.";
317 } else if (!WebFormElementToFormData(form
,
318 WebFormControlElement(),
319 static_cast<ExtractMask
>(
321 EXTRACT_OPTION_TEXT
|
325 error_message
= "failed to parse form.";
328 if (!error_message
.empty()) {
329 WebConsoleMessage console_message
= WebConsoleMessage(
330 WebConsoleMessage::LevelLog
,
331 WebString(base::ASCIIToUTF16("requestAutocomplete: ") +
332 base::ASCIIToUTF16(error_message
)));
333 form
.document().frame()->addMessageToConsole(console_message
);
334 WebFormElement(form
).finishRequestAutocomplete(
335 WebFormElement::AutocompleteResultErrorDisabled
);
339 // Cancel any pending Autofill requests and hide any currently showing popups.
340 ++autofill_query_id_
;
343 in_flight_request_form_
= form
;
344 Send(new AutofillHostMsg_RequestAutocomplete(routing_id(), form_data
));
347 void AutofillAgent::setIgnoreTextChanges(bool ignore
) {
348 ignore_text_changes_
= ignore
;
351 void AutofillAgent::FormControlElementClicked(
352 const WebFormControlElement
& element
,
354 // TODO(estade): Remove this check when PageClickTracker is per-frame.
355 if (element
.document().frame() != render_frame()->GetWebFrame())
358 const WebInputElement
* input_element
= toWebInputElement(&element
);
359 if (!input_element
&& !IsTextAreaElement(element
))
362 ShowSuggestionsOptions options
;
363 options
.autofill_on_empty_values
= true;
364 options
.show_full_suggestion_list
= element
.isAutofilled();
366 // On Android, default to showing the dropdown on field focus.
367 // On desktop, require an extra click after field focus.
368 // See http://crbug.com/427660
369 #if defined(OS_ANDROID)
370 bool single_click_autofill
=
371 !base::CommandLine::ForCurrentProcess()->HasSwitch(
372 switches::kDisableSingleClickAutofill
);
374 bool single_click_autofill
=
375 base::CommandLine::ForCurrentProcess()->HasSwitch(
376 switches::kEnableSingleClickAutofill
);
379 if (!single_click_autofill
) {
380 // Show full suggestions when clicking on an already-focused form field. On
381 // the initial click (not focused yet), only show password suggestions.
382 options
.show_full_suggestion_list
=
383 options
.show_full_suggestion_list
|| was_focused
;
384 options
.show_password_suggestions_only
= !was_focused
;
386 ShowSuggestions(element
, options
);
389 void AutofillAgent::textFieldDidEndEditing(const WebInputElement
& element
) {
390 password_autofill_agent_
->TextFieldDidEndEditing(element
);
391 has_shown_autofill_popup_for_current_edit_
= false;
392 Send(new AutofillHostMsg_DidEndTextFieldEditing(routing_id()));
395 void AutofillAgent::textFieldDidChange(const WebFormControlElement
& element
) {
396 DCHECK(toWebInputElement(&element
) || IsTextAreaElement(element
));
397 if (ignore_text_changes_
)
400 if (!IsUserGesture())
403 // We post a task for doing the Autofill as the caret position is not set
404 // properly at this point (http://bugs.webkit.org/show_bug.cgi?id=16976) and
405 // it is needed to trigger autofill.
406 weak_ptr_factory_
.InvalidateWeakPtrs();
407 base::ThreadTaskRunnerHandle::Get()->PostTask(
408 FROM_HERE
, base::Bind(&AutofillAgent::TextFieldDidChangeImpl
,
409 weak_ptr_factory_
.GetWeakPtr(), element
));
412 void AutofillAgent::TextFieldDidChangeImpl(
413 const WebFormControlElement
& element
) {
414 // If the element isn't focused then the changes don't matter. This check is
415 // required to properly handle IME interactions.
416 if (!element
.focused())
419 const WebInputElement
* input_element
= toWebInputElement(&element
);
421 // |password_autofill_agent_| keeps track of all text changes even if
422 // it isn't displaying UI.
423 password_autofill_agent_
->UpdateStateForTextChange(*input_element
);
425 if (password_generation_agent_
&&
426 password_generation_agent_
->TextDidChangeInTextField(*input_element
)) {
427 is_popup_possibly_visible_
= true;
431 if (password_autofill_agent_
->TextDidChangeInTextField(*input_element
)) {
432 is_popup_possibly_visible_
= true;
438 ShowSuggestionsOptions options
;
439 options
.requires_caret_at_end
= true;
440 ShowSuggestions(element
, options
);
444 if (FindFormAndFieldForFormControlElement(element
, &form
, &field
)) {
445 Send(new AutofillHostMsg_TextFieldDidChange(routing_id(), form
, field
,
446 base::TimeTicks::Now()));
450 void AutofillAgent::textFieldDidReceiveKeyDown(const WebInputElement
& element
,
451 const WebKeyboardEvent
& event
) {
452 if (event
.windowsKeyCode
== ui::VKEY_DOWN
||
453 event
.windowsKeyCode
== ui::VKEY_UP
) {
454 ShowSuggestionsOptions options
;
455 options
.autofill_on_empty_values
= true;
456 options
.requires_caret_at_end
= true;
457 ShowSuggestions(element
, options
);
461 void AutofillAgent::openTextDataListChooser(const WebInputElement
& element
) {
462 ShowSuggestionsOptions options
;
463 options
.autofill_on_empty_values
= true;
464 options
.datalist_only
= true;
465 ShowSuggestions(element
, options
);
468 void AutofillAgent::dataListOptionsChanged(const WebInputElement
& element
) {
469 if (!is_popup_possibly_visible_
|| !element
.focused())
472 TextFieldDidChangeImpl(element
);
475 void AutofillAgent::firstUserGestureObserved() {
476 password_autofill_agent_
->FirstUserGestureObserved();
477 Send(new AutofillHostMsg_FirstUserGestureObserved(routing_id()));
480 void AutofillAgent::AcceptDataListSuggestion(
481 const base::string16
& suggested_value
) {
482 WebInputElement
* input_element
= toWebInputElement(&element_
);
483 DCHECK(input_element
);
484 base::string16 new_value
= suggested_value
;
485 // If this element takes multiple values then replace the last part with
487 if (input_element
->isMultiple() && input_element
->isEmailField()) {
488 std::vector
<base::string16
> parts
= base::SplitString(
489 base::StringPiece16(input_element
->editingValue()),
490 base::ASCIIToUTF16(","), base::KEEP_WHITESPACE
, base::SPLIT_WANT_ALL
);
491 if (parts
.size() == 0)
492 parts
.push_back(base::string16());
494 base::string16 last_part
= parts
.back();
495 // We want to keep just the leading whitespace.
496 for (size_t i
= 0; i
< last_part
.size(); ++i
) {
497 if (!base::IsUnicodeWhitespace(last_part
[i
])) {
498 last_part
= last_part
.substr(0, i
);
502 last_part
.append(suggested_value
);
503 parts
[parts
.size() - 1] = last_part
;
505 new_value
= base::JoinString(parts
, base::ASCIIToUTF16(","));
507 FillFieldWithValue(new_value
, input_element
);
510 void AutofillAgent::OnFieldTypePredictionsAvailable(
511 const std::vector
<FormDataPredictions
>& forms
) {
512 for (size_t i
= 0; i
< forms
.size(); ++i
) {
513 form_cache_
.ShowPredictions(forms
[i
]);
517 void AutofillAgent::OnFillForm(int query_id
, const FormData
& form
) {
518 if (query_id
!= autofill_query_id_
)
521 was_query_node_autofilled_
= element_
.isAutofilled();
522 FillForm(form
, element_
);
523 Send(new AutofillHostMsg_DidFillAutofillFormData(routing_id(),
524 base::TimeTicks::Now()));
527 void AutofillAgent::OnFirstUserGestureObservedInTab() {
528 password_autofill_agent_
->FirstUserGestureObserved();
531 void AutofillAgent::OnPing() {
532 Send(new AutofillHostMsg_PingAck(routing_id()));
535 void AutofillAgent::OnPreviewForm(int query_id
, const FormData
& form
) {
536 if (query_id
!= autofill_query_id_
)
539 was_query_node_autofilled_
= element_
.isAutofilled();
540 PreviewForm(form
, element_
);
541 Send(new AutofillHostMsg_DidPreviewAutofillFormData(routing_id()));
544 void AutofillAgent::OnClearForm() {
545 form_cache_
.ClearFormWithElement(element_
);
548 void AutofillAgent::OnClearPreviewedForm() {
549 if (!element_
.isNull()) {
550 if (password_autofill_agent_
->DidClearAutofillSelection(element_
))
553 ClearPreviewedFormWithElement(element_
, was_query_node_autofilled_
);
555 // TODO(isherman): There seem to be rare cases where this code *is*
556 // reachable: see [ http://crbug.com/96321#c6 ]. Ideally we would
557 // understand those cases and fix the code to avoid them. However, so far I
558 // have been unable to reproduce such a case locally. If you hit this
559 // NOTREACHED(), please file a bug against me.
564 void AutofillAgent::OnFillFieldWithValue(const base::string16
& value
) {
565 WebInputElement
* input_element
= toWebInputElement(&element_
);
567 FillFieldWithValue(value
, input_element
);
568 input_element
->setAutofilled(true);
572 void AutofillAgent::OnPreviewFieldWithValue(const base::string16
& value
) {
573 WebInputElement
* input_element
= toWebInputElement(&element_
);
575 PreviewFieldWithValue(value
, input_element
);
578 void AutofillAgent::OnAcceptDataListSuggestion(const base::string16
& value
) {
579 AcceptDataListSuggestion(value
);
582 void AutofillAgent::OnFillPasswordSuggestion(const base::string16
& username
,
583 const base::string16
& password
) {
584 bool handled
= password_autofill_agent_
->FillSuggestion(
591 void AutofillAgent::OnPreviewPasswordSuggestion(
592 const base::string16
& username
,
593 const base::string16
& password
) {
594 bool handled
= password_autofill_agent_
->PreviewSuggestion(
601 void AutofillAgent::OnRequestAutocompleteResult(
602 WebFormElement::AutocompleteResult result
,
603 const base::string16
& message
,
604 const FormData
& form_data
) {
605 if (in_flight_request_form_
.isNull())
608 if (result
== WebFormElement::AutocompleteResultSuccess
) {
609 FillFormIncludingNonFocusableElements(form_data
, in_flight_request_form_
);
610 if (!in_flight_request_form_
.checkValidity())
611 result
= WebFormElement::AutocompleteResultErrorInvalid
;
614 in_flight_request_form_
.finishRequestAutocomplete(result
);
616 if (!message
.empty()) {
617 const base::string16
prefix(base::ASCIIToUTF16("requestAutocomplete: "));
618 WebConsoleMessage console_message
= WebConsoleMessage(
619 WebConsoleMessage::LevelLog
, WebString(prefix
+ message
));
620 in_flight_request_form_
.document().frame()->addMessageToConsole(
624 in_flight_request_form_
.reset();
627 void AutofillAgent::ShowSuggestions(const WebFormControlElement
& element
,
628 const ShowSuggestionsOptions
& options
) {
629 if (!element
.isEnabled() || element
.isReadOnly())
631 if (!options
.datalist_only
&& !element
.suggestedValue().isEmpty())
634 const WebInputElement
* input_element
= toWebInputElement(&element
);
636 if (!input_element
->isTextField())
638 if (!options
.datalist_only
&& !input_element
->suggestedValue().isEmpty())
641 DCHECK(IsTextAreaElement(element
));
642 if (!element
.toConst
<WebTextAreaElement
>().suggestedValue().isEmpty())
646 // Don't attempt to autofill with values that are too large or if filling
647 // criteria are not met.
648 WebString value
= element
.editingValue();
649 if (!options
.datalist_only
&&
650 (value
.length() > kMaxDataLength
||
651 (!options
.autofill_on_empty_values
&& value
.isEmpty()) ||
652 (options
.requires_caret_at_end
&&
653 (element
.selectionStart() != element
.selectionEnd() ||
654 element
.selectionEnd() != static_cast<int>(value
.length()))))) {
655 // Any popup currently showing is obsolete.
661 if (IsAutofillableInputElement(input_element
) &&
662 password_autofill_agent_
->ShowSuggestions(
663 *input_element
, options
.show_full_suggestion_list
,
664 is_generation_popup_possibly_visible_
)) {
665 is_popup_possibly_visible_
= true;
669 if (is_generation_popup_possibly_visible_
)
672 if (options
.show_password_suggestions_only
)
675 // Password field elements should only have suggestions shown by the password
677 if (input_element
&& input_element
->isPasswordField())
680 QueryAutofillSuggestions(element
, options
.datalist_only
);
683 void AutofillAgent::QueryAutofillSuggestions(
684 const WebFormControlElement
& element
,
685 bool datalist_only
) {
686 if (!element
.document().frame())
689 DCHECK(toWebInputElement(&element
) || IsTextAreaElement(element
));
691 static int query_counter
= 0;
692 autofill_query_id_
= query_counter
++;
696 if (!FindFormAndFieldForFormControlElement(element
, &form
, &field
)) {
697 // If we didn't find the cached form, at least let autocomplete have a shot
698 // at providing suggestions.
699 WebFormControlElementToFormField(element
, EXTRACT_VALUE
, &field
);
702 field
.should_autocomplete
= false;
704 gfx::RectF bounding_box_scaled
= GetScaledBoundingBox(
705 render_frame()->GetRenderView()->GetWebView()->pageScaleFactor(),
708 std::vector
<base::string16
> data_list_values
;
709 std::vector
<base::string16
> data_list_labels
;
710 const WebInputElement
* input_element
= toWebInputElement(&element
);
712 // Find the datalist values and send them to the browser process.
713 GetDataListSuggestions(*input_element
,
717 TrimStringVectorForIPC(&data_list_values
);
718 TrimStringVectorForIPC(&data_list_labels
);
721 is_popup_possibly_visible_
= true;
722 Send(new AutofillHostMsg_SetDataList(routing_id(),
726 Send(new AutofillHostMsg_QueryFormFieldAutofill(
727 routing_id(), autofill_query_id_
, form
, field
, bounding_box_scaled
));
730 void AutofillAgent::FillFieldWithValue(const base::string16
& value
,
731 WebInputElement
* node
) {
732 base::AutoReset
<bool> auto_reset(&ignore_text_changes_
, true);
733 node
->setEditingValue(value
.substr(0, node
->maxLength()));
736 void AutofillAgent::PreviewFieldWithValue(const base::string16
& value
,
737 WebInputElement
* node
) {
738 was_query_node_autofilled_
= element_
.isAutofilled();
739 node
->setSuggestedValue(value
.substr(0, node
->maxLength()));
740 node
->setAutofilled(true);
741 PreviewSuggestion(node
->suggestedValue(), node
->value(), node
);
744 void AutofillAgent::ProcessForms() {
745 // Record timestamp of when the forms are first seen. This is used to
746 // measure the overhead of the Autofill feature.
747 base::TimeTicks forms_seen_timestamp
= base::TimeTicks::Now();
749 WebLocalFrame
* frame
= render_frame()->GetWebFrame();
750 std::vector
<FormData
> forms
= form_cache_
.ExtractNewForms();
752 // Always communicate to browser process for topmost frame.
753 if (!forms
.empty() || !frame
->parent()) {
754 Send(new AutofillHostMsg_FormsSeen(routing_id(), forms
,
755 forms_seen_timestamp
));
759 void AutofillAgent::HidePopup() {
760 if (!is_popup_possibly_visible_
)
762 is_popup_possibly_visible_
= false;
763 is_generation_popup_possibly_visible_
= false;
764 Send(new AutofillHostMsg_HidePopup(routing_id()));
767 bool AutofillAgent::IsUserGesture() const {
768 return WebUserGestureIndicator::isProcessingUserGesture();
771 void AutofillAgent::didAssociateFormControls(const WebVector
<WebNode
>& nodes
) {
772 for (size_t i
= 0; i
< nodes
.size(); ++i
) {
773 WebLocalFrame
* frame
= nodes
[i
].document().frame();
774 // Only monitors dynamic forms created in the top frame. Dynamic forms
775 // inserted in iframes are not captured yet. Frame is only processed
776 // if it has finished loading, otherwise you can end up with a partially
778 if (frame
&& !frame
->isLoading()) {
780 password_autofill_agent_
->OnDynamicFormsSeen();
781 if (password_generation_agent_
)
782 password_generation_agent_
->OnDynamicFormsSeen();
788 void AutofillAgent::ajaxSucceeded() {
789 password_autofill_agent_
->AJAXSucceeded();
792 // LegacyAutofillAgent ---------------------------------------------------------
794 AutofillAgent::LegacyAutofillAgent::LegacyAutofillAgent(
795 content::RenderView
* render_view
,
796 AutofillAgent
* agent
)
797 : content::RenderViewObserver(render_view
), agent_(agent
) {
800 AutofillAgent::LegacyAutofillAgent::~LegacyAutofillAgent() {
803 void AutofillAgent::LegacyAutofillAgent::OnDestruct() {
804 // No-op. Don't delete |this|.
807 void AutofillAgent::LegacyAutofillAgent::FocusChangeComplete() {
808 agent_
->FocusChangeComplete();
811 } // namespace autofill