michael@0: // This Source Code Form is subject to the terms of the Mozilla Public michael@0: // License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: // file, You can obtain one at http://mozilla.org/MPL/2.0/. michael@0: michael@0: this.EXPORTED_SYMBOLS = ["PermissionsUtils"]; michael@0: michael@0: const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components; michael@0: michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: michael@0: michael@0: let gImportedPrefBranches = new Set(); michael@0: michael@0: function importPrefBranch(aPrefBranch, aPermission, aAction) { michael@0: let list = Services.prefs.getChildList(aPrefBranch, {}); michael@0: michael@0: for (let pref of list) { michael@0: let hosts = ""; michael@0: try { michael@0: hosts = Services.prefs.getCharPref(pref); michael@0: } catch (e) {} michael@0: michael@0: if (!hosts) michael@0: continue; michael@0: michael@0: hosts = hosts.split(","); michael@0: michael@0: for (let host of hosts) { michael@0: try { michael@0: let uri = Services.io.newURI("http://" + host, null, null); michael@0: Services.perms.add(uri, aPermission, aAction); michael@0: } catch (e) {} michael@0: } michael@0: michael@0: Services.prefs.setCharPref(pref, ""); michael@0: } michael@0: } michael@0: michael@0: michael@0: this.PermissionsUtils = { michael@0: /** michael@0: * Import permissions from perferences to the Permissions Manager. After being michael@0: * imported, all processed permissions will be set to an empty string. michael@0: * Perferences are only processed once during the application's michael@0: * lifetime - it's safe to call this multiple times without worrying about michael@0: * doing unnecessary work, as the preferences branch will only be processed michael@0: * the first time. michael@0: * michael@0: * @param aPrefBranch Preferences branch to import from. The preferences michael@0: * under this branch can specify whitelist (ALLOW_ACTION) michael@0: * or blacklist (DENY_ACTION) additions using perference michael@0: * names of the form: michael@0: * * .whitelist.add. michael@0: * * .blacklist.add. michael@0: * Where can be any valid preference name. michael@0: * The value is expected to be a comma separated list of michael@0: * host named. eg: michael@0: * * something.example.com michael@0: * * foo.exmaple.com,bar.example.com michael@0: * michael@0: * @param aPermission Permission name to be passsed to the Permissions michael@0: * Manager. michael@0: */ michael@0: importFromPrefs: function(aPrefBranch, aPermission) { michael@0: if (!aPrefBranch.endsWith(".")) michael@0: aPrefBranch += "."; michael@0: michael@0: // Ensure we only import this pref branch once. michael@0: if (gImportedPrefBranches.has(aPrefBranch)) michael@0: return; michael@0: michael@0: importPrefBranch(aPrefBranch + "whitelist.add", aPermission, michael@0: Services.perms.ALLOW_ACTION); michael@0: importPrefBranch(aPrefBranch + "blacklist.add", aPermission, michael@0: Services.perms.DENY_ACTION); michael@0: michael@0: gImportedPrefBranches.add(aPrefBranch); michael@0: } michael@0: };