Skip to content

Commit 8e3cdb4

Browse files
author
Micah Lee
committed
Making it so changes to a ruleset state works even when a ruleset
preference is previously present. https://trac.torproject.org/projects/tor/ticket/8776
1 parent 11f34eb commit 8e3cdb4

File tree

7 files changed

+159
-101
lines changed

7 files changed

+159
-101
lines changed

src/chrome/content/code/ApplicableList.js

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ ApplicableList.prototype = {
8484
this.document = document;
8585

8686
var https_everywhere = CC["@eff.org/https-everywhere;1"].getService(Components.interfaces.nsISupports).wrappedJSObject;
87-
var o_httpsprefs = https_everywhere.get_prefs();
8887

8988
// get the menu popup
9089
this.menupopup = menupopup;
@@ -99,16 +98,16 @@ ApplicableList.prototype = {
9998

10099
var enableLabel = document.createElement('menuitem');
101100
var text = strings.getString("https-everywhere.menu.globalDisable");
102-
if (!o_httpsprefs.getBoolPref("globalEnabled"))
103-
text = strings.getString("https-everywhere.menu.globalEnable");
101+
if(!https_everywhere.prefs.getBoolPref("globalEnabled"))
102+
text = strings.getString("https-everywhere.menu.globalEnable");
104103

105104
enableLabel.setAttribute('label', text);
106105
enableLabel.setAttribute('command', 'https-everywhere-menuitem-globalEnableToggle');
107106
this.prepend_child(enableLabel);
108107

109108
// add the label at the top
110109
var any_rules = false
111-
for (var x in this.all) {
110+
for(var x in this.all) {
112111
any_rules = true; // how did JavaScript get this ugly?
113112
break;
114113
}
@@ -145,28 +144,35 @@ ApplicableList.prototype = {
145144
var location = domWin.document.baseURIObject.asciiSpec; //full url, including about:certerror details
146145

147146
if(location.substr(0, 6) == "about:"){
148-
//"From" portion of the rule is retrieved from the location bar via document.getElementById("urlbar").value
149-
150-
var fromHost = document.getElementById("urlbar").value;
151-
152-
//scheme must be trimmed out to check for applicable rulesets
153-
if(fromHost.indexOf("://") != -1)
154-
fromHost = fromHost.substr(fromHost.indexOf("://") + 3, fromHost.length);
155-
156-
//trim off any page locations - we only want the host - e.g. domain.com
157-
if(fromHost.indexOf("/") != -1)
158-
fromHost = fromHost.substr(0, fromHost.indexOf("/"));
159-
160-
// Search for applicable rulesets for the host listed in the location bar
161-
var plist = HTTPSRules.potentiallyApplicableRulesets(fromHost);
147+
//"From" portion of the rule is retrieved from the location bar via document.getElementById("urlbar").value
162148

163-
for (var i = 0 ; i < plist.length ; i++){
164-
//For each applicable rulset, determine active/inactive, and append to proper list.
165-
if(o_httpsprefs.getBoolPref(plist[i].name))
166-
this.active_rule(plist[i]);
167-
else
168-
this.inactive_rule(plist[i]);
169-
}
149+
var fromHost = document.getElementById("urlbar").value;
150+
151+
//scheme must be trimmed out to check for applicable rulesets
152+
if(fromHost.indexOf("://") != -1)
153+
fromHost = fromHost.substr(fromHost.indexOf("://") + 3, fromHost.length);
154+
155+
//trim off any page locations - we only want the host - e.g. domain.com
156+
if(fromHost.indexOf("/") != -1)
157+
fromHost = fromHost.substr(0, fromHost.indexOf("/"));
158+
159+
// Search for applicable rulesets for the host listed in the location bar
160+
var plist = HTTPSRules.potentiallyApplicableRulesets(fromHost);
161+
for (var i = 0 ; i < plist.length ; i++){
162+
//For each applicable rulset, determine active/inactive, and append to proper list.
163+
var ruleOn = false;
164+
try {
165+
if(https_everywhere.rule_toggle_prefs.getBoolPref(plist[i].name))
166+
ruleOn = true;
167+
} catch(e) {
168+
if(https_everywhere.https_rules.rulesetsByName[plist[i].name].active)
169+
ruleOn = true;
170+
}
171+
if(ruleOn)
172+
this.active_rule(plist[i]);
173+
else
174+
this.inactive_rule(plist[i]);
175+
}
170176
}
171177

172178
// add all applicable commands
@@ -179,7 +185,7 @@ ApplicableList.prototype = {
179185
for(var x in this.inactive)
180186
this.add_command(this.inactive[x]);
181187

182-
if(o_httpsprefs.getBoolPref("globalEnabled")){
188+
if(https_everywhere.prefs.getBoolPref("globalEnabled")){
183189
// add all the menu items
184190
for (var x in this.inactive)
185191
this.add_menuitem(this.inactive[x], 'inactive');

src/chrome/content/code/HTTPSRules.js

Lines changed: 53 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,18 @@ if(versionChecker.compare(appInfo.version, "23.0a1") >= 0) {
3131

3232
ruleset_counter = 0;
3333
function RuleSet(name, xmlName, match_rule, default_off, platform) {
34+
if(xmlName == "WordPress.xml" || xmlName == "Github.xml") {
35+
this.log(NOTE, "RuleSet( name="+name+", xmlName="+xmlName+", match_rule="+match_rule+", default_off="+default_off+", platform="+platform+" )");
36+
}
37+
3438
this.id="httpseR" + ruleset_counter;
3539
ruleset_counter += 1;
3640
this.on_by_default = true;
3741
this.name = name;
3842
this.xmlName = xmlName;
3943
//this.ruleset_match = match_rule;
4044
this.notes = "";
41-
if (match_rule) this.ruleset_match_c = new RegExp(match_rule)
45+
if (match_rule) this.ruleset_match_c = new RegExp(match_rule);
4246
else this.ruleset_match_c = null;
4347
if (default_off) {
4448
// Perhaps problematically, this currently ignores the actual content of
@@ -56,15 +60,15 @@ function RuleSet(name, xmlName, match_rule, default_off, platform) {
5660
this.rules = [];
5761
this.exclusions = [];
5862
this.cookierules = [];
59-
this.prefs = HTTPSEverywhere.instance.prefs;
63+
64+
this.rule_toggle_prefs = HTTPSEverywhere.instance.rule_toggle_prefs;
65+
6066
try {
6167
// if this pref exists, use it
62-
this.active = this.prefs.getBoolPref(name);
63-
} catch (e) {
64-
// if not, create it
65-
this.log(DBUG, "Creating new pref " + name);
68+
this.active = this.rule_toggle_prefs.getBoolPref(name);
69+
} catch(e) {
70+
// if not, use the default
6671
this.active = this.on_by_default;
67-
this.prefs.setBoolPref(name, this.on_by_default);
6872
}
6973
}
7074

@@ -101,34 +105,34 @@ RuleSet.prototype = {
101105
https_everywhereLog(level, msg);
102106
},
103107

104-
wouldMatch: function(hypothetical_uri, alist) {
105-
// return true if this ruleset would match the uri, assuming it were http
106-
// used for judging moot / inactive rulesets
107-
// alist is optional
108-
109-
// if the ruleset is already somewhere in this applicable list, we don't
110-
// care about hypothetical wouldMatch questions
111-
if (alist && (this.name in alist.all)) return false;
112-
113-
this.log(DBUG,"Would " +this.name + " match " +hypothetical_uri.spec +
114-
"? serial " + (alist && alist.serial));
115-
116-
var uri = hypothetical_uri.clone();
117-
if (uri.scheme == "https") uri.scheme = "http";
118-
var urispec = uri.spec;
119-
120-
if (this.ruleset_match_c && !this.ruleset_match_c.test(urispec))
121-
return false;
122-
123-
for (i = 0; i < this.exclusions.length; ++i)
124-
if (this.exclusions[i].pattern_c.test(urispec)) return false;
125-
126-
for (i = 0; i < this.rules.length; ++i)
127-
if (this.rules[i].from_c.test(urispec)) return true;
128-
return false;
129-
},
130-
131-
transformURI: function(uri) {
108+
wouldMatch: function(hypothetical_uri, alist) {
109+
// return true if this ruleset would match the uri, assuming it were http
110+
// used for judging moot / inactive rulesets
111+
// alist is optional
112+
113+
// if the ruleset is already somewhere in this applicable list, we don't
114+
// care about hypothetical wouldMatch questions
115+
if (alist && (this.name in alist.all)) return false;
116+
117+
this.log(DBUG,"Would " +this.name + " match " +hypothetical_uri.spec +
118+
"? serial " + (alist && alist.serial));
119+
120+
var uri = hypothetical_uri.clone();
121+
if (uri.scheme == "https") uri.scheme = "http";
122+
var urispec = uri.spec;
123+
124+
if (this.ruleset_match_c && !this.ruleset_match_c.test(urispec))
125+
return false;
126+
127+
for (i = 0; i < this.exclusions.length; ++i)
128+
if (this.exclusions[i].pattern_c.test(urispec)) return false;
129+
130+
for (i = 0; i < this.rules.length; ++i)
131+
if (this.rules[i].from_c.test(urispec)) return true;
132+
return false;
133+
},
134+
135+
transformURI: function(uri) {
132136
// If no rule applies, return null; if a rule would have applied but was
133137
// inactive, return 0; otherwise, return a fresh uri instance
134138
// for the target
@@ -145,19 +149,28 @@ RuleSet.prototype = {
145149

146150
enable: function() {
147151
// Enable us.
148-
this.prefs.setBoolPref(this.name, true);
152+
this.rule_toggle_prefs.setBoolPref(this.name, true);
149153
this.active = true;
150154
},
151155

152156
disable: function() {
153157
// Disable us.
154-
this.prefs.setBoolPref(this.name, false);
158+
this.rule_toggle_prefs.setBoolPref(this.name, false);
155159
this.active = false;
156160
},
157161

158162
toggle: function() {
159163
this.active = !this.active;
160-
this.prefs.setBoolPref(this.name, this.active);
164+
this.rule_toggle_prefs.setBoolPref(this.name, this.active);
165+
},
166+
167+
clear: function() {
168+
try {
169+
this.rule_toggle_prefs.clearUserPref(this.name);
170+
} catch(e) {
171+
// this ruleset has never been toggled
172+
}
173+
this.active = this.on_by_default;
161174
}
162175
};
163176

@@ -432,8 +445,7 @@ const HTTPSRules = {
432445
// Callable from within the prefs UI and also for cleaning up buggy
433446
// configurations...
434447
for (var i in this.rulesets) {
435-
if (this.rulesets[i].on_by_default) this.rulesets[i].enable();
436-
else this.rulesets[i].disable();
448+
this.rulesets[i].clear();
437449
}
438450
},
439451

@@ -539,7 +551,7 @@ const HTTPSRules = {
539551
intoList.push(fromList[i]);
540552
},
541553

542-
potentiallyApplicableRulesets: function(host) {
554+
potentiallyApplicableRulesets: function(host) {
543555
// Return a list of rulesets that declare targets matching this host
544556
var i, tmp, t;
545557
var results = this.global_rulesets.slice(0); // copy global_rulesets

src/chrome/content/preferences.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ INFO=3;
66
NOTE=4;
77
WARN=5;
88

9-
https_everywhere = CC["@eff.org/https-everywhere;1"].getService(Components.interfaces.nsISupports).wrappedJSObject;
10-
o_httpsprefs = https_everywhere.get_prefs();
9+
https_everywhere = CC["@eff.org/https-everywhere;1"]
10+
.getService(Components.interfaces.nsISupports)
11+
.wrappedJSObject;
12+
1113
rulesets = Array.slice(https_everywhere.https_rules.rulesets);
1214

1315
const id_prefix = "he_enable";
@@ -40,7 +42,7 @@ function resetSelected() {
4042
sel.getRangeAt(t, start, end);
4143
for (var v = start.value; v <= end.value; v++){
4244
var rs = treeView.rules[v];
43-
rs[rs.on_by_default ? "enable" : "disable"]();
45+
rs.clear();
4446
}
4547
}
4648
}
@@ -117,7 +119,15 @@ function getValue(row, col) {
117119
case "note_col":
118120
return row.notes;
119121
case "enabled_col":
120-
return o_httpsprefs.getBoolPref(row.name) ? "true" : "false";
122+
return https_everywhere.https_rules.rulesetsByName[row.name].active;
123+
/*var ruleActive = false;
124+
try {
125+
if(https_everywhere.rule_toggle_prefs.getBoolPref(row.name))
126+
ruleActive = true;
127+
} catch(e) {
128+
ruleActive = https_everywhere.https_rules.rulesetsByName[row.name].active;
129+
}
130+
return ruleActive;*/
121131
default:
122132
return;
123133
}

src/chrome/content/toolbar_button.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,7 @@ httpsEverywhere.toolbarButton = {
109109
* is enabled or disabled.
110110
*/
111111
changeIcon: function() {
112-
var prefs = HTTPSEverywhere.get_prefs();
113-
var enabled = prefs.getBoolPref("globalEnabled");
112+
var enabled = HTTPSEverywhere.prefs.getBoolPref("globalEnabled");
114113

115114
var toolbarbutton = document.getElementById('https-everywhere-button');
116115
if (enabled) {
@@ -125,8 +124,7 @@ httpsEverywhere.toolbarButton = {
125124
*/
126125
updateRulesetsApplied: function() {
127126
var toolbarbutton = document.getElementById('https-everywhere-button');
128-
var prefs = HTTPSEverywhere.get_prefs();
129-
var enabled = prefs.getBoolPref("globalEnabled");
127+
var enabled = HTTPSEverywhere.prefs.getBoolPref("globalEnabled");
130128
if (!enabled) {
131129
toolbarbutton.setAttribute('rulesetsApplied', 0);
132130
return;

src/chrome/locale/en/https-everywhere.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ https-everywhere.menu.enableDisable = Enable / Disable Rules
44
https-everywhere.menu.noRules = (No Rules for This Page)
55
https-everywhere.menu.unknownRules = (Rules for This Page Unknown)
66
https-everywhere.toolbar.hint = HTTPS Everywhere is now active. You can toggle it on a site-by-site basis by clicking the icon in the address bar.
7+
https-everywhere.migration.notification0 = In order to improve your browsing experience with HTTPS Everywhere, we've changed how we store rules. Your existing enabled/disabled rules have been reset to their default values.

0 commit comments

Comments
 (0)