65 lines
2.4 KiB
TypeScript
65 lines
2.4 KiB
TypeScript
/** Typed preload operations exposed only by the Electron shell. */
|
|
|
|
import type { IpcMainInvokeEvent } from 'electron'
|
|
import type { DesktopPluginRecord } from './project-manager.ts'
|
|
import type { DesktopLocale } from './locale.ts'
|
|
|
|
/** IPC channel names kept private to the desktop application bundle. */
|
|
export const DESKTOP_IPC = {
|
|
localeGet: 'dsh-desktop:locale-get',
|
|
boot: 'dsh-desktop:boot',
|
|
bootFailed: 'dsh-desktop:boot-failed',
|
|
directoryPick: 'dsh-desktop:directory-pick',
|
|
pluginsList: 'dsh-desktop:plugins-list',
|
|
pluginsAdd: 'dsh-desktop:plugins-add',
|
|
pluginsRemove: 'dsh-desktop:plugins-remove',
|
|
pluginsUpdate: 'dsh-desktop:plugins-update',
|
|
pluginsToggle: 'dsh-desktop:plugins-toggle',
|
|
updatesCheck: 'dsh-desktop:updates-check',
|
|
updatesInstall: 'dsh-desktop:updates-install',
|
|
updatesState: 'dsh-desktop:updates-state',
|
|
nativeThemeSet: 'dsh-desktop:native-theme-set',
|
|
} as const
|
|
|
|
/** Desktop release update state rendered by desktop-owned UI. */
|
|
export interface DesktopUpdateState {
|
|
readonly phase: 'idle' | 'checking' | 'available' | 'installing' | 'ready' | 'error'
|
|
readonly version?: string
|
|
readonly message?: string
|
|
}
|
|
|
|
/** Narrow bridge exposed through context isolation. */
|
|
export interface DshDesktopApi {
|
|
readonly protocolVersion: 1
|
|
locale(): Promise<DesktopLocale>
|
|
readonly plugins: {
|
|
list(): Promise<readonly DesktopPluginRecord[]>
|
|
add(spec: string): Promise<void>
|
|
remove(name: string): Promise<void>
|
|
update(name: string, version: string): Promise<void>
|
|
toggle(name: string, enabled: boolean): Promise<void>
|
|
}
|
|
readonly updates: {
|
|
check(): Promise<DesktopUpdateState>
|
|
install(): Promise<void>
|
|
subscribe(listener: (state: DesktopUpdateState) => void): () => void
|
|
}
|
|
}
|
|
|
|
/** Scheme of Desktop-owned application and shell documents. */
|
|
export const SCHEME = 'dsh-app'
|
|
|
|
/**
|
|
* Reject IPC outside the allowed Desktop document origins.
|
|
* @param event - IPC caller whose frame URL supplies the origin.
|
|
* @param hostnames - Desktop document hosts allowed for this operation.
|
|
*/
|
|
export function assertDesktopSender(event: IpcMainInvokeEvent, hostnames: readonly string[]): void {
|
|
const senderFrame = event.senderFrame
|
|
if (senderFrame === null) throw new Error('dsh desktop: rejected IPC without a sender frame')
|
|
const url = new URL(senderFrame.url)
|
|
if (url.protocol !== `${SCHEME}:` || !hostnames.includes(url.hostname)) {
|
|
throw new Error('dsh desktop: rejected IPC from an unowned renderer')
|
|
}
|
|
}
|