From 6834e26b370c5eae231a57680f842909b15f696d Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Fri, 19 Jan 2018 11:42:35 -0500 Subject: [PATCH] reseed storage on update in chrome --- src/background/runtime.background.ts | 54 ++++++++++++++++++++++++---- 1 file changed, 47 insertions(+), 7 deletions(-) diff --git a/src/background/runtime.background.ts b/src/background/runtime.background.ts index 30aefc07a8..47e17bf059 100644 --- a/src/background/runtime.background.ts +++ b/src/background/runtime.background.ts @@ -277,19 +277,59 @@ export default class RuntimeBackground { setTimeout(async () => { if (this.onInstalledReason != null) { + if (this.onInstalledReason === 'install') { + BrowserApi.createNewTab('https://bitwarden.com/browser-start/'); + await this.setDefaultSettings(); + } else if (this.onInstalledReason === 'update') { + await this.reseedStorage(); + } + (window as any).ga('send', { hitType: 'event', eventAction: 'onInstalled ' + this.onInstalledReason, }); - - if (this.onInstalledReason === 'install') { - BrowserApi.createNewTab('https://bitwarden.com/browser-start/'); - await this.setDefaultSettings(); - } - this.onInstalledReason = null; } - }, 500); + }, 100); + } + + private async reseedStorage() { + if (!this.platformUtilsService.isChrome() && !this.platformUtilsService.isVivaldi() && + !this.platformUtilsService.isOpera()) { + return; + } + + const currentLockOption = await this.storageService.get(ConstantsService.lockOptionKey); + if (currentLockOption == null) { + return; + } + + const reseed124Key = 'reseededStorage124'; + const reseeded124 = await this.storageService.get(reseed124Key); + if (reseeded124) { + return; + } + + const getStorage = (): Promise => new Promise((resolve) => { + chrome.storage.local.get(null, (o: any) => resolve(o)); + }); + + const clearStorage = (): Promise => 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); } private async setDefaultSettings() {