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