diff --git a/website/electron/ipc-registrar.js b/website/electron/ipc-registrar.js index e1734de60cf2..ce0d3d9dcbcd 100644 --- a/website/electron/ipc-registrar.js +++ b/website/electron/ipc-registrar.js @@ -969,10 +969,19 @@ function createIpcRegistrar({ }); // This setting deliberately has next-launch semantics. Toggling it never - // starts or stops the gateway which owns the current session. - ipcMain.handle("local-gateway:get", () => isLocalGatewayEnabled(store)); - ipcMain.handle("local-gateway:set", (_event, enabled) => - setLocalGatewayEnabled(store, enabled)); + // starts or stops the gateway which owns the current session. It decides + // whether THIS machine runs a gateway, so only the main window's dashboard + // reads or writes it (assertMainWindowDashboard, below; called only after + // registration ends), like the close-action and tray rows: a connection + // window shares this preload and is pointed at another machine's gateway. + ipcMain.handle("local-gateway:get", async (event) => { + assertMainWindowDashboard(event, "local-gateway:get"); + return isLocalGatewayEnabled(store); + }); + ipcMain.handle("local-gateway:set", async (event, enabled) => { + assertMainWindowDashboard(event, "local-gateway:set"); + return setLocalGatewayEnabled(store, enabled); + }); // THE local-dashboard gate. Every channel whose answer is a fact about the // machine in front of the user — rather than about the gateway a window diff --git a/website/electron/test/ipc-registrar.test.js b/website/electron/test/ipc-registrar.test.js index 2ead4b597742..0cd4fd9d04d7 100644 --- a/website/electron/test/ipc-registrar.test.js +++ b/website/electron/test/ipc-registrar.test.js @@ -206,9 +206,6 @@ test("shell handlers preserve sender, argument, and return shapes", async () => h.listeners.get("badge:set")(event, 7.9); assert.deepEqual(lastCall(h.appCalls, "setBadgeCount").slice(1), [7]); - assert.equal(h.handlers.get("local-gateway:get")(event), true); - assert.equal(h.handlers.get("local-gateway:set")(event, false), false); - assert.equal(h.store.data.runLocalGateway, false); assert.deepEqual(h.handlers.get("global-hotkey:get")(event), { accelerator: "", requested: "", @@ -1463,6 +1460,8 @@ const MAIN_WINDOW_SETTING_CHANNELS = [ ["tray-icon:set", [false]], ["minimize-to-tray:get", []], ["minimize-to-tray:set", [true]], + ["local-gateway:get", []], + ["local-gateway:set", [false]], ]; const MAIN_WINDOW_ONLY = (channel) => ({ message: `${channel} is restricted to the main window's dashboard`, @@ -1491,6 +1490,11 @@ function assertCloseActionRefused(h, channel) { false, `${channel}: a refused sender must not read or write the setting`, ); + assert.equal( + h.store.writes.some(([key]) => key === "runLocalGateway"), + false, + `${channel}: a refused sender must not write the local-gateway choice`, + ); } test("close-action channels answer the main window's dashboard, including on a remote gateway", async () => { @@ -1549,6 +1553,13 @@ test("main-window setting channels refuse when there is no main window", async ( } }); +test("local-gateway channels answer the main window's dashboard, including on a remote gateway", async () => { + const h = closeActionHarness({ wslGatewayLocal: false, primaryPortOwner: "foreign" }); + assert.equal(await h.handlers.get("local-gateway:get")(mainFrameEvent()), true); + assert.equal(await h.handlers.get("local-gateway:set")(mainFrameEvent(), false), false); + assert.equal(h.store.data.runLocalGateway, false); +}); + test("tray-icon channels answer the main window's dashboard, including on a remote gateway", async () => { const h = closeActionHarness({ wslGatewayLocal: false, primaryPortOwner: "foreign" }); assert.deepEqual(await h.handlers.get("tray-icon:get")(mainFrameEvent()), { show: true, canHide: false }); diff --git a/website/src/hooks/useLocalGateway.ts b/website/src/hooks/useLocalGateway.ts index a874f65d67ec..b2367e0eab9a 100644 --- a/website/src/hooks/useLocalGateway.ts +++ b/website/src/hooks/useLocalGateway.ts @@ -15,24 +15,29 @@ const localGatewayAPI = (): LocalGatewayAPI | undefined => window.localGatewayAP * shipped default instead of flashing the opposite state. */ export function useLocalGateway() { - const supported = !!localGatewayAPI() + const [refused, setRefused] = useState(false) + const supported = !!localGatewayAPI() && !refused const [enabled, setEnabled] = useState(true) + // main.js answers only the main window's dashboard. A refused read means this + // window (a connection window to another machine) cannot change the choice, + // so the control is hidden rather than shown with a value it cannot know. useEffect(() => { const api = localGatewayAPI() if (!api) return let alive = true - void api.get().then(v => { if (alive) setEnabled(v) }).catch(() => {}) + void api.get().then(v => { if (alive) setEnabled(v) }).catch(() => { if (alive) setRefused(true) }) return () => { alive = false } }, []) // The stored value is authoritative, so the switch renders what main.js - // actually wrote rather than what was asked for. + // actually wrote rather than what was asked for, and a refused write puts + // the switch back where it was. const setLocalGatewayEnabled = useCallback((next: boolean) => { const api = localGatewayAPI() if (!api) return setEnabled(next) - void api.set(next).then(v => setEnabled(v)).catch(() => {}) + void api.set(next).then(v => setEnabled(v)).catch(() => setEnabled(!next)) }, []) return { localGatewayEnabled: enabled, localGatewaySupported: supported, setLocalGatewayEnabled } diff --git a/website/src/test/useLocalGateway.test.ts b/website/src/test/useLocalGateway.test.ts index 1fa1063184c8..72635e518fda 100644 --- a/website/src/test/useLocalGateway.test.ts +++ b/website/src/test/useLocalGateway.test.ts @@ -61,13 +61,24 @@ describe('useLocalGateway', () => { await waitFor(() => expect(result.current.localGatewayEnabled).toBe(true)) }) - it('survives a rejecting bridge without losing its current value', async () => { + it('hides the switch when main.js refuses the read, as it does for a connection window', async () => { installBridge({ - get: () => Promise.reject(new Error('ipc gone')), - set: () => Promise.reject(new Error('ipc gone')), + get: () => Promise.reject(new Error("local-gateway:get is restricted to the main window's dashboard")), + set: vi.fn(), }) const { result } = renderHook(() => useLocalGateway()) + await waitFor(() => expect(result.current.localGatewaySupported).toBe(false)) + }) + + it('puts the switch back when main.js refuses the write', async () => { + installBridge({ + get: () => Promise.resolve(true), + set: () => Promise.reject(new Error("local-gateway:set is restricted to the main window's dashboard")), + }) + const { result } = renderHook(() => useLocalGateway()) + await waitFor(() => expect(result.current.localGatewayEnabled).toBe(true)) + await act(async () => { result.current.setLocalGatewayEnabled(false) }) - expect(result.current.localGatewayEnabled).toBe(false) + await waitFor(() => expect(result.current.localGatewayEnabled).toBe(true)) }) })