Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/base/parts/sandbox/electron-browser/globals.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { INodeProcess, IProcessEnvironment } from '../../../common/platform.js';
7
import { ISandboxConfiguration } from '../common/sandboxTypes.js';
8
import { IpcRenderer, ProcessMemoryInfo, WebFrame, WebUtils } from './electronTypes.js';
9
10
/**
11
* In Electron renderers we cannot expose all of the `process` global of node.js
12
*/
13
export interface ISandboxNodeProcess extends INodeProcess {
14
15
/**
16
* The process.platform property returns a string identifying the operating system platform
17
* on which the Node.js process is running.
18
*/
19
readonly platform: string;
20
21
/**
22
* The process.arch property returns a string identifying the CPU architecture
23
* on which the Node.js process is running.
24
*/
25
readonly arch: string;
26
27
/**
28
* The type will always be `renderer`.
29
*/
30
readonly type: string;
31
32
/**
33
* A list of versions for the current node.js/electron configuration.
34
*/
35
readonly versions: { [key: string]: string | undefined };
36
37
/**
38
* The process.env property returns an object containing the user environment.
39
*/
40
readonly env: IProcessEnvironment;
41
42
/**
43
* The `execPath` will be the location of the executable of this application.
44
*/
45
readonly execPath: string;
46
47
/**
48
* A listener on the process. Only a small subset of listener types are allowed.
49
*/
50
on: (type: string, callback: Function) => void;
51
52
/**
53
* The current working directory of the process.
54
*/
55
cwd: () => string;
56
57
/**
58
* Resolves with a ProcessMemoryInfo
59
*
60
* Returns an object giving memory usage statistics about the current process. Note
61
* that all statistics are reported in Kilobytes. This api should be called after
62
* app ready.
63
*
64
* Chromium does not provide `residentSet` value for macOS. This is because macOS
65
* performs in-memory compression of pages that haven't been recently used. As a
66
* result the resident set size value is not what one would expect. `private`
67
* memory is more representative of the actual pre-compression memory usage of the
68
* process on macOS.
69
*/
70
getProcessMemoryInfo: () => Promise<ProcessMemoryInfo>;
71
72
/**
73
* Returns a process environment that includes all shell environment variables even if
74
* the application was not started from a shell / terminal / console.
75
*
76
* There are different layers of environment that will apply:
77
* - `process.env`: this is the actual environment of the process before this method
78
* - `shellEnv` : if the program was not started from a terminal, we resolve all shell
79
* variables to get the same experience as if the program was started from
80
* a terminal (Linux, macOS)
81
* - `userEnv` : this is instance specific environment, e.g. if the user started the program
82
* from a terminal and changed certain variables
83
*
84
* The order of overwrites is `process.env` < `shellEnv` < `userEnv`.
85
*/
86
shellEnv(): Promise<IProcessEnvironment>;
87
}
88
89
export interface IpcMessagePort {
90
91
/**
92
* Acquire a `MessagePort`. The main process will transfer the port over to
93
* the `responseChannel` with a payload of `requestNonce` so that the source can
94
* correlate the response.
95
*
96
* The source should install a `window.on('message')` listener, ensuring `e.data`
97
* matches `nonce`, `e.source` matches `window` and then receiving the `MessagePort`
98
* via `e.ports[0]`.
99
*/
100
acquire(responseChannel: string, nonce: string): void;
101
}
102
103
export interface ISandboxContext {
104
105
/**
106
* A configuration object made accessible from the main side
107
* to configure the sandbox browser window. Will be `undefined`
108
* for as long as `resolveConfiguration` is not awaited.
109
*/
110
configuration(): ISandboxConfiguration | undefined;
111
112
/**
113
* Allows to await the resolution of the configuration object.
114
*/
115
resolveConfiguration(): Promise<ISandboxConfiguration>;
116
}
117
118
const vscodeGlobal = (globalThis as any).vscode;
119
export const ipcRenderer: IpcRenderer = vscodeGlobal.ipcRenderer;
120
export const ipcMessagePort: IpcMessagePort = vscodeGlobal.ipcMessagePort;
121
export const webFrame: WebFrame = vscodeGlobal.webFrame;
122
export const process: ISandboxNodeProcess = vscodeGlobal.process;
123
export const context: ISandboxContext = vscodeGlobal.context;
124
export const webUtils: WebUtils = vscodeGlobal.webUtils;
125
126
/**
127
* A set of globals only available to main windows that depend
128
* on `preload.js`.
129
*/
130
export interface IMainWindowSandboxGlobals {
131
readonly ipcRenderer: IpcRenderer;
132
readonly ipcMessagePort: IpcMessagePort;
133
readonly webFrame: WebFrame;
134
readonly process: ISandboxNodeProcess;
135
readonly context: ISandboxContext;
136
readonly webUtils: WebUtils;
137
}
138
139
/**
140
* A set of globals that are available in all windows that either
141
* depend on `preload.js` or `preload-aux.js`.
142
*/
143
export interface ISandboxGlobals {
144
readonly ipcRenderer: Pick<import('./electronTypes.js').IpcRenderer, 'send' | 'invoke'>;
145
readonly webFrame: import('./electronTypes.js').WebFrame;
146
}
147
148