1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 this.EXPORTED_SYMBOLS = [ "SitePermissions" ];
7 Components.utils.import("resource://gre/modules/Services.jsm");
10 Services.strings.createBundle("chrome://browser/locale/sitePermissions.properties");
12 this.SitePermissions = {
14 UNKNOWN: Services.perms.UNKNOWN_ACTION,
15 ALLOW: Services.perms.ALLOW_ACTION,
16 BLOCK: Services.perms.DENY_ACTION,
17 SESSION: Components.interfaces.nsICookiePermission.ACCESS_SESSION,
19 /* Checks whether a UI for managing permissions should be exposed for a given
20 * URI. This excludes file URIs, for instance, as they don't have a host,
21 * even though nsIPermissionManager can still handle them.
23 isSupportedURI: function (aURI) {
24 return aURI.schemeIs("http") || aURI.schemeIs("https");
27 /* Returns an array of all permission IDs.
29 listPermissions: function () {
30 let array = Object.keys(gPermissionObject);
31 array.sort((a, b) => {
32 return this.getPermissionLabel(a).localeCompare(this.getPermissionLabel(b));
37 /* Returns an array of permission states to be exposed to the user for a
38 * permission with the given ID.
40 getAvailableStates: function (aPermissionID) {
41 if (aPermissionID in gPermissionObject &&
42 gPermissionObject[aPermissionID].states)
43 return gPermissionObject[aPermissionID].states;
45 if (this.getDefault(aPermissionID) == this.UNKNOWN)
46 return [ SitePermissions.UNKNOWN, SitePermissions.ALLOW, SitePermissions.BLOCK ];
48 return [ SitePermissions.ALLOW, SitePermissions.BLOCK ];
51 /* Returns the default state of a particular permission.
53 getDefault: function (aPermissionID) {
54 if (aPermissionID in gPermissionObject &&
55 gPermissionObject[aPermissionID].getDefault)
56 return gPermissionObject[aPermissionID].getDefault();
61 /* Returns the state of a particular permission for a given URI.
63 get: function (aURI, aPermissionID) {
64 if (!this.isSupportedURI(aURI))
68 if (aPermissionID in gPermissionObject &&
69 gPermissionObject[aPermissionID].exactHostMatch)
70 state = Services.perms.testExactPermission(aURI, aPermissionID);
72 state = Services.perms.testPermission(aURI, aPermissionID);
76 /* Sets the state of a particular permission for a given URI.
78 set: function (aURI, aPermissionID, aState) {
79 if (!this.isSupportedURI(aURI))
82 if (aState == this.UNKNOWN) {
83 this.remove(aURI, aPermissionID);
87 Services.perms.add(aURI, aPermissionID, aState);
89 if (aPermissionID in gPermissionObject &&
90 gPermissionObject[aPermissionID].onChange)
91 gPermissionObject[aPermissionID].onChange(aURI, aState);
94 /* Removes the saved state of a particular permission for a given URI.
96 remove: function (aURI, aPermissionID) {
97 if (!this.isSupportedURI(aURI))
100 Services.perms.remove(aURI.host, aPermissionID);
102 if (aPermissionID in gPermissionObject &&
103 gPermissionObject[aPermissionID].onChange)
104 gPermissionObject[aPermissionID].onChange(aURI, this.UNKNOWN);
107 /* Returns the localized label for the permission with the given ID, to be
108 * used in a UI for managing permissions.
110 getPermissionLabel: function (aPermissionID) {
111 return gStringBundle.GetStringFromName("permission." + aPermissionID + ".label");
114 /* Returns the localized label for the given permission state, to be used in
115 * a UI for managing permissions.
117 getStateLabel: function (aPermissionID, aState) {
118 if (aPermissionID in gPermissionObject &&
119 gPermissionObject[aPermissionID].getStateLabel) {
120 let label = gPermissionObject[aPermissionID].getStateLabel(aState);
127 return gStringBundle.GetStringFromName("alwaysAsk");
129 return gStringBundle.GetStringFromName("allow");
131 return gStringBundle.GetStringFromName("allowForSession");
133 return gStringBundle.GetStringFromName("block");
135 throw new Error("unknown permission state");
140 let gPermissionObject = {
141 /* Holds permission ID => options pairs.
146 * Allows sub domains to have their own permissions.
150 * Called to get the permission's default state.
151 * Defaults to UNKNOWN, indicating that the user will be asked each time
152 * a page asks for that permissions.
155 * Called to get the localized label for the given permission state, to be
156 * used in a UI for managing permissions. May return null for states that
157 * should use their default label.
160 * Called when a permission state changes.
163 * Array of permission states to be exposed to the user.
164 * Defaults to ALLOW, BLOCK and the default state (see getDefault).
168 getDefault: function () {
169 return Services.prefs.getIntPref("permissions.default.image") == 2 ?
170 SitePermissions.BLOCK : SitePermissions.ALLOW;
175 states: [ SitePermissions.ALLOW, SitePermissions.SESSION, SitePermissions.BLOCK ],
176 getDefault: function () {
177 if (Services.prefs.getIntPref("network.cookie.cookieBehavior") == 2)
178 return SitePermissions.BLOCK;
180 if (Services.prefs.getIntPref("network.cookie.lifetimePolicy") == 2)
181 return SitePermissions.SESSION;
183 return SitePermissions.ALLOW;
187 "desktop-notification": {},
193 getDefault: function () {
194 return Services.prefs.getBoolPref("dom.disable_open_during_load") ?
195 SitePermissions.BLOCK : SitePermissions.ALLOW;
200 getDefault: function () {
201 return Services.prefs.getBoolPref("xpinstall.whitelist.required") ?
202 SitePermissions.BLOCK : SitePermissions.ALLOW;
211 states: [ SitePermissions.ALLOW, SitePermissions.UNKNOWN, SitePermissions.BLOCK ],
212 getStateLabel: function (aState) {
213 // indexedDB redefines nsIPermissionManager.UNKNOWN_ACTION (the default)
214 // as "allow" and nsIPermissionManager.ALLOW_ACTION as "ask the user."
216 case SitePermissions.UNKNOWN:
217 return gStringBundle.GetStringFromName("allow");
218 case SitePermissions.ALLOW:
219 return gStringBundle.GetStringFromName("alwaysAsk");
224 onChange: function (aURI, aState) {
225 if (aState == SitePermissions.ALLOW || aState == SitePermissions.BLOCK)
226 Services.perms.remove(aURI.host, "indexedDB-unlimited");