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