|
89 | 89 | .join(';') |
90 | 90 | }; script-src-elem 'none'; report-to noscript-reports-dnr`; // see /content/content.js securitypolicyviolation handler |
91 | 91 |
|
92 | | - let updatingSemaphore; |
| 92 | + browser.runtime.onStartup.addListener(async () => { |
| 93 | + const updatedTabs = autoAllow(await browser.tabs.query({}), true); |
| 94 | + console.debug("DNRPolicy startup updated tabs", updatedTabs); // DEV_ONLY |
| 95 | + if (updatedTabs.length) { |
| 96 | + await Promise.allSettled([ |
| 97 | + ns.saveSession(), |
| 98 | + RequestGuard.DNRPolicy.update(), |
| 99 | + ]); |
| 100 | + for (const tab of updatedTabs) { |
| 101 | + if (tab.status != "unloaded") { |
| 102 | + browser.tabs.reload(tab.id); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + }); |
93 | 107 |
|
94 | 108 | async function update() { |
95 | | - await updatingSemaphore; |
96 | | - |
97 | 109 | const {policy} = ns; |
98 | 110 | if (policy === _lastPolicy) { |
99 | 111 | if (!policy || policy.equals(_lastPolicy)) { |
|
161 | 173 | addRules: Rules[ruleType], |
162 | 174 | removeRuleIds, |
163 | 175 | }); |
164 | | - console.debug(`DNRPolicy ${Rules[ruleType].length} ${ruleType} rules updated in ${Date.now() - ts}ms`); // DEV_ONLY |
| 176 | + const newRules = (await browser.declarativeNetRequest[`get${ruleType}Rules`]()).filter(r => r.priority <= MAX_PRIORITY); // DEV_ONLY |
| 177 | + debug(`DNRPolicy ${Rules[ruleType].length} ${ruleType} rules updated in ${Date.now() - ts}ms`, newRules); // DEV_ONLY |
165 | 178 | } catch (e) { |
166 | 179 | console.error(e, `Failed to update DNRPolicy ${ruleType}rules %o - remove %o, add %o`, Rules[ruleType], addRules, removeRuleIds); |
167 | 180 | } |
168 | 181 | }))); |
169 | | - console.debug(`All DNRPolicy rules updated in ${Date.now() - ts}ms`); // DEV_ONLY |
| 182 | + debug(`All DNRPolicy rules updated in ${Date.now() - ts}ms`); // DEV_ONLY |
170 | 183 | } |
171 | 184 |
|
172 | 185 | async function addTabRules(rules = []) { |
|
266 | 279 | } |
267 | 280 | } |
268 | 281 |
|
| 282 | + function autoAllow(tabs, isStartup = false) { |
| 283 | + const {policy} = ns; |
| 284 | + const updated = []; |
| 285 | + if (policy.autoAllowTop) { |
| 286 | + for(const tab of tabs) { |
| 287 | + const {url} = tab; |
| 288 | + if (Sites.isInternal(url)) continue; |
| 289 | + const perms = policy.get(url).perms; |
| 290 | + console.debug("DNRPolicy autoAllow check", tab, perms, perms === policy.DEFAULT); |
| 291 | + if (perms === policy.DEFAULT || (isStartup && perms.temp)) { |
| 292 | + policy.set(Sites.optimalKey(url), policy.TRUSTED.tempTwin); |
| 293 | + updated.push(tab); |
| 294 | + } |
| 295 | + } |
| 296 | + updated.length && console.debug("DNRPolicy.autoAllow", updated); // DEV_ONLY |
| 297 | + } |
| 298 | + return updated; |
| 299 | + } |
| 300 | + |
| 301 | + let updatingSemaphore; |
| 302 | + |
269 | 303 | RequestGuard.DNRPolicy = { |
270 | 304 | async update() { |
271 | 305 | await updatingSemaphore; |
272 | | - updatingSemaphore = await update(); |
| 306 | + return await (updatingSemaphore = update()); |
273 | 307 | }, |
274 | 308 | async updateTabs() { |
275 | 309 | await updatingSemaphore; |
276 | | - updatingSemaphore = await updateTabs(); |
| 310 | + return await (updatingSemaphore = updateTabs()); |
277 | 311 | } |
278 | | - } |
| 312 | + }; |
279 | 313 |
|
280 | 314 | browser.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => { |
281 | 315 | if (changeInfo.url) { |
282 | 316 | // TODO: see if the update can be made more granular |
283 | | - await RequestGuard.DNRPolicy.updateTabs(); |
| 317 | + (await (autoAllow([tab])).length |
| 318 | + ? Promise.allSettled([RequestGuard.DNRPolicy.update(), ns.saveSession()]) |
| 319 | + : RequestGuard.DNRPolicy.updateTabs() |
| 320 | + ); |
284 | 321 | } |
285 | 322 | }); |
286 | 323 |
|
|
0 commit comments