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

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

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

58
	constructor(private configuration: INativeWindowConfiguration) {
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);
E
Erich Gamma 已提交
80

B
Benjamin Pasero 已提交
81 82
		// Keyboard support
		KeyboardMapperFactory.INSTANCE._onKeyboardLayoutChanged();
M
Martin Aeschlimann 已提交
83
	}
84

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

90 91
		if (this.environmentService.configuration.workspace) {
			this.environmentService.configuration.workspace = reviveWorkspaceIdentifier(this.environmentService.configuration.workspace);
M
Martin Aeschlimann 已提交
92
		}
B
Benjamin Pasero 已提交
93

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

M
Martin Aeschlimann 已提交
106
		if (filesToWait) {
107
			filesToWait.waitMarkerFileUri = URI.revive(filesToWait.waitMarkerFileUri);
M
Martin Aeschlimann 已提交
108
		}
B
Benjamin Pasero 已提交
109
	}
B
Benjamin Pasero 已提交
110

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

114 115
		await domContentLoaded();
		mark('willStartWorkbench');
B
Benjamin Pasero 已提交
116

117
		// Create Workbench
118
		const workbench = new Workbench(document.body, services.serviceCollection, services.logService);
B
Benjamin Pasero 已提交
119

B
Benjamin Pasero 已提交
120 121
		// Listeners
		this.registerListeners(workbench, services.storageService);
122

123
		// Startup
124
		const instantiationService = workbench.startup();
B
Benjamin Pasero 已提交
125

126
		// Window
127
		this._register(instantiationService.createInstance(NativeWindow));
B
Benjamin Pasero 已提交
128

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

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

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

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

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

162
			workbench.layout();
163 164 165
		}
	}

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

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

174
		// Main Process
175 176
		const mainProcessService = this._register(new MainProcessService(this.configuration.windowId));
		serviceCollection.set(IMainProcessService, mainProcessService);
177

B
Benjamin Pasero 已提交
178
		// Environment
179
		serviceCollection.set(IWorkbenchEnvironmentService, this.environmentService);
B
Benjamin Pasero 已提交
180

181
		// Product
182 183
		const productService: IProductService = { _serviceBrand: undefined, ...product };
		serviceCollection.set(IProductService, productService);
184

B
Benjamin Pasero 已提交
185
		// Log
186
		const logService = this._register(new DesktopLogService(this.configuration.windowId, mainProcessService, this.environmentService));
B
Benjamin Pasero 已提交
187 188
		serviceCollection.set(ILogService, logService);

189 190 191
		// Remote
		const remoteAuthorityResolverService = new RemoteAuthorityResolverService();
		serviceCollection.set(IRemoteAuthorityResolverService, remoteAuthorityResolverService);
B
Benjamin Pasero 已提交
192

I
isidor 已提交
193 194 195 196
		// Sign
		const signService = new SignService();
		serviceCollection.set(ISignService, signService);

197
		// Remote Agent
A
Alex Dima 已提交
198
		const remoteAgentService = this._register(new RemoteAgentService(this.environmentService, productService, remoteAuthorityResolverService, signService, logService));
199 200
		serviceCollection.set(IRemoteAgentService, remoteAgentService);

201
		// Electron
202
		const electronService = new ElectronService(this.configuration.windowId, mainProcessService) as IElectronService;
203 204
		serviceCollection.set(IElectronService, electronService);

B
Benjamin Pasero 已提交
205
		// Files
B
Benjamin Pasero 已提交
206
		const fileService = this._register(new FileService(logService));
B
Benjamin Pasero 已提交
207 208
		serviceCollection.set(IFileService, fileService);

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

212
		// User Data Provider
213
		fileService.registerProvider(Schemas.userData, new FileUserDataProvider(this.environmentService.appSettingsHome, this.environmentService.backupHome, diskFileSystemProvider, this.environmentService, logService));
214

215 216
		const connection = remoteAgentService.getConnection();
		if (connection) {
217
			const remoteFileSystemProvider = this._register(new RemoteFileSystemProvider(remoteAgentService));
B
Benjamin Pasero 已提交
218
			fileService.registerProvider(Schemas.vscodeRemote, remoteFileSystemProvider);
219 220
		}

221 222 223 224
		const resourceIdentityService = this._register(new NativeResourceIdentityService());
		serviceCollection.set(IResourceIdentityService, resourceIdentityService);

		const payload = await this.resolveWorkspaceInitializationPayload(resourceIdentityService);
225 226

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

229 230
				// Workspace
				serviceCollection.set(IWorkspaceContextService, service);
B
Benjamin Pasero 已提交
231

232 233
				// Configuration
				serviceCollection.set(IConfigurationService, service);
B
Benjamin Pasero 已提交
234

235 236
				return service;
			}),
B
Benjamin Pasero 已提交
237

238
			this.createStorageService(payload, logService, mainProcessService).then(service => {
239 240 241 242 243 244

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

				return service;
			})
245 246 247
		]);

		return { serviceCollection, logService, storageService: services[1] };
248
	}
249

250
	private async resolveWorkspaceInitializationPayload(resourceIdentityService: IResourceIdentityService): Promise<IWorkspaceInitializationPayload> {
B
Benjamin Pasero 已提交
251 252

		// Multi-root workspace
253 254
		if (this.environmentService.configuration.workspace) {
			return this.environmentService.configuration.workspace;
255 256
		}

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

263 264 265
		// Fallback to empty workspace if we have no payload yet.
		if (!workspaceInitializationPayload) {
			let id: string;
266 267 268
			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) {
269 270 271
				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 已提交
272
			}
273

274 275 276 277
			workspaceInitializationPayload = { id };
		}

		return workspaceInitializationPayload;
B
Benjamin Pasero 已提交
278
	}
279

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

293 294
	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 已提交
295

296 297 298 299 300
		try {
			await workspaceService.initialize(payload);

			return workspaceService;
		} catch (error) {
B
Benjamin Pasero 已提交
301 302
			onUnexpectedError(error);
			logService.error(error);
303

B
Benjamin Pasero 已提交
304
			return workspaceService;
305
		}
B
Benjamin Pasero 已提交
306
	}
307

308
	private async createStorageService(payload: IWorkspaceInitializationPayload, logService: ILogService, mainProcessService: IMainProcessService): Promise<NativeStorageService> {
309
		const globalStorageDatabase = new GlobalStorageDatabaseChannelClient(mainProcessService.getChannel('storage'));
310
		const storageService = new NativeStorageService(globalStorageDatabase, logService, this.environmentService);
311

312 313 314 315 316
		try {
			await storageService.initialize(payload);

			return storageService;
		} catch (error) {
B
Benjamin Pasero 已提交
317 318
			onUnexpectedError(error);
			logService.error(error);
J
Joao Moreno 已提交
319

B
Benjamin Pasero 已提交
320
			return storageService;
321
		}
B
Benjamin Pasero 已提交
322
	}
323

324 325
}

326
export function main(configuration: INativeWindowConfiguration): Promise<void> {
327
	const workbench = new DesktopMain(configuration);
328

329
	return workbench.open();
330
}