From 97778a2bc8d62b85e5c505cb68c72897d71ef644 Mon Sep 17 00:00:00 2001 From: Cesar Gonzalez Date: Wed, 19 Jun 2024 05:11:17 -0500 Subject: [PATCH] Revert "[PM-5189] Working through content script port improvement" This reverts commit 8a48e576e13e94b8003d57e610356af73e378a75. --- .../abstractions/overlay.background.ts | 10 +++-- .../background/overlay.background.spec.ts | 18 +++++++++ .../autofill/background/overlay.background.ts | 37 +++++++++++-------- .../autofill-inline-menu-content.service.ts | 2 +- .../autofill-overlay-content.service.ts | 3 -- .../autofill-overlay-content.service.ts | 16 ++++---- 6 files changed, 54 insertions(+), 32 deletions(-) diff --git a/apps/browser/src/autofill/background/abstractions/overlay.background.ts b/apps/browser/src/autofill/background/abstractions/overlay.background.ts index 8b8ec47e89b..5e79585306c 100644 --- a/apps/browser/src/autofill/background/abstractions/overlay.background.ts +++ b/apps/browser/src/autofill/background/abstractions/overlay.background.ts @@ -94,10 +94,15 @@ export type BackgroundOnMessageHandlerParams = BackgroundMessageParam & Backgrou export type OverlayBackgroundExtensionMessageHandlers = { [key: string]: CallableFunction; + triggerAutofillOverlayReposition: ({ message, sender }: BackgroundOnMessageHandlerParams) => void; checkIsInlineMenuCiphersPopulated: ({ sender }: BackgroundSenderParam) => void; + updateFocusedFieldData: ({ message, sender }: BackgroundOnMessageHandlerParams) => void; + updateIsFieldCurrentlyFocused: ({ message }: BackgroundMessageParam) => void; + checkIsFieldCurrentlyFocused: () => boolean; updateIsFieldCurrentlyFilling: ({ message }: BackgroundMessageParam) => void; checkIsFieldCurrentlyFilling: () => boolean; getAutofillInlineMenuVisibility: () => void; + openAutofillInlineMenu: () => void; closeAutofillInlineMenu: ({ message, sender }: BackgroundOnMessageHandlerParams) => void; checkAutofillInlineMenuFocused: () => void; focusAutofillInlineMenuList: () => void; @@ -127,6 +132,7 @@ export type OverlayPortMessage = OverlayBackgroundExtensionMessage & { direction?: string; inlineMenuCipherId?: string; }; + export type PortMessageParam = { message: OverlayPortMessage; }; @@ -139,10 +145,6 @@ export type OverlayContentScriptPortMessageHandlers = { [key: string]: CallableFunction; autofillOverlayElementClosed: ({ message, port }: PortOnMessageHandlerParams) => void; autofillOverlayAddNewVaultItem: ({ message, port }: PortOnMessageHandlerParams) => void; - triggerAutofillOverlayReposition: ({ port }: PortConnectionParam) => void; - updateFocusedFieldData: ({ message, port }: PortOnMessageHandlerParams) => void; - updateIsFieldCurrentlyFocused: ({ message }: PortMessageParam) => void; - openAutofillInlineMenu: () => void; }; export type InlineMenuButtonPortMessageHandlers = { diff --git a/apps/browser/src/autofill/background/overlay.background.spec.ts b/apps/browser/src/autofill/background/overlay.background.spec.ts index 9ad8fdd598e..f71b3871026 100644 --- a/apps/browser/src/autofill/background/overlay.background.spec.ts +++ b/apps/browser/src/autofill/background/overlay.background.spec.ts @@ -790,6 +790,24 @@ describe("OverlayBackground", () => { }); }); + describe("checkIsFieldCurrentlyFocused message handler", () => { + it("returns true when a form field is currently focused", async () => { + sendMockExtensionMessage({ + command: "updateIsFieldCurrentlyFocused", + isFieldCurrentlyFocused: true, + }); + + sendMockExtensionMessage( + { command: "checkIsFieldCurrentlyFocused" }, + mock(), + sendResponse, + ); + await flushPromises(); + + expect(sendResponse).toHaveBeenCalledWith(true); + }); + }); + describe("checkIsFieldCurrentlyFilling message handler", () => { it("returns true if autofill is currently running", async () => { sendMockExtensionMessage({ diff --git a/apps/browser/src/autofill/background/overlay.background.ts b/apps/browser/src/autofill/background/overlay.background.ts index e9d5795fbe2..48f435ae8c4 100644 --- a/apps/browser/src/autofill/background/overlay.background.ts +++ b/apps/browser/src/autofill/background/overlay.background.ts @@ -77,11 +77,16 @@ export class OverlayBackground implements OverlayBackgroundInterface { private isFieldCurrentlyFilling: boolean = false; private iconsServerUrl: string; private readonly extensionMessageHandlers: OverlayBackgroundExtensionMessageHandlers = { + triggerAutofillOverlayReposition: ({ sender }) => this.triggerOverlayReposition(sender), checkIsInlineMenuCiphersPopulated: ({ sender }) => this.checkIsInlineMenuCiphersPopulated(sender), + updateFocusedFieldData: ({ message, sender }) => this.setFocusedFieldData(message, sender), + updateIsFieldCurrentlyFocused: ({ message }) => this.updateIsFieldCurrentlyFocused(message), + checkIsFieldCurrentlyFocused: () => this.checkIsFieldCurrentlyFocused(), updateIsFieldCurrentlyFilling: ({ message }) => this.updateIsFieldCurrentlyFilling(message), checkIsFieldCurrentlyFilling: () => this.checkIsFieldCurrentlyFilling(), getAutofillInlineMenuVisibility: () => this.getInlineMenuVisibility(), + openAutofillInlineMenu: () => this.openInlineMenu(false), closeAutofillInlineMenu: ({ message, sender }) => this.closeInlineMenu(sender, message), checkAutofillInlineMenuFocused: () => this.checkInlineMenuFocused(), focusAutofillInlineMenuList: () => this.focusInlineMenuList(), @@ -107,10 +112,6 @@ export class OverlayBackground implements OverlayBackgroundInterface { private readonly contentScriptPortMessageHandlers: OverlayContentScriptPortMessageHandlers = { autofillOverlayElementClosed: ({ message, port }) => this.overlayElementClosed(message, port), autofillOverlayAddNewVaultItem: ({ message, port }) => this.addNewVaultItem(message, port), - triggerAutofillOverlayReposition: ({ port }) => this.triggerOverlayReposition(port), - updateFocusedFieldData: ({ message, port }) => this.setFocusedFieldData(message, port), - updateIsFieldCurrentlyFocused: ({ message }) => this.updateIsFieldCurrentlyFocused(message), - openAutofillInlineMenu: () => this.openInlineMenu(false), }; private readonly inlineMenuButtonPortMessageHandlers: InlineMenuButtonPortMessageHandlers = { triggerDelayedAutofillInlineMenuClosure: ({ port }) => this.triggerDelayedInlineMenuClosure(), @@ -617,13 +618,13 @@ export class OverlayBackground implements OverlayBackgroundInterface { * the list and button ports and sets them to null. * * @param overlayElement - The overlay element that was closed, either the list or button - * @param sender - The sender of the port message + * @param port - The port that sent the message */ private overlayElementClosed( { overlayElement }: OverlayBackgroundExtensionMessage, - { sender }: chrome.runtime.Port, + port: chrome.runtime.Port, ) { - if (sender.tab.id !== this.focusedFieldData?.tabId) { + if (port.sender.tab.id !== this.focusedFieldData?.tabId) { this.expiredPorts.forEach((port) => port.disconnect()); this.expiredPorts = []; return; @@ -765,11 +766,11 @@ export class OverlayBackground implements OverlayBackgroundInterface { * Sets the focused field data to the data passed in the extension message. * * @param focusedFieldData - Contains the rects and styles of the focused field. - * @param sender - The sender of the port message + * @param sender - The sender of the extension message */ private setFocusedFieldData( { focusedFieldData }: OverlayBackgroundExtensionMessage, - { sender }: chrome.runtime.Port, + sender: chrome.runtime.MessageSender, ) { if (this.focusedFieldData?.frameId && this.focusedFieldData.frameId !== sender.frameId) { void BrowserApi.tabSendMessage( @@ -1019,12 +1020,9 @@ export class OverlayBackground implements OverlayBackgroundInterface { * data captured in the extension message. * * @param login - The login data captured from the extension message - * @param sender - The sender of the port message + * @param port - The content script port */ - private async addNewVaultItem( - { login }: OverlayAddNewItemMessage, - { sender }: chrome.runtime.Port, - ) { + private async addNewVaultItem({ login }: OverlayAddNewItemMessage, port: chrome.runtime.Port) { if (!login) { return; } @@ -1048,7 +1046,7 @@ export class OverlayBackground implements OverlayBackgroundInterface { collectionIds: cipherView.collectionIds, }); - await this.openAddEditVaultItemPopout(sender.tab, { cipherId: cipherView.id }); + await this.openAddEditVaultItemPopout(port.sender.tab, { cipherId: cipherView.id }); await BrowserApi.sendMessage("inlineAutofillMenuRefreshAddEditCipher"); } @@ -1061,6 +1059,13 @@ export class OverlayBackground implements OverlayBackgroundInterface { this.isFieldCurrentlyFocused = message.isFieldCurrentlyFocused; } + /** + * Allows a content script to check if a form field setup for the inline menu is currently focused. + */ + private checkIsFieldCurrentlyFocused() { + return this.isFieldCurrentlyFocused; + } + /** * Updates the property that identifies if a form field is currently being autofilled. * @@ -1361,7 +1366,7 @@ export class OverlayBackground implements OverlayBackgroundInterface { } }; - private async triggerOverlayReposition({ sender }: chrome.runtime.Port) { + private async triggerOverlayReposition(sender: chrome.runtime.MessageSender) { if (await this.checkShouldRepositionInlineMenu(sender)) { await this.toggleInlineMenuHidden({ isInlineMenuHidden: true }, sender); this.repositionInlineMenuSubject.next(sender); diff --git a/apps/browser/src/autofill/overlay/inline-menu/content/autofill-inline-menu-content.service.ts b/apps/browser/src/autofill/overlay/inline-menu/content/autofill-inline-menu-content.service.ts index 13e7407c016..e44cc2e3bf8 100644 --- a/apps/browser/src/autofill/overlay/inline-menu/content/autofill-inline-menu-content.service.ts +++ b/apps/browser/src/autofill/overlay/inline-menu/content/autofill-inline-menu-content.service.ts @@ -425,7 +425,7 @@ export class AutofillInlineMenuContentService implements AutofillInlineMenuConte */ private sendPortMessage( command: string, - message: Omit = {}, + message: Omit, ) { this.port.postMessage({ command, ...message }); } diff --git a/apps/browser/src/autofill/services/abstractions/autofill-overlay-content.service.ts b/apps/browser/src/autofill/services/abstractions/autofill-overlay-content.service.ts index cceeddbac80..a168ebe72c1 100644 --- a/apps/browser/src/autofill/services/abstractions/autofill-overlay-content.service.ts +++ b/apps/browser/src/autofill/services/abstractions/autofill-overlay-content.service.ts @@ -1,7 +1,6 @@ import { AuthenticationStatus } from "@bitwarden/common/auth/enums/authentication-status"; import { - FocusedFieldData, OverlayAddNewItemMessage, SubFrameOffsetData, } from "../../background/abstractions/overlay.background"; @@ -42,8 +41,6 @@ export type AutofillOverlayContentExtensionMessageHandlers = { export type AutofillOverlayContentExtensionMessage = { command: string; overlayElement?: AutofillOverlayElementType; - focusedFieldData?: FocusedFieldData; - isFieldCurrentlyFocused?: boolean; } & OverlayAddNewItemMessage; export interface AutofillOverlayContentService { 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 af1ae6aaa6c..aa0d46c9f96 100644 --- a/apps/browser/src/autofill/services/autofill-overlay-content.service.ts +++ b/apps/browser/src/autofill/services/autofill-overlay-content.service.ts @@ -347,7 +347,7 @@ export class AutofillOverlayContentService implements AutofillOverlayContentServ * is currently focused. */ private handleFormFieldBlurEvent = () => { - this.sendPortMessage("updateIsFieldCurrentlyFocused", { + void this.sendExtensionMessage("updateIsFieldCurrentlyFocused", { isFieldCurrentlyFocused: false, }); void this.sendExtensionMessage("checkAutofillInlineMenuFocused"); @@ -371,7 +371,7 @@ export class AutofillOverlayContentService implements AutofillOverlayContentServ } if (eventCode === "Enter" && !(await this.isFieldCurrentlyFilling())) { - this.handleOverlayRepositionEvent(); + void this.handleOverlayRepositionEvent(); return; } @@ -511,7 +511,7 @@ export class AutofillOverlayContentService implements AutofillOverlayContentServ return; } - this.sendPortMessage("updateIsFieldCurrentlyFocused", { + await this.sendExtensionMessage("updateIsFieldCurrentlyFocused", { isFieldCurrentlyFocused: true, }); if (this.userInteractionEventTimeout) { @@ -540,7 +540,7 @@ export class AutofillOverlayContentService implements AutofillOverlayContentServ return; } - this.sendPortMessage("openAutofillInlineMenu"); + void this.sendExtensionMessage("openAutofillInlineMenu"); } /** @@ -621,7 +621,7 @@ export class AutofillOverlayContentService implements AutofillOverlayContentServ focusedFieldRects: { width, height, top, left }, }; - this.sendPortMessage("updateFocusedFieldData", { + await this.sendExtensionMessage("updateFocusedFieldData", { focusedFieldData: this.focusedFieldData, }); } @@ -1071,8 +1071,8 @@ export class AutofillOverlayContentService implements AutofillOverlayContentServ * Handles the resize or scroll events that enact * repositioning of existing overlay elements. */ - private handleOverlayRepositionEvent = () => { - this.sendPortMessage("triggerAutofillOverlayReposition"); + private handleOverlayRepositionEvent = async () => { + await this.sendExtensionMessage("triggerAutofillOverlayReposition"); }; private setupRebuildSubFrameOffsetsListeners = () => { @@ -1130,7 +1130,7 @@ export class AutofillOverlayContentService implements AutofillOverlayContentServ */ private sendPortMessage( command: string, - message: Omit = {}, + message: Omit, ) { this.port.postMessage({ command, ...message }); }