Skip to content

Commit

Permalink
Run notebook webviews in consistent origins (#163956)
Browse files Browse the repository at this point in the history
  • Loading branch information
mjbvz authored Oct 18, 2022
1 parent b09701a commit b6f4df8
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 15 deletions.
6 changes: 3 additions & 3 deletions src/vs/workbench/api/browser/mainThreadWebviewPanels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { MainThreadWebviews, reviveWebviewContentOptions, reviveWebviewExtension
import * as extHostProtocol from 'vs/workbench/api/common/extHost.protocol';
import { DiffEditorInput } from 'vs/workbench/common/editor/diffEditorInput';
import { EditorInput } from 'vs/workbench/common/editor/editorInput';
import { WebviewOptions, WebviewOriginStore } from 'vs/workbench/contrib/webview/browser/webview';
import { ExtensionKeyedWebviewOriginStore, WebviewOptions } from 'vs/workbench/contrib/webview/browser/webview';
import { WebviewInput } from 'vs/workbench/contrib/webviewPanel/browser/webviewEditorInput';
import { WebviewIcons } from 'vs/workbench/contrib/webviewPanel/browser/webviewIconManager';
import { IWebViewShowOptions, IWebviewWorkbenchService } from 'vs/workbench/contrib/webviewPanel/browser/webviewWorkbenchService';
Expand Down Expand Up @@ -86,7 +86,7 @@ export class MainThreadWebviewPanels extends Disposable implements extHostProtoc

private readonly _revivers = this._register(new DisposableMap<string>());

private readonly webviewOriginStore: WebviewOriginStore;
private readonly webviewOriginStore: ExtensionKeyedWebviewOriginStore;

constructor(
context: IExtHostContext,
Expand All @@ -101,7 +101,7 @@ export class MainThreadWebviewPanels extends Disposable implements extHostProtoc
) {
super();

this.webviewOriginStore = new WebviewOriginStore('mainThreadWebviewPanel.origins', storageService);
this.webviewOriginStore = new ExtensionKeyedWebviewOriginStore('mainThreadWebviewPanel.origins', storageService);

this._proxy = context.getProxy(extHostProtocol.ExtHostContext.ExtHostWebviewPanels);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@

import { IMouseWheelEvent } from 'vs/base/browser/mouseEvent';
import { coalesce } from 'vs/base/common/arrays';
import { DeferredPromise } from 'vs/base/common/async';
import { decodeBase64 } from 'vs/base/common/buffer';
import { Emitter, Event } from 'vs/base/common/event';
import { Disposable } from 'vs/base/common/lifecycle';
import { getExtensionForMimeType } from 'vs/base/common/mime';
import { FileAccess, Schemas } from 'vs/base/common/network';
import { equals } from 'vs/base/common/objects';
import { isMacintosh, isWeb } from 'vs/base/common/platform';
import { dirname, joinPath } from 'vs/base/common/resources';
import { equals } from 'vs/base/common/objects';
import { URI } from 'vs/base/common/uri';
import * as UUID from 'vs/base/common/uuid';
import { TokenizationRegistry } from 'vs/editor/common/languages';
Expand All @@ -27,6 +28,7 @@ import { IContextMenuService } from 'vs/platform/contextview/browser/contextView
import { IFileDialogService } from 'vs/platform/dialogs/common/dialogs';
import { IFileService } from 'vs/platform/files/common/files';
import { IOpenerService, matchesScheme, matchesSomeScheme } from 'vs/platform/opener/common/opener';
import { IStorageService } from 'vs/platform/storage/common/storage';
import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
import { IWorkspaceTrustManagementService } from 'vs/platform/workspace/common/workspaceTrust';
import { asWebviewUri, webviewGenericCspSource } from 'vs/workbench/common/webview';
Expand All @@ -39,12 +41,11 @@ import { CellUri, INotebookRendererInfo, NotebookSetting, RendererMessagingSpec
import { INotebookKernel } from 'vs/workbench/contrib/notebook/common/notebookKernelService';
import { IScopedRendererMessaging } from 'vs/workbench/contrib/notebook/common/notebookRendererMessagingService';
import { INotebookService } from 'vs/workbench/contrib/notebook/common/notebookService';
import { IWebviewElement, IWebviewService, WebviewContentPurpose } from 'vs/workbench/contrib/webview/browser/webview';
import { IWebviewElement, IWebviewService, WebviewContentPurpose, WebviewOriginStore } from 'vs/workbench/contrib/webview/browser/webview';
import { WebviewWindowDragMonitor } from 'vs/workbench/contrib/webview/browser/webviewWindowDragMonitor';
import { IEditorGroupsService } from 'vs/workbench/services/editor/common/editorGroupsService';
import { IWorkbenchEnvironmentService } from 'vs/workbench/services/environment/common/environmentService';
import { FromWebviewMessage, IAckOutputHeight, IClickedDataUrlMessage, ICodeBlockHighlightRequest, IContentWidgetTopRequest, IControllerPreload, ICreationContent, ICreationRequestMessage, IFindMatch, IMarkupCellInitialization, RendererMetadata, StaticPreloadMetadata, ToWebviewMessage } from './webviewMessages';
import { DeferredPromise } from 'vs/base/common/async';

export interface ICachedInset<K extends ICommonCellInfo> {
outputId: string;
Expand Down Expand Up @@ -98,6 +99,14 @@ interface BacklayerWebviewOptions {
}

export class BackLayerWebView<T extends ICommonCellInfo> extends Disposable {

private static _originStore?: WebviewOriginStore;

private static getOriginStore(storageService: IStorageService): WebviewOriginStore {
this._originStore ??= new WebviewOriginStore('notebook.backlayerWebview.origins', storageService);
return this._originStore;
}

element: HTMLElement;
webview: IWebviewElement | undefined = undefined;
insetMapping: Map<IDisplayOutputViewModel, ICachedInset<T>> = new Map();
Expand Down Expand Up @@ -139,6 +148,7 @@ export class BackLayerWebView<T extends ICommonCellInfo> extends Disposable {
@ILanguageService private readonly languageService: ILanguageService,
@IWorkspaceContextService private readonly workspaceContextService: IWorkspaceContextService,
@IEditorGroupsService private readonly editorGroupService: IEditorGroupsService,
@IStorageService private readonly storageService: IStorageService,
) {
super();

Expand Down Expand Up @@ -912,6 +922,7 @@ var requirejs = (function() {
this.localResourceRootsCache = this._getResourceRootsCache();
const webview = webviewService.createWebviewElement({
id: this.id,
origin: BackLayerWebView.getOriginStore(this.storageService).getOrigin(this.notebookViewType, undefined),
options: {
purpose: WebviewContentPurpose.NotebookRenderer,
enableFindWidget: false,
Expand Down
31 changes: 26 additions & 5 deletions src/vs/workbench/contrib/webview/browser/webview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ export interface IOverlayWebview extends IWebview {
/**
* Stores the unique origins for a webview.
*
* These are randomly generated, but keyed on extension and webview viewType.
* These are randomly generated
*/
export class WebviewOriginStore {

Expand All @@ -315,8 +315,8 @@ export class WebviewOriginStore {
this.state = this.memento.getMemento(StorageScope.APPLICATION, StorageTarget.MACHINE);
}

public getOrigin(viewType: string, extId: ExtensionIdentifier): string {
const key = this.getKey(viewType, extId);
public getOrigin(viewType: string, additionalKey: string | undefined): string {
const key = this.getKey(viewType, additionalKey);

const existing = this.state[key];
if (existing && typeof existing === 'string') {
Expand All @@ -329,7 +329,28 @@ export class WebviewOriginStore {
return newOrigin;
}

private getKey(viewType: string, extId: ExtensionIdentifier): string {
return JSON.stringify({ viewType, extension: extId.value });
private getKey(viewType: string, additionalKey: string | undefined): string {
return JSON.stringify({ viewType, key: additionalKey });
}
}

/**
* Stores the unique origins for a webview.
*
* These are randomly generated, but keyed on extension and webview viewType.
*/
export class ExtensionKeyedWebviewOriginStore {

private readonly store: WebviewOriginStore;

constructor(
rootStorageKey: string,
@IStorageService storageService: IStorageService,
) {
this.store = new WebviewOriginStore(rootStorageKey, storageService);
}

public getOrigin(viewType: string, extId: ExtensionIdentifier): string {
return this.store.getOrigin(viewType, extId.value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { ViewPane } from 'vs/workbench/browser/parts/views/viewPane';
import { IViewletViewOptions } from 'vs/workbench/browser/parts/views/viewsViewlet';
import { Memento, MementoObject } from 'vs/workbench/common/memento';
import { IViewBadge, IViewDescriptorService, IViewsService } from 'vs/workbench/common/views';
import { IOverlayWebview, IWebviewService, WebviewContentPurpose, WebviewOriginStore } from 'vs/workbench/contrib/webview/browser/webview';
import { ExtensionKeyedWebviewOriginStore, IOverlayWebview, IWebviewService, WebviewContentPurpose } from 'vs/workbench/contrib/webview/browser/webview';
import { WebviewWindowDragMonitor } from 'vs/workbench/contrib/webview/browser/webviewWindowDragMonitor';
import { IWebviewViewService, WebviewView } from 'vs/workbench/contrib/webviewView/browser/webviewViewService';
import { IActivityService, NumberBadge } from 'vs/workbench/services/activity/common/activity';
Expand All @@ -39,10 +39,10 @@ const storageKeys = {

export class WebviewViewPane extends ViewPane {

private static _originStore?: WebviewOriginStore;
private static _originStore?: ExtensionKeyedWebviewOriginStore;

private static getOriginStore(storageService: IStorageService): WebviewOriginStore {
this._originStore ??= new WebviewOriginStore('webviewViews.origins', storageService);
private static getOriginStore(storageService: IStorageService): ExtensionKeyedWebviewOriginStore {
this._originStore ??= new ExtensionKeyedWebviewOriginStore('webviewViews.origins', storageService);
return this._originStore;
}

Expand Down

0 comments on commit b6f4df8

Please sign in to comment.