1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-14 10:26:19 +01:00
bitwarden-browser/apps/browser/src/background/commands.background.ts

116 lines
3.6 KiB
TypeScript
Raw Normal View History

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