Unify receiver selector launching and allow switching to app media type

This commit is contained in:
hensm
2020-01-14 02:56:53 +00:00
parent a23ae9efa8
commit d4f71e7dcf
4 changed files with 52 additions and 47 deletions

View File

@@ -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<ReceiverSelection> {
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);
});
}