From eb80e42463731500958da7b62484d0872b87ca9f Mon Sep 17 00:00:00 2001 From: Vishwas Shashidhar Date: Mon, 30 Nov 2020 16:02:53 +0530 Subject: [PATCH] SDA-2694: add logic to delete app cache before app initialization (#1126) --- package.json | 2 +- src/app/config-handler.ts | 28 ++++++++++++++++++++++++---- src/app/main.ts | 33 +++++++++++++++++++++++---------- src/app/version-handler.ts | 1 + src/app/window-handler.ts | 17 +++++++++++++++++ 5 files changed, 66 insertions(+), 15 deletions(-) diff --git a/package.json b/package.json index 37ff9504..b8d173a0 100644 --- a/package.json +++ b/package.json @@ -123,7 +123,7 @@ "browserify": "16.5.1", "cross-env": "5.2.0", "del": "3.0.0", - "electron": "9.3.2", + "electron": "9.3.5", "electron-builder": "22.7.0", "electron-builder-squirrel-windows": "20.38.3", "electron-icon-maker": "0.0.4", diff --git a/src/app/config-handler.ts b/src/app/config-handler.ts index afc094d3..1b2f1635 100644 --- a/src/app/config-handler.ts +++ b/src/app/config-handler.ts @@ -39,6 +39,7 @@ export interface IConfig { mainWinPos?: ICustomRectangle; locale?: string; installVariant?: string; + bootCount?: number; } export interface IGlobalConfig { @@ -117,6 +118,7 @@ class Config { public filteredCloudConfig: ICloudConfig | {}; private isFirstTime: boolean = true; private installVariant: string | undefined; + private bootCount: number | undefined; private readonly configFileName: string; private readonly installVariantFilename: string; private readonly installVariantPath: string; @@ -278,10 +280,19 @@ class Config { // update to the new build number filteredFields.buildNumber = buildNumber; filteredFields.installVariant = this.installVariant; + filteredFields.bootCount = 0; logger.info(`config-handler: setting first time launch for build`, buildNumber); return await this.updateUserConfig(filteredFields); } - await this.updateUserConfig({ buildNumber, installVariant: this.installVariant }); + await this.updateUserConfig({ buildNumber, installVariant: this.installVariant, bootCount: this.bootCount }); + } + + /** + * Gets the boot count for an SDA installation + */ + public getBootCount(): number | undefined { + logger.info(`config-handler: Current boot count is ${this.bootCount}`); + return this.bootCount; } /** @@ -312,9 +323,9 @@ class Config { const { acpFeatureLevelEntitlements, podLevelEntitlements, pmpEntitlements } = this.cloudConfig as ICloudConfig; // Filter out some values - const filteredACP = filterOutSelectedValues(acpFeatureLevelEntitlements, [ true, 'NOT_SET', '', [] ]); - const filteredPod = filterOutSelectedValues(podLevelEntitlements, [ true, 'NOT_SET', '', [] ]); - const filteredPMP = filterOutSelectedValues(pmpEntitlements, [ true, 'NOT_SET', '', [] ]); + const filteredACP = filterOutSelectedValues(acpFeatureLevelEntitlements, [true, 'NOT_SET', '', []]); + const filteredPod = filterOutSelectedValues(podLevelEntitlements, [true, 'NOT_SET', '', []]); + const filteredPMP = filterOutSelectedValues(pmpEntitlements, [true, 'NOT_SET', '', []]); // priority is PMP > ACP > SDA this.filteredCloudConfig = { ...filteredACP, ...filteredPod, ...filteredPMP }; @@ -406,16 +417,25 @@ class Config { if (!installVariant) { logger.info(`config-handler: there's no install variant found, this is a first time launch`); this.isFirstTime = true; + this.bootCount = 0; return; } if (installVariant && typeof installVariant === 'string' && installVariant !== this.installVariant) { logger.info(`config-handler: install variant found is of a different instance, this is a first time launch`); this.isFirstTime = true; + this.bootCount = 0; return; } logger.info(`config-handler: install variant is the same as the existing one, not a first time launch`); this.isFirstTime = false; + this.bootCount = (this.getConfigFields(['bootCount']) as IConfig).bootCount; + if (this.bootCount !== undefined) { + this.bootCount++; + await this.updateUserConfig({ bootCount: this.bootCount }); + } else { + await this.updateUserConfig({ bootCount: 0 }); + } } } diff --git a/src/app/main.ts b/src/app/main.ts index 520a975e..35aa3c06 100644 --- a/src/app/main.ts +++ b/src/app/main.ts @@ -67,18 +67,23 @@ if (!isDevEnv) { } /** - * Main function that init the application + * Restarts the app in case of network issues + */ +const restartOnFirstInstall = async (): Promise => { + const bootCount = config.getBootCount(); + if (bootCount !== undefined && bootCount === 1) { + logger.warn(`Boot count fits the criteria of equal to 1, restarting the app`); + app.relaunch(); + app.quit(); + } + logger.warn(`Boot count does not fit the criteria of lesser than or equal to 1, not restarting the app`); +}; + +/** + * Main function that initialises the application */ let oneStart = false; const startApplication = async () => { - await app.whenReady(); - if (oneStart) { - return; - } - - logger.info('main: app is ready, performing initial checks oneStart: ' + oneStart); - oneStart = true; - createAppCacheFile(); if (config.isFirstTimeLaunch()) { logger.info(`main: This is a first time launch! will update config and handle auto launch`); cleanAppCacheOnInstall(); @@ -87,11 +92,19 @@ const startApplication = async () => { await autoLaunchInstance.handleAutoLaunch(); } } + await app.whenReady(); + if (oneStart) { + return; + } + + logger.info('main: app is ready, performing initial checks oneStart: ' + oneStart); + oneStart = true; + createAppCacheFile(); // Picks global config values and updates them in the user config await config.updateUserConfigOnStart(); - // Setup session properties only after app ready setSessionProperties(); await windowHandler.createApplication(); + restartOnFirstInstall(); logger.info(`main: created application`); }; diff --git a/src/app/version-handler.ts b/src/app/version-handler.ts index 55c0f4ad..26e5aa26 100644 --- a/src/app/version-handler.ts +++ b/src/app/version-handler.ts @@ -77,6 +77,7 @@ class VersionHandler { if (!this.mainUrl) { logger.error(`version-handler: Unable to get pod url for getting version data from server! Setting defaults!`); + logger.info(`version-handler: Setting defaults -> ${JSON.stringify(this.versionInfo)}`); resolve(this.versionInfo); return; } diff --git a/src/app/window-handler.ts b/src/app/window-handler.ts index 10a26958..f206940d 100644 --- a/src/app/window-handler.ts +++ b/src/app/window-handler.ts @@ -386,6 +386,7 @@ export class WindowHandler { cleanAppCacheOnCrash(this.mainWindow); // loads the main window with url from config/cmd line + logger.info(`Loading main window with url ${this.url}`); this.mainWindow.loadURL(this.url); // check for build expiry in case of test builds this.checkExpiry(this.mainWindow); @@ -412,6 +413,22 @@ export class WindowHandler { ); }); + const logEvents = [ + 'did-fail-provisional-load', 'did-frame-finish-load', + 'did-start-loading', 'did-stop-loading', 'will-redirect', + 'did-navigate', 'did-navigate-in-page', 'preload-error', + ]; + + logEvents.forEach((windowEvent: any) => { + this.mainWindow?.webContents.on(windowEvent, () => { + logger.info(`window-handler: Main Window Event Occurred: ${windowEvent}`); + }); + }); + + this.mainWindow.once('ready-to-show', (event: Electron.Event) => { + logger.info(`window-handler: Main Window ready to show: ${event}`); + }); + this.mainWindow.webContents.on('did-finish-load', async () => { // reset to false when the client reloads this.isMana = false;