Path: blob/main/src/vs/platform/browserView/electron-main/browserViewGroupMainService.ts
13397 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { Disposable, DisposableMap } from '../../../base/common/lifecycle.js';6import { Event } from '../../../base/common/event.js';7import { createDecorator, IInstantiationService } from '../../instantiation/common/instantiation.js';8import { generateUuid } from '../../../base/common/uuid.js';9import { IBrowserViewGroupService, IBrowserViewGroupViewEvent } from '../common/browserViewGroup.js';10import { IBrowserViewOwner } from '../common/browserView.js';11import { BrowserViewGroup } from './browserViewGroup.js';12import { CDPEvent, CDPRequest, CDPResponse } from '../common/cdp/types.js';1314export const IBrowserViewGroupMainService = createDecorator<IBrowserViewGroupMainService>('browserViewGroupMainService');1516export interface IBrowserViewGroupMainService extends IBrowserViewGroupService {17readonly _serviceBrand: undefined;18}1920/**21* Main-process service that manages {@link BrowserViewGroup} instances.22*23* Implements {@link IBrowserViewGroupService} so it can be surfaced to24* the workbench/shared process via {@link ProxyChannel}.25*/26export class BrowserViewGroupMainService extends Disposable implements IBrowserViewGroupMainService {27declare readonly _serviceBrand: undefined;2829private readonly groups = this._register(new DisposableMap<string, BrowserViewGroup>());3031constructor(32@IInstantiationService private readonly instantiationService: IInstantiationService33) {34super();35}3637async createGroup(owner: IBrowserViewOwner): Promise<string> {38const id = generateUuid();39const group = this.instantiationService.createInstance(BrowserViewGroup, id, owner);40this.groups.set(id, group);4142// Auto-cleanup when the group disposes itself43Event.once(group.onDidDestroy)(() => {44this.groups.deleteAndLeak(id);45});4647return id;48}4950async destroyGroup(groupId: string): Promise<void> {51this.groups.deleteAndDispose(groupId);52}5354async addViewToGroup(groupId: string, viewId: string): Promise<void> {55return this._getGroup(groupId).addView(viewId);56}5758async removeViewFromGroup(groupId: string, viewId: string): Promise<void> {59return this._getGroup(groupId).removeView(viewId);60}6162async sendCDPMessage(groupId: string, message: CDPRequest): Promise<void> {63return this._getGroup(groupId).debugger.sendMessage(message);64}6566onDynamicDidAddView(groupId: string): Event<IBrowserViewGroupViewEvent> {67return this._getGroup(groupId).onDidAddView;68}6970onDynamicDidRemoveView(groupId: string): Event<IBrowserViewGroupViewEvent> {71return this._getGroup(groupId).onDidRemoveView;72}7374onDynamicDidDestroy(groupId: string): Event<void> {75return this._getGroup(groupId).onDidDestroy;76}7778onDynamicCDPMessage(groupId: string): Event<CDPResponse | CDPEvent> {79return this._getGroup(groupId).debugger.onMessage;80}8182/**83* Get a group or throw if not found.84*/85private _getGroup(groupId: string): BrowserViewGroup {86const group = this.groups.get(groupId);87if (!group) {88throw new Error(`Browser view group ${groupId} not found`);89}90return group;91}92}93949596