desktop.main.ts 14.3 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.
 *--------------------------------------------------------------------------------------------*/

6
import * as fs from 'fs';
7
import * as gracefulFs from 'graceful-fs';
8
import { zoomLevelToZoomFactor } from 'vs/platform/windows/common/windows';
9
import { importEntries, mark } from 'vs/base/common/performance';
10
import { Workbench } from 'vs/workbench/browser/workbench';
11
import { NativeWindow } from 'vs/workbench/electron-browser/window';
12
import { setZoomLevel, setZoomFactor, setFullscreen } from 'vs/base/browser/browser';
13
import { domContentLoaded, addDisposableListener, EventType, scheduleAtNextAnimationFrame } from 'vs/base/browser/dom';
14
import { onUnexpectedError } from 'vs/base/common/errors';
15
import { URI } from 'vs/base/common/uri';
16
import { WorkspaceService } from 'vs/workbench/services/configuration/browser/configurationService';
17
import { NativeWorkbenchEnvironmentService } from 'vs/workbench/services/environment/electron-browser/environmentService';
18
import { INativeWorkbenchConfiguration, IWorkbenchEnvironmentService } from 'vs/workbench/services/environment/common/environmentService';
19
import { ServiceCollection } from 'vs/platform/instantiation/common/serviceCollection';
20
import { ISingleFolderWorkspaceIdentifier, IWorkspaceInitializationPayload, ISingleFolderWorkspaceInitializationPayload, reviveWorkspaceIdentifier } from 'vs/platform/workspaces/common/workspaces';
21
import { ILogService } from 'vs/platform/log/common/log';
22
import { NativeStorageService } from 'vs/platform/storage/node/storageService';
23
import { Schemas } from 'vs/base/common/network';
B
Benjamin Pasero 已提交
24
import { sanitizeFilePath } from 'vs/base/common/extpath';
B
wip  
Benjamin Pasero 已提交
25
import { GlobalStorageDatabaseChannelClient } from 'vs/platform/storage/node/storageIpc';
B
Benjamin Pasero 已提交
26 27 28
import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { IStorageService } from 'vs/platform/storage/common/storage';
29
import { Disposable } from 'vs/base/common/lifecycle';
B
Benjamin Pasero 已提交
30
import { registerWindowDriver } from 'vs/platform/driver/electron-browser/driver';
31
import { IMainProcessService, MainProcessService } from 'vs/platform/ipc/electron-sandbox/mainProcessService';
32 33 34 35
import { RemoteAuthorityResolverService } from 'vs/platform/remote/electron-browser/remoteAuthorityResolverService';
import { IRemoteAuthorityResolverService } from 'vs/platform/remote/common/remoteAuthorityResolver';
import { RemoteAgentService } from 'vs/workbench/services/remote/electron-browser/remoteAgentServiceImpl';
import { IRemoteAgentService } from 'vs/workbench/services/remote/common/remoteAgentService';
36
import { FileService } from 'vs/platform/files/common/fileService';
37
import { IFileService } from 'vs/platform/files/common/files';
38
import { DiskFileSystemProvider } from 'vs/platform/files/electron-browser/diskFileSystemProvider';
39
import { RemoteFileSystemProvider } from 'vs/workbench/services/remote/common/remoteAgentFileSystemChannel';
S
Sandeep Somavarapu 已提交
40
import { ConfigurationCache } from 'vs/workbench/services/configuration/node/configurationCache';
I
isidor 已提交
41 42
import { SignService } from 'vs/platform/sign/node/signService';
import { ISignService } from 'vs/platform/sign/common/sign';
S
Sandeep Somavarapu 已提交
43
import { FileUserDataProvider } from 'vs/workbench/services/userData/common/fileUserDataProvider';
44
import { basename } from 'vs/base/common/resources';
45
import { IProductService } from 'vs/platform/product/common/productService';
46
import product from 'vs/platform/product/common/product';
47 48
import { NativeResourceIdentityService } from 'vs/platform/resource/node/resourceIdentityServiceImpl';
import { IResourceIdentityService } from 'vs/platform/resource/common/resourceIdentityService';
B
Benjamin Pasero 已提交
49
import { NativeLogService } from 'vs/workbench/services/log/electron-browser/logService';
50
import { IElectronService, ElectronService } from 'vs/platform/electron/electron-sandbox/electron';
J
Joao Moreno 已提交
51

