sharedProcessMain.ts 9.2 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

B
Benjamin Pasero 已提交
6 7
'use strict';

E
Erich Gamma 已提交
8
import * as fs from 'fs';
9
import * as platform from 'vs/base/common/platform';
10 11
import product from 'vs/platform/node/product';
import pkg from 'vs/platform/node/package';
J
Joao Moreno 已提交
12
import { serve, Server, connect } from 'vs/base/parts/ipc/node/ipc.net';
E
Erich Gamma 已提交
13
import { TPromise } from 'vs/base/common/winjs.base';
J
Joao Moreno 已提交
14 15 16
import { ServiceCollection } from 'vs/platform/instantiation/common/serviceCollection';
import { SyncDescriptor } from 'vs/platform/instantiation/common/descriptors';
import { InstantiationService } from 'vs/platform/instantiation/common/instantiationService';
J
Joao Moreno 已提交
17 18
import { IEnvironmentService, ParsedArgs } from 'vs/platform/environment/common/environment';
import { EnvironmentService } from 'vs/platform/environment/node/environmentService';
J
Joao Moreno 已提交
19
import { ExtensionManagementChannel } from 'vs/platform/extensionManagement/common/extensionManagementIpc';
20
import { IExtensionManagementService, IExtensionGalleryService } from 'vs/platform/extensionManagement/common/extensionManagement';
J
Joao Moreno 已提交
21
import { ExtensionManagementService } from 'vs/platform/extensionManagement/node/extensionManagementService';
22
import { ExtensionGalleryService } from 'vs/platform/extensionManagement/node/extensionGalleryService';
J
Joao Moreno 已提交
23
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
24
import { ConfigurationService } from 'vs/platform/configuration/node/configurationService';
J
Joao Moreno 已提交
25
import { IRequestService } from 'vs/platform/request/node/request';
26
import { RequestService } from 'vs/platform/request/electron-browser/requestService';
27
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
28
import { combinedAppender, NullTelemetryService, ITelemetryAppender, NullAppender, LogAppender } from 'vs/platform/telemetry/common/telemetryUtils';
29
import { resolveCommonProperties } from 'vs/platform/telemetry/node/commonProperties';
30
import { TelemetryAppenderChannel } from 'vs/platform/telemetry/common/telemetryIpc';
31
import { TelemetryService, ITelemetryServiceConfig } from 'vs/platform/telemetry/common/telemetryService';
32
import { AppInsightsAppender } from 'vs/platform/telemetry/node/appInsightsAppender';
J
Joao Moreno 已提交
33
import { IWindowsService, ActiveWindowManager } from 'vs/platform/windows/common/windows';
J
Joao Moreno 已提交
34
import { WindowsChannelClient } from 'vs/platform/windows/common/windowsIpc';
35
import { ipcRenderer } from 'electron';
36
import { createSharedProcessContributions } from 'vs/code/electron-browser/sharedProcess/contrib/contributions';
37
import { createSpdLogService } from 'vs/platform/log/node/spdlogService';
S
Sandeep Somavarapu 已提交
38 39
import { ILogService, LogLevel } from 'vs/platform/log/common/log';
import { LogLevelSetterChannelClient, FollowerLogService } from 'vs/platform/log/common/logIpc';
S
Sandeep Somavarapu 已提交
40 41 42
import { LocalizationsService } from 'vs/platform/localizations/node/localizations';
import { ILocalizationsService } from 'vs/platform/localizations/common/localizations';
import { LocalizationsChannel } from 'vs/platform/localizations/common/localizationsIpc';
43 44
import { DialogChannelClient } from 'vs/platform/dialogs/common/dialogIpc';
import { IDialogService } from 'vs/platform/dialogs/common/dialogs';
S
Sandeep Somavarapu 已提交
45
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
46

47
export interface ISharedProcessConfiguration {
B
Benjamin Pasero 已提交
48
	readonly machineId: string;
49 50 51 52 53 54
}

