Skip to content

Commit b15489a

Browse files
committed
fix(desktop): narrow display recovery to verified fixes
Drop the session permission hardening and embed focus release: the browser pane and the app window already deny pointer lock, so they do not change #57732. Defer the first show only for maximized Windows windows, and cut the wiring test to one trigger plus listener cleanup.
1 parent a7ab67b commit b15489a

7 files changed

Lines changed: 46 additions & 292 deletions

File tree

‎packages/desktop/src/main/index.ts‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
// Imported first so its evaluation stamps the moment Electron handed control to this module.
22
import { marks } from "./lifecycle/marks"
3-
import { app, protocol } from "electron"
3+
import { app } from "electron"
44
import { acquireApplicationLock, configureApplication } from "./lifecycle/configure"
55
import { startSidecarProbe } from "./service/sidecar-probe"
66
import { registerStorageSnapshotHandler } from "./storage/snapshot"
77
import { createEarlyWindow } from "./windows/early"
88
import { rendererAssetsServed } from "./windows/protocol"
99
import { registerRendererScheme } from "./windows/scheme"
10-
import { wireSessionSecurity } from "./windows/security"
1110

1211
// This module stays small on purpose. Electron holds the ready event until the entry module has
1312
// finished, and the first window should be on screen before the rest of the main process — a few
@@ -16,8 +15,7 @@ import { wireSessionSecurity } from "./windows/security"
1615
configureApplication()
1716

