Bug 1773832 - Refactor LoginManagerChild: getUserNameAndPasswordFields r=dimi

Differential Revision: https://phabricator.services.mozilla.com/D149006
This commit is contained in:
Sergey Galich 2022-06-14 16:15:09 +00:00
Родитель 65abb08ae6
Коммит bb845d1286
3 изменённых файлов: 60 добавлений и 52 удалений

Просмотреть файл

@ -111,9 +111,13 @@ class GeckoViewAutoFillChild extends GeckoViewActorChild {
}
}
const [usernameField] = lazy.LoginManagerChild.forWindow(
window
).getUserNameAndPasswordFields(passwordField || aFormLike.elements[0]);
const loginManagerChild = lazy.LoginManagerChild.forWindow(window);
const docState = loginManagerChild.stateForDocument(
passwordField.ownerDocument
);
const [usernameField] = docState.getUserNameAndPasswordFields(
passwordField || aFormLike.elements[0]
);
const focusedElement = aFormLike.rootElement.ownerDocument.activeElement;
let sendFocusEvent = aFormLike.rootElement === focusedElement;

Просмотреть файл

@ -393,7 +393,7 @@ const observer = {
let [
usernameField,
passwordField,
] = loginManagerChild.getUserNameAndPasswordFields(field);
] = docState.getUserNameAndPasswordFields(field);
if (field == usernameField && passwordField?.value) {
loginManagerChild._passwordEditedOrGenerated(passwordField, {
triggeredByFillingGenerated: docState.generatedPasswordFields.has(
@ -1380,6 +1380,50 @@ class LoginFormState {
oldPasswordField,
};
}
/**
* Returns the username and password fields found in the form by input
* element into form.
*
* @param {HTMLInputElement} aField
* A form field
* @return {Array} [usernameField, newPasswordField, oldPasswordField]
*
* Details of these values are the same as _getFormFields.
*/
getUserNameAndPasswordFields(aField) {
const noResult = [null, null, null];
if (!HTMLInputElement.isInstance(aField)) {
throw new Error("getUserNameAndPasswordFields: input element required");
}
if (aField.nodePrincipal.isNullPrincipal || !aField.isConnected) {
return noResult;
}
// If the element is not a login form field, return all null.
if (
!aField.hasBeenTypePassword &&
!lazy.LoginHelper.isUsernameFieldType(aField)
) {
return noResult;
}
const form = lazy.LoginFormFactory.createFromField(aField);
const doc = aField.ownerDocument;
const formOrigin = lazy.LoginHelper.getLoginOrigin(doc.documentURI);
const recipes = lazy.LoginRecipesContent.getRecipes(
formOrigin,
doc.defaultView
);
const {
usernameField,
newPasswordField,
oldPasswordField,
} = this._getFormFields(form, false, recipes);
return [usernameField, newPasswordField, oldPasswordField];
}
}
/**
@ -3109,51 +3153,6 @@ class LoginManagerChild extends JSWindowActorChild {
}
}
/**
* Returns the username and password fields found in the form by input
* element into form.
*
* @param {HTMLInputElement} aField
* A form field
* @return {Array} [usernameField, newPasswordField, oldPasswordField]
*
* Details of these values are the same as _getFormFields.
*/
getUserNameAndPasswordFields(aField) {
let noResult = [null, null, null];
if (!HTMLInputElement.isInstance(aField)) {
throw new Error("getUserNameAndPasswordFields: input element required");
}
if (aField.nodePrincipal.isNullPrincipal || !aField.isConnected) {
return noResult;
}
// If the element is not a login form field, return all null.
if (
!aField.hasBeenTypePassword &&
!lazy.LoginHelper.isUsernameFieldType(aField)
) {
return noResult;
}
let form = lazy.LoginFormFactory.createFromField(aField);
let doc = aField.ownerDocument;
let formOrigin = lazy.LoginHelper.getLoginOrigin(doc.documentURI);
let recipes = lazy.LoginRecipesContent.getRecipes(
formOrigin,
doc.defaultView
);
const docState = this.stateForDocument(form.ownerDocument);
let {
usernameField,
newPasswordField,
oldPasswordField,
} = docState._getFormFields(form, false, recipes);
return [usernameField, newPasswordField, oldPasswordField];
}
/**
* Verify if a field is a valid login form field and
* returns some information about it's LoginForm.
@ -3182,7 +3181,10 @@ class LoginManagerChild extends JSWindowActorChild {
// This array provides labels that correspond to the return values from
// `getUserNameAndPasswordFields` so we can know which one aField is.
const LOGIN_FIELD_ORDER = ["username", "new-password", "current-password"];
let usernameAndPasswordFields = this.getUserNameAndPasswordFields(aField);
const docState = this.stateForDocument(aField.ownerDocument);
let usernameAndPasswordFields = docState.getUserNameAndPasswordFields(
aField
);
let fieldNameHint;
let indexOfFieldInUsernameAndPasswordFields = usernameAndPasswordFields.indexOf(
aField

Просмотреть файл

@ -1,5 +1,5 @@
/**
* Test for LoginManagerChild.getUserNameAndPasswordFields
* Test for LoginFormState.getUserNameAndPasswordFields
*/
"use strict";
@ -148,7 +148,9 @@ for (let tc of TESTCASES) {
let formOrigin = LoginHelper.getLoginOrigin(document.documentURI);
LoginRecipesContent.cacheRecipes(formOrigin, win, new Set());
let actual = new LoginManagerChild().getUserNameAndPasswordFields(input);
const loginManagerChild = new LoginManagerChild();
const docState = loginManagerChild.stateForDocument(document);
let actual = docState.getUserNameAndPasswordFields(input);
Assert.strictEqual(
testcase.returnedFieldIDs.length,