1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-15 15:53:27 +00:00

[PM-18044] Ensure all calls to receive should get all messages broadcast (#13869)

* feat: make compatible with SDK changes

* feat: use subscription

* feat: update SDK

* fix: lint

* fix: ts strict issues
This commit is contained in:
Andreas Coroiu
2025-05-05 18:19:41 +02:00
committed by GitHub
parent 60bafc1311
commit e0cabd1df0
8 changed files with 106 additions and 113 deletions

View File

@@ -1,47 +0,0 @@
import { Injectable } from "@angular/core";
import { IpcMessage, isIpcMessage } from "@bitwarden/common/platform/ipc";
import { MessageQueue } from "@bitwarden/common/platform/ipc/message-queue";
import { CommunicationBackend, IncomingMessage, OutgoingMessage } from "@bitwarden/sdk-internal";
@Injectable({ providedIn: "root" })
export class WebCommunicationProvider implements CommunicationBackend {
private queue = new MessageQueue<IncomingMessage>();
constructor() {
window.addEventListener("message", async (event: MessageEvent) => {
if (event.origin !== window.origin) {
return;
}
const message = event.data;
if (!isIpcMessage(message)) {
return;
}
void this.queue.enqueue(
new IncomingMessage(
message.message.payload,
message.message.destination,
"BrowserBackground",
),
);
});
}
async send(message: OutgoingMessage): Promise<void> {
if (message.destination === "BrowserBackground") {
window.postMessage(
{ type: "bitwarden-ipc-message", message } satisfies IpcMessage,
window.location.origin,
);
return;
}
throw new Error(`Destination not supported: ${message.destination}`);
}
receive(): Promise<IncomingMessage> {
return this.queue.dequeue();
}
}

View File

@@ -2,22 +2,65 @@ import { inject } from "@angular/core";
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
import { SdkLoadService } from "@bitwarden/common/platform/abstractions/sdk/sdk-load.service";
import { IpcService } from "@bitwarden/common/platform/ipc";
import { IpcClient } from "@bitwarden/sdk-internal";
import { WebCommunicationProvider } from "./web-communication-provider";
import { IpcMessage, IpcService, isIpcMessage } from "@bitwarden/common/platform/ipc";
import {
IncomingMessage,
IpcClient,
IpcCommunicationBackend,
OutgoingMessage,
} from "@bitwarden/sdk-internal";
export class WebIpcService extends IpcService {
private logService = inject(LogService);
private communicationProvider?: WebCommunicationProvider;
private communicationBackend?: IpcCommunicationBackend;
override async init() {
try {
// This function uses classes and functions defined in the SDK, so we need to wait for the SDK to load.
await SdkLoadService.Ready;
this.communicationProvider = new WebCommunicationProvider();
await super.initWithClient(new IpcClient(this.communicationProvider));
this.communicationBackend = new IpcCommunicationBackend({
async send(message: OutgoingMessage): Promise<void> {
if (message.destination === "BrowserBackground") {
window.postMessage(
{
type: "bitwarden-ipc-message",
message: {
destination: message.destination,
payload: message.payload,
topic: message.topic,
},
} satisfies IpcMessage,
window.location.origin,
);
return;
}
throw new Error(`Destination not supported: ${message.destination}`);
},
});
window.addEventListener("message", async (event: MessageEvent) => {
if (event.origin !== window.origin) {
return;
}
const message = event.data;
if (!isIpcMessage(message)) {
return;
}
this.communicationBackend?.deliver_message(
new IncomingMessage(
message.message.payload,
message.message.destination,
"BrowserBackground",
message.message.topic,
),
);
});
await super.initWithClient(new IpcClient(this.communicationBackend));
} catch (e) {
this.logService.error("[IPC] Initialization failed", e);
}