B
Benjamin Pasero 已提交
52
class DesktopMain extends Disposable {
E
Erich Gamma 已提交
53

B
Benjamin Pasero 已提交
54
	private readonly environmentService = new NativeWorkbenchEnvironmentService(this.configuration, this.configuration.execPath);
55

56
	constructor(private configuration: INativeWorkbenchConfiguration) {
B
Benjamin Pasero 已提交
57
		super();
B
Benjamin Pasero 已提交
58

B
Benjamin Pasero 已提交
59 60
		this.init();
	}
M
Martin Aeschlimann 已提交
61

B
Benjamin Pasero 已提交
62
	private init(): void {
63

B
Benjamin Pasero 已提交
64 65
		// Enable gracefulFs
		gracefulFs.gracefulify(fs);
66

B
Benjamin Pasero 已提交
67 68
		// Massage configuration file URIs
		this.reviveUris();
69

B
Benjamin Pasero 已提交
70
		// Setup perf
71
		importEntries(this.environmentService.configuration.perfEntries);
A
Alex Dima 已提交
72

B
Benjamin Pasero 已提交
73
		// Browser config
74 75 76
		const zoomLevel = this.configuration.zoomLevel || 0;
		setZoomFactor(zoomLevelToZoomFactor(zoomLevel));
		setZoomLevel(zoomLevel, true /* isTrusted */);
77
		setFullscreen(!!this.environmentService.configuration.fullscreen);
M
Martin Aeschlimann 已提交
78
	}
79

B
Benjamin Pasero 已提交
80
	private reviveUris() {
81 82
		if (this.environmentService.configuration.folderUri) {
			this.environmentService.configuration.folderUri = URI.revive(this.environmentService.configuration.folderUri);
B
Benjamin Pasero 已提交
83
		}
84

85 86
		if (this.environmentService.configuration.workspace) {
			this.environmentService.configuration.workspace = reviveWorkspaceIdentifier(this.environmentService.configuration.workspace);
M
Martin Aeschlimann 已提交
87
		}
B
Benjamin Pasero 已提交
88

89
		const filesToWait = this.environmentService.configuration.filesToWait;
B
Benjamin Pasero 已提交
90
		const filesToWaitPaths = filesToWait?.paths;
91
		[filesToWaitPaths, this.environmentService.configuration.filesToOpenOrCreate, this.environmentService.configuration.filesToDiff].forEach(paths => {
B
Benjamin Pasero 已提交
92 93 94
			if (Array.isArray(paths)) {
				paths.forEach(path => {
					if (path.fileUri) {
95
						path.fileUri = URI.revive(path.fileUri);
B
Benjamin Pasero 已提交
96 97 98 99
					}
				});
			}
		});
100

M
Martin Aeschlimann 已提交
101
		if (filesToWait) {
102
			filesToWait.waitMarkerFileUri = URI.revive(filesToWait.waitMarkerFileUri);
M
Martin Aeschlimann 已提交
103
		}
B
Benjamin Pasero 已提交
104
	}
B
Benjamin Pasero 已提交
105

106 107
	async open(): Promise<void> {
		const services = await this.initServices();
B
Benjamin Pasero 已提交
108

109 110
		await domContentLoaded();
		mark('willStartWorkbench');
B
Benjamin Pasero 已提交
111

112
		// Create Workbench
113
		const workbench = new Workbench(document.body, services.serviceCollection, services.logService);
B
Benjamin Pasero 已提交
114

B
Benjamin Pasero 已提交
115 116
		// Listeners
		this.registerListeners(workbench, services.storageService);
117

118
		// Startup
119
		const instantiationService = workbench.startup();
B
Benjamin Pasero 已提交
120

121
		// Window
122
		this._register(instantiationService.createInstance(NativeWindow));
B
Benjamin Pasero 已提交
123

124
		// Driver
125
		if (this.environmentService.configuration.driver) {
B
Benjamin Pasero 已提交
126
			instantiationService.invokeFunction(async accessor => this._register(await registerWindowDriver(accessor, this.configuration.windowId)));
127
		}
128

129
		// Logging
130
		services.logService.trace('workbench configuration', JSON.stringify(this.environmentService.configuration));
B
Benjamin Pasero 已提交
131
	}
132

133
	private registerListeners(workbench: Workbench, storageService: NativeStorageService): void {
B
Benjamin Pasero 已提交
134 135 136 137 138 139 140 141 142

		// Layout
		this._register(addDisposableListener(window, EventType.RESIZE, e => this.onWindowResize(e, true, workbench)));

		// Workbench Lifecycle
		this._register(workbench.onShutdown(() => this.dispose()));
		this._register(workbench.onWillShutdown(event => event.join(storageService.close())));
	}

143
	private onWindowResize(e: Event, retry: boolean, workbench: Workbench): void {
144 145 146 147 148 149 150 151
		if (e.target === window) {
			if (window.document && window.document.body && window.document.body.clientWidth === 0) {
				// TODO@Ben this is an electron issue on macOS when simple fullscreen is enabled
				// where for some reason the window clientWidth is reported as 0 when switching
				// between simple fullscreen and normal screen. In that case we schedule the layout
				// call at the next animation frame once, in the hope that the dimensions are
				// proper then.
				if (retry) {
152
					scheduleAtNextAnimationFrame(() => this.onWindowResize(e, false, workbench));
153 154 155 156
				}
				return;
			}

157
			workbench.layout();
158 159 160
		}
	}

S
Sandeep Somavarapu 已提交
161
	private async initServices(): Promise<{ serviceCollection: ServiceCollection, logService: ILogService, storageService: NativeStorageService }> {
B
Benjamin Pasero 已提交
162
		const serviceCollection = new ServiceCollection();
163

164 165
		// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
		// NOTE: DO NOT ADD ANY OTHER SERVICE INTO THE COLLECTION HERE.
166
		// CONTRIBUTE IT VIA WORKBENCH.DESKTOP.MAIN.TS AND registerSingleton().
167 168
		// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

169
		// Main Process
170 171
		const mainProcessService = this._register(new MainProcessService(this.configuration.windowId));
		serviceCollection.set(IMainProcessService, mainProcessService);
172

B
Benjamin Pasero 已提交
173
		// Environment
174
		serviceCollection.set(IWorkbenchEnvironmentService, this.environmentService);
B
Benjamin Pasero 已提交
175

176
		// Product
177 178
		const productService: IProductService = { _serviceBrand: undefined, ...product };
		serviceCollection.set(IProductService, productService);
179

B
Benjamin Pasero 已提交
180
		// Log
B
Benjamin Pasero 已提交
181
		const logService = this._register(new NativeLogService(this.configuration.windowId, mainProcessService, this.environmentService));
B
Benjamin Pasero 已提交
182 183
		serviceCollection.set(ILogService, logService);

184 185 186
		// Remote
		const remoteAuthorityResolverService = new RemoteAuthorityResolverService();
		serviceCollection.set(IRemoteAuthorityResolverService, remoteAuthorityResolverService);
B
Benjamin Pasero 已提交
187

I
isidor 已提交
188 189 190 191
		// Sign
		const signService = new SignService();
		serviceCollection.set(ISignService, signService);

192
		// Remote Agent
A
Alex Dima 已提交
193
		const remoteAgentService = this._register(new RemoteAgentService(this.environmentService, productService, remoteAuthorityResolverService, signService, logService));
194 195
		serviceCollection.set(IRemoteAgentService, remoteAgentService);

196
		// Electron
197
		const electronService = new ElectronService(this.configuration.windowId, mainProcessService) as IElectronService;
198 199
		serviceCollection.set(IElectronService, electronService);

B
Benjamin Pasero 已提交
200
		// Files
B
Benjamin Pasero 已提交
201
		const fileService = this._register(new FileService(logService));
B
Benjamin Pasero 已提交
202 203
		serviceCollection.set(IFileService, fileService);

204
		const diskFileSystemProvider = this._register(new DiskFileSystemProvider(logService, electronService));
205
		fileService.registerProvider(Schemas.file, diskFileSystemProvider);
B
Benjamin Pasero 已提交
206

207
		// User Data Provider
208
		fileService.registerProvider(Schemas.userData, new FileUserDataProvider(this.environmentService.appSettingsHome, this.environmentService.backupHome, diskFileSystemProvider, this.environmentService, logService));
209

210 211
		const connection = remoteAgentService.getConnection();
		if (connection) {
212
			const remoteFileSystemProvider = this._register(new RemoteFileSystemProvider(remoteAgentService));
B
Benjamin Pasero 已提交
213
			fileService.registerProvider(Schemas.vscodeRemote, remoteFileSystemProvider);
214 215
		}

216 217 218 219
		const resourceIdentityService = this._register(new NativeResourceIdentityService());
		serviceCollection.set(IResourceIdentityService, resourceIdentityService);

		const payload = await this.resolveWorkspaceInitializationPayload(resourceIdentityService);
220 221

		const services = await Promise.all([
222
			this.createWorkspaceService(payload, fileService, remoteAgentService, logService).then(service => {
B
Benjamin Pasero 已提交
223

224 225
				// Workspace
				serviceCollection.set(IWorkspaceContextService, service);
B
Benjamin Pasero 已提交
226

227 228
				// Configuration
				serviceCollection.set(IConfigurationService, service);
B
Benjamin Pasero 已提交
229

230 231
				return service;
			}),
B
Benjamin Pasero 已提交
232

233
			this.createStorageService(payload, logService, mainProcessService).then(service => {
234 235 236 237 238 239

				// Storage
				serviceCollection.set(IStorageService, service);

				return service;
			})
240 241 242
		]);

		return { serviceCollection, logService, storageService: services[1] };
243
	}
244

245
	private async resolveWorkspaceInitializationPayload(resourceIdentityService: IResourceIdentityService): Promise<IWorkspaceInitializationPayload> {
B
Benjamin Pasero 已提交
246 247

		// Multi-root workspace
248 249
		if (this.environmentService.configuration.workspace) {
			return this.environmentService.configuration.workspace;
250 251
		}

B
Benjamin Pasero 已提交
252
		// Single-folder workspace
253
		let workspaceInitializationPayload: IWorkspaceInitializationPayload | undefined;
254
		if (this.environmentService.configuration.folderUri) {
255
			workspaceInitializationPayload = await this.resolveSingleFolderWorkspaceInitializationPayload(this.environmentService.configuration.folderUri, resourceIdentityService);
B
Benjamin Pasero 已提交
256
		}
E
Erich Gamma 已提交
257

258 259 260
		// Fallback to empty workspace if we have no payload yet.
		if (!workspaceInitializationPayload) {
			let id: string;
261 262 263
			if (this.environmentService.configuration.backupWorkspaceResource) {
				id = basename(this.environmentService.configuration.backupWorkspaceResource); // we know the backupPath must be a unique path so we leverage its name as workspace ID
			} else if (this.environmentService.isExtensionDevelopment) {
264 265 266
				id = 'ext-dev'; // extension development window never stores backups and is a singleton
			} else {
				throw new Error('Unexpected window configuration without backupPath');
B
Benjamin Pasero 已提交
267
			}
268

269 270 271 272
			workspaceInitializationPayload = { id };
		}

		return workspaceInitializationPayload;
B
Benjamin Pasero 已提交
273
	}
274

275
	private async resolveSingleFolderWorkspaceInitializationPayload(folderUri: ISingleFolderWorkspaceIdentifier, resourceIdentityService: IResourceIdentityService): Promise<ISingleFolderWorkspaceInitializationPayload | undefined> {
276
		try {
277 278 279 280 281
			const folder = folderUri.scheme === Schemas.file
				? URI.file(sanitizeFilePath(folderUri.fsPath, process.env['VSCODE_CWD'] || process.cwd())) // For local: ensure path is absolute
				: folderUri;
			const id = await resourceIdentityService.resolveResourceIdentity(folderUri);
			return { id, folder };
282 283 284 285
		} catch (error) {
			onUnexpectedError(error);
		}
		return;
B
Benjamin Pasero 已提交
286
	}
287

288 289
	private async createWorkspaceService(payload: IWorkspaceInitializationPayload, fileService: FileService, remoteAgentService: IRemoteAgentService, logService: ILogService): Promise<WorkspaceService> {
		const workspaceService = new WorkspaceService({ remoteAuthority: this.environmentService.configuration.remoteAuthority, configurationCache: new ConfigurationCache(this.environmentService) }, this.environmentService, fileService, remoteAgentService);
S
Sandeep Somavarapu 已提交
290

291 292 293 294 295
		try {
			await workspaceService.initialize(payload);

			return workspaceService;
		} catch (error) {
B
Benjamin Pasero 已提交
296 297
			onUnexpectedError(error);
			logService.error(error);
298

B
Benjamin Pasero 已提交
299
			return workspaceService;
300
		}
B
Benjamin Pasero 已提交
301
	}
302

303
	private async createStorageService(payload: IWorkspaceInitializationPayload, logService: ILogService, mainProcessService: IMainProcessService): Promise<NativeStorageService> {
304
		const globalStorageDatabase = new GlobalStorageDatabaseChannelClient(mainProcessService.getChannel('storage'));
305
		const storageService = new NativeStorageService(globalStorageDatabase, logService, this.environmentService);
306

307 308 309 310 311
		try {
			await storageService.initialize(payload);

			return storageService;
		} catch (error) {
B
Benjamin Pasero 已提交
312 313
			onUnexpectedError(error);
			logService.error(error);
J
Joao Moreno 已提交
314

B
Benjamin Pasero 已提交
315
			return storageService;
316
		}
B
Benjamin Pasero 已提交
317
	}
318

319 320
}

321
export function main(configuration: INativeWorkbenchConfiguration): Promise<void> {
322
	const workbench = new DesktopMain(configuration);
323

324
	return workbench.open();
325
}