From fd8de4ca0c1e054f0c3d9e2426ce8717441468c5 Mon Sep 17 00:00:00 2001 From: Daniel James Smith Date: Mon, 18 Oct 2021 16:41:42 +0200 Subject: [PATCH] Use messages/events instead of main.unlockCompleted --- src/background/main.background.ts | 18 +------------- .../lockedVaultPendingNotificationsItem.ts | 7 ++++++ src/background/notification.background.ts | 6 +++++ src/background/runtime.background.ts | 24 +++++++++---------- src/content/message_handler.ts | 7 ++++++ 5 files changed, 33 insertions(+), 29 deletions(-) create mode 100644 src/background/models/lockedVaultPendingNotificationsItem.ts diff --git a/src/background/main.background.ts b/src/background/main.background.ts index 880ab22862..486ef98b3c 100644 --- a/src/background/main.background.ts +++ b/src/background/main.background.ts @@ -129,16 +129,15 @@ export default class MainBackground { onUpdatedRan: boolean; onReplacedRan: boolean; loginToAutoFill: CipherView = null; - lockedVaultPendingNotifications: { commandToRetry: any, from: string }[] = []; private commandsBackground: CommandsBackground; private contextMenusBackground: ContextMenusBackground; private idleBackground: IdleBackground; + private notificationBackground: NotificationBackground; private runtimeBackground: RuntimeBackground; private tabsBackground: TabsBackground; private webRequestBackground: WebRequestBackground; private windowsBackground: WindowsBackground; - private notificationBackground: NotificationBackground; private sidebarAction: any; private buildingContextMenu: boolean; @@ -344,21 +343,6 @@ export default class MainBackground { } } - async unlockCompleted() { - if (this.lockedVaultPendingNotifications.length === 0) { - return; - } - - const item = this.lockedVaultPendingNotifications.pop(); - switch (item.from) { - case 'notificationBar': - await this.notificationBackground.processMessage(item.commandToRetry, item.commandToRetry.sender, null); - break; - default: - break; - } - } - async logout(expired: boolean) { await this.eventService.uploadEvents(); const userId = await this.userService.getUserId(); diff --git a/src/background/models/lockedVaultPendingNotificationsItem.ts b/src/background/models/lockedVaultPendingNotificationsItem.ts new file mode 100644 index 0000000000..df2f2eb7aa --- /dev/null +++ b/src/background/models/lockedVaultPendingNotificationsItem.ts @@ -0,0 +1,7 @@ +export default class lockedVaultPendingNotificationsItem { + commandToRetry: { + msg: any; + sender: chrome.runtime.MessageSender; + } + target: string; +} diff --git a/src/background/notification.background.ts b/src/background/notification.background.ts index 580248a049..58d2a04878 100644 --- a/src/background/notification.background.ts +++ b/src/background/notification.background.ts @@ -48,6 +48,12 @@ export default class NotificationBackground { async processMessage(msg: any, sender: chrome.runtime.MessageSender) { switch (msg.command) { + case 'unlockCompleted': + if (msg.data.target !== 'notification.background') { + return; + } + await this.processMessage(msg.data.commandToRetry.msg, msg.data.commandToRetry.sender); + break; case 'bgGetDataForTab': await this.getDataForTab(sender.tab, msg.responseCommand); break; diff --git a/src/background/runtime.background.ts b/src/background/runtime.background.ts index e57073e1d3..93782ae362 100644 --- a/src/background/runtime.background.ts +++ b/src/background/runtime.background.ts @@ -13,10 +13,13 @@ import { BrowserApi } from '../browser/browserApi'; import MainBackground from './main.background'; import { Utils } from 'jslib-common/misc/utils'; +import lockedVaultPendingNotificationsItem from './models/lockedVaultPendingNotificationsItem'; + export default class RuntimeBackground { private autofillTimeout: any; private pageDetailsToAutoFill: any[] = []; private onInstalledReason: string = null; + private lockedVaultPendingNotifications: lockedVaultPendingNotificationsItem[] = []; constructor(private main: MainBackground, private autofillService: AutofillService, private platformUtilsService: BrowserPlatformUtilsService, @@ -45,11 +48,13 @@ export default class RuntimeBackground { switch (msg.command) { case 'loggedIn': case 'unlocked': - if (this.main.lockedVaultPendingNotifications.length > 0) { + let item: lockedVaultPendingNotificationsItem; + + if (this.lockedVaultPendingNotifications.length > 0) { await BrowserApi.closeLoginTab(); - const item = this.main.lockedVaultPendingNotifications[0]; - if (item.commandToRetry?.sender?.tab?.id) { + item = this.lockedVaultPendingNotifications.pop(); + if (item.commandToRetry.sender?.tab?.id) { await BrowserApi.focusSpecifiedTab(item.commandToRetry.sender.tab.id); } } @@ -59,17 +64,12 @@ export default class RuntimeBackground { this.notificationsService.updateConnection(msg.command === 'unlocked'); this.systemService.cancelProcessReload(); - this.main.unlockCompleted(); + if (item) { + await BrowserApi.tabSendMessageData(item.commandToRetry.sender.tab, 'unlockCompleted', item); + } break; case 'addToLockedVaultPendingNotifications': - const retryMessage = { - commandToRetry: { - ...msg.retryItem, - sender: sender, - }, - from: msg.from, - }; - this.main.lockedVaultPendingNotifications.push(retryMessage); + this.lockedVaultPendingNotifications.push(msg.data); break; case 'logout': await this.main.logout(msg.expired); diff --git a/src/content/message_handler.ts b/src/content/message_handler.ts index 4358a97945..688553db7d 100644 --- a/src/content/message_handler.ts +++ b/src/content/message_handler.ts @@ -20,3 +20,10 @@ window.addEventListener('message', event => { }); } }, false); + +chrome.runtime.onMessage.addListener(event => { + + if (event.command === 'unlockCompleted') { + chrome.runtime.sendMessage(event); + } +});