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 { Disposable } from '../../../base/common/lifecycle.js';
7
>
import * as nls from '../../../nls.js';
8
>
import { IGalleryExtension, AllowedExtensionsConfigKey, IAllowedExtensionsService, AllowedExtensionsConfigValueType } from './extensionManagement.js';
9
>
import { ExtensionType, IExtension, TargetPlatform } from '../../extensions/common/extensions.js';
10
>
import { IProductService } from '../../product/common/productService.js';
11
>
import { createCommandUri, IMarkdownString, MarkdownString } from '../../../base/common/htmlContent.js';
12
>
import { IConfigurationService } from '../../configuration/common/configuration.js';
13
>
import { isBoolean, isObject, isUndefined } from '../../../base/common/types.js';
14
>
import { Emitter } from '../../../base/common/event.js';
15
>
16
function isGalleryExtension(extension: unknown): extension is IGalleryExtension {
17
return (extension as IGalleryExtension).type === 'gallery';
18
}
20
function isIExtension(extension: unknown): extension is IExtension {
21
return (extension as IExtension).type === ExtensionType.User || (extension as IExtension).type === ExtensionType.System;
22
}
24
>
25
>
const VersionRegex = /^(?<version>\d+\.\d+\.\d+(-.*)?)(@(?<platform>.+))?$/;
26
>
27
>
export class AllowedExtensionsService extends Disposable implements IAllowedExtensionsService {
28
>
29
>
_serviceBrand: undefined;
30
>
31
>
private readonly publisherOrgs: string[];
32
>
33
>
private _allowedExtensionsConfigValue: AllowedExtensionsConfigValueType | undefined;
34
>
get allowedExtensionsConfigValue(): AllowedExtensionsConfigValueType | undefined {
35
>
return this._allowedExtensionsConfigValue;
36
>
}
37
>
private _onDidChangeAllowedExtensions = this._register(new Emitter<void>());
38
>
readonly onDidChangeAllowedExtensionsConfigValue = this._onDidChangeAllowedExtensions.event;
39
>
40
>
constructor(
41
>
@IProductService productService: IProductService,
42
>
@IConfigurationService protected readonly configurationService: IConfigurationService
43
>
) {
44
>
super();
45
>
this.publisherOrgs = productService.extensionPublisherOrgs?.map(p => p.toLowerCase()) ?? [];
46
>
this._allowedExtensionsConfigValue = this.getAllowedExtensionsValue();
47
>
this._register(this.configurationService.onDidChangeConfiguration(e => {
48
if (e.affectsConfiguration(AllowedExtensionsConfigKey)) {
49
this._allowedExtensionsConfigValue = this.getAllowedExtensionsValue();
50
this._onDidChangeAllowedExtensions.fire();
51
}
53
>
}
54
>
55
>
private getAllowedExtensionsValue(): AllowedExtensionsConfigValueType | undefined {
56
>
const value = this.configurationService.getValue<AllowedExtensionsConfigValueType | undefined>(AllowedExtensionsConfigKey);
57
>
if (!isObject(value) || Array.isArray(value)) {
58
return undefined;
59
}
60
const entries = Object.entries(value).map(([key, value]) => [key.toLowerCase(), value]);
62
return undefined;
63
}
64
return Object.fromEntries(entries);
66
>
67
>
isAllowed(extension: IGalleryExtension | IExtension | { id: string; publisherDisplayName: string | undefined; version?: string; prerelease?: boolean; targetPlatform?: TargetPlatform }): true | IMarkdownString {
68
if (!this._allowedExtensionsConfigValue) {
69
return true;