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 var gImportedPrefBranches = new Set();
7 function importPrefBranch(aPrefBranch, aPermission, aAction) {
8 let list = Services.prefs.getChildList(aPrefBranch);
10 for (let pref of list) {
11 let origins = Services.prefs.getCharPref(pref, "");
17 origins = origins.split(",");
19 for (let origin of origins) {
23 Services.scriptSecurityManager.createContentPrincipalFromOrigin(
28 // This preference used to contain a list of hosts. For back-compat
29 // reasons, we convert these hosts into http:// and https:// permissions
32 let httpURI = Services.io.newURI("http://" + origin);
33 let httpsURI = Services.io.newURI("https://" + origin);
36 Services.scriptSecurityManager.createContentPrincipal(httpURI, {}),
37 Services.scriptSecurityManager.createContentPrincipal(httpsURI, {}),
42 for (let principal of principals) {
44 Services.perms.addFromPrincipal(principal, aPermission, aAction);
49 Services.prefs.setCharPref(pref, "");
53 export var PermissionsUtils = {
55 * Import permissions from perferences to the Permissions Manager. After being
56 * imported, all processed permissions will be set to an empty string.
57 * Perferences are only processed once during the application's
58 * lifetime - it's safe to call this multiple times without worrying about
59 * doing unnecessary work, as the preferences branch will only be processed
62 * @param aPrefBranch Preferences branch to import from. The preferences
63 * under this branch can specify whitelist (ALLOW_ACTION)
64 * or blacklist (DENY_ACTION) additions using perference
66 * * <BRANCH>.whitelist.add.<ID>
67 * * <BRANCH>.blacklist.add.<ID>
68 * Where <ID> can be any valid preference name.
69 * The value is expected to be a comma separated list of
71 * * something.example.com
72 * * foo.exmaple.com,bar.example.com
74 * @param aPermission Permission name to be passsed to the Permissions
77 importFromPrefs(aPrefBranch, aPermission) {
78 if (!aPrefBranch.endsWith(".")) {
82 // Ensure we only import this pref branch once.
83 if (gImportedPrefBranches.has(aPrefBranch)) {
88 aPrefBranch + "whitelist.add",
90 Services.perms.ALLOW_ACTION
93 aPrefBranch + "blacklist.add",
95 Services.perms.DENY_ACTION
98 gImportedPrefBranches.add(aPrefBranch);
102 // For test use only.
103 export const PermissionsTestUtils = {
104 clearImportedPrefBranches() {
105 gImportedPrefBranches.clear();