1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-09-27 04:03:00 +02: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;
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<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() {
// 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();

View File

@ -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<any> {
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);

View File

@ -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');