Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/platform/ipc/electron-browser/services.ts
5256 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 { IChannel, ProxyChannel } from '../../../base/parts/ipc/common/ipc.js';
7
import { SyncDescriptor } from '../../instantiation/common/descriptors.js';
8
import { registerSingleton } from '../../instantiation/common/extensions.js';
9
import { createDecorator, IInstantiationService, ServiceIdentifier } from '../../instantiation/common/instantiation.js';
10
import { IMainProcessService } from '../common/mainProcessService.js';
11
import { IRemoteService } from '../common/services.js';
12
13
// eslint-disable-next-line @typescript-eslint/no-explicit-any
14
type ChannelClientCtor<T> = { new(channel: IChannel, ...args: any[]): T };
15
type Remote = { getChannel(channelName: string): IChannel };
16
17
abstract class RemoteServiceStub<T extends object> {
18
constructor(
19
channelName: string,
20
options: IRemoteServiceWithChannelClientOptions<T> | IRemoteServiceWithProxyOptions | undefined,
21
remote: Remote,
22
instantiationService: IInstantiationService
23
) {
24
const channel = remote.getChannel(channelName);
25
26
if (isRemoteServiceWithChannelClientOptions(options)) {
27
return instantiationService.createInstance(new SyncDescriptor(options.channelClientCtor, [channel]));
28
}
29
30
return ProxyChannel.toService(channel, options?.proxyOptions);
31
}
32
}
33
34
export interface IRemoteServiceWithChannelClientOptions<T> {
35
readonly channelClientCtor: ChannelClientCtor<T>;
36
}
37
38
export interface IRemoteServiceWithProxyOptions {
39
readonly proxyOptions?: ProxyChannel.ICreateProxyServiceOptions;
40
}
41
42
function isRemoteServiceWithChannelClientOptions<T>(obj: unknown): obj is IRemoteServiceWithChannelClientOptions<T> {
43
const candidate = obj as IRemoteServiceWithChannelClientOptions<T> | undefined;
44
45
return !!candidate?.channelClientCtor;
46
}
47
48
//#region Main Process
49
50
class MainProcessRemoteServiceStub<T extends object> extends RemoteServiceStub<T> {
51
constructor(channelName: string, options: IRemoteServiceWithChannelClientOptions<T> | IRemoteServiceWithProxyOptions | undefined, @IMainProcessService ipcService: IMainProcessService, @IInstantiationService instantiationService: IInstantiationService) {
52
super(channelName, options, ipcService, instantiationService);
53
}
54
}
55
56
export function registerMainProcessRemoteService<T>(id: ServiceIdentifier<T>, channelName: string, options?: IRemoteServiceWithChannelClientOptions<T> | IRemoteServiceWithProxyOptions): void {
57
registerSingleton(id, new SyncDescriptor(MainProcessRemoteServiceStub, [channelName, options], true));
58
}
59
60
//#endregion
61
62
//#region Shared Process
63
64
export const ISharedProcessService = createDecorator<ISharedProcessService>('sharedProcessService');
65
66
export interface ISharedProcessService extends IRemoteService {
67
68
/**
69
* Allows to create a `MessagePort` connection between the
70
* shared process and the renderer process.
71
*
72
* Use this only when you need raw IPC to the shared process
73
* via `postMessage` and `on('message')` of special data structures
74
* like typed arrays.
75
*
76
* Callers have to call `port.start()` after having installed
77
* listeners to enable the data flow.
78
*/
79
createRawConnection(): Promise<MessagePort>;
80
81
notifyRestored(): void;
82
}
83
84
class SharedProcessRemoteServiceStub<T extends object> extends RemoteServiceStub<T> {
85
constructor(channelName: string, options: IRemoteServiceWithChannelClientOptions<T> | IRemoteServiceWithProxyOptions | undefined, @ISharedProcessService ipcService: ISharedProcessService, @IInstantiationService instantiationService: IInstantiationService) {
86
super(channelName, options, ipcService, instantiationService);
87
}
88
}
89
90
export function registerSharedProcessRemoteService<T>(id: ServiceIdentifier<T>, channelName: string, options?: IRemoteServiceWithChannelClientOptions<T> | IRemoteServiceWithProxyOptions): void {
91
registerSingleton(id, new SyncDescriptor(SharedProcessRemoteServiceStub, [channelName, options], true));
92
}
93
94
//#endregion
95
96