feat(electronSecurity): replace electron service remote

This commit is contained in:
Johannes Millan 2023-12-14 16:10:47 +01:00
parent 4b5ad80d26
commit 4fc7fd2bde
8 changed files with 40 additions and 61 deletions

View file

@ -6,6 +6,7 @@ export interface ElectronAPI {
// IPC STUFF
ipcEvent$(evName: string): Observable<unknown>;
send(channel: string, ...args: any[]): void;
invoke(channel: string, ...args: any[]): Promise<any>;
@ -26,4 +27,16 @@ export interface ElectronAPI {
openPath(path: string): Promise<string>;
openExternal(url: string, options?: OpenExternalOptions): Promise<void>;
isMacOS(): boolean;
// TODO implement
reloadMainWin(): void;
openDevTools(): void;
relaunch(): void;
exit(exitCode: number): void;
isSystemDarkMode(): boolean;
}

View file

@ -1,12 +1,15 @@
import {
app,
IpcRenderer,
ipcRenderer,
IpcRendererEvent,
nativeTheme,
OpenExternalOptions,
shell,
webFrame,
} from 'electron';
import { ElectronAPI } from './electronAPI.d';
import { getWin } from './main-window';
export const electronAPI: Partial<ElectronAPI> = {
// TODO use full interface
@ -32,4 +35,10 @@ export const electronAPI: Partial<ElectronAPI> = {
getZoomFactor: () => webFrame.getZoomFactor(),
openPath: (path: string) => shell.openPath(path),
openExternal: (url: string, options?: OpenExternalOptions) => shell.openExternal(url),
isMacOS: () => process.platform === 'darwin',
reloadMainWin: () => getWin().reload(),
openDevTools: () => getWin().webContents.openDevTools(),
relaunch: () => app.relaunch(),
exit: (exitCode: number) => app.exit(exitCode),
isSystemDarkMode: () => nativeTheme.shouldUseDarkColors,
};

View file

@ -29,8 +29,7 @@ const getResponseChannels = (
@Injectable({ providedIn: 'root' })
export class ElectronService {
ipcRenderer?: typeof ipcRenderer;
remote!: typeof remote;
private ipcRenderer?: typeof ipcRenderer;
// fs: typeof fs;
@ -40,45 +39,7 @@ export class ElectronService {
// this.ipcRenderer = electron.ipcRenderer;
this.ipcRenderer = window.electronAPI.ipcRenderer();
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
this.remote = getElectronRemoteModule()!;
// NOTE: works for non-sandboxed electron only
// log to file for production
// if (environment.production || environment.stage) {
// const log = (this.remote as typeof remote).require('electron-log');
// log.transports.maxSize = 1024 * 1024 * 20;
// console.error = log.error;
// console.log = log.log;
// console.warn = log.warn;
// }
}
// NOTE: useful in case we want to disable the node integration
// NOTE: global-error-handler.class.ts also needs to be adjusted
// this.ipcRenderer = {
// on: (channel: string, listener: (event: IpcRendererEvent, ...args: any[]) => void) => {
// },
// send: () => {
// }
// };
// this.webFrame = {
// setZoomFactor(factor: number): void {
// },
// getZoomFactor: () => 1
// };
}
public get isElectronApp(): boolean {
return !!window.navigator.userAgent.match(/Electron/);
}
// TODO move to a better place
public get isMacOS(): boolean {
return this.isElectronApp && this.process && this.process.platform === 'darwin';
}
public get process(): any {
return this.remote ? this.remote.process : null;
}
public callMain(channel: string, data: unknown): Promise<unknown> {

View file

@ -4,13 +4,10 @@ import { IPC } from '../../../../electron/shared-with-frontend/ipc-events.const'
import { Observable, of } from 'rxjs';
import { map } from 'rxjs/operators';
import { IS_ELECTRON } from '../../app.constants';
import { ipcRenderer } from 'electron';
import { ipcEvent$ } from '../../util/ipc-event';
@Injectable({ providedIn: 'root' })
export class ExecBeforeCloseService {
ipcRenderer: typeof ipcRenderer = this._electronService
.ipcRenderer as typeof ipcRenderer;
onBeforeClose$: Observable<string[]> = IS_ELECTRON
? ipcEvent$(IPC.NOTIFY_ON_CLOSE).pipe(map(([, ids]: any) => ids))
: of([]);
@ -18,14 +15,14 @@ export class ExecBeforeCloseService {
constructor(private _electronService: ElectronService) {}
schedule(id: string): void {
this.ipcRenderer.send(IPC.REGISTER_BEFORE_CLOSE, { id });
window.electronAPI.send(IPC.REGISTER_BEFORE_CLOSE, { id });
}
unschedule(id: string): void {
this.ipcRenderer.send(IPC.UNREGISTER_BEFORE_CLOSE, { id });
window.electronAPI.send(IPC.UNREGISTER_BEFORE_CLOSE, { id });
}
setDone(id: string): void {
this.ipcRenderer.send(IPC.BEFORE_CLOSE_DONE, { id });
window.electronAPI.send(IPC.BEFORE_CLOSE_DONE, { id });
}
}

View file

@ -105,7 +105,7 @@ export const createErrorAlert = (
btnReload.innerText = 'Reload App';
btnReload.addEventListener('click', () => {
if (IS_ELECTRON) {
eSvc.remote.getCurrentWindow().webContents.reload();
window.electronAPI.reloadMainWin();
} else {
window.location.reload();
}
@ -155,7 +155,7 @@ export const createErrorAlert = (
}, 1500);
if (IS_ELECTRON) {
eSvc.remote.getCurrentWindow().webContents.openDevTools();
window.electronAPI.openDevTools();
}
};

View file

@ -100,8 +100,8 @@ export class DatabaseService {
private _restartApp(): void {
if (IS_ELECTRON) {
this._electronService.remote.app.relaunch();
this._electronService.remote.app.exit(0);
window.electronAPI.relaunch();
window.electronAPI.exit(0);
} else {
window.location.reload();
}

View file

@ -21,16 +21,16 @@ import { IS_MOUSE_PRIMARY, IS_TOUCH_PRIMARY } from '../../util/is-mouse-primary'
@Injectable({ providedIn: 'root' })
export class GlobalThemeService {
isDarkTheme$: Observable<boolean> =
IS_ELECTRON && this._electronService.isMacOS
IS_ELECTRON && window.electronAPI.isMacOS()
? new Observable((subscriber) => {
subscriber.next(this._electronService.remote.nativeTheme.shouldUseDarkColors);
this._electronService.remote.systemPreferences.subscribeNotification(
'AppleInterfaceThemeChangedNotification',
() =>
subscriber.next(
this._electronService.remote.nativeTheme.shouldUseDarkColors,
),
);
subscriber.next(window.electronAPI.isSystemDarkMode());
// this._electronService.remote.systemPreferences.subscribeNotification(
// 'AppleInterfaceThemeChangedNotification',
// () =>
// subscriber.next(
// this._electronService.remote.nativeTheme.shouldUseDarkColors,
// ),
// );
})
: this._globalConfigService.misc$.pipe(
map((cfg) => cfg.isDarkMode),
@ -50,7 +50,6 @@ export class GlobalThemeService {
constructor(
@Inject(DOCUMENT) private document: Document,
private _materialCssVarsService: MaterialCssVarsService,
private _electronService: ElectronService,
private _workContextService: WorkContextService,
private _globalConfigService: GlobalConfigService,
private _matIconRegistry: MatIconRegistry,

View file

@ -428,7 +428,7 @@ export class JiraApiService {
});
const requestToSend = { requestId, requestInit, url };
if (this._electronService.isElectronApp) {
if (IS_ELECTRON) {
window.electronAPI.send(IPC.JIRA_MAKE_REQUEST_EVENT, {
...requestToSend,
jiraCfg,