Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/platform/browserView/electron-main/browserViewGroupMainService.ts
13397 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { Disposable, DisposableMap } from '../../../base/common/lifecycle.js';
7
import { Event } from '../../../base/common/event.js';
8
import { createDecorator, IInstantiationService } from '../../instantiation/common/instantiation.js';
9
import { generateUuid } from '../../../base/common/uuid.js';
10
import { IBrowserViewGroupService, IBrowserViewGroupViewEvent } from '../common/browserViewGroup.js';
11
import { IBrowserViewOwner } from '../common/browserView.js';
12
import { BrowserViewGroup } from './browserViewGroup.js';
13
import { CDPEvent, CDPRequest, CDPResponse } from '../common/cdp/types.js';
14
15
export const IBrowserViewGroupMainService = createDecorator<IBrowserViewGroupMainService>('browserViewGroupMainService');
16
17
export interface IBrowserViewGroupMainService extends IBrowserViewGroupService {
18
readonly _serviceBrand: undefined;
19
}
20
21
/**
22
* Main-process service that manages {@link BrowserViewGroup} instances.
23
*
24
* Implements {@link IBrowserViewGroupService} so it can be surfaced to
25
* the workbench/shared process via {@link ProxyChannel}.
26
*/
27
export class BrowserViewGroupMainService extends Disposable implements IBrowserViewGroupMainService {
28
declare readonly _serviceBrand: undefined;
29
30
private readonly groups = this._register(new DisposableMap<string, BrowserViewGroup>());
31
32
constructor(
33
@IInstantiationService private readonly instantiationService: IInstantiationService
34
) {
35
super();
36
}
37
38
async createGroup(owner: IBrowserViewOwner): Promise<string> {
39
const id = generateUuid();
40
const group = this.instantiationService.createInstance(BrowserViewGroup, id, owner);
41
this.groups.set(id, group);
42
43
// Auto-cleanup when the group disposes itself
44
Event.once(group.onDidDestroy)(() => {
45
this.groups.deleteAndLeak(id);
46
});
47
48
return id;
49
}
50
51
async destroyGroup(groupId: string): Promise<void> {
52
this.groups.deleteAndDispose(groupId);
53
}
54
55
async addViewToGroup(groupId: string, viewId: string): Promise<void> {
56
return this._getGroup(groupId).addView(viewId);
57
}
58
59
async removeViewFromGroup(groupId: string, viewId: string): Promise<void> {
60
return this._getGroup(groupId).removeView(viewId);
61
}
62
63
async sendCDPMessage(groupId: string, message: CDPRequest): Promise<void> {
64
return this._getGroup(groupId).debugger.sendMessage(message);
65
}
66
67
onDynamicDidAddView(groupId: string): Event<IBrowserViewGroupViewEvent> {
68
return this._getGroup(groupId).onDidAddView;
69
}
70
71
onDynamicDidRemoveView(groupId: string): Event<IBrowserViewGroupViewEvent> {
72
return this._getGroup(groupId).onDidRemoveView;
73
}
74
75
onDynamicDidDestroy(groupId: string): Event<void> {
76
return this._getGroup(groupId).onDidDestroy;
77
}
78
79
onDynamicCDPMessage(groupId: string): Event<CDPResponse | CDPEvent> {
80
return this._getGroup(groupId).debugger.onMessage;
81
}
82
83
/**
84
* Get a group or throw if not found.
85
*/
86
private _getGroup(groupId: string): BrowserViewGroup {
87
const group = this.groups.get(groupId);
88
if (!group) {
89
throw new Error(`Browser view group ${groupId} not found`);
90
}
91
return group;
92
}
93
}
94
95
96