Skip to content

Commit

Permalink
💄 clean-up workspace contribution
Browse files Browse the repository at this point in the history
  • Loading branch information
lszomoru authored and meganrogge committed Jun 15, 2021
1 parent 537b1f0 commit 5d70c90
Showing 1 changed file with 84 additions and 75 deletions.
159 changes: 84 additions & 75 deletions src/vs/workbench/contrib/workspace/browser/workspace.contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,20 +221,65 @@ export class WorkspaceTrustUXHandler extends Disposable implements IWorkbenchCon
})();
}

private get startupPromptSetting(): 'always' | 'once' | 'never' {
return this.configurationService.getValue(WORKSPACE_TRUST_STARTUP_PROMPT);
}
private registerListeners(): void {
this._register(this.workspaceContextService.onWillChangeWorkspaceFolders(e => {
if (e.fromCache) {
return;
}
if (!this.workspaceTrustManagementService.workspaceTrustEnabled) {
return;
}
const trusted = this.workspaceTrustManagementService.isWorkpaceTrusted();

private get useWorkspaceLanguage(): boolean {
return !isSingleFolderWorkspaceIdentifier(toWorkspaceIdentifier(this.workspaceContextService.getWorkspace()));
return e.join(new Promise(async resolve => {
// Workspace is trusted and there are added/changed folders
if (trusted && (e.changes.added.length || e.changes.changed.length)) {
const addedFoldersTrustInfo = await Promise.all(e.changes.added.map(folder => this.workspaceTrustManagementService.getUriTrustInfo(folder.uri)));

if (!addedFoldersTrustInfo.map(info => info.trusted).every(trusted => trusted)) {
const result = await this.dialogService.show(
Severity.Info,
localize('addWorkspaceFolderMessage', "Do you trust the authors of the files in this folder?"),
[localize('yes', 'Yes'), localize('no', 'No')],
{
detail: localize('addWorkspaceFolderDetail', "You are adding files to a trusted workspace that are not currently trusted. Do you trust the authors of these new files?"),
cancelId: 1,
custom: { icon: Codicon.shield }
}
);

// Mark added/changed folders as trusted
await this.workspaceTrustManagementService.setUrisTrust(addedFoldersTrustInfo.map(i => i.uri), result.choice === 0);

resolve();
}
}

resolve();
}));
}));

this._register(this.workspaceTrustManagementService.onDidChangeTrust(trusted => {
this.updateWorkbenchIndicators(trusted);
}));
}

private get modalTitle(): string {
return this.useWorkspaceLanguage ?
localize('workspaceTrust', "Do you trust the authors of the files in this workspace?") :
localize('folderTrust', "Do you trust the authors of the files in this folder?");
private updateWorkbenchIndicators(trusted: boolean): void {
const bannerItem = this.getBannerItem(!trusted);

this.updateStatusbarEntry(trusted);

if (bannerItem) {
if (!trusted) {
this.bannerService.show(bannerItem);
} else {
this.bannerService.hide(BANNER_RESTRICTED_MODE);
}
}
}

//#region Dialog

private async doShowModal(question: string, trustedOption: { label: string, sublabel: string }, untrustedOption: { label: string, sublabel: string }, markdownStrings: string[], trustParentString?: string): Promise<void> {
const result = await this.dialogService.show(
Severity.Info,
Expand Down Expand Up @@ -310,6 +355,10 @@ export class WorkspaceTrustUXHandler extends Disposable implements IWorkbenchCon
return;
}

const title = this.useWorkspaceLanguage ?
localize('workspaceTrust', "Do you trust the authors of the files in this workspace?") :
localize('folderTrust', "Do you trust the authors of the files in this folder?");

let checkboxText: string | undefined;
const workspaceIdentifier = toWorkspaceIdentifier(this.workspaceContextService.getWorkspace())!;
const isSingleFolderWorkspace = isSingleFolderWorkspaceIdentifier(workspaceIdentifier);
Expand All @@ -321,7 +370,7 @@ export class WorkspaceTrustUXHandler extends Disposable implements IWorkbenchCon

// Show Workspace Trust Start Dialog
this.doShowModal(
this.modalTitle,
title,
{ label: localize('trustOption', "Yes, I trust the authors"), sublabel: isSingleFolderWorkspace ? localize('trustFolderOptionDescription', "Trust folder and enable all features") : localize('trustWorkspaceOptionDescription', "Trust workspace and enable all features") },
{ label: localize('dontTrustOption', "No, I don't trust the authors"), sublabel: isSingleFolderWorkspace ? localize('dontTrustFolderOptionDescription', "Browse folder in restricted mode") : localize('dontTrustWorkspaceOptionDescription', "Browse workspace in restricted mode") },
[
Expand All @@ -334,12 +383,18 @@ export class WorkspaceTrustUXHandler extends Disposable implements IWorkbenchCon
);
}

private createStatusbarEntry(): void {
const entry = this.getStatusbarEntry(this.workspaceTrustManagementService.isWorkpaceTrusted());
this.statusbarEntryAccessor.value = this.statusbarService.addEntry(entry, this.entryId, StatusbarAlignment.LEFT, 0.99 * Number.MAX_VALUE /* Right of remote indicator */);
this.statusbarService.updateEntryVisibility(this.entryId, false);
private get startupPromptSetting(): 'always' | 'once' | 'never' {
return this.configurationService.getValue(WORKSPACE_TRUST_STARTUP_PROMPT);
}

private get useWorkspaceLanguage(): boolean {
return !isSingleFolderWorkspaceIdentifier(toWorkspaceIdentifier(this.workspaceContextService.getWorkspace()));
}

//#endregion

//#region Banner

private getBannerItem(restrictedMode: boolean): IBannerItem | undefined {

const dismissedRestricted = this.storageService.getBoolean(BANNER_RESTRICTED_MODE_DISMISSED_KEY, StorageScope.WORKSPACE, false);
Expand Down Expand Up @@ -397,6 +452,16 @@ export class WorkspaceTrustUXHandler extends Disposable implements IWorkbenchCon
}
}

//#endregion

//#region Statusbar

private createStatusbarEntry(): void {
const entry = this.getStatusbarEntry(this.workspaceTrustManagementService.isWorkpaceTrusted());
this.statusbarEntryAccessor.value = this.statusbarService.addEntry(entry, this.entryId, StatusbarAlignment.LEFT, 0.99 * Number.MAX_VALUE /* Right of remote indicator */);
this.statusbarService.updateEntryVisibility(this.entryId, false);
}

private getStatusbarEntry(trusted: boolean): IStatusbarEntry {
const text = workspaceTrustToString(trusted);
const backgroundColor = new ThemeColor(STATUS_BAR_PROMINENT_ITEM_BACKGROUND);
Expand Down Expand Up @@ -465,75 +530,16 @@ export class WorkspaceTrustUXHandler extends Disposable implements IWorkbenchCon

private updateStatusbarEntry(trusted: boolean): void {
this.statusbarEntryAccessor.value?.update(this.getStatusbarEntry(trusted));
this.updateStatusbarEntryVisibility(trusted);
}

private updateStatusbarEntryVisibility(trusted: boolean): void {
this.statusbarService.updateEntryVisibility(this.entryId, !trusted);
}

private updateWorkbenchIndicators(trusted: boolean): void {
const bannerItem = this.getBannerItem(!trusted);

this.updateStatusbarEntry(trusted);

if (bannerItem) {
if (!trusted) {
this.bannerService.show(bannerItem);
} else {
this.bannerService.hide(BANNER_RESTRICTED_MODE);
}
}
}

private registerListeners(): void {

this._register(this.workspaceContextService.onWillChangeWorkspaceFolders(e => {
if (e.fromCache) {
return;
}
if (!this.workspaceTrustManagementService.workspaceTrustEnabled) {
return;
}
const trusted = this.workspaceTrustManagementService.isWorkpaceTrusted();

return e.join(new Promise(async resolve => {
// Workspace is trusted and there are added/changed folders
if (trusted && (e.changes.added.length || e.changes.changed.length)) {
const addedFoldersTrustInfo = await Promise.all(e.changes.added.map(folder => this.workspaceTrustManagementService.getUriTrustInfo(folder.uri)));

if (!addedFoldersTrustInfo.map(info => info.trusted).every(trusted => trusted)) {
const result = await this.dialogService.show(
Severity.Info,
localize('addWorkspaceFolderMessage', "Do you trust the authors of the files in this folder?"),
[localize('yes', 'Yes'), localize('no', 'No')],
{
detail: localize('addWorkspaceFolderDetail', "You are adding files to a trusted workspace that are not currently trusted. Do you trust the authors of these new files?"),
cancelId: 1,
custom: { icon: Codicon.shield }
}
);

// Mark added/changed folders as trusted
await this.workspaceTrustManagementService.setUrisTrust(addedFoldersTrustInfo.map(i => i.uri), result.choice === 0);

resolve();
}
}

resolve();
}));
}));

this._register(this.workspaceTrustManagementService.onDidChangeTrust(trusted => {
this.updateWorkbenchIndicators(trusted);
}));
}
//#endregion
}

Registry.as<IWorkbenchContributionsRegistry>(WorkbenchExtensions.Workbench).registerWorkbenchContribution(WorkspaceTrustRequestHandler, LifecyclePhase.Ready);
Registry.as<IWorkbenchContributionsRegistry>(WorkbenchExtensions.Workbench).registerWorkbenchContribution(WorkspaceTrustUXHandler, LifecyclePhase.Restored);


/**
* Trusted Workspace GUI Editor
*/
Expand Down Expand Up @@ -566,6 +572,7 @@ Registry.as<IEditorRegistry>(EditorExtensions.Editors).registerEditor(
]
);


/*
* Actions
*/
Expand Down Expand Up @@ -602,6 +609,7 @@ registerAction2(class extends Action2 {
}
});


/*
* Configuration
*/
Expand Down Expand Up @@ -656,6 +664,7 @@ Registry.as<IConfigurationRegistry>(ConfigurationExtensions.Configuration)
}
});


/**
* Telemetry
*/
Expand Down

0 comments on commit 5d70c90

Please sign in to comment.