@@ -75,6 +75,23 @@ const makeFullUrl = (url, search = null) =>
75
75
}
76
76
) ) ;
77
77
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
+
78
95
const handlePermissionRequest = async ( webContents , permission , callback , details ) => {
79
96
if ( webContents !== _windows . main . webContents ) {
80
97
// deny: request came from somewhere other than the main window's web contents
@@ -110,14 +127,14 @@ const handlePermissionRequest = async (webContents, permission, callback, detail
110
127
}
111
128
const parentWindow = _windows . main ; // if we ever allow media in non-main windows we'll also need to change this
112
129
if ( askForMicrophone ) {
113
- const microphoneResult = await systemPreferences . askForMediaAccess ( 'microphone' ) ;
130
+ const microphoneResult = await askForMediaAccess ( 'microphone' ) ;
114
131
if ( ! microphoneResult ) {
115
132
displayPermissionDeniedWarning ( parentWindow , 'microphone' ) ;
116
133
return callback ( false ) ;
117
134
}
118
135
}
119
136
if ( askForCamera ) {
120
- const cameraResult = await systemPreferences . askForMediaAccess ( 'camera' ) ;
137
+ const cameraResult = await askForMediaAccess ( 'camera' ) ;
121
138
if ( ! cameraResult ) {
122
139
displayPermissionDeniedWarning ( parentWindow , 'camera' ) ;
123
140
return callback ( false ) ;
0 commit comments