web.main.ts 7.9 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';
38
import { FileUserDataService } from 'vs/workbench/services/userData/common/fileUserDataService';
S
Sandeep Somavarapu 已提交
39
import { IUserDataService } from 'vs/workbench/services/userData/common/userData';
S
Sandeep Somavarapu 已提交
40
import { UserDataFileSystemProvider } from 'vs/workbench//services/userData/common/userDataFileProvider';
41

42 43 44 45
class CodeRendererMain extends Disposable {

	private workbench: Workbench;

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

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

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

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

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

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

72 73
		// Workbench Lifecycle
		this._register(this.workbench.onShutdown(() => this.dispose()));
74

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

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

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

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

		// Environment
92
		const environmentService = new BrowserWorkbenchEnvironmentService(this.configuration);
B
Benjamin Pasero 已提交
93 94 95
		serviceCollection.set(IWorkbenchEnvironmentService, environmentService);

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

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

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

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

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

A
Alex Dima 已提交
115 116 117 118
		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 已提交
119

A
Alex Dima 已提交
120
			fileService.registerProvider(Schemas.vscodeRemote, remoteFileSystemProvider);
A
Alex Dima 已提交
121 122
		}

123 124 125
		// User Data Service
		const userDataService = this._register(new FileUserDataService(environmentService, fileService));
		serviceCollection.set(IUserDataService, userDataService);
S
Sandeep Somavarapu 已提交
126
		fileService.registerProvider(Schemas.userData, new UserDataFileSystemProvider(userDataService));
127

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

		await Promise.all([
131
			this.createWorkspaceService(payload, environmentService, fileService, userDataService, remoteAgentService, logService).then(service => {
132 133 134 135 136 137 138 139 140 141 142

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

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

				return service;
			}),
		]);

143 144
		return { serviceCollection, logService };
	}
145

146 147
	private async createWorkspaceService(payload: IWorkspaceInitializationPayload, environmentService: IWorkbenchEnvironmentService, fileService: FileService, userDataService: IUserDataService, remoteAgentService: IRemoteAgentService, logService: ILogService): Promise<WorkspaceService> {
		const workspaceService = new WorkspaceService({ remoteAuthority: this.configuration.remoteAuthority, configurationCache: new ConfigurationCache() }, fileService, userDataService, remoteAgentService);
148 149 150 151 152 153 154 155 156 157 158 159 160

		try {
			await workspaceService.initialize(payload);

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

			return workspaceService;
		}
	}

161
	private resolveWorkspaceInitializationPayload(): IWorkspaceInitializationPayload {
162 163 164

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

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

		return { id: 'empty-window' };
	}
175 176
}

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

180
	return renderer.open();
B
Benjamin Pasero 已提交
181
}