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

@@ -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) {

View File

@@ -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

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);
});
}

View File

@@ -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;
}
/**