sharedProcessMain.ts 11.1 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/product/node/product';
import pkg from 'vs/platform/product/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';
31
import { WindowsService } from 'vs/platform/windows/electron-browser/windowsService';
32
import { ipcRenderer } from 'electron';
33
import { createBufferSpdLogService } from 'vs/platform/log/node/spdlogService';
S
Sandeep Somavarapu 已提交
34
import { ILogService, LogLevel } from 'vs/platform/log/common/log';
J
Joao Moreno 已提交
35
import { LogLevelSetterChannelClient, FollowerLogService } from 'vs/platform/log/node/logIpc';
S
Sandeep Somavarapu 已提交
36 37
import { LocalizationsService } from 'vs/platform/localizations/node/localizations';
import { ILocalizationsService } from 'vs/platform/localizations/common/localizations';
J
Joao Moreno 已提交
38 39
import { LocalizationsChannel } from 'vs/platform/localizations/node/localizationsIpc';
import { DialogChannelClient } from 'vs/platform/dialogs/node/dialogIpc';
40
import { IDialogService } from 'vs/platform/dialogs/common/dialogs';
41
import { IDisposable, dispose, combinedDisposable } from 'vs/base/common/lifecycle';
S
Sandeep Somavarapu 已提交
42
import { DownloadService } from 'vs/platform/download/node/downloadService';
43
import { IDownloadService } from 'vs/platform/download/common/download';
A
Alex Dima 已提交
44
import { IChannel, IServerChannel, StaticRouter } from 'vs/base/parts/ipc/common/ipc';
S
Sandeep Somavarapu 已提交
45 46 47 48
import { NodeCachedDataCleaner } from 'vs/code/electron-browser/sharedProcess/contrib/nodeCachedDataCleaner';
import { LanguagePackCachedDataCleaner } from 'vs/code/electron-browser/sharedProcess/contrib/languagePackCachedDataCleaner';
import { StorageDataCleaner } from 'vs/code/electron-browser/sharedProcess/contrib/storageDataCleaner';
import { LogsDataCleaner } from 'vs/code/electron-browser/sharedProcess/contrib/logsDataCleaner';
49 50
import { IMainProcessService } from 'vs/platform/ipc/electron-browser/mainProcessService';
import { ServiceIdentifier } from 'vs/platform/instantiation/common/instantiation';
51

52
export interface ISharedProcessConfiguration {
B
Benjamin Pasero 已提交
53
	readonly machineId: string;
54 55 56 57 58 59
}

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

60
interface ISharedProcessInitData {
61
	sharedIPCHandle: string;
J
Joao Moreno 已提交
62
	args: ParsedArgs;
S
Sandeep Somavarapu 已提交
63
	logLevel: LogLevel;
E
Erich Gamma 已提交
64 65
}

66 67
const eventPrefix = 'monacoworkbench';

68 69 70 71 72 73 74 75 76 77 78 79 80 81
class MainProcessService implements IMainProcessService {
	constructor(private server: Server, private mainRouter: StaticRouter) { }

	_serviceBrand: ServiceIdentifier<any>;

	getChannel(channelName: string): IChannel {
		return this.server.getChannel(channelName, this.mainRouter);
	}

	registerChannel(channelName: string, channel: IServerChannel<string>): void {
		this.server.registerChannel(channelName, channel);
	}
}

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

S
Sandeep Somavarapu 已提交
85
	const disposables: IDisposable[] = [];
J
Joao Moreno 已提交
86 87 88 89 90 91

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

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

93
	const environmentService = new EnvironmentService(initData.args, process.execPath);
J
Joao Moreno 已提交
94 95 96

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

100 101 102 103
	logService.info('main', JSON.stringify(configuration));

	services.set(IEnvironmentService, environmentService);
	services.set(ILogService, logService);
104
	services.set(IConfigurationService, new SyncDescriptor(ConfigurationService, [environmentService.appSettingsPath]));
J
Joao Moreno 已提交
105
	services.set(IRequestService, new SyncDescriptor(RequestService));
S
Sandeep Somavarapu 已提交
106
	services.set(IDownloadService, new SyncDescriptor(DownloadService));
J
Joao Moreno 已提交
107

108 109 110 111
	const mainProcessService = new MainProcessService(server, mainRouter);
	services.set(IMainProcessService, mainProcessService);

	const windowsService = new WindowsService(mainProcessService);
J
Joao Moreno 已提交
112
	services.set(IWindowsService, windowsService);
S
Sandeep Somavarapu 已提交
113

J
Joao Moreno 已提交
114
	const activeWindowManager = new ActiveWindowManager(windowsService);
J
Joao Moreno 已提交
115 116
	const activeWindowRouter = new StaticRouter(ctx => activeWindowManager.getActiveClientId().then(id => ctx === id));
	const dialogChannel = server.getChannel('dialog', activeWindowRouter);
117
	services.set(IDialogService, new DialogChannelClient(dialogChannel));
S
Sandeep Somavarapu 已提交
118

