1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-12 22:33:35 +00:00

[PM-5189] Working through content script port improvement

This commit is contained in:
Cesar Gonzalez
2024-06-19 02:08:14 -05:00
parent da357f46b3
commit e30a1ebc5d
6 changed files with 39 additions and 18 deletions

View File

@@ -94,8 +94,6 @@ export type BackgroundOnMessageHandlerParams = BackgroundMessageParam & Backgrou
export type OverlayBackgroundExtensionMessageHandlers = { export type OverlayBackgroundExtensionMessageHandlers = {
[key: string]: CallableFunction; [key: string]: CallableFunction;
autofillOverlayAddNewVaultItem: ({ message, sender }: BackgroundOnMessageHandlerParams) => void;
triggerAutofillOverlayReposition: ({ message, sender }: BackgroundOnMessageHandlerParams) => void; triggerAutofillOverlayReposition: ({ message, sender }: BackgroundOnMessageHandlerParams) => void;
checkIsInlineMenuCiphersPopulated: ({ sender }: BackgroundSenderParam) => void; checkIsInlineMenuCiphersPopulated: ({ sender }: BackgroundSenderParam) => void;
updateFocusedFieldData: ({ message, sender }: BackgroundOnMessageHandlerParams) => void; updateFocusedFieldData: ({ message, sender }: BackgroundOnMessageHandlerParams) => void;
@@ -146,6 +144,7 @@ export type PortOnMessageHandlerParams = PortMessageParam & PortConnectionParam;
export type OverlayContentScriptPortMessageHandlers = { export type OverlayContentScriptPortMessageHandlers = {
[key: string]: CallableFunction; [key: string]: CallableFunction;
autofillOverlayElementClosed: ({ message, port }: PortOnMessageHandlerParams) => void; autofillOverlayElementClosed: ({ message, port }: PortOnMessageHandlerParams) => void;
autofillOverlayAddNewVaultItem: ({ message, port }: PortOnMessageHandlerParams) => void;
}; };
export type InlineMenuButtonPortMessageHandlers = { export type InlineMenuButtonPortMessageHandlers = {

View File

@@ -76,7 +76,6 @@ export class OverlayBackground implements OverlayBackgroundInterface {
private isFieldCurrentlyFilling: boolean = false; private isFieldCurrentlyFilling: boolean = false;
private iconsServerUrl: string; private iconsServerUrl: string;
private readonly extensionMessageHandlers: OverlayBackgroundExtensionMessageHandlers = { private readonly extensionMessageHandlers: OverlayBackgroundExtensionMessageHandlers = {
autofillOverlayAddNewVaultItem: ({ message, sender }) => this.addNewVaultItem(message, sender),
triggerAutofillOverlayReposition: ({ sender }) => this.triggerOverlayReposition(sender), triggerAutofillOverlayReposition: ({ sender }) => this.triggerOverlayReposition(sender),
checkIsInlineMenuCiphersPopulated: ({ sender }) => checkIsInlineMenuCiphersPopulated: ({ sender }) =>
this.checkIsInlineMenuCiphersPopulated(sender), this.checkIsInlineMenuCiphersPopulated(sender),
@@ -111,6 +110,7 @@ export class OverlayBackground implements OverlayBackgroundInterface {
}; };
private readonly contentScriptPortMessageHandlers: OverlayContentScriptPortMessageHandlers = { private readonly contentScriptPortMessageHandlers: OverlayContentScriptPortMessageHandlers = {
autofillOverlayElementClosed: ({ message, port }) => this.overlayElementClosed(message, port), autofillOverlayElementClosed: ({ message, port }) => this.overlayElementClosed(message, port),
autofillOverlayAddNewVaultItem: ({ message, port }) => this.addNewVaultItem(message, port),
}; };
private readonly inlineMenuButtonPortMessageHandlers: InlineMenuButtonPortMessageHandlers = { private readonly inlineMenuButtonPortMessageHandlers: InlineMenuButtonPortMessageHandlers = {
triggerDelayedAutofillInlineMenuClosure: ({ port }) => this.triggerDelayedInlineMenuClosure(), triggerDelayedAutofillInlineMenuClosure: ({ port }) => this.triggerDelayedInlineMenuClosure(),
@@ -1015,12 +1015,9 @@ export class OverlayBackground implements OverlayBackgroundInterface {
* data captured in the extension message. * data captured in the extension message.
* *
* @param login - The login data captured from the extension message * @param login - The login data captured from the extension message
* @param sender - The sender of the extension message * @param port - The content script port
*/ */
private async addNewVaultItem( private async addNewVaultItem({ login }: OverlayAddNewItemMessage, port: chrome.runtime.Port) {
{ login }: OverlayAddNewItemMessage,
sender: chrome.runtime.MessageSender,
) {
if (!login) { if (!login) {
return; return;
} }
@@ -1044,7 +1041,7 @@ export class OverlayBackground implements OverlayBackgroundInterface {
collectionIds: cipherView.collectionIds, collectionIds: cipherView.collectionIds,
}); });
await this.openAddEditVaultItemPopout(sender.tab, { cipherId: cipherView.id }); await this.openAddEditVaultItemPopout(port.sender.tab, { cipherId: cipherView.id });
await BrowserApi.sendMessage("inlineAutofillMenuRefreshAddEditCipher"); await BrowserApi.sendMessage("inlineAutofillMenuRefreshAddEditCipher");
} }

View File

@@ -3,6 +3,9 @@ export const AutofillOverlayElement = {
List: "autofill-inline-menu-list", List: "autofill-inline-menu-list",
} as const; } as const;
export type AutofillOverlayElementType =
(typeof AutofillOverlayElement)[keyof typeof AutofillOverlayElement];
export const AutofillOverlayPort = { export const AutofillOverlayPort = {
ContentScript: "autofill-overlay-content-script-port", ContentScript: "autofill-overlay-content-script-port",
Button: "autofill-inline-menu-button-port", Button: "autofill-inline-menu-button-port",

View File

@@ -1,10 +1,7 @@
import { AutofillExtensionMessage } from "../../../content/abstractions/autofill-init"; import { AutofillExtensionMessage } from "../../../content/abstractions/autofill-init";
import { AutofillOverlayElement } from "../../../enums/autofill-overlay.enum"; import { AutofillOverlayElement } from "../../../enums/autofill-overlay.enum";
import { import { AutofillOverlayContentExtensionMessage } from "../../../services/abstractions/autofill-overlay-content.service";
sendExtensionMessage, import { generateRandomCustomElementName, setElementStyles } from "../../../utils";
generateRandomCustomElementName,
setElementStyles,
} from "../../../utils";
import { import {
InlineMenuExtensionMessageHandlers, InlineMenuExtensionMessageHandlers,
AutofillInlineMenuContentService as AutofillInlineMenuContentServiceInterface, AutofillInlineMenuContentService as AutofillInlineMenuContentServiceInterface,
@@ -13,7 +10,6 @@ import { AutofillInlineMenuButtonIframe } from "../iframe-content/autofill-inlin
import { AutofillInlineMenuListIframe } from "../iframe-content/autofill-inline-menu-list-iframe"; import { AutofillInlineMenuListIframe } from "../iframe-content/autofill-inline-menu-list-iframe";
export class AutofillInlineMenuContentService implements AutofillInlineMenuContentServiceInterface { export class AutofillInlineMenuContentService implements AutofillInlineMenuContentServiceInterface {
private readonly sendExtensionMessage = sendExtensionMessage;
private readonly generateRandomCustomElementName = generateRandomCustomElementName; private readonly generateRandomCustomElementName = generateRandomCustomElementName;
private readonly setElementStyles = setElementStyles; private readonly setElementStyles = setElementStyles;
private isFirefoxBrowser = private isFirefoxBrowser =
@@ -427,7 +423,10 @@ export class AutofillInlineMenuContentService implements AutofillInlineMenuConte
* @param command - The command to send through the port. * @param command - The command to send through the port.
* @param message - The message to send through the port. * @param message - The message to send through the port.
*/ */
private sendPortMessage(command: string, message: Omit<AutofillExtensionMessage, "command">) { private sendPortMessage(
command: string,
message: Omit<AutofillOverlayContentExtensionMessage, "command">,
) {
this.port.postMessage({ command, ...message }); this.port.postMessage({ command, ...message });
} }

View File

@@ -1,7 +1,11 @@
import { AuthenticationStatus } from "@bitwarden/common/auth/enums/authentication-status"; import { AuthenticationStatus } from "@bitwarden/common/auth/enums/authentication-status";
import { SubFrameOffsetData } from "../../background/abstractions/overlay.background"; import {
OverlayAddNewItemMessage,
SubFrameOffsetData,
} from "../../background/abstractions/overlay.background";
import { AutofillExtensionMessageParam } from "../../content/abstractions/autofill-init"; import { AutofillExtensionMessageParam } from "../../content/abstractions/autofill-init";
import { AutofillOverlayElementType } from "../../enums/autofill-overlay.enum";
import AutofillField from "../../models/autofill-field"; import AutofillField from "../../models/autofill-field";
import AutofillPageDetails from "../../models/autofill-page-details"; import AutofillPageDetails from "../../models/autofill-page-details";
import { ElementWithOpId, FormFieldElement } from "../../types"; import { ElementWithOpId, FormFieldElement } from "../../types";
@@ -34,6 +38,11 @@ export type AutofillOverlayContentExtensionMessageHandlers = {
destroyAutofillInlineMenuListeners: () => void; destroyAutofillInlineMenuListeners: () => void;
}; };
export type AutofillOverlayContentExtensionMessage = {
command: string;
overlayElement?: AutofillOverlayElementType;
} & OverlayAddNewItemMessage;
export interface AutofillOverlayContentService { export interface AutofillOverlayContentService {
pageDetailsUpdateRequired: boolean; pageDetailsUpdateRequired: boolean;
messageHandlers: AutofillOverlayContentExtensionMessageHandlers; messageHandlers: AutofillOverlayContentExtensionMessageHandlers;

View File

@@ -31,6 +31,7 @@ import {
} from "../utils"; } from "../utils";
import { import {
AutofillOverlayContentExtensionMessage,
AutofillOverlayContentExtensionMessageHandlers, AutofillOverlayContentExtensionMessageHandlers,
AutofillOverlayContentService as AutofillOverlayContentServiceInterface, AutofillOverlayContentService as AutofillOverlayContentServiceInterface,
OpenAutofillInlineMenuOptions, OpenAutofillInlineMenuOptions,
@@ -233,7 +234,7 @@ export class AutofillOverlayContentService implements AutofillOverlayContentServ
hostname: globalThis.document.location.hostname, hostname: globalThis.document.location.hostname,
}; };
void this.sendExtensionMessage("autofillOverlayAddNewVaultItem", { login }); this.sendPortMessage("autofillOverlayAddNewVaultItem", { login });
} }
/** /**
@@ -1121,6 +1122,19 @@ export class AutofillOverlayContentService implements AutofillOverlayContentServ
); );
} }
/**
* Sends a message through the port to the background script.
*
* @param command - The command to send through the port.
* @param message - The message to send through the port.
*/
private sendPortMessage(
command: string,
message: Omit<AutofillOverlayContentExtensionMessage, "command">,
) {
this.port.postMessage({ command, ...message });
}
/** /**
* Clears the user interaction event timeout. This is used to ensure that * Clears the user interaction event timeout. This is used to ensure that
* the overlay is not repositioned while the user is interacting with it. * the overlay is not repositioned while the user is interacting with it.