environmentService.ts 8.5 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 12
import { IPath } from 'vs/platform/windows/common/windows';
import { IWorkbenchEnvironmentService, IEnvironmentConfiguration } from 'vs/workbench/services/environment/common/environmentService';
13
import { IWorkbenchConstructionOptions } from 'vs/workbench/workbench.web.api';
M
Matt Bierner 已提交
14
import product from 'vs/platform/product/common/product';
15
import { memoize } from 'vs/base/common/decorators';
16
import { onUnexpectedError } from 'vs/base/common/errors';
17

18
export class BrowserEnvironmentConfiguration implements IEnvironmentConfiguration {
19

20 21 22
	constructor(
		private readonly options: IBrowserWorkbenchEnvironmentConstructionOptions,
		private readonly payload: Map<string, string> | undefined,
23
		private readonly backupHome: URI
24
	) { }
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
	@memoize
33
	get backupWorkspaceResource(): URI { return joinPath(this.backupHome, this.options.workspaceId); }
B
Benjamin Pasero 已提交
34

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

		return undefined;
	}

47 48 49 50 51 52 53 54 55 56 57 58
	@memoize
	get filesToDiff(): IPath[] | undefined {
		if (this.payload) {
			const fileToDiffDetail = this.payload.get('diffFileDetail');
			const fileToDiffMaster = this.payload.get('diffFileMaster');
			if (fileToDiffDetail && fileToDiffMaster) {
				return [
					{ fileUri: URI.parse(fileToDiffDetail) },
					{ fileUri: URI.parse(fileToDiffMaster) }
				];
			}
		}
B
Benjamin Pasero 已提交
59

60 61
		return undefined;
	}
B
Benjamin Pasero 已提交
62

63 64
	get highContrast() {
		return false; // could investigate to detect high contrast theme automatically
B
Benjamin Pasero 已提交
65
	}
66 67
}

S
SteVen Batten 已提交
68
interface IBrowserWorkbenchEnvironmentConstructionOptions extends IWorkbenchConstructionOptions {
S
Sandeep Somavarapu 已提交
69
	workspaceId: string;
70
	logsPath: URI;
S
Sandeep Somavarapu 已提交
71 72
}

B
Benjamin Pasero 已提交
73 74 75
interface IExtensionHostDebugEnvironment {
	params: IExtensionHostDebugParams;
	isExtensionDevelopment: boolean;
76
	extensionDevelopmentLocationURI?: URI[];
77
	extensionTestsLocationURI?: URI;
78
	extensionEnabledProposedApi?: string[];
B
Benjamin Pasero 已提交
79 80
}

81
export class BrowserWorkbenchEnvironmentService implements IWorkbenchEnvironmentService {
82

83
	_serviceBrand: undefined;
84

85 86
	private _configuration: IEnvironmentConfiguration | undefined = undefined;
	get configuration(): IEnvironmentConfiguration {
87
		if (!this._configuration) {
88
			this._configuration = new BrowserEnvironmentConfiguration(this.options, this.payload, this.backupHome);
89 90 91 92
		}

		return this._configuration;
	}
93

94 95 96
	@memoize
	get isBuilt(): boolean { return !!product.commit; }

97 98
	@memoize
	get logsPath(): string { return this.options.logsPath.path; }
99

100 101
	get logLevel(): string | undefined { return this.payload?.get('logLevel'); }

102 103
	@memoize
	get logFile(): URI { return joinPath(this.options.logsPath, 'window.log'); }
104

105 106
	@memoize
	get userRoamingDataHome(): URI { return URI.file('/User').with({ scheme: Schemas.userData }); }
107

108 109
	@memoize
	get settingsResource(): URI { return joinPath(this.userRoamingDataHome, 'settings.json'); }
110

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

114 115 116
	@memoize
	get snippetsHome(): URI { return joinPath(this.userRoamingDataHome, 'snippets'); }

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

120 121 122
	@memoize
	get userDataSyncLogResource(): URI { return joinPath(this.options.logsPath, 'userDataSync.log'); }

S
Sandeep Somavarapu 已提交
123
	@memoize
124
	get sync(): 'on' | 'off' | undefined { return undefined; }
125

S
Sandeep Somavarapu 已提交
126 127
	@memoize
	get enableSyncByDefault(): boolean { return !!this.options.enableSyncByDefault; }
128

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

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

B
Benjamin Pasero 已提交
135 136 137 138 139 140
	@memoize
	get backupHome(): URI { return joinPath(this.userRoamingDataHome, BACKUPS); }

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

S
Sandeep Somavarapu 已提交
141 142 143
	@memoize
	get serviceMachineIdResource(): URI { return joinPath(this.userRoamingDataHome, 'machineid'); }

B
Benjamin Pasero 已提交
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
	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;
	}

