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

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

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

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

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

62 63
const eventPrefix = 'monacoworkbench';

64
function main(server: Server, initData: ISharedProcessInitData, configuration: ISharedProcessConfiguration): void {
J
Joao Moreno 已提交
65
	const services = new ServiceCollection();
J
Joao Moreno 已提交
66

S
Sandeep Somavarapu 已提交
67
	const disposables: IDisposable[] = [];
J
Joao Moreno 已提交
68 69 70 71 72 73

	const onExit = () => dispose(disposables);
	process.once('exit', onExit);
	ipcRenderer.once('handshake:goodbye', onExit);

	disposables.push(server);
E
Erich Gamma 已提交
74

75
	const environmentService = new EnvironmentService(initData.args, process.execPath);
J
Joao Moreno 已提交
76 77 78

	const mainRouter = new StaticRouter(ctx => ctx === 'main');
	const logLevelClient = new LogLevelSetterChannelClient(server.getChannel('loglevel', mainRouter));
S
Sandeep Somavarapu 已提交
79
	const logService = new FollowerLogService(logLevelClient, createSpdLogService('sharedprocess', initData.logLevel, environmentService.logsPath));
S
Sandeep Somavarapu 已提交
80
	disposables.push(logService);
J
Joao Moreno 已提交
81

82 83 84 85
	logService.info('main', JSON.stringify(configuration));

	services.set(IEnvironmentService, environmentService);
	services.set(ILogService, logService);
86
	services.set(IConfigurationService, new SyncDescriptor(ConfigurationService));
J
Joao Moreno 已提交
87
	services.set(IRequestService, new SyncDescriptor(RequestService));
S
Sandeep Somavarapu 已提交
88
	services.set(IDownloadService, new SyncDescriptor(DownloadService));
J
Joao Moreno 已提交
89

J
Joao Moreno 已提交
90
	const windowsChannel = server.getChannel('windows', mainRouter);
J
Joao Moreno 已提交
91 92
	const windowsService = new WindowsChannelClient(windowsChannel);
	services.set(IWindowsService, windowsService);
S
Sandeep Somavarapu 已提交
93

J
Joao Moreno 已提交
94
	const activeWindowManager = new ActiveWindowManager(windowsService);
J
Joao Moreno 已提交
95 96
	const activeWindowRouter = new StaticRouter(ctx => activeWindowManager.getActiveClientId().then(id => ctx === id));
	const dialogChannel = server.getChannel('dialog', activeWindowRouter);
97
	services.set(IDialogService, new DialogChannelClient(dialogChannel));
S
Sandeep Somavarapu 已提交
98

J
Joao Moreno 已提交
99 100 101
	const instantiationService = new InstantiationService(services);

	instantiationService.invokeFunction(accessor => {
102
		const services = new ServiceCollection();
103
		const environmentService = accessor.get(IEnvironmentService);
104
		const { appRoot, extensionsPath, extensionDevelopmentLocationURI, isBuilt, installSourcePath } = environmentService;
105
		const telemetryLogService = new FollowerLogService(logLevelClient, createSpdLogService('telemetry', initData.logLevel, environmentService.logsPath));
106 107
		telemetryLogService.info('The below are logs for every telemetry event sent from VS Code once the log level is set to trace.');
		telemetryLogService.info('===========================================================');
108

109
		let appInsightsAppender: ITelemetryAppender | null = NullAppender;
110
		if (!extensionDevelopmentLocationURI && !environmentService.args['disable-telemetry'] && product.enableTelemetry) {
111 112 113 114
			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
			}
115
			const config: ITelemetryServiceConfig = {
116
				appender: combinedAppender(appInsightsAppender, new LogAppender(logService)),
117
				commonProperties: resolveCommonProperties(product.commit, pkg.version, configuration.machineId, installSourcePath),
118 119 120
				piiPaths: [appRoot, extensionsPath]
			};

121
			services.set(ITelemetryService, new SyncDescriptor(TelemetryService, [config]));
122 123 124
		} else {
			services.set(ITelemetryService, NullTelemetryService);
		}
125
		server.registerChannel('telemetryAppender', new TelemetryAppenderChannel(appInsightsAppender));
126

127 128
		services.set(IExtensionManagementService, new SyncDescriptor(ExtensionManagementService));
		services.set(IExtensionGalleryService, new SyncDescriptor(ExtensionGalleryService));
S
Sandeep Somavarapu 已提交
129
		services.set(ILocalizationsService, new SyncDescriptor(LocalizationsService));
130

131 132 133
		const instantiationService2 = instantiationService.createChild(services);

		instantiationService2.invokeFunction(accessor => {
A
Tweaks  
Alex Dima 已提交
134

135
			const extensionManagementService = accessor.get(IExtensionManagementService);
136
			const channel = new ExtensionManagementChannel(extensionManagementService, () => DefaultURITransformer);
137
			server.registerChannel('extensions', channel);
J
Joao Moreno 已提交
138

139 140
			// clean up deprecated extensions
			(extensionManagementService as ExtensionManagementService).removeDeprecatedExtensions();
141

S
Sandeep Somavarapu 已提交
142 143 144 145
			const localizationsService = accessor.get(ILocalizationsService);
			const localizationsChannel = new LocalizationsChannel(localizationsService);
			server.registerChannel('localizations', localizationsChannel);

146
			createSharedProcessContributions(instantiationService2);
S
Sandeep Somavarapu 已提交
147
			disposables.push(extensionManagementService as ExtensionManagementService);
148
		});
J
Joao Moreno 已提交
149
	});
E
Erich Gamma 已提交
150 151
}

J
Johannes Rieken 已提交
152 153
function setupIPC(hook: string): Promise<Server> {
	function setup(retry: boolean): Promise<Server> {
R
Ron Buckton 已提交
154
		return serve(hook).then(null, err => {
E
Erich Gamma 已提交
155
			if (!retry || platform.isWindows || err.code !== 'EADDRINUSE') {
J
Joao Moreno 已提交
156
				return Promise.reject(err);
E
Erich Gamma 已提交
157 158 159 160
			}

			// should retry, not windows and eaddrinuse

161
			return connect(hook, '').then(
E
Erich Gamma 已提交
162 163 164
				client => {
					// we could connect to a running instance. this is not good, abort
					client.dispose();
J
Joao Moreno 已提交
165
					return Promise.reject(new Error('There is an instance already running.'));
E
Erich Gamma 已提交
166 167 168 169 170 171 172 173
				},
				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) {
J
Joao Moreno 已提交
174
						return Promise.reject(new Error('Error deleting the shared ipc hook.'));
E
Erich Gamma 已提交
175
					}
S
Sandeep Somavarapu 已提交
176

E
Erich Gamma 已提交
177 178 179 180 181 182 183 184 185
					return setup(false);
				}
			);
		});
	}

	return setup(true);
}

186 187
async function handshake(configuration: ISharedProcessConfiguration): Promise<void> {
	const data = await new Promise<ISharedProcessInitData>(c => {
M
Matt Bierner 已提交
188
		ipcRenderer.once('handshake:hey there', (_: any, r: ISharedProcessInitData) => c(r));
189
		ipcRenderer.send('handshake:hello');
E
Erich Gamma 已提交
190 191
	});

192 193 194 195
	const server = await setupIPC(data.sharedIPCHandle);

	main(server, data, configuration);
	ipcRenderer.send('handshake:im ready');
196
}