blob: 16849bb439fc835c91e3c80b6769293d8077fcb1 [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]a0962a92013-06-20 18:27:3435#include "third_party/WebKit/public/web/WebDataSource.h"
36#include "third_party/WebKit/public/web/WebDocument.h"
[email protected]961783a2014-02-07 18:38:0537#include "third_party/WebKit/public/web/WebElementCollection.h"
[email protected]a0962a92013-06-20 18:27:3438#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]a0962a92013-06-20 18:27:3443#include "third_party/WebKit/public/web/WebOptionElement.h"
44#include "third_party/WebKit/public/web/WebView.h"
[email protected]c051a1b2011-01-21 23:30:1745#include "ui/base/l10n/l10n_util.h"
[email protected]7e9acd082013-09-17 23:31:1646#include "ui/events/keycodes/keyboard_codes.h"
[email protected]679f128f2010-07-22 22:57:4447
[email protected]a1221aea2013-11-07 01:31:3048using blink::WebAutofillClient;
49using blink::WebFormControlElement;
50using blink::WebFormElement;
51using blink::WebFrame;
52using blink::WebInputElement;
53using blink::WebKeyboardEvent;
54using blink::WebNode;
[email protected]961783a2014-02-07 18:38:0555using blink::WebElementCollection;
[email protected]a1221aea2013-11-07 01:31:3056using blink::WebOptionElement;
57using blink::WebString;
[email protected]679f128f2010-07-22 22:57:4458
[email protected]d86263dcd2014-01-09 10:35:2159namespace autofill {
60
[email protected]e47aec52010-08-12 00:50:3061namespace {
62
[email protected]45a07942013-07-26 08:28:2163// Gets all the data list values (with corresponding label) for the given
64// element.
[email protected]a1221aea2013-11-07 01:31:3065void GetDataListSuggestions(const blink::WebInputElement& element,
[email protected]efeb565f2013-12-12 17:16:4166 bool ignore_current_value,
[email protected]45a07942013-07-26 08:28:2167 std::vector<base::string16>* values,
68 std::vector<base::string16>* labels) {
[email protected]961783a2014-02-07 18:38:0569 WebElementCollection options = element.dataListOptions();
[email protected]bef7f9272012-04-17 10:47:4970 if (options.isNull())
71 return;
72
[email protected]efeb565f2013-12-12 17:16:4173 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]71a90b32012-05-18 09:16:5383 }
[email protected]bef7f9272012-04-17 10:47:4984 for (WebOptionElement option = options.firstItem().to<WebOptionElement>();
[email protected]71a90b32012-05-18 09:16:5385 !option.isNull(); option = options.nextItem().to<WebOptionElement>()) {
86 if (!StartsWith(option.value(), prefix, false) ||
87 option.value() == prefix ||
[email protected]3f812632012-05-16 04:13:3688 !element.isValidValue(option.value()))
[email protected]bef7f9272012-04-17 10:47:4989 continue;
90
91 values->push_back(option.value());
92 if (option.value() != option.label())
93 labels->push_back(option.label());
94 else
[email protected]d5ca8fb2013-04-11 17:54:3195 labels->push_back(base::string16());
[email protected]bef7f9272012-04-17 10:47:4996 }
97}
98
[email protected]ead7fb02013-07-18 18:50:1299// Trim the vector before sending it to the browser process to ensure we
[email protected]5c8de6b92012-06-08 21:24:08100// don't send too much data through the IPC.
[email protected]ead7fb02013-07-18 18:50:12101void TrimStringVectorForIPC(std::vector<base::string16>* strings) {
102 // Limit the size of the vector.
[email protected]d86263dcd2014-01-09 10:35:21103 if (strings->size() > kMaxListSize)
104 strings->resize(kMaxListSize);
[email protected]5c8de6b92012-06-08 21:24:08105
[email protected]ead7fb02013-07-18 18:50:12106 // Limit the size of the strings in the vector.
107 for (size_t i = 0; i < strings->size(); ++i) {
[email protected]d86263dcd2014-01-09 10:35:21108 if ((*strings)[i].length() > kMaxDataLength)
109 (*strings)[i].resize(kMaxDataLength);
[email protected]5c8de6b92012-06-08 21:24:08110 }
111}
112
[email protected]e47aec52010-08-12 00:50:30113} // namespace
114
[email protected]ad19b302013-04-03 07:42:19115AutofillAgent::AutofillAgent(content::RenderView* render_view,
[email protected]3cbdf9362014-01-31 23:12:23116 PasswordAutofillAgent* password_autofill_agent,
117 PasswordGenerationAgent* password_generation_agent)
[email protected]3a034ebb2011-10-03 19:19:44118 : content::RenderViewObserver(render_view),
[email protected]ad19b302013-04-03 07:42:19119 password_autofill_agent_(password_autofill_agent),
[email protected]3cbdf9362014-01-31 23:12:23120 password_generation_agent_(password_generation_agent),
[email protected]679f128f2010-07-22 22:57:44121 autofill_query_id_(0),
122 autofill_action_(AUTOFILL_NONE),
[email protected]73a087f2013-02-06 07:02:06123 web_view_(render_view->GetWebView()),
[email protected]6bcd58a2010-11-25 02:31:20124 display_warning_if_disabled_(false),
[email protected]d77ddc8062010-11-24 01:14:06125 was_query_node_autofilled_(false),
[email protected]e4dce0af2011-09-19 22:22:28126 has_shown_autofill_popup_for_current_edit_(false),
[email protected]b37043c2012-05-31 17:08:12127 did_set_node_text_(false),
[email protected]17b6be72013-04-30 21:33:08128 has_new_forms_for_browser_(false),
[email protected]90f26252013-02-15 19:48:32129 ignore_text_changes_(false),
[email protected]b648f242014-02-25 13:49:06130 is_popup_possibly_visible_(false),
[email protected]2c305252013-04-26 19:57:05131 weak_ptr_factory_(this) {
[email protected]a2ef54c2011-10-10 16:20:31132 render_view->GetWebView()->setAutofillClient(this);
[email protected]03f10dad2013-04-13 08:23:05133
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]679f128f2010-07-22 22:57:44137}
138
[email protected]a64fdc32013-07-31 09:21:13139AutofillAgent::~AutofillAgent() {}
[email protected]d2f05d02011-01-27 18:51:01140
[email protected]663bd9e2011-03-21 01:07:01141bool AutofillAgent::OnMessageReceived(const IPC::Message& message) {
[email protected]676126f72011-01-15 00:03:51142 bool handled = true;
[email protected]663bd9e2011-03-21 01:07:01143 IPC_BEGIN_MESSAGE_MAP(AutofillAgent, message)
[email protected]663bd9e2011-03-21 01:07:01144 IPC_MESSAGE_HANDLER(AutofillMsg_FormDataFilled, OnFormDataFilled)
[email protected]3eb5728c2011-06-20 22:32:24145 IPC_MESSAGE_HANDLER(AutofillMsg_FieldTypePredictionsAvailable,
146 OnFieldTypePredictionsAvailable)
[email protected]6f001a6cf2012-02-09 15:21:53147 IPC_MESSAGE_HANDLER(AutofillMsg_SetAutofillActionFill,
148 OnSetAutofillActionFill)
[email protected]93f2e502013-12-20 09:47:25149 IPC_MESSAGE_HANDLER(AutofillMsg_ClearForm, OnClearForm)
[email protected]6f001a6cf2012-02-09 15:21:53150 IPC_MESSAGE_HANDLER(AutofillMsg_SetAutofillActionPreview,
151 OnSetAutofillActionPreview)
[email protected]93f2e502013-12-20 09:47:25152 IPC_MESSAGE_HANDLER(AutofillMsg_ClearPreviewedForm, OnClearPreviewedForm)
153 IPC_MESSAGE_HANDLER(AutofillMsg_SetNodeText, OnSetNodeText)
[email protected]5c8de6b92012-06-08 21:24:08154 IPC_MESSAGE_HANDLER(AutofillMsg_AcceptDataListSuggestion,
155 OnAcceptDataListSuggestion)
[email protected]e7e83472012-04-05 02:56:26156 IPC_MESSAGE_HANDLER(AutofillMsg_AcceptPasswordAutofillSuggestion,
157 OnAcceptPasswordAutofillSuggestion)
[email protected]82fc1b12013-01-12 00:53:56158 IPC_MESSAGE_HANDLER(AutofillMsg_RequestAutocompleteResult,
159 OnRequestAutocompleteResult)
[email protected]93f2e502013-12-20 09:47:25160 IPC_MESSAGE_HANDLER(AutofillMsg_PageShown, OnPageShown)
[email protected]676126f72011-01-15 00:03:51161 IPC_MESSAGE_UNHANDLED(handled = false)
162 IPC_END_MESSAGE_MAP()
163 return handled;
164}
165
[email protected]2fa18c22011-06-14 23:40:43166void AutofillAgent::DidFinishDocumentLoad(WebFrame* frame) {
[email protected]8acc1962013-03-17 07:04:52167 // 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]5219146e2013-02-28 11:42:30171 // 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]8acc1962013-03-17 07:04:52174 bool has_more_forms = false;
[email protected]5219146e2013-02-28 11:42:30175 if (!frame->parent()) {
[email protected]5219146e2013-02-28 11:42:30176 form_elements_.clear();
[email protected]8acc1962013-03-17 07:04:52177 has_more_forms = form_cache_.ExtractFormsAndFormElements(
178 *frame, kRequiredAutofillFields, &forms, &form_elements_);
[email protected]5219146e2013-02-28 11:42:30179 } else {
180 form_cache_.ExtractForms(*frame, &forms);
181 }
182
[email protected]17b6be72013-04-30 21:33:08183 autofill::FormsSeenState state = has_more_forms ?
184 autofill::PARTIAL_FORMS_SEEN : autofill::NO_SPECIAL_FORMS_SEEN;
185
[email protected]8acc1962013-03-17 07:04:52186 // Always communicate to browser process for topmost frame.
187 if (!forms.empty() || !frame->parent()) {
[email protected]5219146e2013-02-28 11:42:30188 Send(new AutofillHostMsg_FormsSeen(routing_id(), forms,
[email protected]8acc1962013-03-17 07:04:52189 forms_seen_timestamp_,
[email protected]17b6be72013-04-30 21:33:08190 state));
[email protected]5219146e2013-02-28 11:42:30191 }
[email protected]676126f72011-01-15 00:03:51192}
193
[email protected]d7908be2013-01-22 16:50:18194void AutofillAgent::DidCommitProvisionalLoad(WebFrame* frame,
195 bool is_new_navigation) {
[email protected]364481b2013-01-29 01:52:28196 in_flight_request_form_.reset();
[email protected]d7908be2013-01-22 16:50:18197}
198
199void AutofillAgent::FrameDetached(WebFrame* frame) {
[email protected]5219146e2013-02-28 11:42:30200 form_cache_.ResetFrame(*frame);
[email protected]676126f72011-01-15 00:03:51201}
202
[email protected]2a5b1732011-04-01 23:55:55203void AutofillAgent::WillSubmitForm(WebFrame* frame,
204 const WebFormElement& form) {
205 FormData form_data;
[email protected]aaa80c92011-09-16 00:23:54206 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]1d14f582011-09-02 20:42:04213 Send(new AutofillHostMsg_FormSubmitted(routing_id(), form_data,
214 base::TimeTicks::Now()));
[email protected]2a5b1732011-04-01 23:55:55215 }
216}
217
[email protected]2b942c332012-04-25 16:26:26218void 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]b648f242014-02-25 13:49:06222 HidePopup();
[email protected]2b942c332012-04-25 16:26:26223}
224
[email protected]a1221aea2013-11-07 01:31:30225void AutofillAgent::FocusedNodeChanged(const blink::WebNode& node) {
[email protected]f920d6e2013-03-12 20:20:50226 if (node.isNull() || !node.isElementNode())
227 return;
228
[email protected]a1221aea2013-11-07 01:31:30229 blink::WebElement web_element = node.toConst<blink::WebElement>();
[email protected]f920d6e2013-03-12 20:20:50230
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]e5057a22013-04-22 12:41:39240 element_ = *element;
[email protected]e5057a22013-04-22 12:41:39241}
242
[email protected]652ad832013-08-16 11:47:04243void AutofillAgent::OrientationChangeEvent(int orientation) {
[email protected]b648f242014-02-25 13:49:06244 HidePopup();
[email protected]652ad832013-08-16 11:47:04245}
246
[email protected]a1221aea2013-11-07 01:31:30247void AutofillAgent::DidChangeScrollOffset(blink::WebFrame*) {
[email protected]b648f242014-02-25 13:49:06248 HidePopup();
[email protected]2b942c332012-04-25 16:26:26249}
250
[email protected]a1221aea2013-11-07 01:31:30251void AutofillAgent::didRequestAutocomplete(blink::WebFrame* frame,
[email protected]81cd52332012-11-05 20:36:07252 const WebFormElement& form) {
[email protected]fed9d4f2013-12-17 21:32:47253 // 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]ca5190f2013-07-08 11:10:31257 GURL url(frame->document().url());
258 content::SSLStatus ssl_status = render_view()->GetSSLStatusOfFrame(frame);
[email protected]fed9d4f2013-12-17 21:32:47259 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]81cd52332012-11-05 20:36:07264 FormData form_data;
265 if (!in_flight_request_form_.isNull() ||
[email protected]fed9d4f2013-12-17 21:32:47266 (!is_safe && !allow_unsafe) ||
[email protected]81cd52332012-11-05 20:36:07267 !WebFormElementToFormData(form,
268 WebFormControlElement(),
269 REQUIRE_AUTOCOMPLETE,
[email protected]b570fd42012-12-04 22:14:13270 EXTRACT_OPTIONS,
[email protected]81cd52332012-11-05 20:36:07271 &form_data,
272 NULL)) {
273 WebFormElement(form).finishRequestAutocomplete(
[email protected]82fc1b12013-01-12 00:53:56274 WebFormElement::AutocompleteResultErrorDisabled);
[email protected]81cd52332012-11-05 20:36:07275 return;
276 }
277
278 // Cancel any pending Autofill requests and hide any currently showing popups.
279 ++autofill_query_id_;
[email protected]b648f242014-02-25 13:49:06280 HidePopup();
[email protected]81cd52332012-11-05 20:36:07281
282 in_flight_request_form_ = form;
[email protected]ca5190f2013-07-08 11:10:31283 Send(new AutofillHostMsg_RequestAutocomplete(routing_id(), form_data, url));
[email protected]81cd52332012-11-05 20:36:07284}
285
[email protected]90f26252013-02-15 19:48:32286void AutofillAgent::setIgnoreTextChanges(bool ignore) {
287 ignore_text_changes_ = ignore;
288}
289
[email protected]03f10dad2013-04-13 08:23:05290void AutofillAgent::InputElementClicked(const WebInputElement& element,
[email protected]78192082011-01-29 05:43:44291 bool was_focused,
292 bool is_focused) {
[email protected]676126f72011-01-15 00:03:51293 if (was_focused)
[email protected]efeb565f2013-12-12 17:16:41294 ShowSuggestions(element, true, false, true, false);
[email protected]676126f72011-01-15 00:03:51295}
296
[email protected]03f10dad2013-04-13 08:23:05297void AutofillAgent::InputElementLostFocus() {
[email protected]b648f242014-02-25 13:49:06298 HidePopup();
[email protected]676126f72011-01-15 00:03:51299}
300
[email protected]2fa18c22011-06-14 23:40:43301void AutofillAgent::textFieldDidEndEditing(const WebInputElement& element) {
[email protected]ad19b302013-04-03 07:42:19302 password_autofill_agent_->TextFieldDidEndEditing(element);
[email protected]7d24db72011-08-26 06:02:31303 has_shown_autofill_popup_for_current_edit_ = false;
[email protected]2739e0e2011-11-22 22:49:11304 Send(new AutofillHostMsg_DidEndTextFieldEditing(routing_id()));
[email protected]676126f72011-01-15 00:03:51305}
306
[email protected]2fa18c22011-06-14 23:40:43307void AutofillAgent::textFieldDidChange(const WebInputElement& element) {
[email protected]90f26252013-02-15 19:48:32308 if (ignore_text_changes_)
309 return;
310
[email protected]b37043c2012-05-31 17:08:12311 if (did_set_node_text_) {
[email protected]b96a1d12012-11-30 22:44:32312 did_set_node_text_ = false;
313 return;
[email protected]b37043c2012-05-31 17:08:12314 }
315
[email protected]663bd9e2011-03-21 01:07:01316 // We post a task for doing the Autofill as the caret position is not set
[email protected]676126f72011-01-15 00:03:51317 // properly at this point (http://bugs.webkit.org/show_bug.cgi?id=16976) and
318 // it is needed to trigger autofill.
[email protected]5219146e2013-02-28 11:42:30319 weak_ptr_factory_.InvalidateWeakPtrs();
[email protected]d14c5b32013-05-06 05:25:11320 base::MessageLoop::current()->PostTask(
321 FROM_HERE,
322 base::Bind(&AutofillAgent::TextFieldDidChangeImpl,
323 weak_ptr_factory_.GetWeakPtr(),
324 element));
[email protected]676126f72011-01-15 00:03:51325}
326
[email protected]2fa18c22011-06-14 23:40:43327void AutofillAgent::TextFieldDidChangeImpl(const WebInputElement& element) {
[email protected]f9af2832012-12-14 04:20:43328 // 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]3cbdf9362014-01-31 23:12:23333 if (password_generation_agent_ &&
334 password_generation_agent_->TextDidChangeInTextField(element)) {
335 return;
336 }
337
[email protected]ad19b302013-04-03 07:42:19338 if (password_autofill_agent_->TextDidChangeInTextField(element)) {
[email protected]bef7f9272012-04-17 10:47:49339 element_ = element;
[email protected]676126f72011-01-15 00:03:51340 return;
[email protected]e7e83472012-04-05 02:56:26341 }
[email protected]676126f72011-01-15 00:03:51342
[email protected]efeb565f2013-12-12 17:16:41343 ShowSuggestions(element, false, true, false, false);
[email protected]7d24db72011-08-26 06:02:31344
[email protected]1ecbe862012-10-05 01:29:14345 FormData form;
346 FormFieldData field;
[email protected]283ba4ca2011-09-22 05:25:08347 if (FindFormAndFieldForInputElement(element, &form, &field, REQUIRE_NONE)) {
[email protected]1d14f582011-09-02 20:42:04348 Send(new AutofillHostMsg_TextFieldDidChange(routing_id(), form, field,
349 base::TimeTicks::Now()));
350 }
[email protected]676126f72011-01-15 00:03:51351}
352
[email protected]2fa18c22011-06-14 23:40:43353void AutofillAgent::textFieldDidReceiveKeyDown(const WebInputElement& element,
354 const WebKeyboardEvent& event) {
[email protected]ad19b302013-04-03 07:42:19355 if (password_autofill_agent_->TextFieldHandlingKeyDown(element, event)) {
[email protected]bef7f9272012-04-17 10:47:49356 element_ = element;
[email protected]fc1964a2011-01-20 19:33:07357 return;
[email protected]e7e83472012-04-05 02:56:26358 }
[email protected]676126f72011-01-15 00:03:51359
360 if (event.windowsKeyCode == ui::VKEY_DOWN ||
361 event.windowsKeyCode == ui::VKEY_UP)
[email protected]efeb565f2013-12-12 17:16:41362 ShowSuggestions(element, true, true, true, false);
363}
364
365void AutofillAgent::openTextDataListChooser(const WebInputElement& element) {
366 ShowSuggestions(element, true, false, false, true);
[email protected]676126f72011-01-15 00:03:51367}
368
[email protected]d5ca8fb2013-04-11 17:54:31369void AutofillAgent::AcceptDataListSuggestion(
370 const base::string16& suggested_value) {
371 base::string16 new_value = suggested_value;
[email protected]71a90b32012-05-18 09:16:53372 // 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]d5ca8fb2013-04-11 17:54:31376 std::vector<base::string16> parts;
[email protected]71a90b32012-05-18 09:16:53377
378 base::SplitStringDontTrim(element_.editingValue(), ',', &parts);
379 if (parts.size() == 0)
[email protected]d5ca8fb2013-04-11 17:54:31380 parts.push_back(base::string16());
[email protected]71a90b32012-05-18 09:16:53381
[email protected]d5ca8fb2013-04-11 17:54:31382 base::string16 last_part = parts.back();
[email protected]71a90b32012-05-18 09:16:53383 // 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]663bd9e2011-03-21 01:07:01398void AutofillAgent::OnFormDataFilled(int query_id,
[email protected]1ecbe862012-10-05 01:29:14399 const FormData& form) {
[email protected]a2ef54c2011-10-10 16:20:31400 if (!render_view()->GetWebView() || query_id != autofill_query_id_)
[email protected]679f128f2010-07-22 22:57:44401 return;
402
[email protected]bef7f9272012-04-17 10:47:49403 was_query_node_autofilled_ = element_.isAutofilled();
[email protected]6f001a6cf2012-02-09 15:21:53404
[email protected]679f128f2010-07-22 22:57:44405 switch (autofill_action_) {
406 case AUTOFILL_FILL:
[email protected]bef7f9272012-04-17 10:47:49407 FillForm(form, element_);
[email protected]1d14f582011-09-02 20:42:04408 Send(new AutofillHostMsg_DidFillAutofillFormData(routing_id(),
409 base::TimeTicks::Now()));
[email protected]679f128f2010-07-22 22:57:44410 break;
411 case AUTOFILL_PREVIEW:
[email protected]bef7f9272012-04-17 10:47:49412 PreviewForm(form, element_);
[email protected]7d24db72011-08-26 06:02:31413 Send(new AutofillHostMsg_DidPreviewAutofillFormData(routing_id()));
[email protected]679f128f2010-07-22 22:57:44414 break;
415 default:
416 NOTREACHED();
417 }
418 autofill_action_ = AUTOFILL_NONE;
[email protected]e47aec52010-08-12 00:50:30419}
420
[email protected]3eb5728c2011-06-20 22:32:24421void AutofillAgent::OnFieldTypePredictionsAvailable(
422 const std::vector<FormDataPredictions>& forms) {
423 for (size_t i = 0; i < forms.size(); ++i) {
[email protected]aaa80c92011-09-16 00:23:54424 form_cache_.ShowPredictions(forms[i]);
[email protected]3eb5728c2011-06-20 22:32:24425 }
426}
427
[email protected]6f001a6cf2012-02-09 15:21:53428void AutofillAgent::OnSetAutofillActionFill() {
429 autofill_action_ = AUTOFILL_FILL;
430}
431
432void AutofillAgent::OnClearForm() {
[email protected]bef7f9272012-04-17 10:47:49433 form_cache_.ClearFormWithElement(element_);
[email protected]6f001a6cf2012-02-09 15:21:53434}
435
436void AutofillAgent::OnSetAutofillActionPreview() {
437 autofill_action_ = AUTOFILL_PREVIEW;
438}
439
440void AutofillAgent::OnClearPreviewedForm() {
[email protected]93f2e502013-12-20 09:47:25441 if (!element_.isNull()) {
[email protected]b5b1d69f2014-01-23 23:49:32442 if (password_autofill_agent_->DidClearAutofillSelection(element_))
443 return;
444
[email protected]93f2e502013-12-20 09:47:25445 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]6f001a6cf2012-02-09 15:21:53454}
455
[email protected]d5ca8fb2013-04-11 17:54:31456void AutofillAgent::OnSetNodeText(const base::string16& value) {
[email protected]bef7f9272012-04-17 10:47:49457 SetNodeText(value, &element_);
[email protected]6f001a6cf2012-02-09 15:21:53458}
459
[email protected]d5ca8fb2013-04-11 17:54:31460void AutofillAgent::OnAcceptDataListSuggestion(const base::string16& value) {
[email protected]5c8de6b92012-06-08 21:24:08461 AcceptDataListSuggestion(value);
462}
463
[email protected]d5ca8fb2013-04-11 17:54:31464void AutofillAgent::OnAcceptPasswordAutofillSuggestion(
[email protected]d619ba642013-11-22 15:34:33465 const base::string16& username) {
[email protected]e7e83472012-04-05 02:56:26466 // 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]ad19b302013-04-03 07:42:19469 bool handled = password_autofill_agent_->DidAcceptAutofillSuggestion(
[email protected]bef7f9272012-04-17 10:47:49470 element_,
[email protected]d619ba642013-11-22 15:34:33471 username);
[email protected]e7e83472012-04-05 02:56:26472 DCHECK(handled);
473}
474
[email protected]82fc1b12013-01-12 00:53:56475void AutofillAgent::OnRequestAutocompleteResult(
476 WebFormElement::AutocompleteResult result, const FormData& form_data) {
[email protected]364481b2013-01-29 01:52:28477 if (in_flight_request_form_.isNull())
478 return;
479
[email protected]9793fb232013-06-21 10:25:35480 if (result == WebFormElement::AutocompleteResultSuccess) {
[email protected]82fc1b12013-01-12 00:53:56481 FillFormIncludingNonFocusableElements(form_data, in_flight_request_form_);
[email protected]9793fb232013-06-21 10:25:35482 if (!in_flight_request_form_.checkValidityWithoutDispatchingEvents())
483 result = WebFormElement::AutocompleteResultErrorInvalid;
484 }
[email protected]364481b2013-01-29 01:52:28485
[email protected]81cd52332012-11-05 20:36:07486 in_flight_request_form_.finishRequestAutocomplete(result);
487 in_flight_request_form_.reset();
488}
489
[email protected]63dc5402013-08-07 19:57:35490void AutofillAgent::OnPageShown() {
[email protected]d3ae9ed42013-01-14 23:31:01491}
492
[email protected]663bd9e2011-03-21 01:07:01493void AutofillAgent::ShowSuggestions(const WebInputElement& element,
[email protected]78192082011-01-29 05:43:44494 bool autofill_on_empty_values,
495 bool requires_caret_at_end,
[email protected]efeb565f2013-12-12 17:16:41496 bool display_warning_if_disabled,
497 bool datalist_only) {
[email protected]bef7f9272012-04-17 10:47:49498 if (!element.isEnabled() || element.isReadOnly() || !element.isTextField() ||
[email protected]efeb565f2013-12-12 17:16:41499 element.isPasswordField())
500 return;
501 if (!datalist_only && !element.suggestedValue().isEmpty())
[email protected]e47aec52010-08-12 00:50:30502 return;
503
[email protected]1c69f8b72012-03-14 03:18:50504 // Don't attempt to autofill with values that are too large or if filling
505 // criteria are not met.
[email protected]71a90b32012-05-18 09:16:53506 WebString value = element.editingValue();
[email protected]efeb565f2013-12-12 17:16:41507 if (!datalist_only &&
[email protected]d86263dcd2014-01-09 10:35:21508 (value.length() > kMaxDataLength ||
[email protected]efeb565f2013-12-12 17:16:41509 (!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]1c69f8b72012-03-14 03:18:50513 // Any popup currently showing is obsolete.
[email protected]b648f242014-02-25 13:49:06514 HidePopup();
[email protected]e47aec52010-08-12 00:50:30515 return;
[email protected]1c69f8b72012-03-14 03:18:50516 }
[email protected]e47aec52010-08-12 00:50:30517
[email protected]bef7f9272012-04-17 10:47:49518 element_ = element;
[email protected]b648f242014-02-25 13:49:06519 if (password_autofill_agent_->ShowSuggestions(element)) {
520 is_popup_possibly_visible_ = true;
[email protected]c66ba982013-04-05 09:56:17521 return;
[email protected]b648f242014-02-25 13:49:06522 }
[email protected]bef7f9272012-04-17 10:47:49523
[email protected]33915c12013-07-11 23:16:33524 // 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]5af43c132012-11-06 07:03:44527 // 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]d5ca8fb2013-04-11 17:54:31529 const base::string16 autocomplete_attribute =
530 element.getAttribute("autocomplete");
[email protected]33915c12013-07-11 23:16:33531 if (LowerCaseEqualsASCII(autocomplete_attribute, "off"))
532 display_warning_if_disabled = false;
[email protected]bef7f9272012-04-17 10:47:49533
[email protected]efeb565f2013-12-12 17:16:41534 QueryAutofillSuggestions(element,
535 display_warning_if_disabled,
536 datalist_only);
[email protected]77bb0da2010-11-20 01:55:30537}
538
[email protected]cd0a3b72011-08-02 06:16:19539void AutofillAgent::QueryAutofillSuggestions(const WebInputElement& element,
[email protected]efeb565f2013-12-12 17:16:41540 bool display_warning_if_disabled,
541 bool datalist_only) {
[email protected]e69b5f72013-01-24 00:59:13542 if (!element.document().frame())
543 return;
544
[email protected]77bb0da2010-11-20 01:55:30545 static int query_counter = 0;
546 autofill_query_id_ = query_counter++;
[email protected]6bcd58a2010-11-25 02:31:20547 display_warning_if_disabled_ = display_warning_if_disabled;
[email protected]77bb0da2010-11-20 01:55:30548
[email protected]5af43c132012-11-06 07:03:44549 // 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]5af43c132012-11-06 07:03:44554 const RequirementsMask requirements =
555 element.autoComplete() ? REQUIRE_AUTOCOMPLETE : REQUIRE_NONE;
556
[email protected]1ecbe862012-10-05 01:29:14557 FormData form;
558 FormFieldData field;
[email protected]5af43c132012-11-06 07:03:44559 if (!FindFormAndFieldForInputElement(element, &form, &field, requirements)) {
[email protected]7837be62011-01-18 23:45:08560 // If we didn't find the cached form, at least let autocomplete have a shot
561 // at providing suggestions.
[email protected]aaa80c92011-09-16 00:23:54562 WebFormControlElementToFormField(element, EXTRACT_VALUE, &field);
[email protected]7837be62011-01-18 23:45:08563 }
[email protected]efeb565f2013-12-12 17:16:41564 if (datalist_only)
565 field.should_autocomplete = false;
[email protected]77bb0da2010-11-20 01:55:30566
[email protected]f920d6e2013-03-12 20:20:50567 gfx::RectF bounding_box_scaled =
568 GetScaledBoundingBox(web_view_->pageScaleFactor(), &element_);
[email protected]73a087f2013-02-06 07:02:06569
[email protected]5c8de6b92012-06-08 21:24:08570 // Find the datalist values and send them to the browser process.
[email protected]d5ca8fb2013-04-11 17:54:31571 std::vector<base::string16> data_list_values;
572 std::vector<base::string16> data_list_labels;
[email protected]efeb565f2013-12-12 17:16:41573 GetDataListSuggestions(element_,
574 datalist_only,
575 &data_list_values,
576 &data_list_labels);
[email protected]ead7fb02013-07-18 18:50:12577 TrimStringVectorForIPC(&data_list_values);
578 TrimStringVectorForIPC(&data_list_labels);
[email protected]5c8de6b92012-06-08 21:24:08579
[email protected]b648f242014-02-25 13:49:06580 is_popup_possibly_visible_ = true;
[email protected]5c8de6b92012-06-08 21:24:08581 Send(new AutofillHostMsg_SetDataList(routing_id(),
582 data_list_values,
[email protected]ead7fb02013-07-18 18:50:12583 data_list_labels));
[email protected]5c8de6b92012-06-08 21:24:08584
[email protected]2739e0e2011-11-22 22:49:11585 Send(new AutofillHostMsg_QueryFormFieldAutofill(routing_id(),
586 autofill_query_id_,
587 form,
588 field,
[email protected]73a087f2013-02-06 07:02:06589 bounding_box_scaled,
[email protected]2739e0e2011-11-22 22:49:11590 display_warning_if_disabled));
[email protected]e47aec52010-08-12 00:50:30591}
592
[email protected]663bd9e2011-03-21 01:07:01593void AutofillAgent::FillAutofillFormData(const WebNode& node,
[email protected]78192082011-01-29 05:43:44594 int unique_id,
[email protected]663bd9e2011-03-21 01:07:01595 AutofillAction action) {
[email protected]bef7f9272012-04-17 10:47:49596 DCHECK_GT(unique_id, 0);
597
[email protected]679f128f2010-07-22 22:57:44598 static int query_counter = 0;
599 autofill_query_id_ = query_counter++;
600
[email protected]1ecbe862012-10-05 01:29:14601 FormData form;
602 FormFieldData field;
[email protected]283ba4ca2011-09-22 05:25:08603 if (!FindFormAndFieldForInputElement(node.toConst<WebInputElement>(), &form,
604 &field, REQUIRE_AUTOCOMPLETE)) {
[email protected]679f128f2010-07-22 22:57:44605 return;
[email protected]283ba4ca2011-09-22 05:25:08606 }
[email protected]679f128f2010-07-22 22:57:44607
608 autofill_action_ = action;
[email protected]663bd9e2011-03-21 01:07:01609 Send(new AutofillHostMsg_FillAutofillFormData(
[email protected]676126f72011-01-15 00:03:51610 routing_id(), autofill_query_id_, form, field, unique_id));
[email protected]679f128f2010-07-22 22:57:44611}
612
[email protected]d5ca8fb2013-04-11 17:54:31613void AutofillAgent::SetNodeText(const base::string16& value,
[email protected]a1221aea2013-11-07 01:31:30614 blink::WebInputElement* node) {
[email protected]b37043c2012-05-31 17:08:12615 did_set_node_text_ = true;
[email protected]2ee19ec2014-02-25 12:44:31616 node->setEditingValue(value.substr(0, node->maxLength()));
[email protected]6f001a6cf2012-02-09 15:21:53617}
618
[email protected]b648f242014-02-25 13:49:06619void AutofillAgent::HidePopup() {
620 if (!is_popup_possibly_visible_)
621 return;
622
[email protected]b5b1d69f2014-01-23 23:49:32623 if (!element_.isNull())
624 OnClearPreviewedForm();
625
[email protected]b648f242014-02-25 13:49:06626 is_popup_possibly_visible_ = false;
627 Send(new AutofillHostMsg_HidePopup(routing_id()));
[email protected]81cd52332012-11-05 20:36:07628}
629
[email protected]704b6232013-09-16 21:19:12630// TODO(isherman): Decide if we want to support non-password autofill with AJAX.
[email protected]17b6be72013-04-30 21:33:08631void AutofillAgent::didAssociateFormControls(
[email protected]a1221aea2013-11-07 01:31:30632 const blink::WebVector<blink::WebNode>& nodes) {
[email protected]704b6232013-09-16 21:19:12633 for (size_t i = 0; i < nodes.size(); ++i) {
[email protected]a1221aea2013-11-07 01:31:30634 blink::WebFrame* frame = nodes[i].document().frame();
[email protected]704b6232013-09-16 21:19:12635 // 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]17b6be72013-04-30 21:33:08642}
643
[email protected]78192082011-01-29 05:43:44644} // namespace autofill