Optimize serviceWorker tracking for heavy tabs usage (thanks vadimm and barbaz for investigation).

This commit is contained in:
hackademix 2020-12-18 20:47:55 +01:00
parent a61481919a
commit b1d96e3033
1 changed files with 16 additions and 2 deletions

View File

@ -24,6 +24,7 @@ var RequestGuard = (() => {
Object.assign(policyTypesMap, {"webgl": "webgl"}); // fake types Object.assign(policyTypesMap, {"webgl": "webgl"}); // fake types
const TabStatus = { const TabStatus = {
map: new Map(), map: new Map(),
_originsCache: new Map(),
types: ["script", "object", "media", "frame", "font"], types: ["script", "object", "media", "frame", "font"],
newRecords() { newRecords() {
return { return {
@ -37,6 +38,17 @@ var RequestGuard = (() => {
let records = this.map.get(tabId); let records = this.map.get(tabId);
return records && records.origins.has(origin); return records && records.origins.has(origin);
}, },
findTabsByOrigin(origin) {
let tabIds = this._originsCache.get(origin);
if (!tabIds) {
tabIds = [];
for(let [tabId, {origins}] of [...this.map]) {
if (origins.has(origin)) tabIds.push(tabId);
}
this._originsCache.set(origin, tabIds);
}
return tabIds;
},
initTab(tabId, records = this.newRecords()) { initTab(tabId, records = this.newRecords()) {
if (tabId < 0) return; if (tabId < 0) return;
this.map.set(tabId, records); this.map.set(tabId, records);
@ -59,6 +71,7 @@ var RequestGuard = (() => {
} }
if (type.endsWith("frame")) { if (type.endsWith("frame")) {
records.origins.add(Sites.origin(url)); records.origins.add(Sites.origin(url));
TabStatus._originsCache.clear();
} }
let collection = records[what]; let collection = records[what];
if (collection) { if (collection) {
@ -163,6 +176,7 @@ var RequestGuard = (() => {
}, },
onRemovedTab(tabId) { onRemovedTab(tabId) {
TabStatus.map.delete(tabId); TabStatus.map.delete(tabId);
TabStatus._originsCache.clear();
}, },
} }
browser.tabs.onActivated.addListener(TabStatus.onActivatedTab); browser.tabs.onActivated.addListener(TabStatus.onActivatedTab);
@ -245,8 +259,8 @@ var RequestGuard = (() => {
// service worker / importScripts()? // service worker / importScripts()?
let payload = {request, allowed, policyType, serviceWorker: Sites.origin(documentUrl)}; let payload = {request, allowed, policyType, serviceWorker: Sites.origin(documentUrl)};
let recipient = {frameId: 0}; let recipient = {frameId: 0};
for (let tab of await browser.tabs.query({url: ["http://*/*", "https://*/*"]})) { for (let tabId of TabStatus.findTabsByOrigin(payload.serviceWorker)) {
recipient.tabId = tab.id; recipient.tabId = tabId;
try { try {
Messages.send("seen", payload, recipient); Messages.send("seen", payload, recipient);
} catch (e) { } catch (e) {