|
| 1 | +import { currentPlatform } from '../../common/platform'; |
| 2 | +import { linuxPowerSaveBlockerBackend } from '../platform/linux/linux-power-save-blocker'; |
| 3 | +import { cocoaPowerSaveBlockerBackend } from '../platform/macos/cocoa-power-save-blocker'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Block system/display sleep — a drop-in subset of Electron's `powerSaveBlocker`. |
| 7 | + * |
| 8 | + * A start/stop REGISTRY (not an emitter): {@link PowerSaveBlockerImpl.start} takes a |
| 9 | + * blocker `type`, asks the platform backend to {@link PowerSaveBlockerBackend.acquire} a |
| 10 | + * native blocker, stores it under a fresh incrementing id, and returns that id; |
| 11 | + * {@link PowerSaveBlockerImpl.stop} releases the native blocker and forgets the id. |
| 12 | + * |
| 13 | + * Backends: macOS holds an IOKit `IOPMAssertion` (synchronous, no run loop); Linux holds |
| 14 | + * an `org.freedesktop.ScreenSaver` inhibition cookie over the deadlock-safe bounded GDBus |
| 15 | + * method-call primitive (gated behind `SAMBAR_ENABLE_LINUX_POWER_BLOCKER`; a clean no-op |
| 16 | + * when there is no session bus). |
| 17 | + * |
| 18 | + * NO-MECHANISM SEMANTICS (matches Electron, which "always returns an integer identifying |
| 19 | + * the power save blocker"): when {@link PowerSaveBlockerBackend.acquire} returns null (no |
| 20 | + * native mechanism — headless CI, or the gate is off), `start()` STILL returns a real id; |
| 21 | + * the block is simply a documented no-op (`isStarted` true, `stop` true, nothing native to |
| 22 | + * release). Callers never get -1. |
| 23 | + */ |
| 24 | + |
| 25 | +/** Electron's two power-save-blocker types. */ |
| 26 | +export type PowerSaveBlockerType = 'prevent-app-suspension' | 'prevent-display-sleep'; |
| 27 | + |
| 28 | +/** An opaque, platform-owned native blocker handle (a CFTypeRef id, a D-Bus cookie, …). */ |
| 29 | +export type NativeBlocker = unknown; |
| 30 | + |
| 31 | +/** |
| 32 | + * The native operations the registry drives — injectable so the id-bookkeeping is |
| 33 | + * unit-tested with a fake (no FFI). `acquire` returns null when no mechanism is available |
| 34 | + * (the block becomes a no-op); `release` is best-effort and never throws. |
| 35 | + */ |
| 36 | +export type PowerSaveBlockerBackend = { |
| 37 | + acquire: (type: PowerSaveBlockerType) => NativeBlocker | null; |
| 38 | + release: (handle: NativeBlocker) => void; |
| 39 | +}; |
| 40 | + |
| 41 | +/** A no-op backend (no native power management on this platform). */ |
| 42 | +const noopBackend: PowerSaveBlockerBackend = { |
| 43 | + acquire: () => null, |
| 44 | + release: () => undefined, |
| 45 | +}; |
| 46 | + |
| 47 | +const platformBackend = (): PowerSaveBlockerBackend => { |
| 48 | + const platform = currentPlatform(); |
| 49 | + if (platform === 'macos') { |
| 50 | + return cocoaPowerSaveBlockerBackend; |
| 51 | + } |
| 52 | + if (platform === 'linux') { |
| 53 | + return linuxPowerSaveBlockerBackend; |
| 54 | + } |
| 55 | + return noopBackend; |
| 56 | +}; |
| 57 | + |
| 58 | +type Entry = { readonly type: PowerSaveBlockerType; readonly nativeHandle: NativeBlocker | null }; |
| 59 | + |
| 60 | +export class PowerSaveBlockerImpl { |
| 61 | + readonly #backend: PowerSaveBlockerBackend; |
| 62 | + readonly #blockers = new Map<number, Entry>(); |
| 63 | + #nextId = 1; |
| 64 | + |
| 65 | + /** `backend` is injectable so the registry is unit-testable with a fake (no FFI). */ |
| 66 | + constructor(backend: PowerSaveBlockerBackend = platformBackend()) { |
| 67 | + this.#backend = backend; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Start a power-save blocker of `type`. Returns the blocker id (ALWAYS a real id, even |
| 72 | + * when no native mechanism is available — the block is then a no-op). Ids are unique for |
| 73 | + * the process lifetime and never reused. |
| 74 | + */ |
| 75 | + start(type: PowerSaveBlockerType): number { |
| 76 | + const id = this.#nextId++; |
| 77 | + let nativeHandle: NativeBlocker | null = null; |
| 78 | + try { |
| 79 | + nativeHandle = this.#backend.acquire(type); |
| 80 | + } catch { |
| 81 | + nativeHandle = null; // acquire must never take down the caller; treat as a no-op. |
| 82 | + } |
| 83 | + this.#blockers.set(id, { type, nativeHandle }); |
| 84 | + return id; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Stop the blocker with `id`, releasing the native handle. Returns true if `id` referred |
| 89 | + * to a live blocker (now stopped), false for an unknown/already-stopped id. |
| 90 | + */ |
| 91 | + stop(id: number): boolean { |
| 92 | + const entry = this.#blockers.get(id); |
| 93 | + if (entry === undefined) { |
| 94 | + return false; |
| 95 | + } |
| 96 | + this.#blockers.delete(id); |
| 97 | + if (entry.nativeHandle !== null) { |
| 98 | + try { |
| 99 | + this.#backend.release(entry.nativeHandle); |
| 100 | + } catch { |
| 101 | + // Best-effort release; the id is already forgotten. |
| 102 | + } |
| 103 | + } |
| 104 | + return true; |
| 105 | + } |
| 106 | + |
| 107 | + /** Whether `id` refers to a currently-started blocker. */ |
| 108 | + isStarted(id: number): boolean { |
| 109 | + return this.#blockers.has(id); |
| 110 | + } |
| 111 | + |
| 112 | + /** Clear every blocker without releasing natively. Test-only. */ |
| 113 | + resetForTesting(): void { |
| 114 | + this.#blockers.clear(); |
| 115 | + this.#nextId = 1; |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +/** The power-save-blocker singleton. Drop-in equivalent of Electron's `powerSaveBlocker`. */ |
| 120 | +export const powerSaveBlocker = new PowerSaveBlockerImpl(); |
| 121 | +export type PowerSaveBlocker = PowerSaveBlockerImpl; |
0 commit comments