1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-29 23:58:06 +01:00
metamask-extension/ui/helpers/utils/webcam-utils.js

42 lines
1.3 KiB
JavaScript
Raw Normal View History

'use strict';
2020-11-03 00:41:28 +01:00
import {
ENVIRONMENT_TYPE_POPUP,
PLATFORM_BRAVE,
PLATFORM_FIREFOX,
} from '../../../shared/constants/app';
import { getEnvironmentType, getPlatform } from '../../../app/scripts/lib/util';
class WebcamUtils {
2020-11-03 00:41:28 +01:00
static async checkStatus() {
const isPopup = getEnvironmentType() === ENVIRONMENT_TYPE_POPUP;
2020-11-03 00:41:28 +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
2020-11-03 00:41:28 +01:00
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,
};
}
const error = new Error('No webcam found');
error.type = 'NO_WEBCAM_FOUND';
throw error;
}
}
export default WebcamUtils;