diff options
Diffstat (limited to 'background')
-rw-r--r-- | background/main.js | 2 | ||||
-rw-r--r-- | background/page_actions_server.js | 31 | ||||
-rw-r--r-- | background/page_info_server.js | 74 | ||||
-rw-r--r-- | background/policy_injector.js | 7 | ||||
-rw-r--r-- | background/settings_query.js | 65 | ||||
-rw-r--r-- | background/storage.js | 28 | ||||
-rw-r--r-- | background/storage_server.js | 12 |
7 files changed, 24 insertions, 195 deletions
diff --git a/background/main.js b/background/main.js index ffa814e..7c50fd5 100644 --- a/background/main.js +++ b/background/main.js @@ -12,7 +12,6 @@ * IMPORT start_storage_server * IMPORT start_page_actions_server * IMPORT start_policy_injector - * IMPORT start_page_info_server * IMPORT browser * IMPORTS_END */ @@ -20,7 +19,6 @@ start_storage_server(); start_page_actions_server(); start_policy_injector(); -start_page_info_server(); async function init_ext(install_details) { diff --git a/background/page_actions_server.js b/background/page_actions_server.js index c6800a0..58a0073 100644 --- a/background/page_actions_server.js +++ b/background/page_actions_server.js @@ -13,19 +13,20 @@ * IMPORT browser * IMPORT listen_for_connection * IMPORT sha256 - * IMPORT get_query_best + * IMPORT query_best + * IMPORT make_ajax_request * IMPORTS_END */ var storage; -var query_best; var handler; function send_actions(url, port) { - let [pattern, settings] = query_best(url); + const [pattern, settings] = query_best(storage, url); + const repos = storage.get_all(TYPE_PREFIX.REPO); - port.postMessage(["settings", [pattern, settings]]); + port.postMessage(["settings", [pattern, settings, repos]]); if (settings === undefined) return; @@ -85,27 +86,6 @@ async function get_script_text(script_name) } } -function ajax_callback() -{ - if (this.readyState == 4) - this.resolve_callback(this); -} - -function initiate_ajax_request(resolve, method, url) -{ - var xhttp = new XMLHttpRequest(); - xhttp.resolve_callback = resolve; - xhttp.onreadystatechange = ajax_callback; - xhttp.open(method, url, true); - xhttp.send(); -} - -function make_ajax_request(method, url) -{ - return new Promise((resolve, reject) => - initiate_ajax_request(resolve, method, url)); -} - async function fetch_remote_script(script_data) { try { @@ -145,7 +125,6 @@ function new_connection(port) async function start_page_actions_server() { storage = await get_storage(); - query_best = await get_query_best(); listen_for_connection(CONNECTION_TYPE.PAGE_ACTIONS, new_connection); } diff --git a/background/page_info_server.js b/background/page_info_server.js deleted file mode 100644 index 6f02750..0000000 --- a/background/page_info_server.js +++ /dev/null @@ -1,74 +0,0 @@ -/** - * part of Hachette - * Serving of storage data corresponding to requested urls (server side). - * - * Copyright (C) 2021 Wojtek Kosior - * Redistribution terms are gathered in the `copyright' file. - */ - -/* - * IMPORTS_START - * IMPORT listen_for_connection - * IMPORT get_storage - * IMPORT get_query_all - * IMPORT TYPE_PREFIX - * IMPORT CONNECTION_TYPE - * IMPORT url_matches - * IMPORTS_END - */ - -var storage; -var query_all; - -function handle_change(connection_data, change) -{ - if (change.prefix !== TYPE_PREFIX.PAGE) - return; - - connection_data.port.postMessage(["change", change]); -} - -async function handle_subscription(connection_data, message) -{ - const [action, url] = message; - if (action === "unsubscribe") { - connection_data.subscribed.delete(url); - return; - } - - connection_data.subscribed.add(url); - connection_data.port.postMessage(["new_url", query_all(url)]); -} - -function new_connection(port) -{ - console.log("new page info connection!"); - - const connection_data = { - subscribed : new Set(), - port - }; - - let _handle_change = change => handle_change(connection_data, change); - - storage.add_change_listener(_handle_change); - - port.onMessage.addListener(m => handle_subscription(connection_data, m)); - port.onDisconnect.addListener( - () => storage.remove_change_listener(_handle_change) - ); -} - -async function start_page_info_server() -{ - storage = await get_storage(); - query_all = await get_query_all(); - - listen_for_connection(CONNECTION_TYPE.PAGE_INFO, new_connection); -} - -/* - * EXPORTS_START - * EXPORT start_page_info_server - * EXPORTS_END - */ diff --git a/background/policy_injector.js b/background/policy_injector.js index 80a0e3b..9725e99 100644 --- a/background/policy_injector.js +++ b/background/policy_injector.js @@ -16,15 +16,15 @@ * IMPORT gen_unique * IMPORT gen_nonce * IMPORT is_privileged_url + * IMPORT url_item * IMPORT url_extract_target * IMPORT sign_policy - * IMPORT get_query_best + * IMPORT query_best * IMPORT sanitize_csp_header * IMPORTS_END */ var storage; -var query_best; const csp_header_names = new Set([ "content-security-policy", @@ -56,7 +56,7 @@ function url_inject(details) if (targets.policy) targets.target = ""; - let [pattern, settings] = query_best(targets.base_url); + let [pattern, settings] = query_best(storage, targets.base_url); /* Defaults */ if (!pattern) settings = {}; @@ -162,7 +162,6 @@ function headers_inject(details) async function start_policy_injector() { storage = await get_storage(); - query_best = await get_query_best(); let extra_opts = ["blocking", "responseHeaders"]; if (is_chrome) diff --git a/background/settings_query.js b/background/settings_query.js deleted file mode 100644 index d0d9511..0000000 --- a/background/settings_query.js +++ /dev/null @@ -1,65 +0,0 @@ -/** - * Hachette querying page settings with regard to wildcard records - * - * Copyright (C) 2021 Wojtek Kosior - * Redistribution terms are gathered in the `copyright' file. - */ - -/* - * IMPORTS_START - * IMPORT make_once - * IMPORT get_storage - * IMPORT TYPE_PREFIX - * IMPORT for_each_possible_pattern - * IMPORTS_END - */ - -var storage; - -async function init(fun) -{ - storage = await get_storage(); - - return fun; -} - -function check_pattern(pattern, multiple, matched) -{ - const settings = storage.get(TYPE_PREFIX.PAGE, pattern); - - if (settings === undefined) - return; - - matched.push([pattern, settings]); - - if (!multiple) - return false; -} - -function query(url, multiple) -{ - const matched = []; - for_each_possible_pattern(url, p => check_pattern(p, multiple, matched)); - - return multiple ? matched : (matched[0] || [undefined, undefined]); -} - -function query_best(url) -{ - return query(url, false); -} - -function query_all(url) -{ - return query(url, true); -} - -const get_query_best = make_once(() => init(query_best)); -const get_query_all = make_once(() => init(query_all)); - -/* - * EXPORTS_START - * EXPORT get_query_best - * EXPORT get_query_all - * EXPORTS_END - */ diff --git a/background/storage.js b/background/storage.js index 7229a2d..c2160b0 100644 --- a/background/storage.js +++ b/background/storage.js @@ -16,6 +16,7 @@ * IMPORT make_once * IMPORT browser * IMPORT is_chrome + * IMPORT observables * IMPORTS_END */ @@ -98,13 +99,10 @@ async function list(prefix) for (let item of await get_list_var(name)) map.set(item, await get(prefix + item)); - return {map, prefix, name, listeners : new Set(), lock : make_lock()}; + return {map, prefix, name, observable: observables.make(), + lock: make_lock()}; } -var pages; -var bags; -var scripts; - var list_by_prefix = {}; async function init() @@ -125,7 +123,7 @@ exports.add_change_listener = function (cb, prefixes=list_prefixes) prefixes = [prefixes]; for (let prefix of prefixes) - list_by_prefix[prefix].listeners.add(cb); + observables.subscribe(list_by_prefix[prefix].observable, cb); } exports.remove_change_listener = function (cb, prefixes=list_prefixes) @@ -134,13 +132,7 @@ exports.remove_change_listener = function (cb, prefixes=list_prefixes) prefixes = [prefixes]; for (let prefix of prefixes) - list_by_prefix[prefix].listeners.delete(cb); -} - -function broadcast_change(change, list) -{ - for (let listener_callback of list.listeners) - listener_callback(change); + observables.unsubscribe(list_by_prefix[prefix].observable, cb); } /* Prepare some hepler functions to get elements of a list */ @@ -202,7 +194,7 @@ async function _set_item(item, value, list) new_val : value }; - broadcast_change(change, list); + observables.broadcast(list.observable, change); return old_val; } @@ -240,7 +232,7 @@ async function _remove_item(item, list) new_val : undefined }; - broadcast_change(change, list); + observables.broadcast(list.observable, change); return old_val; } @@ -286,7 +278,7 @@ async function _replace_item(old_item, new_item, list, new_val=undefined) new_val : undefined }; - broadcast_change(change, list); + observables.broadcast(list.observable, change); list.map.set(new_item, new_val); @@ -294,7 +286,7 @@ async function _replace_item(old_item, new_item, list, new_val=undefined) change.old_val = undefined; change.new_val = new_val; - broadcast_change(change, list); + observables.broadcast(list.observable, change); return old_val; } @@ -379,7 +371,7 @@ exports.clear = async function () for (let [item, val] of list_entries_it(list)) { change.item = item; change.old_val = val; - broadcast_change(change, list); + observables.broadcast(list.observable, change); } list.map = new Map(); diff --git a/background/storage_server.js b/background/storage_server.js index 554aff2..2252eb5 100644 --- a/background/storage_server.js +++ b/background/storage_server.js @@ -9,7 +9,7 @@ * IMPORTS_START * IMPORT listen_for_connection * IMPORT get_storage - * IMPORT TYPE_PREFIX + * IMPORT list_prefixes * IMPORT CONNECTION_TYPE * IMPORTS_END */ @@ -38,11 +38,11 @@ function new_connection(port) { console.log("new remote storage connection!"); - port.postMessage({ - [TYPE_PREFIX.SCRIPT] : storage.get_all(TYPE_PREFIX.SCRIPT), - [TYPE_PREFIX.BAG] : storage.get_all(TYPE_PREFIX.BAG), - [TYPE_PREFIX.PAGE] : storage.get_all(TYPE_PREFIX.PAGE) - }); + const message = {}; + for (const prefix of list_prefixes) + message[prefix] = storage.get_all(prefix); + + port.postMessage(message); let handle_change = change => port.postMessage(change); |