1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-16 16:23:44 +00:00

Update badge number when saving a new entry (#2284)

* Rename message to changePasswordMessage

* Rename message variable to addLoginMessage

* Add early return and remove unneeded if below

* Update badge and menu after adding an entry

* Adjusted casing of enum properties

* Add explicit check for queueMessageType

* Turn NotificationQueueMessageType into simple enum
This commit is contained in:
Daniel James Smith
2022-01-25 08:16:36 +01:00
committed by GitHub
parent 07a3a1ea06
commit 71913a5eb5
3 changed files with 33 additions and 31 deletions

View File

@@ -1,4 +1,4 @@
export enum NotificationQueueMessageType { export enum NotificationQueueMessageType {
addLogin = "addLogin", AddLogin = 0,
changePassword = "changePassword", ChangePassword = 1,
} }

View File

@@ -170,14 +170,14 @@ export default class NotificationBackground {
continue; continue;
} }
if (this.notificationQueue[i].type === NotificationQueueMessageType.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 === NotificationQueueMessageType.changePassword) { } else if (this.notificationQueue[i].type === NotificationQueueMessageType.ChangePassword) {
BrowserApi.tabSendMessageData(tab, "openNotificationBar", { BrowserApi.tabSendMessageData(tab, "openNotificationBar", {
type: "change", type: "change",
typeData: { typeData: {
@@ -265,7 +265,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: NotificationQueueMessageType.addLogin, type: NotificationQueueMessageType.AddLogin,
username: loginInfo.username, username: loginInfo.username,
password: loginInfo.password, password: loginInfo.password,
domain: loginDomain, domain: loginDomain,
@@ -316,7 +316,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: NotificationQueueMessageType.changePassword, type: NotificationQueueMessageType.ChangePassword,
cipherId: cipherId, cipherId: cipherId,
newPassword: newPassword, newPassword: newPassword,
domain: loginDomain, domain: loginDomain,
@@ -333,8 +333,8 @@ export default class NotificationBackground {
const queueMessage = this.notificationQueue[i]; const queueMessage = this.notificationQueue[i];
if ( if (
queueMessage.tabId !== tab.id || queueMessage.tabId !== tab.id ||
(queueMessage.type !== NotificationQueueMessageType.addLogin && (queueMessage.type !== NotificationQueueMessageType.AddLogin &&
queueMessage.type !== NotificationQueueMessageType.changePassword) queueMessage.type !== NotificationQueueMessageType.ChangePassword)
) { ) {
continue; continue;
} }
@@ -347,37 +347,38 @@ 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 === NotificationQueueMessageType.changePassword) { if (queueMessage.type === NotificationQueueMessageType.ChangePassword) {
const message = queueMessage as AddChangePasswordQueueMessage; const changePasswordMessage = queueMessage as AddChangePasswordQueueMessage;
const cipher = await this.getDecryptedCipherById(message.cipherId); const cipher = await this.getDecryptedCipherById(changePasswordMessage.cipherId);
if (cipher == null) { if (cipher == null) {
return; return;
} }
await this.updateCipher(cipher, message.newPassword); await this.updateCipher(cipher, changePasswordMessage.newPassword);
return; return;
} }
if (queueMessage.type === NotificationQueueMessageType.AddLogin) {
if (!queueMessage.wasVaultLocked) { if (!queueMessage.wasVaultLocked) {
await this.createNewCipher(queueMessage as AddLoginQueueMessage, folderId); await this.createNewCipher(queueMessage as AddLoginQueueMessage, folderId);
BrowserApi.tabSendMessageData(tab, "addedCipher");
return;
} }
// 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 ( const addLoginMessage = queueMessage as AddLoginQueueMessage;
queueMessage.type === NotificationQueueMessageType.addLogin && const ciphers = await this.cipherService.getAllDecryptedForUrl(addLoginMessage.uri);
queueMessage.wasVaultLocked === true
) {
const message = queueMessage as AddLoginQueueMessage;
const ciphers = await this.cipherService.getAllDecryptedForUrl(message.uri);
const usernameMatches = ciphers.filter( const usernameMatches = ciphers.filter(
(c) => c.login.username != null && c.login.username.toLowerCase() === message.username (c) =>
c.login.username != null && c.login.username.toLowerCase() === addLoginMessage.username
); );
if (usernameMatches.length >= 1) { if (usernameMatches.length >= 1) {
await this.updateCipher(usernameMatches[0], message.password); await this.updateCipher(usernameMatches[0], addLoginMessage.password);
return; return;
} }
await this.createNewCipher(message, folderId); await this.createNewCipher(addLoginMessage, folderId);
BrowserApi.tabSendMessageData(tab, "addedCipher");
} }
} }
} }
@@ -427,7 +428,7 @@ export default class NotificationBackground {
const queueMessage = this.notificationQueue[i]; const queueMessage = this.notificationQueue[i];
if ( if (
queueMessage.tabId !== tab.id || queueMessage.tabId !== tab.id ||
queueMessage.type !== NotificationQueueMessageType.addLogin queueMessage.type !== NotificationQueueMessageType.AddLogin
) { ) {
continue; continue;
} }

View File

@@ -28,6 +28,7 @@ const forwardCommands = [
"promptForLogin", "promptForLogin",
"addToLockedVaultPendingNotifications", "addToLockedVaultPendingNotifications",
"unlockCompleted", "unlockCompleted",
"addedCipher",
]; ];
chrome.runtime.onMessage.addListener((event) => { chrome.runtime.onMessage.addListener((event) => {