environmentService.ts 8.0 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
	@memoize
S
Sandeep Somavarapu 已提交
106
	get userDataSyncHome(): URI { return joinPath(this.userRoamingDataHome, 'sync'); }
107

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

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

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

B
Benjamin Pasero 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
	@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;
	}

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

		return this._extensionHostDebugEnvironment.extensionDevelopmentLocationURI;
	}

148 149 150 151 152 153 154 155
	get extensionTestsLocationURI(): URI | undefined {
		if (!this._extensionHostDebugEnvironment) {
			this._extensionHostDebugEnvironment = this.resolveExtensionHostDebugEnvironment();
		}

		return this._extensionHostDebugEnvironment.extensionTestsLocationURI;
	}

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

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

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

172
	// Currently unsupported in web but should look into support
B
Benjamin Pasero 已提交
173 174 175 176 177
	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; }
178
	galleryMachineIdResource?: URI;
179

180 181 182 183 184 185 186
	private payload: Map<string, string> | undefined;

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

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

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

B
Benjamin Pasero 已提交
220 221
		return extensionHostDebugEnvironment;
	}
222 223 224 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

	//#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
253
}