Add in-app browser tabs (#430)

This commit is contained in:
Jinwoo Hong
2026-04-10 00:04:42 -07:00
committed by GitHub
parent fee1165747
commit c6ff6da3a8
45 changed files with 3953 additions and 345 deletions
+80 -23
View File
@@ -4,26 +4,12 @@ import { is } from '@electron-toolkit/utils'
import icon from '../../../resources/icon.png?asset'
import devIcon from '../../../resources/icon-dev.png?asset'
import type { Store } from '../persistence'
const LOCAL_ADDRESS_PATTERN =
/^(?:localhost|127(?:\.\d{1,3}){3}|0\.0\.0\.0|\[[0-9a-f:]+\])(?::\d+)?(?:\/.*)?$/i
function normalizeExternalUrl(rawUrl: string): string | null {
if (LOCAL_ADDRESS_PATTERN.test(rawUrl)) {
try {
return new URL(`http://${rawUrl}`).toString()
} catch {
return null
}
}
try {
const parsed = new URL(rawUrl)
return parsed.protocol === 'https:' || parsed.protocol === 'http:' ? rawUrl : null
} catch {
return null
}
}
import { browserManager } from '../browser/browser-manager'
import { ORCA_BROWSER_PARTITION } from '../../shared/constants'
import {
normalizeBrowserNavigationUrl,
normalizeExternalBrowserUrl
} from '../../shared/browser-url'
function isZoomInShortcut(input: Electron.Input): boolean {
return input.key === '=' || input.key === '+' || input.code === 'NumpadAdd'
@@ -46,6 +32,23 @@ function isZoomOutShortcut(input: Electron.Input): boolean {
)
}
function forceRepaint(window: BrowserWindow): void {
if (window.isDestroyed()) {
return
}
window.webContents.invalidate()
if (window.isMaximized() || window.isFullScreen()) {
return
}
const [width, height] = window.getSize()
window.setSize(width + 1, height)
setTimeout(() => {
if (!window.isDestroyed()) {
window.setSize(width, height)
}
}, 32)
}
export function createMainWindow(store: Store | null): BrowserWindow {
const mainWindow = new BrowserWindow({
width: 1200,
@@ -60,10 +63,26 @@ export function createMainWindow(store: Store | null): BrowserWindow {
icon: is.dev ? devIcon : icon,
webPreferences: {
preload: join(__dirname, '../preload/index.js'),
sandbox: true
sandbox: true,
webviewTag: true
}
})
if (process.platform === 'darwin') {
// Why: persistent parked webviews use separate compositor layers, and on
// recent macOS releases those layers can fail to repaint after occlusion or
// restore. Disabling main-window throttling and forcing a repaint on
// visibility transitions hardens Orca against the same black-surface
// failure mode seen during browser-tab restore and tab switching.
mainWindow.webContents.setBackgroundThrottling(false)
mainWindow.on('restore', () => {
forceRepaint(mainWindow)
})
mainWindow.on('show', () => {
forceRepaint(mainWindow)
})
}
mainWindow.webContents.on('dom-ready', () => {
mainWindow.webContents.setZoomLevel(store?.getUI().uiZoomLevel ?? 0)
})
@@ -82,18 +101,56 @@ export function createMainWindow(store: Store | null): BrowserWindow {
})
mainWindow.webContents.setWindowOpenHandler((details) => {
const externalUrl = normalizeExternalUrl(details.url)
const externalUrl = normalizeExternalBrowserUrl(details.url)
if (externalUrl) {
shell.openExternal(externalUrl)
}
return { action: 'deny' }
})
mainWindow.webContents.on('will-attach-webview', (event, webPreferences, params) => {
const src = typeof params.src === 'string' ? params.src : ''
const normalizedSrc = normalizeBrowserNavigationUrl(src)
const partition = typeof webPreferences.partition === 'string' ? webPreferences.partition : ''
// Why: arbitrary sites must stay inside an unprivileged guest surface. We
// fail closed here so a renderer bug cannot smuggle preload, Node, or a
// non-browser partition into the guest and widen the app privilege boundary.
// The one allowed data URL is Orca's inert blank-tab bootstrap page; deny
// every other data URL so the renderer cannot inject arbitrary inline HTML.
if (!normalizedSrc || partition !== ORCA_BROWSER_PARTITION) {
event.preventDefault()
return
}
delete webPreferences.preload
// Why: older Electron builds expose preloadURL alongside preload; delete
// both so the guest surface cannot inherit the main preload bridge.
delete (webPreferences as Record<string, unknown>).preloadURL
webPreferences.nodeIntegration = false
webPreferences.nodeIntegrationInSubFrames = false
webPreferences.enableBlinkFeatures = ''
webPreferences.disableBlinkFeatures = ''
webPreferences.webSecurity = true
webPreferences.allowRunningInsecureContent = false
webPreferences.contextIsolation = true
webPreferences.sandbox = true
webPreferences.partition = ORCA_BROWSER_PARTITION
})
mainWindow.webContents.on('did-attach-webview', (_event, guest) => {
// Why: popup and navigation policy must attach as soon as Chromium creates
// the guest webContents. Waiting until renderer-driven registration leaves
// a race where target=_blank or early redirects can bypass Orca's intended
// fallback behavior.
browserManager.attachGuestPolicies(guest)
})
// Block ALL in-window navigations to prevent remote pages from inheriting
// the privileged preload bridge (PTY, filesystem, etc.).
// In dev mode, allow navigations to the local dev server (e.g. HMR reloads).
mainWindow.webContents.on('will-navigate', (event, url) => {
const externalUrl = normalizeExternalUrl(url)
const externalUrl = normalizeExternalBrowserUrl(url)
if (externalUrl) {
const target = new URL(externalUrl)