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