2013-09-05 06:15:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2013-11-04 13:38:29 -08:00
|
|
|
"use strict";
|
2013-09-05 06:15:37 -07:00
|
|
|
|
2013-11-04 13:38:29 -08:00
|
|
|
let {Ci,Cu,CC} = require("chrome");
|
2013-09-05 06:15:37 -07:00
|
|
|
const promise = require("sdk/core/promise");
|
|
|
|
|
|
|
|
const {FileUtils} = Cu.import("resource://gre/modules/FileUtils.jsm");
|
|
|
|
const {Services} = Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
let XMLHttpRequest = CC("@mozilla.org/xmlextras/xmlhttprequest;1");
|
|
|
|
let strings = Services.strings.createBundle("chrome://browser/locale/devtools/app-manager.properties");
|
|
|
|
|
|
|
|
function AppValidator(project) {
|
|
|
|
this.project = project;
|
|
|
|
this.errors = [];
|
|
|
|
this.warnings = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
AppValidator.prototype.error = function (message) {
|
|
|
|
this.errors.push(message);
|
2013-11-04 13:38:29 -08:00
|
|
|
};
|
2013-09-05 06:15:37 -07:00
|
|
|
|
|
|
|
AppValidator.prototype.warning = function (message) {
|
|
|
|
this.warnings.push(message);
|
2013-11-04 13:38:29 -08:00
|
|
|
};
|
2013-09-05 06:15:37 -07:00
|
|
|
|
2013-10-08 22:52:38 -07:00
|
|
|
AppValidator.prototype._getPackagedManifestFile = function () {
|
2013-09-05 06:15:37 -07:00
|
|
|
let manifestFile = FileUtils.File(this.project.location);
|
|
|
|
if (!manifestFile.exists()) {
|
|
|
|
this.error(strings.GetStringFromName("validator.nonExistingFolder"));
|
2013-10-08 22:52:38 -07:00
|
|
|
return null;
|
2013-09-05 06:15:37 -07:00
|
|
|
}
|
|
|
|
if (!manifestFile.isDirectory()) {
|
|
|
|
this.error(strings.GetStringFromName("validator.expectProjectFolder"));
|
2013-10-08 22:52:38 -07:00
|
|
|
return null;
|
2013-09-05 06:15:37 -07:00
|
|
|
}
|
|
|
|
manifestFile.append("manifest.webapp");
|
|
|
|
if (!manifestFile.exists() || !manifestFile.isFile()) {
|
|
|
|
this.error(strings.GetStringFromName("validator.wrongManifestFileName"));
|
2013-10-08 22:52:38 -07:00
|
|
|
return null;
|
2013-09-05 06:15:37 -07:00
|
|
|
}
|
2013-10-08 22:52:38 -07:00
|
|
|
return manifestFile;
|
|
|
|
};
|
2013-09-05 06:15:37 -07:00
|
|
|
|
2013-10-08 22:52:38 -07:00
|
|
|
AppValidator.prototype._getPackagedManifestURL = function () {
|
|
|
|
let manifestFile = this._getPackagedManifestFile();
|
|
|
|
if (!manifestFile) {
|
|
|
|
return null;
|
|
|
|
}
|
2013-09-05 06:15:37 -07:00
|
|
|
return Services.io.newFileURI(manifestFile).spec;
|
2013-10-08 22:52:38 -07:00
|
|
|
};
|
2013-09-05 06:15:37 -07:00
|
|
|
|
|
|
|
AppValidator.prototype._fetchManifest = function (manifestURL) {
|
|
|
|
let deferred = promise.defer();
|
2013-10-17 11:27:04 -07:00
|
|
|
this.manifestURL = manifestURL;
|
2013-09-05 06:15:37 -07:00
|
|
|
|
|
|
|
let req = new XMLHttpRequest();
|
|
|
|
try {
|
|
|
|
req.open("GET", manifestURL, true);
|
|
|
|
} catch(e) {
|
|
|
|
this.error(strings.formatStringFromName("validator.invalidManifestURL", [manifestURL], 1));
|
|
|
|
deferred.resolve(null);
|
|
|
|
return deferred.promise;
|
|
|
|
}
|
2013-10-15 10:28:25 -07:00
|
|
|
req.channel.loadFlags |= Ci.nsIRequest.LOAD_BYPASS_CACHE | Ci.nsIRequest.INHIBIT_CACHING;
|
2013-09-05 06:15:37 -07:00
|
|
|
req.onload = (function () {
|
|
|
|
let manifest = null;
|
|
|
|
try {
|
|
|
|
manifest = JSON.parse(req.responseText);
|
|
|
|
} catch(e) {
|
2013-09-06 06:18:19 -07:00
|
|
|
this.error(strings.formatStringFromName("validator.invalidManifestJSON", [e, manifestURL], 2));
|
2013-09-05 06:15:37 -07:00
|
|
|
}
|
|
|
|
deferred.resolve(manifest);
|
|
|
|
}).bind(this);
|
|
|
|
req.onerror = (function () {
|
|
|
|
this.error(strings.formatStringFromName("validator.noAccessManifestURL", [req.statusText, manifestURL], 2));
|
|
|
|
deferred.resolve(null);
|
|
|
|
}).bind(this);
|
|
|
|
|
|
|
|
try {
|
|
|
|
req.send(null);
|
|
|
|
} catch(e) {
|
|
|
|
this.error(strings.formatStringFromName("validator.noAccessManifestURL", [e, manifestURL], 2));
|
|
|
|
deferred.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
return deferred.promise;
|
2013-11-04 13:38:29 -08:00
|
|
|
};
|
2013-09-05 06:15:37 -07:00
|
|
|
|
|
|
|
AppValidator.prototype._getManifest = function () {
|
|
|
|
let manifestURL;
|
|
|
|
if (this.project.type == "packaged") {
|
|
|
|
manifestURL = this._getPackagedManifestURL();
|
|
|
|
if (!manifestURL)
|
|
|
|
return promise.resolve(null);
|
|
|
|
} else if (this.project.type == "hosted") {
|
|
|
|
manifestURL = this.project.location;
|
|
|
|
try {
|
|
|
|
Services.io.newURI(manifestURL, null, null);
|
|
|
|
} catch(e) {
|
|
|
|
this.error(strings.formatStringFromName("validator.invalidHostedManifestURL", [manifestURL, e.message]));
|
|
|
|
return promise.resolve(null);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.error(strings.formatStringFromName("validator.invalidProjectType", [this.project.type], 1));
|
|
|
|
return promise.resolve(null);
|
|
|
|
}
|
|
|
|
return this._fetchManifest(manifestURL);
|
2013-11-04 13:38:29 -08:00
|
|
|
};
|
2013-09-05 06:15:37 -07:00
|
|
|
|
|
|
|
AppValidator.prototype.validateManifest = function (manifest) {
|
|
|
|
if (!manifest.name) {
|
|
|
|
this.error(strings.GetStringFromName("validator.missNameManifestProperty"));
|
|
|
|
}
|
|
|
|
|
2013-11-04 13:38:29 -08:00
|
|
|
if (!manifest.icons || Object.keys(manifest.icons).length === 0) {
|
2013-09-05 06:15:37 -07:00
|
|
|
this.warning(strings.GetStringFromName("validator.missIconsManifestProperty"));
|
|
|
|
} else if (!manifest.icons["128"]) {
|
2013-09-06 06:18:19 -07:00
|
|
|
this.warning(strings.GetStringFromName("validator.missIconMarketplace"));
|
2013-09-05 06:15:37 -07:00
|
|
|
}
|
2013-11-04 13:38:29 -08:00
|
|
|
};
|
2013-09-05 06:15:37 -07:00
|
|
|
|
2013-11-04 13:38:29 -08:00
|
|
|
AppValidator.prototype._getOriginURL = function () {
|
2013-10-17 11:27:04 -07:00
|
|
|
if (this.project.type == "packaged") {
|
|
|
|
let manifestURL = Services.io.newURI(this.manifestURL, null, null);
|
|
|
|
return Services.io.newURI(".", null, manifestURL).spec;
|
|
|
|
} else if (this.project.type == "hosted") {
|
|
|
|
return Services.io.newURI(this.project.location, null, null).prePath;
|
|
|
|
}
|
2013-11-04 13:38:29 -08:00
|
|
|
};
|
2013-10-17 11:27:04 -07:00
|
|
|
|
|
|
|
AppValidator.prototype.validateLaunchPath = function (manifest) {
|
|
|
|
let deferred = promise.defer();
|
|
|
|
// The launch_path field has to start with a `/`
|
|
|
|
if (manifest.launch_path && manifest.launch_path[0] !== "/") {
|
|
|
|
this.error(strings.formatStringFromName("validator.nonAbsoluteLaunchPath", [manifest.launch_path], 1));
|
|
|
|
deferred.resolve();
|
|
|
|
return deferred.promise;
|
|
|
|
}
|
|
|
|
let origin = this._getOriginURL();
|
|
|
|
let path;
|
|
|
|
if (this.project.type == "packaged") {
|
|
|
|
path = "." + ( manifest.launch_path || "/index.html" );
|
|
|
|
} else if (this.project.type == "hosted") {
|
|
|
|
path = manifest.launch_path || "/";
|
|
|
|
}
|
|
|
|
let indexURL;
|
|
|
|
try {
|
|
|
|
indexURL = Services.io.newURI(path, null, Services.io.newURI(origin, null, null)).spec;
|
|
|
|
} catch(e) {
|
2013-11-05 06:16:47 -08:00
|
|
|
this.error(strings.formatStringFromName("validator.accessFailedLaunchPath", [origin + path], 1));
|
2013-10-17 11:27:04 -07:00
|
|
|
deferred.resolve();
|
|
|
|
return deferred.promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
let req = new XMLHttpRequest();
|
|
|
|
try {
|
|
|
|
req.open("HEAD", indexURL, true);
|
|
|
|
} catch(e) {
|
2013-11-05 06:16:47 -08:00
|
|
|
this.error(strings.formatStringFromName("validator.accessFailedLaunchPath", [indexURL], 1));
|
2013-10-17 11:27:04 -07:00
|
|
|
deferred.resolve();
|
|
|
|
return deferred.promise;
|
|
|
|
}
|
|
|
|
req.channel.loadFlags |= Ci.nsIRequest.LOAD_BYPASS_CACHE | Ci.nsIRequest.INHIBIT_CACHING;
|
|
|
|
req.onload = () => {
|
|
|
|
if (req.status >= 400)
|
2013-11-05 06:16:47 -08:00
|
|
|
this.error(strings.formatStringFromName("validator.accessFailedLaunchPathBadHttpCode", [indexURL, req.status], 2));
|
2013-10-17 11:27:04 -07:00
|
|
|
deferred.resolve();
|
|
|
|
};
|
|
|
|
req.onerror = () => {
|
2013-11-05 06:16:47 -08:00
|
|
|
this.error(strings.formatStringFromName("validator.accessFailedLaunchPath", [indexURL], 1));
|
2013-10-17 11:27:04 -07:00
|
|
|
deferred.resolve();
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
req.send(null);
|
|
|
|
} catch(e) {
|
2013-11-05 06:16:47 -08:00
|
|
|
this.error(strings.formatStringFromName("validator.accessFailedLaunchPath", [indexURL], 1));
|
2013-10-17 11:27:04 -07:00
|
|
|
deferred.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
return deferred.promise;
|
2013-11-04 13:38:29 -08:00
|
|
|
};
|
2013-10-17 11:27:04 -07:00
|
|
|
|
2013-09-05 06:15:37 -07:00
|
|
|
AppValidator.prototype.validateType = function (manifest) {
|
|
|
|
let appType = manifest.type || "web";
|
|
|
|
if (["web", "privileged", "certified"].indexOf(appType) === -1) {
|
|
|
|
this.error(strings.formatStringFromName("validator.invalidAppType", [appType], 1));
|
|
|
|
} else if (this.project.type == "hosted" &&
|
|
|
|
["certified", "privileged"].indexOf(appType) !== -1) {
|
|
|
|
this.error(strings.formatStringFromName("validator.invalidHostedPriviledges", [appType], 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
// certified app are not fully supported on the simulator
|
|
|
|
if (appType === "certified") {
|
|
|
|
this.warning(strings.GetStringFromName("validator.noCertifiedSupport"));
|
|
|
|
}
|
2013-11-04 13:38:29 -08:00
|
|
|
};
|
2013-09-05 06:15:37 -07:00
|
|
|
|
|
|
|
AppValidator.prototype.validate = function () {
|
|
|
|
this.errors = [];
|
|
|
|
this.warnings = [];
|
|
|
|
return this._getManifest().
|
|
|
|
then((function (manifest) {
|
|
|
|
if (manifest) {
|
|
|
|
this.manifest = manifest;
|
|
|
|
this.validateManifest(manifest);
|
|
|
|
this.validateType(manifest);
|
2013-10-17 11:27:04 -07:00
|
|
|
return this.validateLaunchPath(manifest);
|
2013-09-05 06:15:37 -07:00
|
|
|
}
|
|
|
|
}).bind(this));
|
2013-11-04 13:38:29 -08:00
|
|
|
};
|
2013-09-05 06:15:37 -07:00
|
|
|
|
|
|
|
exports.AppValidator = AppValidator;
|