[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 | |
[email protected] | a1cb57f | 2011-09-28 22:41:29 | [diff] [blame] | 7 | #include "base/bind.h" |
[email protected] | f920d6e | 2013-03-12 20:20:50 | [diff] [blame] | 8 | #include "base/command_line.h" |
[email protected] | c50a693 | 2013-07-17 22:16:13 | [diff] [blame] | 9 | #include "base/message_loop/message_loop.h" |
[email protected] | 1988e1c | 2013-02-28 20:27:42 | [diff] [blame] | 10 | #include "base/strings/string_split.h" |
[email protected] | c72674b | 2013-06-11 04:16:43 | [diff] [blame] | 11 | #include "base/strings/string_util.h" |
[email protected] | d2d79d5 | 2013-06-07 22:23:48 | [diff] [blame] | 12 | #include "base/strings/utf_string_conversions.h" |
[email protected] | bbd8da9 | 2013-06-28 02:12:20 | [diff] [blame] | 13 | #include "base/time/time.h" |
[email protected] | 45b53fb | 2013-12-12 19:28:06 | [diff] [blame] | 14 | #include "components/autofill/content/common/autofill_messages.h" |
[email protected] | f07e444 | 2013-06-06 23:03:48 | [diff] [blame] | 15 | #include "components/autofill/content/renderer/form_autofill_util.h" |
| 16 | #include "components/autofill/content/renderer/page_click_tracker.h" |
| 17 | #include "components/autofill/content/renderer/password_autofill_agent.h" |
[email protected] | 3cbdf936 | 2014-01-31 23:12:23 | [diff] [blame] | 18 | #include "components/autofill/content/renderer/password_generation_agent.h" |
[email protected] | d04f8191 | 2013-06-18 14:52:13 | [diff] [blame] | 19 | #include "components/autofill/core/common/autofill_constants.h" |
[email protected] | d86263dcd | 2014-01-09 10:35:21 | [diff] [blame] | 20 | #include "components/autofill/core/common/autofill_data_validation.h" |
[email protected] | d04f8191 | 2013-06-18 14:52:13 | [diff] [blame] | 21 | #include "components/autofill/core/common/autofill_switches.h" |
| 22 | #include "components/autofill/core/common/form_data.h" |
| 23 | #include "components/autofill/core/common/form_data_predictions.h" |
| 24 | #include "components/autofill/core/common/form_field_data.h" |
[email protected] | e620d36 | 2013-09-09 08:01:53 | [diff] [blame] | 25 | #include "components/autofill/core/common/password_form.h" |
[email protected] | d04f8191 | 2013-06-18 14:52:13 | [diff] [blame] | 26 | #include "components/autofill/core/common/web_element_descriptor.h" |
[email protected] | fed9d4f | 2013-12-17 21:32:47 | [diff] [blame] | 27 | #include "content/public/common/content_switches.h" |
[email protected] | e449521 | 2012-12-06 03:09:12 | [diff] [blame] | 28 | #include "content/public/common/ssl_status.h" |
[email protected] | ca5190f | 2013-07-08 11:10:31 | [diff] [blame] | 29 | #include "content/public/common/url_constants.h" |
[email protected] | a2ef54c | 2011-10-10 16:20:31 | [diff] [blame] | 30 | #include "content/public/renderer/render_view.h" |
[email protected] | ca5190f | 2013-07-08 11:10:31 | [diff] [blame] | 31 | #include "net/cert/cert_status_flags.h" |
[email protected] | f07e444 | 2013-06-06 23:03:48 | [diff] [blame] | 32 | #include "third_party/WebKit/public/platform/WebRect.h" |
| 33 | #include "third_party/WebKit/public/platform/WebURLRequest.h" |
[email protected] | dbdd6027 | 2014-04-14 22:48:40 | [diff] [blame] | 34 | #include "third_party/WebKit/public/web/WebConsoleMessage.h" |
[email protected] | a0962a9 | 2013-06-20 18:27:34 | [diff] [blame] | 35 | #include "third_party/WebKit/public/web/WebDataSource.h" |
| 36 | #include "third_party/WebKit/public/web/WebDocument.h" |
[email protected] | 961783a | 2014-02-07 18:38:05 | [diff] [blame] | 37 | #include "third_party/WebKit/public/web/WebElementCollection.h" |
[email protected] | a0962a9 | 2013-06-20 18:27:34 | [diff] [blame] | 38 | #include "third_party/WebKit/public/web/WebFormControlElement.h" |
| 39 | #include "third_party/WebKit/public/web/WebFormElement.h" |
[email protected] | a0962a9 | 2013-06-20 18:27:34 | [diff] [blame] | 40 | #include "third_party/WebKit/public/web/WebInputEvent.h" |
[email protected] | 8050465 | 2014-04-18 04:41:50 | [diff] [blame] | 41 | #include "third_party/WebKit/public/web/WebLocalFrame.h" |
[email protected] | a0962a9 | 2013-06-20 18:27:34 | [diff] [blame] | 42 | #include "third_party/WebKit/public/web/WebNode.h" |
[email protected] | a0962a9 | 2013-06-20 18:27:34 | [diff] [blame] | 43 | #include "third_party/WebKit/public/web/WebOptionElement.h" |
[email protected] | e9d29d39 | 2014-03-25 01:15:15 | [diff] [blame] | 44 | #include "third_party/WebKit/public/web/WebTextAreaElement.h" |
[email protected] | a0962a9 | 2013-06-20 18:27:34 | [diff] [blame] | 45 | #include "third_party/WebKit/public/web/WebView.h" |
[email protected] | c051a1b | 2011-01-21 23:30:17 | [diff] [blame] | 46 | #include "ui/base/l10n/l10n_util.h" |
[email protected] | 7e9acd08 | 2013-09-17 23:31:16 | [diff] [blame] | 47 | #include "ui/events/keycodes/keyboard_codes.h" |
[email protected] | 679f128f | 2010-07-22 22:57:44 | [diff] [blame] | 48 | |
[email protected] | a1221aea | 2013-11-07 01:31:30 | [diff] [blame] | 49 | using blink::WebAutofillClient; |
[email protected] | dbdd6027 | 2014-04-14 22:48:40 | [diff] [blame] | 50 | using blink::WebConsoleMessage; |
[email protected] | 5e7e861 | 2014-03-20 14:43:19 | [diff] [blame] | 51 | using blink::WebElement; |
| 52 | using blink::WebElementCollection; |
[email protected] | a1221aea | 2013-11-07 01:31:30 | [diff] [blame] | 53 | using blink::WebFormControlElement; |
| 54 | using blink::WebFormElement; |
| 55 | using blink::WebFrame; |
| 56 | using blink::WebInputElement; |
| 57 | using blink::WebKeyboardEvent; |
[email protected] | c5041c32 | 2014-04-08 05:06:47 | [diff] [blame] | 58 | using blink::WebLocalFrame; |
[email protected] | a1221aea | 2013-11-07 01:31:30 | [diff] [blame] | 59 | using blink::WebNode; |
[email protected] | a1221aea | 2013-11-07 01:31:30 | [diff] [blame] | 60 | using blink::WebOptionElement; |
| 61 | using blink::WebString; |
[email protected] | e9d29d39 | 2014-03-25 01:15:15 | [diff] [blame] | 62 | using blink::WebTextAreaElement; |
[email protected] | 5e7e861 | 2014-03-20 14:43:19 | [diff] [blame] | 63 | using blink::WebVector; |
[email protected] | 679f128f | 2010-07-22 22:57:44 | [diff] [blame] | 64 | |
[email protected] | d86263dcd | 2014-01-09 10:35:21 | [diff] [blame] | 65 | namespace autofill { |
| 66 | |
[email protected] | e47aec5 | 2010-08-12 00:50:30 | [diff] [blame] | 67 | namespace { |
| 68 | |
[email protected] | 45a0794 | 2013-07-26 08:28:21 | [diff] [blame] | 69 | // Gets all the data list values (with corresponding label) for the given |
| 70 | // element. |
[email protected] | 5e7e861 | 2014-03-20 14:43:19 | [diff] [blame] | 71 | void GetDataListSuggestions(const WebInputElement& element, |
[email protected] | efeb565f | 2013-12-12 17:16:41 | [diff] [blame] | 72 | bool ignore_current_value, |
[email protected] | 45a0794 | 2013-07-26 08:28:21 | [diff] [blame] | 73 | std::vector<base::string16>* values, |
| 74 | std::vector<base::string16>* labels) { |
[email protected] | 961783a | 2014-02-07 18:38:05 | [diff] [blame] | 75 | WebElementCollection options = element.dataListOptions(); |
[email protected] | bef7f927 | 2012-04-17 10:47:49 | [diff] [blame] | 76 | if (options.isNull()) |
| 77 | return; |
| 78 | |
[email protected] | efeb565f | 2013-12-12 17:16:41 | [diff] [blame] | 79 | base::string16 prefix; |
| 80 | if (!ignore_current_value) { |
| 81 | prefix = element.editingValue(); |
[email protected] | 7bc4453 | 2014-07-10 16:02:15 | [diff] [blame] | 82 | if (element.isMultiple() && element.isEmailField()) { |
[email protected] | efeb565f | 2013-12-12 17:16:41 | [diff] [blame] | 83 | std::vector<base::string16> parts; |
| 84 | base::SplitStringDontTrim(prefix, ',', &parts); |
[email protected] | 8af69c6c | 2014-03-03 19:05:31 | [diff] [blame] | 85 | if (parts.size() > 0) { |
| 86 | base::TrimWhitespace(parts[parts.size() - 1], base::TRIM_LEADING, |
| 87 | &prefix); |
| 88 | } |
[email protected] | efeb565f | 2013-12-12 17:16:41 | [diff] [blame] | 89 | } |
[email protected] | 71a90b3 | 2012-05-18 09:16:53 | [diff] [blame] | 90 | } |
[email protected] | bef7f927 | 2012-04-17 10:47:49 | [diff] [blame] | 91 | for (WebOptionElement option = options.firstItem().to<WebOptionElement>(); |
[email protected] | 71a90b3 | 2012-05-18 09:16:53 | [diff] [blame] | 92 | !option.isNull(); option = options.nextItem().to<WebOptionElement>()) { |
| 93 | if (!StartsWith(option.value(), prefix, false) || |
| 94 | option.value() == prefix || |
[email protected] | 3f81263 | 2012-05-16 04:13:36 | [diff] [blame] | 95 | !element.isValidValue(option.value())) |
[email protected] | bef7f927 | 2012-04-17 10:47:49 | [diff] [blame] | 96 | continue; |
| 97 | |
| 98 | values->push_back(option.value()); |
| 99 | if (option.value() != option.label()) |
| 100 | labels->push_back(option.label()); |
| 101 | else |
[email protected] | d5ca8fb | 2013-04-11 17:54:31 | [diff] [blame] | 102 | labels->push_back(base::string16()); |
[email protected] | bef7f927 | 2012-04-17 10:47:49 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
[email protected] | ead7fb0 | 2013-07-18 18:50:12 | [diff] [blame] | 106 | // Trim the vector before sending it to the browser process to ensure we |
[email protected] | 5c8de6b9 | 2012-06-08 21:24:08 | [diff] [blame] | 107 | // don't send too much data through the IPC. |
[email protected] | ead7fb0 | 2013-07-18 18:50:12 | [diff] [blame] | 108 | void TrimStringVectorForIPC(std::vector<base::string16>* strings) { |
| 109 | // Limit the size of the vector. |
[email protected] | d86263dcd | 2014-01-09 10:35:21 | [diff] [blame] | 110 | if (strings->size() > kMaxListSize) |
| 111 | strings->resize(kMaxListSize); |
[email protected] | 5c8de6b9 | 2012-06-08 21:24:08 | [diff] [blame] | 112 | |
[email protected] | ead7fb0 | 2013-07-18 18:50:12 | [diff] [blame] | 113 | // Limit the size of the strings in the vector. |
| 114 | for (size_t i = 0; i < strings->size(); ++i) { |
[email protected] | d86263dcd | 2014-01-09 10:35:21 | [diff] [blame] | 115 | if ((*strings)[i].length() > kMaxDataLength) |
| 116 | (*strings)[i].resize(kMaxDataLength); |
[email protected] | 5c8de6b9 | 2012-06-08 21:24:08 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
[email protected] | e47aec5 | 2010-08-12 00:50:30 | [diff] [blame] | 120 | } // namespace |
| 121 | |
[email protected] | ad19b30 | 2013-04-03 07:42:19 | [diff] [blame] | 122 | AutofillAgent::AutofillAgent(content::RenderView* render_view, |
[email protected] | 3cbdf936 | 2014-01-31 23:12:23 | [diff] [blame] | 123 | PasswordAutofillAgent* password_autofill_agent, |
| 124 | PasswordGenerationAgent* password_generation_agent) |
[email protected] | 3a034ebb | 2011-10-03 19:19:44 | [diff] [blame] | 125 | : content::RenderViewObserver(render_view), |
[email protected] | ad19b30 | 2013-04-03 07:42:19 | [diff] [blame] | 126 | password_autofill_agent_(password_autofill_agent), |
[email protected] | 3cbdf936 | 2014-01-31 23:12:23 | [diff] [blame] | 127 | password_generation_agent_(password_generation_agent), |
[email protected] | 679f128f | 2010-07-22 22:57:44 | [diff] [blame] | 128 | autofill_query_id_(0), |
[email protected] | 73a087f | 2013-02-06 07:02:06 | [diff] [blame] | 129 | web_view_(render_view->GetWebView()), |
[email protected] | 6bcd58a | 2010-11-25 02:31:20 | [diff] [blame] | 130 | display_warning_if_disabled_(false), |
[email protected] | d77ddc806 | 2010-11-24 01:14:06 | [diff] [blame] | 131 | was_query_node_autofilled_(false), |
[email protected] | e4dce0af | 2011-09-19 22:22:28 | [diff] [blame] | 132 | has_shown_autofill_popup_for_current_edit_(false), |
[email protected] | b37043c | 2012-05-31 17:08:12 | [diff] [blame] | 133 | did_set_node_text_(false), |
[email protected] | 90f2625 | 2013-02-15 19:48:32 | [diff] [blame] | 134 | ignore_text_changes_(false), |
[email protected] | b648f24 | 2014-02-25 13:49:06 | [diff] [blame] | 135 | is_popup_possibly_visible_(false), |
[email protected] | 5193ea5 | 2014-05-14 01:10:02 | [diff] [blame] | 136 | main_frame_processed_(false), |
[email protected] | 2c30525 | 2013-04-26 19:57:05 | [diff] [blame] | 137 | weak_ptr_factory_(this) { |
[email protected] | a2ef54c | 2011-10-10 16:20:31 | [diff] [blame] | 138 | render_view->GetWebView()->setAutofillClient(this); |
[email protected] | 03f10dad | 2013-04-13 08:23:05 | [diff] [blame] | 139 | |
| 140 | // The PageClickTracker is a RenderViewObserver, and hence will be freed when |
| 141 | // the RenderView is destroyed. |
| 142 | new PageClickTracker(render_view, this); |
[email protected] | 679f128f | 2010-07-22 22:57:44 | [diff] [blame] | 143 | } |
| 144 | |
[email protected] | a64fdc3 | 2013-07-31 09:21:13 | [diff] [blame] | 145 | AutofillAgent::~AutofillAgent() {} |
[email protected] | d2f05d0 | 2011-01-27 18:51:01 | [diff] [blame] | 146 | |
[email protected] | 663bd9e | 2011-03-21 01:07:01 | [diff] [blame] | 147 | bool AutofillAgent::OnMessageReceived(const IPC::Message& message) { |
[email protected] | 676126f7 | 2011-01-15 00:03:51 | [diff] [blame] | 148 | bool handled = true; |
[email protected] | 663bd9e | 2011-03-21 01:07:01 | [diff] [blame] | 149 | IPC_BEGIN_MESSAGE_MAP(AutofillAgent, message) |
[email protected] | 8527a78f | 2014-06-12 12:49:15 | [diff] [blame] | 150 | IPC_MESSAGE_HANDLER(AutofillMsg_Ping, OnPing) |
[email protected] | 63560b3 | 2014-03-04 07:06:26 | [diff] [blame] | 151 | IPC_MESSAGE_HANDLER(AutofillMsg_FillForm, OnFillForm) |
| 152 | IPC_MESSAGE_HANDLER(AutofillMsg_PreviewForm, OnPreviewForm) |
[email protected] | 3eb5728c | 2011-06-20 22:32:24 | [diff] [blame] | 153 | IPC_MESSAGE_HANDLER(AutofillMsg_FieldTypePredictionsAvailable, |
| 154 | OnFieldTypePredictionsAvailable) |
[email protected] | 93f2e50 | 2013-12-20 09:47:25 | [diff] [blame] | 155 | IPC_MESSAGE_HANDLER(AutofillMsg_ClearForm, OnClearForm) |
[email protected] | 93f2e50 | 2013-12-20 09:47:25 | [diff] [blame] | 156 | IPC_MESSAGE_HANDLER(AutofillMsg_ClearPreviewedForm, OnClearPreviewedForm) |
[email protected] | ac9b92f | 2014-03-15 00:48:32 | [diff] [blame] | 157 | IPC_MESSAGE_HANDLER(AutofillMsg_FillFieldWithValue, OnFillFieldWithValue) |
| 158 | IPC_MESSAGE_HANDLER(AutofillMsg_PreviewFieldWithValue, |
| 159 | OnPreviewFieldWithValue) |
[email protected] | 5c8de6b9 | 2012-06-08 21:24:08 | [diff] [blame] | 160 | IPC_MESSAGE_HANDLER(AutofillMsg_AcceptDataListSuggestion, |
| 161 | OnAcceptDataListSuggestion) |
[email protected] | 126b1ad | 2014-05-21 22:37:33 | [diff] [blame] | 162 | IPC_MESSAGE_HANDLER(AutofillMsg_FillPasswordSuggestion, |
| 163 | OnFillPasswordSuggestion) |
| 164 | IPC_MESSAGE_HANDLER(AutofillMsg_PreviewPasswordSuggestion, |
| 165 | OnPreviewPasswordSuggestion) |
[email protected] | 82fc1b1 | 2013-01-12 00:53:56 | [diff] [blame] | 166 | IPC_MESSAGE_HANDLER(AutofillMsg_RequestAutocompleteResult, |
| 167 | OnRequestAutocompleteResult) |
[email protected] | 676126f7 | 2011-01-15 00:03:51 | [diff] [blame] | 168 | IPC_MESSAGE_UNHANDLED(handled = false) |
| 169 | IPC_END_MESSAGE_MAP() |
| 170 | return handled; |
| 171 | } |
| 172 | |
[email protected] | c5041c32 | 2014-04-08 05:06:47 | [diff] [blame] | 173 | void AutofillAgent::DidFinishDocumentLoad(WebLocalFrame* frame) { |
[email protected] | 5193ea5 | 2014-05-14 01:10:02 | [diff] [blame] | 174 | // If the main frame just finished loading, we should process it. |
| 175 | if (!frame->parent()) |
| 176 | main_frame_processed_ = false; |
[email protected] | 8acc196 | 2013-03-17 07:04:52 | [diff] [blame] | 177 | |
[email protected] | 5193ea5 | 2014-05-14 01:10:02 | [diff] [blame] | 178 | ProcessForms(*frame); |
[email protected] | 676126f7 | 2011-01-15 00:03:51 | [diff] [blame] | 179 | } |
| 180 | |
[email protected] | 3609c96 | 2014-07-11 03:18:25 | [diff] [blame] | 181 | void AutofillAgent::DidCommitProvisionalLoad(WebLocalFrame* frame, |
| 182 | bool is_new_navigation) { |
| 183 | form_cache_.ResetFrame(*frame); |
| 184 | } |
| 185 | |
[email protected] | d7908be | 2013-01-22 16:50:18 | [diff] [blame] | 186 | void AutofillAgent::FrameDetached(WebFrame* frame) { |
[email protected] | 5219146e | 2013-02-28 11:42:30 | [diff] [blame] | 187 | form_cache_.ResetFrame(*frame); |
[email protected] | 676126f7 | 2011-01-15 00:03:51 | [diff] [blame] | 188 | } |
| 189 | |
[email protected] | 5e7e861 | 2014-03-20 14:43:19 | [diff] [blame] | 190 | void AutofillAgent::FrameWillClose(WebFrame* frame) { |
| 191 | if (in_flight_request_form_.isNull()) |
| 192 | return; |
| 193 | |
| 194 | for (WebFrame* temp = in_flight_request_form_.document().frame(); |
| 195 | temp; temp = temp->parent()) { |
| 196 | if (temp == frame) { |
| 197 | Send(new AutofillHostMsg_CancelRequestAutocomplete(routing_id())); |
| 198 | break; |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | |
[email protected] | c5041c32 | 2014-04-08 05:06:47 | [diff] [blame] | 203 | void AutofillAgent::WillSubmitForm(WebLocalFrame* frame, |
[email protected] | 2a5b173 | 2011-04-01 23:55:55 | [diff] [blame] | 204 | const WebFormElement& form) { |
| 205 | FormData form_data; |
[email protected] | aaa80c9 | 2011-09-16 00:23:54 | [diff] [blame] | 206 | if (WebFormElementToFormData(form, |
| 207 | WebFormControlElement(), |
[email protected] | c6292f8 | 2014-07-24 10:49:05 | [diff] [blame] | 208 | REQUIRE_NONE, |
[email protected] | aaa80c9 | 2011-09-16 00:23:54 | [diff] [blame] | 209 | static_cast<ExtractMask>( |
| 210 | EXTRACT_VALUE | EXTRACT_OPTION_TEXT), |
| 211 | &form_data, |
| 212 | NULL)) { |
[email protected] | 1d14f58 | 2011-09-02 20:42:04 | [diff] [blame] | 213 | Send(new AutofillHostMsg_FormSubmitted(routing_id(), form_data, |
| 214 | base::TimeTicks::Now())); |
[email protected] | 2a5b173 | 2011-04-01 23:55:55 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | |
[email protected] | 5e7e861 | 2014-03-20 14:43:19 | [diff] [blame] | 218 | void AutofillAgent::FocusedNodeChanged(const WebNode& node) { |
[email protected] | 91dcc6d3 | 2014-07-30 00:01:33 | [diff] [blame] | 219 | HidePopup(); |
| 220 | |
[email protected] | 7e04827 | 2014-04-17 03:38:52 | [diff] [blame] | 221 | if (password_generation_agent_ && |
| 222 | password_generation_agent_->FocusedNodeHasChanged(node)) { |
| 223 | is_popup_possibly_visible_ = true; |
| 224 | return; |
| 225 | } |
| 226 | |
[email protected] | f920d6e | 2013-03-12 20:20:50 | [diff] [blame] | 227 | if (node.isNull() || !node.isElementNode()) |
| 228 | return; |
| 229 | |
[email protected] | 5e7e861 | 2014-03-20 14:43:19 | [diff] [blame] | 230 | WebElement web_element = node.toConst<WebElement>(); |
[email protected] | f920d6e | 2013-03-12 20:20:50 | [diff] [blame] | 231 | |
| 232 | if (!web_element.document().frame()) |
| 233 | return; |
| 234 | |
| 235 | const WebInputElement* element = toWebInputElement(&web_element); |
| 236 | |
| 237 | if (!element || !element->isEnabled() || element->isReadOnly() || |
| 238 | !element->isTextField() || element->isPasswordField()) |
| 239 | return; |
| 240 | |
[email protected] | e5057a2 | 2013-04-22 12:41:39 | [diff] [blame] | 241 | element_ = *element; |
[email protected] | e5057a2 | 2013-04-22 12:41:39 | [diff] [blame] | 242 | } |
| 243 | |
[email protected] | fcdc564 | 2014-05-09 14:32:24 | [diff] [blame] | 244 | void AutofillAgent::OrientationChangeEvent() { |
[email protected] | b648f24 | 2014-02-25 13:49:06 | [diff] [blame] | 245 | HidePopup(); |
[email protected] | 652ad83 | 2013-08-16 11:47:04 | [diff] [blame] | 246 | } |
| 247 | |
estade | 48c8588 | 2014-08-25 18:26:50 | [diff] [blame] | 248 | void AutofillAgent::Resized() { |
| 249 | HidePopup(); |
| 250 | } |
| 251 | |
[email protected] | c5041c32 | 2014-04-08 05:06:47 | [diff] [blame] | 252 | void AutofillAgent::DidChangeScrollOffset(WebLocalFrame*) { |
[email protected] | b648f24 | 2014-02-25 13:49:06 | [diff] [blame] | 253 | HidePopup(); |
[email protected] | 2b942c33 | 2012-04-25 16:26:26 | [diff] [blame] | 254 | } |
| 255 | |
[email protected] | 1c6269a6 | 2014-04-12 03:17:14 | [diff] [blame] | 256 | void AutofillAgent::didRequestAutocomplete( |
[email protected] | 304130f | 2014-05-23 22:58:55 | [diff] [blame] | 257 | const WebFormElement& form) { |
[email protected] | fed9d4f | 2013-12-17 21:32:47 | [diff] [blame] | 258 | // Disallow the dialog over non-https or broken https, except when the |
| 259 | // ignore SSL flag is passed. See http://crbug.com/272512. |
| 260 | // TODO(palmer): this should be moved to the browser process after frames |
| 261 | // get their own processes. |
[email protected] | 1c6269a6 | 2014-04-12 03:17:14 | [diff] [blame] | 262 | GURL url(form.document().url()); |
| 263 | content::SSLStatus ssl_status = |
| 264 | render_view()->GetSSLStatusOfFrame(form.document().frame()); |
[email protected] | e8ca69c | 2014-05-07 15:31:19 | [diff] [blame] | 265 | bool is_safe = url.SchemeIs(url::kHttpsScheme) && |
[email protected] | fed9d4f | 2013-12-17 21:32:47 | [diff] [blame] | 266 | !net::IsCertStatusError(ssl_status.cert_status); |
| 267 | bool allow_unsafe = CommandLine::ForCurrentProcess()->HasSwitch( |
| 268 | ::switches::kReduceSecurityForTesting); |
| 269 | |
[email protected] | 81cd5233 | 2012-11-05 20:36:07 | [diff] [blame] | 270 | FormData form_data; |
[email protected] | a3d81cf | 2014-04-15 23:26:16 | [diff] [blame] | 271 | std::string error_message; |
| 272 | if (!in_flight_request_form_.isNull()) { |
| 273 | error_message = "already active."; |
| 274 | } else if (!is_safe && !allow_unsafe) { |
| 275 | error_message = |
| 276 | "must use a secure connection or --reduce-security-for-testing."; |
| 277 | } else if (!WebFormElementToFormData(form, |
| 278 | WebFormControlElement(), |
| 279 | REQUIRE_AUTOCOMPLETE, |
[email protected] | 191279cb | 2014-06-26 08:02:11 | [diff] [blame] | 280 | static_cast<ExtractMask>( |
| 281 | EXTRACT_VALUE | |
| 282 | EXTRACT_OPTION_TEXT | |
| 283 | EXTRACT_OPTIONS), |
[email protected] | a3d81cf | 2014-04-15 23:26:16 | [diff] [blame] | 284 | &form_data, |
| 285 | NULL)) { |
| 286 | error_message = "failed to parse form."; |
| 287 | } |
| 288 | |
| 289 | if (!error_message.empty()) { |
| 290 | WebConsoleMessage console_message = WebConsoleMessage( |
| 291 | WebConsoleMessage::LevelLog, |
| 292 | WebString(base::ASCIIToUTF16("requestAutocomplete: ") + |
| 293 | base::ASCIIToUTF16(error_message))); |
[email protected] | b221bd6 | 2014-06-06 10:16:37 | [diff] [blame] | 294 | form.document().frame()->addMessageToConsole(console_message); |
[email protected] | 81cd5233 | 2012-11-05 20:36:07 | [diff] [blame] | 295 | WebFormElement(form).finishRequestAutocomplete( |
[email protected] | 82fc1b1 | 2013-01-12 00:53:56 | [diff] [blame] | 296 | WebFormElement::AutocompleteResultErrorDisabled); |
[email protected] | 81cd5233 | 2012-11-05 20:36:07 | [diff] [blame] | 297 | return; |
| 298 | } |
| 299 | |
| 300 | // Cancel any pending Autofill requests and hide any currently showing popups. |
| 301 | ++autofill_query_id_; |
[email protected] | b648f24 | 2014-02-25 13:49:06 | [diff] [blame] | 302 | HidePopup(); |
[email protected] | 81cd5233 | 2012-11-05 20:36:07 | [diff] [blame] | 303 | |
| 304 | in_flight_request_form_ = form; |
[email protected] | ca5190f | 2013-07-08 11:10:31 | [diff] [blame] | 305 | Send(new AutofillHostMsg_RequestAutocomplete(routing_id(), form_data, url)); |
[email protected] | 81cd5233 | 2012-11-05 20:36:07 | [diff] [blame] | 306 | } |
| 307 | |
[email protected] | 90f2625 | 2013-02-15 19:48:32 | [diff] [blame] | 308 | void AutofillAgent::setIgnoreTextChanges(bool ignore) { |
| 309 | ignore_text_changes_ = ignore; |
| 310 | } |
| 311 | |
[email protected] | e9d29d39 | 2014-03-25 01:15:15 | [diff] [blame] | 312 | void AutofillAgent::FormControlElementClicked( |
| 313 | const WebFormControlElement& element, |
| 314 | bool was_focused) { |
| 315 | const WebInputElement* input_element = toWebInputElement(&element); |
| 316 | if (!input_element && !IsTextAreaElement(element)) |
| 317 | return; |
| 318 | |
[email protected] | 580c3805 | 2014-07-16 07:28:09 | [diff] [blame] | 319 | bool show_full_suggestion_list = element.isAutofilled() || was_focused; |
| 320 | bool show_password_suggestions_only = !was_focused; |
| 321 | ShowSuggestions(element, |
| 322 | true, |
| 323 | false, |
| 324 | true, |
| 325 | false, |
| 326 | show_full_suggestion_list, |
| 327 | show_password_suggestions_only); |
[email protected] | 676126f7 | 2011-01-15 00:03:51 | [diff] [blame] | 328 | } |
| 329 | |
[email protected] | 2fa18c2 | 2011-06-14 23:40:43 | [diff] [blame] | 330 | void AutofillAgent::textFieldDidEndEditing(const WebInputElement& element) { |
[email protected] | ad19b30 | 2013-04-03 07:42:19 | [diff] [blame] | 331 | password_autofill_agent_->TextFieldDidEndEditing(element); |
[email protected] | 7d24db7 | 2011-08-26 06:02:31 | [diff] [blame] | 332 | has_shown_autofill_popup_for_current_edit_ = false; |
[email protected] | 2739e0e | 2011-11-22 22:49:11 | [diff] [blame] | 333 | Send(new AutofillHostMsg_DidEndTextFieldEditing(routing_id())); |
[email protected] | 676126f7 | 2011-01-15 00:03:51 | [diff] [blame] | 334 | } |
| 335 | |
[email protected] | e9d29d39 | 2014-03-25 01:15:15 | [diff] [blame] | 336 | void AutofillAgent::textFieldDidChange(const WebFormControlElement& element) { |
[email protected] | 90f2625 | 2013-02-15 19:48:32 | [diff] [blame] | 337 | if (ignore_text_changes_) |
| 338 | return; |
| 339 | |
[email protected] | e9d29d39 | 2014-03-25 01:15:15 | [diff] [blame] | 340 | DCHECK(toWebInputElement(&element) || IsTextAreaElement(element)); |
| 341 | |
[email protected] | b37043c | 2012-05-31 17:08:12 | [diff] [blame] | 342 | if (did_set_node_text_) { |
[email protected] | b96a1d1 | 2012-11-30 22:44:32 | [diff] [blame] | 343 | did_set_node_text_ = false; |
| 344 | return; |
[email protected] | b37043c | 2012-05-31 17:08:12 | [diff] [blame] | 345 | } |
| 346 | |
[email protected] | 663bd9e | 2011-03-21 01:07:01 | [diff] [blame] | 347 | // We post a task for doing the Autofill as the caret position is not set |
[email protected] | 676126f7 | 2011-01-15 00:03:51 | [diff] [blame] | 348 | // properly at this point (http://bugs.webkit.org/show_bug.cgi?id=16976) and |
| 349 | // it is needed to trigger autofill. |
[email protected] | 5219146e | 2013-02-28 11:42:30 | [diff] [blame] | 350 | weak_ptr_factory_.InvalidateWeakPtrs(); |
[email protected] | d14c5b3 | 2013-05-06 05:25:11 | [diff] [blame] | 351 | base::MessageLoop::current()->PostTask( |
| 352 | FROM_HERE, |
| 353 | base::Bind(&AutofillAgent::TextFieldDidChangeImpl, |
| 354 | weak_ptr_factory_.GetWeakPtr(), |
| 355 | element)); |
[email protected] | 676126f7 | 2011-01-15 00:03:51 | [diff] [blame] | 356 | } |
| 357 | |
[email protected] | e9d29d39 | 2014-03-25 01:15:15 | [diff] [blame] | 358 | void AutofillAgent::TextFieldDidChangeImpl( |
| 359 | const WebFormControlElement& element) { |
[email protected] | f9af283 | 2012-12-14 04:20:43 | [diff] [blame] | 360 | // If the element isn't focused then the changes don't matter. This check is |
| 361 | // required to properly handle IME interactions. |
| 362 | if (!element.focused()) |
| 363 | return; |
| 364 | |
[email protected] | e9d29d39 | 2014-03-25 01:15:15 | [diff] [blame] | 365 | const WebInputElement* input_element = toWebInputElement(&element); |
| 366 | if (input_element) { |
| 367 | if (password_generation_agent_ && |
| 368 | password_generation_agent_->TextDidChangeInTextField(*input_element)) { |
[email protected] | 7e04827 | 2014-04-17 03:38:52 | [diff] [blame] | 369 | is_popup_possibly_visible_ = true; |
[email protected] | e9d29d39 | 2014-03-25 01:15:15 | [diff] [blame] | 370 | return; |
| 371 | } |
[email protected] | 3cbdf936 | 2014-01-31 23:12:23 | [diff] [blame] | 372 | |
[email protected] | e9d29d39 | 2014-03-25 01:15:15 | [diff] [blame] | 373 | if (password_autofill_agent_->TextDidChangeInTextField(*input_element)) { |
| 374 | element_ = element; |
| 375 | return; |
| 376 | } |
[email protected] | e7e8347 | 2012-04-05 02:56:26 | [diff] [blame] | 377 | } |
[email protected] | 676126f7 | 2011-01-15 00:03:51 | [diff] [blame] | 378 | |
[email protected] | 580c3805 | 2014-07-16 07:28:09 | [diff] [blame] | 379 | ShowSuggestions(element, false, true, false, false, false, false); |
[email protected] | 7d24db7 | 2011-08-26 06:02:31 | [diff] [blame] | 380 | |
[email protected] | 1ecbe86 | 2012-10-05 01:29:14 | [diff] [blame] | 381 | FormData form; |
| 382 | FormFieldData field; |
[email protected] | e9d29d39 | 2014-03-25 01:15:15 | [diff] [blame] | 383 | if (FindFormAndFieldForFormControlElement(element, |
| 384 | &form, |
| 385 | &field, |
| 386 | REQUIRE_NONE)) { |
[email protected] | 1d14f58 | 2011-09-02 20:42:04 | [diff] [blame] | 387 | Send(new AutofillHostMsg_TextFieldDidChange(routing_id(), form, field, |
| 388 | base::TimeTicks::Now())); |
| 389 | } |
[email protected] | 676126f7 | 2011-01-15 00:03:51 | [diff] [blame] | 390 | } |
| 391 | |
[email protected] | 2fa18c2 | 2011-06-14 23:40:43 | [diff] [blame] | 392 | void AutofillAgent::textFieldDidReceiveKeyDown(const WebInputElement& element, |
| 393 | const WebKeyboardEvent& event) { |
[email protected] | ad19b30 | 2013-04-03 07:42:19 | [diff] [blame] | 394 | if (password_autofill_agent_->TextFieldHandlingKeyDown(element, event)) { |
[email protected] | bef7f927 | 2012-04-17 10:47:49 | [diff] [blame] | 395 | element_ = element; |
[email protected] | fc1964a | 2011-01-20 19:33:07 | [diff] [blame] | 396 | return; |
[email protected] | e7e8347 | 2012-04-05 02:56:26 | [diff] [blame] | 397 | } |
[email protected] | 676126f7 | 2011-01-15 00:03:51 | [diff] [blame] | 398 | |
| 399 | if (event.windowsKeyCode == ui::VKEY_DOWN || |
| 400 | event.windowsKeyCode == ui::VKEY_UP) |
[email protected] | 580c3805 | 2014-07-16 07:28:09 | [diff] [blame] | 401 | ShowSuggestions(element, true, true, true, false, false, false); |
[email protected] | efeb565f | 2013-12-12 17:16:41 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | void AutofillAgent::openTextDataListChooser(const WebInputElement& element) { |
[email protected] | 580c3805 | 2014-07-16 07:28:09 | [diff] [blame] | 405 | ShowSuggestions(element, true, false, false, true, false, false); |
[email protected] | 676126f7 | 2011-01-15 00:03:51 | [diff] [blame] | 406 | } |
| 407 | |
[email protected] | fc22ae5 | 2014-04-23 13:48:04 | [diff] [blame] | 408 | void AutofillAgent::firstUserGestureObserved() { |
| 409 | password_autofill_agent_->FirstUserGestureObserved(); |
| 410 | } |
| 411 | |
[email protected] | d5ca8fb | 2013-04-11 17:54:31 | [diff] [blame] | 412 | void AutofillAgent::AcceptDataListSuggestion( |
| 413 | const base::string16& suggested_value) { |
[email protected] | e9d29d39 | 2014-03-25 01:15:15 | [diff] [blame] | 414 | WebInputElement* input_element = toWebInputElement(&element_); |
| 415 | DCHECK(input_element); |
[email protected] | d5ca8fb | 2013-04-11 17:54:31 | [diff] [blame] | 416 | base::string16 new_value = suggested_value; |
[email protected] | 71a90b3 | 2012-05-18 09:16:53 | [diff] [blame] | 417 | // If this element takes multiple values then replace the last part with |
| 418 | // the suggestion. |
[email protected] | 7bc4453 | 2014-07-10 16:02:15 | [diff] [blame] | 419 | if (input_element->isMultiple() && input_element->isEmailField()) { |
[email protected] | d5ca8fb | 2013-04-11 17:54:31 | [diff] [blame] | 420 | std::vector<base::string16> parts; |
[email protected] | 71a90b3 | 2012-05-18 09:16:53 | [diff] [blame] | 421 | |
[email protected] | e9d29d39 | 2014-03-25 01:15:15 | [diff] [blame] | 422 | base::SplitStringDontTrim(input_element->editingValue(), ',', &parts); |
[email protected] | 71a90b3 | 2012-05-18 09:16:53 | [diff] [blame] | 423 | if (parts.size() == 0) |
[email protected] | d5ca8fb | 2013-04-11 17:54:31 | [diff] [blame] | 424 | parts.push_back(base::string16()); |
[email protected] | 71a90b3 | 2012-05-18 09:16:53 | [diff] [blame] | 425 | |
[email protected] | d5ca8fb | 2013-04-11 17:54:31 | [diff] [blame] | 426 | base::string16 last_part = parts.back(); |
[email protected] | 71a90b3 | 2012-05-18 09:16:53 | [diff] [blame] | 427 | // We want to keep just the leading whitespace. |
| 428 | for (size_t i = 0; i < last_part.size(); ++i) { |
| 429 | if (!IsWhitespace(last_part[i])) { |
| 430 | last_part = last_part.substr(0, i); |
| 431 | break; |
| 432 | } |
| 433 | } |
| 434 | last_part.append(suggested_value); |
| 435 | parts[parts.size() - 1] = last_part; |
| 436 | |
| 437 | new_value = JoinString(parts, ','); |
| 438 | } |
[email protected] | e9d29d39 | 2014-03-25 01:15:15 | [diff] [blame] | 439 | FillFieldWithValue(new_value, input_element); |
[email protected] | 71a90b3 | 2012-05-18 09:16:53 | [diff] [blame] | 440 | } |
| 441 | |
[email protected] | 3eb5728c | 2011-06-20 22:32:24 | [diff] [blame] | 442 | void AutofillAgent::OnFieldTypePredictionsAvailable( |
| 443 | const std::vector<FormDataPredictions>& forms) { |
| 444 | for (size_t i = 0; i < forms.size(); ++i) { |
[email protected] | aaa80c9 | 2011-09-16 00:23:54 | [diff] [blame] | 445 | form_cache_.ShowPredictions(forms[i]); |
[email protected] | 3eb5728c | 2011-06-20 22:32:24 | [diff] [blame] | 446 | } |
| 447 | } |
| 448 | |
[email protected] | 63560b3 | 2014-03-04 07:06:26 | [diff] [blame] | 449 | void AutofillAgent::OnFillForm(int query_id, const FormData& form) { |
| 450 | if (!render_view()->GetWebView() || query_id != autofill_query_id_) |
| 451 | return; |
| 452 | |
| 453 | was_query_node_autofilled_ = element_.isAutofilled(); |
| 454 | FillForm(form, element_); |
| 455 | Send(new AutofillHostMsg_DidFillAutofillFormData(routing_id(), |
| 456 | base::TimeTicks::Now())); |
| 457 | } |
| 458 | |
[email protected] | 8527a78f | 2014-06-12 12:49:15 | [diff] [blame] | 459 | void AutofillAgent::OnPing() { |
| 460 | Send(new AutofillHostMsg_PingAck(routing_id())); |
| 461 | } |
| 462 | |
[email protected] | 63560b3 | 2014-03-04 07:06:26 | [diff] [blame] | 463 | void AutofillAgent::OnPreviewForm(int query_id, const FormData& form) { |
| 464 | if (!render_view()->GetWebView() || query_id != autofill_query_id_) |
| 465 | return; |
| 466 | |
| 467 | was_query_node_autofilled_ = element_.isAutofilled(); |
| 468 | PreviewForm(form, element_); |
| 469 | Send(new AutofillHostMsg_DidPreviewAutofillFormData(routing_id())); |
[email protected] | 6f001a6cf | 2012-02-09 15:21:53 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | void AutofillAgent::OnClearForm() { |
[email protected] | bef7f927 | 2012-04-17 10:47:49 | [diff] [blame] | 473 | form_cache_.ClearFormWithElement(element_); |
[email protected] | 6f001a6cf | 2012-02-09 15:21:53 | [diff] [blame] | 474 | } |
| 475 | |
[email protected] | 6f001a6cf | 2012-02-09 15:21:53 | [diff] [blame] | 476 | void AutofillAgent::OnClearPreviewedForm() { |
[email protected] | 93f2e50 | 2013-12-20 09:47:25 | [diff] [blame] | 477 | if (!element_.isNull()) { |
[email protected] | b5b1d69f | 2014-01-23 23:49:32 | [diff] [blame] | 478 | if (password_autofill_agent_->DidClearAutofillSelection(element_)) |
| 479 | return; |
| 480 | |
[email protected] | 93f2e50 | 2013-12-20 09:47:25 | [diff] [blame] | 481 | ClearPreviewedFormWithElement(element_, was_query_node_autofilled_); |
| 482 | } else { |
| 483 | // TODO(isherman): There seem to be rare cases where this code *is* |
| 484 | // reachable: see [ http://crbug.com/96321#c6 ]. Ideally we would |
| 485 | // understand those cases and fix the code to avoid them. However, so far I |
| 486 | // have been unable to reproduce such a case locally. If you hit this |
| 487 | // NOTREACHED(), please file a bug against me. |
| 488 | NOTREACHED(); |
| 489 | } |
[email protected] | 6f001a6cf | 2012-02-09 15:21:53 | [diff] [blame] | 490 | } |
| 491 | |
[email protected] | ac9b92f | 2014-03-15 00:48:32 | [diff] [blame] | 492 | void AutofillAgent::OnFillFieldWithValue(const base::string16& value) { |
[email protected] | e9d29d39 | 2014-03-25 01:15:15 | [diff] [blame] | 493 | WebInputElement* input_element = toWebInputElement(&element_); |
ziran.sun | ee0fd443 | 2014-08-27 10:10:49 | [diff] [blame] | 494 | if (input_element) { |
[email protected] | e9d29d39 | 2014-03-25 01:15:15 | [diff] [blame] | 495 | FillFieldWithValue(value, input_element); |
ziran.sun | ee0fd443 | 2014-08-27 10:10:49 | [diff] [blame] | 496 | input_element->setAutofilled(true); |
| 497 | } |
[email protected] | ac9b92f | 2014-03-15 00:48:32 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | void AutofillAgent::OnPreviewFieldWithValue(const base::string16& value) { |
[email protected] | e9d29d39 | 2014-03-25 01:15:15 | [diff] [blame] | 501 | WebInputElement* input_element = toWebInputElement(&element_); |
| 502 | if (input_element) |
| 503 | PreviewFieldWithValue(value, input_element); |
[email protected] | 6f001a6cf | 2012-02-09 15:21:53 | [diff] [blame] | 504 | } |
| 505 | |
[email protected] | d5ca8fb | 2013-04-11 17:54:31 | [diff] [blame] | 506 | void AutofillAgent::OnAcceptDataListSuggestion(const base::string16& value) { |
[email protected] | 5c8de6b9 | 2012-06-08 21:24:08 | [diff] [blame] | 507 | AcceptDataListSuggestion(value); |
| 508 | } |
| 509 | |
[email protected] | 126b1ad | 2014-05-21 22:37:33 | [diff] [blame] | 510 | void AutofillAgent::OnFillPasswordSuggestion(const base::string16& username, |
| 511 | const base::string16& password) { |
| 512 | bool handled = password_autofill_agent_->FillSuggestion( |
| 513 | element_, |
| 514 | username, |
| 515 | password); |
| 516 | DCHECK(handled); |
| 517 | } |
| 518 | |
| 519 | void AutofillAgent::OnPreviewPasswordSuggestion( |
[email protected] | c281d50 | 2014-04-24 21:07:36 | [diff] [blame] | 520 | const base::string16& username, |
| 521 | const base::string16& password) { |
[email protected] | 126b1ad | 2014-05-21 22:37:33 | [diff] [blame] | 522 | bool handled = password_autofill_agent_->PreviewSuggestion( |
[email protected] | bef7f927 | 2012-04-17 10:47:49 | [diff] [blame] | 523 | element_, |
[email protected] | c281d50 | 2014-04-24 21:07:36 | [diff] [blame] | 524 | username, |
| 525 | password); |
[email protected] | e7e8347 | 2012-04-05 02:56:26 | [diff] [blame] | 526 | DCHECK(handled); |
| 527 | } |
| 528 | |
[email protected] | 82fc1b1 | 2013-01-12 00:53:56 | [diff] [blame] | 529 | void AutofillAgent::OnRequestAutocompleteResult( |
[email protected] | 5e7e861 | 2014-03-20 14:43:19 | [diff] [blame] | 530 | WebFormElement::AutocompleteResult result, |
[email protected] | dbdd6027 | 2014-04-14 22:48:40 | [diff] [blame] | 531 | const base::string16& message, |
[email protected] | 5e7e861 | 2014-03-20 14:43:19 | [diff] [blame] | 532 | const FormData& form_data) { |
[email protected] | 364481b | 2013-01-29 01:52:28 | [diff] [blame] | 533 | if (in_flight_request_form_.isNull()) |
| 534 | return; |
| 535 | |
[email protected] | 9793fb23 | 2013-06-21 10:25:35 | [diff] [blame] | 536 | if (result == WebFormElement::AutocompleteResultSuccess) { |
[email protected] | 82fc1b1 | 2013-01-12 00:53:56 | [diff] [blame] | 537 | FillFormIncludingNonFocusableElements(form_data, in_flight_request_form_); |
[email protected] | 3bf260a | 2014-04-16 19:37:12 | [diff] [blame] | 538 | if (!in_flight_request_form_.checkValidity()) |
[email protected] | 9793fb23 | 2013-06-21 10:25:35 | [diff] [blame] | 539 | result = WebFormElement::AutocompleteResultErrorInvalid; |
| 540 | } |
[email protected] | 364481b | 2013-01-29 01:52:28 | [diff] [blame] | 541 | |
[email protected] | 81cd5233 | 2012-11-05 20:36:07 | [diff] [blame] | 542 | in_flight_request_form_.finishRequestAutocomplete(result); |
[email protected] | dbdd6027 | 2014-04-14 22:48:40 | [diff] [blame] | 543 | |
| 544 | if (!message.empty()) { |
| 545 | const base::string16 prefix(base::ASCIIToUTF16("requestAutocomplete: ")); |
| 546 | WebConsoleMessage console_message = WebConsoleMessage( |
| 547 | WebConsoleMessage::LevelLog, WebString(prefix + message)); |
| 548 | in_flight_request_form_.document().frame()->addMessageToConsole( |
| 549 | console_message); |
| 550 | } |
| 551 | |
[email protected] | 81cd5233 | 2012-11-05 20:36:07 | [diff] [blame] | 552 | in_flight_request_form_.reset(); |
| 553 | } |
| 554 | |
[email protected] | e9d29d39 | 2014-03-25 01:15:15 | [diff] [blame] | 555 | void AutofillAgent::ShowSuggestions(const WebFormControlElement& element, |
[email protected] | 7819208 | 2011-01-29 05:43:44 | [diff] [blame] | 556 | bool autofill_on_empty_values, |
| 557 | bool requires_caret_at_end, |
[email protected] | efeb565f | 2013-12-12 17:16:41 | [diff] [blame] | 558 | bool display_warning_if_disabled, |
[email protected] | 580c3805 | 2014-07-16 07:28:09 | [diff] [blame] | 559 | bool datalist_only, |
| 560 | bool show_full_suggestion_list, |
| 561 | bool show_password_suggestions_only) { |
[email protected] | e9d29d39 | 2014-03-25 01:15:15 | [diff] [blame] | 562 | if (!element.isEnabled() || element.isReadOnly()) |
[email protected] | efeb565f | 2013-12-12 17:16:41 | [diff] [blame] | 563 | return; |
[email protected] | 580c3805 | 2014-07-16 07:28:09 | [diff] [blame] | 564 | if (!datalist_only && !element.suggestedValue().isEmpty()) |
| 565 | return; |
[email protected] | e9d29d39 | 2014-03-25 01:15:15 | [diff] [blame] | 566 | |
| 567 | const WebInputElement* input_element = toWebInputElement(&element); |
| 568 | if (input_element) { |
| 569 | if (!input_element->isTextField() || input_element->isPasswordField()) |
| 570 | return; |
| 571 | if (!datalist_only && !input_element->suggestedValue().isEmpty()) |
| 572 | return; |
| 573 | } else { |
| 574 | DCHECK(IsTextAreaElement(element)); |
| 575 | if (!element.toConst<WebTextAreaElement>().suggestedValue().isEmpty()) |
| 576 | return; |
| 577 | } |
[email protected] | e47aec5 | 2010-08-12 00:50:30 | [diff] [blame] | 578 | |
[email protected] | 1c69f8b7 | 2012-03-14 03:18:50 | [diff] [blame] | 579 | // Don't attempt to autofill with values that are too large or if filling |
| 580 | // criteria are not met. |
[email protected] | 71a90b3 | 2012-05-18 09:16:53 | [diff] [blame] | 581 | WebString value = element.editingValue(); |
[email protected] | efeb565f | 2013-12-12 17:16:41 | [diff] [blame] | 582 | if (!datalist_only && |
[email protected] | d86263dcd | 2014-01-09 10:35:21 | [diff] [blame] | 583 | (value.length() > kMaxDataLength || |
[email protected] | efeb565f | 2013-12-12 17:16:41 | [diff] [blame] | 584 | (!autofill_on_empty_values && value.isEmpty()) || |
| 585 | (requires_caret_at_end && |
| 586 | (element.selectionStart() != element.selectionEnd() || |
| 587 | element.selectionEnd() != static_cast<int>(value.length()))))) { |
[email protected] | 1c69f8b7 | 2012-03-14 03:18:50 | [diff] [blame] | 588 | // Any popup currently showing is obsolete. |
[email protected] | b648f24 | 2014-02-25 13:49:06 | [diff] [blame] | 589 | HidePopup(); |
[email protected] | e47aec5 | 2010-08-12 00:50:30 | [diff] [blame] | 590 | return; |
[email protected] | 1c69f8b7 | 2012-03-14 03:18:50 | [diff] [blame] | 591 | } |
[email protected] | e47aec5 | 2010-08-12 00:50:30 | [diff] [blame] | 592 | |
[email protected] | bef7f927 | 2012-04-17 10:47:49 | [diff] [blame] | 593 | element_ = element; |
[email protected] | 580c3805 | 2014-07-16 07:28:09 | [diff] [blame] | 594 | if (IsAutofillableInputElement(input_element) && |
| 595 | (password_autofill_agent_->ShowSuggestions(*input_element, |
| 596 | show_full_suggestion_list) || |
| 597 | show_password_suggestions_only)) { |
[email protected] | b648f24 | 2014-02-25 13:49:06 | [diff] [blame] | 598 | is_popup_possibly_visible_ = true; |
[email protected] | c66ba98 | 2013-04-05 09:56:17 | [diff] [blame] | 599 | return; |
[email protected] | b648f24 | 2014-02-25 13:49:06 | [diff] [blame] | 600 | } |
[email protected] | bef7f927 | 2012-04-17 10:47:49 | [diff] [blame] | 601 | |
[email protected] | 33915c1 | 2013-07-11 23:16:33 | [diff] [blame] | 602 | // If autocomplete is disabled at the field level, ensure that the native |
| 603 | // UI won't try to show a warning, since that may conflict with a custom |
| 604 | // popup. Note that we cannot use the WebKit method element.autoComplete() |
[email protected] | 5af43c13 | 2012-11-06 07:03:44 | [diff] [blame] | 605 | // as it does not allow us to distinguish the case where autocomplete is |
| 606 | // disabled for *both* the element and for the form. |
[email protected] | d5ca8fb | 2013-04-11 17:54:31 | [diff] [blame] | 607 | const base::string16 autocomplete_attribute = |
| 608 | element.getAttribute("autocomplete"); |
[email protected] | df80704 | 2014-08-13 16:48:41 | [diff] [blame] | 609 | if (LowerCaseEqualsASCII(autocomplete_attribute, "off")) |
[email protected] | 33915c1 | 2013-07-11 23:16:33 | [diff] [blame] | 610 | display_warning_if_disabled = false; |
[email protected] | bef7f927 | 2012-04-17 10:47:49 | [diff] [blame] | 611 | |
[email protected] | efeb565f | 2013-12-12 17:16:41 | [diff] [blame] | 612 | QueryAutofillSuggestions(element, |
| 613 | display_warning_if_disabled, |
| 614 | datalist_only); |
[email protected] | 77bb0da | 2010-11-20 01:55:30 | [diff] [blame] | 615 | } |
| 616 | |
[email protected] | e9d29d39 | 2014-03-25 01:15:15 | [diff] [blame] | 617 | void AutofillAgent::QueryAutofillSuggestions( |
| 618 | const WebFormControlElement& element, |
| 619 | bool display_warning_if_disabled, |
| 620 | bool datalist_only) { |
[email protected] | e69b5f7 | 2013-01-24 00:59:13 | [diff] [blame] | 621 | if (!element.document().frame()) |
| 622 | return; |
| 623 | |
[email protected] | e9d29d39 | 2014-03-25 01:15:15 | [diff] [blame] | 624 | DCHECK(toWebInputElement(&element) || IsTextAreaElement(element)); |
| 625 | |
[email protected] | 77bb0da | 2010-11-20 01:55:30 | [diff] [blame] | 626 | static int query_counter = 0; |
| 627 | autofill_query_id_ = query_counter++; |
[email protected] | 6bcd58a | 2010-11-25 02:31:20 | [diff] [blame] | 628 | display_warning_if_disabled_ = display_warning_if_disabled; |
[email protected] | 77bb0da | 2010-11-20 01:55:30 | [diff] [blame] | 629 | |
[email protected] | 5af43c13 | 2012-11-06 07:03:44 | [diff] [blame] | 630 | // If autocomplete is disabled at the form level, we want to see if there |
| 631 | // would have been any suggestions were it enabled, so that we can show a |
| 632 | // warning. Otherwise, we want to ignore fields that disable autocomplete, so |
| 633 | // that the suggestions list does not include suggestions for these form |
| 634 | // fields -- see comment 1 on http://crbug.com/69914 |
estade | 0ee9126 | 2014-10-23 19:29:19 | [diff] [blame^] | 635 | RequirementsMask requirements = |
[email protected] | 5af43c13 | 2012-11-06 07:03:44 | [diff] [blame] | 636 | element.autoComplete() ? REQUIRE_AUTOCOMPLETE : REQUIRE_NONE; |
| 637 | |
estade | 0ee9126 | 2014-10-23 19:29:19 | [diff] [blame^] | 638 | // If we're ignoring autocomplete="off", always extract everything. |
| 639 | if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 640 | switches::kIgnoreAutocompleteOffForAutofill)) { |
| 641 | requirements = REQUIRE_NONE; |
| 642 | } |
| 643 | |
[email protected] | 1ecbe86 | 2012-10-05 01:29:14 | [diff] [blame] | 644 | FormData form; |
| 645 | FormFieldData field; |
[email protected] | e9d29d39 | 2014-03-25 01:15:15 | [diff] [blame] | 646 | if (!FindFormAndFieldForFormControlElement(element, &form, &field, |
| 647 | requirements)) { |
[email protected] | 7837be6 | 2011-01-18 23:45:08 | [diff] [blame] | 648 | // If we didn't find the cached form, at least let autocomplete have a shot |
| 649 | // at providing suggestions. |
[email protected] | aaa80c9 | 2011-09-16 00:23:54 | [diff] [blame] | 650 | WebFormControlElementToFormField(element, EXTRACT_VALUE, &field); |
[email protected] | 7837be6 | 2011-01-18 23:45:08 | [diff] [blame] | 651 | } |
[email protected] | efeb565f | 2013-12-12 17:16:41 | [diff] [blame] | 652 | if (datalist_only) |
| 653 | field.should_autocomplete = false; |
[email protected] | 77bb0da | 2010-11-20 01:55:30 | [diff] [blame] | 654 | |
[email protected] | f920d6e | 2013-03-12 20:20:50 | [diff] [blame] | 655 | gfx::RectF bounding_box_scaled = |
| 656 | GetScaledBoundingBox(web_view_->pageScaleFactor(), &element_); |
[email protected] | 73a087f | 2013-02-06 07:02:06 | [diff] [blame] | 657 | |
[email protected] | 7d0ea536 | 2014-05-01 23:39:43 | [diff] [blame] | 658 | std::vector<base::string16> data_list_values; |
| 659 | std::vector<base::string16> data_list_labels; |
[email protected] | e9d29d39 | 2014-03-25 01:15:15 | [diff] [blame] | 660 | const WebInputElement* input_element = toWebInputElement(&element); |
| 661 | if (input_element) { |
| 662 | // Find the datalist values and send them to the browser process. |
[email protected] | e9d29d39 | 2014-03-25 01:15:15 | [diff] [blame] | 663 | GetDataListSuggestions(*input_element, |
| 664 | datalist_only, |
| 665 | &data_list_values, |
| 666 | &data_list_labels); |
| 667 | TrimStringVectorForIPC(&data_list_values); |
| 668 | TrimStringVectorForIPC(&data_list_labels); |
[email protected] | e9d29d39 | 2014-03-25 01:15:15 | [diff] [blame] | 669 | } |
[email protected] | 5c8de6b9 | 2012-06-08 21:24:08 | [diff] [blame] | 670 | |
[email protected] | b648f24 | 2014-02-25 13:49:06 | [diff] [blame] | 671 | is_popup_possibly_visible_ = true; |
[email protected] | 7d0ea536 | 2014-05-01 23:39:43 | [diff] [blame] | 672 | Send(new AutofillHostMsg_SetDataList(routing_id(), |
| 673 | data_list_values, |
| 674 | data_list_labels)); |
| 675 | |
[email protected] | 2739e0e | 2011-11-22 22:49:11 | [diff] [blame] | 676 | Send(new AutofillHostMsg_QueryFormFieldAutofill(routing_id(), |
| 677 | autofill_query_id_, |
| 678 | form, |
| 679 | field, |
[email protected] | 73a087f | 2013-02-06 07:02:06 | [diff] [blame] | 680 | bounding_box_scaled, |
[email protected] | 2739e0e | 2011-11-22 22:49:11 | [diff] [blame] | 681 | display_warning_if_disabled)); |
[email protected] | e47aec5 | 2010-08-12 00:50:30 | [diff] [blame] | 682 | } |
| 683 | |
[email protected] | ac9b92f | 2014-03-15 00:48:32 | [diff] [blame] | 684 | void AutofillAgent::FillFieldWithValue(const base::string16& value, |
[email protected] | 5e7e861 | 2014-03-20 14:43:19 | [diff] [blame] | 685 | WebInputElement* node) { |
[email protected] | b37043c | 2012-05-31 17:08:12 | [diff] [blame] | 686 | did_set_node_text_ = true; |
[email protected] | 2ee19ec | 2014-02-25 12:44:31 | [diff] [blame] | 687 | node->setEditingValue(value.substr(0, node->maxLength())); |
[email protected] | ac9b92f | 2014-03-15 00:48:32 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | void AutofillAgent::PreviewFieldWithValue(const base::string16& value, |
[email protected] | 5e7e861 | 2014-03-20 14:43:19 | [diff] [blame] | 691 | WebInputElement* node) { |
[email protected] | ac9b92f | 2014-03-15 00:48:32 | [diff] [blame] | 692 | was_query_node_autofilled_ = element_.isAutofilled(); |
| 693 | node->setSuggestedValue(value.substr(0, node->maxLength())); |
| 694 | node->setAutofilled(true); |
[email protected] | f47d302 | 2014-03-24 21:24:18 | [diff] [blame] | 695 | node->setSelectionRange(node->value().length(), |
| 696 | node->suggestedValue().length()); |
[email protected] | 6f001a6cf | 2012-02-09 15:21:53 | [diff] [blame] | 697 | } |
| 698 | |
[email protected] | 5193ea5 | 2014-05-14 01:10:02 | [diff] [blame] | 699 | void AutofillAgent::ProcessForms(const WebLocalFrame& frame) { |
| 700 | // Record timestamp of when the forms are first seen. This is used to |
| 701 | // measure the overhead of the Autofill feature. |
| 702 | base::TimeTicks forms_seen_timestamp = base::TimeTicks::Now(); |
| 703 | |
| 704 | std::vector<FormData> forms; |
| 705 | form_cache_.ExtractNewForms(frame, &forms); |
| 706 | |
| 707 | // Always communicate to browser process for topmost frame. |
| 708 | if (!forms.empty() || |
| 709 | (!frame.parent() && !main_frame_processed_)) { |
| 710 | Send(new AutofillHostMsg_FormsSeen(routing_id(), forms, |
| 711 | forms_seen_timestamp)); |
| 712 | } |
| 713 | |
| 714 | if (!frame.parent()) |
| 715 | main_frame_processed_ = true; |
| 716 | } |
| 717 | |
[email protected] | b648f24 | 2014-02-25 13:49:06 | [diff] [blame] | 718 | void AutofillAgent::HidePopup() { |
| 719 | if (!is_popup_possibly_visible_) |
| 720 | return; |
| 721 | |
[email protected] | b5b1d69f | 2014-01-23 23:49:32 | [diff] [blame] | 722 | if (!element_.isNull()) |
| 723 | OnClearPreviewedForm(); |
| 724 | |
[email protected] | b648f24 | 2014-02-25 13:49:06 | [diff] [blame] | 725 | is_popup_possibly_visible_ = false; |
| 726 | Send(new AutofillHostMsg_HidePopup(routing_id())); |
[email protected] | 81cd5233 | 2012-11-05 20:36:07 | [diff] [blame] | 727 | } |
| 728 | |
[email protected] | 5e7e861 | 2014-03-20 14:43:19 | [diff] [blame] | 729 | void AutofillAgent::didAssociateFormControls(const WebVector<WebNode>& nodes) { |
[email protected] | 704b623 | 2013-09-16 21:19:12 | [diff] [blame] | 730 | for (size_t i = 0; i < nodes.size(); ++i) { |
[email protected] | 5193ea5 | 2014-05-14 01:10:02 | [diff] [blame] | 731 | WebLocalFrame* frame = nodes[i].document().frame(); |
[email protected] | 704b623 | 2013-09-16 21:19:12 | [diff] [blame] | 732 | // Only monitors dynamic forms created in the top frame. Dynamic forms |
[email protected] | 5193ea5 | 2014-05-14 01:10:02 | [diff] [blame] | 733 | // inserted in iframes are not captured yet. Frame is only processed |
| 734 | // if it has finished loading, otherwise you can end up with a partially |
| 735 | // parsed form. |
| 736 | if (frame && !frame->parent() && !frame->isLoading()) { |
| 737 | ProcessForms(*frame); |
[email protected] | 704b623 | 2013-09-16 21:19:12 | [diff] [blame] | 738 | password_autofill_agent_->OnDynamicFormsSeen(frame); |
gcasto | 93a9650 | 2014-10-21 04:15:30 | [diff] [blame] | 739 | if (password_generation_agent_) |
| 740 | password_generation_agent_->OnDynamicFormsSeen(frame); |
[email protected] | 704b623 | 2013-09-16 21:19:12 | [diff] [blame] | 741 | return; |
| 742 | } |
| 743 | } |
[email protected] | 17b6be7 | 2013-04-30 21:33:08 | [diff] [blame] | 744 | } |
| 745 | |
[email protected] | 7819208 | 2011-01-29 05:43:44 | [diff] [blame] | 746 | } // namespace autofill |