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/autofill_util.h"
27 #include "components/autofill/core/common/form_data.h"
28 #include "components/autofill/core/common/form_data_predictions.h"
29 #include "components/autofill/core/common/form_field_data.h"
30 #include "components/autofill/core/common/password_form.h"
31 #include "components/autofill/core/common/web_element_descriptor.h"
32 #include "content/public/common/content_switches.h"
33 #include "content/public/common/ssl_status.h"
34 #include "content/public/common/url_constants.h"
35 #include "content/public/renderer/render_frame.h"
36 #include "content/public/renderer/render_view.h"
37 #include "net/cert/cert_status_flags.h"
38 #include "third_party/WebKit/public/platform/WebRect.h"
39 #include "third_party/WebKit/public/platform/WebURLRequest.h"
40 #include "third_party/WebKit/public/web/WebConsoleMessage.h"
41 #include "third_party/WebKit/public/web/WebDataSource.h"
42 #include "third_party/WebKit/public/web/WebDocument.h"
43 #include "third_party/WebKit/public/web/WebElementCollection.h"
44 #include "third_party/WebKit/public/web/WebFormControlElement.h"
45 #include "third_party/WebKit/public/web/WebFormElement.h"
46 #include "third_party/WebKit/public/web/WebInputEvent.h"
47 #include "third_party/WebKit/public/web/WebLocalFrame.h"
48 #include "third_party/WebKit/public/web/WebNode.h"
49 #include "third_party/WebKit/public/web/WebOptionElement.h"
50 #include "third_party/WebKit/public/web/WebTextAreaElement.h"
51 #include "third_party/WebKit/public/web/WebUserGestureIndicator.h"
52 #include "third_party/WebKit/public/web/WebView.h"
53 #include "ui/base/l10n/l10n_util.h"
54 #include "ui/events/keycodes/keyboard_codes.h"
56 using blink::WebAutofillClient
;
57 using blink::WebConsoleMessage
;
58 using blink::WebDocument
;
59 using blink::WebElement
;
60 using blink::WebElementCollection
;
61 using blink::WebFormControlElement
;
62 using blink::WebFormElement
;
63 using blink::WebFrame
;
64 using blink::WebInputElement
;
65 using blink::WebKeyboardEvent
;
66 using blink::WebLocalFrame
;
68 using blink::WebOptionElement
;
69 using blink::WebString
;
70 using blink::WebTextAreaElement
;
71 using blink::WebUserGestureIndicator
;
72 using blink::WebVector
;
78 // Gets all the data list values (with corresponding label) for the given
80 void GetDataListSuggestions(const WebInputElement
& element
,
81 bool ignore_current_value
,
82 std::vector
<base::string16
>* values
,
83 std::vector
<base::string16
>* labels
) {
84 WebElementCollection options
= element
.dataListOptions();
88 base::string16 prefix
;
89 if (!ignore_current_value
) {
90 prefix
= element
.editingValue();
91 if (element
.isMultiple() && element
.isEmailField()) {
92 const base::char16 comma
[2] = { ',', 0 };
93 std::vector
<base::string16
> parts
= base::SplitString(
94 prefix
, comma
, base::TRIM_WHITESPACE
, base::SPLIT_WANT_ALL
);
95 if (parts
.size() > 0) {
96 base::TrimWhitespace(parts
[parts
.size() - 1], base::TRIM_LEADING
,
101 prefix
= base::i18n::ToLower(prefix
);
102 for (WebOptionElement option
= options
.firstItem().to
<WebOptionElement
>();
103 !option
.isNull(); option
= options
.nextItem().to
<WebOptionElement
>()) {
104 if (!base::StartsWith(base::i18n::ToLower(base::string16(option
.value())),
105 prefix
, base::CompareCase::SENSITIVE
) ||
106 !element
.isValidValue(option
.value()))
109 values
->push_back(option
.value());
110 if (option
.value() != option
.label())
111 labels
->push_back(option
.label());
113 labels
->push_back(base::string16());
117 // Trim the vector before sending it to the browser process to ensure we
118 // don't send too much data through the IPC.
119 void TrimStringVectorForIPC(std::vector
<base::string16
>* strings
) {
120 // Limit the size of the vector.
121 if (strings
->size() > kMaxListSize
)
122 strings
->resize(kMaxListSize
);
124 // Limit the size of the strings in the vector.
125 for (size_t i
= 0; i
< strings
->size(); ++i
) {
126 if ((*strings
)[i
].length() > kMaxDataLength
)
127 (*strings
)[i
].resize(kMaxDataLength
);
131 // Extract FormData from the form element and return whether the operation was
133 bool ExtractFormDataOnSave(const WebFormElement
& form_element
, FormData
* data
) {
134 return WebFormElementToFormData(
135 form_element
, WebFormControlElement(),
136 static_cast<ExtractMask
>(EXTRACT_VALUE
| EXTRACT_OPTION_TEXT
), data
,
142 AutofillAgent::ShowSuggestionsOptions::ShowSuggestionsOptions()
143 : autofill_on_empty_values(false),
144 requires_caret_at_end(false),
145 datalist_only(false),
146 show_full_suggestion_list(false),
147 show_password_suggestions_only(false) {
150 AutofillAgent::AutofillAgent(content::RenderFrame
* render_frame
,
151 PasswordAutofillAgent
* password_autofill_agent
,
152 PasswordGenerationAgent
* password_generation_agent
)
153 : content::RenderFrameObserver(render_frame
),
154 form_cache_(*render_frame
->GetWebFrame()),
155 password_autofill_agent_(password_autofill_agent
),
156 password_generation_agent_(password_generation_agent
),
157 legacy_(render_frame
->GetRenderView(), this),
158 autofill_query_id_(0),
159 was_query_node_autofilled_(false),
160 has_shown_autofill_popup_for_current_edit_(false),
161 ignore_text_changes_(false),
162 is_popup_possibly_visible_(false),
163 is_generation_popup_possibly_visible_(false),
164 weak_ptr_factory_(this) {
165 render_frame
->GetWebFrame()->setAutofillClient(this);
167 // This owns itself, and will delete itself when |render_frame| is destructed
168 // (same as AutofillAgent). This object must be constructed after
169 // AutofillAgent so that password generation UI is shown before password
170 // manager UI (see https://crbug.com/498545).
171 new PageClickTracker(render_frame
, this);
174 AutofillAgent::~AutofillAgent() {}
176 bool AutofillAgent::FormDataCompare::operator()(const FormData
& lhs
,
177 const FormData
& rhs
) const {
178 if (lhs
.name
!= rhs
.name
)
179 return lhs
.name
< rhs
.name
;
180 if (lhs
.origin
!= rhs
.origin
)
181 return lhs
.origin
< rhs
.origin
;
182 if (lhs
.action
!= rhs
.action
)
183 return lhs
.action
< rhs
.action
;
184 if (lhs
.is_form_tag
!= rhs
.is_form_tag
)
185 return lhs
.is_form_tag
< rhs
.is_form_tag
;
186 return lhs
.fields
< rhs
.fields
;
189 bool AutofillAgent::OnMessageReceived(const IPC::Message
& message
) {
191 IPC_BEGIN_MESSAGE_MAP(AutofillAgent
, message
)
192 IPC_MESSAGE_HANDLER(AutofillMsg_FirstUserGestureObservedInTab
,
193 OnFirstUserGestureObservedInTab
)
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 (IsKeyboardAccessoryEnabled())
264 void AutofillAgent::FocusedNodeChanged(const WebNode
& node
) {
267 if (node
.isNull() || !node
.isElementNode())
270 WebElement web_element
= node
.toConst
<WebElement
>();
271 const WebInputElement
* element
= toWebInputElement(&web_element
);
273 if (!element
|| !element
->isEnabled() || element
->isReadOnly() ||
274 !element
->isTextField())
280 void AutofillAgent::FocusChangeComplete() {
281 WebDocument doc
= render_frame()->GetWebFrame()->document();
282 WebElement focused_element
;
284 focused_element
= doc
.focusedElement();
286 if (!focused_element
.isNull() && password_generation_agent_
&&
287 password_generation_agent_
->FocusedNodeHasChanged(focused_element
)) {
288 is_generation_popup_possibly_visible_
= true;
289 is_popup_possibly_visible_
= true;
293 void AutofillAgent::didRequestAutocomplete(
294 const WebFormElement
& form
) {
295 DCHECK_EQ(form
.document().frame(), render_frame()->GetWebFrame());
297 // Disallow the dialog over non-https or broken https, except when the
298 // ignore SSL flag is passed. See http://crbug.com/272512.
299 // TODO(palmer): this should be moved to the browser process after frames
300 // get their own processes.
301 GURL
url(form
.document().url());
302 content::SSLStatus ssl_status
=
303 render_frame()->GetRenderView()->GetSSLStatusOfFrame(
304 form
.document().frame());
305 bool is_safe
= url
.SchemeIsCryptographic() &&
306 !net::IsCertStatusError(ssl_status
.cert_status
);
307 bool allow_unsafe
= base::CommandLine::ForCurrentProcess()->HasSwitch(
308 ::switches::kReduceSecurityForTesting
);
310 std::string error_message
;
311 if (!in_flight_request_form_
.isNull()) {
312 error_message
= "already active.";
313 } else if (!is_safe
&& !allow_unsafe
) {
315 "must use a secure connection or --reduce-security-for-testing.";
316 } else if (!WebFormElementToFormData(form
,
317 WebFormControlElement(),
318 static_cast<ExtractMask
>(
320 EXTRACT_OPTION_TEXT
|
324 error_message
= "failed to parse form.";
327 if (!error_message
.empty()) {
328 WebConsoleMessage console_message
= WebConsoleMessage(
329 WebConsoleMessage::LevelLog
,
330 WebString(base::ASCIIToUTF16("requestAutocomplete: ") +
331 base::ASCIIToUTF16(error_message
)));
332 form
.document().frame()->addMessageToConsole(console_message
);
333 WebFormElement(form
).finishRequestAutocomplete(
334 WebFormElement::AutocompleteResultErrorDisabled
);
338 // Cancel any pending Autofill requests and hide any currently showing popups.
339 ++autofill_query_id_
;
342 in_flight_request_form_
= form
;
343 Send(new AutofillHostMsg_RequestAutocomplete(routing_id(), form_data
));
346 void AutofillAgent::setIgnoreTextChanges(bool ignore
) {
347 ignore_text_changes_
= ignore
;
350 void AutofillAgent::FormControlElementClicked(
351 const WebFormControlElement
& element
,
353 // TODO(estade): Remove this check when PageClickTracker is per-frame.
354 if (element
.document().frame() != render_frame()->GetWebFrame())
357 const WebInputElement
* input_element
= toWebInputElement(&element
);
358 if (!input_element
&& !IsTextAreaElement(element
))
361 ShowSuggestionsOptions options
;
362 options
.autofill_on_empty_values
= true;
363 options
.show_full_suggestion_list
= element
.isAutofilled();
365 // On Android, default to showing the dropdown on field focus.
366 // On desktop, require an extra click after field focus.
367 // See http://crbug.com/427660
368 #if defined(OS_ANDROID)
369 bool single_click_autofill
=
370 !base::CommandLine::ForCurrentProcess()->HasSwitch(
371 switches::kDisableSingleClickAutofill
);
373 bool single_click_autofill
=
374 base::CommandLine::ForCurrentProcess()->HasSwitch(
375 switches::kEnableSingleClickAutofill
);
378 if (!single_click_autofill
) {
379 // Show full suggestions when clicking on an already-focused form field. On
380 // the initial click (not focused yet), only show password suggestions.
381 options
.show_full_suggestion_list
=
382 options
.show_full_suggestion_list
|| was_focused
;
383 options
.show_password_suggestions_only
= !was_focused
;
385 ShowSuggestions(element
, options
);
388 void AutofillAgent::textFieldDidEndEditing(const WebInputElement
& element
) {
389 password_autofill_agent_
->TextFieldDidEndEditing(element
);
390 has_shown_autofill_popup_for_current_edit_
= false;
391 Send(new AutofillHostMsg_DidEndTextFieldEditing(routing_id()));
394 void AutofillAgent::textFieldDidChange(const WebFormControlElement
& element
) {
395 DCHECK(toWebInputElement(&element
) || IsTextAreaElement(element
));
396 if (ignore_text_changes_
)
399 if (!IsUserGesture())
402 // We post a task for doing the Autofill as the caret position is not set
403 // properly at this point (http://bugs.webkit.org/show_bug.cgi?id=16976) and
404 // it is needed to trigger autofill.
405 weak_ptr_factory_
.InvalidateWeakPtrs();
406 base::ThreadTaskRunnerHandle::Get()->PostTask(
407 FROM_HERE
, base::Bind(&AutofillAgent::TextFieldDidChangeImpl
,
408 weak_ptr_factory_
.GetWeakPtr(), element
));
411 void AutofillAgent::TextFieldDidChangeImpl(
412 const WebFormControlElement
& element
) {
413 // If the element isn't focused then the changes don't matter. This check is
414 // required to properly handle IME interactions.
415 if (!element
.focused())
418 const WebInputElement
* input_element
= toWebInputElement(&element
);
420 // |password_autofill_agent_| keeps track of all text changes even if
421 // it isn't displaying UI.
422 password_autofill_agent_
->UpdateStateForTextChange(*input_element
);
424 if (password_generation_agent_
&&
425 password_generation_agent_
->TextDidChangeInTextField(*input_element
)) {
426 is_popup_possibly_visible_
= true;
430 if (password_autofill_agent_
->TextDidChangeInTextField(*input_element
)) {
431 is_popup_possibly_visible_
= true;
437 ShowSuggestionsOptions options
;
438 options
.requires_caret_at_end
= true;
439 ShowSuggestions(element
, options
);
443 if (FindFormAndFieldForFormControlElement(element
, &form
, &field
)) {
444 Send(new AutofillHostMsg_TextFieldDidChange(routing_id(), form
, field
,
445 base::TimeTicks::Now()));
449 void AutofillAgent::textFieldDidReceiveKeyDown(const WebInputElement
& element
,
450 const WebKeyboardEvent
& event
) {
451 if (event
.windowsKeyCode
== ui::VKEY_DOWN
||
452 event
.windowsKeyCode
== ui::VKEY_UP
) {
453 ShowSuggestionsOptions options
;
454 options
.autofill_on_empty_values
= true;
455 options
.requires_caret_at_end
= true;
456 ShowSuggestions(element
, options
);
460 void AutofillAgent::openTextDataListChooser(const WebInputElement
& element
) {
461 ShowSuggestionsOptions options
;
462 options
.autofill_on_empty_values
= true;
463 options
.datalist_only
= true;
464 ShowSuggestions(element
, options
);
467 void AutofillAgent::dataListOptionsChanged(const WebInputElement
& element
) {
468 if (!is_popup_possibly_visible_
|| !element
.focused())
471 TextFieldDidChangeImpl(element
);
474 void AutofillAgent::firstUserGestureObserved() {
475 password_autofill_agent_
->FirstUserGestureObserved();
476 Send(new AutofillHostMsg_FirstUserGestureObserved(routing_id()));
479 void AutofillAgent::AcceptDataListSuggestion(
480 const base::string16
& suggested_value
) {
481 WebInputElement
* input_element
= toWebInputElement(&element_
);
482 DCHECK(input_element
);
483 base::string16 new_value
= suggested_value
;
484 // If this element takes multiple values then replace the last part with
486 if (input_element
->isMultiple() && input_element
->isEmailField()) {
487 std::vector
<base::string16
> parts
= base::SplitString(
488 base::StringPiece16(input_element
->editingValue()),
489 base::ASCIIToUTF16(","), base::KEEP_WHITESPACE
, base::SPLIT_WANT_ALL
);
490 if (parts
.size() == 0)
491 parts
.push_back(base::string16());
493 base::string16 last_part
= parts
.back();
494 // We want to keep just the leading whitespace.
495 for (size_t i
= 0; i
< last_part
.size(); ++i
) {
496 if (!base::IsUnicodeWhitespace(last_part
[i
])) {
497 last_part
= last_part
.substr(0, i
);
501 last_part
.append(suggested_value
);
502 parts
[parts
.size() - 1] = last_part
;
504 new_value
= base::JoinString(parts
, base::ASCIIToUTF16(","));
506 FillFieldWithValue(new_value
, input_element
);
509 void AutofillAgent::OnFieldTypePredictionsAvailable(
510 const std::vector
<FormDataPredictions
>& forms
) {
511 for (size_t i
= 0; i
< forms
.size(); ++i
) {
512 form_cache_
.ShowPredictions(forms
[i
]);
516 void AutofillAgent::OnFillForm(int query_id
, const FormData
& form
) {
517 if (query_id
!= autofill_query_id_
)
520 was_query_node_autofilled_
= element_
.isAutofilled();
521 FillForm(form
, element_
);
522 Send(new AutofillHostMsg_DidFillAutofillFormData(routing_id(),
523 base::TimeTicks::Now()));
526 void AutofillAgent::OnFirstUserGestureObservedInTab() {
527 password_autofill_agent_
->FirstUserGestureObserved();
530 void AutofillAgent::OnPing() {
531 Send(new AutofillHostMsg_PingAck(routing_id()));
534 void AutofillAgent::OnPreviewForm(int query_id
, const FormData
& form
) {
535 if (query_id
!= autofill_query_id_
)
538 was_query_node_autofilled_
= element_
.isAutofilled();
539 PreviewForm(form
, element_
);
540 Send(new AutofillHostMsg_DidPreviewAutofillFormData(routing_id()));
543 void AutofillAgent::OnClearForm() {
544 form_cache_
.ClearFormWithElement(element_
);
547 void AutofillAgent::OnClearPreviewedForm() {
548 if (!element_
.isNull()) {
549 if (password_autofill_agent_
->DidClearAutofillSelection(element_
))
552 ClearPreviewedFormWithElement(element_
, was_query_node_autofilled_
);
554 // TODO(isherman): There seem to be rare cases where this code *is*
555 // reachable: see [ http://crbug.com/96321#c6 ]. Ideally we would
556 // understand those cases and fix the code to avoid them. However, so far I
557 // have been unable to reproduce such a case locally. If you hit this
558 // NOTREACHED(), please file a bug against me.
563 void AutofillAgent::OnFillFieldWithValue(const base::string16
& value
) {
564 WebInputElement
* input_element
= toWebInputElement(&element_
);
566 FillFieldWithValue(value
, input_element
);
567 input_element
->setAutofilled(true);
571 void AutofillAgent::OnPreviewFieldWithValue(const base::string16
& value
) {
572 WebInputElement
* input_element
= toWebInputElement(&element_
);
574 PreviewFieldWithValue(value
, input_element
);
577 void AutofillAgent::OnAcceptDataListSuggestion(const base::string16
& value
) {
578 AcceptDataListSuggestion(value
);
581 void AutofillAgent::OnFillPasswordSuggestion(const base::string16
& username
,
582 const base::string16
& password
) {
583 bool handled
= password_autofill_agent_
->FillSuggestion(
590 void AutofillAgent::OnPreviewPasswordSuggestion(
591 const base::string16
& username
,
592 const base::string16
& password
) {
593 bool handled
= password_autofill_agent_
->PreviewSuggestion(
600 void AutofillAgent::OnRequestAutocompleteResult(
601 WebFormElement::AutocompleteResult result
,
602 const base::string16
& message
,
603 const FormData
& form_data
) {
604 if (in_flight_request_form_
.isNull())
607 if (result
== WebFormElement::AutocompleteResultSuccess
) {
608 FillFormIncludingNonFocusableElements(form_data
, in_flight_request_form_
);
609 if (!in_flight_request_form_
.checkValidity())
610 result
= WebFormElement::AutocompleteResultErrorInvalid
;
613 in_flight_request_form_
.finishRequestAutocomplete(result
);
615 if (!message
.empty()) {
616 const base::string16
prefix(base::ASCIIToUTF16("requestAutocomplete: "));
617 WebConsoleMessage console_message
= WebConsoleMessage(
618 WebConsoleMessage::LevelLog
, WebString(prefix
+ message
));
619 in_flight_request_form_
.document().frame()->addMessageToConsole(
623 in_flight_request_form_
.reset();
626 void AutofillAgent::ShowSuggestions(const WebFormControlElement
& element
,
627 const ShowSuggestionsOptions
& options
) {
628 if (!element
.isEnabled() || element
.isReadOnly())
630 if (!options
.datalist_only
&& !element
.suggestedValue().isEmpty())
633 const WebInputElement
* input_element
= toWebInputElement(&element
);
635 if (!input_element
->isTextField())
637 if (!options
.datalist_only
&& !input_element
->suggestedValue().isEmpty())
640 DCHECK(IsTextAreaElement(element
));
641 if (!element
.toConst
<WebTextAreaElement
>().suggestedValue().isEmpty())
645 // Don't attempt to autofill with values that are too large or if filling
646 // criteria are not met.
647 WebString value
= element
.editingValue();
648 if (!options
.datalist_only
&&
649 (value
.length() > kMaxDataLength
||
650 (!options
.autofill_on_empty_values
&& value
.isEmpty()) ||
651 (options
.requires_caret_at_end
&&
652 (element
.selectionStart() != element
.selectionEnd() ||
653 element
.selectionEnd() != static_cast<int>(value
.length()))))) {
654 // Any popup currently showing is obsolete.
660 if (IsAutofillableInputElement(input_element
) &&
661 password_autofill_agent_
->ShowSuggestions(
662 *input_element
, options
.show_full_suggestion_list
,
663 is_generation_popup_possibly_visible_
)) {
664 is_popup_possibly_visible_
= true;
668 if (is_generation_popup_possibly_visible_
)
671 if (options
.show_password_suggestions_only
)
674 // Password field elements should only have suggestions shown by the password
676 if (input_element
&& input_element
->isPasswordField())
679 QueryAutofillSuggestions(element
, options
.datalist_only
);
682 void AutofillAgent::QueryAutofillSuggestions(
683 const WebFormControlElement
& element
,
684 bool datalist_only
) {
685 if (!element
.document().frame())
688 DCHECK(toWebInputElement(&element
) || IsTextAreaElement(element
));
690 static int query_counter
= 0;
691 autofill_query_id_
= query_counter
++;
695 if (!FindFormAndFieldForFormControlElement(element
, &form
, &field
)) {
696 // If we didn't find the cached form, at least let autocomplete have a shot
697 // at providing suggestions.
698 WebFormControlElementToFormField(element
, EXTRACT_VALUE
, &field
);
701 field
.should_autocomplete
= false;
703 gfx::RectF bounding_box_scaled
= GetScaledBoundingBox(
704 render_frame()->GetRenderView()->GetWebView()->pageScaleFactor(),
707 std::vector
<base::string16
> data_list_values
;
708 std::vector
<base::string16
> data_list_labels
;
709 const WebInputElement
* input_element
= toWebInputElement(&element
);
711 // Find the datalist values and send them to the browser process.
712 GetDataListSuggestions(*input_element
,
716 TrimStringVectorForIPC(&data_list_values
);
717 TrimStringVectorForIPC(&data_list_labels
);
720 is_popup_possibly_visible_
= true;
721 Send(new AutofillHostMsg_SetDataList(routing_id(),
725 Send(new AutofillHostMsg_QueryFormFieldAutofill(
726 routing_id(), autofill_query_id_
, form
, field
, bounding_box_scaled
));
729 void AutofillAgent::FillFieldWithValue(const base::string16
& value
,
730 WebInputElement
* node
) {
731 base::AutoReset
<bool> auto_reset(&ignore_text_changes_
, true);
732 node
->setEditingValue(value
.substr(0, node
->maxLength()));
735 void AutofillAgent::PreviewFieldWithValue(const base::string16
& value
,
736 WebInputElement
* node
) {
737 was_query_node_autofilled_
= element_
.isAutofilled();
738 node
->setSuggestedValue(value
.substr(0, node
->maxLength()));
739 node
->setAutofilled(true);
740 PreviewSuggestion(node
->suggestedValue(), node
->value(), node
);
743 void AutofillAgent::ProcessForms() {
744 // Record timestamp of when the forms are first seen. This is used to
745 // measure the overhead of the Autofill feature.
746 base::TimeTicks forms_seen_timestamp
= base::TimeTicks::Now();
748 WebLocalFrame
* frame
= render_frame()->GetWebFrame();
749 std::vector
<FormData
> forms
= form_cache_
.ExtractNewForms();
751 // Always communicate to browser process for topmost frame.
752 if (!forms
.empty() || !frame
->parent()) {
753 Send(new AutofillHostMsg_FormsSeen(routing_id(), forms
,
754 forms_seen_timestamp
));
758 void AutofillAgent::HidePopup() {
759 if (!is_popup_possibly_visible_
)
761 is_popup_possibly_visible_
= false;
762 is_generation_popup_possibly_visible_
= false;
763 Send(new AutofillHostMsg_HidePopup(routing_id()));
766 bool AutofillAgent::IsUserGesture() const {
767 return WebUserGestureIndicator::isProcessingUserGesture();
770 void AutofillAgent::didAssociateFormControls(const WebVector
<WebNode
>& nodes
) {
771 for (size_t i
= 0; i
< nodes
.size(); ++i
) {
772 WebLocalFrame
* frame
= nodes
[i
].document().frame();
773 // Only monitors dynamic forms created in the top frame. Dynamic forms
774 // inserted in iframes are not captured yet. Frame is only processed
775 // if it has finished loading, otherwise you can end up with a partially
777 if (frame
&& !frame
->isLoading()) {
779 password_autofill_agent_
->OnDynamicFormsSeen();
780 if (password_generation_agent_
)
781 password_generation_agent_
->OnDynamicFormsSeen();
787 void AutofillAgent::ajaxSucceeded() {
788 password_autofill_agent_
->AJAXSucceeded();
791 // LegacyAutofillAgent ---------------------------------------------------------
793 AutofillAgent::LegacyAutofillAgent::LegacyAutofillAgent(
794 content::RenderView
* render_view
,
795 AutofillAgent
* agent
)
796 : content::RenderViewObserver(render_view
), agent_(agent
) {
799 AutofillAgent::LegacyAutofillAgent::~LegacyAutofillAgent() {
802 void AutofillAgent::LegacyAutofillAgent::OnDestruct() {
803 // No-op. Don't delete |this|.
806 void AutofillAgent::LegacyAutofillAgent::FocusChangeComplete() {
807 agent_
->FocusChangeComplete();
810 } // namespace autofill