From 483a5f27e7f69f9a0d8b92cef1be17c5c139c986 Mon Sep 17 00:00:00 2001 From: Andreas Coroiu Date: Tue, 17 Dec 2024 15:19:12 +0100 Subject: [PATCH] wip: ping command from web --- apps/browser/src/manifest.json | 2 +- apps/browser/src/manifest.v3.json | 2 +- .../src/app/platform/ipc/web-ipc.service.ts | 20 ++++++++++++++++++- libs/common/src/platform/ipc/index.ts | 1 + libs/common/src/platform/ipc/ping.message.ts | 2 ++ 5 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 libs/common/src/platform/ipc/ping.message.ts diff --git a/apps/browser/src/manifest.json b/apps/browser/src/manifest.json index 58824c6d4f..6a5b296ffb 100644 --- a/apps/browser/src/manifest.json +++ b/apps/browser/src/manifest.json @@ -39,7 +39,7 @@ { "all_frames": false, "js": ["content/ipc-content-script.js"], - "matches": ["https://vault.bitwarden.com/*"], + "matches": ["https://vault.bitwarden.com/*", "https://localhost:4200/*"], "run_at": "document_start" } ], diff --git a/apps/browser/src/manifest.v3.json b/apps/browser/src/manifest.v3.json index b080343764..b89e99cc8a 100644 --- a/apps/browser/src/manifest.v3.json +++ b/apps/browser/src/manifest.v3.json @@ -40,7 +40,7 @@ { "all_frames": false, "js": ["content/ipc-content-script.js"], - "matches": ["https://vault.bitwarden.com/*"], + "matches": ["https://vault.bitwarden.com/*", "https://localhost:4200/*"], "run_at": "document_start" } ], diff --git a/apps/web/src/app/platform/ipc/web-ipc.service.ts b/apps/web/src/app/platform/ipc/web-ipc.service.ts index 9204697983..88bc8fd9c7 100644 --- a/apps/web/src/app/platform/ipc/web-ipc.service.ts +++ b/apps/web/src/app/platform/ipc/web-ipc.service.ts @@ -1,6 +1,12 @@ import { Observable } from "rxjs"; -import { IpcLink, IpcService, isIpcMessage } from "@bitwarden/common/platform/ipc"; +import { + IpcLink, + IpcService, + isIpcMessage, + PingMessagePayload, + PongMessagePayload, +} from "@bitwarden/common/platform/ipc"; export class WebIpcService extends IpcService { private static LinkToExtensionBackground = new IpcLink( @@ -25,5 +31,17 @@ export class WebIpcService extends IpcService { await super.init(); this.manager.register_link(WebIpcService.LinkToExtensionBackground); + + void this.ping(); + } + + async ping() { + await this.manager.send("BrowserBackground", PingMessagePayload); + const message = await this.manager.receive("BrowserBackground"); + + if (message[0] === PongMessagePayload[0]) { + // eslint-disable-next-line no-console + console.log("Connected to extension background"); + } } } diff --git a/libs/common/src/platform/ipc/index.ts b/libs/common/src/platform/ipc/index.ts index 5c42d33bad..5126e9c314 100644 --- a/libs/common/src/platform/ipc/index.ts +++ b/libs/common/src/platform/ipc/index.ts @@ -1,3 +1,4 @@ export * from "./ipc-link"; export * from "./ipc-message"; export * from "./ipc.service"; +export * from "./ping.message"; diff --git a/libs/common/src/platform/ipc/ping.message.ts b/libs/common/src/platform/ipc/ping.message.ts new file mode 100644 index 0000000000..5be064628a --- /dev/null +++ b/libs/common/src/platform/ipc/ping.message.ts @@ -0,0 +1,2 @@ +export const PingMessagePayload = new Uint8Array([9001]); +export const PongMessagePayload = new Uint8Array([9002]);