browser/modules/SitePermissions.jsm

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 this.EXPORTED_SYMBOLS = [ "SitePermissions" ];
michael@0 6
michael@0 7 Components.utils.import("resource://gre/modules/Services.jsm");
michael@0 8
michael@0 9 let gStringBundle =
michael@0 10 Services.strings.createBundle("chrome://browser/locale/sitePermissions.properties");
michael@0 11
michael@0 12 this.SitePermissions = {
michael@0 13
michael@0 14 UNKNOWN: Services.perms.UNKNOWN_ACTION,
michael@0 15 ALLOW: Services.perms.ALLOW_ACTION,
michael@0 16 BLOCK: Services.perms.DENY_ACTION,
michael@0 17 SESSION: Components.interfaces.nsICookiePermission.ACCESS_SESSION,
michael@0 18
michael@0 19 /* Checks whether a UI for managing permissions should be exposed for a given
michael@0 20 * URI. This excludes file URIs, for instance, as they don't have a host,
michael@0 21 * even though nsIPermissionManager can still handle them.
michael@0 22 */
michael@0 23 isSupportedURI: function (aURI) {
michael@0 24 return aURI.schemeIs("http") || aURI.schemeIs("https");
michael@0 25 },
michael@0 26
michael@0 27 /* Returns an array of all permission IDs.
michael@0 28 */
michael@0 29 listPermissions: function () {
michael@0 30 let array = Object.keys(gPermissionObject);
michael@0 31 array.sort((a, b) => {
michael@0 32 return this.getPermissionLabel(a).localeCompare(this.getPermissionLabel(b));
michael@0 33 });
michael@0 34 return array;
michael@0 35 },
michael@0 36
michael@0 37 /* Returns an array of permission states to be exposed to the user for a
michael@0 38 * permission with the given ID.
michael@0 39 */
michael@0 40 getAvailableStates: function (aPermissionID) {
michael@0 41 if (aPermissionID in gPermissionObject &&
michael@0 42 gPermissionObject[aPermissionID].states)
michael@0 43 return gPermissionObject[aPermissionID].states;
michael@0 44
michael@0 45 if (this.getDefault(aPermissionID) == this.UNKNOWN)
michael@0 46 return [ SitePermissions.UNKNOWN, SitePermissions.ALLOW, SitePermissions.BLOCK ];
michael@0 47
michael@0 48 return [ SitePermissions.ALLOW, SitePermissions.BLOCK ];
michael@0 49 },
michael@0 50
michael@0 51 /* Returns the default state of a particular permission.
michael@0 52 */
michael@0 53 getDefault: function (aPermissionID) {
michael@0 54 if (aPermissionID in gPermissionObject &&
michael@0 55 gPermissionObject[aPermissionID].getDefault)
michael@0 56 return gPermissionObject[aPermissionID].getDefault();
michael@0 57
michael@0 58 return this.UNKNOWN;
michael@0 59 },
michael@0 60
michael@0 61 /* Returns the state of a particular permission for a given URI.
michael@0 62 */
michael@0 63 get: function (aURI, aPermissionID) {
michael@0 64 if (!this.isSupportedURI(aURI))
michael@0 65 return this.UNKNOWN;
michael@0 66
michael@0 67 let state;
michael@0 68 if (aPermissionID in gPermissionObject &&
michael@0 69 gPermissionObject[aPermissionID].exactHostMatch)
michael@0 70 state = Services.perms.testExactPermission(aURI, aPermissionID);
michael@0 71 else
michael@0 72 state = Services.perms.testPermission(aURI, aPermissionID);
michael@0 73 return state;
michael@0 74 },
michael@0 75
michael@0 76 /* Sets the state of a particular permission for a given URI.
michael@0 77 */
michael@0 78 set: function (aURI, aPermissionID, aState) {
michael@0 79 if (!this.isSupportedURI(aURI))
michael@0 80 return;
michael@0 81
michael@0 82 if (aState == this.UNKNOWN) {
michael@0 83 this.remove(aURI, aPermissionID);
michael@0 84 return;
michael@0 85 }
michael@0 86
michael@0 87 Services.perms.add(aURI, aPermissionID, aState);
michael@0 88
michael@0 89 if (aPermissionID in gPermissionObject &&
michael@0 90 gPermissionObject[aPermissionID].onChange)
michael@0 91 gPermissionObject[aPermissionID].onChange(aURI, aState);
michael@0 92 },
michael@0 93
michael@0 94 /* Removes the saved state of a particular permission for a given URI.
michael@0 95 */
michael@0 96 remove: function (aURI, aPermissionID) {
michael@0 97 if (!this.isSupportedURI(aURI))
michael@0 98 return;
michael@0 99
michael@0 100 Services.perms.remove(aURI.host, aPermissionID);
michael@0 101
michael@0 102 if (aPermissionID in gPermissionObject &&
michael@0 103 gPermissionObject[aPermissionID].onChange)
michael@0 104 gPermissionObject[aPermissionID].onChange(aURI, this.UNKNOWN);
michael@0 105 },
michael@0 106
michael@0 107 /* Returns the localized label for the permission with the given ID, to be
michael@0 108 * used in a UI for managing permissions.
michael@0 109 */
michael@0 110 getPermissionLabel: function (aPermissionID) {
michael@0 111 return gStringBundle.GetStringFromName("permission." + aPermissionID + ".label");
michael@0 112 },
michael@0 113
michael@0 114 /* Returns the localized label for the given permission state, to be used in
michael@0 115 * a UI for managing permissions.
michael@0 116 */
michael@0 117 getStateLabel: function (aPermissionID, aState) {
michael@0 118 if (aPermissionID in gPermissionObject &&
michael@0 119 gPermissionObject[aPermissionID].getStateLabel) {
michael@0 120 let label = gPermissionObject[aPermissionID].getStateLabel(aState);
michael@0 121 if (label)
michael@0 122 return label;
michael@0 123 }
michael@0 124
michael@0 125 switch (aState) {
michael@0 126 case this.UNKNOWN:
michael@0 127 return gStringBundle.GetStringFromName("alwaysAsk");
michael@0 128 case this.ALLOW:
michael@0 129 return gStringBundle.GetStringFromName("allow");
michael@0 130 case this.SESSION:
michael@0 131 return gStringBundle.GetStringFromName("allowForSession");
michael@0 132 case this.BLOCK:
michael@0 133 return gStringBundle.GetStringFromName("block");
michael@0 134 default:
michael@0 135 throw new Error("unknown permission state");
michael@0 136 }
michael@0 137 }
michael@0 138 };
michael@0 139
michael@0 140 let gPermissionObject = {
michael@0 141 /* Holds permission ID => options pairs.
michael@0 142 *
michael@0 143 * Supported options:
michael@0 144 *
michael@0 145 * - exactHostMatch
michael@0 146 * Allows sub domains to have their own permissions.
michael@0 147 * Defaults to false.
michael@0 148 *
michael@0 149 * - getDefault
michael@0 150 * Called to get the permission's default state.
michael@0 151 * Defaults to UNKNOWN, indicating that the user will be asked each time
michael@0 152 * a page asks for that permissions.
michael@0 153 *
michael@0 154 * - getStateLabel
michael@0 155 * Called to get the localized label for the given permission state, to be
michael@0 156 * used in a UI for managing permissions. May return null for states that
michael@0 157 * should use their default label.
michael@0 158 *
michael@0 159 * - onChange
michael@0 160 * Called when a permission state changes.
michael@0 161 *
michael@0 162 * - states
michael@0 163 * Array of permission states to be exposed to the user.
michael@0 164 * Defaults to ALLOW, BLOCK and the default state (see getDefault).
michael@0 165 */
michael@0 166
michael@0 167 "image": {
michael@0 168 getDefault: function () {
michael@0 169 return Services.prefs.getIntPref("permissions.default.image") == 2 ?
michael@0 170 SitePermissions.BLOCK : SitePermissions.ALLOW;
michael@0 171 }
michael@0 172 },
michael@0 173
michael@0 174 "cookie": {
michael@0 175 states: [ SitePermissions.ALLOW, SitePermissions.SESSION, SitePermissions.BLOCK ],
michael@0 176 getDefault: function () {
michael@0 177 if (Services.prefs.getIntPref("network.cookie.cookieBehavior") == 2)
michael@0 178 return SitePermissions.BLOCK;
michael@0 179
michael@0 180 if (Services.prefs.getIntPref("network.cookie.lifetimePolicy") == 2)
michael@0 181 return SitePermissions.SESSION;
michael@0 182
michael@0 183 return SitePermissions.ALLOW;
michael@0 184 }
michael@0 185 },
michael@0 186
michael@0 187 "desktop-notification": {},
michael@0 188
michael@0 189 "camera": {},
michael@0 190 "microphone": {},
michael@0 191
michael@0 192 "popup": {
michael@0 193 getDefault: function () {
michael@0 194 return Services.prefs.getBoolPref("dom.disable_open_during_load") ?
michael@0 195 SitePermissions.BLOCK : SitePermissions.ALLOW;
michael@0 196 }
michael@0 197 },
michael@0 198
michael@0 199 "install": {
michael@0 200 getDefault: function () {
michael@0 201 return Services.prefs.getBoolPref("xpinstall.whitelist.required") ?
michael@0 202 SitePermissions.BLOCK : SitePermissions.ALLOW;
michael@0 203 }
michael@0 204 },
michael@0 205
michael@0 206 "geo": {
michael@0 207 exactHostMatch: true
michael@0 208 },
michael@0 209
michael@0 210 "indexedDB": {
michael@0 211 states: [ SitePermissions.ALLOW, SitePermissions.UNKNOWN, SitePermissions.BLOCK ],
michael@0 212 getStateLabel: function (aState) {
michael@0 213 // indexedDB redefines nsIPermissionManager.UNKNOWN_ACTION (the default)
michael@0 214 // as "allow" and nsIPermissionManager.ALLOW_ACTION as "ask the user."
michael@0 215 switch (aState) {
michael@0 216 case SitePermissions.UNKNOWN:
michael@0 217 return gStringBundle.GetStringFromName("allow");
michael@0 218 case SitePermissions.ALLOW:
michael@0 219 return gStringBundle.GetStringFromName("alwaysAsk");
michael@0 220 default:
michael@0 221 return null;
michael@0 222 }
michael@0 223 },
michael@0 224 onChange: function (aURI, aState) {
michael@0 225 if (aState == SitePermissions.ALLOW || aState == SitePermissions.BLOCK)
michael@0 226 Services.perms.remove(aURI.host, "indexedDB-unlimited");
michael@0 227 }
michael@0 228 },
michael@0 229
michael@0 230 "fullscreen": {},
michael@0 231
michael@0 232 "pointerLock": {
michael@0 233 exactHostMatch: true
michael@0 234 }
michael@0 235 };

mercurial