Skip to content

Commit

Permalink
Replace DetectRTC package with standard web APIs (MetaMask#7887)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Gudahtt authored and yqrashawn committed Feb 10, 2020
1 parent f99adca commit 7fd20fa
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 34 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@
"deep-extend": "^0.5.1",
"deep-freeze-strict": "1.1.1",
"detect-node": "^2.0.3",
"detectrtc": "^1.3.6",
"dnode": "^1.2.2",
"end-of-stream": "^1.1.0",
"eth-block-tracker": "^4.4.2",
Expand Down
56 changes: 28 additions & 28 deletions ui/lib/webcam-utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict'

import DetectRTC from 'detectrtc'
import {
ENVIRONMENT_TYPE_POPUP,
PLATFORM_BRAVE,
Expand All @@ -9,34 +8,35 @@ import {
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
}
}
}

Expand Down
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9278,11 +9278,6 @@ detective@^5.0.2:
defined "^1.0.0"
minimist "^1.1.1"

detectrtc@^1.3.6:
version "1.3.6"
resolved "https://registry.yarnpkg.com/detectrtc/-/detectrtc-1.3.6.tgz#dabc0353981a3da7732de969071c08b6dddd5b59"
integrity sha1-2rwDU5gaPadzLelpBxwItt3dW1k=

dezalgo@^1.0.0:
version "1.0.3"
resolved "https://registry.yarnpkg.com/dezalgo/-/dezalgo-1.0.3.tgz#7f742de066fc748bc8db820569dddce49bf0d456"
Expand Down

0 comments on commit 7fd20fa

Please sign in to comment.