web.main.ts 8.4 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 27 28 29 30 31
import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { onUnexpectedError } from 'vs/base/common/errors';
import { URI } from 'vs/base/common/uri';
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 37 38 39 40 41 42

interface IWindowConfiguration {
	settingsUri: URI;
	remoteAuthority: string;
	folderUri?: URI;
	workspaceUri?: URI;
}
43 44 45 46 47

class CodeRendererMain extends Disposable {

	private workbench: Workbench;

48 49 50 51
	constructor(private readonly configuration: IWindowConfiguration) {
		super();
	}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

		const remoteAgentService = this._register(new RemoteAgentService(environmentService, productService, remoteAuthorityResolverService, signService));
A
Alex Dima 已提交
107 108
		serviceCollection.set(IRemoteAgentService, remoteAgentService);

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

A
Alex Dima 已提交
113 114 115 116
		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 已提交
117
			fileService.registerProvider(Schemas.vscodeRemote, remoteFileSystemProvider);
A
Alex Dima 已提交
118 119
		}

120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
		const payload = await this.resolveWorkspaceInitializationPayload();

		await Promise.all([
			this.createWorkspaceService(payload, fileService, remoteAgentService, logService).then(service => {

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

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

				return service;
			}),
		]);

135 136
		return { serviceCollection, logService };
	}
137

138 139 140 141
	private createEnvironmentService(): IWorkbenchEnvironmentService {
		const environmentService = new SimpleWorkbenchEnvironmentService();
		environmentService.appRoot = '/web/';
		environmentService.args = { _: [] };
S
Sandeep Somavarapu 已提交
142
		environmentService.appSettingsHome = toResource('/web/settings');
S
Sandeep Somavarapu 已提交
143
		environmentService.settingsResource = this.configuration.settingsUri;
144 145 146 147 148 149 150 151 152
		environmentService.appKeybindingsPath = '/web/settings/keybindings.json';
		environmentService.logsPath = '/web/logs';
		environmentService.debugExtensionHost = {
			port: null,
			break: false
		};
		return environmentService;
	}

153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
	private async createWorkspaceService(payload: IWorkspaceInitializationPayload, fileService: FileService, remoteAgentService: IRemoteAgentService, logService: ILogService): Promise<WorkspaceService> {

		const workspaceService = new WorkspaceService({ userSettingsResource: this.configuration.settingsUri, remoteAuthority: this.configuration.remoteAuthority, configurationCache: new ConfigurationCache() }, new ConfigurationFileService(fileService), remoteAgentService);

		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(this.configuration.workspaceUri.toString()).toString(16), configPath: this.configuration.workspaceUri };
174 175 176 177
		}

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

		return { id: 'empty-window' };
	}
183 184
}

185 186 187 188 189
export interface IWindowConfigurationContents {
	settingsPath: string;
	folderPath?: string;
	workspacePath?: string;
}
190

191 192 193 194 195 196 197 198
export function main(windowConfigurationContents: IWindowConfigurationContents): Promise<void> {
	const windowConfiguration: IWindowConfiguration = {
		settingsUri: toResource(windowConfigurationContents.settingsPath),
		folderUri: windowConfigurationContents.folderPath ? toResource(windowConfigurationContents.folderPath) : undefined,
		workspaceUri: windowConfigurationContents.workspacePath ? toResource(windowConfigurationContents.workspacePath) : undefined,
		remoteAuthority: document.location.host
	};
	const renderer = new CodeRendererMain(windowConfiguration);
199 200
	return renderer.open();
}
201 202 203 204 205 206 207

function toResource(path: string): URI {
	return URI.from({
		scheme: Schemas.vscodeRemote,
		authority: document.location.host,
		path
	});
208
}