Project

General

Profile

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

haketilo / background / patterns_query_manager.js @ 7218849a

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 secret;
53

    
54
const tree = pqt.make();
55
#EXPORT tree
56

    
57
const currently_registered = new Map();
58

    
59
#IF MOZILLA || MV3
60
let registered_script = null;
61
let script_update_occuring = false;
62
let script_update_needed;
63

    
64
async function update_content_script()
65
{
66
    if (script_update_occuring)
67
	return;
68

    
69
    script_update_occuring = true;
70

    
71
    while (script_update_needed) {
72
	script_update_needed = false;
73

    
74
	const code = `\
75
this.haketilo_secret       = ${secret};
76
this.haketilo_pattern_tree = ${JSON.stringify(tree)};
77
if (this.haketilo_content_script_main)
78
    haketilo_content_script_main();`;
79

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

    
87
	if (registered_script)
88
	    registered_script.unregister();
89

    
90
	registered_script = new_script;
91
    }
92

    
93
    script_update_occuring = false;
94
}
95
#ENDIF
96

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

    
111
#IF MOZILLA || MV3
112
    const id = kind === "mappings" ? object.identifier : object.pattern;
113
    currently_registered.set(id, object);
114
#ENDIF
115
}
116

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

    
128
#IF MOZILLA || MV3
129
	currently_registered.delete(change.key);
130
#ENDIF
131
    }
132

    
133
    if (change.new_val !== undefined)
134
	register(kind, change.new_val);
135

    
136
#IF MOZILLA || MV3
137
    script_update_needed = true;
138
    setTimeout(update_content_script, 0);
139
#ENDIF
140
}
141

    
142
async function start(secret_)
143
{
144
    secret = secret_;
145

    
146
    const [mapping_tracking, initial_mappings] =
147
	  await haketilodb.track.mapping(ch => changed("mappings", ch));
148
    const [blocking_tracking, initial_blocking] =
149
	  await haketilodb.track.blocking(ch => changed("blocking", ch));
150

    
151
    initial_mappings.forEach(m => register("mappings", m));
152
    initial_blocking.forEach(b => register("blocking", b));
153

    
154
#IF MOZILLA || MV3
155
    script_update_needed = true;
156
    await update_content_script();
157
#ENDIF
158
}
159
#EXPORT start
(5-5/10)