Skip to content

Commit 94c6eca

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 Conflicts: src/chrome/content/toolbar_button.js
1 parent 1970b0f commit 94c6eca

File tree

7 files changed

+299
-97
lines changed

7 files changed

+299
-97
lines changed

src/chrome/content/code/ApplicableList.js

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

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

8685
// get the menu popup
8786
this.menupopup = menupopup;
@@ -96,16 +95,16 @@ ApplicableList.prototype = {
9695

9796
var enableLabel = document.createElement('menuitem');
9897
var text = strings.getString("https-everywhere.menu.globalDisable");
99-
if (!o_httpsprefs.getBoolPref("globalEnabled"))
100-
text = strings.getString("https-everywhere.menu.globalEnable");
98+
if(!https_everywhere.prefs.getBoolPref("globalEnabled"))
99+
text = strings.getString("https-everywhere.menu.globalEnable");
101100

102101
enableLabel.setAttribute('label', text);
103102
enableLabel.setAttribute('command', 'https-everywhere-menuitem-globalEnableToggle');
104103
this.prepend_child(enableLabel);
105104

106105
// add the label at the top
107106
var any_rules = false
108-
for (var x in this.all) {
107+
for(var x in this.all) {
109108
any_rules = true; // how did JavaScript get this ugly?
110109
break;
111110
}
@@ -142,28 +141,35 @@ ApplicableList.prototype = {
142141
var location = domWin.document.baseURIObject.asciiSpec; //full url, including about:certerror details
143142

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

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

169175
// add all applicable commands
@@ -176,7 +182,7 @@ ApplicableList.prototype = {
176182
for(var x in this.inactive)
177183
this.add_command(this.inactive[x]);
178184

179-
if(o_httpsprefs.getBoolPref("globalEnabled")){
185+
if(https_everywhere.prefs.getBoolPref("globalEnabled")){
180186
// add all the menu items
181187
for (var x in this.inactive)
182188
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
@@ -19,14 +19,18 @@ function CookieRule(host, cookiename) {
1919
localPlatformRegexp = new RegExp("(firefox|mixedcontent)");
2020
ruleset_counter = 0;
2121
function RuleSet(name, xmlName, match_rule, default_off, platform) {
22+
if(xmlName == "WordPress.xml" || xmlName == "Github.xml") {
23+
this.log(NOTE, "RuleSet( name="+name+", xmlName="+xmlName+", match_rule="+match_rule+", default_off="+default_off+", platform="+platform+" )");
24+
}
25+
2226
this.id="httpseR" + ruleset_counter;
2327
ruleset_counter += 1;
2428
this.on_by_default = true;
2529
this.name = name;
2630
this.xmlName = xmlName;
2731
//this.ruleset_match = match_rule;
2832
this.notes = "";
29-
if (match_rule) this.ruleset_match_c = new RegExp(match_rule)
33+
if (match_rule) this.ruleset_match_c = new RegExp(match_rule);
3034
else this.ruleset_match_c = null;
3135
if (default_off) {
3236
// Perhaps problematically, this currently ignores the actual content of
@@ -44,15 +48,15 @@ function RuleSet(name, xmlName, match_rule, default_off, platform) {
4448
this.rules = [];
4549
this.exclusions = [];
4650
this.cookierules = [];
47-
this.prefs = HTTPSEverywhere.instance.prefs;
51+
52+
this.rule_toggle_prefs = HTTPSEverywhere.instance.rule_toggle_prefs;
53+
4854
try {
4955
// if this pref exists, use it
50-
this.active = this.prefs.getBoolPref(name);
51-
} catch (e) {
52-
// if not, create it
53-
this.log(DBUG, "Creating new pref " + name);
56+
this.active = this.rule_toggle_prefs.getBoolPref(name);
57+
} catch(e) {
58+
// if not, use the default
5459
this.active = this.on_by_default;
55-
this.prefs.setBoolPref(name, this.on_by_default);
5660
}
5761
}
5862

@@ -89,34 +93,34 @@ RuleSet.prototype = {
8993
https_everywhereLog(level, msg);
9094
},
9195

92-
wouldMatch: function(hypothetical_uri, alist) {
93-
// return true if this ruleset would match the uri, assuming it were http
94-
// used for judging moot / inactive rulesets
95-
// alist is optional
96-
97-
// if the ruleset is already somewhere in this applicable list, we don't
98-
// care about hypothetical wouldMatch questions
99-
if (alist && (this.name in alist.all)) return false;
100-
101-
this.log(DBUG,"Would " +this.name + " match " +hypothetical_uri.spec +
102-
"? serial " + (alist && alist.serial));
103-
104-
var uri = hypothetical_uri.clone();
105-
if (uri.scheme == "https") uri.scheme = "http";
106-
var urispec = uri.spec;
107-
108-
if (this.ruleset_match_c && !this.ruleset_match_c.test(urispec))
109-
return false;
110-
111-
for (i = 0; i < this.exclusions.length; ++i)
112-
if (this.exclusions[i].pattern_c.test(urispec)) return false;
113-
114-
for (i = 0; i < this.rules.length; ++i)
115-
if (this.rules[i].from_c.test(urispec)) return true;
116-
return false;
117-
},
118-
119-
transformURI: function(uri) {
96+
wouldMatch: function(hypothetical_uri, alist) {
97+
// return true if this ruleset would match the uri, assuming it were http
98+
// used for judging moot / inactive rulesets
99+
// alist is optional
100+
101+
// if the ruleset is already somewhere in this applicable list, we don't
102+
// care about hypothetical wouldMatch questions
103+
if (alist && (this.name in alist.all)) return false;
104+
105+
this.log(DBUG,"Would " +this.name + " match " +hypothetical_uri.spec +
106+
"? serial " + (alist && alist.serial));
107+
108+
var uri = hypothetical_uri.clone();
109+
if (uri.scheme == "https") uri.scheme = "http";
110+
var urispec = uri.spec;
111+
112+
if (this.ruleset_match_c && !this.ruleset_match_c.test(urispec))
113+
return false;
114+
115+
for (i = 0; i < this.exclusions.length; ++i)
116+
if (this.exclusions[i].pattern_c.test(urispec)) return false;
117+
118+
for (i = 0; i < this.rules.length; ++i)
119+
if (this.rules[i].from_c.test(urispec)) return true;
120+
return false;
121+
},
122+
123+
transformURI: function(uri) {
120124
// If no rule applies, return null; if a rule would have applied but was
121125
// inactive, return 0; otherwise, return a fresh uri instance
122126
// for the target
@@ -135,19 +139,28 @@ RuleSet.prototype = {
135139

136140
enable: function() {
137141
// Enable us.
138-
this.prefs.setBoolPref(this.name, true);
142+
this.rule_toggle_prefs.setBoolPref(this.name, true);
139143
this.active = true;
140144
},
141145

142146
disable: function() {
143147
// Disable us.
144-
this.prefs.setBoolPref(this.name, false);
148+
this.rule_toggle_prefs.setBoolPref(this.name, false);
145149
this.active = false;
146150
},
147151

148152
toggle: function() {
149153
this.active = !this.active;
150-
this.prefs.setBoolPref(this.name, this.active);
154+
this.rule_toggle_prefs.setBoolPref(this.name, this.active);
155+
},
156+
157+
clear: function() {
158+
try {
159+
this.rule_toggle_prefs.clearUserPref(this.name);
160+
} catch(e) {
161+
// this ruleset has never been toggled
162+
}
163+
this.active = this.on_by_default;
151164
}
152165
};
153166

@@ -464,8 +477,7 @@ const HTTPSRules = {
464477
// Callable from within the prefs UI and also for cleaning up buggy
465478
// configurations...
466479
for (var i in this.rulesets) {
467-
if (this.rulesets[i].on_by_default) this.rulesets[i].enable();
468-
else this.rulesets[i].disable();
480+
this.rulesets[i].clear();
469481
}
470482
},
471483

@@ -563,7 +575,7 @@ const HTTPSRules = {
563575
},
564576

565577

566-
potentiallyApplicableRulesets: function(host) {
578+
potentiallyApplicableRulesets: function(host) {
567579
// Return a list of rulesets that declare targets matching this host
568580
var i, tmp, t;
569581
var results = this.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
}

0 commit comments

Comments
 (0)