promise conversions

This commit is contained in:
Kyle Spearrin 2017-10-17 09:25:22 -04:00
parent 1b66b255f3
commit 0fdee0f13b
5 changed files with 71 additions and 87 deletions

View File

@ -33,7 +33,7 @@ var bg_isBackground = true,
bg_constantsService = new ConstantsService(bg_i18nService); bg_constantsService = new ConstantsService(bg_i18nService);
bg_cryptoService = new CryptoService(bg_constantsService); bg_cryptoService = new CryptoService(bg_constantsService);
bg_tokenService = new TokenService(bg_utilsService); bg_tokenService = new TokenService(bg_utilsService);
bg_appIdService = new AppIdService(); bg_appIdService = new AppIdService(bg_utilsService);
bg_apiService = new ApiService(bg_tokenService, bg_appIdService, bg_utilsService, bg_constantsService, logout); bg_apiService = new ApiService(bg_tokenService, bg_appIdService, bg_utilsService, bg_constantsService, logout);
bg_environmentService = new EnvironmentService(bg_constantsService, bg_apiService); bg_environmentService = new EnvironmentService(bg_constantsService, bg_apiService);
bg_userService = new UserService(bg_tokenService, bg_apiService, bg_cryptoService, bg_utilsService); bg_userService = new UserService(bg_tokenService, bg_apiService, bg_cryptoService, bg_utilsService);

View File

@ -7,12 +7,15 @@
_service.logIn = function (email, masterPassword, twoFactorProvider, twoFactorToken, remember) { _service.logIn = function (email, masterPassword, twoFactorProvider, twoFactorToken, remember) {
email = email.toLowerCase(); email = email.toLowerCase();
var key = cryptoService.makeKey(masterPassword, email); var key = cryptoService.makeKey(masterPassword, email),
var deferred = $q.defer(); deferred = $q.defer(),
deviceRequest = null;
appIdService.getAppId().then(function (appId) {
deviceRequest = new DeviceRequest(appId, utilsService);
return tokenService.getTwoFactorToken(email);
}).then(function (twoFactorRememberedToken) {
cryptoService.hashPassword(masterPassword, key, function (hashedPassword) { cryptoService.hashPassword(masterPassword, key, function (hashedPassword) {
appIdService.getAppId(function (appId) {
tokenService.getTwoFactorToken(email, function (twoFactorRememberedToken) {
var deviceRequest = new DeviceRequest(appId, utilsService);
var request; var request;
if (twoFactorToken && typeof (twoFactorProvider) !== 'undefined' && twoFactorProvider !== null) { if (twoFactorToken && typeof (twoFactorProvider) !== 'undefined' && twoFactorProvider !== null) {
@ -67,7 +70,6 @@
}); });
}); });
}); });
});
return deferred.promise; return deferred.promise;
}; };

View File

@ -44,7 +44,7 @@
return encodeURIComponent(pagePath); return encodeURIComponent(pagePath);
} }
bgPage.bg_appIdService.getAnonymousAppId(function (gaAnonAppId) { bgPage.bg_appIdService.getAnonymousAppId().then(function (gaAnonAppId) {
gaFunc = function (action, param1, param2, param3, param4) { gaFunc = function (action, param1, param2, param3, param4) {
if (action !== 'send' || !param1) { if (action !== 'send' || !param1) {
return; return;

View File

@ -1,35 +1,27 @@
function AppIdService() { function AppIdService(utilsService) {
this.utilsService = utilsService;
initAppIdService(); initAppIdService();
} }
function initAppIdService() { function initAppIdService() {
AppIdService.prototype.getAppId = function (callback) { AppIdService.prototype.getAppId = function () {
if (!callback || typeof callback !== 'function') { return makeAndGetAppId('appId', this);
throw 'callback function required';
}
makeAndGetAppId('appId', callback);
}; };
AppIdService.prototype.getAnonymousAppId = function (callback) { AppIdService.prototype.getAnonymousAppId = function () {
if (!callback || typeof callback !== 'function') { return makeAndGetAppId('anonymousAppId', this);
throw 'callback function required';
}
makeAndGetAppId('anonymousAppId', callback);
}; };
function makeAndGetAppId(key, callback) { function makeAndGetAppId(key, self) {
chrome.storage.local.get(key, function (obj) { return self.utilsService.getObjFromStorage(key).then(function (obj) {
if (obj && obj[key]) { if (obj) {
callback(obj[key]); return obj;
return;
} }
var setObj = {}; var guid = newGuid();
setObj[key] = newGuid(); return self.utilsService.saveObjToStorage(key, guid).then(function () {
chrome.storage.local.set(setObj, function () { return guid;
callback(setObj[key]);
}); });
}); });
} }

View File

@ -1,5 +1,6 @@
function TokenService(utilsService) { function TokenService(utilsService) {
this.utilsService = utilsService; this.utilsService = utilsService;
initTokenService(); initTokenService();
} }
@ -97,20 +98,9 @@ function initTokenService() {
}); });
}; };
TokenService.prototype.getTwoFactorToken = function (email, callback) { TokenService.prototype.getTwoFactorToken = function (email) {
if (!callback || typeof callback !== 'function') { return this.utilsService.getObjFromStorage('twoFactorToken_' + email).then(function (token) {
throw 'callback function required'; return token;
}
var prop = 'twoFactorToken_' + email;
chrome.storage.local.get(prop, function (obj) {
if (obj && obj[prop]) {
callback(obj[prop]);
return;
}
return callback(null);
}); });
}; };