Bug 1498917 - Make ext-sidebarAction.js consistent with ext-browserAction.js after Bug 1498917. r=mixedpuppy
authorOriol Brufau <oriol-bugzilla@hotmail.com>
Mon, 15 Oct 2018 17:05:15 +0000
changeset 499819 344d0af4e60c610d39e5f314233d605e1e92cfc9
parent 499818 2fd7d3ae070e853281fa49408996ecc9308990a9
child 499820 111455f16f4b52fe6c398fc337d48fe64a29608c
push id1864
push userffxbld-merge
push dateMon, 03 Dec 2018 15:51:40 +0000
treeherdermozilla-release@f040763d99ad [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmixedpuppy
bugs1498917
milestone64.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1498917 - Make ext-sidebarAction.js consistent with ext-browserAction.js after Bug 1498917. r=mixedpuppy Differential Revision: https://phabricator.services.mozilla.com/D8676
browser/components/extensions/parent/ext-sidebarAction.js
--- a/browser/components/extensions/parent/ext-sidebarAction.js
+++ b/browser/components/extensions/parent/ext-sidebarAction.js
@@ -263,81 +263,95 @@ this.sidebarAction = class extends Exten
     } else {
       for (let window of windowTracker.browserWindows()) {
         this.updateWindow(window);
       }
     }
   }
 
   /**
-   * Gets the target object and its associated values corresponding to
-   * the `details` parameter of the various get* and set* API methods.
+   * Gets the target object corresponding to the `details` parameter of the various
+   * get* and set* API methods.
    *
    * @param {Object} details
    *        An object with optional `tabId` or `windowId` properties.
    * @throws if both `tabId` and `windowId` are specified, or if they are invalid.
-   * @returns {Object}
-   *        An object with two properties: `target` and `values`.
-   *        - If a `tabId` was specified, `target` will be the corresponding
-   *          XULElement tab. If a `windowId` was specified, `target` will be
-   *          the corresponding ChromeWindow. Otherwise it will be `null`.
-   *        - `values` will contain the icon, title and panel associated with
-   *          the target.
+   * @returns {XULElement|ChromeWindow|null}
+   *        If a `tabId` was specified, the corresponding XULElement tab.
+   *        If a `windowId` was specified, the corresponding ChromeWindow.
+   *        Otherwise, `null`.
    */
-  getContextData({tabId, windowId}) {
+  getTargetFromDetails({tabId, windowId}) {
     if (tabId != null && windowId != null) {
       throw new ExtensionError("Only one of tabId and windowId can be specified.");
     }
-    let target, values;
     if (tabId != null) {
-      target = tabTracker.getTab(tabId);
-      values = this.tabContext.get(target);
+      return tabTracker.getTab(tabId);
     } else if (windowId != null) {
-      target = windowTracker.getWindow(windowId);
-      values = this.tabContext.get(target);
-    } else {
-      target = null;
-      values = this.globals;
+      return windowTracker.getWindow(windowId);
     }
-    return {target, values};
+    return null;
+  }
+
+  /**
+   * Gets the data associated with a tab, window, or the global one.
+   *
+   * @param {XULElement|ChromeWindow|null} target
+   *        A XULElement tab, a ChromeWindow, or null for the global data.
+   * @returns {Object}
+   *        The icon, title, panel, etc. associated with the target.
+   */
+  getContextData(target) {
+    if (target) {
+      return this.tabContext.get(target);
+    }
+    return this.globals;
   }
 
   /**
    * Set a global, window specific or tab specific property.
    *
-   * @param {Object} details
-   *        An object with optional `tabId` or `windowId` properties.
+   * @param {XULElement|ChromeWindow|null} target
+   *        A XULElement tab, a ChromeWindow, or null for the global data.
    * @param {string} prop
    *        String property to set ["icon", "title", or "panel"].
    * @param {string} value
    *        Value for property.
    */
-  setProperty(details, prop, value) {
-    let {target, values} = this.getContextData(details);
+  setProperty(target, prop, value) {
+    let values = this.getContextData(target);
     if (value === null) {
       delete values[prop];
     } else {
       values[prop] = value;
     }
 
     this.updateOnChange(target);
   }
 
   /**
    * Retrieve the value of a global, window specific or tab specific property.
    *
-   * @param {Object} details
-   *        An object with optional `tabId` or `windowId` properties.
+   * @param {XULElement|ChromeWindow|null} target
+   *        A XULElement tab, a ChromeWindow, or null for the global data.
    * @param {string} prop
    *        String property to retrieve ["icon", "title", or "panel"]
    * @returns {string} value
    *          Value of prop.
    */
-  getProperty(details, prop) {
-    return this.getContextData(details).values[prop];
+  getProperty(target, prop) {
+    return this.getContextData(target)[prop];
+  }
+
+  setPropertyFromDetails(details, prop, value) {
+    return this.setProperty(this.getTargetFromDetails(details), prop, value);
+  }
+
+  getPropertyFromDetails(details, prop) {
+    return this.getProperty(this.getTargetFromDetails(details), prop);
   }
 
   /**
    * Triggers this sidebar action for the given window, with the same effects as
    * if it were toggled via menu or toolbarbutton by a user.
    *
    * @param {ChromeWindow} window
    */
@@ -384,48 +398,48 @@ this.sidebarAction = class extends Exten
 
   getAPI(context) {
     let {extension} = context;
     const sidebarAction = this;
 
     return {
       sidebarAction: {
         async setTitle(details) {
-          sidebarAction.setProperty(details, "title", details.title);
+          sidebarAction.setPropertyFromDetails(details, "title", details.title);
         },
 
         getTitle(details) {
-          return sidebarAction.getProperty(details, "title");
+          return sidebarAction.getPropertyFromDetails(details, "title");
         },
 
         async setIcon(details) {
           let icon = IconDetails.normalize(details, extension, context);
           if (!Object.keys(icon).length) {
             icon = null;
           }
-          sidebarAction.setProperty(details, "icon", icon);
+          sidebarAction.setPropertyFromDetails(details, "icon", icon);
         },
 
         async setPanel(details) {
           let url;
           // Clear the url when given null or empty string.
           if (!details.panel) {
             url = null;
           } else {
             url = context.uri.resolve(details.panel);
             if (!context.checkLoadURL(url)) {
               return Promise.reject({message: `Access denied for URL ${url}`});
             }
           }
 
-          sidebarAction.setProperty(details, "panel", url);
+          sidebarAction.setPropertyFromDetails(details, "panel", url);
         },
 
         getPanel(details) {
-          return sidebarAction.getProperty(details, "panel");
+          return sidebarAction.getPropertyFromDetails(details, "panel");
         },
 
         open() {
           let window = windowTracker.topWindow;
           sidebarAction.open(window);
         },
 
         close() {