Commit 5d0853f2 authored by Amogh Pradeep's avatar Amogh Pradeep
Browse files

Linking UI to preference logic, added security-prefs and utils

parent 44b3e400
......@@ -29,6 +29,13 @@ const SETTINGS_URI = 'chrome://tor-browser-settings/content/settings.html';
*/
var settings = require('./settings');
/**
* Application security prefs module.
*
* @var {object} SecurityPrefs
*/
let SecurityPrefs = require('./modules/security-prefs.js');
/**
* Can be used to get localization entries by key name.
*
......@@ -63,6 +70,7 @@ var chromeMenuItemIdentifier = null;
exports.main = function () {
settings.initialize();
SecurityPrefs.initialize();
// Add the extension's chrome menu item to the main browser menu.
chromeMenuItemIdentifier = chromeWindow.NativeWindow.menu.add({
......
// # Security Settings prefs (as controlled by the Security Slider)
// ### Utilities
var { get, set } = require("sdk/preferences/service");
let utils = require('./utils.js');
// ### Constants
// __kSecuritySettings__.
// A table of all prefs bound to the security slider, and the value
// for each security setting. Note that 2-m and 3-m are identical,
// corresponding to the old 2-medium-high setting.
const kSecuritySettings = {
// Preference name : [0-low 1-m 2-high]
"javascript.options.ion.content" : [true, false, false],
"javascript.options.typeinference" : [true, false, false],
"noscript.forbidMedia" : [false, true, true ],
"media.webaudio.enabled" : [true, false, false],
"mathml.disabled" : [false, true, true ],
"javascript.options.baselinejit.content" : [true, false, false],
"gfx.font_rendering.opentype_svg.enabled" : [true, false, false],
"noscript.global" : [true, false, false],
"noscript.globalHttpsWhitelist" : [false, true, false],
"noscript.forbidFonts" : [false, false, true ],
"svg.in-content.enabled" : [true, true, false],
};
// The Security Settings prefs in question.
const kSliderPref = "extensions.tor-browser-settings@torproject.org.security_level";
const kCustomPref = "extensions.tor-browser-settings@torproject.org.security_custom";
// ### Prefs
// __write_setting_to_prefs(settingIndex)__.
// Take a given setting index and write the appropriate pref values
// to the pref database.
var write_setting_to_prefs = function (settingIndex) {
Object.keys(kSecuritySettings).forEach(
prefName => set(
prefName, kSecuritySettings[prefName][settingIndex]));
};
// __read_setting_from_prefs()__.
// Read the current pref values, and decide if any of our
// security settings matches. Otherwise return null.
var read_setting_from_prefs = function () {
let prefNames = Object.keys(kSecuritySettings);
for (let settingIndex of [0, 1, 2]) {
let possibleSetting = true;
// For the given settingIndex, check if all current pref values
// match the setting.
for (let prefName of prefNames) {
if (kSecuritySettings[prefName][settingIndex] !==
get(prefName)) {
possibleSetting = false;
}
}
if (possibleSetting) {
// We have a match!
return settingIndex;
}
}
// No matching setting; return null.
return null;
};
// __watch_security_prefs(onSettingChanged)__.
// Whenever a pref bound to the security slider changes, onSettingChanged
// is called with the new security setting value (0,1,2 or null).
// Returns a zero-arg function that ends this binding.
var watch_security_prefs = function (onSettingChanged) {
let prefNames = Object.keys(kSecuritySettings);
let unbindFuncs = [];
for (let prefName of prefNames) {
unbindFuncs.push(utils.bindPrefAndInit(
prefName, () => onSettingChanged(read_setting_from_prefs())));
}
// Call all the unbind functions.
return () => unbindFuncs.forEach(unbind => unbind());
};
// __initialized__.
// Have we called initialize() yet?
var initialized = false;
// __initialize()__.
// Defines the behavior of "extensions.torbutton.security_custom",
// "extensions.torbutton.security_slider", and the security-sensitive
// prefs declared in kSecuritySettings.
var initialize = function () {
// Only run once.
if (initialized) {
return;
}
initialized = true;
// When security_custom is set to false, apply security_slider setting
// to the security-sensitive prefs.
utils.bindPrefAndInit(kCustomPref, function (custom) {
if (custom === false) {
write_setting_to_prefs(get(kSliderPref));
}
});
// If security_slider is given a new value, then security_custom should
// be set to false.
utils.bindPref(kSliderPref, function (prefIndex) {
set(kCustomPref, false);
write_setting_to_prefs(prefIndex);
});
// If a security-sensitive pref changes, then decide if the set of pref values
// constitutes a security_slider setting or a custom value.
watch_security_prefs(settingIndex => {
if (settingIndex === null) {
set(kCustomPref, true);
} else {
set(kSliderPref, settingIndex);
set(kCustomPref, false);
}
});
};
// Export initialize() function for external use.
exports.initialize = initialize;
// # Utils.js
// Various helpful utility functions.
// ### Shortcut
let { Cu, Cc, Ci } = require('chrome');
// ### Import Mozilla Services
Cu.import("resource://gre/modules/Services.jsm");
// ## Pref utils
// __prefs__. A shortcut to Mozilla Services.prefs.
let prefs = Services.prefs;
let newPrefs = require('sdk/preferences/service');
// __bindPref(prefName, prefHandler, init)__
// Applies prefHandler whenever the value of the pref changes.
// If init is true, applies prefHandler to the current value.
// Returns a zero-arg function that unbinds the pref.
var bindPref = function (prefName, prefHandler, init = false) {
let update = () => { prefHandler(newPrefs.get(prefName)); },
observer = { observe : function (subject, topic, data) {
if (data === prefName) {
update();
}
} };
prefs.addObserver(prefName, observer, false);
if (init) {
update();
}
return () => { prefs.removeObserver(prefName, observer); };
};
// __bindPrefAndInit(prefName, prefHandler)__
// Applies prefHandler to the current value of pref specified by prefName.
// Re-applies prefHandler whenever the value of the pref changes.
// Returns a zero-arg function that unbinds the pref.
var bindPrefAndInit = (prefName, prefHandler) =>
bindPref(prefName, prefHandler, true);
// Export utility functions for external use.
exports.bindPref = bindPref;
exports.bindPrefAndInit = bindPrefAndInit;
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment