Bug 1429976 - Include collectionName in formautofill-storage-changed notify payload. r=lchang draft
authorRay Lin <ralin@mozilla.com>
Tue, 16 Jan 2018 12:18:48 +0800
changeset 720755 8448e5740805b58da3b3e625589ec73806c5c08d
parent 720581 8e33bdf820dcc2ecd6f326eb83c39fc5e4769dcf
child 746137 e423756ca3d1452f54da449e7c821ef2824cd7b6
push id95625
push userbmo:ralin@mozilla.com
push dateTue, 16 Jan 2018 06:22:01 +0000
reviewerslchang
bugs1429976
milestone59.0a1
Bug 1429976 - Include collectionName in formautofill-storage-changed notify payload. r=lchang MozReview-Commit-ID: 55m97PwMXsx
browser/extensions/formautofill/ProfileStorage.jsm
browser/extensions/formautofill/test/unit/head.js
browser/extensions/formautofill/test/unit/test_addressRecords.js
browser/extensions/formautofill/test/unit/test_creditCardRecords.js
browser/extensions/formautofill/test/unit/test_reconcile.js
--- a/browser/extensions/formautofill/ProfileStorage.jsm
+++ b/browser/extensions/formautofill/ProfileStorage.jsm
@@ -371,17 +371,20 @@ class AutofillRecords {
       let sync = this._getSyncMetaData(recordToSave, true);
       sync.changeCounter = 0;
     }
 
     this._data.push(recordToSave);
 
     this._store.saveSoon();
 
-    Services.obs.notifyObservers({wrappedJSObject: {sourceSync}}, "formautofill-storage-changed", "add");
+    Services.obs.notifyObservers({wrappedJSObject: {
+      sourceSync,
+      collectionName: this._collectionName,
+    }}, "formautofill-storage-changed", "add");
     return recordToSave.guid;
   }
 
   _generateGUID() {
     let guid;
     while (!guid || this._findByGUID(guid)) {
       guid = gUUIDGenerator.generateUUID().toString()
                            .replace(/[{}-]/g, "").substring(0, 12);
@@ -444,19 +447,20 @@ class AutofillRecords {
       syncMetadata.changeCounter += 1;
     }
 
     this._computeFields(recordFound);
     this._data[recordFoundIndex] = recordFound;
 
     this._store.saveSoon();
 
-    let str = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsString);
-    str.data = guid;
-    Services.obs.notifyObservers(str, "formautofill-storage-changed", "update");
+    Services.obs.notifyObservers({wrappedJSObject: {
+      guid,
+      collectionName: this._collectionName,
+    }}, "formautofill-storage-changed", "update");
   }
 
   /**
    * Notifies the storage of the use of the specified record, so we can update
    * the metadata accordingly. This does not bump the Sync change counter, since
    * we don't sync `timesUsed` or `timeLastUsed`.
    *
    * @param  {string} guid
@@ -514,17 +518,20 @@ class AutofillRecords {
       } else {
         // If there's no sync meta-data, this record has never been synced, so
         // we can delete it.
         this._data.splice(index, 1);
       }
     }
 
     this._store.saveSoon();
-    Services.obs.notifyObservers({wrappedJSObject: {sourceSync}}, "formautofill-storage-changed", "remove");
+    Services.obs.notifyObservers({wrappedJSObject: {
+      sourceSync,
+      collectionName: this._collectionName,
+    }}, "formautofill-storage-changed", "remove");
   }
 
   /**
    * Returns the record with the specified GUID.
    *
    * @param   {string} guid
    *          Indicates which record to retrieve.
    * @param   {boolean} [options.rawData = false]
@@ -833,16 +840,17 @@ class AutofillRecords {
           keepSyncMetadata: false,
         });
       }
     }
 
     this._store.saveSoon();
     Services.obs.notifyObservers({wrappedJSObject: {
       sourceSync: true,
+      collectionName: this._collectionName,
     }}, "formautofill-storage-changed", "reconcile");
 
     return {forkedGUID};
   }
 
   _removeSyncedRecord(guid) {
     let index = this._findIndexByGUID(guid, {includeDeleted: true});
     if (index == -1) {
--- a/browser/extensions/formautofill/test/unit/head.js
+++ b/browser/extensions/formautofill/test/unit/head.js
@@ -63,18 +63,22 @@ async function initProfileStorage(fileNa
   let path = getTempFile(fileName).path;
   let profileStorage = new ProfileStorage(path);
   await profileStorage.initialize();
 
   if (!records || !Array.isArray(records)) {
     return profileStorage;
   }
 
-  let onChanged = TestUtils.topicObserved("formautofill-storage-changed",
-                                          (subject, data) => data == "add");
+  let onChanged = TestUtils.topicObserved(
+    "formautofill-storage-changed",
+    (subject, data) =>
+      data == "add" &&
+      subject.wrappedJSObject.collectionName == collectionName
+  );
   for (let record of records) {
     Assert.ok(profileStorage[collectionName].add(record));
     await onChanged;
   }
   await profileStorage._saveImmediately();
   return profileStorage;
 }
 
--- a/browser/extensions/formautofill/test/unit/test_addressRecords.js
+++ b/browser/extensions/formautofill/test/unit/test_addressRecords.js
@@ -1,15 +1,16 @@
 /**
  * Tests ProfileStorage object with addresses records.
  */
 
 "use strict";
 
 const TEST_STORE_FILE_NAME = "test-profile.json";
