提交 81f06c86 编写于 作者: M Matt Bierner

Split markdown security selector into own file

上级 183dfbc2
......@@ -11,14 +11,10 @@ import TelemetryReporter from 'vscode-extension-telemetry';
import { MarkdownEngine } from './markdownEngine';
import DocumentLinkProvider from './documentLinkProvider';
import MDDocumentSymbolProvider from './documentSymbolProvider';
import { MDDocumentContentProvider, getMarkdownUri, isMarkdownFile, ContentSecurityPolicyArbiter } from './previewContentProvider';
import { ExtensionContentSecurityPolicyArbiter, PreviewSecuritySelector } from './security';
import { MDDocumentContentProvider, getMarkdownUri, isMarkdownFile } from './previewContentProvider';
import { TableOfContentsProvider } from './tableOfContentsProvider';
import * as nls from 'vscode-nls';
const localize = nls.loadMessageBundle();
interface IPackageInfo {
name: string;
version: string;
......@@ -30,36 +26,6 @@ interface OpenDocumentLinkArgs {
fragment: string;
}
enum PreviewSecuritySelection {
None,
DisableEnhancedSecurityForWorkspace,
EnableEnhancedSecurityForWorkspace
}
interface PreviewSecurityPickItem extends vscode.QuickPickItem {
id: PreviewSecuritySelection;
}
class ExtensionContentSecurityPolicyArbiter implements ContentSecurityPolicyArbiter {
private readonly key = 'trusted_preview_workspace:';
constructor(
private globalState: vscode.Memento
) { }
public isEnhancedSecurityDisableForWorkspace(): boolean {
return this.globalState.get<boolean>(this.key + vscode.workspace.rootPath, false);
}
public addTrustedWorkspace(rootPath: string): Thenable<void> {
return this.globalState.update(this.key + rootPath, true);
}
public removeTrustedWorkspace(rootPath: string): Thenable<void> {
return this.globalState.update(this.key + rootPath, false);
}
}
const resolveExtensionResources = (extension: vscode.Extension<any>, stylePath: string): vscode.Uri => {
const resource = vscode.Uri.parse(stylePath);
if (resource.scheme) {
......@@ -82,7 +48,7 @@ export function activate(context: vscode.ExtensionContext) {
const contentProvider = new MDDocumentContentProvider(engine, context, cspArbiter);
const contentProviderRegistration = vscode.workspace.registerTextDocumentContentProvider('markdown', contentProvider);
const previewSecuritySelector = new PreviewSecuritySelector(cspArbiter, contentProvider);
if (vscode.workspace.getConfiguration('markdown').get('enableExperimentalExtensionApi', false)) {
for (const extension of vscode.extensions.all) {
const contributes = extension.packageJSON && extension.packageJSON.contributes;
......@@ -192,65 +158,7 @@ export function activate(context: vscode.ExtensionContext) {
}));
context.subscriptions.push(vscode.commands.registerCommand('markdown.showPreviewSecuritySelector', (resource: string | undefined) => {
const workspacePath = vscode.workspace.rootPath || resource;
if (!workspacePath) {
return;
}
let sourceUri: vscode.Uri | null = null;
if (resource) {
sourceUri = vscode.Uri.parse(decodeURIComponent(resource));
}
if (!sourceUri && vscode.window.activeTextEditor) {
const activeDocument = vscode.window.activeTextEditor.document;
if (activeDocument.uri.scheme === 'markdown') {
sourceUri = activeDocument.uri;
} else {
sourceUri = getMarkdownUri(activeDocument.uri);
}
}
vscode.window.showQuickPick<PreviewSecurityPickItem>(
[
{
id: PreviewSecuritySelection.EnableEnhancedSecurityForWorkspace,
label: localize(
'preview.showPreviewSecuritySelector.disallowScriptsForWorkspaceTitle',
'Disable script execution in markdown previews for this workspace'),
description: '',
detail: cspArbiter.isEnhancedSecurityDisableForWorkspace()
? ''
: localize('preview.showPreviewSecuritySelector.currentSelection', 'Current setting')
}, {
id: PreviewSecuritySelection.DisableEnhancedSecurityForWorkspace,
label: localize(
'preview.showPreviewSecuritySelector.allowScriptsForWorkspaceTitle',
'Enable script execution in markdown previews for this workspace'),
description: '',
detail: cspArbiter.isEnhancedSecurityDisableForWorkspace()
? localize('preview.showPreviewSecuritySelector.currentSelection', 'Current setting')
: ''
},
], {
placeHolder: localize('preview.showPreviewSecuritySelector.title', 'Change security settings for the Markdown preview'),
}).then(selection => {
if (!workspacePath) {
return false;
}
switch (selection && selection.id) {
case PreviewSecuritySelection.DisableEnhancedSecurityForWorkspace:
return cspArbiter.addTrustedWorkspace(workspacePath).then(() => true);
case PreviewSecuritySelection.EnableEnhancedSecurityForWorkspace:
return cspArbiter.removeTrustedWorkspace(workspacePath).then(() => true);
}
return false;
}).then(shouldUpdate => {
if (shouldUpdate && sourceUri) {
contentProvider.update(sourceUri);
}
});
previewSecuritySelector.showSecutitySelectorForWorkspace(resource);
}));
context.subscriptions.push(vscode.workspace.onDidSaveTextDocument(document => {
......
......@@ -14,6 +14,10 @@ const localize = nls.loadMessageBundle();
export interface ContentSecurityPolicyArbiter {
isEnhancedSecurityDisableForWorkspace(): boolean;
addTrustedWorkspace(rootPath: string): Thenable<void>;
removeTrustedWorkspace(rootPath: string): Thenable<void>;
}
const previewStrings = {
......
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import * as vscode from 'vscode';
import { ContentSecurityPolicyArbiter, getMarkdownUri, MDDocumentContentProvider } from './previewContentProvider';
import * as nls from 'vscode-nls';
const localize = nls.loadMessageBundle();
export class ExtensionContentSecurityPolicyArbiter implements ContentSecurityPolicyArbiter {
private readonly key = 'trusted_preview_workspace:';
constructor(
private globalState: vscode.Memento
) { }
public isEnhancedSecurityDisableForWorkspace(): boolean {
return this.globalState.get<boolean>(this.key + vscode.workspace.rootPath, false);
}
public addTrustedWorkspace(rootPath: string): Thenable<void> {
return this.globalState.update(this.key + rootPath, true);
}
public removeTrustedWorkspace(rootPath: string): Thenable<void> {
return this.globalState.update(this.key + rootPath, false);
}
}
enum PreviewSecuritySelection {
None,
DisableEnhancedSecurityForWorkspace,
EnableEnhancedSecurityForWorkspace
}
interface PreviewSecurityPickItem extends vscode.QuickPickItem {
id: PreviewSecuritySelection;
}
export class PreviewSecuritySelector {
public constructor(
private cspArbiter: ContentSecurityPolicyArbiter,
private contentProvider: MDDocumentContentProvider
) { }
public showSecutitySelectorForWorkspace(resource: string | undefined): void {
const workspacePath = vscode.workspace.rootPath || resource;
if (!workspacePath) {
return;
}
let sourceUri: vscode.Uri | null = null;
if (resource) {
sourceUri = vscode.Uri.parse(decodeURIComponent(resource));
}
if (!sourceUri && vscode.window.activeTextEditor) {
const activeDocument = vscode.window.activeTextEditor.document;
if (activeDocument.uri.scheme === 'markdown') {
sourceUri = activeDocument.uri;
} else {
sourceUri = getMarkdownUri(activeDocument.uri);
}
}
vscode.window.showQuickPick<PreviewSecurityPickItem>(
[
{
id: PreviewSecuritySelection.EnableEnhancedSecurityForWorkspace,
label: localize(
'preview.showPreviewSecuritySelector.disallowScriptsForWorkspaceTitle',
'Disable script execution in markdown previews for this workspace'),
description: '',
detail: this.cspArbiter.isEnhancedSecurityDisableForWorkspace()
? ''
: localize('preview.showPreviewSecuritySelector.currentSelection', 'Current setting')
}, {
id: PreviewSecuritySelection.DisableEnhancedSecurityForWorkspace,
label: localize(
'preview.showPreviewSecuritySelector.allowScriptsForWorkspaceTitle',
'Enable script execution in markdown previews for this workspace'),
description: '',
detail: this.cspArbiter.isEnhancedSecurityDisableForWorkspace()
? localize('preview.showPreviewSecuritySelector.currentSelection', 'Current setting')
: ''
},
], {
placeHolder: localize('preview.showPreviewSecuritySelector.title', 'Change security settings for the Markdown preview'),
}).then(selection => {
if (!workspacePath) {
return false;
}
switch (selection && selection.id) {
case PreviewSecuritySelection.DisableEnhancedSecurityForWorkspace:
return this.cspArbiter.addTrustedWorkspace(workspacePath).then(() => true);
case PreviewSecuritySelection.EnableEnhancedSecurityForWorkspace:
return this.cspArbiter.removeTrustedWorkspace(workspacePath).then(() => true);
}
return false;
}).then(shouldUpdate => {
if (shouldUpdate && sourceUri) {
this.contentProvider.update(sourceUri);
}
});
}
}
......@@ -2720,7 +2720,7 @@
},
{
"name": "meta.arrow.ts",
"begin": "(?x) (?:\n (?<!\\.|\\$)(\\basync)(?=\\s*[<(])\n) | ((?<![})\\]])\\s*\n (?=\n # sure shot arrow functions even if => is on new line\n (\n [(]\\s*\n (\n ([)]\\s*:) | # ():\n ([_$[:alpha:]][_$[:alnum:]]*\\s*:) | # [(]param:\n (\\.\\.\\.) # [(]...\n )\n ) |\n (\n [<]\\s*[_$[:alpha:]][_$[:alnum:]]*\\s+extends\\s*[^=>] # < typeparam extends \n ) |\n # arrow function possible to detect only with => on same line\n (\n (<([^<>]|\\<[^<>]+\\>)+>\\s*)? # typeparameters\n \\(([^()]|\\([^()]*\\))*\\) # parameteres\n (\\s*:\\s*(.)*)? # return type\n \\s*=> # arrow operator\n )\n )\n)",
"begin": "(?x) (?:\n (?<!\\.|\\$)(\\basync)(?=\\s*[<(])\n) | ((?<![})\\]])\\s*\n (?=\n # sure shot arrow functions even if => is on new line\n (\n [(]\\s*\n (\n ([)]\\s*:) | # ():\n ([_$[:alpha:]][_$[:alnum:]]*\\s*:) # [(]...\n )\n ) |\n (\n [<]\\s*[_$[:alpha:]][_$[:alnum:]]*\\s+extends\\s*[^=>] # < typeparam extends \n ) |\n # arrow function possible to detect only with => on same line\n (\n (<([^<>]|\\<[^<>]+\\>)+>\\s*)? # typeparameters\n \\(([^()]|\\([^()]*\\))*\\) # parameteres\n (\\s*:\\s*(.)*)? # return type\n \\s*=> # arrow operator\n )\n )\n)",
"beginCaptures": {
"1": {
"name": "storage.modifier.async.ts"
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册