[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" |
Mathieu Perreault | a24d0c2 | 2017-11-10 01:22:00 | [diff] [blame] | 13 | #include "base/feature_list.h" |
brettw | 94a2cc2 | 2015-07-01 19:26:54 | [diff] [blame] | 14 | #include "base/i18n/case_conversion.h" |
skyostil | b0daa01 | 2015-06-02 19:03:48 | [diff] [blame] | 15 | #include "base/location.h" |
mathp | 4f6b038 | 2016-04-12 20:41:12 | [diff] [blame] | 16 | #include "base/metrics/field_trial.h" |
skyostil | b0daa01 | 2015-06-02 19:03:48 | [diff] [blame] | 17 | #include "base/single_thread_task_runner.h" |
mgiuca | 30f7588 | 2017-03-28 02:07:19 | [diff] [blame] | 18 | #include "base/strings/string_piece.h" |
[email protected] | 1988e1c | 2013-02-28 20:27:42 | [diff] [blame] | 19 | #include "base/strings/string_split.h" |
[email protected] | c72674b | 2013-06-11 04:16:43 | [diff] [blame] | 20 | #include "base/strings/string_util.h" |
[email protected] | d2d79d5 | 2013-06-07 22:23:48 | [diff] [blame] | 21 | #include "base/strings/utf_string_conversions.h" |
gab | 7966d31 | 2016-05-11 20:35:01 | [diff] [blame] | 22 | #include "base/threading/thread_task_runner_handle.h" |
[email protected] | bbd8da9 | 2013-06-28 02:12:20 | [diff] [blame] | 23 | #include "base/time/time.h" |
avi | 4310d52 | 2015-12-25 19:37:59 | [diff] [blame] | 24 | #include "build/build_config.h" |
[email protected] | f07e444 | 2013-06-06 23:03:48 | [diff] [blame] | 25 | #include "components/autofill/content/renderer/form_autofill_util.h" |
Tao Bai | 6cd41ad | 2017-12-04 18:48:17 | [diff] [blame] | 26 | #include "components/autofill/content/renderer/form_tracker.h" |
[email protected] | f07e444 | 2013-06-06 23:03:48 | [diff] [blame] | 27 | #include "components/autofill/content/renderer/password_autofill_agent.h" |
[email protected] | 3cbdf936 | 2014-01-31 23:12:23 | [diff] [blame] | 28 | #include "components/autofill/content/renderer/password_generation_agent.h" |
jww | fb2a143 | 2016-06-13 22:44:15 | [diff] [blame] | 29 | #include "components/autofill/content/renderer/renderer_save_password_progress_logger.h" |
[email protected] | d04f8191 | 2013-06-18 14:52:13 | [diff] [blame] | 30 | #include "components/autofill/core/common/autofill_constants.h" |
[email protected] | d86263dcd | 2014-01-09 10:35:21 | [diff] [blame] | 31 | #include "components/autofill/core/common/autofill_data_validation.h" |
Mathieu Perreault | a24d0c2 | 2017-11-10 01:22:00 | [diff] [blame] | 32 | #include "components/autofill/core/common/autofill_features.h" |
[email protected] | d04f8191 | 2013-06-18 14:52:13 | [diff] [blame] | 33 | #include "components/autofill/core/common/autofill_switches.h" |
rouslan | c31f81d | 2015-08-24 17:33:52 | [diff] [blame] | 34 | #include "components/autofill/core/common/autofill_util.h" |
[email protected] | d04f8191 | 2013-06-18 14:52:13 | [diff] [blame] | 35 | #include "components/autofill/core/common/form_data.h" |
| 36 | #include "components/autofill/core/common/form_data_predictions.h" |
| 37 | #include "components/autofill/core/common/form_field_data.h" |
[email protected] | e620d36 | 2013-09-09 08:01:53 | [diff] [blame] | 38 | #include "components/autofill/core/common/password_form.h" |
jww | fb2a143 | 2016-06-13 22:44:15 | [diff] [blame] | 39 | #include "components/autofill/core/common/password_form_fill_data.h" |
| 40 | #include "components/autofill/core/common/save_password_progress_logger.h" |
[email protected] | fed9d4f | 2013-12-17 21:32:47 | [diff] [blame] | 41 | #include "content/public/common/content_switches.h" |
Tao Bai | 88d56d4 | 2017-07-17 16:07:16 | [diff] [blame] | 42 | #include "content/public/common/origin_util.h" |
[email protected] | ca5190f | 2013-07-08 11:10:31 | [diff] [blame] | 43 | #include "content/public/common/url_constants.h" |
estade | b1bc9bd | 2014-12-02 22:44:11 | [diff] [blame] | 44 | #include "content/public/renderer/render_frame.h" |
[email protected] | a2ef54c | 2011-10-10 16:20:31 | [diff] [blame] | 45 | #include "content/public/renderer/render_view.h" |
[email protected] | ca5190f | 2013-07-08 11:10:31 | [diff] [blame] | 46 | #include "net/cert/cert_status_flags.h" |
rockot | 734fb66 | 2016-10-15 16:41:30 | [diff] [blame] | 47 | #include "services/service_manager/public/cpp/interface_provider.h" |
Ken Rockot | 0aad7ec | 2018-07-31 15:58:44 | [diff] [blame] | 48 | #include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h" |
Blink Reformat | a30d423 | 2018-04-07 15:31:06 | [diff] [blame] | 49 | #include "third_party/blink/public/platform/web_keyboard_event.h" |
| 50 | #include "third_party/blink/public/platform/web_url_request.h" |
| 51 | #include "third_party/blink/public/web/web_console_message.h" |
| 52 | #include "third_party/blink/public/web/web_document.h" |
| 53 | #include "third_party/blink/public/web/web_element_collection.h" |
| 54 | #include "third_party/blink/public/web/web_form_control_element.h" |
| 55 | #include "third_party/blink/public/web/web_form_element.h" |
| 56 | #include "third_party/blink/public/web/web_local_frame.h" |
| 57 | #include "third_party/blink/public/web/web_node.h" |
| 58 | #include "third_party/blink/public/web/web_option_element.h" |
| 59 | #include "third_party/blink/public/web/web_user_gesture_indicator.h" |
| 60 | #include "third_party/blink/public/web/web_view.h" |
[email protected] | c051a1b | 2011-01-21 23:30:17 | [diff] [blame] | 61 | #include "ui/base/l10n/l10n_util.h" |
[email protected] | 7e9acd08 | 2013-09-17 23:31:16 | [diff] [blame] | 62 | #include "ui/events/keycodes/keyboard_codes.h" |
[email protected] | 679f128f | 2010-07-22 22:57:44 | [diff] [blame] | 63 | |
Parastoo Geranmayeh | 2dcd14f9 | 2018-06-01 17:08:39 | [diff] [blame] | 64 | using blink::WebAutofillState; |
[email protected] | a1221aea | 2013-11-07 01:31:30 | [diff] [blame] | 65 | using blink::WebAutofillClient; |
[email protected] | dbdd6027 | 2014-04-14 22:48:40 | [diff] [blame] | 66 | using blink::WebConsoleMessage; |
rouslan | f7ebd883 | 2015-01-22 01:54:14 | [diff] [blame] | 67 | using blink::WebDocument; |
[email protected] | 5e7e861 | 2014-03-20 14:43:19 | [diff] [blame] | 68 | using blink::WebElement; |
| 69 | using blink::WebElementCollection; |
[email protected] | a1221aea | 2013-11-07 01:31:30 | [diff] [blame] | 70 | using blink::WebFormControlElement; |
| 71 | using blink::WebFormElement; |
| 72 | using blink::WebFrame; |
| 73 | using blink::WebInputElement; |
| 74 | using blink::WebKeyboardEvent; |
[email protected] | c5041c32 | 2014-04-08 05:06:47 | [diff] [blame] | 75 | using blink::WebLocalFrame; |
[email protected] | a1221aea | 2013-11-07 01:31:30 | [diff] [blame] | 76 | using blink::WebNode; |
[email protected] | a1221aea | 2013-11-07 01:31:30 | [diff] [blame] | 77 | using blink::WebOptionElement; |
| 78 | using blink::WebString; |
rouslan | 6a3f8d9 | 2015-04-28 01:00:01 | [diff] [blame] | 79 | using blink::WebUserGestureIndicator; |
[email protected] | 5e7e861 | 2014-03-20 14:43:19 | [diff] [blame] | 80 | using blink::WebVector; |
[email protected] | 679f128f | 2010-07-22 22:57:44 | [diff] [blame] | 81 | |
[email protected] | d86263dcd | 2014-01-09 10:35:21 | [diff] [blame] | 82 | namespace autofill { |
| 83 | |
[email protected] | e47aec5 | 2010-08-12 00:50:30 | [diff] [blame] | 84 | namespace { |
| 85 | |
sebsg | 1c43685 | 2018-04-18 16:54:32 | [diff] [blame] | 86 | // Time to wait, in ms, o ensure that only a single select change will be acted |
| 87 | // upon, instead of multiple in close succession (debounce time). |
sebsg | 248c6b0 | 2018-04-09 20:20:42 | [diff] [blame] | 88 | size_t kWaitTimeForSelectOptionsChangesMs = 50; |
| 89 | |
mathp | 4f6b038 | 2016-04-12 20:41:12 | [diff] [blame] | 90 | // Whether the "single click" autofill feature is enabled, through command-line |
| 91 | // or field trial. |
| 92 | bool IsSingleClickEnabled() { |
Roger McFarlane | 3a9c440 | 2018-05-18 18:29:35 | [diff] [blame] | 93 | return base::FeatureList::IsEnabled(features::kSingleClickAutofill); |
mathp | 4f6b038 | 2016-04-12 20:41:12 | [diff] [blame] | 94 | } |
| 95 | |
[email protected] | 45a0794 | 2013-07-26 08:28:21 | [diff] [blame] | 96 | // Gets all the data list values (with corresponding label) for the given |
| 97 | // element. |
[email protected] | 5e7e861 | 2014-03-20 14:43:19 | [diff] [blame] | 98 | void GetDataListSuggestions(const WebInputElement& element, |
[email protected] | 45a0794 | 2013-07-26 08:28:21 | [diff] [blame] | 99 | std::vector<base::string16>* values, |
| 100 | std::vector<base::string16>* labels) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 101 | for (const auto& option : element.FilteredDataListOptions()) { |
| 102 | values->push_back(option.Value().Utf16()); |
| 103 | if (option.Value() != option.Label()) |
| 104 | labels->push_back(option.Label().Utf16()); |
[email protected] | bef7f927 | 2012-04-17 10:47:49 | [diff] [blame] | 105 | else |
[email protected] | d5ca8fb | 2013-04-11 17:54:31 | [diff] [blame] | 106 | labels->push_back(base::string16()); |
[email protected] | bef7f927 | 2012-04-17 10:47:49 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
[email protected] | ead7fb0 | 2013-07-18 18:50:12 | [diff] [blame] | 110 | // Trim the vector before sending it to the browser process to ensure we |
[email protected] | 5c8de6b9 | 2012-06-08 21:24:08 | [diff] [blame] | 111 | // don't send too much data through the IPC. |
[email protected] | ead7fb0 | 2013-07-18 18:50:12 | [diff] [blame] | 112 | void TrimStringVectorForIPC(std::vector<base::string16>* strings) { |
| 113 | // Limit the size of the vector. |
[email protected] | d86263dcd | 2014-01-09 10:35:21 | [diff] [blame] | 114 | if (strings->size() > kMaxListSize) |
| 115 | strings->resize(kMaxListSize); |
[email protected] | 5c8de6b9 | 2012-06-08 21:24:08 | [diff] [blame] | 116 | |
[email protected] | ead7fb0 | 2013-07-18 18:50:12 | [diff] [blame] | 117 | // Limit the size of the strings in the vector. |
| 118 | for (size_t i = 0; i < strings->size(); ++i) { |
[email protected] | d86263dcd | 2014-01-09 10:35:21 | [diff] [blame] | 119 | if ((*strings)[i].length() > kMaxDataLength) |
| 120 | (*strings)[i].resize(kMaxDataLength); |
[email protected] | 5c8de6b9 | 2012-06-08 21:24:08 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | |
[email protected] | e47aec5 | 2010-08-12 00:50:30 | [diff] [blame] | 124 | } // namespace |
| 125 | |
brettw | b505b7a | 2014-11-26 22:05:32 | [diff] [blame] | 126 | AutofillAgent::ShowSuggestionsOptions::ShowSuggestionsOptions() |
| 127 | : autofill_on_empty_values(false), |
| 128 | requires_caret_at_end(false), |
brettw | b505b7a | 2014-11-26 22:05:32 | [diff] [blame] | 129 | show_full_suggestion_list(false), |
Fabio Tirelo | 83662a2 | 2018-07-06 15:03:48 | [diff] [blame] | 130 | show_password_suggestions_only(false), |
| 131 | autoselect_first_suggestion(false) {} |
brettw | b505b7a | 2014-11-26 22:05:32 | [diff] [blame] | 132 | |
estade | b1bc9bd | 2014-12-02 22:44:11 | [diff] [blame] | 133 | AutofillAgent::AutofillAgent(content::RenderFrame* render_frame, |
[email protected] | 3cbdf936 | 2014-01-31 23:12:23 | [diff] [blame] | 134 | PasswordAutofillAgent* password_autofill_agent, |
Ben Goodger | 205467b | 2017-08-02 19:33:55 | [diff] [blame] | 135 | PasswordGenerationAgent* password_generation_agent, |
Ken Rockot | 0aad7ec | 2018-07-31 15:58:44 | [diff] [blame] | 136 | blink::AssociatedInterfaceRegistry* registry) |
estade | b1bc9bd | 2014-12-02 22:44:11 | [diff] [blame] | 137 | : content::RenderFrameObserver(render_frame), |
Mathieu Perreault | d7b56b49 | 2017-10-18 16:52:17 | [diff] [blame] | 138 | form_cache_(render_frame->GetWebFrame()), |
[email protected] | ad19b30 | 2013-04-03 07:42:19 | [diff] [blame] | 139 | password_autofill_agent_(password_autofill_agent), |
[email protected] | 3cbdf936 | 2014-01-31 23:12:23 | [diff] [blame] | 140 | password_generation_agent_(password_generation_agent), |
[email protected] | 679f128f | 2010-07-22 22:57:44 | [diff] [blame] | 141 | autofill_query_id_(0), |
Parastoo Geranmayeh | 2dcd14f9 | 2018-06-01 17:08:39 | [diff] [blame] | 142 | query_node_autofill_state_(WebAutofillState::kNotFilled), |
[email protected] | 90f2625 | 2013-02-15 19:48:32 | [diff] [blame] | 143 | ignore_text_changes_(false), |
[email protected] | b648f24 | 2014-02-25 13:49:06 | [diff] [blame] | 144 | is_popup_possibly_visible_(false), |
gcasto | cdd33e0d | 2015-06-17 22:10:05 | [diff] [blame] | 145 | is_generation_popup_possibly_visible_(false), |
Tao Bai | e13b5ca | 2017-06-23 16:15:51 | [diff] [blame] | 146 | is_user_gesture_required_(true), |
Tao Bai | 88d56d4 | 2017-07-17 16:07:16 | [diff] [blame] | 147 | is_secure_context_required_(false), |
Tao Bai | 6cd41ad | 2017-12-04 18:48:17 | [diff] [blame] | 148 | form_tracker_(render_frame), |
leon.han | 90fd63b | 2016-07-26 01:23:55 | [diff] [blame] | 149 | binding_(this), |
[email protected] | 2c30525 | 2013-04-26 19:57:05 | [diff] [blame] | 150 | weak_ptr_factory_(this) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 151 | render_frame->GetWebFrame()->SetAutofillClient(this); |
leon.han | 90fd63b | 2016-07-26 01:23:55 | [diff] [blame] | 152 | password_autofill_agent->SetAutofillAgent(this); |
Tao Bai | 6cd41ad | 2017-12-04 18:48:17 | [diff] [blame] | 153 | AddFormObserver(this); |
Ben Goodger | 205467b | 2017-08-02 19:33:55 | [diff] [blame] | 154 | registry->AddInterface( |
leon.han | de85290 | 2016-05-04 09:16:38 | [diff] [blame] | 155 | base::Bind(&AutofillAgent::BindRequest, base::Unretained(this))); |
[email protected] | 679f128f | 2010-07-22 22:57:44 | [diff] [blame] | 156 | } |
| 157 | |
Tao Bai | 6cd41ad | 2017-12-04 18:48:17 | [diff] [blame] | 158 | AutofillAgent::~AutofillAgent() { |
| 159 | RemoveFormObserver(this); |
| 160 | } |
[email protected] | d2f05d0 | 2011-01-27 18:51:01 | [diff] [blame] | 161 | |
Ken Rockot | 0aad7ec | 2018-07-31 15:58:44 | [diff] [blame] | 162 | void AutofillAgent::BindRequest(mojom::AutofillAgentAssociatedRequest request) { |
leon.han | 90fd63b | 2016-07-26 01:23:55 | [diff] [blame] | 163 | binding_.Bind(std::move(request)); |
leon.han | de85290 | 2016-05-04 09:16:38 | [diff] [blame] | 164 | } |
| 165 | |
mathp | 692aba8 | 2015-03-06 20:55:02 | [diff] [blame] | 166 | bool AutofillAgent::FormDataCompare::operator()(const FormData& lhs, |
| 167 | const FormData& rhs) const { |
brettw | 9c68c51 | 2015-11-20 23:26:38 | [diff] [blame] | 168 | return std::tie(lhs.name, lhs.origin, lhs.action, lhs.is_form_tag) < |
| 169 | std::tie(rhs.name, rhs.origin, rhs.action, rhs.is_form_tag); |
mathp | 692aba8 | 2015-03-06 20:55:02 | [diff] [blame] | 170 | } |
| 171 | |
Dmitry Gozman | 9cfe75d | 2018-09-18 21:36:08 | [diff] [blame] | 172 | void AutofillAgent::DidCommitProvisionalLoad(bool is_same_document_navigation, |
| 173 | ui::PageTransition transition) { |
mathp | ffc78e2 | 2015-09-01 14:10:50 | [diff] [blame] | 174 | blink::WebFrame* frame = render_frame()->GetWebFrame(); |
| 175 | // TODO(dvadym): check if we need to check if it is main frame navigation |
| 176 | // http://crbug.com/443155 |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 177 | if (frame->Parent()) |
mathp | ffc78e2 | 2015-09-01 14:10:50 | [diff] [blame] | 178 | return; // Not a top-level navigation. |
| 179 | |
Tao Bai | 6cd41ad | 2017-12-04 18:48:17 | [diff] [blame] | 180 | if (is_same_document_navigation) |
| 181 | return; |
Hajime Hoshi | 53820e22 | 2017-08-17 02:28:11 | [diff] [blame] | 182 | |
Tao Bai | 6cd41ad | 2017-12-04 18:48:17 | [diff] [blame] | 183 | // Navigation to a new page or a page refresh. |
Nektarios Paisios | 6b79cf6f | 2018-02-26 16:49:36 | [diff] [blame] | 184 | |
Hajime Hoshi | 83c3f50 | 2018-03-14 05:49:01 | [diff] [blame] | 185 | element_.Reset(); |
Nektarios Paisios | 6b79cf6f | 2018-02-26 16:49:36 | [diff] [blame] | 186 | |
Tao Bai | 6cd41ad | 2017-12-04 18:48:17 | [diff] [blame] | 187 | form_cache_.Reset(); |
| 188 | ResetLastInteractedElements(); |
Tao Bai | a674b1d | 2018-01-18 22:33:43 | [diff] [blame] | 189 | OnFormNoLongerSubmittable(); |
[email protected] | 676126f7 | 2011-01-15 00:03:51 | [diff] [blame] | 190 | } |
| 191 | |
estade | b1bc9bd | 2014-12-02 22:44:11 | [diff] [blame] | 192 | void AutofillAgent::DidFinishDocumentLoad() { |
| 193 | ProcessForms(); |
[email protected] | 3609c96 | 2014-07-11 03:18:25 | [diff] [blame] | 194 | } |
| 195 | |
estade | 279252707 | 2014-12-17 00:41:47 | [diff] [blame] | 196 | void AutofillAgent::DidChangeScrollOffset() { |
Changwan Ryu | f85180e5 | 2018-08-10 04:31:46 | [diff] [blame] | 197 | if (element_.IsNull()) |
| 198 | return; |
| 199 | |
Changwan Ryu | 9db32db4 | 2018-01-24 14:54:44 | [diff] [blame] | 200 | if (!focus_requires_scroll_) { |
| 201 | // Post a task here since scroll offset may change during layout. |
| 202 | // (https://crbug.com/804886) |
| 203 | weak_ptr_factory_.InvalidateWeakPtrs(); |
Hajime Hoshi | 9f80e74 | 2018-04-13 03:35:04 | [diff] [blame] | 204 | render_frame() |
| 205 | ->GetTaskRunner(blink::TaskType::kInternalUserInteraction) |
| 206 | ->PostTask(FROM_HERE, |
| 207 | base::BindOnce(&AutofillAgent::DidChangeScrollOffsetImpl, |
Changwan Ryu | 9db32db4 | 2018-01-24 14:54:44 | [diff] [blame] | 208 | weak_ptr_factory_.GetWeakPtr(), element_)); |
| 209 | } else if (!IsKeyboardAccessoryEnabled()) { |
| 210 | HidePopup(); |
Changwan Ryu | eb8a057e | 2017-12-08 19:22:33 | [diff] [blame] | 211 | } |
Changwan Ryu | 9db32db4 | 2018-01-24 14:54:44 | [diff] [blame] | 212 | } |
Changwan Ryu | eb8a057e | 2017-12-08 19:22:33 | [diff] [blame] | 213 | |
Changwan Ryu | 9db32db4 | 2018-01-24 14:54:44 | [diff] [blame] | 214 | void AutofillAgent::DidChangeScrollOffsetImpl( |
| 215 | const WebFormControlElement& element) { |
Changwan Ryu | f85180e5 | 2018-08-10 04:31:46 | [diff] [blame] | 216 | if (element != element_ || element_.IsNull() || focus_requires_scroll_ || |
Changwan Ryu | 9db32db4 | 2018-01-24 14:54:44 | [diff] [blame] | 217 | !is_popup_possibly_visible_ || !element_.Focused()) |
estade | b5efbab6 | 2015-04-28 18:07:42 | [diff] [blame] | 218 | return; |
rouslan | c31f81d | 2015-08-24 17:33:52 | [diff] [blame] | 219 | |
Changwan Ryu | 9db32db4 | 2018-01-24 14:54:44 | [diff] [blame] | 220 | FormData form; |
| 221 | FormFieldData field; |
| 222 | if (form_util::FindFormAndFieldForFormControlElement(element_, &form, |
| 223 | &field)) { |
| 224 | GetAutofillDriver()->TextFieldDidScroll( |
| 225 | form, field, |
| 226 | render_frame()->GetRenderView()->ElementBoundsInWindow(element_)); |
| 227 | } |
| 228 | |
| 229 | // Ignore subsequent scroll offset changes. |
| 230 | if (!IsKeyboardAccessoryEnabled()) |
| 231 | HidePopup(); |
estade | b1bc9bd | 2014-12-02 22:44:11 | [diff] [blame] | 232 | } |
| 233 | |
[email protected] | 5e7e861 | 2014-03-20 14:43:19 | [diff] [blame] | 234 | void AutofillAgent::FocusedNodeChanged(const WebNode& node) { |
Ehsan Karamad | 3538c8a | 2017-11-20 16:31:40 | [diff] [blame] | 235 | was_focused_before_now_ = false; |
| 236 | |
Changwan Ryu | eb8a057e | 2017-12-08 19:22:33 | [diff] [blame] | 237 | if ((IsKeyboardAccessoryEnabled() || !focus_requires_scroll_) && |
Ehsan Karamad | 3538c8a | 2017-11-20 16:31:40 | [diff] [blame] | 238 | WebUserGestureIndicator::IsProcessingUserGesture( |
| 239 | node.IsNull() ? nullptr : node.GetDocument().GetFrame())) { |
| 240 | focused_node_was_last_clicked_ = true; |
| 241 | HandleFocusChangeComplete(); |
| 242 | } |
ekaramad | 27ca69b1 | 2017-04-20 18:34:29 | [diff] [blame] | 243 | |
[email protected] | 91dcc6d3 | 2014-07-30 00:01:33 | [diff] [blame] | 244 | HidePopup(); |
| 245 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 246 | if (node.IsNull() || !node.IsElementNode()) { |
| 247 | if (!last_interacted_form_.IsNull()) { |
mathp | 8ac68ce | 2015-12-08 21:49:06 | [diff] [blame] | 248 | // Focus moved away from the last interacted form to somewhere else on |
| 249 | // the page. |
leon.han | 90fd63b | 2016-07-26 01:23:55 | [diff] [blame] | 250 | GetAutofillDriver()->FocusNoLongerOnForm(); |
mathp | 8ac68ce | 2015-12-08 21:49:06 | [diff] [blame] | 251 | } |
estade | b1bc9bd | 2014-12-02 22:44:11 | [diff] [blame] | 252 | return; |
mathp | 8ac68ce | 2015-12-08 21:49:06 | [diff] [blame] | 253 | } |
estade | b1bc9bd | 2014-12-02 22:44:11 | [diff] [blame] | 254 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 255 | WebElement web_element = node.ToConst<WebElement>(); |
| 256 | const WebInputElement* element = ToWebInputElement(&web_element); |
mathp | b4cf7b8 | 2015-12-11 16:42:24 | [diff] [blame] | 257 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 258 | if (!last_interacted_form_.IsNull() && |
| 259 | (!element || last_interacted_form_ != element->Form())) { |
mathp | 8ac68ce | 2015-12-08 21:49:06 | [diff] [blame] | 260 | // The focused element is not part of the last interacted form (could be |
| 261 | // in a different form). |
leon.han | 90fd63b | 2016-07-26 01:23:55 | [diff] [blame] | 262 | GetAutofillDriver()->FocusNoLongerOnForm(); |
mathp | 8ac68ce | 2015-12-08 21:49:06 | [diff] [blame] | 263 | return; |
| 264 | } |
[email protected] | f920d6e | 2013-03-12 20:20:50 | [diff] [blame] | 265 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 266 | if (!element || !element->IsEnabled() || element->IsReadOnly() || |
| 267 | !element->IsTextField()) |
[email protected] | f920d6e | 2013-03-12 20:20:50 | [diff] [blame] | 268 | return; |
| 269 | |
[email protected] | e5057a2 | 2013-04-22 12:41:39 | [diff] [blame] | 270 | element_ = *element; |
Tao Bai | 168e4fc | 2017-06-19 18:46:38 | [diff] [blame] | 271 | |
| 272 | FormData form; |
| 273 | FormFieldData field; |
| 274 | if (form_util::FindFormAndFieldForFormControlElement(element_, &form, |
| 275 | &field)) { |
| 276 | GetAutofillDriver()->FocusOnFormField( |
| 277 | form, field, |
| 278 | render_frame()->GetRenderView()->ElementBoundsInWindow(element_)); |
| 279 | } |
[email protected] | e5057a2 | 2013-04-22 12:41:39 | [diff] [blame] | 280 | } |
| 281 | |
vabr | d62bc3e | 2016-05-04 15:58:52 | [diff] [blame] | 282 | void AutofillAgent::OnDestruct() { |
| 283 | Shutdown(); |
| 284 | base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, this); |
| 285 | } |
| 286 | |
tmartino | 1c5348c9 | 2016-05-26 18:50:56 | [diff] [blame] | 287 | void AutofillAgent::FireHostSubmitEvents(const WebFormElement& form, |
Tao Bai | a674b1d | 2018-01-18 22:33:43 | [diff] [blame] | 288 | bool known_success, |
| 289 | SubmissionSource source) { |
tmartino | 1c5348c9 | 2016-05-26 18:50:56 | [diff] [blame] | 290 | FormData form_data; |
| 291 | if (!form_util::ExtractFormData(form, &form_data)) |
| 292 | return; |
| 293 | |
Tao Bai | a674b1d | 2018-01-18 22:33:43 | [diff] [blame] | 294 | FireHostSubmitEvents(form_data, known_success, source); |
tmartino | 1c5348c9 | 2016-05-26 18:50:56 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | void AutofillAgent::FireHostSubmitEvents(const FormData& form_data, |
Tao Bai | a674b1d | 2018-01-18 22:33:43 | [diff] [blame] | 298 | bool known_success, |
| 299 | SubmissionSource source) { |
| 300 | // We don't want to fire duplicate submission event. |
| 301 | if (!submitted_forms_.insert(form_data).second) |
| 302 | return; |
| 303 | |
| 304 | GetAutofillDriver()->FormSubmitted(form_data, known_success, source, |
| 305 | base::TimeTicks::Now()); |
tmartino | 1c5348c9 | 2016-05-26 18:50:56 | [diff] [blame] | 306 | } |
| 307 | |
vabr | d62bc3e | 2016-05-04 15:58:52 | [diff] [blame] | 308 | void AutofillAgent::Shutdown() { |
leon.han | 7e8c85b | 2016-09-08 14:24:42 | [diff] [blame] | 309 | binding_.Close(); |
vabr | d62bc3e | 2016-05-04 15:58:52 | [diff] [blame] | 310 | weak_ptr_factory_.InvalidateWeakPtrs(); |
| 311 | } |
| 312 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 313 | void AutofillAgent::TextFieldDidEndEditing(const WebInputElement& element) { |
leon.han | 90fd63b | 2016-07-26 01:23:55 | [diff] [blame] | 314 | GetAutofillDriver()->DidEndTextFieldEditing(); |
Ioana Pandele | 9e742fe | 2018-10-11 19:07:54 | [diff] [blame] | 315 | password_autofill_agent_->DidEndTextFieldEditing(); |
Ioana Pandele | 0c36cd0e4 | 2018-11-05 17:51:31 | [diff] [blame^] | 316 | if (password_generation_agent_) |
| 317 | password_generation_agent_->DidEndTextFieldEditing(element); |
[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; |
mathp | 9172c62a | 2015-10-08 18:51:14 | [diff] [blame] | 347 | if (form_util::FindFormAndFieldForFormControlElement(element, &form, |
| 348 | &field)) { |
Tao Bai | 168e4fc | 2017-06-19 18:46:38 | [diff] [blame] | 349 | GetAutofillDriver()->TextFieldDidChange( |
| 350 | form, field, |
| 351 | render_frame()->GetRenderView()->ElementBoundsInWindow(element), |
| 352 | base::TimeTicks::Now()); |
[email protected] | 1d14f58 | 2011-09-02 20:42:04 | [diff] [blame] | 353 | } |
[email protected] | 676126f7 | 2011-01-15 00:03:51 | [diff] [blame] | 354 | } |
| 355 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 356 | void AutofillAgent::TextFieldDidReceiveKeyDown(const WebInputElement& element, |
[email protected] | 2fa18c2 | 2011-06-14 23:40:43 | [diff] [blame] | 357 | const WebKeyboardEvent& event) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 358 | if (event.windows_key_code == ui::VKEY_DOWN || |
| 359 | event.windows_key_code == ui::VKEY_UP) { |
brettw | b505b7a | 2014-11-26 22:05:32 | [diff] [blame] | 360 | ShowSuggestionsOptions options; |
| 361 | options.autofill_on_empty_values = true; |
| 362 | options.requires_caret_at_end = true; |
Fabio Tirelo | 83662a2 | 2018-07-06 15:03:48 | [diff] [blame] | 363 | options.autoselect_first_suggestion = |
| 364 | ShouldAutoselectFirstSuggestionOnArrowDown(); |
brettw | b505b7a | 2014-11-26 22:05:32 | [diff] [blame] | 365 | ShowSuggestions(element, options); |
| 366 | } |
[email protected] | efeb565f | 2013-12-12 17:16:41 | [diff] [blame] | 367 | } |
| 368 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 369 | void AutofillAgent::OpenTextDataListChooser(const WebInputElement& element) { |
brettw | b505b7a | 2014-11-26 22:05:32 | [diff] [blame] | 370 | ShowSuggestionsOptions options; |
| 371 | options.autofill_on_empty_values = true; |
brettw | b505b7a | 2014-11-26 22:05:32 | [diff] [blame] | 372 | ShowSuggestions(element, options); |
[email protected] | 676126f7 | 2011-01-15 00:03:51 | [diff] [blame] | 373 | } |
| 374 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 375 | void AutofillAgent::DataListOptionsChanged(const WebInputElement& element) { |
| 376 | if (!is_popup_possibly_visible_ || !element.Focused()) |
estade | df5088a | 2014-12-15 02:04:07 | [diff] [blame] | 377 | return; |
| 378 | |
Tao Bai | 6cd41ad | 2017-12-04 18:48:17 | [diff] [blame] | 379 | OnProvisionallySaveForm(WebFormElement(), element, |
| 380 | ElementChangeSource::TEXTFIELD_CHANGED); |
estade | df5088a | 2014-12-15 02:04:07 | [diff] [blame] | 381 | } |
| 382 | |
vasilii | fd0e8ca | 2017-04-12 11:11:22 | [diff] [blame] | 383 | void AutofillAgent::UserGestureObserved() { |
| 384 | password_autofill_agent_->UserGestureObserved(); |
[email protected] | fc22ae5 | 2014-04-23 13:48:04 | [diff] [blame] | 385 | } |
| 386 | |
leon.han | 90fd63b | 2016-07-26 01:23:55 | [diff] [blame] | 387 | void AutofillAgent::DoAcceptDataListSuggestion( |
[email protected] | d5ca8fb | 2013-04-11 17:54:31 | [diff] [blame] | 388 | const base::string16& suggested_value) { |
Hajime Hoshi | e6104dc9 | 2018-03-20 05:58:02 | [diff] [blame] | 389 | if (element_.IsNull()) |
| 390 | return; |
| 391 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 392 | WebInputElement* input_element = ToWebInputElement(&element_); |
[email protected] | e9d29d39 | 2014-03-25 01:15:15 | [diff] [blame] | 393 | DCHECK(input_element); |
[email protected] | d5ca8fb | 2013-04-11 17:54:31 | [diff] [blame] | 394 | base::string16 new_value = suggested_value; |
[email protected] | 71a90b3 | 2012-05-18 09:16:53 | [diff] [blame] | 395 | // If this element takes multiple values then replace the last part with |
| 396 | // the suggestion. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 397 | if (input_element->IsMultiple() && input_element->IsEmailField()) { |
Matt Giuca | 93bc158 | 2017-08-01 05:09:57 | [diff] [blame] | 398 | base::string16 value = input_element->EditingValue().Utf16(); |
| 399 | std::vector<base::StringPiece16> parts = |
| 400 | base::SplitStringPiece(value, base::ASCIIToUTF16(","), |
| 401 | base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL); |
[email protected] | 71a90b3 | 2012-05-18 09:16:53 | [diff] [blame] | 402 | if (parts.size() == 0) |
mgiuca | 30f7588 | 2017-03-28 02:07:19 | [diff] [blame] | 403 | parts.push_back(base::StringPiece16()); |
[email protected] | 71a90b3 | 2012-05-18 09:16:53 | [diff] [blame] | 404 | |
mgiuca | 30f7588 | 2017-03-28 02:07:19 | [diff] [blame] | 405 | base::string16 last_part = parts.back().as_string(); |
[email protected] | 71a90b3 | 2012-05-18 09:16:53 | [diff] [blame] | 406 | // We want to keep just the leading whitespace. |
| 407 | for (size_t i = 0; i < last_part.size(); ++i) { |
brettw | b341306 | 2015-06-24 00:39:02 | [diff] [blame] | 408 | if (!base::IsUnicodeWhitespace(last_part[i])) { |
[email protected] | 71a90b3 | 2012-05-18 09:16:53 | [diff] [blame] | 409 | last_part = last_part.substr(0, i); |
| 410 | break; |
| 411 | } |
| 412 | } |
| 413 | last_part.append(suggested_value); |
thestig | ee1440b | 2016-07-08 01:06:55 | [diff] [blame] | 414 | parts.back() = last_part; |
[email protected] | 71a90b3 | 2012-05-18 09:16:53 | [diff] [blame] | 415 | |
brettw | d94a2214 | 2015-07-15 05:19:26 | [diff] [blame] | 416 | new_value = base::JoinString(parts, base::ASCIIToUTF16(",")); |
[email protected] | 71a90b3 | 2012-05-18 09:16:53 | [diff] [blame] | 417 | } |
leon.han | 90fd63b | 2016-07-26 01:23:55 | [diff] [blame] | 418 | DoFillFieldWithValue(new_value, input_element); |
[email protected] | 63560b3 | 2014-03-04 07:06:26 | [diff] [blame] | 419 | } |
| 420 | |
Parastoo Geranmayeh | 522c222 | 2018-07-16 15:14:03 | [diff] [blame] | 421 | void AutofillAgent::TriggerRefillIfNeeded(const FormData& form) { |
| 422 | if (!base::FeatureList::IsEnabled(features::kAutofillDynamicForms)) |
| 423 | return; |
Parastoo Geranmayeh | 1421e63 | 2018-07-25 15:57:03 | [diff] [blame] | 424 | |
| 425 | ReplaceElementIfNowInvalid(form); |
| 426 | |
Parastoo Geranmayeh | 522c222 | 2018-07-16 15:14:03 | [diff] [blame] | 427 | FormFieldData field; |
| 428 | FormData updated_form; |
| 429 | if (form_util::FindFormAndFieldForFormControlElement(element_, &updated_form, |
| 430 | &field) && |
Parastoo Geranmayeh | 1421e63 | 2018-07-25 15:57:03 | [diff] [blame] | 431 | (!element_.IsAutofilled() || !form.DynamicallySameFormAs(updated_form))) { |
Parastoo Geranmayeh | 522c222 | 2018-07-16 15:14:03 | [diff] [blame] | 432 | base::TimeTicks forms_seen_timestamp = base::TimeTicks::Now(); |
| 433 | WebLocalFrame* frame = render_frame()->GetWebFrame(); |
| 434 | std::vector<FormData> forms; |
| 435 | forms.push_back(updated_form); |
| 436 | // Always communicate to browser process for topmost frame. |
Parastoo Geranmayeh | 1421e63 | 2018-07-25 15:57:03 | [diff] [blame] | 437 | if (!forms.empty() || !frame->Parent()) { |
Parastoo Geranmayeh | 522c222 | 2018-07-16 15:14:03 | [diff] [blame] | 438 | GetAutofillDriver()->FormsSeen(forms, forms_seen_timestamp); |
Parastoo Geranmayeh | 1421e63 | 2018-07-25 15:57:03 | [diff] [blame] | 439 | } |
Parastoo Geranmayeh | 522c222 | 2018-07-16 15:14:03 | [diff] [blame] | 440 | } |
| 441 | } |
| 442 | |
leon.han | de85290 | 2016-05-04 09:16:38 | [diff] [blame] | 443 | // mojom::AutofillAgent: |
leon.han | 90fd63b | 2016-07-26 01:23:55 | [diff] [blame] | 444 | void AutofillAgent::FillForm(int32_t id, const FormData& form) { |
Hajime Hoshi | e6104dc9 | 2018-03-20 05:58:02 | [diff] [blame] | 445 | if (element_.IsNull()) |
| 446 | return; |
| 447 | |
mathp | f43ced4 | 2016-08-04 18:28:23 | [diff] [blame] | 448 | if (id != autofill_query_id_ && id != kNoQueryId) |
leon.han | 90fd63b | 2016-07-26 01:23:55 | [diff] [blame] | 449 | return; |
| 450 | |
sebsg | 248c6b0 | 2018-04-09 20:20:42 | [diff] [blame] | 451 | was_last_action_fill_ = true; |
| 452 | |
sebsg | e35b762c | 2018-03-29 20:33:27 | [diff] [blame] | 453 | if (base::FeatureList::IsEnabled(features::kAutofillDynamicForms)) |
| 454 | ReplaceElementIfNowInvalid(form); |
| 455 | |
Parastoo Geranmayeh | 2dcd14f9 | 2018-06-01 17:08:39 | [diff] [blame] | 456 | query_node_autofill_state_ = element_.GetAutofillState(); |
leon.han | 90fd63b | 2016-07-26 01:23:55 | [diff] [blame] | 457 | form_util::FillForm(form, element_); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 458 | if (!element_.Form().IsNull()) |
Tao Bai | e4d2621 | 2017-12-19 01:30:11 | [diff] [blame] | 459 | UpdateLastInteractedForm(element_.Form()); |
leon.han | 90fd63b | 2016-07-26 01:23:55 | [diff] [blame] | 460 | |
| 461 | GetAutofillDriver()->DidFillAutofillFormData(form, base::TimeTicks::Now()); |
Parastoo Geranmayeh | 522c222 | 2018-07-16 15:14:03 | [diff] [blame] | 462 | |
| 463 | TriggerRefillIfNeeded(form); |
[email protected] | 8527a78f | 2014-06-12 12:49:15 | [diff] [blame] | 464 | } |
| 465 | |
leon.han | 90fd63b | 2016-07-26 01:23:55 | [diff] [blame] | 466 | void AutofillAgent::PreviewForm(int32_t id, const FormData& form) { |
Hajime Hoshi | e6104dc9 | 2018-03-20 05:58:02 | [diff] [blame] | 467 | if (element_.IsNull()) |
| 468 | return; |
| 469 | |
leon.han | 90fd63b | 2016-07-26 01:23:55 | [diff] [blame] | 470 | if (id != autofill_query_id_) |
[email protected] | 63560b3 | 2014-03-04 07:06:26 | [diff] [blame] | 471 | return; |
| 472 | |
Parastoo Geranmayeh | 2dcd14f9 | 2018-06-01 17:08:39 | [diff] [blame] | 473 | query_node_autofill_state_ = element_.GetAutofillState(); |
mathp | 9172c62a | 2015-10-08 18:51:14 | [diff] [blame] | 474 | form_util::PreviewForm(form, element_); |
leon.han | 90fd63b | 2016-07-26 01:23:55 | [diff] [blame] | 475 | |
| 476 | GetAutofillDriver()->DidPreviewAutofillFormData(); |
[email protected] | 6f001a6cf | 2012-02-09 15:21:53 | [diff] [blame] | 477 | } |
| 478 | |
leon.han | 90fd63b | 2016-07-26 01:23:55 | [diff] [blame] | 479 | void AutofillAgent::FieldTypePredictionsAvailable( |
leon.han | 35ae41c | 2016-07-29 03:30:58 | [diff] [blame] | 480 | const std::vector<FormDataPredictions>& forms) { |
Mathieu Perreault | d7b56b49 | 2017-10-18 16:52:17 | [diff] [blame] | 481 | bool attach_predictions_to_dom = |
Mathieu Perreault | a24d0c2 | 2017-11-10 01:22:00 | [diff] [blame] | 482 | base::FeatureList::IsEnabled(features::kAutofillShowTypePredictions); |
leon.han | 90fd63b | 2016-07-26 01:23:55 | [diff] [blame] | 483 | for (const auto& form : forms) { |
Mathieu Perreault | d7b56b49 | 2017-10-18 16:52:17 | [diff] [blame] | 484 | form_cache_.ShowPredictions(form, attach_predictions_to_dom); |
leon.han | 90fd63b | 2016-07-26 01:23:55 | [diff] [blame] | 485 | } |
| 486 | } |
| 487 | |
Parastoo Geranmayeh | 7d8ea06 | 2018-05-15 22:16:25 | [diff] [blame] | 488 | void AutofillAgent::ClearSection() { |
Hajime Hoshi | e6104dc9 | 2018-03-20 05:58:02 | [diff] [blame] | 489 | if (element_.IsNull()) |
| 490 | return; |
| 491 | |
Parastoo Geranmayeh | 7d8ea06 | 2018-05-15 22:16:25 | [diff] [blame] | 492 | form_cache_.ClearSectionWithElement(element_); |
[email protected] | 6f001a6cf | 2012-02-09 15:21:53 | [diff] [blame] | 493 | } |
| 494 | |
leon.han | 90fd63b | 2016-07-26 01:23:55 | [diff] [blame] | 495 | void AutofillAgent::ClearPreviewedForm() { |
Hajime Hoshi | 83c3f50 | 2018-03-14 05:49:01 | [diff] [blame] | 496 | // TODO(crbug.com/816533): It is very rare, but it looks like the |element_| |
| 497 | // can be null if a provisional load was committed immediately prior to |
| 498 | // clearing the previewed form. |
| 499 | if (element_.IsNull()) |
| 500 | return; |
[email protected] | b5b1d69f | 2014-01-23 23:49:32 | [diff] [blame] | 501 | |
Hajime Hoshi | 83c3f50 | 2018-03-14 05:49:01 | [diff] [blame] | 502 | if (password_autofill_agent_->DidClearAutofillSelection(element_)) |
| 503 | return; |
| 504 | |
| 505 | form_util::ClearPreviewedFormWithElement(element_, |
Parastoo Geranmayeh | 2dcd14f9 | 2018-06-01 17:08:39 | [diff] [blame] | 506 | query_node_autofill_state_); |
[email protected] | 6f001a6cf | 2012-02-09 15:21:53 | [diff] [blame] | 507 | } |
| 508 | |
leon.han | 35ae41c | 2016-07-29 03:30:58 | [diff] [blame] | 509 | void AutofillAgent::FillFieldWithValue(const base::string16& value) { |
Hajime Hoshi | e6104dc9 | 2018-03-20 05:58:02 | [diff] [blame] | 510 | if (element_.IsNull()) |
| 511 | return; |
| 512 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 513 | WebInputElement* input_element = ToWebInputElement(&element_); |
ziran.sun | ee0fd443 | 2014-08-27 10:10:49 | [diff] [blame] | 514 | if (input_element) { |
leon.han | 35ae41c | 2016-07-29 03:30:58 | [diff] [blame] | 515 | DoFillFieldWithValue(value, input_element); |
Parastoo Geranmayeh | 2dcd14f9 | 2018-06-01 17:08:39 | [diff] [blame] | 516 | input_element->SetAutofillState(WebAutofillState::kAutofilled); |
ziran.sun | ee0fd443 | 2014-08-27 10:10:49 | [diff] [blame] | 517 | } |
[email protected] | ac9b92f | 2014-03-15 00:48:32 | [diff] [blame] | 518 | } |
| 519 | |
leon.han | 35ae41c | 2016-07-29 03:30:58 | [diff] [blame] | 520 | void AutofillAgent::PreviewFieldWithValue(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_); |
[email protected] | e9d29d39 | 2014-03-25 01:15:15 | [diff] [blame] | 525 | if (input_element) |
leon.han | 35ae41c | 2016-07-29 03:30:58 | [diff] [blame] | 526 | DoPreviewFieldWithValue(value, input_element); |
[email protected] | 6f001a6cf | 2012-02-09 15:21:53 | [diff] [blame] | 527 | } |
| 528 | |
leon.han | 35ae41c | 2016-07-29 03:30:58 | [diff] [blame] | 529 | void AutofillAgent::AcceptDataListSuggestion(const base::string16& value) { |
| 530 | DoAcceptDataListSuggestion(value); |
[email protected] | 5c8de6b9 | 2012-06-08 21:24:08 | [diff] [blame] | 531 | } |
| 532 | |
leon.han | 35ae41c | 2016-07-29 03:30:58 | [diff] [blame] | 533 | void AutofillAgent::FillPasswordSuggestion(const base::string16& username, |
| 534 | const base::string16& password) { |
Hajime Hoshi | e6104dc9 | 2018-03-20 05:58:02 | [diff] [blame] | 535 | if (element_.IsNull()) |
| 536 | return; |
| 537 | |
leon.han | 35ae41c | 2016-07-29 03:30:58 | [diff] [blame] | 538 | bool handled = |
| 539 | password_autofill_agent_->FillSuggestion(element_, username, password); |
[email protected] | 126b1ad | 2014-05-21 22:37:33 | [diff] [blame] | 540 | DCHECK(handled); |
| 541 | } |
| 542 | |
leon.han | 35ae41c | 2016-07-29 03:30:58 | [diff] [blame] | 543 | void AutofillAgent::PreviewPasswordSuggestion(const base::string16& username, |
| 544 | const base::string16& password) { |
Hajime Hoshi | e6104dc9 | 2018-03-20 05:58:02 | [diff] [blame] | 545 | if (element_.IsNull()) |
| 546 | return; |
| 547 | |
kinuko | 0073e7c8 | 2017-01-24 00:42:40 | [diff] [blame] | 548 | bool handled = password_autofill_agent_->PreviewSuggestion( |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 549 | element_, blink::WebString::FromUTF16(username), |
| 550 | blink::WebString::FromUTF16(password)); |
[email protected] | e7e8347 | 2012-04-05 02:56:26 | [diff] [blame] | 551 | DCHECK(handled); |
| 552 | } |
| 553 | |
leon.han | 90fd63b | 2016-07-26 01:23:55 | [diff] [blame] | 554 | void AutofillAgent::ShowInitialPasswordAccountSuggestions( |
jww | fb2a143 | 2016-06-13 22:44:15 | [diff] [blame] | 555 | const PasswordFormFillData& form_data) { |
| 556 | std::vector<blink::WebInputElement> elements; |
| 557 | std::unique_ptr<RendererSavePasswordProgressLogger> logger; |
| 558 | if (password_autofill_agent_->logging_state_active()) { |
leon.han | 97e8f75 | 2016-09-06 06:13:20 | [diff] [blame] | 559 | logger.reset(new RendererSavePasswordProgressLogger( |
| 560 | GetPasswordManagerDriver().get())); |
jww | fb2a143 | 2016-06-13 22:44:15 | [diff] [blame] | 561 | logger->LogMessage(SavePasswordProgressLogger:: |
| 562 | STRING_ON_SHOW_INITIAL_PASSWORD_ACCOUNT_SUGGESTIONS); |
| 563 | } |
| 564 | password_autofill_agent_->GetFillableElementFromFormData( |
Vasilii Sukhanov | c270547 | 2018-09-07 16:23:13 | [diff] [blame] | 565 | form_data, logger.get(), &elements); |
jww | fb2a143 | 2016-06-13 22:44:15 | [diff] [blame] | 566 | |
| 567 | // If wait_for_username is true, we don't want to initially show form options |
| 568 | // until the user types in a valid username. |
| 569 | if (form_data.wait_for_username) |
| 570 | return; |
| 571 | |
| 572 | ShowSuggestionsOptions options; |
| 573 | options.autofill_on_empty_values = true; |
| 574 | options.show_full_suggestion_list = true; |
| 575 | for (auto element : elements) |
| 576 | ShowSuggestions(element, options); |
| 577 | } |
| 578 | |
tmartino | 283dda1 | 2016-06-23 21:43:54 | [diff] [blame] | 579 | bool AutofillAgent::CollectFormlessElements(FormData* output) { |
Tao Bai | 86ac3715 | 2018-03-02 14:35:33 | [diff] [blame] | 580 | if (render_frame() == nullptr || render_frame()->GetWebFrame() == nullptr) |
| 581 | return false; |
| 582 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 583 | WebDocument document = render_frame()->GetWebFrame()->GetDocument(); |
tmartino | 283dda1 | 2016-06-23 21:43:54 | [diff] [blame] | 584 | |
| 585 | // Build up the FormData from the unowned elements. This logic mostly |
| 586 | // mirrors the construction of the synthetic form in form_cache.cc, but |
| 587 | // happens at submit-time so we can capture the modifications the user |
| 588 | // has made, and doesn't depend on form_cache's internal state. |
| 589 | std::vector<WebElement> fieldsets; |
| 590 | std::vector<WebFormControlElement> control_elements = |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 591 | form_util::GetUnownedAutofillableFormFieldElements(document.All(), |
tmartino | 283dda1 | 2016-06-23 21:43:54 | [diff] [blame] | 592 | &fieldsets); |
| 593 | |
| 594 | if (control_elements.size() > form_util::kMaxParseableFields) |
| 595 | return false; |
| 596 | |
| 597 | const form_util::ExtractMask extract_mask = |
| 598 | static_cast<form_util::ExtractMask>(form_util::EXTRACT_VALUE | |
| 599 | form_util::EXTRACT_OPTIONS); |
| 600 | |
| 601 | return form_util::UnownedCheckoutFormElementsAndFieldSetsToFormData( |
| 602 | fieldsets, control_elements, nullptr, document, extract_mask, output, |
| 603 | nullptr); |
mathp | ffc78e2 | 2015-09-01 14:10:50 | [diff] [blame] | 604 | } |
| 605 | |
[email protected] | e9d29d39 | 2014-03-25 01:15:15 | [diff] [blame] | 606 | void AutofillAgent::ShowSuggestions(const WebFormControlElement& element, |
brettw | b505b7a | 2014-11-26 22:05:32 | [diff] [blame] | 607 | const ShowSuggestionsOptions& options) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 608 | if (!element.IsEnabled() || element.IsReadOnly()) |
[email protected] | efeb565f | 2013-12-12 17:16:41 | [diff] [blame] | 609 | return; |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 610 | if (!element.SuggestedValue().IsEmpty()) |
[email protected] | 580c3805 | 2014-07-16 07:28:09 | [diff] [blame] | 611 | return; |
[email protected] | e9d29d39 | 2014-03-25 01:15:15 | [diff] [blame] | 612 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 613 | const WebInputElement* input_element = ToWebInputElement(&element); |
[email protected] | e9d29d39 | 2014-03-25 01:15:15 | [diff] [blame] | 614 | if (input_element) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 615 | if (!input_element->IsTextField()) |
[email protected] | e9d29d39 | 2014-03-25 01:15:15 | [diff] [blame] | 616 | return; |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 617 | if (!input_element->SuggestedValue().IsEmpty()) |
[email protected] | e9d29d39 | 2014-03-25 01:15:15 | [diff] [blame] | 618 | return; |
| 619 | } else { |
mathp | 9172c62a | 2015-10-08 18:51:14 | [diff] [blame] | 620 | DCHECK(form_util::IsTextAreaElement(element)); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 621 | if (!element.ToConst<WebFormControlElement>().SuggestedValue().IsEmpty()) |
[email protected] | e9d29d39 | 2014-03-25 01:15:15 | [diff] [blame] | 622 | return; |
| 623 | } |
[email protected] | e47aec5 | 2010-08-12 00:50:30 | [diff] [blame] | 624 | |
[email protected] | 1c69f8b7 | 2012-03-14 03:18:50 | [diff] [blame] | 625 | // Don't attempt to autofill with values that are too large or if filling |
| 626 | // criteria are not met. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 627 | WebString value = element.EditingValue(); |
mathp | 5f90bce2 | 2015-11-22 20:16:58 | [diff] [blame] | 628 | if (value.length() > kMaxDataLength || |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 629 | (!options.autofill_on_empty_values && value.IsEmpty()) || |
mathp | 5f90bce2 | 2015-11-22 20:16:58 | [diff] [blame] | 630 | (options.requires_caret_at_end && |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 631 | (element.SelectionStart() != element.SelectionEnd() || |
| 632 | element.SelectionEnd() != static_cast<int>(value.length())))) { |
[email protected] | 1c69f8b7 | 2012-03-14 03:18:50 | [diff] [blame] | 633 | // Any popup currently showing is obsolete. |
[email protected] | b648f24 | 2014-02-25 13:49:06 | [diff] [blame] | 634 | HidePopup(); |
[email protected] | e47aec5 | 2010-08-12 00:50:30 | [diff] [blame] | 635 | return; |
[email protected] | 1c69f8b7 | 2012-03-14 03:18:50 | [diff] [blame] | 636 | } |
[email protected] | e47aec5 | 2010-08-12 00:50:30 | [diff] [blame] | 637 | |
[email protected] | bef7f927 | 2012-04-17 10:47:49 | [diff] [blame] | 638 | element_ = element; |
mathp | 9172c62a | 2015-10-08 18:51:14 | [diff] [blame] | 639 | if (form_util::IsAutofillableInputElement(input_element) && |
gcasto | cdd33e0d | 2015-06-17 22:10:05 | [diff] [blame] | 640 | password_autofill_agent_->ShowSuggestions( |
| 641 | *input_element, options.show_full_suggestion_list, |
| 642 | is_generation_popup_possibly_visible_)) { |
[email protected] | b648f24 | 2014-02-25 13:49:06 | [diff] [blame] | 643 | is_popup_possibly_visible_ = true; |
[email protected] | c66ba98 | 2013-04-05 09:56:17 | [diff] [blame] | 644 | return; |
[email protected] | b648f24 | 2014-02-25 13:49:06 | [diff] [blame] | 645 | } |
[email protected] | bef7f927 | 2012-04-17 10:47:49 | [diff] [blame] | 646 | |
gcasto | cdd33e0d | 2015-06-17 22:10:05 | [diff] [blame] | 647 | if (is_generation_popup_possibly_visible_) |
| 648 | return; |
| 649 | |
| 650 | if (options.show_password_suggestions_only) |
| 651 | return; |
| 652 | |
jww | 2035507 | 2014-12-12 21:00:55 | [diff] [blame] | 653 | // Password field elements should only have suggestions shown by the password |
| 654 | // autofill agent. |
Dominic Battre | 1bb521c | 2017-12-19 21:10:03 | [diff] [blame] | 655 | if (input_element && input_element->IsPasswordFieldForAutofill() && |
| 656 | !query_password_suggestion_) { |
jww | 2035507 | 2014-12-12 21:00:55 | [diff] [blame] | 657 | return; |
Dominic Battre | 1bb521c | 2017-12-19 21:10:03 | [diff] [blame] | 658 | } |
jww | 2035507 | 2014-12-12 21:00:55 | [diff] [blame] | 659 | |
Fabio Tirelo | 83662a2 | 2018-07-06 15:03:48 | [diff] [blame] | 660 | QueryAutofillSuggestions(element, options.autoselect_first_suggestion); |
[email protected] | 77bb0da | 2010-11-20 01:55:30 | [diff] [blame] | 661 | } |
| 662 | |
Tao Bai | a8c0d8bb | 2017-12-18 19:47:36 | [diff] [blame] | 663 | void AutofillAgent::SetQueryPasswordSuggestion(bool query) { |
| 664 | query_password_suggestion_ = query; |
| 665 | } |
| 666 | |
Tao Bai | 88d56d4 | 2017-07-17 16:07:16 | [diff] [blame] | 667 | void AutofillAgent::SetSecureContextRequired(bool required) { |
| 668 | is_secure_context_required_ = required; |
| 669 | } |
| 670 | |
Changwan Ryu | eb8a057e | 2017-12-08 19:22:33 | [diff] [blame] | 671 | void AutofillAgent::SetFocusRequiresScroll(bool require) { |
| 672 | focus_requires_scroll_ = require; |
| 673 | } |
| 674 | |
gogerald | d4f184b | 2018-09-19 22:24:24 | [diff] [blame] | 675 | void AutofillAgent::GetElementFormAndFieldData( |
| 676 | const std::vector<std::string>& selectors, |
| 677 | GetElementFormAndFieldDataCallback callback) { |
| 678 | FormData form; |
| 679 | FormFieldData field; |
| 680 | blink::WebElement target_element = FindUniqueWebElement(selectors); |
| 681 | if (target_element.IsNull() || !target_element.IsFormControlElement()) { |
| 682 | return std::move(callback).Run(form, field); |
| 683 | } |
| 684 | |
| 685 | blink::WebFormControlElement target_form_control_element = |
| 686 | target_element.To<blink::WebFormControlElement>(); |
gogerald | 3e2f8a1 | 2018-10-11 21:46:54 | [diff] [blame] | 687 | bool success = form_util::FindFormAndFieldForFormControlElement( |
| 688 | target_form_control_element, &form, &field); |
| 689 | if (success) { |
| 690 | // Remember this element so as to autofill the form without focusing the |
| 691 | // field for Autofill Assistant. |
| 692 | element_ = target_form_control_element; |
| 693 | } |
| 694 | // Do not expect failure. |
| 695 | DCHECK(success); |
| 696 | |
gogerald | d4f184b | 2018-09-19 22:24:24 | [diff] [blame] | 697 | return std::move(callback).Run(form, field); |
| 698 | } |
| 699 | |
| 700 | blink::WebElement AutofillAgent::FindUniqueWebElement( |
| 701 | const std::vector<std::string>& selectors) { |
| 702 | DCHECK(selectors.size() > 0); |
| 703 | |
| 704 | blink::WebVector<blink::WebElement> elements = |
| 705 | render_frame()->GetWebFrame()->GetDocument().QuerySelectorAll( |
| 706 | blink::WebString::FromUTF8(selectors[0])); |
| 707 | if (elements.size() != 1) { |
| 708 | return blink::WebElement(); |
| 709 | } |
| 710 | |
| 711 | // Get the unique element in |elements| and match the next selector inside it |
| 712 | // if there are remaining selectors haven't been matched. |
| 713 | blink::WebElement query_element = elements[0]; |
| 714 | for (size_t i = 1; i < selectors.size(); i++) { |
| 715 | elements = query_element.QuerySelectorAll( |
| 716 | blink::WebString::FromUTF8(selectors[i])); |
| 717 | |
| 718 | // Query shadow DOM if necessary. |
| 719 | if (elements.size() == 0 && !query_element.ShadowRoot().IsNull()) { |
gogerald | d5227511 | 2018-09-21 02:17:22 | [diff] [blame] | 720 | // TODO(806868): Query shadow dom when Autofill is available for forms in |
| 721 | // shadow DOM (crbug.com/746593). |
| 722 | return blink::WebElement(); |
gogerald | d4f184b | 2018-09-19 22:24:24 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | // Return an empty element if there are multiple matching elements. |
| 726 | if (elements.size() != 1) { |
| 727 | return blink::WebElement(); |
| 728 | } |
| 729 | |
| 730 | query_element = elements[0]; |
| 731 | } |
| 732 | |
| 733 | return query_element; |
| 734 | } |
| 735 | |
[email protected] | e9d29d39 | 2014-03-25 01:15:15 | [diff] [blame] | 736 | void AutofillAgent::QueryAutofillSuggestions( |
Fabio Tirelo | 83662a2 | 2018-07-06 15:03:48 | [diff] [blame] | 737 | const WebFormControlElement& element, |
| 738 | bool autoselect_first_suggestion) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 739 | if (!element.GetDocument().GetFrame()) |
[email protected] | e69b5f7 | 2013-01-24 00:59:13 | [diff] [blame] | 740 | return; |
| 741 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 742 | DCHECK(ToWebInputElement(&element) || form_util::IsTextAreaElement(element)); |
[email protected] | e9d29d39 | 2014-03-25 01:15:15 | [diff] [blame] | 743 | |
[email protected] | 77bb0da | 2010-11-20 01:55:30 | [diff] [blame] | 744 | static int query_counter = 0; |
| 745 | autofill_query_id_ = query_counter++; |
estade | 0ee9126 | 2014-10-23 19:29:19 | [diff] [blame] | 746 | |
[email protected] | 1ecbe86 | 2012-10-05 01:29:14 | [diff] [blame] | 747 | FormData form; |
| 748 | FormFieldData field; |
mathp | 9172c62a | 2015-10-08 18:51:14 | [diff] [blame] | 749 | if (!form_util::FindFormAndFieldForFormControlElement(element, &form, |
| 750 | &field)) { |
[email protected] | 7837be6 | 2011-01-18 23:45:08 | [diff] [blame] | 751 | // If we didn't find the cached form, at least let autocomplete have a shot |
| 752 | // at providing suggestions. |
kolos | 66b4c29 | 2016-07-29 17:54:53 | [diff] [blame] | 753 | WebFormControlElementToFormField(element, nullptr, form_util::EXTRACT_VALUE, |
| 754 | &field); |
[email protected] | 7837be6 | 2011-01-18 23:45:08 | [diff] [blame] | 755 | } |
[email protected] | 77bb0da | 2010-11-20 01:55:30 | [diff] [blame] | 756 | |
Tao Bai | 88d56d4 | 2017-07-17 16:07:16 | [diff] [blame] | 757 | if (is_secure_context_required_ && |
Tao Bai | 6b032dd | 2017-09-19 18:44:07 | [diff] [blame] | 758 | !(element.GetDocument().IsSecureContext())) { |
Tao Bai | 88d56d4 | 2017-07-17 16:07:16 | [diff] [blame] | 759 | LOG(WARNING) << "Autofill suggestions are disabled because the document " |
Tao Bai | 6b032dd | 2017-09-19 18:44:07 | [diff] [blame] | 760 | "isn't a secure context."; |
Tao Bai | 88d56d4 | 2017-07-17 16:07:16 | [diff] [blame] | 761 | return; |
| 762 | } |
| 763 | |
[email protected] | 7d0ea536 | 2014-05-01 23:39:43 | [diff] [blame] | 764 | std::vector<base::string16> data_list_values; |
| 765 | std::vector<base::string16> data_list_labels; |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 766 | const WebInputElement* input_element = ToWebInputElement(&element); |
[email protected] | e9d29d39 | 2014-03-25 01:15:15 | [diff] [blame] | 767 | if (input_element) { |
| 768 | // Find the datalist values and send them to the browser process. |
[email protected] | e9d29d39 | 2014-03-25 01:15:15 | [diff] [blame] | 769 | GetDataListSuggestions(*input_element, |
[email protected] | e9d29d39 | 2014-03-25 01:15:15 | [diff] [blame] | 770 | &data_list_values, |
| 771 | &data_list_labels); |
| 772 | TrimStringVectorForIPC(&data_list_values); |
| 773 | TrimStringVectorForIPC(&data_list_labels); |
[email protected] | e9d29d39 | 2014-03-25 01:15:15 | [diff] [blame] | 774 | } |
[email protected] | 5c8de6b9 | 2012-06-08 21:24:08 | [diff] [blame] | 775 | |
[email protected] | b648f24 | 2014-02-25 13:49:06 | [diff] [blame] | 776 | is_popup_possibly_visible_ = true; |
[email protected] | 7d0ea536 | 2014-05-01 23:39:43 | [diff] [blame] | 777 | |
leon.han | 35ae41c | 2016-07-29 03:30:58 | [diff] [blame] | 778 | GetAutofillDriver()->SetDataList(data_list_values, data_list_labels); |
leon.han | 90fd63b | 2016-07-26 01:23:55 | [diff] [blame] | 779 | GetAutofillDriver()->QueryFormFieldAutofill( |
| 780 | autofill_query_id_, form, field, |
Fabio Tirelo | 83662a2 | 2018-07-06 15:03:48 | [diff] [blame] | 781 | render_frame()->GetRenderView()->ElementBoundsInWindow(element_), |
| 782 | autoselect_first_suggestion); |
[email protected] | e47aec5 | 2010-08-12 00:50:30 | [diff] [blame] | 783 | } |
| 784 | |
leon.han | 90fd63b | 2016-07-26 01:23:55 | [diff] [blame] | 785 | void AutofillAgent::DoFillFieldWithValue(const base::string16& value, |
| 786 | WebInputElement* node) { |
Tao Bai | 8148326 | 2018-03-02 05:38:12 | [diff] [blame] | 787 | form_tracker_.set_ignore_control_changes(true); |
sebsg | 778c4369 | 2018-01-24 20:01:32 | [diff] [blame] | 788 | node->SetAutofillValue(blink::WebString::FromUTF16(value)); |
Vadym Doroshenko | a332ebc | 2017-09-14 11:46:53 | [diff] [blame] | 789 | password_autofill_agent_->UpdateStateForTextChange(*node); |
Tao Bai | 8148326 | 2018-03-02 05:38:12 | [diff] [blame] | 790 | form_tracker_.set_ignore_control_changes(false); |
[email protected] | ac9b92f | 2014-03-15 00:48:32 | [diff] [blame] | 791 | } |
| 792 | |
leon.han | 90fd63b | 2016-07-26 01:23:55 | [diff] [blame] | 793 | void AutofillAgent::DoPreviewFieldWithValue(const base::string16& value, |
| 794 | WebInputElement* node) { |
Parastoo Geranmayeh | 2dcd14f9 | 2018-06-01 17:08:39 | [diff] [blame] | 795 | query_node_autofill_state_ = element_.GetAutofillState(); |
sebsg | 778c4369 | 2018-01-24 20:01:32 | [diff] [blame] | 796 | node->SetSuggestedValue(blink::WebString::FromUTF16(value)); |
Parastoo Geranmayeh | 2dcd14f9 | 2018-06-01 17:08:39 | [diff] [blame] | 797 | node->SetAutofillState(WebAutofillState::kPreviewed); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 798 | form_util::PreviewSuggestion(node->SuggestedValue().Utf16(), |
| 799 | node->Value().Utf16(), node); |
[email protected] | 6f001a6cf | 2012-02-09 15:21:53 | [diff] [blame] | 800 | } |
| 801 | |
estade | b1bc9bd | 2014-12-02 22:44:11 | [diff] [blame] | 802 | void AutofillAgent::ProcessForms() { |
[email protected] | 5193ea5 | 2014-05-14 01:10:02 | [diff] [blame] | 803 | // Record timestamp of when the forms are first seen. This is used to |
| 804 | // measure the overhead of the Autofill feature. |
| 805 | base::TimeTicks forms_seen_timestamp = base::TimeTicks::Now(); |
| 806 | |
estade | b1bc9bd | 2014-12-02 22:44:11 | [diff] [blame] | 807 | WebLocalFrame* frame = render_frame()->GetWebFrame(); |
estade | ff02e6d | 2014-12-15 21:18:31 | [diff] [blame] | 808 | std::vector<FormData> forms = form_cache_.ExtractNewForms(); |
[email protected] | 5193ea5 | 2014-05-14 01:10:02 | [diff] [blame] | 809 | |
| 810 | // Always communicate to browser process for topmost frame. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 811 | if (!forms.empty() || !frame->Parent()) { |
leon.han | 35ae41c | 2016-07-29 03:30:58 | [diff] [blame] | 812 | GetAutofillDriver()->FormsSeen(forms, forms_seen_timestamp); |
[email protected] | 5193ea5 | 2014-05-14 01:10:02 | [diff] [blame] | 813 | } |
[email protected] | 5193ea5 | 2014-05-14 01:10:02 | [diff] [blame] | 814 | } |
| 815 | |
[email protected] | b648f24 | 2014-02-25 13:49:06 | [diff] [blame] | 816 | void AutofillAgent::HidePopup() { |
| 817 | if (!is_popup_possibly_visible_) |
| 818 | return; |
[email protected] | b648f24 | 2014-02-25 13:49:06 | [diff] [blame] | 819 | is_popup_possibly_visible_ = false; |
gcasto | cdd33e0d | 2015-06-17 22:10:05 | [diff] [blame] | 820 | is_generation_popup_possibly_visible_ = false; |
leon.han | 90fd63b | 2016-07-26 01:23:55 | [diff] [blame] | 821 | |
| 822 | GetAutofillDriver()->HidePopup(); |
[email protected] | 81cd5233 | 2012-11-05 20:36:07 | [diff] [blame] | 823 | } |
| 824 | |
rouslan | 42b09087 | 2015-05-17 05:04:33 | [diff] [blame] | 825 | bool AutofillAgent::IsUserGesture() const { |
Mustaq Ahmed | e473e435 | 2017-11-04 01:04:25 | [diff] [blame] | 826 | return WebUserGestureIndicator::IsProcessingUserGesture( |
| 827 | render_frame()->GetWebFrame()); |
rouslan | 42b09087 | 2015-05-17 05:04:33 | [diff] [blame] | 828 | } |
| 829 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 830 | void AutofillAgent::DidAssociateFormControlsDynamically() { |
vasilii | 31ba8c6c | 2017-04-28 21:35:08 | [diff] [blame] | 831 | // If the control flow is here than the document was at least loaded. The |
| 832 | // whole page doesn't have to be loaded. |
| 833 | ProcessForms(); |
| 834 | password_autofill_agent_->OnDynamicFormsSeen(); |
| 835 | if (password_generation_agent_) |
| 836 | password_generation_agent_->OnDynamicFormsSeen(); |
[email protected] | 17b6be7 | 2013-04-30 21:33:08 | [diff] [blame] | 837 | } |
| 838 | |
ekaramad | 27ca69b1 | 2017-04-20 18:34:29 | [diff] [blame] | 839 | void AutofillAgent::DidCompleteFocusChangeInFrame() { |
| 840 | WebDocument doc = render_frame()->GetWebFrame()->GetDocument(); |
| 841 | WebElement focused_element; |
| 842 | if (!doc.IsNull()) |
| 843 | focused_element = doc.FocusedElement(); |
| 844 | // PasswordGenerationAgent needs to know about focus changes, even if there is |
| 845 | // no focused element. |
| 846 | if (password_generation_agent_ && |
| 847 | password_generation_agent_->FocusedNodeHasChanged(focused_element)) { |
| 848 | is_generation_popup_possibly_visible_ = true; |
| 849 | is_popup_possibly_visible_ = true; |
| 850 | } |
| 851 | if (!focused_element.IsNull() && password_autofill_agent_) |
| 852 | password_autofill_agent_->FocusedNodeHasChanged(focused_element); |
| 853 | |
Changwan Ryu | eb8a057e | 2017-12-08 19:22:33 | [diff] [blame] | 854 | if (!IsKeyboardAccessoryEnabled() && focus_requires_scroll_) |
Ehsan Karamad | 3538c8a | 2017-11-20 16:31:40 | [diff] [blame] | 855 | HandleFocusChangeComplete(); |
ekaramad | 27ca69b1 | 2017-04-20 18:34:29 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | void AutofillAgent::DidReceiveLeftMouseDownOrGestureTapInNode( |
| 859 | const WebNode& node) { |
Ehsan Karamad | 3538c8a | 2017-11-20 16:31:40 | [diff] [blame] | 860 | DCHECK(!node.IsNull()); |
| 861 | focused_node_was_last_clicked_ = node.Focused(); |
| 862 | |
Changwan Ryu | eb8a057e | 2017-12-08 19:22:33 | [diff] [blame] | 863 | if (IsKeyboardAccessoryEnabled() || !focus_requires_scroll_) |
Ehsan Karamad | 3538c8a | 2017-11-20 16:31:40 | [diff] [blame] | 864 | HandleFocusChangeComplete(); |
| 865 | } |
| 866 | |
Tao Bai | 8148326 | 2018-03-02 05:38:12 | [diff] [blame] | 867 | void AutofillAgent::SelectControlDidChange( |
| 868 | const WebFormControlElement& element) { |
| 869 | form_tracker_.SelectControlDidChange(element); |
| 870 | } |
| 871 | |
sebsg | 248c6b0 | 2018-04-09 20:20:42 | [diff] [blame] | 872 | void AutofillAgent::SelectFieldOptionsChanged( |
| 873 | const blink::WebFormControlElement& element) { |
| 874 | if (!was_last_action_fill_ || element_.IsNull()) |
| 875 | return; |
| 876 | |
sebsg | 1c43685 | 2018-04-18 16:54:32 | [diff] [blame] | 877 | // Since a change of a select options often come in batches, use a timer |
| 878 | // to wait for other changes. Stop the timer if it was already running. It |
| 879 | // will be started again for this change. |
| 880 | if (on_select_update_timer_.IsRunning()) |
| 881 | on_select_update_timer_.AbandonAndStop(); |
| 882 | |
| 883 | // Start the timer to notify the driver that the select field was updated |
| 884 | // after the options have finished changing, |
| 885 | on_select_update_timer_.Start( |
| 886 | FROM_HERE, |
| 887 | base::TimeDelta::FromMilliseconds(kWaitTimeForSelectOptionsChangesMs), |
| 888 | base::BindRepeating(&AutofillAgent::SelectWasUpdated, |
| 889 | weak_ptr_factory_.GetWeakPtr(), element)); |
| 890 | } |
| 891 | |
| 892 | void AutofillAgent::SelectWasUpdated( |
| 893 | const blink::WebFormControlElement& element) { |
| 894 | // Look for the form and field associated with the select element. If they are |
| 895 | // found, notify the driver that the the form was modified dynamically. |
sebsg | 248c6b0 | 2018-04-09 20:20:42 | [diff] [blame] | 896 | FormData form; |
| 897 | FormFieldData field; |
| 898 | if (form_util::FindFormAndFieldForFormControlElement(element, &form, |
| 899 | &field) && |
| 900 | !field.option_values.empty()) { |
sebsg | 1c43685 | 2018-04-18 16:54:32 | [diff] [blame] | 901 | GetAutofillDriver()->SelectFieldOptionsDidChange(form); |
sebsg | 248c6b0 | 2018-04-09 20:20:42 | [diff] [blame] | 902 | } |
| 903 | } |
| 904 | |
Ehsan Karamad | 3538c8a | 2017-11-20 16:31:40 | [diff] [blame] | 905 | void AutofillAgent::FormControlElementClicked( |
| 906 | const WebFormControlElement& element, |
| 907 | bool was_focused) { |
| 908 | last_clicked_form_control_element_for_testing_ = element; |
| 909 | last_clicked_form_control_element_was_focused_for_testing_ = was_focused; |
sebsg | 248c6b0 | 2018-04-09 20:20:42 | [diff] [blame] | 910 | was_last_action_fill_ = false; |
Ehsan Karamad | 3538c8a | 2017-11-20 16:31:40 | [diff] [blame] | 911 | |
| 912 | const WebInputElement* input_element = ToWebInputElement(&element); |
| 913 | if (!input_element && !form_util::IsTextAreaElement(element)) |
| 914 | return; |
| 915 | |
| 916 | ShowSuggestionsOptions options; |
| 917 | options.autofill_on_empty_values = true; |
Roger McFarlane | 3a9c440 | 2018-05-18 18:29:35 | [diff] [blame] | 918 | // Show full suggestions when clicking on an already-focused form field. |
| 919 | options.show_full_suggestion_list = element.IsAutofilled() || was_focused; |
Ehsan Karamad | 3538c8a | 2017-11-20 16:31:40 | [diff] [blame] | 920 | |
| 921 | if (!IsSingleClickEnabled()) { |
Roger McFarlane | 3a9c440 | 2018-05-18 18:29:35 | [diff] [blame] | 922 | // On the initial click (not focused yet), only show password suggestions. |
Ehsan Karamad | 3538c8a | 2017-11-20 16:31:40 | [diff] [blame] | 923 | options.show_password_suggestions_only = !was_focused; |
| 924 | } |
| 925 | ShowSuggestions(element, options); |
| 926 | } |
| 927 | |
| 928 | void AutofillAgent::HandleFocusChangeComplete() { |
| 929 | WebElement focused_element = |
| 930 | render_frame()->GetWebFrame()->GetDocument().FocusedElement(); |
| 931 | |
| 932 | if (focused_node_was_last_clicked_ && !focused_element.IsNull() && |
| 933 | focused_element.IsFormControlElement() && |
| 934 | (form_util::IsTextInput(blink::ToWebInputElement(&focused_element)) || |
| 935 | focused_element.HasHTMLTagName("textarea"))) { |
| 936 | FormControlElementClicked(focused_element.ToConst<WebFormControlElement>(), |
| 937 | was_focused_before_now_); |
| 938 | } |
| 939 | |
| 940 | was_focused_before_now_ = true; |
| 941 | focused_node_was_last_clicked_ = false; |
ekaramad | 27ca69b1 | 2017-04-20 18:34:29 | [diff] [blame] | 942 | } |
| 943 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 944 | void AutofillAgent::AjaxSucceeded() { |
Tao Bai | 6cd41ad | 2017-12-04 18:48:17 | [diff] [blame] | 945 | form_tracker_.AjaxSucceeded(); |
| 946 | } |
| 947 | |
Tao Bai | 8148326 | 2018-03-02 05:38:12 | [diff] [blame] | 948 | void AutofillAgent::OnProvisionallySaveForm( |
| 949 | const WebFormElement& form, |
| 950 | const WebFormControlElement& element, |
| 951 | ElementChangeSource source) { |
Tao Bai | 6cd41ad | 2017-12-04 18:48:17 | [diff] [blame] | 952 | if (source == ElementChangeSource::WILL_SEND_SUBMIT_EVENT) { |
Tao Bai | a674b1d | 2018-01-18 22:33:43 | [diff] [blame] | 953 | // Fire the form submission event to avoid missing submission when web site |
| 954 | // handles the onsubmit event, this also gets the form before Javascript |
| 955 | // could change it. |
| 956 | // We don't clear submitted_forms_ because OnFormSubmitted will normally be |
| 957 | // invoked afterwards and we don't want to fire the same event twice. |
| 958 | FireHostSubmitEvents(form, /*known_success=*/false, |
| 959 | SubmissionSource::FORM_SUBMISSION); |
| 960 | ResetLastInteractedElements(); |
| 961 | return; |
Tao Bai | 8148326 | 2018-03-02 05:38:12 | [diff] [blame] | 962 | } else if (source == ElementChangeSource::TEXTFIELD_CHANGED || |
| 963 | source == ElementChangeSource::SELECT_CHANGED) { |
Tao Bai | a674b1d | 2018-01-18 22:33:43 | [diff] [blame] | 964 | // Remember the last form the user interacted with. |
Tao Bai | 6cd41ad | 2017-12-04 18:48:17 | [diff] [blame] | 965 | if (!element.Form().IsNull()) { |
Tao Bai | e4d2621 | 2017-12-19 01:30:11 | [diff] [blame] | 966 | UpdateLastInteractedForm(element.Form()); |
Tao Bai | 6cd41ad | 2017-12-04 18:48:17 | [diff] [blame] | 967 | } else { |
| 968 | // Remove invisible elements |
| 969 | for (auto it = formless_elements_user_edited_.begin(); |
| 970 | it != formless_elements_user_edited_.end();) { |
| 971 | if (form_util::IsWebElementVisible(*it)) { |
| 972 | it = formless_elements_user_edited_.erase(it); |
| 973 | } else { |
| 974 | ++it; |
| 975 | } |
| 976 | } |
| 977 | formless_elements_user_edited_.insert(element); |
Tao Bai | e4d2621 | 2017-12-19 01:30:11 | [diff] [blame] | 978 | provisionally_saved_form_ = std::make_unique<FormData>(); |
| 979 | if (!CollectFormlessElements(provisionally_saved_form_.get())) { |
| 980 | provisionally_saved_form_.reset(); |
Tao Bai | 6cd41ad | 2017-12-04 18:48:17 | [diff] [blame] | 981 | } else { |
| 982 | last_interacted_form_.Reset(); |
| 983 | } |
| 984 | } |
Tao Bai | 8148326 | 2018-03-02 05:38:12 | [diff] [blame] | 985 | |
| 986 | if (source == ElementChangeSource::TEXTFIELD_CHANGED) |
| 987 | OnTextFieldDidChange(*ToWebInputElement(&element)); |
| 988 | else { |
| 989 | FormData form; |
| 990 | FormFieldData field; |
Tao Bai | ec24a4c2 | 2018-03-14 17:16:08 | [diff] [blame] | 991 | if (form_util::FindFormAndFieldForFormControlElement(element, &form, |
| 992 | &field)) { |
| 993 | GetAutofillDriver()->SelectControlDidChange( |
Tao Bai | 8148326 | 2018-03-02 05:38:12 | [diff] [blame] | 994 | form, field, |
| 995 | render_frame()->GetRenderView()->ElementBoundsInWindow(element)); |
| 996 | } |
| 997 | } |
Tao Bai | 6cd41ad | 2017-12-04 18:48:17 | [diff] [blame] | 998 | } |
| 999 | } |
| 1000 | |
| 1001 | void AutofillAgent::OnProbablyFormSubmitted() { |
Tao Bai | a674b1d | 2018-01-18 22:33:43 | [diff] [blame] | 1002 | FormData form_data; |
| 1003 | if (GetSubmittedForm(&form_data)) { |
| 1004 | FireHostSubmitEvents(form_data, /*known_success=*/false, |
| 1005 | SubmissionSource::PROBABLY_FORM_SUBMITTED); |
| 1006 | } |
Tao Bai | 6cd41ad | 2017-12-04 18:48:17 | [diff] [blame] | 1007 | ResetLastInteractedElements(); |
Tao Bai | a674b1d | 2018-01-18 22:33:43 | [diff] [blame] | 1008 | OnFormNoLongerSubmittable(); |
Tao Bai | 6cd41ad | 2017-12-04 18:48:17 | [diff] [blame] | 1009 | } |
| 1010 | |
| 1011 | void AutofillAgent::OnFormSubmitted(const WebFormElement& form) { |
Tao Bai | a674b1d | 2018-01-18 22:33:43 | [diff] [blame] | 1012 | // Fire the submission event here because WILL_SEND_SUBMIT_EVENT is skipped |
| 1013 | // if javascript calls submit() directly. |
| 1014 | FireHostSubmitEvents(form, /*known_success=*/false, |
| 1015 | SubmissionSource::FORM_SUBMISSION); |
Tao Bai | 6cd41ad | 2017-12-04 18:48:17 | [diff] [blame] | 1016 | ResetLastInteractedElements(); |
Tao Bai | a674b1d | 2018-01-18 22:33:43 | [diff] [blame] | 1017 | OnFormNoLongerSubmittable(); |
Tao Bai | 6cd41ad | 2017-12-04 18:48:17 | [diff] [blame] | 1018 | } |
| 1019 | |
| 1020 | void AutofillAgent::OnInferredFormSubmission(SubmissionSource source) { |
| 1021 | // Only handle iframe for FRAME_DETACHED or main frame for |
| 1022 | // SAME_DOCUMENT_NAVIGATION. |
| 1023 | if ((source == SubmissionSource::FRAME_DETACHED && |
| 1024 | !render_frame()->GetWebFrame()->Parent()) || |
| 1025 | (source == SubmissionSource::SAME_DOCUMENT_NAVIGATION && |
| 1026 | render_frame()->GetWebFrame()->Parent())) { |
Tao Bai | a674b1d | 2018-01-18 22:33:43 | [diff] [blame] | 1027 | ResetLastInteractedElements(); |
| 1028 | OnFormNoLongerSubmittable(); |
Tao Bai | 6cd41ad | 2017-12-04 18:48:17 | [diff] [blame] | 1029 | return; |
| 1030 | } |
| 1031 | |
Tao Bai | 2c1b3d06 | 2017-12-07 17:20:28 | [diff] [blame] | 1032 | if (source == SubmissionSource::FRAME_DETACHED) { |
| 1033 | // Should not access the frame because it is now detached. Instead, use |
Tao Bai | e4d2621 | 2017-12-19 01:30:11 | [diff] [blame] | 1034 | // |provisionally_saved_form_|. |
| 1035 | if (provisionally_saved_form_) |
Tao Bai | a674b1d | 2018-01-18 22:33:43 | [diff] [blame] | 1036 | FireHostSubmitEvents(*provisionally_saved_form_, /*known_success=*/true, |
| 1037 | source); |
Tao Bai | 2c1b3d06 | 2017-12-07 17:20:28 | [diff] [blame] | 1038 | } else { |
| 1039 | FormData form_data; |
Tao Bai | e4d2621 | 2017-12-19 01:30:11 | [diff] [blame] | 1040 | if (GetSubmittedForm(&form_data)) |
Tao Bai | a674b1d | 2018-01-18 22:33:43 | [diff] [blame] | 1041 | FireHostSubmitEvents(form_data, /*known_success=*/true, source); |
Tao Bai | 6cd41ad | 2017-12-04 18:48:17 | [diff] [blame] | 1042 | } |
| 1043 | ResetLastInteractedElements(); |
Tao Bai | a674b1d | 2018-01-18 22:33:43 | [diff] [blame] | 1044 | OnFormNoLongerSubmittable(); |
Tao Bai | 6cd41ad | 2017-12-04 18:48:17 | [diff] [blame] | 1045 | } |
| 1046 | |
| 1047 | void AutofillAgent::AddFormObserver(Observer* observer) { |
| 1048 | form_tracker_.AddObserver(observer); |
| 1049 | } |
| 1050 | |
| 1051 | void AutofillAgent::RemoveFormObserver(Observer* observer) { |
| 1052 | form_tracker_.RemoveObserver(observer); |
| 1053 | } |
| 1054 | |
| 1055 | bool AutofillAgent::GetSubmittedForm(FormData* form) { |
| 1056 | if (!last_interacted_form_.IsNull()) { |
Tao Bai | e4d2621 | 2017-12-19 01:30:11 | [diff] [blame] | 1057 | if (form_util::ExtractFormData(last_interacted_form_, form)) { |
| 1058 | return true; |
| 1059 | } else if (provisionally_saved_form_) { |
| 1060 | *form = *provisionally_saved_form_; |
| 1061 | return true; |
| 1062 | } |
Tao Bai | 6cd41ad | 2017-12-04 18:48:17 | [diff] [blame] | 1063 | } else if (formless_elements_user_edited_.size() != 0 && |
| 1064 | !form_util::IsSomeControlElementVisible( |
| 1065 | formless_elements_user_edited_)) { |
| 1066 | // we check if all the elements the user has interacted with are gone, |
Tao Bai | e4d2621 | 2017-12-19 01:30:11 | [diff] [blame] | 1067 | // to decide if submission has occurred, and use the |
| 1068 | // provisionally_saved_form_ saved in OnProvisionallySaveForm() if fail to |
| 1069 | // construct form. |
Tao Bai | 6cd41ad | 2017-12-04 18:48:17 | [diff] [blame] | 1070 | if (CollectFormlessElements(form)) { |
| 1071 | return true; |
Tao Bai | e4d2621 | 2017-12-19 01:30:11 | [diff] [blame] | 1072 | } else if (provisionally_saved_form_) { |
| 1073 | *form = *provisionally_saved_form_; |
Tao Bai | 6cd41ad | 2017-12-04 18:48:17 | [diff] [blame] | 1074 | return true; |
| 1075 | } |
| 1076 | } |
| 1077 | return false; |
| 1078 | } |
| 1079 | |
| 1080 | void AutofillAgent::ResetLastInteractedElements() { |
| 1081 | last_interacted_form_.Reset(); |
Alexander Timokhin | 3c45d1a | 2018-03-28 17:49:28 | [diff] [blame] | 1082 | last_clicked_form_control_element_for_testing_.Reset(); |
Tao Bai | 6cd41ad | 2017-12-04 18:48:17 | [diff] [blame] | 1083 | formless_elements_user_edited_.clear(); |
Tao Bai | e4d2621 | 2017-12-19 01:30:11 | [diff] [blame] | 1084 | provisionally_saved_form_.reset(); |
| 1085 | } |
| 1086 | |
| 1087 | void AutofillAgent::UpdateLastInteractedForm(blink::WebFormElement form) { |
| 1088 | last_interacted_form_ = form; |
| 1089 | provisionally_saved_form_ = std::make_unique<FormData>(); |
| 1090 | if (!form_util::ExtractFormData(last_interacted_form_, |
| 1091 | provisionally_saved_form_.get())) { |
| 1092 | provisionally_saved_form_.reset(); |
| 1093 | } |
gcasto | c21f1c4c | 2015-03-19 21:57:03 | [diff] [blame] | 1094 | } |
| 1095 | |
Tao Bai | a674b1d | 2018-01-18 22:33:43 | [diff] [blame] | 1096 | void AutofillAgent::OnFormNoLongerSubmittable() { |
| 1097 | submitted_forms_.clear(); |
| 1098 | } |
| 1099 | |
Parastoo Geranmayeh | 1421e63 | 2018-07-25 15:57:03 | [diff] [blame] | 1100 | bool AutofillAgent::FindTheUniqueNewVersionOfOldElement( |
| 1101 | WebVector<WebFormControlElement>& elements, |
| 1102 | bool& element_found, |
| 1103 | const WebString& original_element_section, |
| 1104 | const WebFormControlElement& original_element) { |
| 1105 | for (const WebFormControlElement& current_element : elements) { |
| 1106 | if (current_element.IsFocusable() && |
| 1107 | original_element.NameForAutofill() == |
| 1108 | current_element.NameForAutofill()) { |
| 1109 | if (!element_found) { |
| 1110 | element_ = current_element; |
| 1111 | element_found = true; |
| 1112 | } else if (current_element.AutofillSection() == |
| 1113 | element_.AutofillSection() || |
| 1114 | (current_element.AutofillSection() != |
| 1115 | original_element_section && |
| 1116 | element_.AutofillSection() != original_element_section)) { |
| 1117 | // If there are two elements that share the same name with the element_, |
| 1118 | // and the section can't tell them apart, we can't decide between the |
| 1119 | // two. |
| 1120 | element_ = original_element; |
| 1121 | return false; |
| 1122 | } else if (current_element.AutofillSection() == |
| 1123 | original_element_section) { |
| 1124 | // If the current element has the right section, update the element_. |
| 1125 | element_ = current_element; |
| 1126 | } |
| 1127 | } |
| 1128 | } |
| 1129 | return true; |
| 1130 | } |
| 1131 | |
sebsg | e35b762c | 2018-03-29 20:33:27 | [diff] [blame] | 1132 | void AutofillAgent::ReplaceElementIfNowInvalid(const FormData& original_form) { |
| 1133 | // If the document is invalid, bail out. |
| 1134 | if (element_.GetDocument().IsNull()) |
| 1135 | return; |
| 1136 | |
Parastoo Geranmayeh | 1421e63 | 2018-07-25 15:57:03 | [diff] [blame] | 1137 | WebVector<WebFormElement> forms; |
| 1138 | WebVector<WebFormControlElement> elements; |
| 1139 | |
| 1140 | if (original_form.name.empty()) { |
| 1141 | // If the form has no name, check all the forms. |
| 1142 | bool element_found = false; |
| 1143 | element_.GetDocument().Forms(forms); |
| 1144 | for (const WebFormElement& form : forms) { |
| 1145 | form.GetFormControlElements(elements); |
| 1146 | // If finding a unique element is impossible, return. |
| 1147 | if (!FindTheUniqueNewVersionOfOldElement( |
| 1148 | elements, element_found, element_.AutofillSection(), element_)) |
| 1149 | return; |
| 1150 | } |
| 1151 | // If the element is not found, we should still check for unowned elements. |
| 1152 | if (element_found) |
| 1153 | return; |
| 1154 | } |
| 1155 | |
sebsg | e35b762c | 2018-03-29 20:33:27 | [diff] [blame] | 1156 | if (!element_.Form().IsNull()) { |
| 1157 | // If |element_|'s parent form has no elements, |element_| is now invalid |
| 1158 | // and should be updated. |
| 1159 | WebVector<WebFormControlElement> form_elements; |
| 1160 | element_.Form().GetFormControlElements(form_elements); |
| 1161 | if (!form_elements.empty()) |
| 1162 | return; |
| 1163 | } |
| 1164 | |
sebsg | e35b762c | 2018-03-29 20:33:27 | [diff] [blame] | 1165 | WebFormElement form_element; |
Parastoo Geranmayeh | 1421e63 | 2018-07-25 15:57:03 | [diff] [blame] | 1166 | if (!original_form.name.empty()) { |
| 1167 | // Try to find the new version of the form. |
| 1168 | element_.GetDocument().Forms(forms); |
| 1169 | for (const WebFormElement& form : forms) { |
| 1170 | if (original_form.name == form.GetName().Utf16() || |
| 1171 | original_form.name == form.GetAttribute("id").Utf16()) { |
| 1172 | form_element = form; |
| 1173 | break; |
| 1174 | } |
sebsg | e35b762c | 2018-03-29 20:33:27 | [diff] [blame] | 1175 | } |
| 1176 | } |
| 1177 | |
sebsg | f32e98d7 | 2018-05-11 14:43:01 | [diff] [blame] | 1178 | if (form_element.IsNull()) { |
| 1179 | // Could not find the new version of the form, get all the unowned elements. |
| 1180 | std::vector<WebElement> fieldsets; |
| 1181 | elements = form_util::GetUnownedAutofillableFormFieldElements( |
| 1182 | element_.GetDocument().All(), &fieldsets); |
Parastoo Geranmayeh | 1421e63 | 2018-07-25 15:57:03 | [diff] [blame] | 1183 | bool element_found = false; |
| 1184 | FindTheUniqueNewVersionOfOldElement(elements, element_found, |
| 1185 | element_.AutofillSection(), element_); |
| 1186 | return; |
sebsg | f32e98d7 | 2018-05-11 14:43:01 | [diff] [blame] | 1187 | } |
Parastoo Geranmayeh | 1421e63 | 2018-07-25 15:57:03 | [diff] [blame] | 1188 | // This is the case for owned fields that belong to the right named form. |
| 1189 | // Get all the elements of the new version of the form. |
| 1190 | form_element.GetFormControlElements(elements); |
sebsg | e35b762c | 2018-03-29 20:33:27 | [diff] [blame] | 1191 | // Try to find the new version of the last interacted element. |
sebsg | e35b762c | 2018-03-29 20:33:27 | [diff] [blame] | 1192 | for (const WebFormControlElement& element : elements) { |
| 1193 | if (element_.NameForAutofill() == element.NameForAutofill()) { |
| 1194 | element_ = element; |
| 1195 | return; |
| 1196 | } |
| 1197 | } |
| 1198 | } |
| 1199 | |
Ken Rockot | 0aad7ec | 2018-07-31 15:58:44 | [diff] [blame] | 1200 | const mojom::AutofillDriverAssociatedPtr& AutofillAgent::GetAutofillDriver() { |
leon.han | 97e8f75 | 2016-09-06 06:13:20 | [diff] [blame] | 1201 | if (!autofill_driver_) { |
Ken Rockot | 0aad7ec | 2018-07-31 15:58:44 | [diff] [blame] | 1202 | render_frame()->GetRemoteAssociatedInterfaces()->GetInterface( |
blundell | e0a9f158 | 2016-12-20 11:23:32 | [diff] [blame] | 1203 | mojo::MakeRequest(&autofill_driver_)); |
leon.han | 90fd63b | 2016-07-26 01:23:55 | [diff] [blame] | 1204 | } |
leon.han | de85290 | 2016-05-04 09:16:38 | [diff] [blame] | 1205 | |
leon.han | 97e8f75 | 2016-09-06 06:13:20 | [diff] [blame] | 1206 | return autofill_driver_; |
| 1207 | } |
| 1208 | |
Friedrich Horschig | 9c89633 | 2018-07-16 16:09:07 | [diff] [blame] | 1209 | const mojom::PasswordManagerDriverAssociatedPtr& |
leon.han | 97e8f75 | 2016-09-06 06:13:20 | [diff] [blame] | 1210 | AutofillAgent::GetPasswordManagerDriver() { |
| 1211 | DCHECK(password_autofill_agent_); |
| 1212 | return password_autofill_agent_->GetPasswordManagerDriver(); |
leon.han | de85290 | 2016-05-04 09:16:38 | [diff] [blame] | 1213 | } |
| 1214 | |
[email protected] | 7819208 | 2011-01-29 05:43:44 | [diff] [blame] | 1215 | } // namespace autofill |