Skip to content

The agent that grows with you

refactor(desktop): type the media permission handlers without casts

Brooklyn Nicholson22a940206399
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts
index 776258b4182..7ff1200cdbf 100644
--- a/apps/desktop/electron/main.ts
+++ b/apps/desktop/electron/main.ts
@@ -322,6 +322,7 @@ import { createLinkTitleWindow, guardLinkTitleSession, readLinkTitleWindowTitle
import { CHROMIUM_LOG_FILENAME, enableLinuxCrashDiagnostics, linuxCrashDiagnostics } from './linux-crash-diagnostics'
import { notifyLauncherWindowRevealed } from './linux-launcher-ready'
import { decideNvidiaEglFallback, parseNvidiaDriverMajor } from './linux-nvidia-egl-fallback'
+import { isMediaCapturePermission } from './media-capture-permission'
import { createLocalBackendLifecycle, waitForTeardown } from './local-backend-lifecycle'
import { resolveIpcFileReadPath, resolveMediaStreamFile, resolvePreviewTargetPath } from './local-read-path'
import { localSkinProfileKey, readLocalSkinPayload } from './local-skin'
@@ -7153,47 +7154,6 @@ function installContextMenuBridge(window: BrowserWindow) {
})
}
-// Microphone and camera capture. The voice composer drives mic access and
-// renderer features (e.g. desktop plugins) can drive camera access, both
-// through getUserMedia, which Chromium gates behind these two session hooks.
-//
-// The naive `details.mediaTypes.includes('audio')` check works on macOS but
-// breaks on Windows: Chromium frequently fires the request with an empty or
-// undefined `mediaTypes`, so a strict check denies it and getUserMedia throws
-// NotAllowedError. We therefore allow the capture permissions and treat absent
-// metadata as allowed.
-//
-// Granting here is not the last gate: the OS still applies its own capture
-// permission (macOS TCC prompts on first use, per the NSMicrophone/NSCamera
-// usage strings), so the user keeps a real allow/deny and can revoke it in
-// System Settings afterwards.
-function isMediaCapturePermission(permission, details) {
- // HTML5 video/audio fullscreen asks the request handler for 'fullscreen'
- // and the check handler for 'automatic-fullscreen'. Both must be allowed
- // or the native fullscreen button on <video controls> does nothing.
- if (permission === 'fullscreen' || permission === 'automatic-fullscreen') {
- return true
- }
-
- if (permission === 'audioCapture' || permission === 'videoCapture') {
- return true
- }
-
- if (permission !== 'media') {
- return false
- }
-
- const mediaTypes = details?.mediaTypes
-
- // Windows: mediaTypes is often empty for a capture request. Don't deny on
- // missing metadata.
- if (!Array.isArray(mediaTypes) || mediaTypes.length === 0) {
- return true
- }
-
- return mediaTypes.includes('audio') || mediaTypes.includes('video')
-}
-
// Chromium-initiated downloads (renderer anchor/blob downloads, drag-outs)
// land here. Without a handler the OS save dialog opens with the process cwd
// as the default directory (win-unpacked in packaged installs) and whatever
@@ -7233,14 +7193,12 @@ function installMediaPermissions() {
// Synchronous check handler: Chromium consults this for getUserMedia on
// Windows in addition to (or instead of) the request handler. Without it,
// the check defaults to false and capture is denied before the request
- // handler ever runs.
+ // handler ever runs. The check handler carries no mediaTypes metadata, so
+ // the shared predicate runs with `undefined` details and allows the capture
+ // permissions — identical policy to the request handler below, just without
+ // the metadata refinement (absent metadata is allowed there too).
session.defaultSession.setPermissionCheckHandler((_webContents, permission) => {
- return (
- permission === 'media' ||
- (permission as string) === 'automatic-fullscreen' ||
- permission === ('audioCapture' as any) /* todo: is this needed? */ ||
- permission === ('videoCapture' as any)
- )
+ return isMediaCapturePermission(permission, undefined)
})
}
diff --git a/apps/desktop/electron/media-capture-permission.test.ts b/apps/desktop/electron/media-capture-permission.test.ts
new file mode 100644
index 00000000000..02e91788bb9
--- /dev/null
+++ b/apps/desktop/electron/media-capture-permission.test.ts
@@ -0,0 +1,55 @@
+import { describe, expect, it } from 'vitest'
+
+import { isMediaCapturePermission } from './media-capture-permission'
+
+describe('isMediaCapturePermission', () => {
+ it('allows the fullscreen permissions both handlers can receive', () => {
+ // The request handler sees 'fullscreen'; the check handler sees
+ // 'automatic-fullscreen'. Both must be allowed or the native fullscreen
+ // button on <video controls> does nothing.
+ expect(isMediaCapturePermission('fullscreen', undefined)).toBe(true)
+ expect(isMediaCapturePermission('automatic-fullscreen', undefined)).toBe(true)
+ })
+
+ it('allows the capture permissions without metadata (check-handler shape)', () => {
+ // The sync check handler carries no mediaTypes metadata and Chromium sends
+ // the capture permission strings directly.
+ expect(isMediaCapturePermission('audioCapture', undefined)).toBe(true)
+ expect(isMediaCapturePermission('videoCapture', undefined)).toBe(true)
+ })
+
+ it('allows a media request with audio or video mediaTypes', () => {
+ expect(isMediaCapturePermission('media', { mediaTypes: ['audio'] })).toBe(true)
+ expect(isMediaCapturePermission('media', { mediaTypes: ['video'] })).toBe(true)
+ expect(isMediaCapturePermission('media', { mediaTypes: ['audio', 'video'] })).toBe(true)
+ })
+
+ it('allows a media request with absent or empty metadata (Windows shape)', () => {
+ // Chromium on Windows frequently fires the request with an empty or
+ // undefined mediaTypes; a strict check denies it and getUserMedia throws
+ // NotAllowedError.
+ expect(isMediaCapturePermission('media', undefined)).toBe(true)
+ expect(isMediaCapturePermission('media', { mediaTypes: [] })).toBe(true)
+ })
+
+ it('denies unrelated permissions and media requests for other types', () => {
+ expect(isMediaCapturePermission('geolocation', undefined)).toBe(false)
+ expect(isMediaCapturePermission('notifications', undefined)).toBe(false)
+ expect(isMediaCapturePermission('media', { mediaTypes: ['unknown'] })).toBe(false)
+ })
+
+ it('gives both handlers one policy: check-handler strings match the request handler', () => {
+ // Invariant for the shared predicate: every permission string the sync
+ // check handler can see must resolve identically to the async request
+ // handler's decision for the same string with no metadata.
+ for (const permission of [
+ 'media',
+ 'audioCapture',
+ 'videoCapture',
+ 'fullscreen',
+ 'automatic-fullscreen',
+ ] as const) {
+ expect(isMediaCapturePermission(permission, undefined)).toBe(true)
+ }
+ })
+})
diff --git a/apps/desktop/electron/media-capture-permission.ts b/apps/desktop/electron/media-capture-permission.ts
new file mode 100644
index 00000000000..bb8c671bde8
--- /dev/null
+++ b/apps/desktop/electron/media-capture-permission.ts
@@ -0,0 +1,82 @@
+import type { MediaAccessPermissionRequest } from 'electron'
+
+/**
+ * Permission strings the media-capture session hooks can receive.
+ *
+ * Electron's typings narrow the request handler to `PermissionRequestHandlerType`
+ * (a small allow-listed union) and the check handler to a different, even smaller
+ * union that predates the capture permissions. At runtime Chromium sends more
+ * strings than either union admits — `audioCapture`, `videoCapture` and
+ * `automatic-fullscreen` are all live on the check handler — which is why the
+ * call sites widen with `as string` and delegate here. Keeping the widened
+ * superset in one named type documents exactly which extra strings we accept
+ * instead of scattering casts.
+ */
+export type MediaCapturePermissionString =
+ | 'media'
+ | 'audioCapture'
+ | 'videoCapture'
+ | 'fullscreen'
+ | 'automatic-fullscreen'
+ | (string & Record<never, never>)
+
+/**
+ * The metadata a media permission request may carry.
+ *
+ * The async request handler receives `MediaAccessPermissionRequest` (with
+ * `mediaTypes`), while the sync check handler receives
+ * `PermissionCheckHandlerHandlerDetails` (with a singular `mediaType`). The
+ * decision logic only cares about `mediaTypes`, so both shapes flow through
+ * this widened structural type.
+ */
+export type MediaCapturePermissionDetails = Pick<MediaAccessPermissionRequest, 'mediaTypes'>
+
+// Microphone and camera capture. The voice composer drives mic access and
+// renderer features (e.g. desktop plugins) can drive camera access, both
+// through getUserMedia, which Chromium gates behind these two session hooks.
+//
+// The naive `details.mediaTypes.includes('audio')` check works on macOS but
+// breaks on Windows: Chromium frequently fires the request with an empty or
+// undefined `mediaTypes`, so a strict check denies it and getUserMedia throws
+// NotAllowedError. We therefore allow the capture permissions and treat absent
+// metadata as allowed.
+//
+// Granting here is not the last gate: the OS still applies its own capture
+// permission (macOS TCC prompts on first use, per the NSMicrophone/NSCamera
+// usage strings), so the user keeps a real allow/deny and can revoke it in
+// System Settings afterwards.
+//
+// Shared by the async request handler (`setPermissionRequestHandler`, which
+// receives real `mediaTypes`) and the synchronous check handler
+// (`setPermissionCheckHandler`, which Chromium consults for getUserMedia on
+// Windows and whose `details` carry no media-type array). Delegating both to
+// the same predicate is what keeps the two paths from drifting apart again.
+export function isMediaCapturePermission(
+ permission: MediaCapturePermissionString,
+ details: MediaCapturePermissionDetails | undefined,
+): boolean {
+ // HTML5 video/audio fullscreen asks the request handler for 'fullscreen'
+ // and the check handler for 'automatic-fullscreen'. Both must be allowed
+ // or the native fullscreen button on <video controls> does nothing.
+ if (permission === 'fullscreen' || permission === 'automatic-fullscreen') {
+ return true
+ }
+
+ if (permission === 'audioCapture' || permission === 'videoCapture') {
+ return true
+ }
+
+ if (permission !== 'media') {
+ return false
+ }
+
+ const mediaTypes = details?.mediaTypes
+
+ // Windows: mediaTypes is often empty for a capture request. Don't deny on
+ // missing metadata.
+ if (!Array.isArray(mediaTypes) || mediaTypes.length === 0) {
+ return true
+ }
+
+ return mediaTypes.includes('audio') || mediaTypes.includes('video')
+}