web.main.ts 8.8 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';
B
Benjamin Pasero 已提交
22
import { IFileService } 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';
32
import { WebResources } from 'vs/workbench/browser/web.resources';
I
isidor 已提交
33 34
import { ISignService } from 'vs/platform/sign/common/sign';
import { SignService } from 'vs/platform/sign/browser/signService';
35
import { hash } from 'vs/base/common/hash';
B
Benjamin Pasero 已提交
36
import { IWorkbenchConstructionOptions } from 'vs/workbench/workbench.web.api';
S
Sandeep Somavarapu 已提交
37
import { ProductService } from 'vs/platform/product/browser/productService';
S
Sandeep Somavarapu 已提交
38
import { FileUserDataProvider } from 'vs/workbench/services/userData/common/fileUserDataProvider';
B
Benjamin Pasero 已提交
39
import { UserDataFileSystemProvider } from 'vs/workbench/services/userData/common/userDataFileSystemProvider';
S
Sandeep Somavarapu 已提交
40 41 42
import { joinPath, dirname } from 'vs/base/common/resources';
import { InMemoryUserDataProvider } from 'vs/workbench/services/userData/common/inMemoryUserDataProvider';
import { IUserDataProvider } from 'vs/workbench/services/userData/common/userData';
43

44 45 46 47
class CodeRendererMain extends Disposable {

	private workbench: Workbench;

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

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

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

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

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

71 72 73
		// Resource Loading
		this._register(new WebResources(<IFileService>services.serviceCollection.get(IFileService)));

74 75
		// Workbench Lifecycle
		this._register(this.workbench.onShutdown(() => this.dispose()));
76

77 78
		// Startup
		this.workbench.startup();
79 80
	}

81
	private async initServices(): Promise<{ serviceCollection: ServiceCollection, logService: ILogService }> {
82 83
		const serviceCollection = new ServiceCollection();

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

B
Benjamin Pasero 已提交
89 90 91 92 93
		// Log
		const logService = new SimpleLogService();
		serviceCollection.set(ILogService, logService);

		// Environment
S
Sandeep Somavarapu 已提交
94 95
		const remoteUserDataUri = this.getRemoteUserDataUri();
		const environmentService = new BrowserWorkbenchEnvironmentService(this.configuration, remoteUserDataUri);
B
Benjamin Pasero 已提交
96 97 98
		serviceCollection.set(IWorkbenchEnvironmentService, environmentService);

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

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

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

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

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

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);
A
Alex Dima 已提交
124 125
		}

S
Sandeep Somavarapu 已提交
126 127
		// User Data Provider
		fileService.registerProvider(Schemas.userData, new UserDataFileSystemProvider(dirname(environmentService.settingsResource), this.getUserDataPovider(fileService, remoteUserDataUri)));
128

129 130 131
		const payload = await this.resolveWorkspaceInitializationPayload();

		await Promise.all([
S
Sandeep Somavarapu 已提交
132
			this.createWorkspaceService(payload, environmentService, fileService, remoteAgentService, logService).then(service => {
133 134 135 136 137 138 139 140 141 142 143

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

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

				return service;
			}),
		]);

144 145
		return { serviceCollection, logService };
	}
146

S
Sandeep Somavarapu 已提交
147 148
	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);
149 150 151 152 153 154 155 156 157 158 159 160 161

		try {
			await workspaceService.initialize(payload);

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

			return workspaceService;
		}
	}

162
	private resolveWorkspaceInitializationPayload(): IWorkspaceInitializationPayload {
163 164 165

		// Multi-root workspace
		if (this.configuration.workspaceUri) {
166
			return { id: hash(URI.revive(this.configuration.workspaceUri).toString()).toString(16), configPath: URI.revive(this.configuration.workspaceUri) };
167 168 169 170
		}

		// Single-folder workspace
		if (this.configuration.folderUri) {
171
			return { id: hash(URI.revive(this.configuration.folderUri).toString()).toString(16), folder: URI.revive(this.configuration.folderUri) };
172 173 174 175
		}

		return { id: 'empty-window' };
	}
S
Sandeep Somavarapu 已提交
176

S
Sandeep Somavarapu 已提交
177
	private getUserDataPovider(fileService: IFileService, remoteUserDataUri: URI | null): IUserDataProvider {
S
Sandeep Somavarapu 已提交
178
		if (this.configuration.userDataProvider) {
S
Sandeep Somavarapu 已提交
179 180 181
			return this.configuration.userDataProvider;
		} else if (this.configuration.remoteAuthority && remoteUserDataUri) {
			return this._register(new FileUserDataProvider(remoteUserDataUri, fileService));
S
Sandeep Somavarapu 已提交
182
		}
S
Sandeep Somavarapu 已提交
183
		return this._register(new InMemoryUserDataProvider());
S
Sandeep Somavarapu 已提交
184 185 186 187 188 189 190 191 192 193 194 195
	}

	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;
	}
196 197
}

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

201
	return renderer.open();
B
Benjamin Pasero 已提交
202
}