From 6e1bfc33f3d95da60b57bd95d7e3eb9a5ca8af88 Mon Sep 17 00:00:00 2001 From: Christopher Willis-Ford Date: Tue, 17 Mar 2020 16:15:46 -0700 Subject: [PATCH] only call systemPreferences.askForMediaAccess if it exists --- src/main/index.js | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/main/index.js b/src/main/index.js index 2b83665..eaa132f 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -75,6 +75,23 @@ const makeFullUrl = (url, search = null) => } )); +/** + * Prompt in a platform-specific way for permission to access the microphone or camera, if Electron supports doing so. + * Any application-level checks, such as whether or not a particular frame or document should be allowed to ask, + * should be done before calling this function. + * + * @param {string} mediaType - one of Electron's media types, like 'microphone' or 'camera' + * @returns {boolean} - true if permission granted, false otherwise. + */ +const askForMediaAccess = async mediaType => { + if (systemPreferences.askForMediaAccess) { + // Electron currently only implements this on macOS + return await systemPreferences.askForMediaAccess(mediaType); + } + // For other platforms we can't reasonably do anything other than assume we have access. + return true; +}; + const handlePermissionRequest = async (webContents, permission, callback, details) => { if (webContents !== _windows.main.webContents) { // deny: request came from somewhere other than the main window's web contents @@ -110,14 +127,14 @@ const handlePermissionRequest = async (webContents, permission, callback, detail } const parentWindow = _windows.main; // if we ever allow media in non-main windows we'll also need to change this if (askForMicrophone) { - const microphoneResult = await systemPreferences.askForMediaAccess('microphone'); + const microphoneResult = await askForMediaAccess('microphone'); if (!microphoneResult) { displayPermissionDeniedWarning(parentWindow, 'microphone'); return callback(false); } } if (askForCamera) { - const cameraResult = await systemPreferences.askForMediaAccess('camera'); + const cameraResult = await askForMediaAccess('camera'); if (!cameraResult) { displayPermissionDeniedWarning(parentWindow, 'camera'); return callback(false);