@ -1,34 +1,35 @@
'use strict'
import DetectRTC from 'detectrtc'
import { ENVIRONMENT _TYPE _POPUP , PLATFORM _BRAVE , PLATFORM _FIREFOX } from '../../app/scripts/lib/enums'
import { getEnvironmentType , getPlatform } from '../../app/scripts/lib/util'
class WebcamUtils {
static checkStatus ( ) {
return new Promise ( ( resolve , reject ) => {
const isPopup = getEnvironmentType ( window . location . href ) === ENVIRONMENT _TYPE _POPUP
const isFirefoxOrBrave = getPlatform ( ) === ( PLATFORM _FIREFOX || PLATFORM _BRAVE )
try {
DetectRTC . load ( _ => {
if ( DetectRTC . hasWebcam ) {
let environmentReady = true
if ( ( isFirefoxOrBrave && isPopup ) || ( isPopup && ! DetectRTC . isWebsiteHasWebcamPermissions ) ) {
environmentReady = false
}
resolve ( {
permissions : DetectRTC . isWebsiteHasWebcamPermissions ,
environmentReady ,
} )
} else {
reject ( { type : 'NO_WEBCAM_FOUND' } )
}
} )
} catch ( e ) {
reject ( { type : 'UNKNOWN_ERROR' } )
static async checkStatus ( ) {
const isPopup = getEnvironmentType ( window . location . href ) === ENVIRONMENT _TYPE _POPUP
const isFirefoxOrBrave = getPlatform ( ) === ( PLATFORM _FIREFOX || PLATFORM _BRAVE )
const devices = await window . navigator . mediaDevices . enumerateDevices ( )
const webcams = devices . filter ( device => device . kind === 'videoinput' )
const hasWebcam = webcams . length > 0
// A non-empty-string label implies that the webcam has been granted permission, as
// otherwise the label is kept blank to prevent fingerprinting
const hasWebcamPermissions = webcams . some ( webcam => webcam . label && webcam . label . length > 0 )
if ( hasWebcam ) {
let environmentReady = true
if ( ( isFirefoxOrBrave && isPopup ) || ( isPopup && ! hasWebcamPermissions ) ) {
environmentReady = false
}
return {
permissions : hasWebcamPermissions ,
environmentReady ,
}
} )
} else {
const error = new Error ( 'No webcam found' )
error . type = 'NO_WEBCAM_FOUND'
throw error
}
}
}