1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-13 10:24:20 +01:00
bitwarden-browser/src/background/commands.background.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

113 lines
3.4 KiB
TypeScript
Raw Normal View History

import { PasswordGenerationService } from "jslib-common/abstractions/passwordGeneration.service";
import { PlatformUtilsService } from "jslib-common/abstractions/platformUtils.service";
import { VaultTimeoutService } from "jslib-common/abstractions/vaultTimeout.service";
2022-02-24 18:14:04 +01:00
import { BrowserApi } from "../browser/browserApi";
import MainBackground from "./main.background";
import LockedVaultPendingNotificationsItem from "./models/lockedVaultPendingNotificationsItem";
export default class CommandsBackground {
private isSafari: boolean;
private isVivaldi: boolean;
2021-12-21 15:43:35 +01:00
constructor(
private main: MainBackground,
private passwordGenerationService: PasswordGenerationService,
private platformUtilsService: PlatformUtilsService,
private vaultTimeoutService: VaultTimeoutService
) {
this.isSafari = this.platformUtilsService.isSafari();
this.isVivaldi = this.platformUtilsService.isVivaldi();
}
2021-12-21 15:43:35 +01:00
async init() {
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
);
}
2021-10-19 14:15:15 +02:00
if (this.isVivaldi && msg.command === "keyboardShortcutTriggered" && msg.shortcut) {
await this.processCommand(msg.shortcut, sender);
}
}
2021-12-21 15:43:35 +01:00
);
if (!this.isVivaldi && chrome && chrome.commands) {
2021-10-19 14:15:15 +02:00
chrome.commands.onCommand.addListener(async (command: string) => {
await this.processCommand(command);
2021-12-21 15:43:35 +01:00
});
}
}
2021-10-19 14:15:15 +02:00
private async processCommand(command: string, sender?: chrome.runtime.MessageSender) {
switch (command) {
case "generate_password":
await this.generatePasswordToClipboard();
2021-12-21 15:43:35 +01:00
break;
case "autofill_login":
await this.autoFillLogin(sender ? sender.tab : null);
2021-12-21 15:43:35 +01:00
break;
case "open_popup":
await this.openPopup();
2021-12-21 15:43:35 +01:00
break;
case "lock_vault":
await this.vaultTimeoutService.lock(true);
2021-12-21 15:43:35 +01:00
break;
default:
break;
}
}
private async generatePasswordToClipboard() {
const options = (await this.passwordGenerationService.getOptions())[0];
2018-01-09 23:55:28 +01:00
const password = await this.passwordGenerationService.generatePassword(options);
this.platformUtilsService.copyToClipboard(password, { window: window });
this.passwordGenerationService.addHistory(password);
2021-12-21 15:43:35 +01:00
}
2021-10-19 14:15:15 +02:00
private async autoFillLogin(tab?: chrome.tabs.Tab) {
if (!tab) {
tab = await BrowserApi.getTabFromCurrentWindowId();
}
if (tab == null) {
return;
}
if (await this.vaultTimeoutService.isLocked()) {
const retryMessage: LockedVaultPendingNotificationsItem = {
commandToRetry: {
msg: { command: "autofill_login" },
sender: { tab: tab },
2021-12-21 15:43:35 +01:00
},
target: "commands.background",
2021-12-21 15:43:35 +01:00
};
await BrowserApi.tabSendMessageData(
2021-12-21 15:43:35 +01:00
tab,
"addToLockedVaultPendingNotifications",
retryMessage
2021-12-21 15:43:35 +01:00
);
BrowserApi.tabSendMessageData(tab, "promptForLogin");
2021-12-21 15:43:35 +01:00
return;
}
2018-01-18 22:17:58 +01:00
await this.main.collectPageDetailsForContentScript(tab, "autofill_cmd");
}
2018-01-18 22:17:58 +01:00
private async openPopup() {
// Chrome APIs cannot open popup
2018-01-18 22:17:58 +01:00
if (!this.isSafari) {
return;
}
2021-12-21 15:43:35 +01:00
this.main.openPopup();
2021-12-21 15:43:35 +01:00
}
}