Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/platform/browserView/node/browserViewGroupRemoteService.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 { Event } from '../../../base/common/event.js';
7
import { Disposable } from '../../../base/common/lifecycle.js';
8
import { ProxyChannel } from '../../../base/parts/ipc/common/ipc.js';
9
import { IMainProcessService } from '../../ipc/common/mainProcessService.js';
10
import { IBrowserViewGroup, IBrowserViewGroupService, IBrowserViewGroupViewEvent, ipcBrowserViewGroupChannelName } from '../common/browserViewGroup.js';
11
import { IBrowserViewOwner } from '../common/browserView.js';
12
import { CDPEvent, CDPRequest, CDPResponse } from '../common/cdp/types.js';
13
14
/**
15
* Remote-process service for managing browser view groups.
16
*
17
* Connects to the main-process {@link BrowserViewGroupMainService} via
18
* IPC and provides {@link IBrowserViewGroup} instances for
19
* interacting with groups.
20
*
21
* Usable from the shared process.
22
*/
23
export interface IBrowserViewGroupRemoteService {
24
/**
25
* Create a new browser view group.
26
* @param owner The owner of the group's lifecycle.
27
*/
28
createGroup(owner: IBrowserViewOwner): Promise<IBrowserViewGroup>;
29
}
30
31
/**
32
* Remote proxy for a browser view group living in the main process.
33
*/
34
class RemoteBrowserViewGroup extends Disposable implements IBrowserViewGroup {
35
constructor(
36
readonly id: string,
37
private readonly groupService: IBrowserViewGroupService,
38
) {
39
super();
40
41
this._register(groupService.onDynamicDidDestroy(this.id)(() => {
42
// Avoid loops
43
this.dispose(true);
44
}));
45
}
46
47
get onDidAddView(): Event<IBrowserViewGroupViewEvent> {
48
return this.groupService.onDynamicDidAddView(this.id);
49
}
50
51
get onDidRemoveView(): Event<IBrowserViewGroupViewEvent> {
52
return this.groupService.onDynamicDidRemoveView(this.id);
53
}
54
55
get onDidDestroy(): Event<void> {
56
return this.groupService.onDynamicDidDestroy(this.id);
57
}
58
59
async addView(viewId: string): Promise<void> {
60
return this.groupService.addViewToGroup(this.id, viewId);
61
}
62
63
async removeView(viewId: string): Promise<void> {
64
return this.groupService.removeViewFromGroup(this.id, viewId);
65
}
66
67
async sendCDPMessage(msg: CDPRequest): Promise<void> {
68
return this.groupService.sendCDPMessage(this.id, msg);
69
}
70
71
get onCDPMessage(): Event<CDPResponse | CDPEvent> {
72
return this.groupService.onDynamicCDPMessage(this.id);
73
}
74
75
override dispose(fromService = false): void {
76
if (!fromService) {
77
this.groupService.destroyGroup(this.id);
78
}
79
super.dispose();
80
}
81
}
82
83
export class BrowserViewGroupRemoteService implements IBrowserViewGroupRemoteService {
84
private readonly _groupService: IBrowserViewGroupService;
85
private readonly _groups = new Map<string, IBrowserViewGroup>();
86
87
constructor(
88
mainProcessService: IMainProcessService,
89
) {
90
const channel = mainProcessService.getChannel(ipcBrowserViewGroupChannelName);
91
this._groupService = ProxyChannel.toService<IBrowserViewGroupService>(channel);
92
}
93
94
async createGroup(owner: IBrowserViewOwner): Promise<IBrowserViewGroup> {
95
const id = await this._groupService.createGroup(owner);
96
return this._wrap(id);
97
}
98
99
private _wrap(id: string): IBrowserViewGroup {
100
const group = new RemoteBrowserViewGroup(id, this._groupService);
101
this._groups.set(id, group);
102
103
Event.once(group.onDidDestroy)(() => {
104
this._groups.delete(id);
105
});
106
107
return group;
108
}
109
}
110
111