Skip to content
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

Use isolated IPC API #16137

Merged
merged 1 commit into from Jan 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 21 additions & 2 deletions src/@types/global.d.ts
@@ -1,5 +1,5 @@
/*
Copyright 2020 New Vector Ltd
Copyright 2020, 2021 New Vector Ltd

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -17,13 +17,32 @@ limitations under the License.
import "matrix-react-sdk/src/@types/global"; // load matrix-react-sdk's type extensions first
import type {Renderer} from "react-dom";

type ElectronChannel =
"app_onAction" |
"before-quit" |
"check_updates" |
"install_update" |
"ipcCall" |
"ipcReply" |
"loudNotification" |
"preferences" |
"seshat" |
"seshatReply" |
"setBadgeCount" |
"update-downloaded" |
"userDownloadCompleted" |
"userDownloadOpen";

declare global {
interface Window {
mxSendRageshake: (text: string, withLogs?: boolean) => void;
matrixChat: ReturnType<Renderer>;

// electron-only
ipcRenderer: any;
electron: {
on(channel: ElectronChannel, listener: (event: Event, ...args: any[]) => void): void;
send(channel: ElectronChannel, ...args: any[]): void;
}

// opera-only
opera: any;
Expand Down
4 changes: 2 additions & 2 deletions src/vector/init.tsx
@@ -1,8 +1,8 @@
/*
Copyright 2015, 2016 OpenMarket Ltd
Copyright 2017 Vector Creations Ltd
Copyright 2018, 2019, 2020 New Vector Ltd
Copyright 2019 Michael Telatynski <7t3chguy@gmail.com>
Copyright 2018 - 2021 New Vector Ltd

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -39,7 +39,7 @@ import { initRageshake } from "./rageshakesetup";
export const rageshakePromise = initRageshake();

export function preparePlatform() {
if (window.ipcRenderer) {
if (window.electron) {
console.log("Using Electron platform");
PlatformPeg.set(new ElectronPlatform());
} else if (window.matchMedia('(display-mode: standalone)').matches) {
Expand Down
35 changes: 17 additions & 18 deletions src/vector/platform/ElectronPlatform.tsx
@@ -1,9 +1,8 @@
/*
Copyright 2016 Aviral Dasgupta
Copyright 2016 OpenMarket Ltd
Copyright 2018 New Vector Ltd
Copyright 2019 Michael Telatynski <7t3chguy@gmail.com>
Copyright 2020 The Matrix.org Foundation C.I.C.
Copyright 2018 - 2021 New Vector Ltd

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -49,7 +48,7 @@ import {CheckUpdatesPayload} from "matrix-react-sdk/src/dispatcher/payloads/Chec
import ToastStore from "matrix-react-sdk/src/stores/ToastStore";
import GenericExpiringToast from "matrix-react-sdk/src/components/views/toasts/GenericExpiringToast";

const ipcRenderer = window.ipcRenderer;
const electron = window.electron;
const isMac = navigator.platform.toUpperCase().includes('MAC');

function platformFriendlyName(): string {
Expand All @@ -74,7 +73,7 @@ function platformFriendlyName(): string {
function _onAction(payload: ActionPayload) {
// Whitelist payload actions, no point sending most across
if (['call_state'].includes(payload.action)) {
ipcRenderer.send('app_onAction', payload);
electron.send('app_onAction', payload);
}
}

Expand Down Expand Up @@ -104,15 +103,15 @@ class SeshatIndexManager extends BaseEventIndexManager {
constructor() {
super();

ipcRenderer.on('seshatReply', this._onIpcReply);
electron.on('seshatReply', this._onIpcReply);
}

async _ipcCall(name: string, ...args: any[]): Promise<any> {
// TODO this should be moved into the preload.js file.
const ipcCallId = ++this.nextIpcCallId;
return new Promise((resolve, reject) => {
this.pendingIpcCalls[ipcCallId] = {resolve, reject};
window.ipcRenderer.send('seshat', {id: ipcCallId, name, args});
window.electron.send('seshat', {id: ipcCallId, name, args});
});
}

Expand Down Expand Up @@ -230,29 +229,29 @@ export default class ElectronPlatform extends VectorBasePlatform {
false if there is not
or the error if one is encountered
*/
ipcRenderer.on('check_updates', (event, status) => {
electron.on('check_updates', (event, status) => {
dis.dispatch<CheckUpdatesPayload>({
action: Action.CheckUpdates,
...getUpdateCheckStatus(status),
});
});

// try to flush the rageshake logs to indexeddb before quit.
ipcRenderer.on('before-quit', function() {
electron.on('before-quit', function() {
console.log('element-desktop closing');
rageshake.flush();
});

ipcRenderer.on('ipcReply', this._onIpcReply);
ipcRenderer.on('update-downloaded', this.onUpdateDownloaded);
electron.on('ipcReply', this._onIpcReply);
electron.on('update-downloaded', this.onUpdateDownloaded);

ipcRenderer.on('preferences', () => {
electron.on('preferences', () => {
dis.fire(Action.ViewUserSettings);
});

ipcRenderer.on('userDownloadCompleted', (ev, {path, name}) => {
electron.on('userDownloadCompleted', (ev, {path, name}) => {
const onAccept = () => {
ipcRenderer.send('userDownloadOpen', {path});
electron.send('userDownloadOpen', {path});
};

ToastStore.sharedInstance().addOrReplaceToast({
Expand Down Expand Up @@ -328,7 +327,7 @@ export default class ElectronPlatform extends VectorBasePlatform {
if (this.notificationCount === count) return;
super.setNotificationCount(count);

ipcRenderer.send('setBadgeCount', count);
electron.send('setBadgeCount', count);
}

supportsNotifications(): boolean {
Expand Down Expand Up @@ -371,7 +370,7 @@ export default class ElectronPlatform extends VectorBasePlatform {
}

loudNotification(ev: Event, room: Object) {
ipcRenderer.send('loudNotification');
electron.send('loudNotification');
}

async getAppVersion(): Promise<string> {
Expand Down Expand Up @@ -423,14 +422,14 @@ export default class ElectronPlatform extends VectorBasePlatform {

startUpdateCheck() {
super.startUpdateCheck();
ipcRenderer.send('check_updates');
electron.send('check_updates');
}

installUpdate() {
// IPC to the main process to install the update, since quitAndInstall
// doesn't fire the before-quit event so the main process needs to know
// it should exit.
ipcRenderer.send('install_update');
electron.send('install_update');
}

getDefaultDeviceDisplayName(): string {
Expand Down Expand Up @@ -460,7 +459,7 @@ export default class ElectronPlatform extends VectorBasePlatform {
const ipcCallId = ++this.nextIpcCallId;
return new Promise((resolve, reject) => {
this.pendingIpcCalls[ipcCallId] = {resolve, reject};
window.ipcRenderer.send('ipcCall', {id: ipcCallId, name, args});
window.electron.send('ipcCall', {id: ipcCallId, name, args});
// Maybe add a timeout to these? Probably not necessary.
});
}
Expand Down