Skip to content

Small step in tidying the IPCs #30125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
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
6 changes: 5 additions & 1 deletion src/@types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import "@types/modernizr";

import type { ModuleLoader } from "@element-hq/element-web-module-api";
import type { logger } from "matrix-js-sdk/src/logger";
import type { CallState } from "matrix-js-sdk/src/webrtc/call";
import type ContentMessages from "../ContentMessages";
import { type IMatrixClientPeg } from "../MatrixClientPeg";
import type ToastStore from "../stores/ToastStore";
Expand Down Expand Up @@ -51,7 +52,6 @@ import type { RoomListStoreV3Class } from "../stores/room-list-v3/RoomListStoreV
/* eslint-disable @typescript-eslint/naming-convention */

type ElectronChannel =
| "app_onAction"
| "before-quit"
| "check_updates"
| "install_update"
Expand Down Expand Up @@ -133,14 +133,18 @@ declare global {
send(channel: ElectronChannel, ...args: any[]): void;
// Initialisation
initialise(): Promise<{
version: string;
protocol: string;
sessionId: string;
config: IConfigOptions;
supportedSettings: Record<string, boolean>;
canSelfUpdate: boolean;
}>;
// Settings
setSettingValue(settingName: string, value: any): Promise<void>;
getSettingValue(settingName: string): Promise<any>;
// Lifecycle
onCallState(callState: CallState): void;
}

interface DesktopCapturerSource {
Expand Down
32 changes: 13 additions & 19 deletions src/vector/platform/ElectronPlatform.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,7 @@ export default class ElectronPlatform extends BasePlatform {
private readonly eventIndexManager: BaseEventIndexManager = new SeshatIndexManager();
private readonly initialised: Promise<void>;
private readonly electron: Electron;
private protocol!: string;
private sessionId!: string;
private config!: IConfigOptions;
private supportedSettings?: Record<string, boolean>;
private parameters?: Awaited<ReturnType<Electron["initialise"]>>;

public constructor() {
super();
Expand Down Expand Up @@ -189,21 +186,17 @@ export default class ElectronPlatform extends BasePlatform {
super.onAction(payload);
// Whitelist payload actions, no point sending most across
if (["call_state"].includes(payload.action)) {
this.electron.send("app_onAction", payload);
this.electron.onCallState(payload.state);
}
}

private async initialise(): Promise<void> {
const { protocol, sessionId, config, supportedSettings } = await this.electron.initialise();
this.protocol = protocol;
this.sessionId = sessionId;
this.config = config;
this.supportedSettings = supportedSettings;
this.parameters = await this.electron.initialise();
}

public async getConfig(): Promise<IConfigOptions | undefined> {
await this.initialised;
return this.config;
return this.parameters?.config;
}

private onBreadcrumbsUpdate = (): void => {
Expand Down Expand Up @@ -298,12 +291,13 @@ export default class ElectronPlatform extends BasePlatform {
}

public async getAppVersion(): Promise<string> {
return this.ipc.call("getAppVersion");
await this.initialised;
return this.parameters!.version;
}

public supportsSetting(settingName?: string): boolean {
if (settingName === undefined) return true;
return this.supportedSettings?.[settingName] === true;
return this.parameters?.supportedSettings[settingName] === true;
}

public getSettingValue(settingName: string): Promise<any> {
Expand All @@ -315,8 +309,8 @@ export default class ElectronPlatform extends BasePlatform {
}

public async canSelfUpdate(): Promise<boolean> {
const feedUrl = await this.ipc.call("getUpdateFeedUrl");
return Boolean(feedUrl);
await this.initialised;
return this.parameters!.canSelfUpdate;
}

public startUpdateCheck(): void {
Expand Down Expand Up @@ -352,7 +346,7 @@ export default class ElectronPlatform extends BasePlatform {
}

public async setLanguage(preferredLangs: string[]): Promise<any> {
return this.ipc.call("setLanguage", preferredLangs);
return this.electron.setSettingValue("locale", preferredLangs);
}

public setSpellCheckEnabled(enabled: boolean): void {
Expand Down Expand Up @@ -397,7 +391,7 @@ export default class ElectronPlatform extends BasePlatform {
public getSSOCallbackUrl(fragmentAfterLogin?: string): URL {
const url = super.getSSOCallbackUrl(fragmentAfterLogin);
url.protocol = "element";
url.searchParams.set(SSO_ID_KEY, this.sessionId);
url.searchParams.set(SSO_ID_KEY, this.parameters!.sessionId);
return url;
}

Expand Down Expand Up @@ -475,15 +469,15 @@ export default class ElectronPlatform extends BasePlatform {
}

public getOidcClientState(): string {
return `:${SSO_ID_KEY}:${this.sessionId}`;
return `:${SSO_ID_KEY}:${this.parameters!.sessionId}`;
}

/**
* The URL to return to after a successful OIDC authentication
*/
public getOidcCallbackUrl(): URL {
const url = super.getOidcCallbackUrl();
url.protocol = this.protocol;
url.protocol = this.parameters!.protocol;
// Trim the double slash into a single slash to comply with https://datatracker.ietf.org/doc/html/rfc8252#section-7.1
if (url.href.startsWith(`${url.protocol}//`)) {
url.href = url.href.replace("://", ":/");
Expand Down
8 changes: 3 additions & 5 deletions test/unit-tests/vector/platform/ElectronPlatform-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ describe("ElectronPlatform", () => {
}),
setSettingValue: jest.fn().mockResolvedValue(undefined),
getSettingValue: jest.fn().mockResolvedValue(undefined),
onCallState: jest.fn(),
} as unknown as MockedObject<Electron>;

const dispatchSpy = jest.spyOn(dispatcher, "dispatch");
Expand Down Expand Up @@ -399,10 +400,7 @@ describe("ElectronPlatform", () => {
true,
);

const ipcMessage = mockElectron.send.mock.calls.find((call) => call[0] === "app_onAction");
expect(ipcMessage![1]).toEqual({
action: "call_state",
state: "connected",
});
const ipcMessage = mockElectron.onCallState.mock.calls[0];
expect(ipcMessage![0]).toEqual("connected");
});
});
Loading