diff options
Diffstat (limited to 'packages/main/src/infrastructure/electron/impl/ElectronViewFactory.ts')
-rw-r--r-- | packages/main/src/infrastructure/electron/impl/ElectronViewFactory.ts | 62 |
1 files changed, 15 insertions, 47 deletions
diff --git a/packages/main/src/infrastructure/electron/impl/ElectronViewFactory.ts b/packages/main/src/infrastructure/electron/impl/ElectronViewFactory.ts index f8b4a36..df3de85 100644 --- a/packages/main/src/infrastructure/electron/impl/ElectronViewFactory.ts +++ b/packages/main/src/infrastructure/electron/impl/ElectronViewFactory.ts | |||
@@ -18,24 +18,21 @@ | |||
18 | * SPDX-License-Identifier: AGPL-3.0-only | 18 | * SPDX-License-Identifier: AGPL-3.0-only |
19 | */ | 19 | */ |
20 | 20 | ||
21 | import { readFile } from 'node:fs/promises'; | 21 | import type MainStore from '../../../stores/MainStore.js'; |
22 | import type Profile from '../../../stores/Profile.js'; | ||
23 | import type Service from '../../../stores/Service.js'; | ||
24 | import type Resources from '../../resources/Resources.js'; | ||
25 | import type UserAgents from '../UserAgents.js'; | ||
26 | import type { | ||
27 | MainWindow, | ||
28 | Partition, | ||
29 | ServiceView, | ||
30 | ViewFactory, | ||
31 | } from '../types.js'; | ||
22 | 32 | ||
23 | import { ServiceToMainIpcMessage } from '@sophie/service-shared'; | 33 | import ElectronMainWindow from './ElectronMainWindow.js'; |
24 | import { ipcMain, WebSource } from 'electron'; | 34 | import ElectronPartition from './ElectronPartition.js'; |
25 | 35 | import ElectronServiceView from './ElectronServiceView.js'; | |
26 | import type MainStore from '../../../stores/MainStore'; | ||
27 | import type Profile from '../../../stores/Profile'; | ||
28 | import type Service from '../../../stores/Service'; | ||
29 | import { getLogger } from '../../../utils/log'; | ||
30 | import type Resources from '../../resources/Resources'; | ||
31 | import type UserAgents from '../UserAgents'; | ||
32 | import type { MainWindow, Partition, ServiceView, ViewFactory } from '../types'; | ||
33 | |||
34 | import ElectronMainWindow from './ElectronMainWindow'; | ||
35 | import ElectronPartition from './ElectronPartition'; | ||
36 | import ElectronServiceView from './ElectronServiceView'; | ||
37 | |||
38 | const log = getLogger('ElectronViewFactory'); | ||
39 | 36 | ||
40 | export default class ElectronViewFactory implements ViewFactory { | 37 | export default class ElectronViewFactory implements ViewFactory { |
41 | private readonly webContentsIdToServiceView = new Map< | 38 | private readonly webContentsIdToServiceView = new Map< |
@@ -43,29 +40,11 @@ export default class ElectronViewFactory implements ViewFactory { | |||
43 | ElectronServiceView | 40 | ElectronServiceView |
44 | >(); | 41 | >(); |
45 | 42 | ||
46 | private serviceInjectSource: WebSource | undefined; | ||
47 | |||
48 | constructor( | 43 | constructor( |
49 | readonly userAgents: UserAgents, | 44 | readonly userAgents: UserAgents, |
50 | readonly resources: Resources, | 45 | readonly resources: Resources, |
51 | readonly devMode: boolean, | 46 | readonly devMode: boolean, |
52 | ) { | 47 | ) {} |
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 | 48 | ||
70 | async createMainWindow(store: MainStore): Promise<MainWindow> { | 49 | async createMainWindow(store: MainStore): Promise<MainWindow> { |
71 | const mainWindow = new ElectronMainWindow(store, this); | 50 | const mainWindow = new ElectronMainWindow(store, this); |
@@ -94,23 +73,12 @@ export default class ElectronViewFactory implements ViewFactory { | |||
94 | throw new TypeError('Unexpected ProfileSession is not a WrappedSession'); | 73 | throw new TypeError('Unexpected ProfileSession is not a WrappedSession'); |
95 | } | 74 | } |
96 | 75 | ||
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, 'utf-8'), | ||
103 | url: this.resources.getFileURL(injectPackage, injectFile), | ||
104 | }; | ||
105 | } | ||
106 | |||
107 | dispose(): void { | 76 | dispose(): void { |
108 | if (this.webContentsIdToServiceView.size > 0) { | 77 | if (this.webContentsIdToServiceView.size > 0) { |
109 | throw new Error( | 78 | throw new Error( |
110 | 'Must dispose all ServiceView instances before disposing ViewFactory', | 79 | 'Must dispose all ServiceView instances before disposing ViewFactory', |
111 | ); | 80 | ); |
112 | } | 81 | } |
113 | ipcMain.removeHandler(ServiceToMainIpcMessage.ApiExposedInMainWorld); | ||
114 | } | 82 | } |
115 | 83 | ||
116 | unregisterServiceView(id: number): void { | 84 | unregisterServiceView(id: number): void { |