2020-10-11 20:45:25 +02:00
|
|
|
import { BrowserApi } from '../browser/browserApi';
|
2020-10-09 17:16:15 +02:00
|
|
|
|
|
|
|
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');
|
2020-10-09 17:16:15 +02:00
|
|
|
|
|
|
|
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
|
2020-10-09 17:16:15 +02:00
|
|
|
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;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|