mirror of
https://github.com/bitwarden/browser.git
synced 2024-09-27 04:03:00 +02:00
[PS-2303] MV3 Service Cache Enhancements (#4507)
* Add Should Listen Gate * Fixed ServiceCache Being Sent to Wrong Arg * Await Combined Listener
This commit is contained in:
parent
c5e99b807e
commit
9a98e8eb70
@ -21,7 +21,12 @@ if (BrowserApi.manifestVersion === 3) {
|
|||||||
chrome.tabs.onReplaced.addListener(tabsOnReplacedListener);
|
chrome.tabs.onReplaced.addListener(tabsOnReplacedListener);
|
||||||
chrome.tabs.onUpdated.addListener(tabsOnUpdatedListener);
|
chrome.tabs.onUpdated.addListener(tabsOnUpdatedListener);
|
||||||
chrome.contextMenus.onClicked.addListener(contextMenusClickedListener);
|
chrome.contextMenus.onClicked.addListener(contextMenusClickedListener);
|
||||||
BrowserApi.messageListener("runtime.background", runtimeMessageListener);
|
BrowserApi.messageListener(
|
||||||
|
"runtime.background",
|
||||||
|
(message: { command: string }, sender, sendResponse) => {
|
||||||
|
runtimeMessageListener(message, sender);
|
||||||
|
}
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
const bitwardenMain = ((window as any).bitwardenMain = new MainBackground());
|
const bitwardenMain = ((window as any).bitwardenMain = new MainBackground());
|
||||||
bitwardenMain.bootstrap().then(() => {
|
bitwardenMain.bootstrap().then(() => {
|
||||||
|
@ -120,13 +120,24 @@ export class CipherContextMenuHandler {
|
|||||||
await cipherContextMenuHandler.update(tab.url);
|
await cipherContextMenuHandler.update(tab.url);
|
||||||
}
|
}
|
||||||
|
|
||||||
static async messageListener(message: { command: string }, cachedServices: CachedServices) {
|
static async messageListener(
|
||||||
|
message: { command: string },
|
||||||
|
sender: chrome.runtime.MessageSender,
|
||||||
|
cachedServices: CachedServices
|
||||||
|
) {
|
||||||
|
if (!CipherContextMenuHandler.shouldListen(message)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
const cipherContextMenuHandler = await CipherContextMenuHandler.create(cachedServices);
|
const cipherContextMenuHandler = await CipherContextMenuHandler.create(cachedServices);
|
||||||
await cipherContextMenuHandler.messageListener(message);
|
await cipherContextMenuHandler.messageListener(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
async messageListener(message: { command: string }) {
|
private static shouldListen(message: { command: string }) {
|
||||||
if (!LISTENED_TO_COMMANDS.includes(message.command)) {
|
return LISTENED_TO_COMMANDS.includes(message.command);
|
||||||
|
}
|
||||||
|
|
||||||
|
async messageListener(message: { command: string }, sender?: chrome.runtime.MessageSender) {
|
||||||
|
if (!CipherContextMenuHandler.shouldListen(message)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -126,6 +126,7 @@ export class ContextMenuClickedHandler {
|
|||||||
|
|
||||||
static async messageListener(
|
static async messageListener(
|
||||||
message: { command: string; data: LockedVaultPendingNotificationsItem },
|
message: { command: string; data: LockedVaultPendingNotificationsItem },
|
||||||
|
sender: chrome.runtime.MessageSender,
|
||||||
cachedServices: CachedServices
|
cachedServices: CachedServices
|
||||||
) {
|
) {
|
||||||
if (
|
if (
|
||||||
|
@ -1,22 +1,24 @@
|
|||||||
import { combine } from "./combine";
|
import { combine } from "./combine";
|
||||||
|
|
||||||
describe("combine", () => {
|
describe("combine", () => {
|
||||||
it("runs", () => {
|
it("runs", async () => {
|
||||||
const combined = combine([
|
const combined = combine([
|
||||||
(arg: Record<string, unknown>, serviceCache: Record<string, unknown>) => {
|
(arg: Record<string, unknown>, serviceCache: Record<string, unknown>) => {
|
||||||
arg["one"] = true;
|
arg["one"] = true;
|
||||||
serviceCache["one"] = true;
|
serviceCache["one"] = true;
|
||||||
|
return Promise.resolve();
|
||||||
},
|
},
|
||||||
(arg: Record<string, unknown>, serviceCache: Record<string, unknown>) => {
|
(arg: Record<string, unknown>, serviceCache: Record<string, unknown>) => {
|
||||||
if (serviceCache["one"] !== true) {
|
if (serviceCache["one"] !== true) {
|
||||||
throw new Error("One should have ran.");
|
throw new Error("One should have ran.");
|
||||||
}
|
}
|
||||||
arg["two"] = true;
|
arg["two"] = true;
|
||||||
|
return Promise.resolve();
|
||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const arg: Record<string, unknown> = {};
|
const arg: Record<string, unknown> = {};
|
||||||
combined(arg);
|
await combined(arg);
|
||||||
|
|
||||||
expect(arg["one"]).toBeTruthy();
|
expect(arg["one"]).toBeTruthy();
|
||||||
|
|
||||||
|
@ -1,15 +1,15 @@
|
|||||||
import { CachedServices } from "../background/service_factories/factory-options";
|
import { CachedServices } from "../background/service_factories/factory-options";
|
||||||
|
|
||||||
type Listener<T extends unknown[]> = (...args: [...T, CachedServices]) => void;
|
type Listener<T extends unknown[]> = (...args: [...T, CachedServices]) => Promise<void>;
|
||||||
|
|
||||||
export const combine = <T extends unknown[]>(
|
export const combine = <T extends unknown[]>(
|
||||||
listeners: Listener<T>[],
|
listeners: Listener<T>[],
|
||||||
startingServices: CachedServices = {}
|
startingServices: CachedServices = {}
|
||||||
) => {
|
) => {
|
||||||
return (...args: T) => {
|
return async (...args: T) => {
|
||||||
const cachedServices = { ...startingServices };
|
const cachedServices = { ...startingServices };
|
||||||
for (const listener of listeners) {
|
for (const listener of listeners) {
|
||||||
listener(...[...args, cachedServices]);
|
await listener(...[...args, cachedServices]);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
@ -23,7 +23,10 @@ const tabsOnUpdatedListener = combine([
|
|||||||
|
|
||||||
const contextMenusClickedListener = ContextMenuClickedHandler.onClickedListener;
|
const contextMenusClickedListener = ContextMenuClickedHandler.onClickedListener;
|
||||||
|
|
||||||
const runtimeMessageListener = combine([
|
// TODO: All message listeners should be RuntimeMessage in Notifications follow up then this type annotation can be inferred
|
||||||
|
const runtimeMessageListener = combine<
|
||||||
|
[message: { command: string }, sender: chrome.runtime.MessageSender]
|
||||||
|
>([
|
||||||
UpdateBadge.messageListener,
|
UpdateBadge.messageListener,
|
||||||
CipherContextMenuHandler.messageListener,
|
CipherContextMenuHandler.messageListener,
|
||||||
ContextMenuClickedHandler.messageListener,
|
ContextMenuClickedHandler.messageListener,
|
||||||
|
Loading…
Reference in New Issue
Block a user