提交 eb7161d2 编写于 作者: J Joao Moreno

move ipc handles to environment service

上级 3fc422cd
...@@ -5,7 +5,6 @@ ...@@ -5,7 +5,6 @@
'use strict'; 'use strict';
import * as crypto from 'crypto';
import * as fs from 'original-fs'; import * as fs from 'original-fs';
import * as path from 'path'; import * as path from 'path';
import * as os from 'os'; import * as os from 'os';
...@@ -21,7 +20,6 @@ import * as types from 'vs/base/common/types'; ...@@ -21,7 +20,6 @@ import * as types from 'vs/base/common/types';
import { createDecorator } from 'vs/platform/instantiation/common/instantiation'; import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
import product, { IProductConfiguration } from 'vs/platform/product'; import product, { IProductConfiguration } from 'vs/platform/product';
import { parseArgs, ParsedArgs } from 'vs/code/node/argv'; import { parseArgs, ParsedArgs } from 'vs/code/node/argv';
import pkg from 'vs/platform/package';
export interface IProcessEnvironment { export interface IProcessEnvironment {
[key: string]: string; [key: string]: string;
...@@ -49,8 +47,6 @@ export interface IEnvService { ...@@ -49,8 +47,6 @@ export interface IEnvService {
appSettingsHome: string; appSettingsHome: string;
appSettingsPath: string; appSettingsPath: string;
appKeybindingsPath: string; appKeybindingsPath: string;
mainIPCHandle: string;
sharedIPCHandle: string;
createPaths(): TPromise<void>; createPaths(): TPromise<void>;
} }
...@@ -95,12 +91,6 @@ export class EnvService implements IEnvService { ...@@ -95,12 +91,6 @@ export class EnvService implements IEnvService {
private _appKeybindingsPath: string; private _appKeybindingsPath: string;
get appKeybindingsPath(): string { return this._appKeybindingsPath; } get appKeybindingsPath(): string { return this._appKeybindingsPath; }
private _mainIPCHandle: string;
get mainIPCHandle(): string { return this._mainIPCHandle; }
private _sharedIPCHandle: string;
get sharedIPCHandle(): string { return this._sharedIPCHandle; }
constructor() { constructor() {
this._appRoot = path.dirname(URI.parse(require.toUrl('')).fsPath); this._appRoot = path.dirname(URI.parse(require.toUrl('')).fsPath);
this._currentWorkingDirectory = process.env['VSCODE_CWD'] || process.cwd(); this._currentWorkingDirectory = process.env['VSCODE_CWD'] || process.cwd();
...@@ -147,45 +137,6 @@ export class EnvService implements IEnvService { ...@@ -147,45 +137,6 @@ export class EnvService implements IEnvService {
this._isTestingFromCli = this.cliArgs.extensionTestsPath && !this.cliArgs.debugBrkPluginHost; this._isTestingFromCli = this.cliArgs.extensionTestsPath && !this.cliArgs.debugBrkPluginHost;
this._userHome = path.join(os.homedir(), product.dataFolderName); this._userHome = path.join(os.homedir(), product.dataFolderName);
this._userExtensionsHome = this.cliArgs.extensionHomePath || path.join(this._userHome, 'extensions'); this._userExtensionsHome = this.cliArgs.extensionHomePath || path.join(this._userHome, 'extensions');
const prefix = this.getIPCHandleBaseName();
const suffix = process.platform === 'win32' ? '-sock' : '.sock';
this._mainIPCHandle = `${ prefix }-${ pkg.version }${ suffix }`;
this._sharedIPCHandle = `${ prefix }-${ pkg.version }-shared${ suffix }`;
}
private getIPCHandleBaseName(): string {
let name = pkg.name;
// Support to run VS Code multiple times as different user
// by making the socket unique over the logged in user
let userId = EnvService.getUniqueUserId();
if (userId) {
name += `-${ userId }`;
}
if (process.platform === 'win32') {
return `\\\\.\\pipe\\${ name }`;
}
return path.join(os.tmpdir(), name);
}
private static getUniqueUserId(): string {
let username: string;
if (platform.isWindows) {
username = process.env.USERNAME;
} else {
username = process.env.USER;
}
if (!username) {
return ''; // fail gracefully if there is no user name
}
// use sha256 to ensure the userid value can be used in filenames and are unique
return crypto.createHash('sha256').update(username).digest('hex').substr(0, 6);
} }
createPaths(): TPromise<void> { createPaths(): TPromise<void> {
......
...@@ -203,10 +203,11 @@ function main(accessor: ServicesAccessor, mainIpcServer: Server, userEnv: IProce ...@@ -203,10 +203,11 @@ function main(accessor: ServicesAccessor, mainIpcServer: Server, userEnv: IProce
function setupIPC(accessor: ServicesAccessor): TPromise<Server> { function setupIPC(accessor: ServicesAccessor): TPromise<Server> {
const logService = accessor.get(ILogService); const logService = accessor.get(ILogService);
const environmentService = accessor.get(IEnvironmentService);
const envService = accessor.get(IEnvService); const envService = accessor.get(IEnvService);
function setup(retry: boolean): TPromise<Server> { function setup(retry: boolean): TPromise<Server> {
return serve(envService.mainIPCHandle).then(server => { return serve(environmentService.mainIPCHandle).then(server => {
if (platform.isMacintosh) { if (platform.isMacintosh) {
app.dock.show(); // dock might be hidden at this case due to a retry app.dock.show(); // dock might be hidden at this case due to a retry
} }
...@@ -223,7 +224,7 @@ function setupIPC(accessor: ServicesAccessor): TPromise<Server> { ...@@ -223,7 +224,7 @@ function setupIPC(accessor: ServicesAccessor): TPromise<Server> {
} }
// there's a running instance, let's connect to it // there's a running instance, let's connect to it
return connect(envService.mainIPCHandle).then( return connect(environmentService.mainIPCHandle).then(
client => { client => {
// Tests from CLI require to be the only instance currently (TODO@Ben support multiple instances and output) // Tests from CLI require to be the only instance currently (TODO@Ben support multiple instances and output)
...@@ -252,7 +253,7 @@ function setupIPC(accessor: ServicesAccessor): TPromise<Server> { ...@@ -252,7 +253,7 @@ function setupIPC(accessor: ServicesAccessor): TPromise<Server> {
// let's delete it, since we can't connect to it // let's delete it, since we can't connect to it
// and the retry the whole thing // and the retry the whole thing
try { try {
fs.unlinkSync(envService.mainIPCHandle); fs.unlinkSync(environmentService.mainIPCHandle);
} catch (e) { } catch (e) {
logService.log('Fatal error deleting obsolete instance handle', e); logService.log('Fatal error deleting obsolete instance handle', e);
return TPromise.wrapError(e); return TPromise.wrapError(e);
...@@ -370,11 +371,12 @@ function getShellEnvironment(): TPromise<IEnv> { ...@@ -370,11 +371,12 @@ function getShellEnvironment(): TPromise<IEnv> {
function getEnvironment(): TPromise<IEnv> { function getEnvironment(): TPromise<IEnv> {
return getShellEnvironment().then(shellEnv => { return getShellEnvironment().then(shellEnv => {
return instantiationService.invokeFunction(a => { return instantiationService.invokeFunction(a => {
const envService = a.get(IEnvService); const environmentService = a.get(IEnvironmentService);
const instanceEnv = { const instanceEnv = {
VSCODE_PID: String(process.pid), VSCODE_PID: String(process.pid),
VSCODE_IPC_HOOK: envService.mainIPCHandle, VSCODE_IPC_HOOK: environmentService.mainIPCHandle,
VSCODE_SHARED_IPC_HOOK: envService.sharedIPCHandle, VSCODE_SHARED_IPC_HOOK: environmentService.sharedIPCHandle,
VSCODE_NLS_CONFIG: process.env['VSCODE_NLS_CONFIG'] VSCODE_NLS_CONFIG: process.env['VSCODE_NLS_CONFIG']
}; };
......
...@@ -32,4 +32,7 @@ export interface IEnvironmentService { ...@@ -32,4 +32,7 @@ export interface IEnvironmentService {
isBuilt: boolean; isBuilt: boolean;
verbose: boolean; verbose: boolean;
performance: boolean; performance: boolean;
mainIPCHandle: string;
sharedIPCHandle: string;
} }
\ No newline at end of file
...@@ -4,13 +4,15 @@ ...@@ -4,13 +4,15 @@
*--------------------------------------------------------------------------------------------*/ *--------------------------------------------------------------------------------------------*/
import {IEnvironmentService} from 'vs/platform/environment/common/environment'; import {IEnvironmentService} from 'vs/platform/environment/common/environment';
import * as crypto from 'crypto';
import * as paths from 'vs/base/node/paths'; import * as paths from 'vs/base/node/paths';
import product from 'vs/platform/product';
import * as os from 'os'; import * as os from 'os';
import * as path from 'path'; import * as path from 'path';
import {ParsedArgs} from 'vs/code/node/argv'; import {ParsedArgs} from 'vs/code/node/argv';
import URI from 'vs/base/common/uri'; import URI from 'vs/base/common/uri';
import { memoize } from 'vs/base/common/decorators'; import { memoize } from 'vs/base/common/decorators';
import pkg from 'vs/platform/package';
import product from 'vs/platform/product';
// TODO@Ben TODO@Joao this interface should be composed once the main => renderer // TODO@Ben TODO@Joao this interface should be composed once the main => renderer
// communication is also fit for that // communication is also fit for that
...@@ -18,6 +20,42 @@ export interface IEnvironment extends ParsedArgs { ...@@ -18,6 +20,42 @@ export interface IEnvironment extends ParsedArgs {
execPath: string; execPath: string;
} }
function getUniqueUserId(): string {
let username: string;
if (process.platform === 'win32') {
username = process.env.USERNAME;
} else {
username = process.env.USER;
}
if (!username) {
return ''; // fail gracefully if there is no user name
}
// use sha256 to ensure the userid value can be used in filenames and are unique
return crypto.createHash('sha256').update(username).digest('hex').substr(0, 6);
}
function getIPCHandleBaseName(): string {
let name = pkg.name;
// Support to run VS Code multiple times as different user
// by making the socket unique over the logged in user
let userId = getUniqueUserId();
if (userId) {
name += `-${ userId }`;
}
if (process.platform === 'win32') {
return `\\\\.\\pipe\\${ name }`;
}
return path.join(os.tmpdir(), name);
}
const IPCHandlePrefix = getIPCHandleBaseName();
const IPCHandleSuffix = process.platform === 'win32' ? '-sock' : '.sock';
export class EnvironmentService implements IEnvironmentService { export class EnvironmentService implements IEnvironmentService {
_serviceBrand: any; _serviceBrand: any;
...@@ -57,6 +95,12 @@ export class EnvironmentService implements IEnvironmentService { ...@@ -57,6 +95,12 @@ export class EnvironmentService implements IEnvironmentService {
get performance(): boolean { return this.args.performance; } get performance(): boolean { return this.args.performance; }
get logExtensionHostCommunication(): boolean { return this.args.logExtensionHostCommunication; } get logExtensionHostCommunication(): boolean { return this.args.logExtensionHostCommunication; }
@memoize
get mainIPCHandle(): string { return `${ IPCHandlePrefix }-${ pkg.version }${ IPCHandleSuffix }`; }
@memoize
get sharedIPCHandle(): string { return `${ IPCHandlePrefix }-${ pkg.version }-shared${ IPCHandleSuffix }`; }
constructor(private args: IEnvironment) {} constructor(private args: IEnvironment) {}
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册