Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(chrome-extension): cache media permissions in service worker

Richie McIlroy46b28f731aac
diff --git a/apps/chrome-extension/src/background/service-worker.ts b/apps/chrome-extension/src/background/service-worker.ts
index aec96a529..031e6e969 100644
--- a/apps/chrome-extension/src/background/service-worker.ts
+++ b/apps/chrome-extension/src/background/service-worker.ts
@@ -43,6 +43,7 @@ import type {
CameraPreviewEventRelay,
ExtensionAuth,
ExtensionSettings,
+ MediaPermissionSnapshot,
MicrophoneDevice,
MicrophoneWarningVariant,
OffscreenRequest,
@@ -69,6 +70,10 @@ let bootstrapCache: BootstrapData | null = null;
let recordingStatus: RecordingStatus = { phase: "idle" };
let cameraDevicesCache: CameraDevice[] = [];
let microphoneDevicesCache: MicrophoneDevice[] = [];
+let mediaPermissionsCache: MediaPermissionSnapshot = {
+ camera: "unknown",
+ microphone: "unknown",
+};
let uploadProgressTabId: number | null = null;
let activePreviewTabId: number | null = null;
let pendingPreviewTabId: number | null = null;
@@ -939,17 +944,45 @@ const broadcastCameraDevices = (devices: CameraDevice[]) => {
};
// The recorder panel is a cross-origin iframe, so its own enumerateDevices()
-// returns no labelled devices even when the grant exists. Enumerate in the
-// offscreen document instead (a top-level extension page that keeps the grant)
-// and cache the result. Empty results never overwrite a populated cache: a
-// transient enumeration that loses labels must not wipe known devices.
+// returns no labelled devices even when the grant exists, and its permission
+// query is delegated from the host page. Enumerate and query permissions in
+// the offscreen document instead (a top-level extension page that keeps the
+// grant) and cache the result. Empty results never overwrite a populated
+// cache: a transient enumeration that loses labels must not wipe known
+// devices.
const refreshMediaDevicesFromOffscreen = async () => {
try {
const response = await sendOffscreen({
target: "offscreen",
type: "enumerate-devices",
});
- if (response.ok && response.devices) {
+ if (!response.ok) return;
+ if (response.permissions) {
+ mediaPermissionsCache = response.permissions;
+ // A grant Chrome has reset (its automatic clean-up of unused
+ // permissions, or a manual revoke) must drop the cached device list,
+ // otherwise the stale entries hide that recording can no longer reach
+ // any camera/mic. Empty enumeration results alone never clear the
+ // cache — a cross-origin context that transiently loses labels would
+ // then wipe known devices — so only a definitively non-granted state
+ // triggers the clear.
+ if (
+ response.permissions.camera === "prompt" ||
+ response.permissions.camera === "denied"
+ ) {
+ if (cameraDevicesCache.length > 0) {
+ cameraDevicesCache = [];
+ broadcastCameraDevices(cameraDevicesCache);
+ }
+ }
+ if (
+ response.permissions.microphone === "prompt" ||
+ response.permissions.microphone === "denied"
+ ) {
+ microphoneDevicesCache = [];
+ }
+ }
+ if (response.devices) {
if (response.devices.cameras.length > 0) {
cameraDevicesCache = response.devices.cameras;
broadcastCameraDevices(cameraDevicesCache);
@@ -1567,6 +1600,7 @@ const handleRequest = async (
ok: true,
cameraDevices: cameraDevicesCache,
microphoneDevices: microphoneDevicesCache,
+ mediaPermissions: mediaPermissionsCache,
};
}