diff --git a/b2g/chrome/content/dbg-webapps-actors.js b/b2g/chrome/content/dbg-webapps-actors.js index 4d265dc5006e..81f32b5217bc 100644 --- a/b2g/chrome/content/dbg-webapps-actors.js +++ b/b2g/chrome/content/dbg-webapps-actors.js @@ -86,48 +86,75 @@ WebappsActor.prototype = { }); }, - 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; + } - self._registerApp(app, aId, aDir); + if (!aMetadata.origin) { + self._sendError("Missing 'origin' property in metadata.json", aId); + return; + } + + 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. @@ -140,7 +167,7 @@ WebappsActor.prototype = { Ci.nsIThread.DISPATCH_NORMAL); }, - installPackagedApp: function wa_actorInstallPackaged(aDir, aId, aType) { + installPackagedApp: function wa_actorInstallPackaged(aDir, aId) { debug("installPackagedApp"); let self = this; @@ -151,34 +178,45 @@ WebappsActor.prototype = { 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); + } - // 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 - } + let appType = self._getAppType(aManifest.type); - self._registerApp(app, aId, aDir); + // 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; + + // 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); @@ -195,9 +233,6 @@ WebappsActor.prototype = { * 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"); @@ -212,8 +247,6 @@ WebappsActor.prototype = { 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) { @@ -222,15 +255,6 @@ WebappsActor.prototype = { } } - // 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()) { @@ -243,7 +267,7 @@ WebappsActor.prototype = { testFile.append("application.zip"); if (testFile.exists()) { - this.installPackagedApp(appDir, appId, appType); + this.installPackagedApp(appDir, appId); } else { let missing = ["manifest.webapp", "metadata.json"] @@ -261,7 +285,7 @@ WebappsActor.prototype = { message: "hosted app file is missing" } } - this.installHostedApp(appDir, appId, appType); + this.installHostedApp(appDir, appId); } return { appId: appId, path: appDir.path }