1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-09-28 04:08:47 +02:00
bitwarden-browser/src/background/nativeMessaging.background.ts

41 lines
1.0 KiB
TypeScript
Raw Normal View History

2020-10-11 20:45:25 +02:00
import { BrowserApi } from '../browser/browserApi';
export class NativeMessagingBackground {
private connected = false;
private port: browser.runtime.Port | chrome.runtime.Port;
private resolver: any = null;
connect() {
2020-10-11 20:45:25 +02:00
this.port = BrowserApi.connectNative('com.8bit.bitwarden');
this.connected = true;
this.port.onMessage.addListener((msg: any) => {
if (this.resolver) {
this.resolver(msg);
} else {
2020-10-11 20:45:25 +02:00
// tslint:disable-next-line
console.error('NO RESOLVER');
}
});
this.port.onDisconnect.addListener(() => {
this.connected = false;
});
}
send(message: object) {
// If not connected, try to connect
if (!this.connected) {
this.connect();
}
this.port.postMessage(message);
}
await(): Promise<any> {
return new Promise((resolve, reject) => {
this.resolver = resolve;
});
}
}