commit f84953d949bb3a5a368fa7cefbb68a93baba3034
parent 0cad5eb18b1c9510708d16676f8e4121af685ec1
Author: Segun Famisa <sfamisa@mozilla.com>
Date: Mon, 8 Dec 2025 10:02:56 +0000
Bug 2002373 - Move credit card field computation to `CreditCardRecord` r=credential-management-reviewers,dimi
This commit refactors the credit card processing logic by moving the responsibility for computing derived fields from `FormAutofillStorageBase.sys.mjs` into the `CreditCardRecord.sys.mjs` shared module.
Differential Revision: https://phabricator.services.mozilla.com/D274909
Diffstat:
2 files changed, 47 insertions(+), 34 deletions(-)
diff --git a/toolkit/components/formautofill/FormAutofillStorageBase.sys.mjs b/toolkit/components/formautofill/FormAutofillStorageBase.sys.mjs
@@ -136,7 +136,6 @@ const lazy = {};
ChromeUtils.defineESModuleGetters(lazy, {
AutofillTelemetry: "resource://gre/modules/shared/AutofillTelemetry.sys.mjs",
- CreditCard: "resource://gre/modules/CreditCard.sys.mjs",
CreditCardRecord: "resource://gre/modules/shared/CreditCardRecord.sys.mjs",
FormAutofillNameUtils:
"resource://gre/modules/shared/FormAutofillNameUtils.sys.mjs",
@@ -1746,45 +1745,14 @@ export class CreditCardsBase extends AutofillRecords {
// NOTE: Computed fields should be always present in the storage no matter
// it's empty or not.
- let hasNewComputedFields = false;
-
if (creditCard.deleted) {
- return hasNewComputedFields;
+ return;
}
- let type = lazy.CreditCard.getType(creditCard["cc-number"]);
- if (type) {
- creditCard["cc-type"] = type;
- }
-
- // Compute split names
- if (!("cc-given-name" in creditCard)) {
- const nameParts = lazy.FormAutofillNameUtils.splitName(
- creditCard["cc-name"]
- );
- creditCard["cc-given-name"] = nameParts.given;
- creditCard["cc-additional-name"] = nameParts.middle;
- creditCard["cc-family-name"] = nameParts.family;
- hasNewComputedFields = true;
- }
-
- // Compute credit card expiration date
- if (!("cc-exp" in creditCard)) {
- if (creditCard["cc-exp-month"] && creditCard["cc-exp-year"]) {
- creditCard["cc-exp"] =
- String(creditCard["cc-exp-year"]) +
- "-" +
- String(creditCard["cc-exp-month"]).padStart(2, "0");
- } else {
- creditCard["cc-exp"] = "";
- }
- hasNewComputedFields = true;
- }
+ lazy.CreditCardRecord.computeFields(creditCard);
// Encrypt credit card number
await this._encryptNumber(creditCard);
-
- return hasNewComputedFields;
}
async _encryptNumber(_creditCard) {
diff --git a/toolkit/components/formautofill/shared/CreditCardRecord.sys.mjs b/toolkit/components/formautofill/shared/CreditCardRecord.sys.mjs
@@ -12,6 +12,51 @@ import { FormAutofillNameUtils } from "resource://gre/modules/shared/FormAutofil
* for processing and consistent data representation.
*/
export class CreditCardRecord {
+ /**
+ * Computes derived fields from the basic fields in the CreditCard object.
+ *
+ * @param {object} creditCard The credit card object
+ */
+ static computeFields(creditCard) {
+ this.#computeCCNameFields(creditCard);
+ this.#computeCCExpirationDateFields(creditCard);
+ this.#computeCCTypeField(creditCard);
+ }
+
+ static #computeCCExpirationDateFields(creditCard) {
+ if (!("cc-exp" in creditCard)) {
+ if (creditCard["cc-exp-month"] && creditCard["cc-exp-year"]) {
+ creditCard["cc-exp"] =
+ String(creditCard["cc-exp-year"]) +
+ "-" +
+ String(creditCard["cc-exp-month"]).padStart(2, "0");
+ } else {
+ creditCard["cc-exp"] = "";
+ }
+ }
+ }
+
+ static #computeCCNameFields(creditCard) {
+ if (!("cc-given-name" in creditCard)) {
+ const nameParts = FormAutofillNameUtils.splitName(creditCard["cc-name"]);
+ creditCard["cc-given-name"] = nameParts.given;
+ creditCard["cc-additional-name"] = nameParts.middle;
+ creditCard["cc-family-name"] = nameParts.family;
+ }
+ }
+
+ static #computeCCTypeField(creditCard) {
+ const type = CreditCard.getType(creditCard["cc-number"]);
+ if (type) {
+ creditCard["cc-type"] = type;
+ }
+ }
+
+ /**
+ * Normalizes credit card fields by removing derived fields from the CreditCard, leaving the basic fields.
+ *
+ * @param {object} creditCard The credit card object
+ */
static normalizeFields(creditCard) {
this.#normalizeCCNameFields(creditCard);
this.#normalizeCCNumberFields(creditCard);