web.main.ts 9.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

import { mark } from 'vs/base/common/performance';
import { domContentLoaded, addDisposableListener, EventType } from 'vs/base/browser/dom';
import { ServiceCollection } from 'vs/platform/instantiation/common/serviceCollection';
import { ILogService } from 'vs/platform/log/common/log';
import { Disposable } from 'vs/base/common/lifecycle';
S
Sandeep Somavarapu 已提交
11
import { SimpleLogService } from 'vs/workbench/browser/web.simpleservices';
12
import { BrowserWorkbenchEnvironmentService } from 'vs/workbench/services/environment/browser/environmentService';
13
import { Workbench } from 'vs/workbench/browser/workbench';
A
Alex Dima 已提交
14 15
import { IChannel } from 'vs/base/parts/ipc/common/ipc';
import { REMOTE_FILE_SYSTEM_CHANNEL_NAME, RemoteExtensionsFileSystemProvider } from 'vs/platform/remote/common/remoteAgentFileSystemChannel';
B
Benjamin Pasero 已提交
16 17
import { IWorkbenchEnvironmentService } from 'vs/workbench/services/environment/common/environmentService';
import { IProductService } from 'vs/platform/product/common/product';
A
Alex Dima 已提交
18
import { RemoteAgentService } from 'vs/workbench/services/remote/browser/remoteAgentServiceImpl';
B
Benjamin Pasero 已提交
19 20
import { RemoteAuthorityResolverService } from 'vs/platform/remote/browser/remoteAuthorityResolverService';
import { IRemoteAuthorityResolverService } from 'vs/platform/remote/common/remoteAuthorityResolver';
A
Alex Dima 已提交
21
import { IRemoteAgentService } from 'vs/workbench/services/remote/common/remoteAgentService';
22
import { IFileService, IFileSystemProvider } from 'vs/platform/files/common/files';
B
Benjamin Pasero 已提交
23
import { FileService } from 'vs/workbench/services/files/common/fileService';
A
Alex Dima 已提交
24
import { Schemas } from 'vs/base/common/network';
25 26 27
import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { onUnexpectedError } from 'vs/base/common/errors';
B
Benjamin Pasero 已提交
28
import { URI } from 'vs/base/common/uri';
29 30 31
import { IWorkspaceInitializationPayload } from 'vs/platform/workspaces/common/workspaces';
import { WorkspaceService } from 'vs/workbench/services/configuration/browser/configurationService';
import { ConfigurationCache } from 'vs/workbench/services/configuration/browser/configurationCache';
I
isidor 已提交
32 33
import { ISignService } from 'vs/platform/sign/common/sign';
import { SignService } from 'vs/platform/sign/browser/signService';
34
import { hash } from 'vs/base/common/hash';
B
Benjamin Pasero 已提交
35
import { IWorkbenchConstructionOptions } from 'vs/workbench/workbench.web.api';
S
Sandeep Somavarapu 已提交
36
import { ProductService } from 'vs/platform/product/browser/productService';
S
Sandeep Somavarapu 已提交
37
import { FileUserDataProvider } from 'vs/workbench/services/userData/common/fileUserDataProvider';
S
Sandeep Somavarapu 已提交
38
import { BACKUPS } from 'vs/platform/environment/common/environment';
39 40 41
import { joinPath } from 'vs/base/common/resources';
import { BrowserStorageService } from 'vs/platform/storage/browser/storageService';
import { IStorageService } from 'vs/platform/storage/common/storage';
42

43 44 45 46
class CodeRendererMain extends Disposable {

