1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-15 07:43:35 +00:00

more services and async callthroughs for vault list/add

This commit is contained in:
Kyle Spearrin
2016-09-04 00:34:24 -04:00
parent 32f4ab4987
commit 8f0a24b1b9
14 changed files with 303 additions and 166 deletions

View File

@@ -2,130 +2,115 @@
this.cryptoService = cryptoService;
this.userService = userService;
this.apiService = apiService;
initSiteService();
};
!function () {
var ciphersKey = 'ciphers_' + this.userService.userId;
function initSiteService() {
this.userService.getUserId(function (userId) {
var sitesKey = 'sites_' + userId;
SiteService.prototype.get = function (id, callback) {
if (!callback || typeof callback !== 'function') {
throw 'callback function required';
}
SiteService.prototype.get = function (id, callback) {
if (!callback || typeof callback !== 'function') {
throw 'callback function required';
}
chrome.storage.local.get(sitesKey, function (obj) {
var sites = obj[sitesKey];
if (id in sites) {
callback(new Site(sites[id]));
return;
}
chrome.storage.local.get(ciphersKey, function (obj) {
if (!obj) {
callback(null);
});
};
SiteService.prototype.getAll = function (callback) {
if (!callback || typeof callback !== 'function') {
throw 'callback function required';
}
var sites = obj[ciphersKey];
if (id in sites) {
callback(new Site(sites[id]));
return;
chrome.storage.local.get(sitesKey, function (obj) {
var sites = obj[sitesKey];
var response = [];
for (var id in sites) {
response.push(new Site(sites[id]));
}
callback(response);
});
};
SiteService.prototype.save = function (site, callback) {
if (!callback || typeof callback !== 'function') {
throw 'callback function required';
}
callback(null);
});
};
var newRecord = site.id === null,
self = this;
SiteService.prototype.getAll = function (callback) {
if (!callback || typeof callback !== 'function') {
throw 'callback function required';
}
chrome.storage.local.get(ciphersKey, function (obj) {
if (!obj) {
callback([]);
var request = new SiteRequest(site);
if (newRecord) {
self.apiService.postSite(request, apiSuccess, handleError);
}
else {
self.apiService.putSite(site.id, request, apiSuccess, handleError);
}
var sites = obj[ciphersKey];
var response = [];
for (var id in sites) {
response.push(new Site(sites[id]));
}
function apiSuccess(response) {
userService.getUserId(function (userId) {
var data = new SiteData(response, userId);
callback(response);
});
};
chrome.storage.local.get(sitesKey, function (obj) {
var sites = obj[sitesKey];
if (!newRecord && site.id in sites) {
sites[site.id] = data;
}
else {
sites.push(data);
site.id = data.id;
}
SiteService.prototype.save = function (site, callback) {
if (!callback || typeof callback !== 'function') {
throw 'callback function required';
}
var newRecord = site.id === null,
self = this;
var request = new SiteRequest(site);
if (newRecord) {
self.apiService.postSite(request, apiSuccess, handleError);
}
else {
self.apiService.putSite(site.id, request, apiSuccess, handleError);
}
function apiSuccess(response) {
userService.getUserId(function (userId) {
var data = new SiteData(response, userId);
chrome.storage.local.get(ciphersKey, function (obj) {
if (!obj) {
obj = {};
obj[ciphersKey] = [];
}
var sites = obj[ciphersKey];
if (!newRecord && site.id in sites) {
sites[site.id] = data;
}
else {
sites.push(data);
site.id = data.id;
}
obj[ciphersKey] = sites;
chrome.storage.local.set(obj, function () {
callback(site);
obj[sitesKey] = sites;
chrome.storage.local.set(obj, function () {
callback(site);
});
});
});
});
}
};
}
};
SiteService.prototype.delete = function (id, callback) {
if (!callback || typeof callback !== 'function') {
throw 'callback function required';
}
SiteService.prototype.delete = function (id, callback) {
if (!callback || typeof callback !== 'function') {
throw 'callback function required';
}
self.apiService.deleteCipher(id, apiSuccess, handleError);
self.apiService.deleteCipher(id, apiSuccess, handleError);
function apiSuccess(response) {
userService.getUserId(function (userId) {
chrome.storage.local.get(ciphersKey, function (obj) {
if (!obj) {
obj = {};
obj[ciphersKey] = [];
}
function apiSuccess(response) {
userService.getUserId(function (userId) {
chrome.storage.local.get(sitesKey, function (obj) {
var sites = obj[sitesKey];
if (id in sites) {
var index = sites.indexOf(sites[id]);
sites.splice(index, 1);
var sites = obj[ciphersKey];
if (id in sites) {
var index = sites.indexOf(sites[id]);
sites.splice(index, 1);
obj[ciphersKey] = sites;
chrome.storage.local.set(obj, function () {
obj[sitesKey] = sites;
chrome.storage.local.set(obj, function () {
callback();
});
}
else {
callback();
});
}
else {
callback();
}
}
});
});
});
}
};
function handleError() {
// TODO: check for unauth or forbidden and logout
}
};
function handleError() {
// TODO: check for unauth or forbidden and logout
}
}();
});
};