-
Notifications
You must be signed in to change notification settings - Fork 29.4k
/
extensionEnablementService.ts
760 lines (646 loc) · 34.3 KB
/
extensionEnablementService.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { localize } from '../../../../nls.js';
import { Event, Emitter } from '../../../../base/common/event.js';
import { Disposable, toDisposable } from '../../../../base/common/lifecycle.js';
import { IExtensionManagementService, IExtensionIdentifier, IGlobalExtensionEnablementService, ENABLED_EXTENSIONS_STORAGE_PATH, DISABLED_EXTENSIONS_STORAGE_PATH, InstallOperation } from '../../../../platform/extensionManagement/common/extensionManagement.js';
import { IWorkbenchExtensionEnablementService, EnablementState, IExtensionManagementServerService, IWorkbenchExtensionManagementService, IExtensionManagementServer, ExtensionInstallLocation } from '../common/extensionManagement.js';
import { areSameExtensions, BetterMergeId, getExtensionDependencies } from '../../../../platform/extensionManagement/common/extensionManagementUtil.js';
import { IWorkspaceContextService, WorkbenchState } from '../../../../platform/workspace/common/workspace.js';
import { IStorageService, StorageScope } from '../../../../platform/storage/common/storage.js';
import { IWorkbenchEnvironmentService } from '../../environment/common/environmentService.js';
import { IExtension, isAuthenticationProviderExtension, isLanguagePackExtension, isResolverExtension } from '../../../../platform/extensions/common/extensions.js';
import { IConfigurationService } from '../../../../platform/configuration/common/configuration.js';
import { InstantiationType, registerSingleton } from '../../../../platform/instantiation/common/extensions.js';
import { StorageManager } from '../../../../platform/extensionManagement/common/extensionEnablementService.js';
import { webWorkerExtHostConfig, WebWorkerExtHostConfigValue } from '../../extensions/common/extensions.js';
import { IUserDataSyncAccountService } from '../../../../platform/userDataSync/common/userDataSyncAccount.js';
import { IUserDataSyncEnablementService } from '../../../../platform/userDataSync/common/userDataSync.js';
import { ILifecycleService, LifecyclePhase } from '../../lifecycle/common/lifecycle.js';
import { INotificationService, NotificationPriority, Severity } from '../../../../platform/notification/common/notification.js';
import { IHostService } from '../../host/browser/host.js';
import { IExtensionBisectService } from './extensionBisect.js';
import { IWorkspaceTrustManagementService, IWorkspaceTrustRequestService } from '../../../../platform/workspace/common/workspaceTrust.js';
import { IExtensionManifestPropertiesService } from '../../extensions/common/extensionManifestPropertiesService.js';
import { isVirtualWorkspace } from '../../../../platform/workspace/common/virtualWorkspace.js';
import { ILogService } from '../../../../platform/log/common/log.js';
import { IInstantiationService } from '../../../../platform/instantiation/common/instantiation.js';
const SOURCE = 'IWorkbenchExtensionEnablementService';
type WorkspaceType = { readonly virtual: boolean; readonly trusted: boolean };
export class ExtensionEnablementService extends Disposable implements IWorkbenchExtensionEnablementService {
declare readonly _serviceBrand: undefined;
private readonly _onEnablementChanged = new Emitter<readonly IExtension[]>();
public readonly onEnablementChanged: Event<readonly IExtension[]> = this._onEnablementChanged.event;
protected readonly extensionsManager: ExtensionsManager;
private readonly storageManager: StorageManager;
private extensionsDisabledByExtensionDependency: IExtension[] = [];
constructor(
@IStorageService storageService: IStorageService,
@IGlobalExtensionEnablementService protected readonly globalExtensionEnablementService: IGlobalExtensionEnablementService,
@IWorkspaceContextService private readonly contextService: IWorkspaceContextService,
@IWorkbenchEnvironmentService private readonly environmentService: IWorkbenchEnvironmentService,
@IExtensionManagementService extensionManagementService: IExtensionManagementService,
@IConfigurationService private readonly configurationService: IConfigurationService,
@IExtensionManagementServerService private readonly extensionManagementServerService: IExtensionManagementServerService,
@IUserDataSyncEnablementService private readonly userDataSyncEnablementService: IUserDataSyncEnablementService,
@IUserDataSyncAccountService private readonly userDataSyncAccountService: IUserDataSyncAccountService,
@ILifecycleService private readonly lifecycleService: ILifecycleService,
@INotificationService private readonly notificationService: INotificationService,
@IHostService hostService: IHostService,
@IExtensionBisectService private readonly extensionBisectService: IExtensionBisectService,
@IWorkspaceTrustManagementService private readonly workspaceTrustManagementService: IWorkspaceTrustManagementService,
@IWorkspaceTrustRequestService private readonly workspaceTrustRequestService: IWorkspaceTrustRequestService,
@IExtensionManifestPropertiesService private readonly extensionManifestPropertiesService: IExtensionManifestPropertiesService,
@IInstantiationService instantiationService: IInstantiationService,
) {
super();
this.storageManager = this._register(new StorageManager(storageService));
const uninstallDisposable = this._register(Event.filter(extensionManagementService.onDidUninstallExtension, e => !e.error)(({ identifier }) => this._reset(identifier)));
let isDisposed = false;
this._register(toDisposable(() => isDisposed = true));
this.extensionsManager = this._register(instantiationService.createInstance(ExtensionsManager));
this.extensionsManager.whenInitialized().then(() => {
if (!isDisposed) {
this._onDidChangeExtensions([], [], false);
this._register(this.extensionsManager.onDidChangeExtensions(({ added, removed, isProfileSwitch }) => this._onDidChangeExtensions(added, removed, isProfileSwitch)));
uninstallDisposable.dispose();
}
});
this._register(this.globalExtensionEnablementService.onDidChangeEnablement(({ extensions, source }) => this._onDidChangeGloballyDisabledExtensions(extensions, source)));
// delay notification for extensions disabled until workbench restored
if (this.allUserExtensionsDisabled) {
this.lifecycleService.when(LifecyclePhase.Eventually).then(() => {
this.notificationService.prompt(Severity.Info, localize('extensionsDisabled', "All installed extensions are temporarily disabled."), [{
label: localize('Reload', "Reload and Enable Extensions"),
run: () => hostService.reload({ disableExtensions: false })
}], {
sticky: true,
priority: NotificationPriority.URGENT
});
});
}
}
private get hasWorkspace(): boolean {
return this.contextService.getWorkbenchState() !== WorkbenchState.EMPTY;
}
private get allUserExtensionsDisabled(): boolean {
return this.environmentService.disableExtensions === true;
}
getEnablementState(extension: IExtension): EnablementState {
return this._computeEnablementState(extension, this.extensionsManager.extensions, this.getWorkspaceType());
}
getEnablementStates(extensions: IExtension[], workspaceTypeOverrides: Partial<WorkspaceType> = {}): EnablementState[] {
const extensionsEnablements = new Map<IExtension, EnablementState>();
const workspaceType = { ...this.getWorkspaceType(), ...workspaceTypeOverrides };
return extensions.map(extension => this._computeEnablementState(extension, extensions, workspaceType, extensionsEnablements));
}
getDependenciesEnablementStates(extension: IExtension): [IExtension, EnablementState][] {
return getExtensionDependencies(this.extensionsManager.extensions, extension).map(e => [e, this.getEnablementState(e)]);
}
canChangeEnablement(extension: IExtension): boolean {
try {
this.throwErrorIfCannotChangeEnablement(extension);
return true;
} catch (error) {
return false;
}
}
canChangeWorkspaceEnablement(extension: IExtension): boolean {
if (!this.canChangeEnablement(extension)) {
return false;
}
try {
this.throwErrorIfCannotChangeWorkspaceEnablement(extension);
return true;
} catch (error) {
return false;
}
}
private throwErrorIfCannotChangeEnablement(extension: IExtension, donotCheckDependencies?: boolean): void {
if (isLanguagePackExtension(extension.manifest)) {
throw new Error(localize('cannot disable language pack extension', "Cannot change enablement of {0} extension because it contributes language packs.", extension.manifest.displayName || extension.identifier.id));
}
if (this.userDataSyncEnablementService.isEnabled() && this.userDataSyncAccountService.account &&
isAuthenticationProviderExtension(extension.manifest) && extension.manifest.contributes!.authentication!.some(a => a.id === this.userDataSyncAccountService.account!.authenticationProviderId)) {
throw new Error(localize('cannot disable auth extension', "Cannot change enablement {0} extension because Settings Sync depends on it.", extension.manifest.displayName || extension.identifier.id));
}
if (this._isEnabledInEnv(extension)) {
throw new Error(localize('cannot change enablement environment', "Cannot change enablement of {0} extension because it is enabled in environment", extension.manifest.displayName || extension.identifier.id));
}
this.throwErrorIfEnablementStateCannotBeChanged(extension, this.getEnablementState(extension), donotCheckDependencies);
}
private throwErrorIfEnablementStateCannotBeChanged(extension: IExtension, enablementStateOfExtension: EnablementState, donotCheckDependencies?: boolean): void {
switch (enablementStateOfExtension) {
case EnablementState.DisabledByEnvironment:
throw new Error(localize('cannot change disablement environment', "Cannot change enablement of {0} extension because it is disabled in environment", extension.manifest.displayName || extension.identifier.id));
case EnablementState.DisabledByVirtualWorkspace:
throw new Error(localize('cannot change enablement virtual workspace', "Cannot change enablement of {0} extension because it does not support virtual workspaces", extension.manifest.displayName || extension.identifier.id));
case EnablementState.DisabledByExtensionKind:
throw new Error(localize('cannot change enablement extension kind', "Cannot change enablement of {0} extension because of its extension kind", extension.manifest.displayName || extension.identifier.id));
case EnablementState.DisabledByInvalidExtension:
throw new Error(localize('cannot change invalid extension enablement', "Cannot change enablement of {0} extension because of it is invalid", extension.manifest.displayName || extension.identifier.id));
case EnablementState.DisabledByExtensionDependency:
if (donotCheckDependencies) {
break;
}
// Can be changed only when all its dependencies enablements can be changed
for (const dependency of getExtensionDependencies(this.extensionsManager.extensions, extension)) {
if (this.isEnabled(dependency)) {
continue;
}
throw new Error(localize('cannot change enablement dependency', "Cannot enable '{0}' extension because it depends on '{1}' extension that cannot be enabled", extension.manifest.displayName || extension.identifier.id, dependency.manifest.displayName || dependency.identifier.id));
}
}
}
private throwErrorIfCannotChangeWorkspaceEnablement(extension: IExtension): void {
if (!this.hasWorkspace) {
throw new Error(localize('noWorkspace', "No workspace."));
}
if (isAuthenticationProviderExtension(extension.manifest)) {
throw new Error(localize('cannot disable auth extension in workspace', "Cannot change enablement of {0} extension in workspace because it contributes authentication providers", extension.manifest.displayName || extension.identifier.id));
}
}
async setEnablement(extensions: IExtension[], newState: EnablementState): Promise<boolean[]> {
await this.extensionsManager.whenInitialized();
if (newState === EnablementState.EnabledGlobally || newState === EnablementState.EnabledWorkspace) {
extensions.push(...this.getExtensionsToEnableRecursively(extensions, this.extensionsManager.extensions, newState, { dependencies: true, pack: true }));
}
const workspace = newState === EnablementState.DisabledWorkspace || newState === EnablementState.EnabledWorkspace;
for (const extension of extensions) {
if (workspace) {
this.throwErrorIfCannotChangeWorkspaceEnablement(extension);
} else {
this.throwErrorIfCannotChangeEnablement(extension);
}
}
const result: boolean[] = [];
for (const extension of extensions) {
const enablementState = this.getEnablementState(extension);
if (enablementState === EnablementState.DisabledByTrustRequirement
/* All its disabled dependencies are disabled by Trust Requirement */
|| (enablementState === EnablementState.DisabledByExtensionDependency && this.getDependenciesEnablementStates(extension).every(([, e]) => this.isEnabledEnablementState(e) || e === EnablementState.DisabledByTrustRequirement))
) {
const trustState = await this.workspaceTrustRequestService.requestWorkspaceTrust();
result.push(trustState ?? false);
} else {
result.push(await this._setUserEnablementState(extension, newState));
}
}
const changedExtensions = extensions.filter((e, index) => result[index]);
if (changedExtensions.length) {
this._onEnablementChanged.fire(changedExtensions);
}
return result;
}
private getExtensionsToEnableRecursively(extensions: IExtension[], allExtensions: ReadonlyArray<IExtension>, enablementState: EnablementState, options: { dependencies: boolean; pack: boolean }, checked: IExtension[] = []): IExtension[] {
if (!options.dependencies && !options.pack) {
return [];
}
const toCheck = extensions.filter(e => checked.indexOf(e) === -1);
if (!toCheck.length) {
return [];
}
for (const extension of toCheck) {
checked.push(extension);
}
const extensionsToEnable: IExtension[] = [];
for (const extension of allExtensions) {
// Extension is already checked
if (checked.some(e => areSameExtensions(e.identifier, extension.identifier))) {
continue;
}
const enablementStateOfExtension = this.getEnablementState(extension);
// Extension is enabled
if (this.isEnabledEnablementState(enablementStateOfExtension)) {
continue;
}
// Skip if dependency extension is disabled by extension kind
if (enablementStateOfExtension === EnablementState.DisabledByExtensionKind) {
continue;
}
// Check if the extension is a dependency or in extension pack
if (extensions.some(e =>
(options.dependencies && e.manifest.extensionDependencies?.some(id => areSameExtensions({ id }, extension.identifier)))
|| (options.pack && e.manifest.extensionPack?.some(id => areSameExtensions({ id }, extension.identifier))))) {
const index = extensionsToEnable.findIndex(e => areSameExtensions(e.identifier, extension.identifier));
// Extension is not added to the disablement list so add it
if (index === -1) {
extensionsToEnable.push(extension);
}
// Extension is there already in the disablement list.
else {
try {
// Replace only if the enablement state can be changed
this.throwErrorIfEnablementStateCannotBeChanged(extension, enablementStateOfExtension, true);
extensionsToEnable.splice(index, 1, extension);
} catch (error) { /*Do not add*/ }
}
}
}
if (extensionsToEnable.length) {
extensionsToEnable.push(...this.getExtensionsToEnableRecursively(extensionsToEnable, allExtensions, enablementState, options, checked));
}
return extensionsToEnable;
}
private _setUserEnablementState(extension: IExtension, newState: EnablementState): Promise<boolean> {
const currentState = this._getUserEnablementState(extension.identifier);
if (currentState === newState) {
return Promise.resolve(false);
}
switch (newState) {
case EnablementState.EnabledGlobally:
this._enableExtension(extension.identifier);
break;
case EnablementState.DisabledGlobally:
this._disableExtension(extension.identifier);
break;
case EnablementState.EnabledWorkspace:
this._enableExtensionInWorkspace(extension.identifier);
break;
case EnablementState.DisabledWorkspace:
this._disableExtensionInWorkspace(extension.identifier);
break;
}
return Promise.resolve(true);
}
isEnabled(extension: IExtension): boolean {
const enablementState = this.getEnablementState(extension);
return this.isEnabledEnablementState(enablementState);
}
isEnabledEnablementState(enablementState: EnablementState): boolean {
return enablementState === EnablementState.EnabledByEnvironment || enablementState === EnablementState.EnabledWorkspace || enablementState === EnablementState.EnabledGlobally;
}
isDisabledGlobally(extension: IExtension): boolean {
return this._isDisabledGlobally(extension.identifier);
}
private _computeEnablementState(extension: IExtension, extensions: ReadonlyArray<IExtension>, workspaceType: WorkspaceType, computedEnablementStates?: Map<IExtension, EnablementState>): EnablementState {
computedEnablementStates = computedEnablementStates ?? new Map<IExtension, EnablementState>();
let enablementState = computedEnablementStates.get(extension);
if (enablementState !== undefined) {
return enablementState;
}
enablementState = this._getUserEnablementState(extension.identifier);
const isEnabled = this.isEnabledEnablementState(enablementState);
if (isEnabled && !extension.isValid) {
enablementState = EnablementState.DisabledByInvalidExtension;
}
else if (this.extensionBisectService.isDisabledByBisect(extension)) {
enablementState = EnablementState.DisabledByEnvironment;
}
else if (this._isDisabledInEnv(extension)) {
enablementState = EnablementState.DisabledByEnvironment;
}
else if (this._isDisabledByVirtualWorkspace(extension, workspaceType)) {
enablementState = EnablementState.DisabledByVirtualWorkspace;
}
else if (isEnabled && this._isDisabledByWorkspaceTrust(extension, workspaceType)) {
enablementState = EnablementState.DisabledByTrustRequirement;
}
else if (this._isDisabledByExtensionKind(extension)) {
enablementState = EnablementState.DisabledByExtensionKind;
}
else if (isEnabled && this._isDisabledByExtensionDependency(extension, extensions, workspaceType, computedEnablementStates)) {
enablementState = EnablementState.DisabledByExtensionDependency;
}
else if (!isEnabled && this._isEnabledInEnv(extension)) {
enablementState = EnablementState.EnabledByEnvironment;
}
computedEnablementStates.set(extension, enablementState);
return enablementState;
}
private _isDisabledInEnv(extension: IExtension): boolean {
if (this.allUserExtensionsDisabled) {
return !extension.isBuiltin && !isResolverExtension(extension.manifest, this.environmentService.remoteAuthority);
}
const disabledExtensions = this.environmentService.disableExtensions;
if (Array.isArray(disabledExtensions)) {
return disabledExtensions.some(id => areSameExtensions({ id }, extension.identifier));
}
// Check if this is the better merge extension which was migrated to a built-in extension
if (areSameExtensions({ id: BetterMergeId.value }, extension.identifier)) {
return true;
}
return false;
}
private _isEnabledInEnv(extension: IExtension): boolean {
const enabledExtensions = this.environmentService.enableExtensions;
if (Array.isArray(enabledExtensions)) {
return enabledExtensions.some(id => areSameExtensions({ id }, extension.identifier));
}
return false;
}
private _isDisabledByVirtualWorkspace(extension: IExtension, workspaceType: WorkspaceType): boolean {
// Not a virtual workspace
if (!workspaceType.virtual) {
return false;
}
// Supports virtual workspace
if (this.extensionManifestPropertiesService.getExtensionVirtualWorkspaceSupportType(extension.manifest) !== false) {
return false;
}
// Web extension from web extension management server
if (this.extensionManagementServerService.getExtensionManagementServer(extension) === this.extensionManagementServerService.webExtensionManagementServer && this.extensionManifestPropertiesService.canExecuteOnWeb(extension.manifest)) {
return false;
}
return true;
}
private _isDisabledByExtensionKind(extension: IExtension): boolean {
if (this.extensionManagementServerService.remoteExtensionManagementServer || this.extensionManagementServerService.webExtensionManagementServer) {
const installLocation = this.extensionManagementServerService.getExtensionInstallLocation(extension);
for (const extensionKind of this.extensionManifestPropertiesService.getExtensionKind(extension.manifest)) {
if (extensionKind === 'ui') {
if (installLocation === ExtensionInstallLocation.Local) {
return false;
}
}
if (extensionKind === 'workspace') {
if (installLocation === ExtensionInstallLocation.Remote) {
return false;
}
}
if (extensionKind === 'web') {
if (this.extensionManagementServerService.webExtensionManagementServer /* web */) {
if (installLocation === ExtensionInstallLocation.Web || installLocation === ExtensionInstallLocation.Remote) {
return false;
}
} else if (installLocation === ExtensionInstallLocation.Local) {
const enableLocalWebWorker = this.configurationService.getValue<WebWorkerExtHostConfigValue>(webWorkerExtHostConfig);
if (enableLocalWebWorker === true || enableLocalWebWorker === 'auto') {
// Web extensions are enabled on all configurations
return false;
}
}
}
}
return true;
}
return false;
}
private _isDisabledByWorkspaceTrust(extension: IExtension, workspaceType: WorkspaceType): boolean {
if (workspaceType.trusted) {
return false;
}
if (this.contextService.isInsideWorkspace(extension.location)) {
return true;
}
return this.extensionManifestPropertiesService.getExtensionUntrustedWorkspaceSupportType(extension.manifest) === false;
}
private _isDisabledByExtensionDependency(extension: IExtension, extensions: ReadonlyArray<IExtension>, workspaceType: WorkspaceType, computedEnablementStates: Map<IExtension, EnablementState>): boolean {
if (!extension.manifest.extensionDependencies) {
return false;
}
// Find dependency that is from the same server or does not exports any API
const dependencyExtensions = extensions.filter(e =>
extension.manifest.extensionDependencies?.some(id => areSameExtensions(e.identifier, { id })
&& (this.extensionManagementServerService.getExtensionManagementServer(e) === this.extensionManagementServerService.getExtensionManagementServer(extension) || ((e.manifest.main || e.manifest.browser) && e.manifest.api === 'none'))));
if (!dependencyExtensions.length) {
return false;
}
const hasEnablementState = computedEnablementStates.has(extension);
if (!hasEnablementState) {
// Placeholder to handle cyclic deps
computedEnablementStates.set(extension, EnablementState.EnabledGlobally);
}
try {
for (const dependencyExtension of dependencyExtensions) {
const enablementState = this._computeEnablementState(dependencyExtension, extensions, workspaceType, computedEnablementStates);
if (!this.isEnabledEnablementState(enablementState) && enablementState !== EnablementState.DisabledByExtensionKind) {
return true;
}
}
} finally {
if (!hasEnablementState) {
// remove the placeholder
computedEnablementStates.delete(extension);
}
}
return false;
}
private _getUserEnablementState(identifier: IExtensionIdentifier): EnablementState {
if (this.hasWorkspace) {
if (this._getWorkspaceEnabledExtensions().filter(e => areSameExtensions(e, identifier))[0]) {
return EnablementState.EnabledWorkspace;
}
if (this._getWorkspaceDisabledExtensions().filter(e => areSameExtensions(e, identifier))[0]) {
return EnablementState.DisabledWorkspace;
}
}
if (this._isDisabledGlobally(identifier)) {
return EnablementState.DisabledGlobally;
}
return EnablementState.EnabledGlobally;
}
private _isDisabledGlobally(identifier: IExtensionIdentifier): boolean {
return this.globalExtensionEnablementService.getDisabledExtensions().some(e => areSameExtensions(e, identifier));
}
private _enableExtension(identifier: IExtensionIdentifier): Promise<boolean> {
this._removeFromWorkspaceDisabledExtensions(identifier);
this._removeFromWorkspaceEnabledExtensions(identifier);
return this.globalExtensionEnablementService.enableExtension(identifier, SOURCE);
}
private _disableExtension(identifier: IExtensionIdentifier): Promise<boolean> {
this._removeFromWorkspaceDisabledExtensions(identifier);
this._removeFromWorkspaceEnabledExtensions(identifier);
return this.globalExtensionEnablementService.disableExtension(identifier, SOURCE);
}
private _enableExtensionInWorkspace(identifier: IExtensionIdentifier): void {
this._removeFromWorkspaceDisabledExtensions(identifier);
this._addToWorkspaceEnabledExtensions(identifier);
}
private _disableExtensionInWorkspace(identifier: IExtensionIdentifier): void {
this._addToWorkspaceDisabledExtensions(identifier);
this._removeFromWorkspaceEnabledExtensions(identifier);
}
private _addToWorkspaceDisabledExtensions(identifier: IExtensionIdentifier): Promise<boolean> {
if (!this.hasWorkspace) {
return Promise.resolve(false);
}
const disabledExtensions = this._getWorkspaceDisabledExtensions();
if (disabledExtensions.every(e => !areSameExtensions(e, identifier))) {
disabledExtensions.push(identifier);
this._setDisabledExtensions(disabledExtensions);
return Promise.resolve(true);
}
return Promise.resolve(false);
}
private async _removeFromWorkspaceDisabledExtensions(identifier: IExtensionIdentifier): Promise<boolean> {
if (!this.hasWorkspace) {
return false;
}
const disabledExtensions = this._getWorkspaceDisabledExtensions();
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 _addToWorkspaceEnabledExtensions(identifier: IExtensionIdentifier): boolean {
if (!this.hasWorkspace) {
return false;
}
const enabledExtensions = this._getWorkspaceEnabledExtensions();
if (enabledExtensions.every(e => !areSameExtensions(e, identifier))) {
enabledExtensions.push(identifier);
this._setEnabledExtensions(enabledExtensions);
return true;
}
return false;
}
private _removeFromWorkspaceEnabledExtensions(identifier: IExtensionIdentifier): boolean {
if (!this.hasWorkspace) {
return false;
}
const enabledExtensions = this._getWorkspaceEnabledExtensions();
for (let index = 0; index < enabledExtensions.length; index++) {
const disabledExtension = enabledExtensions[index];
if (areSameExtensions(disabledExtension, identifier)) {
enabledExtensions.splice(index, 1);
this._setEnabledExtensions(enabledExtensions);
return true;
}
}
return false;
}
protected _getWorkspaceEnabledExtensions(): IExtensionIdentifier[] {
return this._getExtensions(ENABLED_EXTENSIONS_STORAGE_PATH);
}
private _setEnabledExtensions(enabledExtensions: IExtensionIdentifier[]): void {
this._setExtensions(ENABLED_EXTENSIONS_STORAGE_PATH, enabledExtensions);
}
protected _getWorkspaceDisabledExtensions(): IExtensionIdentifier[] {
return this._getExtensions(DISABLED_EXTENSIONS_STORAGE_PATH);
}
private _setDisabledExtensions(disabledExtensions: IExtensionIdentifier[]): void {
this._setExtensions(DISABLED_EXTENSIONS_STORAGE_PATH, disabledExtensions);
}
private _getExtensions(storageId: string): IExtensionIdentifier[] {
if (!this.hasWorkspace) {
return [];
}
return this.storageManager.get(storageId, StorageScope.WORKSPACE);
}
private _setExtensions(storageId: string, extensions: IExtensionIdentifier[]): void {
this.storageManager.set(storageId, extensions, StorageScope.WORKSPACE);
}
private async _onDidChangeGloballyDisabledExtensions(extensionIdentifiers: ReadonlyArray<IExtensionIdentifier>, source?: string): Promise<void> {
if (source !== SOURCE) {
await this.extensionsManager.whenInitialized();
const extensions = this.extensionsManager.extensions.filter(installedExtension => extensionIdentifiers.some(identifier => areSameExtensions(identifier, installedExtension.identifier)));
this._onEnablementChanged.fire(extensions);
}
}
private _onDidChangeExtensions(added: ReadonlyArray<IExtension>, removed: ReadonlyArray<IExtension>, isProfileSwitch: boolean): void {
const changedExtensions: IExtension[] = added.filter(e => !this.isEnabledEnablementState(this.getEnablementState(e)));
const existingExtensionsDisabledByExtensionDependency = this.extensionsDisabledByExtensionDependency;
this.extensionsDisabledByExtensionDependency = this.extensionsManager.extensions.filter(extension => this.getEnablementState(extension) === EnablementState.DisabledByExtensionDependency);
for (const extension of existingExtensionsDisabledByExtensionDependency) {
if (this.extensionsDisabledByExtensionDependency.every(e => !areSameExtensions(e.identifier, extension.identifier))) {
changedExtensions.push(extension);
}
}
for (const extension of this.extensionsDisabledByExtensionDependency) {
if (existingExtensionsDisabledByExtensionDependency.every(e => !areSameExtensions(e.identifier, extension.identifier))) {
changedExtensions.push(extension);
}
}
if (changedExtensions.length) {
this._onEnablementChanged.fire(changedExtensions);
}
if (!isProfileSwitch) {
removed.forEach(({ identifier }) => this._reset(identifier));
}
}
public async updateExtensionsEnablementsWhenWorkspaceTrustChanges(): Promise<void> {
await this.extensionsManager.whenInitialized();
const computeEnablementStates = (workspaceType: WorkspaceType): [IExtension, EnablementState][] => {
const extensionsEnablements = new Map<IExtension, EnablementState>();
return this.extensionsManager.extensions.map(extension => [extension, this._computeEnablementState(extension, this.extensionsManager.extensions, workspaceType, extensionsEnablements)]);
};
const workspaceType = this.getWorkspaceType();
const enablementStatesWithTrustedWorkspace = computeEnablementStates({ ...workspaceType, trusted: true });
const enablementStatesWithUntrustedWorkspace = computeEnablementStates({ ...workspaceType, trusted: false });
const enablementChangedExtensionsBecauseOfTrust = enablementStatesWithTrustedWorkspace.filter(([, enablementState], index) => enablementState !== enablementStatesWithUntrustedWorkspace[index][1]).map(([extension]) => extension);
if (enablementChangedExtensionsBecauseOfTrust.length) {
this._onEnablementChanged.fire(enablementChangedExtensionsBecauseOfTrust);
}
}
private getWorkspaceType(): WorkspaceType {
return { trusted: this.workspaceTrustManagementService.isWorkspaceTrusted(), virtual: isVirtualWorkspace(this.contextService.getWorkspace()) };
}
private _reset(extension: IExtensionIdentifier) {
this._removeFromWorkspaceDisabledExtensions(extension);
this._removeFromWorkspaceEnabledExtensions(extension);
this.globalExtensionEnablementService.enableExtension(extension);
}
}
class ExtensionsManager extends Disposable {
private _extensions: IExtension[] = [];
get extensions(): readonly IExtension[] { return this._extensions; }
private _onDidChangeExtensions = this._register(new Emitter<{ added: readonly IExtension[]; removed: readonly IExtension[]; readonly isProfileSwitch: boolean }>());
readonly onDidChangeExtensions = this._onDidChangeExtensions.event;
private readonly initializePromise;
private disposed: boolean = false;
constructor(
@IWorkbenchExtensionManagementService private readonly extensionManagementService: IWorkbenchExtensionManagementService,
@IExtensionManagementServerService private readonly extensionManagementServerService: IExtensionManagementServerService,
@ILogService private readonly logService: ILogService
) {
super();
this._register(toDisposable(() => this.disposed = true));
this.initializePromise = this.initialize();
}
whenInitialized(): Promise<void> {
return this.initializePromise;
}
private async initialize(): Promise<void> {
try {
this._extensions = [
...await this.extensionManagementService.getInstalled(),
...await this.extensionManagementService.getInstalledWorkspaceExtensions(true)
];
if (this.disposed) {
return;
}
this._onDidChangeExtensions.fire({ added: this.extensions, removed: [], isProfileSwitch: false });
} catch (error) {
this.logService.error(error);
}
this._register(this.extensionManagementService.onDidInstallExtensions(e =>
this.updateExtensions(e.reduce<IExtension[]>((result, { local, operation }) => {
if (local && operation !== InstallOperation.Migrate) { result.push(local); } return result;
}, []), [], undefined, false)));
this._register(Event.filter(this.extensionManagementService.onDidUninstallExtension, (e => !e.error))(e => this.updateExtensions([], [e.identifier], e.server, false)));
this._register(this.extensionManagementService.onDidChangeProfile(({ added, removed, server }) => {
this.updateExtensions(added, removed.map(({ identifier }) => identifier), server, true);
}));
}
private updateExtensions(added: IExtension[], identifiers: IExtensionIdentifier[], server: IExtensionManagementServer | undefined, isProfileSwitch: boolean): void {
if (added.length) {
for (const extension of added) {
const extensionServer = this.extensionManagementServerService.getExtensionManagementServer(extension);
const index = this._extensions.findIndex(e => areSameExtensions(e.identifier, extension.identifier) && this.extensionManagementServerService.getExtensionManagementServer(e) === extensionServer);
if (index !== -1) {
this._extensions.splice(index, 1);
}
}
this._extensions.push(...added);
}
const removed: IExtension[] = [];
for (const identifier of identifiers) {
const index = this._extensions.findIndex(e => areSameExtensions(e.identifier, identifier) && this.extensionManagementServerService.getExtensionManagementServer(e) === server);
if (index !== -1) {
removed.push(...this._extensions.splice(index, 1));
}
}
if (added.length || removed.length) {
this._onDidChangeExtensions.fire({ added, removed, isProfileSwitch });
}
}
}
registerSingleton(IWorkbenchExtensionEnablementService, ExtensionEnablementService, InstantiationType.Delayed);