Bug 1358846: Part 3 - Trivial cleanups. r?rhelmer draft
authorKris Maglione <maglione.k@gmail.com>
Sat, 22 Apr 2017 18:38:45 -0700
changeset 570712 8bf7ca9a5741fb8128742dfedd3025cfafd11e1a
parent 570711 5e5b623380c036354fc22e82975076d1b5ced3ba
child 570713 8a603c827eca295cbf8ef8fcedb678924274e55a
child 570715 cb5e655044ff0445af50c95d9123c56ac5dbbbc5
push id56555
push usermaglione.k@gmail.com
push dateSat, 29 Apr 2017 22:53:53 +0000
reviewersrhelmer
bugs1358846
milestone55.0a1
Bug 1358846: Part 3 - Trivial cleanups. r?rhelmer MozReview-Commit-ID: Kf3x4uBjiEp
toolkit/mozapps/extensions/internal/XPIProvider.jsm
--- a/toolkit/mozapps/extensions/internal/XPIProvider.jsm
+++ b/toolkit/mozapps/extensions/internal/XPIProvider.jsm
@@ -753,18 +753,17 @@ function canRunInSafeMode(aAddon) {
   // include them here so their uninstall functions get called when switching
   // back to the default set.
 
   // TODO product should make the call about temporary add-ons running
   // in safe mode. assuming for now that they are.
   if (aAddon._installLocation.name == KEY_APP_TEMPORARY)
     return true;
 
-  return aAddon._installLocation.name == KEY_APP_SYSTEM_DEFAULTS ||
-         aAddon._installLocation.name == KEY_APP_SYSTEM_ADDONS;
+  return aAddon._installLocation.isSystem;
 }
 
 /**
  * Calculates whether an add-on should be appDisabled or not.
  *
  * @param  aAddon
  *         The add-on to check
  * @return true if the add-on should not be appDisabled
@@ -2189,17 +2188,18 @@ XPIState.prototype = {
    */
   syncWithDB(aDBAddon, aUpdated = false) {
     logger.debug("Updating XPIState for " + JSON.stringify(aDBAddon));
     // If the add-on changes from disabled to enabled, we should re-check the modified time.
     // If this is a newly found add-on, it won't have an 'enabled' field but we
     // did a full recursive scan in that case, so we don't need to do it again.
     // We don't use aDBAddon.active here because it's not updated until after restart.
     let mustGetMod = (aDBAddon.visible && !aDBAddon.disabled && !this.enabled);
-    this.enabled = (aDBAddon.visible && !aDBAddon.disabled);
+
+    this.enabled = aDBAddon.visible && !aDBAddon.disabled;
     this.version = aDBAddon.version;
     // XXX Eventually also copy bootstrap, etc.
     if (aUpdated || mustGetMod) {
       this.getModTime(new nsIFile(this.descriptor), aDBAddon.id);
       if (this.scanTime != aDBAddon.updateDate) {
         aDBAddon.updateDate = this.scanTime;
         XPIDatabase.saveChanges();
       }
@@ -2232,22 +2232,21 @@ this.XPIStates = {
    * @property {Map<string, XPIState>} sideLoadedAddons
    *        A map of new add-ons detected during install location
    *        directory scans. Keys are add-on IDs, values are XPIState
    *        objects corresponding to those add-ons.
    */
   sideLoadedAddons: new Map(),
 
   get size() {
-    if (!this.db) {
-      return 0;
-    }
     let count = 0;
-    for (let location of this.db.values()) {
-      count += location.size;
+    if (this.db) {
+      for (let location of this.db.values()) {
+        count += location.size;
+      }
     }
     return count;
   },
 
   /**
    * Load extension state data from preferences.
    */
   loadExtensionState() {
@@ -2372,20 +2371,17 @@ this.XPIStates = {
    * Get the XPI state for a specific add-on in a location.
    * If the state is not in our cache, return null.
    * @param aLocation The name of the location where the add-on is installed.
    * @param aId       The add-on ID
    * @return The XPIState entry for the add-on, or null.
    */
   getAddon(aLocation, aId) {
     let location = this.db.get(aLocation);
-    if (!location) {
-      return null;
-    }
-    return location.get(aId);
+    return location && location.get(aId);
   },
 
   /**
    * Find the highest priority location of an add-on by ID and return the
    * location and the XPIState.
    * @param aId   The add-on ID
    * @return [locationName, XPIState] if the add-on is found, [undefined, undefined]
    *         if the add-on is not found.
@@ -2434,24 +2430,23 @@ this.XPIStates = {
   /**
    * Remove the XPIState for an add-on and save the new state.
    * @param aLocation  The name of the add-on location.
    * @param aId        The ID of the add-on.
    */
   removeAddon(aLocation, aId) {
     logger.debug("Removing XPIState for " + aLocation + ":" + aId);
     let location = this.db.get(aLocation);
-    if (!location) {
-      return;
-    }
-    location.delete(aId);
-    if (location.size == 0) {
-      this.db.delete(aLocation);
-    }
-    this.save();
+    if (location) {
+      location.delete(aId);
+      if (location.size == 0) {
+        this.db.delete(aLocation);
+      }
+      this.save();
+    }
   },
 };
 
 const hasOwnProperty = Function.call.bind({}.hasOwnProperty);
 
 this.XPIProvider = {
   get name() {
     return "XPIProvider";
@@ -4566,20 +4561,18 @@ this.XPIProvider = {
       return false;
 
     // The default theme is exempt
     if (aAddon.type == "theme" &&
         aAddon.internalName == XPIProvider.defaultSkin)
       return false;
 
     // System add-ons are exempt
-    let locName = aAddon._installLocation ? aAddon._installLocation.name
-                                          : undefined;
-    if (locName == KEY_APP_SYSTEM_DEFAULTS ||
-        locName == KEY_APP_SYSTEM_ADDONS)
+    let loc = aAddon._installLocation;
+    if (loc && loc.isSystem)
       return false;
 
     if (isAddonPartOfE10SRollout(aAddon)) {
       Preferences.set(PREF_E10S_HAS_NONEXEMPT_ADDON, true);
       return false;
     }
 
     logger.debug("Add-on " + aAddon.id + " blocks e10s rollout.");
@@ -4598,20 +4591,18 @@ this.XPIProvider = {
       return false;
 
     // The hotfix is exempt
     let hotfixID = Preferences.get(PREF_EM_HOTFIX_ID, undefined);
     if (hotfixID && hotfixID == aAddon.id)
       return false;
 
     // System add-ons are exempt
-    let locName = aAddon._installLocation ? aAddon._installLocation.name
-                                          : undefined;
-    if (locName == KEY_APP_SYSTEM_DEFAULTS ||
-        locName == KEY_APP_SYSTEM_ADDONS)
+    let loc = aAddon._installLocation;
+    if (loc && loc.isSystem)
       return false;
 
     return aAddon.bootstrap;
   },
 
   /**
    * In some cases having add-ons active blocks e10s but turning off e10s
    * requires a restart so some add-ons that are normally restartless will
@@ -4879,20 +4870,17 @@ this.XPIProvider = {
                                   metadata: { addonID: aId, URI: uri } });
 
     try {
       // Copy the reason values from the global object into the bootstrap scope.
       for (let name in BOOTSTRAP_REASONS)
         activeAddon.bootstrapScope[name] = BOOTSTRAP_REASONS[name];
 
       // Add other stuff that extensions want.
-      const features = [ "Worker", "ChromeWorker" ];
-
-      for (let feature of features)
-        activeAddon.bootstrapScope[feature] = gGlobalScope[feature];
+      Object.assign(activeAddon.bootstrapScope, {Worker, ChromeWorker});
 
       // Define a console for the add-on
       XPCOMUtils.defineLazyGetter(
         activeAddon.bootstrapScope, "console",
         () => new ConsoleAPI({ consoleID: "addon/" + aId }));
 
       Services.scriptloader.loadSubScript(uri, activeAddon.bootstrapScope);
     } catch (e) {
@@ -7299,18 +7287,17 @@ AddonInternal.prototype = {
       }
     }
 
     // Add-ons that are in locked install locations, or are pending uninstall
     // cannot be upgraded or uninstalled
     if (!this._installLocation.locked && !this.pendingUninstall) {
       // Experiments cannot be upgraded.
       // System add-on upgrades are triggered through a different mechanism (see updateSystemAddons())
-      let isSystem = (this._installLocation.name == KEY_APP_SYSTEM_DEFAULTS ||
-                      this._installLocation.name == KEY_APP_SYSTEM_ADDONS);
+      let isSystem = this._installLocation.isSystem;
       // Add-ons that are installed by a file link cannot be upgraded.
       if (this.type != "experiment" &&
           !this._installLocation.isLinkedAddon(this.id) && !isSystem) {
         permissions |= AddonManager.PERM_CAN_UPGRADE;
       }
 
       permissions |= AddonManager.PERM_CAN_UNINSTALL;
     }
@@ -7667,24 +7654,22 @@ AddonWrapper.prototype = {
     return val;
   },
 
   get hidden() {
     let addon = addonFor(this);
     if (addon._installLocation.name == KEY_APP_TEMPORARY)
       return false;
 
-    return (addon._installLocation.name == KEY_APP_SYSTEM_DEFAULTS ||
-            addon._installLocation.name == KEY_APP_SYSTEM_ADDONS);
+    return addon._installLocation.isSystem;
   },
 
   get isSystem() {
     let addon = addonFor(this);
-    return (addon._installLocation.name == KEY_APP_SYSTEM_DEFAULTS ||
-            addon._installLocation.name == KEY_APP_SYSTEM_ADDONS);
+    return addon._installLocation.isSystem;
   },
 
   // Returns true if Firefox Sync should sync this addon. Only non-hotfixes
   // directly in the profile are considered syncable.
   get isSyncable() {
     let addon = addonFor(this);
     let hotfixID = Preferences.get(PREF_EM_HOTFIX_ID, undefined);
     if (hotfixID && hotfixID == addon.id) {
@@ -8042,16 +8027,19 @@ class DirectoryInstallLocation {
   constructor(aName, aDirectory, aScope) {
     this._name = aName;
     this.locked = true;
     this._directory = aDirectory;
     this._scope = aScope
     this._IDToFileMap = {};
     this._linkedAddons = [];
 
+    this.isSystem = (aName == KEY_APP_SYSTEM_ADDONS ||
+                     aName == KEY_APP_SYSTEM_DEFAULTS);
+
     if (!aDirectory || !aDirectory.exists())
       return;
     if (!aDirectory.isDirectory())
       throw new Error("Location must be a directory.");
 
     this.initialized = false;
   }