Remove FrameDetached and FrameWillClose listeners from AutofillAgent.
[chromium-blink-merge.git] / components / autofill / content / renderer / password_autofill_agent.cc
blob34068a0ee9cd1270787722b2ab8fad12ab75f0e1
1 // Copyright 2013 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.
5 #include "components/autofill/content/renderer/password_autofill_agent.h"
7 #include "base/bind.h"
8 #include "base/command_line.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/metrics/field_trial.h"
12 #include "base/metrics/histogram.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "components/autofill/content/common/autofill_messages.h"
15 #include "components/autofill/content/renderer/form_autofill_util.h"
16 #include "components/autofill/content/renderer/password_form_conversion_utils.h"
17 #include "components/autofill/content/renderer/renderer_save_password_progress_logger.h"
18 #include "components/autofill/core/common/autofill_constants.h"
19 #include "components/autofill/core/common/autofill_switches.h"
20 #include "components/autofill/core/common/form_field_data.h"
21 #include "components/autofill/core/common/password_form.h"
22 #include "components/autofill/core/common/password_form_fill_data.h"
23 #include "content/public/renderer/document_state.h"
24 #include "content/public/renderer/navigation_state.h"
25 #include "content/public/renderer/render_frame.h"
26 #include "content/public/renderer/render_view.h"
27 #include "third_party/WebKit/public/platform/WebVector.h"
28 #include "third_party/WebKit/public/web/WebAutofillClient.h"
29 #include "third_party/WebKit/public/web/WebDocument.h"
30 #include "third_party/WebKit/public/web/WebElement.h"
31 #include "third_party/WebKit/public/web/WebFormElement.h"
32 #include "third_party/WebKit/public/web/WebInputEvent.h"
33 #include "third_party/WebKit/public/web/WebLocalFrame.h"
34 #include "third_party/WebKit/public/web/WebNode.h"
35 #include "third_party/WebKit/public/web/WebNodeList.h"
36 #include "third_party/WebKit/public/web/WebSecurityOrigin.h"
37 #include "third_party/WebKit/public/web/WebUserGestureIndicator.h"
38 #include "third_party/WebKit/public/web/WebView.h"
39 #include "ui/base/page_transition_types.h"
40 #include "ui/events/keycodes/keyboard_codes.h"
41 #include "url/gurl.h"
43 namespace autofill {
44 namespace {
46 // The size above which we stop triggering autocomplete.
47 static const size_t kMaximumTextSizeForAutocomplete = 1000;
49 // Experiment information
50 const char kFillOnAccountSelectFieldTrialName[] = "FillOnAccountSelect";
51 const char kFillOnAccountSelectFieldTrialEnabledWithHighlightGroup[] =
52 "EnableWithHighlight";
53 const char kFillOnAccountSelectFieldTrialEnabledWithNoHighlightGroup[] =
54 "EnableWithNoHighlight";
56 // Maps element names to the actual elements to simplify form filling.
57 typedef std::map<base::string16, blink::WebInputElement> FormInputElementMap;
59 // Use the shorter name when referencing SavePasswordProgressLogger::StringID
60 // values to spare line breaks. The code provides enough context for that
61 // already.
62 typedef SavePasswordProgressLogger Logger;
64 // Utility struct for form lookup and autofill. When we parse the DOM to look up
65 // a form, in addition to action and origin URL's we have to compare all
66 // necessary form elements. To avoid having to look these up again when we want
67 // to fill the form, the FindFormElements function stores the pointers
68 // in a FormElements* result, referenced to ensure they are safe to use.
69 struct FormElements {
70 blink::WebFormElement form_element;
71 FormInputElementMap input_elements;
74 typedef std::vector<FormElements*> FormElementsList;
76 bool FillDataContainsUsername(const PasswordFormFillData& fill_data) {
77 return !fill_data.username_field.name.empty();
80 // Utility function to find the unique entry of the |form_element| for the
81 // specified input |field|. On successful find, adds it to |result| and returns
82 // |true|. Otherwise clears the references from each |HTMLInputElement| from
83 // |result| and returns |false|.
84 bool FindFormInputElement(blink::WebFormElement* form_element,
85 const FormFieldData& field,
86 FormElements* result) {
87 blink::WebVector<blink::WebNode> temp_elements;
88 form_element->getNamedElements(field.name, temp_elements);
90 // Match the first input element, if any.
91 // |getNamedElements| may return non-input elements where the names match,
92 // so the results are filtered for input elements.
93 // If more than one match is made, then we have ambiguity (due to misuse
94 // of "name" attribute) so is it considered not found.
95 bool found_input = false;
96 for (size_t i = 0; i < temp_elements.size(); ++i) {
97 if (temp_elements[i].to<blink::WebElement>().hasHTMLTagName("input")) {
98 // Check for a non-unique match.
99 if (found_input) {
100 found_input = false;
101 break;
104 // Only fill saved passwords into password fields and usernames into
105 // text fields.
106 blink::WebInputElement input_element =
107 temp_elements[i].to<blink::WebInputElement>();
108 if (input_element.isPasswordField() !=
109 (field.form_control_type == "password"))
110 continue;
112 // This element matched, add it to our temporary result. It's possible
113 // there are multiple matches, but for purposes of identifying the form
114 // one suffices and if some function needs to deal with multiple
115 // matching elements it can get at them through the FormElement*.
116 // Note: This assignment adds a reference to the InputElement.
117 result->input_elements[field.name] = input_element;
118 found_input = true;
122 // A required element was not found. This is not the right form.
123 // Make sure no input elements from a partially matched form in this
124 // iteration remain in the result set.
125 // Note: clear will remove a reference from each InputElement.
126 if (!found_input) {
127 result->input_elements.clear();
128 return false;
131 return true;
134 bool ShouldFillOnAccountSelect() {
135 std::string group_name =
136 base::FieldTrialList::FindFullName(kFillOnAccountSelectFieldTrialName);
138 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
139 switches::kDisableFillOnAccountSelect)) {
140 return false;
143 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
144 switches::kEnableFillOnAccountSelect) ||
145 base::CommandLine::ForCurrentProcess()->HasSwitch(
146 switches::kEnableFillOnAccountSelectNoHighlighting)) {
147 return true;
150 return group_name ==
151 kFillOnAccountSelectFieldTrialEnabledWithHighlightGroup ||
152 group_name ==
153 kFillOnAccountSelectFieldTrialEnabledWithNoHighlightGroup;
156 bool ShouldHighlightFields() {
157 std::string group_name =
158 base::FieldTrialList::FindFullName(kFillOnAccountSelectFieldTrialName);
159 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
160 switches::kDisableFillOnAccountSelect) ||
161 base::CommandLine::ForCurrentProcess()->HasSwitch(
162 switches::kEnableFillOnAccountSelect)) {
163 return true;
166 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
167 switches::kEnableFillOnAccountSelectNoHighlighting)) {
168 return false;
171 return group_name !=
172 kFillOnAccountSelectFieldTrialEnabledWithNoHighlightGroup;
175 // Helper to search the given form element for the specified input elements in
176 // |data|, and add results to |result|.
177 bool FindFormInputElements(blink::WebFormElement* form_element,
178 const PasswordFormFillData& data,
179 FormElements* result) {
180 return FindFormInputElement(form_element, data.password_field, result) &&
181 (!FillDataContainsUsername(data) ||
182 FindFormInputElement(form_element, data.username_field, result));
185 // Helper to locate form elements identified by |data|.
186 void FindFormElements(content::RenderFrame* render_frame,
187 const PasswordFormFillData& data,
188 FormElementsList* results) {
189 DCHECK(results);
191 GURL::Replacements rep;
192 rep.ClearQuery();
193 rep.ClearRef();
195 blink::WebDocument doc = render_frame->GetWebFrame()->document();
196 if (!doc.isHTMLDocument())
197 return;
199 GURL full_origin(doc.url());
200 if (data.origin != full_origin.ReplaceComponents(rep))
201 return;
203 blink::WebVector<blink::WebFormElement> forms;
204 doc.forms(forms);
206 for (size_t i = 0; i < forms.size(); ++i) {
207 blink::WebFormElement fe = forms[i];
209 GURL full_action(doc.completeURL(fe.action()));
210 if (full_action.is_empty()) {
211 // The default action URL is the form's origin.
212 full_action = full_origin;
215 // Action URL must match.
216 if (data.action != full_action.ReplaceComponents(rep))
217 continue;
219 scoped_ptr<FormElements> curr_elements(new FormElements);
220 if (!FindFormInputElements(&fe, data, curr_elements.get()))
221 continue;
223 // We found the right element.
224 // Note: this assignment adds a reference to |fe|.
225 curr_elements->form_element = fe;
226 results->push_back(curr_elements.release());
230 bool IsElementEditable(const blink::WebInputElement& element) {
231 return element.isEnabled() && !element.isReadOnly();
234 bool DoUsernamesMatch(const base::string16& username1,
235 const base::string16& username2,
236 bool exact_match) {
237 if (exact_match)
238 return username1 == username2;
239 return StartsWith(username1, username2, true);
242 // Returns |true| if the given element is editable. Otherwise, returns |false|.
243 bool IsElementAutocompletable(const blink::WebInputElement& element) {
244 return IsElementEditable(element);
247 // Returns true if the password specified in |form| is a default value.
248 bool PasswordValueIsDefault(const base::string16& password_element,
249 const base::string16& password_value,
250 blink::WebFormElement form_element) {
251 blink::WebVector<blink::WebNode> temp_elements;
252 form_element.getNamedElements(password_element, temp_elements);
254 // We are loose in our definition here and will return true if any of the
255 // appropriately named elements match the element to be saved. Currently
256 // we ignore filling passwords where naming is ambigious anyway.
257 for (size_t i = 0; i < temp_elements.size(); ++i) {
258 if (temp_elements[i].to<blink::WebElement>().getAttribute("value") ==
259 password_value)
260 return true;
262 return false;
265 // Return true if either password_value or new_password_value is not empty and
266 // not default.
267 bool FormContainsNonDefaultPasswordValue(const PasswordForm& password_form,
268 blink::WebFormElement form_element) {
269 return (!password_form.password_value.empty() &&
270 !PasswordValueIsDefault(password_form.password_element,
271 password_form.password_value,
272 form_element)) ||
273 (!password_form.new_password_value.empty() &&
274 !PasswordValueIsDefault(password_form.new_password_element,
275 password_form.new_password_value,
276 form_element));
279 // Log a message including the name, method and action of |form|.
280 void LogHTMLForm(SavePasswordProgressLogger* logger,
281 SavePasswordProgressLogger::StringID message_id,
282 const blink::WebFormElement& form) {
283 logger->LogHTMLForm(message_id,
284 form.name().utf8(),
285 GURL(form.action().utf8()));
288 // Sets |suggestions_present| to true if there are any suggestions to be derived
289 // from |fill_data|. Unless |show_all| is true, only considers suggestions with
290 // usernames having |current_username| as a prefix. Returns true if a username
291 // from the |fill_data.other_possible_usernames| would be included in the
292 // suggestions.
293 bool GetSuggestionsStats(const PasswordFormFillData& fill_data,
294 const base::string16& current_username,
295 bool show_all,
296 bool* suggestions_present) {
297 *suggestions_present = false;
299 for (const auto& usernames : fill_data.other_possible_usernames) {
300 for (size_t i = 0; i < usernames.second.size(); ++i) {
301 if (show_all ||
302 StartsWith(usernames.second[i], current_username, false)) {
303 *suggestions_present = true;
304 return true;
309 if (show_all ||
310 StartsWith(fill_data.username_field.value, current_username, false)) {
311 *suggestions_present = true;
312 return false;
315 for (const auto& login : fill_data.additional_logins) {
316 if (show_all || StartsWith(login.first, current_username, false)) {
317 *suggestions_present = true;
318 return false;
322 return false;
325 // Returns true if there exists a credential suggestion whose username field is
326 // an exact match to the current username (not just a prefix).
327 bool HasExactMatchSuggestion(const PasswordFormFillData& fill_data,
328 const base::string16& current_username) {
329 if (fill_data.username_field.value == current_username)
330 return true;
332 for (const auto& usernames : fill_data.other_possible_usernames) {
333 for (const auto& username_string : usernames.second) {
334 if (username_string == current_username)
335 return true;
339 for (const auto& login : fill_data.additional_logins) {
340 if (login.first == current_username)
341 return true;
344 return false;
347 // This function attempts to fill |username_element| and |password_element|
348 // with values from |fill_data|. The |password_element| will only have the
349 // |suggestedValue| set, and will be registered for copying that to the real
350 // value through |registration_callback|. The function returns true when
351 // selected username comes from |fill_data.other_possible_usernames|. |options|
352 // should be a bitwise mask of FillUserNameAndPasswordOptions values.
353 bool FillUserNameAndPassword(
354 blink::WebInputElement* username_element,
355 blink::WebInputElement* password_element,
356 const PasswordFormFillData& fill_data,
357 bool exact_username_match,
358 bool set_selection,
359 base::Callback<void(blink::WebInputElement*)> registration_callback) {
360 bool other_possible_username_selected = false;
361 // Don't fill username if password can't be set.
362 if (!IsElementAutocompletable(*password_element))
363 return false;
365 base::string16 current_username;
366 if (!username_element->isNull()) {
367 current_username = username_element->value();
370 // username and password will contain the match found if any.
371 base::string16 username;
372 base::string16 password;
374 // Look for any suitable matches to current field text.
375 if (DoUsernamesMatch(fill_data.username_field.value, current_username,
376 exact_username_match)) {
377 username = fill_data.username_field.value;
378 password = fill_data.password_field.value;
379 } else {
380 // Scan additional logins for a match.
381 PasswordFormFillData::LoginCollection::const_iterator iter;
382 for (iter = fill_data.additional_logins.begin();
383 iter != fill_data.additional_logins.end();
384 ++iter) {
385 if (DoUsernamesMatch(
386 iter->first, current_username, exact_username_match)) {
387 username = iter->first;
388 password = iter->second.password;
389 break;
393 // Check possible usernames.
394 if (username.empty() && password.empty()) {
395 for (PasswordFormFillData::UsernamesCollection::const_iterator iter =
396 fill_data.other_possible_usernames.begin();
397 iter != fill_data.other_possible_usernames.end();
398 ++iter) {
399 for (size_t i = 0; i < iter->second.size(); ++i) {
400 if (DoUsernamesMatch(
401 iter->second[i], current_username, exact_username_match)) {
402 other_possible_username_selected = true;
403 username = iter->second[i];
404 password = iter->first.password;
405 break;
408 if (!username.empty() && !password.empty())
409 break;
413 if (password.empty())
414 return other_possible_username_selected; // No match was found.
416 // TODO(tkent): Check maxlength and pattern for both username and password
417 // fields.
419 // Input matches the username, fill in required values.
420 if (!username_element->isNull() &&
421 IsElementAutocompletable(*username_element)) {
422 username_element->setValue(username, true);
423 username_element->setAutofilled(true);
425 if (set_selection) {
426 username_element->setSelectionRange(current_username.length(),
427 username.length());
429 } else if (current_username != username) {
430 // If the username can't be filled and it doesn't match a saved password
431 // as is, don't autofill a password.
432 return other_possible_username_selected;
435 // Wait to fill in the password until a user gesture occurs. This is to make
436 // sure that we do not fill in the DOM with a password until we believe the
437 // user is intentionally interacting with the page.
438 password_element->setSuggestedValue(password);
439 registration_callback.Run(password_element);
441 password_element->setAutofilled(true);
442 return other_possible_username_selected;
445 // Attempts to fill |username_element| and |password_element| with the
446 // |fill_data|. Will use the data corresponding to the preferred username,
447 // unless the |username_element| already has a value set. In that case,
448 // attempts to fill the password matching the already filled username, if
449 // such a password exists. The |password_element| will have the
450 // |suggestedValue| set, and |suggestedValue| will be registered for copying to
451 // the real value through |registration_callback|. Returns true when the
452 // username gets selected from |other_possible_usernames|, else returns false.
453 bool FillFormOnPasswordReceived(
454 const PasswordFormFillData& fill_data,
455 blink::WebInputElement username_element,
456 blink::WebInputElement password_element,
457 base::Callback<void(blink::WebInputElement*)> registration_callback) {
458 // Do not fill if the password field is in an iframe.
459 DCHECK(password_element.document().frame());
460 if (password_element.document().frame()->parent())
461 return false;
463 // If we can't modify the password, don't try to set the username
464 if (!IsElementAutocompletable(password_element))
465 return false;
467 bool form_contains_username_field = FillDataContainsUsername(fill_data);
468 // If the form contains an autocompletable username field, try to set the
469 // username to the preferred name, but only if:
470 // (a) The fill-on-account-select flag is not set, and
471 // (b) The username element isn't prefilled
473 // If (a) is false, then just mark the username element as autofilled if the
474 // user is not in the "no highlighting" group and return so the fill step is
475 // skipped.
477 // If there is no autocompletable username field, and (a) is false, then the
478 // username element cannot be autofilled, but the user should still be able to
479 // select to fill the password element, so the password element must be marked
480 // as autofilled and the fill step should also be skipped if the user is not
481 // in the "no highlighting" group.
483 // In all other cases, do nothing.
484 bool form_has_fillable_username = form_contains_username_field &&
485 IsElementAutocompletable(username_element);
487 if (ShouldFillOnAccountSelect()) {
488 if (!ShouldHighlightFields()) {
489 return false;
492 if (form_has_fillable_username) {
493 username_element.setAutofilled(true);
494 } else if (username_element.isNull() ||
495 HasExactMatchSuggestion(fill_data, username_element.value())) {
496 password_element.setAutofilled(true);
498 return false;
501 if (form_has_fillable_username && username_element.value().isEmpty()) {
502 // TODO(tkent): Check maxlength and pattern.
503 username_element.setValue(fill_data.username_field.value, true);
506 // Fill if we have an exact match for the username. Note that this sets
507 // username to autofilled.
508 return FillUserNameAndPassword(&username_element,
509 &password_element,
510 fill_data,
511 true /* exact_username_match */,
512 false /* set_selection */,
513 registration_callback);
516 // Takes a |map| with pointers as keys and linked_ptr as values, and returns
517 // true if |key| is not NULL and |map| contains a non-NULL entry for |key|.
518 // Makes sure not to create an entry as a side effect of using the operator [].
519 template <class Key, class Value>
520 bool ContainsNonNullEntryForNonNullKey(
521 const std::map<Key*, linked_ptr<Value>>& map,
522 Key* key) {
523 if (!key)
524 return false;
525 auto it = map.find(key);
526 return it != map.end() && it->second.get();
529 } // namespace
531 ////////////////////////////////////////////////////////////////////////////////
532 // PasswordAutofillAgent, public:
534 PasswordAutofillAgent::PasswordAutofillAgent(content::RenderFrame* render_frame)
535 : content::RenderFrameObserver(render_frame),
536 legacy_(render_frame->GetRenderView(), this),
537 usernames_usage_(NOTHING_TO_AUTOFILL),
538 logging_state_active_(false),
539 was_username_autofilled_(false),
540 was_password_autofilled_(false),
541 username_selection_start_(0),
542 did_stop_loading_(false),
543 weak_ptr_factory_(this) {
544 Send(new AutofillHostMsg_PasswordAutofillAgentConstructed(routing_id()));
547 PasswordAutofillAgent::~PasswordAutofillAgent() {
550 PasswordAutofillAgent::PasswordValueGatekeeper::PasswordValueGatekeeper()
551 : was_user_gesture_seen_(false) {
554 PasswordAutofillAgent::PasswordValueGatekeeper::~PasswordValueGatekeeper() {
557 void PasswordAutofillAgent::PasswordValueGatekeeper::RegisterElement(
558 blink::WebInputElement* element) {
559 if (was_user_gesture_seen_)
560 ShowValue(element);
561 else
562 elements_.push_back(*element);
565 void PasswordAutofillAgent::PasswordValueGatekeeper::OnUserGesture() {
566 was_user_gesture_seen_ = true;
568 for (std::vector<blink::WebInputElement>::iterator it = elements_.begin();
569 it != elements_.end();
570 ++it) {
571 ShowValue(&(*it));
574 elements_.clear();
577 void PasswordAutofillAgent::PasswordValueGatekeeper::Reset() {
578 was_user_gesture_seen_ = false;
579 elements_.clear();
582 void PasswordAutofillAgent::PasswordValueGatekeeper::ShowValue(
583 blink::WebInputElement* element) {
584 if (!element->isNull() && !element->suggestedValue().isEmpty())
585 element->setValue(element->suggestedValue(), true);
588 bool PasswordAutofillAgent::TextFieldDidEndEditing(
589 const blink::WebInputElement& element) {
590 LoginToPasswordInfoMap::const_iterator iter =
591 login_to_password_info_.find(element);
592 if (iter == login_to_password_info_.end())
593 return false;
595 const PasswordInfo& password_info = iter->second;
596 // Don't let autofill overwrite an explicit change made by the user.
597 if (password_info.password_was_edited_last)
598 return false;
600 const PasswordFormFillData& fill_data = password_info.fill_data;
602 // If wait_for_username is false, we should have filled when the text changed.
603 if (!fill_data.wait_for_username)
604 return false;
606 blink::WebInputElement password = password_info.password_field;
607 if (!IsElementEditable(password))
608 return false;
610 blink::WebInputElement username = element; // We need a non-const.
612 // Do not set selection when ending an editing session, otherwise it can
613 // mess with focus.
614 if (FillUserNameAndPassword(
615 &username, &password, fill_data, true, false,
616 base::Bind(&PasswordValueGatekeeper::RegisterElement,
617 base::Unretained(&gatekeeper_)))) {
618 usernames_usage_ = OTHER_POSSIBLE_USERNAME_SELECTED;
620 return true;
623 bool PasswordAutofillAgent::TextDidChangeInTextField(
624 const blink::WebInputElement& element) {
625 // TODO(vabr): Get a mutable argument instead. http://crbug.com/397083
626 blink::WebInputElement mutable_element = element; // We need a non-const.
628 if (element.isTextField())
629 user_modified_elements_[element] = element.value();
631 DCHECK_EQ(element.document().frame(), render_frame()->GetWebFrame());
633 if (element.isPasswordField()) {
634 // Some login forms have event handlers that put a hash of the password into
635 // a hidden field and then clear the password (http://crbug.com/28910,
636 // http://crbug.com/391693). This method gets called before any of those
637 // handlers run, so save away a copy of the password in case it gets lost.
638 // To honor the user having explicitly cleared the password, even an empty
639 // password will be saved here.
640 ProvisionallySavePassword(element.form(), RESTRICTION_NONE);
642 PasswordToLoginMap::iterator iter = password_to_username_.find(element);
643 if (iter != password_to_username_.end()) {
644 login_to_password_info_[iter->second].password_was_edited_last = true;
645 // Note that the suggested value of |mutable_element| was reset when its
646 // value changed.
647 mutable_element.setAutofilled(false);
649 return false;
652 LoginToPasswordInfoMap::iterator iter = login_to_password_info_.find(element);
653 if (iter == login_to_password_info_.end())
654 return false;
656 // The input text is being changed, so any autofilled password is now
657 // outdated.
658 mutable_element.setAutofilled(false);
659 iter->second.password_was_edited_last = false;
661 blink::WebInputElement password = iter->second.password_field;
662 if (password.isAutofilled()) {
663 password.setValue(base::string16(), true);
664 password.setAutofilled(false);
667 // If wait_for_username is true we will fill when the username loses focus.
668 if (iter->second.fill_data.wait_for_username)
669 return false;
671 if (!element.isText() || !IsElementAutocompletable(element) ||
672 !IsElementAutocompletable(password)) {
673 return false;
676 // Don't inline autocomplete if the user is deleting, that would be confusing.
677 // But refresh the popup. Note, since this is ours, return true to signal
678 // no further processing is required.
679 if (iter->second.backspace_pressed_last) {
680 ShowSuggestionPopup(iter->second.fill_data, element, false, false);
681 return true;
684 blink::WebString name = element.nameForAutofill();
685 if (name.isEmpty())
686 return false; // If the field has no name, then we won't have values.
688 // Don't attempt to autofill with values that are too large.
689 if (element.value().length() > kMaximumTextSizeForAutocomplete)
690 return false;
692 // The caret position should have already been updated.
693 PerformInlineAutocomplete(element, password, iter->second.fill_data);
694 return true;
697 bool PasswordAutofillAgent::TextFieldHandlingKeyDown(
698 const blink::WebInputElement& element,
699 const blink::WebKeyboardEvent& event) {
700 // If using the new Autofill UI that lives in the browser, it will handle
701 // keypresses before this function. This is not currently an issue but if
702 // the keys handled there or here change, this issue may appear.
704 LoginToPasswordInfoMap::iterator iter = login_to_password_info_.find(element);
705 if (iter == login_to_password_info_.end())
706 return false;
708 int win_key_code = event.windowsKeyCode;
709 iter->second.backspace_pressed_last =
710 (win_key_code == ui::VKEY_BACK || win_key_code == ui::VKEY_DELETE);
711 return true;
714 bool PasswordAutofillAgent::FillSuggestion(
715 const blink::WebNode& node,
716 const blink::WebString& username,
717 const blink::WebString& password) {
718 // The element in context of the suggestion popup.
719 blink::WebInputElement filled_element;
720 PasswordInfo* password_info;
722 if (!FindLoginInfo(node, &filled_element, &password_info) ||
723 !IsElementAutocompletable(filled_element) ||
724 !IsElementAutocompletable(password_info->password_field)) {
725 return false;
728 password_info->password_was_edited_last = false;
729 // Note that in cases where filled_element is the password element, the value
730 // gets overwritten with the correct one below.
731 filled_element.setValue(username, true);
732 filled_element.setAutofilled(true);
734 password_info->password_field.setValue(password, true);
735 password_info->password_field.setAutofilled(true);
737 filled_element.setSelectionRange(filled_element.value().length(),
738 filled_element.value().length());
740 return true;
743 bool PasswordAutofillAgent::PreviewSuggestion(
744 const blink::WebNode& node,
745 const blink::WebString& username,
746 const blink::WebString& password) {
747 blink::WebInputElement username_element;
748 PasswordInfo* password_info;
750 if (!FindLoginInfo(node, &username_element, &password_info) ||
751 !IsElementAutocompletable(username_element) ||
752 !IsElementAutocompletable(password_info->password_field)) {
753 return false;
756 was_username_autofilled_ = username_element.isAutofilled();
757 username_selection_start_ = username_element.selectionStart();
758 username_element.setSuggestedValue(username);
759 username_element.setAutofilled(true);
760 username_element.setSelectionRange(
761 username_selection_start_,
762 username_element.suggestedValue().length());
764 was_password_autofilled_ = password_info->password_field.isAutofilled();
765 password_info->password_field.setSuggestedValue(password);
766 password_info->password_field.setAutofilled(true);
768 return true;
771 bool PasswordAutofillAgent::DidClearAutofillSelection(
772 const blink::WebNode& node) {
773 blink::WebInputElement username_element;
774 PasswordInfo* password_info;
775 if (!FindLoginInfo(node, &username_element, &password_info))
776 return false;
778 ClearPreview(&username_element, &password_info->password_field);
779 return true;
782 bool PasswordAutofillAgent::FindPasswordInfoForElement(
783 const blink::WebInputElement& element,
784 const blink::WebInputElement** username_element,
785 PasswordInfo** password_info) {
786 DCHECK(username_element && password_info);
787 if (!element.isPasswordField()) {
788 *username_element = &element;
789 } else {
790 PasswordToLoginMap::const_iterator password_iter =
791 password_to_username_.find(element);
792 if (password_iter == password_to_username_.end())
793 return false;
794 *username_element = &password_iter->second;
797 LoginToPasswordInfoMap::iterator iter =
798 login_to_password_info_.find(**username_element);
800 if (iter == login_to_password_info_.end())
801 return false;
803 *password_info = &iter->second;
804 return true;
807 bool PasswordAutofillAgent::ShowSuggestions(
808 const blink::WebInputElement& element,
809 bool show_all) {
810 const blink::WebInputElement* username_element;
811 PasswordInfo* password_info;
812 if (!FindPasswordInfoForElement(element, &username_element, &password_info))
813 return false;
815 // If autocomplete='off' is set on the form elements, no suggestion dialog
816 // should be shown. However, return |true| to indicate that this is a known
817 // password form and that the request to show suggestions has been handled (as
818 // a no-op).
819 if (!IsElementAutocompletable(element) ||
820 !IsElementAutocompletable(password_info->password_field))
821 return true;
823 bool username_is_available =
824 !username_element->isNull() && IsElementEditable(*username_element);
825 // If the element is a password field, a popup should only be shown if there
826 // is no username or the corresponding username element is not editable since
827 // it is only in that case that the username element does not have a
828 // suggestions popup.
829 if (element.isPasswordField() && username_is_available)
830 return true;
832 // Chrome should never show more than one account for a password element since
833 // this implies that the username element cannot be modified. Thus even if
834 // |show_all| is true, check if the element in question is a password element
835 // for the call to ShowSuggestionPopup.
836 return ShowSuggestionPopup(
837 password_info->fill_data,
838 username_element->isNull() ? element : *username_element,
839 show_all && !element.isPasswordField(), element.isPasswordField());
842 bool PasswordAutofillAgent::OriginCanAccessPasswordManager(
843 const blink::WebSecurityOrigin& origin) {
844 return origin.canAccessPasswordManager();
847 void PasswordAutofillAgent::OnDynamicFormsSeen() {
848 SendPasswordForms(false /* only_visible */);
851 void PasswordAutofillAgent::FirstUserGestureObserved() {
852 gatekeeper_.OnUserGesture();
855 void PasswordAutofillAgent::SendPasswordForms(bool only_visible) {
856 scoped_ptr<RendererSavePasswordProgressLogger> logger;
857 if (logging_state_active_) {
858 logger.reset(new RendererSavePasswordProgressLogger(this, routing_id()));
859 logger->LogMessage(Logger::STRING_SEND_PASSWORD_FORMS_METHOD);
860 logger->LogBoolean(Logger::STRING_ONLY_VISIBLE, only_visible);
863 blink::WebFrame* frame = render_frame()->GetWebFrame();
864 // Make sure that this security origin is allowed to use password manager.
865 blink::WebSecurityOrigin origin = frame->document().securityOrigin();
866 if (logger) {
867 logger->LogURL(Logger::STRING_SECURITY_ORIGIN,
868 GURL(origin.toString().utf8()));
870 if (!OriginCanAccessPasswordManager(origin)) {
871 if (logger) {
872 logger->LogMessage(Logger::STRING_SECURITY_ORIGIN_FAILURE);
874 return;
877 // Checks whether the webpage is a redirect page or an empty page.
878 if (IsWebpageEmpty(frame)) {
879 if (logger) {
880 logger->LogMessage(Logger::STRING_WEBPAGE_EMPTY);
882 return;
885 blink::WebVector<blink::WebFormElement> forms;
886 frame->document().forms(forms);
887 if (logger)
888 logger->LogNumber(Logger::STRING_NUMBER_OF_ALL_FORMS, forms.size());
890 std::vector<PasswordForm> password_forms;
891 for (size_t i = 0; i < forms.size(); ++i) {
892 const blink::WebFormElement& form = forms[i];
893 bool is_form_visible = IsWebNodeVisible(form);
894 if (logger) {
895 LogHTMLForm(logger.get(), Logger::STRING_FORM_FOUND_ON_PAGE, form);
896 logger->LogBoolean(Logger::STRING_FORM_IS_VISIBLE, is_form_visible);
899 // If requested, ignore non-rendered forms, e.g. those styled with
900 // display:none.
901 if (only_visible && !is_form_visible)
902 continue;
904 scoped_ptr<PasswordForm> password_form(CreatePasswordForm(form, nullptr));
905 if (password_form.get()) {
906 if (logger) {
907 logger->LogPasswordForm(Logger::STRING_FORM_IS_PASSWORD,
908 *password_form);
910 password_forms.push_back(*password_form);
914 if (password_forms.empty() && !only_visible) {
915 // We need to send the PasswordFormsRendered message regardless of whether
916 // there are any forms visible, as this is also the code path that triggers
917 // showing the infobar.
918 return;
921 if (only_visible) {
922 Send(new AutofillHostMsg_PasswordFormsRendered(routing_id(),
923 password_forms,
924 did_stop_loading_));
925 } else {
926 Send(new AutofillHostMsg_PasswordFormsParsed(routing_id(), password_forms));
930 bool PasswordAutofillAgent::OnMessageReceived(const IPC::Message& message) {
931 bool handled = true;
932 IPC_BEGIN_MESSAGE_MAP(PasswordAutofillAgent, message)
933 IPC_MESSAGE_HANDLER(AutofillMsg_FillPasswordForm, OnFillPasswordForm)
934 IPC_MESSAGE_HANDLER(AutofillMsg_SetLoggingState, OnSetLoggingState)
935 IPC_MESSAGE_UNHANDLED(handled = false)
936 IPC_END_MESSAGE_MAP()
937 return handled;
940 void PasswordAutofillAgent::DidFinishDocumentLoad() {
941 // The |frame| contents have been parsed, but not yet rendered. Let the
942 // PasswordManager know that forms are loaded, even though we can't yet tell
943 // whether they're visible.
944 SendPasswordForms(false);
947 void PasswordAutofillAgent::DidFinishLoad() {
948 // The |frame| contents have been rendered. Let the PasswordManager know
949 // which of the loaded frames are actually visible to the user. This also
950 // triggers the "Save password?" infobar if the user just submitted a password
951 // form.
952 SendPasswordForms(true);
955 void PasswordAutofillAgent::FrameWillClose() {
956 FrameClosing();
959 void PasswordAutofillAgent::DidCommitProvisionalLoad(bool is_new_navigation) {
960 blink::WebFrame* frame = render_frame()->GetWebFrame();
961 // TODO(dvadym): check if we need to check if it is main frame navigation
962 // http://crbug.com/443155
963 if (frame->parent())
964 return; // Not a top-level navigation.
966 content::DocumentState* document_state =
967 content::DocumentState::FromDataSource(frame->dataSource());
968 content::NavigationState* navigation_state =
969 document_state->navigation_state();
970 if (navigation_state->was_within_same_page() && provisionally_saved_form_) {
971 Send(new AutofillHostMsg_InPageNavigation(routing_id(),
972 *provisionally_saved_form_));
973 provisionally_saved_form_.reset();
977 void PasswordAutofillAgent::DidStartLoading() {
978 did_stop_loading_ = false;
979 if (usernames_usage_ != NOTHING_TO_AUTOFILL) {
980 UMA_HISTOGRAM_ENUMERATION("PasswordManager.OtherPossibleUsernamesUsage",
981 usernames_usage_, OTHER_POSSIBLE_USERNAMES_MAX);
982 usernames_usage_ = NOTHING_TO_AUTOFILL;
986 void PasswordAutofillAgent::DidStopLoading() {
987 did_stop_loading_ = true;
990 void PasswordAutofillAgent::FrameDetached() {
991 FrameClosing();
994 void PasswordAutofillAgent::WillSendSubmitEvent(
995 const blink::WebFormElement& form) {
996 // Forms submitted via XHR are not seen by WillSubmitForm if the default
997 // onsubmit handler is overridden. Such submission first gets detected in
998 // DidStartProvisionalLoad, which no longer knows about the particular form,
999 // and uses the candidate stored in |provisionally_saved_form_|.
1001 // User-typed password will get stored to |provisionally_saved_form_| in
1002 // TextDidChangeInTextField. Autofilled or JavaScript-copied passwords need to
1003 // be saved here.
1005 // Only non-empty passwords are saved here. Empty passwords were likely
1006 // cleared by some scripts (http://crbug.com/28910, http://crbug.com/391693).
1007 // Had the user cleared the password, |provisionally_saved_form_| would
1008 // already have been updated in TextDidChangeInTextField.
1009 ProvisionallySavePassword(form, RESTRICTION_NON_EMPTY_PASSWORD);
1012 void PasswordAutofillAgent::WillSubmitForm(const blink::WebFormElement& form) {
1013 scoped_ptr<RendererSavePasswordProgressLogger> logger;
1014 if (logging_state_active_) {
1015 logger.reset(new RendererSavePasswordProgressLogger(this, routing_id()));
1016 logger->LogMessage(Logger::STRING_WILL_SUBMIT_FORM_METHOD);
1017 LogHTMLForm(logger.get(), Logger::STRING_HTML_FORM_FOR_SUBMIT, form);
1020 scoped_ptr<PasswordForm> submitted_form = CreatePasswordForm(form, nullptr);
1022 // If there is a provisionally saved password, copy over the previous
1023 // password value so we get the user's typed password, not the value that
1024 // may have been transformed for submit.
1025 // TODO(gcasto): Do we need to have this action equality check? Is it trying
1026 // to prevent accidentally copying over passwords from a different form?
1027 if (submitted_form) {
1028 if (logger) {
1029 logger->LogPasswordForm(Logger::STRING_CREATED_PASSWORD_FORM,
1030 *submitted_form);
1032 if (provisionally_saved_form_ &&
1033 submitted_form->action == provisionally_saved_form_->action) {
1034 if (logger)
1035 logger->LogMessage(Logger::STRING_SUBMITTED_PASSWORD_REPLACED);
1036 submitted_form->password_value =
1037 provisionally_saved_form_->password_value;
1038 submitted_form->new_password_value =
1039 provisionally_saved_form_->new_password_value;
1040 submitted_form->username_value =
1041 provisionally_saved_form_->username_value;
1044 // Some observers depend on sending this information now instead of when
1045 // the frame starts loading. If there are redirects that cause a new
1046 // RenderView to be instantiated (such as redirects to the WebStore)
1047 // we will never get to finish the load.
1048 Send(new AutofillHostMsg_PasswordFormSubmitted(routing_id(),
1049 *submitted_form));
1050 provisionally_saved_form_.reset();
1051 } else if (logger) {
1052 logger->LogMessage(Logger::STRING_FORM_IS_NOT_PASSWORD);
1056 void PasswordAutofillAgent::LegacyDidStartProvisionalLoad(
1057 blink::WebLocalFrame* navigated_frame) {
1058 scoped_ptr<RendererSavePasswordProgressLogger> logger;
1059 if (logging_state_active_) {
1060 logger.reset(new RendererSavePasswordProgressLogger(this, routing_id()));
1061 logger->LogMessage(Logger::STRING_DID_START_PROVISIONAL_LOAD_METHOD);
1064 if (navigated_frame->parent()) {
1065 if (logger)
1066 logger->LogMessage(Logger::STRING_FRAME_NOT_MAIN_FRAME);
1067 return;
1070 // Bug fix for crbug.com/368690. isProcessingUserGesture() is false when
1071 // the user is performing actions outside the page (e.g. typed url,
1072 // history navigation). We don't want to trigger saving in these cases.
1073 content::DocumentState* document_state =
1074 content::DocumentState::FromDataSource(
1075 navigated_frame->provisionalDataSource());
1076 content::NavigationState* navigation_state =
1077 document_state->navigation_state();
1078 if (ui::PageTransitionIsWebTriggerable(navigation_state->transition_type()) &&
1079 !blink::WebUserGestureIndicator::isProcessingUserGesture()) {
1080 // If onsubmit has been called, try and save that form.
1081 if (provisionally_saved_form_) {
1082 if (logger) {
1083 logger->LogPasswordForm(
1084 Logger::STRING_PROVISIONALLY_SAVED_FORM_FOR_FRAME,
1085 *provisionally_saved_form_);
1087 Send(new AutofillHostMsg_PasswordFormSubmitted(
1088 routing_id(), *provisionally_saved_form_));
1089 provisionally_saved_form_.reset();
1090 } else {
1091 // Loop through the forms on the page looking for one that has been
1092 // filled out. If one exists, try and save the credentials.
1093 blink::WebVector<blink::WebFormElement> forms;
1094 render_frame()->GetWebFrame()->document().forms(forms);
1096 bool password_forms_found = false;
1097 for (size_t i = 0; i < forms.size(); ++i) {
1098 blink::WebFormElement form_element = forms[i];
1099 if (logger) {
1100 LogHTMLForm(logger.get(), Logger::STRING_FORM_FOUND_ON_PAGE,
1101 form_element);
1103 scoped_ptr<PasswordForm> password_form(
1104 CreatePasswordForm(form_element, &user_modified_elements_));
1105 if (password_form.get() && !password_form->username_value.empty() &&
1106 FormContainsNonDefaultPasswordValue(*password_form, form_element)) {
1107 password_forms_found = true;
1108 if (logger) {
1109 logger->LogPasswordForm(Logger::STRING_PASSWORD_FORM_FOUND_ON_PAGE,
1110 *password_form);
1112 Send(new AutofillHostMsg_PasswordFormSubmitted(routing_id(),
1113 *password_form));
1116 if (!password_forms_found && logger)
1117 logger->LogMessage(Logger::STRING_PASSWORD_FORM_NOT_FOUND_ON_PAGE);
1121 // This is a new navigation, so require a new user gesture before filling in
1122 // passwords.
1123 gatekeeper_.Reset();
1126 void PasswordAutofillAgent::OnFillPasswordForm(
1127 int key,
1128 const PasswordFormFillData& form_data) {
1129 if (usernames_usage_ == NOTHING_TO_AUTOFILL) {
1130 if (form_data.other_possible_usernames.size())
1131 usernames_usage_ = OTHER_POSSIBLE_USERNAMES_PRESENT;
1132 else if (usernames_usage_ == NOTHING_TO_AUTOFILL)
1133 usernames_usage_ = OTHER_POSSIBLE_USERNAMES_ABSENT;
1136 FormElementsList forms;
1137 // We own the FormElements* in forms.
1138 FindFormElements(render_frame(), form_data, &forms);
1139 FormElementsList::iterator iter;
1140 for (iter = forms.begin(); iter != forms.end(); ++iter) {
1141 scoped_ptr<FormElements> form_elements(*iter);
1143 // Attach autocomplete listener to enable selecting alternate logins.
1144 blink::WebInputElement username_element, password_element;
1146 // Check whether the password form has a username input field.
1147 bool form_contains_username_field = FillDataContainsUsername(form_data);
1148 if (form_contains_username_field) {
1149 username_element =
1150 form_elements->input_elements[form_data.username_field.name];
1153 // No password field, bail out.
1154 if (form_data.password_field.name.empty())
1155 break;
1157 // We might have already filled this form if there are two <form> elements
1158 // with identical markup.
1159 if (login_to_password_info_.find(username_element) !=
1160 login_to_password_info_.end())
1161 continue;
1163 // Get pointer to password element. (We currently only support single
1164 // password forms).
1165 password_element =
1166 form_elements->input_elements[form_data.password_field.name];
1168 // If wait_for_username is true, we don't want to initially fill the form
1169 // until the user types in a valid username.
1170 if (!form_data.wait_for_username &&
1171 FillFormOnPasswordReceived(
1172 form_data,
1173 username_element,
1174 password_element,
1175 base::Bind(&PasswordValueGatekeeper::RegisterElement,
1176 base::Unretained(&gatekeeper_)))) {
1177 usernames_usage_ = OTHER_POSSIBLE_USERNAME_SELECTED;
1180 PasswordInfo password_info;
1181 password_info.fill_data = form_data;
1182 password_info.password_field = password_element;
1183 login_to_password_info_[username_element] = password_info;
1184 password_to_username_[password_element] = username_element;
1185 login_to_password_info_key_[username_element] = key;
1189 void PasswordAutofillAgent::OnSetLoggingState(bool active) {
1190 logging_state_active_ = active;
1193 ////////////////////////////////////////////////////////////////////////////////
1194 // PasswordAutofillAgent, private:
1196 PasswordAutofillAgent::PasswordInfo::PasswordInfo()
1197 : backspace_pressed_last(false), password_was_edited_last(false) {
1200 bool PasswordAutofillAgent::ShowSuggestionPopup(
1201 const PasswordFormFillData& fill_data,
1202 const blink::WebInputElement& user_input,
1203 bool show_all,
1204 bool show_on_password_field) {
1205 DCHECK(!user_input.isNull());
1206 blink::WebFrame* frame = user_input.document().frame();
1207 if (!frame)
1208 return false;
1210 blink::WebView* webview = frame->view();
1211 if (!webview)
1212 return false;
1214 FormData form;
1215 FormFieldData field;
1216 FindFormAndFieldForFormControlElement(
1217 user_input, &form, &field, REQUIRE_NONE);
1219 blink::WebInputElement selected_element = user_input;
1220 if (show_on_password_field && !selected_element.isPasswordField()) {
1221 LoginToPasswordInfoMap::const_iterator iter =
1222 login_to_password_info_.find(user_input);
1223 DCHECK(iter != login_to_password_info_.end());
1224 selected_element = iter->second.password_field;
1226 gfx::Rect bounding_box(selected_element.boundsInViewportSpace());
1228 blink::WebInputElement username;
1229 if (!show_on_password_field || !user_input.isPasswordField()) {
1230 username = user_input;
1232 LoginToPasswordInfoKeyMap::const_iterator key_it =
1233 login_to_password_info_key_.find(username);
1234 DCHECK(key_it != login_to_password_info_key_.end());
1236 float scale =
1237 render_frame()->GetRenderView()->GetWebView()->pageScaleFactor();
1238 gfx::RectF bounding_box_scaled(bounding_box.x() * scale,
1239 bounding_box.y() * scale,
1240 bounding_box.width() * scale,
1241 bounding_box.height() * scale);
1242 int options = 0;
1243 if (show_all)
1244 options |= SHOW_ALL;
1245 if (show_on_password_field)
1246 options |= IS_PASSWORD_FIELD;
1247 base::string16 username_string(
1248 username.isNull() ? base::string16()
1249 : static_cast<base::string16>(user_input.value()));
1250 Send(new AutofillHostMsg_ShowPasswordSuggestions(
1251 routing_id(), key_it->second, field.text_direction, username_string,
1252 options, bounding_box_scaled));
1254 bool suggestions_present = false;
1255 if (GetSuggestionsStats(fill_data, username_string, show_all,
1256 &suggestions_present)) {
1257 usernames_usage_ = OTHER_POSSIBLE_USERNAME_SHOWN;
1259 return suggestions_present;
1262 void PasswordAutofillAgent::PerformInlineAutocomplete(
1263 const blink::WebInputElement& username_input,
1264 const blink::WebInputElement& password_input,
1265 const PasswordFormFillData& fill_data) {
1266 DCHECK(!fill_data.wait_for_username);
1268 // We need non-const versions of the username and password inputs.
1269 blink::WebInputElement username = username_input;
1270 blink::WebInputElement password = password_input;
1272 // Don't inline autocomplete if the caret is not at the end.
1273 // TODO(jcivelli): is there a better way to test the caret location?
1274 if (username.selectionStart() != username.selectionEnd() ||
1275 username.selectionEnd() != static_cast<int>(username.value().length())) {
1276 return;
1279 // Show the popup with the list of available usernames.
1280 ShowSuggestionPopup(fill_data, username, false, false);
1282 #if !defined(OS_ANDROID)
1283 // Fill the user and password field with the most relevant match. Android
1284 // only fills in the fields after the user clicks on the suggestion popup.
1285 if (FillUserNameAndPassword(
1286 &username,
1287 &password,
1288 fill_data,
1289 false /* exact_username_match */,
1290 true /* set selection */,
1291 base::Bind(&PasswordValueGatekeeper::RegisterElement,
1292 base::Unretained(&gatekeeper_)))) {
1293 usernames_usage_ = OTHER_POSSIBLE_USERNAME_SELECTED;
1295 #endif
1298 void PasswordAutofillAgent::FrameClosing() {
1299 for (auto const& iter : login_to_password_info_) {
1300 login_to_password_info_key_.erase(iter.first);
1301 password_to_username_.erase(iter.second.password_field);
1303 login_to_password_info_.clear();
1304 provisionally_saved_form_.reset();
1305 user_modified_elements_.clear();
1308 bool PasswordAutofillAgent::FindLoginInfo(const blink::WebNode& node,
1309 blink::WebInputElement* found_input,
1310 PasswordInfo** found_password) {
1311 if (!node.isElementNode())
1312 return false;
1314 blink::WebElement element = node.toConst<blink::WebElement>();
1315 if (!element.hasHTMLTagName("input"))
1316 return false;
1318 *found_input = element.to<blink::WebInputElement>();
1319 const blink::WebInputElement* username_element; // ignored
1320 return FindPasswordInfoForElement(*found_input, &username_element,
1321 found_password);
1324 void PasswordAutofillAgent::ClearPreview(
1325 blink::WebInputElement* username,
1326 blink::WebInputElement* password) {
1327 if (!username->suggestedValue().isEmpty()) {
1328 username->setSuggestedValue(blink::WebString());
1329 username->setAutofilled(was_username_autofilled_);
1330 username->setSelectionRange(username_selection_start_,
1331 username->value().length());
1333 if (!password->suggestedValue().isEmpty()) {
1334 password->setSuggestedValue(blink::WebString());
1335 password->setAutofilled(was_password_autofilled_);
1339 void PasswordAutofillAgent::ProvisionallySavePassword(
1340 const blink::WebFormElement& form,
1341 ProvisionallySaveRestriction restriction) {
1342 scoped_ptr<PasswordForm> password_form(
1343 CreatePasswordForm(form, &user_modified_elements_));
1344 if (!password_form || (restriction == RESTRICTION_NON_EMPTY_PASSWORD &&
1345 password_form->password_value.empty() &&
1346 password_form->new_password_value.empty())) {
1347 return;
1349 provisionally_saved_form_ = password_form.Pass();
1352 // LegacyPasswordAutofillAgent -------------------------------------------------
1354 PasswordAutofillAgent::LegacyPasswordAutofillAgent::LegacyPasswordAutofillAgent(
1355 content::RenderView* render_view,
1356 PasswordAutofillAgent* agent)
1357 : content::RenderViewObserver(render_view), agent_(agent) {
1360 PasswordAutofillAgent::LegacyPasswordAutofillAgent::
1361 ~LegacyPasswordAutofillAgent() {
1364 void PasswordAutofillAgent::LegacyPasswordAutofillAgent::OnDestruct() {
1365 // No op. Do not delete |this|.
1368 void PasswordAutofillAgent::LegacyPasswordAutofillAgent::DidStartLoading() {
1369 agent_->DidStartLoading();
1372 void PasswordAutofillAgent::LegacyPasswordAutofillAgent::DidStopLoading() {
1373 agent_->DidStopLoading();
1376 void PasswordAutofillAgent::LegacyPasswordAutofillAgent::
1377 DidStartProvisionalLoad(blink::WebLocalFrame* navigated_frame) {
1378 agent_->LegacyDidStartProvisionalLoad(navigated_frame);
1381 } // namespace autofill