From 07a883feeeea63cc23fb5100a0618f29b0a5da9f Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Sat, 15 Jan 2022 12:35:47 +0100 Subject: make blocking rules queryable in pattern tree just as mappings are --- background/patterns_query_manager.js | 56 +++++++++++++++++++++++++----------- 1 file changed, 39 insertions(+), 17 deletions(-) (limited to 'background') diff --git a/background/patterns_query_manager.js b/background/patterns_query_manager.js index e657448..8b563ef 100644 --- a/background/patterns_query_manager.js +++ b/background/patterns_query_manager.js @@ -4,7 +4,7 @@ * Function: Instantiate the Pattern Tree data structure, filled with mappings * from IndexedDB. * - * Copyright (C) 2021 Wojtek Kosior + * Copyright (C) 2021,2022 Wojtek Kosior * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -54,7 +54,7 @@ let secret; const tree = pqt.make(); #EXPORT tree -const current_mappings = new Map(); +const currently_registered = new Map(); #IF MOZILLA || MV3 let registered_script = null; @@ -92,28 +92,46 @@ if (this.haketilo_content_script_main) script_update_occuring = false; } +#ENDIF -function register_mapping(mapping) +function register(kind, object) { - for (const [pattern, resource] of Object.entries(mapping.payloads)) - pqt.register(tree, pattern, mapping.identifier, resource); - current_mappings.set(mapping.identifier, mapping); -} + if (kind === "mappings") { + for (const [pattern, resource] of Object.entries(object.payloads)) + pqt.register(tree, pattern, object.identifier, resource); + } else /* if (kind === "blocking") */ { + /* + * All simple block/allow rules use "~allow" in place of mapping id. + * This way it won't collide with any real mapping id and will always + * be sorted as higher value than mapping ids. + */ + pqt.register(tree, object.pattern, "~allow", object.allow + 0); + } + +#IF MOZILLA || MV3 + const id = kind === "mappings" ? object.identifier : object.pattern; + currently_registered.set(id, object); #ENDIF +} -function mapping_changed(change) +function changed(kind, change) { - console.log('mapping changes!', arguments); - const old_version = current_mappings.get(change.key); + const old_version = currently_registered.get(change.key); if (old_version !== undefined) { - for (const pattern in old_version.payloads) - pqt.deregister(tree, pattern, change.key); + if (kind === "mappings") { + for (const pattern in old_version.payloads) + pqt.deregister(tree, pattern, change.key); + } else /* if (kind === "blocking") */ { + pqt.deregister(tree, change.key, "~allow"); + } - current_mappings.delete(change.key); +#IF MOZILLA || MV3 + currently_registered.delete(change.key); +#ENDIF } if (change.new_val !== undefined) - register_mapping(change.new_val); + register(kind, change.new_val); #IF MOZILLA || MV3 script_update_needed = true; @@ -125,10 +143,14 @@ async function start(secret_) { secret = secret_; - const [tracking, initial_mappings] = - await haketilodb.track.mappings(mapping_changed); + const [mapping_tracking, initial_mappings] = + await haketilodb.track.mappings(ch => changed("mappings", ch)); + const [blocking_tracking, initial_blocking] = + await haketilodb.track.blocking(ch => changed("blocking", ch)); + + initial_mappings.forEach(m => register("mappings", m)); + initial_blocking.forEach(b => register("blocking", b)); - initial_mappings.forEach(register_mapping); #IF MOZILLA || MV3 script_update_needed = true; await update_content_script(); -- cgit v1.2.3