1817
if (acquireApplicationLock()) {
19-
wireSessionSecurity(app)
20-
registerRendererScheme(protocol)
18+
registerRendererScheme()
2119
// Window first, then the bundle: starting the import before ready delays ready itself, because the
2220
// module graph evaluates on the same thread Chromium needs to finish initialising.
2321
void app.whenReady().then(async () => {

‎packages/desktop/src/main/plugin/embeds.ts‎

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,6 @@ export function createEmbeds() {
9494
// Keep the corner layers up while the box is laid out, even before the page shows.
9595
// Over the DOM card they match its own rounded corners.
9696
const visible = placed && entry.shown
97-
98-
if (!visible) releaseFocus(entry)
9997
entry.corners.forEach((corner) => corner.setVisible(placed && !!entry.cornerKey))
10098
entry.view.setVisible(visible)
10199

@@ -104,14 +102,6 @@ export function createEmbeds() {
104102
entry.listeners.forEach((listener) => listener(visible))
105103
}
106104

107-
const releaseFocus = (entry: Entry) => {
108-
if (entry.window.isDestroyed() || entry.view.webContents.isDestroyed()) return
109-
110-
if (entry.view.webContents.isFocused() && entry.window.isFocused() && !entry.window.webContents.isDestroyed()) {
111-
entry.window.webContents.focus()
112-
}
113-
}
114-
115105
const release = (id: string) => {
116106
const entry = entries.get(id)
117107

@@ -120,7 +110,6 @@ export function createEmbeds() {
120110
entry.listeners.clear()
121111

122112
if (entry.window.isDestroyed()) return
123-
releaseFocus(entry)
124113
entry.view.setVisible(false)
125114
entry.corners.forEach((corner) => entry.window.contentView.removeChildView(corner))
126115
entry.window.contentView.removeChildView(entry.view)
Lines changed: 37 additions & 173 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1+
import { EventEmitter } from "node:events"
12
import { afterEach, beforeEach, describe, expect, jest, test } from "bun:test"
23
import type { Rectangle } from "electron"
3-
import {
4-
reconcileWindowBounds,
5-
wireDisplayRecovery,
6-
type DisplayRecoveryWindow,
7-
type WindowDisplaySnapshot,
8-
} from "./display"
4+
import { reconcileWindowBounds, wireDisplayRecovery, type WindowDisplaySnapshot } from "./display"
95

106
// Exact mixed-DPI geometry from #59229: 200% primary (1560x992 DIP) + 125% upper secondary (2752x1152 DIP).
117
const primaryWorkArea: Rectangle = { x: 0, y: 0, width: 1560, height: 992 }
@@ -14,13 +10,6 @@ const secondaryWorkArea: Rectangle = { x: -102, y: -1152, width: 2752, height: 1
1410

1511
const workAreas = [primaryWorkArea, secondaryWorkArea]
1612

17-
type RecordedCalls = {
18-
setBounds: Rectangle[]
19-
titlebar: number
20-
invalidate: number
21-
capture: (Rectangle | undefined)[]
22-
}
23-
2413
describe("reconcileWindowBounds", () => {
2514
const cases: readonly {
2615
readonly name: string
@@ -102,180 +91,55 @@ describe("reconcileWindowBounds", () => {
10291
describe("wireDisplayRecovery", () => {
10392
beforeEach(() => jest.useFakeTimers())
10493

105-
afterEach(() => {
106-
jest.clearAllTimers()
107-
jest.useRealTimers()
108-
})
109-
110-
function createHarness(initialContent: Rectangle = secondaryWorkArea) {
111-
const messages = new Map<number, (wParam: Buffer, lParam: Buffer) => void>()
112-
const winEvents = new Map<string, Set<() => void>>()
113-
const powerEvents = new Map<string, Set<() => void>>()
114-
const screenEvents = new Map<string, Set<() => void>>()
115-
116-
const calls: RecordedCalls = {
117-
setBounds: [],
118-
titlebar: 0,
119-
invalidate: 0,
120-
capture: [],
121-
}
94+
afterEach(() => jest.useRealTimers())
12295

123-
const state = {
124-
destroyed: false,
125-
visible: true,
126-
minimized: false,
127-
maximized: true,
128-
fullScreen: false,
129-
bounds: { x: -112, y: -1162, width: 2218, height: 938 },
130-
contentBounds: initialContent,
131-
}
96+
test("repairs drifted maximized bounds after resume and drops its global listeners on close", () => {
97+
const display = { workArea: secondaryWorkArea }
98+
const powerMonitor = new EventEmitter()
13299

133-
const add = (map: Map<string, Set<() => void>>, event: string, fn: () => void) => {
134-
const set = map.get(event) ?? new Set()
135-
set.add(fn)
136-
map.set(event, set)
137-
}
138-
139-
const remove = (map: Map<string, Set<() => void>>, event: string, fn: () => void) => {
140-
map.get(event)?.delete(fn)
141-
}
100+
const screen = Object.assign(new EventEmitter(), {
101+
getAllDisplays: () => [display],
102+
getDisplayMatching: () => display,
103+
})
142104

143-
const emit = (map: Map<string, Set<() => void>>, event: string) => {
144-
for (const fn of [...(map.get(event) ?? [])]) fn()
145-
}
105+
const applied: Rectangle[] = []
106+
const captured: (Rectangle | undefined)[] = []
107+
const state = { bounds: secondaryWorkArea }
146108

147-
const win: DisplayRecoveryWindow = {
148-
isDestroyed: () => state.destroyed,
149-
isVisible: () => state.visible,
150-
isMinimized: () => state.minimized,
151-
isMaximized: () => state.maximized,
152-
isFullScreen: () => state.fullScreen,
109+
const win = Object.assign(new EventEmitter(), {
110+
isDestroyed: () => false,
111+
isVisible: () => true,
112+
isMinimized: () => false,
113+
isMaximized: () => true,
114+
isFullScreen: () => false,
153115
getBounds: () => state.bounds,
154-
getContentBounds: () => state.contentBounds,
155-
setBounds: (rect) => {
156-
calls.setBounds.push(rect)
157-
state.contentBounds = rect
158-
},
159-
hookWindowMessage: (msg, cb) => {
160-
messages.set(msg, cb)
161-
},
162-
on: (event, fn) => {
163-
add(winEvents, event, fn)
164-
},
165-
once: (event, fn) => {
166-
const onceFn = () => {
167-
remove(winEvents, event, onceFn)
168-
fn()
169-
}
170-
171-
add(winEvents, event, onceFn)
116+
getContentBounds: () => state.bounds,
117+
setBounds: (bounds: Rectangle) => {
118+
applied.push(bounds)
119+
state.bounds = bounds
172120
},
121+
hookWindowMessage: () => undefined,
173122
webContents: {
174-
isDestroyed: () => state.destroyed,
175-
invalidate: () => {
176-
calls.invalidate++
177-
},
178-
capturePage: (rect) => {
179-
calls.capture.push(rect)
123+
isDestroyed: () => false,
124+
invalidate: () => undefined,
125+
capturePage: (rect?: Rectangle) => {
126+
captured.push(rect)
180127

181-
return Promise.resolve(undefined)
128+
return Promise.resolve()
182129
},
183130
},
184-
}
185-
186-
const secondaryDisplay = { workArea: secondaryWorkArea }
187-
188-
wireDisplayRecovery(win, {
189-
powerMonitor: {
190-
on: (event, fn) => {
191-
add(powerEvents, event, fn)
192-
},
193-
off: (event, fn) => {
194-
remove(powerEvents, event, fn)
195-
},
196-
},
197-
screen: {
198-
getAllDisplays: () => [secondaryDisplay],
199-
getDisplayMatching: () => secondaryDisplay,
200-
on: (event, fn) => {
201-
add(screenEvents, event, fn)
202-
},
203-
off: (event, fn) => {
204-
remove(screenEvents, event, fn)
205-
},
206-
},
207-
updateTitlebar: () => {
208-
calls.titlebar++
209-
},
210131
})
211132

212-
const wParam = (value: number) => {
213-
const buf = Buffer.alloc(4)
214-
buf.writeUInt32LE(value, 0)
215-
216-
return buf
217-
}
218-
219-
return {
220-
state,
221-
calls,
222-
powerEvents,
223-
screenEvents,
224-
sendWinMessage: (msg: number, param = 0) => messages.get(msg)?.(wParam(param), Buffer.alloc(0)),
225-
emitPower: (event: string) => emit(powerEvents, event),
226-
emitScreen: (event: string) => emit(screenEvents, event),
227-
emitWin: (event: string) => emit(winEvents, event),
228-
}
229-
}
133+
wireDisplayRecovery(win, { powerMonitor, screen, updateTitlebar: () => undefined })
134+
state.bounds = { x: -102, y: -1152, width: 2201, height: 921 }
135+
powerMonitor.emit("resume")
136+
jest.advanceTimersByTime(16)
230137

231-
test.each([
232-
["WM_DPICHANGED", (h: ReturnType<typeof createHarness>) => h.sendWinMessage(0x02e0)],
233-
["WM_DISPLAYCHANGE", (h: ReturnType<typeof createHarness>) => h.sendWinMessage(0x007e)],
234-
["WM_SETTINGCHANGE(SPI_SETWORKAREA)", (h: ReturnType<typeof createHarness>) => h.sendWinMessage(0x001a, 0x002f)],
235-
[
236-
"WM_WTSSESSION_CHANGE(WTS_SESSION_UNLOCK)",
237-
(h: ReturnType<typeof createHarness>) => h.sendWinMessage(0x02b1, 0x8),
238-
],
239-
["powerMonitor resume", (h: ReturnType<typeof createHarness>) => h.emitPower("resume")],
240-
["powerMonitor unlock-screen", (h: ReturnType<typeof createHarness>) => h.emitPower("unlock-screen")],
241-
[
242-
"screen display-metrics-changed",
243-
(h: ReturnType<typeof createHarness>) => h.emitScreen("display-metrics-changed"),
244-
],
245-
] as const)("repairs drifted maximized bounds and refreshes the compositor surface on %s", (_label, trigger) => {
246-
const h = createHarness()
247-
h.state.contentBounds = { x: -102, y: -1152, width: 2201, height: 921 }
248-
249-
trigger(h)
250-
jest.advanceTimersToNextTimer()
251-
expect(h.calls.setBounds).toEqual([secondaryWorkArea])
252-
expect(h.calls.titlebar).toBe(1)
253-
expect(h.calls.invalidate).toBe(1)
254-
expect(h.calls.capture).toEqual([{ x: 0, y: 0, width: 1, height: 1 }])
255-
256-
// Settle pass re-verifies after DisplayPort link training without re-setting already-aligned bounds.
257-
jest.advanceTimersToNextTimer()
258-
expect(h.calls.setBounds).toEqual([secondaryWorkArea])
259-
expect(h.calls.titlebar).toBe(2)
260-
expect(h.calls.capture).toHaveLength(2)
261-
})
138+
expect(applied).toEqual([secondaryWorkArea])
139+
expect(captured).toEqual([{ x: 0, y: 0, width: 1, height: 1 }])
262140

263-
test("ignores non-unlock session messages and cleans up all global listeners on close", () => {
264-
const h = createHarness()
265-
// WTS_SESSION_LOCK (0x7) must not wake the compositor while the workstation is locked.
266-
h.sendWinMessage(0x02b1, 0x7)
267-
jest.runOnlyPendingTimers()
268-
expect(h.calls.invalidate).toBe(0)
141+
win.emit("closed")
269142

270-
h.emitPower("resume")
271-
h.state.destroyed = true
272-
h.emitWin("closed")
273-
jest.runOnlyPendingTimers()
274-
expect(h.calls.invalidate).toBe(0)
275-
expect(h.powerEvents.get("resume")?.size ?? 0).toBe(0)
276-
expect(h.powerEvents.get("unlock-screen")?.size ?? 0).toBe(0)
277-
expect(h.screenEvents.get("display-metrics-changed")?.size ?? 0).toBe(0)
278-
expect(h.screenEvents.get("display-added")?.size ?? 0).toBe(0)
279-
expect(h.screenEvents.get("display-removed")?.size ?? 0).toBe(0)
143+
expect([...powerMonitor.eventNames(), ...screen.eventNames()]).toEqual([])
280144
})
281145
})

‎packages/desktop/src/main/windows/early.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ export function createEarlyWindow() {
4949
const zoom = storedZoomLevel()
5050
const icons = app.isPackaged ? path.join(process.resourcesPath, "icons") : path.join(root, "../../resources/icons")
5151

52-
const deferReveal = Boolean(state.isMaximized || state.isFullScreen)
52+
// On Windows, maximize() on a hidden window shows it with SW_SHOWMAXIMIZED, so it never paints at
53+
// its normal bounds first. macOS does not reliably enter fullscreen while hidden, so it shows first.
54+
const deferReveal = process.platform === "win32" && state.isMaximized
5355

5456
const win = new BrowserWindow({
5557
x: state.x,

‎packages/desktop/src/main/windows/scheme.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { BrowserWindow, Protocol } from "electron"
1+
import electron, { type BrowserWindow } from "electron"
22

33
export const rendererProtocol = "oc"
44

@@ -32,8 +32,8 @@ export function isRendererUrl(value?: string, html = false) {
3232

3333
// Scheme privileges can only be granted before the app is ready, so the entry module calls this
3434
// before it loads anything else.
35-
export function registerRendererScheme(protocol: Pick<Protocol, "registerSchemesAsPrivileged">) {
36-
protocol.registerSchemesAsPrivileged([
35+
export function registerRendererScheme() {
36+
electron.protocol.registerSchemesAsPrivileged([
3737
{
3838
scheme: rendererProtocol,
3939
privileges: {

‎packages/desktop/src/main/windows/security.test.ts‎

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { expect, test } from "bun:test"
22
import { EventEmitter } from "node:events"
33
import type { BrowserWindow, Session, WebContents } from "electron"
4-
import { allowRendererPermissions, wireSessionSecurity } from "./security"
4+
import { allowRendererPermissions } from "./security"
55

66
type RequestHandler = NonNullable<Parameters<Session["setPermissionRequestHandler"]>[0]>
77

@@ -121,50 +121,3 @@ test("retains permissions across all live windows on a shared session and revoke
121121
expect(await shared.ask(1, "notifications")).toBe(false)
122122
expect(shared.check(null, "notifications", { isMainFrame: false })).toBe(false)
123123
})
124-
125-
test.each(["pointerLock", "keyboardLock", "geolocation", "notifications"] as const)(
126-
"denies %s on a newly created custom partition before any plugin handler is set (#57732, #45171)",
127-
async (permission) => {
128-
let onSessionCreated: ((session: Session) => void) | undefined
129-
130-
wireSessionSecurity({
131-
on(event, listener) {
132-
if (event === "session-created") onSessionCreated = listener
133-
},
134-
})
135-
136-
const custom = createSession()
137-
onSessionCreated?.(custom.session)
138-
139-
expect(await custom.ask(10, permission, { requestingUrl: "https://example.com/", isMainFrame: true })).toBe(false)
140-
expect(custom.check(10, permission, { requestingUrl: "https://example.com/", isMainFrame: true })).toBe(false)
141-
},
142-
)
143-
144-
test("blocks pointerLock and keyboardLock even when a plugin installs a permissive partition handler or resets to null", async () => {
145-
let onSessionCreated: ((session: Session) => void) | undefined
146-
147-
wireSessionSecurity({
148-
on(event, listener) {
149-
if (event === "session-created") onSessionCreated = listener
150-
},
151-
})
152-
153-
const custom = createSession()
154-
onSessionCreated?.(custom.session)
155-
156-
custom.session.setPermissionRequestHandler((_wc, _permission, callback) => callback(true))
157-
custom.session.setPermissionCheckHandler(() => true)
158-
159-
const page = { requestingUrl: "https://example.com/", isMainFrame: true }
160-
expect(await custom.ask(10, "pointerLock", page)).toBe(false)
161-
expect(custom.check(10, "pointerLock", page)).toBe(false)
162-
expect(await custom.ask(10, "keyboardLock", page)).toBe(false)
163-
expect(custom.check(10, "keyboardLock", page)).toBe(false)
164-
expect(await custom.ask(10, "clipboard-sanitized-write", page)).toBe(true)
165-
166-
custom.session.setPermissionRequestHandler(null)
167-
custom.session.setPermissionCheckHandler(null)
168-
expect(await custom.ask(10, "clipboard-sanitized-write", page)).toBe(false)
169-
expect(custom.check(10, "clipboard-sanitized-write", page)).toBe(false)
170-
})

0 commit comments

Comments
 (0)