Skip to content

Commit

Permalink
Startup dialog goes through the request service (#141592)
Browse files Browse the repository at this point in the history
  • Loading branch information
lszomoru authored Jan 27, 2022
1 parent b297fc9 commit 4f9c935
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 26 deletions.
2 changes: 2 additions & 0 deletions src/vs/platform/workspace/common/workspaceTrust.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,15 @@ export interface IWorkspaceTrustRequestService {

readonly onDidInitiateOpenFilesTrustRequest: Event<void>;
readonly onDidInitiateWorkspaceTrustRequest: Event<WorkspaceTrustRequestOptions | undefined>;
readonly onDidInitiateWorkspaceTrustRequestOnStartup: Event<void>;

completeOpenFilesTrustRequest(result: WorkspaceTrustUriResponse, saveResponse?: boolean): Promise<void>;
requestOpenFilesTrust(openFiles: URI[]): Promise<WorkspaceTrustUriResponse>;

cancelWorkspaceTrustRequest(): void;
completeWorkspaceTrustRequest(trusted?: boolean): Promise<void>;
requestWorkspaceTrust(options?: WorkspaceTrustRequestOptions): Promise<boolean | undefined>;
requestWorkspaceTrustOnStartup(): void;
}

export interface IWorkspaceTrustTransitionParticipant {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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' {
Expand Down
14 changes: 14 additions & 0 deletions src/vs/workbench/services/workspaces/common/workspaceTrust.ts
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,9 @@ export class WorkspaceTrustRequestService extends Disposable implements IWorkspa
private readonly _onDidInitiateWorkspaceTrustRequest = this._register(new Emitter<WorkspaceTrustRequestOptions | undefined>());
readonly onDidInitiateWorkspaceTrustRequest = this._onDidInitiateWorkspaceTrustRequest.event;

private readonly _onDidInitiateWorkspaceTrustRequestOnStartup = this._register(new Emitter<void>());
readonly onDidInitiateWorkspaceTrustRequestOnStartup = this._onDidInitiateWorkspaceTrustRequestOnStartup.event;

constructor(
@IConfigurationService private readonly configurationService: IConfigurationService,
@IWorkspaceTrustManagementService private readonly workspaceTrustManagementService: IWorkspaceTrustManagementService
Expand Down Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ export class TestWorkspaceTrustRequestService implements IWorkspaceTrustRequestS
private readonly _onDidInitiateWorkspaceTrustRequest = new Emitter<WorkspaceTrustRequestOptions>();
readonly onDidInitiateWorkspaceTrustRequest = this._onDidInitiateWorkspaceTrustRequest.event;

private readonly _onDidInitiateWorkspaceTrustRequestOnStartup = new Emitter<void>();
readonly onDidInitiateWorkspaceTrustRequestOnStartup = this._onDidInitiateWorkspaceTrustRequestOnStartup.event;

constructor(private readonly _trusted: boolean) { }

requestOpenUrisHandler = async (uris: URI[]) => {
Expand All @@ -134,4 +137,8 @@ export class TestWorkspaceTrustRequestService implements IWorkspaceTrustRequestS
async requestWorkspaceTrust(options?: WorkspaceTrustRequestOptions): Promise<boolean> {
return this._trusted;
}

requestWorkspaceTrustOnStartup(): void {
throw new Error('Method not implemented.');
}
}

0 comments on commit 4f9c935

Please sign in to comment.