diff options
author | Kristóf Marussy <kristof@marussy.com> | 2022-04-23 01:59:21 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-22 23:59:21 +0000 |
commit | d02644f7c41150709795e57bfd40351b4da35a7b (patch) | |
tree | 2403fb76bd5fae1703f8b55172ffce9e0a5d2bce /src/electron | |
parent | Complete tray icons redesign for all platforms (#28) (diff) | |
download | ferdium-app-d02644f7c41150709795e57bfd40351b4da35a7b.tar.gz ferdium-app-d02644f7c41150709795e57bfd40351b4da35a7b.tar.zst ferdium-app-d02644f7c41150709795e57bfd40351b4da35a7b.zip |
Preload safe debug shim (#29)
In https://github.com/ferdium/ferdium-app/pull/23 we removed usages of
the debug package due to an electron bug.
This patch aims to restore some debug functionality by introducing a
shim. The shim detect whether if it is being introduced in a preload
script where the electron but would be triggered, and falls back to a
simple replacement for debug. However, in the main and renderer
processes, where a preload script is not being used, we still get full
debug functionality.
In this way, a module can be used both in a preload script and outside
of it, while still preserving debug functionality whenever possible.
Signed-off-by: Kristóf Marussy <kristof@marussy.com>
Diffstat (limited to 'src/electron')
-rw-r--r-- | src/electron/Settings.ts | 2 | ||||
-rw-r--r-- | src/electron/ipc-api/autoUpdate.ts | 2 | ||||
-rw-r--r-- | src/electron/ipc-api/cld.ts | 2 | ||||
-rw-r--r-- | src/electron/ipc-api/dnd.ts | 2 | ||||
-rw-r--r-- | src/electron/ipc-api/download.ts | 7 | ||||
-rw-r--r-- | src/electron/ipc-api/sessionStorage.ts | 9 | ||||
-rw-r--r-- | src/electron/macOSPermissions.ts | 19 |
7 files changed, 20 insertions, 23 deletions
diff --git a/src/electron/Settings.ts b/src/electron/Settings.ts index 0956ea486..de010b9a3 100644 --- a/src/electron/Settings.ts +++ b/src/electron/Settings.ts | |||
@@ -2,7 +2,7 @@ import { observable, toJS } from 'mobx'; | |||
2 | import { pathExistsSync, outputJsonSync, readJsonSync } from 'fs-extra'; | 2 | import { pathExistsSync, outputJsonSync, readJsonSync } from 'fs-extra'; |
3 | import { userDataPath } from '../environment-remote'; | 3 | import { userDataPath } from '../environment-remote'; |
4 | 4 | ||
5 | const debug = require('debug')('Ferdium:Settings'); | 5 | const debug = require('../preload-safe-debug')('Ferdium:Settings'); |
6 | 6 | ||
7 | export default class Settings { | 7 | export default class Settings { |
8 | type = ''; | 8 | type = ''; |
diff --git a/src/electron/ipc-api/autoUpdate.ts b/src/electron/ipc-api/autoUpdate.ts index 930644816..839f1f117 100644 --- a/src/electron/ipc-api/autoUpdate.ts +++ b/src/electron/ipc-api/autoUpdate.ts | |||
@@ -2,7 +2,7 @@ import { app, ipcMain, BrowserWindow } from 'electron'; | |||
2 | import { autoUpdater } from 'electron-updater'; | 2 | import { autoUpdater } from 'electron-updater'; |
3 | import { isMac, isWindows } from '../../environment'; | 3 | import { isMac, isWindows } from '../../environment'; |
4 | 4 | ||
5 | const debug = require('debug')('Ferdium:ipcApi:autoUpdate'); | 5 | const debug = require('../../preload-safe-debug')('Ferdium:ipcApi:autoUpdate'); |
6 | 6 | ||
7 | export default (params: { mainWindow: BrowserWindow; settings: any }) => { | 7 | export default (params: { mainWindow: BrowserWindow; settings: any }) => { |
8 | const enableUpdate = Boolean(params.settings.app.get('automaticUpdates')); | 8 | const enableUpdate = Boolean(params.settings.app.get('automaticUpdates')); |
diff --git a/src/electron/ipc-api/cld.ts b/src/electron/ipc-api/cld.ts index 8918b016f..a6332e22d 100644 --- a/src/electron/ipc-api/cld.ts +++ b/src/electron/ipc-api/cld.ts | |||
@@ -2,7 +2,7 @@ import { ipcMain } from 'electron'; | |||
2 | // @ts-ignore | 2 | // @ts-ignore |
3 | import cld from 'cld'; | 3 | import cld from 'cld'; |
4 | 4 | ||
5 | const debug = require('debug')('Ferdium:ipcApi:cld'); | 5 | const debug = require('../../preload-safe-debug')('Ferdium:ipcApi:cld'); |
6 | 6 | ||
7 | export default async () => { | 7 | export default async () => { |
8 | ipcMain.handle('detect-language', async (_event, { sample }) => { | 8 | ipcMain.handle('detect-language', async (_event, { sample }) => { |
diff --git a/src/electron/ipc-api/dnd.ts b/src/electron/ipc-api/dnd.ts index 54a325db2..6b1777367 100644 --- a/src/electron/ipc-api/dnd.ts +++ b/src/electron/ipc-api/dnd.ts | |||
@@ -3,7 +3,7 @@ import { isMac } from '../../environment'; | |||
3 | 3 | ||
4 | const { getDoNotDisturb } = require('macos-notification-state'); | 4 | const { getDoNotDisturb } = require('macos-notification-state'); |
5 | 5 | ||
6 | const debug = require('debug')('Ferdium:ipcApi:dnd'); | 6 | const debug = require('../../preload-safe-debug')('Ferdium:ipcApi:dnd'); |
7 | 7 | ||
8 | export default async () => { | 8 | export default async () => { |
9 | ipcMain.handle('get-dnd', async () => { | 9 | ipcMain.handle('get-dnd', async () => { |
diff --git a/src/electron/ipc-api/download.ts b/src/electron/ipc-api/download.ts index 3631e8fee..21af0d045 100644 --- a/src/electron/ipc-api/download.ts +++ b/src/electron/ipc-api/download.ts | |||
@@ -4,8 +4,7 @@ import mime from 'mime-types'; | |||
4 | import { writeFileSync } from 'fs-extra'; | 4 | import { writeFileSync } from 'fs-extra'; |
5 | import { PathLike } from 'fs'; | 5 | import { PathLike } from 'fs'; |
6 | 6 | ||
7 | // TODO: Go back to 'debug' from 'console.log' when https://github.com/electron/electron/issues/31689 is fixed | 7 | const debug = require('../../preload-safe-debug')('Ferdium:ipcApi:download'); |
8 | // const debug = require('debug')('Ferdium:ipcApi:download'); | ||
9 | 8 | ||
10 | function decodeBase64Image(dataString: string) { | 9 | function decodeBase64Image(dataString: string) { |
11 | const matches = dataString.match(/^data:([+/A-Za-z-]+);base64,(.+)$/); | 10 | const matches = dataString.match(/^data:([+/A-Za-z-]+);base64,(.+)$/); |
@@ -28,7 +27,7 @@ export default (params: { mainWindow: BrowserWindow }) => { | |||
28 | const dl = await download(win!, url, { | 27 | const dl = await download(win!, url, { |
29 | saveAs: true, | 28 | saveAs: true, |
30 | }); | 29 | }); |
31 | console.log('File saved to', dl.savePath); | 30 | debug('File saved to', dl.savePath); |
32 | } else { | 31 | } else { |
33 | const extension = mime.extension(fileOptions.mime); | 32 | const extension = mime.extension(fileOptions.mime); |
34 | const filename = `${fileOptions.name}.${extension}`; | 33 | const filename = `${fileOptions.name}.${extension}`; |
@@ -47,7 +46,7 @@ export default (params: { mainWindow: BrowserWindow }) => { | |||
47 | 'binary', | 46 | 'binary', |
48 | ); | 47 | ); |
49 | 48 | ||
50 | console.log('File blob saved to', saveDialog.filePath); | 49 | debug('File blob saved to', saveDialog.filePath); |
51 | } catch (error) { | 50 | } catch (error) { |
52 | console.error(error); | 51 | console.error(error); |
53 | } | 52 | } |
diff --git a/src/electron/ipc-api/sessionStorage.ts b/src/electron/ipc-api/sessionStorage.ts index 96acacd12..2a9f4b4d1 100644 --- a/src/electron/ipc-api/sessionStorage.ts +++ b/src/electron/ipc-api/sessionStorage.ts | |||
@@ -2,8 +2,7 @@ import { ipcMain, Session, session } from 'electron'; | |||
2 | 2 | ||
3 | import { TODOS_PARTITION_ID } from '../../config'; | 3 | import { TODOS_PARTITION_ID } from '../../config'; |
4 | 4 | ||
5 | // TODO: Go back to 'debug' from 'console.log' when https://github.com/electron/electron/issues/31689 is fixed | 5 | const debug = require('../../preload-safe-debug')('Ferdium:ipcApi:sessionStorage'); |
6 | // const debug = require('debug')('Ferdium:ipcApi:sessionStorage'); | ||
7 | 6 | ||
8 | function deduceSession(serviceId: string | undefined | null): Session { | 7 | function deduceSession(serviceId: string | undefined | null): Session { |
9 | if (serviceId) { | 8 | if (serviceId) { |
@@ -22,14 +21,14 @@ export default async () => { | |||
22 | const serviceSession = deduceSession(serviceId); | 21 | const serviceSession = deduceSession(serviceId); |
23 | serviceSession.flushStorageData(); | 22 | serviceSession.flushStorageData(); |
24 | if (targetsToClear) { | 23 | if (targetsToClear) { |
25 | console.log('Clearing targets:', targetsToClear); | 24 | debug('Clearing targets:', targetsToClear); |
26 | serviceSession.clearStorageData(targetsToClear); | 25 | serviceSession.clearStorageData(targetsToClear); |
27 | } else { | 26 | } else { |
28 | console.log('Clearing all targets'); | 27 | debug('Clearing all targets'); |
29 | serviceSession.clearStorageData(); | 28 | serviceSession.clearStorageData(); |
30 | } | 29 | } |
31 | } catch (error) { | 30 | } catch (error) { |
32 | console.log(error); | 31 | debug(error); |
33 | } | 32 | } |
34 | }); | 33 | }); |
35 | 34 | ||
diff --git a/src/electron/macOSPermissions.ts b/src/electron/macOSPermissions.ts index 07caaaada..2415534e5 100644 --- a/src/electron/macOSPermissions.ts +++ b/src/electron/macOSPermissions.ts | |||
@@ -6,12 +6,11 @@ import { dirname } from 'path'; | |||
6 | import { askForScreenCaptureAccess } from 'node-mac-permissions'; | 6 | import { askForScreenCaptureAccess } from 'node-mac-permissions'; |
7 | import { userDataPath } from '../environment-remote'; | 7 | import { userDataPath } from '../environment-remote'; |
8 | 8 | ||
9 | // TODO: Go back to 'debug' from 'console.log' when https://github.com/electron/electron/issues/31689 is fixed | 9 | const debug = require('../preload-safe-debug')('Ferdium:macOSPermissions'); |
10 | // const debug = require('debug')('Ferdium:macOSPermissions'); | ||
11 | 10 | ||
12 | const isExplicitScreenCapturePermissionReqd = | 11 | const isExplicitScreenCapturePermissionReqd = |
13 | macosVersion.isGreaterThanOrEqualTo('10.15'); | 12 | macosVersion.isGreaterThanOrEqualTo('10.15'); |
14 | console.log( | 13 | debug( |
15 | `Should check explicitly for screen-capture permissions: ${isExplicitScreenCapturePermissionReqd}`, | 14 | `Should check explicitly for screen-capture permissions: ${isExplicitScreenCapturePermissionReqd}`, |
16 | ); | 15 | ); |
17 | 16 | ||
@@ -22,7 +21,7 @@ function hasPromptedForScreenCapturePermission(): string | boolean { | |||
22 | return false; | 21 | return false; |
23 | } | 22 | } |
24 | 23 | ||
25 | console.log('Checking if status file exists'); | 24 | debug('Checking if status file exists'); |
26 | return filePath && pathExistsSync(filePath); | 25 | return filePath && pathExistsSync(filePath); |
27 | } | 26 | } |
28 | 27 | ||
@@ -32,7 +31,7 @@ function hasScreenCapturePermissionAlreadyBeenGranted(): boolean { | |||
32 | } | 31 | } |
33 | 32 | ||
34 | const screenCaptureStatus = systemPreferences.getMediaAccessStatus('screen'); | 33 | const screenCaptureStatus = systemPreferences.getMediaAccessStatus('screen'); |
35 | console.log(`screen-capture permissions status: ${screenCaptureStatus}`); | 34 | debug(`screen-capture permissions status: ${screenCaptureStatus}`); |
36 | return screenCaptureStatus === 'granted'; | 35 | return screenCaptureStatus === 'granted'; |
37 | } | 36 | } |
38 | 37 | ||
@@ -50,18 +49,18 @@ function createStatusFile() { | |||
50 | } | 49 | } |
51 | 50 | ||
52 | export const askFormacOSPermissions = async (mainWindow: BrowserWindow) => { | 51 | export const askFormacOSPermissions = async (mainWindow: BrowserWindow) => { |
53 | console.log('Checking camera & microphone permissions'); | 52 | debug('Checking camera & microphone permissions'); |
54 | systemPreferences.askForMediaAccess('camera'); | 53 | systemPreferences.askForMediaAccess('camera'); |
55 | systemPreferences.askForMediaAccess('microphone'); | 54 | systemPreferences.askForMediaAccess('microphone'); |
56 | 55 | ||
57 | if (hasScreenCapturePermissionAlreadyBeenGranted()) { | 56 | if (hasScreenCapturePermissionAlreadyBeenGranted()) { |
58 | console.log('Already obtained screen-capture permissions - writing status file'); | 57 | debug('Already obtained screen-capture permissions - writing status file'); |
59 | createStatusFile(); | 58 | createStatusFile(); |
60 | return; | 59 | return; |
61 | } | 60 | } |
62 | 61 | ||
63 | if (!hasPromptedForScreenCapturePermission()) { | 62 | if (!hasPromptedForScreenCapturePermission()) { |
64 | console.log('Checking screen capture permissions'); | 63 | debug('Checking screen capture permissions'); |
65 | 64 | ||
66 | const { response } = await dialog.showMessageBox(mainWindow, { | 65 | const { response } = await dialog.showMessageBox(mainWindow, { |
67 | type: 'info', | 66 | type: 'info', |
@@ -74,11 +73,11 @@ export const askFormacOSPermissions = async (mainWindow: BrowserWindow) => { | |||
74 | }); | 73 | }); |
75 | 74 | ||
76 | if (response === 0) { | 75 | if (response === 0) { |
77 | console.log('Asking for access'); | 76 | debug('Asking for access'); |
78 | askForScreenCaptureAccess(); | 77 | askForScreenCaptureAccess(); |
79 | createStatusFile(); | 78 | createStatusFile(); |
80 | } else if (response === 1) { | 79 | } else if (response === 1) { |
81 | console.log("Don't ask again"); | 80 | debug("Don't ask again"); |
82 | createStatusFile(); | 81 | createStatusFile(); |
83 | } | 82 | } |
84 | } | 83 | } |