Bug 1357205 - create a base class for install locations in addons manager r?aswan draft
authorRobert Helmer <rhelmer@mozilla.com>
Wed, 13 Sep 2017 22:57:05 -0700
changeset 664972 cf531097158283bcbf1b789feda82fad18f9e575
parent 664971 336d00c453b93659d459ca0fc9dbcc4731656c29
child 664973 d9300757ab3f8ac8d7ca5a4d95db24c2617c577c
push id79884
push userbmo:rhelmer@mozilla.com
push dateThu, 14 Sep 2017 18:19:17 +0000
reviewersaswan
bugs1357205
milestone57.0a1
Bug 1357205 - create a base class for install locations in addons manager r?aswan MozReview-Commit-ID: H2YpBjfwaeF
toolkit/mozapps/extensions/internal/XPIProvider.jsm
--- a/toolkit/mozapps/extensions/internal/XPIProvider.jsm
+++ b/toolkit/mozapps/extensions/internal/XPIProvider.jsm
@@ -5946,38 +5946,107 @@ PROP_LOCALE_MULTI.forEach(function(aProp
       });
     }
 
     return results;
   });
 });
 
 /**
+ * Base class for objects that identify an install location for add-ons.
+ * This class isn't instantiated directly, see the derived classes below.
+ */
+class InstallLocation {
+  /**
+   * @param {String} name - The string identifier for the install location.
+   * @param {String} scope - The scope of add-ons installed in this location.
+   */
+  constructor(name, scope) {
+    this.locked = true;
+
+    this._name = name;
+    this._scope = scope;
+  }
+
+  /**
+   * 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(rescan = false) {
+    this._readAddons(rescan);
+
+    let locations = new Map();
+    for (let id in this._IDToFileMap) {
+      locations.set(id, this._IDToFileMap[id].clone());
+    }
+    return locations;
+  }
+
+  installAddon() {
+    logger.warn(`Cannot install add-ons to location: ${this._name}`);
+  }
+
+  uninstallAddon() {
+    logger.warn(`Cannot uninstall add-ons to location: ${this._name}`);
+  }
+
+  getStagingDir() {
+    logger.warn(`Location ${this._name} has no staging directory.`);
+  }
+
+ /**
+  * Used to indicate whether the ID is backed by a real file or if it
+  * is instead a pointer.
+  *
+  * This should be overridden by install locations that are backed by real files.
+  *
+  * @param {String} aId - The ID of the addon
+  * @returns {bool} - true if this
+  */
+  isLinkedAddon(aId) {
+    return true;
+  }
+}
+
+/**
  * An object which identifies a directory install location for add-ons. The
  * location consists of a directory which contains the add-ons installed in the
  * location.
- *
  */
-class DirectoryInstallLocation {
+class DirectoryInstallLocation extends InstallLocation {
   /**
    * Each add-on installed in the location is either a directory containing the
    * add-on's files or a text file containing an absolute path to the directory
    * containing the add-ons files. The directory or text file must have the same
    * name as the add-on's ID.
    *
    * @param  aName
    *         The string identifier for the install location
    * @param  aDirectory
    *         The nsIFile directory for the install location
    * @param  aScope
    *         The scope of add-ons installed in this location
   */
   constructor(aName, aDirectory, aScope) {
+    super(aName, 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);
 
@@ -6101,43 +6170,16 @@ class DirectoryInstallLocation {
       }
 
       this._IDToFileMap[id] = entry;
       XPIProvider._addURIMapping(id, entry);
     }
   }
 
   /**
-   * 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(rescan = false) {
-    this._readAddons(rescan);
-
-    let locations = new Map();
-    for (let id in this._IDToFileMap) {
-      locations.set(id, this._IDToFileMap[id].clone());
-    }
-    return locations;
-  }
-
-  /**
    * Gets the directory that the add-on with the given ID is installed in.
    *
    * @param  aId
    *         The ID of the add-on
    * @return The nsIFile
    * @throws if the ID does not match any of the add-ons installed
    */
   getLocationForID(aId) {
@@ -6923,32 +6965,30 @@ const TemporaryInstallLocation = {
 }
 
 /**
  * An object that identifies a registry install location for add-ons. The location
  * consists of a registry key which contains string values mapping ID to the
  * path where an add-on is installed
  *
  */
-class WinRegInstallLocation extends DirectoryInstallLocation {
+class WinRegInstallLocation extends InstallLocation {
   /**
     * @param  aName
     *         The string identifier of this Install Location.
     * @param  aRootKey
     *         The root key (one of the ROOT_KEY_ values from nsIWindowsRegKey).
     * @param  scope
     *         The scope of add-ons installed in this location
     */
   constructor(aName, aRootKey, aScope) {
-    super(aName, undefined, aScope);
+    super(aName, aScope);
 
     this.locked = true;
-    this._name = aName;
     this._rootKey = aRootKey;
-    this._scope = aScope;
     this._IDToFileMap = {};
 
     let path = this._appKeyPath + "\\Extensions";
     let key = Cc["@mozilla.org/windows-registry-key;1"].
               createInstance(Ci.nsIWindowsRegKey);
 
     // Reading the registry may throw an exception, and that's ok.  In error
     // cases, we just leave ourselves in the empty state.
@@ -6998,30 +7038,16 @@ class WinRegInstallLocation extends Dire
         logger.warn("Ignoring missing add-on in " + file.path);
         continue;
       }
 
       this._IDToFileMap[id] = file;
       XPIProvider._addURIMapping(id, file);
     }
   }
-
-  /**
-   * Gets the name of this install location.
-   */
-  get name() {
-    return this._name;
-  }
-
-  /**
-   * @see DirectoryInstallLocation
-   */
-  isLinkedAddon(aId) {
-    return true;
-  }
 }
 
 this.XPIInternal = {
   AddonInternal,
   BOOTSTRAP_REASONS,
   KEY_APP_SYSTEM_ADDONS,
   KEY_APP_SYSTEM_DEFAULTS,
   KEY_APP_TEMPORARY,