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

Replacing setTimeout in auto-lock time limit with chrome alarm (#15931)

This commit is contained in:
Niranjana Binoy 2022-09-23 20:39:25 -04:00 committed by GitHub
parent e74614dbec
commit c836f2f2ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 5 deletions

View File

@ -67,6 +67,7 @@
"name": "__MSG_appName__", "name": "__MSG_appName__",
"permissions": [ "permissions": [
"activeTab", "activeTab",
"alarms",
"clipboardWrite", "clipboardWrite",
"notifications", "notifications",
"scripting", "scripting",

View File

@ -2,6 +2,8 @@ import EventEmitter from 'events';
import { ObservableStore } from '@metamask/obs-store'; import { ObservableStore } from '@metamask/obs-store';
import { METAMASK_CONTROLLER_EVENTS } from '../metamask-controller'; import { METAMASK_CONTROLLER_EVENTS } from '../metamask-controller';
import { MINUTE } from '../../../shared/constants/time'; import { MINUTE } from '../../../shared/constants/time';
import { AUTO_LOCK_TIMEOUT_ALARM } from '../../../shared/constants/alarms';
import { isManifestV3 } from '../../../shared/modules/mv3.utils';
export default class AppStateController extends EventEmitter { export default class AppStateController extends EventEmitter {
/** /**
@ -187,21 +189,44 @@ export default class AppStateController extends EventEmitter {
* *
* @private * @private
*/ */
/* eslint-disable no-undef */
_resetTimer() { _resetTimer() {
const { timeoutMinutes } = this.store.getState(); const { timeoutMinutes } = this.store.getState();
if (this.timer) { if (this.timer) {
clearTimeout(this.timer); if (isManifestV3) {
chrome.alarms.clear(AUTO_LOCK_TIMEOUT_ALARM);
} else {
clearTimeout(this.timer);
}
} }
if (!timeoutMinutes) { if (!timeoutMinutes) {
return; return;
} }
this.timer = setTimeout( if (isManifestV3) {
() => this.onInactiveTimeout(), chrome.alarms.create(AUTO_LOCK_TIMEOUT_ALARM, {
timeoutMinutes * MINUTE, delayInMinutes: timeoutMinutes,
); periodInMinutes: timeoutMinutes,
});
chrome.alarms.onAlarm.addListener(() => {
chrome.alarms.getAll((alarms) => {
const hasAlarm = alarms.find(
(alarm) => alarm.name === AUTO_LOCK_TIMEOUT_ALARM,
);
if (hasAlarm) {
this.onInactiveTimeout();
chrome.alarms.clear(AUTO_LOCK_TIMEOUT_ALARM);
}
});
});
} else {
this.timer = setTimeout(
() => this.onInactiveTimeout(),
timeoutMinutes * MINUTE,
);
}
} }
/** /**

View File

@ -0,0 +1 @@
export const AUTO_LOCK_TIMEOUT_ALARM = 'AUTO_LOCK_TIMEOUT_ALARM';