diff --git a/apps/browser/src/platform/ipc/ipc-foreground.service.ts b/apps/browser/src/platform/ipc/ipc-foreground.service.ts index 3c091f5a01..35de0d3c2b 100644 --- a/apps/browser/src/platform/ipc/ipc-foreground.service.ts +++ b/apps/browser/src/platform/ipc/ipc-foreground.service.ts @@ -1,17 +1,10 @@ -import { Injectable, OnDestroy } from "@angular/core"; -import { Observable, Subject, switchMap, takeUntil } from "rxjs"; +import { Observable } from "rxjs"; -import { SdkService } from "@bitwarden/common/platform/abstractions/sdk/sdk.service"; -import { Destination } from "@bitwarden/sdk-internal"; +import { IpcLink, IpcMessage, IpcService } from "@bitwarden/platform"; +import { Destination, Manager } from "@bitwarden/sdk-internal"; -import { IpcLink } from "./ipc-link"; -import { IpcMessage } from "./ipc-message"; - -@Injectable() -export class IpcForegroundService implements OnDestroy { - private destroy$ = new Subject(); - - private linkToBackground = new IpcLink( +export class IpcForegroundService extends IpcService { + private static LinkToBackground = new IpcLink( async (data) => { await chrome.runtime.sendMessage({ payload: data } as IpcMessage); }, @@ -26,22 +19,7 @@ export class IpcForegroundService implements OnDestroy { [Destination.BrowserBackground], ); - constructor(private sdkService: SdkService) {} - - init() { - this.sdkService.client$ - .pipe( - switchMap(async (client) => { - const manager = client.ipc().create_manager(); - await manager.register_link(this.linkToBackground); - }), - takeUntil(this.destroy$), - ) - .subscribe(); - } - - ngOnDestroy() { - this.destroy$.next(); - this.destroy$.complete(); + protected override async registerLinks(manager: Manager): Promise { + await manager.register_link(IpcForegroundService.LinkToBackground); } } diff --git a/libs/platform/src/index.ts b/libs/platform/src/index.ts index f11ec10284..0fe353d86c 100644 --- a/libs/platform/src/index.ts +++ b/libs/platform/src/index.ts @@ -1 +1,2 @@ export * from "./services/browser-service"; +export * from "./ipc"; diff --git a/libs/platform/src/ipc/index.ts b/libs/platform/src/ipc/index.ts new file mode 100644 index 0000000000..5c42d33bad --- /dev/null +++ b/libs/platform/src/ipc/index.ts @@ -0,0 +1,3 @@ +export * from "./ipc-link"; +export * from "./ipc-message"; +export * from "./ipc.service"; diff --git a/apps/browser/src/platform/ipc/ipc-link.ts b/libs/platform/src/ipc/ipc-link.ts similarity index 100% rename from apps/browser/src/platform/ipc/ipc-link.ts rename to libs/platform/src/ipc/ipc-link.ts diff --git a/apps/browser/src/platform/ipc/ipc-message.ts b/libs/platform/src/ipc/ipc-message.ts similarity index 100% rename from apps/browser/src/platform/ipc/ipc-message.ts rename to libs/platform/src/ipc/ipc-message.ts diff --git a/libs/platform/src/ipc/ipc.service.ts b/libs/platform/src/ipc/ipc.service.ts new file mode 100644 index 0000000000..5f07d16c15 --- /dev/null +++ b/libs/platform/src/ipc/ipc.service.ts @@ -0,0 +1,11 @@ +import { Manager } from "@bitwarden/sdk-internal"; + +export abstract class IpcService { + protected manager = new Manager(); + + async init() { + await this.registerLinks(this.manager); + } + + protected abstract registerLinks(manager: Manager): Promise; +}