OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/renderer/autofill/autofill_agent.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/message_loop.h" | |
9 #include "base/string_util.h" | |
10 #include "base/strings/string_split.h" | |
11 #include "base/time.h" | |
12 #include "base/utf_string_conversions.h" | |
13 #include "chrome/renderer/autofill/form_autofill_util.h" | |
14 #include "chrome/renderer/autofill/password_autofill_manager.h" | |
15 #include "components/autofill/common/autocheckout_status.h" | |
16 #include "components/autofill/common/autofill_messages.h" | |
17 #include "components/autofill/common/form_data.h" | |
18 #include "components/autofill/common/form_data_predictions.h" | |
19 #include "components/autofill/common/form_field_data.h" | |
20 #include "components/autofill/common/web_element_descriptor.h" | |
21 #include "content/public/common/password_form.h" | |
22 #include "content/public/common/ssl_status.h" | |
23 #include "content/public/renderer/render_view.h" | |
24 #include "grit/chromium_strings.h" | |
25 #include "grit/generated_resources.h" | |
26 #include "third_party/WebKit/Source/Platform/chromium/public/WebRect.h" | |
27 #include "third_party/WebKit/Source/Platform/chromium/public/WebURLRequest.h" | |
28 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDataSource.h" | |
29 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | |
30 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFormControlElement
.h" | |
31 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFormElement.h" | |
32 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | |
33 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputEvent.h" | |
34 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNode.h" | |
35 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNodeCollection.h" | |
36 #include "third_party/WebKit/Source/WebKit/chromium/public/WebOptionElement.h" | |
37 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | |
38 #include "ui/base/keycodes/keyboard_codes.h" | |
39 #include "ui/base/l10n/l10n_util.h" | |
40 | |
41 using WebKit::WebAutofillClient; | |
42 using WebKit::WebFormControlElement; | |
43 using WebKit::WebFormElement; | |
44 using WebKit::WebFrame; | |
45 using WebKit::WebInputElement; | |
46 using WebKit::WebKeyboardEvent; | |
47 using WebKit::WebNode; | |
48 using WebKit::WebNodeCollection; | |
49 using WebKit::WebOptionElement; | |
50 using WebKit::WebString; | |
51 | |
52 namespace { | |
53 | |
54 // The size above which we stop triggering autofill for an input text field | |
55 // (so to avoid sending long strings through IPC). | |
56 const size_t kMaximumTextSizeForAutofill = 1000; | |
57 | |
58 // The maximum number of data list elements to send to the browser process | |
59 // via IPC (to prevent long IPC messages). | |
60 const size_t kMaximumDataListSizeForAutofill = 30; | |
61 | |
62 const int kAutocheckoutClickTimeout = 3; | |
63 | |
64 void AppendDataListSuggestions(const WebKit::WebInputElement& element, | |
65 std::vector<string16>* values, | |
66 std::vector<string16>* labels, | |
67 std::vector<string16>* icons, | |
68 std::vector<int>* item_ids) { | |
69 WebNodeCollection options = element.dataListOptions(); | |
70 if (options.isNull()) | |
71 return; | |
72 | |
73 string16 prefix = element.editingValue(); | |
74 if (element.isMultiple() && | |
75 element.formControlType() == WebString::fromUTF8("email")) { | |
76 std::vector<string16> parts; | |
77 base::SplitStringDontTrim(prefix, ',', &parts); | |
78 if (parts.size() > 0) | |
79 TrimWhitespace(parts[parts.size() - 1], TRIM_LEADING, &prefix); | |
80 } | |
81 for (WebOptionElement option = options.firstItem().to<WebOptionElement>(); | |
82 !option.isNull(); option = options.nextItem().to<WebOptionElement>()) { | |
83 if (!StartsWith(option.value(), prefix, false) || | |
84 option.value() == prefix || | |
85 !element.isValidValue(option.value())) | |
86 continue; | |
87 | |
88 values->push_back(option.value()); | |
89 if (option.value() != option.label()) | |
90 labels->push_back(option.label()); | |
91 else | |
92 labels->push_back(string16()); | |
93 icons->push_back(string16()); | |
94 item_ids->push_back(WebAutofillClient::MenuItemIDDataListEntry); | |
95 } | |
96 } | |
97 | |
98 // Trim the vectors before sending them to the browser process to ensure we | |
99 // don't send too much data through the IPC. | |
100 void TrimDataListsForIPC(std::vector<string16>* values, | |
101 std::vector<string16>* labels, | |
102 std::vector<string16>* icons, | |
103 std::vector<int>* unique_ids) { | |
104 // Limit the size of the vectors. | |
105 if (values->size() > kMaximumDataListSizeForAutofill) { | |
106 values->resize(kMaximumDataListSizeForAutofill); | |
107 labels->resize(kMaximumDataListSizeForAutofill); | |
108 icons->resize(kMaximumDataListSizeForAutofill); | |
109 unique_ids->resize(kMaximumDataListSizeForAutofill); | |
110 } | |
111 | |
112 // Limit the size of the strings in the vectors | |
113 for (size_t i = 0; i < values->size(); ++i) { | |
114 if ((*values)[i].length() > kMaximumTextSizeForAutofill) | |
115 (*values)[i].resize(kMaximumTextSizeForAutofill); | |
116 | |
117 if ((*labels)[i].length() > kMaximumTextSizeForAutofill) | |
118 (*labels)[i].resize(kMaximumTextSizeForAutofill); | |
119 | |
120 if ((*icons)[i].length() > kMaximumTextSizeForAutofill) | |
121 (*icons)[i].resize(kMaximumTextSizeForAutofill); | |
122 } | |
123 } | |
124 | |
125 } // namespace | |
126 | |
127 namespace autofill { | |
128 | |
129 AutofillAgent::AutofillAgent( | |
130 content::RenderView* render_view, | |
131 PasswordAutofillManager* password_autofill_manager) | |
132 : content::RenderViewObserver(render_view), | |
133 password_autofill_manager_(password_autofill_manager), | |
134 autofill_query_id_(0), | |
135 autofill_action_(AUTOFILL_NONE), | |
136 topmost_frame_(NULL), | |
137 web_view_(render_view->GetWebView()), | |
138 display_warning_if_disabled_(false), | |
139 was_query_node_autofilled_(false), | |
140 has_shown_autofill_popup_for_current_edit_(false), | |
141 did_set_node_text_(false), | |
142 autocheckout_click_in_progress_(false), | |
143 ignore_text_changes_(false), | |
144 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { | |
145 render_view->GetWebView()->setAutofillClient(this); | |
146 } | |
147 | |
148 AutofillAgent::~AutofillAgent() {} | |
149 | |
150 bool AutofillAgent::OnMessageReceived(const IPC::Message& message) { | |
151 bool handled = true; | |
152 IPC_BEGIN_MESSAGE_MAP(AutofillAgent, message) | |
153 IPC_MESSAGE_HANDLER(AutofillMsg_SuggestionsReturned, OnSuggestionsReturned) | |
154 IPC_MESSAGE_HANDLER(AutofillMsg_FormDataFilled, OnFormDataFilled) | |
155 IPC_MESSAGE_HANDLER(AutofillMsg_FieldTypePredictionsAvailable, | |
156 OnFieldTypePredictionsAvailable) | |
157 IPC_MESSAGE_HANDLER(AutofillMsg_SetAutofillActionFill, | |
158 OnSetAutofillActionFill) | |
159 IPC_MESSAGE_HANDLER(AutofillMsg_ClearForm, | |
160 OnClearForm) | |
161 IPC_MESSAGE_HANDLER(AutofillMsg_SetAutofillActionPreview, | |
162 OnSetAutofillActionPreview) | |
163 IPC_MESSAGE_HANDLER(AutofillMsg_ClearPreviewedForm, | |
164 OnClearPreviewedForm) | |
165 IPC_MESSAGE_HANDLER(AutofillMsg_SetNodeText, | |
166 OnSetNodeText) | |
167 IPC_MESSAGE_HANDLER(AutofillMsg_AcceptDataListSuggestion, | |
168 OnAcceptDataListSuggestion) | |
169 IPC_MESSAGE_HANDLER(AutofillMsg_AcceptPasswordAutofillSuggestion, | |
170 OnAcceptPasswordAutofillSuggestion) | |
171 IPC_MESSAGE_HANDLER(AutofillMsg_RequestAutocompleteResult, | |
172 OnRequestAutocompleteResult) | |
173 IPC_MESSAGE_HANDLER(AutofillMsg_FillFormsAndClick, | |
174 OnFillFormsAndClick) | |
175 IPC_MESSAGE_UNHANDLED(handled = false) | |
176 IPC_END_MESSAGE_MAP() | |
177 return handled; | |
178 } | |
179 | |
180 void AutofillAgent::DidFinishDocumentLoad(WebFrame* frame) { | |
181 // The document has now been fully loaded. Scan for forms to be sent up to | |
182 // the browser. | |
183 std::vector<FormData> forms; | |
184 | |
185 if (!frame->parent()) { | |
186 topmost_frame_ = frame; | |
187 form_elements_.clear(); | |
188 form_cache_.ExtractFormsAndFormElements(*frame, &forms, &form_elements_); | |
189 } else { | |
190 form_cache_.ExtractForms(*frame, &forms); | |
191 } | |
192 | |
193 if (!forms.empty()) { | |
194 Send(new AutofillHostMsg_FormsSeen(routing_id(), forms, | |
195 base::TimeTicks::Now())); | |
196 } | |
197 } | |
198 | |
199 void AutofillAgent::DidStartProvisionalLoad(WebFrame* frame) { | |
200 if (!frame->parent()) { | |
201 topmost_frame_ = NULL; | |
202 WebKit::WebURL provisional_url = | |
203 frame->provisionalDataSource()->request().url(); | |
204 WebKit::WebURL current_url = frame->dataSource()->request().url(); | |
205 // If the URL of the topmost frame is changing and the current page is part | |
206 // of an Autocheckout flow, the click was successful as long as the | |
207 // provisional load is committed. | |
208 if (provisional_url != current_url && click_timer_.IsRunning()) { | |
209 click_timer_.Stop(); | |
210 autocheckout_click_in_progress_ = true; | |
211 } | |
212 } | |
213 } | |
214 | |
215 void AutofillAgent::DidFailProvisionalLoad(WebFrame* frame, | |
216 const WebKit::WebURLError& error) { | |
217 if (autocheckout_click_in_progress_) { | |
218 autocheckout_click_in_progress_ = false; | |
219 ClickFailed(); | |
220 } | |
221 } | |
222 | |
223 void AutofillAgent::DidCommitProvisionalLoad(WebFrame* frame, | |
224 bool is_new_navigation) { | |
225 autocheckout_click_in_progress_ = false; | |
226 in_flight_request_form_.reset(); | |
227 } | |
228 | |
229 void AutofillAgent::FrameDetached(WebFrame* frame) { | |
230 form_cache_.ResetFrame(*frame); | |
231 if (!frame->parent()) { | |
232 // |frame| is about to be destroyed so we need to clear |top_most_frame_|. | |
233 topmost_frame_ = NULL; | |
234 click_timer_.Stop(); | |
235 } | |
236 } | |
237 | |
238 void AutofillAgent::WillSubmitForm(WebFrame* frame, | |
239 const WebFormElement& form) { | |
240 FormData form_data; | |
241 if (WebFormElementToFormData(form, | |
242 WebFormControlElement(), | |
243 REQUIRE_AUTOCOMPLETE, | |
244 static_cast<ExtractMask>( | |
245 EXTRACT_VALUE | EXTRACT_OPTION_TEXT), | |
246 &form_data, | |
247 NULL)) { | |
248 Send(new AutofillHostMsg_FormSubmitted(routing_id(), form_data, | |
249 base::TimeTicks::Now())); | |
250 } | |
251 } | |
252 | |
253 void AutofillAgent::ZoomLevelChanged() { | |
254 // Any time the zoom level changes, the page's content moves, so any Autofill | |
255 // popups should be hidden. This is only needed for the new Autofill UI | |
256 // because WebKit already knows to hide the old UI when this occurs. | |
257 HideHostPopups(); | |
258 } | |
259 | |
260 void AutofillAgent::DidChangeScrollOffset(WebKit::WebFrame*) { | |
261 HidePopups(); | |
262 } | |
263 | |
264 void AutofillAgent::didRequestAutocomplete(WebKit::WebFrame* frame, | |
265 const WebFormElement& form) { | |
266 FormData form_data; | |
267 if (!in_flight_request_form_.isNull() || | |
268 !WebFormElementToFormData(form, | |
269 WebFormControlElement(), | |
270 REQUIRE_AUTOCOMPLETE, | |
271 EXTRACT_OPTIONS, | |
272 &form_data, | |
273 NULL)) { | |
274 WebFormElement(form).finishRequestAutocomplete( | |
275 WebFormElement::AutocompleteResultErrorDisabled); | |
276 return; | |
277 } | |
278 | |
279 // Cancel any pending Autofill requests and hide any currently showing popups. | |
280 ++autofill_query_id_; | |
281 HidePopups(); | |
282 | |
283 in_flight_request_form_ = form; | |
284 // TODO(ramankk): Include SSLStatus within form_data and update the IPC. | |
285 Send(new AutofillHostMsg_RequestAutocomplete( | |
286 routing_id(), | |
287 form_data, | |
288 frame->document().url(), | |
289 render_view()->GetSSLStatusOfFrame(frame))); | |
290 } | |
291 | |
292 void AutofillAgent::setIgnoreTextChanges(bool ignore) { | |
293 ignore_text_changes_ = ignore; | |
294 } | |
295 | |
296 bool AutofillAgent::InputElementClicked(const WebInputElement& element, | |
297 bool was_focused, | |
298 bool is_focused) { | |
299 if (was_focused) | |
300 ShowSuggestions(element, true, false, true); | |
301 | |
302 return false; | |
303 } | |
304 | |
305 bool AutofillAgent::InputElementLostFocus() { | |
306 HideHostPopups(); | |
307 | |
308 return false; | |
309 } | |
310 | |
311 void AutofillAgent::didAcceptAutofillSuggestion(const WebNode& node, | |
312 const WebString& value, | |
313 const WebString& label, | |
314 int item_id, | |
315 unsigned index) { | |
316 if (password_autofill_manager_->DidAcceptAutofillSuggestion(node, value)) | |
317 return; | |
318 | |
319 DCHECK(node == element_); | |
320 | |
321 switch (item_id) { | |
322 case WebAutofillClient::MenuItemIDWarningMessage: | |
323 case WebAutofillClient::MenuItemIDSeparator: | |
324 NOTREACHED(); | |
325 break; | |
326 case WebAutofillClient::MenuItemIDAutofillOptions: | |
327 // User selected 'Autofill Options'. | |
328 Send(new AutofillHostMsg_ShowAutofillDialog(routing_id())); | |
329 break; | |
330 case WebAutofillClient::MenuItemIDClearForm: | |
331 // User selected 'Clear form'. | |
332 form_cache_.ClearFormWithElement(element_); | |
333 break; | |
334 case WebAutofillClient::MenuItemIDAutocompleteEntry: | |
335 case WebAutofillClient::MenuItemIDPasswordEntry: | |
336 // User selected an Autocomplete or password entry, so we fill directly. | |
337 SetNodeText(value, &element_); | |
338 break; | |
339 case WebAutofillClient::MenuItemIDDataListEntry: | |
340 AcceptDataListSuggestion(value); | |
341 break; | |
342 default: | |
343 // A positive item_id is a unique id for an autofill (vs. autocomplete) | |
344 // suggestion. | |
345 DCHECK_GT(item_id, 0); | |
346 // Fill the values for the whole form. | |
347 FillAutofillFormData(node, item_id, AUTOFILL_FILL); | |
348 } | |
349 } | |
350 | |
351 void AutofillAgent::didSelectAutofillSuggestion(const WebNode& node, | |
352 const WebString& value, | |
353 const WebString& label, | |
354 int item_id) { | |
355 if (password_autofill_manager_->DidSelectAutofillSuggestion(node)) | |
356 return; | |
357 | |
358 didClearAutofillSelection(node); | |
359 | |
360 if (item_id > 0) | |
361 FillAutofillFormData(node, item_id, AUTOFILL_PREVIEW); | |
362 } | |
363 | |
364 void AutofillAgent::didClearAutofillSelection(const WebNode& node) { | |
365 if (password_autofill_manager_->DidClearAutofillSelection(node)) | |
366 return; | |
367 | |
368 if (!element_.isNull() && node == element_) { | |
369 ClearPreviewedFormWithElement(element_, was_query_node_autofilled_); | |
370 } else { | |
371 // TODO(isherman): There seem to be rare cases where this code *is* | |
372 // reachable: see [ http://crbug.com/96321#c6 ]. Ideally we would | |
373 // understand those cases and fix the code to avoid them. However, so far I | |
374 // have been unable to reproduce such a case locally. If you hit this | |
375 // NOTREACHED(), please file a bug against me. | |
376 NOTREACHED(); | |
377 } | |
378 } | |
379 | |
380 void AutofillAgent::removeAutocompleteSuggestion(const WebString& name, | |
381 const WebString& value) { | |
382 Send(new AutofillHostMsg_RemoveAutocompleteEntry(routing_id(), name, value)); | |
383 } | |
384 | |
385 void AutofillAgent::textFieldDidEndEditing(const WebInputElement& element) { | |
386 password_autofill_manager_->TextFieldDidEndEditing(element); | |
387 has_shown_autofill_popup_for_current_edit_ = false; | |
388 Send(new AutofillHostMsg_DidEndTextFieldEditing(routing_id())); | |
389 } | |
390 | |
391 void AutofillAgent::textFieldDidChange(const WebInputElement& element) { | |
392 if (ignore_text_changes_) | |
393 return; | |
394 | |
395 if (did_set_node_text_) { | |
396 did_set_node_text_ = false; | |
397 return; | |
398 } | |
399 | |
400 // We post a task for doing the Autofill as the caret position is not set | |
401 // properly at this point (http://bugs.webkit.org/show_bug.cgi?id=16976) and | |
402 // it is needed to trigger autofill. | |
403 weak_ptr_factory_.InvalidateWeakPtrs(); | |
404 MessageLoop::current()->PostTask( | |
405 FROM_HERE, | |
406 base::Bind(&AutofillAgent::TextFieldDidChangeImpl, | |
407 weak_ptr_factory_.GetWeakPtr(), element)); | |
408 } | |
409 | |
410 void AutofillAgent::TextFieldDidChangeImpl(const WebInputElement& element) { | |
411 // If the element isn't focused then the changes don't matter. This check is | |
412 // required to properly handle IME interactions. | |
413 if (!element.focused()) | |
414 return; | |
415 | |
416 if (password_autofill_manager_->TextDidChangeInTextField(element)) { | |
417 element_ = element; | |
418 return; | |
419 } | |
420 | |
421 ShowSuggestions(element, false, true, false); | |
422 | |
423 FormData form; | |
424 FormFieldData field; | |
425 if (FindFormAndFieldForInputElement(element, &form, &field, REQUIRE_NONE)) { | |
426 Send(new AutofillHostMsg_TextFieldDidChange(routing_id(), form, field, | |
427 base::TimeTicks::Now())); | |
428 } | |
429 } | |
430 | |
431 void AutofillAgent::textFieldDidReceiveKeyDown(const WebInputElement& element, | |
432 const WebKeyboardEvent& event) { | |
433 if (password_autofill_manager_->TextFieldHandlingKeyDown(element, event)) { | |
434 element_ = element; | |
435 return; | |
436 } | |
437 | |
438 if (event.windowsKeyCode == ui::VKEY_DOWN || | |
439 event.windowsKeyCode == ui::VKEY_UP) | |
440 ShowSuggestions(element, true, true, true); | |
441 } | |
442 | |
443 void AutofillAgent::OnSuggestionsReturned(int query_id, | |
444 const std::vector<string16>& values, | |
445 const std::vector<string16>& labels, | |
446 const std::vector<string16>& icons, | |
447 const std::vector<int>& unique_ids) { | |
448 if (query_id != autofill_query_id_) | |
449 return; | |
450 | |
451 if (element_.isNull() || !element_.isFocusable()) | |
452 return; | |
453 | |
454 std::vector<string16> v(values); | |
455 std::vector<string16> l(labels); | |
456 std::vector<string16> i(icons); | |
457 std::vector<int> ids(unique_ids); | |
458 | |
459 if (!element_.autoComplete() && !v.empty()) { | |
460 // If autofill is disabled and we had suggestions, show a warning instead. | |
461 v.assign(1, l10n_util::GetStringUTF16(IDS_AUTOFILL_WARNING_FORM_DISABLED)); | |
462 l.assign(1, string16()); | |
463 i.assign(1, string16()); | |
464 ids.assign(1, WebAutofillClient::MenuItemIDWarningMessage); | |
465 } else if (ids.size() > 1 && | |
466 ids[0] == WebAutofillClient::MenuItemIDWarningMessage) { | |
467 // If we received an autofill warning plus some autocomplete suggestions, | |
468 // remove the autofill warning. | |
469 v.erase(v.begin()); | |
470 l.erase(l.begin()); | |
471 i.erase(i.begin()); | |
472 ids.erase(ids.begin()); | |
473 } | |
474 | |
475 // If we were about to show a warning and we shouldn't, don't. | |
476 if (!display_warning_if_disabled_ && !v.empty() && | |
477 ids[0] == WebAutofillClient::MenuItemIDWarningMessage) { | |
478 v.clear(); | |
479 l.clear(); | |
480 i.clear(); | |
481 ids.clear(); | |
482 } | |
483 | |
484 // Only include "Autofill Options" special menu item if we have Autofill | |
485 // items, identified by |unique_ids| having at least one valid value. | |
486 bool has_autofill_item = false; | |
487 for (size_t i = 0; i < ids.size(); ++i) { | |
488 if (ids[i] > 0) { | |
489 has_autofill_item = true; | |
490 break; | |
491 } | |
492 } | |
493 | |
494 if (has_autofill_item) { | |
495 v.push_back(string16()); | |
496 l.push_back(string16()); | |
497 i.push_back(string16()); | |
498 ids.push_back(WebAutofillClient::MenuItemIDSeparator); | |
499 | |
500 if (FormWithElementIsAutofilled(element_)) { | |
501 // The form has been auto-filled, so give the user the chance to clear the | |
502 // form. Append the 'Clear form' menu item. | |
503 v.push_back(l10n_util::GetStringUTF16(IDS_AUTOFILL_CLEAR_FORM_MENU_ITEM)); | |
504 l.push_back(string16()); | |
505 i.push_back(string16()); | |
506 ids.push_back(WebAutofillClient::MenuItemIDClearForm); | |
507 } | |
508 | |
509 // Append the 'Chrome Autofill options' menu item; | |
510 v.push_back(l10n_util::GetStringUTF16(IDS_AUTOFILL_OPTIONS_POPUP)); | |
511 l.push_back(string16()); | |
512 i.push_back(string16()); | |
513 ids.push_back(WebAutofillClient::MenuItemIDAutofillOptions); | |
514 } | |
515 | |
516 CombineDataListEntriesAndShow(element_, v, l, i, ids, has_autofill_item); | |
517 } | |
518 | |
519 void AutofillAgent::CombineDataListEntriesAndShow( | |
520 const WebKit::WebInputElement& element, | |
521 const std::vector<string16>& values, | |
522 const std::vector<string16>& labels, | |
523 const std::vector<string16>& icons, | |
524 const std::vector<int>& item_ids, | |
525 bool has_autofill_item) { | |
526 std::vector<string16> v; | |
527 std::vector<string16> l; | |
528 std::vector<string16> i; | |
529 std::vector<int> ids; | |
530 | |
531 AppendDataListSuggestions(element, &v, &l, &i, &ids); | |
532 | |
533 // If there are both <datalist> items and Autofill suggestions, add a | |
534 // separator between them. | |
535 if (!v.empty() && !values.empty()) { | |
536 v.push_back(string16()); | |
537 l.push_back(string16()); | |
538 i.push_back(string16()); | |
539 ids.push_back(WebAutofillClient::MenuItemIDSeparator); | |
540 } | |
541 | |
542 // Append the Autofill suggestions. | |
543 v.insert(v.end(), values.begin(), values.end()); | |
544 l.insert(l.end(), labels.begin(), labels.end()); | |
545 i.insert(i.end(), icons.begin(), icons.end()); | |
546 ids.insert(ids.end(), item_ids.begin(), item_ids.end()); | |
547 | |
548 if (v.empty()) { | |
549 // No suggestions, any popup currently showing is obsolete. | |
550 HidePopups(); | |
551 return; | |
552 } | |
553 | |
554 WebKit::WebView* web_view = render_view()->GetWebView(); | |
555 if (!web_view) | |
556 return; | |
557 | |
558 // Send to WebKit for display. | |
559 web_view->applyAutofillSuggestions(element, v, l, i, ids); | |
560 | |
561 Send(new AutofillHostMsg_DidShowAutofillSuggestions( | |
562 routing_id(), | |
563 has_autofill_item && !has_shown_autofill_popup_for_current_edit_)); | |
564 has_shown_autofill_popup_for_current_edit_ |= has_autofill_item; | |
565 } | |
566 | |
567 void AutofillAgent::AcceptDataListSuggestion(const string16& suggested_value) { | |
568 string16 new_value = suggested_value; | |
569 // If this element takes multiple values then replace the last part with | |
570 // the suggestion. | |
571 if (element_.isMultiple() && | |
572 element_.formControlType() == WebString::fromUTF8("email")) { | |
573 std::vector<string16> parts; | |
574 | |
575 base::SplitStringDontTrim(element_.editingValue(), ',', &parts); | |
576 if (parts.size() == 0) | |
577 parts.push_back(string16()); | |
578 | |
579 string16 last_part = parts.back(); | |
580 // We want to keep just the leading whitespace. | |
581 for (size_t i = 0; i < last_part.size(); ++i) { | |
582 if (!IsWhitespace(last_part[i])) { | |
583 last_part = last_part.substr(0, i); | |
584 break; | |
585 } | |
586 } | |
587 last_part.append(suggested_value); | |
588 parts[parts.size() - 1] = last_part; | |
589 | |
590 new_value = JoinString(parts, ','); | |
591 } | |
592 SetNodeText(new_value, &element_); | |
593 } | |
594 | |
595 void AutofillAgent::OnFormDataFilled(int query_id, | |
596 const FormData& form) { | |
597 if (!render_view()->GetWebView() || query_id != autofill_query_id_) | |
598 return; | |
599 | |
600 was_query_node_autofilled_ = element_.isAutofilled(); | |
601 | |
602 switch (autofill_action_) { | |
603 case AUTOFILL_FILL: | |
604 FillForm(form, element_); | |
605 Send(new AutofillHostMsg_DidFillAutofillFormData(routing_id(), | |
606 base::TimeTicks::Now())); | |
607 break; | |
608 case AUTOFILL_PREVIEW: | |
609 PreviewForm(form, element_); | |
610 Send(new AutofillHostMsg_DidPreviewAutofillFormData(routing_id())); | |
611 break; | |
612 default: | |
613 NOTREACHED(); | |
614 } | |
615 autofill_action_ = AUTOFILL_NONE; | |
616 } | |
617 | |
618 void AutofillAgent::OnFieldTypePredictionsAvailable( | |
619 const std::vector<FormDataPredictions>& forms) { | |
620 for (size_t i = 0; i < forms.size(); ++i) { | |
621 form_cache_.ShowPredictions(forms[i]); | |
622 } | |
623 } | |
624 | |
625 void AutofillAgent::OnSetAutofillActionFill() { | |
626 autofill_action_ = AUTOFILL_FILL; | |
627 } | |
628 | |
629 void AutofillAgent::OnClearForm() { | |
630 form_cache_.ClearFormWithElement(element_); | |
631 } | |
632 | |
633 void AutofillAgent::OnSetAutofillActionPreview() { | |
634 autofill_action_ = AUTOFILL_PREVIEW; | |
635 } | |
636 | |
637 void AutofillAgent::OnClearPreviewedForm() { | |
638 didClearAutofillSelection(element_); | |
639 } | |
640 | |
641 void AutofillAgent::OnSetNodeText(const string16& value) { | |
642 SetNodeText(value, &element_); | |
643 } | |
644 | |
645 void AutofillAgent::OnAcceptDataListSuggestion(const string16& value) { | |
646 AcceptDataListSuggestion(value); | |
647 } | |
648 | |
649 void AutofillAgent::OnAcceptPasswordAutofillSuggestion(const string16& value) { | |
650 // We need to make sure this is handled here because the browser process | |
651 // skipped it handling because it believed it would be handled here. If it | |
652 // isn't handled here then the browser logic needs to be updated. | |
653 bool handled = password_autofill_manager_->DidAcceptAutofillSuggestion( | |
654 element_, | |
655 value); | |
656 DCHECK(handled); | |
657 } | |
658 | |
659 void AutofillAgent::OnRequestAutocompleteResult( | |
660 WebFormElement::AutocompleteResult result, const FormData& form_data) { | |
661 if (in_flight_request_form_.isNull()) | |
662 return; | |
663 | |
664 if (result == WebFormElement::AutocompleteResultSuccess) | |
665 FillFormIncludingNonFocusableElements(form_data, in_flight_request_form_); | |
666 | |
667 in_flight_request_form_.finishRequestAutocomplete(result); | |
668 in_flight_request_form_.reset(); | |
669 } | |
670 | |
671 void AutofillAgent::OnFillFormsAndClick( | |
672 const std::vector<FormData>& forms, | |
673 const WebElementDescriptor& click_element_descriptor) { | |
674 DCHECK_EQ(forms.size(), form_elements_.size()); | |
675 | |
676 // Fill the form. | |
677 for (size_t i = 0; i < forms.size(); ++i) | |
678 FillFormIncludingNonFocusableElements(forms[i], form_elements_[i]); | |
679 | |
680 // It's possible that clicking the element to proceed in an Autocheckout | |
681 // flow will not actually proceed to the next step in the flow, e.g. there | |
682 // is a new required field that Autocheckout does not know how to fill. In | |
683 // order to capture this case and present the user with an error a timer is | |
684 // set that informs the browser of the error. |click_timer_| has to be started | |
685 // before clicking so it can start before DidStartProvisionalLoad started. | |
686 click_timer_.Start(FROM_HERE, | |
687 base::TimeDelta::FromSeconds(kAutocheckoutClickTimeout), | |
688 this, | |
689 &AutofillAgent::ClickFailed); | |
690 if (!ClickElement(topmost_frame_->document(), | |
691 click_element_descriptor)) { | |
692 click_timer_.Stop(); | |
693 Send(new AutofillHostMsg_ClickFailed(routing_id(), | |
694 MISSING_ADVANCE)); | |
695 } | |
696 } | |
697 | |
698 void AutofillAgent::ClickFailed() { | |
699 Send(new AutofillHostMsg_ClickFailed(routing_id(), | |
700 CANNOT_PROCEED)); | |
701 } | |
702 | |
703 void AutofillAgent::ShowSuggestions(const WebInputElement& element, | |
704 bool autofill_on_empty_values, | |
705 bool requires_caret_at_end, | |
706 bool display_warning_if_disabled) { | |
707 if (!element.isEnabled() || element.isReadOnly() || !element.isTextField() || | |
708 element.isPasswordField() || !element.suggestedValue().isEmpty()) | |
709 return; | |
710 | |
711 // Don't attempt to autofill with values that are too large or if filling | |
712 // criteria are not met. | |
713 WebString value = element.editingValue(); | |
714 if (value.length() > kMaximumTextSizeForAutofill || | |
715 (!autofill_on_empty_values && value.isEmpty()) || | |
716 (requires_caret_at_end && | |
717 (element.selectionStart() != element.selectionEnd() || | |
718 element.selectionEnd() != static_cast<int>(value.length())))) { | |
719 // Any popup currently showing is obsolete. | |
720 HidePopups(); | |
721 return; | |
722 } | |
723 | |
724 element_ = element; | |
725 | |
726 // If autocomplete is disabled at the form level, then we might want to show a | |
727 // warning in place of suggestions. However, if autocomplete is disabled | |
728 // specifically for this field, we never want to show a warning. Otherwise, | |
729 // we might interfere with custom popups (e.g. search suggestions) used by the | |
730 // website. Note that we cannot use the WebKit method element.autoComplete() | |
731 // as it does not allow us to distinguish the case where autocomplete is | |
732 // disabled for *both* the element and for the form. | |
733 // Also, if the field has no name, then we won't have values. | |
734 const string16 autocomplete_attribute = element.getAttribute("autocomplete"); | |
735 if (LowerCaseEqualsASCII(autocomplete_attribute, "off") || | |
736 element.nameForAutofill().isEmpty()) { | |
737 CombineDataListEntriesAndShow(element, std::vector<string16>(), | |
738 std::vector<string16>(), | |
739 std::vector<string16>(), | |
740 std::vector<int>(), false); | |
741 return; | |
742 } | |
743 | |
744 QueryAutofillSuggestions(element, display_warning_if_disabled); | |
745 } | |
746 | |
747 void AutofillAgent::QueryAutofillSuggestions(const WebInputElement& element, | |
748 bool display_warning_if_disabled) { | |
749 if (!element.document().frame()) | |
750 return; | |
751 | |
752 static int query_counter = 0; | |
753 autofill_query_id_ = query_counter++; | |
754 display_warning_if_disabled_ = display_warning_if_disabled; | |
755 | |
756 // If autocomplete is disabled at the form level, we want to see if there | |
757 // would have been any suggestions were it enabled, so that we can show a | |
758 // warning. Otherwise, we want to ignore fields that disable autocomplete, so | |
759 // that the suggestions list does not include suggestions for these form | |
760 // fields -- see comment 1 on http://crbug.com/69914 | |
761 // Rather than testing the form's autocomplete enabled state, we test the | |
762 // element's state. The DCHECK below ensures that this is equivalent. | |
763 DCHECK(element.autoComplete() || !element.form().autoComplete()); | |
764 const RequirementsMask requirements = | |
765 element.autoComplete() ? REQUIRE_AUTOCOMPLETE : REQUIRE_NONE; | |
766 | |
767 FormData form; | |
768 FormFieldData field; | |
769 if (!FindFormAndFieldForInputElement(element, &form, &field, requirements)) { | |
770 // If we didn't find the cached form, at least let autocomplete have a shot | |
771 // at providing suggestions. | |
772 WebFormControlElementToFormField(element, EXTRACT_VALUE, &field); | |
773 } | |
774 | |
775 gfx::Rect bounding_box(element_.boundsInViewportSpace()); | |
776 | |
777 float scale = web_view_->pageScaleFactor(); | |
778 gfx::RectF bounding_box_scaled(bounding_box.x() * scale, | |
779 bounding_box.y() * scale, | |
780 bounding_box.width() * scale, | |
781 bounding_box.height() * scale); | |
782 | |
783 // Find the datalist values and send them to the browser process. | |
784 std::vector<string16> data_list_values; | |
785 std::vector<string16> data_list_labels; | |
786 std::vector<string16> data_list_icons; | |
787 std::vector<int> data_list_unique_ids; | |
788 AppendDataListSuggestions(element_, | |
789 &data_list_values, | |
790 &data_list_labels, | |
791 &data_list_icons, | |
792 &data_list_unique_ids); | |
793 | |
794 TrimDataListsForIPC(&data_list_values, | |
795 &data_list_labels, | |
796 &data_list_icons, | |
797 &data_list_unique_ids); | |
798 | |
799 Send(new AutofillHostMsg_SetDataList(routing_id(), | |
800 data_list_values, | |
801 data_list_labels, | |
802 data_list_icons, | |
803 data_list_unique_ids)); | |
804 | |
805 // Add SSL Status in the formdata to let browser process alert user | |
806 // appropriately using browser UI. | |
807 form.ssl_status = render_view()->GetSSLStatusOfFrame( | |
808 element.document().frame()); | |
809 Send(new AutofillHostMsg_QueryFormFieldAutofill(routing_id(), | |
810 autofill_query_id_, | |
811 form, | |
812 field, | |
813 bounding_box_scaled, | |
814 display_warning_if_disabled)); | |
815 } | |
816 | |
817 void AutofillAgent::FillAutofillFormData(const WebNode& node, | |
818 int unique_id, | |
819 AutofillAction action) { | |
820 DCHECK_GT(unique_id, 0); | |
821 | |
822 static int query_counter = 0; | |
823 autofill_query_id_ = query_counter++; | |
824 | |
825 FormData form; | |
826 FormFieldData field; | |
827 if (!FindFormAndFieldForInputElement(node.toConst<WebInputElement>(), &form, | |
828 &field, REQUIRE_AUTOCOMPLETE)) { | |
829 return; | |
830 } | |
831 | |
832 autofill_action_ = action; | |
833 Send(new AutofillHostMsg_FillAutofillFormData( | |
834 routing_id(), autofill_query_id_, form, field, unique_id)); | |
835 } | |
836 | |
837 void AutofillAgent::SetNodeText(const string16& value, | |
838 WebKit::WebInputElement* node) { | |
839 did_set_node_text_ = true; | |
840 string16 substring = value; | |
841 substring = substring.substr(0, node->maxLength()); | |
842 | |
843 node->setEditingValue(substring); | |
844 } | |
845 | |
846 void AutofillAgent::HidePopups() { | |
847 WebKit::WebView* web_view = render_view()->GetWebView(); | |
848 if (web_view) | |
849 web_view->hidePopups(); | |
850 | |
851 HideHostPopups(); | |
852 } | |
853 | |
854 void AutofillAgent::HideHostPopups() { | |
855 Send(new AutofillHostMsg_HideAutofillPopup(routing_id())); | |
856 } | |
857 | |
858 } // namespace autofill | |
OLD | NEW |