Commit e55b2920 authored by Vaclav Brozek's avatar Vaclav Brozek Committed by Commit Bot

Improve computing autcomplete attributes in utils.

Autocomplete attributes are string properties of DOM elements. For
parsing password forms, a few special values are important.

This CL adds a few functions to password_form_conversion_utils to
compute an enum representation of the autocomplete attributes, and a
simple cache to keep those computed values to minimize string
comparisons.

Bug: 833838
Change-Id: I6c386895be9af54ae1e31c715795a26b29eb0e5c
Reviewed-on: https://chromium-review.googlesource.com/1027881Reviewed-by: default avatarRoger McFarlane <rogerm@chromium.org>
Commit-Queue: Vaclav Brozek <vabr@chromium.org>
Cr-Commit-Position: refs/heads/master@{#561441}
parent 1323de09
...@@ -167,10 +167,12 @@ bool HasPasswordWithAutocompleteAttribute( ...@@ -167,10 +167,12 @@ bool HasPasswordWithAutocompleteAttribute(
const blink::WebInputElement input_element = const blink::WebInputElement input_element =
control_element.ToConst<blink::WebInputElement>(); control_element.ToConst<blink::WebInputElement>();
const AutocompleteFlag flag = AutocompleteFlagForElement(input_element);
if (input_element.IsPasswordFieldForAutofill() && if (input_element.IsPasswordFieldForAutofill() &&
(HasAutocompleteAttributeValue(input_element, "current-password") || (flag == AutocompleteFlag::CURRENT_PASSWORD ||
HasAutocompleteAttributeValue(input_element, "new-password"))) flag == AutocompleteFlag::NEW_PASSWORD)) {
return true; return true;
}
} }
return false; return false;
...@@ -232,7 +234,8 @@ bool FindFormInputElement( ...@@ -232,7 +234,8 @@ bool FindFormInputElement(
// set. Also make sure we avoid keeping password fields having // set. Also make sure we avoid keeping password fields having
// |autocomplete='new-password'| attribute set. // |autocomplete='new-password'| attribute set.
if (ambiguous_and_multiple_password_fields_with_autocomplete && if (ambiguous_and_multiple_password_fields_with_autocomplete &&
!HasAutocompleteAttributeValue(input_element, "current-password")) { AutocompleteFlagForElement(input_element) !=
AutocompleteFlag::CURRENT_PASSWORD) {
continue; continue;
} }
...@@ -591,7 +594,7 @@ bool ShouldShowStandaloneManuallFallback(const blink::WebInputElement& element, ...@@ -591,7 +594,7 @@ bool ShouldShowStandaloneManuallFallback(const blink::WebInputElement& element,
return ( return (
element.IsPasswordFieldForAutofill() && element.IsPasswordFieldForAutofill() &&
!IsCreditCardVerificationPasswordField(element) && !IsCreditCardVerificationPasswordField(element) &&
!HasCreditCardAutocompleteAttributes(element) && AutocompleteFlagForElement(element) != AutocompleteFlag::CREDIT_CARD &&
!base::StartsWith(url.scheme(), "chrome", base::CompareCase::SENSITIVE) && !base::StartsWith(url.scheme(), "chrome", base::CompareCase::SENSITIVE) &&
!url.SchemeIs(url::kAboutScheme) && !url.SchemeIs(url::kAboutScheme) &&
base::FeatureList::IsEnabled( base::FeatureList::IsEnabled(
...@@ -922,7 +925,7 @@ bool PasswordAutofillAgent::IsUsernameOrPasswordField( ...@@ -922,7 +925,7 @@ bool PasswordAutofillAgent::IsUsernameOrPasswordField(
return true; return true;
// If a field declares itself a username input, show the warning. // If a field declares itself a username input, show the warning.
if (HasAutocompleteAttributeValue(element, "username")) if (AutocompleteFlagForElement(element) == AutocompleteFlag::USERNAME)
return true; return true;
// Otherwise, analyze the form and return true if this input element seems // Otherwise, analyze the form and return true if this input element seems
......
...@@ -42,6 +42,20 @@ enum UsernameDetectionMethod { ...@@ -42,6 +42,20 @@ enum UsernameDetectionMethod {
USERNAME_DETECTION_METHOD_COUNT USERNAME_DETECTION_METHOD_COUNT
}; };
// The susbset of autocomplete flags related to passwords.
enum class AutocompleteFlag {
NONE,
USERNAME,
CURRENT_PASSWORD,
NEW_PASSWORD,
// Represents the whole family of cc-* flags.
CREDIT_CARD
};
// Returns the AutocompleteFlag derived from |element|'s autocomplete attribute.
AutocompleteFlag AutocompleteFlagForElement(
const blink::WebInputElement& element);
// The caller of this function is responsible for deleting the returned object. // The caller of this function is responsible for deleting the returned object.
re2::RE2* CreateMatcher(void* instance, const char* pattern); re2::RE2* CreateMatcher(void* instance, const char* pattern);
...@@ -81,15 +95,6 @@ std::unique_ptr<PasswordForm> CreatePasswordFormFromUnownedInputElements( ...@@ -81,15 +95,6 @@ std::unique_ptr<PasswordForm> CreatePasswordFormFromUnownedInputElements(
const FormsPredictionsMap* form_predictions, const FormsPredictionsMap* form_predictions,
UsernameDetectorCache* username_detector_cache); UsernameDetectorCache* username_detector_cache);
// Checks in a case-insensitive way if the autocomplete attribute for the given
// |element| is present and has the specified |value_in_lowercase|.
bool HasAutocompleteAttributeValue(const blink::WebInputElement& element,
base::StringPiece value_in_lowercase);
// Checks in a case-insensitive way if credit card autocomplete attributes for
// the given |element| are present.
bool HasCreditCardAutocompleteAttributes(const blink::WebInputElement& element);
// Returns whether the form |field| has a "password" type, but looks like a // Returns whether the form |field| has a "password" type, but looks like a
// credit card verification field. // credit card verification field.
bool IsCreditCardVerificationPasswordField(const blink::WebInputElement& field); bool IsCreditCardVerificationPasswordField(const blink::WebInputElement& field);
......
...@@ -99,7 +99,8 @@ std::vector<blink::WebInputElement> FindNewPasswordElementsMarkedBySite( ...@@ -99,7 +99,8 @@ std::vector<blink::WebInputElement> FindNewPasswordElementsMarkedBySite(
std::vector<blink::WebInputElement> passwords; std::vector<blink::WebInputElement> passwords;
auto is_new_password_field = [](const blink::WebInputElement& element) { auto is_new_password_field = [](const blink::WebInputElement& element) {
return HasAutocompleteAttributeValue(element, "new-password"); return AutocompleteFlagForElement(element) ==
AutocompleteFlag::NEW_PASSWORD;
}; };
auto field_iter = auto field_iter =
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment