diff --git a/apps/browser/src/background/main.background.ts b/apps/browser/src/background/main.background.ts index 3066ef5eef5..b1a62549496 100644 --- a/apps/browser/src/background/main.background.ts +++ b/apps/browser/src/background/main.background.ts @@ -265,6 +265,7 @@ import { BrowserApi } from "../platform/browser/browser-api"; import { flagEnabled } from "../platform/flags"; import { IpcBackgroundService } from "../platform/ipc/ipc-background.service"; import { IpcContentScriptManagerService } from "../platform/ipc/ipc-content-script-manager.service"; +import { IpcPongService } from "../platform/ipc/ipc-pong.service"; import { UpdateBadge } from "../platform/listeners/update-badge"; /* eslint-disable no-restricted-imports */ import { ChromeMessageSender } from "../platform/messaging/chrome-message.sender"; @@ -412,6 +413,7 @@ export default class MainBackground { ipcContentScriptManagerService: IpcContentScriptManagerService; ipcService: IpcService; + ipcPongService: IpcPongService; onUpdatedRan: boolean; onReplacedRan: boolean; @@ -1333,6 +1335,8 @@ export default class MainBackground { this.authService, this.logService, ); + + this.ipcPongService = new IpcPongService(this.ipcService); } async bootstrap() { @@ -1407,6 +1411,7 @@ export default class MainBackground { await this.initOverlayAndTabsBackground(); await this.ipcService.init(); + await this.ipcPongService.init(); return new Promise((resolve) => { setTimeout(async () => { diff --git a/apps/browser/src/platform/ipc/ipc-pong.service.ts b/apps/browser/src/platform/ipc/ipc-pong.service.ts new file mode 100644 index 00000000000..e6083e24788 --- /dev/null +++ b/apps/browser/src/platform/ipc/ipc-pong.service.ts @@ -0,0 +1,23 @@ +import { IpcService } from "@bitwarden/common/platform/ipc"; +import { Utils } from "@bitwarden/common/platform/misc/utils"; + +/** + * Example service that responds to "ping" messages with "pong". + * This is a simple example of how to use the IpcService to send and receive messages. + * It listens for incoming messages and sends a response back to the sender. + */ +export class IpcPongService { + constructor(private ipcService: IpcService) {} + + /** Must be initalized after IpcService */ + async init() { + this.ipcService.messages$.subscribe((message) => { + if (Utils.fromBufferToUtf8(new Uint8Array(message.data)) === "ping") { + void this.ipcService.send({ + data: Array.from(Utils.fromUtf8ToArray("pong")), + destination: message.source, + }); + } + }); + } +}