Skip to content

Commit 1211809

Browse files
committed
Improved startup behavior.
1 parent db18c27 commit 1211809

File tree

3 files changed

+60
-15
lines changed

3 files changed

+60
-15
lines changed

src/bg/DNRPolicy.js

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,23 @@
8989
.join(';')
9090
}; script-src-elem 'none'; report-to noscript-reports-dnr`; // see /content/content.js securitypolicyviolation handler
9191

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+
});
93107

94108
async function update() {
95-
await updatingSemaphore;
96-
97109
const {policy} = ns;
98110
if (policy === _lastPolicy) {
99111
if (!policy || policy.equals(_lastPolicy)) {
@@ -161,12 +173,13 @@
161173
addRules: Rules[ruleType],
162174
removeRuleIds,
163175
});
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
165178
} catch (e) {
166179
console.error(e, `Failed to update DNRPolicy ${ruleType}rules %o - remove %o, add %o`, Rules[ruleType], addRules, removeRuleIds);
167180
}
168181
})));
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
170183
}
171184

172185
async function addTabRules(rules = []) {
@@ -266,21 +279,45 @@
266279
}
267280
}
268281

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+
269303
RequestGuard.DNRPolicy = {
270304
async update() {
271305
await updatingSemaphore;
272-
updatingSemaphore = await update();
306+
return await (updatingSemaphore = update());
273307
},
274308
async updateTabs() {
275309
await updatingSemaphore;
276-
updatingSemaphore = await updateTabs();
310+
return await (updatingSemaphore = updateTabs());
277311
}
278-
}
312+
};
279313

280314
browser.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => {
281315
if (changeInfo.url) {
282316
// 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+
);
284321
}
285322
});
286323

src/bg/main.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,10 @@
111111
if (!isTorBrowser) {
112112
await include("/nscl/service/prefetchCSSResources.js");
113113
}
114-
await TabGuard.wakening;
115-
114+
await Promise.allSettled([
115+
TabGuard.wakening,
116+
RequestGuard.DNRPolicy?.update(),
117+
]);
116118
try {
117119
await Messages.send("started");
118120
} catch (e) {
@@ -283,8 +285,9 @@
283285
unrestrictedTabs: new Set(),
284286
async toggleTabRestrictions(tabId, restrict = ns.unrestrictedTabs.has(tabId)) {
285287
ns.unrestrictedTabs[restrict ? "delete": "add"](tabId);
286-
Promise.allSettled([session.save(),
287-
RequestGuard.DNRPolicy?.updateTabs()
288+
await Promise.allSettled([
289+
session.save(),
290+
RequestGuard.DNRPolicy?.updateTabs(),
288291
]);
289292
},
290293
isEnforced(tabId = -1) {
@@ -342,7 +345,12 @@
342345
if (isTop) {
343346
if (policy.autoAllowTop && perms === policy.DEFAULT) {
344347
policy.set(Sites.optimalKey(url), perms = policy.TRUSTED.tempTwin);
345-
await RequestGuard.DNRPolicy?.update();
348+
await Promise.allSettled([
349+
RequestGuard.DNRPolicy?.update(),
350+
session.save(),
351+
]);
352+
const dnrRules = (await browser.declarativeNetRequest.getSessionRules()).filter(r => r?.action?.type == "allow"); // DEV_ONLY
353+
debug(`Auto-trusted ${Sites.optimalKey(url)}`, dnrRules); // DEV_ONLY
346354
}
347355
} else {
348356
cascaded = topUrl && ns.sync.cascadeRestrictions;

0 commit comments

Comments
 (0)