+const COLLECTION_NAME = "addresses";
 
 const TEST_ADDRESS_1 = {
   "given-name": "Timothy",
   "additional-name": "John",
   "family-name": "Berners-Lee",
   organization: "World Wide Web Consortium",
   "street-address": "32 Vassar Street\nMIT Room 32-G524",
   "address-level2": "Cambridge",
@@ -373,17 +374,19 @@ add_task(async function test_update() {
 
   let addresses = profileStorage.addresses.getAll();
   let guid = addresses[1].guid;
   let timeLastModified = addresses[1].timeLastModified;
 
   let onChanged = TestUtils.topicObserved(
     "formautofill-storage-changed",
     (subject, data) =>
-      data == "update" && subject.QueryInterface(Ci.nsISupportsString).data == guid
+      data == "update" &&
+      subject.wrappedJSObject.guid == guid &&
+      subject.wrappedJSObject.collectionName == COLLECTION_NAME
   );
 
   Assert.notEqual(addresses[1].country, undefined);
 
   profileStorage.addresses.update(guid, TEST_ADDRESS_3);
   await onChanged;
   await profileStorage._saveImmediately();
 
@@ -486,18 +489,22 @@ add_task(async function test_notifyUsed(
 
 add_task(async function test_remove() {
   let profileStorage = await initProfileStorage(TEST_STORE_FILE_NAME,
                                                 [TEST_ADDRESS_1, TEST_ADDRESS_2]);
 
   let addresses = profileStorage.addresses.getAll();
   let guid = addresses[1].guid;
 
-  let onChanged = TestUtils.topicObserved("formautofill-storage-changed",
-                                          (subject, data) => data == "remove");
+  let onChanged = TestUtils.topicObserved(
+    "formautofill-storage-changed",
+    (subject, data) =>
+      data == "remove" &&
+      subject.wrappedJSObject.collectionName == COLLECTION_NAME
+  );
 
   Assert.equal(addresses.length, 2);
 
   profileStorage.addresses.remove(guid);
   await onChanged;
 
   addresses = profileStorage.addresses.getAll();
 
@@ -514,17 +521,19 @@ MERGE_TESTCASES.forEach((testcase) => {
     let addresses = profileStorage.addresses.getAll();
     let guid = addresses[0].guid;
     let timeLastModified = addresses[0].timeLastModified;
 
     // Merge address and verify the guid in notifyObservers subject
     let onMerged = TestUtils.topicObserved(
       "formautofill-storage-changed",
       (subject, data) =>
-        data == "update" && subject.QueryInterface(Ci.nsISupportsString).data == guid
+        data == "update" &&
+        subject.wrappedJSObject.guid == guid &&
+        subject.wrappedJSObject.collectionName == COLLECTION_NAME
     );
 
     // Force to create sync metadata.
     profileStorage.addresses.pullSyncChanges();
     Assert.equal(getSyncChangeCounter(profileStorage.addresses, guid), 1);
 
     Assert.ok(profileStorage.addresses.mergeIfPossible(guid,
                                                        testcase.addressToMerge,
--- a/browser/extensions/formautofill/test/unit/test_creditCardRecords.js
+++ b/browser/extensions/formautofill/test/unit/test_creditCardRecords.js
@@ -2,16 +2,17 @@
  * Tests ProfileStorage object with creditCards records.
  */
 
 "use strict";
 
 const {ProfileStorage} = Cu.import("resource://formautofill/ProfileStorage.jsm", {});
 
 const TEST_STORE_FILE_NAME = "test-credit-card.json";
+const COLLECTION_NAME = "creditCards";
 
 const TEST_CREDIT_CARD_1 = {
   "cc-name": "John Doe",
   "cc-number": "1234567812345678",
   "cc-exp-month": 4,
   "cc-exp-year": 2017,
 };
 
@@ -143,18 +144,22 @@ const MERGE_TESTCASES = [
     },
   },
 ];
 
 let prepareTestCreditCards = async function(path) {
   let profileStorage = new ProfileStorage(path);
   await profileStorage.initialize();
 
-  let onChanged = TestUtils.topicObserved("formautofill-storage-changed",
-                                          (subject, data) => data == "add");
+  let onChanged = TestUtils.topicObserved(
+    "formautofill-storage-changed",
+    (subject, data) =>
+      data == "add" &&
+      subject.wrappedJSObject.collectionName == COLLECTION_NAME
+  );
   Assert.ok(profileStorage.creditCards.add(TEST_CREDIT_CARD_1));
   await onChanged;
   Assert.ok(profileStorage.creditCards.add(TEST_CREDIT_CARD_2));
   await onChanged;
   await profileStorage._saveImmediately();
 };
 
 let reCCNumber = /^(\*+)(.{4})$/;
@@ -290,18 +295,22 @@ add_task(async function test_update() {
 
   let profileStorage = new ProfileStorage(path);
   await profileStorage.initialize();
 
   let creditCards = profileStorage.creditCards.getAll();
   let guid = creditCards[1].guid;
   let timeLastModified = creditCards[1].timeLastModified;
 
-  let onChanged = TestUtils.topicObserved("formautofill-storage-changed",
-                                          (subject, data) => data == "update");
+  let onChanged = TestUtils.topicObserved(
+    "formautofill-storage-changed",
+    (subject, data) =>
+      data == "update" &&
+      subject.wrappedJSObject.collectionName == COLLECTION_NAME
+  );
 
   Assert.notEqual(creditCards[1]["cc-name"], undefined);
   profileStorage.creditCards.update(guid, TEST_CREDIT_CARD_3);
   await onChanged;
   await profileStorage._saveImmediately();
 
   profileStorage = new ProfileStorage(path);
   await profileStorage.initialize();
@@ -416,18 +425,22 @@ add_task(async function test_remove() {
   await prepareTestCreditCards(path);
 
   let profileStorage = new ProfileStorage(path);
   await profileStorage.initialize();
 
   let creditCards = profileStorage.creditCards.getAll();
   let guid = creditCards[1].guid;
 
-  let onChanged = TestUtils.topicObserved("formautofill-storage-changed",
-                                          (subject, data) => data == "remove");
+  let onChanged = TestUtils.topicObserved(
+    "formautofill-storage-changed",
+    (subject, data) =>
+      data == "remove" &&
+      subject.wrappedJSObject.collectionName == COLLECTION_NAME
+  );
 
   Assert.equal(creditCards.length, 2);
 
   profileStorage.creditCards.remove(guid);
   await onChanged;
   await profileStorage._saveImmediately();
 
   profileStorage = new ProfileStorage(path);
@@ -448,17 +461,19 @@ MERGE_TESTCASES.forEach((testcase) => {
                                                   "creditCards");
     let creditCards = profileStorage.creditCards.getAll();
     let guid = creditCards[0].guid;
     let timeLastModified = creditCards[0].timeLastModified;
     // Merge creditCard and verify the guid in notifyObservers subject
     let onMerged = TestUtils.topicObserved(
       "formautofill-storage-changed",
       (subject, data) =>
-        data == "update" && subject.QueryInterface(Ci.nsISupportsString).data == guid
+        data == "update" &&
+        subject.wrappedJSObject.guid == guid &&
+        subject.wrappedJSObject.collectionName == COLLECTION_NAME
     );
     // Force to create sync metadata.
     profileStorage.creditCards.pullSyncChanges();
     Assert.equal(getSyncChangeCounter(profileStorage.creditCards, guid), 1);
     Assert.ok(profileStorage.creditCards.mergeIfPossible(guid, testcase.creditCardToMerge));
     if (!testcase.noNeedToUpdate) {
       await onMerged;
     }
--- a/browser/extensions/formautofill/test/unit/test_reconcile.js
+++ b/browser/extensions/formautofill/test/unit/test_reconcile.js
@@ -1006,17 +1006,24 @@ add_task(async function test_reconcile_t
       for (let updatedRecord of test.local) {
         profileStorage[collectionName].update(test.parent.guid, updatedRecord);
       }
 
       let localRecord = profileStorage[collectionName].get(test.parent.guid, {
         rawData: true,
       });
 
+      let onReconciled = TestUtils.topicObserved(
+        "formautofill-storage-changed",
+        (subject, data) =>
+          data == "reconcile" &&
+          subject.wrappedJSObject.collectionName == collectionName
+      );
       let {forkedGUID} = profileStorage[collectionName].reconcile(test.remote);
+      await onReconciled;
       let reconciledRecord = profileStorage[collectionName].get(test.parent.guid, {
         rawData: true,
       });
       if (forkedGUID) {
         let forkedRecord = profileStorage[collectionName].get(forkedGUID, {
           rawData: true,
         });