diff --git a/browser/extensions/formautofill/ProfileStorage.jsm b/browser/extensions/formautofill/ProfileStorage.jsm index 8b2a4d47c2cd..b12ae2f07b92 100644 --- a/browser/extensions/formautofill/ProfileStorage.jsm +++ b/browser/extensions/formautofill/ProfileStorage.jsm @@ -14,6 +14,7 @@ * addresses: [ * { * guid, // 12 characters + * version, // schema version in integer * * // address fields * given-name, @@ -45,6 +46,7 @@ * creditCards: [ * { * guid, // 12 characters + * version, // schema version in integer * * // credit card fields * cc-name, @@ -95,7 +97,9 @@ XPCOMUtils.defineLazyServiceGetter(this, "gUUIDGenerator", const PROFILE_JSON_FILE_NAME = "autofill-profiles.json"; -const SCHEMA_VERSION = 1; +const STORAGE_SCHEMA_VERSION = 1; +const ADDRESS_SCHEMA_VERSION = 1; +const CREDIT_CARD_SCHEMA_VERSION = 1; const VALID_PROFILE_FIELDS = [ "given-name", @@ -121,6 +125,7 @@ const VALID_CREDIT_CARD_FIELDS = [ const INTERNAL_FIELDS = [ "guid", + "version", "timeCreated", "timeLastUsed", "timeLastModified", @@ -144,14 +149,17 @@ class AutofillRecords { * A key of "store.data". * @param {Array.} validFields * A list containing non-metadata field names. + * @param {number} schemaVersion + * The schema version for the new record. */ - constructor(store, collectionName, validFields) { + constructor(store, collectionName, validFields, schemaVersion) { FormAutofillUtils.defineLazyLogGetter(this, "AutofillRecords:" + collectionName); this.VALID_FIELDS = validFields; this._store = store; this._collectionName = collectionName; + this._schemaVersion = schemaVersion; } /** @@ -161,7 +169,7 @@ class AutofillRecords { * The current schema version number. */ get version() { - return SCHEMA_VERSION; + return this._schemaVersion; } /** @@ -184,6 +192,7 @@ class AutofillRecords { .replace(/[{}-]/g, "").substring(0, 12); } recordToSave.guid = guid; + recordToSave.version = this.version; // Metadata let now = Date.now(); @@ -374,7 +383,7 @@ class AutofillRecords { class Addresses extends AutofillRecords { constructor(store) { - super(store, "addresses", VALID_PROFILE_FIELDS); + super(store, "addresses", VALID_PROFILE_FIELDS, ADDRESS_SCHEMA_VERSION); } _recordReadProcessor(profile, {noComputedFields} = {}) { @@ -449,7 +458,7 @@ class Addresses extends AutofillRecords { class CreditCards extends AutofillRecords { constructor(store) { - super(store, "creditCards", VALID_CREDIT_CARD_FIELDS); + super(store, "creditCards", VALID_CREDIT_CARD_FIELDS, CREDIT_CARD_SCHEMA_VERSION); } _recordReadProcessor(creditCard, {noComputedFields} = {}) { @@ -541,6 +550,10 @@ function ProfileStorage(path) { } ProfileStorage.prototype = { + get version() { + return STORAGE_SCHEMA_VERSION; + }, + get addresses() { if (!this._addresses) { this._store.ensureDataReady(); @@ -576,7 +589,7 @@ ProfileStorage.prototype = { }, _dataPostProcessor(data) { - data.version = SCHEMA_VERSION; + data.version = this.version; if (!data.addresses) { data.addresses = []; } diff --git a/browser/extensions/formautofill/test/unit/test_addressRecords.js b/browser/extensions/formautofill/test/unit/test_addressRecords.js index c599f28545ef..9632f2a0e36b 100644 --- a/browser/extensions/formautofill/test/unit/test_addressRecords.js +++ b/browser/extensions/formautofill/test/unit/test_addressRecords.js @@ -175,6 +175,7 @@ add_task(async function test_add() { do_check_record_matches(addresses[1], TEST_ADDRESS_2); do_check_neq(addresses[0].guid, undefined); + do_check_eq(addresses[0].version, 1); do_check_neq(addresses[0].timeCreated, undefined); do_check_eq(addresses[0].timeLastModified, addresses[0].timeCreated); do_check_eq(addresses[0].timeLastUsed, 0); diff --git a/browser/extensions/formautofill/test/unit/test_creditCardRecords.js b/browser/extensions/formautofill/test/unit/test_creditCardRecords.js index 01403fb49247..11a56bd9ac39 100644 --- a/browser/extensions/formautofill/test/unit/test_creditCardRecords.js +++ b/browser/extensions/formautofill/test/unit/test_creditCardRecords.js @@ -187,6 +187,7 @@ add_task(async function test_add() { do_check_credit_card_matches(creditCards[1], TEST_CREDIT_CARD_2); do_check_neq(creditCards[0].guid, undefined); + do_check_eq(creditCards[0].version, 1); do_check_neq(creditCards[0].timeCreated, undefined); do_check_eq(creditCards[0].timeLastModified, creditCards[0].timeCreated); do_check_eq(creditCards[0].timeLastUsed, 0);