Bug 1621873 - Enforce mailTabs event/function permissions with the schema, not the code. r=mkmelin
authorGeoff Lankow <geoff@darktrojan.net>
Thu, 12 Mar 2020 12:58:31 +0200
changeset 38476 0ab55e9b87ee6a6fda6fb628fbb82148ede7961e
parent 38475 4af2fc631df9d4eb1709b8d72872f3702737048a
child 38477 ef40d5b3cb731b7bd9e93a5ccaf34f4782d6f020
push id400
push userclokep@gmail.com
push dateMon, 04 May 2020 18:56:09 +0000
reviewersmkmelin
bugs1621873
Bug 1621873 - Enforce mailTabs event/function permissions with the schema, not the code. r=mkmelin
mail/components/extensions/parent/ext-mailTabs.js
mail/components/extensions/schemas/mailTabs.json
--- a/mail/components/extensions/parent/ext-mailTabs.js
+++ b/mail/components/extensions/parent/ext-mailTabs.js
@@ -119,32 +119,16 @@ var uiListener = new (class extends Even
     this.listenerCount--;
     if (this.listenerCount == 0) {
       windowTracker.removeListener("select", this);
       this.lastSelected = new WeakMap();
     }
   }
 })();
 
-class PermissionedEventManager extends EventManager {
-  constructor({ permission, context, name, register }) {
-    super({ context, name, register });
-    this.permission = permission;
-  }
-  addListener(callback) {
-    let { extension } = this.context;
-    if (!extension.hasPermission(this.permission)) {
-      throw new ExtensionError(
-        `The "${this.permission}" permission is required to use ${this.name}.`
-      );
-    }
-    return super.addListener(callback);
-  }
-}
-
 this.mailTabs = class extends ExtensionAPI {
   getAPI(context) {
     let { extension } = context;
     let { tabManager } = extension;
 
     /**
      * Gets the tab for the given tab id, or the active tab if the id is null.
      *
@@ -279,22 +263,16 @@ this.mailTabs = class extends ExtensionA
                 // Prevent the messagePane from showing if a message is selected.
                 nativeTab.folderDisplay._aboutToSelectMessage = true;
               }
             }
           }
         },
 
         async getSelectedMessages(tabId) {
-          if (!extension.hasPermission("messagesRead")) {
-            throw new ExtensionError(
-              `The "messagesRead" permission is required to use mailTabs.getSelectedMessages.`
-            );
-          }
-
           let tab = getTabOrActive(tabId);
           let { folderDisplay } = tab.nativeTab;
           let messageList = folderDisplay.view.dbView.getSelectedMsgHdrs();
           return messageListTracker.startList(messageList, extension);
         },
 
         async setQuickFilter(tabId, state) {
           let tab = getTabOrActive(tabId);
@@ -363,36 +341,34 @@ this.mailTabs = class extends ExtensionA
             window.QuickFilterBarMuxer.reflectFiltererState(
               filterer,
               window.gFolderDisplay
             );
           }
           // Inactive tabs are updated when they become active, except the search doesn't. :(
         },
 
-        onDisplayedFolderChanged: new PermissionedEventManager({
-          permission: "accountsRead",
+        onDisplayedFolderChanged: new EventManager({
           context,
           name: "mailTabs.onDisplayedFolderChanged",
           register: fire => {
             let listener = (event, tab, folder) => {
               fire.sync(tabTracker.getId(tab), convertFolder(folder));
             };
 
             uiListener.on("folder-changed", listener);
             uiListener.incrementListeners();
             return () => {
               uiListener.off("folder-changed", listener);
               uiListener.decrementListeners();
             };
           },
         }).api(),
 
-        onSelectedMessagesChanged: new PermissionedEventManager({
-          permission: "messagesRead",
+        onSelectedMessagesChanged: new EventManager({
           context,
           name: "mailTabs.onSelectedMessagesChanged",
           register: fire => {
             let listener = (event, tab, messages) => {
               fire.sync(
                 tabTracker.getId(tab),
                 messageListTracker.startList(messages, extension)
               );
--- a/mail/components/extensions/schemas/mailTabs.json
+++ b/mail/components/extensions/schemas/mailTabs.json
@@ -151,16 +151,17 @@
             }
           }
         ]
       },
       {
         "name": "getSelectedMessages",
         "type": "function",
         "description": "Lists the selected messages in the current folder. A messages permission is required to do this.",
+        "permissions": ["messagesRead"],
         "async": true,
         "parameters": [
           {
             "name": "tabId",
             "type": "integer",
             "description": "Defaults to the active tab of the current window.",
             "optional": true,
             "minimum": 1
@@ -231,19 +232,21 @@
         ]
       }
     ],
     "events": [
       {
         "name": "onDisplayedFolderChanged",
         "type": "function",
         "description": "Fired when the displayed folder changes in any mail tab.",
+        "permissions": ["accountsRead"],
         "parameters": []
       },
       {
         "name": "onSelectedMessagesChanged",
         "type": "function",
         "description": "Fired when the selected messages change in any mail tab.",
+        "permissions": ["messagesRead"],
         "parameters": []
       }
     ]
   }
 ]