blob: 95ff56c0a45048d80d23abd702f0abfcc47e321a [file] [log] [blame]
[email protected]f07e4442013-06-06 23:03:481// Copyright 2013 The Chromium Authors. All rights reserved.
[email protected]679f128f2010-07-22 22:57:442// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]f07e4442013-06-06 23:03:485#include "components/autofill/content/renderer/autofill_agent.h"
[email protected]679f128f2010-07-22 22:57:446
avi4310d522015-12-25 19:37:597#include <stddef.h>
8
brettw9c68c512015-11-20 23:26:389#include <tuple>
10
[email protected]a1cb57f2011-09-28 22:41:2911#include "base/bind.h"
[email protected]f920d6e2013-03-12 20:20:5012#include "base/command_line.h"
Dominic Battre4f1c5ca2020-02-10 16:57:5013#include "base/debug/alias.h"
14#include "base/debug/dump_without_crashing.h"
Mathieu Perreaulta24d0c22017-11-10 01:22:0015#include "base/feature_list.h"
brettw94a2cc22015-07-01 19:26:5416#include "base/i18n/case_conversion.h"
skyostilb0daa012015-06-02 19:03:4817#include "base/location.h"
mathp4f6b0382016-04-12 20:41:1218#include "base/metrics/field_trial.h"
Christoph Schweringb09a3282020-08-20 13:05:1919#include "base/optional.h"
skyostilb0daa012015-06-02 19:03:4820#include "base/single_thread_task_runner.h"
mgiuca30f75882017-03-28 02:07:1921#include "base/strings/string_piece.h"
[email protected]1988e1c2013-02-28 20:27:4222#include "base/strings/string_split.h"
[email protected]c72674b2013-06-11 04:16:4323#include "base/strings/string_util.h"
[email protected]d2d79d52013-06-07 22:23:4824#include "base/strings/utf_string_conversions.h"
gab7966d312016-05-11 20:35:0125#include "base/threading/thread_task_runner_handle.h"
[email protected]bbd8da92013-06-28 02:12:2026#include "base/time/time.h"
avi4310d522015-12-25 19:37:5927#include "build/build_config.h"
sandromaggid10cf2a2020-04-22 18:53:1528#include "components/autofill/content/renderer/autofill_assistant_agent.h"
[email protected]f07e4442013-06-06 23:03:4829#include "components/autofill/content/renderer/form_autofill_util.h"
Tao Bai6cd41ad2017-12-04 18:48:1730#include "components/autofill/content/renderer/form_tracker.h"
[email protected]f07e4442013-06-06 23:03:4831#include "components/autofill/content/renderer/password_autofill_agent.h"
[email protected]3cbdf9362014-01-31 23:12:2332#include "components/autofill/content/renderer/password_generation_agent.h"
jwwfb2a1432016-06-13 22:44:1533#include "components/autofill/content/renderer/renderer_save_password_progress_logger.h"
[email protected]d04f81912013-06-18 14:52:1334#include "components/autofill/core/common/autofill_constants.h"
[email protected]d86263dcd2014-01-09 10:35:2135#include "components/autofill/core/common/autofill_data_validation.h"
Mathieu Perreaulta24d0c22017-11-10 01:22:0036#include "components/autofill/core/common/autofill_features.h"
[email protected]d04f81912013-06-18 14:52:1337#include "components/autofill/core/common/autofill_switches.h"
Christoph Schweringaa41ba842019-10-14 08:00:0638#include "components/autofill/core/common/autofill_tick_clock.h"
rouslanc31f81d2015-08-24 17:33:5239#include "components/autofill/core/common/autofill_util.h"
[email protected]d04f81912013-06-18 14:52:1340#include "components/autofill/core/common/form_data.h"
41#include "components/autofill/core/common/form_data_predictions.h"
42#include "components/autofill/core/common/form_field_data.h"
jwwfb2a1432016-06-13 22:44:1543#include "components/autofill/core/common/password_form_fill_data.h"
44#include "components/autofill/core/common/save_password_progress_logger.h"
[email protected]fed9d4f2013-12-17 21:32:4745#include "content/public/common/content_switches.h"
Tao Bai88d56d42017-07-17 16:07:1646#include "content/public/common/origin_util.h"
[email protected]ca5190f2013-07-08 11:10:3147#include "content/public/common/url_constants.h"
estadeb1bc9bd2014-12-02 22:44:1148#include "content/public/renderer/render_frame.h"
[email protected]a2ef54c2011-10-10 16:20:3149#include "content/public/renderer/render_view.h"
[email protected]ca5190f2013-07-08 11:10:3150#include "net/cert/cert_status_flags.h"
rockot734fb662016-10-15 16:41:3051#include "services/service_manager/public/cpp/interface_provider.h"
Ken Rockot0aad7ec2018-07-31 15:58:4452#include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h"
Dave Tapuska129cef82019-12-19 16:36:4853#include "third_party/blink/public/common/input/web_keyboard_event.h"
Blink Reformata30d4232018-04-07 15:31:0654#include "third_party/blink/public/platform/web_url_request.h"
Akihiro Otab7c4cb12019-10-16 22:40:5955#include "third_party/blink/public/web/web_ax_enums.h"
Caitlin Fischerf23462e2019-06-21 15:30:4756#include "third_party/blink/public/web/web_ax_object.h"
Blink Reformata30d4232018-04-07 15:31:0657#include "third_party/blink/public/web/web_console_message.h"
58#include "third_party/blink/public/web/web_document.h"
59#include "third_party/blink/public/web/web_element_collection.h"
60#include "third_party/blink/public/web/web_form_control_element.h"
61#include "third_party/blink/public/web/web_form_element.h"
62#include "third_party/blink/public/web/web_local_frame.h"
63#include "third_party/blink/public/web/web_node.h"
64#include "third_party/blink/public/web/web_option_element.h"
Blink Reformata30d4232018-04-07 15:31:0665#include "third_party/blink/public/web/web_view.h"
[email protected]c051a1b2011-01-21 23:30:1766#include "ui/base/l10n/l10n_util.h"
[email protected]7e9acd082013-09-17 23:31:1667#include "ui/events/keycodes/keyboard_codes.h"
[email protected]679f128f2010-07-22 22:57:4468
[email protected]a1221aea2013-11-07 01:31:3069using blink::WebAutofillClient;
Caitlin Fischerf23462e2019-06-21 15:30:4770using blink::WebAutofillState;
71using blink::WebAXObject;
[email protected]dbdd60272014-04-14 22:48:4072using blink::WebConsoleMessage;
rouslanf7ebd8832015-01-22 01:54:1473using blink::WebDocument;
[email protected]5e7e8612014-03-20 14:43:1974using blink::WebElement;
75using blink::WebElementCollection;
[email protected]a1221aea2013-11-07 01:31:3076using blink::WebFormControlElement;
77using blink::WebFormElement;
78using blink::WebFrame;
79using blink::WebInputElement;
80using blink::WebKeyboardEvent;
[email protected]c5041c322014-04-08 05:06:4781using blink::WebLocalFrame;
[email protected]a1221aea2013-11-07 01:31:3082using blink::WebNode;
[email protected]a1221aea2013-11-07 01:31:3083using blink::WebOptionElement;
84using blink::WebString;
[email protected]5e7e8612014-03-20 14:43:1985using blink::WebVector;
[email protected]679f128f2010-07-22 22:57:4486
[email protected]d86263dcd2014-01-09 10:35:2187namespace autofill {
88
Tao Bai6e605e42020-05-20 20:05:5589using form_util::ExtractMask;
Maria Kazinova168a9ed92020-02-06 15:05:2090using form_util::FindFormAndFieldForFormControlElement;
91using form_util::UnownedCheckoutFormElementsAndFieldSetsToFormData;
Jan Wilken Dörrief3785c42019-06-21 10:47:3792using mojom::SubmissionSource;
Friedrich Horschigef921f52020-02-19 15:16:0493using ShowAll = PasswordAutofillAgent::ShowAll;
94using GenerationShowing = PasswordAutofillAgent::GenerationShowing;
Jan Wilken Dörrief3785c42019-06-21 10:47:3795
[email protected]e47aec52010-08-12 00:50:3096namespace {
97
sebsg1c436852018-04-18 16:54:3298// Time to wait, in ms, o ensure that only a single select change will be acted
99// upon, instead of multiple in close succession (debounce time).
sebsg248c6b02018-04-09 20:20:42100size_t kWaitTimeForSelectOptionsChangesMs = 50;
101
Tao Bai6e605e42020-05-20 20:05:55102// Helper function to return EXTRACT_DATALIST if kAutofillExtractAllDatalist is
103// enabled, otherwise EXTRACT_NONE is returned.
104ExtractMask GetExtractDatalistMask() {
105 return base::FeatureList::IsEnabled(features::kAutofillExtractAllDatalists)
106 ? form_util::EXTRACT_DATALIST
107 : form_util::EXTRACT_NONE;
108}
109
[email protected]e47aec52010-08-12 00:50:30110} // namespace
111
brettwb505b7a2014-11-26 22:05:32112AutofillAgent::ShowSuggestionsOptions::ShowSuggestionsOptions()
113 : autofill_on_empty_values(false),
114 requires_caret_at_end(false),
brettwb505b7a2014-11-26 22:05:32115 show_full_suggestion_list(false),
Fabio Tirelo83662a22018-07-06 15:03:48116 autoselect_first_suggestion(false) {}
brettwb505b7a2014-11-26 22:05:32117
estadeb1bc9bd2014-12-02 22:44:11118AutofillAgent::AutofillAgent(content::RenderFrame* render_frame,
[email protected]3cbdf9362014-01-31 23:12:23119 PasswordAutofillAgent* password_autofill_agent,
Ben Goodger205467b2017-08-02 19:33:55120 PasswordGenerationAgent* password_generation_agent,
sandromaggid10cf2a2020-04-22 18:53:15121 AutofillAssistantAgent* autofill_assistant_agent,
Ken Rockot0aad7ec2018-07-31 15:58:44122 blink::AssociatedInterfaceRegistry* registry)
estadeb1bc9bd2014-12-02 22:44:11123 : content::RenderFrameObserver(render_frame),
Mathieu Perreaultd7b56b492017-10-18 16:52:17124 form_cache_(render_frame->GetWebFrame()),
[email protected]ad19b302013-04-03 07:42:19125 password_autofill_agent_(password_autofill_agent),
[email protected]3cbdf9362014-01-31 23:12:23126 password_generation_agent_(password_generation_agent),
sandromaggid10cf2a2020-04-22 18:53:15127 autofill_assistant_agent_(autofill_assistant_agent),
[email protected]679f128f2010-07-22 22:57:44128 autofill_query_id_(0),
Parastoo Geranmayeh2dcd14f92018-06-01 17:08:39129 query_node_autofill_state_(WebAutofillState::kNotFilled),
[email protected]b648f242014-02-25 13:49:06130 is_popup_possibly_visible_(false),
gcastocdd33e0d2015-06-17 22:10:05131 is_generation_popup_possibly_visible_(false),
Tao Baie13b5ca2017-06-23 16:15:51132 is_user_gesture_required_(true),
Tao Bai88d56d42017-07-17 16:07:16133 is_secure_context_required_(false),
Maria Kazinova286b24e2020-02-05 13:20:12134 form_tracker_(render_frame),
135 field_data_manager_(password_autofill_agent->GetFieldDataManager()) {
Blink Reformat1c4d759e2017-04-09 16:34:54136 render_frame->GetWebFrame()->SetAutofillClient(this);
leon.han90fd63b2016-07-26 01:23:55137 password_autofill_agent->SetAutofillAgent(this);
Tao Bai6cd41ad2017-12-04 18:48:17138 AddFormObserver(this);
Ken Rockota9bb5912019-12-18 03:13:03139 registry->AddInterface(base::BindRepeating(
140 &AutofillAgent::BindPendingReceiver, base::Unretained(this)));
[email protected]679f128f2010-07-22 22:57:44141}
142
Christoph Schweringb8fa3822020-05-15 16:04:44143// The destructor is not guaranteed to be called. Destruction happens (only)
144// through the OnDestruct() event, which posts a task to delete this object.
145// The process may be killed before this deletion can happen.
Tao Bai6cd41ad2017-12-04 18:48:17146AutofillAgent::~AutofillAgent() {
147 RemoveFormObserver(this);
148}
[email protected]d2f05d02011-01-27 18:51:01149
Shimi Zhang6b72d292019-07-12 20:52:42150void AutofillAgent::BindPendingReceiver(
151 mojo::PendingAssociatedReceiver<mojom::AutofillAgent> pending_receiver) {
152 receiver_.Bind(std::move(pending_receiver));
leon.hande852902016-05-04 09:16:38153}
154
Dmitry Gozman02dd8272020-06-09 20:01:23155void AutofillAgent::DidCommitProvisionalLoad(ui::PageTransition transition) {
mathpffc78e22015-09-01 14:10:50156 blink::WebFrame* frame = render_frame()->GetWebFrame();
157 // TODO(dvadym): check if we need to check if it is main frame navigation
158 // http://crbug.com/443155
Blink Reformat1c4d759e2017-04-09 16:34:54159 if (frame->Parent())
mathpffc78e22015-09-01 14:10:50160 return; // Not a top-level navigation.
161
Tao Bai6cd41ad2017-12-04 18:48:17162 // Navigation to a new page or a page refresh.
Nektarios Paisios6b79cf6f2018-02-26 16:49:36163
Hajime Hoshi83c3f502018-03-14 05:49:01164 element_.Reset();
Nektarios Paisios6b79cf6f2018-02-26 16:49:36165
Tao Bai6cd41ad2017-12-04 18:48:17166 form_cache_.Reset();
167 ResetLastInteractedElements();
Tao Baia674b1d2018-01-18 22:33:43168 OnFormNoLongerSubmittable();
Christoph Schweringb09a3282020-08-20 13:05:19169 SendPotentiallySubmittedFormToBrowser();
[email protected]676126f72011-01-15 00:03:51170}
171
estadeb1bc9bd2014-12-02 22:44:11172void AutofillAgent::DidFinishDocumentLoad() {
173 ProcessForms();
[email protected]3609c962014-07-11 03:18:25174}
175
estade2792527072014-12-17 00:41:47176void AutofillAgent::DidChangeScrollOffset() {
Changwan Ryuf85180e52018-08-10 04:31:46177 if (element_.IsNull())
178 return;
179
Changwan Ryu9db32db42018-01-24 14:54:44180 if (!focus_requires_scroll_) {
181 // Post a task here since scroll offset may change during layout.
182 // (https://crbug.com/804886)
183 weak_ptr_factory_.InvalidateWeakPtrs();
Hajime Hoshi9f80e742018-04-13 03:35:04184 render_frame()
185 ->GetTaskRunner(blink::TaskType::kInternalUserInteraction)
186 ->PostTask(FROM_HERE,
187 base::BindOnce(&AutofillAgent::DidChangeScrollOffsetImpl,
Changwan Ryu9db32db42018-01-24 14:54:44188 weak_ptr_factory_.GetWeakPtr(), element_));
Friedrich Horschig35355312019-09-09 17:26:34189 } else {
Changwan Ryu9db32db42018-01-24 14:54:44190 HidePopup();
Changwan Ryueb8a057e2017-12-08 19:22:33191 }
Changwan Ryu9db32db42018-01-24 14:54:44192}
Changwan Ryueb8a057e2017-12-08 19:22:33193
Changwan Ryu9db32db42018-01-24 14:54:44194void AutofillAgent::DidChangeScrollOffsetImpl(
195 const WebFormControlElement& element) {
Changwan Ryuf85180e52018-08-10 04:31:46196 if (element != element_ || element_.IsNull() || focus_requires_scroll_ ||
Changwan Ryu9db32db42018-01-24 14:54:44197 !is_popup_possibly_visible_ || !element_.Focused())
estadeb5efbab62015-04-28 18:07:42198 return;
rouslanc31f81d2015-08-24 17:33:52199
Changwan Ryu9db32db42018-01-24 14:54:44200 FormData form;
201 FormFieldData field;
Tao Bai6e605e42020-05-20 20:05:55202 if (FindFormAndFieldForFormControlElement(
203 element_, field_data_manager_.get(),
204 static_cast<ExtractMask>(form_util::EXTRACT_BOUNDS |
205 GetExtractDatalistMask()),
206 &form, &field)) {
Tao Baid9c02cf52020-03-30 22:13:39207 GetAutofillDriver()->TextFieldDidScroll(form, field, field.bounds);
Changwan Ryu9db32db42018-01-24 14:54:44208 }
209
210 // Ignore subsequent scroll offset changes.
Friedrich Horschig35355312019-09-09 17:26:34211 HidePopup();
estadeb1bc9bd2014-12-02 22:44:11212}
213
Kent Tamura69d143e2019-05-22 00:31:45214void AutofillAgent::FocusedElementChanged(const WebElement& element) {
Ehsan Karamad3538c8a2017-11-20 16:31:40215 was_focused_before_now_ = false;
[email protected]91dcc6d32014-07-30 00:01:33216 HidePopup();
217
Kent Tamura69d143e2019-05-22 00:31:45218 if (element.IsNull()) {
Colin Blundell5ffa4c22020-10-22 09:33:45219 // Focus moved away from the last interacted form (if any) to somewhere else
220 // on the page.
221 GetAutofillDriver()->FocusNoLongerOnForm(!last_interacted_form_.IsNull());
estadeb1bc9bd2014-12-02 22:44:11222 return;
mathp8ac68ce2015-12-08 21:49:06223 }
estadeb1bc9bd2014-12-02 22:44:11224
Kent Tamura69d143e2019-05-22 00:31:45225 const WebInputElement* input = ToWebInputElement(&element);
mathpb4cf7b82015-12-11 16:42:24226
Tao Bai0c268902020-07-16 03:26:09227 bool focus_moved_to_new_form = false;
Blink Reformat1c4d759e2017-04-09 16:34:54228 if (!last_interacted_form_.IsNull() &&
Kent Tamura69d143e2019-05-22 00:31:45229 (!input || last_interacted_form_ != input->Form())) {
mathp8ac68ce2015-12-08 21:49:06230 // The focused element is not part of the last interacted form (could be
231 // in a different form).
Colin Blundell5ffa4c22020-10-22 09:33:45232 GetAutofillDriver()->FocusNoLongerOnForm(/*had_interacted_form=*/true);
Tao Bai0c268902020-07-16 03:26:09233 focus_moved_to_new_form = true;
mathp8ac68ce2015-12-08 21:49:06234 }
[email protected]f920d6e2013-03-12 20:20:50235
Tao Bai0c268902020-07-16 03:26:09236 // Calls HandleFocusChangeComplete() after notifying the focus is no longer on
237 // the previous form, then early return. No need to notify the newly focused
238 // element because that will be done by HandleFocusChangeComplete() which
239 // triggers FormControlElementClicked().
240 // Refer to http://crbug.com/1105254
241 if ((IsKeyboardAccessoryEnabled() || !focus_requires_scroll_) &&
242 !element.IsNull() &&
243 element.GetDocument().GetFrame()->HasTransientUserActivation()) {
244 focused_node_was_last_clicked_ = true;
245 HandleFocusChangeComplete();
246 }
247
248 if (focus_moved_to_new_form)
249 return;
250
Kent Tamura69d143e2019-05-22 00:31:45251 if (!input || !input->IsEnabled() || input->IsReadOnly() ||
252 !input->IsTextField())
[email protected]f920d6e2013-03-12 20:20:50253 return;
254
Kent Tamura69d143e2019-05-22 00:31:45255 element_ = *input;
Tao Bai168e4fc2017-06-19 18:46:38256
257 FormData form;
258 FormFieldData field;
Tao Bai6e605e42020-05-20 20:05:55259 if (FindFormAndFieldForFormControlElement(
260 element_, field_data_manager_.get(),
261 static_cast<ExtractMask>(form_util::EXTRACT_BOUNDS |
262 GetExtractDatalistMask()),
263 &form, &field)) {
Tao Baid9c02cf52020-03-30 22:13:39264 GetAutofillDriver()->FocusOnFormField(form, field, field.bounds);
Tao Bai168e4fc2017-06-19 18:46:38265 }
[email protected]e5057a22013-04-22 12:41:39266}
267
vabrd62bc3e2016-05-04 15:58:52268void AutofillAgent::OnDestruct() {
269 Shutdown();
270 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, this);
271}
272
Tommy Martino542b42352019-07-22 20:39:37273void AutofillAgent::AccessibilityModeChanged(const ui::AXMode& mode) {
274 is_screen_reader_enabled_ = mode.has_mode(ui::AXMode::kScreenReader);
275}
276
tmartino1c5348c92016-05-26 18:50:56277void AutofillAgent::FireHostSubmitEvents(const WebFormElement& form,
Tao Baia674b1d2018-01-18 22:33:43278 bool known_success,
279 SubmissionSource source) {
tmartino1c5348c92016-05-26 18:50:56280 FormData form_data;
Maria Kazinova286b24e2020-02-05 13:20:12281 if (!form_util::ExtractFormData(form, *field_data_manager_.get(), &form_data))
tmartino1c5348c92016-05-26 18:50:56282 return;
283
Tao Baia674b1d2018-01-18 22:33:43284 FireHostSubmitEvents(form_data, known_success, source);
tmartino1c5348c92016-05-26 18:50:56285}
286
287void AutofillAgent::FireHostSubmitEvents(const FormData& form_data,
Tao Baia674b1d2018-01-18 22:33:43288 bool known_success,
289 SubmissionSource source) {
290 // We don't want to fire duplicate submission event.
Christoph Schweringb09a3282020-08-20 13:05:19291 if (!base::FeatureList::IsEnabled(
292 features::kAutofillAllowDuplicateFormSubmissions) &&
293 !submitted_forms_.insert(form_data.unique_renderer_id).second) {
Tao Baia674b1d2018-01-18 22:33:43294 return;
Christoph Schweringb09a3282020-08-20 13:05:19295 }
Tao Baia674b1d2018-01-18 22:33:43296
Sebastien Lalancetteb2020be32019-01-08 15:17:59297 GetAutofillDriver()->FormSubmitted(form_data, known_success, source);
tmartino1c5348c92016-05-26 18:50:56298}
299
vabrd62bc3e2016-05-04 15:58:52300void AutofillAgent::Shutdown() {
Shimi Zhang6b72d292019-07-12 20:52:42301 receiver_.reset();
vabrd62bc3e2016-05-04 15:58:52302 weak_ptr_factory_.InvalidateWeakPtrs();
303}
304
Blink Reformat1c4d759e2017-04-09 16:34:54305void AutofillAgent::TextFieldDidEndEditing(const WebInputElement& element) {
Vasilii Sukhanovacf9b5d2018-11-09 16:18:47306 // Sometimes "blur" events are side effects of the password generation
307 // handling the page. They should not affect any UI in the browser.
308 if (password_generation_agent_ &&
309 password_generation_agent_->ShouldIgnoreBlur()) {
310 return;
311 }
Friedrich Horschig8b10e3c62020-03-04 09:55:01312 GetAutofillDriver()->DidEndTextFieldEditing();
Ioana Pandele9e742fe2018-10-11 19:07:54313 password_autofill_agent_->DidEndTextFieldEditing();
Ioana Pandele0c36cd0e42018-11-05 17:51:31314 if (password_generation_agent_)
315 password_generation_agent_->DidEndTextFieldEditing(element);
Christoph Schweringb09a3282020-08-20 13:05:19316
317 SendPotentiallySubmittedFormToBrowser();
[email protected]676126f72011-01-15 00:03:51318}
319
Tao Baie13b5ca2017-06-23 16:15:51320void AutofillAgent::SetUserGestureRequired(bool required) {
Tao Bai6cd41ad2017-12-04 18:48:17321 form_tracker_.set_user_gesture_required(required);
Tao Baie13b5ca2017-06-23 16:15:51322}
323
Blink Reformat1c4d759e2017-04-09 16:34:54324void AutofillAgent::TextFieldDidChange(const WebFormControlElement& element) {
Tao Bai6cd41ad2017-12-04 18:48:17325 form_tracker_.TextFieldDidChange(element);
[email protected]676126f72011-01-15 00:03:51326}
327
Tao Bai6cd41ad2017-12-04 18:48:17328void AutofillAgent::OnTextFieldDidChange(const WebInputElement& element) {
329 if (password_generation_agent_ &&
330 password_generation_agent_->TextDidChangeInTextField(element)) {
331 is_popup_possibly_visible_ = true;
[email protected]f9af2832012-12-14 04:20:43332 return;
Tao Bai6cd41ad2017-12-04 18:48:17333 }
[email protected]f9af2832012-12-14 04:20:43334
Tao Bai6cd41ad2017-12-04 18:48:17335 if (password_autofill_agent_->TextDidChangeInTextField(element)) {
336 is_popup_possibly_visible_ = true;
337 element_ = element;
338 return;
[email protected]e7e83472012-04-05 02:56:26339 }
[email protected]676126f72011-01-15 00:03:51340
brettwb505b7a2014-11-26 22:05:32341 ShowSuggestionsOptions options;
342 options.requires_caret_at_end = true;
343 ShowSuggestions(element, options);
[email protected]7d24db72011-08-26 06:02:31344
[email protected]1ecbe862012-10-05 01:29:14345 FormData form;
346 FormFieldData field;
Tao Bai6e605e42020-05-20 20:05:55347 if (FindFormAndFieldForFormControlElement(
348 element, field_data_manager_.get(),
349 static_cast<ExtractMask>(form_util::EXTRACT_BOUNDS |
350 GetExtractDatalistMask()),
351 &form, &field)) {
Tao Baid9c02cf52020-03-30 22:13:39352 GetAutofillDriver()->TextFieldDidChange(form, field, field.bounds,
353 AutofillTickClock::NowTicks());
[email protected]1d14f582011-09-02 20:42:04354 }
[email protected]676126f72011-01-15 00:03:51355}
356
Blink Reformat1c4d759e2017-04-09 16:34:54357void AutofillAgent::TextFieldDidReceiveKeyDown(const WebInputElement& element,
[email protected]2fa18c22011-06-14 23:40:43358 const WebKeyboardEvent& event) {
Blink Reformat1c4d759e2017-04-09 16:34:54359 if (event.windows_key_code == ui::VKEY_DOWN ||
360 event.windows_key_code == ui::VKEY_UP) {
brettwb505b7a2014-11-26 22:05:32361 ShowSuggestionsOptions options;
362 options.autofill_on_empty_values = true;
363 options.requires_caret_at_end = true;
Fabio Tirelo83662a22018-07-06 15:03:48364 options.autoselect_first_suggestion =
365 ShouldAutoselectFirstSuggestionOnArrowDown();
brettwb505b7a2014-11-26 22:05:32366 ShowSuggestions(element, options);
367 }
[email protected]efeb565f2013-12-12 17:16:41368}
369
Blink Reformat1c4d759e2017-04-09 16:34:54370void AutofillAgent::OpenTextDataListChooser(const WebInputElement& element) {
brettwb505b7a2014-11-26 22:05:32371 ShowSuggestionsOptions options;
372 options.autofill_on_empty_values = true;
brettwb505b7a2014-11-26 22:05:32373 ShowSuggestions(element, options);
[email protected]676126f72011-01-15 00:03:51374}
375
Blink Reformat1c4d759e2017-04-09 16:34:54376void AutofillAgent::DataListOptionsChanged(const WebInputElement& element) {
377 if (!is_popup_possibly_visible_ || !element.Focused())
estadedf5088a2014-12-15 02:04:07378 return;
379
Tao Bai6cd41ad2017-12-04 18:48:17380 OnProvisionallySaveForm(WebFormElement(), element,
381 ElementChangeSource::TEXTFIELD_CHANGED);
estadedf5088a2014-12-15 02:04:07382}
383
vasiliifd0e8ca2017-04-12 11:11:22384void AutofillAgent::UserGestureObserved() {
385 password_autofill_agent_->UserGestureObserved();
[email protected]fc22ae52014-04-23 13:48:04386}
387
leon.han90fd63b2016-07-26 01:23:55388void AutofillAgent::DoAcceptDataListSuggestion(
[email protected]d5ca8fb2013-04-11 17:54:31389 const base::string16& suggested_value) {
Hajime Hoshie6104dc92018-03-20 05:58:02390 if (element_.IsNull())
391 return;
392
Blink Reformat1c4d759e2017-04-09 16:34:54393 WebInputElement* input_element = ToWebInputElement(&element_);
Dominic Battre4f1c5ca2020-02-10 16:57:50394 if (!input_element) {
395 // For reasons not understood yet, this is triggered on elements which are
396 // not input elements.
397
398 // TODO(crbug.com/1048270) Gather debug data.
399 DEBUG_ALIAS_FOR_CSTR(element_name, element_.TagName().Latin1().c_str(), 64);
400 base::debug::DumpWithoutCrashing();
401
402 // Keep this return after removing the TODO(crbug.com/1048270) above.
403 return;
404 }
[email protected]d5ca8fb2013-04-11 17:54:31405 base::string16 new_value = suggested_value;
[email protected]71a90b32012-05-18 09:16:53406 // If this element takes multiple values then replace the last part with
407 // the suggestion.
Blink Reformat1c4d759e2017-04-09 16:34:54408 if (input_element->IsMultiple() && input_element->IsEmailField()) {
Matt Giuca93bc1582017-08-01 05:09:57409 base::string16 value = input_element->EditingValue().Utf16();
410 std::vector<base::StringPiece16> parts =
411 base::SplitStringPiece(value, base::ASCIIToUTF16(","),
412 base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
[email protected]71a90b32012-05-18 09:16:53413 if (parts.size() == 0)
mgiuca30f75882017-03-28 02:07:19414 parts.push_back(base::StringPiece16());
[email protected]71a90b32012-05-18 09:16:53415
Jan Wilken Dörrie4aa966b2020-08-24 18:20:29416 base::string16 last_part(parts.back());
[email protected]71a90b32012-05-18 09:16:53417 // We want to keep just the leading whitespace.
418 for (size_t i = 0; i < last_part.size(); ++i) {
brettwb3413062015-06-24 00:39:02419 if (!base::IsUnicodeWhitespace(last_part[i])) {
[email protected]71a90b32012-05-18 09:16:53420 last_part = last_part.substr(0, i);
421 break;
422 }
423 }
424 last_part.append(suggested_value);
thestigee1440b2016-07-08 01:06:55425 parts.back() = last_part;
[email protected]71a90b32012-05-18 09:16:53426
brettwd94a22142015-07-15 05:19:26427 new_value = base::JoinString(parts, base::ASCIIToUTF16(","));
[email protected]71a90b32012-05-18 09:16:53428 }
leon.han90fd63b2016-07-26 01:23:55429 DoFillFieldWithValue(new_value, input_element);
[email protected]63560b32014-03-04 07:06:26430}
431
Parastoo Geranmayeh522c2222018-07-16 15:14:03432void AutofillAgent::TriggerRefillIfNeeded(const FormData& form) {
Parastoo Geranmayeh1421e632018-07-25 15:57:03433 ReplaceElementIfNowInvalid(form);
434
Parastoo Geranmayeh522c2222018-07-16 15:14:03435 FormFieldData field;
436 FormData updated_form;
Maria Kazinova168a9ed92020-02-06 15:05:20437 if (FindFormAndFieldForFormControlElement(element_, field_data_manager_.get(),
438 &updated_form, &field) &&
Parastoo Geranmayeh1421e632018-07-25 15:57:03439 (!element_.IsAutofilled() || !form.DynamicallySameFormAs(updated_form))) {
Christoph Schweringaa41ba842019-10-14 08:00:06440 base::TimeTicks forms_seen_timestamp = AutofillTickClock::NowTicks();
Parastoo Geranmayeh522c2222018-07-16 15:14:03441 WebLocalFrame* frame = render_frame()->GetWebFrame();
442 std::vector<FormData> forms;
443 forms.push_back(updated_form);
444 // Always communicate to browser process for topmost frame.
Parastoo Geranmayeh1421e632018-07-25 15:57:03445 if (!forms.empty() || !frame->Parent()) {
Parastoo Geranmayeh522c2222018-07-16 15:14:03446 GetAutofillDriver()->FormsSeen(forms, forms_seen_timestamp);
Parastoo Geranmayeh1421e632018-07-25 15:57:03447 }
Parastoo Geranmayeh522c2222018-07-16 15:14:03448 }
449}
450
leon.hande852902016-05-04 09:16:38451// mojom::AutofillAgent:
leon.han90fd63b2016-07-26 01:23:55452void AutofillAgent::FillForm(int32_t id, const FormData& form) {
Hajime Hoshie6104dc92018-03-20 05:58:02453 if (element_.IsNull())
454 return;
455
mathpf43ced42016-08-04 18:28:23456 if (id != autofill_query_id_ && id != kNoQueryId)
leon.han90fd63b2016-07-26 01:23:55457 return;
458
sebsg248c6b02018-04-09 20:20:42459 was_last_action_fill_ = true;
460
sebsge39d1172018-11-07 23:56:46461 // If this is a re-fill, replace the triggering element if it's invalid.
Fabio Tireloa0442b9b92019-03-19 23:59:18462 if (id == kNoQueryId)
sebsge35b762c2018-03-29 20:33:27463 ReplaceElementIfNowInvalid(form);
464
Parastoo Geranmayeh2dcd14f92018-06-01 17:08:39465 query_node_autofill_state_ = element_.GetAutofillState();
leon.han90fd63b2016-07-26 01:23:55466 form_util::FillForm(form, element_);
Blink Reformat1c4d759e2017-04-09 16:34:54467 if (!element_.Form().IsNull())
Tao Baie4d26212017-12-19 01:30:11468 UpdateLastInteractedForm(element_.Form());
leon.han90fd63b2016-07-26 01:23:55469
Christoph Schweringaa41ba842019-10-14 08:00:06470 GetAutofillDriver()->DidFillAutofillFormData(form,
471 AutofillTickClock::NowTicks());
Parastoo Geranmayeh522c2222018-07-16 15:14:03472
473 TriggerRefillIfNeeded(form);
Christoph Schweringb09a3282020-08-20 13:05:19474 SendPotentiallySubmittedFormToBrowser();
[email protected]8527a78f2014-06-12 12:49:15475}
476
leon.han90fd63b2016-07-26 01:23:55477void AutofillAgent::PreviewForm(int32_t id, const FormData& form) {
Hajime Hoshie6104dc92018-03-20 05:58:02478 if (element_.IsNull())
479 return;
480
leon.han90fd63b2016-07-26 01:23:55481 if (id != autofill_query_id_)
[email protected]63560b32014-03-04 07:06:26482 return;
483
Parastoo Geranmayeh2dcd14f92018-06-01 17:08:39484 query_node_autofill_state_ = element_.GetAutofillState();
mathp9172c62a2015-10-08 18:51:14485 form_util::PreviewForm(form, element_);
leon.han90fd63b2016-07-26 01:23:55486
487 GetAutofillDriver()->DidPreviewAutofillFormData();
[email protected]6f001a6cf2012-02-09 15:21:53488}
489
leon.han90fd63b2016-07-26 01:23:55490void AutofillAgent::FieldTypePredictionsAvailable(
leon.han35ae41c2016-07-29 03:30:58491 const std::vector<FormDataPredictions>& forms) {
Mathieu Perreaultd7b56b492017-10-18 16:52:17492 bool attach_predictions_to_dom =
Mathieu Perreaulta24d0c22017-11-10 01:22:00493 base::FeatureList::IsEnabled(features::kAutofillShowTypePredictions);
leon.han90fd63b2016-07-26 01:23:55494 for (const auto& form : forms) {
Mathieu Perreaultd7b56b492017-10-18 16:52:17495 form_cache_.ShowPredictions(form, attach_predictions_to_dom);
leon.han90fd63b2016-07-26 01:23:55496 }
497}
498
Parastoo Geranmayeh7d8ea062018-05-15 22:16:25499void AutofillAgent::ClearSection() {
Hajime Hoshie6104dc92018-03-20 05:58:02500 if (element_.IsNull())
501 return;
502
Parastoo Geranmayeh7d8ea062018-05-15 22:16:25503 form_cache_.ClearSectionWithElement(element_);
[email protected]6f001a6cf2012-02-09 15:21:53504}
505
leon.han90fd63b2016-07-26 01:23:55506void AutofillAgent::ClearPreviewedForm() {
Hajime Hoshi83c3f502018-03-14 05:49:01507 // TODO(crbug.com/816533): It is very rare, but it looks like the |element_|
508 // can be null if a provisional load was committed immediately prior to
509 // clearing the previewed form.
510 if (element_.IsNull())
511 return;
[email protected]b5b1d69f2014-01-23 23:49:32512
Hajime Hoshi83c3f502018-03-14 05:49:01513 if (password_autofill_agent_->DidClearAutofillSelection(element_))
514 return;
515
516 form_util::ClearPreviewedFormWithElement(element_,
Parastoo Geranmayeh2dcd14f92018-06-01 17:08:39517 query_node_autofill_state_);
[email protected]6f001a6cf2012-02-09 15:21:53518}
519
leon.han35ae41c2016-07-29 03:30:58520void AutofillAgent::FillFieldWithValue(const base::string16& value) {
Hajime Hoshie6104dc92018-03-20 05:58:02521 if (element_.IsNull())
522 return;
523
Blink Reformat1c4d759e2017-04-09 16:34:54524 WebInputElement* input_element = ToWebInputElement(&element_);
ziran.sunee0fd4432014-08-27 10:10:49525 if (input_element) {
leon.han35ae41c2016-07-29 03:30:58526 DoFillFieldWithValue(value, input_element);
Parastoo Geranmayeh2dcd14f92018-06-01 17:08:39527 input_element->SetAutofillState(WebAutofillState::kAutofilled);
ziran.sunee0fd4432014-08-27 10:10:49528 }
[email protected]ac9b92f2014-03-15 00:48:32529}
530
leon.han35ae41c2016-07-29 03:30:58531void AutofillAgent::PreviewFieldWithValue(const base::string16& value) {
Hajime Hoshie6104dc92018-03-20 05:58:02532 if (element_.IsNull())
533 return;
534
Blink Reformat1c4d759e2017-04-09 16:34:54535 WebInputElement* input_element = ToWebInputElement(&element_);
[email protected]e9d29d392014-03-25 01:15:15536 if (input_element)
leon.han35ae41c2016-07-29 03:30:58537 DoPreviewFieldWithValue(value, input_element);
[email protected]6f001a6cf2012-02-09 15:21:53538}
539
Akihiro Otab7c4cb12019-10-16 22:40:59540void AutofillAgent::SetSuggestionAvailability(
541 const mojom::AutofillState state) {
Caitlin Fischerf23462e2019-06-21 15:30:47542 if (element_.IsNull())
543 return;
544
545 WebInputElement* input_element = ToWebInputElement(&element_);
Akihiro Otab7c4cb12019-10-16 22:40:59546 if (input_element) {
547 switch (state) {
548 case autofill::mojom::AutofillState::kAutofillAvailable:
549 WebAXObject::FromWebNode(*input_element)
550 .HandleAutofillStateChanged(
551 blink::WebAXAutofillState::kAutofillAvailable);
552 return;
553 case autofill::mojom::AutofillState::kAutocompleteAvailable:
554 WebAXObject::FromWebNode(*input_element)
555 .HandleAutofillStateChanged(
556 blink::WebAXAutofillState::kAutocompleteAvailable);
557 return;
558 case autofill::mojom::AutofillState::kNoSuggestions:
559 WebAXObject::FromWebNode(*input_element)
560 .HandleAutofillStateChanged(
561 blink::WebAXAutofillState::kNoSuggestions);
562 return;
563 }
564 NOTREACHED();
565 }
Caitlin Fischerf23462e2019-06-21 15:30:47566}
567
leon.han35ae41c2016-07-29 03:30:58568void AutofillAgent::AcceptDataListSuggestion(const base::string16& value) {
569 DoAcceptDataListSuggestion(value);
[email protected]5c8de6b92012-06-08 21:24:08570}
571
leon.han35ae41c2016-07-29 03:30:58572void AutofillAgent::FillPasswordSuggestion(const base::string16& username,
573 const base::string16& password) {
Hajime Hoshie6104dc92018-03-20 05:58:02574 if (element_.IsNull())
575 return;
576
leon.han35ae41c2016-07-29 03:30:58577 bool handled =
578 password_autofill_agent_->FillSuggestion(element_, username, password);
[email protected]126b1ad2014-05-21 22:37:33579 DCHECK(handled);
580}
581
leon.han35ae41c2016-07-29 03:30:58582void AutofillAgent::PreviewPasswordSuggestion(const base::string16& username,
583 const base::string16& password) {
Hajime Hoshie6104dc92018-03-20 05:58:02584 if (element_.IsNull())
585 return;
586
kinuko0073e7c82017-01-24 00:42:40587 bool handled = password_autofill_agent_->PreviewSuggestion(
Blink Reformat1c4d759e2017-04-09 16:34:54588 element_, blink::WebString::FromUTF16(username),
589 blink::WebString::FromUTF16(password));
[email protected]e7e83472012-04-05 02:56:26590 DCHECK(handled);
591}
592
Christoph Schweringb09a3282020-08-20 13:05:19593bool AutofillAgent::CollectFormlessElements(FormData* output) const {
Tao Bai86ac37152018-03-02 14:35:33594 if (render_frame() == nullptr || render_frame()->GetWebFrame() == nullptr)
595 return false;
596
Blink Reformat1c4d759e2017-04-09 16:34:54597 WebDocument document = render_frame()->GetWebFrame()->GetDocument();
tmartino283dda12016-06-23 21:43:54598
599 // Build up the FormData from the unowned elements. This logic mostly
600 // mirrors the construction of the synthetic form in form_cache.cc, but
601 // happens at submit-time so we can capture the modifications the user
602 // has made, and doesn't depend on form_cache's internal state.
603 std::vector<WebElement> fieldsets;
604 std::vector<WebFormControlElement> control_elements =
Blink Reformat1c4d759e2017-04-09 16:34:54605 form_util::GetUnownedAutofillableFormFieldElements(document.All(),
tmartino283dda12016-06-23 21:43:54606 &fieldsets);
607
Christoph Schwering33bd21012020-04-26 15:57:24608 if (control_elements.size() > kMaxParseableFields)
tmartino283dda12016-06-23 21:43:54609 return false;
610
Tao Bai6e605e42020-05-20 20:05:55611 const ExtractMask extract_mask = static_cast<ExtractMask>(
612 form_util::EXTRACT_VALUE | form_util::EXTRACT_OPTIONS);
tmartino283dda12016-06-23 21:43:54613
Maria Kazinova168a9ed92020-02-06 15:05:20614 return UnownedCheckoutFormElementsAndFieldSetsToFormData(
615 fieldsets, control_elements, nullptr, document, field_data_manager_.get(),
616 extract_mask, output, nullptr);
mathpffc78e22015-09-01 14:10:50617}
618
[email protected]e9d29d392014-03-25 01:15:15619void AutofillAgent::ShowSuggestions(const WebFormControlElement& element,
brettwb505b7a2014-11-26 22:05:32620 const ShowSuggestionsOptions& options) {
Blink Reformat1c4d759e2017-04-09 16:34:54621 if (!element.IsEnabled() || element.IsReadOnly())
[email protected]efeb565f2013-12-12 17:16:41622 return;
Blink Reformat1c4d759e2017-04-09 16:34:54623 if (!element.SuggestedValue().IsEmpty())
[email protected]580c38052014-07-16 07:28:09624 return;
[email protected]e9d29d392014-03-25 01:15:15625
Blink Reformat1c4d759e2017-04-09 16:34:54626 const WebInputElement* input_element = ToWebInputElement(&element);
[email protected]e9d29d392014-03-25 01:15:15627 if (input_element) {
Blink Reformat1c4d759e2017-04-09 16:34:54628 if (!input_element->IsTextField())
[email protected]e9d29d392014-03-25 01:15:15629 return;
Blink Reformat1c4d759e2017-04-09 16:34:54630 if (!input_element->SuggestedValue().IsEmpty())
[email protected]e9d29d392014-03-25 01:15:15631 return;
632 } else {
mathp9172c62a2015-10-08 18:51:14633 DCHECK(form_util::IsTextAreaElement(element));
Blink Reformat1c4d759e2017-04-09 16:34:54634 if (!element.ToConst<WebFormControlElement>().SuggestedValue().IsEmpty())
[email protected]e9d29d392014-03-25 01:15:15635 return;
636 }
[email protected]e47aec52010-08-12 00:50:30637
[email protected]1c69f8b72012-03-14 03:18:50638 // Don't attempt to autofill with values that are too large or if filling
639 // criteria are not met.
Blink Reformat1c4d759e2017-04-09 16:34:54640 WebString value = element.EditingValue();
mathp5f90bce22015-11-22 20:16:58641 if (value.length() > kMaxDataLength ||
Friedrich Horschig35355312019-09-09 17:26:34642 (!options.autofill_on_empty_values && value.IsEmpty()) ||
mathp5f90bce22015-11-22 20:16:58643 (options.requires_caret_at_end &&
Blink Reformat1c4d759e2017-04-09 16:34:54644 (element.SelectionStart() != element.SelectionEnd() ||
645 element.SelectionEnd() != static_cast<int>(value.length())))) {
[email protected]1c69f8b72012-03-14 03:18:50646 // Any popup currently showing is obsolete.
[email protected]b648f242014-02-25 13:49:06647 HidePopup();
[email protected]e47aec52010-08-12 00:50:30648 return;
[email protected]1c69f8b72012-03-14 03:18:50649 }
[email protected]e47aec52010-08-12 00:50:30650
[email protected]bef7f9272012-04-17 10:47:49651 element_ = element;
mathp9172c62a2015-10-08 18:51:14652 if (form_util::IsAutofillableInputElement(input_element) &&
gcastocdd33e0d2015-06-17 22:10:05653 password_autofill_agent_->ShowSuggestions(
Friedrich Horschigef921f52020-02-19 15:16:04654 *input_element, ShowAll(options.show_full_suggestion_list),
655 GenerationShowing(is_generation_popup_possibly_visible_))) {
[email protected]b648f242014-02-25 13:49:06656 is_popup_possibly_visible_ = true;
[email protected]c66ba982013-04-05 09:56:17657 return;
[email protected]b648f242014-02-25 13:49:06658 }
[email protected]bef7f9272012-04-17 10:47:49659
gcastocdd33e0d2015-06-17 22:10:05660 if (is_generation_popup_possibly_visible_)
661 return;
662
jww20355072014-12-12 21:00:55663 // Password field elements should only have suggestions shown by the password
664 // autofill agent.
Dominic Battre9758fad2019-01-02 14:14:33665 // The /*disable presubmit*/ comment below is used to disable a presubmit
666 // script that ensures that only IsPasswordFieldForAutofill() is used in this
667 // code (it has to appear between the function name and the parentesis to not
668 // match a regex). In this specific case we are actually interested in whether
669 // the field is currently a password field, not whether it has ever been a
670 // password field.
671 if (input_element &&
672 input_element->IsPasswordField /*disable presubmit*/ () &&
Dominic Battre1bb521c2017-12-19 21:10:03673 !query_password_suggestion_) {
jww20355072014-12-12 21:00:55674 return;
Dominic Battre1bb521c2017-12-19 21:10:03675 }
jww20355072014-12-12 21:00:55676
Fabio Tirelo83662a22018-07-06 15:03:48677 QueryAutofillSuggestions(element, options.autoselect_first_suggestion);
[email protected]77bb0da2010-11-20 01:55:30678}
679
Tao Baia8c0d8bb2017-12-18 19:47:36680void AutofillAgent::SetQueryPasswordSuggestion(bool query) {
681 query_password_suggestion_ = query;
682}
683
Tao Bai88d56d42017-07-17 16:07:16684void AutofillAgent::SetSecureContextRequired(bool required) {
685 is_secure_context_required_ = required;
686}
687
Changwan Ryueb8a057e2017-12-08 19:22:33688void AutofillAgent::SetFocusRequiresScroll(bool require) {
689 focus_requires_scroll_ = require;
690}
691
gogeraldd4f184b2018-09-19 22:24:24692void AutofillAgent::GetElementFormAndFieldData(
693 const std::vector<std::string>& selectors,
694 GetElementFormAndFieldDataCallback callback) {
695 FormData form;
696 FormFieldData field;
697 blink::WebElement target_element = FindUniqueWebElement(selectors);
698 if (target_element.IsNull() || !target_element.IsFormControlElement()) {
699 return std::move(callback).Run(form, field);
700 }
701
702 blink::WebFormControlElement target_form_control_element =
703 target_element.To<blink::WebFormControlElement>();
Maria Kazinova168a9ed92020-02-06 15:05:20704 bool success = FindFormAndFieldForFormControlElement(
705 target_form_control_element, field_data_manager_.get(), &form, &field);
gogerald3e2f8a12018-10-11 21:46:54706 if (success) {
707 // Remember this element so as to autofill the form without focusing the
708 // field for Autofill Assistant.
709 element_ = target_form_control_element;
710 }
711 // Do not expect failure.
712 DCHECK(success);
713
gogeraldd4f184b2018-09-19 22:24:24714 return std::move(callback).Run(form, field);
715}
716
717blink::WebElement AutofillAgent::FindUniqueWebElement(
718 const std::vector<std::string>& selectors) {
719 DCHECK(selectors.size() > 0);
720
721 blink::WebVector<blink::WebElement> elements =
722 render_frame()->GetWebFrame()->GetDocument().QuerySelectorAll(
723 blink::WebString::FromUTF8(selectors[0]));
724 if (elements.size() != 1) {
725 return blink::WebElement();
726 }
727
728 // Get the unique element in |elements| and match the next selector inside it
729 // if there are remaining selectors haven't been matched.
730 blink::WebElement query_element = elements[0];
731 for (size_t i = 1; i < selectors.size(); i++) {
732 elements = query_element.QuerySelectorAll(
733 blink::WebString::FromUTF8(selectors[i]));
734
735 // Query shadow DOM if necessary.
736 if (elements.size() == 0 && !query_element.ShadowRoot().IsNull()) {
gogeraldd52275112018-09-21 02:17:22737 // TODO(806868): Query shadow dom when Autofill is available for forms in
738 // shadow DOM (crbug.com/746593).
739 return blink::WebElement();
gogeraldd4f184b2018-09-19 22:24:24740 }
741
742 // Return an empty element if there are multiple matching elements.
743 if (elements.size() != 1) {
744 return blink::WebElement();
745 }
746
747 query_element = elements[0];
748 }
749
750 return query_element;
751}
752
sandromaggid10cf2a2020-04-22 18:53:15753void AutofillAgent::SetAssistantActionState(bool running) {
754 DCHECK(autofill_assistant_agent_);
755 if (running) {
756 autofill_assistant_agent_->DisableKeyboard();
757 } else {
758 autofill_assistant_agent_->EnableKeyboard();
759 }
760}
761
[email protected]e9d29d392014-03-25 01:15:15762void AutofillAgent::QueryAutofillSuggestions(
Fabio Tirelo83662a22018-07-06 15:03:48763 const WebFormControlElement& element,
764 bool autoselect_first_suggestion) {
Blink Reformat1c4d759e2017-04-09 16:34:54765 if (!element.GetDocument().GetFrame())
[email protected]e69b5f72013-01-24 00:59:13766 return;
767
Blink Reformat1c4d759e2017-04-09 16:34:54768 DCHECK(ToWebInputElement(&element) || form_util::IsTextAreaElement(element));
[email protected]e9d29d392014-03-25 01:15:15769
[email protected]77bb0da2010-11-20 01:55:30770 static int query_counter = 0;
771 autofill_query_id_ = query_counter++;
estade0ee91262014-10-23 19:29:19772
[email protected]1ecbe862012-10-05 01:29:14773 FormData form;
774 FormFieldData field;
Tao Bai6e605e42020-05-20 20:05:55775 if (!FindFormAndFieldForFormControlElement(
776 element, field_data_manager_.get(),
777 static_cast<ExtractMask>(form_util::EXTRACT_BOUNDS |
778 GetExtractDatalistMask()),
779 &form, &field)) {
[email protected]7837be62011-01-18 23:45:08780 // If we didn't find the cached form, at least let autocomplete have a shot
781 // at providing suggestions.
Tao Baid9c02cf52020-03-30 22:13:39782 WebFormControlElementToFormField(
783 element, nullptr,
Tao Bai6e605e42020-05-20 20:05:55784 static_cast<ExtractMask>(form_util::EXTRACT_VALUE |
785 form_util::EXTRACT_BOUNDS |
786 GetExtractDatalistMask()),
Tao Baid9c02cf52020-03-30 22:13:39787 &field);
[email protected]7837be62011-01-18 23:45:08788 }
[email protected]77bb0da2010-11-20 01:55:30789
Tao Bai88d56d42017-07-17 16:07:16790 if (is_secure_context_required_ &&
Tao Bai6b032dd2017-09-19 18:44:07791 !(element.GetDocument().IsSecureContext())) {
Tao Bai88d56d42017-07-17 16:07:16792 LOG(WARNING) << "Autofill suggestions are disabled because the document "
Tao Bai6b032dd2017-09-19 18:44:07793 "isn't a secure context.";
Tao Bai88d56d42017-07-17 16:07:16794 return;
795 }
796
Tao Bai6e605e42020-05-20 20:05:55797 if (!base::FeatureList::IsEnabled(features::kAutofillExtractAllDatalists)) {
798 if (const WebInputElement* input_element = ToWebInputElement(&element)) {
799 // Find the datalist values and send them to the browser process.
800 form_util::GetDataListSuggestions(*input_element, &field.datalist_values,
801 &field.datalist_labels);
802 }
[email protected]e9d29d392014-03-25 01:15:15803 }
[email protected]5c8de6b92012-06-08 21:24:08804
[email protected]b648f242014-02-25 13:49:06805 is_popup_possibly_visible_ = true;
Tao Baid9c02cf52020-03-30 22:13:39806 GetAutofillDriver()->QueryFormFieldAutofill(autofill_query_id_, form, field,
807 field.bounds,
808 autoselect_first_suggestion);
[email protected]e47aec52010-08-12 00:50:30809}
810
leon.han90fd63b2016-07-26 01:23:55811void AutofillAgent::DoFillFieldWithValue(const base::string16& value,
812 WebInputElement* node) {
Tao Bai81483262018-03-02 05:38:12813 form_tracker_.set_ignore_control_changes(true);
sebsg778c43692018-01-24 20:01:32814 node->SetAutofillValue(blink::WebString::FromUTF16(value));
Vadym Doroshenkoa332ebc2017-09-14 11:46:53815 password_autofill_agent_->UpdateStateForTextChange(*node);
Tao Bai81483262018-03-02 05:38:12816 form_tracker_.set_ignore_control_changes(false);
[email protected]ac9b92f2014-03-15 00:48:32817}
818
leon.han90fd63b2016-07-26 01:23:55819void AutofillAgent::DoPreviewFieldWithValue(const base::string16& value,
820 WebInputElement* node) {
Parastoo Geranmayeh2dcd14f92018-06-01 17:08:39821 query_node_autofill_state_ = element_.GetAutofillState();
sebsg778c43692018-01-24 20:01:32822 node->SetSuggestedValue(blink::WebString::FromUTF16(value));
Parastoo Geranmayeh2dcd14f92018-06-01 17:08:39823 node->SetAutofillState(WebAutofillState::kPreviewed);
Blink Reformat1c4d759e2017-04-09 16:34:54824 form_util::PreviewSuggestion(node->SuggestedValue().Utf16(),
825 node->Value().Utf16(), node);
[email protected]6f001a6cf2012-02-09 15:21:53826}
827
estadeb1bc9bd2014-12-02 22:44:11828void AutofillAgent::ProcessForms() {
[email protected]5193ea52014-05-14 01:10:02829 // Record timestamp of when the forms are first seen. This is used to
830 // measure the overhead of the Autofill feature.
Christoph Schweringaa41ba842019-10-14 08:00:06831 base::TimeTicks forms_seen_timestamp = AutofillTickClock::NowTicks();
[email protected]5193ea52014-05-14 01:10:02832
estadeb1bc9bd2014-12-02 22:44:11833 WebLocalFrame* frame = render_frame()->GetWebFrame();
Maria Kazinova168a9ed92020-02-06 15:05:20834 std::vector<FormData> forms =
835 form_cache_.ExtractNewForms(field_data_manager_.get());
[email protected]5193ea52014-05-14 01:10:02836
837 // Always communicate to browser process for topmost frame.
Blink Reformat1c4d759e2017-04-09 16:34:54838 if (!forms.empty() || !frame->Parent()) {
leon.han35ae41c2016-07-29 03:30:58839 GetAutofillDriver()->FormsSeen(forms, forms_seen_timestamp);
[email protected]5193ea52014-05-14 01:10:02840 }
[email protected]5193ea52014-05-14 01:10:02841}
842
[email protected]b648f242014-02-25 13:49:06843void AutofillAgent::HidePopup() {
844 if (!is_popup_possibly_visible_)
845 return;
[email protected]b648f242014-02-25 13:49:06846 is_popup_possibly_visible_ = false;
gcastocdd33e0d2015-06-17 22:10:05847 is_generation_popup_possibly_visible_ = false;
leon.han90fd63b2016-07-26 01:23:55848
Friedrich Horschig35355312019-09-09 17:26:34849 // The keyboard accessory has a separate, more complex hiding logic.
850 if (IsKeyboardAccessoryEnabled())
851 return;
852
leon.han90fd63b2016-07-26 01:23:55853 GetAutofillDriver()->HidePopup();
[email protected]81cd52332012-11-05 20:36:07854}
855
Blink Reformat1c4d759e2017-04-09 16:34:54856void AutofillAgent::DidAssociateFormControlsDynamically() {
vasilii31ba8c6c2017-04-28 21:35:08857 // If the control flow is here than the document was at least loaded. The
858 // whole page doesn't have to be loaded.
859 ProcessForms();
860 password_autofill_agent_->OnDynamicFormsSeen();
[email protected]17b6be72013-04-30 21:33:08861}
862
ekaramad27ca69b12017-04-20 18:34:29863void AutofillAgent::DidCompleteFocusChangeInFrame() {
864 WebDocument doc = render_frame()->GetWebFrame()->GetDocument();
865 WebElement focused_element;
866 if (!doc.IsNull())
867 focused_element = doc.FocusedElement();
Ioana Pandele67513a82019-06-11 10:21:00868
869 if (!focused_element.IsNull() && password_autofill_agent_)
870 password_autofill_agent_->FocusedNodeHasChanged(focused_element);
871
ekaramad27ca69b12017-04-20 18:34:29872 // PasswordGenerationAgent needs to know about focus changes, even if there is
873 // no focused element.
874 if (password_generation_agent_ &&
875 password_generation_agent_->FocusedNodeHasChanged(focused_element)) {
876 is_generation_popup_possibly_visible_ = true;
877 is_popup_possibly_visible_ = true;
878 }
ekaramad27ca69b12017-04-20 18:34:29879
Changwan Ryueb8a057e2017-12-08 19:22:33880 if (!IsKeyboardAccessoryEnabled() && focus_requires_scroll_)
Ehsan Karamad3538c8a2017-11-20 16:31:40881 HandleFocusChangeComplete();
Christoph Schweringb09a3282020-08-20 13:05:19882
883 SendPotentiallySubmittedFormToBrowser();
ekaramad27ca69b12017-04-20 18:34:29884}
885
886void AutofillAgent::DidReceiveLeftMouseDownOrGestureTapInNode(
887 const WebNode& node) {
Ehsan Karamad3538c8a2017-11-20 16:31:40888 DCHECK(!node.IsNull());
889 focused_node_was_last_clicked_ = node.Focused();
890
Jan Wilken Dörrie6f5eee5a2019-10-11 16:14:36891 if (IsTouchToFillEnabled() || IsKeyboardAccessoryEnabled() ||
892 !focus_requires_scroll_) {
Ehsan Karamad3538c8a2017-11-20 16:31:40893 HandleFocusChangeComplete();
Jan Wilken Dörrie6f5eee5a2019-10-11 16:14:36894 }
Ehsan Karamad3538c8a2017-11-20 16:31:40895}
896
Tao Bai81483262018-03-02 05:38:12897void AutofillAgent::SelectControlDidChange(
898 const WebFormControlElement& element) {
899 form_tracker_.SelectControlDidChange(element);
900}
901
sebsg248c6b02018-04-09 20:20:42902void AutofillAgent::SelectFieldOptionsChanged(
903 const blink::WebFormControlElement& element) {
904 if (!was_last_action_fill_ || element_.IsNull())
905 return;
906
sebsg1c436852018-04-18 16:54:32907 // Since a change of a select options often come in batches, use a timer
908 // to wait for other changes. Stop the timer if it was already running. It
909 // will be started again for this change.
910 if (on_select_update_timer_.IsRunning())
911 on_select_update_timer_.AbandonAndStop();
912
913 // Start the timer to notify the driver that the select field was updated
914 // after the options have finished changing,
915 on_select_update_timer_.Start(
916 FROM_HERE,
917 base::TimeDelta::FromMilliseconds(kWaitTimeForSelectOptionsChangesMs),
918 base::BindRepeating(&AutofillAgent::SelectWasUpdated,
919 weak_ptr_factory_.GetWeakPtr(), element));
920}
921
Jan Wilken Dörrief147dd772019-08-02 05:38:07922bool AutofillAgent::ShouldSuppressKeyboard(
923 const WebFormControlElement& element) {
sandromaggid10cf2a2020-04-22 18:53:15924 // Note: Consider supporting other autofill types in the future as well.
925 return password_autofill_agent_->ShouldSuppressKeyboard() ||
926 (autofill_assistant_agent_ &&
927 autofill_assistant_agent_->ShouldSuppressKeyboard());
Jan Wilken Dörriea1f2d0632019-05-08 09:29:00928}
929
Maria Kazinova2e53e2d2020-11-16 12:21:35930void AutofillAgent::FormElementReset(const WebFormElement& form) {
931 password_autofill_agent_->InformAboutFormClearing(form);
932}
933
Maria Kazinovad9f324d2020-11-20 23:29:54934void AutofillAgent::PasswordFieldReset(const WebInputElement& element) {
935 password_autofill_agent_->InformAboutFieldClearing(element);
936}
937
sebsg1c436852018-04-18 16:54:32938void AutofillAgent::SelectWasUpdated(
939 const blink::WebFormControlElement& element) {
940 // Look for the form and field associated with the select element. If they are
941 // found, notify the driver that the the form was modified dynamically.
sebsg248c6b02018-04-09 20:20:42942 FormData form;
943 FormFieldData field;
Ian Struiksma2988bc82020-02-18 09:13:13944 if (FindFormAndFieldForFormControlElement(element, field_data_manager_.get(),
Maria Kazinova168a9ed92020-02-06 15:05:20945 &form, &field) &&
sebsg248c6b02018-04-09 20:20:42946 !field.option_values.empty()) {
sebsg1c436852018-04-18 16:54:32947 GetAutofillDriver()->SelectFieldOptionsDidChange(form);
sebsg248c6b02018-04-09 20:20:42948 }
949}
950
Ehsan Karamad3538c8a2017-11-20 16:31:40951void AutofillAgent::FormControlElementClicked(
952 const WebFormControlElement& element,
953 bool was_focused) {
954 last_clicked_form_control_element_for_testing_ = element;
955 last_clicked_form_control_element_was_focused_for_testing_ = was_focused;
sebsg248c6b02018-04-09 20:20:42956 was_last_action_fill_ = false;
Ehsan Karamad3538c8a2017-11-20 16:31:40957
958 const WebInputElement* input_element = ToWebInputElement(&element);
959 if (!input_element && !form_util::IsTextAreaElement(element))
960 return;
961
Jan Wilken Dörrie6f5eee5a2019-10-11 16:14:36962 if (IsTouchToFillEnabled())
963 password_autofill_agent_->TryToShowTouchToFill(element);
964
Ehsan Karamad3538c8a2017-11-20 16:31:40965 ShowSuggestionsOptions options;
966 options.autofill_on_empty_values = true;
Roger McFarlane3a9c4402018-05-18 18:29:35967 // Show full suggestions when clicking on an already-focused form field.
968 options.show_full_suggestion_list = element.IsAutofilled() || was_focused;
Ehsan Karamad3538c8a2017-11-20 16:31:40969
Ehsan Karamad3538c8a2017-11-20 16:31:40970 ShowSuggestions(element, options);
Christoph Schweringb09a3282020-08-20 13:05:19971
972 SendPotentiallySubmittedFormToBrowser();
Ehsan Karamad3538c8a2017-11-20 16:31:40973}
974
975void AutofillAgent::HandleFocusChangeComplete() {
976 WebElement focused_element =
977 render_frame()->GetWebFrame()->GetDocument().FocusedElement();
Tommy Martino542b42352019-07-22 20:39:37978 // When using Talkback on Android, and possibly others, traversing to and
979 // focusing a field will not register as a click. Thus, when screen readers
980 // are used, treat the focused node as if it was the last clicked. Also check
981 // to ensure focus is on a field where text can be entered.
982 if ((focused_node_was_last_clicked_ || is_screen_reader_enabled_) &&
983 !focused_element.IsNull() && focused_element.IsFormControlElement() &&
Ehsan Karamad3538c8a2017-11-20 16:31:40984 (form_util::IsTextInput(blink::ToWebInputElement(&focused_element)) ||
985 focused_element.HasHTMLTagName("textarea"))) {
986 FormControlElementClicked(focused_element.ToConst<WebFormControlElement>(),
987 was_focused_before_now_);
988 }
989
990 was_focused_before_now_ = true;
991 focused_node_was_last_clicked_ = false;
Christoph Schweringb09a3282020-08-20 13:05:19992
993 SendPotentiallySubmittedFormToBrowser();
ekaramad27ca69b12017-04-20 18:34:29994}
995
Blink Reformat1c4d759e2017-04-09 16:34:54996void AutofillAgent::AjaxSucceeded() {
Tao Bai6cd41ad2017-12-04 18:48:17997 form_tracker_.AjaxSucceeded();
Christoph Schweringb09a3282020-08-20 13:05:19998
999 SendPotentiallySubmittedFormToBrowser();
Tao Bai6cd41ad2017-12-04 18:48:171000}
1001
Tao Bai81483262018-03-02 05:38:121002void AutofillAgent::OnProvisionallySaveForm(
1003 const WebFormElement& form,
1004 const WebFormControlElement& element,
1005 ElementChangeSource source) {
Tao Bai6cd41ad2017-12-04 18:48:171006 if (source == ElementChangeSource::WILL_SEND_SUBMIT_EVENT) {
Tao Baia674b1d2018-01-18 22:33:431007 // Fire the form submission event to avoid missing submission when web site
1008 // handles the onsubmit event, this also gets the form before Javascript
1009 // could change it.
1010 // We don't clear submitted_forms_ because OnFormSubmitted will normally be
1011 // invoked afterwards and we don't want to fire the same event twice.
1012 FireHostSubmitEvents(form, /*known_success=*/false,
1013 SubmissionSource::FORM_SUBMISSION);
1014 ResetLastInteractedElements();
Tao Bai81483262018-03-02 05:38:121015 } else if (source == ElementChangeSource::TEXTFIELD_CHANGED ||
1016 source == ElementChangeSource::SELECT_CHANGED) {
Tao Baia674b1d2018-01-18 22:33:431017 // Remember the last form the user interacted with.
Tao Bai6cd41ad2017-12-04 18:48:171018 if (!element.Form().IsNull()) {
Tao Baie4d26212017-12-19 01:30:111019 UpdateLastInteractedForm(element.Form());
Tao Bai6cd41ad2017-12-04 18:48:171020 } else {
1021 // Remove invisible elements
1022 for (auto it = formless_elements_user_edited_.begin();
1023 it != formless_elements_user_edited_.end();) {
1024 if (form_util::IsWebElementVisible(*it)) {
1025 it = formless_elements_user_edited_.erase(it);
1026 } else {
1027 ++it;
1028 }
1029 }
1030 formless_elements_user_edited_.insert(element);
Christoph Schweringd54e56e2020-02-04 16:08:431031 provisionally_saved_form_ = base::make_optional<FormData>();
1032 if (!CollectFormlessElements(&provisionally_saved_form_.value())) {
Tao Baie4d26212017-12-19 01:30:111033 provisionally_saved_form_.reset();
Tao Bai6cd41ad2017-12-04 18:48:171034 } else {
1035 last_interacted_form_.Reset();
1036 }
1037 }
Tao Bai81483262018-03-02 05:38:121038
1039 if (source == ElementChangeSource::TEXTFIELD_CHANGED)
1040 OnTextFieldDidChange(*ToWebInputElement(&element));
1041 else {
1042 FormData form;
1043 FormFieldData field;
Maria Kazinova168a9ed92020-02-06 15:05:201044 if (FindFormAndFieldForFormControlElement(
Tao Bai6e605e42020-05-20 20:05:551045 element, field_data_manager_.get(),
1046 static_cast<ExtractMask>(form_util::EXTRACT_BOUNDS |
1047 GetExtractDatalistMask()),
Tao Baid9c02cf52020-03-30 22:13:391048 &form, &field)) {
1049 GetAutofillDriver()->SelectControlDidChange(form, field, field.bounds);
Tao Bai81483262018-03-02 05:38:121050 }
1051 }
Tao Bai6cd41ad2017-12-04 18:48:171052 }
Christoph Schweringb09a3282020-08-20 13:05:191053 SendPotentiallySubmittedFormToBrowser();
Tao Bai6cd41ad2017-12-04 18:48:171054}
1055
1056void AutofillAgent::OnProbablyFormSubmitted() {
Christoph Schweringb09a3282020-08-20 13:05:191057 base::Optional<FormData> form_data = GetSubmittedForm();
1058 if (form_data.has_value()) {
1059 FireHostSubmitEvents(form_data.value(), /*known_success=*/false,
Tao Baia674b1d2018-01-18 22:33:431060 SubmissionSource::PROBABLY_FORM_SUBMITTED);
1061 }
Tao Bai6cd41ad2017-12-04 18:48:171062 ResetLastInteractedElements();
Tao Baia674b1d2018-01-18 22:33:431063 OnFormNoLongerSubmittable();
Christoph Schweringb09a3282020-08-20 13:05:191064 SendPotentiallySubmittedFormToBrowser();
Tao Bai6cd41ad2017-12-04 18:48:171065}
1066
1067void AutofillAgent::OnFormSubmitted(const WebFormElement& form) {
Tao Baia674b1d2018-01-18 22:33:431068 // Fire the submission event here because WILL_SEND_SUBMIT_EVENT is skipped
1069 // if javascript calls submit() directly.
1070 FireHostSubmitEvents(form, /*known_success=*/false,
1071 SubmissionSource::FORM_SUBMISSION);
Tao Bai6cd41ad2017-12-04 18:48:171072 ResetLastInteractedElements();
Tao Baia674b1d2018-01-18 22:33:431073 OnFormNoLongerSubmittable();
Christoph Schweringb09a3282020-08-20 13:05:191074 SendPotentiallySubmittedFormToBrowser();
Tao Bai6cd41ad2017-12-04 18:48:171075}
1076
1077void AutofillAgent::OnInferredFormSubmission(SubmissionSource source) {
1078 // Only handle iframe for FRAME_DETACHED or main frame for
1079 // SAME_DOCUMENT_NAVIGATION.
1080 if ((source == SubmissionSource::FRAME_DETACHED &&
1081 !render_frame()->GetWebFrame()->Parent()) ||
1082 (source == SubmissionSource::SAME_DOCUMENT_NAVIGATION &&
1083 render_frame()->GetWebFrame()->Parent())) {
Tao Baia674b1d2018-01-18 22:33:431084 ResetLastInteractedElements();
1085 OnFormNoLongerSubmittable();
Christoph Schweringb09a3282020-08-20 13:05:191086 SendPotentiallySubmittedFormToBrowser();
Tao Bai6cd41ad2017-12-04 18:48:171087 return;
1088 }
1089
Tao Bai2c1b3d062017-12-07 17:20:281090 if (source == SubmissionSource::FRAME_DETACHED) {
1091 // Should not access the frame because it is now detached. Instead, use
Tao Baie4d26212017-12-19 01:30:111092 // |provisionally_saved_form_|.
Christoph Schweringd54e56e2020-02-04 16:08:431093 if (provisionally_saved_form_.has_value())
1094 FireHostSubmitEvents(provisionally_saved_form_.value(),
1095 /*known_success=*/true, source);
Tao Bai2c1b3d062017-12-07 17:20:281096 } else {
Christoph Schweringb09a3282020-08-20 13:05:191097 base::Optional<FormData> form_data = GetSubmittedForm();
1098 if (form_data.has_value())
1099 FireHostSubmitEvents(form_data.value(), /*known_success=*/true, source);
Tao Bai6cd41ad2017-12-04 18:48:171100 }
1101 ResetLastInteractedElements();
Tao Baia674b1d2018-01-18 22:33:431102 OnFormNoLongerSubmittable();
Christoph Schweringb09a3282020-08-20 13:05:191103 SendPotentiallySubmittedFormToBrowser();
Tao Bai6cd41ad2017-12-04 18:48:171104}
1105
1106void AutofillAgent::AddFormObserver(Observer* observer) {
1107 form_tracker_.AddObserver(observer);
1108}
1109
1110void AutofillAgent::RemoveFormObserver(Observer* observer) {
1111 form_tracker_.RemoveObserver(observer);
1112}
1113
Jan Wilken Dörried4119112c2020-11-09 18:42:121114void AutofillAgent::TrackAutofilledElement(
1115 const blink::WebFormControlElement& element) {
1116 form_tracker_.TrackAutofilledElement(element);
1117}
1118
Christoph Schweringb09a3282020-08-20 13:05:191119base::Optional<FormData> AutofillAgent::GetSubmittedForm() const {
Tao Bai6cd41ad2017-12-04 18:48:171120 if (!last_interacted_form_.IsNull()) {
Christoph Schweringb09a3282020-08-20 13:05:191121 FormData form;
Maria Kazinova286b24e2020-02-05 13:20:121122 if (form_util::ExtractFormData(last_interacted_form_,
Christoph Schweringb09a3282020-08-20 13:05:191123 *field_data_manager_.get(), &form)) {
1124 return base::make_optional(form);
Christoph Schweringd54e56e2020-02-04 16:08:431125 } else if (provisionally_saved_form_.has_value()) {
Christoph Schweringb09a3282020-08-20 13:05:191126 return base::make_optional(provisionally_saved_form_.value());
Tao Baie4d26212017-12-19 01:30:111127 }
Tao Bai6cd41ad2017-12-04 18:48:171128 } else if (formless_elements_user_edited_.size() != 0 &&
1129 !form_util::IsSomeControlElementVisible(
1130 formless_elements_user_edited_)) {
1131 // we check if all the elements the user has interacted with are gone,
Tao Baie4d26212017-12-19 01:30:111132 // to decide if submission has occurred, and use the
1133 // provisionally_saved_form_ saved in OnProvisionallySaveForm() if fail to
1134 // construct form.
Christoph Schweringb09a3282020-08-20 13:05:191135 FormData form;
1136 if (CollectFormlessElements(&form)) {
1137 return base::make_optional(form);
Christoph Schweringd54e56e2020-02-04 16:08:431138 } else if (provisionally_saved_form_.has_value()) {
Christoph Schweringb09a3282020-08-20 13:05:191139 return base::make_optional(provisionally_saved_form_.value());
Tao Bai6cd41ad2017-12-04 18:48:171140 }
1141 }
Christoph Schweringb09a3282020-08-20 13:05:191142 return base::nullopt;
1143}
1144
1145void AutofillAgent::SendPotentiallySubmittedFormToBrowser() {
1146 GetAutofillDriver()->SetFormToBeProbablySubmitted(GetSubmittedForm());
Tao Bai6cd41ad2017-12-04 18:48:171147}
1148
1149void AutofillAgent::ResetLastInteractedElements() {
1150 last_interacted_form_.Reset();
Alexander Timokhin3c45d1a2018-03-28 17:49:281151 last_clicked_form_control_element_for_testing_.Reset();
Tao Bai6cd41ad2017-12-04 18:48:171152 formless_elements_user_edited_.clear();
Tao Baie4d26212017-12-19 01:30:111153 provisionally_saved_form_.reset();
1154}
1155
1156void AutofillAgent::UpdateLastInteractedForm(blink::WebFormElement form) {
1157 last_interacted_form_ = form;
Christoph Schweringd54e56e2020-02-04 16:08:431158 provisionally_saved_form_ = base::make_optional<FormData>();
Tao Baie4d26212017-12-19 01:30:111159 if (!form_util::ExtractFormData(last_interacted_form_,
Maria Kazinova286b24e2020-02-05 13:20:121160 *field_data_manager_.get(),
Christoph Schweringd54e56e2020-02-04 16:08:431161 &provisionally_saved_form_.value())) {
Tao Baie4d26212017-12-19 01:30:111162 provisionally_saved_form_.reset();
1163 }
gcastoc21f1c4c2015-03-19 21:57:031164}
1165
Tao Baia674b1d2018-01-18 22:33:431166void AutofillAgent::OnFormNoLongerSubmittable() {
1167 submitted_forms_.clear();
1168}
1169
Parastoo Geranmayeh1421e632018-07-25 15:57:031170bool AutofillAgent::FindTheUniqueNewVersionOfOldElement(
Parastoo Geranmayehc5d65c12018-11-21 05:32:021171 const WebVector<WebFormControlElement>& elements,
1172 bool& potential_match_encountered,
1173 WebFormControlElement& matching_element,
Parastoo Geranmayeh1421e632018-07-25 15:57:031174 const WebFormControlElement& original_element) {
Parastoo Geranmayehc5d65c12018-11-21 05:32:021175 if (original_element.IsNull())
1176 return false;
1177
1178 const auto original_element_section = original_element.AutofillSection();
Parastoo Geranmayeh1421e632018-07-25 15:57:031179 for (const WebFormControlElement& current_element : elements) {
1180 if (current_element.IsFocusable() &&
1181 original_element.NameForAutofill() ==
1182 current_element.NameForAutofill()) {
Parastoo Geranmayehc5d65c12018-11-21 05:32:021183 // If this is the first matching element, or is the first with the right
1184 // section, this is the best match so far.
1185 // In other words: bad, then good. => pick good.
1186 if (!potential_match_encountered ||
1187 (current_element.AutofillSection() == original_element_section &&
1188 (matching_element.IsNull() ||
1189 matching_element.AutofillSection() != original_element_section))) {
1190 matching_element = current_element;
1191 potential_match_encountered = true;
1192 } else if (current_element.AutofillSection() !=
1193 original_element_section &&
Parastoo Geranmayehd5f2cc262018-11-24 00:00:141194 !matching_element.IsNull() &&
Parastoo Geranmayehc5d65c12018-11-21 05:32:021195 matching_element.AutofillSection() !=
1196 original_element_section) {
1197 // The so far matching fields are equally bad. Continue the search if
1198 // none of them have the correct section.
1199 // In other words: bad, then bad => pick none.
1200 matching_element.Reset();
Parastoo Geranmayeh1421e632018-07-25 15:57:031201 } else if (current_element.AutofillSection() ==
Parastoo Geranmayehc5d65c12018-11-21 05:32:021202 original_element_section &&
Parastoo Geranmayehd5f2cc262018-11-24 00:00:141203 !matching_element.IsNull() &&
Parastoo Geranmayehc5d65c12018-11-21 05:32:021204 matching_element.AutofillSection() ==
1205 original_element_section) {
1206 // If two or more fields have the matching name and section, we can't
1207 // decide. Two equally good fields => fail.
1208 matching_element.Reset();
Parastoo Geranmayeh1421e632018-07-25 15:57:031209 return false;
Parastoo Geranmayehc5d65c12018-11-21 05:32:021210 } // For the good, then bad case => keep good. Continue the search.
Parastoo Geranmayeh1421e632018-07-25 15:57:031211 }
1212 }
1213 return true;
1214}
1215
sebsge39d1172018-11-07 23:56:461216// TODO(crbug.com/896689): Update this method to use the unique ids once they
1217// are implemented.
sebsge35b762c2018-03-29 20:33:271218void AutofillAgent::ReplaceElementIfNowInvalid(const FormData& original_form) {
1219 // If the document is invalid, bail out.
1220 if (element_.GetDocument().IsNull())
1221 return;
1222
Parastoo Geranmayehc5d65c12018-11-21 05:32:021223 const auto original_element = element_;
1224 WebFormControlElement matching_element;
1225 bool potential_match_encountered = false;
1226
Parastoo Geranmayeh1421e632018-07-25 15:57:031227 if (original_form.name.empty()) {
1228 // If the form has no name, check all the forms.
Jan Wilken Dörrie3f4a8a482020-04-23 07:30:241229 for (const WebFormElement& form : element_.GetDocument().Forms()) {
Parastoo Geranmayehc5d65c12018-11-21 05:32:021230 // If finding a unique element is impossible, don't look further.
Parastoo Geranmayeh1421e632018-07-25 15:57:031231 if (!FindTheUniqueNewVersionOfOldElement(
Jan Wilken Dörrie3f4a8a482020-04-23 07:30:241232 form.GetFormControlElements(), potential_match_encountered,
1233 matching_element, original_element))
Parastoo Geranmayeh1421e632018-07-25 15:57:031234 return;
1235 }
1236 // If the element is not found, we should still check for unowned elements.
Parastoo Geranmayehc5d65c12018-11-21 05:32:021237 if (!matching_element.IsNull()) {
1238 element_ = matching_element;
Parastoo Geranmayeh1421e632018-07-25 15:57:031239 return;
Parastoo Geranmayehc5d65c12018-11-21 05:32:021240 }
Parastoo Geranmayeh1421e632018-07-25 15:57:031241 }
1242
Jan Wilken Dörrie3f4a8a482020-04-23 07:30:241243 // If |element_|'s parent form has no elements, |element_| is now invalid
1244 // and should be updated.
1245 if (!element_.Form().IsNull() &&
1246 element_.Form().GetFormControlElements().empty()) {
1247 return;
sebsge35b762c2018-03-29 20:33:271248 }
1249
sebsge35b762c2018-03-29 20:33:271250 WebFormElement form_element;
Parastoo Geranmayehc5d65c12018-11-21 05:32:021251 bool form_is_found = false;
Parastoo Geranmayeh1421e632018-07-25 15:57:031252 if (!original_form.name.empty()) {
1253 // Try to find the new version of the form.
Jan Wilken Dörrie3f4a8a482020-04-23 07:30:241254 for (const WebFormElement& form : element_.GetDocument().Forms()) {
Parastoo Geranmayeh1421e632018-07-25 15:57:031255 if (original_form.name == form.GetName().Utf16() ||
1256 original_form.name == form.GetAttribute("id").Utf16()) {
Parastoo Geranmayehc5d65c12018-11-21 05:32:021257 if (!form_is_found)
1258 form_element = form;
1259 else // multiple forms with the matching name.
1260 return;
Parastoo Geranmayeh1421e632018-07-25 15:57:031261 }
sebsge35b762c2018-03-29 20:33:271262 }
1263 }
1264
sebsgf32e98d72018-05-11 14:43:011265 if (form_element.IsNull()) {
1266 // Could not find the new version of the form, get all the unowned elements.
1267 std::vector<WebElement> fieldsets;
Jan Wilken Dörrie3f4a8a482020-04-23 07:30:241268 WebVector<WebFormControlElement> elements =
1269 form_util::GetUnownedAutofillableFormFieldElements(
1270 element_.GetDocument().All(), &fieldsets);
Parastoo Geranmayehc5d65c12018-11-21 05:32:021271 // If a unique match was found.
1272 if (FindTheUniqueNewVersionOfOldElement(
1273 elements, potential_match_encountered, matching_element,
1274 original_element) &&
1275 !matching_element.IsNull()) {
1276 element_ = matching_element;
1277 }
Parastoo Geranmayeh1421e632018-07-25 15:57:031278 return;
sebsgf32e98d72018-05-11 14:43:011279 }
Parastoo Geranmayeh1421e632018-07-25 15:57:031280 // This is the case for owned fields that belong to the right named form.
1281 // Get all the elements of the new version of the form.
Parastoo Geranmayehc5d65c12018-11-21 05:32:021282 // If a unique match was found.
Jan Wilken Dörrie3f4a8a482020-04-23 07:30:241283 if (FindTheUniqueNewVersionOfOldElement(form_element.GetFormControlElements(),
1284 potential_match_encountered,
Parastoo Geranmayehc5d65c12018-11-21 05:32:021285 matching_element, original_element) &&
1286 !matching_element.IsNull()) {
1287 element_ = matching_element;
sebsge35b762c2018-03-29 20:33:271288 }
1289}
1290
Shimi Zhang6b72d292019-07-12 20:52:421291const mojo::AssociatedRemote<mojom::AutofillDriver>&
1292AutofillAgent::GetAutofillDriver() {
leon.han97e8f752016-09-06 06:13:201293 if (!autofill_driver_) {
Ken Rockot0aad7ec2018-07-31 15:58:441294 render_frame()->GetRemoteAssociatedInterfaces()->GetInterface(
Shimi Zhang6b72d292019-07-12 20:52:421295 &autofill_driver_);
leon.han90fd63b2016-07-26 01:23:551296 }
leon.hande852902016-05-04 09:16:381297
leon.han97e8f752016-09-06 06:13:201298 return autofill_driver_;
1299}
1300
Shimi Zhang6b72d292019-07-12 20:52:421301const mojo::AssociatedRemote<mojom::PasswordManagerDriver>&
leon.han97e8f752016-09-06 06:13:201302AutofillAgent::GetPasswordManagerDriver() {
1303 DCHECK(password_autofill_agent_);
1304 return password_autofill_agent_->GetPasswordManagerDriver();
leon.hande852902016-05-04 09:16:381305}
1306
[email protected]78192082011-01-29 05:43:441307} // namespace autofill