aboutsummaryrefslogtreecommitdiffstats
path: root/ui/lib/webcam-utils.js
blob: eb717b23a80896315660ecaa0bd35d334c943cc2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
'use strict'

import DetectRTC from 'detectrtc'
const { ENVIRONMENT_TYPE_POPUP } = require('../../app/scripts/lib/enums')
const { getEnvironmentType, getPlatform } = require('../../app/scripts/lib/util')
const { PLATFORM_BRAVE, PLATFORM_FIREFOX } = require('../../app/scripts/lib/enums')

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'})
      }
    })
  }
}

module.exports = WebcamUtils