Bug 1314177 - remove redundancy in install location classes r?aswan draft
authorRobert Helmer <rhelmer@mozilla.com>
Fri, 24 Mar 2017 20:15:07 -0700
changeset 551241 f4f15a44a980f87efa2f88428ff2ec4414fde199
parent 551240 66ad0cfd63ba2939238b012f07fdce6278fcb54c
child 621490 111594202867943f6b7fad6d79b9dd5998be4cb2
push id50995
push userrhelmer@mozilla.com
push dateSat, 25 Mar 2017 05:05:48 +0000
reviewersaswan
bugs1314177
milestone55.0a1
Bug 1314177 - remove redundancy in install location classes r?aswan MozReview-Commit-ID: IXIRJ1w0lJa
toolkit/mozapps/extensions/internal/XPIProvider.jsm
--- a/toolkit/mozapps/extensions/internal/XPIProvider.jsm
+++ b/toolkit/mozapps/extensions/internal/XPIProvider.jsm
@@ -8271,17 +8271,16 @@ class MutableDirectoryInstallLocation ex
    *           "copy"
    *           The source files will be copied,
    *           "proxy"
    *           A "proxy file" is going to refer to the source file path
    * @return an nsIFile indicating where the add-on was installed to
    */
   installAddon({ id, source, existingAddonID, action = "move" }) {
     let trashDir = this.getTrashDir();
-
     let transaction = new SafeInstallOperation();
 
     let moveOldAddon = aId => {
       let file = this._directory.clone();
       file.append(aId);
 
       if (file.exists())
         transaction.moveUnder(file, trashDir);
@@ -8470,53 +8469,16 @@ class SystemAddonInstallLocation extends
     if (aResetSet) {
       this.resetAddonSet();
     }
 
     this.locked = false;
   }
 
   /**
-   * Removes the specified files or directories in the staging directory and
-   * then if the staging directory is empty attempts to remove it.
-   *
-   * @param  aLeafNames
-   *         An array of file or directory to remove from the directory, the
-   *         array may be empty
-   */
-  cleanStagingDir(aLeafNames = []) {
-    let dir = this.getStagingDir();
-
-    for (let name of aLeafNames) {
-      let file = dir.clone();
-      file.append(name);
-      recursiveRemove(file);
-    }
-
-    if (this._stagingDirLock > 0)
-      return;
-
-    let dirEntries = dir.directoryEntries.QueryInterface(Ci.nsIDirectoryEnumerator);
-    try {
-      if (dirEntries.nextFile)
-        return;
-    } finally {
-      dirEntries.close();
-    }
-
-    try {
-      setFilePermissions(dir, FileUtils.PERMS_DIRECTORY);
-      dir.remove(false);
-    } catch (e) {
-      logger.warn("Failed to remove staging dir", e);
-      // Failing to remove the staging directory is ignorable
-    }
-  }
-
-  /**
    * Gets the staging directory to put add-ons that are pending install and
    * uninstall into.
    *
    * @return {nsIFile} - staging directory for system add-on upgrades.
    */
   getStagingDir() {
     this._addonSet = SystemAddonInstallLocation._loadAddonSet();
     let dir = null;
@@ -8528,45 +8490,23 @@ class SystemAddonInstallLocation extends
     } else {
       logger.info("SystemAddonInstallLocation directory is missing");
     }
 
     return dir;
   }
 
   requestStagingDir() {
-    this._stagingDirLock++;
-    if (this._stagingDirPromise)
-      return this._stagingDirPromise;
-
     this._addonSet = SystemAddonInstallLocation._loadAddonSet();
     if (this._addonSet.directory) {
       this._directory = this._baseDir.clone();
       this._directory.append(this._addonSet.directory);
     }
 
-    OS.File.makeDir(this._directory.path);
-    let stagepath = OS.Path.join(this._directory.path, DIR_STAGE);
-    return this._stagingDirPromise = OS.File.makeDir(stagepath).then(null, (e) => {
-      if (e instanceof OS.File.Error && e.becauseExists)
-        return;
-      logger.error("Failed to create staging directory", e);
-      throw e;
-    });
-  }
-
-  releaseStagingDir() {
-    this._stagingDirLock--;
-
-    if (this._stagingDirLock == 0) {
-      this._stagingDirPromise = null;
-      this.cleanStagingDir();
-    }
-
-    return Promise.resolve();
+    super.requestStagingDir();
   }
 
   /**
    * Reads the current set of system add-ons
    */
   static _loadAddonSet() {
     try {
       let setStr = Preferences.get(PREF_SYSTEM_ADDON_SET, null);
@@ -8584,17 +8524,17 @@ class SystemAddonInstallLocation extends
   }
 
   /**
    * Saves the current set of system add-ons
    *
    * @param {Object} aAddonSet - object containing schema, directory and set
    *                 of system add-on IDs and versions.
    */
-  _saveAddonSet(aAddonSet) {
+  static _saveAddonSet(aAddonSet) {
     Preferences.set(PREF_SYSTEM_ADDON_SET, JSON.stringify(aAddonSet));
   }
 
   getAddonLocations() {
     // Updated system add-ons are ignored in safe mode
     if (Services.appinfo.inSafeMode) {
       return new Map();
     }
@@ -8669,17 +8609,17 @@ class SystemAddonInstallLocation extends
    * Resets the add-on set so on the next startup the default set will be used.
    */
   resetAddonSet() {
     logger.info("Removing all system add-on upgrades.");
 
     // remove everything from the pref first, if uninstall
     // fails then at least they will not be re-activated on
     // next restart.
-    this._saveAddonSet({ schema: 1, addons: {} });
+    SystemAddonInstallLocation._saveAddonSet({ schema: 1, addons: {} });
 
     // If this is running at app startup, the pref being cleared
     // will cause later stages of startup to notice that the
     // old updates are now gone.
     //
     // Updates will only be explicitly uninstalled if they are
     // removed restartlessly, for instance if they are no longer
     // part of the latest update set.
@@ -8784,17 +8724,17 @@ class SystemAddonInstallLocation extends
           break;
         } catch (e) {
           logger.debug("Could not create new system add-on updates dir, retrying", e);
         }
       }
 
       // Record the new upgrade directory.
       let state = { schema: 1, directory: newDir.leafName, addons: {} };
-      this._saveAddonSet(state);
+      SystemAddonInstallLocation._saveAddonSet(state);
 
       this._nextDir = newDir;
       let location = this;
 
       let installs = [];
       for (let addon of aAddons) {
         let install = yield createLocalInstall(addon._sourceBundle, location);
         installs.push(install);
@@ -8825,31 +8765,31 @@ class SystemAddonInstallLocation extends
         state = { schema: 1, directory: newDir.leafName, addons: {} };
         for (let addon of aAddons) {
           state.addons[addon.id] = {
             version: addon.version
           }
         }
 
         previousState = SystemAddonInstallLocation._loadAddonSet();
-        this._saveAddonSet(state);
+        SystemAddonInstallLocation._saveAddonSet(state);
 
         let blockers = aAddons.filter(
           addon => AddonManagerPrivate.hasUpgradeListener(addon.id)
         );
 
         if (blockers.length > 0) {
           yield waitForAllPromises(installs.map(postponeAddon));
         } else {
           yield waitForAllPromises(installs.map(installAddon));
         }
       } catch (e) {
         // Roll back to previous upgrade set (if present) on restart.
         if (previousState) {
-          this._saveAddonSet(previousState);
+          SystemAddonInstallLocation._saveAddonSet(previousState);
         }
         // Otherwise, roll back to built-in set on restart.
         // TODO try to do these restartlessly
         this.resetAddonSet();
 
         try {
           yield OS.File.removeDir(newDir.path, { ignorePermissions: true });
         } catch (e) {
@@ -8879,41 +8819,16 @@ class SystemAddonInstallLocation extends
         logger.warn("Attempted to resume system add-on install but upgrade blockers are still present");
       } else {
         yield waitForAllPromises(installs.map(resumeAddon));
       }
     })
   }
 
   /**
-   * Returns a directory that is normally on the same filesystem as the rest of
-   * the install location and can be used for temporarily storing files during
-   * safe move operations. Calling this method will delete the existing trash
-   * directory and its contents.
-   *
-   * @return an nsIFile
-   */
-  getTrashDir() {
-    let trashDir = this._directory.clone();
-    trashDir.append(DIR_TRASH);
-    let trashDirExists = trashDir.exists();
-    try {
-      if (trashDirExists)
-        recursiveRemove(trashDir);
-      trashDirExists = false;
-    } catch (e) {
-      logger.warn("Failed to remove trash directory", e);
-    }
-    if (!trashDirExists)
-      trashDir.create(Ci.nsIFile.DIRECTORY_TYPE, FileUtils.PERMS_DIRECTORY);
-
-    return trashDir;
-  }
-
-  /**
    * Installs an add-on into the install location.
    *
    * @param  id
    *         The ID of the add-on to install
    * @param  source
    *         The source nsIFile to install from
    * @return an nsIFile indicating where the add-on was installed to
    */
@@ -9047,41 +8962,16 @@ class WinRegInstallLocation extends Dire
       }
 
       this._IDToFileMap[id] = file;
       XPIProvider._addURIMapping(id, file);
     }
   }
 
   /**
-   * Gets the name of this install location.
-   */
-  get name() {
-    return this._name;
-  }
-
-  /**
-   * Gets the scope of this install location.
-   */
-  get scope() {
-    return this._scope;
-  }
-
-  /**
-   * Gets an array of nsIFiles for add-ons installed in this location.
-   */
-  getAddonLocations() {
-    let locations = new Map();
-    for (let id in this._IDToFileMap) {
-      locations.set(id, this._IDToFileMap[id].clone());
-    }
-    return locations;
-  }
-
-  /**
    * @see DirectoryInstallLocation
    */
   isLinkedAddon(aId) {
     return true;
   }
 }
 
 var addonTypes = [