blob: d3ea95e79a19d3cdf22af76e46ef10b0ef4d8ea5 [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]426d676a2014-05-28 14:41:0331#include "grit/components_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();
[email protected]7bc44532014-07-10 16:02:1583 if (element.isMultiple() && element.isEmailField()) {
[email protected]efeb565f2013-12-12 17:16:4184 std::vector<base::string16> parts;
85 base::SplitStringDontTrim(prefix, ',', &parts);
[email protected]8af69c6c2014-03-03 19:05:3186 if (parts.size() > 0) {
87 base::TrimWhitespace(parts[parts.size() - 1], base::TRIM_LEADING,
88 &prefix);
89 }
[email protected]efeb565f2013-12-12 17:16:4190 }
[email protected]71a90b32012-05-18 09:16:5391 }
[email protected]bef7f9272012-04-17 10:47:4992 for (WebOptionElement option = options.firstItem().to<WebOptionElement>();
[email protected]71a90b32012-05-18 09:16:5393 !option.isNull(); option = options.nextItem().to<WebOptionElement>()) {
94 if (!StartsWith(option.value(), prefix, false) ||
95 option.value() == prefix ||
[email protected]3f812632012-05-16 04:13:3696 !element.isValidValue(option.value()))
[email protected]bef7f9272012-04-17 10:47:4997 continue;
98
99 values->push_back(option.value());
100 if (option.value() != option.label())
101 labels->push_back(option.label());
102 else
[email protected]d5ca8fb2013-04-11 17:54:31103 labels->push_back(base::string16());
[email protected]bef7f9272012-04-17 10:47:49104 }
105}
106
[email protected]ead7fb02013-07-18 18:50:12107// Trim the vector before sending it to the browser process to ensure we
[email protected]5c8de6b92012-06-08 21:24:08108// don't send too much data through the IPC.
[email protected]ead7fb02013-07-18 18:50:12109void TrimStringVectorForIPC(std::vector<base::string16>* strings) {
110 // Limit the size of the vector.
[email protected]d86263dcd2014-01-09 10:35:21111 if (strings->size() > kMaxListSize)
112 strings->resize(kMaxListSize);
[email protected]5c8de6b92012-06-08 21:24:08113
[email protected]ead7fb02013-07-18 18:50:12114 // Limit the size of the strings in the vector.
115 for (size_t i = 0; i < strings->size(); ++i) {
[email protected]d86263dcd2014-01-09 10:35:21116 if ((*strings)[i].length() > kMaxDataLength)
117 (*strings)[i].resize(kMaxDataLength);
[email protected]5c8de6b92012-06-08 21:24:08118 }
119}
120
[email protected]e47aec52010-08-12 00:50:30121} // namespace
122
[email protected]ad19b302013-04-03 07:42:19123AutofillAgent::AutofillAgent(content::RenderView* render_view,
[email protected]3cbdf9362014-01-31 23:12:23124 PasswordAutofillAgent* password_autofill_agent,
125 PasswordGenerationAgent* password_generation_agent)
[email protected]3a034ebb2011-10-03 19:19:44126 : content::RenderViewObserver(render_view),
[email protected]ad19b302013-04-03 07:42:19127 password_autofill_agent_(password_autofill_agent),
[email protected]3cbdf9362014-01-31 23:12:23128 password_generation_agent_(password_generation_agent),
[email protected]679f128f2010-07-22 22:57:44129 autofill_query_id_(0),
[email protected]73a087f2013-02-06 07:02:06130 web_view_(render_view->GetWebView()),
[email protected]6bcd58a2010-11-25 02:31:20131 display_warning_if_disabled_(false),
[email protected]d77ddc8062010-11-24 01:14:06132 was_query_node_autofilled_(false),
[email protected]e4dce0af2011-09-19 22:22:28133 has_shown_autofill_popup_for_current_edit_(false),
[email protected]b37043c2012-05-31 17:08:12134 did_set_node_text_(false),
[email protected]90f26252013-02-15 19:48:32135 ignore_text_changes_(false),
[email protected]b648f242014-02-25 13:49:06136 is_popup_possibly_visible_(false),
[email protected]5193ea52014-05-14 01:10:02137 main_frame_processed_(false),
[email protected]2c305252013-04-26 19:57:05138 weak_ptr_factory_(this) {
[email protected]a2ef54c2011-10-10 16:20:31139 render_view->GetWebView()->setAutofillClient(this);
[email protected]03f10dad2013-04-13 08:23:05140
141 // The PageClickTracker is a RenderViewObserver, and hence will be freed when
142 // the RenderView is destroyed.
143 new PageClickTracker(render_view, this);
[email protected]679f128f2010-07-22 22:57:44144}
145
[email protected]a64fdc32013-07-31 09:21:13146AutofillAgent::~AutofillAgent() {}
[email protected]d2f05d02011-01-27 18:51:01147
[email protected]663bd9e2011-03-21 01:07:01148bool AutofillAgent::OnMessageReceived(const IPC::Message& message) {
[email protected]676126f72011-01-15 00:03:51149 bool handled = true;
[email protected]663bd9e2011-03-21 01:07:01150 IPC_BEGIN_MESSAGE_MAP(AutofillAgent, message)
[email protected]8527a78f2014-06-12 12:49:15151 IPC_MESSAGE_HANDLER(AutofillMsg_Ping, OnPing)
[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]126b1ad2014-05-21 22:37:33163 IPC_MESSAGE_HANDLER(AutofillMsg_FillPasswordSuggestion,
164 OnFillPasswordSuggestion)
165 IPC_MESSAGE_HANDLER(AutofillMsg_PreviewPasswordSuggestion,
166 OnPreviewPasswordSuggestion)
[email protected]82fc1b12013-01-12 00:53:56167 IPC_MESSAGE_HANDLER(AutofillMsg_RequestAutocompleteResult,
168 OnRequestAutocompleteResult)
[email protected]676126f72011-01-15 00:03:51169 IPC_MESSAGE_UNHANDLED(handled = false)
170 IPC_END_MESSAGE_MAP()
171 return handled;
172}
173
[email protected]c5041c322014-04-08 05:06:47174void AutofillAgent::DidFinishDocumentLoad(WebLocalFrame* frame) {
[email protected]5193ea52014-05-14 01:10:02175 // If the main frame just finished loading, we should process it.
176 if (!frame->parent())
177 main_frame_processed_ = false;
[email protected]8acc1962013-03-17 07:04:52178
[email protected]5193ea52014-05-14 01:10:02179 ProcessForms(*frame);
[email protected]676126f72011-01-15 00:03:51180}
181
[email protected]3609c962014-07-11 03:18:25182void AutofillAgent::DidCommitProvisionalLoad(WebLocalFrame* frame,
183 bool is_new_navigation) {
184 form_cache_.ResetFrame(*frame);
185}
186
[email protected]d7908be2013-01-22 16:50:18187void AutofillAgent::FrameDetached(WebFrame* frame) {
[email protected]5219146e2013-02-28 11:42:30188 form_cache_.ResetFrame(*frame);
[email protected]676126f72011-01-15 00:03:51189}
190
[email protected]5e7e8612014-03-20 14:43:19191void AutofillAgent::FrameWillClose(WebFrame* frame) {
192 if (in_flight_request_form_.isNull())
193 return;
194
195 for (WebFrame* temp = in_flight_request_form_.document().frame();
196 temp; temp = temp->parent()) {
197 if (temp == frame) {
198 Send(new AutofillHostMsg_CancelRequestAutocomplete(routing_id()));
199 break;
200 }
201 }
202}
203
[email protected]c5041c322014-04-08 05:06:47204void AutofillAgent::WillSubmitForm(WebLocalFrame* frame,
[email protected]2a5b1732011-04-01 23:55:55205 const WebFormElement& form) {
206 FormData form_data;
[email protected]aaa80c92011-09-16 00:23:54207 if (WebFormElementToFormData(form,
208 WebFormControlElement(),
[email protected]c6292f82014-07-24 10:49:05209 REQUIRE_NONE,
[email protected]aaa80c92011-09-16 00:23:54210 static_cast<ExtractMask>(
211 EXTRACT_VALUE | EXTRACT_OPTION_TEXT),
212 &form_data,
213 NULL)) {
[email protected]1d14f582011-09-02 20:42:04214 Send(new AutofillHostMsg_FormSubmitted(routing_id(), form_data,
215 base::TimeTicks::Now()));
[email protected]2a5b1732011-04-01 23:55:55216 }
217}
218
[email protected]5e7e8612014-03-20 14:43:19219void AutofillAgent::FocusedNodeChanged(const WebNode& node) {
[email protected]91dcc6d32014-07-30 00:01:33220 HidePopup();
221
[email protected]7e048272014-04-17 03:38:52222 if (password_generation_agent_ &&
223 password_generation_agent_->FocusedNodeHasChanged(node)) {
224 is_popup_possibly_visible_ = true;
225 return;
226 }
227
[email protected]f920d6e2013-03-12 20:20:50228 if (node.isNull() || !node.isElementNode())
229 return;
230
[email protected]5e7e8612014-03-20 14:43:19231 WebElement web_element = node.toConst<WebElement>();
[email protected]f920d6e2013-03-12 20:20:50232
233 if (!web_element.document().frame())
234 return;
235
236 const WebInputElement* element = toWebInputElement(&web_element);
237
238 if (!element || !element->isEnabled() || element->isReadOnly() ||
239 !element->isTextField() || element->isPasswordField())
240 return;
241
[email protected]e5057a22013-04-22 12:41:39242 element_ = *element;
[email protected]e5057a22013-04-22 12:41:39243}
244
[email protected]fcdc5642014-05-09 14:32:24245void AutofillAgent::OrientationChangeEvent() {
[email protected]b648f242014-02-25 13:49:06246 HidePopup();
[email protected]652ad832013-08-16 11:47:04247}
248
estade48c85882014-08-25 18:26:50249void AutofillAgent::Resized() {
250 HidePopup();
251}
252
[email protected]c5041c322014-04-08 05:06:47253void AutofillAgent::DidChangeScrollOffset(WebLocalFrame*) {
[email protected]b648f242014-02-25 13:49:06254 HidePopup();
[email protected]2b942c332012-04-25 16:26:26255}
256
[email protected]1c6269a62014-04-12 03:17:14257void AutofillAgent::didRequestAutocomplete(
[email protected]304130f2014-05-23 22:58:55258 const WebFormElement& form) {
[email protected]fed9d4f2013-12-17 21:32:47259 // Disallow the dialog over non-https or broken https, except when the
260 // ignore SSL flag is passed. See http://crbug.com/272512.
261 // TODO(palmer): this should be moved to the browser process after frames
262 // get their own processes.
[email protected]1c6269a62014-04-12 03:17:14263 GURL url(form.document().url());
264 content::SSLStatus ssl_status =
265 render_view()->GetSSLStatusOfFrame(form.document().frame());
[email protected]e8ca69c2014-05-07 15:31:19266 bool is_safe = url.SchemeIs(url::kHttpsScheme) &&
[email protected]fed9d4f2013-12-17 21:32:47267 !net::IsCertStatusError(ssl_status.cert_status);
268 bool allow_unsafe = CommandLine::ForCurrentProcess()->HasSwitch(
269 ::switches::kReduceSecurityForTesting);
270
[email protected]81cd52332012-11-05 20:36:07271 FormData form_data;
[email protected]a3d81cf2014-04-15 23:26:16272 std::string error_message;
273 if (!in_flight_request_form_.isNull()) {
274 error_message = "already active.";
275 } else if (!is_safe && !allow_unsafe) {
276 error_message =
277 "must use a secure connection or --reduce-security-for-testing.";
278 } else if (!WebFormElementToFormData(form,
279 WebFormControlElement(),
280 REQUIRE_AUTOCOMPLETE,
[email protected]191279cb2014-06-26 08:02:11281 static_cast<ExtractMask>(
282 EXTRACT_VALUE |
283 EXTRACT_OPTION_TEXT |
284 EXTRACT_OPTIONS),
[email protected]a3d81cf2014-04-15 23:26:16285 &form_data,
286 NULL)) {
287 error_message = "failed to parse form.";
288 }
289
290 if (!error_message.empty()) {
291 WebConsoleMessage console_message = WebConsoleMessage(
292 WebConsoleMessage::LevelLog,
293 WebString(base::ASCIIToUTF16("requestAutocomplete: ") +
294 base::ASCIIToUTF16(error_message)));
[email protected]b221bd62014-06-06 10:16:37295 form.document().frame()->addMessageToConsole(console_message);
[email protected]81cd52332012-11-05 20:36:07296 WebFormElement(form).finishRequestAutocomplete(
[email protected]82fc1b12013-01-12 00:53:56297 WebFormElement::AutocompleteResultErrorDisabled);
[email protected]81cd52332012-11-05 20:36:07298 return;
299 }
300
301 // Cancel any pending Autofill requests and hide any currently showing popups.
302 ++autofill_query_id_;
[email protected]b648f242014-02-25 13:49:06303 HidePopup();
[email protected]81cd52332012-11-05 20:36:07304
305 in_flight_request_form_ = form;
[email protected]ca5190f2013-07-08 11:10:31306 Send(new AutofillHostMsg_RequestAutocomplete(routing_id(), form_data, url));
[email protected]81cd52332012-11-05 20:36:07307}
308
[email protected]90f26252013-02-15 19:48:32309void AutofillAgent::setIgnoreTextChanges(bool ignore) {
310 ignore_text_changes_ = ignore;
311}
312
[email protected]e9d29d392014-03-25 01:15:15313void AutofillAgent::FormControlElementClicked(
314 const WebFormControlElement& element,
315 bool was_focused) {
316 const WebInputElement* input_element = toWebInputElement(&element);
317 if (!input_element && !IsTextAreaElement(element))
318 return;
319
[email protected]580c38052014-07-16 07:28:09320 bool show_full_suggestion_list = element.isAutofilled() || was_focused;
321 bool show_password_suggestions_only = !was_focused;
322 ShowSuggestions(element,
323 true,
324 false,
325 true,
326 false,
327 show_full_suggestion_list,
328 show_password_suggestions_only);
[email protected]676126f72011-01-15 00:03:51329}
330
[email protected]2fa18c22011-06-14 23:40:43331void AutofillAgent::textFieldDidEndEditing(const WebInputElement& element) {
[email protected]ad19b302013-04-03 07:42:19332 password_autofill_agent_->TextFieldDidEndEditing(element);
[email protected]7d24db72011-08-26 06:02:31333 has_shown_autofill_popup_for_current_edit_ = false;
[email protected]2739e0e2011-11-22 22:49:11334 Send(new AutofillHostMsg_DidEndTextFieldEditing(routing_id()));
[email protected]676126f72011-01-15 00:03:51335}
336
[email protected]e9d29d392014-03-25 01:15:15337void AutofillAgent::textFieldDidChange(const WebFormControlElement& element) {
[email protected]90f26252013-02-15 19:48:32338 if (ignore_text_changes_)
339 return;
340
[email protected]e9d29d392014-03-25 01:15:15341 DCHECK(toWebInputElement(&element) || IsTextAreaElement(element));
342
[email protected]b37043c2012-05-31 17:08:12343 if (did_set_node_text_) {
[email protected]b96a1d12012-11-30 22:44:32344 did_set_node_text_ = false;
345 return;
[email protected]b37043c2012-05-31 17:08:12346 }
347
[email protected]663bd9e2011-03-21 01:07:01348 // We post a task for doing the Autofill as the caret position is not set
[email protected]676126f72011-01-15 00:03:51349 // properly at this point (http://bugs.webkit.org/show_bug.cgi?id=16976) and
350 // it is needed to trigger autofill.
[email protected]5219146e2013-02-28 11:42:30351 weak_ptr_factory_.InvalidateWeakPtrs();
[email protected]d14c5b32013-05-06 05:25:11352 base::MessageLoop::current()->PostTask(
353 FROM_HERE,
354 base::Bind(&AutofillAgent::TextFieldDidChangeImpl,
355 weak_ptr_factory_.GetWeakPtr(),
356 element));
[email protected]676126f72011-01-15 00:03:51357}
358
[email protected]e9d29d392014-03-25 01:15:15359void AutofillAgent::TextFieldDidChangeImpl(
360 const WebFormControlElement& element) {
[email protected]f9af2832012-12-14 04:20:43361 // If the element isn't focused then the changes don't matter. This check is
362 // required to properly handle IME interactions.
363 if (!element.focused())
364 return;
365
[email protected]e9d29d392014-03-25 01:15:15366 const WebInputElement* input_element = toWebInputElement(&element);
367 if (input_element) {
368 if (password_generation_agent_ &&
369 password_generation_agent_->TextDidChangeInTextField(*input_element)) {
[email protected]7e048272014-04-17 03:38:52370 is_popup_possibly_visible_ = true;
[email protected]e9d29d392014-03-25 01:15:15371 return;
372 }
[email protected]3cbdf9362014-01-31 23:12:23373
[email protected]e9d29d392014-03-25 01:15:15374 if (password_autofill_agent_->TextDidChangeInTextField(*input_element)) {
375 element_ = element;
376 return;
377 }
[email protected]e7e83472012-04-05 02:56:26378 }
[email protected]676126f72011-01-15 00:03:51379
[email protected]580c38052014-07-16 07:28:09380 ShowSuggestions(element, false, true, false, false, false, false);
[email protected]7d24db72011-08-26 06:02:31381
[email protected]1ecbe862012-10-05 01:29:14382 FormData form;
383 FormFieldData field;
[email protected]e9d29d392014-03-25 01:15:15384 if (FindFormAndFieldForFormControlElement(element,
385 &form,
386 &field,
387 REQUIRE_NONE)) {
[email protected]1d14f582011-09-02 20:42:04388 Send(new AutofillHostMsg_TextFieldDidChange(routing_id(), form, field,
389 base::TimeTicks::Now()));
390 }
[email protected]676126f72011-01-15 00:03:51391}
392
[email protected]2fa18c22011-06-14 23:40:43393void AutofillAgent::textFieldDidReceiveKeyDown(const WebInputElement& element,
394 const WebKeyboardEvent& event) {
[email protected]ad19b302013-04-03 07:42:19395 if (password_autofill_agent_->TextFieldHandlingKeyDown(element, event)) {
[email protected]bef7f9272012-04-17 10:47:49396 element_ = element;
[email protected]fc1964a2011-01-20 19:33:07397 return;
[email protected]e7e83472012-04-05 02:56:26398 }
[email protected]676126f72011-01-15 00:03:51399
400 if (event.windowsKeyCode == ui::VKEY_DOWN ||
401 event.windowsKeyCode == ui::VKEY_UP)
[email protected]580c38052014-07-16 07:28:09402 ShowSuggestions(element, true, true, true, false, false, false);
[email protected]efeb565f2013-12-12 17:16:41403}
404
405void AutofillAgent::openTextDataListChooser(const WebInputElement& element) {
[email protected]580c38052014-07-16 07:28:09406 ShowSuggestions(element, true, false, false, true, false, false);
[email protected]676126f72011-01-15 00:03:51407}
408
[email protected]fc22ae52014-04-23 13:48:04409void AutofillAgent::firstUserGestureObserved() {
410 password_autofill_agent_->FirstUserGestureObserved();
411}
412
[email protected]d5ca8fb2013-04-11 17:54:31413void AutofillAgent::AcceptDataListSuggestion(
414 const base::string16& suggested_value) {
[email protected]e9d29d392014-03-25 01:15:15415 WebInputElement* input_element = toWebInputElement(&element_);
416 DCHECK(input_element);
[email protected]d5ca8fb2013-04-11 17:54:31417 base::string16 new_value = suggested_value;
[email protected]71a90b32012-05-18 09:16:53418 // If this element takes multiple values then replace the last part with
419 // the suggestion.
[email protected]7bc44532014-07-10 16:02:15420 if (input_element->isMultiple() && input_element->isEmailField()) {
[email protected]d5ca8fb2013-04-11 17:54:31421 std::vector<base::string16> parts;
[email protected]71a90b32012-05-18 09:16:53422
[email protected]e9d29d392014-03-25 01:15:15423 base::SplitStringDontTrim(input_element->editingValue(), ',', &parts);
[email protected]71a90b32012-05-18 09:16:53424 if (parts.size() == 0)
[email protected]d5ca8fb2013-04-11 17:54:31425 parts.push_back(base::string16());
[email protected]71a90b32012-05-18 09:16:53426
[email protected]d5ca8fb2013-04-11 17:54:31427 base::string16 last_part = parts.back();
[email protected]71a90b32012-05-18 09:16:53428 // We want to keep just the leading whitespace.
429 for (size_t i = 0; i < last_part.size(); ++i) {
430 if (!IsWhitespace(last_part[i])) {
431 last_part = last_part.substr(0, i);
432 break;
433 }
434 }
435 last_part.append(suggested_value);
436 parts[parts.size() - 1] = last_part;
437
438 new_value = JoinString(parts, ',');
439 }
[email protected]e9d29d392014-03-25 01:15:15440 FillFieldWithValue(new_value, input_element);
[email protected]71a90b32012-05-18 09:16:53441}
442
[email protected]3eb5728c2011-06-20 22:32:24443void AutofillAgent::OnFieldTypePredictionsAvailable(
444 const std::vector<FormDataPredictions>& forms) {
445 for (size_t i = 0; i < forms.size(); ++i) {
[email protected]aaa80c92011-09-16 00:23:54446 form_cache_.ShowPredictions(forms[i]);
[email protected]3eb5728c2011-06-20 22:32:24447 }
448}
449
[email protected]63560b32014-03-04 07:06:26450void AutofillAgent::OnFillForm(int query_id, const FormData& form) {
451 if (!render_view()->GetWebView() || query_id != autofill_query_id_)
452 return;
453
454 was_query_node_autofilled_ = element_.isAutofilled();
455 FillForm(form, element_);
456 Send(new AutofillHostMsg_DidFillAutofillFormData(routing_id(),
457 base::TimeTicks::Now()));
458}
459
[email protected]8527a78f2014-06-12 12:49:15460void AutofillAgent::OnPing() {
461 Send(new AutofillHostMsg_PingAck(routing_id()));
462}
463
[email protected]63560b32014-03-04 07:06:26464void AutofillAgent::OnPreviewForm(int query_id, const FormData& form) {
465 if (!render_view()->GetWebView() || query_id != autofill_query_id_)
466 return;
467
468 was_query_node_autofilled_ = element_.isAutofilled();
469 PreviewForm(form, element_);
470 Send(new AutofillHostMsg_DidPreviewAutofillFormData(routing_id()));
[email protected]6f001a6cf2012-02-09 15:21:53471}
472
473void AutofillAgent::OnClearForm() {
[email protected]bef7f9272012-04-17 10:47:49474 form_cache_.ClearFormWithElement(element_);
[email protected]6f001a6cf2012-02-09 15:21:53475}
476
[email protected]6f001a6cf2012-02-09 15:21:53477void AutofillAgent::OnClearPreviewedForm() {
[email protected]93f2e502013-12-20 09:47:25478 if (!element_.isNull()) {
[email protected]b5b1d69f2014-01-23 23:49:32479 if (password_autofill_agent_->DidClearAutofillSelection(element_))
480 return;
481
[email protected]93f2e502013-12-20 09:47:25482 ClearPreviewedFormWithElement(element_, was_query_node_autofilled_);
483 } else {
484 // TODO(isherman): There seem to be rare cases where this code *is*
485 // reachable: see [ http://crbug.com/96321#c6 ]. Ideally we would
486 // understand those cases and fix the code to avoid them. However, so far I
487 // have been unable to reproduce such a case locally. If you hit this
488 // NOTREACHED(), please file a bug against me.
489 NOTREACHED();
490 }
[email protected]6f001a6cf2012-02-09 15:21:53491}
492
[email protected]ac9b92f2014-03-15 00:48:32493void AutofillAgent::OnFillFieldWithValue(const base::string16& value) {
[email protected]e9d29d392014-03-25 01:15:15494 WebInputElement* input_element = toWebInputElement(&element_);
495 if (input_element)
496 FillFieldWithValue(value, input_element);
[email protected]ac9b92f2014-03-15 00:48:32497}
498
499void AutofillAgent::OnPreviewFieldWithValue(const base::string16& value) {
[email protected]e9d29d392014-03-25 01:15:15500 WebInputElement* input_element = toWebInputElement(&element_);
501 if (input_element)
502 PreviewFieldWithValue(value, input_element);
[email protected]6f001a6cf2012-02-09 15:21:53503}
504
[email protected]d5ca8fb2013-04-11 17:54:31505void AutofillAgent::OnAcceptDataListSuggestion(const base::string16& value) {
[email protected]5c8de6b92012-06-08 21:24:08506 AcceptDataListSuggestion(value);
507}
508
[email protected]126b1ad2014-05-21 22:37:33509void AutofillAgent::OnFillPasswordSuggestion(const base::string16& username,
510 const base::string16& password) {
511 bool handled = password_autofill_agent_->FillSuggestion(
512 element_,
513 username,
514 password);
515 DCHECK(handled);
516}
517
518void AutofillAgent::OnPreviewPasswordSuggestion(
[email protected]c281d502014-04-24 21:07:36519 const base::string16& username,
520 const base::string16& password) {
[email protected]126b1ad2014-05-21 22:37:33521 bool handled = password_autofill_agent_->PreviewSuggestion(
[email protected]bef7f9272012-04-17 10:47:49522 element_,
[email protected]c281d502014-04-24 21:07:36523 username,
524 password);
[email protected]e7e83472012-04-05 02:56:26525 DCHECK(handled);
526}
527
[email protected]82fc1b12013-01-12 00:53:56528void AutofillAgent::OnRequestAutocompleteResult(
[email protected]5e7e8612014-03-20 14:43:19529 WebFormElement::AutocompleteResult result,
[email protected]dbdd60272014-04-14 22:48:40530 const base::string16& message,
[email protected]5e7e8612014-03-20 14:43:19531 const FormData& form_data) {
[email protected]364481b2013-01-29 01:52:28532 if (in_flight_request_form_.isNull())
533 return;
534
[email protected]9793fb232013-06-21 10:25:35535 if (result == WebFormElement::AutocompleteResultSuccess) {
[email protected]82fc1b12013-01-12 00:53:56536 FillFormIncludingNonFocusableElements(form_data, in_flight_request_form_);
[email protected]3bf260a2014-04-16 19:37:12537 if (!in_flight_request_form_.checkValidity())
[email protected]9793fb232013-06-21 10:25:35538 result = WebFormElement::AutocompleteResultErrorInvalid;
539 }
[email protected]364481b2013-01-29 01:52:28540
[email protected]81cd52332012-11-05 20:36:07541 in_flight_request_form_.finishRequestAutocomplete(result);
[email protected]dbdd60272014-04-14 22:48:40542
543 if (!message.empty()) {
544 const base::string16 prefix(base::ASCIIToUTF16("requestAutocomplete: "));
545 WebConsoleMessage console_message = WebConsoleMessage(
546 WebConsoleMessage::LevelLog, WebString(prefix + message));
547 in_flight_request_form_.document().frame()->addMessageToConsole(
548 console_message);
549 }
550
[email protected]81cd52332012-11-05 20:36:07551 in_flight_request_form_.reset();
552}
553
[email protected]e9d29d392014-03-25 01:15:15554void AutofillAgent::ShowSuggestions(const WebFormControlElement& element,
[email protected]78192082011-01-29 05:43:44555 bool autofill_on_empty_values,
556 bool requires_caret_at_end,
[email protected]efeb565f2013-12-12 17:16:41557 bool display_warning_if_disabled,
[email protected]580c38052014-07-16 07:28:09558 bool datalist_only,
559 bool show_full_suggestion_list,
560 bool show_password_suggestions_only) {
[email protected]e9d29d392014-03-25 01:15:15561 if (!element.isEnabled() || element.isReadOnly())
[email protected]efeb565f2013-12-12 17:16:41562 return;
[email protected]580c38052014-07-16 07:28:09563 if (!datalist_only && !element.suggestedValue().isEmpty())
564 return;
[email protected]e9d29d392014-03-25 01:15:15565
566 const WebInputElement* input_element = toWebInputElement(&element);
567 if (input_element) {
568 if (!input_element->isTextField() || input_element->isPasswordField())
569 return;
570 if (!datalist_only && !input_element->suggestedValue().isEmpty())
571 return;
572 } else {
573 DCHECK(IsTextAreaElement(element));
574 if (!element.toConst<WebTextAreaElement>().suggestedValue().isEmpty())
575 return;
576 }
[email protected]e47aec52010-08-12 00:50:30577
[email protected]1c69f8b72012-03-14 03:18:50578 // Don't attempt to autofill with values that are too large or if filling
579 // criteria are not met.
[email protected]71a90b32012-05-18 09:16:53580 WebString value = element.editingValue();
[email protected]efeb565f2013-12-12 17:16:41581 if (!datalist_only &&
[email protected]d86263dcd2014-01-09 10:35:21582 (value.length() > kMaxDataLength ||
[email protected]efeb565f2013-12-12 17:16:41583 (!autofill_on_empty_values && value.isEmpty()) ||
584 (requires_caret_at_end &&
585 (element.selectionStart() != element.selectionEnd() ||
586 element.selectionEnd() != static_cast<int>(value.length()))))) {
[email protected]1c69f8b72012-03-14 03:18:50587 // Any popup currently showing is obsolete.
[email protected]b648f242014-02-25 13:49:06588 HidePopup();
[email protected]e47aec52010-08-12 00:50:30589 return;
[email protected]1c69f8b72012-03-14 03:18:50590 }
[email protected]e47aec52010-08-12 00:50:30591
[email protected]bef7f9272012-04-17 10:47:49592 element_ = element;
[email protected]580c38052014-07-16 07:28:09593 if (IsAutofillableInputElement(input_element) &&
594 (password_autofill_agent_->ShowSuggestions(*input_element,
595 show_full_suggestion_list) ||
596 show_password_suggestions_only)) {
[email protected]b648f242014-02-25 13:49:06597 is_popup_possibly_visible_ = true;
[email protected]c66ba982013-04-05 09:56:17598 return;
[email protected]b648f242014-02-25 13:49:06599 }
[email protected]bef7f9272012-04-17 10:47:49600
[email protected]33915c12013-07-11 23:16:33601 // If autocomplete is disabled at the field level, ensure that the native
602 // UI won't try to show a warning, since that may conflict with a custom
603 // popup. Note that we cannot use the WebKit method element.autoComplete()
[email protected]5af43c132012-11-06 07:03:44604 // as it does not allow us to distinguish the case where autocomplete is
605 // disabled for *both* the element and for the form.
[email protected]d5ca8fb2013-04-11 17:54:31606 const base::string16 autocomplete_attribute =
607 element.getAttribute("autocomplete");
[email protected]df807042014-08-13 16:48:41608 if (LowerCaseEqualsASCII(autocomplete_attribute, "off"))
[email protected]33915c12013-07-11 23:16:33609 display_warning_if_disabled = false;
[email protected]bef7f9272012-04-17 10:47:49610
[email protected]efeb565f2013-12-12 17:16:41611 QueryAutofillSuggestions(element,
612 display_warning_if_disabled,
613 datalist_only);
[email protected]77bb0da2010-11-20 01:55:30614}
615
[email protected]e9d29d392014-03-25 01:15:15616void AutofillAgent::QueryAutofillSuggestions(
617 const WebFormControlElement& element,
618 bool display_warning_if_disabled,
619 bool datalist_only) {
[email protected]e69b5f72013-01-24 00:59:13620 if (!element.document().frame())
621 return;
622
[email protected]e9d29d392014-03-25 01:15:15623 DCHECK(toWebInputElement(&element) || IsTextAreaElement(element));
624
[email protected]77bb0da2010-11-20 01:55:30625 static int query_counter = 0;
626 autofill_query_id_ = query_counter++;
[email protected]6bcd58a2010-11-25 02:31:20627 display_warning_if_disabled_ = display_warning_if_disabled;
[email protected]77bb0da2010-11-20 01:55:30628
[email protected]5af43c132012-11-06 07:03:44629 // If autocomplete is disabled at the form level, we want to see if there
630 // would have been any suggestions were it enabled, so that we can show a
631 // warning. Otherwise, we want to ignore fields that disable autocomplete, so
632 // that the suggestions list does not include suggestions for these form
633 // fields -- see comment 1 on http://crbug.com/69914
[email protected]5af43c132012-11-06 07:03:44634 const RequirementsMask requirements =
635 element.autoComplete() ? REQUIRE_AUTOCOMPLETE : REQUIRE_NONE;
636
[email protected]1ecbe862012-10-05 01:29:14637 FormData form;
638 FormFieldData field;
[email protected]e9d29d392014-03-25 01:15:15639 if (!FindFormAndFieldForFormControlElement(element, &form, &field,
640 requirements)) {
[email protected]7837be62011-01-18 23:45:08641 // If we didn't find the cached form, at least let autocomplete have a shot
642 // at providing suggestions.
[email protected]aaa80c92011-09-16 00:23:54643 WebFormControlElementToFormField(element, EXTRACT_VALUE, &field);
[email protected]7837be62011-01-18 23:45:08644 }
[email protected]efeb565f2013-12-12 17:16:41645 if (datalist_only)
646 field.should_autocomplete = false;
[email protected]77bb0da2010-11-20 01:55:30647
[email protected]f920d6e2013-03-12 20:20:50648 gfx::RectF bounding_box_scaled =
649 GetScaledBoundingBox(web_view_->pageScaleFactor(), &element_);
[email protected]73a087f2013-02-06 07:02:06650
[email protected]7d0ea5362014-05-01 23:39:43651 std::vector<base::string16> data_list_values;
652 std::vector<base::string16> data_list_labels;
[email protected]e9d29d392014-03-25 01:15:15653 const WebInputElement* input_element = toWebInputElement(&element);
654 if (input_element) {
655 // Find the datalist values and send them to the browser process.
[email protected]e9d29d392014-03-25 01:15:15656 GetDataListSuggestions(*input_element,
657 datalist_only,
658 &data_list_values,
659 &data_list_labels);
660 TrimStringVectorForIPC(&data_list_values);
661 TrimStringVectorForIPC(&data_list_labels);
[email protected]e9d29d392014-03-25 01:15:15662 }
[email protected]5c8de6b92012-06-08 21:24:08663
[email protected]b648f242014-02-25 13:49:06664 is_popup_possibly_visible_ = true;
[email protected]7d0ea5362014-05-01 23:39:43665 Send(new AutofillHostMsg_SetDataList(routing_id(),
666 data_list_values,
667 data_list_labels));
668
[email protected]2739e0e2011-11-22 22:49:11669 Send(new AutofillHostMsg_QueryFormFieldAutofill(routing_id(),
670 autofill_query_id_,
671 form,
672 field,
[email protected]73a087f2013-02-06 07:02:06673 bounding_box_scaled,
[email protected]2739e0e2011-11-22 22:49:11674 display_warning_if_disabled));
[email protected]e47aec52010-08-12 00:50:30675}
676
[email protected]ac9b92f2014-03-15 00:48:32677void AutofillAgent::FillFieldWithValue(const base::string16& value,
[email protected]5e7e8612014-03-20 14:43:19678 WebInputElement* node) {
[email protected]b37043c2012-05-31 17:08:12679 did_set_node_text_ = true;
[email protected]2ee19ec2014-02-25 12:44:31680 node->setEditingValue(value.substr(0, node->maxLength()));
[email protected]ac9b92f2014-03-15 00:48:32681 node->setAutofilled(true);
682}
683
684void AutofillAgent::PreviewFieldWithValue(const base::string16& value,
[email protected]5e7e8612014-03-20 14:43:19685 WebInputElement* node) {
[email protected]ac9b92f2014-03-15 00:48:32686 was_query_node_autofilled_ = element_.isAutofilled();
687 node->setSuggestedValue(value.substr(0, node->maxLength()));
688 node->setAutofilled(true);
[email protected]f47d3022014-03-24 21:24:18689 node->setSelectionRange(node->value().length(),
690 node->suggestedValue().length());
[email protected]6f001a6cf2012-02-09 15:21:53691}
692
[email protected]5193ea52014-05-14 01:10:02693void AutofillAgent::ProcessForms(const WebLocalFrame& frame) {
694 // Record timestamp of when the forms are first seen. This is used to
695 // measure the overhead of the Autofill feature.
696 base::TimeTicks forms_seen_timestamp = base::TimeTicks::Now();
697
698 std::vector<FormData> forms;
699 form_cache_.ExtractNewForms(frame, &forms);
700
701 // Always communicate to browser process for topmost frame.
702 if (!forms.empty() ||
703 (!frame.parent() && !main_frame_processed_)) {
704 Send(new AutofillHostMsg_FormsSeen(routing_id(), forms,
705 forms_seen_timestamp));
706 }
707
708 if (!frame.parent())
709 main_frame_processed_ = true;
710}
711
[email protected]b648f242014-02-25 13:49:06712void AutofillAgent::HidePopup() {
713 if (!is_popup_possibly_visible_)
714 return;
715
[email protected]b5b1d69f2014-01-23 23:49:32716 if (!element_.isNull())
717 OnClearPreviewedForm();
718
[email protected]b648f242014-02-25 13:49:06719 is_popup_possibly_visible_ = false;
720 Send(new AutofillHostMsg_HidePopup(routing_id()));
[email protected]81cd52332012-11-05 20:36:07721}
722
[email protected]5e7e8612014-03-20 14:43:19723void AutofillAgent::didAssociateFormControls(const WebVector<WebNode>& nodes) {
[email protected]704b6232013-09-16 21:19:12724 for (size_t i = 0; i < nodes.size(); ++i) {
[email protected]5193ea52014-05-14 01:10:02725 WebLocalFrame* frame = nodes[i].document().frame();
[email protected]704b6232013-09-16 21:19:12726 // Only monitors dynamic forms created in the top frame. Dynamic forms
[email protected]5193ea52014-05-14 01:10:02727 // inserted in iframes are not captured yet. Frame is only processed
728 // if it has finished loading, otherwise you can end up with a partially
729 // parsed form.
730 if (frame && !frame->parent() && !frame->isLoading()) {
731 ProcessForms(*frame);
[email protected]704b6232013-09-16 21:19:12732 password_autofill_agent_->OnDynamicFormsSeen(frame);
733 return;
734 }
735 }
[email protected]17b6be72013-04-30 21:33:08736}
737
[email protected]78192082011-01-29 05:43:44738} // namespace autofill