mirror of
https://github.com/microsoft/vscode.git
synced 2026-05-03 15:01:57 +01:00
sandbox - expose limited IPC via preload script
This commit is contained in:
61
src/vs/code/electron-browser/workbench/preload.js
Normal file
61
src/vs/code/electron-browser/workbench/preload.js
Normal file
@@ -0,0 +1,61 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
// @ts-check
|
||||
(function () {
|
||||
'use strict';
|
||||
|
||||
const { ipcRenderer } = require('electron');
|
||||
|
||||
/**
|
||||
* @param {string} channel
|
||||
*/
|
||||
function validateIPC(channel) {
|
||||
if (!channel || !channel.startsWith('vscode:')) {
|
||||
throw new Error(`Unsupported event IPC channel '${channel}'`);
|
||||
}
|
||||
}
|
||||
|
||||
// @ts-ignore
|
||||
window.vscode = {
|
||||
|
||||
/**
|
||||
* A minimal set of methods exposed from ipcRenderer
|
||||
* to support communication to electron-main
|
||||
*/
|
||||
ipcRenderer: {
|
||||
|
||||
/**
|
||||
* @param {string} channel
|
||||
* @param {any[]} args
|
||||
*/
|
||||
send(channel, ...args) {
|
||||
validateIPC(channel);
|
||||
|
||||
ipcRenderer.send(channel, ...args);
|
||||
},
|
||||
|
||||
/**
|
||||
* @param {string} channel
|
||||
* @param {(event: import('electron').IpcRendererEvent, ...args: any[]) => void} listener
|
||||
*/
|
||||
on(channel, listener) {
|
||||
validateIPC(channel);
|
||||
|
||||
ipcRenderer.on(channel, listener);
|
||||
},
|
||||
|
||||
/**
|
||||
* @param {string} channel
|
||||
* @param {(event: import('electron').IpcRendererEvent, ...args: any[]) => void} listener
|
||||
*/
|
||||
removeListener(channel, listener) {
|
||||
validateIPC(channel);
|
||||
|
||||
ipcRenderer.removeListener(channel, listener);
|
||||
}
|
||||
}
|
||||
};
|
||||
}());
|
||||
Reference in New Issue
Block a user