aboutsummaryrefslogtreecommitdiffstats
path: root/packages/main/src/infrastructure/electron/impl/ElectronViewFactory.ts
diff options
context:
space:
mode:
authorLibravatar Kristóf Marussy <kristof@marussy.com>2022-04-08 02:10:22 +0200
committerLibravatar Kristóf Marussy <kristof@marussy.com>2022-05-16 00:55:00 +0200
commitab1cda612cf6d427bffb66d5674a3673eb958e50 (patch)
treeaeb01a0b431ca850b75d276af745bde807851839 /packages/main/src/infrastructure/electron/impl/ElectronViewFactory.ts
parentfix(main): Do not show spurious abort error (diff)
downloadsophie-ab1cda612cf6d427bffb66d5674a3673eb958e50.tar.gz
sophie-ab1cda612cf6d427bffb66d5674a3673eb958e50.tar.zst
sophie-ab1cda612cf6d427bffb66d5674a3673eb958e50.zip
feat(service-preload): Embed service-inject
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 <kristof@marussy.com>
Diffstat (limited to 'packages/main/src/infrastructure/electron/impl/ElectronViewFactory.ts')
-rw-r--r--packages/main/src/infrastructure/electron/impl/ElectronViewFactory.ts39
1 files changed, 1 insertions, 38 deletions
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 @@
18 * SPDX-License-Identifier: AGPL-3.0-only 18 * SPDX-License-Identifier: AGPL-3.0-only
19 */ 19 */
20 20
21import { readFile } from 'node:fs/promises';
22
23import { ServiceToMainIpcMessage } from '@sophie/service-shared';
24import { ipcMain, WebSource } from 'electron';
25
26import type MainStore from '../../../stores/MainStore'; 21import type MainStore from '../../../stores/MainStore';
27import type Profile from '../../../stores/Profile'; 22import type Profile from '../../../stores/Profile';
28import type Service from '../../../stores/Service'; 23import type Service from '../../../stores/Service';
29import { getLogger } from '../../../utils/log';
30import type Resources from '../../resources/Resources'; 24import type Resources from '../../resources/Resources';
31import type UserAgents from '../UserAgents'; 25import type UserAgents from '../UserAgents';
32import type { MainWindow, Partition, ServiceView, ViewFactory } from '../types'; 26import type { MainWindow, Partition, ServiceView, ViewFactory } from '../types';
@@ -35,37 +29,17 @@ import ElectronMainWindow from './ElectronMainWindow';
35import ElectronPartition from './ElectronPartition'; 29import ElectronPartition from './ElectronPartition';
36import ElectronServiceView from './ElectronServiceView'; 30import ElectronServiceView from './ElectronServiceView';
37 31
38const log = getLogger('ElectronViewFactory');
39
40export default class ElectronViewFactory implements ViewFactory { 32export default class ElectronViewFactory implements ViewFactory {
41 private readonly webContentsIdToServiceView = new Map< 33 private readonly webContentsIdToServiceView = new Map<
42 number, 34 number,
43 ElectronServiceView 35 ElectronServiceView
44 >(); 36 >();
45 37
46 private serviceInjectSource: WebSource | undefined;
47
48 constructor( 38 constructor(
49 readonly userAgents: UserAgents, 39 readonly userAgents: UserAgents,
50 readonly resources: Resources, 40 readonly resources: Resources,
51 readonly devMode: boolean, 41 readonly devMode: boolean,
52 ) { 42 ) {}
53 ipcMain.handle(ServiceToMainIpcMessage.ApiExposedInMainWorld, (event) => {
54 if (!this.webContentsIdToServiceView.has(event.sender.id)) {
55 log.error(
56 'Unexpected',
57 ServiceToMainIpcMessage.ApiExposedInMainWorld,
58 'IPC message from webContents',
59 event.sender.id,
60 );
61 throw new Error('Invalid IPC call');
62 }
63 if (this.serviceInjectSource === undefined) {
64 log.error('Service inject source was not loaded');
65 }
66 return this.serviceInjectSource;
67 });
68 }
69 43
70 async createMainWindow(store: MainStore): Promise<MainWindow> { 44 async createMainWindow(store: MainStore): Promise<MainWindow> {
71 const mainWindow = new ElectronMainWindow(store, this); 45 const mainWindow = new ElectronMainWindow(store, this);
@@ -94,23 +68,12 @@ export default class ElectronViewFactory implements ViewFactory {
94 throw new TypeError('Unexpected ProfileSession is not a WrappedSession'); 68 throw new TypeError('Unexpected ProfileSession is not a WrappedSession');
95 } 69 }
96 70
97 async loadServiceInject(): Promise<void> {
98 const injectPackage = 'service-inject';
99 const injectFile = 'index.js';
100 const injectPath = this.resources.getPath(injectPackage, injectFile);
101 this.serviceInjectSource = {
102 code: await readFile(injectPath, 'utf8'),
103 url: this.resources.getFileURL(injectPackage, injectFile),
104 };
105 }
106
107 dispose(): void { 71 dispose(): void {
108 if (this.webContentsIdToServiceView.size > 0) { 72 if (this.webContentsIdToServiceView.size > 0) {
109 throw new Error( 73 throw new Error(
110 'Must dispose all ServiceView instances before disposing ViewFactory', 74 'Must dispose all ServiceView instances before disposing ViewFactory',
111 ); 75 );
112 } 76 }
113 ipcMain.removeHandler(ServiceToMainIpcMessage.ApiExposedInMainWorld);
114 } 77 }
115 78
116 unregisterServiceView(id: number): void { 79 unregisterServiceView(id: number): void {