Skip to content

Commit 9a3917e

Browse files
committed
Merge remote-tracking branch 'origin/main' into t3android/issue-21684
2 parents d09d79d + e455200 commit 9a3917e

261 files changed

Lines changed: 8539 additions & 2455 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎apps/desktop/src/app/DesktopAppIdentity.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const layerElectronApp = (calls: ElectronAppCalls) =>
4242
metadata: Effect.die("unexpected metadata read"),
4343
name: Effect.succeed("T3 Code"),
4444
systemLocale: Effect.succeed("en-US"),
45+
preferredSystemLanguages: Effect.succeed(["en-US"]),
4546
whenReady: Effect.void,
4647
requestSingleInstanceLock: Effect.succeed(true),
4748
quit: Effect.void,

‎apps/desktop/src/app/DesktopLifecycle.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ function layerElectronApp(
3939
metadata: Effect.die("unexpected metadata read"),
4040
name: Effect.succeed("T3 Code"),
4141
systemLocale: Effect.succeed("en-US"),
42+
preferredSystemLanguages: Effect.succeed(["en-US"]),
4243
whenReady: Effect.void,
4344
requestSingleInstanceLock: Effect.succeed(true),
4445
quit,

‎apps/desktop/src/electron/ElectronApp.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export class ElectronApp extends Context.Service<
4646
readonly name: Effect.Effect<string>;
4747
/** Packaged builds ship only en-US; app.getLocale() and Intl defaults do not reflect the OS locale. */
4848
readonly systemLocale: Effect.Effect<string>;
49+
readonly preferredSystemLanguages: Effect.Effect<ReadonlyArray<string>>;
4950
readonly whenReady: Effect.Effect<void, ElectronAppWhenReadyError>;
5051
readonly requestSingleInstanceLock: Effect.Effect<boolean>;
5152
readonly quit: Effect.Effect<void>;
@@ -155,6 +156,7 @@ export const make = ElectronApp.of({
155156
}
156157
return toSystemLocaleTag(identifier, hourCycle);
157158
}),
159+
preferredSystemLanguages: Effect.sync(() => Electron.app.getPreferredSystemLanguages()),
158160
whenReady: Effect.gen(function* () {
159161
const isPackaged = Electron.app.isPackaged;
160162
yield* Effect.tryPromise({

‎apps/desktop/src/ipc/DesktopIpcHandlers.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { installNotificationBadge } from "./methods/notificationBadge.ts";
66
import { getClientSettings, setClientSettings } from "./methods/clientSettings.ts";
77
import { getConnectionCatalog, setConnectionCatalog } from "./methods/connectionCatalog.ts";
88
import {
9+
getHostEnvironmentId,
910
getLocalEnvironmentEnabled,
1011
setLocalEnvironmentEnabled,
1112
} from "./methods/localEnvironment.ts";
@@ -38,6 +39,7 @@ import {
3839
getLocalEnvironmentBearerToken,
3940
installRelayTelemetryEnabled,
4041
getSystemLocale,
42+
getPreferredSystemLanguages,
4143
getWindowFullscreenState,
4244
openExternal,
4345
readMedia,
@@ -94,10 +96,12 @@ export const installDesktopIpcHandlers = Effect.fn("desktop.ipc.installHandlers"
9496
yield* ipc.handleSync(getAppBranding);
9597
yield* installRelayTelemetryEnabled();
9698
yield* ipc.handleSync(getSystemLocale);
99+
yield* ipc.handleSync(getPreferredSystemLanguages);
97100
yield* ipc.handleSync(getWindowFullscreenState);
98101
yield* ipc.handleSync(getLocalEnvironmentBootstraps);
99102
yield* ipc.handleSync(getLocalEnvironmentEnabled);
100103
yield* ipc.handle(setLocalEnvironmentEnabled);
104+
yield* ipc.handle(getHostEnvironmentId);
101105
yield* ipc.handle(getLocalEnvironmentBearerToken);
102106

103107
yield* ipc.handle(getClientSettings);

‎apps/desktop/src/ipc/channels.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,11 @@ export const CLI_COMMAND_UNINSTALL_CHANNEL = "desktop:cli-command-uninstall";
3434
export const GET_APP_BRANDING_CHANNEL = "desktop:get-app-branding";
3535
export const GET_RELAY_TELEMETRY_ENABLED_CHANNEL = "desktop:get-relay-telemetry-enabled";
3636
export const GET_SYSTEM_LOCALE_CHANNEL = "desktop:get-system-locale";
37+
export const GET_PREFERRED_SYSTEM_LANGUAGES_CHANNEL = "desktop:get-preferred-system-languages";
3738
export const GET_LOCAL_ENVIRONMENT_BOOTSTRAPS_CHANNEL = "desktop:get-local-environment-bootstraps";
3839
export const GET_LOCAL_ENVIRONMENT_ENABLED_CHANNEL = "desktop:get-local-environment-enabled";
3940
export const SET_LOCAL_ENVIRONMENT_ENABLED_CHANNEL = "desktop:set-local-environment-enabled";
41+
export const GET_HOST_ENVIRONMENT_ID_CHANNEL = "desktop:get-host-environment-id";
4042
export const GET_LOCAL_ENVIRONMENT_BEARER_TOKEN_CHANNEL =
4143
"desktop:get-local-environment-bearer-token";
4244
export const GET_CLIENT_SETTINGS_CHANNEL = "desktop:get-client-settings";

‎apps/desktop/src/ipc/methods/localEnvironment.test.ts‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import { assert, describe, it } from "@effect/vitest";
2+
import * as NodeServices from "@effect/platform-node/NodeServices";
3+
import * as FileSystem from "effect/FileSystem";
4+
import * as Path from "effect/Path";
25
import * as Effect from "effect/Effect";
36
import * as Layer from "effect/Layer";
47

@@ -10,7 +13,11 @@ import * as ElectronApp from "../../electron/ElectronApp.ts";
1013
import * as ElectronTheme from "../../electron/ElectronTheme.ts";
1114
import * as DesktopAppSettings from "../../settings/DesktopAppSettings.ts";
1215
import * as DesktopWindow from "../../window/DesktopWindow.ts";
13-
import { getLocalEnvironmentEnabled, setLocalEnvironmentEnabled } from "./localEnvironment.ts";
16+
import {
17+
getHostEnvironmentId,
18+
getLocalEnvironmentEnabled,
19+
setLocalEnvironmentEnabled,
20+
} from "./localEnvironment.ts";
1421

1522
const layerUnusedLifecycleRuntime = Layer.mergeAll(
1623
DesktopShutdown.layer,
@@ -27,6 +34,26 @@ const layerUnusedLifecycleRuntime = Layer.mergeAll(
2734
);
2835

2936
describe("local environment IPC", () => {
37+
it.effect("reads the shared home identity without creating one", () =>
38+
Effect.gen(function* () {
39+
const fs = yield* FileSystem.FileSystem;
40+
const path = yield* Path.Path;
41+
const stateDir = yield* fs.makeTempDirectoryScoped();
42+
const environment = DesktopEnvironment.DesktopEnvironment.of({
43+
path,
44+
stateDir,
45+
} as DesktopEnvironment.DesktopEnvironment["Service"]);
46+
const read = getHostEnvironmentId
47+
.handler(undefined)
48+
.pipe(Effect.provideService(DesktopEnvironment.DesktopEnvironment, environment));
49+
assert.strictEqual(yield* read, null);
50+
assert.isFalse(yield* fs.exists(path.join(stateDir, "environment-id")));
51+
yield* fs.writeFileString(path.join(stateDir, "environment-id"), " service-id\n");
52+
assert.strictEqual(yield* read, "service-id");
53+
yield* fs.writeFileString(path.join(stateDir, "environment-id"), "\n");
54+
assert.strictEqual(yield* read, null);
55+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
56+
);
3057
it.effect("relaunches only when the setting changes and keeps other settings", () => {
3158
const relaunchReasons: Array<string> = [];
3259
const layer = Layer.mergeAll(

‎apps/desktop/src/ipc/methods/localEnvironment.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import * as Effect from "effect/Effect";
2+
import * as FileSystem from "effect/FileSystem";
23
import * as Schema from "effect/Schema";
34

5+
import * as DesktopEnvironment from "../../app/DesktopEnvironment.ts";
46
import * as DesktopLifecycle from "../../app/DesktopLifecycle.ts";
57
import * as DesktopAppSettings from "../../settings/DesktopAppSettings.ts";
68
import * as IpcChannels from "../channels.ts";
@@ -28,3 +30,18 @@ export const setLocalEnvironmentEnabled = makeIpcMethod({
2830
}
2931
}),
3032
});
33+
34+
// A background service sharing this app's T3 home records its environment ID in the same state dir.
35+
export const getHostEnvironmentId = makeIpcMethod({
36+
channel: IpcChannels.GET_HOST_ENVIRONMENT_ID_CHANNEL,
37+
payload: Schema.Void,
38+
result: Schema.NullOr(Schema.String),
39+
handler: Effect.fn("desktop.ipc.localEnvironment.getHostEnvironmentId")(function* () {
40+
const environment = yield* DesktopEnvironment.DesktopEnvironment;
41+
const fileSystem = yield* FileSystem.FileSystem;
42+
const contents = yield* fileSystem
43+
.readFileString(environment.path.join(environment.stateDir, "environment-id"))
44+
.pipe(Effect.orElseSucceed(() => ""));
45+
return contents.trim() || null;
46+
}),
47+
});

‎apps/desktop/src/ipc/methods/window.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,15 @@ export const getSystemLocale = DesktopIpc.makeSyncIpcMethod({
106106
}),
107107
});
108108

109+
export const getPreferredSystemLanguages = DesktopIpc.makeSyncIpcMethod({
110+
channel: IpcChannels.GET_PREFERRED_SYSTEM_LANGUAGES_CHANNEL,
111+
result: Schema.Array(Schema.String),
112+
handler: Effect.fn("desktop.ipc.window.getPreferredSystemLanguages")(function* () {
113+
const electronApp = yield* ElectronApp.ElectronApp;
114+
return yield* electronApp.preferredSystemLanguages;
115+
}),
116+
});
117+
109118
export const getWindowFullscreenState = DesktopIpc.makeSyncIpcMethod({
110119
channel: IpcChannels.GET_WINDOW_FULLSCREEN_STATE_CHANNEL,
111120
result: Schema.Boolean,

‎apps/desktop/src/preload.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,12 @@ contextBridge.exposeInMainWorld("desktopBridge", {
107107
const result = ipcRenderer.sendSync(IpcChannels.GET_SYSTEM_LOCALE_CHANNEL);
108108
return typeof result === "string" ? result : null;
109109
},
110+
getPreferredSystemLanguages: () => {
111+
const result: unknown = ipcRenderer.sendSync(
112+
IpcChannels.GET_PREFERRED_SYSTEM_LANGUAGES_CHANNEL,
113+
);
114+
return Array.isArray(result) ? result.filter((tag) => typeof tag === "string") : [];
115+
},
110116
getLocalEnvironmentBootstraps: () => {
111117
const result = ipcRenderer.sendSync(IpcChannels.GET_LOCAL_ENVIRONMENT_BOOTSTRAPS_CHANNEL);
112118
if (!Array.isArray(result)) {
@@ -120,6 +126,7 @@ contextBridge.exposeInMainWorld("desktopBridge", {
120126
ipcRenderer.sendSync(IpcChannels.GET_LOCAL_ENVIRONMENT_ENABLED_CHANNEL) !== false,
121127
setLocalEnvironmentEnabled: (enabled) =>
122128
ipcRenderer.invoke(IpcChannels.SET_LOCAL_ENVIRONMENT_ENABLED_CHANNEL, enabled),
129+
getHostEnvironmentId: () => ipcRenderer.invoke(IpcChannels.GET_HOST_ENVIRONMENT_ID_CHANNEL),
123130
getClientSettings: () => ipcRenderer.invoke(IpcChannels.GET_CLIENT_SETTINGS_CHANNEL),
124131
setClientSettings: (settings) =>
125132
ipcRenderer.invoke(IpcChannels.SET_CLIENT_SETTINGS_CHANNEL, settings),

‎apps/desktop/src/preview/BrowserSession.test.ts‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const { fromPartition, sessions } = vi.hoisted(() => ({
1616
readonly setPermissionRequestHandler: ReturnType<typeof vi.fn>;
1717
readonly setPermissionCheckHandler: ReturnType<typeof vi.fn>;
1818
readonly setUserAgent: ReturnType<typeof vi.fn>;
19+
readonly webRequest: { readonly onBeforeSendHeaders: ReturnType<typeof vi.fn> };
1920
}
2021
>(),
2122
}));
@@ -62,6 +63,7 @@ describe("BrowserSession", () => {
6263
setPermissionRequestHandler: vi.fn(),
6364
setPermissionCheckHandler: vi.fn(),
6465
setUserAgent: vi.fn(),
66+
webRequest: { onBeforeSendHeaders: vi.fn() },
6567
};
6668
sessions.set(partition, browserSession);
6769
return browserSession;
@@ -115,10 +117,12 @@ describe("BrowserSession", () => {
115117
}).pipe(Effect.provide(layer)),
116118
);
117119

118-
it.effect("keeps the guest's effective User-Agent equal to Electron's native one", () =>
120+
it.effect("keeps the native User-Agent except for the Electron token sent to Teams", () =>
119121
Effect.gen(function* () {
120122
const nativeUserAgent =
121123
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) T3Code(Alpha)/0.0.33 Chrome/146.0.7680.216 Electron/41.5.0 Safari/537.36";
124+
const teamsUserAgent =
125+
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) T3Code(Alpha)/0.0.33 Chrome/146.0.7680.216 Safari/537.36";
122126
fromPartition.mockReset();
123127
fromPartition.mockImplementation((partition: string) => {
124128
let userAgent = nativeUserAgent;
@@ -131,6 +135,7 @@ describe("BrowserSession", () => {
131135
setUserAgent: vi.fn((next: string) => {
132136
userAgent = next;
133137
}),
138+
webRequest: { onBeforeSendHeaders: vi.fn() },
134139
};
135140
sessions.set(partition, browserSession);
136141
return browserSession;
@@ -143,6 +148,29 @@ describe("BrowserSession", () => {
143148
const browserSession = sessions.get(partition);
144149
assert.isDefined(browserSession);
145150
assert.strictEqual(browserSession.getUserAgent(), nativeUserAgent);
151+
152+
// Teams redirects Electron to an error page before sign-in; every other host sees the native one.
153+
const calls = browserSession.webRequest.onBeforeSendHeaders.mock.calls;
154+
assert.strictEqual(calls.length, 1);
155+
const [filter, listener] = calls[0] ?? [];
156+
assert.deepStrictEqual(filter, {
157+
urls: ["https://teams.cloud.microsoft/*", "https://teams.microsoft.com/*"],
158+
});
159+
const send = (requestHeaders: Record<string, string>) => {
160+
let response: { requestHeaders?: Record<string, string> } | undefined;
161+
listener({ requestHeaders }, (next: typeof response) => {
162+
response = next;
163+
});
164+
return response;
165+
};
166+
for (const key of ["User-Agent", "user-agent"]) {
167+
const requestHeaders = { [key]: nativeUserAgent, Accept: "*/*" };
168+
assert.deepStrictEqual(send(requestHeaders), {
169+
requestHeaders: { [key]: teamsUserAgent, Accept: "*/*" },
170+
});
171+
assert.strictEqual(requestHeaders[key], nativeUserAgent);
172+
}
173+
assert.deepStrictEqual(send({ Accept: "*/*" }), {});
146174
}).pipe(Effect.provide(layer)),
147175
);
148176

0 commit comments

Comments
 (0)