	private workbench: Workbench;

B
Benjamin Pasero 已提交
47 48
	constructor(
		private readonly domElement: HTMLElement,
49
		private readonly configuration: IWorkbenchConstructionOptions
B
Benjamin Pasero 已提交
50
	) {
51 52 53
		super();
	}

54
	async open(): Promise<void> {
55
		const services = await this.initServices();
56

57 58
		await domContentLoaded();
		mark('willStartWorkbench');
59

60 61
		// Create Workbench
		this.workbench = new Workbench(
B
Benjamin Pasero 已提交
62
			this.domElement,
63 64 65
			services.serviceCollection,
			services.logService
		);
66

67 68
		// Layout
		this._register(addDisposableListener(window, EventType.RESIZE, () => this.workbench.layout()));
69

70 71
		// Workbench Lifecycle
		this._register(this.workbench.onShutdown(() => this.dispose()));
72
		this._register(this.workbench.onWillShutdown(() => services.storageService.close()));
73

74 75
		// Startup
		this.workbench.startup();
76 77
	}

78
	private async initServices(): Promise<{ serviceCollection: ServiceCollection, logService: ILogService, storageService: BrowserStorageService }> {
79 80
		const serviceCollection = new ServiceCollection();

81 82 83 84 85
		// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
		// NOTE: DO NOT ADD ANY OTHER SERVICE INTO THE COLLECTION HERE.
		// CONTRIBUTE IT VIA WORKBENCH.MAIN.TS AND registerSingleton().
		// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

B
Benjamin Pasero 已提交
86 87 88 89
		// Log
		const logService = new SimpleLogService();
		serviceCollection.set(ILogService, logService);

S
Sandeep Somavarapu 已提交
90 91
		const payload = await this.resolveWorkspaceInitializationPayload();

B
Benjamin Pasero 已提交
92
		// Environment
S
Sandeep Somavarapu 已提交
93 94 95 96 97
		const environmentService = new BrowserWorkbenchEnvironmentService({
			workspaceId: payload.id,
			remoteAuthority: this.configuration.remoteAuthority,
			webviewEndpoint: this.configuration.webviewEndpoint
		});
B
Benjamin Pasero 已提交
98 99 100
		serviceCollection.set(IWorkbenchEnvironmentService, environmentService);

		// Product
S
Sandeep Somavarapu 已提交
101
		const productService = new ProductService();
B
Benjamin Pasero 已提交
102 103 104 105 106 107
		serviceCollection.set(IProductService, productService);

		// Remote
		const remoteAuthorityResolverService = new RemoteAuthorityResolverService();
		serviceCollection.set(IRemoteAuthorityResolverService, remoteAuthorityResolverService);

B
Benjamin Pasero 已提交
108
		// Signing
I
isidor 已提交
109 110 111
		const signService = new SignService();
		serviceCollection.set(ISignService, signService);

B
Benjamin Pasero 已提交
112
		// Remote Agent
I
isidor 已提交
113
		const remoteAgentService = this._register(new RemoteAgentService(environmentService, productService, remoteAuthorityResolverService, signService));
A
Alex Dima 已提交
114 115
		serviceCollection.set(IRemoteAgentService, remoteAgentService);

B
Benjamin Pasero 已提交
116
		// Files
B
Benjamin Pasero 已提交
117
		const fileService = this._register(new FileService(logService));
B
Benjamin Pasero 已提交
118 119
		serviceCollection.set(IFileService, fileService);

120
		let userDataProvider: IFileSystemProvider | undefined = this.configuration.userDataProvider;
A
Alex Dima 已提交
121 122 123 124
		const connection = remoteAgentService.getConnection();
		if (connection) {
			const channel = connection.getChannel<IChannel>(REMOTE_FILE_SYSTEM_CHANNEL_NAME);
			const remoteFileSystemProvider = this._register(new RemoteExtensionsFileSystemProvider(channel, remoteAgentService.getEnvironment()));
B
Benjamin Pasero 已提交
125

A
Alex Dima 已提交
126
			fileService.registerProvider(Schemas.vscodeRemote, remoteFileSystemProvider);
127

S
Sandeep Somavarapu 已提交
128 129 130
			if (!userDataProvider) {
				const remoteUserDataUri = this.getRemoteUserDataUri();
				if (remoteUserDataUri) {
S
Sandeep Somavarapu 已提交
131
					userDataProvider = this._register(new FileUserDataProvider(remoteUserDataUri, joinPath(remoteUserDataUri, BACKUPS), remoteFileSystemProvider, environmentService));
S
Sandeep Somavarapu 已提交
132
				}
133
			}
A
Alex Dima 已提交
134 135
		}

S
Sandeep Somavarapu 已提交
136
		// User Data Provider
137 138 139
		if (userDataProvider) {
			fileService.registerProvider(Schemas.userData, userDataProvider);
		}
140

141
		const services = await Promise.all([
S
Sandeep Somavarapu 已提交
142
			this.createWorkspaceService(payload, environmentService, fileService, remoteAgentService, logService).then(service => {
143 144 145 146 147 148 149 150 151

				// Workspace
				serviceCollection.set(IWorkspaceContextService, service);

				// Configuration
				serviceCollection.set(IConfigurationService, service);

				return service;
			}),
152 153 154 155 156 157 158 159

			this.createStorageService(payload, environmentService, fileService, logService).then(service => {

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

				return service;
			})
160 161
		]);

162
		return { serviceCollection, logService, storageService: services[1] };
163
	}
164

165
	private async createStorageService(payload: IWorkspaceInitializationPayload, environmentService: IWorkbenchEnvironmentService, fileService: IFileService, logService: ILogService): Promise<BrowserStorageService> {
166 167 168 169 170 171 172 173 174 175 176 177 178 179
		const storageService = new BrowserStorageService(environmentService, fileService);

		try {
			await storageService.initialize(payload);

			return storageService;
		} catch (error) {
			onUnexpectedError(error);
			logService.error(error);

			return storageService;
		}
	}

S
Sandeep Somavarapu 已提交
180 181
	private async createWorkspaceService(payload: IWorkspaceInitializationPayload, environmentService: IWorkbenchEnvironmentService, fileService: FileService, remoteAgentService: IRemoteAgentService, logService: ILogService): Promise<WorkspaceService> {
		const workspaceService = new WorkspaceService({ remoteAuthority: this.configuration.remoteAuthority, configurationCache: new ConfigurationCache() }, environmentService, fileService, remoteAgentService);
182 183 184 185 186 187 188 189 190 191 192 193 194

		try {
			await workspaceService.initialize(payload);

			return workspaceService;
		} catch (error) {
			onUnexpectedError(error);
			logService.error(error);

			return workspaceService;
		}
	}

195
	private resolveWorkspaceInitializationPayload(): IWorkspaceInitializationPayload {
196 197 198

		// Multi-root workspace
		if (this.configuration.workspaceUri) {
199
			return { id: hash(URI.revive(this.configuration.workspaceUri).toString()).toString(16), configPath: URI.revive(this.configuration.workspaceUri) };
200 201 202 203
		}

		// Single-folder workspace
		if (this.configuration.folderUri) {
204
			return { id: hash(URI.revive(this.configuration.folderUri).toString()).toString(16), folder: URI.revive(this.configuration.folderUri) };
205 206 207 208
		}

		return { id: 'empty-window' };
	}
S
Sandeep Somavarapu 已提交
209 210 211 212 213 214 215 216 217 218 219

	private getRemoteUserDataUri(): URI | null {
		const element = document.getElementById('vscode-remote-user-data-uri');
		if (element) {
			const remoteUserDataPath = element.getAttribute('data-settings');
			if (remoteUserDataPath) {
				return joinPath(URI.revive(JSON.parse(remoteUserDataPath)), 'User');
			}
		}
		return null;
	}
220 221
}

B
Benjamin Pasero 已提交
222
export function main(domElement: HTMLElement, options: IWorkbenchConstructionOptions): Promise<void> {
B
Benjamin Pasero 已提交
223
	const renderer = new CodeRendererMain(domElement, options);
224

225
	return renderer.open();
226
}