From 7388cd174ef4b49513f2c61a9d3f037036b9b1eb Mon Sep 17 00:00:00 2001 From: Daniel James Smith Date: Fri, 15 Oct 2021 15:09:13 +0200 Subject: [PATCH] Move notificationQueue from main into notification.background --- src/background/main.background.ts | 63 +---------------------- src/background/notification.background.ts | 45 ++++++++-------- src/background/tabs.background.ts | 7 +-- 3 files changed, 29 insertions(+), 86 deletions(-) diff --git a/src/background/main.background.ts b/src/background/main.background.ts index d7f469a890..e2ded7cbc0 100644 --- a/src/background/main.background.ts +++ b/src/background/main.background.ts @@ -127,7 +127,6 @@ export default class MainBackground { onUpdatedRan: boolean; onReplacedRan: boolean; loginToAutoFill: any = null; - notificationQueue: any[] = []; private commandsBackground: CommandsBackground; private contextMenusBackground: ContextMenusBackground; @@ -250,7 +249,7 @@ export default class MainBackground { this.notificationBackground = new NotificationBackground(this, this.autofillService, this.cipherService, this.storageService, this.vaultTimeoutService, this.policyService, this.folderService); - this.tabsBackground = new TabsBackground(this); + this.tabsBackground = new TabsBackground(this, this.notificationBackground); this.contextMenusBackground = new ContextMenusBackground(this, this.cipherService, this.passwordGenerationService, this.platformUtilsService, this.vaultTimeoutService, this.eventService, this.totpService); this.idleBackground = new IdleBackground(this.vaultTimeoutService, this.storageService, @@ -292,7 +291,6 @@ export default class MainBackground { setTimeout(async () => { await this.environmentService.setUrlsFromStorage(); await this.setIcon(); - this.cleanupNotificationQueue(); this.fullSync(true); setTimeout(() => this.notificationsService.init(), 2500); resolve(); @@ -390,22 +388,6 @@ export default class MainBackground { }, options); } - async checkNotificationQueue(tab: any = null): Promise { - if (this.notificationQueue.length === 0) { - return; - } - - if (tab != null) { - this.doNotificationQueueCheck(tab); - return; - } - - const currentTab = await BrowserApi.getTabFromCurrentWindow(); - if (currentTab != null) { - this.doNotificationQueueCheck(currentTab); - } - } - async openPopup() { // Chrome APIs cannot open popup @@ -657,49 +639,6 @@ export default class MainBackground { return title.replace(/&/g, '&&'); } - private cleanupNotificationQueue() { - for (let i = this.notificationQueue.length - 1; i >= 0; i--) { - if (this.notificationQueue[i].expires < new Date()) { - this.notificationQueue.splice(i, 1); - } - } - setTimeout(() => this.cleanupNotificationQueue(), 2 * 60 * 1000); // check every 2 minutes - } - - private doNotificationQueueCheck(tab: any) { - if (tab == null) { - return; - } - - const tabDomain = Utils.getDomain(tab.url); - if (tabDomain == null) { - return; - } - - for (let i = 0; i < this.notificationQueue.length; i++) { - if (this.notificationQueue[i].tabId !== tab.id || this.notificationQueue[i].domain !== tabDomain) { - continue; - } - - if (this.notificationQueue[i].type === 'addLogin') { - BrowserApi.tabSendMessageData(tab, 'openNotificationBar', { - type: 'add', - typeData: { - isVaultLocked: this.notificationQueue[i].wasVaultLocked, - }, - }); - } else if (this.notificationQueue[i].type === 'changePassword') { - BrowserApi.tabSendMessageData(tab, 'openNotificationBar', { - type: 'change', - typeData: { - isVaultLocked: this.notificationQueue[i].wasVaultLocked, - }, - }); - } - break; - } - } - private async fullSync(override: boolean = false) { const syncInternal = 6 * 60 * 60 * 1000; // 6 hours const lastSync = await this.syncService.getLastSync(); diff --git a/src/background/notification.background.ts b/src/background/notification.background.ts index 7f1377e008..12ded645c6 100644 --- a/src/background/notification.background.ts +++ b/src/background/notification.background.ts @@ -24,6 +24,9 @@ import AddChangePasswordQueueMessage from './models/addChangePasswordQueueMessag import AddLoginQueueMessage from './models/addLoginQueueMessage'; export default class NotificationBackground { + + private notificationQueue: any[] = []; + constructor(private main: MainBackground, private autofillService: AutofillService, private cipherService: CipherService, private storageService: StorageService, private vaultTimeoutService: VaultTimeoutService, private policyService: PolicyService, @@ -43,7 +46,7 @@ export default class NotificationBackground { } async checkNotificationQueue(tab: any = null): Promise { - if (this.main.notificationQueue.length === 0) { + if (this.notificationQueue.length === 0) { return; } @@ -59,9 +62,9 @@ export default class NotificationBackground { } private cleanupNotificationQueue() { - for (let i = this.main.notificationQueue.length - 1; i >= 0; i--) { - if (this.main.notificationQueue[i].expires < new Date()) { - this.main.notificationQueue.splice(i, 1); + for (let i = this.notificationQueue.length - 1; i >= 0; i--) { + if (this.notificationQueue[i].expires < new Date()) { + this.notificationQueue.splice(i, 1); } } setTimeout(() => this.cleanupNotificationQueue(), 2 * 60 * 1000); // check every 2 minutes @@ -77,23 +80,23 @@ export default class NotificationBackground { return; } - for (let i = 0; i < this.main.notificationQueue.length; i++) { - if (this.main.notificationQueue[i].tabId !== tab.id || this.main.notificationQueue[i].domain !== tabDomain) { + for (let i = 0; i < this.notificationQueue.length; i++) { + if (this.notificationQueue[i].tabId !== tab.id || this.notificationQueue[i].domain !== tabDomain) { continue; } - if (this.main.notificationQueue[i].type === 'addLogin') { + if (this.notificationQueue[i].type === 'addLogin') { BrowserApi.tabSendMessageData(tab, 'openNotificationBar', { type: 'add', typeData: { - isVaultLocked: this.main.notificationQueue[i].wasVaultLocked, + isVaultLocked: this.notificationQueue[i].wasVaultLocked, }, }); - } else if (this.main.notificationQueue[i].type === 'changePassword') { + } else if (this.notificationQueue[i].type === 'changePassword') { BrowserApi.tabSendMessageData(tab, 'openNotificationBar', { type: 'change', typeData: { - isVaultLocked: this.main.notificationQueue[i].wasVaultLocked, + isVaultLocked: this.notificationQueue[i].wasVaultLocked, }, }); } @@ -102,9 +105,9 @@ export default class NotificationBackground { } private removeTabFromNotificationQueue(tab: any) { - for (let i = this.main.notificationQueue.length - 1; i >= 0; i--) { - if (this.main.notificationQueue[i].tabId === tab.id) { - this.main.notificationQueue.splice(i, 1); + for (let i = this.notificationQueue.length - 1; i >= 0; i--) { + if (this.notificationQueue[i].tabId === tab.id) { + this.notificationQueue.splice(i, 1); } } } @@ -211,7 +214,7 @@ export default class NotificationBackground { expires: new Date((new Date()).getTime() + 5 * 60000), // 5 minutes wasVaultLocked: isVaultLocked, }; - this.main.notificationQueue.push(message); + this.notificationQueue.push(message); await this.checkNotificationQueue(tab); } @@ -253,13 +256,13 @@ export default class NotificationBackground { expires: new Date((new Date()).getTime() + 5 * 60000), // 5 minutes wasVaultLocked: isVaultLocked, }; - this.main.notificationQueue.push(message); + this.notificationQueue.push(message); await this.checkNotificationQueue(tab); } private async saveOrUpdateCredentials(tab: any, folderId?: string) { - for (let i = this.main.notificationQueue.length - 1; i >= 0; i--) { - const queueMessage = this.main.notificationQueue[i]; + for (let i = this.notificationQueue.length - 1; i >= 0; i--) { + const queueMessage = this.notificationQueue[i]; if (queueMessage.tabId !== tab.id || (queueMessage.type !== 'addLogin' && queueMessage.type !== 'changePassword')) { continue; @@ -270,7 +273,7 @@ export default class NotificationBackground { continue; } - this.main.notificationQueue.splice(i, 1); + this.notificationQueue.splice(i, 1); BrowserApi.tabSendMessageData(tab, 'closeNotificationBar'); if (queueMessage.type === 'changePassword') { @@ -345,8 +348,8 @@ export default class NotificationBackground { } private async saveNever(tab: any) { - for (let i = this.main.notificationQueue.length - 1; i >= 0; i--) { - const queueMessage = this.main.notificationQueue[i]; + for (let i = this.notificationQueue.length - 1; i >= 0; i--) { + const queueMessage = this.notificationQueue[i]; if (queueMessage.tabId !== tab.id || queueMessage.type !== 'addLogin') { continue; } @@ -356,7 +359,7 @@ export default class NotificationBackground { continue; } - this.main.notificationQueue.splice(i, 1); + this.notificationQueue.splice(i, 1); BrowserApi.tabSendMessageData(tab, 'closeNotificationBar'); const hostname = Utils.getHostname(tab.url); diff --git a/src/background/tabs.background.ts b/src/background/tabs.background.ts index 8f4d4e12b1..7ba7a75a3d 100644 --- a/src/background/tabs.background.ts +++ b/src/background/tabs.background.ts @@ -1,9 +1,10 @@ import MainBackground from './main.background'; +import NotificationBackground from './notification.background'; export default class TabsBackground { private tabs: any; - constructor(private main: MainBackground) { + constructor(private main: MainBackground, private notificationBackground: NotificationBackground) { this.tabs = chrome.tabs; } @@ -23,7 +24,7 @@ export default class TabsBackground { return; } this.main.onReplacedRan = true; - await this.main.checkNotificationQueue(); + await this.notificationBackground.checkNotificationQueue(); await this.main.refreshBadgeAndMenu(); this.main.messagingService.send('tabReplaced'); this.main.messagingService.send('tabChanged'); @@ -34,7 +35,7 @@ export default class TabsBackground { return; } this.main.onUpdatedRan = true; - await this.main.checkNotificationQueue(); + await this.notificationBackground.checkNotificationQueue(); await this.main.refreshBadgeAndMenu(); this.main.messagingService.send('tabUpdated'); this.main.messagingService.send('tabChanged');