1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00

Add Idle Timeout for Sync with mobile (#8201)

* Add idle timeout for sync

* refactor a bit with `goBack` and `clearTimeouts`

* Address lint nit

* Rename handleIdleTimeout -> startIdleTimeout
This commit is contained in:
ricky 2020-03-24 01:00:59 -04:00 committed by GitHub
parent e729add61d
commit b7b827b01c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -12,6 +12,7 @@ import LoadingScreen from '../../components/ui/loading-screen'
const PASSWORD_PROMPT_SCREEN = 'PASSWORD_PROMPT_SCREEN' const PASSWORD_PROMPT_SCREEN = 'PASSWORD_PROMPT_SCREEN'
const REVEAL_SEED_SCREEN = 'REVEAL_SEED_SCREEN' const REVEAL_SEED_SCREEN = 'REVEAL_SEED_SCREEN'
const KEYS_GENERATION_TIME = 30000 const KEYS_GENERATION_TIME = 30000
const IDLE_TIME = KEYS_GENERATION_TIME * 4
export default class MobileSyncPage extends Component { export default class MobileSyncPage extends Component {
static contextTypes = { static contextTypes = {
@ -47,27 +48,45 @@ export default class MobileSyncPage extends Component {
} }
} }
startIdleTimeout () {
this.idleTimeout = setTimeout(() => {
this.clearTimeouts()
this.goBack()
}, IDLE_TIME)
}
handleSubmit (event) { handleSubmit (event) {
event.preventDefault() event.preventDefault()
this.setState({ seedWords: null, error: null }) this.setState({ seedWords: null, error: null })
this.props.requestRevealSeedWords(this.state.password) this.props.requestRevealSeedWords(this.state.password)
.then((seedWords) => { .then((seedWords) => {
this.startKeysGeneration() this.startKeysGeneration()
this.startIdleTimeout()
this.setState({ seedWords, screen: REVEAL_SEED_SCREEN }) this.setState({ seedWords, screen: REVEAL_SEED_SCREEN })
}) })
.catch((error) => this.setState({ error: error.message })) .catch((error) => this.setState({ error: error.message }))
} }
startKeysGeneration () { startKeysGeneration () {
this.handle && clearTimeout(this.handle) this.keysGenerationTimeout && clearTimeout(this.keysGenerationTimeout)
this.disconnectWebsockets() this.disconnectWebsockets()
this.generateCipherKeyAndChannelName() this.generateCipherKeyAndChannelName()
this.initWebsockets() this.initWebsockets()
this.handle = setTimeout(() => { this.keysGenerationTimeout = setTimeout(() => {
this.startKeysGeneration() this.startKeysGeneration()
}, KEYS_GENERATION_TIME) }, KEYS_GENERATION_TIME)
} }
goBack () {
const { history } = this.props
history.push(DEFAULT_ROUTE)
}
clearTimeouts () {
this.keysGenerationTimeout && clearTimeout(this.keysGenerationTimeout)
this.idleTimeout && clearTimeout(this.idleTimeout)
}
generateCipherKeyAndChannelName () { generateCipherKeyAndChannelName () {
this.cipherKey = `${this.props.selectedAddress.substr(-4)}-${PubNub.generateUUID()}` this.cipherKey = `${this.props.selectedAddress.substr(-4)}-${PubNub.generateUUID()}`
this.channelName = `mm-${PubNub.generateUUID()}` this.channelName = `mm-${PubNub.generateUUID()}`
@ -101,7 +120,7 @@ export default class MobileSyncPage extends Component {
if (message.event === 'start-sync') { if (message.event === 'start-sync') {
this.startSyncing() this.startSyncing()
} else if (message.event === 'connection-info') { } else if (message.event === 'connection-info') {
this.handle && clearTimeout(this.handle) this.keysGenerationTimeout && clearTimeout(this.keysGenerationTimeout)
this.disconnectWebsockets() this.disconnectWebsockets()
this.initWithCipherKeyAndChannelName(message.cipher, message.channel) this.initWithCipherKeyAndChannelName(message.cipher, message.channel)
this.initWebsockets() this.initWebsockets()
@ -226,7 +245,7 @@ export default class MobileSyncPage extends Component {
componentWillUnmount () { componentWillUnmount () {
this.handle && clearTimeout(this.handle) this.clearTimeouts()
this.disconnectWebsockets() this.disconnectWebsockets()
} }
@ -353,7 +372,6 @@ export default class MobileSyncPage extends Component {
renderPasswordPromptFooter () { renderPasswordPromptFooter () {
const { t } = this.context const { t } = this.context
const { history } = this.props
const { password } = this.state const { password } = this.state
return ( return (
@ -362,7 +380,7 @@ export default class MobileSyncPage extends Component {
type="default" type="default"
large large
className="new-account-create-form__button" className="new-account-create-form__button"
onClick={() => history.push(DEFAULT_ROUTE)} onClick={() => this.goBack()}
> >
{t('cancel')} {t('cancel')}
</Button> </Button>
@ -381,7 +399,6 @@ export default class MobileSyncPage extends Component {
renderRevealSeedFooter () { renderRevealSeedFooter () {
const { t } = this.context const { t } = this.context
const { history } = this.props
return ( return (
<div className="page-container__footer" style={{ padding: 30 }}> <div className="page-container__footer" style={{ padding: 30 }}>
@ -389,7 +406,7 @@ export default class MobileSyncPage extends Component {
type="default" type="default"
large large
className="page-container__footer-button" className="page-container__footer-button"
onClick={() => history.push(DEFAULT_ROUTE)} onClick={() => this.goBack()}
> >
{t('close')} {t('close')}
</Button> </Button>