Skip to content

Commit

Permalink
Use vscode: channels
Browse files Browse the repository at this point in the history
  • Loading branch information
thegecko committed Dec 24, 2024
1 parent f5f88c6 commit 81f370f
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 30 deletions.
9 changes: 0 additions & 9 deletions src/vs/base/parts/sandbox/electron-sandbox/electronTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
12 changes: 11 additions & 1 deletion src/vs/base/parts/sandbox/electron-sandbox/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -115,6 +115,15 @@ export interface ISandboxContext {
resolveConfiguration(): Promise<ISandboxConfiguration>;
}

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;
Expand All @@ -135,6 +144,7 @@ export interface IMainWindowSandboxGlobals {
readonly process: ISandboxNodeProcess;
readonly context: ISandboxContext;
readonly webUtils: WebUtils;
readonly deviceAccess: IDeviceAccess;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/vs/base/parts/sandbox/electron-sandbox/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
20 changes: 11 additions & 9 deletions src/vs/platform/window/electron-sandbox/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<string | undefined>) {
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<string | undefined>) {
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);
});
}
}
}

Expand Down
20 changes: 11 additions & 9 deletions src/vs/platform/windows/electron-main/windowImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<object> | undefined;
Expand Down

0 comments on commit 81f370f

Please sign in to comment.