export function startup(configuration: ISharedProcessConfiguration) {
	handshake(configuration);
}

55
interface ISharedProcessInitData {
56
	sharedIPCHandle: string;
J
Joao Moreno 已提交
57
	args: ParsedArgs;
S
Sandeep Somavarapu 已提交
58
	logLevel: LogLevel;
E
Erich Gamma 已提交
59 60
}

61 62
const eventPrefix = 'monacoworkbench';

63
function main(server: Server, initData: ISharedProcessInitData, configuration: ISharedProcessConfiguration): void {
J
Joao Moreno 已提交
64
	const services = new ServiceCollection();
S
Sandeep Somavarapu 已提交
65 66
	const disposables: IDisposable[] = [];
	process.once('exit', () => dispose(disposables));
E
Erich Gamma 已提交
67

68
	const environmentService = new EnvironmentService(initData.args, process.execPath);
J
Joao Moreno 已提交
69 70
	const mainRoute = () => TPromise.as('main');
	const logLevelClient = new LogLevelSetterChannelClient(server.getChannel('loglevel', { routeCall: mainRoute, routeEvent: mainRoute }));
S
Sandeep Somavarapu 已提交
71
	const logService = new FollowerLogService(logLevelClient, createSpdLogService('sharedprocess', initData.logLevel, environmentService.logsPath));
S
Sandeep Somavarapu 已提交
72
	disposables.push(logService);
J
Joao Moreno 已提交
73

74 75 76 77
	logService.info('main', JSON.stringify(configuration));

	services.set(IEnvironmentService, environmentService);
	services.set(ILogService, logService);
78
	services.set(IConfigurationService, new SyncDescriptor(ConfigurationService));
J
Joao Moreno 已提交
79
	services.set(IRequestService, new SyncDescriptor(RequestService));
J
Joao Moreno 已提交
80

J
Joao Moreno 已提交
81
	const windowsChannel = server.getChannel('windows', { routeCall: mainRoute, routeEvent: mainRoute });
J
Joao Moreno 已提交
82 83
	const windowsService = new WindowsChannelClient(windowsChannel);
	services.set(IWindowsService, windowsService);
S
Sandeep Somavarapu 已提交
84

J
Joao Moreno 已提交
85
	const activeWindowManager = new ActiveWindowManager(windowsService);
J
Joao Moreno 已提交
86 87
	const route = () => activeWindowManager.getActiveClientId();
	const dialogChannel = server.getChannel('dialog', { routeCall: route, routeEvent: route });
88
	services.set(IDialogService, new DialogChannelClient(dialogChannel));
S
Sandeep Somavarapu 已提交
89

J
Joao Moreno 已提交
90 91 92
	const instantiationService = new InstantiationService(services);

	instantiationService.invokeFunction(accessor => {
93
		const services = new ServiceCollection();
94
		const environmentService = accessor.get(IEnvironmentService);
95
		const { appRoot, extensionsPath, extensionDevelopmentPath, isBuilt, installSourcePath } = environmentService;
96 97 98 99 100 101 102 103
		const telemetryLogService = new FollowerLogService(logLevelClient, createSpdLogService('telemetry', initData.logLevel, environmentService.logsPath));

		let appInsightsAppender: ITelemetryAppender = NullAppender;
		if (product.aiConfig && product.aiConfig.asimovKey && isBuilt) {
			appInsightsAppender = new AppInsightsAppender(eventPrefix, null, product.aiConfig.asimovKey, telemetryLogService);
			disposables.push(appInsightsAppender); // Ensure the AI appender is disposed so that it flushes remaining data
		}
		server.registerChannel('telemetryAppender', new TelemetryAppenderChannel(appInsightsAppender));
104

105
		if (!extensionDevelopmentPath && !environmentService.args['disable-telemetry'] && product.enableTelemetry) {
106
			const config: ITelemetryServiceConfig = {
107
				appender: combinedAppender(appInsightsAppender, new LogAppender(logService)),
108
				commonProperties: resolveCommonProperties(product.commit, pkg.version, configuration.machineId, installSourcePath),
109 110 111 112 113 114 115 116
				piiPaths: [appRoot, extensionsPath]
			};

			services.set(ITelemetryService, new SyncDescriptor(TelemetryService, config));
		} else {
			services.set(ITelemetryService, NullTelemetryService);
		}

117 118
		services.set(IExtensionManagementService, new SyncDescriptor(ExtensionManagementService));
		services.set(IExtensionGalleryService, new SyncDescriptor(ExtensionGalleryService));
S
Sandeep Somavarapu 已提交
119
		services.set(ILocalizationsService, new SyncDescriptor(LocalizationsService));
120

121 122 123 124
		const instantiationService2 = instantiationService.createChild(services);

		instantiationService2.invokeFunction(accessor => {
			const extensionManagementService = accessor.get(IExtensionManagementService);
125
			const channel = new ExtensionManagementChannel(extensionManagementService);
126
			server.registerChannel('extensions', channel);
J
Joao Moreno 已提交
127

128 129
			// clean up deprecated extensions
			(extensionManagementService as ExtensionManagementService).removeDeprecatedExtensions();
130

S
Sandeep Somavarapu 已提交
131 132 133 134
			const localizationsService = accessor.get(ILocalizationsService);
			const localizationsChannel = new LocalizationsChannel(localizationsService);
			server.registerChannel('localizations', localizationsChannel);

135
			createSharedProcessContributions(instantiationService2);
S
Sandeep Somavarapu 已提交
136
			disposables.push(extensionManagementService as ExtensionManagementService);
137
		});
J
Joao Moreno 已提交
138
	});
E
Erich Gamma 已提交
139 140 141 142
}

