idle locking and lock service refactor

This commit is contained in:
Kyle Spearrin 2017-06-05 14:27:24 -04:00
parent 17694ed1e2
commit 96be646641
5 changed files with 146 additions and 66 deletions

View File

@ -427,6 +427,10 @@
"message": "4 hours", "message": "4 hours",
"description": "4 hours" "description": "4 hours"
}, },
"onLocked": {
"message": "On Locked",
"description": "On Locked"
},
"onRestart": { "onRestart": {
"message": "On Restart", "message": "On Restart",
"description": "On Restart" "description": "On Restart"

View File

@ -11,11 +11,13 @@ var userService = new UserService(tokenService, apiService, cryptoService);
var settingsService = new SettingsService(userService); var settingsService = new SettingsService(userService);
var loginService = new LoginService(cryptoService, userService, apiService, settingsService); var loginService = new LoginService(cryptoService, userService, apiService, settingsService);
var folderService = new FolderService(cryptoService, userService, apiService); var folderService = new FolderService(cryptoService, userService, apiService);
var lockService = new LockService(constantsService, cryptoService, folderService, loginService, setIcon, refreshBadgeAndMenu);
var syncService = new SyncService(loginService, folderService, userService, apiService, settingsService, var syncService = new SyncService(loginService, folderService, userService, apiService, settingsService,
cryptoService, logout); cryptoService, logout);
var autofillService = new AutofillService(); var autofillService = new AutofillService();
var passwordGenerationService = new PasswordGenerationService(); var passwordGenerationService = new PasswordGenerationService();
if (chrome.commands) {
chrome.commands.onCommand.addListener(function (command) { chrome.commands.onCommand.addListener(function (command) {
if (command === 'generate_password') { if (command === 'generate_password') {
ga('send', { ga('send', {
@ -28,6 +30,7 @@ chrome.commands.onCommand.addListener(function (command) {
}); });
} }
}); });
}
var loginToAutoFill = null, var loginToAutoFill = null,
pageDetailsToAutoFill = [], pageDetailsToAutoFill = [],
@ -738,57 +741,3 @@ function fullSync(override) {
} }
}); });
} }
// Locking
var lastLockCheck = null;
checkLock();
setInterval(checkLock, 10 * 1000); // check every 10 seconds
function checkLock() {
var now = new Date();
if (lastLockCheck && (now - lastLockCheck) < 5000) {
// can only check lock every 5 seconds
return;
}
lastLockCheck = now;
if (chrome.extension.getViews({ type: 'popup' }).length > 0) {
// popup is open, do not lock
return;
}
cryptoService.getKey().then(function (key) {
if (!key) {
// no key so no need to lock
return;
}
chrome.storage.local.get(constantsService.lockOptionKey, function (obj) {
if (obj && ((!obj[constantsService.lockOptionKey] && obj[constantsService.lockOptionKey] !== 0) ||
obj[constantsService.lockOptionKey] === -1)) {
// no lock option set
return;
}
chrome.storage.local.get(constantsService.lastActiveKey, function (obj2) {
if (obj2 && obj2[constantsService.lastActiveKey]) {
var lastActive = obj2[constantsService.lastActiveKey];
var diffSeconds = ((new Date()).getTime() - lastActive) / 1000;
var lockOptionSeconds = parseInt(obj[constantsService.lockOptionKey]) * 60;
if (diffSeconds >= lockOptionSeconds) {
// need to lock now
Q.all([cryptoService.clearKey(), cryptoService.clearOrgKeys(true)]).then(function () {
cryptoService.clearPrivateKey();
setIcon();
folderService.clearCache();
loginService.clearCache();
refreshBadgeAndMenu();
});
}
}
});
});
});
};

View File

@ -58,6 +58,7 @@
"storage", "storage",
"unlimitedStorage", "unlimitedStorage",
"clipboardWrite", "clipboardWrite",
"idle",
"http://*/*", "http://*/*",
"https://*/*" "https://*/*"
], ],

