1
0
mirror of https://github.com/bitwarden/browser.git synced 2025-02-02 23:11:40 +01:00

[PM-5189] Refactoring and organizing implementation

This commit is contained in:
Cesar Gonzalez 2024-04-07 19:07:01 -05:00
parent 2e22993159
commit 3313807acb
No known key found for this signature in database
GPG Key ID: 3381A5457F8CCECF
2 changed files with 77 additions and 63 deletions

View File

@ -106,10 +106,10 @@ type OverlayBackgroundExtensionMessageHandlers = {
unlockCompleted: ({ message }: BackgroundMessageParam) => void; unlockCompleted: ({ message }: BackgroundMessageParam) => void;
addEditCipherSubmitted: () => void; addEditCipherSubmitted: () => void;
deletedCipher: () => void; deletedCipher: () => void;
checkIsFieldCurrentlyFocused: () => boolean;
checkIsFieldCurrentlyFilling: () => boolean;
updateIsFieldCurrentlyFocused: ({ message }: BackgroundMessageParam) => void; updateIsFieldCurrentlyFocused: ({ message }: BackgroundMessageParam) => void;
checkIsFieldCurrentlyFocused: () => boolean;
updateIsFieldCurrentlyFilling: ({ message }: BackgroundMessageParam) => void; updateIsFieldCurrentlyFilling: ({ message }: BackgroundMessageParam) => void;
checkIsFieldCurrentlyFilling: () => boolean;
checkIsInlineMenuButtonVisible: ({ sender }: BackgroundSenderParam) => void; checkIsInlineMenuButtonVisible: ({ sender }: BackgroundSenderParam) => void;
checkIsInlineMenuListVisible: ({ sender }: BackgroundSenderParam) => void; checkIsInlineMenuListVisible: ({ sender }: BackgroundSenderParam) => void;
checkIsInlineMenuCiphersPopulated: ({ sender }: BackgroundSenderParam) => void; checkIsInlineMenuCiphersPopulated: ({ sender }: BackgroundSenderParam) => void;

View File

@ -77,12 +77,10 @@ class OverlayBackground implements OverlayBackgroundInterface {
unlockCompleted: ({ message }) => this.unlockCompleted(message), unlockCompleted: ({ message }) => this.unlockCompleted(message),
addEditCipherSubmitted: () => this.updateOverlayCiphers(), addEditCipherSubmitted: () => this.updateOverlayCiphers(),
deletedCipher: () => this.updateOverlayCiphers(), deletedCipher: () => this.updateOverlayCiphers(),
checkIsFieldCurrentlyFocused: () => this.isFieldCurrentlyFocused, updateIsFieldCurrentlyFocused: ({ message }) => this.updateIsFieldCurrentlyFocused(message),
checkIsFieldCurrentlyFilling: () => this.isCurrentlyFilling, checkIsFieldCurrentlyFocused: () => this.checkIsFieldCurrentlyFocused(),
updateIsFieldCurrentlyFocused: ({ message }) => updateIsFieldCurrentlyFilling: ({ message }) => this.updateIsFieldCurrentlyFilling(message),
(this.isFieldCurrentlyFocused = message.isFieldCurrentlyFocused), checkIsFieldCurrentlyFilling: () => this.checkIsFieldCurrentlyFilling(),
updateIsFieldCurrentlyFilling: ({ message }) =>
(this.isCurrentlyFilling = message.isFieldCurrentlyFilling),
checkIsInlineMenuButtonVisible: ({ sender }) => this.checkIsInlineMenuButtonVisible(sender), checkIsInlineMenuButtonVisible: ({ sender }) => this.checkIsInlineMenuButtonVisible(sender),
checkIsInlineMenuListVisible: ({ sender }) => this.checkIsInlineMenuListVisible(sender), checkIsInlineMenuListVisible: ({ sender }) => this.checkIsInlineMenuListVisible(sender),
checkIsInlineMenuCiphersPopulated: ({ sender }) => checkIsInlineMenuCiphersPopulated: ({ sender }) =>
@ -123,33 +121,6 @@ class OverlayBackground implements OverlayBackgroundInterface {
private themeStateService: ThemeStateService, private themeStateService: ThemeStateService,
) {} ) {}
private async checkIsInlineMenuButtonVisible(sender: chrome.runtime.MessageSender) {
return await BrowserApi.tabSendMessage(
sender.tab,
{ command: "checkIsInlineMenuButtonVisible" },
{ frameId: 0 },
);
}
private async checkIsInlineMenuListVisible(sender: chrome.runtime.MessageSender) {
return await BrowserApi.tabSendMessage(
sender.tab,
{ command: "checkIsInlineMenuListVisible" },
{ frameId: 0 },
);
}
private checkIsInlineMenuCiphersPopulated(sender: chrome.runtime.MessageSender) {
return sender.tab.id === this.focusedFieldData.tabId && this.overlayLoginCiphers.size > 0;
}
updateSubFrameData(message: any, sender: chrome.runtime.MessageSender) {
const subFrameOffsetsForTab = this.subFrameOffsetsForTab[sender.tab.id];
if (subFrameOffsetsForTab) {
subFrameOffsetsForTab.set(message.subFrameData.frameId, message.subFrameData);
}
}
/** /**
* Removes cached page details for a tab * Removes cached page details for a tab
* based on the passed tabId. * based on the passed tabId.
@ -271,36 +242,11 @@ class OverlayBackground implements OverlayBackgroundInterface {
pageDetailsMap.set(sender.frameId, pageDetails); pageDetailsMap.set(sender.frameId, pageDetails);
} }
private async rebuildSubFrameOffsets(sender: chrome.runtime.MessageSender) { private updateSubFrameData(message: any, sender: chrome.runtime.MessageSender) {
if (sender.frameId === this.focusedFieldData?.frameId) {
return;
}
const subFrameOffsetsForTab = this.subFrameOffsetsForTab[sender.tab.id]; const subFrameOffsetsForTab = this.subFrameOffsetsForTab[sender.tab.id];
if (!subFrameOffsetsForTab) { if (subFrameOffsetsForTab) {
return; subFrameOffsetsForTab.set(message.subFrameData.frameId, message.subFrameData);
} }
if (this.rebuildSubFrameOffsetsTimeout) {
clearTimeout(this.rebuildSubFrameOffsetsTimeout as number);
}
const frameTabs = Array.from(subFrameOffsetsForTab.keys());
for (const frameId of frameTabs) {
if (frameId === sender.frameId) {
continue;
}
subFrameOffsetsForTab.delete(frameId);
await this.buildSubFrameOffsets(sender.tab, frameId, sender.url);
}
this.rebuildSubFrameOffsetsTimeout = setTimeout(() => {
if (this.isFieldCurrentlyFocused) {
void this.updateOverlayPosition({ overlayElement: AutofillOverlayElement.List }, sender);
void this.updateOverlayPosition({ overlayElement: AutofillOverlayElement.Button }, sender);
}
}, 650);
} }
private async buildSubFrameOffsets(tab: chrome.tabs.Tab, frameId: number, url: string) { private async buildSubFrameOffsets(tab: chrome.tabs.Tab, frameId: number, url: string) {
@ -351,6 +297,38 @@ class OverlayBackground implements OverlayBackgroundInterface {
subFrameOffsetsForTab.set(frameId, subFrameData); subFrameOffsetsForTab.set(frameId, subFrameData);
} }
private async rebuildSubFrameOffsets(sender: chrome.runtime.MessageSender) {
if (sender.frameId === this.focusedFieldData?.frameId) {
return;
}
const subFrameOffsetsForTab = this.subFrameOffsetsForTab[sender.tab.id];
if (!subFrameOffsetsForTab) {
return;
}
if (this.rebuildSubFrameOffsetsTimeout) {
clearTimeout(this.rebuildSubFrameOffsetsTimeout as number);
}
const frameTabs = Array.from(subFrameOffsetsForTab.keys());
for (const frameId of frameTabs) {
if (frameId === sender.frameId) {
continue;
}
subFrameOffsetsForTab.delete(frameId);
await this.buildSubFrameOffsets(sender.tab, frameId, sender.url);
}
this.rebuildSubFrameOffsetsTimeout = setTimeout(() => {
if (this.isFieldCurrentlyFocused) {
void this.updateOverlayPosition({ overlayElement: AutofillOverlayElement.List }, sender);
void this.updateOverlayPosition({ overlayElement: AutofillOverlayElement.Button }, sender);
}
}, 650);
}
/** /**
* Triggers autofill for the selected cipher in the overlay list. Also places * Triggers autofill for the selected cipher in the overlay list. Also places
* the selected cipher at the top of the list of ciphers. * the selected cipher at the top of the list of ciphers.
@ -854,6 +832,42 @@ class OverlayBackground implements OverlayBackgroundInterface {
await BrowserApi.sendMessage("inlineAutofillMenuRefreshAddEditCipher"); await BrowserApi.sendMessage("inlineAutofillMenuRefreshAddEditCipher");
} }
private updateIsFieldCurrentlyFocused({ message }: OverlayBackgroundExtensionMessage) {
this.isFieldCurrentlyFocused = message.isFieldCurrentlyFocused;
}
private checkIsFieldCurrentlyFocused() {
return this.isFieldCurrentlyFocused;
}
private updateIsFieldCurrentlyFilling({ message }: OverlayBackgroundExtensionMessage) {
this.isCurrentlyFilling = message.isFieldCurrentlyFilling;
}
private checkIsFieldCurrentlyFilling() {
return this.isCurrentlyFilling;
}
private async checkIsInlineMenuButtonVisible(sender: chrome.runtime.MessageSender) {
return await BrowserApi.tabSendMessage(
sender.tab,
{ command: "checkIsInlineMenuButtonVisible" },
{ frameId: 0 },
);
}
private async checkIsInlineMenuListVisible(sender: chrome.runtime.MessageSender) {
return await BrowserApi.tabSendMessage(
sender.tab,
{ command: "checkIsInlineMenuListVisible" },
{ frameId: 0 },
);
}
private checkIsInlineMenuCiphersPopulated(sender: chrome.runtime.MessageSender) {
return sender.tab.id === this.focusedFieldData.tabId && this.overlayLoginCiphers.size > 0;
}
/** /**
* Sets up the extension message listeners for the overlay. * Sets up the extension message listeners for the overlay.
*/ */