1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-17 16:53:34 +00:00

Declare and use types for notificationQueue

This commit is contained in:
Daniel James Smith
2021-10-15 19:47:48 +02:00
parent d1977f1f08
commit 92459c6098
5 changed files with 30 additions and 22 deletions

View File

@@ -1,9 +1,6 @@
export default class AddChangePasswordQueueMessage { import NotificationQueueMessage from "./notificationQueueMessage";
type: string;
export default class AddChangePasswordQueueMessage extends NotificationQueueMessage {
cipherId: string; cipherId: string;
newPassword: string; newPassword: string;
domain: string;
tabId: string;
expires: Date;
wasVaultLocked: boolean;
} }

View File

@@ -1,10 +1,7 @@
export default class AddLoginQueueMessage { import NotificationQueueMessage from "./notificationQueueMessage";
type: string;
export default class AddLoginQueueMessage extends NotificationQueueMessage {
username: string; username: string;
password: string; password: string;
domain: string;
uri: string; uri: string;
tabId: string;
expires: Date;
wasVaultLocked: boolean;
} }

View File

@@ -0,0 +1,9 @@
import { NotificationQueueMessageType } from "./NotificationQueueMessageType";
export default class NotificationQueueMessage {
type: NotificationQueueMessageType;
domain: string;
tabId: number;
expires: Date;
wasVaultLocked: boolean;
}

View File

@@ -0,0 +1,4 @@
export enum NotificationQueueMessageType {
addLogin = 'addLogin',
changePassword = 'changePassword',
}

View File

@@ -22,10 +22,11 @@ import { PolicyType } from 'jslib-common/enums/policyType';
import AddChangePasswordQueueMessage from './models/addChangePasswordQueueMessage'; import AddChangePasswordQueueMessage from './models/addChangePasswordQueueMessage';
import AddLoginQueueMessage from './models/addLoginQueueMessage'; import AddLoginQueueMessage from './models/addLoginQueueMessage';
import { NotificationQueueMessageType } from './models/NotificationQueueMessageType';
export default class NotificationBackground { export default class NotificationBackground {
private notificationQueue: any[] = []; private notificationQueue: (AddLoginQueueMessage | AddChangePasswordQueueMessage)[] = [];
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,
@@ -131,14 +132,14 @@ export default class NotificationBackground {
continue; continue;
} }
if (this.notificationQueue[i].type === 'addLogin') { if (this.notificationQueue[i].type === NotificationQueueMessageType.addLogin) {
BrowserApi.tabSendMessageData(tab, 'openNotificationBar', { BrowserApi.tabSendMessageData(tab, 'openNotificationBar', {
type: 'add', type: 'add',
typeData: { typeData: {
isVaultLocked: this.notificationQueue[i].wasVaultLocked, isVaultLocked: this.notificationQueue[i].wasVaultLocked,
}, },
}); });
} else if (this.notificationQueue[i].type === 'changePassword') { } else if (this.notificationQueue[i].type === NotificationQueueMessageType.changePassword) {
BrowserApi.tabSendMessageData(tab, 'openNotificationBar', { BrowserApi.tabSendMessageData(tab, 'openNotificationBar', {
type: 'change', type: 'change',
typeData: { typeData: {
@@ -204,7 +205,7 @@ export default class NotificationBackground {
// remove any old messages for this tab // remove any old messages for this tab
this.removeTabFromNotificationQueue(tab); this.removeTabFromNotificationQueue(tab);
const message: AddLoginQueueMessage = { const message: AddLoginQueueMessage = {
type: 'addLogin', type: NotificationQueueMessageType.addLogin,
username: loginInfo.username, username: loginInfo.username,
password: loginInfo.password, password: loginInfo.password,
domain: loginDomain, domain: loginDomain,
@@ -247,7 +248,7 @@ export default class NotificationBackground {
// remove any old messages for this tab // remove any old messages for this tab
this.removeTabFromNotificationQueue(tab); this.removeTabFromNotificationQueue(tab);
const message: AddChangePasswordQueueMessage = { const message: AddChangePasswordQueueMessage = {
type: 'changePassword', type: NotificationQueueMessageType.changePassword,
cipherId: cipherId, cipherId: cipherId,
newPassword: newPassword, newPassword: newPassword,
domain: loginDomain, domain: loginDomain,
@@ -263,7 +264,7 @@ export default class NotificationBackground {
for (let i = this.notificationQueue.length - 1; i >= 0; i--) { for (let i = this.notificationQueue.length - 1; i >= 0; i--) {
const queueMessage = this.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 !== NotificationQueueMessageType.addLogin && queueMessage.type !== NotificationQueueMessageType.changePassword)) {
continue; continue;
} }
@@ -275,7 +276,7 @@ export default class NotificationBackground {
this.notificationQueue.splice(i, 1); this.notificationQueue.splice(i, 1);
BrowserApi.tabSendMessageData(tab, 'closeNotificationBar'); BrowserApi.tabSendMessageData(tab, 'closeNotificationBar');
if (queueMessage.type === 'changePassword') { if (queueMessage.type === NotificationQueueMessageType.changePassword) {
const message = (queueMessage as AddChangePasswordQueueMessage); const message = (queueMessage as AddChangePasswordQueueMessage);
const cipher = await this.getDecryptedCipherById(message.cipherId); const cipher = await this.getDecryptedCipherById(message.cipherId);
if (cipher == null) { if (cipher == null) {
@@ -286,11 +287,11 @@ export default class NotificationBackground {
} }
if (!queueMessage.wasVaultLocked) { if (!queueMessage.wasVaultLocked) {
await this.createNewCipher(queueMessage, folderId); await this.createNewCipher(queueMessage as AddLoginQueueMessage, folderId);
} }
// If the vault was locked, check if a cipher needs updating instead of creating a new one // If the vault was locked, check if a cipher needs updating instead of creating a new one
if (queueMessage.type === 'addLogin' && queueMessage.wasVaultLocked === true) { if (queueMessage.type === NotificationQueueMessageType.addLogin && queueMessage.wasVaultLocked === true) {
const message = (queueMessage as AddLoginQueueMessage); const message = (queueMessage as AddLoginQueueMessage);
const ciphers = await this.cipherService.getAllDecryptedForUrl(message.uri); const ciphers = await this.cipherService.getAllDecryptedForUrl(message.uri);
const usernameMatches = ciphers.filter(c => c.login.username != null && const usernameMatches = ciphers.filter(c => c.login.username != null &&
@@ -349,7 +350,7 @@ export default class NotificationBackground {
private async saveNever(tab: chrome.tabs.Tab) { private async saveNever(tab: chrome.tabs.Tab) {
for (let i = this.notificationQueue.length - 1; i >= 0; i--) { for (let i = this.notificationQueue.length - 1; i >= 0; i--) {
const queueMessage = this.notificationQueue[i]; const queueMessage = this.notificationQueue[i];
if (queueMessage.tabId !== tab.id || queueMessage.type !== 'addLogin') { if (queueMessage.tabId !== tab.id || queueMessage.type !== NotificationQueueMessageType.addLogin) {
continue; continue;
} }