|
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 file, |
|
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 "use strict"; |
|
6 |
|
7 this.EXPORTED_SYMBOLS = ["DocShellCapabilities"]; |
|
8 |
|
9 /** |
|
10 * The external API exported by this module. |
|
11 */ |
|
12 this.DocShellCapabilities = Object.freeze({ |
|
13 collect: function (docShell) { |
|
14 return DocShellCapabilitiesInternal.collect(docShell); |
|
15 }, |
|
16 |
|
17 restore: function (docShell, disallow) { |
|
18 return DocShellCapabilitiesInternal.restore(docShell, disallow); |
|
19 }, |
|
20 }); |
|
21 |
|
22 /** |
|
23 * Internal functionality to save and restore the docShell.allow* properties. |
|
24 */ |
|
25 let DocShellCapabilitiesInternal = { |
|
26 // List of docShell capabilities to (re)store. These are automatically |
|
27 // retrieved from a given docShell if not already collected before. |
|
28 // This is made so they're automatically in sync with all nsIDocShell.allow* |
|
29 // properties. |
|
30 caps: null, |
|
31 |
|
32 allCapabilities: function (docShell) { |
|
33 if (!this.caps) { |
|
34 let keys = Object.keys(docShell); |
|
35 this.caps = keys.filter(k => k.startsWith("allow")).map(k => k.slice(5)); |
|
36 } |
|
37 return this.caps; |
|
38 }, |
|
39 |
|
40 collect: function (docShell) { |
|
41 let caps = this.allCapabilities(docShell); |
|
42 return caps.filter(cap => !docShell["allow" + cap]); |
|
43 }, |
|
44 |
|
45 restore: function (docShell, disallow) { |
|
46 let caps = this.allCapabilities(docShell); |
|
47 for (let cap of caps) |
|
48 docShell["allow" + cap] = !disallow.has(cap); |
|
49 }, |
|
50 }; |