Skip to content

Commit b1d96e3

Browse files
committed
Optimize serviceWorker tracking for heavy tabs usage (thanks vadimm and barbaz for investigation).
1 parent a614819 commit b1d96e3

1 file changed

Lines changed: 16 additions & 2 deletions

File tree

src/bg/RequestGuard.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ var RequestGuard = (() => {
2424
Object.assign(policyTypesMap, {"webgl": "webgl"}); // fake types
2525
const TabStatus = {
2626
map: new Map(),
27+
_originsCache: new Map(),
2728
types: ["script", "object", "media", "frame", "font"],
2829
newRecords() {
2930
return {
@@ -37,6 +38,17 @@ var RequestGuard = (() => {
3738
let records = this.map.get(tabId);
3839
return records && records.origins.has(origin);
3940
},
41+
findTabsByOrigin(origin) {
42+
let tabIds = this._originsCache.get(origin);
43+
if (!tabIds) {
44+
tabIds = [];
45+
for(let [tabId, {origins}] of [...this.map]) {
46+
if (origins.has(origin)) tabIds.push(tabId);
47+
}
48+
this._originsCache.set(origin, tabIds);
49+
}
50+
return tabIds;
51+
},
4052
initTab(tabId, records = this.newRecords()) {
4153
if (tabId < 0) return;
4254
this.map.set(tabId, records);
@@ -59,6 +71,7 @@ var RequestGuard = (() => {
5971
}
6072
if (type.endsWith("frame")) {
6173
records.origins.add(Sites.origin(url));
74+
TabStatus._originsCache.clear();
6275
}
6376
let collection = records[what];
6477
if (collection) {
@@ -163,6 +176,7 @@ var RequestGuard = (() => {
163176
},
164177
onRemovedTab(tabId) {
165178
TabStatus.map.delete(tabId);
179+
TabStatus._originsCache.clear();
166180
},
167181
}
168182
browser.tabs.onActivated.addListener(TabStatus.onActivatedTab);
@@ -245,8 +259,8 @@ var RequestGuard = (() => {
245259
// service worker / importScripts()?
246260
let payload = {request, allowed, policyType, serviceWorker: Sites.origin(documentUrl)};
247261
let recipient = {frameId: 0};
248-
for (let tab of await browser.tabs.query({url: ["http://*/*", "https://*/*"]})) {
249-
recipient.tabId = tab.id;
262+
for (let tabId of TabStatus.findTabsByOrigin(payload.serviceWorker)) {
263+
recipient.tabId = tabId;
250264
try {
251265
Messages.send("seen", payload, recipient);
252266
} catch (e) {

0 commit comments

Comments
 (0)