Chromium Code Reviews| Index: components/autofill/content/renderer/password_form_conversion_utils.cc |
| diff --git a/components/autofill/content/renderer/password_form_conversion_utils.cc b/components/autofill/content/renderer/password_form_conversion_utils.cc |
| index dd97e9aaba7621fba147504501066f4e459358d6..17b37aae2d218b233528ea29418f20d61ebe8798 100644 |
| --- a/components/autofill/content/renderer/password_form_conversion_utils.cc |
| +++ b/components/autofill/content/renderer/password_form_conversion_utils.cc |
| @@ -6,6 +6,7 @@ |
| #include "base/strings/string_util.h" |
| #include "components/autofill/content/renderer/form_autofill_util.h" |
| +#include "components/autofill/core/common/form_data_predictions.h" |
| #include "components/autofill/core/common/password_form.h" |
| #include "third_party/WebKit/public/platform/WebString.h" |
| #include "third_party/WebKit/public/web/WebDocument.h" |
| @@ -107,6 +108,57 @@ bool LocateSpecificPasswords(std::vector<WebInputElement> passwords, |
| return true; |
| } |
| +void FindPredictedUsernameElement( |
| + const WebFormElement& form, |
| + const std::vector<autofill::FormDataPredictions>* form_predictions, |
| + WebVector<WebFormControlElement>& control_elements, |
|
vabr (Chromium)
2015/03/23 16:48:23
No non-const references in arguments. Please use p
dvadym
2015/03/23 17:20:44
Done.
|
| + WebInputElement& predicted_username_element) { |
| + if (!form_predictions) |
| + return; |
| + FormData form_data; |
| + if (!WebFormElementToFormData( |
| + form, WebFormControlElement(), REQUIRE_AUTOCOMPLETE, |
| + static_cast<ExtractMask>(EXTRACT_VALUE | EXTRACT_OPTION_TEXT | |
| + EXTRACT_OPTIONS), |
| + &form_data, NULL)) |
|
vabr (Chromium)
2015/03/23 16:48:23
nit: nullptr
dvadym
2015/03/23 17:20:44
Done.
|
| + return; |
| + // Prediction forms are not user submitted, but |form| can be user submitted. |
| + // We don't care about this flag for finding predictions, so set it to false. |
| + form_data.user_submitted = false; |
| + const autofill::FormDataPredictions* prediction = nullptr; |
| + for (auto& form_prediction : *form_predictions) { |
| + if (form_prediction.data.SameFormAs(form_data)) { |
| + prediction = &form_prediction; |
| + break; |
| + } |
| + } |
| + if (!prediction) |
| + return; |
| + std::vector<blink::WebFormControlElement> autofillable_elements = |
| + ExtractAutofillableElementsFromSet(control_elements, REQUIRE_NONE); |
| + if (autofillable_elements.size() != prediction->fields.size()) { |
| + // Keep things simple. Don't use predictions for forms that were modified |
| + // between page load and the server's response to our query. |
| + return; |
| + } |
| + |
| + for (size_t i = 0; i < autofillable_elements.size(); ++i) { |
| + if (prediction->fields[i].server_type != "USERNAME" && |
| + prediction->fields[i].server_type != "USERNAME_AND_EMAIL_ADDRESS") |
| + continue; |
| + WebFormControlElement& element = autofillable_elements[i]; |
| + |
| + if (base::string16(element.nameForAutofill()) != |
| + prediction->data.fields[i].name) { |
| + // Keep things simple. Don't show predictions for elements whose names |
| + // were modified between page load and the server's response to our query. |
| + continue; |
| + } |
| + predicted_username_element = *toWebInputElement(&element); |
| + break; |
| + } |
| +} |
| + |
| // Get information about a login form encapsulated in a PasswordForm struct. |
| // If an element of |form| has an entry in |nonscript_modified_values|, the |
| // associated string is used instead of the element's value to create |
| @@ -115,7 +167,8 @@ void GetPasswordForm( |
| const WebFormElement& form, |
| PasswordForm* password_form, |
| const std::map<const blink::WebInputElement, blink::WebString>* |
| - nonscript_modified_values) { |
| + nonscript_modified_values, |
| + const std::vector<autofill::FormDataPredictions>* form_predictions) { |
| WebInputElement latest_input_element; |
| WebInputElement username_element; |
| password_form->username_marked_by_site = false; |
| @@ -195,6 +248,14 @@ void GetPasswordForm( |
| } |
| } |
| + WebInputElement predicted_username_element; |
| + FindPredictedUsernameElement(form, form_predictions, control_elements, |
| + predicted_username_element); |
| + if (!predicted_username_element.isNull()) { |
| + username_element = predicted_username_element; |
| + password_form->parsed_using_autofill_predictions = true; |
| + } |
| + |
| if (!username_element.isNull()) { |
| password_form->username_element = username_element.nameForAutofill(); |
| base::string16 username_value = username_element.value(); |
| @@ -273,12 +334,14 @@ void GetPasswordForm( |
| scoped_ptr<PasswordForm> CreatePasswordForm( |
| const WebFormElement& web_form, |
| const std::map<const blink::WebInputElement, blink::WebString>* |
| - nonscript_modified_values) { |
| + nonscript_modified_values, |
| + const std::vector<autofill::FormDataPredictions>* form_predictions) { |
| if (web_form.isNull()) |
| return scoped_ptr<PasswordForm>(); |
| scoped_ptr<PasswordForm> password_form(new PasswordForm()); |
| - GetPasswordForm(web_form, password_form.get(), nonscript_modified_values); |
| + GetPasswordForm(web_form, password_form.get(), nonscript_modified_values, |
| + form_predictions); |
| if (!password_form->action.is_valid()) |
| return scoped_ptr<PasswordForm>(); |