Skip to content
This repository was archived by the owner on Nov 6, 2023. It is now read-only.
Merged
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
45 changes: 31 additions & 14 deletions chromium/background-scripts/rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ const trivial_cookie_rule_c = /.+/;
/* A map of all scope RegExp objects */
const scopes = new Map();

/* Returns the scope object from the map for the given scope string */
/**
* Returns the scope object from the map for the given scope string.
* @param {string} scope ruleset scope string
* @returns {RegExp}
*/
function getScope(scope) {
if (!scopes.has(scope)) {
scopes.set(scope, new RegExp(scope));
Expand Down Expand Up @@ -75,9 +79,10 @@ function CookieRule(host, cookiename) {

/**
*A collection of rules
* @param set_name The name of this set
* @param default_state activity state
* @param note Note will be displayed in popup
* @param {string} set_name The name of this set
* @param {boolean} default_state activity state
* @param {string} scope ruleset scope string
* @param {string} note Note will be displayed in popup
* @constructor
*/
function RuleSet(set_name, default_state, scope, note) {
Expand All @@ -87,7 +92,7 @@ function RuleSet(set_name, default_state, scope, note) {
this.cookierules = null;
this.active = default_state;
this.default_state = default_state;
this.scope = scope;
this.scope = getScope(scope);
this.note = note;
}

Expand Down Expand Up @@ -176,7 +181,6 @@ RuleSet.prototype = {

/**
* Initialize Rule Sets
* @param ruleActiveStates default state for rules
* @constructor
*/
function RuleSets() {
Expand All @@ -189,7 +193,10 @@ function RuleSets() {
// A cache for cookie hostnames.
this.cookieHostCache = new Map();

// A hash of rule name -> active status (true/false).
/**
* A hash of rule name -> active status (true/false).
* @type {Object<string, boolean>}
*/
this.ruleActiveStates = {};

// The key to retrieve user rules from the storage api
Expand Down Expand Up @@ -220,6 +227,8 @@ RuleSets.prototype = {

/**
* Convert XML to JS and load rulesets
* @param {Document} ruleXml
* @param {string} scope
*/
addFromXml: function(ruleXml, scope) {
const rulesets_xml = ruleXml.getElementsByTagName("ruleset");
Expand All @@ -232,9 +241,11 @@ RuleSets.prototype = {
this.addFromJson(rulesets, scope);
},

/**
* @param {*} ruleJson
* @param {string} scope
*/
addFromJson: function(ruleJson, scope) {
const scope_obj = getScope(scope);

if (this.wasm_rs) {
this.wasm_rs.add_all_from_js_array(
ruleJson,
Expand All @@ -244,14 +255,19 @@ RuleSets.prototype = {
} else {
for (let ruleset of ruleJson) {
try {
this.parseOneJsonRuleset(ruleset, scope_obj);
this.parseOneJsonRuleset(ruleset, scope);
} catch(e) {
util.log(util.WARN, 'Error processing ruleset:' + e);
}
}
}
},

/**
* Parse one JSON format ruleset element
* @param {*} ruletag
* @param {string} scope
*/
parseOneJsonRuleset: function(ruletag, scope) {
var default_state = true;
var note = "";
Expand Down Expand Up @@ -320,6 +336,7 @@ RuleSets.prototype = {
/**
* Load a user rule
* @param params
* @param {string} scope
* @returns {boolean}
*/
addUserRule : function(params, scope) {
Expand Down Expand Up @@ -398,7 +415,7 @@ RuleSets.prototype = {
loadStoredUserRules: function() {
return this.getStoredUserRules()
.then(userRules => {
this.addFromJson(userRules, getScope());
this.addFromJson(userRules, '');
util.log(util.INFO, `loaded ${userRules.length} stored user rules`);
});
},
Expand All @@ -409,7 +426,7 @@ RuleSets.prototype = {
* @param cb: Callback to call after success/fail
* */
addNewRuleAndStore: async function(params) {
if (this.addUserRule(params, getScope())) {
if (this.addUserRule(params, '')) {
// If we successfully added the user rule, save it in the storage
// api so it's automatically applied when the extension is
// reloaded.
Expand Down Expand Up @@ -466,7 +483,7 @@ RuleSets.prototype = {
},

loadCustomRuleset: function(ruleset_string) {
this.addFromXml((new DOMParser()).parseFromString(ruleset_string, 'text/xml'));
this.addFromXml((new DOMParser()).parseFromString(ruleset_string, 'text/xml'), '');
},

setRuleActiveState: async function(ruleset_name, active) {
Expand Down Expand Up @@ -565,7 +582,7 @@ RuleSets.prototype = {
if (this.wasm_rs) {
let pa = this.wasm_rs.potentially_applicable(host);
results = new Set([...pa].map(ruleset => {
let rs = new RuleSet(ruleset.name, ruleset.default_state, getScope(ruleset.scope), ruleset.note);
let rs = new RuleSet(ruleset.name, ruleset.default_state, ruleset.scope, ruleset.note);

if (ruleset.cookierules) {
let cookierules = ruleset.cookierules.map(cookierule => {
Expand Down
2 changes: 1 addition & 1 deletion chromium/background-scripts/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ async function applyStoredRulesets(rulesets_obj) {
}

if(!replaces) {
rulesets_obj.addFromJson(util.loadExtensionFile('rules/default.rulesets', 'json'));
rulesets_obj.addFromJson(util.loadExtensionFile('rules/default.rulesets', 'json'), '');
}
}

Expand Down