Bug 843802 - Don't accept an appType parameter when sideloading apps. r=ferjm
authorFabrice Desré <fabrice@mozilla.com>
Fri, 22 Feb 2013 09:42:34 -0800
changeset 122681 87be4954d11b720827f9dff5aa182fe6db395392
parent 122680 09d5766f58799dff4a11a4a1fab40a2eeb5394aa
child 122682 a5d725fc67d3816a4fa032bb843fd2bf005113c2
push id24356
push usergszorc@mozilla.com
push dateSun, 24 Feb 2013 01:00:12 +0000
treeherdermozilla-central@195e706140d1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersferjm
bugs843802
milestone22.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 843802 - Don't accept an appType parameter when sideloading apps. r=ferjm
b2g/chrome/content/dbg-webapps-actors.js
--- a/b2g/chrome/content/dbg-webapps-actors.js
+++ b/b2g/chrome/content/dbg-webapps-actors.js
@@ -81,174 +81,198 @@ WebappsActor.prototype = {
       { from: this.actorID,
         type: "webappsEvent",
         appId: aId,
         error: "installationFailed",
         message: aMsg
       });
   },
 
-  installHostedApp: function wa_actorInstallHosted(aDir, aId, aType) {
+  _getAppType: function wa_actorGetAppType(aType) {
+    let type = Ci.nsIPrincipal.APP_STATUS_INSTALLED;
+
+    if (aType) {
+      type = aType == "privileged" ? Ci.nsIPrincipal.APP_STATUS_PRIVILEGED
+           : aType == "certified" ? Ci.nsIPrincipal.APP_STATUS_CERTIFIED
+           : Ci.nsIPrincipal.APP_STATUS_INSTALLED;
+    }
+
+    return type;
+  },
+
+  installHostedApp: function wa_actorInstallHosted(aDir, aId) {
     debug("installHostedApp");
     let self = this;
 
     let runnable = {
       run: function run() {
         try {
-          // The destination directory for this app.
-          let installDir = FileUtils.getDir(DIRECTORY_NAME,
-                                            ["webapps", aId], true);
-
           // Move manifest.webapp to the destination directory.
           let manFile = aDir.clone();
           manFile.append("manifest.webapp");
-          manFile.moveTo(installDir, "manifest.webapp");
-
-          // Read the origin and manifest url from metadata.json
-          let metaFile = aDir.clone();
-          metaFile.append("metadata.json");
-          DOMApplicationRegistry._loadJSONAsync(metaFile, function(aMetadata) {
-            if (!aMetadata) {
-              self._sendError("Error Parsing metadata.json", aId);
+          DOMApplicationRegistry._loadJSONAsync(manFile, function(aManifest) {
+            if (!aManifest) {
+              self._sendError("Error Parsing manifest.webapp", aId);
               return;
             }
 
-            if (!aMetadata.origin) {
-              self._sendError("Missing 'origin' propery in metadata.json", aId);
+            let appType = self._getAppType(aManifest.type);
+
+            // In production builds, don't allow installation of certified apps.
+#ifdef MOZ_OFFICIAL
+            if (appType == Ci.nsIPrincipal.APP_STATUS_CERTIFIED) {
+              self._sendError("Installing certified apps is not allowed.", aId);
               return;
             }
+#endif
+            // The destination directory for this app.
+            let installDir = FileUtils.getDir(DIRECTORY_NAME,
+                                              ["webapps", aId], true);
+            manFile.moveTo(installDir, "manifest.webapp");
 
-            let origin = aMetadata.origin;
-            let manifestURL = aMetadata.manifestURL ||
-                              origin + "/manifest.webapp";
-            // Create a fake app object with the minimum set of properties we need.
-            let app = {
-              origin: origin,
-              installOrigin: aMetadata.installOrigin || origin,
-              manifestURL: manifestURL,
-              appStatus: aType
-            }
+            // Read the origin and manifest url from metadata.json
+            let metaFile = aDir.clone();
+            metaFile.append("metadata.json");
+            DOMApplicationRegistry._loadJSONAsync(metaFile, function(aMetadata) {
+              if (!aMetadata) {
+                self._sendError("Error Parsing metadata.json", aId);
+                return;
+              }
+
+              if (!aMetadata.origin) {
+                self._sendError("Missing 'origin' property in metadata.json", aId);
+                return;
+              }
 
-            self._registerApp(app, aId, aDir);
+              let origin = aMetadata.origin;
+              let manifestURL = aMetadata.manifestURL ||
+                                origin + "/manifest.webapp";
+              // Create a fake app object with the minimum set of properties we need.
+              let app = {
+                origin: origin,
+                installOrigin: aMetadata.installOrigin || origin,
+                manifestURL: manifestURL,
+                appStatus: appType
+              };
+
+              self._registerApp(app, aId, aDir);
+            });
           });
         } catch(e) {
           // If anything goes wrong, just send it back.
           self._sendError(e.toString(), aId);
         }
       }
     }
 
     Services.tm.currentThread.dispatch(runnable,
                                        Ci.nsIThread.DISPATCH_NORMAL);
   },
 
-  installPackagedApp: function wa_actorInstallPackaged(aDir, aId, aType) {
+  installPackagedApp: function wa_actorInstallPackaged(aDir, aId) {
     debug("installPackagedApp");
     let self = this;
 
     let runnable = {
       run: function run() {
         try {
           // The destination directory for this app.
           let installDir = FileUtils.getDir(DIRECTORY_NAME,
                                             ["webapps", aId], true);
 
-          // Move application.zip to the destination directory.
+          // Move application.zip to the destination directory, and
+          // extract manifest.webapp there.
           let zipFile = aDir.clone();
           zipFile.append("application.zip");
-          zipFile.moveTo(installDir, "application.zip");
-
-          // Extract the manifest.webapp file from the zip.
-          zipFile = installDir.clone();
-          zipFile.append("application.zip");
           let zipReader = Cc["@mozilla.org/libjar/zip-reader;1"]
                             .createInstance(Ci.nsIZipReader);
           zipReader.open(zipFile);
-
           let manFile = installDir.clone();
           manFile.append("manifest.webapp");
           zipReader.extract("manifest.webapp", manFile);
           zipReader.close();
+          zipFile.moveTo(installDir, "application.zip");
 
-          let origin = "app://" + aId;
+          DOMApplicationRegistry._loadJSONAsync(manFile, function(aManifest) {
+            if (!aManifest) {
+              self._sendError("Error Parsing manifest.webapp", aId);
+            }
+
+            let appType = self._getAppType(aManifest.type);
 
-          // Create a fake app object with the minimum set of properties we need.
-          let app = {
-            origin: origin,
-            installOrigin: origin,
-            manifestURL: origin + "/manifest.webapp",
-            appStatus: aType
-          }
+            // In production builds, don't allow installation of certified apps.
+#ifdef MOZ_OFFICIAL
+            if (appType == Ci.nsIPrincipal.APP_STATUS_CERTIFIED) {
+              self._sendError("Installing certified apps is not allowed.", aId);
+              return;
+            }
+#endif
+            let origin = "app://" + aId;
 
-          self._registerApp(app, aId, aDir);
+            // Create a fake app object with the minimum set of properties we need.
+            let app = {
+              origin: origin,
+              installOrigin: origin,
+              manifestURL: origin + "/manifest.webapp",
+              appStatus: appType
+            }
+
+            self._registerApp(app, aId, aDir);
+          });
         } catch(e) {
           // If anything goes wrong, just send it back.
           self._sendError(e.toString(), aId);
         }
       }
     }
 
     Services.tm.currentThread.dispatch(runnable,
                                        Ci.nsIThread.DISPATCH_NORMAL);
   },
 
   /**
     * @param appId   : The id of the app we want to install. We will look for
     *                  the files for the app in $TMP/b2g/$appId :
     *                  For packaged apps: application.zip
     *                  For hosted apps:   metadata.json and manifest.webapp
-    * @param appType : The privilege status of the app, as defined in
-    *                   nsIPrincipal. It's optional and default to
-    *                   APP_STATUS_INSTALLED
     */
   install: function wa_actorInstall(aRequest) {
     debug("install");
 
     Cu.import("resource://gre/modules/Webapps.jsm");
     Cu.import("resource://gre/modules/AppsUtils.jsm");
     Cu.import("resource://gre/modules/FileUtils.jsm");
 
     let appId = aRequest.appId;
     if (!appId) {
       return { error: "missingParameter",
                message: "missing parameter appId" }
     }
 
-    let appType = aRequest.appType || Ci.nsIPrincipal.APP_STATUS_INSTALLED;
-
     // Check that we are not overriding a preinstalled application.
     let reg = DOMApplicationRegistry;
     if (appId in reg.webapps && reg.webapps[appId].removable === false) {
       return { error: "badParameterType",
                message: "The application " + appId + " can't be overriden."
              }
     }
 
-    // In production builds, don't allow installation of certified apps.
-#ifdef MOZ_OFFICIAL
-    if (appType == Ci.nsIPrincipal.APP_STATUS_CERTIFIED) {
-      return { error: "badParameterType",
-               message: "Installing certified apps is not allowed."
-             }
-    }
-#endif
-
     let appDir = FileUtils.getDir("TmpD", ["b2g", appId], false, false);
 
     if (!appDir || !appDir.exists()) {
       return { error: "badParameterType",
                message: "missing directory " + appDir.path
              }
     }
 
     let testFile = appDir.clone();
     testFile.append("application.zip");
 
     if (testFile.exists()) {
-      this.installPackagedApp(appDir, appId, appType);
+      this.installPackagedApp(appDir, appId);
     } else {
       let missing =
         ["manifest.webapp", "metadata.json"]
         .some(function(aName) {
           testFile = appDir.clone();
           testFile.append(aName);
           return !testFile.exists();
         });
@@ -256,17 +280,17 @@ WebappsActor.prototype = {
       if (missing) {
         try {
           aDir.remove(true);
         } catch(e) {}
         return { error: "badParameterType",
                  message: "hosted app file is missing" }
       }
 
-      this.installHostedApp(appDir, appId, appType);
+      this.installHostedApp(appDir, appId);
     }
 
     return { appId: appId, path: appDir.path }
   }
 };
 
 /**
  * The request types this actor can handle.