web.main.ts 8.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 39
import { joinPath } from 'vs/base/common/resources';
import { BACKUPS } from 'vs/platform/environment/common/environment';
40

41 42 43 44
class CodeRendererMain extends Disposable {

	private workbench: Workbench;

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

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

55 56
		await domContentLoaded();
		mark('willStartWorkbench');
57

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

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

68 69
		// Workbench Lifecycle
		this._register(this.workbench.onShutdown(() => this.dispose()));
70

71 72
		// Startup
		this.workbench.startup();
73 74
	}

75
	private async initServices(): Promise<{ serviceCollection: ServiceCollection, logService: ILogService }> {
76 77
		const serviceCollection = new ServiceCollection();

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

B
Benjamin Pasero 已提交
83 84 85 86
		// Log
		const logService = new SimpleLogService();
		serviceCollection.set(ILogService, logService);

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

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

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

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

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

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

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

117
		let userDataProvider: IFileSystemProvider | undefined = this.configuration.userDataProvider;
A
Alex Dima 已提交
118 119 120 121
		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 已提交
122

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

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

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

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

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

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

				return service;
			}),
		]);

151 152
		return { serviceCollection, logService };
	}
153

S
Sandeep Somavarapu 已提交
154 155
	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);
156 157 158 159 160 161 162 163 164 165 166 167 168

		try {
			await workspaceService.initialize(payload);

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

			return workspaceService;
		}
	}

169
	private resolveWorkspaceInitializationPayload(): IWorkspaceInitializationPayload {
170 171 172

		// Multi-root workspace
		if (this.configuration.workspaceUri) {
173
			return { id: hash(URI.revive(this.configuration.workspaceUri).toString()).toString(16), configPath: URI.revive(this.configuration.workspaceUri) };
174 175 176 177
		}

		// Single-folder workspace
		if (this.configuration.folderUri) {
178
			return { id: hash(URI.revive(this.configuration.folderUri).toString()).toString(16), folder: URI.revive(this.configuration.folderUri) };
179 180 181 182
		}

		return { id: 'empty-window' };
	}
S
Sandeep Somavarapu 已提交
183 184 185 186 187 188 189 190 191 192 193

	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;
	}
194 195
}

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

199
	return renderer.open();
200
}