1
0
mirror of https://github.com/bitwarden/browser synced 2026-02-09 13:10:17 +00:00

feat: implement pong service

This commit is contained in:
Andreas Coroiu
2025-04-11 12:15:39 +02:00
parent 0cabdd539c
commit f39f96dc31
2 changed files with 28 additions and 0 deletions

View File

@@ -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<void>((resolve) => {
setTimeout(async () => {

View File

@@ -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,
});
}
});
}
}