From 28f5bbaf87eb69573d16e44a93f5e8efa6f6648a Mon Sep 17 00:00:00 2001 From: Mo Bitar Date: Fri, 21 Dec 2018 10:56:54 -0600 Subject: [PATCH] Use lockAfterDate for locking if timeout is put to sleep --- .../app/services/passcodeManager.js | 35 ++++++++++++++----- .../directives/account-menu.html.haml | 1 - 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/app/assets/javascripts/app/services/passcodeManager.js b/app/assets/javascripts/app/services/passcodeManager.js index 390471e87..357225fab 100644 --- a/app/assets/javascripts/app/services/passcodeManager.js +++ b/app/assets/javascripts/app/services/passcodeManager.js @@ -1,3 +1,5 @@ +const MillisecondsPerSecond = 1000; + class PasscodeManager { constructor($rootScope, authManager, storageManager) { @@ -161,7 +163,6 @@ class PasscodeManager { }); } - const MillisecondsPerSecond = 1000; PasscodeManager.AutoLockIntervalNone = 0; PasscodeManager.AutoLockIntervalImmediate = 1; PasscodeManager.AutoLockIntervalOneMinute = 60 * MillisecondsPerSecond; @@ -175,7 +176,7 @@ class PasscodeManager { return [ { value: PasscodeManager.AutoLockIntervalNone, - label: "None" + label: "Off" }, { value: PasscodeManager.AutoLockIntervalImmediate, @@ -183,24 +184,29 @@ class PasscodeManager { }, { value: PasscodeManager.AutoLockIntervalOneMinute, - label: "1 Min" + label: "1m" }, { value: PasscodeManager.AutoLockIntervalFiveMinutes, - label: "5 Min" + label: "5m" }, { value: PasscodeManager.AutoLockIntervalOneHour, - label: "1 Hr" + label: "1h" } ] } documentVisibilityChanged(visible) { - if(!visible) { - this.beginAutoLockTimer(); - } else { + if(visible) { + // check to see if lockAfterDate is not null, and if the application isn't locked. + // if that's the case, it needs to be locked immediately. + if(this.lockAfterDate && new Date() > this.lockAfterDate && !this.isLocked()) { + this.lockApplication(); + } this.cancelAutoLockTimer(); + } else { + this.beginAutoLockTimer(); } } @@ -210,13 +216,26 @@ class PasscodeManager { return; } + // Use a timeout if possible, but if the computer is put to sleep, timeouts won't work. + // Need to set a date as backup. this.lockAfterDate does not need to be persisted, as + // living in memory seems sufficient. If memory is cleared, then the application will lock anyway. + let addToNow = (seconds) => { + let date = new Date(); + date.setSeconds(date.getSeconds() + seconds); + return date; + } + + this.lockAfterDate = addToNow(interval / MillisecondsPerSecond); this.lockTimeout = setTimeout(() => { this.lockApplication(); + // We don't need to look at this anymore since we've succeeded with timeout lock + this.lockAfterDate = null; }, interval); } cancelAutoLockTimer() { clearTimeout(this.lockTimeout); + this.lockAfterDate = null; } } diff --git a/app/assets/templates/directives/account-menu.html.haml b/app/assets/templates/directives/account-menu.html.haml index 6b75744f9..60d0d357c 100644 --- a/app/assets/templates/directives/account-menu.html.haml +++ b/app/assets/templates/directives/account-menu.html.haml @@ -140,7 +140,6 @@ %div{"ng-if" => "hasPasscode() && !formData.showPasscodeForm"} .sk-p Passcode lock is enabled. - %span{"ng-if" => "isDesktopApplication()"} Your passcode will be required on new sessions after app quit. .sk-notification.contrast .sk-notification-title Options