From 634c5c66a651831690faf1733528140e689b85de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Brandner?= Date: Fri, 11 Feb 2022 11:38:19 +0100 Subject: [PATCH] Switch to IPC channel for `getDesktopCapturerSources` (#320) --- src/electron-main.ts | 19 ++++++++++++++++++- src/preload.ts | 22 +--------------------- 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/src/electron-main.ts b/src/electron-main.ts index ceefb9e..8d290c9 100644 --- a/src/electron-main.ts +++ b/src/electron-main.ts @@ -19,7 +19,17 @@ limitations under the License. // Squirrel on windows starts the app with various flags as hooks to tell us when we've been installed/uninstalled etc. import "./squirrelhooks"; -import { app, ipcMain, powerSaveBlocker, BrowserWindow, Menu, autoUpdater, protocol, dialog } from "electron"; +import { + app, + ipcMain, + powerSaveBlocker, + BrowserWindow, + Menu, + autoUpdater, + protocol, + dialog, + desktopCapturer, +} from "electron"; import AutoLaunch from "auto-launch"; import path from "path"; import windowStateKeeper from 'electron-window-state'; @@ -502,6 +512,13 @@ ipcMain.on('ipcCall', async function(ev, payload) { await keytar.deletePassword("riot.im", `${args[0]}|${args[1]}`); } catch (e) {} break; + case 'getDesktopCapturerSources': + ret = (await desktopCapturer.getSources(args[0])).map((source) => ({ + id: source.id, + name: source.name, + thumbnailURL: source.thumbnail.toDataURL(), + })); + break; default: mainWindow.webContents.send('ipcReply', { diff --git a/src/preload.ts b/src/preload.ts index 2325cfb..a2eebb4 100644 --- a/src/preload.ts +++ b/src/preload.ts @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -import { ipcRenderer, desktopCapturer, contextBridge, IpcRendererEvent, SourcesOptions } from 'electron'; +import { ipcRenderer, contextBridge, IpcRendererEvent } from 'electron'; // Expose only expected IPC wrapper APIs to the renderer process to avoid // handing out generalised messaging access. @@ -36,12 +36,6 @@ const CHANNELS = [ "userDownloadAction", ]; -interface ISource { - id: string; - name: string; - thumbnailURL: string; -} - contextBridge.exposeInMainWorld( "electron", { @@ -59,19 +53,5 @@ contextBridge.exposeInMainWorld( } ipcRenderer.send(channel, ...args); }, - async getDesktopCapturerSources(options: SourcesOptions): Promise { - const sources = await desktopCapturer.getSources(options); - const desktopCapturerSources: ISource[] = []; - - for (const source of sources) { - desktopCapturerSources.push({ - id: source.id, - name: source.name, - thumbnailURL: source.thumbnail.toDataURL(), - }); - } - - return desktopCapturerSources; - }, }, );