environmentService.ts 8.1 KB
Newer Older
1 2 3 4 5
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

6
import { Schemas } from 'vs/base/common/network';
7
import { joinPath } from 'vs/base/common/resources';
8 9
import { URI } from 'vs/base/common/uri';
import { generateUuid } from 'vs/base/common/uuid';
B
Benjamin Pasero 已提交
10
import { BACKUPS, IExtensionHostDebugParams } from 'vs/platform/environment/common/environment';
11
import { IPath, IWindowConfiguration } from 'vs/platform/windows/common/windows';
12 13
import { IWorkbenchEnvironmentService } from 'vs/workbench/services/environment/common/environmentService';
import { IWorkbenchConstructionOptions } from 'vs/workbench/workbench.web.api';
M
Matt Bierner 已提交
14
import product from 'vs/platform/product/common/product';
15
import { serializableToMap } from 'vs/base/common/map';
16
import { memoize } from 'vs/base/common/decorators';
17 18 19

export class BrowserWindowConfiguration implements IWindowConfiguration {

20 21 22 23 24
	constructor(
		private readonly options: IBrowserWorkbenchEnvironmentConstructionOptions,
		private readonly payload: Map<string, string> | undefined,
		private readonly environment: IWorkbenchEnvironmentService
	) { }
25

B
Benjamin Pasero 已提交
26 27 28
	@memoize
	get sessionId(): string { return generateUuid(); }

B
Benjamin Pasero 已提交
29
	@memoize
30 31
	get remoteAuthority(): string | undefined { return this.options.remoteAuthority; }

B
Benjamin Pasero 已提交
32 33 34 35 36 37
	@memoize
	get connectionToken(): string | undefined { return this.options.connectionToken || this.getCookieValue('vscode-tkn'); }

	@memoize
	get backupWorkspaceResource(): URI { return joinPath(this.environment.backupHome, this.options.workspaceId); }

38 39 40 41 42 43 44 45 46 47 48 49
	@memoize
	get filesToOpenOrCreate(): IPath[] | undefined {
		if (this.payload) {
			const fileToOpen = this.payload.get('openFile');
			if (fileToOpen) {
				return [{ fileUri: URI.parse(fileToOpen) }];
			}
		}

		return undefined;
	}

50
	// Currently unsupported in web but should look into support
B
Benjamin Pasero 已提交
51
	get filesToDiff(): IPath[] | undefined { return undefined; }
52 53
	highContrast = false;
	_ = [];
B
Benjamin Pasero 已提交
54 55 56 57 58 59

	private getCookieValue(name: string): string | undefined {
		const m = document.cookie.match('(^|[^;]+)\\s*' + name + '\\s*=\\s*([^;]+)'); // See https://stackoverflow.com/a/25490531

		return m ? m.pop() : undefined;
	}
60 61
}

S
SteVen Batten 已提交
62
interface IBrowserWorkbenchEnvironmentConstructionOptions extends IWorkbenchConstructionOptions {
S
Sandeep Somavarapu 已提交
63
	workspaceId: string;
64
	logsPath: URI;
S
Sandeep Somavarapu 已提交
65 66
}

B
Benjamin Pasero 已提交
67 68 69
interface IExtensionHostDebugEnvironment {
	params: IExtensionHostDebugParams;
	isExtensionDevelopment: boolean;
70
	extensionDevelopmentLocationURI?: URI[];
71
	extensionTestsLocationURI?: URI;
B
Benjamin Pasero 已提交
72 73
}

74
export class BrowserWorkbenchEnvironmentService implements IWorkbenchEnvironmentService {
75

76
	_serviceBrand: undefined;
77

78 79 80 81 82 83 84 85
	private _configuration: IWindowConfiguration | undefined = undefined;
	get configuration(): IWindowConfiguration {
		if (!this._configuration) {
			this._configuration = new BrowserWindowConfiguration(this.options, this.payload, this);
		}

		return this._configuration;
	}
86

87 88 89
	@memoize
	get isBuilt(): boolean { return !!product.commit; }

90 91
	@memoize
	get logsPath(): string { return this.options.logsPath.path; }
92

93 94
	@memoize
	get logFile(): URI { return joinPath(this.options.logsPath, 'window.log'); }
95

96 97
	@memoize
	get userRoamingDataHome(): URI { return URI.file('/User').with({ scheme: Schemas.userData }); }
98

99 100
	@memoize
	get settingsResource(): URI { return joinPath(this.userRoamingDataHome, 'settings.json'); }
101

B
Benjamin Pasero 已提交
102 103 104
	@memoize
	get argvResource(): URI { return joinPath(this.userRoamingDataHome, 'argv.json'); }

105 106 107
	@memoize
	get snippetsHome(): URI { return joinPath(this.userRoamingDataHome, 'snippets'); }

108
	@memoize
S
Sandeep Somavarapu 已提交
109
	get userDataSyncHome(): URI { return joinPath(this.userRoamingDataHome, 'sync'); }
110

111 112 113 114 115 116
	@memoize
	get userDataSyncLogResource(): URI { return joinPath(this.options.logsPath, 'userDataSync.log'); }

	@memoize
	get keybindingsResource(): URI { return joinPath(this.userRoamingDataHome, 'keybindings.json'); }

B
Benjamin Pasero 已提交
117
	@memoize
118 119
	get keyboardLayoutResource(): URI { return joinPath(this.userRoamingDataHome, 'keyboardLayout.json'); }

B
Benjamin Pasero 已提交
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
	@memoize
	get backupHome(): URI { return joinPath(this.userRoamingDataHome, BACKUPS); }

