UNPKG

@theia/core

Version:

Theia is a cloud & desktop IDE framework implemented in TypeScript.

133 lines • 5.98 kB
// ***************************************************************************** // Copyright (C) 2020 Ericsson and others. // // This program and the accompanying materials are made available under the // terms of the Eclipse Public License v. 2.0 which is available at // http://www.eclipse.org/legal/epl-2.0. // // This Source Code may also be made available under the following Secondary // Licenses when the conditions for such availability set forth in the Eclipse // Public License v. 2.0 are satisfied: GNU General Public License, version 2 // with the GNU Classpath Exception which is available at // https://www.gnu.org/software/classpath/license.html. // // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 // ***************************************************************************** import { __decorate } from "tslib"; import { inject, injectable, named, postConstruct } from 'inversify'; import { ConnectionHandlers } from '../../node/messaging/default-messaging-service.js'; import { AbstractChannel, ChannelMultiplexer } from '../../common/message-rpc/channel.js'; import { ContributionProvider, Emitter } from '../../common/index.js'; import { Uint8ArrayReadBuffer, Uint8ArrayWriteBuffer } from '../../common/message-rpc/uint8-array-message-buffer.js'; import { TheiaRendererAPI } from '../electron-api-main.js'; import { ElectronMessagingService } from './electron-messaging-service.js'; import { ElectronConnectionHandler } from './electron-connection-handler.js'; /** * This component replicates the role filled by `MessagingContribution` but for Electron. * Unlike the WebSocket based implementation, we do not expect to receive * connection events. Instead, we'll create channels based on incoming `open` * events on the `ipcMain` channel. * This component allows communication between renderer process (frontend) and electron main process. */ let ElectronMessagingContribution = class ElectronMessagingContribution { constructor() { this.channelHandlers = new ConnectionHandlers(); /** * Each electron window has a main channel and its own multiplexer to route multiple client messages the same IPC connection. */ this.openChannels = new Map(); this.wsHandlers = new ConnectionHandlers(); } init() { TheiaRendererAPI.onIpcData((sender, data) => this.handleIpcEvent(sender, data)); } // eslint-disable-next-line @typescript-eslint/no-explicit-any ipcChannel(spec, callback) { this.channelHandlers.push(spec, callback); } onStart() { for (const contribution of this.messagingContributions.getContributions()) { contribution.configure(this); } for (const connectionHandler of this.connectionHandlers.getContributions()) { this.channelHandlers.push(connectionHandler.path, (params, channel) => { connectionHandler.onConnection(channel); }); } } handleIpcEvent(sender, data) { // Get the multiplexer for a given window id try { const windowChannel = this.openChannels.get(sender.id) ?? this.createWindowChannel(sender); windowChannel.onMessageEmitter.fire(() => new Uint8ArrayReadBuffer(data)); } catch (error) { console.error('IPC: Failed to handle message', { error, data }); } } // Creates a new channel for a given sender/window createWindowChannel(sender) { const mainChannel = new ElectronWebContentChannel(sender); const multiplexer = new ChannelMultiplexer(mainChannel); multiplexer.onDidOpenChannel(openEvent => { const { channel, id } = openEvent; if (this.channelHandlers.route(id, channel)) { console.debug(`Opening channel for service path '${id}'.`); channel.onClose(() => console.debug(`Closing channel on service path '${id}'.`)); } }); sender.once('did-navigate', () => this.deleteChannel(sender.id, 'Window was refreshed')); sender.once('destroyed', () => this.deleteChannel(sender.id, 'Window was closed')); this.openChannels.set(sender.id, mainChannel); return mainChannel; } deleteChannel(senderId, reason) { const channel = this.openChannels.get(senderId); if (channel) { this.openChannels.delete(senderId); channel.onCloseEmitter.fire({ reason: reason }); } } registerConnectionHandler(spec, callback) { this.wsHandlers.push(spec, callback); } }; __decorate([ inject(ContributionProvider), named(ElectronMessagingService.Contribution) ], ElectronMessagingContribution.prototype, "messagingContributions", void 0); __decorate([ inject(ContributionProvider), named(ElectronConnectionHandler) ], ElectronMessagingContribution.prototype, "connectionHandlers", void 0); __decorate([ postConstruct() ], ElectronMessagingContribution.prototype, "init", null); ElectronMessagingContribution = __decorate([ injectable() ], ElectronMessagingContribution); export { ElectronMessagingContribution }; /** * Used to establish a connection between the ipcMain and the Electron frontend (window). * Messages a transferred via electron IPC. */ export class ElectronWebContentChannel extends AbstractChannel { constructor(sender) { super(); this.sender = sender; // Make the message emitter public so that we can easily forward messages received from the ipcMain. this.onMessageEmitter = new Emitter(); } getWriteBuffer() { const writer = new Uint8ArrayWriteBuffer(); writer.onCommit(buffer => { if (!this.sender.isDestroyed()) { TheiaRendererAPI.sendData(this.sender, buffer); } }); return writer; } } //# sourceMappingURL=electron-messaging-contribution.js.map