Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/api/browser/mainThreadExtensionService.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { Action } from '../../../base/common/actions.js';
7
import { VSBuffer } from '../../../base/common/buffer.js';
8
import { CancellationToken } from '../../../base/common/cancellation.js';
9
import { SerializedError, transformErrorFromSerialization } from '../../../base/common/errors.js';
10
import { FileAccess } from '../../../base/common/network.js';
11
import Severity from '../../../base/common/severity.js';
12
import { URI, UriComponents } from '../../../base/common/uri.js';
13
import { localize } from '../../../nls.js';
14
import { ICommandService } from '../../../platform/commands/common/commands.js';
15
import { ILocalExtension } from '../../../platform/extensionManagement/common/extensionManagement.js';
16
import { areSameExtensions } from '../../../platform/extensionManagement/common/extensionManagementUtil.js';
17
import { ExtensionIdentifier, IExtensionDescription } from '../../../platform/extensions/common/extensions.js';
18
import { INotificationService } from '../../../platform/notification/common/notification.js';
19
import { IRemoteConnectionData, ManagedRemoteConnection, RemoteConnection, RemoteConnectionType, ResolvedAuthority, WebSocketRemoteConnection } from '../../../platform/remote/common/remoteAuthorityResolver.js';
20
import { ExtHostContext, ExtHostExtensionServiceShape, MainContext, MainThreadExtensionServiceShape } from '../common/extHost.protocol.js';
21
import { IExtension, IExtensionsWorkbenchService } from '../../contrib/extensions/common/extensions.js';
22
import { IWorkbenchEnvironmentService } from '../../services/environment/common/environmentService.js';
23
import { EnablementState, IWorkbenchExtensionEnablementService } from '../../services/extensionManagement/common/extensionManagement.js';
24
import { ExtensionHostKind } from '../../services/extensions/common/extensionHostKind.js';
25
import { IExtensionDescriptionDelta } from '../../services/extensions/common/extensionHostProtocol.js';
26
import { IExtensionHostProxy, IResolveAuthorityResult } from '../../services/extensions/common/extensionHostProxy.js';
27
import { ActivationKind, ExtensionActivationReason, IExtensionService, IInternalExtensionService, MissingExtensionDependency } from '../../services/extensions/common/extensions.js';
28
import { extHostNamedCustomer, IExtHostContext, IInternalExtHostContext } from '../../services/extensions/common/extHostCustomers.js';
29
import { Dto } from '../../services/extensions/common/proxyIdentifier.js';
30
import { IHostService } from '../../services/host/browser/host.js';
31
import { ITimerService } from '../../services/timer/browser/timerService.js';
32
33
@extHostNamedCustomer(MainContext.MainThreadExtensionService)
34
export class MainThreadExtensionService implements MainThreadExtensionServiceShape {
35
36
private readonly _extensionHostKind: ExtensionHostKind;
37
private readonly _internalExtensionService: IInternalExtensionService;
38
39
constructor(
40
extHostContext: IExtHostContext,
41
@IExtensionService private readonly _extensionService: IExtensionService,
42
@INotificationService private readonly _notificationService: INotificationService,
43
@IExtensionsWorkbenchService private readonly _extensionsWorkbenchService: IExtensionsWorkbenchService,
44
@IHostService private readonly _hostService: IHostService,
45
@IWorkbenchExtensionEnablementService private readonly _extensionEnablementService: IWorkbenchExtensionEnablementService,
46
@ITimerService private readonly _timerService: ITimerService,
47
@ICommandService private readonly _commandService: ICommandService,
48
@IWorkbenchEnvironmentService protected readonly _environmentService: IWorkbenchEnvironmentService,
49
) {
50
this._extensionHostKind = extHostContext.extensionHostKind;
51
52
const internalExtHostContext = (<IInternalExtHostContext>extHostContext);
53
this._internalExtensionService = internalExtHostContext.internalExtensionService;
54
internalExtHostContext._setExtensionHostProxy(
55
new ExtensionHostProxy(extHostContext.getProxy(ExtHostContext.ExtHostExtensionService))
56
);
57
internalExtHostContext._setAllMainProxyIdentifiers(Object.keys(MainContext).map((key) => (<any>MainContext)[key]));
58
}
59
60
public dispose(): void {
61
}
62
63
$getExtension(extensionId: string) {
64
return this._extensionService.getExtension(extensionId);
65
}
66
$activateExtension(extensionId: ExtensionIdentifier, reason: ExtensionActivationReason): Promise<void> {
67
return this._internalExtensionService._activateById(extensionId, reason);
68
}
69
async $onWillActivateExtension(extensionId: ExtensionIdentifier): Promise<void> {
70
this._internalExtensionService._onWillActivateExtension(extensionId);
71
}
72
$onDidActivateExtension(extensionId: ExtensionIdentifier, codeLoadingTime: number, activateCallTime: number, activateResolvedTime: number, activationReason: ExtensionActivationReason): void {
73
this._internalExtensionService._onDidActivateExtension(extensionId, codeLoadingTime, activateCallTime, activateResolvedTime, activationReason);
74
}
75
$onExtensionRuntimeError(extensionId: ExtensionIdentifier, data: SerializedError): void {
76
const error = transformErrorFromSerialization(data);
77
this._internalExtensionService._onExtensionRuntimeError(extensionId, error);
78
console.error(`[${extensionId.value}]${error.message}`);
79
console.error(error.stack);
80
}
81
async $onExtensionActivationError(extensionId: ExtensionIdentifier, data: SerializedError, missingExtensionDependency: MissingExtensionDependency | null): Promise<void> {
82
const error = transformErrorFromSerialization(data);
83
84
this._internalExtensionService._onDidActivateExtensionError(extensionId, error);
85
86
if (missingExtensionDependency) {
87
const extension = await this._extensionService.getExtension(extensionId.value);
88
if (extension) {
89
const local = await this._extensionsWorkbenchService.queryLocal();
90
const installedDependency = local.find(i => areSameExtensions(i.identifier, { id: missingExtensionDependency.dependency }));
91
if (installedDependency?.local) {
92
await this._handleMissingInstalledDependency(extension, installedDependency.local);
93
return;
94
} else {
95
await this._handleMissingNotInstalledDependency(extension, missingExtensionDependency.dependency);
96
return;
97
}
98
}
99
}
100
101
const isDev = !this._environmentService.isBuilt || this._environmentService.isExtensionDevelopment;
102
if (isDev) {
103
this._notificationService.error(error);
104
return;
105
}
106
107
console.error(error.message);
108
}
109
110
private async _handleMissingInstalledDependency(extension: IExtensionDescription, missingInstalledDependency: ILocalExtension): Promise<void> {
111
const extName = extension.displayName || extension.name;
112
if (this._extensionEnablementService.isEnabled(missingInstalledDependency)) {
113
this._notificationService.notify({
114
severity: Severity.Error,
115
message: localize('reload window', "Cannot activate the '{0}' extension because it depends on the '{1}' extension, which is not loaded. Would you like to reload the window to load the extension?", extName, missingInstalledDependency.manifest.displayName || missingInstalledDependency.manifest.name),
116
actions: {
117
primary: [new Action('reload', localize('reload', "Reload Window"), '', true, () => this._hostService.reload())]
118
}
119
});
120
} else {
121
const enablementState = this._extensionEnablementService.getEnablementState(missingInstalledDependency);
122
if (enablementState === EnablementState.DisabledByVirtualWorkspace) {
123
this._notificationService.notify({
124
severity: Severity.Error,
125
message: localize('notSupportedInWorkspace', "Cannot activate the '{0}' extension because it depends on the '{1}' extension which is not supported in the current workspace", extName, missingInstalledDependency.manifest.displayName || missingInstalledDependency.manifest.name),
126
});
127
} else if (enablementState === EnablementState.DisabledByTrustRequirement) {
128
this._notificationService.notify({
129
severity: Severity.Error,
130
message: localize('restrictedMode', "Cannot activate the '{0}' extension because it depends on the '{1}' extension which is not supported in Restricted Mode", extName, missingInstalledDependency.manifest.displayName || missingInstalledDependency.manifest.name),
131
actions: {
132
primary: [new Action('manageWorkspaceTrust', localize('manageWorkspaceTrust', "Manage Workspace Trust"), '', true,
133
() => this._commandService.executeCommand('workbench.trust.manage'))]
134
}
135
});
136
} else if (this._extensionEnablementService.canChangeEnablement(missingInstalledDependency)) {
137
this._notificationService.notify({
138
severity: Severity.Error,
139
message: localize('disabledDep', "Cannot activate the '{0}' extension because it depends on the '{1}' extension which is disabled. Would you like to enable the extension and reload the window?", extName, missingInstalledDependency.manifest.displayName || missingInstalledDependency.manifest.name),
140
actions: {
141
primary: [new Action('enable', localize('enable dep', "Enable and Reload"), '', true,
142
() => this._extensionEnablementService.setEnablement([missingInstalledDependency], enablementState === EnablementState.DisabledGlobally ? EnablementState.EnabledGlobally : EnablementState.EnabledWorkspace)
143
.then(() => this._hostService.reload(), e => this._notificationService.error(e)))]
144
}
145
});
146
} else {
147
this._notificationService.notify({
148
severity: Severity.Error,
149
message: localize('disabledDepNoAction', "Cannot activate the '{0}' extension because it depends on the '{1}' extension which is disabled.", extName, missingInstalledDependency.manifest.displayName || missingInstalledDependency.manifest.name),
150
});
151
}
152
}
153
}
154
155
private async _handleMissingNotInstalledDependency(extension: IExtensionDescription, missingDependency: string): Promise<void> {
156
const extName = extension.displayName || extension.name;
157
let dependencyExtension: IExtension | null = null;
158
try {
159
dependencyExtension = (await this._extensionsWorkbenchService.getExtensions([{ id: missingDependency }], CancellationToken.None))[0];
160
} catch (err) {
161
}
162
if (dependencyExtension) {
163
this._notificationService.notify({
164
severity: Severity.Error,
165
message: localize('uninstalledDep', "Cannot activate the '{0}' extension because it depends on the '{1}' extension from '{2}', which is not installed. Would you like to install the extension and reload the window?", extName, dependencyExtension.displayName, dependencyExtension.publisherDisplayName),
166
actions: {
167
primary: [new Action('install', localize('install missing dep', "Install and Reload"), '', true,
168
() => this._extensionsWorkbenchService.install(dependencyExtension)
169
.then(() => this._hostService.reload(), e => this._notificationService.error(e)))]
170
}
171
});
172
} else {
173
this._notificationService.error(localize('unknownDep', "Cannot activate the '{0}' extension because it depends on an unknown '{1}' extension.", extName, missingDependency));
174
}
175
}
176
177
async $setPerformanceMarks(marks: PerformanceMark[]): Promise<void> {
178
if (this._extensionHostKind === ExtensionHostKind.LocalProcess) {
179
this._timerService.setPerformanceMarks('localExtHost', marks);
180
} else if (this._extensionHostKind === ExtensionHostKind.LocalWebWorker) {
181
this._timerService.setPerformanceMarks('workerExtHost', marks);
182
} else {
183
this._timerService.setPerformanceMarks('remoteExtHost', marks);
184
}
185
}
186
187
async $asBrowserUri(uri: UriComponents): Promise<UriComponents> {
188
return FileAccess.uriToBrowserUri(URI.revive(uri));
189
}
190
}
191
192
class ExtensionHostProxy implements IExtensionHostProxy {
193
constructor(
194
private readonly _actual: ExtHostExtensionServiceShape
195
) { }
196
197
async resolveAuthority(remoteAuthority: string, resolveAttempt: number): Promise<IResolveAuthorityResult> {
198
const resolved = reviveResolveAuthorityResult(await this._actual.$resolveAuthority(remoteAuthority, resolveAttempt));
199
return resolved;
200
}
201
async getCanonicalURI(remoteAuthority: string, uri: URI): Promise<URI | null> {
202
const uriComponents = await this._actual.$getCanonicalURI(remoteAuthority, uri);
203
return (uriComponents ? URI.revive(uriComponents) : uriComponents);
204
}
205
startExtensionHost(extensionsDelta: IExtensionDescriptionDelta): Promise<void> {
206
return this._actual.$startExtensionHost(extensionsDelta);
207
}
208
extensionTestsExecute(): Promise<number> {
209
return this._actual.$extensionTestsExecute();
210
}
211
activateByEvent(activationEvent: string, activationKind: ActivationKind): Promise<void> {
212
return this._actual.$activateByEvent(activationEvent, activationKind);
213
}
214
activate(extensionId: ExtensionIdentifier, reason: ExtensionActivationReason): Promise<boolean> {
215
return this._actual.$activate(extensionId, reason);
216
}
217
setRemoteEnvironment(env: { [key: string]: string | null }): Promise<void> {
218
return this._actual.$setRemoteEnvironment(env);
219
}
220
updateRemoteConnectionData(connectionData: IRemoteConnectionData): Promise<void> {
221
return this._actual.$updateRemoteConnectionData(connectionData);
222
}
223
deltaExtensions(extensionsDelta: IExtensionDescriptionDelta): Promise<void> {
224
return this._actual.$deltaExtensions(extensionsDelta);
225
}
226
test_latency(n: number): Promise<number> {
227
return this._actual.$test_latency(n);
228
}
229
test_up(b: VSBuffer): Promise<number> {
230
return this._actual.$test_up(b);
231
}
232
test_down(size: number): Promise<VSBuffer> {
233
return this._actual.$test_down(size);
234
}
235
}
236
237
function reviveResolveAuthorityResult(result: Dto<IResolveAuthorityResult>): IResolveAuthorityResult {
238
if (result.type === 'ok') {
239
return {
240
type: 'ok',
241
value: {
242
...result.value,
243
authority: reviveResolvedAuthority(result.value.authority),
244
}
245
};
246
} else {
247
return result;
248
}
249
}
250
251
function reviveResolvedAuthority(resolvedAuthority: Dto<ResolvedAuthority>): ResolvedAuthority {
252
return {
253
...resolvedAuthority,
254
connectTo: reviveConnection(resolvedAuthority.connectTo),
255
};
256
}
257
258
function reviveConnection(connection: Dto<RemoteConnection>): RemoteConnection {
259
if (connection.type === RemoteConnectionType.WebSocket) {
260
return new WebSocketRemoteConnection(connection.host, connection.port);
261
}
262
return new ManagedRemoteConnection(connection.id);
263
}
264
265