Skip to content

Commit 10882a0

Browse files
committed
fix(desktop): repair the Windows notification shim's false denial on read
On Windows the notification plugin's injected shim stamps `denied` at startup without ever consulting the backend, whose `permission_state()` returns `Granted` unconditionally on desktop. Its bootstrap short-circuits on the `__TEMPLATE_windows__` literal and compares its own freshly initialised `'default'` against `'granted'`. `getDesktopNotificationPermissionState()` trusted that value, so the mount-time read reported `denied` and the settings effect turned `desktopEnabled` off and persisted it on every launch -- which is why fixing only the enable path leaves a Windows user re-enabling alerts after each restart. Repair the value at the read instead of reporting it. Inside the Windows Tauri app a `denied` reading is never a real denial, and `requestPermission()` is the only call that reaches the backend and rewrites the shim's cached value. It is self-limiting: the next read short-circuits as `granted` and never requests again. Repairing at the read rather than at the toggle also covers `sendDesktopNotification()`, which gates delivery on `=== "granted"` and would otherwise drop notifications at boot. `denied` stays terminal on macOS, on Linux and on ordinary Windows web pages, and a denial that survives the request is still reported as `denied`. Refs #2445 Signed-off-by: VSCteam <[email protected]>
1 parent cd2aa5c commit 10882a0

4 files changed

Lines changed: 189 additions & 2 deletions

File tree

‎desktop/src/features/notifications/lib/desktop.test.mjs‎

Lines changed: 118 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,55 @@ class ThrowingNotification {
2323

2424
globalThis.window = { Notification: ThrowingNotification };
2525

26-
const { sendDesktopNotification } = await import("./desktop.ts");
26+
const { sendDesktopNotification, getDesktopNotificationPermissionState } =
27+
await import("./desktop.ts");
28+
29+
// Stands in for the plugin's injected shim: `permission` is a cached value and
30+
// only `requestPermission()` reaches the backend and rewrites it.
31+
function shimNotification(initialPermission, grantedPermission = "granted") {
32+
class ShimNotification {
33+
static permission = initialPermission;
34+
static requestCount = 0;
35+
36+
static async requestPermission() {
37+
ShimNotification.requestCount += 1;
38+
ShimNotification.permission = grantedPermission;
39+
return grantedPermission;
40+
}
41+
42+
close() {}
43+
}
44+
45+
return ShimNotification;
46+
}
47+
48+
async function withEnvironment({ platform, isTauri, notification }, callback) {
49+
const originalNavigator = Object.getOwnPropertyDescriptor(
50+
globalThis,
51+
"navigator",
52+
);
53+
const originalNotification = window.Notification;
54+
const originalIsTauri = globalThis.isTauri;
55+
56+
Object.defineProperty(globalThis, "navigator", {
57+
configurable: true,
58+
value: { platform, userAgent: "" },
59+
});
60+
window.Notification = notification;
61+
globalThis.isTauri = isTauri;
62+
63+
try {
64+
return await callback();
65+
} finally {
66+
if (originalNavigator) {
67+
Object.defineProperty(globalThis, "navigator", originalNavigator);
68+
} else {
69+
delete globalThis.navigator;
70+
}
71+
window.Notification = originalNotification;
72+
globalThis.isTauri = originalIsTauri;
73+
}
74+
}
2775

2876
test("constructor failure is a delivery miss and does not prevent a later notification", async (t) => {
2977
const warnings = [];
@@ -50,3 +98,72 @@ test("constructor failure is a delivery miss and does not prevent a later notifi
5098
},
5199
]);
52100
});
101+
102+
test("Windows Tauri repairs the shim's false denied state on read", async () => {
103+
const Notification = shimNotification("denied");
104+
105+
const permission = await withEnvironment(
106+
{ platform: "Win32", isTauri: true, notification: Notification },
107+
() => getDesktopNotificationPermissionState(),
108+
);
109+
110+
assert.equal(permission, "granted");
111+
assert.equal(Notification.requestCount, 1);
112+
});
113+
114+
test("the repaired state is cached, so later reads do not request again", async () => {
115+
const Notification = shimNotification("denied");
116+
117+
await withEnvironment(
118+
{ platform: "Win32", isTauri: true, notification: Notification },
119+
async () => {
120+
await getDesktopNotificationPermissionState();
121+
const second = await getDesktopNotificationPermissionState();
122+
assert.equal(second, "granted");
123+
},
124+
);
125+
126+
assert.equal(Notification.requestCount, 1);
127+
});
128+
129+
test("a denial that survives the request is reported as denied", async () => {
130+
const Notification = shimNotification("denied", "denied");
131+
132+
const permission = await withEnvironment(
133+
{ platform: "Win32", isTauri: true, notification: Notification },
134+
() => getDesktopNotificationPermissionState(),
135+
);
136+
137+
assert.equal(permission, "denied");
138+
assert.equal(Notification.requestCount, 1);
139+
});
140+
141+
test("denied stays terminal outside the Windows Tauri app", async () => {
142+
for (const environment of [
143+
{ label: "Windows web", platform: "Win32", isTauri: false },
144+
{ label: "Linux Tauri", platform: "Linux x86_64", isTauri: true },
145+
{ label: "Linux web", platform: "Linux x86_64", isTauri: false },
146+
]) {
147+
const Notification = shimNotification("denied");
148+
149+
const permission = await withEnvironment(
150+
{ ...environment, notification: Notification },
151+
() => getDesktopNotificationPermissionState(),
152+
);
153+
154+
assert.equal(permission, "denied", environment.label);
155+
assert.equal(Notification.requestCount, 0, environment.label);
156+
}
157+
});
158+
159+
test("granted is returned untouched and never triggers a request", async () => {
160+
const Notification = shimNotification("granted");
161+
162+
const permission = await withEnvironment(
163+
{ platform: "Win32", isTauri: true, notification: Notification },
164+
() => getDesktopNotificationPermissionState(),
165+
);
166+
167+
assert.equal(permission, "granted");
168+
assert.equal(Notification.requestCount, 0);
169+
});

