From bb845d1286ee2659de5232aa238c1e31e4b7079d Mon Sep 17 00:00:00 2001 From: Sergey Galich Date: Tue, 14 Jun 2022 16:15:09 +0000 Subject: [PATCH] Bug 1773832 - Refactor LoginManagerChild: getUserNameAndPasswordFields r=dimi Differential Revision: https://phabricator.services.mozilla.com/D149006 --- .../android/actors/GeckoViewAutoFillChild.jsm | 10 +- .../passwordmgr/LoginManagerChild.jsm | 96 ++++++++++--------- .../unit/test_getUserNameAndPasswordFields.js | 6 +- 3 files changed, 60 insertions(+), 52 deletions(-) diff --git a/mobile/android/actors/GeckoViewAutoFillChild.jsm b/mobile/android/actors/GeckoViewAutoFillChild.jsm index 9f1bf02e93fe..5258ddbc844c 100644 --- a/mobile/android/actors/GeckoViewAutoFillChild.jsm +++ b/mobile/android/actors/GeckoViewAutoFillChild.jsm @@ -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; diff --git a/toolkit/components/passwordmgr/LoginManagerChild.jsm b/toolkit/components/passwordmgr/LoginManagerChild.jsm index e087b7434c11..5c1a5da63780 100644 --- a/toolkit/components/passwordmgr/LoginManagerChild.jsm +++ b/toolkit/components/passwordmgr/LoginManagerChild.jsm @@ -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 diff --git a/toolkit/components/passwordmgr/test/unit/test_getUserNameAndPasswordFields.js b/toolkit/components/passwordmgr/test/unit/test_getUserNameAndPasswordFields.js index c962117c3497..908a452359a0 100644 --- a/toolkit/components/passwordmgr/test/unit/test_getUserNameAndPasswordFields.js +++ b/toolkit/components/passwordmgr/test/unit/test_getUserNameAndPasswordFields.js @@ -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,