From ab1cda612cf6d427bffb66d5674a3673eb958e50 Mon Sep 17 00:00:00 2001 From: Kristóf Marussy Date: Fri, 8 Apr 2022 02:10:22 +0200 Subject: feat(service-preload): Embed service-inject MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Embed the service-inject script into the service-preload script to avoid having to load it manually and reduce IPC communication when a service loads. Signed-off-by: Kristóf Marussy --- .../electron/impl/ElectronViewFactory.ts | 39 +--------------------- 1 file changed, 1 insertion(+), 38 deletions(-) (limited to 'packages/main/src/infrastructure/electron/impl') diff --git a/packages/main/src/infrastructure/electron/impl/ElectronViewFactory.ts b/packages/main/src/infrastructure/electron/impl/ElectronViewFactory.ts index 884643a..c72860d 100644 --- a/packages/main/src/infrastructure/electron/impl/ElectronViewFactory.ts +++ b/packages/main/src/infrastructure/electron/impl/ElectronViewFactory.ts @@ -18,15 +18,9 @@ * SPDX-License-Identifier: AGPL-3.0-only */ -import { readFile } from 'node:fs/promises'; - -import { ServiceToMainIpcMessage } from '@sophie/service-shared'; -import { ipcMain, WebSource } from 'electron'; - import type MainStore from '../../../stores/MainStore'; import type Profile from '../../../stores/Profile'; import type Service from '../../../stores/Service'; -import { getLogger } from '../../../utils/log'; import type Resources from '../../resources/Resources'; import type UserAgents from '../UserAgents'; import type { MainWindow, Partition, ServiceView, ViewFactory } from '../types'; @@ -35,37 +29,17 @@ import ElectronMainWindow from './ElectronMainWindow'; import ElectronPartition from './ElectronPartition'; import ElectronServiceView from './ElectronServiceView'; -const log = getLogger('ElectronViewFactory'); - export default class ElectronViewFactory implements ViewFactory { private readonly webContentsIdToServiceView = new Map< number, ElectronServiceView >(); - private serviceInjectSource: WebSource | undefined; - constructor( readonly userAgents: UserAgents, readonly resources: Resources, readonly devMode: boolean, - ) { - ipcMain.handle(ServiceToMainIpcMessage.ApiExposedInMainWorld, (event) => { - if (!this.webContentsIdToServiceView.has(event.sender.id)) { - log.error( - 'Unexpected', - ServiceToMainIpcMessage.ApiExposedInMainWorld, - 'IPC message from webContents', - event.sender.id, - ); - throw new Error('Invalid IPC call'); - } - if (this.serviceInjectSource === undefined) { - log.error('Service inject source was not loaded'); - } - return this.serviceInjectSource; - }); - } + ) {} async createMainWindow(store: MainStore): Promise { const mainWindow = new ElectronMainWindow(store, this); @@ -94,23 +68,12 @@ export default class ElectronViewFactory implements ViewFactory { throw new TypeError('Unexpected ProfileSession is not a WrappedSession'); } - async loadServiceInject(): Promise { - const injectPackage = 'service-inject'; - const injectFile = 'index.js'; - const injectPath = this.resources.getPath(injectPackage, injectFile); - this.serviceInjectSource = { - code: await readFile(injectPath, 'utf8'), - url: this.resources.getFileURL(injectPackage, injectFile), - }; - } - dispose(): void { if (this.webContentsIdToServiceView.size > 0) { throw new Error( 'Must dispose all ServiceView instances before disposing ViewFactory', ); } - ipcMain.removeHandler(ServiceToMainIpcMessage.ApiExposedInMainWorld); } unregisterServiceView(id: number): void { -- cgit v1.2.3-54-g00ecf