2013-07-02 04:42:48 +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/. */
|
|
|
|
|
|
|
|
this.EXPORTED_SYMBOLS = [ "SitePermissions" ];
|
|
|
|
|
|
|
|
Components.utils.import("resource://gre/modules/Services.jsm");
|
|
|
|
|
2015-09-15 21:19:45 +03:00
|
|
|
var gStringBundle =
|
2013-07-02 04:42:48 +04:00
|
|
|
Services.strings.createBundle("chrome://browser/locale/sitePermissions.properties");
|
|
|
|
|
|
|
|
this.SitePermissions = {
|
|
|
|
|
|
|
|
UNKNOWN: Services.perms.UNKNOWN_ACTION,
|
|
|
|
ALLOW: Services.perms.ALLOW_ACTION,
|
|
|
|
BLOCK: Services.perms.DENY_ACTION,
|
|
|
|
SESSION: Components.interfaces.nsICookiePermission.ACCESS_SESSION,
|
|
|
|
|
|
|
|
/* Checks whether a UI for managing permissions should be exposed for a given
|
|
|
|
* URI. This excludes file URIs, for instance, as they don't have a host,
|
|
|
|
* even though nsIPermissionManager can still handle them.
|
|
|
|
*/
|
|
|
|
isSupportedURI: function (aURI) {
|
|
|
|
return aURI.schemeIs("http") || aURI.schemeIs("https");
|
|
|
|
},
|
|
|
|
|
|
|
|
/* Returns an array of all permission IDs.
|
|
|
|
*/
|
2015-09-10 01:19:34 +03:00
|
|
|
listPermissions: function () {
|
2013-07-17 00:12:48 +04:00
|
|
|
let array = Object.keys(gPermissionObject);
|
|
|
|
array.sort((a, b) => {
|
|
|
|
return this.getPermissionLabel(a).localeCompare(this.getPermissionLabel(b));
|
|
|
|
});
|
|
|
|
return array;
|
2013-07-02 04:42:48 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/* Returns an array of permission states to be exposed to the user for a
|
|
|
|
* permission with the given ID.
|
|
|
|
*/
|
|
|
|
getAvailableStates: function (aPermissionID) {
|
2013-07-09 16:44:41 +04:00
|
|
|
if (aPermissionID in gPermissionObject &&
|
|
|
|
gPermissionObject[aPermissionID].states)
|
|
|
|
return gPermissionObject[aPermissionID].states;
|
|
|
|
|
|
|
|
if (this.getDefault(aPermissionID) == this.UNKNOWN)
|
|
|
|
return [ SitePermissions.UNKNOWN, SitePermissions.ALLOW, SitePermissions.BLOCK ];
|
|
|
|
|
|
|
|
return [ SitePermissions.ALLOW, SitePermissions.BLOCK ];
|
2013-07-02 04:42:48 +04:00
|
|
|
},
|
|
|
|
|
2013-07-09 16:44:41 +04:00
|
|
|
/* Returns the default state of a particular permission.
|
|
|
|
*/
|
|
|
|
getDefault: function (aPermissionID) {
|
|
|
|
if (aPermissionID in gPermissionObject &&
|
|
|
|
gPermissionObject[aPermissionID].getDefault)
|
|
|
|
return gPermissionObject[aPermissionID].getDefault();
|
|
|
|
|
|
|
|
return this.UNKNOWN;
|
|
|
|
},
|
|
|
|
|
|
|
|
/* Returns the state of a particular permission for a given URI.
|
2013-07-02 04:42:48 +04:00
|
|
|
*/
|
|
|
|
get: function (aURI, aPermissionID) {
|
|
|
|
if (!this.isSupportedURI(aURI))
|
|
|
|
return this.UNKNOWN;
|
|
|
|
|
|
|
|
let state;
|
2013-07-09 16:44:41 +04:00
|
|
|
if (aPermissionID in gPermissionObject &&
|
|
|
|
gPermissionObject[aPermissionID].exactHostMatch)
|
2013-07-02 04:42:48 +04:00
|
|
|
state = Services.perms.testExactPermission(aURI, aPermissionID);
|
|
|
|
else
|
|
|
|
state = Services.perms.testPermission(aURI, aPermissionID);
|
|
|
|
return state;
|
|
|
|
},
|
|
|
|
|
2013-07-09 16:44:41 +04:00
|
|
|
/* Sets the state of a particular permission for a given URI.
|
2013-07-02 04:42:48 +04:00
|
|
|
*/
|
|
|
|
set: function (aURI, aPermissionID, aState) {
|
|
|
|
if (!this.isSupportedURI(aURI))
|
|
|
|
return;
|
|
|
|
|
2013-07-09 16:44:41 +04:00
|
|
|
if (aState == this.UNKNOWN) {
|
|
|
|
this.remove(aURI, aPermissionID);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-07-02 04:42:48 +04:00
|
|
|
Services.perms.add(aURI, aPermissionID, aState);
|
2013-07-09 16:44:41 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/* Removes the saved state of a particular permission for a given URI.
|
|
|
|
*/
|
2013-07-16 11:59:34 +04:00
|
|
|
remove: function (aURI, aPermissionID) {
|
2013-07-09 16:44:41 +04:00
|
|
|
if (!this.isSupportedURI(aURI))
|
|
|
|
return;
|
|
|
|
|
2015-05-29 23:33:44 +03:00
|
|
|
Services.perms.remove(aURI, aPermissionID);
|
2013-07-02 04:42:48 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/* Returns the localized label for the permission with the given ID, to be
|
|
|
|
* used in a UI for managing permissions.
|
|
|
|
*/
|
|
|
|
getPermissionLabel: function (aPermissionID) {
|
2015-10-29 02:15:00 +03:00
|
|
|
let labelID = gPermissionObject[aPermissionID].labelID || aPermissionID;
|
|
|
|
return gStringBundle.GetStringFromName("permission." + labelID + ".label");
|
2013-07-02 04:42:48 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/* Returns the localized label for the given permission state, to be used in
|
|
|
|
* a UI for managing permissions.
|
|
|
|
*/
|
2013-07-16 11:59:34 +04:00
|
|
|
getStateLabel: function (aPermissionID, aState) {
|
2013-07-02 04:42:48 +04:00
|
|
|
switch (aState) {
|
2013-07-09 16:44:41 +04:00
|
|
|
case this.UNKNOWN:
|
|
|
|
return gStringBundle.GetStringFromName("alwaysAsk");
|
2013-07-02 04:42:48 +04:00
|
|
|
case this.ALLOW:
|
|
|
|
return gStringBundle.GetStringFromName("allow");
|
|
|
|
case this.SESSION:
|
|
|
|
return gStringBundle.GetStringFromName("allowForSession");
|
|
|
|
case this.BLOCK:
|
|
|
|
return gStringBundle.GetStringFromName("block");
|
|
|
|
default:
|
|
|
|
throw new Error("unknown permission state");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-09-15 21:19:45 +03:00
|
|
|
var gPermissionObject = {
|
2013-07-02 04:42:48 +04:00
|
|
|
/* Holds permission ID => options pairs.
|
|
|
|
*
|
|
|
|
* Supported options:
|
|
|
|
*
|
|
|
|
* - exactHostMatch
|
|
|
|
* Allows sub domains to have their own permissions.
|
|
|
|
* Defaults to false.
|
|
|
|
*
|
2013-07-09 16:44:41 +04:00
|
|
|
* - getDefault
|
|
|
|
* Called to get the permission's default state.
|
|
|
|
* Defaults to UNKNOWN, indicating that the user will be asked each time
|
|
|
|
* a page asks for that permissions.
|
|
|
|
*
|
2015-10-29 02:15:00 +03:00
|
|
|
* - labelID
|
|
|
|
* Use the given ID instead of the permission name for looking up strings.
|
|
|
|
* e.g. "desktop-notification2" to use permission.desktop-notification2.label
|
|
|
|
*
|
2013-07-02 04:42:48 +04:00
|
|
|
* - states
|
|
|
|
* Array of permission states to be exposed to the user.
|
2013-07-09 16:44:41 +04:00
|
|
|
* Defaults to ALLOW, BLOCK and the default state (see getDefault).
|
2013-07-02 04:42:48 +04:00
|
|
|
*/
|
|
|
|
|
2013-07-09 16:44:41 +04:00
|
|
|
"image": {
|
|
|
|
getDefault: function () {
|
|
|
|
return Services.prefs.getIntPref("permissions.default.image") == 2 ?
|
|
|
|
SitePermissions.BLOCK : SitePermissions.ALLOW;
|
|
|
|
}
|
|
|
|
},
|
2013-07-02 04:42:48 +04:00
|
|
|
|
|
|
|
"cookie": {
|
2013-07-09 16:44:41 +04:00
|
|
|
states: [ SitePermissions.ALLOW, SitePermissions.SESSION, SitePermissions.BLOCK ],
|
|
|
|
getDefault: function () {
|
|
|
|
if (Services.prefs.getIntPref("network.cookie.cookieBehavior") == 2)
|
|
|
|
return SitePermissions.BLOCK;
|
|
|
|
|
|
|
|
if (Services.prefs.getIntPref("network.cookie.lifetimePolicy") == 2)
|
|
|
|
return SitePermissions.SESSION;
|
|
|
|
|
|
|
|
return SitePermissions.ALLOW;
|
|
|
|
}
|
2013-07-02 04:42:48 +04:00
|
|
|
},
|
|
|
|
|
2015-10-06 02:39:34 +03:00
|
|
|
"desktop-notification": {
|
2015-10-29 02:15:00 +03:00
|
|
|
exactHostMatch: true,
|
|
|
|
labelID: "desktop-notification2",
|
2015-10-06 02:39:34 +03:00
|
|
|
},
|
2013-07-02 04:42:48 +04:00
|
|
|
|
2015-09-10 01:19:34 +03:00
|
|
|
"camera": {},
|
|
|
|
"microphone": {},
|
2014-02-25 15:50:42 +04:00
|
|
|
|
2013-07-09 16:44:41 +04:00
|
|
|
"popup": {
|
|
|
|
getDefault: function () {
|
|
|
|
return Services.prefs.getBoolPref("dom.disable_open_during_load") ?
|
|
|
|
SitePermissions.BLOCK : SitePermissions.ALLOW;
|
|
|
|
}
|
|
|
|
},
|
2013-07-02 04:42:48 +04:00
|
|
|
|
2013-07-09 16:44:41 +04:00
|
|
|
"install": {
|
|
|
|
getDefault: function () {
|
|
|
|
return Services.prefs.getBoolPref("xpinstall.whitelist.required") ?
|
|
|
|
SitePermissions.BLOCK : SitePermissions.ALLOW;
|
|
|
|
}
|
|
|
|
},
|
2013-07-02 04:42:48 +04:00
|
|
|
|
|
|
|
"geo": {
|
|
|
|
exactHostMatch: true
|
|
|
|
},
|
|
|
|
|
2015-09-10 01:19:34 +03:00
|
|
|
"indexedDB": {},
|
2013-07-02 04:42:48 +04:00
|
|
|
|
|
|
|
"pointerLock": {
|
|
|
|
exactHostMatch: true
|
|
|
|
}
|
|
|
|
};
|