1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-15 15:53:27 +00:00

Display error message when browser integration is disabled, or desktop not running

This commit is contained in:
Hinton
2020-10-21 15:56:10 +02:00
parent a3dbf8b65d
commit 9064298309
3 changed files with 62 additions and 9 deletions

View File

@@ -1371,5 +1371,17 @@
}, },
"desktopIntegrationVerificationText": { "desktopIntegrationVerificationText": {
"message": "Please verify that the desktop application shows this fingerprint: " "message": "Please verify that the desktop application shows this fingerprint: "
},
"desktopIntegrationDisabledTitle": {
"message": "Browser integration is not enabled"
},
"desktopIntegrationDisabledDesc": {
"message": "Browser integration is not enabled in the Bitwarden Desktop Application. Please enable it in the settings within the desktop application."
},
"startDesktopTitle": {
"message": "Start the Bitwarden Desktop Application"
},
"startDesktopDesc": {
"message": "The bitwarden desktop application needs to be started before this function can be used."
} }
} }

View File

@@ -16,6 +16,7 @@ const EncryptionAlgorithm = 'sha1';
export class NativeMessagingBackground { export class NativeMessagingBackground {
private connected = false; private connected = false;
private connecting: boolean;
private port: browser.runtime.Port | chrome.runtime.Port; private port: browser.runtime.Port | chrome.runtime.Port;
private resolver: any = null; private resolver: any = null;
@@ -29,22 +30,54 @@ export class NativeMessagingBackground {
private runtimeBackground: RuntimeBackground, private i18nService: I18nService, private userService: UserService, private runtimeBackground: RuntimeBackground, private i18nService: I18nService, private userService: UserService,
private messagingService: MessagingService) {} private messagingService: MessagingService) {}
connect() { async connect() {
this.port = BrowserApi.connectNative('com.8bit.bitwarden'); return new Promise((resolve, reject) => {
this.port = BrowserApi.connectNative('com.8bit.bitwarden');
this.connected = true; this.connecting = true;
this.port.onMessage.addListener((msg) => this.onMessage(msg)); this.port.onMessage.addListener((message: any) => {
if (message.command === 'connected') {
this.connected = true;
this.connecting = false;
resolve();
} else if (message.command === 'disconnected') {
if (this.connecting) {
this.messagingService.send('showDialog', {
text: this.i18nService.t('startDesktopDesc'),
title: this.i18nService.t('startDesktopTitle'),
confirmText: this.i18nService.t('ok'),
type: 'error',
});
reject();
}
this.connected = false;
this.port.disconnect();
return;
}
this.port.onDisconnect.addListener(() => { this.onMessage(message);
this.connected = false; });
this.port.onDisconnect.addListener(() => {
if (BrowserApi.runtimeLastError().message === 'Specified native messaging host not found.') {
this.messagingService.send('showDialog', {
text: this.i18nService.t('desktopIntegrationDisabledDesc'),
title: this.i18nService.t('desktopIntegrationDisabledTitle'),
confirmText: this.i18nService.t('ok'),
type: 'error',
});
}
this.connected = false;
reject();
});
}); });
} }
async send(message: any) { async send(message: any) {
// If not connected, try to connect // If not connected, try to connect
if (!this.connected) { if (!this.connected) {
this.connect(); await this.connect();
} }
if (this.sharedSecret == null) { if (this.sharedSecret == null) {
@@ -118,7 +151,7 @@ export class NativeMessagingBackground {
const fingerprint = (await this.cryptoService.getFingerprint(await this.userService.getUserId(), this.publicKey)).join(' '); const fingerprint = (await this.cryptoService.getFingerprint(await this.userService.getUserId(), this.publicKey)).join(' ');
this.messagingService.send('showDialog', { this.messagingService.send('showDialog', {
html: `${this.i18nService.t('desktopIntegrationVerificationText')}<br><br><strong>${fingerprint}</strong>.`, html: `${this.i18nService.t('desktopIntegrationVerificationText')}<br><br><strong>${fingerprint}</strong>`,
title: this.i18nService.t('desktopSyncVerificationTitle'), title: this.i18nService.t('desktopSyncVerificationTitle'),
confirmText: this.i18nService.t('ok'), confirmText: this.i18nService.t('ok'),
type: 'warning', type: 'warning',
@@ -129,7 +162,7 @@ export class NativeMessagingBackground {
private async sendUnencrypted(message: any) { private async sendUnencrypted(message: any) {
if (!this.connected) { if (!this.connected) {
this.connect(); await this.connect();
} }
message.timestamp = Date.now(); message.timestamp = Date.now();

View File

@@ -229,4 +229,12 @@ export class BrowserApi {
return chrome.runtime.connectNative(application); return chrome.runtime.connectNative(application);
} }
} }
static runtimeLastError(): browser.runtime._LastError | chrome.runtime.LastError {
if (BrowserApi.isWebExtensionsApi) {
return browser.runtime.lastError;
} else if (BrowserApi.isChromeApi) {
return chrome.runtime.lastError;
}
}
} }