From 4f9c9353fb790a5ea2b8867f3ff1018885a87824 Mon Sep 17 00:00:00 2001 From: Ladislau Szomoru <3372902+lszomoru@users.noreply.github.com> Date: Thu, 27 Jan 2022 10:14:49 +0100 Subject: [PATCH] Startup dialog goes through the request service (#141592) --- .../workspace/common/workspaceTrust.ts | 2 + .../browser/workspace.contribution.ts | 57 ++++++++++--------- .../workspaces/common/workspaceTrust.ts | 14 +++++ .../test/common/testWorkspaceTrustService.ts | 7 +++ 4 files changed, 54 insertions(+), 26 deletions(-) diff --git a/src/vs/platform/workspace/common/workspaceTrust.ts b/src/vs/platform/workspace/common/workspaceTrust.ts index 3a0803a5e28c3..0481b2b1a83bd 100644 --- a/src/vs/platform/workspace/common/workspaceTrust.ts +++ b/src/vs/platform/workspace/common/workspaceTrust.ts @@ -83,6 +83,7 @@ export interface IWorkspaceTrustRequestService { readonly onDidInitiateOpenFilesTrustRequest: Event; readonly onDidInitiateWorkspaceTrustRequest: Event; + readonly onDidInitiateWorkspaceTrustRequestOnStartup: Event; completeOpenFilesTrustRequest(result: WorkspaceTrustUriResponse, saveResponse?: boolean): Promise; requestOpenFilesTrust(openFiles: URI[]): Promise; @@ -90,6 +91,7 @@ export interface IWorkspaceTrustRequestService { cancelWorkspaceTrustRequest(): void; completeWorkspaceTrustRequest(trusted?: boolean): Promise; requestWorkspaceTrust(options?: WorkspaceTrustRequestOptions): Promise; + requestWorkspaceTrustOnStartup(): void; } export interface IWorkspaceTrustTransitionParticipant { diff --git a/src/vs/workbench/contrib/workspace/browser/workspace.contribution.ts b/src/vs/workbench/contrib/workspace/browser/workspace.contribution.ts index 2de1c94744e70..51e33205d89a6 100644 --- a/src/vs/workbench/contrib/workspace/browser/workspace.contribution.ts +++ b/src/vs/workbench/contrib/workspace/browser/workspace.contribution.ts @@ -307,6 +307,35 @@ export class WorkspaceTrustUXHandler extends Disposable implements IWorkbenchCon this._register(this.workspaceTrustManagementService.onDidChangeTrust(trusted => { this.updateWorkbenchIndicators(trusted); })); + + this._register(this.workspaceTrustRequestService.onDidInitiateWorkspaceTrustRequestOnStartup(() => { + 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); + if (this.workspaceTrustManagementService.canSetParentFolderTrust()) { + const { name } = splitName(splitName((workspaceIdentifier as ISingleFolderWorkspaceIdentifier).uri.fsPath).parentPath); + checkboxText = localize('checkboxString', "Trust the authors of all files in the parent folder '{0}'", name); + } + + // Show Workspace Trust Start Dialog + this.doShowModal( + 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") }, + [ + !isSingleFolderWorkspace ? + localize('workspaceStartupTrustDetails', "{0} provides features that may automatically execute files in this workspace.", this.productService.nameShort) : + localize('folderStartupTrustDetails', "{0} provides features that may automatically execute files in this folder.", this.productService.nameShort), + localize('startupTrustRequestLearnMore', "If you don't trust the authors of these files, we recommend to continue in restricted mode as the files may be malicious. See [our docs](https://aka.ms/vscode-workspace-trust) to learn more."), + `\`${this.labelService.getWorkspaceLabel(workspaceIdentifier, { verbose: true })}\``, + ], + checkboxText + ); + })); } private updateWorkbenchIndicators(trusted: boolean): void { @@ -404,32 +433,8 @@ 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); - if (this.workspaceTrustManagementService.canSetParentFolderTrust()) { - const { name } = splitName(splitName((workspaceIdentifier as ISingleFolderWorkspaceIdentifier).uri.fsPath).parentPath); - checkboxText = localize('checkboxString', "Trust the authors of all files in the parent folder '{0}'", name); - } - - // Show Workspace Trust Start Dialog - this.doShowModal( - 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") }, - [ - !isSingleFolderWorkspace ? - localize('workspaceStartupTrustDetails', "{0} provides features that may automatically execute files in this workspace.", this.productService.nameShort) : - localize('folderStartupTrustDetails', "{0} provides features that may automatically execute files in this folder.", this.productService.nameShort), - localize('startupTrustRequestLearnMore', "If you don't trust the authors of these files, we recommend to continue in restricted mode as the files may be malicious. See [our docs](https://aka.ms/vscode-workspace-trust) to learn more."), - `\`${this.labelService.getWorkspaceLabel(workspaceIdentifier, { verbose: true })}\``, - ], - checkboxText - ); + // Use the workspace trust request service to show modal dialog + this.workspaceTrustRequestService.requestWorkspaceTrustOnStartup(); } private get startupPromptSetting(): 'always' | 'once' | 'never' { diff --git a/src/vs/workbench/services/workspaces/common/workspaceTrust.ts b/src/vs/workbench/services/workspaces/common/workspaceTrust.ts index d326da2b0c504..77e979b89f942 100644 --- a/src/vs/workbench/services/workspaces/common/workspaceTrust.ts +++ b/src/vs/workbench/services/workspaces/common/workspaceTrust.ts @@ -651,6 +651,9 @@ export class WorkspaceTrustRequestService extends Disposable implements IWorkspa private readonly _onDidInitiateWorkspaceTrustRequest = this._register(new Emitter()); readonly onDidInitiateWorkspaceTrustRequest = this._onDidInitiateWorkspaceTrustRequest.event; + private readonly _onDidInitiateWorkspaceTrustRequestOnStartup = this._register(new Emitter()); + readonly onDidInitiateWorkspaceTrustRequestOnStartup = this._onDidInitiateWorkspaceTrustRequestOnStartup.event; + constructor( @IConfigurationService private readonly configurationService: IConfigurationService, @IWorkspaceTrustManagementService private readonly workspaceTrustManagementService: IWorkspaceTrustManagementService @@ -794,6 +797,17 @@ export class WorkspaceTrustRequestService extends Disposable implements IWorkspa return this._workspaceTrustRequestPromise; } + requestWorkspaceTrustOnStartup(): void { + if (!this._workspaceTrustRequestPromise) { + // Create promise + this._workspaceTrustRequestPromise = new Promise(resolve => { + this._workspaceTrustRequestResolver = resolve; + }); + } + + this._onDidInitiateWorkspaceTrustRequestOnStartup.fire(); + } + //#endregion } diff --git a/src/vs/workbench/services/workspaces/test/common/testWorkspaceTrustService.ts b/src/vs/workbench/services/workspaces/test/common/testWorkspaceTrustService.ts index 6abfd823a1b60..636b664aa6f4e 100644 --- a/src/vs/workbench/services/workspaces/test/common/testWorkspaceTrustService.ts +++ b/src/vs/workbench/services/workspaces/test/common/testWorkspaceTrustService.ts @@ -109,6 +109,9 @@ export class TestWorkspaceTrustRequestService implements IWorkspaceTrustRequestS private readonly _onDidInitiateWorkspaceTrustRequest = new Emitter(); readonly onDidInitiateWorkspaceTrustRequest = this._onDidInitiateWorkspaceTrustRequest.event; + private readonly _onDidInitiateWorkspaceTrustRequestOnStartup = new Emitter(); + readonly onDidInitiateWorkspaceTrustRequestOnStartup = this._onDidInitiateWorkspaceTrustRequestOnStartup.event; + constructor(private readonly _trusted: boolean) { } requestOpenUrisHandler = async (uris: URI[]) => { @@ -134,4 +137,8 @@ export class TestWorkspaceTrustRequestService implements IWorkspaceTrustRequestS async requestWorkspaceTrust(options?: WorkspaceTrustRequestOptions): Promise { return this._trusted; } + + requestWorkspaceTrustOnStartup(): void { + throw new Error('Method not implemented.'); + } }