Bug 1168783 - Expose principal in mozIApplication. r=fabrice

This commit is contained in:
Fernando Jimenez 2015-06-08 14:02:09 +02:00
parent ff60a314e0
commit 55f60ad983
4 changed files with 99 additions and 6 deletions

View File

@ -49,14 +49,16 @@ this.mozIApplication = function(aApp) {
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 principal = this.principal;
if (this.installerIsBrowser) {
let uri = Services.io.newURI(this.origin, null, null);
principal =
Services.scriptSecurityManager.getAppCodebasePrincipal(uri, this.localId,
/*mozbrowser*/false);
}
let perm = Services.perms.testExactPermissionFromPrincipal(principal,
aPermission);
return (perm === Ci.nsIPermissionManager.ALLOW_ACTION);
@ -71,6 +73,26 @@ mozIApplication.prototype = {
return this.widgetPages.find(equalCriterion) !== undefined;
},
get principal() {
if (this._principal) {
return this._principal;
}
this._principal = null;
try {
this._principal = Services.scriptSecurityManager.getAppCodebasePrincipal(
Services.io.newURI(this.origin, null, null),
this.localId,
this.installerIsBrowser
);
} catch(e) {
dump("Could not create app principal " + e + "\n");
}
return this._principal;
},
QueryInterface: function(aIID) {
if (aIID.equals(Ci.mozIApplication) ||
aIID.equals(Ci.nsISupports))

View File

@ -0,0 +1,65 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
const {interfaces: Ci, utils: Cu} = Components;
Cu.import("resource:///modules/AppsUtils.jsm");
add_test(() => {
let app = {
name: "TestApp",
csp: "aCsp",
installOrigin: "http://installorigin.com",
origin: "http://www.example.com",
installTime: Date.now(),
manifestURL: "http://www.example.com/manifest.webapp",
appStatus: Ci.nsIPrincipal.APP_STATUS_NOT_INSTALLED,
removable: false,
id: 123,
localId: 123,
basePath: "/",
progress: 1.0,
installState: "installed",
downloadAvailable: false,
downloading: false,
lastUpdateCheck: Date.now(),
updateTime: Date.now(),
etag: "aEtag",
packageEtag: "aPackageEtag",
manifestHash: "aManifestHash",
packageHash: "aPackageHash",
staged: false,
installerAppId: 345,
installerIsBrowser: false,
storeId: "aStoreId",
storeVersion: 1,
role: "aRole",
redirects: "aRedirects",
kind: "aKind",
enabled: true,
sideloaded: false
};
let mozapp = new mozIApplication(app);
Object.keys(app).forEach((key) => {
if (key == "principal") {
return;
}
Assert.equal(app[key], mozapp[key],
"app[" + key + "] should be equal to mozapp[" + key + "]");
});
Assert.ok(mozapp.principal, "app principal should exist");
let expectedPrincipalOrigin = app.origin + "!appId=" + app.localId;
Assert.equal(mozapp.principal.origin, expectedPrincipalOrigin,
"app principal origin ok");
Assert.equal(mozapp.principal.appId, app.localId, "app principal appId ok");
Assert.equal(mozapp.principal.isInBrowserElement, app.installerIsBrowser,
"app principal isInBrowserElement ok");
run_next_test();
});
function run_test() {
run_next_test();
}

View File

@ -6,3 +6,4 @@ tail =
[test_inter_app_comm_service.js]
[test_manifestSanitizer.js]
[test_manifestHelper.js]
[test_moziapplication.js]

View File

@ -7,11 +7,13 @@
#include "domstubs.idl"
interface nsIPrincipal;
/**
* We expose Gecko-internal helpers related to "web apps" through this
* sub-interface.
*/
[scriptable, uuid(1d856b11-ac29-47d3-bd52-a86e3d45fcf5)]
[scriptable, uuid(e76aa5e0-80b2-404f-bccc-1067828bb6ed)]
interface mozIApplication: nsISupports
{
/* Return true if this app has |permission|. */
@ -58,4 +60,7 @@ interface mozIApplication: nsISupports
/* Returns the kind of the app. */
readonly attribute DOMString kind;
/* Returns the app's principal */
readonly attribute nsIPrincipal principal;
};