Skip to content

Commit

Permalink
feat: Allow passing custom chromium preferences (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
aklinker1 authored Jan 29, 2024
1 parent 149df83 commit 9181ccd
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 25 deletions.
80 changes: 58 additions & 22 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"@babel/runtime": "7.23.9",
"@devicefarmer/adbkit": "3.2.6",
"bunyan": "1.8.15",
"chrome-launcher": "0.15.1",
"chrome-launcher": "1.1.0",
"debounce": "1.2.1",
"es6-error": "4.1.1",
"firefox-profile": "4.5.0",
Expand All @@ -64,6 +64,7 @@
"node-notifier": "10.0.1",
"parse-json": "7.1.1",
"promise-toolbox": "0.21.0",
"set-value": "4.1.0",
"source-map-support": "0.5.21",
"strip-bom": "5.0.0",
"strip-json-comments": "5.0.1",
Expand Down
6 changes: 6 additions & 0 deletions src/cmd/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export default async function run(
firefoxApkComponent,
// Chromium CLI options.
chromiumBinary,
chromiumPref,
chromiumProfile,
},
{
Expand Down Expand Up @@ -86,6 +87,10 @@ export default async function run(
customPrefs['extensions.manifestV3.enabled'] = true;
}

// Create an alias for --chromium-pref since it has been transformed into an
// object containing one or more preferences.
const customChromiumPrefs = { ...chromiumPref };

const manifestData = await getValidatedManifest(sourceDir);

const profileDir = firefoxProfile || chromiumProfile;
Expand Down Expand Up @@ -193,6 +198,7 @@ export default async function run(
...commonRunnerParams,
chromiumBinary,
chromiumProfile,
customChromiumPrefs,
};

const chromiumRunner = await createExtensionRunner({
Expand Down
20 changes: 20 additions & 0 deletions src/extension-runners/chromium.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
launch as defaultChromiumLaunch,
} from 'chrome-launcher';
import WebSocket, { WebSocketServer } from 'ws';
import set from 'set-value';

import { createLogger } from '../util/logger.js';
import { TempDir } from '../util/temp-dir.js';
Expand All @@ -26,6 +27,10 @@ export const DEFAULT_CHROME_FLAGS = ChromeLauncher.defaultFlags().filter(
(flag) => !EXCLUDED_CHROME_FLAGS.includes(flag),
);

const DEFAULT_PREFS = {
'extensions.ui.developer_mode': true,
};

/**
* Implements an IExtensionRunner which manages a Chromium instance.
*/
Expand Down Expand Up @@ -210,6 +215,7 @@ export class ChromiumExtensionRunner {
userDataDir,
// Ignore default flags to keep the extension enabled.
ignoreDefaultFlags: true,
prefs: this.getPrefs(),
});

this.chromiumInstance.process.once('close', () => {
Expand Down Expand Up @@ -414,4 +420,18 @@ export class ChromiumExtensionRunner {
}
}
}

/**
* Returns a deep preferences object based on a set of flat preferences, like
* "extensions.ui.developer_mode".
*/
getPrefs() {
return Object.entries({
...DEFAULT_PREFS,
...(this.params.customChromiumPrefs || {}),
}).reduce((prefs, [key, value]) => {
set(prefs, key, value);
return prefs;
}, {});
}
}
57 changes: 55 additions & 2 deletions tests/unit/test-extension-runners/test.chromium.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ function prepareExtensionRunnerParams({ params } = {}) {

describe('util/extension-runners/chromium', async () => {
it('uses the expected chrome flags', () => {
// Flags from chrome-launcher v0.14.0
// Flags from chrome-launcher v1.1.0
const expectedFlags = [
'--disable-features=Translate',
'--disable-features=Translate,OptimizationHints,MediaRouter,DialMediaRouteProvider,CalculateNativeWinOcclusion,InterestFeedContentSuggestions,CertificateTransparencyComponentUpdater,AutofillServerCommunication',
'--disable-component-extensions-with-background-pages',
'--disable-background-networking',
'--disable-component-update',
Expand All @@ -66,6 +66,9 @@ describe('util/extension-runners/chromium', async () => {
'--password-store=basic',
'--use-mock-keychain',
'--force-fieldtrials=*BackgroundTracing/default/',
'--disable-hang-monitor',
'--disable-prompt-on-repost',
'--disable-domain-reliability',
];

assert.deepEqual(DEFAULT_CHROME_FLAGS, expectedFlags);
Expand Down Expand Up @@ -615,6 +618,56 @@ describe('util/extension-runners/chromium', async () => {
}),
);

it('does pass default prefs to chrome', async () => {
const { params } = prepareExtensionRunnerParams();

const runnerInstance = new ChromiumExtensionRunner(params);
await runnerInstance.run();

sinon.assert.calledOnce(params.chromiumLaunch);
sinon.assert.calledWithMatch(params.chromiumLaunch, {
prefs: {
extensions: {
ui: {
developer_mode: true,
},
},
},
});

await runnerInstance.exit();
});

it('does pass custom prefs to chrome', async () => {
const { params } = prepareExtensionRunnerParams({
params: {
customChromiumPrefs: {
'download.default_directory': '/some/directory',
'extensions.ui.developer_mode': false,
},
},
});

const runnerInstance = new ChromiumExtensionRunner(params);
await runnerInstance.run();

sinon.assert.calledOnce(params.chromiumLaunch);
sinon.assert.calledWithMatch(params.chromiumLaunch, {
prefs: {
download: {
default_directory: '/some/directory',
},
extensions: {
ui: {
developer_mode: false,
},
},
},
});

await runnerInstance.exit();
});

describe('reloadAllExtensions', () => {
let runnerInstance;
let wsClient;
Expand Down

0 comments on commit 9181ccd

Please sign in to comment.