From 15181fb0b73bb06c4196586a9c6b024b4efcd88a Mon Sep 17 00:00:00 2001 From: Noel De Martin Date: Thu, 19 May 2022 10:21:45 +0200 Subject: [PATCH 1/2] MOBILE-4080 core: Improve devtools settings --- src/core/constants.ts | 4 +- src/core/initializers/prepare-devtools.ts | 3 + src/core/services/config.ts | 4 +- src/core/services/db.ts | 2 +- src/core/singletons/browser.ts | 78 +++++++++++++++++++++++ src/core/singletons/logger.ts | 2 +- 6 files changed, 88 insertions(+), 5 deletions(-) diff --git a/src/core/constants.ts b/src/core/constants.ts index cabb5585f..e09712c7b 100644 --- a/src/core/constants.ts +++ b/src/core/constants.ts @@ -14,6 +14,7 @@ import envJson from '@/assets/env.json'; import { EnvironmentConfig } from '@/types/config'; +import { CoreBrowser } from '@singletons/browser'; /** * Context levels enumeration. @@ -154,7 +155,8 @@ export class CoreConstants { // @todo [4.0] This is not the proper way to check for development tools, we should rely only on the BUILD variable. return this.BUILD.isDevelopment || this.BUILD.isTesting - || this.CONFIG.versionname.includes('-dev'); + || this.CONFIG.versionname.includes('-dev') + || CoreBrowser.hasDevelopmentSetting('DevTools'); } } diff --git a/src/core/initializers/prepare-devtools.ts b/src/core/initializers/prepare-devtools.ts index 7f9d565bb..dbd3df0b1 100644 --- a/src/core/initializers/prepare-devtools.ts +++ b/src/core/initializers/prepare-devtools.ts @@ -16,9 +16,11 @@ import { CoreApp, CoreAppProvider } from '@services/app'; import { CoreConfig, CoreConfigProvider } from '@services/config'; import { CoreDB, CoreDbProvider } from '@services/db'; import { CoreCustomURLSchemes, CoreCustomURLSchemesProvider } from '@services/urlschemes'; +import { CoreBrowser } from '@singletons/browser'; import { CoreConstants } from '../constants'; type DevelopmentWindow = Window & { + browser?: typeof CoreBrowser; appProvider?: CoreAppProvider; configProvider?: CoreConfigProvider; dbProvider?: CoreDbProvider; @@ -26,6 +28,7 @@ type DevelopmentWindow = Window & { }; function initializeDevelopmentWindow(window: DevelopmentWindow) { + window.browser = CoreBrowser; window.appProvider = CoreApp.instance; window.configProvider = CoreConfig.instance; window.dbProvider = CoreDB.instance; diff --git a/src/core/services/config.ts b/src/core/services/config.ts index eb2221487..8522db456 100644 --- a/src/core/services/config.ts +++ b/src/core/services/config.ts @@ -201,11 +201,11 @@ export class CoreConfigProvider { * Load development config overrides. */ protected loadDevelopmentConfig(): void { - if (!CoreConstants.enableDevTools() || !CoreBrowser.hasCookie('MoodleAppConfig')) { + if (!CoreConstants.enableDevTools() || !CoreBrowser.hasDevelopmentSetting('Config')) { return; } - this.patchEnvironment(JSON.parse(CoreBrowser.getCookie('MoodleAppConfig') ?? '{}'), { patchDefault: true }); + this.patchEnvironment(JSON.parse(CoreBrowser.getDevelopmentSetting('Config') ?? '{}'), { patchDefault: true }); } /** diff --git a/src/core/services/db.ts b/src/core/services/db.ts index 26721e2a7..841f57aef 100644 --- a/src/core/services/db.ts +++ b/src/core/services/db.ts @@ -36,7 +36,7 @@ export class CoreDbProvider { * @returns Whether queries should be logged. */ loggingEnabled(): boolean { - return CoreBrowser.hasCookie('MoodleAppDBLoggingEnabled') || CoreAppProvider.isAutomated(); + return CoreBrowser.hasDevelopmentSetting('DBLoggingEnabled') || CoreAppProvider.isAutomated(); } /** diff --git a/src/core/singletons/browser.ts b/src/core/singletons/browser.ts index 9ba90176b..c4138c07b 100644 --- a/src/core/singletons/browser.ts +++ b/src/core/singletons/browser.ts @@ -27,6 +27,28 @@ export class CoreBrowser { return new RegExp(`(\\s|;|^)${name}=`).test(document.cookie ?? ''); } + /** + * Check whether a development setting is set. + * + * @param name Setting name. + * @returns Whether the development setting is set. + */ + static hasDevelopmentSetting(name: string): boolean { + const setting = this.getDevelopmentSettingKey(name); + + return this.hasCookie(setting) || this.hasLocalStorage(setting); + } + + /** + * Check whether the given localStorage key is set. + * + * @param key localStorage key. + * @returns Whether the key is set. + */ + static hasLocalStorage(key: string): boolean { + return localStorage.getItem(key) !== null; + } + /** * Read a cookie. * @@ -45,4 +67,60 @@ export class CoreBrowser { return cookies[name] ?? null; } + /** + * Read a localStorage key. + * + * @param key localStorage key. + * @return localStorage value. + */ + static getLocalStorage(key: string): string | null { + return localStorage.getItem(key); + } + + /** + * Get development setting value. + * + * @param name Setting name. + * @returns Development setting value. + */ + static getDevelopmentSetting(name: string): string | null { + const setting = this.getDevelopmentSettingKey(name); + + return this.getCookie(setting) ?? this.getLocalStorage(setting); + } + + /** + * Set development setting. + * + * @param name Setting name. + * @param value Setting value. + */ + static setDevelopmentSetting(name: string, value: string): void { + const setting = this.getDevelopmentSettingKey(name); + + document.cookie = `${setting}=${value};path=/`; + localStorage.setItem(setting, value); + } + + /** + * Unset development setting. + * + * @param name Setting name. + */ + static clearDevelopmentSetting(name: string): void { + const setting = this.getDevelopmentSettingKey(name); + + document.cookie = `${setting}=;path=/;expires=Thu, 01 Jan 1970 00:00:01 GMT`; + localStorage.removeItem(setting); + } + + /** + * Get development setting key. + * + * @param name Development setting name. + */ + protected static getDevelopmentSettingKey(name: string): string { + return `MoodleApp${name}`; + } + } diff --git a/src/core/singletons/logger.ts b/src/core/singletons/logger.ts index 27c61fe23..6d59785b8 100644 --- a/src/core/singletons/logger.ts +++ b/src/core/singletons/logger.ts @@ -69,7 +69,7 @@ export class CoreLogger { static getInstance(className: string): CoreLogger { // Disable log on production and testing. if ( - !CoreBrowser.hasCookie('MoodleAppLoggingEnabled') && + !CoreBrowser.hasDevelopmentSetting('LoggingEnabled') && (CoreConstants.BUILD.isProduction || CoreConstants.BUILD.isTesting) ) { if (CoreConstants.BUILD.isProduction) { From 70a1e516b45eb6c5c89bc9f2711f354a1db7985e Mon Sep 17 00:00:00 2001 From: Noel De Martin Date: Thu, 19 May 2022 10:41:52 +0200 Subject: [PATCH 2/2] MOBILE-4081: Fix performance tests --- src/core/initializers/prepare-automated-tests.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/core/initializers/prepare-automated-tests.ts b/src/core/initializers/prepare-automated-tests.ts index 0a78c2456..a0c9fdc44 100644 --- a/src/core/initializers/prepare-automated-tests.ts +++ b/src/core/initializers/prepare-automated-tests.ts @@ -15,6 +15,7 @@ import { ApplicationRef, NgZone as NgZoneService } from '@angular/core'; import { CorePushNotifications, CorePushNotificationsProvider } from '@features/pushnotifications/services/pushnotifications'; import { CoreApp, CoreAppProvider } from '@services/app'; +import { CoreConfig, CoreConfigProvider } from '@services/config'; import { CoreCronDelegate, CoreCronDelegateService } from '@services/cron'; import { CoreDB, CoreDbProvider } from '@services/db'; import { CoreCustomURLSchemes, CoreCustomURLSchemesProvider } from '@services/urlschemes'; @@ -24,6 +25,7 @@ type AutomatedTestsWindow = Window & { appRef?: ApplicationRef; appProvider?: CoreAppProvider; dbProvider?: CoreDbProvider; + configProvider?: CoreConfigProvider; cronProvider?: CoreCronDelegateService; ngZone?: NgZoneService; pushNotifications?: CorePushNotificationsProvider; @@ -34,6 +36,7 @@ function initializeAutomatedTestsWindow(window: AutomatedTestsWindow) { window.appRef = Application.instance; window.appProvider = CoreApp.instance; window.dbProvider = CoreDB.instance; + window.configProvider = CoreConfig.instance; window.cronProvider = CoreCronDelegate.instance; window.ngZone = NgZone.instance; window.pushNotifications = CorePushNotifications.instance;