From 525d7b1b3b2731951566d432d7868e1af4b0f819 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Fri, 13 Nov 2020 08:32:51 -0500 Subject: [PATCH] Fine tune port connection code Related commit: - https://github.com/gorhill/uBlock/commit/a223031b9851874db04b4c6ccb469586f8bb9556 --- platform/chromium/vapi-background.js | 34 +++++++++++++--------------- src/js/messaging.js | 12 +++------- 2 files changed, 19 insertions(+), 27 deletions(-) diff --git a/platform/chromium/vapi-background.js b/platform/chromium/vapi-background.js index f99af7b9a..d48071184 100644 --- a/platform/chromium/vapi-background.js +++ b/platform/chromium/vapi-background.js @@ -900,23 +900,24 @@ vAPI.messaging = { 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) { - port.onDisconnect.addListener( - port => this.onPortDisconnect(port) + port.onDisconnect.addListener(port => + this.onPortDisconnect(port) ); - port.onMessage.addListener( - (request, port) => this.onPortMessage(request, port) + port.onMessage.addListener((request, port) => + this.onPortMessage(request, port) ); const portDetails = { port }; const sender = port.sender; - if ( sender ) { - portDetails.frameId = sender.frameId; - const { tab, url } = sender; - if ( tab ) { - portDetails.tabId = tab.id; - } - portDetails.url = url; - portDetails.privileged = url.startsWith(this.PRIVILEGED_URL); + const { tab, url } = sender; + portDetails.frameId = sender.frameId; + portDetails.frameURL = url; + portDetails.privileged = url.startsWith(this.PRIVILEGED_URL); + if ( tab ) { + portDetails.tabId = tab.id; + portDetails.tabURL = tab.url; } this.ports.set(port.name, portDetails); // https://bugzilla.mozilla.org/show_bug.cgi?id=1652925#c24 @@ -944,12 +945,9 @@ vAPI.messaging = { vAPI.webextFlavor.major < 61 ) { browser.tabs.onRemoved.addListener(tabId => { - for ( const { port } of this.ports.values() ) { - const tab = port.sender && port.sender.tab; - if ( !tab ) { continue; } - if ( tab.id === tabId ) { - this.onPortDisconnect(port); - } + for ( const { port, tabId: portTabId } of this.ports.values() ) { + if ( portTabId !== tabId ) { continue; } + this.onPortDisconnect(port); } }); } diff --git a/src/js/messaging.js b/src/js/messaging.js index 8c30c1f7a..010f18045 100644 --- a/src/js/messaging.js +++ b/src/js/messaging.js @@ -547,15 +547,9 @@ const µb = µBlock; const retrieveContentScriptParameters = function(sender, request) { if ( µb.readyToFilter !== true ) { return; } - const { url: senderURL, tabId, frameId } = sender; - if ( - tabId === undefined || - frameId === undefined || - senderURL === undefined || - senderURL !== request.url && senderURL.startsWith('about:') === false - ) { - return; - } + const { tabId, frameId } = sender; + if ( tabId === undefined || frameId === undefined ) { return; } + const pageStore = µb.pageStoreFromTabId(tabId); if ( pageStore === null || pageStore.getNetFilteringSwitch() === false ) { return;