diff --git a/ui/app/components/app/modals/qr-scanner/qr-scanner.component.js b/ui/app/components/app/modals/qr-scanner/qr-scanner.component.js index 1537c0f52..0bd4ac79a 100644 --- a/ui/app/components/app/modals/qr-scanner/qr-scanner.component.js +++ b/ui/app/components/app/modals/qr-scanner/qr-scanner.component.js @@ -1,99 +1,139 @@ import React, { Component } from 'react' import PropTypes from 'prop-types' +import log from 'loglevel' import { BrowserQRCodeReader } from '@zxing/library' +import { getEnvironmentType } from '../../../../../../app/scripts/lib/util' +import { ENVIRONMENT_TYPE_FULLSCREEN } from '../../../../../../app/scripts/lib/enums' import Spinner from '../../../ui/spinner' import WebcamUtils from '../../../../../lib/webcam-utils' import PageContainerFooter from '../../../ui/page-container/page-container-footer/page-container-footer.component' +const READY_STATE = { + ACCESSING_CAMERA: 'ACCESSING_CAMERA', + NEED_TO_ALLOW_ACCESS: 'NEED_TO_ALLOW_ACCESS', + READY: 'READY', +} + export default class QrScanner extends Component { static propTypes = { hideModal: PropTypes.func.isRequired, - qrCodeDetected: PropTypes.func, - scanQrCode: PropTypes.func, - error: PropTypes.bool, - errorType: PropTypes.string, + qrCodeDetected: PropTypes.func.isRequired, } static contextTypes = { t: PropTypes.func, } - constructor (props, context) { + constructor (props) { super(props) - this.state = { - ready: false, - msg: context.t('accessingYourCamera'), - } + this.state = this.getInitialState() this.codeReader = null this.permissionChecker = null - this.needsToReinit = false + this.mounted = false // Clear pre-existing qr code data before scanning this.props.qrCodeDetected(null) } componentDidMount () { + this.mounted = true + this.checkEnvironment() + } + + componentDidUpdate (_, prevState) { + const { ready } = this.state + + if (prevState.ready !== ready) { + if (ready === READY_STATE.READY) { + this.initCamera() + } else if (ready === READY_STATE.NEED_TO_ALLOW_ACCESS) { + this.checkPermissions() + } + } + } + + getInitialState () { + return { + ready: READY_STATE.ACCESSING_CAMERA, + error: null, + } + } + + checkEnvironment = async () => { + try { + const { environmentReady } = await WebcamUtils.checkStatus() + if (!environmentReady && getEnvironmentType() !== ENVIRONMENT_TYPE_FULLSCREEN) { + const currentUrl = new URL(window.location.href) + const currentHash = currentUrl.hash + const currentRoute = currentHash + ? currentHash.substring(1) + : null + global.platform.openExtensionInBrowser(currentRoute) + } + } catch (error) { + if (this.mounted) { + this.setState({ error }) + } + } + // initial attempt is required to trigger permission prompt this.initCamera() } - async checkPermisisions () { - const { permissions } = await WebcamUtils.checkStatus() - if (permissions) { - clearTimeout(this.permissionChecker) - // Let the video stream load first... - setTimeout(_ => { - this.setState({ - ready: true, - msg: this.context.t('scanInstructions'), - }) - if (this.needsToReinit) { - this.initCamera() - this.needsToReinit = false + checkPermissions = async () => { + try { + const { permissions } = await WebcamUtils.checkStatus() + if (permissions) { + // Let the video stream load first... + await new Promise(resolve => setTimeout(resolve, 2000)) + if (!this.mounted) { + return } - }, 2000) - } else { - // Keep checking for permissions - this.permissionChecker = setTimeout(_ => { - this.checkPermisisions() - }, 1000) + this.setState({ ready: READY_STATE.READY }) + } else if (this.mounted) { + // Keep checking for permissions + this.permissionChecker = setTimeout(this.checkPermissions, 1000) + } + } catch (error) { + if (this.mounted) { + this.setState({ error }) + } } } componentWillUnmount () { + this.mounted = false clearTimeout(this.permissionChecker) if (this.codeReader) { this.codeReader.reset() } } - initCamera () { + initCamera = async () => { this.codeReader = new BrowserQRCodeReader() - this.codeReader.getVideoInputDevices() - .then(() => { - clearTimeout(this.permissionChecker) - this.checkPermisisions() - this.codeReader.decodeFromInputVideoDevice(undefined, 'video') - .then(content => { - const result = this.parseContent(content.text) - if (result.type !== 'unknown') { - this.props.qrCodeDetected(result) - this.stopAndClose() - } else { - this.setState({ msg: this.context.t('unknownQrCode') }) - } - }) - .catch(err => { - if (err && err.name === 'NotAllowedError') { - this.setState({ msg: this.context.t('youNeedToAllowCameraAccess') }) - clearTimeout(this.permissionChecker) - this.needsToReinit = true - this.checkPermisisions() - } - }) - }).catch(err => { - console.error('[QR-SCANNER]: getVideoInputDevices threw an exception: ', err) - }) + try { + await this.codeReader.getVideoInputDevices() + const content = await this.codeReader.decodeFromInputVideoDevice(undefined, 'video') + const result = this.parseContent(content.text) + if (!this.mounted) { + return + } else if (result.type !== 'unknown') { + this.props.qrCodeDetected(result) + this.stopAndClose() + } else { + this.setState({ error: new Error(this.context.t('unknownQrCode')) }) + } + } catch (error) { + if (!this.mounted) { + return + } + if (error.name === 'NotAllowedError') { + log.info(`Permission denied: '${error}'`) + this.setState({ ready: READY_STATE.NEED_TO_ALLOW_ACCESS }) + } else { + this.setState({ error }) + } + } } parseContent (content) { @@ -126,89 +166,108 @@ export default class QrScanner extends Component { if (this.codeReader) { this.codeReader.reset() } - this.setState({ ready: false }) this.props.hideModal() } tryAgain = () => { - // close the modal - this.stopAndClose() - // wait for the animation and try again - setTimeout(_ => { - this.props.scanQrCode() - }, 1000) + clearTimeout(this.permissionChecker) + if (this.codeReader) { + this.codeReader.reset() + } + this.setState(this.getInitialState(), () => { + this.checkEnvironment() + }) } - renderVideo () { - return ( -