diff --git a/src/background/commands.background.ts b/src/background/commands.background.ts index 32d3243f098..2e78cdd3308 100644 --- a/src/background/commands.background.ts +++ b/src/background/commands.background.ts @@ -5,6 +5,7 @@ import MainBackground from './main.background'; import { PasswordGenerationService } from 'jslib-common/abstractions/passwordGeneration.service'; import { PlatformUtilsService } from 'jslib-common/abstractions/platformUtils.service'; import { VaultTimeoutService } from 'jslib-common/abstractions/vaultTimeout.service'; +import LockedVaultPendingNotificationsItem from './models/lockedVaultPendingNotificationsItem'; export default class CommandsBackground { private isSafari: boolean; @@ -17,20 +18,24 @@ export default class CommandsBackground { } async init() { - if (this.isVivaldi) { - BrowserApi.messageListener('commands.background', async (msg: any, sender: any, sendResponse: any) => { - if (msg.command === 'keyboardShortcutTriggered' && msg.shortcut) { - await this.processCommand(msg.shortcut, sender); - } - }); - } else if (chrome && chrome.commands) { - chrome.commands.onCommand.addListener(async (command: any) => { + BrowserApi.messageListener('commands.background', async (msg: any, sender: chrome.runtime.MessageSender, sendResponse: any) => { + if (msg.command === 'unlockCompleted' && msg.data.target === 'commands.background') { + await this.processCommand(msg.data.commandToRetry.msg.command, msg.data.commandToRetry.sender); + } + + if (this.isVivaldi && msg.command === 'keyboardShortcutTriggered' && msg.shortcut) { + await this.processCommand(msg.shortcut, sender); + } + }); + + if (!this.isVivaldi && chrome && chrome.commands) { + chrome.commands.onCommand.addListener(async (command: string) => { await this.processCommand(command); }); } } - private async processCommand(command: string, sender?: any) { + private async processCommand(command: string, sender?: chrome.runtime.MessageSender) { switch (command) { case 'generate_password': await this.generatePasswordToClipboard(); @@ -56,11 +61,7 @@ export default class CommandsBackground { this.passwordGenerationService.addHistory(password); } - private async autoFillLogin(tab?: any) { - if (await this.vaultTimeoutService.isLocked()) { - return; - } - + private async autoFillLogin(tab?: chrome.tabs.Tab) { if (!tab) { tab = await BrowserApi.getTabFromCurrentWindowId(); } @@ -69,6 +70,20 @@ export default class CommandsBackground { return; } + if (await this.vaultTimeoutService.isLocked()) { + const retryMessage: LockedVaultPendingNotificationsItem = { + commandToRetry: { + msg: { command: 'autofill_login' }, + sender: { tab: tab }, + }, + target: 'commands.background', + }; + await BrowserApi.tabSendMessageData(tab, 'addToLockedVaultPendingNotifications', retryMessage); + + BrowserApi.tabSendMessageData(tab, 'promptForLogin'); + return; + } + await this.main.collectPageDetailsForContentScript(tab, 'autofill_cmd'); } diff --git a/src/background/contextMenus.background.ts b/src/background/contextMenus.background.ts index e46fe8e3694..9a29294d4f1 100644 --- a/src/background/contextMenus.background.ts +++ b/src/background/contextMenus.background.ts @@ -11,8 +11,10 @@ import { VaultTimeoutService } from 'jslib-common/abstractions/vaultTimeout.serv import { EventType } from 'jslib-common/enums/eventType'; import { CipherView } from 'jslib-common/models/view/cipherView'; +import LockedVaultPendingNotificationsItem from './models/lockedVaultPendingNotificationsItem'; export default class ContextMenusBackground { + private readonly noopCommandSuffix = 'noop'; private contextMenus: any; constructor(private main: MainBackground, private cipherService: CipherService, @@ -27,16 +29,22 @@ export default class ContextMenusBackground { return; } - this.contextMenus.onClicked.addListener(async (info: any, tab: any) => { + this.contextMenus.onClicked.addListener(async (info: chrome.contextMenus.OnClickData, tab: chrome.tabs.Tab) => { if (info.menuItemId === 'generate-password') { await this.generatePasswordToClipboard(); } else if (info.menuItemId === 'copy-identifier') { - await this.getClickedElement(info.frameId); + await this.getClickedElement(tab, info.frameId); } else if (info.parentMenuItemId === 'autofill' || info.parentMenuItemId === 'copy-username' || info.parentMenuItemId === 'copy-password' || info.parentMenuItemId === 'copy-totp') { - await this.cipherAction(info); + await this.cipherAction(tab, info); + } + }); + + BrowserApi.messageListener('contextmenus.background', async (msg: any, sender: chrome.runtime.MessageSender, sendResponse: any) => { + if (msg.command === 'unlockCompleted' && msg.data.target === 'contextmenus.background') { + await this.cipherAction(msg.data.commandToRetry.sender.tab, msg.data.commandToRetry.msg.data); } }); } @@ -48,8 +56,7 @@ export default class ContextMenusBackground { this.passwordGenerationService.addHistory(password); } - private async getClickedElement(frameId: number) { - const tab = await BrowserApi.getTabFromCurrentWindow(); + private async getClickedElement(tab: chrome.tabs.Tab, frameId: number) { if (tab == null) { return; } @@ -57,27 +64,38 @@ export default class ContextMenusBackground { BrowserApi.tabSendMessage(tab, { command: 'getClickedElement' }, { frameId: frameId }); } - private async cipherAction(info: any) { + private async cipherAction(tab: chrome.tabs.Tab, info: chrome.contextMenus.OnClickData) { const id = info.menuItemId.split('_')[1]; - if (id === 'noop') { - if (chrome.browserAction && (chrome.browserAction as any).openPopup) { - (chrome.browserAction as any).openPopup(); - } - return; - } if (await this.vaultTimeoutService.isLocked()) { + const retryMessage: LockedVaultPendingNotificationsItem = { + commandToRetry: { + msg: { command: this.noopCommandSuffix, data: info }, + sender: { tab: tab }, + }, + target: 'contextmenus.background', + }; + await BrowserApi.tabSendMessageData(tab, 'addToLockedVaultPendingNotifications', retryMessage); + + BrowserApi.tabSendMessageData(tab, 'promptForLogin'); return; } - const ciphers = await this.cipherService.getAllDecrypted(); - const cipher = ciphers.find(c => c.id === id); + let cipher: CipherView; + if (id === this.noopCommandSuffix) { + const ciphers = await this.cipherService.getAllDecryptedForUrl(tab.url); + cipher = ciphers.length > 0 ? ciphers[0] : null; + } else { + const ciphers = await this.cipherService.getAllDecrypted(); + cipher = ciphers.find(c => c.id === id); + } + if (cipher == null) { return; } if (info.parentMenuItemId === 'autofill') { - await this.startAutofillPage(cipher); + await this.startAutofillPage(tab, cipher); } else if (info.parentMenuItemId === 'copy-username') { this.platformUtilsService.copyToClipboard(cipher.login.username, { window: window }); } else if (info.parentMenuItemId === 'copy-password') { @@ -89,9 +107,8 @@ export default class ContextMenusBackground { } } - private async startAutofillPage(cipher: CipherView) { + private async startAutofillPage(tab: chrome.tabs.Tab, cipher: CipherView) { this.main.loginToAutoFill = cipher; - const tab = await BrowserApi.getTabFromCurrentWindow(); if (tab == null) { return; }