function setupIPC(hook: string): TPromise<Server> {
	function setup(retry: boolean): TPromise<Server> {
R
Ron Buckton 已提交
143
		return serve(hook).then(null, err => {
E
Erich Gamma 已提交
144 145 146 147 148 149
			if (!retry || platform.isWindows || err.code !== 'EADDRINUSE') {
				return TPromise.wrapError(err);
			}

			// should retry, not windows and eaddrinuse

150
			return connect(hook, '').then(
E
Erich Gamma 已提交
151 152 153 154 155 156 157 158 159 160 161 162 163 164
				client => {
					// we could connect to a running instance. this is not good, abort
					client.dispose();
					return TPromise.wrapError(new Error('There is an instance already running.'));
				},
				err => {
					// it happens on Linux and OS X that the pipe is left behind
					// let's delete it, since we can't connect to it
					// and the retry the whole thing
					try {
						fs.unlinkSync(hook);
					} catch (e) {
						return TPromise.wrapError(new Error('Error deleting the shared ipc hook.'));
					}
S
Sandeep Somavarapu 已提交
165

E
Erich Gamma 已提交
166 167 168 169 170 171 172 173 174
					return setup(false);
				}
			);
		});
	}

	return setup(true);
}

175
function startHandshake(): TPromise<ISharedProcessInitData> {
J
Joao Moreno 已提交
176
	return new TPromise<ISharedProcessInitData>((c, e) => {
M
Matt Bierner 已提交
177
		ipcRenderer.once('handshake:hey there', (_: any, r: ISharedProcessInitData) => c(r));
178
		ipcRenderer.send('handshake:hello');
E
Erich Gamma 已提交
179 180 181
	});
}

182
function handshake(configuration: ISharedProcessConfiguration): TPromise<void> {
183
	return startHandshake()
184
		.then(data => setupIPC(data.sharedIPCHandle).then(server => main(server, data, configuration)))
185
		.then(() => ipcRenderer.send('handshake:im ready'));
186
}