Fine tune port connection code

Related commit:
- a223031b98
This commit is contained in:
Raymond Hill 2020-11-13 08:32:51 -05:00
parent 02b4d149e3
commit 525d7b1b3b
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
2 changed files with 19 additions and 27 deletions

View File

@ -900,23 +900,24 @@ vAPI.messaging = {
this.ports.delete(port.name); this.ports.delete(port.name);
}, },
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/runtime/Port
// port.sender is always present for onConnect() listeners.
onPortConnect: function(port) { onPortConnect: function(port) {
port.onDisconnect.addListener( port.onDisconnect.addListener(port =>
port => this.onPortDisconnect(port) this.onPortDisconnect(port)
); );
port.onMessage.addListener( port.onMessage.addListener((request, port) =>
(request, port) => this.onPortMessage(request, port) this.onPortMessage(request, port)
); );
const portDetails = { port }; const portDetails = { port };
const sender = port.sender; const sender = port.sender;
if ( sender ) {
portDetails.frameId = sender.frameId;
const { tab, url } = sender; const { tab, url } = sender;
portDetails.frameId = sender.frameId;
portDetails.frameURL = url;
portDetails.privileged = url.startsWith(this.PRIVILEGED_URL);
if ( tab ) { if ( tab ) {
portDetails.tabId = tab.id; portDetails.tabId = tab.id;
} portDetails.tabURL = tab.url;
portDetails.url = url;
portDetails.privileged = url.startsWith(this.PRIVILEGED_URL);
} }
this.ports.set(port.name, portDetails); this.ports.set(port.name, portDetails);
// https://bugzilla.mozilla.org/show_bug.cgi?id=1652925#c24 // https://bugzilla.mozilla.org/show_bug.cgi?id=1652925#c24
@ -944,13 +945,10 @@ vAPI.messaging = {
vAPI.webextFlavor.major < 61 vAPI.webextFlavor.major < 61
) { ) {
browser.tabs.onRemoved.addListener(tabId => { browser.tabs.onRemoved.addListener(tabId => {
for ( const { port } of this.ports.values() ) { for ( const { port, tabId: portTabId } of this.ports.values() ) {
const tab = port.sender && port.sender.tab; if ( portTabId !== tabId ) { continue; }
if ( !tab ) { continue; }
if ( tab.id === tabId ) {
this.onPortDisconnect(port); this.onPortDisconnect(port);
} }
}
}); });
} }
}, },

View File

@ -547,15 +547,9 @@ const µb = µBlock;
const retrieveContentScriptParameters = function(sender, request) { const retrieveContentScriptParameters = function(sender, request) {
if ( µb.readyToFilter !== true ) { return; } if ( µb.readyToFilter !== true ) { return; }
const { url: senderURL, tabId, frameId } = sender; const { tabId, frameId } = sender;
if ( if ( tabId === undefined || frameId === undefined ) { return; }
tabId === undefined ||
frameId === undefined ||
senderURL === undefined ||
senderURL !== request.url && senderURL.startsWith('about:') === false
) {
return;
}
const pageStore = µb.pageStoreFromTabId(tabId); const pageStore = µb.pageStoreFromTabId(tabId);
if ( pageStore === null || pageStore.getNetFilteringSwitch() === false ) { if ( pageStore === null || pageStore.getNetFilteringSwitch() === false ) {
return; return;