From eb5eb1c49e3f6f588f0aff64fd3d7b563e3dae9d Mon Sep 17 00:00:00 2001 From: Justin Baur <19896123+justindbaur@users.noreply.github.com> Date: Tue, 14 Feb 2023 08:14:51 -0500 Subject: [PATCH] Revert to MV2 Autofill Logic (#4705) --- .../browser/context-menu-clicked-handler.spec.ts | 13 ++++++------- .../browser/context-menu-clicked-handler.ts | 11 ++++++++--- apps/browser/src/background/main.background.ts | 14 ++++++++++++-- 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/apps/browser/src/autofill/browser/context-menu-clicked-handler.spec.ts b/apps/browser/src/autofill/browser/context-menu-clicked-handler.spec.ts index 1ac8b5d9dc..a9dbcbaacc 100644 --- a/apps/browser/src/autofill/browser/context-menu-clicked-handler.spec.ts +++ b/apps/browser/src/autofill/browser/context-menu-clicked-handler.spec.ts @@ -9,13 +9,12 @@ import { CipherType } from "@bitwarden/common/vault/enums/cipher-type"; import { Cipher } from "@bitwarden/common/vault/models/domain/cipher"; import { CipherView } from "@bitwarden/common/vault/models/view/cipher.view"; -import { AutofillTabCommand } from "../commands/autofill-tab-command"; - import { CopyToClipboardAction, ContextMenuClickedHandler, CopyToClipboardOptions, GeneratePasswordToClipboardAction, + AutofillAction, } from "./context-menu-clicked-handler"; import { AUTOFILL_ID, @@ -59,9 +58,9 @@ describe("ContextMenuClickedHandler", () => { let copyToClipboard: CopyToClipboardAction; let generatePasswordToClipboard: GeneratePasswordToClipboardAction; + let autofill: AutofillAction; let authService: MockProxy; let cipherService: MockProxy; - let autofillTabCommand: MockProxy; let totpService: MockProxy; let eventCollectionService: MockProxy; @@ -70,18 +69,18 @@ describe("ContextMenuClickedHandler", () => { beforeEach(() => { copyToClipboard = jest.fn(); generatePasswordToClipboard = jest.fn, [tab: chrome.tabs.Tab]>(); + autofill = jest.fn, [tab: chrome.tabs.Tab, cipher: CipherView]>(); authService = mock(); cipherService = mock(); - autofillTabCommand = mock(); totpService = mock(); eventCollectionService = mock(); sut = new ContextMenuClickedHandler( copyToClipboard, generatePasswordToClipboard, + autofill, authService, cipherService, - autofillTabCommand, totpService, eventCollectionService ); @@ -106,9 +105,9 @@ describe("ContextMenuClickedHandler", () => { await sut.run(createData("T_1", AUTOFILL_ID), { id: 5 } as any); - expect(autofillTabCommand.doAutofillTabWithCipherCommand).toBeCalledTimes(1); + expect(autofill).toBeCalledTimes(1); - expect(autofillTabCommand.doAutofillTabWithCipherCommand).toBeCalledWith({ id: 5 }, cipher); + expect(autofill).toBeCalledWith({ id: 5 }, cipher); }); it("copies username to clipboard", async () => { diff --git a/apps/browser/src/autofill/browser/context-menu-clicked-handler.ts b/apps/browser/src/autofill/browser/context-menu-clicked-handler.ts index 840ae7a774..9583c077c8 100644 --- a/apps/browser/src/autofill/browser/context-menu-clicked-handler.ts +++ b/apps/browser/src/autofill/browser/context-menu-clicked-handler.ts @@ -43,6 +43,7 @@ import { export type CopyToClipboardOptions = { text: string; tab: chrome.tabs.Tab }; export type CopyToClipboardAction = (options: CopyToClipboardOptions) => void; +export type AutofillAction = (tab: chrome.tabs.Tab, cipher: CipherView) => Promise; export type GeneratePasswordToClipboardAction = (tab: chrome.tabs.Tab) => Promise; @@ -53,9 +54,9 @@ export class ContextMenuClickedHandler { constructor( private copyToClipboard: CopyToClipboardAction, private generatePasswordToClipboard: GeneratePasswordToClipboardAction, + private autofillAction: AutofillAction, private authService: AuthService, private cipherService: CipherService, - private autofillTabCommand: AutofillTabCommand, private totpService: TotpService, private eventCollectionService: EventCollectionService ) {} @@ -104,12 +105,16 @@ export class ContextMenuClickedHandler { await stateServiceFactory(cachedServices, serviceOptions) ); + const autofillCommand = new AutofillTabCommand( + await autofillServiceFactory(cachedServices, serviceOptions) + ); + return new ContextMenuClickedHandler( (options) => copyToClipboard(options.tab, options.text), (tab) => generatePasswordToClipboardCommand.generatePasswordToClipboard(tab), + (tab, cipher) => autofillCommand.doAutofillTabWithCipherCommand(tab, cipher), await authServiceFactory(cachedServices, serviceOptions), await cipherServiceFactory(cachedServices, serviceOptions), - new AutofillTabCommand(await autofillServiceFactory(cachedServices, serviceOptions)), await totpServiceFactory(cachedServices, serviceOptions), await eventCollectionServiceFactory(cachedServices, serviceOptions) ); @@ -205,7 +210,7 @@ export class ContextMenuClickedHandler { if (tab == null) { return; } - await this.autofillTabCommand.doAutofillTabWithCipherCommand(tab, cipher); + await this.autofillAction(tab, cipher); break; case COPY_USERNAME_ID: this.copyToClipboard({ text: cipher.login.username, tab: tab }); diff --git a/apps/browser/src/background/main.background.ts b/apps/browser/src/background/main.background.ts index 5458488077..c51b8fa9f6 100644 --- a/apps/browser/src/background/main.background.ts +++ b/apps/browser/src/background/main.background.ts @@ -89,7 +89,6 @@ import TabsBackground from "../autofill/background/tabs.background"; import { CipherContextMenuHandler } from "../autofill/browser/cipher-context-menu-handler"; import { ContextMenuClickedHandler } from "../autofill/browser/context-menu-clicked-handler"; import { MainContextMenuHandler } from "../autofill/browser/main-context-menu-handler"; -import { AutofillTabCommand } from "../autofill/commands/autofill-tab-command"; import { AutofillService as AutofillServiceAbstraction } from "../autofill/services/abstractions/autofill.service"; import AutofillService from "../autofill/services/autofill.service"; import { BrowserApi } from "../browser/browserApi"; @@ -543,9 +542,20 @@ export default class MainBackground { this.platformUtilsService.copyToClipboard(password, { window: window }); this.passwordGenerationService.addHistory(password); }, + async (tab, cipher) => { + this.loginToAutoFill = cipher; + if (tab == null) { + return; + } + + BrowserApi.tabSendMessage(tab, { + command: "collectPageDetails", + tab: tab, + sender: "contextMenu", + }); + }, this.authService, this.cipherService, - new AutofillTabCommand(this.autofillService), this.totpService, this.eventCollectionService );