Skip to content
This repository was archived by the owner on Nov 6, 2023. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/chrome/content/code/HTTPSRules.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,16 +311,19 @@ const RuleWriter = {
this.log(WARN, "TARGETS IS NULL");
var data = this.readFromFile(file);
if (!data) return null;
return this.readFromString(data, rule_store, ruleset_id);
return this.readFromString(data, rule_store, ruleset_id, true);
},

readFromString: function(data, rule_store, ruleset_id) {
readFromString: function(data, rule_store, ruleset_id, custom) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than parsing user rules before exporting them (soon to be: copying them via the message-passing API), I think it would be simpler to have the export function read the raw file contents, and send them across as a list of strings. That way the thin-shell bootstrapped XPCOM extension won't need to include all the HTTPSRules parsing code.

try {
var xmlruleset = dom_parser.parseFromString(data, "text/xml");
} catch(e) { // file has been corrupted; XXX: handle error differently
this.log(WARN,"Error in XML data: " + e + "\n" + data);
return null;
}
if(custom){
HTTPSRules.custom_rulesets.push(data);
}
this.parseOneRuleset(xmlruleset.documentElement, rule_store, ruleset_id);
},

Expand Down Expand Up @@ -406,6 +409,7 @@ const RuleWriter = {
const HTTPSRules = {
init: function() {
try {
this.custom_rulesets = [];
this.rulesets = [];
this.targets = {}; // dict mapping target host pattern -> list of
// applicable ruleset ids
Expand Down Expand Up @@ -593,7 +597,7 @@ const HTTPSRules = {

// Load a ruleset by numeric id, e.g. 234
loadRulesetById: function(ruleset_id) {
RuleWriter.readFromString(this.rulesetStrings[ruleset_id], this, ruleset_id);
RuleWriter.readFromString(this.rulesetStrings[ruleset_id], this, ruleset_id, false);
},

// Get all rulesets matching a given target, lazy-loading from DB as necessary.
Expand Down
8 changes: 8 additions & 0 deletions src/chrome/content/export-settings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const CC = Components.classes;
let HTTPSEverywhere = CC["@eff.org/https-everywhere;1"]
.getService(Components.interfaces.nsISupports)
.wrappedJSObject;

function exportSettingsToFile(){
HTTPSEverywhere.exportSettingsToFile(window);
}
26 changes: 26 additions & 0 deletions src/chrome/content/export-settings.xul
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="chrome://global/skin/" type="text/css"?>
<?xml-stylesheet href="chrome://https-everywhere/content/preferences.css" type="text/css"?>

<!DOCTYPE overlay SYSTEM "chrome://https-everywhere/locale/https-everywhere.dtd">

<dialog id="https-everywhere-export-settings"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
xmlns:html="http://www.w3.org/1999/xhtml"
buttons="cancel,accept"
title="&https-everywhere.prefs.export_settings;"
ondialogaccept="return exportSettingsToFile();"
width="640"
height="480"
align="center">
<script type="application/x-javascript" src="chrome://https-everywhere/content/export-settings.js"/>
<vbox style="overflow:auto" flex="1">
<label style="text-align:center; font-weight:bold; font-size:22px;">&https-everywhere.about.ext_name; - &https-everywhere.prefs.export_settings;</label>
<label><span style="text-align:center; font-size:18px; margin-bottom:10px; color: red; font-weight: bold;">&https-everywhere.prefs.settings_warning;</span>&#160;<span style="text-align:center; font-size:18px; margin-bottom:10px;">&https-everywhere.prefs.settings_warning_delete;</span></label>
<label style="font-weight:bold; margin-top:10px;">&https-everywhere.prefs.settings_warning_explain;</label>
<label style="margin-top:10px; margin-left: 30px;">&#8226; &https-everywhere.prefs.settings_global;</label>
<label style="margin-top:10px; margin-left: 30px;">&#8226; &https-everywhere.prefs.settings_toggle;</label>
<label style="margin-top:10px; margin-left: 30px;">&#8226; &https-everywhere.prefs.settings_custom;</label>
<label style="margin-top: 10px;"><span style="font-weight: bold; color: red;">&https-everywhere.prefs.settings_click_ok;</span>&#160;<span>&https-everywhere.prefs.settings_warning_closing;</span></label>
</vbox>
</dialog>
4 changes: 4 additions & 0 deletions src/chrome/content/toolbar_button.js
Original file line number Diff line number Diff line change
Expand Up @@ -439,3 +439,7 @@ function migratePreferences(gBrowser) {
HTTPSEverywhere.prefs.setIntPref("prefs_version", prefs_version+1);
}
}

function exportSettingsToFile(){
HTTPSEverywhere.exportSettingsToFile(window);
}
4 changes: 4 additions & 0 deletions src/chrome/content/toolbar_button.xul
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
<menupopup id="https-everywhere-context" onpopupshowing="show_applicable_list(this)">
<!-- entries will be written here by ApplicableList.populate_menu() -->
<menuseparator class="hide-on-disable"/>
<menuitem label="&https-everywhere.prefs.export_settings;"
command="https-everywhere-menuitem-exportSettings" class="hide-on-disable"/>
<menuitem label="&https-everywhere.prefs.reset_defaults;"
command="https-everywhere-menuitem-resetToDefaults" class="hide-on-disable"/>
<menuitem label="&https-everywhere.menu.viewAllRules;"
Expand All @@ -63,6 +65,8 @@
<commandset>
<command id="https-everywhere-menuitem-rule-toggle-template"
oncommand="toggle_rule(event.target.attributes['data-id'].value)" />
<command id="https-everywhere-menuitem-exportSettings"
oncommand="exportSettingsToFile()" />
<command id="https-everywhere-menuitem-resetToDefaults"
oncommand="httpsEverywhere.toolbarButton.resetToDefaults()" />
<command id="https-everywhere-menuitem-globalEnableToggle"
Expand Down
99 changes: 99 additions & 0 deletions src/components/https-everywhere.js
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,7 @@ HTTPSEverywhere.prototype = {
this.log(DBUG,"Got sessionstore-windows-restored");
if (!this.isMobile) {
this.maybeShowObservatoryPopup();
this.maybeDoExports();
} else {
this.log(WARN, "Initializing Firefox for Android UI");
Cu.import("chrome://https-everywhere/content/code/AndroidUI.jsm");
Expand Down Expand Up @@ -597,6 +598,39 @@ HTTPSEverywhere.prototype = {
this.maybeCleanupObservatoryPrefs(ssl_observatory);
},

maybeDoExports: function() {
if(this.exportSettings().changed){
this.silentlySaveSettings();
this.maybeShowExportPopup();
}
},

maybeShowExportPopup: function() {
// Show the popup at most once, and only if the user settings are not
// default.
var shown = this.prefs.getBoolPref("export_popup_shown");
if(!shown){
this.tab_opener("chrome://https-everywhere/content/export-settings.xul");
this.prefs.setBoolPref("export_popup_shown", true);
}
},

silentlySaveSettings: function(){
var loc = "ProfD"; // profile directory
var file =
CC["@mozilla.org/file/directory_service;1"]
.getService(CI.nsIProperties)
.get(loc, CI.nsILocalFile)
.clone();
file.append("https_everywhere_settings_autosave.json");
// Check for existence, if not, create.
if (!file.exists()) {
this.log(WARN, file);
file.create(CI.nsIFile.NORMAL_FILE_TYPE, 0600);
}
this.saveSettingsFile(file.path, JSON.stringify(this.exportSettings()));
},

maybeCleanupObservatoryPrefs: function(ssl_observatory) {
// Recover from a past UI processing bug that would leave the Observatory
// accidentally disabled for some users
Expand Down Expand Up @@ -790,7 +824,72 @@ HTTPSEverywhere.prototype = {
thisBranch.setBoolPref("enabled", true);
this.httpNowhereEnabled = true;
}
},

exportSettings: function(){
var changed = false;

var prefs = {
http_nowhere_enabled: this.prefs.getBoolPref("http_nowhere.enabled"),
global_enabled: this.prefs.getBoolPref("globalEnabled"),
show_counter: this.prefs.getBoolPref("show_counter")
};

if(prefs.http_nowhere_enabled == true ||
prefs.global_enabled == false ||
prefs.show_counter == true
){
changed = true;
}

var rule_toggle = {}
for(rule_toggle_key of this.rule_toggle_prefs.getChildList("", {})){
let current_state = this.rule_toggle_prefs.getBoolPref(rule_toggle_key);
if(HTTPSRules.rulesetsByName[rule_toggle_key].on_by_default != current_state){
rule_toggle[rule_toggle_key] = current_state;
changed = true;
}
}

if(HTTPSRules.custom_rulesets.length !== 0){
changed = true;
}

var export_object = {
prefs: prefs,
rule_toggle: rule_toggle,
custom_rulesets: HTTPSRules.custom_rulesets,
changed: changed
};

return export_object;
},

exportSettingsToFile: function(window){
var settings = JSON.stringify(this.exportSettings());

var nsIFilePicker = CI.nsIFilePicker;
var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
fp.init(window, "Save Settings to File", nsIFilePicker.modeSave);
fp.defaultString = "https_everywhere_settings.json";
fp.defaultExtension = "json";
fp.addToRecentDocs = true;
var res = fp.show();
if (res != nsIFilePicker.returnCancel){
this.saveSettingsFile(fp.file.path, settings);
}
},

saveSettingsFile: function(file_path, settings){
var file = CC["@mozilla.org/file/local;1"].createInstance(CI.nsILocalFile);
file.initWithPath(file_path);

output_stream = Components.classes["@mozilla.org/network/file-output-stream;1"].createInstance(CI.nsIFileOutputStream);
output_stream.init(file, 0x02 | 0x08 | 0x20, 0600, 0);
output_stream.write(settings, settings.length);
output_stream.close();
}

};

var prefs = 0;
Expand Down
2 changes: 2 additions & 0 deletions src/defaults/preferences/preferences.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
pref("extensions.https_everywhere.LogLevel", 5);
pref("extensions.https_everywhere.log_to_stdout", false);
pref("extensions.https_everywhere.globalEnabled",true);
pref("extensions.https_everywhere.show_counter",false);
pref("extensions.https_everywhere.export_popup_shown",false);

// this is the HTTPS Everywhere preferences version (for migrations)
pref("extensions.https_everywhere.prefs_version", 0);
Expand Down