From e259543b84471270c13e6ea46621b01bf58732d0 Mon Sep 17 00:00:00 2001 From: Matt Bierner Date: Thu, 7 Feb 2019 16:06:01 -0800 Subject: [PATCH] Split up static webview options and options that can be changed dynamically --- .../electron-browser/extensionEditor.ts | 9 ++-- .../html/electron-browser/htmlPreviewPart.ts | 8 ++-- .../webview/electron-browser/webviewEditor.ts | 9 ++-- .../electron-browser/webviewEditorInput.ts | 2 - .../electron-browser/webviewElement.ts | 47 +++++++++---------- 5 files changed, 36 insertions(+), 39 deletions(-) diff --git a/src/vs/workbench/contrib/extensions/electron-browser/extensionEditor.ts b/src/vs/workbench/contrib/extensions/electron-browser/extensionEditor.ts index d591085df81..0e03b0890ce 100644 --- a/src/vs/workbench/contrib/extensions/electron-browser/extensionEditor.ts +++ b/src/vs/workbench/contrib/extensions/electron-browser/extensionEditor.ts @@ -525,9 +525,12 @@ export class ExtensionEditor extends BaseEditor { .then(renderBody) .then(removeEmbeddedSVGs) .then(body => { - const allowedBadgeProviders = this.extensionsWorkbenchService.allowedBadgeProviders; - const webViewOptions = allowedBadgeProviders.length > 0 ? { allowScripts: false, allowSvgs: false, svgWhiteList: allowedBadgeProviders } : {}; - const wbeviewElement = this.instantiationService.createInstance(WebviewElement, this.partService.getContainer(Parts.EDITOR_PART), webViewOptions); + const wbeviewElement = this.instantiationService.createInstance(WebviewElement, + this.partService.getContainer(Parts.EDITOR_PART), + {}, + { + svgWhiteList: this.extensionsWorkbenchService.allowedBadgeProviders + }); wbeviewElement.mountTo(this.content); const removeLayoutParticipant = arrays.insert(this.layoutParticipants, wbeviewElement); this.contentDisposables.push(toDisposable(removeLayoutParticipant)); diff --git a/src/vs/workbench/contrib/html/electron-browser/htmlPreviewPart.ts b/src/vs/workbench/contrib/html/electron-browser/htmlPreviewPart.ts index fc54e558fff..8f86431edbb 100644 --- a/src/vs/workbench/contrib/html/electron-browser/htmlPreviewPart.ts +++ b/src/vs/workbench/contrib/html/electron-browser/htmlPreviewPart.ts @@ -17,7 +17,7 @@ import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey'; import { IStorageService } from 'vs/platform/storage/common/storage'; import { Dimension } from 'vs/base/browser/dom'; import { BaseWebviewEditor } from 'vs/workbench/contrib/webview/electron-browser/baseWebviewEditor'; -import { WebviewElement, WebviewOptions } from 'vs/workbench/contrib/webview/electron-browser/webviewElement'; +import { WebviewElement, WebviewContentOptions } from 'vs/workbench/contrib/webview/electron-browser/webviewElement'; import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation'; import { IEditorGroupsService, IEditorGroup } from 'vs/workbench/services/editor/common/editorGroupsService'; import { CancellationToken } from 'vs/base/common/cancellation'; @@ -88,7 +88,7 @@ export class HtmlPreviewPart extends BaseWebviewEditor { private get webview(): WebviewElement { if (!this._webview) { - let webviewOptions: WebviewOptions = {}; + let webviewOptions: WebviewContentOptions = {}; if (this.input && this.input instanceof HtmlInput) { webviewOptions = this.input.options; } @@ -96,9 +96,9 @@ export class HtmlPreviewPart extends BaseWebviewEditor { this._webview = this._instantiationService.createInstance(WebviewElement, this._partService.getContainer(Parts.EDITOR_PART), { - ...webviewOptions, useSameOriginForRoot: true - }); + }, + webviewOptions); this._webview.mountTo(this._content); if (this.input && this.input instanceof HtmlInput) { diff --git a/src/vs/workbench/contrib/webview/electron-browser/webviewEditor.ts b/src/vs/workbench/contrib/webview/electron-browser/webviewEditor.ts index db911b8e5a2..3db8dc9bd78 100644 --- a/src/vs/workbench/contrib/webview/electron-browser/webviewEditor.ts +++ b/src/vs/workbench/contrib/webview/electron-browser/webviewEditor.ts @@ -183,10 +183,7 @@ export class WebviewEditor extends BaseWebviewEditor { input.claimWebview(this); webview.update(input.html, { allowScripts: input.options.enableScripts, - allowSvgs: true, - useSameOriginForRoot: false, localResourceRoots: input.options.localResourceRoots || this.getDefaultLocalResourceRoots(), - extensionLocation: input.extensionLocation }, !!input.options.retainContextWhenHidden); if (this._webviewContent) { @@ -224,9 +221,9 @@ export class WebviewEditor extends BaseWebviewEditor { this._partService.getContainer(Parts.EDITOR_PART), { allowSvgs: true, - useSameOriginForRoot: false, - extensionLocation: input.extensionLocation - }); + extensionLocation: input.extensionLocation, + }, + {}); this._webview.mountTo(this._webviewContent); input.webview = this._webview; diff --git a/src/vs/workbench/contrib/webview/electron-browser/webviewEditorInput.ts b/src/vs/workbench/contrib/webview/electron-browser/webviewEditorInput.ts index d07c498efd5..09ec22e7ae0 100644 --- a/src/vs/workbench/contrib/webview/electron-browser/webviewEditorInput.ts +++ b/src/vs/workbench/contrib/webview/electron-browser/webviewEditorInput.ts @@ -207,8 +207,6 @@ export class WebviewEditorInput extends EditorInput { if (this._webview) { this._webview.options = { allowScripts: this._options.enableScripts, - allowSvgs: true, - useSameOriginForRoot: false, localResourceRoots: this._options.localResourceRoots }; } diff --git a/src/vs/workbench/contrib/webview/electron-browser/webviewElement.ts b/src/vs/workbench/contrib/webview/electron-browser/webviewElement.ts index d340251efac..79cda62b62d 100644 --- a/src/vs/workbench/contrib/webview/electron-browser/webviewElement.ts +++ b/src/vs/workbench/contrib/webview/electron-browser/webviewElement.ts @@ -21,14 +21,17 @@ import { endsWith } from 'vs/base/common/strings'; import { isMacintosh } from 'vs/base/common/platform'; export interface WebviewOptions { - readonly allowScripts?: boolean; readonly allowSvgs?: boolean; - readonly svgWhiteList?: string[]; readonly useSameOriginForRoot?: boolean; - readonly localResourceRoots?: ReadonlyArray; readonly extensionLocation?: URI; } +export interface WebviewContentOptions { + readonly allowScripts?: boolean; + readonly svgWhiteList?: string[]; + readonly localResourceRoots?: ReadonlyArray; +} + interface IKeydownEvent { key: string; keyCode: number; @@ -88,14 +91,10 @@ class SvgBlocker extends Disposable { constructor( webview: Electron.WebviewTag, - private readonly _options: WebviewOptions, + private readonly _options: WebviewContentOptions, ) { super(); - if (this._options.allowSvgs) { - return; - } - let loaded = false; this._register(addDisposableListener(webview, 'did-start-loading', () => { if (loaded) { @@ -134,9 +133,6 @@ class SvgBlocker extends Disposable { } private isAllowedSvg(uri: URI): boolean { - if (this._options.allowSvgs) { - return true; - } if (this._options.svgWhiteList) { return this._options.svgWhiteList.indexOf(uri.authority.toLowerCase()) >= 0; } @@ -239,7 +235,8 @@ export class WebviewElement extends Disposable { constructor( private readonly _styleElement: Element, - private _options: WebviewOptions, + private readonly _options: WebviewOptions, + private _contentOptions: WebviewContentOptions, @IInstantiationService instantiationService: IInstantiationService, @IThemeService private readonly _themeService: IThemeService, @IEnvironmentService environmentService: IEnvironmentService, @@ -276,12 +273,14 @@ export class WebviewElement extends Disposable { new WebviewProtocolProvider( this._webview, this._options.extensionLocation, - () => (this._options.localResourceRoots || []), + () => (this._contentOptions.localResourceRoots || []), environmentService, fileService)); - const svgBlocker = this._register(new SvgBlocker(this._webview, this._options)); - svgBlocker.onDidBlockSvg(() => this.onDidBlockSvg()); + if (!this._options.allowSvgs) { + const svgBlocker = this._register(new SvgBlocker(this._webview, this._contentOptions)); + svgBlocker.onDidBlockSvg(() => this.onDidBlockSvg()); + } this._register(new WebviewKeyboardHandler(this._webview, this._keybindingService)); @@ -397,15 +396,15 @@ export class WebviewElement extends Disposable { this._state = value; } - public set options(value: WebviewOptions) { - if (this._options && areWebviewInputOptionsEqual(value, this._options)) { + public set options(value: WebviewContentOptions) { + if (this._contentOptions && areWebviewInputOptionsEqual(value, this._contentOptions)) { return; } - this._options = value; + this._contentOptions = value; this._send('content', { contents: this._contents, - options: this._options, + options: this._contentOptions, state: this._state }); } @@ -414,20 +413,20 @@ export class WebviewElement extends Disposable { this._contents = value; this._send('content', { contents: value, - options: this._options, + options: this._contentOptions, state: this._state }); } - public update(value: string, options: WebviewOptions, retainContextWhenHidden: boolean) { - if (retainContextWhenHidden && value === this._contents && this._options && areWebviewInputOptionsEqual(options, this._options)) { + public update(value: string, options: WebviewContentOptions, retainContextWhenHidden: boolean) { + if (retainContextWhenHidden && value === this._contents && this._contentOptions && areWebviewInputOptionsEqual(options, this._contentOptions)) { return; } this._contents = value; - this._options = options; + this._contentOptions = options; this._send('content', { contents: this._contents, - options: this._options, + options: this._contentOptions, state: this._state }); } -- GitLab