| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- import { BrowserWindow, MessageChannelMain } from "electron";
- import type { MessagePortMain, UtilityProcess as ElectronUtilityProcess } from "electron";
- import { HostMessageTypes, type RemoteUsageRemoteKind } from "@zcode/shared";
- type WebRemoteControlSharedHostKind = "local" | "remote";
- interface WebRemoteControlSharedHostAttachContext {
- kind: WebRemoteControlSharedHostKind;
- terminalOwnerId?: string;
- workspacePath: string;
- workspaceIdentity?: string;
- remoteSessionId?: string;
- initialTaskId?: string;
- }
- interface WebRemoteControlSharedHostAttachment {
- entryId: string;
- attachmentId: string;
- process: ElectronUtilityProcess;
- port: MessagePortMain;
- remoteKind?: RemoteUsageRemoteKind;
- }
- interface WebRemoteControlSharedHostError extends Error {
- code:
- | "DESKTOP_HOST_MISSING"
- | "REMOTE_WORKSPACE_IDENTITY_MISSING"
- | "REMOTE_WORKSPACE_IDENTITY_MISMATCH"
- | "REMOTE_SESSION_MISSING"
- | "REMOTE_SESSION_WINDOW_MISMATCH";
- }
- function createSharedHostError(
- code: WebRemoteControlSharedHostError["code"],
- message: string,
- ): WebRemoteControlSharedHostError {
- const error = new Error(message) as WebRemoteControlSharedHostError;
- error.code = code;
- return error;
- }
- export function createWebRemoteControlSharedHostAttachments(dependencies: {
- windowHostProcessMap: Map<number, ElectronUtilityProcess>;
- createMessageChannel?: () => { port1: MessagePortMain; port2: MessagePortMain };
- attachRemoteWorkspaceSessionHost: (params: {
- windowId: number;
- remoteSessionId: string;
- workspacePath: string;
- workspaceIdentity: string;
- workspaceKey: string;
- clientMode: "web-remote-replayable";
- terminalOwnerId?: string;
- }) => {
- process: ElectronUtilityProcess;
- port: MessagePortMain;
- remoteKind: RemoteUsageRemoteKind;
- };
- logger: {
- info: (...args: unknown[]) => void;
- warn: (...args: unknown[]) => void;
- error: (...args: unknown[]) => void;
- };
- }) {
- let nextAttachmentId = 0;
- const attachments = new Map<
- string,
- {
- windowId: number;
- remoteSessionId?: string;
- port: MessagePortMain;
- }
- >();
- function createMessageChannel() {
- return dependencies.createMessageChannel?.() ?? new MessageChannelMain();
- }
- function attachLocalHost(
- windowId: number,
- terminalOwnerId?: string,
- ): WebRemoteControlSharedHostAttachment {
- const hostProcessWindowId = BrowserWindow.fromId(windowId)?.webContents.id ?? windowId;
- const process = dependencies.windowHostProcessMap.get(hostProcessWindowId);
- if (!process) {
- throw createSharedHostError(
- "DESKTOP_HOST_MISSING",
- `未找到桌面窗口 host process,windowId=${windowId}`,
- );
- }
- const { port1, port2 } = createMessageChannel();
- const attachmentId = `shared-host-attachment-${++nextAttachmentId}`;
- process.postMessage(
- {
- type: HostMessageTypes.AttachServicePort,
- requestId: `shared-host-request-${nextAttachmentId}`,
- attachmentId,
- clientMode: "web-remote-replayable",
- ...(terminalOwnerId ? { terminalOwnerId } : {}),
- scope: { kind: "local" },
- },
- [port2],
- );
- attachments.set(attachmentId, { windowId, port: port1 });
- return {
- entryId: `desktop-host:${windowId}`,
- attachmentId,
- process,
- port: port1,
- };
- }
- async function attachWorkspaceHost(
- windowId: number,
- context: WebRemoteControlSharedHostAttachContext,
- ): Promise<WebRemoteControlSharedHostAttachment> {
- if (context.kind === "local") {
- return attachLocalHost(windowId, context.terminalOwnerId);
- }
- if (!context.remoteSessionId) {
- throw createSharedHostError(
- "REMOTE_SESSION_MISSING",
- "远程 workspace bridge 缺少 remoteSessionId。",
- );
- }
- const workspaceIdentity = context.workspaceIdentity?.trim();
- if (!workspaceIdentity) {
- throw createSharedHostError(
- "REMOTE_WORKSPACE_IDENTITY_MISSING",
- "远程 workspace bridge 缺少 workspaceIdentity,不能建立身份隔离。",
- );
- }
- const workspaceKey = workspaceIdentity || context.workspacePath;
- const attached = dependencies.attachRemoteWorkspaceSessionHost({
- windowId,
- remoteSessionId: context.remoteSessionId,
- workspacePath: context.workspacePath,
- workspaceIdentity,
- workspaceKey,
- clientMode: "web-remote-replayable",
- ...(context.terminalOwnerId ? { terminalOwnerId: context.terminalOwnerId } : {}),
- });
- const attachmentId = `shared-host-attachment-${++nextAttachmentId}`;
- attachments.set(attachmentId, {
- windowId,
- remoteSessionId: context.remoteSessionId,
- port: attached.port,
- });
- return {
- entryId: `remote-session-host:${context.remoteSessionId}`,
- attachmentId,
- process: attached.process,
- port: attached.port,
- remoteKind: attached.remoteKind,
- };
- }
- function releaseAttachment(attachmentId: string): void {
- const attachment = attachments.get(attachmentId);
- if (!attachment) {
- return;
- }
- attachments.delete(attachmentId);
- try {
- attachment.port.close();
- } catch (error) {
- dependencies.logger.warn("[web-remote-control] failed to close shared host attachment", {
- attachmentId,
- error,
- });
- }
- }
- function disposeWindow(windowId: number): void {
- for (const [attachmentId, attachment] of Array.from(attachments)) {
- if (attachment.windowId === windowId) {
- releaseAttachment(attachmentId);
- }
- }
- }
- function disposeRemoteSession(remoteSessionId: string): void {
- for (const [attachmentId, attachment] of Array.from(attachments)) {
- if (attachment.remoteSessionId === remoteSessionId) {
- releaseAttachment(attachmentId);
- }
- }
- }
- function setTerminalOwner(windowId: number, ownerId: string | null): void {
- // local/remote attachment 都归该窗口已有 Host;不把页面 owner 绑定到临时 port。
- const webContentsId = BrowserWindow.fromId(windowId)?.webContents.id ?? windowId;
- dependencies.windowHostProcessMap.get(webContentsId)?.postMessage({
- type: HostMessageTypes.SetTerminalOwner,
- ownerId,
- });
- }
- return {
- setTerminalOwner,
- attachWorkspaceHost,
- releaseAttachment,
- disposeWindow,
- disposeRemoteSession,
- };
- }
|