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