Files
client/app/main/stream-popout-main.js
T
omar e6464bc4ed feat: checkpoint account-free client workflows and source-matched UI
Preserve accumulated client implementation since v0.1.4, including local identities, multi-server state, chat, administration, audio and streaming controls. Refine official-source layout, original artwork, responsive composer overflow, settings navigation and chat scroll anchoring.

Current verification: Node suite passes; isolated packaged chat and responsive control smokes pass without device capture. Native core is reused, not rebuilt in this pass. Strict whole-screen visual parity remains incomplete; no 1:1 or cross-platform acceptance claim. Local checkpoint only; no release or push.
2026-11-01 19:43:18 +03:00

105 lines
6.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
'use strict';
const { randomUUID } = require('node:crypto');
const deny = () => ({ action: 'deny' });
const failure = message => ({ ok: false, error: { code: 'stream_popout', message } });
/** Only a one-use main-frame grant can open the fixed, scriptless same-origin player document. */
function registerStreamPopout({ ipcMain, BrowserWindow, getWindow, trustedSender, appOrigin, getConnection, testOffscreen = false, now = Date.now }) {
const url = `${appOrigin}/stream-popout.html`;
const grants = new Map(), children = new Map();
let attached = null;
const current = grant => testOffscreen || (() => {
const connection = getConnection(grant.connectionId);
return connection?.state === 'connected' && connection.session === grant.epoch;
})();
const trusted = event => trustedSender(event) && event.senderFrame === event.sender.mainFrame;
const close = token => {
grants.delete(token);
const entry = children.get(token);
if (entry && !entry.window.isDestroyed()) entry.window.close();
};
const dispose = () => {
grants.clear();
for (const token of children.keys()) close(token);
};
ipcMain.handle('stream-popout:prepare', (event, input) => {
if (!trusted(event)) return failure('Only the main application frame can detach a stream.');
if (!input || Object.keys(input).some(key => !['connectionId', 'streamId'].includes(key)) || typeof input.connectionId !== 'string' || !/^c[1-9][0-9]*$/.test(input.connectionId) || typeof input.streamId !== 'string' || !input.streamId || input.streamId.length > 256 || /[\u0000-\u001f]/.test(input.streamId)) return failure('Invalid stream owner.');
for (const [token, grant] of grants) if (grant.expires < now()) grants.delete(token);
if (grants.size + children.size >= 16) return failure('Close an existing detached stream before opening another.');
const connection = getConnection(input.connectionId);
const grant = { ...input, frame: event.senderFrame, epoch: connection?.session, expires: now() + 5000 };
if (!current(grant)) return failure('The stream connection is no longer active.');
const token = `stream-popout-${randomUUID()}`;
grants.set(token, grant);
return { ok: true, token, url };
});
ipcMain.handle('stream-popout:close', (event, input) => {
if (!trusted(event) || !input || Object.keys(input).some(key => key !== 'token') || typeof input.token !== 'string') return failure('Invalid detached stream owner.');
close(input.token); return { ok: true };
});
ipcMain.handle('stream-popout:present', (event, input) => {
if (!trusted(event) || !input || Object.keys(input).some(key => key !== 'token')) return failure('Invalid detached stream owner.');
const entry = children.get(input.token);
if (!entry || entry.window.isDestroyed() || !entry.loaded || !current(entry.grant)) return failure('Detached stream is no longer available.');
if (!testOffscreen) entry.window.show();
return { ok: true };
});
const attach = parent => {
if (attached === parent) return;
attached = parent;
parent.webContents.on('did-start-navigation', (_event, _url, sameDocument, isMainFrame) => { if (isMainFrame && !sameDocument) dispose(); });
parent.webContents.on('render-process-gone', dispose);
parent.once('closed', dispose);
parent.webContents.setWindowOpenHandler(details => {
const grant = grants.get(details.frameName);
if (!grant || details.url !== url || details.postBody || grant.frame !== parent.webContents.mainFrame || grant.expires < now() || !current(grant) || parent !== getWindow()) return deny();
grants.delete(details.frameName);
const token = details.frameName;
// Electron constructs the guest WebContents BEFORE createWindow. Security/offscreen prefs
// must be supplied here, and the callback must wrap that exact guest (not a second blank one).
const webPreferences = { contextIsolation: true, sandbox: true, nodeIntegration: false, nodeIntegrationInWorker: false,
nodeIntegrationInSubFrames: false, webSecurity: true, webviewTag: false, navigateOnDragDrop: false,
spellcheck: false, autoplayPolicy: 'no-user-gesture-required', offscreen: Boolean(testOffscreen) };
return {
action: 'allow', outlivesOpener: false,
overrideBrowserWindowOptions: { show: false, focusable: !testOffscreen, webPreferences },
createWindow: inherited => {
const child = new BrowserWindow({ webContents: inherited.webContents,
width: 960, height: 600, minWidth: 320, minHeight: 200, title: 'TeamsPeach',
show: false, focusable: !testOffscreen, skipTaskbar: Boolean(testOffscreen), fullscreenable: !testOffscreen,
...(testOffscreen ? { x: -10000, y: -10000 } : {}), webPreferences });
child.setMenu(null);
if (testOffscreen) child.webContents.setAudioMuted(true);
child.webContents.setWindowOpenHandler(deny);
let loaded = false;
child.webContents.on('will-navigate', (event, destination) => { if (loaded || destination !== url) event.preventDefault(); });
child.webContents.on('will-frame-navigate', event => { if (!event.isMainFrame || loaded || event.url !== url) event.preventDefault(); });
child.webContents.on('will-redirect', event => event.preventDefault());
child.webContents.on('will-attach-webview', event => event.preventDefault());
if (testOffscreen) child.webContents.on('content-bounds-updated', event => event.preventDefault());
const entry = { window: child, grant, loaded: false };
children.set(token, entry);
child.webContents.on('did-finish-load', () => {
if (child.webContents.getURL() !== url) return;
// A wrapped offscreen guest can retain its pre-attachment 0×0 viewport despite
// BrowserWindow outer bounds. Set the loaded document's client size without showing it.
child.setContentSize(960, 600, false);
loaded = true; entry.loaded = true;
if (!parent.isDestroyed()) parent.webContents.send('stream-popout:ready', { token });
});
child.once('closed', () => {
children.delete(token);
if (!parent.isDestroyed() && !parent.webContents.isDestroyed()) parent.webContents.send('stream-popout:closed', { token });
});
return child.webContents;
},
};
});
};
return { attach, dispose };
}
module.exports = { registerStreamPopout };