Fix popup reassigning and popupClose shim message

This commit is contained in:
hensm
2019-02-10 07:56:11 +00:00
parent 27e28b14fc
commit 5c62f40dd2
2 changed files with 51 additions and 24 deletions

View File

@@ -327,6 +327,7 @@ browser.menus.onClicked.addListener(async (info, tab) => {
let popupWinId; let popupWinId;
let popupShimId; let popupShimId;
let popupPort;
/** /**
* Creates popup window for cast destination selection. * Creates popup window for cast destination selection.
@@ -381,31 +382,19 @@ async function openPopup (shimId) {
// Track popup close // Track popup close
browser.windows.onRemoved.addListener(id => { browser.windows.onRemoved.addListener(id => {
if (id === popupWinId) { if (id === popupWinId) {
messageRouter.handleMessage({ shimMap.get(popupShimId).port.postMessage({
subject: "popupClosed" subject: "popupClosed"
}); });
popupWinId = null; popupWinId = null;
popupShimId = null; popupShimId = null;
popupPort = null;
} }
}); });
const shimMap = new Map(); 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) { async function onConnectShim (port) {
const bridgeInfo = await getBridgeInfo(); const bridgeInfo = await getBridgeInfo();
if (bridgeInfo && !bridgeInfo.isVersionCompatible) { if (bridgeInfo && !bridgeInfo.isVersionCompatible) {
@@ -470,6 +459,16 @@ async function onConnectShim (port) {
* otherwise create a new popup. * otherwise create a new popup.
*/ */
if (popupWinId) { if (popupWinId) {
// Reassign popup to new shim
popupPort.postMessage({
subject: "assignPopup"
, data: {
tabId
, frameId
}
});
/** /**
* Notify shim that existing popup has closed and * Notify shim that existing popup has closed and
* to re-populate receiver list for new popup. * 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 => { browser.runtime.onConnect.addListener(port => {
switch (port.name) { switch (port.name) {
case "shim": { case "shim": onConnectShim(port); break;
onConnectShim(port); case "popup": onConnectPopup(port); break;
break;
};
} }
}); });

View File

@@ -42,14 +42,14 @@ class App extends Component {
}); });
} }
async componentDidMount () { async setPort (shimTabId, shimFrameId) {
const { tabId, frameId } = await browser.runtime.sendMessage({ if (this.port) {
subject: "getPopupShimInfo" this.port.disconnect();
}); }
this.port = browser.tabs.connect(tabId, { this.port = browser.tabs.connect(shimTabId, {
name: "popup" name: "popup"
, frameId , frameId: shimFrameId
}); });
this.port.postMessage({ 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) { onCast (receiver) {
this.setState({ this.setState({
isLoading: true isLoading: true