1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/modules/PermissionsUtils.jsm Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,79 @@ 1.4 +// This Source Code Form is subject to the terms of the Mozilla Public 1.5 +// License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 +// file, You can obtain one at http://mozilla.org/MPL/2.0/. 1.7 + 1.8 +this.EXPORTED_SYMBOLS = ["PermissionsUtils"]; 1.9 + 1.10 +const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components; 1.11 + 1.12 +Cu.import("resource://gre/modules/Services.jsm"); 1.13 + 1.14 + 1.15 +let gImportedPrefBranches = new Set(); 1.16 + 1.17 +function importPrefBranch(aPrefBranch, aPermission, aAction) { 1.18 + let list = Services.prefs.getChildList(aPrefBranch, {}); 1.19 + 1.20 + for (let pref of list) { 1.21 + let hosts = ""; 1.22 + try { 1.23 + hosts = Services.prefs.getCharPref(pref); 1.24 + } catch (e) {} 1.25 + 1.26 + if (!hosts) 1.27 + continue; 1.28 + 1.29 + hosts = hosts.split(","); 1.30 + 1.31 + for (let host of hosts) { 1.32 + try { 1.33 + let uri = Services.io.newURI("http://" + host, null, null); 1.34 + Services.perms.add(uri, aPermission, aAction); 1.35 + } catch (e) {} 1.36 + } 1.37 + 1.38 + Services.prefs.setCharPref(pref, ""); 1.39 + } 1.40 +} 1.41 + 1.42 + 1.43 +this.PermissionsUtils = { 1.44 + /** 1.45 + * Import permissions from perferences to the Permissions Manager. After being 1.46 + * imported, all processed permissions will be set to an empty string. 1.47 + * Perferences are only processed once during the application's 1.48 + * lifetime - it's safe to call this multiple times without worrying about 1.49 + * doing unnecessary work, as the preferences branch will only be processed 1.50 + * the first time. 1.51 + * 1.52 + * @param aPrefBranch Preferences branch to import from. The preferences 1.53 + * under this branch can specify whitelist (ALLOW_ACTION) 1.54 + * or blacklist (DENY_ACTION) additions using perference 1.55 + * names of the form: 1.56 + * * <BRANCH>.whitelist.add.<ID> 1.57 + * * <BRANCH>.blacklist.add.<ID> 1.58 + * Where <ID> can be any valid preference name. 1.59 + * The value is expected to be a comma separated list of 1.60 + * host named. eg: 1.61 + * * something.example.com 1.62 + * * foo.exmaple.com,bar.example.com 1.63 + * 1.64 + * @param aPermission Permission name to be passsed to the Permissions 1.65 + * Manager. 1.66 + */ 1.67 + importFromPrefs: function(aPrefBranch, aPermission) { 1.68 + if (!aPrefBranch.endsWith(".")) 1.69 + aPrefBranch += "."; 1.70 + 1.71 + // Ensure we only import this pref branch once. 1.72 + if (gImportedPrefBranches.has(aPrefBranch)) 1.73 + return; 1.74 + 1.75 + importPrefBranch(aPrefBranch + "whitelist.add", aPermission, 1.76 + Services.perms.ALLOW_ACTION); 1.77 + importPrefBranch(aPrefBranch + "blacklist.add", aPermission, 1.78 + Services.perms.DENY_ACTION); 1.79 + 1.80 + gImportedPrefBranches.add(aPrefBranch); 1.81 + } 1.82 +};