[email protected] | f07e444 | 2013-06-06 23:03:48 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors. All rights reserved. |
[email protected] | 679f128f | 2010-07-22 22:57:44 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | f07e444 | 2013-06-06 23:03:48 | [diff] [blame] | 5 | #include "components/autofill/content/renderer/autofill_agent.h" |
[email protected] | 679f128f | 2010-07-22 22:57:44 | [diff] [blame] | 6 | |
avi | 4310d52 | 2015-12-25 19:37:59 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
brettw | 9c68c51 | 2015-11-20 23:26:38 | [diff] [blame] | 9 | #include <tuple> |
| 10 | |
[email protected] | a1cb57f | 2011-09-28 22:41:29 | [diff] [blame] | 11 | #include "base/bind.h" |
[email protected] | f920d6e | 2013-03-12 20:20:50 | [diff] [blame] | 12 | #include "base/command_line.h" |
Dominic Battre | 4f1c5ca | 2020-02-10 16:57:50 | [diff] [blame] | 13 | #include "base/debug/alias.h" |
| 14 | #include "base/debug/dump_without_crashing.h" |
Mathieu Perreault | a24d0c2 | 2017-11-10 01:22:00 | [diff] [blame] | 15 | #include "base/feature_list.h" |
brettw | 94a2cc2 | 2015-07-01 19:26:54 | [diff] [blame] | 16 | #include "base/i18n/case_conversion.h" |
skyostil | b0daa01 | 2015-06-02 19:03:48 | [diff] [blame] | 17 | #include "base/location.h" |
mathp | 4f6b038 | 2016-04-12 20:41:12 | [diff] [blame] | 18 | #include "base/metrics/field_trial.h" |
Christoph Schwering | b09a328 | 2020-08-20 13:05:19 | [diff] [blame] | 19 | #include "base/optional.h" |
skyostil | b0daa01 | 2015-06-02 19:03:48 | [diff] [blame] | 20 | #include "base/single_thread_task_runner.h" |
mgiuca | 30f7588 | 2017-03-28 02:07:19 | [diff] [blame] | 21 | #include "base/strings/string_piece.h" |
[email protected] | 1988e1c | 2013-02-28 20:27:42 | [diff] [blame] | 22 | #include "base/strings/string_split.h" |
[email protected] | c72674b | 2013-06-11 04:16:43 | [diff] [blame] | 23 | #include "base/strings/string_util.h" |
[email protected] | d2d79d5 | 2013-06-07 22:23:48 | [diff] [blame] | 24 | #include "base/strings/utf_string_conversions.h" |
gab | 7966d31 | 2016-05-11 20:35:01 | [diff] [blame] | 25 | #include "base/threading/thread_task_runner_handle.h" |
[email protected] | bbd8da9 | 2013-06-28 02:12:20 | [diff] [blame] | 26 | #include "base/time/time.h" |
avi | 4310d52 | 2015-12-25 19:37:59 | [diff] [blame] | 27 | #include "build/build_config.h" |
sandromaggi | d10cf2a | 2020-04-22 18:53:15 | [diff] [blame] | 28 | #include "components/autofill/content/renderer/autofill_assistant_agent.h" |
[email protected] | f07e444 | 2013-06-06 23:03:48 | [diff] [blame] | 29 | #include "components/autofill/content/renderer/form_autofill_util.h" |
Tao Bai | 6cd41ad | 2017-12-04 18:48:17 | [diff] [blame] | 30 | #include "components/autofill/content/renderer/form_tracker.h" |
[email protected] | f07e444 | 2013-06-06 23:03:48 | [diff] [blame] | 31 | #include "components/autofill/content/renderer/password_autofill_agent.h" |
[email protected] | 3cbdf936 | 2014-01-31 23:12:23 | [diff] [blame] | 32 | #include "components/autofill/content/renderer/password_generation_agent.h" |
jww | fb2a143 | 2016-06-13 22:44:15 | [diff] [blame] | 33 | #include "components/autofill/content/renderer/renderer_save_password_progress_logger.h" |
[email protected] | d04f8191 | 2013-06-18 14:52:13 | [diff] [blame] | 34 | #include "components/autofill/core/common/autofill_constants.h" |
[email protected] | d86263dcd | 2014-01-09 10:35:21 | [diff] [blame] | 35 | #include "components/autofill/core/common/autofill_data_validation.h" |
Mathieu Perreault | a24d0c2 | 2017-11-10 01:22:00 | [diff] [blame] | 36 | #include "components/autofill/core/common/autofill_features.h" |
[email protected] | d04f8191 | 2013-06-18 14:52:13 | [diff] [blame] | 37 | #include "components/autofill/core/common/autofill_switches.h" |
Christoph Schwering | aa41ba84 | 2019-10-14 08:00:06 | [diff] [blame] | 38 | #include "components/autofill/core/common/autofill_tick_clock.h" |
rouslan | c31f81d | 2015-08-24 17:33:52 | [diff] [blame] | 39 | #include "components/autofill/core/common/autofill_util.h" |
[email protected] | d04f8191 | 2013-06-18 14:52:13 | [diff] [blame] | 40 | #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" |
jww | fb2a143 | 2016-06-13 22:44:15 | [diff] [blame] | 43 | #include "components/autofill/core/common/password_form_fill_data.h" |
| 44 | #include "components/autofill/core/common/save_password_progress_logger.h" |
[email protected] | fed9d4f | 2013-12-17 21:32:47 | [diff] [blame] | 45 | #include "content/public/common/content_switches.h" |
Tao Bai | 88d56d4 | 2017-07-17 16:07:16 | [diff] [blame] | 46 | #include "content/public/common/origin_util.h" |
[email protected] | ca5190f | 2013-07-08 11:10:31 | [diff] [blame] | 47 | #include "content/public/common/url_constants.h" |
estade | b1bc9bd | 2014-12-02 22:44:11 | [diff] [blame] | 48 | #include "content/public/renderer/render_frame.h" |
[email protected] | a2ef54c | 2011-10-10 16:20:31 | [diff] [blame] | 49 | #include "content/public/renderer/render_view.h" |
[email protected] | ca5190f | 2013-07-08 11:10:31 | [diff] [blame] | 50 | #include "net/cert/cert_status_flags.h" |
rockot | 734fb66 | 2016-10-15 16:41:30 | [diff] [blame] | 51 | #include "services/service_manager/public/cpp/interface_provider.h" |
Ken Rockot | 0aad7ec | 2018-07-31 15:58:44 | [diff] [blame] | 52 | #include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h" |
Dave Tapuska | 129cef8 | 2019-12-19 16:36:48 | [diff] [blame] | 53 | #include "third_party/blink/public/common/input/web_keyboard_event.h" |
Blink Reformat | a30d423 | 2018-04-07 15:31:06 | [diff] [blame] | 54 | #include "third_party/blink/public/platform/web_url_request.h" |
Akihiro Ota | b7c4cb1 | 2019-10-16 22:40:59 | [diff] [blame] | 55 | #include "third_party/blink/public/web/web_ax_enums.h" |
Caitlin Fischer | f23462e | 2019-06-21 15:30:47 | [diff] [blame] | 56 | #include "third_party/blink/public/web/web_ax_object.h" |
Blink Reformat | a30d423 | 2018-04-07 15:31:06 | [diff] [blame] | 57 | #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 Reformat | a30d423 | 2018-04-07 15:31:06 | [diff] [blame] | 65 | #include "third_party/blink/public/web/web_view.h" |
[email protected] | c051a1b | 2011-01-21 23:30:17 | [diff] [blame] | 66 | #include "ui/base/l10n/l10n_util.h" |
[email protected] | 7e9acd08 | 2013-09-17 23:31:16 | [diff] [blame] | 67 | #include "ui/events/keycodes/keyboard_codes.h" |
[email protected] | 679f128f | 2010-07-22 22:57:44 | [diff] [blame] | 68 | |
[email protected] | a1221aea | 2013-11-07 01:31:30 | [diff] [blame] | 69 | using blink::WebAutofillClient; |
Caitlin Fischer | f23462e | 2019-06-21 15:30:47 | [diff] [blame] | 70 | using blink::WebAutofillState; |
| 71 | using blink::WebAXObject; |
[email protected] | dbdd6027 | 2014-04-14 22:48:40 | [diff] [blame] | 72 | using blink::WebConsoleMessage; |
rouslan | f7ebd883 | 2015-01-22 01:54:14 | [diff] [blame] | 73 | using blink::WebDocument; |
[email protected] | 5e7e861 | 2014-03-20 14:43:19 | [diff] [blame] | 74 | using blink::WebElement; |
| 75 | using blink::WebElementCollection; |
[email protected] | a1221aea | 2013-11-07 01:31:30 | [diff] [blame] | 76 | using blink::WebFormControlElement; |
| 77 | using blink::WebFormElement; |
| 78 | using blink::WebFrame; |
| 79 | using blink::WebInputElement; |
| 80 | using blink::WebKeyboardEvent; |
[email protected] | c5041c32 | 2014-04-08 05:06:47 | [diff] [blame] | 81 | using blink::WebLocalFrame; |
[email protected] | a1221aea | 2013-11-07 01:31:30 | [diff] [blame] | 82 | using blink::WebNode; |
[email protected] | a1221aea | 2013-11-07 01:31:30 | [diff] [blame] | 83 | using blink::WebOptionElement; |
| 84 | using blink::WebString; |
[email protected] | 5e7e861 | 2014-03-20 14:43:19 | [diff] [blame] | 85 | using blink::WebVector; |
[email protected] | 679f128f | 2010-07-22 22:57:44 | [diff] [blame] | 86 | |
[email protected] | d86263dcd | 2014-01-09 10:35:21 | [diff] [blame] | 87 | namespace autofill { |
| 88 | |
Tao Bai | 6e605e4 | 2020-05-20 20:05:55 | [diff] [blame] | 89 | using form_util::ExtractMask; |
Maria Kazinova | 168a9ed9 | 2020-02-06 15:05:20 | [diff] [blame] | 90 | using form_util::FindFormAndFieldForFormControlElement; |
| 91 | using form_util::UnownedCheckoutFormElementsAndFieldSetsToFormData; |
Jan Wilken Dörrie | f3785c4 | 2019-06-21 10:47:37 | [diff] [blame] | 92 | using mojom::SubmissionSource; |
Friedrich Horschig | ef921f5 | 2020-02-19 15:16:04 | [diff] [blame] | 93 | using ShowAll = PasswordAutofillAgent::ShowAll; |
| 94 | using GenerationShowing = PasswordAutofillAgent::GenerationShowing; |
Jan Wilken Dörrie | f3785c4 | 2019-06-21 10:47:37 | [diff] [blame] | 95 | |
[email protected] | e47aec5 | 2010-08-12 00:50:30 | [diff] [blame] | 96 | namespace { |
| 97 | |
sebsg | 1c43685 | 2018-04-18 16:54:32 | [diff] [blame] | 98 | // 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). |
sebsg | 248c6b0 | 2018-04-09 20:20:42 | [diff] [blame] | 100 | size_t kWaitTimeForSelectOptionsChangesMs = 50; |
| 101 | |
Tao Bai | 6e605e4 | 2020-05-20 20:05:55 | [diff] [blame] | 102 | // Helper function to return EXTRACT_DATALIST if kAutofillExtractAllDatalist is |
| 103 | // enabled, otherwise EXTRACT_NONE is returned. |
| 104 | ExtractMask GetExtractDatalistMask() { |
| 105 | return base::FeatureList::IsEnabled(features::kAutofillExtractAllDatalists) |
| 106 | ? form_util::EXTRACT_DATALIST |
| 107 | : form_util::EXTRACT_NONE; |
| 108 | } |
| 109 | |
[email protected] | e47aec5 | 2010-08-12 00:50:30 | [diff] [blame] | 110 | } // namespace |
| 111 | |
brettw | b505b7a | 2014-11-26 22:05:32 | [diff] [blame] | 112 | AutofillAgent::ShowSuggestionsOptions::ShowSuggestionsOptions() |
| 113 | : autofill_on_empty_values(false), |
| 114 | requires_caret_at_end(false), |
brettw | b505b7a | 2014-11-26 22:05:32 | [diff] [blame] | 115 | show_full_suggestion_list(false), |
Fabio Tirelo | 83662a2 | 2018-07-06 15:03:48 | [diff] [blame] | 116 | autoselect_first_suggestion(false) {} |
brettw | b505b7a | 2014-11-26 22:05:32 | [diff] [blame] | 117 | |
estade | b1bc9bd | 2014-12-02 22:44:11 | [diff] [blame] | 118 | AutofillAgent::AutofillAgent(content::RenderFrame* render_frame, |
[email protected] | 3cbdf936 | 2014-01-31 23:12:23 | [diff] [blame] | 119 | PasswordAutofillAgent* password_autofill_agent, |
Ben Goodger | 205467b | 2017-08-02 19:33:55 | [diff] [blame] | 120 | PasswordGenerationAgent* password_generation_agent, |
sandromaggi | d10cf2a | 2020-04-22 18:53:15 | [diff] [blame] | 121 | AutofillAssistantAgent* autofill_assistant_agent, |
Ken Rockot | 0aad7ec | 2018-07-31 15:58:44 | [diff] [blame] | 122 | blink::AssociatedInterfaceRegistry* registry) |
estade | b1bc9bd | 2014-12-02 22:44:11 | [diff] [blame] | 123 | : content::RenderFrameObserver(render_frame), |
Mathieu Perreault | d7b56b49 | 2017-10-18 16:52:17 | [diff] [blame] | 124 | form_cache_(render_frame->GetWebFrame()), |
[email protected] | ad19b30 | 2013-04-03 07:42:19 | [diff] [blame] | 125 | password_autofill_agent_(password_autofill_agent), |
[email protected] | 3cbdf936 | 2014-01-31 23:12:23 | [diff] [blame] | 126 | password_generation_agent_(password_generation_agent), |
sandromaggi | d10cf2a | 2020-04-22 18:53:15 | [diff] [blame] | 127 | autofill_assistant_agent_(autofill_assistant_agent), |
[email protected] | 679f128f | 2010-07-22 22:57:44 | [diff] [blame] | 128 | autofill_query_id_(0), |
Parastoo Geranmayeh | 2dcd14f9 | 2018-06-01 17:08:39 | [diff] [blame] | 129 | query_node_autofill_state_(WebAutofillState::kNotFilled), |
[email protected] | b648f24 | 2014-02-25 13:49:06 | [diff] [blame] | 130 | is_popup_possibly_visible_(false), |
gcasto | cdd33e0d | 2015-06-17 22:10:05 | [diff] [blame] | 131 | is_generation_popup_possibly_visible_(false), |
Tao Bai | e13b5ca | 2017-06-23 16:15:51 | [diff] [blame] | 132 | is_user_gesture_required_(true), |
Tao Bai | 88d56d4 | 2017-07-17 16:07:16 | [diff] [blame] | 133 | is_secure_context_required_(false), |
Maria Kazinova | 286b24e | 2020-02-05 13:20:12 | [diff] [blame] | 134 | form_tracker_(render_frame), |
| 135 | field_data_manager_(password_autofill_agent->GetFieldDataManager()) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 136 | render_frame->GetWebFrame()->SetAutofillClient(this); |
leon.han | 90fd63b | 2016-07-26 01:23:55 | [diff] [blame] | 137 | password_autofill_agent->SetAutofillAgent(this); |
Tao Bai | 6cd41ad | 2017-12-04 18:48:17 | [diff] [blame] | 138 | AddFormObserver(this); |
Ken Rockot | a9bb591 | 2019-12-18 03:13:03 | [diff] [blame] | 139 | registry->AddInterface(base::BindRepeating( |
| 140 | &AutofillAgent::BindPendingReceiver, base::Unretained(this))); |
[email protected] | 679f128f | 2010-07-22 22:57:44 | [diff] [blame] | 141 | } |
| 142 | |
Christoph Schwering | b8fa382 | 2020-05-15 16:04:44 | [diff] [blame] | 143 | // 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 Bai | 6cd41ad | 2017-12-04 18:48:17 | [diff] [blame] | 146 | AutofillAgent::~AutofillAgent() { |
| 147 | RemoveFormObserver(this); |
| 148 | } |
[email protected] | d2f05d0 | 2011-01-27 18:51:01 | [diff] [blame] | 149 | |
Shimi Zhang | 6b72d29 | 2019-07-12 20:52:42 | [diff] [blame] | 150 | void AutofillAgent::BindPendingReceiver( |
| 151 | mojo::PendingAssociatedReceiver<mojom::AutofillAgent> pending_receiver) { |
| 152 | receiver_.Bind(std::move(pending_receiver)); |
leon.han | de85290 | 2016-05-04 09:16:38 | [diff] [blame] | 153 | } |
| 154 | |
Dmitry Gozman | 02dd827 | 2020-06-09 20:01:23 | [diff] [blame] | 155 | void AutofillAgent::DidCommitProvisionalLoad(ui::PageTransition transition) { |
mathp | ffc78e2 | 2015-09-01 14:10:50 | [diff] [blame] | 156 | 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 Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 159 | if (frame->Parent()) |
mathp | ffc78e2 | 2015-09-01 14:10:50 | [diff] [blame] | 160 | return; // Not a top-level navigation. |
| 161 | |
Tao Bai | 6cd41ad | 2017-12-04 18:48:17 | [diff] [blame] | 162 | // Navigation to a new page or a page refresh. |
Nektarios Paisios | 6b79cf6f | 2018-02-26 16:49:36 | [diff] [blame] | 163 | |
Hajime Hoshi | 83c3f50 | 2018-03-14 05:49:01 | [diff] [blame] | 164 | element_.Reset(); |
Nektarios Paisios | 6b79cf6f | 2018-02-26 16:49:36 | [diff] [blame] | 165 | |
Tao Bai | 6cd41ad | 2017-12-04 18:48:17 | [diff] [blame] | 166 | form_cache_.Reset(); |
| 167 | ResetLastInteractedElements(); |
Tao Bai | a674b1d | 2018-01-18 22:33:43 | [diff] [blame] | 168 | OnFormNoLongerSubmittable(); |
Christoph Schwering | b09a328 | 2020-08-20 13:05:19 | [diff] [blame] | 169 | SendPotentiallySubmittedFormToBrowser(); |
[email protected] | 676126f7 | 2011-01-15 00:03:51 | [diff] [blame] | 170 | } |
| 171 | |
estade | b1bc9bd | 2014-12-02 22:44:11 | [diff] [blame] | 172 | void AutofillAgent::DidFinishDocumentLoad() { |
| 173 | ProcessForms(); |
[email protected] | 3609c96 | 2014-07-11 03:18:25 | [diff] [blame] | 174 | } |
| 175 | |
estade | 279252707 | 2014-12-17 00:41:47 | [diff] [blame] | 176 | void AutofillAgent::DidChangeScrollOffset() { |
Changwan Ryu | f85180e5 | 2018-08-10 04:31:46 | [diff] [blame] | 177 | if (element_.IsNull()) |
| 178 | return; |
| 179 | |
Changwan Ryu | 9db32db4 | 2018-01-24 14:54:44 | [diff] [blame] | 180 | 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 Hoshi | 9f80e74 | 2018-04-13 03:35:04 | [diff] [blame] | 184 | render_frame() |
| 185 | ->GetTaskRunner(blink::TaskType::kInternalUserInteraction) |
| 186 | ->PostTask(FROM_HERE, |
| 187 | base::BindOnce(&AutofillAgent::DidChangeScrollOffsetImpl, |
Changwan Ryu | 9db32db4 | 2018-01-24 14:54:44 | [diff] [blame] | 188 | weak_ptr_factory_.GetWeakPtr(), element_)); |
Friedrich Horschig | 3535531 | 2019-09-09 17:26:34 | [diff] [blame] | 189 | } else { |
Changwan Ryu | 9db32db4 | 2018-01-24 14:54:44 | [diff] [blame] | 190 | HidePopup(); |
Changwan Ryu | eb8a057e | 2017-12-08 19:22:33 | [diff] [blame] | 191 | } |
Changwan Ryu | 9db32db4 | 2018-01-24 14:54:44 | [diff] [blame] | 192 | } |
Changwan Ryu | eb8a057e | 2017-12-08 19:22:33 | [diff] [blame] | 193 | |
Changwan Ryu | 9db32db4 | 2018-01-24 14:54:44 | [diff] [blame] | 194 | void AutofillAgent::DidChangeScrollOffsetImpl( |
| 195 | const WebFormControlElement& element) { |
Changwan Ryu | f85180e5 | 2018-08-10 04:31:46 | [diff] [blame] | 196 | if (element != element_ || element_.IsNull() || focus_requires_scroll_ || |
Changwan Ryu | 9db32db4 | 2018-01-24 14:54:44 | [diff] [blame] | 197 | !is_popup_possibly_visible_ || !element_.Focused()) |
estade | b5efbab6 | 2015-04-28 18:07:42 | [diff] [blame] | 198 | return; |
rouslan | c31f81d | 2015-08-24 17:33:52 | [diff] [blame] | 199 | |
Changwan Ryu | 9db32db4 | 2018-01-24 14:54:44 | [diff] [blame] | 200 | FormData form; |
| 201 | FormFieldData field; |
Tao Bai | 6e605e4 | 2020-05-20 20:05:55 | [diff] [blame] | 202 | if (FindFormAndFieldForFormControlElement( |
| 203 | element_, field_data_manager_.get(), |
| 204 | static_cast<ExtractMask>(form_util::EXTRACT_BOUNDS | |
| 205 | GetExtractDatalistMask()), |
| 206 | &form, &field)) { |
Tao Bai | d9c02cf5 | 2020-03-30 22:13:39 | [diff] [blame] | 207 | GetAutofillDriver()->TextFieldDidScroll(form, field, field.bounds); |
Changwan Ryu | 9db32db4 | 2018-01-24 14:54:44 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | // Ignore subsequent scroll offset changes. |
Friedrich Horschig | 3535531 | 2019-09-09 17:26:34 | [diff] [blame] | 211 | HidePopup(); |
estade | b1bc9bd | 2014-12-02 22:44:11 | [diff] [blame] | 212 | } |
| 213 | |
Kent Tamura | 69d143e | 2019-05-22 00:31:45 | [diff] [blame] | 214 | void AutofillAgent::FocusedElementChanged(const WebElement& element) { |
Ehsan Karamad | 3538c8a | 2017-11-20 16:31:40 | [diff] [blame] | 215 | was_focused_before_now_ = false; |
[email protected] | 91dcc6d3 | 2014-07-30 00:01:33 | [diff] [blame] | 216 | HidePopup(); |
| 217 | |
Kent Tamura | 69d143e | 2019-05-22 00:31:45 | [diff] [blame] | 218 | if (element.IsNull()) { |
Colin Blundell | 5ffa4c2 | 2020-10-22 09:33:45 | [diff] [blame] | 219 | // Focus moved away from the last interacted form (if any) to somewhere else |
| 220 | // on the page. |
| 221 | GetAutofillDriver()->FocusNoLongerOnForm(!last_interacted_form_.IsNull()); |
estade | b1bc9bd | 2014-12-02 22:44:11 | [diff] [blame] | 222 | return; |
mathp | 8ac68ce | 2015-12-08 21:49:06 | [diff] [blame] | 223 | } |
estade | b1bc9bd | 2014-12-02 22:44:11 | [diff] [blame] | 224 | |
Kent Tamura | 69d143e | 2019-05-22 00:31:45 | [diff] [blame] | 225 | const WebInputElement* input = ToWebInputElement(&element); |
mathp | b4cf7b8 | 2015-12-11 16:42:24 | [diff] [blame] | 226 | |
Tao Bai | 0c26890 | 2020-07-16 03:26:09 | [diff] [blame] | 227 | bool focus_moved_to_new_form = false; |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 228 | if (!last_interacted_form_.IsNull() && |
Kent Tamura | 69d143e | 2019-05-22 00:31:45 | [diff] [blame] | 229 | (!input || last_interacted_form_ != input->Form())) { |
mathp | 8ac68ce | 2015-12-08 21:49:06 | [diff] [blame] | 230 | // The focused element is not part of the last interacted form (could be |
| 231 | // in a different form). |
Colin Blundell | 5ffa4c2 | 2020-10-22 09:33:45 | [diff] [blame] | 232 | GetAutofillDriver()->FocusNoLongerOnForm(/*had_interacted_form=*/true); |
Tao Bai | 0c26890 | 2020-07-16 03:26:09 | [diff] [blame] | 233 | focus_moved_to_new_form = true; |
mathp | 8ac68ce | 2015-12-08 21:49:06 | [diff] [blame] | 234 | } |
[email protected] | f920d6e | 2013-03-12 20:20:50 | [diff] [blame] | 235 | |
Tao Bai | 0c26890 | 2020-07-16 03:26:09 | [diff] [blame] | 236 | // 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 Tamura | 69d143e | 2019-05-22 00:31:45 | [diff] [blame] | 251 | if (!input || !input->IsEnabled() || input->IsReadOnly() || |
| 252 | !input->IsTextField()) |
[email protected] | f920d6e | 2013-03-12 20:20:50 | [diff] [blame] | 253 | return; |
| 254 | |
Kent Tamura | 69d143e | 2019-05-22 00:31:45 | [diff] [blame] | 255 | element_ = *input; |
Tao Bai | 168e4fc | 2017-06-19 18:46:38 | [diff] [blame] | 256 | |
| 257 | FormData form; |
| 258 | FormFieldData field; |
Tao Bai | 6e605e4 | 2020-05-20 20:05:55 | [diff] [blame] | 259 | if (FindFormAndFieldForFormControlElement( |
| 260 | element_, field_data_manager_.get(), |
| 261 | static_cast<ExtractMask>(form_util::EXTRACT_BOUNDS | |
| 262 | GetExtractDatalistMask()), |
| 263 | &form, &field)) { |
Tao Bai | d9c02cf5 | 2020-03-30 22:13:39 | [diff] [blame] | 264 | GetAutofillDriver()->FocusOnFormField(form, field, field.bounds); |
Tao Bai | 168e4fc | 2017-06-19 18:46:38 | [diff] [blame] | 265 | } |
[email protected] | e5057a2 | 2013-04-22 12:41:39 | [diff] [blame] | 266 | } |
| 267 | |
vabr | d62bc3e | 2016-05-04 15:58:52 | [diff] [blame] | 268 | void AutofillAgent::OnDestruct() { |
| 269 | Shutdown(); |
| 270 | base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, this); |
| 271 | } |
| 272 | |
Tommy Martino | 542b4235 | 2019-07-22 20:39:37 | [diff] [blame] | 273 | void AutofillAgent::AccessibilityModeChanged(const ui::AXMode& mode) { |
| 274 | is_screen_reader_enabled_ = mode.has_mode(ui::AXMode::kScreenReader); |
| 275 | } |
| 276 | |
tmartino | 1c5348c9 | 2016-05-26 18:50:56 | [diff] [blame] | 277 | void AutofillAgent::FireHostSubmitEvents(const WebFormElement& form, |
Tao Bai | a674b1d | 2018-01-18 22:33:43 | [diff] [blame] | 278 | bool known_success, |
| 279 | SubmissionSource source) { |
tmartino | 1c5348c9 | 2016-05-26 18:50:56 | [diff] [blame] | 280 | FormData form_data; |
Maria Kazinova | 286b24e | 2020-02-05 13:20:12 | [diff] [blame] | 281 | if (!form_util::ExtractFormData(form, *field_data_manager_.get(), &form_data)) |
tmartino | 1c5348c9 | 2016-05-26 18:50:56 | [diff] [blame] | 282 | return; |
| 283 | |
Tao Bai | a674b1d | 2018-01-18 22:33:43 | [diff] [blame] | 284 | FireHostSubmitEvents(form_data, known_success, source); |
tmartino | 1c5348c9 | 2016-05-26 18:50:56 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | void AutofillAgent::FireHostSubmitEvents(const FormData& form_data, |
Tao Bai | a674b1d | 2018-01-18 22:33:43 | [diff] [blame] | 288 | bool known_success, |
| 289 | SubmissionSource source) { |
| 290 | // We don't want to fire duplicate submission event. |
Christoph Schwering | b09a328 | 2020-08-20 13:05:19 | [diff] [blame] | 291 | if (!base::FeatureList::IsEnabled( |
| 292 | features::kAutofillAllowDuplicateFormSubmissions) && |
| 293 | !submitted_forms_.insert(form_data.unique_renderer_id).second) { |
Tao Bai | a674b1d | 2018-01-18 22:33:43 | [diff] [blame] | 294 | return; |
Christoph Schwering | b09a328 | 2020-08-20 13:05:19 | [diff] [blame] | 295 | } |
Tao Bai | a674b1d | 2018-01-18 22:33:43 | [diff] [blame] | 296 | |
Sebastien Lalancette | b2020be3 | 2019-01-08 15:17:59 | [diff] [blame] | 297 | GetAutofillDriver()->FormSubmitted(form_data, known_success, source); |
tmartino | 1c5348c9 | 2016-05-26 18:50:56 | [diff] [blame] | 298 | } |
| 299 | |
vabr | d62bc3e | 2016-05-04 15:58:52 | [diff] [blame] | 300 | void AutofillAgent::Shutdown() { |
Shimi Zhang | 6b72d29 | 2019-07-12 20:52:42 | [diff] [blame] | 301 | receiver_.reset(); |
vabr | d62bc3e | 2016-05-04 15:58:52 | [diff] [blame] | 302 | weak_ptr_factory_.InvalidateWeakPtrs(); |
| 303 | } |
| 304 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 305 | void AutofillAgent::TextFieldDidEndEditing(const WebInputElement& element) { |
Vasilii Sukhanov | acf9b5d | 2018-11-09 16:18:47 | [diff] [blame] | 306 | // 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 Horschig | 8b10e3c6 | 2020-03-04 09:55:01 | [diff] [blame] | 312 | GetAutofillDriver()->DidEndTextFieldEditing(); |
Ioana Pandele | 9e742fe | 2018-10-11 19:07:54 | [diff] [blame] | 313 | password_autofill_agent_->DidEndTextFieldEditing(); |
Ioana Pandele | 0c36cd0e4 | 2018-11-05 17:51:31 | [diff] [blame] | 314 | if (password_generation_agent_) |
| 315 | password_generation_agent_->DidEndTextFieldEditing(element); |
Christoph Schwering | b09a328 | 2020-08-20 13:05:19 | [diff] [blame] | 316 | |
| 317 | SendPotentiallySubmittedFormToBrowser(); |
[email protected] | 676126f7 | 2011-01-15 00:03:51 | [diff] [blame] | 318 | } |
| 319 | |
Tao Bai | e13b5ca | 2017-06-23 16:15:51 | [diff] [blame] | 320 | void AutofillAgent::SetUserGestureRequired(bool required) { |
Tao Bai | 6cd41ad | 2017-12-04 18:48:17 | [diff] [blame] | 321 | form_tracker_.set_user_gesture_required(required); |
Tao Bai | e13b5ca | 2017-06-23 16:15:51 | [diff] [blame] | 322 | } |
| 323 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 324 | void AutofillAgent::TextFieldDidChange(const WebFormControlElement& element) { |
Tao Bai | 6cd41ad | 2017-12-04 18:48:17 | [diff] [blame] | 325 | form_tracker_.TextFieldDidChange(element); |
[email protected] | 676126f7 | 2011-01-15 00:03:51 | [diff] [blame] | 326 | } |
| 327 | |
Tao Bai | 6cd41ad | 2017-12-04 18:48:17 | [diff] [blame] | 328 | void AutofillAgent::OnTextFieldDidChange(const WebInputElement& element) { |
| 329 | if (password_generation_agent_ && |
| 330 | password_generation_agent_->TextDidChangeInTextField(element)) { |
| 331 | is_popup_possibly_visible_ = true; |
[email protected] | f9af283 | 2012-12-14 04:20:43 | [diff] [blame] | 332 | return; |
Tao Bai | 6cd41ad | 2017-12-04 18:48:17 | [diff] [blame] | 333 | } |
[email protected] | f9af283 | 2012-12-14 04:20:43 | [diff] [blame] | 334 | |
Tao Bai | 6cd41ad | 2017-12-04 18:48:17 | [diff] [blame] | 335 | if (password_autofill_agent_->TextDidChangeInTextField(element)) { |
| 336 | is_popup_possibly_visible_ = true; |
| 337 | element_ = element; |
| 338 | return; |
[email protected] | e7e8347 | 2012-04-05 02:56:26 | [diff] [blame] | 339 | } |
[email protected] | 676126f7 | 2011-01-15 00:03:51 | [diff] [blame] | 340 | |
brettw | b505b7a | 2014-11-26 22:05:32 | [diff] [blame] | 341 | ShowSuggestionsOptions options; |
| 342 | options.requires_caret_at_end = true; |
| 343 | ShowSuggestions(element, options); |
[email protected] | 7d24db7 | 2011-08-26 06:02:31 | [diff] [blame] | 344 | |
[email protected] | 1ecbe86 | 2012-10-05 01:29:14 | [diff] [blame] | 345 | FormData form; |
| 346 | FormFieldData field; |
Tao Bai | 6e605e4 | 2020-05-20 20:05:55 | [diff] [blame] | 347 | if (FindFormAndFieldForFormControlElement( |
| 348 | element, field_data_manager_.get(), |
| 349 | static_cast<ExtractMask>(form_util::EXTRACT_BOUNDS | |
| 350 | GetExtractDatalistMask()), |
| 351 | &form, &field)) { |
Tao Bai | d9c02cf5 | 2020-03-30 22:13:39 | [diff] [blame] | 352 | GetAutofillDriver()->TextFieldDidChange(form, field, field.bounds, |
| 353 | AutofillTickClock::NowTicks()); |
[email protected] | 1d14f58 | 2011-09-02 20:42:04 | [diff] [blame] | 354 | } |
[email protected] | 676126f7 | 2011-01-15 00:03:51 | [diff] [blame] | 355 | } |
| 356 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 357 | void AutofillAgent::TextFieldDidReceiveKeyDown(const WebInputElement& element, |
[email protected] | 2fa18c2 | 2011-06-14 23:40:43 | [diff] [blame] | 358 | const WebKeyboardEvent& event) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 359 | if (event.windows_key_code == ui::VKEY_DOWN || |
| 360 | event.windows_key_code == ui::VKEY_UP) { |
brettw | b505b7a | 2014-11-26 22:05:32 | [diff] [blame] | 361 | ShowSuggestionsOptions options; |
| 362 | options.autofill_on_empty_values = true; |
| 363 | options.requires_caret_at_end = true; |
Fabio Tirelo | 83662a2 | 2018-07-06 15:03:48 | [diff] [blame] | 364 | options.autoselect_first_suggestion = |
| 365 | ShouldAutoselectFirstSuggestionOnArrowDown(); |
brettw | b505b7a | 2014-11-26 22:05:32 | [diff] [blame] | 366 | ShowSuggestions(element, options); |
| 367 | } |
[email protected] | efeb565f | 2013-12-12 17:16:41 | [diff] [blame] | 368 | } |
| 369 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 370 | void AutofillAgent::OpenTextDataListChooser(const WebInputElement& element) { |
brettw | b505b7a | 2014-11-26 22:05:32 | [diff] [blame] | 371 | ShowSuggestionsOptions options; |
| 372 | options.autofill_on_empty_values = true; |
brettw | b505b7a | 2014-11-26 22:05:32 | [diff] [blame] | 373 | ShowSuggestions(element, options); |
[email protected] | 676126f7 | 2011-01-15 00:03:51 | [diff] [blame] | 374 | } |
| 375 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 376 | void AutofillAgent::DataListOptionsChanged(const WebInputElement& element) { |
| 377 | if (!is_popup_possibly_visible_ || !element.Focused()) |
estade | df5088a | 2014-12-15 02:04:07 | [diff] [blame] | 378 | return; |
| 379 | |
Tao Bai | 6cd41ad | 2017-12-04 18:48:17 | [diff] [blame] | 380 | OnProvisionallySaveForm(WebFormElement(), element, |
| 381 | ElementChangeSource::TEXTFIELD_CHANGED); |
estade | df5088a | 2014-12-15 02:04:07 | [diff] [blame] | 382 | } |
| 383 | |
vasilii | fd0e8ca | 2017-04-12 11:11:22 | [diff] [blame] | 384 | void AutofillAgent::UserGestureObserved() { |
| 385 | password_autofill_agent_->UserGestureObserved(); |
[email protected] | fc22ae5 | 2014-04-23 13:48:04 | [diff] [blame] | 386 | } |
| 387 | |
leon.han | 90fd63b | 2016-07-26 01:23:55 | [diff] [blame] | 388 | void AutofillAgent::DoAcceptDataListSuggestion( |
[email protected] | d5ca8fb | 2013-04-11 17:54:31 | [diff] [blame] | 389 | const base::string16& suggested_value) { |
Hajime Hoshi | e6104dc9 | 2018-03-20 05:58:02 | [diff] [blame] | 390 | if (element_.IsNull()) |
| 391 | return; |
| 392 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 393 | WebInputElement* input_element = ToWebInputElement(&element_); |
Dominic Battre | 4f1c5ca | 2020-02-10 16:57:50 | [diff] [blame] | 394 | 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] | d5ca8fb | 2013-04-11 17:54:31 | [diff] [blame] | 405 | base::string16 new_value = suggested_value; |
[email protected] | 71a90b3 | 2012-05-18 09:16:53 | [diff] [blame] | 406 | // If this element takes multiple values then replace the last part with |
| 407 | // the suggestion. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 408 | if (input_element->IsMultiple() && input_element->IsEmailField()) { |
Matt Giuca | 93bc158 | 2017-08-01 05:09:57 | [diff] [blame] | 409 | 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] | 71a90b3 | 2012-05-18 09:16:53 | [diff] [blame] | 413 | if (parts.size() == 0) |
mgiuca | 30f7588 | 2017-03-28 02:07:19 | [diff] [blame] | 414 | parts.push_back(base::StringPiece16()); |
[email protected] | 71a90b3 | 2012-05-18 09:16:53 | [diff] [blame] | 415 | |
Jan Wilken Dörrie | 4aa966b | 2020-08-24 18:20:29 | [diff] [blame] | 416 | base::string16 last_part(parts.back()); |
[email protected] | 71a90b3 | 2012-05-18 09:16:53 | [diff] [blame] | 417 | // We want to keep just the leading whitespace. |
| 418 | for (size_t i = 0; i < last_part.size(); ++i) { |
brettw | b341306 | 2015-06-24 00:39:02 | [diff] [blame] | 419 | if (!base::IsUnicodeWhitespace(last_part[i])) { |
[email protected] | 71a90b3 | 2012-05-18 09:16:53 | [diff] [blame] | 420 | last_part = last_part.substr(0, i); |
| 421 | break; |
| 422 | } |
| 423 | } |
| 424 | last_part.append(suggested_value); |
thestig | ee1440b | 2016-07-08 01:06:55 | [diff] [blame] | 425 | parts.back() = last_part; |
[email protected] | 71a90b3 | 2012-05-18 09:16:53 | [diff] [blame] | 426 | |
brettw | d94a2214 | 2015-07-15 05:19:26 | [diff] [blame] | 427 | new_value = base::JoinString(parts, base::ASCIIToUTF16(",")); |
[email protected] | 71a90b3 | 2012-05-18 09:16:53 | [diff] [blame] | 428 | } |
leon.han | 90fd63b | 2016-07-26 01:23:55 | [diff] [blame] | 429 | DoFillFieldWithValue(new_value, input_element); |
[email protected] | 63560b3 | 2014-03-04 07:06:26 | [diff] [blame] | 430 | } |
| 431 | |
Parastoo Geranmayeh | 522c222 | 2018-07-16 15:14:03 | [diff] [blame] | 432 | void AutofillAgent::TriggerRefillIfNeeded(const FormData& form) { |
Parastoo Geranmayeh | 1421e63 | 2018-07-25 15:57:03 | [diff] [blame] | 433 | ReplaceElementIfNowInvalid(form); |
| 434 | |
Parastoo Geranmayeh | 522c222 | 2018-07-16 15:14:03 | [diff] [blame] | 435 | FormFieldData field; |
| 436 | FormData updated_form; |
Maria Kazinova | 168a9ed9 | 2020-02-06 15:05:20 | [diff] [blame] | 437 | if (FindFormAndFieldForFormControlElement(element_, field_data_manager_.get(), |
| 438 | &updated_form, &field) && |
Parastoo Geranmayeh | 1421e63 | 2018-07-25 15:57:03 | [diff] [blame] | 439 | (!element_.IsAutofilled() || !form.DynamicallySameFormAs(updated_form))) { |
Christoph Schwering | aa41ba84 | 2019-10-14 08:00:06 | [diff] [blame] | 440 | base::TimeTicks forms_seen_timestamp = AutofillTickClock::NowTicks(); |
Parastoo Geranmayeh | 522c222 | 2018-07-16 15:14:03 | [diff] [blame] | 441 | 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 Geranmayeh | 1421e63 | 2018-07-25 15:57:03 | [diff] [blame] | 445 | if (!forms.empty() || !frame->Parent()) { |
Parastoo Geranmayeh | 522c222 | 2018-07-16 15:14:03 | [diff] [blame] | 446 | GetAutofillDriver()->FormsSeen(forms, forms_seen_timestamp); |
Parastoo Geranmayeh | 1421e63 | 2018-07-25 15:57:03 | [diff] [blame] | 447 | } |
Parastoo Geranmayeh | 522c222 | 2018-07-16 15:14:03 | [diff] [blame] | 448 | } |
| 449 | } |
| 450 | |
leon.han | de85290 | 2016-05-04 09:16:38 | [diff] [blame] | 451 | // mojom::AutofillAgent: |
leon.han | 90fd63b | 2016-07-26 01:23:55 | [diff] [blame] | 452 | void AutofillAgent::FillForm(int32_t id, const FormData& form) { |
Hajime Hoshi | e6104dc9 | 2018-03-20 05:58:02 | [diff] [blame] | 453 | if (element_.IsNull()) |
| 454 | return; |
| 455 | |
mathp | f43ced4 | 2016-08-04 18:28:23 | [diff] [blame] | 456 | if (id != autofill_query_id_ && id != kNoQueryId) |
leon.han | 90fd63b | 2016-07-26 01:23:55 | [diff] [blame] | 457 | return; |
| 458 | |
sebsg | 248c6b0 | 2018-04-09 20:20:42 | [diff] [blame] | 459 | was_last_action_fill_ = true; |
| 460 | |
sebsg | e39d117 | 2018-11-07 23:56:46 | [diff] [blame] | 461 | // If this is a re-fill, replace the triggering element if it's invalid. |
Fabio Tirelo | a0442b9b9 | 2019-03-19 23:59:18 | [diff] [blame] | 462 | if (id == kNoQueryId) |
sebsg | e35b762c | 2018-03-29 20:33:27 | [diff] [blame] | 463 | ReplaceElementIfNowInvalid(form); |
| 464 | |
Parastoo Geranmayeh | 2dcd14f9 | 2018-06-01 17:08:39 | [diff] [blame] | 465 | query_node_autofill_state_ = element_.GetAutofillState(); |
leon.han | 90fd63b | 2016-07-26 01:23:55 | [diff] [blame] | 466 | form_util::FillForm(form, element_); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 467 | if (!element_.Form().IsNull()) |
Tao Bai | e4d2621 | 2017-12-19 01:30:11 | [diff] [blame] | 468 | UpdateLastInteractedForm(element_.Form()); |
leon.han | 90fd63b | 2016-07-26 01:23:55 | [diff] [blame] | 469 | |
Christoph Schwering | aa41ba84 | 2019-10-14 08:00:06 | [diff] [blame] | 470 | GetAutofillDriver()->DidFillAutofillFormData(form, |
| 471 | AutofillTickClock::NowTicks()); |
Parastoo Geranmayeh | 522c222 | 2018-07-16 15:14:03 | [diff] [blame] | 472 | |
| 473 | TriggerRefillIfNeeded(form); |
Christoph Schwering | b09a328 | 2020-08-20 13:05:19 | [diff] [blame] | 474 | SendPotentiallySubmittedFormToBrowser(); |
[email protected] | 8527a78f | 2014-06-12 12:49:15 | [diff] [blame] | 475 | } |
| 476 | |
leon.han | 90fd63b | 2016-07-26 01:23:55 | [diff] [blame] | 477 | void AutofillAgent::PreviewForm(int32_t id, const FormData& form) { |
Hajime Hoshi | e6104dc9 | 2018-03-20 05:58:02 | [diff] [blame] | 478 | if (element_.IsNull()) |
| 479 | return; |
| 480 | |
leon.han | 90fd63b | 2016-07-26 01:23:55 | [diff] [blame] | 481 | if (id != autofill_query_id_) |
[email protected] | 63560b3 | 2014-03-04 07:06:26 | [diff] [blame] | 482 | return; |
| 483 | |
Parastoo Geranmayeh | 2dcd14f9 | 2018-06-01 17:08:39 | [diff] [blame] | 484 | query_node_autofill_state_ = element_.GetAutofillState(); |
mathp | 9172c62a | 2015-10-08 18:51:14 | [diff] [blame] | 485 | form_util::PreviewForm(form, element_); |
leon.han | 90fd63b | 2016-07-26 01:23:55 | [diff] [blame] | 486 | |
| 487 | GetAutofillDriver()->DidPreviewAutofillFormData(); |
[email protected] | 6f001a6cf | 2012-02-09 15:21:53 | [diff] [blame] | 488 | } |
| 489 | |
leon.han | 90fd63b | 2016-07-26 01:23:55 | [diff] [blame] | 490 | void AutofillAgent::FieldTypePredictionsAvailable( |
leon.han | 35ae41c | 2016-07-29 03:30:58 | [diff] [blame] | 491 | const std::vector<FormDataPredictions>& forms) { |
Mathieu Perreault | d7b56b49 | 2017-10-18 16:52:17 | [diff] [blame] | 492 | bool attach_predictions_to_dom = |
Mathieu Perreault | a24d0c2 | 2017-11-10 01:22:00 | [diff] [blame] | 493 | base::FeatureList::IsEnabled(features::kAutofillShowTypePredictions); |
leon.han | 90fd63b | 2016-07-26 01:23:55 | [diff] [blame] | 494 | for (const auto& form : forms) { |
Mathieu Perreault | d7b56b49 | 2017-10-18 16:52:17 | [diff] [blame] | 495 | form_cache_.ShowPredictions(form, attach_predictions_to_dom); |
leon.han | 90fd63b | 2016-07-26 01:23:55 | [diff] [blame] | 496 | } |
| 497 | } |
| 498 | |
Parastoo Geranmayeh | 7d8ea06 | 2018-05-15 22:16:25 | [diff] [blame] | 499 | void AutofillAgent::ClearSection() { |
Hajime Hoshi | e6104dc9 | 2018-03-20 05:58:02 | [diff] [blame] | 500 | if (element_.IsNull()) |
| 501 | return; |
| 502 | |
Parastoo Geranmayeh | 7d8ea06 | 2018-05-15 22:16:25 | [diff] [blame] | 503 | form_cache_.ClearSectionWithElement(element_); |
[email protected] | 6f001a6cf | 2012-02-09 15:21:53 | [diff] [blame] | 504 | } |
| 505 | |
leon.han | 90fd63b | 2016-07-26 01:23:55 | [diff] [blame] | 506 | void AutofillAgent::ClearPreviewedForm() { |
Hajime Hoshi | 83c3f50 | 2018-03-14 05:49:01 | [diff] [blame] | 507 | // 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] | b5b1d69f | 2014-01-23 23:49:32 | [diff] [blame] | 512 | |
Hajime Hoshi | 83c3f50 | 2018-03-14 05:49:01 | [diff] [blame] | 513 | if (password_autofill_agent_->DidClearAutofillSelection(element_)) |
| 514 | return; |
| 515 | |
| 516 | form_util::ClearPreviewedFormWithElement(element_, |
Parastoo Geranmayeh | 2dcd14f9 | 2018-06-01 17:08:39 | [diff] [blame] | 517 | query_node_autofill_state_); |
[email protected] | 6f001a6cf | 2012-02-09 15:21:53 | [diff] [blame] | 518 | } |
| 519 | |
leon.han | 35ae41c | 2016-07-29 03:30:58 | [diff] [blame] | 520 | void AutofillAgent::FillFieldWithValue(const base::string16& value) { |
Hajime Hoshi | e6104dc9 | 2018-03-20 05:58:02 | [diff] [blame] | 521 | if (element_.IsNull()) |
| 522 | return; |
| 523 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 524 | WebInputElement* input_element = ToWebInputElement(&element_); |
ziran.sun | ee0fd443 | 2014-08-27 10:10:49 | [diff] [blame] | 525 | if (input_element) { |
leon.han | 35ae41c | 2016-07-29 03:30:58 | [diff] [blame] | 526 | DoFillFieldWithValue(value, input_element); |
Parastoo Geranmayeh | 2dcd14f9 | 2018-06-01 17:08:39 | [diff] [blame] | 527 | input_element->SetAutofillState(WebAutofillState::kAutofilled); |
ziran.sun | ee0fd443 | 2014-08-27 10:10:49 | [diff] [blame] | 528 | } |
[email protected] | ac9b92f | 2014-03-15 00:48:32 | [diff] [blame] | 529 | } |
| 530 | |
leon.han | 35ae41c | 2016-07-29 03:30:58 | [diff] [blame] | 531 | void AutofillAgent::PreviewFieldWithValue(const base::string16& value) { |
Hajime Hoshi | e6104dc9 | 2018-03-20 05:58:02 | [diff] [blame] | 532 | if (element_.IsNull()) |
| 533 | return; |
| 534 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 535 | WebInputElement* input_element = ToWebInputElement(&element_); |
[email protected] | e9d29d39 | 2014-03-25 01:15:15 | [diff] [blame] | 536 | if (input_element) |
leon.han | 35ae41c | 2016-07-29 03:30:58 | [diff] [blame] | 537 | DoPreviewFieldWithValue(value, input_element); |
[email protected] | 6f001a6cf | 2012-02-09 15:21:53 | [diff] [blame] | 538 | } |
| 539 | |
Akihiro Ota | b7c4cb1 | 2019-10-16 22:40:59 | [diff] [blame] | 540 | void AutofillAgent::SetSuggestionAvailability( |
| 541 | const mojom::AutofillState state) { |
Caitlin Fischer | f23462e | 2019-06-21 15:30:47 | [diff] [blame] | 542 | if (element_.IsNull()) |
| 543 | return; |
| 544 | |
| 545 | WebInputElement* input_element = ToWebInputElement(&element_); |
Akihiro Ota | b7c4cb1 | 2019-10-16 22:40:59 | [diff] [blame] | 546 | 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 Fischer | f23462e | 2019-06-21 15:30:47 | [diff] [blame] | 566 | } |
| 567 | |
leon.han | 35ae41c | 2016-07-29 03:30:58 | [diff] [blame] | 568 | void AutofillAgent::AcceptDataListSuggestion(const base::string16& value) { |
| 569 | DoAcceptDataListSuggestion(value); |
[email protected] | 5c8de6b9 | 2012-06-08 21:24:08 | [diff] [blame] | 570 | } |
| 571 | |
leon.han | 35ae41c | 2016-07-29 03:30:58 | [diff] [blame] | 572 | void AutofillAgent::FillPasswordSuggestion(const base::string16& username, |
| 573 | const base::string16& password) { |
Hajime Hoshi | e6104dc9 | 2018-03-20 05:58:02 | [diff] [blame] | 574 | if (element_.IsNull()) |
| 575 | return; |
| 576 | |
leon.han | 35ae41c | 2016-07-29 03:30:58 | [diff] [blame] | 577 | bool handled = |
| 578 | password_autofill_agent_->FillSuggestion(element_, username, password); |
[email protected] | 126b1ad | 2014-05-21 22:37:33 | [diff] [blame] | 579 | DCHECK(handled); |
| 580 | } |
| 581 | |
leon.han | 35ae41c | 2016-07-29 03:30:58 | [diff] [blame] | 582 | void AutofillAgent::PreviewPasswordSuggestion(const base::string16& username, |
| 583 | const base::string16& password) { |
Hajime Hoshi | e6104dc9 | 2018-03-20 05:58:02 | [diff] [blame] | 584 | if (element_.IsNull()) |
| 585 | return; |
| 586 | |
kinuko | 0073e7c8 | 2017-01-24 00:42:40 | [diff] [blame] | 587 | bool handled = password_autofill_agent_->PreviewSuggestion( |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 588 | element_, blink::WebString::FromUTF16(username), |
| 589 | blink::WebString::FromUTF16(password)); |
[email protected] | e7e8347 | 2012-04-05 02:56:26 | [diff] [blame] | 590 | DCHECK(handled); |
| 591 | } |
| 592 | |
Christoph Schwering | b09a328 | 2020-08-20 13:05:19 | [diff] [blame] | 593 | bool AutofillAgent::CollectFormlessElements(FormData* output) const { |
Tao Bai | 86ac3715 | 2018-03-02 14:35:33 | [diff] [blame] | 594 | if (render_frame() == nullptr || render_frame()->GetWebFrame() == nullptr) |
| 595 | return false; |
| 596 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 597 | WebDocument document = render_frame()->GetWebFrame()->GetDocument(); |
tmartino | 283dda1 | 2016-06-23 21:43:54 | [diff] [blame] | 598 | |
| 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 Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 605 | form_util::GetUnownedAutofillableFormFieldElements(document.All(), |
tmartino | 283dda1 | 2016-06-23 21:43:54 | [diff] [blame] | 606 | &fieldsets); |
| 607 | |
Christoph Schwering | 33bd2101 | 2020-04-26 15:57:24 | [diff] [blame] | 608 | if (control_elements.size() > kMaxParseableFields) |
tmartino | 283dda1 | 2016-06-23 21:43:54 | [diff] [blame] | 609 | return false; |
| 610 | |
Tao Bai | 6e605e4 | 2020-05-20 20:05:55 | [diff] [blame] | 611 | const ExtractMask extract_mask = static_cast<ExtractMask>( |
| 612 | form_util::EXTRACT_VALUE | form_util::EXTRACT_OPTIONS); |
tmartino | 283dda1 | 2016-06-23 21:43:54 | [diff] [blame] | 613 | |
Maria Kazinova | 168a9ed9 | 2020-02-06 15:05:20 | [diff] [blame] | 614 | return UnownedCheckoutFormElementsAndFieldSetsToFormData( |
| 615 | fieldsets, control_elements, nullptr, document, field_data_manager_.get(), |
| 616 | extract_mask, output, nullptr); |
mathp | ffc78e2 | 2015-09-01 14:10:50 | [diff] [blame] | 617 | } |
| 618 | |
[email protected] | e9d29d39 | 2014-03-25 01:15:15 | [diff] [blame] | 619 | void AutofillAgent::ShowSuggestions(const WebFormControlElement& element, |
brettw | b505b7a | 2014-11-26 22:05:32 | [diff] [blame] | 620 | const ShowSuggestionsOptions& options) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 621 | if (!element.IsEnabled() || element.IsReadOnly()) |
[email protected] | efeb565f | 2013-12-12 17:16:41 | [diff] [blame] | 622 | return; |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 623 | if (!element.SuggestedValue().IsEmpty()) |
[email protected] | 580c3805 | 2014-07-16 07:28:09 | [diff] [blame] | 624 | return; |
[email protected] | e9d29d39 | 2014-03-25 01:15:15 | [diff] [blame] | 625 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 626 | const WebInputElement* input_element = ToWebInputElement(&element); |
[email protected] | e9d29d39 | 2014-03-25 01:15:15 | [diff] [blame] | 627 | if (input_element) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 628 | if (!input_element->IsTextField()) |
[email protected] | e9d29d39 | 2014-03-25 01:15:15 | [diff] [blame] | 629 | return; |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 630 | if (!input_element->SuggestedValue().IsEmpty()) |
[email protected] | e9d29d39 | 2014-03-25 01:15:15 | [diff] [blame] | 631 | return; |
| 632 | } else { |
mathp | 9172c62a | 2015-10-08 18:51:14 | [diff] [blame] | 633 | DCHECK(form_util::IsTextAreaElement(element)); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 634 | if (!element.ToConst<WebFormControlElement>().SuggestedValue().IsEmpty()) |
[email protected] | e9d29d39 | 2014-03-25 01:15:15 | [diff] [blame] | 635 | return; |
| 636 | } |
[email protected] | e47aec5 | 2010-08-12 00:50:30 | [diff] [blame] | 637 | |
[email protected] | 1c69f8b7 | 2012-03-14 03:18:50 | [diff] [blame] | 638 | // Don't attempt to autofill with values that are too large or if filling |
| 639 | // criteria are not met. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 640 | WebString value = element.EditingValue(); |
mathp | 5f90bce2 | 2015-11-22 20:16:58 | [diff] [blame] | 641 | if (value.length() > kMaxDataLength || |
Friedrich Horschig | 3535531 | 2019-09-09 17:26:34 | [diff] [blame] | 642 | (!options.autofill_on_empty_values && value.IsEmpty()) || |
mathp | 5f90bce2 | 2015-11-22 20:16:58 | [diff] [blame] | 643 | (options.requires_caret_at_end && |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 644 | (element.SelectionStart() != element.SelectionEnd() || |
| 645 | element.SelectionEnd() != static_cast<int>(value.length())))) { |
[email protected] | 1c69f8b7 | 2012-03-14 03:18:50 | [diff] [blame] | 646 | // Any popup currently showing is obsolete. |
[email protected] | b648f24 | 2014-02-25 13:49:06 | [diff] [blame] | 647 | HidePopup(); |
[email protected] | e47aec5 | 2010-08-12 00:50:30 | [diff] [blame] | 648 | return; |
[email protected] | 1c69f8b7 | 2012-03-14 03:18:50 | [diff] [blame] | 649 | } |
[email protected] | e47aec5 | 2010-08-12 00:50:30 | [diff] [blame] | 650 | |
[email protected] | bef7f927 | 2012-04-17 10:47:49 | [diff] [blame] | 651 | element_ = element; |
mathp | 9172c62a | 2015-10-08 18:51:14 | [diff] [blame] | 652 | if (form_util::IsAutofillableInputElement(input_element) && |
gcasto | cdd33e0d | 2015-06-17 22:10:05 | [diff] [blame] | 653 | password_autofill_agent_->ShowSuggestions( |
Friedrich Horschig | ef921f5 | 2020-02-19 15:16:04 | [diff] [blame] | 654 | *input_element, ShowAll(options.show_full_suggestion_list), |
| 655 | GenerationShowing(is_generation_popup_possibly_visible_))) { |
[email protected] | b648f24 | 2014-02-25 13:49:06 | [diff] [blame] | 656 | is_popup_possibly_visible_ = true; |
[email protected] | c66ba98 | 2013-04-05 09:56:17 | [diff] [blame] | 657 | return; |
[email protected] | b648f24 | 2014-02-25 13:49:06 | [diff] [blame] | 658 | } |
[email protected] | bef7f927 | 2012-04-17 10:47:49 | [diff] [blame] | 659 | |
gcasto | cdd33e0d | 2015-06-17 22:10:05 | [diff] [blame] | 660 | if (is_generation_popup_possibly_visible_) |
| 661 | return; |
| 662 | |
jww | 2035507 | 2014-12-12 21:00:55 | [diff] [blame] | 663 | // Password field elements should only have suggestions shown by the password |
| 664 | // autofill agent. |
Dominic Battre | 9758fad | 2019-01-02 14:14:33 | [diff] [blame] | 665 | // 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 Battre | 1bb521c | 2017-12-19 21:10:03 | [diff] [blame] | 673 | !query_password_suggestion_) { |
jww | 2035507 | 2014-12-12 21:00:55 | [diff] [blame] | 674 | return; |
Dominic Battre | 1bb521c | 2017-12-19 21:10:03 | [diff] [blame] | 675 | } |
jww | 2035507 | 2014-12-12 21:00:55 | [diff] [blame] | 676 | |
Fabio Tirelo | 83662a2 | 2018-07-06 15:03:48 | [diff] [blame] | 677 | QueryAutofillSuggestions(element, options.autoselect_first_suggestion); |
[email protected] | 77bb0da | 2010-11-20 01:55:30 | [diff] [blame] | 678 | } |
| 679 | |
Tao Bai | a8c0d8bb | 2017-12-18 19:47:36 | [diff] [blame] | 680 | void AutofillAgent::SetQueryPasswordSuggestion(bool query) { |
| 681 | query_password_suggestion_ = query; |
| 682 | } |
| 683 | |
Tao Bai | 88d56d4 | 2017-07-17 16:07:16 | [diff] [blame] | 684 | void AutofillAgent::SetSecureContextRequired(bool required) { |
| 685 | is_secure_context_required_ = required; |
| 686 | } |
| 687 | |
Changwan Ryu | eb8a057e | 2017-12-08 19:22:33 | [diff] [blame] | 688 | void AutofillAgent::SetFocusRequiresScroll(bool require) { |
| 689 | focus_requires_scroll_ = require; |
| 690 | } |
| 691 | |
gogerald | d4f184b | 2018-09-19 22:24:24 | [diff] [blame] | 692 | void 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 Kazinova | 168a9ed9 | 2020-02-06 15:05:20 | [diff] [blame] | 704 | bool success = FindFormAndFieldForFormControlElement( |
| 705 | target_form_control_element, field_data_manager_.get(), &form, &field); |
gogerald | 3e2f8a1 | 2018-10-11 21:46:54 | [diff] [blame] | 706 | 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 | |
gogerald | d4f184b | 2018-09-19 22:24:24 | [diff] [blame] | 714 | return std::move(callback).Run(form, field); |
| 715 | } |
| 716 | |
| 717 | blink::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()) { |
gogerald | d5227511 | 2018-09-21 02:17:22 | [diff] [blame] | 737 | // TODO(806868): Query shadow dom when Autofill is available for forms in |
| 738 | // shadow DOM (crbug.com/746593). |
| 739 | return blink::WebElement(); |
gogerald | d4f184b | 2018-09-19 22:24:24 | [diff] [blame] | 740 | } |
| 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 | |
sandromaggi | d10cf2a | 2020-04-22 18:53:15 | [diff] [blame] | 753 | void 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] | e9d29d39 | 2014-03-25 01:15:15 | [diff] [blame] | 762 | void AutofillAgent::QueryAutofillSuggestions( |
Fabio Tirelo | 83662a2 | 2018-07-06 15:03:48 | [diff] [blame] | 763 | const WebFormControlElement& element, |
| 764 | bool autoselect_first_suggestion) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 765 | if (!element.GetDocument().GetFrame()) |
[email protected] | e69b5f7 | 2013-01-24 00:59:13 | [diff] [blame] | 766 | return; |
| 767 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 768 | DCHECK(ToWebInputElement(&element) || form_util::IsTextAreaElement(element)); |
[email protected] | e9d29d39 | 2014-03-25 01:15:15 | [diff] [blame] | 769 | |
[email protected] | 77bb0da | 2010-11-20 01:55:30 | [diff] [blame] | 770 | static int query_counter = 0; |
| 771 | autofill_query_id_ = query_counter++; |
estade | 0ee9126 | 2014-10-23 19:29:19 | [diff] [blame] | 772 | |
[email protected] | 1ecbe86 | 2012-10-05 01:29:14 | [diff] [blame] | 773 | FormData form; |
| 774 | FormFieldData field; |
Tao Bai | 6e605e4 | 2020-05-20 20:05:55 | [diff] [blame] | 775 | if (!FindFormAndFieldForFormControlElement( |
| 776 | element, field_data_manager_.get(), |
| 777 | static_cast<ExtractMask>(form_util::EXTRACT_BOUNDS | |
| 778 | GetExtractDatalistMask()), |
| 779 | &form, &field)) { |
[email protected] | 7837be6 | 2011-01-18 23:45:08 | [diff] [blame] | 780 | // If we didn't find the cached form, at least let autocomplete have a shot |
| 781 | // at providing suggestions. |
Tao Bai | d9c02cf5 | 2020-03-30 22:13:39 | [diff] [blame] | 782 | WebFormControlElementToFormField( |
| 783 | element, nullptr, |
Tao Bai | 6e605e4 | 2020-05-20 20:05:55 | [diff] [blame] | 784 | static_cast<ExtractMask>(form_util::EXTRACT_VALUE | |
| 785 | form_util::EXTRACT_BOUNDS | |
| 786 | GetExtractDatalistMask()), |
Tao Bai | d9c02cf5 | 2020-03-30 22:13:39 | [diff] [blame] | 787 | &field); |
[email protected] | 7837be6 | 2011-01-18 23:45:08 | [diff] [blame] | 788 | } |
[email protected] | 77bb0da | 2010-11-20 01:55:30 | [diff] [blame] | 789 | |
Tao Bai | 88d56d4 | 2017-07-17 16:07:16 | [diff] [blame] | 790 | if (is_secure_context_required_ && |
Tao Bai | 6b032dd | 2017-09-19 18:44:07 | [diff] [blame] | 791 | !(element.GetDocument().IsSecureContext())) { |
Tao Bai | 88d56d4 | 2017-07-17 16:07:16 | [diff] [blame] | 792 | LOG(WARNING) << "Autofill suggestions are disabled because the document " |
Tao Bai | 6b032dd | 2017-09-19 18:44:07 | [diff] [blame] | 793 | "isn't a secure context."; |
Tao Bai | 88d56d4 | 2017-07-17 16:07:16 | [diff] [blame] | 794 | return; |
| 795 | } |
| 796 | |
Tao Bai | 6e605e4 | 2020-05-20 20:05:55 | [diff] [blame] | 797 | 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] | e9d29d39 | 2014-03-25 01:15:15 | [diff] [blame] | 803 | } |
[email protected] | 5c8de6b9 | 2012-06-08 21:24:08 | [diff] [blame] | 804 | |
[email protected] | b648f24 | 2014-02-25 13:49:06 | [diff] [blame] | 805 | is_popup_possibly_visible_ = true; |
Tao Bai | d9c02cf5 | 2020-03-30 22:13:39 | [diff] [blame] | 806 | GetAutofillDriver()->QueryFormFieldAutofill(autofill_query_id_, form, field, |
| 807 | field.bounds, |
| 808 | autoselect_first_suggestion); |
[email protected] | e47aec5 | 2010-08-12 00:50:30 | [diff] [blame] | 809 | } |
| 810 | |
leon.han | 90fd63b | 2016-07-26 01:23:55 | [diff] [blame] | 811 | void AutofillAgent::DoFillFieldWithValue(const base::string16& value, |
| 812 | WebInputElement* node) { |
Tao Bai | 8148326 | 2018-03-02 05:38:12 | [diff] [blame] | 813 | form_tracker_.set_ignore_control_changes(true); |
sebsg | 778c4369 | 2018-01-24 20:01:32 | [diff] [blame] | 814 | node->SetAutofillValue(blink::WebString::FromUTF16(value)); |
Vadym Doroshenko | a332ebc | 2017-09-14 11:46:53 | [diff] [blame] | 815 | password_autofill_agent_->UpdateStateForTextChange(*node); |
Tao Bai | 8148326 | 2018-03-02 05:38:12 | [diff] [blame] | 816 | form_tracker_.set_ignore_control_changes(false); |
[email protected] | ac9b92f | 2014-03-15 00:48:32 | [diff] [blame] | 817 | } |
| 818 | |
leon.han | 90fd63b | 2016-07-26 01:23:55 | [diff] [blame] | 819 | void AutofillAgent::DoPreviewFieldWithValue(const base::string16& value, |
| 820 | WebInputElement* node) { |
Parastoo Geranmayeh | 2dcd14f9 | 2018-06-01 17:08:39 | [diff] [blame] | 821 | query_node_autofill_state_ = element_.GetAutofillState(); |
sebsg | 778c4369 | 2018-01-24 20:01:32 | [diff] [blame] | 822 | node->SetSuggestedValue(blink::WebString::FromUTF16(value)); |
Parastoo Geranmayeh | 2dcd14f9 | 2018-06-01 17:08:39 | [diff] [blame] | 823 | node->SetAutofillState(WebAutofillState::kPreviewed); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 824 | form_util::PreviewSuggestion(node->SuggestedValue().Utf16(), |
| 825 | node->Value().Utf16(), node); |
[email protected] | 6f001a6cf | 2012-02-09 15:21:53 | [diff] [blame] | 826 | } |
| 827 | |
estade | b1bc9bd | 2014-12-02 22:44:11 | [diff] [blame] | 828 | void AutofillAgent::ProcessForms() { |
[email protected] | 5193ea5 | 2014-05-14 01:10:02 | [diff] [blame] | 829 | // Record timestamp of when the forms are first seen. This is used to |
| 830 | // measure the overhead of the Autofill feature. |
Christoph Schwering | aa41ba84 | 2019-10-14 08:00:06 | [diff] [blame] | 831 | base::TimeTicks forms_seen_timestamp = AutofillTickClock::NowTicks(); |
[email protected] | 5193ea5 | 2014-05-14 01:10:02 | [diff] [blame] | 832 | |
estade | b1bc9bd | 2014-12-02 22:44:11 | [diff] [blame] | 833 | WebLocalFrame* frame = render_frame()->GetWebFrame(); |
Maria Kazinova | 168a9ed9 | 2020-02-06 15:05:20 | [diff] [blame] | 834 | std::vector<FormData> forms = |
| 835 | form_cache_.ExtractNewForms(field_data_manager_.get()); |
[email protected] | 5193ea5 | 2014-05-14 01:10:02 | [diff] [blame] | 836 | |
| 837 | // Always communicate to browser process for topmost frame. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 838 | if (!forms.empty() || !frame->Parent()) { |
leon.han | 35ae41c | 2016-07-29 03:30:58 | [diff] [blame] | 839 | GetAutofillDriver()->FormsSeen(forms, forms_seen_timestamp); |
[email protected] | 5193ea5 | 2014-05-14 01:10:02 | [diff] [blame] | 840 | } |
[email protected] | 5193ea5 | 2014-05-14 01:10:02 | [diff] [blame] | 841 | } |
| 842 | |
[email protected] | b648f24 | 2014-02-25 13:49:06 | [diff] [blame] | 843 | void AutofillAgent::HidePopup() { |
| 844 | if (!is_popup_possibly_visible_) |
| 845 | return; |
[email protected] | b648f24 | 2014-02-25 13:49:06 | [diff] [blame] | 846 | is_popup_possibly_visible_ = false; |
gcasto | cdd33e0d | 2015-06-17 22:10:05 | [diff] [blame] | 847 | is_generation_popup_possibly_visible_ = false; |
leon.han | 90fd63b | 2016-07-26 01:23:55 | [diff] [blame] | 848 | |
Friedrich Horschig | 3535531 | 2019-09-09 17:26:34 | [diff] [blame] | 849 | // The keyboard accessory has a separate, more complex hiding logic. |
| 850 | if (IsKeyboardAccessoryEnabled()) |
| 851 | return; |
| 852 | |
leon.han | 90fd63b | 2016-07-26 01:23:55 | [diff] [blame] | 853 | GetAutofillDriver()->HidePopup(); |
[email protected] | 81cd5233 | 2012-11-05 20:36:07 | [diff] [blame] | 854 | } |
| 855 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 856 | void AutofillAgent::DidAssociateFormControlsDynamically() { |
vasilii | 31ba8c6c | 2017-04-28 21:35:08 | [diff] [blame] | 857 | // 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] | 17b6be7 | 2013-04-30 21:33:08 | [diff] [blame] | 861 | } |
| 862 | |
ekaramad | 27ca69b1 | 2017-04-20 18:34:29 | [diff] [blame] | 863 | void AutofillAgent::DidCompleteFocusChangeInFrame() { |
| 864 | WebDocument doc = render_frame()->GetWebFrame()->GetDocument(); |
| 865 | WebElement focused_element; |
| 866 | if (!doc.IsNull()) |
| 867 | focused_element = doc.FocusedElement(); |
Ioana Pandele | 67513a8 | 2019-06-11 10:21:00 | [diff] [blame] | 868 | |
| 869 | if (!focused_element.IsNull() && password_autofill_agent_) |
| 870 | password_autofill_agent_->FocusedNodeHasChanged(focused_element); |
| 871 | |
ekaramad | 27ca69b1 | 2017-04-20 18:34:29 | [diff] [blame] | 872 | // 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 | } |
ekaramad | 27ca69b1 | 2017-04-20 18:34:29 | [diff] [blame] | 879 | |
Changwan Ryu | eb8a057e | 2017-12-08 19:22:33 | [diff] [blame] | 880 | if (!IsKeyboardAccessoryEnabled() && focus_requires_scroll_) |
Ehsan Karamad | 3538c8a | 2017-11-20 16:31:40 | [diff] [blame] | 881 | HandleFocusChangeComplete(); |
Christoph Schwering | b09a328 | 2020-08-20 13:05:19 | [diff] [blame] | 882 | |
| 883 | SendPotentiallySubmittedFormToBrowser(); |
ekaramad | 27ca69b1 | 2017-04-20 18:34:29 | [diff] [blame] | 884 | } |
| 885 | |
| 886 | void AutofillAgent::DidReceiveLeftMouseDownOrGestureTapInNode( |
| 887 | const WebNode& node) { |
Ehsan Karamad | 3538c8a | 2017-11-20 16:31:40 | [diff] [blame] | 888 | DCHECK(!node.IsNull()); |
| 889 | focused_node_was_last_clicked_ = node.Focused(); |
| 890 | |
Jan Wilken Dörrie | 6f5eee5a | 2019-10-11 16:14:36 | [diff] [blame] | 891 | if (IsTouchToFillEnabled() || IsKeyboardAccessoryEnabled() || |
| 892 | !focus_requires_scroll_) { |
Ehsan Karamad | 3538c8a | 2017-11-20 16:31:40 | [diff] [blame] | 893 | HandleFocusChangeComplete(); |
Jan Wilken Dörrie | 6f5eee5a | 2019-10-11 16:14:36 | [diff] [blame] | 894 | } |
Ehsan Karamad | 3538c8a | 2017-11-20 16:31:40 | [diff] [blame] | 895 | } |
| 896 | |
Tao Bai | 8148326 | 2018-03-02 05:38:12 | [diff] [blame] | 897 | void AutofillAgent::SelectControlDidChange( |
| 898 | const WebFormControlElement& element) { |
| 899 | form_tracker_.SelectControlDidChange(element); |
| 900 | } |
| 901 | |
sebsg | 248c6b0 | 2018-04-09 20:20:42 | [diff] [blame] | 902 | void AutofillAgent::SelectFieldOptionsChanged( |
| 903 | const blink::WebFormControlElement& element) { |
| 904 | if (!was_last_action_fill_ || element_.IsNull()) |
| 905 | return; |
| 906 | |
sebsg | 1c43685 | 2018-04-18 16:54:32 | [diff] [blame] | 907 | // 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örrie | f147dd77 | 2019-08-02 05:38:07 | [diff] [blame] | 922 | bool AutofillAgent::ShouldSuppressKeyboard( |
| 923 | const WebFormControlElement& element) { |
sandromaggi | d10cf2a | 2020-04-22 18:53:15 | [diff] [blame] | 924 | // 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örrie | a1f2d063 | 2019-05-08 09:29:00 | [diff] [blame] | 928 | } |
| 929 | |
Maria Kazinova | 2e53e2d | 2020-11-16 12:21:35 | [diff] [blame^] | 930 | void AutofillAgent::FormElementReset(const WebFormElement& form) { |
| 931 | password_autofill_agent_->InformAboutFormClearing(form); |
| 932 | } |
| 933 | |
sebsg | 1c43685 | 2018-04-18 16:54:32 | [diff] [blame] | 934 | void AutofillAgent::SelectWasUpdated( |
| 935 | const blink::WebFormControlElement& element) { |
| 936 | // Look for the form and field associated with the select element. If they are |
| 937 | // found, notify the driver that the the form was modified dynamically. |
sebsg | 248c6b0 | 2018-04-09 20:20:42 | [diff] [blame] | 938 | FormData form; |
| 939 | FormFieldData field; |
Ian Struiksma | 2988bc8 | 2020-02-18 09:13:13 | [diff] [blame] | 940 | if (FindFormAndFieldForFormControlElement(element, field_data_manager_.get(), |
Maria Kazinova | 168a9ed9 | 2020-02-06 15:05:20 | [diff] [blame] | 941 | &form, &field) && |
sebsg | 248c6b0 | 2018-04-09 20:20:42 | [diff] [blame] | 942 | !field.option_values.empty()) { |
sebsg | 1c43685 | 2018-04-18 16:54:32 | [diff] [blame] | 943 | GetAutofillDriver()->SelectFieldOptionsDidChange(form); |
sebsg | 248c6b0 | 2018-04-09 20:20:42 | [diff] [blame] | 944 | } |
| 945 | } |
| 946 | |
Ehsan Karamad | 3538c8a | 2017-11-20 16:31:40 | [diff] [blame] | 947 | void AutofillAgent::FormControlElementClicked( |
| 948 | const WebFormControlElement& element, |
| 949 | bool was_focused) { |
| 950 | last_clicked_form_control_element_for_testing_ = element; |
| 951 | last_clicked_form_control_element_was_focused_for_testing_ = was_focused; |
sebsg | 248c6b0 | 2018-04-09 20:20:42 | [diff] [blame] | 952 | was_last_action_fill_ = false; |
Ehsan Karamad | 3538c8a | 2017-11-20 16:31:40 | [diff] [blame] | 953 | |
| 954 | const WebInputElement* input_element = ToWebInputElement(&element); |
| 955 | if (!input_element && !form_util::IsTextAreaElement(element)) |
| 956 | return; |
| 957 | |
Jan Wilken Dörrie | 6f5eee5a | 2019-10-11 16:14:36 | [diff] [blame] | 958 | if (IsTouchToFillEnabled()) |
| 959 | password_autofill_agent_->TryToShowTouchToFill(element); |
| 960 | |
Ehsan Karamad | 3538c8a | 2017-11-20 16:31:40 | [diff] [blame] | 961 | ShowSuggestionsOptions options; |
| 962 | options.autofill_on_empty_values = true; |
Roger McFarlane | 3a9c440 | 2018-05-18 18:29:35 | [diff] [blame] | 963 | // Show full suggestions when clicking on an already-focused form field. |
| 964 | options.show_full_suggestion_list = element.IsAutofilled() || was_focused; |
Ehsan Karamad | 3538c8a | 2017-11-20 16:31:40 | [diff] [blame] | 965 | |
Ehsan Karamad | 3538c8a | 2017-11-20 16:31:40 | [diff] [blame] | 966 | ShowSuggestions(element, options); |
Christoph Schwering | b09a328 | 2020-08-20 13:05:19 | [diff] [blame] | 967 | |
| 968 | SendPotentiallySubmittedFormToBrowser(); |
Ehsan Karamad | 3538c8a | 2017-11-20 16:31:40 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | void AutofillAgent::HandleFocusChangeComplete() { |
| 972 | WebElement focused_element = |
| 973 | render_frame()->GetWebFrame()->GetDocument().FocusedElement(); |
Tommy Martino | 542b4235 | 2019-07-22 20:39:37 | [diff] [blame] | 974 | // When using Talkback on Android, and possibly others, traversing to and |
| 975 | // focusing a field will not register as a click. Thus, when screen readers |
| 976 | // are used, treat the focused node as if it was the last clicked. Also check |
| 977 | // to ensure focus is on a field where text can be entered. |
| 978 | if ((focused_node_was_last_clicked_ || is_screen_reader_enabled_) && |
| 979 | !focused_element.IsNull() && focused_element.IsFormControlElement() && |
Ehsan Karamad | 3538c8a | 2017-11-20 16:31:40 | [diff] [blame] | 980 | (form_util::IsTextInput(blink::ToWebInputElement(&focused_element)) || |
| 981 | focused_element.HasHTMLTagName("textarea"))) { |
| 982 | FormControlElementClicked(focused_element.ToConst<WebFormControlElement>(), |
| 983 | was_focused_before_now_); |
| 984 | } |
| 985 | |
| 986 | was_focused_before_now_ = true; |
| 987 | focused_node_was_last_clicked_ = false; |
Christoph Schwering | b09a328 | 2020-08-20 13:05:19 | [diff] [blame] | 988 | |
| 989 | SendPotentiallySubmittedFormToBrowser(); |
ekaramad | 27ca69b1 | 2017-04-20 18:34:29 | [diff] [blame] | 990 | } |
| 991 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 992 | void AutofillAgent::AjaxSucceeded() { |
Tao Bai | 6cd41ad | 2017-12-04 18:48:17 | [diff] [blame] | 993 | form_tracker_.AjaxSucceeded(); |
Christoph Schwering | b09a328 | 2020-08-20 13:05:19 | [diff] [blame] | 994 | |
| 995 | SendPotentiallySubmittedFormToBrowser(); |
Tao Bai | 6cd41ad | 2017-12-04 18:48:17 | [diff] [blame] | 996 | } |
| 997 | |
Tao Bai | 8148326 | 2018-03-02 05:38:12 | [diff] [blame] | 998 | void AutofillAgent::OnProvisionallySaveForm( |
| 999 | const WebFormElement& form, |
| 1000 | const WebFormControlElement& element, |
| 1001 | ElementChangeSource source) { |
Tao Bai | 6cd41ad | 2017-12-04 18:48:17 | [diff] [blame] | 1002 | if (source == ElementChangeSource::WILL_SEND_SUBMIT_EVENT) { |
Tao Bai | a674b1d | 2018-01-18 22:33:43 | [diff] [blame] | 1003 | // Fire the form submission event to avoid missing submission when web site |
| 1004 | // handles the onsubmit event, this also gets the form before Javascript |
| 1005 | // could change it. |
| 1006 | // We don't clear submitted_forms_ because OnFormSubmitted will normally be |
| 1007 | // invoked afterwards and we don't want to fire the same event twice. |
| 1008 | FireHostSubmitEvents(form, /*known_success=*/false, |
| 1009 | SubmissionSource::FORM_SUBMISSION); |
| 1010 | ResetLastInteractedElements(); |
Tao Bai | 8148326 | 2018-03-02 05:38:12 | [diff] [blame] | 1011 | } else if (source == ElementChangeSource::TEXTFIELD_CHANGED || |
| 1012 | source == ElementChangeSource::SELECT_CHANGED) { |
Tao Bai | a674b1d | 2018-01-18 22:33:43 | [diff] [blame] | 1013 | // Remember the last form the user interacted with. |
Tao Bai | 6cd41ad | 2017-12-04 18:48:17 | [diff] [blame] | 1014 | if (!element.Form().IsNull()) { |
Tao Bai | e4d2621 | 2017-12-19 01:30:11 | [diff] [blame] | 1015 | UpdateLastInteractedForm(element.Form()); |
Tao Bai | 6cd41ad | 2017-12-04 18:48:17 | [diff] [blame] | 1016 | } else { |
| 1017 | // Remove invisible elements |
| 1018 | for (auto it = formless_elements_user_edited_.begin(); |
| 1019 | it != formless_elements_user_edited_.end();) { |
| 1020 | if (form_util::IsWebElementVisible(*it)) { |
| 1021 | it = formless_elements_user_edited_.erase(it); |
| 1022 | } else { |
| 1023 | ++it; |
| 1024 | } |
| 1025 | } |
| 1026 | formless_elements_user_edited_.insert(element); |
Christoph Schwering | d54e56e | 2020-02-04 16:08:43 | [diff] [blame] | 1027 | provisionally_saved_form_ = base::make_optional<FormData>(); |
| 1028 | if (!CollectFormlessElements(&provisionally_saved_form_.value())) { |
Tao Bai | e4d2621 | 2017-12-19 01:30:11 | [diff] [blame] | 1029 | provisionally_saved_form_.reset(); |
Tao Bai | 6cd41ad | 2017-12-04 18:48:17 | [diff] [blame] | 1030 | } else { |
| 1031 | last_interacted_form_.Reset(); |
| 1032 | } |
| 1033 | } |
Tao Bai | 8148326 | 2018-03-02 05:38:12 | [diff] [blame] | 1034 | |
| 1035 | if (source == ElementChangeSource::TEXTFIELD_CHANGED) |
| 1036 | OnTextFieldDidChange(*ToWebInputElement(&element)); |
| 1037 | else { |
| 1038 | FormData form; |
| 1039 | FormFieldData field; |
Maria Kazinova | 168a9ed9 | 2020-02-06 15:05:20 | [diff] [blame] | 1040 | if (FindFormAndFieldForFormControlElement( |
Tao Bai | 6e605e4 | 2020-05-20 20:05:55 | [diff] [blame] | 1041 | element, field_data_manager_.get(), |
| 1042 | static_cast<ExtractMask>(form_util::EXTRACT_BOUNDS | |
| 1043 | GetExtractDatalistMask()), |
Tao Bai | d9c02cf5 | 2020-03-30 22:13:39 | [diff] [blame] | 1044 | &form, &field)) { |
| 1045 | GetAutofillDriver()->SelectControlDidChange(form, field, field.bounds); |
Tao Bai | 8148326 | 2018-03-02 05:38:12 | [diff] [blame] | 1046 | } |
| 1047 | } |
Tao Bai | 6cd41ad | 2017-12-04 18:48:17 | [diff] [blame] | 1048 | } |
Christoph Schwering | b09a328 | 2020-08-20 13:05:19 | [diff] [blame] | 1049 | SendPotentiallySubmittedFormToBrowser(); |
Tao Bai | 6cd41ad | 2017-12-04 18:48:17 | [diff] [blame] | 1050 | } |
| 1051 | |
| 1052 | void AutofillAgent::OnProbablyFormSubmitted() { |
Christoph Schwering | b09a328 | 2020-08-20 13:05:19 | [diff] [blame] | 1053 | base::Optional<FormData> form_data = GetSubmittedForm(); |
| 1054 | if (form_data.has_value()) { |
| 1055 | FireHostSubmitEvents(form_data.value(), /*known_success=*/false, |
Tao Bai | a674b1d | 2018-01-18 22:33:43 | [diff] [blame] | 1056 | SubmissionSource::PROBABLY_FORM_SUBMITTED); |
| 1057 | } |
Tao Bai | 6cd41ad | 2017-12-04 18:48:17 | [diff] [blame] | 1058 | ResetLastInteractedElements(); |
Tao Bai | a674b1d | 2018-01-18 22:33:43 | [diff] [blame] | 1059 | OnFormNoLongerSubmittable(); |
Christoph Schwering | b09a328 | 2020-08-20 13:05:19 | [diff] [blame] | 1060 | SendPotentiallySubmittedFormToBrowser(); |
Tao Bai | 6cd41ad | 2017-12-04 18:48:17 | [diff] [blame] | 1061 | } |
| 1062 | |
| 1063 | void AutofillAgent::OnFormSubmitted(const WebFormElement& form) { |
Tao Bai | a674b1d | 2018-01-18 22:33:43 | [diff] [blame] | 1064 | // Fire the submission event here because WILL_SEND_SUBMIT_EVENT is skipped |
| 1065 | // if javascript calls submit() directly. |
| 1066 | FireHostSubmitEvents(form, /*known_success=*/false, |
| 1067 | SubmissionSource::FORM_SUBMISSION); |
Tao Bai | 6cd41ad | 2017-12-04 18:48:17 | [diff] [blame] | 1068 | ResetLastInteractedElements(); |
Tao Bai | a674b1d | 2018-01-18 22:33:43 | [diff] [blame] | 1069 | OnFormNoLongerSubmittable(); |
Christoph Schwering | b09a328 | 2020-08-20 13:05:19 | [diff] [blame] | 1070 | SendPotentiallySubmittedFormToBrowser(); |
Tao Bai | 6cd41ad | 2017-12-04 18:48:17 | [diff] [blame] | 1071 | } |
| 1072 | |
| 1073 | void AutofillAgent::OnInferredFormSubmission(SubmissionSource source) { |
| 1074 | // Only handle iframe for FRAME_DETACHED or main frame for |
| 1075 | // SAME_DOCUMENT_NAVIGATION. |
| 1076 | if ((source == SubmissionSource::FRAME_DETACHED && |
| 1077 | !render_frame()->GetWebFrame()->Parent()) || |
| 1078 | (source == SubmissionSource::SAME_DOCUMENT_NAVIGATION && |
| 1079 | render_frame()->GetWebFrame()->Parent())) { |
Tao Bai | a674b1d | 2018-01-18 22:33:43 | [diff] [blame] | 1080 | ResetLastInteractedElements(); |
| 1081 | OnFormNoLongerSubmittable(); |
Christoph Schwering | b09a328 | 2020-08-20 13:05:19 | [diff] [blame] | 1082 | SendPotentiallySubmittedFormToBrowser(); |
Tao Bai | 6cd41ad | 2017-12-04 18:48:17 | [diff] [blame] | 1083 | return; |
| 1084 | } |
| 1085 | |
Tao Bai | 2c1b3d06 | 2017-12-07 17:20:28 | [diff] [blame] | 1086 | if (source == SubmissionSource::FRAME_DETACHED) { |
| 1087 | // Should not access the frame because it is now detached. Instead, use |
Tao Bai | e4d2621 | 2017-12-19 01:30:11 | [diff] [blame] | 1088 | // |provisionally_saved_form_|. |
Christoph Schwering | d54e56e | 2020-02-04 16:08:43 | [diff] [blame] | 1089 | if (provisionally_saved_form_.has_value()) |
| 1090 | FireHostSubmitEvents(provisionally_saved_form_.value(), |
| 1091 | /*known_success=*/true, source); |
Tao Bai | 2c1b3d06 | 2017-12-07 17:20:28 | [diff] [blame] | 1092 | } else { |
Christoph Schwering | b09a328 | 2020-08-20 13:05:19 | [diff] [blame] | 1093 | base::Optional<FormData> form_data = GetSubmittedForm(); |
| 1094 | if (form_data.has_value()) |
| 1095 | FireHostSubmitEvents(form_data.value(), /*known_success=*/true, source); |
Tao Bai | 6cd41ad | 2017-12-04 18:48:17 | [diff] [blame] | 1096 | } |
| 1097 | ResetLastInteractedElements(); |
Tao Bai | a674b1d | 2018-01-18 22:33:43 | [diff] [blame] | 1098 | OnFormNoLongerSubmittable(); |
Christoph Schwering | b09a328 | 2020-08-20 13:05:19 | [diff] [blame] | 1099 | SendPotentiallySubmittedFormToBrowser(); |
Tao Bai | 6cd41ad | 2017-12-04 18:48:17 | [diff] [blame] | 1100 | } |
| 1101 | |
| 1102 | void AutofillAgent::AddFormObserver(Observer* observer) { |
| 1103 | form_tracker_.AddObserver(observer); |
| 1104 | } |
| 1105 | |
| 1106 | void AutofillAgent::RemoveFormObserver(Observer* observer) { |
| 1107 | form_tracker_.RemoveObserver(observer); |
| 1108 | } |
| 1109 | |
Jan Wilken Dörrie | d4119112c | 2020-11-09 18:42:12 | [diff] [blame] | 1110 | void AutofillAgent::TrackAutofilledElement( |
| 1111 | const blink::WebFormControlElement& element) { |
| 1112 | form_tracker_.TrackAutofilledElement(element); |
| 1113 | } |
| 1114 | |
Christoph Schwering | b09a328 | 2020-08-20 13:05:19 | [diff] [blame] | 1115 | base::Optional<FormData> AutofillAgent::GetSubmittedForm() const { |
Tao Bai | 6cd41ad | 2017-12-04 18:48:17 | [diff] [blame] | 1116 | if (!last_interacted_form_.IsNull()) { |
Christoph Schwering | b09a328 | 2020-08-20 13:05:19 | [diff] [blame] | 1117 | FormData form; |
Maria Kazinova | 286b24e | 2020-02-05 13:20:12 | [diff] [blame] | 1118 | if (form_util::ExtractFormData(last_interacted_form_, |
Christoph Schwering | b09a328 | 2020-08-20 13:05:19 | [diff] [blame] | 1119 | *field_data_manager_.get(), &form)) { |
| 1120 | return base::make_optional(form); |
Christoph Schwering | d54e56e | 2020-02-04 16:08:43 | [diff] [blame] | 1121 | } else if (provisionally_saved_form_.has_value()) { |
Christoph Schwering | b09a328 | 2020-08-20 13:05:19 | [diff] [blame] | 1122 | return base::make_optional(provisionally_saved_form_.value()); |
Tao Bai | e4d2621 | 2017-12-19 01:30:11 | [diff] [blame] | 1123 | } |
Tao Bai | 6cd41ad | 2017-12-04 18:48:17 | [diff] [blame] | 1124 | } else if (formless_elements_user_edited_.size() != 0 && |
| 1125 | !form_util::IsSomeControlElementVisible( |
| 1126 | formless_elements_user_edited_)) { |
| 1127 | // we check if all the elements the user has interacted with are gone, |
Tao Bai | e4d2621 | 2017-12-19 01:30:11 | [diff] [blame] | 1128 | // to decide if submission has occurred, and use the |
| 1129 | // provisionally_saved_form_ saved in OnProvisionallySaveForm() if fail to |
| 1130 | // construct form. |
Christoph Schwering | b09a328 | 2020-08-20 13:05:19 | [diff] [blame] | 1131 | FormData form; |
| 1132 | if (CollectFormlessElements(&form)) { |
| 1133 | return base::make_optional(form); |
Christoph Schwering | d54e56e | 2020-02-04 16:08:43 | [diff] [blame] | 1134 | } else if (provisionally_saved_form_.has_value()) { |
Christoph Schwering | b09a328 | 2020-08-20 13:05:19 | [diff] [blame] | 1135 | return base::make_optional(provisionally_saved_form_.value()); |
Tao Bai | 6cd41ad | 2017-12-04 18:48:17 | [diff] [blame] | 1136 | } |
| 1137 | } |
Christoph Schwering | b09a328 | 2020-08-20 13:05:19 | [diff] [blame] | 1138 | return base::nullopt; |
| 1139 | } |
| 1140 | |
| 1141 | void AutofillAgent::SendPotentiallySubmittedFormToBrowser() { |
| 1142 | GetAutofillDriver()->SetFormToBeProbablySubmitted(GetSubmittedForm()); |
Tao Bai | 6cd41ad | 2017-12-04 18:48:17 | [diff] [blame] | 1143 | } |
| 1144 | |
| 1145 | void AutofillAgent::ResetLastInteractedElements() { |
| 1146 | last_interacted_form_.Reset(); |
Alexander Timokhin | 3c45d1a | 2018-03-28 17:49:28 | [diff] [blame] | 1147 | last_clicked_form_control_element_for_testing_.Reset(); |
Tao Bai | 6cd41ad | 2017-12-04 18:48:17 | [diff] [blame] | 1148 | formless_elements_user_edited_.clear(); |
Tao Bai | e4d2621 | 2017-12-19 01:30:11 | [diff] [blame] | 1149 | provisionally_saved_form_.reset(); |
| 1150 | } |
| 1151 | |
| 1152 | void AutofillAgent::UpdateLastInteractedForm(blink::WebFormElement form) { |
| 1153 | last_interacted_form_ = form; |
Christoph Schwering | d54e56e | 2020-02-04 16:08:43 | [diff] [blame] | 1154 | provisionally_saved_form_ = base::make_optional<FormData>(); |
Tao Bai | e4d2621 | 2017-12-19 01:30:11 | [diff] [blame] | 1155 | if (!form_util::ExtractFormData(last_interacted_form_, |
Maria Kazinova | 286b24e | 2020-02-05 13:20:12 | [diff] [blame] | 1156 | *field_data_manager_.get(), |
Christoph Schwering | d54e56e | 2020-02-04 16:08:43 | [diff] [blame] | 1157 | &provisionally_saved_form_.value())) { |
Tao Bai | e4d2621 | 2017-12-19 01:30:11 | [diff] [blame] | 1158 | provisionally_saved_form_.reset(); |
| 1159 | } |
gcasto | c21f1c4c | 2015-03-19 21:57:03 | [diff] [blame] | 1160 | } |
| 1161 | |
Tao Bai | a674b1d | 2018-01-18 22:33:43 | [diff] [blame] | 1162 | void AutofillAgent::OnFormNoLongerSubmittable() { |
| 1163 | submitted_forms_.clear(); |
| 1164 | } |
| 1165 | |
Parastoo Geranmayeh | 1421e63 | 2018-07-25 15:57:03 | [diff] [blame] | 1166 | bool AutofillAgent::FindTheUniqueNewVersionOfOldElement( |
Parastoo Geranmayeh | c5d65c1 | 2018-11-21 05:32:02 | [diff] [blame] | 1167 | const WebVector<WebFormControlElement>& elements, |
| 1168 | bool& potential_match_encountered, |
| 1169 | WebFormControlElement& matching_element, |
Parastoo Geranmayeh | 1421e63 | 2018-07-25 15:57:03 | [diff] [blame] | 1170 | const WebFormControlElement& original_element) { |
Parastoo Geranmayeh | c5d65c1 | 2018-11-21 05:32:02 | [diff] [blame] | 1171 | if (original_element.IsNull()) |
| 1172 | return false; |
| 1173 | |
| 1174 | const auto original_element_section = original_element.AutofillSection(); |
Parastoo Geranmayeh | 1421e63 | 2018-07-25 15:57:03 | [diff] [blame] | 1175 | for (const WebFormControlElement& current_element : elements) { |
| 1176 | if (current_element.IsFocusable() && |
| 1177 | original_element.NameForAutofill() == |
| 1178 | current_element.NameForAutofill()) { |
Parastoo Geranmayeh | c5d65c1 | 2018-11-21 05:32:02 | [diff] [blame] | 1179 | // If this is the first matching element, or is the first with the right |
| 1180 | // section, this is the best match so far. |
| 1181 | // In other words: bad, then good. => pick good. |
| 1182 | if (!potential_match_encountered || |
| 1183 | (current_element.AutofillSection() == original_element_section && |
| 1184 | (matching_element.IsNull() || |
| 1185 | matching_element.AutofillSection() != original_element_section))) { |
| 1186 | matching_element = current_element; |
| 1187 | potential_match_encountered = true; |
| 1188 | } else if (current_element.AutofillSection() != |
| 1189 | original_element_section && |
Parastoo Geranmayeh | d5f2cc26 | 2018-11-24 00:00:14 | [diff] [blame] | 1190 | !matching_element.IsNull() && |
Parastoo Geranmayeh | c5d65c1 | 2018-11-21 05:32:02 | [diff] [blame] | 1191 | matching_element.AutofillSection() != |
| 1192 | original_element_section) { |
| 1193 | // The so far matching fields are equally bad. Continue the search if |
| 1194 | // none of them have the correct section. |
| 1195 | // In other words: bad, then bad => pick none. |
| 1196 | matching_element.Reset(); |
Parastoo Geranmayeh | 1421e63 | 2018-07-25 15:57:03 | [diff] [blame] | 1197 | } else if (current_element.AutofillSection() == |
Parastoo Geranmayeh | c5d65c1 | 2018-11-21 05:32:02 | [diff] [blame] | 1198 | original_element_section && |
Parastoo Geranmayeh | d5f2cc26 | 2018-11-24 00:00:14 | [diff] [blame] | 1199 | !matching_element.IsNull() && |
Parastoo Geranmayeh | c5d65c1 | 2018-11-21 05:32:02 | [diff] [blame] | 1200 | matching_element.AutofillSection() == |
| 1201 | original_element_section) { |
| 1202 | // If two or more fields have the matching name and section, we can't |
| 1203 | // decide. Two equally good fields => fail. |
| 1204 | matching_element.Reset(); |
Parastoo Geranmayeh | 1421e63 | 2018-07-25 15:57:03 | [diff] [blame] | 1205 | return false; |
Parastoo Geranmayeh | c5d65c1 | 2018-11-21 05:32:02 | [diff] [blame] | 1206 | } // For the good, then bad case => keep good. Continue the search. |
Parastoo Geranmayeh | 1421e63 | 2018-07-25 15:57:03 | [diff] [blame] | 1207 | } |
| 1208 | } |
| 1209 | return true; |
| 1210 | } |
| 1211 | |
sebsg | e39d117 | 2018-11-07 23:56:46 | [diff] [blame] | 1212 | // TODO(crbug.com/896689): Update this method to use the unique ids once they |
| 1213 | // are implemented. |
sebsg | e35b762c | 2018-03-29 20:33:27 | [diff] [blame] | 1214 | void AutofillAgent::ReplaceElementIfNowInvalid(const FormData& original_form) { |
| 1215 | // If the document is invalid, bail out. |
| 1216 | if (element_.GetDocument().IsNull()) |
| 1217 | return; |
| 1218 | |
Parastoo Geranmayeh | c5d65c1 | 2018-11-21 05:32:02 | [diff] [blame] | 1219 | const auto original_element = element_; |
| 1220 | WebFormControlElement matching_element; |
| 1221 | bool potential_match_encountered = false; |
| 1222 | |
Parastoo Geranmayeh | 1421e63 | 2018-07-25 15:57:03 | [diff] [blame] | 1223 | if (original_form.name.empty()) { |
| 1224 | // If the form has no name, check all the forms. |
Jan Wilken Dörrie | 3f4a8a48 | 2020-04-23 07:30:24 | [diff] [blame] | 1225 | for (const WebFormElement& form : element_.GetDocument().Forms()) { |
Parastoo Geranmayeh | c5d65c1 | 2018-11-21 05:32:02 | [diff] [blame] | 1226 | // If finding a unique element is impossible, don't look further. |
Parastoo Geranmayeh | 1421e63 | 2018-07-25 15:57:03 | [diff] [blame] | 1227 | if (!FindTheUniqueNewVersionOfOldElement( |
Jan Wilken Dörrie | 3f4a8a48 | 2020-04-23 07:30:24 | [diff] [blame] | 1228 | form.GetFormControlElements(), potential_match_encountered, |
| 1229 | matching_element, original_element)) |
Parastoo Geranmayeh | 1421e63 | 2018-07-25 15:57:03 | [diff] [blame] | 1230 | return; |
| 1231 | } |
| 1232 | // If the element is not found, we should still check for unowned elements. |
Parastoo Geranmayeh | c5d65c1 | 2018-11-21 05:32:02 | [diff] [blame] | 1233 | if (!matching_element.IsNull()) { |
| 1234 | element_ = matching_element; |
Parastoo Geranmayeh | 1421e63 | 2018-07-25 15:57:03 | [diff] [blame] | 1235 | return; |
Parastoo Geranmayeh | c5d65c1 | 2018-11-21 05:32:02 | [diff] [blame] | 1236 | } |
Parastoo Geranmayeh | 1421e63 | 2018-07-25 15:57:03 | [diff] [blame] | 1237 | } |
| 1238 | |
Jan Wilken Dörrie | 3f4a8a48 | 2020-04-23 07:30:24 | [diff] [blame] | 1239 | // If |element_|'s parent form has no elements, |element_| is now invalid |
| 1240 | // and should be updated. |
| 1241 | if (!element_.Form().IsNull() && |
| 1242 | element_.Form().GetFormControlElements().empty()) { |
| 1243 | return; |
sebsg | e35b762c | 2018-03-29 20:33:27 | [diff] [blame] | 1244 | } |
| 1245 | |
sebsg | e35b762c | 2018-03-29 20:33:27 | [diff] [blame] | 1246 | WebFormElement form_element; |
Parastoo Geranmayeh | c5d65c1 | 2018-11-21 05:32:02 | [diff] [blame] | 1247 | bool form_is_found = false; |
Parastoo Geranmayeh | 1421e63 | 2018-07-25 15:57:03 | [diff] [blame] | 1248 | if (!original_form.name.empty()) { |
| 1249 | // Try to find the new version of the form. |
Jan Wilken Dörrie | 3f4a8a48 | 2020-04-23 07:30:24 | [diff] [blame] | 1250 | for (const WebFormElement& form : element_.GetDocument().Forms()) { |
Parastoo Geranmayeh | 1421e63 | 2018-07-25 15:57:03 | [diff] [blame] | 1251 | if (original_form.name == form.GetName().Utf16() || |
| 1252 | original_form.name == form.GetAttribute("id").Utf16()) { |
Parastoo Geranmayeh | c5d65c1 | 2018-11-21 05:32:02 | [diff] [blame] | 1253 | if (!form_is_found) |
| 1254 | form_element = form; |
| 1255 | else // multiple forms with the matching name. |
| 1256 | return; |
Parastoo Geranmayeh | 1421e63 | 2018-07-25 15:57:03 | [diff] [blame] | 1257 | } |
sebsg | e35b762c | 2018-03-29 20:33:27 | [diff] [blame] | 1258 | } |
| 1259 | } |
| 1260 | |
sebsg | f32e98d7 | 2018-05-11 14:43:01 | [diff] [blame] | 1261 | if (form_element.IsNull()) { |
| 1262 | // Could not find the new version of the form, get all the unowned elements. |
| 1263 | std::vector<WebElement> fieldsets; |
Jan Wilken Dörrie | 3f4a8a48 | 2020-04-23 07:30:24 | [diff] [blame] | 1264 | WebVector<WebFormControlElement> elements = |
| 1265 | form_util::GetUnownedAutofillableFormFieldElements( |
| 1266 | element_.GetDocument().All(), &fieldsets); |
Parastoo Geranmayeh | c5d65c1 | 2018-11-21 05:32:02 | [diff] [blame] | 1267 | // If a unique match was found. |
| 1268 | if (FindTheUniqueNewVersionOfOldElement( |
| 1269 | elements, potential_match_encountered, matching_element, |
| 1270 | original_element) && |
| 1271 | !matching_element.IsNull()) { |
| 1272 | element_ = matching_element; |
| 1273 | } |
Parastoo Geranmayeh | 1421e63 | 2018-07-25 15:57:03 | [diff] [blame] | 1274 | return; |
sebsg | f32e98d7 | 2018-05-11 14:43:01 | [diff] [blame] | 1275 | } |
Parastoo Geranmayeh | 1421e63 | 2018-07-25 15:57:03 | [diff] [blame] | 1276 | // This is the case for owned fields that belong to the right named form. |
| 1277 | // Get all the elements of the new version of the form. |
Parastoo Geranmayeh | c5d65c1 | 2018-11-21 05:32:02 | [diff] [blame] | 1278 | // If a unique match was found. |
Jan Wilken Dörrie | 3f4a8a48 | 2020-04-23 07:30:24 | [diff] [blame] | 1279 | if (FindTheUniqueNewVersionOfOldElement(form_element.GetFormControlElements(), |
| 1280 | potential_match_encountered, |
Parastoo Geranmayeh | c5d65c1 | 2018-11-21 05:32:02 | [diff] [blame] | 1281 | matching_element, original_element) && |
| 1282 | !matching_element.IsNull()) { |
| 1283 | element_ = matching_element; |
sebsg | e35b762c | 2018-03-29 20:33:27 | [diff] [blame] | 1284 | } |
| 1285 | } |
| 1286 | |
Shimi Zhang | 6b72d29 | 2019-07-12 20:52:42 | [diff] [blame] | 1287 | const mojo::AssociatedRemote<mojom::AutofillDriver>& |
| 1288 | AutofillAgent::GetAutofillDriver() { |
leon.han | 97e8f75 | 2016-09-06 06:13:20 | [diff] [blame] | 1289 | if (!autofill_driver_) { |
Ken Rockot | 0aad7ec | 2018-07-31 15:58:44 | [diff] [blame] | 1290 | render_frame()->GetRemoteAssociatedInterfaces()->GetInterface( |
Shimi Zhang | 6b72d29 | 2019-07-12 20:52:42 | [diff] [blame] | 1291 | &autofill_driver_); |
leon.han | 90fd63b | 2016-07-26 01:23:55 | [diff] [blame] | 1292 | } |
leon.han | de85290 | 2016-05-04 09:16:38 | [diff] [blame] | 1293 | |
leon.han | 97e8f75 | 2016-09-06 06:13:20 | [diff] [blame] | 1294 | return autofill_driver_; |
| 1295 | } |
| 1296 | |
Shimi Zhang | 6b72d29 | 2019-07-12 20:52:42 | [diff] [blame] | 1297 | const mojo::AssociatedRemote<mojom::PasswordManagerDriver>& |
leon.han | 97e8f75 | 2016-09-06 06:13:20 | [diff] [blame] | 1298 | AutofillAgent::GetPasswordManagerDriver() { |
| 1299 | DCHECK(password_autofill_agent_); |
| 1300 | return password_autofill_agent_->GetPasswordManagerDriver(); |
leon.han | de85290 | 2016-05-04 09:16:38 | [diff] [blame] | 1301 | } |
| 1302 | |
[email protected] | 7819208 | 2011-01-29 05:43:44 | [diff] [blame] | 1303 | } // namespace autofill |