Add ability to set arbitrary arguments in a renderer process (#11850)

This commit is contained in:
Samuel Attard 2018-02-13 04:54:31 +11:00 committed by Charles Kerr
parent 66b57858b8
commit b3234f634b
6 changed files with 57 additions and 0 deletions

View file

@ -136,6 +136,17 @@ void WebContentsPreferences::AppendExtraCommandLineSwitches(
LOG(ERROR) << "preload url must be file:// protocol."; LOG(ERROR) << "preload url must be file:// protocol.";
} }
// Custom args for renderer process
base::Value* customArgs;
if ((web_preferences.Get(options::kCustomArgs, &customArgs))
&& (customArgs->is_list())) {
for (const base::Value& customArg : customArgs->GetList()) {
if (customArg.is_string()) {
command_line->AppendArg(customArg.GetString());
}
}
}
// Run Electron APIs and preload script in isolated world // Run Electron APIs and preload script in isolated world
bool isolated; bool isolated;
if (web_preferences.GetBoolean(options::kContextIsolation, &isolated) && if (web_preferences.GetBoolean(options::kContextIsolation, &isolated) &&

View file

@ -139,6 +139,8 @@ const char kNodeIntegrationInWorker[] = "nodeIntegrationInWorker";
// Enable the web view tag. // Enable the web view tag.
const char kWebviewTag[] = "webviewTag"; const char kWebviewTag[] = "webviewTag";
const char kCustomArgs[] = "additionalArguments";
} // namespace options } // namespace options
namespace switches { namespace switches {

View file

@ -68,6 +68,7 @@ extern const char kBlinkFeatures[];
extern const char kDisableBlinkFeatures[]; extern const char kDisableBlinkFeatures[];
extern const char kNodeIntegrationInWorker[]; extern const char kNodeIntegrationInWorker[];
extern const char kWebviewTag[]; extern const char kWebviewTag[];
extern const char kCustomArgs[];
} // namespace options } // namespace options

View file

@ -353,6 +353,9 @@ It creates a new `BrowserWindow` with native properties as set by the `options`.
script. You can use the `will-attach-webview` event on [webContents](web-contents.md) script. You can use the `will-attach-webview` event on [webContents](web-contents.md)
to strip away the `preload` script and to validate or alter the to strip away the `preload` script and to validate or alter the
`<webview>`'s initial settings. `<webview>`'s initial settings.
* `additionArguments` String[] (optional) - A list of strings that will be appended
to `process.argv` in the renderer process of this app. Useful for passing small
bits of data down to renderer process preload scripts.
When setting minimum or maximum window size with `minWidth`/`maxWidth`/ When setting minimum or maximum window size with `minWidth`/`maxWidth`/
`minHeight`/`maxHeight`, it only constrains the users. It won't prevent you from `minHeight`/`maxHeight`, it only constrains the users. It won't prevent you from

View file

@ -1063,6 +1063,42 @@ describe('BrowserWindow module', () => {
}) })
}) })
describe('"additionalArguments" option', () => {
it('adds extra args to process.argv in the renderer process', (done) => {
const preload = path.join(fixtures, 'module', 'check-arguments.js')
ipcMain.once('answer', (event, argv) => {
assert.ok(argv.includes('--my-magic-arg'))
done()
})
w.destroy()
w = new BrowserWindow({
show: false,
webPreferences: {
preload: preload,
additionalArguments: ['--my-magic-arg']
}
})
w.loadURL(`file://${path.join(fixtures, 'api', 'blank.html')}`)
})
it('adds extra value args to process.argv in the renderer process', (done) => {
const preload = path.join(fixtures, 'module', 'check-arguments.js')
ipcMain.once('answer', (event, argv) => {
assert.ok(argv.includes('--my-magic-arg=foo'))
done()
})
w.destroy()
w = new BrowserWindow({
show: false,
webPreferences: {
preload: preload,
additionalArguments: ['--my-magic-arg=foo']
}
})
w.loadURL(`file://${path.join(fixtures, 'api', 'blank.html')}`)
})
})
describe('"node-integration" option', () => { describe('"node-integration" option', () => {
it('disables node integration when specified to false', (done) => { it('disables node integration when specified to false', (done) => {
const preload = path.join(fixtures, 'module', 'send-later.js') const preload = path.join(fixtures, 'module', 'send-later.js')

View file

@ -0,0 +1,4 @@
var ipcRenderer = require('electron').ipcRenderer
window.onload = function () {
ipcRenderer.send('answer', process.argv)
}