Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions website/electron/ipc-registrar.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 14 additions & 3 deletions website/electron/test/ipc-registrar.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: "",
Expand Down Expand Up @@ -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`,
Expand Down Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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 });
Expand Down
13 changes: 9 additions & 4 deletions website/src/hooks/useLocalGateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
19 changes: 15 additions & 4 deletions website/src/test/useLocalGateway.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
})