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

374 lines
14 KiB
TypeScript
Raw Normal View History

2018-01-09 20:26:20 +01:00
import { CipherType } from 'jslib/enums';
2018-01-24 19:27:58 +01:00
import { CipherView } from 'jslib/models/view/cipherView';
import { LoginUriView } from 'jslib/models/view/loginUriView';
2018-01-24 19:27:58 +01:00
import { LoginView } from 'jslib/models/view/loginView';
import { ConstantsService } from 'jslib/services/constants.service';
2018-01-09 20:26:20 +01:00
import { UtilsService } from 'jslib/services/utils.service';
2018-04-11 20:52:49 +02:00
import { I18nService } from 'jslib/abstractions/i18n.service';
2018-01-26 16:48:32 +01:00
import { Analytics } from 'jslib/misc';
2018-01-09 20:26:20 +01:00
import {
2018-01-10 05:05:46 +01:00
CipherService,
StorageService,
2018-01-09 20:26:20 +01:00
} from 'jslib/abstractions';
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';
2018-01-11 20:45:27 +01:00
import { AutofillService } from '../services/abstractions/autofill.service';
2018-04-10 20:20:03 +02:00
import BrowserPlatformUtilsService from '../services/browserPlatformUtils.service';
2018-01-05 22:30:15 +01:00
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 isSafari: boolean;
private onInstalledReason: string = null;
2017-12-07 21:36:24 +01:00
constructor(private main: MainBackground, private autofillService: AutofillService,
2018-04-10 20:20:03 +02:00
private cipherService: CipherService, private platformUtilsService: BrowserPlatformUtilsService,
2018-04-11 20:52:49 +02:00
private storageService: StorageService, private i18nService: I18nService, private analytics: Analytics) {
this.isSafari = this.platformUtilsService.isSafari();
this.runtime = this.isSafari ? safari.application : chrome.runtime;
// onInstalled listener must be wired up before anything else, so we do it in the ctor
if (!this.isSafari) {
this.runtime.onInstalled.addListener((details: any) => {
this.onInstalledReason = details.reason;
});
}
}
async init() {
2017-12-07 21:36:24 +01:00
if (!this.runtime) {
return;
}
if (this.isSafari) {
// Reload the popup when it's opened
this.runtime.addEventListener('popover', (event: any) => {
const win: Window = event.target.contentWindow;
2018-01-13 03:20:49 +01:00
let href = win.location.href;
if (href.indexOf('#') > -1) {
href = href.substr(0, href.indexOf('#'));
}
2018-01-12 19:27:40 +01:00
if (win.location.toString() === href) {
2018-01-12 18:41:51 +01:00
win.location.reload();
} else {
win.location.href = href;
}
}, true);
2017-12-07 21:46:56 +01:00
}
2018-01-16 06:03:17 +01:00
await this.checkOnInstalled();
2018-01-12 21:29:01 +01:00
BrowserApi.messageListener(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':
case 'locked':
await this.main.setIcon();
2018-03-04 04:48:38 +01:00
await this.main.refreshBadgeAndMenu(msg.command === 'locked');
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;
2018-04-10 20:20:03 +02:00
case 'showDialogResolve':
this.platformUtilsService.resolveDialogPromise(msg.dialogId, msg.confirmed);
break;
2018-01-13 16:13:31 +01:00
case 'bgGetDataForTab':
await this.getDataForTab(sender.tab, msg.responseCommand);
break;
2018-01-12 21:20:19 +01:00
case 'bgOpenNotificationBar':
await BrowserApi.tabSendMessageData(sender.tab, 'openNotificationBar', msg.data);
break;
case 'bgCloseNotificationBar':
await BrowserApi.tabSendMessageData(sender.tab, 'closeNotificationBar');
break;
case 'bgAdjustNotificationBar':
await BrowserApi.tabSendMessageData(sender.tab, 'adjustNotificationBar', msg.data);
break;
case 'bgCollectPageDetails':
this.main.collectPageDetailsForContentScript(sender.tab, msg.sender, sender.frameId);
break;
case 'bgAddLogin':
await this.addLogin(msg.login, sender.tab);
break;
case 'bgAddClose':
this.removeAddLogin(sender.tab);
break;
case 'bgAddSave':
await this.saveAddLogin(sender.tab);
break;
case 'bgNeverSave':
await this.saveNever(sender.tab);
break;
case 'bgUpdateContextMenu':
await this.main.refreshBadgeAndMenu();
break;
case 'collectPageDetailsResponse':
switch (msg.sender) {
case 'notificationBar':
const forms = this.autofillService.getFormsWithPasswordFields(msg.details);
await BrowserApi.tabSendMessageData(msg.tab, 'notificationBarPageDetails', {
details: msg.details,
forms: forms,
});
break;
case 'autofiller':
case 'autofill_cmd':
await this.autofillService.doAutoFillForLastUsedLogin([{
frameId: sender.frameId,
tab: msg.tab,
details: msg.details,
}], msg.sender === 'autofill_cmd');
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;
default:
break;
}
}
2017-12-07 21:36:24 +01:00
private async autofillPage() {
await this.autofillService.doAutoFill({
cipher: this.main.loginToAutoFill,
pageDetails: this.pageDetailsToAutoFill,
fromBackground: true,
});
// reset
this.main.loginToAutoFill = null;
this.pageDetailsToAutoFill = [];
}
private async saveAddLogin(tab: any) {
for (let i = this.main.loginsToAdd.length - 1; i >= 0; i--) {
if (this.main.loginsToAdd[i].tabId !== tab.id) {
continue;
}
const loginInfo = this.main.loginsToAdd[i];
2018-01-05 22:30:15 +01:00
const tabDomain = this.platformUtilsService.getDomain(tab.url);
2017-12-07 21:36:24 +01:00
if (tabDomain != null && tabDomain !== loginInfo.domain) {
continue;
}
this.main.loginsToAdd.splice(i, 1);
2018-01-24 19:27:58 +01:00
const loginModel = new LoginView();
const loginUri = new LoginUriView();
loginUri.uri = loginInfo.uri;
loginModel.uris = [loginUri];
2018-01-24 19:27:58 +01:00
loginModel.username = loginInfo.username;
loginModel.password = loginInfo.password;
const model = new CipherView();
model.name = UtilsService.getHostname(loginInfo.uri) || loginInfo.domain;
2018-01-24 19:27:58 +01:00
model.type = CipherType.Login;
model.login = loginModel;
2017-12-07 21:36:24 +01:00
2018-01-24 19:27:58 +01:00
const cipher = await this.cipherService.encrypt(model);
2017-12-07 21:36:24 +01:00
await this.cipherService.saveWithServer(cipher);
2018-01-19 22:19:24 +01:00
this.analytics.ga('send', {
2017-12-07 21:36:24 +01:00
hitType: 'event',
eventAction: 'Added Login from Notification Bar',
});
BrowserApi.tabSendMessageData(tab, 'closeNotificationBar');
2017-12-07 21:36:24 +01:00
}
}
private async saveNever(tab: any) {
for (let i = this.main.loginsToAdd.length - 1; i >= 0; i--) {
if (this.main.loginsToAdd[i].tabId !== tab.id) {
continue;
}
const loginInfo = this.main.loginsToAdd[i];
2018-01-05 22:30:15 +01:00
const tabDomain = this.platformUtilsService.getDomain(tab.url);
2017-12-07 21:36:24 +01:00
if (tabDomain != null && tabDomain !== loginInfo.domain) {
continue;
}
this.main.loginsToAdd.splice(i, 1);
2018-01-09 20:26:20 +01:00
const hostname = UtilsService.getHostname(tab.url);
2017-12-07 21:36:24 +01:00
await this.cipherService.saveNeverDomain(hostname);
BrowserApi.tabSendMessageData(tab, 'closeNotificationBar');
2017-12-07 21:36:24 +01:00
}
}
private async addLogin(loginInfo: any, tab: any) {
2018-01-05 22:30:15 +01:00
const loginDomain = this.platformUtilsService.getDomain(loginInfo.url);
2017-12-07 21:36:24 +01:00
if (loginDomain == null) {
return;
}
const ciphers = await this.cipherService.getAllDecryptedForUrl(loginInfo.url);
2017-12-07 21:36:24 +01:00
let match = false;
for (let i = 0; i < ciphers.length; i++) {
if (ciphers[i].login.username === loginInfo.username) {
match = true;
break;
}
}
if (!match) {
// remove any old logins for this tab
this.removeAddLogin(tab);
this.main.loginsToAdd.push({
username: loginInfo.username,
password: loginInfo.password,
domain: loginDomain,
uri: loginInfo.url,
tabId: tab.id,
expires: new Date((new Date()).getTime() + 30 * 60000), // 30 minutes
});
await this.main.checkLoginsToAdd(tab);
}
}
private removeAddLogin(tab: any) {
for (let i = this.main.loginsToAdd.length - 1; i >= 0; i--) {
if (this.main.loginsToAdd[i].tabId === tab.id) {
this.main.loginsToAdd.splice(i, 1);
}
}
}
2018-01-16 06:03:17 +01:00
private async checkOnInstalled() {
if (this.isSafari) {
2018-02-16 21:50:23 +01:00
const installedVersion = await this.storageService.get<string>(ConstantsService.installedVersionKey);
2018-01-16 06:03:17 +01:00
if (installedVersion == null) {
this.onInstalledReason = 'install';
2018-01-16 06:03:17 +01:00
} else if (BrowserApi.getApplicationVersion() !== installedVersion) {
this.onInstalledReason = 'update';
2018-01-16 06:03:17 +01:00
}
if (this.onInstalledReason != null) {
2018-02-16 21:50:23 +01:00
await this.storageService.save(ConstantsService.installedVersionKey,
BrowserApi.getApplicationVersion());
2018-01-16 06:03:17 +01:00
}
}
2018-01-16 06:03:17 +01:00
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-19 17:42:35 +01:00
} else if (this.onInstalledReason === 'update') {
await this.reseedStorage();
2018-01-16 06:03:17 +01:00
}
2018-01-19 22:19:24 +01:00
this.analytics.ga('send', {
2018-01-19 17:42:35 +01:00
hitType: 'event',
eventAction: 'onInstalled ' + this.onInstalledReason,
});
this.onInstalledReason = null;
}
2018-01-19 17:42:35 +01:00
}, 100);
}
private async reseedStorage() {
if (!this.platformUtilsService.isChrome() && !this.platformUtilsService.isVivaldi() &&
!this.platformUtilsService.isOpera()) {
return;
}
const currentLockOption = await this.storageService.get<number>(ConstantsService.lockOptionKey);
if (currentLockOption == null) {
return;
}
const reseed124Key = 'reseededStorage124';
2018-01-19 17:46:32 +01:00
const reseeded124 = await this.storageService.get<boolean>(reseed124Key);
2018-01-19 17:42:35 +01:00
if (reseeded124) {
return;
}
const getStorage = (): Promise<any> => new Promise((resolve) => {
chrome.storage.local.get(null, (o: any) => resolve(o));
});
const clearStorage = (): Promise<void> => new Promise((resolve) => {
chrome.storage.local.clear(() => resolve());
});
const storage = await getStorage();
await clearStorage();
for (const key in storage) {
if (!storage.hasOwnProperty(key)) {
continue;
}
await this.storageService.save(key, storage[key]);
}
await this.storageService.save(reseed124Key, true);
2018-01-16 06:03:17 +01:00
}
2018-01-18 04:42:31 +01:00
private async setDefaultSettings() {
// Default lock options to "on restart".
const currentLockOption = await this.storageService.get<number>(ConstantsService.lockOptionKey);
if (currentLockOption == null) {
await this.storageService.save(ConstantsService.lockOptionKey, -1);
}
}
2018-01-13 16:13:31 +01:00
private async getDataForTab(tab: any, responseCommand: string) {
2018-01-13 21:09:05 +01:00
const responseData: any = {};
2018-01-13 16:13:31 +01:00
if (responseCommand === 'notificationBarDataResponse') {
2018-01-13 21:09:05 +01:00
responseData.neverDomains = await this.storageService.get<any>(ConstantsService.neverDomainsKey);
responseData.disabledNotification = await this.storageService.get<boolean>(
2018-01-13 16:13:31 +01:00
ConstantsService.disableAddLoginNotificationKey);
} else if (responseCommand === 'autofillerAutofillOnPageLoadEnabledResponse') {
2018-01-13 21:09:05 +01:00
responseData.autofillEnabled = await this.storageService.get<boolean>(
2018-01-13 16:13:31 +01:00
ConstantsService.enableAutoFillOnPageLoadKey);
2018-01-13 21:09:05 +01:00
} else if (responseCommand === 'notificationBarFrameDataResponse') {
responseData.i18n = {
2018-04-11 20:52:49 +02:00
appName: this.i18nService.t('appName'),
close: this.i18nService.t('close'),
yes: this.i18nService.t('yes'),
never: this.i18nService.t('never'),
notificationAddSave: this.i18nService.t('notificationAddSave'),
notificationNeverSave: this.i18nService.t('notificationNeverSave'),
notificationAddDesc: this.i18nService.t('notificationAddDesc'),
2018-01-13 21:09:05 +01:00
};
2018-01-13 16:13:31 +01:00
}
2018-01-13 21:09:05 +01:00
await BrowserApi.tabSendMessageData(tab, responseCommand, responseData);
2018-01-13 16:13:31 +01:00
}
}