2012-09-19 11:19:00 -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/. */
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2012-10-23 13:11:02 -07:00
|
|
|
function debug(s) {
|
|
|
|
//dump("-*- PermissionSettings Module: " + s + "\n");
|
|
|
|
}
|
2012-09-19 11:19:00 -07:00
|
|
|
|
2012-09-28 15:16:29 -07:00
|
|
|
const Cu = Components.utils;
|
2012-09-19 11:19:00 -07:00
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
|
2012-10-31 01:35:11 -07:00
|
|
|
let EXPORTED_SYMBOLS = ["PermissionSettingsModule"];
|
2012-09-19 11:19:00 -07:00
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
|
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "ppmm",
|
|
|
|
"@mozilla.org/parentprocessmessagemanager;1",
|
|
|
|
"nsIMessageListenerManager");
|
|
|
|
|
2012-10-23 13:11:02 -07:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this,
|
|
|
|
"permissionManager",
|
|
|
|
"@mozilla.org/permissionmanager;1",
|
|
|
|
"nsIPermissionManager");
|
|
|
|
|
|
|
|
XPCOMUtils.defineLazyServiceGetter(this,
|
|
|
|
"secMan",
|
|
|
|
"@mozilla.org/scriptsecuritymanager;1",
|
|
|
|
"nsIScriptSecurityManager");
|
|
|
|
|
|
|
|
XPCOMUtils.defineLazyServiceGetter(this,
|
|
|
|
"appsService",
|
|
|
|
"@mozilla.org/AppsService;1",
|
|
|
|
"nsIAppsService");
|
2012-09-19 11:19:00 -07:00
|
|
|
|
2012-10-31 01:35:11 -07:00
|
|
|
let PermissionSettingsModule = {
|
2012-09-19 11:19:00 -07:00
|
|
|
init: function() {
|
|
|
|
debug("Init");
|
|
|
|
ppmm.addMessageListener("PermissionSettings:AddPermission", this);
|
|
|
|
Services.obs.addObserver(this, "profile-before-change", false);
|
|
|
|
},
|
|
|
|
|
|
|
|
addPermission: function(aData, aCallbacks) {
|
|
|
|
let uri = Services.io.newURI(aData.origin, null, null);
|
|
|
|
let appID = appsService.getAppLocalIdByManifestURL(aData.manifestURL);
|
|
|
|
let principal = secMan.getAppCodebasePrincipal(uri, appID, aData.browserFlag);
|
|
|
|
|
|
|
|
let action;
|
|
|
|
switch (aData.value)
|
|
|
|
{
|
|
|
|
case "unknown":
|
|
|
|
action = Ci.nsIPermissionManager.UNKNOWN_ACTION;
|
|
|
|
break;
|
|
|
|
case "allow":
|
|
|
|
action = Ci.nsIPermissionManager.ALLOW_ACTION;
|
|
|
|
break;
|
|
|
|
case "deny":
|
|
|
|
action = Ci.nsIPermissionManager.DENY_ACTION;
|
|
|
|
break;
|
|
|
|
case "prompt":
|
|
|
|
action = Ci.nsIPermissionManager.PROMPT_ACTION;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
dump("Unsupported PermisionSettings Action: " + aData.value +"\n");
|
|
|
|
action = Ci.nsIPermissionManager.UNKNOWN_ACTION;
|
|
|
|
}
|
|
|
|
debug("add: " + aData.origin + " " + appID + " " + action);
|
|
|
|
permissionManager.addFromPrincipal(principal, aData.type, action);
|
|
|
|
},
|
|
|
|
|
2012-10-23 13:11:02 -07:00
|
|
|
getPermission: function getPermission(aPermission, aManifestURL, aOrigin, aBrowserFlag) {
|
|
|
|
debug("getPermission: " + aPermission + ", " + aManifestURL + ", " + aOrigin);
|
|
|
|
let uri = Services.io.newURI(aOrigin, null, null);
|
|
|
|
let appID = appsService.getAppLocalIdByManifestURL(aManifestURL);
|
|
|
|
let principal = secMan.getAppCodebasePrincipal(uri, appID, aBrowserFlag);
|
|
|
|
let result = permissionManager.testExactPermissionFromPrincipal(principal, aPermission);
|
|
|
|
|
|
|
|
switch (result)
|
|
|
|
{
|
|
|
|
case Ci.nsIPermissionManager.UNKNOWN_ACTION:
|
|
|
|
return "unknown";
|
|
|
|
case Ci.nsIPermissionManager.ALLOW_ACTION:
|
|
|
|
return "allow";
|
|
|
|
case Ci.nsIPermissionManager.DENY_ACTION:
|
|
|
|
return "deny";
|
|
|
|
case Ci.nsIPermissionManager.PROMPT_ACTION:
|
|
|
|
return "prompt";
|
|
|
|
default:
|
|
|
|
dump("Unsupported PermissionSettings Action!\n");
|
|
|
|
return "unknown";
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-09-19 11:19:00 -07:00
|
|
|
observe: function(aSubject, aTopic, aData) {
|
|
|
|
ppmm.removeMessageListener("PermissionSettings:AddPermission", this);
|
|
|
|
Services.obs.removeObserver(this, "profile-before-change");
|
|
|
|
ppmm = null;
|
|
|
|
},
|
|
|
|
|
|
|
|
receiveMessage: function(aMessage) {
|
|
|
|
debug("PermissionSettings::receiveMessage " + aMessage.name);
|
|
|
|
let mm = aMessage.target;
|
|
|
|
let msg = aMessage.data;
|
|
|
|
|
|
|
|
let result;
|
|
|
|
switch (aMessage.name) {
|
|
|
|
case "PermissionSettings:AddPermission":
|
|
|
|
this.addPermission(msg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PermissionSettingsModule.init();
|