‎desktop/src/features/notifications/lib/desktop.ts‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ import {
66
onAction,
77
requestPermission,
88
} from "@tauri-apps/plugin-notification";
9-
import { isLinuxPlatform, isMacPlatform } from "@/shared/lib/platform";
9+
import {
10+
isLinuxPlatform,
11+
isMacPlatform,
12+
isWindowsPlatform,
13+
} from "@/shared/lib/platform";
1014

1115
// Backend event emitted when a native Linux notification is clicked or a
1216
// queued macOS activation becomes available. See src-tauri notification code.
@@ -147,6 +151,25 @@ export async function getDesktopNotificationPermissionState(): Promise<DesktopNo
147151
}
148152

149153
if (window.Notification.permission !== "default") {
154+
// On Windows the notification plugin's injected shim stamps `denied` at
155+
// startup without ever consulting the backend — `permission_state()`
156+
// returns `Granted` unconditionally on desktop — so a `denied` reading
157+
// inside the Windows Tauri app is never a real denial. Repair it by
158+
// requesting once: on Windows `request_permission()` raises no prompt,
159+
// returns `Granted`, and rewrites the shim's cached value, so subsequent
160+
// reads short-circuit here as `granted`.
161+
if (
162+
window.Notification.permission === "denied" &&
163+
isTauri() &&
164+
isWindowsPlatform()
165+
) {
166+
try {
167+
return await requestDesktopNotificationAccess();
168+
} catch {
169+
return window.Notification.permission;
170+
}
171+
}
172+
150173
return window.Notification.permission;
151174
}
152175

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import assert from "node:assert/strict";
2+
import test from "node:test";
3+
4+
import { isWindowsPlatform } from "./platform.ts";
5+
6+
function withNavigator(navigator, callback) {
7+
const original = Object.getOwnPropertyDescriptor(globalThis, "navigator");
8+
Object.defineProperty(globalThis, "navigator", {
9+
configurable: true,
10+
value: navigator,
11+
});
12+
13+
try {
14+
callback();
15+
} finally {
16+
if (original) {
17+
Object.defineProperty(globalThis, "navigator", original);
18+
} else {
19+
delete globalThis.navigator;
20+
}
21+
}
22+
}
23+
24+
test("Windows detection accepts the platform strings WebView2 reports", () => {
25+
for (const platform of ["Win32", "Win64", "Windows"]) {
26+
withNavigator({ platform, userAgent: "" }, () => {
27+
assert.equal(isWindowsPlatform(), true, platform);
28+
});
29+
}
30+
});
31+
32+
test("Windows detection rejects the other desktop platforms", () => {
33+
for (const platform of ["MacIntel", "Darwin", "Linux x86_64"]) {
34+
withNavigator({ platform, userAgent: "" }, () => {
35+
assert.equal(isWindowsPlatform(), false, platform);
36+
});
37+
}
38+
});

‎desktop/src/shared/lib/platform.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ export function isLinuxPlatform(): boolean {
2323
);
2424
}
2525

26+
/** Returns true on Windows desktops. */
27+
export function isWindowsPlatform(): boolean {
28+
if (typeof navigator === "undefined") {
29+
return false;
30+
}
31+
32+
return /^win/i.test(navigator.platform);
33+
}
34+
2635
/**
2736
* The platform's normal application-shortcut modifier:
2837
* - macOS: Command (Meta)

0 commit comments

Comments
 (0)