Bug 1862091 - Refactor FormAutofill entry point script to support both credit cards and addresses. r=credential-management-reviewers,dimi

Differential Revision: https://phabricator.services.mozilla.com/D192292
This commit is contained in:
Issam Mani 2023-10-31 14:55:00 +00:00
Родитель 2fb40c1d8a
Коммит 10a9ab9367
1 изменённых файлов: 42 добавлений и 14 удалений

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

@ -8,12 +8,22 @@ import { FormStateManager } from "resource://gre/modules/shared/FormStateManager
import { CreditCardRecord } from "resource://gre/modules/shared/CreditCardRecord.sys.mjs";
export class FormAutofillChild {
constructor(onSubmitCallback, onAutofillCallback) {
/**
* Creates an instance of FormAutofillChild.
*
* @param {object} callbacks - An object containing callback functions.
* @param {object} callbacks.address - Callbacks related to addresses.
* @param {Function} callbacks.address.autofill - Function called to autofill address fields.
* @param {Function} callbacks.address.submit - Function called on address form submission.
* @param {object} callbacks.creditCard - Callbacks related to credit cards.
* @param {Function} callbacks.creditCard.autofill - Function called to autofill credit card fields.
* @param {Function} callbacks.creditCard.submit - Function called on credit card form submission.
*/
constructor(callbacks) {
this.onFocusIn = this.onFocusIn.bind(this);
this.onSubmit = this.onSubmit.bind(this);
this.onSubmitCallback = onSubmitCallback;
this.onAutofillCallback = onAutofillCallback;
this.callbacks = callbacks;
this.fieldDetailsManager = new FormStateManager();
@ -26,22 +36,37 @@ export class FormAutofillChild {
const validDetails =
this.fieldDetailsManager.identifyAutofillFields(element);
// Only ping swift if current field is a cc field
if (validDetails?.find(field => field.element === element)) {
const fieldNamesWithValues = validDetails?.reduce(
(acc, field) => ({
...acc,
[field.fieldName]: field.element.value,
}),
{}
);
const activeFieldName =
this.fieldDetailsManager.activeFieldDetail?.fieldName;
// Only ping swift if current field is either a cc or address field
if (!validDetails?.find(field => field.element === element)) {
return;
}
const fieldNamesWithValues =
this.transformToFieldNamesWithValues(validDetails);
if (FormAutofillUtils.isAddressField(activeFieldName)) {
this.callbacks.address.autofill(fieldNamesWithValues);
} else if (FormAutofillUtils.isCreditCardField(activeFieldName)) {
// Normalize record format so we always get a consistent
// credit card record format: {cc-number, cc-name, cc-exp-month, cc-exp-year}
CreditCardRecord.normalizeFields(fieldNamesWithValues);
this.onAutofillCallback(fieldNamesWithValues);
this.callbacks.creditCard.autofill(fieldNamesWithValues);
}
}
transformToFieldNamesWithValues(details) {
return details?.reduce(
(acc, field) => ({
...acc,
[field.fieldName]: field.element.value,
}),
{}
);
}
onFocusIn(evt) {
const element = evt.target;
this.fieldDetailsManager.updateActiveInput(element);
@ -61,8 +86,11 @@ export class FormAutofillChild {
CreditCardRecord.normalizeFields(entry.record);
return entry.record;
});
this.onSubmitCallback(creditCardRecords);
this.callbacks.creditCard.submit(creditCardRecords);
}
// TODO(FXSP-133 Phase 3): Support address capture
// this.callbacks.address.submit();
}
fillFormFields(payload) {