Files
orca/src/main/browser/browser-process-user-agent.ts
T
OrcaWin 2847bbe9e8 [comment audit] Consolidate completed code comment cleanup
Combine the completed comment cleanup from 319 stacked PRs into one change against `main`, so the full CI suite validates the combined result once. This replaces the previous audit stack; no additional audit work is included.

The change shortens redundant comments across 2,753 source and shell-snapshot files (25,235 net lines removed). Conflicts preserve current-main executable code. Runtime behavior, test assertions, directives, licenses, generated notices, SAFETY rationales, and compatibility markers are preserved. The audit tracker remains local, outside the repository.

Validation:
- Equivalent source/emitted AST and executable tokens across all 2,750 source files, with separate checks for embedded shell, SQL, JavaScript, CSS, and generated fixtures.
- Identical executable lines in the three shell snapshots; protected comments unchanged.
- Changed-code quality gate passed with no new findings.
- 358 tests passed, 2 skipped across 23 focused test files.
- `git diff --check` passed.
2026-10-23 13:15:45 -07:00

51 lines
1.6 KiB
TypeScript

import { app } from 'electron'
import type { BrowserUserAgentMode } from '../../shared/browser-user-agent-mode'
export type BrowserProcessUserAgentIdentity = Readonly<{
mode: BrowserUserAgentMode
userAgent: string
}>
let identity: BrowserProcessUserAgentIdentity | null = null
const CHROMIUM_ENGINE_COMMENT = '(KHTML, like Gecko)'
// Require the engine comment so app-token stripping cannot consume OS/engine tokens.
// App names may contain spaces; keep stripping within the gap before Chrome/.
export function cleanElectronUserAgent(userAgent: string): string {
if (!userAgent.includes(CHROMIUM_ENGINE_COMMENT)) {
return userAgent
}
return userAgent
.replace(/\s+Electron\/\S+/, '')
.replace(/(\)\s+)(?:[^)\s]+\s+)*?(Chrome\/)/, '$1$2')
}
// Set the shared default before sessions and workers inherit it; native mode leaves it browser-owned.
export function initializeBrowserProcessUserAgent(
mode: BrowserUserAgentMode
): BrowserProcessUserAgentIdentity {
if (identity) {
throw new Error('Browser process user agent was already initialized')
}
if (app.isReady()) {
throw new Error('Browser process user agent must be initialized before Electron readiness')
}
if (mode === 'clean') {
app.userAgentFallback = cleanElectronUserAgent(app.userAgentFallback)
}
identity = Object.freeze({ mode, userAgent: app.userAgentFallback })
return identity
}
export function getBrowserProcessUserAgentIdentity(): BrowserProcessUserAgentIdentity {
if (!identity) {
throw new Error('Browser process user agent is not initialized')
}
return identity
}
export function resetBrowserProcessUserAgentForTests(): void {
identity = null
}