Bug 1885602 - Part 5: Implement navigating to the SUMO help topic from the menu heade...
[gecko.git] / toolkit / modules / PermissionsUtils.sys.mjs
blob593cf97b046c4ceeb3d9832532221520fa5d7e9d
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, "");
13     if (!origins) {
14       continue;
15     }
17     origins = origins.split(",");
19     for (let origin of origins) {
20       let principals = [];
21       try {
22         principals = [
23           Services.scriptSecurityManager.createContentPrincipalFromOrigin(
24             origin
25           ),
26         ];
27       } catch (e) {
28         // This preference used to contain a list of hosts. For back-compat
29         // reasons, we convert these hosts into http:// and https:// permissions
30         // on default ports.
31         try {
32           let httpURI = Services.io.newURI("http://" + origin);
33           let httpsURI = Services.io.newURI("https://" + origin);
35           principals = [
36             Services.scriptSecurityManager.createContentPrincipal(httpURI, {}),
37             Services.scriptSecurityManager.createContentPrincipal(httpsURI, {}),
38           ];
39         } catch (e2) {}
40       }
42       for (let principal of principals) {
43         try {
44           Services.perms.addFromPrincipal(principal, aPermission, aAction);
45         } catch (e) {}
46       }
47     }
49     Services.prefs.setCharPref(pref, "");
50   }
53 export var PermissionsUtils = {
54   /**
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
60    * the first time.
61    *
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
65    *                     names of the form:
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
70    *                     host named. eg:
71    *                     * something.example.com
72    *                     * foo.exmaple.com,bar.example.com
73    *
74    * @param aPermission Permission name to be passsed to the Permissions
75    *                    Manager.
76    */
77   importFromPrefs(aPrefBranch, aPermission) {
78     if (!aPrefBranch.endsWith(".")) {
79       aPrefBranch += ".";
80     }
82     // Ensure we only import this pref branch once.
83     if (gImportedPrefBranches.has(aPrefBranch)) {
84       return;
85     }
87     importPrefBranch(
88       aPrefBranch + "whitelist.add",
89       aPermission,
90       Services.perms.ALLOW_ACTION
91     );
92     importPrefBranch(
93       aPrefBranch + "blacklist.add",
94       aPermission,
95       Services.perms.DENY_ACTION
96     );
98     gImportedPrefBranches.add(aPrefBranch);
99   },
102 // For test use only.
103 export const PermissionsTestUtils = {
104   clearImportedPrefBranches() {
105     gImportedPrefBranches.clear();
106   },