1 // # Security Settings prefs (as controlled by the Security Slider)
5 let {classes: Cc, utils: Cu } = Components;
6 let { getBoolPref, setBoolPref, getIntPref, setIntPref } =
7 Cu.import("resource://gre/modules/Services.jsm", {}).Services.prefs;
8 let { bindPref, bindPrefAndInit } =
9 Cu.import("resource://torbutton/modules/utils.js", {});
10 let logger = Components.classes["@torproject.org/torbutton-logger;1"]
11 .getService(Components.interfaces.nsISupports).wrappedJSObject;
12 let log = (level, msg) => logger.log(level, msg);
16 // __kSecuritySettings__.
17 // A table of all prefs bound to the security slider, and the value
18 // for each security setting. Note that 2-m and 3-m are identical,
19 // corresponding to the old 2-medium-high setting. We also separately
20 // bind NoScript settings to the extensions.torbutton.security_slider
21 // (see noscript-control.js).
22 const kSecuritySettings = {
23 // Preference name : [0, 1-high 2-m 3-m 4-low]
24 "javascript.options.ion" : [, false, false, false, true ],
25 "javascript.options.baselinejit" : [, false, false, false, true ],
26 "javascript.options.native_regexp" : [, false, false, false, true ],
27 "media.webaudio.enabled" : [, false, false, false, true ],
28 "mathml.disabled" : [, true, true, true, false],
29 "gfx.font_rendering.opentype_svg.enabled" : [, false, false, false, true ],
30 "svg.disabled" : [, true, false, false, false],
33 // The Security Settings prefs in question.
34 const kSliderPref = "extensions.torbutton.security_slider";
35 const kCustomPref = "extensions.torbutton.security_custom";
39 // __write_setting_to_prefs(settingIndex)__.
40 // Take a given setting index and write the appropriate pref values
41 // to the pref database.
42 var write_setting_to_prefs = function (settingIndex) {
43 Object.keys(kSecuritySettings).forEach(
44 prefName => setBoolPref(
45 prefName, kSecuritySettings[prefName][settingIndex]));
48 // __read_setting_from_prefs()__.
49 // Read the current pref values, and decide if any of our
50 // security settings matches. Otherwise return null.
51 var read_setting_from_prefs = function () {
52 let prefNames = Object.keys(kSecuritySettings);
53 for (let settingIndex of [1, 2, 3, 4]) {
54 let possibleSetting = true;
55 // For the given settingIndex, check if all current pref values
57 for (let prefName of prefNames) {
58 if (kSecuritySettings[prefName][settingIndex] !==
59 getBoolPref(prefName)) {
60 possibleSetting = false;
63 if (possibleSetting) {
68 // No matching setting; return null.
72 // __watch_security_prefs(onSettingChanged)__.
73 // Whenever a pref bound to the security slider changes, onSettingChanged
74 // is called with the new security setting value (1,2,3,4 or null).
75 // Returns a zero-arg function that ends this binding.
76 var watch_security_prefs = function (onSettingChanged) {
77 let prefNames = Object.keys(kSecuritySettings);
79 for (let prefName of prefNames) {
80 unbindFuncs.push(bindPrefAndInit(
81 prefName, () => onSettingChanged(read_setting_from_prefs())));
83 // Call all the unbind functions.
84 return () => unbindFuncs.forEach(unbind => unbind());
88 // Have we called initialize() yet?
89 var initialized = false;
92 // Defines the behavior of "extensions.torbutton.security_custom",
93 // "extensions.torbutton.security_slider", and the security-sensitive
94 // prefs declared in kSecuritySettings.
95 var initialize = function () {
100 log(4, "Initializing security-prefs.js");
102 // When security_custom is set to false, apply security_slider setting
103 // to the security-sensitive prefs.
104 bindPrefAndInit(kCustomPref, function (custom) {
105 if (custom === false) {
106 write_setting_to_prefs(getIntPref(kSliderPref));
109 // If security_slider is given a new value, then security_custom should
111 bindPref(kSliderPref, function (prefIndex) {
112 setBoolPref(kCustomPref, false);
113 write_setting_to_prefs(prefIndex);
115 // If a security-sensitive pref changes, then decide if the set of pref values
116 // constitutes a security_slider setting or a custom value.
117 watch_security_prefs(settingIndex => {
118 if (settingIndex === null) {
119 setBoolPref(kCustomPref, true);
121 setIntPref(kSliderPref, settingIndex);
122 setBoolPref(kCustomPref, false);
125 // Migrate from old medium-low (3) to new medium (2).
126 if (getBoolPref("extensions.torbutton.security_custom") === false &&
127 getIntPref("extensions.torbutton.security_slider") === 3) {
128 setIntPref("extensions.torbutton.security_slider", 2);
129 write_setting_to_prefs(2);
131 log(4, "security-prefs.js initialization complete");
134 // Export initialize() function for external use.
135 let EXPORTED_SYMBOLS = ["initialize"];