From 071cb939ef7069216ccb9cc8bb5cb04288a30427 Mon Sep 17 00:00:00 2001 From: Salah Benmoussati Date: Wed, 27 Oct 2021 17:05:53 +0200 Subject: [PATCH] SDA-3377 Processing multiple chrome flags provided as installation parameters --- docs/features/config-file.md | 2 ++ spec/chromeFlags.spec.ts | 5 ++++ src/app/chrome-flags.ts | 48 +++++++++++++++++++++++++++--------- 3 files changed, 44 insertions(+), 11 deletions(-) diff --git a/docs/features/config-file.md b/docs/features/config-file.md index 17bbac94..f8f32ce7 100644 --- a/docs/features/config-file.md +++ b/docs/features/config-file.md @@ -57,4 +57,6 @@ position: - openExternal: This allows users to control if the SDA can open an external app. If set to "false", SDA won't be able to open external apps. For example, opening a link from within the SDA in a browser tab. - autoLaunchPath: This allows administrators to set a custom launch path for an app that can bootstrap the SDA. Typically used by administrators who have their own bootstrapped to open other apps like the SDA. - Example can be C:\Program Files\My Bootstrapper\start.exe + - chromeFlags: This allows users to set Chromium flags. + - Example can be "--enable-logging --log-file=C:\\Users\\YOUR_USER\\Desktop\\chrome_debug.log" diff --git a/spec/chromeFlags.spec.ts b/spec/chromeFlags.spec.ts index c1ca61cc..949ff031 100644 --- a/spec/chromeFlags.spec.ts +++ b/spec/chromeFlags.spec.ts @@ -26,6 +26,11 @@ jest.mock('../src/app/config-handler', () => { disableThrottling: 'DISABLED', }; }), + getGlobalConfigFields: jest.fn(() => { + return { + disableThrottling: 'DISABLED', + }; + }), }, }; }); diff --git a/src/app/chrome-flags.ts b/src/app/chrome-flags.ts index 86cbaa34..46c6572b 100644 --- a/src/app/chrome-flags.ts +++ b/src/app/chrome-flags.ts @@ -3,6 +3,8 @@ import { app, session } from 'electron'; import { logger } from '../common/logger'; import { CloudConfigDataTypes, config, IConfig } from './config-handler'; +const CHROME_FLAG_PREFIX = '--'; + // Set default flags logger.info(`chrome-flags: Setting mandatory chrome flags`, { flag: { 'ssl-version-fallback-min': 'tls1.2' }, @@ -63,39 +65,45 @@ export const setChromeFlags = () => { app.commandLine.appendSwitch(key, val); } } - - const cmdArgs = process.argv; + let cmdArgs = process.argv; + const { chromeFlags } = config.getGlobalConfigFields(['chromeFlags']) as any; + if (chromeFlags?.length) { + logger.info( + `chrome-flags: found chrome flags in config file: ${chromeFlags}`, + ); + const splittedChromeFlags = chromeFlagsSplitter(chromeFlags); + cmdArgs = cmdArgs.concat(splittedChromeFlags); + } cmdArgs.forEach((arg) => { // We need to check if the argument key matches the one // in the special args array and return if it does match const argSplit = arg.split('='); const argKey = argSplit[0]; const argValue = argSplit[1] && arg.substring(arg.indexOf('=') + 1); - if (arg.startsWith('--') && specialArgs.includes(argKey)) { + if (arg.startsWith(CHROME_FLAG_PREFIX) && specialArgs.includes(argKey)) { return; } // All the chrome flags starts with -- // So, any other arg (like 'electron' or '.') // need to be skipped - if (arg.startsWith('--')) { + if (arg.startsWith(CHROME_FLAG_PREFIX)) { // Since chrome takes values after an equals // We split the arg and set it either as // just a key, or as a key-value pair if (argKey && argValue) { app.commandLine.appendSwitch(argKey.substr(2), argValue); + logger.info( + `chrome-flags: Appended chrome command line switch ${argKey} with value ${argValue}`, + ); } else { app.commandLine.appendSwitch(argKey); + logger.info( + `chrome-flags: Appended chrome command line switch ${argKey}`, + ); } - logger.info( - `Appended chrome command line switch ${argKey} with value ${argValue}`, - ); } }); - - const { chromeFlags } = config.getConfigFields(['chromeFlags']) as any; - app.commandLine.appendSwitch(chromeFlags); - logger.info(`chrome-flags: Appended chrome command line with ${chromeFlags}`); }; /** @@ -116,3 +124,21 @@ export const setSessionProperties = () => { ); } }; + +/** + * Splits a string of concatenated chrome flags into an array of chrome flags. + * @param flags Chrome flags provided as a string + * @returns An array of chrome flags + */ +const chromeFlagsSplitter = (flags: string) => { + logger.info('chrome-flags: Parsing flags', flags); + const splittedFlags = flags + .split(CHROME_FLAG_PREFIX) + .filter((chromeFlag: string) => chromeFlag.length) + .map( + (filteredFlag: string) => + `${CHROME_FLAG_PREFIX}${filteredFlag.trimEnd()}`, + ); + logger.info('chrome-flags: Parsed flags', splittedFlags); + return splittedFlags; +};