web.main.ts 9.1 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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

				return service;
			})
159 160
		]);

161 162
		return { serviceCollection, logService };
	}
163

164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
	private async createStorageService(payload: IWorkspaceInitializationPayload, environmentService: IWorkbenchEnvironmentService, fileService: IFileService, logService: ILogService): Promise<IStorageService> {
		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 已提交
179 180
	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);
181 182 183 184 185 186 187 188 189 190 191 192 193

		try {
			await workspaceService.initialize(payload);

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

			return workspaceService;
		}
	}

194
	private resolveWorkspaceInitializationPayload(): IWorkspaceInitializationPayload {
195 196 197

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

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

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

	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;
	}
219 220
}

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

224
	return renderer.open();
225
}