blob: 979976f72c14ee71c8dfd95e7bf5c195d680aedb [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"
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]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;
55using blink::WebNodeCollection;
56using 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]bef7f9272012-04-17 10:47:4969 WebNodeCollection options = element.dataListOptions();
70 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]2c305252013-04-26 19:57:05130 weak_ptr_factory_(this) {
[email protected]a2ef54c2011-10-10 16:20:31131 render_view->GetWebView()->setAutofillClient(this);
[email protected]03f10dad2013-04-13 08:23:05132
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]679f128f2010-07-22 22:57:44136}
137
[email protected]a64fdc32013-07-31 09:21:13138AutofillAgent::~AutofillAgent() {}
[email protected]d2f05d02011-01-27 18:51:01139
[email protected]663bd9e2011-03-21 01:07:01140bool AutofillAgent::OnMessageReceived(const IPC::Message& message) {
[email protected]676126f72011-01-15 00:03:51141 bool handled = true;
[email protected]663bd9e2011-03-21 01:07:01142 IPC_BEGIN_MESSAGE_MAP(AutofillAgent, message)
[email protected]663bd9e2011-03-21 01:07:01143 IPC_MESSAGE_HANDLER(AutofillMsg_FormDataFilled, OnFormDataFilled)
[email protected]3eb5728c2011-06-20 22:32:24144 IPC_MESSAGE_HANDLER(AutofillMsg_FieldTypePredictionsAvailable,
145 OnFieldTypePredictionsAvailable)
[email protected]6f001a6cf2012-02-09 15:21:53146 IPC_MESSAGE_HANDLER(AutofillMsg_SetAutofillActionFill,
147 OnSetAutofillActionFill)
[email protected]93f2e502013-12-20 09:47:25148 IPC_MESSAGE_HANDLER(AutofillMsg_ClearForm, OnClearForm)
[email protected]6f001a6cf2012-02-09 15:21:53149 IPC_MESSAGE_HANDLER(AutofillMsg_SetAutofillActionPreview,
150 OnSetAutofillActionPreview)
[email protected]93f2e502013-12-20 09:47:25151 IPC_MESSAGE_HANDLER(AutofillMsg_ClearPreviewedForm, OnClearPreviewedForm)
152 IPC_MESSAGE_HANDLER(AutofillMsg_SetNodeText, OnSetNodeText)
[email protected]5c8de6b92012-06-08 21:24:08153 IPC_MESSAGE_HANDLER(AutofillMsg_AcceptDataListSuggestion,
154 OnAcceptDataListSuggestion)
[email protected]e7e83472012-04-05 02:56:26155 IPC_MESSAGE_HANDLER(AutofillMsg_AcceptPasswordAutofillSuggestion,
156 OnAcceptPasswordAutofillSuggestion)
[email protected]82fc1b12013-01-12 00:53:56157 IPC_MESSAGE_HANDLER(AutofillMsg_RequestAutocompleteResult,
158 OnRequestAutocompleteResult)
[email protected]93f2e502013-12-20 09:47:25159 IPC_MESSAGE_HANDLER(AutofillMsg_PageShown, OnPageShown)
[email protected]676126f72011-01-15 00:03:51160 IPC_MESSAGE_UNHANDLED(handled = false)
161 IPC_END_MESSAGE_MAP()
162 return handled;
163}
164
[email protected]2fa18c22011-06-14 23:40:43165void AutofillAgent::DidFinishDocumentLoad(WebFrame* frame) {
[email protected]8acc1962013-03-17 07:04:52166 // 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]5219146e2013-02-28 11:42:30170 // 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]8acc1962013-03-17 07:04:52173 bool has_more_forms = false;
[email protected]5219146e2013-02-28 11:42:30174 if (!frame->parent()) {
[email protected]5219146e2013-02-28 11:42:30175 form_elements_.clear();
[email protected]8acc1962013-03-17 07:04:52176 has_more_forms = form_cache_.ExtractFormsAndFormElements(
177 *frame, kRequiredAutofillFields, &forms, &form_elements_);
[email protected]5219146e2013-02-28 11:42:30178 } else {
179 form_cache_.ExtractForms(*frame, &forms);
180 }
181
[email protected]17b6be72013-04-30 21:33:08182 autofill::FormsSeenState state = has_more_forms ?
183 autofill::PARTIAL_FORMS_SEEN : autofill::NO_SPECIAL_FORMS_SEEN;
184
[email protected]8acc1962013-03-17 07:04:52185 // Always communicate to browser process for topmost frame.
186 if (!forms.empty() || !frame->parent()) {
[email protected]5219146e2013-02-28 11:42:30187 Send(new AutofillHostMsg_FormsSeen(routing_id(), forms,
[email protected]8acc1962013-03-17 07:04:52188 forms_seen_timestamp_,
[email protected]17b6be72013-04-30 21:33:08189 state));
[email protected]5219146e2013-02-28 11:42:30190 }
[email protected]676126f72011-01-15 00:03:51191}
192
[email protected]d7908be2013-01-22 16:50:18193void AutofillAgent::DidCommitProvisionalLoad(WebFrame* frame,
194 bool is_new_navigation) {
[email protected]364481b2013-01-29 01:52:28195 in_flight_request_form_.reset();
[email protected]d7908be2013-01-22 16:50:18196}
197
198void AutofillAgent::FrameDetached(WebFrame* frame) {
[email protected]5219146e2013-02-28 11:42:30199 form_cache_.ResetFrame(*frame);
[email protected]676126f72011-01-15 00:03:51200}
201
[email protected]2a5b1732011-04-01 23:55:55202void AutofillAgent::WillSubmitForm(WebFrame* frame,
203 const WebFormElement& form) {
204 FormData form_data;
[email protected]aaa80c92011-09-16 00:23:54205 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]1d14f582011-09-02 20:42:04212 Send(new AutofillHostMsg_FormSubmitted(routing_id(), form_data,
213 base::TimeTicks::Now()));
[email protected]2a5b1732011-04-01 23:55:55214 }
215}
216
[email protected]2b942c332012-04-25 16:26:26217void 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]ddabf20d2013-07-19 21:48:20221 HideAutofillUI();
[email protected]2b942c332012-04-25 16:26:26222}
223
[email protected]a1221aea2013-11-07 01:31:30224void AutofillAgent::FocusedNodeChanged(const blink::WebNode& node) {
[email protected]f920d6e2013-03-12 20:20:50225 if (node.isNull() || !node.isElementNode())
226 return;
227
[email protected]a1221aea2013-11-07 01:31:30228 blink::WebElement web_element = node.toConst<blink::WebElement>();
[email protected]f920d6e2013-03-12 20:20:50229
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]e5057a22013-04-22 12:41:39239 element_ = *element;
[email protected]e5057a22013-04-22 12:41:39240}
241
[email protected]652ad832013-08-16 11:47:04242void AutofillAgent::OrientationChangeEvent(int orientation) {
243 HideAutofillUI();
244}
245
[email protected]a1221aea2013-11-07 01:31:30246void AutofillAgent::DidChangeScrollOffset(blink::WebFrame*) {
[email protected]ddabf20d2013-07-19 21:48:20247 HideAutofillUI();
[email protected]2b942c332012-04-25 16:26:26248}
249
[email protected]a1221aea2013-11-07 01:31:30250void AutofillAgent::didRequestAutocomplete(blink::WebFrame* frame,
[email protected]81cd52332012-11-05 20:36:07251 const WebFormElement& form) {
[email protected]fed9d4f2013-12-17 21:32:47252 // 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]ca5190f2013-07-08 11:10:31256 GURL url(frame->document().url());
257 content::SSLStatus ssl_status = render_view()->GetSSLStatusOfFrame(frame);
[email protected]fed9d4f2013-12-17 21:32:47258 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]81cd52332012-11-05 20:36:07263 FormData form_data;
264 if (!in_flight_request_form_.isNull() ||
[email protected]fed9d4f2013-12-17 21:32:47265 (!is_safe && !allow_unsafe) ||
[email protected]81cd52332012-11-05 20:36:07266 !WebFormElementToFormData(form,
267 WebFormControlElement(),
268 REQUIRE_AUTOCOMPLETE,
[email protected]b570fd42012-12-04 22:14:13269 EXTRACT_OPTIONS,
[email protected]81cd52332012-11-05 20:36:07270 &form_data,
271 NULL)) {
272 WebFormElement(form).finishRequestAutocomplete(
[email protected]82fc1b12013-01-12 00:53:56273 WebFormElement::AutocompleteResultErrorDisabled);
[email protected]81cd52332012-11-05 20:36:07274 return;
275 }
276
277 // Cancel any pending Autofill requests and hide any currently showing popups.
278 ++autofill_query_id_;
[email protected]ddabf20d2013-07-19 21:48:20279 HideAutofillUI();
[email protected]81cd52332012-11-05 20:36:07280
281 in_flight_request_form_ = form;
[email protected]ca5190f2013-07-08 11:10:31282 Send(new AutofillHostMsg_RequestAutocomplete(routing_id(), form_data, url));
[email protected]81cd52332012-11-05 20:36:07283}
284
[email protected]90f26252013-02-15 19:48:32285void AutofillAgent::setIgnoreTextChanges(bool ignore) {
286 ignore_text_changes_ = ignore;
287}
288
[email protected]03f10dad2013-04-13 08:23:05289void AutofillAgent::InputElementClicked(const WebInputElement& element,
[email protected]78192082011-01-29 05:43:44290 bool was_focused,
291 bool is_focused) {
[email protected]676126f72011-01-15 00:03:51292 if (was_focused)
[email protected]efeb565f2013-12-12 17:16:41293 ShowSuggestions(element, true, false, true, false);
[email protected]676126f72011-01-15 00:03:51294}
295
[email protected]03f10dad2013-04-13 08:23:05296void AutofillAgent::InputElementLostFocus() {
[email protected]ddabf20d2013-07-19 21:48:20297 HideAutofillUI();
[email protected]676126f72011-01-15 00:03:51298}
299
[email protected]2fa18c22011-06-14 23:40:43300void AutofillAgent::textFieldDidEndEditing(const WebInputElement& element) {
[email protected]ad19b302013-04-03 07:42:19301 password_autofill_agent_->TextFieldDidEndEditing(element);
[email protected]7d24db72011-08-26 06:02:31302 has_shown_autofill_popup_for_current_edit_ = false;
[email protected]2739e0e2011-11-22 22:49:11303 Send(new AutofillHostMsg_DidEndTextFieldEditing(routing_id()));
[email protected]676126f72011-01-15 00:03:51304}
305
[email protected]2fa18c22011-06-14 23:40:43306void AutofillAgent::textFieldDidChange(const WebInputElement& element) {
[email protected]90f26252013-02-15 19:48:32307 if (ignore_text_changes_)
308 return;
309
[email protected]b37043c2012-05-31 17:08:12310 if (did_set_node_text_) {
[email protected]b96a1d12012-11-30 22:44:32311 did_set_node_text_ = false;
312 return;
[email protected]b37043c2012-05-31 17:08:12313 }
314
[email protected]663bd9e2011-03-21 01:07:01315 // We post a task for doing the Autofill as the caret position is not set
[email protected]676126f72011-01-15 00:03:51316 // properly at this point (http://bugs.webkit.org/show_bug.cgi?id=16976) and
317 // it is needed to trigger autofill.
[email protected]5219146e2013-02-28 11:42:30318 weak_ptr_factory_.InvalidateWeakPtrs();
[email protected]d14c5b32013-05-06 05:25:11319 base::MessageLoop::current()->PostTask(
320 FROM_HERE,
321 base::Bind(&AutofillAgent::TextFieldDidChangeImpl,
322 weak_ptr_factory_.GetWeakPtr(),
323 element));
[email protected]676126f72011-01-15 00:03:51324}
325
[email protected]2fa18c22011-06-14 23:40:43326void AutofillAgent::TextFieldDidChangeImpl(const WebInputElement& element) {
[email protected]f9af2832012-12-14 04:20:43327 // 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]3cbdf9362014-01-31 23:12:23332 if (password_generation_agent_ &&
333 password_generation_agent_->TextDidChangeInTextField(element)) {
334 return;
335 }
336
[email protected]ad19b302013-04-03 07:42:19337 if (password_autofill_agent_->TextDidChangeInTextField(element)) {
[email protected]bef7f9272012-04-17 10:47:49338 element_ = element;
[email protected]676126f72011-01-15 00:03:51339 return;
[email protected]e7e83472012-04-05 02:56:26340 }
[email protected]676126f72011-01-15 00:03:51341
[email protected]efeb565f2013-12-12 17:16:41342 ShowSuggestions(element, false, true, false, false);
[email protected]7d24db72011-08-26 06:02:31343
[email protected]1ecbe862012-10-05 01:29:14344 FormData form;
345 FormFieldData field;
[email protected]283ba4ca2011-09-22 05:25:08346 if (FindFormAndFieldForInputElement(element, &form, &field, REQUIRE_NONE)) {
[email protected]1d14f582011-09-02 20:42:04347 Send(new AutofillHostMsg_TextFieldDidChange(routing_id(), form, field,
348 base::TimeTicks::Now()));
349 }
[email protected]676126f72011-01-15 00:03:51350}
351
[email protected]2fa18c22011-06-14 23:40:43352void AutofillAgent::textFieldDidReceiveKeyDown(const WebInputElement& element,
353 const WebKeyboardEvent& event) {
[email protected]ad19b302013-04-03 07:42:19354 if (password_autofill_agent_->TextFieldHandlingKeyDown(element, event)) {
[email protected]bef7f9272012-04-17 10:47:49355 element_ = element;
[email protected]fc1964a2011-01-20 19:33:07356 return;
[email protected]e7e83472012-04-05 02:56:26357 }
[email protected]676126f72011-01-15 00:03:51358
359 if (event.windowsKeyCode == ui::VKEY_DOWN ||
360 event.windowsKeyCode == ui::VKEY_UP)
[email protected]efeb565f2013-12-12 17:16:41361 ShowSuggestions(element, true, true, true, false);
362}
363
364void AutofillAgent::openTextDataListChooser(const WebInputElement& element) {
365 ShowSuggestions(element, true, false, false, true);
[email protected]676126f72011-01-15 00:03:51366}
367
[email protected]d5ca8fb2013-04-11 17:54:31368void AutofillAgent::AcceptDataListSuggestion(
369 const base::string16& suggested_value) {
370 base::string16 new_value = suggested_value;
[email protected]71a90b32012-05-18 09:16:53371 // 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]d5ca8fb2013-04-11 17:54:31375 std::vector<base::string16> parts;
[email protected]71a90b32012-05-18 09:16:53376
377 base::SplitStringDontTrim(element_.editingValue(), ',', &parts);
378 if (parts.size() == 0)
[email protected]d5ca8fb2013-04-11 17:54:31379 parts.push_back(base::string16());
[email protected]71a90b32012-05-18 09:16:53380
[email protected]d5ca8fb2013-04-11 17:54:31381 base::string16 last_part = parts.back();
[email protected]71a90b32012-05-18 09:16:53382 // 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]663bd9e2011-03-21 01:07:01397void AutofillAgent::OnFormDataFilled(int query_id,
[email protected]1ecbe862012-10-05 01:29:14398 const FormData& form) {
[email protected]a2ef54c2011-10-10 16:20:31399 if (!render_view()->GetWebView() || query_id != autofill_query_id_)
[email protected]679f128f2010-07-22 22:57:44400 return;
401
[email protected]bef7f9272012-04-17 10:47:49402 was_query_node_autofilled_ = element_.isAutofilled();
[email protected]6f001a6cf2012-02-09 15:21:53403
[email protected]679f128f2010-07-22 22:57:44404 switch (autofill_action_) {
405 case AUTOFILL_FILL:
[email protected]bef7f9272012-04-17 10:47:49406 FillForm(form, element_);
[email protected]1d14f582011-09-02 20:42:04407 Send(new AutofillHostMsg_DidFillAutofillFormData(routing_id(),
408 base::TimeTicks::Now()));
[email protected]679f128f2010-07-22 22:57:44409 break;
410 case AUTOFILL_PREVIEW:
[email protected]bef7f9272012-04-17 10:47:49411 PreviewForm(form, element_);
[email protected]7d24db72011-08-26 06:02:31412 Send(new AutofillHostMsg_DidPreviewAutofillFormData(routing_id()));
[email protected]679f128f2010-07-22 22:57:44413 break;
414 default:
415 NOTREACHED();
416 }
417 autofill_action_ = AUTOFILL_NONE;
[email protected]e47aec52010-08-12 00:50:30418}
419
[email protected]3eb5728c2011-06-20 22:32:24420void AutofillAgent::OnFieldTypePredictionsAvailable(
421 const std::vector<FormDataPredictions>& forms) {
422 for (size_t i = 0; i < forms.size(); ++i) {
[email protected]aaa80c92011-09-16 00:23:54423 form_cache_.ShowPredictions(forms[i]);
[email protected]3eb5728c2011-06-20 22:32:24424 }
425}
426
[email protected]6f001a6cf2012-02-09 15:21:53427void AutofillAgent::OnSetAutofillActionFill() {
428 autofill_action_ = AUTOFILL_FILL;
429}
430
431void AutofillAgent::OnClearForm() {
[email protected]bef7f9272012-04-17 10:47:49432 form_cache_.ClearFormWithElement(element_);
[email protected]6f001a6cf2012-02-09 15:21:53433}
434
435void AutofillAgent::OnSetAutofillActionPreview() {
436 autofill_action_ = AUTOFILL_PREVIEW;
437}
438
439void AutofillAgent::OnClearPreviewedForm() {
[email protected]93f2e502013-12-20 09:47:25440 if (!element_.isNull()) {
[email protected]b5b1d69f2014-01-23 23:49:32441 if (password_autofill_agent_->DidClearAutofillSelection(element_))
442 return;
443
[email protected]93f2e502013-12-20 09:47:25444 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]6f001a6cf2012-02-09 15:21:53453}
454
[email protected]d5ca8fb2013-04-11 17:54:31455void AutofillAgent::OnSetNodeText(const base::string16& value) {
[email protected]bef7f9272012-04-17 10:47:49456 SetNodeText(value, &element_);
[email protected]6f001a6cf2012-02-09 15:21:53457}
458
[email protected]d5ca8fb2013-04-11 17:54:31459void AutofillAgent::OnAcceptDataListSuggestion(const base::string16& value) {
[email protected]5c8de6b92012-06-08 21:24:08460 AcceptDataListSuggestion(value);
461}
462
[email protected]d5ca8fb2013-04-11 17:54:31463void AutofillAgent::OnAcceptPasswordAutofillSuggestion(
[email protected]d619ba642013-11-22 15:34:33464 const base::string16& username) {
[email protected]e7e83472012-04-05 02:56:26465 // 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]ad19b302013-04-03 07:42:19468 bool handled = password_autofill_agent_->DidAcceptAutofillSuggestion(
[email protected]bef7f9272012-04-17 10:47:49469 element_,
[email protected]d619ba642013-11-22 15:34:33470 username);
[email protected]e7e83472012-04-05 02:56:26471 DCHECK(handled);
472}
473
[email protected]82fc1b12013-01-12 00:53:56474void AutofillAgent::OnRequestAutocompleteResult(
475 WebFormElement::AutocompleteResult result, const FormData& form_data) {
[email protected]364481b2013-01-29 01:52:28476 if (in_flight_request_form_.isNull())
477 return;
478
[email protected]9793fb232013-06-21 10:25:35479 if (result == WebFormElement::AutocompleteResultSuccess) {
[email protected]82fc1b12013-01-12 00:53:56480 FillFormIncludingNonFocusableElements(form_data, in_flight_request_form_);
[email protected]9793fb232013-06-21 10:25:35481 if (!in_flight_request_form_.checkValidityWithoutDispatchingEvents())
482 result = WebFormElement::AutocompleteResultErrorInvalid;
483 }
[email protected]364481b2013-01-29 01:52:28484
[email protected]81cd52332012-11-05 20:36:07485 in_flight_request_form_.finishRequestAutocomplete(result);
486 in_flight_request_form_.reset();
487}
488
[email protected]63dc5402013-08-07 19:57:35489void AutofillAgent::OnPageShown() {
[email protected]d3ae9ed42013-01-14 23:31:01490}
491
[email protected]663bd9e2011-03-21 01:07:01492void AutofillAgent::ShowSuggestions(const WebInputElement& element,
[email protected]78192082011-01-29 05:43:44493 bool autofill_on_empty_values,
494 bool requires_caret_at_end,
[email protected]efeb565f2013-12-12 17:16:41495 bool display_warning_if_disabled,
496 bool datalist_only) {
[email protected]bef7f9272012-04-17 10:47:49497 if (!element.isEnabled() || element.isReadOnly() || !element.isTextField() ||
[email protected]efeb565f2013-12-12 17:16:41498 element.isPasswordField())
499 return;
500 if (!datalist_only && !element.suggestedValue().isEmpty())
[email protected]e47aec52010-08-12 00:50:30501 return;
502
[email protected]1c69f8b72012-03-14 03:18:50503 // Don't attempt to autofill with values that are too large or if filling
504 // criteria are not met.
[email protected]71a90b32012-05-18 09:16:53505 WebString value = element.editingValue();
[email protected]efeb565f2013-12-12 17:16:41506 if (!datalist_only &&
[email protected]d86263dcd2014-01-09 10:35:21507 (value.length() > kMaxDataLength ||
[email protected]efeb565f2013-12-12 17:16:41508 (!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]1c69f8b72012-03-14 03:18:50512 // Any popup currently showing is obsolete.
[email protected]ddabf20d2013-07-19 21:48:20513 HideAutofillUI();
[email protected]e47aec52010-08-12 00:50:30514 return;
[email protected]1c69f8b72012-03-14 03:18:50515 }
[email protected]e47aec52010-08-12 00:50:30516
[email protected]bef7f9272012-04-17 10:47:49517 element_ = element;
[email protected]c66ba982013-04-05 09:56:17518 if (password_autofill_agent_->ShowSuggestions(element))
519 return;
[email protected]bef7f9272012-04-17 10:47:49520
[email protected]33915c12013-07-11 23:16:33521 // 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]5af43c132012-11-06 07:03:44524 // 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]d5ca8fb2013-04-11 17:54:31526 const base::string16 autocomplete_attribute =
527 element.getAttribute("autocomplete");
[email protected]33915c12013-07-11 23:16:33528 if (LowerCaseEqualsASCII(autocomplete_attribute, "off"))
529 display_warning_if_disabled = false;
[email protected]bef7f9272012-04-17 10:47:49530
[email protected]efeb565f2013-12-12 17:16:41531 QueryAutofillSuggestions(element,
532 display_warning_if_disabled,
533 datalist_only);
[email protected]77bb0da2010-11-20 01:55:30534}
535
[email protected]cd0a3b72011-08-02 06:16:19536void AutofillAgent::QueryAutofillSuggestions(const WebInputElement& element,
[email protected]efeb565f2013-12-12 17:16:41537 bool display_warning_if_disabled,
538 bool datalist_only) {
[email protected]e69b5f72013-01-24 00:59:13539 if (!element.document().frame())
540 return;
541
[email protected]77bb0da2010-11-20 01:55:30542 static int query_counter = 0;
543 autofill_query_id_ = query_counter++;
[email protected]6bcd58a2010-11-25 02:31:20544 display_warning_if_disabled_ = display_warning_if_disabled;
[email protected]77bb0da2010-11-20 01:55:30545
[email protected]5af43c132012-11-06 07:03:44546 // 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]5af43c132012-11-06 07:03:44551 const RequirementsMask requirements =
552 element.autoComplete() ? REQUIRE_AUTOCOMPLETE : REQUIRE_NONE;
553
[email protected]1ecbe862012-10-05 01:29:14554 FormData form;
555 FormFieldData field;
[email protected]5af43c132012-11-06 07:03:44556 if (!FindFormAndFieldForInputElement(element, &form, &field, requirements)) {
[email protected]7837be62011-01-18 23:45:08557 // If we didn't find the cached form, at least let autocomplete have a shot
558 // at providing suggestions.
[email protected]aaa80c92011-09-16 00:23:54559 WebFormControlElementToFormField(element, EXTRACT_VALUE, &field);
[email protected]7837be62011-01-18 23:45:08560 }
[email protected]efeb565f2013-12-12 17:16:41561 if (datalist_only)
562 field.should_autocomplete = false;
[email protected]77bb0da2010-11-20 01:55:30563
[email protected]f920d6e2013-03-12 20:20:50564 gfx::RectF bounding_box_scaled =
565 GetScaledBoundingBox(web_view_->pageScaleFactor(), &element_);
[email protected]73a087f2013-02-06 07:02:06566
[email protected]5c8de6b92012-06-08 21:24:08567 // Find the datalist values and send them to the browser process.
[email protected]d5ca8fb2013-04-11 17:54:31568 std::vector<base::string16> data_list_values;
569 std::vector<base::string16> data_list_labels;
[email protected]efeb565f2013-12-12 17:16:41570 GetDataListSuggestions(element_,
571 datalist_only,
572 &data_list_values,
573 &data_list_labels);
[email protected]ead7fb02013-07-18 18:50:12574 TrimStringVectorForIPC(&data_list_values);
575 TrimStringVectorForIPC(&data_list_labels);
[email protected]5c8de6b92012-06-08 21:24:08576
577 Send(new AutofillHostMsg_SetDataList(routing_id(),
578 data_list_values,
[email protected]ead7fb02013-07-18 18:50:12579 data_list_labels));
[email protected]5c8de6b92012-06-08 21:24:08580
[email protected]2739e0e2011-11-22 22:49:11581 Send(new AutofillHostMsg_QueryFormFieldAutofill(routing_id(),
582 autofill_query_id_,
583 form,
584 field,
[email protected]73a087f2013-02-06 07:02:06585 bounding_box_scaled,
[email protected]2739e0e2011-11-22 22:49:11586 display_warning_if_disabled));
[email protected]e47aec52010-08-12 00:50:30587}
588
[email protected]663bd9e2011-03-21 01:07:01589void AutofillAgent::FillAutofillFormData(const WebNode& node,
[email protected]78192082011-01-29 05:43:44590 int unique_id,
[email protected]663bd9e2011-03-21 01:07:01591 AutofillAction action) {
[email protected]bef7f9272012-04-17 10:47:49592 DCHECK_GT(unique_id, 0);
593
[email protected]679f128f2010-07-22 22:57:44594 static int query_counter = 0;
595 autofill_query_id_ = query_counter++;
596
[email protected]1ecbe862012-10-05 01:29:14597 FormData form;
598 FormFieldData field;
[email protected]283ba4ca2011-09-22 05:25:08599 if (!FindFormAndFieldForInputElement(node.toConst<WebInputElement>(), &form,
600 &field, REQUIRE_AUTOCOMPLETE)) {
[email protected]679f128f2010-07-22 22:57:44601 return;
[email protected]283ba4ca2011-09-22 05:25:08602 }
[email protected]679f128f2010-07-22 22:57:44603
604 autofill_action_ = action;
[email protected]663bd9e2011-03-21 01:07:01605 Send(new AutofillHostMsg_FillAutofillFormData(
[email protected]676126f72011-01-15 00:03:51606 routing_id(), autofill_query_id_, form, field, unique_id));
[email protected]679f128f2010-07-22 22:57:44607}
608
[email protected]d5ca8fb2013-04-11 17:54:31609void AutofillAgent::SetNodeText(const base::string16& value,
[email protected]a1221aea2013-11-07 01:31:30610 blink::WebInputElement* node) {
[email protected]b37043c2012-05-31 17:08:12611 did_set_node_text_ = true;
[email protected]d5ca8fb2013-04-11 17:54:31612 base::string16 substring = value;
[email protected]6f001a6cf2012-02-09 15:21:53613 substring = substring.substr(0, node->maxLength());
614
[email protected]71a90b32012-05-18 09:16:53615 node->setEditingValue(substring);
[email protected]6f001a6cf2012-02-09 15:21:53616}
617
[email protected]ddabf20d2013-07-19 21:48:20618void AutofillAgent::HideAutofillUI() {
[email protected]b5b1d69f2014-01-23 23:49:32619 if (!element_.isNull())
620 OnClearPreviewedForm();
621
[email protected]ddabf20d2013-07-19 21:48:20622 Send(new AutofillHostMsg_HideAutofillUI(routing_id()));
[email protected]81cd52332012-11-05 20:36:07623}
624
[email protected]704b6232013-09-16 21:19:12625// TODO(isherman): Decide if we want to support non-password autofill with AJAX.
[email protected]17b6be72013-04-30 21:33:08626void AutofillAgent::didAssociateFormControls(
[email protected]a1221aea2013-11-07 01:31:30627 const blink::WebVector<blink::WebNode>& nodes) {
[email protected]704b6232013-09-16 21:19:12628 for (size_t i = 0; i < nodes.size(); ++i) {
[email protected]a1221aea2013-11-07 01:31:30629 blink::WebFrame* frame = nodes[i].document().frame();
[email protected]704b6232013-09-16 21:19:12630 // 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]17b6be72013-04-30 21:33:08637}
638
[email protected]78192082011-01-29 05:43:44639} // namespace autofill