Skip to content

Commit fe94804

Browse files
author
Benjamin Pasero
committed
bootstrap - use preload script to access Electron IPC
1 parent c7fe3b4 commit fe94804

2 files changed

Lines changed: 26 additions & 13 deletions

File tree

src/bootstrap-window.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
}
2323
}(this, function () {
2424
const path = require.__$__nodeRequire('path');
25-
const ipc = require.__$__nodeRequire('electron').ipcRenderer;
2625
const bootstrap = globalThis.MonacoBootstrap;
2726

2827
/**
@@ -151,6 +150,8 @@
151150
* @returns {() => void}
152151
*/
153152
function registerDeveloperKeybindings(disallowReloadKeybinding) {
153+
const ipcRenderer = globals().ipcRenderer;
154+
154155
const extractKey = function (e) {
155156
return [
156157
e.ctrlKey ? 'ctrl-' : '',
@@ -169,9 +170,9 @@
169170
let listener = function (e) {
170171
const key = extractKey(e);
171172
if (key === TOGGLE_DEV_TOOLS_KB || key === TOGGLE_DEV_TOOLS_KB_ALT) {
172-
ipc.send('vscode:toggleDevTools');
173+
ipcRenderer.send('vscode:toggleDevTools');
173174
} else if (key === RELOAD_KB && !disallowReloadKeybinding) {
174-
ipc.send('vscode:reloadWindow');
175+
ipcRenderer.send('vscode:reloadWindow');
175176
}
176177
};
177178

@@ -191,7 +192,8 @@
191192
*/
192193
function onUnexpectedError(error, enableDeveloperTools) {
193194
if (enableDeveloperTools) {
194-
ipc.send('vscode:openDevTools');
195+
const ipcRenderer = globals().ipcRenderer;
196+
ipcRenderer.send('vscode:openDevTools');
195197
}
196198

197199
console.error(`[uncaught exception]: ${error}`);
@@ -201,7 +203,16 @@
201203
}
202204
}
203205

206+
/**
207+
* @return {typeof import('./vs/base/parts/sandbox/electron-sandbox/globals')}
208+
*/
209+
function globals() {
210+
// @ts-ignore (defined in globals.js)
211+
return window.vscode;
212+
}
213+
204214
return {
205-
load
215+
load,
216+
globals
206217
};
207218
}));

src/vs/code/electron-browser/workbench/workbench.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,20 @@ const perf = (function () {
3333

3434
perf.mark('renderer/started');
3535

36-
// Setup shell environment
37-
process['lazyEnv'] = getLazyEnv();
38-
3936
/**
40-
* @type {{ load: (modules: string[], resultCallback: (result, configuration: object) => any, options: object) => unknown }}
37+
* @type {{
38+
* load: (modules: string[], resultCallback: (result, configuration: object) => any, options: object) => unknown,
39+
* globals: () => typeof import('../../../base/parts/sandbox/electron-sandbox/globals')
40+
* }}
4141
*/
4242
const bootstrapWindow = (() => {
4343
// @ts-ignore (defined in bootstrap-window.js)
4444
return window.MonacoBootstrapWindow;
4545
})();
4646

47+
// Setup shell environment
48+
process['lazyEnv'] = getLazyEnv();
49+
4750
// Load workbench main JS, CSS and NLS all in parallel. This is an
4851
// optimization to prevent a waterfall of loading to happen, because
4952
// we know for a fact that workbench.desktop.main will depend on
@@ -184,22 +187,21 @@ function showPartsSplash(configuration) {
184187
* @returns {Promise<void>}
185188
*/
186189
function getLazyEnv() {
187-
188-
const ipc = require.__$__nodeRequire('electron').ipcRenderer;
190+
const ipcRenderer = bootstrapWindow.globals().ipcRenderer;
189191

190192
return new Promise(function (resolve) {
191193
const handle = setTimeout(function () {
192194
resolve();
193195
console.warn('renderer did not receive lazyEnv in time');
194196
}, 10000);
195197

196-
ipc.once('vscode:acceptShellEnv', function (event, shellEnv) {
198+
ipcRenderer.once('vscode:acceptShellEnv', function (event, shellEnv) {
197199
clearTimeout(handle);
198200
Object.assign(process.env, shellEnv);
199201
// @ts-ignore
200202
resolve(process.env);
201203
});
202204

203-
ipc.send('vscode:fetchShellEnv');
205+
ipcRenderer.send('vscode:fetchShellEnv');
204206
});
205207
}

0 commit comments

Comments
 (0)