1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-10-23 03:36:18 +02:00
metamask-extension/ui/lib/webcam-utils.js
Mark Stacey e79d18de2a
Replace DetectRTC package with standard web APIs (#7887)
The only web API that our usage of DetectRTC relied upon was
'enumerateDevices', which is supported and stable among all of our
supported browsers.

Note that the error handling here is a little... non-standard, and the
logic around how Firefox and Brave are handled should be justified, but
I've left the logic as-is for now to keep this PR small.
2020-01-23 14:04:16 -04:00

37 lines
1.3 KiB
JavaScript

'use strict'
import { ENVIRONMENT_TYPE_POPUP, PLATFORM_BRAVE, PLATFORM_FIREFOX } from '../../app/scripts/lib/enums'
import { getEnvironmentType, getPlatform } from '../../app/scripts/lib/util'
class WebcamUtils {
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
}
}
}
export default WebcamUtils