Document extension messages and remove unnecessary messages/message data

This commit is contained in:
hensm
2022-08-26 21:22:52 +01:00
parent 5b8a55a2de
commit fa953dda63
7 changed files with 44 additions and 38 deletions

View File

@@ -67,7 +67,8 @@ export default class ReceiverSelector extends TypedEventTarget<ReceiverSelectorE
private wasReceiverSelected = false; private wasReceiverSelected = false;
private appId?: string; private appId?: string;
private pageInfo?: ReceiverSelectorPageInfo;
pageInfo?: ReceiverSelectorPageInfo;
constructor() { constructor() {
super(); super();

View File

@@ -93,7 +93,7 @@ export default new (class {
instance.contentPort.postMessage({ instance.contentPort.postMessage({
subject: "cast:initialized", subject: "cast:initialized",
data: await bridge.getInfo() data: { isAvailable: (await bridge.getInfo()).isVersionCompatible }
}); });
return instance; return instance;
@@ -197,12 +197,26 @@ export default new (class {
) { ) {
// Intercept messages to store relevant info // Intercept messages to store relevant info
switch (message.subject) { switch (message.subject) {
case "cast:sessionCreated": case "cast:sessionCreated": {
// Close after session is created
const selector = ReceiverSelector.sharedInstance;
if (
selector.isOpen &&
// If selector context is the same as the instance context
selector.pageInfo?.tabId === instance.contentTabId &&
selector.pageInfo?.frameId === instance.contentFrameId &&
// If selector is supposed to close
(await options.get("receiverSelectorWaitForConnection"))
) {
selector.close();
}
instance.session = { instance.session = {
deviceId: message.data.receiverId, deviceId: message.data.receiverId,
sessionId: message.data.sessionId sessionId: message.data.sessionId
}; };
break; break;
}
} }
instance.contentPort.postMessage(message); instance.contentPort.postMessage(message);
@@ -296,23 +310,6 @@ export default new (class {
break; break;
} }
/**
* TODO: If we're closing a selector, make sure it's the
* same one that caused the session creation.
*/
case "main:closeReceiverSelector": {
const selector = ReceiverSelector.sharedInstance;
const shouldClose = await options.get(
"receiverSelectorWaitForConnection"
);
if (selector.isOpen && shouldClose) {
selector.close();
}
break;
}
} }
} }

View File

@@ -97,9 +97,7 @@ export function ensureInit(): Promise<TypedMessagePort<Message>> {
function handleIncomingMessageToCast(message: Message) { function handleIncomingMessageToCast(message: Message) {
switch (message.subject) { switch (message.subject) {
case "cast:initialized": { case "cast:initialized": {
initializedBridgeInfo = message.data; if (message.data.isAvailable) {
if (initializedBridgeInfo.isVersionCompatible) {
resolve(initializedBackgroundPort); resolve(initializedBackgroundPort);
} else { } else {
reject(); reject();

View File

@@ -49,7 +49,7 @@ eventMessaging.page.addListener(async message => {
// Call page script/framework API script's init function // Call page script/framework API script's init function
const initFn = _window.__onGCastApiAvailable; const initFn = _window.__onGCastApiAvailable;
if (initFn && typeof initFn === "function") { if (initFn && typeof initFn === "function") {
initFn(message.data.isVersionCompatible); initFn(message.data.isAvailable);
} }
break; break;

View File

@@ -143,11 +143,6 @@ export default class {
* and data needed to create cast API objects is sent. * and data needed to create cast API objects is sent.
*/ */
case "cast:sessionCreated": { case "cast:sessionCreated": {
// Notify background to close UI
eventMessaging.page.sendMessage({
subject: "main:closeReceiverSelector"
});
const status = message.data; const status = message.data;
const receiverDevice = this.#receiverDevices.get( const receiverDevice = this.#receiverDevices.get(
status.receiverId status.receiverId

View File

@@ -1,7 +1,6 @@
"use strict"; "use strict";
import type { TypedPort } from "./lib/TypedPort"; import type { TypedPort } from "./lib/TypedPort";
import type { BridgeInfo } from "./lib/bridge";
import type { import type {
ReceiverSelection, ReceiverSelection,
@@ -39,6 +38,7 @@ import type { ReceiverDevice, ReceiverSelectorMediaType } from "./types";
* components. * components.
*/ */
type ExtMessageDefinitions = { type ExtMessageDefinitions = {
/** Initial data to send to selector popup. */
"popup:init": { "popup:init": {
appId?: string; appId?: string;
pageInfo?: { pageInfo?: {
@@ -47,30 +47,49 @@ type ExtMessageDefinitions = {
frameId: number; frameId: number;
}; };
}; };
/** Updates selector popup with new data. */
"popup:update": { "popup:update": {
receiverDevices: ReceiverDevice[]; receiverDevices: ReceiverDevice[];
defaultMediaType?: ReceiverSelectorMediaType; defaultMediaType?: ReceiverSelectorMediaType;
availableMediaTypes?: ReceiverSelectorMediaType; availableMediaTypes?: ReceiverSelectorMediaType;
}; };
"popup:close": undefined;
/**
* Sent from the selector popup when a receiver has been
* selected.
*/
"main:receiverSelected": ReceiverSelection; "main:receiverSelected": ReceiverSelection;
/**
* Sent from the selector popup when a receiver has been
* stopped. Used to provide cast API receiver action updates.
*/
"main:receiverStopped": { deviceId: string }; "main:receiverStopped": { deviceId: string };
/** Allows the selector popup to send cast NS_RECEIVER messages. */
"main:sendReceiverMessage": ReceiverSelectorReceiverMessage; "main:sendReceiverMessage": ReceiverSelectorReceiverMessage;
/** Allows the selector popup to send cast NS_MEDIA messages. */
"main:sendMediaMessage": ReceiverSelectorMediaMessage; "main:sendMediaMessage": ReceiverSelectorMediaMessage;
/**
* Sent from the cast API to trigger receiver selection on session
* request.
*/
"main:selectReceiver": { "main:selectReceiver": {
sessionRequest: SessionRequest; sessionRequest: SessionRequest;
}; };
/** Return message to the cast API when a receiver is selected. */
"cast:selectReceiver/selected": ReceiverSelection; "cast:selectReceiver/selected": ReceiverSelection;
/** Return message to the cast API when a selection is cancelled. */
"cast:selectReceiver/cancelled": undefined; "cast:selectReceiver/cancelled": undefined;
/** Sent to the cast API when a receiver app is stopped. */
"cast:receiverStoppedAction": { deviceId: string }; "cast:receiverStoppedAction": { deviceId: string };
"main:closeReceiverSelector": undefined; /**
* Tells the cast manager to provide the cast API instance with
* receiver data.
*/
"main:initializeCast": { appId: string }; "main:initializeCast": { appId: string };
"cast:initialized": BridgeInfo; "cast:initialized": { isAvailable: boolean };
"cast:receiverDeviceUp": { receiverDevice: ReceiverDevice }; "cast:receiverDeviceUp": { receiverDevice: ReceiverDevice };
"cast:receiverDeviceDown": { receiverDeviceId: string }; "cast:receiverDeviceDown": { receiverDeviceId: string };

View File

@@ -152,10 +152,6 @@
pageInfo = message.data.pageInfo; pageInfo = message.data.pageInfo;
break; break;
case "popup:close":
window.close();
break;
case "popup:update": { case "popup:update": {
if ( if (
message.data.availableMediaTypes !== undefined && message.data.availableMediaTypes !== undefined &&