Project

General

Profile

Download (3.23 KB) Statistics
| Branch: | Tag: | Revision:

haketilo / background / page_actions_server.js @ 6247f163

1
/**
2
 * Hachette serving of page actions to content scripts
3
 *
4
 * Copyright (C) 2021 Wojtek Kosior
5
 * Redistribution terms are gathered in the `copyright' file.
6
 */
7

    
8
/*
9
 * IMPORTS_START
10
 * IMPORT get_storage
11
 * IMPORT light_storage
12
 * IMPORT TYPE_PREFIX
13
 * IMPORT CONNECTION_TYPE
14
 * IMPORT browser
15
 * IMPORT listen_for_connection
16
 * IMPORT sha256
17
 * IMPORT query_best
18
 * IMPORT make_ajax_request
19
 * IMPORTS_END
20
 */
21

    
22
var storage;
23
var handler;
24
let policy_observable;
25

    
26
function send_actions(url, port)
27
{
28
    let [pattern, settings] = query_best(storage, url);
29
    if (!settings)
30
	settings = {allow: policy_observable && policy_observable.value};
31
    const repos = storage.get_all(TYPE_PREFIX.REPO);
32

    
33
    port.postMessage(["settings", [pattern, settings, repos]]);
34

    
35
    let components = settings.components;
36
    let processed_bags = new Set();
37

    
38
    if (components !== undefined)
39
	send_scripts([components], port, processed_bags);
40
}
41

    
42
// TODO: parallelize script fetching
43
async function send_scripts(components, port, processed_bags)
44
{
45
    for (let [prefix, name] of components) {
46
	if (prefix === TYPE_PREFIX.BAG) {
47
	    if (processed_bags.has(name)) {
48
		console.log(`preventing recursive inclusion of bag ${name}`);
49
		continue;
50
	    }
51

    
52
	    var bag = storage.get(TYPE_PREFIX.BAG, name);
53

    
54
	    if (bag === undefined) {
55
		console.log(`no bag in storage for key ${name}`);
56
		continue;
57
	    }
58

    
59
	    processed_bags.add(name);
60
	    await send_scripts(bag, port, processed_bags);
61

    
62
	    processed_bags.delete(name);
63
	} else {
64
	    let script_text = await get_script_text(name);
65
	    if (script_text === undefined)
66
		continue;
67

    
68
	    port.postMessage(["inject", [script_text]]);
69
	}
70
    }
71
}
72

    
73
async function get_script_text(script_name)
74
{
75
    try {
76
	let script_data = storage.get(TYPE_PREFIX.SCRIPT, script_name);
77
	if (script_data === undefined) {
78
	    console.log(`missing data for ${script_name}`);
79
	    return;
80
	}
81
	let script_text = script_data.text;
82
	if (!script_text)
83
	    script_text = await fetch_remote_script(script_data);
84
	return script_text;
85
    } catch (e) {
86
	console.log(e);
87
    }
88
}
89

    
90
async function fetch_remote_script(script_data)
91
{
92
    try {
93
	let xhttp = await make_ajax_request("GET", script_data.url);
94
	if (xhttp.status === 200) {
95
	    let computed_hash = sha256(xhttp.responseText);
96
	    if (computed_hash !== script_data.hash) {
97
		console.log(`Bad hash for ${script_data.url}\n    got ${computed_hash} instead of ${script_data.hash}`);
98
		return;
99
	    }
100
	    return xhttp.responseText;
101
	} else {
102
	    console.log("script not fetched: " + script_data.url);
103
	    return;
104
	}
105
    } catch (e) {
106
	console.log(e);
107
    }
108
}
109

    
110
function handle_message(port, message, handler)
111
{
112
    port.onMessage.removeListener(handler[0]);
113
    let url = message.url;
114
    console.log({url});
115
    send_actions(url, port);
116
}
117

    
118
function new_connection(port)
119
{
120
    console.log("new page actions connection!");
121
    let handler = [];
122
    handler.push(m => handle_message(port, m, handler));
123
    port.onMessage.addListener(handler[0]);
124
}
125

    
126
async function start_page_actions_server()
127
{
128
    storage = await get_storage();
129

    
130
    listen_for_connection(CONNECTION_TYPE.PAGE_ACTIONS, new_connection);
131

    
132
    policy_observable = await light_storage.observe_var("default_allow");
133
}
134

    
135
/*
136
 * EXPORTS_START
137
 * EXPORT start_page_actions_server
138
 * EXPORTS_END
139
 */
(3-3/7)