diff --git a/src/vs/platform/browserView/electron-main/browserViewMainService.ts b/src/vs/platform/browserView/electron-main/browserViewMainService.ts index 85666ec7c0b41..c6337ba35c14a 100644 --- a/src/vs/platform/browserView/electron-main/browserViewMainService.ts +++ b/src/vs/platform/browserView/electron-main/browserViewMainService.ts @@ -61,12 +61,6 @@ export class BrowserViewMainService extends Disposable implements IBrowserViewMa @IApplicationStorageMainService private readonly applicationStorageMainService: IApplicationStorageMainService ) { super(); - - this._register(this.windowsMainService.onDidDestroyWindow(window => { - if (this._perWindowTrustedRoots.delete(window.id)) { - this._recomputeTrustedFileRoots(); - } - })); } async getOrCreateBrowserView(id: string, options: IBrowserViewCreateOptions): Promise { @@ -329,10 +323,28 @@ export class BrowserViewMainService extends Disposable implements IBrowserViewMa async updateTrustedFileRoots(windowId: number, roots: readonly string[]): Promise { this._perWindowTrustedRoots.set(windowId, roots); + this._ensureWindowCloseSubscription(windowId); this._recomputeTrustedFileRoots(); } private readonly _perWindowTrustedRoots = new Map(); + private readonly _windowCloseSubscriptions = this._register(new DisposableMap()); + private _ensureWindowCloseSubscription(windowId: number): void { + if (this._windowCloseSubscriptions.has(windowId)) { + return; + } + const window = this.windowsMainService.getWindowById(windowId); + if (!window) { + return; + } + const onWindowGone = Event.any(window.onDidClose, window.onDidDestroy); + this._windowCloseSubscriptions.set(windowId, Event.once(onWindowGone)(() => { + this._windowCloseSubscriptions.deleteAndDispose(windowId); + if (this._perWindowTrustedRoots.delete(windowId)) { + this._recomputeTrustedFileRoots(); + } + })); + } private _recomputeTrustedFileRoots(): void { const roots = new Set(); for (const contribution of this._perWindowTrustedRoots.values()) {