From 3abc28aeb11deea84dd91f2fcc621f1a54f80867 Mon Sep 17 00:00:00 2001 From: Andreas Coroiu Date: Tue, 17 Dec 2024 10:22:14 +0100 Subject: [PATCH] refactor: create abstract ipc service in libs --- .../platform/ipc/ipc-foreground.service.ts | 36 ++++--------------- libs/platform/src/index.ts | 1 + libs/platform/src/ipc/index.ts | 3 ++ .../platform/src}/ipc/ipc-link.ts | 0 .../platform/src}/ipc/ipc-message.ts | 0 libs/platform/src/ipc/ipc.service.ts | 11 ++++++ 6 files changed, 22 insertions(+), 29 deletions(-) create mode 100644 libs/platform/src/ipc/index.ts rename {apps/browser/src/platform => libs/platform/src}/ipc/ipc-link.ts (100%) rename {apps/browser/src/platform => libs/platform/src}/ipc/ipc-message.ts (100%) create mode 100644 libs/platform/src/ipc/ipc.service.ts 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; +}