From 4c165617f028623fc97a17e52cfc5fd740c58333 Mon Sep 17 00:00:00 2001
From: Cesar Gonzalez <cgonzalez@bitwarden.com>
Date: Mon, 8 Apr 2024 05:28:27 -0500
Subject: [PATCH] [PM-5189] Fixing issue where unlocking account can focus the
 wrong field

---
 .../autofill/background/overlay.background.ts   | 17 ++++++++++++-----
 .../content/abstractions/autofill-init.ts       |  6 +++---
 .../autofill-overlay-content.service.ts         |  2 +-
 3 files changed, 16 insertions(+), 9 deletions(-)

diff --git a/apps/browser/src/autofill/background/overlay.background.ts b/apps/browser/src/autofill/background/overlay.background.ts
index 24ad70ac54..bfb2c203bc 100644
--- a/apps/browser/src/autofill/background/overlay.background.ts
+++ b/apps/browser/src/autofill/background/overlay.background.ts
@@ -607,11 +607,18 @@ class OverlayBackground implements OverlayBackgroundInterface {
   private async openOverlay(isFocusingFieldElement = false, isOpeningFullOverlay = false) {
     const currentTab = await BrowserApi.getTabFromCurrentWindowId();
 
-    await BrowserApi.tabSendMessageData(currentTab, "openAutofillOverlay", {
-      isFocusingFieldElement,
-      isOpeningFullOverlay,
-      authStatus: await this.getAuthStatus(),
-    });
+    await BrowserApi.tabSendMessage(
+      currentTab,
+      {
+        command: "openAutofillOverlay",
+        isFocusingFieldElement,
+        isOpeningFullOverlay,
+        authStatus: await this.getAuthStatus(),
+      },
+      {
+        frameId: this.focusedFieldData?.tabId === currentTab.id ? this.focusedFieldData.frameId : 0,
+      },
+    );
   }
 
   /**
diff --git a/apps/browser/src/autofill/content/abstractions/autofill-init.ts b/apps/browser/src/autofill/content/abstractions/autofill-init.ts
index 0ab2c342eb..aacee606c2 100644
--- a/apps/browser/src/autofill/content/abstractions/autofill-init.ts
+++ b/apps/browser/src/autofill/content/abstractions/autofill-init.ts
@@ -14,12 +14,12 @@ export type AutofillExtensionMessage = {
   ciphers?: any;
   isInlineMenuHidden?: boolean;
   overlayElement?: string;
+  isFocusingFieldElement?: boolean;
+  authStatus?: AuthenticationStatus;
+  isOpeningFullOverlay?: boolean;
   data?: {
-    authStatus?: AuthenticationStatus;
-    isFocusingFieldElement?: boolean;
     isOverlayCiphersPopulated?: boolean;
     direction?: "previous" | "next" | "current";
-    isOpeningFullOverlay?: boolean;
     forceCloseOverlay?: boolean;
     autofillOverlayVisibility?: number;
   };
diff --git a/apps/browser/src/autofill/services/autofill-overlay-content.service.ts b/apps/browser/src/autofill/services/autofill-overlay-content.service.ts
index 55c018f2f5..9d5481d1fd 100644
--- a/apps/browser/src/autofill/services/autofill-overlay-content.service.ts
+++ b/apps/browser/src/autofill/services/autofill-overlay-content.service.ts
@@ -39,7 +39,7 @@ class AutofillOverlayContentService implements AutofillOverlayContentServiceInte
   private autofillFieldKeywordsMap: WeakMap<AutofillField, string> = new WeakMap();
   private eventHandlersMemo: { [key: string]: EventListener } = {};
   readonly extensionMessageHandlers: AutofillOverlayContentExtensionMessageHandlers = {
-    openAutofillOverlay: ({ message }) => this.openAutofillOverlay(message.data),
+    openAutofillOverlay: ({ message }) => this.openAutofillOverlay(message),
     addNewVaultItemFromOverlay: () => this.addNewVaultItem(),
     blurMostRecentOverlayField: () => this.blurMostRecentOverlayField(),
     bgUnlockPopoutOpened: () => this.blurMostRecentOverlayField(true),