Commit e8a90e2c authored by Thomas Rientjes's avatar Thomas Rientjes
Browse files

Refactor existing codebase

parent 4360a8c6
/**
* Setting alteration model.
*/
class SettingAlteration {
/**
* Creates a setting alteration.
*
* @param {String} settingId - The identifier of the altered setting.
* @param {String} previousValue - The previous value of the altered setting.
* @param {String} currentValue - The current value of the altered setting.
*/
constructor (settingId, previousValue, currentValue) {
this.settingId = settingId;
this.previousValue = previousValue;
this.currentValue = currentValue;
}
}
......@@ -40,15 +40,15 @@ function handleSettingAlteration ({ target }) {
// Apply the setting to the user interface.
applySetting({
id: settingId,
value: currentValue
'id': settingId,
'value': currentValue
});
// Inform the attached content script.
vent.emit('setting:altered', {
settingId: settingId,
previousValue: previousValue,
currentValue: currentValue
'settingId': settingId,
'previousValue': previousValue,
'currentValue': currentValue
});
// Memorize the previous value of the setting.
......
......@@ -11,7 +11,6 @@
<link rel="stylesheet" type="text/css" href="css/settings.css">
<script src="js/models/setting-alteration.js"></script>
<script src="js/settings.js"></script>
</head>
......
......@@ -32,9 +32,9 @@ var settings = require('./settings');
/**
* Browser security preferences module.
*
* @var {object} securityPrefs
* @var {Object} securityPrefs
*/
var securityPrefs = require('./modules/security-prefs.js');
var securityPreferences = require('./modules/security-preferences.js');
/**
* Can be used to get localization entries by key name.
......@@ -60,7 +60,7 @@ var chromeWindow = Services.wm.getMostRecentWindow('navigator:browser');
/**
* Uniquely identifies the extension's chrome menu item.
*
* @var {Number} chomeMenuItemIdentifier
* @var {number} chomeMenuItemIdentifier
*/
var chromeMenuItemIdentifier = null;
......@@ -73,12 +73,12 @@ exports.main = function () {
settings.initialize();
// Initialize the security preferences module.
securityPrefs.initialize();
securityPreferences.initialize();
// Add the extension's chrome menu item to the main browser menu.
chromeMenuItemIdentifier = chromeWindow.NativeWindow.menu.add({
name: _('settings_label', BROWSER_NAME),
callback: showSettingsPanel
'name': _('settings_label', BROWSER_NAME),
'callback': showSettingsPanel
});
};
......@@ -118,8 +118,8 @@ function showSettingsPanel () {
// Open up the settings panel in a new tab.
tabBrowser.addTab(SETTINGS_URI, {
selected: true,
parentId: tabBrowser.selectedTab.id
'selected': true,
'parentId': tabBrowser.selectedTab.id
});
} else {
......
// # 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],
"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],
"svg.in-content.enabled" : [true, true, false]
/**
* Security preferences.
*
* @module modules
*/
'use strict';
/**
* Facilitates working with browser preferences.
*
* @var {Object} sdkPreferences
*/
var sdkPreferences = require('sdk/preferences/service');
/**
* Contains generic utility functions.
*
* @var {Object} utilities
*/
var utilities = require('./utilities.js');
/**
* Bindings between preferences and slider levels.
*
* @var {Object} preferenceBindings
*/
var preferenceBindings = {
// Preference name Standard Safer Safest
'javascript.options.ion.content': [true, false, false],
'javascript.options.typeinference': [true, false, false],
'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],
'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";
const nsaPolicyPref = "extensions.nsa.policy";
const kSliderPref = 'extensions.tor-browser-settings@torproject.org.security_level';
const kCustomPref = 'extensions.tor-browser-settings@torproject.org.security_custom';
const nsaPolicyPref = 'extensions.nsa.policy';
const nsaStandard = {
"js": true,
"webgl": true,
"java": true,
"flash": true,
"silverlight": true,
"plugin": true,
"media": true,
"frame": true,
"font": true
'js': true,
'webgl': true,
'java': true,
'flash': true,
'silverlight': true,
'plugin': true,
'media': true,
'frame': true,
'font': true
};
const nsaSafest = {
"js": false,
"webgl": false,
"java": false,
"flash": false,
"silverlight": false,
"plugin": false,
"media": false,
"frame": false,
"font": false
'js': false,
'webgl': false,
'java': false,
'flash': false,
'silverlight': false,
'plugin': false,
'media': false,
'frame': false,
'font': false
};
// ### Prefs
var generate_nsa_policy = function () {
let currentNsaPolicy = get(nsaPolicyPref) || '{"UNTRUSTED":{},"TRUSTED":{"js":true,"webgl":true,"java":true,"flash":true,"silverlight":true,"plugin":true,"media":true,"frame":true,"font":true},"DEFAULT":{"frame":true},"mozilla.org":1,"mozilla.com":1,"mozilla.net":1,"google.com":1,"gstatic.com":1,"googleapis.com":1,"yahoo.com":1,"yimg.com":1,"yahooapis.com":1,"hotmail.com":1,"live.com":1,"wlxrs.com":1,"securecode.com":1,"recaptcha.net":1,"noscript.net":1,"flashgot.net":1,"informaction.com":1,"youtube.com":1,"ytimg.com":1}';
let securitySliderValue = get(kSliderPref);
let currentNsaPolicy = sdkPreferences.get(nsaPolicyPref) || '{"UNTRUSTED":{},"TRUSTED":{"js":true,"webgl":true,"java":true,"flash":true,"silverlight":true,"plugin":true,"media":true,"frame":true,"font":true},"DEFAULT":{"frame":true},"mozilla.org":1,"mozilla.com":1,"mozilla.net":1,"google.com":1,"gstatic.com":1,"googleapis.com":1,"yahoo.com":1,"yimg.com":1,"yahooapis.com":1,"hotmail.com":1,"live.com":1,"wlxrs.com":1,"securecode.com":1,"recaptcha.net":1,"noscript.net":1,"flashgot.net":1,"informaction.com":1,"youtube.com":1,"ytimg.com":1}';
let securitySliderValue = sdkPreferences.get(kSliderPref);
currentNsaPolicy = JSON.parse(currentNsaPolicy);
currentNsaPolicy.UNTRUSTED = {};
if (securitySliderValue === 0 || securitySliderValue === "0") {
if (securitySliderValue === 0 || securitySliderValue === '0') {
currentNsaPolicy.TRUSTED = nsaStandard;
currentNsaPolicy.DEFAULT = nsaStandard;
} else {
......@@ -72,23 +88,23 @@ var generate_nsa_policy = function () {
// 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]));
Object.keys(preferenceBindings).forEach(
prefName => sdkPreferences.set(
prefName, preferenceBindings[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);
let prefNames = Object.keys(preferenceBindings);
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)) {
if (preferenceBindings[prefName][settingIndex] !==
sdkPreferences.get(prefName)) {
possibleSetting = false;
}
}
......@@ -106,11 +122,10 @@ var read_setting_from_prefs = function () {
// 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 prefNames = Object.keys(preferenceBindings);
let unbindFuncs = [];
for (let prefName of prefNames) {
unbindFuncs.push(utils.bindPrefAndInit(
prefName, () => onSettingChanged(read_setting_from_prefs())));
unbindFuncs.push(utilities.bindChangeHandlerToPreference(prefName, () => onSettingChanged(read_setting_from_prefs()), true));
}
// Call all the unbind functions.
return () => unbindFuncs.forEach(unbind => unbind());
......@@ -123,36 +138,38 @@ var initialized = false;
// __initialize()__.
// Defines the behavior of "extensions.torbutton.security_custom",
// "extensions.torbutton.security_slider", and the security-sensitive
// prefs declared in kSecuritySettings.
// prefs declared in preferenceBindings.
var initialize = function () {
// Only run once.
if (initialized) {
return;
}
initialized = true;
set(nsaPolicyPref, generate_nsa_policy());
sdkPreferences.set(nsaPolicyPref, generate_nsa_policy());
// 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));
}
});
utilities.bindChangeHandlerToPreference(kCustomPref, function (custom) {
if (custom === false) {
write_setting_to_prefs(sdkPreferences.get(kSliderPref));
}
}, true);
// If security_slider is given a new value, then security_custom should
// be set to false.
utils.bindPref(kSliderPref, function (prefIndex) {
set(kCustomPref, false);
set(nsaPolicyPref, generate_nsa_policy());
utilities.bindChangeHandlerToPreference(kSliderPref, function (prefIndex) {
sdkPreferences.set(kCustomPref, false);
sdkPreferences.set(nsaPolicyPref, generate_nsa_policy());
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);
sdkPreferences.set(kCustomPref, true);
} else {
set(kSliderPref, settingIndex);
set(kCustomPref, false);
sdkPreferences.set(kSliderPref, settingIndex);
sdkPreferences.set(kCustomPref, false);
}
});
};
......
/**
* Utility functions.
*
* @module modules
*/
'use strict';
/**
* Can be used to obtain references to services.
*
* @var {Object} Services
*/
var { Services } = require('resource://gre/modules/Services.jsm');
/**
* Facilitates working with browser preferences.
*
* @var {Object} preferences
*/
var preferences = Services.prefs;
/**
* Facilitates working with browser preferences.
*
* @var {Object} sdkPreferences
*/
var sdkPreferences = require('sdk/preferences/service');
/**
* Bind a change handler to a specific preference domain.
*/
exports.bindChangeHandlerToPreference = function (domain, handler, initialize = false) {
let updatePreference, observer;
updatePreference = () => {
handler(sdkPreferences.get(domain));
};
observer = {
observe: function (subject, topic, observedDomain) {
if (domain === observedDomain) {
updatePreference();
};
}
};
preferences.addObserver(domain, observer, false);
if (initialize === true) {
updatePreference();
}
return () => {
preferences.removeObserver(domain, observer);
};
};
// # Utils.js
// Various helpful utility functions.
// ### Import Mozilla Services
var { Services } = require("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;
......@@ -65,9 +65,9 @@ var settings = require('sdk/simple-prefs').prefs;
exports.initialize = function () {
pageMod.PageMod({
include: SETTINGS_URI,
contentScriptFile: self.data.url('content-scripts/settings.js'),
onAttach: startListening
'include': SETTINGS_URI,
'contentScriptFile': self.data.url('content-scripts/settings.js'),
'onAttach': startListening
});
};
......@@ -82,26 +82,26 @@ function startListening (worker) {
worker.port.emit('worker:initialized', {
l10n: {
settings_label: _('settings_label', BROWSER_NAME),
security_label: _('security_label'),
standard_label: _('standard_label'),
standard_description: _('standard_description', BROWSER_NAME),
safer_label: _('safer_label'),
safer_description: _('safer_description'),
safer_list_label: _('safer_list_label'),
safest_label: _('safest_label'),
safest_description: _('safest_description'),
safest_list_label: _('safest_list_label'),
learn_more_label: _('learn_more_label'),
js_on_https_sites_only: _('js_on_https_sites_only'),
js_disabled: _('js_disabled'),
limit_typography: _('limit_typography'),
limit_graphics_and_typography: _('limit_graphics_and_typography'),
tap_to_play_media: _('tap_to_play_media')
'l10n': {
'settings_label': _('settings_label', BROWSER_NAME),
'security_label': _('security_label'),
'standard_label': _('standard_label'),
'standard_description': _('standard_description', BROWSER_NAME),
'safer_label': _('safer_label'),
'safer_description': _('safer_description'),
'safer_list_label': _('safer_list_label'),
'safest_label': _('safest_label'),
'safest_description': _('safest_description'),
'safest_list_label': _('safest_list_label'),
'learn_more_label': _('learn_more_label'),
'js_on_https_sites_only': _('js_on_https_sites_only'),
'js_disabled': _('js_disabled'),
'limit_typography': _('limit_typography'),
'limit_graphics_and_typography': _('limit_graphics_and_typography'),
'tap_to_play_media': _('tap_to_play_media')
},
settings: {
security_level: new Setting('security_level', settings['security_level'])
'settings': {
'security_level': new Setting('security_level', settings['security_level'])
}
});
......
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