From 72cbf699d82fc30b4e426e9f5a2b88ee5508552e Mon Sep 17 00:00:00 2001 From: Matt Bierner Date: Wed, 26 Aug 2020 16:21:19 -0700 Subject: [PATCH] Split out main thread webview panels into own class/file --- .../api/browser/extensionHost.contribution.ts | 2 +- .../api/browser/mainThreadCustomEditors.ts | 8 ++--- .../api/browser/mainThreadWebviewManager.ts | 35 +++++++++++++++++++ ...AndViews.ts => mainThreadWebviewPanels.ts} | 20 ++--------- .../workbench/api/common/extHost.protocol.ts | 4 +-- src/vs/workbench/api/common/extHostWebview.ts | 8 ++--- .../test/browser/api/extHostWebview.test.ts | 4 +-- 7 files changed, 50 insertions(+), 31 deletions(-) create mode 100644 src/vs/workbench/api/browser/mainThreadWebviewManager.ts rename src/vs/workbench/api/browser/{mainThreadWebviewPanelsAndViews.ts => mainThreadWebviewPanels.ts} (90%) diff --git a/src/vs/workbench/api/browser/extensionHost.contribution.ts b/src/vs/workbench/api/browser/extensionHost.contribution.ts index 5a9674d46bc..bfabf000891 100644 --- a/src/vs/workbench/api/browser/extensionHost.contribution.ts +++ b/src/vs/workbench/api/browser/extensionHost.contribution.ts @@ -54,7 +54,7 @@ import './mainThreadTreeViews'; import './mainThreadDownloadService'; import './mainThreadUrls'; import './mainThreadWindow'; -import './mainThreadWebviewPanelsAndViews'; +import './mainThreadWebviewManager'; import './mainThreadWorkspace'; import './mainThreadComments'; import './mainThreadNotebook'; diff --git a/src/vs/workbench/api/browser/mainThreadCustomEditors.ts b/src/vs/workbench/api/browser/mainThreadCustomEditors.ts index b3d73594e6f..9bd40eeb40d 100644 --- a/src/vs/workbench/api/browser/mainThreadCustomEditors.ts +++ b/src/vs/workbench/api/browser/mainThreadCustomEditors.ts @@ -19,7 +19,7 @@ import { IFileService } from 'vs/platform/files/common/files'; import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation'; import { ILabelService } from 'vs/platform/label/common/label'; import { IUndoRedoService, UndoRedoElementType } from 'vs/platform/undoRedo/common/undoRedo'; -import type { MainThreadWebviewPanelsAndViews } from 'vs/workbench/api/browser/mainThreadWebviewPanelsAndViews'; +import { MainThreadWebviewPanels } from 'vs/workbench/api/browser/mainThreadWebviewPanels'; import { MainThreadWebviews, reviveWebviewExtension } from 'vs/workbench/api/browser/mainThreadWebviews'; import * as extHostProtocol from 'vs/workbench/api/common/extHost.protocol'; import { editorGroupToViewColumn } from 'vs/workbench/api/common/shared/editor'; @@ -49,7 +49,7 @@ export class MainThreadCustomEditors extends Disposable implements extHostProtoc constructor( private readonly mainThreadWebview: MainThreadWebviews, - private readonly mainThreadWebviewPanelsAndViews: MainThreadWebviewPanelsAndViews, + private readonly mainThreadWebviewPanels: MainThreadWebviewPanels, context: extHostProtocol.IExtHostContext, @IWorkingCopyService workingCopyService: IWorkingCopyService, @IWorkingCopyFileService workingCopyFileService: IWorkingCopyFileService, @@ -121,7 +121,7 @@ export class MainThreadCustomEditors extends Disposable implements extHostProtoc const handle = webviewInput.id; const resource = webviewInput.resource; - this.mainThreadWebviewPanelsAndViews.addWebviewInput(handle, webviewInput); + this.mainThreadWebviewPanels.addWebviewInput(handle, webviewInput); webviewInput.webview.options = options; webviewInput.webview.extension = extension; @@ -210,7 +210,7 @@ export class MainThreadCustomEditors extends Disposable implements extHostProtoc case CustomEditorModelType.Custom: { const model = MainThreadCustomEditorModel.create(this._instantiationService, this._proxyCustomEditors, viewType, resource, options, () => { - return Array.from(this.mainThreadWebviewPanelsAndViews.webviewInputs) + return Array.from(this.mainThreadWebviewPanels.webviewInputs) .filter(editor => editor instanceof CustomEditorInput && isEqual(editor.resource, resource)) as CustomEditorInput[]; }, cancellation, this._backupService); return this._customEditorService.models.add(resource, viewType, model); diff --git a/src/vs/workbench/api/browser/mainThreadWebviewManager.ts b/src/vs/workbench/api/browser/mainThreadWebviewManager.ts new file mode 100644 index 00000000000..abe953dd010 --- /dev/null +++ b/src/vs/workbench/api/browser/mainThreadWebviewManager.ts @@ -0,0 +1,35 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +import { Disposable } from 'vs/base/common/lifecycle'; +import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation'; +import { MainThreadCustomEditors } from 'vs/workbench/api/browser/mainThreadCustomEditors'; +import { MainThreadWebviewPanels } from 'vs/workbench/api/browser/mainThreadWebviewPanels'; +import { MainThreadWebviews } from 'vs/workbench/api/browser/mainThreadWebviews'; +import { MainThreadWebviewsViews } from 'vs/workbench/api/browser/mainThreadWebviewViews'; +import * as extHostProtocol from 'vs/workbench/api/common/extHost.protocol'; +import { extHostCustomer } from '../common/extHostCustomers'; + +@extHostCustomer +export class MainThreadWebviewManager extends Disposable { + constructor( + context: extHostProtocol.IExtHostContext, + @IInstantiationService instantiationService: IInstantiationService, + ) { + super(); + + const webviews = this._register(instantiationService.createInstance(MainThreadWebviews, context)); + context.set(extHostProtocol.MainContext.MainThreadWebviews, webviews); + + const webviewPanels = this._register(instantiationService.createInstance(MainThreadWebviewPanels, webviews, context)); + context.set(extHostProtocol.MainContext.MainThreadWebviewPanels, webviewPanels); + + const customEditors = this._register(instantiationService.createInstance(MainThreadCustomEditors, webviews, webviewPanels, context)); + context.set(extHostProtocol.MainContext.MainThreadCustomEditors, customEditors); + + const webviewViews = this._register(instantiationService.createInstance(MainThreadWebviewsViews, webviews, context)); + context.set(extHostProtocol.MainContext.MainThreadWebviewViews, webviewViews); + } +} diff --git a/src/vs/workbench/api/browser/mainThreadWebviewPanelsAndViews.ts b/src/vs/workbench/api/browser/mainThreadWebviewPanels.ts similarity index 90% rename from src/vs/workbench/api/browser/mainThreadWebviewPanelsAndViews.ts rename to src/vs/workbench/api/browser/mainThreadWebviewPanels.ts index ea94413c303..b089b04b6dd 100644 --- a/src/vs/workbench/api/browser/mainThreadWebviewPanelsAndViews.ts +++ b/src/vs/workbench/api/browser/mainThreadWebviewPanels.ts @@ -6,11 +6,8 @@ import { onUnexpectedError } from 'vs/base/common/errors'; import { Disposable, DisposableStore, dispose, IDisposable } from 'vs/base/common/lifecycle'; import { URI, UriComponents } from 'vs/base/common/uri'; -import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation'; import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry'; -import { MainThreadCustomEditors } from 'vs/workbench/api/browser/mainThreadCustomEditors'; import { MainThreadWebviews, reviveWebviewExtension, reviveWebviewOptions } from 'vs/workbench/api/browser/mainThreadWebviews'; -import { MainThreadWebviewsViews } from 'vs/workbench/api/browser/mainThreadWebviewViews'; import * as extHostProtocol from 'vs/workbench/api/common/extHost.protocol'; import { editorGroupToViewColumn, EditorViewColumn, viewColumnToEditorGroup } from 'vs/workbench/api/common/shared/editor'; import { IEditorInput } from 'vs/workbench/common/editor'; @@ -22,7 +19,6 @@ import { ICreateWebViewShowOptions, IWebviewWorkbenchService, WebviewInputOption import { IEditorGroup, IEditorGroupsService } from 'vs/workbench/services/editor/common/editorGroupsService'; import { IEditorService } from 'vs/workbench/services/editor/common/editorService'; import { IExtensionService } from 'vs/workbench/services/extensions/common/extensions'; -import { extHostNamedCustomer } from '../common/extHostCustomers'; /** * Bi-directional map between webview handles and inputs. @@ -77,8 +73,7 @@ class WebviewViewTypeTransformer { } } -@extHostNamedCustomer(extHostProtocol.MainContext.MainThreadWebviewService) -export class MainThreadWebviewPanelsAndViews extends Disposable implements extHostProtocol.MainThreadWebviewPanelsAndViewsShape { +export class MainThreadWebviewPanels extends Disposable implements extHostProtocol.MainThreadWebviewPanelsShape { private readonly webviewPanelViewType = new WebviewViewTypeTransformer('mainThreadWebview-'); @@ -89,32 +84,21 @@ export class MainThreadWebviewPanelsAndViews extends Disposable implements extHo private readonly _editorProviders = new Map(); private readonly _webviewFromDiffEditorHandles = new Set(); - private readonly _mainThreadWebviews: MainThreadWebviews; - private readonly _revivers = new Map(); constructor( + private readonly _mainThreadWebviews: MainThreadWebviews, context: extHostProtocol.IExtHostContext, @IExtensionService extensionService: IExtensionService, @IEditorGroupsService private readonly _editorGroupService: IEditorGroupsService, @IEditorService private readonly _editorService: IEditorService, @ITelemetryService private readonly _telemetryService: ITelemetryService, @IWebviewWorkbenchService private readonly _webviewWorkbenchService: IWebviewWorkbenchService, - @IInstantiationService private readonly _instantiationService: IInstantiationService, ) { super(); this._proxy = context.getProxy(extHostProtocol.ExtHostContext.ExtHostWebviewPanels); - this._mainThreadWebviews = this._instantiationService.createInstance(MainThreadWebviews, context); - context.set(extHostProtocol.MainContext.MainThreadWebviews, this._mainThreadWebviews); - - const webviewViews = this._instantiationService.createInstance(MainThreadWebviewsViews, this._mainThreadWebviews, context); - context.set(extHostProtocol.MainContext.MainThreadWebviewViews, webviewViews); - - const customEditors = this._instantiationService.createInstance(MainThreadCustomEditors, this._mainThreadWebviews, this, context); - context.set(extHostProtocol.MainContext.MainThreadCustomEditors, customEditors); - this._register(_editorService.onDidActiveEditorChange(() => { const activeInput = this._editorService.activeEditor; if (activeInput instanceof DiffEditorInput && activeInput.primary instanceof WebviewInput && activeInput.secondary instanceof WebviewInput) { diff --git a/src/vs/workbench/api/common/extHost.protocol.ts b/src/vs/workbench/api/common/extHost.protocol.ts index 10ad9f81831..5156730c7f6 100644 --- a/src/vs/workbench/api/common/extHost.protocol.ts +++ b/src/vs/workbench/api/common/extHost.protocol.ts @@ -614,7 +614,7 @@ export interface MainThreadWebviewsShape extends IDisposable { $postMessage(handle: WebviewHandle, value: any): Promise } -export interface MainThreadWebviewPanelsAndViewsShape extends IDisposable { +export interface MainThreadWebviewPanelsShape extends IDisposable { $createWebviewPanel(extension: WebviewExtensionDescription, handle: WebviewHandle, viewType: string, title: string, showOptions: WebviewPanelShowOptions, options: modes.IWebviewPanelOptions & modes.IWebviewOptions): void; $disposeWebview(handle: WebviewHandle): void; $reveal(handle: WebviewHandle, showOptions: WebviewPanelShowOptions): void; @@ -1720,8 +1720,8 @@ export const MainContext = { MainThreadStorage: createMainId('MainThreadStorage'), MainThreadTelemetry: createMainId('MainThreadTelemetry'), MainThreadTerminalService: createMainId('MainThreadTerminalService'), - MainThreadWebviewService: createMainId('MainThreadWebviewService'), MainThreadWebviews: createMainId('MainThreadWebviews'), + MainThreadWebviewPanels: createMainId('MainThreadWebviewPanels'), MainThreadWebviewViews: createMainId('MainThreadWebviewViews'), MainThreadCustomEditors: createMainId('MainThreadCustomEditors'), MainThreadUrls: createMainId('MainThreadUrls'), diff --git a/src/vs/workbench/api/common/extHostWebview.ts b/src/vs/workbench/api/common/extHostWebview.ts index a7836032cd9..3d2673c7142 100644 --- a/src/vs/workbench/api/common/extHostWebview.ts +++ b/src/vs/workbench/api/common/extHostWebview.ts @@ -122,7 +122,7 @@ type IconPath = URI | { light: URI, dark: URI }; class ExtHostWebviewPanel extends Disposable implements vscode.WebviewPanel { readonly #handle: extHostProtocol.WebviewHandle; - readonly #proxy: extHostProtocol.MainThreadWebviewPanelsAndViewsShape; + readonly #proxy: extHostProtocol.MainThreadWebviewPanelsShape; readonly #viewType: string; readonly #webview: ExtHostWebview; @@ -143,7 +143,7 @@ class ExtHostWebviewPanel extends Disposable implements vscode.WebviewPanel { constructor( handle: extHostProtocol.WebviewHandle, - proxy: extHostProtocol.MainThreadWebviewPanelsAndViewsShape, + proxy: extHostProtocol.MainThreadWebviewPanelsShape, viewType: string, title: string, viewColumn: vscode.ViewColumn | undefined, @@ -269,7 +269,7 @@ export class ExtHostWebviews implements extHostProtocol.ExtHostWebviewsShape, ex return generateUuid(); } - private readonly _proxy: extHostProtocol.MainThreadWebviewPanelsAndViewsShape; + private readonly _proxy: extHostProtocol.MainThreadWebviewPanelsShape; private readonly _webviewProxy: extHostProtocol.MainThreadWebviewsShape; private readonly _webviews = new Map(); @@ -287,7 +287,7 @@ export class ExtHostWebviews implements extHostProtocol.ExtHostWebviewsShape, ex private readonly _logService: ILogService, private readonly _deprecationService: IExtHostApiDeprecationService, ) { - this._proxy = mainContext.getProxy(extHostProtocol.MainContext.MainThreadWebviewService); + this._proxy = mainContext.getProxy(extHostProtocol.MainContext.MainThreadWebviewPanels); this._webviewProxy = mainContext.getProxy(extHostProtocol.MainContext.MainThreadWebviews); } diff --git a/src/vs/workbench/test/browser/api/extHostWebview.test.ts b/src/vs/workbench/test/browser/api/extHostWebview.test.ts index 31e8bf7bacf..9e6194a99c7 100644 --- a/src/vs/workbench/test/browser/api/extHostWebview.test.ts +++ b/src/vs/workbench/test/browser/api/extHostWebview.test.ts @@ -8,7 +8,7 @@ import { URI } from 'vs/base/common/uri'; import { mock } from 'vs/base/test/common/mock'; import { IExtensionDescription } from 'vs/platform/extensions/common/extensions'; import { NullLogService } from 'vs/platform/log/common/log'; -import { MainThreadWebviewPanelsAndViews } from 'vs/workbench/api/browser/mainThreadWebviewPanelsAndViews'; +import { MainThreadWebviewManager } from 'vs/workbench/api/browser/mainThreadWebviewManager'; import { IExtHostContext } from 'vs/workbench/api/common/extHost.protocol'; import { NullApiDeprecationService } from 'vs/workbench/api/common/extHostApiDeprecationService'; import { IExtHostRpcService } from 'vs/workbench/api/common/extHostRpcService'; @@ -150,7 +150,7 @@ suite('ExtHostWebview', () => { function createNoopMainThreadWebviews() { - return new class extends mock() { + return new class extends mock() { $createWebviewPanel() { /* noop */ } $registerSerializer() { /* noop */ } $unregisterSerializer() { /* noop */ }