Rename shim -> cast

This commit is contained in:
hensm
2022-03-15 07:00:45 +00:00
parent d7592d5806
commit 9af43c2910
51 changed files with 182 additions and 177 deletions

108
ext/src/cast/export.ts Normal file
View File

@@ -0,0 +1,108 @@
"use strict";
import * as cast from "./api";
import { Message } from "../messaging";
import { BridgeInfo } from "../lib/bridge";
import { TypedMessagePort } from "../lib/TypedMessagePort";
import {
onMessage,
onMessageResponse,
sendMessage
} from "./eventMessageChannel";
let initializedBridgeInfo: BridgeInfo;
let initializedBackgroundPort: MessagePort;
/**
* To support exporting an API from a module, we need to
* retain the event-based message passing despite not
* actually crossing any context boundaries. The cast instance
* listens for and emits these messages, and changing that
* behavior is too messy.
*/
export function ensureInit(): Promise<TypedMessagePort<Message>> {
return new Promise(async (resolve, reject) => {
// If already initialized, just return existing bridge info
if (initializedBridgeInfo) {
if (initializedBridgeInfo.isVersionCompatible) {
resolve(initializedBackgroundPort);
} else {
reject();
}
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,
* whereas in a content script, it will be the content page
* URL.
*/
if (window.location.protocol === "moz-extension:") {
const { default: CastManager } = await import(
"../background/CastManager"
);
// port2 will post bridge messages to port 1
await CastManager.init();
await CastManager.createInstance(channel.port2);
// bridge -> cast instance
channel.port1.onmessage = ev => {
const message = ev.data as Message;
// Send message to cast instance
sendMessage(message);
handleIncomingMessageToCast(message);
};
// cast instance -> bridge
onMessageResponse(message => {
channel.port1.postMessage(message);
});
} else {
/**
* Import reference to message port created by contentBridge.
* Creation of the port triggers side-effects in the
* background script.
*/
const { backgroundPort } = await import("./contentBridge");
// 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 cast messages
onMessage(handleIncomingMessageToCast);
}
function handleIncomingMessageToCast(message: Message) {
switch (message.subject) {
case "cast:initialized": {
initializedBridgeInfo = message.data;
if (initializedBridgeInfo.isVersionCompatible) {
resolve(initializedBackgroundPort);
} else {
reject();
}
}
}
}
});
}
export default cast;