Skip to content

Commit d522f4d

Browse files
Chan Chak ShingHainish
authored andcommitted
Use Map insteads of Object for RuleSets.targets (EFForg#12160)
1 parent f5a20a0 commit d522f4d

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

chromium/rules.js

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ RuleSet.prototype = {
175175
*/
176176
function RuleSets(ruleActiveStates) {
177177
// Load rules into structure
178-
this.targets = {};
178+
this.targets = new Map();
179179

180180
// A cache for potentiallyApplicableRulesets
181181
this.ruleCache = new Map();
@@ -213,12 +213,12 @@ RuleSets.prototype = {
213213
var new_rule_set = new RuleSet(params.host, true, "user rule");
214214
var new_rule = new Rule(params.urlMatcher, params.redirectTo);
215215
new_rule_set.rules.push(new_rule);
216-
if (!(params.host in this.targets)) {
217-
this.targets[params.host] = [];
216+
if (!this.targets.has(params.host)) {
217+
this.targets.set(params.host, []);
218218
}
219219
this.ruleCache.delete(params.host);
220220
// TODO: maybe promote this rule?
221-
this.targets[params.host].push(new_rule_set);
221+
this.targets.get(params.host).push(new_rule_set);
222222
if (new_rule_set.name in this.ruleActiveStates) {
223223
new_rule_set.active = (this.ruleActiveStates[new_rule_set.name] == "true");
224224
}
@@ -234,12 +234,17 @@ RuleSets.prototype = {
234234
removeUserRule: function(ruleset) {
235235
log(INFO, 'removing user rule for ' + JSON.stringify(ruleset));
236236
this.ruleCache.delete(ruleset.name);
237-
this.targets[ruleset.name] = this.targets[ruleset.name].filter(r =>
237+
238+
239+
var tmp = this.targets.get(ruleset.name).filter(r =>
238240
!(r.isEquivalentTo(ruleset))
239241
);
240-
if (this.targets[ruleset.name].length == 0) {
241-
delete this.targets[ruleset.name];
242+
this.targets.set(ruleset.name, tmp);
243+
244+
if (this.targets.get(ruleset.name).length == 0) {
245+
this.targets.delete(ruleset.name);
242246
}
247+
243248
log(INFO, 'done removing rule');
244249
return true;
245250
},
@@ -305,10 +310,10 @@ RuleSets.prototype = {
305310
var targets = ruletag.getElementsByTagName("target");
306311
for (let target of targets) {
307312
var host = target.getAttribute("host");
308-
if (!(host in this.targets)) {
309-
this.targets[host] = [];
313+
if (!this.targets.has(host)) {
314+
this.targets.set(host, []);
310315
}
311-
this.targets[host].push(rule_set);
316+
this.targets.get(host).push(rule_set);
312317
}
313318
},
314319

@@ -328,9 +333,9 @@ RuleSets.prototype = {
328333

329334
var tmp;
330335
var results = [];
331-
if (this.targets[host]) {
336+
if (this.targets.has(host)) {
332337
// Copy the host targets so we don't modify them.
333-
results = results.concat(this.targets[host]);
338+
results = results.concat(this.targets.get(host));
334339
}
335340

336341
// Ensure host is well-formed (RFC 1035)
@@ -344,14 +349,14 @@ RuleSets.prototype = {
344349
for (let i=0; i < segmented.length; i++) {
345350
let tmp = segmented[i];
346351
segmented[i] = "*";
347-
results = results.concat(this.targets[segmented.join(".")]);
352+
results = results.concat(this.targets.get(segmented.join(".")));
348353
segmented[i] = tmp;
349354
}
350355
// now eat away from the left, with *, so that for x.y.z.google.com we
351356
// check *.z.google.com and *.google.com (we did *.y.z.google.com above)
352357
for (var i = 2; i <= segmented.length - 2; ++i) {
353358
var t = "*." + segmented.slice(i,segmented.length).join(".");
354-
results = results.concat(this.targets[t]);
359+
results = results.concat(this.targets.get(t));
355360
}
356361

357362
// Clean the results list, which may contain duplicates or undefined entries

0 commit comments

Comments
 (0)