Reorganize cast sender code

This commit is contained in:
hensm
2022-04-18 03:01:56 +01:00
parent 514b382344
commit abf657c0d4
7 changed files with 90 additions and 106 deletions

View File

@@ -1,81 +0,0 @@
"use strict";
import logger from "./logger";
import { stringify } from "./utils";
import {
ReceiverSelection,
ReceiverSelectionActionType,
ReceiverSelectorMediaType
} from "../background/receiverSelector";
import CastManager from "../cast/CastManager";
interface LoadSenderOptions {
tabId: number;
frameId?: number;
selection: ReceiverSelection;
}
/**
* Loads the appropriate sender for a given receiver
* selector response.
*/
export default async function loadSender(opts: LoadSenderOptions) {
// Cancelled
if (!opts.selection) {
return;
}
if (opts.selection.actionType !== ReceiverSelectionActionType.Cast) {
return;
}
switch (opts.selection.mediaType) {
case ReceiverSelectorMediaType.App: {
const instance = CastManager.getInstance(opts.tabId, opts.frameId);
if (!instance) {
throw logger.error(
`Cast instance not found at tabId ${opts.tabId} / frameId ${opts.frameId}`
);
}
instance.contentPort.postMessage({
subject: "cast:launchApp",
data: { receiverDevice: opts.selection.receiverDevice }
});
break;
}
case ReceiverSelectorMediaType.Tab:
case ReceiverSelectorMediaType.Screen: {
await browser.tabs.executeScript(opts.tabId, {
code: stringify`
window.selectedMedia = ${opts.selection.mediaType};
window.selectedReceiver = ${opts.selection.receiverDevice};
`,
frameId: opts.frameId
});
await browser.tabs.executeScript(opts.tabId, {
file: "senders/mirroring.js",
frameId: opts.frameId
});
break;
}
case ReceiverSelectorMediaType.File: {
const fileUrl = new URL(`file://${opts.selection.filePath}`);
const { init } = await import("../senders/media");
init({
mediaUrl: fileUrl.href,
receiver: opts.selection.receiverDevice
});
break;
}
}
}