1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-14 10:26:19 +01:00
bitwarden-browser/src/background/runtime.background.ts

221 lines
9.1 KiB
TypeScript
Raw Normal View History

import { EnvironmentService } from 'jslib-common/abstractions/environment.service';
import { I18nService } from 'jslib-common/abstractions/i18n.service';
import { MessagingService } from 'jslib-common/abstractions/messaging.service';
import { NotificationsService } from 'jslib-common/abstractions/notifications.service';
import { StorageService } from 'jslib-common/abstractions/storage.service';
import { SystemService } from 'jslib-common/abstractions/system.service';
import { ConstantsService } from 'jslib-common/services/constants.service';
2021-02-10 16:40:15 +01:00
import { AutofillService } from '../services/abstractions/autofill.service';
import BrowserPlatformUtilsService from '../services/browserPlatformUtils.service';
2017-12-07 21:36:24 +01:00
2018-01-12 17:09:30 +01:00
import { BrowserApi } from '../browser/browserApi';
2017-12-07 21:36:24 +01:00
import MainBackground from './main.background';
import { Utils } from 'jslib-common/misc/utils';
2018-04-23 19:04:11 +02:00
import { PolicyType } from 'jslib-common/enums/policyType';
2021-10-13 22:08:02 +02:00
import AddChangePasswordQueueMessage from './models/addChangePasswordQueueMessage';
import AddLoginQueueMessage from './models/addLoginQueueMessage';
export default class RuntimeBackground {
private runtime: any;
2018-04-06 17:48:45 +02:00
private autofillTimeout: any;
2017-12-07 21:36:24 +01:00
private pageDetailsToAutoFill: any[] = [];
private onInstalledReason: string = null;
2017-12-07 21:36:24 +01:00
constructor(private main: MainBackground, private autofillService: AutofillService,
private platformUtilsService: BrowserPlatformUtilsService,
2018-08-20 23:40:39 +02:00
private storageService: StorageService, private i18nService: I18nService,
private notificationsService: NotificationsService, private systemService: SystemService,
private environmentService: EnvironmentService, private messagingService: MessagingService) {
// onInstalled listener must be wired up before anything else, so we do it in the ctor
chrome.runtime.onInstalled.addListener((details: any) => {
this.onInstalledReason = details.reason;
});
}
async init() {
if (!chrome.runtime) {
2017-12-07 21:36:24 +01:00
return;
}
2018-01-16 06:03:17 +01:00
await this.checkOnInstalled();
BrowserApi.messageListener('runtime.background', async (msg: any, sender: any, sendResponse: any) => {
2018-01-12 21:20:19 +01:00
await this.processMessage(msg, sender, sendResponse);
});
}
2017-12-07 21:36:24 +01:00
2018-01-14 00:16:19 +01:00
async processMessage(msg: any, sender: any, sendResponse: any) {
2018-01-12 21:20:19 +01:00
switch (msg.command) {
case 'loggedIn':
case 'unlocked':
if (this.main.lockedVaultPendingNotifications.length > 0) {
await BrowserApi.closeLoginTab();
const item = this.main.lockedVaultPendingNotifications[0];
if (item.commandToRetry?.sender?.tab?.id) {
await BrowserApi.focusSpecifiedTab(item.commandToRetry.sender.tab.id);
}
}
await this.main.setIcon();
await this.main.refreshBadgeAndMenu(false);
this.notificationsService.updateConnection(msg.command === 'unlocked');
this.systemService.cancelProcessReload();
this.main.unlockCompleted();
break;
case 'addToLockedVaultPendingNotifications':
const retryMessage = {
commandToRetry: {
...msg.retryItem,
sender: sender
},
from: msg.from,
};
this.main.lockedVaultPendingNotifications.push(retryMessage);
2018-01-12 21:20:19 +01:00
break;
case 'logout':
await this.main.logout(msg.expired);
break;
case 'syncCompleted':
if (msg.successfully) {
setTimeout(async () => await this.main.refreshBadgeAndMenu(), 2000);
}
break;
2018-01-18 22:17:58 +01:00
case 'openPopup':
await this.main.openPopup();
break;
case 'promptForLogin':
await BrowserApi.createNewTab('popup/index.html?uilocation=popout', true, true);
break;
2018-04-10 20:20:03 +02:00
case 'showDialogResolve':
this.platformUtilsService.resolveDialogPromise(msg.dialogId, msg.confirmed);
break;
2018-01-12 21:20:19 +01:00
case 'bgCollectPageDetails':
await this.main.collectPageDetailsForContentScript(sender.tab, msg.sender, sender.frameId);
2018-01-12 21:20:19 +01:00
break;
case 'bgUpdateContextMenu':
case 'editedCipher':
case 'addedCipher':
case 'deletedCipher':
2018-01-12 21:20:19 +01:00
await this.main.refreshBadgeAndMenu();
break;
case 'bgReseedStorage':
2019-02-13 17:34:42 +01:00
await this.main.reseedStorage();
break;
2018-01-12 21:20:19 +01:00
case 'collectPageDetailsResponse':
switch (msg.sender) {
case 'autofiller':
case 'autofill_cmd':
const totpCode = await this.autofillService.doAutoFillActiveTab([{
2018-01-12 21:20:19 +01:00
frameId: sender.frameId,
tab: msg.tab,
details: msg.details,
}], msg.sender === 'autofill_cmd');
2018-08-31 03:47:49 +02:00
if (totpCode != null) {
this.platformUtilsService.copyToClipboard(totpCode, { window: window });
}
2018-01-12 21:20:19 +01:00
break;
case 'contextMenu':
clearTimeout(this.autofillTimeout);
this.pageDetailsToAutoFill.push({
frameId: sender.frameId,
tab: msg.tab,
details: msg.details,
});
this.autofillTimeout = setTimeout(async () => await this.autofillPage(), 300);
break;
default:
break;
}
break;
2020-08-14 22:20:16 +02:00
case 'authResult':
const vaultUrl = this.environmentService.getWebVaultUrl();
2020-08-14 22:20:16 +02:00
if (msg.referrer == null || Utils.getHostname(vaultUrl) !== msg.referrer) {
return;
}
try {
2020-09-18 22:03:08 +02:00
BrowserApi.createNewTab('popup/index.html?uilocation=popout#/sso?code=' +
msg.code + '&state=' + msg.state);
}
catch { }
2020-08-14 22:20:16 +02:00
break;
case 'webAuthnResult':
const vaultUrl2 = this.environmentService.getWebVaultUrl();
if (msg.referrer == null || Utils.getHostname(vaultUrl2) !== msg.referrer) {
return;
}
const params = `webAuthnResponse=${encodeURIComponent(msg.data)};remember=${msg.remember}`;
BrowserApi.createNewTab(`popup/index.html?uilocation=popout#/2fa;${params}`, undefined, false);
break;
case 'reloadPopup':
this.messagingService.send('reloadPopup');
break;
2021-05-12 05:35:18 +02:00
case 'emailVerificationRequired':
this.messagingService.send('showDialog', {
dialogId: 'emailVerificationRequired',
title: this.i18nService.t('emailVerificationRequired'),
text: this.i18nService.t('emailVerificationRequiredDesc'),
confirmText: this.i18nService.t('ok'),
type: 'info',
});
break;
case 'getClickedElementResponse':
this.platformUtilsService.copyToClipboard(msg.identifier, { window: window });
2018-01-12 21:20:19 +01:00
default:
break;
}
}
2017-12-07 21:36:24 +01:00
private async autofillPage() {
const totpCode = await this.autofillService.doAutoFill({
2017-12-07 21:36:24 +01:00
cipher: this.main.loginToAutoFill,
pageDetails: this.pageDetailsToAutoFill,
2021-02-10 16:40:15 +01:00
fillNewPassword: true,
2017-12-07 21:36:24 +01:00
});
2018-08-31 03:47:49 +02:00
if (totpCode != null) {
this.platformUtilsService.copyToClipboard(totpCode, { window: window });
}
2017-12-07 21:36:24 +01:00
// reset
this.main.loginToAutoFill = null;
this.pageDetailsToAutoFill = [];
}
2018-01-16 06:03:17 +01:00
private async checkOnInstalled() {
setTimeout(async () => {
if (this.onInstalledReason != null) {
if (this.onInstalledReason === 'install') {
BrowserApi.createNewTab('https://bitwarden.com/browser-start/');
2018-01-18 04:42:31 +01:00
await this.setDefaultSettings();
2018-01-16 06:03:17 +01:00
}
this.onInstalledReason = null;
}
2018-01-19 17:42:35 +01:00
}, 100);
}
2018-01-18 04:42:31 +01:00
private async setDefaultSettings() {
// Default timeout option to "on restart".
const currentVaultTimeout = await this.storageService.get<number>(ConstantsService.vaultTimeoutKey);
if (currentVaultTimeout == null) {
await this.storageService.save(ConstantsService.vaultTimeoutKey, -1);
}
// Default action to "lock".
const currentVaultTimeoutAction = await this.storageService.get<string>(ConstantsService.vaultTimeoutActionKey);
if (currentVaultTimeoutAction == null) {
await this.storageService.save(ConstantsService.vaultTimeoutActionKey, 'lock');
2018-01-18 04:42:31 +01:00
}
}
}