web.main.ts 8.6 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';
11
import { SimpleLogService, SimpleProductService, SimpleWorkbenchEnvironmentService } from 'vs/workbench/browser/web.simpleservices';
12
import { Workbench } from 'vs/workbench/browser/workbench';
A
Alex Dima 已提交
13 14
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 已提交
15 16
import { IWorkbenchEnvironmentService } from 'vs/workbench/services/environment/common/environmentService';
import { IProductService } from 'vs/platform/product/common/product';
A
Alex Dima 已提交
17
import { RemoteAgentService } from 'vs/workbench/services/remote/browser/remoteAgentServiceImpl';
B
Benjamin Pasero 已提交
18 19
import { RemoteAuthorityResolverService } from 'vs/platform/remote/browser/remoteAuthorityResolverService';
import { IRemoteAuthorityResolverService } from 'vs/platform/remote/common/remoteAuthorityResolver';
A
Alex Dima 已提交
20
import { IRemoteAgentService } from 'vs/workbench/services/remote/common/remoteAgentService';
B
Benjamin Pasero 已提交
21
import { IFileService } from 'vs/platform/files/common/files';
B
Benjamin Pasero 已提交
22
import { FileService } from 'vs/workbench/services/files/common/fileService';
A
Alex Dima 已提交
23
import { Schemas } from 'vs/base/common/network';
24 25 26
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 已提交
27
import { URI, UriComponents } from 'vs/base/common/uri';
28 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';
import { ConfigurationFileService } from 'vs/workbench/services/configuration/common/configuration';
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';
36
import { joinPath } from 'vs/base/common/resources';
37 38 39

interface IWindowConfiguration {
	remoteAuthority: string;
B
Benjamin Pasero 已提交
40 41

	userDataUri: URI;
42 43 44
	folderUri?: URI;
	workspaceUri?: URI;
}
45 46 47 48 49

class CodeRendererMain extends Disposable {

	private workbench: Workbench;

50 51 52 53
	constructor(private readonly configuration: IWindowConfiguration) {
		super();
	}

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

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

60 61 62 63 64 65
		// Create Workbench
		this.workbench = new Workbench(
			document.body,
			services.serviceCollection,
			services.logService
		);
66

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

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

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

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

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

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

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

		// Environment
93
		const environmentService = this.createEnvironmentService();
B
Benjamin Pasero 已提交
94 95 96 97 98 99 100 101 102 103
		serviceCollection.set(IWorkbenchEnvironmentService, environmentService);

		// Product
		const productService = new SimpleProductService();
		serviceCollection.set(IProductService, productService);

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

I
isidor 已提交
104 105 106 107 108
		// Sign
		const signService = new SignService();
		serviceCollection.set(ISignService, signService);

		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()));
A
Alex Dima 已提交
119
			fileService.registerProvider(Schemas.vscodeRemote, remoteFileSystemProvider);
A
Alex Dima 已提交
120 121
		}

122 123 124
		const payload = await this.resolveWorkspaceInitializationPayload();

		await Promise.all([
125
			this.createWorkspaceService(payload, environmentService, fileService, remoteAgentService, logService).then(service => {
126 127 128 129 130 131 132 133 134 135 136

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

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

				return service;
			}),
		]);

137 138
		return { serviceCollection, logService };
	}
139

140 141 142 143
	private createEnvironmentService(): IWorkbenchEnvironmentService {
		const environmentService = new SimpleWorkbenchEnvironmentService();
		environmentService.appRoot = '/web/';
		environmentService.args = { _: [] };
144
		environmentService.appSettingsHome = joinPath(this.configuration.userDataUri, 'User');
145 146
		environmentService.settingsResource = joinPath(environmentService.appSettingsHome, 'settings.json');
		environmentService.keybindingsResource = joinPath(environmentService.appSettingsHome, 'keybindings.json');
P
Peng Lyu 已提交
147
		environmentService.keyboardLayoutResource = joinPath(environmentService.appSettingsHome, 'keyboardLayout.json');
148 149 150 151 152
		environmentService.logsPath = '/web/logs';
		environmentService.debugExtensionHost = {
			port: null,
			break: false
		};
153

154 155 156
		return environmentService;
	}

157 158
	private async createWorkspaceService(payload: IWorkspaceInitializationPayload, environmentService: IWorkbenchEnvironmentService, fileService: FileService, remoteAgentService: IRemoteAgentService, logService: ILogService): Promise<WorkspaceService> {
		const workspaceService = new WorkspaceService({ userSettingsResource: environmentService.settingsResource, remoteAuthority: this.configuration.remoteAuthority, configurationCache: new ConfigurationCache() }, new ConfigurationFileService(fileService), remoteAgentService);
159 160 161 162 163 164 165 166 167 168 169 170 171

		try {
			await workspaceService.initialize(payload);

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

			return workspaceService;
		}
	}

172
	private resolveWorkspaceInitializationPayload(): IWorkspaceInitializationPayload {
173 174 175

		// Multi-root workspace
		if (this.configuration.workspaceUri) {
176
			return { id: hash(this.configuration.workspaceUri.toString()).toString(16), configPath: this.configuration.workspaceUri };
177 178 179 180
		}

		// Single-folder workspace
		if (this.configuration.folderUri) {
181
			return { id: hash(this.configuration.folderUri.toString()).toString(16), folder: this.configuration.folderUri };
182 183 184 185
		}

		return { id: 'empty-window' };
	}
186 187
}

188
export interface IWindowConfigurationContents {
B
Benjamin Pasero 已提交
189 190 191 192
	authority: string;
	userDataUri: UriComponents;
	folderUri?: UriComponents;
	workspaceUri?: UriComponents;
193
}
194

195 196
export function main(windowConfigurationContents: IWindowConfigurationContents): Promise<void> {
	const windowConfiguration: IWindowConfiguration = {
B
Benjamin Pasero 已提交
197 198 199 200
		userDataUri: URI.revive(windowConfigurationContents.userDataUri),
		remoteAuthority: windowConfigurationContents.authority,
		folderUri: windowConfigurationContents.folderUri ? URI.revive(windowConfigurationContents.folderUri) : undefined,
		workspaceUri: windowConfigurationContents.workspaceUri ? URI.revive(windowConfigurationContents.workspaceUri) : undefined
201
	};
202

203
	const renderer = new CodeRendererMain(windowConfiguration);
204
	return renderer.open();
B
Benjamin Pasero 已提交
205
}