1
0
mirror of https://github.com/bitwarden/browser.git synced 2025-01-03 18:28:13 +01:00

Move notificationQueue from main into notification.background

This commit is contained in:
Daniel James Smith 2021-10-15 15:09:13 +02:00
parent 0eb7fe1407
commit 7388cd174e
No known key found for this signature in database
GPG Key ID: 03E4BD365FF06726
3 changed files with 29 additions and 86 deletions

View File

@ -127,7 +127,6 @@ export default class MainBackground {
onUpdatedRan: boolean; onUpdatedRan: boolean;
onReplacedRan: boolean; onReplacedRan: boolean;
loginToAutoFill: any = null; loginToAutoFill: any = null;
notificationQueue: any[] = [];
private commandsBackground: CommandsBackground; private commandsBackground: CommandsBackground;
private contextMenusBackground: ContextMenusBackground; private contextMenusBackground: ContextMenusBackground;
@ -250,7 +249,7 @@ export default class MainBackground {
this.notificationBackground = new NotificationBackground(this, this.autofillService, this.cipherService, this.notificationBackground = new NotificationBackground(this, this.autofillService, this.cipherService,
this.storageService, this.vaultTimeoutService, this.policyService, this.folderService); 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.contextMenusBackground = new ContextMenusBackground(this, this.cipherService, this.passwordGenerationService,
this.platformUtilsService, this.vaultTimeoutService, this.eventService, this.totpService); this.platformUtilsService, this.vaultTimeoutService, this.eventService, this.totpService);
this.idleBackground = new IdleBackground(this.vaultTimeoutService, this.storageService, this.idleBackground = new IdleBackground(this.vaultTimeoutService, this.storageService,
@ -292,7 +291,6 @@ export default class MainBackground {
setTimeout(async () => { setTimeout(async () => {
await this.environmentService.setUrlsFromStorage(); await this.environmentService.setUrlsFromStorage();
await this.setIcon(); await this.setIcon();
this.cleanupNotificationQueue();
this.fullSync(true); this.fullSync(true);
setTimeout(() => this.notificationsService.init(), 2500); setTimeout(() => this.notificationsService.init(), 2500);
resolve(); resolve();
@ -390,22 +388,6 @@ export default class MainBackground {
}, options); }, options);
} }
async checkNotificationQueue(tab: any = null): Promise<any> {
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() { async openPopup() {
// Chrome APIs cannot open popup // Chrome APIs cannot open popup
@ -657,49 +639,6 @@ export default class MainBackground {
return title.replace(/&/g, '&&'); 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) { private async fullSync(override: boolean = false) {
const syncInternal = 6 * 60 * 60 * 1000; // 6 hours const syncInternal = 6 * 60 * 60 * 1000; // 6 hours
const lastSync = await this.syncService.getLastSync(); const lastSync = await this.syncService.getLastSync();

View File

@ -24,6 +24,9 @@ import AddChangePasswordQueueMessage from './models/addChangePasswordQueueMessag
import AddLoginQueueMessage from './models/addLoginQueueMessage'; import AddLoginQueueMessage from './models/addLoginQueueMessage';
export default class NotificationBackground { export default class NotificationBackground {
private notificationQueue: any[] = [];
constructor(private main: MainBackground, private autofillService: AutofillService, constructor(private main: MainBackground, private autofillService: AutofillService,
private cipherService: CipherService, private storageService: StorageService, private cipherService: CipherService, private storageService: StorageService,
private vaultTimeoutService: VaultTimeoutService, private policyService: PolicyService, private vaultTimeoutService: VaultTimeoutService, private policyService: PolicyService,
@ -43,7 +46,7 @@ export default class NotificationBackground {
} }
async checkNotificationQueue(tab: any = null): Promise<any> { async checkNotificationQueue(tab: any = null): Promise<any> {
if (this.main.notificationQueue.length === 0) { if (this.notificationQueue.length === 0) {
return; return;
} }
@ -59,9 +62,9 @@ export default class NotificationBackground {
} }
private cleanupNotificationQueue() { private cleanupNotificationQueue() {
for (let i = this.main.notificationQueue.length - 1; i >= 0; i--) { for (let i = this.notificationQueue.length - 1; i >= 0; i--) {
if (this.main.notificationQueue[i].expires < new Date()) { if (this.notificationQueue[i].expires < new Date()) {
this.main.notificationQueue.splice(i, 1); this.notificationQueue.splice(i, 1);
} }
} }
setTimeout(() => this.cleanupNotificationQueue(), 2 * 60 * 1000); // check every 2 minutes setTimeout(() => this.cleanupNotificationQueue(), 2 * 60 * 1000); // check every 2 minutes
@ -77,23 +80,23 @@ export default class NotificationBackground {
return; return;
} }
for (let i = 0; i < this.main.notificationQueue.length; i++) { for (let i = 0; i < this.notificationQueue.length; i++) {
if (this.main.notificationQueue[i].tabId !== tab.id || this.main.notificationQueue[i].domain !== tabDomain) { if (this.notificationQueue[i].tabId !== tab.id || this.notificationQueue[i].domain !== tabDomain) {
continue; continue;
} }
if (this.main.notificationQueue[i].type === 'addLogin') { if (this.notificationQueue[i].type === 'addLogin') {
BrowserApi.tabSendMessageData(tab, 'openNotificationBar', { BrowserApi.tabSendMessageData(tab, 'openNotificationBar', {
type: 'add', type: 'add',
typeData: { 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', { BrowserApi.tabSendMessageData(tab, 'openNotificationBar', {
type: 'change', type: 'change',
typeData: { typeData: {
isVaultLocked: this.main.notificationQueue[i].wasVaultLocked, isVaultLocked: this.notificationQueue[i].wasVaultLocked,
}, },
}); });
} }
@ -102,9 +105,9 @@ export default class NotificationBackground {
} }
private removeTabFromNotificationQueue(tab: any) { private removeTabFromNotificationQueue(tab: any) {
for (let i = this.main.notificationQueue.length - 1; i >= 0; i--) { for (let i = this.notificationQueue.length - 1; i >= 0; i--) {
if (this.main.notificationQueue[i].tabId === tab.id) { if (this.notificationQueue[i].tabId === tab.id) {
this.main.notificationQueue.splice(i, 1); this.notificationQueue.splice(i, 1);
} }
} }
} }
@ -211,7 +214,7 @@ export default class NotificationBackground {
expires: new Date((new Date()).getTime() + 5 * 60000), // 5 minutes expires: new Date((new Date()).getTime() + 5 * 60000), // 5 minutes
wasVaultLocked: isVaultLocked, wasVaultLocked: isVaultLocked,
}; };
this.main.notificationQueue.push(message); this.notificationQueue.push(message);
await this.checkNotificationQueue(tab); await this.checkNotificationQueue(tab);
} }
@ -253,13 +256,13 @@ export default class NotificationBackground {
expires: new Date((new Date()).getTime() + 5 * 60000), // 5 minutes expires: new Date((new Date()).getTime() + 5 * 60000), // 5 minutes
wasVaultLocked: isVaultLocked, wasVaultLocked: isVaultLocked,
}; };
this.main.notificationQueue.push(message); this.notificationQueue.push(message);
await this.checkNotificationQueue(tab); await this.checkNotificationQueue(tab);
} }
private async saveOrUpdateCredentials(tab: any, folderId?: string) { private async saveOrUpdateCredentials(tab: any, folderId?: string) {
for (let i = this.main.notificationQueue.length - 1; i >= 0; i--) { for (let i = this.notificationQueue.length - 1; i >= 0; i--) {
const queueMessage = this.main.notificationQueue[i]; const queueMessage = this.notificationQueue[i];
if (queueMessage.tabId !== tab.id || if (queueMessage.tabId !== tab.id ||
(queueMessage.type !== 'addLogin' && queueMessage.type !== 'changePassword')) { (queueMessage.type !== 'addLogin' && queueMessage.type !== 'changePassword')) {
continue; continue;
@ -270,7 +273,7 @@ export default class NotificationBackground {
continue; continue;
} }
this.main.notificationQueue.splice(i, 1); this.notificationQueue.splice(i, 1);
BrowserApi.tabSendMessageData(tab, 'closeNotificationBar'); BrowserApi.tabSendMessageData(tab, 'closeNotificationBar');
if (queueMessage.type === 'changePassword') { if (queueMessage.type === 'changePassword') {
@ -345,8 +348,8 @@ export default class NotificationBackground {
} }
private async saveNever(tab: any) { private async saveNever(tab: any) {
for (let i = this.main.notificationQueue.length - 1; i >= 0; i--) { for (let i = this.notificationQueue.length - 1; i >= 0; i--) {
const queueMessage = this.main.notificationQueue[i]; const queueMessage = this.notificationQueue[i];
if (queueMessage.tabId !== tab.id || queueMessage.type !== 'addLogin') { if (queueMessage.tabId !== tab.id || queueMessage.type !== 'addLogin') {
continue; continue;
} }
@ -356,7 +359,7 @@ export default class NotificationBackground {
continue; continue;
} }
this.main.notificationQueue.splice(i, 1); this.notificationQueue.splice(i, 1);
BrowserApi.tabSendMessageData(tab, 'closeNotificationBar'); BrowserApi.tabSendMessageData(tab, 'closeNotificationBar');
const hostname = Utils.getHostname(tab.url); const hostname = Utils.getHostname(tab.url);

View File

@ -1,9 +1,10 @@
import MainBackground from './main.background'; import MainBackground from './main.background';
import NotificationBackground from './notification.background';
export default class TabsBackground { export default class TabsBackground {
private tabs: any; private tabs: any;
constructor(private main: MainBackground) { constructor(private main: MainBackground, private notificationBackground: NotificationBackground) {
this.tabs = chrome.tabs; this.tabs = chrome.tabs;
} }
@ -23,7 +24,7 @@ export default class TabsBackground {
return; return;
} }
this.main.onReplacedRan = true; this.main.onReplacedRan = true;
await this.main.checkNotificationQueue(); await this.notificationBackground.checkNotificationQueue();
await this.main.refreshBadgeAndMenu(); await this.main.refreshBadgeAndMenu();
this.main.messagingService.send('tabReplaced'); this.main.messagingService.send('tabReplaced');
this.main.messagingService.send('tabChanged'); this.main.messagingService.send('tabChanged');
@ -34,7 +35,7 @@ export default class TabsBackground {
return; return;
} }
this.main.onUpdatedRan = true; this.main.onUpdatedRan = true;
await this.main.checkNotificationQueue(); await this.notificationBackground.checkNotificationQueue();
await this.main.refreshBadgeAndMenu(); await this.main.refreshBadgeAndMenu();
this.main.messagingService.send('tabUpdated'); this.main.messagingService.send('tabUpdated');
this.main.messagingService.send('tabChanged'); this.main.messagingService.send('tabChanged');