blob: 3d05c63b10a204c9cc2ab9629986643784748cde [file] [log] [blame]
[email protected]f07e4442013-06-06 23:03:481// Copyright 2013 The Chromium Authors. All rights reserved.
[email protected]679f128f2010-07-22 22:57:442// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]f07e4442013-06-06 23:03:485#include "components/autofill/content/renderer/autofill_agent.h"
[email protected]679f128f2010-07-22 22:57:446
[email protected]a1cb57f2011-09-28 22:41:297#include "base/bind.h"
[email protected]f920d6e2013-03-12 20:20:508#include "base/command_line.h"
[email protected]c50a6932013-07-17 22:16:139#include "base/message_loop/message_loop.h"
[email protected]1988e1c2013-02-28 20:27:4210#include "base/strings/string_split.h"
[email protected]c72674b2013-06-11 04:16:4311#include "base/strings/string_util.h"
[email protected]d2d79d52013-06-07 22:23:4812#include "base/strings/utf_string_conversions.h"
[email protected]bbd8da92013-06-28 02:12:2013#include "base/time/time.h"
[email protected]45b53fb2013-12-12 19:28:0614#include "components/autofill/content/common/autofill_messages.h"
[email protected]f07e4442013-06-06 23:03:4815#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]3cbdf9362014-01-31 23:12:2318#include "components/autofill/content/renderer/password_generation_agent.h"
[email protected]d04f81912013-06-18 14:52:1319#include "components/autofill/core/common/autofill_constants.h"
[email protected]d86263dcd2014-01-09 10:35:2120#include "components/autofill/core/common/autofill_data_validation.h"
[email protected]d04f81912013-06-18 14:52:1321#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]e620d362013-09-09 08:01:5325#include "components/autofill/core/common/password_form.h"
[email protected]d04f81912013-06-18 14:52:1326#include "components/autofill/core/common/web_element_descriptor.h"
[email protected]fed9d4f2013-12-17 21:32:4727#include "content/public/common/content_switches.h"
[email protected]e4495212012-12-06 03:09:1228#include "content/public/common/ssl_status.h"
[email protected]ca5190f2013-07-08 11:10:3129#include "content/public/common/url_constants.h"
[email protected]a2ef54c2011-10-10 16:20:3130#include "content/public/renderer/render_view.h"
[email protected]8ed495c2013-06-19 00:52:0031#include "grit/component_strings.h"
[email protected]ca5190f2013-07-08 11:10:3132#include "net/cert/cert_status_flags.h"
[email protected]f07e4442013-06-06 23:03:4833#include "third_party/WebKit/public/platform/WebRect.h"
34#include "third_party/WebKit/public/platform/WebURLRequest.h"
[email protected]dbdd60272014-04-14 22:48:4035#include "third_party/WebKit/public/web/WebConsoleMessage.h"
[email protected]a0962a92013-06-20 18:27:3436#include "third_party/WebKit/public/web/WebDataSource.h"
37#include "third_party/WebKit/public/web/WebDocument.h"
[email protected]961783a2014-02-07 18:38:0538#include "third_party/WebKit/public/web/WebElementCollection.h"
[email protected]a0962a92013-06-20 18:27:3439#include "third_party/WebKit/public/web/WebFormControlElement.h"
40#include "third_party/WebKit/public/web/WebFormElement.h"
[email protected]a0962a92013-06-20 18:27:3441#include "third_party/WebKit/public/web/WebInputEvent.h"
[email protected]80504652014-04-18 04:41:5042#include "third_party/WebKit/public/web/WebLocalFrame.h"
[email protected]a0962a92013-06-20 18:27:3443#include "third_party/WebKit/public/web/WebNode.h"
[email protected]a0962a92013-06-20 18:27:3444#include "third_party/WebKit/public/web/WebOptionElement.h"
[email protected]e9d29d392014-03-25 01:15:1545#include "third_party/WebKit/public/web/WebTextAreaElement.h"
[email protected]a0962a92013-06-20 18:27:3446#include "third_party/WebKit/public/web/WebView.h"
[email protected]c051a1b2011-01-21 23:30:1747#include "ui/base/l10n/l10n_util.h"
[email protected]7e9acd082013-09-17 23:31:1648#include "ui/events/keycodes/keyboard_codes.h"
[email protected]679f128f2010-07-22 22:57:4449
[email protected]a1221aea2013-11-07 01:31:3050using blink::WebAutofillClient;
[email protected]dbdd60272014-04-14 22:48:4051using blink::WebConsoleMessage;
[email protected]5e7e8612014-03-20 14:43:1952using blink::WebElement;
53using blink::WebElementCollection;
[email protected]a1221aea2013-11-07 01:31:3054using blink::WebFormControlElement;
55using blink::WebFormElement;
56using blink::WebFrame;
57using blink::WebInputElement;
58using blink::WebKeyboardEvent;
[email protected]c5041c322014-04-08 05:06:4759using blink::WebLocalFrame;
[email protected]a1221aea2013-11-07 01:31:3060using blink::WebNode;
[email protected]a1221aea2013-11-07 01:31:3061using blink::WebOptionElement;
62using blink::WebString;
[email protected]e9d29d392014-03-25 01:15:1563using blink::WebTextAreaElement;
[email protected]5e7e8612014-03-20 14:43:1964using blink::WebVector;
[email protected]679f128f2010-07-22 22:57:4465
[email protected]d86263dcd2014-01-09 10:35:2166namespace autofill {
67
[email protected]e47aec52010-08-12 00:50:3068namespace {
69
[email protected]45a07942013-07-26 08:28:2170// Gets all the data list values (with corresponding label) for the given
71// element.
[email protected]5e7e8612014-03-20 14:43:1972void GetDataListSuggestions(const WebInputElement& element,
[email protected]efeb565f2013-12-12 17:16:4173 bool ignore_current_value,
[email protected]45a07942013-07-26 08:28:2174 std::vector<base::string16>* values,
75 std::vector<base::string16>* labels) {
[email protected]961783a2014-02-07 18:38:0576 WebElementCollection options = element.dataListOptions();
[email protected]bef7f9272012-04-17 10:47:4977 if (options.isNull())
78 return;
79
[email protected]efeb565f2013-12-12 17:16:4180 base::string16 prefix;
81 if (!ignore_current_value) {
82 prefix = element.editingValue();
83 if (element.isMultiple() &&
84 element.formControlType() == WebString::fromUTF8("email")) {
85 std::vector<base::string16> parts;
86 base::SplitStringDontTrim(prefix, ',', &parts);
[email protected]8af69c6c2014-03-03 19:05:3187 if (parts.size() > 0) {
88 base::TrimWhitespace(parts[parts.size() - 1], base::TRIM_LEADING,
89 &prefix);
90 }
[email protected]efeb565f2013-12-12 17:16:4191 }
[email protected]71a90b32012-05-18 09:16:5392 }
[email protected]bef7f9272012-04-17 10:47:4993 for (WebOptionElement option = options.firstItem().to<WebOptionElement>();
[email protected]71a90b32012-05-18 09:16:5394 !option.isNull(); option = options.nextItem().to<WebOptionElement>()) {
95 if (!StartsWith(option.value(), prefix, false) ||
96 option.value() == prefix ||
[email protected]3f812632012-05-16 04:13:3697 !element.isValidValue(option.value()))
[email protected]bef7f9272012-04-17 10:47:4998 continue;
99
100 values->push_back(option.value());
101 if (option.value() != option.label())
102 labels->push_back(option.label());
103 else
[email protected]d5ca8fb2013-04-11 17:54:31104 labels->push_back(base::string16());
[email protected]bef7f9272012-04-17 10:47:49105 }
106}
107
[email protected]ead7fb02013-07-18 18:50:12108// Trim the vector before sending it to the browser process to ensure we
[email protected]5c8de6b92012-06-08 21:24:08109// don't send too much data through the IPC.
[email protected]ead7fb02013-07-18 18:50:12110void TrimStringVectorForIPC(std::vector<base::string16>* strings) {
111 // Limit the size of the vector.
[email protected]d86263dcd2014-01-09 10:35:21112 if (strings->size() > kMaxListSize)
113 strings->resize(kMaxListSize);
[email protected]5c8de6b92012-06-08 21:24:08114
[email protected]ead7fb02013-07-18 18:50:12115 // Limit the size of the strings in the vector.
116 for (size_t i = 0; i < strings->size(); ++i) {
[email protected]d86263dcd2014-01-09 10:35:21117 if ((*strings)[i].length() > kMaxDataLength)
118 (*strings)[i].resize(kMaxDataLength);
[email protected]5c8de6b92012-06-08 21:24:08119 }
120}
121
[email protected]e47aec52010-08-12 00:50:30122} // namespace
123
[email protected]ad19b302013-04-03 07:42:19124AutofillAgent::AutofillAgent(content::RenderView* render_view,
[email protected]3cbdf9362014-01-31 23:12:23125 PasswordAutofillAgent* password_autofill_agent,
126 PasswordGenerationAgent* password_generation_agent)
[email protected]3a034ebb2011-10-03 19:19:44127 : content::RenderViewObserver(render_view),
[email protected]ad19b302013-04-03 07:42:19128 password_autofill_agent_(password_autofill_agent),
[email protected]3cbdf9362014-01-31 23:12:23129 password_generation_agent_(password_generation_agent),
[email protected]679f128f2010-07-22 22:57:44130 autofill_query_id_(0),
[email protected]73a087f2013-02-06 07:02:06131 web_view_(render_view->GetWebView()),
[email protected]6bcd58a2010-11-25 02:31:20132 display_warning_if_disabled_(false),
[email protected]d77ddc8062010-11-24 01:14:06133 was_query_node_autofilled_(false),
[email protected]e4dce0af2011-09-19 22:22:28134 has_shown_autofill_popup_for_current_edit_(false),
[email protected]b37043c2012-05-31 17:08:12135 did_set_node_text_(false),
[email protected]17b6be72013-04-30 21:33:08136 has_new_forms_for_browser_(false),
[email protected]90f26252013-02-15 19:48:32137 ignore_text_changes_(false),
[email protected]b648f242014-02-25 13:49:06138 is_popup_possibly_visible_(false),
[email protected]2c305252013-04-26 19:57:05139 weak_ptr_factory_(this) {
[email protected]a2ef54c2011-10-10 16:20:31140 render_view->GetWebView()->setAutofillClient(this);
[email protected]03f10dad2013-04-13 08:23:05141
142 // The PageClickTracker is a RenderViewObserver, and hence will be freed when
143 // the RenderView is destroyed.
144 new PageClickTracker(render_view, this);
[email protected]679f128f2010-07-22 22:57:44145}
146
[email protected]a64fdc32013-07-31 09:21:13147AutofillAgent::~AutofillAgent() {}
[email protected]d2f05d02011-01-27 18:51:01148
[email protected]663bd9e2011-03-21 01:07:01149bool AutofillAgent::OnMessageReceived(const IPC::Message& message) {
[email protected]676126f72011-01-15 00:03:51150 bool handled = true;
[email protected]663bd9e2011-03-21 01:07:01151 IPC_BEGIN_MESSAGE_MAP(AutofillAgent, message)
[email protected]63560b32014-03-04 07:06:26152 IPC_MESSAGE_HANDLER(AutofillMsg_FillForm, OnFillForm)
153 IPC_MESSAGE_HANDLER(AutofillMsg_PreviewForm, OnPreviewForm)
[email protected]3eb5728c2011-06-20 22:32:24154 IPC_MESSAGE_HANDLER(AutofillMsg_FieldTypePredictionsAvailable,
155 OnFieldTypePredictionsAvailable)
[email protected]93f2e502013-12-20 09:47:25156 IPC_MESSAGE_HANDLER(AutofillMsg_ClearForm, OnClearForm)
[email protected]93f2e502013-12-20 09:47:25157 IPC_MESSAGE_HANDLER(AutofillMsg_ClearPreviewedForm, OnClearPreviewedForm)
[email protected]ac9b92f2014-03-15 00:48:32158 IPC_MESSAGE_HANDLER(AutofillMsg_FillFieldWithValue, OnFillFieldWithValue)
159 IPC_MESSAGE_HANDLER(AutofillMsg_PreviewFieldWithValue,
160 OnPreviewFieldWithValue)
[email protected]5c8de6b92012-06-08 21:24:08161 IPC_MESSAGE_HANDLER(AutofillMsg_AcceptDataListSuggestion,
162 OnAcceptDataListSuggestion)
[email protected]e7e83472012-04-05 02:56:26163 IPC_MESSAGE_HANDLER(AutofillMsg_AcceptPasswordAutofillSuggestion,
164 OnAcceptPasswordAutofillSuggestion)
[email protected]82fc1b12013-01-12 00:53:56165 IPC_MESSAGE_HANDLER(AutofillMsg_RequestAutocompleteResult,
166 OnRequestAutocompleteResult)
[email protected]676126f72011-01-15 00:03:51167 IPC_MESSAGE_UNHANDLED(handled = false)
168 IPC_END_MESSAGE_MAP()
169 return handled;
170}
171
[email protected]c5041c322014-04-08 05:06:47172void AutofillAgent::DidFinishDocumentLoad(WebLocalFrame* frame) {
[email protected]8acc1962013-03-17 07:04:52173 // Record timestamp on document load. This is used to record overhead of
174 // Autofill feature.
175 forms_seen_timestamp_ = base::TimeTicks::Now();
176
[email protected]5219146e2013-02-28 11:42:30177 // The document has now been fully loaded. Scan for forms to be sent up to
178 // the browser.
179 std::vector<FormData> forms;
[email protected]8acc1962013-03-17 07:04:52180 bool has_more_forms = false;
[email protected]5219146e2013-02-28 11:42:30181 if (!frame->parent()) {
[email protected]5219146e2013-02-28 11:42:30182 form_elements_.clear();
[email protected]8acc1962013-03-17 07:04:52183 has_more_forms = form_cache_.ExtractFormsAndFormElements(
184 *frame, kRequiredAutofillFields, &forms, &form_elements_);
[email protected]5219146e2013-02-28 11:42:30185 } else {
186 form_cache_.ExtractForms(*frame, &forms);
187 }
188
[email protected]17b6be72013-04-30 21:33:08189 autofill::FormsSeenState state = has_more_forms ?
190 autofill::PARTIAL_FORMS_SEEN : autofill::NO_SPECIAL_FORMS_SEEN;
191
[email protected]8acc1962013-03-17 07:04:52192 // Always communicate to browser process for topmost frame.
193 if (!forms.empty() || !frame->parent()) {
[email protected]5219146e2013-02-28 11:42:30194 Send(new AutofillHostMsg_FormsSeen(routing_id(), forms,
[email protected]8acc1962013-03-17 07:04:52195 forms_seen_timestamp_,
[email protected]17b6be72013-04-30 21:33:08196 state));
[email protected]5219146e2013-02-28 11:42:30197 }
[email protected]676126f72011-01-15 00:03:51198}
199
[email protected]d7908be2013-01-22 16:50:18200void AutofillAgent::FrameDetached(WebFrame* frame) {
[email protected]5219146e2013-02-28 11:42:30201 form_cache_.ResetFrame(*frame);
[email protected]676126f72011-01-15 00:03:51202}
203
[email protected]5e7e8612014-03-20 14:43:19204void AutofillAgent::FrameWillClose(WebFrame* frame) {
205 if (in_flight_request_form_.isNull())
206 return;
207
208 for (WebFrame* temp = in_flight_request_form_.document().frame();
209 temp; temp = temp->parent()) {
210 if (temp == frame) {
211 Send(new AutofillHostMsg_CancelRequestAutocomplete(routing_id()));
212 break;
213 }
214 }
215}
216
[email protected]c5041c322014-04-08 05:06:47217void AutofillAgent::WillSubmitForm(WebLocalFrame* frame,
[email protected]2a5b1732011-04-01 23:55:55218 const WebFormElement& form) {
219 FormData form_data;
[email protected]aaa80c92011-09-16 00:23:54220 if (WebFormElementToFormData(form,
221 WebFormControlElement(),
222 REQUIRE_AUTOCOMPLETE,
223 static_cast<ExtractMask>(
224 EXTRACT_VALUE | EXTRACT_OPTION_TEXT),
225 &form_data,
226 NULL)) {
[email protected]1d14f582011-09-02 20:42:04227 Send(new AutofillHostMsg_FormSubmitted(routing_id(), form_data,
228 base::TimeTicks::Now()));
[email protected]2a5b1732011-04-01 23:55:55229 }
230}
231
[email protected]2b942c332012-04-25 16:26:26232void AutofillAgent::ZoomLevelChanged() {
233 // Any time the zoom level changes, the page's content moves, so any Autofill
234 // popups should be hidden. This is only needed for the new Autofill UI
235 // because WebKit already knows to hide the old UI when this occurs.
[email protected]b648f242014-02-25 13:49:06236 HidePopup();
[email protected]2b942c332012-04-25 16:26:26237}
238
[email protected]5e7e8612014-03-20 14:43:19239void AutofillAgent::FocusedNodeChanged(const WebNode& node) {
[email protected]7e048272014-04-17 03:38:52240 if (password_generation_agent_ &&
241 password_generation_agent_->FocusedNodeHasChanged(node)) {
242 is_popup_possibly_visible_ = true;
243 return;
244 }
245
[email protected]f920d6e2013-03-12 20:20:50246 if (node.isNull() || !node.isElementNode())
247 return;
248
[email protected]5e7e8612014-03-20 14:43:19249 WebElement web_element = node.toConst<WebElement>();
[email protected]f920d6e2013-03-12 20:20:50250
251 if (!web_element.document().frame())
252 return;
253
254 const WebInputElement* element = toWebInputElement(&web_element);
255
256 if (!element || !element->isEnabled() || element->isReadOnly() ||
257 !element->isTextField() || element->isPasswordField())
258 return;
259
[email protected]e5057a22013-04-22 12:41:39260 element_ = *element;
[email protected]e5057a22013-04-22 12:41:39261}
262
[email protected]652ad832013-08-16 11:47:04263void AutofillAgent::OrientationChangeEvent(int orientation) {
[email protected]b648f242014-02-25 13:49:06264 HidePopup();
[email protected]652ad832013-08-16 11:47:04265}
266
[email protected]c5041c322014-04-08 05:06:47267void AutofillAgent::DidChangeScrollOffset(WebLocalFrame*) {
[email protected]b648f242014-02-25 13:49:06268 HidePopup();
[email protected]2b942c332012-04-25 16:26:26269}
270
[email protected]1c6269a62014-04-12 03:17:14271void AutofillAgent::didRequestAutocomplete(
272 const WebFormElement& form,
273 const blink::WebAutocompleteParams& details) {
274 // TODO(estade): honor |details|.
275
[email protected]fed9d4f2013-12-17 21:32:47276 // Disallow the dialog over non-https or broken https, except when the
277 // ignore SSL flag is passed. See http://crbug.com/272512.
278 // TODO(palmer): this should be moved to the browser process after frames
279 // get their own processes.
[email protected]1c6269a62014-04-12 03:17:14280 GURL url(form.document().url());
281 content::SSLStatus ssl_status =
282 render_view()->GetSSLStatusOfFrame(form.document().frame());
[email protected]fed9d4f2013-12-17 21:32:47283 bool is_safe = url.SchemeIs(content::kHttpsScheme) &&
284 !net::IsCertStatusError(ssl_status.cert_status);
285 bool allow_unsafe = CommandLine::ForCurrentProcess()->HasSwitch(
286 ::switches::kReduceSecurityForTesting);
287
[email protected]81cd52332012-11-05 20:36:07288 FormData form_data;
[email protected]a3d81cf2014-04-15 23:26:16289 std::string error_message;
290 if (!in_flight_request_form_.isNull()) {
291 error_message = "already active.";
292 } else if (!is_safe && !allow_unsafe) {
293 error_message =
294 "must use a secure connection or --reduce-security-for-testing.";
295 } else if (!WebFormElementToFormData(form,
296 WebFormControlElement(),
297 REQUIRE_AUTOCOMPLETE,
298 EXTRACT_OPTIONS,
299 &form_data,
300 NULL)) {
301 error_message = "failed to parse form.";
302 }
303
304 if (!error_message.empty()) {
305 WebConsoleMessage console_message = WebConsoleMessage(
306 WebConsoleMessage::LevelLog,
307 WebString(base::ASCIIToUTF16("requestAutocomplete: ") +
308 base::ASCIIToUTF16(error_message)));
[email protected]81cd52332012-11-05 20:36:07309 WebFormElement(form).finishRequestAutocomplete(
[email protected]82fc1b12013-01-12 00:53:56310 WebFormElement::AutocompleteResultErrorDisabled);
[email protected]81cd52332012-11-05 20:36:07311 return;
312 }
313
314 // Cancel any pending Autofill requests and hide any currently showing popups.
315 ++autofill_query_id_;
[email protected]b648f242014-02-25 13:49:06316 HidePopup();
[email protected]81cd52332012-11-05 20:36:07317
318 in_flight_request_form_ = form;
[email protected]ca5190f2013-07-08 11:10:31319 Send(new AutofillHostMsg_RequestAutocomplete(routing_id(), form_data, url));
[email protected]81cd52332012-11-05 20:36:07320}
321
[email protected]90f26252013-02-15 19:48:32322void AutofillAgent::setIgnoreTextChanges(bool ignore) {
323 ignore_text_changes_ = ignore;
324}
325
[email protected]e9d29d392014-03-25 01:15:15326void AutofillAgent::FormControlElementClicked(
327 const WebFormControlElement& element,
328 bool was_focused) {
329 const WebInputElement* input_element = toWebInputElement(&element);
330 if (!input_element && !IsTextAreaElement(element))
331 return;
332
[email protected]676126f72011-01-15 00:03:51333 if (was_focused)
[email protected]efeb565f2013-12-12 17:16:41334 ShowSuggestions(element, true, false, true, false);
[email protected]676126f72011-01-15 00:03:51335}
336
[email protected]e9d29d392014-03-25 01:15:15337void AutofillAgent::FormControlElementLostFocus() {
[email protected]b648f242014-02-25 13:49:06338 HidePopup();
[email protected]676126f72011-01-15 00:03:51339}
340
[email protected]2fa18c22011-06-14 23:40:43341void AutofillAgent::textFieldDidEndEditing(const WebInputElement& element) {
[email protected]ad19b302013-04-03 07:42:19342 password_autofill_agent_->TextFieldDidEndEditing(element);
[email protected]7d24db72011-08-26 06:02:31343 has_shown_autofill_popup_for_current_edit_ = false;
[email protected]2739e0e2011-11-22 22:49:11344 Send(new AutofillHostMsg_DidEndTextFieldEditing(routing_id()));
[email protected]676126f72011-01-15 00:03:51345}
346
[email protected]e9d29d392014-03-25 01:15:15347void AutofillAgent::textFieldDidChange(const WebFormControlElement& element) {
[email protected]90f26252013-02-15 19:48:32348 if (ignore_text_changes_)
349 return;
350
[email protected]e9d29d392014-03-25 01:15:15351 DCHECK(toWebInputElement(&element) || IsTextAreaElement(element));
352
[email protected]b37043c2012-05-31 17:08:12353 if (did_set_node_text_) {
[email protected]b96a1d12012-11-30 22:44:32354 did_set_node_text_ = false;
355 return;
[email protected]b37043c2012-05-31 17:08:12356 }
357
[email protected]663bd9e2011-03-21 01:07:01358 // We post a task for doing the Autofill as the caret position is not set
[email protected]676126f72011-01-15 00:03:51359 // properly at this point (http://bugs.webkit.org/show_bug.cgi?id=16976) and
360 // it is needed to trigger autofill.
[email protected]5219146e2013-02-28 11:42:30361 weak_ptr_factory_.InvalidateWeakPtrs();
[email protected]d14c5b32013-05-06 05:25:11362 base::MessageLoop::current()->PostTask(
363 FROM_HERE,
364 base::Bind(&AutofillAgent::TextFieldDidChangeImpl,
365 weak_ptr_factory_.GetWeakPtr(),
366 element));
[email protected]676126f72011-01-15 00:03:51367}
368
[email protected]e9d29d392014-03-25 01:15:15369void AutofillAgent::TextFieldDidChangeImpl(
370 const WebFormControlElement& element) {
[email protected]f9af2832012-12-14 04:20:43371 // If the element isn't focused then the changes don't matter. This check is
372 // required to properly handle IME interactions.
373 if (!element.focused())
374 return;
375
[email protected]e9d29d392014-03-25 01:15:15376 const WebInputElement* input_element = toWebInputElement(&element);
377 if (input_element) {
378 if (password_generation_agent_ &&
379 password_generation_agent_->TextDidChangeInTextField(*input_element)) {
[email protected]7e048272014-04-17 03:38:52380 is_popup_possibly_visible_ = true;
[email protected]e9d29d392014-03-25 01:15:15381 return;
382 }
[email protected]3cbdf9362014-01-31 23:12:23383
[email protected]e9d29d392014-03-25 01:15:15384 if (password_autofill_agent_->TextDidChangeInTextField(*input_element)) {
385 element_ = element;
386 return;
387 }
[email protected]e7e83472012-04-05 02:56:26388 }
[email protected]676126f72011-01-15 00:03:51389
[email protected]efeb565f2013-12-12 17:16:41390 ShowSuggestions(element, false, true, false, false);
[email protected]7d24db72011-08-26 06:02:31391
[email protected]1ecbe862012-10-05 01:29:14392 FormData form;
393 FormFieldData field;
[email protected]e9d29d392014-03-25 01:15:15394 if (FindFormAndFieldForFormControlElement(element,
395 &form,
396 &field,
397 REQUIRE_NONE)) {
[email protected]1d14f582011-09-02 20:42:04398 Send(new AutofillHostMsg_TextFieldDidChange(routing_id(), form, field,
399 base::TimeTicks::Now()));
400 }
[email protected]676126f72011-01-15 00:03:51401}
402
[email protected]2fa18c22011-06-14 23:40:43403void AutofillAgent::textFieldDidReceiveKeyDown(const WebInputElement& element,
404 const WebKeyboardEvent& event) {
[email protected]ad19b302013-04-03 07:42:19405 if (password_autofill_agent_->TextFieldHandlingKeyDown(element, event)) {
[email protected]bef7f9272012-04-17 10:47:49406 element_ = element;
[email protected]fc1964a2011-01-20 19:33:07407 return;
[email protected]e7e83472012-04-05 02:56:26408 }
[email protected]676126f72011-01-15 00:03:51409
410 if (event.windowsKeyCode == ui::VKEY_DOWN ||
411 event.windowsKeyCode == ui::VKEY_UP)
[email protected]efeb565f2013-12-12 17:16:41412 ShowSuggestions(element, true, true, true, false);
413}
414
415void AutofillAgent::openTextDataListChooser(const WebInputElement& element) {
416 ShowSuggestions(element, true, false, false, true);
[email protected]676126f72011-01-15 00:03:51417}
418
[email protected]fc22ae52014-04-23 13:48:04419void AutofillAgent::firstUserGestureObserved() {
420 password_autofill_agent_->FirstUserGestureObserved();
421}
422
[email protected]d5ca8fb2013-04-11 17:54:31423void AutofillAgent::AcceptDataListSuggestion(
424 const base::string16& suggested_value) {
[email protected]e9d29d392014-03-25 01:15:15425 WebInputElement* input_element = toWebInputElement(&element_);
426 DCHECK(input_element);
[email protected]d5ca8fb2013-04-11 17:54:31427 base::string16 new_value = suggested_value;
[email protected]71a90b32012-05-18 09:16:53428 // If this element takes multiple values then replace the last part with
429 // the suggestion.
[email protected]e9d29d392014-03-25 01:15:15430 if (input_element->isMultiple() &&
431 input_element->formControlType() == WebString::fromUTF8("email")) {
[email protected]d5ca8fb2013-04-11 17:54:31432 std::vector<base::string16> parts;
[email protected]71a90b32012-05-18 09:16:53433
[email protected]e9d29d392014-03-25 01:15:15434 base::SplitStringDontTrim(input_element->editingValue(), ',', &parts);
[email protected]71a90b32012-05-18 09:16:53435 if (parts.size() == 0)
[email protected]d5ca8fb2013-04-11 17:54:31436 parts.push_back(base::string16());
[email protected]71a90b32012-05-18 09:16:53437
[email protected]d5ca8fb2013-04-11 17:54:31438 base::string16 last_part = parts.back();
[email protected]71a90b32012-05-18 09:16:53439 // We want to keep just the leading whitespace.
440 for (size_t i = 0; i < last_part.size(); ++i) {
441 if (!IsWhitespace(last_part[i])) {
442 last_part = last_part.substr(0, i);
443 break;
444 }
445 }
446 last_part.append(suggested_value);
447 parts[parts.size() - 1] = last_part;
448
449 new_value = JoinString(parts, ',');
450 }
[email protected]e9d29d392014-03-25 01:15:15451 FillFieldWithValue(new_value, input_element);
[email protected]71a90b32012-05-18 09:16:53452}
453
[email protected]3eb5728c2011-06-20 22:32:24454void AutofillAgent::OnFieldTypePredictionsAvailable(
455 const std::vector<FormDataPredictions>& forms) {
456 for (size_t i = 0; i < forms.size(); ++i) {
[email protected]aaa80c92011-09-16 00:23:54457 form_cache_.ShowPredictions(forms[i]);
[email protected]3eb5728c2011-06-20 22:32:24458 }
459}
460
[email protected]63560b32014-03-04 07:06:26461void AutofillAgent::OnFillForm(int query_id, const FormData& form) {
462 if (!render_view()->GetWebView() || query_id != autofill_query_id_)
463 return;
464
465 was_query_node_autofilled_ = element_.isAutofilled();
466 FillForm(form, element_);
467 Send(new AutofillHostMsg_DidFillAutofillFormData(routing_id(),
468 base::TimeTicks::Now()));
469}
470
471void AutofillAgent::OnPreviewForm(int query_id, const FormData& form) {
472 if (!render_view()->GetWebView() || query_id != autofill_query_id_)
473 return;
474
475 was_query_node_autofilled_ = element_.isAutofilled();
476 PreviewForm(form, element_);
477 Send(new AutofillHostMsg_DidPreviewAutofillFormData(routing_id()));
[email protected]6f001a6cf2012-02-09 15:21:53478}
479
480void AutofillAgent::OnClearForm() {
[email protected]bef7f9272012-04-17 10:47:49481 form_cache_.ClearFormWithElement(element_);
[email protected]6f001a6cf2012-02-09 15:21:53482}
483
[email protected]6f001a6cf2012-02-09 15:21:53484void AutofillAgent::OnClearPreviewedForm() {
[email protected]93f2e502013-12-20 09:47:25485 if (!element_.isNull()) {
[email protected]b5b1d69f2014-01-23 23:49:32486 if (password_autofill_agent_->DidClearAutofillSelection(element_))
487 return;
488
[email protected]93f2e502013-12-20 09:47:25489 ClearPreviewedFormWithElement(element_, was_query_node_autofilled_);
490 } else {
491 // TODO(isherman): There seem to be rare cases where this code *is*
492 // reachable: see [ http://crbug.com/96321#c6 ]. Ideally we would
493 // understand those cases and fix the code to avoid them. However, so far I
494 // have been unable to reproduce such a case locally. If you hit this
495 // NOTREACHED(), please file a bug against me.
496 NOTREACHED();
497 }
[email protected]6f001a6cf2012-02-09 15:21:53498}
499
[email protected]ac9b92f2014-03-15 00:48:32500void AutofillAgent::OnFillFieldWithValue(const base::string16& value) {
[email protected]e9d29d392014-03-25 01:15:15501 WebInputElement* input_element = toWebInputElement(&element_);
502 if (input_element)
503 FillFieldWithValue(value, input_element);
[email protected]ac9b92f2014-03-15 00:48:32504}
505
506void AutofillAgent::OnPreviewFieldWithValue(const base::string16& value) {
[email protected]e9d29d392014-03-25 01:15:15507 WebInputElement* input_element = toWebInputElement(&element_);
508 if (input_element)
509 PreviewFieldWithValue(value, input_element);
[email protected]6f001a6cf2012-02-09 15:21:53510}
511
[email protected]d5ca8fb2013-04-11 17:54:31512void AutofillAgent::OnAcceptDataListSuggestion(const base::string16& value) {
[email protected]5c8de6b92012-06-08 21:24:08513 AcceptDataListSuggestion(value);
514}
515
[email protected]d5ca8fb2013-04-11 17:54:31516void AutofillAgent::OnAcceptPasswordAutofillSuggestion(
[email protected]d619ba642013-11-22 15:34:33517 const base::string16& username) {
[email protected]e7e83472012-04-05 02:56:26518 // We need to make sure this is handled here because the browser process
519 // skipped it handling because it believed it would be handled here. If it
520 // isn't handled here then the browser logic needs to be updated.
[email protected]ad19b302013-04-03 07:42:19521 bool handled = password_autofill_agent_->DidAcceptAutofillSuggestion(
[email protected]bef7f9272012-04-17 10:47:49522 element_,
[email protected]d619ba642013-11-22 15:34:33523 username);
[email protected]e7e83472012-04-05 02:56:26524 DCHECK(handled);
525}
526
[email protected]82fc1b12013-01-12 00:53:56527void AutofillAgent::OnRequestAutocompleteResult(
[email protected]5e7e8612014-03-20 14:43:19528 WebFormElement::AutocompleteResult result,
[email protected]dbdd60272014-04-14 22:48:40529 const base::string16& message,
[email protected]5e7e8612014-03-20 14:43:19530 const FormData& form_data) {
[email protected]364481b2013-01-29 01:52:28531 if (in_flight_request_form_.isNull())
532 return;
533
[email protected]9793fb232013-06-21 10:25:35534 if (result == WebFormElement::AutocompleteResultSuccess) {
[email protected]82fc1b12013-01-12 00:53:56535 FillFormIncludingNonFocusableElements(form_data, in_flight_request_form_);
[email protected]3bf260a2014-04-16 19:37:12536 if (!in_flight_request_form_.checkValidity())
[email protected]9793fb232013-06-21 10:25:35537 result = WebFormElement::AutocompleteResultErrorInvalid;
538 }
[email protected]364481b2013-01-29 01:52:28539
[email protected]81cd52332012-11-05 20:36:07540 in_flight_request_form_.finishRequestAutocomplete(result);
[email protected]dbdd60272014-04-14 22:48:40541
542 if (!message.empty()) {
543 const base::string16 prefix(base::ASCIIToUTF16("requestAutocomplete: "));
544 WebConsoleMessage console_message = WebConsoleMessage(
545 WebConsoleMessage::LevelLog, WebString(prefix + message));
546 in_flight_request_form_.document().frame()->addMessageToConsole(
547 console_message);
548 }
549
[email protected]81cd52332012-11-05 20:36:07550 in_flight_request_form_.reset();
551}
552
[email protected]e9d29d392014-03-25 01:15:15553void AutofillAgent::ShowSuggestions(const WebFormControlElement& element,
[email protected]78192082011-01-29 05:43:44554 bool autofill_on_empty_values,
555 bool requires_caret_at_end,
[email protected]efeb565f2013-12-12 17:16:41556 bool display_warning_if_disabled,
557 bool datalist_only) {
[email protected]e9d29d392014-03-25 01:15:15558 if (!element.isEnabled() || element.isReadOnly())
[email protected]efeb565f2013-12-12 17:16:41559 return;
[email protected]e9d29d392014-03-25 01:15:15560
561 const WebInputElement* input_element = toWebInputElement(&element);
562 if (input_element) {
563 if (!input_element->isTextField() || input_element->isPasswordField())
564 return;
565 if (!datalist_only && !input_element->suggestedValue().isEmpty())
566 return;
567 } else {
568 DCHECK(IsTextAreaElement(element));
569 if (!element.toConst<WebTextAreaElement>().suggestedValue().isEmpty())
570 return;
571 }
[email protected]e47aec52010-08-12 00:50:30572
[email protected]1c69f8b72012-03-14 03:18:50573 // Don't attempt to autofill with values that are too large or if filling
574 // criteria are not met.
[email protected]71a90b32012-05-18 09:16:53575 WebString value = element.editingValue();
[email protected]efeb565f2013-12-12 17:16:41576 if (!datalist_only &&
[email protected]d86263dcd2014-01-09 10:35:21577 (value.length() > kMaxDataLength ||
[email protected]efeb565f2013-12-12 17:16:41578 (!autofill_on_empty_values && value.isEmpty()) ||
579 (requires_caret_at_end &&
580 (element.selectionStart() != element.selectionEnd() ||
581 element.selectionEnd() != static_cast<int>(value.length()))))) {
[email protected]1c69f8b72012-03-14 03:18:50582 // Any popup currently showing is obsolete.
[email protected]b648f242014-02-25 13:49:06583 HidePopup();
[email protected]e47aec52010-08-12 00:50:30584 return;
[email protected]1c69f8b72012-03-14 03:18:50585 }
[email protected]e47aec52010-08-12 00:50:30586
[email protected]bef7f9272012-04-17 10:47:49587 element_ = element;
[email protected]e9d29d392014-03-25 01:15:15588 if (input_element &&
589 password_autofill_agent_->ShowSuggestions(*input_element)) {
[email protected]b648f242014-02-25 13:49:06590 is_popup_possibly_visible_ = true;
[email protected]c66ba982013-04-05 09:56:17591 return;
[email protected]b648f242014-02-25 13:49:06592 }
[email protected]bef7f9272012-04-17 10:47:49593
[email protected]33915c12013-07-11 23:16:33594 // If autocomplete is disabled at the field level, ensure that the native
595 // UI won't try to show a warning, since that may conflict with a custom
596 // popup. Note that we cannot use the WebKit method element.autoComplete()
[email protected]5af43c132012-11-06 07:03:44597 // as it does not allow us to distinguish the case where autocomplete is
598 // disabled for *both* the element and for the form.
[email protected]d5ca8fb2013-04-11 17:54:31599 const base::string16 autocomplete_attribute =
600 element.getAttribute("autocomplete");
[email protected]33915c12013-07-11 23:16:33601 if (LowerCaseEqualsASCII(autocomplete_attribute, "off"))
602 display_warning_if_disabled = false;
[email protected]bef7f9272012-04-17 10:47:49603
[email protected]efeb565f2013-12-12 17:16:41604 QueryAutofillSuggestions(element,
605 display_warning_if_disabled,
606 datalist_only);
[email protected]77bb0da2010-11-20 01:55:30607}
608
[email protected]e9d29d392014-03-25 01:15:15609void AutofillAgent::QueryAutofillSuggestions(
610 const WebFormControlElement& element,
611 bool display_warning_if_disabled,
612 bool datalist_only) {
[email protected]e69b5f72013-01-24 00:59:13613 if (!element.document().frame())
614 return;
615
[email protected]e9d29d392014-03-25 01:15:15616 DCHECK(toWebInputElement(&element) || IsTextAreaElement(element));
617
[email protected]77bb0da2010-11-20 01:55:30618 static int query_counter = 0;
619 autofill_query_id_ = query_counter++;
[email protected]6bcd58a2010-11-25 02:31:20620 display_warning_if_disabled_ = display_warning_if_disabled;
[email protected]77bb0da2010-11-20 01:55:30621
[email protected]5af43c132012-11-06 07:03:44622 // If autocomplete is disabled at the form level, we want to see if there
623 // would have been any suggestions were it enabled, so that we can show a
624 // warning. Otherwise, we want to ignore fields that disable autocomplete, so
625 // that the suggestions list does not include suggestions for these form
626 // fields -- see comment 1 on http://crbug.com/69914
[email protected]5af43c132012-11-06 07:03:44627 const RequirementsMask requirements =
628 element.autoComplete() ? REQUIRE_AUTOCOMPLETE : REQUIRE_NONE;
629
[email protected]1ecbe862012-10-05 01:29:14630 FormData form;
631 FormFieldData field;
[email protected]e9d29d392014-03-25 01:15:15632 if (!FindFormAndFieldForFormControlElement(element, &form, &field,
633 requirements)) {
[email protected]7837be62011-01-18 23:45:08634 // If we didn't find the cached form, at least let autocomplete have a shot
635 // at providing suggestions.
[email protected]aaa80c92011-09-16 00:23:54636 WebFormControlElementToFormField(element, EXTRACT_VALUE, &field);
[email protected]7837be62011-01-18 23:45:08637 }
[email protected]efeb565f2013-12-12 17:16:41638 if (datalist_only)
639 field.should_autocomplete = false;
[email protected]77bb0da2010-11-20 01:55:30640
[email protected]f920d6e2013-03-12 20:20:50641 gfx::RectF bounding_box_scaled =
642 GetScaledBoundingBox(web_view_->pageScaleFactor(), &element_);
[email protected]73a087f2013-02-06 07:02:06643
[email protected]e9d29d392014-03-25 01:15:15644 const WebInputElement* input_element = toWebInputElement(&element);
645 if (input_element) {
646 // Find the datalist values and send them to the browser process.
647 std::vector<base::string16> data_list_values;
648 std::vector<base::string16> data_list_labels;
649 GetDataListSuggestions(*input_element,
650 datalist_only,
651 &data_list_values,
652 &data_list_labels);
653 TrimStringVectorForIPC(&data_list_values);
654 TrimStringVectorForIPC(&data_list_labels);
655
656 Send(new AutofillHostMsg_SetDataList(routing_id(),
657 data_list_values,
658 data_list_labels));
659 }
[email protected]5c8de6b92012-06-08 21:24:08660
[email protected]b648f242014-02-25 13:49:06661 is_popup_possibly_visible_ = true;
[email protected]2739e0e2011-11-22 22:49:11662 Send(new AutofillHostMsg_QueryFormFieldAutofill(routing_id(),
663 autofill_query_id_,
664 form,
665 field,
[email protected]73a087f2013-02-06 07:02:06666 bounding_box_scaled,
[email protected]2739e0e2011-11-22 22:49:11667 display_warning_if_disabled));
[email protected]e47aec52010-08-12 00:50:30668}
669
[email protected]ac9b92f2014-03-15 00:48:32670void AutofillAgent::FillFieldWithValue(const base::string16& value,
[email protected]5e7e8612014-03-20 14:43:19671 WebInputElement* node) {
[email protected]b37043c2012-05-31 17:08:12672 did_set_node_text_ = true;
[email protected]2ee19ec2014-02-25 12:44:31673 node->setEditingValue(value.substr(0, node->maxLength()));
[email protected]ac9b92f2014-03-15 00:48:32674 node->setAutofilled(true);
675}
676
677void AutofillAgent::PreviewFieldWithValue(const base::string16& value,
[email protected]5e7e8612014-03-20 14:43:19678 WebInputElement* node) {
[email protected]ac9b92f2014-03-15 00:48:32679 was_query_node_autofilled_ = element_.isAutofilled();
680 node->setSuggestedValue(value.substr(0, node->maxLength()));
681 node->setAutofilled(true);
[email protected]f47d3022014-03-24 21:24:18682 node->setSelectionRange(node->value().length(),
683 node->suggestedValue().length());
[email protected]6f001a6cf2012-02-09 15:21:53684}
685
[email protected]b648f242014-02-25 13:49:06686void AutofillAgent::HidePopup() {
687 if (!is_popup_possibly_visible_)
688 return;
689
[email protected]b5b1d69f2014-01-23 23:49:32690 if (!element_.isNull())
691 OnClearPreviewedForm();
692
[email protected]b648f242014-02-25 13:49:06693 is_popup_possibly_visible_ = false;
694 Send(new AutofillHostMsg_HidePopup(routing_id()));
[email protected]81cd52332012-11-05 20:36:07695}
696
[email protected]704b6232013-09-16 21:19:12697// TODO(isherman): Decide if we want to support non-password autofill with AJAX.
[email protected]5e7e8612014-03-20 14:43:19698void AutofillAgent::didAssociateFormControls(const WebVector<WebNode>& nodes) {
[email protected]704b6232013-09-16 21:19:12699 for (size_t i = 0; i < nodes.size(); ++i) {
[email protected]5e7e8612014-03-20 14:43:19700 WebFrame* frame = nodes[i].document().frame();
[email protected]704b6232013-09-16 21:19:12701 // Only monitors dynamic forms created in the top frame. Dynamic forms
702 // inserted in iframes are not captured yet.
[email protected]c7e170c92014-03-12 16:39:32703 if (frame && !frame->parent()) {
[email protected]704b6232013-09-16 21:19:12704 password_autofill_agent_->OnDynamicFormsSeen(frame);
705 return;
706 }
707 }
[email protected]17b6be72013-04-30 21:33:08708}
709
[email protected]78192082011-01-29 05:43:44710} // namespace autofill