Skip to content

Commit 6e1bfc3

Browse files
author
Christopher Willis-Ford
committed
only call systemPreferences.askForMediaAccess if it exists
1 parent 7496870 commit 6e1bfc3

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

src/main/index.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,23 @@ const makeFullUrl = (url, search = null) =>
7575
}
7676
));
7777

78+
/**
79+
* Prompt in a platform-specific way for permission to access the microphone or camera, if Electron supports doing so.
80+
* Any application-level checks, such as whether or not a particular frame or document should be allowed to ask,
81+
* should be done before calling this function.
82+
*
83+
* @param {string} mediaType - one of Electron's media types, like 'microphone' or 'camera'
84+
* @returns {boolean} - true if permission granted, false otherwise.
85+
*/
86+
const askForMediaAccess = async mediaType => {
87+
if (systemPreferences.askForMediaAccess) {
88+
// Electron currently only implements this on macOS
89+
return await systemPreferences.askForMediaAccess(mediaType);
90+
}
91+
// For other platforms we can't reasonably do anything other than assume we have access.
92+
return true;
93+
};
94+
7895
const handlePermissionRequest = async (webContents, permission, callback, details) => {
7996
if (webContents !== _windows.main.webContents) {
8097
// deny: request came from somewhere other than the main window's web contents
@@ -110,14 +127,14 @@ const handlePermissionRequest = async (webContents, permission, callback, detail
110127
}
111128
const parentWindow = _windows.main; // if we ever allow media in non-main windows we'll also need to change this
112129
if (askForMicrophone) {
113-
const microphoneResult = await systemPreferences.askForMediaAccess('microphone');
130+
const microphoneResult = await askForMediaAccess('microphone');
114131
if (!microphoneResult) {
115132
displayPermissionDeniedWarning(parentWindow, 'microphone');
116133
return callback(false);
117134
}
118135
}
119136
if (askForCamera) {
120-
const cameraResult = await systemPreferences.askForMediaAccess('camera');
137+
const cameraResult = await askForMediaAccess('camera');
121138
if (!cameraResult) {
122139
displayPermissionDeniedWarning(parentWindow, 'camera');
123140
return callback(false);

0 commit comments

Comments
 (0)