1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-24 12:06:15 +01:00

track last used autofill for priority filling

This commit is contained in:
Kyle Spearrin 2017-08-29 12:52:11 -04:00
parent 8804cc5a96
commit 518585cfb5
4 changed files with 99 additions and 23 deletions

View File

@ -83,7 +83,7 @@ chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) {
messageTab(msg.tab.id, 'pageDetails', { details: msg.details, forms: forms }); messageTab(msg.tab.id, 'pageDetails', { details: msg.details, forms: forms });
} }
else if (msg.sender === 'autofiller') { else if (msg.sender === 'autofiller') {
bg_autofillService.doAutoFillForFirstLogin([{ bg_autofillService.doAutoFillForLastUsedLogin([{
frameId: sender.frameId, tab: msg.tab, details: msg.details frameId: sender.frameId, tab: msg.tab, details: msg.details
}]); }]);
} }

View File

@ -88,13 +88,14 @@ var CipherString = function () {
} }
}; };
var Login = function (obj, alreadyEncrypted) { var Login = function (obj, alreadyEncrypted, localData) {
this.id = obj.id ? obj.id : null; this.id = obj.id ? obj.id : null;
this.organizationId = obj.organizationId ? obj.organizationId : null; this.organizationId = obj.organizationId ? obj.organizationId : null;
this.folderId = obj.folderId ? obj.folderId : null; this.folderId = obj.folderId ? obj.folderId : null;
this.favorite = obj.favorite ? true : false; this.favorite = obj.favorite ? true : false;
this.organizationUseTotp = obj.organizationUseTotp ? true : false; this.organizationUseTotp = obj.organizationUseTotp ? true : false;
this.edit = obj.edit ? true : false; this.edit = obj.edit ? true : false;
this.localData = localData;
if (alreadyEncrypted === true) { if (alreadyEncrypted === true) {
this.name = obj.name ? obj.name : null; this.name = obj.name ? obj.name : null;
@ -176,7 +177,8 @@ var Folder = function (obj, alreadyEncrypted) {
id: self.id, id: self.id,
organizationId: self.organizationId, organizationId: self.organizationId,
folderId: self.folderId, folderId: self.folderId,
favorite: self.favorite favorite: self.favorite,
localData: self.localData
}; };
var attachments = []; var attachments = [];

View File

@ -135,7 +135,7 @@ function initAutofill() {
return formData; return formData;
}; };
AutofillService.prototype.doAutoFill = function (login, pageDetails, fromBackground, skipTotp) { AutofillService.prototype.doAutoFill = function (login, pageDetails, fromBackground, skipTotp, skipLastUsed) {
var deferred = Q.defer(); var deferred = Q.defer();
var self = this; var self = this;
@ -168,6 +168,9 @@ function initAutofill() {
} }
didAutofill = true; didAutofill = true;
if (!skipLastUsed) {
self.loginService.updateLastUsedDate(login.id, function () { });
}
chrome.tabs.sendMessage(tab.id, { chrome.tabs.sendMessage(tab.id, {
command: 'fillForm', command: 'fillForm',
@ -207,7 +210,7 @@ function initAutofill() {
return deferred.promise; return deferred.promise;
}; };
AutofillService.prototype.doAutoFillForFirstLogin = function (pageDetails) { AutofillService.prototype.doAutoFillForLastUsedLogin = function (pageDetails) {
var self = this; var self = this;
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) { chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
@ -230,10 +233,11 @@ function initAutofill() {
return; return;
} }
self.doAutoFill(logins[0], pageDetails, true, true); var sortedLogins = logins.sort(self.loginService.sortLoginsByLastUsed);
self.doAutoFill(sortedLogins[0], pageDetails, true, true, true);
}); });
}); });
} };
function loadPasswordFields(pageDetails, canBeHidden) { function loadPasswordFields(pageDetails, canBeHidden) {
var arr = []; var arr = [];

View File

@ -55,15 +55,23 @@ function initLoginService() {
this.userService.getUserId(function (userId) { this.userService.getUserId(function (userId) {
var loginsKey = 'sites_' + userId; var loginsKey = 'sites_' + userId;
var localDataKey = 'sitesLocalData';
chrome.storage.local.get(loginsKey, function (obj) { chrome.storage.local.get(localDataKey, function (localDataObj) {
var logins = obj[loginsKey]; var localData = localDataObj[localDataKey];
if (logins && id in logins) { if (!localData) {
callback(new Login(logins[id])); localData = {};
return;
} }
callback(null); chrome.storage.local.get(loginsKey, function (obj) {
var logins = obj[loginsKey];
if (logins && id in logins) {
callback(new Login(logins[id], false, localData[id]));
return;
}
callback(null);
});
}); });
}); });
}; };
@ -75,19 +83,27 @@ function initLoginService() {
this.userService.getUserId(function (userId) { this.userService.getUserId(function (userId) {
var loginsKey = 'sites_' + userId; var loginsKey = 'sites_' + userId;
var localDataKey = 'sitesLocalData';
chrome.storage.local.get(loginsKey, function (obj) { chrome.storage.local.get(localDataKey, function (localDataObj) {
var logins = obj[loginsKey]; var localData = localDataObj[localDataKey];
var response = []; if (!localData) {
for (var id in logins) { localData = {};
if (!id) {
continue;
}
response.push(new Login(logins[id]));
} }
callback(response); chrome.storage.local.get(loginsKey, function (obj) {
var logins = obj[loginsKey];
var response = [];
for (var id in logins) {
if (!id) {
continue;
}
response.push(new Login(logins[id], false, localData[id]));
}
callback(response);
});
}); });
}); });
}; };
@ -242,6 +258,46 @@ function initLoginService() {
}); });
}; };
LoginService.prototype.updateLastUsedDate = function (id, callback) {
if (!callback || typeof callback !== 'function') {
throw 'callback function required';
}
var self = this;
var localDataKey = 'sitesLocalData';
chrome.storage.local.get(localDataKey, function (obj) {
var loginsLocalData = obj[localDataKey];
if (!loginsLocalData) {
loginsLocalData = {};
}
if (loginsLocalData[id]) {
loginsLocalData[id].lastUsedDate = new Date().getTime();
}
else {
loginsLocalData[id] = {
lastUsedDate: new Date().getTime()
};
}
obj[localDataKey] = loginsLocalData;
chrome.storage.local.set(obj, function () {
if (self.decryptedLoginCache) {
for (var i = 0; i < self.decryptedLoginCache.length; i++) {
if (self.decryptedLoginCache[i].id === id) {
self.decryptedLoginCache[i].localData = loginsLocalData[id];
break;
}
}
}
callback();
});
});
};
LoginService.prototype.replace = function (logins, callback) { LoginService.prototype.replace = function (logins, callback) {
if (!callback || typeof callback !== 'function') { if (!callback || typeof callback !== 'function') {
throw 'callback function required'; throw 'callback function required';
@ -441,6 +497,20 @@ function initLoginService() {
return deferred.promise; return deferred.promise;
}; };
LoginService.prototype.sortLoginsByLastUsed = function (a, b) {
var aLastUsed = a.localData && a.localData.lastUsedDate ? a.localData.lastUsedDate : null;
var bLastUsed = b.localData && b.localData.lastUsedDate ? b.localData.lastUsedDate : null;
if (aLastUsed && (!bLastUsed || aLastUsed < bLastUsed)) {
return 1;
}
if (bLastUsed && (!aLastUsed || aLastUsed > bLastUsed)) {
return -1;
}
return 0;
};
function handleError(error, deferred) { function handleError(error, deferred) {
deferred.reject(error); deferred.reject(error);
} }