Path: blob/main/src/vs/platform/ipc/electron-browser/services.ts
5256 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 { IChannel, ProxyChannel } from '../../../base/parts/ipc/common/ipc.js';6import { SyncDescriptor } from '../../instantiation/common/descriptors.js';7import { registerSingleton } from '../../instantiation/common/extensions.js';8import { createDecorator, IInstantiationService, ServiceIdentifier } from '../../instantiation/common/instantiation.js';9import { IMainProcessService } from '../common/mainProcessService.js';10import { IRemoteService } from '../common/services.js';1112// eslint-disable-next-line @typescript-eslint/no-explicit-any13type ChannelClientCtor<T> = { new(channel: IChannel, ...args: any[]): T };14type Remote = { getChannel(channelName: string): IChannel };1516abstract class RemoteServiceStub<T extends object> {17constructor(18channelName: string,19options: IRemoteServiceWithChannelClientOptions<T> | IRemoteServiceWithProxyOptions | undefined,20remote: Remote,21instantiationService: IInstantiationService22) {23const channel = remote.getChannel(channelName);2425if (isRemoteServiceWithChannelClientOptions(options)) {26return instantiationService.createInstance(new SyncDescriptor(options.channelClientCtor, [channel]));27}2829return ProxyChannel.toService(channel, options?.proxyOptions);30}31}3233export interface IRemoteServiceWithChannelClientOptions<T> {34readonly channelClientCtor: ChannelClientCtor<T>;35}3637export interface IRemoteServiceWithProxyOptions {38readonly proxyOptions?: ProxyChannel.ICreateProxyServiceOptions;39}4041function isRemoteServiceWithChannelClientOptions<T>(obj: unknown): obj is IRemoteServiceWithChannelClientOptions<T> {42const candidate = obj as IRemoteServiceWithChannelClientOptions<T> | undefined;4344return !!candidate?.channelClientCtor;45}4647//#region Main Process4849class MainProcessRemoteServiceStub<T extends object> extends RemoteServiceStub<T> {50constructor(channelName: string, options: IRemoteServiceWithChannelClientOptions<T> | IRemoteServiceWithProxyOptions | undefined, @IMainProcessService ipcService: IMainProcessService, @IInstantiationService instantiationService: IInstantiationService) {51super(channelName, options, ipcService, instantiationService);52}53}5455export function registerMainProcessRemoteService<T>(id: ServiceIdentifier<T>, channelName: string, options?: IRemoteServiceWithChannelClientOptions<T> | IRemoteServiceWithProxyOptions): void {56registerSingleton(id, new SyncDescriptor(MainProcessRemoteServiceStub, [channelName, options], true));57}5859//#endregion6061//#region Shared Process6263export const ISharedProcessService = createDecorator<ISharedProcessService>('sharedProcessService');6465export interface ISharedProcessService extends IRemoteService {6667/**68* Allows to create a `MessagePort` connection between the69* shared process and the renderer process.70*71* Use this only when you need raw IPC to the shared process72* via `postMessage` and `on('message')` of special data structures73* like typed arrays.74*75* Callers have to call `port.start()` after having installed76* listeners to enable the data flow.77*/78createRawConnection(): Promise<MessagePort>;7980notifyRestored(): void;81}8283class SharedProcessRemoteServiceStub<T extends object> extends RemoteServiceStub<T> {84constructor(channelName: string, options: IRemoteServiceWithChannelClientOptions<T> | IRemoteServiceWithProxyOptions | undefined, @ISharedProcessService ipcService: ISharedProcessService, @IInstantiationService instantiationService: IInstantiationService) {85super(channelName, options, ipcService, instantiationService);86}87}8889export function registerSharedProcessRemoteService<T>(id: ServiceIdentifier<T>, channelName: string, options?: IRemoteServiceWithChannelClientOptions<T> | IRemoteServiceWithProxyOptions): void {90registerSingleton(id, new SyncDescriptor(SharedProcessRemoteServiceStub, [channelName, options], true));91}9293//#endregion949596