J
Joao Moreno 已提交
119 120 121
	const instantiationService = new InstantiationService(services);

	instantiationService.invokeFunction(accessor => {
122
		const services = new ServiceCollection();
123
		const environmentService = accessor.get(IEnvironmentService);
124
		const { appRoot, extensionsPath, extensionDevelopmentLocationURI: extensionDevelopmentLocationURI, isBuilt, installSourcePath } = environmentService;
125
		const telemetryLogService = new FollowerLogService(logLevelClient, createBufferSpdLogService('telemetry', initData.logLevel, environmentService.logsPath));
126 127
		telemetryLogService.info('The below are logs for every telemetry event sent from VS Code once the log level is set to trace.');
		telemetryLogService.info('===========================================================');
128

129
		let appInsightsAppender: ITelemetryAppender | null = NullAppender;
130
		if (!extensionDevelopmentLocationURI && !environmentService.args['disable-telemetry'] && product.enableTelemetry) {
131 132 133 134
			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
			}
135
			const config: ITelemetryServiceConfig = {
136
				appender: combinedAppender(appInsightsAppender, new LogAppender(logService)),
137
				commonProperties: resolveCommonProperties(product.commit, pkg.version, configuration.machineId, installSourcePath),
138 139 140
				piiPaths: [appRoot, extensionsPath]
			};

141
			services.set(ITelemetryService, new SyncDescriptor(TelemetryService, [config]));
142 143 144
		} else {
			services.set(ITelemetryService, NullTelemetryService);
		}
145
		server.registerChannel('telemetryAppender', new TelemetryAppenderChannel(appInsightsAppender));
146

147
		services.set(IExtensionManagementService, new SyncDescriptor(ExtensionManagementService));
148
		services.set(IExtensionGalleryService, new SyncDescriptor(ExtensionGalleryService));
S
Sandeep Somavarapu 已提交
149
		services.set(ILocalizationsService, new SyncDescriptor(LocalizationsService));
150

151 152 153
		const instantiationService2 = instantiationService.createChild(services);

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

155
			const extensionManagementService = accessor.get(IExtensionManagementService);
156
			const channel = new ExtensionManagementChannel(extensionManagementService, () => null);
157
			server.registerChannel('extensions', channel);
J
Joao Moreno 已提交
158

S
Sandeep Somavarapu 已提交
159 160 161 162
			const localizationsService = accessor.get(ILocalizationsService);
			const localizationsChannel = new LocalizationsChannel(localizationsService);
			server.registerChannel('localizations', localizationsChannel);

163 164 165 166 167
			// clean up deprecated extensions
			(extensionManagementService as ExtensionManagementService).removeDeprecatedExtensions();
			// update localizations cache
			(localizationsService as LocalizationsService).update();
			// cache clean ups
168
			disposables.push(combinedDisposable(
S
Sandeep Somavarapu 已提交
169 170 171 172
				instantiationService2.createInstance(NodeCachedDataCleaner),
				instantiationService2.createInstance(LanguagePackCachedDataCleaner),
				instantiationService2.createInstance(StorageDataCleaner),
				instantiationService2.createInstance(LogsDataCleaner)
173
			));
S
Sandeep Somavarapu 已提交
174
			disposables.push(extensionManagementService as ExtensionManagementService);
175
		});
J
Joao Moreno 已提交
176
	});
E
Erich Gamma 已提交
177 178
}

J
Johannes Rieken 已提交
179 180
function setupIPC(hook: string): Promise<Server> {
	function setup(retry: boolean): Promise<Server> {
R
Ron Buckton 已提交
181
		return serve(hook).then(null, err => {
E
Erich Gamma 已提交
182
			if (!retry || platform.isWindows || err.code !== 'EADDRINUSE') {
J
Joao Moreno 已提交
183
				return Promise.reject(err);
E
Erich Gamma 已提交
184 185 186 187
			}

			// should retry, not windows and eaddrinuse

188
			return connect(hook, '').then(
E
Erich Gamma 已提交
189 190 191
				client => {
					// we could connect to a running instance. this is not good, abort
					client.dispose();
J
Joao Moreno 已提交
192
					return Promise.reject(new Error('There is an instance already running.'));
E
Erich Gamma 已提交
193 194 195 196 197 198 199 200
				},
				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 已提交
201
						return Promise.reject(new Error('Error deleting the shared ipc hook.'));
E
Erich Gamma 已提交
202
					}
S
Sandeep Somavarapu 已提交
203

E
Erich Gamma 已提交
204 205 206 207 208 209 210 211 212
					return setup(false);
				}
			);
		});
	}

	return setup(true);
}

213 214
async function handshake(configuration: ISharedProcessConfiguration): Promise<void> {
	const data = await new Promise<ISharedProcessInitData>(c => {
M
Matt Bierner 已提交
215
		ipcRenderer.once('handshake:hey there', (_: any, r: ISharedProcessInitData) => c(r));
216
		ipcRenderer.send('handshake:hello');
E
Erich Gamma 已提交
217 218
	});

219 220 221 222
	const server = await setupIPC(data.sharedIPCHandle);

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