blob: cea773dfc2a01e240eede1d05495dcb3ce5678d2 [file] [log] [blame]
[email protected]66f9052c2012-01-06 00:07:361// Copyright (c) 2012 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]e4b2fa32013-03-09 22:56:565#include "components/autofill/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]8704f89b2011-04-15 00:30:059#include "base/message_loop.h"
[email protected]e4495212012-12-06 03:09:1210#include "base/string_util.h"
[email protected]1988e1c2013-02-28 20:27:4211#include "base/strings/string_split.h"
[email protected]1d14f582011-09-02 20:42:0412#include "base/time.h"
[email protected]2377cafe2010-10-27 01:19:0813#include "base/utf_string_conversions.h"
[email protected]edf48d42013-03-07 05:44:4314#include "components/autofill/common/autocheckout_status.h"
[email protected]8acc1962013-03-17 07:04:5215#include "components/autofill/common/autofill_constants.h"
[email protected]edf48d42013-03-07 05:44:4316#include "components/autofill/common/autofill_messages.h"
[email protected]f920d6e2013-03-12 20:20:5017#include "components/autofill/common/autofill_switches.h"
[email protected]edf48d42013-03-07 05:44:4318#include "components/autofill/common/form_data.h"
19#include "components/autofill/common/form_data_predictions.h"
20#include "components/autofill/common/form_field_data.h"
21#include "components/autofill/common/web_element_descriptor.h"
[email protected]e4b2fa32013-03-09 22:56:5622#include "components/autofill/renderer/form_autofill_util.h"
[email protected]ad19b302013-04-03 07:42:1923#include "components/autofill/renderer/password_autofill_agent.h"
[email protected]1ecbe862012-10-05 01:29:1424#include "content/public/common/password_form.h"
[email protected]e4495212012-12-06 03:09:1225#include "content/public/common/ssl_status.h"
[email protected]a2ef54c2011-10-10 16:20:3126#include "content/public/renderer/render_view.h"
[email protected]ff733012012-01-30 17:44:3627#include "grit/chromium_strings.h"
[email protected]679f128f2010-07-22 22:57:4428#include "grit/generated_resources.h"
[email protected]cff498e2013-01-10 01:03:0229#include "third_party/WebKit/Source/Platform/chromium/public/WebRect.h"
[email protected]d7908be2013-01-22 16:50:1830#include "third_party/WebKit/Source/Platform/chromium/public/WebURLRequest.h"
[email protected]e4495212012-12-06 03:09:1231#include "third_party/WebKit/Source/WebKit/chromium/public/WebDataSource.h"
[email protected]5219146e2013-02-28 11:42:3032#include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
[email protected]8bd0fe62011-01-17 06:44:3733#include "third_party/WebKit/Source/WebKit/chromium/public/WebFormControlElement.h"
[email protected]81cd52332012-11-05 20:36:0734#include "third_party/WebKit/Source/WebKit/chromium/public/WebFormElement.h"
[email protected]8bd0fe62011-01-17 06:44:3735#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
[email protected]a22f7e02011-02-09 07:15:3536#include "third_party/WebKit/Source/WebKit/chromium/public/WebInputEvent.h"
[email protected]aaa80c92011-09-16 00:23:5437#include "third_party/WebKit/Source/WebKit/chromium/public/WebNode.h"
[email protected]bef7f9272012-04-17 10:47:4938#include "third_party/WebKit/Source/WebKit/chromium/public/WebNodeCollection.h"
39#include "third_party/WebKit/Source/WebKit/chromium/public/WebOptionElement.h"
[email protected]8bd0fe62011-01-17 06:44:3740#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
[email protected]b6d81262011-01-13 17:36:0941#include "ui/base/keycodes/keyboard_codes.h"
[email protected]c051a1b2011-01-21 23:30:1742#include "ui/base/l10n/l10n_util.h"
[email protected]679f128f2010-07-22 22:57:4443
[email protected]8acc1962013-03-17 07:04:5244using components::autofill::kRequiredAutofillFields;
[email protected]bef7f9272012-04-17 10:47:4945using WebKit::WebAutofillClient;
[email protected]679f128f2010-07-22 22:57:4446using WebKit::WebFormControlElement;
47using WebKit::WebFormElement;
48using WebKit::WebFrame;
49using WebKit::WebInputElement;
[email protected]7aab84b2010-11-20 01:04:2650using WebKit::WebKeyboardEvent;
[email protected]679f128f2010-07-22 22:57:4451using WebKit::WebNode;
[email protected]bef7f9272012-04-17 10:47:4952using WebKit::WebNodeCollection;
53using WebKit::WebOptionElement;
[email protected]679f128f2010-07-22 22:57:4454using WebKit::WebString;
55
[email protected]e47aec52010-08-12 00:50:3056namespace {
57
58// The size above which we stop triggering autofill for an input text field
59// (so to avoid sending long strings through IPC).
[email protected]663bd9e2011-03-21 01:07:0160const size_t kMaximumTextSizeForAutofill = 1000;
[email protected]e47aec52010-08-12 00:50:3061
[email protected]5c8de6b92012-06-08 21:24:0862// The maximum number of data list elements to send to the browser process
63// via IPC (to prevent long IPC messages).
64const size_t kMaximumDataListSizeForAutofill = 30;
65
[email protected]d7908be2013-01-22 16:50:1866const int kAutocheckoutClickTimeout = 3;
67
[email protected]bef7f9272012-04-17 10:47:4968void AppendDataListSuggestions(const WebKit::WebInputElement& element,
[email protected]d5ca8fb2013-04-11 17:54:3169 std::vector<base::string16>* values,
70 std::vector<base::string16>* labels,
71 std::vector<base::string16>* icons,
[email protected]bef7f9272012-04-17 10:47:4972 std::vector<int>* item_ids) {
73 WebNodeCollection options = element.dataListOptions();
74 if (options.isNull())
75 return;
76
[email protected]d5ca8fb2013-04-11 17:54:3177 base::string16 prefix = element.editingValue();
[email protected]71a90b32012-05-18 09:16:5378 if (element.isMultiple() &&
79 element.formControlType() == WebString::fromUTF8("email")) {
[email protected]d5ca8fb2013-04-11 17:54:3180 std::vector<base::string16> parts;
[email protected]71a90b32012-05-18 09:16:5381 base::SplitStringDontTrim(prefix, ',', &parts);
82 if (parts.size() > 0)
83 TrimWhitespace(parts[parts.size() - 1], TRIM_LEADING, &prefix);
84 }
[email protected]bef7f9272012-04-17 10:47:4985 for (WebOptionElement option = options.firstItem().to<WebOptionElement>();
[email protected]71a90b32012-05-18 09:16:5386 !option.isNull(); option = options.nextItem().to<WebOptionElement>()) {
87 if (!StartsWith(option.value(), prefix, false) ||
88 option.value() == prefix ||
[email protected]3f812632012-05-16 04:13:3689 !element.isValidValue(option.value()))
[email protected]bef7f9272012-04-17 10:47:4990 continue;
91
92 values->push_back(option.value());
93 if (option.value() != option.label())
94 labels->push_back(option.label());
95 else
[email protected]d5ca8fb2013-04-11 17:54:3196 labels->push_back(base::string16());
97 icons->push_back(base::string16());
[email protected]bef7f9272012-04-17 10:47:4998 item_ids->push_back(WebAutofillClient::MenuItemIDDataListEntry);
99 }
100}
101
[email protected]5c8de6b92012-06-08 21:24:08102// Trim the vectors before sending them to the browser process to ensure we
103// don't send too much data through the IPC.
[email protected]d5ca8fb2013-04-11 17:54:31104void TrimDataListsForIPC(std::vector<base::string16>* values,
105 std::vector<base::string16>* labels,
106 std::vector<base::string16>* icons,
[email protected]5c8de6b92012-06-08 21:24:08107 std::vector<int>* unique_ids) {
108 // Limit the size of the vectors.
109 if (values->size() > kMaximumDataListSizeForAutofill) {
110 values->resize(kMaximumDataListSizeForAutofill);
111 labels->resize(kMaximumDataListSizeForAutofill);
112 icons->resize(kMaximumDataListSizeForAutofill);
113 unique_ids->resize(kMaximumDataListSizeForAutofill);
114 }
115
116 // Limit the size of the strings in the vectors
117 for (size_t i = 0; i < values->size(); ++i) {
118 if ((*values)[i].length() > kMaximumTextSizeForAutofill)
119 (*values)[i].resize(kMaximumTextSizeForAutofill);
120
121 if ((*labels)[i].length() > kMaximumTextSizeForAutofill)
122 (*labels)[i].resize(kMaximumTextSizeForAutofill);
123
124 if ((*icons)[i].length() > kMaximumTextSizeForAutofill)
125 (*icons)[i].resize(kMaximumTextSizeForAutofill);
126 }
127}
128
[email protected]f920d6e2013-03-12 20:20:50129gfx::RectF GetScaledBoundingBox(float scale, WebInputElement* element) {
130 gfx::Rect bounding_box(element->boundsInViewportSpace());
131 return gfx::RectF(bounding_box.x() * scale,
132 bounding_box.y() * scale,
133 bounding_box.width() * scale,
134 bounding_box.height() * scale);
135}
136
[email protected]e47aec52010-08-12 00:50:30137} // namespace
138
[email protected]78192082011-01-29 05:43:44139namespace autofill {
140
[email protected]ad19b302013-04-03 07:42:19141AutofillAgent::AutofillAgent(content::RenderView* render_view,
142 PasswordAutofillAgent* password_autofill_agent)
[email protected]3a034ebb2011-10-03 19:19:44143 : content::RenderViewObserver(render_view),
[email protected]ad19b302013-04-03 07:42:19144 password_autofill_agent_(password_autofill_agent),
[email protected]679f128f2010-07-22 22:57:44145 autofill_query_id_(0),
146 autofill_action_(AUTOFILL_NONE),
[email protected]d7908be2013-01-22 16:50:18147 topmost_frame_(NULL),
[email protected]73a087f2013-02-06 07:02:06148 web_view_(render_view->GetWebView()),
[email protected]6bcd58a2010-11-25 02:31:20149 display_warning_if_disabled_(false),
[email protected]d77ddc8062010-11-24 01:14:06150 was_query_node_autofilled_(false),
[email protected]e4dce0af2011-09-19 22:22:28151 has_shown_autofill_popup_for_current_edit_(false),
[email protected]b37043c2012-05-31 17:08:12152 did_set_node_text_(false),
[email protected]d7908be2013-01-22 16:50:18153 autocheckout_click_in_progress_(false),
[email protected]8a5b38692013-03-27 07:03:56154 is_whitelisted_for_autocheckout_(false),
[email protected]90f26252013-02-15 19:48:32155 ignore_text_changes_(false),
[email protected]5219146e2013-02-28 11:42:30156 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) {
[email protected]a2ef54c2011-10-10 16:20:31157 render_view->GetWebView()->setAutofillClient(this);
[email protected]679f128f2010-07-22 22:57:44158}
159
[email protected]663bd9e2011-03-21 01:07:01160AutofillAgent::~AutofillAgent() {}
[email protected]d2f05d02011-01-27 18:51:01161
[email protected]663bd9e2011-03-21 01:07:01162bool AutofillAgent::OnMessageReceived(const IPC::Message& message) {
[email protected]676126f72011-01-15 00:03:51163 bool handled = true;
[email protected]663bd9e2011-03-21 01:07:01164 IPC_BEGIN_MESSAGE_MAP(AutofillAgent, message)
[email protected]8acc1962013-03-17 07:04:52165 IPC_MESSAGE_HANDLER(AutofillMsg_GetAllForms, OnGetAllForms)
[email protected]663bd9e2011-03-21 01:07:01166 IPC_MESSAGE_HANDLER(AutofillMsg_SuggestionsReturned, OnSuggestionsReturned)
167 IPC_MESSAGE_HANDLER(AutofillMsg_FormDataFilled, OnFormDataFilled)
[email protected]3eb5728c2011-06-20 22:32:24168 IPC_MESSAGE_HANDLER(AutofillMsg_FieldTypePredictionsAvailable,
169 OnFieldTypePredictionsAvailable)
[email protected]6f001a6cf2012-02-09 15:21:53170 IPC_MESSAGE_HANDLER(AutofillMsg_SetAutofillActionFill,
171 OnSetAutofillActionFill)
172 IPC_MESSAGE_HANDLER(AutofillMsg_ClearForm,
173 OnClearForm)
174 IPC_MESSAGE_HANDLER(AutofillMsg_SetAutofillActionPreview,
175 OnSetAutofillActionPreview)
176 IPC_MESSAGE_HANDLER(AutofillMsg_ClearPreviewedForm,
177 OnClearPreviewedForm)
178 IPC_MESSAGE_HANDLER(AutofillMsg_SetNodeText,
179 OnSetNodeText)
[email protected]5c8de6b92012-06-08 21:24:08180 IPC_MESSAGE_HANDLER(AutofillMsg_AcceptDataListSuggestion,
181 OnAcceptDataListSuggestion)
[email protected]e7e83472012-04-05 02:56:26182 IPC_MESSAGE_HANDLER(AutofillMsg_AcceptPasswordAutofillSuggestion,
183 OnAcceptPasswordAutofillSuggestion)
[email protected]82fc1b12013-01-12 00:53:56184 IPC_MESSAGE_HANDLER(AutofillMsg_RequestAutocompleteResult,
185 OnRequestAutocompleteResult)
[email protected]d3ae9ed42013-01-14 23:31:01186 IPC_MESSAGE_HANDLER(AutofillMsg_FillFormsAndClick,
187 OnFillFormsAndClick)
[email protected]8a5b38692013-03-27 07:03:56188 IPC_MESSAGE_HANDLER(AutofillMsg_WhitelistedForAutocheckout,
189 OnWhitelistedForAutocheckout)
[email protected]676126f72011-01-15 00:03:51190 IPC_MESSAGE_UNHANDLED(handled = false)
191 IPC_END_MESSAGE_MAP()
192 return handled;
193}
194
[email protected]2fa18c22011-06-14 23:40:43195void AutofillAgent::DidFinishDocumentLoad(WebFrame* frame) {
[email protected]8acc1962013-03-17 07:04:52196 // Record timestamp on document load. This is used to record overhead of
197 // Autofill feature.
198 forms_seen_timestamp_ = base::TimeTicks::Now();
199
[email protected]5219146e2013-02-28 11:42:30200 // The document has now been fully loaded. Scan for forms to be sent up to
201 // the browser.
202 std::vector<FormData> forms;
[email protected]8acc1962013-03-17 07:04:52203 bool has_more_forms = false;
[email protected]5219146e2013-02-28 11:42:30204 if (!frame->parent()) {
205 topmost_frame_ = frame;
206 form_elements_.clear();
[email protected]8acc1962013-03-17 07:04:52207 has_more_forms = form_cache_.ExtractFormsAndFormElements(
208 *frame, kRequiredAutofillFields, &forms, &form_elements_);
[email protected]8a5b38692013-03-27 07:03:56209 is_whitelisted_for_autocheckout_ = false;
[email protected]5219146e2013-02-28 11:42:30210 } else {
211 form_cache_.ExtractForms(*frame, &forms);
212 }
213
[email protected]8acc1962013-03-17 07:04:52214 // Always communicate to browser process for topmost frame.
215 if (!forms.empty() || !frame->parent()) {
[email protected]5219146e2013-02-28 11:42:30216 Send(new AutofillHostMsg_FormsSeen(routing_id(), forms,
[email protected]8acc1962013-03-17 07:04:52217 forms_seen_timestamp_,
218 has_more_forms));
[email protected]5219146e2013-02-28 11:42:30219 }
[email protected]676126f72011-01-15 00:03:51220}
221
[email protected]d7908be2013-01-22 16:50:18222void AutofillAgent::DidStartProvisionalLoad(WebFrame* frame) {
223 if (!frame->parent()) {
224 topmost_frame_ = NULL;
[email protected]6f99d2212013-03-29 21:16:29225 if (click_timer_.IsRunning()) {
[email protected]d7908be2013-01-22 16:50:18226 click_timer_.Stop();
227 autocheckout_click_in_progress_ = true;
228 }
229 }
[email protected]676126f72011-01-15 00:03:51230}
231
[email protected]d7908be2013-01-22 16:50:18232void AutofillAgent::DidFailProvisionalLoad(WebFrame* frame,
233 const WebKit::WebURLError& error) {
[email protected]6f99d2212013-03-29 21:16:29234 if (!frame->parent() && autocheckout_click_in_progress_) {
[email protected]d7908be2013-01-22 16:50:18235 autocheckout_click_in_progress_ = false;
236 ClickFailed();
237 }
238}
239
240void AutofillAgent::DidCommitProvisionalLoad(WebFrame* frame,
241 bool is_new_navigation) {
[email protected]364481b2013-01-29 01:52:28242 in_flight_request_form_.reset();
[email protected]6f99d2212013-03-29 21:16:29243 if (!frame->parent())
244 autocheckout_click_in_progress_ = false;
[email protected]d7908be2013-01-22 16:50:18245}
246
247void AutofillAgent::FrameDetached(WebFrame* frame) {
[email protected]5219146e2013-02-28 11:42:30248 form_cache_.ResetFrame(*frame);
[email protected]d7908be2013-01-22 16:50:18249 if (!frame->parent()) {
250 // |frame| is about to be destroyed so we need to clear |top_most_frame_|.
251 topmost_frame_ = NULL;
252 click_timer_.Stop();
253 }
[email protected]676126f72011-01-15 00:03:51254}
255
[email protected]2a5b1732011-04-01 23:55:55256void AutofillAgent::WillSubmitForm(WebFrame* frame,
257 const WebFormElement& form) {
258 FormData form_data;
[email protected]aaa80c92011-09-16 00:23:54259 if (WebFormElementToFormData(form,
260 WebFormControlElement(),
261 REQUIRE_AUTOCOMPLETE,
262 static_cast<ExtractMask>(
263 EXTRACT_VALUE | EXTRACT_OPTION_TEXT),
264 &form_data,
265 NULL)) {
[email protected]1d14f582011-09-02 20:42:04266 Send(new AutofillHostMsg_FormSubmitted(routing_id(), form_data,
267 base::TimeTicks::Now()));
[email protected]2a5b1732011-04-01 23:55:55268 }
269}
270
[email protected]2b942c332012-04-25 16:26:26271void AutofillAgent::ZoomLevelChanged() {
272 // Any time the zoom level changes, the page's content moves, so any Autofill
273 // popups should be hidden. This is only needed for the new Autofill UI
274 // because WebKit already knows to hide the old UI when this occurs.
[email protected]136fc9182013-03-11 22:11:34275 HideHostAutofillUi();
[email protected]2b942c332012-04-25 16:26:26276}
277
[email protected]f920d6e2013-03-12 20:20:50278void AutofillAgent::FocusedNodeChanged(const WebKit::WebNode& node) {
[email protected]8a5b38692013-03-27 07:03:56279 if (!is_whitelisted_for_autocheckout_)
[email protected]f920d6e2013-03-12 20:20:50280 return;
281
282 if (node.isNull() || !node.isElementNode())
283 return;
284
285 WebKit::WebElement web_element = node.toConst<WebKit::WebElement>();
286
287 if (!web_element.document().frame())
288 return;
289
290 const WebInputElement* element = toWebInputElement(&web_element);
291
292 if (!element || !element->isEnabled() || element->isReadOnly() ||
293 !element->isTextField() || element->isPasswordField())
294 return;
295
296 FormData form;
297 FormFieldData field;
298 // This must be called to short circuit this method if it fails.
299 if (!FindFormAndFieldForInputElement(*element, &form, &field, REQUIRE_NONE))
300 return;
301
302 content::SSLStatus ssl_status = render_view()->GetSSLStatusOfFrame(
303 element->document().frame());
304
305 element_ = *element;
306
307 Send(new AutofillHostMsg_MaybeShowAutocheckoutBubble(
308 routing_id(),
309 form.origin,
310 ssl_status,
311 GetScaledBoundingBox(web_view_->pageScaleFactor(), &element_)));
312}
313
[email protected]2b942c332012-04-25 16:26:26314void AutofillAgent::DidChangeScrollOffset(WebKit::WebFrame*) {
[email protected]136fc9182013-03-11 22:11:34315 HideAutofillUi();
[email protected]2b942c332012-04-25 16:26:26316}
317
[email protected]81cd52332012-11-05 20:36:07318void AutofillAgent::didRequestAutocomplete(WebKit::WebFrame* frame,
319 const WebFormElement& form) {
320 FormData form_data;
321 if (!in_flight_request_form_.isNull() ||
322 !WebFormElementToFormData(form,
323 WebFormControlElement(),
324 REQUIRE_AUTOCOMPLETE,
[email protected]b570fd42012-12-04 22:14:13325 EXTRACT_OPTIONS,
[email protected]81cd52332012-11-05 20:36:07326 &form_data,
327 NULL)) {
328 WebFormElement(form).finishRequestAutocomplete(
[email protected]82fc1b12013-01-12 00:53:56329 WebFormElement::AutocompleteResultErrorDisabled);
[email protected]81cd52332012-11-05 20:36:07330 return;
331 }
332
333 // Cancel any pending Autofill requests and hide any currently showing popups.
334 ++autofill_query_id_;
[email protected]136fc9182013-03-11 22:11:34335 HideAutofillUi();
[email protected]81cd52332012-11-05 20:36:07336
337 in_flight_request_form_ = form;
[email protected]fa17237d2013-03-21 12:58:03338 // Add SSL Status in the formdata to let browser process alert user
339 // appropriately using browser UI.
340 form_data.ssl_status = render_view()->GetSSLStatusOfFrame(frame);
[email protected]e4495212012-12-06 03:09:12341 Send(new AutofillHostMsg_RequestAutocomplete(
342 routing_id(),
343 form_data,
[email protected]fa17237d2013-03-21 12:58:03344 frame->document().url()));
[email protected]81cd52332012-11-05 20:36:07345}
346
[email protected]90f26252013-02-15 19:48:32347void AutofillAgent::setIgnoreTextChanges(bool ignore) {
348 ignore_text_changes_ = ignore;
349}
350
[email protected]663bd9e2011-03-21 01:07:01351bool AutofillAgent::InputElementClicked(const WebInputElement& element,
[email protected]78192082011-01-29 05:43:44352 bool was_focused,
353 bool is_focused) {
[email protected]676126f72011-01-15 00:03:51354 if (was_focused)
355 ShowSuggestions(element, true, false, true);
[email protected]2fa18c22011-06-14 23:40:43356
[email protected]676126f72011-01-15 00:03:51357 return false;
358}
359
[email protected]65ec8db2011-11-04 14:50:44360bool AutofillAgent::InputElementLostFocus() {
[email protected]136fc9182013-03-11 22:11:34361 HideHostAutofillUi();
[email protected]97183052011-11-17 16:35:22362
[email protected]65ec8db2011-11-04 14:50:44363 return false;
364}
365
[email protected]3d07d6b2011-07-13 00:25:00366void AutofillAgent::didAcceptAutofillSuggestion(const WebNode& node,
[email protected]2fa18c22011-06-14 23:40:43367 const WebString& value,
368 const WebString& label,
[email protected]bef7f9272012-04-17 10:47:49369 int item_id,
[email protected]78192082011-01-29 05:43:44370 unsigned index) {
[email protected]ad19b302013-04-03 07:42:19371 if (password_autofill_agent_->DidAcceptAutofillSuggestion(node, value))
[email protected]b43c1352011-03-04 23:55:34372 return;
373
[email protected]bef7f9272012-04-17 10:47:49374 DCHECK(node == element_);
[email protected]6f001a6cf2012-02-09 15:21:53375
[email protected]bef7f9272012-04-17 10:47:49376 switch (item_id) {
377 case WebAutofillClient::MenuItemIDWarningMessage:
378 case WebAutofillClient::MenuItemIDSeparator:
379 NOTREACHED();
380 break;
381 case WebAutofillClient::MenuItemIDAutofillOptions:
382 // User selected 'Autofill Options'.
383 Send(new AutofillHostMsg_ShowAutofillDialog(routing_id()));
384 break;
385 case WebAutofillClient::MenuItemIDClearForm:
386 // User selected 'Clear form'.
387 form_cache_.ClearFormWithElement(element_);
388 break;
389 case WebAutofillClient::MenuItemIDAutocompleteEntry:
390 case WebAutofillClient::MenuItemIDPasswordEntry:
[email protected]71a90b32012-05-18 09:16:53391 // User selected an Autocomplete or password entry, so we fill directly.
[email protected]bef7f9272012-04-17 10:47:49392 SetNodeText(value, &element_);
393 break;
[email protected]71a90b32012-05-18 09:16:53394 case WebAutofillClient::MenuItemIDDataListEntry:
395 AcceptDataListSuggestion(value);
396 break;
[email protected]bef7f9272012-04-17 10:47:49397 default:
398 // A positive item_id is a unique id for an autofill (vs. autocomplete)
399 // suggestion.
400 DCHECK_GT(item_id, 0);
401 // Fill the values for the whole form.
402 FillAutofillFormData(node, item_id, AUTOFILL_FILL);
[email protected]676126f72011-01-15 00:03:51403 }
[email protected]676126f72011-01-15 00:03:51404}
405
[email protected]3d07d6b2011-07-13 00:25:00406void AutofillAgent::didSelectAutofillSuggestion(const WebNode& node,
[email protected]2fa18c22011-06-14 23:40:43407 const WebString& value,
408 const WebString& label,
[email protected]bef7f9272012-04-17 10:47:49409 int item_id) {
[email protected]ad19b302013-04-03 07:42:19410 if (password_autofill_agent_->DidSelectAutofillSuggestion(node))
[email protected]fc1964a2011-01-20 19:33:07411 return;
[email protected]676126f72011-01-15 00:03:51412
[email protected]3d07d6b2011-07-13 00:25:00413 didClearAutofillSelection(node);
[email protected]bef7f9272012-04-17 10:47:49414
415 if (item_id > 0)
416 FillAutofillFormData(node, item_id, AUTOFILL_PREVIEW);
[email protected]676126f72011-01-15 00:03:51417}
418
[email protected]3d07d6b2011-07-13 00:25:00419void AutofillAgent::didClearAutofillSelection(const WebNode& node) {
[email protected]ad19b302013-04-03 07:42:19420 if (password_autofill_agent_->DidClearAutofillSelection(node))
[email protected]6075d0502011-09-13 02:10:21421 return;
422
[email protected]bef7f9272012-04-17 10:47:49423 if (!element_.isNull() && node == element_) {
424 ClearPreviewedFormWithElement(element_, was_query_node_autofilled_);
[email protected]091b2222011-09-14 02:52:55425 } else {
426 // TODO(isherman): There seem to be rare cases where this code *is*
427 // reachable: see [ http://crbug.com/96321#c6 ]. Ideally we would
428 // understand those cases and fix the code to avoid them. However, so far I
429 // have been unable to reproduce such a case locally. If you hit this
430 // NOTREACHED(), please file a bug against me.
431 NOTREACHED();
432 }
[email protected]676126f72011-01-15 00:03:51433}
434
[email protected]2fa18c22011-06-14 23:40:43435void AutofillAgent::removeAutocompleteSuggestion(const WebString& name,
436 const WebString& value) {
[email protected]663bd9e2011-03-21 01:07:01437 Send(new AutofillHostMsg_RemoveAutocompleteEntry(routing_id(), name, value));
[email protected]679f128f2010-07-22 22:57:44438}
439
[email protected]2fa18c22011-06-14 23:40:43440void AutofillAgent::textFieldDidEndEditing(const WebInputElement& element) {
[email protected]ad19b302013-04-03 07:42:19441 password_autofill_agent_->TextFieldDidEndEditing(element);
[email protected]7d24db72011-08-26 06:02:31442 has_shown_autofill_popup_for_current_edit_ = false;
[email protected]2739e0e2011-11-22 22:49:11443 Send(new AutofillHostMsg_DidEndTextFieldEditing(routing_id()));
[email protected]676126f72011-01-15 00:03:51444}
445
[email protected]2fa18c22011-06-14 23:40:43446void AutofillAgent::textFieldDidChange(const WebInputElement& element) {
[email protected]90f26252013-02-15 19:48:32447 if (ignore_text_changes_)
448 return;
449
[email protected]b37043c2012-05-31 17:08:12450 if (did_set_node_text_) {
[email protected]b96a1d12012-11-30 22:44:32451 did_set_node_text_ = false;
452 return;
[email protected]b37043c2012-05-31 17:08:12453 }
454
[email protected]663bd9e2011-03-21 01:07:01455 // We post a task for doing the Autofill as the caret position is not set
[email protected]676126f72011-01-15 00:03:51456 // properly at this point (http://bugs.webkit.org/show_bug.cgi?id=16976) and
457 // it is needed to trigger autofill.
[email protected]5219146e2013-02-28 11:42:30458 weak_ptr_factory_.InvalidateWeakPtrs();
[email protected]676126f72011-01-15 00:03:51459 MessageLoop::current()->PostTask(
460 FROM_HERE,
[email protected]a1cb57f2011-09-28 22:41:29461 base::Bind(&AutofillAgent::TextFieldDidChangeImpl,
[email protected]5219146e2013-02-28 11:42:30462 weak_ptr_factory_.GetWeakPtr(), element));
[email protected]676126f72011-01-15 00:03:51463}
464
[email protected]2fa18c22011-06-14 23:40:43465void AutofillAgent::TextFieldDidChangeImpl(const WebInputElement& element) {
[email protected]f9af2832012-12-14 04:20:43466 // If the element isn't focused then the changes don't matter. This check is
467 // required to properly handle IME interactions.
468 if (!element.focused())
469 return;
470
[email protected]ad19b302013-04-03 07:42:19471 if (password_autofill_agent_->TextDidChangeInTextField(element)) {
[email protected]bef7f9272012-04-17 10:47:49472 element_ = element;
[email protected]676126f72011-01-15 00:03:51473 return;
[email protected]e7e83472012-04-05 02:56:26474 }
[email protected]676126f72011-01-15 00:03:51475
476 ShowSuggestions(element, false, true, false);
[email protected]7d24db72011-08-26 06:02:31477
[email protected]1ecbe862012-10-05 01:29:14478 FormData form;
479 FormFieldData field;
[email protected]283ba4ca2011-09-22 05:25:08480 if (FindFormAndFieldForInputElement(element, &form, &field, REQUIRE_NONE)) {
[email protected]1d14f582011-09-02 20:42:04481 Send(new AutofillHostMsg_TextFieldDidChange(routing_id(), form, field,
482 base::TimeTicks::Now()));
483 }
[email protected]676126f72011-01-15 00:03:51484}
485
[email protected]2fa18c22011-06-14 23:40:43486void AutofillAgent::textFieldDidReceiveKeyDown(const WebInputElement& element,
487 const WebKeyboardEvent& event) {
[email protected]ad19b302013-04-03 07:42:19488 if (password_autofill_agent_->TextFieldHandlingKeyDown(element, event)) {
[email protected]bef7f9272012-04-17 10:47:49489 element_ = element;
[email protected]fc1964a2011-01-20 19:33:07490 return;
[email protected]e7e83472012-04-05 02:56:26491 }
[email protected]676126f72011-01-15 00:03:51492
493 if (event.windowsKeyCode == ui::VKEY_DOWN ||
494 event.windowsKeyCode == ui::VKEY_UP)
495 ShowSuggestions(element, true, true, true);
496}
497
[email protected]d5ca8fb2013-04-11 17:54:31498void AutofillAgent::OnSuggestionsReturned(
499 int query_id,
500 const std::vector<base::string16>& values,
501 const std::vector<base::string16>& labels,
502 const std::vector<base::string16>& icons,
503 const std::vector<int>& unique_ids) {
[email protected]bef7f9272012-04-17 10:47:49504 if (query_id != autofill_query_id_)
[email protected]679f128f2010-07-22 22:57:44505 return;
506
[email protected]bef7f9272012-04-17 10:47:49507 if (element_.isNull() || !element_.isFocusable())
[email protected]679f128f2010-07-22 22:57:44508 return;
509
[email protected]d5ca8fb2013-04-11 17:54:31510 std::vector<base::string16> v(values);
511 std::vector<base::string16> l(labels);
512 std::vector<base::string16> i(icons);
[email protected]679f128f2010-07-22 22:57:44513 std::vector<int> ids(unique_ids);
[email protected]679f128f2010-07-22 22:57:44514
[email protected]bef7f9272012-04-17 10:47:49515 if (!element_.autoComplete() && !v.empty()) {
[email protected]cd0a3b72011-08-02 06:16:19516 // If autofill is disabled and we had suggestions, show a warning instead.
517 v.assign(1, l10n_util::GetStringUTF16(IDS_AUTOFILL_WARNING_FORM_DISABLED));
[email protected]d5ca8fb2013-04-11 17:54:31518 l.assign(1, base::string16());
519 i.assign(1, base::string16());
[email protected]bef7f9272012-04-17 10:47:49520 ids.assign(1, WebAutofillClient::MenuItemIDWarningMessage);
521 } else if (ids.size() > 1 &&
522 ids[0] == WebAutofillClient::MenuItemIDWarningMessage) {
523 // If we received an autofill warning plus some autocomplete suggestions,
524 // remove the autofill warning.
[email protected]1866d062010-11-16 06:19:56525 v.erase(v.begin());
526 l.erase(l.begin());
527 i.erase(i.begin());
528 ids.erase(ids.begin());
529 }
530
[email protected]6bcd58a2010-11-25 02:31:20531 // If we were about to show a warning and we shouldn't, don't.
[email protected]bef7f9272012-04-17 10:47:49532 if (!display_warning_if_disabled_ && !v.empty() &&
533 ids[0] == WebAutofillClient::MenuItemIDWarningMessage) {
534 v.clear();
535 l.clear();
536 i.clear();
537 ids.clear();
538 }
[email protected]6bcd58a2010-11-25 02:31:20539
[email protected]663bd9e2011-03-21 01:07:01540 // Only include "Autofill Options" special menu item if we have Autofill
[email protected]4f03a2d32010-12-01 01:49:36541 // items, identified by |unique_ids| having at least one valid value.
542 bool has_autofill_item = false;
543 for (size_t i = 0; i < ids.size(); ++i) {
544 if (ids[i] > 0) {
545 has_autofill_item = true;
546 break;
547 }
548 }
549
[email protected]bef7f9272012-04-17 10:47:49550 if (has_autofill_item) {
[email protected]d5ca8fb2013-04-11 17:54:31551 v.push_back(base::string16());
552 l.push_back(base::string16());
553 i.push_back(base::string16());
[email protected]bef7f9272012-04-17 10:47:49554 ids.push_back(WebAutofillClient::MenuItemIDSeparator);
[email protected]679f128f2010-07-22 22:57:44555
[email protected]bef7f9272012-04-17 10:47:49556 if (FormWithElementIsAutofilled(element_)) {
557 // The form has been auto-filled, so give the user the chance to clear the
558 // form. Append the 'Clear form' menu item.
559 v.push_back(l10n_util::GetStringUTF16(IDS_AUTOFILL_CLEAR_FORM_MENU_ITEM));
[email protected]d5ca8fb2013-04-11 17:54:31560 l.push_back(base::string16());
561 i.push_back(base::string16());
[email protected]bef7f9272012-04-17 10:47:49562 ids.push_back(WebAutofillClient::MenuItemIDClearForm);
563 }
564
[email protected]2377cafe2010-10-27 01:19:08565 // Append the 'Chrome Autofill options' menu item;
[email protected]ff733012012-01-30 17:44:36566 v.push_back(l10n_util::GetStringUTF16(IDS_AUTOFILL_OPTIONS_POPUP));
[email protected]d5ca8fb2013-04-11 17:54:31567 l.push_back(base::string16());
568 i.push_back(base::string16());
[email protected]bef7f9272012-04-17 10:47:49569 ids.push_back(WebAutofillClient::MenuItemIDAutofillOptions);
570 }
571
572 CombineDataListEntriesAndShow(element_, v, l, i, ids, has_autofill_item);
573}
574
575void AutofillAgent::CombineDataListEntriesAndShow(
576 const WebKit::WebInputElement& element,
[email protected]d5ca8fb2013-04-11 17:54:31577 const std::vector<base::string16>& values,
578 const std::vector<base::string16>& labels,
579 const std::vector<base::string16>& icons,
[email protected]bef7f9272012-04-17 10:47:49580 const std::vector<int>& item_ids,
581 bool has_autofill_item) {
[email protected]d5ca8fb2013-04-11 17:54:31582 std::vector<base::string16> v;
583 std::vector<base::string16> l;
584 std::vector<base::string16> i;
[email protected]bef7f9272012-04-17 10:47:49585 std::vector<int> ids;
586
587 AppendDataListSuggestions(element, &v, &l, &i, &ids);
588
589 // If there are both <datalist> items and Autofill suggestions, add a
590 // separator between them.
591 if (!v.empty() && !values.empty()) {
[email protected]d5ca8fb2013-04-11 17:54:31592 v.push_back(base::string16());
593 l.push_back(base::string16());
594 i.push_back(base::string16());
[email protected]bef7f9272012-04-17 10:47:49595 ids.push_back(WebAutofillClient::MenuItemIDSeparator);
596 }
597
598 // Append the Autofill suggestions.
599 v.insert(v.end(), values.begin(), values.end());
600 l.insert(l.end(), labels.begin(), labels.end());
601 i.insert(i.end(), icons.begin(), icons.end());
602 ids.insert(ids.end(), item_ids.begin(), item_ids.end());
603
[email protected]ef47f9a2012-11-06 04:40:09604 if (v.empty()) {
605 // No suggestions, any popup currently showing is obsolete.
[email protected]136fc9182013-03-11 22:11:34606 HideAutofillUi();
[email protected]ef47f9a2012-11-06 04:40:09607 return;
608 }
609
[email protected]bef7f9272012-04-17 10:47:49610 WebKit::WebView* web_view = render_view()->GetWebView();
611 if (!web_view)
612 return;
613
[email protected]679f128f2010-07-22 22:57:44614 // Send to WebKit for display.
[email protected]bef7f9272012-04-17 10:47:49615 web_view->applyAutofillSuggestions(element, v, l, i, ids);
[email protected]468327f2010-10-04 20:02:29616
[email protected]7d24db72011-08-26 06:02:31617 Send(new AutofillHostMsg_DidShowAutofillSuggestions(
618 routing_id(),
619 has_autofill_item && !has_shown_autofill_popup_for_current_edit_));
620 has_shown_autofill_popup_for_current_edit_ |= has_autofill_item;
[email protected]679f128f2010-07-22 22:57:44621}
622
[email protected]d5ca8fb2013-04-11 17:54:31623void AutofillAgent::AcceptDataListSuggestion(
624 const base::string16& suggested_value) {
625 base::string16 new_value = suggested_value;
[email protected]71a90b32012-05-18 09:16:53626 // If this element takes multiple values then replace the last part with
627 // the suggestion.
628 if (element_.isMultiple() &&
629 element_.formControlType() == WebString::fromUTF8("email")) {
[email protected]d5ca8fb2013-04-11 17:54:31630 std::vector<base::string16> parts;
[email protected]71a90b32012-05-18 09:16:53631
632 base::SplitStringDontTrim(element_.editingValue(), ',', &parts);
633 if (parts.size() == 0)
[email protected]d5ca8fb2013-04-11 17:54:31634 parts.push_back(base::string16());
[email protected]71a90b32012-05-18 09:16:53635
[email protected]d5ca8fb2013-04-11 17:54:31636 base::string16 last_part = parts.back();
[email protected]71a90b32012-05-18 09:16:53637 // We want to keep just the leading whitespace.
638 for (size_t i = 0; i < last_part.size(); ++i) {
639 if (!IsWhitespace(last_part[i])) {
640 last_part = last_part.substr(0, i);
641 break;
642 }
643 }
644 last_part.append(suggested_value);
645 parts[parts.size() - 1] = last_part;
646
647 new_value = JoinString(parts, ',');
648 }
649 SetNodeText(new_value, &element_);
650}
651
[email protected]663bd9e2011-03-21 01:07:01652void AutofillAgent::OnFormDataFilled(int query_id,
[email protected]1ecbe862012-10-05 01:29:14653 const FormData& form) {
[email protected]a2ef54c2011-10-10 16:20:31654 if (!render_view()->GetWebView() || query_id != autofill_query_id_)
[email protected]679f128f2010-07-22 22:57:44655 return;
656
[email protected]bef7f9272012-04-17 10:47:49657 was_query_node_autofilled_ = element_.isAutofilled();
[email protected]6f001a6cf2012-02-09 15:21:53658
[email protected]679f128f2010-07-22 22:57:44659 switch (autofill_action_) {
660 case AUTOFILL_FILL:
[email protected]bef7f9272012-04-17 10:47:49661 FillForm(form, element_);
[email protected]1d14f582011-09-02 20:42:04662 Send(new AutofillHostMsg_DidFillAutofillFormData(routing_id(),
663 base::TimeTicks::Now()));
[email protected]679f128f2010-07-22 22:57:44664 break;
665 case AUTOFILL_PREVIEW:
[email protected]bef7f9272012-04-17 10:47:49666 PreviewForm(form, element_);
[email protected]7d24db72011-08-26 06:02:31667 Send(new AutofillHostMsg_DidPreviewAutofillFormData(routing_id()));
[email protected]679f128f2010-07-22 22:57:44668 break;
669 default:
670 NOTREACHED();
671 }
672 autofill_action_ = AUTOFILL_NONE;
[email protected]e47aec52010-08-12 00:50:30673}
674
[email protected]3eb5728c2011-06-20 22:32:24675void AutofillAgent::OnFieldTypePredictionsAvailable(
676 const std::vector<FormDataPredictions>& forms) {
677 for (size_t i = 0; i < forms.size(); ++i) {
[email protected]aaa80c92011-09-16 00:23:54678 form_cache_.ShowPredictions(forms[i]);
[email protected]3eb5728c2011-06-20 22:32:24679 }
680}
681
[email protected]6f001a6cf2012-02-09 15:21:53682void AutofillAgent::OnSetAutofillActionFill() {
683 autofill_action_ = AUTOFILL_FILL;
684}
685
686void AutofillAgent::OnClearForm() {
[email protected]bef7f9272012-04-17 10:47:49687 form_cache_.ClearFormWithElement(element_);
[email protected]6f001a6cf2012-02-09 15:21:53688}
689
690void AutofillAgent::OnSetAutofillActionPreview() {
691 autofill_action_ = AUTOFILL_PREVIEW;
692}
693
694void AutofillAgent::OnClearPreviewedForm() {
[email protected]bef7f9272012-04-17 10:47:49695 didClearAutofillSelection(element_);
[email protected]6f001a6cf2012-02-09 15:21:53696}
697
[email protected]d5ca8fb2013-04-11 17:54:31698void AutofillAgent::OnSetNodeText(const base::string16& value) {
[email protected]bef7f9272012-04-17 10:47:49699 SetNodeText(value, &element_);
[email protected]6f001a6cf2012-02-09 15:21:53700}
701
[email protected]d5ca8fb2013-04-11 17:54:31702void AutofillAgent::OnAcceptDataListSuggestion(const base::string16& value) {
[email protected]5c8de6b92012-06-08 21:24:08703 AcceptDataListSuggestion(value);
704}
705
[email protected]d5ca8fb2013-04-11 17:54:31706void AutofillAgent::OnAcceptPasswordAutofillSuggestion(
707 const base::string16& value) {
[email protected]e7e83472012-04-05 02:56:26708 // We need to make sure this is handled here because the browser process
709 // skipped it handling because it believed it would be handled here. If it
710 // isn't handled here then the browser logic needs to be updated.
[email protected]ad19b302013-04-03 07:42:19711 bool handled = password_autofill_agent_->DidAcceptAutofillSuggestion(
[email protected]bef7f9272012-04-17 10:47:49712 element_,
[email protected]e7e83472012-04-05 02:56:26713 value);
714 DCHECK(handled);
715}
716
[email protected]8acc1962013-03-17 07:04:52717void AutofillAgent::OnGetAllForms() {
718 form_elements_.clear();
719
720 // Force fetch all non empty forms.
721 std::vector<FormData> forms;
722 form_cache_.ExtractFormsAndFormElements(
723 *topmost_frame_, 0, &forms, &form_elements_);
724
725 // OnGetAllForms should only be called if AutofillAgent reported to
726 // AutofillManager that there are more forms
727 DCHECK(!forms.empty());
728
729 // Report to AutofillManager that all forms are being sent.
730 Send(new AutofillHostMsg_FormsSeen(routing_id(), forms,
731 forms_seen_timestamp_, false));
732}
733
[email protected]82fc1b12013-01-12 00:53:56734void AutofillAgent::OnRequestAutocompleteResult(
735 WebFormElement::AutocompleteResult result, const FormData& form_data) {
[email protected]364481b2013-01-29 01:52:28736 if (in_flight_request_form_.isNull())
737 return;
738
[email protected]82fc1b12013-01-12 00:53:56739 if (result == WebFormElement::AutocompleteResultSuccess)
740 FillFormIncludingNonFocusableElements(form_data, in_flight_request_form_);
[email protected]364481b2013-01-29 01:52:28741
[email protected]81cd52332012-11-05 20:36:07742 in_flight_request_form_.finishRequestAutocomplete(result);
743 in_flight_request_form_.reset();
744}
745
[email protected]d3ae9ed42013-01-14 23:31:01746void AutofillAgent::OnFillFormsAndClick(
[email protected]14696912013-02-04 05:37:02747 const std::vector<FormData>& forms,
[email protected]d3ae9ed42013-01-14 23:31:01748 const WebElementDescriptor& click_element_descriptor) {
[email protected]14696912013-02-04 05:37:02749 DCHECK_EQ(forms.size(), form_elements_.size());
750
751 // Fill the form.
752 for (size_t i = 0; i < forms.size(); ++i)
753 FillFormIncludingNonFocusableElements(forms[i], form_elements_[i]);
[email protected]d3ae9ed42013-01-14 23:31:01754
[email protected]6f99d2212013-03-29 21:16:29755 // Exit early if there is nothing to click.
756 if (click_element_descriptor.retrieval_method == WebElementDescriptor::NONE)
757 return;
758
[email protected]d7908be2013-01-22 16:50:18759 // It's possible that clicking the element to proceed in an Autocheckout
760 // flow will not actually proceed to the next step in the flow, e.g. there
761 // is a new required field that Autocheckout does not know how to fill. In
762 // order to capture this case and present the user with an error a timer is
763 // set that informs the browser of the error. |click_timer_| has to be started
764 // before clicking so it can start before DidStartProvisionalLoad started.
765 click_timer_.Start(FROM_HERE,
766 base::TimeDelta::FromSeconds(kAutocheckoutClickTimeout),
767 this,
768 &AutofillAgent::ClickFailed);
769 if (!ClickElement(topmost_frame_->document(),
770 click_element_descriptor)) {
771 click_timer_.Stop();
772 Send(new AutofillHostMsg_ClickFailed(routing_id(),
773 MISSING_ADVANCE));
774 }
775}
776
[email protected]8a5b38692013-03-27 07:03:56777void AutofillAgent::OnWhitelistedForAutocheckout() {
778 is_whitelisted_for_autocheckout_ = true;
779}
780
[email protected]d7908be2013-01-22 16:50:18781void AutofillAgent::ClickFailed() {
782 Send(new AutofillHostMsg_ClickFailed(routing_id(),
783 CANNOT_PROCEED));
[email protected]d3ae9ed42013-01-14 23:31:01784}
785
[email protected]663bd9e2011-03-21 01:07:01786void AutofillAgent::ShowSuggestions(const WebInputElement& element,
[email protected]78192082011-01-29 05:43:44787 bool autofill_on_empty_values,
788 bool requires_caret_at_end,
789 bool display_warning_if_disabled) {
[email protected]bef7f9272012-04-17 10:47:49790 if (!element.isEnabled() || element.isReadOnly() || !element.isTextField() ||
791 element.isPasswordField() || !element.suggestedValue().isEmpty())
[email protected]e47aec52010-08-12 00:50:30792 return;
793
[email protected]1c69f8b72012-03-14 03:18:50794 // Don't attempt to autofill with values that are too large or if filling
795 // criteria are not met.
[email protected]71a90b32012-05-18 09:16:53796 WebString value = element.editingValue();
[email protected]1c69f8b72012-03-14 03:18:50797 if (value.length() > kMaximumTextSizeForAutofill ||
798 (!autofill_on_empty_values && value.isEmpty()) ||
799 (requires_caret_at_end &&
800 (element.selectionStart() != element.selectionEnd() ||
801 element.selectionEnd() != static_cast<int>(value.length())))) {
802 // Any popup currently showing is obsolete.
[email protected]136fc9182013-03-11 22:11:34803 HideAutofillUi();
[email protected]e47aec52010-08-12 00:50:30804 return;
[email protected]1c69f8b72012-03-14 03:18:50805 }
[email protected]e47aec52010-08-12 00:50:30806
[email protected]bef7f9272012-04-17 10:47:49807 element_ = element;
[email protected]c66ba982013-04-05 09:56:17808 if (password_autofill_agent_->ShowSuggestions(element))
809 return;
[email protected]bef7f9272012-04-17 10:47:49810
[email protected]5af43c132012-11-06 07:03:44811 // If autocomplete is disabled at the form level, then we might want to show a
812 // warning in place of suggestions. However, if autocomplete is disabled
813 // specifically for this field, we never want to show a warning. Otherwise,
814 // we might interfere with custom popups (e.g. search suggestions) used by the
815 // website. Note that we cannot use the WebKit method element.autoComplete()
816 // as it does not allow us to distinguish the case where autocomplete is
817 // disabled for *both* the element and for the form.
818 // Also, if the field has no name, then we won't have values.
[email protected]d5ca8fb2013-04-11 17:54:31819 const base::string16 autocomplete_attribute =
820 element.getAttribute("autocomplete");
[email protected]5af43c132012-11-06 07:03:44821 if (LowerCaseEqualsASCII(autocomplete_attribute, "off") ||
[email protected]bef7f9272012-04-17 10:47:49822 element.nameForAutofill().isEmpty()) {
[email protected]d5ca8fb2013-04-11 17:54:31823 CombineDataListEntriesAndShow(element, std::vector<base::string16>(),
824 std::vector<base::string16>(),
825 std::vector<base::string16>(),
[email protected]bef7f9272012-04-17 10:47:49826 std::vector<int>(), false);
827 return;
828 }
829
[email protected]663bd9e2011-03-21 01:07:01830 QueryAutofillSuggestions(element, display_warning_if_disabled);
[email protected]77bb0da2010-11-20 01:55:30831}
832
[email protected]cd0a3b72011-08-02 06:16:19833void AutofillAgent::QueryAutofillSuggestions(const WebInputElement& element,
[email protected]78192082011-01-29 05:43:44834 bool display_warning_if_disabled) {
[email protected]e69b5f72013-01-24 00:59:13835 if (!element.document().frame())
836 return;
837
[email protected]77bb0da2010-11-20 01:55:30838 static int query_counter = 0;
839 autofill_query_id_ = query_counter++;
[email protected]6bcd58a2010-11-25 02:31:20840 display_warning_if_disabled_ = display_warning_if_disabled;
[email protected]77bb0da2010-11-20 01:55:30841
[email protected]5af43c132012-11-06 07:03:44842 // If autocomplete is disabled at the form level, we want to see if there
843 // would have been any suggestions were it enabled, so that we can show a
844 // warning. Otherwise, we want to ignore fields that disable autocomplete, so
845 // that the suggestions list does not include suggestions for these form
846 // fields -- see comment 1 on http://crbug.com/69914
847 // Rather than testing the form's autocomplete enabled state, we test the
848 // element's state. The DCHECK below ensures that this is equivalent.
849 DCHECK(element.autoComplete() || !element.form().autoComplete());
850 const RequirementsMask requirements =
851 element.autoComplete() ? REQUIRE_AUTOCOMPLETE : REQUIRE_NONE;
852
[email protected]1ecbe862012-10-05 01:29:14853 FormData form;
854 FormFieldData field;
[email protected]5af43c132012-11-06 07:03:44855 if (!FindFormAndFieldForInputElement(element, &form, &field, requirements)) {
[email protected]7837be62011-01-18 23:45:08856 // If we didn't find the cached form, at least let autocomplete have a shot
857 // at providing suggestions.
[email protected]aaa80c92011-09-16 00:23:54858 WebFormControlElementToFormField(element, EXTRACT_VALUE, &field);
[email protected]7837be62011-01-18 23:45:08859 }
[email protected]77bb0da2010-11-20 01:55:30860
[email protected]f920d6e2013-03-12 20:20:50861 gfx::RectF bounding_box_scaled =
862 GetScaledBoundingBox(web_view_->pageScaleFactor(), &element_);
[email protected]73a087f2013-02-06 07:02:06863
[email protected]5c8de6b92012-06-08 21:24:08864 // Find the datalist values and send them to the browser process.
[email protected]d5ca8fb2013-04-11 17:54:31865 std::vector<base::string16> data_list_values;
866 std::vector<base::string16> data_list_labels;
867 std::vector<base::string16> data_list_icons;
[email protected]5c8de6b92012-06-08 21:24:08868 std::vector<int> data_list_unique_ids;
869 AppendDataListSuggestions(element_,
870 &data_list_values,
871 &data_list_labels,
872 &data_list_icons,
873 &data_list_unique_ids);
874
875 TrimDataListsForIPC(&data_list_values,
876 &data_list_labels,
877 &data_list_icons,
878 &data_list_unique_ids);
879
880 Send(new AutofillHostMsg_SetDataList(routing_id(),
881 data_list_values,
882 data_list_labels,
883 data_list_icons,
884 data_list_unique_ids));
885
[email protected]e899b962013-01-18 20:52:08886 // Add SSL Status in the formdata to let browser process alert user
887 // appropriately using browser UI.
888 form.ssl_status = render_view()->GetSSLStatusOfFrame(
889 element.document().frame());
[email protected]2739e0e2011-11-22 22:49:11890 Send(new AutofillHostMsg_QueryFormFieldAutofill(routing_id(),
891 autofill_query_id_,
892 form,
893 field,
[email protected]73a087f2013-02-06 07:02:06894 bounding_box_scaled,
[email protected]2739e0e2011-11-22 22:49:11895 display_warning_if_disabled));
[email protected]e47aec52010-08-12 00:50:30896}
897
[email protected]663bd9e2011-03-21 01:07:01898void AutofillAgent::FillAutofillFormData(const WebNode& node,
[email protected]78192082011-01-29 05:43:44899 int unique_id,
[email protected]663bd9e2011-03-21 01:07:01900 AutofillAction action) {
[email protected]bef7f9272012-04-17 10:47:49901 DCHECK_GT(unique_id, 0);
902
[email protected]679f128f2010-07-22 22:57:44903 static int query_counter = 0;
904 autofill_query_id_ = query_counter++;
905
[email protected]1ecbe862012-10-05 01:29:14906 FormData form;
907 FormFieldData field;
[email protected]283ba4ca2011-09-22 05:25:08908 if (!FindFormAndFieldForInputElement(node.toConst<WebInputElement>(), &form,
909 &field, REQUIRE_AUTOCOMPLETE)) {
[email protected]679f128f2010-07-22 22:57:44910 return;
[email protected]283ba4ca2011-09-22 05:25:08911 }
[email protected]679f128f2010-07-22 22:57:44912
913 autofill_action_ = action;
[email protected]663bd9e2011-03-21 01:07:01914 Send(new AutofillHostMsg_FillAutofillFormData(
[email protected]676126f72011-01-15 00:03:51915 routing_id(), autofill_query_id_, form, field, unique_id));
[email protected]679f128f2010-07-22 22:57:44916}
917
[email protected]d5ca8fb2013-04-11 17:54:31918void AutofillAgent::SetNodeText(const base::string16& value,
[email protected]6f001a6cf2012-02-09 15:21:53919 WebKit::WebInputElement* node) {
[email protected]b37043c2012-05-31 17:08:12920 did_set_node_text_ = true;
[email protected]d5ca8fb2013-04-11 17:54:31921 base::string16 substring = value;
[email protected]6f001a6cf2012-02-09 15:21:53922 substring = substring.substr(0, node->maxLength());
923
[email protected]71a90b32012-05-18 09:16:53924 node->setEditingValue(substring);
[email protected]6f001a6cf2012-02-09 15:21:53925}
926
[email protected]136fc9182013-03-11 22:11:34927void AutofillAgent::HideAutofillUi() {
[email protected]81cd52332012-11-05 20:36:07928 WebKit::WebView* web_view = render_view()->GetWebView();
929 if (web_view)
930 web_view->hidePopups();
[email protected]ef47f9a2012-11-06 04:40:09931
[email protected]136fc9182013-03-11 22:11:34932 HideHostAutofillUi();
[email protected]ef47f9a2012-11-06 04:40:09933}
934
[email protected]136fc9182013-03-11 22:11:34935void AutofillAgent::HideHostAutofillUi() {
936 Send(new AutofillHostMsg_HideAutofillUi(routing_id()));
[email protected]81cd52332012-11-05 20:36:07937}
938
[email protected]78192082011-01-29 05:43:44939} // namespace autofill