2018-08-01 00:30:40 +02:00
|
|
|
'use strict'
|
|
|
|
|
2020-01-09 04:34:58 +01:00
|
|
|
import { ENVIRONMENT_TYPE_POPUP, PLATFORM_BRAVE, PLATFORM_FIREFOX } from '../../app/scripts/lib/enums'
|
|
|
|
import { getEnvironmentType, getPlatform } from '../../app/scripts/lib/util'
|
2018-08-01 00:30:40 +02:00
|
|
|
|
|
|
|
class WebcamUtils {
|
|
|
|
|
2020-01-23 19:04:16 +01:00
|
|
|
static async checkStatus () {
|
2020-01-24 20:12:58 +01:00
|
|
|
const isPopup = getEnvironmentType() === ENVIRONMENT_TYPE_POPUP
|
2020-01-23 19:04:16 +01:00
|
|
|
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,
|
2018-08-04 01:36:01 +02:00
|
|
|
}
|
2020-01-23 19:04:16 +01:00
|
|
|
} else {
|
|
|
|
const error = new Error('No webcam found')
|
|
|
|
error.type = 'NO_WEBCAM_FOUND'
|
|
|
|
throw error
|
|
|
|
}
|
2018-08-01 00:30:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-09 04:34:58 +01:00
|
|
|
export default WebcamUtils
|