|
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/. |
|
4 |
|
5 this.EXPORTED_SYMBOLS = ["PermissionsUtils"]; |
|
6 |
|
7 const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components; |
|
8 |
|
9 Cu.import("resource://gre/modules/Services.jsm"); |
|
10 |
|
11 |
|
12 let gImportedPrefBranches = new Set(); |
|
13 |
|
14 function importPrefBranch(aPrefBranch, aPermission, aAction) { |
|
15 let list = Services.prefs.getChildList(aPrefBranch, {}); |
|
16 |
|
17 for (let pref of list) { |
|
18 let hosts = ""; |
|
19 try { |
|
20 hosts = Services.prefs.getCharPref(pref); |
|
21 } catch (e) {} |
|
22 |
|
23 if (!hosts) |
|
24 continue; |
|
25 |
|
26 hosts = hosts.split(","); |
|
27 |
|
28 for (let host of hosts) { |
|
29 try { |
|
30 let uri = Services.io.newURI("http://" + host, null, null); |
|
31 Services.perms.add(uri, aPermission, aAction); |
|
32 } catch (e) {} |
|
33 } |
|
34 |
|
35 Services.prefs.setCharPref(pref, ""); |
|
36 } |
|
37 } |
|
38 |
|
39 |
|
40 this.PermissionsUtils = { |
|
41 /** |
|
42 * Import permissions from perferences to the Permissions Manager. After being |
|
43 * imported, all processed permissions will be set to an empty string. |
|
44 * Perferences are only processed once during the application's |
|
45 * lifetime - it's safe to call this multiple times without worrying about |
|
46 * doing unnecessary work, as the preferences branch will only be processed |
|
47 * the first time. |
|
48 * |
|
49 * @param aPrefBranch Preferences branch to import from. The preferences |
|
50 * under this branch can specify whitelist (ALLOW_ACTION) |
|
51 * or blacklist (DENY_ACTION) additions using perference |
|
52 * names of the form: |
|
53 * * <BRANCH>.whitelist.add.<ID> |
|
54 * * <BRANCH>.blacklist.add.<ID> |
|
55 * Where <ID> can be any valid preference name. |
|
56 * The value is expected to be a comma separated list of |
|
57 * host named. eg: |
|
58 * * something.example.com |
|
59 * * foo.exmaple.com,bar.example.com |
|
60 * |
|
61 * @param aPermission Permission name to be passsed to the Permissions |
|
62 * Manager. |
|
63 */ |
|
64 importFromPrefs: function(aPrefBranch, aPermission) { |
|
65 if (!aPrefBranch.endsWith(".")) |
|
66 aPrefBranch += "."; |
|
67 |
|
68 // Ensure we only import this pref branch once. |
|
69 if (gImportedPrefBranches.has(aPrefBranch)) |
|
70 return; |
|
71 |
|
72 importPrefBranch(aPrefBranch + "whitelist.add", aPermission, |
|
73 Services.perms.ALLOW_ACTION); |
|
74 importPrefBranch(aPrefBranch + "blacklist.add", aPermission, |
|
75 Services.perms.DENY_ACTION); |
|
76 |
|
77 gImportedPrefBranches.add(aPrefBranch); |
|
78 } |
|
79 }; |