diff --git a/ext/src/main.js b/ext/src/main.js index f573bf9..8d70325 100755 --- a/ext/src/main.js +++ b/ext/src/main.js @@ -327,6 +327,7 @@ browser.menus.onClicked.addListener(async (info, tab) => { let popupWinId; let popupShimId; +let popupPort; /** * Creates popup window for cast destination selection. @@ -381,31 +382,19 @@ async function openPopup (shimId) { // Track popup close browser.windows.onRemoved.addListener(id => { if (id === popupWinId) { - messageRouter.handleMessage({ + shimMap.get(popupShimId).port.postMessage({ subject: "popupClosed" }); popupWinId = null; popupShimId = null; + popupPort = null; } }); const shimMap = new Map(); -browser.runtime.onMessage.addListener(message => { - if (message.subject === "getPopupShimInfo") { - return new Promise(resolve => { - const { tabId, frameId } = shimMap.get(popupShimId); - - resolve({ - tabId - , frameId - }); - }); - } -}); - async function onConnectShim (port) { const bridgeInfo = await getBridgeInfo(); if (bridgeInfo && !bridgeInfo.isVersionCompatible) { @@ -470,6 +459,16 @@ async function onConnectShim (port) { * otherwise create a new popup. */ if (popupWinId) { + + // Reassign popup to new shim + popupPort.postMessage({ + subject: "assignPopup" + , data: { + tabId + , frameId + } + }); + /** * Notify shim that existing popup has closed and * to re-populate receiver list for new popup. @@ -505,12 +504,27 @@ async function onConnectShim (port) { }); } +function onConnectPopup (port) { + if (popupPort) { + popupPort.disconnect(); + } + + popupPort = port; + + const { tabId, frameId } = shimMap.get(popupShimId); + port.postMessage({ + subject: "assignPopup" + , data: { + tabId + , frameId + } + }); +} + browser.runtime.onConnect.addListener(port => { switch (port.name) { - case "shim": { - onConnectShim(port); - break; - }; + case "shim": onConnectShim(port); break; + case "popup": onConnectPopup(port); break; } }); diff --git a/ext/src/popup/index.jsx b/ext/src/popup/index.jsx index 671a53e..fb7bbe8 100755 --- a/ext/src/popup/index.jsx +++ b/ext/src/popup/index.jsx @@ -42,14 +42,14 @@ class App extends Component { }); } - async componentDidMount () { - const { tabId, frameId } = await browser.runtime.sendMessage({ - subject: "getPopupShimInfo" - }); + async setPort (shimTabId, shimFrameId) { + if (this.port) { + this.port.disconnect(); + } - this.port = browser.tabs.connect(tabId, { + this.port = browser.tabs.connect(shimTabId, { name: "popup" - , frameId + , frameId: shimFrameId }); this.port.postMessage({ @@ -84,6 +84,19 @@ class App extends Component { }); } + componentDidMount () { + const backgroundPort = browser.runtime.connect({ + name: "popup" + }); + + backgroundPort.onMessage.addListener(message => { + if (message.subject === "assignPopup") { + this.setPort(message.data.tabId + , message.data.frameId); + } + }); + } + onCast (receiver) { this.setState({ isLoading: true