1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-09-18 02:41:15 +02:00

added closure for bg script

This commit is contained in:
Kyle Spearrin 2017-09-09 11:42:12 -04:00
parent 390bd6db1a
commit ff50d935b2

View File

@ -1,34 +1,53 @@
var isBackground = true,
loginToAutoFill = null,
bg_utilsService,
bg_i18nService,
bg_constantsService,
bg_cryptoService,
bg_tokenService,
bg_appIdService,
bg_apiService,
bg_environmentService,
bg_userService,
bg_settingsService,
bg_loginService,
bg_folderService,
bg_lockService,
bg_syncService,
bg_passwordGenerationService,
bg_totpService,
bg_autofillService;
(function () {
var loginToAutoFill = null,
pageDetailsToAutoFill = [],
autofillTimeout = null,
menuOptionsLoaded = [],
pendingAuthRequests = [],
bg_syncTimeout = null;
syncTimeout = null,
bg_loginsToAdd = [];
var bg_loginsToAdd = [];
var bg_utilsService = new UtilsService();
var bg_i18nService = new i18nService(bg_utilsService);
var bg_constantsService = new ConstantsService(bg_i18nService);
var bg_cryptoService = new CryptoService(bg_constantsService);
var bg_tokenService = new TokenService();
var bg_appIdService = new AppIdService();
var bg_apiService = new ApiService(bg_tokenService, bg_appIdService, bg_utilsService, bg_constantsService, logout);
var bg_environmentService = new EnvironmentService(bg_constantsService, bg_apiService);
var bg_userService = new UserService(bg_tokenService, bg_apiService, bg_cryptoService);
var bg_settingsService = new SettingsService(bg_userService);
var bg_loginService = new LoginService(bg_cryptoService, bg_userService, bg_apiService, bg_settingsService);
var bg_folderService = new FolderService(bg_cryptoService, bg_userService, bg_apiService, bg_i18nService);
var bg_lockService = new LockService(bg_constantsService, bg_cryptoService, bg_folderService, bg_loginService, setIcon,
// init services
bg_utilsService = new UtilsService();
bg_i18nService = new i18nService(bg_utilsService);
bg_constantsService = new ConstantsService(bg_i18nService);
bg_cryptoService = new CryptoService(bg_constantsService);
bg_tokenService = new TokenService();
bg_appIdService = new AppIdService();
bg_apiService = new ApiService(bg_tokenService, bg_appIdService, bg_utilsService, bg_constantsService, logout);
bg_environmentService = new EnvironmentService(bg_constantsService, bg_apiService);
bg_userService = new UserService(bg_tokenService, bg_apiService, bg_cryptoService);
bg_settingsService = new SettingsService(bg_userService);
bg_loginService = new LoginService(bg_cryptoService, bg_userService, bg_apiService, bg_settingsService);
bg_folderService = new FolderService(bg_cryptoService, bg_userService, bg_apiService, bg_i18nService);
bg_lockService = new LockService(bg_constantsService, bg_cryptoService, bg_folderService, bg_loginService, setIcon,
refreshBadgeAndMenu);
var bg_syncService = new SyncService(bg_loginService, bg_folderService, bg_userService, bg_apiService, bg_settingsService,
bg_syncService = new SyncService(bg_loginService, bg_folderService, bg_userService, bg_apiService, bg_settingsService,
bg_cryptoService, logout);
var bg_passwordGenerationService = new PasswordGenerationService();
var bg_totpService = new TotpService(bg_constantsService);
var bg_autofillService = new AutofillService(bg_utilsService, bg_totpService, bg_tokenService, bg_loginService);
bg_passwordGenerationService = new PasswordGenerationService();
bg_totpService = new TotpService(bg_constantsService);
bg_autofillService = new AutofillService(bg_utilsService, bg_totpService, bg_tokenService, bg_loginService);
if (chrome.commands) {
if (chrome.commands) {
chrome.commands.onCommand.addListener(function (command) {
if (command === 'generate_password') {
ga('send', {
@ -52,9 +71,9 @@ if (chrome.commands) {
});
}
});
}
}
chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) {
chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) {
if (msg.command === 'loggedIn' || msg.command === 'unlocked' || msg.command === 'locked') {
setIcon();
refreshBadgeAndMenu();
@ -113,9 +132,9 @@ chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) {
} else if (msg.command === 'bgUpdateContextMenu') {
refreshBadgeAndMenu();
}
});
});
if (chrome.runtime.onInstalled) {
if (chrome.runtime.onInstalled) {
chrome.runtime.onInstalled.addListener(function (details) {
ga('send', {
hitType: 'event',
@ -126,41 +145,41 @@ if (chrome.runtime.onInstalled) {
chrome.tabs.create({ url: 'https://bitwarden.com/browser-start/' }, function (tab) { });
}
});
}
}
chrome.tabs.onActivated.addListener(function (activeInfo) {
chrome.tabs.onActivated.addListener(function (activeInfo) {
refreshBadgeAndMenu();
});
});
var onReplacedRan = false;
chrome.tabs.onReplaced.addListener(function (addedTabId, removedTabId) {
var onReplacedRan = false;
chrome.tabs.onReplaced.addListener(function (addedTabId, removedTabId) {
if (onReplacedRan) {
return;
}
onReplacedRan = true;
checkbg_loginsToAdd();
refreshBadgeAndMenu();
});
});
var onUpdatedRan = false;
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
var onUpdatedRan = false;
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
if (onUpdatedRan) {
return;
}
onUpdatedRan = true;
checkbg_loginsToAdd();
refreshBadgeAndMenu();
});
});
chrome.windows.onFocusChanged.addListener(function (windowId) {
chrome.windows.onFocusChanged.addListener(function (windowId) {
if (windowId === null || windowId < 0) {
return;
}
refreshBadgeAndMenu();
});
});
chrome.contextMenus.onClicked.addListener(function (info, tab) {
chrome.contextMenus.onClicked.addListener(function (info, tab) {
if (info.menuItemId === 'generate-password') {
ga('send', {
hitType: 'event',
@ -209,9 +228,9 @@ chrome.contextMenus.onClicked.addListener(function (info, tab) {
});
}
});
});
chrome.webRequest.onAuthRequired.addListener(function (details, callback) {
chrome.webRequest.onAuthRequired.addListener(function (details, callback) {
if (!details.url || pendingAuthRequests.indexOf(details.requestId) != -1) {
if (callback) {
callback();
@ -265,20 +284,20 @@ chrome.webRequest.onAuthRequired.addListener(function (details, callback) {
callback();
});
}
}, { urls: ['http://*/*', 'https://*/*'] }, [bg_utilsService.isFirefox() ? 'blocking' : 'asyncBlocking']);
}, { urls: ['http://*/*', 'https://*/*'] }, [bg_utilsService.isFirefox() ? 'blocking' : 'asyncBlocking']);
chrome.webRequest.onCompleted.addListener(completeAuthRequest, { urls: ['http://*/*'] });
chrome.webRequest.onErrorOccurred.addListener(completeAuthRequest, { urls: ['http://*/*'] });
chrome.webRequest.onCompleted.addListener(completeAuthRequest, { urls: ['http://*/*'] });
chrome.webRequest.onErrorOccurred.addListener(completeAuthRequest, { urls: ['http://*/*'] });
function completeAuthRequest(details) {
function completeAuthRequest(details) {
var i = pendingAuthRequests.indexOf(details.requestId);
if (i > -1) {
pendingAuthRequests.splice(i, 1);
}
}
}
var buildingContextMenu = false;
function buildContextMenu(callback) {
var buildingContextMenu = false;
function buildContextMenu(callback) {
if (buildingContextMenu) {
return;
}
@ -343,9 +362,9 @@ function buildContextMenu(callback) {
});
});
});
}
}
function setIcon() {
function setIcon() {
bg_userService.isAuthenticated(function (isAuthenticated) {
bg_cryptoService.getKey().then(function (key) {
var suffix = '';
@ -364,9 +383,9 @@ function setIcon() {
});
});
});
}
}
function refreshBadgeAndMenu() {
function refreshBadgeAndMenu() {
chrome.tabs.query({ active: true, windowId: chrome.windows.WINDOW_ID_CURRENT }, function (tabs) {
var tab = null;
if (tabs.length > 0) {
@ -389,14 +408,14 @@ function refreshBadgeAndMenu() {
}
});
});
}
}
function contextMenuReady(tab, contextMenuEnabled) {
function contextMenuReady(tab, contextMenuEnabled) {
loadMenuAndUpdateBadge(tab.url, tab.id, contextMenuEnabled);
onUpdatedRan = onReplacedRan = false;
}
}
function loadMenuAndUpdateBadge(url, tabId, contextMenuEnabled) {
function loadMenuAndUpdateBadge(url, tabId, contextMenuEnabled) {
if (!url) {
return;
}
@ -448,9 +467,9 @@ function loadMenuAndUpdateBadge(url, tabId, contextMenuEnabled) {
tabId: tabId
});
});
}
}
function messageCurrentTab(command, data) {
function messageCurrentTab(command, data) {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
var tabId = null;
if (tabs.length > 0) {
@ -462,9 +481,9 @@ function messageCurrentTab(command, data) {
messageTab(tabId, command, data);
});
}
}
function messageTab(tabId, command, data, callback) {
function messageTab(tabId, command, data, callback) {
if (!tabId) {
return;
}
@ -482,9 +501,9 @@ function messageTab(tabId, command, data, callback) {
callback();
}
});
}
}
function collectPageDetailsForContentScript(tab, sender) {
function collectPageDetailsForContentScript(tab, sender) {
if (!tab || !tab.id) {
return;
}
@ -494,9 +513,9 @@ function collectPageDetailsForContentScript(tab, sender) {
return;
}
});
}
}
function addLogin(login, tab) {
function addLogin(login, tab) {
var loginDomain = bg_utilsService.getDomain(login.url);
if (!loginDomain) {
return;
@ -527,9 +546,9 @@ function addLogin(login, tab) {
checkbg_loginsToAdd(tab);
}
});
}
}
function cleanupbg_loginsToAdd() {
function cleanupbg_loginsToAdd() {
for (var i = bg_loginsToAdd.length - 1; i >= 0; i--) {
if (bg_loginsToAdd[i].expires < new Date()) {
bg_loginsToAdd.splice(i, 1);
@ -537,17 +556,17 @@ function cleanupbg_loginsToAdd() {
}
setTimeout(cleanupbg_loginsToAdd, 2 * 60 * 1000); // check every 2 minutes
}
}
function removeAddLogin(tab) {
function removeAddLogin(tab) {
for (var i = bg_loginsToAdd.length - 1; i >= 0; i--) {
if (bg_loginsToAdd[i].tabId === tab.id) {
bg_loginsToAdd.splice(i, 1);
}
}
}
}
function saveAddLogin(tab) {
function saveAddLogin(tab) {
for (var i = bg_loginsToAdd.length - 1; i >= 0; i--) {
if (bg_loginsToAdd[i].tabId === tab.id) {
var loginToAdd = bg_loginsToAdd[i];
@ -577,9 +596,9 @@ function saveAddLogin(tab) {
}
}
}
}
}
function saveNever(tab) {
function saveNever(tab) {
for (var i = bg_loginsToAdd.length - 1; i >= 0; i--) {
if (bg_loginsToAdd[i].tabId === tab.id) {
var loginToAdd = bg_loginsToAdd[i];
@ -593,9 +612,9 @@ function saveNever(tab) {
}
}
}
}
}
function checkbg_loginsToAdd(tab, callback) {
function checkbg_loginsToAdd(tab, callback) {
if (!bg_loginsToAdd.length) {
if (callback) {
callback();
@ -644,9 +663,9 @@ function checkbg_loginsToAdd(tab, callback) {
}
}
}
}
}
function startAutofillPage(login) {
function startAutofillPage(login) {
loginToAutoFill = login;
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
var tab = null;
@ -664,25 +683,25 @@ function startAutofillPage(login) {
chrome.tabs.sendMessage(tab.id, { command: 'collectPageDetails', tab: tab }, function () {
});
});
}
}
function autofillPage() {
function autofillPage() {
bg_autofillService.doAutoFill(loginToAutoFill, pageDetailsToAutoFill, true);
// reset
loginToAutoFill = null;
pageDetailsToAutoFill = [];
}
}
function loadLoginContextMenuOptions(login) {
function loadLoginContextMenuOptions(login) {
var title = login.name + (login.username && login.username !== '' ? ' (' + login.username + ')' : '');
loadContextMenuOptions(title, login.id, login);
}
}
function loadNoLoginsContextMenuOptions(noLoginsMessage) {
function loadNoLoginsContextMenuOptions(noLoginsMessage) {
loadContextMenuOptions(noLoginsMessage, 'noop', null);
}
}
function loadContextMenuOptions(title, idSuffix, login) {
function loadContextMenuOptions(title, idSuffix, login) {
if (menuOptionsLoaded.indexOf(idSuffix) > -1) {
return;
}
@ -695,6 +714,10 @@ function loadContextMenuOptions(title, idSuffix, login) {
parentId: 'autofill',
contexts: ['all'],
title: title
}, function () {
if (chrome.runtime.lastError) {
return;
}
});
}
@ -710,6 +733,10 @@ function loadContextMenuOptions(title, idSuffix, login) {
parentId: 'copy-username',
contexts: ['all'],
title: title
}, function () {
if (chrome.runtime.lastError) {
return;
}
});
}
@ -720,12 +747,16 @@ function loadContextMenuOptions(title, idSuffix, login) {
parentId: 'copy-password',
contexts: ['all'],
title: title
}, function () {
if (chrome.runtime.lastError) {
return;
}
});
}
}
}
// TODO: Fix callback hell by moving to promises
function logout(expired, callback) {
// TODO: Fix callback hell by moving to promises
function logout(expired, callback) {
bg_userService.getUserId(function (userId) {
bg_syncService.setLastSync(new Date(0), function () {
bg_settingsService.clear(function () {
@ -748,9 +779,9 @@ function logout(expired, callback) {
});
});
});
}
}
function fullSync(override) {
function fullSync(override) {
override = override || false;
log('check fullSync - ' + override);
bg_syncService.getLastSync(function (lastSync) {
@ -770,29 +801,30 @@ function fullSync(override) {
scheduleNextSync();
}
});
}
}
function scheduleNextSync() {
if (bg_syncTimeout) {
log('clearing bg_syncTimeout');
clearTimeout(bg_syncTimeout);
function scheduleNextSync() {
if (syncTimeout) {
log('clearing syncTimeout');
clearTimeout(syncTimeout);
}
else {
log('don\'t need to clear bg_syncTimeout');
log('don\'t need to clear syncTimeout');
}
log('scheduleNextSync');
bg_syncTimeout = setTimeout(fullSync, 5 * 60 * 1000); // check every 5 minutes
}
syncTimeout = setTimeout(fullSync, 5 * 60 * 1000); // check every 5 minutes
}
function log(msg) {
function log(msg) {
console.log(new Date() + ' - Background: ' + msg);
}
}
// Bootstrap
// Bootstrap
bg_environmentService.setUrlsFromStorage(function () {
bg_environmentService.setUrlsFromStorage(function () {
setIcon();
cleanupbg_loginsToAdd();
fullSync(true);
});
});
})();