1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-17 16:53:34 +00:00

[PM-8027] Cleaning up implementation details

This commit is contained in:
Cesar Gonzalez
2024-05-31 15:06:46 -05:00
parent f3d41f91aa
commit ff96fc1773
4 changed files with 15 additions and 21 deletions

View File

@@ -98,7 +98,6 @@ class AutofillInit implements AutofillInitInterface {
): Promise<AutofillPageDetails | void> { ): Promise<AutofillPageDetails | void> {
const pageDetails: AutofillPageDetails = const pageDetails: AutofillPageDetails =
await this.collectAutofillContentService.getPageDetails(); await this.collectAutofillContentService.getPageDetails();
if (sendDetailsInResponse) { if (sendDetailsInResponse) {
return pageDetails; return pageDetails;
} }

View File

@@ -15,8 +15,6 @@ type UpdateAutofillDataAttributeParams = {
}; };
interface CollectAutofillContentService { interface CollectAutofillContentService {
autofillFormElements: AutofillFormElements;
autofillFieldElements: AutofillFieldElements;
getPageDetails(): Promise<AutofillPageDetails>; getPageDetails(): Promise<AutofillPageDetails>;
getAutofillFieldElementByOpid(opid: string): HTMLElement | null; getAutofillFieldElementByOpid(opid: string): HTMLElement | null;
deepQueryElements<T>( deepQueryElements<T>(

View File

@@ -72,17 +72,14 @@ describe("CollectAutofillContentService", () => {
it("returns an object with empty forms and fields if no fields were found on a previous iteration", async () => { it("returns an object with empty forms and fields if no fields were found on a previous iteration", async () => {
collectAutofillContentService["domRecentlyMutated"] = false; collectAutofillContentService["domRecentlyMutated"] = false;
collectAutofillContentService["noFieldsFound"] = true; collectAutofillContentService["noFieldsFound"] = true;
jest.spyOn(collectAutofillContentService as any, "buildFormattedPageDetails"); jest.spyOn(collectAutofillContentService as any, "getFormattedPageDetails");
jest.spyOn(collectAutofillContentService as any, "queryAutofillFormAndFieldElements"); jest.spyOn(collectAutofillContentService as any, "queryAutofillFormAndFieldElements");
jest.spyOn(collectAutofillContentService as any, "buildAutofillFormsData"); jest.spyOn(collectAutofillContentService as any, "buildAutofillFormsData");
jest.spyOn(collectAutofillContentService as any, "buildAutofillFieldsData"); jest.spyOn(collectAutofillContentService as any, "buildAutofillFieldsData");
await collectAutofillContentService.getPageDetails(); await collectAutofillContentService.getPageDetails();
expect(collectAutofillContentService["buildFormattedPageDetails"]).toHaveBeenCalledWith( expect(collectAutofillContentService["getFormattedPageDetails"]).toHaveBeenCalledWith({}, []);
{},
[],
);
expect( expect(
collectAutofillContentService["queryAutofillFormAndFieldElements"], collectAutofillContentService["queryAutofillFormAndFieldElements"],
).not.toHaveBeenCalled(); ).not.toHaveBeenCalled();
@@ -159,7 +156,7 @@ describe("CollectAutofillContentService", () => {
collectAutofillContentService["autofillFieldElements"] = new Map([ collectAutofillContentService["autofillFieldElements"] = new Map([
[fieldElement, autofillField], [fieldElement, autofillField],
]); ]);
jest.spyOn(collectAutofillContentService as any, "buildFormattedPageDetails"); jest.spyOn(collectAutofillContentService as any, "getFormattedPageDetails");
jest.spyOn(collectAutofillContentService as any, "getFormattedAutofillFormsData"); jest.spyOn(collectAutofillContentService as any, "getFormattedAutofillFormsData");
jest.spyOn(collectAutofillContentService as any, "getFormattedAutofillFieldsData"); jest.spyOn(collectAutofillContentService as any, "getFormattedAutofillFieldsData");
jest.spyOn(collectAutofillContentService as any, "queryAutofillFormAndFieldElements"); jest.spyOn(collectAutofillContentService as any, "queryAutofillFormAndFieldElements");
@@ -168,7 +165,7 @@ describe("CollectAutofillContentService", () => {
await collectAutofillContentService.getPageDetails(); await collectAutofillContentService.getPageDetails();
expect(collectAutofillContentService["buildFormattedPageDetails"]).toHaveBeenCalled(); expect(collectAutofillContentService["getFormattedPageDetails"]).toHaveBeenCalled();
expect(collectAutofillContentService["getFormattedAutofillFormsData"]).toHaveBeenCalled(); expect(collectAutofillContentService["getFormattedAutofillFormsData"]).toHaveBeenCalled();
expect(collectAutofillContentService["getFormattedAutofillFieldsData"]).toHaveBeenCalled(); expect(collectAutofillContentService["getFormattedAutofillFieldsData"]).toHaveBeenCalled();
expect( expect(

View File

@@ -37,15 +37,14 @@ class CollectAutofillContentService implements CollectAutofillContentServiceInte
private readonly autofillOverlayContentService: AutofillOverlayContentService; private readonly autofillOverlayContentService: AutofillOverlayContentService;
private noFieldsFound = false; private noFieldsFound = false;
private domRecentlyMutated = true; private domRecentlyMutated = true;
autofillFormElements: AutofillFormElements = new Map(); private autofillFormElements: AutofillFormElements = new Map();
autofillFieldElements: AutofillFieldElements = new Map(); private autofillFieldElements: AutofillFieldElements = new Map();
private currentLocationHref = ""; private currentLocationHref = "";
private intersectionObserver: IntersectionObserver; private intersectionObserver: IntersectionObserver;
private elementInitializingIntersectionObserver: Set<Element> = new Set(); private elementInitializingIntersectionObserver: Set<Element> = new Set();
private mutationObserver: MutationObserver; private mutationObserver: MutationObserver;
private updateAutofillElementsAfterMutationTimeout: number | NodeJS.Timeout; private updateAutofillElementsAfterMutationTimeout: number | NodeJS.Timeout;
private mutationsQueue: MutationRecord[][] = []; private mutationsQueue: MutationRecord[][] = [];
private cachedAutofillPageDetails: AutofillPageDetails;
private readonly updateAfterMutationTimeoutDelay = 1000; private readonly updateAfterMutationTimeoutDelay = 1000;
private readonly formFieldQueryString; private readonly formFieldQueryString;
private readonly nonInputFormFieldTags = new Set(["textarea", "select"]); private readonly nonInputFormFieldTags = new Set(["textarea", "select"]);
@@ -95,13 +94,13 @@ class CollectAutofillContentService implements CollectAutofillContentServiceInte
} }
if (!this.domRecentlyMutated && this.noFieldsFound) { if (!this.domRecentlyMutated && this.noFieldsFound) {
return this.buildFormattedPageDetails({}, []); return this.getFormattedPageDetails({}, []);
} }
if (!this.domRecentlyMutated && this.autofillFieldElements.size) { if (!this.domRecentlyMutated && this.autofillFieldElements.size) {
this.updateCachedAutofillFieldVisibility(); this.updateCachedAutofillFieldVisibility();
return this.buildFormattedPageDetails( return this.getFormattedPageDetails(
this.getFormattedAutofillFormsData(), this.getFormattedAutofillFormsData(),
this.getFormattedAutofillFieldsData(), this.getFormattedAutofillFieldsData(),
); );
@@ -120,7 +119,7 @@ class CollectAutofillContentService implements CollectAutofillContentServiceInte
} }
this.domRecentlyMutated = false; this.domRecentlyMutated = false;
const pageDetails = this.buildFormattedPageDetails(autofillFormsData, autofillFieldsData); const pageDetails = this.getFormattedPageDetails(autofillFormsData, autofillFieldsData);
this.setupInlineMenuListeners(pageDetails); this.setupInlineMenuListeners(pageDetails);
return pageDetails; return pageDetails;
@@ -274,11 +273,11 @@ class CollectAutofillContentService implements CollectAutofillContentServiceInte
* @param autofillFormsData - The data for all the forms found in the page * @param autofillFormsData - The data for all the forms found in the page
* @param autofillFieldsData - The data for all the fields found in the page * @param autofillFieldsData - The data for all the fields found in the page
*/ */
private buildFormattedPageDetails( private getFormattedPageDetails(
autofillFormsData: Record<string, AutofillForm>, autofillFormsData: Record<string, AutofillForm>,
autofillFieldsData: AutofillField[], autofillFieldsData: AutofillField[],
): AutofillPageDetails { ): AutofillPageDetails {
this.cachedAutofillPageDetails = { return {
title: document.title, title: document.title,
url: (document.defaultView || globalThis).location.href, url: (document.defaultView || globalThis).location.href,
documentUrl: document.location.href, documentUrl: document.location.href,
@@ -286,8 +285,6 @@ class CollectAutofillContentService implements CollectAutofillContentServiceInte
fields: autofillFieldsData, fields: autofillFieldsData,
collectedTimestamp: Date.now(), collectedTimestamp: Date.now(),
}; };
return this.cachedAutofillPageDetails;
} }
/** /**
@@ -1448,7 +1445,10 @@ class CollectAutofillContentService implements CollectAutofillContentServiceInte
void this.autofillOverlayContentService?.setupAutofillOverlayListenerOnField( void this.autofillOverlayContentService?.setupAutofillOverlayListenerOnField(
formFieldElement, formFieldElement,
cachedAutofillFieldElement, cachedAutofillFieldElement,
this.cachedAutofillPageDetails, this.getFormattedPageDetails(
this.getFormattedAutofillFormsData(),
this.getFormattedAutofillFieldsData(),
),
); );
this.intersectionObserver?.unobserve(entry.target); this.intersectionObserver?.unobserve(entry.target);