From 994c412bd2b7ea5be8553da5761485c72bb0a69d Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Sun, 25 Apr 2021 19:34:34 -0700 Subject: [PATCH] renderer: Use ipcRenderer.sendTo to communicate with other WebContents. Signed-off-by: Anders Kaseorg --- app/renderer/js/components/webview.ts | 2 +- app/renderer/js/main.ts | 28 ++++++++----------- app/renderer/js/notification/helpers.ts | 8 ++++-- .../js/pages/preference/general-section.ts | 6 +++- app/renderer/js/tray.ts | 5 ++-- 5 files changed, 26 insertions(+), 23 deletions(-) diff --git a/app/renderer/js/components/webview.ts b/app/renderer/js/components/webview.ts index 0b829613..2c736824 100644 --- a/app/renderer/js/components/webview.ts +++ b/app/renderer/js/components/webview.ts @@ -335,6 +335,6 @@ export default class WebView { async send(channel: string, ...parameters: unknown[]): Promise { await this.domReady; - await this.$el!.send(channel, ...parameters); + ipcRenderer.sendTo(this.$el!.getWebContentsId(), channel, ...parameters); } } diff --git a/app/renderer/js/main.ts b/app/renderer/js/main.ts index 9684c018..79f15a0b 100644 --- a/app/renderer/js/main.ts +++ b/app/renderer/js/main.ts @@ -783,10 +783,8 @@ class ServerManagerView { updateGeneralSettings(setting: string, value: unknown): void { if (this.getActiveWebview()) { - const webContents = remote.webContents.fromId( - this.getActiveWebview().getWebContentsId(), - ); - webContents.send(setting, value); + const webContentsId = this.getActiveWebview().getWebContentsId(); + ipcRenderer.sendTo(webContentsId, setting, value); } } @@ -1065,10 +1063,8 @@ class ServerManagerView { "toggle-silent", newSettings.silent, ); - const webContents = remote.webContents.fromId( - this.getActiveWebview().getWebContentsId(), - ); - webContents.send("toggle-dnd", state, newSettings); + const webContentsId = this.getActiveWebview().getWebContentsId(); + ipcRenderer.sendTo(webContentsId, "toggle-dnd", state, newSettings); }, ); @@ -1197,22 +1193,22 @@ class ServerManagerView { await this.openSettings("AddServer"); }); - ipcRenderer.on("set-active", async () => { + ipcRenderer.on("set-active", () => { const webviews: NodeListOf = document.querySelectorAll( "webview", ); - await Promise.all( - [...webviews].map(async (webview) => webview.send("set-active")), - ); + for (const webview of webviews) { + ipcRenderer.sendTo(webview.getWebContentsId(), "set-active"); + } }); - ipcRenderer.on("set-idle", async () => { + ipcRenderer.on("set-idle", () => { const webviews: NodeListOf = document.querySelectorAll( "webview", ); - await Promise.all( - [...webviews].map(async (webview) => webview.send("set-idle")), - ); + for (const webview of webviews) { + ipcRenderer.sendTo(webview.getWebContentsId(), "set-idle"); + } }); ipcRenderer.on("open-network-settings", async () => { diff --git a/app/renderer/js/notification/helpers.ts b/app/renderer/js/notification/helpers.ts index 8b5ff1ef..fac8c188 100644 --- a/app/renderer/js/notification/helpers.ts +++ b/app/renderer/js/notification/helpers.ts @@ -1,4 +1,4 @@ -import {remote} from "electron"; +import {ipcRenderer, remote} from "electron"; // Do not change this export const appId = "org.zulip.zulip-electron"; @@ -10,5 +10,9 @@ const webContentsId = webContents.id; // This function will focus the server that sent // the notification. Main function implemented in main.js export function focusCurrentServer(): void { - currentWindow.webContents.send("focus-webview-with-id", webContentsId); + ipcRenderer.sendTo( + currentWindow.webContents.id, + "focus-webview-with-id", + webContentsId, + ); } diff --git a/app/renderer/js/pages/preference/general-section.ts b/app/renderer/js/pages/preference/general-section.ts index d2749e76..f54acb68 100644 --- a/app/renderer/js/pages/preference/general-section.ts +++ b/app/renderer/js/pages/preference/general-section.ts @@ -352,7 +352,11 @@ export function initGeneralSection(props: GeneralSectionProps): void { const newValue = !ConfigUtil.getConfigItem("silent", true); ConfigUtil.setConfigItem("silent", newValue); updateSilentOption(); - currentBrowserWindow.webContents.send("toggle-silent", newValue); + ipcRenderer.sendTo( + currentBrowserWindow.webContents.id, + "toggle-silent", + newValue, + ); }, }); } diff --git a/app/renderer/js/tray.ts b/app/renderer/js/tray.ts index aa0dd676..1a8508d5 100644 --- a/app/renderer/js/tray.ts +++ b/app/renderer/js/tray.ts @@ -125,7 +125,7 @@ function sendAction(action: string): void { win.restore(); } - win.webContents.send(action); + ipcRenderer.sendTo(win.webContents.id, action); } const createTray = function (): void { @@ -219,8 +219,7 @@ function toggleTray(): void { const selector = "webview:not([class*=disabled])"; const webview: WebviewTag = document.querySelector(selector)!; - const webContents = remote.webContents.fromId(webview.getWebContentsId()); - webContents.send("toggletray", state); + ipcRenderer.sendTo(webview.getWebContentsId(), "toggletray", state); } ipcRenderer.on("toggletray", toggleTray);