Path: blob/main/src/vs/platform/ipc/electron-browser/services.ts
3294 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';1112type ChannelClientCtor<T> = { new(channel: IChannel, ...args: any[]): T };13type Remote = { getChannel(channelName: string): IChannel };1415abstract class RemoteServiceStub<T extends object> {16constructor(17channelName: string,18options: IRemoteServiceWithChannelClientOptions<T> | IRemoteServiceWithProxyOptions | undefined,19remote: Remote,20instantiationService: IInstantiationService21) {22const channel = remote.getChannel(channelName);2324if (isRemoteServiceWithChannelClientOptions(options)) {25return instantiationService.createInstance(new SyncDescriptor(options.channelClientCtor, [channel]));26}2728return ProxyChannel.toService(channel, options?.proxyOptions);29}30}3132export interface IRemoteServiceWithChannelClientOptions<T> {33readonly channelClientCtor: ChannelClientCtor<T>;34}3536export interface IRemoteServiceWithProxyOptions {37readonly proxyOptions?: ProxyChannel.ICreateProxyServiceOptions;38}3940function isRemoteServiceWithChannelClientOptions<T>(obj: unknown): obj is IRemoteServiceWithChannelClientOptions<T> {41const candidate = obj as IRemoteServiceWithChannelClientOptions<T> | undefined;4243return !!candidate?.channelClientCtor;44}4546//#region Main Process4748class MainProcessRemoteServiceStub<T extends object> extends RemoteServiceStub<T> {49constructor(channelName: string, options: IRemoteServiceWithChannelClientOptions<T> | IRemoteServiceWithProxyOptions | undefined, @IMainProcessService ipcService: IMainProcessService, @IInstantiationService instantiationService: IInstantiationService) {50super(channelName, options, ipcService, instantiationService);51}52}5354export function registerMainProcessRemoteService<T>(id: ServiceIdentifier<T>, channelName: string, options?: IRemoteServiceWithChannelClientOptions<T> | IRemoteServiceWithProxyOptions): void {55registerSingleton(id, new SyncDescriptor(MainProcessRemoteServiceStub, [channelName, options], true));56}5758//#endregion5960//#region Shared Process6162export const ISharedProcessService = createDecorator<ISharedProcessService>('sharedProcessService');6364export interface ISharedProcessService extends IRemoteService {6566/**67* Allows to create a `MessagePort` connection between the68* shared process and the renderer process.69*70* Use this only when you need raw IPC to the shared process71* via `postMessage` and `on('message')` of special data structures72* like typed arrays.73*74* Callers have to call `port.start()` after having installed75* listeners to enable the data flow.76*/77createRawConnection(): Promise<MessagePort>;7879notifyRestored(): void;80}8182class SharedProcessRemoteServiceStub<T extends object> extends RemoteServiceStub<T> {83constructor(channelName: string, options: IRemoteServiceWithChannelClientOptions<T> | IRemoteServiceWithProxyOptions | undefined, @ISharedProcessService ipcService: ISharedProcessService, @IInstantiationService instantiationService: IInstantiationService) {84super(channelName, options, ipcService, instantiationService);85}86}8788export function registerSharedProcessRemoteService<T>(id: ServiceIdentifier<T>, channelName: string, options?: IRemoteServiceWithChannelClientOptions<T> | IRemoteServiceWithProxyOptions): void {89registerSingleton(id, new SyncDescriptor(SharedProcessRemoteServiceStub, [channelName, options], true));90}9192//#endregion939495