diff --git a/ext/src/background/ShimManager.ts b/ext/src/background/ShimManager.ts index 22b4312..f4e4334 100644 --- a/ext/src/background/ShimManager.ts +++ b/ext/src/background/ShimManager.ts @@ -156,15 +156,11 @@ export default new class ShimManager { case "main:/selectReceiverBegin": { const contentTab = await browser.tabs.get(shim.contentTabId); - const availableMediaTypes = getMediaTypesForPageUrl( - contentTab.url); try { - const selection = await ReceiverSelectorManager - .getSelection( - ReceiverSelectorMediaType.App - , availableMediaTypes - , shim.requestedAppId); + const selection = + await ReceiverSelectorManager.getSelection( + shim.contentTabId, shim.contentFrameId); // Handle cancellation if (!selection) { diff --git a/ext/src/background/background.ts b/ext/src/background/background.ts index 24594ca..c093d90 100755 --- a/ext/src/background/background.ts +++ b/ext/src/background/background.ts @@ -65,12 +65,7 @@ function initBrowserAction () { * top-level frame. */ browser.browserAction.onClicked.addListener(async tab => { - const currentShim = ShimManager.getShim(tab.id); - const selection = await ReceiverSelectorManager.getSelection( - ReceiverSelectorMediaType.Tab - , getMediaTypesForPageUrl(tab.url) - & ~ReceiverSelectorMediaType.App - , currentShim.requestedAppId); + const selection = await ReceiverSelectorManager.getSelection(tab.id); if (selection) { loadSender({ @@ -161,11 +156,8 @@ async function initMenus () { switch (info.menuItemId) { case menuIdMediaCast: { - const currentShim = ShimManager.getShim(tab.id, info.frameId); const selection = await ReceiverSelectorManager.getSelection( - ReceiverSelectorMediaType.App - , availableMediaTypes - , currentShim.requestedAppId); + tab.id, info.frameId); // Selection cancelled if (!selection) { @@ -204,11 +196,8 @@ async function initMenus () { } case menuIdMirroringCast: { - const currentShim = ShimManager.getShim(tab.id, info.frameId); const selection = await ReceiverSelectorManager.getSelection( - ReceiverSelectorMediaType.Tab - , availableMediaTypes & ~ReceiverSelectorMediaType.App - , currentShim.requestedAppId); + tab.id, info.frameId); loadSender({ tabId: tab.id diff --git a/ext/src/background/receiverSelector/ReceiverSelectorManager.ts b/ext/src/background/receiverSelector/ReceiverSelectorManager.ts index 3071662..1efbb10 100644 --- a/ext/src/background/receiverSelector/ReceiverSelectorManager.ts +++ b/ext/src/background/receiverSelector/ReceiverSelectorManager.ts @@ -2,8 +2,11 @@ import options from "../../lib/options"; +import ShimManager from "../ShimManager"; import StatusManager from "../StatusManager"; +import { getMediaTypesForPageUrl } from "../../lib/utils"; + import { ReceiverSelector , ReceiverSelectorType } from "./"; import { ReceiverSelection @@ -49,16 +52,50 @@ async function getSelector () { * - Rejects if the selection fails. */ async function getSelection ( - defaultMediaType = - ReceiverSelectorMediaType.Tab - , availableMediaTypes = - ReceiverSelectorMediaType.Tab - | ReceiverSelectorMediaType.Screen - | ReceiverSelectorMediaType.File - , requestedAppId: string) + contextTabId: number + , contextFrameId = 0) : Promise { return new Promise(async (resolve, reject) => { + const currentShim = ShimManager.getShim( + contextTabId, contextFrameId); + + let defaultMediaType = ReceiverSelectorMediaType.Tab; + let availableMediaTypes; + + try { + const { url } = await browser.webNavigation.getFrame({ + tabId: contextTabId + , frameId: contextFrameId + }); + + availableMediaTypes = getMediaTypesForPageUrl(url); + } catch (err) { + console.error("fx_cast (Debug): Failed to locate frame"); + reject(); + return; + } + + // Enable app media type if initialized sender app is found + if (currentShim) { + defaultMediaType = ReceiverSelectorMediaType.App; + availableMediaTypes |= ReceiverSelectorMediaType.App; + } + + const opts = await options.getAll(); + + // Remove mirroring media types if mirroring is not enabled + if (!opts.mirroringEnabled) { + availableMediaTypes &= ~( + ReceiverSelectorMediaType.Tab + | ReceiverSelectorMediaType.Screen); + } + + // Remove file media type if local media is not enabled + if (!opts.mediaEnabled || !opts.localMediaEnabled) { + availableMediaTypes &= ~ReceiverSelectorMediaType.File; + } + // Close an existing open selector if (sharedSelector && sharedSelector.isOpen) { @@ -84,21 +121,6 @@ async function getSelection ( }); - const opts = await options.getAll(); - - // Remove mirroring media types if mirroring is not enabled. - if (!opts.mirroringEnabled) { - availableMediaTypes &= ~( - ReceiverSelectorMediaType.Tab - | ReceiverSelectorMediaType.Screen); - } - - // Remove file media type if local media is not enabled - if (!opts.mediaEnabled || !opts.localMediaEnabled) { - availableMediaTypes &= ~ReceiverSelectorMediaType.File; - } - - // Ensure status manager is initialized await StatusManager.init(); @@ -106,7 +128,7 @@ async function getSelection ( Array.from(StatusManager.getReceivers()) , defaultMediaType , availableMediaTypes - , requestedAppId); + , currentShim?.requestedAppId); }); } diff --git a/ext/src/lib/utils.ts b/ext/src/lib/utils.ts index c075c77..f2ecccd 100644 --- a/ext/src/lib/utils.ts +++ b/ext/src/lib/utils.ts @@ -63,9 +63,7 @@ export function getMediaTypesForPageUrl ( // Only meant to run on normal web pages if (url.protocol === "http:" || url.protocol === "https:") { - availableMediaTypes |= ( - ReceiverSelectorMediaType.App - | ReceiverSelectorMediaType.Tab); + availableMediaTypes |= ReceiverSelectorMediaType.Tab; } /**