From 77f7ccec430f9dd6ab49c3b46f5dd597e90da2d1 Mon Sep 17 00:00:00 2001 From: Bill Erickson <berickxx@gmail.com> Date: Thu, 9 Dec 2021 11:20:37 -0500 Subject: [PATCH] LP1954301 Extension differentiates tabs with same ID Signed-off-by: Bill Erickson <berickxx@gmail.com> Signed-off-by: Michele Morgan <mmorgan@noblenet.org> --- extension/app/extension.js | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/extension/app/extension.js b/extension/app/extension.js index a46a2d3299..affb361860 100644 --- a/extension/app/extension.js +++ b/extension/app/extension.js @@ -69,6 +69,8 @@ function onDisconnected(port) { function onNativeMessage(message) { var tabId = message.clientid; + console.debug("Hatch responded to request on tab " + tabId); + if (tabId && browserPorts[tabId]) { message.from = 'extension'; browserPorts[tabId].postMessage(message); @@ -79,13 +81,36 @@ function onNativeMessage(message) { } + /** * Called when our content script opens connection to this extension. */ +var portCounter = 0; // Additional tab name differentiation chrome.runtime.onConnect.addListener(function(port) { - var tabId = port.sender.tab.id; + + // When a new tab connects, it has no custom name. Since a tab + // with the same tab number can connect multiple times -- sometimes + // in an unexpected order -- give each port a unique name. This + // name will persist for the life of the connected port object. + // + // The port name can normally be any string, but our Java backend + // currently expects a Long (numeric) value, so format the port name + // as the tab number, followed by up to 12 zeroes, followed by our + // auto-increment. This can accommodate more than trillion unique + // tab identifiers before the browser has to be restarted. + // + // TODO: teach the Java backend to treat "msgid" as a String + // instead of a Long so we can use tab names like: + // port.name = tabNumber + "-" + increment++; + if (port.name == "") { + let base = (port.sender.tab.id + "").padEnd(12, "0"); + port.name = Number(base) + portCounter++; + } + + var tabId = port.name; browserPorts[tabId] = port; + console.debug('new port connected with id ' + tabId); port.onMessage.addListener(function(msg) { -- 2.11.0