Bug 1617379 - Give WebExtension experiments access to addressBookCache. r=mkmelin DONTBUILD
authorGeoff Lankow <geoff@darktrojan.net>
Tue, 31 Mar 2020 13:58:34 +0300
changeset 38637 36da94e8ddb44309bcdb28ed73970648bd4790dc
parent 38636 1f8b1a573fb2b6f8c18d031063658308da98ba9a
child 38638 3b49df5d969673a9593768d930e76cffdbafe7f5
push id400
push userclokep@gmail.com
push dateMon, 04 May 2020 18:56:09 +0000
reviewersmkmelin
bugs1617379
Bug 1617379 - Give WebExtension experiments access to addressBookCache. r=mkmelin DONTBUILD
mail/components/extensions/parent/ext-mail.js
mail/components/extensions/test/xpcshell/test_ext_experiments.js
mail/components/extensions/test/xpcshell/xpcshell.ini
--- a/mail/components/extensions/parent/ext-mail.js
+++ b/mail/components/extensions/parent/ext-mail.js
@@ -1632,16 +1632,35 @@ extensions.on("startup", (type, extensio
   // eslint-disable-line mozilla/balanced-listeners
   if (extension.hasPermission("accountsRead")) {
     defineLazyGetter(
       extension,
       "folderManager",
       () => new FolderManager(extension)
     );
   }
+  if (extension.hasPermission("addressBooks")) {
+    defineLazyGetter(extension, "addressBookManager", () => {
+      if (!("addressBookCache" in this)) {
+        extensions.loadModule("addressBook");
+      }
+      return {
+        findAddressBookById: this.addressBookCache.findAddressBookById.bind(
+          this.addressBookCache
+        ),
+        findContactById: this.addressBookCache.findContactById.bind(
+          this.addressBookCache
+        ),
+        findMailingListById: this.addressBookCache.findMailingListById.bind(
+          this.addressBookCache
+        ),
+        convert: this.addressBookCache.convert.bind(this.addressBookCache),
+      };
+    });
+  }
   if (extension.hasPermission("messagesRead")) {
     defineLazyGetter(
       extension,
       "messageManager",
       () => new MessageManager(extension)
     );
   }
   defineLazyGetter(extension, "tabManager", () => new TabManager(extension));
--- a/mail/components/extensions/test/xpcshell/test_ext_experiments.js
+++ b/mail/components/extensions/test/xpcshell/test_ext_experiments.js
@@ -53,20 +53,40 @@ add_task(async function test_managers() 
 
       messageList = await browser.messages.continueList(messageList.id);
       browser.test.assertEq(null, messageList.id);
       browser.test.assertEq(1, messageList.messages.length);
       browser.test.assertTrue(
         testMessage.subject != messageList.messages[0].subject
       );
 
+      let [bookUID, contactUID, listUID] = await new Promise(resolve => {
+        browser.test.onMessage.addListener(function listener(...args) {
+          browser.test.onMessage.removeListener(listener);
+          resolve(args);
+        });
+        browser.test.sendMessage("get UIDs");
+      });
+      let [
+        foundBook,
+        foundContact,
+        foundList,
+      ] = await browser.testapi.testCanFindAddressBookItems(
+        bookUID,
+        contactUID,
+        listUID
+      );
+      browser.test.assertEq("new book", foundBook.name);
+      browser.test.assertEq("new contact", foundContact.properties.DisplayName);
+      browser.test.assertEq("new list", foundList.name);
+
       browser.test.notifyPass("finished");
     },
     files: {
-      "schema.json": JSON.stringify([
+      "schema.json": [
         {
           namespace: "testapi",
           functions: [
             {
               name: "testCanGetFolder",
               type: "function",
               async: true,
               parameters: [
@@ -100,65 +120,138 @@ add_task(async function test_managers() 
               parameters: [],
             },
             {
               name: "testCanStartMessageList",
               type: "function",
               async: true,
               parameters: [],
             },
+            {
+              name: "testCanFindAddressBookItems",
+              type: "function",
+              async: true,
+              parameters: [
+                { name: "bookUID", type: "string" },
+                { name: "contactUID", type: "string" },
+                { name: "listUID", type: "string" },
+              ],
+            },
           ],
         },
-      ]),
-      "implementation.js": `
-        var { ExtensionCommon } = ChromeUtils.import("resource://gre/modules/ExtensionCommon.jsm");
-        var { MailServices } = ChromeUtils.import("resource:///modules/MailServices.jsm");
-        var testapi = class extends ExtensionCommon.ExtensionAPI {
+      ],
+      "implementation.js": () => {
+        var { ExtensionCommon } = ChromeUtils.import(
+          "resource://gre/modules/ExtensionCommon.jsm"
+        );
+        var { MailServices } = ChromeUtils.import(
+          "resource:///modules/MailServices.jsm"
+        );
+        this.testapi = class extends ExtensionCommon.ExtensionAPI {
           getAPI(context) {
             return {
               testapi: {
-                async testCanGetFolder({accountId, path}) {
-                  let realFolder = context.extension.folderManager.get(accountId, path);
+                async testCanGetFolder({ accountId, path }) {
+                  let realFolder = context.extension.folderManager.get(
+                    accountId,
+                    path
+                  );
                   return realFolder.getTotalMessages(false);
                 },
                 async testCanConvertFolder() {
-                  let realFolder = [...MailServices.accounts.allFolders.enumerate()].find(f => f.name == "test1");
+                  let realFolder = [
+                    ...MailServices.accounts.allFolders.enumerate(),
+                  ].find(f => f.name == "test1");
                   return context.extension.folderManager.convert(realFolder);
                 },
                 async testCanGetMessage(messageId) {
-                  let realMessage = context.extension.messageManager.get(messageId);
+                  let realMessage = context.extension.messageManager.get(
+                    messageId
+                  );
                   return realMessage.subject;
                 },
                 async testCanConvertMessage() {
-                  let realFolder = [...MailServices.accounts.allFolders.enumerate()].find(f => f.name == "test1");
+                  let realFolder = [
+                    ...MailServices.accounts.allFolders.enumerate(),
+                  ].find(f => f.name == "test1");
                   let realMessage = realFolder.messages.getNext();
                   return context.extension.messageManager.convert(realMessage);
                 },
                 async testCanStartMessageList() {
-                  let realFolder = [...MailServices.accounts.allFolders.enumerate()].find(f => f.name == "test1");
-                  return context.extension.messageManager.startMessageList(realFolder.messages);
+                  let realFolder = [
+                    ...MailServices.accounts.allFolders.enumerate(),
+                  ].find(f => f.name == "test1");
+                  return context.extension.messageManager.startMessageList(
+                    realFolder.messages
+                  );
+                },
+                async testCanFindAddressBookItems(
+                  bookUID,
+                  contactUID,
+                  listUID
+                ) {
+                  let foundBook = context.extension.addressBookManager.findAddressBookById(
+                    bookUID
+                  );
+                  let foundContact = context.extension.addressBookManager.findContactById(
+                    contactUID
+                  );
+                  let foundList = context.extension.addressBookManager.findMailingListById(
+                    listUID
+                  );
+
+                  return [
+                    context.extension.addressBookManager.convert(foundBook),
+                    context.extension.addressBookManager.convert(foundContact),
+                    context.extension.addressBookManager.convert(foundList),
+                  ];
                 },
               },
             };
           }
         };
-      `,
+      },
     },
     manifest: {
-      permissions: ["accountsRead", "messagesRead"],
+      permissions: ["accountsRead", "addressBooks", "messagesRead"],
       experiment_apis: {
         testapi: {
           schema: "schema.json",
           parent: {
             scopes: ["addon_parent"],
             paths: [["testapi"]],
             script: "implementation.js",
           },
         },
       },
     },
   });
+
+  let dirPrefId = MailServices.ab.newAddressBook("new book", "", 101);
+  let book = MailServices.ab.getDirectoryFromId(dirPrefId);
+
+  let contact = Cc["@mozilla.org/addressbook/cardproperty;1"].createInstance(
+    Ci.nsIAbCard
+  );
+  contact.displayName = "new contact";
+  contact.firstName = "new";
+  contact.lastName = "contact";
+  contact.primaryEmail = "new.contact@invalid";
+  contact = book.addCard(contact);
+
+  let list = Cc["@mozilla.org/addressbook/directoryproperty;1"].createInstance(
+    Ci.nsIAbDirectory
+  );
+  list.isMailList = true;
+  list.dirName = "new list";
+  list = book.addMailList(list);
+  list.addCard(contact);
+
   Services.prefs.setIntPref("extensions.webextensions.messagesPerPage", 4);
+
   await extension.startup();
+  await extension.awaitMessage("get UIDs");
+  extension.sendMessage(book.UID, contact.UID, list.UID);
   await extension.awaitFinish("finished");
   await extension.unload();
+
   Services.prefs.clearUserPref("extensions.webextensions.messagesPerPage");
 });
--- a/mail/components/extensions/test/xpcshell/xpcshell.ini
+++ b/mail/components/extensions/test/xpcshell/xpcshell.ini
@@ -4,13 +4,14 @@ tags = webextensions
 
 [test_ext_accounts.js]
 [test_ext_addressBook.js]
 tags = addrbook
 [test_ext_alias.js]
 [test_ext_cloudFile.js]
 support-files = data/cloudFile1.txt data/cloudFile2.txt
 [test_ext_experiments.js]
+tags = addrbook
 [test_ext_folders.js]
 [test_ext_messages.js]
 [test_ext_messages_get.js]
 [test_ext_messages_onNewMailReceived.js]
 [test_ext_messages_query.js]