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