161
	get extensionDevelopmentLocationURI(): URI[] | undefined {
B
Benjamin Pasero 已提交
162 163 164 165 166 167 168
		if (!this._extensionHostDebugEnvironment) {
			this._extensionHostDebugEnvironment = this.resolveExtensionHostDebugEnvironment();
		}

		return this._extensionHostDebugEnvironment.extensionDevelopmentLocationURI;
	}

169 170 171 172 173 174 175 176
	get extensionTestsLocationURI(): URI | undefined {
		if (!this._extensionHostDebugEnvironment) {
			this._extensionHostDebugEnvironment = this.resolveExtensionHostDebugEnvironment();
		}

		return this._extensionHostDebugEnvironment.extensionTestsLocationURI;
	}

177 178 179 180 181 182 183 184
	get extensionEnabledProposedApi(): string[] | undefined {
		if (!this._extensionHostDebugEnvironment) {
			this._extensionHostDebugEnvironment = this.resolveExtensionHostDebugEnvironment();
		}

		return this._extensionHostDebugEnvironment.extensionEnabledProposedApi;
	}

185 186
	get disableExtensions() { return this.payload?.get('disableExtensions') === 'true'; }

B
Benjamin Pasero 已提交
187 188
	@memoize
	get webviewExternalEndpoint(): string {
189
		// TODO@matt: get fallback from product.json
M
Matt Bierner 已提交
190
		return (this.options.webviewEndpoint || 'https://{{uuid}}.vscode-webview-test.com/{{commit}}').replace('{{commit}}', product.commit || '0d728c31ebdf03869d2687d9be0b017667c9ff37');
B
Benjamin Pasero 已提交
191 192 193 194 195 196 197 198 199 200 201 202
	}

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

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

203 204 205 206
	get disableTelemetry(): boolean { return false; }

	get verbose(): boolean { return this.payload?.get('verbose') === 'true'; }
	get logExtensionHostCommunication(): boolean { return this.payload?.get('logExtensionHostCommunication') === 'true'; }
207

208 209 210 211
	private payload: Map<string, string> | undefined;

	constructor(readonly options: IBrowserWorkbenchEnvironmentConstructionOptions) {
		if (options.workspaceProvider && Array.isArray(options.workspaceProvider.payload)) {
212 213 214 215 216
			try {
				this.payload = new Map(options.workspaceProvider.payload);
			} catch (error) {
				onUnexpectedError(error); // possible invalid payload for map
			}
217 218
		}
	}
219

B
Benjamin Pasero 已提交
220 221 222 223 224 225 226
	private resolveExtensionHostDebugEnvironment(): IExtensionHostDebugEnvironment {
		const extensionHostDebugEnvironment: IExtensionHostDebugEnvironment = {
			params: {
				port: null,
				break: false
			},
			isExtensionDevelopment: false,
227
			extensionDevelopmentLocationURI: undefined
228
		};
M
Matt Bierner 已提交
229

230
		// Fill in selected extra environmental properties
231 232
		if (this.payload) {
			for (const [key, value] of this.payload) {
233 234
				switch (key) {
					case 'extensionDevelopmentPath':
B
Benjamin Pasero 已提交
235 236
						extensionHostDebugEnvironment.extensionDevelopmentLocationURI = [URI.parse(value)];
						extensionHostDebugEnvironment.isExtensionDevelopment = true;
237
						break;
A
Andre Weinand 已提交
238 239 240
					case 'extensionTestsPath':
						extensionHostDebugEnvironment.extensionTestsLocationURI = URI.parse(value);
						break;
241
					case 'debugId':
B
Benjamin Pasero 已提交
242
						extensionHostDebugEnvironment.params.debugId = value;
243 244
						break;
					case 'inspect-brk-extensions':
B
Benjamin Pasero 已提交
245
						extensionHostDebugEnvironment.params.port = parseInt(value);
246
						extensionHostDebugEnvironment.params.break = true;
247
						break;
248 249 250
					case 'enableProposedApi':
						extensionHostDebugEnvironment.extensionEnabledProposedApi = [];
						break;
251 252 253
				}
			}
		}
S
Sandeep Somavarapu 已提交
254

B
Benjamin Pasero 已提交
255 256
		return extensionHostDebugEnvironment;
	}
257 258

	get skipReleaseNotes(): boolean { return false; }
259
}