From 81f370f0e7f85dd1180a0b6928c1039e6331b366 Mon Sep 17 00:00:00 2001 From: robmor01 Date: Tue, 24 Dec 2024 16:05:15 +0000 Subject: [PATCH] Use vscode: channels --- .../sandbox/electron-sandbox/electronTypes.ts | 9 --------- .../parts/sandbox/electron-sandbox/globals.ts | 12 ++++++++++- .../parts/sandbox/electron-sandbox/preload.ts | 4 ++-- .../window/electron-sandbox/window.ts | 20 ++++++++++--------- .../windows/electron-main/windowImpl.ts | 20 ++++++++++--------- 5 files changed, 35 insertions(+), 30 deletions(-) diff --git a/src/vs/base/parts/sandbox/electron-sandbox/electronTypes.ts b/src/vs/base/parts/sandbox/electron-sandbox/electronTypes.ts index 4291ec20bdc7c..a132d7d6eb41d 100644 --- a/src/vs/base/parts/sandbox/electron-sandbox/electronTypes.ts +++ b/src/vs/base/parts/sandbox/electron-sandbox/electronTypes.ts @@ -188,12 +188,3 @@ export interface WebUtils { */ getPathForFile(file: File): string; } - -export interface IDevice { - id: string; - label: string; -} - -export interface IDeviceAccess { - handleDeviceAccess: (callback: (event: IpcRendererEvent, type: string, devices: IDevice[]) => void) => void; -} diff --git a/src/vs/base/parts/sandbox/electron-sandbox/globals.ts b/src/vs/base/parts/sandbox/electron-sandbox/globals.ts index a19b59f71add5..b0a07f70a1424 100644 --- a/src/vs/base/parts/sandbox/electron-sandbox/globals.ts +++ b/src/vs/base/parts/sandbox/electron-sandbox/globals.ts @@ -5,7 +5,7 @@ import { INodeProcess, IProcessEnvironment } from '../../../common/platform.js'; import { ISandboxConfiguration } from '../common/sandboxTypes.js'; -import { IpcRenderer, ProcessMemoryInfo, WebFrame, WebUtils, IDeviceAccess } from './electronTypes.js'; +import { IpcRenderer, ProcessMemoryInfo, WebFrame, WebUtils, IpcRendererEvent } from './electronTypes.js'; /** * In Electron renderers we cannot expose all of the `process` global of node.js @@ -115,6 +115,15 @@ export interface ISandboxContext { resolveConfiguration(): Promise; } +export interface IDevice { + id: string; + label: string; +} + +export interface IDeviceAccess { + handleDeviceAccess: (callback: (event: IpcRendererEvent, devices: IDevice[]) => void) => void; +} + const vscodeGlobal = (globalThis as any).vscode; export const ipcRenderer: IpcRenderer = vscodeGlobal.ipcRenderer; export const ipcMessagePort: IpcMessagePort = vscodeGlobal.ipcMessagePort; @@ -135,6 +144,7 @@ export interface IMainWindowSandboxGlobals { readonly process: ISandboxNodeProcess; readonly context: ISandboxContext; readonly webUtils: WebUtils; + readonly deviceAccess: IDeviceAccess; } /** diff --git a/src/vs/base/parts/sandbox/electron-sandbox/preload.ts b/src/vs/base/parts/sandbox/electron-sandbox/preload.ts index 7bcfbec882136..ea35285fcf0b3 100644 --- a/src/vs/base/parts/sandbox/electron-sandbox/preload.ts +++ b/src/vs/base/parts/sandbox/electron-sandbox/preload.ts @@ -10,7 +10,7 @@ const { ipcRenderer, webFrame, contextBridge, webUtils } = require('electron'); type ISandboxConfiguration = import('vs/base/parts/sandbox/common/sandboxTypes.js').ISandboxConfiguration; - type IDeviceAccess = import('vs/base/parts/sandbox/electron-sandbox/electronTypes.js').IDeviceAccess; + type IDeviceAccess = import('vs/base/parts/sandbox/electron-sandbox/globals.js').IDeviceAccess; //#region Utilities @@ -94,7 +94,7 @@ //#region Device Access const deviceAccess: IDeviceAccess = { - handleDeviceAccess: callback => ipcRenderer.on('device-access', callback), + handleDeviceAccess: callback => ipcRenderer.on('vscode:device-access', callback), }; //#endregion diff --git a/src/vs/platform/window/electron-sandbox/window.ts b/src/vs/platform/window/electron-sandbox/window.ts index 362409bee8142..6c4171bbf4043 100644 --- a/src/vs/platform/window/electron-sandbox/window.ts +++ b/src/vs/platform/window/electron-sandbox/window.ts @@ -7,9 +7,9 @@ import { getZoomLevel, setZoomFactor, setZoomLevel } from '../../../base/browser import { getActiveWindow, getWindows } from '../../../base/browser/dom.js'; import { mainWindow } from '../../../base/browser/window.js'; import { ISandboxConfiguration } from '../../../base/parts/sandbox/common/sandboxTypes.js'; -import { ISandboxGlobals, ipcRenderer, webFrame, deviceAccess } from '../../../base/parts/sandbox/electron-sandbox/globals.js'; +import { ISandboxGlobals, ipcRenderer, webFrame, IDevice, deviceAccess } from '../../../base/parts/sandbox/electron-sandbox/globals.js'; import { zoomLevelToZoomFactor } from '../common/window.js'; -import { IpcRendererEvent, IDevice } from '../../../base/parts/sandbox/electron-sandbox/electronTypes.js'; +import { IpcRendererEvent } from '../../../base/parts/sandbox/electron-sandbox/electronTypes.js'; export enum ApplyZoomTarget { ACTIVE_WINDOW = 1, @@ -65,14 +65,16 @@ export function zoomOut(target: ApplyZoomTarget | Window): void { applyZoom(getZoomLevel(typeof target === 'number' ? getActiveWindow() : target) - 1, target); } -export function registerDeviceAccessHandler(handler: (devices: IDevice[], type: string) => Promise) { - const asyncHandler = async (event: IpcRendererEvent, type: string, devices: IDevice[]) => { - const id = await handler(devices, type); - event.sender.send(type, id); - }; - +export function registerDeviceAccessHandler(handler: (devices: IDevice[]) => Promise) { for (const { window } of getWindows()) { - getGlobals(window)?.deviceAccess.handleDeviceAccess(asyncHandler); + const globals = getGlobals(window); + + if (globals) { + globals.deviceAccess.handleDeviceAccess(async (_event: IpcRendererEvent, devices: IDevice[]) => { + const id = await handler(devices); + globals.ipcRenderer.send('vscode:device-access', id); + }); + } } } diff --git a/src/vs/platform/windows/electron-main/windowImpl.ts b/src/vs/platform/windows/electron-main/windowImpl.ts index 43eead4882561..aa2f7fde3ac99 100644 --- a/src/vs/platform/windows/electron-main/windowImpl.ts +++ b/src/vs/platform/windows/electron-main/windowImpl.ts @@ -714,36 +714,38 @@ export class CodeWindow extends BaseWindow implements ICodeWindow { this._win.webContents.session.on('select-usb-device', (event, details, callback) => { event.preventDefault(); - const type = 'select-usb-device'; const items = details.deviceList.map(device => ({ id: device.deviceId, label: device.productName || device.serialNumber || `${device.vendorId}:${device.productId}` })); - Electron.ipcMain.once(type, (_event, value) => callback(value)); - this._win.webContents.send('device-access', type, items); + handleDeviceSelect(items, callback); }); this._win.webContents.session.on('select-hid-device', (event, details, callback) => { event.preventDefault(); - const type = 'select-hid-device'; const items = details.deviceList.map(device => ({ id: device.deviceId, label: device.name })); - Electron.ipcMain.once(type, (_event, value) => callback(value)); - this._win.webContents.send('device-access', type, items); + handleDeviceSelect(items, callback); }); this._win.webContents.session.on('select-serial-port', (event, portList, _webContents, callback) => { event.preventDefault(); - const type = 'select-serial-port'; const items = portList.map(device => ({ id: device.portId, label: device.displayName || device.portName })); - Electron.ipcMain.once(type, (_event, value) => callback(value)); - this._win.webContents.send('device-access', type, items); + handleDeviceSelect(items, callback); }); + + const handleDeviceSelect = (items: { id: string; label: string }[], callback: (id: string) => void) => { + // Listen to callback from renderer + electron.ipcMain.once('vscode:device-access', (_event, value) => callback(value)); + + // Send details of list to be picked from + this.send('vscode:device-access', items); + }; } private marketplaceHeadersPromise: Promise | undefined;