View File

@ -18,6 +18,7 @@
<option value="30">{{i18n.thirtyMinutes}}</option> <option value="30">{{i18n.thirtyMinutes}}</option>
<option value="60">{{i18n.oneHour}}</option> <option value="60">{{i18n.oneHour}}</option>
<option value="240">{{i18n.fourHours}}</option> <option value="240">{{i18n.fourHours}}</option>
<option value="-2">{{i18n.onLocked}}</option>
<option value="-1">{{i18n.onRestart}}</option> <option value="-1">{{i18n.onRestart}}</option>
<option value="">{{i18n.never}}</option> <option value="">{{i18n.never}}</option>
</select> </select>

125
src/services/lockService.js Normal file
View File

@ -0,0 +1,125 @@
function LockService(constantsService, cryptoService, folderService, loginService, setIcon, refreshBadgeAndMenu) {
this.lastLockCheck = null;
this.constantsService = constantsService;
this.cryptoService = cryptoService;
this.folderService = folderService;
this.loginService = loginService;
this.setIcon = setIcon;
this.refreshBadgeAndMenu = refreshBadgeAndMenu;
initLockService(this);
};
function initLockService(self) {
checkLock();
setInterval(checkLock, 10 * 1000); // check every 10 seconds
function checkLock() {
var now = new Date();
if (self.lastLockCheck && (now - self.lastLockCheck) < 5000) {
// can only check lock every 5 seconds
return;
}
self.lastLockCheck = now;
if (chrome.extension.getViews({ type: 'popup' }).length > 0) {
// popup is open, do not lock
return;
}
var lockOptionSeconds = null;
self.cryptoService.getKey().then(function (key) {
if (!key) {
// no key so no need to lock
return false;
}
return getLockOption();
}).then(function (lockOption) {
if (lockOption === false || lockOption < 0) {
return;
}
lockOptionSeconds = lockOption * 60;
return getLastActive();
}).then(function (lastActive) {
if (lockOptionSeconds === null) {
return;
}
var diffSeconds = ((new Date()).getTime() - lastActive) / 1000;
if (diffSeconds >= lockOptionSeconds) {
// need to lock now
self.lock();
}
});
}
if (chrome.idle && chrome.idle.onStateChanged) {
chrome.idle.onStateChanged.addListener(function (newState) {
if (newState === 'locked') {
getLockOption().then(function (lockOption) {
if (lockOption === -2) {
self.lock();
}
});
}
});
}
LockService.prototype.lock = function () {
Q.all([self.cryptoService.clearKey(), self.cryptoService.clearOrgKeys(true)]).then(function () {
self.cryptoService.clearPrivateKey();
self.setIcon();
self.folderService.clearCache();
self.loginService.clearCache();
self.refreshBadgeAndMenu();
});
};
function getLockOption() {
var deferred = Q.defer();
chrome.storage.local.get(self.constantsService.lockOptionKey, function (obj) {
if (obj && obj[constantsService.lockOptionKey] === 0 || obj[constantsService.lockOptionKey]) {
deferred.resolve(parseInt(obj[self.constantsService.lockOptionKey]));
}
else {
deferred.reject();
}
});
return deferred.promise;
}
function getLastActive() {
var deferred = Q.defer();
chrome.storage.local.get(self.constantsService.lastActiveKey, function (obj) {
if (obj && obj[constantsService.lastActiveKey]) {
deferred.resolve(obj[constantsService.lastActiveKey]);
}
else {
deferred.reject();
}
});
return deferred.promise;
}
function getIdleState(detectionInterval) {
detectionInterval = detectionInterval || (60 * 5);
var deferred = Q.defer();
if (chrome.idle && chrome.idle.queryState) {
chrome.idle.queryState(detectionInterval, function (state) {
deferred.resolve(state);
});
}
else {
deferred.resolve('active');
}
return deferred.promise;
}
};