Project

General

Profile

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

haketilo / background / patterns_query_manager.js @ cf838016

1
/**
2
 * This file is part of Haketilo.
3
 *
4
 * Function: Instantiate the Pattern Tree data structure, filled with mappings
5
 *           from IndexedDB.
6
 *
7
 * Copyright (C) 2021, 2022 Wojtek Kosior
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation, either version 3 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * As additional permission under GNU GPL version 3 section 7, you
20
 * may distribute forms of that code without the copy of the GNU
21
 * GPL normally required by section 4, provided you include this
22
 * license notice and, in case of non-source distribution, a URL
23
 * through which recipients can access the Corresponding Source.
24
 * If you modify file(s) with this exception, you may extend this
25
 * exception to your version of the file(s), but you are not
26
 * obligated to do so. If you do not wish to do so, delete this
27
 * exception statement from your version.
28
 *
29
 * As a special exception to the GPL, any HTML file which merely
30
 * makes function calls to this code, and for that purpose
31
 * includes it by reference shall be deemed a separate work for
32
 * copyright law purposes. If you modify this code, you may extend
33
 * this exception to your version of the code, but you are not
34
 * obligated to do so. If you do not wish to do so, delete this
35
 * exception statement from your version.
36
 *
37
 * You should have received a copy of the GNU General Public License
38
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
39
 *
40
 * I, Wojtek Kosior, thereby promise not to sue for violation of this file's
41
 * license. Although I request that you do not make use of this code in a
42
 * proprietary program, I am not going to enforce this in court.
43
 */
44

    
45
#IMPORT common/patterns_query_tree.js  AS pqt
46
#IMPORT common/indexeddb.js            AS haketilodb
47

    
48
#IF MOZILLA || MV3
49
#FROM common/browser.js IMPORT browser
50
#ENDIF
51

    
52
let default_allow = {};
53
#EXPORT default_allow
54

    
55
let secret;
56

    
57
const tree = pqt.make();
58
#EXPORT tree
59

    
60
const currently_registered = new Map();
61

    
62
#IF MOZILLA || MV3
63
let registered_script = null;
64
let script_update_occuring = false;
65
let script_update_needed;
66

    
67
async function update_content_script() {
68
    if (script_update_occuring)
69
	return;
70

    
71
    script_update_occuring = true;
72

    
73
    while (script_update_needed) {
74
	script_update_needed = false;
75

    
76
	const code = `\
77
this.haketilo_secret        = ${JSON.stringify(secret)};
78
this.haketilo_pattern_tree  = ${JSON.stringify(tree)};
79
this.haketilo_default_allow = ${JSON.stringify(default_allow.value)};
80
if (this.haketilo_content_script_main)
81
    haketilo_content_script_main();`;
82

    
83
	const new_script = await browser.contentScripts.register({
84
	    "js": [{code}],
85
	    "matches": ["<all_urls>"],
86
	    "allFrames": true,
87
	    "runAt": "document_start"
88
	});
89

    
90
	if (registered_script)
91
	    registered_script.unregister();
92

    
93
	registered_script = new_script;
94
    }
95

    
96
    script_update_occuring = false;
97
}
98
#ENDIF
99

    
100
function register(kind, object) {
101
    if (kind === "mappings") {
102
	for (const [pattern, resource] of Object.entries(object.payloads || {}))
103
	    pqt.register(tree, pattern, object.identifier, resource);
104
    } else /* if (kind === "blocking") */ {
105
	/*
106
	 * All simple block/allow rules use "~allow" in place of mapping id.
107
	 * This way it won't collide with any real mapping id and will always
108
	 * be sorted as higher value than mapping ids.
109
	 */
110
	pqt.register(tree, object.pattern, "~allow", object.allow + 0);
111
    }
112

    
113
    const id = kind === "mappings" ? object.identifier : object.pattern;
114
    currently_registered.set(id, object);
115
}
116

    
117
function changed(kind, change) {
118
    const old_version = currently_registered.get(change.key);
119
    if (old_version !== undefined) {
120
	if (kind === "mappings") {
121
	    for (const pattern in old_version.payloads || {})
122
		pqt.deregister(tree, pattern, change.key);
123
	} else /* if (kind === "blocking") */ {
124
	    pqt.deregister(tree, change.key, "~allow");
125
	}
126

    
127
	currently_registered.delete(change.key);
128
    }
129

    
130
    if (change.new_val !== undefined)
131
	register(kind, change.new_val);
132

    
133
#IF MOZILLA || MV3
134
    script_update_needed = true;
135
    setTimeout(update_content_script, 0);
136
#ENDIF
137
}
138

    
139
function setting_changed(change) {
140
    if (change.key !== "default_allow")
141
	return;
142

    
143
    default_allow.value = (change.new_val || {}).value;
144

    
145
#IF MOZILLA || MV3
146
    script_update_needed = true;
147
    setTimeout(update_content_script, 0);
148
#ENDIF
149
}
150

    
151
async function start(secret_) {
152
    secret = secret_;
153

    
154
    const [mapping_tracking, initial_mappings] =
155
	  await haketilodb.track.mapping(ch => changed("mappings", ch));
156
    const [blocking_tracking, initial_blocking] =
157
	  await haketilodb.track.blocking(ch => changed("blocking", ch));
158

    
159
    initial_mappings.forEach(m => register("mappings", m));
160
    initial_blocking.forEach(b => register("blocking", b));
161

    
162
    const [setting_tracking, initial_settings] =
163
	  await haketilodb.track.settings(setting_changed);
164

    
165
    for (const setting of initial_settings) {
166
	if (setting.name === "default_allow")
167
	    Object.assign(default_allow, setting);
168
    }
169

    
170
#IF MOZILLA || MV3
171
    script_update_needed = true;
172
    await update_content_script();
173
#ENDIF
174
}
175
#EXPORT start
(5-5/7)