1
|
/**
|
2
|
* This file is part of Haketilo.
|
3
|
*
|
4
|
* Function: Allow content scripts to query IndexedDB through messages to
|
5
|
* background script.
|
6
|
*
|
7
|
* Copyright (C) 2022 Wojtek Kosior <koszko@koszko.org>
|
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/indexeddb.js AS haketilodb
|
46
|
|
47
|
#FROM common/browser.js IMPORT browser
|
48
|
|
49
|
async function get_resource_files(getting, id) {
|
50
|
if (getting.defs_by_res_id.has(id))
|
51
|
return;
|
52
|
|
53
|
getting.defs_by_res_id.set(id, null);
|
54
|
|
55
|
const definition = await haketilodb.idb_get(getting.tx, "resource", id);
|
56
|
if (!definition)
|
57
|
throw {haketilo_error_type: "missing", id};
|
58
|
|
59
|
getting.defs_by_res_id.set(id, definition);
|
60
|
|
61
|
const file_proms = (definition.scripts || [])
|
62
|
.map(s => haketilodb.idb_get(getting.tx, "files", s.hash_key));
|
63
|
|
64
|
const deps_proms = (definition.dependencies || [])
|
65
|
.map(dep_id => get_resource_files(getting, dep_id));
|
66
|
|
67
|
const files = (await Promise.all(file_proms)).map(f => f.contents);
|
68
|
getting.files_by_res_id.set(id, files);
|
69
|
|
70
|
await Promise.all(deps_proms);
|
71
|
}
|
72
|
|
73
|
function get_files_list(defs_by_res_id, files_by_res_id, root_id) {
|
74
|
const processed = new Set(), to_process = [["start", root_id]],
|
75
|
trace = new Set(), files = [];
|
76
|
|
77
|
while (to_process.length > 0) {
|
78
|
const [what, id] = to_process.pop();
|
79
|
if (what === "end") {
|
80
|
trace.delete(id);
|
81
|
files.push(...files_by_res_id.get(id));
|
82
|
continue;
|
83
|
}
|
84
|
|
85
|
if (trace.has(id))
|
86
|
throw {haketilo_error_type: "circular", id};
|
87
|
|
88
|
if (processed.has(id))
|
89
|
continue;
|
90
|
|
91
|
trace.add(id);
|
92
|
to_process.push(["end", id]);
|
93
|
processed.add(id);
|
94
|
|
95
|
const ds = (defs_by_res_id.get(id).dependencies || []).reverse();
|
96
|
ds.forEach(dep_id => to_process.push(["start", dep_id]));
|
97
|
}
|
98
|
|
99
|
return files;
|
100
|
}
|
101
|
|
102
|
async function send_resource_files(root_resource_id, send_cb) {
|
103
|
const db = await haketilodb.get();
|
104
|
const getting = {
|
105
|
defs_by_res_id: new Map(),
|
106
|
files_by_res_id: new Map(),
|
107
|
tx: db.transaction(["files", "resource"])
|
108
|
};
|
109
|
|
110
|
let prom_cbs, prom = new Promise((...cbs) => prom_cbs = cbs);
|
111
|
|
112
|
getting.tx.onerror = e => prom_cbs[1]({haketilo_error_type: "db", e});
|
113
|
|
114
|
get_resource_files(getting, root_resource_id, new Set()).then(...prom_cbs);
|
115
|
|
116
|
try {
|
117
|
await prom;
|
118
|
const files = get_files_list(
|
119
|
getting.defs_by_res_id,
|
120
|
getting.files_by_res_id,
|
121
|
root_resource_id
|
122
|
);
|
123
|
var to_send = {files};
|
124
|
} catch(e) {
|
125
|
if (typeof e === "object" && "haketilo_error_type" in e) {
|
126
|
if (e.haketilo_error_type === "db") {
|
127
|
console.error(e.e);
|
128
|
delete e.e;
|
129
|
}
|
130
|
var to_send = {error: e};
|
131
|
} else {
|
132
|
console.error(e);
|
133
|
var to_send = {error: {haketilo_error_type: "other"}};
|
134
|
}
|
135
|
}
|
136
|
|
137
|
send_cb(to_send);
|
138
|
}
|
139
|
|
140
|
function on_indexeddb_files_request([type, resource_id], sender, respond_cb) {
|
141
|
if (type !== "indexeddb_files")
|
142
|
return;
|
143
|
|
144
|
send_resource_files(resource_id, respond_cb);
|
145
|
|
146
|
return true;
|
147
|
}
|
148
|
|
149
|
function start() {
|
150
|
browser.runtime.onMessage.addListener(on_indexeddb_files_request);
|
151
|
}
|
152
|
#EXPORT start
|