diff --git a/src/vs/base/parts/ipc/electron-sandbox/ipc.mp.ts b/src/vs/base/parts/ipc/electron-browser/ipc.mp.ts similarity index 90% rename from src/vs/base/parts/ipc/electron-sandbox/ipc.mp.ts rename to src/vs/base/parts/ipc/electron-browser/ipc.mp.ts index 98e865fe9bf..3f031a057e2 100644 --- a/src/vs/base/parts/ipc/electron-sandbox/ipc.mp.ts +++ b/src/vs/base/parts/ipc/electron-browser/ipc.mp.ts @@ -3,7 +3,7 @@ * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ -import { ipcRenderer } from 'vs/base/parts/sandbox/electron-sandbox/globals'; +import { ipcRenderer } from 'electron'; import { Event } from 'vs/base/common/event'; import { ClientConnectionEvent, IPCServer } from 'vs/base/parts/ipc/common/ipc'; import { Protocol as MessagePortProtocol } from 'vs/base/parts/ipc/common/ipc.mp'; @@ -39,6 +39,9 @@ export class Server extends IPCServer { }; // Send one port back to the requestor + // Note: we intentionally use `electron` APIs here because + // transferables like the `MessagePort` cannot be transfered + // over preload scripts when `contextIsolation: true` ipcRenderer.postMessage('vscode:createMessageChannelResult', nonce, [outgoingPort]); return result; diff --git a/src/vs/base/parts/sandbox/electron-browser/preload.js b/src/vs/base/parts/sandbox/electron-browser/preload.js index c217aa7a64d..bd12c6d8fb2 100644 --- a/src/vs/base/parts/sandbox/electron-browser/preload.js +++ b/src/vs/base/parts/sandbox/electron-browser/preload.js @@ -35,17 +35,6 @@ } }, - /** - * @param {string} channel - * @param {any} message - * @param {MessagePort[]} transfer - */ - postMessage(channel, message, transfer) { - if (validateIPC(channel)) { - ipcRenderer.postMessage(channel, message, transfer); - } - }, - /** * @param {string} channel * @param {any[]} args diff --git a/src/vs/base/parts/sandbox/electron-sandbox/electronTypes.ts b/src/vs/base/parts/sandbox/electron-sandbox/electronTypes.ts index 1adb322d1ba..e03f9e13673 100644 --- a/src/vs/base/parts/sandbox/electron-sandbox/electronTypes.ts +++ b/src/vs/base/parts/sandbox/electron-sandbox/electronTypes.ts @@ -15,10 +15,12 @@ export interface IpcRendererEvent extends Event { // Docs: https://electronjs.org/docs/api/structures/ipc-renderer-event - /** - * A list of MessagePorts that were transferred with this message - */ - ports: MessagePort[]; + // Note: API with `Transferable` intentionally commented out because you + // cannot transfer these when `contextIsolation: true`. + // /** + // * A list of MessagePorts that were transferred with this message + // */ + // ports: MessagePort[]; /** * The `IpcRenderer` instance that emitted the event originally */ @@ -93,20 +95,23 @@ export interface IpcRenderer { * If you do not need a response to the message, consider using `ipcRenderer.send`. */ invoke(channel: string, ...args: any[]): Promise; - /** - * Send a message to the main process, optionally transferring ownership of zero or - * more `MessagePort` objects. - * - * The transferred `MessagePort` objects will be available in the main process as - * `MessagePortMain` objects by accessing the `ports` property of the emitted - * event. - * - * For example: - * - * For more information on using `MessagePort` and `MessageChannel`, see the MDN - * documentation. - */ - postMessage(channel: string, message: any, transfer?: MessagePort[]): void; + + // Note: API with `Transferable` intentionally commented out because you + // cannot transfer these when `contextIsolation: true`. + // /** + // * Send a message to the main process, optionally transferring ownership of zero or + // * more `MessagePort` objects. + // * + // * The transferred `MessagePort` objects will be available in the main process as + // * `MessagePortMain` objects by accessing the `ports` property of the emitted + // * event. + // * + // * For example: + // * + // * For more information on using `MessagePort` and `MessageChannel`, see the MDN + // * documentation. + // */ + // postMessage(channel: string, message: any): void; } export interface WebFrame { diff --git a/src/vs/code/electron-browser/sharedProcess/sharedProcessMain.ts b/src/vs/code/electron-browser/sharedProcess/sharedProcessMain.ts index 50b7e18bc89..18f4dd64f99 100644 --- a/src/vs/code/electron-browser/sharedProcess/sharedProcessMain.ts +++ b/src/vs/code/electron-browser/sharedProcess/sharedProcessMain.ts @@ -7,7 +7,8 @@ import product from 'vs/platform/product/common/product'; import * as fs from 'fs'; import { release } from 'os'; import { gracefulify } from 'graceful-fs'; -import { Server as MessagePortServer } from 'vs/base/parts/ipc/electron-sandbox/ipc.mp'; +import { ipcRenderer } from 'electron'; +import { Server as MessagePortServer } from 'vs/base/parts/ipc/electron-browser/ipc.mp'; import { StaticRouter, ProxyChannel } from 'vs/base/parts/ipc/common/ipc'; import { ServiceCollection } from 'vs/platform/instantiation/common/serviceCollection'; import { SyncDescriptor } from 'vs/platform/instantiation/common/descriptors'; @@ -28,7 +29,6 @@ import { resolveCommonProperties } from 'vs/platform/telemetry/common/commonProp import { TelemetryAppenderChannel } from 'vs/platform/telemetry/common/telemetryIpc'; import { TelemetryService } from 'vs/platform/telemetry/common/telemetryService'; import { AppInsightsAppender } from 'vs/platform/telemetry/node/appInsightsAppender'; -import { ipcRenderer } from 'vs/base/parts/sandbox/electron-sandbox/globals'; import { ILogService, ILoggerService, MultiplexLogService, ConsoleLogger } from 'vs/platform/log/common/log'; import { LogLevelChannelClient, FollowerLogService } from 'vs/platform/log/common/logIpc'; import { LocalizationsService } from 'vs/platform/localizations/node/localizations'; @@ -40,7 +40,8 @@ import { NodeCachedDataCleaner } from 'vs/code/electron-browser/sharedProcess/co import { LanguagePackCachedDataCleaner } from 'vs/code/electron-browser/sharedProcess/contrib/languagePackCachedDataCleaner'; import { StorageDataCleaner } from 'vs/code/electron-browser/sharedProcess/contrib/storageDataCleaner'; import { LogsDataCleaner } from 'vs/code/electron-browser/sharedProcess/contrib/logsDataCleaner'; -import { IMainProcessService, MessagePortMainProcessService } from 'vs/platform/ipc/electron-sandbox/mainProcessService'; +import { IMainProcessService } from 'vs/platform/ipc/electron-sandbox/mainProcessService'; +import { MessagePortMainProcessService } from 'vs/platform/ipc/electron-browser/mainProcessService'; import { SpdLogLogger } from 'vs/platform/log/node/spdlogLog'; import { DiagnosticsService } from 'vs/platform/diagnostics/node/diagnosticsService'; import { IDiagnosticsService } from 'vs/platform/diagnostics/common/diagnostics'; diff --git a/src/vs/platform/ipc/electron-browser/mainProcessService.ts b/src/vs/platform/ipc/electron-browser/mainProcessService.ts new file mode 100644 index 00000000000..9253eddb605 --- /dev/null +++ b/src/vs/platform/ipc/electron-browser/mainProcessService.ts @@ -0,0 +1,29 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +import { IChannel, IServerChannel, StaticRouter } from 'vs/base/parts/ipc/common/ipc'; +import { Server as MessagePortServer } from 'vs/base/parts/ipc/electron-browser/ipc.mp'; +import { IMainProcessService } from 'vs/platform/ipc/electron-sandbox/mainProcessService'; + +/** + * An implementation of `IMainProcessService` that leverages MessagePorts. + */ +export class MessagePortMainProcessService implements IMainProcessService { + + declare readonly _serviceBrand: undefined; + + constructor( + private server: MessagePortServer, + private router: StaticRouter + ) { } + + getChannel(channelName: string): IChannel { + return this.server.getChannel(channelName, this.router); + } + + registerChannel(channelName: string, channel: IServerChannel): void { + this.server.registerChannel(channelName, channel); + } +} diff --git a/src/vs/platform/ipc/electron-sandbox/mainProcessService.ts b/src/vs/platform/ipc/electron-sandbox/mainProcessService.ts index 2f326523ce5..22e6149ee95 100644 --- a/src/vs/platform/ipc/electron-sandbox/mainProcessService.ts +++ b/src/vs/platform/ipc/electron-sandbox/mainProcessService.ts @@ -3,11 +3,10 @@ * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ -import { IChannel, IServerChannel, StaticRouter } from 'vs/base/parts/ipc/common/ipc'; +import { IChannel, IServerChannel } from 'vs/base/parts/ipc/common/ipc'; import { Client as IPCElectronClient } from 'vs/base/parts/ipc/electron-sandbox/ipc.electron'; import { Disposable } from 'vs/base/common/lifecycle'; import { createDecorator } from 'vs/platform/instantiation/common/instantiation'; -import { Server as MessagePortServer } from 'vs/base/parts/ipc/electron-sandbox/ipc.mp'; export const IMainProcessService = createDecorator('mainProcessService'); @@ -45,24 +44,3 @@ export class ElectronIPCMainProcessService extends Disposable implements IMainPr this.mainProcessConnection.registerChannel(channelName, channel); } } - -/** - * An implementation of `IMainProcessService` that leverages MessagePorts. - */ -export class MessagePortMainProcessService implements IMainProcessService { - - declare readonly _serviceBrand: undefined; - - constructor( - private server: MessagePortServer, - private router: StaticRouter - ) { } - - getChannel(channelName: string): IChannel { - return this.server.getChannel(channelName, this.router); - } - - registerChannel(channelName: string, channel: IServerChannel): void { - this.server.registerChannel(channelName, channel); - } -}