363 lines
15 KiB
TypeScript
363 lines
15 KiB
TypeScript
/* eslint-disable max-lines -- Why: this file is the central main-window IPC wiring point; splitting it during the mobile release compatibility rebase would increase release risk. */
|
|
import { randomUUID } from 'node:crypto'
|
|
|
|
import { app, ipcMain, session } from 'electron'
|
|
import type { BrowserWindow } from 'electron'
|
|
import type { Store } from '../persistence'
|
|
import type { CreateWorktreeResult, WorktreeStartupLaunch } from '../../shared/types'
|
|
import { ORCA_BROWSER_PARTITION } from '../../shared/constants'
|
|
import { registerRepoHandlers } from '../ipc/repos'
|
|
import { registerWorktreeHandlers } from '../ipc/worktrees'
|
|
import { registerWorkspaceCleanupHandlers } from '../ipc/workspace-cleanup'
|
|
import { getLocalPtyProvider, registerPtyHandlers } from '../ipc/pty'
|
|
import { registerDaemonManagementHandlers } from '../ipc/pty-management'
|
|
import { registerSshHandlers } from '../ipc/ssh'
|
|
import { registerRemoteWorkspaceHandlers } from '../ipc/remote-workspace'
|
|
import { browserManager } from '../browser/browser-manager'
|
|
import { hasSystemMediaAccess, requestSystemMediaAccess } from '../browser/browser-media-access'
|
|
import type { OrcaRuntimeService } from '../runtime/orca-runtime'
|
|
import {
|
|
checkForUpdatesFromMenu,
|
|
downloadUpdate,
|
|
getUpdateStatus,
|
|
quitAndInstall,
|
|
setupAutoUpdater,
|
|
dismissNudge
|
|
} from '../updater'
|
|
import { scheduleHistoryGc } from '../terminal-history'
|
|
import { hydrateLocalPtyRegistryAtBoot } from '../memory/hydrate-local-pty-registry'
|
|
import type { ClaudeRuntimeAuthPreparation } from '../claude-accounts/runtime-auth-service'
|
|
import { getKnownWorktreeIdsForHistoryGc } from './history-gc-worktree-ids'
|
|
import type {
|
|
RuntimeMarkdownReadTabResult,
|
|
RuntimeMarkdownSaveTabResult
|
|
} from '../../shared/mobile-markdown-document'
|
|
import { requestMobileMarkdownFromRenderer } from './mobile-markdown-request-relay'
|
|
|
|
export function attachMainWindowServices(
|
|
mainWindow: BrowserWindow,
|
|
store: Store,
|
|
runtime: OrcaRuntimeService,
|
|
getSelectedCodexHomePath?: () => string | null,
|
|
prepareClaudeAuth?: () => Promise<ClaudeRuntimeAuthPreparation>
|
|
): void {
|
|
registerRepoHandlers(mainWindow, store)
|
|
registerWorktreeHandlers(mainWindow, store, runtime)
|
|
registerWorkspaceCleanupHandlers(store, { runtime, getLocalPtyProvider })
|
|
registerPtyHandlers(
|
|
mainWindow,
|
|
runtime,
|
|
getSelectedCodexHomePath,
|
|
() => store.getSettings(),
|
|
prepareClaudeAuth,
|
|
store
|
|
)
|
|
// Why: the Manage Sessions settings panel (docs/daemon-staleness-ux.md §Phase 1)
|
|
// uses a narrow `pty:management:*` IPC surface that reads the live
|
|
// DaemonPtyRouter via getDaemonProvider(). Registering here — after
|
|
// registerPtyHandlers — keeps this wiring alongside the rest of the PTY IPC
|
|
// and ensures the handlers are re-installed on macOS app re-activation when
|
|
// the main window is recreated.
|
|
registerDaemonManagementHandlers()
|
|
// Why: do not enumerate repo paths from background GC. `git worktree list`
|
|
// can re-touch protected folders on macOS and trigger folder-access prompts.
|
|
scheduleHistoryGc(async () => {
|
|
return getKnownWorktreeIdsForHistoryGc(store)
|
|
})
|
|
// Why: warm-reattach gap.
|
|
// Daemon-hosted PTYs survive renderer restarts on purpose, so on a fresh
|
|
// Orca launch the daemon's `listSessions()` returns sessions that
|
|
// `pty:spawn` hasn't re-registered yet. Without this hydration, the
|
|
// memory snapshot omits those PTYs and the renderer mislabels their
|
|
// workspaces as `· REMOTE` while showing `—` for CPU/Memory.
|
|
// `hydrateLocalPtyRegistryAtBoot` is idempotent (no-op after the first
|
|
// call), so calling it on every macOS dock re-activation — when this
|
|
// function re-runs as the main window is recreated — does not redo the
|
|
// git I/O or daemon RPC.
|
|
void hydrateLocalPtyRegistryAtBoot(store)
|
|
registerSshHandlers(store, () => mainWindow, runtime)
|
|
registerRemoteWorkspaceHandlers(store, () => mainWindow)
|
|
registerFileDropRelay(mainWindow)
|
|
setupAutoUpdater(mainWindow, {
|
|
getLastUpdateCheckAt: () => store.getUI().lastUpdateCheckAt,
|
|
onBeforeQuit: () => store.flush(),
|
|
setLastUpdateCheckAt: (timestamp) => {
|
|
store.updateUI({ lastUpdateCheckAt: timestamp })
|
|
},
|
|
getPendingUpdateNudgeId: () => store.getUI().pendingUpdateNudgeId ?? null,
|
|
getDismissedUpdateNudgeId: () => store.getUI().dismissedUpdateNudgeId ?? null,
|
|
setPendingUpdateNudgeId: (id) => {
|
|
// Why: the nudge lifecycle is owned by the main process. When applying a
|
|
// new campaign, persist the pending id AND clear the version dismissal
|
|
// together so relaunches cannot resurrect the old hidden-card state
|
|
// between nudge apply and renderer sync. When clearing (id is null),
|
|
// only touch pendingUpdateNudgeId — clearing dismissedUpdateVersion here
|
|
// would silently un-dismiss an update if the flow ever changes.
|
|
if (id) {
|
|
store.updateUI({ pendingUpdateNudgeId: id, dismissedUpdateVersion: null })
|
|
} else {
|
|
store.updateUI({ pendingUpdateNudgeId: null })
|
|
}
|
|
},
|
|
setDismissedUpdateNudgeId: (id) => {
|
|
store.updateUI({ dismissedUpdateNudgeId: id })
|
|
}
|
|
})
|
|
registerRuntimeWindowLifecycle(mainWindow, runtime)
|
|
|
|
const allowedPermissions = new Set(['media', 'fullscreen', 'pointerLock'])
|
|
mainWindow.webContents.session.setPermissionRequestHandler(
|
|
(_webContents, permission, callback, details) => {
|
|
if (permission === 'media') {
|
|
void requestSystemMediaAccess(details).then(callback, (error: unknown) => {
|
|
console.error('[permissions] Failed to request media access:', error)
|
|
callback(false)
|
|
})
|
|
return
|
|
}
|
|
callback(allowedPermissions.has(permission))
|
|
}
|
|
)
|
|
mainWindow.webContents.session.setPermissionCheckHandler(
|
|
(_webContents, permission, _origin, details) => {
|
|
if (permission !== 'media') {
|
|
return allowedPermissions.has(permission)
|
|
}
|
|
return hasSystemMediaAccess(details?.mediaType)
|
|
}
|
|
)
|
|
|
|
const browserSession = session.fromPartition(ORCA_BROWSER_PARTITION)
|
|
browserSession.setPermissionRequestHandler((webContents, permission, callback, details) => {
|
|
// Why: the in-app browser is for dev previews and lightweight browsing, not
|
|
// trusted desktop-app privileges. Denying by default keeps arbitrary sites
|
|
// from silently escalating into camera/mic/notification prompts inside Orca.
|
|
// Why `media` is allowed through: camera/mic are still gated by macOS TCC
|
|
// at the app-process level, so granting here only *permits* Chromium to
|
|
// use whatever the OS has already authorized for Orca. Denying at this
|
|
// layer would make pages inside the in-app browser throw NotAllowedError
|
|
// even after the user granted Camera/Microphone via Settings → Permissions
|
|
// or System Settings — the bug #1273 partially addressed.
|
|
if (permission === 'media') {
|
|
void requestSystemMediaAccess(
|
|
details as Electron.MediaAccessPermissionRequest | undefined
|
|
).then(
|
|
(granted) => {
|
|
if (!granted) {
|
|
browserManager.notifyPermissionDenied({
|
|
guestWebContentsId: webContents.id,
|
|
permission,
|
|
rawUrl: webContents.getURL()
|
|
})
|
|
}
|
|
callback(granted)
|
|
},
|
|
(error: unknown) => {
|
|
console.error('[permissions] Browser media access failed:', error)
|
|
browserManager.notifyPermissionDenied({
|
|
guestWebContentsId: webContents.id,
|
|
permission,
|
|
rawUrl: webContents.getURL()
|
|
})
|
|
callback(false)
|
|
}
|
|
)
|
|
return
|
|
}
|
|
const allowed = permission === 'fullscreen'
|
|
if (!allowed) {
|
|
browserManager.notifyPermissionDenied({
|
|
guestWebContentsId: webContents.id,
|
|
permission,
|
|
rawUrl: webContents.getURL()
|
|
})
|
|
}
|
|
callback(allowed)
|
|
})
|
|
browserSession.setPermissionCheckHandler((_webContents, permission, _origin, details) => {
|
|
if (permission === 'fullscreen') {
|
|
return true
|
|
}
|
|
if (permission === 'media') {
|
|
return hasSystemMediaAccess(details?.mediaType)
|
|
}
|
|
return false
|
|
})
|
|
browserSession.setDisplayMediaRequestHandler((_request, callback) => {
|
|
// Why: arbitrary sites inside Orca should never be able to capture the
|
|
// desktop or application windows until there is explicit product UX for
|
|
// selecting a source and surfacing that choice to the user.
|
|
// Why: pass undefined (not null) to satisfy Electron's typed callback
|
|
// signature while still denying the request.
|
|
callback({ video: undefined, audio: undefined })
|
|
})
|
|
browserSession.on('will-download', (_event, item, webContents) => {
|
|
// Why: browser-tab downloads need explicit product UX before arbitrary sites
|
|
// can write files through Orca. Pause the item and route it through
|
|
// BrowserManager so the user must explicitly accept the save path first.
|
|
browserManager.handleGuestWillDownload({ guestWebContentsId: webContents.id, item })
|
|
})
|
|
|
|
mainWindow.on('closed', () => {
|
|
// Why: parked browser webviews can outlive the visible tab body until the
|
|
// renderer process exits. Clearing main-owned guest registrations on window
|
|
// close prevents stale tab→webContents ids from leaking across app relaunch
|
|
// or hot-reload cycles.
|
|
browserManager.unregisterAll()
|
|
})
|
|
}
|
|
|
|
function registerRuntimeWindowLifecycle(
|
|
mainWindow: BrowserWindow,
|
|
runtime: OrcaRuntimeService
|
|
): void {
|
|
runtime.attachWindow(mainWindow.id)
|
|
const send = (channel: string, ...args: unknown[]): void => {
|
|
if (!mainWindow.isDestroyed()) {
|
|
mainWindow.webContents.send(channel, ...args)
|
|
}
|
|
}
|
|
runtime.setNotifier({
|
|
worktreesChanged: (repoId) => send('worktrees:changed', { repoId }),
|
|
worktreeBaseStatus: (event) => send('worktree:baseStatus', event),
|
|
worktreeRemoteBranchConflict: (event) => send('worktree:remoteBranchConflict', event),
|
|
reposChanged: () => send('repos:changed'),
|
|
activateWorktree: (
|
|
repoId,
|
|
worktreeId,
|
|
setup?: CreateWorktreeResult['setup'],
|
|
startup?: WorktreeStartupLaunch
|
|
) => {
|
|
send('ui:activateWorktree', {
|
|
repoId,
|
|
worktreeId,
|
|
...(setup ? { setup } : {}),
|
|
...(startup ? { startup } : {})
|
|
})
|
|
},
|
|
createTerminal: (worktreeId, opts) =>
|
|
send('ui:createTerminal', { worktreeId, command: opts.command, title: opts.title }),
|
|
revealTerminalSession: (worktreeId, opts) =>
|
|
new Promise((resolve, reject) => {
|
|
const requestId = randomUUID()
|
|
const timer = setTimeout(() => {
|
|
ipcMain.removeListener('terminal:tabCreateReply', handler)
|
|
reject(new Error('Terminal reveal timed out'))
|
|
}, 10_000)
|
|
const handler = (
|
|
_event: Electron.IpcMainEvent,
|
|
reply: { requestId: string; tabId?: string; title?: string; error?: string }
|
|
): void => {
|
|
if (reply.requestId !== requestId) {
|
|
return
|
|
}
|
|
clearTimeout(timer)
|
|
ipcMain.removeListener('terminal:tabCreateReply', handler)
|
|
if (reply.error) {
|
|
reject(new Error(reply.error))
|
|
return
|
|
}
|
|
resolve({ tabId: reply.tabId!, title: reply.title })
|
|
}
|
|
ipcMain.on('terminal:tabCreateReply', handler)
|
|
send('ui:createTerminal', {
|
|
requestId,
|
|
worktreeId,
|
|
ptyId: opts.ptyId,
|
|
title: opts.title ?? undefined,
|
|
activate: opts.activate !== false,
|
|
// Why: pre-minted tabId from main keeps the renderer's tab id aligned
|
|
// with the paneKey baked into the PTY env at spawn time, so hook
|
|
// events route to the right slot.
|
|
...(opts.tabId !== undefined ? { tabId: opts.tabId } : {}),
|
|
...(opts.leafId !== undefined ? { leafId: opts.leafId } : {})
|
|
})
|
|
}),
|
|
splitTerminal: (tabId, paneRuntimeId, opts) => {
|
|
send('ui:splitTerminal', {
|
|
tabId,
|
|
paneRuntimeId,
|
|
direction: opts.direction,
|
|
command: opts.command
|
|
})
|
|
},
|
|
renameTerminal: (tabId, title) => send('ui:renameTerminal', { tabId, title }),
|
|
focusTerminal: (tabId, worktreeId, leafId) =>
|
|
send('ui:focusTerminal', { tabId, worktreeId, leafId }),
|
|
focusEditorTab: (tabId, worktreeId) => send('ui:focusEditorTab', { tabId, worktreeId }),
|
|
closeSessionTab: (tabId, worktreeId) => send('ui:closeSessionTab', { tabId, worktreeId }),
|
|
openFile: (worktreeId, filePath, relativePath) =>
|
|
send('ui:openFileFromMobile', { worktreeId, filePath, relativePath }),
|
|
readMobileMarkdownTab: (worktreeId, tabId) =>
|
|
requestMobileMarkdownFromRenderer(mainWindow, {
|
|
operation: 'read',
|
|
worktreeId,
|
|
tabId
|
|
}) as Promise<RuntimeMarkdownReadTabResult>,
|
|
saveMobileMarkdownTab: (worktreeId, tabId, baseVersion, content) =>
|
|
requestMobileMarkdownFromRenderer(mainWindow, {
|
|
operation: 'save',
|
|
worktreeId,
|
|
tabId,
|
|
baseVersion,
|
|
content
|
|
}) as Promise<RuntimeMarkdownSaveTabResult>,
|
|
closeTerminal: (tabId, paneRuntimeId) => send('ui:closeTerminal', { tabId, paneRuntimeId }),
|
|
sleepWorktree: (worktreeId) => send('ui:sleepWorktree', { worktreeId }),
|
|
terminalFitOverrideChanged: (ptyId, mode, cols, rows) =>
|
|
send('runtime:terminalFitOverrideChanged', { ptyId, mode, cols, rows }),
|
|
terminalDriverChanged: (ptyId, driver) =>
|
|
send('runtime:terminalDriverChanged', { ptyId, driver })
|
|
})
|
|
// Why: the runtime must fail closed while the renderer graph is being torn
|
|
// down or rebuilt, otherwise future CLI calls could act on stale terminal
|
|
// mappings during reload transitions.
|
|
mainWindow.webContents.on('did-start-loading', () => {
|
|
runtime.markRendererReloading(mainWindow.id)
|
|
})
|
|
mainWindow.on('closed', () => {
|
|
runtime.markGraphUnavailable(mainWindow.id)
|
|
})
|
|
}
|
|
|
|
function registerFileDropRelay(mainWindow: BrowserWindow): void {
|
|
ipcMain.removeAllListeners('terminal:file-dropped-from-preload')
|
|
ipcMain.on(
|
|
'terminal:file-dropped-from-preload',
|
|
(
|
|
_event,
|
|
args:
|
|
| { paths: string[]; target: 'editor' }
|
|
| { paths: string[]; target: 'terminal'; tabId?: string }
|
|
| { paths: string[]; target: 'composer' }
|
|
| { paths: string[]; target: 'file-explorer'; destinationDir: string }
|
|
) => {
|
|
if (mainWindow.isDestroyed()) {
|
|
return
|
|
}
|
|
|
|
// Why: relay exactly one IPC event per drop gesture so the renderer
|
|
// receives the full batch of paths without timer-based reconstruction.
|
|
mainWindow.webContents.send('terminal:file-drop', args)
|
|
}
|
|
)
|
|
}
|
|
|
|
export function registerUpdaterHandlers(_store: Store): void {
|
|
ipcMain.removeHandler('updater:getStatus')
|
|
ipcMain.removeHandler('updater:getVersion')
|
|
ipcMain.removeHandler('updater:check')
|
|
ipcMain.removeHandler('updater:download')
|
|
ipcMain.removeHandler('updater:quitAndInstall')
|
|
ipcMain.removeHandler('updater:dismissNudge')
|
|
|
|
ipcMain.handle('updater:getStatus', () => getUpdateStatus())
|
|
ipcMain.handle('updater:getVersion', () => app.getVersion())
|
|
ipcMain.handle('updater:check', (_event, options?: { includePrerelease?: boolean }) =>
|
|
checkForUpdatesFromMenu(options)
|
|
)
|
|
ipcMain.handle('updater:download', () => downloadUpdate())
|
|
ipcMain.handle('updater:quitAndInstall', () => quitAndInstall())
|
|
ipcMain.handle('updater:dismissNudge', () => dismissNudge())
|
|
}
|