desktop.main.ts 14.4 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-sandbox/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 19
import { IWorkbenchEnvironmentService } from 'vs/workbench/services/environment/common/environmentService';
import { INativeWorkbenchConfiguration } from 'vs/workbench/services/environment/electron-sandbox/environmentService';
20
import { ServiceCollection } from 'vs/platform/instantiation/common/serviceCollection';
21
import { ISingleFolderWorkspaceIdentifier, IWorkspaceInitializationPayload, ISingleFolderWorkspaceInitializationPayload, reviveWorkspaceIdentifier } from 'vs/platform/workspaces/common/workspaces';
22
import { ILogService } from 'vs/platform/log/common/log';
23
import { NativeStorageService } from 'vs/platform/storage/node/storageService';
24
import { Schemas } from 'vs/base/common/network';
B
Benjamin Pasero 已提交
25
import { sanitizeFilePath } from 'vs/base/common/extpath';
B
wip  
Benjamin Pasero 已提交
26
import { GlobalStorageDatabaseChannelClient } from 'vs/platform/storage/node/storageIpc';
B
Benjamin Pasero 已提交
27 28 29
import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { IStorageService } from 'vs/platform/storage/common/storage';
30
import { Disposable } from 'vs/base/common/lifecycle';
B
Benjamin Pasero 已提交
31
import { registerWindowDriver } from 'vs/platform/driver/electron-browser/driver';
32
import { IMainProcessService, MainProcessService } from 'vs/platform/ipc/electron-sandbox/mainProcessService';
33
import { RemoteAuthorityResolverService } from 'vs/platform/remote/electron-sandbox/remoteAuthorityResolverService';
34 35 36
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';
37
import { FileService } from 'vs/platform/files/common/fileService';
38
import { IFileService } from 'vs/platform/files/common/files';
39
import { DiskFileSystemProvider } from 'vs/platform/files/electron-browser/diskFileSystemProvider';
40
import { RemoteFileSystemProvider } from 'vs/workbench/services/remote/common/remoteAgentFileSystemChannel';
41
import { ConfigurationCache } from 'vs/workbench/services/configuration/electron-browser/configurationCache';
I
isidor 已提交
42 43
import { SignService } from 'vs/platform/sign/node/signService';
import { ISignService } from 'vs/platform/sign/common/sign';
S
Sandeep Somavarapu 已提交
44
import { FileUserDataProvider } from 'vs/workbench/services/userData/common/fileUserDataProvider';
45
import { basename } from 'vs/base/common/resources';
46
import { IProductService } from 'vs/platform/product/common/productService';
47
import product from 'vs/platform/product/common/product';
48 49
import { NativeResourceIdentityService } from 'vs/platform/resource/node/resourceIdentityServiceImpl';
import { IResourceIdentityService } from 'vs/platform/resource/common/resourceIdentityService';
B
Benjamin Pasero 已提交
50
import { NativeLogService } from 'vs/workbench/services/log/electron-browser/logService';
51
import { IElectronService, ElectronService } from 'vs/platform/electron/electron-sandbox/electron';
J
Joao Moreno 已提交
52

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

55 56
	private readonly productService: IProductService = { _serviceBrand: undefined, ...product };
	private readonly environmentService = new NativeWorkbenchEnvironmentService(this.configuration, this.productService);
57

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

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

B
Benjamin Pasero 已提交
64
	private init(): void {
65

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

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

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

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

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

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

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

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

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

111 112
		await domContentLoaded();
		mark('willStartWorkbench');
B
Benjamin Pasero 已提交
113

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

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

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

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

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

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

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

		// 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())));
	}

145
	private onWindowResize(e: Event, retry: boolean, workbench: Workbench): void {
146 147 148 149 150 151 152 153
		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) {
154
					scheduleAtNextAnimationFrame(() => this.onWindowResize(e, false, workbench));
155 156 157 158
				}
				return;
			}

159
			workbench.layout();
160 161 162
		}
	}

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

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

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

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

178
		// Product
179
		serviceCollection.set(IProductService, this.productService);
180

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

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

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

193
		// Remote Agent
194
		const remoteAgentService = this._register(new RemoteAgentService(this.environmentService, this.productService, remoteAuthorityResolverService, signService, logService));
195 196
		serviceCollection.set(IRemoteAgentService, remoteAgentService);

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

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

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

208
		// User Data Provider
S
Sandeep Somavarapu 已提交
209
		fileService.registerProvider(Schemas.userData, new FileUserDataProvider(this.environmentService.appSettingsHome, this.environmentService.configuration.backupPath ? URI.file(this.environmentService.configuration.backupPath) : undefined, diskFileSystemProvider, this.environmentService, logService));
210

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

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

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

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

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

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

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

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

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

				return service;
			})
241 242 243
		]);

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

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

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

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

259 260 261
		// Fallback to empty workspace if we have no payload yet.
		if (!workspaceInitializationPayload) {
			let id: string;
S
Sandeep Somavarapu 已提交
262 263
			if (this.environmentService.backupWorkspaceHome) {
				id = basename(this.environmentService.backupWorkspaceHome); // we know the backupPath must be a unique path so we leverage its name as workspace ID
264
			} else if (this.environmentService.isExtensionDevelopment) {
265 266 267
				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 已提交
268
			}
269

270 271 272 273
			workspaceInitializationPayload = { id };
		}

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

276
	private async resolveSingleFolderWorkspaceInitializationPayload(folderUri: ISingleFolderWorkspaceIdentifier, resourceIdentityService: IResourceIdentityService): Promise<ISingleFolderWorkspaceInitializationPayload | undefined> {
277
		try {
278 279 280 281 282
			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 };
283 284 285 286
		} catch (error) {
			onUnexpectedError(error);
		}
		return;
B
Benjamin Pasero 已提交
287
	}
288

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

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

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

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

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

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

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

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

320 321
}

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

325
	return workbench.open();
326
}