Path: blob/main/src/vs/platform/extensionManagement/common/abstractExtensionManagementService.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 { distinct, isNonEmptyArray } from '../../../base/common/arrays.js';6import { Barrier, CancelablePromise, createCancelablePromise } from '../../../base/common/async.js';7import { CancellationToken } from '../../../base/common/cancellation.js';8import { CancellationError, getErrorMessage, isCancellationError } from '../../../base/common/errors.js';9import { Emitter, Event } from '../../../base/common/event.js';10import { Disposable, toDisposable } from '../../../base/common/lifecycle.js';11import { ResourceMap } from '../../../base/common/map.js';12import { isWeb } from '../../../base/common/platform.js';13import { URI } from '../../../base/common/uri.js';14import * as nls from '../../../nls.js';15import {16ExtensionManagementError, IExtensionGalleryService, IExtensionIdentifier, IExtensionManagementParticipant, IGalleryExtension, ILocalExtension, InstallOperation,17IExtensionsControlManifest, StatisticType, isTargetPlatformCompatible, TargetPlatformToString, ExtensionManagementErrorCode,18InstallOptions, UninstallOptions, Metadata, InstallExtensionEvent, DidUninstallExtensionEvent, InstallExtensionResult, UninstallExtensionEvent, IExtensionManagementService, InstallExtensionInfo, EXTENSION_INSTALL_DEP_PACK_CONTEXT, ExtensionGalleryError,19IProductVersion, ExtensionGalleryErrorCode,20EXTENSION_INSTALL_SOURCE_CONTEXT,21DidUpdateExtensionMetadata,22UninstallExtensionInfo,23ExtensionSignatureVerificationCode,24IAllowedExtensionsService25} from './extensionManagement.js';26import { areSameExtensions, ExtensionKey, getGalleryExtensionId, getGalleryExtensionTelemetryData, getLocalExtensionTelemetryData, isMalicious } from './extensionManagementUtil.js';27import { ExtensionType, IExtensionManifest, isApplicationScopedExtension, TargetPlatform } from '../../extensions/common/extensions.js';28import { areApiProposalsCompatible } from '../../extensions/common/extensionValidator.js';29import { ILogService } from '../../log/common/log.js';30import { IProductService } from '../../product/common/productService.js';31import { ITelemetryService } from '../../telemetry/common/telemetry.js';32import { IUriIdentityService } from '../../uriIdentity/common/uriIdentity.js';33import { IUserDataProfilesService } from '../../userDataProfile/common/userDataProfile.js';34import { IMarkdownString, MarkdownString } from '../../../base/common/htmlContent.js';3536export type InstallableExtension = { readonly manifest: IExtensionManifest; extension: IGalleryExtension | URI; options: InstallOptions };3738export type InstallExtensionTaskOptions = InstallOptions & { readonly profileLocation: URI; readonly productVersion: IProductVersion };39export interface IInstallExtensionTask {40readonly manifest: IExtensionManifest;41readonly identifier: IExtensionIdentifier;42readonly source: IGalleryExtension | URI;43readonly operation: InstallOperation;44readonly options: InstallExtensionTaskOptions;45readonly verificationStatus?: ExtensionSignatureVerificationCode;46run(): Promise<ILocalExtension>;47waitUntilTaskIsFinished(): Promise<ILocalExtension>;48cancel(): void;49}5051export type UninstallExtensionTaskOptions = UninstallOptions & { readonly profileLocation: URI };52export interface IUninstallExtensionTask {53readonly options: UninstallExtensionTaskOptions;54readonly extension: ILocalExtension;55run(): Promise<void>;56waitUntilTaskIsFinished(): Promise<void>;57cancel(): void;58}5960export abstract class CommontExtensionManagementService extends Disposable implements IExtensionManagementService {6162_serviceBrand: undefined;6364readonly preferPreReleases: boolean;6566constructor(67@IProductService protected readonly productService: IProductService,68@IAllowedExtensionsService protected readonly allowedExtensionsService: IAllowedExtensionsService,69) {70super();71this.preferPreReleases = this.productService.quality !== 'stable';72}7374async canInstall(extension: IGalleryExtension): Promise<true | IMarkdownString> {75const allowedToInstall = this.allowedExtensionsService.isAllowed({ id: extension.identifier.id, publisherDisplayName: extension.publisherDisplayName });76if (allowedToInstall !== true) {77return new MarkdownString(nls.localize('not allowed to install', "This extension cannot be installed because {0}", allowedToInstall.value));78}7980if (!(await this.isExtensionPlatformCompatible(extension))) {81const learnLink = isWeb ? 'https://aka.ms/vscode-web-extensions-guide' : 'https://aka.ms/vscode-platform-specific-extensions';82return new MarkdownString(`${nls.localize('incompatible platform', "The '{0}' extension is not available in {1} for the {2}.",83extension.displayName ?? extension.identifier.id, this.productService.nameLong, TargetPlatformToString(await this.getTargetPlatform()))} [${nls.localize('learn why', "Learn Why")}](${learnLink})`);84}8586return true;87}8889protected async isExtensionPlatformCompatible(extension: IGalleryExtension): Promise<boolean> {90const currentTargetPlatform = await this.getTargetPlatform();91return extension.allTargetPlatforms.some(targetPlatform => isTargetPlatformCompatible(targetPlatform, extension.allTargetPlatforms, currentTargetPlatform));92}9394abstract readonly onInstallExtension: Event<InstallExtensionEvent>;95abstract readonly onDidInstallExtensions: Event<readonly InstallExtensionResult[]>;96abstract readonly onUninstallExtension: Event<UninstallExtensionEvent>;97abstract readonly onDidUninstallExtension: Event<DidUninstallExtensionEvent>;98abstract readonly onDidUpdateExtensionMetadata: Event<DidUpdateExtensionMetadata>;99abstract installFromGallery(extension: IGalleryExtension, options?: InstallOptions): Promise<ILocalExtension>;100abstract installGalleryExtensions(extensions: InstallExtensionInfo[]): Promise<InstallExtensionResult[]>;101abstract uninstall(extension: ILocalExtension, options?: UninstallOptions): Promise<void>;102abstract uninstallExtensions(extensions: UninstallExtensionInfo[]): Promise<void>;103abstract toggleApplicationScope(extension: ILocalExtension, fromProfileLocation: URI): Promise<ILocalExtension>;104abstract getExtensionsControlManifest(): Promise<IExtensionsControlManifest>;105abstract resetPinnedStateForAllUserExtensions(pinned: boolean): Promise<void>;106abstract registerParticipant(pariticipant: IExtensionManagementParticipant): void;107abstract getTargetPlatform(): Promise<TargetPlatform>;108abstract zip(extension: ILocalExtension): Promise<URI>;109abstract getManifest(vsix: URI): Promise<IExtensionManifest>;110abstract install(vsix: URI, options?: InstallOptions): Promise<ILocalExtension>;111abstract installFromLocation(location: URI, profileLocation: URI): Promise<ILocalExtension>;112abstract installExtensionsFromProfile(extensions: IExtensionIdentifier[], fromProfileLocation: URI, toProfileLocation: URI): Promise<ILocalExtension[]>;113abstract getInstalled(type?: ExtensionType, profileLocation?: URI, productVersion?: IProductVersion): Promise<ILocalExtension[]>;114abstract copyExtensions(fromProfileLocation: URI, toProfileLocation: URI): Promise<void>;115abstract download(extension: IGalleryExtension, operation: InstallOperation, donotVerifySignature: boolean): Promise<URI>;116abstract cleanUp(): Promise<void>;117abstract updateMetadata(local: ILocalExtension, metadata: Partial<Metadata>, profileLocation: URI): Promise<ILocalExtension>;118}119120export abstract class AbstractExtensionManagementService extends CommontExtensionManagementService implements IExtensionManagementService {121122declare readonly _serviceBrand: undefined;123124private extensionsControlManifest: Promise<IExtensionsControlManifest> | undefined;125private lastReportTimestamp = 0;126private readonly installingExtensions = new Map<string, { task: IInstallExtensionTask; waitingTasks: IInstallExtensionTask[] }>();127private readonly uninstallingExtensions = new Map<string, IUninstallExtensionTask>();128129private readonly _onInstallExtension = this._register(new Emitter<InstallExtensionEvent>());130get onInstallExtension() { return this._onInstallExtension.event; }131132protected readonly _onDidInstallExtensions = this._register(new Emitter<InstallExtensionResult[]>());133get onDidInstallExtensions() { return this._onDidInstallExtensions.event; }134135protected readonly _onUninstallExtension = this._register(new Emitter<UninstallExtensionEvent>());136get onUninstallExtension() { return this._onUninstallExtension.event; }137138protected _onDidUninstallExtension = this._register(new Emitter<DidUninstallExtensionEvent>());139get onDidUninstallExtension() { return this._onDidUninstallExtension.event; }140141protected readonly _onDidUpdateExtensionMetadata = this._register(new Emitter<DidUpdateExtensionMetadata>());142get onDidUpdateExtensionMetadata() { return this._onDidUpdateExtensionMetadata.event; }143144private readonly participants: IExtensionManagementParticipant[] = [];145146constructor(147@IExtensionGalleryService protected readonly galleryService: IExtensionGalleryService,148@ITelemetryService protected readonly telemetryService: ITelemetryService,149@IUriIdentityService protected readonly uriIdentityService: IUriIdentityService,150@ILogService protected readonly logService: ILogService,151@IProductService productService: IProductService,152@IAllowedExtensionsService allowedExtensionsService: IAllowedExtensionsService,153@IUserDataProfilesService protected readonly userDataProfilesService: IUserDataProfilesService,154) {155super(productService, allowedExtensionsService);156this._register(toDisposable(() => {157this.installingExtensions.forEach(({ task }) => task.cancel());158this.uninstallingExtensions.forEach(promise => promise.cancel());159this.installingExtensions.clear();160this.uninstallingExtensions.clear();161}));162}163164async installFromGallery(extension: IGalleryExtension, options: InstallOptions = {}): Promise<ILocalExtension> {165try {166const results = await this.installGalleryExtensions([{ extension, options }]);167const result = results.find(({ identifier }) => areSameExtensions(identifier, extension.identifier));168if (result?.local) {169return result?.local;170}171if (result?.error) {172throw result.error;173}174throw new ExtensionManagementError(`Unknown error while installing extension ${extension.identifier.id}`, ExtensionManagementErrorCode.Unknown);175} catch (error) {176throw toExtensionManagementError(error);177}178}179180async installGalleryExtensions(extensions: InstallExtensionInfo[]): Promise<InstallExtensionResult[]> {181if (!this.galleryService.isEnabled()) {182throw new ExtensionManagementError(nls.localize('MarketPlaceDisabled', "Marketplace is not enabled"), ExtensionManagementErrorCode.NotAllowed);183}184185const results: InstallExtensionResult[] = [];186const installableExtensions: InstallableExtension[] = [];187188await Promise.allSettled(extensions.map(async ({ extension, options }) => {189try {190const compatible = await this.checkAndGetCompatibleVersion(extension, !!options?.installGivenVersion, !!options?.installPreReleaseVersion, options.productVersion ?? { version: this.productService.version, date: this.productService.date });191installableExtensions.push({ ...compatible, options });192} catch (error) {193results.push({ identifier: extension.identifier, operation: InstallOperation.Install, source: extension, error, profileLocation: options.profileLocation ?? this.getCurrentExtensionsManifestLocation() });194}195}));196197if (installableExtensions.length) {198results.push(...await this.installExtensions(installableExtensions));199}200201return results;202}203204async uninstall(extension: ILocalExtension, options?: UninstallOptions): Promise<void> {205this.logService.trace('ExtensionManagementService#uninstall', extension.identifier.id);206return this.uninstallExtensions([{ extension, options }]);207}208209async toggleApplicationScope(extension: ILocalExtension, fromProfileLocation: URI): Promise<ILocalExtension> {210if (isApplicationScopedExtension(extension.manifest) || extension.isBuiltin) {211return extension;212}213214if (extension.isApplicationScoped) {215let local = await this.updateMetadata(extension, { isApplicationScoped: false }, this.userDataProfilesService.defaultProfile.extensionsResource);216if (!this.uriIdentityService.extUri.isEqual(fromProfileLocation, this.userDataProfilesService.defaultProfile.extensionsResource)) {217local = await this.copyExtension(extension, this.userDataProfilesService.defaultProfile.extensionsResource, fromProfileLocation);218}219220for (const profile of this.userDataProfilesService.profiles) {221const existing = (await this.getInstalled(ExtensionType.User, profile.extensionsResource))222.find(e => areSameExtensions(e.identifier, extension.identifier));223if (existing) {224this._onDidUpdateExtensionMetadata.fire({ local: existing, profileLocation: profile.extensionsResource });225} else {226this._onDidUninstallExtension.fire({ identifier: extension.identifier, profileLocation: profile.extensionsResource });227}228}229return local;230}231232else {233const local = this.uriIdentityService.extUri.isEqual(fromProfileLocation, this.userDataProfilesService.defaultProfile.extensionsResource)234? await this.updateMetadata(extension, { isApplicationScoped: true }, this.userDataProfilesService.defaultProfile.extensionsResource)235: await this.copyExtension(extension, fromProfileLocation, this.userDataProfilesService.defaultProfile.extensionsResource, { isApplicationScoped: true });236237this._onDidInstallExtensions.fire([{ identifier: local.identifier, operation: InstallOperation.Install, local, profileLocation: this.userDataProfilesService.defaultProfile.extensionsResource, applicationScoped: true }]);238return local;239}240241}242243getExtensionsControlManifest(): Promise<IExtensionsControlManifest> {244const now = new Date().getTime();245246if (!this.extensionsControlManifest || now - this.lastReportTimestamp > 1000 * 60 * 5) { // 5 minute cache freshness247this.extensionsControlManifest = this.updateControlCache();248this.lastReportTimestamp = now;249}250251return this.extensionsControlManifest;252}253254registerParticipant(participant: IExtensionManagementParticipant): void {255this.participants.push(participant);256}257258async resetPinnedStateForAllUserExtensions(pinned: boolean): Promise<void> {259try {260await this.joinAllSettled(this.userDataProfilesService.profiles.map(261async profile => {262const extensions = await this.getInstalled(ExtensionType.User, profile.extensionsResource);263await this.joinAllSettled(extensions.map(264async extension => {265if (extension.pinned !== pinned) {266await this.updateMetadata(extension, { pinned }, profile.extensionsResource);267}268}));269}));270} catch (error) {271this.logService.error('Error while resetting pinned state for all user extensions', getErrorMessage(error));272throw error;273}274}275276protected async installExtensions(extensions: InstallableExtension[]): Promise<InstallExtensionResult[]> {277const installExtensionResultsMap = new Map<string, InstallExtensionResult & { profileLocation: URI }>();278const installingExtensionsMap = new Map<string, { task: IInstallExtensionTask; root: IInstallExtensionTask | undefined; uninstallTaskToWaitFor?: IUninstallExtensionTask }>();279const alreadyRequestedInstallations: Promise<any>[] = [];280281const getInstallExtensionTaskKey = (extension: IGalleryExtension, profileLocation: URI) => `${ExtensionKey.create(extension).toString()}-${profileLocation.toString()}`;282const createInstallExtensionTask = (manifest: IExtensionManifest, extension: IGalleryExtension | URI, options: InstallExtensionTaskOptions, root: IInstallExtensionTask | undefined): void => {283let uninstallTaskToWaitFor;284if (!URI.isUri(extension)) {285if (installingExtensionsMap.has(`${extension.identifier.id.toLowerCase()}-${options.profileLocation.toString()}`)) {286return;287}288const existingInstallingExtension = this.installingExtensions.get(getInstallExtensionTaskKey(extension, options.profileLocation));289if (existingInstallingExtension) {290if (root && this.canWaitForTask(root, existingInstallingExtension.task)) {291const identifier = existingInstallingExtension.task.identifier;292this.logService.info('Waiting for already requested installing extension', identifier.id, root.identifier.id, options.profileLocation.toString());293existingInstallingExtension.waitingTasks.push(root);294// add promise that waits until the extension is completely installed, ie., onDidInstallExtensions event is triggered for this extension295alreadyRequestedInstallations.push(296Event.toPromise(297Event.filter(this.onDidInstallExtensions, results => results.some(result => areSameExtensions(result.identifier, identifier)))298).then(results => {299this.logService.info('Finished waiting for already requested installing extension', identifier.id, root.identifier.id, options.profileLocation.toString());300const result = results.find(result => areSameExtensions(result.identifier, identifier));301if (!result?.local) {302// Extension failed to install303throw new Error(`Extension ${identifier.id} is not installed`);304}305}));306}307return;308}309uninstallTaskToWaitFor = this.uninstallingExtensions.get(this.getUninstallExtensionTaskKey(extension.identifier, options.profileLocation));310}311const installExtensionTask = this.createInstallExtensionTask(manifest, extension, options);312const key = `${getGalleryExtensionId(manifest.publisher, manifest.name)}-${options.profileLocation.toString()}`;313installingExtensionsMap.set(key, { task: installExtensionTask, root, uninstallTaskToWaitFor });314this._onInstallExtension.fire({ identifier: installExtensionTask.identifier, source: extension, profileLocation: options.profileLocation });315this.logService.info('Installing extension:', installExtensionTask.identifier.id, options);316// only cache gallery extensions tasks317if (!URI.isUri(extension)) {318this.installingExtensions.set(getInstallExtensionTaskKey(extension, options.profileLocation), { task: installExtensionTask, waitingTasks: [] });319}320};321322try {323// Start installing extensions324for (const { manifest, extension, options } of extensions) {325const isApplicationScoped = options.isApplicationScoped || options.isBuiltin || isApplicationScopedExtension(manifest);326const installExtensionTaskOptions: InstallExtensionTaskOptions = {327...options,328isApplicationScoped,329profileLocation: isApplicationScoped ? this.userDataProfilesService.defaultProfile.extensionsResource : options.profileLocation ?? this.getCurrentExtensionsManifestLocation(),330productVersion: options.productVersion ?? { version: this.productService.version, date: this.productService.date }331};332333const existingInstallExtensionTask = !URI.isUri(extension) ? this.installingExtensions.get(getInstallExtensionTaskKey(extension, installExtensionTaskOptions.profileLocation)) : undefined;334if (existingInstallExtensionTask) {335this.logService.info('Extension is already requested to install', existingInstallExtensionTask.task.identifier.id, installExtensionTaskOptions.profileLocation.toString());336alreadyRequestedInstallations.push(existingInstallExtensionTask.task.waitUntilTaskIsFinished());337} else {338createInstallExtensionTask(manifest, extension, installExtensionTaskOptions, undefined);339}340}341342// collect and start installing all dependencies and pack extensions343await Promise.all([...installingExtensionsMap.values()].map(async ({ task }) => {344if (task.options.donotIncludePackAndDependencies) {345this.logService.info('Installing the extension without checking dependencies and pack', task.identifier.id);346} else {347try {348let preferPreRelease = this.preferPreReleases;349if (task.options.installPreReleaseVersion) {350preferPreRelease = true;351} else if (!URI.isUri(task.source) && task.source.hasPreReleaseVersion) {352// Explicitly asked to install the release version353preferPreRelease = false;354}355const installed = await this.getInstalled(undefined, task.options.profileLocation, task.options.productVersion);356const allDepsAndPackExtensionsToInstall = await this.getAllDepsAndPackExtensions(task.identifier, task.manifest, preferPreRelease, task.options.productVersion, installed);357const options: InstallExtensionTaskOptions = { ...task.options, pinned: false, installGivenVersion: false, context: { ...task.options.context, [EXTENSION_INSTALL_DEP_PACK_CONTEXT]: true } };358for (const { gallery, manifest } of distinct(allDepsAndPackExtensionsToInstall, ({ gallery }) => gallery.identifier.id)) {359const existing = installed.find(e => areSameExtensions(e.identifier, gallery.identifier));360// Skip if the extension is already installed and has the same application scope361if (existing && existing.isApplicationScoped === !!options.isApplicationScoped) {362continue;363}364createInstallExtensionTask(manifest, gallery, options, task);365}366} catch (error) {367// Installing through VSIX368if (URI.isUri(task.source)) {369// Ignore installing dependencies and packs370if (isNonEmptyArray(task.manifest.extensionDependencies)) {371this.logService.warn(`Cannot install dependencies of extension:`, task.identifier.id, error.message);372}373if (isNonEmptyArray(task.manifest.extensionPack)) {374this.logService.warn(`Cannot install packed extensions of extension:`, task.identifier.id, error.message);375}376} else {377this.logService.error('Error while preparing to install dependencies and extension packs of the extension:', task.identifier.id);378throw error;379}380}381}382}));383384const otherProfilesToUpdate = await this.getOtherProfilesToUpdateExtension([...installingExtensionsMap.values()].map(({ task }) => task));385for (const [profileLocation, task] of otherProfilesToUpdate) {386createInstallExtensionTask(task.manifest, task.source, { ...task.options, profileLocation }, undefined);387}388389// Install extensions in parallel and wait until all extensions are installed / failed390await this.joinAllSettled([...installingExtensionsMap.entries()].map(async ([key, { task, uninstallTaskToWaitFor }]) => {391const startTime = new Date().getTime();392let local: ILocalExtension;393try {394if (uninstallTaskToWaitFor) {395this.logService.info('Waiting for existing uninstall task to complete before installing', task.identifier.id);396try {397await uninstallTaskToWaitFor.waitUntilTaskIsFinished();398this.logService.info('Finished waiting for uninstall task, proceeding with install', task.identifier.id);399} catch (error) {400this.logService.info('Uninstall task failed, proceeding with install anyway', task.identifier.id, getErrorMessage(error));401}402}403404local = await task.run();405await this.joinAllSettled(this.participants.map(participant => participant.postInstall(local, task.source, task.options, CancellationToken.None)), ExtensionManagementErrorCode.PostInstall);406} catch (e) {407const error = toExtensionManagementError(e);408if (!URI.isUri(task.source)) {409reportTelemetry(this.telemetryService, task.operation === InstallOperation.Update ? 'extensionGallery:update' : 'extensionGallery:install', {410extensionData: getGalleryExtensionTelemetryData(task.source),411error,412source: task.options.context?.[EXTENSION_INSTALL_SOURCE_CONTEXT]413});414}415installExtensionResultsMap.set(key, { error, identifier: task.identifier, operation: task.operation, source: task.source, context: task.options.context, profileLocation: task.options.profileLocation, applicationScoped: task.options.isApplicationScoped });416this.logService.error('Error while installing the extension', task.identifier.id, getErrorMessage(error), task.options.profileLocation.toString());417throw error;418}419if (!URI.isUri(task.source)) {420const isUpdate = task.operation === InstallOperation.Update;421const durationSinceUpdate = isUpdate ? undefined : (new Date().getTime() - task.source.lastUpdated) / 1000;422reportTelemetry(this.telemetryService, isUpdate ? 'extensionGallery:update' : 'extensionGallery:install', {423extensionData: getGalleryExtensionTelemetryData(task.source),424verificationStatus: task.verificationStatus,425duration: new Date().getTime() - startTime,426durationSinceUpdate,427source: task.options.context?.[EXTENSION_INSTALL_SOURCE_CONTEXT]428});429// In web, report extension install statistics explicitly. In Desktop, statistics are automatically updated while downloading the VSIX.430if (isWeb && task.operation !== InstallOperation.Update) {431try {432await this.galleryService.reportStatistic(local.manifest.publisher, local.manifest.name, local.manifest.version, StatisticType.Install);433} catch (error) { /* ignore */ }434}435}436installExtensionResultsMap.set(key, { local, identifier: task.identifier, operation: task.operation, source: task.source, context: task.options.context, profileLocation: task.options.profileLocation, applicationScoped: local.isApplicationScoped });437}));438439if (alreadyRequestedInstallations.length) {440await this.joinAllSettled(alreadyRequestedInstallations);441}442} catch (error) {443const getAllDepsAndPacks = (extension: ILocalExtension, profileLocation: URI, allDepsOrPacks: string[]) => {444const depsOrPacks = [];445if (extension.manifest.extensionDependencies?.length) {446depsOrPacks.push(...extension.manifest.extensionDependencies);447}448if (extension.manifest.extensionPack?.length) {449depsOrPacks.push(...extension.manifest.extensionPack);450}451for (const id of depsOrPacks) {452if (allDepsOrPacks.includes(id.toLowerCase())) {453continue;454}455allDepsOrPacks.push(id.toLowerCase());456const installed = installExtensionResultsMap.get(`${id.toLowerCase()}-${profileLocation.toString()}`);457if (installed?.local) {458allDepsOrPacks = getAllDepsAndPacks(installed.local, profileLocation, allDepsOrPacks);459}460}461return allDepsOrPacks;462};463const getErrorResult = (task: IInstallExtensionTask) => ({ identifier: task.identifier, operation: InstallOperation.Install, source: task.source, context: task.options.context, profileLocation: task.options.profileLocation, error });464465const rollbackTasks: IUninstallExtensionTask[] = [];466for (const [key, { task, root }] of installingExtensionsMap) {467const result = installExtensionResultsMap.get(key);468if (!result) {469task.cancel();470installExtensionResultsMap.set(key, getErrorResult(task));471}472// If the extension is installed by a root task and the root task is failed, then uninstall the extension473else if (result.local && root && !installExtensionResultsMap.get(`${root.identifier.id.toLowerCase()}-${task.options.profileLocation.toString()}`)?.local) {474rollbackTasks.push(this.createUninstallExtensionTask(result.local, { versionOnly: true, profileLocation: task.options.profileLocation }));475installExtensionResultsMap.set(key, getErrorResult(task));476}477}478for (const [key, { task }] of installingExtensionsMap) {479const result = installExtensionResultsMap.get(key);480if (!result?.local) {481continue;482}483if (task.options.donotIncludePackAndDependencies) {484continue;485}486const depsOrPacks = getAllDepsAndPacks(result.local, task.options.profileLocation, [result.local.identifier.id.toLowerCase()]).slice(1);487if (depsOrPacks.some(depOrPack => installingExtensionsMap.has(`${depOrPack.toLowerCase()}-${task.options.profileLocation.toString()}`) && !installExtensionResultsMap.get(`${depOrPack.toLowerCase()}-${task.options.profileLocation.toString()}`)?.local)) {488rollbackTasks.push(this.createUninstallExtensionTask(result.local, { versionOnly: true, profileLocation: task.options.profileLocation }));489installExtensionResultsMap.set(key, getErrorResult(task));490}491}492493if (rollbackTasks.length) {494await Promise.allSettled(rollbackTasks.map(async rollbackTask => {495try {496await rollbackTask.run();497this.logService.info('Rollback: Uninstalled extension', rollbackTask.extension.identifier.id);498} catch (error) {499this.logService.warn('Rollback: Error while uninstalling extension', rollbackTask.extension.identifier.id, getErrorMessage(error));500}501}));502}503} finally {504// Finally, remove all the tasks from the cache505for (const { task } of installingExtensionsMap.values()) {506if (task.source && !URI.isUri(task.source)) {507this.installingExtensions.delete(getInstallExtensionTaskKey(task.source, task.options.profileLocation));508}509}510}511const results = [...installExtensionResultsMap.values()];512for (const result of results) {513if (result.local) {514this.logService.info(`Extension installed successfully:`, result.identifier.id, result.profileLocation.toString());515}516}517this._onDidInstallExtensions.fire(results);518return results;519}520521private async getOtherProfilesToUpdateExtension(tasks: IInstallExtensionTask[]): Promise<[URI, IInstallExtensionTask][]> {522const otherProfilesToUpdate: [URI, IInstallExtensionTask][] = [];523const profileExtensionsCache = new ResourceMap<ILocalExtension[]>();524for (const task of tasks) {525if (task.operation !== InstallOperation.Update526|| task.options.isApplicationScoped527|| task.options.pinned528|| task.options.installGivenVersion529|| URI.isUri(task.source)530) {531continue;532}533for (const profile of this.userDataProfilesService.profiles) {534if (this.uriIdentityService.extUri.isEqual(profile.extensionsResource, task.options.profileLocation)) {535continue;536}537let installedExtensions = profileExtensionsCache.get(profile.extensionsResource);538if (!installedExtensions) {539installedExtensions = await this.getInstalled(ExtensionType.User, profile.extensionsResource);540profileExtensionsCache.set(profile.extensionsResource, installedExtensions);541}542const installedExtension = installedExtensions.find(e => areSameExtensions(e.identifier, task.identifier));543if (installedExtension && !installedExtension.pinned) {544otherProfilesToUpdate.push([profile.extensionsResource, task]);545}546}547}548return otherProfilesToUpdate;549}550551private canWaitForTask(taskToWait: IInstallExtensionTask, taskToWaitFor: IInstallExtensionTask): boolean {552for (const [, { task, waitingTasks }] of this.installingExtensions.entries()) {553if (task === taskToWait) {554// Cannot be waited, If taskToWaitFor is waiting for taskToWait555if (waitingTasks.includes(taskToWaitFor)) {556return false;557}558// Cannot be waited, If taskToWaitFor is waiting for tasks waiting for taskToWait559if (waitingTasks.some(waitingTask => this.canWaitForTask(waitingTask, taskToWaitFor))) {560return false;561}562}563// Cannot be waited, if the taskToWait cannot be waited for the task created the taskToWaitFor564// Because, the task waits for the tasks it created565if (task === taskToWaitFor && waitingTasks[0] && !this.canWaitForTask(taskToWait, waitingTasks[0])) {566return false;567}568}569return true;570}571572private async joinAllSettled<T>(promises: Promise<T>[], errorCode?: ExtensionManagementErrorCode): Promise<T[]> {573const results: T[] = [];574const errors: ExtensionManagementError[] = [];575const promiseResults = await Promise.allSettled(promises);576for (const r of promiseResults) {577if (r.status === 'fulfilled') {578results.push(r.value);579} else {580errors.push(toExtensionManagementError(r.reason, errorCode));581}582}583584if (!errors.length) {585return results;586}587588// Throw if there are errors589if (errors.length === 1) {590throw errors[0];591}592593let error = new ExtensionManagementError('', ExtensionManagementErrorCode.Unknown);594for (const current of errors) {595error = new ExtensionManagementError(596error.message ? `${error.message}, ${current.message}` : current.message,597current.code !== ExtensionManagementErrorCode.Unknown && current.code !== ExtensionManagementErrorCode.Internal ? current.code : error.code598);599}600throw error;601}602603private async getAllDepsAndPackExtensions(extensionIdentifier: IExtensionIdentifier, manifest: IExtensionManifest, preferPreRelease: boolean, productVersion: IProductVersion, installed: ILocalExtension[]): Promise<{ gallery: IGalleryExtension; manifest: IExtensionManifest }[]> {604if (!this.galleryService.isEnabled()) {605return [];606}607608const knownIdentifiers: IExtensionIdentifier[] = [];609610const allDependenciesAndPacks: { gallery: IGalleryExtension; manifest: IExtensionManifest }[] = [];611const collectDependenciesAndPackExtensionsToInstall = async (extensionIdentifier: IExtensionIdentifier, manifest: IExtensionManifest): Promise<void> => {612knownIdentifiers.push(extensionIdentifier);613const dependecies: string[] = manifest.extensionDependencies || [];614const dependenciesAndPackExtensions = [...dependecies];615if (manifest.extensionPack) {616const existing = installed.find(e => areSameExtensions(e.identifier, extensionIdentifier));617for (const extension of manifest.extensionPack) {618// add only those extensions which are new in currently installed extension619if (!(existing && existing.manifest.extensionPack && existing.manifest.extensionPack.some(old => areSameExtensions({ id: old }, { id: extension })))) {620if (dependenciesAndPackExtensions.every(e => !areSameExtensions({ id: e }, { id: extension }))) {621dependenciesAndPackExtensions.push(extension);622}623}624}625}626627if (dependenciesAndPackExtensions.length) {628// filter out known extensions629const ids = dependenciesAndPackExtensions.filter(id => knownIdentifiers.every(galleryIdentifier => !areSameExtensions(galleryIdentifier, { id })));630if (ids.length) {631const galleryExtensions = await this.galleryService.getExtensions(ids.map(id => ({ id, preRelease: preferPreRelease })), CancellationToken.None);632for (const galleryExtension of galleryExtensions) {633if (knownIdentifiers.find(identifier => areSameExtensions(identifier, galleryExtension.identifier))) {634continue;635}636const isDependency = dependecies.some(id => areSameExtensions({ id }, galleryExtension.identifier));637let compatible;638try {639compatible = await this.checkAndGetCompatibleVersion(galleryExtension, false, preferPreRelease, productVersion);640} catch (error) {641if (!isDependency) {642this.logService.info('Skipping the packed extension as it cannot be installed', galleryExtension.identifier.id, getErrorMessage(error));643continue;644} else {645throw error;646}647}648allDependenciesAndPacks.push({ gallery: compatible.extension, manifest: compatible.manifest });649await collectDependenciesAndPackExtensionsToInstall(compatible.extension.identifier, compatible.manifest);650}651}652}653};654655await collectDependenciesAndPackExtensionsToInstall(extensionIdentifier, manifest);656return allDependenciesAndPacks;657}658659private async checkAndGetCompatibleVersion(extension: IGalleryExtension, sameVersion: boolean, installPreRelease: boolean, productVersion: IProductVersion): Promise<{ extension: IGalleryExtension; manifest: IExtensionManifest }> {660let compatibleExtension: IGalleryExtension | null;661662const extensionsControlManifest = await this.getExtensionsControlManifest();663if (isMalicious(extension.identifier, extensionsControlManifest.malicious)) {664throw new ExtensionManagementError(nls.localize('malicious extension', "Can't install '{0}' extension since it was reported to be problematic.", extension.identifier.id), ExtensionManagementErrorCode.Malicious);665}666667const deprecationInfo = extensionsControlManifest.deprecated[extension.identifier.id.toLowerCase()];668if (deprecationInfo?.extension?.autoMigrate) {669this.logService.info(`The '${extension.identifier.id}' extension is deprecated, fetching the compatible '${deprecationInfo.extension.id}' extension instead.`);670compatibleExtension = (await this.galleryService.getExtensions([{ id: deprecationInfo.extension.id, preRelease: deprecationInfo.extension.preRelease }], { targetPlatform: await this.getTargetPlatform(), compatible: true, productVersion }, CancellationToken.None))[0];671if (!compatibleExtension) {672throw new ExtensionManagementError(nls.localize('notFoundDeprecatedReplacementExtension', "Can't install '{0}' extension since it was deprecated and the replacement extension '{1}' can't be found.", extension.identifier.id, deprecationInfo.extension.id), ExtensionManagementErrorCode.Deprecated);673}674}675676else {677if (await this.canInstall(extension) !== true) {678const targetPlatform = await this.getTargetPlatform();679throw new ExtensionManagementError(nls.localize('incompatible platform', "The '{0}' extension is not available in {1} for the {2}.", extension.identifier.id, this.productService.nameLong, TargetPlatformToString(targetPlatform)), ExtensionManagementErrorCode.IncompatibleTargetPlatform);680}681682compatibleExtension = await this.getCompatibleVersion(extension, sameVersion, installPreRelease, productVersion);683if (!compatibleExtension) {684const incompatibleApiProposalsMessages: string[] = [];685if (!areApiProposalsCompatible(extension.properties.enabledApiProposals ?? [], incompatibleApiProposalsMessages)) {686throw new ExtensionManagementError(nls.localize('incompatibleAPI', "Can't install '{0}' extension. {1}", extension.displayName ?? extension.identifier.id, incompatibleApiProposalsMessages[0]), ExtensionManagementErrorCode.IncompatibleApi);687}688/** If no compatible release version is found, check if the extension has a release version or not and throw relevant error */689if (!installPreRelease && extension.hasPreReleaseVersion && extension.properties.isPreReleaseVersion && (await this.galleryService.getExtensions([extension.identifier], CancellationToken.None))[0]) {690throw new ExtensionManagementError(nls.localize('notFoundReleaseExtension', "Can't install release version of '{0}' extension because it has no release version.", extension.displayName ?? extension.identifier.id), ExtensionManagementErrorCode.ReleaseVersionNotFound);691}692throw new ExtensionManagementError(nls.localize('notFoundCompatibleDependency', "Can't install '{0}' extension because it is not compatible with the current version of {1} (version {2}).", extension.identifier.id, this.productService.nameLong, this.productService.version), ExtensionManagementErrorCode.Incompatible);693}694}695696this.logService.info('Getting Manifest...', compatibleExtension.identifier.id);697const manifest = await this.galleryService.getManifest(compatibleExtension, CancellationToken.None);698if (manifest === null) {699throw new ExtensionManagementError(`Missing manifest for extension ${compatibleExtension.identifier.id}`, ExtensionManagementErrorCode.Invalid);700}701702if (manifest.version !== compatibleExtension.version) {703throw new ExtensionManagementError(`Cannot install '${compatibleExtension.identifier.id}' extension because of version mismatch in Marketplace`, ExtensionManagementErrorCode.Invalid);704}705706return { extension: compatibleExtension, manifest };707}708709protected async getCompatibleVersion(extension: IGalleryExtension, sameVersion: boolean, includePreRelease: boolean, productVersion: IProductVersion): Promise<IGalleryExtension | null> {710const targetPlatform = await this.getTargetPlatform();711let compatibleExtension: IGalleryExtension | null = null;712713if (!sameVersion && extension.hasPreReleaseVersion && extension.properties.isPreReleaseVersion !== includePreRelease) {714compatibleExtension = (await this.galleryService.getExtensions([{ ...extension.identifier, preRelease: includePreRelease }], { targetPlatform, compatible: true, productVersion }, CancellationToken.None))[0] || null;715}716717if (!compatibleExtension && await this.galleryService.isExtensionCompatible(extension, includePreRelease, targetPlatform, productVersion)) {718compatibleExtension = extension;719}720721if (!compatibleExtension) {722if (sameVersion) {723compatibleExtension = (await this.galleryService.getExtensions([{ ...extension.identifier, version: extension.version }], { targetPlatform, compatible: true, productVersion }, CancellationToken.None))[0] || null;724} else {725compatibleExtension = await this.galleryService.getCompatibleExtension(extension, includePreRelease, targetPlatform, productVersion);726}727}728729return compatibleExtension;730}731732private getUninstallExtensionTaskKey(identifier: IExtensionIdentifier, profileLocation: URI, version?: string): string {733return `${identifier.id.toLowerCase()}${version ? `-${version}` : ''}@${profileLocation.toString()}`;734}735736async uninstallExtensions(extensions: UninstallExtensionInfo[]): Promise<void> {737738const getUninstallExtensionTaskKey = (extension: ILocalExtension, uninstallOptions: UninstallExtensionTaskOptions) => this.getUninstallExtensionTaskKey(extension.identifier, uninstallOptions.profileLocation, uninstallOptions.versionOnly ? extension.manifest.version : undefined);739740const createUninstallExtensionTask = (extension: ILocalExtension, uninstallOptions: UninstallExtensionTaskOptions): void => {741let installTaskToWaitFor: IInstallExtensionTask | undefined;742for (const { task } of this.installingExtensions.values()) {743if (!(task.source instanceof URI) && areSameExtensions(task.identifier, extension.identifier) && this.uriIdentityService.extUri.isEqual(task.options.profileLocation, uninstallOptions.profileLocation)) {744installTaskToWaitFor = task;745break;746}747}748const task = this.createUninstallExtensionTask(extension, uninstallOptions);749this.uninstallingExtensions.set(getUninstallExtensionTaskKey(task.extension, uninstallOptions), task);750this.logService.info('Uninstalling extension from the profile:', `${extension.identifier.id}@${extension.manifest.version}`, uninstallOptions.profileLocation.toString());751this._onUninstallExtension.fire({ identifier: extension.identifier, profileLocation: uninstallOptions.profileLocation, applicationScoped: extension.isApplicationScoped });752allTasks.push({ task, installTaskToWaitFor });753};754755const postUninstallExtension = (extension: ILocalExtension, uninstallOptions: UninstallExtensionTaskOptions, error?: ExtensionManagementError): void => {756if (error) {757this.logService.error('Failed to uninstall extension from the profile:', `${extension.identifier.id}@${extension.manifest.version}`, uninstallOptions.profileLocation.toString(), error.message);758} else {759this.logService.info('Successfully uninstalled extension from the profile', `${extension.identifier.id}@${extension.manifest.version}`, uninstallOptions.profileLocation.toString());760}761reportTelemetry(this.telemetryService, 'extensionGallery:uninstall', { extensionData: getLocalExtensionTelemetryData(extension), error });762this._onDidUninstallExtension.fire({ identifier: extension.identifier, error: error?.code, profileLocation: uninstallOptions.profileLocation, applicationScoped: extension.isApplicationScoped });763};764765const allTasks: { task: IUninstallExtensionTask; installTaskToWaitFor?: IInstallExtensionTask }[] = [];766const processedTasks: IUninstallExtensionTask[] = [];767const alreadyRequestedUninstalls: Promise<any>[] = [];768const extensionsToRemove: ILocalExtension[] = [];769770const installedExtensionsMap = new ResourceMap<ILocalExtension[]>();771const getInstalledExtensions = async (profileLocation: URI) => {772let installed = installedExtensionsMap.get(profileLocation);773if (!installed) {774installedExtensionsMap.set(profileLocation, installed = await this.getInstalled(ExtensionType.User, profileLocation));775}776return installed;777};778779for (const { extension, options } of extensions) {780const uninstallOptions: UninstallExtensionTaskOptions = {781...options,782profileLocation: extension.isApplicationScoped ? this.userDataProfilesService.defaultProfile.extensionsResource : options?.profileLocation ?? this.getCurrentExtensionsManifestLocation()783};784const uninstallExtensionTask = this.uninstallingExtensions.get(getUninstallExtensionTaskKey(extension, uninstallOptions));785if (uninstallExtensionTask) {786this.logService.info('Extensions is already requested to uninstall', extension.identifier.id);787alreadyRequestedUninstalls.push(uninstallExtensionTask.waitUntilTaskIsFinished());788} else {789createUninstallExtensionTask(extension, uninstallOptions);790}791792if (uninstallOptions.remove || extension.isApplicationScoped) {793if (uninstallOptions.remove) {794extensionsToRemove.push(extension);795}796for (const profile of this.userDataProfilesService.profiles) {797if (this.uriIdentityService.extUri.isEqual(profile.extensionsResource, uninstallOptions.profileLocation)) {798continue;799}800const installed = await getInstalledExtensions(profile.extensionsResource);801const profileExtension = installed.find(e => areSameExtensions(e.identifier, extension.identifier));802if (profileExtension) {803const uninstallOptionsWithProfile = { ...uninstallOptions, profileLocation: profile.extensionsResource };804const uninstallExtensionTask = this.uninstallingExtensions.get(getUninstallExtensionTaskKey(profileExtension, uninstallOptionsWithProfile));805if (uninstallExtensionTask) {806this.logService.info('Extensions is already requested to uninstall', profileExtension.identifier.id);807alreadyRequestedUninstalls.push(uninstallExtensionTask.waitUntilTaskIsFinished());808} else {809createUninstallExtensionTask(profileExtension, uninstallOptionsWithProfile);810}811}812}813}814}815816try {817for (const { task } of allTasks.slice(0)) {818const installed = await getInstalledExtensions(task.options.profileLocation);819820if (task.options.donotIncludePack) {821this.logService.info('Uninstalling the extension without including packed extension', `${task.extension.identifier.id}@${task.extension.manifest.version}`);822} else {823const packedExtensions = this.getAllPackExtensionsToUninstall(task.extension, installed);824for (const packedExtension of packedExtensions) {825if (this.uninstallingExtensions.has(getUninstallExtensionTaskKey(packedExtension, task.options))) {826this.logService.info('Extensions is already requested to uninstall', packedExtension.identifier.id);827} else {828createUninstallExtensionTask(packedExtension, task.options);829}830}831}832if (task.options.donotCheckDependents) {833this.logService.info('Uninstalling the extension without checking dependents', `${task.extension.identifier.id}@${task.extension.manifest.version}`);834} else {835this.checkForDependents(allTasks.map(({ task }) => task.extension), installed, task.extension);836}837}838839// Uninstall extensions in parallel and wait until all extensions are uninstalled / failed840await this.joinAllSettled(allTasks.map(async ({ task, installTaskToWaitFor }) => {841try {842// Wait for opposite task if it exists843if (installTaskToWaitFor) {844this.logService.info('Waiting for existing install task to complete before uninstalling', task.extension.identifier.id);845try {846await installTaskToWaitFor.waitUntilTaskIsFinished();847this.logService.info('Finished waiting for install task, proceeding with uninstall', task.extension.identifier.id);848} catch (error) {849this.logService.info('Install task failed, proceeding with uninstall anyway', task.extension.identifier.id, getErrorMessage(error));850}851}852853await task.run();854await this.joinAllSettled(this.participants.map(participant => participant.postUninstall(task.extension, task.options, CancellationToken.None)));855// only report if extension has a mapped gallery extension. UUID identifies the gallery extension.856if (task.extension.identifier.uuid) {857try {858await this.galleryService.reportStatistic(task.extension.manifest.publisher, task.extension.manifest.name, task.extension.manifest.version, StatisticType.Uninstall);859} catch (error) { /* ignore */ }860}861} catch (e) {862const error = toExtensionManagementError(e);863postUninstallExtension(task.extension, task.options, error);864throw error;865} finally {866processedTasks.push(task);867}868}));869870if (alreadyRequestedUninstalls.length) {871await this.joinAllSettled(alreadyRequestedUninstalls);872}873874for (const { task } of allTasks) {875postUninstallExtension(task.extension, task.options);876}877878if (extensionsToRemove.length) {879await this.joinAllSettled(extensionsToRemove.map(extension => this.deleteExtension(extension)));880}881} catch (e) {882const error = toExtensionManagementError(e);883for (const { task } of allTasks) {884// cancel the tasks885try { task.cancel(); } catch (error) { /* ignore */ }886if (!processedTasks.includes(task)) {887postUninstallExtension(task.extension, task.options, error);888}889}890throw error;891} finally {892// Remove tasks from cache893for (const { task } of allTasks) {894if (!this.uninstallingExtensions.delete(getUninstallExtensionTaskKey(task.extension, task.options))) {895this.logService.warn('Uninstallation task is not found in the cache', task.extension.identifier.id);896}897}898}899}900901private checkForDependents(extensionsToUninstall: ILocalExtension[], installed: ILocalExtension[], extensionToUninstall: ILocalExtension): void {902for (const extension of extensionsToUninstall) {903const dependents = this.getDependents(extension, installed);904if (dependents.length) {905const remainingDependents = dependents.filter(dependent => !extensionsToUninstall.some(e => areSameExtensions(e.identifier, dependent.identifier)));906if (remainingDependents.length) {907throw new Error(this.getDependentsErrorMessage(extension, remainingDependents, extensionToUninstall));908}909}910}911}912913private getDependentsErrorMessage(dependingExtension: ILocalExtension, dependents: ILocalExtension[], extensionToUninstall: ILocalExtension): string {914if (extensionToUninstall === dependingExtension) {915if (dependents.length === 1) {916return nls.localize('singleDependentError', "Cannot uninstall '{0}' extension. '{1}' extension depends on this.",917extensionToUninstall.manifest.displayName || extensionToUninstall.manifest.name, dependents[0].manifest.displayName || dependents[0].manifest.name);918}919if (dependents.length === 2) {920return nls.localize('twoDependentsError', "Cannot uninstall '{0}' extension. '{1}' and '{2}' extensions depend on this.",921extensionToUninstall.manifest.displayName || extensionToUninstall.manifest.name, dependents[0].manifest.displayName || dependents[0].manifest.name, dependents[1].manifest.displayName || dependents[1].manifest.name);922}923return nls.localize('multipleDependentsError', "Cannot uninstall '{0}' extension. '{1}', '{2}' and other extension depend on this.",924extensionToUninstall.manifest.displayName || extensionToUninstall.manifest.name, dependents[0].manifest.displayName || dependents[0].manifest.name, dependents[1].manifest.displayName || dependents[1].manifest.name);925}926if (dependents.length === 1) {927return nls.localize('singleIndirectDependentError', "Cannot uninstall '{0}' extension . It includes uninstalling '{1}' extension and '{2}' extension depends on this.",928extensionToUninstall.manifest.displayName || extensionToUninstall.manifest.name, dependingExtension.manifest.displayName929|| dependingExtension.manifest.name, dependents[0].manifest.displayName || dependents[0].manifest.name);930}931if (dependents.length === 2) {932return nls.localize('twoIndirectDependentsError', "Cannot uninstall '{0}' extension. It includes uninstalling '{1}' extension and '{2}' and '{3}' extensions depend on this.",933extensionToUninstall.manifest.displayName || extensionToUninstall.manifest.name, dependingExtension.manifest.displayName934|| dependingExtension.manifest.name, dependents[0].manifest.displayName || dependents[0].manifest.name, dependents[1].manifest.displayName || dependents[1].manifest.name);935}936return nls.localize('multipleIndirectDependentsError', "Cannot uninstall '{0}' extension. It includes uninstalling '{1}' extension and '{2}', '{3}' and other extensions depend on this.",937extensionToUninstall.manifest.displayName || extensionToUninstall.manifest.name, dependingExtension.manifest.displayName938|| dependingExtension.manifest.name, dependents[0].manifest.displayName || dependents[0].manifest.name, dependents[1].manifest.displayName || dependents[1].manifest.name);939940}941942private getAllPackExtensionsToUninstall(extension: ILocalExtension, installed: ILocalExtension[], checked: ILocalExtension[] = []): ILocalExtension[] {943if (checked.indexOf(extension) !== -1) {944return [];945}946checked.push(extension);947const extensionsPack = extension.manifest.extensionPack ? extension.manifest.extensionPack : [];948if (extensionsPack.length) {949const packedExtensions = installed.filter(i => !i.isBuiltin && extensionsPack.some(id => areSameExtensions({ id }, i.identifier)));950const packOfPackedExtensions: ILocalExtension[] = [];951for (const packedExtension of packedExtensions) {952packOfPackedExtensions.push(...this.getAllPackExtensionsToUninstall(packedExtension, installed, checked));953}954return [...packedExtensions, ...packOfPackedExtensions];955}956return [];957}958959private getDependents(extension: ILocalExtension, installed: ILocalExtension[]): ILocalExtension[] {960return installed.filter(e => e.manifest.extensionDependencies && e.manifest.extensionDependencies.some(id => areSameExtensions({ id }, extension.identifier)));961}962963private async updateControlCache(): Promise<IExtensionsControlManifest> {964try {965this.logService.trace('ExtensionManagementService.updateControlCache');966return await this.galleryService.getExtensionsControlManifest();967} catch (err) {968this.logService.trace('ExtensionManagementService.refreshControlCache - failed to get extension control manifest', getErrorMessage(err));969return { malicious: [], deprecated: {}, search: [] };970}971}972973protected abstract getCurrentExtensionsManifestLocation(): URI;974protected abstract createInstallExtensionTask(manifest: IExtensionManifest, extension: URI | IGalleryExtension, options: InstallExtensionTaskOptions): IInstallExtensionTask;975protected abstract createUninstallExtensionTask(extension: ILocalExtension, options: UninstallExtensionTaskOptions): IUninstallExtensionTask;976protected abstract copyExtension(extension: ILocalExtension, fromProfileLocation: URI, toProfileLocation: URI, metadata?: Partial<Metadata>): Promise<ILocalExtension>;977protected abstract moveExtension(extension: ILocalExtension, fromProfileLocation: URI, toProfileLocation: URI, metadata?: Partial<Metadata>): Promise<ILocalExtension>;978protected abstract removeExtension(extension: ILocalExtension, fromProfileLocation: URI): Promise<void>;979protected abstract deleteExtension(extension: ILocalExtension): Promise<void>;980}981982export function toExtensionManagementError(error: Error, code?: ExtensionManagementErrorCode): ExtensionManagementError {983if (error instanceof ExtensionManagementError) {984return error;985}986let extensionManagementError: ExtensionManagementError;987if (error instanceof ExtensionGalleryError) {988extensionManagementError = new ExtensionManagementError(error.message, error.code === ExtensionGalleryErrorCode.DownloadFailedWriting ? ExtensionManagementErrorCode.DownloadFailedWriting : ExtensionManagementErrorCode.Gallery);989} else {990extensionManagementError = new ExtensionManagementError(error.message, isCancellationError(error) ? ExtensionManagementErrorCode.Cancelled : (code ?? ExtensionManagementErrorCode.Internal));991}992extensionManagementError.stack = error.stack;993return extensionManagementError;994}995996function reportTelemetry(telemetryService: ITelemetryService, eventName: string,997{998extensionData,999verificationStatus,1000duration,1001error,1002source,1003durationSinceUpdate1004}: {1005extensionData: any;1006verificationStatus?: ExtensionSignatureVerificationCode;1007duration?: number;1008durationSinceUpdate?: number;1009source?: string;1010error?: ExtensionManagementError | ExtensionGalleryError;1011}): void {10121013/* __GDPR__1014"extensionGallery:install" : {1015"owner": "sandy081",1016"success": { "classification": "SystemMetaData", "purpose": "PerformanceAndHealth", "isMeasurement": true },1017"duration" : { "classification": "SystemMetaData", "purpose": "PerformanceAndHealth", "isMeasurement": true },1018"durationSinceUpdate" : { "classification": "SystemMetaData", "purpose": "FeatureInsight", "isMeasurement": true },1019"errorcode": { "classification": "CallstackOrException", "purpose": "PerformanceAndHealth" },1020"recommendationReason": { "retiredFromVersion": "1.23.0", "classification": "SystemMetaData", "purpose": "FeatureInsight", "isMeasurement": true },1021"verificationStatus" : { "classification": "SystemMetaData", "purpose": "FeatureInsight" },1022"source": { "classification": "SystemMetaData", "purpose": "FeatureInsight" },1023"${include}": [1024"${GalleryExtensionTelemetryData}"1025]1026}1027*/1028/* __GDPR__1029"extensionGallery:uninstall" : {1030"owner": "sandy081",1031"success": { "classification": "SystemMetaData", "purpose": "PerformanceAndHealth", "isMeasurement": true },1032"duration" : { "classification": "SystemMetaData", "purpose": "PerformanceAndHealth", "isMeasurement": true },1033"errorcode": { "classification": "CallstackOrException", "purpose": "PerformanceAndHealth" },1034"${include}": [1035"${GalleryExtensionTelemetryData}"1036]1037}1038*/1039/* __GDPR__1040"extensionGallery:update" : {1041"owner": "sandy081",1042"success": { "classification": "SystemMetaData", "purpose": "PerformanceAndHealth", "isMeasurement": true },1043"duration" : { "classification": "SystemMetaData", "purpose": "PerformanceAndHealth", "isMeasurement": true },1044"errorcode": { "classification": "CallstackOrException", "purpose": "PerformanceAndHealth" },1045"verificationStatus" : { "classification": "SystemMetaData", "purpose": "FeatureInsight" },1046"source": { "classification": "SystemMetaData", "purpose": "FeatureInsight" },1047"${include}": [1048"${GalleryExtensionTelemetryData}"1049]1050}1051*/1052telemetryService.publicLog(eventName, {1053...extensionData,1054source,1055duration,1056durationSinceUpdate,1057success: !error,1058errorcode: error?.code,1059verificationStatus: verificationStatus === ExtensionSignatureVerificationCode.Success ? 'Verified' : (verificationStatus ?? 'Unverified')1060});1061}10621063export abstract class AbstractExtensionTask<T> {10641065private readonly barrier = new Barrier();1066private cancellablePromise: CancelablePromise<T> | undefined;10671068async waitUntilTaskIsFinished(): Promise<T> {1069await this.barrier.wait();1070return this.cancellablePromise!;1071}10721073run(): Promise<T> {1074if (!this.cancellablePromise) {1075this.cancellablePromise = createCancelablePromise(token => this.doRun(token));1076}1077this.barrier.open();1078return this.cancellablePromise;1079}10801081cancel(): void {1082if (!this.cancellablePromise) {1083this.cancellablePromise = createCancelablePromise(token => {1084return new Promise((c, e) => {1085const disposable = token.onCancellationRequested(() => {1086disposable.dispose();1087e(new CancellationError());1088});1089});1090});1091this.barrier.open();1092}1093this.cancellablePromise.cancel();1094}10951096protected abstract doRun(token: CancellationToken): Promise<T>;1097}109810991100