From bdc3eb559c5c9b3d2945e85a41f969cd9d2d74ca Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Wed, 17 Jan 2018 23:21:17 -0500 Subject: [PATCH] onInstalled listener must be wired up in ctor --- src/background.ts | 4 ++- src/background/runtime.background.ts | 46 ++++++++++++++-------------- 2 files changed, 26 insertions(+), 24 deletions(-) diff --git a/src/background.ts b/src/background.ts index e989cef5d91..fb96ade703e 100644 --- a/src/background.ts +++ b/src/background.ts @@ -3,4 +3,6 @@ import MainBackground from './background/main.background'; const bitwardenIsBackground = (window as any).bitwardenIsBackground = true; const bitwardenMain = (window as any).bitwardenMain = new MainBackground(); -bitwardenMain.bootstrap(); +bitwardenMain.bootstrap().then(() => { + // Finished bootstrapping +}); diff --git a/src/background/runtime.background.ts b/src/background/runtime.background.ts index e85fca8e08e..5b42a8bedaa 100644 --- a/src/background/runtime.background.ts +++ b/src/background/runtime.background.ts @@ -20,12 +20,20 @@ export default class RuntimeBackground { private autofillTimeout: number; private pageDetailsToAutoFill: any[] = []; private isSafari: boolean; + private onInstalledReason: string = null; constructor(private main: MainBackground, private autofillService: AutofillService, private cipherService: CipherService, private platformUtilsService: PlatformUtilsService, private storageService: StorageService, private i18nService: any) { 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() { @@ -250,43 +258,35 @@ export default class RuntimeBackground { } private async checkOnInstalled() { - const gettingStartedUrl = 'https://bitwarden.com/browser-start/'; - if (this.isSafari) { const installedVersionKey = 'installedVersion'; const installedVersion = await this.storageService.get(installedVersionKey); - let reason: string = null; if (installedVersion == null) { - reason = 'install'; + this.onInstalledReason = 'install'; } else if (BrowserApi.getApplicationVersion() !== installedVersion) { - reason = 'update'; + this.onInstalledReason = 'update'; } - if (reason != null) { + if (this.onInstalledReason != null) { await this.storageService.save(installedVersionKey, BrowserApi.getApplicationVersion()); + } + } + + setTimeout(async () => { + if (this.onInstalledReason != null) { (window as any).ga('send', { hitType: 'event', - eventAction: 'onInstalled ' + reason, - }); - } - - if (reason === 'install') { - BrowserApi.createNewTab(gettingStartedUrl); - await this.setDefaultSettings(); - } - } else if (this.runtime.onInstalled) { - this.runtime.onInstalled.addListener(async (details: any) => { - (window as any).ga('send', { - hitType: 'event', - eventAction: 'onInstalled ' + details.reason, + eventAction: 'onInstalled ' + this.onInstalledReason, }); - if (details.reason === 'install') { - BrowserApi.createNewTab(gettingStartedUrl); + if (this.onInstalledReason === 'install') { + BrowserApi.createNewTab('https://bitwarden.com/browser-start/'); await this.setDefaultSettings(); } - }); - } + + this.onInstalledReason = null; + } + }, 500); } private async setDefaultSettings() {