1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-12-18 15:47:57 +01:00

refactor: create abstract ipc service in libs

This commit is contained in:
Andreas Coroiu 2024-12-17 10:22:14 +01:00
parent 0ab9618422
commit 3abc28aeb1
No known key found for this signature in database
GPG Key ID: E70B5FFC81DFEC1A
6 changed files with 22 additions and 29 deletions

View File

@ -1,17 +1,10 @@
import { Injectable, OnDestroy } from "@angular/core"; import { Observable } from "rxjs";
import { Observable, Subject, switchMap, takeUntil } from "rxjs";
import { SdkService } from "@bitwarden/common/platform/abstractions/sdk/sdk.service"; import { IpcLink, IpcMessage, IpcService } from "@bitwarden/platform";
import { Destination } from "@bitwarden/sdk-internal"; import { Destination, Manager } from "@bitwarden/sdk-internal";
import { IpcLink } from "./ipc-link"; export class IpcForegroundService extends IpcService {
import { IpcMessage } from "./ipc-message"; private static LinkToBackground = new IpcLink(
@Injectable()
export class IpcForegroundService implements OnDestroy {
private destroy$ = new Subject<void>();
private linkToBackground = new IpcLink(
async (data) => { async (data) => {
await chrome.runtime.sendMessage({ payload: data } as IpcMessage); await chrome.runtime.sendMessage({ payload: data } as IpcMessage);
}, },
@ -26,22 +19,7 @@ export class IpcForegroundService implements OnDestroy {
[Destination.BrowserBackground], [Destination.BrowserBackground],
); );
constructor(private sdkService: SdkService) {} protected override async registerLinks(manager: Manager): Promise<void> {
await manager.register_link(IpcForegroundService.LinkToBackground);
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();
} }
} }

View File

@ -1 +1,2 @@
export * from "./services/browser-service"; export * from "./services/browser-service";
export * from "./ipc";

View File

@ -0,0 +1,3 @@
export * from "./ipc-link";
export * from "./ipc-message";
export * from "./ipc.service";

View File

@ -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<void>;
}