提交 76b73311 编写于 作者: B Benjamin Pasero

sandbox - make platform.ts fit for sandbox usages

上级 f65e2fbd
...@@ -26,15 +26,16 @@ export interface IProcessEnvironment { ...@@ -26,15 +26,16 @@ export interface IProcessEnvironment {
[key: string]: string; [key: string]: string;
} }
interface INodeProcess { export interface INodeProcess {
platform: string; platform: 'win32' | 'linux' | 'darwin';
env: IProcessEnvironment; env: IProcessEnvironment;
getuid(): number;
nextTick: Function; nextTick: Function;
versions?: { versions?: {
electron?: string; electron?: string;
}; };
type?: string; type?: string;
getuid(): number;
cwd(): string;
} }
declare const process: INodeProcess; declare const process: INodeProcess;
declare const global: any; declare const global: any;
...@@ -47,9 +48,20 @@ interface INavigator { ...@@ -47,9 +48,20 @@ interface INavigator {
declare const navigator: INavigator; declare const navigator: INavigator;
declare const self: any; declare const self: any;
const isElectronRenderer = (typeof process !== 'undefined' && typeof process.versions !== 'undefined' && typeof process.versions.electron !== 'undefined' && process.type === 'renderer'); const _globals = (typeof self === 'object' ? self : typeof global === 'object' ? global : {} as any);
let nodeProcess: INodeProcess | undefined = undefined;
if (typeof process !== 'undefined') {
// Native environment (non-sandboxed)
nodeProcess = process;
} else if (typeof _globals.vscode !== 'undefined') {
// Native envionment (sandboxed)
nodeProcess = _globals.vscode.process;
}
const isElectronRenderer = typeof nodeProcess?.versions?.electron === 'string' && nodeProcess.type === 'renderer';
// OS detection // Web environment
if (typeof navigator === 'object' && !isElectronRenderer) { if (typeof navigator === 'object' && !isElectronRenderer) {
_userAgent = navigator.userAgent; _userAgent = navigator.userAgent;
_isWindows = _userAgent.indexOf('Windows') >= 0; _isWindows = _userAgent.indexOf('Windows') >= 0;
...@@ -59,13 +71,16 @@ if (typeof navigator === 'object' && !isElectronRenderer) { ...@@ -59,13 +71,16 @@ if (typeof navigator === 'object' && !isElectronRenderer) {
_isWeb = true; _isWeb = true;
_locale = navigator.language; _locale = navigator.language;
_language = _locale; _language = _locale;
} else if (typeof process === 'object') { }
_isWindows = (process.platform === 'win32');
_isMacintosh = (process.platform === 'darwin'); // Native environment
_isLinux = (process.platform === 'linux'); else if (typeof nodeProcess === 'object') {
_isWindows = (nodeProcess.platform === 'win32');
_isMacintosh = (nodeProcess.platform === 'darwin');
_isLinux = (nodeProcess.platform === 'linux');
_locale = LANGUAGE_DEFAULT; _locale = LANGUAGE_DEFAULT;
_language = LANGUAGE_DEFAULT; _language = LANGUAGE_DEFAULT;
const rawNlsConfig = process.env['VSCODE_NLS_CONFIG']; const rawNlsConfig = nodeProcess.env['VSCODE_NLS_CONFIG'];
if (rawNlsConfig) { if (rawNlsConfig) {
try { try {
const nlsConfig: NLSConfig = JSON.parse(rawNlsConfig); const nlsConfig: NLSConfig = JSON.parse(rawNlsConfig);
...@@ -80,6 +95,11 @@ if (typeof navigator === 'object' && !isElectronRenderer) { ...@@ -80,6 +95,11 @@ if (typeof navigator === 'object' && !isElectronRenderer) {
_isNative = true; _isNative = true;
} }
// Unknown environment
else {
console.error('Unable to resolve platform.');
}
export const enum Platform { export const enum Platform {
Web, Web,
Mac, Mac,
...@@ -114,7 +134,7 @@ export const platform = _platform; ...@@ -114,7 +134,7 @@ export const platform = _platform;
export const userAgent = _userAgent; export const userAgent = _userAgent;
export function isRootUser(): boolean { export function isRootUser(): boolean {
return _isNative && !_isWindows && (process.getuid() === 0); return !!nodeProcess && !_isWindows && (nodeProcess.getuid() === 0);
} }
/** /**
...@@ -157,7 +177,6 @@ export const locale = _locale; ...@@ -157,7 +177,6 @@ export const locale = _locale;
*/ */
export const translationsConfigFile = _translationsConfigFile; export const translationsConfigFile = _translationsConfigFile;
const _globals = (typeof self === 'object' ? self : typeof global === 'object' ? global : {} as any);
export const globals: any = _globals; export const globals: any = _globals;
interface ISetImmediate { interface ISetImmediate {
...@@ -196,8 +215,8 @@ export const setImmediate: ISetImmediate = (function defineSetImmediate() { ...@@ -196,8 +215,8 @@ export const setImmediate: ISetImmediate = (function defineSetImmediate() {
globals.postMessage({ vscodeSetImmediateId: myId }, '*'); globals.postMessage({ vscodeSetImmediateId: myId }, '*');
}; };
} }
if (typeof process !== 'undefined' && typeof process.nextTick === 'function') { if (nodeProcess) {
return process.nextTick.bind(process); return nodeProcess.nextTick.bind(nodeProcess);
} }
const _promise = Promise.resolve(); const _promise = Promise.resolve();
return (callback: (...args: any[]) => void) => _promise.then(callback); return (callback: (...args: any[]) => void) => _promise.then(callback);
......
...@@ -3,19 +3,11 @@ ...@@ -3,19 +3,11 @@
* Licensed under the MIT License. See License.txt in the project root for license information. * Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/ *--------------------------------------------------------------------------------------------*/
import { isWindows, isMacintosh, setImmediate, IProcessEnvironment, globals } from 'vs/base/common/platform'; import { isWindows, isMacintosh, setImmediate, globals, INodeProcess } from 'vs/base/common/platform';
export interface IProcess { declare const process: INodeProcess;
platform: 'win32' | 'linux' | 'darwin';
env: IProcessEnvironment;
cwd(): string; let safeProcess: INodeProcess;
nextTick(callback: (...args: any[]) => void): void;
}
declare const process: IProcess;
let safeProcess: IProcess;
// Native node.js environment // Native node.js environment
if (typeof process !== 'undefined') { if (typeof process !== 'undefined') {
...@@ -30,10 +22,15 @@ else if (typeof globals.vscode !== 'undefined') { ...@@ -30,10 +22,15 @@ else if (typeof globals.vscode !== 'undefined') {
// Web environment // Web environment
else { else {
safeProcess = { safeProcess = {
cwd(): string { return '/'; },
env: Object.create(null), // Supported
get platform(): 'win32' | 'linux' | 'darwin' { return isWindows ? 'win32' : isMacintosh ? 'darwin' : 'linux'; }, get platform(): 'win32' | 'linux' | 'darwin' { return isWindows ? 'win32' : isMacintosh ? 'darwin' : 'linux'; },
nextTick(callback: (...args: any[]) => void): void { return setImmediate(callback); } nextTick(callback: (...args: any[]) => void): void { return setImmediate(callback); },
// Unsupported
get env() { return Object.create(null); },
cwd(): string { return '/'; },
getuid(): number { return -1; }
}; };
} }
......
...@@ -98,9 +98,10 @@ ...@@ -98,9 +98,10 @@
* Support for a subset of access to node.js global `process`. * Support for a subset of access to node.js global `process`.
*/ */
process: { process: {
platform: process.platform, get platform() { return process.platform; },
env: process.env, get env() { return process.env; },
versions: process.versions, get versions() { return process.versions; },
get type() { return 'renderer'; },
_whenEnvResolved: undefined, _whenEnvResolved: undefined,
whenEnvResolved: whenEnvResolved:
...@@ -136,6 +137,14 @@ ...@@ -136,6 +137,14 @@
return process.cwd(); return process.cwd();
}, },
getuid:
/**
* @returns the numeric user identity of the process
*/
function () {
return process.getuid();
},
getProcessMemoryInfo: getProcessMemoryInfo:
/** /**
* @returns {Promise<import('electron').ProcessMemoryInfo>} * @returns {Promise<import('electron').ProcessMemoryInfo>}
...@@ -160,7 +169,7 @@ ...@@ -160,7 +169,7 @@
* Some information about the context we are running in. * Some information about the context we are running in.
*/ */
context: { context: {
sandbox: process.argv.includes('--enable-sandbox') get sandbox() { return process.argv.includes('--enable-sandbox'); }
} }
}; };
......
...@@ -4,8 +4,7 @@ ...@@ -4,8 +4,7 @@
*--------------------------------------------------------------------------------------------*/ *--------------------------------------------------------------------------------------------*/
import type { ProcessMemoryInfo } from 'vs/base/parts/sandbox/common/electronTypes'; import type { ProcessMemoryInfo } from 'vs/base/parts/sandbox/common/electronTypes';
import type { IProcess } from 'vs/base/common/process'; import { globals, INodeProcess } from 'vs/base/common/platform';
import { globals } from 'vs/base/common/platform';
export const ipcRenderer = globals.vscode.ipcRenderer as { export const ipcRenderer = globals.vscode.ipcRenderer as {
...@@ -75,7 +74,7 @@ export const crashReporter = globals.vscode.crashReporter as { ...@@ -75,7 +74,7 @@ export const crashReporter = globals.vscode.crashReporter as {
addExtraParameter(key: string, value: string): void; addExtraParameter(key: string, value: string): void;
}; };
export const process = globals.vscode.process as IProcess & { export const process = globals.vscode.process as INodeProcess & {
/** /**
* The process.platform property returns a string identifying the operating system platform * The process.platform property returns a string identifying the operating system platform
...@@ -84,7 +83,17 @@ export const process = globals.vscode.process as IProcess & { ...@@ -84,7 +83,17 @@ export const process = globals.vscode.process as IProcess & {
platform: 'win32' | 'linux' | 'darwin'; platform: 'win32' | 'linux' | 'darwin';
/** /**
* The process.env property returns an object containing the user environment. See environ(7). * The type will always be Electron renderer.
*/
type: 'renderer';
/**
* A list of versions for the current node.js/electron configuration.
*/
versions: { [key: string]: string | undefined };
/**
* The process.env property returns an object containing the user environment.
*/ */
env: { [key: string]: string | undefined }; env: { [key: string]: string | undefined };
...@@ -93,6 +102,11 @@ export const process = globals.vscode.process as IProcess & { ...@@ -93,6 +102,11 @@ export const process = globals.vscode.process as IProcess & {
*/ */
cwd(): string; cwd(): string;
/**
* Returns the numeric user identity of the process.
*/
getuid(): number;
/** /**
* Allows to await resolving the full process environment by checking for the shell environment * Allows to await resolving the full process environment by checking for the shell environment
* of the OS in certain cases (e.g. when the app is started from the Dock on macOS). * of the OS in certain cases (e.g. when the app is started from the Dock on macOS).
...@@ -125,11 +139,6 @@ export const process = globals.vscode.process as IProcess & { ...@@ -125,11 +139,6 @@ export const process = globals.vscode.process as IProcess & {
* process on macOS. * process on macOS.
*/ */
getProcessMemoryInfo: () => Promise<ProcessMemoryInfo>; getProcessMemoryInfo: () => Promise<ProcessMemoryInfo>;
/**
* A list of versions for the current node.js/electron configuration.
*/
versions: { [key: string]: string | undefined };
}; };
export const context = globals.vscode.context as { export const context = globals.vscode.context as {
......
...@@ -91,6 +91,7 @@ export class SimpleWorkbenchEnvironmentService implements INativeWorkbenchEnviro ...@@ -91,6 +91,7 @@ export class SimpleWorkbenchEnvironmentService implements INativeWorkbenchEnviro
get userDataSyncHome(): URI { return joinPath(this.userRoamingDataHome, 'syncHome'); } get userDataSyncHome(): URI { return joinPath(this.userRoamingDataHome, 'syncHome'); }
get tmpDir(): URI { return joinPath(this.userRoamingDataHome, 'tmp'); } get tmpDir(): URI { return joinPath(this.userRoamingDataHome, 'tmp'); }
get backupWorkspaceHome(): URI { return joinPath(this.userRoamingDataHome, 'Backups', 'workspace'); } get backupWorkspaceHome(): URI { return joinPath(this.userRoamingDataHome, 'Backups', 'workspace'); }
get logsPath(): string { return joinPath(this.userRoamingDataHome, 'logs').path; }
options?: IWorkbenchConstructionOptions | undefined; options?: IWorkbenchConstructionOptions | undefined;
logExtensionHostCommunication?: boolean | undefined; logExtensionHostCommunication?: boolean | undefined;
...@@ -107,7 +108,6 @@ export class SimpleWorkbenchEnvironmentService implements INativeWorkbenchEnviro ...@@ -107,7 +108,6 @@ export class SimpleWorkbenchEnvironmentService implements INativeWorkbenchEnviro
disableExtensions: boolean | string[] = []; disableExtensions: boolean | string[] = [];
extensionDevelopmentLocationURI?: URI[] | undefined; extensionDevelopmentLocationURI?: URI[] | undefined;
extensionTestsLocationURI?: URI | undefined; extensionTestsLocationURI?: URI | undefined;
logsPath: string = undefined!;
logLevel?: string | undefined; logLevel?: string | undefined;
args: NativeParsedArgs = Object.create(null); args: NativeParsedArgs = Object.create(null);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册