src/vs/platform/extensionManagement/common/extensionEnablementService.ts
164 LOC · 90 covered · 74 uncovered · 27 ranges · 585 concepts · 4 introducers · 348 tests
File neighbourhood
The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.
Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file
In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.
Graph controls are ready.
Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.
/*---------------------------------------------------------------------------------------------
abstractSynchronizer.ts ×49
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Emitter, Event } from '../../../base/common/event.js';
import { Disposable } from '../../../base/common/lifecycle.js';
import { isUndefinedOrNull } from '../../../base/common/types.js';
import { DISABLED_EXTENSIONS_STORAGE_PATH, IExtensionIdentifier, IExtensionManagementService, IGlobalExtensionEnablementService, InstallOperation } from './extensionManagement.js';
import { areSameExtensions } from './extensionManagementUtil.js';
import { IProfileStorageValueChangeEvent, IStorageService, StorageScope, StorageTarget } from '../../storage/common/storage.js';
export class GlobalExtensionEnablementService extends Disposable implements IGlobalExtensionEnablementService {
declare readonly _serviceBrand: undefined;
private _onDidChangeEnablement = this._register(new Emitter<{ readonly extensions: IExtensionIdentifier[]; readonly source?: string }>());
readonly onDidChangeEnablement: Event<{ readonly extensions: IExtensionIdentifier[]; readonly source?: string }> = this._onDidChangeEnablement.event;
private readonly storageManager: StorageManager;
constructor(
@IExtensionManagementService extensionManagementService: IExtensionManagementService,
) {
super();
this.storageManager = this._register(new StorageManager(storageService));
this._register(this.storageManager.onDidChange(extensions => this._onDidChangeEnablement.fire({ extensions, source: 'storage' })));
this._register(extensionManagementService.onDidInstallExtensions(e => e.forEach(({ local, operation }) => {
if (local && operation === InstallOperation.Migrate) {
this._removeFromDisabledExtensions(local.identifier); /* Reset migrated extensions */
}
}
async enableExtension(extension: IExtensionIdentifier, source?: string): Promise<boolean> {
if (this._removeFromDisabledExtensions(extension)) {
this._onDidChangeEnablement.fire({ extensions: [extension], source });
return true;
}
return false;
}
async disableExtension(extension: IExtensionIdentifier, source?: string): Promise<boolean> {
if (this._addToDisabledExtensions(extension)) {
this._onDidChangeEnablement.fire({ extensions: [extension], source });
return true;
}
return false;
}
getDisabledExtensions(): IExtensionIdentifier[] {
}
async getDisabledExtensionsAsync(): Promise<IExtensionIdentifier[]> {
return this.getDisabledExtensions();
}
private _addToDisabledExtensions(identifier: IExtensionIdentifier): boolean {
const disabledExtensions = this.getDisabledExtensions();
if (disabledExtensions.every(e => !areSameExtensions(e, identifier))) {
disabledExtensions.push(identifier);
this._setDisabledExtensions(disabledExtensions);
return true;
}
return false;
}
private _removeFromDisabledExtensions(identifier: IExtensionIdentifier): boolean {
const disabledExtensions = this.getDisabledExtensions();
for (let index = 0; index < disabledExtensions.length; index++) {
const disabledExtension = disabledExtensions[index];
if (areSameExtensions(disabledExtension, identifier)) {
disabledExtensions.splice(index, 1);
this._setDisabledExtensions(disabledExtensions);
return true;
}
}
return false;
}
private _setDisabledExtensions(disabledExtensions: IExtensionIdentifier[]): void {
this._setExtensions(DISABLED_EXTENSIONS_STORAGE_PATH, disabledExtensions);
}
private _getExtensions(storageId: string): IExtensionIdentifier[] {
}
private _setExtensions(storageId: string, extensions: IExtensionIdentifier[]): void {
this.storageManager.set(storageId, extensions, StorageScope.PROFILE);
}
}
export class StorageManager extends Disposable {
private storage: { [key: string]: string } = Object.create(null);
private _onDidChange: Emitter<IExtensionIdentifier[]> = this._register(new Emitter<IExtensionIdentifier[]>());
readonly onDidChange: Event<IExtensionIdentifier[]> = this._onDidChange.event;
constructor(private storageService: IStorageService) {
this._register(storageService.onDidChangeValue(StorageScope.PROFILE, undefined, this._store)(e => this.onDidStorageChange(e)));
}
get(key: string, scope: StorageScope): IExtensionIdentifier[] {
if (scope === StorageScope.PROFILE) {
if (isUndefinedOrNull(this.storage[key])) {
this.storage[key] = this._get(key, scope);
}
value = this.storage[key];
} else {
value = this._get(key, scope);
}
}
set(key: string, value: IExtensionIdentifier[], scope: StorageScope): void {
const newValue: string = JSON.stringify(value.map(({ id, uuid }): IExtensionIdentifier => ({ id, uuid })));
const oldValue = this._get(key, scope);
if (oldValue !== newValue) {
if (scope === StorageScope.PROFILE) {
if (value.length) {
this.storage[key] = newValue;
} else {
delete this.storage[key];
}
}
this._set(key, value.length ? newValue : undefined, scope);
}
}
private onDidStorageChange(storageChangeEvent: IProfileStorageValueChangeEvent): void {
const newValue = this._get(storageChangeEvent.key, storageChangeEvent.scope);
if (newValue !== this.storage[storageChangeEvent.key]) {
const oldValues = this.get(storageChangeEvent.key, storageChangeEvent.scope);
delete this.storage[storageChangeEvent.key];
const newValues = this.get(storageChangeEvent.key, storageChangeEvent.scope);
const added = oldValues.filter(oldValue => !newValues.some(newValue => areSameExtensions(oldValue, newValue)));
const removed = newValues.filter(newValue => !oldValues.some(oldValue => areSameExtensions(oldValue, newValue)));
if (added.length || removed.length) {
this._onDidChange.fire([...added, ...removed]);
}
}
}
private _get(key: string, scope: StorageScope): string {
}
private _set(key: string, value: string | undefined, scope: StorageScope): void {
if (value) {
// Enablement state is synced separately through extensions
this.storageService.store(key, value, scope, StorageTarget.MACHINE);
} else {
this.storageService.remove(key, scope);
}
}