summaryrefslogtreecommitdiffstats
path: root/toolkit/components/formautofill/FormAutofillStorageBase.sys.mjs
diff options
context:
space:
mode:
Diffstat (limited to 'toolkit/components/formautofill/FormAutofillStorageBase.sys.mjs')
-rw-r--r--toolkit/components/formautofill/FormAutofillStorageBase.sys.mjs193
1 files changed, 46 insertions, 147 deletions
diff --git a/toolkit/components/formautofill/FormAutofillStorageBase.sys.mjs b/toolkit/components/formautofill/FormAutofillStorageBase.sys.mjs
index 591bfc1578..f360be4fa6 100644
--- a/toolkit/components/formautofill/FormAutofillStorageBase.sys.mjs
+++ b/toolkit/components/formautofill/FormAutofillStorageBase.sys.mjs
@@ -130,18 +130,19 @@
*/
import { FormAutofill } from "resource://autofill/FormAutofill.sys.mjs";
+import { AddressRecord } from "resource://gre/modules/shared/AddressRecord.sys.mjs";
const lazy = {};
ChromeUtils.defineESModuleGetters(lazy, {
- AutofillTelemetry: "resource://autofill/AutofillTelemetry.sys.mjs",
+ 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",
FormAutofillUtils: "resource://gre/modules/shared/FormAutofillUtils.sys.mjs",
OSKeyStore: "resource://gre/modules/OSKeyStore.sys.mjs",
- PhoneNumber: "resource://autofill/phonenumberutils/PhoneNumber.sys.mjs",
+ PhoneNumber: "resource://gre/modules/shared/PhoneNumber.sys.mjs",
});
const CryptoHash = Components.Constructor(
@@ -166,23 +167,6 @@ export const ADDRESS_SCHEMA_VERSION = 1;
// Please talk to the sync team before changing this!
export const CREDIT_CARD_SCHEMA_VERSION = 3;
-const NAME_COMPONENTS = ["given-name", "additional-name", "family-name"];
-
-const STREET_ADDRESS_COMPONENTS = [
- "address-line1",
- "address-line2",
- "address-line3",
-];
-
-const TEL_COMPONENTS = [
- "tel-country-code",
- "tel-national",
- "tel-area-code",
- "tel-local",
- "tel-local-prefix",
- "tel-local-suffix",
-];
-
const VALID_ADDRESS_FIELDS = [
"name",
"organization",
@@ -198,9 +182,9 @@ const VALID_ADDRESS_FIELDS = [
const VALID_ADDRESS_COMPUTED_FIELDS = [
"country-name",
- ...NAME_COMPONENTS,
- ...STREET_ADDRESS_COMPONENTS,
- ...TEL_COMPONENTS,
+ ...AddressRecord.NAME_COMPONENTS,
+ ...AddressRecord.STREET_ADDRESS_COMPONENTS,
+ ...AddressRecord.TEL_COMPONENTS,
];
const VALID_CREDIT_CARD_FIELDS = [
@@ -299,20 +283,18 @@ class AutofillRecords {
});
}
- observe(subject, topic, data) {
- switch (topic) {
- case "formautofill-storage-changed":
- let collectionName = subject.wrappedJSObject.collectionName;
- if (collectionName != this._collectionName) {
- return;
- }
- const telemetryType =
- subject.wrappedJSObject.collectionName == "creditCards"
- ? lazy.AutofillTelemetry.CREDIT_CARD
- : lazy.AutofillTelemetry.ADDRESS;
- const count = this._data.filter(entry => !entry.deleted).length;
- lazy.AutofillTelemetry.recordAutofillProfileCount(telemetryType, count);
- break;
+ observe(subject, topic, _data) {
+ if (topic == "formautofill-storage-changed") {
+ let collectionName = subject.wrappedJSObject.collectionName;
+ if (collectionName != this._collectionName) {
+ return;
+ }
+ const telemetryType =
+ subject.wrappedJSObject.collectionName == "creditCards"
+ ? lazy.AutofillTelemetry.CREDIT_CARD
+ : lazy.AutofillTelemetry.ADDRESS;
+ const count = this._data.filter(entry => !entry.deleted).length;
+ lazy.AutofillTelemetry.recordAutofillProfileCount(telemetryType, count);
}
}
@@ -675,7 +657,7 @@ class AutofillRecords {
// Excluding *-name fields from the sync payload would prevent older devices from
// synchronizing with newer devices. To maintain backward compatibility, keep those deprecated
// ields in the payload, ensuring that older devices can still sync with newer devices.
- const fieldsToKeep = NAME_COMPONENTS;
+ const fieldsToKeep = AddressRecord.NAME_COMPONENTS;
await this._stripComputedFields(clonedRecord, fieldsToKeep);
} else {
this._recordReadProcessor(clonedRecord);
@@ -703,7 +685,7 @@ class AutofillRecords {
await Promise.all(
clonedRecords.map(async record => {
if (rawData) {
- const fieldsToKeep = NAME_COMPONENTS;
+ const fieldsToKeep = AddressRecord.NAME_COMPONENTS;
await this._stripComputedFields(record, fieldsToKeep);
} else {
this._recordReadProcessor(record);
@@ -1398,7 +1380,12 @@ class AutofillRecords {
return hasChanges;
}
- hasChanges |= await this.computeFields(record);
+ const originalNumFields = Object.keys(record).length;
+ await this.computeFields(record);
+ const hasNewComputedFields =
+ Object.keys(record).length != originalNumFields;
+
+ hasChanges |= hasNewComputedFields;
return hasChanges;
}
@@ -1486,36 +1473,36 @@ class AutofillRecords {
}
// An interface to be inherited.
- _recordReadProcessor(record) {}
+ _recordReadProcessor(_record) {}
// An interface to be inherited.
- async computeFields(record) {}
+ async computeFields(_record) {}
/**
* An interface to be inherited to mutate the argument to normalize it.
*
- * @param {object} partialRecord containing the record passed by the consumer of
+ * @param {object} _partialRecord containing the record passed by the consumer of
* storage and in the case of `update` with
* `preserveOldProperties` will only include the
* properties that the user is changing so the
* lack of a field doesn't mean that the record
* won't have that field.
*/
- _normalizeFields(partialRecord) {}
+ _normalizeFields(_partialRecord) {}
/**
* An interface to be inherited to validate that the complete record is
* consistent and isn't missing required fields. Overrides should throw for
* invalid records.
*
- * @param {object} record containing the complete record that would be stored
+ * @param {object} _record containing the complete record that would be stored
* if this doesn't throw due to an error.
* @throws
*/
- _validateFields(record) {}
+ _validateFields(_record) {}
// An interface to be inherited.
- migrateRemoteRecord(remoteRecord) {}
+ migrateRemoteRecord(_remoteRecord) {}
}
export class AddressesBase extends AutofillRecords {
@@ -1578,99 +1565,9 @@ export class AddressesBase extends AutofillRecords {
// NOTE: Computed fields should be always present in the storage no matter
// it's empty or not.
- let hasNewComputedFields = false;
-
- if (address.deleted) {
- return hasNewComputedFields;
- }
-
- // Compute split names
- if (!("given-name" in address)) {
- const nameParts = lazy.FormAutofillNameUtils.splitName(address.name);
- address["given-name"] = nameParts.given;
- address["additional-name"] = nameParts.middle;
- address["family-name"] = nameParts.family;
- hasNewComputedFields = true;
- }
-
- // Compute address lines
- if (!("address-line1" in address)) {
- let streetAddress = [];
- if (address["street-address"]) {
- streetAddress = address["street-address"]
- .split("\n")
- .map(s => s.trim());
- }
- for (let i = 0; i < 3; i++) {
- address[`address-line${i + 1}`] = streetAddress[i] || "";
- }
- if (streetAddress.length > 3) {
- address["address-line3"] = lazy.FormAutofillUtils.toOneLineAddress(
- streetAddress.slice(2)
- );
- }
- hasNewComputedFields = true;
- }
-
- // Compute country name
- if (!("country-name" in address)) {
- if (address.country) {
- try {
- address["country-name"] = Services.intl.getRegionDisplayNames(
- undefined,
- [address.country]
- );
- } catch (e) {
- address["country-name"] = "";
- }
- } else {
- address["country-name"] = "";
- }
- hasNewComputedFields = true;
+ if (!address.deleted) {
+ AddressRecord.computeFields(address);
}
-
- // Compute tel
- if (!("tel-national" in address)) {
- if (address.tel) {
- let tel = lazy.PhoneNumber.Parse(
- address.tel,
- address.country || FormAutofill.DEFAULT_REGION
- );
- if (tel) {
- if (tel.countryCode) {
- address["tel-country-code"] = tel.countryCode;
- }
- if (tel.nationalNumber) {
- address["tel-national"] = tel.nationalNumber;
- }
-
- // PhoneNumberUtils doesn't support parsing the components of a telephone
- // number so we hard coded the parser for US numbers only. We will need
- // to figure out how to parse numbers from other regions when we support
- // new countries in the future.
- if (tel.nationalNumber && tel.countryCode == "+1") {
- let telComponents = tel.nationalNumber.match(
- /(\d{3})((\d{3})(\d{4}))$/
- );
- if (telComponents) {
- address["tel-area-code"] = telComponents[1];
- address["tel-local"] = telComponents[2];
- address["tel-local-prefix"] = telComponents[3];
- address["tel-local-suffix"] = telComponents[4];
- }
- }
- } else {
- // Treat "tel" as "tel-national" directly if it can't be parsed.
- address["tel-national"] = address.tel;
- }
- }
-
- TEL_COMPONENTS.forEach(c => {
- address[c] = address[c] || "";
- });
- }
-
- return hasNewComputedFields;
}
_normalizeFields(address) {
@@ -1700,7 +1597,7 @@ export class AddressesBase extends AutofillRecords {
}
_normalizeAddressFields(address) {
- if (STREET_ADDRESS_COMPONENTS.some(c => !!address[c])) {
+ if (AddressRecord.STREET_ADDRESS_COMPONENTS.some(c => !!address[c])) {
// Treat "street-address" as "address-line1" if it contains only one line
// and "address-line1" is omitted.
if (
@@ -1714,14 +1611,14 @@ export class AddressesBase extends AutofillRecords {
// Concatenate "address-line*" if "street-address" is omitted.
if (!address["street-address"]) {
- address["street-address"] = STREET_ADDRESS_COMPONENTS.map(
+ address["street-address"] = AddressRecord.STREET_ADDRESS_COMPONENTS.map(
c => address[c]
)
.join("\n")
.replace(/\n+$/, "");
}
}
- STREET_ADDRESS_COMPONENTS.forEach(c => delete address[c]);
+ AddressRecord.STREET_ADDRESS_COMPONENTS.forEach(c => delete address[c]);
}
_normalizeCountryFields(address) {
@@ -1753,7 +1650,7 @@ export class AddressesBase extends AutofillRecords {
}
_normalizeTelFields(address) {
- if (address.tel || TEL_COMPONENTS.some(c => !!address[c])) {
+ if (address.tel || AddressRecord.TEL_COMPONENTS.some(c => !!address[c])) {
lazy.FormAutofillUtils.compressTel(address);
let possibleRegion = address.country || FormAutofill.DEFAULT_REGION;
@@ -1764,7 +1661,7 @@ export class AddressesBase extends AutofillRecords {
address.tel = tel.internationalNumber;
}
}
- TEL_COMPONENTS.forEach(c => delete address[c]);
+ AddressRecord.TEL_COMPONENTS.forEach(c => delete address[c]);
}
/**
@@ -1793,12 +1690,14 @@ export class AddressesBase extends AutofillRecords {
// we will rebuild it and replace the local `name` field with "Jane Poe".
if (
!("name" in remoteRecord) &&
- NAME_COMPONENTS.some(c => c in remoteRecord)
+ AddressRecord.NAME_COMPONENTS.some(c => c in remoteRecord)
) {
const localRecord = this._findByGUID(remoteRecord.guid);
if (
localRecord &&
- NAME_COMPONENTS.every(c => remoteRecord[c] == localRecord[c])
+ AddressRecord.NAME_COMPONENTS.every(
+ c => remoteRecord[c] == localRecord[c]
+ )
) {
remoteRecord.name = localRecord.name;
} else {
@@ -1815,7 +1714,7 @@ export class AddressesBase extends AutofillRecords {
// This also means that the incoming remote record will also contain *-name fields.
// However, since the autofill storage does not expect remote records to contain
// computed fields while merging, we remove them from the remote record.
- NAME_COMPONENTS.forEach(f => delete remoteRecord[f]);
+ AddressRecord.NAME_COMPONENTS.forEach(f => delete remoteRecord[f]);
}
}
@@ -1879,7 +1778,7 @@ export class CreditCardsBase extends AutofillRecords {
return hasNewComputedFields;
}
- async _encryptNumber(creditCard) {
+ async _encryptNumber(_creditCard) {
throw Components.Exception("", Cr.NS_ERROR_NOT_IMPLEMENTED);
}