2012-08-28 06:43:57 +04: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/. */
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const Cu = Components.utils;
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cr = Components.results;
|
|
|
|
|
2013-09-13 16:32:47 +04:00
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
2014-03-25 17:14:06 +04:00
|
|
|
Cu.import("resource://gre/modules/Promise.jsm");
|
2012-08-28 06:43:57 +04:00
|
|
|
|
2014-04-15 18:45:06 +04:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "FileUtils",
|
|
|
|
"resource://gre/modules/FileUtils.jsm");
|
|
|
|
|
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "WebappOSUtils",
|
|
|
|
"resource://gre/modules/WebappOSUtils.jsm");
|
2014-03-25 17:14:06 +04:00
|
|
|
|
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "NetUtil",
|
|
|
|
"resource://gre/modules/NetUtil.jsm");
|
2012-12-07 23:20:15 +04:00
|
|
|
|
2012-09-23 04:48:26 +04:00
|
|
|
// Shared code for AppsServiceChild.jsm, Webapps.jsm and Webapps.js
|
2012-08-28 06:43:57 +04:00
|
|
|
|
2014-07-31 01:00:15 +04:00
|
|
|
this.EXPORTED_SYMBOLS =
|
|
|
|
["AppsUtils", "ManifestHelper", "isAbsoluteURI", "mozIApplication"];
|
2012-08-28 06:43:57 +04:00
|
|
|
|
|
|
|
function debug(s) {
|
|
|
|
//dump("-*- AppsUtils.jsm: " + s + "\n");
|
|
|
|
}
|
|
|
|
|
2013-05-14 23:00:09 +04:00
|
|
|
this.isAbsoluteURI = function(aURI) {
|
2013-01-09 03:50:01 +04:00
|
|
|
let foo = Services.io.newURI("http://foo", null, null);
|
|
|
|
let bar = Services.io.newURI("http://bar", null, null);
|
|
|
|
return Services.io.newURI(aURI, null, foo).prePath != foo.prePath ||
|
|
|
|
Services.io.newURI(aURI, null, bar).prePath != bar.prePath;
|
|
|
|
}
|
|
|
|
|
2014-04-21 19:30:15 +04:00
|
|
|
this.mozIApplication = function(aApp) {
|
|
|
|
_setAppProperties(this, aApp);
|
2013-02-07 03:21:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
mozIApplication.prototype = {
|
|
|
|
hasPermission: function(aPermission) {
|
|
|
|
let uri = Services.io.newURI(this.origin, null, null);
|
|
|
|
let secMan = Cc["@mozilla.org/scriptsecuritymanager;1"]
|
|
|
|
.getService(Ci.nsIScriptSecurityManager);
|
|
|
|
// This helper checks an URI inside |aApp|'s origin and part of |aApp| has a
|
|
|
|
// specific permission. It is not checking if browsers inside |aApp| have such
|
|
|
|
// permission.
|
|
|
|
let principal = secMan.getAppCodebasePrincipal(uri, this.localId,
|
|
|
|
/*mozbrowser*/false);
|
|
|
|
let perm = Services.perms.testExactPermissionFromPrincipal(principal,
|
|
|
|
aPermission);
|
|
|
|
return (perm === Ci.nsIPermissionManager.ALLOW_ACTION);
|
|
|
|
},
|
|
|
|
|
2014-08-19 18:14:08 +04:00
|
|
|
hasWidgetPage: function(aPageURL) {
|
|
|
|
return this.widgetPages.indexOf(aPageURL) != -1;
|
|
|
|
},
|
|
|
|
|
2013-02-07 03:21:15 +04:00
|
|
|
QueryInterface: function(aIID) {
|
2013-10-09 02:52:26 +04:00
|
|
|
if (aIID.equals(Ci.mozIApplication) ||
|
2013-02-07 03:21:15 +04:00
|
|
|
aIID.equals(Ci.nsISupports))
|
|
|
|
return this;
|
|
|
|
throw Cr.NS_ERROR_NO_INTERFACE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-21 19:30:15 +04:00
|
|
|
function _setAppProperties(aObj, aApp) {
|
|
|
|
aObj.name = aApp.name;
|
|
|
|
aObj.csp = aApp.csp;
|
|
|
|
aObj.installOrigin = aApp.installOrigin;
|
|
|
|
aObj.origin = aApp.origin;
|
2014-06-10 21:37:38 +04:00
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
2014-04-21 19:30:15 +04:00
|
|
|
aObj.apkPackageName = aApp.apkPackageName;
|
2014-01-09 22:18:55 +04:00
|
|
|
#endif
|
2014-04-21 19:30:15 +04:00
|
|
|
aObj.receipts = aApp.receipts ? JSON.parse(JSON.stringify(aApp.receipts)) : null;
|
|
|
|
aObj.installTime = aApp.installTime;
|
|
|
|
aObj.manifestURL = aApp.manifestURL;
|
|
|
|
aObj.appStatus = aApp.appStatus;
|
|
|
|
aObj.removable = aApp.removable;
|
|
|
|
aObj.id = aApp.id;
|
|
|
|
aObj.localId = aApp.localId;
|
|
|
|
aObj.basePath = aApp.basePath;
|
|
|
|
aObj.progress = aApp.progress || 0.0;
|
|
|
|
aObj.installState = aApp.installState || "installed";
|
|
|
|
aObj.downloadAvailable = aApp.downloadAvailable;
|
|
|
|
aObj.downloading = aApp.downloading;
|
|
|
|
aObj.readyToApplyDownload = aApp.readyToApplyDownload;
|
|
|
|
aObj.downloadSize = aApp.downloadSize || 0;
|
|
|
|
aObj.lastUpdateCheck = aApp.lastUpdateCheck;
|
|
|
|
aObj.updateTime = aApp.updateTime;
|
|
|
|
aObj.etag = aApp.etag;
|
|
|
|
aObj.packageEtag = aApp.packageEtag;
|
|
|
|
aObj.manifestHash = aApp.manifestHash;
|
|
|
|
aObj.packageHash = aApp.packageHash;
|
|
|
|
aObj.staged = aApp.staged;
|
|
|
|
aObj.installerAppId = aApp.installerAppId || Ci.nsIScriptSecurityManager.NO_APP_ID;
|
|
|
|
aObj.installerIsBrowser = !!aApp.installerIsBrowser;
|
|
|
|
aObj.storeId = aApp.storeId || "";
|
|
|
|
aObj.storeVersion = aApp.storeVersion || 0;
|
|
|
|
aObj.role = aApp.role || "";
|
|
|
|
aObj.redirects = aApp.redirects;
|
2014-08-19 18:14:08 +04:00
|
|
|
aObj.widgetPages = aApp.widgetPages || [];
|
2014-08-01 21:41:52 +04:00
|
|
|
aObj.kind = aApp.kind;
|
2014-04-21 19:30:15 +04:00
|
|
|
}
|
2012-08-28 06:43:57 +04:00
|
|
|
|
2014-04-21 19:30:15 +04:00
|
|
|
this.AppsUtils = {
|
|
|
|
// Clones a app, without the manifest.
|
|
|
|
cloneAppObject: function(aApp) {
|
|
|
|
let obj = {};
|
|
|
|
_setAppProperties(obj, aApp);
|
|
|
|
return obj;
|
2012-08-28 06:43:57 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
getAppByManifestURL: function getAppByManifestURL(aApps, aManifestURL) {
|
|
|
|
debug("getAppByManifestURL " + aManifestURL);
|
|
|
|
// This could be O(1) if |webapps| was a dictionary indexed on manifestURL
|
|
|
|
// which should be the unique app identifier.
|
|
|
|
// It's currently O(n).
|
|
|
|
for (let id in aApps) {
|
|
|
|
let app = aApps[id];
|
|
|
|
if (app.manifestURL == aManifestURL) {
|
2014-04-21 19:30:15 +04:00
|
|
|
return new mozIApplication(app);
|
2012-08-28 06:43:57 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
2014-05-01 13:23:00 +04:00
|
|
|
getManifestFor: function getManifestFor(aManifestURL) {
|
|
|
|
debug("getManifestFor(" + aManifestURL + ")");
|
|
|
|
return DOMApplicationRegistry.getManifestFor(aManifestURL);
|
|
|
|
},
|
|
|
|
|
2012-08-28 06:43:57 +04:00
|
|
|
getAppLocalIdByManifestURL: function getAppLocalIdByManifestURL(aApps, aManifestURL) {
|
|
|
|
debug("getAppLocalIdByManifestURL " + aManifestURL);
|
|
|
|
for (let id in aApps) {
|
|
|
|
if (aApps[id].manifestURL == aManifestURL) {
|
|
|
|
return aApps[id].localId;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Ci.nsIScriptSecurityManager.NO_APP_ID;
|
|
|
|
},
|
|
|
|
|
2013-01-25 13:57:14 +04:00
|
|
|
getAppLocalIdByStoreId: function(aApps, aStoreId) {
|
|
|
|
debug("getAppLocalIdByStoreId:" + aStoreId);
|
|
|
|
for (let id in aApps) {
|
|
|
|
if (aApps[id].storeId == aStoreId) {
|
|
|
|
return aApps[id].localId;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Ci.nsIScriptSecurityManager.NO_APP_ID;
|
|
|
|
},
|
|
|
|
|
2014-09-17 00:14:02 +04:00
|
|
|
getManifestCSPByLocalId: function getManifestCSPByLocalId(aApps, aLocalId) {
|
|
|
|
debug("getManifestCSPByLocalId " + aLocalId);
|
2012-10-19 14:43:17 +04:00
|
|
|
for (let id in aApps) {
|
|
|
|
let app = aApps[id];
|
|
|
|
if (app.localId == aLocalId) {
|
2014-09-17 00:14:02 +04:00
|
|
|
return ( app.csp || "" );
|
2012-10-19 14:43:17 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
},
|
|
|
|
|
2014-09-17 00:14:02 +04:00
|
|
|
getDefaultCSPByLocalId: function(aApps, aLocalId) {
|
|
|
|
debug("getDefaultCSPByLocalId " + aLocalId);
|
|
|
|
for (let id in aApps) {
|
|
|
|
let app = aApps[id];
|
|
|
|
if (app.localId == aLocalId) {
|
|
|
|
// Use the app kind and the app status to choose the right default CSP.
|
|
|
|
try {
|
|
|
|
switch (app.appStatus) {
|
|
|
|
case Ci.nsIPrincipal.APP_STATUS_CERTIFIED:
|
|
|
|
return Services.prefs.getCharPref("security.apps.certified.CSP.default");
|
|
|
|
break;
|
|
|
|
case Ci.nsIPrincipal.APP_STATUS_PRIVILEGED:
|
|
|
|
return Services.prefs.getCharPref("security.apps.privileged.CSP.default");
|
|
|
|
break;
|
|
|
|
case Ci.nsIPrincipal.APP_STATUS_INSTALLED:
|
|
|
|
return app.kind == "hosted-trusted"
|
|
|
|
? Services.prefs.getCharPref("security.apps.trusted.CSP.default")
|
|
|
|
: "";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} catch(e) {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "default-src 'self'; object-src 'none'";
|
|
|
|
},
|
|
|
|
|
2012-08-28 06:43:57 +04:00
|
|
|
getAppByLocalId: function getAppByLocalId(aApps, aLocalId) {
|
|
|
|
debug("getAppByLocalId " + aLocalId);
|
|
|
|
for (let id in aApps) {
|
|
|
|
let app = aApps[id];
|
|
|
|
if (app.localId == aLocalId) {
|
2014-04-21 19:30:15 +04:00
|
|
|
return new mozIApplication(app);
|
2012-08-28 06:43:57 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
|
|
|
getManifestURLByLocalId: function getManifestURLByLocalId(aApps, aLocalId) {
|
|
|
|
debug("getManifestURLByLocalId " + aLocalId);
|
|
|
|
for (let id in aApps) {
|
|
|
|
let app = aApps[id];
|
|
|
|
if (app.localId == aLocalId) {
|
|
|
|
return app.manifestURL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
2012-08-31 18:34:28 +04:00
|
|
|
},
|
|
|
|
|
2013-03-15 18:18:58 +04:00
|
|
|
getCoreAppsBasePath: function getCoreAppsBasePath() {
|
|
|
|
debug("getCoreAppsBasePath()");
|
|
|
|
try {
|
|
|
|
return FileUtils.getDir("coreAppsDir", ["webapps"], false).path;
|
|
|
|
} catch(e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
getAppInfo: function getAppInfo(aApps, aAppId) {
|
2013-09-13 16:32:47 +04:00
|
|
|
let app = aApps[aAppId];
|
|
|
|
|
|
|
|
if (!app) {
|
2013-03-15 18:18:58 +04:00
|
|
|
debug("No webapp for " + aAppId);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We can have 3rd party apps that are non-removable,
|
|
|
|
// so we can't use the 'removable' property for isCoreApp
|
|
|
|
// Instead, we check if the app is installed under /system/b2g
|
|
|
|
let isCoreApp = false;
|
2013-09-13 16:32:47 +04:00
|
|
|
|
2013-03-15 18:18:58 +04:00
|
|
|
#ifdef MOZ_WIDGET_GONK
|
|
|
|
isCoreApp = app.basePath == this.getCoreAppsBasePath();
|
|
|
|
#endif
|
2013-08-16 22:08:03 +04:00
|
|
|
debug(app.basePath + " isCoreApp: " + isCoreApp);
|
2014-06-26 07:17:25 +04:00
|
|
|
|
|
|
|
// Before bug 910473, this is a temporary workaround to get correct path
|
|
|
|
// from child process in mochitest.
|
|
|
|
let prefName = "dom.mozApps.auto_confirm_install";
|
|
|
|
if (Services.prefs.prefHasUserValue(prefName) &&
|
|
|
|
Services.prefs.getBoolPref(prefName)) {
|
|
|
|
return { "path": app.basePath + "/" + app.id,
|
|
|
|
"isCoreApp": isCoreApp };
|
|
|
|
}
|
|
|
|
|
2014-03-16 01:37:37 +04:00
|
|
|
return { "path": WebappOSUtils.getPackagePath(app),
|
2013-03-15 18:18:58 +04:00
|
|
|
"isCoreApp": isCoreApp };
|
|
|
|
},
|
|
|
|
|
2013-04-18 14:18:30 +04:00
|
|
|
/**
|
|
|
|
* Remove potential HTML tags from displayable fields in the manifest.
|
|
|
|
* We check name, description, developer name, and permission description
|
|
|
|
*/
|
|
|
|
sanitizeManifest: function(aManifest) {
|
|
|
|
let sanitizer = Cc["@mozilla.org/parserutils;1"]
|
|
|
|
.getService(Ci.nsIParserUtils);
|
|
|
|
if (!sanitizer) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
function sanitize(aStr) {
|
|
|
|
return sanitizer.convertToPlainText(aStr,
|
|
|
|
Ci.nsIDocumentEncoder.OutputRaw, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
function sanitizeEntryPoint(aRoot) {
|
|
|
|
aRoot.name = sanitize(aRoot.name);
|
|
|
|
|
|
|
|
if (aRoot.description) {
|
|
|
|
aRoot.description = sanitize(aRoot.description);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aRoot.developer && aRoot.developer.name) {
|
|
|
|
aRoot.developer.name = sanitize(aRoot.developer.name);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aRoot.permissions) {
|
|
|
|
for (let permission in aRoot.permissions) {
|
|
|
|
if (aRoot.permissions[permission].description) {
|
|
|
|
aRoot.permissions[permission].description =
|
|
|
|
sanitize(aRoot.permissions[permission].description);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// First process the main section, then the entry points.
|
|
|
|
sanitizeEntryPoint(aManifest);
|
|
|
|
|
|
|
|
if (aManifest.entry_points) {
|
|
|
|
for (let entry in aManifest.entry_points) {
|
|
|
|
sanitizeEntryPoint(aManifest.entry_points[entry]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-09-23 04:48:26 +04:00
|
|
|
/**
|
2013-02-15 22:13:02 +04:00
|
|
|
* From https://developer.mozilla.org/en/OpenWebApps/The_Manifest
|
|
|
|
* Only the name property is mandatory.
|
2012-09-23 04:48:26 +04:00
|
|
|
*/
|
2013-01-11 18:08:12 +04:00
|
|
|
checkManifest: function(aManifest, app) {
|
2012-09-23 04:48:26 +04:00
|
|
|
if (aManifest.name == undefined)
|
|
|
|
return false;
|
|
|
|
|
2013-04-18 14:18:30 +04:00
|
|
|
this.sanitizeManifest(aManifest);
|
|
|
|
|
2013-01-09 03:50:01 +04:00
|
|
|
// launch_path, entry_points launch paths, message hrefs, and activity hrefs can't be absolute
|
|
|
|
if (aManifest.launch_path && isAbsoluteURI(aManifest.launch_path))
|
2012-09-23 04:48:26 +04:00
|
|
|
return false;
|
|
|
|
|
|
|
|
function checkAbsoluteEntryPoints(entryPoints) {
|
|
|
|
for (let name in entryPoints) {
|
2013-01-09 03:50:01 +04:00
|
|
|
if (entryPoints[name].launch_path && isAbsoluteURI(entryPoints[name].launch_path)) {
|
2012-09-23 04:48:26 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (checkAbsoluteEntryPoints(aManifest.entry_points))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (let localeName in aManifest.locales) {
|
|
|
|
if (checkAbsoluteEntryPoints(aManifest.locales[localeName].entry_points)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-09 03:50:01 +04:00
|
|
|
if (aManifest.activities) {
|
|
|
|
for (let activityName in aManifest.activities) {
|
|
|
|
let activity = aManifest.activities[activityName];
|
|
|
|
if (activity.href && isAbsoluteURI(activity.href)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// |messages| is an array of items, where each item is either a string or
|
|
|
|
// a {name: href} object.
|
|
|
|
let messages = aManifest.messages;
|
|
|
|
if (messages) {
|
|
|
|
if (!Array.isArray(messages)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (let item of aManifest.messages) {
|
|
|
|
if (typeof item == "object") {
|
|
|
|
let keys = Object.keys(item);
|
|
|
|
if (keys.length != 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (isAbsoluteURI(item[keys[0]])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-15 22:13:02 +04:00
|
|
|
// The 'size' field must be a positive integer.
|
|
|
|
if (aManifest.size) {
|
|
|
|
aManifest.size = parseInt(aManifest.size);
|
|
|
|
if (Number.isNaN(aManifest.size) || aManifest.size < 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-11 16:15:48 +04:00
|
|
|
// The 'role' field must be a string.
|
|
|
|
if (aManifest.role && (typeof aManifest.role !== "string")) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-09-23 04:48:26 +04:00
|
|
|
return true;
|
2012-10-03 09:38:06 +04:00
|
|
|
},
|
|
|
|
|
2012-11-30 02:52:02 +04:00
|
|
|
checkManifestContentType: function
|
2012-12-07 23:20:15 +04:00
|
|
|
checkManifestContentType(aInstallOrigin, aWebappOrigin, aContentType) {
|
|
|
|
let hadCharset = { };
|
|
|
|
let charset = { };
|
2014-04-15 18:45:06 +04:00
|
|
|
let netutil = Cc["@mozilla.org/network/util;1"].getService(Ci.nsINetUtil);
|
|
|
|
let contentType = netutil.parseContentType(aContentType, charset, hadCharset);
|
2012-12-07 23:20:15 +04:00
|
|
|
if (aInstallOrigin != aWebappOrigin &&
|
2012-11-30 02:52:02 +04:00
|
|
|
contentType != "application/x-web-app-manifest+json") {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
2014-08-29 04:20:27 +04:00
|
|
|
/**
|
|
|
|
* Checks if the app role is allowed.
|
|
|
|
* Only certified apps can be themes.
|
|
|
|
* @param aRole : the role assigned to this app.
|
|
|
|
* @param aStatus : the APP_STATUS_* for this app.
|
|
|
|
*/
|
|
|
|
checkAppRole: function(aRole, aStatus) {
|
|
|
|
if (aRole == "theme" && aStatus !== Ci.nsIPrincipal.APP_STATUS_CERTIFIED) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
2013-01-11 18:08:12 +04:00
|
|
|
/**
|
|
|
|
* Method to apply modifications to webapp manifests file saved internally.
|
|
|
|
* For now, only ensure app can't rename itself.
|
|
|
|
*/
|
2013-02-14 21:23:58 +04:00
|
|
|
ensureSameAppName: function ensureSameAppName(aOldManifest, aNewManifest, aApp) {
|
2013-01-11 18:08:12 +04:00
|
|
|
// Ensure that app name can't be updated
|
2013-02-14 21:23:58 +04:00
|
|
|
aNewManifest.name = aApp.name;
|
2013-01-11 18:08:12 +04:00
|
|
|
|
|
|
|
// Nor through localized names
|
2013-02-14 21:23:58 +04:00
|
|
|
if ('locales' in aNewManifest) {
|
2014-07-31 01:00:15 +04:00
|
|
|
let defaultName =
|
|
|
|
new ManifestHelper(aOldManifest, aApp.origin, aApp.manifestURL).name;
|
2013-02-14 21:23:58 +04:00
|
|
|
for (let locale in aNewManifest.locales) {
|
|
|
|
let entry = aNewManifest.locales[locale];
|
2013-01-11 18:08:12 +04:00
|
|
|
if (!entry.name) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// In case previous manifest didn't had a name,
|
|
|
|
// we use the default app name
|
|
|
|
let localizedName = defaultName;
|
2013-02-14 21:23:58 +04:00
|
|
|
if (aOldManifest && 'locales' in aOldManifest &&
|
|
|
|
locale in aOldManifest.locales) {
|
|
|
|
localizedName = aOldManifest.locales[locale].name;
|
2013-01-11 18:08:12 +04:00
|
|
|
}
|
|
|
|
entry.name = localizedName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-10-03 09:38:06 +04:00
|
|
|
/**
|
2012-11-19 02:43:38 +04:00
|
|
|
* Determines whether the manifest allows installs for the given origin.
|
|
|
|
* @param object aManifest
|
|
|
|
* @param string aInstallOrigin
|
|
|
|
* @return boolean
|
|
|
|
**/
|
|
|
|
checkInstallAllowed: function checkInstallAllowed(aManifest, aInstallOrigin) {
|
|
|
|
if (!aManifest.installs_allowed_from) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function cbCheckAllowedOrigin(aOrigin) {
|
|
|
|
return aOrigin == "*" || aOrigin == aInstallOrigin;
|
|
|
|
}
|
|
|
|
|
|
|
|
return aManifest.installs_allowed_from.some(cbCheckAllowedOrigin);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine the type of app (app, privileged, certified)
|
|
|
|
* that is installed by the manifest
|
|
|
|
* @param object aManifest
|
|
|
|
* @returns integer
|
|
|
|
**/
|
2012-10-03 09:38:06 +04:00
|
|
|
getAppManifestStatus: function getAppManifestStatus(aManifest) {
|
|
|
|
let type = aManifest.type || "web";
|
|
|
|
|
|
|
|
switch(type) {
|
|
|
|
case "web":
|
2014-09-17 00:14:02 +04:00
|
|
|
case "trusted":
|
2012-10-03 09:38:06 +04:00
|
|
|
return Ci.nsIPrincipal.APP_STATUS_INSTALLED;
|
|
|
|
case "privileged":
|
|
|
|
return Ci.nsIPrincipal.APP_STATUS_PRIVILEGED;
|
|
|
|
case "certified":
|
|
|
|
return Ci.nsIPrincipal.APP_STATUS_CERTIFIED;
|
|
|
|
default:
|
|
|
|
throw new Error("Webapps.jsm: Undetermined app manifest type");
|
|
|
|
}
|
2012-10-10 20:16:49 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines if an update or a factory reset occured.
|
|
|
|
*/
|
|
|
|
isFirstRun: function isFirstRun(aPrefBranch) {
|
|
|
|
let savedmstone = null;
|
|
|
|
try {
|
|
|
|
savedmstone = aPrefBranch.getCharPref("gecko.mstone");
|
|
|
|
} catch (e) {}
|
|
|
|
|
|
|
|
let mstone = Services.appinfo.platformVersion;
|
|
|
|
|
|
|
|
let savedBuildID = null;
|
|
|
|
try {
|
|
|
|
savedBuildID = aPrefBranch.getCharPref("gecko.buildID");
|
|
|
|
} catch (e) {}
|
|
|
|
|
|
|
|
let buildID = Services.appinfo.platformBuildID;
|
|
|
|
|
|
|
|
aPrefBranch.setCharPref("gecko.mstone", mstone);
|
|
|
|
aPrefBranch.setCharPref("gecko.buildID", buildID);
|
|
|
|
|
|
|
|
return ((mstone != savedmstone) || (buildID != savedBuildID));
|
|
|
|
},
|
2012-12-19 23:37:13 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if two manifests have the same set of properties and that the
|
|
|
|
* values of these properties are the same, in each locale.
|
|
|
|
* Manifests here are raw json ones.
|
|
|
|
*/
|
|
|
|
compareManifests: function compareManifests(aManifest1, aManifest2) {
|
|
|
|
// 1. check if we have the same locales in both manifests.
|
|
|
|
let locales1 = [];
|
|
|
|
let locales2 = [];
|
|
|
|
if (aManifest1.locales) {
|
|
|
|
for (let locale in aManifest1.locales) {
|
|
|
|
locales1.push(locale);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (aManifest2.locales) {
|
|
|
|
for (let locale in aManifest2.locales) {
|
|
|
|
locales2.push(locale);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (locales1.sort().join() !== locales2.sort().join()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper function to check the app name and developer information for
|
|
|
|
// two given roots.
|
|
|
|
let checkNameAndDev = function(aRoot1, aRoot2) {
|
|
|
|
let name1 = aRoot1.name;
|
|
|
|
let name2 = aRoot2.name;
|
|
|
|
if (name1 !== name2) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
let dev1 = aRoot1.developer;
|
|
|
|
let dev2 = aRoot2.developer;
|
|
|
|
if ((dev1 && !dev2) || (dev2 && !dev1)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-01-11 09:47:00 +04:00
|
|
|
return (!dev1 && !dev2) ||
|
|
|
|
(dev1.name === dev2.name && dev1.url === dev2.url);
|
2012-12-19 23:37:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// 2. For each locale, check if the name and dev info are the same.
|
|
|
|
if (!checkNameAndDev(aManifest1, aManifest2)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let locale in aManifest1.locales) {
|
|
|
|
if (!checkNameAndDev(aManifest1.locales[locale],
|
|
|
|
aManifest2.locales[locale])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nothing failed.
|
|
|
|
return true;
|
2013-08-02 04:00:22 +04:00
|
|
|
},
|
|
|
|
|
2014-03-25 17:14:06 +04:00
|
|
|
// Asynchronously loads a JSON file. aPath is a string representing the path
|
2013-07-31 23:34:19 +04:00
|
|
|
// of the file to be read.
|
2014-03-25 17:14:06 +04:00
|
|
|
loadJSONAsync: function(aPath) {
|
|
|
|
let deferred = Promise.defer();
|
|
|
|
|
|
|
|
try {
|
|
|
|
let file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
|
|
|
|
file.initWithPath(aPath);
|
|
|
|
|
|
|
|
let channel = NetUtil.newChannel(file);
|
|
|
|
channel.contentType = "application/json";
|
|
|
|
|
|
|
|
NetUtil.asyncFetch(channel, function(aStream, aResult) {
|
|
|
|
if (!Components.isSuccessCode(aResult)) {
|
|
|
|
deferred.resolve(null);
|
|
|
|
|
|
|
|
if (aResult == Cr.NS_ERROR_FILE_NOT_FOUND) {
|
|
|
|
// We expect this under certain circumstances, like for webapps.json
|
|
|
|
// on firstrun, so we return early without reporting an error.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Cu.reportError("AppsUtils: Could not read from json file " + aPath);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
// Obtain a converter to read from a UTF-8 encoded input stream.
|
|
|
|
let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"]
|
|
|
|
.createInstance(Ci.nsIScriptableUnicodeConverter);
|
|
|
|
converter.charset = "UTF-8";
|
|
|
|
|
|
|
|
// Read json file into a string
|
|
|
|
let data = JSON.parse(converter.ConvertToUnicode(NetUtil.readInputStreamToString(aStream,
|
|
|
|
aStream.available()) || ""));
|
|
|
|
aStream.close();
|
|
|
|
|
|
|
|
deferred.resolve(data);
|
|
|
|
} catch (ex) {
|
|
|
|
Cu.reportError("AppsUtils: Could not parse JSON: " +
|
|
|
|
aPath + " " + ex + "\n" + ex.stack);
|
|
|
|
deferred.resolve(null);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} catch (ex) {
|
|
|
|
Cu.reportError("AppsUtils: Could not read from " +
|
|
|
|
aPath + " : " + ex + "\n" + ex.stack);
|
|
|
|
deferred.resolve(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
return deferred.promise;
|
2013-07-31 23:34:19 +04:00
|
|
|
},
|
|
|
|
|
2013-08-02 04:00:22 +04:00
|
|
|
// Returns the MD5 hash of a string.
|
|
|
|
computeHash: function(aString) {
|
|
|
|
let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"]
|
|
|
|
.createInstance(Ci.nsIScriptableUnicodeConverter);
|
|
|
|
converter.charset = "UTF-8";
|
|
|
|
let result = {};
|
|
|
|
// Data is an array of bytes.
|
|
|
|
let data = converter.convertToByteArray(aString, result);
|
|
|
|
|
|
|
|
let hasher = Cc["@mozilla.org/security/hash;1"]
|
|
|
|
.createInstance(Ci.nsICryptoHash);
|
|
|
|
hasher.init(hasher.MD5);
|
|
|
|
hasher.update(data, data.length);
|
|
|
|
// We're passing false to get the binary hash and not base64.
|
|
|
|
let hash = hasher.finish(false);
|
|
|
|
|
|
|
|
function toHexString(charCode) {
|
|
|
|
return ("0" + charCode.toString(16)).slice(-2);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert the binary hash data to a hex string.
|
|
|
|
return [toHexString(hash.charCodeAt(i)) for (i in hash)].join("");
|
2012-12-19 23:37:13 +04:00
|
|
|
}
|
2012-08-28 06:43:57 +04:00
|
|
|
}
|
2012-10-03 09:38:03 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper object to access manifest information with locale support
|
|
|
|
*/
|
2014-07-31 01:00:15 +04:00
|
|
|
this.ManifestHelper = function(aManifest, aOrigin, aManifestURL) {
|
|
|
|
// If the app is packaged, we resolve uris against the origin.
|
|
|
|
// If it's not, against the manifest url.
|
|
|
|
|
|
|
|
if (!aOrigin || !aManifestURL) {
|
|
|
|
throw Error("ManifestHelper needs both origin and manifestURL");
|
|
|
|
}
|
|
|
|
|
|
|
|
this._baseURI = Services.io.newURI(
|
|
|
|
aOrigin.startsWith("app://") ? aOrigin : aManifestURL, null, null);
|
|
|
|
|
|
|
|
// We keep the manifest url in all cases since we need it to
|
|
|
|
// resolve the package path for packaged apps.
|
|
|
|
this._manifestURL = Services.io.newURI(aManifestURL, null, null);
|
|
|
|
|
2012-10-03 09:38:03 +04:00
|
|
|
this._manifest = aManifest;
|
2014-07-31 01:00:15 +04:00
|
|
|
let chrome = Cc["@mozilla.org/chrome/chrome-registry;1"]
|
|
|
|
.getService(Ci.nsIXULChromeRegistry)
|
|
|
|
.QueryInterface(Ci.nsIToolkitChromeRegistry);
|
2012-11-15 14:30:48 +04:00
|
|
|
let locale = chrome.getSelectedLocale("global").toLowerCase();
|
2012-10-03 09:38:03 +04:00
|
|
|
this._localeRoot = this._manifest;
|
|
|
|
|
|
|
|
if (this._manifest.locales && this._manifest.locales[locale]) {
|
|
|
|
this._localeRoot = this._manifest.locales[locale];
|
|
|
|
}
|
|
|
|
else if (this._manifest.locales) {
|
|
|
|
// try with the language part of the locale ("en" for en-GB) only
|
|
|
|
let lang = locale.split('-')[0];
|
|
|
|
if (lang != locale && this._manifest.locales[lang])
|
|
|
|
this._localeRoot = this._manifest.locales[lang];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ManifestHelper.prototype = {
|
|
|
|
_localeProp: function(aProp) {
|
|
|
|
if (this._localeRoot[aProp] != undefined)
|
|
|
|
return this._localeRoot[aProp];
|
|
|
|
return this._manifest[aProp];
|
|
|
|
},
|
|
|
|
|
|
|
|
get name() {
|
|
|
|
return this._localeProp("name");
|
|
|
|
},
|
|
|
|
|
|
|
|
get description() {
|
|
|
|
return this._localeProp("description");
|
|
|
|
},
|
|
|
|
|
2013-08-16 13:48:37 +04:00
|
|
|
get type() {
|
|
|
|
return this._localeProp("type");
|
|
|
|
},
|
|
|
|
|
2012-10-03 09:38:03 +04:00
|
|
|
get version() {
|
|
|
|
return this._localeProp("version");
|
|
|
|
},
|
|
|
|
|
|
|
|
get launch_path() {
|
|
|
|
return this._localeProp("launch_path");
|
|
|
|
},
|
|
|
|
|
|
|
|
get developer() {
|
2013-01-11 09:47:00 +04:00
|
|
|
// Default to {} in order to avoid exception in code
|
|
|
|
// that doesn't check for null `developer`
|
|
|
|
return this._localeProp("developer") || {};
|
2012-10-03 09:38:03 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
get icons() {
|
|
|
|
return this._localeProp("icons");
|
|
|
|
},
|
|
|
|
|
|
|
|
get appcache_path() {
|
|
|
|
return this._localeProp("appcache_path");
|
|
|
|
},
|
|
|
|
|
|
|
|
get orientation() {
|
|
|
|
return this._localeProp("orientation");
|
|
|
|
},
|
|
|
|
|
|
|
|
get package_path() {
|
|
|
|
return this._localeProp("package_path");
|
|
|
|
},
|
|
|
|
|
2014-08-19 18:14:08 +04:00
|
|
|
get widgetPages() {
|
|
|
|
return this._localeProp("widgetPages");
|
|
|
|
},
|
|
|
|
|
2012-10-03 09:38:03 +04:00
|
|
|
get size() {
|
|
|
|
return this._manifest["size"] || 0;
|
|
|
|
},
|
|
|
|
|
|
|
|
get permissions() {
|
|
|
|
if (this._manifest.permissions) {
|
|
|
|
return this._manifest.permissions;
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
},
|
|
|
|
|
2014-01-22 12:50:02 +04:00
|
|
|
get biggestIconURL() {
|
|
|
|
let icons = this._localeProp("icons");
|
2014-01-22 13:02:47 +04:00
|
|
|
if (!icons) {
|
2014-01-22 12:50:02 +04:00
|
|
|
return null;
|
2014-01-22 13:02:47 +04:00
|
|
|
}
|
2014-01-22 12:50:02 +04:00
|
|
|
|
|
|
|
let iconSizes = Object.keys(icons);
|
2014-01-22 13:02:47 +04:00
|
|
|
if (iconSizes.length == 0) {
|
2014-01-22 12:50:02 +04:00
|
|
|
return null;
|
2014-01-22 13:02:47 +04:00
|
|
|
}
|
2014-01-22 12:50:02 +04:00
|
|
|
|
|
|
|
iconSizes.sort((a, b) => a - b);
|
|
|
|
let biggestIconSize = iconSizes.pop();
|
|
|
|
let biggestIcon = icons[biggestIconSize];
|
2014-07-31 01:00:15 +04:00
|
|
|
let biggestIconURL = this._baseURI.resolve(biggestIcon);
|
2014-01-22 12:50:02 +04:00
|
|
|
|
|
|
|
return biggestIconURL;
|
|
|
|
},
|
|
|
|
|
2012-10-03 09:38:03 +04:00
|
|
|
iconURLForSize: function(aSize) {
|
|
|
|
let icons = this._localeProp("icons");
|
|
|
|
if (!icons)
|
|
|
|
return null;
|
|
|
|
let dist = 100000;
|
|
|
|
let icon = null;
|
|
|
|
for (let size in icons) {
|
|
|
|
let iSize = parseInt(size);
|
|
|
|
if (Math.abs(iSize - aSize) < dist) {
|
2014-07-31 01:00:15 +04:00
|
|
|
icon = this._baseURI.resolve(icons[size]);
|
2012-10-03 09:38:03 +04:00
|
|
|
dist = Math.abs(iSize - aSize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return icon;
|
|
|
|
},
|
|
|
|
|
|
|
|
fullLaunchPath: function(aStartPoint) {
|
|
|
|
// If no start point is specified, we use the root launch path.
|
|
|
|
// In all error cases, we just return null.
|
|
|
|
if ((aStartPoint || "") === "") {
|
2014-07-31 01:00:15 +04:00
|
|
|
return this._baseURI.resolve(this._localeProp("launch_path") || "/");
|
2012-10-03 09:38:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Search for the l10n entry_points property.
|
|
|
|
let entryPoints = this._localeProp("entry_points");
|
|
|
|
if (!entryPoints) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entryPoints[aStartPoint]) {
|
2014-07-31 01:00:15 +04:00
|
|
|
return this._baseURI.resolve(entryPoints[aStartPoint].launch_path || "/");
|
2012-10-03 09:38:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
2014-07-31 01:00:15 +04:00
|
|
|
resolveURL: function(aURI) {
|
2013-01-09 03:50:01 +04:00
|
|
|
// This should be enforced higher up, but check it here just in case.
|
2013-01-09 11:35:52 +04:00
|
|
|
if (isAbsoluteURI(aURI)) {
|
2014-07-31 01:00:15 +04:00
|
|
|
throw new Error("Webapps.jsm: non-relative URI passed to resolve");
|
2013-01-09 03:50:01 +04:00
|
|
|
}
|
2014-07-31 01:00:15 +04:00
|
|
|
return this._baseURI.resolve(aURI);
|
2012-10-03 09:38:03 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
fullAppcachePath: function() {
|
|
|
|
let appcachePath = this._localeProp("appcache_path");
|
2014-07-31 01:00:15 +04:00
|
|
|
return this._baseURI.resolve(appcachePath ? appcachePath : "/");
|
2012-10-03 09:38:03 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
fullPackagePath: function() {
|
|
|
|
let packagePath = this._localeProp("package_path");
|
2014-07-31 01:00:15 +04:00
|
|
|
return this._manifestURL.resolve(packagePath ? packagePath : "/");
|
2013-09-11 16:15:48 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
get role() {
|
|
|
|
return this._manifest.role || "";
|
2012-10-03 09:38:03 +04:00
|
|
|
}
|
|
|
|
}
|