Index: components/autofill/content/renderer/password_autofill_agent.cc |
diff --git a/components/autofill/content/renderer/password_autofill_agent.cc b/components/autofill/content/renderer/password_autofill_agent.cc |
index f9172c5e7a47b7710bcda0bf4b45d7e2ce06c845..7ba3d86065daa81c7d5788c4133fe5ab04fe038a 100644 |
--- a/components/autofill/content/renderer/password_autofill_agent.cc |
+++ b/components/autofill/content/renderer/password_autofill_agent.cc |
@@ -84,6 +84,30 @@ bool IsNamedElementVisible( |
return false; |
} |
+// Returns true if password form has username and password fields with either |
+// same or no name and id attributes supplied. |
+bool PasswordFormWithAmbiguousOrNoNameAndIdAttribute( |
+ const PasswordFormFillData& fill_data) { |
+ // TODO(pritam.nikam): Extend below logic once patch lands for storing the |
+ // password forms having name or id attributes missing from the fields. |
+ // http://crbug.com/497630 |
dvadym
2015/09/07 15:26:00
Please rebase on the top of save patch and impleme
Pritam Nikam
2015/09/08 15:56:19
Done.
|
+ return fill_data.username_field.name == fill_data.password_field.name; |
+} |
+ |
+bool IsPasswordField(const FormFieldData& field) { |
+ return (field.form_control_type == "password"); |
+} |
+ |
+// Returns the |field|'s autofillable name. If no name or id attribute is |
+// specified returns a dummy name. |
+base::string16 FieldName(const FormFieldData& field, |
+ bool ambiguous_or_no_name_and_id_attribute) { |
+ return ambiguous_or_no_name_and_id_attribute |
dvadym
2015/09/07 15:26:00
Is the case of no-username field considered here?
Pritam Nikam
2015/09/08 15:56:19
For the case of no-username field, |ambiguous_or_n
dvadym
2015/09/11 14:15:34
Acknowledged
|
+ ? IsPasswordField(field) ? base::ASCIIToUTF16("anonymous_password") |
+ : base::ASCIIToUTF16("anonymous_username") |
+ : field.name; |
+} |
+ |
// Utility function to find the unique entry of |control_elements| for the |
// specified input |field|. On successful find, adds it to |result| and returns |
// |true|. Otherwise clears the references from each |HTMLInputElement| from |
@@ -91,16 +115,20 @@ bool IsNamedElementVisible( |
bool FindFormInputElement( |
const std::vector<blink::WebFormControlElement>& control_elements, |
const FormFieldData& field, |
+ bool ambiguous_or_no_name_and_id_attribute, |
FormInputElementMap* result) { |
// Match the first input element, if any. |
- // If more than one match is made, then we have ambiguity (due to misuse |
- // of "name" attribute) so is it considered not found. |
bool found_input = false; |
+ base::string16 field_name = |
+ FieldName(field, ambiguous_or_no_name_and_id_attribute); |
for (size_t i = 0; i < control_elements.size(); ++i) { |
- if (control_elements[i].nameForAutofill() != field.name) |
- continue; |
+ DCHECK(control_elements[i].hasHTMLTagName("input")); |
- if (!control_elements[i].hasHTMLTagName("input")) |
+ // Only fill saved passwords into password fields and usernames into text |
+ // fields. |
+ const blink::WebInputElement input_element = |
+ control_elements[i].toConst<blink::WebInputElement>(); |
+ if (input_element.isPasswordField() != IsPasswordField(field)) |
continue; |
// Check for a non-unique match. |
@@ -109,15 +137,7 @@ bool FindFormInputElement( |
break; |
} |
- // Only fill saved passwords into password fields and usernames into |
- // text fields. |
- const blink::WebInputElement input_element = |
- control_elements[i].toConst<blink::WebInputElement>(); |
- if (input_element.isPasswordField() != |
- (field.form_control_type == "password")) |
- continue; |
- |
- (*result)[field.name] = input_element; |
+ (*result)[field_name] = input_element; |
found_input = true; |
} |
@@ -179,15 +199,20 @@ bool ShouldHighlightFields() { |
bool FindFormInputElements( |
const std::vector<blink::WebFormControlElement>& control_elements, |
const PasswordFormFillData& data, |
+ bool ambiguous_or_no_name_and_id_attribute, |
FormInputElementMap* result) { |
- return FindFormInputElement(control_elements, data.password_field, result) && |
- (!FillDataContainsFillableUsername(data) || |
- FindFormInputElement(control_elements, data.username_field, result)); |
+ return FindFormInputElement(control_elements, data.password_field, |
+ ambiguous_or_no_name_and_id_attribute, result) && |
+ (!(ambiguous_or_no_name_and_id_attribute || |
+ FillDataContainsFillableUsername(data)) || |
+ FindFormInputElement(control_elements, data.username_field, |
+ ambiguous_or_no_name_and_id_attribute, result)); |
} |
// Helper to locate form elements identified by |data|. |
void FindFormElements(content::RenderFrame* render_frame, |
const PasswordFormFillData& data, |
+ bool ambiguous_or_no_name_and_id_attribute, |
FormElementsList* results) { |
DCHECK(results); |
@@ -211,7 +236,8 @@ void FindFormElements(content::RenderFrame* render_frame, |
std::vector<blink::WebFormControlElement> control_elements = |
ExtractAutofillableElementsInForm(fe); |
FormInputElementMap cur_map; |
- if (FindFormInputElements(control_elements, data, &cur_map)) |
+ if (FindFormInputElements(control_elements, data, |
+ ambiguous_or_no_name_and_id_attribute, &cur_map)) |
results->push_back(cur_map); |
} |
// If the element to be filled are not in a <form> element, the "action" and |
@@ -222,7 +248,9 @@ void FindFormElements(content::RenderFrame* render_frame, |
std::vector<blink::WebFormControlElement> control_elements = |
GetUnownedAutofillableFormFieldElements(doc.all(), nullptr); |
FormInputElementMap unowned_elements_map; |
- if (FindFormInputElements(control_elements, data, &unowned_elements_map)) |
+ if (FindFormInputElements(control_elements, data, |
+ ambiguous_or_no_name_and_id_attribute, |
+ &unowned_elements_map)) |
results->push_back(unowned_elements_map); |
} |
@@ -451,8 +479,9 @@ bool FillFormOnPasswordReceived( |
if (!IsElementAutocompletable(password_element)) |
return false; |
- bool form_contains_fillable_username_field = |
- FillDataContainsFillableUsername(fill_data); |
+ base::string16 username_field_name = |
+ FieldName(fill_data.username_field, |
+ PasswordFormWithAmbiguousOrNoNameAndIdAttribute(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 |
@@ -469,7 +498,7 @@ bool FillFormOnPasswordReceived( |
// in the "no highlighting" group. |
// |
// In all other cases, do nothing. |
- bool form_has_fillable_username = form_contains_fillable_username_field && |
+ bool form_has_fillable_username = !username_field_name.empty() && |
IsElementAutocompletable(username_element); |
if (ShouldFillOnAccountSelect()) { |
@@ -1203,24 +1232,28 @@ void PasswordAutofillAgent::LegacyDidStartProvisionalLoad( |
void PasswordAutofillAgent::OnFillPasswordForm( |
int key, |
const PasswordFormFillData& form_data) { |
- |
+ bool ambiguous_or_no_name_and_id_attribute = |
+ PasswordFormWithAmbiguousOrNoNameAndIdAttribute(form_data); |
FormElementsList forms; |
- FindFormElements(render_frame(), form_data, &forms); |
+ FindFormElements(render_frame(), form_data, |
+ ambiguous_or_no_name_and_id_attribute, &forms); |
FormElementsList::iterator iter; |
for (iter = forms.begin(); iter != forms.end(); ++iter) { |
// Attach autocomplete listener to enable selecting alternate logins. |
blink::WebInputElement username_element, password_element; |
+ base::string16 username_field_name = FieldName( |
+ form_data.username_field, ambiguous_or_no_name_and_id_attribute); |
+ base::string16 password_field_name = FieldName( |
+ form_data.password_field, ambiguous_or_no_name_and_id_attribute); |
+ |
// Check whether the password form has a username input field. |
- bool form_contains_fillable_username_field = |
- FillDataContainsFillableUsername(form_data); |
- if (form_contains_fillable_username_field) { |
- username_element = |
- (*iter)[form_data.username_field.name]; |
+ if (!username_field_name.empty()) { |
+ username_element = (*iter)[username_field_name]; |
} |
// No password field, bail out. |
- if (form_data.password_field.name.empty()) |
+ if (password_field_name.empty()) |
break; |
// We might have already filled this form if there are two <form> elements |
@@ -1231,7 +1264,7 @@ void PasswordAutofillAgent::OnFillPasswordForm( |
// Get pointer to password element. (We currently only support single |
// password forms). |
- password_element = (*iter)[form_data.password_field.name]; |
+ password_element = (*iter)[password_field_name]; |
// If wait_for_username is true, we don't want to initially fill the form |
// until the user types in a valid username. |