Skip to content

Commit

Permalink
Replacing setTimeout in auto-lock time limit with chrome alarm (#15931)
Browse files Browse the repository at this point in the history
  • Loading branch information
NiranjanaBinoy authored Sep 24, 2022
1 parent e74614d commit c836f2f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
1 change: 1 addition & 0 deletions app/manifest/v3/_base.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"name": "__MSG_appName__",
"permissions": [
"activeTab",
"alarms",
"clipboardWrite",
"notifications",
"scripting",
Expand Down
35 changes: 30 additions & 5 deletions app/scripts/controllers/app-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import EventEmitter from 'events';
import { ObservableStore } from '@metamask/obs-store';
import { METAMASK_CONTROLLER_EVENTS } from '../metamask-controller';
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 {
/**
Expand Down Expand Up @@ -187,21 +189,44 @@ export default class AppStateController extends EventEmitter {
*
* @private
*/
/* eslint-disable no-undef */
_resetTimer() {
const { timeoutMinutes } = this.store.getState();

if (this.timer) {
clearTimeout(this.timer);
if (isManifestV3) {
chrome.alarms.clear(AUTO_LOCK_TIMEOUT_ALARM);
} else {
clearTimeout(this.timer);
}
}

if (!timeoutMinutes) {
return;
}

this.timer = setTimeout(
() => this.onInactiveTimeout(),
timeoutMinutes * MINUTE,
);
if (isManifestV3) {
chrome.alarms.create(AUTO_LOCK_TIMEOUT_ALARM, {
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,
);
}
}

/**
Expand Down
1 change: 1 addition & 0 deletions shared/constants/alarms.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const AUTO_LOCK_TIMEOUT_ALARM = 'AUTO_LOCK_TIMEOUT_ALARM';

0 comments on commit c836f2f

Please sign in to comment.