mirror of
https://github.com/hensm/fx_cast.git
synced 2026-06-11 10:09:59 +00:00
Allow mediaCast sender to run in background context
This commit is contained in:
@@ -16,6 +16,8 @@ import { ErrorCode
|
||||
, SessionStatus
|
||||
, VolumeControlType } from "../enums";
|
||||
|
||||
import { RepeatMode } from "../media/enums";
|
||||
|
||||
import { ListenerObject
|
||||
, onMessage
|
||||
, sendMessageResponse } from "../../eventMessageChannel";
|
||||
@@ -305,9 +307,10 @@ export default class Session {
|
||||
, autoplay: loadRequest.autoplay || false
|
||||
, currentTime: loadRequest.currentTime || 0
|
||||
, customData: loadRequest.customData || {}
|
||||
, repeatMode: "REPEAT_OFF"
|
||||
, repeatMode: RepeatMode.OFF
|
||||
});
|
||||
|
||||
|
||||
let hasResponded = false;
|
||||
|
||||
this.addMessageListener(
|
||||
@@ -318,23 +321,28 @@ export default class Session {
|
||||
return;
|
||||
}
|
||||
|
||||
const mediaObject = JSON.parse(data);
|
||||
const message = JSON.parse(data);
|
||||
|
||||
if (mediaObject.status && mediaObject.status.length > 0) {
|
||||
if (message.status && message.status.length > 0) {
|
||||
hasResponded = true;
|
||||
|
||||
const media = new Media(
|
||||
this.sessionId
|
||||
, mediaObject.status[0].mediaSessionId
|
||||
, message.status[0].mediaSessionId
|
||||
, _id.get(this));
|
||||
|
||||
media.media = loadRequest.media;
|
||||
this.media = [ media ];
|
||||
|
||||
media.play();
|
||||
successCallback(media);
|
||||
|
||||
if (successCallback) {
|
||||
successCallback(media);
|
||||
}
|
||||
} else {
|
||||
errorCallback(new _Error(ErrorCode.SESSION_ERROR));
|
||||
if (errorCallback) {
|
||||
errorCallback(new _Error(ErrorCode.SESSION_ERROR));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -210,9 +210,6 @@ export function _requestSession (
|
||||
|
||||
sessionRequestInProgress = true;
|
||||
|
||||
sessionSuccessCallback = successCallback;
|
||||
sessionErrorCallback = errorCallback;
|
||||
|
||||
|
||||
const selectedReceiver = new Receiver_(
|
||||
_receiver.id
|
||||
@@ -235,8 +232,8 @@ export function _requestSession (
|
||||
|
||||
sessionRequestInProgress = false;
|
||||
|
||||
if (sessionSuccessCallback) {
|
||||
sessionSuccessCallback(session);
|
||||
if (successCallback) {
|
||||
successCallback(session);
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
@@ -293,7 +293,7 @@ export default class Media {
|
||||
}
|
||||
|
||||
|
||||
private _sendMediaMessage (
|
||||
public _sendMediaMessage (
|
||||
message: any
|
||||
, successCallback?: SuccessCallback
|
||||
, errorCallback?: ErrorCallback) {
|
||||
|
||||
@@ -4,11 +4,12 @@ import * as cast from "./cast";
|
||||
|
||||
import { BridgeInfo } from "../lib/bridge";
|
||||
import { Message } from "../types";
|
||||
import { onMessage } from "./eventMessageChannel";
|
||||
|
||||
import { onMessage, onMessageResponse, sendMessage } from "./eventMessageChannel";
|
||||
|
||||
|
||||
let initializedBridgeInfo: BridgeInfo;
|
||||
let initializedBackgroundPort: browser.runtime.Port;
|
||||
let initializedBackgroundPort: MessagePort;
|
||||
|
||||
/**
|
||||
* To support exporting an API from a module, we need to
|
||||
@@ -17,7 +18,7 @@ let initializedBackgroundPort: browser.runtime.Port;
|
||||
* for and emits these messages, and changing that behavior
|
||||
* is too messy.
|
||||
*/
|
||||
export function ensureInit (): Promise<browser.runtime.Port> {
|
||||
export function ensureInit (): Promise<MessagePort> {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
|
||||
// If already initialized, just return existing bridge info
|
||||
@@ -31,6 +32,9 @@ export function ensureInit (): Promise<browser.runtime.Port> {
|
||||
return;
|
||||
}
|
||||
|
||||
const channel = new MessageChannel();
|
||||
initializedBackgroundPort = channel.port1;
|
||||
|
||||
/**
|
||||
* If the module is imported into a background script
|
||||
* context, the location will be the internal extension URL,
|
||||
@@ -38,14 +42,48 @@ export function ensureInit (): Promise<browser.runtime.Port> {
|
||||
* URL.
|
||||
*/
|
||||
if (window.location.protocol === "moz-extension:") {
|
||||
//
|
||||
const { default: createShim } = await import("../createShim");
|
||||
|
||||
// port2 will post bridge messages to port 1
|
||||
await createShim(channel.port2);
|
||||
|
||||
// bridge -> shim
|
||||
channel.port1.onmessage = ev => {
|
||||
const message = ev.data as Message;
|
||||
|
||||
// Send message to shim
|
||||
sendMessage(message);
|
||||
handleIncomingMessageToShim(message);
|
||||
};
|
||||
|
||||
// shim -> bridge
|
||||
onMessageResponse(message => {
|
||||
channel.port1.postMessage(message);
|
||||
});
|
||||
} else {
|
||||
// Trigger message port setup side-effects
|
||||
/**
|
||||
* Import reference to message port created by contentBridge.
|
||||
* Creation of the port triggers side-effects in the
|
||||
* background script.
|
||||
*/
|
||||
const { backgroundPort } = await import("./contentBridge");
|
||||
initializedBackgroundPort = backgroundPort;
|
||||
|
||||
// backgroundPort -> channel.port2
|
||||
backgroundPort.onMessage.addListener((message: Message) => {
|
||||
channel.port2.postMessage(message);
|
||||
});
|
||||
|
||||
// channel.port2 -> backgroundPort
|
||||
channel.port2.onmessage = ev => {
|
||||
const message = ev.data as Message;
|
||||
backgroundPort.postMessage(message);
|
||||
};
|
||||
|
||||
// Handle shim messages
|
||||
onMessage(handleIncomingMessageToShim);
|
||||
}
|
||||
|
||||
onMessage(message => {
|
||||
function handleIncomingMessageToShim (message: Message) {
|
||||
switch (message.subject) {
|
||||
case "shim:/initialized": {
|
||||
initializedBridgeInfo = message.data;
|
||||
@@ -57,7 +95,7 @@ export function ensureInit (): Promise<browser.runtime.Port> {
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user