diff --git a/src/vs/base/parts/ipc/common/ipc.ts b/src/vs/base/parts/ipc/common/ipc.ts index e0205092202..cb81bef96bc 100644 --- a/src/vs/base/parts/ipc/common/ipc.ts +++ b/src/vs/base/parts/ipc/common/ipc.ts @@ -32,7 +32,6 @@ export interface IServerChannel { listen(ctx: TContext, event: string, arg?: any): Event; } - export const enum RequestType { Promise = 100, PromiseCancel = 101, @@ -833,3 +832,29 @@ export class StaticRouter implements IClientRouter return await this.route(hub); } } + +export class SimpleServiceProxyChannel implements IServerChannel { + + private service: { [key: string]: unknown }; + + constructor(service: unknown) { + this.service = service as { [key: string]: unknown }; + } + + listen(_: unknown, event: string): Event { + throw new Error(`Events are currently unsupported by SimpleServiceProxyChannel: ${event}`); + } + + call(_: unknown, command: string, arg?: any): Promise { + const target = this.service[command]; + if (typeof target === 'function') { + if (Array.isArray(arg)) { + return target.apply(this.service, arg); + } + + return target.call(this.service, arg); + } + + throw new Error(`Call Not Found: ${command}`); + } +} diff --git a/src/vs/code/electron-main/app.ts b/src/vs/code/electron-main/app.ts index 72b1fdc43fb..8f18f8d7915 100644 --- a/src/vs/code/electron-main/app.ts +++ b/src/vs/code/electron-main/app.ts @@ -32,7 +32,7 @@ import { NullTelemetryService, combinedAppender, LogAppender } from 'vs/platform import { TelemetryAppenderClient } from 'vs/platform/telemetry/node/telemetryIpc'; import { TelemetryService, ITelemetryServiceConfig } from 'vs/platform/telemetry/common/telemetryService'; import { resolveCommonProperties } from 'vs/platform/telemetry/node/commonProperties'; -import { getDelayedChannel, StaticRouter } from 'vs/base/parts/ipc/common/ipc'; +import { getDelayedChannel, StaticRouter, SimpleServiceProxyChannel } from 'vs/base/parts/ipc/common/ipc'; import product from 'vs/platform/product/common/product'; import { ProxyAuthHandler } from 'vs/code/electron-main/auth'; import { Disposable } from 'vs/base/common/lifecycle'; @@ -45,7 +45,6 @@ import { Win32UpdateService } from 'vs/platform/update/electron-main/updateServi import { LinuxUpdateService } from 'vs/platform/update/electron-main/updateService.linux'; import { DarwinUpdateService } from 'vs/platform/update/electron-main/updateService.darwin'; import { IIssueService } from 'vs/platform/issue/node/issue'; -import { IssueChannel } from 'vs/platform/issue/electron-main/issueIpc'; import { IssueMainService } from 'vs/platform/issue/electron-main/issueMainService'; import { LoggerChannel } from 'vs/platform/log/common/logIpc'; import { setUnexpectedErrorHandler, onUnexpectedError } from 'vs/base/common/errors'; @@ -77,7 +76,7 @@ import { IFileService } from 'vs/platform/files/common/files'; import { DiskFileSystemProvider } from 'vs/platform/files/node/diskFileSystemProvider'; import { ExtensionHostDebugBroadcastChannel } from 'vs/platform/debug/common/extensionHostDebugIpc'; import { IElectronService } from 'vs/platform/electron/node/electron'; -import { ElectronMainService, ElectronChannel } from 'vs/platform/electron/electron-main/electronMainService'; +import { ElectronMainService } from 'vs/platform/electron/electron-main/electronMainService'; export class CodeApplication extends Disposable { @@ -539,11 +538,11 @@ export class CodeApplication extends Disposable { electronIpcServer.registerChannel('update', updateChannel); const issueService = accessor.get(IIssueService); - const issueChannel = new IssueChannel(issueService); + const issueChannel = new SimpleServiceProxyChannel(issueService); electronIpcServer.registerChannel('issue', issueChannel); const electronService = accessor.get(IElectronService); - const electronChannel = new ElectronChannel(electronService); + const electronChannel = new SimpleServiceProxyChannel(electronService); electronIpcServer.registerChannel('electron', electronChannel); const workspacesMainService = accessor.get(IWorkspacesMainService); diff --git a/src/vs/platform/electron/electron-browser/electronService.ts b/src/vs/platform/electron/electron-browser/electronService.ts index fa2b5f09b18..3633f642e94 100644 --- a/src/vs/platform/electron/electron-browser/electronService.ts +++ b/src/vs/platform/electron/electron-browser/electronService.ts @@ -3,26 +3,14 @@ * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ -import { IMainProcessService } from 'vs/platform/ipc/electron-browser/mainProcessService'; +import { IElectronService } from 'vs/platform/electron/node/electron'; +import { IMainProcessService, createSimpleMainChannelProxy } from 'vs/platform/ipc/electron-browser/mainProcessService'; export class ElectronService { _serviceBrand: undefined; constructor(@IMainProcessService mainProcessService: IMainProcessService) { - const channel = mainProcessService.getChannel('electron'); - - // Proxy: forward any property access to the channel - return new Proxy({}, { - get(_target, propKey, _receiver) { - if (typeof propKey === 'string') { - return function (...args: any[]) { - return channel.call(propKey, ...args); - }; - } - - throw new Error(`Not Implemented in ElectronService: ${String(propKey)}`); - } - }) as ElectronService; + return createSimpleMainChannelProxy('electron', mainProcessService); } } diff --git a/src/vs/platform/electron/electron-main/electronMainService.ts b/src/vs/platform/electron/electron-main/electronMainService.ts index bcdee9d4a02..1846a5cb086 100644 --- a/src/vs/platform/electron/electron-main/electronMainService.ts +++ b/src/vs/platform/electron/electron-main/electronMainService.ts @@ -6,8 +6,6 @@ import { IElectronService } from 'vs/platform/electron/node/electron'; import { IWindowsMainService, ICodeWindow } from 'vs/platform/windows/electron-main/windows'; import { MessageBoxOptions, MessageBoxReturnValue, shell } from 'electron'; -import { IServerChannel } from 'vs/base/parts/ipc/common/ipc'; -import { Event } from 'vs/base/common/event'; export class ElectronMainService implements IElectronService { @@ -35,29 +33,3 @@ export class ElectronMainService implements IElectronService { shell.showItemInFolder(path); } } - -export class ElectronChannel implements IServerChannel { - - private service: { [key: string]: unknown }; - - constructor(service: IElectronService) { - this.service = service as unknown as { [key: string]: unknown }; - } - - listen(_: unknown, event: string): Event { - throw new Error(`Event not found: ${event}`); - } - - call(_: unknown, command: string, arg?: any): Promise { - const target = this.service[command]; - if (typeof target === 'function') { - if (Array.isArray(arg)) { - return target.apply(this.service, arg); - } - - return target.call(this.service, arg); - } - - throw new Error(`Call Not Found in ElectronService: ${command}`); - } -} diff --git a/src/vs/platform/ipc/electron-browser/mainProcessService.ts b/src/vs/platform/ipc/electron-browser/mainProcessService.ts index c72b1c703d3..ca24035c1e2 100644 --- a/src/vs/platform/ipc/electron-browser/mainProcessService.ts +++ b/src/vs/platform/ipc/electron-browser/mainProcessService.ts @@ -41,3 +41,19 @@ export class MainProcessService extends Disposable implements IMainProcessServic this.mainProcessConnection.registerChannel(channelName, channel); } } + +export function createSimpleMainChannelProxy(channelName: string, mainProcessService: IMainProcessService): T { + const channel = mainProcessService.getChannel(channelName); + + return new Proxy({}, { + get(_target, propKey, _receiver) { + if (typeof propKey === 'string') { + return function (...args: any[]) { + return channel.call(propKey, ...args); + }; + } + + throw new Error(`Unable to provide main channel proxy implementation for: ${String(propKey)} in ${channelName}`); + } + }) as T; +} diff --git a/src/vs/platform/issue/electron-browser/issueService.ts b/src/vs/platform/issue/electron-browser/issueService.ts index 56068fab19e..f7e67b7a3ac 100644 --- a/src/vs/platform/issue/electron-browser/issueService.ts +++ b/src/vs/platform/issue/electron-browser/issueService.ts @@ -3,29 +3,14 @@ * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ -import { IChannel } from 'vs/base/parts/ipc/common/ipc'; -import { IIssueService, IssueReporterData, ProcessExplorerData } from 'vs/platform/issue/node/issue'; -import { IMainProcessService } from 'vs/platform/ipc/electron-browser/mainProcessService'; +import { IIssueService } from 'vs/platform/issue/node/issue'; +import { IMainProcessService, createSimpleMainChannelProxy } from 'vs/platform/ipc/electron-browser/mainProcessService'; -export class IssueService implements IIssueService { +export class IssueService { _serviceBrand: undefined; - private channel: IChannel; - constructor(@IMainProcessService mainProcessService: IMainProcessService) { - this.channel = mainProcessService.getChannel('issue'); - } - - openReporter(data: IssueReporterData): Promise { - return this.channel.call('openIssueReporter', data); - } - - openProcessExplorer(data: ProcessExplorerData): Promise { - return this.channel.call('openProcessExplorer', data); - } - - getSystemStatus(): Promise { - return this.channel.call('getSystemStatus'); + return createSimpleMainChannelProxy('issue', mainProcessService); } } diff --git a/src/vs/platform/issue/electron-main/issueIpc.ts b/src/vs/platform/issue/electron-main/issueIpc.ts deleted file mode 100644 index 271bcf5ceee..00000000000 --- a/src/vs/platform/issue/electron-main/issueIpc.ts +++ /dev/null @@ -1,30 +0,0 @@ -/*--------------------------------------------------------------------------------------------- - * Copyright (c) Microsoft Corporation. All rights reserved. - * Licensed under the MIT License. See License.txt in the project root for license information. - *--------------------------------------------------------------------------------------------*/ - -import { IServerChannel } from 'vs/base/parts/ipc/common/ipc'; -import { Event } from 'vs/base/common/event'; -import { IIssueService } from 'vs/platform/issue/node/issue'; - -export class IssueChannel implements IServerChannel { - - constructor(private service: IIssueService) { } - - listen(_: unknown, event: string): Event { - throw new Error(`Event not found: ${event}`); - } - - call(_: unknown, command: string, arg?: any): Promise { - switch (command) { - case 'openIssueReporter': - return this.service.openReporter(arg); - case 'openProcessExplorer': - return this.service.openProcessExplorer(arg); - case 'getSystemStatus': - return this.service.getSystemStatus(); - } - - throw new Error(`Call not found: ${command}`); - } -} \ No newline at end of file