	@memoize
	get untitledWorkspacesHome(): URI { return joinPath(this.userRoamingDataHome, 'Workspaces'); }

	private _extensionHostDebugEnvironment: IExtensionHostDebugEnvironment | undefined = undefined;
	get debugExtensionHost(): IExtensionHostDebugParams {
		if (!this._extensionHostDebugEnvironment) {
			this._extensionHostDebugEnvironment = this.resolveExtensionHostDebugEnvironment();
		}

		return this._extensionHostDebugEnvironment.params;
	}

	get isExtensionDevelopment(): boolean {
		if (!this._extensionHostDebugEnvironment) {
			this._extensionHostDebugEnvironment = this.resolveExtensionHostDebugEnvironment();
		}

		return this._extensionHostDebugEnvironment.isExtensionDevelopment;
	}

143
	get extensionDevelopmentLocationURI(): URI[] | undefined {
B
Benjamin Pasero 已提交
144 145 146 147 148 149 150
		if (!this._extensionHostDebugEnvironment) {
			this._extensionHostDebugEnvironment = this.resolveExtensionHostDebugEnvironment();
		}

		return this._extensionHostDebugEnvironment.extensionDevelopmentLocationURI;
	}

151 152 153 154 155 156 157 158
	get extensionTestsLocationURI(): URI | undefined {
		if (!this._extensionHostDebugEnvironment) {
			this._extensionHostDebugEnvironment = this.resolveExtensionHostDebugEnvironment();
		}

		return this._extensionHostDebugEnvironment.extensionTestsLocationURI;
	}

B
Benjamin Pasero 已提交
159 160 161
	@memoize
	get webviewExternalEndpoint(): string {
		// TODO: get fallback from product.json
M
Matt Bierner 已提交
162
		return (this.options.webviewEndpoint || 'https://{{uuid}}.vscode-webview-test.com/{{commit}}').replace('{{commit}}', product.commit || '0d728c31ebdf03869d2687d9be0b017667c9ff37');
B
Benjamin Pasero 已提交
163 164 165 166 167 168 169 170 171 172 173 174
	}

	@memoize
	get webviewResourceRoot(): string {
		return `${this.webviewExternalEndpoint}/vscode-resource/{{resource}}`;
	}

	@memoize
	get webviewCspSource(): string {
		return this.webviewExternalEndpoint.replace('{{uuid}}', '*');
	}

175
	// Currently unsupported in web but should look into support
B
Benjamin Pasero 已提交
176 177 178 179 180
	get disableExtensions() { return false; }
	get extensionsPath(): string | undefined { return undefined; }
	get verbose(): boolean { return false; }
	get disableUpdates(): boolean { return false; }
	get logExtensionHostCommunication(): boolean { return false; }
181
	galleryMachineIdResource?: URI;
182

183 184 185 186 187 188 189
	private payload: Map<string, string> | undefined;

	constructor(readonly options: IBrowserWorkbenchEnvironmentConstructionOptions) {
		if (options.workspaceProvider && Array.isArray(options.workspaceProvider.payload)) {
			this.payload = serializableToMap(options.workspaceProvider.payload);
		}
	}
190

B
Benjamin Pasero 已提交
191 192 193 194 195 196 197
	private resolveExtensionHostDebugEnvironment(): IExtensionHostDebugEnvironment {
		const extensionHostDebugEnvironment: IExtensionHostDebugEnvironment = {
			params: {
				port: null,
				break: false
			},
			isExtensionDevelopment: false,
198
			extensionDevelopmentLocationURI: undefined
199
		};
M
Matt Bierner 已提交
200

201
		// Fill in selected extra environmental properties
202 203
		if (this.payload) {
			for (const [key, value] of this.payload) {
204 205
				switch (key) {
					case 'extensionDevelopmentPath':
B
Benjamin Pasero 已提交
206 207
						extensionHostDebugEnvironment.extensionDevelopmentLocationURI = [URI.parse(value)];
						extensionHostDebugEnvironment.isExtensionDevelopment = true;
208
						break;
A
Andre Weinand 已提交
209 210 211
					case 'extensionTestsPath':
						extensionHostDebugEnvironment.extensionTestsLocationURI = URI.parse(value);
						break;
212
					case 'debugId':
B
Benjamin Pasero 已提交
213
						extensionHostDebugEnvironment.params.debugId = value;
214 215
						break;
					case 'inspect-brk-extensions':
B
Benjamin Pasero 已提交
216
						extensionHostDebugEnvironment.params.port = parseInt(value);
217
						extensionHostDebugEnvironment.params.break = true;
218 219 220 221
						break;
				}
			}
		}
S
Sandeep Somavarapu 已提交
222

B
Benjamin Pasero 已提交
223 224
		return extensionHostDebugEnvironment;
	}
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255

	//#region TODO MOVE TO NODE LAYER

	args = { _: [] };

	mainIPCHandle!: string;
	sharedIPCHandle!: string;

	nodeCachedDataDir?: string;

	driverHandle?: string;
	driverVerbose!: boolean;

	installSourcePath!: string;

	builtinExtensionsPath!: string;

	globalStorageHome!: string;
	workspaceStorageHome!: string;

	backupWorkspacesPath!: string;

	machineSettingsResource!: URI;

	userHome!: string;
	userDataPath!: string;
	appRoot!: string;
	appSettingsHome!: URI;
	execPath!: string;

	//#endregion
256
}