From 4c0d53583504626d4da0b0b0fb7c87e4ffca3fd5 Mon Sep 17 00:00:00 2001 From: dvadym Date: Thu, 13 Aug 2015 08:25:52 -0700 Subject: [PATCH] Fill only the password field on "no username" credentials in PasswordManager when there is no other credentials stored. BUG=359315 Review URL: https://codereview.chromium.org/1288343002 Cr-Commit-Position: refs/heads/master@{#343199} --- .../password_autofill_agent_browsertest.cc | 22 ++++++++++++++++++++++ .../content/renderer/password_autofill_agent.cc | 18 +++++++++++------- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/chrome/renderer/autofill/password_autofill_agent_browsertest.cc b/chrome/renderer/autofill/password_autofill_agent_browsertest.cc index 27554582508c..f3743717e9bf 100644 --- a/chrome/renderer/autofill/password_autofill_agent_browsertest.cc +++ b/chrome/renderer/autofill/password_autofill_agent_browsertest.cc @@ -1948,4 +1948,26 @@ TEST_F(PasswordAutofillAgentTest, IgnoreNotPasswordFields) { ASSERT_FALSE(message); } +// Tests that only the password field is autocompleted when the browser sends +// back data with only one credentials and empty username. +TEST_F(PasswordAutofillAgentTest, NotAutofillNoUsername) { + fill_data_.username_field.value.clear(); + fill_data_.additional_logins.clear(); + SimulateOnFillPasswordForm(fill_data_); + + CheckTextFieldsState("", false, kAlicePassword, true); +} + +// Tests that both the username and the password fields are autocompleted +// despite the empty username, when the browser sends back data more than one +// credential. +TEST_F(PasswordAutofillAgentTest, + AutofillNoUsernameWhenOtherCredentialsStored) { + fill_data_.username_field.value.clear(); + ASSERT_FALSE(fill_data_.additional_logins.empty()); + SimulateOnFillPasswordForm(fill_data_); + + CheckTextFieldsState("", true, kAlicePassword, true); +} + } // namespace autofill diff --git a/components/autofill/content/renderer/password_autofill_agent.cc b/components/autofill/content/renderer/password_autofill_agent.cc index 1c836a15c8d4..3c48077a45dc 100644 --- a/components/autofill/content/renderer/password_autofill_agent.cc +++ b/components/autofill/content/renderer/password_autofill_agent.cc @@ -75,8 +75,10 @@ struct FormElements { typedef std::vector FormElementsList; -bool FillDataContainsUsername(const PasswordFormFillData& fill_data) { - return !fill_data.username_field.name.empty(); +bool FillDataContainsFillableUsername(const PasswordFormFillData& fill_data) { + return !fill_data.username_field.name.empty() && + (!fill_data.additional_logins.empty() || + !fill_data.username_field.value.empty()); } // Utility function to find the unique entry of the |form_element| for the @@ -180,7 +182,7 @@ bool FindFormInputElements(blink::WebFormElement* form_element, const PasswordFormFillData& data, FormElements* result) { return FindFormInputElement(form_element, data.password_field, result) && - (!FillDataContainsUsername(data) || + (!FillDataContainsFillableUsername(data) || FindFormInputElement(form_element, data.username_field, result)); } @@ -466,7 +468,8 @@ bool FillFormOnPasswordReceived( if (!IsElementAutocompletable(password_element)) return false; - bool form_contains_username_field = FillDataContainsUsername(fill_data); + bool form_contains_fillable_username_field = + FillDataContainsFillableUsername(fill_data); // If the form contains an autocompletable username field, try to set the // username to the preferred name, but only if: // (a) The fill-on-account-select flag is not set, and @@ -483,7 +486,7 @@ bool FillFormOnPasswordReceived( // in the "no highlighting" group. // // In all other cases, do nothing. - bool form_has_fillable_username = form_contains_username_field && + bool form_has_fillable_username = form_contains_fillable_username_field && IsElementAutocompletable(username_element); if (ShouldFillOnAccountSelect()) { @@ -1173,8 +1176,9 @@ void PasswordAutofillAgent::OnFillPasswordForm( blink::WebInputElement username_element, password_element; // Check whether the password form has a username input field. - bool form_contains_username_field = FillDataContainsUsername(form_data); - if (form_contains_username_field) { + bool form_contains_fillable_username_field = + FillDataContainsFillableUsername(form_data); + if (form_contains_fillable_username_field) { username_element = form_elements->input_elements[form_data.username_field.name]; } -- 2.11.4.GIT